[FLTMC][NFSD] Fix version definition
[reactos.git] / reactos / base / applications / fltmc / fltmc.cpp
1 /*
2 * PROJECT: ReactOS fltmc utility
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/fltmc/fltmc.c
5 * PURPOSE: Control utility for file system filter drivers
6 * PROGRAMMERS: Copyright 2016 Ged Murphy (gedmurphy@gmail.com)
7 */
8
9 // Please leave this temporary hack in place
10 // it's used to keep VS2015 happy for development.
11 #ifdef __REACTOS__
12 #include <stdarg.h>
13 #include <windef.h>
14 #include <winbase.h>
15 #include <wchar.h>
16 #else
17 #include <Windows.h>
18 #endif
19 #include <fltuser.h>
20 #include <atlstr.h>
21 #include "resource.h"
22
23 EXTERN_C int wmain(int argc, WCHAR *argv[]);
24
25 void
26 LoadAndPrintString(ULONG MessageId, ...)
27 {
28 va_list args;
29
30 CAtlStringW Message;
31 if (Message.LoadStringW(MessageId))
32 {
33 va_start(args, MessageId);
34 vwprintf(Message.GetBuffer(), args);
35 va_end(args);
36 }
37 }
38
39 void
40 PrintErrorText(_In_ ULONG ErrorCode)
41 {
42 WCHAR Buffer[256];
43 if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM,
44 0,
45 ErrorCode,
46 0,
47 Buffer,
48 256,
49 0))
50 {
51 wprintf(L"%s\n", Buffer);
52 }
53 }
54
55 DWORD
56 SetDriverLoadPrivilege()
57 {
58 TOKEN_PRIVILEGES TokenPrivileges;
59 HANDLE hToken;
60 LUID luid;
61 BOOL bSuccess;
62 DWORD dwError = ERROR_SUCCESS;
63
64 bSuccess = OpenProcessToken(GetCurrentProcess(),
65 TOKEN_ADJUST_PRIVILEGES,
66 &hToken);
67 if (bSuccess == FALSE)
68 return GetLastError();
69
70 if (!LookupPrivilegeValueW(NULL, SE_LOAD_DRIVER_NAME, &luid))
71 return GetLastError();
72
73 TokenPrivileges.PrivilegeCount = 1;
74 TokenPrivileges.Privileges[0].Luid = luid;
75 TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
76
77 bSuccess = AdjustTokenPrivileges(hToken,
78 FALSE,
79 &TokenPrivileges,
80 sizeof(TOKEN_PRIVILEGES),
81 NULL,
82 NULL);
83 if (bSuccess == FALSE)
84 dwError = GetLastError();
85
86 CloseHandle(hToken);
87
88 return dwError;
89 }
90
91 void
92 LoadFilter(_In_ LPWSTR FilterName)
93 {
94 DWORD dwError;
95 dwError = SetDriverLoadPrivilege();
96 if (dwError != ERROR_SUCCESS)
97 {
98 LoadAndPrintString(IDS_ERROR_PRIV, HRESULT_FROM_WIN32(dwError));
99 return;
100 }
101
102 HRESULT hr = FilterLoad(FilterName);
103 if (hr != S_OK)
104 {
105 LoadAndPrintString(IDS_ERROR_LOAD, hr);
106 PrintErrorText(hr);
107 }
108 }
109
110 void
111 UnloadFilter(_In_ LPWSTR FilterName)
112 {
113 DWORD dwError;
114 dwError = SetDriverLoadPrivilege();
115 if (dwError != ERROR_SUCCESS)
116 {
117 LoadAndPrintString(IDS_ERROR_PRIV, HRESULT_FROM_WIN32(dwError));
118 return;
119 }
120
121 HRESULT hr = FilterUnload(FilterName);
122 if (hr != S_OK)
123 {
124 LoadAndPrintString(IDS_ERROR_UNLOAD, hr);
125 PrintErrorText(hr);
126 }
127 }
128
129 void
130 PrintFilterInfo(_In_ PVOID Buffer,
131 _In_ BOOL IsNewStyle)
132 {
133 WCHAR FilterName[128] = { 0 };
134 WCHAR Altitude[64] = { 0 };
135
136 if (IsNewStyle)
137 {
138 PFILTER_AGGREGATE_STANDARD_INFORMATION FilterAggInfo;
139 FilterAggInfo = (PFILTER_AGGREGATE_STANDARD_INFORMATION)Buffer;
140
141 if (FilterAggInfo->Type.MiniFilter.FilterNameLength < 128)
142 {
143 CopyMemory(FilterName,
144 (PCHAR)FilterAggInfo + FilterAggInfo->Type.MiniFilter.FilterNameBufferOffset,
145 FilterAggInfo->Type.MiniFilter.FilterNameLength);
146 FilterName[FilterAggInfo->Type.MiniFilter.FilterNameLength] = UNICODE_NULL;
147 }
148
149 if (FilterAggInfo->Type.MiniFilter.FilterNameLength < 64)
150 {
151 CopyMemory(Altitude,
152 (PCHAR)FilterAggInfo + FilterAggInfo->Type.MiniFilter.FilterAltitudeBufferOffset,
153 FilterAggInfo->Type.MiniFilter.FilterAltitudeLength);
154 FilterName[FilterAggInfo->Type.MiniFilter.FilterAltitudeLength] = UNICODE_NULL;
155 }
156
157 wprintf(L"%-38s %-10lu %-10s %-10lu\n",
158 FilterName,
159 FilterAggInfo->Type.MiniFilter.NumberOfInstances,
160 Altitude,
161 FilterAggInfo->Type.MiniFilter.FrameID);
162 }
163 else
164 {
165 PFILTER_FULL_INFORMATION FilterInfo;
166 FilterInfo = (PFILTER_FULL_INFORMATION)Buffer;
167
168 if (FilterInfo->FilterNameLength < 128)
169 {
170 CopyMemory(FilterName,
171 FilterInfo->FilterNameBuffer,
172 FilterInfo->FilterNameLength);
173 FilterName[FilterInfo->FilterNameLength] = UNICODE_NULL;
174 }
175
176 wprintf(L"%-38s %-10lu %-10lu\n",
177 FilterName,
178 FilterInfo->NumberOfInstances,
179 FilterInfo->FrameID);
180 }
181 }
182
183 void
184 ListFilters()
185 {
186 HANDLE FindHandle;
187 BYTE Buffer[1024];
188 ULONG BytesReturned;
189 BOOL IsNewStyle = TRUE;
190 HRESULT hr;
191
192 hr = FilterFindFirst(FilterAggregateStandardInformation,
193 Buffer,
194 1024,
195 &BytesReturned,
196 &FindHandle);
197 if (!SUCCEEDED(hr))
198 {
199 IsNewStyle = FALSE;
200 hr = FilterFindFirst(FilterFullInformation,
201 Buffer,
202 1024,
203 &BytesReturned,
204 &FindHandle);
205 }
206
207 if (SUCCEEDED(hr))
208 {
209 if (IsNewStyle)
210 {
211 LoadAndPrintString(IDS_DISPLAY_FILTERS1);
212 wprintf(L"------------------------------ ------------- ------------ -----\n");
213 }
214 else
215 {
216 LoadAndPrintString(IDS_DISPLAY_FILTERS2);
217 wprintf(L"------------------------------ ------------- -----\n");
218 }
219
220 PrintFilterInfo(Buffer, IsNewStyle);
221
222 do
223 {
224 hr = FilterFindNext(FindHandle,
225 IsNewStyle ? FilterAggregateStandardInformation : FilterFullInformation,
226 Buffer,
227 1024,
228 &BytesReturned);
229 if (SUCCEEDED(hr))
230 {
231 PrintFilterInfo(Buffer, IsNewStyle);
232 }
233
234 } while (SUCCEEDED(hr));
235
236 FilterFindClose(FindHandle);
237 }
238
239 if (!SUCCEEDED(hr) && hr != HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS))
240 {
241 LoadAndPrintString(IDS_ERROR_PRIV, hr);
242 PrintErrorText(hr);
243 }
244 }
245
246 int wmain(int argc, WCHAR *argv[])
247 {
248 if (argc < 2)
249 {
250 LoadAndPrintString(IDS_USAGE);
251 return 0;
252 }
253
254 wprintf(L"\n");
255
256 if (!_wcsicmp(argv[1], L"help"))
257 {
258 LoadAndPrintString(IDS_USAGE);
259 }
260 else if (!_wcsicmp(argv[1], L"load"))
261 {
262 if (argc == 3)
263 {
264 LoadFilter(argv[2]);
265 }
266 else
267 {
268 LoadAndPrintString(IDS_USAGE_LOAD);
269 wprintf(L"fltmc.exe load [name]\n\n");
270 }
271 }
272 else if (!_wcsicmp(argv[1], L"unload"))
273 {
274 if (argc == 3)
275 {
276 UnloadFilter(argv[2]);
277 }
278 else
279 {
280 LoadAndPrintString(IDS_USAGE_UNLOAD);
281 wprintf(L"fltmc.exe unload [name]\n\n");
282 }
283 }
284 else if (!_wcsicmp(argv[1], L"filters"))
285 {
286 if (argc == 2)
287 {
288 ListFilters();
289 }
290 else
291 {
292 LoadAndPrintString(IDS_USAGE_FILTERS);
293 wprintf(L"fltmc.exe filters\n\n");
294 }
295 }
296
297 return 0;
298 }