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