Import libarchive-2.5.5.
[dragonfly.git] / contrib / libarchive-2.0 / libarchive / archive_write_disk.c
blobde9d7fe8141aecdb2c8ef47503967237e019f0d8
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.5 2007/04/02 00:32:03 kientzle 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
84 #include "archive.h"
85 #include "archive_string.h"
86 #include "archive_entry.h"
87 #include "archive_private.h"
89 struct fixup_entry {
90 struct fixup_entry *next;
91 mode_t mode;
92 int64_t mtime;
93 int64_t atime;
94 unsigned long mtime_nanos;
95 unsigned long atime_nanos;
96 unsigned long fflags_set;
97 int fixup; /* bitmask of what needs fixing */
98 char *name;
102 * We use a bitmask to track which operations remain to be done for
103 * this file. In particular, this helps us avoid unnecessary
104 * operations when it's possible to take care of one step as a
105 * side-effect of another. For example, mkdir() can specify the mode
106 * for the newly-created object but symlink() cannot. This means we
107 * can skip chmod() if mkdir() succeeded, but we must explicitly
108 * chmod() if we're trying to create a directory that already exists
109 * (mkdir() failed) or if we're restoring a symlink. Similarly, we
110 * need to verify UID/GID before trying to restore SUID/SGID bits;
111 * that verification can occur explicitly through a stat() call or
112 * implicitly because of a successful chown() call.
114 #define TODO_MODE_FORCE 0x40000000
115 #define TODO_MODE_BASE 0x20000000
116 #define TODO_SUID 0x10000000
117 #define TODO_SUID_CHECK 0x08000000
118 #define TODO_SGID 0x04000000
119 #define TODO_SGID_CHECK 0x02000000
120 #define TODO_MODE (TODO_MODE_BASE|TODO_SUID|TODO_SGID)
121 #define TODO_TIMES ARCHIVE_EXTRACT_TIME
122 #define TODO_OWNER ARCHIVE_EXTRACT_OWNER
123 #define TODO_FFLAGS ARCHIVE_EXTRACT_FFLAGS
124 #define TODO_ACLS ARCHIVE_EXTRACT_ACL
125 #define TODO_XATTR ARCHIVE_EXTRACT_XATTR
127 struct archive_write_disk {
128 struct archive archive;
130 mode_t user_umask;
131 struct fixup_entry *fixup_list;
132 struct fixup_entry *current_fixup;
133 uid_t user_uid;
134 dev_t skip_file_dev;
135 ino_t skip_file_ino;
137 gid_t (*lookup_gid)(void *private, const char *gname, gid_t gid);
138 void (*cleanup_gid)(void *private);
139 void *lookup_gid_data;
140 uid_t (*lookup_uid)(void *private, const char *gname, gid_t gid);
141 void (*cleanup_uid)(void *private);
142 void *lookup_uid_data;
145 * Full path of last file to satisfy symlink checks.
147 struct archive_string path_safe;
150 * Cached stat data from disk for the current entry.
151 * If this is valid, pst points to st. Otherwise,
152 * pst is null.
154 struct stat st;
155 struct stat *pst;
157 /* Information about the object being restored right now. */
158 struct archive_entry *entry; /* Entry being extracted. */
159 char *name; /* Name of entry, possibly edited. */
160 struct archive_string _name_data; /* backing store for 'name' */
161 /* Tasks remaining for this object. */
162 int todo;
163 /* Tasks deferred until end-of-archive. */
164 int deferred;
165 /* Options requested by the client. */
166 int flags;
167 /* Handle for the file we're restoring. */
168 int fd;
169 /* Current offset for writing data to the file. */
170 off_t offset;
171 /* Dir we were in before this restore; only for deep paths. */
172 int restore_pwd;
173 /* Mode we should use for this entry; affected by _PERM and umask. */
174 mode_t mode;
175 /* UID/GID to use in restoring this entry. */
176 uid_t uid;
177 gid_t gid;
181 * Default mode for dirs created automatically (will be modified by umask).
182 * Note that POSIX specifies 0777 for implicity-created dirs, "modified
183 * by the process' file creation mask."
185 #define DEFAULT_DIR_MODE 0777
187 * Dir modes are restored in two steps: During the extraction, the permissions
188 * in the archive are modified to match the following limits. During
189 * the post-extract fixup pass, the permissions from the archive are
190 * applied.
192 #define MINIMUM_DIR_MODE 0700
193 #define MAXIMUM_DIR_MODE 0775
195 static int check_symlinks(struct archive_write_disk *);
196 static int create_filesystem_object(struct archive_write_disk *);
197 static struct fixup_entry *current_fixup(struct archive_write_disk *, const char *pathname);
198 #ifdef HAVE_FCHDIR
199 static void edit_deep_directories(struct archive_write_disk *ad);
200 #endif
201 static int cleanup_pathname(struct archive_write_disk *);
202 static int create_dir(struct archive_write_disk *, char *);
203 static int create_parent_dir(struct archive_write_disk *, char *);
204 static int restore_entry(struct archive_write_disk *);
205 #ifdef HAVE_POSIX_ACL
206 static int set_acl(struct archive_write_disk *, int fd, struct archive_entry *,
207 acl_type_t, int archive_entry_acl_type, const char *tn);
208 #endif
209 static int set_acls(struct archive_write_disk *);
210 static int set_xattrs(struct archive_write_disk *);
211 static int set_fflags(struct archive_write_disk *);
212 static int set_fflags_platform(struct archive_write_disk *, int fd,
213 const char *name, mode_t mode,
214 unsigned long fflags_set, unsigned long fflags_clear);
215 static int set_ownership(struct archive_write_disk *);
216 static int set_mode(struct archive_write_disk *, int mode);
217 static int set_time(struct archive_write_disk *);
218 static struct fixup_entry *sort_dir_list(struct fixup_entry *p);
219 static gid_t trivial_lookup_gid(void *, const char *, gid_t);
220 static uid_t trivial_lookup_uid(void *, const char *, uid_t);
223 static struct archive_vtable *archive_write_disk_vtable(void);
225 static int _archive_write_close(struct archive *);
226 static int _archive_write_finish(struct archive *);
227 static int _archive_write_header(struct archive *, struct archive_entry *);
228 static int _archive_write_finish_entry(struct archive *);
229 static ssize_t _archive_write_data(struct archive *, const void *, size_t);
230 static ssize_t _archive_write_data_block(struct archive *, const void *, size_t, off_t);
232 static struct archive_vtable *
233 archive_write_disk_vtable(void)
235 static struct archive_vtable av;
236 static int inited = 0;
238 if (!inited) {
239 av.archive_write_close = _archive_write_close;
240 av.archive_write_finish = _archive_write_finish;
241 av.archive_write_header = _archive_write_header;
242 av.archive_write_finish_entry = _archive_write_finish_entry;
243 av.archive_write_data = _archive_write_data;
244 av.archive_write_data_block = _archive_write_data_block;
246 return (&av);
251 archive_write_disk_set_options(struct archive *_a, int flags)
253 struct archive_write_disk *a = (struct archive_write_disk *)_a;
255 a->flags = flags;
256 return (ARCHIVE_OK);
261 * Extract this entry to disk.
263 * TODO: Validate hardlinks. According to the standards, we're
264 * supposed to check each extracted hardlink and squawk if it refers
265 * to a file that we didn't restore. I'm not entirely convinced this
266 * is a good idea, but more importantly: Is there any way to validate
267 * hardlinks without keeping a complete list of filenames from the
268 * entire archive?? Ugh.
271 static int
272 _archive_write_header(struct archive *_a, struct archive_entry *entry)
274 struct archive_write_disk *a = (struct archive_write_disk *)_a;
275 struct fixup_entry *fe;
276 int ret, r;
278 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
279 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
280 "archive_write_disk_header");
281 archive_clear_error(&a->archive);
282 if (a->archive.state & ARCHIVE_STATE_DATA) {
283 r = _archive_write_finish_entry(&a->archive);
284 if (r != ARCHIVE_OK)
285 return (r);
288 /* Set up for this particular entry. */
289 a->pst = NULL;
290 a->current_fixup = NULL;
291 a->deferred = 0;
292 a->entry = entry;
293 a->fd = -1;
294 a->offset = 0;
295 a->uid = a->user_uid;
296 a->mode = archive_entry_mode(a->entry);
297 archive_strcpy(&(a->_name_data), archive_entry_pathname(a->entry));
298 a->name = a->_name_data.s;
299 archive_clear_error(&a->archive);
302 * Clean up the requested path. This is necessary for correct
303 * dir restores; the dir restore logic otherwise gets messed
304 * up by nonsense like "dir/.".
306 ret = cleanup_pathname(a);
307 if (ret != ARCHIVE_OK)
308 return (ret);
311 * Set the umask to zero so we get predictable mode settings.
312 * This gets done on every call to _write_header in case the
313 * user edits their umask during the extraction for some
314 * reason. This will be reset before we return. Note that we
315 * don't need to do this in _finish_entry, as the chmod(), etc,
316 * system calls don't obey umask.
318 a->user_umask = umask(0);
319 /* From here on, early exit requires "goto done" to clean up. */
321 /* Figure out what we need to do for this entry. */
322 a->todo = TODO_MODE_BASE;
323 if (a->flags & ARCHIVE_EXTRACT_PERM) {
324 a->todo |= TODO_MODE_FORCE; /* Be pushy about permissions. */
326 * SGID requires an extra "check" step because we
327 * cannot easily predict the GID that the system will
328 * assign. (Different systems assign GIDs to files
329 * based on a variety of criteria, including process
330 * credentials and the gid of the enclosing
331 * directory.) We can only restore the SGID bit if
332 * the file has the right GID, and we only know the
333 * GID if we either set it (see set_ownership) or if
334 * we've actually called stat() on the file after it
335 * was restored. Since there are several places at
336 * which we might verify the GID, we need a TODO bit
337 * to keep track.
339 if (a->mode & S_ISGID)
340 a->todo |= TODO_SGID | TODO_SGID_CHECK;
342 * Verifying the SUID is simpler, but can still be
343 * done in multiple ways, hence the separate "check" bit.
345 if (a->mode & S_ISUID)
346 a->todo |= TODO_SUID | TODO_SUID_CHECK;
347 } else {
349 * User didn't request full permissions, so don't
350 * restore SUID, SGID bits and obey umask.
352 a->mode &= ~S_ISUID;
353 a->mode &= ~S_ISGID;
354 a->mode &= ~S_ISVTX;
355 a->mode &= ~a->user_umask;
357 if (a->flags & ARCHIVE_EXTRACT_OWNER)
358 a->todo |= TODO_OWNER;
359 if (a->flags & ARCHIVE_EXTRACT_TIME)
360 a->todo |= TODO_TIMES;
361 if (a->flags & ARCHIVE_EXTRACT_ACL)
362 a->todo |= TODO_ACLS;
363 if (a->flags & ARCHIVE_EXTRACT_FFLAGS)
364 a->todo |= TODO_FFLAGS;
365 if (a->flags & ARCHIVE_EXTRACT_SECURE_SYMLINKS) {
366 ret = check_symlinks(a);
367 if (ret != ARCHIVE_OK)
368 goto done;
370 #ifdef HAVE_FCHDIR
371 /* If path exceeds PATH_MAX, shorten the path. */
372 edit_deep_directories(a);
373 #endif
375 ret = restore_entry(a);
377 #ifdef HAVE_FCHDIR
378 /* If we changed directory above, restore it here. */
379 if (a->restore_pwd >= 0) {
380 fchdir(a->restore_pwd);
381 close(a->restore_pwd);
382 a->restore_pwd = -1;
384 #endif
387 * Fixup uses the unedited pathname from archive_entry_pathname(),
388 * because it is relative to the base dir and the edited path
389 * might be relative to some intermediate dir as a result of the
390 * deep restore logic.
392 if (a->deferred & TODO_MODE) {
393 fe = current_fixup(a, archive_entry_pathname(entry));
394 fe->fixup |= TODO_MODE_BASE;
395 fe->mode = a->mode;
398 if (a->deferred & TODO_TIMES) {
399 fe = current_fixup(a, archive_entry_pathname(entry));
400 fe->fixup |= TODO_TIMES;
401 fe->mtime = archive_entry_mtime(entry);
402 fe->mtime_nanos = archive_entry_mtime_nsec(entry);
403 fe->atime = archive_entry_atime(entry);
404 fe->atime_nanos = archive_entry_atime_nsec(entry);
407 if (a->deferred & TODO_FFLAGS) {
408 fe = current_fixup(a, archive_entry_pathname(entry));
409 fe->fixup |= TODO_FFLAGS;
410 /* TODO: Complete this.. defer fflags from below. */
413 /* We've created the object and are ready to pour data into it. */
414 if (ret == ARCHIVE_OK)
415 a->archive.state = ARCHIVE_STATE_DATA;
416 done:
417 /* Restore the user's umask before returning. */
418 umask(a->user_umask);
420 return (ret);
424 archive_write_disk_set_skip_file(struct archive *_a, dev_t d, ino_t i)
426 struct archive_write_disk *a = (struct archive_write_disk *)_a;
427 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
428 ARCHIVE_STATE_ANY, "archive_write_disk_set_skip_file");
429 a->skip_file_dev = d;
430 a->skip_file_ino = i;
431 return (ARCHIVE_OK);
434 static ssize_t
435 _archive_write_data_block(struct archive *_a,
436 const void *buff, size_t size, off_t offset)
438 struct archive_write_disk *a = (struct archive_write_disk *)_a;
439 ssize_t bytes_written = 0;
441 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
442 ARCHIVE_STATE_DATA, "archive_write_disk_block");
443 if (a->fd < 0)
444 return (ARCHIVE_OK);
445 archive_clear_error(&a->archive);
447 /* Seek if necessary to the specified offset. */
448 if (offset != a->offset) {
449 if (lseek(a->fd, offset, SEEK_SET) < 0) {
450 archive_set_error(&a->archive, errno, "Seek failed");
451 return (ARCHIVE_WARN);
453 a->offset = offset;
456 /* Write the data. */
457 while (size > 0) {
458 bytes_written = write(a->fd, buff, size);
459 if (bytes_written < 0) {
460 archive_set_error(&a->archive, errno, "Write failed");
461 return (ARCHIVE_WARN);
463 size -= bytes_written;
464 a->offset += bytes_written;
466 return (ARCHIVE_OK);
469 static ssize_t
470 _archive_write_data(struct archive *_a, const void *buff, size_t size)
472 struct archive_write_disk *a = (struct archive_write_disk *)_a;
473 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
474 ARCHIVE_STATE_DATA, "archive_write_data");
475 if (a->fd < 0)
476 return (ARCHIVE_OK);
478 return (_archive_write_data_block(_a, buff, size, a->offset));
481 static int
482 _archive_write_finish_entry(struct archive *_a)
484 struct archive_write_disk *a = (struct archive_write_disk *)_a;
485 int ret = ARCHIVE_OK;
487 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
488 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
489 "archive_write_finish_entry");
490 if (a->archive.state & ARCHIVE_STATE_HEADER)
491 return (ARCHIVE_OK);
492 archive_clear_error(&a->archive);
494 /* Restore metadata. */
497 * Look up the "real" UID only if we're going to need it. We
498 * need this for TODO_SGID because chown() requires both.
500 if (a->todo & (TODO_OWNER | TODO_SUID | TODO_SGID)) {
501 a->uid = a->lookup_uid(a->lookup_uid_data,
502 archive_entry_uname(a->entry),
503 archive_entry_uid(a->entry));
505 /* Look up the "real" GID only if we're going to need it. */
506 if (a->todo & (TODO_OWNER | TODO_SGID | TODO_SUID)) {
507 a->gid = a->lookup_gid(a->lookup_gid_data,
508 archive_entry_gname(a->entry),
509 archive_entry_gid(a->entry));
512 * If restoring ownership, do it before trying to restore suid/sgid
513 * bits. If we set the owner, we know what it is and can skip
514 * a stat() call to examine the ownership of the file on disk.
516 if (a->todo & TODO_OWNER)
517 ret = set_ownership(a);
518 if (a->todo & TODO_MODE) {
519 int r2 = set_mode(a, a->mode);
520 if (r2 < ret) ret = r2;
522 if (a->todo & TODO_TIMES) {
523 int r2 = set_time(a);
524 if (r2 < ret) ret = r2;
526 if (a->todo & TODO_ACLS) {
527 int r2 = set_acls(a);
528 if (r2 < ret) ret = r2;
530 if (a->todo & TODO_XATTR) {
531 int r2 = set_xattrs(a);
532 if (r2 < ret) ret = r2;
534 if (a->todo & TODO_FFLAGS) {
535 int r2 = set_fflags(a);
536 if (r2 < ret) ret = r2;
539 /* If there's an fd, we can close it now. */
540 if (a->fd >= 0) {
541 close(a->fd);
542 a->fd = -1;
544 a->archive.state = ARCHIVE_STATE_HEADER;
545 return (ret);
549 archive_write_disk_set_group_lookup(struct archive *_a,
550 void *private_data,
551 gid_t (*lookup_gid)(void *private, const char *gname, gid_t gid),
552 void (*cleanup_gid)(void *private))
554 struct archive_write_disk *a = (struct archive_write_disk *)_a;
555 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
556 ARCHIVE_STATE_ANY, "archive_write_disk_set_group_lookup");
558 a->lookup_gid = lookup_gid;
559 a->cleanup_gid = cleanup_gid;
560 a->lookup_gid_data = private_data;
561 return (ARCHIVE_OK);
565 archive_write_disk_set_user_lookup(struct archive *_a,
566 void *private_data,
567 uid_t (*lookup_uid)(void *private, const char *uname, uid_t uid),
568 void (*cleanup_uid)(void *private))
570 struct archive_write_disk *a = (struct archive_write_disk *)_a;
571 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
572 ARCHIVE_STATE_ANY, "archive_write_disk_set_user_lookup");
574 a->lookup_uid = lookup_uid;
575 a->cleanup_uid = cleanup_uid;
576 a->lookup_uid_data = private_data;
577 return (ARCHIVE_OK);
582 * Create a new archive_write_disk object and initialize it with global state.
584 struct archive *
585 archive_write_disk_new(void)
587 struct archive_write_disk *a;
589 a = (struct archive_write_disk *)malloc(sizeof(*a));
590 if (a == NULL)
591 return (NULL);
592 memset(a, 0, sizeof(*a));
593 a->archive.magic = ARCHIVE_WRITE_DISK_MAGIC;
594 /* We're ready to write a header immediately. */
595 a->archive.state = ARCHIVE_STATE_HEADER;
596 a->archive.vtable = archive_write_disk_vtable();
597 a->lookup_uid = trivial_lookup_uid;
598 a->lookup_gid = trivial_lookup_gid;
599 a->user_uid = geteuid();
600 archive_string_ensure(&a->path_safe, 64);
601 return (&a->archive);
606 * If pathname is longer than PATH_MAX, chdir to a suitable
607 * intermediate dir and edit the path down to a shorter suffix. Note
608 * that this routine never returns an error; if the chdir() attempt
609 * fails for any reason, we just go ahead with the long pathname. The
610 * object creation is likely to fail, but any error will get handled
611 * at that time.
613 #ifdef HAVE_FCHDIR
614 static void
615 edit_deep_directories(struct archive_write_disk *a)
617 int ret;
618 char *tail = a->name;
620 a->restore_pwd = -1;
622 /* If path is short, avoid the open() below. */
623 if (strlen(tail) <= PATH_MAX)
624 return;
626 /* Try to record our starting dir. */
627 a->restore_pwd = open(".", O_RDONLY);
628 if (a->restore_pwd < 0)
629 return;
631 /* As long as the path is too long... */
632 while (strlen(tail) > PATH_MAX) {
633 /* Locate a dir prefix shorter than PATH_MAX. */
634 tail += PATH_MAX - 8;
635 while (tail > a->name && *tail != '/')
636 tail--;
637 /* Exit if we find a too-long path component. */
638 if (tail <= a->name)
639 return;
640 /* Create the intermediate dir and chdir to it. */
641 *tail = '\0'; /* Terminate dir portion */
642 ret = create_dir(a, a->name);
643 if (ret == ARCHIVE_OK && chdir(a->name) != 0)
644 ret = ARCHIVE_WARN;
645 *tail = '/'; /* Restore the / we removed. */
646 if (ret != ARCHIVE_OK)
647 return;
648 tail++;
649 /* The chdir() succeeded; we've now shortened the path. */
650 a->name = tail;
652 return;
654 #endif
657 * The main restore function.
659 static int
660 restore_entry(struct archive_write_disk *a)
662 int ret = ARCHIVE_OK, en;
664 if (a->flags & ARCHIVE_EXTRACT_UNLINK && !S_ISDIR(a->mode)) {
665 if (unlink(a->name) == 0) {
666 /* We removed it, we're done. */
667 } else if (errno == ENOENT) {
668 /* File didn't exist, that's just as good. */
669 } else if (rmdir(a->name) == 0) {
670 /* It was a dir, but now it's gone. */
671 } else {
672 /* We tried, but couldn't get rid of it. */
673 archive_set_error(&a->archive, errno,
674 "Could not unlink");
675 return(ARCHIVE_WARN);
679 /* Try creating it first; if this fails, we'll try to recover. */
680 en = create_filesystem_object(a);
682 if (en == ENOTDIR || en == ENOENT) {
683 /* If the parent dir doesn't exist, try creating it. */
684 create_parent_dir(a, a->name);
685 /* Now try to create the object again. */
686 en = create_filesystem_object(a);
689 if (en == EEXIST) {
690 /* If we're not overwriting, we're done. */
691 if (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE) {
692 archive_set_error(&a->archive, en, "Already exists");
693 return (ARCHIVE_WARN);
696 /* Find out what's in the way before we go any further. */
697 if (lstat(a->name, &a->st) != 0) {
698 archive_set_error(&a->archive, errno,
699 "Can't stat existing object");
700 return (ARCHIVE_WARN);
703 /* TODO: if it's a symlink... */
705 /* If it's our archive, we're done. */
706 if (a->skip_file_dev > 0 &&
707 a->skip_file_ino > 0 &&
708 a->st.st_dev == a->skip_file_dev &&
709 a->st.st_ino == a->skip_file_ino) {
710 archive_set_error(&a->archive, 0, "Refusing to overwrite archive");
711 return (ARCHIVE_FAILED);
714 if (!S_ISDIR(a->st.st_mode)) {
715 /* A non-dir is in the way, unlink it. */
716 if (unlink(a->name) != 0) {
717 archive_set_error(&a->archive, errno,
718 "Can't unlink already-existing object");
719 return (ARCHIVE_WARN);
721 /* Try again. */
722 en = create_filesystem_object(a);
723 } else if (!S_ISDIR(a->mode)) {
724 /* A dir is in the way of a non-dir, rmdir it. */
725 if (rmdir(a->name) != 0) {
726 archive_set_error(&a->archive, errno,
727 "Can't remove already-existing dir");
728 return (ARCHIVE_WARN);
730 /* Try again. */
731 en = create_filesystem_object(a);
732 } else {
734 * There's a dir in the way of a dir. Don't
735 * waste time with rmdir()/mkdir(), just fix
736 * up the permissions on the existing dir.
737 * Note that we don't change perms on existing
738 * dirs unless _EXTRACT_PERM is specified.
740 if ((a->mode != a->st.st_mode)
741 && (a->todo & TODO_MODE_FORCE))
742 a->deferred |= (a->todo & TODO_MODE);
743 /* Ownership doesn't need deferred fixup. */
744 en = 0; /* Forget the EEXIST. */
748 if (en) {
749 /* Everything failed; give up here. */
750 archive_set_error(&a->archive, en, "Can't create '%s'", a->name);
751 return (ARCHIVE_WARN);
754 a->pst = NULL; /* Cached stat data no longer valid. */
755 return (ret);
759 * Returns 0 if creation succeeds, or else returns errno value from
760 * the failed system call. Note: This function should only ever perform
761 * a single system call.
764 create_filesystem_object(struct archive_write_disk *a)
766 /* Create the entry. */
767 const char *linkname;
768 mode_t final_mode, mode;
769 int r;
771 /* We identify hard/symlinks according to the link names. */
772 /* Since link(2) and symlink(2) don't handle modes, we're done here. */
773 linkname = archive_entry_hardlink(a->entry);
774 if (linkname != NULL)
775 return link(linkname, a->name) ? errno : 0;
776 linkname = archive_entry_symlink(a->entry);
777 if (linkname != NULL)
778 return symlink(linkname, a->name) ? errno : 0;
781 * The remaining system calls all set permissions, so let's
782 * try to take advantage of that to avoid an extra chmod()
783 * call. (Recall that umask is set to zero right now!)
786 /* Mode we want for the final restored object (w/o file type bits). */
787 final_mode = a->mode & 07777;
789 * The mode that will actually be restored in this step. Note
790 * that SUID, SGID, etc, require additional work to ensure
791 * security, so we never restore them at this point.
793 mode = final_mode & 0777;
795 switch (a->mode & S_IFMT) {
796 default:
797 /* Fall through, as required by POSIX. */
798 case S_IFREG:
799 a->fd = open(a->name,
800 O_WRONLY | O_CREAT | O_EXCL, mode);
801 r = (a->fd < 0);
802 break;
803 case S_IFCHR:
804 r = mknod(a->name, mode | S_IFCHR,
805 archive_entry_rdev(a->entry));
806 break;
807 case S_IFBLK:
808 r = mknod(a->name, mode | S_IFBLK,
809 archive_entry_rdev(a->entry));
810 break;
811 case S_IFDIR:
812 mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE;
813 r = mkdir(a->name, mode);
814 if (r == 0) {
815 /* Defer setting dir times. */
816 a->deferred |= (a->todo & TODO_TIMES);
817 a->todo &= ~TODO_TIMES;
818 /* Never use an immediate chmod(). */
819 if (mode != final_mode)
820 a->deferred |= (a->todo & TODO_MODE);
821 a->todo &= ~TODO_MODE;
823 break;
824 case S_IFIFO:
825 r = mkfifo(a->name, mode);
826 break;
829 /* All the system calls above set errno on failure. */
830 if (r)
831 return (errno);
833 /* If we managed to set the final mode, we've avoided a chmod(). */
834 if (mode == final_mode)
835 a->todo &= ~TODO_MODE;
836 return (0);
840 * Cleanup function for archive_extract. Mostly, this involves processing
841 * the fixup list, which is used to address a number of problems:
842 * * Dir permissions might prevent us from restoring a file in that
843 * dir, so we restore the dir with minimum 0700 permissions first,
844 * then correct the mode at the end.
845 * * Similarly, the act of restoring a file touches the directory
846 * and changes the timestamp on the dir, so we have to touch-up dir
847 * timestamps at the end as well.
848 * * Some file flags can interfere with the restore by, for example,
849 * preventing the creation of hardlinks to those files.
851 * Note that tar/cpio do not require that archives be in a particular
852 * order; there is no way to know when the last file has been restored
853 * within a directory, so there's no way to optimize the memory usage
854 * here by fixing up the directory any earlier than the
855 * end-of-archive.
857 * XXX TODO: Directory ACLs should be restored here, for the same
858 * reason we set directory perms here. XXX
860 static int
861 _archive_write_close(struct archive *_a)
863 struct archive_write_disk *a = (struct archive_write_disk *)_a;
864 struct fixup_entry *next, *p;
865 int ret;
867 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
868 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
869 "archive_write_disk_close");
870 ret = _archive_write_finish_entry(&a->archive);
872 /* Sort dir list so directories are fixed up in depth-first order. */
873 p = sort_dir_list(a->fixup_list);
875 while (p != NULL) {
876 a->pst = NULL; /* Mark stat cache as out-of-date. */
877 if (p->fixup & TODO_TIMES) {
878 struct timeval times[2];
879 times[1].tv_sec = p->mtime;
880 times[1].tv_usec = p->mtime_nanos / 1000;
881 times[0].tv_sec = p->atime;
882 times[0].tv_usec = p->atime_nanos / 1000;
883 #ifdef HAVE_LUTIMES
884 lutimes(p->name, times);
885 #else
886 utimes(p->name, times);
887 #endif
889 if (p->fixup & TODO_MODE_BASE)
890 chmod(p->name, p->mode);
892 if (p->fixup & TODO_FFLAGS)
893 set_fflags_platform(a, -1, p->name,
894 p->mode, p->fflags_set, 0);
896 next = p->next;
897 free(p->name);
898 free(p);
899 p = next;
901 a->fixup_list = NULL;
902 return (ret);
905 static int
906 _archive_write_finish(struct archive *_a)
908 struct archive_write_disk *a = (struct archive_write_disk *)_a;
909 int ret;
910 ret = _archive_write_close(&a->archive);
911 archive_string_free(&a->_name_data);
912 archive_string_free(&a->archive.error_string);
913 archive_string_free(&a->path_safe);
914 free(a);
915 return (ret);
919 * Simple O(n log n) merge sort to order the fixup list. In
920 * particular, we want to restore dir timestamps depth-first.
922 static struct fixup_entry *
923 sort_dir_list(struct fixup_entry *p)
925 struct fixup_entry *a, *b, *t;
927 if (p == NULL)
928 return (NULL);
929 /* A one-item list is already sorted. */
930 if (p->next == NULL)
931 return (p);
933 /* Step 1: split the list. */
934 t = p;
935 a = p->next->next;
936 while (a != NULL) {
937 /* Step a twice, t once. */
938 a = a->next;
939 if (a != NULL)
940 a = a->next;
941 t = t->next;
943 /* Now, t is at the mid-point, so break the list here. */
944 b = t->next;
945 t->next = NULL;
946 a = p;
948 /* Step 2: Recursively sort the two sub-lists. */
949 a = sort_dir_list(a);
950 b = sort_dir_list(b);
952 /* Step 3: Merge the returned lists. */
953 /* Pick the first element for the merged list. */
954 if (strcmp(a->name, b->name) > 0) {
955 t = p = a;
956 a = a->next;
957 } else {
958 t = p = b;
959 b = b->next;
962 /* Always put the later element on the list first. */
963 while (a != NULL && b != NULL) {
964 if (strcmp(a->name, b->name) > 0) {
965 t->next = a;
966 a = a->next;
967 } else {
968 t->next = b;
969 b = b->next;
971 t = t->next;
974 /* Only one list is non-empty, so just splice it on. */
975 if (a != NULL)
976 t->next = a;
977 if (b != NULL)
978 t->next = b;
980 return (p);
984 * Returns a new, initialized fixup entry.
986 * TODO: Reduce the memory requirements for this list by using a tree
987 * structure rather than a simple list of names.
989 static struct fixup_entry *
990 new_fixup(struct archive_write_disk *a, const char *pathname)
992 struct fixup_entry *fe;
994 fe = (struct fixup_entry *)malloc(sizeof(struct fixup_entry));
995 if (fe == NULL)
996 return (NULL);
997 fe->next = a->fixup_list;
998 a->fixup_list = fe;
999 fe->fixup = 0;
1000 fe->name = strdup(pathname);
1001 return (fe);
1005 * Returns a fixup structure for the current entry.
1007 static struct fixup_entry *
1008 current_fixup(struct archive_write_disk *a, const char *pathname)
1010 if (a->current_fixup == NULL)
1011 a->current_fixup = new_fixup(a, pathname);
1012 return (a->current_fixup);
1015 /* TODO: Make this work. */
1017 * TODO: The deep-directory support bypasses this; disable deep directory
1018 * support if we're doing symlink checks.
1021 * TODO: Someday, integrate this with the deep dir support; they both
1022 * scan the path and both can be optimized by comparing against other
1023 * recent paths.
1025 static int
1026 check_symlinks(struct archive_write_disk *a)
1028 char *pn, *p;
1029 char c;
1030 int r;
1031 struct stat st;
1034 * Gaurd against symlink tricks. Reject any archive entry whose
1035 * destination would be altered by a symlink.
1037 /* Whatever we checked last time doesn't need to be re-checked. */
1038 pn = a->name;
1039 p = a->path_safe.s;
1040 while ((*pn != '\0') && (*p == *pn))
1041 ++p, ++pn;
1042 c = pn[0];
1043 /* Keep going until we've checked the entire name. */
1044 while (pn[0] != '\0' && (pn[0] != '/' || pn[1] != '\0')) {
1045 /* Skip the next path element. */
1046 while (*pn != '\0' && *pn != '/')
1047 ++pn;
1048 c = pn[0];
1049 pn[0] = '\0';
1050 /* Check that we haven't hit a symlink. */
1051 r = lstat(a->name, &st);
1052 if (r != 0) {
1053 /* We've hit a dir that doesn't exist; stop now. */
1054 if (errno == ENOENT)
1055 break;
1056 } else if (S_ISLNK(st.st_mode)) {
1057 if (c == '\0') {
1059 * Last element is symlink; remove it
1060 * so we can overwrite it with the
1061 * item being extracted.
1063 if (unlink(a->name)) {
1064 archive_set_error(&a->archive, errno,
1065 "Could not remove symlink %s",
1066 a->name);
1067 pn[0] = c;
1068 return (ARCHIVE_WARN);
1071 * Even if we did remove it, a warning
1072 * is in order. The warning is silly,
1073 * though, if we're just replacing one
1074 * symlink with another symlink.
1076 if (!S_ISLNK(a->mode)) {
1077 archive_set_error(&a->archive, 0,
1078 "Removing symlink %s",
1079 a->name);
1081 /* Symlink gone. No more problem! */
1082 pn[0] = c;
1083 return (0);
1084 } else if (a->flags & ARCHIVE_EXTRACT_UNLINK) {
1085 /* User asked us to remove problems. */
1086 if (unlink(a->name) != 0) {
1087 archive_set_error(&a->archive, 0,
1088 "Cannot remove intervening symlink %s",
1089 a->name);
1090 pn[0] = c;
1091 return (ARCHIVE_WARN);
1093 } else {
1094 archive_set_error(&a->archive, 0,
1095 "Cannot extract through symlink %s",
1096 a->name);
1097 pn[0] = c;
1098 return (ARCHIVE_WARN);
1102 pn[0] = c;
1103 /* We've checked and/or cleaned the whole path, so remember it. */
1104 archive_strcpy(&a->path_safe, a->name);
1105 return (ARCHIVE_OK);
1109 * Canonicalize the pathname. In particular, this strips duplicate
1110 * '/' characters, '.' elements, and trailing '/'. It also raises an
1111 * error for an empty path, a trailing '..' or (if _SECURE_NODOTDOT is
1112 * set) any '..' in the path.
1114 static int
1115 cleanup_pathname(struct archive_write_disk *a)
1117 char *dest, *src;
1118 char separator = '\0';
1119 int lastdotdot = 0; /* True if last elt copied was '..' */
1121 dest = src = a->name;
1122 if (*src == '\0') {
1123 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1124 "Invalid empty pathname");
1125 return (ARCHIVE_WARN);
1128 /* Skip leading '/'. */
1129 if (*src == '/')
1130 separator = *src++;
1132 /* Scan the pathname one element at a time. */
1133 for (;;) {
1134 /* src points to first char after '/' */
1135 if (src[0] == '\0') {
1136 break;
1137 } else if (src[0] == '/') {
1138 /* Found '//', ignore second one. */
1139 src++;
1140 continue;
1141 } else if (src[0] == '.') {
1142 if (src[1] == '\0') {
1143 /* Ignore trailing '.' */
1144 break;
1145 } else if (src[1] == '/') {
1146 /* Skip './'. */
1147 src += 2;
1148 continue;
1149 } else if (src[1] == '.') {
1150 if (src[2] == '/' || src[2] == '\0') {
1151 /* Conditionally warn about '..' */
1152 if (a->flags & ARCHIVE_EXTRACT_SECURE_NODOTDOT) {
1153 archive_set_error(&a->archive,
1154 ARCHIVE_ERRNO_MISC,
1155 "Path contains '..'");
1156 return (ARCHIVE_WARN);
1158 lastdotdot = 1;
1159 } else
1160 lastdotdot = 0;
1162 * Note: Under no circumstances do we
1163 * remove '..' elements. In
1164 * particular, restoring
1165 * '/foo/../bar/' should create the
1166 * 'foo' dir as a side-effect.
1168 } else
1169 lastdotdot = 0;
1170 } else
1171 lastdotdot = 0;
1173 /* Copy current element, including leading '/'. */
1174 if (separator)
1175 *dest++ = '/';
1176 while (*src != '\0' && *src != '/') {
1177 *dest++ = *src++;
1180 if (*src == '\0')
1181 break;
1183 /* Skip '/' separator. */
1184 separator = *src++;
1187 * We've just copied zero or more path elements, not including the
1188 * final '/'.
1190 if (lastdotdot) {
1191 /* Trailing '..' is always wrong. */
1192 archive_set_error(&a->archive,
1193 ARCHIVE_ERRNO_MISC,
1194 "Path contains trailing '..'");
1195 return (ARCHIVE_WARN);
1197 if (dest == a->name) {
1199 * Nothing got copied. The path must have been something
1200 * like '.' or '/' or './' or '/././././/./'.
1202 if (separator)
1203 *dest++ = '/';
1204 else
1205 *dest++ = '.';
1207 /* Terminate the result. */
1208 *dest = '\0';
1209 return (ARCHIVE_OK);
1213 * Create the parent directory of the specified path, assuming path
1214 * is already in mutable storage.
1216 static int
1217 create_parent_dir(struct archive_write_disk *a, char *path)
1219 char *slash;
1220 int r;
1222 /* Remove tail element to obtain parent name. */
1223 slash = strrchr(path, '/');
1224 if (slash == NULL)
1225 return (ARCHIVE_OK);
1226 *slash = '\0';
1227 r = create_dir(a, path);
1228 *slash = '/';
1229 return (r);
1233 * Create the specified dir, recursing to create parents as necessary.
1235 * Returns ARCHIVE_OK if the path exists when we're done here.
1236 * Otherwise, returns ARCHIVE_WARN.
1237 * Assumes path is in mutable storage; path is unchanged on exit.
1239 static int
1240 create_dir(struct archive_write_disk *a, char *path)
1242 struct stat st;
1243 struct fixup_entry *le;
1244 char *slash, *base;
1245 mode_t mode_final, mode;
1246 int r;
1248 r = ARCHIVE_OK;
1250 /* Check for special names and just skip them. */
1251 slash = strrchr(path, '/');
1252 base = strrchr(path, '/');
1253 if (slash == NULL)
1254 base = path;
1255 else
1256 base = slash + 1;
1258 if (base[0] == '\0' ||
1259 (base[0] == '.' && base[1] == '\0') ||
1260 (base[0] == '.' && base[1] == '.' && base[2] == '\0')) {
1261 /* Don't bother trying to create null path, '.', or '..'. */
1262 if (slash != NULL) {
1263 *slash = '\0';
1264 r = create_dir(a, path);
1265 *slash = '/';
1266 return (r);
1268 return (ARCHIVE_OK);
1272 * Yes, this should be stat() and not lstat(). Using lstat()
1273 * here loses the ability to extract through symlinks. Also note
1274 * that this should not use the a->st cache.
1276 if (stat(path, &st) == 0) {
1277 if (S_ISDIR(st.st_mode))
1278 return (ARCHIVE_OK);
1279 if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
1280 archive_set_error(&a->archive, EEXIST,
1281 "Can't create directory '%s'", path);
1282 return (ARCHIVE_WARN);
1284 if (unlink(path) != 0) {
1285 archive_set_error(&a->archive, errno,
1286 "Can't create directory '%s': "
1287 "Conflicting file cannot be removed");
1288 return (ARCHIVE_WARN);
1290 } else if (errno != ENOENT && errno != ENOTDIR) {
1291 /* Stat failed? */
1292 archive_set_error(&a->archive, errno, "Can't test directory '%s'", path);
1293 return (ARCHIVE_WARN);
1294 } else if (slash != NULL) {
1295 *slash = '\0';
1296 r = create_dir(a, path);
1297 *slash = '/';
1298 if (r != ARCHIVE_OK)
1299 return (r);
1303 * Mode we want for the final restored directory. Per POSIX,
1304 * implicitly-created dirs must be created obeying the umask.
1305 * There's no mention whether this is different for privileged
1306 * restores (which the rest of this code handles by pretending
1307 * umask=0). I've chosen here to always obey the user's umask for
1308 * implicit dirs, even if _EXTRACT_PERM was specified.
1310 mode_final = DEFAULT_DIR_MODE & ~a->user_umask;
1311 /* Mode we want on disk during the restore process. */
1312 mode = mode_final;
1313 mode |= MINIMUM_DIR_MODE;
1314 mode &= MAXIMUM_DIR_MODE;
1315 if (mkdir(path, mode) == 0) {
1316 if (mode != mode_final) {
1317 le = new_fixup(a, path);
1318 le->fixup |=TODO_MODE_BASE;
1319 le->mode = mode_final;
1321 return (ARCHIVE_OK);
1325 * Without the following check, a/b/../b/c/d fails at the
1326 * second visit to 'b', so 'd' can't be created. Note that we
1327 * don't add it to the fixup list here, as it's already been
1328 * added.
1330 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1331 return (ARCHIVE_OK);
1333 archive_set_error(&a->archive, errno, "Failed to create dir '%s'", path);
1334 return (ARCHIVE_WARN);
1338 * Note: Although we can skip setting the user id if the desired user
1339 * id matches the current user, we cannot skip setting the group, as
1340 * many systems set the gid bit based on the containing directory. So
1341 * we have to perform a chown syscall if we want to restore the SGID
1342 * bit. (The alternative is to stat() and then possibly chown(); it's
1343 * more efficient to skip the stat() and just always chown().) Note
1344 * that a successful chown() here clears the TODO_SGID_CHECK bit, which
1345 * allows set_mode to skip the stat() check for the GID.
1347 static int
1348 set_ownership(struct archive_write_disk *a)
1350 /* If we know we can't change it, don't bother trying. */
1351 if (a->user_uid != 0 && a->user_uid != a->uid) {
1352 archive_set_error(&a->archive, errno,
1353 "Can't set UID=%d", a->uid);
1354 return (ARCHIVE_WARN);
1357 #ifdef HAVE_FCHOWN
1358 if (a->fd >= 0 && fchown(a->fd, a->uid, a->gid) == 0)
1359 goto success;
1360 #endif
1362 #ifdef HAVE_LCHOWN
1363 if (lchown(a->name, a->uid, a->gid) == 0)
1364 goto success;
1365 #else
1366 if (!S_ISLNK(a->mode) && chown(a->name, a->uid, a->gid) == 0)
1367 goto success;
1368 #endif
1370 archive_set_error(&a->archive, errno,
1371 "Can't set user=%d/group=%d for %s", a->uid, a->gid,
1372 a->name);
1373 return (ARCHIVE_WARN);
1374 success:
1375 a->todo &= ~TODO_OWNER;
1376 /* We know the user/group are correct now. */
1377 a->todo &= ~TODO_SGID_CHECK;
1378 a->todo &= ~TODO_SUID_CHECK;
1379 return (ARCHIVE_OK);
1382 static int
1383 set_time(struct archive_write_disk *a)
1385 const struct stat *st;
1386 struct timeval times[2];
1388 st = archive_entry_stat(a->entry);
1390 times[1].tv_sec = st->st_mtime;
1391 times[1].tv_usec = ARCHIVE_STAT_MTIME_NANOS(st) / 1000;
1393 times[0].tv_sec = st->st_atime;
1394 times[0].tv_usec = ARCHIVE_STAT_ATIME_NANOS(st) / 1000;
1396 #ifdef HAVE_FUTIMES
1397 if (a->fd >= 0 && futimes(a->fd, times) == 0) {
1398 return (ARCHIVE_OK);
1400 #endif
1402 #ifdef HAVE_LUTIMES
1403 if (lutimes(a->name, times) != 0)
1404 #else
1405 if (!S_ISLNK(a->mode) && utimes(a->name, times) != 0)
1406 #endif
1408 archive_set_error(&a->archive, errno, "Can't update time for %s",
1409 a->name);
1410 return (ARCHIVE_WARN);
1414 * Note: POSIX does not provide a portable way to restore ctime.
1415 * (Apart from resetting the system clock, which is distasteful.)
1416 * So, any restoration of ctime will necessarily be OS-specific.
1419 /* XXX TODO: Can FreeBSD restore ctime? XXX */
1420 return (ARCHIVE_OK);
1423 static int
1424 set_mode(struct archive_write_disk *a, int mode)
1426 int r = ARCHIVE_OK;
1428 if (a->todo & TODO_SGID_CHECK) {
1430 * If we don't know the GID is right, we must stat()
1431 * to verify it. We can't just check the GID of this
1432 * process, since systems sometimes set GID from
1433 * the enclosing dir or based on ACLs.
1435 if (a->pst != NULL) {
1436 /* Already have stat() data available. */
1437 #ifdef HAVE_FSTAT
1438 } else if (fd >= 0 && fstat(fd, &a->st) == 0) {
1439 a->pst = &a->st;
1440 #endif
1441 } else if (stat(a->name, &a->st) == 0) {
1442 a->pst = &a->st;
1443 } else {
1444 archive_set_error(&a->archive, errno,
1445 "Couldn't stat file");
1446 return (ARCHIVE_WARN);
1448 if (a->pst->st_gid != a->gid) {
1449 mode &= ~ S_ISGID;
1450 archive_set_error(&a->archive, -1, "Can't restore SGID bit");
1451 r = ARCHIVE_WARN;
1453 /* While we're here, double-check the UID. */
1454 if (a->pst->st_uid != a->uid
1455 && (a->todo & TODO_SUID)) {
1456 mode &= ~ S_ISUID;
1457 archive_set_error(&a->archive, -1, "Can't restore SUID bit");
1458 r = ARCHIVE_WARN;
1460 a->todo &= ~TODO_SGID_CHECK;
1461 a->todo &= ~TODO_SUID_CHECK;
1462 } else if (a->todo & TODO_SUID_CHECK) {
1464 * If we don't know the UID is right, we can just check
1465 * the user, since all systems set the file UID from
1466 * the process UID.
1468 if (a->user_uid != a->uid) {
1469 mode &= ~ S_ISUID;
1470 archive_set_error(&a->archive, -1, "Can't make file SUID");
1471 r = ARCHIVE_WARN;
1473 a->todo &= ~TODO_SUID_CHECK;
1476 if (S_ISLNK(a->mode)) {
1477 #ifdef HAVE_LCHMOD
1479 * If this is a symlink, use lchmod(). If the
1480 * platform doesn't support lchmod(), just skip it. A
1481 * platform that doesn't provide a way to set
1482 * permissions on symlinks probably ignores
1483 * permissions on symlinks, so a failure here has no
1484 * impact.
1486 if (lchmod(a->name, mode) != 0) {
1487 archive_set_error(&a->archive, errno, "Can't set permissions");
1488 r = ARCHIVE_WARN;
1490 #endif
1491 } else if (!S_ISDIR(a->mode)) {
1493 * If it's not a symlink and not a dir, then use
1494 * fchmod() or chmod(), depending on whether we have
1495 * an fd. Dirs get their perms set during the
1496 * post-extract fixup, which is handled elsewhere.
1498 #ifdef HAVE_FCHMOD
1499 if (a->fd >= 0) {
1500 if (fchmod(a->fd, mode) != 0) {
1501 archive_set_error(&a->archive, errno,
1502 "Can't set permissions");
1503 r = ARCHIVE_WARN;
1505 } else
1506 #endif
1507 /* If this platform lacks fchmod(), then
1508 * we'll just use chmod(). */
1509 if (chmod(a->name, mode) != 0) {
1510 archive_set_error(&a->archive, errno,
1511 "Can't set permissions");
1512 r = ARCHIVE_WARN;
1515 return (r);
1518 static int
1519 set_fflags(struct archive_write_disk *a)
1521 struct fixup_entry *le;
1522 unsigned long set, clear;
1523 int r;
1524 int critical_flags;
1525 mode_t mode = archive_entry_mode(a->entry);
1528 * Make 'critical_flags' hold all file flags that can't be
1529 * immediately restored. For example, on BSD systems,
1530 * SF_IMMUTABLE prevents hardlinks from being created, so
1531 * should not be set until after any hardlinks are created. To
1532 * preserve some semblance of portability, this uses #ifdef
1533 * extensively. Ugly, but it works.
1535 * Yes, Virginia, this does create a security race. It's mitigated
1536 * somewhat by the practice of creating dirs 0700 until the extract
1537 * is done, but it would be nice if we could do more than that.
1538 * People restoring critical file systems should be wary of
1539 * other programs that might try to muck with files as they're
1540 * being restored.
1542 /* Hopefully, the compiler will optimize this mess into a constant. */
1543 critical_flags = 0;
1544 #ifdef SF_IMMUTABLE
1545 critical_flags |= SF_IMMUTABLE;
1546 #endif
1547 #ifdef UF_IMMUTABLE
1548 critical_flags |= UF_IMMUTABLE;
1549 #endif
1550 #ifdef SF_APPEND
1551 critical_flags |= SF_APPEND;
1552 #endif
1553 #ifdef UF_APPEND
1554 critical_flags |= UF_APPEND;
1555 #endif
1556 #ifdef EXT2_APPEND_FL
1557 critical_flags |= EXT2_APPEND_FL;
1558 #endif
1559 #ifdef EXT2_IMMUTABLE_FL
1560 critical_flags |= EXT2_IMMUTABLE_FL;
1561 #endif
1563 if (a->todo & TODO_FFLAGS) {
1564 archive_entry_fflags(a->entry, &set, &clear);
1567 * The first test encourages the compiler to eliminate
1568 * all of this if it's not necessary.
1570 if ((critical_flags != 0) && (set & critical_flags)) {
1571 le = current_fixup(a, a->name);
1572 le->fixup |= TODO_FFLAGS;
1573 le->fflags_set = set;
1574 /* Store the mode if it's not already there. */
1575 if ((le->fixup & TODO_MODE) == 0)
1576 le->mode = mode;
1577 } else {
1578 r = set_fflags_platform(a, a->fd,
1579 a->name, mode, set, clear);
1580 if (r != ARCHIVE_OK)
1581 return (r);
1584 return (ARCHIVE_OK);
1588 #if ( defined(HAVE_LCHFLAGS) || defined(HAVE_CHFLAGS) || defined(HAVE_FCHFLAGS) ) && !defined(__linux)
1589 static int
1590 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
1591 mode_t mode, unsigned long set, unsigned long clear)
1593 (void)mode; /* UNUSED */
1594 if (set == 0 && clear == 0)
1595 return (ARCHIVE_OK);
1598 * XXX Is the stat here really necessary? Or can I just use
1599 * the 'set' flags directly? In particular, I'm not sure
1600 * about the correct approach if we're overwriting an existing
1601 * file that already has flags on it. XXX
1603 if (fd >= 0 && fstat(fd, &a->st) == 0)
1604 a->pst = &a->st;
1605 else if (lstat(name, &a->st) == 0)
1606 a->pst = &a->st;
1607 else {
1608 archive_set_error(&a->archive, errno,
1609 "Couldn't stat file");
1610 return (ARCHIVE_WARN);
1613 a->st.st_flags &= ~clear;
1614 a->st.st_flags |= set;
1615 #ifdef HAVE_FCHFLAGS
1616 /* If platform has fchflags() and we were given an fd, use it. */
1617 if (fd >= 0 && fchflags(fd, a->st.st_flags) == 0)
1618 return (ARCHIVE_OK);
1619 #endif
1621 * If we can't use the fd to set the flags, we'll use the
1622 * pathname to set flags. We prefer lchflags() but will use
1623 * chflags() if we must.
1625 #ifdef HAVE_LCHFLAGS
1626 if (lchflags(name, a->st.st_flags) == 0)
1627 return (ARCHIVE_OK);
1628 #elif defined(HAVE_CHFLAGS)
1629 if (S_ISLNK(a->st.st_mode)) {
1630 archive_set_error(&a->archive, errno,
1631 "Can't set file flags on symlink.");
1632 return (ARCHIVE_WARN);
1634 if (chflags(name, a->st.st_flags) == 0)
1635 return (ARCHIVE_OK);
1636 #endif
1637 archive_set_error(&a->archive, errno,
1638 "Failed to set file flags");
1639 return (ARCHIVE_WARN);
1642 #elif defined(__linux) && defined(EXT2_IOC_GETFLAGS) && defined(EXT2_IOC_SETFLAGS)
1645 * Linux has flags too, but uses ioctl() to access them instead of
1646 * having a separate chflags() system call.
1648 static int
1649 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
1650 mode_t mode, unsigned long set, unsigned long clear)
1652 int ret;
1653 int myfd = fd;
1654 unsigned long newflags, oldflags;
1655 unsigned long sf_mask = 0;
1657 if (set == 0 && clear == 0)
1658 return (ARCHIVE_OK);
1659 /* Only regular files and dirs can have flags. */
1660 if (!S_ISREG(mode) && !S_ISDIR(mode))
1661 return (ARCHIVE_OK);
1663 /* If we weren't given an fd, open it ourselves. */
1664 if (myfd < 0)
1665 myfd = open(name, O_RDONLY|O_NONBLOCK);
1666 if (myfd < 0)
1667 return (ARCHIVE_OK);
1670 * Linux has no define for the flags that are only settable by
1671 * the root user. This code may seem a little complex, but
1672 * there seem to be some Linux systems that lack these
1673 * defines. (?) The code below degrades reasonably gracefully
1674 * if sf_mask is incomplete.
1676 #ifdef EXT2_IMMUTABLE_FL
1677 sf_mask |= EXT2_IMMUTABLE_FL;
1678 #endif
1679 #ifdef EXT2_APPEND_FL
1680 sf_mask |= EXT2_APPEND_FL;
1681 #endif
1683 * XXX As above, this would be way simpler if we didn't have
1684 * to read the current flags from disk. XXX
1686 ret = ARCHIVE_OK;
1687 /* Try setting the flags as given. */
1688 if (ioctl(myfd, EXT2_IOC_GETFLAGS, &oldflags) >= 0) {
1689 newflags = (oldflags & ~clear) | set;
1690 if (ioctl(myfd, EXT2_IOC_SETFLAGS, &newflags) >= 0)
1691 goto cleanup;
1692 if (errno != EPERM)
1693 goto fail;
1695 /* If we couldn't set all the flags, try again with a subset. */
1696 if (ioctl(myfd, EXT2_IOC_GETFLAGS, &oldflags) >= 0) {
1697 newflags &= ~sf_mask;
1698 oldflags &= sf_mask;
1699 newflags |= oldflags;
1700 if (ioctl(myfd, EXT2_IOC_SETFLAGS, &newflags) >= 0)
1701 goto cleanup;
1703 /* We couldn't set the flags, so report the failure. */
1704 fail:
1705 archive_set_error(&a->archive, errno,
1706 "Failed to set file flags");
1707 ret = ARCHIVE_WARN;
1708 cleanup:
1709 if (fd < 0)
1710 close(myfd);
1711 return (ret);
1714 #else /* Not HAVE_CHFLAGS && Not __linux */
1717 * Of course, some systems have neither BSD chflags() nor Linux' flags
1718 * support through ioctl().
1720 static int
1721 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
1722 mode_t mode, unsigned long set, unsigned long clear)
1724 (void)ad; /* UNUSED */
1725 (void)fd; /* UNUSED */
1726 (void)name; /* UNUSED */
1727 (void)mode; /* UNUSED */
1728 (void)set; /* UNUSED */
1729 (void)clear; /* UNUSED */
1730 return (ARCHIVE_OK);
1733 #endif /* __linux */
1735 #ifndef HAVE_POSIX_ACL
1736 /* Default empty function body to satisfy mainline code. */
1737 static int
1738 set_acls(struct archive_write_disk *a)
1740 (void)a; /* UNUSED */
1741 return (ARCHIVE_OK);
1744 #else
1747 * XXX TODO: What about ACL types other than ACCESS and DEFAULT?
1749 static int
1750 set_acls(struct archive_write_disk *a)
1752 int ret;
1754 ret = set_acl(a, a->fd, a->entry, ACL_TYPE_ACCESS,
1755 ARCHIVE_ENTRY_ACL_TYPE_ACCESS, "access");
1756 if (ret != ARCHIVE_OK)
1757 return (ret);
1758 ret = set_acl(a, a->fd, a->entry, ACL_TYPE_DEFAULT,
1759 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT, "default");
1760 return (ret);
1764 static int
1765 set_acl(struct archive_write_disk *a, int fd, struct archive_entry *entry,
1766 acl_type_t acl_type, int ae_requested_type, const char *tname)
1768 acl_t acl;
1769 acl_entry_t acl_entry;
1770 acl_permset_t acl_permset;
1771 int ret;
1772 int ae_type, ae_permset, ae_tag, ae_id;
1773 uid_t ae_uid;
1774 gid_t ae_gid;
1775 const char *ae_name;
1776 int entries;
1777 const char *name;
1779 ret = ARCHIVE_OK;
1780 entries = archive_entry_acl_reset(entry, ae_requested_type);
1781 if (entries == 0)
1782 return (ARCHIVE_OK);
1783 acl = acl_init(entries);
1784 while (archive_entry_acl_next(entry, ae_requested_type, &ae_type,
1785 &ae_permset, &ae_tag, &ae_id, &ae_name) == ARCHIVE_OK) {
1786 acl_create_entry(&acl, &acl_entry);
1788 switch (ae_tag) {
1789 case ARCHIVE_ENTRY_ACL_USER:
1790 acl_set_tag_type(acl_entry, ACL_USER);
1791 ae_uid = a->lookup_uid(a->lookup_uid_data,
1792 ae_name, ae_id);
1793 acl_set_qualifier(acl_entry, &ae_uid);
1794 break;
1795 case ARCHIVE_ENTRY_ACL_GROUP:
1796 acl_set_tag_type(acl_entry, ACL_GROUP);
1797 ae_gid = a->lookup_gid(a->lookup_gid_data,
1798 ae_name, ae_id);
1799 acl_set_qualifier(acl_entry, &ae_gid);
1800 break;
1801 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1802 acl_set_tag_type(acl_entry, ACL_USER_OBJ);
1803 break;
1804 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1805 acl_set_tag_type(acl_entry, ACL_GROUP_OBJ);
1806 break;
1807 case ARCHIVE_ENTRY_ACL_MASK:
1808 acl_set_tag_type(acl_entry, ACL_MASK);
1809 break;
1810 case ARCHIVE_ENTRY_ACL_OTHER:
1811 acl_set_tag_type(acl_entry, ACL_OTHER);
1812 break;
1813 default:
1814 /* XXX */
1815 break;
1818 acl_get_permset(acl_entry, &acl_permset);
1819 acl_clear_perms(acl_permset);
1820 if (ae_permset & ARCHIVE_ENTRY_ACL_EXECUTE)
1821 acl_add_perm(acl_permset, ACL_EXECUTE);
1822 if (ae_permset & ARCHIVE_ENTRY_ACL_WRITE)
1823 acl_add_perm(acl_permset, ACL_WRITE);
1824 if (ae_permset & ARCHIVE_ENTRY_ACL_READ)
1825 acl_add_perm(acl_permset, ACL_READ);
1828 name = archive_entry_pathname(entry);
1830 /* Try restoring the ACL through 'fd' if we can. */
1831 #if HAVE_ACL_SET_FD
1832 if (fd >= 0 && acl_type == ACL_TYPE_ACCESS && acl_set_fd(fd, acl) == 0)
1833 ret = ARCHIVE_OK;
1834 else
1835 #else
1836 #if HAVE_ACL_SET_FD_NP
1837 if (fd >= 0 && acl_set_fd_np(fd, acl, acl_type) == 0)
1838 ret = ARCHIVE_OK;
1839 else
1840 #endif
1841 #endif
1842 if (acl_set_file(name, acl_type, acl) != 0) {
1843 archive_set_error(&a->archive, errno, "Failed to set %s acl", tname);
1844 ret = ARCHIVE_WARN;
1846 acl_free(acl);
1847 return (ret);
1849 #endif
1851 #if HAVE_LSETXATTR
1853 * Restore extended attributes - Linux implementation
1855 static int
1856 set_xattrs(struct archive_write_disk *a)
1858 struct archive_entry *entry = a->entry;
1859 static int warning_done = 0;
1860 int ret = ARCHIVE_OK;
1861 int i = archive_entry_xattr_reset(entry);
1863 while (i--) {
1864 const char *name;
1865 const void *value;
1866 size_t size;
1867 archive_entry_xattr_next(entry, &name, &value, &size);
1868 if (name != NULL &&
1869 strncmp(name, "xfsroot.", 8) != 0 &&
1870 strncmp(name, "system.", 7) != 0) {
1871 int e;
1872 #if HAVE_FSETXATTR
1873 if (a->fd >= 0)
1874 e = fsetxattr(a->fd, name, value, size, 0);
1875 else
1876 #endif
1878 e = lsetxattr(archive_entry_pathname(entry),
1879 name, value, size, 0);
1881 if (e == -1) {
1882 if (errno == ENOTSUP) {
1883 if (!warning_done) {
1884 warning_done = 1;
1885 archive_set_error(&a->archive, errno,
1886 "Cannot restore extended "
1887 "attributes on this file "
1888 "system");
1890 } else
1891 archive_set_error(&a->archive, errno,
1892 "Failed to set extended attribute");
1893 ret = ARCHIVE_WARN;
1895 } else {
1896 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1897 "Invalid extended attribute encountered");
1898 ret = ARCHIVE_WARN;
1901 return (ret);
1903 #else
1905 * Restore extended attributes - stub implementation for unsupported systems
1907 static int
1908 set_xattrs(struct archive_write_disk *a)
1910 static int warning_done = 0;
1912 /* If there aren't any extended attributes, then it's okay not
1913 * to extract them, otherwise, issue a single warning. */
1914 if (archive_entry_xattr_count(a->entry) != 0 && !warning_done) {
1915 warning_done = 1;
1916 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1917 "Cannot restore extended attributes on this system");
1918 return (ARCHIVE_WARN);
1920 /* Warning was already emitted; suppress further warnings. */
1921 return (ARCHIVE_OK);
1923 #endif
1927 * Trivial implementations of gid/uid lookup functions.
1928 * These are normally overridden by the client, but these stub
1929 * versions ensure that we always have something that works.
1931 static gid_t
1932 trivial_lookup_gid(void *private_data, const char *gname, gid_t gid)
1934 (void)private_data; /* UNUSED */
1935 (void)gname; /* UNUSED */
1936 return (gid);
1939 static uid_t
1940 trivial_lookup_uid(void *private_data, const char *uname, uid_t uid)
1942 (void)private_data; /* UNUSED */
1943 (void)uname; /* UNUSED */
1944 return (uid);