blob: 8fa69ffafc1cfc4186659705a16c6f6f49b23a92 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath237ae422002-11-03 14:05:15 +00002/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath237ae422002-11-03 14:05:15 +00004 */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00005#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +02006#include "bb_archive.h"
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00007
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00008void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00009{
10 file_header_t *file_header = archive_handle->file_header;
11 int dst_fd;
12 int res;
Denys Vlasenkof167e452015-10-22 13:30:34 +020013 char *hard_link;
Denys Vlasenko6c563e32015-10-22 01:07:13 +020014#if ENABLE_FEATURE_TAR_LONG_OPTIONS
15 char *dst_name;
Denys Vlasenko6c563e32015-10-22 01:07:13 +020016#else
17# define dst_name (file_header->name)
Denys Vlasenko6c563e32015-10-22 01:07:13 +020018#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000019
J. Tang77a2c512010-03-19 14:48:51 +010020#if ENABLE_FEATURE_TAR_SELINUX
Denys Vlasenko6111f962012-02-23 13:45:18 +010021 char *sctx = archive_handle->tar__sctx[PAX_NEXT_FILE];
J. Tang77a2c512010-03-19 14:48:51 +010022 if (!sctx)
Denys Vlasenko6111f962012-02-23 13:45:18 +010023 sctx = archive_handle->tar__sctx[PAX_GLOBAL];
J. Tang77a2c512010-03-19 14:48:51 +010024 if (sctx) { /* setfscreatecon is 4 syscalls, avoid if possible */
25 setfscreatecon(sctx);
Denys Vlasenko6111f962012-02-23 13:45:18 +010026 free(archive_handle->tar__sctx[PAX_NEXT_FILE]);
27 archive_handle->tar__sctx[PAX_NEXT_FILE] = NULL;
J. Tang77a2c512010-03-19 14:48:51 +010028 }
29#endif
30
Denys Vlasenkof167e452015-10-22 13:30:34 +020031 /* Hard links are encoded as regular files of size 0
32 * with a nonempty link field */
33 hard_link = NULL;
34 if (S_ISREG(file_header->mode) && file_header->size == 0)
35 hard_link = file_header->link_target;
36
Denys Vlasenko6c563e32015-10-22 01:07:13 +020037#if ENABLE_FEATURE_TAR_LONG_OPTIONS
38 dst_name = file_header->name;
Denys Vlasenko6c563e32015-10-22 01:07:13 +020039 if (archive_handle->tar__strip_components) {
40 unsigned n = archive_handle->tar__strip_components;
41 do {
42 dst_name = strchr(dst_name, '/');
43 if (!dst_name || dst_name[1] == '\0') {
44 data_skip(archive_handle);
Denys Vlasenko537389c2015-10-22 13:38:09 +020045 goto ret;
Denys Vlasenko6c563e32015-10-22 01:07:13 +020046 }
47 dst_name++;
48 /*
49 * Link target is shortened only for hardlinks:
50 * softlinks restored unchanged.
51 */
Denys Vlasenkof167e452015-10-22 13:30:34 +020052 if (hard_link) {
Denys Vlasenko6c563e32015-10-22 01:07:13 +020053// GNU tar 1.26 does not check that we reached end of link name:
54// if "dir/hardlink" is hardlinked to "file",
55// tar xvf a.tar --strip-components=1 says:
56// tar: hardlink: Cannot hard link to '': No such file or directory
57// and continues processing. We silently skip such entries.
Denys Vlasenkof167e452015-10-22 13:30:34 +020058 hard_link = strchr(hard_link, '/');
59 if (!hard_link || hard_link[1] == '\0') {
Denys Vlasenko6c563e32015-10-22 01:07:13 +020060 data_skip(archive_handle);
Denys Vlasenko537389c2015-10-22 13:38:09 +020061 goto ret;
Denys Vlasenko6c563e32015-10-22 01:07:13 +020062 }
Denys Vlasenkof167e452015-10-22 13:30:34 +020063 hard_link++;
Denys Vlasenko6c563e32015-10-22 01:07:13 +020064 }
65 } while (--n != 0);
66 }
67#endif
68
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000069 if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
Denys Vlasenko6c563e32015-10-22 01:07:13 +020070 char *slash = strrchr(dst_name, '/');
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010071 if (slash) {
72 *slash = '\0';
Denys Vlasenko6c563e32015-10-22 01:07:13 +020073 bb_make_directory(dst_name, -1, FILEUTILS_RECUR);
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010074 *slash = '/';
75 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000076 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000077
Denys Vlasenkod57d6262009-09-17 02:43:14 +020078 if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000079 /* Remove the entry if it exists */
Denys Vlasenko02365a62010-04-09 10:52:52 +020080 if (!S_ISDIR(file_header->mode)) {
Denys Vlasenkof167e452015-10-22 13:30:34 +020081 if (hard_link) {
Denys Vlasenko02365a62010-04-09 10:52:52 +020082 /* Ugly special case:
83 * tar cf t.tar hardlink1 hardlink2 hardlink1
84 * results in this tarball structure:
85 * hardlink1
86 * hardlink2 -> hardlink1
87 * hardlink1 -> hardlink1 <== !!!
88 */
Denys Vlasenkof167e452015-10-22 13:30:34 +020089 if (strcmp(hard_link, dst_name) == 0)
Denys Vlasenko02365a62010-04-09 10:52:52 +020090 goto ret;
91 }
92 /* Proceed with deleting */
Denys Vlasenko6c563e32015-10-22 01:07:13 +020093 if (unlink(dst_name) == -1
Denys Vlasenko02365a62010-04-09 10:52:52 +020094 && errno != ENOENT
95 ) {
96 bb_perror_msg_and_die("can't remove old file %s",
Denys Vlasenko6c563e32015-10-22 01:07:13 +020097 dst_name);
Denys Vlasenko02365a62010-04-09 10:52:52 +020098 }
Glenn L McGrath4cee66d2003-08-28 19:12:23 +000099 }
100 }
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000101 else if (archive_handle->ah_flags & ARCHIVE_EXTRACT_NEWER) {
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000102 /* Remove the existing entry if its older than the extracted entry */
Denys Vlasenko6ccaa232009-11-24 01:16:12 +0100103 struct stat existing_sb;
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200104 if (lstat(dst_name, &existing_sb) == -1) {
Glenn L McGrath8dc8cb12003-11-15 23:44:31 +0000105 if (errno != ENOENT) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200106 bb_perror_msg_and_die("can't stat old file");
Glenn L McGrath8dc8cb12003-11-15 23:44:31 +0000107 }
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000108 }
Denys Vlasenko6ccaa232009-11-24 01:16:12 +0100109 else if (existing_sb.st_mtime >= file_header->mtime) {
Denys Vlasenkoa84db182018-02-20 15:57:45 +0100110 if (!S_ISDIR(file_header->mode)) {
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000111 bb_error_msg("%s not created: newer or "
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200112 "same age file exists", dst_name);
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000113 }
114 data_skip(archive_handle);
J. Tang77a2c512010-03-19 14:48:51 +0100115 goto ret;
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000116 }
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200117 else if ((unlink(dst_name) == -1) && (errno != EISDIR)) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200118 bb_perror_msg_and_die("can't remove old file %s",
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200119 dst_name);
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000120 }
121 }
122
Denys Vlasenkof167e452015-10-22 13:30:34 +0200123 /* Handle hard links separately */
124 if (hard_link) {
125 res = link(hard_link, dst_name);
Denys Vlasenkoa84db182018-02-20 15:57:45 +0100126 if (res != 0) {
Denys Vlasenko06f20bf2017-01-26 00:27:53 +0100127 /* shared message */
Denys Vlasenkob920a382017-07-24 17:20:13 +0200128 bb_perror_msg("can't create %slink '%s' to '%s'",
Denys Vlasenkobc9bbeb2017-08-10 11:52:42 +0200129 "hard", dst_name, hard_link
Denys Vlasenkob920a382017-07-24 17:20:13 +0200130 );
Glenn L McGrath3d5828f2003-08-14 02:55:15 +0000131 }
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200132 /* Hardlinks have no separate mode/ownership, skip chown/chmod */
133 goto ret;
134 }
135
136 /* Create the filesystem entry */
137 switch (file_header->mode & S_IFMT) {
138 case S_IFREG: {
139 /* Regular file */
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200140 char *dst_nameN;
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200141 int flags = O_WRONLY | O_CREAT | O_EXCL;
142 if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
143 flags = O_WRONLY | O_CREAT | O_TRUNC;
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200144 dst_nameN = dst_name;
Denys Vlasenko2aec7732013-02-20 15:58:42 +0100145#ifdef ARCHIVE_REPLACE_VIA_RENAME
146 if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME)
147 /* rpm-style temp file name */
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200148 dst_nameN = xasprintf("%s;%x", dst_name, (int)getpid());
Denys Vlasenko2aec7732013-02-20 15:58:42 +0100149#endif
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200150 dst_fd = xopen3(dst_nameN,
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200151 flags,
152 file_header->mode
153 );
154 bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
155 close(dst_fd);
Denys Vlasenko2aec7732013-02-20 15:58:42 +0100156#ifdef ARCHIVE_REPLACE_VIA_RENAME
157 if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME) {
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200158 xrename(dst_nameN, dst_name);
159 free(dst_nameN);
Denys Vlasenko2aec7732013-02-20 15:58:42 +0100160 }
161#endif
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200162 break;
163 }
164 case S_IFDIR:
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200165 res = mkdir(dst_name, file_header->mode);
Denys Vlasenkoa84db182018-02-20 15:57:45 +0100166 if ((res != 0)
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200167 && (errno != EISDIR) /* btw, Linux doesn't return this */
168 && (errno != EEXIST)
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200169 ) {
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200170 bb_perror_msg("can't make dir %s", dst_name);
Denis Vlasenko714701c2006-12-22 00:21:07 +0000171 }
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200172 break;
173 case S_IFLNK:
174 /* Symlink */
Denys Vlasenko243d1752010-07-04 04:26:55 +0200175//TODO: what if file_header->link_target == NULL (say, corrupted tarball?)
Denys Vlasenkob920a382017-07-24 17:20:13 +0200176
177 /* To avoid a directory traversal attack via symlinks,
Denys Vlasenkobc9bbeb2017-08-10 11:52:42 +0200178 * do not restore symlinks with ".." components
179 * or symlinks starting with "/", unless a magic
180 * envvar is set.
Denys Vlasenkob920a382017-07-24 17:20:13 +0200181 *
182 * For example, consider a .tar created via:
183 * $ tar cvf bug.tar anything.txt
184 * $ ln -s /tmp symlink
185 * $ tar --append -f bug.tar symlink
186 * $ rm symlink
187 * $ mkdir symlink
188 * $ tar --append -f bug.tar symlink/evil.py
189 *
190 * This will result in an archive that contains:
191 * $ tar --list -f bug.tar
192 * anything.txt
193 * symlink [-> /tmp]
194 * symlink/evil.py
195 *
196 * Untarring bug.tar would otherwise place evil.py in '/tmp'.
197 */
Denys Vlasenkoa84db182018-02-20 15:57:45 +0100198 create_or_remember_symlink(&archive_handle->symlink_placeholders,
199 file_header->link_target,
200 dst_name);
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200201 break;
202 case S_IFSOCK:
203 case S_IFBLK:
204 case S_IFCHR:
205 case S_IFIFO:
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200206 res = mknod(dst_name, file_header->mode, file_header->device);
Denys Vlasenkoa84db182018-02-20 15:57:45 +0100207 if (res != 0) {
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200208 bb_perror_msg("can't create node %s", dst_name);
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200209 }
210 break;
211 default:
212 bb_error_msg_and_die("unrecognized file type");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000213 }
214
Denys Vlasenkod86b4c32010-06-18 02:00:55 +0200215 if (!S_ISLNK(file_header->mode)) {
216 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
Denis Vlasenko8b814b42009-04-21 00:52:21 +0000217 uid_t uid = file_header->uid;
218 gid_t gid = file_header->gid;
Denys Vlasenkod86b4c32010-06-18 02:00:55 +0200219#if ENABLE_FEATURE_TAR_UNAME_GNAME
220 if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
221 if (file_header->tar__uname) {
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100222//TODO: cache last name/id pair?
Denys Vlasenkod86b4c32010-06-18 02:00:55 +0200223 struct passwd *pwd = getpwnam(file_header->tar__uname);
224 if (pwd) uid = pwd->pw_uid;
225 }
226 if (file_header->tar__gname) {
227 struct group *grp = getgrnam(file_header->tar__gname);
228 if (grp) gid = grp->gr_gid;
229 }
Denis Vlasenko8b814b42009-04-21 00:52:21 +0000230 }
Denys Vlasenkod86b4c32010-06-18 02:00:55 +0200231#endif
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100232 /* GNU tar 1.15.1 uses chown, not lchown */
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200233 chown(dst_name, uid, gid);
Denys Vlasenkod86b4c32010-06-18 02:00:55 +0200234 }
Denis Vlasenko75103842007-06-20 14:49:47 +0000235 /* uclibc has no lchmod, glibc is even stranger -
236 * it has lchmod which seems to do nothing!
237 * so we use chmod... */
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200238 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200239 chmod(dst_name, file_header->mode);
Denis Vlasenko75103842007-06-20 14:49:47 +0000240 }
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200241 if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100242 struct timeval t[2];
Denys Vlasenko389cca42009-11-15 02:28:56 +0100243
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100244 t[1].tv_sec = t[0].tv_sec = file_header->mtime;
245 t[1].tv_usec = t[0].tv_usec = 0;
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200246 utimes(dst_name, t);
Denis Vlasenko75103842007-06-20 14:49:47 +0000247 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000248 }
J. Tang77a2c512010-03-19 14:48:51 +0100249
250 ret: ;
251#if ENABLE_FEATURE_TAR_SELINUX
252 if (sctx) {
253 /* reset the context after creating an entry */
254 setfscreatecon(NULL);
255 }
256#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000257}