Debian Codegraph @

tar.c

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