blob: b5cae9f125d61bcc098420ccb3b562d45aa246af [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
Glenn L McGrath21110a02003-01-28 01:45:48 +000017#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb7a76df2002-11-23 10:44:47 +000018static char *longname = NULL;
19static char *linkname = NULL;
20#endif
21
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000022/* NB: _DESTROYS_ str[len] character! */
23static unsigned long long getOctal(char *str, int len)
24{
25 unsigned long long v;
26 /* Actually, tar header allows leading spaces also.
27 * Oh well, we will be liberal and skip this...
28 * The only downside probably is that we allow "-123" too :)
29 if (*str < '0' || *str > '7')
30 bb_error_msg_and_die("corrupted octal value in tar header");
31 */
32 str[len] = '\0';
33 v = strtoull(str, &str, 8);
34 if (*str)
35 bb_error_msg_and_die("corrupted octal value in tar header");
36 return v;
37}
38
39void BUG_tar_header_size(void);
Rob Landleydfba7412006-03-06 20:47:33 +000040char get_header_tar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +000041{
Paul Fox94ff9f12005-07-20 19:24:13 +000042 static int end = 0;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000043
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000044 file_header_t *file_header = archive_handle->file_header;
45 struct {
46 /* ustar header, Posix 1003.1 */
47 char name[100]; /* 0-99 */
48 char mode[8]; /* 100-107 */
49 char uid[8]; /* 108-115 */
50 char gid[8]; /* 116-123 */
51 char size[12]; /* 124-135 */
52 char mtime[12]; /* 136-147 */
53 char chksum[8]; /* 148-155 */
54 char typeflag; /* 156-156 */
55 char linkname[100]; /* 157-256 */
56 char magic[6]; /* 257-262 */
57 char version[2]; /* 263-264 */
58 char uname[32]; /* 265-296 */
59 char gname[32]; /* 297-328 */
60 char devmajor[8]; /* 329-336 */
61 char devminor[8]; /* 337-344 */
62 char prefix[155]; /* 345-499 */
63 char padding[12]; /* 500-512 */
64 } tar;
65 char *cp;
66 int sum, i;
67
68 if (sizeof(tar) != 512)
69 BUG_tar_header_size();
70
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000071 /* Align header */
Glenn L McGrath237ae422002-11-03 14:05:15 +000072 data_align(archive_handle, 512);
Glenn L McGrath95ebf612001-10-25 14:18:08 +000073
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000074 xread(archive_handle->src_fd, &tar, 512);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000075 archive_handle->offset += 512;
76
77 /* If there is no filename its an empty header */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000078 if (tar.name[0] == 0) {
Paul Fox94ff9f12005-07-20 19:24:13 +000079 if (end) {
80 /* This is the second consecutive empty header! End of archive!
81 * Read until the end to empty the pipe from gz or bz2
82 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000083 while (full_read(archive_handle->src_fd, &tar, 512) == 512)
84 /* repeat */;
Denis Vlasenko13858992006-10-08 12:49:22 +000085 return EXIT_FAILURE;
Paul Fox94ff9f12005-07-20 19:24:13 +000086 }
87 end = 1;
Denis Vlasenko13858992006-10-08 12:49:22 +000088 return EXIT_SUCCESS;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000089 }
Paul Fox94ff9f12005-07-20 19:24:13 +000090 end = 0;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000091
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +000092 /* Check header has valid magic, "ustar" is for the proper tar
93 * 0's are for the old tar format
Glenn L McGrath95ebf612001-10-25 14:18:08 +000094 */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000095 if (strncmp(tar.magic, "ustar", 5) != 0) {
Bernhard Reutner-Fischera1bccc02006-04-02 20:17:55 +000096#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
Denis Vlasenko376ce1e2006-11-24 14:51:01 +000097 if (memcmp(tar.magic, "\0\0\0\0", 5) != 0)
Glenn L McGrath1d23f3a2002-08-13 05:06:43 +000098#endif
Denis Vlasenko13858992006-10-08 12:49:22 +000099 bb_error_msg_and_die("invalid tar magic");
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000100 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000101 /* Do checksum on headers */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000102 sum = ' ' * sizeof(tar.chksum);
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000103 for (i = 0; i < 148 ; i++) {
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000104 sum += ((char*)&tar)[i];
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000105 }
Denis Vlasenko13858992006-10-08 12:49:22 +0000106 for (i = 156; i < 512 ; i++) {
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000107 sum += ((char*)&tar)[i];
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000108 }
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000109 /* This field does not need special treatment (getOctal) */
110 if (sum != xstrtoul(tar.chksum, 8)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000111 bb_error_msg_and_die("invalid tar header checksum");
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000112 }
113
Glenn L McGrath21110a02003-01-28 01:45:48 +0000114#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Glenn L McGrathb7a76df2002-11-23 10:44:47 +0000115 if (longname) {
116 file_header->name = longname;
117 longname = NULL;
118 }
119 else if (linkname) {
120 file_header->name = linkname;
121 linkname = NULL;
122 } else
123#endif
Rob Landley06249fe2006-02-20 19:28:53 +0000124 {
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000125 file_header->name = xstrndup(tar.name, sizeof(tar.name));
126 if (tar.prefix[0]) {
Rob Landley06249fe2006-02-20 19:28:53 +0000127 char *temp = file_header->name;
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000128 file_header->name = concat_path_file(tar.prefix, temp);
Rob Landley06249fe2006-02-20 19:28:53 +0000129 free(temp);
130 }
Glenn L McGrath75762702002-08-22 11:50:31 +0000131 }
Glenn L McGrathc5c1a8a2002-10-19 06:19:22 +0000132
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000133 /* getOctal trashes subsequent field, therefore we call it
134 * on fields in reverse order */
135#define GET_OCTAL(a) getOctal((a), sizeof(a))
136 if (tar.devmajor[0]) {
137 unsigned minor = GET_OCTAL(tar.devminor);
138 unsigned major = GET_OCTAL(tar.devmajor);
139 file_header->device = makedev(major, minor);
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000140 }
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000141 file_header->mtime = GET_OCTAL(tar.mtime);
142 file_header->size = GET_OCTAL(tar.size);
143 file_header->gid = GET_OCTAL(tar.gid);
144 file_header->uid = GET_OCTAL(tar.uid);
145 file_header->link_name = !tar.linkname[0] ? NULL :
146 xstrndup(tar.linkname, sizeof(tar.linkname));
Glenn L McGrath916ba532004-02-20 02:34:42 +0000147 /* Set bits 0-11 of the files mode */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000148 file_header->mode = 07777 & GET_OCTAL(tar.mode);
149#undef GET_OCTAL
Glenn L McGrath916ba532004-02-20 02:34:42 +0000150
151 /* Set bits 12-15 of the files mode */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000152 switch (tar.typeflag) {
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000153 /* busybox identifies hard links as being regular files with 0 size and a link name */
154 case '1':
Glenn L McGrath916ba532004-02-20 02:34:42 +0000155 file_header->mode |= S_IFREG;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000156 break;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000157 case '7':
158 /* Reserved for high performance files, treat as normal file */
Glenn L McGrath99b12542002-08-22 17:47:09 +0000159 case 0:
Glenn L McGrath21110a02003-01-28 01:45:48 +0000160 case '0':
Bernhard Reutner-Fischera1bccc02006-04-02 20:17:55 +0000161#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATIBILITY
Glenn L McGrath87af49f2003-09-09 17:41:03 +0000162 if (last_char_is(file_header->name, '/')) {
163 file_header->mode |= S_IFDIR;
Glenn L McGrathc9f1fce2004-02-20 02:25:18 +0000164 } else
165#endif
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000166 file_header->mode |= S_IFREG;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000167 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000168 case '2':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000169 file_header->mode |= S_IFLNK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000170 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000171 case '3':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000172 file_header->mode |= S_IFCHR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000173 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000174 case '4':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000175 file_header->mode |= S_IFBLK;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000176 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000177 case '5':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000178 file_header->mode |= S_IFDIR;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000179 break;
Glenn L McGrath21110a02003-01-28 01:45:48 +0000180 case '6':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000181 file_header->mode |= S_IFIFO;
Glenn L McGrath99b12542002-08-22 17:47:09 +0000182 break;
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000183#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000184 case 'L':
185 longname = xzalloc(file_header->size + 1);
186 xread(archive_handle->src_fd, longname, file_header->size);
187 archive_handle->offset += file_header->size;
188 return get_header_tar(archive_handle);
189 case 'K':
190 linkname = xzalloc(file_header->size + 1);
191 xread(archive_handle->src_fd, linkname, file_header->size);
192 archive_handle->offset += file_header->size;
193 file_header->name = linkname;
194 return get_header_tar(archive_handle);
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000195 case 'D': /* GNU dump dir */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000196 case 'M': /* Continuation of multi volume archive */
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000197 case 'N': /* Old GNU for names > 100 characters */
198 case 'S': /* Sparse file */
199 case 'V': /* Volume header */
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000200#endif
Rob Landleyff6e21c2006-07-06 20:30:19 +0000201 case 'g': /* pax global header */
202 case 'x': /* pax extended header */
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000203 bb_error_msg("ignoring extension type %c", tar.typeflag);
Rob Landleyff6e21c2006-07-06 20:30:19 +0000204 break;
Glenn L McGrathb0e163a2004-02-19 08:48:30 +0000205 default:
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000206 bb_error_msg("unknown typeflag: 0x%x", tar.typeflag);
Glenn L McGrath6aa52232004-02-17 11:55:06 +0000207 }
Denis Vlasenko376ce1e2006-11-24 14:51:01 +0000208
209 /* Strip trailing '/' in directories */
210 /* Must be done after mode is set as '/' is used to check if its a directory */
211 cp = last_char_is(file_header->name, '/');
212 if (cp) *cp = '\0';
Glenn L McGrath3d5828f2003-08-14 02:55:15 +0000213
Glenn L McGrath8e940982002-11-04 23:47:31 +0000214 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000215 archive_handle->action_header(archive_handle->file_header);
216 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
217 archive_handle->action_data(archive_handle);
Rob Landley8bb50782006-05-26 23:44:51 +0000218 llist_add_to(&(archive_handle->passed), file_header->name);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000219 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000220 data_skip(archive_handle);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000221 }
222 archive_handle->offset += file_header->size;
223
Glenn L McGrath54ac0572003-11-15 00:24:43 +0000224 free(file_header->link_name);
225
Denis Vlasenko13858992006-10-08 12:49:22 +0000226 return EXIT_SUCCESS;
Eric Andersen2276d832002-07-11 11:11:56 +0000227}