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/.
32 #include "wimlib/win32_common.h"
33 #include "wimlib/error.h"
34 #include "wimlib/util.h"
37 win32_error_to_errno(DWORD err_code)
39 /* This mapping is that used in Cygwin.
40 * Some of these choices are arbitrary. */
42 case ERROR_ACCESS_DENIED:
44 case ERROR_ACTIVE_CONNECTIONS:
46 case ERROR_ALREADY_EXISTS:
48 case ERROR_BAD_DEVICE:
50 case ERROR_BAD_EXE_FORMAT:
52 case ERROR_BAD_NETPATH:
54 case ERROR_BAD_NET_NAME:
56 case ERROR_BAD_NET_RESP:
58 case ERROR_BAD_PATHNAME:
64 case ERROR_BAD_USERNAME:
66 case ERROR_BEGINNING_OF_MEDIA:
68 case ERROR_BROKEN_PIPE:
74 case ERROR_CALL_NOT_IMPLEMENTED:
76 case ERROR_CANNOT_MAKE:
78 case ERROR_CHILD_NOT_COMPLETE:
80 case ERROR_COMMITMENT_LIMIT:
84 case ERROR_DEVICE_DOOR_OPEN:
86 case ERROR_DEVICE_IN_USE:
88 case ERROR_DEVICE_REQUIRES_CLEANING:
92 case ERROR_DIR_NOT_EMPTY:
94 case ERROR_DISK_CORRUPT:
102 case ERROR_EAS_DIDNT_FIT:
105 case ERROR_EAS_NOT_SUPPORTED:
108 case ERROR_EA_LIST_INCONSISTENT:
110 case ERROR_EA_TABLE_FULL:
112 case ERROR_END_OF_MEDIA:
114 case ERROR_EOM_OVERFLOW:
116 case ERROR_EXE_MACHINE_TYPE_MISMATCH:
118 case ERROR_EXE_MARKED_INVALID:
120 case ERROR_FILEMARK_DETECTED:
122 case ERROR_FILENAME_EXCED_RANGE:
124 case ERROR_FILE_CORRUPT:
126 case ERROR_FILE_EXISTS:
128 case ERROR_FILE_INVALID:
130 case ERROR_FILE_NOT_FOUND:
132 case ERROR_HANDLE_DISK_FULL:
135 case ERROR_HANDLE_EOF:
138 case ERROR_INVALID_ADDRESS:
140 case ERROR_INVALID_AT_INTERRUPT_TIME:
142 case ERROR_INVALID_BLOCK_LENGTH:
144 case ERROR_INVALID_DATA:
146 case ERROR_INVALID_DRIVE:
148 case ERROR_INVALID_EA_NAME:
150 case ERROR_INVALID_EXE_SIGNATURE:
153 case ERROR_INVALID_FUNCTION:
156 case ERROR_INVALID_HANDLE:
158 case ERROR_INVALID_NAME:
160 case ERROR_INVALID_PARAMETER:
162 case ERROR_INVALID_SIGNAL_NUMBER:
164 case ERROR_IOPL_NOT_ENABLED:
166 case ERROR_IO_DEVICE:
168 case ERROR_IO_INCOMPLETE:
170 case ERROR_IO_PENDING:
172 case ERROR_LOCK_VIOLATION:
174 case ERROR_MAX_THRDS_REACHED:
176 case ERROR_META_EXPANSION_TOO_LONG:
178 case ERROR_MOD_NOT_FOUND:
181 case ERROR_MORE_DATA:
184 case ERROR_NEGATIVE_SEEK:
186 case ERROR_NETNAME_DELETED:
190 case ERROR_NONE_MAPPED:
192 case ERROR_NONPAGED_SYSTEM_RESOURCES:
195 case ERROR_NOT_CONNECTED:
198 case ERROR_NOT_ENOUGH_MEMORY:
200 case ERROR_NOT_OWNER:
203 case ERROR_NOT_READY:
206 case ERROR_NOT_SAME_DEVICE:
208 case ERROR_NOT_SUPPORTED:
212 case ERROR_NO_DATA_DETECTED:
215 case ERROR_NO_MEDIA_IN_DRIVE:
219 case ERROR_NO_MORE_FILES:
223 case ERROR_NO_MORE_ITEMS:
226 case ERROR_NO_MORE_SEARCH_HANDLES:
228 case ERROR_NO_PROC_SLOTS:
230 case ERROR_NO_SIGNAL_SENT:
232 case ERROR_NO_SYSTEM_RESOURCES:
236 case ERROR_OPEN_FAILED:
238 case ERROR_OPEN_FILES:
240 case ERROR_OUTOFMEMORY:
242 case ERROR_PAGED_SYSTEM_RESOURCES:
244 case ERROR_PAGEFILE_QUOTA:
246 case ERROR_PATH_NOT_FOUND:
248 case ERROR_PIPE_BUSY:
250 case ERROR_PIPE_CONNECTED:
253 case ERROR_PIPE_LISTENING:
255 case ERROR_PIPE_NOT_CONNECTED:
258 case ERROR_POSSIBLE_DEADLOCK:
260 case ERROR_PRIVILEGE_NOT_HELD:
262 case ERROR_PROCESS_ABORTED:
264 case ERROR_PROC_NOT_FOUND:
267 case ERROR_REM_NOT_LIST:
270 case ERROR_SECTOR_NOT_FOUND:
274 case ERROR_SETMARK_DETECTED:
276 case ERROR_SHARING_BUFFER_EXCEEDED:
278 case ERROR_SHARING_VIOLATION:
280 case ERROR_SIGNAL_PENDING:
282 case ERROR_SIGNAL_REFUSED:
285 case ERROR_SXS_CANT_GEN_ACTCTX:
288 case ERROR_THREAD_1_INACTIVE:
290 case ERROR_TOO_MANY_LINKS:
292 case ERROR_TOO_MANY_OPEN_FILES:
294 case ERROR_WAIT_NO_CHILDREN:
296 case ERROR_WORKING_SET_QUOTA:
298 case ERROR_WRITE_PROTECT:
306 set_errno_from_win32_error(DWORD err)
308 errno = win32_error_to_errno(err);
312 set_errno_from_GetLastError(void)
314 set_errno_from_win32_error(GetLastError());
318 set_errno_from_nt_status(NTSTATUS status)
320 set_errno_from_win32_error((*func_RtlNtStatusToDosError)(status));
324 win32_modify_privilege(const wchar_t *privilege, bool enable)
328 TOKEN_PRIVILEGES newState;
331 if (!OpenProcessToken(GetCurrentProcess(),
332 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
336 if (!LookupPrivilegeValue(NULL, privilege, &luid))
337 goto out_close_handle;
339 newState.PrivilegeCount = 1;
340 newState.Privileges[0].Luid = luid;
341 newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
342 SetLastError(ERROR_SUCCESS);
343 ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
344 if (ret && GetLastError() == ERROR_NOT_ALL_ASSIGNED)
353 win32_modify_capture_privileges(bool enable)
355 return win32_modify_privilege(SE_BACKUP_NAME, enable)
356 && win32_modify_privilege(SE_SECURITY_NAME, enable);
360 win32_modify_apply_privileges(bool enable)
362 return win32_modify_privilege(SE_RESTORE_NAME, enable)
363 && win32_modify_privilege(SE_SECURITY_NAME, enable)
364 && win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
368 win32_release_capture_and_apply_privileges(void)
370 win32_modify_capture_privileges(false);
371 win32_modify_apply_privileges(false);
375 win32_open_existing_file(const wchar_t *path, DWORD dwDesiredAccess)
377 return CreateFile(path,
380 NULL, /* lpSecurityAttributes */
382 FILE_FLAG_BACKUP_SEMANTICS |
383 FILE_FLAG_OPEN_REPARSE_POINT,
384 NULL /* hTemplateFile */);
387 /* Pointers to dynamically loaded functions */
389 /* kernel32.dll: Vista and later */
390 BOOL (WINAPI *func_CreateSymbolicLinkW)(const wchar_t *lpSymlinkFileName,
391 const wchar_t *lpTargetFileName,
396 NTSTATUS (WINAPI *func_NtOpenFile) (PHANDLE FileHandle,
397 ACCESS_MASK DesiredAccess,
398 POBJECT_ATTRIBUTES ObjectAttributes,
399 PIO_STATUS_BLOCK IoStatusBlock,
403 NTSTATUS (WINAPI *func_NtReadFile) (HANDLE FileHandle,
405 PIO_APC_ROUTINE ApcRoutine,
407 PIO_STATUS_BLOCK IoStatusBlock,
410 PLARGE_INTEGER ByteOffset,
413 NTSTATUS (WINAPI *func_NtQueryInformationFile)(HANDLE FileHandle,
414 PIO_STATUS_BLOCK IoStatusBlock,
415 PVOID FileInformation,
417 FILE_INFORMATION_CLASS FileInformationClass);
419 NTSTATUS (WINAPI *func_NtQuerySecurityObject)(HANDLE handle,
420 SECURITY_INFORMATION SecurityInformation,
421 PSECURITY_DESCRIPTOR SecurityDescriptor,
423 PULONG LengthNeeded);
425 NTSTATUS (WINAPI *func_NtQueryDirectoryFile) (HANDLE FileHandle,
427 PIO_APC_ROUTINE ApcRoutine,
429 PIO_STATUS_BLOCK IoStatusBlock,
430 PVOID FileInformation,
432 FILE_INFORMATION_CLASS FileInformationClass,
433 BOOLEAN ReturnSingleEntry,
434 PUNICODE_STRING FileName,
435 BOOLEAN RestartScan);
437 NTSTATUS (WINAPI *func_NtQueryVolumeInformationFile) (HANDLE FileHandle,
438 PIO_STATUS_BLOCK IoStatusBlock,
441 FS_INFORMATION_CLASS FsInformationClass);
443 NTSTATUS (WINAPI *func_NtSetSecurityObject)(HANDLE Handle,
444 SECURITY_INFORMATION SecurityInformation,
445 PSECURITY_DESCRIPTOR SecurityDescriptor);
447 NTSTATUS (WINAPI *func_NtClose) (HANDLE Handle);
449 DWORD (WINAPI *func_RtlNtStatusToDosError)(NTSTATUS status);
451 NTSTATUS (WINAPI *func_RtlCreateSystemVolumeInformationFolder)
452 (PCUNICODE_STRING VolumeRootPath);
454 static OSVERSIONINFO windows_version_info = {
455 .dwOSVersionInfoSize = sizeof(OSVERSIONINFO),
458 static bool acquired_privileges = false;
461 windows_version_is_at_least(unsigned major, unsigned minor)
463 return windows_version_info.dwMajorVersion > major ||
464 (windows_version_info.dwMajorVersion == major &&
465 windows_version_info.dwMinorVersion >= minor);
474 #define DLL_SYM(name, required) { (void **)&func_##name, #name, required }
476 #define for_each_sym(sym, spec) \
477 for ((sym) = (spec)->syms; (sym)->name; (sym)++)
482 const struct dll_sym syms[];
485 struct dll_spec ntdll_spec = {
486 .name = L"ntdll.dll",
488 DLL_SYM(NtOpenFile, true),
489 DLL_SYM(NtReadFile, true),
490 DLL_SYM(NtQueryInformationFile, true),
491 DLL_SYM(NtQuerySecurityObject, true),
492 DLL_SYM(NtQueryDirectoryFile, true),
493 DLL_SYM(NtQueryVolumeInformationFile, true),
494 DLL_SYM(NtSetSecurityObject, true),
495 DLL_SYM(NtClose, true),
496 DLL_SYM(RtlNtStatusToDosError, true),
497 DLL_SYM(RtlCreateSystemVolumeInformationFolder, false),
502 struct dll_spec kernel32_spec = {
503 .name = L"kernel32.dll",
505 DLL_SYM(CreateSymbolicLinkW, false),
511 init_dll(struct dll_spec *spec)
513 const struct dll_sym *sym;
517 spec->handle = LoadLibrary(spec->name);
519 for_each_sym(sym, spec) {
521 ERROR("%ls could not be loaded!", spec->name);
522 return WIMLIB_ERR_UNSUPPORTED;
527 for_each_sym(sym, spec) {
528 addr = (void *)GetProcAddress(spec->handle, sym->name);
530 *(sym->func_ptr) = addr;
531 } else if (sym->required) {
532 ERROR("Can't find %s in %ls", sym->name, spec->name);
533 return WIMLIB_ERR_UNSUPPORTED;
540 cleanup_dll(struct dll_spec *spec)
542 const struct dll_sym *sym;
545 FreeLibrary(spec->handle);
548 for_each_sym(sym, spec)
549 *(sym->func_ptr) = NULL;
553 /* One-time initialization for Windows capture/apply code. */
555 win32_global_init(int init_flags)
559 /* Try to acquire useful privileges. */
560 if (!(init_flags & WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES)) {
561 ret = WIMLIB_ERR_INSUFFICIENT_PRIVILEGES;
562 if (!win32_modify_capture_privileges(true))
563 if (init_flags & WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES)
565 if (!win32_modify_apply_privileges(true))
566 if (init_flags & WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES)
568 acquired_privileges = true;
571 /* Get Windows version information. */
572 GetVersionEx(&windows_version_info);
574 /* Try to dynamically load some functions. */
575 ret = init_dll(&kernel32_spec);
579 ret = init_dll(&ntdll_spec);
581 goto out_cleanup_kernel32;
585 out_cleanup_kernel32:
586 cleanup_dll(&kernel32_spec);
588 win32_release_capture_and_apply_privileges();
593 win32_global_cleanup(void)
595 if (acquired_privileges)
596 win32_release_capture_and_apply_privileges();
598 cleanup_dll(&kernel32_spec);
599 cleanup_dll(&ntdll_spec);
602 #endif /* __WIN32__ */