archive_disk_acl_linux.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*-
  2. * Copyright (c) 2003-2009 Tim Kientzle
  3. * Copyright (c) 2010-2012 Michihiro NAKAJIMA
  4. * Copyright (c) 2017 Martin Matuska
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "archive_platform.h"
  28. #if ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_LIBRICHACL
  29. #ifdef HAVE_ERRNO_H
  30. #include <errno.h>
  31. #endif
  32. #ifdef HAVE_FCNTL_H
  33. #include <fcntl.h>
  34. #endif
  35. #if HAVE_ACL_LIBACL_H
  36. #include <acl/libacl.h>
  37. #endif
  38. #ifdef HAVE_SYS_ACL_H
  39. #include <sys/acl.h>
  40. #endif
  41. #ifdef HAVE_SYS_RICHACL_H
  42. #include <sys/richacl.h>
  43. #endif
  44. #include "archive_entry.h"
  45. #include "archive_private.h"
  46. #include "archive_read_disk_private.h"
  47. #include "archive_write_disk_private.h"
  48. typedef struct {
  49. const int a_perm; /* Libarchive permission or flag */
  50. const int p_perm; /* Platform permission or flag */
  51. } acl_perm_map_t;
  52. #if ARCHIVE_ACL_LIBACL
  53. static const acl_perm_map_t acl_posix_perm_map[] = {
  54. {ARCHIVE_ENTRY_ACL_EXECUTE, ACL_EXECUTE},
  55. {ARCHIVE_ENTRY_ACL_WRITE, ACL_WRITE},
  56. {ARCHIVE_ENTRY_ACL_READ, ACL_READ},
  57. };
  58. static const int acl_posix_perm_map_size =
  59. (int)(sizeof(acl_posix_perm_map)/sizeof(acl_posix_perm_map[0]));
  60. #endif /* ARCHIVE_ACL_LIBACL */
  61. #if ARCHIVE_ACL_LIBRICHACL
  62. static const acl_perm_map_t acl_nfs4_perm_map[] = {
  63. {ARCHIVE_ENTRY_ACL_EXECUTE, RICHACE_EXECUTE},
  64. {ARCHIVE_ENTRY_ACL_READ_DATA, RICHACE_READ_DATA},
  65. {ARCHIVE_ENTRY_ACL_LIST_DIRECTORY, RICHACE_LIST_DIRECTORY},
  66. {ARCHIVE_ENTRY_ACL_WRITE_DATA, RICHACE_WRITE_DATA},
  67. {ARCHIVE_ENTRY_ACL_ADD_FILE, RICHACE_ADD_FILE},
  68. {ARCHIVE_ENTRY_ACL_APPEND_DATA, RICHACE_APPEND_DATA},
  69. {ARCHIVE_ENTRY_ACL_ADD_SUBDIRECTORY, RICHACE_ADD_SUBDIRECTORY},
  70. {ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS, RICHACE_READ_NAMED_ATTRS},
  71. {ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS, RICHACE_WRITE_NAMED_ATTRS},
  72. {ARCHIVE_ENTRY_ACL_DELETE_CHILD, RICHACE_DELETE_CHILD},
  73. {ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES, RICHACE_READ_ATTRIBUTES},
  74. {ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES, RICHACE_WRITE_ATTRIBUTES},
  75. {ARCHIVE_ENTRY_ACL_DELETE, RICHACE_DELETE},
  76. {ARCHIVE_ENTRY_ACL_READ_ACL, RICHACE_READ_ACL},
  77. {ARCHIVE_ENTRY_ACL_WRITE_ACL, RICHACE_WRITE_ACL},
  78. {ARCHIVE_ENTRY_ACL_WRITE_OWNER, RICHACE_WRITE_OWNER},
  79. {ARCHIVE_ENTRY_ACL_SYNCHRONIZE, RICHACE_SYNCHRONIZE}
  80. };
  81. static const int acl_nfs4_perm_map_size =
  82. (int)(sizeof(acl_nfs4_perm_map)/sizeof(acl_nfs4_perm_map[0]));
  83. static const acl_perm_map_t acl_nfs4_flag_map[] = {
  84. {ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT, RICHACE_FILE_INHERIT_ACE},
  85. {ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT, RICHACE_DIRECTORY_INHERIT_ACE},
  86. {ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT, RICHACE_NO_PROPAGATE_INHERIT_ACE},
  87. {ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY, RICHACE_INHERIT_ONLY_ACE},
  88. {ARCHIVE_ENTRY_ACL_ENTRY_INHERITED, RICHACE_INHERITED_ACE}
  89. };
  90. static const int acl_nfs4_flag_map_size =
  91. (int)(sizeof(acl_nfs4_flag_map)/sizeof(acl_nfs4_flag_map[0]));
  92. #endif /* ARCHIVE_ACL_LIBRICHACL */
  93. #if ARCHIVE_ACL_LIBACL
  94. /*
  95. * Translate POSIX.1e ACLs into libarchive internal structure
  96. */
  97. static int
  98. translate_acl(struct archive_read_disk *a,
  99. struct archive_entry *entry, acl_t acl, int default_entry_acl_type)
  100. {
  101. acl_tag_t acl_tag;
  102. acl_entry_t acl_entry;
  103. acl_permset_t acl_permset;
  104. int i, entry_acl_type;
  105. int r, s, ae_id, ae_tag, ae_perm;
  106. void *q;
  107. const char *ae_name;
  108. s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
  109. if (s == -1) {
  110. archive_set_error(&a->archive, errno,
  111. "Failed to get first ACL entry");
  112. return (ARCHIVE_WARN);
  113. }
  114. while (s == 1) {
  115. ae_id = -1;
  116. ae_name = NULL;
  117. ae_perm = 0;
  118. if (acl_get_tag_type(acl_entry, &acl_tag) != 0) {
  119. archive_set_error(&a->archive, errno,
  120. "Failed to get ACL tag type");
  121. return (ARCHIVE_WARN);
  122. }
  123. switch (acl_tag) {
  124. case ACL_USER:
  125. q = acl_get_qualifier(acl_entry);
  126. if (q != NULL) {
  127. ae_id = (int)*(uid_t *)q;
  128. acl_free(q);
  129. ae_name = archive_read_disk_uname(&a->archive,
  130. ae_id);
  131. }
  132. ae_tag = ARCHIVE_ENTRY_ACL_USER;
  133. break;
  134. case ACL_GROUP:
  135. q = acl_get_qualifier(acl_entry);
  136. if (q != NULL) {
  137. ae_id = (int)*(gid_t *)q;
  138. acl_free(q);
  139. ae_name = archive_read_disk_gname(&a->archive,
  140. ae_id);
  141. }
  142. ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
  143. break;
  144. case ACL_MASK:
  145. ae_tag = ARCHIVE_ENTRY_ACL_MASK;
  146. break;
  147. case ACL_USER_OBJ:
  148. ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
  149. break;
  150. case ACL_GROUP_OBJ:
  151. ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
  152. break;
  153. case ACL_OTHER:
  154. ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
  155. break;
  156. default:
  157. /* Skip types that libarchive can't support. */
  158. s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
  159. continue;
  160. }
  161. // XXX acl_type maps to allow/deny/audit/YYYY bits
  162. entry_acl_type = default_entry_acl_type;
  163. if (acl_get_permset(acl_entry, &acl_permset) != 0) {
  164. archive_set_error(&a->archive, errno,
  165. "Failed to get ACL permission set");
  166. return (ARCHIVE_WARN);
  167. }
  168. for (i = 0; i < acl_posix_perm_map_size; ++i) {
  169. r = acl_get_perm(acl_permset,
  170. acl_posix_perm_map[i].p_perm);
  171. if (r == -1) {
  172. archive_set_error(&a->archive, errno,
  173. "Failed to check permission in an ACL "
  174. "permission set");
  175. return (ARCHIVE_WARN);
  176. } else if (r)
  177. ae_perm |= acl_posix_perm_map[i].a_perm;
  178. }
  179. archive_entry_acl_add_entry(entry, entry_acl_type,
  180. ae_perm, ae_tag,
  181. ae_id, ae_name);
  182. s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
  183. if (s == -1) {
  184. archive_set_error(&a->archive, errno,
  185. "Failed to get next ACL entry");
  186. return (ARCHIVE_WARN);
  187. }
  188. }
  189. return (ARCHIVE_OK);
  190. }
  191. #endif /* ARCHIVE_ACL_LIBACL */
  192. #if ARCHIVE_ACL_LIBRICHACL
  193. /*
  194. * Translate RichACL into libarchive internal ACL
  195. */
  196. static int
  197. translate_richacl(struct archive_read_disk *a, struct archive_entry *entry,
  198. struct richacl *richacl)
  199. {
  200. int ae_id, ae_tag, ae_perm;
  201. int entry_acl_type, i;
  202. const char *ae_name;
  203. struct richace *richace;
  204. richacl_for_each_entry(richace, richacl) {
  205. ae_name = NULL;
  206. ae_tag = 0;
  207. ae_perm = 0;
  208. ae_id = -1;
  209. switch (richace->e_type) {
  210. case RICHACE_ACCESS_ALLOWED_ACE_TYPE:
  211. entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_ALLOW;
  212. break;
  213. case RICHACE_ACCESS_DENIED_ACE_TYPE:
  214. entry_acl_type = ARCHIVE_ENTRY_ACL_TYPE_DENY;
  215. break;
  216. default: /* Unknown entry type, skip */
  217. continue;
  218. }
  219. /* Unsupported */
  220. if (richace->e_flags & RICHACE_UNMAPPED_WHO)
  221. continue;
  222. if (richace->e_flags & RICHACE_SPECIAL_WHO) {
  223. switch (richace->e_id) {
  224. case RICHACE_OWNER_SPECIAL_ID:
  225. ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
  226. break;
  227. case RICHACE_GROUP_SPECIAL_ID:
  228. ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
  229. break;
  230. case RICHACE_EVERYONE_SPECIAL_ID:
  231. ae_tag = ARCHIVE_ENTRY_ACL_EVERYONE;
  232. break;
  233. default: /* Unknown special ID type */
  234. continue;
  235. }
  236. } else {
  237. ae_id = richace->e_id;
  238. if (richace->e_flags & RICHACE_IDENTIFIER_GROUP) {
  239. ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
  240. ae_name = archive_read_disk_gname(&a->archive,
  241. (gid_t)(richace->e_id));
  242. } else {
  243. ae_tag = ARCHIVE_ENTRY_ACL_USER;
  244. ae_name = archive_read_disk_uname(&a->archive,
  245. (uid_t)(richace->e_id));
  246. }
  247. }
  248. for (i = 0; i < acl_nfs4_flag_map_size; ++i) {
  249. if ((richace->e_flags &
  250. acl_nfs4_flag_map[i].p_perm) != 0)
  251. ae_perm |= acl_nfs4_flag_map[i].a_perm;
  252. }
  253. for (i = 0; i < acl_nfs4_perm_map_size; ++i) {
  254. if ((richace->e_mask &
  255. acl_nfs4_perm_map[i].p_perm) != 0)
  256. ae_perm |=
  257. acl_nfs4_perm_map[i].a_perm;
  258. }
  259. archive_entry_acl_add_entry(entry, entry_acl_type,
  260. ae_perm, ae_tag, ae_id, ae_name);
  261. }
  262. return (ARCHIVE_OK);
  263. }
  264. #endif /* ARCHIVE_ACL_LIBRICHACL */
  265. #if ARCHIVE_ACL_LIBRICHACL
  266. static int
  267. _richacl_mode_to_mask(short mode)
  268. {
  269. int mask = 0;
  270. if (mode & S_IROTH)
  271. mask |= RICHACE_POSIX_MODE_READ;
  272. if (mode & S_IWOTH)
  273. mask |= RICHACE_POSIX_MODE_WRITE;
  274. if (mode & S_IXOTH)
  275. mask |= RICHACE_POSIX_MODE_EXEC;
  276. return (mask);
  277. }
  278. static void
  279. _richacl_mode_to_masks(struct richacl *richacl, __LA_MODE_T mode)
  280. {
  281. richacl->a_owner_mask = _richacl_mode_to_mask((mode & 0700) >> 6);
  282. richacl->a_group_mask = _richacl_mode_to_mask((mode & 0070) >> 3);
  283. richacl->a_other_mask = _richacl_mode_to_mask(mode & 0007);
  284. }
  285. #endif /* ARCHIVE_ACL_LIBRICHACL */
  286. #if ARCHIVE_ACL_LIBRICHACL
  287. static int
  288. set_richacl(struct archive *a, int fd, const char *name,
  289. struct archive_acl *abstract_acl, __LA_MODE_T mode,
  290. int ae_requested_type, const char *tname)
  291. {
  292. int ae_type, ae_permset, ae_tag, ae_id;
  293. uid_t ae_uid;
  294. gid_t ae_gid;
  295. const char *ae_name;
  296. int entries;
  297. int i;
  298. int ret;
  299. int e = 0;
  300. struct richacl *richacl = NULL;
  301. struct richace *richace;
  302. ret = ARCHIVE_OK;
  303. entries = archive_acl_reset(abstract_acl, ae_requested_type);
  304. if (entries == 0)
  305. return (ARCHIVE_OK);
  306. if (ae_requested_type != ARCHIVE_ENTRY_ACL_TYPE_NFS4) {
  307. errno = ENOENT;
  308. archive_set_error(a, errno, "Unsupported ACL type");
  309. return (ARCHIVE_FAILED);
  310. }
  311. richacl = richacl_alloc(entries);
  312. if (richacl == NULL) {
  313. archive_set_error(a, errno,
  314. "Failed to initialize RichACL working storage");
  315. return (ARCHIVE_FAILED);
  316. }
  317. e = 0;
  318. while (archive_acl_next(a, abstract_acl, ae_requested_type, &ae_type,
  319. &ae_permset, &ae_tag, &ae_id, &ae_name) == ARCHIVE_OK) {
  320. richace = &(richacl->a_entries[e]);
  321. richace->e_flags = 0;
  322. richace->e_mask = 0;
  323. switch (ae_tag) {
  324. case ARCHIVE_ENTRY_ACL_USER:
  325. ae_uid = archive_write_disk_uid(a, ae_name, ae_id);
  326. richace->e_id = ae_uid;
  327. break;
  328. case ARCHIVE_ENTRY_ACL_GROUP:
  329. ae_gid = archive_write_disk_gid(a, ae_name, ae_id);
  330. richace->e_id = ae_gid;
  331. richace->e_flags |= RICHACE_IDENTIFIER_GROUP;
  332. break;
  333. case ARCHIVE_ENTRY_ACL_USER_OBJ:
  334. richace->e_flags |= RICHACE_SPECIAL_WHO;
  335. richace->e_id = RICHACE_OWNER_SPECIAL_ID;
  336. break;
  337. case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
  338. richace->e_flags |= RICHACE_SPECIAL_WHO;
  339. richace->e_id = RICHACE_GROUP_SPECIAL_ID;
  340. break;
  341. case ARCHIVE_ENTRY_ACL_EVERYONE:
  342. richace->e_flags |= RICHACE_SPECIAL_WHO;
  343. richace->e_id = RICHACE_EVERYONE_SPECIAL_ID;
  344. break;
  345. default:
  346. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  347. "Unsupported ACL tag");
  348. ret = ARCHIVE_FAILED;
  349. goto exit_free;
  350. }
  351. switch (ae_type) {
  352. case ARCHIVE_ENTRY_ACL_TYPE_ALLOW:
  353. richace->e_type =
  354. RICHACE_ACCESS_ALLOWED_ACE_TYPE;
  355. break;
  356. case ARCHIVE_ENTRY_ACL_TYPE_DENY:
  357. richace->e_type =
  358. RICHACE_ACCESS_DENIED_ACE_TYPE;
  359. break;
  360. case ARCHIVE_ENTRY_ACL_TYPE_AUDIT:
  361. case ARCHIVE_ENTRY_ACL_TYPE_ALARM:
  362. break;
  363. default:
  364. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  365. "Unsupported ACL entry type");
  366. ret = ARCHIVE_FAILED;
  367. goto exit_free;
  368. }
  369. for (i = 0; i < acl_nfs4_perm_map_size; ++i) {
  370. if (ae_permset & acl_nfs4_perm_map[i].a_perm)
  371. richace->e_mask |= acl_nfs4_perm_map[i].p_perm;
  372. }
  373. for (i = 0; i < acl_nfs4_flag_map_size; ++i) {
  374. if (ae_permset &
  375. acl_nfs4_flag_map[i].a_perm)
  376. richace->e_flags |= acl_nfs4_flag_map[i].p_perm;
  377. }
  378. e++;
  379. }
  380. /* Fill RichACL masks */
  381. _richacl_mode_to_masks(richacl, mode);
  382. if (fd >= 0) {
  383. if (richacl_set_fd(fd, richacl) == 0)
  384. ret = ARCHIVE_OK;
  385. else {
  386. if (errno == EOPNOTSUPP) {
  387. /* Filesystem doesn't support ACLs */
  388. ret = ARCHIVE_OK;
  389. } else {
  390. archive_set_error(a, errno,
  391. "Failed to set richacl on fd: %s", tname);
  392. ret = ARCHIVE_WARN;
  393. }
  394. }
  395. } else if (richacl_set_file(name, richacl) != 0) {
  396. if (errno == EOPNOTSUPP) {
  397. /* Filesystem doesn't support ACLs */
  398. ret = ARCHIVE_OK;
  399. } else {
  400. archive_set_error(a, errno, "Failed to set richacl: %s",
  401. tname);
  402. ret = ARCHIVE_WARN;
  403. }
  404. }
  405. exit_free:
  406. richacl_free(richacl);
  407. return (ret);
  408. }
  409. #endif /* ARCHIVE_ACL_RICHACL */
  410. #if ARCHIVE_ACL_LIBACL
  411. static int
  412. set_acl(struct archive *a, int fd, const char *name,
  413. struct archive_acl *abstract_acl,
  414. int ae_requested_type, const char *tname)
  415. {
  416. int acl_type = 0;
  417. int ae_type, ae_permset, ae_tag, ae_id;
  418. uid_t ae_uid;
  419. gid_t ae_gid;
  420. const char *ae_name;
  421. int entries;
  422. int i;
  423. int ret;
  424. acl_t acl = NULL;
  425. acl_entry_t acl_entry;
  426. acl_permset_t acl_permset;
  427. ret = ARCHIVE_OK;
  428. entries = archive_acl_reset(abstract_acl, ae_requested_type);
  429. if (entries == 0)
  430. return (ARCHIVE_OK);
  431. switch (ae_requested_type) {
  432. case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
  433. acl_type = ACL_TYPE_ACCESS;
  434. break;
  435. case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
  436. acl_type = ACL_TYPE_DEFAULT;
  437. break;
  438. default:
  439. errno = ENOENT;
  440. archive_set_error(a, errno, "Unsupported ACL type");
  441. return (ARCHIVE_FAILED);
  442. }
  443. acl = acl_init(entries);
  444. if (acl == (acl_t)NULL) {
  445. archive_set_error(a, errno,
  446. "Failed to initialize ACL working storage");
  447. return (ARCHIVE_FAILED);
  448. }
  449. while (archive_acl_next(a, abstract_acl, ae_requested_type, &ae_type,
  450. &ae_permset, &ae_tag, &ae_id, &ae_name) == ARCHIVE_OK) {
  451. if (acl_create_entry(&acl, &acl_entry) != 0) {
  452. archive_set_error(a, errno,
  453. "Failed to create a new ACL entry");
  454. ret = ARCHIVE_FAILED;
  455. goto exit_free;
  456. }
  457. switch (ae_tag) {
  458. case ARCHIVE_ENTRY_ACL_USER:
  459. ae_uid = archive_write_disk_uid(a, ae_name, ae_id);
  460. acl_set_tag_type(acl_entry, ACL_USER);
  461. acl_set_qualifier(acl_entry, &ae_uid);
  462. break;
  463. case ARCHIVE_ENTRY_ACL_GROUP:
  464. ae_gid = archive_write_disk_gid(a, ae_name, ae_id);
  465. acl_set_tag_type(acl_entry, ACL_GROUP);
  466. acl_set_qualifier(acl_entry, &ae_gid);
  467. break;
  468. case ARCHIVE_ENTRY_ACL_USER_OBJ:
  469. acl_set_tag_type(acl_entry, ACL_USER_OBJ);
  470. break;
  471. case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
  472. acl_set_tag_type(acl_entry, ACL_GROUP_OBJ);
  473. break;
  474. case ARCHIVE_ENTRY_ACL_MASK:
  475. acl_set_tag_type(acl_entry, ACL_MASK);
  476. break;
  477. case ARCHIVE_ENTRY_ACL_OTHER:
  478. acl_set_tag_type(acl_entry, ACL_OTHER);
  479. break;
  480. default:
  481. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  482. "Unsupported ACL tag");
  483. ret = ARCHIVE_FAILED;
  484. goto exit_free;
  485. }
  486. if (acl_get_permset(acl_entry, &acl_permset) != 0) {
  487. archive_set_error(a, errno,
  488. "Failed to get ACL permission set");
  489. ret = ARCHIVE_FAILED;
  490. goto exit_free;
  491. }
  492. if (acl_clear_perms(acl_permset) != 0) {
  493. archive_set_error(a, errno,
  494. "Failed to clear ACL permissions");
  495. ret = ARCHIVE_FAILED;
  496. goto exit_free;
  497. }
  498. for (i = 0; i < acl_posix_perm_map_size; ++i) {
  499. if (ae_permset & acl_posix_perm_map[i].a_perm) {
  500. if (acl_add_perm(acl_permset,
  501. acl_posix_perm_map[i].p_perm) != 0) {
  502. archive_set_error(a, errno,
  503. "Failed to add ACL permission");
  504. ret = ARCHIVE_FAILED;
  505. goto exit_free;
  506. }
  507. }
  508. }
  509. }
  510. if (fd >= 0 && ae_requested_type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
  511. if (acl_set_fd(fd, acl) == 0)
  512. ret = ARCHIVE_OK;
  513. else {
  514. if (errno == EOPNOTSUPP) {
  515. /* Filesystem doesn't support ACLs */
  516. ret = ARCHIVE_OK;
  517. } else {
  518. archive_set_error(a, errno,
  519. "Failed to set acl on fd: %s", tname);
  520. ret = ARCHIVE_WARN;
  521. }
  522. }
  523. } else if (acl_set_file(name, acl_type, acl) != 0) {
  524. if (errno == EOPNOTSUPP) {
  525. /* Filesystem doesn't support ACLs */
  526. ret = ARCHIVE_OK;
  527. } else {
  528. archive_set_error(a, errno, "Failed to set acl: %s",
  529. tname);
  530. ret = ARCHIVE_WARN;
  531. }
  532. }
  533. exit_free:
  534. acl_free(acl);
  535. return (ret);
  536. }
  537. #endif /* ARCHIVE_ACL_LIBACL */
  538. int
  539. archive_read_disk_entry_setup_acls(struct archive_read_disk *a,
  540. struct archive_entry *entry, int *fd)
  541. {
  542. const char *accpath;
  543. int r;
  544. #if ARCHIVE_ACL_LIBACL
  545. acl_t acl;
  546. #endif
  547. #if ARCHIVE_ACL_LIBRICHACL
  548. struct richacl *richacl;
  549. mode_t mode;
  550. #endif
  551. accpath = NULL;
  552. r = ARCHIVE_OK;
  553. /* For default ACLs we need reachable accpath */
  554. if (*fd < 0 || S_ISDIR(archive_entry_mode(entry))) {
  555. accpath = archive_read_disk_entry_setup_path(a, entry, fd);
  556. if (accpath == NULL)
  557. return (ARCHIVE_WARN);
  558. }
  559. archive_entry_acl_clear(entry);
  560. #if ARCHIVE_ACL_LIBACL
  561. acl = NULL;
  562. #endif
  563. #if ARCHIVE_ACL_LIBRICHACL
  564. richacl = NULL;
  565. #endif
  566. #if ARCHIVE_ACL_LIBRICHACL
  567. /* Try NFSv4 ACL first. */
  568. if (*fd >= 0)
  569. richacl = richacl_get_fd(*fd);
  570. else if ((!a->follow_symlinks)
  571. && (archive_entry_filetype(entry) == AE_IFLNK))
  572. /* We can't get the ACL of a symlink, so we assume it can't
  573. have one */
  574. richacl = NULL;
  575. else
  576. richacl = richacl_get_file(accpath);
  577. /* Ignore "trivial" ACLs that just mirror the file mode. */
  578. if (richacl != NULL) {
  579. mode = archive_entry_mode(entry);
  580. if (richacl_equiv_mode(richacl, &mode) == 0) {
  581. richacl_free(richacl);
  582. richacl = NULL;
  583. return (ARCHIVE_OK);
  584. }
  585. }
  586. if (richacl != NULL) {
  587. r = translate_richacl(a, entry, richacl);
  588. richacl_free(richacl);
  589. richacl = NULL;
  590. if (r != ARCHIVE_OK) {
  591. archive_set_error(&a->archive, errno,
  592. "Couldn't translate NFSv4 ACLs");
  593. }
  594. return (r);
  595. }
  596. #endif /* ARCHIVE_ACL_LIBRICHACL */
  597. #if ARCHIVE_ACL_LIBACL
  598. /* Retrieve access ACL from file. */
  599. if (*fd >= 0)
  600. acl = acl_get_fd(*fd);
  601. else if ((!a->follow_symlinks)
  602. && (archive_entry_filetype(entry) == AE_IFLNK))
  603. /* We can't get the ACL of a symlink, so we assume it can't
  604. have one. */
  605. acl = NULL;
  606. else
  607. acl = acl_get_file(accpath, ACL_TYPE_ACCESS);
  608. if (acl != NULL) {
  609. r = translate_acl(a, entry, acl, ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
  610. acl_free(acl);
  611. acl = NULL;
  612. if (r != ARCHIVE_OK) {
  613. archive_set_error(&a->archive, errno,
  614. "Couldn't translate access ACLs");
  615. return (r);
  616. }
  617. }
  618. /* Only directories can have default ACLs. */
  619. if (S_ISDIR(archive_entry_mode(entry))) {
  620. acl = acl_get_file(accpath, ACL_TYPE_DEFAULT);
  621. if (acl != NULL) {
  622. r = translate_acl(a, entry, acl,
  623. ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
  624. acl_free(acl);
  625. if (r != ARCHIVE_OK) {
  626. archive_set_error(&a->archive, errno,
  627. "Couldn't translate default ACLs");
  628. return (r);
  629. }
  630. }
  631. }
  632. #endif /* ARCHIVE_ACL_LIBACL */
  633. return (r);
  634. }
  635. int
  636. archive_write_disk_set_acls(struct archive *a, int fd, const char *name,
  637. struct archive_acl *abstract_acl, __LA_MODE_T mode)
  638. {
  639. int ret = ARCHIVE_OK;
  640. #if !ARCHIVE_ACL_LIBRICHACL
  641. (void)mode; /* UNUSED */
  642. #endif
  643. #if ARCHIVE_ACL_LIBRICHACL
  644. if ((archive_acl_types(abstract_acl)
  645. & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
  646. ret = set_richacl(a, fd, name, abstract_acl, mode,
  647. ARCHIVE_ENTRY_ACL_TYPE_NFS4, "nfs4");
  648. }
  649. #if ARCHIVE_ACL_LIBACL
  650. else
  651. #endif
  652. #endif /* ARCHIVE_ACL_LIBRICHACL */
  653. #if ARCHIVE_ACL_LIBACL
  654. if ((archive_acl_types(abstract_acl)
  655. & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) != 0) {
  656. if ((archive_acl_types(abstract_acl)
  657. & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
  658. ret = set_acl(a, fd, name, abstract_acl,
  659. ARCHIVE_ENTRY_ACL_TYPE_ACCESS, "access");
  660. if (ret != ARCHIVE_OK)
  661. return (ret);
  662. }
  663. if ((archive_acl_types(abstract_acl)
  664. & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0)
  665. ret = set_acl(a, fd, name, abstract_acl,
  666. ARCHIVE_ENTRY_ACL_TYPE_DEFAULT, "default");
  667. }
  668. #endif /* ARCHIVE_ACL_LIBACL */
  669. return (ret);
  670. }
  671. #endif /* ARCHIVE_ACL_LIBACL || ARCHIVE_ACL_LIBRICHACL */