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