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