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