d283e12f2378ade196043a87a42d80caf458162b
[bbaumbach/samba-autobuild/.git] / source4 / lib / socket_wrapper / socket_wrapper.c
1 /* 
2    Socket wrapper library. Passes all socket communication over 
3    unix domain sockets if the environment variable SOCKET_WRAPPER_DIR 
4    is set.
5    Copyright (C) Jelmer Vernooij 2005
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #ifdef _SAMBA_BUILD_
23 #include "includes.h"
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #else
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/socket.h>
30 #include <errno.h>
31 #include <sys/un.h>
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <stdio.h>
38 #endif
39 #include "dlinklist.h"
40
41 /* LD_PRELOAD doesn't work yet, so REWRITE_CALLS is all we support
42  * for now */
43 #define REWRITE_CALLS 
44
45 #ifdef REWRITE_CALLS
46 #define real_accept accept
47 #define real_connect connect
48 #define real_bind bind
49 #define real_getpeername getpeername
50 #define real_getsockname getsockname
51 #define real_getsockopt getsockopt
52 #define real_setsockopt setsockopt
53 #define real_recvfrom recvfrom
54 #define real_sendto sendto
55 #define real_socket socket
56 #define real_close close
57 #endif
58
59 static struct sockaddr *sockaddr_dup(const void *data, socklen_t len)
60 {
61         struct sockaddr *ret = (struct sockaddr *)malloc(len);
62         memcpy(ret, data, len);
63         return ret;
64 }
65
66 struct socket_info
67 {
68         int fd;
69
70         int domain;
71         int type;
72         int protocol;
73         int bound;
74
75         char *path;
76         char *tmp_path;
77
78         struct sockaddr *myname;
79         socklen_t myname_len;
80
81         struct sockaddr *peername;
82         socklen_t peername_len;
83
84         struct socket_info *prev, *next;
85 };
86
87 static struct socket_info *sockets = NULL;
88
89 static int convert_un_in(const struct sockaddr_un *un, struct sockaddr_in *in, socklen_t *len)
90 {
91         unsigned int prt;
92         const char *p;
93         int type;
94
95         if ((*len) < sizeof(struct sockaddr_in)) {
96                 return 0;
97         }
98
99         in->sin_family = AF_INET;
100         in->sin_port = htons(1025); /* Default to 1025 */
101         p = strrchr(un->sun_path, '/');
102         if (p) p++; else p = un->sun_path;
103
104         if (sscanf(p, "sock_ip_%d_%u", &type, &prt) == 2) {
105                 in->sin_port = htons(prt);
106         }
107         in->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
108         *len = sizeof(struct sockaddr_in);
109         return 0;
110 }
111
112 static int convert_in_un(struct socket_info *si, const struct sockaddr_in *in, struct sockaddr_un *un)
113 {
114         int type = si->type;
115         uint16_t prt = ntohs(in->sin_port);
116         if (prt == 0) {
117                 struct stat st;
118                 /* handle auto-allocation of ephemeral ports */
119                 prt = 5000;
120                 do {
121                         snprintf(un->sun_path, sizeof(un->sun_path), "%s/sock_ip_%d_%u", 
122                                  getenv("SOCKET_WRAPPER_DIR"), type, ++prt);
123                 } while (stat(un->sun_path, &st) == 0 && prt < 10000);
124                 ((struct sockaddr_in *)si->myname)->sin_port = htons(prt);
125         } 
126         snprintf(un->sun_path, sizeof(un->sun_path), "%s/sock_ip_%d_%u", 
127                  getenv("SOCKET_WRAPPER_DIR"), type, prt);
128         return 0;
129 }
130
131 static struct socket_info *find_socket_info(int fd)
132 {
133         struct socket_info *i;
134         for (i = sockets; i; i = i->next) {
135                 if (i->fd == fd) 
136                         return i;
137         }
138
139         return NULL;
140 }
141
142 static int sockaddr_convert_to_un(struct socket_info *si, const struct sockaddr *in_addr, socklen_t in_len, 
143                                          struct sockaddr_un *out_addr)
144 {
145         if (!out_addr)
146                 return 0;
147
148         out_addr->sun_family = AF_UNIX;
149
150         switch (in_addr->sa_family) {
151         case AF_INET:
152                 return convert_in_un(si, (const struct sockaddr_in *)in_addr, out_addr);
153         case AF_UNIX:
154                 memcpy(out_addr, in_addr, sizeof(*out_addr));
155                 return 0;
156         default:
157                 break;
158         }
159         
160         errno = EAFNOSUPPORT;
161         return -1;
162 }
163
164 static int sockaddr_convert_from_un(const struct socket_info *si, 
165                                     const struct sockaddr_un *in_addr, 
166                                     socklen_t un_addrlen,
167                                     int family,
168                                     struct sockaddr *out_addr,
169                                     socklen_t *out_len)
170 {
171         if (out_addr == NULL || out_len == NULL) 
172                 return 0;
173
174         if (un_addrlen == 0) {
175                 *out_len = 0;
176                 return 0;
177         }
178
179         switch (family) {
180         case AF_INET:
181                 return convert_un_in(in_addr, (struct sockaddr_in *)out_addr, out_len);
182         case AF_UNIX:
183                 memcpy(out_addr, in_addr, sizeof(*in_addr));
184                 *out_len = sizeof(*in_addr);
185                 return 0;
186         default:
187                 break;
188         }
189
190         errno = EAFNOSUPPORT;
191         return -1;
192 }
193
194 int swrap_socket(int domain, int type, int protocol)
195 {
196         struct socket_info *si;
197         int fd;
198
199         if (!getenv("SOCKET_WRAPPER_DIR")) {
200                 return real_socket(domain, type, protocol);
201         }
202         
203         fd = real_socket(AF_UNIX, type, 0);
204
205         if (fd == -1) return -1;
206
207         si = calloc(1, sizeof(struct socket_info));
208
209         si->domain = domain;
210         si->type = type;
211         si->protocol = protocol;
212         si->fd = fd;
213
214         DLIST_ADD(sockets, si);
215
216         return si->fd;
217 }
218
219 int swrap_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
220 {
221         struct socket_info *parent_si, *child_si;
222         int fd;
223         socklen_t un_addrlen = sizeof(struct sockaddr_un);
224         struct sockaddr_un un_addr;
225         int ret;
226
227         parent_si = find_socket_info(s);
228         if (!parent_si) {
229                 return real_accept(s, addr, addrlen);
230         }
231
232         ret = real_accept(s, (struct sockaddr *)&un_addr, &un_addrlen);
233         if (ret == -1) return ret;
234
235         fd = ret;
236
237         ret = sockaddr_convert_from_un(parent_si, &un_addr, un_addrlen,
238                                        parent_si->domain, addr, addrlen);
239         if (ret == -1) return ret;
240
241         child_si = malloc(sizeof(struct socket_info));
242         memset(child_si, 0, sizeof(*child_si));
243
244         child_si->fd = fd;
245         child_si->bound = 1;
246
247         child_si->myname_len = parent_si->myname_len;
248         child_si->myname = sockaddr_dup(parent_si->myname, parent_si->myname_len);
249
250         child_si->peername_len = *addrlen;
251         child_si->peername = sockaddr_dup(addr, *addrlen);
252
253         DLIST_ADD(sockets, child_si);
254
255         return fd;
256 }
257
258 /* using sendto() or connect() on an unbound socket would give the
259    recipient no way to reply, as unlike UDP and TCP, a unix domain
260    socket can't auto-assign emphemeral port numbers, so we need to
261    assign it here */
262 static int swrap_auto_bind(struct socket_info *si)
263 {
264         struct sockaddr_un un_addr;
265         struct sockaddr_in in;
266         int i;
267         
268         un_addr.sun_family = AF_UNIX;
269         
270         for (i=0;i<1000;i++) {
271                 snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), 
272                          "%s/sock_ip_%u_%u", getenv("SOCKET_WRAPPER_DIR"), 
273                          SOCK_DGRAM, i + 10000);
274                 if (bind(si->fd, (struct sockaddr *)&un_addr, 
275                          sizeof(un_addr)) == 0) {
276                         si->tmp_path = strdup(un_addr.sun_path);
277                         si->bound = 1;
278                         break;
279                 }
280         }
281         if (i == 1000) {
282                 return -1;
283         }
284         
285         memset(&in, 0, sizeof(in));
286         in.sin_family = AF_INET;
287         in.sin_port   = htons(i);
288         in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
289         
290         si->myname_len = sizeof(in);
291         si->myname = sockaddr_dup(&in, si->myname_len);
292         si->bound = 1;
293         return 0;
294 }
295
296
297 int swrap_connect(int s, const struct sockaddr *serv_addr, socklen_t addrlen)
298 {
299         int ret;
300         struct sockaddr_un un_addr;
301         struct socket_info *si = find_socket_info(s);
302
303         if (!si) {
304                 return real_connect(s, serv_addr, addrlen);
305         }
306
307         /* only allow pseudo loopback connections */
308         if (serv_addr->sa_family == AF_INET &&
309                 ((const struct sockaddr_in *)serv_addr)->sin_addr.s_addr != 
310             htonl(INADDR_LOOPBACK)) {
311                 errno = ENETUNREACH;
312                 return -1;
313         }
314
315         if (si->bound == 0 && si->domain != AF_UNIX) {
316                 ret = swrap_auto_bind(si);
317                 if (ret == -1) return -1;
318         }
319
320         ret = sockaddr_convert_to_un(si, (const struct sockaddr *)serv_addr, addrlen, &un_addr);
321         if (ret == -1) return -1;
322
323         ret = real_connect(s, (struct sockaddr *)&un_addr, 
324                            sizeof(struct sockaddr_un));
325
326         if (ret == 0) {
327                 si->peername_len = addrlen;
328                 si->peername = sockaddr_dup(serv_addr, addrlen);
329         }
330
331         return ret;
332 }
333
334 int swrap_bind(int s, const struct sockaddr *myaddr, socklen_t addrlen)
335 {
336         int ret;
337         struct sockaddr_un un_addr;
338         struct socket_info *si = find_socket_info(s);
339
340         if (!si) {
341                 return real_bind(s, myaddr, addrlen);
342         }
343
344         si->myname_len = addrlen;
345         si->myname = sockaddr_dup(myaddr, addrlen);
346
347         if (myaddr->sa_family == AF_INET &&
348             ((const struct sockaddr_in *)myaddr)->sin_addr.s_addr == 0) {
349                 ((struct sockaddr_in *)si->myname)->sin_addr.s_addr = 
350                         htonl(INADDR_LOOPBACK);
351         }
352         ret = sockaddr_convert_to_un(si, (const struct sockaddr *)myaddr, addrlen, &un_addr);
353         if (ret == -1) return -1;
354
355         unlink(un_addr.sun_path);
356
357         ret = real_bind(s, (struct sockaddr *)&un_addr,
358                         sizeof(struct sockaddr_un));
359
360         if (ret == 0) {
361                 si->bound = 1;
362         }
363
364         return ret;
365 }
366
367 int swrap_getpeername(int s, struct sockaddr *name, socklen_t *addrlen)
368 {
369         struct socket_info *si = find_socket_info(s);
370
371         if (!si) {
372                 return real_getpeername(s, name, addrlen);
373         }
374
375         if (!si->peername) 
376         {
377                 errno = ENOTCONN;
378                 return -1;
379         }
380
381         memcpy(name, si->peername, si->peername_len);
382         *addrlen = si->peername_len;
383
384         return 0;
385 }
386
387 int swrap_getsockname(int s, struct sockaddr *name, socklen_t *addrlen)
388 {
389         struct socket_info *si = find_socket_info(s);
390
391         if (!si) {
392                 return real_getsockname(s, name, addrlen);
393         }
394
395         memcpy(name, si->myname, si->myname_len);
396         *addrlen = si->myname_len;
397
398         return 0;
399 }
400
401 int swrap_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
402 {
403         struct socket_info *si = find_socket_info(s);
404
405         if (!si) {
406                 return real_getsockopt(s, level, optname, optval, optlen);
407         }
408
409         if (level == SOL_SOCKET) {
410                 return real_getsockopt(s, level, optname, optval, optlen);
411         } 
412
413         switch (si->domain) {
414         case AF_UNIX:
415                 return real_getsockopt(s, level, optname, optval, optlen);
416         default:
417                 errno = ENOPROTOOPT;
418                 return -1;
419         }
420 }
421
422 int swrap_setsockopt(int s, int  level,  int  optname,  const  void  *optval, socklen_t optlen)
423 {
424         struct socket_info *si = find_socket_info(s);
425
426         if (!si) {
427                 return real_setsockopt(s, level, optname, optval, optlen);
428         }
429
430         if (level == SOL_SOCKET) {
431                 return real_setsockopt(s, level, optname, optval, optlen);
432         }
433
434         switch (si->domain) {
435         case AF_UNIX:
436                 return real_setsockopt(s, level, optname, optval, optlen);
437         case AF_INET:
438                 /* Silence some warnings */
439 #ifdef TCP_NODELAY
440                 if (optname == TCP_NODELAY) 
441                         return 0;
442 #endif
443         default:
444                 errno = ENOPROTOOPT;
445                 return -1;
446         }
447 }
448
449 ssize_t swrap_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
450 {
451         struct sockaddr_un un_addr;
452         socklen_t un_addrlen = sizeof(un_addr);
453         int ret;
454         struct socket_info *si = find_socket_info(s);
455
456         if (!si) {
457                 return real_recvfrom(s, buf, len, flags, from, fromlen);
458         }
459
460         ret = real_recvfrom(s, buf, len, flags, (struct sockaddr *)&un_addr, &un_addrlen);
461         if (ret == -1) 
462                 return ret;
463
464         if (sockaddr_convert_from_un(si, &un_addr, un_addrlen,
465                                      si->domain, from, fromlen) == -1) {
466                 return -1;
467         }
468         
469         return ret;
470 }
471
472
473 ssize_t swrap_sendto(int  s,  const  void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
474 {
475         struct sockaddr_un un_addr;
476         int ret;
477         struct socket_info *si = find_socket_info(s);
478
479         if (!si) {
480                 return real_sendto(s, buf, len, flags, to, tolen);
481         }
482
483         if (si->bound == 0 && si->domain != AF_UNIX) {
484                 ret = swrap_auto_bind(si);
485                 if (ret == -1) return -1;
486         }
487
488         ret = sockaddr_convert_to_un(si, to, tolen, &un_addr);
489         if (ret == -1) return -1;
490
491         ret = real_sendto(s, buf, len, flags, (struct sockaddr *)&un_addr, sizeof(un_addr));
492
493         return ret;
494 }
495
496 int swrap_close(int fd)
497 {
498         struct socket_info *si = find_socket_info(fd);
499
500         if (si) {
501                 DLIST_REMOVE(sockets, si);
502
503                 free(si->path);
504                 free(si->myname);
505                 free(si->peername);
506                 if (si->tmp_path) {
507                         unlink(si->tmp_path);
508                         free(si->tmp_path);
509                 }
510                 free(si);
511         }
512
513         return real_close(fd);
514 }