2004-02-09 23:22:54 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2003-2004 Tim Kientzle
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer
|
|
|
|
* in this position and unchanged.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2004-03-09 19:50:41 +00:00
|
|
|
#include "archive_platform.h"
|
2004-02-09 23:22:54 +00:00
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2004-02-12 20:35:59 +00:00
|
|
|
#include <sys/types.h>
|
2004-03-09 19:50:41 +00:00
|
|
|
#ifdef HAVE_POSIX_ACL
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <sys/acl.h>
|
2004-03-09 19:50:41 +00:00
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2004-04-26 23:37:54 +00:00
|
|
|
#ifdef LINUX
|
|
|
|
#include <ext2fs/ext2_fs.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
#include "archive.h"
|
|
|
|
#include "archive_string.h"
|
|
|
|
#include "archive_entry.h"
|
|
|
|
#include "archive_private.h"
|
|
|
|
|
2004-06-03 23:29:47 +00:00
|
|
|
struct fixup_entry {
|
|
|
|
struct fixup_entry *next;
|
|
|
|
mode_t mode;
|
|
|
|
int64_t mtime;
|
|
|
|
int64_t atime;
|
|
|
|
unsigned long mtime_nanos;
|
|
|
|
unsigned long atime_nanos;
|
|
|
|
unsigned long fflags_set;
|
|
|
|
int fixup; /* bitmask of what needs fixing */
|
|
|
|
char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define FIXUP_MODE 1
|
|
|
|
#define FIXUP_TIMES 2
|
|
|
|
#define FIXUP_FFLAGS 4
|
|
|
|
|
|
|
|
struct extract {
|
2004-06-27 16:44:51 +00:00
|
|
|
mode_t umask;
|
2004-06-28 14:12:09 +00:00
|
|
|
mode_t default_dir_mode;
|
2004-06-03 23:29:47 +00:00
|
|
|
struct archive_string mkdirpath;
|
|
|
|
struct fixup_entry *fixup_list;
|
2004-06-27 23:27:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Cached stat data from disk for the current entry.
|
|
|
|
* If this is valid, pst points to st. Otherwise,
|
|
|
|
* pst is null.
|
|
|
|
*
|
|
|
|
* TODO: Have all of the stat calls use this cached data
|
|
|
|
* if possible.
|
|
|
|
*/
|
|
|
|
struct stat st;
|
|
|
|
struct stat *pst;
|
2004-06-03 23:29:47 +00:00
|
|
|
};
|
|
|
|
|
2004-06-27 23:27:28 +00:00
|
|
|
/* Default mode for dirs created automatically (will be modified by umask). */
|
2004-06-27 05:59:15 +00:00
|
|
|
#define DEFAULT_DIR_MODE 0777
|
2004-06-27 03:19:01 +00:00
|
|
|
/*
|
|
|
|
* Mode to use for newly-created dirs during extraction; the correct
|
|
|
|
* mode will be set at the end of the extraction.
|
|
|
|
*/
|
|
|
|
#define SECURE_DIR_MODE 0700
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
static void archive_extract_cleanup(struct archive *);
|
2004-06-27 23:27:28 +00:00
|
|
|
static int extract_block_device(struct archive *,
|
2004-02-09 23:22:54 +00:00
|
|
|
struct archive_entry *, int);
|
2004-06-27 23:27:28 +00:00
|
|
|
static int extract_char_device(struct archive *,
|
2004-02-09 23:22:54 +00:00
|
|
|
struct archive_entry *, int);
|
2004-06-27 23:27:28 +00:00
|
|
|
static int extract_device(struct archive *,
|
2004-02-09 23:22:54 +00:00
|
|
|
struct archive_entry *, int flags, mode_t mode);
|
2004-06-27 23:27:28 +00:00
|
|
|
static int extract_dir(struct archive *, struct archive_entry *, int);
|
|
|
|
static int extract_fifo(struct archive *, struct archive_entry *, int);
|
|
|
|
static int extract_file(struct archive *, struct archive_entry *, int);
|
|
|
|
static int extract_hard_link(struct archive *, struct archive_entry *, int);
|
|
|
|
static int extract_symlink(struct archive *, struct archive_entry *, int);
|
2004-04-05 21:12:29 +00:00
|
|
|
static gid_t lookup_gid(struct archive *, const char *uname, gid_t);
|
|
|
|
static uid_t lookup_uid(struct archive *, const char *uname, uid_t);
|
2004-02-09 23:22:54 +00:00
|
|
|
static int mkdirpath(struct archive *, const char *);
|
2004-06-27 03:19:01 +00:00
|
|
|
static int mkdirpath_recursive(struct archive *, char *,
|
|
|
|
const struct stat *, mode_t, int);
|
2004-06-27 23:27:28 +00:00
|
|
|
static int restore_metadata(struct archive *, struct archive_entry *,
|
|
|
|
int flags);
|
2004-03-09 19:50:41 +00:00
|
|
|
#ifdef HAVE_POSIX_ACL
|
2004-04-05 21:12:29 +00:00
|
|
|
static int set_acl(struct archive *, struct archive_entry *,
|
2004-04-06 23:16:50 +00:00
|
|
|
acl_type_t, int archive_entry_acl_type, const char *tn);
|
2004-03-09 19:50:41 +00:00
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
static int set_acls(struct archive *, struct archive_entry *);
|
2004-05-27 05:02:35 +00:00
|
|
|
static int set_fflags(struct archive *, const char *name, mode_t mode,
|
|
|
|
unsigned long fflags_set, unsigned long fflags_clear);
|
2004-02-09 23:22:54 +00:00
|
|
|
static int set_ownership(struct archive *, struct archive_entry *, int);
|
2004-03-09 19:50:41 +00:00
|
|
|
static int set_perm(struct archive *, struct archive_entry *, int mode,
|
|
|
|
int flags);
|
2004-02-09 23:22:54 +00:00
|
|
|
static int set_time(struct archive *, struct archive_entry *, int);
|
2004-06-03 23:29:47 +00:00
|
|
|
static struct fixup_entry *sort_dir_list(struct fixup_entry *p);
|
2004-03-09 19:50:41 +00:00
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract this entry to disk.
|
|
|
|
*
|
2004-06-03 23:29:47 +00:00
|
|
|
* TODO: Validate hardlinks. According to the standards, we're
|
|
|
|
* supposed to check each extracted hardlink and squawk if it refers
|
|
|
|
* to a file that we didn't restore. I'm not entirely convinced this
|
|
|
|
* is a good idea, but more importantly: Is there any way to validate
|
|
|
|
* hardlinks without keeping a complete list of filenames from the
|
|
|
|
* entire archive?? Ugh.
|
2004-02-09 23:22:54 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
archive_read_extract(struct archive *a, struct archive_entry *entry, int flags)
|
|
|
|
{
|
2004-06-03 23:29:47 +00:00
|
|
|
mode_t mode;
|
|
|
|
struct extract *extract;
|
2004-02-09 23:22:54 +00:00
|
|
|
int ret;
|
|
|
|
int restore_pwd;
|
|
|
|
|
2004-06-03 23:29:47 +00:00
|
|
|
if (a->extract == NULL) {
|
|
|
|
a->extract = malloc(sizeof(*a->extract));
|
|
|
|
if (a->extract == NULL) {
|
|
|
|
archive_set_error(a, ENOMEM, "Can't extract");
|
|
|
|
return (ARCHIVE_FATAL);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2004-06-03 23:29:47 +00:00
|
|
|
a->cleanup_archive_extract = archive_extract_cleanup;
|
|
|
|
memset(a->extract, 0, sizeof(*a->extract));
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2004-06-03 23:29:47 +00:00
|
|
|
extract = a->extract;
|
2004-06-27 16:44:51 +00:00
|
|
|
umask(extract->umask = umask(0)); /* Read the current umask. */
|
2004-06-28 14:12:09 +00:00
|
|
|
extract->default_dir_mode = DEFAULT_DIR_MODE & ~extract->umask;
|
2004-06-27 23:27:28 +00:00
|
|
|
extract->pst = NULL;
|
2004-06-03 23:29:47 +00:00
|
|
|
restore_pwd = -1;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: If pathname is longer than PATH_MAX, record starting
|
|
|
|
* directory and move to a suitable intermediate dir, which
|
|
|
|
* might require creating them!
|
|
|
|
*/
|
|
|
|
if (strlen(archive_entry_pathname(entry)) > PATH_MAX) {
|
|
|
|
restore_pwd = open(".", O_RDONLY);
|
|
|
|
/* XXX chdir() to a suitable intermediate dir XXX */
|
|
|
|
/* XXX Update pathname in 'entry' XXX */
|
|
|
|
}
|
|
|
|
|
2004-06-27 23:27:28 +00:00
|
|
|
if (stat(archive_entry_pathname(entry), &extract->st) == 0)
|
|
|
|
extract->pst = &extract->st;
|
|
|
|
|
|
|
|
if (extract->pst != NULL &&
|
|
|
|
extract->pst->st_dev == a->skip_file_dev &&
|
|
|
|
extract->pst->st_ino == a->skip_file_ino) {
|
|
|
|
archive_set_error(a, 0, "Refusing to overwrite archive");
|
|
|
|
ret = ARCHIVE_WARN;
|
|
|
|
} else if (archive_entry_hardlink(entry) != NULL)
|
|
|
|
ret = extract_hard_link(a, entry, flags);
|
|
|
|
else {
|
|
|
|
mode = archive_entry_mode(entry);
|
|
|
|
switch (mode & S_IFMT) {
|
|
|
|
default:
|
|
|
|
/* Fall through, as required by POSIX. */
|
|
|
|
case S_IFREG:
|
|
|
|
ret = extract_file(a, entry, flags);
|
|
|
|
break;
|
|
|
|
case S_IFLNK: /* Symlink */
|
|
|
|
ret = extract_symlink(a, entry, flags);
|
|
|
|
break;
|
|
|
|
case S_IFCHR:
|
|
|
|
ret = extract_char_device(a, entry, flags);
|
|
|
|
break;
|
|
|
|
case S_IFBLK:
|
|
|
|
ret = extract_block_device(a, entry, flags);
|
|
|
|
break;
|
|
|
|
case S_IFDIR:
|
|
|
|
ret = extract_dir(a, entry, flags);
|
|
|
|
break;
|
|
|
|
case S_IFIFO:
|
|
|
|
ret = extract_fifo(a, entry, flags);
|
|
|
|
break;
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we changed directory above, restore it here. */
|
|
|
|
if (restore_pwd >= 0)
|
|
|
|
fchdir(restore_pwd);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cleanup function for archive_extract. Free name/mode list and
|
2004-03-09 19:50:41 +00:00
|
|
|
* restore permissions and dir timestamps. This must be done last;
|
|
|
|
* otherwise, the dir permission might prevent us from restoring a
|
|
|
|
* file. Similarly, the act of restoring a file touches the directory
|
|
|
|
* and changes the timestamp on the dir, so we have to touch-up the
|
|
|
|
* timestamps at the end as well. Note that tar/cpio do not require
|
|
|
|
* that archives be in a particular order; there is no way to know
|
|
|
|
* when the last file has been restored within a directory, so there's
|
|
|
|
* no way to optimize the memory usage here by fixing up the directory
|
|
|
|
* any earlier than the end-of-archive.
|
2004-02-09 23:22:54 +00:00
|
|
|
*
|
2004-03-09 19:50:41 +00:00
|
|
|
* XXX TODO: Directory ACLs should be restored here, for the same
|
|
|
|
* reason we set directory perms here. XXX
|
2004-02-09 23:22:54 +00:00
|
|
|
*
|
|
|
|
* Registering this function (rather than calling it explicitly by
|
2004-03-09 19:50:41 +00:00
|
|
|
* name from archive_read_finish) reduces static link pollution, since
|
2004-02-09 23:22:54 +00:00
|
|
|
* applications that don't use this API won't get this file linked in.
|
|
|
|
*/
|
2004-06-27 23:27:28 +00:00
|
|
|
static void
|
|
|
|
archive_extract_cleanup(struct archive *a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2004-06-03 23:29:47 +00:00
|
|
|
struct fixup_entry *next, *p;
|
|
|
|
struct extract *extract;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-03-09 19:50:41 +00:00
|
|
|
/* Sort dir list so directories are fixed up in depth-first order. */
|
2004-06-03 23:29:47 +00:00
|
|
|
extract = a->extract;
|
|
|
|
p = sort_dir_list(extract->fixup_list);
|
2004-03-09 19:50:41 +00:00
|
|
|
|
|
|
|
while (p != NULL) {
|
2004-05-27 05:02:35 +00:00
|
|
|
if (p->fixup & FIXUP_TIMES) {
|
|
|
|
struct timeval times[2];
|
|
|
|
times[1].tv_sec = p->mtime;
|
|
|
|
times[1].tv_usec = p->mtime_nanos / 1000;
|
|
|
|
times[0].tv_sec = p->atime;
|
|
|
|
times[0].tv_usec = p->atime_nanos / 1000;
|
|
|
|
utimes(p->name, times);
|
|
|
|
}
|
|
|
|
if (p->fixup & FIXUP_MODE)
|
2004-06-27 16:44:51 +00:00
|
|
|
chmod(p->name, p->mode);
|
2004-03-09 19:50:41 +00:00
|
|
|
|
2004-05-27 05:02:35 +00:00
|
|
|
if (p->fixup & FIXUP_FFLAGS)
|
|
|
|
set_fflags(a, p->name, p->mode, p->fflags_set, 0);
|
2004-03-09 19:50:41 +00:00
|
|
|
|
|
|
|
next = p->next;
|
|
|
|
free(p->name);
|
|
|
|
free(p);
|
|
|
|
p = next;
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2004-06-03 23:29:47 +00:00
|
|
|
extract->fixup_list = NULL;
|
|
|
|
archive_string_free(&extract->mkdirpath);
|
|
|
|
free(a->extract);
|
|
|
|
a->extract = NULL;
|
2004-03-09 19:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-05-27 05:02:35 +00:00
|
|
|
* Simple O(n log n) merge sort to order the fixup list. In
|
|
|
|
* particular, we want to restore dir timestamps depth-first.
|
2004-03-09 19:50:41 +00:00
|
|
|
*/
|
2004-06-03 23:29:47 +00:00
|
|
|
static struct fixup_entry *
|
|
|
|
sort_dir_list(struct fixup_entry *p)
|
2004-03-09 19:50:41 +00:00
|
|
|
{
|
2004-06-03 23:29:47 +00:00
|
|
|
struct fixup_entry *a, *b, *t;
|
2004-03-09 19:50:41 +00:00
|
|
|
|
|
|
|
if (p == NULL)
|
2004-05-27 05:02:35 +00:00
|
|
|
return (NULL);
|
2004-03-09 19:50:41 +00:00
|
|
|
/* A one-item list is already sorted. */
|
|
|
|
if (p->next == NULL)
|
|
|
|
return (p);
|
|
|
|
|
|
|
|
/* Step 1: split the list. */
|
|
|
|
t = p;
|
|
|
|
a = p->next->next;
|
|
|
|
while (a != NULL) {
|
|
|
|
/* Step a twice, t once. */
|
|
|
|
a = a->next;
|
|
|
|
if (a != NULL)
|
|
|
|
a = a->next;
|
|
|
|
t = t->next;
|
|
|
|
}
|
|
|
|
/* Now, t is at the mid-point, so break the list here. */
|
|
|
|
b = t->next;
|
|
|
|
t->next = NULL;
|
|
|
|
a = p;
|
|
|
|
|
|
|
|
/* Step 2: Recursively sort the two sub-lists. */
|
|
|
|
a = sort_dir_list(a);
|
|
|
|
b = sort_dir_list(b);
|
|
|
|
|
|
|
|
/* Step 3: Merge the returned lists. */
|
|
|
|
/* Pick the first element for the merged list. */
|
|
|
|
if (strcmp(a->name, b->name) > 0) {
|
|
|
|
t = p = a;
|
|
|
|
a = a->next;
|
|
|
|
} else {
|
|
|
|
t = p = b;
|
|
|
|
b = b->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Always put the later element on the list first. */
|
|
|
|
while (a != NULL && b != NULL) {
|
|
|
|
if (strcmp(a->name, b->name) > 0) {
|
|
|
|
t->next = a;
|
|
|
|
a = a->next;
|
|
|
|
} else {
|
|
|
|
t->next = b;
|
|
|
|
b = b->next;
|
|
|
|
}
|
|
|
|
t = t->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only one list is non-empty, so just splice it on. */
|
|
|
|
if (a != NULL)
|
|
|
|
t->next = a;
|
|
|
|
if (b != NULL)
|
|
|
|
t->next = b;
|
|
|
|
|
|
|
|
return (p);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-27 23:27:28 +00:00
|
|
|
extract_file(struct archive *a, struct archive_entry *entry, int flags)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2004-06-27 23:27:28 +00:00
|
|
|
struct extract *extract;
|
2004-06-27 03:19:01 +00:00
|
|
|
const char *name;
|
|
|
|
mode_t mode;
|
2004-06-27 23:27:28 +00:00
|
|
|
int fd, r, r2;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-06-27 23:27:28 +00:00
|
|
|
extract = a->extract;
|
2004-06-27 03:19:01 +00:00
|
|
|
name = archive_entry_pathname(entry);
|
2004-06-27 23:27:28 +00:00
|
|
|
mode = archive_entry_mode(entry) & 0777;
|
2004-02-09 23:22:54 +00:00
|
|
|
r = ARCHIVE_OK;
|
|
|
|
|
2004-04-29 05:19:42 +00:00
|
|
|
/*
|
|
|
|
* If we're not supposed to overwrite pre-existing files,
|
|
|
|
* use O_EXCL. Otherwise, use O_TRUNC.
|
|
|
|
*/
|
|
|
|
if (flags & (ARCHIVE_EXTRACT_UNLINK | ARCHIVE_EXTRACT_NO_OVERWRITE))
|
|
|
|
fd = open(name, O_WRONLY | O_CREAT | O_EXCL, mode);
|
|
|
|
else
|
|
|
|
fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, mode);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* Try removing a pre-existing file. */
|
2004-06-27 03:19:01 +00:00
|
|
|
if (fd < 0 && !(flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
|
2004-02-09 23:22:54 +00:00
|
|
|
unlink(name);
|
|
|
|
fd = open(name, O_WRONLY | O_CREAT | O_EXCL, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Might be a non-existent parent dir; try fixing that. */
|
2004-06-27 03:19:01 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
mkdirpath(a, name);
|
|
|
|
fd = open(name, O_WRONLY | O_CREAT | O_EXCL, mode);
|
|
|
|
}
|
|
|
|
if (fd < 0) {
|
|
|
|
archive_set_error(a, errno, "Can't open '%s'", name);
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
r = archive_read_data_into_fd(a, fd);
|
2004-06-27 23:27:28 +00:00
|
|
|
extract->pst = NULL; /* Cached stat data no longer valid. */
|
|
|
|
r2 = restore_metadata(a, entry, flags);
|
2004-06-27 03:19:01 +00:00
|
|
|
close(fd);
|
2004-06-27 23:27:28 +00:00
|
|
|
return (err_combine(r, r2));
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-27 23:27:28 +00:00
|
|
|
extract_dir(struct archive *a, struct archive_entry *entry, int flags)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2004-06-03 23:29:47 +00:00
|
|
|
struct extract *extract;
|
|
|
|
const struct stat *st;
|
2004-06-27 03:19:01 +00:00
|
|
|
char *p;
|
|
|
|
size_t len;
|
2004-06-28 14:12:09 +00:00
|
|
|
mode_t mode;
|
2004-06-03 23:29:47 +00:00
|
|
|
|
|
|
|
extract = a->extract;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-06-27 03:19:01 +00:00
|
|
|
/* Copy path to mutable storage. */
|
2004-06-27 23:27:28 +00:00
|
|
|
archive_strcpy(&(extract->mkdirpath),
|
|
|
|
archive_entry_pathname(entry));
|
2004-06-27 03:19:01 +00:00
|
|
|
p = extract->mkdirpath.s;
|
|
|
|
len = strlen(p);
|
|
|
|
if (len > 2 && p[len - 1] == '.' && p[len - 2] == '/')
|
|
|
|
p[--len] = '\0'; /* Remove trailing "/." */
|
|
|
|
if (len > 2 && p[len - 1] == '/')
|
|
|
|
p[--len] = '\0'; /* Remove trailing "/" */
|
|
|
|
/* Recursively try to build the path. */
|
2004-06-27 23:27:28 +00:00
|
|
|
st = archive_entry_stat(entry);
|
2004-06-28 14:12:09 +00:00
|
|
|
mode = st->st_mode;
|
|
|
|
/* Obey umask unless ARCHIVE_EXTRACT_PERM for explicit dirs. */
|
|
|
|
if ((flags & ARCHIVE_EXTRACT_PERM) == 0)
|
|
|
|
mode &= ~extract->umask;
|
2004-06-27 23:27:28 +00:00
|
|
|
extract->pst = NULL; /* Invalidate cached stat data. */
|
2004-06-28 14:12:09 +00:00
|
|
|
if (mkdirpath_recursive(a, p, st, mode, flags))
|
2004-06-27 03:19:01 +00:00
|
|
|
return (ARCHIVE_WARN);
|
2004-06-27 23:27:28 +00:00
|
|
|
archive_entry_set_mode(entry, 0700);
|
|
|
|
return (restore_metadata(a, entry, flags));
|
2004-06-27 03:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convenience form.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
mkdirpath(struct archive *a, const char *path)
|
|
|
|
{
|
|
|
|
struct extract *extract;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
extract = a->extract;
|
|
|
|
|
|
|
|
/* Copy path to mutable storage. */
|
|
|
|
archive_strcpy(&(extract->mkdirpath), path);
|
|
|
|
p = extract->mkdirpath.s;
|
|
|
|
p = strrchr(extract->mkdirpath.s, '/');
|
|
|
|
if (p == NULL)
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
/* Recursively try to build the path. */
|
2004-06-27 05:59:15 +00:00
|
|
|
if (mkdirpath_recursive(a, extract->mkdirpath.s,
|
2004-06-28 14:12:09 +00:00
|
|
|
NULL, extract->default_dir_mode, 0))
|
2004-06-27 03:19:01 +00:00
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns 0 if it successfully created necessary directories.
|
|
|
|
* Otherwise, returns ARCHIVE_WARN.
|
|
|
|
*/
|
|
|
|
static int
|
2004-06-27 23:27:28 +00:00
|
|
|
mkdirpath_recursive(struct archive *a, char *path,
|
|
|
|
const struct stat *desired_stat, mode_t mode, int flags)
|
2004-06-27 03:19:01 +00:00
|
|
|
{
|
2004-06-27 23:27:28 +00:00
|
|
|
struct stat st;
|
2004-06-27 03:19:01 +00:00
|
|
|
struct extract *extract;
|
|
|
|
struct fixup_entry *le;
|
|
|
|
char *p;
|
|
|
|
mode_t writable_mode = SECURE_DIR_MODE;
|
|
|
|
int r;
|
2004-06-03 23:29:47 +00:00
|
|
|
|
2004-06-27 03:19:01 +00:00
|
|
|
extract = a->extract;
|
|
|
|
|
|
|
|
if (path[0] == '.' && path[1] == 0)
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
|
|
|
|
if (mode != writable_mode ||
|
2004-06-27 23:27:28 +00:00
|
|
|
(desired_stat != NULL && (flags & ARCHIVE_EXTRACT_TIME))) {
|
2004-06-03 23:29:47 +00:00
|
|
|
/* Add this dir to the fixup list. */
|
|
|
|
le = malloc(sizeof(struct fixup_entry));
|
|
|
|
le->fixup = 0;
|
|
|
|
le->next = extract->fixup_list;
|
|
|
|
extract->fixup_list = le;
|
2004-06-27 03:19:01 +00:00
|
|
|
le->name = strdup(path);
|
2004-06-03 23:29:47 +00:00
|
|
|
|
|
|
|
if (mode != writable_mode) {
|
2004-06-28 14:12:09 +00:00
|
|
|
le->mode = mode;
|
2004-06-03 23:29:47 +00:00
|
|
|
le->fixup |= FIXUP_MODE;
|
2004-06-27 03:19:01 +00:00
|
|
|
mode = writable_mode;
|
2004-06-03 23:29:47 +00:00
|
|
|
}
|
|
|
|
if (flags & ARCHIVE_EXTRACT_TIME) {
|
2004-06-27 23:27:28 +00:00
|
|
|
le->mtime = desired_stat->st_mtime;
|
|
|
|
le->mtime_nanos = ARCHIVE_STAT_MTIME_NANOS(desired_stat);
|
|
|
|
le->atime = desired_stat->st_atime;
|
|
|
|
le->atime_nanos = ARCHIVE_STAT_ATIME_NANOS(desired_stat);
|
2004-06-03 23:29:47 +00:00
|
|
|
le->fixup |= FIXUP_TIMES;
|
|
|
|
}
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-06-27 16:44:51 +00:00
|
|
|
/*
|
|
|
|
* Try to make the longest dir first. Most archives are
|
|
|
|
* written in a reasonable order, so this will almost always
|
|
|
|
* save us from having to inspect the parent dirs.
|
|
|
|
*/
|
2004-06-27 03:19:01 +00:00
|
|
|
if (mkdir(path, mode) == 0)
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ARCHIVE_OK);
|
2004-04-29 05:19:42 +00:00
|
|
|
/*
|
2004-05-27 05:02:35 +00:00
|
|
|
* Do "unlink first" after. The preceding syscall will always
|
|
|
|
* fail if something already exists, so we save a little time
|
|
|
|
* in the common case by not trying to unlink until we know
|
|
|
|
* something is there.
|
2004-04-29 05:19:42 +00:00
|
|
|
*/
|
2004-05-27 05:02:35 +00:00
|
|
|
if ((flags & ARCHIVE_EXTRACT_UNLINK))
|
2004-06-27 03:19:01 +00:00
|
|
|
unlink(path);
|
2004-04-26 23:37:54 +00:00
|
|
|
/*
|
2004-06-27 03:19:01 +00:00
|
|
|
* Yes, this should be stat() and not lstat(). Using lstat()
|
|
|
|
* here loses the ability to extract through symlinks. If
|
|
|
|
* clients don't want to extract through symlinks, they should
|
|
|
|
* specify ARCHIVE_EXTRACT_UNLINK.
|
2004-06-27 23:27:28 +00:00
|
|
|
*
|
|
|
|
* Note that this cannot use the extract->st cache.
|
2004-04-26 23:37:54 +00:00
|
|
|
*/
|
2004-06-27 23:27:28 +00:00
|
|
|
if (stat(path, &st) == 0) {
|
2004-05-27 05:02:35 +00:00
|
|
|
/* Already exists! */
|
2004-06-27 23:27:28 +00:00
|
|
|
if (S_ISDIR(st.st_mode))
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ARCHIVE_OK);
|
2004-05-27 05:02:35 +00:00
|
|
|
if ((flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
|
2004-06-27 03:19:01 +00:00
|
|
|
archive_set_error(a, EEXIST,
|
|
|
|
"Can't create directory '%s'", path);
|
2004-05-27 05:02:35 +00:00
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
/* Not a dir: remove it and create a directory. */
|
2004-06-27 03:19:01 +00:00
|
|
|
if (unlink(path) == 0 &&
|
|
|
|
mkdir(path, mode) == 0)
|
2004-05-27 05:02:35 +00:00
|
|
|
return (ARCHIVE_OK);
|
2004-06-27 03:19:01 +00:00
|
|
|
} else if (errno != ENOENT) {
|
2004-05-27 05:02:35 +00:00
|
|
|
/* Stat failed? */
|
2004-06-27 03:19:01 +00:00
|
|
|
archive_set_error(a, errno, "Can't test directory '%s'", path);
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
|
2004-06-27 03:19:01 +00:00
|
|
|
/* Doesn't exist: try creating parent dir. */
|
|
|
|
p = strrchr(path, '/');
|
|
|
|
if (p != NULL) {
|
|
|
|
*p = '\0'; /* Terminate path name. */
|
2004-06-28 14:12:09 +00:00
|
|
|
/* Note that implicit dirs always obey the umask. */
|
|
|
|
r = mkdirpath_recursive(a, path, NULL,
|
|
|
|
extract->default_dir_mode, 0);
|
2004-06-27 03:19:01 +00:00
|
|
|
*p = '/'; /* Restore the '/' we just overwrote. */
|
|
|
|
if (r != ARCHIVE_OK)
|
|
|
|
return (r);
|
2004-06-27 16:44:51 +00:00
|
|
|
/* Parent exists now; let's create the last component. */
|
|
|
|
p++;
|
|
|
|
/* Of course, "", ".", and ".." are easy. */
|
|
|
|
if (p[0] == '\0')
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
if (p[0] == '.' && p[1] == '\0')
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
if (p[0] == '.' && p[1] == '.' && p[2] == '\0')
|
|
|
|
return (ARCHIVE_OK);
|
2004-06-27 03:19:01 +00:00
|
|
|
if (mkdir(path, mode) == 0)
|
|
|
|
return (ARCHIVE_OK);
|
2004-06-27 16:44:51 +00:00
|
|
|
/*
|
|
|
|
* Without the following check, a/b/../b/c/d fails at
|
|
|
|
* the second visit to 'b', so 'd' can't be created.
|
|
|
|
*/
|
2004-06-27 23:27:28 +00:00
|
|
|
if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
|
2004-06-27 16:44:51 +00:00
|
|
|
return (ARCHIVE_OK);
|
2004-06-27 03:19:01 +00:00
|
|
|
}
|
|
|
|
archive_set_error(a, errno, "Failed to create dir '%s'", path);
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-27 23:27:28 +00:00
|
|
|
extract_hard_link(struct archive *a, struct archive_entry *entry, int flags)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2004-06-27 23:27:28 +00:00
|
|
|
struct extract *extract;
|
2004-04-30 01:31:20 +00:00
|
|
|
int r;
|
|
|
|
const char *pathname;
|
|
|
|
const char *linkname;
|
|
|
|
|
2004-06-27 23:27:28 +00:00
|
|
|
extract = a->extract;
|
2004-04-30 01:31:20 +00:00
|
|
|
pathname = archive_entry_pathname(entry);
|
|
|
|
linkname = archive_entry_hardlink(entry);
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/* Just remove any pre-existing file with this name. */
|
|
|
|
if (!(flags & ARCHIVE_EXTRACT_NO_OVERWRITE))
|
2004-04-30 01:31:20 +00:00
|
|
|
unlink(pathname);
|
|
|
|
|
|
|
|
r = link(linkname, pathname);
|
2004-06-27 23:27:28 +00:00
|
|
|
extract->pst = NULL; /* Invalidate cached stat data. */
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-04-30 01:31:20 +00:00
|
|
|
if (r != 0) {
|
|
|
|
/* Might be a non-existent parent dir; try fixing that. */
|
|
|
|
mkdirpath(a, pathname);
|
|
|
|
r = link(linkname, pathname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r != 0) {
|
|
|
|
/* XXX Better error message here XXX */
|
|
|
|
archive_set_error(a, errno,
|
|
|
|
"Can't restore hardlink to '%s'", linkname);
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set ownership, time, permission information. */
|
2004-06-27 23:27:28 +00:00
|
|
|
r = restore_metadata(a, entry, flags);
|
|
|
|
return (r);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-27 23:27:28 +00:00
|
|
|
extract_symlink(struct archive *a, struct archive_entry *entry, int flags)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2004-06-27 23:27:28 +00:00
|
|
|
struct extract *extract;
|
2004-04-30 01:31:20 +00:00
|
|
|
int r;
|
|
|
|
const char *pathname;
|
|
|
|
const char *linkname;
|
|
|
|
|
2004-06-27 23:27:28 +00:00
|
|
|
extract = a->extract;
|
2004-04-30 01:31:20 +00:00
|
|
|
pathname = archive_entry_pathname(entry);
|
|
|
|
linkname = archive_entry_symlink(entry);
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/* Just remove any pre-existing file with this name. */
|
|
|
|
if (!(flags & ARCHIVE_EXTRACT_NO_OVERWRITE))
|
2004-04-30 01:31:20 +00:00
|
|
|
unlink(pathname);
|
|
|
|
|
|
|
|
r = symlink(linkname, pathname);
|
2004-06-27 23:27:28 +00:00
|
|
|
extract->pst = NULL; /* Invalidate cached stat data. */
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-04-30 01:31:20 +00:00
|
|
|
if (r != 0) {
|
|
|
|
/* Might be a non-existent parent dir; try fixing that. */
|
|
|
|
mkdirpath(a, pathname);
|
|
|
|
r = symlink(linkname, pathname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r != 0) {
|
2004-02-09 23:22:54 +00:00
|
|
|
/* XXX Better error message here XXX */
|
2004-04-30 01:31:20 +00:00
|
|
|
archive_set_error(a, errno,
|
|
|
|
"Can't restore symlink to '%s'", linkname);
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
|
2004-06-27 23:27:28 +00:00
|
|
|
r = restore_metadata(a, entry, flags);
|
|
|
|
return (r);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-27 23:27:28 +00:00
|
|
|
extract_device(struct archive *a, struct archive_entry *entry,
|
2004-02-09 23:22:54 +00:00
|
|
|
int flags, mode_t mode)
|
|
|
|
{
|
2004-06-27 23:27:28 +00:00
|
|
|
struct extract *extract;
|
2004-02-09 23:22:54 +00:00
|
|
|
int r;
|
|
|
|
|
2004-06-27 23:27:28 +00:00
|
|
|
extract = a->extract;
|
2004-02-09 23:22:54 +00:00
|
|
|
/* Just remove any pre-existing file with this name. */
|
|
|
|
if (!(flags & ARCHIVE_EXTRACT_NO_OVERWRITE))
|
|
|
|
unlink(archive_entry_pathname(entry));
|
|
|
|
|
|
|
|
r = mknod(archive_entry_pathname(entry), mode,
|
2004-06-27 18:38:13 +00:00
|
|
|
archive_entry_rdev(entry));
|
2004-06-27 23:27:28 +00:00
|
|
|
extract->pst = NULL; /* Invalidate cached stat data. */
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* Might be a non-existent parent dir; try fixing that. */
|
|
|
|
if (r != 0 && errno == ENOENT) {
|
|
|
|
mkdirpath(a, archive_entry_pathname(entry));
|
|
|
|
r = mknod(archive_entry_pathname(entry), mode,
|
2004-06-27 18:38:13 +00:00
|
|
|
archive_entry_rdev(entry));
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r != 0) {
|
2004-06-27 23:27:28 +00:00
|
|
|
archive_set_error(a, errno, "Can't restore device node");
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
|
2004-06-27 23:27:28 +00:00
|
|
|
r = restore_metadata(a, entry, flags);
|
|
|
|
return (r);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-27 23:27:28 +00:00
|
|
|
extract_char_device(struct archive *a, struct archive_entry *entry, int flags)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
mode_t mode;
|
|
|
|
|
2004-06-27 18:38:13 +00:00
|
|
|
mode = (archive_entry_mode(entry) & ~S_IFMT) | S_IFCHR;
|
2004-06-27 23:27:28 +00:00
|
|
|
return (extract_device(a, entry, flags, mode));
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-27 23:27:28 +00:00
|
|
|
extract_block_device(struct archive *a, struct archive_entry *entry, int flags)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
mode_t mode;
|
|
|
|
|
2004-06-27 18:38:13 +00:00
|
|
|
mode = (archive_entry_mode(entry) & ~S_IFMT) | S_IFBLK;
|
2004-06-27 23:27:28 +00:00
|
|
|
return (extract_device(a, entry, flags, mode));
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-27 23:27:28 +00:00
|
|
|
extract_fifo(struct archive *a, struct archive_entry *entry, int flags)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2004-06-27 23:27:28 +00:00
|
|
|
struct extract *extract;
|
2004-02-09 23:22:54 +00:00
|
|
|
int r;
|
|
|
|
|
2004-06-27 23:27:28 +00:00
|
|
|
extract = a->extract;
|
2004-02-09 23:22:54 +00:00
|
|
|
/* Just remove any pre-existing file with this name. */
|
|
|
|
if (!(flags & ARCHIVE_EXTRACT_NO_OVERWRITE))
|
|
|
|
unlink(archive_entry_pathname(entry));
|
|
|
|
|
|
|
|
r = mkfifo(archive_entry_pathname(entry),
|
2004-06-27 23:27:28 +00:00
|
|
|
archive_entry_mode(entry));
|
|
|
|
extract->pst = NULL; /* Invalidate cached stat data. */
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* Might be a non-existent parent dir; try fixing that. */
|
|
|
|
if (r != 0 && errno == ENOENT) {
|
|
|
|
mkdirpath(a, archive_entry_pathname(entry));
|
|
|
|
r = mkfifo(archive_entry_pathname(entry),
|
2004-06-27 23:27:28 +00:00
|
|
|
archive_entry_mode(entry));
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r != 0) {
|
|
|
|
archive_set_error(a, errno, "Can't restore fifo");
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
|
2004-06-27 23:27:28 +00:00
|
|
|
r = restore_metadata(a, entry, flags);
|
|
|
|
return (r);
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-06-27 23:27:28 +00:00
|
|
|
static int
|
|
|
|
restore_metadata(struct archive *a, struct archive_entry *entry, int flags)
|
|
|
|
{
|
|
|
|
int r, r2;
|
|
|
|
|
|
|
|
r = set_ownership(a, entry, flags);
|
|
|
|
r2 = set_time(a, entry, flags);
|
|
|
|
r = err_combine(r, r2);
|
|
|
|
r2 = set_perm(a, entry, archive_entry_mode(entry), flags);
|
|
|
|
return (err_combine(r, r2));
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_ownership(struct archive *a, struct archive_entry *entry, int flags)
|
|
|
|
{
|
2004-04-05 21:12:29 +00:00
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/* Not changed. */
|
|
|
|
if ((flags & ARCHIVE_EXTRACT_OWNER) == 0)
|
2004-06-27 23:27:28 +00:00
|
|
|
return (ARCHIVE_OK);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-04-05 21:12:29 +00:00
|
|
|
uid = lookup_uid(a, archive_entry_uname(entry),
|
2004-06-27 23:27:28 +00:00
|
|
|
archive_entry_uid(entry));
|
2004-04-05 21:12:29 +00:00
|
|
|
gid = lookup_gid(a, archive_entry_gname(entry),
|
2004-06-27 23:27:28 +00:00
|
|
|
archive_entry_gid(entry));
|
2004-04-05 21:12:29 +00:00
|
|
|
|
2004-06-27 23:27:28 +00:00
|
|
|
/* If we know we can't change it, don't bother trying. */
|
|
|
|
if (a->user_uid != 0 && a->user_uid != uid)
|
|
|
|
return (ARCHIVE_OK);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-04-05 21:12:29 +00:00
|
|
|
if (lchown(archive_entry_pathname(entry), uid, gid)) {
|
2004-02-09 23:22:54 +00:00
|
|
|
archive_set_error(a, errno,
|
2004-04-05 21:12:29 +00:00
|
|
|
"Can't set user=%d/group=%d for %s", uid, gid,
|
2004-02-09 23:22:54 +00:00
|
|
|
archive_entry_pathname(entry));
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_time(struct archive *a, struct archive_entry *entry, int flags)
|
|
|
|
{
|
|
|
|
const struct stat *st;
|
|
|
|
struct timeval times[2];
|
|
|
|
|
|
|
|
(void)a; /* UNUSED */
|
|
|
|
st = archive_entry_stat(entry);
|
|
|
|
|
|
|
|
if ((flags & ARCHIVE_EXTRACT_TIME) == 0)
|
|
|
|
return (ARCHIVE_OK);
|
2004-06-27 23:27:28 +00:00
|
|
|
/* It's a waste of time to mess with dir timestamps here. */
|
|
|
|
if (S_ISDIR(archive_entry_mode(entry)))
|
|
|
|
return (ARCHIVE_OK);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
times[1].tv_sec = st->st_mtime;
|
2004-04-21 05:13:42 +00:00
|
|
|
times[1].tv_usec = ARCHIVE_STAT_MTIME_NANOS(st) / 1000;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
times[0].tv_sec = st->st_atime;
|
2004-04-21 05:13:42 +00:00
|
|
|
times[0].tv_usec = ARCHIVE_STAT_ATIME_NANOS(st) / 1000;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-03-09 19:50:41 +00:00
|
|
|
#ifdef HAVE_LUTIMES
|
2004-02-09 23:22:54 +00:00
|
|
|
if (lutimes(archive_entry_pathname(entry), times) != 0) {
|
2004-03-09 19:50:41 +00:00
|
|
|
#else
|
|
|
|
if ((archive_entry_mode(entry) & S_IFMT) != S_IFLNK &&
|
|
|
|
utimes(archive_entry_pathname(entry), times) != 0) {
|
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
archive_set_error(a, errno, "Can't update time for %s",
|
|
|
|
archive_entry_pathname(entry));
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: POSIX does not provide a portable way to restore ctime.
|
2004-06-03 23:29:47 +00:00
|
|
|
* (Apart from resetting the system clock, which is distasteful.)
|
2004-02-09 23:22:54 +00:00
|
|
|
* So, any restoration of ctime will necessarily be OS-specific.
|
|
|
|
*/
|
|
|
|
|
2004-04-29 05:19:42 +00:00
|
|
|
/* XXX TODO: Can FreeBSD restore ctime? XXX */
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_perm(struct archive *a, struct archive_entry *entry, int mode, int flags)
|
|
|
|
{
|
2004-06-27 16:44:51 +00:00
|
|
|
struct extract *extract;
|
2004-06-27 23:27:28 +00:00
|
|
|
struct fixup_entry *le;
|
2004-02-09 23:22:54 +00:00
|
|
|
const char *name;
|
2004-06-27 23:27:28 +00:00
|
|
|
unsigned long set, clear;
|
|
|
|
int r;
|
|
|
|
int critical_flags;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-06-27 16:44:51 +00:00
|
|
|
extract = a->extract;
|
2004-06-27 23:27:28 +00:00
|
|
|
|
|
|
|
/* Obey umask unless ARCHIVE_EXTRACT_PERM. */
|
|
|
|
if ((flags & ARCHIVE_EXTRACT_PERM) == 0)
|
|
|
|
mode &= ~extract->umask; /* Enforce umask. */
|
2004-02-09 23:22:54 +00:00
|
|
|
name = archive_entry_pathname(entry);
|
2004-06-27 23:27:28 +00:00
|
|
|
|
|
|
|
if (mode & (S_ISUID | S_ISGID)) {
|
|
|
|
if (extract->pst == NULL && stat(name, &extract->st) != 0) {
|
|
|
|
archive_set_error(a, errno, "Can't check ownership");
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
extract->pst = &extract->st;
|
|
|
|
/*
|
|
|
|
* TODO: Use the uid/gid looked up in set_ownership
|
|
|
|
* above rather than the uid/gid stored in the entry.
|
|
|
|
*/
|
|
|
|
if (extract->pst->st_uid != archive_entry_uid(entry))
|
|
|
|
mode &= ~ S_ISUID;
|
|
|
|
if (extract->pst->st_gid != archive_entry_gid(entry))
|
|
|
|
mode &= ~ S_ISGID;
|
|
|
|
}
|
|
|
|
|
2004-06-30 04:54:07 +00:00
|
|
|
/*
|
|
|
|
* Ensure we change permissions on the object we extracted,
|
|
|
|
* and not any incidental symlink that might have gotten in
|
|
|
|
* the way.
|
|
|
|
*/
|
|
|
|
if (!S_ISLNK(archive_entry_mode(entry))) {
|
|
|
|
if (chmod(name, mode) != 0) {
|
|
|
|
archive_set_error(a, errno, "Can't set permissions");
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
} else {
|
2004-03-09 19:50:41 +00:00
|
|
|
#ifdef HAVE_LCHMOD
|
2004-06-30 04:54:07 +00:00
|
|
|
/*
|
|
|
|
* If lchmod() isn't supported, it's no big deal.
|
|
|
|
* Permissions on symlinks are actually ignored on
|
|
|
|
* most platforms.
|
|
|
|
*/
|
|
|
|
if (lchmod(name, mode) != 0) {
|
|
|
|
archive_set_error(a, errno, "Can't set permissions");
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
2004-03-09 19:50:41 +00:00
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2004-05-27 05:02:35 +00:00
|
|
|
|
2004-06-27 23:27:28 +00:00
|
|
|
if (flags & ARCHIVE_EXTRACT_ACL) {
|
|
|
|
r = set_acls(a, entry);
|
|
|
|
if (r != ARCHIVE_OK)
|
|
|
|
return (r);
|
|
|
|
}
|
2004-06-03 23:29:47 +00:00
|
|
|
|
2004-05-27 05:02:35 +00:00
|
|
|
/*
|
|
|
|
* Make 'critical_flags' hold all file flags that can't be
|
|
|
|
* immediately restored. For example, on BSD systems,
|
|
|
|
* SF_IMMUTABLE prevents hardlinks from being created, so
|
|
|
|
* should not be set until after any hardlinks are created. To
|
|
|
|
* preserve some semblance of portability, this uses #ifdef
|
|
|
|
* extensively. Ugly, but it works.
|
|
|
|
*
|
2004-06-27 23:27:28 +00:00
|
|
|
* Yes, Virginia, this does create a security race. It's mitigated
|
|
|
|
* somewhat by the practice of creating dirs 0700 until the extract
|
|
|
|
* is done, but it would be nice if we could do more than that.
|
2004-05-27 05:02:35 +00:00
|
|
|
* People restoring critical file systems should be wary of
|
|
|
|
* other programs that might try to muck with files as they're
|
|
|
|
* being restored.
|
|
|
|
*/
|
|
|
|
/* Hopefully, the compiler will optimize this mess into a constant. */
|
|
|
|
critical_flags = 0;
|
|
|
|
#ifdef SF_IMMUTABLE
|
|
|
|
critical_flags |= SF_IMMUTABLE;
|
|
|
|
#endif
|
|
|
|
#ifdef UF_IMMUTABLE
|
|
|
|
critical_flags |= UF_IMMUTABLE;
|
|
|
|
#endif
|
|
|
|
#ifdef SF_APPEND
|
|
|
|
critical_flags |= SF_APPEND;
|
|
|
|
#endif
|
|
|
|
#ifdef UF_APPEND
|
|
|
|
critical_flags |= UF_APPEND;
|
|
|
|
#endif
|
|
|
|
#ifdef EXT2_APPEND_FL
|
|
|
|
critical_flags |= EXT2_APPEND_FL;
|
|
|
|
#endif
|
|
|
|
#ifdef EXT2_IMMUTABLE_FL
|
|
|
|
critical_flags |= EXT2_IMMUTABLE_FL;
|
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-06-27 23:27:28 +00:00
|
|
|
if (flags & ARCHIVE_EXTRACT_FFLAGS) {
|
|
|
|
archive_entry_fflags(entry, &set, &clear);
|
2004-05-27 05:02:35 +00:00
|
|
|
|
2004-06-27 23:27:28 +00:00
|
|
|
/*
|
|
|
|
* The first test encourages the compiler to eliminate
|
|
|
|
* all of this if it's not necessary.
|
|
|
|
*/
|
|
|
|
if ((critical_flags != 0) && (set & critical_flags)) {
|
|
|
|
le = malloc(sizeof(struct fixup_entry));
|
|
|
|
le->fixup = FIXUP_FFLAGS;
|
|
|
|
le->next = extract->fixup_list;
|
|
|
|
extract->fixup_list = le;
|
|
|
|
le->name = strdup(archive_entry_pathname(entry));
|
|
|
|
le->mode = archive_entry_mode(entry);
|
|
|
|
le->fflags_set = set;
|
|
|
|
} else {
|
|
|
|
r = set_fflags(a, archive_entry_pathname(entry),
|
|
|
|
archive_entry_mode(entry), set, clear);
|
|
|
|
if (r != ARCHIVE_OK)
|
|
|
|
return (r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (ARCHIVE_OK);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-05-27 05:02:35 +00:00
|
|
|
set_fflags(struct archive *a, const char *name, mode_t mode,
|
|
|
|
unsigned long set, unsigned long clear)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2004-06-27 23:27:28 +00:00
|
|
|
struct extract *extract;
|
2004-02-09 23:22:54 +00:00
|
|
|
int ret;
|
2004-04-26 23:37:54 +00:00
|
|
|
#ifdef LINUX
|
|
|
|
int fd;
|
|
|
|
int err;
|
|
|
|
unsigned long newflags, oldflags;
|
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-06-27 23:27:28 +00:00
|
|
|
extract = a->extract;
|
2004-02-09 23:22:54 +00:00
|
|
|
ret = ARCHIVE_OK;
|
2004-04-26 23:37:54 +00:00
|
|
|
if (set == 0 && clear == 0)
|
|
|
|
return (ret);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-03-09 19:50:41 +00:00
|
|
|
#ifdef HAVE_CHFLAGS
|
2004-05-27 05:02:35 +00:00
|
|
|
(void)mode; /* UNUSED */
|
2004-04-26 23:37:54 +00:00
|
|
|
/*
|
|
|
|
* XXX Is the stat here really necessary? Or can I just use
|
|
|
|
* the 'set' flags directly? In particular, I'm not sure
|
|
|
|
* about the correct approach if we're overwriting an existing
|
|
|
|
* file that already has flags on it. XXX
|
|
|
|
*/
|
2004-06-27 23:27:28 +00:00
|
|
|
if (stat(name, &extract->st) == 0) {
|
|
|
|
extract->st.st_flags &= ~clear;
|
|
|
|
extract->st.st_flags |= set;
|
2004-07-04 18:28:56 +00:00
|
|
|
if (chflags(name, extract->st.st_flags) != 0) {
|
2004-02-09 23:22:54 +00:00
|
|
|
archive_set_error(a, errno,
|
|
|
|
"Failed to set file flags");
|
|
|
|
ret = ARCHIVE_WARN;
|
|
|
|
}
|
2004-06-27 23:27:28 +00:00
|
|
|
extract->pst = &extract->st;
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2004-03-09 19:50:41 +00:00
|
|
|
#endif
|
2004-04-26 23:37:54 +00:00
|
|
|
/* Linux has flags too, but no chflags syscall */
|
|
|
|
#ifdef LINUX
|
|
|
|
/*
|
|
|
|
* Linux has no define for the flags that are only settable
|
|
|
|
* by the root user...
|
|
|
|
*/
|
|
|
|
#define SF_MASK (EXT2_IMMUTABLE_FL|EXT2_APPEND_FL)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX As above, this would be way simpler if we didn't have
|
|
|
|
* to read the current flags from disk. XXX
|
|
|
|
*/
|
2004-05-27 05:02:35 +00:00
|
|
|
if ((S_ISREG(mode) || S_ISDIR(mode)) &&
|
2004-04-26 23:37:54 +00:00
|
|
|
((fd = open(name, O_RDONLY|O_NONBLOCK)) >= 0)) {
|
|
|
|
err = 1;
|
|
|
|
if (fd >= 0 && (ioctl(fd, EXT2_IOC_GETFLAGS, &oldflags) >= 0)) {
|
|
|
|
newflags = (oldflags & ~clear) | set;
|
|
|
|
if (ioctl(fd, EXT2_IOC_SETFLAGS, &newflags) >= 0) {
|
|
|
|
err = 0;
|
|
|
|
} else if (errno == EPERM) {
|
|
|
|
if (ioctl(fd, EXT2_IOC_GETFLAGS, &oldflags) >= 0) {
|
|
|
|
newflags &= ~SF_MASK;
|
|
|
|
oldflags &= SF_MASK;
|
|
|
|
newflags |= oldflags;
|
|
|
|
if (ioctl(fd, EXT2_IOC_SETFLAGS, &newflags) >= 0)
|
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
if (err) {
|
|
|
|
archive_set_error(a, errno,
|
|
|
|
"Failed to set file flags");
|
|
|
|
ret = ARCHIVE_WARN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2004-03-09 19:50:41 +00:00
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2004-04-05 21:12:29 +00:00
|
|
|
#ifndef HAVE_POSIX_ACL
|
|
|
|
/* Default empty function body to satisfy mainline code. */
|
|
|
|
static int
|
|
|
|
set_acls(struct archive *a, struct archive_entry *entry)
|
|
|
|
{
|
|
|
|
(void)a;
|
|
|
|
(void)entry;
|
2004-04-12 01:16:16 +00:00
|
|
|
|
2004-04-05 21:12:29 +00:00
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/*
|
|
|
|
* XXX TODO: What about ACL types other than ACCESS and DEFAULT?
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
set_acls(struct archive *a, struct archive_entry *entry)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2004-04-05 21:12:29 +00:00
|
|
|
ret = set_acl(a, entry, ACL_TYPE_ACCESS,
|
2004-04-06 23:16:50 +00:00
|
|
|
ARCHIVE_ENTRY_ACL_TYPE_ACCESS, "access");
|
2004-04-05 21:12:29 +00:00
|
|
|
if (ret != ARCHIVE_OK)
|
|
|
|
return (ret);
|
|
|
|
ret = set_acl(a, entry, ACL_TYPE_DEFAULT,
|
2004-04-06 23:16:50 +00:00
|
|
|
ARCHIVE_ENTRY_ACL_TYPE_DEFAULT, "default");
|
2004-03-09 19:50:41 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2004-04-05 21:12:29 +00:00
|
|
|
|
2004-03-09 19:50:41 +00:00
|
|
|
static int
|
2004-04-05 21:12:29 +00:00
|
|
|
set_acl(struct archive *a, struct archive_entry *entry, acl_type_t acl_type,
|
2004-04-06 23:16:50 +00:00
|
|
|
int ae_requested_type, const char *typename)
|
2004-03-09 19:50:41 +00:00
|
|
|
{
|
|
|
|
acl_t acl;
|
2004-04-05 21:12:29 +00:00
|
|
|
acl_entry_t acl_entry;
|
|
|
|
acl_permset_t acl_permset;
|
2004-03-09 19:50:41 +00:00
|
|
|
int ret;
|
2004-04-05 21:12:29 +00:00
|
|
|
int ae_type, ae_permset, ae_tag, ae_id;
|
|
|
|
uid_t ae_uid;
|
|
|
|
gid_t ae_gid;
|
|
|
|
const char *ae_name;
|
|
|
|
int entries;
|
|
|
|
const char *name;
|
2004-03-09 19:50:41 +00:00
|
|
|
|
|
|
|
ret = ARCHIVE_OK;
|
2004-04-05 21:12:29 +00:00
|
|
|
entries = archive_entry_acl_reset(entry, ae_requested_type);
|
|
|
|
if (entries == 0)
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
acl = acl_init(entries);
|
|
|
|
while (archive_entry_acl_next(entry, ae_requested_type, &ae_type,
|
|
|
|
&ae_permset, &ae_tag, &ae_id, &ae_name) == ARCHIVE_OK) {
|
|
|
|
acl_create_entry(&acl, &acl_entry);
|
|
|
|
|
2004-04-12 01:16:16 +00:00
|
|
|
switch (ae_tag) {
|
|
|
|
case ARCHIVE_ENTRY_ACL_USER:
|
2004-04-05 21:12:29 +00:00
|
|
|
acl_set_tag_type(acl_entry, ACL_USER);
|
|
|
|
ae_uid = lookup_uid(a, ae_name, ae_id);
|
|
|
|
acl_set_qualifier(acl_entry, &ae_uid);
|
2004-04-12 01:16:16 +00:00
|
|
|
break;
|
|
|
|
case ARCHIVE_ENTRY_ACL_GROUP:
|
2004-04-05 21:12:29 +00:00
|
|
|
acl_set_tag_type(acl_entry, ACL_GROUP);
|
|
|
|
ae_gid = lookup_gid(a, ae_name, ae_id);
|
|
|
|
acl_set_qualifier(acl_entry, &ae_gid);
|
2004-04-12 01:16:16 +00:00
|
|
|
break;
|
|
|
|
case ARCHIVE_ENTRY_ACL_USER_OBJ:
|
2004-04-05 21:12:29 +00:00
|
|
|
acl_set_tag_type(acl_entry, ACL_USER_OBJ);
|
2004-04-12 01:16:16 +00:00
|
|
|
break;
|
|
|
|
case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
|
2004-04-05 21:12:29 +00:00
|
|
|
acl_set_tag_type(acl_entry, ACL_GROUP_OBJ);
|
2004-04-12 01:16:16 +00:00
|
|
|
break;
|
|
|
|
case ARCHIVE_ENTRY_ACL_MASK:
|
2004-04-05 21:12:29 +00:00
|
|
|
acl_set_tag_type(acl_entry, ACL_MASK);
|
2004-04-12 01:16:16 +00:00
|
|
|
break;
|
|
|
|
case ARCHIVE_ENTRY_ACL_OTHER:
|
2004-04-05 21:12:29 +00:00
|
|
|
acl_set_tag_type(acl_entry, ACL_OTHER);
|
2004-04-12 01:16:16 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* XXX */
|
|
|
|
break;
|
|
|
|
}
|
2004-04-05 21:12:29 +00:00
|
|
|
|
|
|
|
acl_get_permset(acl_entry, &acl_permset);
|
|
|
|
acl_clear_perms(acl_permset);
|
|
|
|
if (ae_permset & ARCHIVE_ENTRY_ACL_EXECUTE)
|
|
|
|
acl_add_perm(acl_permset, ACL_EXECUTE);
|
|
|
|
if (ae_permset & ARCHIVE_ENTRY_ACL_WRITE)
|
|
|
|
acl_add_perm(acl_permset, ACL_WRITE);
|
|
|
|
if (ae_permset & ARCHIVE_ENTRY_ACL_READ)
|
|
|
|
acl_add_perm(acl_permset, ACL_READ);
|
|
|
|
}
|
2004-03-09 19:50:41 +00:00
|
|
|
|
2004-04-05 21:12:29 +00:00
|
|
|
name = archive_entry_pathname(entry);
|
2004-04-12 01:16:16 +00:00
|
|
|
|
2004-04-05 21:12:29 +00:00
|
|
|
if (acl_set_file(name, acl_type, acl) != 0) {
|
2004-04-06 23:16:50 +00:00
|
|
|
archive_set_error(a, errno, "Failed to set %s acl", typename);
|
2004-03-09 19:50:41 +00:00
|
|
|
ret = ARCHIVE_WARN;
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2004-04-05 21:12:29 +00:00
|
|
|
acl_free(acl);
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
2004-03-09 19:50:41 +00:00
|
|
|
#endif
|
2004-04-05 21:12:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX The following gid/uid lookups can be a performance bottleneck.
|
|
|
|
* Some form of caching would probably be very effective, though
|
|
|
|
* I have concerns about staleness.
|
|
|
|
*/
|
|
|
|
static gid_t
|
|
|
|
lookup_gid(struct archive *a, const char *gname, gid_t gid)
|
|
|
|
{
|
|
|
|
struct group *grent;
|
|
|
|
|
|
|
|
(void)a; /* UNUSED */
|
|
|
|
|
|
|
|
/* Look up gid from gname. */
|
|
|
|
if (gname != NULL && *gname != '\0') {
|
|
|
|
grent = getgrnam(gname);
|
|
|
|
if (grent != NULL)
|
|
|
|
gid = grent->gr_gid;
|
|
|
|
}
|
|
|
|
return (gid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uid_t
|
|
|
|
lookup_uid(struct archive *a, const char *uname, uid_t uid)
|
|
|
|
{
|
|
|
|
struct passwd *pwent;
|
|
|
|
|
|
|
|
(void)a; /* UNUSED */
|
|
|
|
|
|
|
|
/* Look up uid from uname. */
|
|
|
|
if (uname != NULL && *uname != '\0') {
|
|
|
|
pwent = getpwnam(uname);
|
|
|
|
if (pwent != NULL)
|
|
|
|
uid = pwent->pw_uid;
|
|
|
|
}
|
|
|
|
return (uid);
|
|
|
|
}
|
2004-05-13 06:01:14 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
archive_read_extract_set_progress_callback(struct archive *a,
|
|
|
|
void (*progress_func)(void *), void *user_data)
|
|
|
|
{
|
|
|
|
a->extract_progress = progress_func;
|
|
|
|
a->extract_progress_user_data = user_data;
|
|
|
|
}
|