xref: /openbsd/bin/pax/tar.c (revision 17df1aa7)
1 /*	$OpenBSD: tar.c,v 1.42 2009/10/27 23:59:22 deraadt Exp $	*/
2 /*	$NetBSD: tar.c,v 1.5 1995/03/21 09:07:49 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1992 Keith Muller.
6  * Copyright (c) 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Keith Muller of the University of California, San Diego.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/types.h>
38 #include <sys/time.h>
39 #include <sys/stat.h>
40 #include <sys/param.h>
41 #include <string.h>
42 #include <stdio.h>
43 #include <unistd.h>
44 #include <stdlib.h>
45 #include "pax.h"
46 #include "extern.h"
47 #include "tar.h"
48 
49 /*
50  * Routines for reading, writing and header identify of various versions of tar
51  */
52 
53 static size_t expandname(char *, size_t, char **, const char *, size_t);
54 static u_long tar_chksm(char *, int);
55 static char *name_split(char *, int);
56 static int ul_oct(u_long, char *, int, int);
57 #ifndef LONG_OFF_T
58 static int uqd_oct(u_quad_t, char *, int, int);
59 #endif
60 
61 static uid_t uid_nobody;
62 static uid_t uid_warn;
63 static gid_t gid_nobody;
64 static gid_t gid_warn;
65 
66 /*
67  * Routines common to all versions of tar
68  */
69 
70 static int tar_nodir;			/* do not write dirs under old tar */
71 char *gnu_name_string;			/* GNU ././@LongLink hackery name */
72 char *gnu_link_string;			/* GNU ././@LongLink hackery link */
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(ARCHD *ignore, 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 LONG_OFF_T
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 	force_one_volume = 1;
329 	return(0);
330 }
331 
332 /*
333  * tar_opt()
334  *	handle tar format specific -o options
335  * Return:
336  *	0 if ok -1 otherwise
337  */
338 
339 int
340 tar_opt(void)
341 {
342 	OPLIST *opt;
343 
344 	while ((opt = opt_next()) != NULL) {
345 		if (strcmp(opt->name, TAR_OPTION) ||
346 		    strcmp(opt->value, TAR_NODIR)) {
347 			paxwarn(1, "Unknown tar format -o option/value pair %s=%s",
348 			    opt->name, opt->value);
349 			paxwarn(1,"%s=%s is the only supported tar format option",
350 			    TAR_OPTION, TAR_NODIR);
351 			return(-1);
352 		}
353 
354 		/*
355 		 * we only support one option, and only when writing
356 		 */
357 		if ((act != APPND) && (act != ARCHIVE)) {
358 			paxwarn(1, "%s=%s is only supported when writing.",
359 			    opt->name, opt->value);
360 			return(-1);
361 		}
362 		tar_nodir = 1;
363 	}
364 	return(0);
365 }
366 
367 
368 /*
369  * tar_rd()
370  *	extract the values out of block already determined to be a tar header.
371  *	store the values in the ARCHD parameter.
372  * Return:
373  *	0
374  */
375 
376 int
377 tar_rd(ARCHD *arcn, char *buf)
378 {
379 	HD_TAR *hd;
380 	char *pt;
381 
382 	/*
383 	 * we only get proper sized buffers passed to us
384 	 */
385 	if (tar_id(buf, BLKMULT) < 0)
386 		return(-1);
387 	memset(arcn, 0, sizeof(*arcn));
388 	arcn->org_name = arcn->name;
389 	arcn->sb.st_nlink = 1;
390 
391 	/*
392 	 * copy out the name and values in the stat buffer
393 	 */
394 	hd = (HD_TAR *)buf;
395 	if (hd->linkflag != LONGLINKTYPE && hd->linkflag != LONGNAMETYPE) {
396 		arcn->nlen = expandname(arcn->name, sizeof(arcn->name),
397 		    &gnu_name_string, hd->name, sizeof(hd->name));
398 		arcn->ln_nlen = expandname(arcn->ln_name, sizeof(arcn->ln_name),
399 		    &gnu_link_string, hd->linkname, sizeof(hd->linkname));
400 	}
401 	arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode,sizeof(hd->mode),OCT) &
402 	    0xfff);
403 	arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
404 	arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
405 #ifdef LONG_OFF_T
406 	arcn->sb.st_size = (off_t)asc_ul(hd->size, sizeof(hd->size), OCT);
407 #else
408 	arcn->sb.st_size = (off_t)asc_uqd(hd->size, sizeof(hd->size), OCT);
409 #endif
410 	arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT);
411 	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
412 
413 	/*
414 	 * have to look at the last character, it may be a '/' and that is used
415 	 * to encode this as a directory
416 	 */
417 	pt = &(arcn->name[arcn->nlen - 1]);
418 	arcn->pad = 0;
419 	arcn->skip = 0;
420 	switch (hd->linkflag) {
421 	case SYMTYPE:
422 		/*
423 		 * symbolic link, need to get the link name and set the type in
424 		 * the st_mode so -v printing will look correct.
425 		 */
426 		arcn->type = PAX_SLK;
427 		arcn->sb.st_mode |= S_IFLNK;
428 		break;
429 	case LNKTYPE:
430 		/*
431 		 * hard link, need to get the link name, set the type in the
432 		 * st_mode and st_nlink so -v printing will look better.
433 		 */
434 		arcn->type = PAX_HLK;
435 		arcn->sb.st_nlink = 2;
436 
437 		/*
438 		 * no idea of what type this thing really points at, but
439 		 * we set something for printing only.
440 		 */
441 		arcn->sb.st_mode |= S_IFREG;
442 		break;
443 	case LONGLINKTYPE:
444 	case LONGNAMETYPE:
445 		/*
446 		 * GNU long link/file; we tag these here and let the
447 		 * pax internals deal with it -- too ugly otherwise.
448 		 */
449 		arcn->type =
450 		    hd->linkflag == LONGLINKTYPE ? PAX_GLL : PAX_GLF;
451 		arcn->pad = TAR_PAD(arcn->sb.st_size);
452 		arcn->skip = arcn->sb.st_size;
453 		break;
454 	case DIRTYPE:
455 		/*
456 		 * It is a directory, set the mode for -v printing
457 		 */
458 		arcn->type = PAX_DIR;
459 		arcn->sb.st_mode |= S_IFDIR;
460 		arcn->sb.st_nlink = 2;
461 		break;
462 	case AREGTYPE:
463 	case REGTYPE:
464 	default:
465 		/*
466 		 * If we have a trailing / this is a directory and NOT a file.
467 		 */
468 		arcn->ln_name[0] = '\0';
469 		arcn->ln_nlen = 0;
470 		if (*pt == '/') {
471 			/*
472 			 * it is a directory, set the mode for -v printing
473 			 */
474 			arcn->type = PAX_DIR;
475 			arcn->sb.st_mode |= S_IFDIR;
476 			arcn->sb.st_nlink = 2;
477 		} else {
478 			/*
479 			 * have a file that will be followed by data. Set the
480 			 * skip value to the size field and calculate the size
481 			 * of the padding.
482 			 */
483 			arcn->type = PAX_REG;
484 			arcn->sb.st_mode |= S_IFREG;
485 			arcn->pad = TAR_PAD(arcn->sb.st_size);
486 			arcn->skip = arcn->sb.st_size;
487 		}
488 		break;
489 	}
490 
491 	/*
492 	 * strip off any trailing slash.
493 	 */
494 	if (*pt == '/') {
495 		*pt = '\0';
496 		--arcn->nlen;
497 	}
498 	return(0);
499 }
500 
501 /*
502  * tar_wr()
503  *	write a tar header for the file specified in the ARCHD to the archive.
504  *	Have to check for file types that cannot be stored and file names that
505  *	are too long. Be careful of the term (last arg) to ul_oct, each field
506  *	of tar has it own spec for the termination character(s).
507  *	ASSUMED: space after header in header block is zero filled
508  * Return:
509  *	0 if file has data to be written after the header, 1 if file has NO
510  *	data to write after the header, -1 if archive write failed
511  */
512 
513 int
514 tar_wr(ARCHD *arcn)
515 {
516 	HD_TAR *hd;
517 	int len;
518 	char hdblk[sizeof(HD_TAR)];
519 
520 	/*
521 	 * check for those file system types which tar cannot store
522 	 */
523 	switch (arcn->type) {
524 	case PAX_DIR:
525 		/*
526 		 * user asked that dirs not be written to the archive
527 		 */
528 		if (tar_nodir)
529 			return(1);
530 		break;
531 	case PAX_CHR:
532 		paxwarn(1, "Tar cannot archive a character device %s",
533 		    arcn->org_name);
534 		return(1);
535 	case PAX_BLK:
536 		paxwarn(1, "Tar cannot archive a block device %s", arcn->org_name);
537 		return(1);
538 	case PAX_SCK:
539 		paxwarn(1, "Tar cannot archive a socket %s", arcn->org_name);
540 		return(1);
541 	case PAX_FIF:
542 		paxwarn(1, "Tar cannot archive a fifo %s", arcn->org_name);
543 		return(1);
544 	case PAX_SLK:
545 	case PAX_HLK:
546 	case PAX_HRG:
547 		if (arcn->ln_nlen > sizeof(hd->linkname)) {
548 			paxwarn(1, "Link name too long for tar %s",
549 			    arcn->ln_name);
550 			return(1);
551 		}
552 		break;
553 	case PAX_REG:
554 	case PAX_CTG:
555 	default:
556 		break;
557 	}
558 
559 	/*
560 	 * check file name len, remember extra char for dirs (the / at the end)
561 	 */
562 	len = arcn->nlen;
563 	if (arcn->type == PAX_DIR)
564 		++len;
565 	if (len > sizeof(hd->name)) {
566 		paxwarn(1, "File name too long for tar %s", arcn->name);
567 		return(1);
568 	}
569 
570 	/*
571 	 * Copy the data out of the ARCHD into the tar header based on the type
572 	 * of the file. Remember, many tar readers want all fields to be
573 	 * padded with zero so we zero the header first.  We then set the
574 	 * linkflag field (type), the linkname, the size, and set the padding
575 	 * (if any) to be added after the file data (0 for all other types,
576 	 * as they only have a header).
577 	 */
578 	memset(hdblk, 0, sizeof(hdblk));
579 	hd = (HD_TAR *)hdblk;
580 	fieldcpy(hd->name, sizeof(hd->name), arcn->name, sizeof(arcn->name));
581 	arcn->pad = 0;
582 
583 	if (arcn->type == PAX_DIR) {
584 		/*
585 		 * directories are the same as files, except have a filename
586 		 * that ends with a /, we add the slash here. No data follows
587 		 * dirs, so no pad.
588 		 */
589 		hd->linkflag = AREGTYPE;
590 		hd->name[len-1] = '/';
591 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
592 			goto out;
593 	} else if (arcn->type == PAX_SLK) {
594 		/*
595 		 * no data follows this file, so no pad
596 		 */
597 		hd->linkflag = SYMTYPE;
598 		fieldcpy(hd->linkname, sizeof(hd->linkname), arcn->ln_name,
599 		    sizeof(arcn->ln_name));
600 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
601 			goto out;
602 	} else if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) {
603 		/*
604 		 * no data follows this file, so no pad
605 		 */
606 		hd->linkflag = LNKTYPE;
607 		fieldcpy(hd->linkname, sizeof(hd->linkname), arcn->ln_name,
608 		    sizeof(arcn->ln_name));
609 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
610 			goto out;
611 	} else {
612 		/*
613 		 * data follows this file, so set the pad
614 		 */
615 		hd->linkflag = AREGTYPE;
616 #		ifdef LONG_OFF_T
617 		if (ul_oct((u_long)arcn->sb.st_size, hd->size,
618 		    sizeof(hd->size), 1)) {
619 #		else
620 		if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size,
621 		    sizeof(hd->size), 1)) {
622 #		endif
623 			paxwarn(1,"File is too large for tar %s", arcn->org_name);
624 			return(1);
625 		}
626 		arcn->pad = TAR_PAD(arcn->sb.st_size);
627 	}
628 
629 	/*
630 	 * copy those fields that are independent of the type
631 	 */
632 	if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 0) ||
633 	    ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 0) ||
634 	    ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 0) ||
635 	    ul_oct((u_long)(u_int)arcn->sb.st_mtime, hd->mtime, sizeof(hd->mtime), 1))
636 		goto out;
637 
638 	/*
639 	 * calculate and add the checksum, then write the header. A return of
640 	 * 0 tells the caller to now write the file data, 1 says no data needs
641 	 * to be written
642 	 */
643 	if (ul_oct(tar_chksm(hdblk, sizeof(HD_TAR)), hd->chksum,
644 	    sizeof(hd->chksum), 3))
645 		goto out;
646 	if (wr_rdbuf(hdblk, sizeof(HD_TAR)) < 0)
647 		return(-1);
648 	if (wr_skip((off_t)(BLKMULT - sizeof(HD_TAR))) < 0)
649 		return(-1);
650 	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
651 		return(0);
652 	return(1);
653 
654     out:
655 	/*
656 	 * header field is out of range
657 	 */
658 	paxwarn(1, "Tar header field is too small for %s", arcn->org_name);
659 	return(1);
660 }
661 
662 /*
663  * Routines for POSIX ustar
664  */
665 
666 /*
667  * ustar_strd()
668  *	initialization for ustar read
669  * Return:
670  *	0 if ok, -1 otherwise
671  */
672 
673 int
674 ustar_strd(void)
675 {
676 	if ((usrtb_start() < 0) || (grptb_start() < 0))
677 		return(-1);
678 	return(0);
679 }
680 
681 /*
682  * ustar_stwr()
683  *	initialization for ustar write
684  * Return:
685  *	0 if ok, -1 otherwise
686  */
687 
688 int
689 ustar_stwr(void)
690 {
691 	if ((uidtb_start() < 0) || (gidtb_start() < 0))
692 		return(-1);
693 	return(0);
694 }
695 
696 /*
697  * ustar_id()
698  *	determine if a block given to us is a valid ustar header. We have to
699  *	be on the lookout for those pesky blocks of all zero's
700  * Return:
701  *	0 if a ustar header, -1 otherwise
702  */
703 
704 int
705 ustar_id(char *blk, int size)
706 {
707 	HD_USTAR *hd;
708 
709 	if (size < BLKMULT)
710 		return(-1);
711 	hd = (HD_USTAR *)blk;
712 
713 	/*
714 	 * check for block of zero's first, a simple and fast test then check
715 	 * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
716 	 * programs are fouled up and create archives missing the \0. Last we
717 	 * check the checksum. If ok we have to assume it is a valid header.
718 	 */
719 	if (hd->prefix[0] == '\0' && hd->name[0] == '\0')
720 		return(-1);
721 	if (strncmp(hd->magic, TMAGIC, TMAGLEN - 1) != 0)
722 		return(-1);
723 	if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT))
724 		return(-1);
725 	return(0);
726 }
727 
728 /*
729  * ustar_rd()
730  *	extract the values out of block already determined to be a ustar header.
731  *	store the values in the ARCHD parameter.
732  * Return:
733  *	0
734  */
735 
736 int
737 ustar_rd(ARCHD *arcn, char *buf)
738 {
739 	HD_USTAR *hd;
740 	char *dest;
741 	int cnt = 0;
742 	dev_t devmajor;
743 	dev_t devminor;
744 
745 	/*
746 	 * we only get proper sized buffers
747 	 */
748 	if (ustar_id(buf, BLKMULT) < 0)
749 		return(-1);
750 	memset(arcn, 0, sizeof(*arcn));
751 	arcn->org_name = arcn->name;
752 	arcn->sb.st_nlink = 1;
753 	hd = (HD_USTAR *)buf;
754 
755 	/*
756 	 * see if the filename is split into two parts. if, so joint the parts.
757 	 * we copy the prefix first and add a / between the prefix and name.
758 	 */
759 	dest = arcn->name;
760 	if (*(hd->prefix) != '\0') {
761 		cnt = fieldcpy(dest, sizeof(arcn->name) - 1, hd->prefix,
762 		    sizeof(hd->prefix));
763 		dest += cnt;
764 		*dest++ = '/';
765 		cnt++;
766 	} else {
767 		cnt = 0;
768 	}
769 
770 	if (hd->typeflag != LONGLINKTYPE && hd->typeflag != LONGNAMETYPE) {
771 		arcn->nlen = cnt + expandname(dest, sizeof(arcn->name) - cnt,
772 		    &gnu_name_string, hd->name, sizeof(hd->name));
773 		arcn->ln_nlen = expandname(arcn->ln_name, sizeof(arcn->ln_name),
774 		    &gnu_link_string, hd->linkname, sizeof(hd->linkname));
775 	}
776 
777 	/*
778 	 * follow the spec to the letter. we should only have mode bits, strip
779 	 * off all other crud we may be passed.
780 	 */
781 	arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode, sizeof(hd->mode), OCT) &
782 	    0xfff);
783 #ifdef LONG_OFF_T
784 	arcn->sb.st_size = (off_t)asc_ul(hd->size, sizeof(hd->size), OCT);
785 #else
786 	arcn->sb.st_size = (off_t)asc_uqd(hd->size, sizeof(hd->size), OCT);
787 #endif
788 	arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT);
789 	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
790 
791 	/*
792 	 * If we can find the ascii names for gname and uname in the password
793 	 * and group files we will use the uid's and gid they bind. Otherwise
794 	 * we use the uid and gid values stored in the header. (This is what
795 	 * the posix spec wants).
796 	 */
797 	hd->gname[sizeof(hd->gname) - 1] = '\0';
798 	if (gid_name(hd->gname, &(arcn->sb.st_gid)) < 0)
799 		arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
800 	hd->uname[sizeof(hd->uname) - 1] = '\0';
801 	if (uid_name(hd->uname, &(arcn->sb.st_uid)) < 0)
802 		arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
803 
804 	/*
805 	 * set the defaults, these may be changed depending on the file type
806 	 */
807 	arcn->pad = 0;
808 	arcn->skip = 0;
809 	arcn->sb.st_rdev = (dev_t)0;
810 
811 	/*
812 	 * set the mode and PAX type according to the typeflag in the header
813 	 */
814 	switch (hd->typeflag) {
815 	case FIFOTYPE:
816 		arcn->type = PAX_FIF;
817 		arcn->sb.st_mode |= S_IFIFO;
818 		break;
819 	case DIRTYPE:
820 		arcn->type = PAX_DIR;
821 		arcn->sb.st_mode |= S_IFDIR;
822 		arcn->sb.st_nlink = 2;
823 
824 		/*
825 		 * Some programs that create ustar archives append a '/'
826 		 * to the pathname for directories. This clearly violates
827 		 * ustar specs, but we will silently strip it off anyway.
828 		 */
829 		if (arcn->name[arcn->nlen - 1] == '/')
830 			arcn->name[--arcn->nlen] = '\0';
831 		break;
832 	case BLKTYPE:
833 	case CHRTYPE:
834 		/*
835 		 * this type requires the rdev field to be set.
836 		 */
837 		if (hd->typeflag == BLKTYPE) {
838 			arcn->type = PAX_BLK;
839 			arcn->sb.st_mode |= S_IFBLK;
840 		} else {
841 			arcn->type = PAX_CHR;
842 			arcn->sb.st_mode |= S_IFCHR;
843 		}
844 		devmajor = (dev_t)asc_ul(hd->devmajor,sizeof(hd->devmajor),OCT);
845 		devminor = (dev_t)asc_ul(hd->devminor,sizeof(hd->devminor),OCT);
846 		arcn->sb.st_rdev = TODEV(devmajor, devminor);
847 		break;
848 	case SYMTYPE:
849 	case LNKTYPE:
850 		if (hd->typeflag == SYMTYPE) {
851 			arcn->type = PAX_SLK;
852 			arcn->sb.st_mode |= S_IFLNK;
853 		} else {
854 			arcn->type = PAX_HLK;
855 			/*
856 			 * so printing looks better
857 			 */
858 			arcn->sb.st_mode |= S_IFREG;
859 			arcn->sb.st_nlink = 2;
860 		}
861 		break;
862 	case LONGLINKTYPE:
863 	case LONGNAMETYPE:
864 		/*
865 		 * GNU long link/file; we tag these here and let the
866 		 * pax internals deal with it -- too ugly otherwise.
867 		 */
868 		arcn->type =
869 		    hd->typeflag == LONGLINKTYPE ? PAX_GLL : PAX_GLF;
870 		arcn->pad = TAR_PAD(arcn->sb.st_size);
871 		arcn->skip = arcn->sb.st_size;
872 		break;
873 	case CONTTYPE:
874 	case AREGTYPE:
875 	case REGTYPE:
876 	default:
877 		/*
878 		 * these types have file data that follows. Set the skip and
879 		 * pad fields.
880 		 */
881 		arcn->type = PAX_REG;
882 		arcn->pad = TAR_PAD(arcn->sb.st_size);
883 		arcn->skip = arcn->sb.st_size;
884 		arcn->sb.st_mode |= S_IFREG;
885 		break;
886 	}
887 	return(0);
888 }
889 
890 /*
891  * ustar_wr()
892  *	write a ustar header for the file specified in the ARCHD to the archive
893  *	Have to check for file types that cannot be stored and file names that
894  *	are too long. Be careful of the term (last arg) to ul_oct, we only use
895  *	'\0' for the termination character (this is different than picky tar)
896  *	ASSUMED: space after header in header block is zero filled
897  * Return:
898  *	0 if file has data to be written after the header, 1 if file has NO
899  *	data to write after the header, -1 if archive write failed
900  */
901 
902 int
903 ustar_wr(ARCHD *arcn)
904 {
905 	HD_USTAR *hd;
906 	char *pt;
907 	char hdblk[sizeof(HD_USTAR)];
908 
909 	/*
910 	 * check for those file system types ustar cannot store
911 	 */
912 	if (arcn->type == PAX_SCK) {
913 		paxwarn(1, "Ustar cannot archive a socket %s", arcn->org_name);
914 		return(1);
915 	}
916 
917 	/*
918 	 * check the length of the linkname
919 	 */
920 	if (((arcn->type == PAX_SLK) || (arcn->type == PAX_HLK) ||
921 	    (arcn->type == PAX_HRG)) && (arcn->ln_nlen > sizeof(hd->linkname))){
922 		paxwarn(1, "Link name too long for ustar %s", arcn->ln_name);
923 		return(1);
924 	}
925 
926 	/*
927 	 * split the path name into prefix and name fields (if needed). if
928 	 * pt != arcn->name, the name has to be split
929 	 */
930 	if ((pt = name_split(arcn->name, arcn->nlen)) == NULL) {
931 		paxwarn(1, "File name too long for ustar %s", arcn->name);
932 		return(1);
933 	}
934 
935 	/*
936 	 * zero out the header so we don't have to worry about zero fill below
937 	 */
938 	memset(hdblk, 0, sizeof(hdblk));
939 	hd = (HD_USTAR *)hdblk;
940 	arcn->pad = 0L;
941 
942 	/*
943 	 * split the name, or zero out the prefix
944 	 */
945 	if (pt != arcn->name) {
946 		/*
947 		 * name was split, pt points at the / where the split is to
948 		 * occur, we remove the / and copy the first part to the prefix
949 		 */
950 		*pt = '\0';
951 		fieldcpy(hd->prefix, sizeof(hd->prefix), arcn->name,
952 		    sizeof(arcn->name));
953 		*pt++ = '/';
954 	}
955 
956 	/*
957 	 * copy the name part. this may be the whole path or the part after
958 	 * the prefix
959 	 */
960 	fieldcpy(hd->name, sizeof(hd->name), pt,
961 	    sizeof(arcn->name) - (pt - arcn->name));
962 
963 	/*
964 	 * set the fields in the header that are type dependent
965 	 */
966 	switch (arcn->type) {
967 	case PAX_DIR:
968 		hd->typeflag = DIRTYPE;
969 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
970 			goto out;
971 		break;
972 	case PAX_CHR:
973 	case PAX_BLK:
974 		if (arcn->type == PAX_CHR)
975 			hd->typeflag = CHRTYPE;
976 		else
977 			hd->typeflag = BLKTYPE;
978 		if (ul_oct((u_long)MAJOR(arcn->sb.st_rdev), hd->devmajor,
979 		   sizeof(hd->devmajor), 3) ||
980 		   ul_oct((u_long)MINOR(arcn->sb.st_rdev), hd->devminor,
981 		   sizeof(hd->devminor), 3) ||
982 		   ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
983 			goto out;
984 		break;
985 	case PAX_FIF:
986 		hd->typeflag = FIFOTYPE;
987 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
988 			goto out;
989 		break;
990 	case PAX_SLK:
991 	case PAX_HLK:
992 	case PAX_HRG:
993 		if (arcn->type == PAX_SLK)
994 			hd->typeflag = SYMTYPE;
995 		else
996 			hd->typeflag = LNKTYPE;
997 		fieldcpy(hd->linkname, sizeof(hd->linkname), arcn->ln_name,
998 		    sizeof(arcn->ln_name));
999 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
1000 			goto out;
1001 		break;
1002 	case PAX_REG:
1003 	case PAX_CTG:
1004 	default:
1005 		/*
1006 		 * file data with this type, set the padding
1007 		 */
1008 		if (arcn->type == PAX_CTG)
1009 			hd->typeflag = CONTTYPE;
1010 		else
1011 			hd->typeflag = REGTYPE;
1012 		arcn->pad = TAR_PAD(arcn->sb.st_size);
1013 #		ifdef LONG_OFF_T
1014 		if (ul_oct((u_long)arcn->sb.st_size, hd->size,
1015 		    sizeof(hd->size), 3)) {
1016 #		else
1017 		if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size,
1018 		    sizeof(hd->size), 3)) {
1019 #		endif
1020 			paxwarn(1,"File is too long for ustar %s",arcn->org_name);
1021 			return(1);
1022 		}
1023 		break;
1024 	}
1025 
1026 	strncpy(hd->magic, TMAGIC, TMAGLEN);
1027 	strncpy(hd->version, TVERSION, TVERSLEN);
1028 
1029 	/*
1030 	 * set the remaining fields. Some versions want all 16 bits of mode
1031 	 * we better humor them (they really do not meet spec though)....
1032 	 */
1033 	if (ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 3)) {
1034 		if (uid_nobody == 0) {
1035 			if (uid_name("nobody", &uid_nobody) == -1)
1036 				goto out;
1037 		}
1038 		if (uid_warn != arcn->sb.st_uid) {
1039 			uid_warn = arcn->sb.st_uid;
1040 			paxwarn(1,
1041 			    "Ustar header field is too small for uid %lu, "
1042 			    "using nobody", (u_long)arcn->sb.st_uid);
1043 		}
1044 		if (ul_oct((u_long)uid_nobody, hd->uid, sizeof(hd->uid), 3))
1045 			goto out;
1046 	}
1047 	if (ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 3)) {
1048 		if (gid_nobody == 0) {
1049 			if (gid_name("nobody", &gid_nobody) == -1)
1050 				goto out;
1051 		}
1052 		if (gid_warn != arcn->sb.st_gid) {
1053 			gid_warn = arcn->sb.st_gid;
1054 			paxwarn(1,
1055 			    "Ustar header field is too small for gid %lu, "
1056 			    "using nobody", (u_long)arcn->sb.st_gid);
1057 		}
1058 		if (ul_oct((u_long)gid_nobody, hd->gid, sizeof(hd->gid), 3))
1059 			goto out;
1060 	}
1061 	if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3) ||
1062 	    ul_oct((u_long)(u_int)arcn->sb.st_mtime,hd->mtime,sizeof(hd->mtime),3))
1063 		goto out;
1064 	strncpy(hd->uname, name_uid(arcn->sb.st_uid, 0), sizeof(hd->uname));
1065 	strncpy(hd->gname, name_gid(arcn->sb.st_gid, 0), sizeof(hd->gname));
1066 
1067 	/*
1068 	 * calculate and store the checksum write the header to the archive
1069 	 * return 0 tells the caller to now write the file data, 1 says no data
1070 	 * needs to be written
1071 	 */
1072 	if (ul_oct(tar_chksm(hdblk, sizeof(HD_USTAR)), hd->chksum,
1073 	   sizeof(hd->chksum), 3))
1074 		goto out;
1075 	if (wr_rdbuf(hdblk, sizeof(HD_USTAR)) < 0)
1076 		return(-1);
1077 	if (wr_skip((off_t)(BLKMULT - sizeof(HD_USTAR))) < 0)
1078 		return(-1);
1079 	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
1080 		return(0);
1081 	return(1);
1082 
1083     out:
1084 	/*
1085 	 * header field is out of range
1086 	 */
1087 	paxwarn(1, "Ustar header field is too small for %s", arcn->org_name);
1088 	return(1);
1089 }
1090 
1091 /*
1092  * name_split()
1093  *	see if the name has to be split for storage in a ustar header. We try
1094  *	to fit the entire name in the name field without splitting if we can.
1095  *	The split point is always at a /
1096  * Return
1097  *	character pointer to split point (always the / that is to be removed
1098  *	if the split is not needed, the points is set to the start of the file
1099  *	name (it would violate the spec to split there). A NULL is returned if
1100  *	the file name is too long
1101  */
1102 
1103 static char *
1104 name_split(char *name, int len)
1105 {
1106 	char *start;
1107 
1108 	/*
1109 	 * check to see if the file name is small enough to fit in the name
1110 	 * field. if so just return a pointer to the name.
1111 	 * The strings can fill the complete name and prefix fields
1112 	 * without a NUL terminator.
1113 	 */
1114 	if (len <= TNMSZ)
1115 		return(name);
1116 	if (len > (TPFSZ + TNMSZ + 1))
1117 		return(NULL);
1118 
1119 	/*
1120 	 * we start looking at the biggest sized piece that fits in the name
1121 	 * field. We walk forward looking for a slash to split at. The idea is
1122 	 * to find the biggest piece to fit in the name field (or the smallest
1123 	 * prefix we can find) (the -1 is correct the biggest piece would
1124 	 * include the slash between the two parts that gets thrown away)
1125 	 */
1126 	start = name + len - TNMSZ - 1;
1127 	while ((*start != '\0') && (*start != '/'))
1128 		++start;
1129 
1130 	/*
1131 	 * if we hit the end of the string, this name cannot be split, so we
1132 	 * cannot store this file.
1133 	 */
1134 	if (*start == '\0')
1135 		return(NULL);
1136 	len = start - name;
1137 
1138 	/*
1139 	 * NOTE: /str where the length of str == TNMSZ can not be stored under
1140 	 * the p1003.1-1990 spec for ustar. We could force a prefix of / and
1141 	 * the file would then expand on extract to //str. The len == 0 below
1142 	 * makes this special case follow the spec to the letter.
1143 	 */
1144 	if ((len > TPFSZ) || (len == 0))
1145 		return(NULL);
1146 
1147 	/*
1148 	 * ok have a split point, return it to the caller
1149 	 */
1150 	return(start);
1151 }
1152 
1153 static size_t
1154 expandname(char *buf, size_t len, char **gnu_name, const char *name,
1155     size_t limit)
1156 {
1157 	size_t nlen;
1158 
1159 	if (*gnu_name) {
1160 		/* *gnu_name is NUL terminated */
1161 		if ((nlen = strlcpy(buf, *gnu_name, len)) >= len)
1162 			nlen = len - 1;
1163 		free(*gnu_name);
1164 		*gnu_name = NULL;
1165 	} else
1166 		nlen = fieldcpy(buf, len, name, limit);
1167 	return(nlen);
1168 }
1169