blob: fb7e9ae8f79b4ddab4c866864f46b9927b0bfb1a [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Rob Landley06249fe2006-02-20 19:28:53 +00002/* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath6aa52232004-02-17 11:55:06 +00003 *
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +00004 * FIXME:
Eric Andersenaff114c2004-04-14 17:51:38 +00005 * In privileged mode if uname and gname map to a uid and gid then use the
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +00006 * mapped value instead of the uid/gid values in tar header
Glenn L McGrathb0e163a2004-02-19 08:48:30 +00007 *
8 * References:
9 * GNU tar and star man pages,
10 * Opengroup's ustar interchange format,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000011 * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
Glenn L McGrath95ebf612001-10-25 14:18:08 +000012 */
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
Rob Landley161aae72005-10-11 20:17:30 +000017#include <sys/sysmacros.h> /* For makedev */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000018#include "unarchive.h"
19#include "libbb.h"
20
Glenn L McGrath21110a02003-01-28 01:45:48 +000021#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb7a76df2002-11-23 10:44:47 +000022static char *longname = NULL;
23static char *linkname = NULL;
24#endif
25
Rob Landleydfba7412006-03-06 20:47:33 +000026char get_header_tar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +000027{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000028 file_header_t *file_header = archive_handle->file_header;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000029 union {
Glenn L McGrathef91bf62003-09-12 06:31:28 +000030 /* ustar header, Posix 1003.1 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000031 unsigned char raw[512];
32 struct {
Glenn L McGrath99b12542002-08-22 17:47:09 +000033 char name[100]; /* 0-99 */
34 char mode[8]; /* 100-107 */
35 char uid[8]; /* 108-115 */
36 char gid[8]; /* 116-123 */
37 char size[12]; /* 124-135 */
38 char mtime[12]; /* 136-147 */
39 char chksum[8]; /* 148-155 */
40 char typeflag; /* 156-156 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000041 char linkname[100]; /* 157-256 */
Glenn L McGrath99b12542002-08-22 17:47:09 +000042 char magic[6]; /* 257-262 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000043 char version[2]; /* 263-264 */
Glenn L McGrath99b12542002-08-22 17:47:09 +000044 char uname[32]; /* 265-296 */
45 char gname[32]; /* 297-328 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000046 char devmajor[8]; /* 329-336 */
47 char devminor[8]; /* 337-344 */
48 char prefix[155]; /* 345-499 */
49 char padding[12]; /* 500-512 */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000050 } formatted;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000051 } tar;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000052 long sum = 0;
53 long i;
Paul Fox94ff9f12005-07-20 19:24:13 +000054 static int end = 0;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000055
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000056 /* Align header */
Glenn L McGrath237ae422002-11-03 14:05:15 +000057 data_align(archive_handle, 512);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000058
Rob Landley53437472006-07-16 08:14:35 +000059 xread(archive_handle->src_fd, tar.raw, 512);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000060 archive_handle->offset += 512;
61
62 /* If there is no filename its an empty header */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000063 if (tar.formatted.name[0] == 0) {
Paul Fox94ff9f12005-07-20 19:24:13 +000064 if (end) {
65 /* This is the second consecutive empty header! End of archive!
66 * Read until the end to empty the pipe from gz or bz2
67 */
Rob Landley53437472006-07-16 08:14:35 +000068 while (full_read(archive_handle->src_fd, tar.raw, 512) == 512);
Paul Fox94ff9f12005-07-20 19:24:13 +000069 return(EXIT_FAILURE);
70 }
71 end = 1;
Glenn L McGrathe57feeb2003-11-14 12:57:14 +000072 return(EXIT_SUCCESS);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000073 }
Paul Fox94ff9f12005-07-20 19:24:13 +000074 end = 0;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000075
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +000076 /* Check header has valid magic, "ustar" is for the proper tar
77 * 0's are for the old tar format
Glenn L McGrath95ebf612001-10-25 14:18:08 +000078 */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000079 if (strncmp(tar.formatted.magic, "ustar", 5) != 0) {
Bernhard Reutner-Fischera1bccc02006-04-02 20:17:55 +000080#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000081 if (strncmp(tar.formatted.magic, "\0\0\0\0\0", 5) != 0)
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +000082#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 bb_error_msg_and_die("Invalid tar magic");
Glenn L McGrath95ebf612001-10-25 14:18:08 +000084 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +000085 /* Do checksum on headers */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000086 for (i = 0; i < 148 ; i++) {
Glenn L McGrath95ebf612001-10-25 14:18:08 +000087 sum += tar.raw[i];
88 }
89 sum += ' ' * 8;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000090 for (i = 156; i < 512 ; i++) {
Glenn L McGrath95ebf612001-10-25 14:18:08 +000091 sum += tar.raw[i];
92 }
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000093 if (sum != strtol(tar.formatted.chksum, NULL, 8)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 bb_error_msg("Invalid tar header checksum");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000095 return(EXIT_FAILURE);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000096 }
97
Glenn L McGrath21110a02003-01-28 01:45:48 +000098#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb7a76df2002-11-23 10:44:47 +000099 if (longname) {
100 file_header->name = longname;
101 longname = NULL;
102 }
103 else if (linkname) {
104 file_header->name = linkname;
105 linkname = NULL;
106 } else
107#endif
Rob Landley06249fe2006-02-20 19:28:53 +0000108 {
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000109 file_header->name = bb_xstrndup(tar.formatted.name,100);
Rob Landley06249fe2006-02-20 19:28:53 +0000110
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000111 if (tar.formatted.prefix[0]) {
Rob Landley06249fe2006-02-20 19:28:53 +0000112 char *temp = file_header->name;
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000113 file_header->name = concat_path_file(tar.formatted.prefix, temp);
Rob Landley06249fe2006-02-20 19:28:53 +0000114 free(temp);
115 }
Glenn L McGrath75762702002-08-22 11:50:31 +0000116 }
Glenn L McGrathc5c1a8a2002-10-19 06:19:22 +0000117
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000118 file_header->uid = strtol(tar.formatted.uid, NULL, 8);
119 file_header->gid = strtol(tar.formatted.gid, NULL, 8);
120 file_header->size = strtol(tar.formatted.size, NULL, 8);
121 file_header->mtime = strtol(tar.formatted.mtime, NULL, 8);
122 file_header->link_name = (tar.formatted.linkname[0] != '\0') ?
123 bb_xstrdup(tar.formatted.linkname) : NULL;
124 file_header->device = makedev(strtol(tar.formatted.devmajor, NULL, 8),
125 strtol(tar.formatted.devminor, NULL, 8));
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000126
Glenn L McGrath916ba532004-02-20 02:34:42 +0000127 /* Set bits 0-11 of the files mode */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000128 file_header->mode = 07777 & strtol(tar.formatted.mode, NULL, 8);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000129
130 /* Set bits 12-15 of the files mode */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000131 switch (tar.formatted.typeflag) {
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000132 /* busybox identifies hard links as being regular files with 0 size and a link name */
133 case '1':
Glenn L McGrath916ba532004-02-20 02:34:42 +0000134 file_header->mode |= S_IFREG;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000135 break;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000136 case '7':
137 /* Reserved for high performance files, treat as normal file */
Glenn L McGrath99b12542002-08-22 17:47:09 +0000138 case 0:
Glenn L McGrath21110a02003-01-28 01:45:48 +0000139 case '0':
Bernhard Reutner-Fischera1bccc02006-04-02 20:17:55 +0000140#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000141 if (last_char_is(file_header->name, '/')) {
142 file_header->mode |= S_IFDIR;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000143 } else
144#endif
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000145 file_header->mode |= S_IFREG;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000146 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000147 case '2':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000148 file_header->mode |= S_IFLNK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000149 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000150 case '3':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000151 file_header->mode |= S_IFCHR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000152 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000153 case '4':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000154 file_header->mode |= S_IFBLK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000155 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000156 case '5':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000157 file_header->mode |= S_IFDIR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000158 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000159 case '6':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000160 file_header->mode |= S_IFIFO;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000161 break;
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000162#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000163 case 'L': {
Rob Landley1ec5b292006-05-29 07:42:02 +0000164 longname = xzalloc(file_header->size + 1);
Rob Landley53437472006-07-16 08:14:35 +0000165 xread(archive_handle->src_fd, longname, file_header->size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000166 archive_handle->offset += file_header->size;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000167
Glenn L McGrathb7a76df2002-11-23 10:44:47 +0000168 return(get_header_tar(archive_handle));
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000169 }
170 case 'K': {
Rob Landley1ec5b292006-05-29 07:42:02 +0000171 linkname = xzalloc(file_header->size + 1);
Rob Landley53437472006-07-16 08:14:35 +0000172 xread(archive_handle->src_fd, linkname, file_header->size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000173 archive_handle->offset += file_header->size;
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000174
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000175 file_header->name = linkname;
Glenn L McGrathb7a76df2002-11-23 10:44:47 +0000176 return(get_header_tar(archive_handle));
Glenn L McGrathb3c4e9a2002-09-15 16:54:49 +0000177 }
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000178 case 'D': /* GNU dump dir */
179 case 'M': /* Continuation of multi volume archive*/
180 case 'N': /* Old GNU for names > 100 characters */
181 case 'S': /* Sparse file */
182 case 'V': /* Volume header */
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000183#endif
Rob Landleyff6e21c2006-07-06 20:30:19 +0000184 case 'g': /* pax global header */
185 case 'x': /* pax extended header */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000186 bb_error_msg("Ignoring extension type %c", tar.formatted.typeflag);
Rob Landleyff6e21c2006-07-06 20:30:19 +0000187 break;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000188 default:
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000189 bb_error_msg("Unknown typeflag: 0x%x", tar.formatted.typeflag);
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000190 }
191 { /* Strip trailing '/' in directories */
192 /* Must be done after mode is set as '/' is used to check if its a directory */
193 char *tmp = last_char_is(file_header->name, '/');
194 if (tmp) {
195 *tmp = '\0';
196 }
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000197 }
Glenn L McGrath3d5828f2003-08-14 02:55:15 +0000198
Glenn L McGrath8e940982002-11-04 23:47:31 +0000199 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000200 archive_handle->action_header(archive_handle->file_header);
201 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
202 archive_handle->action_data(archive_handle);
Rob Landley8bb50782006-05-26 23:44:51 +0000203 llist_add_to(&(archive_handle->passed), file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000204 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000205 data_skip(archive_handle);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000206 }
207 archive_handle->offset += file_header->size;
208
Glenn L McGrath54ac0572003-11-15 00:24:43 +0000209 free(file_header->link_name);
210
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000211 return(EXIT_SUCCESS);
Eric Andersen2276d832002-07-11 11:11:56 +0000212}