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