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