acls.c 29 KB

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