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