Always use lchmod() if it is available.
[rsync/rsync.git] / xattrs.c
CommitLineData
16edf865
WD
1/*
2 * Extended Attribute support for rsync.
3 * Written by Jay Fenlason, vaguely based on the ACLs patch.
4 *
5 * Copyright (C) 2004 Red Hat, Inc.
b3bf9b9d 6 * Copyright (C) 2006-2009 Wayne Davison
16edf865
WD
7 *
8 * This program is free software; you can redistribute it and/or modify
8e41b68e
WD
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
16edf865
WD
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
4fd842f9 19 * with this program; if not, visit the http://fsf.org website.
16edf865
WD
20 */
21
22#include "rsync.h"
1b42f628 23#include "ifuncs.h"
5dd14f0c 24#include "inums.h"
16edf865
WD
25#include "lib/sysxattrs.h"
26
27#ifdef SUPPORT_XATTRS
28
29extern int dry_run;
30extern int am_root;
31extern int am_sender;
32extern int am_generator;
33extern int read_only;
34extern int list_only;
524eaa82 35extern int preserve_xattrs;
16edf865
WD
36extern int checksum_seed;
37
38#define RSYNC_XAL_INITIAL 5
39#define RSYNC_XAL_LIST_INITIAL 100
40
41#define MAX_FULL_DATUM 32
42
43#define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
44 && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
45
46#define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
47
6283e9ef
WD
48#define XSTATE_ABBREV 1
49#define XSTATE_DONE 2
50#define XSTATE_TODO 3
16edf865
WD
51
52#define USER_PREFIX "user."
53#define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
54#define SYSTEM_PREFIX "system."
55#define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
56
57#ifdef HAVE_LINUX_XATTRS
9439c0cb
WD
58#define MIGHT_NEED_RPRE (am_root < 0)
59#define RSYNC_PREFIX USER_PREFIX "rsync."
16edf865 60#else
9439c0cb 61#define MIGHT_NEED_RPRE am_root
16edf865 62#define RSYNC_PREFIX "rsync."
16edf865 63#endif
9439c0cb
WD
64#define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
65
d724dd18
WD
66#define XSTAT_SUFFIX "stat"
67#define XSTAT_ATTR RSYNC_PREFIX "%" XSTAT_SUFFIX
68#define XACC_ACL_SUFFIX "aacl"
69#define XACC_ACL_ATTR RSYNC_PREFIX "%" XACC_ACL_SUFFIX
70#define XDEF_ACL_SUFFIX "dacl"
71#define XDEF_ACL_ATTR RSYNC_PREFIX "%" XDEF_ACL_SUFFIX
16edf865
WD
72
73typedef struct {
74 char *datum, *name;
75 size_t datum_len, name_len;
68ddbaf6 76 int num;
16edf865
WD
77} rsync_xa;
78
79static size_t namebuf_len = 0;
80static char *namebuf = NULL;
81
82static item_list empty_xattr = EMPTY_ITEM_LIST;
83static item_list rsync_xal_l = EMPTY_ITEM_LIST;
84
cb197514
WD
85static size_t prior_xattr_count = (size_t)-1;
86
16edf865
WD
87/* ------------------------------------------------------------------------- */
88
89static void rsync_xal_free(item_list *xalp)
90{
91 size_t i;
92 rsync_xa *rxas = xalp->items;
93
94 for (i = 0; i < xalp->count; i++) {
95 free(rxas[i].datum);
96 /*free(rxas[i].name);*/
97 }
98 xalp->count = 0;
99}
100
13710874 101void free_xattr(stat_x *sxp)
16edf865
WD
102{
103 if (!sxp->xattr)
104 return;
105 rsync_xal_free(sxp->xattr);
106 free(sxp->xattr);
107 sxp->xattr = NULL;
108}
109
110static int rsync_xal_compare_names(const void *x1, const void *x2)
111{
112 const rsync_xa *xa1 = x1;
113 const rsync_xa *xa2 = x2;
114 return strcmp(xa1->name, xa2->name);
115}
116
117static ssize_t get_xattr_names(const char *fname)
118{
119 ssize_t list_len;
6d56efa6 120 int64 arg;
16edf865
WD
121
122 if (!namebuf) {
123 namebuf_len = 1024;
124 namebuf = new_array(char, namebuf_len);
125 if (!namebuf)
126 out_of_memory("get_xattr_names");
127 }
128
71daa07f
WD
129 while (1) {
130 /* The length returned includes all the '\0' terminators. */
131 list_len = sys_llistxattr(fname, namebuf, namebuf_len);
132 if (list_len >= 0) {
133 if ((size_t)list_len <= namebuf_len)
134 break;
135 } else if (errno == ENOTSUP)
136 return 0;
137 else if (errno != ERANGE) {
6d56efa6 138 arg = namebuf_len;
71daa07f 139 got_error:
3f0211b6 140 rsyserr(FERROR_XFER, errno,
6d56efa6 141 "get_xattr_names: llistxattr(\"%s\",%s) failed",
adc2476f 142 fname, big_num(arg));
16edf865
WD
143 return -1;
144 }
71daa07f
WD
145 list_len = sys_llistxattr(fname, NULL, 0);
146 if (list_len < 0) {
147 arg = 0;
148 goto got_error;
149 }
16edf865
WD
150 if (namebuf_len)
151 free(namebuf);
152 namebuf_len = list_len + 1024;
153 namebuf = new_array(char, namebuf_len);
154 if (!namebuf)
155 out_of_memory("get_xattr_names");
16edf865
WD
156 }
157
71daa07f 158 return list_len;
16edf865
WD
159}
160
161/* On entry, the *len_ptr parameter contains the size of the extra space we
162 * should allocate when we create a buffer for the data. On exit, it contains
163 * the length of the datum. */
164static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr,
165 int no_missing_error)
166{
167 size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
41979f25 168 size_t extra_len = *len_ptr;
16edf865
WD
169 char *ptr;
170
41979f25
WD
171 *len_ptr = datum_len;
172
16edf865
WD
173 if (datum_len == (size_t)-1) {
174 if (errno == ENOTSUP || no_missing_error)
175 return NULL;
3f0211b6 176 rsyserr(FERROR_XFER, errno,
16edf865
WD
177 "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
178 fname, name);
179 return NULL;
180 }
181
41979f25
WD
182 if (!datum_len && !extra_len)
183 extra_len = 1; /* request non-zero amount of memory */
774d1c36
WD
184 if (datum_len + extra_len < datum_len)
185 overflow_exit("get_xattr_data");
186 if (!(ptr = new_array(char, datum_len + extra_len)))
16edf865
WD
187 out_of_memory("get_xattr_data");
188
16edf865
WD
189 if (datum_len) {
190 size_t len = sys_lgetxattr(fname, name, ptr, datum_len);
191 if (len != datum_len) {
192 if (len == (size_t)-1) {
3f0211b6 193 rsyserr(FERROR_XFER, errno,
16edf865
WD
194 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
195 " failed", fname, name, (long)datum_len);
196 } else {
3f0211b6 197 rprintf(FERROR_XFER,
16edf865
WD
198 "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
199 " returned %ld\n", fname, name,
200 (long)datum_len, (long)len);
201 }
202 free(ptr);
203 return NULL;
204 }
205 }
206
207 return ptr;
208}
209
210static int rsync_xal_get(const char *fname, item_list *xalp)
211{
212 ssize_t list_len, name_len;
213 size_t datum_len, name_offset;
214 char *name, *ptr;
215#ifdef HAVE_LINUX_XATTRS
216 int user_only = am_sender ? 0 : !am_root;
217#endif
68ddbaf6
WD
218 rsync_xa *rxa;
219 int count;
16edf865
WD
220
221 /* This puts the name list into the "namebuf" buffer. */
222 if ((list_len = get_xattr_names(fname)) < 0)
223 return -1;
224
225 for (name = namebuf; list_len > 0; name += name_len) {
16edf865
WD
226 name_len = strlen(name) + 1;
227 list_len -= name_len;
228
229#ifdef HAVE_LINUX_XATTRS
230 /* We always ignore the system namespace, and non-root
231 * ignores everything but the user namespace. */
232 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
233 : HAS_PREFIX(name, SYSTEM_PREFIX))
234 continue;
235#endif
236
524eaa82 237 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
a685271d
WD
238 if (name_len > RPRE_LEN && name[RPRE_LEN] == '%'
239 && HAS_PREFIX(name, RSYNC_PREFIX)) {
240 if ((am_sender && preserve_xattrs < 2)
d724dd18
WD
241 || (am_root < 0
242 && (strcmp(name+RPRE_LEN+1, XSTAT_SUFFIX) == 0
243 || strcmp(name+RPRE_LEN+1, XACC_ACL_SUFFIX) == 0
244 || strcmp(name+RPRE_LEN+1, XDEF_ACL_SUFFIX) == 0)))
a685271d
WD
245 continue;
246 }
9439c0cb 247
16edf865
WD
248 datum_len = name_len; /* Pass extra size to get_xattr_data() */
249 if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
250 return -1;
251
252 if (datum_len > MAX_FULL_DATUM) {
253 /* For large datums, we store a flag and a checksum. */
254 name_offset = 1 + MAX_DIGEST_LEN;
255 sum_init(checksum_seed);
256 sum_update(ptr, datum_len);
257 free(ptr);
258
259 if (!(ptr = new_array(char, name_offset + name_len)))
260 out_of_memory("rsync_xal_get");
261 *ptr = XSTATE_ABBREV;
262 sum_end(ptr + 1);
263 } else
264 name_offset = datum_len;
265
a685271d
WD
266 rxa = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
267 rxa->name = ptr + name_offset;
268 memcpy(rxa->name, name, name_len);
269 rxa->datum = ptr;
270 rxa->name_len = name_len;
271 rxa->datum_len = datum_len;
16edf865 272 }
68ddbaf6
WD
273 count = xalp->count;
274 rxa = xalp->items;
275 if (count > 1)
276 qsort(rxa, count, sizeof (rsync_xa), rsync_xal_compare_names);
277 for (rxa += count-1; count; count--, rxa--)
278 rxa->num = count;
16edf865
WD
279 return 0;
280}
281
282/* Read the xattr(s) for this filename. */
13710874 283int get_xattr(const char *fname, stat_x *sxp)
16edf865
WD
284{
285 sxp->xattr = new(item_list);
286 *sxp->xattr = empty_xattr;
0d5ebab1
WD
287
288#ifdef NO_SPECIAL_XATTRS
289 if (IS_SPECIAL(sxp->st.st_mode))
290 return 0;
291#endif
292#ifdef NO_DEVICE_XATTRS
293 if (IS_DEVICE(sxp->st.st_mode))
294 return 0;
295#endif
296#ifdef NO_SYMLINK_XATTRS
297 if (S_ISLNK(sxp->st.st_mode))
298 return 0;
299#endif
300
16edf865
WD
301 if (rsync_xal_get(fname, sxp->xattr) < 0) {
302 free_xattr(sxp);
303 return -1;
304 }
305 return 0;
306}
307
e9489cd6
WD
308int copy_xattrs(const char *source, const char *dest)
309{
310 ssize_t list_len, name_len;
311 size_t datum_len;
312 char *name, *ptr;
313#ifdef HAVE_LINUX_XATTRS
97dde6b6 314 int user_only = am_sender ? 0 : am_root <= 0;
e9489cd6
WD
315#endif
316
317 /* This puts the name list into the "namebuf" buffer. */
318 if ((list_len = get_xattr_names(source)) < 0)
319 return -1;
320
321 for (name = namebuf; list_len > 0; name += name_len) {
322 name_len = strlen(name) + 1;
323 list_len -= name_len;
324
325#ifdef HAVE_LINUX_XATTRS
326 /* We always ignore the system namespace, and non-root
327 * ignores everything but the user namespace. */
328 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
329 : HAS_PREFIX(name, SYSTEM_PREFIX))
330 continue;
331#endif
332
333 datum_len = 0;
334 if (!(ptr = get_xattr_data(source, name, &datum_len, 0)))
335 return -1;
336 if (sys_lsetxattr(dest, name, ptr, datum_len) < 0) {
337 int save_errno = errno ? errno : EINVAL;
338 rsyserr(FERROR_XFER, errno,
339 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
340 dest, name);
341 errno = save_errno;
342 return -1;
343 }
344 free(ptr);
345 }
346
347 return 0;
348}
349
16edf865
WD
350static int find_matching_xattr(item_list *xalp)
351{
352 size_t i, j;
353 item_list *lst = rsync_xal_l.items;
354
355 for (i = 0; i < rsync_xal_l.count; i++) {
356 rsync_xa *rxas1 = lst[i].items;
357 rsync_xa *rxas2 = xalp->items;
358
359 /* Wrong number of elements? */
360 if (lst[i].count != xalp->count)
361 continue;
362 /* any elements different? */
363 for (j = 0; j < xalp->count; j++) {
364 if (rxas1[j].name_len != rxas2[j].name_len
365 || rxas1[j].datum_len != rxas2[j].datum_len
366 || strcmp(rxas1[j].name, rxas2[j].name))
367 break;
368 if (rxas1[j].datum_len > MAX_FULL_DATUM) {
369 if (memcmp(rxas1[j].datum + 1,
370 rxas2[j].datum + 1,
371 MAX_DIGEST_LEN) != 0)
372 break;
373 } else {
374 if (memcmp(rxas1[j].datum, rxas2[j].datum,
375 rxas2[j].datum_len))
376 break;
377 }
378 }
379 /* no differences found. This is The One! */
380 if (j == xalp->count)
381 return i;
382 }
383
384 return -1;
385}
386
387/* Store *xalp on the end of rsync_xal_l */
388static void rsync_xal_store(item_list *xalp)
389{
390 item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
391 /* Since the following call starts a new list, we know it will hold the
392 * entire initial-count, not just enough space for one new item. */
393 *new_lst = empty_xattr;
394 (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
395 memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
396 new_lst->count = xalp->count;
397 xalp->count = 0;
398}
399
400/* Send the make_xattr()-generated xattr list for this flist entry. */
1b502f3e 401int send_xattr(int f, stat_x *sxp)
16edf865
WD
402{
403 int ndx = find_matching_xattr(sxp->xattr);
404
405 /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
f31514ad 406 write_varint(f, ndx + 1);
16edf865
WD
407
408 if (ndx < 0) {
409 rsync_xa *rxa;
410 int count = sxp->xattr->count;
f31514ad 411 write_varint(f, count);
16edf865 412 for (rxa = sxp->xattr->items; count--; rxa++) {
b4e6aac9 413 size_t name_len = rxa->name_len;
d724dd18
WD
414 const char *name = rxa->name;
415 /* Strip the rsync prefix from disguised namespaces. */
b4e6aac9 416 if (name_len > RPRE_LEN
16edf865 417#ifdef HAVE_LINUX_XATTRS
b4e6aac9 418 && am_root < 0
d724dd18 419#endif
b4e6aac9 420 && name[RPRE_LEN] != '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
d724dd18
WD
421 name += RPRE_LEN;
422 name_len -= RPRE_LEN;
423 }
424#ifndef HAVE_LINUX_XATTRS
425 else {
426 /* Put everything else in the user namespace. */
427 name_len += UPRE_LEN;
428 }
429#endif
430 write_varint(f, name_len);
f31514ad 431 write_varint(f, rxa->datum_len);
d724dd18
WD
432#ifndef HAVE_LINUX_XATTRS
433 if (name_len > rxa->name_len) {
16edf865 434 write_buf(f, USER_PREFIX, UPRE_LEN);
d724dd18 435 name_len -= UPRE_LEN;
16edf865
WD
436 }
437#endif
d724dd18 438 write_buf(f, name, name_len);
16edf865
WD
439 if (rxa->datum_len > MAX_FULL_DATUM)
440 write_buf(f, rxa->datum + 1, MAX_DIGEST_LEN);
441 else
442 write_buf(f, rxa->datum, rxa->datum_len);
443 }
444 ndx = rsync_xal_l.count; /* pre-incremented count */
445 rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
446 }
447
448 return ndx;
449}
450
451/* Return a flag indicating if we need to change a file's xattrs. If
452 * "find_all" is specified, also mark any abbreviated xattrs that we
453 * need so that send_xattr_request() can tell the sender about them. */
13710874 454int xattr_diff(struct file_struct *file, stat_x *sxp, int find_all)
16edf865
WD
455{
456 item_list *lst = rsync_xal_l.items;
457 rsync_xa *snd_rxa, *rec_rxa;
458 int snd_cnt, rec_cnt;
459 int cmp, same, xattrs_equal = 1;
460
461 if (sxp && XATTR_READY(*sxp)) {
462 rec_rxa = sxp->xattr->items;
463 rec_cnt = sxp->xattr->count;
464 } else {
465 rec_rxa = NULL;
466 rec_cnt = 0;
467 }
468
469 if (F_XATTR(file) >= 0)
470 lst += F_XATTR(file);
471 else
472 lst = &empty_xattr;
473
474 snd_rxa = lst->items;
475 snd_cnt = lst->count;
476
477 /* If the count of the sender's xattrs is different from our
478 * (receiver's) xattrs, the lists are not the same. */
479 if (snd_cnt != rec_cnt) {
480 if (!find_all)
481 return 1;
482 xattrs_equal = 0;
483 }
484
485 while (snd_cnt) {
486 cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1;
487 if (cmp > 0)
488 same = 0;
489 else if (snd_rxa->datum_len > MAX_FULL_DATUM) {
490 same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
491 && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1,
492 MAX_DIGEST_LEN) == 0;
493 /* Flag unrequested items that we need. */
494 if (!same && find_all && snd_rxa->datum[0] == XSTATE_ABBREV)
495 snd_rxa->datum[0] = XSTATE_TODO;
496 } else {
497 same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
498 && memcmp(snd_rxa->datum, rec_rxa->datum,
499 snd_rxa->datum_len) == 0;
500 }
501 if (!same) {
502 if (!find_all)
503 return 1;
504 xattrs_equal = 0;
505 }
506
507 if (cmp <= 0) {
508 snd_rxa++;
509 snd_cnt--;
510 }
511 if (cmp >= 0) {
512 rec_rxa++;
513 rec_cnt--;
514 }
515 }
516
517 if (rec_cnt)
518 xattrs_equal = 0;
519
520 return !xattrs_equal;
521}
522
7462c6ac
WD
523/* When called by the generator (with a NULL fname), this tells the sender
524 * all the abbreviated xattr values we need. When called by the sender
525 * (with a non-NULL fname), we send all the extra xattr data it needs.
526 * The generator may also call with f_out < 0 to just change all the
527 * XSTATE_ABBREV states into XSTATE_DONE. */
16edf865
WD
528void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
529{
530 item_list *lst = rsync_xal_l.items;
68ddbaf6 531 int cnt, prior_req = 0;
16edf865
WD
532 rsync_xa *rxa;
533
534 lst += F_XATTR(file);
68ddbaf6 535 for (rxa = lst->items, cnt = lst->count; cnt--; rxa++) {
16edf865
WD
536 if (rxa->datum_len <= MAX_FULL_DATUM)
537 continue;
538 switch (rxa->datum[0]) {
b769ad6a
WD
539 case XSTATE_ABBREV:
540 /* Items left abbreviated matched the sender's checksum, so
541 * the receiver will cache the local data for future use. */
542 if (am_generator)
543 rxa->datum[0] = XSTATE_DONE;
16edf865
WD
544 continue;
545 case XSTATE_TODO:
7462c6ac 546 assert(f_out >= 0);
16edf865
WD
547 break;
548 default:
549 continue;
550 }
551
552 /* Flag that we handled this abbreviated item. */
553 rxa->datum[0] = XSTATE_DONE;
554
68ddbaf6
WD
555 write_varint(f_out, rxa->num - prior_req);
556 prior_req = rxa->num;
16edf865
WD
557
558 if (fname) {
559 size_t len = 0;
560 char *ptr;
561
562 /* Re-read the long datum. */
a685271d
WD
563 if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0))) {
564 rprintf(FERROR_XFER, "failed to re-read xattr %s for %s\n", rxa->name, fname);
565 write_varint(f_out, 0);
16edf865 566 continue;
a685271d 567 }
16edf865 568
f31514ad 569 write_varint(f_out, len); /* length might have changed! */
16edf865
WD
570 write_buf(f_out, ptr, len);
571 free(ptr);
572 }
573 }
574
7462c6ac
WD
575 if (f_out >= 0)
576 write_byte(f_out, 0); /* end the list */
16edf865
WD
577}
578
16edf865
WD
579/* When called by the sender, read the request from the generator and mark
580 * any needed xattrs with a flag that lets us know they need to be sent to
581 * the receiver. When called by the receiver, reads the sent data and
582 * stores it in place of its checksum. */
e107b6b1 583int recv_xattr_request(struct file_struct *file, int f_in)
16edf865
WD
584{
585 item_list *lst = rsync_xal_l.items;
586 char *old_datum, *name;
587 rsync_xa *rxa;
68ddbaf6 588 int rel_pos, cnt, num, got_xattr_data = 0;
16edf865
WD
589
590 if (F_XATTR(file) < 0) {
591 rprintf(FERROR, "recv_xattr_request: internal data error!\n");
e4c598c8 592 exit_cleanup(RERR_PROTOCOL);
16edf865
WD
593 }
594 lst += F_XATTR(file);
595
596 cnt = lst->count;
597 rxa = lst->items;
68ddbaf6 598 num = 0;
f31514ad 599 while ((rel_pos = read_varint(f_in)) != 0) {
68ddbaf6
WD
600 num += rel_pos;
601 while (cnt && rxa->num < num) {
602 rxa++;
603 cnt--;
604 }
605 if (!cnt || rxa->num != num) {
606 rprintf(FERROR, "[%s] could not find xattr #%d for %s\n",
607 who_am_i(), num, f_name(file, NULL));
e4c598c8 608 exit_cleanup(RERR_PROTOCOL);
68ddbaf6 609 }
6283e9ef 610 if (!XATTR_ABBREV(*rxa) || rxa->datum[0] != XSTATE_ABBREV) {
e424e261
WD
611 rprintf(FERROR, "[%s] internal abbrev error on %s (%s, len=%ld)!\n",
612 who_am_i(), f_name(file, NULL), rxa->name, (long)rxa->datum_len);
e4c598c8 613 exit_cleanup(RERR_PROTOCOL);
16edf865
WD
614 }
615
616 if (am_sender) {
617 rxa->datum[0] = XSTATE_TODO;
618 continue;
619 }
620
621 old_datum = rxa->datum;
f31514ad 622 rxa->datum_len = read_varint(f_in);
16edf865
WD
623
624 if (rxa->name_len + rxa->datum_len < rxa->name_len)
774d1c36 625 overflow_exit("recv_xattr_request");
16edf865
WD
626 rxa->datum = new_array(char, rxa->datum_len + rxa->name_len);
627 if (!rxa->datum)
628 out_of_memory("recv_xattr_request");
629 name = rxa->datum + rxa->datum_len;
630 memcpy(name, rxa->name, rxa->name_len);
631 rxa->name = name;
632 free(old_datum);
633 read_buf(f_in, rxa->datum, rxa->datum_len);
e107b6b1 634 got_xattr_data = 1;
16edf865 635 }
e107b6b1
WD
636
637 return got_xattr_data;
16edf865
WD
638}
639
640/* ------------------------------------------------------------------------- */
641
642/* receive and build the rsync_xattr_lists */
1b502f3e 643void receive_xattr(int f, struct file_struct *file)
16edf865
WD
644{
645 static item_list temp_xattr = EMPTY_ITEM_LIST;
68ddbaf6 646 int count, num;
d724dd18
WD
647#ifdef HAVE_LINUX_XATTRS
648 int need_sort = 0;
649#else
650 int need_sort = 1;
651#endif
f31514ad 652 int ndx = read_varint(f);
16edf865
WD
653
654 if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) {
655 rprintf(FERROR, "receive_xattr: xa index %d out of"
656 " range for %s\n", ndx, f_name(file, NULL));
657 exit_cleanup(RERR_STREAMIO);
658 }
659
660 if (ndx != 0) {
661 F_XATTR(file) = ndx - 1;
662 return;
663 }
664
f31514ad 665 if ((count = read_varint(f)) != 0) {
16edf865
WD
666 (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
667 temp_xattr.count = 0;
668 }
669
68ddbaf6 670 for (num = 1; num <= count; num++) {
16edf865
WD
671 char *ptr, *name;
672 rsync_xa *rxa;
f31514ad
WD
673 size_t name_len = read_varint(f);
674 size_t datum_len = read_varint(f);
16edf865 675 size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len;
9439c0cb 676 size_t extra_len = MIGHT_NEED_RPRE ? RPRE_LEN : 0;
774d1c36
WD
677 if ((dget_len + extra_len < dget_len)
678 || (dget_len + extra_len + name_len < dget_len))
679 overflow_exit("receive_xattr");
16edf865
WD
680 ptr = new_array(char, dget_len + extra_len + name_len);
681 if (!ptr)
682 out_of_memory("receive_xattr");
683 name = ptr + dget_len + extra_len;
684 read_buf(f, name, name_len);
685 if (dget_len == datum_len)
686 read_buf(f, ptr, dget_len);
687 else {
688 *ptr = XSTATE_ABBREV;
689 read_buf(f, ptr + 1, MAX_DIGEST_LEN);
690 }
691#ifdef HAVE_LINUX_XATTRS
692 /* Non-root can only save the user namespace. */
68ddbaf6
WD
693 if (am_root <= 0 && !HAS_PREFIX(name, USER_PREFIX)) {
694 if (!am_root) {
695 free(ptr);
696 continue;
697 }
698 name -= RPRE_LEN;
699 name_len += RPRE_LEN;
700 memcpy(name, RSYNC_PREFIX, RPRE_LEN);
d724dd18 701 need_sort = 1;
16edf865
WD
702 }
703#else
704 /* This OS only has a user namespace, so we either
705 * strip the user prefix, or we put a non-user
706 * namespace inside our rsync hierarchy. */
707 if (HAS_PREFIX(name, USER_PREFIX)) {
708 name += UPRE_LEN;
709 name_len -= UPRE_LEN;
710 } else if (am_root) {
711 name -= RPRE_LEN;
712 name_len += RPRE_LEN;
713 memcpy(name, RSYNC_PREFIX, RPRE_LEN);
714 } else {
715 free(ptr);
716 continue;
717 }
718#endif
524eaa82
WD
719 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
720 if (preserve_xattrs < 2 && name_len > RPRE_LEN
721 && name[RPRE_LEN] == '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
9439c0cb
WD
722 free(ptr);
723 continue;
724 }
16edf865
WD
725 rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
726 rxa->name = name;
727 rxa->datum = ptr;
728 rxa->name_len = name_len;
729 rxa->datum_len = datum_len;
68ddbaf6 730 rxa->num = num;
16edf865
WD
731 }
732
d724dd18
WD
733 if (need_sort && count > 1)
734 qsort(temp_xattr.items, count, sizeof (rsync_xa), rsync_xal_compare_names);
735
16edf865
WD
736 ndx = rsync_xal_l.count; /* pre-incremented count */
737 rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
738
739 F_XATTR(file) = ndx;
740}
741
13710874 742/* Turn the xattr data in stat_x into cached xattr data, setting the index
16edf865 743 * values in the file struct. */
cb197514 744void cache_tmp_xattr(struct file_struct *file, stat_x *sxp)
16edf865
WD
745{
746 int ndx;
747
748 if (!sxp->xattr)
749 return;
750
cb197514
WD
751 if (prior_xattr_count == (size_t)-1)
752 prior_xattr_count = rsync_xal_l.count;
16edf865
WD
753 ndx = find_matching_xattr(sxp->xattr);
754 if (ndx < 0)
755 rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
756
757 F_XATTR(file) = ndx;
758}
759
cb197514
WD
760void uncache_tmp_xattrs(void)
761{
762 if (prior_xattr_count != (size_t)-1) {
763 item_list *xattr_item = rsync_xal_l.items;
764 item_list *xattr_start = xattr_item + prior_xattr_count;
765 xattr_item += rsync_xal_l.count;
766 rsync_xal_l.count = prior_xattr_count;
767 while (xattr_item-- > xattr_start) {
768 rsync_xal_free(xattr_item);
4c3e9c09 769 free(xattr_item->items);
cb197514
WD
770 }
771 prior_xattr_count = (size_t)-1;
772 }
773}
774
16edf865 775static int rsync_xal_set(const char *fname, item_list *xalp,
13710874 776 const char *fnamecmp, stat_x *sxp)
16edf865
WD
777{
778 rsync_xa *rxas = xalp->items;
779 ssize_t list_len;
780 size_t i, len;
781 char *name, *ptr, sum[MAX_DIGEST_LEN];
97dde6b6
WD
782#ifdef HAVE_LINUX_XATTRS
783 int user_only = am_root <= 0;
784#endif
b4e6aac9
WD
785 size_t name_len;
786 int ret = 0;
16edf865
WD
787
788 /* This puts the current name list into the "namebuf" buffer. */
789 if ((list_len = get_xattr_names(fname)) < 0)
790 return -1;
791
792 for (i = 0; i < xalp->count; i++) {
793 name = rxas[i].name;
794
795 if (XATTR_ABBREV(rxas[i])) {
796 /* See if the fnamecmp version is identical. */
797 len = name_len = rxas[i].name_len;
798 if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
799 still_abbrev:
800 if (am_generator)
801 continue;
802 rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n",
803 rxas[i].name, full_fname(fname));
804 ret = -1;
805 continue;
806 }
807 if (len != rxas[i].datum_len) {
808 free(ptr);
809 goto still_abbrev;
810 }
811
812 sum_init(checksum_seed);
813 sum_update(ptr, len);
814 sum_end(sum);
815 if (memcmp(sum, rxas[i].datum + 1, MAX_DIGEST_LEN) != 0) {
816 free(ptr);
817 goto still_abbrev;
818 }
819
820 if (fname == fnamecmp)
821 ; /* Value is already set when identical */
822 else if (sys_lsetxattr(fname, name, ptr, len) < 0) {
3f0211b6 823 rsyserr(FERROR_XFER, errno,
16edf865
WD
824 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
825 fname, name);
826 ret = -1;
827 } else /* make sure caller sets mtime */
828 sxp->st.st_mtime = (time_t)-1;
829
830 if (am_generator) { /* generator items stay abbreviated */
16edf865
WD
831 free(ptr);
832 continue;
833 }
834
835 memcpy(ptr + len, name, name_len);
836 free(rxas[i].datum);
837
838 rxas[i].name = name = ptr + len;
839 rxas[i].datum = ptr;
840 continue;
841 }
842
843 if (sys_lsetxattr(fname, name, rxas[i].datum, rxas[i].datum_len) < 0) {
3f0211b6 844 rsyserr(FERROR_XFER, errno,
16edf865
WD
845 "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
846 fname, name);
847 ret = -1;
848 } else /* make sure caller sets mtime */
849 sxp->st.st_mtime = (time_t)-1;
850 }
851
852 /* Remove any extraneous names. */
853 for (name = namebuf; list_len > 0; name += name_len) {
854 name_len = strlen(name) + 1;
855 list_len -= name_len;
856
857#ifdef HAVE_LINUX_XATTRS
858 /* We always ignore the system namespace, and non-root
859 * ignores everything but the user namespace. */
97dde6b6
WD
860 if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
861 : HAS_PREFIX(name, SYSTEM_PREFIX))
16edf865
WD
862 continue;
863#endif
e107b6b1
WD
864 if (am_root < 0 && name_len > RPRE_LEN
865 && name[RPRE_LEN] == '%' && strcmp(name, XSTAT_ATTR) == 0)
866 continue;
16edf865
WD
867
868 for (i = 0; i < xalp->count; i++) {
869 if (strcmp(name, rxas[i].name) == 0)
870 break;
871 }
872 if (i == xalp->count) {
873 if (sys_lremovexattr(fname, name) < 0) {
3f0211b6 874 rsyserr(FERROR_XFER, errno,
16edf865
WD
875 "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
876 fname, name);
877 ret = -1;
878 } else /* make sure caller sets mtime */
879 sxp->st.st_mtime = (time_t)-1;
880 }
881 }
882
883 return ret;
884}
885
886/* Set extended attributes on indicated filename. */
887int set_xattr(const char *fname, const struct file_struct *file,
13710874 888 const char *fnamecmp, stat_x *sxp)
16edf865
WD
889{
890 int ndx;
891 item_list *lst = rsync_xal_l.items;
892
893 if (dry_run)
894 return 1; /* FIXME: --dry-run needs to compute this value */
895
896 if (read_only || list_only) {
897 errno = EROFS;
898 return -1;
899 }
900
0d5ebab1
WD
901#ifdef NO_SPECIAL_XATTRS
902 if (IS_SPECIAL(sxp->st.st_mode)) {
903 errno = ENOTSUP;
904 return -1;
905 }
906#endif
907#ifdef NO_DEVICE_XATTRS
908 if (IS_DEVICE(sxp->st.st_mode)) {
909 errno = ENOTSUP;
910 return -1;
911 }
912#endif
913#ifdef NO_SYMLINK_XATTRS
914 if (S_ISLNK(sxp->st.st_mode)) {
915 errno = ENOTSUP;
916 return -1;
917 }
918#endif
919
16edf865
WD
920 ndx = F_XATTR(file);
921 return rsync_xal_set(fname, lst + ndx, fnamecmp, sxp);
922}
923
7ed6bc53
WD
924#ifdef SUPPORT_ACLS
925char *get_xattr_acl(const char *fname, int is_access_acl, size_t *len_p)
926{
927 const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
e516b69e 928 *len_p = 0; /* no extra data alloc needed from get_xattr_data() */
7ed6bc53
WD
929 return get_xattr_data(fname, name, len_p, 1);
930}
931
932int set_xattr_acl(const char *fname, int is_access_acl, const char *buf, size_t buf_len)
933{
934 const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
935 if (sys_lsetxattr(fname, name, buf, buf_len) < 0) {
3f0211b6 936 rsyserr(FERROR_XFER, errno,
7ed6bc53
WD
937 "set_xattr_acl: lsetxattr(\"%s\",\"%s\") failed",
938 fname, name);
939 return -1;
940 }
941 return 0;
942}
943
944int del_def_xattr_acl(const char *fname)
945{
946 return sys_lremovexattr(fname, XDEF_ACL_ATTR);
947}
948#endif
949
9439c0cb
WD
950int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
951{
952 int mode, rdev_major, rdev_minor, uid, gid, len;
953 char buf[256];
954
955 if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode))
956 return -1;
957
958 if (xst)
959 *xst = *fst;
960 else
961 xst = fst;
962 if (fname) {
963 fd = -1;
964 len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1);
965 } else {
966 fname = "fd";
967 len = sys_fgetxattr(fd, XSTAT_ATTR, buf, sizeof buf - 1);
968 }
969 if (len >= (int)sizeof buf) {
970 len = -1;
971 errno = ERANGE;
972 }
973 if (len < 0) {
974 if (errno == ENOTSUP || errno == ENOATTR)
975 return -1;
976 if (errno == EPERM && S_ISLNK(fst->st_mode)) {
977 xst->st_uid = 0;
978 xst->st_gid = 0;
979 return 0;
980 }
3f0211b6 981 rsyserr(FERROR_XFER, errno, "failed to read xattr %s for %s",
9439c0cb
WD
982 XSTAT_ATTR, full_fname(fname));
983 return -1;
984 }
985 buf[len] = '\0';
986
987 if (sscanf(buf, "%o %d,%d %d:%d",
988 &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
989 rprintf(FERROR, "Corrupt %s xattr attached to %s: \"%s\"\n",
990 XSTAT_ATTR, full_fname(fname), buf);
991 exit_cleanup(RERR_FILEIO);
992 }
993
994 xst->st_mode = from_wire_mode(mode);
995 xst->st_rdev = MAKEDEV(rdev_major, rdev_minor);
996 xst->st_uid = uid;
997 xst->st_gid = gid;
998
999 return 0;
1000}
1001
e107b6b1 1002int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode)
9439c0cb
WD
1003{
1004 STRUCT_STAT fst, xst;
1005 dev_t rdev;
1006 mode_t mode, fmode;
1007
1008 if (dry_run)
1009 return 0;
1010
1011 if (read_only || list_only) {
3f0211b6 1012 rsyserr(FERROR_XFER, EROFS, "failed to write xattr %s for %s",
9439c0cb
WD
1013 XSTAT_ATTR, full_fname(fname));
1014 return -1;
1015 }
1016
1017 if (x_lstat(fname, &fst, &xst) < 0) {
3f0211b6 1018 rsyserr(FERROR_XFER, errno, "failed to re-stat %s",
9439c0cb
WD
1019 full_fname(fname));
1020 return -1;
1021 }
1022
1023 fst.st_mode &= (_S_IFMT | CHMOD_BITS);
e107b6b1 1024 fmode = new_mode & (_S_IFMT | CHMOD_BITS);
9439c0cb 1025
a8e6e148 1026 if (IS_DEVICE(fmode)) {
9439c0cb
WD
1027 uint32 *devp = F_RDEV_P(file);
1028 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1029 } else
1030 rdev = 0;
1031
1032 /* Dump the special permissions and enable full owner access. */
1033 mode = (fst.st_mode & _S_IFMT) | (fmode & ACCESSPERMS)
1034 | (S_ISDIR(fst.st_mode) ? 0700 : 0600);
1035 if (fst.st_mode != mode)
1036 do_chmod(fname, mode);
a8e6e148 1037 if (!IS_DEVICE(fst.st_mode))
9439c0cb
WD
1038 fst.st_rdev = 0; /* just in case */
1039
1040 if (mode == fmode && fst.st_rdev == rdev
fb7b9ddc 1041 && fst.st_uid == F_OWNER(file) && fst.st_gid == F_GROUP(file)) {
9439c0cb
WD
1042 /* xst.st_mode will be 0 if there's no current stat xattr */
1043 if (xst.st_mode && sys_lremovexattr(fname, XSTAT_ATTR) < 0) {
3f0211b6 1044 rsyserr(FERROR_XFER, errno,
9439c0cb
WD
1045 "delete of stat xattr failed for %s",
1046 full_fname(fname));
1047 return -1;
1048 }
1049 return 0;
1050 }
1051
1052 if (xst.st_mode != fmode || xst.st_rdev != rdev
fb7b9ddc 1053 || xst.st_uid != F_OWNER(file) || xst.st_gid != F_GROUP(file)) {
9439c0cb
WD
1054 char buf[256];
1055 int len = snprintf(buf, sizeof buf, "%o %u,%u %u:%u",
1056 to_wire_mode(fmode),
1057 (int)major(rdev), (int)minor(rdev),
fb7b9ddc 1058 F_OWNER(file), F_GROUP(file));
9439c0cb
WD
1059 if (sys_lsetxattr(fname, XSTAT_ATTR, buf, len) < 0) {
1060 if (errno == EPERM && S_ISLNK(fst.st_mode))
1061 return 0;
3f0211b6 1062 rsyserr(FERROR_XFER, errno,
9439c0cb
WD
1063 "failed to write xattr %s for %s",
1064 XSTAT_ATTR, full_fname(fname));
1065 return -1;
1066 }
1067 }
1068
1069 return 0;
1070}
1071
1072int x_stat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
1073{
1074 int ret = do_stat(fname, fst);
1075 if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
1076 xst->st_mode = 0;
1077 return ret;
1078}
1079
1080int x_lstat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
1081{
1082 int ret = do_lstat(fname, fst);
1083 if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
1084 xst->st_mode = 0;
1085 return ret;
1086}
1087
1088int x_fstat(int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
1089{
1090 int ret = do_fstat(fd, fst);
1091 if ((ret < 0 || get_stat_xattr(NULL, fd, fst, xst) < 0) && xst)
1092 xst->st_mode = 0;
1093 return ret;
1094}
1095
16edf865 1096#endif /* SUPPORT_XATTRS */