chiark / gitweb /
c8f34227388659c4743b8b706c77a269f34da761
[authbind.git] / libauthbind.c
1 /*
2  *  libauthbind.c - bind(2)-redirector library for authbind
3  *
4  *  authbind is Copyright (C) 1998 Ian Jackson
5  * 
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  * 
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  * 
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software Foundation,
18  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
19  * 
20  */
21
22 #include <dlfcn.h>
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/socket.h>
30 #include <sys/wait.h>
31 #include <netinet/in.h>
32
33 static const char *rcsid="$Id$";
34
35 #include "authbind.h"
36
37 typedef void anyfn_type(void);
38 typedef int bindfn_type(int fd, const struct sockaddr *addr, socklen_t addrlen);
39
40 #define STDERRSTR_CONST(m) write(2,m,sizeof(m)-1)
41 #define STDERRSTR_STRING(m) write(2,m,strlen(m))
42
43 static int find_any(const char *name, anyfn_type **keep) {
44   static const char *dlerr;
45   anyfn_type *kv;
46
47   if (*keep) return 0;
48   kv= dlsym(RTLD_NEXT,name);
49   if (kv) { *keep= kv; return 0; }
50   dlerr= dlerror(); if (!dlerr) dlerr= "dlsym() failed for no reason";
51   STDERRSTR_CONST("libauthbind: error finding original version of ");
52   STDERRSTR_STRING(name);
53   STDERRSTR_CONST(": ");
54   STDERRSTR_STRING(dlerr);
55   STDERRSTR_STRING("\n");
56   errno= ENOSYS;
57   return -1;
58 }
59
60 static bindfn_type find_bind, *old_bind= find_bind;
61
62 int find_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
63   anyfn_type *anyfn;
64   if (find_any("bind",&anyfn)) return -1;
65   old_bind= (bindfn_type*)anyfn;
66   return old_bind(fd,addr,addrlen);
67 }
68
69 static int exiterrno(int e) {
70   _exit(e>0 && e<128 ? e : -1);
71 }
72
73 static void removepreload(void) {
74   const char *myself, *found;
75   char *newval, *preload;
76   int lpreload, lmyself, before, after;
77
78   preload= getenv(PRELOAD_VAR);
79   myself= getenv(AUTHBINDLIB_VAR);
80   if (!myself || !preload) return;
81
82   lpreload= strlen(preload);
83   lmyself= strlen(myself);
84
85   if (lmyself < 1 || lpreload<lmyself) return;
86   if (lpreload==lmyself) {
87     if (!strcmp(preload,myself)) unsetenv(PRELOAD_VAR);
88     return;
89   }
90   if (!memcmp(preload,myself,lmyself) && preload[lmyself]==':') {
91     before= 0; after= lpreload-(lmyself+1);
92   } else if (!memcmp(preload+lpreload-lmyself,myself,lmyself) &&
93              preload[lpreload-(lmyself+1)]==':') {
94     before= lpreload-(lmyself+1); after= 0;
95   } else {
96     if (lpreload<lmyself+2) return;
97     found= preload+1;
98     for (;;) {
99       found= strstr(found,myself); if (!found) return;
100       if (found > preload+lpreload-(lmyself+1)) return;
101       if (found[-1]==':' && found[lmyself]==':') break;
102       found++;
103     }
104     before= found-preload;
105     after= lpreload-(before+lmyself+1);
106   }
107   newval= malloc(before+after+1);
108   if (newval) {
109     memcpy(newval,preload,before);
110     strcpy(newval+before,preload+lpreload-after);
111     if (setenv(PRELOAD_VAR,newval,1)) return;
112     free(newval);
113   }
114   strcpy(preload+before,preload+lpreload-after);
115   return;
116 }
117
118 int _init(void);
119 int _init(void) {
120   char *levels;
121   int levelno;
122
123   /* If AUTHBIND_LEVELS is
124    *  unset => always strip from preload
125    *  set and starts with `y' => never strip from preload, keep AUTHBIND_LEVELS
126    *  set to integer > 1 => do not strip now, subtract one from AUTHBIND_LEVELS
127    *  set to integer 1 => do not strip now, unset AUTHBIND_LEVELS
128    *  set to empty string or 0 => strip now, unset AUTHBIND_LEVELS
129    */
130   levels= getenv(AUTHBIND_LEVELS_VAR);
131   if (levels) {
132     if (levels[0]=='y') return 0;
133     levelno= atoi(levels);
134     if (levelno > 0) {
135       levelno--;
136       if (levelno > 0) sprintf(levels,"%d",levelno);
137       else unsetenv(AUTHBIND_LEVELS_VAR);
138       return 0;
139     }
140     unsetenv(AUTHBIND_LEVELS_VAR);
141   }
142   removepreload();
143   return 0;
144 }
145
146 int bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
147   pid_t child, rchild;
148   char portarg[5], addrarg[9];
149   int status;
150
151   if (addr->sa_family != AF_INET || addrlen != sizeof(struct sockaddr_in) ||
152       !geteuid() || ((struct sockaddr_in*)addr)->sin_port == 0 ||
153       ntohs(((struct sockaddr_in*)addr)->sin_port) >= IPPORT_RESERVED/2)
154     return old_bind(fd,addr,addrlen);
155
156   sprintf(addrarg,"%08lx",
157           ((unsigned long)(((struct sockaddr_in*)addr)->sin_addr.s_addr))&0x0ffffffffUL);
158   sprintf(portarg,"%04x",
159           ((unsigned int)(((struct sockaddr_in*)addr)->sin_port))&0x0ffff);
160
161   child= fork(); if (child==-1) return -1;
162
163   if (!child) {
164     if (dup2(fd,0)) exiterrno(errno);
165     removepreload();
166     execl(HELPER,HELPER,addrarg,portarg,(char*)0);
167     status= errno;
168     STDERRSTR_CONST("libauthbind: possible installation problem - "
169                     "could not invoke " HELPER " (");
170     STDERRSTR_STRING(rcsid);
171     STDERRSTR_CONST(")\n");
172     exiterrno(status);
173   }
174
175   rchild= waitpid(child,&status,0);
176   if (rchild==-1) return -1;
177   if (rchild!=child) { errno= ECHILD; return -1; }
178
179   if (WIFEXITED(status)) {
180     if (WEXITSTATUS(status)) { errno= WEXITSTATUS(status); return -1; }
181     return 0;
182   } else {
183     errno= ENOSYS;
184     return -1;
185   }
186 }