]> wimlib.net Git - wimlib/blobdiff - src/win32_common.c
Directly link with ntdll on Windows
[wimlib] / src / win32_common.c
index bc75686e8a8498a837c36546aebc8ef005eafe61..086397abd5acd8f9f994bf4d38d4a2e61d9acd15 100644 (file)
@@ -3,7 +3,7 @@
  */
 
 /*
- * Copyright (C) 2013, 2014, 2015 Eric Biggers
+ * Copyright (C) 2013-2016 Eric Biggers
  *
  * This file is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the Free
 
 #include "wimlib/win32_common.h"
 
+#include "wimlib/assert.h"
 #include "wimlib/error.h"
 #include "wimlib/util.h"
+#include "wimlib/win32_vss.h"
 
 static bool
 win32_modify_privilege(const wchar_t *privilege, bool enable)
@@ -62,16 +64,21 @@ out:
 static bool
 win32_modify_capture_privileges(bool enable)
 {
-       return win32_modify_privilege(SE_BACKUP_NAME, enable)
-           && win32_modify_privilege(SE_SECURITY_NAME, enable);
+       bool ok = true;
+       ok &= win32_modify_privilege(SE_BACKUP_NAME, enable);
+       ok &= win32_modify_privilege(SE_SECURITY_NAME, enable);
+       return ok;
 }
 
 static bool
 win32_modify_apply_privileges(bool enable)
 {
-       return win32_modify_privilege(SE_RESTORE_NAME, enable)
-           && win32_modify_privilege(SE_SECURITY_NAME, enable)
-           && win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
+       bool ok = true;
+       ok &= win32_modify_privilege(SE_RESTORE_NAME, enable);
+       ok &= win32_modify_privilege(SE_SECURITY_NAME, enable);
+       ok &= win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
+       ok &= win32_modify_privilege(SE_MANAGE_VOLUME_NAME, enable);
+       return ok;
 }
 
 static void
@@ -85,106 +92,6 @@ win32_release_capture_and_apply_privileges(void)
 
 /* ntdll.dll  */
 
