xattrs.c 27 KB

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