chiark / gitweb /
remove rcsids
[authbind.git] / helper.c
1 /*
2  *  helper.c - setuid helper program 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 <errno.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32
33 #ifndef CONFIGDIR
34 # define CONFIGDIR "/etc/authbind"
35 #endif
36
37 static void exiterrno(int e) {
38   exit(e>0 && e<128 ? e : ENOSYS);
39 }
40
41 static void perrorfail(const char *m) {
42   int e;
43   e= errno;
44   fprintf(stderr,"libauthbind's helper: %s: %s\n",m,strerror(e));
45   exiterrno(e);
46 }
47
48 static void badusage(void) {
49   fprintf(stderr,"libauthbind's helper: bad usage\n");
50   exit(ENOSYS);
51 }
52
53 static struct sockaddr_in saddr4;
54 static struct sockaddr_in6 saddr6;
55
56 static struct sockaddr *saddr_any;
57 static const void *addr_any;
58 static size_t saddrlen_any, addrlen_any;
59
60 static void authorised(void) {
61   if (bind(0,saddr_any,saddrlen_any)) exiterrno(errno);
62   else _exit(0);
63 }
64
65 static void hex2bytes(const char *string, unsigned char *out, int len) {
66   int i;
67   for (i=0; i<len; i++) {
68     char hex[3], *ep;
69     hex[0]= *string++;  if (!hex[0]) badusage();
70     hex[1]= *string++;  if (!hex[1]) badusage();
71     hex[2]= 0;
72     *out++ = strtoul(hex,&ep,16);
73     if (ep != &hex[2]) badusage();
74   }
75 }
76
77 int main(int argc, const char *const *argv) {
78   uid_t uid;
79   char fnbuf[300];
80   char *ep;
81   const char *np;
82   const char *tophalfchar="";
83   unsigned long port, addr4=0, haddr4=0;
84   unsigned int hport;
85   int af;
86   FILE *file;
87
88   if (argc == 3) {
89     af= AF_INET;
90     saddr_any= (void*)&saddr4;
91     saddrlen_any= sizeof(saddr4);
92     addr_any= &saddr4.sin_addr.s_addr;
93     addrlen_any= sizeof(saddr4.sin_addr.s_addr);
94     addr4= strtoul(argv[1],&ep,16);
95     if (*ep || addr4&~0x0ffffffffUL) badusage();
96     haddr4= ntohl(addr4);
97   } else if (argc == 4 && !strcmp(argv[3],"6")) {
98     af= AF_INET6;
99     saddr_any= (void*)&saddr6;
100     saddrlen_any= sizeof(saddr6);
101     addr_any= &saddr6.sin6_addr.s6_addr;
102     addrlen_any= sizeof(saddr6.sin6_addr.s6_addr);
103     hex2bytes(argv[1], saddr6.sin6_addr.s6_addr,
104               sizeof(saddr6.sin6_addr.s6_addr));
105   } else {
106     badusage();
107     abort();
108   }
109
110   port= strtoul(argv[2],&ep,16); if (*ep || port&~0x0ffffUL) badusage();
111   hport= htons(port);
112   if (hport >= IPPORT_RESERVED/2) tophalfchar= "!";
113
114   if (chdir(CONFIGDIR)) perrorfail("chdir " CONFIGDIR);
115
116   fnbuf[sizeof(fnbuf)-1]= 0;
117
118   switch (af) {
119   case AF_INET:
120     saddr4.sin_family= af;
121     saddr4.sin_port= port;
122     saddr4.sin_addr.s_addr= addr4;
123     break;
124   case AF_INET6:
125     saddr6.sin6_family= af;
126     saddr6.sin6_port= port;
127     break;
128   default:
129     abort();
130   }
131
132   snprintf(fnbuf,sizeof(fnbuf)-1,"byport/%s%u",tophalfchar,hport);
133   if (!access(fnbuf,X_OK)) authorised();
134   if (errno != ENOENT) exiterrno(errno);
135
136   char npbuf[INET_ADDRSTRLEN + INET6_ADDRSTRLEN];
137   np= inet_ntop(af,addr_any,npbuf,addrlen_any);
138   assert(np);
139
140   if (af == AF_INET) {
141     snprintf(fnbuf,sizeof(fnbuf)-1,"byaddr/%s%s:%u",np,tophalfchar,hport);
142     if (!access(fnbuf,X_OK)) authorised();
143     if (errno != ENOENT) exiterrno(errno);
144   }
145
146   snprintf(fnbuf,sizeof(fnbuf)-1,"byaddr/%s%s,%u",np,tophalfchar,hport);
147   if (!access(fnbuf,X_OK)) authorised();
148   if (errno != ENOENT) exiterrno(errno);
149
150   uid= getuid(); if (uid==(uid_t)-1) perrorfail("getuid");
151   snprintf(fnbuf,sizeof(fnbuf)-1,"byuid/%s%lu",tophalfchar,(unsigned long)uid);
152
153   file= fopen(fnbuf,"r");
154   if (!file) exiterrno(errno==ENOENT ? EPERM : errno);
155
156   while (fgets(fnbuf,sizeof(fnbuf)-1,file)) {
157     unsigned int a1,a2,a3,a4, alen,pmin,pmax;
158     int nchar= -1;
159
160     if (af == AF_INET &&
161         (sscanf(fnbuf," %u.%u.%u.%u/%u: %u,%u %n",
162                 &a1,&a2,&a3,&a4,&alen,&pmin,&pmax,&nchar),
163          nchar == strlen(fnbuf))) {
164
165       if (alen>32 || pmin&~0x0ffff || pmax&~0x0ffff ||
166           a1&~0x0ff || a2&~0xff || a3&~0x0ff || a4&~0x0ff)
167         continue;
168
169       unsigned long thaddr, thmask;
170       thaddr= (a1<<24)|(a2<<16)|(a3<<8)|(a4);
171       thmask= 0x0ffffffffUL<<(32-alen);
172       if ((haddr4&thmask) != thaddr) continue;
173
174     } else {
175
176       char *comma = strchr(fnbuf,',');
177       if (comma) continue;
178       *comma++ = '\0';
179
180       char *hyphen = strchr(fnbuf,'-');
181       const char *min, *max;
182       if (hyphen) {
183         *hyphen++ = '\0';
184         min = fnbuf;
185         max = hyphen;
186       } else {
187         min = fnbuf;
188         max = fnbuf;
189       }
190       unsigned char minaddr[addrlen_any];
191       unsigned char maxaddr[addrlen_any];
192       if (inet_pton(af,min,minaddr) != 1 ||
193           inet_pton(af,max,maxaddr) != 1)
194         continue;
195       if (memcmp(addr_any,minaddr,addrlen_any) < 0 ||
196           memcmp(addr_any,maxaddr,addrlen_any) > 0)
197         continue;
198
199       sscanf(comma," %u-%u %n",
200              &pmin,&pmax,&nchar);
201       if (nchar != strlen(comma))
202         continue;
203
204     }
205     if (hport<pmin || hport>pmax) continue;
206
207     authorised();
208   }
209   if (ferror(file)) perrorfail("read per-uid file");
210   _exit(ENOENT);
211 }