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