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.

422 lines
14 KiB

  1. #define UNICODE
  2. #define INITGUID
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include <clusapi.h>
  6. typedef HCLUSTER
  7. (WINAPI *PFN_OPENCLUSTER)(
  8. IN LPCWSTR lpszClusterName
  9. );
  10. typedef BOOL
  11. (WINAPI *PFN_CLOSECLUSTER)(
  12. IN HCLUSTER hCluster
  13. );
  14. typedef DWORD
  15. (WINAPI *PFN_CREATECLUSTERRESOURCETYPE)(
  16. IN HCLUSTER hCluster,
  17. IN LPCWSTR lpszResourceTypeName,
  18. IN LPCWSTR lpszDisplayName,
  19. IN LPCWSTR lpszResourceTypeDll,
  20. IN DWORD dwLooksAlivePollInterval,
  21. IN DWORD dwIsAlivePollInterval
  22. );
  23. typedef DWORD
  24. (WINAPI *PFN_DELETECLUSTERRESOURCETYPE)(
  25. IN HCLUSTER hCluster,
  26. IN LPCWSTR lpszResourceTypeName
  27. );
  28. typedef HCLUSENUM
  29. (WINAPI
  30. *PFN_ClusterOpenEnum)(
  31. IN HCLUSTER hCluster,
  32. IN DWORD dwType
  33. );
  34. typedef DWORD
  35. (WINAPI
  36. *PFN_ClusterEnum)(
  37. IN HCLUSENUM hEnum,
  38. IN DWORD dwIndex,
  39. OUT LPDWORD lpdwType,
  40. OUT LPWSTR lpszName,
  41. IN OUT LPDWORD lpcbName
  42. );
  43. typedef DWORD
  44. (WINAPI
  45. *PFN_ClusterCloseEnum)(
  46. IN HCLUSENUM hEnum
  47. );
  48. typedef HRESOURCE
  49. (WINAPI
  50. *PFN_OpenClusterResource)(
  51. IN HCLUSTER hCluster,
  52. IN LPCWSTR lpszResourceName
  53. );
  54. typedef BOOL
  55. (WINAPI
  56. *PFN_CloseClusterResource)(
  57. IN HRESOURCE hResource
  58. );
  59. typedef DWORD
  60. (WINAPI
  61. *PFN_DeleteClusterResource)(
  62. IN HRESOURCE hResource
  63. );
  64. typedef DWORD
  65. (WINAPI
  66. *PFN_OfflineClusterResource)(
  67. IN HRESOURCE hResource
  68. );
  69. typedef HKEY
  70. (WINAPI
  71. *PFN_GetClusterResourceKey)(
  72. IN HRESOURCE hResource,
  73. IN REGSAM samDesired
  74. );
  75. typedef LONG
  76. (WINAPI
  77. *PFN_ClusterRegCloseKey)(
  78. IN HKEY hKey
  79. );
  80. typedef LONG
  81. (WINAPI
  82. *PFN_ClusterRegQueryValue)(
  83. IN HKEY hKey,
  84. IN LPCWSTR lpszValueName,
  85. OUT LPDWORD lpValueType,
  86. OUT LPBYTE lpData,
  87. IN OUT LPDWORD lpcbData
  88. );
  89. typedef CLUSTER_RESOURCE_STATE
  90. (WINAPI
  91. *PFN_GetClusterResourceState)(
  92. IN HRESOURCE hResource,
  93. OUT OPTIONAL LPWSTR lpszNodeName,
  94. IN OUT LPDWORD lpcbNodeName,
  95. OUT OPTIONAL LPWSTR lpszGroupName,
  96. IN OUT LPDWORD lpcbGroupName
  97. );
  98. typedef DWORD
  99. (WINAPI *PFN_DLLREGISTERCLUADMINEXTENSION)(
  100. IN HCLUSTER hCluster
  101. );
  102. typedef DWORD
  103. (WINAPI *PFN_DLLUNREGISTERCLUADMINEXTENSION)(
  104. IN HCLUSTER hCluster
  105. );
  106. BOOL
  107. RegisterIisServerInstanceResourceType(
  108. LPWSTR pszResType,
  109. LPWSTR pszResTypeDisplayName,
  110. LPWSTR pszPath,
  111. LPWSTR pszAdminPath
  112. )
  113. {
  114. HCLUSTER hC;
  115. DWORD dwErr = ERROR_SUCCESS;
  116. HINSTANCE hClusapi;
  117. PFN_OPENCLUSTER pfnOpenCluster;
  118. PFN_CLOSECLUSTER pfnCloseCluster;
  119. PFN_CREATECLUSTERRESOURCETYPE pfnCreateClusterResourceType;
  120. HRESULT hres;
  121. if ( hClusapi = LoadLibrary( L"clusapi.dll" ) )
  122. {
  123. pfnOpenCluster = (PFN_OPENCLUSTER)GetProcAddress( hClusapi, "OpenCluster" );
  124. pfnCloseCluster = (PFN_CLOSECLUSTER)GetProcAddress( hClusapi, "CloseCluster" );
  125. pfnCreateClusterResourceType = (PFN_CREATECLUSTERRESOURCETYPE)GetProcAddress( hClusapi, "CreateClusterResourceType" );
  126. if ( pfnOpenCluster &&
  127. pfnCloseCluster &&
  128. pfnCreateClusterResourceType )
  129. {
  130. if ( hC = pfnOpenCluster( NULL ) )
  131. {
  132. hres = pfnCreateClusterResourceType(
  133. hC,
  134. pszResType,
  135. pszResType,
  136. pszPath,
  137. 5000,
  138. 60000 );
  139. if ( SUCCEEDED( hres ) )
  140. {
  141. HINSTANCE hAdmin;
  142. PFN_DLLREGISTERCLUADMINEXTENSION pfnDllRegisterCluAdminExtension;
  143. if ( hAdmin = LoadLibrary( pszAdminPath ) )
  144. {
  145. pfnDllRegisterCluAdminExtension =
  146. (PFN_DLLREGISTERCLUADMINEXTENSION)GetProcAddress( hAdmin, "DllRegisterCluAdminExtension" );
  147. if ( pfnDllRegisterCluAdminExtension )
  148. {
  149. if ( FAILED(hres = pfnDllRegisterCluAdminExtension( hC )) )
  150. {
  151. dwErr = hres;
  152. }
  153. }
  154. else
  155. {
  156. dwErr = GetLastError();
  157. }
  158. FreeLibrary( hAdmin );
  159. }
  160. else
  161. {
  162. dwErr = GetLastError();
  163. }
  164. }
  165. else
  166. {
  167. dwErr = hres;
  168. }
  169. pfnCloseCluster( hC );
  170. if ( dwErr )
  171. {
  172. SetLastError( dwErr );
  173. }
  174. }
  175. }
  176. else
  177. {
  178. dwErr = GetLastError();
  179. }
  180. FreeLibrary( hClusapi );
  181. }
  182. else
  183. {
  184. dwErr = GetLastError();
  185. }
  186. return dwErr == ERROR_SUCCESS ? TRUE : FALSE;
  187. }
  188. BOOL
  189. UnregisterIisServerInstanceResourceType(
  190. LPWSTR pszResType,
  191. LPWSTR pszAdminPath
  192. )
  193. {
  194. HCLUSTER hC;
  195. DWORD dwErr = ERROR_SUCCESS;
  196. HINSTANCE hClusapi;
  197. PFN_OPENCLUSTER pfnOpenCluster;
  198. PFN_CLOSECLUSTER pfnCloseCluster;
  199. PFN_DELETECLUSTERRESOURCETYPE pfnDeleteClusterResourceType;
  200. PFN_ClusterOpenEnum pfnClusterOpenEnum;
  201. PFN_ClusterEnum pfnClusterEnum;
  202. PFN_ClusterCloseEnum pfnClusterCloseEnum;
  203. PFN_OpenClusterResource pfnOpenClusterResource;
  204. PFN_CloseClusterResource pfnCloseClusterResource;
  205. PFN_DeleteClusterResource pfnDeleteClusterResource;
  206. PFN_OfflineClusterResource pfnOfflineClusterResource;
  207. PFN_GetClusterResourceKey pfnGetClusterResourceKey;
  208. PFN_ClusterRegCloseKey pfnClusterRegCloseKey;
  209. PFN_ClusterRegQueryValue pfnClusterRegQueryValue;
  210. PFN_GetClusterResourceState pfnGetClusterResourceState;
  211. HRESULT hres;
  212. HCLUSENUM hClusEnum;
  213. WCHAR awchResName[256];
  214. WCHAR awchResType[256];
  215. DWORD dwEnum;
  216. DWORD dwType;
  217. DWORD dwStrLen;
  218. HRESOURCE hRes;
  219. HKEY hKey;
  220. BOOL fDel;
  221. DWORD dwRetry;
  222. if ( hClusapi = LoadLibrary( L"clusapi.dll" ) )
  223. {
  224. pfnOpenCluster = (PFN_OPENCLUSTER)GetProcAddress( hClusapi, "OpenCluster" );
  225. pfnCloseCluster = (PFN_CLOSECLUSTER)GetProcAddress( hClusapi, "CloseCluster" );
  226. pfnDeleteClusterResourceType = (PFN_DELETECLUSTERRESOURCETYPE)GetProcAddress( hClusapi, "DeleteClusterResourceType" );
  227. pfnClusterOpenEnum = (PFN_ClusterOpenEnum)GetProcAddress( hClusapi, "ClusterOpenEnum" );
  228. pfnClusterEnum = (PFN_ClusterEnum)GetProcAddress( hClusapi, "ClusterEnum" );
  229. pfnClusterCloseEnum = (PFN_ClusterCloseEnum)GetProcAddress( hClusapi, "ClusterCloseEnum" );
  230. pfnOpenClusterResource = (PFN_OpenClusterResource)GetProcAddress( hClusapi, "OpenClusterResource" );
  231. pfnCloseClusterResource = (PFN_CloseClusterResource)GetProcAddress( hClusapi, "CloseClusterResource" );
  232. pfnDeleteClusterResource = (PFN_DeleteClusterResource)GetProcAddress( hClusapi, "DeleteClusterResource" );
  233. pfnOfflineClusterResource = (PFN_OfflineClusterResource)GetProcAddress( hClusapi, "OfflineClusterResource" );
  234. pfnGetClusterResourceKey = (PFN_GetClusterResourceKey)GetProcAddress( hClusapi, "GetClusterResourceKey" );
  235. pfnClusterRegCloseKey = (PFN_ClusterRegCloseKey)GetProcAddress( hClusapi, "ClusterRegCloseKey" );
  236. pfnClusterRegQueryValue = (PFN_ClusterRegQueryValue)GetProcAddress( hClusapi, "ClusterRegQueryValue" );
  237. pfnGetClusterResourceState = (PFN_GetClusterResourceState)GetProcAddress( hClusapi, "GetClusterResourceState" );
  238. if ( pfnOpenCluster &&
  239. pfnCloseCluster &&
  240. pfnDeleteClusterResourceType &&
  241. pfnClusterOpenEnum &&
  242. pfnClusterEnum &&
  243. pfnClusterCloseEnum &&
  244. pfnOpenClusterResource &&
  245. pfnCloseClusterResource &&
  246. pfnDeleteClusterResource &&
  247. pfnOfflineClusterResource &&
  248. pfnGetClusterResourceKey &&
  249. pfnClusterRegCloseKey &&
  250. pfnClusterRegQueryValue &&
  251. pfnGetClusterResourceState )
  252. {
  253. if ( hC = pfnOpenCluster( NULL ) )
  254. {
  255. //
  256. // Delete all resources of type pszResType
  257. //
  258. if ( (hClusEnum = pfnClusterOpenEnum( hC, CLUSTER_ENUM_RESOURCE )) != NULL )
  259. {
  260. for ( dwEnum = 0 ;
  261. pfnClusterEnum( hClusEnum,
  262. dwEnum,
  263. &dwType,
  264. awchResName,
  265. &(dwStrLen=sizeof(awchResName)/sizeof(WCHAR)) )
  266. == ERROR_SUCCESS ;
  267. ++dwEnum )
  268. {
  269. if ( hRes = pfnOpenClusterResource( hC, awchResName ) )
  270. {
  271. if ( hKey = pfnGetClusterResourceKey( hRes, KEY_READ ) )
  272. {
  273. dwStrLen = sizeof(awchResType)/sizeof(WCHAR);
  274. fDel = pfnClusterRegQueryValue( hKey,
  275. L"Type",
  276. &dwType,
  277. (LPBYTE)awchResType,
  278. &dwStrLen )
  279. == ERROR_SUCCESS &&
  280. !wcscmp( awchResType, pszResType );
  281. pfnClusterRegCloseKey( hKey );
  282. if ( fDel )
  283. {
  284. pfnOfflineClusterResource( hRes );
  285. for ( dwRetry = 0 ;
  286. dwRetry < 30 &&
  287. pfnGetClusterResourceState( hRes,
  288. NULL,
  289. &dwStrLen,
  290. NULL,
  291. &dwStrLen )
  292. != ClusterResourceOffline ;
  293. ++dwRetry )
  294. {
  295. Sleep( 1000 );
  296. }
  297. pfnDeleteClusterResource( hRes );
  298. }
  299. }
  300. pfnCloseClusterResource( hRes );
  301. }
  302. }
  303. pfnClusterCloseEnum( hClusEnum );
  304. }
  305. dwErr = pfnDeleteClusterResourceType(
  306. hC,
  307. pszResType );
  308. HINSTANCE hAdmin;
  309. PFN_DLLUNREGISTERCLUADMINEXTENSION pfnDllUnregisterCluAdminExtension;
  310. if ( hAdmin = LoadLibrary( pszAdminPath ) )
  311. {
  312. pfnDllUnregisterCluAdminExtension =
  313. (PFN_DLLUNREGISTERCLUADMINEXTENSION)GetProcAddress( hAdmin, "DllUnregisterCluAdminExtension" );
  314. if ( pfnDllUnregisterCluAdminExtension )
  315. {
  316. if ( FAILED(hres = pfnDllUnregisterCluAdminExtension( hC )) )
  317. {
  318. dwErr = hres;
  319. }
  320. }
  321. else
  322. {
  323. dwErr = GetLastError();
  324. }
  325. FreeLibrary( hAdmin );
  326. }
  327. else
  328. {
  329. dwErr = GetLastError();
  330. }
  331. pfnCloseCluster( hC );
  332. if ( dwErr )
  333. {
  334. SetLastError( dwErr );
  335. }
  336. }
  337. }
  338. else
  339. {
  340. dwErr = GetLastError();
  341. }
  342. FreeLibrary( hClusapi );
  343. }
  344. else
  345. {
  346. dwErr = GetLastError();
  347. }
  348. return dwErr == ERROR_SUCCESS ? TRUE : FALSE;
  349. }
  350. int __cdecl main( int argc, char*argv[] )
  351. {
  352. BOOL fSt;
  353. if ( !strcmp(argv[1],"install" ) )
  354. {
  355. fSt = RegisterIisServerInstanceResourceType(
  356. L"IIS Server Instance", // do not touch
  357. L"IIS Server Instance", // do not touch
  358. L"c:\\winnt\\system32\\inetsrv\\clusiis4.dll", // path to clusiis4.dll
  359. L"c:\\winnt\\system32\\inetsrv\\iisclex4.dll" ); // path to admin ext
  360. }
  361. else
  362. {
  363. fSt = UnregisterIisServerInstanceResourceType(
  364. L"IIS Server Instance", // do not touch
  365. L"c:\\winnt\\system32\\inetsrv\\iisclex4.dll" ); // path to admin ext
  366. }
  367. if ( !fSt )
  368. {
  369. printf( "Error %d\n", GetLastError() );
  370. }
  371. return 0;
  372. }