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);
374 /* Pointers to dynamically loaded functions */
378 NTSTATUS (WINAPI *func_NtCreateFile)(PHANDLE FileHandle,
379 ACCESS_MASK DesiredAccess,
380 POBJECT_ATTRIBUTES ObjectAttributes,
381 PIO_STATUS_BLOCK IoStatusBlock,
382 PLARGE_INTEGER AllocationSize,
383 ULONG FileAttributes,
385 ULONG CreateDisposition,
390 NTSTATUS (WINAPI *func_NtOpenFile) (PHANDLE FileHandle,
391 ACCESS_MASK DesiredAccess,
392 POBJECT_ATTRIBUTES ObjectAttributes,
393 PIO_STATUS_BLOCK IoStatusBlock,
397 NTSTATUS (WINAPI *func_NtReadFile) (HANDLE FileHandle,
399 PIO_APC_ROUTINE ApcRoutine,
401 PIO_STATUS_BLOCK IoStatusBlock,
404 PLARGE_INTEGER ByteOffset,
407 NTSTATUS (WINAPI *func_NtWriteFile) (HANDLE FileHandle,
409 PIO_APC_ROUTINE ApcRoutine,
411 PIO_STATUS_BLOCK IoStatusBlock,
414 PLARGE_INTEGER ByteOffset,
417 NTSTATUS (WINAPI *func_NtQueryInformationFile)(HANDLE FileHandle,
418 PIO_STATUS_BLOCK IoStatusBlock,
419 PVOID FileInformation,
421 FILE_INFORMATION_CLASS FileInformationClass);
423 NTSTATUS (WINAPI *func_NtQuerySecurityObject)(HANDLE handle,
424 SECURITY_INFORMATION SecurityInformation,
425 PSECURITY_DESCRIPTOR SecurityDescriptor,
427 PULONG LengthNeeded);
429 NTSTATUS (WINAPI *func_NtQueryDirectoryFile) (HANDLE FileHandle,
431 PIO_APC_ROUTINE ApcRoutine,
433 PIO_STATUS_BLOCK IoStatusBlock,
434 PVOID FileInformation,
436 FILE_INFORMATION_CLASS FileInformationClass,
437 BOOLEAN ReturnSingleEntry,
438 PUNICODE_STRING FileName,
439 BOOLEAN RestartScan);
441 NTSTATUS (WINAPI *func_NtQueryVolumeInformationFile) (HANDLE FileHandle,
442 PIO_STATUS_BLOCK IoStatusBlock,
445 FS_INFORMATION_CLASS FsInformationClass);
447 NTSTATUS (WINAPI *func_NtSetInformationFile)(HANDLE FileHandle,
448 PIO_STATUS_BLOCK IoStatusBlock,
449 PVOID FileInformation,
451 FILE_INFORMATION_CLASS FileInformationClass);
453 NTSTATUS (WINAPI *func_NtSetSecurityObject)(HANDLE Handle,
454 SECURITY_INFORMATION SecurityInformation,
455 PSECURITY_DESCRIPTOR SecurityDescriptor);
457 NTSTATUS (WINAPI *func_NtFsControlFile) (HANDLE FileHandle,
459 PIO_APC_ROUTINE ApcRoutine,
461 PIO_STATUS_BLOCK IoStatusBlock,
464 ULONG InputBufferLength,
466 ULONG OutputBufferLength);
468 NTSTATUS (WINAPI *func_NtClose) (HANDLE Handle);
470 DWORD (WINAPI *func_RtlNtStatusToDosError)(NTSTATUS status);
472 NTSTATUS (WINAPI *func_RtlDosPathNameToNtPathName_U_WithStatus)
474 OUT PUNICODE_STRING NtName,
475 OUT PCWSTR *PartName,
476 OUT PRTL_RELATIVE_NAME_U RelativeName);
478 NTSTATUS (WINAPI *func_RtlCreateSystemVolumeInformationFolder)
479 (PCUNICODE_STRING VolumeRootPath);
481 static OSVERSIONINFO windows_version_info = {
482 .dwOSVersionInfoSize = sizeof(OSVERSIONINFO),
485 static bool acquired_privileges = false;
488 windows_version_is_at_least(unsigned major, unsigned minor)
490 return windows_version_info.dwMajorVersion > major ||
491 (windows_version_info.dwMajorVersion == major &&
492 windows_version_info.dwMinorVersion >= minor);
501 #define DLL_SYM(name, required) { (void **)&func_##name, #name, required }
503 #define for_each_sym(sym, spec) \
504 for ((sym) = (spec)->syms; (sym)->name; (sym)++)
509 const struct dll_sym syms[];
512 struct dll_spec ntdll_spec = {
513 .name = L"ntdll.dll",
515 DLL_SYM(NtCreateFile, true),
516 DLL_SYM(NtOpenFile, true),
517 DLL_SYM(NtReadFile, true),
518 DLL_SYM(NtWriteFile, true),
519 DLL_SYM(NtQueryInformationFile, true),
520 DLL_SYM(NtQuerySecurityObject, true),
521 DLL_SYM(NtQueryDirectoryFile, true),
522 DLL_SYM(NtQueryVolumeInformationFile, true),
523 DLL_SYM(NtSetInformationFile, true),
524 DLL_SYM(NtSetSecurityObject, true),
525 DLL_SYM(NtFsControlFile, true),
526 DLL_SYM(NtClose, true),
527 DLL_SYM(RtlNtStatusToDosError, true),
528 DLL_SYM(RtlCreateSystemVolumeInformationFolder, false),
529 DLL_SYM(RtlDosPathNameToNtPathName_U_WithStatus, true),
535 init_dll(struct dll_spec *spec)
537 const struct dll_sym *sym;
541 spec->handle = LoadLibrary(spec->name);
543 for_each_sym(sym, spec) {
545 ERROR("%ls could not be loaded!", spec->name);
546 return WIMLIB_ERR_UNSUPPORTED;
551 for_each_sym(sym, spec) {
552 addr = (void *)GetProcAddress(spec->handle, sym->name);
554 *(sym->func_ptr) = addr;
555 } else if (sym->required) {
556 ERROR("Can't find %s in %ls", sym->name, spec->name);
557 return WIMLIB_ERR_UNSUPPORTED;
564 cleanup_dll(struct dll_spec *spec)
566 const struct dll_sym *sym;
569 FreeLibrary(spec->handle);
572 for_each_sym(sym, spec)
573 *(sym->func_ptr) = NULL;
577 /* One-time initialization for Windows capture/apply code. */
579 win32_global_init(int init_flags)
583 /* Try to acquire useful privileges. */
584 if (!(init_flags & WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES)) {
585 ret = WIMLIB_ERR_INSUFFICIENT_PRIVILEGES;
586 if (!win32_modify_capture_privileges(true))
587 if (init_flags & WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES)
589 if (!win32_modify_apply_privileges(true))
590 if (init_flags & WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES)
592 acquired_privileges = true;
595 /* Get Windows version information. */
596 GetVersionEx(&windows_version_info);
598 ret = init_dll(&ntdll_spec);
605 win32_release_capture_and_apply_privileges();
610 win32_global_cleanup(void)
612 if (acquired_privileges)
613 win32_release_capture_and_apply_privileges();
615 cleanup_dll(&ntdll_spec);
618 #endif /* __WIN32__ */