1
2
3
4
5
6
7
8
9
10
11
12
13
14#include "libbb.h"
15#include "unarchive.h"
16
17
18
19
20
21
22
23
24
25
26
27
28static off_t getBase256_len12(const char *str)
29{
30 off_t value;
31 int len;
32
33
34
35 if (sizeof(off_t) >= 12) {
36
37 len = 12;
38 value = *str++ & 0x3f;
39 while (--len)
40 value = (value << 8) + (unsigned char) *str++;
41 return value;
42 }
43
44#ifdef CHECK_FOR_OVERFLOW
45
46 char c = *str++ & 0x3f;
47 len = 12;
48 while (1) {
49 if (c)
50 bb_error_msg_and_die("overflow in base-256 encoded file size");
51 if (--len == sizeof(off_t))
52 break;
53 c = *str++;
54 }
55#else
56 str += (12 - sizeof(off_t));
57#endif
58
59
60
61
62
63
64
65
66
67
68
69
70 if (sizeof(off_t) == 8) {
71 value = *(off_t*)str;
72 value = SWAP_BE64(value);
73 } else if (sizeof(off_t) == 4) {
74 value = *(off_t*)str;
75 value = SWAP_BE32(value);
76 } else {
77 value = 0;
78 len = sizeof(off_t);
79 while (--len)
80 value = (value << 8) + (unsigned char) *str++;
81 }
82 return value;
83}
84
85
86static unsigned long long getOctal(char *str, int len)
87{
88 unsigned long long v;
89
90
91
92 str[len] = '\0';
93 v = strtoull(str, &str, 8);
94
95
96 if (*str != '\0' && *str != ' ')
97 bb_error_msg_and_die("corrupted octal value in tar header");
98 return v;
99}
100#define GET_OCTAL(a) getOctal((a), sizeof(a))
101
102void BUG_tar_header_size(void);
103char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
104{
105 file_header_t *file_header = archive_handle->file_header;
106 struct {
107
108 char name[100];
109 char mode[8];
110 char uid[8];
111 char gid[8];
112 char size[12];
113 char mtime[12];
114 char chksum[8];
115 char typeflag;
116 char linkname[100];
117
118
119
120
121
122 char magic[8];
123 char uname[32];
124 char gname[32];
125 char devmajor[8];
126 char devminor[8];
127 char prefix[155];
128 char padding[12];
129 } tar;
130 char *cp;
131 int i, sum_u, sum;
132#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
133 int sum_s;
134#endif
135 int parse_names;
136
137
138#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
139# define p_longname (archive_handle->tar__longname)
140# define p_linkname (archive_handle->tar__linkname)
141#else
142# define p_longname 0
143# define p_linkname 0
144#endif
145
146 if (sizeof(tar) != 512)
147 BUG_tar_header_size();
148
149#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
150 again:
151#endif
152
153 data_align(archive_handle, 512);
154
155 again_after_align:
156
157#if ENABLE_DESKTOP || ENABLE_FEATURE_TAR_AUTODETECT
158
159 *(uint32_t*)(&tar) = 0;
160 i = full_read(archive_handle->src_fd, &tar, 512);
161
162
163
164
165
166
167
168 if (i == 0) {
169 xfunc_error_retval = 0;
170 short_read:
171 bb_error_msg_and_die("short read");
172 }
173 if (i != 512) {
174 IF_FEATURE_TAR_AUTODETECT(goto autodetect;)
175 goto short_read;
176 }
177
178#else
179 i = 512;
180 xread(archive_handle->src_fd, &tar, i);
181#endif
182 archive_handle->offset += i;
183
184
185 if (tar.name[0] == 0 && tar.prefix[0] == 0) {
186 if (archive_handle->tar__end) {
187
188
189
190 while (full_read(archive_handle->src_fd, &tar, 512) == 512)
191 continue;
192 return EXIT_FAILURE;
193 }
194 archive_handle->tar__end = 1;
195 return EXIT_SUCCESS;
196 }
197 archive_handle->tar__end = 0;
198
199
200
201 if (strncmp(tar.magic, "ustar", 5) != 0
202 && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
203 || memcmp(tar.magic, "\0\0\0\0", 5) != 0)
204 ) {
205#if ENABLE_FEATURE_TAR_AUTODETECT
206 char FAST_FUNC (*get_header_ptr)(archive_handle_t *);
207
208 autodetect:
209
210
211
212
213
214#if ENABLE_FEATURE_SEAMLESS_GZ
215 if (tar.name[0] == 0x1f && tar.name[1] == (char)0x8b) {
216 get_header_ptr = get_header_tar_gz;
217 } else
218#endif
219#if ENABLE_FEATURE_SEAMLESS_BZ2
220 if (tar.name[0] == 'B' && tar.name[1] == 'Z'
221 && tar.name[2] == 'h' && isdigit(tar.name[3])
222 ) {
223 get_header_ptr = get_header_tar_bz2;
224 } else
225#endif
226 goto err;
227
228
229
230 if (lseek(archive_handle->src_fd, -i, SEEK_CUR) != 0)
231 goto err;
232 while (get_header_ptr(archive_handle) == EXIT_SUCCESS)
233 continue;
234 return EXIT_FAILURE;
235 err:
236#endif
237 bb_error_msg_and_die("invalid tar magic");
238 }
239
240
241
242
243
244#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
245 sum_s = ' ' * sizeof(tar.chksum);
246#endif
247 sum_u = ' ' * sizeof(tar.chksum);
248 for (i = 0; i < 148; i++) {
249 sum_u += ((unsigned char*)&tar)[i];
250#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
251 sum_s += ((signed char*)&tar)[i];
252#endif
253 }
254 for (i = 156; i < 512; i++) {
255 sum_u += ((unsigned char*)&tar)[i];
256#if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
257 sum_s += ((signed char*)&tar)[i];
258#endif
259 }
260
261 {
262 char *endp;
263 sum = strtoul(tar.chksum, &endp, 8);
264 if ((*endp != '\0' && *endp != ' ')
265 || (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum))
266 ) {
267 bb_error_msg_and_die("invalid tar header checksum");
268 }
269 }
270
271 sum = strtoul(tar.chksum, NULL, 8);
272 if (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) {
273 bb_error_msg_and_die("invalid tar header checksum");
274 }
275
276
277 if (!tar.typeflag) tar.typeflag = '0';
278 parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
279
280
281
282 if (tar.devmajor[0]) {
283 char t = tar.prefix[0];
284
285 unsigned minor = GET_OCTAL(tar.devminor);
286 unsigned major = GET_OCTAL(tar.devmajor);
287 file_header->device = makedev(major, minor);
288 tar.prefix[0] = t;
289 }
290 file_header->link_target = NULL;
291 if (!p_linkname && parse_names && tar.linkname[0]) {
292 file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname));
293
294
295 }
296#if ENABLE_FEATURE_TAR_UNAME_GNAME
297 file_header->tar__uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL;
298 file_header->tar__gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL;
299#endif
300
301
302 file_header->mtime = (tar.mtime[0] & 0x80)
303 ? 0
304 : GET_OCTAL(tar.mtime);
305
306 file_header->size = (tar.size[0] & 0xc0) == 0x80
307 ? getBase256_len12(tar.size)
308 : GET_OCTAL(tar.size);
309 file_header->gid = GET_OCTAL(tar.gid);
310 file_header->uid = GET_OCTAL(tar.uid);
311
312 file_header->mode = 07777 & GET_OCTAL(tar.mode);
313
314 file_header->name = NULL;
315 if (!p_longname && parse_names) {
316
317
318 tar.mode[0] = '\0';
319 if (tar.prefix[0]) {
320
321
322 tar.padding[0] = '\0';
323 file_header->name = concat_path_file(tar.prefix, tar.name);
324 } else
325 file_header->name = xstrdup(tar.name);
326 }
327
328
329
330 switch (tar.typeflag) {
331
332 case '1':
333 file_header->mode |= S_IFREG;
334 break;
335 case '7':
336
337 case '0':
338#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
339 if (last_char_is(file_header->name, '/')) {
340 goto set_dir;
341 }
342#endif
343 file_header->mode |= S_IFREG;
344 break;
345 case '2':
346 file_header->mode |= S_IFLNK;
347
348
349 size0:
350 file_header->size = 0;
351 break;
352 case '3':
353 file_header->mode |= S_IFCHR;
354 goto size0;
355 case '4':
356 file_header->mode |= S_IFBLK;
357 goto size0;
358 case '5':
359 IF_FEATURE_TAR_OLDGNU_COMPATIBILITY(set_dir:)
360 file_header->mode |= S_IFDIR;
361 goto size0;
362 case '6':
363 file_header->mode |= S_IFIFO;
364 goto size0;
365#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
366 case 'L':
367
368 free(p_longname);
369
370 p_longname = xzalloc(file_header->size + 1);
371
372 xread(archive_handle->src_fd, p_longname, file_header->size);
373 archive_handle->offset += file_header->size;
374
375
376
377 goto again;
378 case 'K':
379 free(p_linkname);
380 p_linkname = xzalloc(file_header->size + 1);
381 xread(archive_handle->src_fd, p_linkname, file_header->size);
382 archive_handle->offset += file_header->size;
383
384 goto again;
385 case 'D':
386 case 'M':
387 case 'N':
388 case 'S':
389 case 'V':
390#endif
391 case 'g':
392 case 'x': {
393 off_t sz;
394 bb_error_msg("warning: skipping header '%c'", tar.typeflag);
395 sz = (file_header->size + 511) & ~(off_t)511;
396 archive_handle->offset += sz;
397 sz >>= 9;
398 while (sz--)
399 xread(archive_handle->src_fd, &tar, 512);
400
401 goto again_after_align;
402 }
403 default:
404 bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag);
405 }
406
407#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
408 if (p_longname) {
409 file_header->name = p_longname;
410 p_longname = NULL;
411 }
412 if (p_linkname) {
413 file_header->link_target = p_linkname;
414 p_linkname = NULL;
415 }
416#endif
417 if (strncmp(file_header->name, "/../"+1, 3) == 0
418 || strstr(file_header->name, "/../")
419 ) {
420 bb_error_msg_and_die("name with '..' encountered: '%s'",
421 file_header->name);
422 }
423
424
425
426 cp = last_char_is(file_header->name, '/');
427
428 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
429 archive_handle->action_header( file_header);
430
431
432 if (cp) *cp = '\0';
433 archive_handle->ah_flags |= ARCHIVE_EXTRACT_QUIET;
434 archive_handle->action_data(archive_handle);
435 llist_add_to(&(archive_handle->passed), file_header->name);
436 } else {
437 data_skip(archive_handle);
438 free(file_header->name);
439 }
440 archive_handle->offset += file_header->size;
441
442 free(file_header->link_target);
443
444#if ENABLE_FEATURE_TAR_UNAME_GNAME
445 free(file_header->tar__uname);
446 free(file_header->tar__gname);
447#endif
448 return EXIT_SUCCESS;
449}
450