Add safety check for local --remove-source-files.
[rsync.git] / socket.c
index a43385ec3850820971eea45bee19b760c7733811..c2075adf8bb81cd05bc8342fdd37666704b7563e 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 1992-2001 Andrew Tridgell <tridge@samba.org>
  * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
- * Copyright (C) 2003-2009 Wayne Davison
+ * Copyright (C) 2003-2020 Wayne Davison
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 #include "rsync.h"
 #include "itypes.h"
+#include "ifuncs.h"
+#ifdef HAVE_NETINET_IN_SYSTM_H
 #include <netinet/in_systm.h>
+#endif
+#ifdef HAVE_NETINET_IP_H
 #include <netinet/ip.h>
+#endif
 #include <netinet/tcp.h>
 
 extern char *bind_address;
 extern char *sockopts;
 extern int default_af_hint;
 extern int connect_timeout;
+extern int pid_file_fd;
 
 #ifdef HAVE_SIGACTION
 static struct sigaction sigact;
@@ -44,8 +50,7 @@ static int sock_exec(const char *prog);
 /* Establish a proxy connection on an open socket to a web proxy by using the
  * CONNECT method.  If proxy_user and proxy_pass are not NULL, they are used to
  * authenticate to the proxy using the "Basic" proxy-authorization protocol. */
