Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

385 lines
9.1 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. install.c
  5. Abstract:
  6. This is the main module to install the filespy filter driver. This was
  7. adapted from the sfilter installation program.
  8. // @@BEGIN_DDKSPLIT
  9. Author:
  10. Molly Brown (MollyBro)
  11. // @@END_DDKSPLIT
  12. Environment:
  13. User Mode Only
  14. // @@BEGIN_DDKSPLIT
  15. Revision History:
  16. Neal Christiansen (nealch) 28-Jun-2000
  17. Updated to not fail if it can't copy the driver. Updated to handle
  18. the service already being present. Updated to add the DebugDisplay
  19. registry value.
  20. // @@END_DDKSPLIT
  21. --*/
  22. #include <windows.h>
  23. #include <stdio.h>
  24. #include <conio.h>
  25. #include "filespy.h"
  26. #include "fspyServ.h"
  27. //
  28. // Enable these warnings in the code.
  29. //
  30. #pragma warning(error:4101) // Unreferenced local variable
  31. /////////////////////////////////////////////////////////////////////////////
  32. //
  33. // Globals
  34. //
  35. /////////////////////////////////////////////////////////////////////////////
  36. VOID
  37. DisplayError(
  38. DWORD Code
  39. );
  40. /////////////////////////////////////////////////////////////////////////////
  41. //
  42. // Functions
  43. //
  44. /////////////////////////////////////////////////////////////////////////////
  45. void __cdecl
  46. main (
  47. int argc,
  48. char *argv[]
  49. )
  50. /*++
  51. Routine Description:
  52. This is the program entry point and main processing routine for the
  53. installation console mode application. It will be responsible for
  54. installing the file system filter driver into the registry and preparing
  55. the system for a reboot.
  56. Arguments:
  57. argc - The count of arguments passed into the command line.
  58. argv - Array of arguments passed into the command line.
  59. Return Value:
  60. None.
  61. --*/
  62. {
  63. SC_HANDLE scmHandle = NULL;
  64. SC_HANDLE filespyService = NULL;
  65. DWORD errorCode = 0;
  66. DWORD tagID;
  67. BOOL status;
  68. HKEY key = NULL;
  69. LONG openStatus;
  70. ULONG initialValue;
  71. WCHAR driverDirectory[MAX_PATH];
  72. WCHAR driverFullPath[MAX_PATH];
  73. UNREFERENCED_PARAMETER( argc );
  74. UNREFERENCED_PARAMETER( argv );
  75. //
  76. // Begin by displaying an introduction message to the user to let them
  77. // know that the application has started.
  78. //
  79. printf( "\nFilespy.sys Simple Installation Aid\n"
  80. "Copyright (c) 1999 Microsoft Corporation\n\n\n" );
  81. //
  82. // Get the directory where we are going to put the driver
  83. // Get the full path to the driver
  84. //
  85. GetSystemDirectory( driverDirectory, sizeof(driverDirectory) );
  86. wcscat( driverDirectory, L"\\drivers" );
  87. swprintf( driverFullPath, L"%s\\filespy.sys", driverDirectory );
  88. //
  89. // Obtain a handle to the service control manager requesting all access
  90. //
  91. scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  92. //
  93. // Verify that a handle could be obtained.
  94. //
  95. if (!scmHandle) {
  96. //
  97. // A handle could not be obtained. Get the error code and display a
  98. // useful message to the user.
  99. //
  100. printf( "The Service Control Manager could not be opened.\n" );
  101. DisplayError( GetLastError() );
  102. return;
  103. }
  104. //
  105. // Install the service with the Service Control Manager.
  106. //
  107. filespyService = CreateService ( scmHandle,
  108. FILESPY_SERVICE_NAME,
  109. L"Sample File System Filter that displays File Operations",
  110. SERVICE_ALL_ACCESS,
  111. SERVICE_FILE_SYSTEM_DRIVER,
  112. SERVICE_DEMAND_START,
  113. SERVICE_ERROR_NORMAL,
  114. driverFullPath,
  115. L"FSFilter Activity Monitor",
  116. &tagID,
  117. NULL,
  118. NULL,
  119. NULL );
  120. //
  121. // Check to see if the driver could actually be installed.
  122. //
  123. if (!filespyService) {
  124. //
  125. // The driver could not be installed. Display a useful error message
  126. // and exit.
  127. //
  128. errorCode = GetLastError();
  129. //
  130. // If the service already existed, just go on and copy the driver
  131. //
  132. if (ERROR_SERVICE_EXISTS == errorCode) {
  133. printf( "The FILESPY service already exists.\n" );
  134. goto CopyTheFile;
  135. }
  136. printf( "The Filespy service could not be created.\n" );
  137. DisplayError( errorCode );
  138. goto Cleanup;
  139. }
  140. //
  141. // Get a handle to the key for the driver so that it can be altered in the
  142. // next step.
  143. //
  144. openStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  145. L"SYSTEM\\CurrentControlSet\\Services\\filespy",
  146. 0,
  147. KEY_ALL_ACCESS,
  148. &key);
  149. //
  150. // Check the return to make sure that the application could get a
  151. // handle to the key.
  152. //
  153. if (openStatus != ERROR_SUCCESS) {
  154. //
  155. // A problem has occurred. Delete the service so that it is not
  156. // installed, then display error message and exit.
  157. //
  158. DeleteService( filespyService );
  159. printf( "Registry key could not be opened for driver.\n" );
  160. DisplayError( openStatus );
  161. goto Cleanup;
  162. }
  163. //
  164. // Delete the ImagePath value in the newly created key so that the
  165. // system looks for the driver in the normal location.
  166. //
  167. openStatus = RegDeleteValue (key, L"ImagePath");
  168. //
  169. // report an error and go on if we can't delete the key
  170. //
  171. if (openStatus != ERROR_SUCCESS) {
  172. printf("Could not delete ImagePath key.\n") ;
  173. DisplayError (openStatus) ;
  174. }
  175. //
  176. // Add the MaxRecords and MaxNames parameters to the registry
  177. //
  178. initialValue = 500;
  179. openStatus = RegSetValueEx( key,
  180. L"MaxRecords",
  181. 0,
  182. REG_DWORD,
  183. (PUCHAR)&initialValue,
  184. sizeof(initialValue));
  185. openStatus = RegSetValueEx( key,
  186. L"MaxNames",
  187. 0,
  188. REG_DWORD,
  189. (PUCHAR)&initialValue,
  190. sizeof(initialValue) );
  191. //
  192. // Display a message indicating that the driver has successfully been
  193. // installed and the system will be shutting down.
  194. //
  195. printf("The FILESPY service was successfully created.\n");
  196. CopyTheFile:
  197. //
  198. // Copy the file to the appropriate directory on the target drive.
  199. //
  200. status = CopyFile( L"filespy.sys", driverFullPath, FALSE );
  201. if (!status) {
  202. printf("\nCopying of \"filespy.sys\" to \"%S\" failed.\n",
  203. driverDirectory );
  204. DisplayError( GetLastError() );
  205. } else {
  206. printf( "\"filespy.sys\" was successfully copied to \"%S\".\n",
  207. driverDirectory );
  208. }
  209. Cleanup:
  210. //
  211. // Close the key handle if it is set since it is no longer needed.
  212. //
  213. if (key) {
  214. RegCloseKey( key );
  215. }
  216. //
  217. // The driver has now been installed or there was an error. Close the
  218. // service handle and scmHandle handle if they were set
  219. // since we don't need them any longer.
  220. //
  221. if(filespyService){
  222. CloseServiceHandle(filespyService);
  223. }
  224. if(scmHandle) {
  225. CloseServiceHandle(scmHandle);
  226. }
  227. //
  228. // Display a message indicating that the driver has successfully been
  229. // installed and the system will be shutting down.
  230. //
  231. if (!errorCode) {
  232. printf( "\nDriver successfully installed.\n\n"
  233. "The driver can be started immediately with the following command line:\n"
  234. " sc start filespy\n"
  235. "or by rebooting the system.\n" );
  236. }
  237. }
  238. VOID
  239. DisplayError (
  240. DWORD Code
  241. )
  242. /*++
  243. Routine Description:
  244. This routine will display an error message based off of the Win32 error
  245. code that is passed in. This allows the user to see an understandable
  246. error message instead of just the code.
  247. Arguments:
  248. Code - The error code to be translated.
  249. Return Value:
  250. None.
  251. --*/
  252. {
  253. WCHAR buffer[80];
  254. DWORD count;
  255. //
  256. // Translate the Win32 error code into a useful message.
  257. //
  258. count = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM,
  259. NULL,
  260. Code,
  261. 0,
  262. buffer,
  263. sizeof(buffer),
  264. NULL);
  265. //
  266. // Make sure that the message could be translated.
  267. //
  268. if (count == 0) {
  269. printf( "(%d) Error could not be translated.\n", Code );
  270. return;
  271. }
  272. else {
  273. //
  274. // Display the translated error.
  275. //
  276. printf( "(%d) %S\n", Code, buffer );
  277. return;
  278. }
  279. }