Tidied up a few things in preparation for changes.
[rsync/rsync.git] / socket.c
1 /* -*- c-file-style: "linux" -*-
2
3    rsync -- fast file replication program
4
5    Copyright (C) 1992-2001 by Andrew Tridgell <tridge@samba.org>
6    Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /**
24  * @file socket.c
25  *
26  * Socket functions used in rsync.
27  *
28  * This file is now converted to use the new-style getaddrinfo()
29  * interface, which supports IPv6 but is also supported on recent
30  * IPv4-only machines.  On systems that don't have that interface, we
31  * emulate it using the KAME implementation.
32  **/
33
34 #include "rsync.h"
35
36
37 /**
38  * Establish a proxy connection on an open socket to a web proxy by
39  * using the HTTP CONNECT method.
40  **/
41 static int establish_proxy_connection(int fd, char *host, int port)
42 {
43         char buffer[1024];
44         char *cp;
45
46         snprintf(buffer, sizeof(buffer), "CONNECT %s:%d HTTP/1.0\r\n\r\n", host, port);
47         if (write(fd, buffer, strlen(buffer)) != (int)strlen(buffer)) {
48                 rprintf(FERROR, "failed to write to proxy: %s\n",
49                         strerror(errno));
50                 return -1;
51         }
52
53         for (cp = buffer; cp < &buffer[sizeof (buffer) - 1]; cp++) {
54                 if (read(fd, cp, 1) != 1) {
55                         rprintf(FERROR, "failed to read from proxy: %s\n",
56                                 strerror(errno));
57                         return -1;
58                 }
59                 if (*cp == '\n')
60                         break;
61         }
62
63         if (*cp != '\n')
64                 cp++;
65         *cp-- = '\0';
66         if (*cp == '\r')
67                 *cp = '\0';
68         if (strncmp(buffer, "HTTP/", 5) != 0) {
69                 rprintf(FERROR, "bad response from proxy - %s\n",
70                         buffer);
71                 return -1;
72         }
73         for (cp = &buffer[5]; isdigit(*(uchar*)cp) || *cp == '.'; cp++) {}
74         while (*cp == ' ')
75                 cp++;
76         if (*cp != '2') {
77                 rprintf(FERROR, "bad response from proxy - %s\n",
78                         buffer);
79                 return -1;
80         }
81         /* throw away the rest of the HTTP header */
82         while (1) {
83                 for (cp = buffer; cp < &buffer[sizeof (buffer) - 1];
84                      cp++) {
85                         if (read(fd, cp, 1) != 1) {
86                                 rprintf(FERROR, "failed to read from proxy: %s\n",
87                                         strerror(errno));
88                                 return -1;
89                         }
90                         if (*cp == '\n')
91                                 break;
92                 }
93                 if (cp > buffer && *cp == '\n')
94                         cp--;
95                 if (cp == buffer && (*cp == '\n' || *cp == '\r'))
96                         break;
97         }
98         return 0;
99 }
100
101
102 /**
103  * Try to set the local address for a newly-created socket.  Return -1
104  * if this fails.
105  **/
106 int try_bind_local(int s,
107                    int ai_family, int ai_socktype,
108                    const char *bind_address)
109 {
110         int error;
111         struct addrinfo bhints, *bres_all, *r;
112
113         memset(&bhints, 0, sizeof(bhints));
114         bhints.ai_family = ai_family;
115         bhints.ai_socktype = ai_socktype;
116         bhints.ai_flags = AI_PASSIVE;
117         if ((error = getaddrinfo(bind_address, NULL, &bhints, &bres_all))) {
118                 rprintf(FERROR, RSYNC_NAME ": getaddrinfo %s: %s\n",
119                         bind_address, gai_strerror(error));
120                 return -1;
121         }
122
123         for (r = bres_all; r; r = r->ai_next) {
124                 if (bind(s, r->ai_addr, r->ai_addrlen) == -1)
125                         continue;
126                 freeaddrinfo(bres_all);
127                 return s;
128         }
129
130         /* no error message; there might be some problem that allows
131          * creation of the socket but not binding, perhaps if the
132          * machine has no ipv6 address of this name. */
133         freeaddrinfo(bres_all);
134         return -1;
135 }
136
137
138 /**
139  * Open a socket to a tcp remote host with the specified port .
140  *
141  * Based on code from Warren.  Proxy support by Stephen Rothwell.
142  * getaddrinfo() rewrite contributed by KAME.net.
143  *
144  * Now that we support IPv6 we need to look up the remote machine's
145  * address first, using @p af_hint to set a preference for the type
146  * of address.  Then depending on whether it has v4 or v6 addresses we
147  * try to open a connection.
148  *
149  * The loop allows for machines with some addresses which may not be
150  * reachable, perhaps because we can't e.g. route ipv6 to that network
151  * but we can get ip4 packets through.
152  *
153  * @param bind_address Local address to use.  Normally NULL to bind
154  * the wildcard address.
155  *
156  * @param af_hint Address family, e.g. AF_INET or AF_INET6.
157  **/
158 int open_socket_out(char *host, int port, const char *bind_address,
159                     int af_hint)
160 {
161         int type = SOCK_STREAM;
162         int error;
163         int s;
164         struct addrinfo hints, *res0, *res;
165         char portbuf[10];
166         char *h;
167         int proxied = 0;
168         char buffer[1024];
169         char *cp;
170
171         /* if we have a RSYNC_PROXY env variable then redirect our
172          * connetcion via a web proxy at the given address. The format
173          * is hostname:port */
174         h = getenv("RSYNC_PROXY");
175         proxied = h != NULL && *h != '\0';
176
177         if (proxied) {
178                 strlcpy(buffer, h, sizeof(buffer));
179                 cp = strchr(buffer, ':');
180                 if (cp == NULL) {
181                         rprintf(FERROR,
182                                 "invalid proxy specification: should be HOST:PORT\n");
183                         return -1;
184                 }
185                 *cp++ = '\0';
186                 strcpy(portbuf, cp);
187                 h = buffer;
188                 if (verbose >= 2) {
189                         rprintf(FINFO, "connection via http proxy %s port %s\n",
190                                 h, portbuf);
191                 }
192         } else {
193                 snprintf(portbuf, sizeof(portbuf), "%d", port);
194                 h = host;
195         }
196
197         memset(&hints, 0, sizeof(hints));
198         hints.ai_family = af_hint;
199         hints.ai_socktype = type;
200         error = getaddrinfo(h, portbuf, &hints, &res0);
201         if (error) {
202                 rprintf(FERROR, RSYNC_NAME ": getaddrinfo: %s %s: %s\n",
203                         h, portbuf, gai_strerror(error));
204                 return -1;
205         }
206
207         s = -1;
208         /* Try to connect to all addresses for this machine until we get
209          * through.  It might e.g. be multi-homed, or have both IPv4 and IPv6
210          * addresses.  We need to create a socket for each record, since the
211          * address record tells us what protocol to use to try to connect. */
212         for (res = res0; res; res = res->ai_next) {
213                 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
214                 if (s < 0)
215                         continue;
216
217                 if (bind_address)
218                         if (try_bind_local(s, res->ai_family, type,
219                                            bind_address) == -1) {
220                                 close(s);
221                                 s = -1;
222                                 continue;
223                         }
224
225                 if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
226                         close(s);
227                         s = -1;
228                         continue;
229                 }
230                 if (proxied &&
231                     establish_proxy_connection(s, host, port) != 0) {
232                         close(s);
233                         s = -1;
234                         continue;
235                 } else
236                         break;
237         }
238         freeaddrinfo(res0);
239         if (s < 0) {
240                 rprintf(FERROR, RSYNC_NAME ": failed to connect to %s: %s\n",
241                         h, strerror(errno));
242                 return -1;
243         }
244         return s;
245 }
246
247
248 /**
249  * Open an outgoing socket, but allow for it to be intercepted by
250  * $RSYNC_CONNECT_PROG, which will execute a program across a TCP
251  * socketpair rather than really opening a socket.
252  *
253  * We use this primarily in testing to detect TCP flow bugs, but not
254  * cause security problems by really opening remote connections.
255  *
256  * This is based on the Samba LIBSMB_PROG feature.
257  *
258  * @param bind_address Local address to use.  Normally NULL to get the stack default.
259  **/
260 int open_socket_out_wrapped(char *host, int port, const char *bind_address,
261                             int af_hint)
262 {
263         char *prog;
264
265         if ((prog = getenv("RSYNC_CONNECT_PROG")) != NULL)
266                 return sock_exec(prog);
267         return open_socket_out(host, port, bind_address, af_hint);
268 }
269
270
271
272 /**
273  * Open a socket of the specified type, port and address for incoming data
274  *
275  * Try to be better about handling the results of getaddrinfo(): when
276  * opening an inbound socket, we might get several address results,
277  * e.g. for the machine's ipv4 and ipv6 name.
278  *
279  * If binding a wildcard, then any one of them should do.  If an address
280  * was specified but it's insufficiently specific then that's not our
281  * fault.
282  *
283  * However, some of the advertized addresses may not work because e.g. we
284  * don't have IPv6 support in the kernel.  In that case go on and try all
285  * addresses until one succeeds.
286  *
287  * @param bind_address Local address to bind, or NULL to allow it to
288  * default.
289  **/
290 static int open_socket_in(int type, int port, const char *bind_address,
291                           int af_hint)
292 {
293         int one=1;
294         int s;
295         struct addrinfo hints, *all_ai, *resp;
296         char portbuf[10];
297         int error;
298
299         memset(&hints, 0, sizeof(hints));
300         hints.ai_family = af_hint;
301         hints.ai_socktype = type;
302         hints.ai_flags = AI_PASSIVE;
303         snprintf(portbuf, sizeof(portbuf), "%d", port);
304         error = getaddrinfo(bind_address, portbuf, &hints, &all_ai);
305         if (error) {
306                 rprintf(FERROR, RSYNC_NAME ": getaddrinfo: bind address %s: %s\n",
307                         bind_address, gai_strerror(error));
308                 return -1;
309         }
310
311         /* We may not be able to create the socket, if for example the
312          * machine knows about IPv6 in the C library, but not in the
313          * kernel. */
314         for (resp = all_ai; resp; resp = resp->ai_next) {
315                 s = socket(resp->ai_family, resp->ai_socktype,
316                            resp->ai_protocol);
317
318                 if (s == -1)
319                         /* See if there's another address that will work... */
320                         continue;
321
322                 setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
323                            (char *)&one, sizeof one);
324
325                 /* now we've got a socket - we need to bind it */
326                 if (bind(s, all_ai->ai_addr, all_ai->ai_addrlen) < 0) {
327                         /* Nope, try another */
328                         close(s);
329                         continue;
330                 }
331
332                 freeaddrinfo(all_ai);
333                 return s;
334         }
335
336         rprintf(FERROR, RSYNC_NAME ": open inbound socket on port %d failed: "
337                 "%s\n",
338                 port,
339                 strerror(errno));
340
341         freeaddrinfo(all_ai);
342         return -1;
343 }
344
345
346 /*
347  * Determine if a file descriptor is in fact a socket
348  */
349 int is_a_socket(int fd)
350 {
351         int v;
352         socklen_t l;
353         l = sizeof(int);
354
355         /* Parameters to getsockopt, setsockopt etc are very
356          * unstandardized across platforms, so don't be surprised if
357          * there are compiler warnings on e.g. SCO OpenSwerver or AIX.
358          * It seems they all eventually get the right idea.
359          *
360          * Debian says: ``The fifth argument of getsockopt and
361          * setsockopt is in reality an int [*] (and this is what BSD
362          * 4.* and libc4 and libc5 have).  Some POSIX confusion
363          * resulted in the present socklen_t.  The draft standard has
364          * not been adopted yet, but glibc2 already follows it and
365          * also has socklen_t [*]. See also accept(2).''
366          *
367          * We now return to your regularly scheduled programming.  */
368         return getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0;
369 }
370
371
372 static RETSIGTYPE sigchld_handler(UNUSED(int val))
373 {
374         signal(SIGCHLD, sigchld_handler);
375 #ifdef WNOHANG
376         while (waitpid(-1, NULL, WNOHANG) > 0) {}
377 #endif
378 }
379
380
381 void start_accept_loop(int port, int (*fn)(int, int))
382 {
383         int s;
384         extern char *bind_address;
385         extern int default_af_hint;
386
387         /* open an incoming socket */
388         s = open_socket_in(SOCK_STREAM, port, bind_address, default_af_hint);
389         if (s == -1)
390                 exit_cleanup(RERR_SOCKETIO);
391
392         /* ready to listen */
393         if (listen(s, 5) == -1) {
394                 close(s);
395                 exit_cleanup(RERR_SOCKETIO);
396         }
397
398
399         /* now accept incoming connections - forking a new process
400            for each incoming connection */
401         while (1) {
402                 fd_set fds;
403                 pid_t pid;
404                 int fd;
405                 struct sockaddr_storage addr;
406                 socklen_t addrlen = sizeof addr;
407
408                 /* close log file before the potentially very long select so
409                    file can be trimmed by another process instead of growing
410                    forever */
411                 log_close();
412
413                 FD_ZERO(&fds);
414                 FD_SET(s, &fds);
415
416                 if (select(s+1, &fds, NULL, NULL, NULL) != 1)
417                         continue;
418
419                 if (!FD_ISSET(s, &fds))
420                         continue;
421
422                 fd = accept(s,(struct sockaddr *)&addr,&addrlen);
423
424                 if (fd == -1)
425                         continue;
426
427                 signal(SIGCHLD, sigchld_handler);
428
429                 if ((pid = fork()) == 0) {
430                         int ret;
431                         close(s);
432                         /* open log file in child before possibly giving
433                            up privileges  */
434                         log_open();
435                         ret = fn(fd, fd);
436                         close_all();
437                         _exit(ret);
438                 } else if (pid < 0) {
439                         rprintf(FERROR,
440                                 RSYNC_NAME
441                                 ": could not create child server process: %s\n",
442                                 strerror(errno));
443                         close(fd);
444                         /* This might have happened because we're
445                          * overloaded.  Sleep briefly before trying to
446                          * accept again. */
447                         sleep(2);
448                 } else {
449                         /* Parent doesn't need this fd anymore. */
450                         close(fd);
451                 }
452         }
453 }
454
455
456 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
457
458 struct
459 {
460   char *name;
461   int level;
462   int option;
463   int value;
464   int opttype;
465 } socket_options[] = {
466   {"SO_KEEPALIVE",      SOL_SOCKET,    SO_KEEPALIVE,    0,                 OPT_BOOL},
467   {"SO_REUSEADDR",      SOL_SOCKET,    SO_REUSEADDR,    0,                 OPT_BOOL},
468   {"SO_BROADCAST",      SOL_SOCKET,    SO_BROADCAST,    0,                 OPT_BOOL},
469 #ifdef TCP_NODELAY
470   {"TCP_NODELAY",       IPPROTO_TCP,   TCP_NODELAY,     0,                 OPT_BOOL},
471 #endif
472 #ifdef IPTOS_LOWDELAY
473   {"IPTOS_LOWDELAY",    IPPROTO_IP,    IP_TOS,          IPTOS_LOWDELAY,    OPT_ON},
474 #endif
475 #ifdef IPTOS_THROUGHPUT
476   {"IPTOS_THROUGHPUT",  IPPROTO_IP,    IP_TOS,          IPTOS_THROUGHPUT,  OPT_ON},
477 #endif
478 #ifdef SO_SNDBUF
479   {"SO_SNDBUF",         SOL_SOCKET,    SO_SNDBUF,       0,                 OPT_INT},
480 #endif
481 #ifdef SO_RCVBUF
482   {"SO_RCVBUF",         SOL_SOCKET,    SO_RCVBUF,       0,                 OPT_INT},
483 #endif
484 #ifdef SO_SNDLOWAT
485   {"SO_SNDLOWAT",       SOL_SOCKET,    SO_SNDLOWAT,     0,                 OPT_INT},
486 #endif
487 #ifdef SO_RCVLOWAT
488   {"SO_RCVLOWAT",       SOL_SOCKET,    SO_RCVLOWAT,     0,                 OPT_INT},
489 #endif
490 #ifdef SO_SNDTIMEO
491   {"SO_SNDTIMEO",       SOL_SOCKET,    SO_SNDTIMEO,     0,                 OPT_INT},
492 #endif
493 #ifdef SO_RCVTIMEO
494   {"SO_RCVTIMEO",       SOL_SOCKET,    SO_RCVTIMEO,     0,                 OPT_INT},
495 #endif
496   {NULL,0,0,0,0}};
497
498
499
500 /**
501  * Set user socket options
502  **/
503 void set_socket_options(int fd, char *options)
504 {
505         char *tok;
506
507         if (!options || !*options)
508                 return;
509
510         options = strdup(options);
511
512         if (!options)
513                 out_of_memory("set_socket_options");
514
515         for (tok = strtok(options, " \t,"); tok; tok = strtok(NULL," \t,")) {
516                 int ret=0,i;
517                 int value = 1;
518                 char *p;
519                 int got_value = 0;
520
521                 if ((p = strchr(tok,'='))) {
522                         *p = 0;
523                         value = atoi(p+1);
524                         got_value = 1;
525                 }
526
527                 for (i = 0; socket_options[i].name; i++) {
528                         if (strcmp(socket_options[i].name,tok)==0)
529                                 break;
530                 }
531
532                 if (!socket_options[i].name) {
533                         rprintf(FERROR,"Unknown socket option %s\n",tok);
534                         continue;
535                 }
536
537                 switch (socket_options[i].opttype) {
538                 case OPT_BOOL:
539                 case OPT_INT:
540                         ret = setsockopt(fd,socket_options[i].level,
541                                          socket_options[i].option,(char *)&value,sizeof(int));
542                         break;
543
544                 case OPT_ON:
545                         if (got_value)
546                                 rprintf(FERROR,"syntax error - %s does not take a value\n",tok);
547
548                         {
549                                 int on = socket_options[i].value;
550                                 ret = setsockopt(fd,socket_options[i].level,
551                                                  socket_options[i].option,(char *)&on,sizeof(int));
552                         }
553                         break;
554                 }
555
556                 if (ret != 0)
557                         rprintf(FERROR, "failed to set socket option %s: %s\n", tok,
558                                 strerror(errno));
559         }
560
561         free(options);
562 }
563
564 /**
565  * Become a daemon, discarding the controlling terminal
566  **/
567 void become_daemon(void)
568 {
569         int i;
570
571         if (fork()) {
572                 _exit(0);
573         }
574
575         /* detach from the terminal */
576 #ifdef HAVE_SETSID
577         setsid();
578 #else
579 #ifdef TIOCNOTTY
580         i = open("/dev/tty", O_RDWR);
581         if (i >= 0) {
582                 ioctl(i, (int)TIOCNOTTY, (char *)0);
583                 close(i);
584         }
585 #endif /* TIOCNOTTY */
586 #endif
587         /* make sure that stdin, stdout an stderr don't stuff things
588            up (library functions, for example) */
589         for (i = 0; i < 3; i++) {
590                 close(i);
591                 open("/dev/null", O_RDWR);
592         }
593 }
594
595
596 /**
597  * This is like socketpair but uses tcp. It is used by the Samba
598  * regression test code.
599  *
600  * The function guarantees that nobody else can attach to the socket,
601  * or if they do that this function fails and the socket gets closed
602  * returns 0 on success, -1 on failure the resulting file descriptors
603  * are symmetrical.
604  **/
605 static int socketpair_tcp(int fd[2])
606 {
607         int listener;
608         struct sockaddr_in sock;
609         struct sockaddr_in sock2;
610         socklen_t socklen = sizeof(sock);
611         int connect_done = 0;
612
613         fd[0] = fd[1] = listener = -1;
614
615         memset(&sock, 0, sizeof(sock));
616
617         if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1)
618                 goto failed;
619
620         memset(&sock2, 0, sizeof(sock2));
621 #ifdef HAVE_SOCKADDR_LEN
622         sock2.sin_len = sizeof(sock2);
623 #endif
624         sock2.sin_family = PF_INET;
625
626         bind(listener, (struct sockaddr *)&sock2, sizeof(sock2));
627
628         if (listen(listener, 1) != 0)
629                 goto failed;
630
631         if (getsockname(listener, (struct sockaddr *)&sock, &socklen) != 0)
632                 goto failed;
633
634         if ((fd[1] = socket(PF_INET, SOCK_STREAM, 0)) == -1)
635                 goto failed;
636
637         set_nonblocking(fd[1]);
638
639         sock.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
640
641         if (connect(fd[1],(struct sockaddr *)&sock,sizeof(sock)) == -1) {
642                 if (errno != EINPROGRESS)
643                         goto failed;
644         } else
645                 connect_done = 1;
646
647         if ((fd[0] = accept(listener, (struct sockaddr *)&sock, &socklen)) == -1)
648                 goto failed;
649
650         close(listener);
651         if (connect_done == 0) {
652                 if (connect(fd[1],(struct sockaddr *)&sock,sizeof(sock)) != 0
653                     && errno != EISCONN)
654                         goto failed;
655         }
656
657         set_blocking(fd[1]);
658
659         /* all OK! */
660         return 0;
661
662  failed:
663         if (fd[0] != -1)
664                 close(fd[0]);
665         if (fd[1] != -1)
666                 close(fd[1]);
667         if (listener != -1)
668                 close(listener);
669         return -1;
670 }
671
672
673
674 /**
675  * Run a program on a local tcp socket, so that we can talk to it's
676  * stdin and stdout.  This is used to fake a connection to a daemon
677  * for testing -- not for the normal case of running SSH.
678  *
679  * @return a socket which is attached to a subprocess running
680  * "prog". stdin and stdout are attached. stderr is left attached to
681  * the original stderr
682  **/
683 int sock_exec(const char *prog)
684 {
685         int fd[2];
686
687         if (socketpair_tcp(fd) != 0) {
688                 rprintf(FERROR, RSYNC_NAME ": socketpair_tcp failed (%s)\n",
689                         strerror(errno));
690                 return -1;
691         }
692         if (fork() == 0) {
693                 close(fd[0]);
694                 close(0);
695                 close(1);
696                 dup(fd[1]);
697                 dup(fd[1]);
698                 if (verbose > 3) {
699                         /* Can't use rprintf because we've forked. */
700                         fprintf(stderr,
701                                 RSYNC_NAME ": execute socket program \"%s\"\n",
702                                 prog);
703                 }
704                 exit(system(prog));
705         }
706         close(fd[1]);
707         return fd[0];
708 }