tar.c revision 1.15
1/*	$NetBSD: tar.c,v 1.15 1999/08/18 17:46:28 kleink 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.15 1999/08/18 17:46:28 kleink 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	case DIRTYPE:	/* see below */
518	default:
519		/*
520		 * If we have a trailing / this is a directory and NOT a file.
521		 * Note: V7 tar doesn't actually have DIRTYPE, but it was
522		 * reported that V7 archives using USTAR directories do exist.
523		 */
524		arcn->ln_name[0] = '\0';
525		arcn->ln_nlen = 0;
526		if (*pt == '/' || hd->linkflag == DIRTYPE) {
527			/*
528			 * it is a directory, set the mode for -v printing
529			 */
530			arcn->type = PAX_DIR;
531			arcn->sb.st_mode |= S_IFDIR;
532			arcn->sb.st_nlink = 2;
533		} else {
534			/*
535			 * have a file that will be followed by data. Set the
536			 * skip value to the size field and caluculate the size
537			 * of the padding.
538			 */
539			arcn->type = PAX_REG;
540			arcn->sb.st_mode |= S_IFREG;
541			arcn->pad = TAR_PAD(arcn->sb.st_size);
542			arcn->skip = arcn->sb.st_size;
543		}
544		break;
545	}
546
547	/*
548	 * strip off any trailing slash.
549	 */
550	if (*pt == '/') {
551		*pt = '\0';
552		--arcn->nlen;
553	}
554	return(0);
555}
556
557/*
558 * tar_wr()
559 *	write a tar header for the file specified in the ARCHD to the archive.
560 *	Have to check for file types that cannot be stored and file names that
561 *	are too long. Be careful of the term (last arg) to ul_oct, each field
562 *	of tar has it own spec for the termination character(s).
563 *	ASSUMED: space after header in header block is zero filled
564 * Return:
565 *	0 if file has data to be written after the header, 1 if file has NO
566 *	data to write after the header, -1 if archive write failed
567 */
568
569#if __STDC__
570int
571tar_wr(ARCHD *arcn)
572#else
573int
574tar_wr(arcn)
575	ARCHD *arcn;
576#endif
577{
578	HD_TAR *hd;
579	int len;
580	char hdblk[sizeof(HD_TAR)];
581
582	/*
583	 * check for those file system types which tar cannot store
584	 */
585	switch(arcn->type) {
586	case PAX_DIR:
587		/*
588		 * user asked that dirs not be written to the archive
589		 */
590		if (tar_nodir)
591			return(1);
592		break;
593	case PAX_CHR:
594		tty_warn(1, "Tar cannot archive a character device %s",
595		    arcn->org_name);
596		return(1);
597	case PAX_BLK:
598		tty_warn(1,
599		    "Tar cannot archive a block device %s", arcn->org_name);
600		return(1);
601	case PAX_SCK:
602		tty_warn(1, "Tar cannot archive a socket %s", arcn->org_name);
603		return(1);
604	case PAX_FIF:
605		tty_warn(1, "Tar cannot archive a fifo %s", arcn->org_name);
606		return(1);
607	case PAX_SLK:
608	case PAX_HLK:
609	case PAX_HRG:
610		if (arcn->ln_nlen > sizeof(hd->linkname)) {
611			tty_warn(1,"Link name too long for tar %s",
612			    arcn->ln_name);
613			return(1);
614		}
615		break;
616	case PAX_REG:
617	case PAX_CTG:
618	default:
619		break;
620	}
621
622	/*
623	 * check file name len, remember extra char for dirs (the / at the end)
624	 */
625	len = arcn->nlen;
626	if (arcn->type == PAX_DIR)
627		++len;
628	if (len > sizeof(hd->name)) {
629		tty_warn(1, "File name too long for tar %s", arcn->name);
630		return(1);
631	}
632
633	/*
634	 * copy the data out of the ARCHD into the tar header based on the type
635	 * of the file. Remember many tar readers want the unused fields to be
636	 * padded with zero. We set the linkflag field (type), the linkname
637	 * (or zero if not used),the size, and set the padding (if any) to be
638	 * added after the file data (0 for all other types, as they only have
639	 * a header)
640	 */
641	hd = (HD_TAR *)hdblk;
642	zf_strncpy(hd->name, arcn->name, sizeof(hd->name));
643	arcn->pad = 0;
644
645	if (arcn->type == PAX_DIR) {
646		/*
647		 * directories are the same as files, except have a filename
648		 * that ends with a /, we add the slash here. No data follows,
649		 * dirs, so no pad.
650		 */
651		hd->linkflag = AREGTYPE;
652		memset(hd->linkname, 0, sizeof(hd->linkname));
653		hd->name[len-1] = '/';
654		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
655			goto out;
656	} else if (arcn->type == PAX_SLK) {
657		/*
658		 * no data follows this file, so no pad
659		 */
660		hd->linkflag = SYMTYPE;
661		zf_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname));
662		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
663			goto out;
664	} else if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) {
665		/*
666		 * no data follows this file, so no pad
667		 */
668		hd->linkflag = LNKTYPE;
669		zf_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname));
670		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
671			goto out;
672	} else {
673		/*
674		 * data follows this file, so set the pad
675		 */
676		hd->linkflag = AREGTYPE;
677		memset(hd->linkname, 0, sizeof(hd->linkname));
678#		ifdef NET2_STAT
679		if (ul_oct((u_long)arcn->sb.st_size, hd->size,
680		    sizeof(hd->size), 1)) {
681#		else
682		if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size,
683		    sizeof(hd->size), 1)) {
684#		endif
685			tty_warn(1,"File is too large for tar %s",
686			    arcn->org_name);
687			return(1);
688		}
689		arcn->pad = TAR_PAD(arcn->sb.st_size);
690	}
691
692	/*
693	 * copy those fields that are independent of the type
694	 */
695	if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 0) ||
696	    ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 0) ||
697	    ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 0) ||
698	    ul_oct((u_long)arcn->sb.st_mtime, hd->mtime, sizeof(hd->mtime), 1))
699		goto out;
700
701	/*
702	 * calculate and add the checksum, then write the header. A return of
703	 * 0 tells the caller to now write the file data, 1 says no data needs
704	 * to be written
705	 */
706	if (ul_oct(tar_chksm(hdblk, sizeof(HD_TAR)), hd->chksum,
707	    sizeof(hd->chksum), 2))
708		goto out;
709	if (wr_rdbuf(hdblk, sizeof(HD_TAR)) < 0)
710		return(-1);
711	if (wr_skip((off_t)(BLKMULT - sizeof(HD_TAR))) < 0)
712		return(-1);
713	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
714		return(0);
715	return(1);
716
717    out:
718	/*
719	 * header field is out of range
720	 */
721	tty_warn(1, "Tar header field is too small for %s", arcn->org_name);
722	return(1);
723}
724
725/*
726 * Routines for POSIX ustar
727 */
728
729/*
730 * ustar_strd()
731 *	initialization for ustar read
732 * Return:
733 *	0 if ok, -1 otherwise
734 */
735
736#if __STDC__
737int
738ustar_strd(void)
739#else
740int
741ustar_strd()
742#endif
743{
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#if __STDC__
755int
756ustar_stwr(void)
757#else
758int
759ustar_stwr()
760#endif
761{
762	return(0);
763}
764
765/*
766 * ustar_id()
767 *	determine if a block given to us is a valid ustar header. We have to
768 *	be on the lookout for those pesky blocks of all zero's
769 * Return:
770 *	0 if a ustar header, -1 otherwise
771 */
772
773#if __STDC__
774int
775ustar_id(char *blk, int size)
776#else
777int
778ustar_id(blk, size)
779	char *blk;
780	int size;
781#endif
782{
783	HD_USTAR *hd;
784
785	if (size < BLKMULT)
786		return(-1);
787	hd = (HD_USTAR *)blk;
788
789	/*
790	 * check for block of zero's first, a simple and fast test then check
791	 * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
792	 * programs are fouled up and create archives missing the \0. Last we
793	 * check the checksum. If ok we have to assume it is a valid header.
794	 */
795	if (hd->name[0] == '\0')
796		return(-1);
797	if (strncmp(hd->magic, TMAGIC, TMAGLEN - 1) != 0)
798		return(-1);
799	if (!strncmp(hd->magic, "ustar  ", 8))
800		is_oldgnutar = 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#if __STDC__
815int
816ustar_rd(ARCHD *arcn, char *buf)
817#else
818int
819ustar_rd(arcn, buf)
820	ARCHD *arcn;
821	char *buf;
822#endif
823{
824	HD_USTAR *hd;
825	char *dest;
826	int cnt;
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	hd = (HD_USTAR *)buf;
839
840	/*
841	 * see if the filename is split into two parts. if, so joint the parts.
842	 * we copy the prefix first and add a / between the prefix and name.
843	 */
844	dest = arcn->name;
845	if (*(hd->prefix) != '\0') {
846		cnt = l_strncpy(arcn->name, hd->prefix, sizeof(hd->prefix));
847		dest += cnt;
848		*dest++ = '/';
849	}
850	cnt = l_strncpy(dest, hd->name, sizeof(hd->name));
851	dest += cnt;
852	*dest = '\0';
853	arcn->nlen = dest - arcn->name;
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	arcn->sb.st_size = (size_t)asc_ul(hd->size, sizeof(hd->size), OCT);
862	arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT);
863	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
864
865	/*
866	 * If we can find the ascii names for gname and uname in the password
867	 * and group files we will use the uid's and gid they bind. Otherwise
868	 * we use the uid and gid values stored in the header. (This is what
869	 * the posix spec wants).
870	 */
871	hd->gname[sizeof(hd->gname) - 1] = '\0';
872	if (gid_from_group(hd->gname, &(arcn->sb.st_gid)) < 0)
873		arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
874	hd->uname[sizeof(hd->uname) - 1] = '\0';
875	if (uid_from_user(hd->uname, &(arcn->sb.st_uid)) < 0)
876		arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
877
878	/*
879	 * set the defaults, these may be changed depending on the file type
880	 */
881	arcn->ln_name[0] = '\0';
882	arcn->ln_nlen = 0;
883	arcn->pad = 0;
884	arcn->skip = 0;
885	arcn->sb.st_rdev = (dev_t)0;
886
887	/*
888	 * set the mode and PAX type according to the typeflag in the header
889	 */
890	switch(hd->typeflag) {
891	case FIFOTYPE:
892		arcn->type = PAX_FIF;
893		arcn->sb.st_mode |= S_IFIFO;
894		break;
895	case DIRTYPE:
896		arcn->type = PAX_DIR;
897		arcn->sb.st_mode |= S_IFDIR;
898		arcn->sb.st_nlink = 2;
899
900		/*
901		 * Some programs that create ustar archives append a '/'
902		 * to the pathname for directories. This clearly violates
903		 * ustar specs, but we will silently strip it off anyway.
904		 */
905		if (arcn->name[arcn->nlen - 1] == '/')
906			arcn->name[--arcn->nlen] = '\0';
907		break;
908	case BLKTYPE:
909	case CHRTYPE:
910		/*
911		 * this type requires the rdev field to be set.
912		 */
913		if (hd->typeflag == BLKTYPE) {
914			arcn->type = PAX_BLK;
915			arcn->sb.st_mode |= S_IFBLK;
916		} else {
917			arcn->type = PAX_CHR;
918			arcn->sb.st_mode |= S_IFCHR;
919		}
920		devmajor = (dev_t)asc_ul(hd->devmajor,sizeof(hd->devmajor),OCT);
921		devminor = (dev_t)asc_ul(hd->devminor,sizeof(hd->devminor),OCT);
922		arcn->sb.st_rdev = TODEV(devmajor, devminor);
923		break;
924	case SYMTYPE:
925	case LNKTYPE:
926		if (hd->typeflag == SYMTYPE) {
927			arcn->type = PAX_SLK;
928			arcn->sb.st_mode |= S_IFLNK;
929		} else {
930			arcn->type = PAX_HLK;
931			/*
932			 * so printing looks better
933			 */
934			arcn->sb.st_mode |= S_IFREG;
935			arcn->sb.st_nlink = 2;
936		}
937		/*
938		 * copy the link name
939		 */
940		arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname,
941			sizeof(hd->linkname));
942		arcn->ln_name[arcn->ln_nlen] = '\0';
943		break;
944	case CONTTYPE:
945	case AREGTYPE:
946	case REGTYPE:
947	default:
948		/*
949		 * these types have file data that follows. Set the skip and
950		 * pad fields.
951		 */
952		arcn->type = PAX_REG;
953		arcn->pad = TAR_PAD(arcn->sb.st_size);
954		arcn->skip = arcn->sb.st_size;
955		arcn->sb.st_mode |= S_IFREG;
956		break;
957	}
958	return(0);
959}
960
961/*
962 * ustar_wr()
963 *	write a ustar header for the file specified in the ARCHD to the archive
964 *	Have to check for file types that cannot be stored and file names that
965 *	are too long. Be careful of the term (last arg) to ul_oct, we only use
966 *	'\0' for the termination character (this is different than picky tar)
967 *	ASSUMED: space after header in header block is zero filled
968 * Return:
969 *	0 if file has data to be written after the header, 1 if file has NO
970 *	data to write after the header, -1 if archive write failed
971 */
972
973#if __STDC__
974int
975ustar_wr(ARCHD *arcn)
976#else
977int
978ustar_wr(arcn)
979	ARCHD *arcn;
980#endif
981{
982	HD_USTAR *hd;
983	char *pt;
984	char hdblk[sizeof(HD_USTAR)];
985	const char *user, *group;
986
987	/*
988	 * check for those file system types ustar cannot store
989	 */
990	if (arcn->type == PAX_SCK) {
991		tty_warn(1, "Ustar cannot archive a socket %s", arcn->org_name);
992		return(1);
993	}
994
995	/*
996	 * check the length of the linkname
997	 */
998	if (((arcn->type == PAX_SLK) || (arcn->type == PAX_HLK) ||
999	    (arcn->type == PAX_HRG)) && (arcn->ln_nlen > sizeof(hd->linkname))){
1000		tty_warn(1, "Link name too long for ustar %s", arcn->ln_name);
1001		return(1);
1002	}
1003
1004	/*
1005	 * split the path name into prefix and name fields (if needed). if
1006	 * pt != arcn->name, the name has to be split
1007	 */
1008	if ((pt = name_split(arcn->name, arcn->nlen)) == NULL) {
1009		tty_warn(1, "File name too long for ustar %s", arcn->name);
1010		return(1);
1011	}
1012	hd = (HD_USTAR *)hdblk;
1013	arcn->pad = 0L;
1014
1015	/*
1016	 * split the name, or zero out the prefix
1017	 */
1018	if (pt != arcn->name) {
1019		/*
1020		 * name was split, pt points at the / where the split is to
1021		 * occur, we remove the / and copy the first part to the prefix
1022		 */
1023		*pt = '\0';
1024		zf_strncpy(hd->prefix, arcn->name, sizeof(hd->prefix));
1025		*pt++ = '/';
1026	} else
1027		memset(hd->prefix, 0, sizeof(hd->prefix));
1028
1029	/*
1030	 * copy the name part. this may be the whole path or the part after
1031	 * the prefix
1032	 */
1033	zf_strncpy(hd->name, pt, sizeof(hd->name));
1034
1035	/*
1036	 * set the fields in the header that are type dependent
1037	 */
1038	switch(arcn->type) {
1039	case PAX_DIR:
1040		hd->typeflag = DIRTYPE;
1041		memset(hd->linkname, 0, sizeof(hd->linkname));
1042		memset(hd->devmajor, 0, sizeof(hd->devmajor));
1043		memset(hd->devminor, 0, sizeof(hd->devminor));
1044		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
1045			goto out;
1046		break;
1047	case PAX_CHR:
1048	case PAX_BLK:
1049		if (arcn->type == PAX_CHR)
1050			hd->typeflag = CHRTYPE;
1051		else
1052			hd->typeflag = BLKTYPE;
1053		memset(hd->linkname, 0, sizeof(hd->linkname));
1054		if (ul_oct((u_long)MAJOR(arcn->sb.st_rdev), hd->devmajor,
1055		   sizeof(hd->devmajor), 3) ||
1056		   ul_oct((u_long)MINOR(arcn->sb.st_rdev), hd->devminor,
1057		   sizeof(hd->devminor), 3) ||
1058		   ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
1059			goto out;
1060		break;
1061	case PAX_FIF:
1062		hd->typeflag = FIFOTYPE;
1063		memset(hd->linkname, 0, sizeof(hd->linkname));
1064		memset(hd->devmajor, 0, sizeof(hd->devmajor));
1065		memset(hd->devminor, 0, sizeof(hd->devminor));
1066		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
1067			goto out;
1068		break;
1069	case PAX_SLK:
1070	case PAX_HLK:
1071	case PAX_HRG:
1072		if (arcn->type == PAX_SLK)
1073			hd->typeflag = SYMTYPE;
1074		else
1075			hd->typeflag = LNKTYPE;
1076		zf_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname));
1077		memset(hd->devmajor, 0, sizeof(hd->devmajor));
1078		memset(hd->devminor, 0, sizeof(hd->devminor));
1079		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
1080			goto out;
1081		break;
1082	case PAX_REG:
1083	case PAX_CTG:
1084	default:
1085		/*
1086		 * file data with this type, set the padding
1087		 */
1088		if (arcn->type == PAX_CTG)
1089			hd->typeflag = CONTTYPE;
1090		else
1091			hd->typeflag = REGTYPE;
1092		memset(hd->linkname, 0, sizeof(hd->linkname));
1093		memset(hd->devmajor, 0, sizeof(hd->devmajor));
1094		memset(hd->devminor, 0, sizeof(hd->devminor));
1095		arcn->pad = TAR_PAD(arcn->sb.st_size);
1096#		ifdef NET2_STAT
1097		if (ul_oct((u_long)arcn->sb.st_size, hd->size,
1098		    sizeof(hd->size), 3)) {
1099#		else
1100		if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size,
1101		    sizeof(hd->size), 3)) {
1102#		endif
1103			tty_warn(1,"File is too long for ustar %s",
1104			    arcn->org_name);
1105			return(1);
1106		}
1107		break;
1108	}
1109
1110	zf_strncpy(hd->magic, TMAGIC, TMAGLEN);
1111	zf_strncpy(hd->version, TVERSION, TVERSLEN);
1112
1113	/*
1114	 * set the remaining fields. Some versions want all 16 bits of mode
1115	 * we better humor them (they really do not meet spec though)....
1116	 */
1117	if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3) ||
1118	    ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 3)  ||
1119	    ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 3) ||
1120	    ul_oct((u_long)arcn->sb.st_mtime,hd->mtime,sizeof(hd->mtime),3))
1121		goto out;
1122	user = user_from_uid(arcn->sb.st_uid, 1);
1123	group = group_from_gid(arcn->sb.st_gid, 1);
1124	zf_strncpy(hd->uname, user ? user : "", sizeof(hd->uname));
1125	zf_strncpy(hd->gname, group ? group : "", sizeof(hd->gname));
1126
1127	/*
1128	 * calculate and store the checksum write the header to the archive
1129	 * return 0 tells the caller to now write the file data, 1 says no data
1130	 * needs to be written
1131	 */
1132	if (ul_oct(tar_chksm(hdblk, sizeof(HD_USTAR)), hd->chksum,
1133	   sizeof(hd->chksum), 3))
1134		goto out;
1135	if (wr_rdbuf(hdblk, sizeof(HD_USTAR)) < 0)
1136		return(-1);
1137	if (wr_skip((off_t)(BLKMULT - sizeof(HD_USTAR))) < 0)
1138		return(-1);
1139	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
1140		return(0);
1141	return(1);
1142
1143    out:
1144    	/*
1145	 * header field is out of range
1146	 */
1147	tty_warn(1, "Ustar header field is too small for %s", arcn->org_name);
1148	return(1);
1149}
1150
1151/*
1152 * name_split()
1153 *	see if the name has to be split for storage in a ustar header. We try
1154 *	to fit the entire name in the name field without splitting if we can.
1155 *	The split point is always at a /
1156 * Return
1157 *	character pointer to split point (always the / that is to be removed
1158 *	if the split is not needed, the points is set to the start of the file
1159 *	name (it would violate the spec to split there). A NULL is returned if
1160 *	the file name is too long
1161 */
1162
1163#if __STDC__
1164static char *
1165name_split(char *name, int len)
1166#else
1167static char *
1168name_split(name, len)
1169	char *name;
1170	int len;
1171#endif
1172{
1173	char *start;
1174
1175	/*
1176	 * check to see if the file name is small enough to fit in the name
1177	 * field. if so just return a pointer to the name.
1178	 */
1179	if (len <= TNMSZ)
1180		return(name);
1181	if (len > (TPFSZ + TNMSZ + 1))
1182		return(NULL);
1183
1184	/*
1185	 * we start looking at the biggest sized piece that fits in the name
1186	 * field. We walk foward looking for a slash to split at. The idea is
1187	 * to find the biggest piece to fit in the name field (or the smallest
1188	 * prefix we can find) (the -1 is correct the biggest piece would
1189	 * include the slash between the two parts that gets thrown away)
1190	 */
1191	start = name + len - TNMSZ - 1;
1192	while ((*start != '\0') && (*start != '/'))
1193		++start;
1194
1195	/*
1196	 * if we hit the end of the string, this name cannot be split, so we
1197	 * cannot store this file.
1198	 */
1199	if (*start == '\0')
1200		return(NULL);
1201	len = start - name;
1202
1203	/*
1204	 * NOTE: /str where the length of str == TNMSZ can not be stored under
1205	 * the p1003.1-1990 spec for ustar. We could force a prefix of / and
1206	 * the file would then expand on extract to //str. The len == 0 below
1207	 * makes this special case follow the spec to the letter.
1208	 */
1209	if ((len > TPFSZ) || (len == 0))
1210		return(NULL);
1211
1212	/*
1213	 * ok have a split point, return it to the caller
1214	 */
1215	return(start);
1216}
1217
1218/*
1219 * deal with GNU tar -X switch.  basically, we go through each line of
1220 * the file, building a string from the "glob" lines in the file into
1221 * RE lines, of the form `/^RE$//', which we pass to rep_add(), which
1222 * will add a empty replacement (exclusion), for the named files.
1223 */
1224int
1225tar_gnutar_X_compat(path)
1226	const char *path;
1227{
1228	char *line, sbuf[MAXPATHLEN * 2 + 1 + 5];
1229	FILE *fp;
1230	int lineno = 0, i, j;
1231	size_t len;
1232
1233	fp = fopen(path, "r");
1234	if (fp == NULL) {
1235		tty_warn(1, "can not open %s: %s", path,
1236		    strerror(errno));
1237		return(-1);
1238	}
1239
1240	while ((line = fgetln(fp, &len))) {
1241		lineno++;
1242		if (len > MAXPATHLEN) {
1243			tty_warn(0, "pathname too long, line %d of %s",
1244			    lineno, path);
1245		}
1246		if (line[len - 1] == '\n')
1247			len--;
1248		for (i = 0, j = 2; i < len; i++) {
1249			/*
1250			 * convert glob to regexp, escaping everything
1251			 */
1252			if (line[i] == '*')
1253				sbuf[j++] = '.';
1254			else if (line[i] == '?')
1255				line[i] = '.';
1256			else if (!isalnum(line[i]) && !isblank(line[i]))
1257				sbuf[j++] = '\\';
1258			sbuf[j++] = line[i];
1259		}
1260		sbuf[0] = sbuf[j + 1] = sbuf[j + 2] = '/';
1261		sbuf[1] = '^';
1262		sbuf[j] = '$';
1263		sbuf[j + 3] = '\0';
1264		if (rep_add(sbuf) < 0)
1265			return (-1);
1266	}
1267	return (0);
1268}
1269