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