1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2011-2012 Michihiro NAKAJIMA
4 * Copyright (c) 2016 Martin Matuska
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "archive_platform.h"
29 __FBSDID("$FreeBSD: stable/12/contrib/libarchive/libarchive/archive_read_support_format_tar.c 372820 2022-12-28 22:12:33Z git2svn $");
30
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #include <stddef.h>
35 #ifdef HAVE_STDLIB_H
36 #include <stdlib.h>
37 #endif
38 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #endif
41
42 #include "archive.h"
43 #include "archive_acl_private.h" /* For ACL parsing routines. */
44 #include "archive_entry.h"
45 #include "archive_entry_locale.h"
46 #include "archive_private.h"
47 #include "archive_read_private.h"
48
49 #define tar_min(a,b) ((a) < (b) ? (a) : (b))
50
51 /*
52 * Layout of POSIX 'ustar' tar header.
53 */
54 struct archive_entry_header_ustar {
55 char name[100];
56 char mode[8];
57 char uid[8];
58 char gid[8];
59 char size[12];
60 char mtime[12];
61 char checksum[8];
62 char typeflag[1];
63 char linkname[100]; /* "old format" header ends here */
64 char magic[6]; /* For POSIX: "ustar\0" */
65 char version[2]; /* For POSIX: "00" */
66 char uname[32];
67 char gname[32];
68 char rdevmajor[8];
69 char rdevminor[8];
70 char prefix[155];
71 };
72
73 /*
74 * Structure of GNU tar header
75 */
76 struct gnu_sparse {
77 char offset[12];
78 char numbytes[12];
79 };
80
81 struct archive_entry_header_gnutar {
82 char name[100];
83 char mode[8];
84 char uid[8];
85 char gid[8];
86 char size[12];
87 char mtime[12];
88 char checksum[8];
89 char typeflag[1];
90 char linkname[100];
91 char magic[8]; /* "ustar \0" (note blank/blank/null at end) */
92 char uname[32];
93 char gname[32];
94 char rdevmajor[8];
95 char rdevminor[8];
96 char atime[12];
97 char ctime[12];
98 char offset[12];
99 char longnames[4];
100 char unused[1];
101 struct gnu_sparse sparse[4];
102 char isextended[1];
103 char realsize[12];
104 /*
105 * Old GNU format doesn't use POSIX 'prefix' field; they use
106 * the 'L' (longname) entry instead.
107 */
108 };
109
110 /*
111 * Data specific to this format.
112 */
113 struct sparse_block {
114 struct sparse_block *next;
115 int64_t offset;
116 int64_t remaining;
117 int hole;
118 };
119
120 struct tar {
121 struct archive_string acl_text;
122 struct archive_string entry_pathname;
123 /* For "GNU.sparse.name" and other similar path extensions. */
124 struct archive_string entry_pathname_override;
125 struct archive_string entry_linkpath;
126 struct archive_string entry_uname;
127 struct archive_string entry_gname;
128 struct archive_string longlink;
129 struct archive_string longname;
130 struct archive_string pax_header;
131 struct archive_string pax_global;
132 struct archive_string line;
133 int pax_hdrcharset_binary;
134 int header_recursion_depth;
135 int64_t entry_bytes_remaining;
136 int64_t entry_offset;
137 int64_t entry_padding;
138 int64_t entry_bytes_unconsumed;
139 int64_t realsize;
140 int sparse_allowed;
141 struct sparse_block *sparse_list;
142 struct sparse_block *sparse_last;
143 int64_t sparse_offset;
144 int64_t sparse_numbytes;
145 int sparse_gnu_major;
146 int sparse_gnu_minor;
147 char sparse_gnu_pending;
148
149 struct archive_string localname;
150 struct archive_string_conv *opt_sconv;
151 struct archive_string_conv *sconv;
152 struct archive_string_conv *sconv_acl;
153 struct archive_string_conv *sconv_default;
154 int init_default_conversion;
155 int compat_2x;
156 int process_mac_extensions;
157 int read_concatenated_archives;
158 int realsize_override;
159 };
160
161 static int archive_block_is_null(const char *p);
162 static char *base64_decode(const char *, size_t, size_t *);
163 static int gnu_add_sparse_entry(struct archive_read *, struct tar *,
164 int64_t offset, int64_t remaining);
165
166 static void gnu_clear_sparse_list(struct tar *);
167 static int gnu_sparse_old_read(struct archive_read *, struct tar *,
168 const struct archive_entry_header_gnutar *header, size_t *);
169 static int gnu_sparse_old_parse(struct archive_read *, struct tar *,
170 const struct gnu_sparse *sparse, int length);
171 static int gnu_sparse_01_parse(struct archive_read *, struct tar *,
172 const char *);
173 static ssize_t gnu_sparse_10_read(struct archive_read *, struct tar *,
174 size_t *);
175 static int header_Solaris_ACL(struct archive_read *, struct tar *,
176 struct archive_entry *, const void *, size_t *);
177 static int header_common(struct archive_read *, struct tar *,
178 struct archive_entry *, const void *);
179 static int header_old_tar(struct archive_read *, struct tar *,
180 struct archive_entry *, const void *);
181 static int header_pax_extensions(struct archive_read *, struct tar *,
182 struct archive_entry *, const void *, size_t *);
183 static int header_pax_global(struct archive_read *, struct tar *,
184 struct archive_entry *, const void *h, size_t *);
185 static int header_longlink(struct archive_read *, struct tar *,
186 struct archive_entry *, const void *h, size_t *);
187 static int header_longname(struct archive_read *, struct tar *,
188 struct archive_entry *, const void *h, size_t *);
189 static int read_mac_metadata_blob(struct archive_read *, struct tar *,
190 struct archive_entry *, const void *h, size_t *);
191 static int header_volume(struct archive_read *, struct tar *,
192 struct archive_entry *, const void *h, size_t *);
193 static int header_ustar(struct archive_read *, struct tar *,
194 struct archive_entry *, const void *h);
195 static int header_gnutar(struct archive_read *, struct tar *,
196 struct archive_entry *, const void *h, size_t *);
197 static int archive_read_format_tar_bid(struct archive_read *, int);
198 static int archive_read_format_tar_options(struct archive_read *,
199 const char *, const char *);
200 static int archive_read_format_tar_cleanup(struct archive_read *);
201 static int archive_read_format_tar_read_data(struct archive_read *a,
202 const void **buff, size_t *size, int64_t *offset);
203 static int archive_read_format_tar_skip(struct archive_read *a);
204 static int archive_read_format_tar_read_header(struct archive_read *,
205 struct archive_entry *);
206 static int checksum(struct archive_read *, const void *);
207 static int pax_attribute(struct archive_read *, struct tar *,
208 struct archive_entry *, const char *key, const char *value,
209 size_t value_length);
210 static int pax_attribute_acl(struct archive_read *, struct tar *,
211 struct archive_entry *, const char *, int);
212 static int pax_attribute_xattr(struct archive_entry *, const char *,
213 const char *);
214 static int pax_header(struct archive_read *, struct tar *,
215 struct archive_entry *, struct archive_string *);
216 static void pax_time(const char *, int64_t *sec, long *nanos);
217 static ssize_t readline(struct archive_read *, struct tar *, const char **,
218 ssize_t limit, size_t *);
219 static int read_body_to_string(struct archive_read *, struct tar *,
220 struct archive_string *, const void *h, size_t *);
221 static int solaris_sparse_parse(struct archive_read *, struct tar *,
222 struct archive_entry *, const char *);
223 static int64_t tar_atol(const char *, size_t);
224 static int64_t tar_atol10(const char *, size_t);
225 static int64_t tar_atol256(const char *, size_t);
226 static int64_t tar_atol8(const char *, size_t);
227 static int tar_read_header(struct archive_read *, struct tar *,
228 struct archive_entry *, size_t *);
229 static int tohex(int c);
230 static char *url_decode(const char *);
231 static void tar_flush_unconsumed(struct archive_read *, size_t *);
232
233
234 int
archive_read_support_format_gnutar(struct archive * a)235 archive_read_support_format_gnutar(struct archive *a)
236 {
237 archive_check_magic(a, ARCHIVE_READ_MAGIC,
238 ARCHIVE_STATE_NEW, "archive_read_support_format_gnutar");
239 return (archive_read_support_format_tar(a));
240 }
241
242
243 int
archive_read_support_format_tar(struct archive * _a)244 archive_read_support_format_tar(struct archive *_a)
245 {
246 struct archive_read *a = (struct archive_read *)_a;
247 struct tar *tar;
248 int r;
249
250 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
251 ARCHIVE_STATE_NEW, "archive_read_support_format_tar");
252
253 tar = (struct tar *)calloc(1, sizeof(*tar));
254 if (tar == NULL) {
255 archive_set_error(&a->archive, ENOMEM,
256 "Can't allocate tar data");
257 return (ARCHIVE_FATAL);
258 }
259 #ifdef HAVE_COPYFILE_H
260 /* Set this by default on Mac OS. */
261 tar->process_mac_extensions = 1;
262 #endif
263
264 r = __archive_read_register_format(a, tar, "tar",
265 archive_read_format_tar_bid,
266 archive_read_format_tar_options,
267 archive_read_format_tar_read_header,
268 archive_read_format_tar_read_data,
269 archive_read_format_tar_skip,
270 NULL,
271 archive_read_format_tar_cleanup,
272 NULL,
273 NULL);
274
275 if (r != ARCHIVE_OK)
276 free(tar);
277 return (ARCHIVE_OK);
278 }
279
280 static int
archive_read_format_tar_cleanup(struct archive_read * a)281 archive_read_format_tar_cleanup(struct archive_read *a)
282 {
283 struct tar *tar;
284
285 tar = (struct tar *)(a->format->data);
286 gnu_clear_sparse_list(tar);
287 archive_string_free(&tar->acl_text);
288 archive_string_free(&tar->entry_pathname);
289 archive_string_free(&tar->entry_pathname_override);
290 archive_string_free(&tar->entry_linkpath);
291 archive_string_free(&tar->entry_uname);
292 archive_string_free(&tar->entry_gname);
293 archive_string_free(&tar->line);
294 archive_string_free(&tar->pax_global);
295 archive_string_free(&tar->pax_header);
296 archive_string_free(&tar->longname);
297 archive_string_free(&tar->longlink);
298 archive_string_free(&tar->localname);
299 free(tar);
300 (a->format->data) = NULL;
301 return (ARCHIVE_OK);
302 }
303
304 /*
305 * Validate number field
306 *
307 * This has to be pretty lenient in order to accommodate the enormous
308 * variety of tar writers in the world:
309 * = POSIX (IEEE Std 1003.1-1988) ustar requires octal values with leading
310 * zeros and allows fields to be terminated with space or null characters
311 * = Many writers use different termination (in particular, libarchive
312 * omits terminator bytes to squeeze one or two more digits)
313 * = Many writers pad with space and omit leading zeros
314 * = GNU tar and star write base-256 values if numbers are too
315 * big to be represented in octal
316 *
317 * Examples of specific tar headers that we should support:
318 * = Perl Archive::Tar terminates uid, gid, devminor and devmajor with two
319 * null bytes, pads size with spaces and other numeric fields with zeroes
320 * = plexus-archiver prior to 2.6.3 (before switching to commons-compress)
321 * may have uid and gid fields filled with spaces without any octal digits
322 * at all and pads all numeric fields with spaces
323 *
324 * This should tolerate all variants in use. It will reject a field
325 * where the writer just left garbage after a trailing NUL.
326 */
327 static int
validate_number_field(const char * p_field,size_t i_size)328 validate_number_field(const char* p_field, size_t i_size)
329 {
330 unsigned char marker = (unsigned char)p_field[0];
331 if (marker == 128 || marker == 255 || marker == 0) {
332 /* Base-256 marker, there's nothing we can check. */
333 return 1;
334 } else {
335 /* Must be octal */
336 size_t i = 0;
337 /* Skip any leading spaces */
338 while (i < i_size && p_field[i] == ' ') {
339 ++i;
340 }
341 /* Skip octal digits. */
342 while (i < i_size && p_field[i] >= '0' && p_field[i] <= '7') {
343 ++i;
344 }
345 /* Any remaining characters must be space or NUL padding. */
346 while (i < i_size) {
347 if (p_field[i] != ' ' && p_field[i] != 0) {
348 return 0;
349 }
350 ++i;
351 }
352 return 1;
353 }
354 }
355
356 static int
archive_read_format_tar_bid(struct archive_read * a,int best_bid)357 archive_read_format_tar_bid(struct archive_read *a, int best_bid)
358 {
359 int bid;
360 const char *h;
361 const struct archive_entry_header_ustar *header;
362
363 (void)best_bid; /* UNUSED */
364
365 bid = 0;
366
367 /* Now let's look at the actual header and see if it matches. */
368 h = __archive_read_ahead(a, 512, NULL);
369 if (h == NULL)
370 return (-1);
371
372 /* If it's an end-of-archive mark, we can handle it. */
373 if (h[0] == 0 && archive_block_is_null(h)) {
374 /*
375 * Usually, I bid the number of bits verified, but
376 * in this case, 4096 seems excessive so I picked 10 as
377 * an arbitrary but reasonable-seeming value.
378 */
379 return (10);
380 }
381
382 /* If it's not an end-of-archive mark, it must have a valid checksum.*/
383 if (!checksum(a, h))
384 return (0);
385 bid += 48; /* Checksum is usually 6 octal digits. */
386
387 header = (const struct archive_entry_header_ustar *)h;
388
389 /* Recognize POSIX formats. */
390 if ((memcmp(header->magic, "ustar\0", 6) == 0)
391 && (memcmp(header->version, "00", 2) == 0))
392 bid += 56;
393
394 /* Recognize GNU tar format. */
395 if ((memcmp(header->magic, "ustar ", 6) == 0)
396 && (memcmp(header->version, " \0", 2) == 0))
397 bid += 56;
398
399 /* Type flag must be null, digit or A-Z, a-z. */
400 if (header->typeflag[0] != 0 &&
401 !( header->typeflag[0] >= '0' && header->typeflag[0] <= '9') &&
402 !( header->typeflag[0] >= 'A' && header->typeflag[0] <= 'Z') &&
403 !( header->typeflag[0] >= 'a' && header->typeflag[0] <= 'z') )
404 return (0);
405 bid += 2; /* 6 bits of variation in an 8-bit field leaves 2 bits. */
406
407 /*
408 * Check format of mode/uid/gid/mtime/size/rdevmajor/rdevminor fields.
409 */
410 if (validate_number_field(header->mode, sizeof(header->mode)) == 0
411 || validate_number_field(header->uid, sizeof(header->uid)) == 0
412 || validate_number_field(header->gid, sizeof(header->gid)) == 0
413 || validate_number_field(header->mtime, sizeof(header->mtime)) == 0
414 || validate_number_field(header->size, sizeof(header->size)) == 0
415 || validate_number_field(header->rdevmajor, sizeof(header->rdevmajor)) == 0
416 || validate_number_field(header->rdevminor, sizeof(header->rdevminor)) == 0) {
417 bid = 0;
418 }
419
420 return (bid);
421 }
422
423 static int
archive_read_format_tar_options(struct archive_read * a,const char * key,const char * val)424 archive_read_format_tar_options(struct archive_read *a,
425 const char *key, const char *val)
426 {
427 struct tar *tar;
428 int ret = ARCHIVE_FAILED;
429
430 tar = (struct tar *)(a->format->data);
431 if (strcmp(key, "compat-2x") == 0) {
432 /* Handle UTF-8 filenames as libarchive 2.x */
433 tar->compat_2x = (val != NULL && val[0] != 0);
434 tar->init_default_conversion = tar->compat_2x;
435 return (ARCHIVE_OK);
436 } else if (strcmp(key, "hdrcharset") == 0) {
437 if (val == NULL || val[0] == 0)
438 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
439 "tar: hdrcharset option needs a character-set name");
440 else {
441 tar->opt_sconv =
442 archive_string_conversion_from_charset(
443 &a->archive, val, 0);
444 if (tar->opt_sconv != NULL)
445 ret = ARCHIVE_OK;
446 else
447 ret = ARCHIVE_FATAL;
448 }
449 return (ret);
450 } else if (strcmp(key, "mac-ext") == 0) {
451 tar->process_mac_extensions = (val != NULL && val[0] != 0);
452 return (ARCHIVE_OK);
453 } else if (strcmp(key, "read_concatenated_archives") == 0) {
454 tar->read_concatenated_archives = (val != NULL && val[0] != 0);
455 return (ARCHIVE_OK);
456 }
457
458 /* Note: The "warn" return is just to inform the options
459 * supervisor that we didn't handle it. It will generate
460 * a suitable error if no one used this option. */
461 return (ARCHIVE_WARN);
462 }
463
464 /* utility function- this exists to centralize the logic of tracking
465 * how much unconsumed data we have floating around, and to consume
466 * anything outstanding since we're going to do read_aheads
467 */
468 static void
tar_flush_unconsumed(struct archive_read * a,size_t * unconsumed)469 tar_flush_unconsumed(struct archive_read *a, size_t *unconsumed)
470 {
471 if (*unconsumed) {
472 /*
473 void *data = (void *)__archive_read_ahead(a, *unconsumed, NULL);
474 * this block of code is to poison claimed unconsumed space, ensuring
475 * things break if it is in use still.
476 * currently it WILL break things, so enable it only for debugging this issue
477 if (data) {
478 memset(data, 0xff, *unconsumed);
479 }
480 */
481 __archive_read_consume(a, *unconsumed);
482 *unconsumed = 0;
483 }
484 }
485
486 /*
487 * The function invoked by archive_read_next_header(). This
488 * just sets up a few things and then calls the internal
489 * tar_read_header() function below.
490 */
491 static int
archive_read_format_tar_read_header(struct archive_read * a,struct archive_entry * entry)492 archive_read_format_tar_read_header(struct archive_read *a,
493 struct archive_entry *entry)
494 {
495 /*
496 * When converting tar archives to cpio archives, it is
497 * essential that each distinct file have a distinct inode
498 * number. To simplify this, we keep a static count here to
499 * assign fake dev/inode numbers to each tar entry. Note that
500 * pax format archives may overwrite this with something more
501 * useful.
502 *
503 * Ideally, we would track every file read from the archive so
504 * that we could assign the same dev/ino pair to hardlinks,
505 * but the memory required to store a complete lookup table is
506 * probably not worthwhile just to support the relatively
507 * obscure tar->cpio conversion case.
508 */
509 static int default_inode;
510 static int default_dev;
511 struct tar *tar;
512 const char *p;
513 const wchar_t *wp;
514 int r;
515 size_t l, unconsumed = 0;
516
517 /* Assign default device/inode values. */
518 archive_entry_set_dev(entry, 1 + default_dev); /* Don't use zero. */
519 archive_entry_set_ino(entry, ++default_inode); /* Don't use zero. */
520 /* Limit generated st_ino number to 16 bits. */
521 if (default_inode >= 0xffff) {
522 ++default_dev;
523 default_inode = 0;
524 }
525
526 tar = (struct tar *)(a->format->data);
527 tar->entry_offset = 0;
528 gnu_clear_sparse_list(tar);
529 tar->realsize = -1; /* Mark this as "unset" */
530 tar->realsize_override = 0;
531
532 /* Setup default string conversion. */
533 tar->sconv = tar->opt_sconv;
534 if (tar->sconv == NULL) {
535 if (!tar->init_default_conversion) {
536 tar->sconv_default =
537 archive_string_default_conversion_for_read(&(a->archive));
538 tar->init_default_conversion = 1;
539 }
540 tar->sconv = tar->sconv_default;
541 }
542
543 r = tar_read_header(a, tar, entry, &unconsumed);
544
545 tar_flush_unconsumed(a, &unconsumed);
546
547 /*
548 * "non-sparse" files are really just sparse files with
549 * a single block.
550 */
551 if (tar->sparse_list == NULL) {
552 if (gnu_add_sparse_entry(a, tar, 0, tar->entry_bytes_remaining)
553 != ARCHIVE_OK)
554 return (ARCHIVE_FATAL);
555 } else {
556 struct sparse_block *sb;
557
558 for (sb = tar->sparse_list; sb != NULL; sb = sb->next) {
559 if (!sb->hole)
560 archive_entry_sparse_add_entry(entry,
561 sb->offset, sb->remaining);
562 }
563 }
564
565 if (r == ARCHIVE_OK && archive_entry_filetype(entry) == AE_IFREG) {
566 /*
567 * "Regular" entry with trailing '/' is really
568 * directory: This is needed for certain old tar
569 * variants and even for some broken newer ones.
570 */
571 if ((wp = archive_entry_pathname_w(entry)) != NULL) {
572 l = wcslen(wp);
573 if (l > 0 && wp[l - 1] == L'/') {
574 archive_entry_set_filetype(entry, AE_IFDIR);
575 tar->entry_bytes_remaining = 0;
576 tar->entry_padding = 0;
577 }
578 } else if ((p = archive_entry_pathname(entry)) != NULL) {
579 l = strlen(p);
580 if (l > 0 && p[l - 1] == '/') {
581 archive_entry_set_filetype(entry, AE_IFDIR);
582 tar->entry_bytes_remaining = 0;
583 tar->entry_padding = 0;
584 }
585 }
586 }
587 return (r);
588 }
589
590 static int
archive_read_format_tar_read_data(struct archive_read * a,const void ** buff,size_t * size,int64_t * offset)591 archive_read_format_tar_read_data(struct archive_read *a,
592 const void **buff, size_t *size, int64_t *offset)
593 {
594 ssize_t bytes_read;
595 struct tar *tar;
596 struct sparse_block *p;
597
598 tar = (struct tar *)(a->format->data);
599
600 for (;;) {
601 /* Remove exhausted entries from sparse list. */
602 while (tar->sparse_list != NULL &&
603 tar->sparse_list->remaining == 0) {
604 p = tar->sparse_list;
605 tar->sparse_list = p->next;
606 free(p);
607 }
608
609 if (tar->entry_bytes_unconsumed) {
610 __archive_read_consume(a, tar->entry_bytes_unconsumed);
611 tar->entry_bytes_unconsumed = 0;
612 }
613
614 /* If we're at end of file, return EOF. */
615 if (tar->sparse_list == NULL ||
616 tar->entry_bytes_remaining == 0) {
617 if (__archive_read_consume(a, tar->entry_padding) < 0)
618 return (ARCHIVE_FATAL);
619 tar->entry_padding = 0;
620 *buff = NULL;
621 *size = 0;
622 *offset = tar->realsize;
623 return (ARCHIVE_EOF);
624 }
625
626 *buff = __archive_read_ahead(a, 1, &bytes_read);
627 if (bytes_read < 0)
628 return (ARCHIVE_FATAL);
629 if (*buff == NULL) {
630 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
631 "Truncated tar archive");
632 return (ARCHIVE_FATAL);
633 }
634 if (bytes_read > tar->entry_bytes_remaining)
635 bytes_read = (ssize_t)tar->entry_bytes_remaining;
636 /* Don't read more than is available in the
637 * current sparse block. */
638 if (tar->sparse_list->remaining < bytes_read)
639 bytes_read = (ssize_t)tar->sparse_list->remaining;
640 *size = bytes_read;
641 *offset = tar->sparse_list->offset;
642 tar->sparse_list->remaining -= bytes_read;
643 tar->sparse_list->offset += bytes_read;
644 tar->entry_bytes_remaining -= bytes_read;
645 tar->entry_bytes_unconsumed = bytes_read;
646
647 if (!tar->sparse_list->hole)
648 return (ARCHIVE_OK);
649 /* Current is hole data and skip this. */
650 }
651 }
652
653 static int
archive_read_format_tar_skip(struct archive_read * a)654 archive_read_format_tar_skip(struct archive_read *a)
655 {
656 int64_t bytes_skipped;
657 int64_t request;
658 struct sparse_block *p;
659 struct tar* tar;
660
661 tar = (struct tar *)(a->format->data);
662
663 /* Do not consume the hole of a sparse file. */
664 request = 0;
665 for (p = tar->sparse_list; p != NULL; p = p->next) {
666 if (!p->hole) {
667 if (p->remaining >= INT64_MAX - request) {
668 return ARCHIVE_FATAL;
669 }
670 request += p->remaining;
671 }
672 }
673 if (request > tar->entry_bytes_remaining)
674 request = tar->entry_bytes_remaining;
675 request += tar->entry_padding + tar->entry_bytes_unconsumed;
676
677 bytes_skipped = __archive_read_consume(a, request);
678 if (bytes_skipped < 0)
679 return (ARCHIVE_FATAL);
680
681 tar->entry_bytes_remaining = 0;
682 tar->entry_bytes_unconsumed = 0;
683 tar->entry_padding = 0;
684
685 /* Free the sparse list. */
686 gnu_clear_sparse_list(tar);
687
688 return (ARCHIVE_OK);
689 }
690
691 /*
692 * This function recursively interprets all of the headers associated
693 * with a single entry.
694 */
695 static int
tar_read_header(struct archive_read * a,struct tar * tar,struct archive_entry * entry,size_t * unconsumed)696 tar_read_header(struct archive_read *a, struct tar *tar,
697 struct archive_entry *entry, size_t *unconsumed)
698 {
699 ssize_t bytes;
700 int err, eof_vol_header;
701 const char *h;
702 const struct archive_entry_header_ustar *header;
703 const struct archive_entry_header_gnutar *gnuheader;
704
705 eof_vol_header = 0;
706
707 /* Loop until we find a workable header record. */
708 for (;;) {
709 tar_flush_unconsumed(a, unconsumed);
710
711 /* Read 512-byte header record */
712 h = __archive_read_ahead(a, 512, &bytes);
713 if (bytes < 0)
714 return ((int)bytes);
715 if (bytes == 0) { /* EOF at a block boundary. */
716 /* Some writers do omit the block of nulls. <sigh> */
717 return (ARCHIVE_EOF);
718 }
719 if (bytes < 512) { /* Short block at EOF; this is bad. */
720 archive_set_error(&a->archive,
721 ARCHIVE_ERRNO_FILE_FORMAT,
722 "Truncated tar archive");
723 return (ARCHIVE_FATAL);
724 }
725 *unconsumed = 512;
726
727 /* Header is workable if it's not an end-of-archive mark. */
728 if (h[0] != 0 || !archive_block_is_null(h))
729 break;
730
731 /* Ensure format is set for archives with only null blocks. */
732 if (a->archive.archive_format_name == NULL) {
733 a->archive.archive_format = ARCHIVE_FORMAT_TAR;
734 a->archive.archive_format_name = "tar";
735 }
736
737 if (!tar->read_concatenated_archives) {
738 /* Try to consume a second all-null record, as well. */
739 tar_flush_unconsumed(a, unconsumed);
740 h = __archive_read_ahead(a, 512, NULL);
741 if (h != NULL && h[0] == 0 && archive_block_is_null(h))
742 __archive_read_consume(a, 512);
743 archive_clear_error(&a->archive);
744 return (ARCHIVE_EOF);
745 }
746
747 /*
748 * We're reading concatenated archives, ignore this block and
749 * loop to get the next.
750 */
751 }
752
753 /*
754 * Note: If the checksum fails and we return ARCHIVE_RETRY,
755 * then the client is likely to just retry. This is a very
756 * crude way to search for the next valid header!
757 *
758 * TODO: Improve this by implementing a real header scan.
759 */
760 if (!checksum(a, h)) {
761 tar_flush_unconsumed(a, unconsumed);
762 archive_set_error(&a->archive, EINVAL, "Damaged tar archive");
763 return (ARCHIVE_RETRY); /* Retryable: Invalid header */
764 }
765
766 if (++tar->header_recursion_depth > 32) {
767 tar_flush_unconsumed(a, unconsumed);
768 archive_set_error(&a->archive, EINVAL, "Too many special headers");
769 return (ARCHIVE_WARN);
770 }
771
772 /* Determine the format variant. */
773 header = (const struct archive_entry_header_ustar *)h;
774
775 switch(header->typeflag[0]) {
776 case 'A': /* Solaris tar ACL */
777 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
778 a->archive.archive_format_name = "Solaris tar";
779 err = header_Solaris_ACL(a, tar, entry, h, unconsumed);
780 break;
781 case 'g': /* POSIX-standard 'g' header. */
782 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
783 a->archive.archive_format_name = "POSIX pax interchange format";
784 err = header_pax_global(a, tar, entry, h, unconsumed);
785 if (err == ARCHIVE_EOF)
786 return (err);
787 break;
788 case 'K': /* Long link name (GNU tar, others) */
789 err = header_longlink(a, tar, entry, h, unconsumed);
790 break;
791 case 'L': /* Long filename (GNU tar, others) */
792 err = header_longname(a, tar, entry, h, unconsumed);
793 break;
794 case 'V': /* GNU volume header */
795 err = header_volume(a, tar, entry, h, unconsumed);
796 if (err == ARCHIVE_EOF)
797 eof_vol_header = 1;
798 break;
799 case 'X': /* Used by SUN tar; same as 'x'. */
800 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
801 a->archive.archive_format_name =
802 "POSIX pax interchange format (Sun variant)";
803 err = header_pax_extensions(a, tar, entry, h, unconsumed);
804 break;
805 case 'x': /* POSIX-standard 'x' header. */
806 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
807 a->archive.archive_format_name = "POSIX pax interchange format";
808 err = header_pax_extensions(a, tar, entry, h, unconsumed);
809 break;
810 default:
811 gnuheader = (const struct archive_entry_header_gnutar *)h;
812 if (memcmp(gnuheader->magic, "ustar \0", 8) == 0) {
813 a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR;
814 a->archive.archive_format_name = "GNU tar format";
815 err = header_gnutar(a, tar, entry, h, unconsumed);
816 } else if (memcmp(header->magic, "ustar", 5) == 0) {
817 if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
818 a->archive.archive_format = ARCHIVE_FORMAT_TAR_USTAR;
819 a->archive.archive_format_name = "POSIX ustar format";
820 }
821 err = header_ustar(a, tar, entry, h);
822 } else {
823 a->archive.archive_format = ARCHIVE_FORMAT_TAR;
824 a->archive.archive_format_name = "tar (non-POSIX)";
825 err = header_old_tar(a, tar, entry, h);
826 }
827 }
828 if (err == ARCHIVE_FATAL)
829 return (err);
830
831 tar_flush_unconsumed(a, unconsumed);
832
833 h = NULL;
834 header = NULL;
835
836 --tar->header_recursion_depth;
837 /* Yuck. Apple's design here ends up storing long pathname
838 * extensions for both the AppleDouble extension entry and the
839 * regular entry.
840 */
841 if ((err == ARCHIVE_WARN || err == ARCHIVE_OK) &&
842 tar->header_recursion_depth == 0 &&
843 tar->process_mac_extensions) {
844 int err2 = read_mac_metadata_blob(a, tar, entry, h, unconsumed);
845 if (err2 < err)
846 err = err2;
847 }
848
849 /* We return warnings or success as-is. Anything else is fatal. */
850 if (err == ARCHIVE_WARN || err == ARCHIVE_OK) {
851 if (tar->sparse_gnu_pending) {
852 if (tar->sparse_gnu_major == 1 &&
853 tar->sparse_gnu_minor == 0) {
854 ssize_t bytes_read;
855
856 tar->sparse_gnu_pending = 0;
857 /* Read initial sparse map. */
858 bytes_read = gnu_sparse_10_read(a, tar, unconsumed);
859 if (bytes_read < 0)
860 return ((int)bytes_read);
861 tar->entry_bytes_remaining -= bytes_read;
862 } else {
863 archive_set_error(&a->archive,
864 ARCHIVE_ERRNO_MISC,
865 "Unrecognized GNU sparse file format");
866 return (ARCHIVE_WARN);
867 }
868 tar->sparse_gnu_pending = 0;
869 }
870 return (err);
871 }
872 if (err == ARCHIVE_EOF) {
873 if (!eof_vol_header) {
874 /* EOF when recursively reading a header is bad. */
875 archive_set_error(&a->archive, EINVAL,
876 "Damaged tar archive");
877 } else {
878 /* If we encounter just a GNU volume header treat
879 * this situation as an empty archive */
880 return (ARCHIVE_EOF);
881 }
882 }
883 return (ARCHIVE_FATAL);
884 }
885
886 /*
887 * Return true if block checksum is correct.
888 */
889 static int
checksum(struct archive_read * a,const void * h)890 checksum(struct archive_read *a, const void *h)
891 {
892 const unsigned char *bytes;
893 const struct archive_entry_header_ustar *header;
894 int check, sum;
895 size_t i;
896
897 (void)a; /* UNUSED */
898 bytes = (const unsigned char *)h;
899 header = (const struct archive_entry_header_ustar *)h;
900
901 /* Checksum field must hold an octal number */
902 for (i = 0; i < sizeof(header->checksum); ++i) {
903 char c = header->checksum[i];
904 if (c != ' ' && c != '\0' && (c < '0' || c > '7'))
905 return 0;
906 }
907
908 /*
909 * Test the checksum. Note that POSIX specifies _unsigned_
910 * bytes for this calculation.
911 */
912 sum = (int)tar_atol(header->checksum, sizeof(header->checksum));
913 check = 0;
914 for (i = 0; i < 148; i++)
915 check += (unsigned char)bytes[i];
916 for (; i < 156; i++)
917 check += 32;
918 for (; i < 512; i++)
919 check += (unsigned char)bytes[i];
920 if (sum == check)
921 return (1);
922
923 /*
924 * Repeat test with _signed_ bytes, just in case this archive
925 * was created by an old BSD, Solaris, or HP-UX tar with a
926 * broken checksum calculation.
927 */
928 check = 0;
929 for (i = 0; i < 148; i++)
930 check += (signed char)bytes[i];
931 for (; i < 156; i++)
932 check += 32;
933 for (; i < 512; i++)
934 check += (signed char)bytes[i];
935 if (sum == check)
936 return (1);
937
938 return (0);
939 }
940
941 /*
942 * Return true if this block contains only nulls.
943 */
944 static int
archive_block_is_null(const char * p)945 archive_block_is_null(const char *p)
946 {
947 unsigned i;
948
949 for (i = 0; i < 512; i++)
950 if (*p++)
951 return (0);
952 return (1);
953 }
954
955 /*
956 * Interpret 'A' Solaris ACL header
957 */
958 static int
header_Solaris_ACL(struct archive_read * a,struct tar * tar,struct archive_entry * entry,const void * h,size_t * unconsumed)959 header_Solaris_ACL(struct archive_read *a, struct tar *tar,
960 struct archive_entry *entry, const void *h, size_t *unconsumed)
961 {
962 const struct archive_entry_header_ustar *header;
963 size_t size;
964 int err, acl_type;
965 int64_t type;
966 char *acl, *p;
967
968 /*
969 * read_body_to_string adds a NUL terminator, but we need a little
970 * more to make sure that we don't overrun acl_text later.
971 */
972 header = (const struct archive_entry_header_ustar *)h;
973 size = (size_t)tar_atol(header->size, sizeof(header->size));
974 err = read_body_to_string(a, tar, &(tar->acl_text), h, unconsumed);
975 if (err != ARCHIVE_OK)
976 return (err);
977
978 /* Recursively read next header */
979 err = tar_read_header(a, tar, entry, unconsumed);
980 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
981 return (err);
982
983 /* TODO: Examine the first characters to see if this
984 * is an AIX ACL descriptor. We'll likely never support
985 * them, but it would be polite to recognize and warn when
986 * we do see them. */
987
988 /* Leading octal number indicates ACL type and number of entries. */
989 p = acl = tar->acl_text.s;
990 type = 0;
991 while (*p != '\0' && p < acl + size) {
992 if (*p < '0' || *p > '7') {
993 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
994 "Malformed Solaris ACL attribute (invalid digit)");
995 return(ARCHIVE_WARN);
996 }
997 type <<= 3;
998 type += *p - '0';
999 if (type > 077777777) {
1000 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1001 "Malformed Solaris ACL attribute (count too large)");
1002 return (ARCHIVE_WARN);
1003 }
1004 p++;
1005 }
1006 switch ((int)type & ~0777777) {
1007 case 01000000:
1008 /* POSIX.1e ACL */
1009 acl_type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1010 break;
1011 case 03000000:
1012 /* NFSv4 ACL */
1013 acl_type = ARCHIVE_ENTRY_ACL_TYPE_NFS4;
1014 break;
1015 default:
1016 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1017 "Malformed Solaris ACL attribute (unsupported type %o)",
1018 (int)type);
1019 return (ARCHIVE_WARN);
1020 }
1021 p++;
1022
1023 if (p >= acl + size) {
1024 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1025 "Malformed Solaris ACL attribute (body overflow)");
1026 return(ARCHIVE_WARN);
1027 }
1028
1029 /* ACL text is null-terminated; find the end. */
1030 size -= (p - acl);
1031 acl = p;
1032
1033 while (*p != '\0' && p < acl + size)
1034 p++;
1035
1036 if (tar->sconv_acl == NULL) {
1037 tar->sconv_acl = archive_string_conversion_from_charset(
1038 &(a->archive), "UTF-8", 1);
1039 if (tar->sconv_acl == NULL)
1040 return (ARCHIVE_FATAL);
1041 }
1042 archive_strncpy(&(tar->localname), acl, p - acl);
1043 err = archive_acl_from_text_l(archive_entry_acl(entry),
1044 tar->localname.s, acl_type, tar->sconv_acl);
1045 if (err != ARCHIVE_OK) {
1046 if (errno == ENOMEM) {
1047 archive_set_error(&a->archive, ENOMEM,
1048 "Can't allocate memory for ACL");
1049 } else
1050 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1051 "Malformed Solaris ACL attribute (unparsable)");
1052 }
1053 return (err);
1054 }
1055
1056 /*
1057 * Interpret 'K' long linkname header.
1058 */
1059 static int
header_longlink(struct archive_read * a,struct tar * tar,struct archive_entry * entry,const void * h,size_t * unconsumed)1060 header_longlink(struct archive_read *a, struct tar *tar,
1061 struct archive_entry *entry, const void *h, size_t *unconsumed)
1062 {
1063 int err;
1064
1065 err = read_body_to_string(a, tar, &(tar->longlink), h, unconsumed);
1066 if (err != ARCHIVE_OK)
1067 return (err);
1068 err = tar_read_header(a, tar, entry, unconsumed);
1069 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
1070 return (err);
1071 /* Set symlink if symlink already set, else hardlink. */
1072 archive_entry_copy_link(entry, tar->longlink.s);
1073 return (ARCHIVE_OK);
1074 }
1075
1076 static int
set_conversion_failed_error(struct archive_read * a,struct archive_string_conv * sconv,const char * name)1077 set_conversion_failed_error(struct archive_read *a,
1078 struct archive_string_conv *sconv, const char *name)
1079 {
1080 if (errno == ENOMEM) {
1081 archive_set_error(&a->archive, ENOMEM,
1082 "Can't allocate memory for %s", name);
1083 return (ARCHIVE_FATAL);
1084 }
1085 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1086 "%s can't be converted from %s to current locale.",
1087 name, archive_string_conversion_charset_name(sconv));
1088 return (ARCHIVE_WARN);
1089 }
1090
1091 /*
1092 * Interpret 'L' long filename header.
1093 */
1094 static int
header_longname(struct archive_read * a,struct tar * tar,struct archive_entry * entry,const void * h,size_t * unconsumed)1095 header_longname(struct archive_read *a, struct tar *tar,
1096 struct archive_entry *entry, const void *h, size_t *unconsumed)
1097 {
1098 int err;
1099
1100 err = read_body_to_string(a, tar, &(tar->longname), h, unconsumed);
1101 if (err != ARCHIVE_OK)
1102 return (err);
1103 /* Read and parse "real" header, then override name. */
1104 err = tar_read_header(a, tar, entry, unconsumed);
1105 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
1106 return (err);
1107 if (archive_entry_copy_pathname_l(entry, tar->longname.s,
1108 archive_strlen(&(tar->longname)), tar->sconv) != 0)
1109 err = set_conversion_failed_error(a, tar->sconv, "Pathname");
1110 return (err);
1111 }
1112
1113
1114 /*
1115 * Interpret 'V' GNU tar volume header.
1116 */
1117 static int
header_volume(struct archive_read * a,struct tar * tar,struct archive_entry * entry,const void * h,size_t * unconsumed)1118 header_volume(struct archive_read *a, struct tar *tar,
1119 struct archive_entry *entry, const void *h, size_t *unconsumed)
1120 {
1121 (void)h;
1122
1123 /* Just skip this and read the next header. */
1124 return (tar_read_header(a, tar, entry, unconsumed));
1125 }
1126
1127 /*
1128 * Read body of an archive entry into an archive_string object.
1129 */
1130 static int
read_body_to_string(struct archive_read * a,struct tar * tar,struct archive_string * as,const void * h,size_t * unconsumed)1131 read_body_to_string(struct archive_read *a, struct tar *tar,
1132 struct archive_string *as, const void *h, size_t *unconsumed)
1133 {
1134 int64_t size;
1135 const struct archive_entry_header_ustar *header;
1136 const void *src;
1137
1138 (void)tar; /* UNUSED */
1139 header = (const struct archive_entry_header_ustar *)h;
1140 size = tar_atol(header->size, sizeof(header->size));
1141 if ((size > 1048576) || (size < 0)) {
1142 archive_set_error(&a->archive, EINVAL,
1143 "Special header too large");
1144 return (ARCHIVE_FATAL);
1145 }
1146
1147 /* Fail if we can't make our buffer big enough. */
1148 if (archive_string_ensure(as, (size_t)size+1) == NULL) {
1149 archive_set_error(&a->archive, ENOMEM,
1150 "No memory");
1151 return (ARCHIVE_FATAL);
1152 }
1153
1154 tar_flush_unconsumed(a, unconsumed);
1155
1156 /* Read the body into the string. */
1157 *unconsumed = (size_t)((size + 511) & ~ 511);
1158 src = __archive_read_ahead(a, *unconsumed, NULL);
1159 if (src == NULL) {
1160 *unconsumed = 0;
1161 return (ARCHIVE_FATAL);
1162 }
1163 memcpy(as->s, src, (size_t)size);
1164 as->s[size] = '\0';
1165 as->length = (size_t)size;
1166 return (ARCHIVE_OK);
1167 }
1168
1169 /*
1170 * Parse out common header elements.
1171 *
1172 * This would be the same as header_old_tar, except that the
1173 * filename is handled slightly differently for old and POSIX
1174 * entries (POSIX entries support a 'prefix'). This factoring
1175 * allows header_old_tar and header_ustar
1176 * to handle filenames differently, while still putting most of the
1177 * common parsing into one place.
1178 */
1179 static int
header_common(struct archive_read * a,struct tar * tar,struct archive_entry * entry,const void * h)1180 header_common(struct archive_read *a, struct tar *tar,
1181 struct archive_entry *entry, const void *h)
1182 {
1183 const struct archive_entry_header_ustar *header;
1184 char tartype;
1185 int err = ARCHIVE_OK;
1186
1187 header = (const struct archive_entry_header_ustar *)h;
1188 if (header->linkname[0])
1189 archive_strncpy(&(tar->entry_linkpath),
1190 header->linkname, sizeof(header->linkname));
1191 else
1192 archive_string_empty(&(tar->entry_linkpath));
1193
1194 /* Parse out the numeric fields (all are octal) */
1195 archive_entry_set_mode(entry,
1196 (mode_t)tar_atol(header->mode, sizeof(header->mode)));
1197 archive_entry_set_uid(entry, tar_atol(header->uid, sizeof(header->uid)));
1198 archive_entry_set_gid(entry, tar_atol(header->gid, sizeof(header->gid)));
1199 tar->entry_bytes_remaining = tar_atol(header->size, sizeof(header->size));
1200 if (tar->entry_bytes_remaining < 0) {
1201 tar->entry_bytes_remaining = 0;
1202 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1203 "Tar entry has negative size");
1204 return (ARCHIVE_FATAL);
1205 }
1206 if (tar->entry_bytes_remaining == INT64_MAX) {
1207 /* Note: tar_atol returns INT64_MAX on overflow */
1208 tar->entry_bytes_remaining = 0;
1209 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1210 "Tar entry size overflow");
1211 return (ARCHIVE_FATAL);
1212 }
1213 tar->realsize = tar->entry_bytes_remaining;
1214 archive_entry_set_size(entry, tar->entry_bytes_remaining);
1215 archive_entry_set_mtime(entry, tar_atol(header->mtime, sizeof(header->mtime)), 0);
1216
1217 /* Handle the tar type flag appropriately. */
1218 tartype = header->typeflag[0];
1219
1220 switch (tartype) {
1221 case '1': /* Hard link */
1222 if (archive_entry_copy_hardlink_l(entry, tar->entry_linkpath.s,
1223 archive_strlen(&(tar->entry_linkpath)), tar->sconv) != 0) {
1224 err = set_conversion_failed_error(a, tar->sconv,
1225 "Linkname");
1226 if (err == ARCHIVE_FATAL)
1227 return (err);
1228 }
1229 /*
1230 * The following may seem odd, but: Technically, tar
1231 * does not store the file type for a "hard link"
1232 * entry, only the fact that it is a hard link. So, I
1233 * leave the type zero normally. But, pax interchange
1234 * format allows hard links to have data, which
1235 * implies that the underlying entry is a regular
1236 * file.
1237 */
1238 if (archive_entry_size(entry) > 0)
1239 archive_entry_set_filetype(entry, AE_IFREG);
1240
1241 /*
1242 * A tricky point: Traditionally, tar readers have
1243 * ignored the size field when reading hardlink
1244 * entries, and some writers put non-zero sizes even
1245 * though the body is empty. POSIX blessed this
1246 * convention in the 1988 standard, but broke with
1247 * this tradition in 2001 by permitting hardlink
1248 * entries to store valid bodies in pax interchange
1249 * format, but not in ustar format. Since there is no
1250 * hard and fast way to distinguish pax interchange
1251 * from earlier archives (the 'x' and 'g' entries are
1252 * optional, after all), we need a heuristic.
1253 */
1254 if (archive_entry_size(entry) == 0) {
1255 /* If the size is already zero, we're done. */
1256 } else if (a->archive.archive_format
1257 == ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
1258 /* Definitely pax extended; must obey hardlink size. */
1259 } else if (a->archive.archive_format == ARCHIVE_FORMAT_TAR
1260 || a->archive.archive_format == ARCHIVE_FORMAT_TAR_GNUTAR)
1261 {
1262 /* Old-style or GNU tar: we must ignore the size. */
1263 archive_entry_set_size(entry, 0);
1264 tar->entry_bytes_remaining = 0;
1265 } else if (archive_read_format_tar_bid(a, 50) > 50) {
1266 /*
1267 * We don't know if it's pax: If the bid
1268 * function sees a valid ustar header
1269 * immediately following, then let's ignore
1270 * the hardlink size.
1271 */
1272 archive_entry_set_size(entry, 0);
1273 tar->entry_bytes_remaining = 0;
1274 }
1275 /*
1276 * TODO: There are still two cases I'd like to handle:
1277 * = a ustar non-pax archive with a hardlink entry at
1278 * end-of-archive. (Look for block of nulls following?)
1279 * = a pax archive that has not seen any pax headers
1280 * and has an entry which is a hardlink entry storing
1281 * a body containing an uncompressed tar archive.
1282 * The first is worth addressing; I don't see any reliable
1283 * way to deal with the second possibility.
1284 */
1285 break;
1286 case '2': /* Symlink */
1287 archive_entry_set_filetype(entry, AE_IFLNK);
1288 archive_entry_set_size(entry, 0);
1289 tar->entry_bytes_remaining = 0;
1290 if (archive_entry_copy_symlink_l(entry, tar->entry_linkpath.s,
1291 archive_strlen(&(tar->entry_linkpath)), tar->sconv) != 0) {
1292 err = set_conversion_failed_error(a, tar->sconv,
1293 "Linkname");
1294 if (err == ARCHIVE_FATAL)
1295 return (err);
1296 }
1297 break;
1298 case '3': /* Character device */
1299 archive_entry_set_filetype(entry, AE_IFCHR);
1300 archive_entry_set_size(entry, 0);
1301 tar->entry_bytes_remaining = 0;
1302 break;
1303 case '4': /* Block device */
1304 archive_entry_set_filetype(entry, AE_IFBLK);
1305 archive_entry_set_size(entry, 0);
1306 tar->entry_bytes_remaining = 0;
1307 break;
1308 case '5': /* Dir */
1309 archive_entry_set_filetype(entry, AE_IFDIR);
1310 archive_entry_set_size(entry, 0);
1311 tar->entry_bytes_remaining = 0;
1312 break;
1313 case '6': /* FIFO device */
1314 archive_entry_set_filetype(entry, AE_IFIFO);
1315 archive_entry_set_size(entry, 0);
1316 tar->entry_bytes_remaining = 0;
1317 break;
1318 case 'D': /* GNU incremental directory type */
1319 /*
1320 * No special handling is actually required here.
1321 * It might be nice someday to preprocess the file list and
1322 * provide it to the client, though.
1323 */
1324 archive_entry_set_filetype(entry, AE_IFDIR);
1325 break;
1326 case 'M': /* GNU "Multi-volume" (remainder of file from last archive)*/
1327 /*
1328 * As far as I can tell, this is just like a regular file
1329 * entry, except that the contents should be _appended_ to
1330 * the indicated file at the indicated offset. This may
1331 * require some API work to fully support.
1332 */
1333 break;
1334 case 'N': /* Old GNU "long filename" entry. */
1335 /* The body of this entry is a script for renaming
1336 * previously-extracted entries. Ugh. It will never
1337 * be supported by libarchive. */
1338 archive_entry_set_filetype(entry, AE_IFREG);
1339 break;
1340 case 'S': /* GNU sparse files */
1341 /*
1342 * Sparse files are really just regular files with
1343 * sparse information in the extended area.
1344 */
1345 /* FALLTHROUGH */
1346 case '0':
1347 /*
1348 * Enable sparse file "read" support only for regular
1349 * files and explicit GNU sparse files. However, we
1350 * don't allow non-standard file types to be sparse.
1351 */
1352 tar->sparse_allowed = 1;
1353 /* FALLTHROUGH */
1354 default: /* Regular file and non-standard types */
1355 /*
1356 * Per POSIX: non-recognized types should always be
1357 * treated as regular files.
1358 */
1359 archive_entry_set_filetype(entry, AE_IFREG);
1360 break;
1361 }
1362 return (err);
1363 }
1364
1365 /*
1366 * Parse out header elements for "old-style" tar archives.
1367 */
1368 static int
header_old_tar(struct archive_read * a,struct tar * tar,struct archive_entry * entry,const void * h)1369 header_old_tar(struct archive_read *a, struct tar *tar,
1370 struct archive_entry *entry, const void *h)
1371 {
1372 const struct archive_entry_header_ustar *header;
1373 int err = ARCHIVE_OK, err2;
1374
1375 /* Copy filename over (to ensure null termination). */
1376 header = (const struct archive_entry_header_ustar *)h;
1377 if (archive_entry_copy_pathname_l(entry,
1378 header->name, sizeof(header->name), tar->sconv) != 0) {
1379 err = set_conversion_failed_error(a, tar->sconv, "Pathname");
1380 if (err == ARCHIVE_FATAL)
1381 return (err);
1382 }
1383
1384 /* Grab rest of common fields */
1385 err2 = header_common(a, tar, entry, h);
1386 if (err > err2)
1387 err = err2;
1388
1389 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1390 return (err);
1391 }
1392
1393 /*
1394 * Read a Mac AppleDouble-encoded blob of file metadata,
1395 * if there is one.
1396 */
1397 static int
read_mac_metadata_blob(struct archive_read * a,struct tar * tar,struct archive_entry * entry,const void * h,size_t * unconsumed)1398 read_mac_metadata_blob(struct archive_read *a, struct tar *tar,
1399 struct archive_entry *entry, const void *h, size_t *unconsumed)
1400 {
1401 int64_t size;
1402 size_t msize;
1403 const void *data;
1404 const char *p, *name;
1405 const wchar_t *wp, *wname;
1406
1407 (void)h; /* UNUSED */
1408
1409 wname = wp = archive_entry_pathname_w(entry);
1410 if (wp != NULL) {
1411 /* Find the last path element. */
1412 for (; *wp != L'\0'; ++wp) {
1413 if (wp[0] == '/' && wp[1] != L'\0')
1414 wname = wp + 1;
1415 }
1416 /*
1417 * If last path element starts with "._", then
1418 * this is a Mac extension.
1419 */
1420 if (wname[0] != L'.' || wname[1] != L'_' || wname[2] == L'\0')
1421 return ARCHIVE_OK;
1422 } else {
1423 /* Find the last path element. */
1424 name = p = archive_entry_pathname(entry);
1425 if (p == NULL)
1426 return (ARCHIVE_FAILED);
1427 for (; *p != '\0'; ++p) {
1428 if (p[0] == '/' && p[1] != '\0')
1429 name = p + 1;
1430 }
1431 /*
1432 * If last path element starts with "._", then
1433 * this is a Mac extension.
1434 */
1435 if (name[0] != '.' || name[1] != '_' || name[2] == '\0')
1436 return ARCHIVE_OK;
1437 }
1438
1439 /* Read the body as a Mac OS metadata blob. */
1440 size = archive_entry_size(entry);
1441 msize = (size_t)size;
1442 if (size < 0 || (uintmax_t)msize != (uintmax_t)size) {
1443 *unconsumed = 0;
1444 return (ARCHIVE_FATAL);
1445 }
1446
1447 /*
1448 * TODO: Look beyond the body here to peek at the next header.
1449 * If it's a regular header (not an extension header)
1450 * that has the wrong name, just return the current
1451 * entry as-is, without consuming the body here.
1452 * That would reduce the risk of us mis-identifying
1453 * an ordinary file that just happened to have
1454 * a name starting with "._".
1455 *
1456 * Q: Is the above idea really possible? Even
1457 * when there are GNU or pax extension entries?
1458 */
1459 data = __archive_read_ahead(a, msize, NULL);
1460 if (data == NULL) {
1461 *unconsumed = 0;
1462 return (ARCHIVE_FATAL);
1463 }
1464 archive_entry_copy_mac_metadata(entry, data, msize);
1465 *unconsumed = (msize + 511) & ~ 511;
1466 tar_flush_unconsumed(a, unconsumed);
1467 return (tar_read_header(a, tar, entry, unconsumed));
1468 }
1469
1470 /*
1471 * Parse a file header for a pax extended archive entry.
1472 */
1473 static int
header_pax_global(struct archive_read * a,struct tar * tar,struct archive_entry * entry,const void * h,size_t * unconsumed)1474 header_pax_global(struct archive_read *a, struct tar *tar,
1475 struct archive_entry *entry, const void *h, size_t *unconsumed)
1476 {
1477 int err;
1478
1479 err = read_body_to_string(a, tar, &(tar->pax_global), h, unconsumed);
1480 if (err != ARCHIVE_OK)
1481 return (err);
1482 err = tar_read_header(a, tar, entry, unconsumed);
1483 return (err);
1484 }
1485
1486 static int
header_pax_extensions(struct archive_read * a,struct tar * tar,struct archive_entry * entry,const void * h,size_t * unconsumed)1487 header_pax_extensions(struct archive_read *a, struct tar *tar,
1488 struct archive_entry *entry, const void *h, size_t *unconsumed)
1489 {
1490 int err, err2;
1491
1492 err = read_body_to_string(a, tar, &(tar->pax_header), h, unconsumed);
1493 if (err != ARCHIVE_OK)
1494 return (err);
1495
1496 /* Parse the next header. */
1497 err = tar_read_header(a, tar, entry, unconsumed);
1498 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
1499 return (err);
1500
1501 /*
1502 * TODO: Parse global/default options into 'entry' struct here
1503 * before handling file-specific options.
1504 *
1505 * This design (parse standard header, then overwrite with pax
1506 * extended attribute data) usually works well, but isn't ideal;
1507 * it would be better to parse the pax extended attributes first
1508 * and then skip any fields in the standard header that were
1509 * defined in the pax header.
1510 */
1511 err2 = pax_header(a, tar, entry, &tar->pax_header);
1512 err = err_combine(err, err2);
1513 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1514 return (err);
1515 }
1516
1517
1518 /*
1519 * Parse a file header for a Posix "ustar" archive entry. This also
1520 * handles "pax" or "extended ustar" entries.
1521 */
1522 static int
header_ustar(struct archive_read * a,struct tar * tar,struct archive_entry * entry,const void * h)1523 header_ustar(struct archive_read *a, struct tar *tar,
1524 struct archive_entry *entry, const void *h)
1525 {
1526 const struct archive_entry_header_ustar *header;
1527 struct archive_string *as;
1528 int err = ARCHIVE_OK, r;
1529
1530 header = (const struct archive_entry_header_ustar *)h;
1531
1532 /* Copy name into an internal buffer to ensure null-termination. */
1533 as = &(tar->entry_pathname);
1534 if (header->prefix[0]) {
1535 archive_strncpy(as, header->prefix, sizeof(header->prefix));
1536 if (as->s[archive_strlen(as) - 1] != '/')
1537 archive_strappend_char(as, '/');
1538 archive_strncat(as, header->name, sizeof(header->name));
1539 } else {
1540 archive_strncpy(as, header->name, sizeof(header->name));
1541 }
1542 if (archive_entry_copy_pathname_l(entry, as->s, archive_strlen(as),
1543 tar->sconv) != 0) {
1544 err = set_conversion_failed_error(a, tar->sconv, "Pathname");
1545 if (err == ARCHIVE_FATAL)
1546 return (err);
1547 }
1548
1549 /* Handle rest of common fields. */
1550 r = header_common(a, tar, entry, h);
1551 if (r == ARCHIVE_FATAL)
1552 return (r);
1553 if (r < err)
1554 err = r;
1555
1556 /* Handle POSIX ustar fields. */
1557 if (archive_entry_copy_uname_l(entry,
1558 header->uname, sizeof(header->uname), tar->sconv) != 0) {
1559 err = set_conversion_failed_error(a, tar->sconv, "Uname");
1560 if (err == ARCHIVE_FATAL)
1561 return (err);
1562 }
1563
1564 if (archive_entry_copy_gname_l(entry,
1565 header->gname, sizeof(header->gname), tar->sconv) != 0) {
1566 err = set_conversion_failed_error(a, tar->sconv, "Gname");
1567 if (err == ARCHIVE_FATAL)
1568 return (err);
1569 }
1570
1571 /* Parse out device numbers only for char and block specials. */
1572 if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
1573 archive_entry_set_rdevmajor(entry, (dev_t)
1574 tar_atol(header->rdevmajor, sizeof(header->rdevmajor)));
1575 archive_entry_set_rdevminor(entry, (dev_t)
1576 tar_atol(header->rdevminor, sizeof(header->rdevminor)));
1577 }
1578
1579 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1580
1581 return (err);
1582 }
1583
1584
1585 /*
1586 * Parse the pax extended attributes record.
1587 *
1588 * Returns non-zero if there's an error in the data.
1589 */
1590 static int
pax_header(struct archive_read * a,struct tar * tar,struct archive_entry * entry,struct archive_string * in_as)1591 pax_header(struct archive_read *a, struct tar *tar,
1592 struct archive_entry *entry, struct archive_string *in_as)
1593 {
1594 size_t attr_length, l, line_length, value_length;
1595 char *p;
1596 char *key, *value;
1597 struct archive_string *as;
1598 struct archive_string_conv *sconv;
1599 int err, err2;
1600 char *attr = in_as->s;
1601
1602 attr_length = in_as->length;
1603 tar->pax_hdrcharset_binary = 0;
1604 archive_string_empty(&(tar->entry_gname));
1605 archive_string_empty(&(tar->entry_linkpath));
1606 archive_string_empty(&(tar->entry_pathname));
1607 archive_string_empty(&(tar->entry_pathname_override));
1608 archive_string_empty(&(tar->entry_uname));
1609 err = ARCHIVE_OK;
1610 while (attr_length > 0) {
1611 /* Parse decimal length field at start of line. */
1612 line_length = 0;
1613 l = attr_length;
1614 p = attr; /* Record start of line. */
1615 while (l>0) {
1616 if (*p == ' ') {
1617 p++;
1618 l--;
1619 break;
1620 }
1621 if (*p < '0' || *p > '9') {
1622 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1623 "Ignoring malformed pax extended attributes");
1624 return (ARCHIVE_WARN);
1625 }
1626 line_length *= 10;
1627 line_length += *p - '0';
1628 if (line_length > 999999) {
1629 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1630 "Rejecting pax extended attribute > 1MB");
1631 return (ARCHIVE_WARN);
1632 }
1633 p++;
1634 l--;
1635 }
1636
1637 /*
1638 * Parsed length must be no bigger than available data,
1639 * at least 1, and the last character of the line must
1640 * be '\n'.
1641 */
1642 if (line_length > attr_length
1643 || line_length < 1
1644 || attr[line_length - 1] != '\n')
1645 {
1646 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1647 "Ignoring malformed pax extended attribute");
1648 return (ARCHIVE_WARN);
1649 }
1650
1651 /* Null-terminate the line. */
1652 attr[line_length - 1] = '\0';
1653
1654 /* Find end of key and null terminate it. */
1655 key = p;
1656 if (key[0] == '=')
1657 return (-1);
1658 while (*p && *p != '=')
1659 ++p;
1660 if (*p == '\0') {
1661 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1662 "Invalid pax extended attributes");
1663 return (ARCHIVE_WARN);
1664 }
1665 *p = '\0';
1666
1667 value = p + 1;
1668
1669 /* Some values may be binary data */
1670 value_length = attr + line_length - 1 - value;
1671
1672 /* Identify this attribute and set it in the entry. */
1673 err2 = pax_attribute(a, tar, entry, key, value, value_length);
1674 if (err2 == ARCHIVE_FATAL)
1675 return (err2);
1676 err = err_combine(err, err2);
1677
1678 /* Skip to next line */
1679 attr += line_length;
1680 attr_length -= line_length;
1681 }
1682
1683 /*
1684 * PAX format uses UTF-8 as default charset for its metadata
1685 * unless hdrcharset=BINARY is present in its header.
1686 * We apply the charset specified by the hdrcharset option only
1687 * when the hdrcharset attribute(in PAX header) is BINARY because
1688 * we respect the charset described in PAX header and BINARY also
1689 * means that metadata(filename,uname and gname) character-set
1690 * is unknown.
1691 */
1692 if (tar->pax_hdrcharset_binary)
1693 sconv = tar->opt_sconv;
1694 else {
1695 sconv = archive_string_conversion_from_charset(
1696 &(a->archive), "UTF-8", 1);
1697 if (sconv == NULL)
1698 return (ARCHIVE_FATAL);
1699 if (tar->compat_2x)
1700 archive_string_conversion_set_opt(sconv,
1701 SCONV_SET_OPT_UTF8_LIBARCHIVE2X);
1702 }
1703
1704 if (archive_strlen(&(tar->entry_gname)) > 0) {
1705 if (archive_entry_copy_gname_l(entry, tar->entry_gname.s,
1706 archive_strlen(&(tar->entry_gname)), sconv) != 0) {
1707 err = set_conversion_failed_error(a, sconv, "Gname");
1708 if (err == ARCHIVE_FATAL)
1709 return (err);
1710 /* Use a converted an original name. */
1711 archive_entry_copy_gname(entry, tar->entry_gname.s);
1712 }
1713 }
1714 if (archive_strlen(&(tar->entry_linkpath)) > 0) {
1715 if (archive_entry_copy_link_l(entry, tar->entry_linkpath.s,
1716 archive_strlen(&(tar->entry_linkpath)), sconv) != 0) {
1717 err = set_conversion_failed_error(a, sconv, "Linkname");
1718 if (err == ARCHIVE_FATAL)
1719 return (err);
1720 /* Use a converted an original name. */
1721 archive_entry_copy_link(entry, tar->entry_linkpath.s);
1722 }
1723 }
1724 /*
1725 * Some extensions (such as the GNU sparse file extensions)
1726 * deliberately store a synthetic name under the regular 'path'
1727 * attribute and the real file name under a different attribute.
1728 * Since we're supposed to not care about the order, we
1729 * have no choice but to store all of the various filenames
1730 * we find and figure it all out afterwards. This is the
1731 * figuring out part.
1732 */
1733 as = NULL;
1734 if (archive_strlen(&(tar->entry_pathname_override)) > 0)
1735 as = &(tar->entry_pathname_override);
1736 else if (archive_strlen(&(tar->entry_pathname)) > 0)
1737 as = &(tar->entry_pathname);
1738 if (as != NULL) {
1739 if (archive_entry_copy_pathname_l(entry, as->s,
1740 archive_strlen(as), sconv) != 0) {
1741 err = set_conversion_failed_error(a, sconv, "Pathname");
1742 if (err == ARCHIVE_FATAL)
1743 return (err);
1744 /* Use a converted an original name. */
1745 archive_entry_copy_pathname(entry, as->s);
1746 }
1747 }
1748 if (archive_strlen(&(tar->entry_uname)) > 0) {
1749 if (archive_entry_copy_uname_l(entry, tar->entry_uname.s,
1750 archive_strlen(&(tar->entry_uname)), sconv) != 0) {
1751 err = set_conversion_failed_error(a, sconv, "Uname");
1752 if (err == ARCHIVE_FATAL)
1753 return (err);
1754 /* Use a converted an original name. */
1755 archive_entry_copy_uname(entry, tar->entry_uname.s);
1756 }
1757 }
1758 return (err);
1759 }
1760
1761 static int
pax_attribute_xattr(struct archive_entry * entry,const char * name,const char * value)1762 pax_attribute_xattr(struct archive_entry *entry,
1763 const char *name, const char *value)
1764 {
1765 char *name_decoded;
1766 void *value_decoded;
1767 size_t value_len;
1768
1769 if (strlen(name) < 18 || (memcmp(name, "LIBARCHIVE.xattr.", 17)) != 0)
1770 return 3;
1771
1772 name += 17;
1773
1774 /* URL-decode name */
1775 name_decoded = url_decode(name);
1776 if (name_decoded == NULL)
1777 return 2;
1778
1779 /* Base-64 decode value */
1780 value_decoded = base64_decode(value, strlen(value), &value_len);
1781 if (value_decoded == NULL) {
1782 free(name_decoded);
1783 return 1;
1784 }
1785
1786 archive_entry_xattr_add_entry(entry, name_decoded,
1787 value_decoded, value_len);
1788
1789 free(name_decoded);
1790 free(value_decoded);
1791 return 0;
1792 }
1793
1794 static int
pax_attribute_schily_xattr(struct archive_entry * entry,const char * name,const char * value,size_t value_length)1795 pax_attribute_schily_xattr(struct archive_entry *entry,
1796 const char *name, const char *value, size_t value_length)
1797 {
1798 if (strlen(name) < 14 || (memcmp(name, "SCHILY.xattr.", 13)) != 0)
1799 return 1;
1800
1801 name += 13;
1802
1803 archive_entry_xattr_add_entry(entry, name, value, value_length);
1804
1805 return 0;
1806 }
1807
1808 static int
pax_attribute_rht_security_selinux(struct archive_entry * entry,const char * value,size_t value_length)1809 pax_attribute_rht_security_selinux(struct archive_entry *entry,
1810 const char *value, size_t value_length)
1811 {
1812 archive_entry_xattr_add_entry(entry, "security.selinux",
1813 value, value_length);
1814
1815 return 0;
1816 }
1817
1818 static int
pax_attribute_acl(struct archive_read * a,struct tar * tar,struct archive_entry * entry,const char * value,int type)1819 pax_attribute_acl(struct archive_read *a, struct tar *tar,
1820 struct archive_entry *entry, const char *value, int type)
1821 {
1822 int r;
1823 const char* errstr;
1824
1825 switch (type) {
1826 case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
1827 errstr = "SCHILY.acl.access";
1828 break;
1829 case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
1830 errstr = "SCHILY.acl.default";
1831 break;
1832 case ARCHIVE_ENTRY_ACL_TYPE_NFS4:
1833 errstr = "SCHILY.acl.ace";
1834 break;
1835 default:
1836 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1837 "Unknown ACL type: %d", type);
1838 return(ARCHIVE_FATAL);
1839 }
1840
1841 if (tar->sconv_acl == NULL) {
1842 tar->sconv_acl =
1843 archive_string_conversion_from_charset(
1844 &(a->archive), "UTF-8", 1);
1845 if (tar->sconv_acl == NULL)
1846 return (ARCHIVE_FATAL);
1847 }
1848
1849 r = archive_acl_from_text_l(archive_entry_acl(entry), value, type,
1850 tar->sconv_acl);
1851 if (r != ARCHIVE_OK) {
1852 if (r == ARCHIVE_FATAL) {
1853 archive_set_error(&a->archive, ENOMEM,
1854 "%s %s", "Can't allocate memory for ",
1855 errstr);
1856 return (r);
1857 }
1858 archive_set_error(&a->archive,
1859 ARCHIVE_ERRNO_MISC, "%s %s", "Parse error: ", errstr);
1860 }
1861 return (r);
1862 }
1863
1864 /*
1865 * Parse a single key=value attribute. key/value pointers are
1866 * assumed to point into reasonably long-lived storage.
1867 *
1868 * Note that POSIX reserves all-lowercase keywords. Vendor-specific
1869 * extensions should always have keywords of the form "VENDOR.attribute"
1870 * In particular, it's quite feasible to support many different
1871 * vendor extensions here. I'm using "LIBARCHIVE" for extensions
1872 * unique to this library.
1873 *
1874 * Investigate other vendor-specific extensions and see if
1875 * any of them look useful.
1876 */
1877 static int
pax_attribute(struct archive_read * a,struct tar * tar,struct archive_entry * entry,const char * key,const char * value,size_t value_length)1878 pax_attribute(struct archive_read *a, struct tar *tar,
1879 struct archive_entry *entry, const char *key, const char *value, size_t value_length)
1880 {
1881 int64_t s;
1882 long n;
1883 int err = ARCHIVE_OK, r;
1884
1885 #ifndef __FreeBSD__
1886 if (value == NULL)
1887 value = ""; /* Disable compiler warning; do not pass
1888 * NULL pointer to strlen(). */
1889 #endif
1890 switch (key[0]) {
1891 case 'G':
1892 /* Reject GNU.sparse.* headers on non-regular files. */
1893 if (strncmp(key, "GNU.sparse", 10) == 0 &&
1894 !tar->sparse_allowed) {
1895 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1896 "Non-regular file cannot be sparse");
1897 return (ARCHIVE_FATAL);
1898 }
1899
1900 /* GNU "0.0" sparse pax format. */
1901 if (strcmp(key, "GNU.sparse.numblocks") == 0) {
1902 tar->sparse_offset = -1;
1903 tar->sparse_numbytes = -1;
1904 tar->sparse_gnu_major = 0;
1905 tar->sparse_gnu_minor = 0;
1906 }
1907 if (strcmp(key, "GNU.sparse.offset") == 0) {
1908 tar->sparse_offset = tar_atol10(value, strlen(value));
1909 if (tar->sparse_numbytes != -1) {
1910 if (gnu_add_sparse_entry(a, tar,
1911 tar->sparse_offset, tar->sparse_numbytes)
1912 != ARCHIVE_OK)
1913 return (ARCHIVE_FATAL);
1914 tar->sparse_offset = -1;
1915 tar->sparse_numbytes = -1;
1916 }
1917 }
1918 if (strcmp(key, "GNU.sparse.numbytes") == 0) {
1919 tar->sparse_numbytes = tar_atol10(value, strlen(value));
1920 if (tar->sparse_offset != -1) {
1921 if (gnu_add_sparse_entry(a, tar,
1922 tar->sparse_offset, tar->sparse_numbytes)
1923 != ARCHIVE_OK)
1924 return (ARCHIVE_FATAL);
1925 tar->sparse_offset = -1;
1926 tar->sparse_numbytes = -1;
1927 }
1928 }
1929 if (strcmp(key, "GNU.sparse.size") == 0) {
1930 tar->realsize = tar_atol10(value, strlen(value));
1931 archive_entry_set_size(entry, tar->realsize);
1932 tar->realsize_override = 1;
1933 }
1934
1935 /* GNU "0.1" sparse pax format. */
1936 if (strcmp(key, "GNU.sparse.map") == 0) {
1937 tar->sparse_gnu_major = 0;
1938 tar->sparse_gnu_minor = 1;
1939 if (gnu_sparse_01_parse(a, tar, value) != ARCHIVE_OK)
1940 return (ARCHIVE_WARN);
1941 }
1942
1943 /* GNU "1.0" sparse pax format */
1944 if (strcmp(key, "GNU.sparse.major") == 0) {
1945 tar->sparse_gnu_major = (int)tar_atol10(value, strlen(value));
1946 tar->sparse_gnu_pending = 1;
1947 }
1948 if (strcmp(key, "GNU.sparse.minor") == 0) {
1949 tar->sparse_gnu_minor = (int)tar_atol10(value, strlen(value));
1950 tar->sparse_gnu_pending = 1;
1951 }
1952 if (strcmp(key, "GNU.sparse.name") == 0) {
1953 /*
1954 * The real filename; when storing sparse
1955 * files, GNU tar puts a synthesized name into
1956 * the regular 'path' attribute in an attempt
1957 * to limit confusion. ;-)
1958 */
1959 archive_strcpy(&(tar->entry_pathname_override), value);
1960 }
1961 if (strcmp(key, "GNU.sparse.realsize") == 0) {
1962 tar->realsize = tar_atol10(value, strlen(value));
1963 archive_entry_set_size(entry, tar->realsize);
1964 tar->realsize_override = 1;
1965 }
1966 break;
1967 case 'L':
1968 /* Our extensions */
1969 /* TODO: Handle arbitrary extended attributes... */
1970 /*
1971 if (strcmp(key, "LIBARCHIVE.xxxxxxx") == 0)
1972 archive_entry_set_xxxxxx(entry, value);
1973 */
1974 if (strcmp(key, "LIBARCHIVE.creationtime") == 0) {
1975 pax_time(value, &s, &n);
1976 archive_entry_set_birthtime(entry, s, n);
1977 }
1978 if (strcmp(key, "LIBARCHIVE.symlinktype") == 0) {
1979 if (strcmp(value, "file") == 0) {
1980 archive_entry_set_symlink_type(entry,
1981 AE_SYMLINK_TYPE_FILE);
1982 } else if (strcmp(value, "dir") == 0) {
1983 archive_entry_set_symlink_type(entry,
1984 AE_SYMLINK_TYPE_DIRECTORY);
1985 }
1986 }
1987 if (memcmp(key, "LIBARCHIVE.xattr.", 17) == 0)
1988 pax_attribute_xattr(entry, key, value);
1989 break;
1990 case 'R':
1991 /* GNU tar uses RHT.security header to store SELinux xattrs
1992 * SCHILY.xattr.security.selinux == RHT.security.selinux */
1993 if (strcmp(key, "RHT.security.selinux") == 0) {
1994 pax_attribute_rht_security_selinux(entry, value,
1995 value_length);
1996 }
1997 break;
1998 case 'S':
1999 /* We support some keys used by the "star" archiver */
2000 if (strcmp(key, "SCHILY.acl.access") == 0) {
2001 r = pax_attribute_acl(a, tar, entry, value,
2002 ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
2003 if (r == ARCHIVE_FATAL)
2004 return (r);
2005 } else if (strcmp(key, "SCHILY.acl.default") == 0) {
2006 r = pax_attribute_acl(a, tar, entry, value,
2007 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
2008 if (r == ARCHIVE_FATAL)
2009 return (r);
2010 } else if (strcmp(key, "SCHILY.acl.ace") == 0) {
2011 r = pax_attribute_acl(a, tar, entry, value,
2012 ARCHIVE_ENTRY_ACL_TYPE_NFS4);
2013 if (r == ARCHIVE_FATAL)
2014 return (r);
2015 } else if (strcmp(key, "SCHILY.devmajor") == 0) {
2016 archive_entry_set_rdevmajor(entry,
2017 (dev_t)tar_atol10(value, strlen(value)));
2018 } else if (strcmp(key, "SCHILY.devminor") == 0) {
2019 archive_entry_set_rdevminor(entry,
2020 (dev_t)tar_atol10(value, strlen(value)));
2021 } else if (strcmp(key, "SCHILY.fflags") == 0) {
2022 archive_entry_copy_fflags_text(entry, value);
2023 } else if (strcmp(key, "SCHILY.dev") == 0) {
2024 archive_entry_set_dev(entry,
2025 (dev_t)tar_atol10(value, strlen(value)));
2026 } else if (strcmp(key, "SCHILY.ino") == 0) {
2027 archive_entry_set_ino(entry,
2028 tar_atol10(value, strlen(value)));
2029 } else if (strcmp(key, "SCHILY.nlink") == 0) {
2030 archive_entry_set_nlink(entry, (unsigned)
2031 tar_atol10(value, strlen(value)));
2032 } else if (strcmp(key, "SCHILY.realsize") == 0) {
2033 tar->realsize = tar_atol10(value, strlen(value));
2034 tar->realsize_override = 1;
2035 archive_entry_set_size(entry, tar->realsize);
2036 } else if (strncmp(key, "SCHILY.xattr.", 13) == 0) {
2037 pax_attribute_schily_xattr(entry, key, value,
2038 value_length);
2039 } else if (strcmp(key, "SUN.holesdata") == 0) {
2040 /* A Solaris extension for sparse. */
2041 r = solaris_sparse_parse(a, tar, entry, value);
2042 if (r < err) {
2043 if (r == ARCHIVE_FATAL)
2044 return (r);
2045 err = r;
2046 archive_set_error(&a->archive,
2047 ARCHIVE_ERRNO_MISC,
2048 "Parse error: SUN.holesdata");
2049 }
2050 }
2051 break;
2052 case 'a':
2053 if (strcmp(key, "atime") == 0) {
2054 pax_time(value, &s, &n);
2055 archive_entry_set_atime(entry, s, n);
2056 }
2057 break;
2058 case 'c':
2059 if (strcmp(key, "ctime") == 0) {
2060 pax_time(value, &s, &n);
2061 archive_entry_set_ctime(entry, s, n);
2062 } else if (strcmp(key, "charset") == 0) {
2063 /* TODO: Publish charset information in entry. */
2064 } else if (strcmp(key, "comment") == 0) {
2065 /* TODO: Publish comment in entry. */
2066 }
2067 break;
2068 case 'g':
2069 if (strcmp(key, "gid") == 0) {
2070 archive_entry_set_gid(entry,
2071 tar_atol10(value, strlen(value)));
2072 } else if (strcmp(key, "gname") == 0) {
2073 archive_strcpy(&(tar->entry_gname), value);
2074 }
2075 break;
2076 case 'h':
2077 if (strcmp(key, "hdrcharset") == 0) {
2078 if (strcmp(value, "BINARY") == 0)
2079 /* Binary mode. */
2080 tar->pax_hdrcharset_binary = 1;
2081 else if (strcmp(value, "ISO-IR 10646 2000 UTF-8") == 0)
2082 tar->pax_hdrcharset_binary = 0;
2083 }
2084 break;
2085 case 'l':
2086 /* pax interchange doesn't distinguish hardlink vs. symlink. */
2087 if (strcmp(key, "linkpath") == 0) {
2088 archive_strcpy(&(tar->entry_linkpath), value);
2089 }
2090 break;
2091 case 'm':
2092 if (strcmp(key, "mtime") == 0) {
2093 pax_time(value, &s, &n);
2094 archive_entry_set_mtime(entry, s, n);
2095 }
2096 break;
2097 case 'p':
2098 if (strcmp(key, "path") == 0) {
2099 archive_strcpy(&(tar->entry_pathname), value);
2100 }
2101 break;
2102 case 'r':
2103 /* POSIX has reserved 'realtime.*' */
2104 break;
2105 case 's':
2106 /* POSIX has reserved 'security.*' */
2107 /* Someday: if (strcmp(key, "security.acl") == 0) { ... } */
2108 if (strcmp(key, "size") == 0) {
2109 /* "size" is the size of the data in the entry. */
2110 tar->entry_bytes_remaining
2111 = tar_atol10(value, strlen(value));
2112 if (tar->entry_bytes_remaining < 0) {
2113 tar->entry_bytes_remaining = 0;
2114 archive_set_error(&a->archive,
2115 ARCHIVE_ERRNO_MISC,
2116 "Tar size attribute is negative");
2117 return (ARCHIVE_FATAL);
2118 }
2119 if (tar->entry_bytes_remaining == INT64_MAX) {
2120 /* Note: tar_atol returns INT64_MAX on overflow */
2121 tar->entry_bytes_remaining = 0;
2122 archive_set_error(&a->archive,
2123 ARCHIVE_ERRNO_MISC,
2124 "Tar size attribute overflow");
2125 return (ARCHIVE_FATAL);
2126 }
2127 /*
2128 * The "size" pax header keyword always overrides the
2129 * "size" field in the tar header.
2130 * GNU.sparse.realsize, GNU.sparse.size and
2131 * SCHILY.realsize override this value.
2132 */
2133 if (!tar->realsize_override) {
2134 archive_entry_set_size(entry,
2135 tar->entry_bytes_remaining);
2136 tar->realsize
2137 = tar->entry_bytes_remaining;
2138 }
2139 }
2140 break;
2141 case 'u':
2142 if (strcmp(key, "uid") == 0) {
2143 archive_entry_set_uid(entry,
2144 tar_atol10(value, strlen(value)));
2145 } else if (strcmp(key, "uname") == 0) {
2146 archive_strcpy(&(tar->entry_uname), value);
2147 }
2148 break;
2149 }
2150 return (err);
2151 }
2152
2153
2154
2155 /*
2156 * parse a decimal time value, which may include a fractional portion
2157 */
2158 static void
pax_time(const char * p,int64_t * ps,long * pn)2159 pax_time(const char *p, int64_t *ps, long *pn)
2160 {
2161 char digit;
2162 int64_t s;
2163 unsigned long l;
2164 int sign;
2165 int64_t limit, last_digit_limit;
2166
2167 limit = INT64_MAX / 10;
2168 last_digit_limit = INT64_MAX % 10;
2169
2170 s = 0;
2171 sign = 1;
2172 if (*p == '-') {
2173 sign = -1;
2174 p++;
2175 }
2176 while (*p >= '0' && *p <= '9') {
2177 digit = *p - '0';
2178 if (s > limit ||
2179 (s == limit && digit > last_digit_limit)) {
2180 s = INT64_MAX;
2181 break;
2182 }
2183 s = (s * 10) + digit;
2184 ++p;
2185 }
2186
2187 *ps = s * sign;
2188
2189 /* Calculate nanoseconds. */
2190 *pn = 0;
2191
2192 if (*p != '.')
2193 return;
2194
2195 l = 100000000UL;
2196 do {
2197 ++p;
2198 if (*p >= '0' && *p <= '9')
2199 *pn += (*p - '0') * l;
2200 else
2201 break;
2202 } while (l /= 10);
2203 }
2204
2205 /*
2206 * Parse GNU tar header
2207 */
2208 static int
header_gnutar(struct archive_read * a,struct tar * tar,struct archive_entry * entry,const void * h,size_t * unconsumed)2209 header_gnutar(struct archive_read *a, struct tar *tar,
2210 struct archive_entry *entry, const void *h, size_t *unconsumed)
2211 {
2212 const struct archive_entry_header_gnutar *header;
2213 int64_t t;
2214 int err = ARCHIVE_OK;
2215
2216 /*
2217 * GNU header is like POSIX ustar, except 'prefix' is
2218 * replaced with some other fields. This also means the
2219 * filename is stored as in old-style archives.
2220 */
2221
2222 /* Grab fields common to all tar variants. */
2223 err = header_common(a, tar, entry, h);
2224 if (err == ARCHIVE_FATAL)
2225 return (err);
2226
2227 /* Copy filename over (to ensure null termination). */
2228 header = (const struct archive_entry_header_gnutar *)h;
2229 if (archive_entry_copy_pathname_l(entry,
2230 header->name, sizeof(header->name), tar->sconv) != 0) {
2231 err = set_conversion_failed_error(a, tar->sconv, "Pathname");
2232 if (err == ARCHIVE_FATAL)
2233 return (err);
2234 }
2235
2236 /* Fields common to ustar and GNU */
2237 /* XXX Can the following be factored out since it's common
2238 * to ustar and gnu tar? Is it okay to move it down into
2239 * header_common, perhaps? */
2240 if (archive_entry_copy_uname_l(entry,
2241 header->uname, sizeof(header->uname), tar->sconv) != 0) {
2242 err = set_conversion_failed_error(a, tar->sconv, "Uname");
2243 if (err == ARCHIVE_FATAL)
2244 return (err);
2245 }
2246
2247 if (archive_entry_copy_gname_l(entry,
2248 header->gname, sizeof(header->gname), tar->sconv) != 0) {
2249 err = set_conversion_failed_error(a, tar->sconv, "Gname");
2250 if (err == ARCHIVE_FATAL)
2251 return (err);
2252 }
2253
2254 /* Parse out device numbers only for char and block specials */
2255 if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
2256 archive_entry_set_rdevmajor(entry, (dev_t)
2257 tar_atol(header->rdevmajor, sizeof(header->rdevmajor)));
2258 archive_entry_set_rdevminor(entry, (dev_t)
2259 tar_atol(header->rdevminor, sizeof(header->rdevminor)));
2260 } else
2261 archive_entry_set_rdev(entry, 0);
2262
2263 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
2264
2265 /* Grab GNU-specific fields. */
2266 t = tar_atol(header->atime, sizeof(header->atime));
2267 if (t > 0)
2268 archive_entry_set_atime(entry, t, 0);
2269 t = tar_atol(header->ctime, sizeof(header->ctime));
2270 if (t > 0)
2271 archive_entry_set_ctime(entry, t, 0);
2272
2273 if (header->realsize[0] != 0) {
2274 tar->realsize
2275 = tar_atol(header->realsize, sizeof(header->realsize));
2276 archive_entry_set_size(entry, tar->realsize);
2277 tar->realsize_override = 1;
2278 }
2279
2280 if (header->sparse[0].offset[0] != 0) {
2281 if (gnu_sparse_old_read(a, tar, header, unconsumed)
2282 != ARCHIVE_OK)
2283 return (ARCHIVE_FATAL);
2284 } else {
2285 if (header->isextended[0] != 0) {
2286 /* XXX WTF? XXX */
2287 }
2288 }
2289
2290 return (err);
2291 }
2292
2293 static int
gnu_add_sparse_entry(struct archive_read * a,struct tar * tar,int64_t offset,int64_t remaining)2294 gnu_add_sparse_entry(struct archive_read *a, struct tar *tar,
2295 int64_t offset, int64_t remaining)
2296 {
2297 struct sparse_block *p;
2298
2299 p = (struct sparse_block *)calloc(1, sizeof(*p));
2300 if (p == NULL) {
2301 archive_set_error(&a->archive, ENOMEM, "Out of memory");
2302 return (ARCHIVE_FATAL);
2303 }
2304 if (tar->sparse_last != NULL)
2305 tar->sparse_last->next = p;
2306 else
2307 tar->sparse_list = p;
2308 tar->sparse_last = p;
2309 if (remaining < 0 || offset < 0 || offset > INT64_MAX - remaining) {
2310 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Malformed sparse map data");
2311 return (ARCHIVE_FATAL);
2312 }
2313 p->offset = offset;
2314 p->remaining = remaining;
2315 return (ARCHIVE_OK);
2316 }
2317
2318 static void
gnu_clear_sparse_list(struct tar * tar)2319 gnu_clear_sparse_list(struct tar *tar)
2320 {
2321 struct sparse_block *p;
2322
2323 while (tar->sparse_list != NULL) {
2324 p = tar->sparse_list;
2325 tar->sparse_list = p->next;
2326 free(p);
2327 }
2328 tar->sparse_last = NULL;
2329 }
2330
2331 /*
2332 * GNU tar old-format sparse data.
2333 *
2334 * GNU old-format sparse data is stored in a fixed-field
2335 * format. Offset/size values are 11-byte octal fields (same
2336 * format as 'size' field in ustart header). These are
2337 * stored in the header, allocating subsequent header blocks
2338 * as needed. Extending the header in this way is a pretty
2339 * severe POSIX violation; this design has earned GNU tar a
2340 * lot of criticism.
2341 */
2342
2343 static int
gnu_sparse_old_read(struct archive_read * a,struct tar * tar,const struct archive_entry_header_gnutar * header,size_t * unconsumed)2344 gnu_sparse_old_read(struct archive_read *a, struct tar *tar,
2345 const struct archive_entry_header_gnutar *header, size_t *unconsumed)
2346 {
2347 ssize_t bytes_read;
2348 const void *data;
2349 struct extended {
2350 struct gnu_sparse sparse[21];
2351 char isextended[1];
2352 char padding[7];
2353 };
2354 const struct extended *ext;
2355
2356 if (gnu_sparse_old_parse(a, tar, header->sparse, 4) != ARCHIVE_OK)
2357 return (ARCHIVE_FATAL);
2358 if (header->isextended[0] == 0)
2359 return (ARCHIVE_OK);
2360
2361 do {
2362 tar_flush_unconsumed(a, unconsumed);
2363 data = __archive_read_ahead(a, 512, &bytes_read);
2364 if (bytes_read < 0)
2365 return (ARCHIVE_FATAL);
2366 if (bytes_read < 512) {
2367 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2368 "Truncated tar archive "
2369 "detected while reading sparse file data");
2370 return (ARCHIVE_FATAL);
2371 }
2372 *unconsumed = 512;
2373 ext = (const struct extended *)data;
2374 if (gnu_sparse_old_parse(a, tar, ext->sparse, 21) != ARCHIVE_OK)
2375 return (ARCHIVE_FATAL);
2376 } while (ext->isextended[0] != 0);
2377 if (tar->sparse_list != NULL)
2378 tar->entry_offset = tar->sparse_list->offset;
2379 return (ARCHIVE_OK);
2380 }
2381
2382 static int
gnu_sparse_old_parse(struct archive_read * a,struct tar * tar,const struct gnu_sparse * sparse,int length)2383 gnu_sparse_old_parse(struct archive_read *a, struct tar *tar,
2384 const struct gnu_sparse *sparse, int length)
2385 {
2386 while (length > 0 && sparse->offset[0] != 0) {
2387 if (gnu_add_sparse_entry(a, tar,
2388 tar_atol(sparse->offset, sizeof(sparse->offset)),
2389 tar_atol(sparse->numbytes, sizeof(sparse->numbytes)))
2390 != ARCHIVE_OK)
2391 return (ARCHIVE_FATAL);
2392 sparse++;
2393 length--;
2394 }
2395 return (ARCHIVE_OK);
2396 }
2397
2398 /*
2399 * GNU tar sparse format 0.0
2400 *
2401 * Beginning with GNU tar 1.15, sparse files are stored using
2402 * information in the pax extended header. The GNU tar maintainers
2403 * have gone through a number of variations in the process of working
2404 * out this scheme; fortunately, they're all numbered.
2405 *
2406 * Sparse format 0.0 uses attribute GNU.sparse.numblocks to store the
2407 * number of blocks, and GNU.sparse.offset/GNU.sparse.numbytes to
2408 * store offset/size for each block. The repeated instances of these
2409 * latter fields violate the pax specification (which frowns on
2410 * duplicate keys), so this format was quickly replaced.
2411 */
2412
2413 /*
2414 * GNU tar sparse format 0.1
2415 *
2416 * This version replaced the offset/numbytes attributes with
2417 * a single "map" attribute that stored a list of integers. This
2418 * format had two problems: First, the "map" attribute could be very
2419 * long, which caused problems for some implementations. More
2420 * importantly, the sparse data was lost when extracted by archivers
2421 * that didn't recognize this extension.
2422 */
2423
2424 static int
gnu_sparse_01_parse(struct archive_read * a,struct tar * tar,const char * p)2425 gnu_sparse_01_parse(struct archive_read *a, struct tar *tar, const char *p)
2426 {
2427 const char *e;
2428 int64_t offset = -1, size = -1;
2429
2430 for (;;) {
2431 e = p;
2432 while (*e != '\0' && *e != ',') {
2433 if (*e < '0' || *e > '9')
2434 return (ARCHIVE_WARN);
2435 e++;
2436 }
2437 if (offset < 0) {
2438 offset = tar_atol10(p, e - p);
2439 if (offset < 0)
2440 return (ARCHIVE_WARN);
2441 } else {
2442 size = tar_atol10(p, e - p);
2443 if (size < 0)
2444 return (ARCHIVE_WARN);
2445 if (gnu_add_sparse_entry(a, tar, offset, size)
2446 != ARCHIVE_OK)
2447 return (ARCHIVE_FATAL);
2448 offset = -1;
2449 }
2450 if (*e == '\0')
2451 return (ARCHIVE_OK);
2452 p = e + 1;
2453 }
2454 }
2455
2456 /*
2457 * GNU tar sparse format 1.0
2458 *
2459 * The idea: The offset/size data is stored as a series of base-10
2460 * ASCII numbers prepended to the file data, so that dearchivers that
2461 * don't support this format will extract the block map along with the
2462 * data and a separate post-process can restore the sparseness.
2463 *
2464 * Unfortunately, GNU tar 1.16 had a bug that added unnecessary
2465 * padding to the body of the file when using this format. GNU tar
2466 * 1.17 corrected this bug without bumping the version number, so
2467 * it's not possible to support both variants. This code supports
2468 * the later variant at the expense of not supporting the former.
2469 *
2470 * This variant also replaced GNU.sparse.size with GNU.sparse.realsize
2471 * and introduced the GNU.sparse.major/GNU.sparse.minor attributes.
2472 */
2473
2474 /*
2475 * Read the next line from the input, and parse it as a decimal
2476 * integer followed by '\n'. Returns positive integer value or
2477 * negative on error.
2478 */
2479 static int64_t
gnu_sparse_10_atol(struct archive_read * a,struct tar * tar,int64_t * remaining,size_t * unconsumed)2480 gnu_sparse_10_atol(struct archive_read *a, struct tar *tar,
2481 int64_t *remaining, size_t *unconsumed)
2482 {
2483 int64_t l, limit, last_digit_limit;
2484 const char *p;
2485 ssize_t bytes_read;
2486 int base, digit;
2487
2488 base = 10;
2489 limit = INT64_MAX / base;
2490 last_digit_limit = INT64_MAX % base;
2491
2492 /*
2493 * Skip any lines starting with '#'; GNU tar specs
2494 * don't require this, but they should.
2495 */
2496 do {
2497 bytes_read = readline(a, tar, &p,
2498 (ssize_t)tar_min(*remaining, 100), unconsumed);
2499 if (bytes_read <= 0)
2500 return (ARCHIVE_FATAL);
2501 *remaining -= bytes_read;
2502 } while (p[0] == '#');
2503
2504 l = 0;
2505 while (bytes_read > 0) {
2506 if (*p == '\n')
2507 return (l);
2508 if (*p < '0' || *p >= '0' + base)
2509 return (ARCHIVE_WARN);
2510 digit = *p - '0';
2511 if (l > limit || (l == limit && digit > last_digit_limit))
2512 l = INT64_MAX; /* Truncate on overflow. */
2513 else
2514 l = (l * base) + digit;
2515 p++;
2516 bytes_read--;
2517 }
2518 /* TODO: Error message. */
2519 return (ARCHIVE_WARN);
2520 }
2521
2522 /*
2523 * Returns length (in bytes) of the sparse data description
2524 * that was read.
2525 */
2526 static ssize_t
gnu_sparse_10_read(struct archive_read * a,struct tar * tar,size_t * unconsumed)2527 gnu_sparse_10_read(struct archive_read *a, struct tar *tar, size_t *unconsumed)
2528 {
2529 ssize_t bytes_read;
2530 int entries;
2531 int64_t offset, size, to_skip, remaining;
2532
2533 /* Clear out the existing sparse list. */
2534 gnu_clear_sparse_list(tar);
2535
2536 remaining = tar->entry_bytes_remaining;
2537
2538 /* Parse entries. */
2539 entries = (int)gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
2540 if (entries < 0)
2541 return (ARCHIVE_FATAL);
2542 /* Parse the individual entries. */
2543 while (entries-- > 0) {
2544 /* Parse offset/size */
2545 offset = gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
2546 if (offset < 0)
2547 return (ARCHIVE_FATAL);
2548 size = gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
2549 if (size < 0)
2550 return (ARCHIVE_FATAL);
2551 /* Add a new sparse entry. */
2552 if (gnu_add_sparse_entry(a, tar, offset, size) != ARCHIVE_OK)
2553 return (ARCHIVE_FATAL);
2554 }
2555 /* Skip rest of block... */
2556 tar_flush_unconsumed(a, unconsumed);
2557 bytes_read = (ssize_t)(tar->entry_bytes_remaining - remaining);
2558 to_skip = 0x1ff & -bytes_read;
2559 /* Fail if tar->entry_bytes_remaing would get negative */
2560 if (to_skip > remaining)
2561 return (ARCHIVE_FATAL);
2562 if (to_skip != __archive_read_consume(a, to_skip))
2563 return (ARCHIVE_FATAL);
2564 return ((ssize_t)(bytes_read + to_skip));
2565 }
2566
2567 /*
2568 * Solaris pax extension for a sparse file. This is recorded with the
2569 * data and hole pairs. The way recording sparse information by Solaris'
2570 * pax simply indicates where data and sparse are, so the stored contents
2571 * consist of both data and hole.
2572 */
2573 static int
solaris_sparse_parse(struct archive_read * a,struct tar * tar,struct archive_entry * entry,const char * p)2574 solaris_sparse_parse(struct archive_read *a, struct tar *tar,
2575 struct archive_entry *entry, const char *p)
2576 {
2577 const char *e;
2578 int64_t start, end;
2579 int hole = 1;
2580
2581 (void)entry; /* UNUSED */
2582
2583 end = 0;
2584 if (*p == ' ')
2585 p++;
2586 else
2587 return (ARCHIVE_WARN);
2588 for (;;) {
2589 e = p;
2590 while (*e != '\0' && *e != ' ') {
2591 if (*e < '0' || *e > '9')
2592 return (ARCHIVE_WARN);
2593 e++;
2594 }
2595 start = end;
2596 end = tar_atol10(p, e - p);
2597 if (end < 0)
2598 return (ARCHIVE_WARN);
2599 if (start < end) {
2600 if (gnu_add_sparse_entry(a, tar, start,
2601 end - start) != ARCHIVE_OK)
2602 return (ARCHIVE_FATAL);
2603 tar->sparse_last->hole = hole;
2604 }
2605 if (*e == '\0')
2606 return (ARCHIVE_OK);
2607 p = e + 1;
2608 hole = hole == 0;
2609 }
2610 }
2611
2612 /*-
2613 * Convert text->integer.
2614 *
2615 * Traditional tar formats (including POSIX) specify base-8 for
2616 * all of the standard numeric fields. This is a significant limitation
2617 * in practice:
2618 * = file size is limited to 8GB
2619 * = rdevmajor and rdevminor are limited to 21 bits
2620 * = uid/gid are limited to 21 bits
2621 *
2622 * There are two workarounds for this:
2623 * = pax extended headers, which use variable-length string fields
2624 * = GNU tar and STAR both allow either base-8 or base-256 in
2625 * most fields. The high bit is set to indicate base-256.
2626 *
2627 * On read, this implementation supports both extensions.
2628 */
2629 static int64_t
tar_atol(const char * p,size_t char_cnt)2630 tar_atol(const char *p, size_t char_cnt)
2631 {
2632 /*
2633 * Technically, GNU tar considers a field to be in base-256
2634 * only if the first byte is 0xff or 0x80.
2635 */
2636 if (*p & 0x80)
2637 return (tar_atol256(p, char_cnt));
2638 return (tar_atol8(p, char_cnt));
2639 }
2640
2641 /*
2642 * Note that this implementation does not (and should not!) obey
2643 * locale settings; you cannot simply substitute strtol here, since
2644 * it does obey locale.
2645 */
2646 static int64_t
tar_atol_base_n(const char * p,size_t char_cnt,int base)2647 tar_atol_base_n(const char *p, size_t char_cnt, int base)
2648 {
2649 int64_t l, maxval, limit, last_digit_limit;
2650 int digit, sign;
2651
2652 maxval = INT64_MAX;
2653 limit = INT64_MAX / base;
2654 last_digit_limit = INT64_MAX % base;
2655
2656 /* the pointer will not be dereferenced if char_cnt is zero
2657 * due to the way the && operator is evaluated.
2658 */
2659 while (char_cnt != 0 && (*p == ' ' || *p == '\t')) {
2660 p++;
2661 char_cnt--;
2662 }
2663
2664 sign = 1;
2665 if (char_cnt != 0 && *p == '-') {
2666 sign = -1;
2667 p++;
2668 char_cnt--;
2669
2670 maxval = INT64_MIN;
2671 limit = -(INT64_MIN / base);
2672 last_digit_limit = -(INT64_MIN % base);
2673 }
2674
2675 l = 0;
2676 if (char_cnt != 0) {
2677 digit = *p - '0';
2678 while (digit >= 0 && digit < base && char_cnt != 0) {
2679 if (l>limit || (l == limit && digit >= last_digit_limit)) {
2680 return maxval; /* Truncate on overflow. */
2681 }
2682 l = (l * base) + digit;
2683 digit = *++p - '0';
2684 char_cnt--;
2685 }
2686 }
2687 return (sign < 0) ? -l : l;
2688 }
2689
2690 static int64_t
tar_atol8(const char * p,size_t char_cnt)2691 tar_atol8(const char *p, size_t char_cnt)
2692 {
2693 return tar_atol_base_n(p, char_cnt, 8);
2694 }
2695
2696 static int64_t
tar_atol10(const char * p,size_t char_cnt)2697 tar_atol10(const char *p, size_t char_cnt)
2698 {
2699 return tar_atol_base_n(p, char_cnt, 10);
2700 }
2701
2702 /*
2703 * Parse a base-256 integer. This is just a variable-length
2704 * twos-complement signed binary value in big-endian order, except
2705 * that the high-order bit is ignored. The values here can be up to
2706 * 12 bytes, so we need to be careful about overflowing 64-bit
2707 * (8-byte) integers.
2708 *
2709 * This code unashamedly assumes that the local machine uses 8-bit
2710 * bytes and twos-complement arithmetic.
2711 */
2712 static int64_t
tar_atol256(const char * _p,size_t char_cnt)2713 tar_atol256(const char *_p, size_t char_cnt)
2714 {
2715 uint64_t l;
2716 const unsigned char *p = (const unsigned char *)_p;
2717 unsigned char c, neg;
2718
2719 /* Extend 7-bit 2s-comp to 8-bit 2s-comp, decide sign. */
2720 c = *p;
2721 if (c & 0x40) {
2722 neg = 0xff;
2723 c |= 0x80;
2724 l = ~ARCHIVE_LITERAL_ULL(0);
2725 } else {
2726 neg = 0;
2727 c &= 0x7f;
2728 l = 0;
2729 }
2730
2731 /* If more than 8 bytes, check that we can ignore
2732 * high-order bits without overflow. */
2733 while (char_cnt > sizeof(int64_t)) {
2734 --char_cnt;
2735 if (c != neg)
2736 return neg ? INT64_MIN : INT64_MAX;
2737 c = *++p;
2738 }
2739
2740 /* c is first byte that fits; if sign mismatch, return overflow */
2741 if ((c ^ neg) & 0x80) {
2742 return neg ? INT64_MIN : INT64_MAX;
2743 }
2744
2745 /* Accumulate remaining bytes. */
2746 while (--char_cnt > 0) {
2747 l = (l << 8) | c;
2748 c = *++p;
2749 }
2750 l = (l << 8) | c;
2751 /* Return signed twos-complement value. */
2752 return (int64_t)(l);
2753 }
2754
2755 /*
2756 * Returns length of line (including trailing newline)
2757 * or negative on error. 'start' argument is updated to
2758 * point to first character of line. This avoids copying
2759 * when possible.
2760 */
2761 static ssize_t
readline(struct archive_read * a,struct tar * tar,const char ** start,ssize_t limit,size_t * unconsumed)2762 readline(struct archive_read *a, struct tar *tar, const char **start,
2763 ssize_t limit, size_t *unconsumed)
2764 {
2765 ssize_t bytes_read;
2766 ssize_t total_size = 0;
2767 const void *t;
2768 const char *s;
2769 void *p;
2770
2771 tar_flush_unconsumed(a, unconsumed);
2772
2773 t = __archive_read_ahead(a, 1, &bytes_read);
2774 if (bytes_read <= 0)
2775 return (ARCHIVE_FATAL);
2776 s = t; /* Start of line? */
2777 p = memchr(t, '\n', bytes_read);
2778 /* If we found '\n' in the read buffer, return pointer to that. */
2779 if (p != NULL) {
2780 bytes_read = 1 + ((const char *)p) - s;
2781 if (bytes_read > limit) {
2782 archive_set_error(&a->archive,
2783 ARCHIVE_ERRNO_FILE_FORMAT,
2784 "Line too long");
2785 return (ARCHIVE_FATAL);
2786 }
2787 *unconsumed = bytes_read;
2788 *start = s;
2789 return (bytes_read);
2790 }
2791 *unconsumed = bytes_read;
2792 /* Otherwise, we need to accumulate in a line buffer. */
2793 for (;;) {
2794 if (total_size + bytes_read > limit) {
2795 archive_set_error(&a->archive,
2796 ARCHIVE_ERRNO_FILE_FORMAT,
2797 "Line too long");
2798 return (ARCHIVE_FATAL);
2799 }
2800 if (archive_string_ensure(&tar->line, total_size + bytes_read) == NULL) {
2801 archive_set_error(&a->archive, ENOMEM,
2802 "Can't allocate working buffer");
2803 return (ARCHIVE_FATAL);
2804 }
2805 memcpy(tar->line.s + total_size, t, bytes_read);
2806 tar_flush_unconsumed(a, unconsumed);
2807 total_size += bytes_read;
2808 /* If we found '\n', clean up and return. */
2809 if (p != NULL) {
2810 *start = tar->line.s;
2811 return (total_size);
2812 }
2813 /* Read some more. */
2814 t = __archive_read_ahead(a, 1, &bytes_read);
2815 if (bytes_read <= 0)
2816 return (ARCHIVE_FATAL);
2817 s = t; /* Start of line? */
2818 p = memchr(t, '\n', bytes_read);
2819 /* If we found '\n', trim the read. */
2820 if (p != NULL) {
2821 bytes_read = 1 + ((const char *)p) - s;
2822 }
2823 *unconsumed = bytes_read;
2824 }
2825 }
2826
2827 /*
2828 * base64_decode - Base64 decode
2829 *
2830 * This accepts most variations of base-64 encoding, including:
2831 * * with or without line breaks
2832 * * with or without the final group padded with '=' or '_' characters
2833 * (The most economical Base-64 variant does not pad the last group and
2834 * omits line breaks; RFC1341 used for MIME requires both.)
2835 */
2836 static char *
base64_decode(const char * s,size_t len,size_t * out_len)2837 base64_decode(const char *s, size_t len, size_t *out_len)
2838 {
2839 static const unsigned char digits[64] = {
2840 'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
2841 'O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b',
2842 'c','d','e','f','g','h','i','j','k','l','m','n','o','p',
2843 'q','r','s','t','u','v','w','x','y','z','0','1','2','3',
2844 '4','5','6','7','8','9','+','/' };
2845 static unsigned char decode_table[128];
2846 char *out, *d;
2847 const unsigned char *src = (const unsigned char *)s;
2848
2849 /* If the decode table is not yet initialized, prepare it. */
2850 if (decode_table[digits[1]] != 1) {
2851 unsigned i;
2852 memset(decode_table, 0xff, sizeof(decode_table));
2853 for (i = 0; i < sizeof(digits); i++)
2854 decode_table[digits[i]] = i;
2855 }
2856
2857 /* Allocate enough space to hold the entire output. */
2858 /* Note that we may not use all of this... */
2859 out = (char *)malloc(len - len / 4 + 1);
2860 if (out == NULL) {
2861 *out_len = 0;
2862 return (NULL);
2863 }
2864 d = out;
2865
2866 while (len > 0) {
2867 /* Collect the next group of (up to) four characters. */
2868 int v = 0;
2869 int group_size = 0;
2870 while (group_size < 4 && len > 0) {
2871 /* '=' or '_' padding indicates final group. */
2872 if (*src == '=' || *src == '_') {
2873 len = 0;
2874 break;
2875 }
2876 /* Skip illegal characters (including line breaks) */
2877 if (*src > 127 || *src < 32
2878 || decode_table[*src] == 0xff) {
2879 len--;
2880 src++;
2881 continue;
2882 }
2883 v <<= 6;
2884 v |= decode_table[*src++];
2885 len --;
2886 group_size++;
2887 }
2888 /* Align a short group properly. */
2889 v <<= 6 * (4 - group_size);
2890 /* Unpack the group we just collected. */
2891 switch (group_size) {
2892 case 4: d[2] = v & 0xff;
2893 /* FALLTHROUGH */
2894 case 3: d[1] = (v >> 8) & 0xff;
2895 /* FALLTHROUGH */
2896 case 2: d[0] = (v >> 16) & 0xff;
2897 break;
2898 case 1: /* this is invalid! */
2899 break;
2900 }
2901 d += group_size * 3 / 4;
2902 }
2903
2904 *out_len = d - out;
2905 return (out);
2906 }
2907
2908 static char *
url_decode(const char * in)2909 url_decode(const char *in)
2910 {
2911 char *out, *d;
2912 const char *s;
2913
2914 out = (char *)malloc(strlen(in) + 1);
2915 if (out == NULL)
2916 return (NULL);
2917 for (s = in, d = out; *s != '\0'; ) {
2918 if (s[0] == '%' && s[1] != '\0' && s[2] != '\0') {
2919 /* Try to convert % escape */
2920 int digit1 = tohex(s[1]);
2921 int digit2 = tohex(s[2]);
2922 if (digit1 >= 0 && digit2 >= 0) {
2923 /* Looks good, consume three chars */
2924 s += 3;
2925 /* Convert output */
2926 *d++ = ((digit1 << 4) | digit2);
2927 continue;
2928 }
2929 /* Else fall through and treat '%' as normal char */
2930 }
2931 *d++ = *s++;
2932 }
2933 *d = '\0';
2934 return (out);
2935 }
2936
2937 static int
tohex(int c)2938 tohex(int c)
2939 {
2940 if (c >= '0' && c <= '9')
2941 return (c - '0');
2942 else if (c >= 'A' && c <= 'F')
2943 return (c - 'A' + 10);
2944 else if (c >= 'a' && c <= 'f')
2945 return (c - 'a' + 10);
2946 else
2947 return (-1);
2948 }
2949