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.

565 lines
14 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved.
  3. Module Name:
  4. hsmmgdrc.cpp
  5. Abstract:
  6. Implementation of CHsmManagedResourceCollection
  7. Author:
  8. Cat Brant [cbrant] 24-Jan-1997
  9. Revision History:
  10. --*/
  11. #include "stdafx.h"
  12. #include "resource.h"
  13. #include "wsb.h"
  14. #include "HsmEng.h"
  15. #include "hsmserv.h"
  16. #include "hsmmgdrc.h"
  17. #include "fsa.h"
  18. #include "hsmconn.h"
  19. #define WSB_TRACE_IS WSB_TRACE_BIT_HSMENG
  20. HRESULT
  21. CHsmManagedResourceCollection::Add(
  22. IUnknown* pCollectable
  23. )
  24. /*++
  25. Implements:
  26. IWsbCollection::Add
  27. --*/
  28. {
  29. HRESULT hr = S_OK;
  30. WsbTraceIn(OLESTR("CHsmManagedResourceCollection::Add"), OLESTR(""));
  31. try {
  32. CComPtr<IHsmManagedResource> pHsmResource;
  33. CComPtr<IUnknown> pResourceUnknown;
  34. CComPtr<IFsaResource> pFsaResource;
  35. CComPtr<IHsmServer> pHsmServer;
  36. GUID hsmId;
  37. ULONG level;
  38. //
  39. // Contact the FSA Resource to tell it
  40. // that it is managed.
  41. //
  42. WsbAffirmHr(pCollectable->QueryInterface(IID_IHsmManagedResource,
  43. (void**)&pHsmResource));
  44. WsbAffirmHr(pHsmResource->GetFsaResource(&pResourceUnknown));
  45. WsbAffirmHr(pResourceUnknown->QueryInterface(IID_IFsaResource,
  46. (void**)&pFsaResource));
  47. WsbAffirmHr(pFsaResource->GetHsmLevel(&level));
  48. // this may have to change if HsmConn starts using the service id (second parameter)
  49. WsbAssertHr(HsmConnectFromId(HSMCONN_TYPE_HSM, GUID_NULL, IID_IHsmServer, (void**) &pHsmServer));
  50. WsbAffirmHr(pHsmServer->GetID(&hsmId));
  51. WsbAffirmHr(pFsaResource->ManagedBy(hsmId, level, FALSE));
  52. //
  53. // If FSA added OK add it to the engine
  54. //
  55. WsbAffirmHr(m_icoll->Add(pCollectable));
  56. } WsbCatch(hr);
  57. WsbTraceOut(OLESTR("CHsmManagedResourceCollection::Add"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  58. return(hr);
  59. }
  60. HRESULT
  61. CHsmManagedResourceCollection::DeleteAllAndRelease(
  62. void
  63. )
  64. /*++
  65. Implements:
  66. IWsbCollection::DeleteAllAndRelease().
  67. --*/
  68. {
  69. HRESULT hr = S_OK;
  70. WsbTraceIn(OLESTR("CHsmManagedResourceCollection::DeleteAllAndRelease"), OLESTR(""));
  71. Lock();
  72. try {
  73. // Release the resources without unmanaging them
  74. if (m_coll) {
  75. WsbAffirmHr(m_coll->RemoveAllAndRelease());
  76. }
  77. } WsbCatch(hr);
  78. Unlock();
  79. WsbTraceOut(OLESTR("CHsmManagedResourceCollection::DeleteAllAndRelease"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  80. return(hr);
  81. }
  82. HRESULT
  83. CHsmManagedResourceCollection::Remove(
  84. IUnknown* pCollectable,
  85. REFIID riid,
  86. void** ppElement
  87. )
  88. /*++
  89. Implements:
  90. IWsbCollection::Remove
  91. --*/
  92. {
  93. HRESULT hr = S_OK;
  94. WsbTraceIn(OLESTR("CHsmManagedResourceCollection::Remove"), OLESTR(""));
  95. try {
  96. CComPtr<IHsmManagedResource> pHsmResource;
  97. CComPtr<IUnknown> pResourceUnknown;
  98. CComPtr<IFsaResource> pFsaResource;
  99. CComPtr<IHsmServer> pHsmServer;
  100. GUID hsmId;
  101. ULONG level;
  102. // Contact the FSA Resource to tell it that it is no longer
  103. // managed.
  104. //
  105. WsbAffirmHr(pCollectable->QueryInterface(IID_IHsmManagedResource,
  106. (void**)&pHsmResource));
  107. WsbAffirmHr(pHsmResource->GetFsaResource(&pResourceUnknown));
  108. WsbAffirmHr(pResourceUnknown->QueryInterface(IID_IFsaResource,
  109. (void**)&pFsaResource));
  110. WsbAffirmHr(pFsaResource->GetHsmLevel(&level));
  111. // this may have to change if HsmConn starts using the service id (second parameter)
  112. WsbAssertHr(HsmConnectFromId(HSMCONN_TYPE_HSM, GUID_NULL, IID_IHsmServer, (void**) &pHsmServer));
  113. WsbAffirmHr(pHsmServer->GetID(&hsmId));
  114. //
  115. // We don't care if the resource complains that we
  116. // don't have it. Just tell the resource and
  117. // then delete it from our collection
  118. //
  119. (void)pFsaResource->ManagedBy(hsmId, level, TRUE);
  120. WsbAffirmHr(m_icoll->Remove(pCollectable, riid, ppElement));
  121. } WsbCatch(hr);
  122. WsbTraceOut(OLESTR("CHsmManagedResourceCollection::Remove"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  123. return(hr);
  124. }
  125. HRESULT
  126. CHsmManagedResourceCollection::RemoveAndRelease(
  127. IN IUnknown* pCollectable
  128. )
  129. /*++
  130. Implements:
  131. IHsmManagedResourceCollection::RemoveAndRelease().
  132. --*/
  133. {
  134. HRESULT hr = S_OK;
  135. WsbTraceIn(OLESTR("CHsmManagedResourceCollection::RemoveAndRelease"), OLESTR(""));
  136. try {
  137. WsbAssert(0 != pCollectable, E_POINTER);
  138. WsbAffirmHr(Remove(pCollectable, IID_IWsbCollectable, NULL));
  139. } WsbCatch(hr);
  140. WsbTraceOut(OLESTR("CHsmManagedResourceCollection::RemoveAndRelease"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  141. return(hr);
  142. }
  143. HRESULT
  144. CHsmManagedResourceCollection::RemoveAllAndRelease(
  145. void
  146. )
  147. /*++
  148. Implements:
  149. IWsbCollection::RemoveAllAndRelease().
  150. --*/
  151. {
  152. CComPtr<IWsbCollectable> pCollectable;
  153. CComPtr<IWsbEnum> pEnum;
  154. HRESULT hr = S_OK;
  155. WsbTraceIn(OLESTR("CHsmManagedResourceCollection::RemoveAllAndRelease"), OLESTR(""));
  156. Lock();
  157. try {
  158. // Get an enumerator
  159. WsbAffirmHr(Enum(&pEnum));
  160. // Start at the end of the list, and keep removing from the
  161. // back. For some types of collections, this may not be the most
  162. // efficient way to remove all the elements.
  163. for (hr = pEnum->Last(IID_IWsbCollectable, (void**) &pCollectable);
  164. SUCCEEDED(hr);
  165. hr = pEnum->Last(IID_IWsbCollectable, (void**) &pCollectable)) {
  166. hr = RemoveAndRelease(pCollectable);
  167. pCollectable = 0;
  168. }
  169. // We should have emptied the list.
  170. if (hr == WSB_E_NOTFOUND) {
  171. hr = S_OK;
  172. }
  173. } WsbCatch(hr);
  174. Unlock();
  175. WsbTraceOut(OLESTR("CHsmManagedResourceCollection::RemoveAllAndRelease"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  176. return(hr);
  177. }
  178. HRESULT
  179. CHsmManagedResourceCollection::FinalConstruct(
  180. void
  181. )
  182. /*++
  183. Implements:
  184. CComObjectRoot::FinalConstruct().
  185. --*/
  186. {
  187. HRESULT hr = S_OK;
  188. WsbTraceIn(OLESTR("CHsmManagedResourceCollection::FinalConstruct"), OLESTR(""));
  189. try {
  190. WsbAffirmHr(CWsbPersistStream::FinalConstruct());
  191. WsbAssertHr(CoCreateInstance(CLSID_CWsbOrderedCollection, NULL, CLSCTX_ALL,
  192. IID_IWsbIndexedCollection, (void**) &m_icoll));
  193. WsbAssertHr(m_icoll->QueryInterface(IID_IWsbCollection,
  194. (void**)&m_coll));
  195. } WsbCatch(hr);
  196. WsbTraceOut(OLESTR("CHsmManagedResourceCollection::FinalConstruct"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  197. return(hr);
  198. }
  199. void CHsmManagedResourceCollection::FinalRelease(
  200. )
  201. {
  202. WsbTraceIn(OLESTR("CHsmManagedResourceCollection::FinalRelease"), OLESTR(""));
  203. // Force a release of the resources
  204. if (m_coll) {
  205. m_coll->RemoveAllAndRelease();
  206. }
  207. // Let the parent class do his thing.
  208. CWsbPersistStream::FinalRelease();
  209. WsbTraceOut(OLESTR("CHsmManagedResourceCollection::FinalRelease"), OLESTR(""));
  210. }
  211. HRESULT
  212. CHsmManagedResourceCollection::GetClassID(
  213. OUT CLSID* pClsid
  214. )
  215. /*++
  216. Implements:
  217. IPersist::GetClassID().
  218. --*/
  219. {
  220. HRESULT hr = S_OK;
  221. WsbTraceIn(OLESTR("CHsmManagedResourceCollection::GetClassID"), OLESTR(""));
  222. try {
  223. WsbAssert(0 != pClsid, E_POINTER);
  224. *pClsid = CLSID_CHsmManagedResourceCollection;
  225. } WsbCatch(hr);
  226. WsbTraceOut(OLESTR("CHsmManagedResourceCollection::GetClassID"), OLESTR("hr = <%ls>, CLSID = <%ls>"), WsbHrAsString(hr), WsbGuidAsString(*pClsid));
  227. return(hr);
  228. }
  229. HRESULT
  230. CHsmManagedResourceCollection::GetSizeMax(
  231. OUT ULARGE_INTEGER* pSize
  232. )
  233. /*++
  234. Implements:
  235. IPersistStream::GetSizeMax().
  236. --*/
  237. {
  238. HRESULT hr = S_OK;
  239. WsbTraceIn(OLESTR("CHsmManagedResourceCollection::GetSizeMax"), OLESTR(""));
  240. try {
  241. CComPtr<IPersistStream> pPStream;
  242. WsbAffirmHr(m_icoll->QueryInterface(IID_IPersistStream,
  243. (void**)&pPStream));
  244. WsbAffirmHr(pPStream->GetSizeMax(pSize));
  245. } WsbCatch(hr);
  246. WsbTraceOut(OLESTR("CHsmManagedResourceCollection::GetSizeMax"), OLESTR("hr = <%ls>, Size = <%ls>"), WsbHrAsString(hr), WsbPtrToUliAsString(pSize));
  247. return(hr);
  248. }
  249. HRESULT
  250. CHsmManagedResourceCollection::Load(
  251. IN IStream* pStream
  252. )
  253. /*++
  254. Implements:
  255. IPersistStream::Load().
  256. --*/
  257. {
  258. HRESULT hr = S_OK;
  259. WsbTraceIn(OLESTR("CHsmManagedResourceCollection::Load"), OLESTR(""));
  260. try {
  261. CComPtr<IPersistStream> pPStream;
  262. WsbAffirmHr(m_icoll->QueryInterface(IID_IPersistStream,
  263. (void**)&pPStream));
  264. WsbAffirmHr(pPStream->Load(pStream));
  265. } WsbCatch(hr);
  266. WsbTraceOut(OLESTR("CHsmManagedResourceCollection::Load"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  267. return(hr);
  268. }
  269. HRESULT
  270. CHsmManagedResourceCollection::Save(
  271. IN IStream* pStream,
  272. IN BOOL clearDirty
  273. )
  274. /*++
  275. Implements:
  276. IPersistStream::Save().
  277. --*/
  278. {
  279. HRESULT hr = S_OK;
  280. WsbTraceIn(OLESTR("CHsmManagedResourceCollection::Save"), OLESTR("clearDirty = <%ls>"), WsbBoolAsString(clearDirty));
  281. try {
  282. CComPtr<IPersistStream> pPStream;
  283. WsbAffirmHr(m_icoll->QueryInterface(IID_IPersistStream,
  284. (void**)&pPStream));
  285. WsbAffirmHr(pPStream->Save(pStream, clearDirty));
  286. } WsbCatch(hr);
  287. WsbTraceOut(OLESTR("CHsmManagedResourceCollection::Save"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  288. return(hr);
  289. }
  290. HRESULT
  291. CHsmManagedResourceCollection::Test(
  292. OUT USHORT* passed,
  293. OUT USHORT* failed
  294. )
  295. /*++
  296. Implements:
  297. IWsbTestable::Test().
  298. --*/
  299. {
  300. *passed = 0;
  301. *failed = 0;
  302. HRESULT hr = S_OK;
  303. #if defined(_DEBUG)
  304. WsbTraceIn(OLESTR("CHsmManagedResourceCollection::Test"), OLESTR(""));
  305. try {
  306. ULONG entries;
  307. CComPtr<IWsbLong> pLong1;
  308. CComPtr<IWsbLong> pLong2;
  309. CComPtr<IWsbLong> pLong3;
  310. CComPtr<IWsbLong> pLong4;
  311. hr = S_OK;
  312. // Check that collection is empty
  313. try {
  314. WsbAssertHr(GetEntries(&entries));
  315. WsbAssert(entries == 0, E_FAIL);
  316. } WsbCatch(hr);
  317. if (hr == S_OK) {
  318. (*passed)++;
  319. } else {
  320. (*failed)++;
  321. }
  322. // Add some elements to the collection
  323. WsbAssertHr(CoCreateInstance(CLSID_CWsbLong, NULL, CLSCTX_ALL,
  324. IID_IWsbLong, (void**) &pLong1));
  325. WsbAssertHr(CoCreateInstance(CLSID_CWsbLong, NULL, CLSCTX_ALL,
  326. IID_IWsbLong, (void**) &pLong2));
  327. WsbAssertHr(CoCreateInstance(CLSID_CWsbLong, NULL, CLSCTX_ALL,
  328. IID_IWsbLong, (void**) &pLong3));
  329. WsbAssertHr(CoCreateInstance(CLSID_CWsbLong, NULL, CLSCTX_ALL,
  330. IID_IWsbLong, (void**) &pLong4));
  331. WsbAssertHr(pLong1->SetLong(57));
  332. WsbAssertHr(pLong2->SetLong(-48));
  333. WsbAssertHr(pLong3->SetLong(23));
  334. WsbAssertHr(pLong4->SetLong(187));
  335. try {
  336. WsbAssertHr(Add(pLong1));
  337. WsbAssertHr(Add(pLong2));
  338. WsbAssertHr(Add(pLong3));
  339. WsbAssertHr(Add(pLong4));
  340. WsbAssertHr(GetEntries(&entries));
  341. WsbAssert(entries == 4, E_FAIL);
  342. } WsbCatch(hr);
  343. if (hr == S_OK) {
  344. (*passed)++;
  345. } else {
  346. (*failed)++;
  347. }
  348. // Check the order
  349. /* try {
  350. ULONG fetched;
  351. int i;
  352. CComPtr<IWsbEnum> pEnum;
  353. CComPtr<IWsbLong> pLong[5];
  354. LONG value[4];
  355. WsbAssertHr(Enum(&pEnum));
  356. WsbAssertHr(pEnum->First(5, IID_IWsbLong, (void**)&pLong,
  357. &fetched));
  358. WsbAssert(fetched == 4, E_FAIL);
  359. for (i = 0; i < 4; i++) {
  360. WsbAssertHr(pLong[i]->GetLong(&value[i]));
  361. }
  362. for (i = 0; i < 3; i++) {
  363. WsbAssert(value[i] < value[i+1], E_FAIL);
  364. }
  365. } WsbCatch(hr);
  366. */
  367. if (hr == S_OK) {
  368. (*passed)++;
  369. } else {
  370. (*failed)++;
  371. }
  372. // Save/load
  373. try {
  374. CComPtr<IPersistFile> pFile;
  375. CComPtr<IWsbCollection> pSorted2;
  376. WsbAssertHr(((IUnknown*)(IWsbPersistStream*)this)->QueryInterface(IID_IPersistFile,
  377. (void**) &pFile));
  378. WsbAssertHr(pFile->Save(OLESTR("c:\\WsbTests\\WsbSorted.tst"), TRUE));
  379. pFile = 0;
  380. WsbAssertHr(CoCreateInstance(CLSID_CHsmManagedResourceCollection, NULL,
  381. CLSCTX_ALL, IID_IPersistFile, (void**) &pFile));
  382. WsbAssertHr(pFile->Load(OLESTR("c:\\WsbTests\\WsbSorted.tst"), 0));
  383. WsbAssertHr(pFile->QueryInterface(IID_IWsbCollection,
  384. (void**) &pSorted2));
  385. WsbAssertHr(pSorted2->GetEntries(&entries));
  386. WsbAssert(entries == 4, E_FAIL);
  387. } WsbCatch(hr);
  388. if (hr == S_OK) {
  389. (*passed)++;
  390. } else {
  391. (*failed)++;
  392. }
  393. } WsbCatch(hr);
  394. // Tally up the results
  395. if (*failed) {
  396. hr = S_FALSE;
  397. } else {
  398. hr = S_OK;
  399. }
  400. WsbTraceOut(OLESTR("CHsmManagedResourceCollection::Test"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  401. #endif // _DEBUG
  402. return(hr);
  403. }