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