blob: 274bd46077b51fc9f77163a4ed2809d89b158d47 [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
Denis Vlasenkod6772502006-11-24 17:21:44 +000017#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
18static char *longname;
19static char *linkname;
20#else
21enum {
22 longname = 0,
23 linkname = 0,
24};
Glenn L McGrathb7a76df2002-11-23 10:44:47 +000025#endif
26
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000027/* NB: _DESTROYS_ str[len] character! */
28static unsigned long long getOctal(char *str, int len)
29{
30 unsigned long long v;
31 /* Actually, tar header allows leading spaces also.
32 * Oh well, we will be liberal and skip this...
33 * The only downside probably is that we allow "-123" too :)
34 if (*str < '0' || *str > '7')
35 bb_error_msg_and_die("corrupted octal value in tar header");
36 */
37 str[len] = '\0';
38 v = strtoull(str, &str, 8);
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +000039 if (*str && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY || *str != ' '))
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000040 bb_error_msg_and_die("corrupted octal value in tar header");
41 return v;
42}
Denis Vlasenkob5963352006-11-26 01:46:59 +000043#define GET_OCTAL(a) getOctal((a), sizeof(a))
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000044
45void BUG_tar_header_size(void);
Rob Landleydfba7412006-03-06 20:47:33 +000046char get_header_tar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +000047{
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +000048 static smallint end;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000049
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000050 file_header_t *file_header = archive_handle->file_header;
51 struct {
52 /* ustar header, Posix 1003.1 */
53 char name[100]; /* 0-99 */
54 char mode[8]; /* 100-107 */
55 char uid[8]; /* 108-115 */
56 char gid[8]; /* 116-123 */
57 char size[12]; /* 124-135 */
58 char mtime[12]; /* 136-147 */
59 char chksum[8]; /* 148-155 */
60 char typeflag; /* 156-156 */
61 char linkname[100]; /* 157-256 */
Denis Vlasenko85128622007-11-16 20:35:30 +000062 /* POSIX: "ustar" NUL "00" */
63 /* GNU tar: "ustar " NUL */
Paul Fox16aec392007-11-17 19:11:05 +000064 /* Normally it's defined as magic[6] followed by
65 * version[2], but we put them together to save code.
66 */
Denis Vlasenko85128622007-11-16 20:35:30 +000067 char magic[8]; /* 257-264 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000068 char uname[32]; /* 265-296 */
69 char gname[32]; /* 297-328 */
70 char devmajor[8]; /* 329-336 */
71 char devminor[8]; /* 337-344 */
72 char prefix[155]; /* 345-499 */
73 char padding[12]; /* 500-512 */
74 } tar;
75 char *cp;
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +000076 int i, sum_u, sum;
77#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
78 int sum_s;
79#endif
Denis Vlasenkod6772502006-11-24 17:21:44 +000080 int parse_names;
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000081
82 if (sizeof(tar) != 512)
83 BUG_tar_header_size();
84
Denis Vlasenko666da5e2006-12-26 18:17:42 +000085#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
86 again:
87#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000088 /* Align header */
Glenn L McGrath237ae422002-11-03 14:05:15 +000089 data_align(archive_handle, 512);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000090
Denis Vlasenkob5963352006-11-26 01:46:59 +000091 again_after_align:
92
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000093 xread(archive_handle->src_fd, &tar, 512);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000094 archive_handle->offset += 512;
95
96 /* If there is no filename its an empty header */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000097 if (tar.name[0] == 0) {
Paul Fox94ff9f12005-07-20 19:24:13 +000098 if (end) {
99 /* This is the second consecutive empty header! End of archive!
100 * Read until the end to empty the pipe from gz or bz2
101 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000102 while (full_read(archive_handle->src_fd, &tar, 512) == 512)
103 /* repeat */;
Denis Vlasenko13858992006-10-08 12:49:22 +0000104 return EXIT_FAILURE;
Paul Fox94ff9f12005-07-20 19:24:13 +0000105 }
106 end = 1;
Denis Vlasenko13858992006-10-08 12:49:22 +0000107 return EXIT_SUCCESS;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000108 }
Paul Fox94ff9f12005-07-20 19:24:13 +0000109 end = 0;
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000110
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000111 /* Check header has valid magic, "ustar" is for the proper tar
112 * 0's are for the old tar format
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000113 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000114 if (strncmp(tar.magic, "ustar", 5) != 0) {
Denis Vlasenkod6772502006-11-24 17:21:44 +0000115#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000116 if (memcmp(tar.magic, "\0\0\0\0", 5) != 0)
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +0000117#endif
Denis Vlasenko13858992006-10-08 12:49:22 +0000118 bb_error_msg_and_die("invalid tar magic");
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000119 }
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000120
121 /* Do checksum on headers.
122 * POSIX says that checksum is done on unsigned bytes, but
Denis Vlasenko940494f2007-03-04 18:09:50 +0000123 * Sun and HP-UX gets it wrong... more details in
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000124 * GNU tar source. */
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000125#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
126 sum_s = ' ' * sizeof(tar.chksum);
127#endif
128 sum_u = ' ' * sizeof(tar.chksum);
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000129 for (i = 0; i < 148; i++) {
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000130 sum_u += ((unsigned char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000131#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000132 sum_s += ((signed char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000133#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000134 }
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000135 for (i = 156; i < 512; i++) {
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000136 sum_u += ((unsigned char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000137#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
Denis Vlasenkodcbd51d2007-03-03 20:06:59 +0000138 sum_s += ((signed char*)&tar)[i];
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000139#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000140 }
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000141#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
142 sum = strtoul(tar.chksum, &cp, 8);
143 if ((*cp && *cp != ' ')
144 || (sum_u != sum USE_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum))
145 ) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000146 bb_error_msg_and_die("invalid tar header checksum");
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000147 }
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +0000148#else
149 /* This field does not need special treatment (getOctal) */
150 sum = xstrtoul(tar.chksum, 8);
151 if (sum_u != sum USE_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) {
152 bb_error_msg_and_die("invalid tar header checksum");
153 }
154#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000155
Denis Vlasenkob5963352006-11-26 01:46:59 +0000156 /* 0 is reserved for high perf file, treat as normal file */
157 if (!tar.typeflag) tar.typeflag = '0';
158 parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
Denis Vlasenkod6772502006-11-24 17:21:44 +0000159
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000160 /* getOctal trashes subsequent field, therefore we call it
161 * on fields in reverse order */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000162 if (tar.devmajor[0]) {
163 unsigned minor = GET_OCTAL(tar.devminor);
164 unsigned major = GET_OCTAL(tar.devmajor);
165 file_header->device = makedev(major, minor);
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000166 }
Denis Vlasenko75103842007-06-20 14:49:47 +0000167 file_header->link_target = NULL;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000168 if (!linkname && parse_names && tar.linkname[0]) {
169 /* we trash magic[0] here, it's ok */
170 tar.linkname[sizeof(tar.linkname)] = '\0';
Denis Vlasenko75103842007-06-20 14:49:47 +0000171 file_header->link_target = xstrdup(tar.linkname);
172 /* FIXME: what if we have non-link object with link_target? */
173 /* Will link_target be free()ed? */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000174 }
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000175 file_header->mtime = GET_OCTAL(tar.mtime);
176 file_header->size = GET_OCTAL(tar.size);
177 file_header->gid = GET_OCTAL(tar.gid);
178 file_header->uid = GET_OCTAL(tar.uid);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000179 /* Set bits 0-11 of the files mode */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000180 file_header->mode = 07777 & GET_OCTAL(tar.mode);
Glenn L McGrath916ba532004-02-20 02:34:42 +0000181
Denis Vlasenkob5963352006-11-26 01:46:59 +0000182 file_header->name = NULL;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000183 if (!longname && parse_names) {
184 /* we trash mode[0] here, it's ok */
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000185 tar.name[sizeof(tar.name)] = '\0';
Denis Vlasenko87cd4a82006-11-25 23:47:32 +0000186 if (tar.prefix[0]) {
187 /* and padding[0] */
188 tar.prefix[sizeof(tar.prefix)] = '\0';
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000189 file_header->name = concat_path_file(tar.prefix, tar.name);
Denis Vlasenko87cd4a82006-11-25 23:47:32 +0000190 } else
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000191 file_header->name = xstrdup(tar.name);
192 }
193
Glenn L McGrath916ba532004-02-20 02:34:42 +0000194 /* Set bits 12-15 of the files mode */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000195 /* (typeflag was not trashed because chksum does not use getOctal) */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000196 switch (tar.typeflag) {
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000197 /* busybox identifies hard links as being regular files with 0 size and a link name */
198 case '1':
Glenn L McGrath916ba532004-02-20 02:34:42 +0000199 file_header->mode |= S_IFREG;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000200 break;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000201 case '7':
Denis Vlasenkob5963352006-11-26 01:46:59 +0000202 /* case 0: */
Glenn L McGrath21110a02003-01-28 01:45:48 +0000203 case '0':
Denis Vlasenkod6772502006-11-24 17:21:44 +0000204#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000205 if (last_char_is(file_header->name, '/')) {
206 file_header->mode |= S_IFDIR;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000207 } else
208#endif
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000209 file_header->mode |= S_IFREG;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000210 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000211 case '2':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000212 file_header->mode |= S_IFLNK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000213 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000214 case '3':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000215 file_header->mode |= S_IFCHR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000216 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000217 case '4':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000218 file_header->mode |= S_IFBLK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000219 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000220 case '5':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000221 file_header->mode |= S_IFDIR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000222 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000223 case '6':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000224 file_header->mode |= S_IFIFO;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000225 break;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000226#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000227 case 'L':
Denis Vlasenkob5963352006-11-26 01:46:59 +0000228 /* free: paranoia: tar with several consecutive longnames */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000229 free(longname);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000230 /* For paranoia reasons we allocate extra NUL char */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000231 longname = xzalloc(file_header->size + 1);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000232 /* We read ASCIZ string, including NUL */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000233 xread(archive_handle->src_fd, longname, file_header->size);
234 archive_handle->offset += file_header->size;
Denis Vlasenkob5963352006-11-26 01:46:59 +0000235 /* return get_header_tar(archive_handle); */
236 /* gcc 4.1.1 didn't optimize it into jump */
237 /* so we will do it ourself, this also saves stack */
238 goto again;
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000239 case 'K':
Denis Vlasenkod6772502006-11-24 17:21:44 +0000240 free(linkname);
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000241 linkname = xzalloc(file_header->size + 1);
242 xread(archive_handle->src_fd, linkname, file_header->size);
243 archive_handle->offset += file_header->size;
Denis Vlasenkob5963352006-11-26 01:46:59 +0000244 /* return get_header_tar(archive_handle); */
245 goto again;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000246 case 'D': /* GNU dump dir */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000247 case 'M': /* Continuation of multi volume archive */
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000248 case 'N': /* Old GNU for names > 100 characters */
249 case 'S': /* Sparse file */
250 case 'V': /* Volume header */
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000251#endif
Rob Landleyff6e21c2006-07-06 20:30:19 +0000252 case 'g': /* pax global header */
Denis Vlasenkob5963352006-11-26 01:46:59 +0000253 case 'x': { /* pax extended header */
254 off_t sz;
255 bb_error_msg("warning: skipping header '%c'", tar.typeflag);
256 sz = (file_header->size + 511) & ~(off_t)511;
257 archive_handle->offset += sz;
258 sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
259 while (sz--)
260 xread(archive_handle->src_fd, &tar, 512);
261 /* return get_header_tar(archive_handle); */
262 goto again_after_align;
263 }
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000264 default:
Denis Vlasenkob5963352006-11-26 01:46:59 +0000265 bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag);
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000266 }
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000267
Denis Vlasenkod6772502006-11-24 17:21:44 +0000268#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
269 if (longname) {
270 file_header->name = longname;
271 longname = NULL;
272 }
273 if (linkname) {
Denis Vlasenko75103842007-06-20 14:49:47 +0000274 file_header->link_target = linkname;
Denis Vlasenkod6772502006-11-24 17:21:44 +0000275 linkname = NULL;
276 }
277#endif
Denis Vlasenkoc1660fe2006-11-26 15:42:03 +0000278 if (!strncmp(file_header->name, "/../"+1, 3)
279 || strstr(file_header->name, "/../")
280 ) {
281 bb_error_msg_and_die("name with '..' encountered: '%s'",
282 file_header->name);
283 }
Denis Vlasenkod6772502006-11-24 17:21:44 +0000284
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000285 /* Strip trailing '/' in directories */
Denis Vlasenko714701c2006-12-22 00:21:07 +0000286 /* Must be done after mode is set as '/' is used to check if it's a directory */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000287 cp = last_char_is(file_header->name, '/');
Glenn L McGrath3d5828f2003-08-14 02:55:15 +0000288
Glenn L McGrath8e940982002-11-04 23:47:31 +0000289 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000290 archive_handle->action_header(archive_handle->file_header);
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000291 /* Note that we kill the '/' only after action_header() */
292 /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
293 if (cp) *cp = '\0';
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000294 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
295 archive_handle->action_data(archive_handle);
Rob Landley8bb50782006-05-26 23:44:51 +0000296 llist_add_to(&(archive_handle->passed), file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000297 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000298 data_skip(archive_handle);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000299 free(file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000300 }
301 archive_handle->offset += file_header->size;
302
Denis Vlasenko75103842007-06-20 14:49:47 +0000303 free(file_header->link_target);
Denis Vlasenkob5963352006-11-26 01:46:59 +0000304 /* Do not free(file_header->name)! */
Glenn L McGrath54ac0572003-11-15 00:24:43 +0000305
Denis Vlasenko13858992006-10-08 12:49:22 +0000306 return EXIT_SUCCESS;
Eric Andersen2276d832002-07-11 11:11:56 +0000307}