Windows: Store OS version as a global
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSInit.cpp
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Kernel Drivers, LLC.
3  * Copyright (c) 2009, 2010, 2011 Your File System, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
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
13  *   notice,
14  *   this list of conditions and the following disclaimer in the
15  *   documentation
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.
21  *
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.
33  */
34
35 //
36 // File: AFSInit.cpp
37 //
38
39 #include "AFSCommon.h"
40
41 //
42 // DriverEntry
43 //
44 // This is the initial entry point for the driver.
45 //
46 // Inputs:
47 //  DriverObject        Pointer to Driver Object created by I/O manager
48 //  RegistryPath        Pointer to registry path representing this Driver
49 //
50 // Returns:
51 //  Success             To indicate Driver's inituaialization processing
52 //                      was successful
53 //  NT ERROR STATUS     Otherwise -- Driver does not remain loaded
54 //
55
56 NTSTATUS
57 DriverEntry( PDRIVER_OBJECT DriverObject,
58              PUNICODE_STRING RegistryPath)
59 {
60
61     NTSTATUS ntStatus = STATUS_SUCCESS;
62     AFSDeviceExt    *pDeviceExt;
63     UNICODE_STRING uniDeviceName;
64     ULONG ulIndex = 0;
65     UNICODE_STRING uniRoutine;
66
67     BOOLEAN bExit = FALSE;
68
69     __try
70     {
71
72         AFSPrint("AFSLibrary DriverEntry Initialization build %s:%s\n", __DATE__, __TIME__);
73
74         //
75         // Our backdoor to not let the driver load
76         //
77
78         if( bExit)
79         {
80
81             //
82             // Return a failure so we can update the binary and manually start it without
83             // having to do a reboot
84             //
85
86             try_return( ntStatus = STATUS_UNSUCCESSFUL);
87         }
88
89         //
90         // Perform some initialization
91         //
92
93         AFSLibraryDriverObject = DriverObject;
94
95         //
96         // Setup the registry string
97         //
98
99         AFSRegistryPath.MaximumLength = RegistryPath->MaximumLength;
100         AFSRegistryPath.Length        = RegistryPath->Length;
101
102         AFSRegistryPath.Buffer = (PWSTR)AFSLibExAllocatePoolWithTag( PagedPool,
103                                                                      AFSRegistryPath.Length,
104                                                                      AFS_GENERIC_MEMORY_13_TAG);
105
106         if( AFSRegistryPath.Buffer == NULL)
107         {
108
109             AFSPrint("AFS DriverEntry Failed to allocate registry path buffer\n");
110
111             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES);
112         }
113
114         RtlCopyMemory( AFSRegistryPath.Buffer,
115                        RegistryPath->Buffer,
116                        RegistryPath->Length);
117
118         RtlZeroMemory( &AFSRtlSysVersion,
119                        sizeof( RTL_OSVERSIONINFOW));
120
121         AFSRtlSysVersion.dwOSVersionInfoSize = sizeof( RTL_OSVERSIONINFOW);
122
123         RtlGetVersion( &AFSRtlSysVersion);
124
125 #if 0
126         //
127         // By not fetching the RtlSetSaclSecurityDescriptor function
128         // pointer it disables the additional of a mandatory label
129         // to the default acl which is returned by AFSRedir for all
130         // security information queries.   The addition of the
131         // mandatory label appears to have a negative consequence
132         // for roaming profiles and redirected folders.  All links
133         // become untrusted and IE9 is unable to open a new instance
134         // to a non-default home page.
135         //
136         //
137         // Only retrieve this function for Vista and above since
138         // Mandatory Labels only exist on those operating systems.
139         //
140
141         if( AFSRtlSysVersion.dwMajorVersion >= 6)
142         {
143             RtlInitUnicodeString( &uniRoutine,
144                                   L"RtlSetSaclSecurityDescriptor");
145
146             AFSRtlSetSaclSecurityDescriptor = (PAFSRtlSetSaclSecurityDescriptor)MmGetSystemRoutineAddress( &uniRoutine);
147         }
148
149 #endif
150
151         RtlInitUnicodeString( &uniRoutine,
152                               L"RtlSetGroupSecurityDescriptor");
153
154         AFSRtlSetGroupSecurityDescriptor = (PAFSRtlSetGroupSecurityDescriptor)MmGetSystemRoutineAddress( &uniRoutine);
155
156         ntStatus = AFSCreateDefaultSecurityDescriptor();
157
158         if( !NT_SUCCESS( ntStatus))
159         {
160
161             AFSPrint("AFS DriverEntry  AFSCreateDefaultSecurityDescriptor failed Status %08lX\n", ntStatus);
162
163             ntStatus = STATUS_SUCCESS;
164         }
165
166         //
167         // Initialize the control device
168         //
169
170         RtlInitUnicodeString( &uniDeviceName,
171                               AFS_LIBRARY_CONTROL_DEVICE_NAME);
172
173         ntStatus = IoCreateDevice( DriverObject,
174                                    sizeof( AFSDeviceExt),
175                                    &uniDeviceName,
176                                    FILE_DEVICE_NETWORK_FILE_SYSTEM,
177                                    0,
178                                    FALSE,
179                                    &AFSLibraryDeviceObject);
180
181         if( !NT_SUCCESS( ntStatus))
182         {
183
184             AFSPrint("AFS DriverEntry - Failed to allocate device control object Status %08lX\n", ntStatus);
185
186             try_return( ntStatus);
187         }
188
189         //
190         // Setup the device extension
191         //
192
193         pDeviceExt = (AFSDeviceExt *)AFSLibraryDeviceObject->DeviceExtension;
194
195         //
196         // Now initialize the control device
197         //
198
199         ntStatus = AFSInitializeLibraryDevice();
200
201         if( !NT_SUCCESS( ntStatus))
202         {
203
204             try_return( ntStatus);
205         }
206
207         //
208         // Initialize the worker thread pool counts.
209         //
210
211         pDeviceExt->Specific.Library.WorkerCount = 0;
212
213         pDeviceExt->Specific.Library.IOWorkerCount = 0;
214
215         //
216         // Fill in the dispatch table
217         //
218
219         for( ulIndex = 0; ulIndex <= IRP_MJ_MAXIMUM_FUNCTION; ulIndex++)
220         {
221
222             DriverObject->MajorFunction[ ulIndex] = AFSDefaultDispatch;
223         }
224
225         DriverObject->MajorFunction[IRP_MJ_CREATE] =                    AFSCreate;
226         DriverObject->MajorFunction[IRP_MJ_CLOSE] =                     AFSClose;
227         DriverObject->MajorFunction[IRP_MJ_READ] =                      AFSRead;
228         DriverObject->MajorFunction[IRP_MJ_WRITE] =                     AFSWrite;
229         DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] =         AFSQueryFileInfo;
230         DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] =           AFSSetFileInfo;
231         DriverObject->MajorFunction[IRP_MJ_QUERY_EA] =                  AFSQueryEA;
232         DriverObject->MajorFunction[IRP_MJ_SET_EA] =                    AFSSetEA;
233         DriverObject->MajorFunction[IRP_MJ_FLUSH_BUFFERS] =             AFSFlushBuffers;
234         DriverObject->MajorFunction[IRP_MJ_QUERY_VOLUME_INFORMATION] =  AFSQueryVolumeInfo;
235         DriverObject->MajorFunction[IRP_MJ_SET_VOLUME_INFORMATION] =    AFSSetVolumeInfo;
236         DriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL] =         AFSDirControl;
237         DriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] =       AFSFSControl;
238         DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] =            AFSDevControl;
239         DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] =   AFSInternalDevControl;
240         DriverObject->MajorFunction[IRP_MJ_SHUTDOWN] =                  AFSShutdown;
241         DriverObject->MajorFunction[IRP_MJ_LOCK_CONTROL] =              AFSLockControl;
242         DriverObject->MajorFunction[IRP_MJ_CLEANUP] =                   AFSCleanup;
243         DriverObject->MajorFunction[IRP_MJ_QUERY_SECURITY] =            AFSQuerySecurity;
244         DriverObject->MajorFunction[IRP_MJ_SET_SECURITY] =              AFSSetSecurity;
245         DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] =            AFSSystemControl;
246         //DriverObject->MajorFunction[IRP_MJ_QUERY_QUOTA] =               AFSQueryQuota;
247         //DriverObject->MajorFunction[IRP_MJ_SET_QUOTA] =                 AFSSetQuota;
248
249         DriverObject->DriverUnload = AFSUnload;
250
251         AFSSysProcess = PsGetCurrentProcessId();
252
253 try_exit:
254
255         if( !NT_SUCCESS( ntStatus))
256         {
257
258             AFSPrint("AFSLibrary DriverEntry failed to initialize %08lX\n", ntStatus);
259
260             if( AFSRegistryPath.Buffer != NULL)
261             {
262
263                 AFSLibExFreePoolWithTag( AFSRegistryPath.Buffer,
264                                          AFS_GENERIC_MEMORY_13_TAG);
265             }
266
267             if( AFSLibraryDeviceObject != NULL)
268             {
269
270                 AFSRemoveLibraryDevice();
271
272                 IoDeleteDevice( AFSLibraryDeviceObject);
273             }
274         }
275     }
276     __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
277     {
278
279         AFSPrint( "EXCEPTION - AFS DriverEntry\n");
280
281         AFSDumpTraceFilesFnc();
282     }
283
284     return ntStatus;
285 }
286
287 void
288 AFSUnload( IN PDRIVER_OBJECT DriverObject)
289 {
290
291     UNREFERENCED_PARAMETER(DriverObject);
292     if( AFSGlobalRoot != NULL)
293     {
294
295         AFSInvalidateVolume( AFSGlobalRoot,
296                              AFS_INVALIDATE_CALLBACK);
297
298         ClearFlag( AFSGlobalRoot->Flags, AFS_VOLUME_ACTIVE_GLOBAL_ROOT);
299
300         AFSShutdownVolumeWorker( AFSGlobalRoot);
301     }
302
303     if( AFSLibraryDeviceObject != NULL)
304     {
305
306         AFSRemoveWorkerPool();
307     }
308
309     if( AFSRegistryPath.Buffer != NULL)
310     {
311
312         AFSLibExFreePoolWithTag( AFSRegistryPath.Buffer,
313                                  AFS_GENERIC_MEMORY_13_TAG);
314     }
315
316     AFSCloseLibrary();
317
318     if( AFSDefaultSD != NULL)
319     {
320
321         AFSLibExFreePoolWithTag( AFSDefaultSD,
322                                  AFS_GENERIC_MEMORY_27_TAG);
323     }
324
325     if( AFSLibraryDeviceObject != NULL)
326     {
327
328         AFSRemoveLibraryDevice();
329
330         IoDeleteDevice( AFSLibraryDeviceObject);
331     }
332
333     return;
334 }