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_NtOpenFile) (PHANDLE FileHandle,
414 ACCESS_MASK DesiredAccess,
415 POBJECT_ATTRIBUTES ObjectAttributes,
416 PIO_STATUS_BLOCK IoStatusBlock,
420 NTSTATUS (WINAPI *func_NtQueryInformationFile)(HANDLE FileHandle,
421 PIO_STATUS_BLOCK IoStatusBlock,
422 PVOID FileInformation,
424 FILE_INFORMATION_CLASS FileInformationClass);
426 NTSTATUS (WINAPI *func_NtQuerySecurityObject)(HANDLE handle,
427 SECURITY_INFORMATION SecurityInformation,
428 PSECURITY_DESCRIPTOR SecurityDescriptor,
430 PULONG LengthNeeded);
432 NTSTATUS (WINAPI *func_NtQueryDirectoryFile) (HANDLE FileHandle,
434 PIO_APC_ROUTINE ApcRoutine,
436 PIO_STATUS_BLOCK IoStatusBlock,
437 PVOID FileInformation,
439 FILE_INFORMATION_CLASS FileInformationClass,
440 BOOLEAN ReturnSingleEntry,
441 PUNICODE_STRING FileName,
442 BOOLEAN RestartScan);
444 NTSTATUS (WINAPI *func_NtQueryVolumeInformationFile) (HANDLE FileHandle,
445 PIO_STATUS_BLOCK IoStatusBlock,
448 FS_INFORMATION_CLASS FsInformationClass);
450 NTSTATUS (WINAPI *func_NtSetSecurityObject)(HANDLE Handle,
451 SECURITY_INFORMATION SecurityInformation,
452 PSECURITY_DESCRIPTOR SecurityDescriptor);
454 NTSTATUS (WINAPI *func_NtClose) (HANDLE Handle);
456 DWORD (WINAPI *func_RtlNtStatusToDosError)(NTSTATUS status);
458 NTSTATUS (WINAPI *func_RtlCreateSystemVolumeInformationFolder)
459 (PCUNICODE_STRING VolumeRootPath);
461 static OSVERSIONINFO windows_version_info = {
462 .dwOSVersionInfoSize = sizeof(OSVERSIONINFO),
465 static bool acquired_privileges = false;
468 windows_version_is_at_least(unsigned major, unsigned minor)
470 return windows_version_info.dwMajorVersion > major ||
471 (windows_version_info.dwMajorVersion == major &&
472 windows_version_info.dwMinorVersion >= minor);
481 #define DLL_SYM(name, required) { (void **)&func_##name, #name, required }
483 #define for_each_sym(sym, spec) \
484 for ((sym) = (spec)->syms; (sym)->name; (sym)++)
489 const struct dll_sym syms[];
492 struct dll_spec ntdll_spec = {
493 .name = L"ntdll.dll",
495 DLL_SYM(NtOpenFile, true),
496 DLL_SYM(NtReadFile, true),
497 DLL_SYM(NtQueryInformationFile, true),
498 DLL_SYM(NtQuerySecurityObject, true),
499 DLL_SYM(NtQueryDirectoryFile, true),
500 DLL_SYM(NtQueryVolumeInformationFile, true),
501 DLL_SYM(NtSetSecurityObject, true),
502 DLL_SYM(NtClose, true),
503 DLL_SYM(RtlNtStatusToDosError, true),
504 DLL_SYM(RtlCreateSystemVolumeInformationFolder, false),
509 struct dll_spec kernel32_spec = {
510 .name = L"kernel32.dll",
512 DLL_SYM(CreateSymbolicLinkW, false),
518 init_dll(struct dll_spec *spec)
520 const struct dll_sym *sym;
524 spec->handle = LoadLibrary(spec->name);
526 for_each_sym(sym, spec) {
528 ERROR("%ls could not be loaded!", spec->name);
529 return WIMLIB_ERR_UNSUPPORTED;
534 for_each_sym(sym, spec) {
535 addr = (void *)GetProcAddress(spec->handle, sym->name);
537 *(sym->func_ptr) = addr;
538 } else if (sym->required) {
539 ERROR("Can't find %s in %ls", sym->name, spec->name);
540 return WIMLIB_ERR_UNSUPPORTED;
547 cleanup_dll(struct dll_spec *spec)
549 const struct dll_sym *sym;
552 FreeLibrary(spec->handle);
555 for_each_sym(sym, spec)
556 *(sym->func_ptr) = NULL;
560 /* One-time initialization for Windows capture/apply code. */
562 win32_global_init(int init_flags)
566 /* Try to acquire useful privileges. */
567 if (!(init_flags & WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES)) {
568 ret = WIMLIB_ERR_INSUFFICIENT_PRIVILEGES;
569 if (!win32_modify_capture_privileges(true))
570 if (init_flags & WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES)
572 if (!win32_modify_apply_privileges(true))
573 if (init_flags & WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES)
575 acquired_privileges = true;
578 /* Get Windows version information. */
579 GetVersionEx(&windows_version_info);
581 /* Try to dynamically load some functions. */
582 ret = init_dll(&kernel32_spec);
586 ret = init_dll(&ntdll_spec);
588 goto out_cleanup_kernel32;
592 out_cleanup_kernel32:
593 cleanup_dll(&kernel32_spec);
595 win32_release_capture_and_apply_privileges();
600 win32_global_cleanup(void)
602 if (acquired_privileges)
603 win32_release_capture_and_apply_privileges();
605 cleanup_dll(&kernel32_spec);
606 cleanup_dll(&ntdll_spec);
609 #endif /* __WIN32__ */