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