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 | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License along | |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
20 | */ | |
21 | ||
22 | #include "rsync.h" | |
23 | #include "lib/sysxattrs.h" | |
24 | ||
25 | #ifdef SUPPORT_XATTRS | |
26 | ||
27 | extern int dry_run; | |
28 | extern int am_root; | |
29 | extern int am_sender; | |
30 | extern int am_generator; | |
31 | extern int read_only; | |
32 | extern int list_only; | |
33 | extern int checksum_seed; | |
34 | ||
35 | #define RSYNC_XAL_INITIAL 5 | |
36 | #define RSYNC_XAL_LIST_INITIAL 100 | |
37 | ||
38 | #define MAX_FULL_DATUM 32 | |
39 | ||
40 | #define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \ | |
41 | && strncmp(str, prfx, sizeof (prfx) - 1) == 0) | |
42 | ||
43 | #define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len) | |
44 | ||
45 | #define XSTATE_ABBREV 0 | |
46 | #define XSTATE_DONE 1 | |
47 | #define XSTATE_TODO 2 | |
48 | #define XSTATE_LOCAL 3 | |
49 | ||
50 | #define USER_PREFIX "user." | |
51 | #define UPRE_LEN ((int)sizeof USER_PREFIX - 1) | |
52 | #define SYSTEM_PREFIX "system." | |
53 | #define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1) | |
54 | ||
55 | #ifdef HAVE_LINUX_XATTRS | |
56 | #define RPRE_LEN 0 | |
57 | #else | |
58 | #define RSYNC_PREFIX "rsync." | |
59 | #define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1) | |
60 | #endif | |
61 | ||
62 | typedef struct { | |
63 | char *datum, *name; | |
64 | size_t datum_len, name_len; | |
65 | } rsync_xa; | |
66 | ||
67 | static size_t namebuf_len = 0; | |
68 | static char *namebuf = NULL; | |
69 | ||
70 | static item_list empty_xattr = EMPTY_ITEM_LIST; | |
71 | static item_list rsync_xal_l = EMPTY_ITEM_LIST; | |
72 | ||
73 | /* ------------------------------------------------------------------------- */ | |
74 | ||
75 | static void rsync_xal_free(item_list *xalp) | |
76 | { | |
77 | size_t i; | |
78 | rsync_xa *rxas = xalp->items; | |
79 | ||
80 | for (i = 0; i < xalp->count; i++) { | |
81 | free(rxas[i].datum); | |
82 | /*free(rxas[i].name);*/ | |
83 | } | |
84 | xalp->count = 0; | |
85 | } | |
86 | ||
87 | void free_xattr(statx *sxp) | |
88 | { | |
89 | if (!sxp->xattr) | |
90 | return; | |
91 | rsync_xal_free(sxp->xattr); | |
92 | free(sxp->xattr); | |
93 | sxp->xattr = NULL; | |
94 | } | |
95 | ||
96 | static int rsync_xal_compare_names(const void *x1, const void *x2) | |
97 | { | |
98 | const rsync_xa *xa1 = x1; | |
99 | const rsync_xa *xa2 = x2; | |
100 | return strcmp(xa1->name, xa2->name); | |
101 | } | |
102 | ||
103 | static ssize_t get_xattr_names(const char *fname) | |
104 | { | |
105 | ssize_t list_len; | |
106 | ||
107 | if (!namebuf) { | |
108 | namebuf_len = 1024; | |
109 | namebuf = new_array(char, namebuf_len); | |
110 | if (!namebuf) | |
111 | out_of_memory("get_xattr_names"); | |
112 | } | |
113 | ||
114 | /* The length returned includes all the '\0' terminators. */ | |
115 | list_len = sys_llistxattr(fname, namebuf, namebuf_len); | |
116 | if (list_len > (ssize_t)namebuf_len) { | |
117 | list_len = -1; | |
118 | errno = ERANGE; | |
119 | } | |
120 | if (list_len >= 0) | |
121 | return list_len; | |
122 | if (errno == ENOTSUP) | |
123 | return 0; | |
124 | if (errno == ERANGE) { | |
125 | list_len = sys_llistxattr(fname, NULL, 0); | |
126 | if (list_len < 0) { | |
127 | rsyserr(FERROR, errno, | |
128 | "get_xattr_names: llistxattr(\"%s\",0) failed", | |
129 | fname); | |
130 | return -1; | |
131 | } | |
132 | if (namebuf_len) | |
133 | free(namebuf); | |
134 | namebuf_len = list_len + 1024; | |
135 | namebuf = new_array(char, namebuf_len); | |
136 | if (!namebuf) | |
137 | out_of_memory("get_xattr_names"); | |
138 | list_len = sys_llistxattr(fname, namebuf, namebuf_len); | |
139 | if (list_len >= 0) | |
140 | return list_len; | |
141 | } | |
142 | ||
143 | rsyserr(FERROR, errno, | |
144 | "get_xattr_names: llistxattr(\"%s\",%ld) failed", | |
145 | fname, (long)namebuf_len); | |
146 | return -1; | |
147 | } | |
148 | ||
149 | /* On entry, the *len_ptr parameter contains the size of the extra space we | |
150 | * should allocate when we create a buffer for the data. On exit, it contains | |
151 | * the length of the datum. */ | |
152 | static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr, | |
153 | int no_missing_error) | |
154 | { | |
155 | size_t datum_len = sys_lgetxattr(fname, name, NULL, 0); | |
156 | char *ptr; | |
157 | ||
158 | if (datum_len == (size_t)-1) { | |
159 | if (errno == ENOTSUP || no_missing_error) | |
160 | return NULL; | |
161 | rsyserr(FERROR, errno, | |
162 | "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed", | |
163 | fname, name); | |
164 | return NULL; | |
165 | } | |
166 | ||
167 | if (datum_len + *len_ptr < datum_len /* checks for overflow */ | |
168 | || !(ptr = new_array(char, datum_len + *len_ptr))) | |
169 | out_of_memory("get_xattr_data"); | |
170 | ||
171 | *len_ptr = datum_len; | |
172 | ||
173 | if (datum_len) { | |
174 | size_t len = sys_lgetxattr(fname, name, ptr, datum_len); | |
175 | if (len != datum_len) { | |
176 | if (len == (size_t)-1) { | |
177 | rsyserr(FERROR, errno, | |
178 | "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)" | |
179 | " failed", fname, name, (long)datum_len); | |
180 | } else { | |
181 | rprintf(FERROR, | |
182 | "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)" | |
183 | " returned %ld\n", fname, name, | |
184 | (long)datum_len, (long)len); | |
185 | } | |
186 | free(ptr); | |
187 | return NULL; | |
188 | } | |
189 | } | |
190 | ||
191 | return ptr; | |
192 | } | |
193 | ||
194 | static int rsync_xal_get(const char *fname, item_list *xalp) | |
195 | { | |
196 | ssize_t list_len, name_len; | |
197 | size_t datum_len, name_offset; | |
198 | char *name, *ptr; | |
199 | #ifdef HAVE_LINUX_XATTRS | |
200 | int user_only = am_sender ? 0 : !am_root; | |
201 | #endif | |
202 | ||
203 | /* This puts the name list into the "namebuf" buffer. */ | |
204 | if ((list_len = get_xattr_names(fname)) < 0) | |
205 | return -1; | |
206 | ||
207 | for (name = namebuf; list_len > 0; name += name_len) { | |
208 | rsync_xa *rxas; | |
209 | ||
210 | name_len = strlen(name) + 1; | |
211 | list_len -= name_len; | |
212 | ||
213 | #ifdef HAVE_LINUX_XATTRS | |
214 | /* We always ignore the system namespace, and non-root | |
215 | * ignores everything but the user namespace. */ | |
216 | if (user_only ? !HAS_PREFIX(name, USER_PREFIX) | |
217 | : HAS_PREFIX(name, SYSTEM_PREFIX)) | |
218 | continue; | |
219 | #endif | |
220 | ||
221 | datum_len = name_len; /* Pass extra size to get_xattr_data() */ | |
222 | if (!(ptr = get_xattr_data(fname, name, &datum_len, 0))) | |
223 | return -1; | |
224 | ||
225 | if (datum_len > MAX_FULL_DATUM) { | |
226 | /* For large datums, we store a flag and a checksum. */ | |
227 | name_offset = 1 + MAX_DIGEST_LEN; | |
228 | sum_init(checksum_seed); | |
229 | sum_update(ptr, datum_len); | |
230 | free(ptr); | |
231 | ||
232 | if (!(ptr = new_array(char, name_offset + name_len))) | |
233 | out_of_memory("rsync_xal_get"); | |
234 | *ptr = XSTATE_ABBREV; | |
235 | sum_end(ptr + 1); | |
236 | } else | |
237 | name_offset = datum_len; | |
238 | ||
239 | rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL); | |
240 | rxas->name = ptr + name_offset; | |
241 | memcpy(rxas->name, name, name_len); | |
242 | rxas->datum = ptr; | |
243 | rxas->name_len = name_len; | |
244 | rxas->datum_len = datum_len; | |
245 | } | |
246 | if (xalp->count > 1) | |
247 | qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names); | |
248 | return 0; | |
249 | } | |
250 | ||
251 | /* Read the xattr(s) for this filename. */ | |
252 | int get_xattr(const char *fname, statx *sxp) | |
253 | { | |
254 | sxp->xattr = new(item_list); | |
255 | *sxp->xattr = empty_xattr; | |
256 | if (rsync_xal_get(fname, sxp->xattr) < 0) { | |
257 | free_xattr(sxp); | |
258 | return -1; | |
259 | } | |
260 | return 0; | |
261 | } | |
262 | ||
263 | static int find_matching_xattr(item_list *xalp) | |
264 | { | |
265 | size_t i, j; | |
266 | item_list *lst = rsync_xal_l.items; | |
267 | ||
268 | for (i = 0; i < rsync_xal_l.count; i++) { | |
269 | rsync_xa *rxas1 = lst[i].items; | |
270 | rsync_xa *rxas2 = xalp->items; | |
271 | ||
272 | /* Wrong number of elements? */ | |
273 | if (lst[i].count != xalp->count) | |
274 | continue; | |
275 | /* any elements different? */ | |
276 | for (j = 0; j < xalp->count; j++) { | |
277 | if (rxas1[j].name_len != rxas2[j].name_len | |
278 | || rxas1[j].datum_len != rxas2[j].datum_len | |
279 | || strcmp(rxas1[j].name, rxas2[j].name)) | |
280 | break; | |
281 | if (rxas1[j].datum_len > MAX_FULL_DATUM) { | |
282 | if (memcmp(rxas1[j].datum + 1, | |
283 | rxas2[j].datum + 1, | |
284 | MAX_DIGEST_LEN) != 0) | |
285 | break; | |
286 | } else { | |
287 | if (memcmp(rxas1[j].datum, rxas2[j].datum, | |
288 | rxas2[j].datum_len)) | |
289 | break; | |
290 | } | |
291 | } | |
292 | /* no differences found. This is The One! */ | |
293 | if (j == xalp->count) | |
294 | return i; | |
295 | } | |
296 | ||
297 | return -1; | |
298 | } | |
299 | ||
300 | /* Store *xalp on the end of rsync_xal_l */ | |
301 | static void rsync_xal_store(item_list *xalp) | |
302 | { | |
303 | item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL); | |
304 | /* Since the following call starts a new list, we know it will hold the | |
305 | * entire initial-count, not just enough space for one new item. */ | |
306 | *new_lst = empty_xattr; | |
307 | (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count); | |
308 | memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa)); | |
309 | new_lst->count = xalp->count; | |
310 | xalp->count = 0; | |
311 | } | |
312 | ||
313 | /* Send the make_xattr()-generated xattr list for this flist entry. */ | |
314 | int send_xattr(statx *sxp, int f) | |
315 | { | |
316 | int ndx = find_matching_xattr(sxp->xattr); | |
317 | ||
318 | /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */ | |
f31514ad | 319 | write_varint(f, ndx + 1); |
16edf865 WD |
320 | |
321 | if (ndx < 0) { | |
322 | rsync_xa *rxa; | |
323 | int count = sxp->xattr->count; | |
f31514ad | 324 | write_varint(f, count); |
16edf865 WD |
325 | for (rxa = sxp->xattr->items; count--; rxa++) { |
326 | #ifdef HAVE_LINUX_XATTRS | |
f31514ad WD |
327 | write_varint(f, rxa->name_len); |
328 | write_varint(f, rxa->datum_len); | |
16edf865 WD |
329 | write_buf(f, rxa->name, rxa->name_len); |
330 | #else | |
331 | /* We strip the rsync prefix from disguised namespaces | |
332 | * and put everything else in the user namespace. */ | |
333 | if (HAS_PREFIX(rxa->name, RSYNC_PREFIX) | |
334 | && rxa->name[RPRE_LEN] != '%') { | |
f31514ad WD |
335 | write_varint(f, rxa->name_len - RPRE_LEN); |
336 | write_varint(f, rxa->datum_len); | |
16edf865 WD |
337 | write_buf(f, rxa->name + RPRE_LEN, rxa->name_len - RPRE_LEN); |
338 | } else { | |
f31514ad WD |
339 | write_varint(f, rxa->name_len + UPRE_LEN); |
340 | write_varint(f, rxa->datum_len); | |
16edf865 WD |
341 | write_buf(f, USER_PREFIX, UPRE_LEN); |
342 | write_buf(f, rxa->name, rxa->name_len); | |
343 | } | |
344 | #endif | |
345 | if (rxa->datum_len > MAX_FULL_DATUM) | |
346 | write_buf(f, rxa->datum + 1, MAX_DIGEST_LEN); | |
347 | else | |
348 | write_buf(f, rxa->datum, rxa->datum_len); | |
349 | } | |
350 | ndx = rsync_xal_l.count; /* pre-incremented count */ | |
351 | rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */ | |
352 | } | |
353 | ||
354 | return ndx; | |
355 | } | |
356 | ||
357 | /* Return a flag indicating if we need to change a file's xattrs. If | |
358 | * "find_all" is specified, also mark any abbreviated xattrs that we | |
359 | * need so that send_xattr_request() can tell the sender about them. */ | |
360 | int xattr_diff(struct file_struct *file, statx *sxp, int find_all) | |
361 | { | |
362 | item_list *lst = rsync_xal_l.items; | |
363 | rsync_xa *snd_rxa, *rec_rxa; | |
364 | int snd_cnt, rec_cnt; | |
365 | int cmp, same, xattrs_equal = 1; | |
366 | ||
367 | if (sxp && XATTR_READY(*sxp)) { | |
368 | rec_rxa = sxp->xattr->items; | |
369 | rec_cnt = sxp->xattr->count; | |
370 | } else { | |
371 | rec_rxa = NULL; | |
372 | rec_cnt = 0; | |
373 | } | |
374 | ||
375 | if (F_XATTR(file) >= 0) | |
376 | lst += F_XATTR(file); | |
377 | else | |
378 | lst = &empty_xattr; | |
379 | ||
380 | snd_rxa = lst->items; | |
381 | snd_cnt = lst->count; | |
382 | ||
383 | /* If the count of the sender's xattrs is different from our | |
384 | * (receiver's) xattrs, the lists are not the same. */ | |
385 | if (snd_cnt != rec_cnt) { | |
386 | if (!find_all) | |
387 | return 1; | |
388 | xattrs_equal = 0; | |
389 | } | |
390 | ||
391 | while (snd_cnt) { | |
392 | cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1; | |
393 | if (cmp > 0) | |
394 | same = 0; | |
395 | else if (snd_rxa->datum_len > MAX_FULL_DATUM) { | |
396 | same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len | |
397 | && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1, | |
398 | MAX_DIGEST_LEN) == 0; | |
399 | /* Flag unrequested items that we need. */ | |
400 | if (!same && find_all && snd_rxa->datum[0] == XSTATE_ABBREV) | |
401 | snd_rxa->datum[0] = XSTATE_TODO; | |
402 | } else { | |
403 | same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len | |
404 | && memcmp(snd_rxa->datum, rec_rxa->datum, | |
405 | snd_rxa->datum_len) == 0; | |
406 | } | |
407 | if (!same) { | |
408 | if (!find_all) | |
409 | return 1; | |
410 | xattrs_equal = 0; | |
411 | } | |
412 | ||
413 | if (cmp <= 0) { | |
414 | snd_rxa++; | |
415 | snd_cnt--; | |
416 | } | |
417 | if (cmp >= 0) { | |
418 | rec_rxa++; | |
419 | rec_cnt--; | |
420 | } | |
421 | } | |
422 | ||
423 | if (rec_cnt) | |
424 | xattrs_equal = 0; | |
425 | ||
426 | return !xattrs_equal; | |
427 | } | |
428 | ||
429 | /* When called by the generator with a NULL fname, this tells the sender | |
430 | * which abbreviated xattr values we need. When called by the sender | |
431 | * (with a non-NULL fname), we send all the extra xattr data it needs. */ | |
432 | void send_xattr_request(const char *fname, struct file_struct *file, int f_out) | |
433 | { | |
434 | item_list *lst = rsync_xal_l.items; | |
435 | int j, cnt, prior_req = -1; | |
436 | rsync_xa *rxa; | |
437 | ||
438 | lst += F_XATTR(file); | |
439 | cnt = lst->count; | |
440 | for (rxa = lst->items, j = 0; j < cnt; rxa++, j++) { | |
441 | if (rxa->datum_len <= MAX_FULL_DATUM) | |
442 | continue; | |
443 | switch (rxa->datum[0]) { | |
444 | case XSTATE_LOCAL: | |
445 | /* Items set locally will get cached by receiver. */ | |
446 | rxa->datum[0] = XSTATE_DONE; | |
447 | continue; | |
448 | case XSTATE_TODO: | |
449 | break; | |
450 | default: | |
451 | continue; | |
452 | } | |
453 | ||
454 | /* Flag that we handled this abbreviated item. */ | |
455 | rxa->datum[0] = XSTATE_DONE; | |
456 | ||
f31514ad | 457 | write_varint(f_out, j - prior_req); |
16edf865 WD |
458 | prior_req = j; |
459 | ||
460 | if (fname) { | |
461 | size_t len = 0; | |
462 | char *ptr; | |
463 | ||
464 | /* Re-read the long datum. */ | |
465 | if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0))) | |
466 | continue; | |
467 | ||
f31514ad | 468 | write_varint(f_out, len); /* length might have changed! */ |
16edf865 WD |
469 | write_buf(f_out, ptr, len); |
470 | free(ptr); | |
471 | } | |
472 | } | |
473 | ||
474 | write_byte(f_out, 0); /* end the list */ | |
475 | } | |
476 | ||
477 | /* Any items set locally by the generator that the receiver doesn't | |
478 | * get told about get changed back to XSTATE_ABBREV. */ | |
479 | void xattr_clear_locals(struct file_struct *file) | |
480 | { | |
481 | item_list *lst = rsync_xal_l.items; | |
482 | rsync_xa *rxa; | |
483 | int cnt; | |
484 | ||
485 | if (F_XATTR(file) < 0) | |
486 | return; | |
487 | ||
488 | lst += F_XATTR(file); | |
489 | cnt = lst->count; | |
490 | for (rxa = lst->items; cnt--; rxa++) { | |
491 | if (rxa->datum_len <= MAX_FULL_DATUM) | |
492 | continue; | |
493 | if (rxa->datum[0] == XSTATE_LOCAL) | |
494 | rxa->datum[0] = XSTATE_ABBREV; | |
495 | } | |
496 | } | |
497 | ||
498 | /* When called by the sender, read the request from the generator and mark | |
499 | * any needed xattrs with a flag that lets us know they need to be sent to | |
500 | * the receiver. When called by the receiver, reads the sent data and | |
501 | * stores it in place of its checksum. */ | |
502 | void recv_xattr_request(struct file_struct *file, int f_in) | |
503 | { | |
504 | item_list *lst = rsync_xal_l.items; | |
505 | char *old_datum, *name; | |
506 | rsync_xa *rxa; | |
507 | int rel_pos, cnt; | |
508 | ||
509 | if (F_XATTR(file) < 0) { | |
510 | rprintf(FERROR, "recv_xattr_request: internal data error!\n"); | |
511 | exit_cleanup(RERR_STREAMIO); | |
512 | } | |
513 | lst += F_XATTR(file); | |
514 | ||
515 | cnt = lst->count; | |
516 | rxa = lst->items; | |
517 | rxa -= 1; | |
f31514ad | 518 | while ((rel_pos = read_varint(f_in)) != 0) { |
16edf865 WD |
519 | rxa += rel_pos; |
520 | cnt -= rel_pos; | |
521 | if (cnt < 0 || rxa->datum_len <= MAX_FULL_DATUM | |
522 | || rxa->datum[0] != XSTATE_ABBREV) { | |
523 | rprintf(FERROR, "recv_xattr_request: internal abbrev error!\n"); | |
524 | exit_cleanup(RERR_STREAMIO); | |
525 | } | |
526 | ||
527 | if (am_sender) { | |
528 | rxa->datum[0] = XSTATE_TODO; | |
529 | continue; | |
530 | } | |
531 | ||
532 | old_datum = rxa->datum; | |
f31514ad | 533 | rxa->datum_len = read_varint(f_in); |
16edf865 WD |
534 | |
535 | if (rxa->name_len + rxa->datum_len < rxa->name_len) | |
536 | out_of_memory("recv_xattr_request"); /* overflow */ | |
537 | rxa->datum = new_array(char, rxa->datum_len + rxa->name_len); | |
538 | if (!rxa->datum) | |
539 | out_of_memory("recv_xattr_request"); | |
540 | name = rxa->datum + rxa->datum_len; | |
541 | memcpy(name, rxa->name, rxa->name_len); | |
542 | rxa->name = name; | |
543 | free(old_datum); | |
544 | read_buf(f_in, rxa->datum, rxa->datum_len); | |
545 | } | |
546 | } | |
547 | ||
548 | /* ------------------------------------------------------------------------- */ | |
549 | ||
550 | /* receive and build the rsync_xattr_lists */ | |
551 | void receive_xattr(struct file_struct *file, int f) | |
552 | { | |
553 | static item_list temp_xattr = EMPTY_ITEM_LIST; | |
554 | int count; | |
f31514ad | 555 | int ndx = read_varint(f); |
16edf865 WD |
556 | |
557 | if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) { | |
558 | rprintf(FERROR, "receive_xattr: xa index %d out of" | |
559 | " range for %s\n", ndx, f_name(file, NULL)); | |
560 | exit_cleanup(RERR_STREAMIO); | |
561 | } | |
562 | ||
563 | if (ndx != 0) { | |
564 | F_XATTR(file) = ndx - 1; | |
565 | return; | |
566 | } | |
567 | ||
f31514ad | 568 | if ((count = read_varint(f)) != 0) { |
16edf865 WD |
569 | (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count); |
570 | temp_xattr.count = 0; | |
571 | } | |
572 | ||
573 | while (count--) { | |
574 | char *ptr, *name; | |
575 | rsync_xa *rxa; | |
f31514ad WD |
576 | size_t name_len = read_varint(f); |
577 | size_t datum_len = read_varint(f); | |
16edf865 WD |
578 | size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len; |
579 | #ifdef HAVE_LINUX_XATTRS | |
580 | size_t extra_len = 0; | |
581 | #else | |
582 | size_t extra_len = am_root ? RPRE_LEN : 0; | |
583 | if (dget_len + extra_len < dget_len) | |
584 | out_of_memory("receive_xattr"); /* overflow */ | |
585 | #endif | |
586 | if (dget_len + extra_len + name_len < dget_len) | |
587 | out_of_memory("receive_xattr"); /* overflow */ | |
588 | ptr = new_array(char, dget_len + extra_len + name_len); | |
589 | if (!ptr) | |
590 | out_of_memory("receive_xattr"); | |
591 | name = ptr + dget_len + extra_len; | |
592 | read_buf(f, name, name_len); | |
593 | if (dget_len == datum_len) | |
594 | read_buf(f, ptr, dget_len); | |
595 | else { | |
596 | *ptr = XSTATE_ABBREV; | |
597 | read_buf(f, ptr + 1, MAX_DIGEST_LEN); | |
598 | } | |
599 | #ifdef HAVE_LINUX_XATTRS | |
600 | /* Non-root can only save the user namespace. */ | |
601 | if (!am_root && !HAS_PREFIX(name, USER_PREFIX)) { | |
602 | free(ptr); | |
603 | continue; | |
604 | } | |
605 | #else | |
606 | /* This OS only has a user namespace, so we either | |
607 | * strip the user prefix, or we put a non-user | |
608 | * namespace inside our rsync hierarchy. */ | |
609 | if (HAS_PREFIX(name, USER_PREFIX)) { | |
610 | name += UPRE_LEN; | |
611 | name_len -= UPRE_LEN; | |
612 | } else if (am_root) { | |
613 | name -= RPRE_LEN; | |
614 | name_len += RPRE_LEN; | |
615 | memcpy(name, RSYNC_PREFIX, RPRE_LEN); | |
616 | } else { | |
617 | free(ptr); | |
618 | continue; | |
619 | } | |
620 | #endif | |
621 | rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1); | |
622 | rxa->name = name; | |
623 | rxa->datum = ptr; | |
624 | rxa->name_len = name_len; | |
625 | rxa->datum_len = datum_len; | |
626 | } | |
627 | ||
628 | ndx = rsync_xal_l.count; /* pre-incremented count */ | |
629 | rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */ | |
630 | ||
631 | F_XATTR(file) = ndx; | |
632 | } | |
633 | ||
634 | /* Turn the xattr data in statx into cached xattr data, setting the index | |
635 | * values in the file struct. */ | |
636 | void cache_xattr(struct file_struct *file, statx *sxp) | |
637 | { | |
638 | int ndx; | |
639 | ||
640 | if (!sxp->xattr) | |
641 | return; | |
642 | ||
643 | ndx = find_matching_xattr(sxp->xattr); | |
644 | if (ndx < 0) | |
645 | rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */ | |
646 | ||
647 | F_XATTR(file) = ndx; | |
648 | } | |
649 | ||
650 | static int rsync_xal_set(const char *fname, item_list *xalp, | |
651 | const char *fnamecmp, statx *sxp) | |
652 | { | |
653 | rsync_xa *rxas = xalp->items; | |
654 | ssize_t list_len; | |
655 | size_t i, len; | |
656 | char *name, *ptr, sum[MAX_DIGEST_LEN]; | |
657 | int name_len, ret = 0; | |
658 | ||
659 | /* This puts the current name list into the "namebuf" buffer. */ | |
660 | if ((list_len = get_xattr_names(fname)) < 0) | |
661 | return -1; | |
662 | ||
663 | for (i = 0; i < xalp->count; i++) { | |
664 | name = rxas[i].name; | |
665 | ||
666 | if (XATTR_ABBREV(rxas[i])) { | |
667 | /* See if the fnamecmp version is identical. */ | |
668 | len = name_len = rxas[i].name_len; | |
669 | if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) { | |
670 | still_abbrev: | |
671 | if (am_generator) | |
672 | continue; | |
673 | rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n", | |
674 | rxas[i].name, full_fname(fname)); | |
675 | ret = -1; | |
676 | continue; | |
677 | } | |
678 | if (len != rxas[i].datum_len) { | |
679 | free(ptr); | |
680 | goto still_abbrev; | |
681 | } | |
682 | ||
683 | sum_init(checksum_seed); | |
684 | sum_update(ptr, len); | |
685 | sum_end(sum); | |
686 | if (memcmp(sum, rxas[i].datum + 1, MAX_DIGEST_LEN) != 0) { | |
687 | free(ptr); | |
688 | goto still_abbrev; | |
689 | } | |
690 | ||
691 | if (fname == fnamecmp) | |
692 | ; /* Value is already set when identical */ | |
693 | else if (sys_lsetxattr(fname, name, ptr, len) < 0) { | |
694 | rsyserr(FERROR, errno, | |
695 | "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed", | |
696 | fname, name); | |
697 | ret = -1; | |
698 | } else /* make sure caller sets mtime */ | |
699 | sxp->st.st_mtime = (time_t)-1; | |
700 | ||
701 | if (am_generator) { /* generator items stay abbreviated */ | |
702 | if (rxas[i].datum[0] == XSTATE_ABBREV) | |
703 | rxas[i].datum[0] = XSTATE_LOCAL; | |
704 | free(ptr); | |
705 | continue; | |
706 | } | |
707 | ||
708 | memcpy(ptr + len, name, name_len); | |
709 | free(rxas[i].datum); | |
710 | ||
711 | rxas[i].name = name = ptr + len; | |
712 | rxas[i].datum = ptr; | |
713 | continue; | |
714 | } | |
715 | ||
716 | if (sys_lsetxattr(fname, name, rxas[i].datum, rxas[i].datum_len) < 0) { | |
717 | rsyserr(FERROR, errno, | |
718 | "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed", | |
719 | fname, name); | |
720 | ret = -1; | |
721 | } else /* make sure caller sets mtime */ | |
722 | sxp->st.st_mtime = (time_t)-1; | |
723 | } | |
724 | ||
725 | /* Remove any extraneous names. */ | |
726 | for (name = namebuf; list_len > 0; name += name_len) { | |
727 | name_len = strlen(name) + 1; | |
728 | list_len -= name_len; | |
729 | ||
730 | #ifdef HAVE_LINUX_XATTRS | |
731 | /* We always ignore the system namespace, and non-root | |
732 | * ignores everything but the user namespace. */ | |
733 | if (am_root ? HAS_PREFIX(name, SYSTEM_PREFIX) | |
734 | : !HAS_PREFIX(name, USER_PREFIX)) | |
735 | continue; | |
736 | #endif | |
737 | ||
738 | for (i = 0; i < xalp->count; i++) { | |
739 | if (strcmp(name, rxas[i].name) == 0) | |
740 | break; | |
741 | } | |
742 | if (i == xalp->count) { | |
743 | if (sys_lremovexattr(fname, name) < 0) { | |
744 | rsyserr(FERROR, errno, | |
745 | "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed", | |
746 | fname, name); | |
747 | ret = -1; | |
748 | } else /* make sure caller sets mtime */ | |
749 | sxp->st.st_mtime = (time_t)-1; | |
750 | } | |
751 | } | |
752 | ||
753 | return ret; | |
754 | } | |
755 | ||
756 | /* Set extended attributes on indicated filename. */ | |
757 | int set_xattr(const char *fname, const struct file_struct *file, | |
758 | const char *fnamecmp, statx *sxp) | |
759 | { | |
760 | int ndx; | |
761 | item_list *lst = rsync_xal_l.items; | |
762 | ||
763 | if (dry_run) | |
764 | return 1; /* FIXME: --dry-run needs to compute this value */ | |
765 | ||
766 | if (read_only || list_only) { | |
767 | errno = EROFS; | |
768 | return -1; | |
769 | } | |
770 | ||
771 | ndx = F_XATTR(file); | |
772 | return rsync_xal_set(fname, lst + ndx, fnamecmp, sxp); | |
773 | } | |
774 | ||
775 | #endif /* SUPPORT_XATTRS */ |