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 /* NB: _DESTROYS_ str[len] character! */
23 static unsigned long long getOctal(char *str, int len)
26 /* Actually, tar header allows leading spaces also.
27 * Oh well, we will be liberal and skip this...
28 * The only downside probably is that we allow "-123" too :)
29 if (*str < '0' || *str > '7')
30 bb_error_msg_and_die("corrupted octal value in tar header");
33 v = strtoull(str, &str, 8);
35 bb_error_msg_and_die("corrupted octal value in tar header");
39 void BUG_tar_header_size(void);
40 char get_header_tar(archive_handle_t *archive_handle)
44 file_header_t *file_header = archive_handle->file_header;
46 /* ustar header, Posix 1003.1 */
47 char name[100]; /* 0-99 */
48 char mode[8]; /* 100-107 */
49 char uid[8]; /* 108-115 */
50 char gid[8]; /* 116-123 */
51 char size[12]; /* 124-135 */
52 char mtime[12]; /* 136-147 */
53 char chksum[8]; /* 148-155 */
54 char typeflag; /* 156-156 */
55 char linkname[100]; /* 157-256 */
56 char magic[6]; /* 257-262 */
57 char version[2]; /* 263-264 */
58 char uname[32]; /* 265-296 */
59 char gname[32]; /* 297-328 */
60 char devmajor[8]; /* 329-336 */
61 char devminor[8]; /* 337-344 */
62 char prefix[155]; /* 345-499 */
63 char padding[12]; /* 500-512 */
68 if (sizeof(tar) != 512)
69 BUG_tar_header_size();
72 data_align(archive_handle, 512);
74 xread(archive_handle->src_fd, &tar, 512);
75 archive_handle->offset += 512;
77 /* If there is no filename its an empty header */
78 if (tar.name[0] == 0) {
80 /* This is the second consecutive empty header! End of archive!
81 * Read until the end to empty the pipe from gz or bz2
83 while (full_read(archive_handle->src_fd, &tar, 512) == 512)
92 /* Check header has valid magic, "ustar" is for the proper tar
93 * 0's are for the old tar format
95 if (strncmp(tar.magic, "ustar", 5) != 0) {
96 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
97 if (memcmp(tar.magic, "\0\0\0\0", 5) != 0)
99 bb_error_msg_and_die("invalid tar magic");
101 /* Do checksum on headers */
102 sum = ' ' * sizeof(tar.chksum);
103 for (i = 0; i < 148 ; i++) {
104 sum += ((char*)&tar)[i];
106 for (i = 156; i < 512 ; i++) {
107 sum += ((char*)&tar)[i];
109 /* This field does not need special treatment (getOctal) */
110 if (sum != xstrtoul(tar.chksum, 8)) {
111 bb_error_msg_and_die("invalid tar header checksum");
114 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
116 file_header->name = longname;
120 file_header->name = linkname;
125 file_header->name = xstrndup(tar.name, sizeof(tar.name));
127 char *temp = file_header->name;
128 file_header->name = concat_path_file(tar.prefix, temp);
133 /* getOctal trashes subsequent field, therefore we call it
134 * on fields in reverse order */
135 #define GET_OCTAL(a) getOctal((a), sizeof(a))
136 if (tar.devmajor[0]) {
137 unsigned minor = GET_OCTAL(tar.devminor);
138 unsigned major = GET_OCTAL(tar.devmajor);
139 file_header->device = makedev(major, minor);
141 file_header->mtime = GET_OCTAL(tar.mtime);
142 file_header->size = GET_OCTAL(tar.size);
143 file_header->gid = GET_OCTAL(tar.gid);
144 file_header->uid = GET_OCTAL(tar.uid);
145 file_header->link_name = !tar.linkname[0] ? NULL :
146 xstrndup(tar.linkname, sizeof(tar.linkname));
147 /* Set bits 0-11 of the files mode */
148 file_header->mode = 07777 & GET_OCTAL(tar.mode);
151 /* Set bits 12-15 of the files mode */
152 switch (tar.typeflag) {
153 /* busybox identifies hard links as being regular files with 0 size and a link name */
155 file_header->mode |= S_IFREG;
158 /* Reserved for high performance files, treat as normal file */
161 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
162 if (last_char_is(file_header->name, '/')) {
163 file_header->mode |= S_IFDIR;
166 file_header->mode |= S_IFREG;
169 file_header->mode |= S_IFLNK;
172 file_header->mode |= S_IFCHR;
175 file_header->mode |= S_IFBLK;
178 file_header->mode |= S_IFDIR;
181 file_header->mode |= S_IFIFO;
183 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
185 longname = xzalloc(file_header->size + 1);
186 xread(archive_handle->src_fd, longname, file_header->size);
187 archive_handle->offset += file_header->size;
188 return get_header_tar(archive_handle);
190 linkname = xzalloc(file_header->size + 1);
191 xread(archive_handle->src_fd, linkname, file_header->size);
192 archive_handle->offset += file_header->size;
193 file_header->name = linkname;
194 return get_header_tar(archive_handle);
195 case 'D': /* GNU dump dir */
196 case 'M': /* Continuation of multi volume archive */
197 case 'N': /* Old GNU for names > 100 characters */
198 case 'S': /* Sparse file */
199 case 'V': /* Volume header */
201 case 'g': /* pax global header */
202 case 'x': /* pax extended header */
203 bb_error_msg("ignoring extension type %c", tar.typeflag);
206 bb_error_msg("unknown typeflag: 0x%x", tar.typeflag);
209 /* Strip trailing '/' in directories */
210 /* Must be done after mode is set as '/' is used to check if its a directory */
211 cp = last_char_is(file_header->name, '/');
214 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
215 archive_handle->action_header(archive_handle->file_header);
216 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
217 archive_handle->action_data(archive_handle);
218 llist_add_to(&(archive_handle->passed), file_header->name);
220 data_skip(archive_handle);
222 archive_handle->offset += file_header->size;
224 free(file_header->link_name);