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