data_extract_all.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. create_or_remember_link(&archive_handle->link_placeholders,
  119. hard_link,
  120. dst_name,
  121. 1);
  122. /* Hardlinks have no separate mode/ownership, skip chown/chmod */
  123. goto ret;
  124. }
  125. /* Create the filesystem entry */
  126. switch (file_header->mode & S_IFMT) {
  127. case S_IFREG: {
  128. /* Regular file */
  129. char *dst_nameN;
  130. int flags = O_WRONLY | O_CREAT | O_EXCL;
  131. if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
  132. flags = O_WRONLY | O_CREAT | O_TRUNC;
  133. dst_nameN = dst_name;
  134. #ifdef ARCHIVE_REPLACE_VIA_RENAME
  135. if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME)
  136. /* rpm-style temp file name */
  137. dst_nameN = xasprintf("%s;%x", dst_name, (int)getpid());
  138. #endif
  139. dst_fd = xopen3(dst_nameN,
  140. flags,
  141. file_header->mode
  142. );
  143. bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
  144. close(dst_fd);
  145. #ifdef ARCHIVE_REPLACE_VIA_RENAME
  146. if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME) {
  147. xrename(dst_nameN, dst_name);
  148. free(dst_nameN);
  149. }
  150. #endif
  151. break;
  152. }
  153. case S_IFDIR:
  154. res = mkdir(dst_name, file_header->mode);
  155. if ((res != 0)
  156. && (errno != EISDIR) /* btw, Linux doesn't return this */
  157. && (errno != EEXIST)
  158. ) {
  159. bb_perror_msg("can't make dir %s", dst_name);
  160. }
  161. break;
  162. case S_IFLNK:
  163. /* Symlink */
  164. //TODO: what if file_header->link_target == NULL (say, corrupted tarball?)
  165. /* To avoid a directory traversal attack via symlinks,
  166. * do not restore symlinks with ".." components
  167. * or symlinks starting with "/", unless a magic
  168. * envvar is set.
  169. *
  170. * For example, consider a .tar created via:
  171. * $ tar cvf bug.tar anything.txt
  172. * $ ln -s /tmp symlink
  173. * $ tar --append -f bug.tar symlink
  174. * $ rm symlink
  175. * $ mkdir symlink
  176. * $ tar --append -f bug.tar symlink/evil.py
  177. *
  178. * This will result in an archive that contains:
  179. * $ tar --list -f bug.tar
  180. * anything.txt
  181. * symlink [-> /tmp]
  182. * symlink/evil.py
  183. *
  184. * Untarring bug.tar would otherwise place evil.py in '/tmp'.
  185. */
  186. create_or_remember_link(&archive_handle->link_placeholders,
  187. file_header->link_target,
  188. dst_name,
  189. 0);
  190. break;
  191. case S_IFSOCK:
  192. case S_IFBLK:
  193. case S_IFCHR:
  194. case S_IFIFO:
  195. res = mknod(dst_name, file_header->mode, file_header->device);
  196. if (res != 0) {
  197. bb_perror_msg("can't create node %s", dst_name);
  198. }
  199. break;
  200. default:
  201. bb_error_msg_and_die("unrecognized file type");
  202. }
  203. if (!S_ISLNK(file_header->mode)) {
  204. if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
  205. uid_t uid = file_header->uid;
  206. gid_t gid = file_header->gid;
  207. #if ENABLE_FEATURE_TAR_UNAME_GNAME
  208. if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
  209. if (file_header->tar__uname) {
  210. //TODO: cache last name/id pair?
  211. struct passwd *pwd = getpwnam(file_header->tar__uname);
  212. if (pwd) uid = pwd->pw_uid;
  213. }
  214. if (file_header->tar__gname) {
  215. struct group *grp = getgrnam(file_header->tar__gname);
  216. if (grp) gid = grp->gr_gid;
  217. }
  218. }
  219. #endif
  220. /* GNU tar 1.15.1 uses chown, not lchown */
  221. chown(dst_name, uid, gid);
  222. }
  223. /* uclibc has no lchmod, glibc is even stranger -
  224. * it has lchmod which seems to do nothing!
  225. * so we use chmod... */
  226. if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
  227. chmod(dst_name, file_header->mode);
  228. }
  229. if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
  230. struct timeval t[2];
  231. t[1].tv_sec = t[0].tv_sec = file_header->mtime;
  232. t[1].tv_usec = t[0].tv_usec = 0;
  233. utimes(dst_name, t);
  234. }
  235. }
  236. ret: ;
  237. #if ENABLE_FEATURE_TAR_SELINUX
  238. if (sctx) {
  239. /* reset the context after creating an entry */
  240. setfscreatecon(NULL);
  241. }
  242. #endif
  243. }