archive_read_disk_entry_from_file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*-
  2. * Copyright (c) 2003-2009 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. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD$");
  27. #ifdef HAVE_SYS_TYPES_H
  28. /* Mac OSX requires sys/types.h before sys/acl.h. */
  29. #include <sys/types.h>
  30. #endif
  31. #ifdef HAVE_SYS_ACL_H
  32. #include <sys/acl.h>
  33. #endif
  34. #ifdef HAVE_SYS_EXTATTR_H
  35. #include <sys/extattr.h>
  36. #endif
  37. #ifdef HAVE_SYS_PARAM_H
  38. #include <sys/param.h>
  39. #endif
  40. #ifdef HAVE_SYS_STAT_H
  41. #include <sys/stat.h>
  42. #endif
  43. #ifdef HAVE_SYS_XATTR_H
  44. #include <sys/xattr.h>
  45. #endif
  46. #ifdef HAVE_ACL_LIBACL_H
  47. #include <acl/libacl.h>
  48. #endif
  49. #ifdef HAVE_ATTR_XATTR_H
  50. #include <attr/xattr.h>
  51. #endif
  52. #ifdef HAVE_ERRNO_H
  53. #include <errno.h>
  54. #endif
  55. #ifdef HAVE_LIMITS_H
  56. #include <limits.h>
  57. #endif
  58. #ifdef HAVE_WINDOWS_H
  59. #include <windows.h>
  60. #endif
  61. #include "archive.h"
  62. #include "archive_entry.h"
  63. #include "archive_private.h"
  64. #include "archive_read_disk_private.h"
  65. /*
  66. * Linux and FreeBSD plug this obvious hole in POSIX.1e in
  67. * different ways.
  68. */
  69. #if HAVE_ACL_GET_PERM
  70. #define ACL_GET_PERM acl_get_perm
  71. #elif HAVE_ACL_GET_PERM_NP
  72. #define ACL_GET_PERM acl_get_perm_np
  73. #endif
  74. static int setup_acls_posix1e(struct archive_read_disk *,
  75. struct archive_entry *, int fd);
  76. static int setup_xattrs(struct archive_read_disk *,
  77. struct archive_entry *, int fd);
  78. int
  79. archive_read_disk_entry_from_file(struct archive *_a,
  80. struct archive_entry *entry,
  81. int fd, const struct stat *st)
  82. {
  83. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  84. const char *path, *name;
  85. struct stat s;
  86. int initial_fd = fd;
  87. int r, r1;
  88. archive_clear_error(_a);
  89. path = archive_entry_sourcepath(entry);
  90. if (path == NULL)
  91. path = archive_entry_pathname(entry);
  92. #ifdef EXT2_IOC_GETFLAGS
  93. /* Linux requires an extra ioctl to pull the flags. Although
  94. * this is an extra step, it has a nice side-effect: We get an
  95. * open file descriptor which we can use in the subsequent lookups. */
  96. if ((S_ISREG(st->st_mode) || S_ISDIR(st->st_mode))) {
  97. if (fd < 0)
  98. fd = open(pathname, O_RDONLY | O_NONBLOCK | O_BINARY);
  99. if (fd >= 0) {
  100. unsigned long stflags;
  101. int r = ioctl(fd, EXT2_IOC_GETFLAGS, &stflags);
  102. if (r == 0 && stflags != 0)
  103. archive_entry_set_fflags(entry, stflags, 0);
  104. }
  105. }
  106. #endif
  107. if (st == NULL) {
  108. /* TODO: On Windows, use GetFileInfoByHandle() here.
  109. * Using Windows stat() call is badly broken, but
  110. * even the stat() wrapper has problems because
  111. * 'struct stat' is broken on Windows.
  112. */
  113. #if HAVE_FSTAT
  114. if (fd >= 0) {
  115. if (fstat(fd, &s) != 0)
  116. return (ARCHIVE_FATAL);
  117. } else
  118. #endif
  119. #if HAVE_LSTAT
  120. if (!a->follow_symlinks) {
  121. if (lstat(path, &s) != 0)
  122. return (ARCHIVE_FATAL);
  123. } else
  124. #endif
  125. if (stat(path, &s) != 0)
  126. return (ARCHIVE_FATAL);
  127. st = &s;
  128. }
  129. archive_entry_copy_stat(entry, st);
  130. /* Lookup uname/gname */
  131. name = archive_read_disk_uname(_a, archive_entry_uid(entry));
  132. if (name != NULL)
  133. archive_entry_copy_uname(entry, name);
  134. name = archive_read_disk_gname(_a, archive_entry_gid(entry));
  135. if (name != NULL)
  136. archive_entry_copy_gname(entry, name);
  137. #ifdef HAVE_STRUCT_STAT_ST_FLAGS
  138. /* On FreeBSD, we get flags for free with the stat. */
  139. /* TODO: Does this belong in copy_stat()? */
  140. if (st->st_flags != 0)
  141. archive_entry_set_fflags(entry, st->st_flags, 0);
  142. #endif
  143. #ifdef HAVE_READLINK
  144. if (S_ISLNK(st->st_mode)) {
  145. char linkbuffer[PATH_MAX + 1];
  146. int lnklen = readlink(path, linkbuffer, PATH_MAX);
  147. if (lnklen < 0) {
  148. archive_set_error(&a->archive, errno,
  149. "Couldn't read link data");
  150. return (ARCHIVE_WARN);
  151. }
  152. linkbuffer[lnklen] = 0;
  153. archive_entry_set_symlink(entry, linkbuffer);
  154. }
  155. #endif
  156. r = setup_acls_posix1e(a, entry, fd);
  157. r1 = setup_xattrs(a, entry, fd);
  158. if (r1 < r)
  159. r = r1;
  160. /* If we opened the file earlier in this function, close it. */
  161. if (initial_fd != fd)
  162. close(fd);
  163. return (r);
  164. }
  165. #ifdef HAVE_POSIX_ACL
  166. static void setup_acl_posix1e(struct archive_read_disk *a,
  167. struct archive_entry *entry, acl_t acl, int archive_entry_acl_type);
  168. static int
  169. setup_acls_posix1e(struct archive_read_disk *a,
  170. struct archive_entry *entry, int fd)
  171. {
  172. const char *accpath;
  173. acl_t acl;
  174. accpath = archive_entry_sourcepath(entry);
  175. if (accpath == NULL)
  176. accpath = archive_entry_pathname(entry);
  177. archive_entry_acl_clear(entry);
  178. /* Retrieve access ACL from file. */
  179. if (fd >= 0)
  180. acl = acl_get_fd(fd);
  181. #if HAVE_ACL_GET_LINK_NP
  182. else if (!a->follow_symlinks)
  183. acl = acl_get_link_np(accpath, ACL_TYPE_ACCESS);
  184. #endif
  185. else
  186. acl = acl_get_file(accpath, ACL_TYPE_ACCESS);
  187. if (acl != NULL) {
  188. setup_acl_posix1e(a, entry, acl,
  189. ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
  190. acl_free(acl);
  191. }
  192. /* Only directories can have default ACLs. */
  193. if (S_ISDIR(archive_entry_mode(entry))) {
  194. acl = acl_get_file(accpath, ACL_TYPE_DEFAULT);
  195. if (acl != NULL) {
  196. setup_acl_posix1e(a, entry, acl,
  197. ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
  198. acl_free(acl);
  199. }
  200. }
  201. return (ARCHIVE_OK);
  202. }
  203. /*
  204. * Translate POSIX.1e ACL into libarchive internal structure.
  205. */
  206. static void
  207. setup_acl_posix1e(struct archive_read_disk *a,
  208. struct archive_entry *entry, acl_t acl, int archive_entry_acl_type)
  209. {
  210. acl_tag_t acl_tag;
  211. acl_entry_t acl_entry;
  212. acl_permset_t acl_permset;
  213. int s, ae_id, ae_tag, ae_perm;
  214. const char *ae_name;
  215. s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
  216. while (s == 1) {
  217. ae_id = -1;
  218. ae_name = NULL;
  219. acl_get_tag_type(acl_entry, &acl_tag);
  220. if (acl_tag == ACL_USER) {
  221. ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry);
  222. ae_name = archive_read_disk_uname(&a->archive, ae_id);
  223. ae_tag = ARCHIVE_ENTRY_ACL_USER;
  224. } else if (acl_tag == ACL_GROUP) {
  225. ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry);
  226. ae_name = archive_read_disk_gname(&a->archive, ae_id);
  227. ae_tag = ARCHIVE_ENTRY_ACL_GROUP;
  228. } else if (acl_tag == ACL_MASK) {
  229. ae_tag = ARCHIVE_ENTRY_ACL_MASK;
  230. } else if (acl_tag == ACL_USER_OBJ) {
  231. ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
  232. } else if (acl_tag == ACL_GROUP_OBJ) {
  233. ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
  234. } else if (acl_tag == ACL_OTHER) {
  235. ae_tag = ARCHIVE_ENTRY_ACL_OTHER;
  236. } else {
  237. /* Skip types that libarchive can't support. */
  238. continue;
  239. }
  240. acl_get_permset(acl_entry, &acl_permset);
  241. ae_perm = 0;
  242. /*
  243. * acl_get_perm() is spelled differently on different
  244. * platforms; see above.
  245. */
  246. if (ACL_GET_PERM(acl_permset, ACL_EXECUTE))
  247. ae_perm |= ARCHIVE_ENTRY_ACL_EXECUTE;
  248. if (ACL_GET_PERM(acl_permset, ACL_READ))
  249. ae_perm |= ARCHIVE_ENTRY_ACL_READ;
  250. if (ACL_GET_PERM(acl_permset, ACL_WRITE))
  251. ae_perm |= ARCHIVE_ENTRY_ACL_WRITE;
  252. archive_entry_acl_add_entry(entry,
  253. archive_entry_acl_type, ae_perm, ae_tag,
  254. ae_id, ae_name);
  255. s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
  256. }
  257. }
  258. #else
  259. static int
  260. setup_acls_posix1e(struct archive_read_disk *a,
  261. struct archive_entry *entry, int fd)
  262. {
  263. (void)a; /* UNUSED */
  264. (void)entry; /* UNUSED */
  265. (void)fd; /* UNUSED */
  266. return (ARCHIVE_OK);
  267. }
  268. #endif
  269. #if HAVE_LISTXATTR && HAVE_LLISTXATTR && HAVE_GETXATTR && HAVE_LGETXATTR
  270. /*
  271. * Linux extended attribute support.
  272. *
  273. * TODO: By using a stack-allocated buffer for the first
  274. * call to getxattr(), we might be able to avoid the second
  275. * call entirely. We only need the second call if the
  276. * stack-allocated buffer is too small. But a modest buffer
  277. * of 1024 bytes or so will often be big enough. Same applies
  278. * to listxattr().
  279. */
  280. static int
  281. setup_xattr(struct archive_read_disk *a,
  282. struct archive_entry *entry, const char *name, int fd)
  283. {
  284. ssize_t size;
  285. void *value = NULL;
  286. const char *accpath;
  287. (void)fd; /* UNUSED */
  288. accpath = archive_entry_sourcepath(entry);
  289. if (accpath == NULL)
  290. accpath = archive_entry_pathname(entry);
  291. if (!a->follow_symlinks)
  292. size = lgetxattr(accpath, name, NULL, 0);
  293. else
  294. size = getxattr(accpath, name, NULL, 0);
  295. if (size == -1) {
  296. archive_set_error(&a->archive, errno,
  297. "Couldn't query extended attribute");
  298. return (ARCHIVE_WARN);
  299. }
  300. if (size > 0 && (value = malloc(size)) == NULL) {
  301. archive_set_error(&a->archive, errno, "Out of memory");
  302. return (ARCHIVE_FATAL);
  303. }
  304. if (!a->follow_symlinks)
  305. size = lgetxattr(accpath, name, value, size);
  306. else
  307. size = getxattr(accpath, name, value, size);
  308. if (size == -1) {
  309. archive_set_error(&a->archive, errno,
  310. "Couldn't read extended attribute");
  311. return (ARCHIVE_WARN);
  312. }
  313. archive_entry_xattr_add_entry(entry, name, value, size);
  314. free(value);
  315. return (ARCHIVE_OK);
  316. }
  317. static int
  318. setup_xattrs(struct archive_read_disk *a,
  319. struct archive_entry *entry, int fd)
  320. {
  321. char *list, *p;
  322. const char *path;
  323. ssize_t list_size;
  324. path = archive_entry_sourcepath(entry);
  325. if (path == NULL)
  326. path = archive_entry_pathname(entry);
  327. if (!a->follow_symlinks)
  328. list_size = llistxattr(path, NULL, 0);
  329. else
  330. list_size = listxattr(path, NULL, 0);
  331. if (list_size == -1) {
  332. if (errno == ENOTSUP)
  333. return (ARCHIVE_OK);
  334. archive_set_error(&a->archive, errno,
  335. "Couldn't list extended attributes");
  336. return (ARCHIVE_WARN);
  337. }
  338. if (list_size == 0)
  339. return (ARCHIVE_OK);
  340. if ((list = malloc(list_size)) == NULL) {
  341. archive_set_error(&a->archive, errno, "Out of memory");
  342. return (ARCHIVE_FATAL);
  343. }
  344. if (!a->follow_symlinks)
  345. list_size = llistxattr(path, list, list_size);
  346. else
  347. list_size = listxattr(path, list, list_size);
  348. if (list_size == -1) {
  349. archive_set_error(&a->archive, errno,
  350. "Couldn't retrieve extended attributes");
  351. free(list);
  352. return (ARCHIVE_WARN);
  353. }
  354. for (p = list; (p - list) < list_size; p += strlen(p) + 1) {
  355. if (strncmp(p, "system.", 7) == 0 ||
  356. strncmp(p, "xfsroot.", 8) == 0)
  357. continue;
  358. setup_xattr(a, entry, p, fd);
  359. }
  360. free(list);
  361. return (ARCHIVE_OK);
  362. }
  363. #elif HAVE_EXTATTR_GET_FILE && HAVE_EXTATTR_LIST_FILE
  364. /*
  365. * FreeBSD extattr interface.
  366. */
  367. /* TODO: Implement this. Follow the Linux model above, but
  368. * with FreeBSD-specific system calls, of course. Be careful
  369. * to not include the system extattrs that hold ACLs; we handle
  370. * those separately.
  371. */
  372. int
  373. setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
  374. int namespace, const char *name, const char *fullname, int fd);
  375. int
  376. setup_xattr(struct archive_read_disk *a, struct archive_entry *entry,
  377. int namespace, const char *name, const char *fullname, int fd)
  378. {
  379. ssize_t size;
  380. void *value = NULL;
  381. const char *accpath;
  382. (void)fd; /* UNUSED */
  383. accpath = archive_entry_sourcepath(entry);
  384. if (accpath == NULL)
  385. accpath = archive_entry_pathname(entry);
  386. if (!a->follow_symlinks)
  387. size = extattr_get_link(accpath, namespace, name, NULL, 0);
  388. else
  389. size = extattr_get_file(accpath, namespace, name, NULL, 0);
  390. if (size == -1) {
  391. archive_set_error(&a->archive, errno,
  392. "Couldn't query extended attribute");
  393. return (ARCHIVE_WARN);
  394. }
  395. if (size > 0 && (value = malloc(size)) == NULL) {
  396. archive_set_error(&a->archive, errno, "Out of memory");
  397. return (ARCHIVE_FATAL);
  398. }
  399. if (!a->follow_symlinks)
  400. size = extattr_get_link(accpath, namespace, name, value, size);
  401. else
  402. size = extattr_get_file(accpath, namespace, name, value, size);
  403. if (size == -1) {
  404. archive_set_error(&a->archive, errno,
  405. "Couldn't read extended attribute");
  406. return (ARCHIVE_WARN);
  407. }
  408. archive_entry_xattr_add_entry(entry, fullname, value, size);
  409. free(value);
  410. return (ARCHIVE_OK);
  411. }
  412. static int
  413. setup_xattrs(struct archive_read_disk *a,
  414. struct archive_entry *entry, int fd)
  415. {
  416. char buff[512];
  417. char *list, *p;
  418. ssize_t list_size;
  419. const char *path;
  420. int namespace = EXTATTR_NAMESPACE_USER;
  421. path = archive_entry_sourcepath(entry);
  422. if (path == NULL)
  423. path = archive_entry_pathname(entry);
  424. if (!a->follow_symlinks)
  425. list_size = extattr_list_link(path, namespace, NULL, 0);
  426. else
  427. list_size = extattr_list_file(path, namespace, NULL, 0);
  428. if (list_size == -1 && errno == EOPNOTSUPP)
  429. return (ARCHIVE_OK);
  430. if (list_size == -1) {
  431. archive_set_error(&a->archive, errno,
  432. "Couldn't list extended attributes");
  433. return (ARCHIVE_WARN);
  434. }
  435. if (list_size == 0)
  436. return (ARCHIVE_OK);
  437. if ((list = malloc(list_size)) == NULL) {
  438. archive_set_error(&a->archive, errno, "Out of memory");
  439. return (ARCHIVE_FATAL);
  440. }
  441. if (!a->follow_symlinks)
  442. list_size = extattr_list_link(path, namespace, list, list_size);
  443. else
  444. list_size = extattr_list_file(path, namespace, list, list_size);
  445. if (list_size == -1) {
  446. archive_set_error(&a->archive, errno,
  447. "Couldn't retrieve extended attributes");
  448. free(list);
  449. return (ARCHIVE_WARN);
  450. }
  451. p = list;
  452. while ((p - list) < list_size) {
  453. size_t len = 255 & (int)*p;
  454. char *name;
  455. strcpy(buff, "user.");
  456. name = buff + strlen(buff);
  457. memcpy(name, p + 1, len);
  458. name[len] = '\0';
  459. setup_xattr(a, entry, namespace, name, buff, fd);
  460. p += 1 + len;
  461. }
  462. free(list);
  463. return (ARCHIVE_OK);
  464. }
  465. #else
  466. /*
  467. * Generic (stub) extended attribute support.
  468. */
  469. static int
  470. setup_xattrs(struct archive_read_disk *a,
  471. struct archive_entry *entry, int fd)
  472. {
  473. (void)a; /* UNUSED */
  474. (void)entry; /* UNUSED */
  475. (void)fd; /* UNUSED */
  476. return (ARCHIVE_OK);
  477. }
  478. #endif