2 * win32_common.c - Windows code common to applying and capturing images.
6 * Copyright (C) 2013, 2014 Eric Biggers
8 * This file is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
13 * This file is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this file; if not, see http://www.gnu.org/licenses/.
30 #include "wimlib/win32_common.h"
32 #include "wimlib/error.h"
33 #include "wimlib/util.h"
36 win32_error_to_errno(DWORD err_code)
38 /* This mapping is that used in Cygwin.
39 * Some of these choices are arbitrary. */
41 case ERROR_ACCESS_DENIED:
43 case ERROR_ACTIVE_CONNECTIONS:
45 case ERROR_ALREADY_EXISTS:
47 case ERROR_BAD_DEVICE:
49 case ERROR_BAD_EXE_FORMAT:
51 case ERROR_BAD_NETPATH:
53 case ERROR_BAD_NET_NAME:
55 case ERROR_BAD_NET_RESP:
57 case ERROR_BAD_PATHNAME:
63 case ERROR_BAD_USERNAME:
65 case ERROR_BEGINNING_OF_MEDIA:
67 case ERROR_BROKEN_PIPE:
73 case ERROR_CALL_NOT_IMPLEMENTED:
75 case ERROR_CANNOT_MAKE:
77 case ERROR_CHILD_NOT_COMPLETE:
79 case ERROR_COMMITMENT_LIMIT:
83 case ERROR_DEVICE_DOOR_OPEN:
85 case ERROR_DEVICE_IN_USE:
87 case ERROR_DEVICE_REQUIRES_CLEANING:
91 case ERROR_DIR_NOT_EMPTY:
93 case ERROR_DISK_CORRUPT:
101 case ERROR_EAS_DIDNT_FIT:
104 case ERROR_EAS_NOT_SUPPORTED:
107 case ERROR_EA_LIST_INCONSISTENT:
109 case ERROR_EA_TABLE_FULL:
111 case ERROR_END_OF_MEDIA:
113 case ERROR_EOM_OVERFLOW:
115 case ERROR_EXE_MACHINE_TYPE_MISMATCH:
117 case ERROR_EXE_MARKED_INVALID:
119 case ERROR_FILEMARK_DETECTED:
121 case ERROR_FILENAME_EXCED_RANGE:
123 case ERROR_FILE_CORRUPT:
125 case ERROR_FILE_EXISTS:
127 case ERROR_FILE_INVALID:
129 case ERROR_FILE_NOT_FOUND:
131 case ERROR_HANDLE_DISK_FULL:
134 case ERROR_HANDLE_EOF:
137 case ERROR_INVALID_ADDRESS:
139 case ERROR_INVALID_AT_INTERRUPT_TIME:
141 case ERROR_INVALID_BLOCK_LENGTH:
143 case ERROR_INVALID_DATA:
145 case ERROR_INVALID_DRIVE:
147 case ERROR_INVALID_EA_NAME:
149 case ERROR_INVALID_EXE_SIGNATURE:
152 case ERROR_INVALID_FUNCTION:
155 case ERROR_INVALID_HANDLE:
157 case ERROR_INVALID_NAME:
159 case ERROR_INVALID_PARAMETER:
161 case ERROR_INVALID_SIGNAL_NUMBER:
163 case ERROR_IOPL_NOT_ENABLED:
165 case ERROR_IO_DEVICE:
167 case ERROR_IO_INCOMPLETE:
169 case ERROR_IO_PENDING:
171 case ERROR_LOCK_VIOLATION:
173 case ERROR_MAX_THRDS_REACHED:
175 case ERROR_META_EXPANSION_TOO_LONG:
177 case ERROR_MOD_NOT_FOUND:
180 case ERROR_MORE_DATA:
183 case ERROR_NEGATIVE_SEEK:
185 case ERROR_NETNAME_DELETED:
189 case ERROR_NONE_MAPPED:
191 case ERROR_NONPAGED_SYSTEM_RESOURCES:
194 case ERROR_NOT_CONNECTED:
197 case ERROR_NOT_ENOUGH_MEMORY:
199 case ERROR_NOT_OWNER:
202 case ERROR_NOT_READY:
205 case ERROR_NOT_SAME_DEVICE:
207 case ERROR_NOT_SUPPORTED:
211 case ERROR_NO_DATA_DETECTED:
214 case ERROR_NO_MEDIA_IN_DRIVE:
218 case ERROR_NO_MORE_FILES:
222 case ERROR_NO_MORE_ITEMS:
225 case ERROR_NO_MORE_SEARCH_HANDLES:
227 case ERROR_NO_PROC_SLOTS:
229 case ERROR_NO_SIGNAL_SENT:
231 case ERROR_NO_SYSTEM_RESOURCES:
235 case ERROR_OPEN_FAILED:
237 case ERROR_OPEN_FILES:
239 case ERROR_OUTOFMEMORY:
241 case ERROR_PAGED_SYSTEM_RESOURCES:
243 case ERROR_PAGEFILE_QUOTA:
245 case ERROR_PATH_NOT_FOUND:
247 case ERROR_PIPE_BUSY:
249 case ERROR_PIPE_CONNECTED:
252 case ERROR_PIPE_LISTENING:
254 case ERROR_PIPE_NOT_CONNECTED:
257 case ERROR_POSSIBLE_DEADLOCK:
259 case ERROR_PRIVILEGE_NOT_HELD:
261 case ERROR_PROCESS_ABORTED:
263 case ERROR_PROC_NOT_FOUND:
266 case ERROR_REM_NOT_LIST:
269 case ERROR_SECTOR_NOT_FOUND:
273 case ERROR_SETMARK_DETECTED:
275 case ERROR_SHARING_BUFFER_EXCEEDED:
277 case ERROR_SHARING_VIOLATION:
279 case ERROR_SIGNAL_PENDING:
281 case ERROR_SIGNAL_REFUSED:
284 case ERROR_SXS_CANT_GEN_ACTCTX:
287 case ERROR_THREAD_1_INACTIVE:
289 case ERROR_TOO_MANY_LINKS:
291 case ERROR_TOO_MANY_OPEN_FILES:
293 case ERROR_WAIT_NO_CHILDREN:
295 case ERROR_WORKING_SET_QUOTA:
297 case ERROR_WRITE_PROTECT:
305 set_errno_from_win32_error(DWORD err)
307 errno = win32_error_to_errno(err);
311 set_errno_from_GetLastError(void)
313 set_errno_from_win32_error(GetLastError());
317 set_errno_from_nt_status(NTSTATUS status)
319 set_errno_from_win32_error((*func_RtlNtStatusToDosError)(status));
323 win32_modify_privilege(const wchar_t *privilege, bool enable)
327 TOKEN_PRIVILEGES newState;
330 if (!OpenProcessToken(GetCurrentProcess(),
331 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
335 if (!LookupPrivilegeValue(NULL, privilege, &luid))
336 goto out_close_handle;
338 newState.PrivilegeCount = 1;
339 newState.Privileges[0].Luid = luid;
340 newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
341 SetLastError(ERROR_SUCCESS);
342 ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
343 if (ret && GetLastError() == ERROR_NOT_ALL_ASSIGNED)
352 win32_modify_capture_privileges(bool enable)
354 return win32_modify_privilege(SE_BACKUP_NAME, enable)
355 && win32_modify_privilege(SE_SECURITY_NAME, enable);
359 win32_modify_apply_privileges(bool enable)
361 return win32_modify_privilege(SE_RESTORE_NAME, enable)
362 && win32_modify_privilege(SE_SECURITY_NAME, enable)
363 && win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
367 win32_release_capture_and_apply_privileges(void)
369 win32_modify_capture_privileges(false);
370 win32_modify_apply_privileges(false);
373 /* Pointers to dynamically loaded functions */
377 NTSTATUS (WINAPI *func_NtCreateFile)(PHANDLE FileHandle,
378 ACCESS_MASK DesiredAccess,
379 POBJECT_ATTRIBUTES ObjectAttributes,
380 PIO_STATUS_BLOCK IoStatusBlock,
381 PLARGE_INTEGER AllocationSize,
382 ULONG FileAttributes,
384 ULONG CreateDisposition,
389 NTSTATUS (WINAPI *func_NtOpenFile) (PHANDLE FileHandle,
390 ACCESS_MASK DesiredAccess,
391 POBJECT_ATTRIBUTES ObjectAttributes,
392 PIO_STATUS_BLOCK IoStatusBlock,
396 NTSTATUS (WINAPI *func_NtReadFile) (HANDLE FileHandle,
398 PIO_APC_ROUTINE ApcRoutine,
400 PIO_STATUS_BLOCK IoStatusBlock,
403 PLARGE_INTEGER ByteOffset,
406 NTSTATUS (WINAPI *func_NtWriteFile) (HANDLE FileHandle,
408 PIO_APC_ROUTINE ApcRoutine,
410 PIO_STATUS_BLOCK IoStatusBlock,
413 PLARGE_INTEGER ByteOffset,
416 NTSTATUS (WINAPI *func_NtQueryInformationFile)(HANDLE FileHandle,
417 PIO_STATUS_BLOCK IoStatusBlock,
418 PVOID FileInformation,
420 FILE_INFORMATION_CLASS FileInformationClass);
422 NTSTATUS (WINAPI *func_NtQuerySecurityObject)(HANDLE handle,
423 SECURITY_INFORMATION SecurityInformation,
424 PSECURITY_DESCRIPTOR SecurityDescriptor,
426 PULONG LengthNeeded);
428 NTSTATUS (WINAPI *func_NtQueryDirectoryFile) (HANDLE FileHandle,
430 PIO_APC_ROUTINE ApcRoutine,
432 PIO_STATUS_BLOCK IoStatusBlock,
433 PVOID FileInformation,
435 FILE_INFORMATION_CLASS FileInformationClass,
436 BOOLEAN ReturnSingleEntry,
437 PUNICODE_STRING FileName,
438 BOOLEAN RestartScan);
440 NTSTATUS (WINAPI *func_NtQueryVolumeInformationFile) (HANDLE FileHandle,
441 PIO_STATUS_BLOCK IoStatusBlock,
444 FS_INFORMATION_CLASS FsInformationClass);
446 NTSTATUS (WINAPI *func_NtSetInformationFile)(HANDLE FileHandle,
447 PIO_STATUS_BLOCK IoStatusBlock,
448 PVOID FileInformation,
450 FILE_INFORMATION_CLASS FileInformationClass);
452 NTSTATUS (WINAPI *func_NtSetSecurityObject)(HANDLE Handle,
453 SECURITY_INFORMATION SecurityInformation,
454 PSECURITY_DESCRIPTOR SecurityDescriptor);
456 NTSTATUS (WINAPI *func_NtFsControlFile) (HANDLE FileHandle,
458 PIO_APC_ROUTINE ApcRoutine,
460 PIO_STATUS_BLOCK IoStatusBlock,
463 ULONG InputBufferLength,
465 ULONG OutputBufferLength);
467 NTSTATUS (WINAPI *func_NtClose) (HANDLE Handle);
469 DWORD (WINAPI *func_RtlNtStatusToDosError)(NTSTATUS status);
471 BOOLEAN (WINAPI *func_RtlDosPathNameToNtPathName_U)
473 OUT PUNICODE_STRING NtName,
474 OUT PCWSTR *PartName,
475 OUT PRTL_RELATIVE_NAME_U RelativeName);
477 NTSTATUS (WINAPI *func_RtlDosPathNameToNtPathName_U_WithStatus)
479 OUT PUNICODE_STRING NtName,
480 OUT PCWSTR *PartName,
481 OUT PRTL_RELATIVE_NAME_U RelativeName);
483 NTSTATUS (WINAPI *func_RtlCreateSystemVolumeInformationFolder)
484 (PCUNICODE_STRING VolumeRootPath);
486 static bool acquired_privileges = false;
494 #define DLL_SYM(name, required) { (void **)&func_##name, #name, required }
496 #define for_each_sym(sym, spec) \
497 for ((sym) = (spec)->syms; (sym)->name; (sym)++)
502 const struct dll_sym syms[];
505 struct dll_spec ntdll_spec = {
506 .name = L"ntdll.dll",
508 DLL_SYM(NtCreateFile, true),
509 DLL_SYM(NtOpenFile, true),
510 DLL_SYM(NtReadFile, true),
511 DLL_SYM(NtWriteFile, true),
512 DLL_SYM(NtQueryInformationFile, true),
513 DLL_SYM(NtQuerySecurityObject, true),
514 DLL_SYM(NtQueryDirectoryFile, true),
515 DLL_SYM(NtQueryVolumeInformationFile, true),
516 DLL_SYM(NtSetInformationFile, true),
517 DLL_SYM(NtSetSecurityObject, true),
518 DLL_SYM(NtFsControlFile, true),
519 DLL_SYM(NtClose, true),
520 DLL_SYM(RtlNtStatusToDosError, true),
521 DLL_SYM(RtlCreateSystemVolumeInformationFolder, false),
522 DLL_SYM(RtlDosPathNameToNtPathName_U, true),
523 DLL_SYM(RtlDosPathNameToNtPathName_U_WithStatus, false), /* Not present on XP */
529 init_dll(struct dll_spec *spec)
531 const struct dll_sym *sym;
535 spec->handle = LoadLibrary(spec->name);
537 for_each_sym(sym, spec) {
539 ERROR("%ls could not be loaded!", spec->name);
540 return WIMLIB_ERR_UNSUPPORTED;
545 for_each_sym(sym, spec) {
546 addr = (void *)GetProcAddress(spec->handle, sym->name);
548 *(sym->func_ptr) = addr;
549 } else if (sym->required) {
550 ERROR("Can't find %s in %ls", sym->name, spec->name);
551 return WIMLIB_ERR_UNSUPPORTED;
558 cleanup_dll(struct dll_spec *spec)
560 const struct dll_sym *sym;
563 FreeLibrary(spec->handle);
566 for_each_sym(sym, spec)
567 *(sym->func_ptr) = NULL;
571 /* One-time initialization for Windows capture/apply code. */
573 win32_global_init(int init_flags)
577 /* Try to acquire useful privileges. */
578 if (!(init_flags & WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES)) {
579 ret = WIMLIB_ERR_INSUFFICIENT_PRIVILEGES;
580 if (!win32_modify_capture_privileges(true))
581 if (init_flags & WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES)
583 if (!win32_modify_apply_privileges(true))
584 if (init_flags & WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES)
586 acquired_privileges = true;
589 ret = init_dll(&ntdll_spec);
596 win32_release_capture_and_apply_privileges();
601 win32_global_cleanup(void)
603 if (acquired_privileges)
604 win32_release_capture_and_apply_privileges();
606 cleanup_dll(&ntdll_spec);
610 * Translates a Win32-namespace path into an NT-namespace path.
612 * On success, returns 0. The NT-namespace path will be stored in the
613 * UNICODE_STRING structure pointed to by nt_path. nt_path->Buffer will be set
614 * to a new buffer that must later be freed with HeapFree(). (Really
615 * RtlHeapFree(), but HeapFree() seems to be the same thing.)
617 * On failure, returns WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_PARAM.
620 win32_path_to_nt_path(const wchar_t *win32_path, UNICODE_STRING *nt_path)
624 if (func_RtlDosPathNameToNtPathName_U_WithStatus) {
625 status = (*func_RtlDosPathNameToNtPathName_U_WithStatus)(win32_path,
629 if ((*func_RtlDosPathNameToNtPathName_U)(win32_path, nt_path,
631 status = STATUS_SUCCESS;
633 status = STATUS_NO_MEMORY;
636 if (likely(NT_SUCCESS(status)))
639 if (status == STATUS_NO_MEMORY)
640 return WIMLIB_ERR_NOMEM;
642 ERROR("\"%ls\": invalid path name (status=0x%08"PRIx32")",
643 win32_path, (u32)status);
644 return WIMLIB_ERR_INVALID_PARAM;
648 win32_get_drive_path(const wchar_t *file_path, wchar_t drive_path[7])
652 file_abspath = realpath(file_path, NULL);
654 return WIMLIB_ERR_NOMEM;
656 if (file_abspath[0] == L'\0' || file_abspath[1] != L':') {
657 ERROR("\"%ls\": Path format not recognized", file_abspath);
659 return WIMLIB_ERR_UNSUPPORTED;
662 wsprintf(drive_path, L"\\\\.\\%lc:", file_abspath[0]);
668 #endif /* __WIN32__ */