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