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