2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-03 19:35:15 +05:30
|
|
|
/*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2002-11-03 19:35:15 +05:30
|
|
|
*/
|
2002-09-25 08:17:48 +05:30
|
|
|
#include "libbb.h"
|
2011-09-22 16:15:14 +05:30
|
|
|
#include "bb_archive.h"
|
2002-09-25 08:17:48 +05:30
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
|
2002-09-25 08:17:48 +05:30
|
|
|
{
|
|
|
|
file_header_t *file_header = archive_handle->file_header;
|
|
|
|
int dst_fd;
|
|
|
|
int res;
|
2015-10-22 17:00:34 +05:30
|
|
|
char *hard_link;
|
2015-10-22 04:37:13 +05:30
|
|
|
#if ENABLE_FEATURE_TAR_LONG_OPTIONS
|
|
|
|
char *dst_name;
|
|
|
|
#else
|
|
|
|
# define dst_name (file_header->name)
|
|
|
|
#endif
|
2002-09-25 08:17:48 +05:30
|
|
|
|
2010-03-19 19:18:51 +05:30
|
|
|
#if ENABLE_FEATURE_TAR_SELINUX
|
2012-02-23 18:15:18 +05:30
|
|
|
char *sctx = archive_handle->tar__sctx[PAX_NEXT_FILE];
|
2010-03-19 19:18:51 +05:30
|
|
|
if (!sctx)
|
2012-02-23 18:15:18 +05:30
|
|
|
sctx = archive_handle->tar__sctx[PAX_GLOBAL];
|
2010-03-19 19:18:51 +05:30
|
|
|
if (sctx) { /* setfscreatecon is 4 syscalls, avoid if possible */
|
|
|
|
setfscreatecon(sctx);
|
2012-02-23 18:15:18 +05:30
|
|
|
free(archive_handle->tar__sctx[PAX_NEXT_FILE]);
|
|
|
|
archive_handle->tar__sctx[PAX_NEXT_FILE] = NULL;
|
2010-03-19 19:18:51 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-22 17:00:34 +05:30
|
|
|
/* Hard links are encoded as regular files of size 0
|
|
|
|
* with a nonempty link field */
|
|
|
|
hard_link = NULL;
|
|
|
|
if (S_ISREG(file_header->mode) && file_header->size == 0)
|
|
|
|
hard_link = file_header->link_target;
|
|
|
|
|
2015-10-22 04:37:13 +05:30
|
|
|
#if ENABLE_FEATURE_TAR_LONG_OPTIONS
|
|
|
|
dst_name = file_header->name;
|
|
|
|
if (archive_handle->tar__strip_components) {
|
|
|
|
unsigned n = archive_handle->tar__strip_components;
|
|
|
|
do {
|
|
|
|
dst_name = strchr(dst_name, '/');
|
|
|
|
if (!dst_name || dst_name[1] == '\0') {
|
|
|
|
data_skip(archive_handle);
|
2015-10-22 17:08:09 +05:30
|
|
|
goto ret;
|
2015-10-22 04:37:13 +05:30
|
|
|
}
|
|
|
|
dst_name++;
|
|
|
|
/*
|
|
|
|
* Link target is shortened only for hardlinks:
|
|
|
|
* softlinks restored unchanged.
|
|
|
|
*/
|
2015-10-22 17:00:34 +05:30
|
|
|
if (hard_link) {
|
2015-10-22 04:37:13 +05:30
|
|
|
// GNU tar 1.26 does not check that we reached end of link name:
|
|
|
|
// if "dir/hardlink" is hardlinked to "file",
|
|
|
|
// tar xvf a.tar --strip-components=1 says:
|
|
|
|
// tar: hardlink: Cannot hard link to '': No such file or directory
|
|
|
|
// and continues processing. We silently skip such entries.
|
2015-10-22 17:00:34 +05:30
|
|
|
hard_link = strchr(hard_link, '/');
|
|
|
|
if (!hard_link || hard_link[1] == '\0') {
|
2015-10-22 04:37:13 +05:30
|
|
|
data_skip(archive_handle);
|
2015-10-22 17:08:09 +05:30
|
|
|
goto ret;
|
2015-10-22 04:37:13 +05:30
|
|
|
}
|
2015-10-22 17:00:34 +05:30
|
|
|
hard_link++;
|
2015-10-22 04:37:13 +05:30
|
|
|
}
|
|
|
|
} while (--n != 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-06-28 10:34:09 +05:30
|
|
|
if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
|
2015-10-22 04:37:13 +05:30
|
|
|
char *slash = strrchr(dst_name, '/');
|
2009-11-29 12:15:33 +05:30
|
|
|
if (slash) {
|
|
|
|
*slash = '\0';
|
2015-10-22 04:37:13 +05:30
|
|
|
bb_make_directory(dst_name, -1, FILEUTILS_RECUR);
|
2009-11-29 12:15:33 +05:30
|
|
|
*slash = '/';
|
|
|
|
}
|
2004-03-15 13:59:22 +05:30
|
|
|
}
|
2002-09-25 08:17:48 +05:30
|
|
|
|
2009-09-17 06:13:14 +05:30
|
|
|
if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
|
2008-04-05 08:14:30 +05:30
|
|
|
/* Remove the entry if it exists */
|
2010-04-09 14:22:52 +05:30
|
|
|
if (!S_ISDIR(file_header->mode)) {
|
2015-10-22 17:00:34 +05:30
|
|
|
if (hard_link) {
|
2010-04-09 14:22:52 +05:30
|
|
|
/* Ugly special case:
|
|
|
|
* tar cf t.tar hardlink1 hardlink2 hardlink1
|
|
|
|
* results in this tarball structure:
|
|
|
|
* hardlink1
|
|
|
|
* hardlink2 -> hardlink1
|
|
|
|
* hardlink1 -> hardlink1 <== !!!
|
|
|
|
*/
|
2015-10-22 17:00:34 +05:30
|
|
|
if (strcmp(hard_link, dst_name) == 0)
|
2010-04-09 14:22:52 +05:30
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
/* Proceed with deleting */
|
2015-10-22 04:37:13 +05:30
|
|
|
if (unlink(dst_name) == -1
|
2010-04-09 14:22:52 +05:30
|
|
|
&& errno != ENOENT
|
|
|
|
) {
|
|
|
|
bb_perror_msg_and_die("can't remove old file %s",
|
2015-10-22 04:37:13 +05:30
|
|
|
dst_name);
|
2010-04-09 14:22:52 +05:30
|
|
|
}
|
2003-08-29 00:42:23 +05:30
|
|
|
}
|
|
|
|
}
|
2008-06-28 10:34:09 +05:30
|
|
|
else if (archive_handle->ah_flags & ARCHIVE_EXTRACT_NEWER) {
|
2003-08-29 00:42:23 +05:30
|
|
|
/* Remove the existing entry if its older than the extracted entry */
|
2009-11-24 05:46:12 +05:30
|
|
|
struct stat existing_sb;
|
2015-10-22 04:37:13 +05:30
|
|
|
if (lstat(dst_name, &existing_sb) == -1) {
|
2003-11-16 05:14:31 +05:30
|
|
|
if (errno != ENOENT) {
|
2009-09-17 06:13:14 +05:30
|
|
|
bb_perror_msg_and_die("can't stat old file");
|
2003-11-16 05:14:31 +05:30
|
|
|
}
|
2003-08-29 00:42:23 +05:30
|
|
|
}
|
2009-11-24 05:46:12 +05:30
|
|
|
else if (existing_sb.st_mtime >= file_header->mtime) {
|
2018-02-20 20:27:45 +05:30
|
|
|
if (!S_ISDIR(file_header->mode)) {
|
2006-10-11 02:30:47 +05:30
|
|
|
bb_error_msg("%s not created: newer or "
|
2015-10-22 04:37:13 +05:30
|
|
|
"same age file exists", dst_name);
|
2003-08-29 00:42:23 +05:30
|
|
|
}
|
|
|
|
data_skip(archive_handle);
|
2010-03-19 19:18:51 +05:30
|
|
|
goto ret;
|
2003-08-29 00:42:23 +05:30
|
|
|
}
|
2015-10-22 04:37:13 +05:30
|
|
|
else if ((unlink(dst_name) == -1) && (errno != EISDIR)) {
|
2009-09-17 06:13:14 +05:30
|
|
|
bb_perror_msg_and_die("can't remove old file %s",
|
2015-10-22 04:37:13 +05:30
|
|
|
dst_name);
|
2003-08-29 00:42:23 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 17:00:34 +05:30
|
|
|
/* Handle hard links separately */
|
|
|
|
if (hard_link) {
|
2018-05-22 21:04:31 +05:30
|
|
|
create_or_remember_link(&archive_handle->link_placeholders,
|
|
|
|
hard_link,
|
|
|
|
dst_name,
|
|
|
|
1);
|
2010-04-09 17:41:45 +05:30
|
|
|
/* Hardlinks have no separate mode/ownership, skip chown/chmod */
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the filesystem entry */
|
|
|
|
switch (file_header->mode & S_IFMT) {
|
|
|
|
case S_IFREG: {
|
|
|
|
/* Regular file */
|
2015-10-22 04:37:13 +05:30
|
|
|
char *dst_nameN;
|
2010-04-09 17:41:45 +05:30
|
|
|
int flags = O_WRONLY | O_CREAT | O_EXCL;
|
|
|
|
if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
|
|
|
|
flags = O_WRONLY | O_CREAT | O_TRUNC;
|
2015-10-22 04:37:13 +05:30
|
|
|
dst_nameN = dst_name;
|
2013-02-20 20:28:42 +05:30
|
|
|
#ifdef ARCHIVE_REPLACE_VIA_RENAME
|
|
|
|
if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME)
|
|
|
|
/* rpm-style temp file name */
|
2015-10-22 04:37:13 +05:30
|
|
|
dst_nameN = xasprintf("%s;%x", dst_name, (int)getpid());
|
2013-02-20 20:28:42 +05:30
|
|
|
#endif
|
2015-10-22 04:37:13 +05:30
|
|
|
dst_fd = xopen3(dst_nameN,
|
2010-04-09 17:41:45 +05:30
|
|
|
flags,
|
|
|
|
file_header->mode
|
|
|
|
);
|
|
|
|
bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
|
|
|
|
close(dst_fd);
|
2013-02-20 20:28:42 +05:30
|
|
|
#ifdef ARCHIVE_REPLACE_VIA_RENAME
|
|
|
|
if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME) {
|
2015-10-22 04:37:13 +05:30
|
|
|
xrename(dst_nameN, dst_name);
|
|
|
|
free(dst_nameN);
|
2013-02-20 20:28:42 +05:30
|
|
|
}
|
|
|
|
#endif
|
2010-04-09 17:41:45 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
case S_IFDIR:
|
2015-10-22 04:37:13 +05:30
|
|
|
res = mkdir(dst_name, file_header->mode);
|
2018-02-20 20:27:45 +05:30
|
|
|
if ((res != 0)
|
2010-04-09 17:41:45 +05:30
|
|
|
&& (errno != EISDIR) /* btw, Linux doesn't return this */
|
|
|
|
&& (errno != EEXIST)
|
|
|
|
) {
|
2015-10-22 04:37:13 +05:30
|
|
|
bb_perror_msg("can't make dir %s", dst_name);
|
2006-12-22 05:51:07 +05:30
|
|
|
}
|
2010-04-09 17:41:45 +05:30
|
|
|
break;
|
|
|
|
case S_IFLNK:
|
|
|
|
/* Symlink */
|
2010-07-04 07:56:55 +05:30
|
|
|
//TODO: what if file_header->link_target == NULL (say, corrupted tarball?)
|
2017-07-24 20:50:13 +05:30
|
|
|
|
|
|
|
/* To avoid a directory traversal attack via symlinks,
|
2017-08-10 15:22:42 +05:30
|
|
|
* do not restore symlinks with ".." components
|
|
|
|
* or symlinks starting with "/", unless a magic
|
|
|
|
* envvar is set.
|
2017-07-24 20:50:13 +05:30
|
|
|
*
|
|
|
|
* For example, consider a .tar created via:
|
|
|
|
* $ tar cvf bug.tar anything.txt
|
|
|
|
* $ ln -s /tmp symlink
|
|
|
|
* $ tar --append -f bug.tar symlink
|
|
|
|
* $ rm symlink
|
|
|
|
* $ mkdir symlink
|
|
|
|
* $ tar --append -f bug.tar symlink/evil.py
|
|
|
|
*
|
|
|
|
* This will result in an archive that contains:
|
|
|
|
* $ tar --list -f bug.tar
|
|
|
|
* anything.txt
|
|
|
|
* symlink [-> /tmp]
|
|
|
|
* symlink/evil.py
|
|
|
|
*
|
|
|
|
* Untarring bug.tar would otherwise place evil.py in '/tmp'.
|
|
|
|
*/
|
2018-05-22 21:04:31 +05:30
|
|
|
create_or_remember_link(&archive_handle->link_placeholders,
|
2018-02-20 20:27:45 +05:30
|
|
|
file_header->link_target,
|
2018-05-22 21:04:31 +05:30
|
|
|
dst_name,
|
|
|
|
0);
|
2010-04-09 17:41:45 +05:30
|
|
|
break;
|
|
|
|
case S_IFSOCK:
|
|
|
|
case S_IFBLK:
|
|
|
|
case S_IFCHR:
|
|
|
|
case S_IFIFO:
|
2015-10-22 04:37:13 +05:30
|
|
|
res = mknod(dst_name, file_header->mode, file_header->device);
|
2018-02-20 20:27:45 +05:30
|
|
|
if (res != 0) {
|
2015-10-22 04:37:13 +05:30
|
|
|
bb_perror_msg("can't create node %s", dst_name);
|
2002-09-25 08:17:48 +05:30
|
|
|
}
|
2010-04-09 17:41:45 +05:30
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bb_error_msg_and_die("unrecognized file type");
|
2002-09-25 08:17:48 +05:30
|
|
|
}
|
|
|
|
|
2010-06-18 05:30:55 +05:30
|
|
|
if (!S_ISLNK(file_header->mode)) {
|
|
|
|
if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
|
2009-04-21 06:22:21 +05:30
|
|
|
uid_t uid = file_header->uid;
|
|
|
|
gid_t gid = file_header->gid;
|
2010-06-18 05:30:55 +05:30
|
|
|
#if ENABLE_FEATURE_TAR_UNAME_GNAME
|
|
|
|
if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
|
|
|
|
if (file_header->tar__uname) {
|
2009-11-29 12:15:33 +05:30
|
|
|
//TODO: cache last name/id pair?
|
2010-06-18 05:30:55 +05:30
|
|
|
struct passwd *pwd = getpwnam(file_header->tar__uname);
|
|
|
|
if (pwd) uid = pwd->pw_uid;
|
|
|
|
}
|
|
|
|
if (file_header->tar__gname) {
|
|
|
|
struct group *grp = getgrnam(file_header->tar__gname);
|
|
|
|
if (grp) gid = grp->gr_gid;
|
|
|
|
}
|
2009-04-21 06:22:21 +05:30
|
|
|
}
|
2010-06-18 05:30:55 +05:30
|
|
|
#endif
|
2009-11-29 12:15:33 +05:30
|
|
|
/* GNU tar 1.15.1 uses chown, not lchown */
|
2015-10-22 04:37:13 +05:30
|
|
|
chown(dst_name, uid, gid);
|
2010-06-18 05:30:55 +05:30
|
|
|
}
|
2007-06-20 20:19:47 +05:30
|
|
|
/* uclibc has no lchmod, glibc is even stranger -
|
|
|
|
* it has lchmod which seems to do nothing!
|
|
|
|
* so we use chmod... */
|
2009-09-17 06:13:14 +05:30
|
|
|
if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
|
2015-10-22 04:37:13 +05:30
|
|
|
chmod(dst_name, file_header->mode);
|
2007-06-20 20:19:47 +05:30
|
|
|
}
|
2009-09-17 06:13:14 +05:30
|
|
|
if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
|
2009-11-30 00:10:36 +05:30
|
|
|
struct timeval t[2];
|
2009-11-15 06:58:56 +05:30
|
|
|
|
2009-11-30 00:10:36 +05:30
|
|
|
t[1].tv_sec = t[0].tv_sec = file_header->mtime;
|
|
|
|
t[1].tv_usec = t[0].tv_usec = 0;
|
2015-10-22 04:37:13 +05:30
|
|
|
utimes(dst_name, t);
|
2007-06-20 20:19:47 +05:30
|
|
|
}
|
2002-09-25 08:17:48 +05:30
|
|
|
}
|
2010-03-19 19:18:51 +05:30
|
|
|
|
|
|
|
ret: ;
|
|
|
|
#if ENABLE_FEATURE_TAR_SELINUX
|
|
|
|
if (sctx) {
|
|
|
|
/* reset the context after creating an entry */
|
|
|
|
setfscreatecon(NULL);
|
|
|
|
}
|
|
|
|
#endif
|
2002-09-25 08:17:48 +05:30
|
|
|
}
|