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