read.c revision 346901
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2007 Kai Wang
5 * Copyright (c) 2007 Tim Kientzle
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer
13 *    in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: stable/11/usr.bin/ar/read.c 346901 2019-04-29 18:28:34Z emaste $");
32
33#include <sys/queue.h>
34#include <sys/stat.h>
35#include <archive.h>
36#include <archive_entry.h>
37#include <errno.h>
38#include <libgen.h>
39#include <stdio.h>
40#include <string.h>
41#include <sysexits.h>
42
43#include "ar.h"
44
45static void read_archive(struct bsdar *bsdar, char mode);
46
47void
48ar_mode_p(struct bsdar *bsdar)
49{
50
51	read_archive(bsdar, 'p');
52}
53
54void
55ar_mode_t(struct bsdar *bsdar)
56{
57
58	read_archive(bsdar, 't');
59}
60
61void
62ar_mode_x(struct bsdar *bsdar)
63{
64
65	read_archive(bsdar, 'x');
66}
67
68/*
69 * Handle read modes: 'x', 't' and 'p'.
70 */
71static void
72read_archive(struct bsdar *bsdar, char mode)
73{
74	struct archive		 *a;
75	struct archive_entry	 *entry;
76	struct stat		  sb;
77	struct tm		 *tp;
78	const char		 *bname;
79	const char		 *name;
80	mode_t			  md;
81	size_t			  size;
82	time_t			  mtime;
83	uid_t			  uid;
84	gid_t			  gid;
85	char			**av;
86	char			  buf[25];
87	char			  find;
88	int			  flags, r, i;
89
90	if ((a = archive_read_new()) == NULL)
91		bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_read_new failed");
92	archive_read_support_format_ar(a);
93	AC(archive_read_open_filename(a, bsdar->filename, DEF_BLKSZ));
94
95	for (;;) {
96		r = archive_read_next_header(a, &entry);
97		if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
98		    r == ARCHIVE_FATAL)
99			bsdar_warnc(bsdar, archive_errno(a), "%s",
100			    archive_error_string(a));
101		if (r == ARCHIVE_EOF || r == ARCHIVE_FATAL)
102			break;
103		if (r == ARCHIVE_RETRY) {
104			bsdar_warnc(bsdar, 0, "Retrying...");
105			continue;
106		}
107
108		if ((name = archive_entry_pathname(entry)) == NULL)
109			break;
110
111		/* Skip pseudo members. */
112		if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0)
113			continue;
114
115		if (bsdar->argc > 0) {
116			find = 0;
117			for(i = 0; i < bsdar->argc; i++) {
118				av = &bsdar->argv[i];
119				if (*av == NULL)
120					continue;
121				if ((bname = basename(*av)) == NULL)
122					bsdar_errc(bsdar, EX_SOFTWARE, errno,
123					    "basename failed");
124				if (strcmp(bname, name) != 0)
125					continue;
126
127				*av = NULL;
128				find = 1;
129				break;
130			}
131			if (!find)
132				continue;
133		}
134
135		if (mode == 't') {
136			if (bsdar->options & AR_V) {
137				md = archive_entry_mode(entry);
138				uid = archive_entry_uid(entry);
139				gid = archive_entry_gid(entry);
140				size = archive_entry_size(entry);
141				mtime = archive_entry_mtime(entry);
142				(void)strmode(md, buf);
143				(void)fprintf(stdout, "%s %6d/%-6d %8ju ",
144				    buf + 1, uid, gid, (uintmax_t)size);
145				tp = localtime(&mtime);
146				(void)strftime(buf, sizeof(buf),
147				    "%b %e %H:%M %Y", tp);
148				(void)fprintf(stdout, "%s %s", buf, name);
149			} else
150				(void)fprintf(stdout, "%s", name);
151			r = archive_read_data_skip(a);
152			if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
153			    r == ARCHIVE_FATAL) {
154				(void)fprintf(stdout, "\n");
155				bsdar_warnc(bsdar, archive_errno(a), "%s",
156				    archive_error_string(a));
157			}
158
159			if (r == ARCHIVE_FATAL)
160				break;
161
162			(void)fprintf(stdout, "\n");
163		} else {
164			/* mode == 'x' || mode = 'p' */
165			if (mode == 'p') {
166				if (bsdar->options & AR_V) {
167					(void)fprintf(stdout, "\n<%s>\n\n",
168					    name);
169					fflush(stdout);
170				}
171				r = archive_read_data_into_fd(a, 1);
172			} else {
173				/* mode == 'x' */
174				if (stat(name, &sb) != 0) {
175					if (errno != ENOENT) {
176						bsdar_warnc(bsdar, 0,
177						    "stat %s failed",
178						    bsdar->filename);
179						continue;
180					}
181				} else {
182					/* stat success, file exist */
183					if (bsdar->options & AR_CC)
184						continue;
185					if (bsdar->options & AR_U &&
186					    archive_entry_mtime(entry) <=
187					    sb.st_mtime)
188						continue;
189				}
190
191				if (bsdar->options & AR_V)
192					(void)fprintf(stdout, "x - %s\n", name);
193				/* Disallow absolute paths. */
194				if (name[0] == '/') {
195					bsdar_warnc(bsdar, 0,
196					    "Absolute path '%s'", name);
197					continue;
198				}
199				/* Basic path security flags. */
200				flags = ARCHIVE_EXTRACT_SECURE_SYMLINKS |
201				    ARCHIVE_EXTRACT_SECURE_NODOTDOT;
202				if (bsdar->options & AR_O)
203					flags |= ARCHIVE_EXTRACT_TIME;
204
205				r = archive_read_extract(a, entry, flags);
206			}
207
208			if (r)
209				bsdar_warnc(bsdar, archive_errno(a), "%s",
210				    archive_error_string(a));
211		}
212	}
213	AC(archive_read_close(a));
214	AC(archive_read_free(a));
215}
216