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