Avoid a non-writable-by-the-user file when copying xattrs.
[rsync/rsync.git] / clientserver.c
index d3bcb2d..469371f 100644 (file)
@@ -48,7 +48,6 @@ extern int write_batch;
 extern int default_af_hint;
 extern int logfile_format_has_i;
 extern int logfile_format_has_o_or_i;
-extern mode_t orig_umask;
 extern char *bind_address;
 extern char *config_file;
 extern char *logfile_format;
@@ -56,7 +55,6 @@ extern char *files_from;
 extern char *tmpdir;
 extern struct chmod_mode_struct *chmod_modes;
 extern filter_rule_list daemon_filter_list;
-extern char curr_dir[MAXPATHLEN];
 #ifdef ICONV_OPTION
 extern char *iconv_opt;
 extern iconv_t ic_send, ic_recv;
@@ -432,46 +430,33 @@ static int add_a_group(int f_out, const char *gname)
        return 0;
 }
 
+#ifdef HAVE_GETGROUPLIST
+static int want_all_groups(int f_out, uid_t uid)
+{
+       const char *err;
+       gid_count = MAX_GID_LIST;
+       if ((err = getallgroups(uid, gid_list, &gid_count)) != NULL) {
+               rsyserr(FLOG, errno, "%s", err);
+               io_printf(f_out, "@ERROR: %s\n", err);
+               return -1;
+       }
+       return 0;
+}
+#elif defined HAVE_INITGROUPS
 static struct passwd *want_all_groups(int f_out, uid_t uid)
 {
-#if defined HAVE_GETGROUPLIST || defined HAVE_INITGROUPS
        struct passwd *pw;
        if ((pw = getpwuid(uid)) == NULL) {
                rsyserr(FLOG, errno, "getpwuid failed");
                io_printf(f_out, "@ERROR: getpwuid failed\n");
                return NULL;
        }
-# ifdef HAVE_GETGROUPLIST
-       /* Get all the process's groups, with the pw_gid group first. */
-       gid_count = MAX_GID_LIST;
-       if (getgrouplist(pw->pw_name, pw->pw_gid, gid_list, &gid_count) < 0) {
-               rsyserr(FLOG, errno, "getgrouplist failed");
-               io_printf(f_out, "@ERROR: getgrouplist failed\n");
-               return NULL;
-       }
-       /* Paranoia: is the default group not first in the list? */
-       if (gid_list[0] != pw->pw_gid) {
-               int j;
-               for (j = 0; j < gid_count; j++) {
-                       if (gid_list[j] == pw->pw_gid) {
-                               gid_list[j] = gid_list[0];
-                               gid_list[0] = pw->pw_gid;
-                               break;
-                       }
-               }
-       }
-# else
        /* Start with the default group and initgroups() will add the reset. */
        gid_count = 1;
        gid_list[0] = pw->pw_gid;
-# endif
        return pw;
-#else
-       rprintf(FLOG, "This rsync does not support a gid of \"*\"\n");
-       io_printf(f_out, "@ERROR: invalid gid setting.\n");
-       return NULL;
-#endif
 }
+#endif
 
 static void set_env_str(const char *var, const char *str)
 {
@@ -498,7 +483,9 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char
        int argc;
        char **argv, **orig_argv, **orig_early_argv, *module_chdir;
        char line[BIGPATHBUFLEN];
+#if defined HAVE_INITGROUPS && !defined HAVE_GETGROUPLIST
        struct passwd *pw = NULL;
+#endif
        uid_t uid;
        int set_uid;
        char *p, *err_msg = NULL;
@@ -595,8 +582,17 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char
        if (p) {
                /* The "*" gid must be the first item in the list. */
                if (strcmp(p, "*") == 0) {
+#ifdef HAVE_GETGROUPLIST
+                       if (want_all_groups(f_out, uid) < 0)
+                               return -1;
+#elif defined HAVE_INITGROUPS
                        if ((pw = want_all_groups(f_out, uid)) == NULL)
                                return -1;
+#else
+                       rprintf(FLOG, "This rsync does not support a gid of \"*\"\n");
+                       io_printf(f_out, "@ERROR: invalid gid setting.\n");
+                       return -1;
+#endif
                } else if (add_a_group(f_out, p) < 0)
                        return -1;
                while ((p = strtok(NULL, ", ")) != NULL) {
@@ -616,6 +612,11 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char
        }
 
        module_dir = lp_path(i);
+       if (*module_dir == '\0') {
+               rprintf(FLOG, "No path specified for module %s\n", name);
+               io_printf(f_out, "@ERROR: no path setting.\n");
+               return -1;
+       }
        if (use_chroot) {
                if ((p = strstr(module_dir, "/./")) != NULL) {
                        *p = '\0'; /* Temporary... */
@@ -674,7 +675,6 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char
        if (*lp_prexfer_exec(i) || *lp_postxfer_exec(i)) {
                int status;
 
-               umask(orig_umask);
                /* For post-xfer exec, fork a new process to run the rsync
                 * daemon while this process waits for the exit status and
                 * runs the indicated command at that point. */
@@ -686,6 +686,9 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char
                                return -1;
                        }
                        if (pid) {
+                               close(f_in);
+                               if (f_out != f_in)
+                                       close(f_out);
                                set_env_num("RSYNC_PID", (long)pid);
                                if (wait_process(pid, &status, 0) < 0)
                                        status = -1;
@@ -743,7 +746,6 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char
                        set_blocking(fds[1]);
                        pre_exec_fd = fds[1];
                }
-               umask(0);
        }
 #endif
 
@@ -812,7 +814,11 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char
        }
 
        if (set_uid) {
-               if (setuid(uid) || seteuid(uid)) {
+               if (setuid(uid) < 0
+#ifdef HAVE_SETEUID
+                || seteuid(uid) < 0
+#endif
+               ) {
                        rsyserr(FLOG, errno, "setuid %ld failed", (long)uid);
                        io_printf(f_out, "@ERROR: setuid failed\n");
                        return -1;
@@ -949,7 +955,7 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char
         && (use_chroot ? lp_numeric_ids(i) != False : lp_numeric_ids(i) == True))
                numeric_ids = -1; /* Set --numeric-ids w/o breaking protocol. */
 
-       if (lp_timeout(i) && lp_timeout(i) > io_timeout)
+       if (lp_timeout(i) && (!io_timeout || lp_timeout(i) < io_timeout))
                set_io_timeout(lp_timeout(i));
 
        /* If we have some incoming/outgoing chmod changes, append them to
@@ -1070,7 +1076,7 @@ static void create_pid_file(void)
                return;
 
        cleanup_set_pid(pid);
-       if ((fd = do_open(pid_file, O_WRONLY|O_CREAT|O_EXCL, 0666 & ~orig_umask)) == -1) {
+       if ((fd = do_open(pid_file, O_WRONLY|O_CREAT|O_EXCL, 0666)) == -1) {
          failure:
                cleanup_set_pid(0);
                fprintf(stderr, "failed to create pid file %s: %s\n", pid_file, strerror(errno));