xref: /freebsd/usr.bin/ar/read.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/queue.h>
32 #include <sys/stat.h>
33 #include <archive.h>
34 #include <archive_entry.h>
35 #include <assert.h>
36 #include <errno.h>
37 #include <libgen.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include "ar.h"
43 
44 /*
45  * Handle read modes: 'x', 't' and 'p'.
46  */
47 int
48 ar_read_archive(struct bsdar *bsdar, int mode, FILE *out)
49 {
50 	struct archive		 *a;
51 	struct archive_entry	 *entry;
52 	struct stat		  sb;
53 	struct tm		 *tp;
54 	const char		 *bname;
55 	const char		 *name;
56 	mode_t			  md;
57 	size_t			  size;
58 	time_t			  mtime;
59 	uid_t			  uid;
60 	gid_t			  gid;
61 	char			**av;
62 	char			  buf[25];
63 	char			  find;
64 	int			  exitcode, flags, r, i;
65 
66 	assert(mode == 'p' || mode == 't' || mode == 'x');
67 
68 	if ((a = archive_read_new()) == NULL)
69 		bsdar_errc(bsdar, 0, "archive_read_new failed");
70 	archive_read_support_format_ar(a);
71 	AC(archive_read_open_filename(a, bsdar->filename, DEF_BLKSZ));
72 
73 	exitcode = EXIT_SUCCESS;
74 
75 	for (;;) {
76 		r = archive_read_next_header(a, &entry);
77 		if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
78 		    r == ARCHIVE_FATAL)
79 			bsdar_warnc(bsdar, archive_errno(a), "%s",
80 			    archive_error_string(a));
81 		if (r == ARCHIVE_EOF || r == ARCHIVE_FATAL)
82 			break;
83 		if (r == ARCHIVE_RETRY) {
84 			bsdar_warnc(bsdar, 0, "Retrying...");
85 			continue;
86 		}
87 
88 		if ((name = archive_entry_pathname(entry)) == NULL)
89 			break;
90 
91 		/* Skip pseudo members. */
92 		if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0 ||
93 		    strcmp(name, "/SYM64/") == 0)
94 			continue;
95 
96 		if (bsdar->argc > 0) {
97 			find = 0;
98 			for(i = 0; i < bsdar->argc; i++) {
99 				av = &bsdar->argv[i];
100 				if (*av == NULL)
101 					continue;
102 				if ((bname = basename(*av)) == NULL)
103 					bsdar_errc(bsdar, errno,
104 					    "basename failed");
105 				if (strcmp(bname, name) != 0)
106 					continue;
107 
108 				*av = NULL;
109 				find = 1;
110 				break;
111 			}
112 			if (!find)
113 				continue;
114 		}
115 
116 		if (mode == 't') {
117 			if (bsdar->options & AR_V) {
118 				md = archive_entry_mode(entry);
119 				uid = archive_entry_uid(entry);
120 				gid = archive_entry_gid(entry);
121 				size = archive_entry_size(entry);
122 				mtime = archive_entry_mtime(entry);
123 				(void)strmode(md, buf);
124 				(void)fprintf(out, "%s %6d/%-6d %8ju ",
125 				    buf + 1, uid, gid, (uintmax_t)size);
126 				tp = localtime(&mtime);
127 				(void)strftime(buf, sizeof(buf),
128 				    "%b %e %H:%M %Y", tp);
129 				(void)fprintf(out, "%s %s", buf, name);
130 			} else
131 				(void)fprintf(out, "%s", name);
132 			r = archive_read_data_skip(a);
133 			if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
134 			    r == ARCHIVE_FATAL) {
135 				(void)fprintf(out, "\n");
136 				bsdar_warnc(bsdar, archive_errno(a), "%s",
137 				    archive_error_string(a));
138 			}
139 
140 			if (r == ARCHIVE_FATAL)
141 				break;
142 
143 			(void)fprintf(out, "\n");
144 		} else {
145 			/* mode == 'x' || mode = 'p' */
146 			if (mode == 'p') {
147 				if (bsdar->options & AR_V) {
148 					(void)fprintf(out, "\n<%s>\n\n",
149 					    name);
150 					fflush(out);
151 				}
152 				r = archive_read_data_into_fd(a, 1);
153 			} else {
154 				/* mode == 'x' */
155 				if (stat(name, &sb) != 0) {
156 					if (errno != ENOENT) {
157 						bsdar_warnc(bsdar, 0,
158 						    "stat %s failed",
159 						    bsdar->filename);
160 						continue;
161 					}
162 				} else {
163 					/* stat success, file exist */
164 					if (bsdar->options & AR_CC)
165 						continue;
166 					if (bsdar->options & AR_U &&
167 					    archive_entry_mtime(entry) <=
168 					    sb.st_mtime)
169 						continue;
170 				}
171 
172 				if (bsdar->options & AR_V)
173 					(void)fprintf(out, "x - %s\n", name);
174 				/* Disallow absolute paths. */
175 				if (name[0] == '/') {
176 					bsdar_warnc(bsdar, 0,
177 					    "Absolute path '%s'", name);
178 					continue;
179 				}
180 				/* Basic path security flags. */
181 				flags = ARCHIVE_EXTRACT_SECURE_SYMLINKS |
182 				    ARCHIVE_EXTRACT_SECURE_NODOTDOT;
183 				if (bsdar->options & AR_O)
184 					flags |= ARCHIVE_EXTRACT_TIME;
185 
186 				r = archive_read_extract(a, entry, flags);
187 			}
188 
189 			if (r) {
190 				bsdar_warnc(bsdar, archive_errno(a), "%s",
191 				    archive_error_string(a));
192 				exitcode = EXIT_FAILURE;
193 			}
194 		}
195 	}
196 
197 	if (r == ARCHIVE_FATAL)
198 		exitcode = EXIT_FAILURE;
199 
200 	AC(archive_read_close(a));
201 	AC(archive_read_free(a));
202 
203 	return (exitcode);
204 }
205