]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/unzip/unzip.c
Merge llvm-project main llvmorg-15-init-17485-ga3e38b4a206b
[FreeBSD/FreeBSD.git] / usr.bin / unzip / unzip.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009, 2010 Joerg Sonnenberger <joerg@NetBSD.org>
5  * Copyright (c) 2007-2008 Dag-Erling Smørgrav
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  *    in this position and unchanged.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  *
32  * This file would be much shorter if we didn't care about command-line
33  * compatibility with Info-ZIP's UnZip, which requires us to duplicate
34  * parts of libarchive in order to gain more detailed control of its
35  * behaviour for the purpose of implementing the -n, -o, -L and -a
36  * options.
37  */
38
39 #include <sys/queue.h>
40 #include <sys/stat.h>
41
42 #include <ctype.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <fnmatch.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 #include <archive.h>
53 #include <archive_entry.h>
54 #include <readpassphrase.h>
55
56 /* command-line options */
57 static int               a_opt;         /* convert EOL */
58 static int               C_opt;         /* match case-insensitively */
59 static int               c_opt;         /* extract to stdout */
60 static const char       *d_arg;         /* directory */
61 static int               f_opt;         /* update existing files only */
62 static int               j_opt;         /* junk directories */
63 static int               L_opt;         /* lowercase names */
64 static int               n_opt;         /* never overwrite */
65 static int               o_opt;         /* always overwrite */
66 static int               p_opt;         /* extract to stdout, quiet */
67 static char             *P_arg;         /* passphrase */
68 static int               q_opt;         /* quiet */
69 static int               t_opt;         /* test */
70 static int               u_opt;         /* update */
71 static int               v_opt;         /* verbose/list */
72 static const char       *y_str = "";    /* 4 digit year */
73 static int               Z1_opt;        /* zipinfo mode list files only */
74
75 /* debug flag */
76 static int               unzip_debug;
77
78 /* zipinfo mode */
79 static int               zipinfo_mode;
80
81 /* running on tty? */
82 static int               tty;
83
84 /* convenience macro */
85 /* XXX should differentiate between ARCHIVE_{WARN,FAIL,RETRY} */
86 #define ac(call)                                                \
87         do {                                                    \
88                 int acret = (call);                             \
89                 if (acret != ARCHIVE_OK)                        \
90                         errorx("%s", archive_error_string(a));  \
91         } while (0)
92
93 /*
94  * Indicates that last info() did not end with EOL.  This helps error() et
95  * al. avoid printing an error message on the same line as an incomplete
96  * informational message.
97  */
98 static int noeol;
99
100 /* for an interactive passphrase input */
101 static char *passphrase_buf;
102
103 /* fatal error message + errno */
104 static void
105 error(const char *fmt, ...)
106 {
107         va_list ap;
108
109         if (noeol)
110                 fprintf(stdout, "\n");
111         fflush(stdout);
112         fprintf(stderr, "unzip: ");
113         va_start(ap, fmt);
114         vfprintf(stderr, fmt, ap);
115         va_end(ap);
116         fprintf(stderr, ": %s\n", strerror(errno));
117         exit(EXIT_FAILURE);
118 }
119
120 /* fatal error message, no errno */
121 static void
122 errorx(const char *fmt, ...)
123 {
124         va_list ap;
125
126         if (noeol)
127                 fprintf(stdout, "\n");
128         fflush(stdout);
129         fprintf(stderr, "unzip: ");
130         va_start(ap, fmt);
131         vfprintf(stderr, fmt, ap);
132         va_end(ap);
133         fprintf(stderr, "\n");
134         exit(EXIT_FAILURE);
135 }
136
137 /* non-fatal error message + errno */
138 static void
139 warning(const char *fmt, ...)
140 {
141         va_list ap;
142
143         if (noeol)
144                 fprintf(stdout, "\n");
145         fflush(stdout);
146         fprintf(stderr, "unzip: ");
147         va_start(ap, fmt);
148         vfprintf(stderr, fmt, ap);
149         va_end(ap);
150         fprintf(stderr, ": %s\n", strerror(errno));
151 }
152
153 /* non-fatal error message, no errno */
154 static void
155 warningx(const char *fmt, ...)
156 {
157         va_list ap;
158
159         if (noeol)
160                 fprintf(stdout, "\n");
161         fflush(stdout);
162         fprintf(stderr, "unzip: ");
163         va_start(ap, fmt);
164         vfprintf(stderr, fmt, ap);
165         va_end(ap);
166         fprintf(stderr, "\n");
167 }
168
169 /* informational message (if not -q) */
170 static void
171 info(const char *fmt, ...)
172 {
173         va_list ap;
174
175         if (q_opt && !unzip_debug)
176                 return;
177         va_start(ap, fmt);
178         vfprintf(stdout, fmt, ap);
179         va_end(ap);
180         fflush(stdout);
181
182         if (*fmt == '\0')
183                 noeol = 1;
184         else
185                 noeol = fmt[strlen(fmt) - 1] != '\n';
186 }
187
188 /* debug message (if unzip_debug) */
189 static void
190 debug(const char *fmt, ...)
191 {
192         va_list ap;
193
194         if (!unzip_debug)
195                 return;
196         va_start(ap, fmt);
197         vfprintf(stderr, fmt, ap);
198         va_end(ap);
199         fflush(stderr);
200
201         if (*fmt == '\0')
202                 noeol = 1;
203         else
204                 noeol = fmt[strlen(fmt) - 1] != '\n';
205 }
206
207 /* duplicate a path name, possibly converting to lower case */
208 static char *
209 pathdup(const char *path)
210 {
211         char *str;
212         size_t i, len;
213
214         if (path == NULL || path[0] == '\0')
215                 return (NULL);
216
217         len = strlen(path);
218         while (len && path[len - 1] == '/')
219                 len--;
220         if ((str = malloc(len + 1)) == NULL) {
221                 errno = ENOMEM;
222                 error("malloc()");
223         }
224         if (L_opt) {
225                 for (i = 0; i < len; ++i)
226                         str[i] = tolower((unsigned char)path[i]);
227         } else {
228                 memcpy(str, path, len);
229         }
230         str[len] = '\0';
231
232         return (str);
233 }
234
235 /* concatenate two path names */
236 static char *
237 pathcat(const char *prefix, const char *path)
238 {
239         char *str;
240         size_t prelen, len;
241
242         prelen = prefix ? strlen(prefix) + 1 : 0;
243         len = strlen(path) + 1;
244         if ((str = malloc(prelen + len)) == NULL) {
245                 errno = ENOMEM;
246                 error("malloc()");
247         }
248         if (prefix) {
249                 memcpy(str, prefix, prelen);    /* includes zero */
250                 str[prelen - 1] = '/';          /* splat zero */
251         }
252         memcpy(str + prelen, path, len);        /* includes zero */
253
254         return (str);
255 }
256
257 /*
258  * Pattern lists for include / exclude processing
259  */
260 struct pattern {
261         STAILQ_ENTRY(pattern) link;
262         char pattern[];
263 };
264
265 STAILQ_HEAD(pattern_list, pattern);
266 static struct pattern_list include = STAILQ_HEAD_INITIALIZER(include);
267 static struct pattern_list exclude = STAILQ_HEAD_INITIALIZER(exclude);
268
269 /*
270  * Add an entry to a pattern list
271  */
272 static void
273 add_pattern(struct pattern_list *list, const char *pattern)
274 {
275         struct pattern *entry;
276         size_t len;
277
278         debug("adding pattern '%s'\n", pattern);
279         len = strlen(pattern);
280         if ((entry = malloc(sizeof *entry + len + 1)) == NULL) {
281                 errno = ENOMEM;
282                 error("malloc()");
283         }
284         memcpy(entry->pattern, pattern, len + 1);
285         STAILQ_INSERT_TAIL(list, entry, link);
286 }
287
288 /*
289  * Match a string against a list of patterns
290  */
291 static int
292 match_pattern(struct pattern_list *list, const char *str)
293 {
294         struct pattern *entry;
295
296         STAILQ_FOREACH(entry, list, link) {
297                 if (fnmatch(entry->pattern, str, C_opt ? FNM_CASEFOLD : 0) == 0)
298                         return (1);
299         }
300         return (0);
301 }
302
303 /*
304  * Verify that a given pathname is in the include list and not in the
305  * exclude list.
306  */
307 static int
308 accept_pathname(const char *pathname)
309 {
310
311         if (!STAILQ_EMPTY(&include) && !match_pattern(&include, pathname))
312                 return (0);
313         if (!STAILQ_EMPTY(&exclude) && match_pattern(&exclude, pathname))
314                 return (0);
315         return (1);
316 }
317
318 /*
319  * Create the specified directory with the specified mode, taking certain
320  * precautions on they way.
321  */
322 static void
323 make_dir(const char *path, int mode)
324 {
325         struct stat sb;
326
327         if (lstat(path, &sb) == 0) {
328                 if (S_ISDIR(sb.st_mode))
329                         return;
330                 /*
331                  * Normally, we should either ask the user about removing
332                  * the non-directory of the same name as a directory we
333                  * wish to create, or respect the -n or -o command-line
334                  * options.  However, this may lead to a later failure or
335                  * even compromise (if this non-directory happens to be a
336                  * symlink to somewhere unsafe), so we don't.
337                  */
338
339                 /*
340                  * Don't check unlink() result; failure will cause mkdir()
341                  * to fail later, which we will catch.
342                  */
343                 (void)unlink(path);
344         }
345         if (mkdir(path, mode) != 0 && errno != EEXIST)
346                 error("mkdir('%s')", path);
347 }
348
349 /*
350  * Ensure that all directories leading up to (but not including) the
351  * specified path exist.
352  *
353  * XXX inefficient + modifies the file in-place
354  */
355 static void
356 make_parent(char *path)
357 {
358         struct stat sb;
359         char *sep;
360
361         sep = strrchr(path, '/');
362         if (sep == NULL || sep == path)
363                 return;
364         *sep = '\0';
365         if (lstat(path, &sb) == 0) {
366                 if (S_ISDIR(sb.st_mode)) {
367                         *sep = '/';
368                         return;
369                 }
370                 unlink(path);
371         }
372         make_parent(path);
373         mkdir(path, 0755);
374         *sep = '/';
375
376 #if 0
377         for (sep = path; (sep = strchr(sep, '/')) != NULL; sep++) {
378                 /* root in case of absolute d_arg */
379                 if (sep == path)
380                         continue;
381                 *sep = '\0';
382                 make_dir(path, 0755);
383                 *sep = '/';
384         }
385 #endif
386 }
387
388 /*
389  * Extract a directory.
390  */
391 static void
392 extract_dir(struct archive *a, struct archive_entry *e, const char *path)
393 {
394         int mode;
395
396         /*
397          * Dropbox likes to create '/' directory entries, just ignore
398          * such junk.
399          */
400         if (*path == '\0')
401                 return;
402
403         mode = archive_entry_mode(e) & 0777;
404         if (mode == 0)
405                 mode = 0755;
406
407         /*
408          * Some zipfiles contain directories with weird permissions such
409          * as 0644 or 0444.  This can cause strange issues such as being
410          * unable to extract files into the directory we just created, or
411          * the user being unable to remove the directory later without
412          * first manually changing its permissions.  Therefore, we whack
413          * the permissions into shape, assuming that the user wants full
414          * access and that anyone who gets read access also gets execute
415          * access.
416          */
417         mode |= 0700;
418         if (mode & 0040)
419                 mode |= 0010;
420         if (mode & 0004)
421                 mode |= 0001;
422
423         info("   creating: %s/\n", path);
424         make_dir(path, mode);
425         ac(archive_read_data_skip(a));
426 }
427
428 static unsigned char buffer[8192];
429 static char spinner[] = { '|', '/', '-', '\\' };
430
431 static int
432 handle_existing_file(char **path)
433 {
434         size_t alen;
435         ssize_t len;
436         char buf[4];
437
438         for (;;) {
439                 fprintf(stderr,
440                     "replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ",
441                     *path);
442                 if (fgets(buf, sizeof(buf), stdin) == NULL) {
443                         clearerr(stdin);
444                         printf("NULL\n(EOF or read error, "
445                             "treating as \"[N]one\"...)\n");
446                         n_opt = 1;
447                         return -1;
448                 }
449                 switch (*buf) {
450                 case 'A':
451                         o_opt = 1;
452                         /* FALLTHROUGH */
453                 case 'y':
454                 case 'Y':
455                         (void)unlink(*path);
456                         return 1;
457                 case 'N':
458                         n_opt = 1;
459                         /* FALLTHROUGH */
460                 case 'n':
461                         return -1;
462                 case 'r':
463                 case 'R':
464                         printf("New name: ");
465                         fflush(stdout);
466                         free(*path);
467                         *path = NULL;
468                         alen = 0;
469                         len = getline(path, &alen, stdin);
470                         if ((*path)[len - 1] == '\n')
471                                 (*path)[len - 1] = '\0';
472                         return 0;
473                 default:
474                         break;
475                 }
476         }
477 }
478
479 /*
480  * Detect binary files by a combination of character white list and
481  * black list. NUL bytes and other control codes without use in text files
482  * result directly in switching the file to binary mode. Otherwise, at least
483  * one white-listed byte has to be found.
484  *
485  * Black-listed: 0..6, 14..25, 28..31
486  * 0xf3ffc07f = 11110011111111111100000001111111b
487  * White-listed: 9..10, 13, >= 32
488  * 0x00002600 = 00000000000000000010011000000000b
489  *
490  * See the proginfo/txtvsbin.txt in the zip sources for a detailed discussion.
491  */
492 #define BYTE_IS_BINARY(x)       ((x) < 32 && (0xf3ffc07fU & (1U << (x))))
493 #define BYTE_IS_TEXT(x)         ((x) >= 32 || (0x00002600U & (1U << (x))))
494
495 static int
496 check_binary(const unsigned char *buf, size_t len)
497 {
498         int rv;
499         for (rv = 1; len--; ++buf) {
500                 if (BYTE_IS_BINARY(*buf))
501                         return 1;
502                 if (BYTE_IS_TEXT(*buf))
503                         rv = 0;
504         }
505
506         return rv;
507 }
508
509 /*
510  * Extract to a file descriptor
511  */
512 static int
513 extract2fd(struct archive *a, char *pathname, int fd)
514 {
515         int cr, text, warn;
516         ssize_t len;
517         unsigned char *p, *q, *end;
518
519         text = a_opt;
520         warn = 0;
521         cr = 0;
522
523         /* loop over file contents and write to fd */
524         for (int n = 0; ; n++) {
525                 if (fd != STDOUT_FILENO)
526                         if (tty && (n % 4) == 0)
527                                 info(" %c\b\b", spinner[(n / 4) % sizeof spinner]);
528
529                 len = archive_read_data(a, buffer, sizeof buffer);
530
531                 if (len < 0)
532                         ac(len);
533
534                 /* left over CR from previous buffer */
535                 if (a_opt && cr) {
536                         if (len == 0 || buffer[0] != '\n')
537                                 if (write(fd, "\r", 1) != 1)
538                                         error("write('%s')", pathname);
539                         cr = 0;
540                 }
541
542                 /* EOF */
543                 if (len == 0)
544                         break;
545                 end = buffer + len;
546
547                 /*
548                  * Detect whether this is a text file.  The correct way to
549                  * do this is to check the least significant bit of the
550                  * "internal file attributes" field of the corresponding
551                  * file header in the central directory, but libarchive
552                  * does not provide access to this field, so we have to
553                  * guess by looking for non-ASCII characters in the
554                  * buffer.  Hopefully we won't guess wrong.  If we do
555                  * guess wrong, we print a warning message later.
556                  */
557                 if (a_opt && n == 0) {
558                         if (check_binary(buffer, len))
559                                 text = 0;
560                 }
561
562                 /* simple case */
563                 if (!a_opt || !text) {
564                         if (write(fd, buffer, len) != len)
565                                 error("write('%s')", pathname);
566                         continue;
567                 }
568
569                 /* hard case: convert \r\n to \n (sigh...) */
570                 for (p = buffer; p < end; p = q + 1) {
571                         for (q = p; q < end; q++) {
572                                 if (!warn && BYTE_IS_BINARY(*q)) {
573                                         warningx("%s may be corrupted due"
574                                             " to weak text file detection"
575                                             " heuristic", pathname);
576                                         warn = 1;
577                                 }
578                                 if (q[0] != '\r')
579                                         continue;
580                                 if (&q[1] == end) {
581                                         cr = 1;
582                                         break;
583                                 }
584                                 if (q[1] == '\n')
585                                         break;
586                         }
587                         if (write(fd, p, q - p) != q - p)
588                                 error("write('%s')", pathname);
589                 }
590         }
591
592         return text;
593 }
594
595 /*
596  * Extract a regular file.
597  */
598 static void
599 extract_file(struct archive *a, struct archive_entry *e, char **path)
600 {
601         int mode;
602         struct timespec mtime;
603         struct stat sb;
604         struct timespec ts[2];
605         int fd, check, text;
606         const char *linkname;
607
608         mode = archive_entry_mode(e) & 0777;
609         if (mode == 0)
610                 mode = 0644;
611         mtime.tv_sec = archive_entry_mtime(e);
612         mtime.tv_nsec = archive_entry_mtime_nsec(e);
613
614         /* look for existing file of same name */
615 recheck:
616         if (lstat(*path, &sb) == 0) {
617                 if (u_opt || f_opt) {
618                         /* check if up-to-date */
619                         if (S_ISREG(sb.st_mode) &&
620                             (sb.st_mtim.tv_sec > mtime.tv_sec ||
621                             (sb.st_mtim.tv_sec == mtime.tv_sec &&
622                             sb.st_mtim.tv_nsec >= mtime.tv_nsec)))
623                                 return;
624                         (void)unlink(*path);
625                 } else if (o_opt) {
626                         /* overwrite */
627                         (void)unlink(*path);
628                 } else if (n_opt) {
629                         /* do not overwrite */
630                         return;
631                 } else {
632                         check = handle_existing_file(path);
633                         if (check == 0)
634                                 goto recheck;
635                         if (check == -1)
636                                 return; /* do not overwrite */
637                 }
638         } else {
639                 if (f_opt)
640                         return;
641         }
642
643         ts[0].tv_sec = 0;
644         ts[0].tv_nsec = UTIME_NOW;
645         ts[1] = mtime;
646
647         /* process symlinks */
648         linkname = archive_entry_symlink(e);
649         if (linkname != NULL) {
650                 if (symlink(linkname, *path) != 0)
651                         error("symlink('%s')", *path);
652                 info(" extracting: %s -> %s\n", *path, linkname);
653                 if (lchmod(*path, mode) != 0)
654                         warning("Cannot set mode for '%s'", *path);
655                 /* set access and modification time */
656                 if (utimensat(AT_FDCWD, *path, ts, AT_SYMLINK_NOFOLLOW) != 0)
657                         warning("utimensat('%s')", *path);
658                 return;
659         }
660
661         if ((fd = open(*path, O_RDWR|O_CREAT|O_TRUNC, mode)) < 0)
662                 error("open('%s')", *path);
663
664         info(" extracting: %s", *path);
665
666         text = extract2fd(a, *path, fd);
667
668         if (tty)
669                 info("  \b\b");
670         if (text)
671                 info(" (text)");
672         info("\n");
673
674         /* set access and modification time */
675         if (futimens(fd, ts) != 0)
676                 error("futimens('%s')", *path);
677         if (close(fd) != 0)
678                 error("close('%s')", *path);
679 }
680
681 /*
682  * Extract a zipfile entry: first perform some sanity checks to ensure
683  * that it is either a directory or a regular file and that the path is
684  * not absolute and does not try to break out of the current directory;
685  * then call either extract_dir() or extract_file() as appropriate.
686  *
687  * This is complicated a bit by the various ways in which we need to
688  * manipulate the path name.  Case conversion (if requested by the -L
689  * option) happens first, but the include / exclude patterns are applied
690  * to the full converted path name, before the directory part of the path
691  * is removed in accordance with the -j option.  Sanity checks are
692  * intentionally done earlier than they need to be, so the user will get a
693  * warning about insecure paths even for files or directories which
694  * wouldn't be extracted anyway.
695  */
696 static void
697 extract(struct archive *a, struct archive_entry *e)
698 {
699         char *pathname, *realpathname;
700         mode_t filetype;
701         char *p, *q;
702
703         if ((pathname = pathdup(archive_entry_pathname(e))) == NULL) {
704                 warningx("skipping empty or unreadable filename entry");
705                 ac(archive_read_data_skip(a));
706                 return;
707         }
708         filetype = archive_entry_filetype(e);
709
710         /* sanity checks */
711         if (pathname[0] == '/' ||
712             strncmp(pathname, "../", 3) == 0 ||
713             strstr(pathname, "/../") != NULL) {
714                 warningx("skipping insecure entry '%s'", pathname);
715                 ac(archive_read_data_skip(a));
716                 free(pathname);
717                 return;
718         }
719
720         /* I don't think this can happen in a zipfile.. */
721         if (!S_ISDIR(filetype) && !S_ISREG(filetype) && !S_ISLNK(filetype)) {
722                 warningx("skipping non-regular entry '%s'", pathname);
723                 ac(archive_read_data_skip(a));
724                 free(pathname);
725                 return;
726         }
727
728         /* skip directories in -j case */
729         if (S_ISDIR(filetype) && j_opt) {
730                 ac(archive_read_data_skip(a));
731                 free(pathname);
732                 return;
733         }
734
735         /* apply include / exclude patterns */
736         if (!accept_pathname(pathname)) {
737                 ac(archive_read_data_skip(a));
738                 free(pathname);
739                 return;
740         }
741
742         /* apply -j and -d */
743         if (j_opt) {
744                 for (p = q = pathname; *p; ++p)
745                         if (*p == '/')
746                                 q = p + 1;
747                 realpathname = pathcat(d_arg, q);
748         } else {
749                 realpathname = pathcat(d_arg, pathname);
750         }
751
752         /* ensure that parent directory exists */
753         make_parent(realpathname);
754
755         if (S_ISDIR(filetype))
756                 extract_dir(a, e, realpathname);
757         else
758                 extract_file(a, e, &realpathname);
759
760         free(realpathname);
761         free(pathname);
762 }
763
764 static void
765 extract_stdout(struct archive *a, struct archive_entry *e)
766 {
767         char *pathname;
768         mode_t filetype;
769
770         if ((pathname = pathdup(archive_entry_pathname(e))) == NULL) {
771                 warningx("skipping empty or unreadable filename entry");
772                 ac(archive_read_data_skip(a));
773                 return;
774         }
775         filetype = archive_entry_filetype(e);
776
777         /* I don't think this can happen in a zipfile.. */
778         if (!S_ISDIR(filetype) && !S_ISREG(filetype) && !S_ISLNK(filetype)) {
779                 warningx("skipping non-regular entry '%s'", pathname);
780                 ac(archive_read_data_skip(a));
781                 free(pathname);
782                 return;
783         }
784
785         /* skip directories in -j case */
786         if (S_ISDIR(filetype)) {
787                 ac(archive_read_data_skip(a));
788                 free(pathname);
789                 return;
790         }
791
792         /* apply include / exclude patterns */
793         if (!accept_pathname(pathname)) {
794                 ac(archive_read_data_skip(a));
795                 free(pathname);
796                 return;
797         }
798
799         if (c_opt)
800                 info("x %s\n", pathname);
801
802         (void)extract2fd(a, pathname, STDOUT_FILENO);
803
804         free(pathname);
805 }
806
807 /*
808  * Print the name of an entry to stdout.
809  */
810 static void
811 list(struct archive *a, struct archive_entry *e)
812 {
813         char buf[20];
814         time_t mtime;
815         struct tm *tm;
816
817         mtime = archive_entry_mtime(e);
818         tm = localtime(&mtime);
819         if (*y_str)
820                 strftime(buf, sizeof(buf), "%m-%d-%G %R", tm);
821         else
822                 strftime(buf, sizeof(buf), "%m-%d-%g %R", tm);
823
824         if (!zipinfo_mode) {
825                 if (v_opt == 1) {
826                         printf(" %8ju  %s   %s\n",
827                             (uintmax_t)archive_entry_size(e),
828                             buf, archive_entry_pathname(e));
829                 } else if (v_opt == 2) {
830                         printf("%8ju  Stored  %7ju   0%%  %s  %08x  %s\n",
831                             (uintmax_t)archive_entry_size(e),
832                             (uintmax_t)archive_entry_size(e),
833                             buf,
834                             0U,
835                             archive_entry_pathname(e));
836                 }
837         } else {
838                 if (Z1_opt)
839                         printf("%s\n",archive_entry_pathname(e));
840         }
841         ac(archive_read_data_skip(a));
842 }
843
844 /*
845  * Extract to memory to check CRC
846  */
847 static int
848 test(struct archive *a, struct archive_entry *e)
849 {
850         ssize_t len;
851         int error_count;
852
853         error_count = 0;
854         if (S_ISDIR(archive_entry_filetype(e)))
855                 return 0;
856
857         info("    testing: %s\t", archive_entry_pathname(e));
858         while ((len = archive_read_data(a, buffer, sizeof buffer)) > 0)
859                 /* nothing */;
860         if (len < 0) {
861                 info(" %s\n", archive_error_string(a));
862                 ++error_count;
863         } else {
864                 info(" OK\n");
865         }
866
867         /* shouldn't be necessary, but it doesn't hurt */
868         ac(archive_read_data_skip(a));
869
870         return error_count;
871 }
872
873 /*
874  * Callback function for reading passphrase.
875  * Originally from cpio.c and passphrase.c, libarchive.
876  */
877 #define PPBUFF_SIZE 1024
878 static const char *
879 passphrase_callback(struct archive *a, void *_client_data)
880 {
881         char *p;
882
883         (void)a; /* UNUSED */
884         (void)_client_data; /* UNUSED */
885
886         if (passphrase_buf == NULL) {
887                 passphrase_buf = malloc(PPBUFF_SIZE);
888                 if (passphrase_buf == NULL) {
889                         errno = ENOMEM;
890                         error("malloc()");
891                 }
892         }
893
894         p = readpassphrase("\nEnter password: ", passphrase_buf,
895                 PPBUFF_SIZE, RPP_ECHO_OFF);
896
897         if (p == NULL && errno != EINTR)
898                 error("Error reading password");
899
900         return p;
901 }
902
903 /*
904  * Main loop: open the zipfile, iterate over its contents and decide what
905  * to do with each entry.
906  */
907 static void
908 unzip(const char *fn)
909 {
910         struct archive *a;
911         struct archive_entry *e;
912         int ret;
913         uintmax_t total_size, file_count, error_count;
914
915         if ((a = archive_read_new()) == NULL)
916                 error("archive_read_new failed");
917
918         ac(archive_read_support_format_zip(a));
919
920         if (P_arg)
921                 archive_read_add_passphrase(a, P_arg);
922         else
923                 archive_read_set_passphrase_callback(a, NULL,
924                         &passphrase_callback);
925
926         ac(archive_read_open_filename(a, fn, 8192));
927
928         if (!zipinfo_mode) {
929                 if (!p_opt && !q_opt)
930                         printf("Archive:  %s\n", fn);
931                 if (v_opt == 1) {
932                         printf("  Length     %sDate   Time    Name\n", y_str);
933                         printf(" --------    %s----   ----    ----\n", y_str);
934                 } else if (v_opt == 2) {
935                         printf(" Length   Method    Size  Ratio   %sDate   Time   CRC-32    Name\n", y_str);
936                         printf("--------  ------  ------- -----   %s----   ----   ------    ----\n", y_str);
937                 }
938         }
939
940         total_size = 0;
941         file_count = 0;
942         error_count = 0;
943         for (;;) {
944                 ret = archive_read_next_header(a, &e);
945                 if (ret == ARCHIVE_EOF)
946                         break;
947                 ac(ret);
948                 if (!zipinfo_mode) {
949                         if (t_opt)
950                                 error_count += test(a, e);
951                         else if (v_opt)
952                                 list(a, e);
953                         else if (p_opt || c_opt)
954                                 extract_stdout(a, e);
955                         else
956                                 extract(a, e);
957                 } else {
958                         if (Z1_opt)
959                                 list(a, e);
960                 }
961
962                 total_size += archive_entry_size(e);
963                 ++file_count;
964         }
965
966         if (zipinfo_mode) {
967                 if (v_opt == 1) {
968                         printf(" --------                   %s-------\n", y_str);
969                         printf(" %8ju                   %s%ju file%s\n",
970                             total_size, y_str, file_count, file_count != 1 ? "s" : "");
971                 } else if (v_opt == 2) {
972                         printf("--------          -------  ---                            %s-------\n", y_str);
973                         printf("%8ju          %7ju   0%%                            %s%ju file%s\n",
974                             total_size, total_size, y_str, file_count,
975                             file_count != 1 ? "s" : "");
976                 }
977         }
978
979         ac(archive_read_free(a));
980
981         if (passphrase_buf != NULL) {
982                 memset_s(passphrase_buf, PPBUFF_SIZE, 0, PPBUFF_SIZE);
983                 free(passphrase_buf);
984         }
985
986         if (t_opt) {
987                 if (error_count > 0) {
988                         errorx("%ju checksum error(s) found.", error_count);
989                 }
990                 else {
991                         printf("No errors detected in compressed data of %s.\n",
992                                fn);
993                 }
994         }
995 }
996
997 static void
998 usage(void)
999 {
1000
1001         fprintf(stderr,
1002 "Usage: unzip [-aCcfjLlnopqtuvyZ1] [-d dir] [-x pattern] [-P password] zipfile\n"
1003 "             [member ...]\n");
1004         exit(EXIT_FAILURE);
1005 }
1006
1007 static int
1008 getopts(int argc, char *argv[])
1009 {
1010         int opt;
1011
1012         optreset = optind = 1;
1013         while ((opt = getopt(argc, argv, "aCcd:fjLlnopP:qtuvx:yZ1")) != -1)
1014                 switch (opt) {
1015                 case '1':
1016                         Z1_opt = 1;
1017                         break;
1018                 case 'a':
1019                         a_opt = 1;
1020                         break;
1021                 case 'C':
1022                         C_opt = 1;
1023                         break;
1024                 case 'c':
1025                         c_opt = 1;
1026                         break;
1027                 case 'd':
1028                         d_arg = optarg;
1029                         break;
1030                 case 'f':
1031                         f_opt = 1;
1032                         break;
1033                 case 'j':
1034                         j_opt = 1;
1035                         break;
1036                 case 'L':
1037                         L_opt = 1;
1038                         break;
1039                 case 'l':
1040                         if (v_opt == 0)
1041                                 v_opt = 1;
1042                         break;
1043                 case 'n':
1044                         n_opt = 1;
1045                         break;
1046                 case 'o':
1047                         o_opt = 1;
1048                         q_opt = 1;
1049                         break;
1050                 case 'p':
1051                         p_opt = 1;
1052                         break;
1053                 case 'P':
1054                         P_arg = optarg;
1055                         break;
1056                 case 'q':
1057                         q_opt = 1;
1058                         break;
1059                 case 't':
1060                         t_opt = 1;
1061                         break;
1062                 case 'u':
1063                         u_opt = 1;
1064                         break;
1065                 case 'v':
1066                         v_opt = 2;
1067                         break;
1068                 case 'x':
1069                         add_pattern(&exclude, optarg);
1070                         break;
1071                 case 'y':
1072                         y_str = "  ";
1073                         break;
1074                 case 'Z':
1075                         zipinfo_mode = 1;
1076                         break;
1077                 default:
1078                         usage();
1079                 }
1080
1081         return (optind);
1082 }
1083
1084 int
1085 main(int argc, char *argv[])
1086 {
1087         const char *zipfile;
1088         int nopts;
1089
1090         if (isatty(STDOUT_FILENO))
1091                 tty = 1;
1092
1093         if (getenv("UNZIP_DEBUG") != NULL)
1094                 unzip_debug = 1;
1095         for (int i = 0; i < argc; ++i)
1096                 debug("%s%c", argv[i], (i < argc - 1) ? ' ' : '\n');
1097
1098         /*
1099          * Info-ZIP's unzip(1) expects certain options to come before the
1100          * zipfile name, and others to come after - though it does not
1101          * enforce this.  For simplicity, we accept *all* options both
1102          * before and after the zipfile name.
1103          */
1104         nopts = getopts(argc, argv);
1105
1106         /*
1107          * When more of the zipinfo mode options are implemented, this
1108          * will need to change.
1109          */
1110         if (zipinfo_mode && !Z1_opt) {
1111                 printf("Zipinfo mode needs additional options\n");
1112                 exit(EXIT_FAILURE);
1113         }
1114
1115         if (argc <= nopts)
1116                 usage();
1117         zipfile = argv[nopts++];
1118
1119         if (strcmp(zipfile, "-") == 0)
1120                 zipfile = NULL; /* STDIN */
1121
1122         while (nopts < argc && *argv[nopts] != '-')
1123                 add_pattern(&include, argv[nopts++]);
1124
1125         nopts--; /* fake argv[0] */
1126         nopts += getopts(argc - nopts, argv + nopts);
1127
1128         if (n_opt + o_opt + u_opt > 1)
1129                 errorx("-n, -o and -u are contradictory");
1130
1131         unzip(zipfile);
1132
1133         exit(EXIT_SUCCESS);
1134 }