2 * win32_common.c - Windows code common to applying and capturing images.
6 * Copyright (C) 2013, 2014 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
34 #include "wimlib/win32_common.h"
35 #include "wimlib/assert.h"
36 #include "wimlib/error.h"
37 #include "wimlib/util.h"
40 win32_error_to_errno(DWORD err_code)
42 /* This mapping is that used in Cygwin.
43 * Some of these choices are arbitrary. */
45 case ERROR_ACCESS_DENIED:
47 case ERROR_ACTIVE_CONNECTIONS:
49 case ERROR_ALREADY_EXISTS:
51 case ERROR_BAD_DEVICE:
53 case ERROR_BAD_EXE_FORMAT:
55 case ERROR_BAD_NETPATH:
57 case ERROR_BAD_NET_NAME:
59 case ERROR_BAD_NET_RESP:
61 case ERROR_BAD_PATHNAME:
67 case ERROR_BAD_USERNAME:
69 case ERROR_BEGINNING_OF_MEDIA:
71 case ERROR_BROKEN_PIPE:
77 case ERROR_CALL_NOT_IMPLEMENTED:
79 case ERROR_CANNOT_MAKE:
81 case ERROR_CHILD_NOT_COMPLETE:
83 case ERROR_COMMITMENT_LIMIT:
87 case ERROR_DEVICE_DOOR_OPEN:
89 case ERROR_DEVICE_IN_USE:
91 case ERROR_DEVICE_REQUIRES_CLEANING:
95 case ERROR_DIR_NOT_EMPTY:
97 case ERROR_DISK_CORRUPT:
105 case ERROR_EAS_DIDNT_FIT:
108 case ERROR_EAS_NOT_SUPPORTED:
111 case ERROR_EA_LIST_INCONSISTENT:
113 case ERROR_EA_TABLE_FULL:
115 case ERROR_END_OF_MEDIA:
117 case ERROR_EOM_OVERFLOW:
119 case ERROR_EXE_MACHINE_TYPE_MISMATCH:
121 case ERROR_EXE_MARKED_INVALID:
123 case ERROR_FILEMARK_DETECTED:
125 case ERROR_FILENAME_EXCED_RANGE:
127 case ERROR_FILE_CORRUPT:
129 case ERROR_FILE_EXISTS:
131 case ERROR_FILE_INVALID:
133 case ERROR_FILE_NOT_FOUND:
135 case ERROR_HANDLE_DISK_FULL:
138 case ERROR_HANDLE_EOF:
141 case ERROR_INVALID_ADDRESS:
143 case ERROR_INVALID_AT_INTERRUPT_TIME:
145 case ERROR_INVALID_BLOCK_LENGTH:
147 case ERROR_INVALID_DATA:
149 case ERROR_INVALID_DRIVE:
151 case ERROR_INVALID_EA_NAME:
153 case ERROR_INVALID_EXE_SIGNATURE:
156 case ERROR_INVALID_FUNCTION:
159 case ERROR_INVALID_HANDLE:
161 case ERROR_INVALID_NAME:
163 case ERROR_INVALID_PARAMETER:
165 case ERROR_INVALID_SIGNAL_NUMBER:
167 case ERROR_IOPL_NOT_ENABLED:
169 case ERROR_IO_DEVICE:
171 case ERROR_IO_INCOMPLETE:
173 case ERROR_IO_PENDING:
175 case ERROR_LOCK_VIOLATION:
177 case ERROR_MAX_THRDS_REACHED:
179 case ERROR_META_EXPANSION_TOO_LONG:
181 case ERROR_MOD_NOT_FOUND:
184 case ERROR_MORE_DATA:
187 case ERROR_NEGATIVE_SEEK:
189 case ERROR_NETNAME_DELETED:
193 case ERROR_NONE_MAPPED:
195 case ERROR_NONPAGED_SYSTEM_RESOURCES:
198 case ERROR_NOT_CONNECTED:
201 case ERROR_NOT_ENOUGH_MEMORY:
203 case ERROR_NOT_OWNER:
206 case ERROR_NOT_READY:
209 case ERROR_NOT_SAME_DEVICE:
211 case ERROR_NOT_SUPPORTED:
215 case ERROR_NO_DATA_DETECTED:
218 case ERROR_NO_MEDIA_IN_DRIVE:
222 case ERROR_NO_MORE_FILES:
226 case ERROR_NO_MORE_ITEMS:
229 case ERROR_NO_MORE_SEARCH_HANDLES:
231 case ERROR_NO_PROC_SLOTS:
233 case ERROR_NO_SIGNAL_SENT:
235 case ERROR_NO_SYSTEM_RESOURCES:
239 case ERROR_OPEN_FAILED:
241 case ERROR_OPEN_FILES:
243 case ERROR_OUTOFMEMORY:
245 case ERROR_PAGED_SYSTEM_RESOURCES:
247 case ERROR_PAGEFILE_QUOTA:
249 case ERROR_PATH_NOT_FOUND:
251 case ERROR_PIPE_BUSY:
253 case ERROR_PIPE_CONNECTED:
256 case ERROR_PIPE_LISTENING:
258 case ERROR_PIPE_NOT_CONNECTED:
261 case ERROR_POSSIBLE_DEADLOCK:
263 case ERROR_PRIVILEGE_NOT_HELD:
265 case ERROR_PROCESS_ABORTED:
267 case ERROR_PROC_NOT_FOUND:
270 case ERROR_REM_NOT_LIST:
273 case ERROR_SECTOR_NOT_FOUND:
277 case ERROR_SETMARK_DETECTED:
279 case ERROR_SHARING_BUFFER_EXCEEDED:
281 case ERROR_SHARING_VIOLATION:
283 case ERROR_SIGNAL_PENDING:
285 case ERROR_SIGNAL_REFUSED:
288 case ERROR_SXS_CANT_GEN_ACTCTX:
291 case ERROR_THREAD_1_INACTIVE:
293 case ERROR_TOO_MANY_LINKS:
295 case ERROR_TOO_MANY_OPEN_FILES:
297 case ERROR_WAIT_NO_CHILDREN:
299 case ERROR_WORKING_SET_QUOTA:
301 case ERROR_WRITE_PROTECT:
310 set_errno_from_win32_error(DWORD err)
312 errno = win32_error_to_errno(err);
316 set_errno_from_GetLastError(void)
318 set_errno_from_win32_error(GetLastError());
322 set_errno_from_nt_status(NTSTATUS status)
324 set_errno_from_win32_error((*func_RtlNtStatusToDosError)(status));
327 /* Given a Windows-style path, return the number of characters of the prefix
328 * that specify the path to the root directory of a drive, or return 0 if the
329 * drive is relative (or at least on the current drive, in the case of
330 * absolute-but-not-really-absolute paths like \Windows\System32) */
332 win32_path_drive_spec_len(const wchar_t *path)
336 if (!wcsncmp(path, L"\\\\?\\", 4)) {
337 /* \\?\-prefixed path. Check for following drive letter and
339 if (path[4] != L'\0' && path[5] == L':' &&
340 is_any_path_separator(path[6]))
343 /* Not a \\?\-prefixed path. Check for an initial drive letter
344 * and path separator. */
345 if (path[0] != L'\0' && path[1] == L':' &&
346 is_any_path_separator(path[2]))
349 /* Include any additional path separators.*/
351 while (is_any_path_separator(path[n]))
357 win32_path_is_root_of_drive(const wchar_t *path)
359 size_t drive_spec_len;
360 wchar_t full_path[32768];
363 ret = GetFullPathName(path, ARRAY_LEN(full_path), full_path, NULL);
364 if (ret > 0 && ret < ARRAY_LEN(full_path))
367 /* Explicit drive letter and path separator? */
368 drive_spec_len = win32_path_drive_spec_len(path);
369 if (drive_spec_len > 0 && path[drive_spec_len] == L'\0')
372 /* All path separators? */
373 for (const wchar_t *p = path; *p != L'\0'; p++)
374 if (!is_any_path_separator(*p))
380 /* Given a path, which may not yet exist, get a set of flags that describe the
381 * features of the volume the path is on. */
383 win32_get_vol_flags(const wchar_t *path, unsigned *vol_flags_ret,
384 bool *supports_SetFileShortName_ret)
389 size_t drive_spec_len;
390 wchar_t filesystem_name[MAX_PATH + 1];
392 if (supports_SetFileShortName_ret)
393 *supports_SetFileShortName_ret = false;
395 drive_spec_len = win32_path_drive_spec_len(path);
397 if (drive_spec_len == 0)
398 if (path[0] != L'\0' && path[1] == L':') /* Drive-relative path? */
401 if (drive_spec_len == 0) {
402 /* Path does not start with a drive letter; use the volume of
403 * the current working directory. */
406 /* Path starts with a drive letter (or \\?\ followed by a drive
407 * letter); use it. */
408 volume = alloca((drive_spec_len + 2) * sizeof(wchar_t));
409 wmemcpy(volume, path, drive_spec_len);
410 /* Add trailing backslash in case this was a drive-relative
412 volume[drive_spec_len] = L'\\';
413 volume[drive_spec_len + 1] = L'\0';
415 bret = GetVolumeInformation(
416 volume, /* lpRootPathName */
417 NULL, /* lpVolumeNameBuffer */
418 0, /* nVolumeNameSize */
419 NULL, /* lpVolumeSerialNumber */
420 NULL, /* lpMaximumComponentLength */
421 &vol_flags, /* lpFileSystemFlags */
422 filesystem_name, /* lpFileSystemNameBuffer */
423 ARRAY_LEN(filesystem_name)); /* nFileSystemNameSize */
425 set_errno_from_GetLastError();
426 WARNING_WITH_ERRNO("Failed to get volume information for "
427 "path \"%ls\"", path);
428 vol_flags = 0xffffffff;
432 if (wcsstr(filesystem_name, L"NTFS")) {
433 /* FILE_SUPPORTS_HARD_LINKS is only supported on Windows 7 and later.
434 * Force it on anyway if filesystem is NTFS. */
435 vol_flags |= FILE_SUPPORTS_HARD_LINKS;
437 if (supports_SetFileShortName_ret)
438 *supports_SetFileShortName_ret = true;
442 DEBUG("using vol_flags = %x", vol_flags);
443 *vol_flags_ret = vol_flags;
448 win32_modify_privilege(const wchar_t *privilege, bool enable)
452 TOKEN_PRIVILEGES newState;
455 if (!OpenProcessToken(GetCurrentProcess(),
456 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
460 if (!LookupPrivilegeValue(NULL, privilege, &luid))
461 goto out_close_handle;
463 newState.PrivilegeCount = 1;
464 newState.Privileges[0].Luid = luid;
465 newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
466 SetLastError(ERROR_SUCCESS);
467 ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
468 if (ret && GetLastError() == ERROR_NOT_ALL_ASSIGNED)
477 win32_modify_capture_privileges(bool enable)
479 return win32_modify_privilege(SE_BACKUP_NAME, enable)
480 && win32_modify_privilege(SE_SECURITY_NAME, enable);
484 win32_modify_apply_privileges(bool enable)
486 return win32_modify_privilege(SE_RESTORE_NAME, enable)
487 && win32_modify_privilege(SE_SECURITY_NAME, enable)
488 && win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
492 win32_release_capture_and_apply_privileges(void)
494 win32_modify_capture_privileges(false);
495 win32_modify_apply_privileges(false);
499 win32_open_existing_file(const wchar_t *path, DWORD dwDesiredAccess)
501 return CreateFile(path,
504 NULL, /* lpSecurityAttributes */
506 FILE_FLAG_BACKUP_SEMANTICS |
507 FILE_FLAG_OPEN_REPARSE_POINT,
508 NULL /* hTemplateFile */);
511 /* Pointers to dynamically loaded functions */
513 /* kernel32.dll: Vista and later */
514 BOOL (WINAPI *func_CreateSymbolicLinkW)(const wchar_t *lpSymlinkFileName,
515 const wchar_t *lpTargetFileName,
520 NTSTATUS (WINAPI *func_NtOpenFile) (PHANDLE FileHandle,
521 ACCESS_MASK DesiredAccess,
522 POBJECT_ATTRIBUTES ObjectAttributes,
523 PIO_STATUS_BLOCK IoStatusBlock,
527 NTSTATUS (WINAPI *func_NtReadFile) (HANDLE FileHandle,
529 PIO_APC_ROUTINE ApcRoutine,
531 PIO_STATUS_BLOCK IoStatusBlock,
534 PLARGE_INTEGER ByteOffset,
537 NTSTATUS (WINAPI *func_NtOpenFile) (PHANDLE FileHandle,
538 ACCESS_MASK DesiredAccess,
539 POBJECT_ATTRIBUTES ObjectAttributes,
540 PIO_STATUS_BLOCK IoStatusBlock,
544 NTSTATUS (WINAPI *func_NtQueryInformationFile)(HANDLE FileHandle,
545 PIO_STATUS_BLOCK IoStatusBlock,
546 PVOID FileInformation,
548 FILE_INFORMATION_CLASS FileInformationClass);
550 NTSTATUS (WINAPI *func_NtQuerySecurityObject)(HANDLE handle,
551 SECURITY_INFORMATION SecurityInformation,
552 PSECURITY_DESCRIPTOR SecurityDescriptor,
554 PULONG LengthNeeded);
556 NTSTATUS (WINAPI *func_NtQueryDirectoryFile) (HANDLE FileHandle,
558 PIO_APC_ROUTINE ApcRoutine,
560 PIO_STATUS_BLOCK IoStatusBlock,
561 PVOID FileInformation,
563 FILE_INFORMATION_CLASS FileInformationClass,
564 BOOLEAN ReturnSingleEntry,
565 PUNICODE_STRING FileName,
566 BOOLEAN RestartScan);
568 NTSTATUS (WINAPI *func_NtQueryVolumeInformationFile) (HANDLE FileHandle,
569 PIO_STATUS_BLOCK IoStatusBlock,
572 FS_INFORMATION_CLASS FsInformationClass);
574 NTSTATUS (WINAPI *func_NtSetSecurityObject)(HANDLE Handle,
575 SECURITY_INFORMATION SecurityInformation,
576 PSECURITY_DESCRIPTOR SecurityDescriptor);
578 NTSTATUS (WINAPI *func_NtClose) (HANDLE Handle);
580 DWORD (WINAPI *func_RtlNtStatusToDosError)(NTSTATUS status);
582 NTSTATUS (WINAPI *func_RtlCreateSystemVolumeInformationFolder)
583 (PCUNICODE_STRING VolumeRootPath);
585 static OSVERSIONINFO windows_version_info = {
586 .dwOSVersionInfoSize = sizeof(OSVERSIONINFO),
589 static bool acquired_privileges = false;
592 windows_version_is_at_least(unsigned major, unsigned minor)
594 return windows_version_info.dwMajorVersion > major ||
595 (windows_version_info.dwMajorVersion == major &&
596 windows_version_info.dwMinorVersion >= minor);
605 #define DLL_SYM(name, required) { (void **)&func_##name, #name, required }
607 #define for_each_sym(sym, spec) \
608 for ((sym) = (spec)->syms; (sym)->name; (sym)++)
613 const struct dll_sym syms[];
616 struct dll_spec ntdll_spec = {
617 .name = L"ntdll.dll",
619 DLL_SYM(NtOpenFile, true),
620 DLL_SYM(NtReadFile, true),
621 DLL_SYM(NtQueryInformationFile, true),
622 DLL_SYM(NtQuerySecurityObject, true),
623 DLL_SYM(NtQueryDirectoryFile, true),
624 DLL_SYM(NtQueryVolumeInformationFile, true),
625 DLL_SYM(NtSetSecurityObject, true),
626 DLL_SYM(NtClose, true),
627 DLL_SYM(RtlNtStatusToDosError, true),
628 DLL_SYM(RtlCreateSystemVolumeInformationFolder, false),
633 struct dll_spec kernel32_spec = {
634 .name = L"kernel32.dll",
636 DLL_SYM(CreateSymbolicLinkW, false),
642 init_dll(struct dll_spec *spec)
644 const struct dll_sym *sym;
648 spec->handle = LoadLibrary(spec->name);
650 for_each_sym(sym, spec) {
652 ERROR("%ls could not be loaded!", spec->name);
653 return WIMLIB_ERR_UNSUPPORTED;
658 for_each_sym(sym, spec) {
659 addr = (void *)GetProcAddress(spec->handle, sym->name);
661 *(sym->func_ptr) = addr;
662 } else if (sym->required) {
663 ERROR("Can't find %s in %ls", sym->name, spec->name);
664 return WIMLIB_ERR_UNSUPPORTED;
671 cleanup_dll(struct dll_spec *spec)
673 const struct dll_sym *sym;
676 FreeLibrary(spec->handle);
679 for_each_sym(sym, spec)
680 *(sym->func_ptr) = NULL;
684 /* One-time initialization for Windows capture/apply code. */
686 win32_global_init(int init_flags)
690 /* Try to acquire useful privileges. */
691 if (!(init_flags & WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES)) {
692 ret = WIMLIB_ERR_INSUFFICIENT_PRIVILEGES;
693 if (!win32_modify_capture_privileges(true))
694 if (init_flags & WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES)
696 if (!win32_modify_apply_privileges(true))
697 if (init_flags & WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES)
699 acquired_privileges = true;
702 /* Get Windows version information. */
703 GetVersionEx(&windows_version_info);
705 /* Try to dynamically load some functions. */
706 ret = init_dll(&kernel32_spec);
710 ret = init_dll(&ntdll_spec);
712 goto out_cleanup_kernel32;
716 out_cleanup_kernel32:
717 cleanup_dll(&kernel32_spec);
719 win32_release_capture_and_apply_privileges();
724 win32_global_cleanup(void)
726 if (acquired_privileges)
727 win32_release_capture_and_apply_privileges();
729 cleanup_dll(&kernel32_spec);
730 cleanup_dll(&ntdll_spec);
733 #endif /* __WIN32__ */