Leaked source code of windows server 2003
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.

419 lines
9.8 KiB

  1. /*++
  2. Copyright (c) 2002 Microsoft Corporation
  3. Module Name :
  4. disblwww.cxx
  5. Abstract:
  6. Determine if IIS should be disabled on upgrade.
  7. Author:
  8. Christopher Achille (cachille)
  9. Project:
  10. IIS Compatability Dll
  11. Revision History:
  12. May 2002: Created
  13. --*/
  14. #include <tchar.h>
  15. #include <nt.h>
  16. #include <ntrtl.h>
  17. #include <nturtl.h>
  18. #include <windows.h>
  19. #include "disblwww.hxx"
  20. // ShouldW3SVCBeDisabledOnUpgrade
  21. //
  22. // Should we disable W3SVC on Upgrade?
  23. //
  24. // Parameters
  25. // pbDisable - [out] Should the service be disabled or not
  26. //
  27. // Return Values:
  28. // TRUE - Success checking
  29. // FALSE - Failure checking
  30. //
  31. BOOL ShouldW3SVCBeDisabledOnUpgrade( LPBOOL pbDisable )
  32. {
  33. BOOL bHasLockDownBeenRun;
  34. BOOL bIsW3SVCAlreadyDisabled;
  35. BOOL bIsRegistryBlockSet;
  36. BOOL bIsWin2kUpgrade;
  37. BOOL bIsIISInstalled;
  38. if ( !IsIISInstalled( &bIsIISInstalled ) ||
  39. !IsWin2kUpgrade( &bIsWin2kUpgrade ) )
  40. {
  41. // Failed to query appropriate information
  42. return FALSE;
  43. }
  44. if ( !bIsWin2kUpgrade ||
  45. !bIsIISInstalled )
  46. {
  47. // Don't disable, since we are only suppose to do this on Win2k
  48. // upgrades with IIS
  49. *pbDisable = FALSE;
  50. return TRUE;
  51. }
  52. if ( !HasLockdownBeenRun( &bHasLockDownBeenRun ) ||
  53. !IsW3SVCDisabled( &bIsW3SVCAlreadyDisabled ) ||
  54. !HasRegistryBlockEntryBeenSet( &bIsRegistryBlockSet ) )
  55. {
  56. // Failed to query, so lets fail
  57. return FALSE;
  58. }
  59. if ( bHasLockDownBeenRun ||
  60. bIsW3SVCAlreadyDisabled ||
  61. bIsRegistryBlockSet )
  62. {
  63. // One of these conditions has been met, so we don't have to disable
  64. *pbDisable = FALSE;
  65. }
  66. else
  67. {
  68. // Disable, since none of the conditions were met
  69. *pbDisable = TRUE;
  70. }
  71. return TRUE;
  72. }
  73. // HasLockdownBeenRun
  74. //
  75. // Has the lockdown tool been run?
  76. //
  77. // Parameters
  78. // pbBeenRun [out] - TRUE == It has been run
  79. // FALSE == It has not been run
  80. //
  81. // Return
  82. // TRUE - Success checking
  83. // FALSE - Failure checking
  84. //
  85. BOOL
  86. HasLockdownBeenRun( LPBOOL pbBeenRun )
  87. {
  88. HKEY hRegKey;
  89. HKEY hLockdownKey;
  90. // Initialize to FALSE
  91. *pbBeenRun = FALSE;
  92. if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  93. LOCKDOWN_REGISTRY_LOCATION,
  94. 0,
  95. KEY_READ,
  96. &hRegKey ) != ERROR_SUCCESS )
  97. {
  98. // Failed to open key, lets fail
  99. return FALSE;
  100. }
  101. if ( RegOpenKeyEx( hRegKey,
  102. LOCKDOWN_REGISTRY_KEY,
  103. 0,
  104. KEY_READ,
  105. &hLockdownKey ) == ERROR_SUCCESS )
  106. {
  107. // We found the key, so it must have been run
  108. RegCloseKey( hLockdownKey );
  109. *pbBeenRun = TRUE;
  110. }
  111. RegCloseKey( hRegKey );
  112. return TRUE;
  113. }
  114. // IsW3SVCDisabled
  115. //
  116. // Check is W3SVC is already disabled
  117. //
  118. // pbDisabled
  119. BOOL
  120. IsW3SVCDisabled( LPBOOL pbDisabled )
  121. {
  122. DWORD dwW3SVCStartupType;
  123. DWORD dwIISAdminStartupType;
  124. if ( !QueryServiceStartType( W3SVC_SERVICENAME, &dwW3SVCStartupType ) ||
  125. !QueryServiceStartType( IISADMIN_SERVICENAME, &dwIISAdminStartupType ) )
  126. {
  127. // Failure quering services
  128. return FALSE;
  129. }
  130. *pbDisabled = ( dwW3SVCStartupType == SERVICE_DISABLED ) ||
  131. ( dwIISAdminStartupType == SERVICE_DISABLED );
  132. return TRUE;
  133. }
  134. // HasRegistryBlockEntryBeenSet
  135. //
  136. // Has Someone set a flag in the registry telling us not
  137. // to disable ourselves
  138. //
  139. BOOL
  140. HasRegistryBlockEntryBeenSet( LPBOOL pbIsSet )
  141. {
  142. HKEY hRegKey;
  143. HKEY hBlockKey;
  144. // Initialize to FALSE
  145. *pbIsSet = FALSE;
  146. if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  147. SERVICE_DISABLE_BLOCK_LOCATION,
  148. 0,
  149. KEY_READ,
  150. &hRegKey ) != ERROR_SUCCESS )
  151. {
  152. // Failed to open key, lets fail
  153. return FALSE;
  154. }
  155. if ( RegOpenKeyEx( hRegKey,
  156. SERVICE_DISABLE_BLOCK_KEY,
  157. 0,
  158. KEY_READ,
  159. &hBlockKey ) == ERROR_SUCCESS )
  160. {
  161. DWORD dwIndex;
  162. DWORD dwErr = ERROR_SUCCESS;
  163. TCHAR szValueName[ MAX_PATH ];
  164. DWORD dwValueNameLength;
  165. DWORD dwValue;
  166. DWORD dwValueLength;
  167. DWORD dwType;
  168. // Now lets check and see if anything is set here
  169. for ( dwIndex = 0;
  170. ( *pbIsSet == FALSE ) &&
  171. ( dwErr == ERROR_SUCCESS ) &&
  172. ( dwIndex < MAX_PATH ); // This is just incase we get caught in a loop
  173. dwIndex++)
  174. {
  175. dwValueNameLength = sizeof(szValueName)/sizeof(szValueName[0]);
  176. dwValueLength = sizeof( dwValue );
  177. dwErr = RegEnumValue( hBlockKey, // Key to enum
  178. dwIndex, // First entry
  179. szValueName, // Name of Value
  180. &dwValueNameLength, // Length of Name buffer
  181. NULL, // Reserved
  182. &dwType, // Reg Type
  183. (LPBYTE) &dwValue, // Value in registry
  184. &dwValueLength ); // Size of value
  185. if ( ( dwErr == ERROR_SUCCESS ) &&
  186. ( dwType == REG_DWORD ) )
  187. {
  188. *pbIsSet = TRUE;
  189. }
  190. if ( dwErr == ERROR_INSUFFICIENT_BUFFER )
  191. {
  192. // If the buffer is too small, then skip this one
  193. dwErr = ERROR_SUCCESS;
  194. }
  195. }
  196. RegCloseKey( hBlockKey );
  197. }
  198. RegCloseKey( hRegKey );
  199. return TRUE;
  200. }
  201. // IsIISInstalled
  202. //
  203. // Is IIS installed on this machine?
  204. //
  205. BOOL IsIISInstalled( LPBOOL pbIsIISInstalled )
  206. {
  207. SC_HANDLE hSCM;
  208. SC_HANDLE hW3Service;
  209. BOOL bRet = TRUE;
  210. *pbIsIISInstalled = FALSE;
  211. hSCM = OpenSCManager( NULL, SERVICES_ACTIVE_DATABASE, GENERIC_READ );
  212. if ( hSCM == NULL )
  213. {
  214. // Failed to open SCM
  215. return FALSE;
  216. }
  217. hW3Service = OpenService( hSCM, W3SVC_SERVICENAME, SERVICE_QUERY_CONFIG );
  218. if ( hW3Service != NULL )
  219. {
  220. // W3SVC service is installed
  221. *pbIsIISInstalled = TRUE;
  222. CloseServiceHandle( hW3Service );
  223. }
  224. else
  225. {
  226. if ( ( GetLastError() != ERROR_INVALID_NAME ) &&
  227. ( GetLastError() != ERROR_SERVICE_DOES_NOT_EXIST ) )
  228. {
  229. bRet = FALSE;
  230. }
  231. }
  232. CloseServiceHandle( hSCM );
  233. return bRet;
  234. }
  235. // IsWin2kUpgrate
  236. //
  237. // Make sure this is a Win2k Upgrade
  238. //
  239. BOOL
  240. IsWin2kUpgrade( LPBOOL pbIsWin2k )
  241. {
  242. OSVERSIONINFO osVerInfo;
  243. osVerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  244. if ( !GetVersionEx( &osVerInfo ) )
  245. {
  246. // Failed to check version
  247. return FALSE;
  248. }
  249. *pbIsWin2k = ( osVerInfo.dwMajorVersion == 5 ) &&
  250. ( osVerInfo.dwMinorVersion == 0 );
  251. return TRUE;
  252. }
  253. // QueryServiceStartType
  254. //
  255. // Query the start type for the particular service
  256. //
  257. // Parameters
  258. // szServiceName - [in] The name of the service to query
  259. // pdwStartType - [out] The Service Start Type
  260. // see QUERY_SERVICE_CONFIG.dwStartType
  261. //
  262. // Return:
  263. // TRUE - Successfully queried
  264. // FALSE - Could not be retrieved
  265. //
  266. BOOL
  267. QueryServiceStartType( LPTSTR szServiceName, LPDWORD pdwStartType )
  268. {
  269. SC_HANDLE hSCM;
  270. SC_HANDLE hW3Service;
  271. LPBYTE pBuffer;
  272. BOOL bRet = FALSE;
  273. DWORD dwErr;
  274. DWORD dwSizeNeeded;
  275. hSCM = OpenSCManager( NULL, SERVICES_ACTIVE_DATABASE, GENERIC_READ );
  276. if ( hSCM == NULL )
  277. {
  278. // Failed to open SCM
  279. return FALSE;
  280. }
  281. hW3Service = OpenService( hSCM, szServiceName, SERVICE_QUERY_CONFIG );
  282. if ( hW3Service != NULL )
  283. {
  284. if ( !QueryServiceConfig( hW3Service, NULL, 0, &dwSizeNeeded ) &&
  285. ( GetLastError() == ERROR_INSUFFICIENT_BUFFER ) )
  286. {
  287. pBuffer = new ( BYTE[ dwSizeNeeded ] );
  288. if ( pBuffer &&
  289. QueryServiceConfig( hW3Service, (LPQUERY_SERVICE_CONFIG) pBuffer,
  290. dwSizeNeeded, &dwSizeNeeded ) )
  291. {
  292. *pdwStartType = ( (LPQUERY_SERVICE_CONFIG) pBuffer )->dwStartType;
  293. bRet = TRUE;
  294. }
  295. if ( pBuffer )
  296. {
  297. // Free buffer
  298. delete pBuffer;
  299. }
  300. }
  301. CloseServiceHandle( hW3Service );
  302. }
  303. CloseServiceHandle( hSCM );
  304. return bRet;
  305. }
  306. // NotifyIISToDisableW3SVCOnUpgrade
  307. //
  308. // Notify IIS that W3SVC should be disabled when we upgrade
  309. //
  310. // Parameters:
  311. // bDisable - Disable/Don't Disable web service on upgrade
  312. BOOL
  313. NotifyIISToDisableW3SVCOnUpgrade( BOOL bDisable )
  314. {
  315. HKEY hKey;
  316. DWORD dwValue = bDisable;
  317. BOOL bRet = TRUE;
  318. DWORD dwRet;
  319. // Open Node where this is going to be set
  320. dwRet = RegCreateKeyEx( HKEY_LOCAL_MACHINE, // Root Key
  321. REGISTR_IISSETUP_LOCATION, // Subkey
  322. 0, // Reserved
  323. _T(""), // Class ID
  324. REG_OPTION_NON_VOLATILE,
  325. KEY_WRITE, // Write Access
  326. NULL,
  327. &hKey,
  328. NULL );
  329. if ( dwRet != ERROR_SUCCESS )
  330. {
  331. // Failed to open key
  332. return FALSE;
  333. }
  334. if ( RegSetValueEx( hKey, // Key
  335. REGISTR_IISSETUP_DISABLEW3SVC, // Value Name
  336. 0, // Reserver
  337. REG_DWORD, // DWORD
  338. (LPBYTE) &dwValue, // Value
  339. sizeof(dwValue) ) != ERROR_SUCCESS )
  340. {
  341. // Failed to set value
  342. bRet =FALSE;
  343. }
  344. RegCloseKey( hKey );
  345. return bRet;
  346. }