archive_write_disk_acl.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*-
  2. * Copyright (c) 2003-2010 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer
  10. * in this position and unchanged.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "archive_platform.h"
  27. __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_disk.c 201159 2009-12-29 05:35:40Z kientzle $");
  28. #ifdef HAVE_SYS_TYPES_H
  29. #include <sys/types.h>
  30. #endif
  31. #ifdef HAVE_SYS_ACL_H
  32. #define _ACL_PRIVATE /* For debugging */
  33. #include <sys/acl.h>
  34. #endif
  35. #ifdef HAVE_ERRNO_H
  36. #include <errno.h>
  37. #endif
  38. #include "archive.h"
  39. #include "archive_entry.h"
  40. #include "archive_acl_private.h"
  41. #include "archive_write_disk_private.h"
  42. #ifndef HAVE_POSIX_ACL
  43. /* Default empty function body to satisfy mainline code. */
  44. int
  45. archive_write_disk_set_acls(struct archive *a, int fd, const char *name,
  46. struct archive_acl *abstract_acl)
  47. {
  48. (void)a; /* UNUSED */
  49. (void)fd; /* UNUSED */
  50. (void)name; /* UNUSED */
  51. (void)abstract_acl; /* UNUSED */
  52. return (ARCHIVE_OK);
  53. }
  54. #else
  55. static int set_acl(struct archive *, int fd, const char *,
  56. struct archive_acl *,
  57. acl_type_t, int archive_entry_acl_type, const char *tn);
  58. /*
  59. * XXX TODO: What about ACL types other than ACCESS and DEFAULT?
  60. */
  61. int
  62. archive_write_disk_set_acls(struct archive *a, int fd, const char *name,
  63. struct archive_acl *abstract_acl)
  64. {
  65. int ret;
  66. if (archive_acl_count(abstract_acl, ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) > 0) {
  67. ret = set_acl(a, fd, name, abstract_acl, ACL_TYPE_ACCESS,
  68. ARCHIVE_ENTRY_ACL_TYPE_ACCESS, "access");
  69. if (ret != ARCHIVE_OK)
  70. return (ret);
  71. ret = set_acl(a, fd, name, abstract_acl, ACL_TYPE_DEFAULT,
  72. ARCHIVE_ENTRY_ACL_TYPE_DEFAULT, "default");
  73. return (ret);
  74. #ifdef ACL_TYPE_NFS4
  75. } else if (archive_acl_count(abstract_acl, ARCHIVE_ENTRY_ACL_TYPE_NFS4) > 0) {
  76. ret = set_acl(a, fd, name, abstract_acl, ACL_TYPE_NFS4,
  77. ARCHIVE_ENTRY_ACL_TYPE_NFS4, "nfs4");
  78. return (ret);
  79. #endif
  80. } else
  81. return ARCHIVE_OK;
  82. }
  83. static struct {
  84. int archive_perm;
  85. int platform_perm;
  86. } acl_perm_map[] = {
  87. {ARCHIVE_ENTRY_ACL_EXECUTE, ACL_EXECUTE},
  88. {ARCHIVE_ENTRY_ACL_WRITE, ACL_WRITE},
  89. {ARCHIVE_ENTRY_ACL_READ, ACL_READ},
  90. #ifdef ACL_TYPE_NFS4
  91. {ARCHIVE_ENTRY_ACL_READ_DATA, ACL_READ_DATA},
  92. {ARCHIVE_ENTRY_ACL_LIST_DIRECTORY, ACL_LIST_DIRECTORY},
  93. {ARCHIVE_ENTRY_ACL_WRITE_DATA, ACL_WRITE_DATA},
  94. {ARCHIVE_ENTRY_ACL_ADD_FILE, ACL_ADD_FILE},
  95. {ARCHIVE_ENTRY_ACL_APPEND_DATA, ACL_APPEND_DATA},
  96. {ARCHIVE_ENTRY_ACL_ADD_SUBDIRECTORY, ACL_ADD_SUBDIRECTORY},
  97. {ARCHIVE_ENTRY_ACL_READ_NAMED_ATTRS, ACL_READ_NAMED_ATTRS},
  98. {ARCHIVE_ENTRY_ACL_WRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS},
  99. {ARCHIVE_ENTRY_ACL_DELETE_CHILD, ACL_DELETE_CHILD},
  100. {ARCHIVE_ENTRY_ACL_READ_ATTRIBUTES, ACL_READ_ATTRIBUTES},
  101. {ARCHIVE_ENTRY_ACL_WRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES},
  102. {ARCHIVE_ENTRY_ACL_DELETE, ACL_DELETE},
  103. {ARCHIVE_ENTRY_ACL_READ_ACL, ACL_READ_ACL},
  104. {ARCHIVE_ENTRY_ACL_WRITE_ACL, ACL_WRITE_ACL},
  105. {ARCHIVE_ENTRY_ACL_WRITE_OWNER, ACL_WRITE_OWNER},
  106. {ARCHIVE_ENTRY_ACL_SYNCHRONIZE, ACL_SYNCHRONIZE}
  107. #endif
  108. };
  109. #ifdef ACL_TYPE_NFS4
  110. static struct {
  111. int archive_inherit;
  112. int platform_inherit;
  113. } acl_inherit_map[] = {
  114. {ARCHIVE_ENTRY_ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_FILE_INHERIT},
  115. {ARCHIVE_ENTRY_ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT},
  116. {ARCHIVE_ENTRY_ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_NO_PROPAGATE_INHERIT},
  117. {ARCHIVE_ENTRY_ACL_ENTRY_INHERIT_ONLY, ACL_ENTRY_INHERIT_ONLY}
  118. };
  119. #endif
  120. static int
  121. set_acl(struct archive *a, int fd, const char *name,
  122. struct archive_acl *abstract_acl,
  123. acl_type_t acl_type, int ae_requested_type, const char *tname)
  124. {
  125. acl_t acl;
  126. acl_entry_t acl_entry;
  127. acl_permset_t acl_permset;
  128. #ifdef ACL_TYPE_NFS4
  129. acl_flagset_t acl_flagset;
  130. #endif
  131. int ret;
  132. int ae_type, ae_permset, ae_tag, ae_id;
  133. uid_t ae_uid;
  134. gid_t ae_gid;
  135. const char *ae_name;
  136. int entries;
  137. int i;
  138. ret = ARCHIVE_OK;
  139. entries = archive_acl_reset(abstract_acl, ae_requested_type);
  140. if (entries == 0)
  141. return (ARCHIVE_OK);
  142. acl = acl_init(entries);
  143. while (archive_acl_next(a, abstract_acl, ae_requested_type, &ae_type,
  144. &ae_permset, &ae_tag, &ae_id, &ae_name) == ARCHIVE_OK) {
  145. acl_create_entry(&acl, &acl_entry);
  146. switch (ae_tag) {
  147. case ARCHIVE_ENTRY_ACL_USER:
  148. acl_set_tag_type(acl_entry, ACL_USER);
  149. ae_uid = archive_write_disk_uid(a, ae_name, ae_id);
  150. acl_set_qualifier(acl_entry, &ae_uid);
  151. break;
  152. case ARCHIVE_ENTRY_ACL_GROUP:
  153. acl_set_tag_type(acl_entry, ACL_GROUP);
  154. ae_gid = archive_write_disk_gid(a, ae_name, ae_id);
  155. acl_set_qualifier(acl_entry, &ae_gid);
  156. break;
  157. case ARCHIVE_ENTRY_ACL_USER_OBJ:
  158. acl_set_tag_type(acl_entry, ACL_USER_OBJ);
  159. break;
  160. case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
  161. acl_set_tag_type(acl_entry, ACL_GROUP_OBJ);
  162. break;
  163. case ARCHIVE_ENTRY_ACL_MASK:
  164. acl_set_tag_type(acl_entry, ACL_MASK);
  165. break;
  166. case ARCHIVE_ENTRY_ACL_OTHER:
  167. acl_set_tag_type(acl_entry, ACL_OTHER);
  168. break;
  169. #ifdef ACL_TYPE_NFS4
  170. case ARCHIVE_ENTRY_ACL_EVERYONE:
  171. acl_set_tag_type(acl_entry, ACL_EVERYONE);
  172. break;
  173. #endif
  174. default:
  175. /* XXX */
  176. break;
  177. }
  178. #ifdef ACL_TYPE_NFS4
  179. switch (ae_type) {
  180. case ARCHIVE_ENTRY_ACL_TYPE_ALLOW:
  181. acl_set_entry_type_np(acl_entry, ACL_ENTRY_TYPE_ALLOW);
  182. break;
  183. case ARCHIVE_ENTRY_ACL_TYPE_DENY:
  184. acl_set_entry_type_np(acl_entry, ACL_ENTRY_TYPE_DENY);
  185. break;
  186. case ARCHIVE_ENTRY_ACL_TYPE_AUDIT:
  187. acl_set_entry_type_np(acl_entry, ACL_ENTRY_TYPE_AUDIT);
  188. break;
  189. case ARCHIVE_ENTRY_ACL_TYPE_ALARM:
  190. acl_set_entry_type_np(acl_entry, ACL_ENTRY_TYPE_ALARM);
  191. break;
  192. case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
  193. case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
  194. // These don't translate directly into the system ACL.
  195. break;
  196. default:
  197. // XXX error handling here.
  198. break;
  199. }
  200. #endif
  201. acl_get_permset(acl_entry, &acl_permset);
  202. acl_clear_perms(acl_permset);
  203. for (i = 0; i < (int)(sizeof(acl_perm_map) / sizeof(acl_perm_map[0])); ++i) {
  204. if (ae_permset & acl_perm_map[i].archive_perm)
  205. acl_add_perm(acl_permset,
  206. acl_perm_map[i].platform_perm);
  207. }
  208. #ifdef ACL_TYPE_NFS4
  209. acl_get_flagset_np(acl_entry, &acl_flagset);
  210. acl_clear_flags_np(acl_flagset);
  211. for (i = 0; i < (int)(sizeof(acl_inherit_map) / sizeof(acl_inherit_map[0])); ++i) {
  212. if (ae_permset & acl_inherit_map[i].archive_inherit)
  213. acl_add_flag_np(acl_flagset,
  214. acl_inherit_map[i].platform_inherit);
  215. }
  216. #endif
  217. }
  218. /* Try restoring the ACL through 'fd' if we can. */
  219. #if HAVE_ACL_SET_FD
  220. if (fd >= 0 && acl_type == ACL_TYPE_ACCESS && acl_set_fd(fd, acl) == 0)
  221. ret = ARCHIVE_OK;
  222. else
  223. #else
  224. #if HAVE_ACL_SET_FD_NP
  225. if (fd >= 0 && acl_set_fd_np(fd, acl, acl_type) == 0)
  226. ret = ARCHIVE_OK;
  227. else
  228. #endif
  229. #endif
  230. #if HAVE_ACL_SET_LINK_NP
  231. if (acl_set_link_np(name, acl_type, acl) != 0) {
  232. archive_set_error(a, errno, "Failed to set %s acl", tname);
  233. ret = ARCHIVE_WARN;
  234. }
  235. #else
  236. /* TODO: Skip this if 'name' is a symlink. */
  237. if (acl_set_file(name, acl_type, acl) != 0) {
  238. archive_set_error(a, errno, "Failed to set %s acl", tname);
  239. ret = ARCHIVE_WARN;
  240. }
  241. #endif
  242. acl_free(acl);
  243. return (ret);
  244. }
  245. #endif