xattrs.c 28 KB

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