From 1ad43de1a123e599348cb6dd1eb870d6ded6d807 Mon Sep 17 00:00:00 2001 From: Bill Zissimopoulos Date: Wed, 25 Nov 2015 16:04:05 -0800 Subject: [PATCH] dll: fsctl.c, ntstatus.c --- build/VStudio/winfsp_dll.vcxproj | 1 + build/VStudio/winfsp_dll.vcxproj.filters | 3 + inc/winfsp/fsctl.h | 8 +- inc/winfsp/winfsp.h | 2 + src/dll/fsctl.c | 96 ++++++++++++++++++++++++ src/dll/ntstatus.c | 12 +++ 6 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 src/dll/ntstatus.c diff --git a/build/VStudio/winfsp_dll.vcxproj b/build/VStudio/winfsp_dll.vcxproj index b03b1b0d..66962919 100644 --- a/build/VStudio/winfsp_dll.vcxproj +++ b/build/VStudio/winfsp_dll.vcxproj @@ -164,6 +164,7 @@ + diff --git a/build/VStudio/winfsp_dll.vcxproj.filters b/build/VStudio/winfsp_dll.vcxproj.filters index 03c515d9..85aaea75 100644 --- a/build/VStudio/winfsp_dll.vcxproj.filters +++ b/build/VStudio/winfsp_dll.vcxproj.filters @@ -17,6 +17,9 @@ Source + + Source + diff --git a/inc/winfsp/fsctl.h b/inc/winfsp/fsctl.h index 13cb84f3..fb6acf0a 100644 --- a/inc/winfsp/fsctl.h +++ b/inc/winfsp/fsctl.h @@ -29,7 +29,7 @@ extern const __declspec(selectany) GUID FspFsvrtDeviceClassGuid = #define FSP_FSCTL_TRANSACT \ CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 0x800 + 'T', METHOD_BUFFERED, FILE_ANY_ACCESS) -#define FSP_FSCTL_CREATE_BUFFER_SIZE 64 +#define FSP_FSCTL_CREATE_BUFFER_SIZE 128 #define FSP_FSCTL_TRANSACT_BUFFER_SIZE 4096 #define FSP_FSCTL_TRANSACT_REQ_SIZEMAX 1536 @@ -60,8 +60,10 @@ typedef struct #pragma warning(pop) #if !defined(WINFSP_SYS_DRIVER_H_INTERNAL) -NTSTATUS FspFsctlCreateVolume(PSECURITY_DESCRIPTOR SecurityDescriptor, PHANDLE *PHandle); -NTSTATUS FspFsctlOpenVolume(PWSTR VolumeName); +NTSTATUS FspFsctlCreateVolume(PWSTR DevicePath, PSECURITY_DESCRIPTOR SecurityDescriptor, + PHANDLE *PHandle); +NTSTATUS FspFsctlOpenVolume(PWSTR VolumePath, + PHANDLE *PHandle); NTSTATUS FspFsctlDeleteVolume(HANDLE Handle); NTSTATUS FspFsctlTransact(HANDLE Handle, const FSP_TRANSACT_RSP *Responses, size_t NumResponses, diff --git a/inc/winfsp/winfsp.h b/inc/winfsp/winfsp.h index da1b6314..dff14250 100644 --- a/inc/winfsp/winfsp.h +++ b/inc/winfsp/winfsp.h @@ -13,4 +13,6 @@ #include #include +NTSTATUS FspNtStatusFromWin32(DWORD Error); + #endif diff --git a/src/dll/fsctl.c b/src/dll/fsctl.c index 0e008698..75291b01 100644 --- a/src/dll/fsctl.c +++ b/src/dll/fsctl.c @@ -6,3 +6,99 @@ #include #include +#include + +#define GLOBALROOT L"\\\\?\\GLOBALROOT" + +NTSTATUS FspFsctlCreateVolume(PWSTR DevicePath, PSECURITY_DESCRIPTOR SecurityDescriptor, + PHANDLE *PHandle) +{ + NTSTATUS Result = STATUS_SUCCESS; + WCHAR DevicePathBuf[(sizeof GLOBALROOT + FSP_FSCTL_CREATE_BUFFER_SIZE) / sizeof(WCHAR)]; + WCHAR VolumePathBuf[FSP_FSCTL_CREATE_BUFFER_SIZE / sizeof(WCHAR)]; + PSECURITY_DESCRIPTOR SecurityDescriptorBuf = 0; + DWORD SecurityDescriptorLen, Bytes; + HANDLE DeviceHandle = INVALID_HANDLE_VALUE; + + *PHandle = 0; + + DevicePathBuf[0] = L'\0'; + StringCbPrintf(DevicePathBuf, sizeof DevicePathBuf, GLOBALROOT "%S", DevicePath); + + if (!MakeSelfRelativeSD(SecurityDescriptor, 0, &SecurityDescriptorLen)) + { + SecurityDescriptorBuf = malloc(SecurityDescriptorLen); + if (0 == SecurityDescriptorBuf) + { + Result = STATUS_INSUFFICIENT_RESOURCES; + goto exit; + } + if (!MakeSelfRelativeSD(SecurityDescriptor, SecurityDescriptorBuf, &SecurityDescriptorLen)) + { + Result = FspNtStatusFromWin32(GetLastError()); + goto exit; + } + } + + DeviceHandle = CreateFileW(DevicePathBuf, + 0, FILE_SHARE_WRITE | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0, OPEN_EXISTING, 0, 0); + if (INVALID_HANDLE_VALUE == DeviceHandle) + { + Result = FspNtStatusFromWin32(GetLastError()); + goto exit; + } + + if (!DeviceIoControl(DeviceHandle, FSP_FSCTL_CREATE, + SecurityDescriptorBuf, SecurityDescriptorLen, VolumePathBuf, sizeof VolumePathBuf, + &Bytes, 0)) + { + Result = FspNtStatusFromWin32(GetLastError()); + goto exit; + } + + Result = FspFsctlOpenVolume(VolumePathBuf, PHandle); + +exit: + if (INVALID_HANDLE_VALUE != DeviceHandle) + CloseHandle(DeviceHandle); + free(SecurityDescriptorBuf); + return Result; +} + +NTSTATUS FspFsctlOpenVolume(PWSTR VolumePath, + PHANDLE *PHandle) +{ + NTSTATUS Result = STATUS_SUCCESS; + WCHAR DevicePathBuf[(sizeof GLOBALROOT + FSP_FSCTL_CREATE_BUFFER_SIZE) / sizeof(WCHAR)]; + HANDLE DeviceHandle; + + *PHandle = 0; + + DevicePathBuf[0] = L'\0'; + StringCbPrintf(DevicePathBuf, sizeof DevicePathBuf, GLOBALROOT "%S", VolumePath); + + DeviceHandle = CreateFileW(DevicePathBuf, + 0, FILE_SHARE_WRITE | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0, OPEN_EXISTING, 0, 0); + if (INVALID_HANDLE_VALUE == DeviceHandle) + { + Result = FspNtStatusFromWin32(GetLastError()); + goto exit; + } + + *PHandle = DeviceHandle; + +exit: + return Result; +} + +NTSTATUS FspFsctlDeleteVolume(HANDLE Handle) +{ + return STATUS_NOT_IMPLEMENTED; +} + +NTSTATUS FspFsctlTransact(HANDLE Handle, + const FSP_TRANSACT_RSP *Responses, size_t NumResponses, + const FSP_TRANSACT_REQ *Requests, size_t *NumRequests) +{ + return STATUS_NOT_IMPLEMENTED; +} diff --git a/src/dll/ntstatus.c b/src/dll/ntstatus.c new file mode 100644 index 00000000..16a4c963 --- /dev/null +++ b/src/dll/ntstatus.c @@ -0,0 +1,12 @@ +/** + * @file dll/ntstatus.c + * + * @copyright 2015 Bill Zissimopoulos + */ + +#include + +NTSTATUS FspNtStatusFromWin32(DWORD Error) +{ + return STATUS_ACCESS_DENIED; +}