xref: /freebsd/usr.bin/ar/read.c (revision 086f0908)
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  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
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 <stdlib.h>
41 #include <string.h>
42 
43 #include "ar.h"
44 
45 static int read_archive(struct bsdar *bsdar, char mode);
46 
47 int
48 ar_mode_p(struct bsdar *bsdar)
49 {
50 
51 	return (read_archive(bsdar, 'p'));
52 }
53 
54 int
55 ar_mode_t(struct bsdar *bsdar)
56 {
57 
58 	return (read_archive(bsdar, 't'));
59 }
60 
61 int
62 ar_mode_x(struct bsdar *bsdar)
63 {
64 
65 	return (read_archive(bsdar, 'x'));
66 }
67 
68 /*
69  * Handle read modes: 'x', 't' and 'p'.
70  */
71 static int
72 read_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			  exitcode, flags, r, i;
89 
90 	if ((a = archive_read_new()) == NULL)
91 		bsdar_errc(bsdar, 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 	exitcode = EXIT_SUCCESS;
96 
97 	for (;;) {
98 		r = archive_read_next_header(a, &entry);
99 		if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
100 		    r == ARCHIVE_FATAL)
101 			bsdar_warnc(bsdar, archive_errno(a), "%s",
102 			    archive_error_string(a));
103 		if (r == ARCHIVE_EOF || r == ARCHIVE_FATAL)
104 			break;
105 		if (r == ARCHIVE_RETRY) {
106 			bsdar_warnc(bsdar, 0, "Retrying...");
107 			continue;
108 		}
109 
110 		if ((name = archive_entry_pathname(entry)) == NULL)
111 			break;
112 
113 		/* Skip pseudo members. */
114 		if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0 ||
115 		    strcmp(name, "/SYM64/") == 0)
116 			continue;
117 
118 		if (bsdar->argc > 0) {
119 			find = 0;
120 			for(i = 0; i < bsdar->argc; i++) {
121 				av = &bsdar->argv[i];
122 				if (*av == NULL)
123 					continue;
124 				if ((bname = basename(*av)) == NULL)
125 					bsdar_errc(bsdar, errno,
126 					    "basename failed");
127 				if (strcmp(bname, name) != 0)
128 					continue;
129 
130 				*av = NULL;
131 				find = 1;
132 				break;
133 			}
134 			if (!find)
135 				continue;
136 		}
137 
138 		if (mode == 't') {
139 			if (bsdar->options & AR_V) {
140 				md = archive_entry_mode(entry);
141 				uid = archive_entry_uid(entry);
142 				gid = archive_entry_gid(entry);
143 				size = archive_entry_size(entry);
144 				mtime = archive_entry_mtime(entry);
145 				(void)strmode(md, buf);
146 				(void)fprintf(stdout, "%s %6d/%-6d %8ju ",
147 				    buf + 1, uid, gid, (uintmax_t)size);
148 				tp = localtime(&mtime);
149 				(void)strftime(buf, sizeof(buf),
150 				    "%b %e %H:%M %Y", tp);
151 				(void)fprintf(stdout, "%s %s", buf, name);
152 			} else
153 				(void)fprintf(stdout, "%s", name);
154 			r = archive_read_data_skip(a);
155 			if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
156 			    r == ARCHIVE_FATAL) {
157 				(void)fprintf(stdout, "\n");
158 				bsdar_warnc(bsdar, archive_errno(a), "%s",
159 				    archive_error_string(a));
160 			}
161 
162 			if (r == ARCHIVE_FATAL)
163 				break;
164 
165 			(void)fprintf(stdout, "\n");
166 		} else {
167 			/* mode == 'x' || mode = 'p' */
168 			if (mode == 'p') {
169 				if (bsdar->options & AR_V) {
170 					(void)fprintf(stdout, "\n<%s>\n\n",
171 					    name);
172 					fflush(stdout);
173 				}
174 				r = archive_read_data_into_fd(a, 1);
175 			} else {
176 				/* mode == 'x' */
177 				if (stat(name, &sb) != 0) {
178 					if (errno != ENOENT) {
179 						bsdar_warnc(bsdar, 0,
180 						    "stat %s failed",
181 						    bsdar->filename);
182 						continue;
183 					}
184 				} else {
185 					/* stat success, file exist */
186 					if (bsdar->options & AR_CC)
187 						continue;
188 					if (bsdar->options & AR_U &&
189 					    archive_entry_mtime(entry) <=
190 					    sb.st_mtime)
191 						continue;
192 				}
193 
194 				if (bsdar->options & AR_V)
195 					(void)fprintf(stdout, "x - %s\n", name);
196 				/* Disallow absolute paths. */
197 				if (name[0] == '/') {
198 					bsdar_warnc(bsdar, 0,
199 					    "Absolute path '%s'", name);
200 					continue;
201 				}
202 				/* Basic path security flags. */
203 				flags = ARCHIVE_EXTRACT_SECURE_SYMLINKS |
204 				    ARCHIVE_EXTRACT_SECURE_NODOTDOT;
205 				if (bsdar->options & AR_O)
206 					flags |= ARCHIVE_EXTRACT_TIME;
207 
208 				r = archive_read_extract(a, entry, flags);
209 			}
210 
211 			if (r) {
212 				bsdar_warnc(bsdar, archive_errno(a), "%s",
213 				    archive_error_string(a));
214 				exitcode = EXIT_FAILURE;
215 			}
216 		}
217 	}
218 
219 	if (r == ARCHIVE_FATAL)
220 		exitcode = EXIT_FAILURE;
221 
222 	AC(archive_read_close(a));
223 	AC(archive_read_free(a));
224 
225 	return (exitcode);
226 }
227