data_extract_all.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  4. */
  5. #include "libbb.h"
  6. #include "bb_archive.h"
  7. void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
  8. {
  9. file_header_t *file_header = archive_handle->file_header;
  10. int dst_fd;
  11. int res;
  12. char *hard_link;
  13. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  14. char *dst_name;
  15. #else
  16. # define dst_name (file_header->name)
  17. #endif
  18. #if ENABLE_FEATURE_TAR_SELINUX
  19. char *sctx = archive_handle->tar__sctx[PAX_NEXT_FILE];
  20. if (!sctx)
  21. sctx = archive_handle->tar__sctx[PAX_GLOBAL];
  22. if (sctx) { /* setfscreatecon is 4 syscalls, avoid if possible */
  23. setfscreatecon(sctx);
  24. free(archive_handle->tar__sctx[PAX_NEXT_FILE]);
  25. archive_handle->tar__sctx[PAX_NEXT_FILE] = NULL;
  26. }
  27. #endif
  28. /* Hard links are encoded as regular files of size 0
  29. * with a nonempty link field */
  30. hard_link = NULL;
  31. if (S_ISREG(file_header->mode) && file_header->size == 0)
  32. hard_link = file_header->link_target;
  33. #if ENABLE_FEATURE_TAR_LONG_OPTIONS
  34. dst_name = file_header->name;
  35. if (archive_handle->tar__strip_components) {
  36. unsigned n = archive_handle->tar__strip_components;
  37. do {
  38. dst_name = strchr(dst_name, '/');
  39. if (!dst_name || dst_name[1] == '\0') {
  40. data_skip(archive_handle);
  41. goto ret;
  42. }
  43. dst_name++;
  44. /*
  45. * Link target is shortened only for hardlinks:
  46. * softlinks restored unchanged.
  47. */
  48. if (hard_link) {
  49. // GNU tar 1.26 does not check that we reached end of link name:
  50. // if "dir/hardlink" is hardlinked to "file",
  51. // tar xvf a.tar --strip-components=1 says:
  52. // tar: hardlink: Cannot hard link to '': No such file or directory
  53. // and continues processing. We silently skip such entries.
  54. hard_link = strchr(hard_link, '/');
  55. if (!hard_link || hard_link[1] == '\0') {
  56. data_skip(archive_handle);
  57. goto ret;
  58. }
  59. hard_link++;
  60. }
  61. } while (--n != 0);
  62. }
  63. #endif
  64. if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
  65. char *slash = strrchr(dst_name, '/');
  66. if (slash) {
  67. *slash = '\0';
  68. bb_make_directory(dst_name, -1, FILEUTILS_RECUR);
  69. *slash = '/';
  70. }
  71. }
  72. if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
  73. /* Remove the entry if it exists */
  74. if (!S_ISDIR(file_header->mode)) {
  75. if (hard_link) {
  76. /* Ugly special case:
  77. * tar cf t.tar hardlink1 hardlink2 hardlink1
  78. * results in this tarball structure:
  79. * hardlink1
  80. * hardlink2 -> hardlink1
  81. * hardlink1 -> hardlink1 <== !!!
  82. */
  83. if (strcmp(hard_link, dst_name) == 0)
  84. goto ret;
  85. }
  86. /* Proceed with deleting */
  87. if (unlink(dst_name) == -1
  88. && errno != ENOENT
  89. ) {
  90. bb_perror_msg_and_die("can't remove old file %s",
  91. dst_name);
  92. }
  93. }
  94. }
  95. else if (archive_handle->ah_flags & ARCHIVE_EXTRACT_NEWER) {
  96. /* Remove the existing entry if its older than the extracted entry */
  97. struct stat existing_sb;
  98. if (lstat(dst_name, &existing_sb) == -1) {
  99. if (errno != ENOENT) {
  100. bb_perror_msg_and_die("can't stat old file");
  101. }
  102. }
  103. else if (existing_sb.st_mtime >= file_header->mtime) {
  104. if (!S_ISDIR(file_header->mode)) {
  105. bb_error_msg("%s not created: newer or "
  106. "same age file exists", dst_name);
  107. }
  108. data_skip(archive_handle);
  109. goto ret;
  110. }
  111. else if ((unlink(dst_name) == -1) && (errno != EISDIR)) {
  112. bb_perror_msg_and_die("can't remove old file %s",
  113. dst_name);
  114. }
  115. }
  116. /* Handle hard links separately */
  117. if (hard_link) {
  118. res = link(hard_link, dst_name);
  119. if (res != 0) {
  120. /* shared message */
  121. bb_perror_msg("can't create %slink '%s' to '%s'",
  122. "hard", dst_name, hard_link
  123. );
  124. }
  125. /* Hardlinks have no separate mode/ownership, skip chown/chmod */
  126. goto ret;
  127. }
  128. /* Create the filesystem entry */
  129. switch (file_header->mode & S_IFMT) {
  130. case S_IFREG: {
  131. /* Regular file */
  132. char *dst_nameN;
  133. int flags = O_WRONLY | O_CREAT | O_EXCL;
  134. if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
  135. flags = O_WRONLY | O_CREAT | O_TRUNC;
  136. dst_nameN = dst_name;
  137. #ifdef ARCHIVE_REPLACE_VIA_RENAME
  138. if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME)
  139. /* rpm-style temp file name */
  140. dst_nameN = xasprintf("%s;%x", dst_name, (int)getpid());
  141. #endif
  142. dst_fd = xopen3(dst_nameN,
  143. flags,
  144. file_header->mode
  145. );
  146. bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
  147. close(dst_fd);
  148. #ifdef ARCHIVE_REPLACE_VIA_RENAME
  149. if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME) {
  150. xrename(dst_nameN, dst_name);
  151. free(dst_nameN);
  152. }
  153. #endif
  154. break;
  155. }
  156. case S_IFDIR:
  157. res = mkdir(dst_name, file_header->mode);
  158. if ((res != 0)
  159. && (errno != EISDIR) /* btw, Linux doesn't return this */
  160. && (errno != EEXIST)
  161. ) {
  162. bb_perror_msg("can't make dir %s", dst_name);
  163. }
  164. break;
  165. case S_IFLNK:
  166. /* Symlink */
  167. //TODO: what if file_header->link_target == NULL (say, corrupted tarball?)
  168. /* To avoid a directory traversal attack via symlinks,
  169. * do not restore symlinks with ".." components
  170. * or symlinks starting with "/", unless a magic
  171. * envvar is set.
  172. *
  173. * For example, consider a .tar created via:
  174. * $ tar cvf bug.tar anything.txt
  175. * $ ln -s /tmp symlink
  176. * $ tar --append -f bug.tar symlink
  177. * $ rm symlink
  178. * $ mkdir symlink
  179. * $ tar --append -f bug.tar symlink/evil.py
  180. *
  181. * This will result in an archive that contains:
  182. * $ tar --list -f bug.tar
  183. * anything.txt
  184. * symlink [-> /tmp]
  185. * symlink/evil.py
  186. *
  187. * Untarring bug.tar would otherwise place evil.py in '/tmp'.
  188. */
  189. create_or_remember_symlink(&archive_handle->symlink_placeholders,
  190. file_header->link_target,
  191. dst_name);
  192. break;
  193. case S_IFSOCK:
  194. case S_IFBLK:
  195. case S_IFCHR:
  196. case S_IFIFO:
  197. res = mknod(dst_name, file_header->mode, file_header->device);
  198. if (res != 0) {
  199. bb_perror_msg("can't create node %s", dst_name);
  200. }
  201. break;
  202. default:
  203. bb_error_msg_and_die("unrecognized file type");
  204. }
  205. if (!S_ISLNK(file_header->mode)) {
  206. if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
  207. uid_t uid = file_header->uid;
  208. gid_t gid = file_header->gid;
  209. #if ENABLE_FEATURE_TAR_UNAME_GNAME
  210. if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
  211. if (file_header->tar__uname) {
  212. //TODO: cache last name/id pair?
  213. struct passwd *pwd = getpwnam(file_header->tar__uname);
  214. if (pwd) uid = pwd->pw_uid;
  215. }
  216. if (file_header->tar__gname) {
  217. struct group *grp = getgrnam(file_header->tar__gname);
  218. if (grp) gid = grp->gr_gid;
  219. }
  220. }
  221. #endif
  222. /* GNU tar 1.15.1 uses chown, not lchown */
  223. chown(dst_name, uid, gid);
  224. }
  225. /* uclibc has no lchmod, glibc is even stranger -
  226. * it has lchmod which seems to do nothing!
  227. * so we use chmod... */
  228. if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
  229. chmod(dst_name, file_header->mode);
  230. }
  231. if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
  232. struct timeval t[2];
  233. t[1].tv_sec = t[0].tv_sec = file_header->mtime;
  234. t[1].tv_usec = t[0].tv_usec = 0;
  235. utimes(dst_name, t);
  236. }
  237. }
  238. ret: ;
  239. #if ENABLE_FEATURE_TAR_SELINUX
  240. if (sctx) {
  241. /* reset the context after creating an entry */
  242. setfscreatecon(NULL);
  243. }
  244. #endif
  245. }