tar.c revision 1.59 1 /* $NetBSD: tar.c,v 1.59 2004/09/26 22:49:05 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. 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 #if HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
38 #endif
39
40 #include <sys/cdefs.h>
41 #if !defined(lint)
42 #if 0
43 static char sccsid[] = "@(#)tar.c 8.2 (Berkeley) 4/18/94";
44 #else
45 __RCSID("$NetBSD: tar.c,v 1.59 2004/09/26 22:49:05 christos Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <sys/types.h>
50 #include <sys/time.h>
51 #include <sys/stat.h>
52 #include <sys/param.h>
53
54 #include <ctype.h>
55 #include <errno.h>
56 #include <grp.h>
57 #include <pwd.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62
63 #include "pax.h"
64 #include "extern.h"
65 #include "tar.h"
66
67 /*
68 * Routines for reading, writing and header identify of various versions of tar
69 */
70
71 static int expandname(char *, size_t, char **, size_t *, const char *, size_t);
72 static void longlink(ARCHD *, int);
73 static u_long tar_chksm(char *, int);
74 static char *name_split(char *, int);
75 static int ul_oct(u_long, char *, int, int);
76 #if !defined(NET2_STAT) && !defined(_LP64)
77 static int ull_oct(unsigned long long, char *, int, int);
78 #endif
79 static int tar_gnutar_exclude_one(const char *, size_t);
80 static int check_sum(char *, size_t, char *, size_t, int);
81
82 /*
83 * Routines common to all versions of tar
84 */
85
86 static int tar_nodir; /* do not write dirs under old tar */
87 int is_gnutar; /* behave like gnu tar; enable gnu
88 * extensions and skip end-ofvolume
89 * checks
90 */
91 static int seen_gnu_warning; /* Have we warned yet? */
92 static char *gnu_hack_string; /* ././@LongLink hackery */
93 static int gnu_hack_len; /* len of gnu_hack_string */
94 char *gnu_name_string; /* ././@LongLink hackery name */
95 char *gnu_link_string; /* ././@LongLink hackery link */
96 size_t gnu_name_length; /* ././@LongLink hackery name */
97 size_t gnu_link_length; /* ././@LongLink hackery link */
98 static int gnu_short_trailer; /* gnu short trailer */
99
100 static const char LONG_LINK[] = "././@LongLink";
101
102 #ifdef _PAX_
103 char DEV_0[] = "/dev/rst0";
104 char DEV_1[] = "/dev/rst1";
105 char DEV_4[] = "/dev/rst4";
106 char DEV_5[] = "/dev/rst5";
107 char DEV_7[] = "/dev/rst7";
108 char DEV_8[] = "/dev/rst8";
109 #endif
110
111 static int
112 check_sum(char *hd, size_t hdlen, char *bl, size_t bllen, int quiet)
113 {
114 u_long hdck, blck;
115
116 hdck = asc_ul(hd, hdlen, OCT);
117 blck = tar_chksm(bl, bllen);
118
119 if (hdck != blck) {
120 if (!quiet)
121 tty_warn(0, "Header checksum %lo does not match %lo",
122 hdck, blck);
123 return(-1);
124 }
125 return(0);
126 }
127
128
129 /*
130 * tar_endwr()
131 * add the tar trailer of two null blocks
132 * Return:
133 * 0 if ok, -1 otherwise (what wr_skip returns)
134 */
135
136 int
137 tar_endwr(void)
138 {
139 return(wr_skip((off_t)(NULLCNT * BLKMULT)));
140 }
141
142 /*
143 * tar_endrd()
144 * no cleanup needed here, just return size of trailer (for append)
145 * Return:
146 * size of trailer BLKMULT
147 */
148
149 off_t
150 tar_endrd(void)
151 {
152 return((off_t)((gnu_short_trailer ? 1 : NULLCNT) * BLKMULT));
153 }
154
155 /*
156 * tar_trail()
157 * Called to determine if a header block is a valid trailer. We are passed
158 * the block, the in_sync flag (which tells us we are in resync mode;
159 * looking for a valid header), and cnt (which starts at zero) which is
160 * used to count the number of empty blocks we have seen so far.
161 * Return:
162 * 0 if a valid trailer, -1 if not a valid trailer, or 1 if the block
163 * could never contain a header.
164 */
165
166 int
167 tar_trail(char *buf, int in_resync, int *cnt)
168 {
169 int i;
170
171 gnu_short_trailer = 0;
172 /*
173 * look for all zero, trailer is two consecutive blocks of zero
174 */
175 for (i = 0; i < BLKMULT; ++i) {
176 if (buf[i] != '\0')
177 break;
178 }
179
180 /*
181 * if not all zero it is not a trailer, but MIGHT be a header.
182 */
183 if (i != BLKMULT)
184 return(-1);
185
186 /*
187 * When given a zero block, we must be careful!
188 * If we are not in resync mode, check for the trailer. Have to watch
189 * out that we do not mis-identify file data as the trailer, so we do
190 * NOT try to id a trailer during resync mode. During resync mode we
191 * might as well throw this block out since a valid header can NEVER be
192 * a block of all 0 (we must have a valid file name).
193 */
194 if (!in_resync) {
195 ++*cnt;
196 /*
197 * old GNU tar (up through 1.13) only writes one block of
198 * trailers, so we pretend we got another
199 */
200 if (is_gnutar) {
201 gnu_short_trailer = 1;
202 ++*cnt;
203 }
204 if (*cnt >= NULLCNT)
205 return(0);
206 }
207 return(1);
208 }
209
210 /*
211 * ul_oct()
212 * convert an unsigned long to an octal string. many oddball field
213 * termination characters are used by the various versions of tar in the
214 * different fields. term selects which kind to use. str is '0' padded
215 * at the front to len. we are unable to use only one format as many old
216 * tar readers are very cranky about this.
217 * Return:
218 * 0 if the number fit into the string, -1 otherwise
219 */
220
221 static int
222 ul_oct(u_long val, char *str, int len, int term)
223 {
224 char *pt;
225
226 /*
227 * term selects the appropriate character(s) for the end of the string
228 */
229 pt = str + len - 1;
230 switch(term) {
231 case 3:
232 *pt-- = '\0';
233 break;
234 case 2:
235 *pt-- = ' ';
236 *pt-- = '\0';
237 break;
238 case 1:
239 *pt-- = ' ';
240 break;
241 case 0:
242 default:
243 *pt-- = '\0';
244 *pt-- = ' ';
245 break;
246 }
247
248 /*
249 * convert and blank pad if there is space
250 */
251 while (pt >= str) {
252 *pt-- = '0' + (char)(val & 0x7);
253 if ((val = val >> 3) == (u_long)0)
254 break;
255 }
256
257 while (pt >= str)
258 *pt-- = '0';
259 if (val != (u_long)0)
260 return(-1);
261 return(0);
262 }
263
264 #if !defined(NET2_STAT) && !defined(_LP64)
265 /*
266 * ull_oct()
267 * convert an unsigned long long to an octal string. one of many oddball
268 * field termination characters are used by the various versions of tar
269 * in the different fields. term selects which kind to use. str is '0'
270 * padded at the front to len. we are unable to use only one format as
271 * many old tar readers are very cranky about this.
272 * Return:
273 * 0 if the number fit into the string, -1 otherwise
274 */
275
276 static int
277 ull_oct(unsigned long long val, char *str, int len, int term)
278 {
279 char *pt;
280
281 /*
282 * term selects the appropriate character(s) for the end of the string
283 */
284 pt = str + len - 1;
285 switch(term) {
286 case 3:
287 *pt-- = '\0';
288 break;
289 case 2:
290 *pt-- = ' ';
291 *pt-- = '\0';
292 break;
293 case 1:
294 *pt-- = ' ';
295 break;
296 case 0:
297 default:
298 *pt-- = '\0';
299 *pt-- = ' ';
300 break;
301 }
302
303 /*
304 * convert and blank pad if there is space
305 */
306 while (pt >= str) {
307 *pt-- = '0' + (char)(val & 0x7);
308 if ((val = val >> 3) == 0)
309 break;
310 }
311
312 while (pt >= str)
313 *pt-- = '0';
314 if (val != (unsigned long long)0)
315 return(-1);
316 return(0);
317 }
318 #endif
319
320 /*
321 * tar_chksm()
322 * calculate the checksum for a tar block counting the checksum field as
323 * all blanks (BLNKSUM is that value pre-calculated, the sum of 8 blanks).
324 * NOTE: we use len to short circuit summing 0's on write since we ALWAYS
325 * pad headers with 0.
326 * Return:
327 * unsigned long checksum
328 */
329
330 static u_long
331 tar_chksm(char *blk, int len)
332 {
333 char *stop;
334 char *pt;
335 u_long chksm = BLNKSUM; /* initial value is checksum field sum */
336
337 /*
338 * add the part of the block before the checksum field
339 */
340 pt = blk;
341 stop = blk + CHK_OFFSET;
342 while (pt < stop)
343 chksm += (u_long)(*pt++ & 0xff);
344 /*
345 * move past the checksum field and keep going, spec counts the
346 * checksum field as the sum of 8 blanks (which is pre-computed as
347 * BLNKSUM).
348 * ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding
349 * starts, no point in summing zero's)
350 */
351 pt += CHK_LEN;
352 stop = blk + len;
353 while (pt < stop)
354 chksm += (u_long)(*pt++ & 0xff);
355 return(chksm);
356 }
357
358 /*
359 * Routines for old BSD style tar (also made portable to sysV tar)
360 */
361
362 /*
363 * tar_id()
364 * determine if a block given to us is a valid tar header (and not a USTAR
365 * header). We have to be on the lookout for those pesky blocks of all
366 * zero's.
367 * Return:
368 * 0 if a tar header, -1 otherwise
369 */
370
371 int
372 tar_id(char *blk, int size)
373 {
374 HD_TAR *hd;
375 HD_USTAR *uhd;
376
377 if (size < BLKMULT)
378 return(-1);
379 hd = (HD_TAR *)blk;
380 uhd = (HD_USTAR *)blk;
381
382 /*
383 * check for block of zero's first, a simple and fast test, then make
384 * sure this is not a ustar header by looking for the ustar magic
385 * cookie. We should use TMAGLEN, but some USTAR archive programs are
386 * wrong and create archives missing the \0. Last we check the
387 * checksum. If this is ok we have to assume it is a valid header.
388 */
389 if (hd->name[0] == '\0')
390 return(-1);
391 if (strncmp(uhd->magic, TMAGIC, TMAGLEN - 1) == 0)
392 return(-1);
393 return check_sum(hd->chksum, sizeof(hd->chksum), blk, BLKMULT, 1);
394 }
395
396 /*
397 * tar_opt()
398 * handle tar format specific -o options
399 * Return:
400 * 0 if ok -1 otherwise
401 */
402
403 int
404 tar_opt(void)
405 {
406 OPLIST *opt;
407
408 while ((opt = opt_next()) != NULL) {
409 if (strcmp(opt->name, TAR_OPTION) ||
410 strcmp(opt->value, TAR_NODIR)) {
411 tty_warn(1,
412 "Unknown tar format -o option/value pair %s=%s",
413 opt->name, opt->value);
414 tty_warn(1,
415 "%s=%s is the only supported tar format option",
416 TAR_OPTION, TAR_NODIR);
417 return(-1);
418 }
419
420 /*
421 * we only support one option, and only when writing
422 */
423 if ((act != APPND) && (act != ARCHIVE)) {
424 tty_warn(1, "%s=%s is only supported when writing.",
425 opt->name, opt->value);
426 return(-1);
427 }
428 tar_nodir = 1;
429 }
430 return(0);
431 }
432
433
434 /*
435 * tar_rd()
436 * extract the values out of block already determined to be a tar header.
437 * store the values in the ARCHD parameter.
438 * Return:
439 * 0
440 */
441
442 int
443 tar_rd(ARCHD *arcn, char *buf)
444 {
445 HD_TAR *hd;
446 char *pt;
447
448 /*
449 * we only get proper sized buffers passed to us
450 */
451 if (tar_id(buf, BLKMULT) < 0)
452 return(-1);
453 memset(arcn, 0, sizeof(*arcn));
454 arcn->org_name = arcn->name;
455 arcn->pat = NULL;
456 arcn->sb.st_nlink = 1;
457
458 /*
459 * copy out the name and values in the stat buffer
460 */
461 hd = (HD_TAR *)buf;
462 if (hd->linkflag != LONGLINKTYPE && hd->linkflag != LONGNAMETYPE) {
463 arcn->nlen = expandname(arcn->name, sizeof(arcn->name),
464 &gnu_name_string, &gnu_name_length, hd->name,
465 sizeof(hd->name));
466 arcn->ln_nlen = expandname(arcn->ln_name, sizeof(arcn->ln_name),
467 &gnu_link_string, &gnu_link_length, hd->linkname,
468 sizeof(hd->linkname));
469 }
470 arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode,sizeof(hd->mode),OCT) &
471 0xfff);
472 arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
473 arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
474 arcn->sb.st_size = (off_t)ASC_OFFT(hd->size, sizeof(hd->size), OCT);
475 arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT);
476 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
477
478 /*
479 * have to look at the last character, it may be a '/' and that is used
480 * to encode this as a directory
481 */
482 pt = &(arcn->name[arcn->nlen - 1]);
483 arcn->pad = 0;
484 arcn->skip = 0;
485 switch(hd->linkflag) {
486 case SYMTYPE:
487 /*
488 * symbolic link, need to get the link name and set the type in
489 * the st_mode so -v printing will look correct.
490 */
491 arcn->type = PAX_SLK;
492 arcn->sb.st_mode |= S_IFLNK;
493 break;
494 case LNKTYPE:
495 /*
496 * hard link, need to get the link name, set the type in the
497 * st_mode and st_nlink so -v printing will look better.
498 */
499 arcn->type = PAX_HLK;
500 arcn->sb.st_nlink = 2;
501
502 /*
503 * no idea of what type this thing really points at, but
504 * we set something for printing only.
505 */
506 arcn->sb.st_mode |= S_IFREG;
507 break;
508 case LONGLINKTYPE:
509 case LONGNAMETYPE:
510 /*
511 * GNU long link/file; we tag these here and let the
512 * pax internals deal with it -- too ugly otherwise.
513 */
514 if (hd->linkflag != LONGLINKTYPE)
515 arcn->type = PAX_GLF;
516 else
517 arcn->type = PAX_GLL;
518 arcn->pad = TAR_PAD(arcn->sb.st_size);
519 arcn->skip = arcn->sb.st_size;
520 break;
521 case AREGTYPE:
522 case REGTYPE:
523 case DIRTYPE: /* see below */
524 default:
525 /*
526 * If we have a trailing / this is a directory and NOT a file.
527 * Note: V7 tar doesn't actually have DIRTYPE, but it was
528 * reported that V7 archives using USTAR directories do exist.
529 */
530 if (*pt == '/' || hd->linkflag == DIRTYPE) {
531 /*
532 * it is a directory, set the mode for -v printing
533 */
534 arcn->type = PAX_DIR;
535 arcn->sb.st_mode |= S_IFDIR;
536 arcn->sb.st_nlink = 2;
537 } else {
538 /*
539 * have a file that will be followed by data. Set the
540 * skip value to the size field and calculate the size
541 * of the padding.
542 */
543 arcn->type = PAX_REG;
544 arcn->sb.st_mode |= S_IFREG;
545 arcn->pad = TAR_PAD(arcn->sb.st_size);
546 arcn->skip = arcn->sb.st_size;
547 }
548 break;
549 }
550
551 /*
552 * strip off any trailing slash.
553 */
554 if (*pt == '/') {
555 *pt = '\0';
556 --arcn->nlen;
557 }
558 return(0);
559 }
560
561 /*
562 * tar_wr()
563 * write a tar header for the file specified in the ARCHD to the archive.
564 * Have to check for file types that cannot be stored and file names that
565 * are too long. Be careful of the term (last arg) to ul_oct, each field
566 * of tar has it own spec for the termination character(s).
567 * ASSUMED: space after header in header block is zero filled
568 * Return:
569 * 0 if file has data to be written after the header, 1 if file has NO
570 * data to write after the header, -1 if archive write failed
571 */
572
573 int
574 tar_wr(ARCHD *arcn)
575 {
576 HD_TAR *hd;
577 int len;
578 char hdblk[sizeof(HD_TAR)];
579
580 /*
581 * check for those file system types which tar cannot store
582 */
583 switch(arcn->type) {
584 case PAX_DIR:
585 /*
586 * user asked that dirs not be written to the archive
587 */
588 if (tar_nodir)
589 return(1);
590 break;
591 case PAX_CHR:
592 tty_warn(1, "Tar cannot archive a character device %s",
593 arcn->org_name);
594 return(1);
595 case PAX_BLK:
596 tty_warn(1,
597 "Tar cannot archive a block device %s", arcn->org_name);
598 return(1);
599 case PAX_SCK:
600 tty_warn(1, "Tar cannot archive a socket %s", arcn->org_name);
601 return(1);
602 case PAX_FIF:
603 tty_warn(1, "Tar cannot archive a fifo %s", arcn->org_name);
604 return(1);
605 case PAX_SLK:
606 case PAX_HLK:
607 case PAX_HRG:
608 if (arcn->ln_nlen > sizeof(hd->linkname)) {
609 tty_warn(1,"Link name too long for tar %s",
610 arcn->ln_name);
611 return(1);
612 }
613 break;
614 case PAX_REG:
615 case PAX_CTG:
616 default:
617 break;
618 }
619
620 /*
621 * check file name len, remember extra char for dirs (the / at the end)
622 */
623 len = arcn->nlen;
624 if (arcn->type == PAX_DIR)
625 ++len;
626 if (len >= sizeof(hd->name)) {
627 tty_warn(1, "File name too long for tar %s", arcn->name);
628 return(1);
629 }
630
631 /*
632 * copy the data out of the ARCHD into the tar header based on the type
633 * of the file. Remember many tar readers want the unused fields to be
634 * padded with zero. We set the linkflag field (type), the linkname
635 * (or zero if not used),the size, and set the padding (if any) to be
636 * added after the file data (0 for all other types, as they only have
637 * a header)
638 */
639 memset(hdblk, 0, sizeof(hdblk));
640 hd = (HD_TAR *)hdblk;
641 strlcpy(hd->name, arcn->name, sizeof(hd->name));
642 arcn->pad = 0;
643
644 if (arcn->type == PAX_DIR) {
645 /*
646 * directories are the same as files, except have a filename
647 * that ends with a /, we add the slash here. No data follows,
648 * dirs, so no pad.
649 */
650 hd->linkflag = AREGTYPE;
651 hd->name[len-1] = '/';
652 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
653 goto out;
654 } else if (arcn->type == PAX_SLK) {
655 /*
656 * no data follows this file, so no pad
657 */
658 hd->linkflag = SYMTYPE;
659 strlcpy(hd->linkname, arcn->ln_name, sizeof(hd->linkname));
660 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
661 goto out;
662 } else if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) {
663 /*
664 * no data follows this file, so no pad
665 */
666 hd->linkflag = LNKTYPE;
667 strlcpy(hd->linkname, arcn->ln_name, sizeof(hd->linkname));
668 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
669 goto out;
670 } else {
671 /*
672 * data follows this file, so set the pad
673 */
674 hd->linkflag = AREGTYPE;
675 if (OFFT_OCT(arcn->sb.st_size, hd->size, sizeof(hd->size), 1)) {
676 tty_warn(1,"File is too large for tar %s",
677 arcn->org_name);
678 return(1);
679 }
680 arcn->pad = TAR_PAD(arcn->sb.st_size);
681 }
682
683 /*
684 * copy those fields that are independent of the type
685 */
686 if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 0) ||
687 ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 0) ||
688 ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 0) ||
689 ul_oct((u_long)arcn->sb.st_mtime, hd->mtime, sizeof(hd->mtime), 1))
690 goto out;
691
692 /*
693 * calculate and add the checksum, then write the header. A return of
694 * 0 tells the caller to now write the file data, 1 says no data needs
695 * to be written
696 */
697 if (ul_oct(tar_chksm(hdblk, sizeof(HD_TAR)), hd->chksum,
698 sizeof(hd->chksum), 3))
699 goto out; /* XXX Something's wrong here
700 * because a zero-byte file can
701 * cause this to be done and
702 * yet the resulting warning
703 * seems incorrect */
704
705 if (wr_rdbuf(hdblk, sizeof(HD_TAR)) < 0)
706 return(-1);
707 if (wr_skip((off_t)(BLKMULT - sizeof(HD_TAR))) < 0)
708 return(-1);
709 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
710 return(0);
711 return(1);
712
713 out:
714 /*
715 * header field is out of range
716 */
717 tty_warn(1, "Tar header field is too small for %s", arcn->org_name);
718 return(1);
719 }
720
721 /*
722 * Routines for POSIX ustar
723 */
724
725 /*
726 * ustar_strd()
727 * initialization for ustar read
728 * Return:
729 * 0 if ok, -1 otherwise
730 */
731
732 int
733 ustar_strd(void)
734 {
735 return(0);
736 }
737
738 /*
739 * ustar_stwr()
740 * initialization for ustar write
741 * Return:
742 * 0 if ok, -1 otherwise
743 */
744
745 int
746 ustar_stwr(void)
747 {
748 return(0);
749 }
750
751 /*
752 * ustar_id()
753 * determine if a block given to us is a valid ustar header. We have to
754 * be on the lookout for those pesky blocks of all zero's
755 * Return:
756 * 0 if a ustar header, -1 otherwise
757 */
758
759 int
760 ustar_id(char *blk, int size)
761 {
762 HD_USTAR *hd;
763
764 if (size < BLKMULT)
765 return(-1);
766 hd = (HD_USTAR *)blk;
767
768 /*
769 * check for block of zero's first, a simple and fast test then check
770 * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
771 * programs are fouled up and create archives missing the \0. Last we
772 * check the checksum. If ok we have to assume it is a valid header.
773 */
774 if (hd->name[0] == '\0')
775 return(-1);
776 if (strncmp(hd->magic, TMAGIC, TMAGLEN - 1) != 0)
777 return(-1);
778 /* This is GNU tar */
779 if (strncmp(hd->magic, "ustar ", 8) == 0 && !is_gnutar &&
780 !seen_gnu_warning) {
781 seen_gnu_warning = 1;
782 tty_warn(0,
783 "Trying to read GNU tar archive with extensions off");
784 }
785 return check_sum(hd->chksum, sizeof(hd->chksum), blk, BLKMULT, 0);
786 }
787
788 /*
789 * ustar_rd()
790 * extract the values out of block already determined to be a ustar header.
791 * store the values in the ARCHD parameter.
792 * Return:
793 * 0
794 */
795
796 int
797 ustar_rd(ARCHD *arcn, char *buf)
798 {
799 HD_USTAR *hd;
800 char *dest;
801 int cnt;
802 dev_t devmajor;
803 dev_t devminor;
804
805 /*
806 * we only get proper sized buffers
807 */
808 if (ustar_id(buf, BLKMULT) < 0)
809 return(-1);
810
811 memset(arcn, 0, sizeof(*arcn));
812 arcn->org_name = arcn->name;
813 arcn->pat = NULL;
814 arcn->sb.st_nlink = 1;
815 hd = (HD_USTAR *)buf;
816
817 /*
818 * see if the filename is split into two parts. if, so joint the parts.
819 * we copy the prefix first and add a / between the prefix and name.
820 */
821 dest = arcn->name;
822 if (*(hd->prefix) != '\0') {
823 cnt = strlcpy(arcn->name, hd->prefix, sizeof(arcn->name));
824 dest += cnt;
825 *dest++ = '/';
826 cnt++;
827 } else {
828 cnt = 0;
829 }
830
831 if (hd->typeflag != LONGLINKTYPE && hd->typeflag != LONGNAMETYPE) {
832 arcn->nlen = expandname(dest, sizeof(arcn->name) - cnt,
833 &gnu_name_string, &gnu_name_length, hd->name,
834 sizeof(hd->name)) + cnt;
835 arcn->ln_nlen = expandname(arcn->ln_name,
836 sizeof(arcn->ln_name), &gnu_link_string, &gnu_link_length,
837 hd->linkname, sizeof(hd->linkname));
838 }
839
840 /*
841 * follow the spec to the letter. we should only have mode bits, strip
842 * off all other crud we may be passed.
843 */
844 arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode, sizeof(hd->mode), OCT) &
845 0xfff);
846 arcn->sb.st_size = (off_t)ASC_OFFT(hd->size, sizeof(hd->size), OCT);
847 arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT);
848 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
849
850 /*
851 * If we can find the ascii names for gname and uname in the password
852 * and group files we will use the uid's and gid they bind. Otherwise
853 * we use the uid and gid values stored in the header. (This is what
854 * the posix spec wants).
855 */
856 hd->gname[sizeof(hd->gname) - 1] = '\0';
857 if (gid_from_group(hd->gname, &(arcn->sb.st_gid)) < 0)
858 arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
859 hd->uname[sizeof(hd->uname) - 1] = '\0';
860 if (uid_from_user(hd->uname, &(arcn->sb.st_uid)) < 0)
861 arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
862
863 /*
864 * set the defaults, these may be changed depending on the file type
865 */
866 arcn->pad = 0;
867 arcn->skip = 0;
868 arcn->sb.st_rdev = (dev_t)0;
869
870 /*
871 * set the mode and PAX type according to the typeflag in the header
872 */
873 switch(hd->typeflag) {
874 case FIFOTYPE:
875 arcn->type = PAX_FIF;
876 arcn->sb.st_mode |= S_IFIFO;
877 break;
878 case DIRTYPE:
879 arcn->type = PAX_DIR;
880 arcn->sb.st_mode |= S_IFDIR;
881 arcn->sb.st_nlink = 2;
882
883 /*
884 * Some programs that create ustar archives append a '/'
885 * to the pathname for directories. This clearly violates
886 * ustar specs, but we will silently strip it off anyway.
887 */
888 if (arcn->name[arcn->nlen - 1] == '/')
889 arcn->name[--arcn->nlen] = '\0';
890 break;
891 case BLKTYPE:
892 case CHRTYPE:
893 /*
894 * this type requires the rdev field to be set.
895 */
896 if (hd->typeflag == BLKTYPE) {
897 arcn->type = PAX_BLK;
898 arcn->sb.st_mode |= S_IFBLK;
899 } else {
900 arcn->type = PAX_CHR;
901 arcn->sb.st_mode |= S_IFCHR;
902 }
903 devmajor = (dev_t)asc_ul(hd->devmajor,sizeof(hd->devmajor),OCT);
904 devminor = (dev_t)asc_ul(hd->devminor,sizeof(hd->devminor),OCT);
905 arcn->sb.st_rdev = TODEV(devmajor, devminor);
906 break;
907 case SYMTYPE:
908 case LNKTYPE:
909 if (hd->typeflag == SYMTYPE) {
910 arcn->type = PAX_SLK;
911 arcn->sb.st_mode |= S_IFLNK;
912 } else {
913 arcn->type = PAX_HLK;
914 /*
915 * so printing looks better
916 */
917 arcn->sb.st_mode |= S_IFREG;
918 arcn->sb.st_nlink = 2;
919 }
920 break;
921 case LONGLINKTYPE:
922 case LONGNAMETYPE:
923 if (is_gnutar) {
924 /*
925 * GNU long link/file; we tag these here and let the
926 * pax internals deal with it -- too ugly otherwise.
927 */
928 if (hd->typeflag != LONGLINKTYPE)
929 arcn->type = PAX_GLF;
930 else
931 arcn->type = PAX_GLL;
932 arcn->pad = TAR_PAD(arcn->sb.st_size);
933 arcn->skip = arcn->sb.st_size;
934 } else {
935 tty_warn(1, "GNU Long %s found in posix ustar archive.",
936 hd->typeflag == LONGLINKTYPE ? "Link" : "File");
937 }
938 break;
939 case CONTTYPE:
940 case AREGTYPE:
941 case REGTYPE:
942 default:
943 /*
944 * these types have file data that follows. Set the skip and
945 * pad fields.
946 */
947 arcn->type = PAX_REG;
948 arcn->pad = TAR_PAD(arcn->sb.st_size);
949 arcn->skip = arcn->sb.st_size;
950 arcn->sb.st_mode |= S_IFREG;
951 break;
952 }
953 return(0);
954 }
955
956 static int
957 expandname(char *buf, size_t len, char **gnu_name, size_t *gnu_length,
958 const char *name, size_t nlen)
959 {
960 if (*gnu_name) {
961 len = strlcpy(buf, *gnu_name, len);
962 free(*gnu_name);
963 *gnu_name = NULL;
964 *gnu_length = 0;
965 } else {
966 if (len > ++nlen)
967 len = nlen;
968 len = strlcpy(buf, name, len);
969 }
970 return len;
971 }
972
973 static void
974 longlink(ARCHD *arcn, int type)
975 {
976 ARCHD larc;
977
978 (void)memset(&larc, 0, sizeof(larc));
979
980 larc.type = type;
981 larc.nlen = strlcpy(larc.name, LONG_LINK, sizeof(larc.name));
982
983 switch (type) {
984 case PAX_GLL:
985 gnu_hack_string = arcn->ln_name;
986 gnu_hack_len = arcn->ln_nlen + 1;
987 break;
988 case PAX_GLF:
989 gnu_hack_string = arcn->name;
990 gnu_hack_len = arcn->nlen + 1;
991 break;
992 default:
993 errx(1, "Invalid type in GNU longlink %d\n", type);
994 }
995
996 /*
997 * We need a longlink now.
998 */
999 ustar_wr(&larc);
1000 }
1001
1002 /*
1003 * ustar_wr()
1004 * write a ustar header for the file specified in the ARCHD to the archive
1005 * Have to check for file types that cannot be stored and file names that
1006 * are too long. Be careful of the term (last arg) to ul_oct, we only use
1007 * '\0' for the termination character (this is different than picky tar)
1008 * ASSUMED: space after header in header block is zero filled
1009 * Return:
1010 * 0 if file has data to be written after the header, 1 if file has NO
1011 * data to write after the header, -1 if archive write failed
1012 */
1013
1014 int
1015 ustar_wr(ARCHD *arcn)
1016 {
1017 HD_USTAR *hd;
1018 char *pt;
1019 char hdblk[sizeof(HD_USTAR)];
1020 const char *user, *group;
1021
1022 switch (arcn->type) {
1023 case PAX_SCK:
1024 /*
1025 * check for those file system types ustar cannot store
1026 */
1027 if (!is_gnutar)
1028 tty_warn(1, "Ustar cannot archive a socket %s",
1029 arcn->org_name);
1030 return(1);
1031
1032 case PAX_SLK:
1033 case PAX_HLK:
1034 case PAX_HRG:
1035 /*
1036 * check the length of the linkname
1037 */
1038 if (arcn->ln_nlen >= sizeof(hd->linkname)) {
1039 if (is_gnutar) {
1040 longlink(arcn, PAX_GLL);
1041 } else {
1042 tty_warn(1, "Link name too long for ustar %s",
1043 arcn->ln_name);
1044 return(1);
1045 }
1046 }
1047 break;
1048 default:
1049 break;
1050 }
1051
1052 /*
1053 * split the path name into prefix and name fields (if needed). if
1054 * pt != arcn->name, the name has to be split
1055 */
1056 if ((pt = name_split(arcn->name, arcn->nlen)) == NULL) {
1057 if (is_gnutar) {
1058 longlink(arcn, PAX_GLF);
1059 pt = arcn->name;
1060 } else {
1061 tty_warn(1, "File name too long for ustar %s",
1062 arcn->name);
1063 return(1);
1064 }
1065 }
1066
1067 /*
1068 * zero out the header so we don't have to worry about zero fill below
1069 */
1070 memset(hdblk, 0, sizeof(hdblk));
1071 hd = (HD_USTAR *)hdblk;
1072 arcn->pad = 0L;
1073
1074 /*
1075 * split the name, or zero out the prefix
1076 */
1077 if (pt != arcn->name) {
1078 /*
1079 * name was split, pt points at the / where the split is to
1080 * occur, we remove the / and copy the first part to the prefix
1081 */
1082 *pt = '\0';
1083 strlcpy(hd->prefix, arcn->name, sizeof(hd->prefix));
1084 *pt++ = '/';
1085 }
1086
1087 /*
1088 * copy the name part. this may be the whole path or the part after
1089 * the prefix
1090 */
1091 strlcpy(hd->name, pt, sizeof(hd->name));
1092
1093 /*
1094 * set the fields in the header that are type dependent
1095 */
1096 switch(arcn->type) {
1097 case PAX_DIR:
1098 hd->typeflag = DIRTYPE;
1099 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
1100 goto out;
1101 break;
1102 case PAX_CHR:
1103 case PAX_BLK:
1104 if (arcn->type == PAX_CHR)
1105 hd->typeflag = CHRTYPE;
1106 else
1107 hd->typeflag = BLKTYPE;
1108 if (ul_oct((u_long)MAJOR(arcn->sb.st_rdev), hd->devmajor,
1109 sizeof(hd->devmajor), 3) ||
1110 ul_oct((u_long)MINOR(arcn->sb.st_rdev), hd->devminor,
1111 sizeof(hd->devminor), 3) ||
1112 ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
1113 goto out;
1114 break;
1115 case PAX_FIF:
1116 hd->typeflag = FIFOTYPE;
1117 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
1118 goto out;
1119 break;
1120 case PAX_GLL:
1121 case PAX_SLK:
1122 case PAX_HLK:
1123 case PAX_HRG:
1124 if (arcn->type == PAX_SLK)
1125 hd->typeflag = SYMTYPE;
1126 else if (arcn->type == PAX_GLL)
1127 hd->typeflag = LONGLINKTYPE;
1128 else
1129 hd->typeflag = LNKTYPE;
1130 strlcpy(hd->linkname, arcn->ln_name, sizeof(hd->linkname));
1131 if (ul_oct((u_long)gnu_hack_len, hd->size,
1132 sizeof(hd->size), 3))
1133 goto out;
1134 break;
1135 case PAX_GLF:
1136 case PAX_REG:
1137 case PAX_CTG:
1138 default:
1139 /*
1140 * file data with this type, set the padding
1141 */
1142 if (arcn->type == PAX_GLF) {
1143 hd->typeflag = LONGNAMETYPE;
1144 arcn->pad = TAR_PAD(gnu_hack_len);
1145 if (OFFT_OCT((u_long)gnu_hack_len, hd->size,
1146 sizeof(hd->size), 3)) {
1147 tty_warn(1,"File is too long for ustar %s",
1148 arcn->org_name);
1149 return(1);
1150 }
1151 } else {
1152 if (arcn->type == PAX_CTG)
1153 hd->typeflag = CONTTYPE;
1154 else
1155 hd->typeflag = REGTYPE;
1156 arcn->pad = TAR_PAD(arcn->sb.st_size);
1157 if (OFFT_OCT(arcn->sb.st_size, hd->size,
1158 sizeof(hd->size), 3)) {
1159 tty_warn(1,"File is too long for ustar %s",
1160 arcn->org_name);
1161 return(1);
1162 }
1163 }
1164 break;
1165 }
1166
1167 strncpy(hd->magic, TMAGIC, TMAGLEN);
1168 if (is_gnutar)
1169 hd->magic[TMAGLEN - 1] = hd->magic[TMAGLEN] = ' ';
1170 else
1171 strncpy(hd->version, TVERSION, TVERSLEN);
1172
1173 /*
1174 * set the remaining fields. Some versions want all 16 bits of mode
1175 * we better humor them (they really do not meet spec though)....
1176 */
1177 if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3) ||
1178 ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 3) ||
1179 ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 3) ||
1180 ul_oct((u_long)arcn->sb.st_mtime,hd->mtime,sizeof(hd->mtime),3))
1181 goto out;
1182 user = user_from_uid(arcn->sb.st_uid, 1);
1183 group = group_from_gid(arcn->sb.st_gid, 1);
1184 strncpy(hd->uname, user ? user : "", sizeof(hd->uname));
1185 strncpy(hd->gname, group ? group : "", sizeof(hd->gname));
1186
1187 /*
1188 * calculate and store the checksum write the header to the archive
1189 * return 0 tells the caller to now write the file data, 1 says no data
1190 * needs to be written
1191 */
1192 if (ul_oct(tar_chksm(hdblk, sizeof(HD_USTAR)), hd->chksum,
1193 sizeof(hd->chksum), 3))
1194 goto out;
1195 if (wr_rdbuf(hdblk, sizeof(HD_USTAR)) < 0)
1196 return(-1);
1197 if (wr_skip((off_t)(BLKMULT - sizeof(HD_USTAR))) < 0)
1198 return(-1);
1199 if (gnu_hack_string) {
1200 int res = wr_rdbuf(gnu_hack_string, gnu_hack_len);
1201 int pad = gnu_hack_len;
1202 gnu_hack_string = NULL;
1203 gnu_hack_len = 0;
1204 if (res < 0)
1205 return(-1);
1206 if (wr_skip((off_t)(BLKMULT - (pad % BLKMULT))) < 0)
1207 return(-1);
1208 }
1209 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
1210 return(0);
1211 return(1);
1212
1213 out:
1214 /*
1215 * header field is out of range
1216 */
1217 tty_warn(1, "Ustar header field is too small for %s", arcn->org_name);
1218 return(1);
1219 }
1220
1221 /*
1222 * name_split()
1223 * see if the name has to be split for storage in a ustar header. We try
1224 * to fit the entire name in the name field without splitting if we can.
1225 * The split point is always at a /
1226 * Return
1227 * character pointer to split point (always the / that is to be removed
1228 * if the split is not needed, the points is set to the start of the file
1229 * name (it would violate the spec to split there). A NULL is returned if
1230 * the file name is too long
1231 */
1232
1233 static char *
1234 name_split(char *name, int len)
1235 {
1236 char *start;
1237
1238 /*
1239 * check to see if the file name is small enough to fit in the name
1240 * field. if so just return a pointer to the name.
1241 */
1242 if (len < TNMSZ)
1243 return(name);
1244 /*
1245 * GNU tar does not honor the prefix+name mode if the magic
1246 * is not "ustar\0". So in GNU tar compatibility mode, we don't
1247 * split the filename into prefix+name because we are setting
1248 * the magic to "ustar " as GNU tar does. This of course will
1249 * end up creating a LongLink record in cases where it does not
1250 * really need do, but we are behaving like GNU tar after all.
1251 */
1252 if (is_gnutar || len > (TPFSZ + TNMSZ))
1253 return(NULL);
1254
1255 /*
1256 * we start looking at the biggest sized piece that fits in the name
1257 * field. We walk forward looking for a slash to split at. The idea is
1258 * to find the biggest piece to fit in the name field (or the smallest
1259 * prefix we can find) (the -1 is correct the biggest piece would
1260 * include the slash between the two parts that gets thrown away)
1261 */
1262 start = name + len - TNMSZ;
1263 while ((*start != '\0') && (*start != '/'))
1264 ++start;
1265
1266 /*
1267 * if we hit the end of the string, this name cannot be split, so we
1268 * cannot store this file.
1269 */
1270 if (*start == '\0')
1271 return(NULL);
1272 len = start - name;
1273
1274 /*
1275 * NOTE: /str where the length of str == TNMSZ cannot be stored under
1276 * the p1003.1-1990 spec for ustar. We could force a prefix of / and
1277 * the file would then expand on extract to //str. The len == 0 below
1278 * makes this special case follow the spec to the letter.
1279 */
1280 if ((len >= TPFSZ) || (len == 0))
1281 return(NULL);
1282
1283 /*
1284 * ok have a split point, return it to the caller
1285 */
1286 return(start);
1287 }
1288
1289 /*
1290 * convert a glob into a RE, and add it to the list. we convert to
1291 * four different RE's (because we're using BRE's and can't use |
1292 * alternation :-() with this padding:
1293 * .*\/ and $
1294 * .*\/ and \/.*
1295 * ^ and $
1296 * ^ and \/.*
1297 */
1298 static int
1299 tar_gnutar_exclude_one(const char *line, size_t len)
1300 {
1301 /* 2 * buffer len + nul */
1302 char sbuf[MAXPATHLEN * 2 + 1];
1303 /* + / + // + .*""/\/ + \/.* */
1304 char rabuf[MAXPATHLEN * 2 + 1 + 1 + 2 + 4 + 4];
1305 int i, j;
1306
1307 if (line[len - 1] == '\n')
1308 len--;
1309 strncpy(sbuf, ".*" "\\/", j = 4);
1310 for (i = 0; i < len; i++) {
1311 /*
1312 * convert glob to regexp, escaping everything
1313 */
1314 if (line[i] == '*')
1315 sbuf[j++] = '.';
1316 else if (line[i] == '?') {
1317 sbuf[j++] = '.';
1318 continue;
1319 } else if (!isalnum((unsigned char)line[i]) &&
1320 !isblank((unsigned char)line[i]))
1321 sbuf[j++] = '\\';
1322 sbuf[j++] = line[i];
1323 }
1324 sbuf[j] = '\0';
1325 /* don't need the .*\/ ones if we start with /, i guess */
1326 if (line[0] != '/') {
1327 (void)snprintf(rabuf, sizeof rabuf, "/.*\\/%s$//", sbuf);
1328 if (rep_add(rabuf) < 0)
1329 return (-1);
1330 (void)snprintf(rabuf, sizeof rabuf, "/.*\\/%s\\/.*//", sbuf);
1331 if (rep_add(rabuf) < 0)
1332 return (-1);
1333 }
1334
1335 (void)snprintf(rabuf, sizeof rabuf, "/^%s$//", sbuf);
1336 if (rep_add(rabuf) < 0)
1337 return (-1);
1338 (void)snprintf(rabuf, sizeof rabuf, "/^%s\\/.*//", sbuf);
1339 if (rep_add(rabuf) < 0)
1340 return (-1);
1341
1342 return (0);
1343 }
1344
1345 /*
1346 * deal with GNU tar -X/--exclude-from & --exclude switchs. basically,
1347 * we go through each line of the file, building a string from the "glob"
1348 * lines in the file into RE lines, of the form `/^RE$//', which we pass
1349 * to rep_add(), which will add a empty replacement (exclusion), for the
1350 * named files.
1351 */
1352 int
1353 tar_gnutar_minus_minus_exclude(path)
1354 const char *path;
1355 {
1356 size_t len = strlen(path);
1357
1358 if (len > MAXPATHLEN)
1359 tty_warn(0, "pathname too long: %s", path);
1360
1361 return (tar_gnutar_exclude_one(path, len));
1362 }
1363
1364 int
1365 tar_gnutar_X_compat(path)
1366 const char *path;
1367 {
1368 char *line;
1369 FILE *fp;
1370 int lineno = 0;
1371 size_t len;
1372
1373 fp = fopen(path, "r");
1374 if (fp == NULL) {
1375 tty_warn(1, "cannot open %s: %s", path,
1376 strerror(errno));
1377 return(-1);
1378 }
1379
1380 while ((line = fgetln(fp, &len))) {
1381 lineno++;
1382 if (len > MAXPATHLEN) {
1383 tty_warn(0, "pathname too long, line %d of %s",
1384 lineno, path);
1385 }
1386 if (tar_gnutar_exclude_one(line, len))
1387 return (-1);
1388 }
1389 return (0);
1390 }
1391