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