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