More -Wwrite-strings cleanup and make sure you can actually play it.
[dragonfly.git] / contrib / libarchive / archive_read_extract.c
blob521da3172bc0ce8fa79e69713517c805fa86e46d
1 /*-
2 * Copyright (c) 2003-2004 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_extract.c,v 1.39 2005/04/17 22:49:00 kientzle Exp $");
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_ACL_H
32 #include <sys/acl.h>
33 #endif
34 #ifdef HAVE_SYS_IOCTL_H
35 #include <sys/ioctl.h>
36 #endif
37 #include <sys/stat.h>
38 #include <sys/time.h>
40 #ifdef HAVE_EXT2FS_EXT2_FS_H
41 #include <ext2fs/ext2_fs.h> /* for Linux file flags */
42 #endif
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <grp.h>
46 #ifdef HAVE_LINUX_EXT2_FS_H
47 #include <linux/ext2_fs.h> /* for Linux file flags */
48 #endif
49 #include <limits.h>
50 #include <pwd.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
56 #include "archive.h"
57 #include "archive_string.h"
58 #include "archive_entry.h"
59 #include "archive_private.h"
61 struct fixup_entry {
62 struct fixup_entry *next;
63 mode_t mode;
64 int64_t mtime;
65 int64_t atime;
66 unsigned long mtime_nanos;
67 unsigned long atime_nanos;
68 unsigned long fflags_set;
69 int fixup; /* bitmask of what needs fixing */
70 char *name;
73 #define FIXUP_MODE 1
74 #define FIXUP_TIMES 2
75 #define FIXUP_FFLAGS 4
77 struct bucket {
78 char *name;
79 int hash;
80 id_t id;
83 struct extract {
84 mode_t umask;
85 mode_t default_dir_mode;
86 struct archive_string create_parent_dir;
87 struct fixup_entry *fixup_list;
88 struct fixup_entry *current_fixup;
90 struct bucket ucache[127];
91 struct bucket gcache[127];
94 * Cached stat data from disk for the current entry.
95 * If this is valid, pst points to st. Otherwise,
96 * pst is null.
98 * TODO: Have all of the stat calls use this cached data
99 * if possible.
101 struct stat st;
102 struct stat *pst;
105 /* Default mode for dirs created automatically (will be modified by umask). */
106 #define DEFAULT_DIR_MODE 0777
108 * Mode to use for newly-created dirs during extraction; the correct
109 * mode will be set at the end of the extraction.
111 #define SECURE_DIR_MODE 0700
113 static void archive_extract_cleanup(struct archive *);
114 static int extract_block_device(struct archive *,
115 struct archive_entry *, int);
116 static int extract_char_device(struct archive *,
117 struct archive_entry *, int);
118 static int extract_device(struct archive *,
119 struct archive_entry *, int flags, mode_t mode);
120 static int extract_dir(struct archive *, struct archive_entry *, int);
121 static int extract_fifo(struct archive *, struct archive_entry *, int);
122 static int extract_file(struct archive *, struct archive_entry *, int);
123 static int extract_hard_link(struct archive *, struct archive_entry *, int);
124 static int extract_symlink(struct archive *, struct archive_entry *, int);
125 static unsigned int hash(const char *);
126 static gid_t lookup_gid(struct archive *, const char *uname, gid_t);
127 static uid_t lookup_uid(struct archive *, const char *uname, uid_t);
128 static int create_dir(struct archive *, const char *, int flags);
129 static int create_dir_mutable(struct archive *, char *, int flags);
130 static int create_dir_recursive(struct archive *, char *, int flags);
131 static int create_parent_dir(struct archive *, const char *, int flags);
132 static int create_parent_dir_mutable(struct archive *, char *, int flags);
133 static int restore_metadata(struct archive *, struct archive_entry *,
134 int flags);
135 #ifdef HAVE_POSIX_ACL
136 static int set_acl(struct archive *, struct archive_entry *,
137 acl_type_t, int archive_entry_acl_type, const char *tn);
138 #endif
139 static int set_acls(struct archive *, struct archive_entry *);
140 static int set_fflags(struct archive *, const char *name, mode_t mode,
141 unsigned long fflags_set, unsigned long fflags_clear);
142 static int set_ownership(struct archive *, struct archive_entry *, int);
143 static int set_perm(struct archive *, struct archive_entry *, int mode,
144 int flags);
145 static int set_time(struct archive *, struct archive_entry *, int);
146 static struct fixup_entry *sort_dir_list(struct fixup_entry *p);
150 * Extract this entry to disk.
152 * TODO: Validate hardlinks. According to the standards, we're
153 * supposed to check each extracted hardlink and squawk if it refers
154 * to a file that we didn't restore. I'm not entirely convinced this
155 * is a good idea, but more importantly: Is there any way to validate
156 * hardlinks without keeping a complete list of filenames from the
157 * entire archive?? Ugh.
161 archive_read_extract(struct archive *a, struct archive_entry *entry, int flags)
163 mode_t mode;
164 struct extract *extract;
165 int ret;
166 int restore_pwd;
167 char *original_filename;
169 if (a->extract == NULL) {
170 a->extract = malloc(sizeof(*a->extract));
171 if (a->extract == NULL) {
172 archive_set_error(a, ENOMEM, "Can't extract");
173 return (ARCHIVE_FATAL);
175 a->cleanup_archive_extract = archive_extract_cleanup;
176 memset(a->extract, 0, sizeof(*a->extract));
178 extract = a->extract;
179 umask(extract->umask = umask(0)); /* Read the current umask. */
180 extract->default_dir_mode = DEFAULT_DIR_MODE & ~extract->umask;
181 extract->pst = NULL;
182 extract->current_fixup = NULL;
183 restore_pwd = -1;
184 original_filename = NULL;
187 * If pathname is longer than PATH_MAX, record starting directory
188 * and chdir to a suitable intermediate dir.
190 if (strlen(archive_entry_pathname(entry)) > PATH_MAX) {
191 char *intdir, *tail;
194 * Yes, the copy here is necessary because we edit
195 * the pathname in-place to create intermediate dirnames.
197 original_filename = strdup(archive_entry_pathname(entry));
199 restore_pwd = open(".", O_RDONLY);
201 * "intdir" points to the initial dir section we're going
202 * to remove, "tail" points to the remainder of the path.
204 intdir = tail = original_filename;
205 while (strlen(tail) > PATH_MAX) {
206 intdir = tail;
208 /* Locate a dir prefix shorter than PATH_MAX. */
209 tail = intdir + PATH_MAX - 8;
210 while (tail > intdir && *tail != '/')
211 tail--;
212 if (tail <= intdir) {
213 archive_set_error(a, EPERM,
214 "Path element too long");
215 ret = ARCHIVE_WARN;
216 goto cleanup;
219 /* Create intdir and chdir to it. */
220 *tail = '\0'; /* Terminate dir portion */
221 ret = create_dir(a, intdir, flags);
222 if (ret == ARCHIVE_OK && chdir(intdir) != 0) {
223 archive_set_error(a, errno, "Couldn't chdir");
224 ret = ARCHIVE_WARN;
226 *tail = '/'; /* Restore the / we removed. */
227 if (ret != ARCHIVE_OK)
228 goto cleanup;
229 tail++;
231 archive_entry_set_pathname(entry, tail);
234 if (stat(archive_entry_pathname(entry), &extract->st) == 0)
235 extract->pst = &extract->st;
237 if (extract->pst != NULL &&
238 extract->pst->st_dev == a->skip_file_dev &&
239 extract->pst->st_ino == a->skip_file_ino) {
240 archive_set_error(a, 0, "Refusing to overwrite archive");
241 ret = ARCHIVE_WARN;
242 } else if (archive_entry_hardlink(entry) != NULL)
243 ret = extract_hard_link(a, entry, flags);
244 else {
245 mode = archive_entry_mode(entry);
246 switch (mode & S_IFMT) {
247 default:
248 /* Fall through, as required by POSIX. */
249 case S_IFREG:
250 ret = extract_file(a, entry, flags);
251 break;
252 case S_IFLNK: /* Symlink */
253 ret = extract_symlink(a, entry, flags);
254 break;
255 case S_IFCHR:
256 ret = extract_char_device(a, entry, flags);
257 break;
258 case S_IFBLK:
259 ret = extract_block_device(a, entry, flags);
260 break;
261 case S_IFDIR:
262 ret = extract_dir(a, entry, flags);
263 break;
264 case S_IFIFO:
265 ret = extract_fifo(a, entry, flags);
266 break;
271 cleanup:
272 /* If we changed directory above, restore it here. */
273 if (restore_pwd >= 0 && original_filename != NULL) {
274 fchdir(restore_pwd);
275 close(restore_pwd);
276 archive_entry_copy_pathname(entry, original_filename);
277 free(original_filename);
280 return (ret);
284 * Cleanup function for archive_extract. Mostly, this involves processing
285 * the fixup list, which is used to address a number of problems:
286 * * Dir permissions might prevent us from restoring a file in that
287 * dir, so we restore the dir 0700 first, then correct the
288 * mode at the end.
289 * * Similarly, the act of restoring a file touches the directory
290 * and changes the timestamp on the dir, so we have to touch-up the
291 * timestamps at the end as well.
292 * * Some file flags can interfere with the restore by, for example,
293 * preventing the creation of hardlinks to those files.
295 * Note that tar/cpio do not require that archives be in a particular
296 * order; there is no way to know when the last file has been restored
297 * within a directory, so there's no way to optimize the memory usage
298 * here by fixing up the directory any earlier than the
299 * end-of-archive.
301 * XXX TODO: Directory ACLs should be restored here, for the same
302 * reason we set directory perms here. XXX
304 * Registering this function (rather than calling it explicitly by
305 * name from archive_read_finish) reduces static link pollution, since
306 * applications that don't use this API won't get this file linked in.
308 static void
309 archive_extract_cleanup(struct archive *a)
311 struct fixup_entry *next, *p;
312 struct extract *extract;
314 /* Sort dir list so directories are fixed up in depth-first order. */
315 extract = a->extract;
316 p = sort_dir_list(extract->fixup_list);
318 while (p != NULL) {
319 extract->pst = NULL; /* Mark stat buff as out-of-date. */
320 if (p->fixup & FIXUP_TIMES) {
321 struct timeval times[2];
322 times[1].tv_sec = p->mtime;
323 times[1].tv_usec = p->mtime_nanos / 1000;
324 times[0].tv_sec = p->atime;
325 times[0].tv_usec = p->atime_nanos / 1000;
326 utimes(p->name, times);
328 if (p->fixup & FIXUP_MODE)
329 chmod(p->name, p->mode);
331 if (p->fixup & FIXUP_FFLAGS)
332 set_fflags(a, p->name, p->mode, p->fflags_set, 0);
334 next = p->next;
335 free(p->name);
336 free(p);
337 p = next;
339 extract->fixup_list = NULL;
340 archive_string_free(&extract->create_parent_dir);
341 free(a->extract);
342 a->extract = NULL;
346 * Simple O(n log n) merge sort to order the fixup list. In
347 * particular, we want to restore dir timestamps depth-first.
349 static struct fixup_entry *
350 sort_dir_list(struct fixup_entry *p)
352 struct fixup_entry *a, *b, *t;
354 if (p == NULL)
355 return (NULL);
356 /* A one-item list is already sorted. */
357 if (p->next == NULL)
358 return (p);
360 /* Step 1: split the list. */
361 t = p;
362 a = p->next->next;
363 while (a != NULL) {
364 /* Step a twice, t once. */
365 a = a->next;
366 if (a != NULL)
367 a = a->next;
368 t = t->next;
370 /* Now, t is at the mid-point, so break the list here. */
371 b = t->next;
372 t->next = NULL;
373 a = p;
375 /* Step 2: Recursively sort the two sub-lists. */
376 a = sort_dir_list(a);
377 b = sort_dir_list(b);
379 /* Step 3: Merge the returned lists. */
380 /* Pick the first element for the merged list. */
381 if (strcmp(a->name, b->name) > 0) {
382 t = p = a;
383 a = a->next;
384 } else {
385 t = p = b;
386 b = b->next;
389 /* Always put the later element on the list first. */
390 while (a != NULL && b != NULL) {
391 if (strcmp(a->name, b->name) > 0) {
392 t->next = a;
393 a = a->next;
394 } else {
395 t->next = b;
396 b = b->next;
398 t = t->next;
401 /* Only one list is non-empty, so just splice it on. */
402 if (a != NULL)
403 t->next = a;
404 if (b != NULL)
405 t->next = b;
407 return (p);
411 * Returns a new, initialized fixup entry.
413 * TODO: Reduce the memory requirements for this list by using a tree
414 * structure rather than a simple list of names.
416 static struct fixup_entry *
417 new_fixup(struct archive *a, const char *pathname)
419 struct extract *extract;
420 struct fixup_entry *fe;
422 extract = a->extract;
423 fe = malloc(sizeof(struct fixup_entry));
424 if (fe == NULL)
425 return (NULL);
426 fe->next = extract->fixup_list;
427 extract->fixup_list = fe;
428 fe->fixup = 0;
429 fe->name = strdup(pathname);
430 return (fe);
434 * Returns a fixup structure for the current entry.
436 static struct fixup_entry *
437 current_fixup(struct archive *a, const char *pathname)
439 struct extract *extract;
441 extract = a->extract;
442 if (extract->current_fixup == NULL)
443 extract->current_fixup = new_fixup(a, pathname);
444 return (extract->current_fixup);
447 static int
448 extract_file(struct archive *a, struct archive_entry *entry, int flags)
450 struct extract *extract;
451 const char *name;
452 mode_t mode;
453 int fd, r, r2;
455 extract = a->extract;
456 name = archive_entry_pathname(entry);
457 mode = archive_entry_mode(entry) & 0777;
458 r = ARCHIVE_OK;
461 * If we're not supposed to overwrite pre-existing files,
462 * use O_EXCL. Otherwise, use O_TRUNC.
464 if (flags & (ARCHIVE_EXTRACT_UNLINK | ARCHIVE_EXTRACT_NO_OVERWRITE))
465 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, mode);
466 else
467 fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, mode);
469 /* Try removing a pre-existing file. */
470 if (fd < 0 && !(flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
471 unlink(name);
472 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, mode);
475 /* Might be a non-existent parent dir; try fixing that. */
476 if (fd < 0) {
477 create_parent_dir(a, name, flags);
478 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, mode);
480 if (fd < 0) {
481 archive_set_error(a, errno, "Can't open '%s'", name);
482 return (ARCHIVE_WARN);
484 r = archive_read_data_into_fd(a, fd);
485 close(fd);
486 extract->pst = NULL; /* Cached stat data no longer valid. */
487 r2 = restore_metadata(a, entry, flags);
488 return (err_combine(r, r2));
491 static int
492 extract_dir(struct archive *a, struct archive_entry *entry, int flags)
494 struct extract *extract;
495 struct fixup_entry *fe;
496 char *path, *p;
498 extract = a->extract;
499 extract->pst = NULL; /* Invalidate cached stat data. */
501 /* Copy path to mutable storage. */
502 archive_strcpy(&(extract->create_parent_dir),
503 archive_entry_pathname(entry));
504 path = extract->create_parent_dir.s;
506 /* Deal with any troublesome trailing path elements. */
507 for (;;) {
508 if (*path == '\0')
509 return (ARCHIVE_OK);
510 /* Locate last element; trim trailing '/'. */
511 p = strrchr(path, '/');
512 if (p != NULL) {
513 if (p[1] == '\0') {
514 *p = '\0';
515 continue;
517 p++;
518 } else
519 p = path;
520 /* Trim trailing '.'. */
521 if (p[0] == '.' && p[1] == '\0') {
522 p[0] = '\0';
523 continue;
525 /* Just exit on trailing '..'. */
526 if (p[0] == '.' && p[1] == '.' && p[2] == '\0')
527 return (ARCHIVE_OK);
528 break;
531 if (mkdir(path, SECURE_DIR_MODE) == 0)
532 goto success;
534 if (extract->pst == NULL && stat(path, &extract->st) == 0)
535 extract->pst = &extract->st;
537 if (extract->pst != NULL) {
538 extract->pst = &extract->st;
539 /* If dir already exists, don't reset permissions. */
540 if (S_ISDIR(extract->pst->st_mode))
541 return (ARCHIVE_OK);
542 /* It exists but isn't a dir. */
543 if ((flags & ARCHIVE_EXTRACT_UNLINK))
544 unlink(path);
545 } else {
546 /* Doesn't already exist; try building the parent path. */
547 if (create_parent_dir_mutable(a, path, flags) != ARCHIVE_OK)
548 return (ARCHIVE_WARN);
551 /* One final attempt to create the dir. */
552 if (mkdir(path, SECURE_DIR_MODE) != 0) {
553 archive_set_error(a, errno, "Can't create directory");
554 return (ARCHIVE_WARN);
557 success:
558 /* Add this dir to the fixup list. */
559 fe = current_fixup(a, path);
560 fe->fixup |= FIXUP_MODE;
561 fe->mode = archive_entry_mode(entry);
562 if ((flags & ARCHIVE_EXTRACT_PERM) == 0)
563 fe->mode &= ~extract->umask;
564 if (flags & ARCHIVE_EXTRACT_TIME) {
565 fe->fixup |= FIXUP_TIMES;
566 fe->mtime = archive_entry_mtime(entry);
567 fe->mtime_nanos = archive_entry_mtime_nsec(entry);
568 fe->atime = archive_entry_atime(entry);
569 fe->atime_nanos = archive_entry_atime_nsec(entry);
571 /* For now, set the mode to SECURE_DIR_MODE. */
572 archive_entry_set_mode(entry, SECURE_DIR_MODE);
573 return (restore_metadata(a, entry, flags));
578 * Create the parent of the specified path. Copy the provided
579 * path into mutable storage first.
581 static int
582 create_parent_dir(struct archive *a, const char *path, int flags)
584 int r;
586 /* Copy path to mutable storage. */
587 archive_strcpy(&(a->extract->create_parent_dir), path);
588 r = create_parent_dir_mutable(a, a->extract->create_parent_dir.s, flags);
589 return (r);
593 * Like create_parent_dir, but creates the dir actually requested, not
594 * the parent.
596 static int
597 create_dir(struct archive *a, const char *path, int flags)
599 int r;
600 /* Copy path to mutable storage. */
601 archive_strcpy(&(a->extract->create_parent_dir), path);
602 r = create_dir_mutable(a, a->extract->create_parent_dir.s, flags);
603 return (r);
607 * Create the parent directory of the specified path, assuming path
608 * is already in mutable storage.
610 static int
611 create_parent_dir_mutable(struct archive *a, char *path, int flags)
613 char *slash;
614 int r;
616 /* Remove tail element to obtain parent name. */
617 slash = strrchr(path, '/');
618 if (slash == NULL)
619 return (ARCHIVE_OK);
620 *slash = '\0';
621 r = create_dir_mutable(a, path, flags);
622 *slash = '/';
623 return (r);
627 * Create the specified dir, assuming path is already in
628 * mutable storage.
630 static int
631 create_dir_mutable(struct archive *a, char *path, int flags)
633 mode_t old_umask;
634 int r;
636 old_umask = umask(~SECURE_DIR_MODE);
637 r = create_dir_recursive(a, path, flags);
638 umask(old_umask);
639 return (r);
643 * Create the specified dir, recursing to create parents as necessary.
645 * Returns ARCHIVE_OK if the path exists when we're done here.
646 * Otherwise, returns ARCHIVE_WARN.
648 static int
649 create_dir_recursive(struct archive *a, char *path, int flags)
651 struct stat st;
652 struct extract *extract;
653 struct fixup_entry *le;
654 char *slash, *base;
655 int r;
657 extract = a->extract;
658 r = ARCHIVE_OK;
660 /* Check for special names and just skip them. */
661 slash = strrchr(path, '/');
662 base = strrchr(path, '/');
663 if (slash == NULL)
664 base = path;
665 else
666 base = slash + 1;
668 if (base[0] == '\0' ||
669 (base[0] == '.' && base[1] == '\0') ||
670 (base[0] == '.' && base[1] == '.' && base[2] == '\0')) {
671 /* Don't bother trying to create null path, '.', or '..'. */
672 if (slash != NULL) {
673 *slash = '\0';
674 r = create_dir_recursive(a, path, flags);
675 *slash = '/';
676 return (r);
678 return (ARCHIVE_OK);
682 * Yes, this should be stat() and not lstat(). Using lstat()
683 * here loses the ability to extract through symlinks. Also note
684 * that this should not use the extract->st cache.
686 if (stat(path, &st) == 0) {
687 if (S_ISDIR(st.st_mode))
688 return (ARCHIVE_OK);
689 if ((flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
690 archive_set_error(a, EEXIST,
691 "Can't create directory '%s'", path);
692 return (ARCHIVE_WARN);
694 if (unlink(path) != 0) {
695 archive_set_error(a, errno,
696 "Can't create directory '%s': "
697 "Conflicting file cannot be removed");
698 return (ARCHIVE_WARN);
700 } else if (errno != ENOENT && errno != ENOTDIR) {
701 /* Stat failed? */
702 archive_set_error(a, errno, "Can't test directory '%s'", path);
703 return (ARCHIVE_WARN);
704 } else if (slash != NULL) {
705 *slash = '\0';
706 r = create_dir_recursive(a, path, flags);
707 *slash = '/';
708 if (r != ARCHIVE_OK)
709 return (r);
712 if (mkdir(path, SECURE_DIR_MODE) == 0) {
713 le = new_fixup(a, path);
714 le->fixup |= FIXUP_MODE;
715 le->mode = extract->default_dir_mode;
716 return (ARCHIVE_OK);
720 * Without the following check, a/b/../b/c/d fails at the
721 * second visit to 'b', so 'd' can't be created. Note that we
722 * don't add it to the fixup list here, as it's already been
723 * added.
725 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
726 return (ARCHIVE_OK);
728 archive_set_error(a, errno, "Failed to create dir '%s'", path);
729 return (ARCHIVE_WARN);
732 static int
733 extract_hard_link(struct archive *a, struct archive_entry *entry, int flags)
735 struct extract *extract;
736 int r;
737 const char *pathname;
738 const char *linkname;
740 extract = a->extract;
741 pathname = archive_entry_pathname(entry);
742 linkname = archive_entry_hardlink(entry);
744 /* Just remove any pre-existing file with this name. */
745 if (!(flags & ARCHIVE_EXTRACT_NO_OVERWRITE))
746 unlink(pathname);
748 r = link(linkname, pathname);
749 extract->pst = NULL; /* Invalidate cached stat data. */
751 if (r != 0) {
752 /* Might be a non-existent parent dir; try fixing that. */
753 create_parent_dir(a, pathname, flags);
754 r = link(linkname, pathname);
757 if (r != 0) {
758 /* XXX Better error message here XXX */
759 archive_set_error(a, errno,
760 "Can't restore hardlink to '%s'", linkname);
761 return (ARCHIVE_WARN);
764 /* Set ownership, time, permission information. */
765 r = restore_metadata(a, entry, flags);
766 return (r);
769 static int
770 extract_symlink(struct archive *a, struct archive_entry *entry, int flags)
772 struct extract *extract;
773 int r;
774 const char *pathname;
775 const char *linkname;
777 extract = a->extract;
778 pathname = archive_entry_pathname(entry);
779 linkname = archive_entry_symlink(entry);
781 /* Just remove any pre-existing file with this name. */
782 if (!(flags & ARCHIVE_EXTRACT_NO_OVERWRITE))
783 unlink(pathname);
785 r = symlink(linkname, pathname);
786 extract->pst = NULL; /* Invalidate cached stat data. */
788 if (r != 0) {
789 /* Might be a non-existent parent dir; try fixing that. */
790 create_parent_dir(a, pathname, flags);
791 r = symlink(linkname, pathname);
794 if (r != 0) {
795 /* XXX Better error message here XXX */
796 archive_set_error(a, errno,
797 "Can't restore symlink to '%s'", linkname);
798 return (ARCHIVE_WARN);
801 r = restore_metadata(a, entry, flags);
802 return (r);
805 static int
806 extract_device(struct archive *a, struct archive_entry *entry,
807 int flags, mode_t mode)
809 struct extract *extract;
810 int r;
812 extract = a->extract;
813 /* Just remove any pre-existing file with this name. */
814 if (!(flags & ARCHIVE_EXTRACT_NO_OVERWRITE))
815 unlink(archive_entry_pathname(entry));
817 r = mknod(archive_entry_pathname(entry), mode,
818 archive_entry_rdev(entry));
819 extract->pst = NULL; /* Invalidate cached stat data. */
821 /* Might be a non-existent parent dir; try fixing that. */
822 if (r != 0 && errno == ENOENT) {
823 create_parent_dir(a, archive_entry_pathname(entry), flags);
824 r = mknod(archive_entry_pathname(entry), mode,
825 archive_entry_rdev(entry));
828 if (r != 0) {
829 archive_set_error(a, errno, "Can't restore device node");
830 return (ARCHIVE_WARN);
833 r = restore_metadata(a, entry, flags);
834 return (r);
837 static int
838 extract_char_device(struct archive *a, struct archive_entry *entry, int flags)
840 mode_t mode;
842 mode = (archive_entry_mode(entry) & ~S_IFMT) | S_IFCHR;
843 return (extract_device(a, entry, flags, mode));
846 static int
847 extract_block_device(struct archive *a, struct archive_entry *entry, int flags)
849 mode_t mode;
851 mode = (archive_entry_mode(entry) & ~S_IFMT) | S_IFBLK;
852 return (extract_device(a, entry, flags, mode));
855 static int
856 extract_fifo(struct archive *a, struct archive_entry *entry, int flags)
858 struct extract *extract;
859 int r;
861 extract = a->extract;
862 /* Just remove any pre-existing file with this name. */
863 if (!(flags & ARCHIVE_EXTRACT_NO_OVERWRITE))
864 unlink(archive_entry_pathname(entry));
866 r = mkfifo(archive_entry_pathname(entry),
867 archive_entry_mode(entry));
868 extract->pst = NULL; /* Invalidate cached stat data. */
870 /* Might be a non-existent parent dir; try fixing that. */
871 if (r != 0 && errno == ENOENT) {
872 create_parent_dir(a, archive_entry_pathname(entry), flags);
873 r = mkfifo(archive_entry_pathname(entry),
874 archive_entry_mode(entry));
877 if (r != 0) {
878 archive_set_error(a, errno, "Can't restore fifo");
879 return (ARCHIVE_WARN);
882 r = restore_metadata(a, entry, flags);
883 return (r);
886 static int
887 restore_metadata(struct archive *a, struct archive_entry *entry, int flags)
889 int r, r2;
891 r = set_ownership(a, entry, flags);
892 r2 = set_time(a, entry, flags);
893 r = err_combine(r, r2);
894 r2 = set_perm(a, entry, archive_entry_mode(entry), flags);
895 return (err_combine(r, r2));
898 static int
899 set_ownership(struct archive *a, struct archive_entry *entry, int flags)
901 uid_t uid;
902 gid_t gid;
904 /* Not changed. */
905 if ((flags & ARCHIVE_EXTRACT_OWNER) == 0)
906 return (ARCHIVE_OK);
908 uid = lookup_uid(a, archive_entry_uname(entry),
909 archive_entry_uid(entry));
910 gid = lookup_gid(a, archive_entry_gname(entry),
911 archive_entry_gid(entry));
913 /* If we know we can't change it, don't bother trying. */
914 if (a->user_uid != 0 && a->user_uid != uid)
915 return (ARCHIVE_OK);
917 #ifdef HAVE_LCHOWN
918 if (lchown(archive_entry_pathname(entry), uid, gid))
919 #else
920 if (!S_ISLNK(archive_entry_mode(entry))
921 && chown(archive_entry_pathname(entry), uid, gid) != 0)
922 #endif
924 archive_set_error(a, errno,
925 "Can't set user=%d/group=%d for %s", uid, gid,
926 archive_entry_pathname(entry));
927 return (ARCHIVE_WARN);
929 return (ARCHIVE_OK);
932 static int
933 set_time(struct archive *a, struct archive_entry *entry, int flags)
935 const struct stat *st;
936 struct timeval times[2];
938 (void)a; /* UNUSED */
939 st = archive_entry_stat(entry);
941 if ((flags & ARCHIVE_EXTRACT_TIME) == 0)
942 return (ARCHIVE_OK);
943 /* It's a waste of time to mess with dir timestamps here. */
944 if (S_ISDIR(archive_entry_mode(entry)))
945 return (ARCHIVE_OK);
947 times[1].tv_sec = st->st_mtime;
948 times[1].tv_usec = ARCHIVE_STAT_MTIME_NANOS(st) / 1000;
950 times[0].tv_sec = st->st_atime;
951 times[0].tv_usec = ARCHIVE_STAT_ATIME_NANOS(st) / 1000;
953 #ifdef HAVE_LUTIMES
954 if (lutimes(archive_entry_pathname(entry), times) != 0) {
955 #else
956 if ((archive_entry_mode(entry) & S_IFMT) != S_IFLNK &&
957 utimes(archive_entry_pathname(entry), times) != 0) {
958 #endif
959 archive_set_error(a, errno, "Can't update time for %s",
960 archive_entry_pathname(entry));
961 return (ARCHIVE_WARN);
965 * Note: POSIX does not provide a portable way to restore ctime.
966 * (Apart from resetting the system clock, which is distasteful.)
967 * So, any restoration of ctime will necessarily be OS-specific.
970 /* XXX TODO: Can FreeBSD restore ctime? XXX */
972 return (ARCHIVE_OK);
975 static int
976 set_perm(struct archive *a, struct archive_entry *entry, int mode, int flags)
978 struct extract *extract;
979 struct fixup_entry *le;
980 const char *name;
981 unsigned long set, clear;
982 int r;
983 int critical_flags;
985 extract = a->extract;
987 /* Obey umask unless ARCHIVE_EXTRACT_PERM. */
988 if ((flags & ARCHIVE_EXTRACT_PERM) == 0)
989 mode &= ~extract->umask; /* Enforce umask. */
990 name = archive_entry_pathname(entry);
992 if (mode & (S_ISUID | S_ISGID)) {
993 if (extract->pst == NULL && stat(name, &extract->st) != 0) {
994 archive_set_error(a, errno, "Can't check ownership");
995 return (ARCHIVE_WARN);
997 extract->pst = &extract->st;
999 * TODO: Use the uid/gid looked up in set_ownership
1000 * above rather than the uid/gid stored in the entry.
1002 if (extract->pst->st_uid != archive_entry_uid(entry))
1003 mode &= ~ S_ISUID;
1004 if (extract->pst->st_gid != archive_entry_gid(entry))
1005 mode &= ~ S_ISGID;
1009 * Ensure we change permissions on the object we extracted,
1010 * and not any incidental symlink that might have gotten in
1011 * the way.
1013 if (!S_ISLNK(archive_entry_mode(entry))) {
1014 if (chmod(name, mode) != 0) {
1015 archive_set_error(a, errno, "Can't set permissions");
1016 return (ARCHIVE_WARN);
1018 #ifdef HAVE_LCHMOD
1019 } else {
1021 * If lchmod() isn't supported, it's no big deal.
1022 * Permissions on symlinks are actually ignored on
1023 * most platforms.
1025 if (lchmod(name, mode) != 0) {
1026 archive_set_error(a, errno, "Can't set permissions");
1027 return (ARCHIVE_WARN);
1029 #endif
1032 if (flags & ARCHIVE_EXTRACT_ACL) {
1033 r = set_acls(a, entry);
1034 if (r != ARCHIVE_OK)
1035 return (r);
1039 * Make 'critical_flags' hold all file flags that can't be
1040 * immediately restored. For example, on BSD systems,
1041 * SF_IMMUTABLE prevents hardlinks from being created, so
1042 * should not be set until after any hardlinks are created. To
1043 * preserve some semblance of portability, this uses #ifdef
1044 * extensively. Ugly, but it works.
1046 * Yes, Virginia, this does create a security race. It's mitigated
1047 * somewhat by the practice of creating dirs 0700 until the extract
1048 * is done, but it would be nice if we could do more than that.
1049 * People restoring critical file systems should be wary of
1050 * other programs that might try to muck with files as they're
1051 * being restored.
1053 /* Hopefully, the compiler will optimize this mess into a constant. */
1054 critical_flags = 0;
1055 #ifdef SF_IMMUTABLE
1056 critical_flags |= SF_IMMUTABLE;
1057 #endif
1058 #ifdef UF_IMMUTABLE
1059 critical_flags |= UF_IMMUTABLE;
1060 #endif
1061 #ifdef SF_APPEND
1062 critical_flags |= SF_APPEND;
1063 #endif
1064 #ifdef UF_APPEND
1065 critical_flags |= UF_APPEND;
1066 #endif
1067 #ifdef EXT2_APPEND_FL
1068 critical_flags |= EXT2_APPEND_FL;
1069 #endif
1070 #ifdef EXT2_IMMUTABLE_FL
1071 critical_flags |= EXT2_IMMUTABLE_FL;
1072 #endif
1074 if (flags & ARCHIVE_EXTRACT_FFLAGS) {
1075 archive_entry_fflags(entry, &set, &clear);
1078 * The first test encourages the compiler to eliminate
1079 * all of this if it's not necessary.
1081 if ((critical_flags != 0) && (set & critical_flags)) {
1082 le = current_fixup(a, archive_entry_pathname(entry));
1083 le->fixup |= FIXUP_FFLAGS;
1084 le->fflags_set = set;
1085 /* Store the mode if it's not already there. */
1086 if ((le->fixup & FIXUP_MODE) == 0)
1087 le->mode = mode;
1088 } else {
1089 r = set_fflags(a, archive_entry_pathname(entry),
1090 mode, set, clear);
1091 if (r != ARCHIVE_OK)
1092 return (r);
1095 return (ARCHIVE_OK);
1098 static int
1099 set_fflags(struct archive *a, const char *name, mode_t mode,
1100 unsigned long set, unsigned long clear)
1102 struct extract *extract;
1103 int ret;
1104 #ifdef linux
1105 int fd;
1106 int err;
1107 unsigned long newflags, oldflags;
1108 #endif
1110 extract = a->extract;
1111 ret = ARCHIVE_OK;
1112 if (set == 0 && clear == 0)
1113 return (ret);
1115 #ifdef HAVE_CHFLAGS
1116 (void)mode; /* UNUSED */
1118 * XXX Is the stat here really necessary? Or can I just use
1119 * the 'set' flags directly? In particular, I'm not sure
1120 * about the correct approach if we're overwriting an existing
1121 * file that already has flags on it. XXX
1123 if (stat(name, &extract->st) == 0) {
1124 extract->st.st_flags &= ~clear;
1125 extract->st.st_flags |= set;
1126 if (chflags(name, extract->st.st_flags) != 0) {
1127 archive_set_error(a, errno,
1128 "Failed to set file flags");
1129 ret = ARCHIVE_WARN;
1131 extract->pst = &extract->st;
1133 #else
1134 #ifdef linux
1135 /* Linux has flags too, but no chflags syscall */
1137 * Linux has no define for the flags that are only settable
1138 * by the root user...
1140 #define SF_MASK (EXT2_IMMUTABLE_FL|EXT2_APPEND_FL)
1143 * XXX As above, this would be way simpler if we didn't have
1144 * to read the current flags from disk. XXX
1146 if ((S_ISREG(mode) || S_ISDIR(mode)) &&
1147 ((fd = open(name, O_RDONLY|O_NONBLOCK)) >= 0)) {
1148 err = 1;
1149 if (fd >= 0 && (ioctl(fd, EXT2_IOC_GETFLAGS, &oldflags) >= 0)) {
1150 newflags = (oldflags & ~clear) | set;
1151 if (ioctl(fd, EXT2_IOC_SETFLAGS, &newflags) >= 0) {
1152 err = 0;
1153 } else if (errno == EPERM) {
1154 if (ioctl(fd, EXT2_IOC_GETFLAGS, &oldflags) >= 0) {
1155 newflags &= ~SF_MASK;
1156 oldflags &= SF_MASK;
1157 newflags |= oldflags;
1158 if (ioctl(fd, EXT2_IOC_SETFLAGS, &newflags) >= 0)
1159 err = 0;
1163 close(fd);
1164 if (err) {
1165 archive_set_error(a, errno,
1166 "Failed to set file flags");
1167 ret = ARCHIVE_WARN;
1170 #endif /* linux */
1171 #endif /* HAVE_CHFLAGS */
1173 return (ret);
1176 #ifndef HAVE_POSIX_ACL
1177 /* Default empty function body to satisfy mainline code. */
1178 static int
1179 set_acls(struct archive *a, struct archive_entry *entry)
1181 (void)a;
1182 (void)entry;
1184 return (ARCHIVE_OK);
1187 #else
1190 * XXX TODO: What about ACL types other than ACCESS and DEFAULT?
1192 static int
1193 set_acls(struct archive *a, struct archive_entry *entry)
1195 int ret;
1197 ret = set_acl(a, entry, ACL_TYPE_ACCESS,
1198 ARCHIVE_ENTRY_ACL_TYPE_ACCESS, "access");
1199 if (ret != ARCHIVE_OK)
1200 return (ret);
1201 ret = set_acl(a, entry, ACL_TYPE_DEFAULT,
1202 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT, "default");
1203 return (ret);
1207 static int
1208 set_acl(struct archive *a, struct archive_entry *entry, acl_type_t acl_type,
1209 int ae_requested_type, const char *typename)
1211 acl_t acl;
1212 acl_entry_t acl_entry;
1213 acl_permset_t acl_permset;
1214 int ret;
1215 int ae_type, ae_permset, ae_tag, ae_id;
1216 uid_t ae_uid;
1217 gid_t ae_gid;
1218 const char *ae_name;
1219 int entries;
1220 const char *name;
1222 ret = ARCHIVE_OK;
1223 entries = archive_entry_acl_reset(entry, ae_requested_type);
1224 if (entries == 0)
1225 return (ARCHIVE_OK);
1226 acl = acl_init(entries);
1227 while (archive_entry_acl_next(entry, ae_requested_type, &ae_type,
1228 &ae_permset, &ae_tag, &ae_id, &ae_name) == ARCHIVE_OK) {
1229 acl_create_entry(&acl, &acl_entry);
1231 switch (ae_tag) {
1232 case ARCHIVE_ENTRY_ACL_USER:
1233 acl_set_tag_type(acl_entry, ACL_USER);
1234 ae_uid = lookup_uid(a, ae_name, ae_id);
1235 acl_set_qualifier(acl_entry, &ae_uid);
1236 break;
1237 case ARCHIVE_ENTRY_ACL_GROUP:
1238 acl_set_tag_type(acl_entry, ACL_GROUP);
1239 ae_gid = lookup_gid(a, ae_name, ae_id);
1240 acl_set_qualifier(acl_entry, &ae_gid);
1241 break;
1242 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1243 acl_set_tag_type(acl_entry, ACL_USER_OBJ);
1244 break;
1245 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1246 acl_set_tag_type(acl_entry, ACL_GROUP_OBJ);
1247 break;
1248 case ARCHIVE_ENTRY_ACL_MASK:
1249 acl_set_tag_type(acl_entry, ACL_MASK);
1250 break;
1251 case ARCHIVE_ENTRY_ACL_OTHER:
1252 acl_set_tag_type(acl_entry, ACL_OTHER);
1253 break;
1254 default:
1255 /* XXX */
1256 break;
1259 acl_get_permset(acl_entry, &acl_permset);
1260 acl_clear_perms(acl_permset);
1261 if (ae_permset & ARCHIVE_ENTRY_ACL_EXECUTE)
1262 acl_add_perm(acl_permset, ACL_EXECUTE);
1263 if (ae_permset & ARCHIVE_ENTRY_ACL_WRITE)
1264 acl_add_perm(acl_permset, ACL_WRITE);
1265 if (ae_permset & ARCHIVE_ENTRY_ACL_READ)
1266 acl_add_perm(acl_permset, ACL_READ);
1269 name = archive_entry_pathname(entry);
1271 if (acl_set_file(name, acl_type, acl) != 0) {
1272 archive_set_error(a, errno, "Failed to set %s acl", typename);
1273 ret = ARCHIVE_WARN;
1275 acl_free(acl);
1276 return (ret);
1278 #endif
1281 * The following routines do some basic caching of uname/gname lookups.
1282 * All such lookups go through these routines, including ACL conversions.
1284 * TODO: Provide an API for clients to override these routines.
1286 static gid_t
1287 lookup_gid(struct archive *a, const char *gname, gid_t gid)
1289 struct group *grent;
1290 struct extract *extract;
1291 int h;
1292 struct bucket *b;
1293 int cache_size;
1295 extract = a->extract;
1296 cache_size = sizeof(extract->gcache) / sizeof(extract->gcache[0]);
1298 /* If no gname, just use the gid provided. */
1299 if (gname == NULL || *gname == '\0')
1300 return (gid);
1302 /* Try to find gname in the cache. */
1303 h = hash(gname);
1304 b = &extract->gcache[h % cache_size ];
1305 if (b->name != NULL && b->hash == h && strcmp(gname, b->name) == 0)
1306 return ((gid_t)b->id);
1308 /* Free the cache slot for a new entry. */
1309 if (b->name != NULL)
1310 free(b->name);
1311 b->name = strdup(gname);
1312 /* Note: If strdup fails, that's okay; we just won't cache. */
1313 b->hash = h;
1314 grent = getgrnam(gname);
1315 if (grent != NULL)
1316 gid = grent->gr_gid;
1317 b->id = gid;
1319 return (gid);
1322 static uid_t
1323 lookup_uid(struct archive *a, const char *uname, uid_t uid)
1325 struct passwd *pwent;
1326 struct extract *extract;
1327 int h;
1328 struct bucket *b;
1329 int cache_size;
1331 extract = a->extract;
1332 cache_size = sizeof(extract->ucache) / sizeof(extract->ucache[0]);
1334 /* If no uname, just use the uid provided. */
1335 if (uname == NULL || *uname == '\0')
1336 return (uid);
1338 /* Try to find uname in the cache. */
1339 h = hash(uname);
1340 b = &extract->ucache[h % cache_size ];
1341 if (b->name != NULL && b->hash == h && strcmp(uname, b->name) == 0)
1342 return ((uid_t)b->id);
1344 /* Free the cache slot for a new entry. */
1345 if (b->name != NULL)
1346 free(b->name);
1347 b->name = strdup(uname);
1348 /* Note: If strdup fails, that's okay; we just won't cache. */
1349 b->hash = h;
1350 pwent = getpwnam(uname);
1351 if (pwent != NULL)
1352 uid = pwent->pw_uid;
1353 b->id = uid;
1355 return (uid);
1358 static unsigned int
1359 hash(const char *p)
1361 /* A 32-bit version of Peter Weinberger's (PJW) hash algorithm,
1362 as used by ELF for hashing function names. */
1363 unsigned g,h = 0;
1364 while(*p != '\0') {
1365 h = ( h << 4 ) + *p++;
1366 if (( g = h & 0xF0000000 )) {
1367 h ^= g >> 24;
1368 h &= 0x0FFFFFFF;
1371 return h;
1374 void
1375 archive_read_extract_set_progress_callback(struct archive *a,
1376 void (*progress_func)(void *), void *user_data)
1378 a->extract_progress = progress_func;
1379 a->extract_progress_user_data = user_data;