-static int establish_proxy_connection(int fd, char *host, int port,
-                                     char *proxy_user, char *proxy_pass)
+static int establish_proxy_connection(int fd, char *host, int port, char *proxy_user, char *proxy_pass)
 {
        char *cp, buffer[1024];
        char *authhdr, authbuf[1024];
@@ -69,9 +74,8 @@ static int establish_proxy_connection(int fd, char *host, int port,
                authhdr = "";
        }
 
-       snprintf(buffer, sizeof buffer, "CONNECT %s:%d HTTP/1.0%s%s\r\n\r\n",
-                host, port, authhdr, authbuf);
-       len = strlen(buffer);
+       len = snprintf(buffer, sizeof buffer, "CONNECT %s:%d HTTP/1.0%s%s\r\n\r\n", host, port, authhdr, authbuf);
+       assert(len > 0 && len < (int)sizeof buffer);
        if (write(fd, buffer, len) != len) {
                rsyserr(FERROR, errno, "failed to write to proxy");
                return -1;
@@ -157,7 +161,7 @@ int try_bind_local(int s, int ai_family, int ai_socktype,
 }
 
 /* connect() timeout handler based on alarm() */
-static RETSIGTYPE contimeout_handler(UNUSED(int val))
+static void contimeout_handler(UNUSED(int val))
 {
        connect_timeout = -1;
 }
@@ -178,8 +182,7 @@ static RETSIGTYPE contimeout_handler(UNUSED(int val))
  * bind_addr: local address to use.  Normally NULL to bind the wildcard address.
  *
  * af_hint: address family, e.g. AF_INET or AF_INET6. */
-int open_socket_out(char *host, int port, const char *bind_addr,
-                   int af_hint)
+int open_socket_out(char *host, int port, const char *bind_addr, int af_hint)
 {
        int type = SOCK_STREAM;
        int error, s, j, addr_cnt, *errnos;
@@ -191,7 +194,7 @@ int open_socket_out(char *host, int port, const char *bind_addr,
        char *proxy_user = NULL, *proxy_pass = NULL;
 
        /* if we have a RSYNC_PROXY env variable then redirect our
-        * connetcion via a web proxy at the given address. */
+        * connection via a web proxy at the given address. */
        h = getenv("RSYNC_PROXY");
        proxied = h != NULL && *h != '\0';
 
@@ -246,8 +249,6 @@ int open_socket_out(char *host, int port, const char *bind_addr,
 
        for (res = res0, addr_cnt = 0; res; res = res->ai_next, addr_cnt++) {}
        errnos = new_array0(int, addr_cnt);
-       if (!errnos)
-               out_of_memory("open_socket_out");
 
        s = -1;
        /* Try to connect to all addresses for this machine until we get
@@ -290,19 +291,16 @@ int open_socket_out(char *host, int port, const char *bind_addr,
                        continue;
                }
 
-               if (proxied
-                && establish_proxy_connection(s, host, port,
-                                              proxy_user, proxy_pass) != 0) {
+               if (proxied && establish_proxy_connection(s, host, port, proxy_user, proxy_pass) != 0) {
                        close(s);
                        s = -1;
                        continue;
                }
                if (DEBUG_GTE(CONNECT, 2)) {
                        char buf[2048];
-                       if ((error = getnameinfo(res->ai_addr, res->ai_addrlen, buf, sizeof buf, NULL, 0, NI_NUMERICHOST) != 0))
-                               rprintf(FINFO, "error in getnameinfo: %s\n", gai_strerror(error));
-                       else
-                               rprintf(FINFO, "Connected to %s (%s)\n", h, buf);
+                       if ((error = getnameinfo(res->ai_addr, res->ai_addrlen, buf, sizeof buf, NULL, 0, NI_NUMERICHOST)) != 0)
+                               snprintf(buf, sizeof buf, "*getnameinfo failure: %s*", gai_strerror(error));
+                       rprintf(FINFO, "Connected to %s (%s)\n", h, buf);
                }
                break;
        }
@@ -312,8 +310,8 @@ int open_socket_out(char *host, int port, const char *bind_addr,
                for (res = res0, j = 0; res; res = res->ai_next, j++) {
                        if (errnos[j] == 0)
                                continue;
-                       if (inet_ntop(res->ai_family, res->ai_addr->sa_data + 2, buf, sizeof buf) == NULL)
-                               strlcpy(buf, "*inet_ntop failed*", sizeof buf);
+                       if ((error = getnameinfo(res->ai_addr, res->ai_addrlen, buf, sizeof buf, NULL, 0, NI_NUMERICHOST)) != 0)
+                               snprintf(buf, sizeof buf, "*getnameinfo failure: %s*", gai_strerror(error));
                        rsyserr(FERROR, errnos[j], "failed to connect to %s (%s)", h, buf);
                }
                if (s < 0)
@@ -337,8 +335,7 @@ int open_socket_out(char *host, int port, const char *bind_addr,
  * This is based on the Samba LIBSMB_PROG feature.
  *
  * bind_addr: local address to use.  Normally NULL to get the stack default. */
-int open_socket_out_wrapped(char *host, int port, const char *bind_addr,
-                           int af_hint)
+int open_socket_out_wrapped(char *host, int port, const char *bind_addr, int af_hint)
 {
        char *prog = getenv("RSYNC_CONNECT_PROG");
 
@@ -356,8 +353,7 @@ int open_socket_out_wrapped(char *host, int port, const char *bind_addr,
                                len += hlen;
                }
                f = prog;
-               if (!(prog = new_array(char, len)))
-                       out_of_memory("open_socket_out_wrapped");
+               prog = new_array(char, len);
                for (t = prog; *f; f++) {
                        if (*f == '%') {
                                switch (*++f) {
@@ -425,8 +421,6 @@ static int *open_socket_in(int type, int port, const char *bind_addr,
 
        socks = new_array(int, maxs + 1);
        errmsgs = new_array(char *, maxs);
-       if (!socks || !errmsgs)
-               out_of_memory("open_socket_in");
 
        /* We may not be able to create the socket, if for example the
         * machine knows about IPv6 in the C library, but not in the
@@ -455,9 +449,8 @@ static int *open_socket_in(int type, int port, const char *bind_addr,
 
 #ifdef IPV6_V6ONLY
                if (resp->ai_family == AF_INET6) {
-                       if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
-                                      (char *)&one, sizeof one) < 0
-                           && default_af_hint != AF_INET6) {
+                       if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&one, sizeof one) < 0
+                        && default_af_hint != AF_INET6) {
                                close(s);
                                continue;
                        }
@@ -526,7 +519,7 @@ int is_a_socket(int fd)
 }
 
 
-static RETSIGTYPE sigchld_handler(UNUSED(int val))
+static void sigchld_handler(UNUSED(int val))
 {
 #ifdef WNOHANG
        while (waitpid(-1, NULL, WNOHANG) > 0) {}
@@ -558,8 +551,7 @@ void start_accept_loop(int port, int (*fn)(int, int))
                        rsyserr(FERROR, errno, "listen() on socket failed");
 #ifdef INET6
                        if (errno == EADDRINUSE && i > 0) {
-                               rprintf(FINFO,
-                                   "Try using --ipv4 or --ipv6 to avoid this listen() error.\n");
+                               rprintf(FINFO, "Try using --ipv4 or --ipv6 to avoid this listen() error.\n");
                        }
 #endif
                        exit_cleanup(RERR_SOCKETIO);
@@ -594,8 +586,7 @@ void start_accept_loop(int port, int (*fn)(int, int))
 
                for (i = 0, fd = -1; sp[i] >= 0; i++) {
                        if (FD_ISSET(sp[i], &fds)) {
-                               fd = accept(sp[i], (struct sockaddr *)&addr,
-                                           &addrlen);
+                               fd = accept(sp[i], (struct sockaddr *)&addr, &addrlen);
                                break;
                        }
                }
@@ -607,6 +598,8 @@ void start_accept_loop(int port, int (*fn)(int, int))
 
                if ((pid = fork()) == 0) {
                        int ret;
+                       if (pid_file_fd >= 0)
+                               close(pid_file_fd);
                        for (i = 0; sp[i] >= 0; i++)
                                close(sp[i]);
                        /* Re-open log file in child before possibly giving
@@ -643,7 +636,9 @@ struct
 } socket_options[] = {
   {"SO_KEEPALIVE",      SOL_SOCKET,    SO_KEEPALIVE,    0,                 OPT_BOOL},
   {"SO_REUSEADDR",      SOL_SOCKET,    SO_REUSEADDR,    0,                 OPT_BOOL},
+#ifdef SO_BROADCAST
   {"SO_BROADCAST",      SOL_SOCKET,    SO_BROADCAST,    0,                 OPT_BOOL},
+#endif
 #ifdef TCP_NODELAY
   {"TCP_NODELAY",       IPPROTO_TCP,   TCP_NODELAY,     0,                 OPT_BOOL},
 #endif
@@ -685,9 +680,6 @@ void set_socket_options(int fd, char *options)
 
        options = strdup(options);
 
-       if (!options)
-               out_of_memory("set_socket_options");
-
        for (tok = strtok(options, " \t,"); tok; tok = strtok(NULL," \t,")) {
                int ret=0,i;
                int value = 1;
@@ -792,8 +784,7 @@ static int socketpair_tcp(int fd[2])
        set_blocking(fd[1]);
 
        if (connect_done == 0) {
-               if (connect(fd[1], (struct sockaddr *)&sock, sizeof sock) != 0
-                   && errno != EISCONN)
+               if (connect(fd[1], (struct sockaddr *)&sock, sizeof sock) != 0 && errno != EISCONN)
                        goto failed;
        }
 
@@ -815,7 +806,7 @@ static int socketpair_tcp(int fd[2])
  * stdout.  This is used to fake a connection to a daemon for testing -- not
  * for the normal case of running SSH.
  *
- * Retruns a socket which is attached to a subprocess running "prog". stdin and
+ * Returns a socket which is attached to a subprocess running "prog". stdin and
  * stdout are attached. stderr is left attached to the original stderr. */
 static int sock_exec(const char *prog)
 {
@@ -842,7 +833,7 @@ static int sock_exec(const char *prog)
                        fprintf(stderr, "Failed to run \"%s\"\n", prog);
                        exit(1);
                }
-               exit(system(prog));
+               exit(shell_exec(prog));
        }
 
        close(fd[1]);