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"
31 #include "wimlib/error.h"
32 #include "wimlib/util.h"
35 win32_error_to_errno(DWORD err_code)
37 /* This mapping is that used in Cygwin.
38 * Some of these choices are arbitrary. */
40 case ERROR_ACCESS_DENIED:
42 case ERROR_ACTIVE_CONNECTIONS:
44 case ERROR_ALREADY_EXISTS:
46 case ERROR_BAD_DEVICE:
48 case ERROR_BAD_EXE_FORMAT:
50 case ERROR_BAD_NETPATH:
52 case ERROR_BAD_NET_NAME:
54 case ERROR_BAD_NET_RESP:
56 case ERROR_BAD_PATHNAME:
62 case ERROR_BAD_USERNAME:
64 case ERROR_BEGINNING_OF_MEDIA:
66 case ERROR_BROKEN_PIPE:
72 case ERROR_CALL_NOT_IMPLEMENTED:
74 case ERROR_CANNOT_MAKE:
76 case ERROR_CHILD_NOT_COMPLETE:
78 case ERROR_COMMITMENT_LIMIT:
82 case ERROR_DEVICE_DOOR_OPEN:
84 case ERROR_DEVICE_IN_USE:
86 case ERROR_DEVICE_REQUIRES_CLEANING:
90 case ERROR_DIR_NOT_EMPTY:
92 case ERROR_DISK_CORRUPT:
100 case ERROR_EAS_DIDNT_FIT:
103 case ERROR_EAS_NOT_SUPPORTED:
106 case ERROR_EA_LIST_INCONSISTENT:
108 case ERROR_EA_TABLE_FULL:
110 case ERROR_END_OF_MEDIA:
112 case ERROR_EOM_OVERFLOW:
114 case ERROR_EXE_MACHINE_TYPE_MISMATCH:
116 case ERROR_EXE_MARKED_INVALID:
118 case ERROR_FILEMARK_DETECTED:
120 case ERROR_FILENAME_EXCED_RANGE:
122 case ERROR_FILE_CORRUPT:
124 case ERROR_FILE_EXISTS:
126 case ERROR_FILE_INVALID:
128 case ERROR_FILE_NOT_FOUND:
130 case ERROR_HANDLE_DISK_FULL:
133 case ERROR_HANDLE_EOF:
136 case ERROR_INVALID_ADDRESS:
138 case ERROR_INVALID_AT_INTERRUPT_TIME:
140 case ERROR_INVALID_BLOCK_LENGTH:
142 case ERROR_INVALID_DATA:
144 case ERROR_INVALID_DRIVE:
146 case ERROR_INVALID_EA_NAME:
148 case ERROR_INVALID_EXE_SIGNATURE:
151 case ERROR_INVALID_FUNCTION:
154 case ERROR_INVALID_HANDLE:
156 case ERROR_INVALID_NAME:
158 case ERROR_INVALID_PARAMETER:
160 case ERROR_INVALID_SIGNAL_NUMBER:
162 case ERROR_IOPL_NOT_ENABLED:
164 case ERROR_IO_DEVICE:
166 case ERROR_IO_INCOMPLETE:
168 case ERROR_IO_PENDING:
170 case ERROR_LOCK_VIOLATION:
172 case ERROR_MAX_THRDS_REACHED:
174 case ERROR_META_EXPANSION_TOO_LONG:
176 case ERROR_MOD_NOT_FOUND:
179 case ERROR_MORE_DATA:
182 case ERROR_NEGATIVE_SEEK:
184 case ERROR_NETNAME_DELETED:
188 case ERROR_NONE_MAPPED:
190 case ERROR_NONPAGED_SYSTEM_RESOURCES:
193 case ERROR_NOT_CONNECTED:
196 case ERROR_NOT_ENOUGH_MEMORY:
198 case ERROR_NOT_OWNER:
201 case ERROR_NOT_READY:
204 case ERROR_NOT_SAME_DEVICE:
206 case ERROR_NOT_SUPPORTED:
210 case ERROR_NO_DATA_DETECTED:
213 case ERROR_NO_MEDIA_IN_DRIVE:
217 case ERROR_NO_MORE_FILES:
221 case ERROR_NO_MORE_ITEMS:
224 case ERROR_NO_MORE_SEARCH_HANDLES:
226 case ERROR_NO_PROC_SLOTS:
228 case ERROR_NO_SIGNAL_SENT:
230 case ERROR_NO_SYSTEM_RESOURCES:
234 case ERROR_OPEN_FAILED:
236 case ERROR_OPEN_FILES:
238 case ERROR_OUTOFMEMORY:
240 case ERROR_PAGED_SYSTEM_RESOURCES:
242 case ERROR_PAGEFILE_QUOTA:
244 case ERROR_PATH_NOT_FOUND:
246 case ERROR_PIPE_BUSY:
248 case ERROR_PIPE_CONNECTED:
251 case ERROR_PIPE_LISTENING:
253 case ERROR_PIPE_NOT_CONNECTED:
256 case ERROR_POSSIBLE_DEADLOCK:
258 case ERROR_PRIVILEGE_NOT_HELD:
260 case ERROR_PROCESS_ABORTED:
262 case ERROR_PROC_NOT_FOUND:
265 case ERROR_REM_NOT_LIST:
268 case ERROR_SECTOR_NOT_FOUND:
272 case ERROR_SETMARK_DETECTED:
274 case ERROR_SHARING_BUFFER_EXCEEDED:
276 case ERROR_SHARING_VIOLATION:
278 case ERROR_SIGNAL_PENDING:
280 case ERROR_SIGNAL_REFUSED:
283 case ERROR_SXS_CANT_GEN_ACTCTX:
286 case ERROR_THREAD_1_INACTIVE:
288 case ERROR_TOO_MANY_LINKS:
290 case ERROR_TOO_MANY_OPEN_FILES:
292 case ERROR_WAIT_NO_CHILDREN:
294 case ERROR_WORKING_SET_QUOTA:
296 case ERROR_WRITE_PROTECT:
304 set_errno_from_win32_error(DWORD err)
306 errno = win32_error_to_errno(err);
310 set_errno_from_GetLastError(void)
312 set_errno_from_win32_error(GetLastError());
316 set_errno_from_nt_status(NTSTATUS status)
318 set_errno_from_win32_error((*func_RtlNtStatusToDosError)(status));
322 win32_modify_privilege(const wchar_t *privilege, bool enable)
326 TOKEN_PRIVILEGES newState;
329 if (!OpenProcessToken(GetCurrentProcess(),
330 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
334 if (!LookupPrivilegeValue(NULL, privilege, &luid))
335 goto out_close_handle;
337 newState.PrivilegeCount = 1;
338 newState.Privileges[0].Luid = luid;
339 newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
340 SetLastError(ERROR_SUCCESS);
341 ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
342 if (ret && GetLastError() == ERROR_NOT_ALL_ASSIGNED)
351 win32_modify_capture_privileges(bool enable)
353 return win32_modify_privilege(SE_BACKUP_NAME, enable)
354 && win32_modify_privilege(SE_SECURITY_NAME, enable);
358 win32_modify_apply_privileges(bool enable)
360 return win32_modify_privilege(SE_RESTORE_NAME, enable)
361 && win32_modify_privilege(SE_SECURITY_NAME, enable)
362 && win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
366 win32_release_capture_and_apply_privileges(void)
368 win32_modify_capture_privileges(false);
369 win32_modify_apply_privileges(false);
372 /* Pointers to dynamically loaded functions */
376 NTSTATUS (WINAPI *func_NtCreateFile)(PHANDLE FileHandle,
377 ACCESS_MASK DesiredAccess,
378 POBJECT_ATTRIBUTES ObjectAttributes,
379 PIO_STATUS_BLOCK IoStatusBlock,
380 PLARGE_INTEGER AllocationSize,
381 ULONG FileAttributes,
383 ULONG CreateDisposition,
388 NTSTATUS (WINAPI *func_NtOpenFile) (PHANDLE FileHandle,
389 ACCESS_MASK DesiredAccess,
390 POBJECT_ATTRIBUTES ObjectAttributes,
391 PIO_STATUS_BLOCK IoStatusBlock,
395 NTSTATUS (WINAPI *func_NtReadFile) (HANDLE FileHandle,
397 PIO_APC_ROUTINE ApcRoutine,
399 PIO_STATUS_BLOCK IoStatusBlock,
402 PLARGE_INTEGER ByteOffset,
405 NTSTATUS (WINAPI *func_NtWriteFile) (HANDLE FileHandle,
407 PIO_APC_ROUTINE ApcRoutine,
409 PIO_STATUS_BLOCK IoStatusBlock,
412 PLARGE_INTEGER ByteOffset,
415 NTSTATUS (WINAPI *func_NtQueryInformationFile)(HANDLE FileHandle,
416 PIO_STATUS_BLOCK IoStatusBlock,
417 PVOID FileInformation,
419 FILE_INFORMATION_CLASS FileInformationClass);
421 NTSTATUS (WINAPI *func_NtQuerySecurityObject)(HANDLE handle,
422 SECURITY_INFORMATION SecurityInformation,
423 PSECURITY_DESCRIPTOR SecurityDescriptor,
425 PULONG LengthNeeded);
427 NTSTATUS (WINAPI *func_NtQueryDirectoryFile) (HANDLE FileHandle,
429 PIO_APC_ROUTINE ApcRoutine,
431 PIO_STATUS_BLOCK IoStatusBlock,
432 PVOID FileInformation,
434 FILE_INFORMATION_CLASS FileInformationClass,
435 BOOLEAN ReturnSingleEntry,
436 PUNICODE_STRING FileName,
437 BOOLEAN RestartScan);
439 NTSTATUS (WINAPI *func_NtQueryVolumeInformationFile) (HANDLE FileHandle,
440 PIO_STATUS_BLOCK IoStatusBlock,
443 FS_INFORMATION_CLASS FsInformationClass);
445 NTSTATUS (WINAPI *func_NtSetInformationFile)(HANDLE FileHandle,
446 PIO_STATUS_BLOCK IoStatusBlock,
447 PVOID FileInformation,
449 FILE_INFORMATION_CLASS FileInformationClass);
451 NTSTATUS (WINAPI *func_NtSetSecurityObject)(HANDLE Handle,
452 SECURITY_INFORMATION SecurityInformation,
453 PSECURITY_DESCRIPTOR SecurityDescriptor);
455 NTSTATUS (WINAPI *func_NtFsControlFile) (HANDLE FileHandle,
457 PIO_APC_ROUTINE ApcRoutine,
459 PIO_STATUS_BLOCK IoStatusBlock,
462 ULONG InputBufferLength,
464 ULONG OutputBufferLength);
466 NTSTATUS (WINAPI *func_NtClose) (HANDLE Handle);
468 DWORD (WINAPI *func_RtlNtStatusToDosError)(NTSTATUS status);
470 BOOLEAN (WINAPI *func_RtlDosPathNameToNtPathName_U)
472 OUT PUNICODE_STRING NtName,
473 OUT PCWSTR *PartName,
474 OUT PRTL_RELATIVE_NAME_U RelativeName);
476 NTSTATUS (WINAPI *func_RtlDosPathNameToNtPathName_U_WithStatus)
478 OUT PUNICODE_STRING NtName,
479 OUT PCWSTR *PartName,
480 OUT PRTL_RELATIVE_NAME_U RelativeName);
482 NTSTATUS (WINAPI *func_RtlCreateSystemVolumeInformationFolder)
483 (PCUNICODE_STRING VolumeRootPath);
485 static bool acquired_privileges = false;
493 #define DLL_SYM(name, required) { (void **)&func_##name, #name, required }
495 #define for_each_sym(sym, spec) \
496 for ((sym) = (spec)->syms; (sym)->name; (sym)++)
501 const struct dll_sym syms[];
504 struct dll_spec ntdll_spec = {
505 .name = L"ntdll.dll",
507 DLL_SYM(NtCreateFile, true),
508 DLL_SYM(NtOpenFile, true),
509 DLL_SYM(NtReadFile, true),
510 DLL_SYM(NtWriteFile, true),
511 DLL_SYM(NtQueryInformationFile, true),
512 DLL_SYM(NtQuerySecurityObject, true),
513 DLL_SYM(NtQueryDirectoryFile, true),
514 DLL_SYM(NtQueryVolumeInformationFile, true),
515 DLL_SYM(NtSetInformationFile, true),
516 DLL_SYM(NtSetSecurityObject, true),
517 DLL_SYM(NtFsControlFile, true),
518 DLL_SYM(NtClose, true),
519 DLL_SYM(RtlNtStatusToDosError, true),
520 DLL_SYM(RtlCreateSystemVolumeInformationFolder, false),
521 DLL_SYM(RtlDosPathNameToNtPathName_U, true),
522 DLL_SYM(RtlDosPathNameToNtPathName_U_WithStatus, false), /* Not present on XP */
528 init_dll(struct dll_spec *spec)
530 const struct dll_sym *sym;
534 spec->handle = LoadLibrary(spec->name);
536 for_each_sym(sym, spec) {
538 ERROR("%ls could not be loaded!", spec->name);
539 return WIMLIB_ERR_UNSUPPORTED;
544 for_each_sym(sym, spec) {
545 addr = (void *)GetProcAddress(spec->handle, sym->name);
547 *(sym->func_ptr) = addr;
548 } else if (sym->required) {
549 ERROR("Can't find %s in %ls", sym->name, spec->name);
550 return WIMLIB_ERR_UNSUPPORTED;
557 cleanup_dll(struct dll_spec *spec)
559 const struct dll_sym *sym;
562 FreeLibrary(spec->handle);
565 for_each_sym(sym, spec)
566 *(sym->func_ptr) = NULL;
570 /* One-time initialization for Windows capture/apply code. */
572 win32_global_init(int init_flags)
576 /* Try to acquire useful privileges. */
577 if (!(init_flags & WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES)) {
578 ret = WIMLIB_ERR_INSUFFICIENT_PRIVILEGES;
579 if (!win32_modify_capture_privileges(true))
580 if (init_flags & WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES)
582 if (!win32_modify_apply_privileges(true))
583 if (init_flags & WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES)
585 acquired_privileges = true;
588 ret = init_dll(&ntdll_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(&ntdll_spec);
609 * Translates a Win32-namespace path into an NT-namespace path.
611 * On success, returns 0. The NT-namespace path will be stored in the
612 * UNICODE_STRING structure pointed to by nt_path. nt_path->Buffer will be set
613 * to a new buffer that must later be freed with HeapFree(). (Really
614 * RtlHeapFree(), but HeapFree() seems to be the same thing.)
616 * On failure, returns WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_PARAM.
619 win32_path_to_nt_path(const wchar_t *win32_path, UNICODE_STRING *nt_path)
623 if (func_RtlDosPathNameToNtPathName_U_WithStatus) {
624 status = (*func_RtlDosPathNameToNtPathName_U_WithStatus)(win32_path,
628 if ((*func_RtlDosPathNameToNtPathName_U)(win32_path, nt_path,
630 status = STATUS_SUCCESS;
632 status = STATUS_NO_MEMORY;
635 if (likely(NT_SUCCESS(status)))
638 if (status == STATUS_NO_MEMORY)
639 return WIMLIB_ERR_NOMEM;
641 ERROR("\"%ls\": invalid path name (status=0x%08"PRIx32")",
642 win32_path, (u32)status);
643 return WIMLIB_ERR_INVALID_PARAM;
647 win32_get_drive_path(const wchar_t *file_path, wchar_t drive_path[7])
651 file_abspath = realpath(file_path, NULL);
653 return WIMLIB_ERR_NOMEM;
655 if (file_abspath[0] == L'\0' || file_abspath[1] != L':') {
656 ERROR("\"%ls\": Path format not recognized", file_abspath);
658 return WIMLIB_ERR_UNSUPPORTED;
661 wsprintf(drive_path, L"\\\\.\\%lc:", file_abspath[0]);
667 #endif /* __WIN32__ */