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