-NTSTATUS (WINAPI *func_NtCreateFile)(PHANDLE FileHandle,
-                                    ACCESS_MASK DesiredAccess,
-                                    POBJECT_ATTRIBUTES ObjectAttributes,
-                                    PIO_STATUS_BLOCK IoStatusBlock,
-                                    PLARGE_INTEGER AllocationSize,
-                                    ULONG FileAttributes,
-                                    ULONG ShareAccess,
-                                    ULONG CreateDisposition,
-                                    ULONG CreateOptions,
-                                    PVOID EaBuffer,
-                                    ULONG EaLength);
-
-NTSTATUS (WINAPI *func_NtOpenFile) (PHANDLE FileHandle,
-                                   ACCESS_MASK DesiredAccess,
-                                   POBJECT_ATTRIBUTES ObjectAttributes,
-                                   PIO_STATUS_BLOCK IoStatusBlock,
-                                   ULONG ShareAccess,
-                                   ULONG OpenOptions);
-
-NTSTATUS (WINAPI *func_NtReadFile) (HANDLE FileHandle,
-                                   HANDLE Event,
-                                   PIO_APC_ROUTINE ApcRoutine,
-                                   PVOID ApcContext,
-                                   PIO_STATUS_BLOCK IoStatusBlock,
-                                   PVOID Buffer,
-                                   ULONG Length,
-                                   PLARGE_INTEGER ByteOffset,
-                                   PULONG Key);
-
-NTSTATUS (WINAPI *func_NtWriteFile) (HANDLE FileHandle,
-                                    HANDLE Event,
-                                    PIO_APC_ROUTINE ApcRoutine,
-                                    PVOID ApcContext,
-                                    PIO_STATUS_BLOCK IoStatusBlock,
-                                    PVOID Buffer,
-                                    ULONG Length,
-                                    PLARGE_INTEGER ByteOffset,
-                                    PULONG Key);
-
-NTSTATUS (WINAPI *func_NtQueryInformationFile)(HANDLE FileHandle,
-                                              PIO_STATUS_BLOCK IoStatusBlock,
-                                              PVOID FileInformation,
-                                              ULONG Length,
-                                              FILE_INFORMATION_CLASS FileInformationClass);
-
-NTSTATUS (WINAPI *func_NtQuerySecurityObject)(HANDLE handle,
-                                             SECURITY_INFORMATION SecurityInformation,
-                                             PSECURITY_DESCRIPTOR SecurityDescriptor,
-                                             ULONG Length,
-                                             PULONG LengthNeeded);
-
-NTSTATUS (WINAPI *func_NtQueryDirectoryFile) (HANDLE FileHandle,
-                                             HANDLE Event,
-                                             PIO_APC_ROUTINE ApcRoutine,
-                                             PVOID ApcContext,
-                                             PIO_STATUS_BLOCK IoStatusBlock,
-                                             PVOID FileInformation,
-                                             ULONG Length,
-                                             FILE_INFORMATION_CLASS FileInformationClass,
-                                             BOOLEAN ReturnSingleEntry,
-                                             PUNICODE_STRING FileName,
-                                             BOOLEAN RestartScan);
-
-NTSTATUS (WINAPI *func_NtQueryVolumeInformationFile) (HANDLE FileHandle,
-                                                     PIO_STATUS_BLOCK IoStatusBlock,
-                                                     PVOID FsInformation,
-                                                     ULONG Length,
-                                                     FS_INFORMATION_CLASS FsInformationClass);
-
-NTSTATUS (WINAPI *func_NtSetInformationFile)(HANDLE FileHandle,
-                                            PIO_STATUS_BLOCK IoStatusBlock,
-                                            PVOID FileInformation,
-                                            ULONG Length,
-                                            FILE_INFORMATION_CLASS FileInformationClass);
-
-NTSTATUS (WINAPI *func_NtSetSecurityObject)(HANDLE Handle,
-                                           SECURITY_INFORMATION SecurityInformation,
-                                           PSECURITY_DESCRIPTOR SecurityDescriptor);
-
-NTSTATUS (WINAPI *func_NtFsControlFile) (HANDLE FileHandle,
-                                        HANDLE Event,
-                                        PIO_APC_ROUTINE ApcRoutine,
-                                        PVOID ApcContext,
-                                        PIO_STATUS_BLOCK IoStatusBlock,
-                                        ULONG FsControlCode,
-                                        PVOID InputBuffer,
-                                        ULONG InputBufferLength,
-                                        PVOID OutputBuffer,
-                                        ULONG OutputBufferLength);
-
-NTSTATUS (WINAPI *func_NtClose) (HANDLE Handle);
-
-DWORD (WINAPI *func_RtlNtStatusToDosError)(NTSTATUS status);
-
-BOOLEAN (WINAPI *func_RtlDosPathNameToNtPathName_U)
-                 (IN PCWSTR DosName,
-                  OUT PUNICODE_STRING NtName,
-                  OUT PCWSTR *PartName,
-                  OUT PRTL_RELATIVE_NAME_U RelativeName);
-
 NTSTATUS (WINAPI *func_RtlDosPathNameToNtPathName_U_WithStatus)
                (IN PCWSTR DosName,
                 OUT PUNICODE_STRING NtName,
@@ -216,21 +123,7 @@ struct dll_spec {
 struct dll_spec ntdll_spec = {
        .name = L"ntdll.dll",
        .syms = {
-               DLL_SYM(NtCreateFile, true),
-               DLL_SYM(NtOpenFile, true),
-               DLL_SYM(NtReadFile, true),
-               DLL_SYM(NtWriteFile, true),
-               DLL_SYM(NtQueryInformationFile, true),
-               DLL_SYM(NtQuerySecurityObject, true),
-               DLL_SYM(NtQueryDirectoryFile, true),
-               DLL_SYM(NtQueryVolumeInformationFile, true),
-               DLL_SYM(NtSetInformationFile, true),
-               DLL_SYM(NtSetSecurityObject, true),
-               DLL_SYM(NtFsControlFile, true),
-               DLL_SYM(NtClose, true),
-               DLL_SYM(RtlNtStatusToDosError, true),
                DLL_SYM(RtlCreateSystemVolumeInformationFolder, false),
-               DLL_SYM(RtlDosPathNameToNtPathName_U, true),
                DLL_SYM(RtlDosPathNameToNtPathName_U_WithStatus, false), /* Not present on XP  */
                {NULL, NULL},
        },
@@ -311,6 +204,8 @@ out_drop_privs:
 void
 win32_global_cleanup(void)
 {
+       vss_global_cleanup();
+
        if (acquired_privileges)
                win32_release_capture_and_apply_privileges();
 
@@ -337,8 +232,7 @@ win32_path_to_nt_path(const wchar_t *win32_path, UNICODE_STRING *nt_path)
                                                                         nt_path,
                                                                         NULL, NULL);
        } else {
-               if ((*func_RtlDosPathNameToNtPathName_U)(win32_path, nt_path,
-                                                        NULL, NULL))
+               if (RtlDosPathNameToNtPathName_U(win32_path, nt_path, NULL, NULL))
                        status = STATUS_SUCCESS;
                else
                        status = STATUS_NO_MEMORY;
@@ -374,6 +268,51 @@ win32_get_drive_path(const wchar_t *file_path, wchar_t drive_path[7])
        return 0;
 }
 
+/* Try to attach an instance of the Windows Overlay File System Filter Driver to
+ * the specified drive (such as C:)  */
+bool
+win32_try_to_attach_wof(const wchar_t *drive)
+{
+       HMODULE fltlib;
+       bool retval = false;
+
+       /* Use FilterAttach() from Fltlib.dll.  */
+
+       fltlib = LoadLibrary(L"Fltlib.dll");
+
+       if (!fltlib) {
+               WARNING("Failed to load Fltlib.dll");
+               return retval;
+       }
+
+       HRESULT (WINAPI *func_FilterAttach)(LPCWSTR lpFilterName,
+                                           LPCWSTR lpVolumeName,
+                                           LPCWSTR lpInstanceName,
+                                           DWORD dwCreatedInstanceNameLength,
+                                           LPWSTR lpCreatedInstanceName);
+
+       func_FilterAttach = (void *)GetProcAddress(fltlib, "FilterAttach");
+
+       if (func_FilterAttach) {
+               HRESULT res;
+
+               res = (*func_FilterAttach)(L"wof", drive, NULL, 0, NULL);
+
+               if (res != S_OK)
+                       res = (*func_FilterAttach)(L"wofadk", drive, NULL, 0, NULL);
+
+               if (res == S_OK)
+                       retval = true;
+       } else {
+               WARNING("FilterAttach() does not exist in Fltlib.dll");
+       }
+
+       FreeLibrary(fltlib);
+
+       return retval;
+}
+
+
 static void
 windows_msg(u32 code, const wchar_t *format, va_list va,
            bool is_ntstatus, bool is_error)
@@ -399,9 +338,11 @@ retry:
        if (n >= buflen)
                goto realloc;
 
-       ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
-                           NULL,
-                           is_ntstatus ? (*func_RtlNtStatusToDosError)(code) : code,
+       ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
+                               FORMAT_MESSAGE_IGNORE_INSERTS |
+                               (is_ntstatus ? FORMAT_MESSAGE_FROM_HMODULE : 0),
+                           (is_ntstatus ? ntdll_spec.handle : NULL),
+                           code,
                            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                            &buf[n],
                            buflen - n,
@@ -476,4 +417,39 @@ winnt_error(NTSTATUS status, const wchar_t *format, ...)
        va_end(va);
 }
 
+/*
+ * Synchronously execute a filesystem control method.  This is a wrapper around
+ * NtFsControlFile() that handles STATUS_PENDING.  Note that SYNCHRONIZE
+ * permission is, in general, required on the handle.
+ */
+NTSTATUS
+winnt_fsctl(HANDLE h, u32 code, const void *in, u32 in_size,
+           void *out, u32 out_size_avail, u32 *actual_out_size_ret)
+{
+       IO_STATUS_BLOCK iosb;
+       NTSTATUS status;
+
+       status = NtFsControlFile(h, NULL, NULL, NULL, &iosb, code,
+                                (void *)in, in_size, out, out_size_avail);
+       if (status == STATUS_PENDING) {
+               /* Beware: this case is often encountered with remote
+                * filesystems, but rarely with local filesystems.  */
+
+               status = NtWaitForSingleObject(h, FALSE, NULL);
+               if (NT_SUCCESS(status)) {
+                       status = iosb.Status;
+               } else {
+                       /* We shouldn't be issuing ioctls on a handle to which
+                        * we don't have SYNCHRONIZE access.  Otherwise we have
+                        * no way to wait for them to complete.  */
+                       wimlib_assert(status != STATUS_ACCESS_DENIED);
+               }
+       }
+
+       if (NT_SUCCESS(status) && actual_out_size_ret != NULL)
+               *actual_out_size_ret = (u32)iosb.Information;
+
+       return status;
+}
+
 #endif /* __WIN32__ */