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