acls.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /*
  2. * Handle passing Access Control Lists between systems.
  3. *
  4. * Copyright (C) 1996 Andrew Tridgell
  5. * Copyright (C) 1996 Paul Mackerras
  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 "lib/sysacls.h"
  23. #ifdef SUPPORT_ACLS
  24. extern int dry_run;
  25. extern int am_root;
  26. extern int read_only;
  27. extern int list_only;
  28. extern int orig_umask;
  29. extern int numeric_ids;
  30. extern int inc_recurse;
  31. extern int preserve_devices;
  32. extern int preserve_specials;
  33. /* Flags used to indicate what items are being transmitted for an entry. */
  34. #define XMIT_USER_OBJ (1<<0)
  35. #define XMIT_GROUP_OBJ (1<<1)
  36. #define XMIT_MASK_OBJ (1<<2)
  37. #define XMIT_OTHER_OBJ (1<<3)
  38. #define XMIT_NAME_LIST (1<<4)
  39. #define NO_ENTRY ((uchar)0x80) /* Default value of a NON-name-list entry. */
  40. #define NAME_IS_USER (1u<<31) /* Bit used only on a name-list entry. */
  41. /* When we send the access bits over the wire, we shift them 2 bits to the
  42. * left and use the lower 2 bits as flags (relevant only to a name entry).
  43. * This makes the protocol more efficient than sending a value that would
  44. * be likely to have its hightest bits set. */
  45. #define XFLAG_NAME_FOLLOWS 0x0001u
  46. #define XFLAG_NAME_IS_USER 0x0002u
  47. /* === ACL structures === */
  48. typedef struct {
  49. id_t id;
  50. uint32 access;
  51. } id_access;
  52. typedef struct {
  53. id_access *idas;
  54. int count;
  55. } ida_entries;
  56. typedef struct {
  57. char *name;
  58. uchar len;
  59. } idname;
  60. typedef struct rsync_acl {
  61. ida_entries names;
  62. /* These will be NO_ENTRY if there's no such entry. */
  63. uchar user_obj;
  64. uchar group_obj;
  65. uchar mask_obj;
  66. uchar other_obj;
  67. } rsync_acl;
  68. typedef struct {
  69. rsync_acl racl;
  70. SMB_ACL_T sacl;
  71. } acl_duo;
  72. static const rsync_acl empty_rsync_acl = {
  73. {NULL, 0}, NO_ENTRY, NO_ENTRY, NO_ENTRY, NO_ENTRY
  74. };
  75. static item_list access_acl_list = EMPTY_ITEM_LIST;
  76. static item_list default_acl_list = EMPTY_ITEM_LIST;
  77. static size_t prior_access_count = (size_t)-1;
  78. static size_t prior_default_count = (size_t)-1;
  79. /* === Calculations on ACL types === */
  80. static const char *str_acl_type(SMB_ACL_TYPE_T type)
  81. {
  82. switch (type) {
  83. case SMB_ACL_TYPE_ACCESS:
  84. #ifdef HAVE_OSX_ACLS
  85. return "ACL_TYPE_EXTENDED";
  86. #else
  87. return "ACL_TYPE_ACCESS";
  88. #endif
  89. case SMB_ACL_TYPE_DEFAULT:
  90. return "ACL_TYPE_DEFAULT";
  91. default:
  92. break;
  93. }
  94. return "unknown ACL type!";
  95. }
  96. static int calc_sacl_entries(const rsync_acl *racl)
  97. {
  98. /* A System ACL always gets user/group/other permission entries. */
  99. return racl->names.count
  100. #ifdef ACLS_NEED_MASK
  101. + 1
  102. #else
  103. + (racl->mask_obj != NO_ENTRY)
  104. #endif
  105. + 3;
  106. }
  107. /* Extracts and returns the permission bits from the ACL. This cannot be
  108. * called on an rsync_acl that has NO_ENTRY in any spot but the mask. */
  109. static int rsync_acl_get_perms(const rsync_acl *racl)
  110. {
  111. return (racl->user_obj << 6)
  112. + ((racl->mask_obj != NO_ENTRY ? racl->mask_obj : racl->group_obj) << 3)
  113. + racl->other_obj;
  114. }
  115. /* Removes the permission-bit entries from the ACL because these
  116. * can be reconstructed from the file's mode. */
  117. static void rsync_acl_strip_perms(stat_x *sxp)
  118. {
  119. rsync_acl *racl = sxp->acc_acl;
  120. racl->user_obj = NO_ENTRY;
  121. if (racl->mask_obj == NO_ENTRY)
  122. racl->group_obj = NO_ENTRY;
  123. else {
  124. int group_perms = (sxp->st.st_mode >> 3) & 7;
  125. if (racl->group_obj == group_perms)
  126. racl->group_obj = NO_ENTRY;
  127. #ifndef HAVE_SOLARIS_ACLS
  128. if (racl->names.count != 0 && racl->mask_obj == group_perms)
  129. racl->mask_obj = NO_ENTRY;
  130. #endif
  131. }
  132. racl->other_obj = NO_ENTRY;
  133. }
  134. /* Given an empty rsync_acl, fake up the permission bits. */
  135. static void rsync_acl_fake_perms(rsync_acl *racl, mode_t mode)
  136. {
  137. racl->user_obj = (mode >> 6) & 7;
  138. racl->group_obj = (mode >> 3) & 7;
  139. racl->other_obj = mode & 7;
  140. }
  141. /* === Rsync ACL functions === */
  142. static rsync_acl *create_racl(void)
  143. {
  144. rsync_acl *racl = new(rsync_acl);
  145. if (!racl)
  146. out_of_memory("create_racl");
  147. *racl = empty_rsync_acl;
  148. return racl;
  149. }
  150. static BOOL ida_entries_equal(const ida_entries *ial1, const ida_entries *ial2)
  151. {
  152. id_access *ida1, *ida2;
  153. int count = ial1->count;
  154. if (count != ial2->count)
  155. return False;
  156. ida1 = ial1->idas;
  157. ida2 = ial2->idas;
  158. for (; count--; ida1++, ida2++) {
  159. if (ida1->access != ida2->access || ida1->id != ida2->id)
  160. return False;
  161. }
  162. return True;
  163. }
  164. static BOOL rsync_acl_equal(const rsync_acl *racl1, const rsync_acl *racl2)
  165. {
  166. return racl1->user_obj == racl2->user_obj
  167. && racl1->group_obj == racl2->group_obj
  168. && racl1->mask_obj == racl2->mask_obj
  169. && racl1->other_obj == racl2->other_obj
  170. && ida_entries_equal(&racl1->names, &racl2->names);
  171. }
  172. /* Are the extended (non-permission-bit) entries equal? If so, the rest of
  173. * the ACL will be handled by the normal mode-preservation code. This is
  174. * only meaningful for access ACLs! Note: the 1st arg is a fully-populated
  175. * rsync_acl, but the 2nd parameter can be a condensed rsync_acl, which means
  176. * that it might have several of its permission objects set to NO_ENTRY. */
  177. static BOOL rsync_acl_equal_enough(const rsync_acl *racl1,
  178. const rsync_acl *racl2, mode_t m)
  179. {
  180. if ((racl1->mask_obj ^ racl2->mask_obj) & NO_ENTRY)
  181. return False; /* One has a mask and the other doesn't */
  182. /* When there's a mask, the group_obj becomes an extended entry. */
  183. if (racl1->mask_obj != NO_ENTRY) {
  184. /* A condensed rsync_acl with a mask can only have no
  185. * group_obj when it was identical to the mask. This
  186. * means that it was also identical to the group attrs
  187. * from the mode. */
  188. if (racl2->group_obj == NO_ENTRY) {
  189. if (racl1->group_obj != ((m >> 3) & 7))
  190. return False;
  191. } else if (racl1->group_obj != racl2->group_obj)
  192. return False;
  193. }
  194. return ida_entries_equal(&racl1->names, &racl2->names);
  195. }
  196. static void rsync_acl_free(rsync_acl *racl)
  197. {
  198. if (racl->names.idas)
  199. free(racl->names.idas);
  200. *racl = empty_rsync_acl;
  201. }
  202. void free_acl(stat_x *sxp)
  203. {
  204. if (sxp->acc_acl) {
  205. rsync_acl_free(sxp->acc_acl);
  206. free(sxp->acc_acl);
  207. sxp->acc_acl = NULL;
  208. }
  209. if (sxp->def_acl) {
  210. rsync_acl_free(sxp->def_acl);
  211. free(sxp->def_acl);
  212. sxp->def_acl = NULL;
  213. }
  214. }
  215. #ifdef SMB_ACL_NEED_SORT
  216. static int id_access_sorter(const void *r1, const void *r2)
  217. {
  218. id_access *ida1 = (id_access *)r1;
  219. id_access *ida2 = (id_access *)r2;
  220. id_t rid1 = ida1->id, rid2 = ida2->id;
  221. if ((ida1->access ^ ida2->access) & NAME_IS_USER)
  222. return ida1->access & NAME_IS_USER ? -1 : 1;
  223. return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
  224. }
  225. #endif
  226. /* === System ACLs === */
  227. /* Unpack system ACL -> rsync ACL verbatim. Return whether we succeeded. */
  228. static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl)
  229. {
  230. static item_list temp_ida_list = EMPTY_ITEM_LIST;
  231. SMB_ACL_ENTRY_T entry;
  232. const char *errfun;
  233. int rc;
  234. errfun = "sys_acl_get_entry";
  235. for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
  236. rc == 1;
  237. rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
  238. SMB_ACL_TAG_T tag_type;
  239. uint32 access;
  240. id_t g_u_id;
  241. id_access *ida;
  242. if ((rc = sys_acl_get_info(entry, &tag_type, &access, &g_u_id)) != 0) {
  243. errfun = "sys_acl_get_info";
  244. break;
  245. }
  246. /* continue == done with entry; break == store in temporary ida list */
  247. switch (tag_type) {
  248. #ifndef HAVE_OSX_ACLS
  249. case SMB_ACL_USER_OBJ:
  250. if (racl->user_obj == NO_ENTRY)
  251. racl->user_obj = access;
  252. else
  253. rprintf(FINFO, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
  254. continue;
  255. case SMB_ACL_GROUP_OBJ:
  256. if (racl->group_obj == NO_ENTRY)
  257. racl->group_obj = access;
  258. else
  259. rprintf(FINFO, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
  260. continue;
  261. case SMB_ACL_MASK:
  262. if (racl->mask_obj == NO_ENTRY)
  263. racl->mask_obj = access;
  264. else
  265. rprintf(FINFO, "unpack_smb_acl: warning: duplicate MASK entry ignored\n");
  266. continue;
  267. case SMB_ACL_OTHER:
  268. if (racl->other_obj == NO_ENTRY)
  269. racl->other_obj = access;
  270. else
  271. rprintf(FINFO, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
  272. continue;
  273. #endif
  274. case SMB_ACL_USER:
  275. access |= NAME_IS_USER;
  276. break;
  277. case SMB_ACL_GROUP:
  278. break;
  279. default:
  280. rprintf(FINFO, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
  281. continue;
  282. }
  283. ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
  284. ida->id = g_u_id;
  285. ida->access = access;
  286. }
  287. if (rc) {
  288. rsyserr(FERROR_XFER, errno, "unpack_smb_acl: %s()", errfun);
  289. rsync_acl_free(racl);
  290. return False;
  291. }
  292. /* Transfer the count id_access items out of the temp_ida_list
  293. * into the names ida_entries list in racl. */
  294. if (temp_ida_list.count) {
  295. #ifdef SMB_ACL_NEED_SORT
  296. if (temp_ida_list.count > 1) {
  297. qsort(temp_ida_list.items, temp_ida_list.count,
  298. sizeof (id_access), id_access_sorter);
  299. }
  300. #endif
  301. if (!(racl->names.idas = new_array(id_access, temp_ida_list.count)))
  302. out_of_memory("unpack_smb_acl");
  303. memcpy(racl->names.idas, temp_ida_list.items,
  304. temp_ida_list.count * sizeof (id_access));
  305. } else
  306. racl->names.idas = NULL;
  307. racl->names.count = temp_ida_list.count;
  308. /* Truncate the temporary list now that its idas have been saved. */
  309. temp_ida_list.count = 0;
  310. return True;
  311. }
  312. /* Synactic sugar for system calls */
  313. #define CALL_OR_ERROR(func,args,str) \
  314. do { \
  315. if (func args) { \
  316. errfun = str; \
  317. goto error_exit; \
  318. } \
  319. } while (0)
  320. #define COE(func,args) CALL_OR_ERROR(func,args,#func)
  321. #define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
  322. #ifndef HAVE_OSX_ACLS
  323. /* Store the permissions in the system ACL entry. */
  324. static int store_access_in_entry(uint32 access, SMB_ACL_ENTRY_T entry)
  325. {
  326. if (sys_acl_set_access_bits(entry, access)) {
  327. rsyserr(FERROR_XFER, errno, "store_access_in_entry sys_acl_set_access_bits()");
  328. return -1;
  329. }
  330. return 0;
  331. }
  332. #endif
  333. /* Pack rsync ACL -> system ACL verbatim. Return whether we succeeded. */
  334. static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
  335. {
  336. #ifdef ACLS_NEED_MASK
  337. uchar mask_bits;
  338. #endif
  339. size_t count;
  340. id_access *ida;
  341. const char *errfun = NULL;
  342. SMB_ACL_ENTRY_T entry;
  343. if (!(*smb_acl = sys_acl_init(calc_sacl_entries(racl)))) {
  344. rsyserr(FERROR_XFER, errno, "pack_smb_acl: sys_acl_init()");
  345. return False;
  346. }
  347. #ifndef HAVE_OSX_ACLS
  348. COE( sys_acl_create_entry,(smb_acl, &entry) );
  349. COE( sys_acl_set_info,(entry, SMB_ACL_USER_OBJ, racl->user_obj & ~NO_ENTRY, 0) );
  350. #endif
  351. for (ida = racl->names.idas, count = racl->names.count; count; ida++, count--) {
  352. #ifdef SMB_ACL_NEED_SORT
  353. if (!(ida->access & NAME_IS_USER))
  354. break;
  355. #endif
  356. COE( sys_acl_create_entry,(smb_acl, &entry) );
  357. COE( sys_acl_set_info,
  358. (entry,
  359. ida->access & NAME_IS_USER ? SMB_ACL_USER : SMB_ACL_GROUP,
  360. ida->access & ~NAME_IS_USER, ida->id) );
  361. }
  362. #ifndef HAVE_OSX_ACLS
  363. COE( sys_acl_create_entry,(smb_acl, &entry) );
  364. COE( sys_acl_set_info,(entry, SMB_ACL_GROUP_OBJ, racl->group_obj & ~NO_ENTRY, 0) );
  365. #ifdef SMB_ACL_NEED_SORT
  366. for ( ; count; ida++, count--) {
  367. COE( sys_acl_create_entry,(smb_acl, &entry) );
  368. COE( sys_acl_set_info,(entry, SMB_ACL_GROUP, ida->access, ida->id) );
  369. }
  370. #endif
  371. #ifdef ACLS_NEED_MASK
  372. mask_bits = racl->mask_obj == NO_ENTRY ? racl->group_obj & ~NO_ENTRY : racl->mask_obj;
  373. COE( sys_acl_create_entry,(smb_acl, &entry) );
  374. COE( sys_acl_set_info,(entry, SMB_ACL_MASK, mask_bits, NULL) );
  375. #else
  376. if (racl->mask_obj != NO_ENTRY) {
  377. COE( sys_acl_create_entry,(smb_acl, &entry) );
  378. COE( sys_acl_set_info,(entry, SMB_ACL_MASK, racl->mask_obj, 0) );
  379. }
  380. #endif
  381. COE( sys_acl_create_entry,(smb_acl, &entry) );
  382. COE( sys_acl_set_info,(entry, SMB_ACL_OTHER, racl->other_obj & ~NO_ENTRY, 0) );
  383. #endif
  384. #ifdef DEBUG
  385. if (sys_acl_valid(*smb_acl) < 0)
  386. rprintf(FERROR_XFER, "pack_smb_acl: warning: system says the ACL I packed is invalid\n");
  387. #endif
  388. return True;
  389. error_exit:
  390. if (errfun) {
  391. rsyserr(FERROR_XFER, errno, "pack_smb_acl %s()", errfun);
  392. }
  393. sys_acl_free_acl(*smb_acl);
  394. return False;
  395. }
  396. static int find_matching_rsync_acl(const rsync_acl *racl, SMB_ACL_TYPE_T type,
  397. const item_list *racl_list)
  398. {
  399. static int access_match = -1, default_match = -1;
  400. int *match = type == SMB_ACL_TYPE_ACCESS ? &access_match : &default_match;
  401. size_t count = racl_list->count;
  402. /* If this is the first time through or we didn't match the last
  403. * time, then start at the end of the list, which should be the
  404. * best place to start hunting. */
  405. if (*match == -1)
  406. *match = racl_list->count - 1;
  407. while (count--) {
  408. rsync_acl *base = racl_list->items;
  409. if (rsync_acl_equal(base + *match, racl))
  410. return *match;
  411. if (!(*match)--)
  412. *match = racl_list->count - 1;
  413. }
  414. *match = -1;
  415. return *match;
  416. }
  417. static int get_rsync_acl(const char *fname, rsync_acl *racl,
  418. SMB_ACL_TYPE_T type, mode_t mode)
  419. {
  420. SMB_ACL_T sacl;
  421. #ifdef SUPPORT_XATTRS
  422. /* --fake-super support: load ACLs from an xattr. */
  423. if (am_root < 0) {
  424. char *buf;
  425. size_t len;
  426. int cnt;
  427. if ((buf = get_xattr_acl(fname, type == SMB_ACL_TYPE_ACCESS, &len)) == NULL)
  428. return 0;
  429. cnt = (len - 4*4) / (4+4);
  430. if (len < 4*4 || len != (size_t)cnt*(4+4) + 4*4) {
  431. free(buf);
  432. return -1;
  433. }
  434. racl->user_obj = IVAL(buf, 0);
  435. if (racl->user_obj == NO_ENTRY)
  436. racl->user_obj = (mode >> 6) & 7;
  437. racl->group_obj = IVAL(buf, 4);
  438. if (racl->group_obj == NO_ENTRY)
  439. racl->group_obj = (mode >> 3) & 7;
  440. racl->mask_obj = IVAL(buf, 8);
  441. racl->other_obj = IVAL(buf, 12);
  442. if (racl->other_obj == NO_ENTRY)
  443. racl->other_obj = mode & 7;
  444. if (cnt) {
  445. char *bp = buf + 4*4;
  446. id_access *ida;
  447. if (!(ida = racl->names.idas = new_array(id_access, cnt)))
  448. out_of_memory("get_rsync_acl");
  449. racl->names.count = cnt;
  450. for ( ; cnt--; ida++, bp += 4+4) {
  451. ida->id = IVAL(bp, 0);
  452. ida->access = IVAL(bp, 4);
  453. }
  454. }
  455. free(buf);
  456. return 0;
  457. }
  458. #endif
  459. if ((sacl = sys_acl_get_file(fname, type)) != 0) {
  460. BOOL ok = unpack_smb_acl(sacl, racl);
  461. sys_acl_free_acl(sacl);
  462. if (!ok) {
  463. return -1;
  464. }
  465. } else if (no_acl_syscall_error(errno)) {
  466. /* ACLs are not supported, so pretend we have a basic ACL. */
  467. if (type == SMB_ACL_TYPE_ACCESS)
  468. rsync_acl_fake_perms(racl, mode);
  469. } else {
  470. rsyserr(FERROR_XFER, errno, "get_acl: sys_acl_get_file(%s, %s)",
  471. fname, str_acl_type(type));
  472. return -1;
  473. }
  474. return 0;
  475. }
  476. /* Return the Access Control List for the given filename. */
  477. int get_acl(const char *fname, stat_x *sxp)
  478. {
  479. sxp->acc_acl = create_racl();
  480. if (S_ISREG(sxp->st.st_mode) || S_ISDIR(sxp->st.st_mode)) {
  481. /* Everyone supports this. */
  482. } else if (S_ISLNK(sxp->st.st_mode)) {
  483. return 0;
  484. } else if (IS_SPECIAL(sxp->st.st_mode)) {
  485. #ifndef NO_SPECIAL_ACLS
  486. if (!preserve_specials)
  487. #endif
  488. return 0;
  489. } else if (IS_DEVICE(sxp->st.st_mode)) {
  490. #ifndef NO_DEVICE_ACLS
  491. if (!preserve_devices)
  492. #endif
  493. return 0;
  494. }
  495. if (get_rsync_acl(fname, sxp->acc_acl, SMB_ACL_TYPE_ACCESS,
  496. sxp->st.st_mode) < 0) {
  497. free_acl(sxp);
  498. return -1;
  499. }
  500. if (S_ISDIR(sxp->st.st_mode)) {
  501. sxp->def_acl = create_racl();
  502. if (get_rsync_acl(fname, sxp->def_acl, SMB_ACL_TYPE_DEFAULT,
  503. sxp->st.st_mode) < 0) {
  504. free_acl(sxp);
  505. return -1;
  506. }
  507. }
  508. return 0;
  509. }
  510. /* === Send functions === */
  511. /* Send the ida list over the file descriptor. */
  512. static void send_ida_entries(int f, const ida_entries *idal)
  513. {
  514. id_access *ida;
  515. size_t count = idal->count;
  516. write_varint(f, idal->count);
  517. for (ida = idal->idas; count--; ida++) {
  518. uint32 xbits = ida->access << 2;
  519. const char *name;
  520. if (ida->access & NAME_IS_USER) {
  521. xbits |= XFLAG_NAME_IS_USER;
  522. name = numeric_ids ? NULL : add_uid(ida->id);
  523. } else
  524. name = numeric_ids ? NULL : add_gid(ida->id);
  525. write_varint(f, ida->id);
  526. if (inc_recurse && name) {
  527. int len = strlen(name);
  528. write_varint(f, xbits | XFLAG_NAME_FOLLOWS);
  529. write_byte(f, len);
  530. write_buf(f, name, len);
  531. } else
  532. write_varint(f, xbits);
  533. }
  534. }
  535. static void send_rsync_acl(int f, rsync_acl *racl, SMB_ACL_TYPE_T type,
  536. item_list *racl_list)
  537. {
  538. int ndx = find_matching_rsync_acl(racl, type, racl_list);
  539. /* Send 0 (-1 + 1) to indicate that literal ACL data follows. */
  540. write_varint(f, ndx + 1);
  541. if (ndx < 0) {
  542. rsync_acl *new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);
  543. uchar flags = 0;
  544. if (racl->user_obj != NO_ENTRY)
  545. flags |= XMIT_USER_OBJ;
  546. if (racl->group_obj != NO_ENTRY)
  547. flags |= XMIT_GROUP_OBJ;
  548. if (racl->mask_obj != NO_ENTRY)
  549. flags |= XMIT_MASK_OBJ;
  550. if (racl->other_obj != NO_ENTRY)
  551. flags |= XMIT_OTHER_OBJ;
  552. if (racl->names.count)
  553. flags |= XMIT_NAME_LIST;
  554. write_byte(f, flags);
  555. if (flags & XMIT_USER_OBJ)
  556. write_varint(f, racl->user_obj);
  557. if (flags & XMIT_GROUP_OBJ)
  558. write_varint(f, racl->group_obj);
  559. if (flags & XMIT_MASK_OBJ)
  560. write_varint(f, racl->mask_obj);
  561. if (flags & XMIT_OTHER_OBJ)
  562. write_varint(f, racl->other_obj);
  563. if (flags & XMIT_NAME_LIST)
  564. send_ida_entries(f, &racl->names);
  565. /* Give the allocated data to the new list object. */
  566. *new_racl = *racl;
  567. *racl = empty_rsync_acl;
  568. }
  569. }
  570. /* Send the ACL from the stat_x structure down the indicated file descriptor.
  571. * This also frees the ACL data. */
  572. void send_acl(int f, stat_x *sxp)
  573. {
  574. if (!sxp->acc_acl) {
  575. sxp->acc_acl = create_racl();
  576. rsync_acl_fake_perms(sxp->acc_acl, sxp->st.st_mode);
  577. }
  578. /* Avoid sending values that can be inferred from other data. */
  579. rsync_acl_strip_perms(sxp);
  580. send_rsync_acl(f, sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list);
  581. if (S_ISDIR(sxp->st.st_mode)) {
  582. if (!sxp->def_acl)
  583. sxp->def_acl = create_racl();
  584. send_rsync_acl(f, sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list);
  585. }
  586. }
  587. /* === Receive functions === */
  588. static uint32 recv_acl_access(int f, uchar *name_follows_ptr)
  589. {
  590. uint32 access = read_varint(f);
  591. if (name_follows_ptr) {
  592. int flags = access & 3;
  593. access >>= 2;
  594. if (am_root >= 0 && access & ~SMB_ACL_VALID_NAME_BITS)
  595. goto value_error;
  596. if (flags & XFLAG_NAME_FOLLOWS)
  597. *name_follows_ptr = 1;
  598. else
  599. *name_follows_ptr = 0;
  600. if (flags & XFLAG_NAME_IS_USER)
  601. access |= NAME_IS_USER;
  602. } else if (am_root >= 0 && access & ~SMB_ACL_VALID_OBJ_BITS) {
  603. value_error:
  604. rprintf(FERROR_XFER, "recv_acl_access: value out of range: %x\n",
  605. access);
  606. exit_cleanup(RERR_STREAMIO);
  607. }
  608. return access;
  609. }
  610. static uchar recv_ida_entries(int f, ida_entries *ent)
  611. {
  612. uchar computed_mask_bits = 0;
  613. int i, count = read_varint(f);
  614. if (count) {
  615. if (!(ent->idas = new_array(id_access, count)))
  616. out_of_memory("recv_ida_entries");
  617. } else
  618. ent->idas = NULL;
  619. ent->count = count;
  620. for (i = 0; i < count; i++) {
  621. uchar has_name;
  622. id_t id = read_varint(f);
  623. uint32 access = recv_acl_access(f, &has_name);
  624. if (has_name) {
  625. if (access & NAME_IS_USER)
  626. id = recv_user_name(f, id);
  627. else
  628. id = recv_group_name(f, id, NULL);
  629. } else if (access & NAME_IS_USER) {
  630. if (inc_recurse && am_root && !numeric_ids)
  631. id = match_uid(id);
  632. } else {
  633. if (inc_recurse && (!am_root || !numeric_ids))
  634. id = match_gid(id, NULL);
  635. }
  636. ent->idas[i].id = id;
  637. ent->idas[i].access = access;
  638. computed_mask_bits |= access;
  639. }
  640. return computed_mask_bits & ~NO_ENTRY;
  641. }
  642. static int recv_rsync_acl(int f, item_list *racl_list, SMB_ACL_TYPE_T type, mode_t mode)
  643. {
  644. uchar computed_mask_bits = 0;
  645. acl_duo *duo_item;
  646. uchar flags;
  647. int ndx = read_varint(f);
  648. if (ndx < 0 || (size_t)ndx > racl_list->count) {
  649. rprintf(FERROR_XFER, "recv_acl_index: %s ACL index %d > %d\n",
  650. str_acl_type(type), ndx, (int)racl_list->count);
  651. exit_cleanup(RERR_STREAMIO);
  652. }
  653. if (ndx != 0)
  654. return ndx - 1;
  655. ndx = racl_list->count;
  656. duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
  657. duo_item->racl = empty_rsync_acl;
  658. flags = read_byte(f);
  659. if (flags & XMIT_USER_OBJ)
  660. duo_item->racl.user_obj = recv_acl_access(f, NULL);
  661. if (flags & XMIT_GROUP_OBJ)
  662. duo_item->racl.group_obj = recv_acl_access(f, NULL);
  663. if (flags & XMIT_MASK_OBJ)
  664. duo_item->racl.mask_obj = recv_acl_access(f, NULL);
  665. if (flags & XMIT_OTHER_OBJ)
  666. duo_item->racl.other_obj = recv_acl_access(f, NULL);
  667. if (flags & XMIT_NAME_LIST)
  668. computed_mask_bits |= recv_ida_entries(f, &duo_item->racl.names);
  669. #ifdef HAVE_OSX_ACLS
  670. /* If we received a superfluous mask, throw it away. */
  671. duo_item->racl.mask_obj = NO_ENTRY;
  672. #else
  673. if (duo_item->racl.names.count && duo_item->racl.mask_obj == NO_ENTRY) {
  674. /* Mask must be non-empty with lists. */
  675. if (type == SMB_ACL_TYPE_ACCESS)
  676. computed_mask_bits = (mode >> 3) & 7;
  677. else
  678. computed_mask_bits |= duo_item->racl.group_obj & ~NO_ENTRY;
  679. duo_item->racl.mask_obj = computed_mask_bits;
  680. }
  681. #endif
  682. duo_item->sacl = NULL;
  683. return ndx;
  684. }
  685. /* Receive the ACL info the sender has included for this file-list entry. */
  686. void receive_acl(int f, struct file_struct *file)
  687. {
  688. F_ACL(file) = recv_rsync_acl(f, &access_acl_list, SMB_ACL_TYPE_ACCESS, file->mode);
  689. if (S_ISDIR(file->mode))
  690. F_DIR_DEFACL(file) = recv_rsync_acl(f, &default_acl_list, SMB_ACL_TYPE_DEFAULT, 0);
  691. }
  692. static int cache_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, item_list *racl_list)
  693. {
  694. int ndx;
  695. if (!racl)
  696. ndx = -1;
  697. else if ((ndx = find_matching_rsync_acl(racl, type, racl_list)) == -1) {
  698. acl_duo *new_duo;
  699. ndx = racl_list->count;
  700. new_duo = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
  701. new_duo->racl = *racl;
  702. new_duo->sacl = NULL;
  703. *racl = empty_rsync_acl;
  704. }
  705. return ndx;
  706. }
  707. /* Turn the ACL data in stat_x into cached ACL data, setting the index
  708. * values in the file struct. */
  709. void cache_tmp_acl(struct file_struct *file, stat_x *sxp)
  710. {
  711. if (prior_access_count == (size_t)-1)
  712. prior_access_count = access_acl_list.count;
  713. F_ACL(file) = cache_rsync_acl(sxp->acc_acl,
  714. SMB_ACL_TYPE_ACCESS, &access_acl_list);
  715. if (S_ISDIR(sxp->st.st_mode)) {
  716. if (prior_default_count == (size_t)-1)
  717. prior_default_count = default_acl_list.count;
  718. F_DIR_DEFACL(file) = cache_rsync_acl(sxp->def_acl,
  719. SMB_ACL_TYPE_DEFAULT, &default_acl_list);
  720. }
  721. }
  722. static void uncache_duo_acls(item_list *duo_list, size_t start)
  723. {
  724. acl_duo *duo_item = duo_list->items;
  725. acl_duo *duo_start = duo_item + start;
  726. duo_item += duo_list->count;
  727. duo_list->count = start;
  728. while (duo_item-- > duo_start) {
  729. rsync_acl_free(&duo_item->racl);
  730. if (duo_item->sacl)
  731. sys_acl_free_acl(duo_item->sacl);
  732. }
  733. }
  734. void uncache_tmp_acls(void)
  735. {
  736. if (prior_access_count != (size_t)-1) {
  737. uncache_duo_acls(&access_acl_list, prior_access_count);
  738. prior_access_count = (size_t)-1;
  739. }
  740. if (prior_default_count != (size_t)-1) {
  741. uncache_duo_acls(&default_acl_list, prior_default_count);
  742. prior_default_count = (size_t)-1;
  743. }
  744. }
  745. #ifndef HAVE_OSX_ACLS
  746. static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
  747. {
  748. SMB_ACL_ENTRY_T entry;
  749. const char *errfun;
  750. int rc;
  751. if (S_ISDIR(mode)) {
  752. /* If the sticky bit is going on, it's not safe to allow all
  753. * the new ACL to go into effect before it gets set. */
  754. #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
  755. if (mode & S_ISVTX)
  756. mode &= ~0077;
  757. #else
  758. if (mode & S_ISVTX && !(old_mode & S_ISVTX))
  759. mode &= ~0077;
  760. } else {
  761. /* If setuid or setgid is going off, it's not safe to allow all
  762. * the new ACL to go into effect before they get cleared. */
  763. if ((old_mode & S_ISUID && !(mode & S_ISUID))
  764. || (old_mode & S_ISGID && !(mode & S_ISGID)))
  765. mode &= ~0077;
  766. #endif
  767. }
  768. errfun = "sys_acl_get_entry";
  769. for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
  770. rc == 1;
  771. rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
  772. SMB_ACL_TAG_T tag_type;
  773. if ((rc = sys_acl_get_tag_type(entry, &tag_type)) != 0) {
  774. errfun = "sys_acl_get_tag_type";
  775. break;
  776. }
  777. switch (tag_type) {
  778. case SMB_ACL_USER_OBJ:
  779. COE2( store_access_in_entry,((mode >> 6) & 7, entry) );
  780. break;
  781. case SMB_ACL_GROUP_OBJ:
  782. /* group is only empty when identical to group perms. */
  783. if (racl->group_obj != NO_ENTRY)
  784. break;
  785. COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
  786. break;
  787. case SMB_ACL_MASK:
  788. #ifndef HAVE_SOLARIS_ACLS
  789. #ifndef ACLS_NEED_MASK
  790. /* mask is only empty when we don't need it. */
  791. if (racl->mask_obj == NO_ENTRY)
  792. break;
  793. #endif
  794. COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
  795. #endif
  796. break;
  797. case SMB_ACL_OTHER:
  798. COE2( store_access_in_entry,(mode & 7, entry) );
  799. break;
  800. }
  801. }
  802. if (rc) {
  803. error_exit:
  804. if (errfun) {
  805. rsyserr(FERROR_XFER, errno, "change_sacl_perms: %s()",
  806. errfun);
  807. }
  808. return (mode_t)-1;
  809. }
  810. #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
  811. /* Ensure that chmod() will be called to restore any lost setid bits. */
  812. if (old_mode & (S_ISUID | S_ISGID | S_ISVTX)
  813. && BITS_EQUAL(old_mode, mode, CHMOD_BITS))
  814. old_mode &= ~(S_ISUID | S_ISGID | S_ISVTX);
  815. #endif
  816. /* Return the mode of the file on disk, as we will set them. */
  817. return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);
  818. }
  819. #endif
  820. static int set_rsync_acl(const char *fname, acl_duo *duo_item,
  821. SMB_ACL_TYPE_T type, stat_x *sxp, mode_t mode)
  822. {
  823. if (type == SMB_ACL_TYPE_DEFAULT
  824. && duo_item->racl.user_obj == NO_ENTRY) {
  825. int rc;
  826. #ifdef SUPPORT_XATTRS
  827. /* --fake-super support: delete default ACL from xattrs. */
  828. if (am_root < 0)
  829. rc = del_def_xattr_acl(fname);
  830. else
  831. #endif
  832. rc = sys_acl_delete_def_file(fname);
  833. if (rc < 0) {
  834. rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_delete_def_file(%s)",
  835. fname);
  836. return -1;
  837. }
  838. #ifdef SUPPORT_XATTRS
  839. } else if (am_root < 0) {
  840. /* --fake-super support: store ACLs in an xattr. */
  841. int cnt = duo_item->racl.names.count;
  842. size_t len = 4*4 + cnt * (4+4);
  843. char *buf = new_array(char, len);
  844. int rc;
  845. SIVAL(buf, 0, duo_item->racl.user_obj);
  846. SIVAL(buf, 4, duo_item->racl.group_obj);
  847. SIVAL(buf, 8, duo_item->racl.mask_obj);
  848. SIVAL(buf, 12, duo_item->racl.other_obj);
  849. if (cnt) {
  850. char *bp = buf + 4*4;
  851. id_access *ida = duo_item->racl.names.idas;
  852. for ( ; cnt--; ida++, bp += 4+4) {
  853. SIVAL(bp, 0, ida->id);
  854. SIVAL(bp, 4, ida->access);
  855. }
  856. }
  857. rc = set_xattr_acl(fname, type == SMB_ACL_TYPE_ACCESS, buf, len);
  858. free(buf);
  859. return rc;
  860. #endif
  861. } else {
  862. mode_t cur_mode = sxp->st.st_mode;
  863. if (!duo_item->sacl
  864. && !pack_smb_acl(&duo_item->sacl, &duo_item->racl))
  865. return -1;
  866. #ifdef HAVE_OSX_ACLS
  867. mode = 0; /* eliminate compiler warning */
  868. #else
  869. if (type == SMB_ACL_TYPE_ACCESS) {
  870. cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl,
  871. cur_mode, mode);
  872. if (cur_mode == (mode_t)-1)
  873. return 0;
  874. }
  875. #endif
  876. if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {
  877. rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_set_file(%s, %s)",
  878. fname, str_acl_type(type));
  879. return -1;
  880. }
  881. if (type == SMB_ACL_TYPE_ACCESS)
  882. sxp->st.st_mode = cur_mode;
  883. }
  884. return 0;
  885. }
  886. /* Given a fname, this sets extended access ACL entries, the default ACL (for a
  887. * dir), and the regular mode bits on the file. Call this with fname set to
  888. * NULL to just check if the ACL is different.
  889. *
  890. * If the ACL operation has a side-effect of changing the file's mode, the
  891. * sxp->st.st_mode value will be changed to match.
  892. *
  893. * Returns 0 for an unchanged ACL, 1 for changed, -1 for failed. */
  894. int set_acl(const char *fname, const struct file_struct *file, stat_x *sxp, mode_t new_mode)
  895. {
  896. int changed = 0;
  897. int32 ndx;
  898. BOOL eq;
  899. if (!dry_run && (read_only || list_only)) {
  900. errno = EROFS;
  901. return -1;
  902. }
  903. ndx = F_ACL(file);
  904. if (ndx >= 0 && (size_t)ndx < access_acl_list.count) {
  905. acl_duo *duo_item = access_acl_list.items;
  906. duo_item += ndx;
  907. eq = sxp->acc_acl
  908. && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, new_mode);
  909. if (!eq) {
  910. changed = 1;
  911. if (!dry_run && fname
  912. && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_ACCESS,
  913. sxp, new_mode) < 0)
  914. return -1;
  915. }
  916. }
  917. if (!S_ISDIR(new_mode))
  918. return changed;
  919. ndx = F_DIR_DEFACL(file);
  920. if (ndx >= 0 && (size_t)ndx < default_acl_list.count) {
  921. acl_duo *duo_item = default_acl_list.items;
  922. duo_item += ndx;
  923. eq = sxp->def_acl && rsync_acl_equal(sxp->def_acl, &duo_item->racl);
  924. if (!eq) {
  925. changed = 1;
  926. if (!dry_run && fname
  927. && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_DEFAULT,
  928. sxp, new_mode) < 0)
  929. return -1;
  930. }
  931. }
  932. return changed;
  933. }
  934. /* Non-incremental recursion needs to convert all the received IDs.
  935. * This is done in a single pass after receiving the whole file-list. */
  936. static void match_racl_ids(const item_list *racl_list)
  937. {
  938. int list_cnt, name_cnt;
  939. acl_duo *duo_item = racl_list->items;
  940. for (list_cnt = racl_list->count; list_cnt--; duo_item++) {
  941. ida_entries *idal = &duo_item->racl.names;
  942. id_access *ida = idal->idas;
  943. for (name_cnt = idal->count; name_cnt--; ida++) {
  944. if (ida->access & NAME_IS_USER)
  945. ida->id = match_uid(ida->id);
  946. else
  947. ida->id = match_gid(ida->id, NULL);
  948. }
  949. }
  950. }
  951. void match_acl_ids(void)
  952. {
  953. match_racl_ids(&access_acl_list);
  954. match_racl_ids(&default_acl_list);
  955. }
  956. /* This is used by dest_mode(). */
  957. int default_perms_for_dir(const char *dir)
  958. {
  959. rsync_acl racl;
  960. SMB_ACL_T sacl;
  961. BOOL ok;
  962. int perms;
  963. if (dir == NULL)
  964. dir = ".";
  965. perms = ACCESSPERMS & ~orig_umask;
  966. /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
  967. sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
  968. if (sacl == NULL) {
  969. /* Couldn't get an ACL. Darn. */
  970. switch (errno) {
  971. case EINVAL:
  972. /* If SMB_ACL_TYPE_DEFAULT isn't valid, then the ACLs must be non-POSIX. */
  973. break;
  974. #ifdef ENOTSUP
  975. case ENOTSUP:
  976. #endif
  977. case ENOSYS:
  978. /* No ACLs are available. */
  979. break;
  980. case ENOENT:
  981. if (dry_run) {
  982. /* We're doing a dry run, so the containing directory
  983. * wasn't actually created. Don't worry about it. */
  984. break;
  985. }
  986. /* Otherwise fall through. */
  987. default:
  988. rprintf(FWARNING,
  989. "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
  990. dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
  991. }
  992. return perms;
  993. }
  994. /* Convert it. */
  995. racl = empty_rsync_acl;
  996. ok = unpack_smb_acl(sacl, &racl);
  997. sys_acl_free_acl(sacl);
  998. if (!ok) {
  999. rprintf(FWARNING, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
  1000. return perms;
  1001. }
  1002. /* Apply the permission-bit entries of the default ACL, if any. */
  1003. if (racl.user_obj != NO_ENTRY) {
  1004. perms = rsync_acl_get_perms(&racl);
  1005. if (verbose > 2)
  1006. rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
  1007. }
  1008. rsync_acl_free(&racl);
  1009. return perms;
  1010. }
  1011. #endif /* SUPPORT_ACLS */