Import libarchive 2.2.3 fixing a few memory leaks and other fixes.
[dragonfly/port-amd64.git] / contrib / libarchive-2 / libarchive / archive_write_disk.c
blobac2bcbde0a038920da0d405ecbe01e5352356c6a
1 /*-
2 * Copyright (c) 2003-2007 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_write_disk.c,v 1.11 2007/05/21 04:22:38 cperciva Exp $");
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33 #ifdef HAVE_SYS_ACL_H
34 #include <sys/acl.h>
35 #endif
36 #ifdef HAVE_ATTR_XATTR_H
37 #include <attr/xattr.h>
38 #endif
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
41 #endif
42 #ifdef HAVE_SYS_STAT_H
43 #include <sys/stat.h>
44 #endif
45 #ifdef HAVE_SYS_TIME_H
46 #include <sys/time.h>
47 #endif
49 #ifdef HAVE_EXT2FS_EXT2_FS_H
50 #include <ext2fs/ext2_fs.h> /* for Linux file flags */
51 #endif
52 #ifdef HAVE_ERRNO_H
53 #include <errno.h>
54 #endif
55 #ifdef HAVE_FCNTL_H
56 #include <fcntl.h>
57 #endif
58 #ifdef HAVE_GRP_H
59 #include <grp.h>
60 #endif
61 #ifdef HAVE_LINUX_FS_H
62 #include <linux/fs.h> /* for Linux file flags */
63 #endif
64 #ifdef HAVE_LINUX_EXT2_FS_H
65 #include <linux/ext2_fs.h> /* for Linux file flags */
66 #endif
67 #ifdef HAVE_LIMITS_H
68 #include <limits.h>
69 #endif
70 #ifdef HAVE_PWD_H
71 #include <pwd.h>
72 #endif
73 #include <stdio.h>
74 #ifdef HAVE_STDLIB_H
75 #include <stdlib.h>
76 #endif
77 #ifdef HAVE_STRING_H
78 #include <string.h>
79 #endif
80 #ifdef HAVE_UNISTD_H
81 #include <unistd.h>
82 #endif
83 #ifdef HAVE_UTIME_H
84 #include <utime.h>
85 #endif
87 #include "archive.h"
88 #include "archive_string.h"
89 #include "archive_entry.h"
90 #include "archive_private.h"
92 struct fixup_entry {
93 struct fixup_entry *next;
94 mode_t mode;
95 int64_t mtime;
96 int64_t atime;
97 unsigned long mtime_nanos;
98 unsigned long atime_nanos;
99 unsigned long fflags_set;
100 int fixup; /* bitmask of what needs fixing */
101 char *name;
105 * We use a bitmask to track which operations remain to be done for
106 * this file. In particular, this helps us avoid unnecessary
107 * operations when it's possible to take care of one step as a
108 * side-effect of another. For example, mkdir() can specify the mode
109 * for the newly-created object but symlink() cannot. This means we
110 * can skip chmod() if mkdir() succeeded, but we must explicitly
111 * chmod() if we're trying to create a directory that already exists
112 * (mkdir() failed) or if we're restoring a symlink. Similarly, we
113 * need to verify UID/GID before trying to restore SUID/SGID bits;
114 * that verification can occur explicitly through a stat() call or
115 * implicitly because of a successful chown() call.
117 #define TODO_MODE_FORCE 0x40000000
118 #define TODO_MODE_BASE 0x20000000
119 #define TODO_SUID 0x10000000
120 #define TODO_SUID_CHECK 0x08000000
121 #define TODO_SGID 0x04000000
122 #define TODO_SGID_CHECK 0x02000000
123 #define TODO_MODE (TODO_MODE_BASE|TODO_SUID|TODO_SGID)
124 #define TODO_TIMES ARCHIVE_EXTRACT_TIME
125 #define TODO_OWNER ARCHIVE_EXTRACT_OWNER
126 #define TODO_FFLAGS ARCHIVE_EXTRACT_FFLAGS
127 #define TODO_ACLS ARCHIVE_EXTRACT_ACL
128 #define TODO_XATTR ARCHIVE_EXTRACT_XATTR
130 struct archive_write_disk {
131 struct archive archive;
133 mode_t user_umask;
134 struct fixup_entry *fixup_list;
135 struct fixup_entry *current_fixup;
136 uid_t user_uid;
137 dev_t skip_file_dev;
138 ino_t skip_file_ino;
140 gid_t (*lookup_gid)(void *private, const char *gname, gid_t gid);
141 void (*cleanup_gid)(void *private);
142 void *lookup_gid_data;
143 uid_t (*lookup_uid)(void *private, const char *gname, gid_t gid);
144 void (*cleanup_uid)(void *private);
145 void *lookup_uid_data;
148 * Full path of last file to satisfy symlink checks.
150 struct archive_string path_safe;
153 * Cached stat data from disk for the current entry.
154 * If this is valid, pst points to st. Otherwise,
155 * pst is null.
157 struct stat st;
158 struct stat *pst;
160 /* Information about the object being restored right now. */
161 struct archive_entry *entry; /* Entry being extracted. */
162 char *name; /* Name of entry, possibly edited. */
163 struct archive_string _name_data; /* backing store for 'name' */
164 /* Tasks remaining for this object. */
165 int todo;
166 /* Tasks deferred until end-of-archive. */
167 int deferred;
168 /* Options requested by the client. */
169 int flags;
170 /* Handle for the file we're restoring. */
171 int fd;
172 /* Current offset for writing data to the file. */
173 off_t offset;
174 /* Dir we were in before this restore; only for deep paths. */
175 int restore_pwd;
176 /* Mode we should use for this entry; affected by _PERM and umask. */
177 mode_t mode;
178 /* UID/GID to use in restoring this entry. */
179 uid_t uid;
180 gid_t gid;
184 * Default mode for dirs created automatically (will be modified by umask).
185 * Note that POSIX specifies 0777 for implicity-created dirs, "modified
186 * by the process' file creation mask."
188 #define DEFAULT_DIR_MODE 0777
190 * Dir modes are restored in two steps: During the extraction, the permissions
191 * in the archive are modified to match the following limits. During
192 * the post-extract fixup pass, the permissions from the archive are
193 * applied.
195 #define MINIMUM_DIR_MODE 0700
196 #define MAXIMUM_DIR_MODE 0775
198 static int check_symlinks(struct archive_write_disk *);
199 static int create_filesystem_object(struct archive_write_disk *);
200 static struct fixup_entry *current_fixup(struct archive_write_disk *, const char *pathname);
201 #ifdef HAVE_FCHDIR
202 static void edit_deep_directories(struct archive_write_disk *ad);
203 #endif
204 static int cleanup_pathname(struct archive_write_disk *);
205 static int create_dir(struct archive_write_disk *, char *);
206 static int create_parent_dir(struct archive_write_disk *, char *);
207 static int older(struct stat *, struct archive_entry *);
208 static int restore_entry(struct archive_write_disk *);
209 #ifdef HAVE_POSIX_ACL
210 static int set_acl(struct archive_write_disk *, int fd, struct archive_entry *,
211 acl_type_t, int archive_entry_acl_type, const char *tn);
212 #endif
213 static int set_acls(struct archive_write_disk *);
214 static int set_xattrs(struct archive_write_disk *);
215 static int set_fflags(struct archive_write_disk *);
216 static int set_fflags_platform(struct archive_write_disk *, int fd,
217 const char *name, mode_t mode,
218 unsigned long fflags_set, unsigned long fflags_clear);
219 static int set_ownership(struct archive_write_disk *);
220 static int set_mode(struct archive_write_disk *, int mode);
221 static int set_time(struct archive_write_disk *);
222 static struct fixup_entry *sort_dir_list(struct fixup_entry *p);
223 static gid_t trivial_lookup_gid(void *, const char *, gid_t);
224 static uid_t trivial_lookup_uid(void *, const char *, uid_t);
227 static struct archive_vtable *archive_write_disk_vtable(void);
229 static int _archive_write_close(struct archive *);
230 static int _archive_write_finish(struct archive *);
231 static int _archive_write_header(struct archive *, struct archive_entry *);
232 static int _archive_write_finish_entry(struct archive *);
233 static ssize_t _archive_write_data(struct archive *, const void *, size_t);
234 static ssize_t _archive_write_data_block(struct archive *, const void *, size_t, off_t);
236 static struct archive_vtable *
237 archive_write_disk_vtable(void)
239 static struct archive_vtable av;
240 static int inited = 0;
242 if (!inited) {
243 av.archive_write_close = _archive_write_close;
244 av.archive_write_finish = _archive_write_finish;
245 av.archive_write_header = _archive_write_header;
246 av.archive_write_finish_entry = _archive_write_finish_entry;
247 av.archive_write_data = _archive_write_data;
248 av.archive_write_data_block = _archive_write_data_block;
250 return (&av);
255 archive_write_disk_set_options(struct archive *_a, int flags)
257 struct archive_write_disk *a = (struct archive_write_disk *)_a;
259 a->flags = flags;
260 return (ARCHIVE_OK);
265 * Extract this entry to disk.
267 * TODO: Validate hardlinks. According to the standards, we're
268 * supposed to check each extracted hardlink and squawk if it refers
269 * to a file that we didn't restore. I'm not entirely convinced this
270 * is a good idea, but more importantly: Is there any way to validate
271 * hardlinks without keeping a complete list of filenames from the
272 * entire archive?? Ugh.
275 static int
276 _archive_write_header(struct archive *_a, struct archive_entry *entry)
278 struct archive_write_disk *a = (struct archive_write_disk *)_a;
279 struct fixup_entry *fe;
280 int ret, r;
282 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
283 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
284 "archive_write_disk_header");
285 archive_clear_error(&a->archive);
286 if (a->archive.state & ARCHIVE_STATE_DATA) {
287 r = _archive_write_finish_entry(&a->archive);
288 if (r != ARCHIVE_OK)
289 return (r);
292 /* Set up for this particular entry. */
293 a->pst = NULL;
294 a->current_fixup = NULL;
295 a->deferred = 0;
296 if (a->entry) {
297 archive_entry_free(a->entry);
298 a->entry = NULL;
300 a->entry = archive_entry_clone(entry);
301 a->fd = -1;
302 a->offset = 0;
303 a->uid = a->user_uid;
304 a->mode = archive_entry_mode(a->entry);
305 archive_strcpy(&(a->_name_data), archive_entry_pathname(a->entry));
306 a->name = a->_name_data.s;
307 archive_clear_error(&a->archive);
310 * Clean up the requested path. This is necessary for correct
311 * dir restores; the dir restore logic otherwise gets messed
312 * up by nonsense like "dir/.".
314 ret = cleanup_pathname(a);
315 if (ret != ARCHIVE_OK)
316 return (ret);
319 * Set the umask to zero so we get predictable mode settings.
320 * This gets done on every call to _write_header in case the
321 * user edits their umask during the extraction for some
322 * reason. This will be reset before we return. Note that we
323 * don't need to do this in _finish_entry, as the chmod(), etc,
324 * system calls don't obey umask.
326 a->user_umask = umask(0);
327 /* From here on, early exit requires "goto done" to clean up. */
329 /* Figure out what we need to do for this entry. */
330 a->todo = TODO_MODE_BASE;
331 if (a->flags & ARCHIVE_EXTRACT_PERM) {
332 a->todo |= TODO_MODE_FORCE; /* Be pushy about permissions. */
334 * SGID requires an extra "check" step because we
335 * cannot easily predict the GID that the system will
336 * assign. (Different systems assign GIDs to files
337 * based on a variety of criteria, including process
338 * credentials and the gid of the enclosing
339 * directory.) We can only restore the SGID bit if
340 * the file has the right GID, and we only know the
341 * GID if we either set it (see set_ownership) or if
342 * we've actually called stat() on the file after it
343 * was restored. Since there are several places at
344 * which we might verify the GID, we need a TODO bit
345 * to keep track.
347 if (a->mode & S_ISGID)
348 a->todo |= TODO_SGID | TODO_SGID_CHECK;
350 * Verifying the SUID is simpler, but can still be
351 * done in multiple ways, hence the separate "check" bit.
353 if (a->mode & S_ISUID)
354 a->todo |= TODO_SUID | TODO_SUID_CHECK;
355 } else {
357 * User didn't request full permissions, so don't
358 * restore SUID, SGID bits and obey umask.
360 a->mode &= ~S_ISUID;
361 a->mode &= ~S_ISGID;
362 a->mode &= ~S_ISVTX;
363 a->mode &= ~a->user_umask;
365 if (a->flags & ARCHIVE_EXTRACT_OWNER)
366 a->todo |= TODO_OWNER;
367 if (a->flags & ARCHIVE_EXTRACT_TIME)
368 a->todo |= TODO_TIMES;
369 if (a->flags & ARCHIVE_EXTRACT_ACL)
370 a->todo |= TODO_ACLS;
371 if (a->flags & ARCHIVE_EXTRACT_FFLAGS)
372 a->todo |= TODO_FFLAGS;
373 if (a->flags & ARCHIVE_EXTRACT_SECURE_SYMLINKS) {
374 ret = check_symlinks(a);
375 if (ret != ARCHIVE_OK)
376 goto done;
378 #ifdef HAVE_FCHDIR
379 /* If path exceeds PATH_MAX, shorten the path. */
380 edit_deep_directories(a);
381 #endif
383 ret = restore_entry(a);
385 #ifdef HAVE_FCHDIR
386 /* If we changed directory above, restore it here. */
387 if (a->restore_pwd >= 0) {
388 fchdir(a->restore_pwd);
389 close(a->restore_pwd);
390 a->restore_pwd = -1;
392 #endif
395 * Fixup uses the unedited pathname from archive_entry_pathname(),
396 * because it is relative to the base dir and the edited path
397 * might be relative to some intermediate dir as a result of the
398 * deep restore logic.
400 if (a->deferred & TODO_MODE) {
401 fe = current_fixup(a, archive_entry_pathname(entry));
402 fe->fixup |= TODO_MODE_BASE;
403 fe->mode = a->mode;
406 if (a->deferred & TODO_TIMES) {
407 fe = current_fixup(a, archive_entry_pathname(entry));
408 fe->fixup |= TODO_TIMES;
409 fe->mtime = archive_entry_mtime(entry);
410 fe->mtime_nanos = archive_entry_mtime_nsec(entry);
411 fe->atime = archive_entry_atime(entry);
412 fe->atime_nanos = archive_entry_atime_nsec(entry);
415 if (a->deferred & TODO_FFLAGS) {
416 fe = current_fixup(a, archive_entry_pathname(entry));
417 fe->fixup |= TODO_FFLAGS;
418 /* TODO: Complete this.. defer fflags from below. */
421 /* We've created the object and are ready to pour data into it. */
422 if (ret == ARCHIVE_OK)
423 a->archive.state = ARCHIVE_STATE_DATA;
424 done:
425 /* Restore the user's umask before returning. */
426 umask(a->user_umask);
428 return (ret);
432 archive_write_disk_set_skip_file(struct archive *_a, dev_t d, ino_t i)
434 struct archive_write_disk *a = (struct archive_write_disk *)_a;
435 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
436 ARCHIVE_STATE_ANY, "archive_write_disk_set_skip_file");
437 a->skip_file_dev = d;
438 a->skip_file_ino = i;
439 return (ARCHIVE_OK);
442 static ssize_t
443 _archive_write_data_block(struct archive *_a,
444 const void *buff, size_t size, off_t offset)
446 struct archive_write_disk *a = (struct archive_write_disk *)_a;
447 ssize_t bytes_written = 0;
449 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
450 ARCHIVE_STATE_DATA, "archive_write_disk_block");
451 if (a->fd < 0)
452 return (ARCHIVE_OK);
453 archive_clear_error(&a->archive);
455 /* Seek if necessary to the specified offset. */
456 if (offset != a->offset) {
457 if (lseek(a->fd, offset, SEEK_SET) < 0) {
458 archive_set_error(&a->archive, errno, "Seek failed");
459 return (ARCHIVE_WARN);
461 a->offset = offset;
464 /* Write the data. */
465 while (size > 0) {
466 bytes_written = write(a->fd, buff, size);
467 if (bytes_written < 0) {
468 archive_set_error(&a->archive, errno, "Write failed");
469 return (ARCHIVE_WARN);
471 size -= bytes_written;
472 a->offset += bytes_written;
474 return (ARCHIVE_OK);
477 static ssize_t
478 _archive_write_data(struct archive *_a, const void *buff, size_t size)
480 struct archive_write_disk *a = (struct archive_write_disk *)_a;
481 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
482 ARCHIVE_STATE_DATA, "archive_write_data");
483 if (a->fd < 0)
484 return (ARCHIVE_OK);
486 return (_archive_write_data_block(_a, buff, size, a->offset));
489 static int
490 _archive_write_finish_entry(struct archive *_a)
492 struct archive_write_disk *a = (struct archive_write_disk *)_a;
493 int ret = ARCHIVE_OK;
495 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
496 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
497 "archive_write_finish_entry");
498 if (a->archive.state & ARCHIVE_STATE_HEADER)
499 return (ARCHIVE_OK);
500 archive_clear_error(&a->archive);
502 /* Restore metadata. */
505 * Look up the "real" UID only if we're going to need it. We
506 * need this for TODO_SGID because chown() requires both.
508 if (a->todo & (TODO_OWNER | TODO_SUID | TODO_SGID)) {
509 a->uid = a->lookup_uid(a->lookup_uid_data,
510 archive_entry_uname(a->entry),
511 archive_entry_uid(a->entry));
513 /* Look up the "real" GID only if we're going to need it. */
514 if (a->todo & (TODO_OWNER | TODO_SGID | TODO_SUID)) {
515 a->gid = a->lookup_gid(a->lookup_gid_data,
516 archive_entry_gname(a->entry),
517 archive_entry_gid(a->entry));
520 * If restoring ownership, do it before trying to restore suid/sgid
521 * bits. If we set the owner, we know what it is and can skip
522 * a stat() call to examine the ownership of the file on disk.
524 if (a->todo & TODO_OWNER)
525 ret = set_ownership(a);
526 if (a->todo & TODO_MODE) {
527 int r2 = set_mode(a, a->mode);
528 if (r2 < ret) ret = r2;
530 if (a->todo & TODO_TIMES) {
531 int r2 = set_time(a);
532 if (r2 < ret) ret = r2;
534 if (a->todo & TODO_ACLS) {
535 int r2 = set_acls(a);
536 if (r2 < ret) ret = r2;
538 if (a->todo & TODO_XATTR) {
539 int r2 = set_xattrs(a);
540 if (r2 < ret) ret = r2;
542 if (a->todo & TODO_FFLAGS) {
543 int r2 = set_fflags(a);
544 if (r2 < ret) ret = r2;
547 /* If there's an fd, we can close it now. */
548 if (a->fd >= 0) {
549 close(a->fd);
550 a->fd = -1;
552 /* If there's an entry, we can release it now. */
553 if (a->entry) {
554 archive_entry_free(a->entry);
555 a->entry = NULL;
557 a->archive.state = ARCHIVE_STATE_HEADER;
558 return (ret);
562 archive_write_disk_set_group_lookup(struct archive *_a,
563 void *private_data,
564 gid_t (*lookup_gid)(void *private, const char *gname, gid_t gid),
565 void (*cleanup_gid)(void *private))
567 struct archive_write_disk *a = (struct archive_write_disk *)_a;
568 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
569 ARCHIVE_STATE_ANY, "archive_write_disk_set_group_lookup");
571 a->lookup_gid = lookup_gid;
572 a->cleanup_gid = cleanup_gid;
573 a->lookup_gid_data = private_data;
574 return (ARCHIVE_OK);
578 archive_write_disk_set_user_lookup(struct archive *_a,
579 void *private_data,
580 uid_t (*lookup_uid)(void *private, const char *uname, uid_t uid),
581 void (*cleanup_uid)(void *private))
583 struct archive_write_disk *a = (struct archive_write_disk *)_a;
584 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
585 ARCHIVE_STATE_ANY, "archive_write_disk_set_user_lookup");
587 a->lookup_uid = lookup_uid;
588 a->cleanup_uid = cleanup_uid;
589 a->lookup_uid_data = private_data;
590 return (ARCHIVE_OK);
595 * Create a new archive_write_disk object and initialize it with global state.
597 struct archive *
598 archive_write_disk_new(void)
600 struct archive_write_disk *a;
602 a = (struct archive_write_disk *)malloc(sizeof(*a));
603 if (a == NULL)
604 return (NULL);
605 memset(a, 0, sizeof(*a));
606 a->archive.magic = ARCHIVE_WRITE_DISK_MAGIC;
607 /* We're ready to write a header immediately. */
608 a->archive.state = ARCHIVE_STATE_HEADER;
609 a->archive.vtable = archive_write_disk_vtable();
610 a->lookup_uid = trivial_lookup_uid;
611 a->lookup_gid = trivial_lookup_gid;
612 a->user_uid = geteuid();
613 archive_string_ensure(&a->path_safe, 64);
614 return (&a->archive);
619 * If pathname is longer than PATH_MAX, chdir to a suitable
620 * intermediate dir and edit the path down to a shorter suffix. Note
621 * that this routine never returns an error; if the chdir() attempt
622 * fails for any reason, we just go ahead with the long pathname. The
623 * object creation is likely to fail, but any error will get handled
624 * at that time.
626 #ifdef HAVE_FCHDIR
627 static void
628 edit_deep_directories(struct archive_write_disk *a)
630 int ret;
631 char *tail = a->name;
633 a->restore_pwd = -1;
635 /* If path is short, avoid the open() below. */
636 if (strlen(tail) <= PATH_MAX)
637 return;
639 /* Try to record our starting dir. */
640 a->restore_pwd = open(".", O_RDONLY);
641 if (a->restore_pwd < 0)
642 return;
644 /* As long as the path is too long... */
645 while (strlen(tail) > PATH_MAX) {
646 /* Locate a dir prefix shorter than PATH_MAX. */
647 tail += PATH_MAX - 8;
648 while (tail > a->name && *tail != '/')
649 tail--;
650 /* Exit if we find a too-long path component. */
651 if (tail <= a->name)
652 return;
653 /* Create the intermediate dir and chdir to it. */
654 *tail = '\0'; /* Terminate dir portion */
655 ret = create_dir(a, a->name);
656 if (ret == ARCHIVE_OK && chdir(a->name) != 0)
657 ret = ARCHIVE_WARN;
658 *tail = '/'; /* Restore the / we removed. */
659 if (ret != ARCHIVE_OK)
660 return;
661 tail++;
662 /* The chdir() succeeded; we've now shortened the path. */
663 a->name = tail;
665 return;
667 #endif
670 * The main restore function.
672 static int
673 restore_entry(struct archive_write_disk *a)
675 int ret = ARCHIVE_OK, en;
677 if (a->flags & ARCHIVE_EXTRACT_UNLINK && !S_ISDIR(a->mode)) {
678 if (unlink(a->name) == 0) {
679 /* We removed it, we're done. */
680 } else if (errno == ENOENT) {
681 /* File didn't exist, that's just as good. */
682 } else if (rmdir(a->name) == 0) {
683 /* It was a dir, but now it's gone. */
684 } else {
685 /* We tried, but couldn't get rid of it. */
686 archive_set_error(&a->archive, errno,
687 "Could not unlink");
688 return(ARCHIVE_WARN);
692 /* Try creating it first; if this fails, we'll try to recover. */
693 en = create_filesystem_object(a);
695 if ((en == ENOTDIR || en == ENOENT)
696 && !(a->flags & ARCHIVE_EXTRACT_NO_AUTODIR)) {
697 /* If the parent dir doesn't exist, try creating it. */
698 create_parent_dir(a, a->name);
699 /* Now try to create the object again. */
700 en = create_filesystem_object(a);
703 if ((en == EISDIR || en == EEXIST)
704 && (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
705 /* If we're not overwriting, we're done. */
706 archive_set_error(&a->archive, en, "Already exists");
707 return (ARCHIVE_WARN);
711 * Some platforms return EISDIR if you call
712 * open(O_WRONLY | O_EXCL | O_CREAT) on a directory, some
713 * return EEXIST. POSIX is ambiguous, requiring EISDIR
714 * for open(O_WRONLY) on a dir and EEXIST for open(O_EXCL | O_CREAT)
715 * on an existing item.
717 if (en == EISDIR) {
718 /* A dir is in the way of a non-dir, rmdir it. */
719 if (rmdir(a->name) != 0) {
720 archive_set_error(&a->archive, errno,
721 "Can't remove already-existing dir");
722 return (ARCHIVE_WARN);
724 /* Try again. */
725 en = create_filesystem_object(a);
726 } else if (en == EEXIST) {
728 * We know something is in the way, but we don't know what;
729 * we need to find out before we go any further.
731 if (lstat(a->name, &a->st) != 0) {
732 archive_set_error(&a->archive, errno,
733 "Can't stat existing object");
734 return (ARCHIVE_WARN);
737 /* TODO: if it's a symlink... */
739 if (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER) {
740 if (!older(&(a->st), a->entry)) {
741 archive_set_error(&a->archive, 0,
742 "File on disk is not older; skipping.");
743 return (ARCHIVE_FAILED);
747 /* If it's our archive, we're done. */
748 if (a->skip_file_dev > 0 &&
749 a->skip_file_ino > 0 &&
750 a->st.st_dev == a->skip_file_dev &&
751 a->st.st_ino == a->skip_file_ino) {
752 archive_set_error(&a->archive, 0, "Refusing to overwrite archive");
753 return (ARCHIVE_FAILED);
756 if (!S_ISDIR(a->st.st_mode)) {
757 /* A non-dir is in the way, unlink it. */
758 if (unlink(a->name) != 0) {
759 archive_set_error(&a->archive, errno,
760 "Can't unlink already-existing object");
761 return (ARCHIVE_WARN);
763 /* Try again. */
764 en = create_filesystem_object(a);
765 } else if (!S_ISDIR(a->mode)) {
766 /* A dir is in the way of a non-dir, rmdir it. */
767 if (rmdir(a->name) != 0) {
768 archive_set_error(&a->archive, errno,
769 "Can't remove already-existing dir");
770 return (ARCHIVE_WARN);
772 /* Try again. */
773 en = create_filesystem_object(a);
774 } else {
776 * There's a dir in the way of a dir. Don't
777 * waste time with rmdir()/mkdir(), just fix
778 * up the permissions on the existing dir.
779 * Note that we don't change perms on existing
780 * dirs unless _EXTRACT_PERM is specified.
782 if ((a->mode != a->st.st_mode)
783 && (a->todo & TODO_MODE_FORCE))
784 a->deferred |= (a->todo & TODO_MODE);
785 /* Ownership doesn't need deferred fixup. */
786 en = 0; /* Forget the EEXIST. */
790 if (en) {
791 /* Everything failed; give up here. */
792 archive_set_error(&a->archive, en, "Can't create '%s'", a->name);
793 return (ARCHIVE_WARN);
796 a->pst = NULL; /* Cached stat data no longer valid. */
797 return (ret);
801 * Returns 0 if creation succeeds, or else returns errno value from
802 * the failed system call. Note: This function should only ever perform
803 * a single system call.
806 create_filesystem_object(struct archive_write_disk *a)
808 /* Create the entry. */
809 const char *linkname;
810 mode_t final_mode, mode;
811 int r;
813 /* We identify hard/symlinks according to the link names. */
814 /* Since link(2) and symlink(2) don't handle modes, we're done here. */
815 linkname = archive_entry_hardlink(a->entry);
816 if (linkname != NULL)
817 return link(linkname, a->name) ? errno : 0;
818 linkname = archive_entry_symlink(a->entry);
819 if (linkname != NULL)
820 return symlink(linkname, a->name) ? errno : 0;
823 * The remaining system calls all set permissions, so let's
824 * try to take advantage of that to avoid an extra chmod()
825 * call. (Recall that umask is set to zero right now!)
828 /* Mode we want for the final restored object (w/o file type bits). */
829 final_mode = a->mode & 07777;
831 * The mode that will actually be restored in this step. Note
832 * that SUID, SGID, etc, require additional work to ensure
833 * security, so we never restore them at this point.
835 mode = final_mode & 0777;
837 switch (a->mode & S_IFMT) {
838 default:
839 /* POSIX requires that we fall through here. */
840 /* FALLTHROUGH */
841 case S_IFREG:
842 a->fd = open(a->name,
843 O_WRONLY | O_CREAT | O_EXCL, mode);
844 r = (a->fd < 0);
845 break;
846 case S_IFCHR:
847 r = mknod(a->name, mode | S_IFCHR,
848 archive_entry_rdev(a->entry));
849 break;
850 case S_IFBLK:
851 r = mknod(a->name, mode | S_IFBLK,
852 archive_entry_rdev(a->entry));
853 break;
854 case S_IFDIR:
855 mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE;
856 r = mkdir(a->name, mode);
857 if (r == 0) {
858 /* Defer setting dir times. */
859 a->deferred |= (a->todo & TODO_TIMES);
860 a->todo &= ~TODO_TIMES;
861 /* Never use an immediate chmod(). */
862 if (mode != final_mode)
863 a->deferred |= (a->todo & TODO_MODE);
864 a->todo &= ~TODO_MODE;
866 break;
867 case S_IFIFO:
868 r = mkfifo(a->name, mode);
869 break;
872 /* All the system calls above set errno on failure. */
873 if (r)
874 return (errno);
876 /* If we managed to set the final mode, we've avoided a chmod(). */
877 if (mode == final_mode)
878 a->todo &= ~TODO_MODE;
879 return (0);
883 * Cleanup function for archive_extract. Mostly, this involves processing
884 * the fixup list, which is used to address a number of problems:
885 * * Dir permissions might prevent us from restoring a file in that
886 * dir, so we restore the dir with minimum 0700 permissions first,
887 * then correct the mode at the end.
888 * * Similarly, the act of restoring a file touches the directory
889 * and changes the timestamp on the dir, so we have to touch-up dir
890 * timestamps at the end as well.
891 * * Some file flags can interfere with the restore by, for example,
892 * preventing the creation of hardlinks to those files.
894 * Note that tar/cpio do not require that archives be in a particular
895 * order; there is no way to know when the last file has been restored
896 * within a directory, so there's no way to optimize the memory usage
897 * here by fixing up the directory any earlier than the
898 * end-of-archive.
900 * XXX TODO: Directory ACLs should be restored here, for the same
901 * reason we set directory perms here. XXX
903 static int
904 _archive_write_close(struct archive *_a)
906 struct archive_write_disk *a = (struct archive_write_disk *)_a;
907 struct fixup_entry *next, *p;
908 int ret;
910 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
911 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
912 "archive_write_disk_close");
913 ret = _archive_write_finish_entry(&a->archive);
915 /* Sort dir list so directories are fixed up in depth-first order. */
916 p = sort_dir_list(a->fixup_list);
918 while (p != NULL) {
919 a->pst = NULL; /* Mark stat cache as out-of-date. */
920 if (p->fixup & TODO_TIMES) {
921 #ifdef HAVE_UTIMES
922 /* {f,l,}utimes() are preferred, when available. */
923 struct timeval times[2];
924 times[1].tv_sec = p->mtime;
925 times[1].tv_usec = p->mtime_nanos / 1000;
926 times[0].tv_sec = p->atime;
927 times[0].tv_usec = p->atime_nanos / 1000;
928 #ifdef HAVE_LUTIMES
929 lutimes(p->name, times);
930 #else
931 utimes(p->name, times);
932 #endif
933 #else
934 /* utime() is more portable, but less precise. */
935 struct utimbuf times;
936 times.modtime = p->mtime;
937 times.actime = p->atime;
939 utime(p->name, &times);
940 #endif
942 if (p->fixup & TODO_MODE_BASE)
943 chmod(p->name, p->mode);
945 if (p->fixup & TODO_FFLAGS)
946 set_fflags_platform(a, -1, p->name,
947 p->mode, p->fflags_set, 0);
949 next = p->next;
950 free(p->name);
951 free(p);
952 p = next;
954 a->fixup_list = NULL;
955 return (ret);
958 static int
959 _archive_write_finish(struct archive *_a)
961 struct archive_write_disk *a = (struct archive_write_disk *)_a;
962 int ret;
963 ret = _archive_write_close(&a->archive);
964 if (a->cleanup_gid != NULL && a->lookup_gid_data != NULL)
965 (a->cleanup_gid)(a->lookup_gid_data);
966 if (a->cleanup_uid != NULL && a->lookup_uid_data != NULL)
967 (a->cleanup_uid)(a->lookup_uid_data);
968 archive_string_free(&a->_name_data);
969 archive_string_free(&a->archive.error_string);
970 archive_string_free(&a->path_safe);
971 free(a);
972 return (ret);
976 * Simple O(n log n) merge sort to order the fixup list. In
977 * particular, we want to restore dir timestamps depth-first.
979 static struct fixup_entry *
980 sort_dir_list(struct fixup_entry *p)
982 struct fixup_entry *a, *b, *t;
984 if (p == NULL)
985 return (NULL);
986 /* A one-item list is already sorted. */
987 if (p->next == NULL)
988 return (p);
990 /* Step 1: split the list. */
991 t = p;
992 a = p->next->next;
993 while (a != NULL) {
994 /* Step a twice, t once. */
995 a = a->next;
996 if (a != NULL)
997 a = a->next;
998 t = t->next;
1000 /* Now, t is at the mid-point, so break the list here. */
1001 b = t->next;
1002 t->next = NULL;
1003 a = p;
1005 /* Step 2: Recursively sort the two sub-lists. */
1006 a = sort_dir_list(a);
1007 b = sort_dir_list(b);
1009 /* Step 3: Merge the returned lists. */
1010 /* Pick the first element for the merged list. */
1011 if (strcmp(a->name, b->name) > 0) {
1012 t = p = a;
1013 a = a->next;
1014 } else {
1015 t = p = b;
1016 b = b->next;
1019 /* Always put the later element on the list first. */
1020 while (a != NULL && b != NULL) {
1021 if (strcmp(a->name, b->name) > 0) {
1022 t->next = a;
1023 a = a->next;
1024 } else {
1025 t->next = b;
1026 b = b->next;
1028 t = t->next;
1031 /* Only one list is non-empty, so just splice it on. */
1032 if (a != NULL)
1033 t->next = a;
1034 if (b != NULL)
1035 t->next = b;
1037 return (p);
1041 * Returns a new, initialized fixup entry.
1043 * TODO: Reduce the memory requirements for this list by using a tree
1044 * structure rather than a simple list of names.
1046 static struct fixup_entry *
1047 new_fixup(struct archive_write_disk *a, const char *pathname)
1049 struct fixup_entry *fe;
1051 fe = (struct fixup_entry *)malloc(sizeof(struct fixup_entry));
1052 if (fe == NULL)
1053 return (NULL);
1054 fe->next = a->fixup_list;
1055 a->fixup_list = fe;
1056 fe->fixup = 0;
1057 fe->name = strdup(pathname);
1058 return (fe);
1062 * Returns a fixup structure for the current entry.
1064 static struct fixup_entry *
1065 current_fixup(struct archive_write_disk *a, const char *pathname)
1067 if (a->current_fixup == NULL)
1068 a->current_fixup = new_fixup(a, pathname);
1069 return (a->current_fixup);
1072 /* TODO: Make this work. */
1074 * TODO: The deep-directory support bypasses this; disable deep directory
1075 * support if we're doing symlink checks.
1078 * TODO: Someday, integrate this with the deep dir support; they both
1079 * scan the path and both can be optimized by comparing against other
1080 * recent paths.
1082 static int
1083 check_symlinks(struct archive_write_disk *a)
1085 char *pn, *p;
1086 char c;
1087 int r;
1088 struct stat st;
1091 * Gaurd against symlink tricks. Reject any archive entry whose
1092 * destination would be altered by a symlink.
1094 /* Whatever we checked last time doesn't need to be re-checked. */
1095 pn = a->name;
1096 p = a->path_safe.s;
1097 while ((*pn != '\0') && (*p == *pn))
1098 ++p, ++pn;
1099 c = pn[0];
1100 /* Keep going until we've checked the entire name. */
1101 while (pn[0] != '\0' && (pn[0] != '/' || pn[1] != '\0')) {
1102 /* Skip the next path element. */
1103 while (*pn != '\0' && *pn != '/')
1104 ++pn;
1105 c = pn[0];
1106 pn[0] = '\0';
1107 /* Check that we haven't hit a symlink. */
1108 r = lstat(a->name, &st);
1109 if (r != 0) {
1110 /* We've hit a dir that doesn't exist; stop now. */
1111 if (errno == ENOENT)
1112 break;
1113 } else if (S_ISLNK(st.st_mode)) {
1114 if (c == '\0') {
1116 * Last element is symlink; remove it
1117 * so we can overwrite it with the
1118 * item being extracted.
1120 if (unlink(a->name)) {
1121 archive_set_error(&a->archive, errno,
1122 "Could not remove symlink %s",
1123 a->name);
1124 pn[0] = c;
1125 return (ARCHIVE_WARN);
1128 * Even if we did remove it, a warning
1129 * is in order. The warning is silly,
1130 * though, if we're just replacing one
1131 * symlink with another symlink.
1133 if (!S_ISLNK(a->mode)) {
1134 archive_set_error(&a->archive, 0,
1135 "Removing symlink %s",
1136 a->name);
1138 /* Symlink gone. No more problem! */
1139 pn[0] = c;
1140 return (0);
1141 } else if (a->flags & ARCHIVE_EXTRACT_UNLINK) {
1142 /* User asked us to remove problems. */
1143 if (unlink(a->name) != 0) {
1144 archive_set_error(&a->archive, 0,
1145 "Cannot remove intervening symlink %s",
1146 a->name);
1147 pn[0] = c;
1148 return (ARCHIVE_WARN);
1150 } else {
1151 archive_set_error(&a->archive, 0,
1152 "Cannot extract through symlink %s",
1153 a->name);
1154 pn[0] = c;
1155 return (ARCHIVE_WARN);
1159 pn[0] = c;
1160 /* We've checked and/or cleaned the whole path, so remember it. */
1161 archive_strcpy(&a->path_safe, a->name);
1162 return (ARCHIVE_OK);
1166 * Canonicalize the pathname. In particular, this strips duplicate
1167 * '/' characters, '.' elements, and trailing '/'. It also raises an
1168 * error for an empty path, a trailing '..' or (if _SECURE_NODOTDOT is
1169 * set) any '..' in the path.
1171 static int
1172 cleanup_pathname(struct archive_write_disk *a)
1174 char *dest, *src;
1175 char separator = '\0';
1176 int lastdotdot = 0; /* True if last elt copied was '..' */
1178 dest = src = a->name;
1179 if (*src == '\0') {
1180 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1181 "Invalid empty pathname");
1182 return (ARCHIVE_WARN);
1185 /* Skip leading '/'. */
1186 if (*src == '/')
1187 separator = *src++;
1189 /* Scan the pathname one element at a time. */
1190 for (;;) {
1191 /* src points to first char after '/' */
1192 if (src[0] == '\0') {
1193 break;
1194 } else if (src[0] == '/') {
1195 /* Found '//', ignore second one. */
1196 src++;
1197 continue;
1198 } else if (src[0] == '.') {
1199 if (src[1] == '\0') {
1200 /* Ignore trailing '.' */
1201 break;
1202 } else if (src[1] == '/') {
1203 /* Skip './'. */
1204 src += 2;
1205 continue;
1206 } else if (src[1] == '.') {
1207 if (src[2] == '/' || src[2] == '\0') {
1208 /* Conditionally warn about '..' */
1209 if (a->flags & ARCHIVE_EXTRACT_SECURE_NODOTDOT) {
1210 archive_set_error(&a->archive,
1211 ARCHIVE_ERRNO_MISC,
1212 "Path contains '..'");
1213 return (ARCHIVE_WARN);
1215 lastdotdot = 1;
1216 } else
1217 lastdotdot = 0;
1219 * Note: Under no circumstances do we
1220 * remove '..' elements. In
1221 * particular, restoring
1222 * '/foo/../bar/' should create the
1223 * 'foo' dir as a side-effect.
1225 } else
1226 lastdotdot = 0;
1227 } else
1228 lastdotdot = 0;
1230 /* Copy current element, including leading '/'. */
1231 if (separator)
1232 *dest++ = '/';
1233 while (*src != '\0' && *src != '/') {
1234 *dest++ = *src++;
1237 if (*src == '\0')
1238 break;
1240 /* Skip '/' separator. */
1241 separator = *src++;
1244 * We've just copied zero or more path elements, not including the
1245 * final '/'.
1247 if (lastdotdot) {
1248 /* Trailing '..' is always wrong. */
1249 archive_set_error(&a->archive,
1250 ARCHIVE_ERRNO_MISC,
1251 "Path contains trailing '..'");
1252 return (ARCHIVE_WARN);
1254 if (dest == a->name) {
1256 * Nothing got copied. The path must have been something
1257 * like '.' or '/' or './' or '/././././/./'.
1259 if (separator)
1260 *dest++ = '/';
1261 else
1262 *dest++ = '.';
1264 /* Terminate the result. */
1265 *dest = '\0';
1266 return (ARCHIVE_OK);
1270 * Create the parent directory of the specified path, assuming path
1271 * is already in mutable storage.
1273 static int
1274 create_parent_dir(struct archive_write_disk *a, char *path)
1276 char *slash;
1277 int r;
1279 /* Remove tail element to obtain parent name. */
1280 slash = strrchr(path, '/');
1281 if (slash == NULL)
1282 return (ARCHIVE_OK);
1283 *slash = '\0';
1284 r = create_dir(a, path);
1285 *slash = '/';
1286 return (r);
1290 * Create the specified dir, recursing to create parents as necessary.
1292 * Returns ARCHIVE_OK if the path exists when we're done here.
1293 * Otherwise, returns ARCHIVE_WARN.
1294 * Assumes path is in mutable storage; path is unchanged on exit.
1296 static int
1297 create_dir(struct archive_write_disk *a, char *path)
1299 struct stat st;
1300 struct fixup_entry *le;
1301 char *slash, *base;
1302 mode_t mode_final, mode;
1303 int r;
1305 r = ARCHIVE_OK;
1307 /* Check for special names and just skip them. */
1308 slash = strrchr(path, '/');
1309 if (slash == NULL)
1310 base = path;
1311 else
1312 base = slash + 1;
1314 if (base[0] == '\0' ||
1315 (base[0] == '.' && base[1] == '\0') ||
1316 (base[0] == '.' && base[1] == '.' && base[2] == '\0')) {
1317 /* Don't bother trying to create null path, '.', or '..'. */
1318 if (slash != NULL) {
1319 *slash = '\0';
1320 r = create_dir(a, path);
1321 *slash = '/';
1322 return (r);
1324 return (ARCHIVE_OK);
1328 * Yes, this should be stat() and not lstat(). Using lstat()
1329 * here loses the ability to extract through symlinks. Also note
1330 * that this should not use the a->st cache.
1332 if (stat(path, &st) == 0) {
1333 if (S_ISDIR(st.st_mode))
1334 return (ARCHIVE_OK);
1335 if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
1336 archive_set_error(&a->archive, EEXIST,
1337 "Can't create directory '%s'", path);
1338 return (ARCHIVE_WARN);
1340 if (unlink(path) != 0) {
1341 archive_set_error(&a->archive, errno,
1342 "Can't create directory '%s': "
1343 "Conflicting file cannot be removed");
1344 return (ARCHIVE_WARN);
1346 } else if (errno != ENOENT && errno != ENOTDIR) {
1347 /* Stat failed? */
1348 archive_set_error(&a->archive, errno, "Can't test directory '%s'", path);
1349 return (ARCHIVE_WARN);
1350 } else if (slash != NULL) {
1351 *slash = '\0';
1352 r = create_dir(a, path);
1353 *slash = '/';
1354 if (r != ARCHIVE_OK)
1355 return (r);
1359 * Mode we want for the final restored directory. Per POSIX,
1360 * implicitly-created dirs must be created obeying the umask.
1361 * There's no mention whether this is different for privileged
1362 * restores (which the rest of this code handles by pretending
1363 * umask=0). I've chosen here to always obey the user's umask for
1364 * implicit dirs, even if _EXTRACT_PERM was specified.
1366 mode_final = DEFAULT_DIR_MODE & ~a->user_umask;
1367 /* Mode we want on disk during the restore process. */
1368 mode = mode_final;
1369 mode |= MINIMUM_DIR_MODE;
1370 mode &= MAXIMUM_DIR_MODE;
1371 if (mkdir(path, mode) == 0) {
1372 if (mode != mode_final) {
1373 le = new_fixup(a, path);
1374 le->fixup |=TODO_MODE_BASE;
1375 le->mode = mode_final;
1377 return (ARCHIVE_OK);
1381 * Without the following check, a/b/../b/c/d fails at the
1382 * second visit to 'b', so 'd' can't be created. Note that we
1383 * don't add it to the fixup list here, as it's already been
1384 * added.
1386 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1387 return (ARCHIVE_OK);
1389 archive_set_error(&a->archive, errno, "Failed to create dir '%s'", path);
1390 return (ARCHIVE_WARN);
1394 * Note: Although we can skip setting the user id if the desired user
1395 * id matches the current user, we cannot skip setting the group, as
1396 * many systems set the gid bit based on the containing directory. So
1397 * we have to perform a chown syscall if we want to restore the SGID
1398 * bit. (The alternative is to stat() and then possibly chown(); it's
1399 * more efficient to skip the stat() and just always chown().) Note
1400 * that a successful chown() here clears the TODO_SGID_CHECK bit, which
1401 * allows set_mode to skip the stat() check for the GID.
1403 static int
1404 set_ownership(struct archive_write_disk *a)
1406 /* If we know we can't change it, don't bother trying. */
1407 if (a->user_uid != 0 && a->user_uid != a->uid) {
1408 archive_set_error(&a->archive, errno,
1409 "Can't set UID=%d", a->uid);
1410 return (ARCHIVE_WARN);
1413 #ifdef HAVE_FCHOWN
1414 if (a->fd >= 0 && fchown(a->fd, a->uid, a->gid) == 0)
1415 goto success;
1416 #endif
1418 #ifdef HAVE_LCHOWN
1419 if (lchown(a->name, a->uid, a->gid) == 0)
1420 goto success;
1421 #else
1422 if (!S_ISLNK(a->mode) && chown(a->name, a->uid, a->gid) == 0)
1423 goto success;
1424 #endif
1426 archive_set_error(&a->archive, errno,
1427 "Can't set user=%d/group=%d for %s", a->uid, a->gid,
1428 a->name);
1429 return (ARCHIVE_WARN);
1430 success:
1431 a->todo &= ~TODO_OWNER;
1432 /* We know the user/group are correct now. */
1433 a->todo &= ~TODO_SGID_CHECK;
1434 a->todo &= ~TODO_SUID_CHECK;
1435 return (ARCHIVE_OK);
1438 #ifdef HAVE_UTIMES
1440 * The utimes()-family functions provide high resolution and
1441 * a way to set time on an fd or a symlink. We prefer them
1442 * when they're available.
1444 static int
1445 set_time(struct archive_write_disk *a)
1447 struct timeval times[2];
1449 times[1].tv_sec = archive_entry_mtime(a->entry);
1450 times[1].tv_usec = archive_entry_mtime_nsec(a->entry) / 1000;
1452 times[0].tv_sec = archive_entry_atime(a->entry);
1453 times[0].tv_usec = archive_entry_atime_nsec(a->entry) / 1000;
1455 #ifdef HAVE_FUTIMES
1456 if (a->fd >= 0 && futimes(a->fd, times) == 0) {
1457 return (ARCHIVE_OK);
1459 #endif
1461 #ifdef HAVE_LUTIMES
1462 if (lutimes(a->name, times) != 0)
1463 #else
1464 if (!S_ISLNK(a->mode) && utimes(a->name, times) != 0)
1465 #endif
1467 archive_set_error(&a->archive, errno, "Can't update time for %s",
1468 a->name);
1469 return (ARCHIVE_WARN);
1473 * Note: POSIX does not provide a portable way to restore ctime.
1474 * (Apart from resetting the system clock, which is distasteful.)
1475 * So, any restoration of ctime will necessarily be OS-specific.
1478 /* XXX TODO: Can FreeBSD restore ctime? XXX */
1479 return (ARCHIVE_OK);
1481 #elif defined(HAVE_UTIME)
1483 * utime() is an older, more standard interface that we'll use
1484 * if utimes() isn't available.
1486 static int
1487 set_time(struct archive_write_disk *a)
1489 struct utimbuf times;
1491 times.modtime = archive_entry_mtime(a->entry);
1492 times.actime = archive_entry_atime(a->entry);
1493 if (!S_ISLNK(a->mode) && utime(a->name, &times) != 0) {
1494 archive_set_error(&a->archive, errno,
1495 "Can't update time for %s", a->name);
1496 return (ARCHIVE_WARN);
1498 return (ARCHIVE_OK);
1500 #else
1501 /* This platform doesn't give us a way to restore the time. */
1502 static int
1503 set_time(struct archive_write_disk *a)
1505 (void)a; /* UNUSED */
1506 archive_set_error(&a->archive, errno,
1507 "Can't update time for %s", a->name);
1508 return (ARCHIVE_WARN);
1510 #endif
1513 static int
1514 set_mode(struct archive_write_disk *a, int mode)
1516 int r = ARCHIVE_OK;
1517 mode &= 07777; /* Strip off file type bits. */
1519 if (a->todo & TODO_SGID_CHECK) {
1521 * If we don't know the GID is right, we must stat()
1522 * to verify it. We can't just check the GID of this
1523 * process, since systems sometimes set GID from
1524 * the enclosing dir or based on ACLs.
1526 if (a->pst != NULL) {
1527 /* Already have stat() data available. */
1528 #ifdef HAVE_FSTAT
1529 } else if (fd >= 0 && fstat(fd, &a->st) == 0) {
1530 a->pst = &a->st;
1531 #endif
1532 } else if (stat(a->name, &a->st) == 0) {
1533 a->pst = &a->st;
1534 } else {
1535 archive_set_error(&a->archive, errno,
1536 "Couldn't stat file");
1537 return (ARCHIVE_WARN);
1539 if (a->pst->st_gid != a->gid) {
1540 mode &= ~ S_ISGID;
1541 archive_set_error(&a->archive, -1, "Can't restore SGID bit");
1542 r = ARCHIVE_WARN;
1544 /* While we're here, double-check the UID. */
1545 if (a->pst->st_uid != a->uid
1546 && (a->todo & TODO_SUID)) {
1547 mode &= ~ S_ISUID;
1548 archive_set_error(&a->archive, -1, "Can't restore SUID bit");
1549 r = ARCHIVE_WARN;
1551 a->todo &= ~TODO_SGID_CHECK;
1552 a->todo &= ~TODO_SUID_CHECK;
1553 } else if (a->todo & TODO_SUID_CHECK) {
1555 * If we don't know the UID is right, we can just check
1556 * the user, since all systems set the file UID from
1557 * the process UID.
1559 if (a->user_uid != a->uid) {
1560 mode &= ~ S_ISUID;
1561 archive_set_error(&a->archive, -1, "Can't make file SUID");
1562 r = ARCHIVE_WARN;
1564 a->todo &= ~TODO_SUID_CHECK;
1567 if (S_ISLNK(a->mode)) {
1568 #ifdef HAVE_LCHMOD
1570 * If this is a symlink, use lchmod(). If the
1571 * platform doesn't support lchmod(), just skip it. A
1572 * platform that doesn't provide a way to set
1573 * permissions on symlinks probably ignores
1574 * permissions on symlinks, so a failure here has no
1575 * impact.
1577 if (lchmod(a->name, mode) != 0) {
1578 archive_set_error(&a->archive, errno,
1579 "Can't set permissions to 0%o", (int)mode);
1580 r = ARCHIVE_WARN;
1582 #endif
1583 } else if (!S_ISDIR(a->mode)) {
1585 * If it's not a symlink and not a dir, then use
1586 * fchmod() or chmod(), depending on whether we have
1587 * an fd. Dirs get their perms set during the
1588 * post-extract fixup, which is handled elsewhere.
1590 #ifdef HAVE_FCHMOD
1591 if (a->fd >= 0) {
1592 if (fchmod(a->fd, mode) != 0) {
1593 archive_set_error(&a->archive, errno,
1594 "Can't set permissions to 0%o", (int)mode);
1595 r = ARCHIVE_WARN;
1597 } else
1598 #endif
1599 /* If this platform lacks fchmod(), then
1600 * we'll just use chmod(). */
1601 if (chmod(a->name, mode) != 0) {
1602 archive_set_error(&a->archive, errno,
1603 "Can't set permissions to 0%o", (int)mode);
1604 r = ARCHIVE_WARN;
1607 return (r);
1610 static int
1611 set_fflags(struct archive_write_disk *a)
1613 struct fixup_entry *le;
1614 unsigned long set, clear;
1615 int r;
1616 int critical_flags;
1617 mode_t mode = archive_entry_mode(a->entry);
1620 * Make 'critical_flags' hold all file flags that can't be
1621 * immediately restored. For example, on BSD systems,
1622 * SF_IMMUTABLE prevents hardlinks from being created, so
1623 * should not be set until after any hardlinks are created. To
1624 * preserve some semblance of portability, this uses #ifdef
1625 * extensively. Ugly, but it works.
1627 * Yes, Virginia, this does create a security race. It's mitigated
1628 * somewhat by the practice of creating dirs 0700 until the extract
1629 * is done, but it would be nice if we could do more than that.
1630 * People restoring critical file systems should be wary of
1631 * other programs that might try to muck with files as they're
1632 * being restored.
1634 /* Hopefully, the compiler will optimize this mess into a constant. */
1635 critical_flags = 0;
1636 #ifdef SF_IMMUTABLE
1637 critical_flags |= SF_IMMUTABLE;
1638 #endif
1639 #ifdef UF_IMMUTABLE
1640 critical_flags |= UF_IMMUTABLE;
1641 #endif
1642 #ifdef SF_APPEND
1643 critical_flags |= SF_APPEND;
1644 #endif
1645 #ifdef UF_APPEND
1646 critical_flags |= UF_APPEND;
1647 #endif
1648 #ifdef EXT2_APPEND_FL
1649 critical_flags |= EXT2_APPEND_FL;
1650 #endif
1651 #ifdef EXT2_IMMUTABLE_FL
1652 critical_flags |= EXT2_IMMUTABLE_FL;
1653 #endif
1655 if (a->todo & TODO_FFLAGS) {
1656 archive_entry_fflags(a->entry, &set, &clear);
1659 * The first test encourages the compiler to eliminate
1660 * all of this if it's not necessary.
1662 if ((critical_flags != 0) && (set & critical_flags)) {
1663 le = current_fixup(a, a->name);
1664 le->fixup |= TODO_FFLAGS;
1665 le->fflags_set = set;
1666 /* Store the mode if it's not already there. */
1667 if ((le->fixup & TODO_MODE) == 0)
1668 le->mode = mode;
1669 } else {
1670 r = set_fflags_platform(a, a->fd,
1671 a->name, mode, set, clear);
1672 if (r != ARCHIVE_OK)
1673 return (r);
1676 return (ARCHIVE_OK);
1680 #if ( defined(HAVE_LCHFLAGS) || defined(HAVE_CHFLAGS) || defined(HAVE_FCHFLAGS) ) && !defined(__linux)
1681 static int
1682 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
1683 mode_t mode, unsigned long set, unsigned long clear)
1685 (void)mode; /* UNUSED */
1686 if (set == 0 && clear == 0)
1687 return (ARCHIVE_OK);
1690 * XXX Is the stat here really necessary? Or can I just use
1691 * the 'set' flags directly? In particular, I'm not sure
1692 * about the correct approach if we're overwriting an existing
1693 * file that already has flags on it. XXX
1695 if (fd >= 0 && fstat(fd, &a->st) == 0)
1696 a->pst = &a->st;
1697 else if (lstat(name, &a->st) == 0)
1698 a->pst = &a->st;
1699 else {
1700 archive_set_error(&a->archive, errno,
1701 "Couldn't stat file");
1702 return (ARCHIVE_WARN);
1705 a->st.st_flags &= ~clear;
1706 a->st.st_flags |= set;
1707 #ifdef HAVE_FCHFLAGS
1708 /* If platform has fchflags() and we were given an fd, use it. */
1709 if (fd >= 0 && fchflags(fd, a->st.st_flags) == 0)
1710 return (ARCHIVE_OK);
1711 #endif
1713 * If we can't use the fd to set the flags, we'll use the
1714 * pathname to set flags. We prefer lchflags() but will use
1715 * chflags() if we must.
1717 #ifdef HAVE_LCHFLAGS
1718 if (lchflags(name, a->st.st_flags) == 0)
1719 return (ARCHIVE_OK);
1720 #elif defined(HAVE_CHFLAGS)
1721 if (S_ISLNK(a->st.st_mode)) {
1722 archive_set_error(&a->archive, errno,
1723 "Can't set file flags on symlink.");
1724 return (ARCHIVE_WARN);
1726 if (chflags(name, a->st.st_flags) == 0)
1727 return (ARCHIVE_OK);
1728 #endif
1729 archive_set_error(&a->archive, errno,
1730 "Failed to set file flags");
1731 return (ARCHIVE_WARN);
1734 #elif defined(__linux) && defined(EXT2_IOC_GETFLAGS) && defined(EXT2_IOC_SETFLAGS)
1737 * Linux has flags too, but uses ioctl() to access them instead of
1738 * having a separate chflags() system call.
1740 static int
1741 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
1742 mode_t mode, unsigned long set, unsigned long clear)
1744 int ret;
1745 int myfd = fd;
1746 unsigned long newflags, oldflags;
1747 unsigned long sf_mask = 0;
1749 if (set == 0 && clear == 0)
1750 return (ARCHIVE_OK);
1751 /* Only regular files and dirs can have flags. */
1752 if (!S_ISREG(mode) && !S_ISDIR(mode))
1753 return (ARCHIVE_OK);
1755 /* If we weren't given an fd, open it ourselves. */
1756 if (myfd < 0)
1757 myfd = open(name, O_RDONLY|O_NONBLOCK);
1758 if (myfd < 0)
1759 return (ARCHIVE_OK);
1762 * Linux has no define for the flags that are only settable by
1763 * the root user. This code may seem a little complex, but
1764 * there seem to be some Linux systems that lack these
1765 * defines. (?) The code below degrades reasonably gracefully
1766 * if sf_mask is incomplete.
1768 #ifdef EXT2_IMMUTABLE_FL
1769 sf_mask |= EXT2_IMMUTABLE_FL;
1770 #endif
1771 #ifdef EXT2_APPEND_FL
1772 sf_mask |= EXT2_APPEND_FL;
1773 #endif
1775 * XXX As above, this would be way simpler if we didn't have
1776 * to read the current flags from disk. XXX
1778 ret = ARCHIVE_OK;
1779 /* Try setting the flags as given. */
1780 if (ioctl(myfd, EXT2_IOC_GETFLAGS, &oldflags) >= 0) {
1781 newflags = (oldflags & ~clear) | set;
1782 if (ioctl(myfd, EXT2_IOC_SETFLAGS, &newflags) >= 0)
1783 goto cleanup;
1784 if (errno != EPERM)
1785 goto fail;
1787 /* If we couldn't set all the flags, try again with a subset. */
1788 if (ioctl(myfd, EXT2_IOC_GETFLAGS, &oldflags) >= 0) {
1789 newflags &= ~sf_mask;
1790 oldflags &= sf_mask;
1791 newflags |= oldflags;
1792 if (ioctl(myfd, EXT2_IOC_SETFLAGS, &newflags) >= 0)
1793 goto cleanup;
1795 /* We couldn't set the flags, so report the failure. */
1796 fail:
1797 archive_set_error(&a->archive, errno,
1798 "Failed to set file flags");
1799 ret = ARCHIVE_WARN;
1800 cleanup:
1801 if (fd < 0)
1802 close(myfd);
1803 return (ret);
1806 #else /* Not HAVE_CHFLAGS && Not __linux */
1809 * Of course, some systems have neither BSD chflags() nor Linux' flags
1810 * support through ioctl().
1812 static int
1813 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
1814 mode_t mode, unsigned long set, unsigned long clear)
1816 (void)a; /* UNUSED */
1817 (void)fd; /* UNUSED */
1818 (void)name; /* UNUSED */
1819 (void)mode; /* UNUSED */
1820 (void)set; /* UNUSED */
1821 (void)clear; /* UNUSED */
1822 return (ARCHIVE_OK);
1825 #endif /* __linux */
1827 #ifndef HAVE_POSIX_ACL
1828 /* Default empty function body to satisfy mainline code. */
1829 static int
1830 set_acls(struct archive_write_disk *a)
1832 (void)a; /* UNUSED */
1833 return (ARCHIVE_OK);
1836 #else
1839 * XXX TODO: What about ACL types other than ACCESS and DEFAULT?
1841 static int
1842 set_acls(struct archive_write_disk *a)
1844 int ret;
1846 ret = set_acl(a, a->fd, a->entry, ACL_TYPE_ACCESS,
1847 ARCHIVE_ENTRY_ACL_TYPE_ACCESS, "access");
1848 if (ret != ARCHIVE_OK)
1849 return (ret);
1850 ret = set_acl(a, a->fd, a->entry, ACL_TYPE_DEFAULT,
1851 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT, "default");
1852 return (ret);
1856 static int
1857 set_acl(struct archive_write_disk *a, int fd, struct archive_entry *entry,
1858 acl_type_t acl_type, int ae_requested_type, const char *tname)
1860 acl_t acl;
1861 acl_entry_t acl_entry;
1862 acl_permset_t acl_permset;
1863 int ret;
1864 int ae_type, ae_permset, ae_tag, ae_id;
1865 uid_t ae_uid;
1866 gid_t ae_gid;
1867 const char *ae_name;
1868 int entries;
1869 const char *name;
1871 ret = ARCHIVE_OK;
1872 entries = archive_entry_acl_reset(entry, ae_requested_type);
1873 if (entries == 0)
1874 return (ARCHIVE_OK);
1875 acl = acl_init(entries);
1876 while (archive_entry_acl_next(entry, ae_requested_type, &ae_type,
1877 &ae_permset, &ae_tag, &ae_id, &ae_name) == ARCHIVE_OK) {
1878 acl_create_entry(&acl, &acl_entry);
1880 switch (ae_tag) {
1881 case ARCHIVE_ENTRY_ACL_USER:
1882 acl_set_tag_type(acl_entry, ACL_USER);
1883 ae_uid = a->lookup_uid(a->lookup_uid_data,
1884 ae_name, ae_id);
1885 acl_set_qualifier(acl_entry, &ae_uid);
1886 break;
1887 case ARCHIVE_ENTRY_ACL_GROUP:
1888 acl_set_tag_type(acl_entry, ACL_GROUP);
1889 ae_gid = a->lookup_gid(a->lookup_gid_data,
1890 ae_name, ae_id);
1891 acl_set_qualifier(acl_entry, &ae_gid);
1892 break;
1893 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1894 acl_set_tag_type(acl_entry, ACL_USER_OBJ);
1895 break;
1896 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1897 acl_set_tag_type(acl_entry, ACL_GROUP_OBJ);
1898 break;
1899 case ARCHIVE_ENTRY_ACL_MASK:
1900 acl_set_tag_type(acl_entry, ACL_MASK);
1901 break;
1902 case ARCHIVE_ENTRY_ACL_OTHER:
1903 acl_set_tag_type(acl_entry, ACL_OTHER);
1904 break;
1905 default:
1906 /* XXX */
1907 break;
1910 acl_get_permset(acl_entry, &acl_permset);
1911 acl_clear_perms(acl_permset);
1912 if (ae_permset & ARCHIVE_ENTRY_ACL_EXECUTE)
1913 acl_add_perm(acl_permset, ACL_EXECUTE);
1914 if (ae_permset & ARCHIVE_ENTRY_ACL_WRITE)
1915 acl_add_perm(acl_permset, ACL_WRITE);
1916 if (ae_permset & ARCHIVE_ENTRY_ACL_READ)
1917 acl_add_perm(acl_permset, ACL_READ);
1920 name = archive_entry_pathname(entry);
1922 /* Try restoring the ACL through 'fd' if we can. */
1923 #if HAVE_ACL_SET_FD
1924 if (fd >= 0 && acl_type == ACL_TYPE_ACCESS && acl_set_fd(fd, acl) == 0)
1925 ret = ARCHIVE_OK;
1926 else
1927 #else
1928 #if HAVE_ACL_SET_FD_NP
1929 if (fd >= 0 && acl_set_fd_np(fd, acl, acl_type) == 0)
1930 ret = ARCHIVE_OK;
1931 else
1932 #endif
1933 #endif
1934 if (acl_set_file(name, acl_type, acl) != 0) {
1935 archive_set_error(&a->archive, errno, "Failed to set %s acl", tname);
1936 ret = ARCHIVE_WARN;
1938 acl_free(acl);
1939 return (ret);
1941 #endif
1943 #if HAVE_LSETXATTR
1945 * Restore extended attributes - Linux implementation
1947 static int
1948 set_xattrs(struct archive_write_disk *a)
1950 struct archive_entry *entry = a->entry;
1951 static int warning_done = 0;
1952 int ret = ARCHIVE_OK;
1953 int i = archive_entry_xattr_reset(entry);
1955 while (i--) {
1956 const char *name;
1957 const void *value;
1958 size_t size;
1959 archive_entry_xattr_next(entry, &name, &value, &size);
1960 if (name != NULL &&
1961 strncmp(name, "xfsroot.", 8) != 0 &&
1962 strncmp(name, "system.", 7) != 0) {
1963 int e;
1964 #if HAVE_FSETXATTR
1965 if (a->fd >= 0)
1966 e = fsetxattr(a->fd, name, value, size, 0);
1967 else
1968 #endif
1970 e = lsetxattr(archive_entry_pathname(entry),
1971 name, value, size, 0);
1973 if (e == -1) {
1974 if (errno == ENOTSUP) {
1975 if (!warning_done) {
1976 warning_done = 1;
1977 archive_set_error(&a->archive, errno,
1978 "Cannot restore extended "
1979 "attributes on this file "
1980 "system");
1982 } else
1983 archive_set_error(&a->archive, errno,
1984 "Failed to set extended attribute");
1985 ret = ARCHIVE_WARN;
1987 } else {
1988 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1989 "Invalid extended attribute encountered");
1990 ret = ARCHIVE_WARN;
1993 return (ret);
1995 #else
1997 * Restore extended attributes - stub implementation for unsupported systems
1999 static int
2000 set_xattrs(struct archive_write_disk *a)
2002 static int warning_done = 0;
2004 /* If there aren't any extended attributes, then it's okay not
2005 * to extract them, otherwise, issue a single warning. */
2006 if (archive_entry_xattr_count(a->entry) != 0 && !warning_done) {
2007 warning_done = 1;
2008 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2009 "Cannot restore extended attributes on this system");
2010 return (ARCHIVE_WARN);
2012 /* Warning was already emitted; suppress further warnings. */
2013 return (ARCHIVE_OK);
2015 #endif
2019 * Trivial implementations of gid/uid lookup functions.
2020 * These are normally overridden by the client, but these stub
2021 * versions ensure that we always have something that works.
2023 static gid_t
2024 trivial_lookup_gid(void *private_data, const char *gname, gid_t gid)
2026 (void)private_data; /* UNUSED */
2027 (void)gname; /* UNUSED */
2028 return (gid);
2031 static uid_t
2032 trivial_lookup_uid(void *private_data, const char *uname, uid_t uid)
2034 (void)private_data; /* UNUSED */
2035 (void)uname; /* UNUSED */
2036 return (uid);
2040 * Test if file on disk is older than entry.
2042 static int
2043 older(struct stat *st, struct archive_entry *entry)
2045 /* First, test the seconds and return if we have a definite answer. */
2046 /* Definitely older. */
2047 if (st->st_mtime < archive_entry_mtime(entry))
2048 return (1);
2049 /* Definitely younger. */
2050 if (st->st_mtime > archive_entry_mtime(entry))
2051 return (0);
2052 /* If this platform supports fractional seconds, try those. */
2053 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
2054 /* Definitely older. */
2055 if (st->st_mtimespec.tv_nsec < archive_entry_mtime_nsec(entry))
2056 return (1);
2057 /* Definitely younger. */
2058 if (st->st_mtimespec.tv_nsec > archive_entry_mtime_nsec(entry))
2059 return (0);
2060 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
2061 /* Definitely older. */
2062 if (st->st_mtim.tv_nsec < archive_entry_mtime_nsec(entry))
2063 return (1);
2064 /* Definitely older. */
2065 if (st->st_mtim.tv_nsec > archive_entry_mtime_nsec(entry))
2066 return (0);
2067 #else
2068 /* This system doesn't have high-res timestamps. */
2069 #endif
2070 /* Same age, so not older. */
2071 return (0);