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.

384 lines
7.1 KiB

  1. //***************************************************************************
  2. //
  3. // MAINDLL.CPP
  4. //
  5. // Module: IPSec WMI provider for SCE
  6. //
  7. // Purpose: Contains DLL entry points. Also has code that controls
  8. // when the DLL can be unloaded by tracking the number of
  9. // objects and locks as well as routines that support
  10. // self registration.
  11. //
  12. // Copyright (c) 1999-2001 Microsoft Corporation
  13. //
  14. // Original Create Date: 2/19/2001
  15. // Original Author: shawnwu
  16. //***************************************************************************
  17. #include <objbase.h>
  18. #include "netsecprov.h"
  19. #include "netseccore_i.c"
  20. #include "resource.h"
  21. #include "ipsecparser.h"
  22. #ifdef _MERGE_PROXYSTUB
  23. extern "C" HINSTANCE hProxyDll;
  24. #endif
  25. CComModule _Module;
  26. BEGIN_OBJECT_MAP(ObjectMap)
  27. OBJECT_ENTRY(CLSID_NetSecProv, CNetSecProv)
  28. OBJECT_ENTRY(CLSID_IPSecPathParser, CIPSecPathParser)
  29. OBJECT_ENTRY(CLSID_IPSecQueryParser, CIPSecQueryParser)
  30. END_OBJECT_MAP()
  31. LPCWSTR lpszIPSecProvMof = L"Wbem\\NetProv.mof";
  32. //
  33. //LPCWSTR lpszIPSecRsopMof = L"Wbem\\NetRsop.mof";
  34. //
  35. /*
  36. Routine Description:
  37. Name:
  38. DllMain
  39. Functionality:
  40. Entry point for DLL.
  41. Virtual:
  42. N/A.
  43. Arguments:
  44. hInstance - Handle to the instance.
  45. ulReason - indicates reason of being called.
  46. pvReserved -
  47. Return Value:
  48. Success:
  49. TRUE
  50. Failure:
  51. FALSE
  52. Notes:
  53. See MSDN for standard DllMain
  54. */
  55. extern "C"
  56. BOOL
  57. WINAPI DllMain (
  58. IN HINSTANCE hInstance,
  59. IN ULONG ulReason,
  60. IN LPVOID pvReserved
  61. )
  62. {
  63. #ifdef _MERGE_PROXYSTUB
  64. if (!PrxDllMain(hInstance, dwReason, lpReserved))
  65. {
  66. return FALSE;
  67. }
  68. #endif
  69. if (ulReason == DLL_PROCESS_ATTACH)
  70. {
  71. OutputDebugString(L"IPSecProv.dll loaded.\n");
  72. _Module.Init(ObjectMap, hInstance);
  73. DisableThreadLibraryCalls(hInstance);
  74. }
  75. else if (ulReason == DLL_PROCESS_DETACH)
  76. {
  77. OutputDebugString(L"IPSecProv.dll unloaded.\n");
  78. _Module.Term();
  79. }
  80. return TRUE;
  81. }
  82. /*
  83. Routine Description:
  84. Name:
  85. DllGetClassObject
  86. Functionality:
  87. This method creates an object of a specified CLSID and
  88. retrieves an interface pointer to this object.
  89. Virtual:
  90. N/A.
  91. Arguments:
  92. rclsid - Class ID (guid ref).
  93. REFIID - Interface ID (guid ref).
  94. ppv - Receives the class factory interface pointer.
  95. Return Value:
  96. Success:
  97. S_OK
  98. Failure:
  99. Other error code.
  100. Notes:
  101. See MSDN for standard CComModule::DllGetClassObject
  102. */
  103. STDAPI
  104. DllGetClassObject (
  105. IN REFCLSID rclsid,
  106. IN REFIID riid,
  107. OUT PPVOID ppv
  108. )
  109. {
  110. #ifdef _MERGE_PROXYSTUB
  111. if (PrxDllGetClassObject(rclsid, riid, ppv) == S_OK)
  112. {
  113. return S_OK;
  114. }
  115. #endif
  116. return _Module.GetClassObject(rclsid, riid, ppv);
  117. }
  118. /*
  119. Routine Description:
  120. Name:
  121. DllCanUnloadNow
  122. Functionality:
  123. Called periodically by COM in order to determine if the
  124. DLL can be freed.
  125. Virtual:
  126. N/A.
  127. Arguments:
  128. None.
  129. Return Value:
  130. S_OK if it's OK to unload, otherwise, S_FALSE
  131. Notes:
  132. See MSDN for standard DllCanUnloadNow
  133. */
  134. STDAPI
  135. DllCanUnloadNow ()
  136. {
  137. #ifdef _MERGE_PROXYSTUB
  138. if (PrxDllCanUnloadNow() != S_OK)
  139. {
  140. return S_FALSE;
  141. }
  142. #endif
  143. return (_Module.GetLockCount() == 0) ? S_OK : S_FALSE;
  144. }
  145. /*
  146. Routine Description:
  147. Name:
  148. DllRegisterServer
  149. Functionality:
  150. Called to register our dll. We also compile the mof file(s).
  151. Virtual:
  152. N/A.
  153. Arguments:
  154. None.
  155. Return Value:
  156. Success:
  157. Various success codes.
  158. Failure:
  159. Various error codes indicating the problem.
  160. Notes:
  161. See MSDN for standard DllRegisterServer
  162. */
  163. STDAPI
  164. DllRegisterServer ()
  165. {
  166. #ifdef _MERGE_PROXYSTUB
  167. HRESULT hRes = PrxDllRegisterServer();
  168. if (FAILED(hRes))
  169. {
  170. return hRes;
  171. }
  172. #endif
  173. HRESULT hr = _Module.RegisterServer(TRUE);
  174. //
  175. // now compile the MOF files, will ignore if such compilation fails
  176. //
  177. if (SUCCEEDED(hr))
  178. {
  179. //
  180. // is this arbitrary?
  181. //
  182. const int WBEM_MOF_FILE_LEN = _MAX_FNAME;
  183. //
  184. // potentially append L'\\'
  185. //
  186. WCHAR szMofFile[MAX_PATH + 1 + WBEM_MOF_FILE_LEN];
  187. szMofFile[0] = L'\0';
  188. UINT uSysDirLen = ::GetSystemDirectory( szMofFile, MAX_PATH );
  189. if (uSysDirLen > 0 && uSysDirLen < MAX_PATH)
  190. {
  191. if (szMofFile[uSysDirLen] != L'\\')
  192. {
  193. szMofFile[uSysDirLen] = L'\\';
  194. //
  195. // we are not going to overrun buffer because of the extra 1 for szMofFile
  196. //
  197. szMofFile[uSysDirLen + 1] = L'\0';
  198. ++uSysDirLen;
  199. }
  200. HRESULT hrIgnore = WBEM_NO_ERROR;
  201. //
  202. // this protects buffer overrun
  203. //
  204. if (wcslen(lpszIPSecProvMof) < WBEM_MOF_FILE_LEN)
  205. {
  206. wcscpy(szMofFile + uSysDirLen, lpszIPSecProvMof);
  207. hrIgnore = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  208. if (SUCCEEDED(hrIgnore))
  209. {
  210. CComPtr<IMofCompiler> srpMof;
  211. hrIgnore = ::CoCreateInstance (CLSID_MofCompiler, NULL, CLSCTX_INPROC_SERVER, IID_IMofCompiler, (void **)&srpMof);
  212. if (SUCCEEDED(hrIgnore))
  213. {
  214. WBEM_COMPILE_STATUS_INFO stat;
  215. hrIgnore = srpMof->CompileFile( szMofFile, NULL,NULL,NULL,NULL, 0,0,0, &stat);
  216. //
  217. // compile RSOP mof
  218. // this protects buffer overrun
  219. //if (wcslen(lpszIPSecRsopMof) < WBEM_MOF_FILE_LEN)
  220. //{
  221. // wcscpy(szMofFile + uSysDirLen, lpszIPSecRsopMof);
  222. // hrIgnore = srpMof->CompileFile( szMofFile, NULL,NULL,NULL,NULL, 0,0,0, &stat);
  223. //}
  224. //
  225. }
  226. ::CoUninitialize();
  227. }
  228. }
  229. }
  230. }
  231. return hr;
  232. }
  233. /*
  234. Routine Description:
  235. Name:
  236. DllUnregisterServer
  237. Functionality:
  238. Called to un-register our dll. There is no equivalence in MOF compilation.
  239. Virtual:
  240. N/A.
  241. Arguments:
  242. None.
  243. Return Value:
  244. Success:
  245. Various success codes.
  246. Failure:
  247. Various error codes indicating the problem.
  248. Notes:
  249. See MSDN for standard DllUnregisterServer.
  250. $undone:shawnwu, should we also delete all classes registered by our MOF?
  251. */
  252. STDAPI
  253. DllUnregisterServer ()
  254. {
  255. #ifdef _MERGE_PROXYSTUB
  256. PrxDllUnregisterServer();
  257. #endif
  258. _Module.UnregisterServer();
  259. return S_OK;
  260. }