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