busybox/archival/libunarchive/get_header_tar.c

213 lines
5.8 KiB
C
Raw Normal View History

/* vi: set sw=4 ts=4: */
/* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*
* FIXME:
* In privileged mode if uname and gname map to a uid and gid then use the
* mapped value instead of the uid/gid values in tar header
*
* References:
* GNU tar and star man pages,
* Opengroup's ustar interchange format,
2006-01-25 05:38:53 +05:30
* http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
2001-10-25 19:48:08 +05:30
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sysmacros.h> /* For makedev */
2001-10-25 19:48:08 +05:30
#include "unarchive.h"
#include "libbb.h"
#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
2002-11-23 16:14:47 +05:30
static char *longname = NULL;
static char *linkname = NULL;
#endif
char get_header_tar(archive_handle_t *archive_handle)
2001-10-25 19:48:08 +05:30
{
2002-09-25 08:17:48 +05:30
file_header_t *file_header = archive_handle->file_header;
2001-10-25 19:48:08 +05:30
union {
/* ustar header, Posix 1003.1 */
2001-10-25 19:48:08 +05:30
unsigned char raw[512];
struct {
2002-08-22 23:17:09 +05:30
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 */
2001-10-25 19:48:08 +05:30
char linkname[100]; /* 157-256 */
2002-08-22 23:17:09 +05:30
char magic[6]; /* 257-262 */
2001-10-25 19:48:08 +05:30
char version[2]; /* 263-264 */
2002-08-22 23:17:09 +05:30
char uname[32]; /* 265-296 */
char gname[32]; /* 297-328 */
2001-10-25 19:48:08 +05:30
char devmajor[8]; /* 329-336 */
char devminor[8]; /* 337-344 */
char prefix[155]; /* 345-499 */
char padding[12]; /* 500-512 */
} formatted;
2001-10-25 19:48:08 +05:30
} tar;
long sum = 0;
long i;
static int end = 0;
2001-10-25 19:48:08 +05:30
2002-09-25 08:17:48 +05:30
/* Align header */
data_align(archive_handle, 512);
2002-09-25 08:17:48 +05:30
xread(archive_handle->src_fd, tar.raw, 512);
2002-09-25 08:17:48 +05:30
archive_handle->offset += 512;
2001-10-25 19:48:08 +05:30
2002-09-25 08:17:48 +05:30
/* If there is no filename its an empty header */
if (tar.formatted.name[0] == 0) {
if (end) {
/* This is the second consecutive empty header! End of archive!
* Read until the end to empty the pipe from gz or bz2
*/
while (full_read(archive_handle->src_fd, tar.raw, 512) == 512);
return(EXIT_FAILURE);
}
end = 1;
return(EXIT_SUCCESS);
2001-10-25 19:48:08 +05:30
}
end = 0;
2001-10-25 19:48:08 +05:30
/* Check header has valid magic, "ustar" is for the proper tar
* 0's are for the old tar format
2001-10-25 19:48:08 +05:30
*/
if (strncmp(tar.formatted.magic, "ustar", 5) != 0) {
#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
if (strncmp(tar.formatted.magic, "\0\0\0\0\0", 5) != 0)
#endif
2003-03-19 14:43:01 +05:30
bb_error_msg_and_die("Invalid tar magic");
2001-10-25 19:48:08 +05:30
}
/* Do checksum on headers */
2002-09-25 08:17:48 +05:30
for (i = 0; i < 148 ; i++) {
2001-10-25 19:48:08 +05:30
sum += tar.raw[i];
}
sum += ' ' * 8;
2002-09-25 08:17:48 +05:30
for (i = 156; i < 512 ; i++) {
2001-10-25 19:48:08 +05:30
sum += tar.raw[i];
}
if (sum != strtol(tar.formatted.chksum, NULL, 8)) {
2003-03-19 14:43:01 +05:30
bb_error_msg("Invalid tar header checksum");
2002-09-25 08:17:48 +05:30
return(EXIT_FAILURE);
2001-10-25 19:48:08 +05:30
}
#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
2002-11-23 16:14:47 +05:30
if (longname) {
file_header->name = longname;
longname = NULL;
}
else if (linkname) {
file_header->name = linkname;
linkname = NULL;
} else
#endif
{
file_header->name = bb_xstrndup(tar.formatted.name,100);
if (tar.formatted.prefix[0]) {
char *temp = file_header->name;
file_header->name = concat_path_file(tar.formatted.prefix, temp);
free(temp);
}
}
2002-10-19 11:49:22 +05:30
file_header->uid = strtol(tar.formatted.uid, NULL, 8);
file_header->gid = strtol(tar.formatted.gid, NULL, 8);
file_header->size = strtol(tar.formatted.size, NULL, 8);
file_header->mtime = strtol(tar.formatted.mtime, NULL, 8);
file_header->link_name = (tar.formatted.linkname[0] != '\0') ?
bb_xstrdup(tar.formatted.linkname) : NULL;
file_header->device = makedev(strtol(tar.formatted.devmajor, NULL, 8),
strtol(tar.formatted.devminor, NULL, 8));
2004-02-20 08:04:42 +05:30
/* Set bits 0-11 of the files mode */
file_header->mode = 07777 & strtol(tar.formatted.mode, NULL, 8);
2004-02-20 08:04:42 +05:30
/* Set bits 12-15 of the files mode */
switch (tar.formatted.typeflag) {
/* busybox identifies hard links as being regular files with 0 size and a link name */
case '1':
2004-02-20 08:04:42 +05:30
file_header->mode |= S_IFREG;
break;
case '7':
/* Reserved for high performance files, treat as normal file */
2002-08-22 23:17:09 +05:30
case 0:
case '0':
#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
if (last_char_is(file_header->name, '/')) {
file_header->mode |= S_IFDIR;
} else
#endif
file_header->mode |= S_IFREG;
2002-08-22 23:17:09 +05:30
break;
case '2':
2002-09-25 08:17:48 +05:30
file_header->mode |= S_IFLNK;
2002-08-22 23:17:09 +05:30
break;
case '3':
2002-09-25 08:17:48 +05:30
file_header->mode |= S_IFCHR;
2002-08-22 23:17:09 +05:30
break;
case '4':
2002-09-25 08:17:48 +05:30
file_header->mode |= S_IFBLK;
2002-08-22 23:17:09 +05:30
break;
case '5':
2002-09-25 08:17:48 +05:30
file_header->mode |= S_IFDIR;
2002-08-22 23:17:09 +05:30
break;
case '6':
2002-09-25 08:17:48 +05:30
file_header->mode |= S_IFIFO;
2002-08-22 23:17:09 +05:30
break;
#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
case 'L': {
2006-05-29 13:12:02 +05:30
longname = xzalloc(file_header->size + 1);
xread(archive_handle->src_fd, longname, file_header->size);
2002-09-25 08:17:48 +05:30
archive_handle->offset += file_header->size;
2002-11-23 16:14:47 +05:30
return(get_header_tar(archive_handle));
}
case 'K': {
2006-05-29 13:12:02 +05:30
linkname = xzalloc(file_header->size + 1);
xread(archive_handle->src_fd, linkname, file_header->size);
2002-09-25 08:17:48 +05:30
archive_handle->offset += file_header->size;
2002-09-25 08:17:48 +05:30
file_header->name = linkname;
2002-11-23 16:14:47 +05:30
return(get_header_tar(archive_handle));
}
case 'D': /* GNU dump dir */
case 'M': /* Continuation of multi volume archive*/
case 'N': /* Old GNU for names > 100 characters */
case 'S': /* Sparse file */
case 'V': /* Volume header */
#endif
case 'g': /* pax global header */
case 'x': /* pax extended header */
bb_error_msg("Ignoring extension type %c", tar.formatted.typeflag);
break;
default:
bb_error_msg("Unknown typeflag: 0x%x", tar.formatted.typeflag);
}
{ /* Strip trailing '/' in directories */
/* Must be done after mode is set as '/' is used to check if its a directory */
char *tmp = last_char_is(file_header->name, '/');
if (tmp) {
*tmp = '\0';
}
}
if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
2002-09-25 08:17:48 +05:30
archive_handle->action_header(archive_handle->file_header);
archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
archive_handle->action_data(archive_handle);
llist_add_to(&(archive_handle->passed), file_header->name);
2002-09-25 08:17:48 +05:30
} else {
data_skip(archive_handle);
2002-09-25 08:17:48 +05:30
}
archive_handle->offset += file_header->size;
2003-11-15 05:54:43 +05:30
free(file_header->link_name);
2002-09-25 08:17:48 +05:30
return(EXIT_SUCCESS);
}