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