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.

483 lines
13 KiB

  1. //////////////////////////////////////////////////////////////////////
  2. // IPSecBase.h : Declaration of the base classes for the Network
  3. // security WMI provider for SCE
  4. // Copyright (c)1997-2001 Microsoft Corporation
  5. //
  6. // Original Create Date: 3/6/2001
  7. // Original Author: shawnwu
  8. //////////////////////////////////////////////////////////////////////
  9. #pragma once
  10. #include "globals.h"
  11. extern CComVariant g_varRollbackGuid;
  12. extern const DWORD IP_ADDR_LENGTH;
  13. extern const DWORD GUID_STRING_LENGTH;
  14. interface ISceKeyChain;
  15. /*
  16. Class description
  17. Naming:
  18. CIPSecBase stands for Base for IPSec.
  19. Base class:
  20. (1) CComObjectRootEx for threading model and IUnknown.
  21. (2) IIPSecObjectImpl which implements the common interface for all C++ classes
  22. representing WMI classes.
  23. Purpose of class:
  24. (1) Allow our provider class (CNetSecProv) to easily create our objects that has
  25. implementation for the various WMI classes they represent.
  26. (2) Being a base for all our C++ classes that has implementation for various WMI
  27. classes.
  28. Design:
  29. (1) CreateObject function allows the caller to get an IIPSecObjectImpl object.
  30. IIPSecObjectImpl is the uniform interface this provider uses. Everything
  31. the provider does for WMI goes through this IIPSecObjectImpl.
  32. (2) To provide base class support for its sub-classes, this class implements
  33. the following facilities:
  34. (a) It caches all the necessary COM interfaces coming from WMI.
  35. (b) It knows the key chain object (which holds key property information
  36. chain object may be about the WMI object the C++ is trying to represent).
  37. This key given by the provider, but sometimes the sub-class needs
  38. to modify the key chain according its own unique need.
  39. (c) Provide helper for spawn an instance (SpawnObjectInstance) that can be
  40. used to fill in properties.
  41. (d) Provide helper for spawn a rollback instance (SpawnRollbackInstance)
  42. that can be used to fill in properties.
  43. (e) Provide helper for finding policies by name (FindPolicyByName).
  44. Use:
  45. (1) For provider (CNetSecProv), all it needs to call is CreateObject. Since that is
  46. a static function, it doesn't need to create an instance for the need.
  47. (2) For sub-classes, just call the needed function. Of course, all static functions
  48. are available for the sub-classes' static functions.
  49. Notes:
  50. (1) It contains several template functions. This reduces the duplicate code.
  51. (2) InitMembers is really just intended for private use. But since it is used inside
  52. a template function, we have to make it public. Since unrelated classes can never
  53. create this class (its constructor is protected!), this should not be a problem
  54. because the method is not part of the interface this class gives out (IIPSecObjectImpl),
  55. so no body but its sub-classes can call it.
  56. */
  57. class ATL_NO_VTABLE CIPSecBase :
  58. public CComObjectRootEx<CComMultiThreadModel>,
  59. public IIPSecObjectImpl
  60. {
  61. protected:
  62. CIPSecBase(){}
  63. virtual ~CIPSecBase(){}
  64. BEGIN_COM_MAP(CIPSecBase)
  65. COM_INTERFACE_ENTRY(IIPSecObjectImpl)
  66. END_COM_MAP()
  67. DECLARE_NOT_AGGREGATABLE( CIPSecBase )
  68. DECLARE_REGISTRY_RESOURCEID(IDR_NETSECPROV)
  69. public:
  70. //
  71. // IIPSecObjectImpl methods:
  72. // This is an abstract class.
  73. //
  74. STDMETHOD(QueryInstance) (
  75. IN LPCWSTR pszQuery,
  76. IN IWbemContext * pCtx,
  77. IN IWbemObjectSink * pSink
  78. ) = 0;
  79. STDMETHOD(DeleteInstance) (
  80. IN IWbemContext * pCtx,
  81. IN IWbemObjectSink * pSink
  82. ) = 0;
  83. STDMETHOD(ExecuteMethod) (
  84. IN BSTR bstrMethod,
  85. IN IWbemContext * pCtx,
  86. IN IWbemObjectSink * pSink
  87. )
  88. {
  89. return WBEM_E_NOT_SUPPORTED;
  90. }
  91. STDMETHOD(PutInstance) (
  92. IN IWbemClassObject * pInst,
  93. IN IWbemContext * pCtx,
  94. IN IWbemObjectSink * pSink
  95. ) = 0;
  96. STDMETHOD(GetInstance) (
  97. IN IWbemContext * pCtx,
  98. IN IWbemObjectSink * pSink
  99. ) = 0;
  100. static HRESULT CreateObject (
  101. IN IWbemServices * pNamespace,
  102. IN IIPSecKeyChain * pKeyChain,
  103. IN IWbemContext * pCtx,
  104. OUT IIPSecObjectImpl ** ppObjImp
  105. );
  106. //
  107. // some template functions
  108. //
  109. /*
  110. Routine Description:
  111. Name:
  112. CIPSecBase::FindPolicyByName
  113. Functionality:
  114. Enumerate all policies. If the name is given, then we will give the policy.
  115. If the name is empty, then we will just give back the current one and advance
  116. the enumeration handle (pdwResumeHandle).
  117. Virtual:
  118. No.
  119. Arguments:
  120. pszName - The name of the policy. Optional.
  121. ppPolicy - Receives the policy. This can be PIPSEC_MM_POLICY or PIPSEC_QM_POLICY.
  122. Caller needs to free this by calling SPDApiBufferFree(*ppPolicy);
  123. pdwResumeHandle - In-bound: the current handle, out-bound: the next handle.
  124. Return Value:
  125. Success:
  126. WBEM_NO_ERROR
  127. Failure:
  128. (1) WBEM_E_INVALID_PARAMETER if ppPolicy == NULL or pdwResumeHandle == NULL.
  129. (2) WBEM_E_NOT_FOUND if the policy is not found.
  130. Notes:
  131. (1) Main Mode and Quick Mode polices have slightly different data strctures. Perfect
  132. place to write this template function to reduce duplicate code.
  133. (2) Caller needs to free any policy returned by calling SPDApiBufferFree; Don't just
  134. delete/free it. This is a good place, if time allowed, to write a wrapper to
  135. automatically call SPDApiBufferFree inside its destructor.
  136. */
  137. template <class Policy>
  138. static HRESULT FindPolicyByName (
  139. IN LPCWSTR pszName OPTIONAL,
  140. OUT Policy ** ppPolicy,
  141. IN OUT DWORD * pdwResumeHandle
  142. )
  143. {
  144. if (ppPolicy == NULL || pdwResumeHandle == NULL)
  145. {
  146. return WBEM_E_INVALID_PARAMETER;
  147. }
  148. *ppPolicy = NULL;
  149. DWORD dwCount = 0;
  150. //
  151. // get the current policy (determined by pdwResumeHandle)
  152. //
  153. DWORD dwResult = EnumPolicies(NULL, ppPolicy, &dwCount, pdwResumeHandle);
  154. while (dwResult == ERROR_SUCCESS && dwCount > 0)
  155. {
  156. if (pszName == NULL || *pszName == L'\0' || _wcsicmp((*ppPolicy)->pszPolicyName, pszName) == 0)
  157. {
  158. //
  159. // if no name is given or the names match (case-insentively), this is it.
  160. //
  161. return WBEM_NO_ERROR;
  162. }
  163. else
  164. {
  165. //
  166. // names are given but they don't match
  167. //
  168. //
  169. // Free the current policy
  170. //
  171. ::SPDApiBufferFree(*ppPolicy);
  172. *ppPolicy = NULL;
  173. dwCount = 0;
  174. //
  175. // get the next
  176. //
  177. dwResult = EnumPolicies(NULL, ppPolicy, &dwCount, pdwResumeHandle);
  178. }
  179. }
  180. return WBEM_E_NOT_FOUND;
  181. };
  182. HRESULT InitMembers (
  183. IN IWbemContext * pCtx,
  184. IN IWbemServices * pNamespace,
  185. IN IIPSecKeyChain * pKeyChain,
  186. IN LPCWSTR pszWmiClassName
  187. );
  188. protected:
  189. //
  190. // some template functions
  191. //
  192. /*
  193. Routine Description:
  194. Name:
  195. CIPSecBase::CreateIPSecObject
  196. Functionality:
  197. Private helper to create an IIPSecObjectImpl object (our C++ classes that
  198. implement their corresponding WMI classes). Normally, to do this needs a
  199. class name. However, the class name is captured inside the key chain.
  200. Virtual:
  201. No.
  202. Arguments:
  203. pSub - The template type. Not used otherwise.
  204. pNamespace - COM interface pointer for the namespace (given by WMI).
  205. pKeyChain - COM interface pointer representing the query's key properties.
  206. pszWmiClassName - The name of the WMI class this class is created to represent.
  207. pCtx - COM interface pointer given by WMI that needs to be passed around
  208. for various WMI API's.
  209. ppObjImp - Receives the object.
  210. Return Value:
  211. Success:
  212. WBEM_NO_ERROR
  213. Failure:
  214. (1) WBEM_E_INVALID_PARAMETER if pNamespace == NULL or pKeyChain == NULL or ppObjImp == NULL.
  215. (2) WBEM_E_NOT_SUPPORTED if the class that supports the WMI class name is not properly
  216. implemented for IID_IIPSecObjectImpl interface.
  217. Notes:
  218. */
  219. template <class Sub>
  220. static HRESULT CreateIPSecObject (
  221. IN Sub * pSub,
  222. IN IWbemServices * pNamespace,
  223. IN IIPSecKeyChain * pKeyChain,
  224. IN LPCWSTR pszWmiClassName,
  225. IN IWbemContext * pCtx,
  226. OUT IIPSecObjectImpl ** ppObjImp
  227. )
  228. {
  229. //
  230. // pSub is just used for type
  231. //
  232. if (NULL == pNamespace ||
  233. NULL == pKeyChain ||
  234. NULL == ppObjImp)
  235. {
  236. return WBEM_E_INVALID_PARAMETER;
  237. }
  238. *ppObjImp = NULL;
  239. //
  240. // This is a confusing ATL CComObject creation sequence. Basically,
  241. // CComObject will create the proper class and wrapped it with
  242. // its implementation for the IUnknown. Our C++ classes (like CMMFilter)
  243. // is never the leaf in ATL world. In other word, you never create
  244. // classes like CMMFilter directly. Instead, the most derived class
  245. // is CComObject< >. Take a while to get used too.
  246. //
  247. CComObject<Sub> *pTheSubClass;
  248. HRESULT hr = CComObject< Sub >::CreateInstance(&pTheSubClass);
  249. if (SUCCEEDED(hr))
  250. {
  251. hr = pTheSubClass->QueryInterface(IID_IIPSecObjectImpl, (void**)ppObjImp);
  252. if (S_OK == hr)
  253. {
  254. hr = pTheSubClass->InitMembers(pCtx, pNamespace, pKeyChain, pszWmiClassName);
  255. }
  256. else
  257. {
  258. hr = WBEM_E_NOT_SUPPORTED;
  259. }
  260. }
  261. return SUCCEEDED(hr) ? WBEM_NO_ERROR : hr;
  262. }
  263. /*
  264. Routine Description:
  265. Name:
  266. CIPSecBase::EnumPolicies
  267. Functionality:
  268. Helper for FindPolicyByName because different mode's enumeration functions have different names.
  269. In order for our template function to work, it has been wrapped up.
  270. See EnumMMPolicies for details.
  271. */
  272. static DWORD EnumPolicies (
  273. IN LPCWSTR pszServer,
  274. OUT PIPSEC_MM_POLICY * ppPolicy,
  275. OUT DWORD * pdwCount,
  276. IN OUT DWORD * pdwResumeHandle
  277. )
  278. {
  279. //
  280. // bad IPSec API prototype causes this casting
  281. //
  282. return ::EnumMMPolicies((LPWSTR)pszServer, ppPolicy, 1, pdwCount, pdwResumeHandle);
  283. }
  284. /*
  285. Routine Description:
  286. Name:
  287. CIPSecBase::EnumPolicies
  288. Functionality:
  289. Helper for FindPolicyByName because different mode's enumeration functions have different names.
  290. In order for our template function to work, it has been wrapped up.
  291. See EnumQMPolicies for details.
  292. */
  293. static DWORD EnumPolicies (
  294. IN LPCWSTR pszServer,
  295. OUT PIPSEC_QM_POLICY * ppPolicy,
  296. OUT DWORD * pdwCount,
  297. IN OUT DWORD * pdwResumeHandle
  298. )
  299. {
  300. //
  301. // bad IPSec API prototype causes this casting
  302. //
  303. return ::EnumQMPolicies((LPWSTR)pszServer, ppPolicy, 1, pdwCount, pdwResumeHandle);
  304. }
  305. HRESULT SpawnObjectInstance (
  306. OUT IWbemClassObject **ppObj
  307. );
  308. HRESULT SpawnRollbackInstance (
  309. IN LPCWSTR pszClassName,
  310. OUT IWbemClassObject ** ppObj
  311. );
  312. CComPtr<IWbemServices> m_srpNamespace;
  313. CComPtr<IWbemContext> m_srpCtx;
  314. CComPtr<IIPSecKeyChain> m_srpKeyChain;
  315. CComBSTR m_bstrWMIClassName;
  316. private:
  317. CComPtr<IWbemClassObject> m_srpClassDefinition;
  318. };
  319. /*
  320. // implementation for the WMI class called Nsp_RollbackFilterSettings
  321. class CNspRollbackFilter
  322. {
  323. [key] string FilterGUID;
  324. string StorePath;
  325. string FilterName;
  326. uint32 dwFilterType; // tunnel, transport, Mainmode
  327. string PolicyGUID;
  328. string AuthGUID;
  329. };
  330. // implementation for the WMI class called Nsp_RollbackPolicySettings
  331. class CNspRollbackPolicy
  332. {
  333. [key] string PolicyGUID;
  334. uint32 dwPolicyType; // MMPolicy, MMAuth, QMPolicy
  335. };
  336. */