1 Depends-On-Patch: acls.diff
2 Depends-On-Patch: xattrs.diff
4 This patch adds a new option: --fake-super, which tells rsync to copy in a
5 fake super-user mode that stores various file attributes in an extended-
6 attribute value instead of as real file-system attributes. The items
9 mode the real mode of a file is always (666 & umask) while
10 the real mode of a directory is always (777 & umask).
12 rdev devices and special files are created as zero-length
15 uid the real owner is always left unchanged.
17 gid the real group is always left unchanged.
19 A daemon can set "fake super = yes" in the rsync.conf file for any module
20 that you'd like to run without root perms while pretending it has them (the
21 client cannot affect this).
23 The --fake-super option only affects the side where the option is used. To
24 affect the remote side of a remote-shell connection, specify an rsync path:
26 rsync -av --rsync-path='rsync --fake-super' /src/ host:/dest/
28 For a local copy where you want to affect only one side or the other,
29 you'll need to turn the copy into a remote copy to localhost.
31 After applying this patch, run these commands for a successful build:
39 @@ -129,7 +129,7 @@ static int make_bak_dir(char *fullpath)
41 /* Try to transfer the directory settings of the
42 * actual dir that the files are coming from. */
43 - if (do_stat(rel, &sx.st) < 0) {
44 + if (x_stat(rel, &sx.st, NULL) < 0) {
45 rsyserr(FERROR, errno,
46 "make_bak_dir stat %s failed",
48 @@ -200,7 +200,7 @@ static int keep_backup(char *fname)
51 /* return if no file to keep */
52 - if (do_lstat(fname, &sx.st) < 0)
53 + if (x_lstat(fname, &sx.st, NULL) < 0)
56 sx.acc_acl = sx.def_acl = NULL;
57 --- old/clientserver.c
58 +++ new/clientserver.c
59 @@ -625,6 +625,11 @@ static int rsync_module(int f_in, int f_
60 ret = parse_arguments(&argc, (const char ***) &argv, 0);
61 quiet = 0; /* Don't let someone try to be tricky. */
63 + if (lp_fake_super(i))
65 + else if (am_root < 0) /* Treat --fake-super from client as --super. */
68 if (filesfrom_fd == 0)
73 @@ -181,7 +181,7 @@ static int readlink_stat(const char *pat
77 - return do_stat(path, stp);
78 + return x_stat(path, stp, NULL);
82 @@ -189,17 +189,17 @@ int link_stat(const char *path, STRUCT_S
86 - return do_stat(path, stp);
87 - if (do_lstat(path, stp) < 0)
88 + return x_stat(path, stp, NULL);
89 + if (x_lstat(path, stp, NULL) < 0)
91 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
93 - if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
94 + if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
99 - return do_stat(path, stp);
100 + return x_stat(path, stp, NULL);
104 @@ -793,7 +793,7 @@ struct file_struct *make_file(char *fnam
105 if (save_errno == ENOENT) {
107 /* Avoid "vanished" error if symlink points nowhere. */
108 - if (copy_links && do_lstat(thisname, &st) == 0
109 + if (copy_links && x_lstat(thisname, &st, NULL) == 0
110 && S_ISLNK(st.st_mode)) {
111 io_error |= IOERR_GENERAL;
112 rprintf(FERROR, "symlink has no referent: %s\n",
113 @@ -963,7 +963,7 @@ struct file_struct *make_file(char *fnam
114 int save_mode = file->mode;
115 file->mode = S_IFDIR; /* Find a directory with our name. */
116 if (flist_find(the_file_list, file) >= 0
117 - && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) {
118 + && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
119 file->modtime = st2.st_mtime;
120 file->length = st2.st_size;
121 file->mode = st2.st_mode;
124 @@ -1510,13 +1510,14 @@ void generate_files(int f_out, struct fi
125 recv_generator(fbuf, file, i, itemizing, maybe_ATTRS_REPORT,
128 - /* We need to ensure that any dirs we create have writeable
129 + /* We need to ensure that any dirs we create have rwx
130 * permissions during the time we are putting files within
131 * them. This is then fixed after the transfer is done. */
133 - if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)
134 + if (am_root <= 0 && S_ISDIR(file->mode)
135 + && (file->mode & S_IRWXU) != S_IRWXU
137 - mode_t mode = file->mode | S_IWUSR; /* user write */
138 + mode_t mode = file->mode | S_IRWXU; /* user rwx */
139 char *fname = local_name ? local_name : fbuf;
140 if (do_chmod(fname, mode) < 0) {
141 rsyserr(FERROR, errno,
144 @@ -150,6 +150,7 @@ typedef struct
150 BOOL ignore_nonreadable;
152 @@ -197,6 +198,7 @@ static service sDefault =
153 /* syslog_facility; */ LOG_DAEMON,
156 + /* fake_super; */ False,
157 /* ignore_errors; */ False,
158 /* ignore_nonreadable; */ False,
160 @@ -298,6 +300,7 @@ static struct parm_struct parm_table[] =
161 {"dont compress", P_STRING, P_LOCAL, &sDefault.dont_compress, NULL,0},
162 {"exclude from", P_STRING, P_LOCAL, &sDefault.exclude_from, NULL,0},
163 {"exclude", P_STRING, P_LOCAL, &sDefault.exclude, NULL,0},
164 + {"fake super", P_BOOL, P_LOCAL, &sDefault.fake_super, NULL,0},
165 {"filter", P_STRING, P_LOCAL, &sDefault.filter, NULL,0},
166 {"gid", P_STRING, P_LOCAL, &sDefault.gid, NULL,0},
167 {"hosts allow", P_STRING, P_LOCAL, &sDefault.hosts_allow, NULL,0},
168 @@ -412,6 +415,7 @@ FN_LOCAL_INTEGER(lp_max_connections, max
169 FN_LOCAL_INTEGER(lp_max_verbosity, max_verbosity)
170 FN_LOCAL_INTEGER(lp_timeout, timeout)
172 +FN_LOCAL_BOOL(lp_fake_super, fake_super)
173 FN_LOCAL_BOOL(lp_ignore_errors, ignore_errors)
174 FN_LOCAL_BOOL(lp_ignore_nonreadable, ignore_nonreadable)
175 FN_LOCAL_BOOL(lp_list, list)
176 @@ -816,7 +820,7 @@ BOOL lp_load(char *pszFname, int globals
179 pstrcpy(n2,pszFname);
180 - else if (am_server && !am_root)
181 + else if (am_server && am_root <= 0)
182 pstrcpy(n2,RSYNCD_USERCONF);
184 pstrcpy(n2,RSYNCD_SYSCONF);
187 @@ -73,7 +73,7 @@ int protocol_version = PROTOCOL_VERSION;
188 int sparse_files = 0;
189 int do_compression = 0;
190 int def_compress_level = Z_DEFAULT_COMPRESSION;
192 +int am_root = 0; /* 0 = normal, 1 = super, 2 = --super, -1 = --fake-super */
195 int am_generator = 0;
196 @@ -330,6 +330,7 @@ void usage(enum logcode F)
197 rprintf(F," -t, --times preserve times\n");
198 rprintf(F," -O, --omit-dir-times omit directories when preserving times\n");
199 rprintf(F," --super receiver attempts super-user activities\n");
200 + rprintf(F," --fake-super fake root by storing/reading ownership/etc in EAs\n");
201 rprintf(F," -S, --sparse handle sparse files efficiently\n");
202 rprintf(F," -n, --dry-run show what would have been transferred\n");
203 rprintf(F," -W, --whole-file copy files whole (without rsync algorithm)\n");
204 @@ -454,6 +455,7 @@ static struct poptOption long_options[]
205 {"modify-window", 0, POPT_ARG_INT, &modify_window, OPT_MODIFY_WINDOW, 0, 0 },
206 {"super", 0, POPT_ARG_VAL, &am_root, 2, 0, 0 },
207 {"no-super", 0, POPT_ARG_VAL, &am_root, 0, 0, 0 },
208 + {"fake-super", 0, POPT_ARG_VAL, &am_root, -1, 0, 0 },
209 {"owner", 'o', POPT_ARG_VAL, &preserve_uid, 1, 0, 0 },
210 {"no-owner", 0, POPT_ARG_VAL, &preserve_uid, 0, 0, 0 },
211 {"no-o", 0, POPT_ARG_VAL, &preserve_uid, 0, 0, 0 },
214 @@ -528,7 +528,7 @@ int recv_files(int f_in, struct file_lis
218 - } else if (do_fstat(fd1,&st) != 0) {
219 + } else if (x_fstat(fd1, &st, NULL) != 0) {
220 rsyserr(FERROR, errno, "fstat %s failed",
221 full_fname(fnamecmp));
222 discard_receive_data(f_in, file->length);
225 @@ -197,7 +197,9 @@ int set_file_attrs(char *fname, struct f
226 (long)sxp->st.st_gid, (long)file->gid);
229 - if (do_lchown(fname,
232 + else if (do_lchown(fname,
233 change_uid ? file->uid : sxp->st.st_uid,
234 change_gid ? file->gid : sxp->st.st_gid) != 0) {
235 /* shouldn't have attempted to change uid or gid
236 @@ -206,7 +208,7 @@ int set_file_attrs(char *fname, struct f
237 change_uid ? "chown" : "chgrp",
242 /* a lchown had been done - we have to re-stat if the
243 * destination had the setuid or setgid bits set due
244 * to the side effect of the chown call */
245 @@ -224,6 +226,28 @@ int set_file_attrs(char *fname, struct f
246 if (preserve_xattrs && set_xattr(fname, file, sxp) == 0)
250 + if (am_root < 0 && !S_ISLNK(file->mode)) {
251 + mode_t mode = sxp->st.st_mode & ~CHMOD_BITS;
252 + mode |= (S_ISDIR(mode) ? 0777 : 0666) & (~orig_umask | S_IRWXU);
253 + if (sxp->st.st_mode != mode)
254 + do_chmod(fname, mode);
255 + switch (set_stat_xattr(fname, file)) {
259 + rsyserr(FERROR, errno,
260 + "write of stat xattr failed for %s",
261 + full_fname(fname));
264 + rsyserr(FERROR, errno,
265 + "delete of stat xattr failed for %s",
266 + full_fname(fname));
272 /* It's OK to call set_acl() now, even for a dir, as the generator
273 * will enable owner-writability using chmod, if necessary.
274 @@ -237,7 +261,7 @@ int set_file_attrs(char *fname, struct f
277 if ((sxp->st.st_mode & CHMOD_BITS) != (new_mode & CHMOD_BITS)) {
278 - int ret = do_chmod(fname, new_mode);
279 + int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode);
281 rsyserr(FERROR, errno,
282 "failed to set permissions on %s",
290 extern int read_only;
291 extern int list_only;
292 extern int preserve_perms;
293 @@ -79,6 +80,15 @@ int do_mknod(char *pathname, mode_t mode
295 if (dry_run) return 0;
296 RETURN_ERROR_IF_RO_OR_LO;
298 + /* For --fake-super, we create a normal file with mode 0600. */
300 + int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
301 + if (fd < 0 || close(fd) < 0)
306 #if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
308 return mkfifo(pathname, mode);
315 -int dry_run, read_only, list_only, verbose;
321 int preserve_perms = 0;
328 /* These are to make syscall.o shut up. */
330 +int am_root = 0; /* TODO: add option to set this to -1. */
333 int preserve_perms = 0;
338 /* These are to make syscall.o shut up. */
343 int preserve_perms = 0;
347 #ifdef SUPPORT_XATTRS
351 +extern mode_t orig_umask;
352 extern unsigned int file_struct_len;
354 #define RSYNC_XAL_INITIAL 5
355 #define RSYNC_XAL_LIST_INITIAL 100
357 +#define FAKE_XATTR "user.rsync%stat"
362 @@ -130,9 +134,15 @@ static int rsync_xal_get(const char *fna
365 for (left = name_size, name = namebuf; left > 0 ; left -= len, name += len) {
366 - rsync_xa *rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
369 len = strlen(name) + 1;
370 + if (am_root < 0 && len == sizeof FAKE_XATTR
371 + && name[10] == '%' && strcmp(name, FAKE_XATTR) == 0)
374 + rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
376 datum_size = sys_lgetxattr(fname, name, NULL, 0);
377 if (datum_size < 0) {
378 if (errno == ENOTSUP)
379 @@ -285,10 +295,19 @@ void receive_xattr(struct file_struct *f
380 out_of_memory("receive_xattr");
381 read_buf(f, ptr, name_len);
382 read_buf(f, ptr + name_len, datum_len);
384 + if (am_root < 0 && name_len == sizeof FAKE_XATTR
385 + && ptr[10] == '%' && strcmp(ptr, FAKE_XATTR) == 0) {
387 + temp_xattr.count--;
391 rxa->name_len = name_len;
392 rxa->datum_len = datum_len;
394 rxa->datum = ptr + name_len;
396 #ifdef HAVE_OSX_XATTRS
397 if (strncmp(rxa->name, unique_prefix, upre_len) == 0) {
398 rxa->name_len -= upre_len;
399 @@ -365,4 +384,100 @@ int set_xattr(const char *fname, const s
400 return rsync_xal_set(fname, lst + ndx); /* TODO: This needs to return 1 if no xattrs changed! */
403 +int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *st)
405 + int mode, rdev_major, rdev_minor, uid, gid, len;
409 + len = sys_lgetxattr(fname, FAKE_XATTR, buf, sizeof buf - 1);
411 + len = sys_fgetxattr(fd, FAKE_XATTR, buf, sizeof buf - 1);
412 + if (len < 0 || len >= (int)sizeof buf) {
413 + if (errno == ENOTSUP || errno == ENOATTR)
419 + if (sscanf(buf, "%o %d,%d %d:%d",
420 + &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
425 + st->st_mode = mode;
426 + st->st_rdev = MAKEDEV(rdev_major, rdev_minor);
433 +int set_stat_xattr(const char *fname, struct file_struct *file)
435 + STRUCT_STAT fst, xst;
441 + if (x_stat(fname, &fst, &xst) < 0)
444 + if (IS_DEVICE(file->mode) || IS_SPECIAL(file->mode))
445 + rdev = file->u.rdev;
449 + /* This is the mode that the file will be. */
450 + fst.st_mode &= ~CHMOD_BITS;
451 + fst.st_mode |= (S_ISDIR(file->mode) ? 0777 : 0666) & ~orig_umask;
452 + if (!IS_DEVICE(fst.st_mode) && !IS_SPECIAL(fst.st_mode))
453 + fst.st_rdev = 0; /* just in case */
455 + if (fst.st_mode == file->mode && fst.st_rdev == rdev
456 + && fst.st_uid == file->uid && fst.st_gid == file->gid) {
457 + /* xst.st_mode will be 0 if there's no current stat xattr */
458 + if (xst.st_mode && sys_lremovexattr(fname, FAKE_XATTR) < 0)
463 + if (xst.st_mode != file->mode || xst.st_rdev != rdev
464 + || xst.st_uid != file->uid || xst.st_gid != file->gid) {
466 + int len = snprintf(buf, sizeof buf, "%o %u,%u %u:%u",
468 + (int)major(rdev), (int)minor(rdev),
469 + (int)file->uid, (int)file->gid);
470 + return sys_lsetxattr(fname, FAKE_XATTR, buf, len, 0);
475 +int x_stat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
477 + int ret = do_stat(fname, fst);
478 + if (get_stat_xattr(fname, -1, xst? xst : fst) != 0 && xst)
483 +int x_lstat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
485 + int ret = do_lstat(fname, fst);
486 + if (get_stat_xattr(fname, -1, xst? xst : fst) != 0 && xst)
491 +int x_fstat(int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
493 + int ret = do_fstat(fd, fst);
494 + if (get_stat_xattr(NULL, fd, xst? xst : fst) != 0 && xst)
499 #endif /* SUPPORT_XATTRS */