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