2 * win32_common.c - Windows code common to applying and capturing images.
6 * Copyright (C) 2013-2016 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/assert.h"
31 #include "wimlib/error.h"
32 #include "wimlib/util.h"
33 #include "wimlib/win32_vss.h"
36 win32_modify_privilege(const wchar_t *privilege, bool enable)
40 TOKEN_PRIVILEGES newState;
43 if (!OpenProcessToken(GetCurrentProcess(),
44 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
48 if (!LookupPrivilegeValue(NULL, privilege, &luid))
49 goto out_close_handle;
51 newState.PrivilegeCount = 1;
52 newState.Privileges[0].Luid = luid;
53 newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
54 SetLastError(ERROR_SUCCESS);
55 ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
56 if (ret && GetLastError() == ERROR_NOT_ALL_ASSIGNED)
65 win32_modify_capture_privileges(bool enable)
68 ok &= win32_modify_privilege(SE_BACKUP_NAME, enable);
69 ok &= win32_modify_privilege(SE_SECURITY_NAME, enable);
74 win32_modify_apply_privileges(bool enable)
77 ok &= win32_modify_privilege(SE_RESTORE_NAME, enable);
78 ok &= win32_modify_privilege(SE_SECURITY_NAME, enable);
79 ok &= win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
80 ok &= win32_modify_privilege(SE_MANAGE_VOLUME_NAME, enable);
85 win32_release_capture_and_apply_privileges(void)
87 win32_modify_capture_privileges(false);
88 win32_modify_apply_privileges(false);
91 /* Pointers to dynamically loaded functions */
93 NTSTATUS (WINAPI *func_RtlDosPathNameToNtPathName_U_WithStatus)
95 OUT PUNICODE_STRING NtName,
97 OUT PRTL_RELATIVE_NAME_U RelativeName);
99 NTSTATUS (WINAPI *func_RtlCreateSystemVolumeInformationFolder)
100 (PCUNICODE_STRING VolumeRootPath);
102 static bool acquired_privileges = false;
104 static HMODULE ntdll_handle = NULL;
109 ntdll_handle = LoadLibrary(L"ntdll.dll");
112 ERROR("Unable to load ntdll.dll");
113 return WIMLIB_ERR_UNSUPPORTED;
116 func_RtlDosPathNameToNtPathName_U_WithStatus =
117 (void *)GetProcAddress(ntdll_handle,
118 "RtlDosPathNameToNtPathName_U_WithStatus");
120 func_RtlCreateSystemVolumeInformationFolder =
121 (void *)GetProcAddress(ntdll_handle,
122 "RtlCreateSystemVolumeInformationFolder");
126 /* One-time initialization for Windows capture/apply code. */
128 win32_global_init(int init_flags)
132 /* Try to acquire useful privileges. */
133 if (!(init_flags & WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES)) {
134 ret = WIMLIB_ERR_INSUFFICIENT_PRIVILEGES;
135 if (!win32_modify_capture_privileges(true))
136 if (init_flags & WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES)
138 if (!win32_modify_apply_privileges(true))
139 if (init_flags & WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES)
141 acquired_privileges = true;
151 win32_release_capture_and_apply_privileges();
156 win32_global_cleanup(void)
158 vss_global_cleanup();
160 if (acquired_privileges)
161 win32_release_capture_and_apply_privileges();
163 FreeLibrary(ntdll_handle);
168 * Translates a Win32-namespace path into an NT-namespace path.
170 * On success, returns 0. The NT-namespace path will be stored in the
171 * UNICODE_STRING structure pointed to by nt_path. nt_path->Buffer will be set
172 * to a new buffer that must later be freed with HeapFree(). (Really
173 * RtlHeapFree(), but HeapFree() seems to be the same thing.)
175 * On failure, returns WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_PARAM.
178 win32_path_to_nt_path(const wchar_t *win32_path, UNICODE_STRING *nt_path)
182 if (func_RtlDosPathNameToNtPathName_U_WithStatus) {
183 status = (*func_RtlDosPathNameToNtPathName_U_WithStatus)(win32_path,
187 if (RtlDosPathNameToNtPathName_U(win32_path, nt_path, NULL, NULL))
188 status = STATUS_SUCCESS;
190 status = STATUS_NO_MEMORY;
193 if (likely(NT_SUCCESS(status)))
196 if (status == STATUS_NO_MEMORY)
197 return WIMLIB_ERR_NOMEM;
199 winnt_error(status, L"\"%ls\": invalid path name", win32_path);
200 return WIMLIB_ERR_INVALID_PARAM;
204 win32_get_drive_path(const wchar_t *file_path, wchar_t drive_path[7])
208 file_abspath = realpath(file_path, NULL);
210 return WIMLIB_ERR_NOMEM;
212 if (file_abspath[0] == L'\0' || file_abspath[1] != L':') {
213 ERROR("\"%ls\": Path format not recognized", file_abspath);
215 return WIMLIB_ERR_UNSUPPORTED;
218 wsprintf(drive_path, L"\\\\.\\%lc:", file_abspath[0]);
223 /* Try to attach an instance of the Windows Overlay Filesystem filter driver to
224 * the specified drive (such as C:) */
226 win32_try_to_attach_wof(const wchar_t *drive)
231 /* Use FilterAttach() from Fltlib.dll. */
233 fltlib = LoadLibrary(L"Fltlib.dll");
236 WARNING("Failed to load Fltlib.dll");
240 HRESULT (WINAPI *func_FilterAttach)(LPCWSTR lpFilterName,
241 LPCWSTR lpVolumeName,
242 LPCWSTR lpInstanceName,
243 DWORD dwCreatedInstanceNameLength,
244 LPWSTR lpCreatedInstanceName);
246 func_FilterAttach = (void *)GetProcAddress(fltlib, "FilterAttach");
248 if (func_FilterAttach) {
251 res = (*func_FilterAttach)(L"wof", drive, NULL, 0, NULL);
254 res = (*func_FilterAttach)(L"wofadk", drive, NULL, 0, NULL);
259 WARNING("FilterAttach() does not exist in Fltlib.dll");
269 windows_msg(u32 code, const wchar_t *format, va_list va,
270 bool is_ntstatus, bool is_error)
272 wchar_t _buf[STACK_MAX / 8];
274 size_t buflen = ARRAY_LEN(_buf);
281 n = vsnwprintf(buf, buflen, format, va2);
287 n += snwprintf(&buf[n], buflen - n,
289 L" (status=%08"PRIx32"): " :
290 L" (err=%"PRIu32"): "),
296 ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
297 FORMAT_MESSAGE_IGNORE_INSERTS |
298 (is_ntstatus ? FORMAT_MESSAGE_FROM_HMODULE : 0),
299 (is_ntstatus ? ntdll_handle : NULL),
301 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
307 if (n >= buflen || (ret == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER))
310 if (buf[n - 1] == L'\n')
312 if (buf[n - 1] == L'\r')
314 if (buf[n - 1] == L'.')
329 buf = MALLOC(buflen * sizeof(buf[0]));
332 ERROR("Ran out of memory while building error message!!!");
336 win32_warning(DWORD err, const wchar_t *format, ...)
340 va_start(va, format);
341 windows_msg(err, format, va, false, false);
346 win32_error(DWORD err, const wchar_t *format, ...)
350 va_start(va, format);
351 windows_msg(err, format, va, false, true);
356 winnt_warning(NTSTATUS status, const wchar_t *format, ...)
360 va_start(va, format);
361 windows_msg(status, format, va, true, false);
366 winnt_error(NTSTATUS status, const wchar_t *format, ...)
370 va_start(va, format);
371 windows_msg(status, format, va, true, true);
376 * Synchronously execute a filesystem control method. This is a wrapper around
377 * NtFsControlFile() that handles STATUS_PENDING. Note that SYNCHRONIZE
378 * permission is, in general, required on the handle.
381 winnt_fsctl(HANDLE h, u32 code, const void *in, u32 in_size,
382 void *out, u32 out_size_avail, u32 *actual_out_size_ret)
384 IO_STATUS_BLOCK iosb;
387 status = NtFsControlFile(h, NULL, NULL, NULL, &iosb, code,
388 (void *)in, in_size, out, out_size_avail);
389 if (status == STATUS_PENDING) {
390 /* Beware: this case is often encountered with remote
391 * filesystems, but rarely with local filesystems. */
393 status = NtWaitForSingleObject(h, FALSE, NULL);
394 if (NT_SUCCESS(status)) {
395 status = iosb.Status;
397 /* We shouldn't be issuing ioctls on a handle to which
398 * we don't have SYNCHRONIZE access. Otherwise we have
399 * no way to wait for them to complete. */
400 wimlib_assert(status != STATUS_ACCESS_DENIED);
404 if (NT_SUCCESS(status) && actual_out_size_ret != NULL)
405 *actual_out_size_ret = (u32)iosb.Information;