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.

463 lines
11 KiB

  1. // TranxMgr.cpp: implementation for the CTranxMgr
  2. //
  3. // Copyright (c)1997-2001 Microsoft Corporation
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "precomp.h"
  7. #include "TranxMgr.h"
  8. #include "FilterMM.h"
  9. #include "FilterTr.h"
  10. #include "FilterTun.h"
  11. #include "PolicyMM.h"
  12. #include "PolicyQM.h"
  13. #include "AuthMM.h"
  14. /*
  15. Routine Description:
  16. Name:
  17. CTranxManager::ExecMethod
  18. Functionality:
  19. This is our C++ class implmeneting Nsp_TranxManager WMI class which
  20. can execute methods. The class is defined to support "rollback".
  21. Virtual:
  22. Yes (part of IIPSecObjectImpl)
  23. Arguments:
  24. pNamespace - our namespace.
  25. pszMethod - The name of the method.
  26. pCtx - COM interface pointer by WMI and needed for various WMI APIs
  27. pInParams - COM interface pointer to the input parameter object.
  28. pSink - COM interface pointer for notifying WMI about results.
  29. Return Value:
  30. Success:
  31. Various success codes indicating the result.
  32. Failure:
  33. Various errors may occur. We return various error code to indicate such errors.
  34. Notes:
  35. Even if errors happen amid the rolling back execution, we will continue. However, we will
  36. return the first error.
  37. */
  38. HRESULT
  39. CTranxManager::ExecMethod (
  40. IN IWbemServices * pNamespace,
  41. IN LPCWSTR pszMethod,
  42. IN IWbemContext * pCtx,
  43. IN IWbemClassObject * pInParams,
  44. IN IWbemObjectSink * pSink
  45. )
  46. {
  47. if (pszMethod == NULL || *pszMethod == L'\0')
  48. {
  49. return WBEM_E_INVALID_PARAMETER;
  50. }
  51. else if (_wcsicmp(pszMethod, g_pszRollback) != 0)
  52. {
  53. //
  54. // we only support Rollback method
  55. //
  56. //
  57. // $undone:shawnwu, don't check in this code. This is to test
  58. //
  59. /*
  60. if (_wcsicmp(pszMethod, L"ParseXMLFile") == 0)
  61. {
  62. if (pInParams == NULL)
  63. {
  64. return WBEM_E_INVALID_PARAMETER;
  65. }
  66. CComVariant varInput;
  67. HRESULT hrIgnore = pInParams->Get(L"InputFile", 0, &varInput, NULL, NULL);
  68. if (SUCCEEDED(hrIgnore) && varInput.vt == VT_BSTR && varInput.bstrVal != NULL)
  69. {
  70. CComVariant varOutput;
  71. hrIgnore = pInParams->Get(L"OutputFile", 0, &varOutput, NULL, NULL);
  72. if (SUCCEEDED(hrIgnore) && varOutput.vt == VT_BSTR && varOutput.bstrVal != NULL)
  73. {
  74. CComVariant varArea;
  75. hrIgnore = pInParams->Get(L"Area", 0, &varArea, NULL, NULL);
  76. //
  77. // allow the section to be empty - meaning all areas.
  78. //
  79. if (FAILED(hrIgnore) || varArea.vt != VT_BSTR || varArea.bstrVal == NULL)
  80. {
  81. varArea.Clear();
  82. varArea.vt = VT_BSTR;
  83. varArea.bstrVal = NULL;
  84. }
  85. //
  86. // get element info we care about
  87. //
  88. CComVariant varElement;
  89. hrIgnore = pInParams->Get(L"Element", 0, &varElement, NULL, NULL);
  90. //
  91. // allow the element to be empty - meaning every element.
  92. //
  93. if (FAILED(hrIgnore) || varElement.vt != VT_BSTR || varElement.bstrVal == NULL)
  94. {
  95. varElement.Clear();
  96. varElement.vt = VT_BSTR;
  97. varElement.bstrVal = NULL;
  98. }
  99. CComVariant varSingleArea;
  100. hrIgnore = pInParams->Get(L"SingleArea", 0, &varSingleArea, NULL, NULL);
  101. bool bSingleArea = false;
  102. if (SUCCEEDED(hrIgnore) && varSingleArea.vt == VT_BOOL)
  103. {
  104. bSingleArea = (varSingleArea.boolVal == VARIANT_TRUE);
  105. }
  106. return ParseXMLFile(varInput.bstrVal, varOutput.bstrVal, varArea.bstrVal, varElement.bstrVal, bSingleArea);
  107. }
  108. }
  109. return WBEM_E_INVALID_METHOD_PARAMETERS;
  110. }
  111. */
  112. return WBEM_E_NOT_SUPPORTED;
  113. }
  114. else if (pInParams == NULL)
  115. {
  116. return WBEM_E_INVALID_PARAMETER;
  117. }
  118. //
  119. // we are executing g_pszRollback ("Rollback") method
  120. //
  121. //
  122. // get the in parameters (rollback token and rollback flag)
  123. //
  124. CComVariant varToken, varClearAll;
  125. //
  126. // if no token, then we will use the GUID_NULL as the token
  127. //
  128. HRESULT hr = pInParams->Get(g_pszTokenGuid, 0, &varToken, NULL, NULL);
  129. //
  130. // We require the input parameter object to be of good type.
  131. // We won't tolerate a wrong type of data, either, even though
  132. // we can live with missing token, though.
  133. //
  134. if (FAILED(hr) || SUCCEEDED(hr) && varToken.vt != VT_BSTR)
  135. {
  136. return WBEM_E_INVALID_OBJECT;
  137. }
  138. else if (SUCCEEDED(hr) && varToken.bstrVal == NULL || *(varToken.bstrVal) == L'\0')
  139. {
  140. varToken.Clear();
  141. varToken = pszRollbackAll;
  142. }
  143. //
  144. // we will allow the flag to be missing.
  145. // In that case, it is equivalent to false, meaning not to clear all
  146. //
  147. bool bClearAll = false;
  148. hr = pInParams->Get(g_pszClearAll, 0, &varClearAll, NULL, NULL);
  149. if (SUCCEEDED(hr) && varClearAll.vt == VT_BOOL)
  150. {
  151. bClearAll = (varClearAll.boolVal == VARIANT_TRUE);
  152. }
  153. //
  154. // will return the first error
  155. //
  156. HRESULT hrFirstError = WBEM_NO_ERROR;
  157. //
  158. // rollback MM filters first
  159. //
  160. hr = CIPSecFilter::Rollback(pNamespace, varToken.bstrVal, bClearAll);
  161. if (FAILED(hr))
  162. {
  163. hrFirstError = hr;
  164. }
  165. //
  166. // rollback policies
  167. //
  168. hr = CIPSecPolicy::Rollback(pNamespace, varToken.bstrVal, bClearAll);
  169. if (FAILED(hr) && SUCCEEDED(hrFirstError))
  170. {
  171. hrFirstError = hr;
  172. }
  173. //
  174. // rollback main mode authentication
  175. //
  176. hr = CAuthMM::Rollback(pNamespace, varToken.bstrVal, bClearAll);
  177. return FAILED(hrFirstError) ? hrFirstError : hr;
  178. }
  179. /*
  180. Routine Description:
  181. Name:
  182. CMMPolicy::ParseXMLFile
  183. Functionality:
  184. Testing the MSXML parser.
  185. Virtual:
  186. No.
  187. Arguments:
  188. pszInput - The input file (XML) name.
  189. pszOutput - We will write the parsing result into this output file.
  190. Return Value:
  191. Success:
  192. Various success codes indicating the result.
  193. Failure:
  194. Various errors may occur. We return various error code to indicate such errors.
  195. Notes:
  196. This is just for testing.
  197. HRESULT
  198. CTranxManager::ParseXMLFile (
  199. IN LPCWSTR pszInput,
  200. IN LPCWSTR pszOutput,
  201. IN LPCWSTR pszArea,
  202. IN LPCWSTR pszElement,
  203. IN bool bSingleArea
  204. )
  205. {
  206. CComPtr<ISAXXMLReader> srpRdr;
  207. HRESULT hr = ::CoCreateInstance(
  208. CLSID_SAXXMLReader,
  209. NULL,
  210. CLSCTX_ALL,
  211. IID_ISAXXMLReader,
  212. (void **)&srpRdr);
  213. if (SUCCEEDED(hr))
  214. {
  215. CComObject<CXMLContent> * pXMLContent;
  216. hr = CComObject<CXMLContent>::CreateInstance(&pXMLContent);
  217. if (SUCCEEDED(hr))
  218. {
  219. CComPtr<ISAXContentHandler> srpHandler;
  220. pXMLContent->AddRef();
  221. if (S_OK == pXMLContent->QueryInterface(IID_ISAXContentHandler, (void**)&srpHandler))
  222. {
  223. hr = srpRdr->putContentHandler(srpHandler);
  224. }
  225. else
  226. {
  227. hr = WBEM_E_NOT_SUPPORTED;
  228. }
  229. if (SUCCEEDED(hr))
  230. {
  231. pXMLContent->SetOutputFile(pszOutput);
  232. pXMLContent->SetSection(pszArea, pszElement, bSingleArea);
  233. }
  234. pXMLContent->Release();
  235. //
  236. // CXMLErrorHandler * pEH;
  237. // hr = srpRdr->putErrorHandler(pDH);
  238. // CXMLDTDHandler * pDH;
  239. // hr = srpRdr->putDTDHandler(pDH);
  240. //
  241. if (SUCCEEDED(hr))
  242. {
  243. hr = srpRdr->parseURL(pszInput);
  244. //
  245. // we allow parseURL to fail because we may stop processing
  246. // in the middle of parsing XML.
  247. //
  248. if (FAILED(hr) && pXMLContent->ParseComplete())
  249. {
  250. hr = WBEM_S_FALSE;
  251. }
  252. }
  253. }
  254. }
  255. return SUCCEEDED(hr) ? WBEM_NO_ERROR : hr;
  256. }
  257. */
  258. /*
  259. //----------------------------------------------------------------------------------
  260. // Implementation for CFileStream
  261. //----------------------------------------------------------------------------------
  262. bool
  263. CFileStream::Open (
  264. IN LPCWSTR pszName,
  265. IN bool bRead = true
  266. )
  267. {
  268. this->m_bRead = bRead;
  269. long len;
  270. if (pszName == NULL)
  271. {
  272. m_hFile = GetStdHandle(STD_INPUT_HANDLE);
  273. }
  274. else
  275. {
  276. if (bRead)
  277. {
  278. m_hFile = ::CreateFile(
  279. pszName,
  280. GENERIC_READ,
  281. FILE_SHARE_READ,
  282. NULL,
  283. OPEN_EXISTING,
  284. FILE_ATTRIBUTE_NORMAL,
  285. NULL
  286. );
  287. }
  288. else
  289. {
  290. m_hFile = ::CreateFile(
  291. pszName,
  292. GENERIC_WRITE,
  293. FILE_SHARE_READ,
  294. NULL,
  295. CREATE_ALWAYS,
  296. FILE_ATTRIBUTE_NORMAL,
  297. NULL
  298. );
  299. }
  300. }
  301. return (m_hFile == INVALID_HANDLE_VALUE) ? false : true;
  302. }
  303. HRESULT
  304. CFileStream::Read (
  305. OUT void * pv,
  306. IN ULONG cb,
  307. OUT ULONG * pcbRead
  308. )
  309. {
  310. if (!read)
  311. {
  312. return E_FAIL;
  313. }
  314. DWORD len = 0;
  315. BOOL rc = ReadFile(
  316. m_hFile, // handle of file to read
  317. pv, // address of buffer that receives data
  318. cb, // number of bytes to read
  319. &len, // address of number of bytes read
  320. NULL // address of structure for data
  321. );
  322. *pcbRead = len;
  323. if (*pcbRead == 0)
  324. {
  325. return S_FALSE;
  326. }
  327. return (rc) ? S_OK : E_FAIL;
  328. }
  329. HRESULT
  330. CFileStream::Write (
  331. IN const void * pv,
  332. IN ULONG cb,
  333. OUT ULONG * pcbWritten
  334. )
  335. {
  336. if (read)
  337. {
  338. return E_FAIL;
  339. }
  340. BOOL rc = WriteFile(
  341. m_hFile, // handle of file to write
  342. pv, // address of buffer that contains data
  343. cb, // number of bytes to write
  344. pcbWritten, // address of number of bytes written
  345. NULL // address of structure for overlapped I/O
  346. );
  347. return (rc) ? S_OK : E_FAIL;
  348. }
  349. */