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