Debian Codegraph @

cpio.c

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