2 * win32_common.c - Windows code common to applying and capturing images.
6 * Copyright (C) 2013, 2014, 2015 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/.
28 #include "wimlib/win32_common.h"
30 #include "wimlib/error.h"
31 #include "wimlib/util.h"
34 win32_modify_privilege(const wchar_t *privilege, bool enable)
38 TOKEN_PRIVILEGES newState;
41 if (!OpenProcessToken(GetCurrentProcess(),
42 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
46 if (!LookupPrivilegeValue(NULL, privilege, &luid))
47 goto out_close_handle;
49 newState.PrivilegeCount = 1;
50 newState.Privileges[0].Luid = luid;
51 newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
52 SetLastError(ERROR_SUCCESS);
53 ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
54 if (ret && GetLastError() == ERROR_NOT_ALL_ASSIGNED)
63 win32_modify_capture_privileges(bool enable)
66 ok &= win32_modify_privilege(SE_BACKUP_NAME, enable);
67 ok &= win32_modify_privilege(SE_SECURITY_NAME, enable);
72 win32_modify_apply_privileges(bool enable)
75 ok &= win32_modify_privilege(SE_RESTORE_NAME, enable);
76 ok &= win32_modify_privilege(SE_SECURITY_NAME, enable);
77 ok &= win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
78 ok &= win32_modify_privilege(SE_MANAGE_VOLUME_NAME, enable);
83 win32_release_capture_and_apply_privileges(void)
85 win32_modify_capture_privileges(false);
86 win32_modify_apply_privileges(false);
89 /* Pointers to dynamically loaded functions */
93 NTSTATUS (WINAPI *func_NtCreateFile)(PHANDLE FileHandle,
94 ACCESS_MASK DesiredAccess,
95 POBJECT_ATTRIBUTES ObjectAttributes,
96 PIO_STATUS_BLOCK IoStatusBlock,
97 PLARGE_INTEGER AllocationSize,
100 ULONG CreateDisposition,
105 NTSTATUS (WINAPI *func_NtOpenFile) (PHANDLE FileHandle,
106 ACCESS_MASK DesiredAccess,
107 POBJECT_ATTRIBUTES ObjectAttributes,
108 PIO_STATUS_BLOCK IoStatusBlock,
112 NTSTATUS (WINAPI *func_NtReadFile) (HANDLE FileHandle,
114 PIO_APC_ROUTINE ApcRoutine,
116 PIO_STATUS_BLOCK IoStatusBlock,
119 PLARGE_INTEGER ByteOffset,
122 NTSTATUS (WINAPI *func_NtWriteFile) (HANDLE FileHandle,
124 PIO_APC_ROUTINE ApcRoutine,
126 PIO_STATUS_BLOCK IoStatusBlock,
129 PLARGE_INTEGER ByteOffset,
132 NTSTATUS (WINAPI *func_NtQueryInformationFile)(HANDLE FileHandle,
133 PIO_STATUS_BLOCK IoStatusBlock,
134 PVOID FileInformation,
136 FILE_INFORMATION_CLASS FileInformationClass);
138 NTSTATUS (WINAPI *func_NtQuerySecurityObject)(HANDLE handle,
139 SECURITY_INFORMATION SecurityInformation,
140 PSECURITY_DESCRIPTOR SecurityDescriptor,
142 PULONG LengthNeeded);
144 NTSTATUS (WINAPI *func_NtQueryDirectoryFile) (HANDLE FileHandle,
146 PIO_APC_ROUTINE ApcRoutine,
148 PIO_STATUS_BLOCK IoStatusBlock,
149 PVOID FileInformation,
151 FILE_INFORMATION_CLASS FileInformationClass,
152 BOOLEAN ReturnSingleEntry,
153 PUNICODE_STRING FileName,
154 BOOLEAN RestartScan);
156 NTSTATUS (WINAPI *func_NtQueryVolumeInformationFile) (HANDLE FileHandle,
157 PIO_STATUS_BLOCK IoStatusBlock,
160 FS_INFORMATION_CLASS FsInformationClass);
162 NTSTATUS (WINAPI *func_NtSetInformationFile)(HANDLE FileHandle,
163 PIO_STATUS_BLOCK IoStatusBlock,
164 PVOID FileInformation,
166 FILE_INFORMATION_CLASS FileInformationClass);
168 NTSTATUS (WINAPI *func_NtSetSecurityObject)(HANDLE Handle,
169 SECURITY_INFORMATION SecurityInformation,
170 PSECURITY_DESCRIPTOR SecurityDescriptor);
172 NTSTATUS (WINAPI *func_NtFsControlFile) (HANDLE FileHandle,
174 PIO_APC_ROUTINE ApcRoutine,
176 PIO_STATUS_BLOCK IoStatusBlock,
179 ULONG InputBufferLength,
181 ULONG OutputBufferLength);
183 NTSTATUS (WINAPI *func_NtClose) (HANDLE Handle);
185 DWORD (WINAPI *func_RtlNtStatusToDosError)(NTSTATUS status);
187 BOOLEAN (WINAPI *func_RtlDosPathNameToNtPathName_U)
189 OUT PUNICODE_STRING NtName,
190 OUT PCWSTR *PartName,
191 OUT PRTL_RELATIVE_NAME_U RelativeName);
193 NTSTATUS (WINAPI *func_RtlDosPathNameToNtPathName_U_WithStatus)
195 OUT PUNICODE_STRING NtName,
196 OUT PCWSTR *PartName,
197 OUT PRTL_RELATIVE_NAME_U RelativeName);
199 NTSTATUS (WINAPI *func_RtlCreateSystemVolumeInformationFolder)
200 (PCUNICODE_STRING VolumeRootPath);
202 static bool acquired_privileges = false;
210 #define DLL_SYM(name, required) { (void **)&func_##name, #name, required }
212 #define for_each_sym(sym, spec) \
213 for ((sym) = (spec)->syms; (sym)->name; (sym)++)
218 const struct dll_sym syms[];
221 struct dll_spec ntdll_spec = {
222 .name = L"ntdll.dll",
224 DLL_SYM(NtCreateFile, true),
225 DLL_SYM(NtOpenFile, true),
226 DLL_SYM(NtReadFile, true),
227 DLL_SYM(NtWriteFile, true),
228 DLL_SYM(NtQueryInformationFile, true),
229 DLL_SYM(NtQuerySecurityObject, true),
230 DLL_SYM(NtQueryDirectoryFile, true),
231 DLL_SYM(NtQueryVolumeInformationFile, true),
232 DLL_SYM(NtSetInformationFile, true),
233 DLL_SYM(NtSetSecurityObject, true),
234 DLL_SYM(NtFsControlFile, true),
235 DLL_SYM(NtClose, true),
236 DLL_SYM(RtlNtStatusToDosError, true),
237 DLL_SYM(RtlCreateSystemVolumeInformationFolder, false),
238 DLL_SYM(RtlDosPathNameToNtPathName_U, true),
239 DLL_SYM(RtlDosPathNameToNtPathName_U_WithStatus, false), /* Not present on XP */
245 init_dll(struct dll_spec *spec)
247 const struct dll_sym *sym;
251 spec->handle = LoadLibrary(spec->name);
253 for_each_sym(sym, spec) {
255 ERROR("%ls could not be loaded!", spec->name);
256 return WIMLIB_ERR_UNSUPPORTED;
261 for_each_sym(sym, spec) {
262 addr = (void *)GetProcAddress(spec->handle, sym->name);
264 *(sym->func_ptr) = addr;
265 } else if (sym->required) {
266 ERROR("Can't find %s in %ls", sym->name, spec->name);
267 return WIMLIB_ERR_UNSUPPORTED;
274 cleanup_dll(struct dll_spec *spec)
276 const struct dll_sym *sym;
279 FreeLibrary(spec->handle);
282 for_each_sym(sym, spec)
283 *(sym->func_ptr) = NULL;
287 /* One-time initialization for Windows capture/apply code. */
289 win32_global_init(int init_flags)
293 /* Try to acquire useful privileges. */
294 if (!(init_flags & WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES)) {
295 ret = WIMLIB_ERR_INSUFFICIENT_PRIVILEGES;
296 if (!win32_modify_capture_privileges(true))
297 if (init_flags & WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES)
299 if (!win32_modify_apply_privileges(true))
300 if (init_flags & WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES)
302 acquired_privileges = true;
305 ret = init_dll(&ntdll_spec);
312 win32_release_capture_and_apply_privileges();
317 win32_global_cleanup(void)
319 if (acquired_privileges)
320 win32_release_capture_and_apply_privileges();
322 cleanup_dll(&ntdll_spec);
326 * Translates a Win32-namespace path into an NT-namespace path.
328 * On success, returns 0. The NT-namespace path will be stored in the
329 * UNICODE_STRING structure pointed to by nt_path. nt_path->Buffer will be set
330 * to a new buffer that must later be freed with HeapFree(). (Really
331 * RtlHeapFree(), but HeapFree() seems to be the same thing.)
333 * On failure, returns WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_PARAM.
336 win32_path_to_nt_path(const wchar_t *win32_path, UNICODE_STRING *nt_path)
340 if (func_RtlDosPathNameToNtPathName_U_WithStatus) {
341 status = (*func_RtlDosPathNameToNtPathName_U_WithStatus)(win32_path,
345 if ((*func_RtlDosPathNameToNtPathName_U)(win32_path, nt_path,
347 status = STATUS_SUCCESS;
349 status = STATUS_NO_MEMORY;
352 if (likely(NT_SUCCESS(status)))
355 if (status == STATUS_NO_MEMORY)
356 return WIMLIB_ERR_NOMEM;
358 winnt_error(status, L"\"%ls\": invalid path name", win32_path);
359 return WIMLIB_ERR_INVALID_PARAM;
363 win32_get_drive_path(const wchar_t *file_path, wchar_t drive_path[7])
367 file_abspath = realpath(file_path, NULL);
369 return WIMLIB_ERR_NOMEM;
371 if (file_abspath[0] == L'\0' || file_abspath[1] != L':') {
372 ERROR("\"%ls\": Path format not recognized", file_abspath);
374 return WIMLIB_ERR_UNSUPPORTED;
377 wsprintf(drive_path, L"\\\\.\\%lc:", file_abspath[0]);
383 windows_msg(u32 code, const wchar_t *format, va_list va,
384 bool is_ntstatus, bool is_error)
386 wchar_t _buf[STACK_MAX / 8];
388 size_t buflen = ARRAY_LEN(_buf);
393 n = vsnwprintf(buf, buflen, format, va);
398 n += snwprintf(&buf[n], buflen - n,
400 L" (status=%08"PRIx32"): " :
401 L" (err=%"PRIu32"): "),
407 ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
409 is_ntstatus ? (*func_RtlNtStatusToDosError)(code) : code,
410 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
416 if (n >= buflen || (ret == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER))
419 if (buf[n - 1] == L'\n')
421 if (buf[n - 1] == L'\r')
423 if (buf[n - 1] == L'.')
438 buf = MALLOC(buflen * sizeof(buf[0]));
441 ERROR("Ran out of memory while building error message!!!");
445 win32_warning(DWORD err, const wchar_t *format, ...)
449 va_start(va, format);
450 windows_msg(err, format, va, false, false);
455 win32_error(DWORD err, const wchar_t *format, ...)
459 va_start(va, format);
460 windows_msg(err, format, va, false, true);
465 winnt_warning(NTSTATUS status, const wchar_t *format, ...)
469 va_start(va, format);
470 windows_msg(status, format, va, true, false);
475 winnt_error(NTSTATUS status, const wchar_t *format, ...)
479 va_start(va, format);
480 windows_msg(status, format, va, true, true);
484 #endif /* __WIN32__ */