]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/cpio/src/tar.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / cpio / src / tar.c
1 /* $FreeBSD$ */
2
3 /* tar.c - read in write tar headers for cpio
4    Copyright (C) 1992, 2001, 2004 Free Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License along
17    with this program; if not, write to the Free Software Foundation, Inc.,
18    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include <system.h>
21
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include "filetypes.h"
26 #include "cpiohdr.h"
27 #include "dstring.h"
28 #include "extern.h"
29 #include <rmt.h>
30 #include "tarhdr.h"
31
32 /* Stash the tar linkname in static storage.  */
33
34 static char *
35 stash_tar_linkname (char *linkname)
36 {
37   static char hold_tar_linkname[TARLINKNAMESIZE + 1];
38
39   strncpy (hold_tar_linkname, linkname, TARLINKNAMESIZE);
40   hold_tar_linkname[TARLINKNAMESIZE] = '\0';
41   return hold_tar_linkname;
42 }
43
44 /* Try to split a long file name into prefix and suffix parts separated
45    by a slash. Return the length of the prefix (not counting the slash). */
46    
47 static size_t
48 split_long_name (const char *name, size_t length)
49 {
50   size_t i;
51
52   if (length > TARPREFIXSIZE)
53     length = TARPREFIXSIZE+2;
54   for (i = length - 1; i > 0; i--)
55     if (name[i] == '/')
56       break;
57   return i;
58 }
59
60 /* Stash the tar filename and optional prefix in static storage.  */
61
62 static char *
63 stash_tar_filename (char *prefix, char *filename)
64 {
65   static char hold_tar_filename[TARNAMESIZE + TARPREFIXSIZE + 2];
66   if (prefix == NULL || *prefix == '\0')
67     {
68       strncpy (hold_tar_filename, filename, TARNAMESIZE);
69       hold_tar_filename[TARNAMESIZE] = '\0';
70     }
71   else
72     {
73       strncpy (hold_tar_filename, prefix, TARPREFIXSIZE);
74       hold_tar_filename[TARPREFIXSIZE] = '\0';
75       strcat (hold_tar_filename, "/");
76       strncat (hold_tar_filename, filename, TARNAMESIZE);
77       hold_tar_filename[TARPREFIXSIZE + TARNAMESIZE] = '\0';
78     }
79   return hold_tar_filename;
80 }
81
82 /* Convert a number into a string of octal digits.
83    Convert long VALUE into a DIGITS-digit field at WHERE,
84    including a trailing space and room for a NUL.  DIGITS==3 means
85    1 digit, a space, and room for a NUL.
86
87    We assume the trailing NUL is already there and don't fill it in.
88    This fact is used by start_header and finish_header, so don't change it!
89
90    This is be equivalent to:
91    sprintf (where, "%*lo ", digits - 2, value);
92    except that sprintf fills in the trailing NUL and we don't.  */
93
94 static void
95 to_oct (register long value, register int digits, register char *where)
96 {
97   --digits;                     /* Leave the trailing NUL slot alone.  */
98
99   /* Produce the digits -- at least one.  */
100   do
101     {
102       where[--digits] = '0' + (char) (value & 7); /* One octal digit.  */
103       value >>= 3;
104     }
105   while (digits > 0 && value != 0);
106
107   /* Add leading zeroes, if necessary.  */
108   while (digits > 0)
109     where[--digits] = '0';
110 }
111
112 \f
113
114 /* Compute and return a checksum for TAR_HDR,
115    counting the checksum bytes as if they were spaces.  */
116
117 unsigned long
118 tar_checksum (struct tar_header *tar_hdr)
119 {
120   unsigned long sum = 0;
121   char *p = (char *) tar_hdr;
122   char *q = p + TARRECORDSIZE;
123   int i;
124
125   while (p < tar_hdr->chksum)
126     sum += *p++ & 0xff;
127   for (i = 0; i < 8; ++i)
128     {
129       sum += ' ';
130       ++p;
131     }
132   while (p < q)
133     sum += *p++ & 0xff;
134   return sum;
135 }
136
137 /* Write out header FILE_HDR, including the file name, to file
138    descriptor OUT_DES.  */
139
140 void
141 write_out_tar_header (struct new_cpio_header *file_hdr, int out_des)
142 {
143   int name_len;
144   union tar_record tar_rec;
145   struct tar_header *tar_hdr = (struct tar_header *) &tar_rec;
146
147   bzero ((char *) &tar_rec, TARRECORDSIZE);
148
149   /* process_copy_out must ensure that file_hdr->c_name is short enough,
150      or we will lose here.  */
151
152   name_len = strlen (file_hdr->c_name);
153   if (name_len <= TARNAMESIZE)
154     {
155       strncpy (tar_hdr->name, file_hdr->c_name, name_len);
156     }
157   else
158     {
159       /* Fit as much as we can into `name', the rest into `prefix'.  */
160       int prefix_len = split_long_name (file_hdr->c_name, name_len);
161
162       strncpy (tar_hdr->prefix, file_hdr->c_name, prefix_len);
163       strncpy (tar_hdr->name, file_hdr->c_name + prefix_len + 1,
164                name_len - prefix_len - 1);
165     }
166
167   /* Ustar standard (POSIX.1-1988) requires the mode to contain only 3 octal
168      digits */
169   to_oct (file_hdr->c_mode & MODE_ALL, 8, tar_hdr->mode);
170   to_oct (file_hdr->c_uid, 8, tar_hdr->uid);
171   to_oct (file_hdr->c_gid, 8, tar_hdr->gid);
172   to_oct (file_hdr->c_filesize, 12, tar_hdr->size);
173   to_oct (file_hdr->c_mtime, 12, tar_hdr->mtime);
174
175   switch (file_hdr->c_mode & CP_IFMT)
176     {
177     case CP_IFREG:
178       if (file_hdr->c_tar_linkname)
179         {
180           /* process_copy_out makes sure that c_tar_linkname is shorter
181              than TARLINKNAMESIZE.  */
182           strncpy (tar_hdr->linkname, file_hdr->c_tar_linkname,
183                    TARLINKNAMESIZE);
184           tar_hdr->typeflag = LNKTYPE;
185           to_oct (0, 12, tar_hdr->size);
186         }
187       else
188         tar_hdr->typeflag = REGTYPE;
189       break;
190     case CP_IFDIR:
191       tar_hdr->typeflag = DIRTYPE;
192       break;
193     case CP_IFCHR:
194       tar_hdr->typeflag = CHRTYPE;
195       break;
196     case CP_IFBLK:
197       tar_hdr->typeflag = BLKTYPE;
198       break;
199 #ifdef CP_IFIFO
200     case CP_IFIFO:
201       tar_hdr->typeflag = FIFOTYPE;
202       break;
203 #endif /* CP_IFIFO */
204 #ifdef CP_IFLNK
205     case CP_IFLNK:
206       tar_hdr->typeflag = SYMTYPE;
207       /* process_copy_out makes sure that c_tar_linkname is shorter
208          than TARLINKNAMESIZE.  */
209       strncpy (tar_hdr->linkname, file_hdr->c_tar_linkname,
210                TARLINKNAMESIZE);
211       to_oct (0, 12, tar_hdr->size);
212       break;
213 #endif /* CP_IFLNK */
214     }
215
216   if (archive_format == arf_ustar)
217     {
218       char *name;
219
220       strncpy (tar_hdr->magic, TMAGIC, TMAGLEN);
221       strncpy (tar_hdr->magic + TMAGLEN, TVERSION, TVERSLEN);
222
223       name = getuser (file_hdr->c_uid);
224       if (name)
225         strcpy (tar_hdr->uname, name);
226       name = getgroup (file_hdr->c_gid);
227       if (name)
228         strcpy (tar_hdr->gname, name);
229
230       to_oct (file_hdr->c_rdev_maj, 8, tar_hdr->devmajor);
231       to_oct (file_hdr->c_rdev_min, 8, tar_hdr->devminor);
232     }
233
234   to_oct (tar_checksum (tar_hdr), 8, tar_hdr->chksum);
235
236   tape_buffered_write ((char *) &tar_rec, out_des, TARRECORDSIZE);
237 }
238
239 /* Return nonzero iff all the bytes in BLOCK are NUL.
240    SIZE is the number of bytes to check in BLOCK; it must be a
241    multiple of sizeof (long).  */
242
243 int
244 null_block (long *block, int size)
245 {
246   register long *p = block;
247   register int i = size / sizeof (long);
248
249   while (i--)
250     if (*p++)
251       return 0;
252   return 1;
253 }
254
255 /* Read a tar header, including the file name, from file descriptor IN_DES
256    into FILE_HDR.  */
257
258 void
259 read_in_tar_header (struct new_cpio_header *file_hdr, int in_des)
260 {
261   long bytes_skipped = 0;
262   int warned = false;
263   union tar_record tar_rec;
264   struct tar_header *tar_hdr = (struct tar_header *) &tar_rec;
265   uid_t *uidp;
266   gid_t *gidp;
267
268   tape_buffered_read ((char *) &tar_rec, in_des, TARRECORDSIZE);
269
270   /* Check for a block of 0's.  */
271   if (null_block ((long *) &tar_rec, TARRECORDSIZE))
272     {
273 #if 0
274       /* Found one block of 512 0's.  If the next block is also all 0's
275          then this is the end of the archive.  If not, assume the
276          previous block was all corruption and continue reading
277          the archive.  */
278       /* Commented out because GNU tar sometimes creates archives with
279          only one block of 0's at the end.  This happened for the
280          cpio 2.0 distribution!  */
281       tape_buffered_read ((char *) &tar_rec, in_des, TARRECORDSIZE);
282       if (null_block ((long *) &tar_rec, TARRECORDSIZE))
283 #endif
284         {
285           file_hdr->c_name = "TRAILER!!!";
286           return;
287         }
288 #if 0
289       bytes_skipped = TARRECORDSIZE;
290 #endif
291     }
292
293   while (1)
294     {
295       otoa (tar_hdr->chksum, &file_hdr->c_chksum);
296
297       if (file_hdr->c_chksum != tar_checksum (tar_hdr))
298         {
299           /* If the checksum is bad, skip 1 byte and try again.  When
300              we try again we do not look for an EOF record (all zeros),
301              because when we start skipping bytes in a corrupted archive
302              the chances are pretty good that we might stumble across
303              2 blocks of 512 zeros (that probably is not really the last
304              record) and it is better to miss the EOF and give the user
305              a "premature EOF" error than to give up too soon on a corrupted
306              archive.  */
307           if (!warned)
308             {
309               error (0, 0, _("invalid header: checksum error"));
310               warned = true;
311             }
312           bcopy (((char *) &tar_rec) + 1, (char *) &tar_rec,
313                  TARRECORDSIZE - 1);
314           tape_buffered_read (((char *) &tar_rec) + (TARRECORDSIZE - 1), in_des, 1);
315           ++bytes_skipped;
316           continue;
317         }
318
319       if (archive_format != arf_ustar)
320         file_hdr->c_name = stash_tar_filename (NULL, tar_hdr->name);
321       else
322         file_hdr->c_name = stash_tar_filename (tar_hdr->prefix, tar_hdr->name);
323       file_hdr->c_nlink = 1;
324       otoa (tar_hdr->mode, &file_hdr->c_mode);
325       file_hdr->c_mode = file_hdr->c_mode & 07777;
326   /* Debian hack: This version of cpio uses the -n flag also to extract
327      tar archives using the numeric UID/GID instead of the user/group
328      names in /etc/passwd and /etc/groups.  (98/10/15) -BEM */
329       if (archive_format == arf_ustar && !numeric_uid
330           && (uidp = getuidbyname (tar_hdr->uname)))
331         file_hdr->c_uid = *uidp;
332       else
333         otoa (tar_hdr->uid, &file_hdr->c_uid);
334
335       if (archive_format == arf_ustar && !numeric_uid
336           && (gidp = getgidbyname (tar_hdr->gname)))
337         file_hdr->c_gid = *gidp;
338       else
339         otoa (tar_hdr->gid, &file_hdr->c_gid);
340       otoa (tar_hdr->size, &file_hdr->c_filesize);
341       otoa (tar_hdr->mtime, &file_hdr->c_mtime);
342       otoa (tar_hdr->devmajor, (unsigned long *) &file_hdr->c_rdev_maj);
343       otoa (tar_hdr->devminor, (unsigned long *) &file_hdr->c_rdev_min);
344       file_hdr->c_tar_linkname = NULL;
345
346       switch (tar_hdr->typeflag)
347         {
348         case REGTYPE:
349         case CONTTYPE:          /* For now, punt.  */
350         default:
351           file_hdr->c_mode |= CP_IFREG;
352           break;
353         case DIRTYPE:
354           file_hdr->c_mode |= CP_IFDIR;
355           break;
356         case CHRTYPE:
357           file_hdr->c_mode |= CP_IFCHR;
358           /* If a POSIX tar header has a valid linkname it's always supposed
359              to set typeflag to be LNKTYPE.  System V.4 tar seems to
360              be broken, and for device files with multiple links it
361              puts the name of the link into linkname, but leaves typeflag 
362              as CHRTYPE, BLKTYPE, FIFOTYPE, etc.  */
363           file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
364
365           /* Does POSIX say that the filesize must be 0 for devices?  We
366              assume so, but HPUX's POSIX tar sets it to be 1 which causes
367              us problems (when reading an archive we assume we can always
368              skip to the next file by skipping filesize bytes).  For 
369              now at least, it's easier to clear filesize for devices,
370              rather than check everywhere we skip in copyin.c.  */
371           file_hdr->c_filesize = 0;
372           break;
373         case BLKTYPE:
374           file_hdr->c_mode |= CP_IFBLK;
375           file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
376           file_hdr->c_filesize = 0;
377           break;
378 #ifdef CP_IFIFO
379         case FIFOTYPE:
380           file_hdr->c_mode |= CP_IFIFO;
381           file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
382           file_hdr->c_filesize = 0;
383           break;
384 #endif
385         case SYMTYPE:
386 #ifdef CP_IFLNK
387           file_hdr->c_mode |= CP_IFLNK;
388           file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
389           file_hdr->c_filesize = 0;
390           break;
391           /* Else fall through.  */
392 #endif
393         case LNKTYPE:
394           file_hdr->c_mode |= CP_IFREG;
395           file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
396           file_hdr->c_filesize = 0;
397           break;
398
399         case AREGTYPE:
400           /* Old tar format; if the last char in filename is '/' then it is
401              a directory, otherwise it's a regular file.  */
402           if (file_hdr->c_name[strlen (file_hdr->c_name) - 1] == '/')
403             file_hdr->c_mode |= CP_IFDIR;
404           else
405             file_hdr->c_mode |= CP_IFREG;
406           break;
407         case 'x': case 'g':
408           /* Ignore pax 'x' and 'g' extension entries. */
409           /* Skip body of this entry. */
410           while (file_hdr->c_filesize > 0) {
411             tape_buffered_read(((char *) &tar_rec), in_des, TARRECORDSIZE);
412             if (file_hdr->c_filesize > TARRECORDSIZE)
413                     file_hdr->c_filesize -= TARRECORDSIZE;
414             else
415                     file_hdr->c_filesize = 0;
416           }
417           /* Read next header and return that instead. */
418           read_in_tar_header(file_hdr, in_des);
419           break;
420         }
421       break;
422     }
423   if (bytes_skipped > 0)
424     warn_junk_bytes (bytes_skipped);
425 }
426
427 /* Convert the string of octal digits S into a number and store
428    it in *N.  Return nonzero if the whole string was converted,
429    zero if there was something after the number.
430    Skip leading and trailing spaces.  */
431
432 int
433 otoa (char *s, unsigned long *n)
434 {
435   unsigned long val = 0;
436
437   while (*s == ' ')
438     ++s;
439   while (*s >= '0' && *s <= '7')
440     val = 8 * val + *s++ - '0';
441   while (*s == ' ')
442     ++s;
443   *n = val;
444   return *s == '\0';
445 }
446
447 /* Return
448    2 if BUF is a valid POSIX tar header (the checksum is correct
449    and it has the "ustar" magic string),
450    1 if BUF is a valid old tar header (the checksum is correct),
451    0 otherwise.  */
452
453 int
454 is_tar_header (char *buf)
455 {
456   struct tar_header *tar_hdr = (struct tar_header *) buf;
457   unsigned long chksum;
458
459   otoa (tar_hdr->chksum, &chksum);
460
461   if (chksum != tar_checksum (tar_hdr))
462     return 0;
463
464   /* GNU tar 1.10 and previous set the magic field to be "ustar " instead
465      of "ustar\0".  Only look at the first 5 characters of the magic
466      field so we can recognize old GNU tar ustar archives.  */
467   if (!strncmp (tar_hdr->magic, TMAGIC, TMAGLEN - 1))
468       return 2;
469   return 1;
470 }
471
472 /* Return true if the filename is too long to fit in a tar header.
473    For old tar headers, if the filename's length is less than or equal
474    to 100 then it will fit, otherwise it will not.  For POSIX tar headers,
475    if the filename's length is less than or equal to 100 then it
476    will definitely fit, and if it is greater than 256 then it
477    will definitely not fit.  If the length is between 100 and 256,
478    then the filename will fit only if it is possible to break it
479    into a 155 character "prefix" and 100 character "name".  There
480    must be a slash between the "prefix" and the "name", although
481    the slash is not stored or counted in either the "prefix" or
482    the "name", and there must be at least one character in both
483    the "prefix" and the "name".  If it is not possible to break down
484    the filename like this then it will not fit.  */
485
486 int
487 is_tar_filename_too_long (char *name)
488 {
489   int whole_name_len;
490   int prefix_name_len;
491   char *p;
492
493   whole_name_len = strlen (name);
494   if (whole_name_len <= TARNAMESIZE)
495     return false;
496
497   if (archive_format != arf_ustar)
498     return true;
499
500   if (whole_name_len > TARNAMESIZE + TARPREFIXSIZE + 1)
501     return true;
502
503   /* See whether we can split up the name into acceptably-sized
504      `prefix' and `name' (`p') pieces. */
505   prefix_name_len = split_long_name (name, whole_name_len);
506
507   /* Interestingly, a name consisting of a slash followed by
508      TARNAMESIZE characters can't be stored, because the prefix
509      would be empty, and thus ignored.  */
510   if (prefix_name_len == 0
511       || whole_name_len - prefix_name_len - 1 > TARNAMESIZE)
512     return true;
513
514   return false;
515 }