chiark / gitweb /
test: udp-preload: Provide close
[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 WRAP(socket) {
163     if (!((domain==AF_INET || domain==AF_INET6) &&
164           type==SOCK_DGRAM))
165         return old_socket(domain,type,protocol);
166     int fd=socket(AF_UNIX,SOCK_DGRAM,0);
167     if (fd<0) return fd;
168     if (fd>=tablesz) {
169         int newsz=(fd+1)*2;
170         table=realloc(table,newsz*sizeof(*table));
171         if (!table) goto fail;
172         while (tablesz<newsz) table[tablesz++]=0;
173     }
174     free(table[fd]);
175     table[fd]=malloc(sizeof(*table[fd]));
176     if (!table[fd]) goto fail;
177     table[fd]->af=domain;
178     return fd;
179
180  fail:
181     close(fd);
182     return -1;
183 }
184
185 WRAP(close) {
186     if (fd<tablesz) {
187         free(table[fd]);
188         table[fd]=0;
189     }
190     return old_close(fd);
191 }
192
193 WRAP(bind) {
194     fdinfo *ent=lookup(fd);
195     if (!ent) return old_bind(fd,addr,addrlen);
196     const char *dir = getenv("UDP_PRELOAD_DIR");
197     if (!dir) { errno=ECHILD; return -1; }
198     struct sockaddr_un sun;
199     memset(&sun,0,sizeof(sun));
200     sun.sun_family=AF_UNIX;
201     int dl = strlen(dir);
202     if (dl + 1 + ADDRPORTSTRLEN + 1 > sizeof(sun.sun_path)) {
203         errno=ENAMETOOLONG; return -1;
204     }
205     strcpy(sun.sun_path,dir);
206     char *p=sun.sun_path+dl;
207     *p++='/';
208     if (addrport2str(p,addr,addrlen)) return -1;
209 //fprintf(stderr,"binding %s\n",sun.sun_path);
210     if (unlink(sun.sun_path) && errno!=ENOENT) return -1;
211     return old_bind(fd,(const void*)&sun,sizeof(sun));
212 }
213
214 WRAP(setsockopt) {
215     fdinfo *ent=lookup(fd);
216     if (!ent) return old_setsockopt(fd,level,optname,optval,optlen);
217     if (ent->af==AF_INET6 && level==IPPROTO_IPV6 && optname==IPV6_V6ONLY
218         && optlen==sizeof(int) && *(int*)optval==1) {
219         return 0;
220     }
221     errno=ENOTTY;
222     return -1;
223 }
224
225 WRAP(getsockname) {
226     fdinfo *ent=lookup(fd);
227     if (!ent) return old_getsockname(fd,addr,addrlen);
228     struct sockaddr_un sun;
229     socklen_t sunlen=sizeof(sun);
230     if (old_getsockname(fd,(void*)&sun,&sunlen)) return -1;
231     if (sun.sun_family!=AF_UNIX || sunlen>sizeof(sun)) {
232 //fprintf(stderr,"old_getsockname af=%lu sunlen=%lu\n",
233 //      (unsigned long)sun.sun_family,
234 //      (unsigned long)sunlen);
235         errno=EDOM; return -1;
236     }
237     if (str2addrport(sun.sun_path,addr,addrlen)) return -1;
238     return 0;
239 }