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