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. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)cpio.c 8.1 (Berkeley) 5/31/93 38 * $FreeBSD: src/bin/pax/cpio.c,v 1.12.2.1 2001/08/01 05:03:11 obrien Exp $ 39 * $DragonFly: src/bin/pax/cpio.c,v 1.2 2003/06/17 04:22:50 dillon Exp $ 40 */ 41 42 #include <sys/types.h> 43 #include <sys/time.h> 44 #include <sys/stat.h> 45 #include <string.h> 46 #include <stdio.h> 47 #include <unistd.h> 48 #include <stdlib.h> 49 #include "pax.h" 50 #include "cpio.h" 51 #include "extern.h" 52 53 static int rd_nm __P((register ARCHD *, int)); 54 static int rd_ln_nm __P((register ARCHD *)); 55 static int com_rd __P((register ARCHD *)); 56 57 /* 58 * Routines which support the different cpio versions 59 */ 60 61 static int swp_head; /* binary cpio header byte swap */ 62 63 /* 64 * Routines common to all versions of cpio 65 */ 66 67 /* 68 * cpio_strd() 69 * Fire up the hard link detection code 70 * Return: 71 * 0 if ok -1 otherwise (the return values of lnk_start()) 72 */ 73 74 #ifdef __STDC__ 75 int 76 cpio_strd(void) 77 #else 78 int 79 cpio_strd() 80 #endif 81 { 82 return(lnk_start()); 83 } 84 85 /* 86 * cpio_trail() 87 * Called to determine if a header block is a valid trailer. We are 88 * passed the block, the in_sync flag (which tells us we are in resync 89 * mode; looking for a valid header), and cnt (which starts at zero) 90 * which is used to count the number of empty blocks we have seen so far. 91 * Return: 92 * 0 if a valid trailer, -1 if not a valid trailer, 93 */ 94 95 #ifdef __STDC__ 96 int 97 cpio_trail(register ARCHD *arcn) 98 #else 99 int 100 cpio_trail(arcn) 101 register ARCHD *arcn; 102 #endif 103 { 104 /* 105 * look for trailer id in file we are about to process 106 */ 107 if ((strcmp(arcn->name, TRAILER) == 0) && (arcn->sb.st_size == 0)) 108 return(0); 109 return(-1); 110 } 111 112 /* 113 * com_rd() 114 * operations common to all cpio read functions. 115 * Return: 116 * 0 117 */ 118 119 #ifdef __STDC__ 120 static int 121 com_rd(register ARCHD *arcn) 122 #else 123 static int 124 com_rd(arcn) 125 register ARCHD *arcn; 126 #endif 127 { 128 arcn->skip = 0; 129 arcn->pat = NULL; 130 arcn->org_name = arcn->name; 131 switch(arcn->sb.st_mode & C_IFMT) { 132 case C_ISFIFO: 133 arcn->type = PAX_FIF; 134 break; 135 case C_ISDIR: 136 arcn->type = PAX_DIR; 137 break; 138 case C_ISBLK: 139 arcn->type = PAX_BLK; 140 break; 141 case C_ISCHR: 142 arcn->type = PAX_CHR; 143 break; 144 case C_ISLNK: 145 arcn->type = PAX_SLK; 146 break; 147 case C_ISOCK: 148 arcn->type = PAX_SCK; 149 break; 150 case C_ISCTG: 151 case C_ISREG: 152 default: 153 /* 154 * we have file data, set up skip (pad is set in the format 155 * specific sections) 156 */ 157 arcn->sb.st_mode = (arcn->sb.st_mode & 0xfff) | C_ISREG; 158 arcn->type = PAX_REG; 159 arcn->skip = arcn->sb.st_size; 160 break; 161 } 162 if (chk_lnk(arcn) < 0) 163 return(-1); 164 return(0); 165 } 166 167 /* 168 * cpio_end_wr() 169 * write the special file with the name trailer in the proper format 170 * Return: 171 * result of the write of the trailer from the cpio specific write func 172 */ 173 174 #ifdef __STDC__ 175 int 176 cpio_endwr(void) 177 #else 178 int 179 cpio_endwr() 180 #endif 181 { 182 ARCHD last; 183 184 /* 185 * create a trailer request and call the proper format write function 186 */ 187 memset(&last, 0, sizeof(last)); 188 last.nlen = sizeof(TRAILER) - 1; 189 last.type = PAX_REG; 190 last.sb.st_nlink = 1; 191 (void)strcpy(last.name, TRAILER); 192 return((*frmt->wr)(&last)); 193 } 194 195 /* 196 * rd_nam() 197 * read in the file name which follows the cpio header 198 * Return: 199 * 0 if ok, -1 otherwise 200 */ 201 202 #ifdef __STDC__ 203 static int 204 rd_nm(register ARCHD *arcn, int nsz) 205 #else 206 static int 207 rd_nm(arcn, nsz) 208 register ARCHD *arcn; 209 int nsz; 210 #endif 211 { 212 /* 213 * do not even try bogus values 214 */ 215 if ((nsz == 0) || (nsz > sizeof(arcn->name))) { 216 paxwarn(1, "Cpio file name length %d is out of range", nsz); 217 return(-1); 218 } 219 220 /* 221 * read the name and make sure it is not empty and is \0 terminated 222 */ 223 if ((rd_wrbuf(arcn->name,nsz) != nsz) || (arcn->name[nsz-1] != '\0') || 224 (arcn->name[0] == '\0')) { 225 paxwarn(1, "Cpio file name in header is corrupted"); 226 return(-1); 227 } 228 return(0); 229 } 230 231 /* 232 * rd_ln_nm() 233 * read in the link name for a file with links. The link name is stored 234 * like file data (and is NOT \0 terminated!) 235 * Return: 236 * 0 if ok, -1 otherwise 237 */ 238 239 #ifdef __STDC__ 240 static int 241 rd_ln_nm(register ARCHD *arcn) 242 #else 243 static int 244 rd_ln_nm(arcn) 245 register ARCHD *arcn; 246 #endif 247 { 248 /* 249 * check the length specified for bogus values 250 */ 251 if ((arcn->sb.st_size == 0) || 252 (arcn->sb.st_size >= sizeof(arcn->ln_name))) { 253 # ifdef NET2_STAT 254 paxwarn(1, "Cpio link name length is invalid: %lu", 255 arcn->sb.st_size); 256 # else 257 paxwarn(1, "Cpio link name length is invalid: %qu", 258 arcn->sb.st_size); 259 # endif 260 return(-1); 261 } 262 263 /* 264 * read in the link name and \0 terminate it 265 */ 266 if (rd_wrbuf(arcn->ln_name, (int)arcn->sb.st_size) != 267 (int)arcn->sb.st_size) { 268 paxwarn(1, "Cpio link name read error"); 269 return(-1); 270 } 271 arcn->ln_nlen = arcn->sb.st_size; 272 arcn->ln_name[arcn->ln_nlen] = '\0'; 273 274 /* 275 * watch out for those empty link names 276 */ 277 if (arcn->ln_name[0] == '\0') { 278 paxwarn(1, "Cpio link name is corrupt"); 279 return(-1); 280 } 281 return(0); 282 } 283 284 /* 285 * Routines common to the extended byte oriented cpio format 286 */ 287 288 /* 289 * cpio_id() 290 * determine if a block given to us is a valid extended byte oriented 291 * cpio header 292 * Return: 293 * 0 if a valid header, -1 otherwise 294 */ 295 296 #ifdef __STDC__ 297 int 298 cpio_id(char *blk, int size) 299 #else 300 int 301 cpio_id(blk, size) 302 char *blk; 303 int size; 304 #endif 305 { 306 if ((size < sizeof(HD_CPIO)) || 307 (strncmp(blk, AMAGIC, sizeof(AMAGIC) - 1) != 0)) 308 return(-1); 309 return(0); 310 } 311 312 /* 313 * cpio_rd() 314 * determine if a buffer is a byte oriented extended cpio archive entry. 315 * convert and store the values in the ARCHD parameter. 316 * Return: 317 * 0 if a valid header, -1 otherwise. 318 */ 319 320 #ifdef __STDC__ 321 int 322 cpio_rd(register ARCHD *arcn, register char *buf) 323 #else 324 int 325 cpio_rd(arcn, buf) 326 register ARCHD *arcn; 327 register char *buf; 328 #endif 329 { 330 register int nsz; 331 register HD_CPIO *hd; 332 333 /* 334 * check that this is a valid header, if not return -1 335 */ 336 if (cpio_id(buf, sizeof(HD_CPIO)) < 0) 337 return(-1); 338 hd = (HD_CPIO *)buf; 339 340 /* 341 * byte oriented cpio (posix) does not have padding! extract the octal 342 * ascii fields from the header 343 */ 344 arcn->pad = 0L; 345 arcn->sb.st_dev = (dev_t)asc_ul(hd->c_dev, sizeof(hd->c_dev), OCT); 346 arcn->sb.st_ino = (ino_t)asc_ul(hd->c_ino, sizeof(hd->c_ino), OCT); 347 arcn->sb.st_mode = (mode_t)asc_ul(hd->c_mode, sizeof(hd->c_mode), OCT); 348 arcn->sb.st_uid = (uid_t)asc_ul(hd->c_uid, sizeof(hd->c_uid), OCT); 349 arcn->sb.st_gid = (gid_t)asc_ul(hd->c_gid, sizeof(hd->c_gid), OCT); 350 arcn->sb.st_nlink = (nlink_t)asc_ul(hd->c_nlink, sizeof(hd->c_nlink), 351 OCT); 352 arcn->sb.st_rdev = (dev_t)asc_ul(hd->c_rdev, sizeof(hd->c_rdev), OCT); 353 arcn->sb.st_mtime = (time_t)asc_ul(hd->c_mtime, sizeof(hd->c_mtime), 354 OCT); 355 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime; 356 # ifdef NET2_STAT 357 arcn->sb.st_size = (off_t)asc_ul(hd->c_filesize,sizeof(hd->c_filesize), 358 OCT); 359 # else 360 arcn->sb.st_size = (off_t)asc_uqd(hd->c_filesize,sizeof(hd->c_filesize), 361 OCT); 362 # endif 363 364 /* 365 * check name size and if valid, read in the name of this entry (name 366 * follows header in the archive) 367 */ 368 if ((nsz = (int)asc_ul(hd->c_namesize,sizeof(hd->c_namesize),OCT)) < 2) 369 return(-1); 370 arcn->nlen = nsz - 1; 371 if (rd_nm(arcn, nsz) < 0) 372 return(-1); 373 374 if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) { 375 /* 376 * no link name to read for this file 377 */ 378 arcn->ln_nlen = 0; 379 arcn->ln_name[0] = '\0'; 380 return(com_rd(arcn)); 381 } 382 383 /* 384 * check link name size and read in the link name. Link names are 385 * stored like file data. 386 */ 387 if (rd_ln_nm(arcn) < 0) 388 return(-1); 389 390 /* 391 * we have a valid header (with a link) 392 */ 393 return(com_rd(arcn)); 394 } 395 396 /* 397 * cpio_endrd() 398 * no cleanup needed here, just return size of the trailer (for append) 399 * Return: 400 * size of trailer header in this format 401 */ 402 403 #ifdef __STDC__ 404 off_t 405 cpio_endrd(void) 406 #else 407 off_t 408 cpio_endrd() 409 #endif 410 { 411 return((off_t)(sizeof(HD_CPIO) + sizeof(TRAILER))); 412 } 413 414 /* 415 * cpio_stwr() 416 * start up the device mapping table 417 * Return: 418 * 0 if ok, -1 otherwise (what dev_start() returns) 419 */ 420 421 #ifdef __STDC__ 422 int 423 cpio_stwr(void) 424 #else 425 int 426 cpio_stwr() 427 #endif 428 { 429 return(dev_start()); 430 } 431 432 /* 433 * cpio_wr() 434 * copy the data in the ARCHD to buffer in extended byte oriented cpio 435 * format. 436 * Return 437 * 0 if file has data to be written after the header, 1 if file has NO 438 * data to write after the header, -1 if archive write failed 439 */ 440 441 #ifdef __STDC__ 442 int 443 cpio_wr(register ARCHD *arcn) 444 #else 445 int 446 cpio_wr(arcn) 447 register ARCHD *arcn; 448 #endif 449 { 450 register HD_CPIO *hd; 451 register int nsz; 452 char hdblk[sizeof(HD_CPIO)]; 453 454 /* 455 * check and repair truncated device and inode fields in the header 456 */ 457 if (map_dev(arcn, (u_long)CPIO_MASK, (u_long)CPIO_MASK) < 0) 458 return(-1); 459 460 arcn->pad = 0L; 461 nsz = arcn->nlen + 1; 462 hd = (HD_CPIO *)hdblk; 463 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR)) 464 arcn->sb.st_rdev = 0; 465 466 switch(arcn->type) { 467 case PAX_CTG: 468 case PAX_REG: 469 case PAX_HRG: 470 /* 471 * set data size for file data 472 */ 473 # ifdef NET2_STAT 474 if (ul_asc((u_long)arcn->sb.st_size, hd->c_filesize, 475 sizeof(hd->c_filesize), OCT)) { 476 # else 477 if (uqd_asc((u_quad_t)arcn->sb.st_size, hd->c_filesize, 478 sizeof(hd->c_filesize), OCT)) { 479 # endif 480 paxwarn(1,"File is too large for cpio format %s", 481 arcn->org_name); 482 return(1); 483 } 484 break; 485 case PAX_SLK: 486 /* 487 * set data size to hold link name 488 */ 489 if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize, 490 sizeof(hd->c_filesize), OCT)) 491 goto out; 492 break; 493 default: 494 /* 495 * all other file types have no file data 496 */ 497 if (ul_asc((u_long)0, hd->c_filesize, sizeof(hd->c_filesize), 498 OCT)) 499 goto out; 500 break; 501 } 502 503 /* 504 * copy the values to the header using octal ascii 505 */ 506 if (ul_asc((u_long)MAGIC, hd->c_magic, sizeof(hd->c_magic), OCT) || 507 ul_asc((u_long)arcn->sb.st_dev, hd->c_dev, sizeof(hd->c_dev), 508 OCT) || 509 ul_asc((u_long)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino), 510 OCT) || 511 ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode), 512 OCT) || 513 ul_asc((u_long)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid), 514 OCT) || 515 ul_asc((u_long)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid), 516 OCT) || 517 ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink), 518 OCT) || 519 ul_asc((u_long)arcn->sb.st_rdev, hd->c_rdev, sizeof(hd->c_rdev), 520 OCT) || 521 ul_asc((u_long)arcn->sb.st_mtime,hd->c_mtime,sizeof(hd->c_mtime), 522 OCT) || 523 ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), OCT)) 524 goto out; 525 526 /* 527 * write the file name to the archive 528 */ 529 if ((wr_rdbuf(hdblk, (int)sizeof(HD_CPIO)) < 0) || 530 (wr_rdbuf(arcn->name, nsz) < 0)) { 531 paxwarn(1, "Unable to write cpio header for %s", arcn->org_name); 532 return(-1); 533 } 534 535 /* 536 * if this file has data, we are done. The caller will write the file 537 * data, if we are link tell caller we are done, go to next file 538 */ 539 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) || 540 (arcn->type == PAX_HRG)) 541 return(0); 542 if (arcn->type != PAX_SLK) 543 return(1); 544 545 /* 546 * write the link name to the archive, tell the caller to go to the 547 * next file as we are done. 548 */ 549 if (wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) { 550 paxwarn(1,"Unable to write cpio link name for %s",arcn->org_name); 551 return(-1); 552 } 553 return(1); 554 555 out: 556 /* 557 * header field is out of range 558 */ 559 paxwarn(1, "Cpio header field is too small to store file %s", 560 arcn->org_name); 561 return(1); 562 } 563 564 /* 565 * Routines common to the system VR4 version of cpio (with/without file CRC) 566 */ 567 568 /* 569 * vcpio_id() 570 * determine if a block given to us is a valid system VR4 cpio header 571 * WITHOUT crc. WATCH it the magic cookies are in OCTAL, the header 572 * uses HEX 573 * Return: 574 * 0 if a valid header, -1 otherwise 575 */ 576 577 #ifdef __STDC__ 578 int 579 vcpio_id(char *blk, int size) 580 #else 581 int 582 vcpio_id(blk, size) 583 char *blk; 584 int size; 585 #endif 586 { 587 if ((size < sizeof(HD_VCPIO)) || 588 (strncmp(blk, AVMAGIC, sizeof(AVMAGIC) - 1) != 0)) 589 return(-1); 590 return(0); 591 } 592 593 /* 594 * crc_id() 595 * determine if a block given to us is a valid system VR4 cpio header 596 * WITH crc. WATCH it the magic cookies are in OCTAL the header uses HEX 597 * Return: 598 * 0 if a valid header, -1 otherwise 599 */ 600 601 #ifdef __STDC__ 602 int 603 crc_id(char *blk, int size) 604 #else 605 int 606 crc_id(blk, size) 607 char *blk; 608 int size; 609 #endif 610 { 611 if ((size < sizeof(HD_VCPIO)) || 612 (strncmp(blk, AVCMAGIC, sizeof(AVCMAGIC) - 1) != 0)) 613 return(-1); 614 return(0); 615 } 616 617 /* 618 * crc_strd() 619 w set file data CRC calculations. Fire up the hard link detection code 620 * Return: 621 * 0 if ok -1 otherwise (the return values of lnk_start()) 622 */ 623 624 #ifdef __STDC__ 625 int 626 crc_strd(void) 627 #else 628 int 629 crc_strd() 630 #endif 631 { 632 docrc = 1; 633 return(lnk_start()); 634 } 635 636 /* 637 * vcpio_rd() 638 * determine if a buffer is a system VR4 archive entry. (with/without CRC) 639 * convert and store the values in the ARCHD parameter. 640 * Return: 641 * 0 if a valid header, -1 otherwise. 642 */ 643 644 #ifdef __STDC__ 645 int 646 vcpio_rd(register ARCHD *arcn, register char *buf) 647 #else 648 int 649 vcpio_rd(arcn, buf) 650 register ARCHD *arcn; 651 register char *buf; 652 #endif 653 { 654 register HD_VCPIO *hd; 655 dev_t devminor; 656 dev_t devmajor; 657 register int nsz; 658 659 /* 660 * during the id phase it was determined if we were using CRC, use the 661 * proper id routine. 662 */ 663 if (docrc) { 664 if (crc_id(buf, sizeof(HD_VCPIO)) < 0) 665 return(-1); 666 } else { 667 if (vcpio_id(buf, sizeof(HD_VCPIO)) < 0) 668 return(-1); 669 } 670 671 hd = (HD_VCPIO *)buf; 672 arcn->pad = 0L; 673 674 /* 675 * extract the hex ascii fields from the header 676 */ 677 arcn->sb.st_ino = (ino_t)asc_ul(hd->c_ino, sizeof(hd->c_ino), HEX); 678 arcn->sb.st_mode = (mode_t)asc_ul(hd->c_mode, sizeof(hd->c_mode), HEX); 679 arcn->sb.st_uid = (uid_t)asc_ul(hd->c_uid, sizeof(hd->c_uid), HEX); 680 arcn->sb.st_gid = (gid_t)asc_ul(hd->c_gid, sizeof(hd->c_gid), HEX); 681 arcn->sb.st_mtime = (time_t)asc_ul(hd->c_mtime,sizeof(hd->c_mtime),HEX); 682 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime; 683 # ifdef NET2_STAT 684 arcn->sb.st_size = (off_t)asc_ul(hd->c_filesize, 685 sizeof(hd->c_filesize), HEX); 686 # else 687 arcn->sb.st_size = (off_t)asc_uqd(hd->c_filesize, 688 sizeof(hd->c_filesize), HEX); 689 # endif 690 arcn->sb.st_nlink = (nlink_t)asc_ul(hd->c_nlink, sizeof(hd->c_nlink), 691 HEX); 692 devmajor = (dev_t)asc_ul(hd->c_maj, sizeof(hd->c_maj), HEX); 693 devminor = (dev_t)asc_ul(hd->c_min, sizeof(hd->c_min), HEX); 694 arcn->sb.st_dev = TODEV(devmajor, devminor); 695 devmajor = (dev_t)asc_ul(hd->c_rmaj, sizeof(hd->c_maj), HEX); 696 devminor = (dev_t)asc_ul(hd->c_rmin, sizeof(hd->c_min), HEX); 697 arcn->sb.st_rdev = TODEV(devmajor, devminor); 698 arcn->crc = asc_ul(hd->c_chksum, sizeof(hd->c_chksum), HEX); 699 700 /* 701 * check the length of the file name, if ok read it in, return -1 if 702 * bogus 703 */ 704 if ((nsz = (int)asc_ul(hd->c_namesize,sizeof(hd->c_namesize),HEX)) < 2) 705 return(-1); 706 arcn->nlen = nsz - 1; 707 if (rd_nm(arcn, nsz) < 0) 708 return(-1); 709 710 /* 711 * skip padding. header + filename is aligned to 4 byte boundries 712 */ 713 if (rd_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0) 714 return(-1); 715 716 /* 717 * if not a link (or a file with no data), calculate pad size (for 718 * padding which follows the file data), clear the link name and return 719 */ 720 if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) { 721 /* 722 * we have a valid header (not a link) 723 */ 724 arcn->ln_nlen = 0; 725 arcn->ln_name[0] = '\0'; 726 arcn->pad = VCPIO_PAD(arcn->sb.st_size); 727 return(com_rd(arcn)); 728 } 729 730 /* 731 * read in the link name and skip over the padding 732 */ 733 if ((rd_ln_nm(arcn) < 0) || 734 (rd_skip((off_t)(VCPIO_PAD(arcn->sb.st_size))) < 0)) 735 return(-1); 736 737 /* 738 * we have a valid header (with a link) 739 */ 740 return(com_rd(arcn)); 741 } 742 743 /* 744 * vcpio_endrd() 745 * no cleanup needed here, just return size of the trailer (for append) 746 * Return: 747 * size of trailer header in this format 748 */ 749 750 #ifdef __STDC__ 751 off_t 752 vcpio_endrd(void) 753 #else 754 off_t 755 vcpio_endrd() 756 #endif 757 { 758 return((off_t)(sizeof(HD_VCPIO) + sizeof(TRAILER) + 759 (VCPIO_PAD(sizeof(HD_VCPIO) + sizeof(TRAILER))))); 760 } 761 762 /* 763 * crc_stwr() 764 * start up the device mapping table, enable crc file calculation 765 * Return: 766 * 0 if ok, -1 otherwise (what dev_start() returns) 767 */ 768 769 #ifdef __STDC__ 770 int 771 crc_stwr(void) 772 #else 773 int 774 crc_stwr() 775 #endif 776 { 777 docrc = 1; 778 return(dev_start()); 779 } 780 781 /* 782 * vcpio_wr() 783 * copy the data in the ARCHD to buffer in system VR4 cpio 784 * (with/without crc) format. 785 * Return 786 * 0 if file has data to be written after the header, 1 if file has 787 * NO data to write after the header, -1 if archive write failed 788 */ 789 790 #ifdef __STDC__ 791 int 792 vcpio_wr(register ARCHD *arcn) 793 #else 794 int 795 vcpio_wr(arcn) 796 register ARCHD *arcn; 797 #endif 798 { 799 register HD_VCPIO *hd; 800 unsigned int nsz; 801 char hdblk[sizeof(HD_VCPIO)]; 802 803 /* 804 * check and repair truncated device and inode fields in the cpio 805 * header 806 */ 807 if (map_dev(arcn, (u_long)VCPIO_MASK, (u_long)VCPIO_MASK) < 0) 808 return(-1); 809 nsz = arcn->nlen + 1; 810 hd = (HD_VCPIO *)hdblk; 811 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR)) 812 arcn->sb.st_rdev = 0; 813 814 /* 815 * add the proper magic value depending whether we were asked for 816 * file data crc's, and the crc if needed. 817 */ 818 if (docrc) { 819 if (ul_asc((u_long)VCMAGIC, hd->c_magic, sizeof(hd->c_magic), 820 OCT) || 821 ul_asc((u_long)arcn->crc,hd->c_chksum,sizeof(hd->c_chksum), 822 HEX)) 823 goto out; 824 } else { 825 if (ul_asc((u_long)VMAGIC, hd->c_magic, sizeof(hd->c_magic), 826 OCT) || 827 ul_asc((u_long)0L, hd->c_chksum, sizeof(hd->c_chksum),HEX)) 828 goto out; 829 } 830 831 switch(arcn->type) { 832 case PAX_CTG: 833 case PAX_REG: 834 case PAX_HRG: 835 /* 836 * caller will copy file data to the archive. tell him how 837 * much to pad. 838 */ 839 arcn->pad = VCPIO_PAD(arcn->sb.st_size); 840 # ifdef NET2_STAT 841 if (ul_asc((u_long)arcn->sb.st_size, hd->c_filesize, 842 sizeof(hd->c_filesize), HEX)) { 843 # else 844 if (uqd_asc((u_quad_t)arcn->sb.st_size, hd->c_filesize, 845 sizeof(hd->c_filesize), HEX)) { 846 # endif 847 paxwarn(1,"File is too large for sv4cpio format %s", 848 arcn->org_name); 849 return(1); 850 } 851 break; 852 case PAX_SLK: 853 /* 854 * no file data for the caller to process, the file data has 855 * the size of the link 856 */ 857 arcn->pad = 0L; 858 if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize, 859 sizeof(hd->c_filesize), HEX)) 860 goto out; 861 break; 862 default: 863 /* 864 * no file data for the caller to process 865 */ 866 arcn->pad = 0L; 867 if (ul_asc((u_long)0L, hd->c_filesize, sizeof(hd->c_filesize), 868 HEX)) 869 goto out; 870 break; 871 } 872 873 /* 874 * set the other fields in the header 875 */ 876 if (ul_asc((u_long)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino), 877 HEX) || 878 ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode), 879 HEX) || 880 ul_asc((u_long)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid), 881 HEX) || 882 ul_asc((u_long)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid), 883 HEX) || 884 ul_asc((u_long)arcn->sb.st_mtime, hd->c_mtime, sizeof(hd->c_mtime), 885 HEX) || 886 ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink), 887 HEX) || 888 ul_asc((u_long)MAJOR(arcn->sb.st_dev),hd->c_maj, sizeof(hd->c_maj), 889 HEX) || 890 ul_asc((u_long)MINOR(arcn->sb.st_dev),hd->c_min, sizeof(hd->c_min), 891 HEX) || 892 ul_asc((u_long)MAJOR(arcn->sb.st_rdev),hd->c_rmaj,sizeof(hd->c_maj), 893 HEX) || 894 ul_asc((u_long)MINOR(arcn->sb.st_rdev),hd->c_rmin,sizeof(hd->c_min), 895 HEX) || 896 ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), HEX)) 897 goto out; 898 899 /* 900 * write the header, the file name and padding as required. 901 */ 902 if ((wr_rdbuf(hdblk, (int)sizeof(HD_VCPIO)) < 0) || 903 (wr_rdbuf(arcn->name, (int)nsz) < 0) || 904 (wr_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)) { 905 paxwarn(1,"Could not write sv4cpio header for %s",arcn->org_name); 906 return(-1); 907 } 908 909 /* 910 * if we have file data, tell the caller we are done, copy the file 911 */ 912 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) || 913 (arcn->type == PAX_HRG)) 914 return(0); 915 916 /* 917 * if we are not a link, tell the caller we are done, go to next file 918 */ 919 if (arcn->type != PAX_SLK) 920 return(1); 921 922 /* 923 * write the link name, tell the caller we are done. 924 */ 925 if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) || 926 (wr_skip((off_t)(VCPIO_PAD(arcn->ln_nlen))) < 0)) { 927 paxwarn(1,"Could not write sv4cpio link name for %s", 928 arcn->org_name); 929 return(-1); 930 } 931 return(1); 932 933 out: 934 /* 935 * header field is out of range 936 */ 937 paxwarn(1,"Sv4cpio header field is too small for file %s",arcn->org_name); 938 return(1); 939 } 940 941 /* 942 * Routines common to the old binary header cpio 943 */ 944 945 /* 946 * bcpio_id() 947 * determine if a block given to us is a old binary cpio header 948 * (with/without header byte swapping) 949 * Return: 950 * 0 if a valid header, -1 otherwise 951 */ 952 953 #ifdef __STDC__ 954 int 955 bcpio_id(char *blk, int size) 956 #else 957 int 958 bcpio_id(blk, size) 959 char *blk; 960 int size; 961 #endif 962 { 963 if (size < sizeof(HD_BCPIO)) 964 return(-1); 965 966 /* 967 * check both normal and byte swapped magic cookies 968 */ 969 if (((u_short)SHRT_EXT(blk)) == MAGIC) 970 return(0); 971 if (((u_short)RSHRT_EXT(blk)) == MAGIC) { 972 if (!swp_head) 973 ++swp_head; 974 return(0); 975 } 976 return(-1); 977 } 978 979 /* 980 * bcpio_rd() 981 * determine if a buffer is a old binary archive entry. (it may have byte 982 * swapped header) convert and store the values in the ARCHD parameter. 983 * This is a very old header format and should not really be used. 984 * Return: 985 * 0 if a valid header, -1 otherwise. 986 */ 987 988 #ifdef __STDC__ 989 int 990 bcpio_rd(register ARCHD *arcn, register char *buf) 991 #else 992 int 993 bcpio_rd(arcn, buf) 994 register ARCHD *arcn; 995 register char *buf; 996 #endif 997 { 998 register HD_BCPIO *hd; 999 register int nsz; 1000 1001 /* 1002 * check the header 1003 */ 1004 if (bcpio_id(buf, sizeof(HD_BCPIO)) < 0) 1005 return(-1); 1006 1007 arcn->pad = 0L; 1008 hd = (HD_BCPIO *)buf; 1009 if (swp_head) { 1010 /* 1011 * header has swapped bytes on 16 bit boundaries 1012 */ 1013 arcn->sb.st_dev = (dev_t)(RSHRT_EXT(hd->h_dev)); 1014 arcn->sb.st_ino = (ino_t)(RSHRT_EXT(hd->h_ino)); 1015 arcn->sb.st_mode = (mode_t)(RSHRT_EXT(hd->h_mode)); 1016 arcn->sb.st_uid = (uid_t)(RSHRT_EXT(hd->h_uid)); 1017 arcn->sb.st_gid = (gid_t)(RSHRT_EXT(hd->h_gid)); 1018 arcn->sb.st_nlink = (nlink_t)(RSHRT_EXT(hd->h_nlink)); 1019 arcn->sb.st_rdev = (dev_t)(RSHRT_EXT(hd->h_rdev)); 1020 arcn->sb.st_mtime = (time_t)(RSHRT_EXT(hd->h_mtime_1)); 1021 arcn->sb.st_mtime = (arcn->sb.st_mtime << 16) | 1022 ((time_t)(RSHRT_EXT(hd->h_mtime_2))); 1023 arcn->sb.st_size = (off_t)(RSHRT_EXT(hd->h_filesize_1)); 1024 arcn->sb.st_size = (arcn->sb.st_size << 16) | 1025 ((off_t)(RSHRT_EXT(hd->h_filesize_2))); 1026 nsz = (int)(RSHRT_EXT(hd->h_namesize)); 1027 } else { 1028 arcn->sb.st_dev = (dev_t)(SHRT_EXT(hd->h_dev)); 1029 arcn->sb.st_ino = (ino_t)(SHRT_EXT(hd->h_ino)); 1030 arcn->sb.st_mode = (mode_t)(SHRT_EXT(hd->h_mode)); 1031 arcn->sb.st_uid = (uid_t)(SHRT_EXT(hd->h_uid)); 1032 arcn->sb.st_gid = (gid_t)(SHRT_EXT(hd->h_gid)); 1033 arcn->sb.st_nlink = (nlink_t)(SHRT_EXT(hd->h_nlink)); 1034 arcn->sb.st_rdev = (dev_t)(SHRT_EXT(hd->h_rdev)); 1035 arcn->sb.st_mtime = (time_t)(SHRT_EXT(hd->h_mtime_1)); 1036 arcn->sb.st_mtime = (arcn->sb.st_mtime << 16) | 1037 ((time_t)(SHRT_EXT(hd->h_mtime_2))); 1038 arcn->sb.st_size = (off_t)(SHRT_EXT(hd->h_filesize_1)); 1039 arcn->sb.st_size = (arcn->sb.st_size << 16) | 1040 ((off_t)(SHRT_EXT(hd->h_filesize_2))); 1041 nsz = (int)(SHRT_EXT(hd->h_namesize)); 1042 } 1043 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime; 1044 1045 /* 1046 * check the file name size, if bogus give up. otherwise read the file 1047 * name 1048 */ 1049 if (nsz < 2) 1050 return(-1); 1051 arcn->nlen = nsz - 1; 1052 if (rd_nm(arcn, nsz) < 0) 1053 return(-1); 1054 1055 /* 1056 * header + file name are aligned to 2 byte boundries, skip if needed 1057 */ 1058 if (rd_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0) 1059 return(-1); 1060 1061 /* 1062 * if not a link (or a file with no data), calculate pad size (for 1063 * padding which follows the file data), clear the link name and return 1064 */ 1065 if (((arcn->sb.st_mode & C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)){ 1066 /* 1067 * we have a valid header (not a link) 1068 */ 1069 arcn->ln_nlen = 0; 1070 arcn->ln_name[0] = '\0'; 1071 arcn->pad = BCPIO_PAD(arcn->sb.st_size); 1072 return(com_rd(arcn)); 1073 } 1074 1075 if ((rd_ln_nm(arcn) < 0) || 1076 (rd_skip((off_t)(BCPIO_PAD(arcn->sb.st_size))) < 0)) 1077 return(-1); 1078 1079 /* 1080 * we have a valid header (with a link) 1081 */ 1082 return(com_rd(arcn)); 1083 } 1084 1085 /* 1086 * bcpio_endrd() 1087 * no cleanup needed here, just return size of the trailer (for append) 1088 * Return: 1089 * size of trailer header in this format 1090 */ 1091 1092 #ifdef __STDC__ 1093 off_t 1094 bcpio_endrd(void) 1095 #else 1096 off_t 1097 bcpio_endrd() 1098 #endif 1099 { 1100 return((off_t)(sizeof(HD_BCPIO) + sizeof(TRAILER) + 1101 (BCPIO_PAD(sizeof(HD_BCPIO) + sizeof(TRAILER))))); 1102 } 1103 1104 /* 1105 * bcpio_wr() 1106 * copy the data in the ARCHD to buffer in old binary cpio format 1107 * There is a real chance of field overflow with this critter. So we 1108 * always check the conversion is ok. nobody in his their right mind 1109 * should write an achive in this format... 1110 * Return 1111 * 0 if file has data to be written after the header, 1 if file has NO 1112 * data to write after the header, -1 if archive write failed 1113 */ 1114 1115 #ifdef __STDC__ 1116 int 1117 bcpio_wr(register ARCHD *arcn) 1118 #else 1119 int 1120 bcpio_wr(arcn) 1121 register ARCHD *arcn; 1122 #endif 1123 { 1124 register HD_BCPIO *hd; 1125 register int nsz; 1126 char hdblk[sizeof(HD_BCPIO)]; 1127 off_t t_offt; 1128 int t_int; 1129 time_t t_timet; 1130 1131 /* 1132 * check and repair truncated device and inode fields in the cpio 1133 * header 1134 */ 1135 if (map_dev(arcn, (u_long)BCPIO_MASK, (u_long)BCPIO_MASK) < 0) 1136 return(-1); 1137 1138 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR)) 1139 arcn->sb.st_rdev = 0; 1140 hd = (HD_BCPIO *)hdblk; 1141 1142 switch(arcn->type) { 1143 case PAX_CTG: 1144 case PAX_REG: 1145 case PAX_HRG: 1146 /* 1147 * caller will copy file data to the archive. tell him how 1148 * much to pad. 1149 */ 1150 arcn->pad = BCPIO_PAD(arcn->sb.st_size); 1151 hd->h_filesize_1[0] = CHR_WR_0(arcn->sb.st_size); 1152 hd->h_filesize_1[1] = CHR_WR_1(arcn->sb.st_size); 1153 hd->h_filesize_2[0] = CHR_WR_2(arcn->sb.st_size); 1154 hd->h_filesize_2[1] = CHR_WR_3(arcn->sb.st_size); 1155 t_offt = (off_t)(SHRT_EXT(hd->h_filesize_1)); 1156 t_offt = (t_offt<<16) | ((off_t)(SHRT_EXT(hd->h_filesize_2))); 1157 if (arcn->sb.st_size != t_offt) { 1158 paxwarn(1,"File is too large for bcpio format %s", 1159 arcn->org_name); 1160 return(1); 1161 } 1162 break; 1163 case PAX_SLK: 1164 /* 1165 * no file data for the caller to process, the file data has 1166 * the size of the link 1167 */ 1168 arcn->pad = 0L; 1169 hd->h_filesize_1[0] = CHR_WR_0(arcn->ln_nlen); 1170 hd->h_filesize_1[1] = CHR_WR_1(arcn->ln_nlen); 1171 hd->h_filesize_2[0] = CHR_WR_2(arcn->ln_nlen); 1172 hd->h_filesize_2[1] = CHR_WR_3(arcn->ln_nlen); 1173 t_int = (int)(SHRT_EXT(hd->h_filesize_1)); 1174 t_int = (t_int << 16) | ((int)(SHRT_EXT(hd->h_filesize_2))); 1175 if (arcn->ln_nlen != t_int) 1176 goto out; 1177 break; 1178 default: 1179 /* 1180 * no file data for the caller to process 1181 */ 1182 arcn->pad = 0L; 1183 hd->h_filesize_1[0] = (char)0; 1184 hd->h_filesize_1[1] = (char)0; 1185 hd->h_filesize_2[0] = (char)0; 1186 hd->h_filesize_2[1] = (char)0; 1187 break; 1188 } 1189 1190 /* 1191 * build up the rest of the fields 1192 */ 1193 hd->h_magic[0] = CHR_WR_2(MAGIC); 1194 hd->h_magic[1] = CHR_WR_3(MAGIC); 1195 hd->h_dev[0] = CHR_WR_2(arcn->sb.st_dev); 1196 hd->h_dev[1] = CHR_WR_3(arcn->sb.st_dev); 1197 if (arcn->sb.st_dev != (dev_t)(SHRT_EXT(hd->h_dev))) 1198 goto out; 1199 hd->h_ino[0] = CHR_WR_2(arcn->sb.st_ino); 1200 hd->h_ino[1] = CHR_WR_3(arcn->sb.st_ino); 1201 if (arcn->sb.st_ino != (ino_t)(SHRT_EXT(hd->h_ino))) 1202 goto out; 1203 hd->h_mode[0] = CHR_WR_2(arcn->sb.st_mode); 1204 hd->h_mode[1] = CHR_WR_3(arcn->sb.st_mode); 1205 if (arcn->sb.st_mode != (mode_t)(SHRT_EXT(hd->h_mode))) 1206 goto out; 1207 hd->h_uid[0] = CHR_WR_2(arcn->sb.st_uid); 1208 hd->h_uid[1] = CHR_WR_3(arcn->sb.st_uid); 1209 if (arcn->sb.st_uid != (uid_t)(SHRT_EXT(hd->h_uid))) 1210 goto out; 1211 hd->h_gid[0] = CHR_WR_2(arcn->sb.st_gid); 1212 hd->h_gid[1] = CHR_WR_3(arcn->sb.st_gid); 1213 if (arcn->sb.st_gid != (gid_t)(SHRT_EXT(hd->h_gid))) 1214 goto out; 1215 hd->h_nlink[0] = CHR_WR_2(arcn->sb.st_nlink); 1216 hd->h_nlink[1] = CHR_WR_3(arcn->sb.st_nlink); 1217 if (arcn->sb.st_nlink != (nlink_t)(SHRT_EXT(hd->h_nlink))) 1218 goto out; 1219 hd->h_rdev[0] = CHR_WR_2(arcn->sb.st_rdev); 1220 hd->h_rdev[1] = CHR_WR_3(arcn->sb.st_rdev); 1221 if (arcn->sb.st_rdev != (dev_t)(SHRT_EXT(hd->h_rdev))) 1222 goto out; 1223 hd->h_mtime_1[0] = CHR_WR_0(arcn->sb.st_mtime); 1224 hd->h_mtime_1[1] = CHR_WR_1(arcn->sb.st_mtime); 1225 hd->h_mtime_2[0] = CHR_WR_2(arcn->sb.st_mtime); 1226 hd->h_mtime_2[1] = CHR_WR_3(arcn->sb.st_mtime); 1227 t_timet = (time_t)(SHRT_EXT(hd->h_mtime_1)); 1228 t_timet = (t_timet << 16) | ((time_t)(SHRT_EXT(hd->h_mtime_2))); 1229 if (arcn->sb.st_mtime != t_timet) 1230 goto out; 1231 nsz = arcn->nlen + 1; 1232 hd->h_namesize[0] = CHR_WR_2(nsz); 1233 hd->h_namesize[1] = CHR_WR_3(nsz); 1234 if (nsz != (int)(SHRT_EXT(hd->h_namesize))) 1235 goto out; 1236 1237 /* 1238 * write the header, the file name and padding as required. 1239 */ 1240 if ((wr_rdbuf(hdblk, (int)sizeof(HD_BCPIO)) < 0) || 1241 (wr_rdbuf(arcn->name, nsz) < 0) || 1242 (wr_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)) { 1243 paxwarn(1, "Could not write bcpio header for %s", arcn->org_name); 1244 return(-1); 1245 } 1246 1247 /* 1248 * if we have file data, tell the caller we are done 1249 */ 1250 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) || 1251 (arcn->type == PAX_HRG)) 1252 return(0); 1253 1254 /* 1255 * if we are not a link, tell the caller we are done, go to next file 1256 */ 1257 if (arcn->type != PAX_SLK) 1258 return(1); 1259 1260 /* 1261 * write the link name, tell the caller we are done. 1262 */ 1263 if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) || 1264 (wr_skip((off_t)(BCPIO_PAD(arcn->ln_nlen))) < 0)) { 1265 paxwarn(1,"Could not write bcpio link name for %s",arcn->org_name); 1266 return(-1); 1267 } 1268 return(1); 1269 1270 out: 1271 /* 1272 * header field is out of range 1273 */ 1274 paxwarn(1,"Bcpio header field is too small for file %s", arcn->org_name); 1275 return(1); 1276 } 1277