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