blob: aeb54190f88269b6a1a91c3acfe92ccc7608dcfe [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenko2ab94032017-10-05 15:33:28 +02002/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath6aa52232004-02-17 11:55:06 +00004 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * FIXME:
Eric Andersenaff114c2004-04-14 17:51:38 +00006 * In privileged mode if uname and gname map to a uid and gid then use the
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +00007 * mapped value instead of the uid/gid values in tar header
Glenn L McGrathb0e163a2004-02-19 08:48:30 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * References:
Glenn L McGrathb0e163a2004-02-19 08:48:30 +000010 * GNU tar and star man pages,
11 * Opengroup's ustar interchange format,
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020012 * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
Glenn L McGrath95ebf612001-10-25 14:18:08 +000013 */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000014#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +020015#include "bb_archive.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
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000020/* NB: _DESTROYS_ str[len] character! */
21static unsigned long long getOctal(char *str, int len)
22{
23 unsigned long long v;
Denys Vlasenko7b48eb42010-05-06 20:08:14 +020024 char *end;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000025 /* NB: leading spaces are allowed. Using strtoull to handle that.
Denys Vlasenko8d338172009-09-23 17:16:37 +020026 * The downside is that we accept e.g. "-123" too :(
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000027 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000028 str[len] = '\0';
Denys Vlasenko7b48eb42010-05-06 20:08:14 +020029 v = strtoull(str, &end, 8);
Denys Vlasenko8d338172009-09-23 17:16:37 +020030 /* std: "Each numeric field is terminated by one or more
31 * <space> or NUL characters". We must support ' '! */
Denys Vlasenko7b48eb42010-05-06 20:08:14 +020032 if (*end != '\0' && *end != ' ') {
33 int8_t first = str[0];
34 if (!(first & 0x80))
35 bb_error_msg_and_die("corrupted octal value in tar header");
36 /*
37 * GNU tar uses "base-256 encoding" for very large numbers.
38 * Encoding is binary, with highest bit always set as a marker
39 * and sign in next-highest bit:
40 * 80 00 .. 00 - zero
41 * bf ff .. ff - largest positive number
42 * ff ff .. ff - minus 1
43 * c0 00 .. 00 - smallest negative number
44 *
45 * Example of tar file with 8914993153 (0x213600001) byte file.
46 * Field starts at offset 7c:
47 * 00070 30 30 30 00 30 30 30 30 30 30 30 00 80 00 00 00 |000.0000000.....|
48 * 00080 00 00 00 02 13 60 00 01 31 31 31 32 30 33 33 36 |.....`..11120336|
49 *
50 * NB: tarballs with NEGATIVE unix times encoded that way were seen!
51 */
Denys Vlasenkof74f2802011-10-19 14:51:12 +020052 /* Sign-extend 7bit 'first' to 64bit 'v' (that is, using 6th bit as sign): */
53 first <<= 1;
54 first >>= 1; /* now 7th bit = 6th bit */
55 v = first; /* sign-extend 8 bits to 64 */
Denys Vlasenko7b48eb42010-05-06 20:08:14 +020056 while (--len != 0)
Etienne Le Sueurcfc212c2012-06-09 08:37:05 +020057 v = (v << 8) + (uint8_t) *++str;
Denys Vlasenko7b48eb42010-05-06 20:08:14 +020058 }
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000059 return v;
60}
Denis Vlasenkob5963352006-11-26 01:46:59 +000061#define GET_OCTAL(a) getOctal((a), sizeof(a))
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000062
Denys Vlasenko015db582016-06-19 18:15:33 +020063#define TAR_EXTD (ENABLE_FEATURE_TAR_GNU_EXTENSIONS || ENABLE_FEATURE_TAR_SELINUX)
64#if !TAR_EXTD
65#define process_pax_hdr(archive_handle, sz, global) \
66 process_pax_hdr(archive_handle, sz)
67#endif
Denys Vlasenko6111f962012-02-23 13:45:18 +010068/* "global" is 0 or 1 */
69static void process_pax_hdr(archive_handle_t *archive_handle, unsigned sz, int global)
J. Tang77a2c512010-03-19 14:48:51 +010070{
Denys Vlasenko015db582016-06-19 18:15:33 +020071#if !TAR_EXTD
72 unsigned blk_sz = (sz + 511) & (~511);
73 seek_by_read(archive_handle->src_fd, blk_sz);
74#else
75 unsigned blk_sz = (sz + 511) & (~511);
J. Tang77a2c512010-03-19 14:48:51 +010076 char *buf, *p;
J. Tang77a2c512010-03-19 14:48:51 +010077
Denys Vlasenko6111f962012-02-23 13:45:18 +010078 p = buf = xmalloc(blk_sz + 1);
79 xread(archive_handle->src_fd, buf, blk_sz);
80 archive_handle->offset += blk_sz;
81
J. Tang77a2c512010-03-19 14:48:51 +010082 /* prevent bb_strtou from running off the buffer */
83 buf[sz] = '\0';
J. Tang77a2c512010-03-19 14:48:51 +010084
J. Tang77a2c512010-03-19 14:48:51 +010085 while (sz != 0) {
86 char *end, *value;
87 unsigned len;
88
89 /* Every record has this format: "LEN NAME=VALUE\n" */
90 len = bb_strtou(p, &end, 10);
91 /* expect errno to be EINVAL, because the character
92 * following the digits should be a space
93 */
94 p += len;
95 sz -= len;
Denys Vlasenko0f592d72014-01-10 18:02:38 +010096 if (
97 /** (int)sz < 0 - not good enough for huge malicious VALUE of 2^32-1 */
98 (int)(sz|len) < 0 /* this works */
J. Tang77a2c512010-03-19 14:48:51 +010099 || len == 0
100 || errno != EINVAL
101 || *end != ' '
102 ) {
103 bb_error_msg("malformed extended header, skipped");
104 // More verbose version:
105 //bb_error_msg("malformed extended header at %"OFF_FMT"d, skipped",
106 // archive_handle->offset - (sz + len));
107 break;
108 }
109 /* overwrite the terminating newline with NUL
110 * (we do not bother to check that it *was* a newline)
111 */
112 p[-1] = '\0';
J. Tang77a2c512010-03-19 14:48:51 +0100113 value = end + 1;
Denys Vlasenko6111f962012-02-23 13:45:18 +0100114
Denys Vlasenko015db582016-06-19 18:15:33 +0200115# if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
Denys Vlasenko9655f952016-11-11 17:56:45 +0100116 if (!global) {
117 if (is_prefixed_with(value, "path=")) {
118 value += sizeof("path=") - 1;
119 free(archive_handle->tar__longname);
120 archive_handle->tar__longname = xstrdup(value);
121 continue;
122 }
123 if (is_prefixed_with(value, "linkpath=")) {
124 value += sizeof("linkpath=") - 1;
125 free(archive_handle->tar__linkname);
126 archive_handle->tar__linkname = xstrdup(value);
127 continue;
128 }
Denys Vlasenko6111f962012-02-23 13:45:18 +0100129 }
Denys Vlasenko015db582016-06-19 18:15:33 +0200130# endif
Denys Vlasenko6111f962012-02-23 13:45:18 +0100131
Denys Vlasenko015db582016-06-19 18:15:33 +0200132# if ENABLE_FEATURE_TAR_SELINUX
Denys Vlasenko6111f962012-02-23 13:45:18 +0100133 /* Scan for SELinux contexts, via "RHT.security.selinux" keyword.
134 * This is what Red Hat's patched version of tar uses.
135 */
Denys Vlasenko015db582016-06-19 18:15:33 +0200136# define SELINUX_CONTEXT_KEYWORD "RHT.security.selinux"
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100137 if (is_prefixed_with(value, SELINUX_CONTEXT_KEYWORD"=")) {
J. Tang77a2c512010-03-19 14:48:51 +0100138 value += sizeof(SELINUX_CONTEXT_KEYWORD"=") - 1;
Denys Vlasenko6111f962012-02-23 13:45:18 +0100139 free(archive_handle->tar__sctx[global]);
140 archive_handle->tar__sctx[global] = xstrdup(value);
141 continue;
J. Tang77a2c512010-03-19 14:48:51 +0100142 }
Denys Vlasenko015db582016-06-19 18:15:33 +0200143# endif
J. Tang77a2c512010-03-19 14:48:51 +0100144 }
145
146 free(buf);
Denys Vlasenko015db582016-06-19 18:15:33 +0200147#endif
J. Tang77a2c512010-03-19 14:48:51 +0100148}
J. Tang77a2c512010-03-19 14:48:51 +0100149
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000150char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000151{
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000152 file_header_t *file_header = archive_handle->file_header;
Denys Vlasenko52827e32010-06-26 18:21:36 +0200153 struct tar_header_t tar;
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000154 char *cp;
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000155 int i, sum_u, sum;
156#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
157 int sum_s;
158#endif
Denis Vlasenkod6772502006-11-24 17:21:44 +0000159 int parse_names;
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000160
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000161 /* Our "private data" */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000162#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100163# define p_longname (archive_handle->tar__longname)
164# define p_linkname (archive_handle->tar__linkname)
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000165#else
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100166# define p_longname 0
167# define p_linkname 0
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000168#endif
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000169
J. Tang77a2c512010-03-19 14:48:51 +0100170#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS || ENABLE_FEATURE_TAR_SELINUX
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000171 again:
172#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000173 /* Align header */
Glenn L McGrath237ae422002-11-03 14:05:15 +0000174 data_align(archive_handle, 512);
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000175
Denis Vlasenkob5963352006-11-26 01:46:59 +0000176 again_after_align:
177
Denis Vlasenko1f0b95f2009-03-13 14:26:44 +0000178#if ENABLE_DESKTOP || ENABLE_FEATURE_TAR_AUTODETECT
Denis Vlasenko0381d422008-07-10 23:06:00 +0000179 /* to prevent misdetection of bz2 sig */
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100180 *(aliased_uint32_t*)&tar = 0;
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +0000181 i = full_read(archive_handle->src_fd, &tar, 512);
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000182 /* If GNU tar sees EOF in above read, it says:
Denis Vlasenko23ffb6a2008-02-13 17:52:42 +0000183 * "tar: A lone zero block at N", where N = kilobyte
184 * where EOF was met (not EOF block, actual EOF!),
Denis Vlasenko0381d422008-07-10 23:06:00 +0000185 * and exits with EXIT_SUCCESS.
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000186 * We will mimic exit(EXIT_SUCCESS), although we will not mimic
Denis Vlasenko12c06222008-02-14 08:52:30 +0000187 * the message and we don't check whether we indeed
188 * saw zero block directly before this. */
Denis Vlasenko0381d422008-07-10 23:06:00 +0000189 if (i == 0) {
Denys Vlasenko9655f952016-11-11 17:56:45 +0100190 /* GNU tar 1.29 will be silent if tar archive ends abruptly
191 * (if there are no zero blocks at all, and last read returns zero,
192 * not short read 0 < len < 512). Complain only if
193 * the very first read fails. Grrr.
194 */
195 if (archive_handle->offset == 0)
196 bb_error_msg("short read");
Denys Vlasenkoebfa9b52013-11-19 14:44:04 +0100197 /* this merely signals end of archive, not exit(1): */
198 return EXIT_FAILURE;
Denis Vlasenko0381d422008-07-10 23:06:00 +0000199 }
200 if (i != 512) {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000201 IF_FEATURE_TAR_AUTODETECT(goto autodetect;)
Denys Vlasenkoebfa9b52013-11-19 14:44:04 +0100202 bb_error_msg_and_die("short read");
Denis Vlasenko0381d422008-07-10 23:06:00 +0000203 }
204
Denis Vlasenko12c06222008-02-14 08:52:30 +0000205#else
Denis Vlasenko0381d422008-07-10 23:06:00 +0000206 i = 512;
207 xread(archive_handle->src_fd, &tar, i);
Denis Vlasenko12c06222008-02-14 08:52:30 +0000208#endif
Denis Vlasenko0381d422008-07-10 23:06:00 +0000209 archive_handle->offset += i;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000210
211 /* If there is no filename its an empty header */
Denys Vlasenko9655f952016-11-11 17:56:45 +0100212 if (tar.name[0] == 0 && tar.prefix[0] == 0
213 /* Have seen a tar archive with pax 'x' header supplying UTF8 filename,
214 * with actual file having all name fields NUL-filled. Check this: */
215 && !p_longname
216 ) {
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100217 if (archive_handle->tar__end) {
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000218 /* Second consecutive empty header - end of archive.
Paul Fox94ff9f12005-07-20 19:24:13 +0000219 * Read until the end to empty the pipe from gz or bz2
220 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000221 while (full_read(archive_handle->src_fd, &tar, 512) == 512)
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000222 continue;
Denys Vlasenkoebfa9b52013-11-19 14:44:04 +0100223 return EXIT_FAILURE; /* "end of archive" */
Paul Fox94ff9f12005-07-20 19:24:13 +0000224 }
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100225 archive_handle->tar__end = 1;
Denys Vlasenkoebfa9b52013-11-19 14:44:04 +0100226 return EXIT_SUCCESS; /* "decoded one header" */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000227 }
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100228 archive_handle->tar__end = 0;
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000229
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000230 /* Check header has valid magic, "ustar" is for the proper tar,
231 * five NULs are for the old tar format */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100232 if (!is_prefixed_with(tar.magic, "ustar")
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000233 && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
234 || memcmp(tar.magic, "\0\0\0\0", 5) != 0)
235 ) {
236#if ENABLE_FEATURE_TAR_AUTODETECT
Denis Vlasenko1f0b95f2009-03-13 14:26:44 +0000237 autodetect:
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000238 /* Two different causes for lseek() != 0:
239 * unseekable fd (would like to support that too, but...),
240 * or not first block (false positive, it's not .gz/.bz2!) */
Denis Vlasenko0381d422008-07-10 23:06:00 +0000241 if (lseek(archive_handle->src_fd, -i, SEEK_CUR) != 0)
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000242 goto err;
Denys Vlasenko640ce3d2014-02-02 02:06:38 +0100243 if (setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_compressed:*/ 0) != 0)
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000244 err:
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100245 bb_error_msg_and_die("invalid tar magic");
246 archive_handle->offset = 0;
247 goto again_after_align;
248#endif
Denis Vlasenko431a7c92008-02-19 11:26:28 +0000249 bb_error_msg_and_die("invalid tar magic");
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000250 }
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000251
252 /* Do checksum on headers.
253 * POSIX says that checksum is done on unsigned bytes, but
Denis Vlasenko940494f2007-03-04 18:09:50 +0000254 * Sun and HP-UX gets it wrong... more details in
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000255 * GNU tar source. */
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000256#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
257 sum_s = ' ' * sizeof(tar.chksum);
258#endif
259 sum_u = ' ' * sizeof(tar.chksum);
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000260 for (i = 0; i < 148; i++) {
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000261 sum_u += ((unsigned char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000262#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000263 sum_s += ((signed char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000264#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000265 }
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000266 for (i = 156; i < 512; i++) {
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000267 sum_u += ((unsigned char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000268#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000269 sum_s += ((signed char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000270#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000271 }
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000272 /* This field does not need special treatment (getOctal) */
Denys Vlasenko8d338172009-09-23 17:16:37 +0200273 {
274 char *endp; /* gcc likes temp var for &endp */
275 sum = strtoul(tar.chksum, &endp, 8);
276 if ((*endp != '\0' && *endp != ' ')
277 || (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum))
278 ) {
279 bb_error_msg_and_die("invalid tar header checksum");
280 }
281 }
Denys Vlasenkod6459682010-01-03 23:41:11 +0100282 /* don't use xstrtoul, tar.chksum may have leading spaces */
283 sum = strtoul(tar.chksum, NULL, 8);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000284 if (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) {
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000285 bb_error_msg_and_die("invalid tar header checksum");
286 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000287
Denis Vlasenkob5963352006-11-26 01:46:59 +0000288 /* 0 is reserved for high perf file, treat as normal file */
289 if (!tar.typeflag) tar.typeflag = '0';
290 parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
Denis Vlasenkod6772502006-11-24 17:21:44 +0000291
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000292 /* getOctal trashes subsequent field, therefore we call it
293 * on fields in reverse order */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000294 if (tar.devmajor[0]) {
Denis Vlasenkod93400b2008-04-29 03:54:16 +0000295 char t = tar.prefix[0];
296 /* we trash prefix[0] here, but we DO need it later! */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000297 unsigned minor = GET_OCTAL(tar.devminor);
298 unsigned major = GET_OCTAL(tar.devmajor);
299 file_header->device = makedev(major, minor);
Denis Vlasenkod93400b2008-04-29 03:54:16 +0000300 tar.prefix[0] = t;
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000301 }
Denis Vlasenko75103842007-06-20 14:49:47 +0000302 file_header->link_target = NULL;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000303 if (!p_linkname && parse_names && tar.linkname[0]) {
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000304 file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname));
Denis Vlasenko75103842007-06-20 14:49:47 +0000305 /* FIXME: what if we have non-link object with link_target? */
306 /* Will link_target be free()ed? */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000307 }
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000308#if ENABLE_FEATURE_TAR_UNAME_GNAME
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100309 file_header->tar__uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL;
310 file_header->tar__gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL;
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000311#endif
Denys Vlasenko7b48eb42010-05-06 20:08:14 +0200312 file_header->mtime = GET_OCTAL(tar.mtime);
313 file_header->size = GET_OCTAL(tar.size);
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000314 file_header->gid = GET_OCTAL(tar.gid);
315 file_header->uid = GET_OCTAL(tar.uid);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000316 /* Set bits 0-11 of the files mode */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000317 file_header->mode = 07777 & GET_OCTAL(tar.mode);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000318
Denis Vlasenkob5963352006-11-26 01:46:59 +0000319 file_header->name = NULL;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000320 if (!p_longname && parse_names) {
Denis Vlasenkod6772502006-11-24 17:21:44 +0000321 /* we trash mode[0] here, it's ok */
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000322 //tar.name[sizeof(tar.name)] = '\0'; - gcc 4.3.0 would complain
323 tar.mode[0] = '\0';
Denis Vlasenko87cd4a82006-11-25 23:47:32 +0000324 if (tar.prefix[0]) {
325 /* and padding[0] */
Denis Vlasenkobc1918a2008-04-15 01:17:50 +0000326 //tar.prefix[sizeof(tar.prefix)] = '\0'; - gcc 4.3.0 would complain
327 tar.padding[0] = '\0';
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000328 file_header->name = concat_path_file(tar.prefix, tar.name);
Denis Vlasenko87cd4a82006-11-25 23:47:32 +0000329 } else
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000330 file_header->name = xstrdup(tar.name);
331 }
332
Glenn L McGrath916ba532004-02-20 02:34:42 +0000333 /* Set bits 12-15 of the files mode */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000334 /* (typeflag was not trashed because chksum does not use getOctal) */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000335 switch (tar.typeflag) {
Ian Wienand954dbd32011-07-29 08:33:47 +0200336 case '1': /* hardlink */
337 /* we mark hardlinks as regular files with zero size and a link name */
Glenn L McGrath916ba532004-02-20 02:34:42 +0000338 file_header->mode |= S_IFREG;
Ian Wienand954dbd32011-07-29 08:33:47 +0200339 /* on size of link fields from star(4)
340 * ... For tar archives written by pre POSIX.1-1988
341 * implementations, the size field usually contains the size of
342 * the file and needs to be ignored as no data may follow this
343 * header type. For POSIX.1- 1988 compliant archives, the size
344 * field needs to be 0. For POSIX.1-2001 compliant archives,
345 * the size field may be non zero, indicating that file data is
346 * included in the archive.
347 * i.e; always assume this is zero for safety.
348 */
349 goto size0;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000350 case '7':
Denis Vlasenkob5963352006-11-26 01:46:59 +0000351 /* case 0: */
Glenn L McGrath21110a02003-01-28 01:45:48 +0000352 case '0':
Denis Vlasenkod6772502006-11-24 17:21:44 +0000353#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000354 if (last_char_is(file_header->name, '/')) {
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000355 goto set_dir;
356 }
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000357#endif
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000358 file_header->mode |= S_IFREG;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000359 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000360 case '2':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000361 file_header->mode |= S_IFLNK;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000362 /* have seen tarballs with size field containing
363 * the size of the link target's name */
364 size0:
365 file_header->size = 0;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000366 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000367 case '3':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000368 file_header->mode |= S_IFCHR;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000369 goto size0; /* paranoia */
Glenn L McGrath21110a02003-01-28 01:45:48 +0000370 case '4':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000371 file_header->mode |= S_IFBLK;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000372 goto size0;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000373 case '5':
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000374 IF_FEATURE_TAR_OLDGNU_COMPATIBILITY(set_dir:)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000375 file_header->mode |= S_IFDIR;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000376 goto size0;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000377 case '6':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000378 file_header->mode |= S_IFIFO;
Denis Vlasenkoadc772a2008-07-20 17:10:43 +0000379 goto size0;
Denys Vlasenkobdb540e2015-05-11 16:55:16 +0200380 case 'g': /* pax global header */
381 case 'x': { /* pax extended header */
382 if ((uoff_t)file_header->size > 0xfffff) /* paranoia */
383 goto skip_ext_hdr;
384 process_pax_hdr(archive_handle, file_header->size, (tar.typeflag == 'g'));
385 goto again_after_align;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000386#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
Denys Vlasenkobdb540e2015-05-11 16:55:16 +0200387/* See http://www.gnu.org/software/tar/manual/html_node/Extensions.html */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000388 case 'L':
Denis Vlasenkob5963352006-11-26 01:46:59 +0000389 /* free: paranoia: tar with several consecutive longnames */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000390 free(p_longname);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000391 /* For paranoia reasons we allocate extra NUL char */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000392 p_longname = xzalloc(file_header->size + 1);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000393 /* We read ASCIZ string, including NUL */
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000394 xread(archive_handle->src_fd, p_longname, file_header->size);
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000395 archive_handle->offset += file_header->size;
Denis Vlasenkob5963352006-11-26 01:46:59 +0000396 /* return get_header_tar(archive_handle); */
397 /* gcc 4.1.1 didn't optimize it into jump */
398 /* so we will do it ourself, this also saves stack */
399 goto again;
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000400 case 'K':
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000401 free(p_linkname);
402 p_linkname = xzalloc(file_header->size + 1);
403 xread(archive_handle->src_fd, p_linkname, file_header->size);
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000404 archive_handle->offset += file_header->size;
Denis Vlasenkob5963352006-11-26 01:46:59 +0000405 /* return get_header_tar(archive_handle); */
406 goto again;
Denys Vlasenkobdb540e2015-05-11 16:55:16 +0200407/*
408 * case 'S': // Sparse file
409 * Was seen in the wild. Not supported (yet?).
410 * See https://www.gnu.org/software/tar/manual/html_section/tar_92.html
411 * for the format. (An "Old GNU Format" was seen, not PAX formats).
412 */
413// case 'D': /* GNU dump dir */
414// case 'M': /* Continuation of multi volume archive */
415// case 'N': /* Old GNU for names > 100 characters */
416// case 'V': /* Volume header */
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000417#endif
Denys Vlasenko6111f962012-02-23 13:45:18 +0100418 }
J. Tang77a2c512010-03-19 14:48:51 +0100419 skip_ext_hdr:
J. Tang77a2c512010-03-19 14:48:51 +0100420 {
Denis Vlasenkob5963352006-11-26 01:46:59 +0000421 off_t sz;
422 bb_error_msg("warning: skipping header '%c'", tar.typeflag);
423 sz = (file_header->size + 511) & ~(off_t)511;
424 archive_handle->offset += sz;
425 sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
426 while (sz--)
427 xread(archive_handle->src_fd, &tar, 512);
428 /* return get_header_tar(archive_handle); */
429 goto again_after_align;
430 }
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000431 default:
Denis Vlasenkob5963352006-11-26 01:46:59 +0000432 bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag);
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000433 }
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000434
Denis Vlasenkod6772502006-11-24 17:21:44 +0000435#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000436 if (p_longname) {
437 file_header->name = p_longname;
438 p_longname = NULL;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000439 }
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000440 if (p_linkname) {
441 file_header->link_target = p_linkname;
442 p_linkname = NULL;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000443 }
444#endif
Denys Vlasenko5e29e262011-03-01 17:21:07 +0100445
446 /* Everything up to and including last ".." component is stripped */
Denys Vlasenkob80acf52011-03-02 01:21:02 +0100447 overlapping_strcpy(file_header->name, strip_unsafe_prefix(file_header->name));
Denys Vlasenko6c563e32015-10-22 01:07:13 +0200448//TODO: do the same for file_header->link_target?
Denis Vlasenkod6772502006-11-24 17:21:44 +0000449
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000450 /* Strip trailing '/' in directories */
Denis Vlasenko714701c2006-12-22 00:21:07 +0000451 /* Must be done after mode is set as '/' is used to check if it's a directory */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000452 cp = last_char_is(file_header->name, '/');
Glenn L McGrath3d5828f2003-08-14 02:55:15 +0000453
Glenn L McGrath8e940982002-11-04 23:47:31 +0000454 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Denis Vlasenko0381d422008-07-10 23:06:00 +0000455 archive_handle->action_header(/*archive_handle->*/ file_header);
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000456 /* Note that we kill the '/' only after action_header() */
457 /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
Denys Vlasenko02365a62010-04-09 10:52:52 +0200458 if (cp)
459 *cp = '\0';
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000460 archive_handle->action_data(archive_handle);
Denys Vlasenko440a5092012-06-22 16:27:21 +0200461 if (archive_handle->accept || archive_handle->reject
462 || (archive_handle->ah_flags & ARCHIVE_REMEMBER_NAMES)
463 ) {
Denys Vlasenkob768aeb2010-06-26 18:22:41 +0200464 llist_add_to(&archive_handle->passed, file_header->name);
Denys Vlasenko440a5092012-06-22 16:27:21 +0200465 } else /* Caller isn't interested in list of unpacked files */
Denys Vlasenkob768aeb2010-06-26 18:22:41 +0200466 free(file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000467 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000468 data_skip(archive_handle);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000469 free(file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000470 }
471 archive_handle->offset += file_header->size;
472
Denis Vlasenko75103842007-06-20 14:49:47 +0000473 free(file_header->link_target);
Denys Vlasenkob768aeb2010-06-26 18:22:41 +0200474 /* Do not free(file_header->name)!
475 * It might be inserted in archive_handle->passed - see above */
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000476#if ENABLE_FEATURE_TAR_UNAME_GNAME
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100477 free(file_header->tar__uname);
478 free(file_header->tar__gname);
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000479#endif
Denys Vlasenkoebfa9b52013-11-19 14:44:04 +0100480 return EXIT_SUCCESS; /* "decoded one header" */
Eric Andersen2276d832002-07-11 11:11:56 +0000481}