Add a way to specify xattr name filtering.
[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-2015 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))
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 (dget_len == datum_len)
828                         read_buf(f, ptr, dget_len);
829                 else {
830                         *ptr = XSTATE_ABBREV;
831                         read_buf(f, ptr + 1, MAX_DIGEST_LEN);
832                 }
833
834                 if (saw_xattr_filter) {
835                         if (name_is_excluded(name, NAME_IS_XATTR, ALL_FILTERS)) {
836                                 free(ptr);
837                                 continue;
838                         }
839                 }
840 #ifdef HAVE_LINUX_XATTRS
841                 /* Non-root can only save the user namespace. */
842                 if (am_root <= 0 && !HAS_PREFIX(name, USER_PREFIX)) {
843                         if (!am_root && !saw_xattr_filter) {
844                                 free(ptr);
845                                 continue;
846                         }
847                         name -= RPRE_LEN;
848                         name_len += RPRE_LEN;
849                         memcpy(name, RSYNC_PREFIX, RPRE_LEN);
850                         need_sort = 1;
851                 }
852 #else
853                 /* This OS only has a user namespace, so we either
854                  * strip the user prefix, or we put a non-user
855                  * namespace inside our rsync hierarchy. */
856                 if (HAS_PREFIX(name, USER_PREFIX)) {
857                         name += UPRE_LEN;
858                         name_len -= UPRE_LEN;
859                 } else if (am_root) {
860                         name -= RPRE_LEN;
861                         name_len += RPRE_LEN;
862                         memcpy(name, RSYNC_PREFIX, RPRE_LEN);
863                 } else {
864                         free(ptr);
865                         continue;
866                 }
867 #endif
868                 /* No rsync.%FOO attributes are copied w/o 2 -X options. */
869                 if (preserve_xattrs < 2 && name_len > RPRE_LEN
870                  && name[RPRE_LEN] == '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
871                         free(ptr);
872                         continue;
873                 }
874
875                 rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
876                 rxa->name = name;
877                 rxa->datum = ptr;
878                 rxa->name_len = name_len;
879                 rxa->datum_len = datum_len;
880                 rxa->num = num;
881         }
882
883         if (need_sort && count > 1)
884                 qsort(temp_xattr.items, count, sizeof (rsync_xa), rsync_xal_compare_names);
885
886         ndx = rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
887
888         F_XATTR(file) = ndx;
889 }
890
891 /* Turn the xattr data in stat_x into cached xattr data, setting the index
892  * values in the file struct. */
893 void cache_tmp_xattr(struct file_struct *file, stat_x *sxp)
894 {
895         int ndx;
896
897         if (!sxp->xattr)
898                 return;
899
900         if (prior_xattr_count == (size_t)-1)
901                 prior_xattr_count = rsync_xal_l.count;
902         ndx = find_matching_xattr(sxp->xattr);
903         if (ndx < 0)
904                 rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
905
906         F_XATTR(file) = ndx;
907 }
908
909 void uncache_tmp_xattrs(void)
910 {
911         if (prior_xattr_count != (size_t)-1) {
912                 rsync_xa_list *xa_list_item = rsync_xal_l.items;
913                 rsync_xa_list *xa_list_start = xa_list_item + prior_xattr_count;
914                 xa_list_item += rsync_xal_l.count;
915                 rsync_xal_l.count = prior_xattr_count;
916                 while (xa_list_item-- > xa_list_start) {
917                         struct ht_int64_node *node;
918                         rsync_xa_list_ref *ref;
919
920                         rsync_xal_free(&xa_list_item->xa_items);
921
922                         if (rsync_xal_h == NULL)
923                                 continue;
924
925                         node = hashtable_find(rsync_xal_h, xa_list_item->key, 0);
926                         if (node == NULL)
927                                 continue;
928
929                         if (node->data == NULL)
930                                 continue;
931
932                         ref = node->data;
933                         if (xa_list_item->ndx == ref->ndx) {
934                                 /* xa_list_item is the first in the list. */
935                                 node->data = ref->next;
936                                 free(ref);
937                                 continue;
938                         }
939
940                         while (ref != NULL) {
941                                 if (ref->next == NULL) {
942                                         ref = NULL;
943                                         break;
944                                 }
945                                 if (xa_list_item->ndx == ref->next->ndx) {
946                                         ref->next = ref->next->next;
947                                         free(ref);
948                                         break;
949                                 }
950                                 ref = ref->next;
951                         }
952                 }
953                 prior_xattr_count = (size_t)-1;
954         }
955 }
956
957 static int rsync_xal_set(const char *fname, item_list *xalp,
958                          const char *fnamecmp, stat_x *sxp)
959 {
960         rsync_xa *rxas = xalp->items;
961         ssize_t list_len;
962         size_t i, len;
963         char *name, *ptr, sum[MAX_DIGEST_LEN];
964 #ifdef HAVE_LINUX_XATTRS
965         int user_only = am_root <= 0;
966 #endif
967         size_t name_len;
968         int ret = 0;
969
970         /* This puts the current name list into the "namebuf" buffer. */
971         if ((list_len = get_xattr_names(fname)) < 0)
972                 return -1;
973
974         for (i = 0; i < xalp->count; i++) {
975                 name = rxas[i].name;
976
977                 if (XATTR_ABBREV(rxas[i])) {
978                         /* See if the fnamecmp version is identical. */
979                         len = name_len = rxas[i].name_len;
980                         if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
981                           still_abbrev:
982                                 if (am_generator)
983                                         continue;
984                                 rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n",
985                                         rxas[i].name, full_fname(fname));
986                                 ret = -1;
987                                 continue;
988                         }
989                         if (len != rxas[i].datum_len) {
990                                 free(ptr);
991                                 goto still_abbrev;
992                         }
993
994                         sum_init(-1, checksum_seed);
995                         sum_update(ptr, len);
996                         sum_end(sum);
997                         if (memcmp(sum, rxas[i].datum + 1, MAX_DIGEST_LEN) != 0) {
998                                 free(ptr);
999                                 goto still_abbrev;
1000                         }
1001
1002                         if (fname == fnamecmp)
1003                                 ; /* Value is already set when identical */
1004                         else if (sys_lsetxattr(fname, name, ptr, len) < 0) {
1005                                 rsyserr(FERROR_XFER, errno,
1006                                         "rsync_xal_set: lsetxattr(%s,\"%s\") failed",
1007                                         full_fname(fname), name);
1008                                 ret = -1;
1009                         } else /* make sure caller sets mtime */
1010                                 sxp->st.st_mtime = (time_t)-1;
1011
1012                         if (am_generator) { /* generator items stay abbreviated */
1013                                 free(ptr);
1014                                 continue;
1015                         }
1016
1017                         memcpy(ptr + len, name, name_len);
1018                         free(rxas[i].datum);
1019
1020                         rxas[i].name = name = ptr + len;
1021                         rxas[i].datum = ptr;
1022                         continue;
1023                 }
1024
1025                 if (sys_lsetxattr(fname, name, rxas[i].datum, rxas[i].datum_len) < 0) {
1026                         rsyserr(FERROR_XFER, errno,
1027                                 "rsync_xal_set: lsetxattr(%s,\"%s\") failed",
1028                                 full_fname(fname), name);
1029                         ret = -1;
1030                 } else /* make sure caller sets mtime */
1031                         sxp->st.st_mtime = (time_t)-1;
1032         }
1033
1034         /* Remove any extraneous names. */
1035         for (name = namebuf; list_len > 0; name += name_len) {
1036                 name_len = strlen(name) + 1;
1037                 list_len -= name_len;
1038
1039                 if (saw_xattr_filter) {
1040                         if (name_is_excluded(name, NAME_IS_XATTR, ALL_FILTERS))
1041                                 continue;
1042                 }
1043 #ifdef HAVE_LINUX_XATTRS
1044                 /* Choose between ignoring the system namespace or (non-root) ignoring any non-user namespace. */
1045                 else if (user_only ? !HAS_PREFIX(name, USER_PREFIX) : HAS_PREFIX(name, SYSTEM_PREFIX))
1046                         continue;
1047 #endif
1048                 if (am_root < 0 && name_len > RPRE_LEN && name[RPRE_LEN] == '%' && strcmp(name, XSTAT_ATTR) == 0)
1049                         continue;
1050
1051                 for (i = 0; i < xalp->count; i++) {
1052                         if (strcmp(name, rxas[i].name) == 0)
1053                                 break;
1054                 }
1055                 if (i == xalp->count) {
1056                         if (sys_lremovexattr(fname, name) < 0) {
1057                                 rsyserr(FERROR_XFER, errno,
1058                                         "rsync_xal_set: lremovexattr(%s,\"%s\") failed",
1059                                         full_fname(fname), name);
1060                                 ret = -1;
1061                         } else /* make sure caller sets mtime */
1062                                 sxp->st.st_mtime = (time_t)-1;
1063                 }
1064         }
1065
1066         return ret;
1067 }
1068
1069 /* Set extended attributes on indicated filename. */
1070 int set_xattr(const char *fname, const struct file_struct *file,
1071               const char *fnamecmp, stat_x *sxp)
1072 {
1073         rsync_xa_list *glst = rsync_xal_l.items;
1074         item_list *lst;
1075         int ndx;
1076
1077         if (dry_run)
1078                 return 1; /* FIXME: --dry-run needs to compute this value */
1079
1080         if (read_only || list_only) {
1081                 errno = EROFS;
1082                 return -1;
1083         }
1084
1085 #ifdef NO_SPECIAL_XATTRS
1086         if (IS_SPECIAL(sxp->st.st_mode)) {
1087                 errno = ENOTSUP;
1088                 return -1;
1089         }
1090 #endif
1091 #ifdef NO_DEVICE_XATTRS
1092         if (IS_DEVICE(sxp->st.st_mode)) {
1093                 errno = ENOTSUP;
1094                 return -1;
1095         }
1096 #endif
1097 #ifdef NO_SYMLINK_XATTRS
1098         if (S_ISLNK(sxp->st.st_mode)) {
1099                 errno = ENOTSUP;
1100                 return -1;
1101         }
1102 #endif
1103
1104         ndx = F_XATTR(file);
1105         glst += ndx;
1106         lst = &glst->xa_items;
1107         return rsync_xal_set(fname, lst, fnamecmp, sxp);
1108 }
1109
1110 #ifdef SUPPORT_ACLS
1111 char *get_xattr_acl(const char *fname, int is_access_acl, size_t *len_p)
1112 {
1113         const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
1114         *len_p = 0; /* no extra data alloc needed from get_xattr_data() */
1115         return get_xattr_data(fname, name, len_p, 1);
1116 }
1117
1118 int set_xattr_acl(const char *fname, int is_access_acl, const char *buf, size_t buf_len)
1119 {
1120         const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
1121         if (sys_lsetxattr(fname, name, buf, buf_len) < 0) {
1122                 rsyserr(FERROR_XFER, errno,
1123                         "set_xattr_acl: lsetxattr(%s,\"%s\") failed",
1124                         full_fname(fname), name);
1125                 return -1;
1126         }
1127         return 0;
1128 }
1129
1130 int del_def_xattr_acl(const char *fname)
1131 {
1132         return sys_lremovexattr(fname, XDEF_ACL_ATTR);
1133 }
1134 #endif
1135
1136 int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
1137 {
1138         int mode, rdev_major, rdev_minor, uid, gid, len;
1139         char buf[256];
1140
1141         if (am_root >= 0 || IS_DEVICE(fst->st_mode) || IS_SPECIAL(fst->st_mode))
1142                 return -1;
1143
1144         if (xst)
1145                 *xst = *fst;
1146         else
1147                 xst = fst;
1148         if (fname) {
1149                 fd = -1;
1150                 len = sys_lgetxattr(fname, XSTAT_ATTR, buf, sizeof buf - 1);
1151         } else {
1152                 fname = "fd";
1153                 len = sys_fgetxattr(fd, XSTAT_ATTR, buf, sizeof buf - 1);
1154         }
1155         if (len >= (int)sizeof buf) {
1156                 len = -1;
1157                 errno = ERANGE;
1158         }
1159         if (len < 0) {
1160                 if (errno == ENOTSUP || errno == ENOATTR)
1161                         return -1;
1162                 if (errno == EPERM && S_ISLNK(fst->st_mode)) {
1163                         xst->st_uid = 0;
1164                         xst->st_gid = 0;
1165                         return 0;
1166                 }
1167                 rsyserr(FERROR_XFER, errno, "failed to read xattr %s for %s",
1168                         XSTAT_ATTR, full_fname(fname));
1169                 return -1;
1170         }
1171         buf[len] = '\0';
1172
1173         if (sscanf(buf, "%o %d,%d %d:%d",
1174                    &mode, &rdev_major, &rdev_minor, &uid, &gid) != 5) {
1175                 rprintf(FERROR, "Corrupt %s xattr attached to %s: \"%s\"\n",
1176                         XSTAT_ATTR, full_fname(fname), buf);
1177                 exit_cleanup(RERR_FILEIO);
1178         }
1179
1180         xst->st_mode = from_wire_mode(mode);
1181         xst->st_rdev = MAKEDEV(rdev_major, rdev_minor);
1182         xst->st_uid = uid;
1183         xst->st_gid = gid;
1184
1185         return 0;
1186 }
1187
1188 int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode)
1189 {
1190         STRUCT_STAT fst, xst;
1191         dev_t rdev;
1192         mode_t mode, fmode;
1193
1194         if (dry_run)
1195                 return 0;
1196
1197         if (read_only || list_only) {
1198                 rsyserr(FERROR_XFER, EROFS, "failed to write xattr %s for %s",
1199                         XSTAT_ATTR, full_fname(fname));
1200                 return -1;
1201         }
1202
1203         if (x_lstat(fname, &fst, &xst) < 0) {
1204                 rsyserr(FERROR_XFER, errno, "failed to re-stat %s",
1205                         full_fname(fname));
1206                 return -1;
1207         }
1208
1209         fst.st_mode &= (_S_IFMT | CHMOD_BITS);
1210         fmode = new_mode & (_S_IFMT | CHMOD_BITS);
1211
1212         if (IS_DEVICE(fmode)) {
1213                 uint32 *devp = F_RDEV_P(file);
1214                 rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1215         } else
1216                 rdev = 0;
1217
1218         /* Dump the special permissions and enable full owner access. */
1219         mode = (fst.st_mode & _S_IFMT) | (fmode & ACCESSPERMS)
1220              | (S_ISDIR(fst.st_mode) ? 0700 : 0600);
1221         if (fst.st_mode != mode)
1222                 do_chmod(fname, mode);
1223         if (!IS_DEVICE(fst.st_mode))
1224                 fst.st_rdev = 0; /* just in case */
1225
1226         if (mode == fmode && fst.st_rdev == rdev
1227          && fst.st_uid == F_OWNER(file) && fst.st_gid == F_GROUP(file)) {
1228                 /* xst.st_mode will be 0 if there's no current stat xattr */
1229                 if (xst.st_mode && sys_lremovexattr(fname, XSTAT_ATTR) < 0) {
1230                         rsyserr(FERROR_XFER, errno,
1231                                 "delete of stat xattr failed for %s",
1232                                 full_fname(fname));
1233                         return -1;
1234                 }
1235                 return 0;
1236         }
1237
1238         if (xst.st_mode != fmode || xst.st_rdev != rdev
1239          || xst.st_uid != F_OWNER(file) || xst.st_gid != F_GROUP(file)) {
1240                 char buf[256];
1241                 int len = snprintf(buf, sizeof buf, "%o %u,%u %u:%u",
1242                         to_wire_mode(fmode),
1243                         (int)major(rdev), (int)minor(rdev),
1244                         F_OWNER(file), F_GROUP(file));
1245                 if (sys_lsetxattr(fname, XSTAT_ATTR, buf, len) < 0) {
1246                         if (errno == EPERM && S_ISLNK(fst.st_mode))
1247                                 return 0;
1248                         rsyserr(FERROR_XFER, errno,
1249                                 "failed to write xattr %s for %s",
1250                                 XSTAT_ATTR, full_fname(fname));
1251                         return -1;
1252                 }
1253         }
1254
1255         return 0;
1256 }
1257
1258 int x_stat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
1259 {
1260         int ret = do_stat(fname, fst);
1261         if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
1262                 xst->st_mode = 0;
1263         return ret;
1264 }
1265
1266 int x_lstat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
1267 {
1268         int ret = do_lstat(fname, fst);
1269         if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
1270                 xst->st_mode = 0;
1271         return ret;
1272 }
1273
1274 int x_fstat(int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
1275 {
1276         int ret = do_fstat(fd, fst);
1277         if ((ret < 0 || get_stat_xattr(NULL, fd, fst, xst) < 0) && xst)
1278                 xst->st_mode = 0;
1279         return ret;
1280 }
1281
1282 #endif /* SUPPORT_XATTRS */