Fixed the code that maintains the files with a consistent mode (so
[rsync/rsync-patches.git] / fake-super.diff
CommitLineData
c44efccb
WD
1Depends-On-Patch: acls.diff
2Depends-On-Patch: xattrs.diff
3
4This patch adds a new option: --fake-super, which tells rsync to copy in a
5fake super-user mode that stores various file attributes in an extended-
6attribute value instead of as real file-system attributes. The items
7affected are:
8
9 mode the real mode of a file is always (666 & umask) while
10 the real mode of a directory is always (777 & umask).
11
12 rdev devices and special files are created as zero-length
13 normal files.
14
15 uid the real owner is always left unchanged.
16
17 gid the real group is always left unchanged.
18
19A daemon can set "fake super = yes" in the rsync.conf file for any module
20that you'd like to run without root perms while pretending it has them (the
21client cannot affect this).
22
23The --fake-super option only affects the side where the option is used. To
24affect the remote side of a remote-shell connection, specify an rsync path:
25
26 rsync -av --rsync-path='rsync --fake-super' /src/ host:/dest/
27
28For a local copy where you want to affect only one side or the other,
29you'll need to turn the copy into a remote copy to localhost.
30
31After applying this patch, run these commands for a successful build:
32
33 ./prepare-source
34 ./configure
35 make
36
e66b9b1f
WD
37--- old/backup.c
38+++ new/backup.c
39@@ -129,7 +129,7 @@ static int make_bak_dir(char *fullpath)
40 if (p >= rel) {
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",
47 full_fname(rel));
48@@ -200,7 +200,7 @@ static int keep_backup(char *fname)
49 int ret_code;
c44efccb 50
e66b9b1f
WD
51 /* return if no file to keep */
52- if (do_lstat(fname, &sx.st) < 0)
53+ if (x_lstat(fname, &sx.st, NULL) < 0)
54 return 1;
55 #ifdef SUPPORT_ACLS
56 sx.acc_acl = sx.def_acl = NULL;
c44efccb
WD
57--- old/clientserver.c
58+++ new/clientserver.c
611f9f3c
WD
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. */
c44efccb 62
611f9f3c 63+ if (lp_fake_super(i))
c44efccb 64+ am_root = -1;
611f9f3c
WD
65+ else if (am_root < 0) /* Treat --fake-super from client as --super. */
66+ am_root = 2;
c44efccb 67+
611f9f3c
WD
68 if (filesfrom_fd == 0)
69 filesfrom_fd = f_in;
70
e66b9b1f
WD
71--- old/flist.c
72+++ new/flist.c
73@@ -181,7 +181,7 @@ static int readlink_stat(const char *pat
74 }
75 return 0;
76 #else
77- return do_stat(path, stp);
78+ return x_stat(path, stp, NULL);
79 #endif
80 }
81
82@@ -189,17 +189,17 @@ int link_stat(const char *path, STRUCT_S
83 {
84 #ifdef SUPPORT_LINKS
85 if (copy_links)
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)
90 return -1;
91 if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
92 STRUCT_STAT st;
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))
95 *stp = st;
96 }
97 return 0;
98 #else
99- return do_stat(path, stp);
100+ return x_stat(path, stp, NULL);
101 #endif
102 }
103
104@@ -793,7 +793,7 @@ struct file_struct *make_file(char *fnam
105 if (save_errno == ENOENT) {
106 #ifdef SUPPORT_LINKS
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;
c44efccb
WD
122--- old/generator.c
123+++ new/generator.c
124@@ -1510,13 +1510,14 @@ void generate_files(int f_out, struct fi
125 recv_generator(fbuf, file, i, itemizing, maybe_ATTRS_REPORT,
126 code, f_out);
127
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. */
132 #ifdef HAVE_CHMOD
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
136 && dir_tweaking) {
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,
142--- old/loadparm.c
143+++ new/loadparm.c
144@@ -150,6 +150,7 @@ typedef struct
145 int syslog_facility;
146 int timeout;
147
148+ BOOL fake_super;
149 BOOL ignore_errors;
150 BOOL ignore_nonreadable;
151 BOOL list;
152@@ -197,6 +198,7 @@ static service sDefault =
153 /* syslog_facility; */ LOG_DAEMON,
154 /* timeout; */ 0,
155
156+ /* fake_super; */ False,
157 /* ignore_errors; */ False,
158 /* ignore_nonreadable; */ False,
159 /* list; */ True,
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)
171
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
177
178 if (pszFname)
179 pstrcpy(n2,pszFname);
180- else if (am_server && !am_root)
181+ else if (am_server && am_root <= 0)
182 pstrcpy(n2,RSYNCD_USERCONF);
183 else
184 pstrcpy(n2,RSYNCD_SYSCONF);
185--- old/options.c
186+++ new/options.c
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;
191-int am_root = 0;
192+int am_root = 0; /* 0 = normal, 1 = super, 2 = --super, -1 = --fake-super */
193 int am_server = 0;
194 int am_sender = 0;
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 },
e66b9b1f
WD
212--- old/receiver.c
213+++ new/receiver.c
214@@ -528,7 +528,7 @@ int recv_files(int f_in, struct file_lis
215 if (fd1 == -1) {
216 st.st_mode = 0;
217 st.st_size = 0;
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);
c44efccb
WD
223--- old/rsync.c
224+++ new/rsync.c
3ae9c1ee 225@@ -197,7 +197,9 @@ int set_file_attrs(char *fname, struct f
c44efccb
WD
226 (long)sxp->st.st_gid, (long)file->gid);
227 }
228 }
3ae9c1ee
WD
229- if (do_lchown(fname,
230+ if (am_root < 0)
231+ ;
232+ else if (do_lchown(fname,
c44efccb
WD
233 change_uid ? file->uid : sxp->st.st_uid,
234 change_gid ? file->gid : sxp->st.st_gid) != 0) {
3ae9c1ee
WD
235 /* shouldn't have attempted to change uid or gid
236@@ -206,7 +208,7 @@ int set_file_attrs(char *fname, struct f
c44efccb
WD
237 change_uid ? "chown" : "chgrp",
238 full_fname(fname));
239 goto cleanup;
240- }
241+ } else
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 */
9b91764c 245@@ -224,6 +226,28 @@ int set_file_attrs(char *fname, struct f
e66b9b1f
WD
246 if (preserve_xattrs && set_xattr(fname, file, sxp) == 0)
247 updated = 1;
c44efccb 248 #endif
e66b9b1f
WD
249+
250+ if (am_root < 0 && !S_ISLNK(file->mode)) {
9b91764c
WD
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);
3ae9c1ee
WD
255+ switch (set_stat_xattr(fname, file)) {
256+ case 0:
257+ break;
258+ case -1:
c44efccb
WD
259+ rsyserr(FERROR, errno,
260+ "write of stat xattr failed for %s",
261+ full_fname(fname));
3ae9c1ee
WD
262+ break;
263+ case -2:
264+ rsyserr(FERROR, errno,
265+ "delete of stat xattr failed for %s",
266+ full_fname(fname));
267+ break;
c44efccb
WD
268+ }
269+ }
270+
e66b9b1f
WD
271 #ifdef SUPPORT_ACLS
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.
9b91764c 274@@ -237,7 +261,7 @@ int set_file_attrs(char *fname, struct f
c44efccb 275
e66b9b1f
WD
276 #ifdef HAVE_CHMOD
277 if ((sxp->st.st_mode & CHMOD_BITS) != (new_mode & CHMOD_BITS)) {
278- int ret = do_chmod(fname, new_mode);
9b91764c 279+ int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode);
e66b9b1f
WD
280 if (ret < 0) {
281 rsyserr(FERROR, errno,
282 "failed to set permissions on %s",
c44efccb
WD
283--- old/syscall.c
284+++ new/syscall.c
e66b9b1f 285@@ -28,6 +28,7 @@
c44efccb
WD
286 #endif
287
288 extern int dry_run;
289+extern int am_root;
290 extern int read_only;
291 extern int list_only;
292 extern int preserve_perms;
e66b9b1f 293@@ -79,6 +80,15 @@ int do_mknod(char *pathname, mode_t mode
c44efccb
WD
294 {
295 if (dry_run) return 0;
296 RETURN_ERROR_IF_RO_OR_LO;
297+
298+ /* For --fake-super, we create a normal file with mode 0600. */
299+ if (am_root < 0) {
300+ int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
301+ if (fd < 0 || close(fd) < 0)
302+ return -1;
303+ return 0;
304+ }
305+
306 #if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
307 if (S_ISFIFO(mode))
308 return mkfifo(pathname, mode);
c44efccb
WD
309--- old/t_unsafe.c
310+++ new/t_unsafe.c
311@@ -24,7 +24,11 @@
312
313 #include "rsync.h"
314
315-int dry_run, read_only, list_only, verbose;
316+int dry_run = 0;
317+int am_root = 0;
318+int read_only = 0;
319+int list_only = 0;
320+int verbose = 0;
321 int preserve_perms = 0;
322
323 int
324--- old/tls.c
325+++ new/tls.c
326@@ -39,6 +39,7 @@
327
328 /* These are to make syscall.o shut up. */
329 int dry_run = 0;
330+int am_root = 0; /* TODO: add option to set this to -1. */
331 int read_only = 1;
332 int list_only = 0;
333 int preserve_perms = 0;
334--- old/trimslash.c
335+++ new/trimslash.c
336@@ -23,6 +23,7 @@
337
338 /* These are to make syscall.o shut up. */
339 int dry_run = 0;
340+int am_root = 0;
341 int read_only = 1;
342 int list_only = 0;
343 int preserve_perms = 0;
344--- old/xattr.c
345+++ new/xattr.c
e66b9b1f 346@@ -26,11 +26,15 @@
c44efccb
WD
347 #ifdef SUPPORT_XATTRS
348
349 extern int dry_run;
350+extern int am_root;
e66b9b1f 351+extern mode_t orig_umask;
c44efccb
WD
352 extern unsigned int file_struct_len;
353
354 #define RSYNC_XAL_INITIAL 5
e66b9b1f
WD
355 #define RSYNC_XAL_LIST_INITIAL 100
356
357+#define FAKE_XATTR "user.rsync%stat"
358+
359 typedef struct {
360 char *name;
361 char *datum;
362@@ -130,9 +134,15 @@ static int rsync_xal_get(const char *fna
c44efccb
WD
363 if (name_size == 0)
364 return 0;
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);
367+ rsync_xa *rxas;
368
369 len = strlen(name) + 1;
370+ if (am_root < 0 && len == sizeof FAKE_XATTR
d30eff93 371+ && name[10] == '%' && strcmp(name, FAKE_XATTR) == 0)
c44efccb
WD
372+ continue;
373+
374+ rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
375+
376 datum_size = sys_lgetxattr(fname, name, NULL, 0);
377 if (datum_size < 0) {
378 if (errno == ENOTSUP)
e66b9b1f 379@@ -285,10 +295,19 @@ void receive_xattr(struct file_struct *f
c44efccb
WD
380 out_of_memory("receive_xattr");
381 read_buf(f, ptr, name_len);
382 read_buf(f, ptr + name_len, datum_len);
383+
384+ if (am_root < 0 && name_len == sizeof FAKE_XATTR
d30eff93 385+ && ptr[10] == '%' && strcmp(ptr, FAKE_XATTR) == 0) {
c44efccb
WD
386+ free(ptr);
387+ temp_xattr.count--;
388+ continue;
389+ }
390+
391 rxa->name_len = name_len;
392 rxa->datum_len = datum_len;
393 rxa->name = ptr;
394 rxa->datum = ptr + name_len;
395+
396 #ifdef HAVE_OSX_XATTRS
397 if (strncmp(rxa->name, unique_prefix, upre_len) == 0) {
398 rxa->name_len -= upre_len;
e66b9b1f
WD
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! */
401 }
402
403+int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *st)
404+{
405+ int mode, rdev_major, rdev_minor, uid, gid, len;
406+ char buf[256];
407+
408+ if (fname)
409+ len = sys_lgetxattr(fname, FAKE_XATTR, buf, sizeof buf - 1);
410+ else
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)
414+ return -1;
415+ return -1;
416+ }
417+ buf[len] = '\0';
418+
419+ if (sscanf(buf, "%o %d,%d %d:%d",
420+ &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
421+ errno = EINVAL;
422+ return -1;
423+ }
424+
425+ st->st_mode = mode;
426+ st->st_rdev = MAKEDEV(rdev_major, rdev_minor);
427+ st->st_uid = uid;
428+ st->st_gid = gid;
429+
430+ return 0;
431+}
432+
433+int set_stat_xattr(const char *fname, struct file_struct *file)
434+{
435+ STRUCT_STAT fst, xst;
436+ dev_t rdev;
437+
438+ if (dry_run)
439+ return 0;
440+
441+ if (x_stat(fname, &fst, &xst) < 0)
442+ return -1;
443+
444+ if (IS_DEVICE(file->mode) || IS_SPECIAL(file->mode))
445+ rdev = file->u.rdev;
446+ else
447+ rdev = 0;
448+
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 */
454+
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)
459+ return -2;
460+ return 0;
461+ }
462+
463+ if (xst.st_mode != file->mode || xst.st_rdev != rdev
464+ || xst.st_uid != file->uid || xst.st_gid != file->gid) {
465+ char buf[256];
466+ int len = snprintf(buf, sizeof buf, "%o %u,%u %u:%u",
467+ (int)file->mode,
468+ (int)major(rdev), (int)minor(rdev),
469+ (int)file->uid, (int)file->gid);
470+ return sys_lsetxattr(fname, FAKE_XATTR, buf, len, 0);
471+ }
472+ return 0;
473+}
474+
475+int x_stat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
476+{
477+ int ret = do_stat(fname, fst);
478+ if (get_stat_xattr(fname, -1, xst? xst : fst) != 0 && xst)
479+ xst->st_mode = 0;
480+ return ret;
481+}
482+
483+int x_lstat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
484+{
485+ int ret = do_lstat(fname, fst);
486+ if (get_stat_xattr(fname, -1, xst? xst : fst) != 0 && xst)
487+ xst->st_mode = 0;
488+ return ret;
489+}
490+
491+int x_fstat(int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
492+{
493+ int ret = do_fstat(fd, fst);
494+ if (get_stat_xattr(NULL, fd, xst? xst : fst) != 0 && xst)
495+ xst->st_mode = 0;
496+ return ret;
497+}
498+
499 #endif /* SUPPORT_XATTRS */