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