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