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