archive_read_disk_entry_from_file.c 14 KB

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