xattrs.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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. if (!xalp->malloced)
  79. return;
  80. for (i = 0; i < xalp->count; i++) {
  81. free(rxas[i].datum);
  82. /*free(rxas[i].name);*/
  83. }
  84. free(xalp->items);
  85. }
  86. void free_xattr(stat_x *sxp)
  87. {
  88. if (!sxp->xattr)
  89. return;
  90. rsync_xal_free(sxp->xattr);
  91. free(sxp->xattr);
  92. sxp->xattr = NULL;
  93. }
  94. static int rsync_xal_compare_names(const void *x1, const void *x2)
  95. {
  96. const rsync_xa *xa1 = x1;
  97. const rsync_xa *xa2 = x2;
  98. return strcmp(xa1->name, xa2->name);
  99. }
  100. static ssize_t get_xattr_names(const char *fname)
  101. {
  102. ssize_t list_len;
  103. double arg;
  104. if (!namebuf) {
  105. namebuf_len = 1024;
  106. namebuf = new_array(char, namebuf_len);
  107. if (!namebuf)
  108. out_of_memory("get_xattr_names");
  109. }
  110. while (1) {
  111. /* The length returned includes all the '\0' terminators. */
  112. list_len = sys_llistxattr(fname, namebuf, namebuf_len);
  113. if (list_len >= 0) {
  114. if ((size_t)list_len <= namebuf_len)
  115. break;
  116. } else if (errno == ENOTSUP)
  117. return 0;
  118. else if (errno != ERANGE) {
  119. arg = (double)namebuf_len;
  120. got_error:
  121. rsyserr(FERROR_XFER, errno,
  122. "get_xattr_names: llistxattr(\"%s\",%.0f) failed",
  123. full_fname(fname), arg);
  124. return -1;
  125. }
  126. list_len = sys_llistxattr(fname, NULL, 0);
  127. if (list_len < 0) {
  128. arg = 0;
  129. goto got_error;
  130. }
  131. if (namebuf_len)
  132. free(namebuf);
  133. namebuf_len = list_len + 1024;
  134. namebuf = new_array(char, namebuf_len);
  135. if (!namebuf)
  136. out_of_memory("get_xattr_names");
  137. }
  138. return list_len;
  139. }
  140. /* On entry, the *len_ptr parameter contains the size of the extra space we
  141. * should allocate when we create a buffer for the data. On exit, it contains
  142. * the length of the datum. */
  143. static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr,
  144. int no_missing_error)
  145. {
  146. size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
  147. size_t extra_len = *len_ptr;
  148. char *ptr;
  149. *len_ptr = datum_len;
  150. if (datum_len == (size_t)-1) {
  151. if (errno == ENOTSUP || no_missing_error)
  152. return NULL;
  153. rsyserr(FERROR_XFER, errno,
  154. "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
  155. full_fname(fname), name);
  156. return NULL;
  157. }
  158. if (!datum_len && !extra_len)
  159. extra_len = 1; /* request non-zero amount of memory */
  160. if (datum_len + extra_len < datum_len)
  161. overflow_exit("get_xattr_data");
  162. if (!(ptr = new_array(char, datum_len + extra_len)))
  163. out_of_memory("get_xattr_data");
  164. if (datum_len) {
  165. size_t len = sys_lgetxattr(fname, name, ptr, datum_len);
  166. if (len != datum_len) {
  167. if (len == (size_t)-1) {
  168. rsyserr(FERROR_XFER, errno,
  169. "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
  170. " failed", full_fname(fname), name, (long)datum_len);
  171. } else {
  172. rprintf(FERROR_XFER,
  173. "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
  174. " returned %ld\n", full_fname(fname), name,
  175. (long)datum_len, (long)len);
  176. }
  177. free(ptr);
  178. return NULL;
  179. }
  180. }
  181. return ptr;
  182. }
  183. static int rsync_xal_get(const char *fname, item_list *xalp)
  184. {
  185. ssize_t list_len, name_len;
  186. size_t datum_len, name_offset;
  187. char *name, *ptr;
  188. #ifdef HAVE_LINUX_XATTRS
  189. int user_only = am_sender ? 0 : am_root <= 0;
  190. #endif
  191. rsync_xa *rxa;
  192. int count;
  193. /* This puts the name list into the "namebuf" buffer. */
  194. if ((list_len = get_xattr_names(fname)) < 0)
  195. return -1;
  196. for (name = namebuf; list_len > 0; name += name_len) {
  197. name_len = strlen(name) + 1;
  198. list_len -= name_len;
  199. #ifdef HAVE_LINUX_XATTRS
  200. /* We always ignore the system namespace, and non-root
  201. * ignores everything but the user namespace. */
  202. if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
  203. : HAS_PREFIX(name, SYSTEM_PREFIX))
  204. continue;
  205. #endif
  206. /* No rsync.%FOO attributes are copied w/o 2 -X options. */
  207. if (name_len > RPRE_LEN && name[RPRE_LEN] == '%'
  208. && HAS_PREFIX(name, RSYNC_PREFIX)) {
  209. if ((am_sender && preserve_xattrs < 2)
  210. || (am_root < 0
  211. && (strcmp(name+RPRE_LEN+1, XSTAT_SUFFIX) == 0
  212. || strcmp(name+RPRE_LEN+1, XACC_ACL_SUFFIX) == 0
  213. || strcmp(name+RPRE_LEN+1, XDEF_ACL_SUFFIX) == 0)))
  214. continue;
  215. }
  216. datum_len = name_len; /* Pass extra size to get_xattr_data() */
  217. if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
  218. return -1;
  219. if (datum_len > MAX_FULL_DATUM) {
  220. /* For large datums, we store a flag and a checksum. */
  221. name_offset = 1 + MAX_DIGEST_LEN;
  222. sum_init(checksum_seed);
  223. sum_update(ptr, datum_len);
  224. free(ptr);
  225. if (!(ptr = new_array(char, name_offset + name_len)))
  226. out_of_memory("rsync_xal_get");
  227. *ptr = XSTATE_ABBREV;
  228. sum_end(ptr + 1);
  229. } else
  230. name_offset = datum_len;
  231. rxa = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
  232. rxa->name = ptr + name_offset;
  233. memcpy(rxa->name, name, name_len);
  234. rxa->datum = ptr;
  235. rxa->name_len = name_len;
  236. rxa->datum_len = datum_len;
  237. }
  238. count = xalp->count;
  239. rxa = xalp->items;
  240. if (count > 1)
  241. qsort(rxa, count, sizeof (rsync_xa), rsync_xal_compare_names);
  242. for (rxa += count-1; count; count--, rxa--)
  243. rxa->num = count;
  244. return 0;
  245. }
  246. /* Read the xattr(s) for this filename. */
  247. int get_xattr(const char *fname, stat_x *sxp)
  248. {
  249. sxp->xattr = new(item_list);
  250. *sxp->xattr = empty_xattr;
  251. if (S_ISREG(sxp->st.st_mode) || S_ISDIR(sxp->st.st_mode)) {
  252. /* Everyone supports this. */
  253. } else if (S_ISLNK(sxp->st.st_mode)) {
  254. #ifndef NO_SYMLINK_XATTRS
  255. if (!preserve_links)
  256. #endif
  257. return 0;
  258. } else if (IS_SPECIAL(sxp->st.st_mode)) {
  259. #ifndef NO_SPECIAL_XATTRS
  260. if (!preserve_specials)
  261. #endif
  262. return 0;
  263. } else if (IS_DEVICE(sxp->st.st_mode)) {
  264. #ifndef NO_DEVICE_XATTRS
  265. if (!preserve_devices)
  266. #endif
  267. return 0;
  268. }
  269. if (rsync_xal_get(fname, sxp->xattr) < 0) {
  270. free_xattr(sxp);
  271. return -1;
  272. }
  273. return 0;
  274. }
  275. int copy_xattrs(const char *source, const char *dest)
  276. {
  277. ssize_t list_len, name_len;
  278. size_t datum_len;
  279. char *name, *ptr;
  280. #ifdef HAVE_LINUX_XATTRS
  281. int user_only = am_root <= 0;
  282. #endif
  283. /* This puts the name list into the "namebuf" buffer. */
  284. if ((list_len = get_xattr_names(source)) < 0)
  285. return -1;
  286. for (name = namebuf; list_len > 0; name += name_len) {
  287. name_len = strlen(name) + 1;
  288. list_len -= name_len;
  289. #ifdef HAVE_LINUX_XATTRS
  290. /* We always ignore the system namespace, and non-root
  291. * ignores everything but the user namespace. */
  292. if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
  293. : HAS_PREFIX(name, SYSTEM_PREFIX))
  294. continue;
  295. #endif
  296. datum_len = 0;
  297. if (!(ptr = get_xattr_data(source, name, &datum_len, 0)))
  298. return -1;
  299. if (sys_lsetxattr(dest, name, ptr, datum_len) < 0) {
  300. int save_errno = errno ? errno : EINVAL;
  301. rsyserr(FERROR_XFER, errno,
  302. "copy_xattrs: lsetxattr(\"%s\",\"%s\") failed",
  303. full_fname(dest), name);
  304. errno = save_errno;
  305. return -1;
  306. }
  307. free(ptr);
  308. }
  309. return 0;
  310. }
  311. static int find_matching_xattr(item_list *xalp)
  312. {
  313. size_t i, j;
  314. item_list *lst = rsync_xal_l.items;
  315. for (i = 0; i < rsync_xal_l.count; i++) {
  316. rsync_xa *rxas1 = lst[i].items;
  317. rsync_xa *rxas2 = xalp->items;
  318. /* Wrong number of elements? */
  319. if (lst[i].count != xalp->count)
  320. continue;
  321. /* any elements different? */
  322. for (j = 0; j < xalp->count; j++) {
  323. if (rxas1[j].name_len != rxas2[j].name_len
  324. || rxas1[j].datum_len != rxas2[j].datum_len
  325. || strcmp(rxas1[j].name, rxas2[j].name))
  326. break;
  327. if (rxas1[j].datum_len > MAX_FULL_DATUM) {
  328. if (memcmp(rxas1[j].datum + 1,
  329. rxas2[j].datum + 1,
  330. MAX_DIGEST_LEN) != 0)
  331. break;
  332. } else {
  333. if (memcmp(rxas1[j].datum, rxas2[j].datum,
  334. rxas2[j].datum_len))
  335. break;
  336. }
  337. }
  338. /* no differences found. This is The One! */
  339. if (j == xalp->count)
  340. return i;
  341. }
  342. return -1;
  343. }
  344. /* Store *xalp on the end of rsync_xal_l */
  345. static void rsync_xal_store(item_list *xalp)
  346. {
  347. item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
  348. /* Since the following call starts a new list, we know it will hold the
  349. * entire initial-count, not just enough space for one new item. */
  350. *new_lst = empty_xattr;
  351. (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
  352. memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
  353. new_lst->count = xalp->count;
  354. xalp->count = 0;
  355. }
  356. /* Send the make_xattr()-generated xattr list for this flist entry. */
  357. int send_xattr(int f, stat_x *sxp)
  358. {
  359. int ndx = find_matching_xattr(sxp->xattr);
  360. /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
  361. write_varint(f, ndx + 1);
  362. if (ndx < 0) {
  363. rsync_xa *rxa;
  364. int count = sxp->xattr->count;
  365. write_varint(f, count);
  366. for (rxa = sxp->xattr->items; count--; rxa++) {
  367. size_t name_len = rxa->name_len;
  368. const char *name = rxa->name;
  369. /* Strip the rsync prefix from disguised namespaces. */
  370. if (name_len > RPRE_LEN
  371. #ifdef HAVE_LINUX_XATTRS
  372. && am_root < 0
  373. #endif
  374. && name[RPRE_LEN] != '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
  375. name += RPRE_LEN;
  376. name_len -= RPRE_LEN;
  377. }
  378. #ifndef HAVE_LINUX_XATTRS
  379. else {
  380. /* Put everything else in the user namespace. */
  381. name_len += UPRE_LEN;
  382. }
  383. #endif
  384. write_varint(f, name_len);
  385. write_varint(f, rxa->datum_len);
  386. #ifndef HAVE_LINUX_XATTRS
  387. if (name_len > rxa->name_len) {
  388. write_buf(f, USER_PREFIX, UPRE_LEN);
  389. name_len -= UPRE_LEN;
  390. }
  391. #endif
  392. write_buf(f, name, name_len);
  393. if (rxa->datum_len > MAX_FULL_DATUM)
  394. write_buf(f, rxa->datum + 1, MAX_DIGEST_LEN);
  395. else
  396. write_buf(f, rxa->datum, rxa->datum_len);
  397. }
  398. ndx = rsync_xal_l.count; /* pre-incremented count */
  399. rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
  400. }
  401. return ndx;
  402. }
  403. /* Return a flag indicating if we need to change a file's xattrs. If
  404. * "find_all" is specified, also mark any abbreviated xattrs that we
  405. * need so that send_xattr_request() can tell the sender about them. */
  406. int xattr_diff(struct file_struct *file, stat_x *sxp, int find_all)
  407. {
  408. item_list *lst = rsync_xal_l.items;
  409. rsync_xa *snd_rxa, *rec_rxa;
  410. int snd_cnt, rec_cnt;
  411. int cmp, same, xattrs_equal = 1;
  412. if (sxp && XATTR_READY(*sxp)) {
  413. rec_rxa = sxp->xattr->items;
  414. rec_cnt = sxp->xattr->count;
  415. } else {
  416. rec_rxa = NULL;
  417. rec_cnt = 0;
  418. }
  419. if (F_XATTR(file) >= 0)
  420. lst += F_XATTR(file);
  421. else
  422. lst = &empty_xattr;
  423. snd_rxa = lst->items;
  424. snd_cnt = lst->count;
  425. /* If the count of the sender's xattrs is different from our
  426. * (receiver's) xattrs, the lists are not the same. */
  427. if (snd_cnt != rec_cnt) {
  428. if (!find_all)
  429. return 1;
  430. xattrs_equal = 0;
  431. }
  432. while (snd_cnt) {
  433. cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1;
  434. if (cmp > 0)
  435. same = 0;
  436. else if (snd_rxa->datum_len > MAX_FULL_DATUM) {
  437. same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
  438. && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1,
  439. MAX_DIGEST_LEN) == 0;
  440. /* Flag unrequested items that we need. */
  441. if (!same && find_all && snd_rxa->datum[0] == XSTATE_ABBREV)
  442. snd_rxa->datum[0] = XSTATE_TODO;
  443. } else {
  444. same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
  445. && memcmp(snd_rxa->datum, rec_rxa->datum,
  446. snd_rxa->datum_len) == 0;
  447. }
  448. if (!same) {
  449. if (!find_all)
  450. return 1;
  451. xattrs_equal = 0;
  452. }
  453. if (cmp <= 0) {
  454. snd_rxa++;
  455. snd_cnt--;
  456. }
  457. if (cmp >= 0) {
  458. rec_rxa++;
  459. rec_cnt--;
  460. }
  461. }
  462. if (rec_cnt)
  463. xattrs_equal = 0;
  464. return !xattrs_equal;
  465. }
  466. /* When called by the generator (with a NULL fname), this tells the sender
  467. * all the abbreviated xattr values we need. When called by the sender
  468. * (with a non-NULL fname), we send all the extra xattr data it needs.
  469. * The generator may also call with f_out < 0 to just change all the
  470. * XSTATE_ABBREV states into XSTATE_DONE. */
  471. void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
  472. {
  473. item_list *lst = rsync_xal_l.items;
  474. int cnt, prior_req = 0;
  475. rsync_xa *rxa;
  476. lst += F_XATTR(file);
  477. for (rxa = lst->items, cnt = lst->count; cnt--; rxa++) {
  478. if (rxa->datum_len <= MAX_FULL_DATUM)
  479. continue;
  480. switch (rxa->datum[0]) {
  481. case XSTATE_ABBREV:
  482. /* Items left abbreviated matched the sender's checksum, so
  483. * the receiver will cache the local data for future use. */
  484. if (am_generator)
  485. rxa->datum[0] = XSTATE_DONE;
  486. continue;
  487. case XSTATE_TODO:
  488. assert(f_out >= 0);
  489. break;
  490. default:
  491. continue;
  492. }
  493. /* Flag that we handled this abbreviated item. */
  494. rxa->datum[0] = XSTATE_DONE;
  495. write_varint(f_out, rxa->num - prior_req);
  496. prior_req = rxa->num;
  497. if (fname) {
  498. size_t len = 0;
  499. char *ptr;
  500. /* Re-read the long datum. */
  501. if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0))) {
  502. rprintf(FERROR_XFER, "failed to re-read xattr %s for %s\n", rxa->name, fname);
  503. write_varint(f_out, 0);
  504. continue;
  505. }
  506. write_varint(f_out, len); /* length might have changed! */
  507. write_buf(f_out, ptr, len);
  508. free(ptr);
  509. }
  510. }
  511. if (f_out >= 0)
  512. write_byte(f_out, 0); /* end the list */
  513. }
  514. /* When called by the sender, read the request from the generator and mark
  515. * any needed xattrs with a flag that lets us know they need to be sent to
  516. * the receiver. When called by the receiver, reads the sent data and
  517. * stores it in place of its checksum. */
  518. int recv_xattr_request(struct file_struct *file, int f_in)
  519. {
  520. item_list *lst = rsync_xal_l.items;
  521. char *old_datum, *name;
  522. rsync_xa *rxa;
  523. int rel_pos, cnt, num, got_xattr_data = 0;
  524. if (F_XATTR(file) < 0) {
  525. rprintf(FERROR, "recv_xattr_request: internal data error!\n");
  526. exit_cleanup(RERR_PROTOCOL);
  527. }
  528. lst += F_XATTR(file);
  529. cnt = lst->count;
  530. rxa = lst->items;
  531. num = 0;
  532. while ((rel_pos = read_varint(f_in)) != 0) {
  533. num += rel_pos;
  534. while (cnt && rxa->num < num) {
  535. rxa++;
  536. cnt--;
  537. }
  538. if (!cnt || rxa->num != num) {
  539. rprintf(FERROR, "[%s] could not find xattr #%d for %s\n",
  540. who_am_i(), num, f_name(file, NULL));
  541. exit_cleanup(RERR_PROTOCOL);
  542. }
  543. if (!XATTR_ABBREV(*rxa) || rxa->datum[0] != XSTATE_ABBREV) {
  544. rprintf(FERROR, "[%s] internal abbrev error on %s (%s, len=%ld)!\n",
  545. who_am_i(), f_name(file, NULL), rxa->name, (long)rxa->datum_len);
  546. exit_cleanup(RERR_PROTOCOL);
  547. }
  548. if (am_sender) {
  549. rxa->datum[0] = XSTATE_TODO;
  550. continue;
  551. }
  552. old_datum = rxa->datum;
  553. rxa->datum_len = read_varint(f_in);
  554. if (rxa->name_len + rxa->datum_len < rxa->name_len)
  555. overflow_exit("recv_xattr_request");
  556. rxa->datum = new_array(char, rxa->datum_len + rxa->name_len);
  557. if (!rxa->datum)
  558. out_of_memory("recv_xattr_request");
  559. name = rxa->datum + rxa->datum_len;
  560. memcpy(name, rxa->name, rxa->name_len);
  561. rxa->name = name;
  562. free(old_datum);
  563. read_buf(f_in, rxa->datum, rxa->datum_len);
  564. got_xattr_data = 1;
  565. }
  566. return got_xattr_data;
  567. }
  568. /* ------------------------------------------------------------------------- */
  569. /* receive and build the rsync_xattr_lists */
  570. void receive_xattr(int f, struct file_struct *file)
  571. {
  572. static item_list temp_xattr = EMPTY_ITEM_LIST;
  573. int count, num;
  574. #ifdef HAVE_LINUX_XATTRS
  575. int need_sort = 0;
  576. #else
  577. int need_sort = 1;
  578. #endif
  579. int ndx = read_varint(f);
  580. if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) {
  581. rprintf(FERROR, "receive_xattr: xa index %d out of"
  582. " range for %s\n", ndx, f_name(file, NULL));
  583. exit_cleanup(RERR_PROTOCOL);
  584. }
  585. if (ndx != 0) {
  586. F_XATTR(file) = ndx - 1;
  587. return;
  588. }
  589. if ((count = read_varint(f)) != 0) {
  590. (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
  591. temp_xattr.count = 0;
  592. }
  593. for (num = 1; num <= count; num++) {
  594. char *ptr, *name;
  595. rsync_xa *rxa;
  596. size_t name_len = read_varint(f);
  597. size_t datum_len = read_varint(f);
  598. size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len;
  599. size_t extra_len = MIGHT_NEED_RPRE ? RPRE_LEN : 0;
  600. if ((dget_len + extra_len < dget_len)
  601. || (dget_len + extra_len + name_len < dget_len))
  602. overflow_exit("receive_xattr");
  603. ptr = new_array(char, dget_len + extra_len + name_len);
  604. if (!ptr)
  605. out_of_memory("receive_xattr");
  606. name = ptr + dget_len + extra_len;
  607. read_buf(f, name, name_len);
  608. if (dget_len == datum_len)
  609. read_buf(f, ptr, dget_len);
  610. else {
  611. *ptr = XSTATE_ABBREV;
  612. read_buf(f, ptr + 1, MAX_DIGEST_LEN);
  613. }
  614. #ifdef HAVE_LINUX_XATTRS
  615. /* Non-root can only save the user namespace. */
  616. if (am_root <= 0 && !HAS_PREFIX(name, USER_PREFIX)) {
  617. if (!am_root) {
  618. free(ptr);
  619. continue;
  620. }
  621. name -= RPRE_LEN;
  622. name_len += RPRE_LEN;
  623. memcpy(name, RSYNC_PREFIX, RPRE_LEN);
  624. need_sort = 1;
  625. }
  626. #else
  627. /* This OS only has a user namespace, so we either
  628. * strip the user prefix, or we put a non-user
  629. * namespace inside our rsync hierarchy. */
  630. if (HAS_PREFIX(name, USER_PREFIX)) {
  631. name += UPRE_LEN;
  632. name_len -= UPRE_LEN;
  633. } else if (am_root) {
  634. name -= RPRE_LEN;
  635. name_len += RPRE_LEN;
  636. memcpy(name, RSYNC_PREFIX, RPRE_LEN);
  637. } else {
  638. free(ptr);
  639. continue;
  640. }
  641. #endif
  642. /* No rsync.%FOO attributes are copied w/o 2 -X options. */
  643. if (preserve_xattrs < 2 && name_len > RPRE_LEN
  644. && name[RPRE_LEN] == '%' && HAS_PREFIX(name, RSYNC_PREFIX)) {
  645. free(ptr);
  646. continue;
  647. }
  648. rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
  649. rxa->name = name;
  650. rxa->datum = ptr;
  651. rxa->name_len = name_len;
  652. rxa->datum_len = datum_len;
  653. rxa->num = num;
  654. }
  655. if (need_sort && count > 1)
  656. qsort(temp_xattr.items, count, sizeof (rsync_xa), rsync_xal_compare_names);
  657. ndx = rsync_xal_l.count; /* pre-incremented count */
  658. rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
  659. F_XATTR(file) = ndx;
  660. }
  661. /* Turn the xattr data in stat_x into cached xattr data, setting the index
  662. * values in the file struct. */
  663. void cache_tmp_xattr(struct file_struct *file, stat_x *sxp)
  664. {
  665. int ndx;
  666. if (!sxp->xattr)
  667. return;
  668. if (prior_xattr_count == (size_t)-1)
  669. prior_xattr_count = rsync_xal_l.count;
  670. ndx = find_matching_xattr(sxp->xattr);
  671. if (ndx < 0)
  672. rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
  673. F_XATTR(file) = ndx;
  674. }
  675. void uncache_tmp_xattrs(void)
  676. {
  677. if (prior_xattr_count != (size_t)-1) {
  678. item_list *xattr_item = rsync_xal_l.items;
  679. item_list *xattr_start = xattr_item + prior_xattr_count;
  680. xattr_item += rsync_xal_l.count;
  681. rsync_xal_l.count = prior_xattr_count;
  682. while (xattr_item-- > xattr_start)
  683. rsync_xal_free(xattr_item);
  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 */