chiark / gitweb /
test: udp-preload: Introduce sun_prep
[secnet.git] / test / udp-preload.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 #define _GNU_SOURCE
23
24 #include <dlfcn.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <sys/socket.h>
33 #include <sys/wait.h>
34 #include <netinet/in.h>
35
36 #include <sys/socket.h>
37 #include <sys/un.h>
38 #include <arpa/inet.h>
39
40 #define STDERRSTR_CONST(m) write(2,m,sizeof(m)-1)
41 #define STDERRSTR_STRING(m) write(2,m,strlen(m))
42
43 typedef void anyfn_type(void);
44
45 static anyfn_type *find_any(const char *name) {
46   static const char *dlerr;
47   anyfn_type *kv;
48
49   kv= dlsym(RTLD_NEXT,name); if (kv) return kv;
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 0;
58 }
59
60 #define socket_args int domain, int type, int protocol
61 #define close_args  int fd
62 #define bind_args   int fd, const struct sockaddr *addr, socklen_t addrlen
63 #define setsockopt_args  int fd, int level, int optname, \
64                          const void *optval, socklen_t optlen
65 #define getsockname_args int fd, struct sockaddr *addr, socklen_t *addrlen
66 #define WRAPS(X)                                        \
67     X(socket,     (domain,type,protocol))               \
68     X(close,      (fd))                                 \
69     X(bind,       (fd,addr,addrlen))                    \
70     X(setsockopt, (fd,level,optname,optval,optlen))     \
71     X(getsockname,(fd,addr,addrlen))
72
73 #define DEF_OLD(fn,args)                                \
74   typedef int fn##_fn_type(fn##_args);                  \
75   static int find_##fn(fn##_args);                      \
76   static fn##_fn_type find_##fn, *old_##fn=find_##fn;   \
77   static int find_##fn(fn##_args) {                     \
78     anyfn_type *anyfn;                                  \
79     anyfn= find_any(#fn); if (!anyfn) return -1;        \
80     old_##fn= (fn##_fn_type*)anyfn;                     \
81     return old_##fn args;                               \
82   }
83
84 WRAPS(DEF_OLD)
85
86 #define WRAP(fn) int fn(fn##_args)
87
88 typedef struct{
89     int af;
90 } fdinfo;
91 static fdinfo **table;
92 static int tablesz;
93
94 static fdinfo *lookup(int fd) {
95     if (fd>=tablesz) return 0;
96     return table[fd];
97 }
98
99 #define ADDRPORTSTRLEN (INET6_ADDRSTRLEN+1+5) /* not including nul */
100
101 static int addrport2str(char buf[ADDRPORTSTRLEN+1],
102                         const struct sockaddr *addr, socklen_t addrlen) {
103     const void *addrv=addr;
104     const void *iav;
105     const struct sockaddr_in  *sin;
106     const struct sockaddr_in6 *sin6;
107     uint16_t port;
108     socklen_t el;
109     switch (addr->sa_family) {
110     case AF_INET:  sin =addrv; el=sizeof(*sin ); iav=&sin ->sin_addr ; port=sin ->sin_port ; break;
111     case AF_INET6: sin6=addrv; el=sizeof(*sin6); iav=&sin6->sin6_addr; port=sin6->sin6_port; break;
112     default: errno=ESRCH; return -1;
113     }
114 //fprintf(stderr,"af=%lu el=%lu addrlen=%lu\n",
115 //      (unsigned long)addr->sa_family,
116 //      (unsigned long)el,
117 //      (unsigned long)addrlen);
118     if (addrlen!=el) { errno=EINVAL; return -1; }
119     char *p=buf;
120     if (!inet_ntop(addr->sa_family,iav,p,INET6_ADDRSTRLEN)) return -1;
121     p+=strlen(p);
122     sprintf(p,",%u",(unsigned)ntohs(port));
123     return 0;
124 }
125
126 static int str2addrport(char *str,
127                         struct sockaddr *addr, socklen_t *addrlen) {
128     union {
129         struct sockaddr_in  sin;
130         struct sockaddr_in6 sin6;
131     } si;
132
133     memset(&si,0,sizeof(si));
134
135     int af;
136     void *iav;
137     uint16_t *portp;
138     socklen_t al;
139     switch (str[strcspn(str,".:")]) {
140     case '.': af=AF_INET ; iav=&si.sin .sin_addr ; al=sizeof(si.sin ); portp=&si.sin .sin_port ; break;
141     case ':': af=AF_INET6; iav=&si.sin6.sin6_addr; al=sizeof(si.sin6); portp=&si.sin6.sin6_port; break;
142     default: errno=ESRCH; return -1;
143     }
144     si.sin.sin_family=af;
145
146     char *comma=strchr(str,',');
147     if (!comma) { errno=ESRCH; return -1; }
148     *comma++=0;
149     if (inet_pton(af,str,iav)) return -1;
150
151     char *ep;
152     errno=0;
153     unsigned long port=strtoul(comma,&ep,10);
154     if (ep==comma || *ep || errno || port>65536) { errno=ESRCH; return -1; }
155     *portp= htons(port);
156
157     if (addr) memcpy(addr,&si, *addrlen<al ? *addrlen : al);
158     *addrlen=al;
159     return 0;
160 }
161
162 static char *sun_prep(struct sockaddr_un *sun) {
163     const char *dir=getenv("UDP_PRELOAD_DIR");
164     if (!dir) { errno=ECHILD; return 0; }
165
166     memset(sun,0,sizeof(*sun));
167     sun->sun_family=AF_UNIX;
168     int dl = strlen(dir);
169     if (dl + 1 + ADDRPORTSTRLEN + 1 > sizeof(sun->sun_path)) {
170         errno=ENAMETOOLONG; return 0;
171     }
172     strcpy(sun->sun_path,dir);
173     char *p=sun->sun_path+dl;
174     *p++='/';
175     return p;
176 }
177
178 WRAP(socket) {
179     if (!((domain==AF_INET || domain==AF_INET6) &&
180           type==SOCK_DGRAM))
181         return old_socket(domain,type,protocol);
182     int fd=socket(AF_UNIX,SOCK_DGRAM,0);
183     if (fd<0) return fd;
184     if (fd>=tablesz) {
185         int newsz=(fd+1)*2;
186         table=realloc(table,newsz*sizeof(*table));
187         if (!table) goto fail;
188         while (tablesz<newsz) table[tablesz++]=0;
189     }
190     free(table[fd]);
191     table[fd]=malloc(sizeof(*table[fd]));
192     if (!table[fd]) goto fail;
193     table[fd]->af=domain;
194     return fd;
195
196  fail:
197     close(fd);
198     return -1;
199 }
200
201 WRAP(close) {
202     if (fd<tablesz) {
203         free(table[fd]);
204         table[fd]=0;
205     }
206     return old_close(fd);
207 }
208
209 WRAP(bind) {
210     fdinfo *ent=lookup(fd);
211     if (!ent) return old_bind(fd,addr,addrlen);
212     struct sockaddr_un sun;
213     char *p=sun_prep(&sun);
214     if (addrport2str(p,addr,addrlen)) return -1;
215 //fprintf(stderr,"binding %s\n",sun.sun_path);
216     if (unlink(sun.sun_path) && errno!=ENOENT) return -1;
217     return old_bind(fd,(const void*)&sun,sizeof(sun));
218 }
219
220 WRAP(setsockopt) {
221     fdinfo *ent=lookup(fd);
222     if (!ent) return old_setsockopt(fd,level,optname,optval,optlen);
223     if (ent->af==AF_INET6 && level==IPPROTO_IPV6 && optname==IPV6_V6ONLY
224         && optlen==sizeof(int) && *(int*)optval==1) {
225         return 0;
226     }
227     errno=ENOTTY;
228     return -1;
229 }
230
231 WRAP(getsockname) {
232     fdinfo *ent=lookup(fd);
233     if (!ent) return old_getsockname(fd,addr,addrlen);
234     struct sockaddr_un sun;
235     socklen_t sunlen=sizeof(sun);
236     if (old_getsockname(fd,(void*)&sun,&sunlen)) return -1;
237     if (sun.sun_family!=AF_UNIX || sunlen>sizeof(sun)) {
238 //fprintf(stderr,"old_getsockname af=%lu sunlen=%lu\n",
239 //      (unsigned long)sun.sun_family,
240 //      (unsigned long)sunlen);
241         errno=EDOM; return -1;
242     }
243     if (str2addrport(sun.sun_path,addr,addrlen)) return -1;
244     return 0;
245 }