2 * Copyright (c) 2008, 2009, 2010, 2011 Kernel Drivers, LLC.
3 * Copyright (c) 2009, 2010, 2011 Your File System, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * - Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
14 * this list of conditions and the following disclaimer in the
16 * and/or other materials provided with the distribution.
17 * - Neither the names of Kernel Drivers, LLC and Your File System, Inc.
18 * nor the names of their contributors may be used to endorse or promote
19 * products derived from this software without specific prior written
20 * permission from Kernel Drivers, LLC and Your File System, Inc.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
26 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include "AFSCommon.h"
44 extern void *KeServiceDescriptorTable;
51 // This is the initial entry point for the driver.
54 // DriverObject Pointer to Driver Object created by I/O manager
55 // RegistryPath Pointer to registry path representing this Driver
58 // Success To indicate Driver's inituaialization processing
60 // NT ERROR STATUS Otherwise -- Driver does not remain loaded
64 DriverEntry( PDRIVER_OBJECT DriverObject,
65 PUNICODE_STRING RegistryPath)
68 NTSTATUS ntStatus = STATUS_SUCCESS;
69 AFSDeviceExt *pDeviceExt;
70 ULONG ulTimeIncrement = 0;
71 UNICODE_STRING uniSymLinkName;
72 UNICODE_STRING uniDeviceName;
75 UNICODE_STRING uniValueName;
76 BOOLEAN bExit = FALSE;
77 UNICODE_STRING uniRoutine;
78 RTL_OSVERSIONINFOW sysVersion;
83 DbgPrint("AFSRedirFs DriverEntry Initialization build %s:%s\n", __DATE__, __TIME__);
86 // Initialize some local variables for easier processing
89 uniSymLinkName.Buffer = NULL;
91 AFSDumpFileLocation.Length = 0;
92 AFSDumpFileLocation.MaximumLength = 0;
93 AFSDumpFileLocation.Buffer = NULL;
95 AFSDumpFileName.Length = 0;
96 AFSDumpFileName.Buffer = NULL;
97 AFSDumpFileName.MaximumLength = 0;
99 ExInitializeResourceLite( &AFSDbgLogLock);
102 // Initialize the server name
107 RtlZeroMemory( &sysVersion,
108 sizeof( RTL_OSVERSIONINFOW));
110 sysVersion.dwOSVersionInfoSize = sizeof( RTL_OSVERSIONINFOW);
112 RtlGetVersion( &sysVersion);
114 RtlInitUnicodeString( &uniRoutine,
115 L"ZwSetInformationToken");
117 AFSSetInformationToken = (PAFSSetInformationToken)MmGetSystemRoutineAddress( &uniRoutine);
119 if( AFSSetInformationToken == NULL)
122 AFSSrvcTableEntry *pServiceTable = NULL;
124 pServiceTable = (AFSSrvcTableEntry *)KeServiceDescriptorTable;
127 // Only perform this lookup for Windows XP.
130 if( pServiceTable != NULL &&
131 sysVersion.dwMajorVersion == 5 &&
132 sysVersion.dwMinorVersion == 1)
134 AFSSetInformationToken = (PAFSSetInformationToken)pServiceTable->ServiceTable[ 0xE6];
140 // And the global root share name
143 RtlInitUnicodeString( &AFSGlobalRootName,
144 AFS_GLOBAL_ROOT_SHARE_NAME);
146 RtlZeroMemory( &AFSNoPAGAuthGroup,
150 // Our backdoor to not let the driver load
155 try_return( ntStatus);
159 // Perform some initialization
162 AFSDriverObject = DriverObject;
164 ntStatus = AFSReadRegistry( RegistryPath);
166 if( !NT_SUCCESS( ntStatus))
169 DbgPrint("AFS DriverEntry: Failed to read registry Status %08lX\n", ntStatus);
171 ntStatus = STATUS_SUCCESS;
175 // Initialize the debug log and dump file interface
178 AFSInitializeDbgLog();
180 AFSInitializeDumpFile();
184 if( BooleanFlagOn( AFSDebugFlags, AFS_DBG_FLAG_BREAK_ON_ENTRY))
187 DbgPrint("AFSRedirFs DriverEntry - Break on entry\n");
196 try_return( ntStatus = STATUS_UNSUCCESSFUL);
201 if( BooleanFlagOn( AFSDebugFlags, AFS_DBG_REQUIRE_CLEAN_SHUTDOWN) &&
202 !BooleanFlagOn( AFSDebugFlags, AFS_DBG_CLEAN_SHUTDOWN))
205 AFSPrint("AFS DriverEntry: Failed to shutdown clean, exiting\n");
207 try_return( ntStatus = STATUS_UNSUCCESSFUL);
211 // Setup the registry string
214 AFSRegistryPath.MaximumLength = RegistryPath->MaximumLength;
215 AFSRegistryPath.Length = RegistryPath->Length;
217 AFSRegistryPath.Buffer = (PWSTR)ExAllocatePoolWithTag( PagedPool,
218 AFSRegistryPath.Length,
219 AFS_GENERIC_MEMORY_18_TAG);
221 if( AFSRegistryPath.Buffer == NULL)
224 DbgPrint("AFSRedirFs DriverEntry Failed to allocate registry path buffer\n");
226 try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
229 RtlCopyMemory( AFSRegistryPath.Buffer,
230 RegistryPath->Buffer,
231 RegistryPath->Length);
233 if( BooleanFlagOn( AFSDebugFlags, AFS_DBG_REQUIRE_CLEAN_SHUTDOWN))
237 // Update the shutdown flag
242 RtlInitUnicodeString( &uniValueName,
243 AFS_REG_SHUTDOWN_STATUS);
245 AFSUpdateRegistryParameter( &uniValueName,
251 RtlInitUnicodeString( &uniDeviceName,
252 AFS_CONTROL_DEVICE_NAME);
254 ntStatus = IoCreateDeviceSecure( DriverObject,
255 sizeof( AFSDeviceExt),
257 FILE_DEVICE_NETWORK_FILE_SYSTEM,
260 &SDDL_DEVOBJ_SYS_ALL_ADM_RWX_WORLD_RWX_RES_RWX,
261 (LPCGUID)&GUID_SD_AFS_REDIRECTOR_CONTROL_OBJECT,
264 if( !NT_SUCCESS( ntStatus))
267 DbgPrint("AFS DriverEntry - Failed to allocate device control object Status %08lX\n", ntStatus);
269 try_return( ntStatus);
273 // Setup the device extension
276 pDeviceExt = (AFSDeviceExt *)AFSDeviceObject->DeviceExtension;
278 InitializeListHead( &pDeviceExt->Specific.Control.DirNotifyList);
279 FsRtlNotifyInitializeSync( &pDeviceExt->Specific.Control.NotifySync);
282 // Now initialize the control device
285 ntStatus = AFSInitializeControlDevice();
287 if( !NT_SUCCESS( ntStatus))
290 try_return( ntStatus);
294 // Allocate our symbolic link for service communication
297 RtlInitUnicodeString( &uniSymLinkName,
300 ntStatus = IoCreateSymbolicLink( &uniSymLinkName,
303 if( !NT_SUCCESS( ntStatus))
306 DbgPrint("AFS DriverEntry - Failed to create symbolic link Status %08lX\n", ntStatus);
309 // OK, no one can communicate with us so fail
312 try_return( ntStatus);
316 // Fill in the dispatch table
319 for( ulIndex = 0; ulIndex <= IRP_MJ_MAXIMUM_FUNCTION; ulIndex++)
322 DriverObject->MajorFunction[ ulIndex] = AFSDefaultDispatch;
325 DriverObject->MajorFunction[IRP_MJ_CREATE] = AFSCreate;
326 DriverObject->MajorFunction[IRP_MJ_CLOSE] = AFSClose;
327 DriverObject->MajorFunction[IRP_MJ_READ] = AFSRead;
328 DriverObject->MajorFunction[IRP_MJ_WRITE] = AFSWrite;
329 DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] = AFSQueryFileInfo;
330 DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] = AFSSetFileInfo;
331 DriverObject->MajorFunction[IRP_MJ_QUERY_EA] = AFSQueryEA;
332 DriverObject->MajorFunction[IRP_MJ_SET_EA] = AFSSetEA;
333 DriverObject->MajorFunction[IRP_MJ_FLUSH_BUFFERS] = AFSFlushBuffers;
334 DriverObject->MajorFunction[IRP_MJ_QUERY_VOLUME_INFORMATION] = AFSQueryVolumeInfo;
335 DriverObject->MajorFunction[IRP_MJ_SET_VOLUME_INFORMATION] = AFSSetVolumeInfo;
336 DriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL] = AFSDirControl;
337 DriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] = AFSFSControl;
338 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = AFSDevControl;
339 DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = AFSInternalDevControl;
340 DriverObject->MajorFunction[IRP_MJ_SHUTDOWN] = AFSShutdown;
341 DriverObject->MajorFunction[IRP_MJ_LOCK_CONTROL] = AFSLockControl;
342 DriverObject->MajorFunction[IRP_MJ_CLEANUP] = AFSCleanup;
343 DriverObject->MajorFunction[IRP_MJ_QUERY_SECURITY] = AFSQuerySecurity;
344 DriverObject->MajorFunction[IRP_MJ_SET_SECURITY] = AFSSetSecurity;
345 DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = AFSSystemControl;
346 //DriverObject->MajorFunction[IRP_MJ_QUERY_QUOTA] = AFSQueryQuota;
347 //DriverObject->MajorFunction[IRP_MJ_SET_QUOTA] = AFSSetQuota;
350 // Since we are not a true FSD then we are not controlling a device and hence these will not be needed
355 DriverObject->MajorFunction[IRP_MJ_POWER] = AFSPower;
356 DriverObject->MajorFunction[IRP_MJ_PNP] = AFSPnP;
361 // Fast IO Dispatch table
364 DriverObject->FastIoDispatch = &AFSFastIoDispatch;
366 RtlZeroMemory( &AFSFastIoDispatch,
367 sizeof( AFSFastIoDispatch));
370 // Again, since we are not a registered FSD many of these are not going to be called. They are here
374 AFSFastIoDispatch.SizeOfFastIoDispatch = sizeof(FAST_IO_DISPATCH);
375 AFSFastIoDispatch.FastIoCheckIfPossible = AFSFastIoCheckIfPossible; // CheckForFastIo
376 AFSFastIoDispatch.FastIoRead = AFSFastIoRead; // Read
377 AFSFastIoDispatch.FastIoWrite = AFSFastIoWrite; // Write
378 AFSFastIoDispatch.FastIoQueryBasicInfo = AFSFastIoQueryBasicInfo; // QueryBasicInfo
379 AFSFastIoDispatch.FastIoQueryStandardInfo = AFSFastIoQueryStandardInfo; // QueryStandardInfo
380 AFSFastIoDispatch.FastIoLock = AFSFastIoLock; // Lock
381 AFSFastIoDispatch.FastIoUnlockSingle = AFSFastIoUnlockSingle; // UnlockSingle
382 AFSFastIoDispatch.FastIoUnlockAll = AFSFastIoUnlockAll; // UnlockAll
383 AFSFastIoDispatch.FastIoUnlockAllByKey = AFSFastIoUnlockAllByKey; // UnlockAllByKey
384 AFSFastIoDispatch.FastIoQueryNetworkOpenInfo = AFSFastIoQueryNetworkOpenInfo;
385 AFSFastIoDispatch.AcquireForCcFlush = AFSFastIoAcquireForCCFlush;
386 AFSFastIoDispatch.ReleaseForCcFlush = AFSFastIoReleaseForCCFlush;
387 AFSFastIoDispatch.FastIoDeviceControl = AFSFastIoDevCtrl;
388 AFSFastIoDispatch.AcquireFileForNtCreateSection = AFSFastIoAcquireFile;
389 AFSFastIoDispatch.ReleaseFileForNtCreateSection = AFSFastIoReleaseFile;
390 AFSFastIoDispatch.FastIoDetachDevice = AFSFastIoDetachDevice;
391 //AFSFastIoDispatch.AcquireForModWrite = AFSFastIoAcquireForModWrite;
392 //AFSFastIoDispatch.ReleaseForModWrite = AFSFastIoReleaseForModWrite;
393 AFSFastIoDispatch.MdlRead = AFSFastIoMdlRead;
394 AFSFastIoDispatch.MdlReadComplete = AFSFastIoMdlReadComplete;
395 AFSFastIoDispatch.PrepareMdlWrite = AFSFastIoPrepareMdlWrite;
396 AFSFastIoDispatch.MdlWriteComplete = AFSFastIoMdlWriteComplete;
397 AFSFastIoDispatch.FastIoReadCompressed = AFSFastIoReadCompressed;
398 AFSFastIoDispatch.FastIoWriteCompressed = AFSFastIoWriteCompressed;
399 AFSFastIoDispatch.MdlReadCompleteCompressed = AFSFastIoMdlReadCompleteCompressed;
400 AFSFastIoDispatch.MdlWriteCompleteCompressed = AFSFastIoMdlWriteCompleteCompressed;
401 AFSFastIoDispatch.FastIoQueryOpen = AFSFastIoQueryOpen;
404 // Cache manager callback routines.
407 AFSCacheManagerCallbacks.AcquireForLazyWrite = &AFSAcquireFcbForLazyWrite;
408 AFSCacheManagerCallbacks.ReleaseFromLazyWrite = &AFSReleaseFcbFromLazyWrite;
409 AFSCacheManagerCallbacks.AcquireForReadAhead = &AFSAcquireFcbForReadAhead;
410 AFSCacheManagerCallbacks.ReleaseFromReadAhead = &AFSReleaseFcbFromReadAhead;
416 AFSSysProcess = PsGetCurrentProcessId();
419 // Register for shutdown notification
422 IoRegisterShutdownNotification( AFSDeviceObject);
425 // Initialize the system process cb
428 AFSInitializeProcessCB( 0,
429 (ULONGLONG)AFSSysProcess);
432 // Initialize the redirector device
435 ntStatus = AFSInitRDRDevice();
437 if( !NT_SUCCESS( ntStatus))
440 DbgPrint("AFS DriverEntry Failed to initialize redirector device Status %08lX\n");
442 try_return( ntStatus);
446 // Initialize some server name based strings
449 AFSInitServerStrings();
452 // Register the call back for process creation and tear down
455 PsSetCreateProcessNotifyRoutine( AFSProcessNotify,
460 if( !NT_SUCCESS( ntStatus))
463 DbgPrint("AFSRedirFs DriverEntry failed to initialize %08lX\n", ntStatus);
465 if( AFSRegistryPath.Buffer != NULL)
468 ExFreePool( AFSRegistryPath.Buffer);
471 if( uniSymLinkName.Buffer != NULL)
474 IoDeleteSymbolicLink( &uniSymLinkName);
477 if( AFSDeviceObject != NULL)
480 AFSRemoveControlDevice();
482 FsRtlNotifyUninitializeSync( &pDeviceExt->Specific.Control.NotifySync);
484 IoUnregisterShutdownNotification( AFSDeviceObject);
486 IoDeleteDevice( AFSDeviceObject);
491 ExDeleteResourceLite( &AFSDbgLogLock);
494 __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()) )
499 "EXCEPTION - AFSRedirFs DriverEntry\n");