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