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