Import of libarchive and bsdtar 1.3.1
[dragonfly.git] / contrib / libarchive-1.3.1 / tar / read.c
1 /*-
2  * Copyright (c) 2003-2004 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "bsdtar_platform.h"
28 __FBSDID("$FreeBSD: src/usr.bin/tar/read.c,v 1.26 2006/07/30 18:34:40 kientzle Exp $");
29
30 #ifdef MAJOR_IN_MKDEV
31 #include <sys/mkdev.h>
32 #endif
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36
37 #include <errno.h>
38 #include <grp.h>
39 #include <limits.h>
40 #include <pwd.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <time.h>
45 #include <unistd.h>
46
47 #include "bsdtar.h"
48
49 static void     cleanup_security(struct bsdtar *);
50 static void     list_item_verbose(struct bsdtar *, FILE *,
51                     struct archive_entry *);
52 static void     read_archive(struct bsdtar *bsdtar, char mode);
53 static int      security_problem(struct bsdtar *, struct archive_entry *);
54
55 void
56 tar_mode_t(struct bsdtar *bsdtar)
57 {
58         read_archive(bsdtar, 't');
59 }
60
61 void
62 tar_mode_x(struct bsdtar *bsdtar)
63 {
64         read_archive(bsdtar, 'x');
65 }
66
67 /*
68  * Handle 'x' and 't' modes.
69  */
70 static void
71 read_archive(struct bsdtar *bsdtar, char mode)
72 {
73         FILE                     *out;
74         struct archive           *a;
75         struct archive_entry     *entry;
76         const struct stat        *st;
77         int                       r;
78
79         while (*bsdtar->argv) {
80                 include(bsdtar, *bsdtar->argv);
81                 bsdtar->argv++;
82         }
83
84         if (bsdtar->names_from_file != NULL)
85                 include_from_file(bsdtar, bsdtar->names_from_file);
86
87         a = archive_read_new();
88         archive_read_support_compression_all(a);
89         archive_read_support_format_all(a);
90         if (archive_read_open_file(a, bsdtar->filename,
91             bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
92             DEFAULT_BYTES_PER_BLOCK))
93                 bsdtar_errc(bsdtar, 1, 0, "Error opening archive: %s",
94                     archive_error_string(a));
95
96         do_chdir(bsdtar);
97         for (;;) {
98                 /* Support --fast-read option */
99                 if (bsdtar->option_fast_read &&
100                     unmatched_inclusions(bsdtar) == 0)
101                         break;
102
103                 r = archive_read_next_header(a, &entry);
104                 if (r == ARCHIVE_EOF)
105                         break;
106                 if (r == ARCHIVE_WARN)
107                         bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
108                 if (r == ARCHIVE_FATAL) {
109                         bsdtar->return_value = 1;
110                         bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
111                         break;
112                 }
113                 if (r == ARCHIVE_RETRY) {
114                         /* Retryable error: try again */
115                         bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
116                         bsdtar_warnc(bsdtar, 0, "Retrying...");
117                         continue;
118                 }
119
120                 /*
121                  * Exclude entries that are too old.
122                  */
123                 st = archive_entry_stat(entry);
124                 if (bsdtar->newer_ctime_sec > 0) {
125                         if (st->st_ctime < bsdtar->newer_ctime_sec)
126                                 continue; /* Too old, skip it. */
127                         if (st->st_ctime == bsdtar->newer_ctime_sec
128                             && ARCHIVE_STAT_CTIME_NANOS(st)
129                             <= bsdtar->newer_ctime_nsec)
130                                 continue; /* Too old, skip it. */
131                 }
132                 if (bsdtar->newer_mtime_sec > 0) {
133                         if (st->st_mtime < bsdtar->newer_mtime_sec)
134                                 continue; /* Too old, skip it. */
135                         if (st->st_mtime == bsdtar->newer_mtime_sec
136                             && ARCHIVE_STAT_MTIME_NANOS(st)
137                             <= bsdtar->newer_mtime_nsec)
138                                 continue; /* Too old, skip it. */
139                 }
140
141                 /*
142                  * Note that pattern exclusions are checked before
143                  * pathname rewrites are handled.  This gives more
144                  * control over exclusions, since rewrites always lose
145                  * information.  (For example, consider a rewrite
146                  * s/foo[0-9]/foo/.  If we check exclusions after the
147                  * rewrite, there would be no way to exclude foo1/bar
148                  * while allowing foo2/bar.)
149                  */
150                 if (excluded(bsdtar, archive_entry_pathname(entry)))
151                         continue; /* Excluded by a pattern test. */
152
153                 /*
154                  * Modify the pathname as requested by the user.  We
155                  * do this for -t as well to give users a way to
156                  * preview the effects of their rewrites.  We also do
157                  * this before extraction security checks (including
158                  * leading '/' removal).  Note that some rewrite
159                  * failures prevent extraction.
160                  */
161                 if (edit_pathname(bsdtar, entry))
162                         continue; /* Excluded by a rewrite failure. */
163
164                 if (mode == 't') {
165                         /* Perversely, gtar uses -O to mean "send to stderr"
166                          * when used with -t. */
167                         out = bsdtar->option_stdout ? stderr : stdout;
168
169                         if (bsdtar->verbose < 2)
170                                 safe_fprintf(out, "%s",
171                                     archive_entry_pathname(entry));
172                         else
173                                 list_item_verbose(bsdtar, out, entry);
174                         fflush(out);
175                         r = archive_read_data_skip(a);
176                         if (r == ARCHIVE_WARN) {
177                                 fprintf(out, "\n");
178                                 bsdtar_warnc(bsdtar, 0, "%s",
179                                     archive_error_string(a));
180                         }
181                         if (r == ARCHIVE_RETRY) {
182                                 fprintf(out, "\n");
183                                 bsdtar_warnc(bsdtar, 0, "%s",
184                                     archive_error_string(a));
185                         }
186                         if (r == ARCHIVE_FATAL) {
187                                 fprintf(out, "\n");
188                                 bsdtar_warnc(bsdtar, 0, "%s",
189                                     archive_error_string(a));
190                                 break;
191                         }
192                         fprintf(out, "\n");
193                 } else {
194                         /*
195                          * Skip security problems before prompting.
196                          * Otherwise, the user may be confused that a
197                          * file they wanted to extract was
198                          * subsequently skipped.
199                          */
200                         if (security_problem(bsdtar, entry))
201                                 continue;
202
203                         if (bsdtar->option_interactive &&
204                             !yes("extract '%s'", archive_entry_pathname(entry)))
205                                 continue;
206
207                         /*
208                          * Format here is from SUSv2, including the
209                          * deferred '\n'.
210                          */
211                         if (bsdtar->verbose) {
212                                 safe_fprintf(stderr, "x %s",
213                                     archive_entry_pathname(entry));
214                                 fflush(stderr);
215                         }
216                         if (bsdtar->option_stdout) {
217                                 /* TODO: Catch/recover any errors here. */
218                                 archive_read_data_into_fd(a, 1);
219                         } else if (archive_read_extract(a, entry,
220                                        bsdtar->extract_flags)) {
221                                 if (!bsdtar->verbose)
222                                         safe_fprintf(stderr, "%s",
223                                             archive_entry_pathname(entry));
224                                 safe_fprintf(stderr, ": %s",
225                                     archive_error_string(a));
226                                 if (!bsdtar->verbose)
227                                         fprintf(stderr, "\n");
228                                 /*
229                                  * TODO: Decide how to handle
230                                  * extraction error... <sigh>
231                                  */
232                                 bsdtar->return_value = 1;
233                         }
234                         if (bsdtar->verbose)
235                                 fprintf(stderr, "\n");
236                 }
237         }
238
239         if (bsdtar->verbose > 2)
240                 fprintf(stdout, "Archive Format: %s,  Compression: %s\n",
241                     archive_format_name(a), archive_compression_name(a));
242
243         archive_read_finish(a);
244         cleanup_security(bsdtar);
245 }
246
247
248 /*
249  * Display information about the current file.
250  *
251  * The format here roughly duplicates the output of 'ls -l'.
252  * This is based on SUSv2, where 'tar tv' is documented as
253  * listing additional information in an "unspecified format,"
254  * and 'pax -l' is documented as using the same format as 'ls -l'.
255  */
256 static void
257 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
258 {
259         const struct stat       *st;
260         char                     tmp[100];
261         size_t                   w;
262         const char              *p;
263         const char              *fmt;
264         time_t                   tim;
265         static time_t            now;
266
267         st = archive_entry_stat(entry);
268
269         /*
270          * We avoid collecting the entire list in memory at once by
271          * listing things as we see them.  However, that also means we can't
272          * just pre-compute the field widths.  Instead, we start with guesses
273          * and just widen them as necessary.  These numbers are completely
274          * arbitrary.
275          */
276         if (!bsdtar->u_width) {
277                 bsdtar->u_width = 6;
278                 bsdtar->gs_width = 13;
279         }
280         if (!now)
281                 time(&now);
282         bsdtar_strmode(entry, tmp);
283         fprintf(out, "%s %d ", tmp, (int)(st->st_nlink));
284
285         /* Use uname if it's present, else uid. */
286         p = archive_entry_uname(entry);
287         if ((p == NULL) || (*p == '\0')) {
288                 sprintf(tmp, "%d ", st->st_uid);
289                 p = tmp;
290         }
291         w = strlen(p);
292         if (w > bsdtar->u_width)
293                 bsdtar->u_width = w;
294         fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
295
296         /* Use gname if it's present, else gid. */
297         p = archive_entry_gname(entry);
298         if (p != NULL && p[0] != '\0') {
299                 fprintf(out, "%s", p);
300                 w = strlen(p);
301         } else {
302                 sprintf(tmp, "%d", st->st_gid);
303                 w = strlen(tmp);
304                 fprintf(out, "%s", tmp);
305         }
306
307         /*
308          * Print device number or file size, right-aligned so as to make
309          * total width of group and devnum/filesize fields be gs_width.
310          * If gs_width is too small, grow it.
311          */
312         if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
313                 sprintf(tmp, "%d,%u",
314                     major(st->st_rdev),
315                     (unsigned)minor(st->st_rdev)); /* ls(1) also casts here. */
316         } else {
317                 /*
318                  * Note the use of platform-dependent macros to format
319                  * the filesize here.  We need the format string and the
320                  * corresponding type for the cast.
321                  */
322                 sprintf(tmp, BSDTAR_FILESIZE_PRINTF,
323                     (BSDTAR_FILESIZE_TYPE)st->st_size);
324         }
325         if (w + strlen(tmp) >= bsdtar->gs_width)
326                 bsdtar->gs_width = w+strlen(tmp)+1;
327         fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
328
329         /* Format the time using 'ls -l' conventions. */
330         tim = (time_t)st->st_mtime;
331         if (abs(tim - now) > (365/2)*86400)
332                 fmt = bsdtar->day_first ? "%e %b  %Y" : "%b %e  %Y";
333         else
334                 fmt = bsdtar->day_first ? "%e %b %R" : "%b %e %R";
335         strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
336         fprintf(out, " %s ", tmp);
337         safe_fprintf(out, "%s", archive_entry_pathname(entry));
338
339         /* Extra information for links. */
340         if (archive_entry_hardlink(entry)) /* Hard link */
341                 safe_fprintf(out, " link to %s",
342                     archive_entry_hardlink(entry));
343         else if (S_ISLNK(st->st_mode)) /* Symbolic link */
344                 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
345 }
346
347 /*
348  * Structure for storing path of last successful security check.
349  */
350 struct security {
351         char    *path;
352         size_t   path_size;
353 };
354
355 /*
356  * Check for a variety of security issues.  Fix what we can here,
357  * generate warnings as appropriate, return non-zero to prevent
358  * this entry from being extracted.
359  */
360 static int
361 security_problem(struct bsdtar *bsdtar, struct archive_entry *entry)
362 {
363         struct stat st;
364         const char *name, *pn;
365         char *p;
366         int r;
367
368         /* -P option forces us to just accept all pathnames as-is. */
369         if (bsdtar->option_absolute_paths)
370                 return (0);
371
372         name = archive_entry_pathname(entry);
373
374         /* Reject any archive entry with '..' as a path element. */
375         pn = name;
376         while (pn != NULL && pn[0] != '\0') {
377                 if (pn[0] == '.' && pn[1] == '.' &&
378                     (pn[2] == '\0' || pn[2] == '/')) {
379                         bsdtar_warnc(bsdtar, 0,
380                             "Skipping pathname containing ..");
381                         bsdtar->return_value = 1;
382                         return (1);
383                 }
384                 pn = strchr(pn, '/');
385                 if (pn != NULL)
386                         pn++;
387         }
388
389         /*
390          * Gaurd against symlink tricks.  Reject any archive entry whose
391          * destination would be altered by a symlink.
392          */
393         /* XXX TODO: Make this faster by comparing current path to
394          * prefix of last successful check to avoid duplicate lstat()
395          * calls. XXX */
396         pn = name;
397         if (bsdtar->security == NULL) {
398                 bsdtar->security = malloc(sizeof(*bsdtar->security));
399                 if (bsdtar->security == NULL)
400                         bsdtar_errc(bsdtar, 1, errno, "No Memory");
401                 bsdtar->security->path_size = MAXPATHLEN + 1;
402                 bsdtar->security->path = malloc(bsdtar->security->path_size);
403                 if (bsdtar->security->path == NULL)
404                         bsdtar_errc(bsdtar, 1, errno, "No Memory");
405         }
406         if (strlen(name) >= bsdtar->security->path_size) {
407                 free(bsdtar->security->path);
408                 while (strlen(name) >= bsdtar->security->path_size)
409                         bsdtar->security->path_size *= 2;
410                 bsdtar->security->path = malloc(bsdtar->security->path_size);
411                 if (bsdtar->security->path == NULL)
412                         bsdtar_errc(bsdtar, 1, errno, "No Memory");
413         }
414         p = bsdtar->security->path;
415         while (pn != NULL && pn[0] != '\0') {
416                 *p++ = *pn++;
417                 while (*pn != '\0' && *pn != '/')
418                         *p++ = *pn++;
419                 p[0] = '\0';
420                 r = lstat(bsdtar->security->path, &st);
421                 if (r != 0) {
422                         if (errno == ENOENT)
423                                 break;
424                 } else if (S_ISLNK(st.st_mode)) {
425                         if (pn[0] == '\0') {
426                                 /*
427                                  * Last element is symlink; remove it
428                                  * so we can overwrite it with the
429                                  * item being extracted.
430                                  */
431                                 if (!S_ISLNK(archive_entry_mode(entry))) {
432                                         /*
433                                          * Warn only if the symlink is being
434                                          * replaced with a non-symlink.
435                                          */
436                                         bsdtar_warnc(bsdtar, 0,
437                                             "Removing symlink %s",
438                                             bsdtar->security->path);
439                                 }
440                                 if (unlink(bsdtar->security->path))
441                                         bsdtar_errc(bsdtar, 1, errno,
442                                             "Unlink failed");
443                                 /* Symlink gone.  No more problem! */
444                                 return (0);
445                         } else if (bsdtar->option_unlink_first) {
446                                 /* User asked us to remove problems. */
447                                 if (unlink(bsdtar->security->path))
448                                         bsdtar_errc(bsdtar, 1, errno,
449                                             "Unlink failed");
450                         } else {
451                                 bsdtar_warnc(bsdtar, 0,
452                                     "Cannot extract %s through symlink %s",
453                                     name, bsdtar->security->path);
454                                 bsdtar->return_value = 1;
455                                 return (1);
456                         }
457                 }
458         }
459
460         return (0);
461 }
462
463 static void
464 cleanup_security(struct bsdtar *bsdtar)
465 {
466         if (bsdtar->security != NULL) {
467                 free(bsdtar->security->path);
468                 free(bsdtar->security);
469         }
470 }