chiark / gitweb /
test: udp-preload: Fix inet_ntop calling convention
[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 sendto_args int fd, const void *buf, size_t len, int flags, \
64                     const struct sockaddr *addr, socklen_t addrlen
65 #define setsockopt_args  int fd, int level, int optname, \
66                          const void *optval, socklen_t optlen
67 #define getsockname_args int fd, struct sockaddr *addr, socklen_t *addrlen
68 #define WRAPS(X)                                        \
69     X(socket,     int,     (domain,type,protocol))              \
70     X(close,      int,     (fd))                                        \
71     X(bind,       int,     (fd,addr,addrlen))                   \
72     X(sendto,     ssize_t, (fd,buf,len,flags,addr,addrlen))     \
73     X(setsockopt, int,     (fd,level,optname,optval,optlen))    \
74     X(getsockname,int,     (fd,addr,addrlen))
75
76 #define DEF_OLD(fn,rt,args)                             \
77   typedef rt fn##_fn_type(fn##_args);                   \
78   static rt find_##fn(fn##_args);                       \
79   static fn##_fn_type find_##fn, *old_##fn=find_##fn;   \
80   static rt find_##fn(fn##_args) {                      \
81     anyfn_type *anyfn;                                  \
82     anyfn= find_any(#fn); if (!anyfn) return -1;        \
83     old_##fn= (fn##_fn_type*)anyfn;                     \
84     return old_##fn args;                               \
85   }
86
87 WRAPS(DEF_OLD)
88
89 #define WRAP(fn) int fn(fn##_args)
90 #define TWRAP(fn) fn(fn##_args)
91
92 typedef struct{
93     int af;
94 } fdinfo;
95 static fdinfo **table;
96 static int tablesz;
97
98 static fdinfo *lookup(int fd) {
99     if (fd<0 || fd>=tablesz) return 0;
100     return table[fd];
101 }
102
103 #define ADDRPORTSTRLEN (INET6_ADDRSTRLEN+1+5) /* not including nul */
104
105 static int addrport2str(char buf[ADDRPORTSTRLEN+1],
106                         const struct sockaddr *addr, socklen_t addrlen) {
107     const void *addrv=addr;
108     const void *iav;
109     const struct sockaddr_in  *sin;
110     const struct sockaddr_in6 *sin6;
111     uint16_t port;
112     socklen_t el;
113     switch (addr->sa_family) {
114     case AF_INET:  sin =addrv; el=sizeof(*sin ); iav=&sin ->sin_addr ; port=sin ->sin_port ; break;
115     case AF_INET6: sin6=addrv; el=sizeof(*sin6); iav=&sin6->sin6_addr; port=sin6->sin6_port; break;
116     default: errno=ESRCH; return -1;
117     }
118 //fprintf(stderr,"af=%lu el=%lu addrlen=%lu\n",
119 //      (unsigned long)addr->sa_family,
120 //      (unsigned long)el,
121 //      (unsigned long)addrlen);
122     if (addrlen!=el) { errno=EINVAL; return -1; }
123     char *p=buf;
124     if (!inet_ntop(addr->sa_family,iav,p,INET6_ADDRSTRLEN)) return -1;
125     p+=strlen(p);
126     sprintf(p,",%u",(unsigned)ntohs(port));
127     return 0;
128 }
129
130 static int str2addrport(char *str,
131                         struct sockaddr *addr, socklen_t *addrlen) {
132     union {
133         struct sockaddr_in  sin;
134         struct sockaddr_in6 sin6;
135     } si;
136
137     memset(&si,0,sizeof(si));
138
139     int af;
140     void *iav;
141     uint16_t *portp;
142     socklen_t al;
143     switch (str[strcspn(str,".:")]) {
144     case '.': af=AF_INET ; iav=&si.sin .sin_addr ; al=sizeof(si.sin ); portp=&si.sin .sin_port ; break;
145     case ':': af=AF_INET6; iav=&si.sin6.sin6_addr; al=sizeof(si.sin6); portp=&si.sin6.sin6_port; break;
146     default: errno=ESRCH; return -1;
147     }
148     si.sin.sin_family=af;
149
150     char *comma=strchr(str,',');
151     if (!comma) { errno=ESRCH; return -1; }
152     *comma++=0;
153     int r=inet_pton(af,str,iav);
154 //fprintf(stderr,"inet_pton(%d,\"%s\",)=%d\n",af,str,r);
155     if (r<0) return -1;
156     if (r==0) { errno=ENOTTY; return -1; }
157
158     char *ep;
159     errno=0;
160     unsigned long port=strtoul(comma,&ep,10);
161     if (ep==comma || *ep || errno || port>65536) { errno=ESRCH; return -1; }
162     *portp= htons(port);
163
164     if (addr) memcpy(addr,&si, *addrlen<al ? *addrlen : al);
165     *addrlen=al;
166     return 0;
167 }
168
169 static char *sun_prep(struct sockaddr_un *sun) {
170     const char *dir=getenv("UDP_PRELOAD_DIR");
171     if (!dir) { errno=ECHILD; return 0; }
172
173     memset(sun,0,sizeof(*sun));
174     sun->sun_family=AF_UNIX;
175     int dl = strlen(dir);
176     if (dl + 1 + ADDRPORTSTRLEN + 1 > sizeof(sun->sun_path)) {
177         errno=ENAMETOOLONG; return 0;
178     }
179     strcpy(sun->sun_path,dir);
180     char *p=sun->sun_path+dl;
181     *p++='/';
182     return p;
183 }
184
185 WRAP(socket) {
186     if (!((domain==AF_INET || domain==AF_INET6) &&
187           type==SOCK_DGRAM))
188         return old_socket(domain,type,protocol);
189     int fd=socket(AF_UNIX,SOCK_DGRAM,0);
190     if (fd<0) return fd;
191     if (fd>=tablesz) {
192         int newsz=(fd+1)*2;
193         table=realloc(table,newsz*sizeof(*table));
194         if (!table) goto fail;
195         while (tablesz<newsz) table[tablesz++]=0;
196     }
197     free(table[fd]);
198     table[fd]=malloc(sizeof(*table[fd]));
199     if (!table[fd]) goto fail;
200     table[fd]->af=domain;
201     return fd;
202
203  fail:
204     close(fd);
205     return -1;
206 }
207
208 WRAP(close) {
209     if (fd>=0 && fd<tablesz) {
210         free(table[fd]);
211         table[fd]=0;
212     }
213     return old_close(fd);
214 }
215
216 WRAP(bind) {
217     fdinfo *ent=lookup(fd);
218     if (!ent) return old_bind(fd,addr,addrlen);
219     struct sockaddr_un sun;
220     char *p=sun_prep(&sun);
221     if (addrport2str(p,addr,addrlen)) return -1;
222 //fprintf(stderr,"binding %s\n",sun.sun_path);
223     if (unlink(sun.sun_path) && errno!=ENOENT) return -1;
224     return old_bind(fd,(const void*)&sun,sizeof(sun));
225 }
226
227 WRAP(setsockopt) {
228     fdinfo *ent=lookup(fd);
229     if (!ent) return old_setsockopt(fd,level,optname,optval,optlen);
230     if (ent->af==AF_INET6 && level==IPPROTO_IPV6 && optname==IPV6_V6ONLY
231         && optlen==sizeof(int) && *(int*)optval==1) {
232         return 0;
233     }
234     errno=ENOTTY;
235     return -1;
236 }
237
238 WRAP(getsockname) {
239     fdinfo *ent=lookup(fd);
240     if (!ent) return old_getsockname(fd,addr,addrlen);
241     struct sockaddr_un sun;
242     socklen_t sunlen=sizeof(sun);
243     if (old_getsockname(fd,(void*)&sun,&sunlen)) return -1;
244     if (sun.sun_family!=AF_UNIX || sunlen>sizeof(sun)) {
245 //fprintf(stderr,"old_getsockname af=%lu sunlen=%lu\n",
246 //      (unsigned long)sun.sun_family,
247 //      (unsigned long)sunlen);
248         errno=EDOM; return -1;
249     }
250     char *slash=strrchr(sun.sun_path,'/');
251     if (str2addrport(slash ? slash+1 : sun.sun_path,
252                      addr,addrlen)) return -1;
253     return 0;
254 }
255
256 ssize_t TWRAP(sendto) {
257     fdinfo *ent=lookup(fd);
258     if (!ent) return old_sendto(fd,buf,len,flags,addr,addrlen);
259
260     if (flags) { errno=ENOEXEC; return -1; }
261
262     const char *leaf=getenv("UDP_PRELOAD_SERVER");
263     if (!leaf) leaf="udp";
264     if (strlen(leaf) > ADDRPORTSTRLEN) { errno=ENAMETOOLONG; return -1; }
265     struct sockaddr_un sun;
266     char *p=sun_prep(&sun);
267     strcpy(p,leaf);
268
269     char tbuf[ADDRPORTSTRLEN+1];
270     memset(tbuf,0,sizeof(tbuf));
271     if (addrport2str(tbuf,addr,addrlen)) return -1;
272
273     struct iovec iov[2];
274     iov[0].iov_base=tbuf;
275     iov[0].iov_len=sizeof(tbuf);
276     iov[1].iov_base=(void*)buf;
277     iov[1].iov_len=len;
278     
279     struct msghdr m;
280     memset(&m,0,sizeof(m));
281     m.msg_name=&sun;
282     m.msg_namelen=sizeof(sun);
283     m.msg_iov=iov;
284     m.msg_iovlen=2;
285
286     return sendmsg(fd,&m,0);
287 }