1 /*-
2  * Copyright (c) 2003-2010 Tim Kientzle
3  * Copyright (c) 2011-2012 Michihiro NAKAJIMA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer
11  *    in this position and unchanged.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "archive_platform.h"
29 __FBSDID("$FreeBSD$");
30 
31 #if defined(_WIN32) && !defined(__CYGWIN__)
32 
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
35 #endif
36 #ifdef HAVE_SYS_UTIME_H
37 #include <sys/utime.h>
38 #endif
39 #ifdef HAVE_ERRNO_H
40 #include <errno.h>
41 #endif
42 #ifdef HAVE_FCNTL_H
43 #include <fcntl.h>
44 #endif
45 #ifdef HAVE_LIMITS_H
46 #include <limits.h>
47 #endif
48 #ifdef HAVE_STDLIB_H
49 #include <stdlib.h>
50 #endif
51 #include <winioctl.h>
52 
53 /* TODO: Support Mac OS 'quarantine' feature.  This is really just a
54  * standard tag to mark files that have been downloaded as "tainted".
55  * On Mac OS, we should mark the extracted files as tainted if the
56  * archive being read was tainted.  Windows has a similar feature; we
57  * should investigate ways to support this generically. */
58 
59 #include "archive.h"
60 #include "archive_acl_private.h"
61 #include "archive_string.h"
62 #include "archive_entry.h"
63 #include "archive_private.h"
64 
65 #ifndef O_BINARY
66 #define O_BINARY 0
67 #endif
68 #ifndef IO_REPARSE_TAG_SYMLINK
69 /* Old SDKs do not provide IO_REPARSE_TAG_SYMLINK */
70 #define	IO_REPARSE_TAG_SYMLINK 0xA000000CL
71 #endif
72 
SetFilePointerEx_perso(HANDLE hFile,LARGE_INTEGER liDistanceToMove,PLARGE_INTEGER lpNewFilePointer,DWORD dwMoveMethod)73 static BOOL SetFilePointerEx_perso(HANDLE hFile,
74                              LARGE_INTEGER liDistanceToMove,
75                              PLARGE_INTEGER lpNewFilePointer,
76                              DWORD dwMoveMethod)
77 {
78 	LARGE_INTEGER li;
79 	li.QuadPart = liDistanceToMove.QuadPart;
80 	li.LowPart = SetFilePointer(
81 	    hFile, li.LowPart, &li.HighPart, dwMoveMethod);
82 	if(lpNewFilePointer) {
83 		lpNewFilePointer->QuadPart = li.QuadPart;
84 	}
85 	return li.LowPart != (DWORD)-1 || GetLastError() == NO_ERROR;
86 }
87 
88 struct fixup_entry {
89 	struct fixup_entry	*next;
90 	struct archive_acl	 acl;
91 	mode_t			 mode;
92 	int64_t			 atime;
93 	int64_t                  birthtime;
94 	int64_t			 mtime;
95 	int64_t			 ctime;
96 	unsigned long		 atime_nanos;
97 	unsigned long            birthtime_nanos;
98 	unsigned long		 mtime_nanos;
99 	unsigned long		 ctime_nanos;
100 	unsigned long		 fflags_set;
101 	int			 fixup; /* bitmask of what needs fixing */
102 	wchar_t			*name;
103 };
104 
105 /*
106  * We use a bitmask to track which operations remain to be done for
107  * this file.  In particular, this helps us avoid unnecessary
108  * operations when it's possible to take care of one step as a
109  * side-effect of another.  For example, mkdir() can specify the mode
110  * for the newly-created object but symlink() cannot.  This means we
111  * can skip chmod() if mkdir() succeeded, but we must explicitly
112  * chmod() if we're trying to create a directory that already exists
113  * (mkdir() failed) or if we're restoring a symlink.  Similarly, we
114  * need to verify UID/GID before trying to restore SUID/SGID bits;
115  * that verification can occur explicitly through a stat() call or
116  * implicitly because of a successful chown() call.
117  */
118 #define	TODO_MODE_FORCE		0x40000000
119 #define	TODO_MODE_BASE		0x20000000
120 #define	TODO_SUID		0x10000000
121 #define	TODO_SUID_CHECK		0x08000000
122 #define	TODO_SGID		0x04000000
123 #define	TODO_SGID_CHECK		0x02000000
124 #define	TODO_MODE		(TODO_MODE_BASE|TODO_SUID|TODO_SGID)
125 #define	TODO_TIMES		ARCHIVE_EXTRACT_TIME
126 #define	TODO_OWNER		ARCHIVE_EXTRACT_OWNER
127 #define	TODO_FFLAGS		ARCHIVE_EXTRACT_FFLAGS
128 #define	TODO_ACLS		ARCHIVE_EXTRACT_ACL
129 #define	TODO_XATTR		ARCHIVE_EXTRACT_XATTR
130 #define	TODO_MAC_METADATA	ARCHIVE_EXTRACT_MAC_METADATA
131 
132 struct archive_write_disk {
133 	struct archive	archive;
134 
135 	mode_t			 user_umask;
136 	struct fixup_entry	*fixup_list;
137 	struct fixup_entry	*current_fixup;
138 	int64_t			 user_uid;
139 	int			 skip_file_set;
140 	int64_t			 skip_file_dev;
141 	int64_t			 skip_file_ino;
142 	time_t			 start_time;
143 
144 	int64_t (*lookup_gid)(void *private, const char *gname, int64_t gid);
145 	void  (*cleanup_gid)(void *private);
146 	void			*lookup_gid_data;
147 	int64_t (*lookup_uid)(void *private, const char *uname, int64_t uid);
148 	void  (*cleanup_uid)(void *private);
149 	void			*lookup_uid_data;
150 
151 	/*
152 	 * Full path of last file to satisfy symlink checks.
153 	 */
154 	struct archive_wstring	path_safe;
155 
156 	/*
157 	 * Cached stat data from disk for the current entry.
158 	 * If this is valid, pst points to st.  Otherwise,
159 	 * pst is null.
160 	 */
161 	BY_HANDLE_FILE_INFORMATION		 st;
162 	BY_HANDLE_FILE_INFORMATION		*pst;
163 
164 	/* Information about the object being restored right now. */
165 	struct archive_entry	*entry; /* Entry being extracted. */
166 	wchar_t			*name; /* Name of entry, possibly edited. */
167 	struct archive_wstring	 _name_data; /* backing store for 'name' */
168 	wchar_t			*tmpname; /* Temporary name */
169 	struct archive_wstring	_tmpname_data; /* backing store for 'tmpname' */
170 	/* Tasks remaining for this object. */
171 	int			 todo;
172 	/* Tasks deferred until end-of-archive. */
173 	int			 deferred;
174 	/* Options requested by the client. */
175 	int			 flags;
176 	/* Handle for the file we're restoring. */
177 	HANDLE		 fh;
178 	/* Current offset for writing data to the file. */
179 	int64_t			 offset;
180 	/* Last offset actually written to disk. */
181 	int64_t			 fd_offset;
182 	/* Total bytes actually written to files. */
183 	int64_t			 total_bytes_written;
184 	/* Maximum size of file, -1 if unknown. */
185 	int64_t			 filesize;
186 	/* Dir we were in before this restore; only for deep paths. */
187 	int			 restore_pwd;
188 	/* Mode we should use for this entry; affected by _PERM and umask. */
189 	mode_t			 mode;
190 	/* UID/GID to use in restoring this entry. */
191 	int64_t			 uid;
192 	int64_t			 gid;
193 };
194 
195 /*
196  * Default mode for dirs created automatically (will be modified by umask).
197  * Note that POSIX specifies 0777 for implicitly-created dirs, "modified
198  * by the process' file creation mask."
199  */
200 #define	DEFAULT_DIR_MODE 0777
201 /*
202  * Dir modes are restored in two steps:  During the extraction, the permissions
203  * in the archive are modified to match the following limits.  During
204  * the post-extract fixup pass, the permissions from the archive are
205  * applied.
206  */
207 #define	MINIMUM_DIR_MODE 0700
208 #define	MAXIMUM_DIR_MODE 0775
209 
210 static int	disk_unlink(const wchar_t *);
211 static int	disk_rmdir(const wchar_t *);
212 static int	check_symlinks(struct archive_write_disk *);
213 static int	create_filesystem_object(struct archive_write_disk *);
214 static struct fixup_entry *current_fixup(struct archive_write_disk *,
215 		    const wchar_t *pathname);
216 static int	cleanup_pathname(struct archive_write_disk *);
217 static int	create_dir(struct archive_write_disk *, wchar_t *);
218 static int	create_parent_dir(struct archive_write_disk *, wchar_t *);
219 static int	la_chmod(const wchar_t *, mode_t);
220 static int	la_mktemp(struct archive_write_disk *);
221 static int	older(BY_HANDLE_FILE_INFORMATION *, struct archive_entry *);
222 static int	permissive_name_w(struct archive_write_disk *);
223 static int	restore_entry(struct archive_write_disk *);
224 static int	set_acls(struct archive_write_disk *, HANDLE h,
225 		    const wchar_t *, struct archive_acl *);
226 static int	set_xattrs(struct archive_write_disk *);
227 static int	clear_nochange_fflags(struct archive_write_disk *);
228 static int	set_fflags(struct archive_write_disk *);
229 static int	set_fflags_platform(const wchar_t *, unsigned long,
230 		    unsigned long);
231 static int	set_ownership(struct archive_write_disk *);
232 static int	set_mode(struct archive_write_disk *, int mode);
233 static int	set_times(struct archive_write_disk *, HANDLE, int,
234 		    const wchar_t *, time_t, long, time_t, long, time_t,
235 		    long, time_t, long);
236 static int	set_times_from_entry(struct archive_write_disk *);
237 static struct fixup_entry *sort_dir_list(struct fixup_entry *p);
238 static ssize_t	write_data_block(struct archive_write_disk *,
239 		    const char *, size_t);
240 
241 static struct archive_vtable *archive_write_disk_vtable(void);
242 
243 static int	_archive_write_disk_close(struct archive *);
244 static int	_archive_write_disk_free(struct archive *);
245 static int	_archive_write_disk_header(struct archive *,
246 		    struct archive_entry *);
247 static int64_t	_archive_write_disk_filter_bytes(struct archive *, int);
248 static int	_archive_write_disk_finish_entry(struct archive *);
249 static ssize_t	_archive_write_disk_data(struct archive *, const void *,
250 		    size_t);
251 static ssize_t	_archive_write_disk_data_block(struct archive *, const void *,
252 		    size_t, int64_t);
253 
254 #define bhfi_dev(bhfi)	((bhfi)->dwVolumeSerialNumber)
255 /* Treat FileIndex as i-node. We should remove a sequence number
256  * which is high-16-bits of nFileIndexHigh. */
257 #define bhfi_ino(bhfi)	\
258 	((((int64_t)((bhfi)->nFileIndexHigh & 0x0000FFFFUL)) << 32) \
259     + (bhfi)->nFileIndexLow)
260 #define bhfi_size(bhfi)	\
261     ((((int64_t)(bhfi)->nFileSizeHigh) << 32) + (bhfi)->nFileSizeLow)
262 
263 static int
file_information(struct archive_write_disk * a,wchar_t * path,BY_HANDLE_FILE_INFORMATION * st,mode_t * mode,int sim_lstat)264 file_information(struct archive_write_disk *a, wchar_t *path,
265     BY_HANDLE_FILE_INFORMATION *st, mode_t *mode, int sim_lstat)
266 {
267 	HANDLE h;
268 	int r;
269 	DWORD flag = FILE_FLAG_BACKUP_SEMANTICS;
270 	WIN32_FIND_DATAW	findData;
271 
272 	if (sim_lstat || mode != NULL) {
273 		h = FindFirstFileW(path, &findData);
274 		if (h == INVALID_HANDLE_VALUE &&
275 		    GetLastError() == ERROR_INVALID_NAME) {
276 			wchar_t *full;
277 			full = __la_win_permissive_name_w(path);
278 			h = FindFirstFileW(full, &findData);
279 			free(full);
280 		}
281 		if (h == INVALID_HANDLE_VALUE) {
282 			la_dosmaperr(GetLastError());
283 			return (-1);
284 		}
285 		FindClose(h);
286 	}
287 
288 	/* Is symlink file ? */
289 	if (sim_lstat &&
290 	    ((findData.dwFileAttributes
291 		        & FILE_ATTRIBUTE_REPARSE_POINT) &&
292 		(findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK)))
293 		flag |= FILE_FLAG_OPEN_REPARSE_POINT;
294 
295 	h = CreateFileW(a->name, 0, 0, NULL,
296 	    OPEN_EXISTING, flag, NULL);
297 	if (h == INVALID_HANDLE_VALUE &&
298 	    GetLastError() == ERROR_INVALID_NAME) {
299 		wchar_t *full;
300 		full = __la_win_permissive_name_w(path);
301 		h = CreateFileW(full, 0, 0, NULL,
302 		    OPEN_EXISTING, flag, NULL);
303 		free(full);
304 	}
305 	if (h == INVALID_HANDLE_VALUE) {
306 		la_dosmaperr(GetLastError());
307 		return (-1);
308 	}
309 	r = GetFileInformationByHandle(h, st);
310 	CloseHandle(h);
311 	if (r == 0) {
312 		la_dosmaperr(GetLastError());
313 		return (-1);
314 	}
315 
316 	if (mode == NULL)
317 		return (0);
318 
319 	*mode = S_IRUSR | S_IRGRP | S_IROTH;
320 	if ((st->dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0)
321 		*mode |= S_IWUSR | S_IWGRP | S_IWOTH;
322 	if ((st->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
323 	    findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK)
324 		*mode |= S_IFLNK;
325 	else if (st->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
326 		*mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
327 	else {
328 		const wchar_t *p;
329 
330 		*mode |= S_IFREG;
331 		p = wcsrchr(path, L'.');
332 		if (p != NULL && wcslen(p) == 4) {
333 			switch (p[1]) {
334 			case L'B': case L'b':
335 				if ((p[2] == L'A' || p[2] == L'a' ) &&
336 				    (p[3] == L'T' || p[3] == L't' ))
337 					*mode |= S_IXUSR | S_IXGRP | S_IXOTH;
338 				break;
339 			case L'C': case L'c':
340 				if (((p[2] == L'M' || p[2] == L'm' ) &&
341 				    (p[3] == L'D' || p[3] == L'd' )))
342 					*mode |= S_IXUSR | S_IXGRP | S_IXOTH;
343 				break;
344 			case L'E': case L'e':
345 				if ((p[2] == L'X' || p[2] == L'x' ) &&
346 				    (p[3] == L'E' || p[3] == L'e' ))
347 					*mode |= S_IXUSR | S_IXGRP | S_IXOTH;
348 				break;
349 			default:
350 				break;
351 			}
352 		}
353 	}
354 	return (0);
355 }
356 
357 /*
358  * Note: The path, for example, "aa/a/../b../c" will be converted to "aa/c"
359  * by GetFullPathNameW() W32 API, which __la_win_permissive_name_w uses.
360  * It means we cannot handle multiple dirs in one archive_entry.
361  * So we have to make the full-pathname in another way, which does not
362  * break "../" path string.
363  */
364 static int
permissive_name_w(struct archive_write_disk * a)365 permissive_name_w(struct archive_write_disk *a)
366 {
367 	wchar_t *wn, *wnp;
368 	wchar_t *ws, *wsp;
369 	DWORD l;
370 
371 	wnp = a->name;
372 	if (wnp[0] == L'\\' && wnp[1] == L'\\' &&
373 	    wnp[2] == L'?' && wnp[3] == L'\\')
374 		/* We have already a permissive name. */
375 		return (0);
376 
377 	if (wnp[0] == L'\\' && wnp[1] == L'\\' &&
378 		wnp[2] == L'.' && wnp[3] == L'\\') {
379 		/* This is a device name */
380 		if (((wnp[4] >= L'a' && wnp[4] <= L'z') ||
381 		     (wnp[4] >= L'A' && wnp[4] <= L'Z')) &&
382 			 wnp[5] == L':' && wnp[6] == L'\\') {
383 			wnp[2] = L'?';/* Not device name. */
384 			return (0);
385 		}
386 	}
387 
388 	/*
389 	 * A full-pathname starting with a drive name like "C:\abc".
390 	 */
391 	if (((wnp[0] >= L'a' && wnp[0] <= L'z') ||
392 	     (wnp[0] >= L'A' && wnp[0] <= L'Z')) &&
393 		 wnp[1] == L':' && wnp[2] == L'\\') {
394 		wn = _wcsdup(wnp);
395 		if (wn == NULL)
396 			return (-1);
397 		archive_wstring_ensure(&(a->_name_data), 4 + wcslen(wn) + 1);
398 		a->name = a->_name_data.s;
399 		/* Prepend "\\?\" */
400 		archive_wstrncpy(&(a->_name_data), L"\\\\?\\", 4);
401 		archive_wstrcat(&(a->_name_data), wn);
402 		free(wn);
403 		return (0);
404 	}
405 
406 	/*
407 	 * A full-pathname pointing to a network drive
408 	 * like "\\<server-name>\<share-name>\file".
409 	 */
410 	if (wnp[0] == L'\\' && wnp[1] == L'\\' && wnp[2] != L'\\') {
411 		const wchar_t *p = &wnp[2];
412 
413 		/* Skip server-name letters. */
414 		while (*p != L'\\' && *p != L'\0')
415 			++p;
416 		if (*p == L'\\') {
417 			const wchar_t *rp = ++p;
418 			/* Skip share-name letters. */
419 			while (*p != L'\\' && *p != L'\0')
420 				++p;
421 			if (*p == L'\\' && p != rp) {
422 				/* Now, match patterns such as
423 				 * "\\server-name\share-name\" */
424 				wn = _wcsdup(wnp);
425 				if (wn == NULL)
426 					return (-1);
427 				archive_wstring_ensure(&(a->_name_data),
428 					8 + wcslen(wn) + 1);
429 				a->name = a->_name_data.s;
430 				/* Prepend "\\?\UNC\" */
431 				archive_wstrncpy(&(a->_name_data),
432 					L"\\\\?\\UNC\\", 8);
433 				archive_wstrcat(&(a->_name_data), wn+2);
434 				free(wn);
435 				return (0);
436 			}
437 		}
438 		return (0);
439 	}
440 
441 	/*
442 	 * Get current working directory.
443 	 */
444 	l = GetCurrentDirectoryW(0, NULL);
445 	if (l == 0)
446 		return (-1);
447 	ws = malloc(l * sizeof(wchar_t));
448 	l = GetCurrentDirectoryW(l, ws);
449 	if (l == 0) {
450 		free(ws);
451 		return (-1);
452 	}
453 	wsp = ws;
454 
455 	/*
456 	 * A full-pathname starting without a drive name like "\abc".
457 	 */
458 	if (wnp[0] == L'\\') {
459 		wn = _wcsdup(wnp);
460 		if (wn == NULL)
461 			return (-1);
462 		archive_wstring_ensure(&(a->_name_data),
463 			4 + 2 + wcslen(wn) + 1);
464 		a->name = a->_name_data.s;
465 		/* Prepend "\\?\" and drive name. */
466 		archive_wstrncpy(&(a->_name_data), L"\\\\?\\", 4);
467 		archive_wstrncat(&(a->_name_data), wsp, 2);
468 		archive_wstrcat(&(a->_name_data), wn);
469 		free(wsp);
470 		free(wn);
471 		return (0);
472 	}
473 
474 	wn = _wcsdup(wnp);
475 	if (wn == NULL)
476 		return (-1);
477 	archive_wstring_ensure(&(a->_name_data), 4 + l + 1 + wcslen(wn) + 1);
478 	a->name = a->_name_data.s;
479 	/* Prepend "\\?\" and drive name if not already added. */
480 	if (l > 3 && wsp[0] == L'\\' && wsp[1] == L'\\' &&
481 		wsp[2] == L'?' && wsp[3] == L'\\')
482 	{
483 		archive_wstrncpy(&(a->_name_data), wsp, l);
484 	}
485 	else if (l > 2 && wsp[0] == L'\\' && wsp[1] == L'\\' && wsp[2] != L'\\')
486 	{
487 		archive_wstrncpy(&(a->_name_data), L"\\\\?\\UNC\\", 8);
488 		archive_wstrncat(&(a->_name_data), wsp+2, l-2);
489 	}
490 	else
491 	{
492 		archive_wstrncpy(&(a->_name_data), L"\\\\?\\", 4);
493 		archive_wstrncat(&(a->_name_data), wsp, l);
494 	}
495 	archive_wstrncat(&(a->_name_data), L"\\", 1);
496 	archive_wstrcat(&(a->_name_data), wn);
497 	a->name = a->_name_data.s;
498 	free(wsp);
499 	free(wn);
500 	return (0);
501 }
502 
503 static int
la_chmod(const wchar_t * path,mode_t mode)504 la_chmod(const wchar_t *path, mode_t mode)
505 {
506 	DWORD attr;
507 	BOOL r;
508 	wchar_t *fullname;
509 	int ret = 0;
510 
511 	fullname = NULL;
512 	attr = GetFileAttributesW(path);
513 	if (attr == (DWORD)-1 &&
514 	    GetLastError() == ERROR_INVALID_NAME) {
515 		fullname = __la_win_permissive_name_w(path);
516 		attr = GetFileAttributesW(fullname);
517 	}
518 	if (attr == (DWORD)-1) {
519 		la_dosmaperr(GetLastError());
520 		ret = -1;
521 		goto exit_chmode;
522 	}
523 	if (mode & _S_IWRITE)
524 		attr &= ~FILE_ATTRIBUTE_READONLY;
525 	else
526 		attr |= FILE_ATTRIBUTE_READONLY;
527 	if (fullname != NULL)
528 		r = SetFileAttributesW(fullname, attr);
529 	else
530 		r = SetFileAttributesW(path, attr);
531 	if (r == 0) {
532 		la_dosmaperr(GetLastError());
533 		ret = -1;
534 	}
535 exit_chmode:
536 	free(fullname);
537 	return (ret);
538 }
539 
540 static int
la_mktemp(struct archive_write_disk * a)541 la_mktemp(struct archive_write_disk *a)
542 {
543 	int fd;
544 	mode_t mode;
545 
546 	archive_wstring_empty(&(a->_tmpname_data));
547 	archive_wstrcpy(&(a->_tmpname_data), a->name);
548 	archive_wstrcat(&(a->_tmpname_data), L".XXXXXX");
549 	a->tmpname = a->_tmpname_data.s;
550 
551 	fd = __archive_mkstemp(a->tmpname);
552 	if (fd == -1)
553 		return -1;
554 
555 	mode = a->mode & 0777 & ~a->user_umask;
556 	if (la_chmod(a->tmpname, mode) == -1) {
557 		la_dosmaperr(GetLastError());
558 		_close(fd);
559 		return -1;
560 	}
561 	return (fd);
562 }
563 
564 static void *
la_GetFunctionKernel32(const char * name)565 la_GetFunctionKernel32(const char *name)
566 {
567 	static HINSTANCE lib;
568 	static int set;
569 	if (!set) {
570 		set = 1;
571 		lib = LoadLibrary(TEXT("kernel32.dll"));
572 	}
573 	if (lib == NULL) {
574 		fprintf(stderr, "Can't load kernel32.dll?!\n");
575 		exit(1);
576 	}
577 	return (void *)GetProcAddress(lib, name);
578 }
579 
580 static int
la_CreateHardLinkW(wchar_t * linkname,wchar_t * target)581 la_CreateHardLinkW(wchar_t *linkname, wchar_t *target)
582 {
583 	static BOOLEAN (WINAPI *f)(LPWSTR, LPWSTR, LPSECURITY_ATTRIBUTES);
584 	static int set;
585 	BOOL ret;
586 
587 	if (!set) {
588 		set = 1;
589 		f = la_GetFunctionKernel32("CreateHardLinkW");
590 	}
591 	if (!f) {
592 		errno = ENOTSUP;
593 		return (0);
594 	}
595 	ret = (*f)(linkname, target, NULL);
596 	if (!ret) {
597 		/* Under windows 2000, it is necessary to remove
598 		 * the "\\?\" prefix. */
599 #define IS_UNC(name)	((name[0] == L'U' || name[0] == L'u') &&	\
600 			 (name[1] == L'N' || name[1] == L'n') &&	\
601 			 (name[2] == L'C' || name[2] == L'c') &&	\
602 			 name[3] == L'\\')
603 		if (!wcsncmp(linkname,L"\\\\?\\", 4)) {
604 			linkname += 4;
605 			if (IS_UNC(linkname))
606 				linkname += 4;
607 		}
608 		if (!wcsncmp(target,L"\\\\?\\", 4)) {
609 			target += 4;
610 			if (IS_UNC(target))
611 				target += 4;
612 		}
613 #undef IS_UNC
614 		ret = (*f)(linkname, target, NULL);
615 	}
616 	return (ret);
617 }
618 
619 /*
620  * Create file or directory symolic link
621  *
622  * If linktype is AE_SYMLINK_TYPE_UNDEFINED (or unknown), guess linktype from
623  * the link target
624  */
625 static int
la_CreateSymbolicLinkW(const wchar_t * linkname,const wchar_t * target,int linktype)626 la_CreateSymbolicLinkW(const wchar_t *linkname, const wchar_t *target,
627     int linktype) {
628 	static BOOLEAN (WINAPI *f)(LPCWSTR, LPCWSTR, DWORD);
629 	static int set;
630 	wchar_t *ttarget, *p;
631 	int len;
632 	DWORD attrs = 0;
633 	DWORD flags = 0;
634 	DWORD newflags = 0;
635 	BOOL ret = 0;
636 
637 	if (!set) {
638 		set = 1;
639 		f = la_GetFunctionKernel32("CreateSymbolicLinkW");
640 	}
641 	if (!f)
642 		return (0);
643 
644 	len = wcslen(target);
645 	if (len == 0) {
646 		errno = EINVAL;
647 		return(0);
648 	}
649 	/*
650 	 * When writing path targets, we need to translate slashes
651 	 * to backslashes
652 	 */
653 	ttarget = malloc((len + 1) * sizeof(wchar_t));
654 	if (ttarget == NULL)
655 		return(0);
656 
657 	p = ttarget;
658 
659 	while(*target != L'\0') {
660 		if (*target == L'/')
661 			*p = L'\\';
662 		else
663 			*p = *target;
664 		target++;
665 		p++;
666 	}
667 	*p = L'\0';
668 
669 	/*
670 	 * In case of undefined symlink type we guess it from the target.
671 	 * If the target equals ".", "..", ends with a backslash or a
672 	 * backslash followed by "." or ".." we assume it is a directory
673 	 * symlink. In all other cases we assume a file symlink.
674 	 */
675 	if (linktype != AE_SYMLINK_TYPE_FILE && (
676 		linktype == AE_SYMLINK_TYPE_DIRECTORY ||
677 		*(p - 1) == L'\\' || (*(p - 1) == L'.' && (
678 	    len == 1 || *(p - 2) == L'\\' || ( *(p - 2) == L'.' && (
679 	    len == 2 || *(p - 3) == L'\\')))))) {
680 #if defined(SYMBOLIC_LINK_FLAG_DIRECTORY)
681 		flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
682 #else
683 		flags |= 0x1;
684 #endif
685 	}
686 
687 #if defined(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)
688 	newflags = flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
689 #else
690 	newflags = flags | 0x2;
691 #endif
692 
693 	/*
694 	 * Windows won't overwrite existing links
695 	 */
696 	attrs = GetFileAttributesW(linkname);
697 	if (attrs != INVALID_FILE_ATTRIBUTES) {
698 		if (attrs & FILE_ATTRIBUTE_DIRECTORY)
699 			disk_rmdir(linkname);
700 		else
701 			disk_unlink(linkname);
702 	}
703 
704 	ret = (*f)(linkname, ttarget, newflags);
705 	/*
706 	 * Prior to Windows 10 calling CreateSymbolicLinkW() will fail
707 	 * if SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE is set
708 	 */
709 	if (!ret) {
710 		ret = (*f)(linkname, ttarget, flags);
711 	}
712 	free(ttarget);
713 	return (ret);
714 }
715 
716 static int
la_ftruncate(HANDLE handle,int64_t length)717 la_ftruncate(HANDLE handle, int64_t length)
718 {
719 	LARGE_INTEGER distance;
720 
721 	if (GetFileType(handle) != FILE_TYPE_DISK) {
722 		errno = EBADF;
723 		return (-1);
724 	}
725 	distance.QuadPart = length;
726 	if (!SetFilePointerEx_perso(handle, distance, NULL, FILE_BEGIN)) {
727 		la_dosmaperr(GetLastError());
728 		return (-1);
729 	}
730 	if (!SetEndOfFile(handle)) {
731 		la_dosmaperr(GetLastError());
732 		return (-1);
733 	}
734 	return (0);
735 }
736 
737 static int
lazy_stat(struct archive_write_disk * a)738 lazy_stat(struct archive_write_disk *a)
739 {
740 	if (a->pst != NULL) {
741 		/* Already have stat() data available. */
742 		return (ARCHIVE_OK);
743 	}
744 	if (a->fh != INVALID_HANDLE_VALUE &&
745 	    GetFileInformationByHandle(a->fh, &a->st) == 0) {
746 		a->pst = &a->st;
747 		return (ARCHIVE_OK);
748 	}
749 
750 	/*
751 	 * XXX At this point, symlinks should not be hit, otherwise
752 	 * XXX a race occurred.  Do we want to check explicitly for that?
753 	 */
754 	if (file_information(a, a->name, &a->st, NULL, 1) == 0) {
755 		a->pst = &a->st;
756 		return (ARCHIVE_OK);
757 	}
758 	archive_set_error(&a->archive, errno, "Couldn't stat file");
759 	return (ARCHIVE_WARN);
760 }
761 
762 static struct archive_vtable *
archive_write_disk_vtable(void)763 archive_write_disk_vtable(void)
764 {
765 	static struct archive_vtable av;
766 	static int inited = 0;
767 
768 	if (!inited) {
769 		av.archive_close = _archive_write_disk_close;
770 		av.archive_filter_bytes = _archive_write_disk_filter_bytes;
771 		av.archive_free = _archive_write_disk_free;
772 		av.archive_write_header = _archive_write_disk_header;
773 		av.archive_write_finish_entry
774 		    = _archive_write_disk_finish_entry;
775 		av.archive_write_data = _archive_write_disk_data;
776 		av.archive_write_data_block = _archive_write_disk_data_block;
777 		inited = 1;
778 	}
779 	return (&av);
780 }
781 
782 static int64_t
_archive_write_disk_filter_bytes(struct archive * _a,int n)783 _archive_write_disk_filter_bytes(struct archive *_a, int n)
784 {
785 	struct archive_write_disk *a = (struct archive_write_disk *)_a;
786 	(void)n; /* UNUSED */
787 	if (n == -1 || n == 0)
788 		return (a->total_bytes_written);
789 	return (-1);
790 }
791 
792 
793 int
archive_write_disk_set_options(struct archive * _a,int flags)794 archive_write_disk_set_options(struct archive *_a, int flags)
795 {
796 	struct archive_write_disk *a = (struct archive_write_disk *)_a;
797 
798 	a->flags = flags;
799 	return (ARCHIVE_OK);
800 }
801 
802 
803 /*
804  * Extract this entry to disk.
805  *
806  * TODO: Validate hardlinks.  According to the standards, we're
807  * supposed to check each extracted hardlink and squawk if it refers
808  * to a file that we didn't restore.  I'm not entirely convinced this
809  * is a good idea, but more importantly: Is there any way to validate
810  * hardlinks without keeping a complete list of filenames from the
811  * entire archive?? Ugh.
812  *
813  */
814 static int
_archive_write_disk_header(struct archive * _a,struct archive_entry * entry)815 _archive_write_disk_header(struct archive *_a, struct archive_entry *entry)
816 {
817 	struct archive_write_disk *a = (struct archive_write_disk *)_a;
818 	struct fixup_entry *fe;
819 	int ret, r;
820 
821 	archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
822 	    ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
823 	    "archive_write_disk_header");
824 	archive_clear_error(&a->archive);
825 	if (a->archive.state & ARCHIVE_STATE_DATA) {
826 		r = _archive_write_disk_finish_entry(&a->archive);
827 		if (r == ARCHIVE_FATAL)
828 			return (r);
829 	}
830 
831 	/* Set up for this particular entry. */
832 	a->pst = NULL;
833 	a->current_fixup = NULL;
834 	a->deferred = 0;
835 	archive_entry_free(a->entry);
836 	a->entry = NULL;
837 	a->entry = archive_entry_clone(entry);
838 	a->fh = INVALID_HANDLE_VALUE;
839 	a->fd_offset = 0;
840 	a->offset = 0;
841 	a->restore_pwd = -1;
842 	a->uid = a->user_uid;
843 	a->mode = archive_entry_mode(a->entry);
844 	if (archive_entry_size_is_set(a->entry))
845 		a->filesize = archive_entry_size(a->entry);
846 	else
847 		a->filesize = -1;
848 	archive_wstrcpy(&(a->_name_data), archive_entry_pathname_w(a->entry));
849 	a->name = a->_name_data.s;
850 	archive_clear_error(&a->archive);
851 
852 	/*
853 	 * Clean up the requested path.  This is necessary for correct
854 	 * dir restores; the dir restore logic otherwise gets messed
855 	 * up by nonsense like "dir/.".
856 	 */
857 	ret = cleanup_pathname(a);
858 	if (ret != ARCHIVE_OK)
859 		return (ret);
860 
861 	/*
862 	 * Generate a full-pathname and use it from here.
863 	 */
864 	if (permissive_name_w(a) < 0) {
865 		errno = EINVAL;
866 		return (ARCHIVE_FAILED);
867 	}
868 
869 	/*
870 	 * Query the umask so we get predictable mode settings.
871 	 * This gets done on every call to _write_header in case the
872 	 * user edits their umask during the extraction for some
873 	 * reason.
874 	 */
875 	umask(a->user_umask = umask(0));
876 
877 	/* Figure out what we need to do for this entry. */
878 	a->todo = TODO_MODE_BASE;
879 	if (a->flags & ARCHIVE_EXTRACT_PERM) {
880 		a->todo |= TODO_MODE_FORCE; /* Be pushy about permissions. */
881 		/*
882 		 * SGID requires an extra "check" step because we
883 		 * cannot easily predict the GID that the system will
884 		 * assign.  (Different systems assign GIDs to files
885 		 * based on a variety of criteria, including process
886 		 * credentials and the gid of the enclosing
887 		 * directory.)  We can only restore the SGID bit if
888 		 * the file has the right GID, and we only know the
889 		 * GID if we either set it (see set_ownership) or if
890 		 * we've actually called stat() on the file after it
891 		 * was restored.  Since there are several places at
892 		 * which we might verify the GID, we need a TODO bit
893 		 * to keep track.
894 		 */
895 		if (a->mode & S_ISGID)
896 			a->todo |= TODO_SGID | TODO_SGID_CHECK;
897 		/*
898 		 * Verifying the SUID is simpler, but can still be
899 		 * done in multiple ways, hence the separate "check" bit.
900 		 */
901 		if (a->mode & S_ISUID)
902 			a->todo |= TODO_SUID | TODO_SUID_CHECK;
903 	} else {
904 		/*
905 		 * User didn't request full permissions, so don't
906 		 * restore SUID, SGID bits and obey umask.
907 		 */
908 		a->mode &= ~S_ISUID;
909 		a->mode &= ~S_ISGID;
910 		a->mode &= ~S_ISVTX;
911 		a->mode &= ~a->user_umask;
912 	}
913 #if 0
914 	if (a->flags & ARCHIVE_EXTRACT_OWNER)
915 		a->todo |= TODO_OWNER;
916 #endif
917 	if (a->flags & ARCHIVE_EXTRACT_TIME)
918 		a->todo |= TODO_TIMES;
919 	if (a->flags & ARCHIVE_EXTRACT_ACL) {
920 		if (archive_entry_filetype(a->entry) == AE_IFDIR)
921 			a->deferred |= TODO_ACLS;
922 		else
923 			a->todo |= TODO_ACLS;
924 	}
925 	if (a->flags & ARCHIVE_EXTRACT_XATTR)
926 		a->todo |= TODO_XATTR;
927 	if (a->flags & ARCHIVE_EXTRACT_FFLAGS)
928 		a->todo |= TODO_FFLAGS;
929 	if (a->flags & ARCHIVE_EXTRACT_SECURE_SYMLINKS) {
930 		ret = check_symlinks(a);
931 		if (ret != ARCHIVE_OK)
932 			return (ret);
933 	}
934 
935 	ret = restore_entry(a);
936 
937 	/*
938 	 * TODO: There are rumours that some extended attributes must
939 	 * be restored before file data is written.  If this is true,
940 	 * then we either need to write all extended attributes both
941 	 * before and after restoring the data, or find some rule for
942 	 * determining which must go first and which last.  Due to the
943 	 * many ways people are using xattrs, this may prove to be an
944 	 * intractable problem.
945 	 */
946 
947 	/*
948 	 * Fixup uses the unedited pathname from archive_entry_pathname(),
949 	 * because it is relative to the base dir and the edited path
950 	 * might be relative to some intermediate dir as a result of the
951 	 * deep restore logic.
952 	 */
953 	if (a->deferred & TODO_MODE) {
954 		fe = current_fixup(a, archive_entry_pathname_w(entry));
955 		fe->fixup |= TODO_MODE_BASE;
956 		fe->mode = a->mode;
957 	}
958 
959 	if ((a->deferred & TODO_TIMES)
960 		&& (archive_entry_mtime_is_set(entry)
961 		    || archive_entry_atime_is_set(entry))) {
962 		fe = current_fixup(a, archive_entry_pathname_w(entry));
963 		fe->mode = a->mode;
964 		fe->fixup |= TODO_TIMES;
965 		if (archive_entry_atime_is_set(entry)) {
966 			fe->atime = archive_entry_atime(entry);
967 			fe->atime_nanos = archive_entry_atime_nsec(entry);
968 		} else {
969 			/* If atime is unset, use start time. */
970 			fe->atime = a->start_time;
971 			fe->atime_nanos = 0;
972 		}
973 		if (archive_entry_mtime_is_set(entry)) {
974 			fe->mtime = archive_entry_mtime(entry);
975 			fe->mtime_nanos = archive_entry_mtime_nsec(entry);
976 		} else {
977 			/* If mtime is unset, use start time. */
978 			fe->mtime = a->start_time;
979 			fe->mtime_nanos = 0;
980 		}
981 		if (archive_entry_birthtime_is_set(entry)) {
982 			fe->birthtime = archive_entry_birthtime(entry);
983 			fe->birthtime_nanos = archive_entry_birthtime_nsec(entry);
984 		} else {
985 			/* If birthtime is unset, use mtime. */
986 			fe->birthtime = fe->mtime;
987 			fe->birthtime_nanos = fe->mtime_nanos;
988 		}
989 	}
990 
991 	if (a->deferred & TODO_ACLS) {
992 		fe = current_fixup(a, archive_entry_pathname_w(entry));
993 		archive_acl_copy(&fe->acl, archive_entry_acl(entry));
994 	}
995 
996 	if (a->deferred & TODO_FFLAGS) {
997 		unsigned long set, clear;
998 
999 		fe = current_fixup(a, archive_entry_pathname_w(entry));
1000 		archive_entry_fflags(entry, &set, &clear);
1001 		fe->fflags_set = set;
1002 	}
1003 
1004 	/*
1005 	 * On Windows, A creating sparse file requires a special mark.
1006 	 */
1007 	if (a->fh != INVALID_HANDLE_VALUE &&
1008 	    archive_entry_sparse_count(entry) > 0) {
1009 		int64_t base = 0, offset, length;
1010 		int i, cnt = archive_entry_sparse_reset(entry);
1011 		int sparse = 0;
1012 
1013 		for (i = 0; i < cnt; i++) {
1014 			archive_entry_sparse_next(entry, &offset, &length);
1015 			if (offset - base >= 4096) {
1016 				sparse = 1;/* we have a hole. */
1017 				break;
1018 			}
1019 			base = offset + length;
1020 		}
1021 		if (sparse) {
1022 			DWORD dmy;
1023 			/* Mark this file as sparse. */
1024 			DeviceIoControl(a->fh, FSCTL_SET_SPARSE,
1025 			    NULL, 0, NULL, 0, &dmy, NULL);
1026 		}
1027 	}
1028 
1029 	/* We've created the object and are ready to pour data into it. */
1030 	if (ret >= ARCHIVE_WARN)
1031 		a->archive.state = ARCHIVE_STATE_DATA;
1032 	/*
1033 	 * If it's not open, tell our client not to try writing.
1034 	 * In particular, dirs, links, etc, don't get written to.
1035 	 */
1036 	if (a->fh == INVALID_HANDLE_VALUE) {
1037 		archive_entry_set_size(entry, 0);
1038 		a->filesize = 0;
1039 	}
1040 
1041 	return (ret);
1042 }
1043 
1044 int
archive_write_disk_set_skip_file(struct archive * _a,la_int64_t d,la_int64_t i)1045 archive_write_disk_set_skip_file(struct archive *_a, la_int64_t d, la_int64_t i)
1046 {
1047 	struct archive_write_disk *a = (struct archive_write_disk *)_a;
1048 	archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1049 	    ARCHIVE_STATE_ANY, "archive_write_disk_set_skip_file");
1050 	a->skip_file_set = 1;
1051 	a->skip_file_dev = d;
1052 	a->skip_file_ino = i;
1053 	return (ARCHIVE_OK);
1054 }
1055 
1056 static ssize_t
write_data_block(struct archive_write_disk * a,const char * buff,size_t size)1057 write_data_block(struct archive_write_disk *a, const char *buff, size_t size)
1058 {
1059 	OVERLAPPED ol;
1060 	uint64_t start_size = size;
1061 	DWORD bytes_written = 0;
1062 	ssize_t block_size = 0, bytes_to_write;
1063 
1064 	if (size == 0)
1065 		return (ARCHIVE_OK);
1066 
1067 	if (a->filesize == 0 || a->fh == INVALID_HANDLE_VALUE) {
1068 		archive_set_error(&a->archive, 0,
1069 		    "Attempt to write to an empty file");
1070 		return (ARCHIVE_WARN);
1071 	}
1072 
1073 	if (a->flags & ARCHIVE_EXTRACT_SPARSE) {
1074 		/* XXX TODO XXX Is there a more appropriate choice here ? */
1075 		/* This needn't match the filesystem allocation size. */
1076 		block_size = 16*1024;
1077 	}
1078 
1079 	/* If this write would run beyond the file size, truncate it. */
1080 	if (a->filesize >= 0 && (int64_t)(a->offset + size) > a->filesize)
1081 		start_size = size = (size_t)(a->filesize - a->offset);
1082 
1083 	/* Write the data. */
1084 	while (size > 0) {
1085 		if (block_size == 0) {
1086 			bytes_to_write = size;
1087 		} else {
1088 			/* We're sparsifying the file. */
1089 			const char *p, *end;
1090 			int64_t block_end;
1091 
1092 			/* Skip leading zero bytes. */
1093 			for (p = buff, end = buff + size; p < end; ++p) {
1094 				if (*p != '\0')
1095 					break;
1096 			}
1097 			a->offset += p - buff;
1098 			size -= p - buff;
1099 			buff = p;
1100 			if (size == 0)
1101 				break;
1102 
1103 			/* Calculate next block boundary after offset. */
1104 			block_end
1105 			    = (a->offset / block_size + 1) * block_size;
1106 
1107 			/* If the adjusted write would cross block boundary,
1108 			 * truncate it to the block boundary. */
1109 			bytes_to_write = size;
1110 			if (a->offset + bytes_to_write > block_end)
1111 				bytes_to_write = (DWORD)(block_end - a->offset);
1112 		}
1113 		memset(&ol, 0, sizeof(ol));
1114 		ol.Offset = (DWORD)(a->offset & 0xFFFFFFFF);
1115 		ol.OffsetHigh = (DWORD)(a->offset >> 32);
1116 		if (!WriteFile(a->fh, buff, (uint32_t)bytes_to_write,
1117 		    &bytes_written, &ol)) {
1118 			DWORD lasterr;
1119 
1120 			lasterr = GetLastError();
1121 			if (lasterr == ERROR_ACCESS_DENIED)
1122 				errno = EBADF;
1123 			else
1124 				la_dosmaperr(lasterr);
1125 			archive_set_error(&a->archive, errno, "Write failed");
1126 			return (ARCHIVE_WARN);
1127 		}
1128 		buff += bytes_written;
1129 		size -= bytes_written;
1130 		a->total_bytes_written += bytes_written;
1131 		a->offset += bytes_written;
1132 		a->fd_offset = a->offset;
1133 	}
1134 	return ((ssize_t)(start_size - size));
1135 }
1136 
1137 static ssize_t
_archive_write_disk_data_block(struct archive * _a,const void * buff,size_t size,int64_t offset)1138 _archive_write_disk_data_block(struct archive *_a,
1139     const void *buff, size_t size, int64_t offset)
1140 {
1141 	struct archive_write_disk *a = (struct archive_write_disk *)_a;
1142 	ssize_t r;
1143 
1144 	archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1145 	    ARCHIVE_STATE_DATA, "archive_write_data_block");
1146 
1147 	a->offset = offset;
1148 	r = write_data_block(a, buff, size);
1149 	if (r < ARCHIVE_OK)
1150 		return (r);
1151 	if ((size_t)r < size) {
1152 		archive_set_error(&a->archive, 0,
1153 		    "Write request too large");
1154 		return (ARCHIVE_WARN);
1155 	}
1156 #if ARCHIVE_VERSION_NUMBER < 3999000
1157 	return (ARCHIVE_OK);
1158 #else
1159 	return (size);
1160 #endif
1161 }
1162 
1163 static ssize_t
_archive_write_disk_data(struct archive * _a,const void * buff,size_t size)1164 _archive_write_disk_data(struct archive *_a, const void *buff, size_t size)
1165 {
1166 	struct archive_write_disk *a = (struct archive_write_disk *)_a;
1167 
1168 	archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1169 	    ARCHIVE_STATE_DATA, "archive_write_data");
1170 
1171 	return (write_data_block(a, buff, size));
1172 }
1173 
1174 static int
_archive_write_disk_finish_entry(struct archive * _a)1175 _archive_write_disk_finish_entry(struct archive *_a)
1176 {
1177 	struct archive_write_disk *a = (struct archive_write_disk *)_a;
1178 	int ret = ARCHIVE_OK;
1179 
1180 	archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1181 	    ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1182 	    "archive_write_finish_entry");
1183 	if (a->archive.state & ARCHIVE_STATE_HEADER)
1184 		return (ARCHIVE_OK);
1185 	archive_clear_error(&a->archive);
1186 
1187 	/* Pad or truncate file to the right size. */
1188 	if (a->fh == INVALID_HANDLE_VALUE) {
1189 		/* There's no file. */
1190 	} else if (a->filesize < 0) {
1191 		/* File size is unknown, so we can't set the size. */
1192 	} else if (a->fd_offset == a->filesize) {
1193 		/* Last write ended at exactly the filesize; we're done. */
1194 		/* Hopefully, this is the common case. */
1195 	} else {
1196 		if (la_ftruncate(a->fh, a->filesize) == -1) {
1197 			archive_set_error(&a->archive, errno,
1198 			    "File size could not be restored");
1199 			return (ARCHIVE_FAILED);
1200 		}
1201 	}
1202 
1203 	/* Restore metadata. */
1204 
1205 	/*
1206 	 * Look up the "real" UID only if we're going to need it.
1207 	 * TODO: the TODO_SGID condition can be dropped here, can't it?
1208 	 */
1209 	if (a->todo & (TODO_OWNER | TODO_SUID | TODO_SGID)) {
1210 		a->uid = archive_write_disk_uid(&a->archive,
1211 		    archive_entry_uname(a->entry),
1212 		    archive_entry_uid(a->entry));
1213 	}
1214 	/* Look up the "real" GID only if we're going to need it. */
1215 	/* TODO: the TODO_SUID condition can be dropped here, can't it? */
1216 	if (a->todo & (TODO_OWNER | TODO_SGID | TODO_SUID)) {
1217 		a->gid = archive_write_disk_gid(&a->archive,
1218 		    archive_entry_gname(a->entry),
1219 		    archive_entry_gid(a->entry));
1220 	 }
1221 
1222 	/*
1223 	 * Restore ownership before set_mode tries to restore suid/sgid
1224 	 * bits.  If we set the owner, we know what it is and can skip
1225 	 * a stat() call to examine the ownership of the file on disk.
1226 	 */
1227 	if (a->todo & TODO_OWNER)
1228 		ret = set_ownership(a);
1229 
1230 	/*
1231 	 * set_mode must precede ACLs on systems such as Solaris and
1232 	 * FreeBSD where setting the mode implicitly clears extended ACLs
1233 	 */
1234 	if (a->todo & TODO_MODE) {
1235 		int r2 = set_mode(a, a->mode);
1236 		if (r2 < ret) ret = r2;
1237 	}
1238 
1239 	/*
1240 	 * Security-related extended attributes (such as
1241 	 * security.capability on Linux) have to be restored last,
1242 	 * since they're implicitly removed by other file changes.
1243 	 */
1244 	if (a->todo & TODO_XATTR) {
1245 		int r2 = set_xattrs(a);
1246 		if (r2 < ret) ret = r2;
1247 	}
1248 
1249 	/*
1250 	 * Some flags prevent file modification; they must be restored after
1251 	 * file contents are written.
1252 	 */
1253 	if (a->todo & TODO_FFLAGS) {
1254 		int r2 = set_fflags(a);
1255 		if (r2 < ret) ret = r2;
1256 	}
1257 
1258 	/*
1259 	 * Time must follow most other metadata;
1260 	 * otherwise atime will get changed.
1261 	 */
1262 	if (a->todo & TODO_TIMES) {
1263 		int r2 = set_times_from_entry(a);
1264 		if (r2 < ret) ret = r2;
1265 	}
1266 
1267 	/*
1268 	 * ACLs must be restored after timestamps because there are
1269 	 * ACLs that prevent attribute changes (including time).
1270 	 */
1271 	if (a->todo & TODO_ACLS) {
1272 		int r2 = set_acls(a, a->fh,
1273 				  archive_entry_pathname_w(a->entry),
1274 				  archive_entry_acl(a->entry));
1275 		if (r2 < ret) ret = r2;
1276 	}
1277 
1278 	/* If there's an fd, we can close it now. */
1279 	if (a->fh != INVALID_HANDLE_VALUE) {
1280 		CloseHandle(a->fh);
1281 		a->fh = INVALID_HANDLE_VALUE;
1282 		if (a->tmpname) {
1283 			/* Windows does not support atomic rename */
1284 			disk_unlink(a->name);
1285 			if (_wrename(a->tmpname, a->name) != 0) {
1286 				la_dosmaperr(GetLastError());
1287 				archive_set_error(&a->archive, errno,
1288 				    "Failed to rename temporary file");
1289 				ret = ARCHIVE_FAILED;
1290 				disk_unlink(a->tmpname);
1291 			}
1292 			a->tmpname = NULL;
1293 		}
1294 	}
1295 	/* If there's an entry, we can release it now. */
1296 	archive_entry_free(a->entry);
1297 	a->entry = NULL;
1298 	a->archive.state = ARCHIVE_STATE_HEADER;
1299 	return (ret);
1300 }
1301 
1302 int
archive_write_disk_set_group_lookup(struct archive * _a,void * private_data,la_int64_t (* lookup_gid)(void * private,const char * gname,la_int64_t gid),void (* cleanup_gid)(void * private))1303 archive_write_disk_set_group_lookup(struct archive *_a,
1304     void *private_data,
1305     la_int64_t (*lookup_gid)(void *private, const char *gname, la_int64_t gid),
1306     void (*cleanup_gid)(void *private))
1307 {
1308 	struct archive_write_disk *a = (struct archive_write_disk *)_a;
1309 	archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1310 	    ARCHIVE_STATE_ANY, "archive_write_disk_set_group_lookup");
1311 
1312 	if (a->cleanup_gid != NULL && a->lookup_gid_data != NULL)
1313 		(a->cleanup_gid)(a->lookup_gid_data);
1314 
1315 	a->lookup_gid = lookup_gid;
1316 	a->cleanup_gid = cleanup_gid;
1317 	a->lookup_gid_data = private_data;
1318 	return (ARCHIVE_OK);
1319 }
1320 
1321 int
archive_write_disk_set_user_lookup(struct archive * _a,void * private_data,int64_t (* lookup_uid)(void * private,const char * uname,int64_t uid),void (* cleanup_uid)(void * private))1322 archive_write_disk_set_user_lookup(struct archive *_a,
1323     void *private_data,
1324     int64_t (*lookup_uid)(void *private, const char *uname, int64_t uid),
1325     void (*cleanup_uid)(void *private))
1326 {
1327 	struct archive_write_disk *a = (struct archive_write_disk *)_a;
1328 	archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1329 	    ARCHIVE_STATE_ANY, "archive_write_disk_set_user_lookup");
1330 
1331 	if (a->cleanup_uid != NULL && a->lookup_uid_data != NULL)
1332 		(a->cleanup_uid)(a->lookup_uid_data);
1333 
1334 	a->lookup_uid = lookup_uid;
1335 	a->cleanup_uid = cleanup_uid;
1336 	a->lookup_uid_data = private_data;
1337 	return (ARCHIVE_OK);
1338 }
1339 
1340 int64_t
archive_write_disk_gid(struct archive * _a,const char * name,la_int64_t id)1341 archive_write_disk_gid(struct archive *_a, const char *name, la_int64_t id)
1342 {
1343        struct archive_write_disk *a = (struct archive_write_disk *)_a;
1344        archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1345            ARCHIVE_STATE_ANY, "archive_write_disk_gid");
1346        if (a->lookup_gid)
1347                return (a->lookup_gid)(a->lookup_gid_data, name, id);
1348        return (id);
1349 }
1350 
1351 int64_t
archive_write_disk_uid(struct archive * _a,const char * name,la_int64_t id)1352 archive_write_disk_uid(struct archive *_a, const char *name, la_int64_t id)
1353 {
1354        struct archive_write_disk *a = (struct archive_write_disk *)_a;
1355        archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1356            ARCHIVE_STATE_ANY, "archive_write_disk_uid");
1357        if (a->lookup_uid)
1358                return (a->lookup_uid)(a->lookup_uid_data, name, id);
1359        return (id);
1360 }
1361 
1362 /*
1363  * Create a new archive_write_disk object and initialize it with global state.
1364  */
1365 struct archive *
archive_write_disk_new(void)1366 archive_write_disk_new(void)
1367 {
1368 	struct archive_write_disk *a;
1369 
1370 	a = (struct archive_write_disk *)calloc(1, sizeof(*a));
1371 	if (a == NULL)
1372 		return (NULL);
1373 	a->archive.magic = ARCHIVE_WRITE_DISK_MAGIC;
1374 	/* We're ready to write a header immediately. */
1375 	a->archive.state = ARCHIVE_STATE_HEADER;
1376 	a->archive.vtable = archive_write_disk_vtable();
1377 	a->start_time = time(NULL);
1378 	/* Query and restore the umask. */
1379 	umask(a->user_umask = umask(0));
1380 	if (archive_wstring_ensure(&a->path_safe, 512) == NULL) {
1381 		free(a);
1382 		return (NULL);
1383 	}
1384 	return (&a->archive);
1385 }
1386 
1387 static int
disk_unlink(const wchar_t * path)1388 disk_unlink(const wchar_t *path)
1389 {
1390 	wchar_t *fullname;
1391 	int r;
1392 
1393 	r = _wunlink(path);
1394 	if (r != 0 && GetLastError() == ERROR_INVALID_NAME) {
1395 		fullname = __la_win_permissive_name_w(path);
1396 		r = _wunlink(fullname);
1397 		free(fullname);
1398 	}
1399 	return (r);
1400 }
1401 
1402 static int
disk_rmdir(const wchar_t * path)1403 disk_rmdir(const wchar_t *path)
1404 {
1405 	wchar_t *fullname;
1406 	int r;
1407 
1408 	r = _wrmdir(path);
1409 	if (r != 0 && GetLastError() == ERROR_INVALID_NAME) {
1410 		fullname = __la_win_permissive_name_w(path);
1411 		r = _wrmdir(fullname);
1412 		free(fullname);
1413 	}
1414 	return (r);
1415 }
1416 
1417 /*
1418  * The main restore function.
1419  */
1420 static int
restore_entry(struct archive_write_disk * a)1421 restore_entry(struct archive_write_disk *a)
1422 {
1423 	int ret = ARCHIVE_OK, en;
1424 
1425 	if (a->flags & ARCHIVE_EXTRACT_UNLINK && !S_ISDIR(a->mode)) {
1426 		/*
1427 		 * TODO: Fix this.  Apparently, there are platforms
1428 		 * that still allow root to hose the entire filesystem
1429 		 * by unlinking a dir.  The S_ISDIR() test above
1430 		 * prevents us from using unlink() here if the new
1431 		 * object is a dir, but that doesn't mean the old
1432 		 * object isn't a dir.
1433 		 */
1434 		if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS)
1435 			(void)clear_nochange_fflags(a);
1436 		if (disk_unlink(a->name) == 0) {
1437 			/* We removed it, reset cached stat. */
1438 			a->pst = NULL;
1439 		} else if (errno == ENOENT) {
1440 			/* File didn't exist, that's just as good. */
1441 		} else if (disk_rmdir(a->name) == 0) {
1442 			/* It was a dir, but now it's gone. */
1443 			a->pst = NULL;
1444 		} else {
1445 			/* We tried, but couldn't get rid of it. */
1446 			archive_set_error(&a->archive, errno,
1447 			    "Could not unlink");
1448 			return(ARCHIVE_FAILED);
1449 		}
1450 	}
1451 
1452 	/* Try creating it first; if this fails, we'll try to recover. */
1453 	en = create_filesystem_object(a);
1454 
1455 	if ((en == ENOTDIR || en == ENOENT)
1456 	    && !(a->flags & ARCHIVE_EXTRACT_NO_AUTODIR)) {
1457 		wchar_t *full;
1458 		/* If the parent dir doesn't exist, try creating it. */
1459 		create_parent_dir(a, a->name);
1460 		/* Now try to create the object again. */
1461 		full = __la_win_permissive_name_w(a->name);
1462 		if (full == NULL) {
1463 			en = EINVAL;
1464 		} else {
1465 			/* Remove multiple directories such as "a/../b../c" */
1466 			archive_wstrcpy(&(a->_name_data), full);
1467 			a->name = a->_name_data.s;
1468 			free(full);
1469 			en = create_filesystem_object(a);
1470 		}
1471 	}
1472 
1473 	if ((en == ENOENT) && (archive_entry_hardlink(a->entry) != NULL)) {
1474 		archive_set_error(&a->archive, en,
1475 			"Hard-link target '%s' does not exist.",
1476 			archive_entry_hardlink(a->entry));
1477 		return (ARCHIVE_FAILED);
1478 	}
1479 
1480 	if ((en == EISDIR || en == EEXIST)
1481 	    && (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
1482 		/* If we're not overwriting, we're done. */
1483 		if (S_ISDIR(a->mode)) {
1484 			/* Don't overwrite any settings on existing directories. */
1485 			a->todo = 0;
1486 		}
1487 		archive_entry_unset_size(a->entry);
1488 		return (ARCHIVE_OK);
1489 	}
1490 
1491 	/*
1492 	 * Some platforms return EISDIR if you call
1493 	 * open(O_WRONLY | O_EXCL | O_CREAT) on a directory, some
1494 	 * return EEXIST.  POSIX is ambiguous, requiring EISDIR
1495 	 * for open(O_WRONLY) on a dir and EEXIST for open(O_EXCL | O_CREAT)
1496 	 * on an existing item.
1497 	 */
1498 	if (en == EISDIR) {
1499 		/* A dir is in the way of a non-dir, rmdir it. */
1500 		if (disk_rmdir(a->name) != 0) {
1501 			archive_set_error(&a->archive, errno,
1502 			    "Can't remove already-existing dir");
1503 			return (ARCHIVE_FAILED);
1504 		}
1505 		a->pst = NULL;
1506 		/* Try again. */
1507 		en = create_filesystem_object(a);
1508 	} else if (en == EEXIST) {
1509 		mode_t st_mode;
1510 		mode_t lst_mode;
1511 		BY_HANDLE_FILE_INFORMATION lst;
1512 		/*
1513 		 * We know something is in the way, but we don't know what;
1514 		 * we need to find out before we go any further.
1515 		 */
1516 		int r = 0;
1517 		int dirlnk = 0;
1518 
1519 		/*
1520 		 * The SECURE_SYMLINK logic has already removed a
1521 		 * symlink to a dir if the client wants that.  So
1522 		 * follow the symlink if we're creating a dir.
1523 		 * If it's not a dir (or it's a broken symlink),
1524 		 * then don't follow it.
1525 		 *
1526 		 * Windows distinguishes file and directory symlinks.
1527 		 * A file symlink may erroneously point to a directory
1528 		 * and a directory symlink to a file. Windows does not follow
1529 		 * such symlinks. We always need both source and target
1530 		 * information.
1531 		 */
1532 		r = file_information(a, a->name, &lst, &lst_mode, 1);
1533 		if (r != 0) {
1534 			archive_set_error(&a->archive, errno,
1535 			    "Can't stat existing object");
1536 			return (ARCHIVE_FAILED);
1537 		} else if (S_ISLNK(lst_mode)) {
1538 			if (lst.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1539 				dirlnk = 1;
1540 			/* In case of a symlink we need target information */
1541 			r = file_information(a, a->name, &a->st, &st_mode, 0);
1542 			if (r != 0) {
1543 				a->st = lst;
1544 				st_mode = lst_mode;
1545 			}
1546 		} else {
1547 			a->st = lst;
1548 			st_mode = lst_mode;
1549 		}
1550 
1551 		/*
1552 		 * NO_OVERWRITE_NEWER doesn't apply to directories.
1553 		 */
1554 		if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER)
1555 		    &&  !S_ISDIR(st_mode)) {
1556 			if (!older(&(a->st), a->entry)) {
1557 				archive_entry_unset_size(a->entry);
1558 				return (ARCHIVE_OK);
1559 			}
1560 		}
1561 
1562 		/* If it's our archive, we're done. */
1563 		if (a->skip_file_set &&
1564 		    bhfi_dev(&a->st) == a->skip_file_dev &&
1565 		    bhfi_ino(&a->st) == a->skip_file_ino) {
1566 			archive_set_error(&a->archive, 0,
1567 			    "Refusing to overwrite archive");
1568 			return (ARCHIVE_FAILED);
1569 		}
1570 
1571 		if (!S_ISDIR(st_mode)) {
1572 			if (a->flags &
1573 			    ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS) {
1574 				(void)clear_nochange_fflags(a);
1575 			}
1576 			if ((a->flags & ARCHIVE_EXTRACT_SAFE_WRITES) &&
1577 				S_ISREG(st_mode)) {
1578 				int fd = la_mktemp(a);
1579 
1580 				if (fd == -1) {
1581 					la_dosmaperr(GetLastError());
1582 					archive_set_error(&a->archive, errno,
1583 					    "Can't create temporary file");
1584 					return (ARCHIVE_FAILED);
1585 				}
1586 				a->fh = (HANDLE)_get_osfhandle(fd);
1587 				if (a->fh == INVALID_HANDLE_VALUE) {
1588 					la_dosmaperr(GetLastError());
1589 					return (ARCHIVE_FAILED);
1590 				}
1591 				a->pst = NULL;
1592 				en = 0;
1593 			} else {
1594 				if (dirlnk) {
1595 					/* Edge case: dir symlink pointing
1596 					 * to a file */
1597 					if (disk_rmdir(a->name) != 0) {
1598 						archive_set_error(&a->archive,
1599 						    errno, "Can't unlink "
1600 						    "directory symlink");
1601 						return (ARCHIVE_FAILED);
1602 					}
1603 				} else {
1604 					if (disk_unlink(a->name) != 0) {
1605 						/* A non-dir is in the way,
1606 						 * unlink it. */
1607 						archive_set_error(&a->archive,
1608 						    errno, "Can't unlink "
1609 						    "already-existing object");
1610 						return (ARCHIVE_FAILED);
1611 					}
1612 				}
1613 				a->pst = NULL;
1614 				/* Try again. */
1615 				en = create_filesystem_object(a);
1616 			}
1617 		} else if (!S_ISDIR(a->mode)) {
1618 			/* A dir is in the way of a non-dir, rmdir it. */
1619 			if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS)
1620 				(void)clear_nochange_fflags(a);
1621 			if (disk_rmdir(a->name) != 0) {
1622 				archive_set_error(&a->archive, errno,
1623 				    "Can't remove already-existing dir");
1624 				return (ARCHIVE_FAILED);
1625 			}
1626 			/* Try again. */
1627 			en = create_filesystem_object(a);
1628 		} else {
1629 			/*
1630 			 * There's a dir in the way of a dir.  Don't
1631 			 * waste time with rmdir()/mkdir(), just fix
1632 			 * up the permissions on the existing dir.
1633 			 * Note that we don't change perms on existing
1634 			 * dirs unless _EXTRACT_PERM is specified.
1635 			 */
1636 			if ((a->mode != st_mode)
1637 			    && (a->todo & TODO_MODE_FORCE))
1638 				a->deferred |= (a->todo & TODO_MODE);
1639 			/* Ownership doesn't need deferred fixup. */
1640 			en = 0; /* Forget the EEXIST. */
1641 		}
1642 	}
1643 
1644 	if (en) {
1645 		/* Everything failed; give up here. */
1646 		archive_set_error(&a->archive, en, "Can't create '%ls'",
1647 		    a->name);
1648 		return (ARCHIVE_FAILED);
1649 	}
1650 
1651 	a->pst = NULL; /* Cached stat data no longer valid. */
1652 	return (ret);
1653 }
1654 
1655 /*
1656  * Returns 0 if creation succeeds, or else returns errno value from
1657  * the failed system call.   Note:  This function should only ever perform
1658  * a single system call.
1659  */
1660 static int
create_filesystem_object(struct archive_write_disk * a)1661 create_filesystem_object(struct archive_write_disk *a)
1662 {
1663 	/* Create the entry. */
1664 	const wchar_t *linkname;
1665 	wchar_t *fullname;
1666 	mode_t final_mode, mode;
1667 	int r;
1668 	DWORD attrs = 0;
1669 
1670 	/* We identify hard/symlinks according to the link names. */
1671 	/* Since link(2) and symlink(2) don't handle modes, we're done here. */
1672 	linkname = archive_entry_hardlink_w(a->entry);
1673 	if (linkname != NULL) {
1674 		wchar_t *linkfull, *namefull;
1675 
1676 		linkfull = __la_win_permissive_name_w(linkname);
1677 		namefull = __la_win_permissive_name_w(a->name);
1678 		if (linkfull == NULL || namefull == NULL) {
1679 			errno = EINVAL;
1680 			r = -1;
1681 		} else {
1682 			/*
1683 			 * Unlinking and linking here is really not atomic,
1684 			 * but doing it right, would require us to construct
1685 			 * an mktemplink() function, and then use _wrename().
1686 			 */
1687 			if (a->flags & ARCHIVE_EXTRACT_SAFE_WRITES) {
1688 				attrs = GetFileAttributesW(namefull);
1689 				if (attrs != INVALID_FILE_ATTRIBUTES) {
1690 					if (attrs & FILE_ATTRIBUTE_DIRECTORY)
1691 						disk_rmdir(namefull);
1692 					else
1693 						disk_unlink(namefull);
1694 				}
1695 			}
1696 			r = la_CreateHardLinkW(namefull, linkfull);
1697 			if (r == 0) {
1698 				la_dosmaperr(GetLastError());
1699 				r = errno;
1700 			} else
1701 				r = 0;
1702 		}
1703 		/*
1704 		 * New cpio and pax formats allow hardlink entries
1705 		 * to carry data, so we may have to open the file
1706 		 * for hardlink entries.
1707 		 *
1708 		 * If the hardlink was successfully created and
1709 		 * the archive doesn't have carry data for it,
1710 		 * consider it to be non-authoritative for meta data.
1711 		 * This is consistent with GNU tar and BSD pax.
1712 		 * If the hardlink does carry data, let the last
1713 		 * archive entry decide ownership.
1714 		 */
1715 		if (r == 0 && a->filesize <= 0) {
1716 			a->todo = 0;
1717 			a->deferred = 0;
1718 		} else if (r == 0 && a->filesize > 0) {
1719 			a->fh = CreateFileW(namefull, GENERIC_WRITE, 0, NULL,
1720 			    TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1721 			if (a->fh == INVALID_HANDLE_VALUE) {
1722 				la_dosmaperr(GetLastError());
1723 				r = errno;
1724 			}
1725 		}
1726 		free(linkfull);
1727 		free(namefull);
1728 		return (r);
1729 	}
1730 	linkname = archive_entry_symlink_w(a->entry);
1731 	if (linkname != NULL) {
1732 		/*
1733 		 * Unlinking and linking here is really not atomic,
1734 		 * but doing it right, would require us to construct
1735 		 * an mktemplink() function, and then use _wrename().
1736 		 */
1737 		attrs = GetFileAttributesW(a->name);
1738 		if (attrs != INVALID_FILE_ATTRIBUTES) {
1739 			if (attrs & FILE_ATTRIBUTE_DIRECTORY)
1740 				disk_rmdir(a->name);
1741 			else
1742 				disk_unlink(a->name);
1743 		}
1744 #if HAVE_SYMLINK
1745 		return symlink(linkname, a->name) ? errno : 0;
1746 #else
1747 		errno = 0;
1748 		r = la_CreateSymbolicLinkW((const wchar_t *)a->name, linkname,
1749 		    archive_entry_symlink_type(a->entry));
1750 		if (r == 0) {
1751 			if (errno == 0)
1752 				la_dosmaperr(GetLastError());
1753 			r = errno;
1754 		} else
1755 			r = 0;
1756 		return (r);
1757 #endif
1758 	}
1759 
1760 	/*
1761 	 * The remaining system calls all set permissions, so let's
1762 	 * try to take advantage of that to avoid an extra chmod()
1763 	 * call.  (Recall that umask is set to zero right now!)
1764 	 */
1765 
1766 	/* Mode we want for the final restored object (w/o file type bits). */
1767 	final_mode = a->mode & 07777;
1768 	/*
1769 	 * The mode that will actually be restored in this step.  Note
1770 	 * that SUID, SGID, etc, require additional work to ensure
1771 	 * security, so we never restore them at this point.
1772 	 */
1773 	mode = final_mode & 0777 & ~a->user_umask;
1774 
1775 	switch (a->mode & AE_IFMT) {
1776 	default:
1777 		/* POSIX requires that we fall through here. */
1778 		/* FALLTHROUGH */
1779 	case AE_IFREG:
1780 		a->tmpname = NULL;
1781 		fullname = a->name;
1782 		/* O_WRONLY | O_CREAT | O_EXCL */
1783 		a->fh = CreateFileW(fullname, GENERIC_WRITE, 0, NULL,
1784 		    CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
1785 		if (a->fh == INVALID_HANDLE_VALUE &&
1786 		    GetLastError() == ERROR_INVALID_NAME &&
1787 		    fullname == a->name) {
1788 			fullname = __la_win_permissive_name_w(a->name);
1789 			a->fh = CreateFileW(fullname, GENERIC_WRITE, 0, NULL,
1790 			    CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
1791 		}
1792 		if (a->fh == INVALID_HANDLE_VALUE) {
1793 			if (GetLastError() == ERROR_ACCESS_DENIED) {
1794 				DWORD attr;
1795 				/* Simulate an errno of POSIX system. */
1796 				attr = GetFileAttributesW(fullname);
1797 				if (attr == (DWORD)-1)
1798 					la_dosmaperr(GetLastError());
1799 				else if (attr & FILE_ATTRIBUTE_DIRECTORY)
1800 					errno = EISDIR;
1801 				else
1802 					errno = EACCES;
1803 			} else
1804 				la_dosmaperr(GetLastError());
1805 			r = 1;
1806 		} else
1807 			r = 0;
1808 		if (fullname != a->name)
1809 			free(fullname);
1810 		break;
1811 	case AE_IFCHR:
1812 	case AE_IFBLK:
1813 		/* TODO: Find a better way to warn about our inability
1814 		 * to restore a block device node. */
1815 		return (EINVAL);
1816 	case AE_IFDIR:
1817 		mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE;
1818 		fullname = a->name;
1819 		r = CreateDirectoryW(fullname, NULL);
1820 		if (r == 0 && GetLastError() == ERROR_INVALID_NAME &&
1821 			fullname == a->name) {
1822 			fullname = __la_win_permissive_name_w(a->name);
1823 			r = CreateDirectoryW(fullname, NULL);
1824 		}
1825 		if (r != 0) {
1826 			r = 0;
1827 			/* Defer setting dir times. */
1828 			a->deferred |= (a->todo & TODO_TIMES);
1829 			a->todo &= ~TODO_TIMES;
1830 			/* Never use an immediate chmod(). */
1831 			/* We can't avoid the chmod() entirely if EXTRACT_PERM
1832 			 * because of SysV SGID inheritance. */
1833 			if ((mode != final_mode)
1834 			    || (a->flags & ARCHIVE_EXTRACT_PERM))
1835 				a->deferred |= (a->todo & TODO_MODE);
1836 			a->todo &= ~TODO_MODE;
1837 		} else {
1838 			la_dosmaperr(GetLastError());
1839 			r = -1;
1840 		}
1841 		if (fullname != a->name)
1842 			free(fullname);
1843 		break;
1844 	case AE_IFIFO:
1845 		/* TODO: Find a better way to warn about our inability
1846 		 * to restore a fifo. */
1847 		return (EINVAL);
1848 	}
1849 
1850 	/* All the system calls above set errno on failure. */
1851 	if (r)
1852 		return (errno);
1853 
1854 	/* If we managed to set the final mode, we've avoided a chmod(). */
1855 	if (mode == final_mode)
1856 		a->todo &= ~TODO_MODE;
1857 	return (0);
1858 }
1859 
1860 /*
1861  * Cleanup function for archive_extract.  Mostly, this involves processing
1862  * the fixup list, which is used to address a number of problems:
1863  *   * Dir permissions might prevent us from restoring a file in that
1864  *     dir, so we restore the dir with minimum 0700 permissions first,
1865  *     then correct the mode at the end.
1866  *   * Similarly, the act of restoring a file touches the directory
1867  *     and changes the timestamp on the dir, so we have to touch-up dir
1868  *     timestamps at the end as well.
1869  *   * Some file flags can interfere with the restore by, for example,
1870  *     preventing the creation of hardlinks to those files.
1871  *   * Mac OS extended metadata includes ACLs, so must be deferred on dirs.
1872  *
1873  * Note that tar/cpio do not require that archives be in a particular
1874  * order; there is no way to know when the last file has been restored
1875  * within a directory, so there's no way to optimize the memory usage
1876  * here by fixing up the directory any earlier than the
1877  * end-of-archive.
1878  *
1879  * XXX TODO: Directory ACLs should be restored here, for the same
1880  * reason we set directory perms here. XXX
1881  */
1882 static int
_archive_write_disk_close(struct archive * _a)1883 _archive_write_disk_close(struct archive *_a)
1884 {
1885 	struct archive_write_disk *a = (struct archive_write_disk *)_a;
1886 	struct fixup_entry *next, *p;
1887 	int ret;
1888 
1889 	archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1890 	    ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1891 	    "archive_write_disk_close");
1892 	ret = _archive_write_disk_finish_entry(&a->archive);
1893 
1894 	/* Sort dir list so directories are fixed up in depth-first order. */
1895 	p = sort_dir_list(a->fixup_list);
1896 
1897 	while (p != NULL) {
1898 		a->pst = NULL; /* Mark stat cache as out-of-date. */
1899 		if (p->fixup & TODO_TIMES) {
1900 			set_times(a, INVALID_HANDLE_VALUE, p->mode, p->name,
1901 			    p->atime, p->atime_nanos,
1902 			    p->birthtime, p->birthtime_nanos,
1903 			    p->mtime, p->mtime_nanos,
1904 			    p->ctime, p->ctime_nanos);
1905 		}
1906 		if (p->fixup & TODO_MODE_BASE)
1907 			la_chmod(p->name, p->mode);
1908 		if (p->fixup & TODO_ACLS)
1909 			set_acls(a, INVALID_HANDLE_VALUE, p->name, &p->acl);
1910 		if (p->fixup & TODO_FFLAGS)
1911 			set_fflags_platform(p->name, p->fflags_set, 0);
1912 		next = p->next;
1913 		archive_acl_clear(&p->acl);
1914 		free(p->name);
1915 		free(p);
1916 		p = next;
1917 	}
1918 	a->fixup_list = NULL;
1919 	return (ret);
1920 }
1921 
1922 static int
_archive_write_disk_free(struct archive * _a)1923 _archive_write_disk_free(struct archive *_a)
1924 {
1925 	struct archive_write_disk *a;
1926 	int ret;
1927 	if (_a == NULL)
1928 		return (ARCHIVE_OK);
1929 	archive_check_magic(_a, ARCHIVE_WRITE_DISK_MAGIC,
1930 	    ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_disk_free");
1931 	a = (struct archive_write_disk *)_a;
1932 	ret = _archive_write_disk_close(&a->archive);
1933 	archive_write_disk_set_group_lookup(&a->archive, NULL, NULL, NULL);
1934 	archive_write_disk_set_user_lookup(&a->archive, NULL, NULL, NULL);
1935 	archive_entry_free(a->entry);
1936 	archive_wstring_free(&a->_name_data);
1937 	archive_wstring_free(&a->_tmpname_data);
1938 	archive_string_free(&a->archive.error_string);
1939 	archive_wstring_free(&a->path_safe);
1940 	a->archive.magic = 0;
1941 	__archive_clean(&a->archive);
1942 	free(a);
1943 	return (ret);
1944 }
1945 
1946 /*
1947  * Simple O(n log n) merge sort to order the fixup list.  In
1948  * particular, we want to restore dir timestamps depth-first.
1949  */
1950 static struct fixup_entry *
sort_dir_list(struct fixup_entry * p)1951 sort_dir_list(struct fixup_entry *p)
1952 {
1953 	struct fixup_entry *a, *b, *t;
1954 
1955 	if (p == NULL)
1956 		return (NULL);
1957 	/* A one-item list is already sorted. */
1958 	if (p->next == NULL)
1959 		return (p);
1960 
1961 	/* Step 1: split the list. */
1962 	t = p;
1963 	a = p->next->next;
1964 	while (a != NULL) {
1965 		/* Step a twice, t once. */
1966 		a = a->next;
1967 		if (a != NULL)
1968 			a = a->next;
1969 		t = t->next;
1970 	}
1971 	/* Now, t is at the mid-point, so break the list here. */
1972 	b = t->next;
1973 	t->next = NULL;
1974 	a = p;
1975 
1976 	/* Step 2: Recursively sort the two sub-lists. */
1977 	a = sort_dir_list(a);
1978 	b = sort_dir_list(b);
1979 
1980 	/* Step 3: Merge the returned lists. */
1981 	/* Pick the first element for the merged list. */
1982 	if (wcscmp(a->name, b->name) > 0) {
1983 		t = p = a;
1984 		a = a->next;
1985 	} else {
1986 		t = p = b;
1987 		b = b->next;
1988 	}
1989 
1990 	/* Always put the later element on the list first. */
1991 	while (a != NULL && b != NULL) {
1992 		if (wcscmp(a->name, b->name) > 0) {
1993 			t->next = a;
1994 			a = a->next;
1995 		} else {
1996 			t->next = b;
1997 			b = b->next;
1998 		}
1999 		t = t->next;
2000 	}
2001 
2002 	/* Only one list is non-empty, so just splice it on. */
2003 	if (a != NULL)
2004 		t->next = a;
2005 	if (b != NULL)
2006 		t->next = b;
2007 
2008 	return (p);
2009 }
2010 
2011 /*
2012  * Returns a new, initialized fixup entry.
2013  *
2014  * TODO: Reduce the memory requirements for this list by using a tree
2015  * structure rather than a simple list of names.
2016  */
2017 static struct fixup_entry *
new_fixup(struct archive_write_disk * a,const wchar_t * pathname)2018 new_fixup(struct archive_write_disk *a, const wchar_t *pathname)
2019 {
2020 	struct fixup_entry *fe;
2021 
2022 	fe = (struct fixup_entry *)calloc(1, sizeof(struct fixup_entry));
2023 	if (fe == NULL)
2024 		return (NULL);
2025 	fe->next = a->fixup_list;
2026 	a->fixup_list = fe;
2027 	fe->fixup = 0;
2028 	fe->name = _wcsdup(pathname);
2029 	fe->fflags_set = 0;
2030 	return (fe);
2031 }
2032 
2033 /*
2034  * Returns a fixup structure for the current entry.
2035  */
2036 static struct fixup_entry *
current_fixup(struct archive_write_disk * a,const wchar_t * pathname)2037 current_fixup(struct archive_write_disk *a, const wchar_t *pathname)
2038 {
2039 	if (a->current_fixup == NULL)
2040 		a->current_fixup = new_fixup(a, pathname);
2041 	return (a->current_fixup);
2042 }
2043 
2044 /*
2045  * TODO: The deep-directory support bypasses this; disable deep directory
2046  * support if we're doing symlink checks.
2047  */
2048 /*
2049  * TODO: Someday, integrate this with the deep dir support; they both
2050  * scan the path and both can be optimized by comparing against other
2051  * recent paths.
2052  */
2053 static int
check_symlinks(struct archive_write_disk * a)2054 check_symlinks(struct archive_write_disk *a)
2055 {
2056 	wchar_t *pn, *p;
2057 	wchar_t c;
2058 	int r;
2059 	BY_HANDLE_FILE_INFORMATION st;
2060 	mode_t st_mode;
2061 
2062 	/*
2063 	 * Guard against symlink tricks.  Reject any archive entry whose
2064 	 * destination would be altered by a symlink.
2065 	 */
2066 	/* Whatever we checked last time doesn't need to be re-checked. */
2067 	pn = a->name;
2068 	p = a->path_safe.s;
2069 	while ((*pn != '\0') && (*p == *pn))
2070 		++p, ++pn;
2071 	/* Skip leading backslashes */
2072 	while (*pn == '\\')
2073 		++pn;
2074 	c = pn[0];
2075 	/* Keep going until we've checked the entire name. */
2076 	while (pn[0] != '\0' && (pn[0] != '\\' || pn[1] != '\0')) {
2077 		/* Skip the next path element. */
2078 		while (*pn != '\0' && *pn != '\\')
2079 			++pn;
2080 		c = pn[0];
2081 		pn[0] = '\0';
2082 		/* Check that we haven't hit a symlink. */
2083 		r = file_information(a, a->name, &st, &st_mode, 1);
2084 		if (r != 0) {
2085 			/* We've hit a dir that doesn't exist; stop now. */
2086 			if (errno == ENOENT)
2087 				break;
2088 		} else if (S_ISLNK(st_mode)) {
2089 			if (c == '\0') {
2090 				/*
2091 				 * Last element is a file or directory symlink.
2092 				 * Remove it so we can overwrite it with the
2093 				 * item being extracted.
2094 				 */
2095 				if (a->flags &
2096 				    ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS) {
2097 					(void)clear_nochange_fflags(a);
2098 				}
2099 				if (st.dwFileAttributes &
2100 				    FILE_ATTRIBUTE_DIRECTORY) {
2101 					r = disk_rmdir(a->name);
2102 				} else {
2103 					r = disk_unlink(a->name);
2104 				}
2105 				if (r) {
2106 					archive_set_error(&a->archive, errno,
2107 					    "Could not remove symlink %ls",
2108 					    a->name);
2109 					pn[0] = c;
2110 					return (ARCHIVE_FAILED);
2111 				}
2112 				a->pst = NULL;
2113 				/*
2114 				 * Even if we did remove it, a warning
2115 				 * is in order.  The warning is silly,
2116 				 * though, if we're just replacing one
2117 				 * symlink with another symlink.
2118 				 */
2119 				if (!S_ISLNK(a->mode)) {
2120 					archive_set_error(&a->archive, 0,
2121 					    "Removing symlink %ls",
2122 					    a->name);
2123 				}
2124 				/* Symlink gone.  No more problem! */
2125 				pn[0] = c;
2126 				return (0);
2127 			} else if (a->flags & ARCHIVE_EXTRACT_UNLINK) {
2128 				/* User asked us to remove problems. */
2129 				if (a->flags &
2130 				    ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS) {
2131 					(void)clear_nochange_fflags(a);
2132 				}
2133 				if (st.dwFileAttributes &
2134 				    FILE_ATTRIBUTE_DIRECTORY) {
2135 					r = disk_rmdir(a->name);
2136 				} else {
2137 					r = disk_unlink(a->name);
2138 				}
2139 				if (r != 0) {
2140 					archive_set_error(&a->archive, 0,
2141 					    "Cannot remove intervening "
2142 					    "symlink %ls", a->name);
2143 					pn[0] = c;
2144 					return (ARCHIVE_FAILED);
2145 				}
2146 				a->pst = NULL;
2147 			} else {
2148 				archive_set_error(&a->archive, 0,
2149 				    "Cannot extract through symlink %ls",
2150 				    a->name);
2151 				pn[0] = c;
2152 				return (ARCHIVE_FAILED);
2153 			}
2154 		}
2155 		pn[0] = c;
2156 		pn++;
2157 	}
2158 	pn[0] = c;
2159 	/* We've checked and/or cleaned the whole path, so remember it. */
2160 	archive_wstrcpy(&a->path_safe, a->name);
2161 	return (ARCHIVE_OK);
2162 }
2163 
2164 static int
guidword(wchar_t * p,int n)2165 guidword(wchar_t *p, int n)
2166 {
2167 	int i;
2168 
2169 	for (i = 0; i < n; i++) {
2170 		if ((*p >= L'0' && *p <= L'9') ||
2171 		    (*p >= L'a' && *p <= L'f') ||
2172 		    (*p >= L'A' && *p <= L'F'))
2173 			p++;
2174 		else
2175 			return (-1);
2176 	}
2177 	return (0);
2178 }
2179 
2180 /*
2181  * Canonicalize the pathname.  In particular, this strips duplicate
2182  * '\' characters, '.' elements, and trailing '\'.  It also raises an
2183  * error for an empty path, a trailing '..' or (if _SECURE_NODOTDOT is
2184  * set) any '..' in the path.
2185  */
2186 static int
cleanup_pathname(struct archive_write_disk * a)2187 cleanup_pathname(struct archive_write_disk *a)
2188 {
2189 	wchar_t *dest, *src, *p, *top;
2190 	wchar_t separator = L'\0';
2191 
2192 	p = a->name;
2193 	if (*p == L'\0') {
2194 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2195 		    "Invalid empty pathname");
2196 		return (ARCHIVE_FAILED);
2197 	}
2198 
2199 	/* Replace '/' by '\' */
2200 	for (; *p != L'\0'; p++) {
2201 		if (*p == L'/')
2202 			*p = L'\\';
2203 	}
2204 	p = a->name;
2205 
2206 	/* Skip leading "\\.\" or "\\?\" or "\\?\UNC\" or
2207 	 * "\\?\Volume{GUID}\"
2208 	 * (absolute path prefixes used by Windows API) */
2209 	if (p[0] == L'\\' && p[1] == L'\\' &&
2210 	    (p[2] == L'.' || p[2] == L'?') && p[3] ==  L'\\')
2211 	{
2212 		/* A path begin with "\\?\UNC\" */
2213 		if (p[2] == L'?' &&
2214 		    (p[4] == L'U' || p[4] == L'u') &&
2215 		    (p[5] == L'N' || p[5] == L'n') &&
2216 		    (p[6] == L'C' || p[6] == L'c') &&
2217 		    p[7] == L'\\')
2218 			p += 8;
2219 		/* A path begin with "\\?\Volume{GUID}\" */
2220 		else if (p[2] == L'?' &&
2221 		    (p[4] == L'V' || p[4] == L'v') &&
2222 		    (p[5] == L'O' || p[5] == L'o') &&
2223 		    (p[6] == L'L' || p[6] == L'l') &&
2224 		    (p[7] == L'U' || p[7] == L'u') &&
2225 		    (p[8] == L'M' || p[8] == L'm') &&
2226 		    (p[9] == L'E' || p[9] == L'e') &&
2227 		    p[10] == L'{') {
2228 			if (guidword(p+11, 8) == 0 && p[19] == L'-' &&
2229 			    guidword(p+20, 4) == 0 && p[24] == L'-' &&
2230 			    guidword(p+25, 4) == 0 && p[29] == L'-' &&
2231 			    guidword(p+30, 4) == 0 && p[34] == L'-' &&
2232 			    guidword(p+35, 12) == 0 && p[47] == L'}' &&
2233 			    p[48] == L'\\')
2234 				p += 49;
2235 			else
2236 				p += 4;
2237 		/* A path begin with "\\.\PhysicalDriveX" */
2238 		} else if (p[2] == L'.' &&
2239 		    (p[4] == L'P' || p[4] == L'p') &&
2240 		    (p[5] == L'H' || p[5] == L'h') &&
2241 		    (p[6] == L'Y' || p[6] == L'y') &&
2242 		    (p[7] == L'S' || p[7] == L's') &&
2243 		    (p[8] == L'I' || p[8] == L'i') &&
2244 		    (p[9] == L'C' || p[9] == L'c') &&
2245 		    (p[9] == L'A' || p[9] == L'a') &&
2246 		    (p[9] == L'L' || p[9] == L'l') &&
2247 		    (p[9] == L'D' || p[9] == L'd') &&
2248 		    (p[9] == L'R' || p[9] == L'r') &&
2249 		    (p[9] == L'I' || p[9] == L'i') &&
2250 		    (p[9] == L'V' || p[9] == L'v') &&
2251 		    (p[9] == L'E' || p[9] == L'e') &&
2252 		    (p[10] >= L'0' && p[10] <= L'9') &&
2253 		    p[11] == L'\0') {
2254 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2255 			    "Path is a physical drive name");
2256 			return (ARCHIVE_FAILED);
2257 		} else
2258 			p += 4;
2259 	}
2260 
2261 	/* Skip leading drive letter from archives created
2262 	 * on Windows. */
2263 	if (((p[0] >= L'a' && p[0] <= L'z') ||
2264 	     (p[0] >= L'A' && p[0] <= L'Z')) &&
2265 		 p[1] == L':') {
2266 		if (p[2] == L'\0') {
2267 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2268 			    "Path is a drive name");
2269 			return (ARCHIVE_FAILED);
2270 		}
2271 		if (p[2] == L'\\')
2272 			p += 2;
2273 	}
2274 
2275 	top = dest = src = p;
2276 	/* Rewrite the path name if its character is a unusable. */
2277 	for (; *p != L'\0'; p++) {
2278 		if (*p == L':' || *p == L'*' || *p == L'?' || *p == L'"' ||
2279 		    *p == L'<' || *p == L'>' || *p == L'|')
2280 			*p = L'_';
2281 	}
2282 	/* Skip leading '\'. */
2283 	if (*src == L'\\')
2284 		separator = *src++;
2285 
2286 	/* Scan the pathname one element at a time. */
2287 	for (;;) {
2288 		/* src points to first char after '\' */
2289 		if (src[0] == L'\0') {
2290 			break;
2291 		} else if (src[0] == L'\\') {
2292 			/* Found '\\'('//'), ignore second one. */
2293 			src++;
2294 			continue;
2295 		} else if (src[0] == L'.') {
2296 			if (src[1] == L'\0') {
2297 				/* Ignore trailing '.' */
2298 				break;
2299 			} else if (src[1] == L'\\') {
2300 				/* Skip '.\'. */
2301 				src += 2;
2302 				continue;
2303 			} else if (src[1] == L'.') {
2304 				if (src[2] == L'\\' || src[2] == L'\0') {
2305 					/* Conditionally warn about '..' */
2306 					if (a->flags &
2307 					    ARCHIVE_EXTRACT_SECURE_NODOTDOT) {
2308 						archive_set_error(&a->archive,
2309 						    ARCHIVE_ERRNO_MISC,
2310 						    "Path contains '..'");
2311 						return (ARCHIVE_FAILED);
2312 					}
2313 				}
2314 				/*
2315 				 * Note: Under no circumstances do we
2316 				 * remove '..' elements.  In
2317 				 * particular, restoring
2318 				 * '\foo\..\bar\' should create the
2319 				 * 'foo' dir as a side-effect.
2320 				 */
2321 			}
2322 		}
2323 
2324 		/* Copy current element, including leading '\'. */
2325 		if (separator)
2326 			*dest++ = L'\\';
2327 		while (*src != L'\0' && *src != L'\\') {
2328 			*dest++ = *src++;
2329 		}
2330 
2331 		if (*src == L'\0')
2332 			break;
2333 
2334 		/* Skip '\' separator. */
2335 		separator = *src++;
2336 	}
2337 	/*
2338 	 * We've just copied zero or more path elements, not including the
2339 	 * final '\'.
2340 	 */
2341 	if (dest == top) {
2342 		/*
2343 		 * Nothing got copied.  The path must have been something
2344 		 * like '.' or '\' or './' or '/././././/./'.
2345 		 */
2346 		if (separator)
2347 			*dest++ = L'\\';
2348 		else
2349 			*dest++ = L'.';
2350 	}
2351 	/* Terminate the result. */
2352 	*dest = L'\0';
2353 	return (ARCHIVE_OK);
2354 }
2355 
2356 /*
2357  * Create the parent directory of the specified path, assuming path
2358  * is already in mutable storage.
2359  */
2360 static int
create_parent_dir(struct archive_write_disk * a,wchar_t * path)2361 create_parent_dir(struct archive_write_disk *a, wchar_t *path)
2362 {
2363 	wchar_t *slash;
2364 	int r;
2365 
2366 	/* Remove tail element to obtain parent name. */
2367 	slash = wcsrchr(path, L'\\');
2368 	if (slash == NULL)
2369 		return (ARCHIVE_OK);
2370 	*slash = L'\0';
2371 	r = create_dir(a, path);
2372 	*slash = L'\\';
2373 	return (r);
2374 }
2375 
2376 /*
2377  * Create the specified dir, recursing to create parents as necessary.
2378  *
2379  * Returns ARCHIVE_OK if the path exists when we're done here.
2380  * Otherwise, returns ARCHIVE_FAILED.
2381  * Assumes path is in mutable storage; path is unchanged on exit.
2382  */
2383 static int
create_dir(struct archive_write_disk * a,wchar_t * path)2384 create_dir(struct archive_write_disk *a, wchar_t *path)
2385 {
2386 	BY_HANDLE_FILE_INFORMATION st;
2387 	struct fixup_entry *le;
2388 	wchar_t *slash, *base, *full;
2389 	mode_t mode_final, mode, st_mode;
2390 	int r;
2391 
2392 	/* Check for special names and just skip them. */
2393 	slash = wcsrchr(path, L'\\');
2394 	if (slash == NULL)
2395 		base = path;
2396 	else
2397 		base = slash + 1;
2398 
2399 	if (base[0] == L'\0' ||
2400 	    (base[0] == L'.' && base[1] == L'\0') ||
2401 	    (base[0] == L'.' && base[1] == L'.' && base[2] == L'\0')) {
2402 		/* Don't bother trying to create null path, '.', or '..'. */
2403 		if (slash != NULL) {
2404 			*slash = L'\0';
2405 			r = create_dir(a, path);
2406 			*slash = L'\\';
2407 			return (r);
2408 		}
2409 		return (ARCHIVE_OK);
2410 	}
2411 
2412 	/*
2413 	 * Yes, this should be stat() and not lstat().  Using lstat()
2414 	 * here loses the ability to extract through symlinks.  Also note
2415 	 * that this should not use the a->st cache.
2416 	 */
2417 	if (file_information(a, path, &st, &st_mode, 0) == 0) {
2418 		if (S_ISDIR(st_mode))
2419 			return (ARCHIVE_OK);
2420 		if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
2421 			archive_set_error(&a->archive, EEXIST,
2422 			    "Can't create directory '%ls'", path);
2423 			return (ARCHIVE_FAILED);
2424 		}
2425 		if (disk_unlink(path) != 0) {
2426 			archive_set_error(&a->archive, errno,
2427 			    "Can't create directory '%ls': "
2428 			    "Conflicting file cannot be removed",
2429 			    path);
2430 			return (ARCHIVE_FAILED);
2431 		}
2432 	} else if (errno != ENOENT && errno != ENOTDIR) {
2433 		/* Stat failed? */
2434 		archive_set_error(&a->archive, errno,
2435 		    "Can't test directory '%ls'", path);
2436 		return (ARCHIVE_FAILED);
2437 	} else if (slash != NULL) {
2438 		*slash = '\0';
2439 		r = create_dir(a, path);
2440 		*slash = '\\';
2441 		if (r != ARCHIVE_OK)
2442 			return (r);
2443 	}
2444 
2445 	/*
2446 	 * Mode we want for the final restored directory.  Per POSIX,
2447 	 * implicitly-created dirs must be created obeying the umask.
2448 	 * There's no mention whether this is different for privileged
2449 	 * restores (which the rest of this code handles by pretending
2450 	 * umask=0).  I've chosen here to always obey the user's umask for
2451 	 * implicit dirs, even if _EXTRACT_PERM was specified.
2452 	 */
2453 	mode_final = DEFAULT_DIR_MODE & ~a->user_umask;
2454 	/* Mode we want on disk during the restore process. */
2455 	mode = mode_final;
2456 	mode |= MINIMUM_DIR_MODE;
2457 	mode &= MAXIMUM_DIR_MODE;
2458 	/*
2459 	 * Apply __la_win_permissive_name_w to path in order to
2460 	 * remove '../' path string.
2461 	 */
2462 	full = __la_win_permissive_name_w(path);
2463 	if (full == NULL)
2464 		errno = EINVAL;
2465 	else if (CreateDirectoryW(full, NULL) != 0) {
2466 		if (mode != mode_final) {
2467 			le = new_fixup(a, path);
2468 			le->fixup |=TODO_MODE_BASE;
2469 			le->mode = mode_final;
2470 		}
2471 		free(full);
2472 		return (ARCHIVE_OK);
2473 	} else {
2474 		la_dosmaperr(GetLastError());
2475 	}
2476 	free(full);
2477 
2478 	/*
2479 	 * Without the following check, a/b/../b/c/d fails at the
2480 	 * second visit to 'b', so 'd' can't be created.  Note that we
2481 	 * don't add it to the fixup list here, as it's already been
2482 	 * added.
2483 	 */
2484 	if (file_information(a, path, &st, &st_mode, 0) == 0 &&
2485 	    S_ISDIR(st_mode))
2486 		return (ARCHIVE_OK);
2487 
2488 	archive_set_error(&a->archive, errno, "Failed to create dir '%ls'",
2489 	    path);
2490 	return (ARCHIVE_FAILED);
2491 }
2492 
2493 /*
2494  * Note: Although we can skip setting the user id if the desired user
2495  * id matches the current user, we cannot skip setting the group, as
2496  * many systems set the gid based on the containing directory.  So
2497  * we have to perform a chown syscall if we want to set the SGID
2498  * bit.  (The alternative is to stat() and then possibly chown(); it's
2499  * more efficient to skip the stat() and just always chown().)  Note
2500  * that a successful chown() here clears the TODO_SGID_CHECK bit, which
2501  * allows set_mode to skip the stat() check for the GID.
2502  */
2503 static int
set_ownership(struct archive_write_disk * a)2504 set_ownership(struct archive_write_disk *a)
2505 {
2506 /* unfortunately, on win32 there is no 'root' user with uid 0,
2507    so we just have to try the chown and see if it works */
2508 
2509 	/* If we know we can't change it, don't bother trying. */
2510 	if (a->user_uid != 0  &&  a->user_uid != a->uid) {
2511 		archive_set_error(&a->archive, errno,
2512 		    "Can't set UID=%jd", (intmax_t)a->uid);
2513 		return (ARCHIVE_WARN);
2514 	}
2515 
2516 	archive_set_error(&a->archive, errno,
2517 	    "Can't set user=%jd/group=%jd for %ls",
2518 	    (intmax_t)a->uid, (intmax_t)a->gid, a->name);
2519 	return (ARCHIVE_WARN);
2520 }
2521 
2522 static int
set_times(struct archive_write_disk * a,HANDLE h,int mode,const wchar_t * name,time_t atime,long atime_nanos,time_t birthtime,long birthtime_nanos,time_t mtime,long mtime_nanos,time_t ctime_sec,long ctime_nanos)2523 set_times(struct archive_write_disk *a,
2524     HANDLE h, int mode, const wchar_t *name,
2525     time_t atime, long atime_nanos,
2526     time_t birthtime, long birthtime_nanos,
2527     time_t mtime, long mtime_nanos,
2528     time_t ctime_sec, long ctime_nanos)
2529 {
2530 #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
2531 #define WINTIME(sec, nsec) ((Int32x32To64(sec, 10000000) + EPOC_TIME)\
2532 	 + (((nsec)/1000)*10))
2533 
2534 	HANDLE hw = 0;
2535 	ULARGE_INTEGER wintm;
2536 	FILETIME *pfbtime;
2537 	FILETIME fatime, fbtime, fmtime;
2538 
2539 	(void)ctime_sec; /* UNUSED */
2540 	(void)ctime_nanos; /* UNUSED */
2541 
2542 	if (h != INVALID_HANDLE_VALUE) {
2543 		hw = NULL;
2544 	} else {
2545 		wchar_t *ws;
2546 
2547 		if (S_ISLNK(mode))
2548 			return (ARCHIVE_OK);
2549 		ws = __la_win_permissive_name_w(name);
2550 		if (ws == NULL)
2551 			goto settimes_failed;
2552 		hw = CreateFileW(ws, FILE_WRITE_ATTRIBUTES,
2553 		    0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
2554 		free(ws);
2555 		if (hw == INVALID_HANDLE_VALUE)
2556 			goto settimes_failed;
2557 		h = hw;
2558 	}
2559 
2560 	wintm.QuadPart = WINTIME(atime, atime_nanos);
2561 	fatime.dwLowDateTime = wintm.LowPart;
2562 	fatime.dwHighDateTime = wintm.HighPart;
2563 	wintm.QuadPart = WINTIME(mtime, mtime_nanos);
2564 	fmtime.dwLowDateTime = wintm.LowPart;
2565 	fmtime.dwHighDateTime = wintm.HighPart;
2566 	/*
2567 	 * SetFileTime() supports birthtime.
2568 	 */
2569 	if (birthtime > 0 || birthtime_nanos > 0) {
2570 		wintm.QuadPart = WINTIME(birthtime, birthtime_nanos);
2571 		fbtime.dwLowDateTime = wintm.LowPart;
2572 		fbtime.dwHighDateTime = wintm.HighPart;
2573 		pfbtime = &fbtime;
2574 	} else
2575 		pfbtime = NULL;
2576 	if (SetFileTime(h, pfbtime, &fatime, &fmtime) == 0)
2577 		goto settimes_failed;
2578 	CloseHandle(hw);
2579 	return (ARCHIVE_OK);
2580 
2581 settimes_failed:
2582 	CloseHandle(hw);
2583 	archive_set_error(&a->archive, EINVAL, "Can't restore time");
2584 	return (ARCHIVE_WARN);
2585 }
2586 
2587 static int
set_times_from_entry(struct archive_write_disk * a)2588 set_times_from_entry(struct archive_write_disk *a)
2589 {
2590 	time_t atime, birthtime, mtime, ctime_sec;
2591 	long atime_nsec, birthtime_nsec, mtime_nsec, ctime_nsec;
2592 
2593 	/* Suitable defaults. */
2594 	atime = birthtime = mtime = ctime_sec = a->start_time;
2595 	atime_nsec = birthtime_nsec = mtime_nsec = ctime_nsec = 0;
2596 
2597 	/* If no time was provided, we're done. */
2598 	if (!archive_entry_atime_is_set(a->entry)
2599 	    && !archive_entry_birthtime_is_set(a->entry)
2600 	    && !archive_entry_mtime_is_set(a->entry))
2601 		return (ARCHIVE_OK);
2602 
2603 	if (archive_entry_atime_is_set(a->entry)) {
2604 		atime = archive_entry_atime(a->entry);
2605 		atime_nsec = archive_entry_atime_nsec(a->entry);
2606 	}
2607 	if (archive_entry_birthtime_is_set(a->entry)) {
2608 		birthtime = archive_entry_birthtime(a->entry);
2609 		birthtime_nsec = archive_entry_birthtime_nsec(a->entry);
2610 	}
2611 	if (archive_entry_mtime_is_set(a->entry)) {
2612 		mtime = archive_entry_mtime(a->entry);
2613 		mtime_nsec = archive_entry_mtime_nsec(a->entry);
2614 	}
2615 	if (archive_entry_ctime_is_set(a->entry)) {
2616 		ctime_sec = archive_entry_ctime(a->entry);
2617 		ctime_nsec = archive_entry_ctime_nsec(a->entry);
2618 	}
2619 
2620 	return set_times(a, a->fh, a->mode, a->name,
2621 			 atime, atime_nsec,
2622 			 birthtime, birthtime_nsec,
2623 			 mtime, mtime_nsec,
2624 			 ctime_sec, ctime_nsec);
2625 }
2626 
2627 static int
set_mode(struct archive_write_disk * a,int mode)2628 set_mode(struct archive_write_disk *a, int mode)
2629 {
2630 	int r = ARCHIVE_OK;
2631 	mode &= 07777; /* Strip off file type bits. */
2632 
2633 	if (a->todo & TODO_SGID_CHECK) {
2634 		/*
2635 		 * If we don't know the GID is right, we must stat()
2636 		 * to verify it.  We can't just check the GID of this
2637 		 * process, since systems sometimes set GID from
2638 		 * the enclosing dir or based on ACLs.
2639 		 */
2640 		if ((r = lazy_stat(a)) != ARCHIVE_OK)
2641 			return (r);
2642 		if (0 != a->gid) {
2643 			mode &= ~ S_ISGID;
2644 		}
2645 		/* While we're here, double-check the UID. */
2646 		if (0 != a->uid
2647 		    && (a->todo & TODO_SUID)) {
2648 			mode &= ~ S_ISUID;
2649 		}
2650 		a->todo &= ~TODO_SGID_CHECK;
2651 		a->todo &= ~TODO_SUID_CHECK;
2652 	} else if (a->todo & TODO_SUID_CHECK) {
2653 		/*
2654 		 * If we don't know the UID is right, we can just check
2655 		 * the user, since all systems set the file UID from
2656 		 * the process UID.
2657 		 */
2658 		if (a->user_uid != a->uid) {
2659 			mode &= ~ S_ISUID;
2660 		}
2661 		a->todo &= ~TODO_SUID_CHECK;
2662 	}
2663 
2664 	if (S_ISLNK(a->mode)) {
2665 #ifdef HAVE_LCHMOD
2666 		/*
2667 		 * If this is a symlink, use lchmod().  If the
2668 		 * platform doesn't support lchmod(), just skip it.  A
2669 		 * platform that doesn't provide a way to set
2670 		 * permissions on symlinks probably ignores
2671 		 * permissions on symlinks, so a failure here has no
2672 		 * impact.
2673 		 */
2674 		if (lchmod(a->name, mode) != 0) {
2675 			archive_set_error(&a->archive, errno,
2676 			    "Can't set permissions to 0%o", (int)mode);
2677 			r = ARCHIVE_WARN;
2678 		}
2679 #endif
2680 	} else if (!S_ISDIR(a->mode)) {
2681 		/*
2682 		 * If it's not a symlink and not a dir, then use
2683 		 * fchmod() or chmod(), depending on whether we have
2684 		 * an fd.  Dirs get their perms set during the
2685 		 * post-extract fixup, which is handled elsewhere.
2686 		 */
2687 #ifdef HAVE_FCHMOD
2688 		if (a->fd >= 0) {
2689 			if (fchmod(a->fd, mode) != 0) {
2690 				archive_set_error(&a->archive, errno,
2691 				    "Can't set permissions to 0%o", (int)mode);
2692 				r = ARCHIVE_WARN;
2693 			}
2694 		} else
2695 #endif
2696 			/* If this platform lacks fchmod(), then
2697 			 * we'll just use chmod(). */
2698 			if (la_chmod(a->name, mode) != 0) {
2699 				archive_set_error(&a->archive, errno,
2700 				    "Can't set permissions to 0%o", (int)mode);
2701 				r = ARCHIVE_WARN;
2702 			}
2703 	}
2704 	return (r);
2705 }
2706 
set_fflags_platform(const wchar_t * name,unsigned long fflags_set,unsigned long fflags_clear)2707 static int set_fflags_platform(const wchar_t *name, unsigned long fflags_set,
2708     unsigned long fflags_clear)
2709 {
2710 	DWORD oldflags, newflags;
2711 	wchar_t *fullname;
2712 
2713 	const DWORD settable_flags =
2714 	    FILE_ATTRIBUTE_ARCHIVE |
2715 	    FILE_ATTRIBUTE_HIDDEN |
2716 	    FILE_ATTRIBUTE_NORMAL |
2717 	    FILE_ATTRIBUTE_NOT_CONTENT_INDEXED |
2718 	    FILE_ATTRIBUTE_OFFLINE |
2719 	    FILE_ATTRIBUTE_READONLY |
2720 	    FILE_ATTRIBUTE_SYSTEM |
2721 	    FILE_ATTRIBUTE_TEMPORARY;
2722 
2723 	oldflags = GetFileAttributesW(name);
2724 	if (oldflags == (DWORD)-1 &&
2725 	    GetLastError() == ERROR_INVALID_NAME) {
2726 		fullname = __la_win_permissive_name_w(name);
2727 		oldflags = GetFileAttributesW(fullname);
2728 	}
2729 	if (oldflags == (DWORD)-1) {
2730 		la_dosmaperr(GetLastError());
2731 		return (ARCHIVE_WARN);
2732 	}
2733 	newflags = ((oldflags & ~fflags_clear) | fflags_set) & settable_flags;
2734 	if(SetFileAttributesW(name, newflags) == 0)
2735 		return (ARCHIVE_WARN);
2736 	return (ARCHIVE_OK);
2737 }
2738 
2739 static int
clear_nochange_fflags(struct archive_write_disk * a)2740 clear_nochange_fflags(struct archive_write_disk *a)
2741 {
2742 	return (set_fflags_platform(a->name, 0, FILE_ATTRIBUTE_READONLY));
2743 }
2744 
2745 static int
set_fflags(struct archive_write_disk * a)2746 set_fflags(struct archive_write_disk *a)
2747 {
2748 	unsigned long	set, clear;
2749 
2750 	if (a->todo & TODO_FFLAGS) {
2751 		archive_entry_fflags(a->entry, &set, &clear);
2752 		if (set == 0  && clear == 0)
2753 			return (ARCHIVE_OK);
2754 		return (set_fflags_platform(a->name, set, clear));
2755 
2756         }
2757 	return (ARCHIVE_OK);
2758 }
2759 
2760 /* Default empty function body to satisfy mainline code. */
2761 static int
set_acls(struct archive_write_disk * a,HANDLE h,const wchar_t * name,struct archive_acl * acl)2762 set_acls(struct archive_write_disk *a, HANDLE h, const wchar_t *name,
2763 	 struct archive_acl *acl)
2764 {
2765 	(void)a; /* UNUSED */
2766 	(void)h; /* UNUSED */
2767 	(void)name; /* UNUSED */
2768 	(void)acl; /* UNUSED */
2769 	return (ARCHIVE_OK);
2770 }
2771 
2772 /*
2773  * Restore extended attributes - stub implementation for unsupported systems
2774  */
2775 static int
set_xattrs(struct archive_write_disk * a)2776 set_xattrs(struct archive_write_disk *a)
2777 {
2778 	static int warning_done = 0;
2779 
2780 	/* If there aren't any extended attributes, then it's okay not
2781 	 * to extract them, otherwise, issue a single warning. */
2782 	if (archive_entry_xattr_count(a->entry) != 0 && !warning_done) {
2783 		warning_done = 1;
2784 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2785 		    "Cannot restore extended attributes on this system");
2786 		return (ARCHIVE_WARN);
2787 	}
2788 	/* Warning was already emitted; suppress further warnings. */
2789 	return (ARCHIVE_OK);
2790 }
2791 
2792 static void
fileTimeToUtc(const FILETIME * filetime,time_t * t,long * ns)2793 fileTimeToUtc(const FILETIME *filetime, time_t *t, long *ns)
2794 {
2795 	ULARGE_INTEGER utc;
2796 
2797 	utc.HighPart = filetime->dwHighDateTime;
2798 	utc.LowPart  = filetime->dwLowDateTime;
2799 	if (utc.QuadPart >= EPOC_TIME) {
2800 		utc.QuadPart -= EPOC_TIME;
2801 		/* milli seconds base */
2802 		*t = (time_t)(utc.QuadPart / 10000000);
2803 		/* nano seconds base */
2804 		*ns = (long)(utc.QuadPart % 10000000) * 100;
2805 	} else {
2806 		*t = 0;
2807 		*ns = 0;
2808 	}
2809 }
2810 /*
2811  * Test if file on disk is older than entry.
2812  */
2813 static int
older(BY_HANDLE_FILE_INFORMATION * st,struct archive_entry * entry)2814 older(BY_HANDLE_FILE_INFORMATION *st, struct archive_entry *entry)
2815 {
2816 	time_t sec;
2817 	long nsec;
2818 
2819 	fileTimeToUtc(&st->ftLastWriteTime, &sec, &nsec);
2820 	/* First, test the seconds and return if we have a definite answer. */
2821 	/* Definitely older. */
2822 	if (sec < archive_entry_mtime(entry))
2823 		return (1);
2824 	/* Definitely younger. */
2825 	if (sec > archive_entry_mtime(entry))
2826 		return (0);
2827 	if (nsec < archive_entry_mtime_nsec(entry))
2828 		return (1);
2829 	/* Same age or newer, so not older. */
2830 	return (0);
2831 }
2832 
2833 #endif /* _WIN32 && !__CYGWIN__ */
2834 
2835