tar.c revision 1.22 1 /* $NetBSD: tar.c,v 1.22 2002/10/11 13:07:36 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.22 2002/10/11 13:07:36 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;
654 if (wr_rdbuf(hdblk, sizeof(HD_TAR)) < 0)
655 return(-1);
656 if (wr_skip((off_t)(BLKMULT - sizeof(HD_TAR))) < 0)
657 return(-1);
658 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
659 return(0);
660 return(1);
661
662 out:
663 /*
664 * header field is out of range
665 */
666 tty_warn(1, "Tar header field is too small for %s", arcn->org_name);
667 return(1);
668 }
669
670 /*
671 * Routines for POSIX ustar
672 */
673
674 /*
675 * ustar_strd()
676 * initialization for ustar read
677 * Return:
678 * 0 if ok, -1 otherwise
679 */
680
681 int
682 ustar_strd(void)
683 {
684 return(0);
685 }
686
687 /*
688 * ustar_stwr()
689 * initialization for ustar write
690 * Return:
691 * 0 if ok, -1 otherwise
692 */
693
694 int
695 ustar_stwr(void)
696 {
697 return(0);
698 }
699
700 /*
701 * ustar_id()
702 * determine if a block given to us is a valid ustar header. We have to
703 * be on the lookout for those pesky blocks of all zero's
704 * Return:
705 * 0 if a ustar header, -1 otherwise
706 */
707
708 int
709 ustar_id(char *blk, int size)
710 {
711 HD_USTAR *hd;
712
713 if (size < BLKMULT)
714 return(-1);
715 hd = (HD_USTAR *)blk;
716
717 /*
718 * check for block of zero's first, a simple and fast test then check
719 * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
720 * programs are fouled up and create archives missing the \0. Last we
721 * check the checksum. If ok we have to assume it is a valid header.
722 */
723 if (hd->name[0] == '\0')
724 return(-1);
725 if (strncmp(hd->magic, TMAGIC, TMAGLEN - 1) != 0)
726 return(-1);
727 if (!strncmp(hd->magic, "ustar ", 8))
728 is_oldgnutar = 1;
729 if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT))
730 return(-1);
731 return(0);
732 }
733
734 /*
735 * ustar_rd()
736 * extract the values out of block already determined to be a ustar header.
737 * store the values in the ARCHD parameter.
738 * Return:
739 * 0
740 */
741
742 int
743 ustar_rd(ARCHD *arcn, char *buf)
744 {
745 HD_USTAR *hd;
746 char *dest;
747 int cnt;
748 dev_t devmajor;
749 dev_t devminor;
750
751 /*
752 * we only get proper sized buffers
753 */
754 if (ustar_id(buf, BLKMULT) < 0)
755 return(-1);
756 arcn->org_name = arcn->name;
757 arcn->sb.st_nlink = 1;
758 arcn->pat = NULL;
759 arcn->nlen = 0;
760 hd = (HD_USTAR *)buf;
761
762 /*
763 * see if the filename is split into two parts. if, so joint the parts.
764 * we copy the prefix first and add a / between the prefix and name.
765 */
766 dest = arcn->name;
767 if (*(hd->prefix) != '\0') {
768 cnt = strlcpy(arcn->name, hd->prefix, sizeof(arcn->name));
769 dest += cnt;
770 *dest++ = '/';
771 cnt++;
772 }
773 arcn->nlen = cnt + strlcpy(dest, hd->name, sizeof(arcn->name) - cnt);
774
775 /*
776 * follow the spec to the letter. we should only have mode bits, strip
777 * off all other crud we may be passed.
778 */
779 arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode, sizeof(hd->mode), OCT) &
780 0xfff);
781 arcn->sb.st_size = (off_t)ASC_OFFT(hd->size, sizeof(hd->size), OCT);
782 arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT);
783 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
784
785 /*
786 * If we can find the ascii names for gname and uname in the password
787 * and group files we will use the uid's and gid they bind. Otherwise
788 * we use the uid and gid values stored in the header. (This is what
789 * the posix spec wants).
790 */
791 hd->gname[sizeof(hd->gname) - 1] = '\0';
792 if (gid_from_group(hd->gname, &(arcn->sb.st_gid)) < 0)
793 arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
794 hd->uname[sizeof(hd->uname) - 1] = '\0';
795 if (uid_from_user(hd->uname, &(arcn->sb.st_uid)) < 0)
796 arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
797
798 /*
799 * set the defaults, these may be changed depending on the file type
800 */
801 arcn->ln_name[0] = '\0';
802 arcn->ln_nlen = 0;
803 arcn->pad = 0;
804 arcn->skip = 0;
805 arcn->sb.st_rdev = (dev_t)0;
806
807 /*
808 * set the mode and PAX type according to the typeflag in the header
809 */
810 switch(hd->typeflag) {
811 case FIFOTYPE:
812 arcn->type = PAX_FIF;
813 arcn->sb.st_mode |= S_IFIFO;
814 break;
815 case DIRTYPE:
816 arcn->type = PAX_DIR;
817 arcn->sb.st_mode |= S_IFDIR;
818 arcn->sb.st_nlink = 2;
819
820 /*
821 * Some programs that create ustar archives append a '/'
822 * to the pathname for directories. This clearly violates
823 * ustar specs, but we will silently strip it off anyway.
824 */
825 if (arcn->name[arcn->nlen - 1] == '/')
826 arcn->name[--arcn->nlen] = '\0';
827 break;
828 case BLKTYPE:
829 case CHRTYPE:
830 /*
831 * this type requires the rdev field to be set.
832 */
833 if (hd->typeflag == BLKTYPE) {
834 arcn->type = PAX_BLK;
835 arcn->sb.st_mode |= S_IFBLK;
836 } else {
837 arcn->type = PAX_CHR;
838 arcn->sb.st_mode |= S_IFCHR;
839 }
840 devmajor = (dev_t)asc_ul(hd->devmajor,sizeof(hd->devmajor),OCT);
841 devminor = (dev_t)asc_ul(hd->devminor,sizeof(hd->devminor),OCT);
842 arcn->sb.st_rdev = TODEV(devmajor, devminor);
843 break;
844 case SYMTYPE:
845 case LNKTYPE:
846 if (hd->typeflag == SYMTYPE) {
847 arcn->type = PAX_SLK;
848 arcn->sb.st_mode |= S_IFLNK;
849 } else {
850 arcn->type = PAX_HLK;
851 /*
852 * so printing looks better
853 */
854 arcn->sb.st_mode |= S_IFREG;
855 arcn->sb.st_nlink = 2;
856 }
857 /*
858 * copy the link name
859 */
860 arcn->ln_nlen = strlcpy(arcn->ln_name, hd->linkname,
861 sizeof(arcn->ln_name));
862 break;
863 case CONTTYPE:
864 case AREGTYPE:
865 case REGTYPE:
866 default:
867 /*
868 * these types have file data that follows. Set the skip and
869 * pad fields.
870 */
871 arcn->type = PAX_REG;
872 arcn->pad = TAR_PAD(arcn->sb.st_size);
873 arcn->skip = arcn->sb.st_size;
874 arcn->sb.st_mode |= S_IFREG;
875 break;
876 }
877 return(0);
878 }
879
880 /*
881 * ustar_wr()
882 * write a ustar header for the file specified in the ARCHD to the archive
883 * Have to check for file types that cannot be stored and file names that
884 * are too long. Be careful of the term (last arg) to ul_oct, we only use
885 * '\0' for the termination character (this is different than picky tar)
886 * ASSUMED: space after header in header block is zero filled
887 * Return:
888 * 0 if file has data to be written after the header, 1 if file has NO
889 * data to write after the header, -1 if archive write failed
890 */
891
892 int
893 ustar_wr(ARCHD *arcn)
894 {
895 HD_USTAR *hd;
896 char *pt;
897 char hdblk[sizeof(HD_USTAR)];
898 const char *user, *group;
899
900 /*
901 * check for those file system types ustar cannot store
902 */
903 if (arcn->type == PAX_SCK) {
904 tty_warn(1, "Ustar cannot archive a socket %s", arcn->org_name);
905 return(1);
906 }
907
908 /*
909 * check the length of the linkname
910 */
911 if (((arcn->type == PAX_SLK) || (arcn->type == PAX_HLK) ||
912 (arcn->type == PAX_HRG)) && (arcn->ln_nlen >= sizeof(hd->linkname))){
913 tty_warn(1, "Link name too long for ustar %s", arcn->ln_name);
914 return(1);
915 }
916
917 /*
918 * split the path name into prefix and name fields (if needed). if
919 * pt != arcn->name, the name has to be split
920 */
921 if ((pt = name_split(arcn->name, arcn->nlen)) == NULL) {
922 tty_warn(1, "File name too long for ustar %s", arcn->name);
923 return(1);
924 }
925
926 /*
927 * zero out the header so we don't have to worry about zero fill below
928 */
929 memset(hdblk, 0, sizeof(hdblk));
930 hd = (HD_USTAR *)hdblk;
931 arcn->pad = 0L;
932
933 /*
934 * split the name, or zero out the prefix
935 */
936 if (pt != arcn->name) {
937 /*
938 * name was split, pt points at the / where the split is to
939 * occur, we remove the / and copy the first part to the prefix
940 */
941 *pt = '\0';
942 strlcpy(hd->prefix, arcn->name, sizeof(hd->prefix));
943 *pt++ = '/';
944 }
945
946 /*
947 * copy the name part. this may be the whole path or the part after
948 * the prefix
949 */
950 strlcpy(hd->name, pt, sizeof(hd->name));
951
952 /*
953 * set the fields in the header that are type dependent
954 */
955 switch(arcn->type) {
956 case PAX_DIR:
957 hd->typeflag = DIRTYPE;
958 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
959 goto out;
960 break;
961 case PAX_CHR:
962 case PAX_BLK:
963 if (arcn->type == PAX_CHR)
964 hd->typeflag = CHRTYPE;
965 else
966 hd->typeflag = BLKTYPE;
967 if (ul_oct((u_long)MAJOR(arcn->sb.st_rdev), hd->devmajor,
968 sizeof(hd->devmajor), 3) ||
969 ul_oct((u_long)MINOR(arcn->sb.st_rdev), hd->devminor,
970 sizeof(hd->devminor), 3) ||
971 ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
972 goto out;
973 break;
974 case PAX_FIF:
975 hd->typeflag = FIFOTYPE;
976 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
977 goto out;
978 break;
979 case PAX_SLK:
980 case PAX_HLK:
981 case PAX_HRG:
982 if (arcn->type == PAX_SLK)
983 hd->typeflag = SYMTYPE;
984 else
985 hd->typeflag = LNKTYPE;
986 strlcpy(hd->linkname, arcn->ln_name, sizeof(hd->linkname));
987 if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
988 goto out;
989 break;
990 case PAX_REG:
991 case PAX_CTG:
992 default:
993 /*
994 * file data with this type, set the padding
995 */
996 if (arcn->type == PAX_CTG)
997 hd->typeflag = CONTTYPE;
998 else
999 hd->typeflag = REGTYPE;
1000 arcn->pad = TAR_PAD(arcn->sb.st_size);
1001 if (OFFT_OCT(arcn->sb.st_size, hd->size, sizeof(hd->size), 3)) {
1002 tty_warn(1,"File is too long for ustar %s",
1003 arcn->org_name);
1004 return(1);
1005 }
1006 break;
1007 }
1008
1009 strncpy(hd->magic, TMAGIC, TMAGLEN);
1010 strncpy(hd->version, TVERSION, TVERSLEN);
1011
1012 /*
1013 * set the remaining fields. Some versions want all 16 bits of mode
1014 * we better humor them (they really do not meet spec though)....
1015 */
1016 if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3) ||
1017 ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 3) ||
1018 ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 3) ||
1019 ul_oct((u_long)arcn->sb.st_mtime,hd->mtime,sizeof(hd->mtime),3))
1020 goto out;
1021 user = user_from_uid(arcn->sb.st_uid, 1);
1022 group = group_from_gid(arcn->sb.st_gid, 1);
1023 strncpy(hd->uname, user ? user : "", sizeof(hd->uname));
1024 strncpy(hd->gname, group ? group : "", sizeof(hd->gname));
1025
1026 /*
1027 * calculate and store the checksum write the header to the archive
1028 * return 0 tells the caller to now write the file data, 1 says no data
1029 * needs to be written
1030 */
1031 if (ul_oct(tar_chksm(hdblk, sizeof(HD_USTAR)), hd->chksum,
1032 sizeof(hd->chksum), 3))
1033 goto out;
1034 if (wr_rdbuf(hdblk, sizeof(HD_USTAR)) < 0)
1035 return(-1);
1036 if (wr_skip((off_t)(BLKMULT - sizeof(HD_USTAR))) < 0)
1037 return(-1);
1038 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
1039 return(0);
1040 return(1);
1041
1042 out:
1043 /*
1044 * header field is out of range
1045 */
1046 tty_warn(1, "Ustar header field is too small for %s", arcn->org_name);
1047 return(1);
1048 }
1049
1050 /*
1051 * name_split()
1052 * see if the name has to be split for storage in a ustar header. We try
1053 * to fit the entire name in the name field without splitting if we can.
1054 * The split point is always at a /
1055 * Return
1056 * character pointer to split point (always the / that is to be removed
1057 * if the split is not needed, the points is set to the start of the file
1058 * name (it would violate the spec to split there). A NULL is returned if
1059 * the file name is too long
1060 */
1061
1062 static char *
1063 name_split(char *name, int len)
1064 {
1065 char *start;
1066
1067 /*
1068 * check to see if the file name is small enough to fit in the name
1069 * field. if so just return a pointer to the name.
1070 */
1071 if (len < TNMSZ)
1072 return(name);
1073 if (len > (TPFSZ + TNMSZ))
1074 return(NULL);
1075
1076 /*
1077 * we start looking at the biggest sized piece that fits in the name
1078 * field. We walk forward looking for a slash to split at. The idea is
1079 * to find the biggest piece to fit in the name field (or the smallest
1080 * prefix we can find) (the -1 is correct the biggest piece would
1081 * include the slash between the two parts that gets thrown away)
1082 */
1083 start = name + len - TNMSZ;
1084 while ((*start != '\0') && (*start != '/'))
1085 ++start;
1086
1087 /*
1088 * if we hit the end of the string, this name cannot be split, so we
1089 * cannot store this file.
1090 */
1091 if (*start == '\0')
1092 return(NULL);
1093 len = start - name;
1094
1095 /*
1096 * NOTE: /str where the length of str == TNMSZ can not be stored under
1097 * the p1003.1-1990 spec for ustar. We could force a prefix of / and
1098 * the file would then expand on extract to //str. The len == 0 below
1099 * makes this special case follow the spec to the letter.
1100 */
1101 if ((len >= TPFSZ) || (len == 0))
1102 return(NULL);
1103
1104 /*
1105 * ok have a split point, return it to the caller
1106 */
1107 return(start);
1108 }
1109
1110 /*
1111 * deal with GNU tar -X switch. basically, we go through each line of
1112 * the file, building a string from the "glob" lines in the file into
1113 * RE lines, of the form `/^RE$//', which we pass to rep_add(), which
1114 * will add a empty replacement (exclusion), for the named files.
1115 */
1116 int
1117 tar_gnutar_X_compat(path)
1118 const char *path;
1119 {
1120 char *line, sbuf[MAXPATHLEN * 2 + 1 + 5];
1121 FILE *fp;
1122 int lineno = 0, i, j;
1123 size_t len;
1124
1125 fp = fopen(path, "r");
1126 if (fp == NULL) {
1127 tty_warn(1, "can not open %s: %s", path,
1128 strerror(errno));
1129 return(-1);
1130 }
1131
1132 while ((line = fgetln(fp, &len))) {
1133 lineno++;
1134 if (len > MAXPATHLEN) {
1135 tty_warn(0, "pathname too long, line %d of %s",
1136 lineno, path);
1137 }
1138 if (line[len - 1] == '\n')
1139 len--;
1140 for (i = 0, j = 2; i < len; i++) {
1141 /*
1142 * convert glob to regexp, escaping everything
1143 */
1144 if (line[i] == '*')
1145 sbuf[j++] = '.';
1146 else if (line[i] == '?')
1147 line[i] = '.';
1148 else if (!isalnum(line[i]) && !isblank(line[i]))
1149 sbuf[j++] = '\\';
1150 sbuf[j++] = line[i];
1151 }
1152 sbuf[0] = sbuf[j + 1] = sbuf[j + 2] = '/';
1153 sbuf[1] = '^';
1154 sbuf[j] = '$';
1155 sbuf[j + 3] = '\0';
1156 if (rep_add(sbuf) < 0)
1157 return (-1);
1158 }
1159 return (0);
1160 }
1161