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