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