1 /* vi: set sw=4 ts=4: */
2 /* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
5 * In privileged mode if uname and gname map to a uid and gid then use the
6 * mapped value instead of the uid/gid values in tar header
9 * GNU tar and star man pages,
10 * Opengroup's ustar interchange format,
11 * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
15 #include "unarchive.h"
17 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
18 static char *longname = NULL;
19 static char *linkname = NULL;
22 char get_header_tar(archive_handle_t *archive_handle)
24 file_header_t *file_header = archive_handle->file_header;
26 /* ustar header, Posix 1003.1 */
27 unsigned char raw[512];
29 char name[100]; /* 0-99 */
30 char mode[8]; /* 100-107 */
31 char uid[8]; /* 108-115 */
32 char gid[8]; /* 116-123 */
33 char size[12]; /* 124-135 */
34 char mtime[12]; /* 136-147 */
35 char chksum[8]; /* 148-155 */
36 char typeflag; /* 156-156 */
37 char linkname[100]; /* 157-256 */
38 char magic[6]; /* 257-262 */
39 char version[2]; /* 263-264 */
40 char uname[32]; /* 265-296 */
41 char gname[32]; /* 297-328 */
42 char devmajor[8]; /* 329-336 */
43 char devminor[8]; /* 337-344 */
44 char prefix[155]; /* 345-499 */
45 char padding[12]; /* 500-512 */
53 data_align(archive_handle, 512);
55 xread(archive_handle->src_fd, tar.raw, 512);
56 archive_handle->offset += 512;
58 /* If there is no filename its an empty header */
59 if (tar.formatted.name[0] == 0) {
61 /* This is the second consecutive empty header! End of archive!
62 * Read until the end to empty the pipe from gz or bz2
64 while (full_read(archive_handle->src_fd, tar.raw, 512) == 512);
72 /* Check header has valid magic, "ustar" is for the proper tar
73 * 0's are for the old tar format
75 if (strncmp(tar.formatted.magic, "ustar", 5) != 0) {
76 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
77 if (strncmp(tar.formatted.magic, "\0\0\0\0\0", 5) != 0)
79 bb_error_msg_and_die("invalid tar magic");
81 /* Do checksum on headers */
82 for (i = 0; i < 148 ; i++) {
86 for (i = 156; i < 512 ; i++) {
89 if (sum != xstrtoul(tar.formatted.chksum, 8)) {
90 bb_error_msg_and_die("invalid tar header checksum");
93 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
95 file_header->name = longname;
99 file_header->name = linkname;
104 file_header->name = xstrndup(tar.formatted.name, 100);
105 if (tar.formatted.prefix[0]) {
106 char *temp = file_header->name;
107 file_header->name = concat_path_file(tar.formatted.prefix, temp);
112 file_header->uid = xstrtoul(tar.formatted.uid, 8);
113 file_header->gid = xstrtoul(tar.formatted.gid, 8);
115 file_header->size = xstrtoul(tar.formatted.size, 8);
116 file_header->mtime = xstrtoul(tar.formatted.mtime, 8);
117 file_header->link_name = tar.formatted.linkname[0] ?
118 xstrdup(tar.formatted.linkname) : NULL;
119 file_header->device = makedev(xstrtoul(tar.formatted.devmajor, 8),
120 xstrtoul(tar.formatted.devminor, 8));
122 /* Set bits 0-11 of the files mode */
123 file_header->mode = 07777 & xstrtoul(tar.formatted.mode, 8);
125 /* Set bits 12-15 of the files mode */
126 switch (tar.formatted.typeflag) {
127 /* busybox identifies hard links as being regular files with 0 size and a link name */
129 file_header->mode |= S_IFREG;
132 /* Reserved for high performance files, treat as normal file */
135 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
136 if (last_char_is(file_header->name, '/')) {
137 file_header->mode |= S_IFDIR;
140 file_header->mode |= S_IFREG;
143 file_header->mode |= S_IFLNK;
146 file_header->mode |= S_IFCHR;
149 file_header->mode |= S_IFBLK;
152 file_header->mode |= S_IFDIR;
155 file_header->mode |= S_IFIFO;
157 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
159 longname = xzalloc(file_header->size + 1);
160 xread(archive_handle->src_fd, longname, file_header->size);
161 archive_handle->offset += file_header->size;
163 return get_header_tar(archive_handle);
166 linkname = xzalloc(file_header->size + 1);
167 xread(archive_handle->src_fd, linkname, file_header->size);
168 archive_handle->offset += file_header->size;
170 file_header->name = linkname;
171 return get_header_tar(archive_handle);
173 case 'D': /* GNU dump dir */
174 case 'M': /* Continuation of multi volume archive*/
175 case 'N': /* Old GNU for names > 100 characters */
176 case 'S': /* Sparse file */
177 case 'V': /* Volume header */
179 case 'g': /* pax global header */
180 case 'x': /* pax extended header */
181 bb_error_msg("ignoring extension type %c", tar.formatted.typeflag);
184 bb_error_msg("unknown typeflag: 0x%x", tar.formatted.typeflag);
186 { /* Strip trailing '/' in directories */
187 /* Must be done after mode is set as '/' is used to check if its a directory */
188 char *tmp = last_char_is(file_header->name, '/');
194 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
195 archive_handle->action_header(archive_handle->file_header);
196 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
197 archive_handle->action_data(archive_handle);
198 llist_add_to(&(archive_handle->passed), file_header->name);
200 data_skip(archive_handle);
202 archive_handle->offset += file_header->size;
204 free(file_header->link_name);