blob: 21bbc97156f56a9b31a899712173705343c94d12 [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
Glenn L McGrath95ebf612001-10-25 14:18:08 +000014#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000015#include "unarchive.h"
Glenn L McGrath95ebf612001-10-25 14:18:08 +000016
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010017typedef uint32_t aliased_uint32_t FIX_ALIASING;
18typedef off_t aliased_off_t FIX_ALIASING;
19
20
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000021/* NB: _DESTROYS_ str[len] character! */
22static unsigned long long getOctal(char *str, int len)
23{
24 unsigned long long v;
Denys Vlasenko7b48eb42010-05-06 20:08:14 +020025 char *end;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000026 /* NB: leading spaces are allowed. Using strtoull to handle that.
Denys Vlasenko8d338172009-09-23 17:16:37 +020027 * The downside is that we accept e.g. "-123" too :(
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000028 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000029 str[len] = '\0';
Denys Vlasenko7b48eb42010-05-06 20:08:14 +020030 v = strtoull(str, &end, 8);
Denys Vlasenko8d338172009-09-23 17:16:37 +020031 /* std: "Each numeric field is terminated by one or more
32 * <space> or NUL characters". We must support ' '! */
Denys Vlasenko7b48eb42010-05-06 20:08:14 +020033 if (*end != '\0' && *end != ' ') {
34 int8_t first = str[0];
35 if (!(first & 0x80))
36 bb_error_msg_and_die("corrupted octal value in tar header");
37 /*
38 * GNU tar uses "base-256 encoding" for very large numbers.
39 * Encoding is binary, with highest bit always set as a marker
40 * and sign in next-highest bit:
41 * 80 00 .. 00 - zero
42 * bf ff .. ff - largest positive number
43 * ff ff .. ff - minus 1
44 * c0 00 .. 00 - smallest negative number
45 *
46 * Example of tar file with 8914993153 (0x213600001) byte file.
47 * Field starts at offset 7c:
48 * 00070 30 30 30 00 30 30 30 30 30 30 30 00 80 00 00 00 |000.0000000.....|
49 * 00080 00 00 00 02 13 60 00 01 31 31 31 32 30 33 33 36 |.....`..11120336|
50 *
51 * NB: tarballs with NEGATIVE unix times encoded that way were seen!
52 */
53 v = first;
54 /* Sign-extend using 6th bit: */
55 v <<= sizeof(unsigned long long)*8 - 7;
56 v = (long long)v >> (sizeof(unsigned long long)*8 - 7);
57 while (--len != 0)
58 v = (v << 8) + (unsigned char) *str++;
59 }
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000060 return v;
61}
Denis Vlasenkob5963352006-11-26 01:46:59 +000062#define GET_OCTAL(a) getOctal((a), sizeof(a))
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000063
J. Tang77a2c512010-03-19 14:48:51 +010064#if ENABLE_FEATURE_TAR_SELINUX
65/* Scan a PAX header for SELinux contexts, via "RHT.security.selinux" keyword.
66 * This is what Red Hat's patched version of tar uses.
67 */
68# define SELINUX_CONTEXT_KEYWORD "RHT.security.selinux"
69static char *get_selinux_sctx_from_pax_hdr(archive_handle_t *archive_handle, unsigned sz)
70{
71 char *buf, *p;
72 char *result;
73
74 p = buf = xmalloc(sz + 1);
75 /* prevent bb_strtou from running off the buffer */
76 buf[sz] = '\0';
77 xread(archive_handle->src_fd, buf, sz);
78 archive_handle->offset += sz;
79
80 result = NULL;
81 while (sz != 0) {
82 char *end, *value;
83 unsigned len;
84
85 /* Every record has this format: "LEN NAME=VALUE\n" */
86 len = bb_strtou(p, &end, 10);
87 /* expect errno to be EINVAL, because the character
88 * following the digits should be a space
89 */
90 p += len;
91 sz -= len;
92 if ((int)sz < 0
93 || len == 0
94 || errno != EINVAL
95 || *end != ' '
96 ) {
97 bb_error_msg("malformed extended header, skipped");
98 // More verbose version:
99 //bb_error_msg("malformed extended header at %"OFF_FMT"d, skipped",
100 // archive_handle->offset - (sz + len));
101 break;
102 }
103 /* overwrite the terminating newline with NUL
104 * (we do not bother to check that it *was* a newline)
105 */
106 p[-1] = '\0';
107 /* Is it selinux security context? */
108 value = end + 1;
109 if (strncmp(value, SELINUX_CONTEXT_KEYWORD"=", sizeof(SELINUX_CONTEXT_KEYWORD"=") - 1) == 0) {
110 value += sizeof(SELINUX_CONTEXT_KEYWORD"=") - 1;
111 result = xstrdup(value);
112 break;
113 }
114 }
115
116 free(buf);
117 return result;
118}
119#endif
120
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000121char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000122{
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000123 file_header_t *file_header = archive_handle->file_header;
Denys Vlasenko52827e32010-06-26 18:21:36 +0200124 struct tar_header_t tar;
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000125 char *cp;
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000126 int i, sum_u, sum;
127#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
128 int sum_s;
129#endif
Denis Vlasenkod6772502006-11-24 17:21:44 +0000130 int parse_names;
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000131
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000132 /* Our "private data" */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000133#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100134# define p_longname (archive_handle->tar__longname)
135# define p_linkname (archive_handle->tar__linkname)
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000136#else
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100137# define p_longname 0
138# define p_linkname 0
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000139#endif
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000140
J. Tang77a2c512010-03-19 14:48:51 +0100141#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS || ENABLE_FEATURE_TAR_SELINUX
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000142 again:
143#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000144 /* Align header */
Glenn L McGrath237ae422002-11-03 14:05:15 +0000145 data_align(archive_handle, 512);
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000146
Denis Vlasenkob5963352006-11-26 01:46:59 +0000147 again_after_align:
148
Denis Vlasenko1f0b95f2009-03-13 14:26:44 +0000149#if ENABLE_DESKTOP || ENABLE_FEATURE_TAR_AUTODETECT
Denis Vlasenko0381d422008-07-10 23:06:00 +0000150 /* to prevent misdetection of bz2 sig */
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100151 *(aliased_uint32_t*)&tar = 0;
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +0000152 i = full_read(archive_handle->src_fd, &tar, 512);
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000153 /* If GNU tar sees EOF in above read, it says:
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +0000154 * "tar: A lone zero block at N", where N = kilobyte
155 * where EOF was met (not EOF block, actual EOF!),
Denis Vlasenko0381d422008-07-10 23:06:00 +0000156 * and exits with EXIT_SUCCESS.
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000157 * We will mimic exit(EXIT_SUCCESS), although we will not mimic
Denis Vlasenko12c06222008-02-14 08:52:30 +0000158 * the message and we don't check whether we indeed
159 * saw zero block directly before this. */
Denis Vlasenko0381d422008-07-10 23:06:00 +0000160 if (i == 0) {
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +0000161 xfunc_error_retval = 0;
Denis Vlasenko0381d422008-07-10 23:06:00 +0000162 short_read:
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +0000163 bb_error_msg_and_die("short read");
Denis Vlasenko0381d422008-07-10 23:06:00 +0000164 }
165 if (i != 512) {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000166 IF_FEATURE_TAR_AUTODETECT(goto autodetect;)
Denis Vlasenko0381d422008-07-10 23:06:00 +0000167 goto short_read;
168 }
169
Denis Vlasenko12c06222008-02-14 08:52:30 +0000170#else
Denis Vlasenko0381d422008-07-10 23:06:00 +0000171 i = 512;
172 xread(archive_handle->src_fd, &tar, i);
Denis Vlasenko12c06222008-02-14 08:52:30 +0000173#endif
Denis Vlasenko0381d422008-07-10 23:06:00 +0000174 archive_handle->offset += i;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000175
176 /* If there is no filename its an empty header */
Denis Vlasenko05efca92008-04-29 04:12:58 +0000177 if (tar.name[0] == 0 && tar.prefix[0] == 0) {
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100178 if (archive_handle->tar__end) {
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000179 /* Second consecutive empty header - end of archive.
Paul Fox94ff9f12005-07-20 19:24:13 +0000180 * Read until the end to empty the pipe from gz or bz2
181 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000182 while (full_read(archive_handle->src_fd, &tar, 512) == 512)
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000183 continue;
Denis Vlasenko13858992006-10-08 12:49:22 +0000184 return EXIT_FAILURE;
Paul Fox94ff9f12005-07-20 19:24:13 +0000185 }
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100186 archive_handle->tar__end = 1;
Denis Vlasenko13858992006-10-08 12:49:22 +0000187 return EXIT_SUCCESS;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000188 }
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100189 archive_handle->tar__end = 0;
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000190
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000191 /* Check header has valid magic, "ustar" is for the proper tar,
192 * five NULs are for the old tar format */
193 if (strncmp(tar.magic, "ustar", 5) != 0
194 && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
195 || memcmp(tar.magic, "\0\0\0\0", 5) != 0)
196 ) {
197#if ENABLE_FEATURE_TAR_AUTODETECT
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000198 char FAST_FUNC (*get_header_ptr)(archive_handle_t *);
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000199
Denis Vlasenko1f0b95f2009-03-13 14:26:44 +0000200 autodetect:
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000201 /* tar gz/bz autodetect: check for gz/bz2 magic.
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000202 * If we see the magic, and it is the very first block,
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000203 * we can switch to get_header_tar_gz/bz2/lzma().
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000204 * Needs seekable fd. I wish recv(MSG_PEEK) works
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000205 * on any fd... */
Denys Vlasenko52827e32010-06-26 18:21:36 +0200206# if ENABLE_FEATURE_SEAMLESS_GZ
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000207 if (tar.name[0] == 0x1f && tar.name[1] == (char)0x8b) { /* gzip */
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000208 get_header_ptr = get_header_tar_gz;
209 } else
Denys Vlasenko52827e32010-06-26 18:21:36 +0200210# endif
211# if ENABLE_FEATURE_SEAMLESS_BZ2
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000212 if (tar.name[0] == 'B' && tar.name[1] == 'Z'
213 && tar.name[2] == 'h' && isdigit(tar.name[3])
214 ) { /* bzip2 */
215 get_header_ptr = get_header_tar_bz2;
216 } else
Denys Vlasenko52827e32010-06-26 18:21:36 +0200217# endif
218# if ENABLE_FEATURE_SEAMLESS_XZ
219 //TODO
220# endif
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000221 goto err;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000222 /* Two different causes for lseek() != 0:
223 * unseekable fd (would like to support that too, but...),
224 * or not first block (false positive, it's not .gz/.bz2!) */
Denis Vlasenko0381d422008-07-10 23:06:00 +0000225 if (lseek(archive_handle->src_fd, -i, SEEK_CUR) != 0)
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000226 goto err;
227 while (get_header_ptr(archive_handle) == EXIT_SUCCESS)
228 continue;
229 return EXIT_FAILURE;
230 err:
231#endif /* FEATURE_TAR_AUTODETECT */
232 bb_error_msg_and_die("invalid tar magic");
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000233 }
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000234
235 /* Do checksum on headers.
236 * POSIX says that checksum is done on unsigned bytes, but
Denis Vlasenko940494f2007-03-04 18:09:50 +0000237 * Sun and HP-UX gets it wrong... more details in
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000238 * GNU tar source. */
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000239#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
240 sum_s = ' ' * sizeof(tar.chksum);
241#endif
242 sum_u = ' ' * sizeof(tar.chksum);
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000243 for (i = 0; i < 148; i++) {
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000244 sum_u += ((unsigned char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000245#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000246 sum_s += ((signed char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000247#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000248 }
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000249 for (i = 156; i < 512; i++) {
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000250 sum_u += ((unsigned char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000251#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000252 sum_s += ((signed char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000253#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000254 }
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000255 /* This field does not need special treatment (getOctal) */
Denys Vlasenko8d338172009-09-23 17:16:37 +0200256 {
257 char *endp; /* gcc likes temp var for &endp */
258 sum = strtoul(tar.chksum, &endp, 8);
259 if ((*endp != '\0' && *endp != ' ')
260 || (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum))
261 ) {
262 bb_error_msg_and_die("invalid tar header checksum");
263 }
264 }
Denys Vlasenkod6459682010-01-03 23:41:11 +0100265 /* don't use xstrtoul, tar.chksum may have leading spaces */
266 sum = strtoul(tar.chksum, NULL, 8);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000267 if (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) {
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000268 bb_error_msg_and_die("invalid tar header checksum");
269 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000270
Denis Vlasenkob5963352006-11-26 01:46:59 +0000271 /* 0 is reserved for high perf file, treat as normal file */
272 if (!tar.typeflag) tar.typeflag = '0';
273 parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
Denis Vlasenkod6772502006-11-24 17:21:44 +0000274
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000275 /* getOctal trashes subsequent field, therefore we call it
276 * on fields in reverse order */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000277 if (tar.devmajor[0]) {
Denis Vlasenkod93400b2008-04-29 03:54:16 +0000278 char t = tar.prefix[0];
279 /* we trash prefix[0] here, but we DO need it later! */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000280 unsigned minor = GET_OCTAL(tar.devminor);
281 unsigned major = GET_OCTAL(tar.devmajor);
282 file_header->device = makedev(major, minor);
Denis Vlasenkod93400b2008-04-29 03:54:16 +0000283 tar.prefix[0] = t;
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000284 }
Denis Vlasenko75103842007-06-20 14:49:47 +0000285 file_header->link_target = NULL;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000286 if (!p_linkname && parse_names && tar.linkname[0]) {
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000287 file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname));
Denis Vlasenko75103842007-06-20 14:49:47 +0000288 /* FIXME: what if we have non-link object with link_target? */
289 /* Will link_target be free()ed? */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000290 }
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000291#if ENABLE_FEATURE_TAR_UNAME_GNAME
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100292 file_header->tar__uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL;
293 file_header->tar__gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL;
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000294#endif
Denys Vlasenko7b48eb42010-05-06 20:08:14 +0200295 file_header->mtime = GET_OCTAL(tar.mtime);
296 file_header->size = GET_OCTAL(tar.size);
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000297 file_header->gid = GET_OCTAL(tar.gid);
298 file_header->uid = GET_OCTAL(tar.uid);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000299 /* Set bits 0-11 of the files mode */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000300 file_header->mode = 07777 & GET_OCTAL(tar.mode);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000301
Denis Vlasenkob5963352006-11-26 01:46:59 +0000302 file_header->name = NULL;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000303 if (!p_longname && parse_names) {
Denis Vlasenkod6772502006-11-24 17:21:44 +0000304 /* we trash mode[0] here, it's ok */
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000305 //tar.name[sizeof(tar.name)] = '\0'; - gcc 4.3.0 would complain
306 tar.mode[0] = '\0';
Denis Vlasenko87cd4a82006-11-25 23:47:32 +0000307 if (tar.prefix[0]) {
308 /* and padding[0] */
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000309 //tar.prefix[sizeof(tar.prefix)] = '\0'; - gcc 4.3.0 would complain
310 tar.padding[0] = '\0';
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000311 file_header->name = concat_path_file(tar.prefix, tar.name);
Denis Vlasenko87cd4a82006-11-25 23:47:32 +0000312 } else
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000313 file_header->name = xstrdup(tar.name);
314 }
315
Glenn L McGrath916ba532004-02-20 02:34:42 +0000316 /* Set bits 12-15 of the files mode */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000317 /* (typeflag was not trashed because chksum does not use getOctal) */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000318 switch (tar.typeflag) {
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000319 /* busybox identifies hard links as being regular files with 0 size and a link name */
320 case '1':
Glenn L McGrath916ba532004-02-20 02:34:42 +0000321 file_header->mode |= S_IFREG;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000322 break;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000323 case '7':
Denis Vlasenkob5963352006-11-26 01:46:59 +0000324 /* case 0: */
Glenn L McGrath21110a02003-01-28 01:45:48 +0000325 case '0':
Denis Vlasenkod6772502006-11-24 17:21:44 +0000326#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000327 if (last_char_is(file_header->name, '/')) {
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000328 goto set_dir;
329 }
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000330#endif
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000331 file_header->mode |= S_IFREG;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000332 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000333 case '2':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000334 file_header->mode |= S_IFLNK;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000335 /* have seen tarballs with size field containing
336 * the size of the link target's name */
337 size0:
338 file_header->size = 0;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000339 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000340 case '3':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000341 file_header->mode |= S_IFCHR;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000342 goto size0; /* paranoia */
Glenn L McGrath21110a02003-01-28 01:45:48 +0000343 case '4':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000344 file_header->mode |= S_IFBLK;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000345 goto size0;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000346 case '5':
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000347 IF_FEATURE_TAR_OLDGNU_COMPATIBILITY(set_dir:)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000348 file_header->mode |= S_IFDIR;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000349 goto size0;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000350 case '6':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000351 file_header->mode |= S_IFIFO;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000352 goto size0;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000353#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000354 case 'L':
Denis Vlasenkob5963352006-11-26 01:46:59 +0000355 /* free: paranoia: tar with several consecutive longnames */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000356 free(p_longname);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000357 /* For paranoia reasons we allocate extra NUL char */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000358 p_longname = xzalloc(file_header->size + 1);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000359 /* We read ASCIZ string, including NUL */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000360 xread(archive_handle->src_fd, p_longname, file_header->size);
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000361 archive_handle->offset += file_header->size;
Denis Vlasenkob5963352006-11-26 01:46:59 +0000362 /* return get_header_tar(archive_handle); */
363 /* gcc 4.1.1 didn't optimize it into jump */
364 /* so we will do it ourself, this also saves stack */
365 goto again;
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000366 case 'K':
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000367 free(p_linkname);
368 p_linkname = xzalloc(file_header->size + 1);
369 xread(archive_handle->src_fd, p_linkname, file_header->size);
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000370 archive_handle->offset += file_header->size;
Denis Vlasenkob5963352006-11-26 01:46:59 +0000371 /* return get_header_tar(archive_handle); */
372 goto again;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000373 case 'D': /* GNU dump dir */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000374 case 'M': /* Continuation of multi volume archive */
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000375 case 'N': /* Old GNU for names > 100 characters */
376 case 'S': /* Sparse file */
377 case 'V': /* Volume header */
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000378#endif
J. Tang77a2c512010-03-19 14:48:51 +0100379#if !ENABLE_FEATURE_TAR_SELINUX
Rob Landleyff6e21c2006-07-06 20:30:19 +0000380 case 'g': /* pax global header */
J. Tang77a2c512010-03-19 14:48:51 +0100381 case 'x': /* pax extended header */
382#else
383 skip_ext_hdr:
384#endif
385 {
Denis Vlasenkob5963352006-11-26 01:46:59 +0000386 off_t sz;
387 bb_error_msg("warning: skipping header '%c'", tar.typeflag);
388 sz = (file_header->size + 511) & ~(off_t)511;
389 archive_handle->offset += sz;
390 sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
391 while (sz--)
392 xread(archive_handle->src_fd, &tar, 512);
393 /* return get_header_tar(archive_handle); */
394 goto again_after_align;
395 }
J. Tang77a2c512010-03-19 14:48:51 +0100396#if ENABLE_FEATURE_TAR_SELINUX
397 case 'g': /* pax global header */
398 case 'x': { /* pax extended header */
399 char **pp;
400 if ((uoff_t)file_header->size > 0xfffff) /* paranoia */
401 goto skip_ext_hdr;
402 pp = (tar.typeflag == 'g') ? &archive_handle->tar__global_sctx : &archive_handle->tar__next_file_sctx;
403 free(*pp);
404 *pp = get_selinux_sctx_from_pax_hdr(archive_handle, file_header->size);
405 goto again;
406 }
407#endif
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000408 default:
Denis Vlasenkob5963352006-11-26 01:46:59 +0000409 bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag);
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000410 }
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000411
Denis Vlasenkod6772502006-11-24 17:21:44 +0000412#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000413 if (p_longname) {
414 file_header->name = p_longname;
415 p_longname = NULL;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000416 }
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000417 if (p_linkname) {
418 file_header->link_target = p_linkname;
419 p_linkname = NULL;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000420 }
421#endif
Denis Vlasenko0381d422008-07-10 23:06:00 +0000422 if (strncmp(file_header->name, "/../"+1, 3) == 0
Denis Vlasenkoc1660fe2006-11-26 15:42:03 +0000423 || strstr(file_header->name, "/../")
424 ) {
425 bb_error_msg_and_die("name with '..' encountered: '%s'",
426 file_header->name);
427 }
Denis Vlasenkod6772502006-11-24 17:21:44 +0000428
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000429 /* Strip trailing '/' in directories */
Denis Vlasenko714701c2006-12-22 00:21:07 +0000430 /* Must be done after mode is set as '/' is used to check if it's a directory */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000431 cp = last_char_is(file_header->name, '/');
Glenn L McGrath3d5828f2003-08-14 02:55:15 +0000432
Glenn L McGrath8e940982002-11-04 23:47:31 +0000433 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Denis Vlasenko0381d422008-07-10 23:06:00 +0000434 archive_handle->action_header(/*archive_handle->*/ file_header);
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000435 /* Note that we kill the '/' only after action_header() */
436 /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
Denys Vlasenko02365a62010-04-09 10:52:52 +0200437 if (cp)
438 *cp = '\0';
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000439 archive_handle->action_data(archive_handle);
Denys Vlasenkob768aeb2010-06-26 18:22:41 +0200440 if (archive_handle->accept || archive_handle->reject)
441 llist_add_to(&archive_handle->passed, file_header->name);
442 else /* Caller isn't interested in list of unpacked files */
443 free(file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000444 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000445 data_skip(archive_handle);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000446 free(file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000447 }
448 archive_handle->offset += file_header->size;
449
Denis Vlasenko75103842007-06-20 14:49:47 +0000450 free(file_header->link_target);
Denys Vlasenkob768aeb2010-06-26 18:22:41 +0200451 /* Do not free(file_header->name)!
452 * It might be inserted in archive_handle->passed - see above */
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000453#if ENABLE_FEATURE_TAR_UNAME_GNAME
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100454 free(file_header->tar__uname);
455 free(file_header->tar__gname);
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000456#endif
Denis Vlasenko13858992006-10-08 12:49:22 +0000457 return EXIT_SUCCESS;
Eric Andersen2276d832002-07-11 11:11:56 +0000458}