2 * Copyright (c) 2003-2004 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include "bsdtar_platform.h"
28 __FBSDID("$FreeBSD: src/usr.bin/tar/read.c,v 1.21 2004/10/17 23:57:10 kientzle Exp $");
30 #include <sys/param.h>
31 #include <sys/types.h>
35 #include <archive_entry.h>
48 static void cleanup_security(struct bsdtar *);
49 static void list_item_verbose(struct bsdtar *, FILE *,
50 struct archive_entry *);
51 static void read_archive(struct bsdtar *bsdtar, char mode);
52 static int security_problem(struct bsdtar *, struct archive_entry *);
55 tar_mode_t(struct bsdtar *bsdtar)
57 read_archive(bsdtar, 't');
61 tar_mode_x(struct bsdtar *bsdtar)
63 read_archive(bsdtar, 'x');
67 * Handle 'x' and 't' modes.
70 read_archive(struct bsdtar *bsdtar, char mode)
74 struct archive_entry *entry;
77 while (*bsdtar->argv) {
78 include(bsdtar, *bsdtar->argv);
82 if (bsdtar->names_from_file != NULL)
83 include_from_file(bsdtar, bsdtar->names_from_file);
85 a = archive_read_new();
86 archive_read_support_compression_all(a);
87 archive_read_support_format_all(a);
88 if (archive_read_open_file(a, bsdtar->filename,
89 bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block :
90 DEFAULT_BYTES_PER_BLOCK))
91 bsdtar_errc(bsdtar, 1, 0, "Error opening archive: %s",
92 archive_error_string(a));
96 /* Support --fast-read option */
97 if (bsdtar->option_fast_read &&
98 unmatched_inclusions(bsdtar) == 0)
101 r = archive_read_next_header(a, &entry);
102 if (r == ARCHIVE_EOF)
104 if (r == ARCHIVE_WARN)
105 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
106 if (r == ARCHIVE_FATAL) {
107 bsdtar->return_value = 1;
108 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
111 if (r == ARCHIVE_RETRY) {
112 /* Retryable error: try again */
113 bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
114 bsdtar_warnc(bsdtar, 0, "Retrying...");
118 if (excluded(bsdtar, archive_entry_pathname(entry)))
122 /* Perversely, gtar uses -O to mean "send to stderr"
123 * when used with -t. */
124 out = bsdtar->option_stdout ? stderr : stdout;
126 if (bsdtar->verbose < 2)
127 safe_fprintf(out, "%s",
128 archive_entry_pathname(entry));
130 list_item_verbose(bsdtar, out, entry);
132 r = archive_read_data_skip(a);
133 if (r == ARCHIVE_WARN) {
135 bsdtar_warnc(bsdtar, 0, "%s",
136 archive_error_string(a));
138 if (r == ARCHIVE_RETRY) {
140 bsdtar_warnc(bsdtar, 0, "%s",
141 archive_error_string(a));
143 if (r == ARCHIVE_FATAL) {
145 bsdtar_warnc(bsdtar, 0, "%s",
146 archive_error_string(a));
151 if (bsdtar->option_interactive &&
152 !yes("extract '%s'", archive_entry_pathname(entry)))
155 if (security_problem(bsdtar, entry))
159 * Format here is from SUSv2, including the
162 if (bsdtar->verbose) {
163 safe_fprintf(stderr, "x %s",
164 archive_entry_pathname(entry));
167 if (bsdtar->option_stdout) {
168 /* TODO: Catch/recover any errors here. */
169 archive_read_data_into_fd(a, 1);
170 } else if (archive_read_extract(a, entry,
171 bsdtar->extract_flags)) {
172 if (!bsdtar->verbose)
173 safe_fprintf(stderr, "%s",
174 archive_entry_pathname(entry));
175 safe_fprintf(stderr, ": %s",
176 archive_error_string(a));
177 if (!bsdtar->verbose)
178 fprintf(stderr, "\n");
180 * TODO: Decide how to handle
181 * extraction error... <sigh>
183 bsdtar->return_value = 1;
186 fprintf(stderr, "\n");
190 if (bsdtar->verbose > 2)
191 fprintf(stdout, "Archive Format: %s, Compression: %s\n",
192 archive_format_name(a), archive_compression_name(a));
194 archive_read_finish(a);
195 cleanup_security(bsdtar);
200 * Display information about the current file.
202 * The format here roughly duplicates the output of 'ls -l'.
203 * This is based on SUSv2, where 'tar tv' is documented as
204 * listing additional information in an "unspecified format,"
205 * and 'pax -l' is documented as using the same format as 'ls -l'.
208 list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
210 const struct stat *st;
218 st = archive_entry_stat(entry);
221 * We avoid collecting the entire list in memory at once by
222 * listing things as we see them. However, that also means we can't
223 * just pre-compute the field widths. Instead, we start with guesses
224 * and just widen them as necessary. These numbers are completely
227 if (!bsdtar->u_width) {
229 bsdtar->gs_width = 13;
233 bsdtar_strmode(entry, tmp);
234 fprintf(out, "%s %d ", tmp, st->st_nlink);
236 /* Use uname if it's present, else uid. */
237 p = archive_entry_uname(entry);
238 if ((p == NULL) || (*p == '\0')) {
239 sprintf(tmp, "%d ", st->st_uid);
243 if (w > bsdtar->u_width)
245 fprintf(out, "%-*s ", (int)bsdtar->u_width, p);
247 /* Use gname if it's present, else gid. */
248 p = archive_entry_gname(entry);
249 if (p != NULL && p[0] != '\0') {
250 fprintf(out, "%s", p);
253 sprintf(tmp, "%d", st->st_gid);
255 fprintf(out, "%s", tmp);
259 * Print device number or file size, right-aligned so as to make
260 * total width of group and devnum/filesize fields be gs_width.
261 * If gs_width is too small, grow it.
263 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
264 sprintf(tmp, "%d,%u",
266 (unsigned)minor(st->st_rdev)); /* ls(1) also casts here. */
269 * Note the use of platform-dependent macros to format
270 * the filesize here. We need the format string and the
271 * corresponding type for the cast.
273 sprintf(tmp, BSDTAR_FILESIZE_PRINTF,
274 (BSDTAR_FILESIZE_TYPE)st->st_size);
276 if (w + strlen(tmp) >= bsdtar->gs_width)
277 bsdtar->gs_width = w+strlen(tmp)+1;
278 fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp);
280 /* Format the time using 'ls -l' conventions. */
281 tim = (time_t)st->st_mtime;
282 if (abs(tim - now) > (365/2)*86400)
283 fmt = bsdtar->day_first ? "%e %b %Y" : "%b %e %Y";
285 fmt = bsdtar->day_first ? "%e %b %R" : "%b %e %R";
286 strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
287 fprintf(out, " %s ", tmp);
288 safe_fprintf(out, "%s", archive_entry_pathname(entry));
290 /* Extra information for links. */
291 if (archive_entry_hardlink(entry)) /* Hard link */
292 safe_fprintf(out, " link to %s",
293 archive_entry_hardlink(entry));
294 else if (S_ISLNK(st->st_mode)) /* Symbolic link */
295 safe_fprintf(out, " -> %s", archive_entry_symlink(entry));
304 * Check for a variety of security issues. Fix what we can here,
305 * generate warnings as appropriate, return non-zero to prevent
306 * this entry from being extracted.
309 security_problem(struct bsdtar *bsdtar, struct archive_entry *entry)
312 const char *name, *pn;
316 /* -P option forces us to just accept all pathnames. */
317 if (bsdtar->option_absolute_paths)
320 /* Strip leading '/'. */
321 name = archive_entry_pathname(entry);
322 if (name[0] == '/') {
323 /* Generate a warning the first time this happens. */
324 if (!bsdtar->warned_lead_slash) {
325 bsdtar_warnc(bsdtar, 0,
326 "Removing leading '/' from member names");
327 bsdtar->warned_lead_slash = 1;
329 while (name[0] == '/')
331 archive_entry_set_pathname(entry, name);
334 /* Reject any archive entry with '..' as a path element. */
336 while (pn != NULL && pn[0] != '\0') {
337 if (pn[0] == '.' && pn[1] == '.' &&
338 (pn[2] == '\0' || pn[2] == '/')) {
339 bsdtar_warnc(bsdtar, 0,
340 "Skipping pathname containing ..");
341 bsdtar->return_value = 1;
344 pn = strchr(pn, '/');
350 * Gaurd against symlink tricks. Reject any archive entry whose
351 * destination would be altered by a symlink.
353 /* XXX TODO: Make this faster by comparing current path to
354 * prefix of last successful check to avoid duplicate lstat()
357 if (bsdtar->security == NULL) {
358 bsdtar->security = malloc(sizeof(*bsdtar->security));
359 if (bsdtar->security == NULL)
360 bsdtar_errc(bsdtar, 1, errno, "No Memory");
361 bsdtar->security->path_size = MAXPATHLEN + 1;
362 bsdtar->security->path = malloc(bsdtar->security->path_size);
363 if (bsdtar->security->path == NULL)
364 bsdtar_errc(bsdtar, 1, errno, "No Memory");
366 if (strlen(name) >= bsdtar->security->path_size) {
367 free(bsdtar->security->path);
368 while (strlen(name) >= bsdtar->security->path_size)
369 bsdtar->security->path_size *= 2;
370 bsdtar->security->path = malloc(bsdtar->security->path_size);
371 if (bsdtar->security->path == NULL)
372 bsdtar_errc(bsdtar, 1, errno, "No Memory");
374 p = bsdtar->security->path;
375 while (pn != NULL && pn[0] != '\0') {
377 while (*pn != '\0' && *pn != '/')
380 r = lstat(bsdtar->security->path, &st);
384 } else if (S_ISLNK(st.st_mode)) {
387 * Last element is symlink; remove it
388 * so we can overwrite it with the
389 * item being extracted.
391 if (!S_ISLNK(archive_entry_mode(entry))) {
393 * Warn only if the symlink is being
394 * replaced with a non-symlink.
396 bsdtar_warnc(bsdtar, 0,
397 "Removing symlink %s",
398 bsdtar->security->path);
400 if (unlink(bsdtar->security->path))
401 bsdtar_errc(bsdtar, 1, errno,
403 /* Symlink gone. No more problem! */
405 } else if (bsdtar->option_unlink_first) {
406 /* User asked us to remove problems. */
407 if (unlink(bsdtar->security->path))
408 bsdtar_errc(bsdtar, 1, errno,
411 bsdtar_warnc(bsdtar, 0,
412 "Cannot extract %s through symlink %s",
413 name, bsdtar->security->path);
414 bsdtar->return_value = 1;
424 cleanup_security(struct bsdtar *bsdtar)
426 if (bsdtar->security != NULL) {
427 free(bsdtar->security->path);
428 free(bsdtar->security);