blob: e658444e03c8fd0d7be418690df0254f77b2050d [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 */
5
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00006#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +02007#include "bb_archive.h"
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00008
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00009void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000010{
11 file_header_t *file_header = archive_handle->file_header;
12 int dst_fd;
13 int res;
Denys Vlasenkof167e452015-10-22 13:30:34 +020014 char *hard_link;
Denys Vlasenko6c563e32015-10-22 01:07:13 +020015#if ENABLE_FEATURE_TAR_LONG_OPTIONS
16 char *dst_name;
Denys Vlasenko6c563e32015-10-22 01:07:13 +020017#else
18# define dst_name (file_header->name)
Denys Vlasenko6c563e32015-10-22 01:07:13 +020019#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000020
J. Tang77a2c512010-03-19 14:48:51 +010021#if ENABLE_FEATURE_TAR_SELINUX
Denys Vlasenko6111f962012-02-23 13:45:18 +010022 char *sctx = archive_handle->tar__sctx[PAX_NEXT_FILE];
J. Tang77a2c512010-03-19 14:48:51 +010023 if (!sctx)
Denys Vlasenko6111f962012-02-23 13:45:18 +010024 sctx = archive_handle->tar__sctx[PAX_GLOBAL];
J. Tang77a2c512010-03-19 14:48:51 +010025 if (sctx) { /* setfscreatecon is 4 syscalls, avoid if possible */
26 setfscreatecon(sctx);
Denys Vlasenko6111f962012-02-23 13:45:18 +010027 free(archive_handle->tar__sctx[PAX_NEXT_FILE]);
28 archive_handle->tar__sctx[PAX_NEXT_FILE] = NULL;
J. Tang77a2c512010-03-19 14:48:51 +010029 }
30#endif
31
Denys Vlasenkof167e452015-10-22 13:30:34 +020032 /* Hard links are encoded as regular files of size 0
33 * with a nonempty link field */
34 hard_link = NULL;
35 if (S_ISREG(file_header->mode) && file_header->size == 0)
36 hard_link = file_header->link_target;
37
Denys Vlasenko6c563e32015-10-22 01:07:13 +020038#if ENABLE_FEATURE_TAR_LONG_OPTIONS
39 dst_name = file_header->name;
Denys Vlasenko6c563e32015-10-22 01:07:13 +020040 if (archive_handle->tar__strip_components) {
41 unsigned n = archive_handle->tar__strip_components;
42 do {
43 dst_name = strchr(dst_name, '/');
44 if (!dst_name || dst_name[1] == '\0') {
45 data_skip(archive_handle);
Denys Vlasenko537389c2015-10-22 13:38:09 +020046 goto ret;
Denys Vlasenko6c563e32015-10-22 01:07:13 +020047 }
48 dst_name++;
49 /*
50 * Link target is shortened only for hardlinks:
51 * softlinks restored unchanged.
52 */
Denys Vlasenkof167e452015-10-22 13:30:34 +020053 if (hard_link) {
Denys Vlasenko6c563e32015-10-22 01:07:13 +020054// GNU tar 1.26 does not check that we reached end of link name:
55// if "dir/hardlink" is hardlinked to "file",
56// tar xvf a.tar --strip-components=1 says:
57// tar: hardlink: Cannot hard link to '': No such file or directory
58// and continues processing. We silently skip such entries.
Denys Vlasenkof167e452015-10-22 13:30:34 +020059 hard_link = strchr(hard_link, '/');
60 if (!hard_link || hard_link[1] == '\0') {
Denys Vlasenko6c563e32015-10-22 01:07:13 +020061 data_skip(archive_handle);
Denys Vlasenko537389c2015-10-22 13:38:09 +020062 goto ret;
Denys Vlasenko6c563e32015-10-22 01:07:13 +020063 }
Denys Vlasenkof167e452015-10-22 13:30:34 +020064 hard_link++;
Denys Vlasenko6c563e32015-10-22 01:07:13 +020065 }
66 } while (--n != 0);
67 }
68#endif
69
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000070 if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
Denys Vlasenko6c563e32015-10-22 01:07:13 +020071 char *slash = strrchr(dst_name, '/');
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010072 if (slash) {
73 *slash = '\0';
Denys Vlasenko6c563e32015-10-22 01:07:13 +020074 bb_make_directory(dst_name, -1, FILEUTILS_RECUR);
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010075 *slash = '/';
76 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000077 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000078
Denys Vlasenkod57d6262009-09-17 02:43:14 +020079 if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000080 /* Remove the entry if it exists */
Denys Vlasenko02365a62010-04-09 10:52:52 +020081 if (!S_ISDIR(file_header->mode)) {
Denys Vlasenkof167e452015-10-22 13:30:34 +020082 if (hard_link) {
Denys Vlasenko02365a62010-04-09 10:52:52 +020083 /* Ugly special case:
84 * tar cf t.tar hardlink1 hardlink2 hardlink1
85 * results in this tarball structure:
86 * hardlink1
87 * hardlink2 -> hardlink1
88 * hardlink1 -> hardlink1 <== !!!
89 */
Denys Vlasenkof167e452015-10-22 13:30:34 +020090 if (strcmp(hard_link, dst_name) == 0)
Denys Vlasenko02365a62010-04-09 10:52:52 +020091 goto ret;
92 }
93 /* Proceed with deleting */
Denys Vlasenko6c563e32015-10-22 01:07:13 +020094 if (unlink(dst_name) == -1
Denys Vlasenko02365a62010-04-09 10:52:52 +020095 && errno != ENOENT
96 ) {
97 bb_perror_msg_and_die("can't remove old file %s",
Denys Vlasenko6c563e32015-10-22 01:07:13 +020098 dst_name);
Denys Vlasenko02365a62010-04-09 10:52:52 +020099 }
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000100 }
101 }
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000102 else if (archive_handle->ah_flags & ARCHIVE_EXTRACT_NEWER) {
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000103 /* Remove the existing entry if its older than the extracted entry */
Denys Vlasenko6ccaa232009-11-24 01:16:12 +0100104 struct stat existing_sb;
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200105 if (lstat(dst_name, &existing_sb) == -1) {
Glenn L McGrath8dc8cb12003-11-15 23:44:31 +0000106 if (errno != ENOENT) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200107 bb_perror_msg_and_die("can't stat old file");
Glenn L McGrath8dc8cb12003-11-15 23:44:31 +0000108 }
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000109 }
Denys Vlasenko6ccaa232009-11-24 01:16:12 +0100110 else if (existing_sb.st_mtime >= file_header->mtime) {
Pascal Bellard873bb312010-10-18 00:54:51 +0200111 if (!(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
112 && !S_ISDIR(file_header->mode)
113 ) {
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000114 bb_error_msg("%s not created: newer or "
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200115 "same age file exists", dst_name);
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000116 }
117 data_skip(archive_handle);
J. Tang77a2c512010-03-19 14:48:51 +0100118 goto ret;
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000119 }
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200120 else if ((unlink(dst_name) == -1) && (errno != EISDIR)) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200121 bb_perror_msg_and_die("can't remove old file %s",
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200122 dst_name);
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000123 }
124 }
125
Denys Vlasenkof167e452015-10-22 13:30:34 +0200126 /* Handle hard links separately */
127 if (hard_link) {
128 res = link(hard_link, dst_name);
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200129 if (res != 0 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) {
Denys Vlasenko06f20bf2017-01-26 00:27:53 +0100130 /* shared message */
Denys Vlasenkob920a382017-07-24 17:20:13 +0200131 bb_perror_msg("can't create %slink '%s' to '%s'",
Denys Vlasenkobc9bbeb2017-08-10 11:52:42 +0200132 "hard", dst_name, hard_link
Denys Vlasenkob920a382017-07-24 17:20:13 +0200133 );
Glenn L McGrath3d5828f2003-08-14 02:55:15 +0000134 }
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200135 /* Hardlinks have no separate mode/ownership, skip chown/chmod */
136 goto ret;
137 }
138
139 /* Create the filesystem entry */
140 switch (file_header->mode & S_IFMT) {
141 case S_IFREG: {
142 /* Regular file */
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200143 char *dst_nameN;
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200144 int flags = O_WRONLY | O_CREAT | O_EXCL;
145 if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
146 flags = O_WRONLY | O_CREAT | O_TRUNC;
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200147 dst_nameN = dst_name;
Denys Vlasenko2aec7732013-02-20 15:58:42 +0100148#ifdef ARCHIVE_REPLACE_VIA_RENAME
149 if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME)
150 /* rpm-style temp file name */
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200151 dst_nameN = xasprintf("%s;%x", dst_name, (int)getpid());
Denys Vlasenko2aec7732013-02-20 15:58:42 +0100152#endif
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200153 dst_fd = xopen3(dst_nameN,
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200154 flags,
155 file_header->mode
156 );
157 bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
158 close(dst_fd);
Denys Vlasenko2aec7732013-02-20 15:58:42 +0100159#ifdef ARCHIVE_REPLACE_VIA_RENAME
160 if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME) {
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200161 xrename(dst_nameN, dst_name);
162 free(dst_nameN);
Denys Vlasenko2aec7732013-02-20 15:58:42 +0100163 }
164#endif
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200165 break;
166 }
167 case S_IFDIR:
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200168 res = mkdir(dst_name, file_header->mode);
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200169 if ((res == -1)
170 && (errno != EISDIR) /* btw, Linux doesn't return this */
171 && (errno != EEXIST)
172 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
173 ) {
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200174 bb_perror_msg("can't make dir %s", dst_name);
Denis Vlasenko714701c2006-12-22 00:21:07 +0000175 }
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200176 break;
177 case S_IFLNK:
178 /* Symlink */
Denys Vlasenko243d1752010-07-04 04:26:55 +0200179//TODO: what if file_header->link_target == NULL (say, corrupted tarball?)
Denys Vlasenkob920a382017-07-24 17:20:13 +0200180
181 /* To avoid a directory traversal attack via symlinks,
Denys Vlasenkobc9bbeb2017-08-10 11:52:42 +0200182 * do not restore symlinks with ".." components
183 * or symlinks starting with "/", unless a magic
184 * envvar is set.
Denys Vlasenkob920a382017-07-24 17:20:13 +0200185 *
186 * For example, consider a .tar created via:
187 * $ tar cvf bug.tar anything.txt
188 * $ ln -s /tmp symlink
189 * $ tar --append -f bug.tar symlink
190 * $ rm symlink
191 * $ mkdir symlink
192 * $ tar --append -f bug.tar symlink/evil.py
193 *
194 * This will result in an archive that contains:
195 * $ tar --list -f bug.tar
196 * anything.txt
197 * symlink [-> /tmp]
198 * symlink/evil.py
199 *
200 * Untarring bug.tar would otherwise place evil.py in '/tmp'.
201 */
Denys Vlasenkobc9bbeb2017-08-10 11:52:42 +0200202 if (!unsafe_symlink_target(file_header->link_target)) {
203 res = symlink(file_header->link_target, dst_name);
204 if (res != 0
205 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
206 ) {
207 /* shared message */
208 bb_perror_msg("can't create %slink '%s' to '%s'",
209 "sym",
210 dst_name, file_header->link_target
211 );
212 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000213 }
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200214 break;
215 case S_IFSOCK:
216 case S_IFBLK:
217 case S_IFCHR:
218 case S_IFIFO:
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200219 res = mknod(dst_name, file_header->mode, file_header->device);
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200220 if ((res == -1)
221 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
222 ) {
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200223 bb_perror_msg("can't create node %s", dst_name);
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200224 }
225 break;
226 default:
227 bb_error_msg_and_die("unrecognized file type");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000228 }
229
Denys Vlasenkod86b4c32010-06-18 02:00:55 +0200230 if (!S_ISLNK(file_header->mode)) {
231 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
Denis Vlasenko8b814b42009-04-21 00:52:21 +0000232 uid_t uid = file_header->uid;
233 gid_t gid = file_header->gid;
Denys Vlasenkod86b4c32010-06-18 02:00:55 +0200234#if ENABLE_FEATURE_TAR_UNAME_GNAME
235 if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
236 if (file_header->tar__uname) {
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100237//TODO: cache last name/id pair?
Denys Vlasenkod86b4c32010-06-18 02:00:55 +0200238 struct passwd *pwd = getpwnam(file_header->tar__uname);
239 if (pwd) uid = pwd->pw_uid;
240 }
241 if (file_header->tar__gname) {
242 struct group *grp = getgrnam(file_header->tar__gname);
243 if (grp) gid = grp->gr_gid;
244 }
Denis Vlasenko8b814b42009-04-21 00:52:21 +0000245 }
Denys Vlasenkod86b4c32010-06-18 02:00:55 +0200246#endif
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100247 /* GNU tar 1.15.1 uses chown, not lchown */
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200248 chown(dst_name, uid, gid);
Denys Vlasenkod86b4c32010-06-18 02:00:55 +0200249 }
Denis Vlasenko75103842007-06-20 14:49:47 +0000250 /* uclibc has no lchmod, glibc is even stranger -
251 * it has lchmod which seems to do nothing!
252 * so we use chmod... */
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200253 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200254 chmod(dst_name, file_header->mode);
Denis Vlasenko75103842007-06-20 14:49:47 +0000255 }
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200256 if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100257 struct timeval t[2];
Denys Vlasenko389cca42009-11-15 02:28:56 +0100258
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100259 t[1].tv_sec = t[0].tv_sec = file_header->mtime;
260 t[1].tv_usec = t[0].tv_usec = 0;
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200261 utimes(dst_name, t);
Denis Vlasenko75103842007-06-20 14:49:47 +0000262 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000263 }
J. Tang77a2c512010-03-19 14:48:51 +0100264
265 ret: ;
266#if ENABLE_FEATURE_TAR_SELINUX
267 if (sctx) {
268 /* reset the context after creating an entry */
269 setfscreatecon(NULL);
270 }
271#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000272}