2014-09-22 19:24:12 +00:00
|
|
|
/*
|
2016-08-09 22:10:05 +00:00
|
|
|
* PROJECT: Filesystem Filter Manager library
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
* FILE: dll/win32/fltlib/fltlib.c
|
|
|
|
* PURPOSE:
|
|
|
|
* PROGRAMMERS: Ged Murphy (ged.murphy@reactos.org)
|
|
|
|
*/
|
2014-09-22 19:24:12 +00:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2016-08-09 22:10:05 +00:00
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
|
2017-12-07 23:06:13 +00:00
|
|
|
#include <windef.h>
|
|
|
|
#include <winbase.h>
|
2016-08-09 22:10:05 +00:00
|
|
|
|
|
|
|
#define NTOS_MODE_USER
|
|
|
|
#include <ndk/iofuncs.h>
|
|
|
|
#include <ndk/obfuncs.h>
|
|
|
|
#include <ndk/rtlfuncs.h>
|
2016-09-06 15:52:38 +00:00
|
|
|
#include <fltmgr_shared.h>
|
2016-08-09 22:10:05 +00:00
|
|
|
|
|
|
|
/* DATA ****************************************************************************/
|
|
|
|
|
|
|
|
static
|
|
|
|
HRESULT
|
|
|
|
FilterLoadUnload(_In_z_ LPCWSTR lpFilterName,
|
|
|
|
_In_ BOOL Load);
|
2014-09-22 19:24:12 +00:00
|
|
|
|
2016-08-09 22:10:05 +00:00
|
|
|
static
|
|
|
|
DWORD
|
|
|
|
SendIoctl(_In_ HANDLE Handle,
|
|
|
|
_In_ ULONG IoControlCode,
|
|
|
|
_In_reads_bytes_opt_(BufferSize) LPVOID lpInBuffer,
|
|
|
|
_In_ DWORD BufferSize);
|
2014-09-22 19:24:12 +00:00
|
|
|
|
|
|
|
|
2016-08-09 22:10:05 +00:00
|
|
|
/* PUBLIC FUNCTIONS ****************************************************************/
|
2014-09-22 19:24:12 +00:00
|
|
|
|
2016-08-09 22:10:05 +00:00
|
|
|
_Must_inspect_result_
|
|
|
|
HRESULT
|
|
|
|
WINAPI
|
|
|
|
FilterLoad(_In_ LPCWSTR lpFilterName)
|
2014-09-22 19:24:12 +00:00
|
|
|
{
|
2016-08-09 22:10:05 +00:00
|
|
|
return FilterLoadUnload(lpFilterName, TRUE);
|
2014-09-22 19:24:12 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 22:10:05 +00:00
|
|
|
_Must_inspect_result_
|
|
|
|
HRESULT
|
|
|
|
WINAPI
|
|
|
|
FilterUnload(_In_ LPCWSTR lpFilterName)
|
2014-09-22 19:24:12 +00:00
|
|
|
{
|
2016-08-09 22:10:05 +00:00
|
|
|
return FilterLoadUnload(lpFilterName, FALSE);
|
2014-09-22 19:24:12 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 22:10:05 +00:00
|
|
|
|
|
|
|
/* PRIVATE FUNCTIONS ****************************************************************/
|
|
|
|
|
2017-05-18 22:06:49 +00:00
|
|
|
HRESULT
|
|
|
|
NtStatusToHResult(_In_ NTSTATUS Status)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
hr = RtlNtStatusToDosError(Status);
|
|
|
|
if (hr != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
hr = (ULONG)hr | 0x80070000;
|
|
|
|
}
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2016-08-09 22:10:05 +00:00
|
|
|
static
|
|
|
|
HRESULT
|
|
|
|
FilterLoadUnload(_In_z_ LPCWSTR lpFilterName,
|
|
|
|
_In_ BOOL Load)
|
2015-07-19 13:14:56 +00:00
|
|
|
{
|
2016-09-06 15:52:38 +00:00
|
|
|
PFILTER_NAME FilterName;
|
2016-08-09 22:10:05 +00:00
|
|
|
HANDLE hFltMgr;
|
2018-04-23 09:42:32 +00:00
|
|
|
SIZE_T StringLength;
|
|
|
|
SIZE_T BufferLength;
|
2016-08-09 22:10:05 +00:00
|
|
|
DWORD dwError;
|
|
|
|
|
|
|
|
/* Get a handle to the filter manager */
|
2016-09-06 15:52:38 +00:00
|
|
|
hFltMgr = CreateFileW(L"\\\\.\\fltmgr",
|
|
|
|
GENERIC_WRITE,
|
2016-08-09 22:10:05 +00:00
|
|
|
FILE_SHARE_WRITE,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
&hFltMgr);
|
|
|
|
if (hFltMgr == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
dwError = GetLastError();
|
|
|
|
return HRESULT_FROM_WIN32(dwError);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calc and allocate a buffer to hold our filter name */
|
2016-09-06 15:52:38 +00:00
|
|
|
StringLength = wcslen(lpFilterName) * sizeof(WCHAR);
|
|
|
|
BufferLength = StringLength + sizeof(FILTER_NAME);
|
2016-08-09 22:10:05 +00:00
|
|
|
FilterName = RtlAllocateHeap(GetProcessHeap(),
|
|
|
|
0,
|
2016-09-06 15:52:38 +00:00
|
|
|
BufferLength);
|
2016-08-09 22:10:05 +00:00
|
|
|
if (FilterName == NULL)
|
|
|
|
{
|
|
|
|
CloseHandle(hFltMgr);
|
|
|
|
return HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
|
|
|
|
}
|
|
|
|
|
2016-09-06 15:52:38 +00:00
|
|
|
/* Build up the filter name */
|
|
|
|
FilterName->Length = StringLength;
|
|
|
|
CopyMemory(FilterName->FilterName, lpFilterName, StringLength);
|
2016-08-09 22:10:05 +00:00
|
|
|
|
|
|
|
/* Tell the filter manager to load the filter for us */
|
|
|
|
dwError = SendIoctl(hFltMgr,
|
2017-05-18 22:06:49 +00:00
|
|
|
Load ? IOCTL_FILTER_LOAD : IOCTL_FILTER_UNLOAD,
|
2016-08-09 22:10:05 +00:00
|
|
|
FilterName,
|
2016-09-06 15:52:38 +00:00
|
|
|
BufferLength);
|
2016-08-09 22:10:05 +00:00
|
|
|
|
2016-11-12 21:53:33 +00:00
|
|
|
/* Cleanup and bail*/
|
2016-09-06 15:52:38 +00:00
|
|
|
RtlFreeHeap(GetProcessHeap(), 0, FilterName);
|
2016-08-09 22:10:05 +00:00
|
|
|
CloseHandle(hFltMgr);
|
2016-09-06 15:52:38 +00:00
|
|
|
|
2016-08-09 22:10:05 +00:00
|
|
|
return HRESULT_FROM_WIN32(dwError);
|
2015-07-19 13:14:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 22:10:05 +00:00
|
|
|
static
|
|
|
|
DWORD
|
|
|
|
SendIoctl(_In_ HANDLE Handle,
|
|
|
|
_In_ ULONG IoControlCode,
|
|
|
|
_In_reads_bytes_opt_(BufferSize) LPVOID lpInBuffer,
|
|
|
|
_In_ DWORD BufferSize)
|
2014-09-22 19:24:12 +00:00
|
|
|
{
|
2016-08-09 22:10:05 +00:00
|
|
|
IO_STATUS_BLOCK IoStatusBlock;
|
|
|
|
NTSTATUS Status;
|
2014-09-22 19:24:12 +00:00
|
|
|
|
2016-08-09 22:10:05 +00:00
|
|
|
Status = NtDeviceIoControlFile(Handle,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&IoStatusBlock,
|
|
|
|
IoControlCode,
|
|
|
|
lpInBuffer,
|
|
|
|
BufferSize,
|
|
|
|
NULL,
|
|
|
|
0);
|
|
|
|
if (Status == STATUS_PENDING)
|
|
|
|
{
|
|
|
|
Status = NtWaitForSingleObject(Handle, FALSE, NULL);
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
Status = IoStatusBlock.Status;
|
|
|
|
}
|
|
|
|
}
|
2014-09-22 19:24:12 +00:00
|
|
|
|
2016-08-09 22:10:05 +00:00
|
|
|
return RtlNtStatusToDosError(Status);
|
2014-09-22 19:24:12 +00:00
|
|
|
}
|
2016-08-09 22:10:05 +00:00
|
|
|
|
|
|
|
BOOL
|
|
|
|
WINAPI
|
|
|
|
DllMain(_In_ HINSTANCE hinstDLL,
|
|
|
|
_In_ DWORD dwReason,
|
|
|
|
_In_ LPVOID lpvReserved)
|
|
|
|
{
|
|
|
|
switch (dwReason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
DisableThreadLibraryCalls(hinstDLL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|