More error messages.
[rsync.git] / clientserver.c
index 044f86dcfc2670ca12d24a3ab280bcad63f103b2..1b3801bc3b1e2b5079f58b7e015c92cc027cdd06 100644 (file)
@@ -42,9 +42,10 @@ int start_socket_client(char *host, char *path, int argc, char *argv[])
        char *p, *user=NULL;
        extern int remote_version;
        extern int am_sender;
-       extern struct in_addr socket_address;
        extern char *shell_cmd;
-
+       extern int kludge_around_eof;
+       extern char *bind_address;
+       
        if (argc == 0 && !am_sender) {
                extern int list_only;
                list_only = 1;
@@ -63,7 +64,7 @@ int start_socket_client(char *host, char *path, int argc, char *argv[])
         }
         
        if (*path == '/') {
-               rprintf(FERROR,"ERROR: The remote path must start with a module name\n");
+               rprintf(FERROR,"ERROR: The remote path must start with a module name not a /\n");
                return -1;
        }
 
@@ -77,7 +78,8 @@ int start_socket_client(char *host, char *path, int argc, char *argv[])
        if (!user) user = getenv("USER");
        if (!user) user = getenv("LOGNAME");
 
-       fd = open_socket_out(host, rsync_port, &socket_address);
+       fd = open_socket_out_wrapped (host, rsync_port, bind_address,
+                                     global_opts.af_hint);
        if (fd == -1) {
                exit_cleanup(RERR_SOCKETIO);
        }
@@ -106,6 +108,10 @@ int start_socket_client(char *host, char *path, int argc, char *argv[])
        io_printf(fd,"%s\n",path);
        if (p) *p = '/';
 
+       /* Old servers may just drop the connection here,
+        rather than sending a proper EXIT command.  Yuck. */
+       kludge_around_eof = remote_version < 25;
+
        while (1) {
                if (!read_line(fd, line, sizeof(line)-1)) {
                        return -1;
@@ -117,8 +123,12 @@ int start_socket_client(char *host, char *path, int argc, char *argv[])
                }
 
                if (strcmp(line,"@RSYNCD: OK") == 0) break;
+
+               if (strcmp(line,"@RSYNCD: EXIT") == 0) exit(0);
+
                rprintf(FINFO,"%s\n", line);
        }
+       kludge_around_eof = False;
 
        for (i=0;i<sargc;i++) {
                io_printf(fd,"%s\n", sargs[i]);
@@ -141,7 +151,7 @@ static int rsync_module(int fd, int i)
        char *argv[MAX_ARGS];
        char **argp;
        char line[MAXPATHLEN];
-       uid_t uid = (uid_t)-2;
+       uid_t uid = (uid_t)-2;  /* canonically "nobody" */
        gid_t gid = (gid_t)-2;
        char *p;
        char *addr = client_addr(fd);
@@ -196,7 +206,7 @@ static int rsync_module(int fd, int i)
                if (!name_to_uid(p, &uid)) {
                        if (!isdigit(*p)) {
                                rprintf(FERROR,"Invalid uid %s\n", p);
-                               io_printf(fd,"@ERROR: invalid uid\n");
+                               io_printf(fd,"@ERROR: invalid uid %s\n", p);
                                return -1;
                        } 
                        uid = atoi(p);
@@ -206,12 +216,19 @@ static int rsync_module(int fd, int i)
                if (!name_to_gid(p, &gid)) {
                        if (!isdigit(*p)) {
                                rprintf(FERROR,"Invalid gid %s\n", p);
-                               io_printf(fd,"@ERROR: invalid gid\n");
+                               io_printf(fd,"@ERROR: invalid gid %s\n", p);
                                return -1;
                        } 
                        gid = atoi(p);
                }
        }
+        
+        /* TODO: If we're not root, but the configuration requests
+         * that we change to some uid other than the current one, then
+         * log a warning. */
+
+        /* TODO: Perhaps take a list of gids, and make them into the
+         * supplementary groups. */
 
        p = lp_include_from(i);
        add_exclude_file(p, 1, 1);
@@ -228,6 +245,18 @@ static int rsync_module(int fd, int i)
        log_init();
 
        if (use_chroot) {
+               /*
+                * XXX: The 'use chroot' flag is a fairly reliable
+                * source of confusion, because it fails under two
+                * important circumstances: running as non-root,
+                * running on Win32 (or possibly others).  On the
+                * other hand, if you are running as root, then it
+                * might be better to always use chroot.
+                *
+                * So, perhaps if we can't chroot we should just issue
+                * a warning, unless a "require chroot" flag is set,
+                * in which case we fail.
+                */
                if (chroot(lp_path(i))) {
                        rsyserr(FERROR, errno, "chroot %s failed", lp_path(i));
                        io_printf(fd,"@ERROR: chroot failed\n");
@@ -251,13 +280,13 @@ static int rsync_module(int fd, int i)
 
        if (am_root) {
                if (setgid(gid)) {
-                       rsyserr(FERROR, errno, "setgid %d failed", gid);
+                       rsyserr(FERROR, errno, "setgid %d failed", (int) gid);
                        io_printf(fd,"@ERROR: setgid failed\n");
                        return -1;
                }
 
                if (setuid(uid)) {
-                       rsyserr(FERROR, errno, "setuid %d failed", uid);
+                       rsyserr(FERROR, errno, "setuid %d failed", (int) uid);
                        io_printf(fd,"@ERROR: setuid failed\n");
                        return -1;
                }
@@ -370,10 +399,14 @@ static void send_listing(int fd)
 {
        int n = lp_numservices();
        int i;
-       
+       extern int remote_version;
+
        for (i=0;i<n;i++)
                if (lp_list(i))
                    io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
+
+       if (remote_version >= 25)
+               io_printf(fd,"@RSYNCD: EXIT\n");
 }
 
 /* this is called when a socket connection is established to a client
@@ -468,7 +501,8 @@ int daemon_main(void)
                return start_daemon(STDIN_FILENO);
        }
 
-       become_daemon();
+       if (!global_opts.no_detach)
+           become_daemon();
 
        if (!lp_load(config_file, 1)) {
                exit_cleanup(RERR_SYNTAX);
@@ -476,10 +510,12 @@ int daemon_main(void)
 
        log_init();
 
-       rprintf(FINFO, "rsyncd version %s starting, listening on port %d\n", VERSION,
+       rprintf(FINFO, "rsyncd version %s starting, listening on port %d\n",
+               RSYNC_VERSION,
                 rsync_port);
         /* TODO: If listening on a particular address, then show that
-         * address too. */
+         * address too.  In fact, why not just do inet_ntop on the
+         * local address??? */
 
        if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
                char pidbuf[16];
@@ -492,7 +528,7 @@ int daemon_main(void)
                    rsyserr(FLOG, errno, "failed to create pid file %s", pid_file);
                    exit_cleanup(RERR_FILEIO);
                }
-               slprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
+               snprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
                write(fd, pidbuf, strlen(pidbuf));
                close(fd);
        }