2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-02-20 19:28:53 +00:00
|
|
|
/* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2004-02-17 11:55:06 +00:00
|
|
|
*
|
2004-02-20 02:25:18 +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
|
|
|
*
|
|
|
|
* References:
|
|
|
|
* GNU tar and star man pages,
|
|
|
|
* Opengroup's ustar interchange format,
|
2006-01-25 00:08:53 +00:00
|
|
|
* http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
|
2001-10-25 14:18:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
2006-08-03 15:41:12 +00:00
|
|
|
#include "unarchive.h"
|
2001-10-25 14:18:08 +00:00
|
|
|
|
2006-11-24 17:21:44 +00:00
|
|
|
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
|
|
|
|
static char *longname;
|
|
|
|
static char *linkname;
|
|
|
|
#else
|
|
|
|
enum {
|
|
|
|
longname = 0,
|
|
|
|
linkname = 0,
|
|
|
|
};
|
2002-11-23 10:44:47 +00:00
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
/* Actually, tar header allows leading spaces also.
|
|
|
|
* Oh well, we will be liberal and skip this...
|
|
|
|
* The only downside probably is that we allow "-123" too :)
|
|
|
|
if (*str < '0' || *str > '7')
|
|
|
|
bb_error_msg_and_die("corrupted octal value in tar header");
|
|
|
|
*/
|
|
|
|
str[len] = '\0';
|
|
|
|
v = strtoull(str, &str, 8);
|
|
|
|
if (*str)
|
|
|
|
bb_error_msg_and_die("corrupted octal value in tar header");
|
|
|
|
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
|
|
|
|
|
|
|
void BUG_tar_header_size(void);
|
2006-03-06 20:47:33 +00:00
|
|
|
char get_header_tar(archive_handle_t *archive_handle)
|
2001-10-25 14:18:08 +00:00
|
|
|
{
|
2006-11-26 01:46:59 +00:00
|
|
|
static int end;
|
2006-11-24 14:51:01 +00:00
|
|
|
|
2002-09-25 02:47:48 +00:00
|
|
|
file_header_t *file_header = archive_handle->file_header;
|
2006-11-24 14:51:01 +00:00
|
|
|
struct {
|
2003-09-12 06:31:28 +00:00
|
|
|
/* ustar header, Posix 1003.1 */
|
2006-11-24 14:51:01 +00:00
|
|
|
char name[100]; /* 0-99 */
|
|
|
|
char mode[8]; /* 100-107 */
|
|
|
|
char uid[8]; /* 108-115 */
|
|
|
|
char gid[8]; /* 116-123 */
|
|
|
|
char size[12]; /* 124-135 */
|
|
|
|
char mtime[12]; /* 136-147 */
|
|
|
|
char chksum[8]; /* 148-155 */
|
|
|
|
char typeflag; /* 156-156 */
|
|
|
|
char linkname[100]; /* 157-256 */
|
|
|
|
char magic[6]; /* 257-262 */
|
|
|
|
char version[2]; /* 263-264 */
|
|
|
|
char uname[32]; /* 265-296 */
|
|
|
|
char gname[32]; /* 297-328 */
|
|
|
|
char devmajor[8]; /* 329-336 */
|
|
|
|
char devminor[8]; /* 337-344 */
|
|
|
|
char prefix[155]; /* 345-499 */
|
|
|
|
char padding[12]; /* 500-512 */
|
2001-10-25 14:18:08 +00:00
|
|
|
} tar;
|
2006-11-24 14:51:01 +00:00
|
|
|
char *cp;
|
|
|
|
int sum, i;
|
2006-11-24 17:21:44 +00:00
|
|
|
int parse_names;
|
2006-11-24 14:51:01 +00:00
|
|
|
|
|
|
|
if (sizeof(tar) != 512)
|
|
|
|
BUG_tar_header_size();
|
2001-10-25 14:18:08 +00:00
|
|
|
|
2006-12-26 18:17:42 +00:00
|
|
|
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
|
|
|
|
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:
|
|
|
|
|
2006-11-24 14:51:01 +00:00
|
|
|
xread(archive_handle->src_fd, &tar, 512);
|
2002-09-25 02:47:48 +00:00
|
|
|
archive_handle->offset += 512;
|
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 */
|
2006-11-24 14:51:01 +00:00
|
|
|
if (tar.name[0] == 0) {
|
2005-07-20 19:24:13 +00:00
|
|
|
if (end) {
|
|
|
|
/* This is the second consecutive empty header! End of archive!
|
|
|
|
* 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)
|
|
|
|
/* repeat */;
|
2006-10-08 12:49:22 +00:00
|
|
|
return EXIT_FAILURE;
|
2005-07-20 19:24:13 +00:00
|
|
|
}
|
|
|
|
end = 1;
|
2006-10-08 12:49:22 +00:00
|
|
|
return EXIT_SUCCESS;
|
2001-10-25 14:18:08 +00:00
|
|
|
}
|
2005-07-20 19:24:13 +00:00
|
|
|
end = 0;
|
2001-10-25 14:18:08 +00:00
|
|
|
|
2002-08-13 05:06:43 +00:00
|
|
|
/* Check header has valid magic, "ustar" is for the proper tar
|
|
|
|
* 0's are for the old tar format
|
2001-10-25 14:18:08 +00:00
|
|
|
*/
|
2006-11-24 14:51:01 +00:00
|
|
|
if (strncmp(tar.magic, "ustar", 5) != 0) {
|
2006-11-24 17:21:44 +00:00
|
|
|
#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
|
2006-11-24 14:51:01 +00:00
|
|
|
if (memcmp(tar.magic, "\0\0\0\0", 5) != 0)
|
2002-08-13 05:06:43 +00:00
|
|
|
#endif
|
2006-10-08 12:49:22 +00:00
|
|
|
bb_error_msg_and_die("invalid tar magic");
|
2001-10-25 14:18:08 +00:00
|
|
|
}
|
|
|
|
/* Do checksum on headers */
|
2006-11-24 14:51:01 +00:00
|
|
|
sum = ' ' * sizeof(tar.chksum);
|
2006-10-10 21:00:47 +00:00
|
|
|
for (i = 0; i < 148 ; i++) {
|
2006-11-24 14:51:01 +00:00
|
|
|
sum += ((char*)&tar)[i];
|
2001-10-25 14:18:08 +00:00
|
|
|
}
|
2006-10-08 12:49:22 +00:00
|
|
|
for (i = 156; i < 512 ; i++) {
|
2006-11-24 14:51:01 +00:00
|
|
|
sum += ((char*)&tar)[i];
|
2001-10-25 14:18:08 +00:00
|
|
|
}
|
2006-11-24 14:51:01 +00:00
|
|
|
/* This field does not need special treatment (getOctal) */
|
|
|
|
if (sum != xstrtoul(tar.chksum, 8)) {
|
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]) {
|
|
|
|
unsigned minor = GET_OCTAL(tar.devminor);
|
|
|
|
unsigned major = GET_OCTAL(tar.devmajor);
|
|
|
|
file_header->device = makedev(major, minor);
|
2006-10-10 21:00:47 +00:00
|
|
|
}
|
2006-11-24 17:21:44 +00:00
|
|
|
file_header->link_name = NULL;
|
|
|
|
if (!linkname && parse_names && tar.linkname[0]) {
|
|
|
|
/* we trash magic[0] here, it's ok */
|
|
|
|
tar.linkname[sizeof(tar.linkname)] = '\0';
|
|
|
|
file_header->link_name = xstrdup(tar.linkname);
|
|
|
|
/* FIXME: what if we have non-link object with link_name? */
|
|
|
|
/* Will link_name be free()ed? */
|
|
|
|
}
|
2006-11-24 14:51:01 +00:00
|
|
|
file_header->mtime = GET_OCTAL(tar.mtime);
|
|
|
|
file_header->size = GET_OCTAL(tar.size);
|
|
|
|
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;
|
2006-11-24 17:21:44 +00:00
|
|
|
if (!longname && parse_names) {
|
|
|
|
/* we trash mode[0] here, it's ok */
|
2006-11-24 14:53:18 +00:00
|
|
|
tar.name[sizeof(tar.name)] = '\0';
|
2006-11-25 23:47:32 +00:00
|
|
|
if (tar.prefix[0]) {
|
|
|
|
/* and padding[0] */
|
|
|
|
tar.prefix[sizeof(tar.prefix)] = '\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) {
|
2004-02-19 08:48:30 +00:00
|
|
|
/* busybox identifies hard links as being regular files with 0 size and a link name */
|
|
|
|
case '1':
|
2004-02-20 02:34:42 +00:00
|
|
|
file_header->mode |= S_IFREG;
|
2004-02-19 08:48:30 +00:00
|
|
|
break;
|
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, '/')) {
|
|
|
|
file_header->mode |= S_IFDIR;
|
2004-02-20 02:25:18 +00:00
|
|
|
} else
|
|
|
|
#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;
|
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;
|
2002-08-22 17:47:09 +00:00
|
|
|
break;
|
2003-01-28 01:45:48 +00:00
|
|
|
case '4':
|
2002-09-25 02:47:48 +00:00
|
|
|
file_header->mode |= S_IFBLK;
|
2002-08-22 17:47:09 +00:00
|
|
|
break;
|
2003-01-28 01:45:48 +00:00
|
|
|
case '5':
|
2002-09-25 02:47:48 +00:00
|
|
|
file_header->mode |= S_IFDIR;
|
2002-08-22 17:47:09 +00:00
|
|
|
break;
|
2003-01-28 01:45:48 +00:00
|
|
|
case '6':
|
2002-09-25 02:47:48 +00:00
|
|
|
file_header->mode |= S_IFIFO;
|
2002-08-22 17:47:09 +00:00
|
|
|
break;
|
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 */
|
2006-11-24 17:21:44 +00:00
|
|
|
free(longname);
|
2006-11-26 01:46:59 +00:00
|
|
|
/* For paranoia reasons we allocate extra NUL char */
|
2006-11-24 14:51:01 +00:00
|
|
|
longname = xzalloc(file_header->size + 1);
|
2006-11-26 01:46:59 +00:00
|
|
|
/* We read ASCIZ string, including NUL */
|
2006-11-24 14:51:01 +00:00
|
|
|
xread(archive_handle->src_fd, longname, file_header->size);
|
|
|
|
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':
|
2006-11-24 17:21:44 +00:00
|
|
|
free(linkname);
|
2006-11-24 14:51:01 +00:00
|
|
|
linkname = xzalloc(file_header->size + 1);
|
|
|
|
xread(archive_handle->src_fd, linkname, file_header->size);
|
|
|
|
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 */
|
2006-11-26 01:46:59 +00:00
|
|
|
case 'x': { /* pax extended header */
|
|
|
|
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
|
|
|
|
if (longname) {
|
|
|
|
file_header->name = longname;
|
|
|
|
longname = NULL;
|
|
|
|
}
|
|
|
|
if (linkname) {
|
|
|
|
file_header->link_name = linkname;
|
|
|
|
linkname = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2006-11-26 15:42:03 +00:00
|
|
|
if (!strncmp(file_header->name, "/../"+1, 3)
|
|
|
|
|| strstr(file_header->name, "/../")
|
|
|
|
) {
|
|
|
|
bb_error_msg_and_die("name with '..' encountered: '%s'",
|
|
|
|
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) {
|
2002-09-25 02:47:48 +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/") */
|
|
|
|
if (cp) *cp = '\0';
|
2002-09-25 02:47:48 +00:00
|
|
|
archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
|
|
|
|
archive_handle->action_data(archive_handle);
|
2006-05-26 23:44:51 +00:00
|
|
|
llist_add_to(&(archive_handle->passed), 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;
|
|
|
|
|
2003-11-15 00:24:43 +00:00
|
|
|
free(file_header->link_name);
|
2006-11-26 01:46:59 +00:00
|
|
|
/* Do not free(file_header->name)! */
|
2003-11-15 00:24:43 +00:00
|
|
|
|
2006-10-08 12:49:22 +00:00
|
|
|
return EXIT_SUCCESS;
|
2002-07-11 11:11:56 +00:00
|
|
|
}
|