2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2010-08-16 18:14:46 +00:00
|
|
|
/* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2004-02-17 11:55:06 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* FIXME:
|
2004-04-14 17:51:38 +00:00
|
|
|
* In privileged mode if uname and gname map to a uid and gid then use the
|
2004-02-20 02:25:18 +00:00
|
|
|
* mapped value instead of the uid/gid values in tar header
|
2004-02-19 08:48:30 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* References:
|
2004-02-19 08:48:30 +00:00
|
|
|
* GNU tar and star man pages,
|
|
|
|
* Opengroup's ustar interchange format,
|
2010-08-16 18:14:46 +00:00
|
|
|
* http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
|
2001-10-25 14:18:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
2011-09-22 10:45:14 +00:00
|
|
|
#include "bb_archive.h"
|
2001-10-25 14:18:08 +00:00
|
|
|
|
2010-02-04 14:00:15 +00:00
|
|
|
typedef uint32_t aliased_uint32_t FIX_ALIASING;
|
|
|
|
typedef off_t aliased_off_t FIX_ALIASING;
|
|
|
|
|
2006-11-24 14:51:01 +00:00
|
|
|
/* NB: _DESTROYS_ str[len] character! */
|
|
|
|
static unsigned long long getOctal(char *str, int len)
|
|
|
|
{
|
|
|
|
unsigned long long v;
|
2010-05-06 18:08:14 +00:00
|
|
|
char *end;
|
2008-06-28 05:04:09 +00:00
|
|
|
/* NB: leading spaces are allowed. Using strtoull to handle that.
|
2009-09-23 15:16:37 +00:00
|
|
|
* The downside is that we accept e.g. "-123" too :(
|
2008-06-28 05:04:09 +00:00
|
|
|
*/
|
2006-11-24 14:51:01 +00:00
|
|
|
str[len] = '\0';
|
2010-05-06 18:08:14 +00:00
|
|
|
v = strtoull(str, &end, 8);
|
2009-09-23 15:16:37 +00:00
|
|
|
/* std: "Each numeric field is terminated by one or more
|
|
|
|
* <space> or NUL characters". We must support ' '! */
|
2010-05-06 18:08:14 +00:00
|
|
|
if (*end != '\0' && *end != ' ') {
|
|
|
|
int8_t first = str[0];
|
|
|
|
if (!(first & 0x80))
|
|
|
|
bb_error_msg_and_die("corrupted octal value in tar header");
|
|
|
|
/*
|
|
|
|
* GNU tar uses "base-256 encoding" for very large numbers.
|
|
|
|
* Encoding is binary, with highest bit always set as a marker
|
|
|
|
* and sign in next-highest bit:
|
|
|
|
* 80 00 .. 00 - zero
|
|
|
|
* bf ff .. ff - largest positive number
|
|
|
|
* ff ff .. ff - minus 1
|
|
|
|
* c0 00 .. 00 - smallest negative number
|
|
|
|
*
|
|
|
|
* Example of tar file with 8914993153 (0x213600001) byte file.
|
|
|
|
* Field starts at offset 7c:
|
|
|
|
* 00070 30 30 30 00 30 30 30 30 30 30 30 00 80 00 00 00 |000.0000000.....|
|
|
|
|
* 00080 00 00 00 02 13 60 00 01 31 31 31 32 30 33 33 36 |.....`..11120336|
|
|
|
|
*
|
|
|
|
* NB: tarballs with NEGATIVE unix times encoded that way were seen!
|
|
|
|
*/
|
2011-10-19 12:51:12 +00:00
|
|
|
/* Sign-extend 7bit 'first' to 64bit 'v' (that is, using 6th bit as sign): */
|
|
|
|
first <<= 1;
|
|
|
|
first >>= 1; /* now 7th bit = 6th bit */
|
|
|
|
v = first; /* sign-extend 8 bits to 64 */
|
2010-05-06 18:08:14 +00:00
|
|
|
while (--len != 0)
|
2012-06-09 06:37:05 +00:00
|
|
|
v = (v << 8) + (uint8_t) *++str;
|
2010-05-06 18:08:14 +00:00
|
|
|
}
|
2006-11-24 14:51:01 +00:00
|
|
|
return v;
|
|
|
|
}
|
2006-11-26 01:46:59 +00:00
|
|
|
#define GET_OCTAL(a) getOctal((a), sizeof(a))
|
2006-11-24 14:51:01 +00:00
|
|
|
|
2012-02-23 12:45:18 +00:00
|
|
|
/* "global" is 0 or 1 */
|
|
|
|
static void process_pax_hdr(archive_handle_t *archive_handle, unsigned sz, int global)
|
2010-03-19 13:48:51 +00:00
|
|
|
{
|
|
|
|
char *buf, *p;
|
2012-02-23 12:45:18 +00:00
|
|
|
unsigned blk_sz;
|
|
|
|
|
|
|
|
blk_sz = (sz + 511) & (~511);
|
|
|
|
p = buf = xmalloc(blk_sz + 1);
|
|
|
|
xread(archive_handle->src_fd, buf, blk_sz);
|
|
|
|
archive_handle->offset += blk_sz;
|
2010-03-19 13:48:51 +00:00
|
|
|
|
|
|
|
/* prevent bb_strtou from running off the buffer */
|
|
|
|
buf[sz] = '\0';
|
|
|
|
|
|
|
|
while (sz != 0) {
|
|
|
|
char *end, *value;
|
|
|
|
unsigned len;
|
|
|
|
|
|
|
|
/* Every record has this format: "LEN NAME=VALUE\n" */
|
|
|
|
len = bb_strtou(p, &end, 10);
|
|
|
|
/* expect errno to be EINVAL, because the character
|
|
|
|
* following the digits should be a space
|
|
|
|
*/
|
|
|
|
p += len;
|
|
|
|
sz -= len;
|
2014-01-10 17:02:38 +00:00
|
|
|
if (
|
|
|
|
/** (int)sz < 0 - not good enough for huge malicious VALUE of 2^32-1 */
|
|
|
|
(int)(sz|len) < 0 /* this works */
|
2010-03-19 13:48:51 +00:00
|
|
|
|| len == 0
|
|
|
|
|| errno != EINVAL
|
|
|
|
|| *end != ' '
|
|
|
|
) {
|
|
|
|
bb_error_msg("malformed extended header, skipped");
|
|
|
|
// More verbose version:
|
|
|
|
//bb_error_msg("malformed extended header at %"OFF_FMT"d, skipped",
|
|
|
|
// archive_handle->offset - (sz + len));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* overwrite the terminating newline with NUL
|
|
|
|
* (we do not bother to check that it *was* a newline)
|
|
|
|
*/
|
|
|
|
p[-1] = '\0';
|
|
|
|
value = end + 1;
|
2012-02-23 12:45:18 +00:00
|
|
|
|
|
|
|
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
|
2015-03-12 16:48:34 +00:00
|
|
|
if (!global && is_prefixed_with(value, "path=")) {
|
2012-02-23 12:45:18 +00:00
|
|
|
value += sizeof("path=") - 1;
|
|
|
|
free(archive_handle->tar__longname);
|
|
|
|
archive_handle->tar__longname = xstrdup(value);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_TAR_SELINUX
|
|
|
|
/* Scan for SELinux contexts, via "RHT.security.selinux" keyword.
|
|
|
|
* This is what Red Hat's patched version of tar uses.
|
|
|
|
*/
|
|
|
|
# define SELINUX_CONTEXT_KEYWORD "RHT.security.selinux"
|
2015-03-12 16:48:34 +00:00
|
|
|
if (is_prefixed_with(value, SELINUX_CONTEXT_KEYWORD"=")) {
|
2010-03-19 13:48:51 +00:00
|
|
|
value += sizeof(SELINUX_CONTEXT_KEYWORD"=") - 1;
|
2012-02-23 12:45:18 +00:00
|
|
|
free(archive_handle->tar__sctx[global]);
|
|
|
|
archive_handle->tar__sctx[global] = xstrdup(value);
|
|
|
|
continue;
|
2010-03-19 13:48:51 +00:00
|
|
|
}
|
2012-02-23 12:45:18 +00:00
|
|
|
#endif
|
2010-03-19 13:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
|
2001-10-25 14:18:08 +00:00
|
|
|
{
|
2002-09-25 02:47:48 +00:00
|
|
|
file_header_t *file_header = archive_handle->file_header;
|
2010-06-26 16:21:36 +00:00
|
|
|
struct tar_header_t tar;
|
2006-11-24 14:51:01 +00:00
|
|
|
char *cp;
|
2007-06-21 12:41:59 +00:00
|
|
|
int i, sum_u, sum;
|
|
|
|
#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
|
|
|
|
int sum_s;
|
|
|
|
#endif
|
2006-11-24 17:21:44 +00:00
|
|
|
int parse_names;
|
2006-11-24 14:51:01 +00:00
|
|
|
|
2008-06-28 05:04:09 +00:00
|
|
|
/* Our "private data" */
|
|
|
|
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
|
2010-01-06 09:53:17 +00:00
|
|
|
# define p_longname (archive_handle->tar__longname)
|
|
|
|
# define p_linkname (archive_handle->tar__linkname)
|
2008-06-28 05:04:09 +00:00
|
|
|
#else
|
2010-01-06 09:53:17 +00:00
|
|
|
# define p_longname 0
|
|
|
|
# define p_linkname 0
|
2008-06-28 05:04:09 +00:00
|
|
|
#endif
|
|
|
|
|
2010-03-19 13:48:51 +00:00
|
|
|
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS || ENABLE_FEATURE_TAR_SELINUX
|
2006-12-26 18:17:42 +00:00
|
|
|
again:
|
|
|
|
#endif
|
2002-09-25 02:47:48 +00:00
|
|
|
/* Align header */
|
2002-11-03 14:05:15 +00:00
|
|
|
data_align(archive_handle, 512);
|
2002-09-25 02:47:48 +00:00
|
|
|
|
2006-11-26 01:46:59 +00:00
|
|
|
again_after_align:
|
|
|
|
|
2009-03-13 14:26:44 +00:00
|
|
|
#if ENABLE_DESKTOP || ENABLE_FEATURE_TAR_AUTODETECT
|
2008-07-10 23:06:00 +00:00
|
|
|
/* to prevent misdetection of bz2 sig */
|
2010-02-04 14:00:15 +00:00
|
|
|
*(aliased_uint32_t*)&tar = 0;
|
2008-02-13 17:52:42 +00:00
|
|
|
i = full_read(archive_handle->src_fd, &tar, 512);
|
2008-06-28 05:04:09 +00:00
|
|
|
/* If GNU tar sees EOF in above read, it says:
|
2008-02-13 17:52:42 +00:00
|
|
|
* "tar: A lone zero block at N", where N = kilobyte
|
|
|
|
* where EOF was met (not EOF block, actual EOF!),
|
2008-07-10 23:06:00 +00:00
|
|
|
* and exits with EXIT_SUCCESS.
|
2008-05-19 09:29:47 +00:00
|
|
|
* We will mimic exit(EXIT_SUCCESS), although we will not mimic
|
2008-02-14 08:52:30 +00:00
|
|
|
* the message and we don't check whether we indeed
|
|
|
|
* saw zero block directly before this. */
|
2008-07-10 23:06:00 +00:00
|
|
|
if (i == 0) {
|
2013-11-19 13:44:04 +00:00
|
|
|
bb_error_msg("short read");
|
|
|
|
/* this merely signals end of archive, not exit(1): */
|
|
|
|
return EXIT_FAILURE;
|
2008-07-10 23:06:00 +00:00
|
|
|
}
|
|
|
|
if (i != 512) {
|
2009-04-21 11:09:40 +00:00
|
|
|
IF_FEATURE_TAR_AUTODETECT(goto autodetect;)
|
2013-11-19 13:44:04 +00:00
|
|
|
bb_error_msg_and_die("short read");
|
2008-07-10 23:06:00 +00:00
|
|
|
}
|
|
|
|
|
2008-02-14 08:52:30 +00:00
|
|
|
#else
|
2008-07-10 23:06:00 +00:00
|
|
|
i = 512;
|
|
|
|
xread(archive_handle->src_fd, &tar, i);
|
2008-02-14 08:52:30 +00:00
|
|
|
#endif
|
2008-07-10 23:06:00 +00:00
|
|
|
archive_handle->offset += i;
|
2001-10-25 14:18:08 +00:00
|
|
|
|
2002-09-25 02:47:48 +00:00
|
|
|
/* If there is no filename its an empty header */
|
2008-04-29 04:12:58 +00:00
|
|
|
if (tar.name[0] == 0 && tar.prefix[0] == 0) {
|
2010-01-06 09:53:17 +00:00
|
|
|
if (archive_handle->tar__end) {
|
2008-06-28 05:04:09 +00:00
|
|
|
/* Second consecutive empty header - end of archive.
|
2005-07-20 19:24:13 +00:00
|
|
|
* Read until the end to empty the pipe from gz or bz2
|
|
|
|
*/
|
2006-11-24 14:51:01 +00:00
|
|
|
while (full_read(archive_handle->src_fd, &tar, 512) == 512)
|
2008-02-19 11:26:28 +00:00
|
|
|
continue;
|
2013-11-19 13:44:04 +00:00
|
|
|
return EXIT_FAILURE; /* "end of archive" */
|
2005-07-20 19:24:13 +00:00
|
|
|
}
|
2010-01-06 09:53:17 +00:00
|
|
|
archive_handle->tar__end = 1;
|
2013-11-19 13:44:04 +00:00
|
|
|
return EXIT_SUCCESS; /* "decoded one header" */
|
2001-10-25 14:18:08 +00:00
|
|
|
}
|
2010-01-06 09:53:17 +00:00
|
|
|
archive_handle->tar__end = 0;
|
2001-10-25 14:18:08 +00:00
|
|
|
|
2008-02-19 11:26:28 +00:00
|
|
|
/* Check header has valid magic, "ustar" is for the proper tar,
|
|
|
|
* five NULs are for the old tar format */
|
2015-03-12 16:48:34 +00:00
|
|
|
if (!is_prefixed_with(tar.magic, "ustar")
|
2008-02-19 11:26:28 +00:00
|
|
|
&& (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
|
|
|
|
|| memcmp(tar.magic, "\0\0\0\0", 5) != 0)
|
|
|
|
) {
|
|
|
|
#if ENABLE_FEATURE_TAR_AUTODETECT
|
2009-03-13 14:26:44 +00:00
|
|
|
autodetect:
|
2008-06-28 05:04:09 +00:00
|
|
|
/* Two different causes for lseek() != 0:
|
|
|
|
* unseekable fd (would like to support that too, but...),
|
|
|
|
* or not first block (false positive, it's not .gz/.bz2!) */
|
2008-07-10 23:06:00 +00:00
|
|
|
if (lseek(archive_handle->src_fd, -i, SEEK_CUR) != 0)
|
2008-02-19 11:26:28 +00:00
|
|
|
goto err;
|
2014-02-02 01:06:38 +00:00
|
|
|
if (setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_compressed:*/ 0) != 0)
|
2008-02-19 11:26:28 +00:00
|
|
|
err:
|
2012-03-06 15:27:48 +00:00
|
|
|
bb_error_msg_and_die("invalid tar magic");
|
|
|
|
archive_handle->offset = 0;
|
|
|
|
goto again_after_align;
|
|
|
|
#endif
|
2008-02-19 11:26:28 +00:00
|
|
|
bb_error_msg_and_die("invalid tar magic");
|
2001-10-25 14:18:08 +00:00
|
|
|
}
|
2007-03-03 20:06:59 +00:00
|
|
|
|
|
|
|
/* Do checksum on headers.
|
|
|
|
* POSIX says that checksum is done on unsigned bytes, but
|
2007-03-04 18:09:50 +00:00
|
|
|
* Sun and HP-UX gets it wrong... more details in
|
2007-03-03 20:06:59 +00:00
|
|
|
* GNU tar source. */
|
2007-06-21 12:41:59 +00:00
|
|
|
#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
|
|
|
|
sum_s = ' ' * sizeof(tar.chksum);
|
|
|
|
#endif
|
|
|
|
sum_u = ' ' * sizeof(tar.chksum);
|
2007-07-21 15:08:09 +00:00
|
|
|
for (i = 0; i < 148; i++) {
|
2007-03-03 20:06:59 +00:00
|
|
|
sum_u += ((unsigned char*)&tar)[i];
|
2007-06-21 12:41:59 +00:00
|
|
|
#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
|
2007-03-03 20:06:59 +00:00
|
|
|
sum_s += ((signed char*)&tar)[i];
|
2007-06-21 12:41:59 +00:00
|
|
|
#endif
|
2001-10-25 14:18:08 +00:00
|
|
|
}
|
2007-07-21 15:08:09 +00:00
|
|
|
for (i = 156; i < 512; i++) {
|
2007-03-03 20:06:59 +00:00
|
|
|
sum_u += ((unsigned char*)&tar)[i];
|
2007-06-21 12:41:59 +00:00
|
|
|
#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
|
2007-03-03 20:06:59 +00:00
|
|
|
sum_s += ((signed char*)&tar)[i];
|
2007-06-21 12:41:59 +00:00
|
|
|
#endif
|
2001-10-25 14:18:08 +00:00
|
|
|
}
|
2006-11-24 14:51:01 +00:00
|
|
|
/* This field does not need special treatment (getOctal) */
|
2009-09-23 15:16:37 +00:00
|
|
|
{
|
|
|
|
char *endp; /* gcc likes temp var for &endp */
|
|
|
|
sum = strtoul(tar.chksum, &endp, 8);
|
|
|
|
if ((*endp != '\0' && *endp != ' ')
|
|
|
|
|| (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum))
|
|
|
|
) {
|
|
|
|
bb_error_msg_and_die("invalid tar header checksum");
|
|
|
|
}
|
|
|
|
}
|
2010-01-03 22:41:11 +00:00
|
|
|
/* don't use xstrtoul, tar.chksum may have leading spaces */
|
|
|
|
sum = strtoul(tar.chksum, NULL, 8);
|
2009-04-21 11:09:40 +00:00
|
|
|
if (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) {
|
2006-10-08 12:49:22 +00:00
|
|
|
bb_error_msg_and_die("invalid tar header checksum");
|
2001-10-25 14:18:08 +00:00
|
|
|
}
|
|
|
|
|
2006-11-26 01:46:59 +00:00
|
|
|
/* 0 is reserved for high perf file, treat as normal file */
|
|
|
|
if (!tar.typeflag) tar.typeflag = '0';
|
|
|
|
parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
|
2006-11-24 17:21:44 +00:00
|
|
|
|
2006-11-24 14:51:01 +00:00
|
|
|
/* getOctal trashes subsequent field, therefore we call it
|
|
|
|
* on fields in reverse order */
|
|
|
|
if (tar.devmajor[0]) {
|
2008-04-29 03:54:16 +00:00
|
|
|
char t = tar.prefix[0];
|
|
|
|
/* we trash prefix[0] here, but we DO need it later! */
|
2006-11-24 14:51:01 +00:00
|
|
|
unsigned minor = GET_OCTAL(tar.devminor);
|
|
|
|
unsigned major = GET_OCTAL(tar.devmajor);
|
|
|
|
file_header->device = makedev(major, minor);
|
2008-04-29 03:54:16 +00:00
|
|
|
tar.prefix[0] = t;
|
2006-10-10 21:00:47 +00:00
|
|
|
}
|
2007-06-20 14:49:47 +00:00
|
|
|
file_header->link_target = NULL;
|
2008-06-28 05:04:09 +00:00
|
|
|
if (!p_linkname && parse_names && tar.linkname[0]) {
|
2008-04-15 01:17:50 +00:00
|
|
|
file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname));
|
2007-06-20 14:49:47 +00:00
|
|
|
/* FIXME: what if we have non-link object with link_target? */
|
|
|
|
/* Will link_target be free()ed? */
|
2006-11-24 17:21:44 +00:00
|
|
|
}
|
2008-02-14 20:37:54 +00:00
|
|
|
#if ENABLE_FEATURE_TAR_UNAME_GNAME
|
2010-01-06 09:53:17 +00:00
|
|
|
file_header->tar__uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL;
|
|
|
|
file_header->tar__gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL;
|
2008-02-14 20:37:54 +00:00
|
|
|
#endif
|
2010-05-06 18:08:14 +00:00
|
|
|
file_header->mtime = GET_OCTAL(tar.mtime);
|
|
|
|
file_header->size = GET_OCTAL(tar.size);
|
2006-11-24 14:51:01 +00:00
|
|
|
file_header->gid = GET_OCTAL(tar.gid);
|
|
|
|
file_header->uid = GET_OCTAL(tar.uid);
|
2004-02-20 02:34:42 +00:00
|
|
|
/* Set bits 0-11 of the files mode */
|
2006-11-24 14:51:01 +00:00
|
|
|
file_header->mode = 07777 & GET_OCTAL(tar.mode);
|
2004-02-20 02:34:42 +00:00
|
|
|
|
2006-11-26 01:46:59 +00:00
|
|
|
file_header->name = NULL;
|
2008-06-28 05:04:09 +00:00
|
|
|
if (!p_longname && parse_names) {
|
2006-11-24 17:21:44 +00:00
|
|
|
/* we trash mode[0] here, it's ok */
|
2008-04-15 01:17:50 +00:00
|
|
|
//tar.name[sizeof(tar.name)] = '\0'; - gcc 4.3.0 would complain
|
|
|
|
tar.mode[0] = '\0';
|
2006-11-25 23:47:32 +00:00
|
|
|
if (tar.prefix[0]) {
|
|
|
|
/* and padding[0] */
|
2008-04-15 01:17:50 +00:00
|
|
|
//tar.prefix[sizeof(tar.prefix)] = '\0'; - gcc 4.3.0 would complain
|
|
|
|
tar.padding[0] = '\0';
|
2006-11-24 14:53:18 +00:00
|
|
|
file_header->name = concat_path_file(tar.prefix, tar.name);
|
2006-11-25 23:47:32 +00:00
|
|
|
} else
|
2006-11-24 14:53:18 +00:00
|
|
|
file_header->name = xstrdup(tar.name);
|
|
|
|
}
|
|
|
|
|
2004-02-20 02:34:42 +00:00
|
|
|
/* Set bits 12-15 of the files mode */
|
2006-11-24 17:21:44 +00:00
|
|
|
/* (typeflag was not trashed because chksum does not use getOctal) */
|
2006-11-24 14:51:01 +00:00
|
|
|
switch (tar.typeflag) {
|
2011-07-29 06:33:47 +00:00
|
|
|
case '1': /* hardlink */
|
|
|
|
/* we mark hardlinks as regular files with zero size and a link name */
|
2004-02-20 02:34:42 +00:00
|
|
|
file_header->mode |= S_IFREG;
|
2011-07-29 06:33:47 +00:00
|
|
|
/* on size of link fields from star(4)
|
|
|
|
* ... For tar archives written by pre POSIX.1-1988
|
|
|
|
* implementations, the size field usually contains the size of
|
|
|
|
* the file and needs to be ignored as no data may follow this
|
|
|
|
* header type. For POSIX.1- 1988 compliant archives, the size
|
|
|
|
* field needs to be 0. For POSIX.1-2001 compliant archives,
|
|
|
|
* the size field may be non zero, indicating that file data is
|
|
|
|
* included in the archive.
|
|
|
|
* i.e; always assume this is zero for safety.
|
|
|
|
*/
|
|
|
|
goto size0;
|
2004-02-20 02:25:18 +00:00
|
|
|
case '7':
|
2006-11-26 01:46:59 +00:00
|
|
|
/* case 0: */
|
2003-01-28 01:45:48 +00:00
|
|
|
case '0':
|
2006-11-24 17:21:44 +00:00
|
|
|
#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
|
2003-09-09 17:41:03 +00:00
|
|
|
if (last_char_is(file_header->name, '/')) {
|
2008-07-20 17:10:43 +00:00
|
|
|
goto set_dir;
|
|
|
|
}
|
2004-02-20 02:25:18 +00:00
|
|
|
#endif
|
2006-11-24 14:51:01 +00:00
|
|
|
file_header->mode |= S_IFREG;
|
2002-08-22 17:47:09 +00:00
|
|
|
break;
|
2003-01-28 01:45:48 +00:00
|
|
|
case '2':
|
2002-09-25 02:47:48 +00:00
|
|
|
file_header->mode |= S_IFLNK;
|
2008-07-20 17:10:43 +00:00
|
|
|
/* have seen tarballs with size field containing
|
|
|
|
* the size of the link target's name */
|
|
|
|
size0:
|
|
|
|
file_header->size = 0;
|
2002-08-22 17:47:09 +00:00
|
|
|
break;
|
2003-01-28 01:45:48 +00:00
|
|
|
case '3':
|
2002-09-25 02:47:48 +00:00
|
|
|
file_header->mode |= S_IFCHR;
|
2008-07-20 17:10:43 +00:00
|
|
|
goto size0; /* paranoia */
|
2003-01-28 01:45:48 +00:00
|
|
|
case '4':
|
2002-09-25 02:47:48 +00:00
|
|
|
file_header->mode |= S_IFBLK;
|
2008-07-20 17:10:43 +00:00
|
|
|
goto size0;
|
2003-01-28 01:45:48 +00:00
|
|
|
case '5':
|
2009-04-21 11:09:40 +00:00
|
|
|
IF_FEATURE_TAR_OLDGNU_COMPATIBILITY(set_dir:)
|
2002-09-25 02:47:48 +00:00
|
|
|
file_header->mode |= S_IFDIR;
|
2008-07-20 17:10:43 +00:00
|
|
|
goto size0;
|
2003-01-28 01:45:48 +00:00
|
|
|
case '6':
|
2002-09-25 02:47:48 +00:00
|
|
|
file_header->mode |= S_IFIFO;
|
2008-07-20 17:10:43 +00:00
|
|
|
goto size0;
|
2006-11-24 17:21:44 +00:00
|
|
|
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
|
2006-11-24 14:51:01 +00:00
|
|
|
case 'L':
|
2006-11-26 01:46:59 +00:00
|
|
|
/* free: paranoia: tar with several consecutive longnames */
|
2008-06-28 05:04:09 +00:00
|
|
|
free(p_longname);
|
2006-11-26 01:46:59 +00:00
|
|
|
/* For paranoia reasons we allocate extra NUL char */
|
2008-06-28 05:04:09 +00:00
|
|
|
p_longname = xzalloc(file_header->size + 1);
|
2006-11-26 01:46:59 +00:00
|
|
|
/* We read ASCIZ string, including NUL */
|
2008-06-28 05:04:09 +00:00
|
|
|
xread(archive_handle->src_fd, p_longname, file_header->size);
|
2006-11-24 14:51:01 +00:00
|
|
|
archive_handle->offset += file_header->size;
|
2006-11-26 01:46:59 +00:00
|
|
|
/* return get_header_tar(archive_handle); */
|
|
|
|
/* gcc 4.1.1 didn't optimize it into jump */
|
|
|
|
/* so we will do it ourself, this also saves stack */
|
|
|
|
goto again;
|
2006-11-24 14:51:01 +00:00
|
|
|
case 'K':
|
2008-06-28 05:04:09 +00:00
|
|
|
free(p_linkname);
|
|
|
|
p_linkname = xzalloc(file_header->size + 1);
|
|
|
|
xread(archive_handle->src_fd, p_linkname, file_header->size);
|
2006-11-24 14:51:01 +00:00
|
|
|
archive_handle->offset += file_header->size;
|
2006-11-26 01:46:59 +00:00
|
|
|
/* return get_header_tar(archive_handle); */
|
|
|
|
goto again;
|
2004-02-19 08:48:30 +00:00
|
|
|
case 'D': /* GNU dump dir */
|
2006-11-24 14:51:01 +00:00
|
|
|
case 'M': /* Continuation of multi volume archive */
|
2004-02-19 08:48:30 +00:00
|
|
|
case 'N': /* Old GNU for names > 100 characters */
|
|
|
|
case 'S': /* Sparse file */
|
|
|
|
case 'V': /* Volume header */
|
2004-02-17 11:55:06 +00:00
|
|
|
#endif
|
2006-07-06 20:30:19 +00:00
|
|
|
case 'g': /* pax global header */
|
2012-02-23 12:45:18 +00:00
|
|
|
case 'x': { /* pax extended header */
|
|
|
|
if ((uoff_t)file_header->size > 0xfffff) /* paranoia */
|
|
|
|
goto skip_ext_hdr;
|
|
|
|
process_pax_hdr(archive_handle, file_header->size, (tar.typeflag == 'g'));
|
|
|
|
goto again_after_align;
|
|
|
|
}
|
2010-03-19 13:48:51 +00:00
|
|
|
skip_ext_hdr:
|
|
|
|
{
|
2006-11-26 01:46:59 +00:00
|
|
|
off_t sz;
|
|
|
|
bb_error_msg("warning: skipping header '%c'", tar.typeflag);
|
|
|
|
sz = (file_header->size + 511) & ~(off_t)511;
|
|
|
|
archive_handle->offset += sz;
|
|
|
|
sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
|
|
|
|
while (sz--)
|
|
|
|
xread(archive_handle->src_fd, &tar, 512);
|
|
|
|
/* return get_header_tar(archive_handle); */
|
|
|
|
goto again_after_align;
|
|
|
|
}
|
2004-02-19 08:48:30 +00:00
|
|
|
default:
|
2006-11-26 01:46:59 +00:00
|
|
|
bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag);
|
2002-08-13 05:06:43 +00:00
|
|
|
}
|
2003-08-14 02:55:15 +00:00
|
|
|
|
2006-11-24 17:21:44 +00:00
|
|
|
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
|
2008-06-28 05:04:09 +00:00
|
|
|
if (p_longname) {
|
|
|
|
file_header->name = p_longname;
|
|
|
|
p_longname = NULL;
|
2006-11-24 17:21:44 +00:00
|
|
|
}
|
2008-06-28 05:04:09 +00:00
|
|
|
if (p_linkname) {
|
|
|
|
file_header->link_target = p_linkname;
|
|
|
|
p_linkname = NULL;
|
2006-11-24 17:21:44 +00:00
|
|
|
}
|
|
|
|
#endif
|
2011-03-01 16:21:07 +00:00
|
|
|
|
|
|
|
/* Everything up to and including last ".." component is stripped */
|
2011-03-02 00:21:02 +00:00
|
|
|
overlapping_strcpy(file_header->name, strip_unsafe_prefix(file_header->name));
|
2006-11-24 17:21:44 +00:00
|
|
|
|
2006-11-24 14:51:01 +00:00
|
|
|
/* Strip trailing '/' in directories */
|
2006-12-22 00:21:07 +00:00
|
|
|
/* Must be done after mode is set as '/' is used to check if it's a directory */
|
2006-11-24 14:51:01 +00:00
|
|
|
cp = last_char_is(file_header->name, '/');
|
|
|
|
|
2002-11-04 23:47:31 +00:00
|
|
|
if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
|
2008-07-10 23:06:00 +00:00
|
|
|
archive_handle->action_header(/*archive_handle->*/ file_header);
|
2006-11-24 14:53:18 +00:00
|
|
|
/* Note that we kill the '/' only after action_header() */
|
|
|
|
/* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
|
2010-04-09 08:52:52 +00:00
|
|
|
if (cp)
|
|
|
|
*cp = '\0';
|
2002-09-25 02:47:48 +00:00
|
|
|
archive_handle->action_data(archive_handle);
|
2012-06-22 14:27:21 +00:00
|
|
|
if (archive_handle->accept || archive_handle->reject
|
|
|
|
|| (archive_handle->ah_flags & ARCHIVE_REMEMBER_NAMES)
|
|
|
|
) {
|
2010-06-26 16:22:41 +00:00
|
|
|
llist_add_to(&archive_handle->passed, file_header->name);
|
2012-06-22 14:27:21 +00:00
|
|
|
} else /* Caller isn't interested in list of unpacked files */
|
2010-06-26 16:22:41 +00:00
|
|
|
free(file_header->name);
|
2002-09-25 02:47:48 +00:00
|
|
|
} else {
|
2004-03-15 08:29:22 +00:00
|
|
|
data_skip(archive_handle);
|
2006-11-26 01:46:59 +00:00
|
|
|
free(file_header->name);
|
2002-09-25 02:47:48 +00:00
|
|
|
}
|
|
|
|
archive_handle->offset += file_header->size;
|
|
|
|
|
2007-06-20 14:49:47 +00:00
|
|
|
free(file_header->link_target);
|
2010-06-26 16:22:41 +00:00
|
|
|
/* Do not free(file_header->name)!
|
|
|
|
* It might be inserted in archive_handle->passed - see above */
|
2008-02-14 20:37:54 +00:00
|
|
|
#if ENABLE_FEATURE_TAR_UNAME_GNAME
|
2010-01-06 09:53:17 +00:00
|
|
|
free(file_header->tar__uname);
|
|
|
|
free(file_header->tar__gname);
|
2008-02-14 20:37:54 +00:00
|
|
|
#endif
|
2013-11-19 13:44:04 +00:00
|
|
|
return EXIT_SUCCESS; /* "decoded one header" */
|
2002-07-11 11:11:56 +00:00
|
|
|
}
|