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