Changed error message that just said "open %s: %s" to "cannot create %s: %s"
[rsync/rsync.git] / clientserver.c
index 49928d5..a429b8d 100644 (file)
@@ -167,24 +167,28 @@ static int rsync_module(int fd, int i)
        if (lp_read_only(i))
                read_only = 1;
 
-       p = lp_uid(i);
-       if (!name_to_uid(p, &uid)) {
-               if (!isdigit(*p)) {
-                       rprintf(FERROR,"Invalid uid %s\n", p);
-                       io_printf(fd,"@ERROR: invalid uid\n");
-                       return -1;
-               } 
-               uid = atoi(p);
-       }
+       am_root = (getuid() == 0);
 
-       p = lp_gid(i);
-       if (!name_to_gid(p, &gid)) {
-               if (!isdigit(*p)) {
-                       rprintf(FERROR,"Invalid gid %s\n", p);
-                       io_printf(fd,"@ERROR: invalid gid\n");
-                       return -1;
-               } 
-               gid = atoi(p);
+       if (am_root) {
+               p = lp_uid(i);
+               if (!name_to_uid(p, &uid)) {
+                       if (!isdigit(*p)) {
+                               rprintf(FERROR,"Invalid uid %s\n", p);
+                               io_printf(fd,"@ERROR: invalid uid\n");
+                               return -1;
+                       } 
+                       uid = atoi(p);
+               }
+
+               p = lp_gid(i);
+               if (!name_to_gid(p, &gid)) {
+                       if (!isdigit(*p)) {
+                               rprintf(FERROR,"Invalid gid %s\n", p);
+                               io_printf(fd,"@ERROR: invalid gid\n");
+                               return -1;
+                       } 
+                       gid = atoi(p);
+               }
        }
 
        p = lp_include_from(i);
@@ -208,34 +212,36 @@ static int rsync_module(int fd, int i)
                        return -1;
                }
 
-               if (chdir("/")) {
+               if (!push_dir("/", 0)) {
+                       rprintf(FERROR,"chdir %s failed\n", lp_path(i));
+                       io_printf(fd,"@ERROR: chdir failed\n");
+                       return -1;
+               }
+
+       } else {
+               if (!push_dir(lp_path(i), 0)) {
                        rprintf(FERROR,"chdir %s failed\n", lp_path(i));
                        io_printf(fd,"@ERROR: chdir failed\n");
                        return -1;
                }
+       }
 
-               if (setgid(gid) || getgid() != gid) {
+       if (am_root) {
+               if (setgid(gid)) {
                        rprintf(FERROR,"setgid %d failed\n", gid);
                        io_printf(fd,"@ERROR: setgid failed\n");
                        return -1;
                }
 
-               if (setuid(uid) || getuid() != uid) {
+               if (setuid(uid)) {
                        rprintf(FERROR,"setuid %d failed\n", uid);
                        io_printf(fd,"@ERROR: setuid failed\n");
                        return -1;
                }
 
-       } else {
-               if (!push_dir(lp_path(i), 0)) {
-                       rprintf(FERROR,"chdir %s failed\n", lp_path(i));
-                       io_printf(fd,"@ERROR: chdir failed\n");
-                       return -1;
-               }
+               am_root = (getuid() == 0);
        }
 
-       am_root = (getuid() == 0);
-
        io_printf(fd,"@RSYNCD: OK\n");
 
        argv[argc++] = "rsyncd";
@@ -286,7 +292,7 @@ static int rsync_module(int fd, int i)
                }
        }
 
-       ret = parse_arguments(argc, argv);
+       ret = parse_arguments(argc, argv, 0);
 
        if (request) {
                if (*auth_user) {
@@ -416,11 +422,6 @@ int daemon_main(void)
        extern char *config_file;
        char *pid_file;
 
-       /* this ensures that we don't call getcwd after the chroot,
-           which doesn't work on platforms that use popen("pwd","r")
-           for getcwd */
-       push_dir("/", 0);
-
        if (is_a_socket(STDIN_FILENO)) {
                int i;