1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6 #include "bb_archive.h"
8 void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
10 file_header_t *file_header = archive_handle->file_header;
14 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
17 # define dst_name (file_header->name)
20 #if ENABLE_FEATURE_TAR_SELINUX
21 char *sctx = archive_handle->tar__sctx[PAX_NEXT_FILE];
23 sctx = archive_handle->tar__sctx[PAX_GLOBAL];
24 if (sctx) { /* setfscreatecon is 4 syscalls, avoid if possible */
26 free(archive_handle->tar__sctx[PAX_NEXT_FILE]);
27 archive_handle->tar__sctx[PAX_NEXT_FILE] = NULL;
31 /* Hard links are encoded as regular files of size 0
32 * with a nonempty link field */
34 if (S_ISREG(file_header->mode) && file_header->size == 0)
35 hard_link = file_header->link_target;
37 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
38 dst_name = file_header->name;
39 if (archive_handle->tar__strip_components) {
40 unsigned n = archive_handle->tar__strip_components;
42 dst_name = strchr(dst_name, '/');
43 if (!dst_name || dst_name[1] == '\0') {
44 data_skip(archive_handle);
49 * Link target is shortened only for hardlinks:
50 * softlinks restored unchanged.
53 // 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.
58 hard_link = strchr(hard_link, '/');
59 if (!hard_link || hard_link[1] == '\0') {
60 data_skip(archive_handle);
69 if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
70 char *slash = strrchr(dst_name, '/');
73 bb_make_directory(dst_name, -1, FILEUTILS_RECUR);
78 if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
79 /* Remove the entry if it exists */
80 if (!S_ISDIR(file_header->mode)) {
83 * tar cf t.tar hardlink1 hardlink2 hardlink1
84 * results in this tarball structure:
86 * hardlink2 -> hardlink1
87 * hardlink1 -> hardlink1 <== !!!
89 if (strcmp(hard_link, dst_name) == 0)
92 /* Proceed with deleting */
93 if (unlink(dst_name) == -1
96 bb_perror_msg_and_die("can't remove old file %s",
101 else if (archive_handle->ah_flags & ARCHIVE_EXTRACT_NEWER) {
102 /* Remove the existing entry if its older than the extracted entry */
103 struct stat existing_sb;
104 if (lstat(dst_name, &existing_sb) == -1) {
105 if (errno != ENOENT) {
106 bb_simple_perror_msg_and_die("can't stat old file");
109 else if (existing_sb.st_mtime >= file_header->mtime) {
110 if (!S_ISDIR(file_header->mode)) {
111 bb_error_msg("%s not created: newer or "
112 "same age file exists", dst_name);
114 data_skip(archive_handle);
117 else if ((unlink(dst_name) == -1) && (errno != EISDIR)) {
118 bb_perror_msg_and_die("can't remove old file %s",
123 /* Handle hard links separately */
125 create_or_remember_link(&archive_handle->link_placeholders,
129 /* Hardlinks have no separate mode/ownership, skip chown/chmod */
133 /* Create the filesystem entry */
134 switch (file_header->mode & S_IFMT) {
138 int flags = O_WRONLY | O_CREAT | O_EXCL;
139 if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
140 flags = O_WRONLY | O_CREAT | O_TRUNC;
141 dst_nameN = dst_name;
142 #ifdef ARCHIVE_REPLACE_VIA_RENAME
143 if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME)
144 /* rpm-style temp file name */
145 dst_nameN = xasprintf("%s;%x", dst_name, (int)getpid());
147 dst_fd = xopen3(dst_nameN,
151 bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
153 #ifdef ARCHIVE_REPLACE_VIA_RENAME
154 if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME) {
155 xrename(dst_nameN, dst_name);
162 res = mkdir(dst_name, file_header->mode);
164 && (errno != EISDIR) /* btw, Linux doesn't return this */
167 bb_perror_msg("can't make dir %s", dst_name);
172 //TODO: what if file_header->link_target == NULL (say, corrupted tarball?)
174 /* To avoid a directory traversal attack via symlinks,
175 * do not restore symlinks with ".." components
176 * or symlinks starting with "/", unless a magic
179 * For example, consider a .tar created via:
180 * $ tar cvf bug.tar anything.txt
181 * $ ln -s /tmp symlink
182 * $ tar --append -f bug.tar symlink
185 * $ tar --append -f bug.tar symlink/evil.py
187 * This will result in an archive that contains:
188 * $ tar --list -f bug.tar
193 * Untarring bug.tar would otherwise place evil.py in '/tmp'.
195 create_or_remember_link(&archive_handle->link_placeholders,
196 file_header->link_target,
204 res = mknod(dst_name, file_header->mode, file_header->device);
206 bb_perror_msg("can't create node %s", dst_name);
210 bb_simple_error_msg_and_die("unrecognized file type");
213 if (!S_ISLNK(file_header->mode)) {
214 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
215 uid_t uid = file_header->uid;
216 gid_t gid = file_header->gid;
217 #if ENABLE_FEATURE_TAR_UNAME_GNAME
218 if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
219 if (file_header->tar__uname) {
220 //TODO: cache last name/id pair?
221 struct passwd *pwd = getpwnam(file_header->tar__uname);
222 if (pwd) uid = pwd->pw_uid;
224 if (file_header->tar__gname) {
225 struct group *grp = getgrnam(file_header->tar__gname);
226 if (grp) gid = grp->gr_gid;
230 /* GNU tar 1.15.1 uses chown, not lchown */
231 chown(dst_name, uid, gid);
233 /* uclibc has no lchmod, glibc is even stranger -
234 * it has lchmod which seems to do nothing!
235 * so we use chmod... */
236 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
237 chmod(dst_name, file_header->mode);
239 if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
242 t[1].tv_sec = t[0].tv_sec = file_header->mtime;
243 t[1].tv_usec = t[0].tv_usec = 0;
249 #if ENABLE_FEATURE_TAR_SELINUX
251 /* reset the context after creating an entry */
252 setfscreatecon(NULL);