1 /*- 2 * Copyright (c) 1992 Keith Muller. 3 * Copyright (c) 1992, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Keith Muller of the University of California, San Diego. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)tar.c 8.2 (Berkeley) 4/18/94 34 * $FreeBSD: src/bin/pax/tar.c,v 1.13.2.1 2001/08/01 05:03:12 obrien Exp $ 35 * $DragonFly: src/bin/pax/tar.c,v 1.7 2006/09/27 21:58:08 pavalos Exp $ 36 */ 37 38 #include <sys/types.h> 39 #include <sys/time.h> 40 #include <sys/stat.h> 41 #include <string.h> 42 #include <stdio.h> 43 #include <unistd.h> 44 #include <stdlib.h> 45 #include "pax.h" 46 #include "extern.h" 47 #include "tar.h" 48 49 /* 50 * Routines for reading, writing and header identify of various versions of tar 51 */ 52 53 static u_long tar_chksm (char *, int); 54 static char *name_split (char *, int); 55 static int ul_oct (u_long, char *, int, int); 56 static int uqd_oct (u_quad_t, char *, int, int); 57 58 /* 59 * Routines common to all versions of tar 60 */ 61 62 static int tar_nodir; /* do not write dirs under old tar */ 63 64 /* 65 * tar_endwr() 66 * add the tar trailer of two null blocks 67 * Return: 68 * 0 if ok, -1 otherwise (what wr_skip returns) 69 */ 70 71 int 72 tar_endwr(void) 73 { 74 return(wr_skip((off_t)(NULLCNT*BLKMULT))); 75 } 76 77 /* 78 * tar_endrd() 79 * no cleanup needed here, just return size of trailer (for append) 80 * Return: 81 * size of trailer (2 * BLKMULT) 82 */ 83 84 off_t 85 tar_endrd(void) 86 { 87 return((off_t)(NULLCNT*BLKMULT)); 88 } 89 90 /* 91 * tar_trail() 92 * Called to determine if a header block is a valid trailer. We are passed 93 * the block, the in_sync flag (which tells us we are in resync mode; 94 * looking for a valid header), and cnt (which starts at zero) which is 95 * used to count the number of empty blocks we have seen so far. 96 * Return: 97 * 0 if a valid trailer, -1 if not a valid trailer, or 1 if the block 98 * could never contain a header. 99 */ 100 101 int 102 tar_trail(char *buf, int in_resync, int *cnt) 103 { 104 int i; 105 106 /* 107 * look for all zero, trailer is two consecutive blocks of zero 108 */ 109 for (i = 0; i < BLKMULT; ++i) { 110 if (buf[i] != '\0') 111 break; 112 } 113 114 /* 115 * if not all zero it is not a trailer, but MIGHT be a header. 116 */ 117 if (i != BLKMULT) 118 return(-1); 119 120 /* 121 * When given a zero block, we must be careful! 122 * If we are not in resync mode, check for the trailer. Have to watch 123 * out that we do not mis-identify file data as the trailer, so we do 124 * NOT try to id a trailer during resync mode. During resync mode we 125 * might as well throw this block out since a valid header can NEVER be 126 * a block of all 0 (we must have a valid file name). 127 */ 128 if (!in_resync && (++*cnt >= NULLCNT)) 129 return(0); 130 return(1); 131 } 132 133 /* 134 * ul_oct() 135 * convert an unsigned long to an octal string. many oddball field 136 * termination characters are used by the various versions of tar in the 137 * different fields. term selects which kind to use. str is '0' padded 138 * at the front to len. we are unable to use only one format as many old 139 * tar readers are very cranky about this. 140 * Return: 141 * 0 if the number fit into the string, -1 otherwise 142 */ 143 144 static int 145 ul_oct(u_long val, char *str, int len, int term) 146 { 147 char *pt; 148 149 /* 150 * term selects the appropriate character(s) for the end of the string 151 */ 152 pt = str + len - 1; 153 switch(term) { 154 case 3: 155 *pt-- = '\0'; 156 break; 157 case 2: 158 *pt-- = ' '; 159 *pt-- = '\0'; 160 break; 161 case 1: 162 *pt-- = ' '; 163 break; 164 case 0: 165 default: 166 *pt-- = '\0'; 167 *pt-- = ' '; 168 break; 169 } 170 171 /* 172 * convert and blank pad if there is space 173 */ 174 while (pt >= str) { 175 *pt-- = '0' + (char)(val & 0x7); 176 if ((val = val >> 3) == (u_long)0) 177 break; 178 } 179 180 while (pt >= str) 181 *pt-- = '0'; 182 if (val != (u_long)0) 183 return(-1); 184 return(0); 185 } 186 187 /* 188 * uqd_oct() 189 * convert an u_quad_t to an octal string. one of many oddball field 190 * termination characters are used by the various versions of tar in the 191 * different fields. term selects which kind to use. str is '0' padded 192 * at the front to len. we are unable to use only one format as many old 193 * tar readers are very cranky about this. 194 * Return: 195 * 0 if the number fit into the string, -1 otherwise 196 */ 197 198 static int 199 uqd_oct(u_quad_t val, char *str, int len, int term) 200 { 201 char *pt; 202 203 /* 204 * term selects the appropriate character(s) for the end of the string 205 */ 206 pt = str + len - 1; 207 switch(term) { 208 case 3: 209 *pt-- = '\0'; 210 break; 211 case 2: 212 *pt-- = ' '; 213 *pt-- = '\0'; 214 break; 215 case 1: 216 *pt-- = ' '; 217 break; 218 case 0: 219 default: 220 *pt-- = '\0'; 221 *pt-- = ' '; 222 break; 223 } 224 225 /* 226 * convert and blank pad if there is space 227 */ 228 while (pt >= str) { 229 *pt-- = '0' + (char)(val & 0x7); 230 if ((val = val >> 3) == 0) 231 break; 232 } 233 234 while (pt >= str) 235 *pt-- = '0'; 236 if (val != (u_quad_t)0) 237 return(-1); 238 return(0); 239 } 240 241 /* 242 * tar_chksm() 243 * calculate the checksum for a tar block counting the checksum field as 244 * all blanks (BLNKSUM is that value pre-calculated, the sum of 8 blanks). 245 * NOTE: we use len to short circuit summing 0's on write since we ALWAYS 246 * pad headers with 0. 247 * Return: 248 * unsigned long checksum 249 */ 250 251 static u_long 252 tar_chksm(char *blk, int len) 253 { 254 char *stop; 255 char *pt; 256 u_long chksm = BLNKSUM; /* initial value is checksum field sum */ 257 258 /* 259 * add the part of the block before the checksum field 260 */ 261 pt = blk; 262 stop = blk + CHK_OFFSET; 263 while (pt < stop) 264 chksm += (u_long)(*pt++ & 0xff); 265 /* 266 * move past the checksum field and keep going, spec counts the 267 * checksum field as the sum of 8 blanks (which is pre-computed as 268 * BLNKSUM). 269 * ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding 270 * starts, no point in summing zero's) 271 */ 272 pt += CHK_LEN; 273 stop = blk + len; 274 while (pt < stop) 275 chksm += (u_long)(*pt++ & 0xff); 276 return(chksm); 277 } 278 279 /* 280 * Routines for old BSD style tar (also made portable to sysV tar) 281 */ 282 283 /* 284 * tar_id() 285 * determine if a block given to us is a valid tar header (and not a USTAR 286 * header). We have to be on the lookout for those pesky blocks of all 287 * zero's. 288 * Return: 289 * 0 if a tar header, -1 otherwise 290 */ 291 292 int 293 tar_id(char *blk, int size) 294 { 295 HD_TAR *hd; 296 HD_USTAR *uhd; 297 298 if (size < BLKMULT) 299 return(-1); 300 hd = (HD_TAR *)blk; 301 uhd = (HD_USTAR *)blk; 302 303 /* 304 * check for block of zero's first, a simple and fast test, then make 305 * sure this is not a ustar header by looking for the ustar magic 306 * cookie. We should use TMAGLEN, but some USTAR archive programs are 307 * wrong and create archives missing the \0. Last we check the 308 * checksum. If this is ok we have to assume it is a valid header. 309 */ 310 if (hd->name[0] == '\0') 311 return(-1); 312 if (strncmp(uhd->magic, TMAGIC, TMAGLEN - 1) == 0) 313 return(-1); 314 if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT)) 315 return(-1); 316 return(0); 317 } 318 319 /* 320 * tar_opt() 321 * handle tar format specific -o options 322 * Return: 323 * 0 if ok -1 otherwise 324 */ 325 326 int 327 tar_opt(void) 328 { 329 OPLIST *opt; 330 331 while ((opt = opt_next()) != NULL) { 332 if (strcmp(opt->name, TAR_OPTION) || 333 strcmp(opt->value, TAR_NODIR)) { 334 paxwarn(1, "Unknown tar format -o option/value pair %s=%s", 335 opt->name, opt->value); 336 paxwarn(1,"%s=%s is the only supported tar format option", 337 TAR_OPTION, TAR_NODIR); 338 return(-1); 339 } 340 341 /* 342 * we only support one option, and only when writing 343 */ 344 if ((act != APPND) && (act != ARCHIVE)) { 345 paxwarn(1, "%s=%s is only supported when writing.", 346 opt->name, opt->value); 347 return(-1); 348 } 349 tar_nodir = 1; 350 } 351 return(0); 352 } 353 354 355 /* 356 * tar_rd() 357 * extract the values out of block already determined to be a tar header. 358 * store the values in the ARCHD parameter. 359 * Return: 360 * 0 361 */ 362 363 int 364 tar_rd(ARCHD *arcn, char *buf) 365 { 366 HD_TAR *hd; 367 char *pt; 368 369 /* 370 * we only get proper sized buffers passed to us 371 */ 372 if (tar_id(buf, BLKMULT) < 0) 373 return(-1); 374 arcn->org_name = arcn->name; 375 arcn->sb.st_nlink = 1; 376 arcn->pat = NULL; 377 378 /* 379 * copy out the name and values in the stat buffer 380 */ 381 hd = (HD_TAR *)buf; 382 arcn->nlen = l_strncpy(arcn->name, hd->name, sizeof(arcn->name) - 1); 383 arcn->name[arcn->nlen] = '\0'; 384 arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode,sizeof(hd->mode),OCT) & 385 0xfff); 386 arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT); 387 arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT); 388 arcn->sb.st_size = (off_t)asc_uqd(hd->size, sizeof(hd->size), OCT); 389 arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT); 390 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime; 391 392 /* 393 * have to look at the last character, it may be a '/' and that is used 394 * to encode this as a directory 395 */ 396 pt = &(arcn->name[arcn->nlen - 1]); 397 arcn->pad = 0; 398 arcn->skip = 0; 399 switch(hd->linkflag) { 400 case SYMTYPE: 401 /* 402 * symbolic link, need to get the link name and set the type in 403 * the st_mode so -v printing will look correct. 404 */ 405 arcn->type = PAX_SLK; 406 arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname, 407 sizeof(arcn->ln_name) - 1); 408 arcn->ln_name[arcn->ln_nlen] = '\0'; 409 arcn->sb.st_mode |= S_IFLNK; 410 break; 411 case LNKTYPE: 412 /* 413 * hard link, need to get the link name, set the type in the 414 * st_mode and st_nlink so -v printing will look better. 415 */ 416 arcn->type = PAX_HLK; 417 arcn->sb.st_nlink = 2; 418 arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname, 419 sizeof(arcn->ln_name) - 1); 420 arcn->ln_name[arcn->ln_nlen] = '\0'; 421 422 /* 423 * no idea of what type this thing really points at, but 424 * we set something for printing only. 425 */ 426 arcn->sb.st_mode |= S_IFREG; 427 break; 428 case DIRTYPE: 429 /* 430 * It is a directory, set the mode for -v printing 431 */ 432 arcn->type = PAX_DIR; 433 arcn->sb.st_mode |= S_IFDIR; 434 arcn->sb.st_nlink = 2; 435 arcn->ln_name[0] = '\0'; 436 arcn->ln_nlen = 0; 437 break; 438 case AREGTYPE: 439 case REGTYPE: 440 default: 441 /* 442 * If we have a trailing / this is a directory and NOT a file. 443 */ 444 arcn->ln_name[0] = '\0'; 445 arcn->ln_nlen = 0; 446 if (*pt == '/') { 447 /* 448 * it is a directory, set the mode for -v printing 449 */ 450 arcn->type = PAX_DIR; 451 arcn->sb.st_mode |= S_IFDIR; 452 arcn->sb.st_nlink = 2; 453 } else { 454 /* 455 * have a file that will be followed by data. Set the 456 * skip value to the size field and calculate the size 457 * of the padding. 458 */ 459 arcn->type = PAX_REG; 460 arcn->sb.st_mode |= S_IFREG; 461 arcn->pad = TAR_PAD(arcn->sb.st_size); 462 arcn->skip = arcn->sb.st_size; 463 } 464 break; 465 } 466 467 /* 468 * strip off any trailing slash. 469 */ 470 if (*pt == '/') { 471 *pt = '\0'; 472 --arcn->nlen; 473 } 474 return(0); 475 } 476 477 /* 478 * tar_wr() 479 * write a tar header for the file specified in the ARCHD to the archive. 480 * Have to check for file types that cannot be stored and file names that 481 * are too long. Be careful of the term (last arg) to ul_oct, each field 482 * of tar has it own spec for the termination character(s). 483 * ASSUMED: space after header in header block is zero filled 484 * Return: 485 * 0 if file has data to be written after the header, 1 if file has NO 486 * data to write after the header, -1 if archive write failed 487 */ 488 489 int 490 tar_wr(ARCHD *arcn) 491 { 492 HD_TAR *hd; 493 int len; 494 char hdblk[sizeof(HD_TAR)]; 495 496 /* 497 * check for those file system types which tar cannot store 498 */ 499 switch(arcn->type) { 500 case PAX_DIR: 501 /* 502 * user asked that dirs not be written to the archive 503 */ 504 if (tar_nodir) 505 return(1); 506 break; 507 case PAX_CHR: 508 paxwarn(1, "Tar cannot archive a character device %s", 509 arcn->org_name); 510 return(1); 511 case PAX_BLK: 512 paxwarn(1, "Tar cannot archive a block device %s", arcn->org_name); 513 return(1); 514 case PAX_SCK: 515 paxwarn(1, "Tar cannot archive a socket %s", arcn->org_name); 516 return(1); 517 case PAX_FIF: 518 paxwarn(1, "Tar cannot archive a fifo %s", arcn->org_name); 519 return(1); 520 case PAX_SLK: 521 case PAX_HLK: 522 case PAX_HRG: 523 if (arcn->ln_nlen > sizeof(hd->linkname)) { 524 paxwarn(1,"Link name too long for tar %s", arcn->ln_name); 525 return(1); 526 } 527 break; 528 case PAX_REG: 529 case PAX_CTG: 530 default: 531 break; 532 } 533 534 /* 535 * check file name len, remember extra char for dirs (the / at the end) 536 */ 537 len = arcn->nlen; 538 if (arcn->type == PAX_DIR) 539 ++len; 540 if (len >= sizeof(hd->name)) { 541 paxwarn(1, "File name too long for tar %s", arcn->name); 542 return(1); 543 } 544 545 /* 546 * copy the data out of the ARCHD into the tar header based on the type 547 * of the file. Remember many tar readers want the unused fields to be 548 * padded with zero. We set the linkflag field (type), the linkname 549 * (or zero if not used),the size, and set the padding (if any) to be 550 * added after the file data (0 for all other types, as they only have 551 * a header) 552 */ 553 hd = (HD_TAR *)hdblk; 554 l_strncpy(hd->name, arcn->name, sizeof(hd->name) - 1); 555 hd->name[sizeof(hd->name) - 1] = '\0'; 556 arcn->pad = 0; 557 558 if (arcn->type == PAX_DIR) { 559 /* 560 * directories are the same as files, except have a filename 561 * that ends with a /, we add the slash here. No data follows 562 * dirs, so no pad. 563 */ 564 hd->linkflag = AREGTYPE; 565 memset(hd->linkname, 0, sizeof(hd->linkname)); 566 hd->name[len-1] = '/'; 567 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1)) 568 goto out; 569 } else if (arcn->type == PAX_SLK) { 570 /* 571 * no data follows this file, so no pad 572 */ 573 hd->linkflag = SYMTYPE; 574 l_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname) - 1); 575 hd->linkname[sizeof(hd->linkname) - 1] = '\0'; 576 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1)) 577 goto out; 578 } else if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) { 579 /* 580 * no data follows this file, so no pad 581 */ 582 hd->linkflag = LNKTYPE; 583 l_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname) - 1); 584 hd->linkname[sizeof(hd->linkname) - 1] = '\0'; 585 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1)) 586 goto out; 587 } else { 588 /* 589 * data follows this file, so set the pad 590 */ 591 hd->linkflag = AREGTYPE; 592 memset(hd->linkname, 0, sizeof(hd->linkname)); 593 if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size, 594 sizeof(hd->size), 1)) { 595 paxwarn(1,"File is too large for tar %s", arcn->org_name); 596 return(1); 597 } 598 arcn->pad = TAR_PAD(arcn->sb.st_size); 599 } 600 601 /* 602 * copy those fields that are independent of the type 603 */ 604 if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 0) || 605 ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 0) || 606 ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 0) || 607 ul_oct((u_long)arcn->sb.st_mtime, hd->mtime, sizeof(hd->mtime), 1)) 608 goto out; 609 610 /* 611 * calculate and add the checksum, then write the header. A return of 612 * 0 tells the caller to now write the file data, 1 says no data needs 613 * to be written 614 */ 615 if (ul_oct(tar_chksm(hdblk, sizeof(HD_TAR)), hd->chksum, 616 sizeof(hd->chksum), 3)) 617 goto out; 618 if (wr_rdbuf(hdblk, sizeof(HD_TAR)) < 0) 619 return(-1); 620 if (wr_skip((off_t)(BLKMULT - sizeof(HD_TAR))) < 0) 621 return(-1); 622 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG)) 623 return(0); 624 return(1); 625 626 out: 627 /* 628 * header field is out of range 629 */ 630 paxwarn(1, "Tar header field is too small for %s", arcn->org_name); 631 return(1); 632 } 633 634 /* 635 * Routines for POSIX ustar 636 */ 637 638 /* 639 * ustar_strd() 640 * initialization for ustar read 641 * Return: 642 * 0 if ok, -1 otherwise 643 */ 644 645 int 646 ustar_strd(void) 647 { 648 if ((usrtb_start() < 0) || (grptb_start() < 0)) 649 return(-1); 650 return(0); 651 } 652 653 /* 654 * ustar_stwr() 655 * initialization for ustar write 656 * Return: 657 * 0 if ok, -1 otherwise 658 */ 659 660 int 661 ustar_stwr(void) 662 { 663 if ((uidtb_start() < 0) || (gidtb_start() < 0)) 664 return(-1); 665 return(0); 666 } 667 668 /* 669 * ustar_id() 670 * determine if a block given to us is a valid ustar header. We have to 671 * be on the lookout for those pesky blocks of all zero's 672 * Return: 673 * 0 if a ustar header, -1 otherwise 674 */ 675 676 int 677 ustar_id(char *blk, int size) 678 { 679 HD_USTAR *hd; 680 681 if (size < BLKMULT) 682 return(-1); 683 hd = (HD_USTAR *)blk; 684 685 /* 686 * check for block of zero's first, a simple and fast test then check 687 * ustar magic cookie. We should use TMAGLEN, but some USTAR archive 688 * programs are fouled up and create archives missing the \0. Last we 689 * check the checksum. If ok we have to assume it is a valid header. 690 */ 691 if (hd->name[0] == '\0') 692 return(-1); 693 if (strncmp(hd->magic, TMAGIC, TMAGLEN - 1) != 0) 694 return(-1); 695 if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT)) 696 return(-1); 697 return(0); 698 } 699 700 /* 701 * ustar_rd() 702 * extract the values out of block already determined to be a ustar header. 703 * store the values in the ARCHD parameter. 704 * Return: 705 * 0 706 */ 707 708 int 709 ustar_rd(ARCHD *arcn, char *buf) 710 { 711 HD_USTAR *hd; 712 char *dest; 713 int cnt = 0; 714 dev_t devmajor; 715 dev_t devminor; 716 717 /* 718 * we only get proper sized buffers 719 */ 720 if (ustar_id(buf, BLKMULT) < 0) 721 return(-1); 722 arcn->org_name = arcn->name; 723 arcn->sb.st_nlink = 1; 724 arcn->pat = NULL; 725 arcn->nlen = 0; 726 hd = (HD_USTAR *)buf; 727 728 /* 729 * see if the filename is split into two parts. if, so joint the parts. 730 * we copy the prefix first and add a / between the prefix and name. 731 */ 732 dest = arcn->name; 733 if (*(hd->prefix) != '\0') { 734 cnt = l_strncpy(dest, hd->prefix, sizeof(arcn->name) - 2); 735 dest += cnt; 736 *dest++ = '/'; 737 cnt++; 738 } 739 arcn->nlen = cnt + l_strncpy(dest, hd->name, sizeof(arcn->name) - cnt); 740 arcn->name[arcn->nlen] = '\0'; 741 742 /* 743 * follow the spec to the letter. we should only have mode bits, strip 744 * off all other crud we may be passed. 745 */ 746 arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode, sizeof(hd->mode), OCT) & 747 0xfff); 748 arcn->sb.st_size = (off_t)asc_uqd(hd->size, sizeof(hd->size), OCT); 749 arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT); 750 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime; 751 752 /* 753 * If we can find the ascii names for gname and uname in the password 754 * and group files we will use the uid's and gid they bind. Otherwise 755 * we use the uid and gid values stored in the header. (This is what 756 * the POSIX spec wants). 757 */ 758 hd->gname[sizeof(hd->gname) - 1] = '\0'; 759 if (gid_name(hd->gname, &(arcn->sb.st_gid)) < 0) 760 arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT); 761 hd->uname[sizeof(hd->uname) - 1] = '\0'; 762 if (uid_name(hd->uname, &(arcn->sb.st_uid)) < 0) 763 arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT); 764 765 /* 766 * set the defaults, these may be changed depending on the file type 767 */ 768 arcn->ln_name[0] = '\0'; 769 arcn->ln_nlen = 0; 770 arcn->pad = 0; 771 arcn->skip = 0; 772 arcn->sb.st_rdev = (dev_t)0; 773 774 /* 775 * set the mode and PAX type according to the typeflag in the header 776 */ 777 switch(hd->typeflag) { 778 case FIFOTYPE: 779 arcn->type = PAX_FIF; 780 arcn->sb.st_mode |= S_IFIFO; 781 break; 782 case DIRTYPE: 783 arcn->type = PAX_DIR; 784 arcn->sb.st_mode |= S_IFDIR; 785 arcn->sb.st_nlink = 2; 786 787 /* 788 * Some programs that create ustar archives append a '/' 789 * to the pathname for directories. This clearly violates 790 * ustar specs, but we will silently strip it off anyway. 791 */ 792 if (arcn->name[arcn->nlen - 1] == '/') 793 arcn->name[--arcn->nlen] = '\0'; 794 break; 795 case BLKTYPE: 796 case CHRTYPE: 797 /* 798 * this type requires the rdev field to be set. 799 */ 800 if (hd->typeflag == BLKTYPE) { 801 arcn->type = PAX_BLK; 802 arcn->sb.st_mode |= S_IFBLK; 803 } else { 804 arcn->type = PAX_CHR; 805 arcn->sb.st_mode |= S_IFCHR; 806 } 807 devmajor = (dev_t)asc_ul(hd->devmajor,sizeof(hd->devmajor),OCT); 808 devminor = (dev_t)asc_ul(hd->devminor,sizeof(hd->devminor),OCT); 809 arcn->sb.st_rdev = TODEV(devmajor, devminor); 810 break; 811 case SYMTYPE: 812 case LNKTYPE: 813 if (hd->typeflag == SYMTYPE) { 814 arcn->type = PAX_SLK; 815 arcn->sb.st_mode |= S_IFLNK; 816 } else { 817 arcn->type = PAX_HLK; 818 /* 819 * so printing looks better 820 */ 821 arcn->sb.st_mode |= S_IFREG; 822 arcn->sb.st_nlink = 2; 823 } 824 /* 825 * copy the link name 826 */ 827 arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname, 828 sizeof(arcn->ln_name) - 1); 829 arcn->ln_name[arcn->ln_nlen] = '\0'; 830 break; 831 case CONTTYPE: 832 case AREGTYPE: 833 case REGTYPE: 834 default: 835 /* 836 * these types have file data that follows. Set the skip and 837 * pad fields. 838 */ 839 arcn->type = PAX_REG; 840 arcn->pad = TAR_PAD(arcn->sb.st_size); 841 arcn->skip = arcn->sb.st_size; 842 arcn->sb.st_mode |= S_IFREG; 843 break; 844 } 845 return(0); 846 } 847 848 /* 849 * ustar_wr() 850 * write a ustar header for the file specified in the ARCHD to the archive 851 * Have to check for file types that cannot be stored and file names that 852 * are too long. Be careful of the term (last arg) to ul_oct, we only use 853 * '\0' for the termination character (this is different than picky tar) 854 * ASSUMED: space after header in header block is zero filled 855 * Return: 856 * 0 if file has data to be written after the header, 1 if file has NO 857 * data to write after the header, -1 if archive write failed 858 */ 859 860 int 861 ustar_wr(ARCHD *arcn) 862 { 863 HD_USTAR *hd; 864 char *pt; 865 char hdblk[sizeof(HD_USTAR)]; 866 867 /* 868 * check for those file system types ustar cannot store 869 */ 870 if (arcn->type == PAX_SCK) { 871 paxwarn(1, "Ustar cannot archive a socket %s", arcn->org_name); 872 return(1); 873 } 874 875 /* 876 * check the length of the linkname 877 */ 878 if (((arcn->type == PAX_SLK) || (arcn->type == PAX_HLK) || 879 (arcn->type == PAX_HRG)) && (arcn->ln_nlen >= sizeof(hd->linkname))){ 880 paxwarn(1, "Link name too long for ustar %s", arcn->ln_name); 881 return(1); 882 } 883 884 /* 885 * split the path name into prefix and name fields (if needed). if 886 * pt != arcn->name, the name has to be split 887 */ 888 if ((pt = name_split(arcn->name, arcn->nlen)) == NULL) { 889 paxwarn(1, "File name too long for ustar %s", arcn->name); 890 return(1); 891 } 892 hd = (HD_USTAR *)hdblk; 893 arcn->pad = 0L; 894 895 /* 896 * split the name, or zero out the prefix 897 */ 898 if (pt != arcn->name) { 899 /* 900 * name was split, pt points at the / where the split is to 901 * occur, we remove the / and copy the first part to the prefix 902 */ 903 *pt = '\0'; 904 l_strncpy(hd->prefix, arcn->name, sizeof(hd->prefix) - 1); 905 *pt++ = '/'; 906 } else 907 memset(hd->prefix, 0, sizeof(hd->prefix)); 908 909 /* 910 * copy the name part. this may be the whole path or the part after 911 * the prefix 912 */ 913 l_strncpy(hd->name, pt, sizeof(hd->name) - 1); 914 hd->name[sizeof(hd->name) - 1] = '\0'; 915 916 /* 917 * set the fields in the header that are type dependent 918 */ 919 switch(arcn->type) { 920 case PAX_DIR: 921 hd->typeflag = DIRTYPE; 922 memset(hd->linkname, 0, sizeof(hd->linkname)); 923 memset(hd->devmajor, 0, sizeof(hd->devmajor)); 924 memset(hd->devminor, 0, sizeof(hd->devminor)); 925 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3)) 926 goto out; 927 break; 928 case PAX_CHR: 929 case PAX_BLK: 930 if (arcn->type == PAX_CHR) 931 hd->typeflag = CHRTYPE; 932 else 933 hd->typeflag = BLKTYPE; 934 memset(hd->linkname, 0, sizeof(hd->linkname)); 935 if (ul_oct((u_long)MAJOR(arcn->sb.st_rdev), hd->devmajor, 936 sizeof(hd->devmajor), 3) || 937 ul_oct((u_long)MINOR(arcn->sb.st_rdev), hd->devminor, 938 sizeof(hd->devminor), 3) || 939 ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3)) 940 goto out; 941 break; 942 case PAX_FIF: 943 hd->typeflag = FIFOTYPE; 944 memset(hd->linkname, 0, sizeof(hd->linkname)); 945 memset(hd->devmajor, 0, sizeof(hd->devmajor)); 946 memset(hd->devminor, 0, sizeof(hd->devminor)); 947 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3)) 948 goto out; 949 break; 950 case PAX_SLK: 951 case PAX_HLK: 952 case PAX_HRG: 953 if (arcn->type == PAX_SLK) 954 hd->typeflag = SYMTYPE; 955 else 956 hd->typeflag = LNKTYPE; 957 l_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname) - 1); 958 hd->linkname[sizeof(hd->linkname) - 1] = '\0'; 959 memset(hd->devmajor, 0, sizeof(hd->devmajor)); 960 memset(hd->devminor, 0, sizeof(hd->devminor)); 961 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3)) 962 goto out; 963 break; 964 case PAX_REG: 965 case PAX_CTG: 966 default: 967 /* 968 * file data with this type, set the padding 969 */ 970 if (arcn->type == PAX_CTG) 971 hd->typeflag = CONTTYPE; 972 else 973 hd->typeflag = REGTYPE; 974 memset(hd->linkname, 0, sizeof(hd->linkname)); 975 memset(hd->devmajor, 0, sizeof(hd->devmajor)); 976 memset(hd->devminor, 0, sizeof(hd->devminor)); 977 arcn->pad = TAR_PAD(arcn->sb.st_size); 978 if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size, 979 sizeof(hd->size), 3)) { 980 paxwarn(1,"File is too long for ustar %s",arcn->org_name); 981 return(1); 982 } 983 break; 984 } 985 986 l_strncpy(hd->magic, TMAGIC, TMAGLEN); 987 l_strncpy(hd->version, TVERSION, TVERSLEN); 988 989 /* 990 * set the remaining fields. Some versions want all 16 bits of mode 991 * we better humor them (they really do not meet spec though).... 992 */ 993 if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3) || 994 ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 3) || 995 ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 3) || 996 ul_oct((u_long)arcn->sb.st_mtime,hd->mtime,sizeof(hd->mtime),3)) 997 goto out; 998 l_strncpy(hd->uname,name_uid(arcn->sb.st_uid, 0),sizeof(hd->uname)); 999 l_strncpy(hd->gname,name_gid(arcn->sb.st_gid, 0),sizeof(hd->gname)); 1000 1001 /* 1002 * calculate and store the checksum write the header to the archive 1003 * return 0 tells the caller to now write the file data, 1 says no data 1004 * needs to be written 1005 */ 1006 if (ul_oct(tar_chksm(hdblk, sizeof(HD_USTAR)), hd->chksum, 1007 sizeof(hd->chksum), 3)) 1008 goto out; 1009 if (wr_rdbuf(hdblk, sizeof(HD_USTAR)) < 0) 1010 return(-1); 1011 if (wr_skip((off_t)(BLKMULT - sizeof(HD_USTAR))) < 0) 1012 return(-1); 1013 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG)) 1014 return(0); 1015 return(1); 1016 1017 out: 1018 /* 1019 * header field is out of range 1020 */ 1021 paxwarn(1, "Ustar header field is too small for %s", arcn->org_name); 1022 return(1); 1023 } 1024 1025 /* 1026 * name_split() 1027 * see if the name has to be split for storage in a ustar header. We try 1028 * to fit the entire name in the name field without splitting if we can. 1029 * The split point is always at a / 1030 * Return 1031 * character pointer to split point (always the / that is to be removed 1032 * if the split is not needed, the points is set to the start of the file 1033 * name (it would violate the spec to split there). A NULL is returned if 1034 * the file name is too long 1035 */ 1036 1037 static char * 1038 name_split(char *name, int len) 1039 { 1040 char *start; 1041 1042 /* 1043 * check to see if the file name is small enough to fit in the name 1044 * field. if so just return a pointer to the name. 1045 */ 1046 if (len < TNMSZ) 1047 return(name); 1048 if (len > (TPFSZ + TNMSZ)) 1049 return(NULL); 1050 1051 /* 1052 * we start looking at the biggest sized piece that fits in the name 1053 * field. We walk forward looking for a slash to split at. The idea is 1054 * to find the biggest piece to fit in the name field (or the smallest 1055 * prefix we can find) 1056 */ 1057 start = name + len - TNMSZ; 1058 while ((*start != '\0') && (*start != '/')) 1059 ++start; 1060 1061 /* 1062 * if we hit the end of the string, this name cannot be split, so we 1063 * cannot store this file. 1064 */ 1065 if (*start == '\0') 1066 return(NULL); 1067 len = start - name; 1068 1069 /* 1070 * NOTE: /str where the length of str == TNMSZ can not be stored under 1071 * the p1003.1-1990 spec for ustar. We could force a prefix of / and 1072 * the file would then expand on extract to //str. The len == 0 below 1073 * makes this special case follow the spec to the letter. 1074 */ 1075 if ((len >= TPFSZ) || (len == 0)) 1076 return(NULL); 1077 1078 /* 1079 * ok have a split point, return it to the caller 1080 */ 1081 return(start); 1082 } 1083