Fix --remove-source-files sanity check w/--copy-links the right way.
[rsync.git] / xattrs.c
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-2018 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 "inums.h"
25 #include "lib/sysxattrs.h"
26
27 #ifdef SUPPORT_XATTRS
28
29 extern int dry_run;
30 extern int am_root;
31 extern int am_sender;
32 extern int am_generator;
33 extern int read_only;
34 extern int list_only;
35 extern int preserve_xattrs;
36 extern int preserve_links;
37 extern int preserve_devices;
38 extern int preserve_specials;
39 extern int checksum_seed;
40 extern int saw_xattr_filter;
41
42 #define RSYNC_XAL_INITIAL 5
43 #define RSYNC_XAL_LIST_INITIAL 100
44
45 #define MAX_FULL_DATUM 32
46
47 #define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
48                             && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
49
50 #define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
51
52 #define XSTATE_ABBREV   1
53 #define XSTATE_DONE     2
54 #define XSTATE_TODO     3
55
56 #define USER_PREFIX "user."
57 #define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
58 #define SYSTEM_PREFIX "system."
59 #define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
60
61 #ifdef HAVE_LINUX_XATTRS
62 #define MIGHT_NEED_RPRE (am_root < 0)
63 #define RSYNC_PREFIX USER_PREFIX "rsync."
64 #else
65 #define MIGHT_NEED_RPRE am_root
66 #define RSYNC_PREFIX "rsync."
67 #endif
68 #define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
69
70 #define XSTAT_SUFFIX "stat"
71 #define XSTAT_ATTR RSYNC_PREFIX "%" XSTAT_SUFFIX
72 #define XACC_ACL_SUFFIX "aacl"
73 #define XACC_ACL_ATTR RSYNC_PREFIX "%" XACC_ACL_SUFFIX
74 #define XDEF_ACL_SUFFIX "dacl"
75 #define XDEF_ACL_ATTR RSYNC_PREFIX "%" XDEF_ACL_SUFFIX
76
77 typedef struct {
78         char *datum, *name;
79         size_t datum_len, name_len;
80         int num;
81 } rsync_xa;
82
83 struct _rsync_xa_list;
84
85 typedef struct _rsync_xa_list_ref {
86         struct _rsync_xa_list_ref *next;
87         int ndx;
88 } rsync_xa_list_ref;
89
90 typedef struct _rsync_xa_list {
91         int ndx;
92         int64 key;
93         item_list xa_items;
94 } rsync_xa_list;
95
96 static size_t namebuf_len = 0;
97 static char *namebuf = NULL;
98
99 static const rsync_xa_list empty_xa_list = {
100         .xa_items = EMPTY_ITEM_LIST,
101 };
102 static const item_list empty_xattr = EMPTY_ITEM_LIST;
103 static item_list rsync_xal_l = EMPTY_ITEM_LIST;
104 static struct hashtable *rsync_xal_h = NULL;
105
106 static size_t prior_xattr_count = (size_t)-1;
107
108 /* ------------------------------------------------------------------------- */
109
110 static void rsync_xal_free(item_list *xalp)
111 {
112         size_t i;
113         rsync_xa *rxas = xalp->items;
114
115         if (!xalp->malloced)
116                 return;
117
118         for (i = 0; i < xalp->count; i++) {
119                 free(rxas[i].datum);
120                 /*free(rxas[i].name);*/
121         }
122         free(xalp->items);
123 }
124
125 void free_xattr(stat_x *sxp)
126 {
127         if (!sxp->xattr)
128                 return;
129         rsync_xal_free(sxp->xattr);
130         free(sxp->xattr);
131         sxp->xattr = NULL;
132 }
133
134 static int rsync_xal_compare_names(const void *x1, const void *x2)
135 {
136         const rsync_xa *xa1 = x1;
137         const rsync_xa *xa2 = x2;
138         return strcmp(xa1->name, xa2->name);
139 }
140
141 static ssize_t get_xattr_names(const char *fname)
142 {
143         ssize_t list_len;
144         int64 arg;
145
146         if (!namebuf) {
147                 namebuf_len = 1024;
148                 namebuf = new_array(char, namebuf_len);
149                 if (!namebuf)
150                         out_of_memory("get_xattr_names");
151         }
152
153         while (1) {
154                 /* The length returned includes all the '\0' terminators. */
155                 list_len = sys_llistxattr(fname, namebuf, namebuf_len);
156                 if (list_len >= 0) {
157                         if ((size_t)list_len <= namebuf_len)
158                                 break;
159                 } else if (errno == ENOTSUP)
160                         return 0;
161                 else if (errno != ERANGE) {
162                         arg = namebuf_len;
163                   got_error:
164                         rsyserr(FERROR_XFER, errno,
165                                 "get_xattr_names: llistxattr(%s,%s) failed",
166                                 full_fname(fname), big_num(arg));
167                         return -1;
168                 }
169                 list_len = sys_llistxattr(fname, NULL, 0);
170                 if (list_len < 0) {
171                         arg = 0;
172                         goto got_error;
173                 }
174                 if (namebuf_len)
175                         free(namebuf);
176                 namebuf_len = list_len + 1024;
177                 namebuf = new_array(char, namebuf_len);
178                 if (!namebuf)
179                         out_of_memory("get_xattr_names");
180         }
181
182         return list_len;
183 }
184
185 /* On entry, the *len_ptr parameter contains the size of the extra space we
186  * should allocate when we create a buffer for the data.  On exit, it contains
187  * the length of the datum. */
188 static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr,
189                             int no_missing_error)
190 {
191         size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
192         size_t extra_len = *len_ptr;
193         char *ptr;
194
195         *len_ptr = datum_len;
196
197         if (datum_len == (size_t)-1) {
198                 if (errno == ENOTSUP || no_missing_error)
199                         return NULL;
200                 rsyserr(FERROR_XFER, errno,
201                         "get_xattr_data: lgetxattr(%s,\"%s\",0) failed",
202                         full_fname(fname), name);
203                 return NULL;
204         }
205
206         if (!datum_len && !extra_len)
207                 extra_len = 1; /* request non-zero amount of memory */
208         if (datum_len + extra_len < datum_len)
209                 overflow_exit("get_xattr_data");
210         if (!(ptr = new_array(char, datum_len + extra_len)))
211                 out_of_memory("get_xattr_data");
212
213         if (datum_len) {
214                 size_t len = sys_lgetxattr(fname, name, ptr, datum_len);
215                 if (len != datum_len) {
216                         if (len == (size_t)-1) {
217                                 rsyserr(FERROR_XFER, errno,
218                                     "get_xattr_data: lgetxattr(%s,\"%s\",%ld) failed",
219                                     full_fname(fname), name, (long)datum_len);
220                         } else {
221                                 rprintf(FERROR_XFER,
222                                     "get_xattr_data: lgetxattr(%s,\"%s\",%ld) returned %ld\n",
223                                     full_fname(fname), name,
224                                     (long)datum_len, (long)len);
225                         }
226                         free(ptr);
227                         return NULL;
228                 }
229         }
230
231         return ptr;
232 }
233
234 static int rsync_xal_get(const char *fname, item_list *xalp)
235 {
236         ssize_t list_len, name_len;
237         size_t datum_len, name_offset;
238         char *name, *ptr;
239 #ifdef HAVE_LINUX_XATTRS
240         int user_only = am_sender ? 0 : !am_root;
241 #endif
242         rsync_xa *rxa;
243         int count;
244
245         /* This puts the name list into the "namebuf" buffer. */
246         if ((list_len = get_xattr_names(fname)) < 0)
247                 return -1;
248
249         for (name = namebuf; list_len > 0; name += name_len) {
250                 name_len = strlen(name) + 1;
251                 list_len -= name_len;
252
253                 if (saw_xattr_filter) {
254                         if (name_is_excluded(name, NAME_IS_XATTR, ALL_FILTERS))
255                                 continue;
256                 }
257 #ifdef HAVE_LINUX_XATTRS
258                 /* Choose between ignoring the system namespace or (non-root) ignoring any non-user namespace. */
259                 else if (user_only ? !HAS_PREFIX(name, USER_PREFIX) : HAS_PREFIX(name, SYSTEM_PREFIX))
260                         continue;
261 #endif
262
263                 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
264                 if (name_len > RPRE_LEN && name[RPRE_LEN] == '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
265                         if ((am_sender && preserve_xattrs < 2)
266                          || (am_root < 0
267                           && (strcmp(name+RPRE_LEN+1, XSTAT_SUFFIX) == 0
268                            || strcmp(name+RPRE_LEN+1, XACC_ACL_SUFFIX) == 0
269                            || strcmp(name+RPRE_LEN+1, XDEF_ACL_SUFFIX) == 0)))
270                                 continue;
271                 }
272
273                 datum_len = name_len; /* Pass extra size to get_xattr_data() */
274                 if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
275                         return -1;
276
277                 if (datum_len > MAX_FULL_DATUM) {
278                         /* For large datums, we store a flag and a checksum. */
279                         name_offset = 1 + MAX_DIGEST_LEN;
280                         sum_init(-1, checksum_seed);
281                         sum_update(ptr, datum_len);
282                         free(ptr);
283
284                         if (!(ptr = new_array(char, name_offset + name_len)))
285                                 out_of_memory("rsync_xal_get");
286                         *ptr = XSTATE_ABBREV;
287                         sum_end(ptr + 1);
288                 } else
289                         name_offset = datum_len;
290
291                 rxa = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
292                 rxa->name = ptr + name_offset;
293                 memcpy(rxa->name, name, name_len);
294                 rxa->datum = ptr;
295                 rxa->name_len = name_len;
296                 rxa->datum_len = datum_len;
297         }
298         count = xalp->count;
299         rxa = xalp->items;
300         if (count > 1)
301                 qsort(rxa, count, sizeof (rsync_xa), rsync_xal_compare_names);
302         for (rxa += count-1; count; count--, rxa--)
303                 rxa->num = count;
304         return 0;
305 }
306
307 /* Read the xattr(s) for this filename. */
308 int get_xattr(const char *fname, stat_x *sxp)
309 {
310         sxp->xattr = new(item_list);
311         *sxp->xattr = empty_xattr;
312
313         if (S_ISREG(sxp->st.st_mode) || S_ISDIR(sxp->st.st_mode)) {
314                 /* Everyone supports this. */
315         } else if (S_ISLNK(sxp->st.st_mode)) {
316 #ifndef NO_SYMLINK_XATTRS
317                 if (!preserve_links)
318 #endif
319                         return 0;
320         } else if (IS_SPECIAL(sxp->st.st_mode)) {
321 #ifndef NO_SPECIAL_XATTRS
322                 if (!preserve_specials)
323 #endif
324                         return 0;
325         } else if (IS_DEVICE(sxp->st.st_mode)) {
326 #ifndef NO_DEVICE_XATTRS
327                 if (!preserve_devices)
328 #endif
329                         return 0;
330         } else if (IS_MISSING_FILE(sxp->st))
331                 return 0;
332
333         if (rsync_xal_get(fname, sxp->xattr) < 0) {
334                 free_xattr(sxp);
335                 return -1;
336         }
337         return 0;
338 }
339
340 int copy_xattrs(const char *source, const char *dest)
341 {
342         ssize_t list_len, name_len;
343         size_t datum_len;
344         char *name, *ptr;
345 #ifdef HAVE_LINUX_XATTRS
346         int user_only = am_sender ? 0 : am_root <= 0;
347 #endif
348
349         /* This puts the name list into the "namebuf" buffer. */
350         if ((list_len = get_xattr_names(source)) < 0)
351                 return -1;
352
353         for (name = namebuf; list_len > 0; name += name_len) {
354                 name_len = strlen(name) + 1;
355                 list_len -= name_len;
356
357                 if (saw_xattr_filter) {
358                         if (name_is_excluded(name, NAME_IS_XATTR, ALL_FILTERS))
359                                 continue;
360                 }
361 #ifdef HAVE_LINUX_XATTRS
362                 /* Choose between ignoring the system namespace or (non-root) ignoring any non-user namespace. */
363                 else if (user_only ? !HAS_PREFIX(name, USER_PREFIX) : HAS_PREFIX(name, SYSTEM_PREFIX))
364                         continue;
365 #endif
366
367                 datum_len = 0;
368                 if (!(ptr = get_xattr_data(source, name, &datum_len, 0)))
369                         return -1;
370                 if (sys_lsetxattr(dest, name, ptr, datum_len) < 0) {
371                         int save_errno = errno ? errno : EINVAL;
372                         rsyserr(FERROR_XFER, errno,
373                                 "copy_xattrs: lsetxattr(%s,\"%s\") failed",
374                                 full_fname(dest), name);
375                         errno = save_errno;
376                         return -1;
377                 }
378                 free(ptr);
379         }
380
381         return 0;
382 }
383
384 static int64 xattr_lookup_hash(const item_list *xalp)
385 {
386         const rsync_xa *rxas = xalp->items;
387         size_t i;
388         int64 key = hashlittle(&xalp->count, sizeof xalp->count);
389
390         for (i = 0; i < xalp->count; i++) {
391                 key += hashlittle(rxas[i].name, rxas[i].name_len);
392                 if (rxas[i].datum_len > MAX_FULL_DATUM)
393                         key += hashlittle(rxas[i].datum, MAX_DIGEST_LEN);
394                 else
395                         key += hashlittle(rxas[i].datum, rxas[i].datum_len);
396         }
397
398         if (key == 0) {
399                 /* This is very unlikely, but we should never
400                  * return 0 as hashtable_find() doesn't like it. */
401                 return 1;
402         }
403
404         return key;
405 }
406
407 static int find_matching_xattr(const item_list *xalp)
408 {
409         const struct ht_int64_node *node;
410         const rsync_xa_list_ref *ref;
411         int64 key;
412
413         if (rsync_xal_h == NULL)
414                 return -1;
415
416         key = xattr_lookup_hash(xalp);
417
418         node = hashtable_find(rsync_xal_h, key, 0);
419         if (node == NULL)
420                 return -1;
421
422         if (node->data == NULL)
423                 return -1;
424
425         for (ref = node->data; ref != NULL; ref = ref->next) {
426                 const rsync_xa_list *ptr = rsync_xal_l.items;
427                 const rsync_xa *rxas1;
428                 const rsync_xa *rxas2 = xalp->items;
429                 size_t j;
430
431                 ptr += ref->ndx;
432                 rxas1 = ptr->xa_items.items;
433
434                 /* Wrong number of elements? */
435                 if (ptr->xa_items.count != xalp->count)
436                         continue;
437                 /* any elements different? */
438                 for (j = 0; j < xalp->count; j++) {
439                         if (rxas1[j].name_len != rxas2[j].name_len
440                          || rxas1[j].datum_len != rxas2[j].datum_len
441                          || strcmp(rxas1[j].name, rxas2[j].name))
442                                 break;
443                         if (rxas1[j].datum_len > MAX_FULL_DATUM) {
444                                 if (memcmp(rxas1[j].datum + 1,
445                                            rxas2[j].datum + 1,
446                                            MAX_DIGEST_LEN) != 0)
447                                         break;
448                         } else {
449                                 if (memcmp(rxas1[j].datum, rxas2[j].datum,
450                                            rxas2[j].datum_len))
451                                         break;
452                         }
453                 }
454                 /* no differences found.  This is The One! */
455                 if (j == xalp->count)
456                         return ref->ndx;
457         }
458
459         return -1;
460 }
461
462 /* Store *xalp on the end of rsync_xal_l */
463 static int rsync_xal_store(item_list *xalp)
464 {
465         struct ht_int64_node *node;
466         int ndx = rsync_xal_l.count; /* pre-incremented count */
467         rsync_xa_list *new_list = EXPAND_ITEM_LIST(&rsync_xal_l, rsync_xa_list, RSYNC_XAL_LIST_INITIAL);
468         rsync_xa_list_ref *new_ref;
469         /* Since the following call starts a new list, we know it will hold the
470          * entire initial-count, not just enough space for one new item. */
471         *new_list = empty_xa_list;
472         (void)EXPAND_ITEM_LIST(&new_list->xa_items, rsync_xa, xalp->count);
473         memcpy(new_list->xa_items.items, xalp->items, xalp->count * sizeof (rsync_xa));
474         new_list->xa_items.count = xalp->count;
475         xalp->count = 0;
476
477         new_list->ndx = ndx;
478         new_list->key = xattr_lookup_hash(&new_list->xa_items);
479
480         if (rsync_xal_h == NULL)
481                 rsync_xal_h = hashtable_create(512, 1);
482         if (rsync_xal_h == NULL)
483                 out_of_memory("rsync_xal_h hashtable_create()");
484
485         node = hashtable_find(rsync_xal_h, new_list->key, 1);
486         if (node == NULL)
487                 out_of_memory("rsync_xal_h hashtable_find()");
488
489         new_ref = new0(rsync_xa_list_ref);
490         if (new_ref == NULL)
491                 out_of_memory("new0(rsync_xa_list_ref)");
492
493         new_ref->ndx = ndx;
494
495         if (node->data != NULL) {
496                 rsync_xa_list_ref *ref = node->data;
497
498                 while (ref != NULL) {
499                         if (ref->next != NULL) {
500                                 ref = ref->next;
501                                 continue;
502                         }
503
504                         ref->next = new_ref;
505                         break;
506                 }
507         } else
508                 node->data = new_ref;
509
510         return ndx;
511 }
512
513 /* Send the make_xattr()-generated xattr list for this flist entry. */
514 int send_xattr(int f, stat_x *sxp)
515 {
516         int ndx = find_matching_xattr(sxp->xattr);
517
518         /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
519         write_varint(f, ndx + 1);
520
521         if (ndx < 0) {
522                 rsync_xa *rxa;
523                 int count = sxp->xattr->count;
524                 write_varint(f, count);
525                 for (rxa = sxp->xattr->items; count--; rxa++) {
526                         size_t name_len = rxa->name_len;
527                         const char *name = rxa->name;
528                         /* Strip the rsync prefix from disguised namespaces. */
529                         if (name_len > RPRE_LEN
530 #ifdef HAVE_LINUX_XATTRS
531                          && am_root < 0
532 #endif
533                          && name[RPRE_LEN] != '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
534                                 name += RPRE_LEN;
535                                 name_len -= RPRE_LEN;
536                         }
537 #ifndef HAVE_LINUX_XATTRS
538                         else {
539                                 /* Put everything else in the user namespace. */
540                                 name_len += UPRE_LEN;
541                         }
542 #endif
543                         write_varint(f, name_len);
544                         write_varint(f, rxa->datum_len);
545 #ifndef HAVE_LINUX_XATTRS
546                         if (name_len > rxa->name_len) {
547                                 write_buf(f, USER_PREFIX, UPRE_LEN);
548                                 name_len -= UPRE_LEN;
549                         }
550 #endif
551                         write_buf(f, name, name_len);
552                         if (rxa->datum_len > MAX_FULL_DATUM)
553                                 write_buf(f, rxa->datum + 1, MAX_DIGEST_LEN);
554                         else
555                                 write_bigbuf(f, rxa->datum, rxa->datum_len);
556                 }
557                 ndx = rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
558         }
559
560         return ndx;
561 }
562
563 /* Return a flag indicating if we need to change a file's xattrs.  If
564  * "find_all" is specified, also mark any abbreviated xattrs that we
565  * need so that send_xattr_request() can tell the sender about them. */
566 int xattr_diff(struct file_struct *file, stat_x *sxp, int find_all)
567 {
568         const rsync_xa_list *glst = rsync_xal_l.items;
569         const item_list *lst;
570         rsync_xa *snd_rxa, *rec_rxa;
571         int snd_cnt, rec_cnt;
572         int cmp, same, xattrs_equal = 1;
573
574         if (sxp && XATTR_READY(*sxp)) {
575                 rec_rxa = sxp->xattr->items;
576                 rec_cnt = sxp->xattr->count;
577         } else {
578                 rec_rxa = NULL;
579                 rec_cnt = 0;
580         }
581
582         if (F_XATTR(file) >= 0) {
583                 glst += F_XATTR(file);
584                 lst = &glst->xa_items;
585         } else
586                 lst = &empty_xattr;
587
588         snd_rxa = lst->items;
589         snd_cnt = lst->count;
590
591         /* If the count of the sender's xattrs is different from our
592          * (receiver's) xattrs, the lists are not the same. */
593         if (snd_cnt != rec_cnt) {
594                 if (!find_all)
595                         return 1;
596                 xattrs_equal = 0;
597         }
598
599         while (snd_cnt) {
600                 cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1;
601                 if (cmp > 0)
602                         same = 0;
603                 else if (snd_rxa->datum_len > MAX_FULL_DATUM) {
604                         same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
605                             && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1,
606                                       MAX_DIGEST_LEN) == 0;
607                         /* Flag unrequested items that we need. */
608                         if (!same && find_all && snd_rxa->datum[0] == XSTATE_ABBREV)
609                                 snd_rxa->datum[0] = XSTATE_TODO;
610                 } else {
611                         same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
612                             && memcmp(snd_rxa->datum, rec_rxa->datum,
613                                       snd_rxa->datum_len) == 0;
614                 }
615                 if (!same) {
616                         if (!find_all)
617                                 return 1;
618                         xattrs_equal = 0;
619                 }
620
621                 if (cmp <= 0) {
622                         snd_rxa++;
623                         snd_cnt--;
624                 }
625                 if (cmp >= 0) {
626                         rec_rxa++;
627                         rec_cnt--;
628                 }
629         }
630
631         if (rec_cnt)
632                 xattrs_equal = 0;
633
634         return !xattrs_equal;
635 }
636
637 /* When called by the generator (with a NULL fname), this tells the sender
638  * all the abbreviated xattr values we need.  When called by the sender
639  * (with a non-NULL fname), we send all the extra xattr data it needs.
640  * The generator may also call with f_out < 0 to just change all the
641  * XSTATE_ABBREV states into XSTATE_DONE. */
642 void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
643 {
644         const rsync_xa_list *glst = rsync_xal_l.items;
645         const item_list *lst;
646         int cnt, prior_req = 0;
647         rsync_xa *rxa;
648
649         glst += F_XATTR(file);
650         lst = &glst->xa_items;
651
652         for (rxa = lst->items, cnt = lst->count; cnt--; rxa++) {
653                 if (rxa->datum_len <= MAX_FULL_DATUM)
654                         continue;
655                 switch (rxa->datum[0]) {
656                 case XSTATE_ABBREV:
657                         /* Items left abbreviated matched the sender's checksum, so
658                          * the receiver will cache the local data for future use. */
659                         if (am_generator)
660                                 rxa->datum[0] = XSTATE_DONE;
661                         continue;
662                 case XSTATE_TODO:
663                         assert(f_out >= 0);
664                         break;
665                 default:
666                         continue;
667                 }
668
669                 /* Flag that we handled this abbreviated item. */
670                 rxa->datum[0] = XSTATE_DONE;
671
672                 write_varint(f_out, rxa->num - prior_req);
673                 prior_req = rxa->num;
674
675                 if (fname) {
676                         size_t len = 0;
677                         char *ptr;
678
679                         /* Re-read the long datum. */
680                         if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0))) {
681                                 rprintf(FERROR_XFER, "failed to re-read xattr %s for %s\n", rxa->name, fname);
682                                 write_varint(f_out, 0);
683                                 continue;
684                         }
685
686                         write_varint(f_out, len); /* length might have changed! */
687                         write_bigbuf(f_out, ptr, len);
688                         free(ptr);
689                 }
690         }
691
692         if (f_out >= 0)
693                 write_byte(f_out, 0); /* end the list */
694 }
695
696 /* When called by the sender, read the request from the generator and mark
697  * any needed xattrs with a flag that lets us know they need to be sent to
698  * the receiver.  When called by the receiver, reads the sent data and
699  * stores it in place of its checksum. */
700 int recv_xattr_request(struct file_struct *file, int f_in)
701 {
702         const rsync_xa_list *glst = rsync_xal_l.items;
703         const item_list *lst;
704         char *old_datum, *name;
705         rsync_xa *rxa;
706         int rel_pos, cnt, num, got_xattr_data = 0;
707
708         if (F_XATTR(file) < 0) {
709                 rprintf(FERROR, "recv_xattr_request: internal data error!\n");
710                 exit_cleanup(RERR_PROTOCOL);
711         }
712         glst += F_XATTR(file);
713         lst = &glst->xa_items;
714
715         cnt = lst->count;
716         rxa = lst->items;
717         num = 0;
718         while ((rel_pos = read_varint(f_in)) != 0) {
719                 num += rel_pos;
720                 if (am_sender) {
721                         /* The sender-related num values are only in order on the sender.
722                          * We use that order here to scan foward or backward as needed. */
723                         if (rel_pos < 0) {
724                                 while (cnt < (int)lst->count && rxa->num > num) {
725                                         rxa--;
726                                         cnt++;
727                                 }
728                         } else {
729                                 while (cnt > 1 && rxa->num < num) {
730                                         rxa++;
731                                         cnt--;
732                                 }
733                         }
734                 } else {
735                         int j;
736                         /* The receiving side has no known num order, so we just scan
737                          * forward (w/wrap) and hope that the next value is near by. */
738                         for (j = lst->count; j > 1 && rxa->num != num; j--) {
739                                 if (--cnt)
740                                         rxa++;
741                                 else {
742                                         cnt = lst->count;
743                                         rxa = lst->items;
744                                 }
745                         }
746                 }
747                 if (!cnt || rxa->num != num) {
748                         rprintf(FERROR, "[%s] could not find xattr #%d for %s\n",
749                                 who_am_i(), num, f_name(file, NULL));
750                         exit_cleanup(RERR_PROTOCOL);
751                 }
752                 if (!XATTR_ABBREV(*rxa) || rxa->datum[0] != XSTATE_ABBREV) {
753                         rprintf(FERROR, "[%s] internal abbrev error on %s (%s, len=%ld)!\n",
754                                 who_am_i(), f_name(file, NULL), rxa->name, (long)rxa->datum_len);
755                         exit_cleanup(RERR_PROTOCOL);
756                 }
757
758                 if (am_sender) {
759                         rxa->datum[0] = XSTATE_TODO;
760                         continue;
761                 }
762
763                 old_datum = rxa->datum;
764                 rxa->datum_len = read_varint(f_in);
765
766                 if (rxa->name_len + rxa->datum_len < rxa->name_len)
767                         overflow_exit("recv_xattr_request");
768                 rxa->datum = new_array(char, rxa->datum_len + rxa->name_len);
769                 if (!rxa->datum)
770                         out_of_memory("recv_xattr_request");
771                 name = rxa->datum + rxa->datum_len;
772                 memcpy(name, rxa->name, rxa->name_len);
773                 rxa->name = name;
774                 free(old_datum);
775                 read_buf(f_in, rxa->datum, rxa->datum_len);
776                 got_xattr_data = 1;
777         }
778
779         return got_xattr_data;
780 }
781
782 /* ------------------------------------------------------------------------- */
783
784 /* receive and build the rsync_xattr_lists */
785 void receive_xattr(int f, struct file_struct *file)
786 {
787         static item_list temp_xattr = EMPTY_ITEM_LIST;
788         int count, num;
789 #ifdef HAVE_LINUX_XATTRS
790         int need_sort = 0;
791 #else
792         int need_sort = 1;
793 #endif
794         int ndx = read_varint(f);
795
796         if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) {
797                 rprintf(FERROR, "receive_xattr: xa index %d out of"
798                         " range for %s\n", ndx, f_name(file, NULL));
799                 exit_cleanup(RERR_STREAMIO);
800         }
801
802         if (ndx != 0) {
803                 F_XATTR(file) = ndx - 1;
804                 return;
805         }
806
807         if ((count = read_varint(f)) != 0) {
808                 (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
809                 temp_xattr.count = 0;
810         }
811
812         for (num = 1; num <= count; num++) {
813                 char *ptr, *name;
814                 rsync_xa *rxa;
815                 size_t name_len = read_varint(f);
816                 size_t datum_len = read_varint(f);
817                 size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len;
818                 size_t extra_len = MIGHT_NEED_RPRE ? RPRE_LEN : 0;
819                 if ((dget_len + extra_len < dget_len)
820                  || (dget_len + extra_len + name_len < dget_len + extra_len))
821                         overflow_exit("receive_xattr");
822                 ptr = new_array(char, dget_len + extra_len + name_len);
823                 if (!ptr)
824                         out_of_memory("receive_xattr");
825                 name = ptr + dget_len + extra_len;
826                 read_buf(f, name, name_len);
827                 if (name_len < 1 || name[name_len-1] != '\0') {
828                         rprintf(FERROR, "Invalid xattr name received (missing trailing \\0).\n");
829                         exit_cleanup(RERR_FILEIO);
830                 }
831                 if (dget_len == datum_len)
832                         read_buf(f, ptr, dget_len);
833                 else {
834                         *ptr = XSTATE_ABBREV;
835                         read_buf(f, ptr + 1, MAX_DIGEST_LEN);
836                 }
837
838                 if (saw_xattr_filter) {
839                         if (name_is_excluded(name, NAME_IS_XATTR, ALL_FILTERS)) {
840                                 free(ptr);
841                                 continue;
842                         }
843                 }
844 #ifdef HAVE_LINUX_XATTRS
845                 /* Non-root can only save the user namespace. */
846                 if (am_root <= 0 && !HAS_PREFIX(name, USER_PREFIX)) {
847                         if (!am_root && !saw_xattr_filter) {
848                                 free(ptr);
849                                 continue;
850                         }
851                         name -= RPRE_LEN;
852                         name_len += RPRE_LEN;
853                         memcpy(name, RSYNC_PREFIX, RPRE_LEN);
854                         need_sort = 1;
855                 }
856 #else
857                 /* This OS only has a user namespace, so we either
858                  * strip the user prefix, or we put a non-user
859                  * namespace inside our rsync hierarchy. */
860                 if (HAS_PREFIX(name, USER_PREFIX)) {
861                         name += UPRE_LEN;
862                         name_len -= UPRE_LEN;
863                 } else if (am_root) {
864                         name -= RPRE_LEN;
865                         name_len += RPRE_LEN;
866                         memcpy(name, RSYNC_PREFIX, RPRE_LEN);
867                 } else {
868                         free(ptr);
869                         continue;
870                 }
871 #endif
872                 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
873                 if (preserve_xattrs < 2 && name_len > RPRE_LEN
874                  && name[RPRE_LEN] == '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
875                         free(ptr);
876                         continue;
877                 }
878
879                 rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
880                 rxa->name = name;
881                 rxa->datum = ptr;
882                 rxa->name_len = name_len;
883                 rxa->datum_len = datum_len;
884                 rxa->num = num;
885         }
886
887         if (need_sort && count > 1)
888                 qsort(temp_xattr.items, count, sizeof (rsync_xa), rsync_xal_compare_names);
889
890         ndx = rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
891
892         F_XATTR(file) = ndx;
893 }
894
895 /* Turn the xattr data in stat_x into cached xattr data, setting the index
896  * values in the file struct. */
897 void cache_tmp_xattr(struct file_struct *file, stat_x *sxp)
898 {
899         int ndx;
900
901         if (!sxp->xattr)
902                 return;
903
904         if (prior_xattr_count == (size_t)-1)
905                 prior_xattr_count = rsync_xal_l.count;
906         ndx = find_matching_xattr(sxp->xattr);
907         if (ndx < 0)
908                 rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
909
910         F_XATTR(file) = ndx;
911 }
912
913 void uncache_tmp_xattrs(void)
914 {
915         if (prior_xattr_count != (size_t)-1) {
916                 rsync_xa_list *xa_list_item = rsync_xal_l.items;
917                 rsync_xa_list *xa_list_start = xa_list_item + prior_xattr_count;
918                 xa_list_item += rsync_xal_l.count;
919                 rsync_xal_l.count = prior_xattr_count;
920                 while (xa_list_item-- > xa_list_start) {
921                         struct ht_int64_node *node;
922                         rsync_xa_list_ref *ref;
923
924                         rsync_xal_free(&xa_list_item->xa_items);
925
926                         if (rsync_xal_h == NULL)
927                                 continue;
928
929                         node = hashtable_find(rsync_xal_h, xa_list_item->key, 0);
930                         if (node == NULL)
931                                 continue;
932
933                         if (node->data == NULL)
934                                 continue;
935
936                         ref = node->data;
937                         if (xa_list_item->ndx == ref->ndx) {
938                                 /* xa_list_item is the first in the list. */
939                                 node->data = ref->next;
940                                 free(ref);
941                                 continue;
942                         }
943
944                         while (ref != NULL) {
945                                 if (ref->next == NULL) {
946                                         ref = NULL;
947                                         break;
948                                 }
949                                 if (xa_list_item->ndx == ref->next->ndx) {
950                                         ref->next = ref->next->next;
951                                         free(ref);
952                                         break;
953                                 }
954                                 ref = ref->next;
955                         }
956                 }
957                 prior_xattr_count = (size_t)-1;
958         }
959 }
960
961 static int rsync_xal_set(const char *fname, item_list *xalp,
962                          const char *fnamecmp, stat_x *sxp)
963 {
964         rsync_xa *rxas = xalp->items;
965         ssize_t list_len;
966         size_t i, len;
967         char *name, *ptr, sum[MAX_DIGEST_LEN];
968 #ifdef HAVE_LINUX_XATTRS
969         int user_only = am_root <= 0;
970 #endif
971         size_t name_len;
972         int ret = 0;
973
974         /* This puts the current name list into the "namebuf" buffer. */
975         if ((list_len = get_xattr_names(fname)) < 0)
976                 return -1;
977
978         for (i = 0; i < xalp->count; i++) {
979                 name = rxas[i].name;
980
981                 if (XATTR_ABBREV(rxas[i])) {
982                         int sum_len;
983                         /* See if the fnamecmp version is identical. */
984                         len = name_len = rxas[i].name_len;
985                         if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
986                           still_abbrev:
987                                 if (am_generator)
988                                         continue;
989                                 rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n",
990                                         rxas[i].name, full_fname(fname));
991                                 ret = -1;
992                                 continue;
993                         }
994                         if (len != rxas[i].datum_len) {
995                                 free(ptr);
996                                 goto still_abbrev;
997                         }
998
999                         sum_init(-1, checksum_seed);
1000                         sum_update(ptr, len);
1001                         sum_len = sum_end(sum);
1002                         if (memcmp(sum, rxas[i].datum + 1, sum_len) != 0) {
1003                                 free(ptr);
1004                                 goto still_abbrev;
1005                         }
1006
1007                         if (fname == fnamecmp)
1008                                 ; /* Value is already set when identical */
1009                         else if (sys_lsetxattr(fname, name, ptr, len) < 0) {
1010                                 rsyserr(FERROR_XFER, errno,
1011                                         "rsync_xal_set: lsetxattr(%s,\"%s\") failed",
1012                                         full_fname(fname), name);
1013                                 ret = -1;
1014                         } else /* make sure caller sets mtime */
1015                                 sxp->st.st_mtime = (time_t)-1;
1016
1017                         if (am_generator) { /* generator items stay abbreviated */
1018                                 free(ptr);
1019                                 continue;
1020                         }
1021
1022                         memcpy(ptr + len, name, name_len);
1023                         free(rxas[i].datum);
1024
1025                         rxas[i].name = name = ptr + len;
1026                         rxas[i].datum = ptr;
1027                         continue;
1028                 }
1029
1030                 if (sys_lsetxattr(fname, name, rxas[i].datum, rxas[i].datum_len) < 0) {
1031                         rsyserr(FERROR_XFER, errno,
1032                                 "rsync_xal_set: lsetxattr(%s,\"%s\") failed",
1033                                 full_fname(fname), name);
1034                         ret = -1;
1035                 } else /* make sure caller sets mtime */
1036                         sxp->st.st_mtime = (time_t)-1;
1037         }
1038
1039         /* Remove any extraneous names. */
1040         for (name = namebuf; list_len > 0; name += name_len) {
1041                 name_len = strlen(name) + 1;
1042                 list_len -= name_len;
1043
1044                 if (saw_xattr_filter) {
1045                         if (name_is_excluded(name, NAME_IS_XATTR, ALL_FILTERS))
1046                                 continue;
1047                 }
1048 #ifdef HAVE_LINUX_XATTRS
1049                 /* Choose between ignoring the system namespace or (non-root) ignoring any non-user namespace. */
1050                 else if (user_only ? !HAS_PREFIX(name, USER_PREFIX) : HAS_PREFIX(name, SYSTEM_PREFIX))
1051                         continue;
1052 #endif
1053                 if (am_root < 0 && name_len > RPRE_LEN && name[RPRE_LEN] == '%' && strcmp(name, XSTAT_ATTR) == 0)
1054                         continue;
1055
1056                 for (i = 0; i < xalp->count; i++) {
1057                         if (strcmp(name, rxas[i].name) == 0)
1058                                 break;
1059                 }
1060                 if (i == xalp->count) {
1061                         if (sys_lremovexattr(fname, name) < 0) {
1062                                 rsyserr(FERROR_XFER, errno,
1063                                         "rsync_xal_set: lremovexattr(%s,\"%s\") failed",
1064                                         full_fname(fname), name);
1065                                 ret = -1;
1066                         } else /* make sure caller sets mtime */
1067                                 sxp->st.st_mtime = (time_t)-1;
1068                 }
1069         }
1070
1071         return ret;
1072 }
1073
1074 /* Set extended attributes on indicated filename. */
1075 int set_xattr(const char *fname, const struct file_struct *file,
1076               const char *fnamecmp, stat_x *sxp)
1077 {
1078         rsync_xa_list *glst = rsync_xal_l.items;
1079         item_list *lst;
1080         int ndx;
1081
1082         if (dry_run)
1083                 return 1; /* FIXME: --dry-run needs to compute this value */
1084
1085         if (read_only || list_only) {
1086                 errno = EROFS;
1087                 return -1;
1088         }
1089
1090 #ifdef NO_SPECIAL_XATTRS
1091         if (IS_SPECIAL(sxp->st.st_mode)) {
1092                 errno = ENOTSUP;
1093                 return -1;
1094         }
1095 #endif
1096 #ifdef NO_DEVICE_XATTRS
1097         if (IS_DEVICE(sxp->st.st_mode)) {
1098                 errno = ENOTSUP;
1099                 return -1;
1100         }
1101 #endif
1102 #ifdef NO_SYMLINK_XATTRS
1103         if (S_ISLNK(sxp->st.st_mode)) {
1104                 errno = ENOTSUP;
1105                 return -1;
1106         }
1107 #endif
1108
1109         ndx = F_XATTR(file);
1110         glst += ndx;
1111         lst = &glst->xa_items;
1112         return rsync_xal_set(fname, lst, fnamecmp, sxp);
1113 }
1114
1115 #ifdef SUPPORT_ACLS
1116 char *get_xattr_acl(const char *fname, int is_access_acl, size_t *len_p)
1117 {
1118         const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
1119         *len_p = 0; /* no extra data alloc needed from get_xattr_data() */
1120         return get_xattr_data(fname, name, len_p, 1);
1121 }
1122
1123 int set_xattr_acl(const char *fname, int is_access_acl, const char *buf, size_t buf_len)
1124 {
1125         const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
1126         if (sys_lsetxattr(fname, name, buf, buf_len) < 0) {
1127                 rsyserr(FERROR_XFER, errno,
1128                         "set_xattr_acl: lsetxattr(%s,\"%s\") failed",
1129                         full_fname(fname), name);
1130                 return -1;
1131         }
1132         return 0;
1133 }
1134
1135 int del_def_xattr_acl(const char *fname)
1136 {
1137         return sys_lremovexattr(fname, XDEF_ACL_ATTR);
1138 }
1139 #endif
1140
1141 int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
1142 {
1143         int mode, rdev_major, rdev_minor, uid, gid, len;
1144         char buf[256];
1145
1146         if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode))
1147                 return -1;
1148
1149         if (xst)
1150                 *xst = *fst;
1151         else
1152                 xst = fst;
1153         if (fname) {
1154                 fd = -1;
1155                 len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1);
1156         } else {
1157                 fname = "fd";
1158                 len = sys_fgetxattr(fd, XSTAT_ATTR, buf, sizeof buf - 1);
1159         }
1160         if (len >= (int)sizeof buf) {
1161                 len = -1;
1162                 errno = ERANGE;
1163         }
1164         if (len < 0) {
1165                 if (errno == ENOTSUP || errno == ENOATTR)
1166                         return -1;
1167                 if (errno == EPERM && S_ISLNK(fst->st_mode)) {
1168                         xst->st_uid = 0;
1169                         xst->st_gid = 0;
1170                         return 0;
1171                 }
1172                 rsyserr(FERROR_XFER, errno, "failed to read xattr %s for %s",
1173                         XSTAT_ATTR, full_fname(fname));
1174                 return -1;
1175         }
1176         buf[len] = '\0';
1177
1178         if (sscanf(buf, "%o %d,%d %d:%d",
1179                    &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
1180                 rprintf(FERROR, "Corrupt %s xattr attached to %s: \"%s\"\n",
1181                         XSTAT_ATTR, full_fname(fname), buf);
1182                 exit_cleanup(RERR_FILEIO);
1183         }
1184
1185         xst->st_mode = from_wire_mode(mode);
1186         xst->st_rdev = MAKEDEV(rdev_major, rdev_minor);
1187         xst->st_uid = uid;
1188         xst->st_gid = gid;
1189
1190         return 0;
1191 }
1192
1193 int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode)
1194 {
1195         STRUCT_STAT fst, xst;
1196         dev_t rdev;
1197         mode_t mode, fmode;
1198
1199         if (dry_run)
1200                 return 0;
1201
1202         if (read_only || list_only) {
1203                 rsyserr(FERROR_XFER, EROFS, "failed to write xattr %s for %s",
1204                         XSTAT_ATTR, full_fname(fname));
1205                 return -1;
1206         }
1207
1208         if (x_lstat(fname, &fst, &xst) < 0) {
1209                 rsyserr(FERROR_XFER, errno, "failed to re-stat %s",
1210                         full_fname(fname));
1211                 return -1;
1212         }
1213
1214         fst.st_mode &= (_S_IFMT | CHMOD_BITS);
1215         fmode = new_mode & (_S_IFMT | CHMOD_BITS);
1216
1217         if (IS_DEVICE(fmode)) {
1218                 uint32 *devp = F_RDEV_P(file);
1219                 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1220         } else
1221                 rdev = 0;
1222
1223         /* Dump the special permissions and enable full owner access. */
1224         mode = (fst.st_mode & _S_IFMT) | (fmode & ACCESSPERMS)
1225              | (S_ISDIR(fst.st_mode) ? 0700 : 0600);
1226         if (fst.st_mode != mode)
1227                 do_chmod(fname, mode);
1228         if (!IS_DEVICE(fst.st_mode))
1229                 fst.st_rdev = 0; /* just in case */
1230
1231         if (mode == fmode && fst.st_rdev == rdev
1232          && fst.st_uid == F_OWNER(file) && fst.st_gid == F_GROUP(file)) {
1233                 /* xst.st_mode will be 0 if there's no current stat xattr */
1234                 if (xst.st_mode && sys_lremovexattr(fname, XSTAT_ATTR) < 0) {
1235                         rsyserr(FERROR_XFER, errno,
1236                                 "delete of stat xattr failed for %s",
1237                                 full_fname(fname));
1238                         return -1;
1239                 }
1240                 return 0;
1241         }
1242
1243         if (xst.st_mode != fmode || xst.st_rdev != rdev
1244          || xst.st_uid != F_OWNER(file) || xst.st_gid != F_GROUP(file)) {
1245                 char buf[256];
1246                 int len = snprintf(buf, sizeof buf, "%o %u,%u %u:%u",
1247                         to_wire_mode(fmode),
1248                         (int)major(rdev), (int)minor(rdev),
1249                         F_OWNER(file), F_GROUP(file));
1250                 if (sys_lsetxattr(fname, XSTAT_ATTR, buf, len) < 0) {
1251                         if (errno == EPERM && S_ISLNK(fst.st_mode))
1252                                 return 0;
1253                         rsyserr(FERROR_XFER, errno,
1254                                 "failed to write xattr %s for %s",
1255                                 XSTAT_ATTR, full_fname(fname));
1256                         return -1;
1257                 }
1258         }
1259
1260         return 0;
1261 }
1262
1263 int x_stat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
1264 {
1265         int ret = do_stat(fname, fst);
1266         if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
1267                 xst->st_mode = 0;
1268         return ret;
1269 }
1270
1271 int x_lstat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
1272 {
1273         int ret = do_lstat(fname, fst);
1274         if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
1275                 xst->st_mode = 0;
1276         return ret;
1277 }
1278
1279 int x_fstat(int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
1280 {
1281         int ret = do_fstat(fd, fst);
1282         if ((ret < 0 || get_stat_xattr(NULL, fd, fst, xst) < 0) && xst)
1283                 xst->st_mode = 0;
1284         return ret;
1285 }
1286
1287 #endif /* SUPPORT_XATTRS */