2 * helper.c - setuid helper program for authbind
4 * authbind is Copyright (C) 1998 Ian Jackson
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)
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.
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.
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
35 static void exiterrno(int e) {
36 exit(e>0 && e<128 ? e : ENOSYS);
39 static void perrorfail(const char *m) {
42 fprintf(stderr,"libauthbind's helper: %s: %s\n",m,strerror(e));
46 static void badusage(void) {
47 fprintf(stderr,"libauthbind's helper: bad usage\n");
51 static struct sockaddr_in saddr4;
52 static struct sockaddr_in6 saddr6;
54 static struct sockaddr *saddr_any;
55 static const void *addr_any;
56 static size_t saddrlen_any, addrlen_any;
58 static void authorised(void) {
59 if (bind(0,saddr_any,saddrlen_any)) exiterrno(errno);
63 static void hex2bytes(const char *string, unsigned char *out, int len) {
65 for (i=0; i<len; i++) {
67 hex[0]= *string++; if (!hex[0]) badusage();
68 hex[1]= *string++; if (!hex[1]) badusage();
70 *out++ = strtoul(hex,&ep,16);
71 if (ep != &hex[2]) badusage();
75 int main(int argc, const char *const *argv) {
80 const char *tophalfchar="";
81 unsigned long port, addr4=0, haddr4=0;
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();
95 } else if (argc == 4 && !strcmp(argv[3],"6")) {
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));
108 port= strtoul(argv[2],&ep,16); if (*ep || port&~0x0ffffUL) badusage();
110 if (hport >= IPPORT_RESERVED/2) tophalfchar= "!";
112 if (chdir(CONFIGDIR)) perrorfail("chdir " CONFIGDIR);
114 fnbuf[sizeof(fnbuf)-1]= 0;
118 saddr4.sin_family= af;
119 saddr4.sin_port= port;
120 saddr4.sin_addr.s_addr= addr4;
123 saddr6.sin6_family= af;
124 saddr6.sin6_port= port;
130 snprintf(fnbuf,sizeof(fnbuf)-1,"byport/%s%u",tophalfchar,hport);
131 if (!access(fnbuf,X_OK)) authorised();
132 if (errno != ENOENT) exiterrno(errno);
134 char npbuf[INET_ADDRSTRLEN + INET6_ADDRSTRLEN];
135 np= inet_ntop(af,addr_any,npbuf,sizeof(npbuf));
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);
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);
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);
154 uid= getuid(); if (uid==(uid_t)-1) perrorfail("getuid");
155 snprintf(fnbuf,sizeof(fnbuf)-1,"byuid/%s%lu",tophalfchar,(unsigned long)uid);
157 file= fopen(fnbuf,"r");
158 if (!file) exiterrno(errno==ENOENT ? EPERM : errno);
160 while (fgets(fnbuf,sizeof(fnbuf)-1,file)) {
161 unsigned int a1,a2,a3,a4, alen,pmin,pmax;
165 (sscanf(fnbuf," %u.%u.%u.%u/%u: %u,%u %n",
166 &a1,&a2,&a3,&a4,&alen,&pmin,&pmax,&nchar),
167 nchar == strlen(fnbuf))) {
169 if (alen>32 || pmin&~0x0ffff || pmax&~0x0ffff ||
170 a1&~0x0ff || a2&~0xff || a3&~0x0ff || a4&~0x0ff)
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;
180 char *comma = strchr(fnbuf,',');
184 char *hyphen = strchr(fnbuf,'-');
185 const char *min, *max;
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)
199 if (memcmp(addr_any,minaddr,addrlen_any) < 0 ||
200 memcmp(addr_any,maxaddr,addrlen_any) > 0)
203 sscanf(comma," %u-%u %n",
205 if (nchar != strlen(comma))
209 if (hport<pmin || hport>pmax) continue;
213 if (ferror(file)) perrorfail("read per-uid file");