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"
18 * GNU tar uses "base-256 encoding" for very large numbers (>8 billion).
19 * Encoding is binary, with highest bit always set as a marker
20 * and sign in next-highest bit:
22 * bf ff .. ff - largest positive number
23 * ff ff .. ff - minus 1
24 * c0 00 .. 00 - smallest negative number
26 * We expect it only in size field, where negative numbers don't make sense.
28 static off_t getBase256_len12(const char *str)
33 /* if (*str & 0x40) error; - caller prevents this */
35 if (sizeof(off_t) >= 12) {
36 /* Probably 128-bit (16 byte) off_t. Can be optimized. */
38 value = *str++ & 0x3f;
40 value = (value << 8) + (unsigned char) *str++;
44 #ifdef CHECK_FOR_OVERFLOW
45 /* Can be optimized to eat 32-bit chunks */
46 char c = *str++ & 0x3f;
50 bb_error_msg_and_die("overflow in base-256 encoded file size");
51 if (--len == sizeof(off_t))
56 str += (12 - sizeof(off_t));
59 /* Now str points to sizeof(off_t) least significant bytes.
61 * Example of tar file with 8914993153 (0x213600001) byte file.
62 * Field starts at offset 7c:
63 * 00070 30 30 30 00 30 30 30 30 30 30 30 00 80 00 00 00 |000.0000000.....|
64 * 00080 00 00 00 02 13 60 00 01 31 31 31 32 30 33 33 36 |.....`..11120336|
66 * str is at offset 80 or 84 now (64-bit or 32-bit off_t).
67 * We (ab)use the fact that value happens to be aligned,
68 * and fetch it in one go:
70 if (sizeof(off_t) == 8) {
72 value = SWAP_BE64(value);
73 } else if (sizeof(off_t) == 4) {
75 value = SWAP_BE32(value);
80 value = (value << 8) + (unsigned char) *str++;
85 /* NB: _DESTROYS_ str[len] character! */
86 static unsigned long long getOctal(char *str, int len)
89 /* NB: leading spaces are allowed. Using strtoull to handle that.
90 * The downside is that we accept e.g. "-123" too :)
93 v = strtoull(str, &str, 8);
94 if (*str && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY || *str != ' '))
95 bb_error_msg_and_die("corrupted octal value in tar header");
98 #define GET_OCTAL(a) getOctal((a), sizeof(a))
100 void BUG_tar_header_size(void);
101 char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
103 file_header_t *file_header = archive_handle->file_header;
105 /* ustar header, Posix 1003.1 */
106 char name[100]; /* 0-99 */
107 char mode[8]; /* 100-107 */
108 char uid[8]; /* 108-115 */
109 char gid[8]; /* 116-123 */
110 char size[12]; /* 124-135 */
111 char mtime[12]; /* 136-147 */
112 char chksum[8]; /* 148-155 */
113 char typeflag; /* 156-156 */
114 char linkname[100]; /* 157-256 */
115 /* POSIX: "ustar" NUL "00" */
116 /* GNU tar: "ustar " NUL */
117 /* Normally it's defined as magic[6] followed by
118 * version[2], but we put them together to simplify code
120 char magic[8]; /* 257-264 */
121 char uname[32]; /* 265-296 */
122 char gname[32]; /* 297-328 */
123 char devmajor[8]; /* 329-336 */
124 char devminor[8]; /* 337-344 */
125 char prefix[155]; /* 345-499 */
126 char padding[12]; /* 500-512 */
130 #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
135 /* Our "private data" */
136 #define p_end (*(smallint *)(&archive_handle->ah_priv[0]))
137 #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
138 #define p_longname (*(char* *)(&archive_handle->ah_priv[1]))
139 #define p_linkname (*(char* *)(&archive_handle->ah_priv[2]))
144 // if (!archive_handle->ah_priv_inited) {
145 // archive_handle->ah_priv_inited = 1;
147 // IF_FEATURE_TAR_GNU_EXTENSIONS(p_longname = NULL;)
148 // IF_FEATURE_TAR_GNU_EXTENSIONS(p_linkname = NULL;)
151 if (sizeof(tar) != 512)
152 BUG_tar_header_size();
154 #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
158 data_align(archive_handle, 512);
162 #if ENABLE_DESKTOP || ENABLE_FEATURE_TAR_AUTODETECT
163 /* to prevent misdetection of bz2 sig */
164 *(uint32_t*)(&tar) = 0;
165 i = full_read(archive_handle->src_fd, &tar, 512);
166 /* If GNU tar sees EOF in above read, it says:
167 * "tar: A lone zero block at N", where N = kilobyte
168 * where EOF was met (not EOF block, actual EOF!),
169 * and exits with EXIT_SUCCESS.
170 * We will mimic exit(EXIT_SUCCESS), although we will not mimic
171 * the message and we don't check whether we indeed
172 * saw zero block directly before this. */
174 xfunc_error_retval = 0;
176 bb_error_msg_and_die("short read");
179 IF_FEATURE_TAR_AUTODETECT(goto autodetect;)
185 xread(archive_handle->src_fd, &tar, i);
187 archive_handle->offset += i;
189 /* If there is no filename its an empty header */
190 if (tar.name[0] == 0 && tar.prefix[0] == 0) {
192 /* Second consecutive empty header - end of archive.
193 * Read until the end to empty the pipe from gz or bz2
195 while (full_read(archive_handle->src_fd, &tar, 512) == 512)
204 /* Check header has valid magic, "ustar" is for the proper tar,
205 * five NULs are for the old tar format */
206 if (strncmp(tar.magic, "ustar", 5) != 0
207 && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
208 || memcmp(tar.magic, "\0\0\0\0", 5) != 0)
210 #if ENABLE_FEATURE_TAR_AUTODETECT
211 char FAST_FUNC (*get_header_ptr)(archive_handle_t *);
214 /* tar gz/bz autodetect: check for gz/bz2 magic.
215 * If we see the magic, and it is the very first block,
216 * we can switch to get_header_tar_gz/bz2/lzma().
217 * Needs seekable fd. I wish recv(MSG_PEEK) works
219 #if ENABLE_FEATURE_SEAMLESS_GZ
220 if (tar.name[0] == 0x1f && tar.name[1] == (char)0x8b) { /* gzip */
221 get_header_ptr = get_header_tar_gz;
224 #if ENABLE_FEATURE_SEAMLESS_BZ2
225 if (tar.name[0] == 'B' && tar.name[1] == 'Z'
226 && tar.name[2] == 'h' && isdigit(tar.name[3])
228 get_header_ptr = get_header_tar_bz2;
232 /* Two different causes for lseek() != 0:
233 * unseekable fd (would like to support that too, but...),
234 * or not first block (false positive, it's not .gz/.bz2!) */
235 if (lseek(archive_handle->src_fd, -i, SEEK_CUR) != 0)
237 while (get_header_ptr(archive_handle) == EXIT_SUCCESS)
241 #endif /* FEATURE_TAR_AUTODETECT */
242 bb_error_msg_and_die("invalid tar magic");
245 /* Do checksum on headers.
246 * POSIX says that checksum is done on unsigned bytes, but
247 * Sun and HP-UX gets it wrong... more details in
249 #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
250 sum_s = ' ' * sizeof(tar.chksum);
252 sum_u = ' ' * sizeof(tar.chksum);
253 for (i = 0; i < 148; i++) {
254 sum_u += ((unsigned char*)&tar)[i];
255 #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
256 sum_s += ((signed char*)&tar)[i];
259 for (i = 156; i < 512; i++) {
260 sum_u += ((unsigned char*)&tar)[i];
261 #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
262 sum_s += ((signed char*)&tar)[i];
265 #if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
266 sum = strtoul(tar.chksum, &cp, 8);
267 if ((*cp && *cp != ' ')
268 || (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum))
270 bb_error_msg_and_die("invalid tar header checksum");
273 /* This field does not need special treatment (getOctal) */
274 sum = xstrtoul(tar.chksum, 8);
275 if (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) {
276 bb_error_msg_and_die("invalid tar header checksum");
280 /* 0 is reserved for high perf file, treat as normal file */
281 if (!tar.typeflag) tar.typeflag = '0';
282 parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
284 /* getOctal trashes subsequent field, therefore we call it
285 * on fields in reverse order */
286 if (tar.devmajor[0]) {
287 char t = tar.prefix[0];
288 /* we trash prefix[0] here, but we DO need it later! */
289 unsigned minor = GET_OCTAL(tar.devminor);
290 unsigned major = GET_OCTAL(tar.devmajor);
291 file_header->device = makedev(major, minor);
294 file_header->link_target = NULL;
295 if (!p_linkname && parse_names && tar.linkname[0]) {
296 file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname));
297 /* FIXME: what if we have non-link object with link_target? */
298 /* Will link_target be free()ed? */
300 #if ENABLE_FEATURE_TAR_UNAME_GNAME
301 file_header->uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL;
302 file_header->gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL;
304 file_header->mtime = GET_OCTAL(tar.mtime);
305 /* Size field: handle GNU tar's "base256 encoding" */
306 file_header->size = (*tar.size & 0xc0) == 0x80 /* positive base256? */
307 ? getBase256_len12(tar.size)
308 : GET_OCTAL(tar.size);
309 file_header->gid = GET_OCTAL(tar.gid);
310 file_header->uid = GET_OCTAL(tar.uid);
311 /* Set bits 0-11 of the files mode */
312 file_header->mode = 07777 & GET_OCTAL(tar.mode);
314 file_header->name = NULL;
315 if (!p_longname && parse_names) {
316 /* we trash mode[0] here, it's ok */
317 //tar.name[sizeof(tar.name)] = '\0'; - gcc 4.3.0 would complain
321 //tar.prefix[sizeof(tar.prefix)] = '\0'; - gcc 4.3.0 would complain
322 tar.padding[0] = '\0';
323 file_header->name = concat_path_file(tar.prefix, tar.name);
325 file_header->name = xstrdup(tar.name);
328 /* Set bits 12-15 of the files mode */
329 /* (typeflag was not trashed because chksum does not use getOctal) */
330 switch (tar.typeflag) {
331 /* busybox identifies hard links as being regular files with 0 size and a link name */
333 file_header->mode |= S_IFREG;
338 #if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
339 if (last_char_is(file_header->name, '/')) {
343 file_header->mode |= S_IFREG;
346 file_header->mode |= S_IFLNK;
347 /* have seen tarballs with size field containing
348 * the size of the link target's name */
350 file_header->size = 0;
353 file_header->mode |= S_IFCHR;
354 goto size0; /* paranoia */
356 file_header->mode |= S_IFBLK;
359 IF_FEATURE_TAR_OLDGNU_COMPATIBILITY(set_dir:)
360 file_header->mode |= S_IFDIR;
363 file_header->mode |= S_IFIFO;
365 #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
367 /* free: paranoia: tar with several consecutive longnames */
369 /* For paranoia reasons we allocate extra NUL char */
370 p_longname = xzalloc(file_header->size + 1);
371 /* We read ASCIZ string, including NUL */
372 xread(archive_handle->src_fd, p_longname, file_header->size);
373 archive_handle->offset += file_header->size;
374 /* return get_header_tar(archive_handle); */
375 /* gcc 4.1.1 didn't optimize it into jump */
376 /* so we will do it ourself, this also saves stack */
380 p_linkname = xzalloc(file_header->size + 1);
381 xread(archive_handle->src_fd, p_linkname, file_header->size);
382 archive_handle->offset += file_header->size;
383 /* return get_header_tar(archive_handle); */
385 case 'D': /* GNU dump dir */
386 case 'M': /* Continuation of multi volume archive */
387 case 'N': /* Old GNU for names > 100 characters */
388 case 'S': /* Sparse file */
389 case 'V': /* Volume header */
391 case 'g': /* pax global header */
392 case 'x': { /* pax extended header */
394 bb_error_msg("warning: skipping header '%c'", tar.typeflag);
395 sz = (file_header->size + 511) & ~(off_t)511;
396 archive_handle->offset += sz;
397 sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
399 xread(archive_handle->src_fd, &tar, 512);
400 /* return get_header_tar(archive_handle); */
401 goto again_after_align;
404 bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag);
407 #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
409 file_header->name = p_longname;
413 file_header->link_target = p_linkname;
417 if (strncmp(file_header->name, "/../"+1, 3) == 0
418 || strstr(file_header->name, "/../")
420 bb_error_msg_and_die("name with '..' encountered: '%s'",
424 /* Strip trailing '/' in directories */
425 /* Must be done after mode is set as '/' is used to check if it's a directory */
426 cp = last_char_is(file_header->name, '/');
428 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
429 archive_handle->action_header(/*archive_handle->*/ file_header);
430 /* Note that we kill the '/' only after action_header() */
431 /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
433 archive_handle->ah_flags |= ARCHIVE_EXTRACT_QUIET;
434 archive_handle->action_data(archive_handle);
435 llist_add_to(&(archive_handle->passed), file_header->name);
437 data_skip(archive_handle);
438 free(file_header->name);
440 archive_handle->offset += file_header->size;
442 free(file_header->link_target);
443 /* Do not free(file_header->name)! (why?) */
444 #if ENABLE_FEATURE_TAR_UNAME_GNAME
445 free(file_header->uname);
446 free(file_header->gname);