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.

653 lines
19 KiB

  1. // This is a part of the Microsoft Management Console.
  2. // Copyright (C) Microsoft Corporation, 1995 - 1999
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Management Console and related
  7. // electronic documentation provided with the interfaces.
  8. #include "stdafx.h"
  9. #define __dwFILE__ __dwFILE_CAPESNPN_EVENTS_CPP__
  10. /////////////////////////////////////////////////////////////////////////////
  11. // Event handlers for IFrame::Notify
  12. HRESULT CSnapin::OnFolder(MMC_COOKIE cookie, LPARAM arg, LPARAM param)
  13. {
  14. ASSERT(FALSE);
  15. return S_OK;
  16. }
  17. HRESULT CSnapin::OnAddImages(MMC_COOKIE cookie, LPARAM arg, LPARAM param)
  18. {
  19. if (arg == 0)
  20. return E_INVALIDARG;
  21. // if cookie is from a different snapin
  22. // if (IsMyCookie(cookie) == FALSE)
  23. if (0)
  24. {
  25. // add the images for the scope tree only
  26. ::CBitmap bmp16x16;
  27. ::CBitmap bmp32x32;
  28. LPIMAGELIST lpImageList = reinterpret_cast<LPIMAGELIST>(arg);
  29. // Load the bitmaps from the dll
  30. bmp16x16.LoadBitmap(IDB_16x16);
  31. bmp32x32.LoadBitmap(IDB_32x32);
  32. // Set the images
  33. lpImageList->ImageListSetStrip(
  34. reinterpret_cast<LONG_PTR*>(static_cast<HBITMAP>(bmp16x16)),
  35. reinterpret_cast<LONG_PTR*>(static_cast<HBITMAP>(bmp32x32)),
  36. 0, RGB(255, 0, 255));
  37. lpImageList->Release();
  38. }
  39. else
  40. {
  41. ASSERT(m_pImageResult != NULL);
  42. ::CBitmap bmp16x16;
  43. ::CBitmap bmp32x32;
  44. // Load the bitmaps from the dll
  45. bmp16x16.LoadBitmap(IDB_16x16);
  46. bmp32x32.LoadBitmap(IDB_32x32);
  47. // Set the images
  48. m_pImageResult->ImageListSetStrip(reinterpret_cast<LONG_PTR*>(static_cast<HBITMAP>(bmp16x16)),
  49. reinterpret_cast<LONG_PTR*>(static_cast<HBITMAP>(bmp32x32)),
  50. 0, RGB(255, 0, 255));
  51. }
  52. return S_OK;
  53. }
  54. typedef IMessageView *LPMESSAGEVIEW;
  55. HRESULT CSnapin::OnShow(MMC_COOKIE cookie, LPARAM arg, LPARAM param)
  56. {
  57. CComponentDataImpl* pComp = dynamic_cast<CComponentDataImpl*>(m_pComponentData);
  58. CFolder* pFolder = pComp->FindObject(cookie);
  59. if ((cookie == NULL) || (pFolder == NULL))
  60. {
  61. return S_OK;
  62. }
  63. // Note - arg is TRUE when it is time to enumerate
  64. if (arg == TRUE)
  65. {
  66. m_pCurrentlySelectedScopeFolder = pFolder;
  67. // if list view on display
  68. if (m_CustomViewID == VIEW_DEFAULT_LV)
  69. {
  70. // Show the headers for this nodetype
  71. if (S_OK != InitializeHeaders(cookie))
  72. {
  73. // UNDONE: add informative "server down" result object
  74. goto done;
  75. }
  76. return Enumerate(cookie, param);
  77. }
  78. else if (m_CustomViewID == VIEW_ERROR_OCX)
  79. {
  80. HRESULT hr;
  81. CString strMessage;
  82. strMessage.LoadString(IDS_ERROR_CANNOT_LOAD_TEMPLATES);
  83. LPUNKNOWN pUnk = NULL;
  84. LPMESSAGEVIEW pMessageView = NULL;
  85. hr = m_pConsole->QueryResultView(&pUnk);
  86. _JumpIfError(hr, done, "QueryResultView IUnk");
  87. hr = pUnk->QueryInterface(IID_IMessageView, reinterpret_cast<void**>(&pMessageView));
  88. _JumpIfError(hr, done, "IID_IMessageView");
  89. pMessageView->SetIcon(Icon_Error);
  90. CAutoLPWSTR pwszErrorCode = BuildErrorMessage(pComp->GetCreateFolderHRESULT());
  91. pMessageView->SetTitleText(strMessage);
  92. pMessageView->SetBodyText(pwszErrorCode);
  93. pUnk->Release();
  94. pMessageView->Release();
  95. }
  96. }
  97. else
  98. {
  99. m_pCurrentlySelectedScopeFolder = NULL;
  100. // if list view is on display
  101. if (m_CustomViewID == VIEW_DEFAULT_LV)
  102. {
  103. RemoveResultItems(cookie);
  104. }
  105. // Free data associated with the result pane items, because
  106. // your node is no longer being displayed.
  107. // Note: The console will remove the items from the result pane
  108. }
  109. done:
  110. return S_OK;
  111. }
  112. HRESULT CSnapin::OnDelete(LPDATAOBJECT pDataObject, LPARAM arg, LPARAM param)
  113. {
  114. HRESULT hr;
  115. WCHAR ** aszCertTypesCurrentlySupported;
  116. WCHAR ** aszNewCertTypesSupported;
  117. WCHAR ** aszNameOfDeleteType;
  118. HRESULTITEM itemID;
  119. HWND hwndConsole;
  120. CTemplateList TemplateList;
  121. CString cstrMsg, cstrTitle;
  122. cstrMsg.LoadString(IDS_ASK_CONFIRM_DELETETEMPLATES);
  123. cstrTitle.LoadString(IDS_TITLE_ASK_CONFIRM_DELETETEMPLATES);
  124. HWND hwndMain = NULL;
  125. m_pConsole->GetMainWindow(&hwndMain);
  126. if (IDYES != MessageBox(
  127. hwndMain,
  128. (LPCWSTR)cstrMsg,
  129. (LPCWSTR)cstrTitle,
  130. MB_YESNO))
  131. return ERROR_CANCELLED;
  132. CWaitCursor hourglass;
  133. ASSERT(m_pResult != NULL); // make sure we QI'ed for the interface
  134. ASSERT(m_pComponentData != NULL);
  135. if(!IsMMCMultiSelectDataObject(pDataObject))
  136. {
  137. INTERNAL * pInternal = ExtractInternalFormat(pDataObject);
  138. if ((pInternal == NULL) ||
  139. (pInternal->m_cookie == NULL))
  140. {
  141. //ASSERT(FALSE);
  142. return S_OK;
  143. }
  144. CFolder* pFolder = reinterpret_cast<CFolder*>(pInternal->m_cookie);
  145. hr = myRetrieveCATemplateList(pFolder->m_hCAInfo, FALSE, TemplateList);
  146. if (FAILED(hr))
  147. {
  148. m_pConsole->GetMainWindow(&hwndConsole);
  149. MyErrorBox(hwndConsole, IDS_CERTTYPE_INFO_FAIL ,IDS_SNAPIN_NAME, hr);
  150. return hr;
  151. }
  152. ASSERT(pFolder != NULL);
  153. ASSERT(pFolder->m_hCertType != NULL);
  154. FOLDER_TYPES type = pFolder->GetType();
  155. if(type == CA_CERT_TYPE)
  156. {
  157. hr = myRemoveFromCATemplateList(
  158. pFolder->m_hCAInfo,
  159. TemplateList,
  160. pFolder->m_hCertType);
  161. if (FAILED(hr))
  162. {
  163. m_pConsole->GetMainWindow(&hwndConsole);
  164. MyErrorBox(hwndConsole, IDS_CERTTYPE_INFO_FAIL ,IDS_SNAPIN_NAME, hr);
  165. return hr;
  166. }
  167. m_pConsole->GetMainWindow(&hwndConsole);
  168. hr = UpdateCATemplateList(
  169. hwndConsole,
  170. pFolder->m_hCAInfo,
  171. TemplateList);
  172. if (S_OK!=hr)
  173. {
  174. if ((HRESULT) ERROR_CANCELLED != hr)
  175. {
  176. MyErrorBox(hwndConsole, IDS_DELETE_ERROR ,IDS_SNAPIN_NAME, hr);
  177. }
  178. else
  179. {
  180. // user canceled removing it, add it back
  181. hr = myAddToCATemplateList(
  182. pFolder->m_hCAInfo,
  183. TemplateList,
  184. pFolder->m_hCertType,
  185. FALSE);
  186. _PrintIfError(hr, "myAddToCATemplatelist");
  187. }
  188. return hr;
  189. }
  190. hr = m_pResult->FindItemByLParam ((LPARAM)pFolder, &itemID);
  191. hr = m_pResult->DeleteItem (itemID, 0);
  192. delete(pFolder);
  193. }
  194. }
  195. else
  196. {
  197. // Is multiple select, get all selected items and paste each one
  198. MMC_COOKIE currentCookie = NULL;
  199. HCAINFO hCAInfo = NULL;
  200. CDataObject* pDO = dynamic_cast <CDataObject*>(pDataObject);
  201. ASSERT (pDO);
  202. if ( pDO )
  203. {
  204. INT i;
  205. bool fTemplateListRetrieved = false;
  206. for(i=pDO->QueryCookieCount()-1; i >= 0; i--)
  207. {
  208. hr = pDO->GetCookieAt(i, &currentCookie);
  209. if(hr != S_OK)
  210. {
  211. return hr;
  212. }
  213. CFolder* pFolder = reinterpret_cast<CFolder*>(currentCookie);
  214. if(!fTemplateListRetrieved)
  215. {
  216. hr = myRetrieveCATemplateList(
  217. pFolder->m_hCAInfo,
  218. FALSE,
  219. TemplateList);
  220. if (FAILED(hr))
  221. {
  222. m_pConsole->GetMainWindow(&hwndConsole);
  223. MyErrorBox(hwndConsole, IDS_CERTTYPE_INFO_FAIL ,IDS_SNAPIN_NAME, hr);
  224. return hr;
  225. }
  226. fTemplateListRetrieved = true;
  227. }
  228. ASSERT(pFolder != NULL);
  229. ASSERT(pFolder->m_hCertType != NULL);
  230. FOLDER_TYPES type = pFolder->GetType();
  231. if(type == CA_CERT_TYPE)
  232. {
  233. if(hCAInfo == NULL)
  234. {
  235. // Grab the CA this type belongs to.
  236. hCAInfo = pFolder->m_hCAInfo;
  237. }
  238. hr = myRemoveFromCATemplateList(
  239. hCAInfo,
  240. TemplateList,
  241. pFolder->m_hCertType);
  242. if (FAILED(hr))
  243. {
  244. m_pConsole->GetMainWindow(&hwndConsole);
  245. MyErrorBox(hwndConsole, IDS_CERTTYPE_INFO_FAIL ,IDS_SNAPIN_NAME, hr);
  246. return hr;
  247. }
  248. }
  249. }
  250. if(hCAInfo)
  251. {
  252. m_pConsole->GetMainWindow(&hwndConsole);
  253. hr = UpdateCATemplateList(
  254. hwndConsole,
  255. hCAInfo,
  256. TemplateList);
  257. if (S_OK != hr)
  258. {
  259. if ((HRESULT) ERROR_CANCELLED != hr)
  260. {
  261. MyErrorBox(hwndConsole, IDS_DELETE_ERROR ,IDS_SNAPIN_NAME, hr);
  262. }
  263. else
  264. {
  265. // user canceled, add the templates back
  266. for(i=pDO->QueryCookieCount()-1; i >= 0; i--)
  267. {
  268. hr = pDO->GetCookieAt(i, &currentCookie);
  269. if(hr != S_OK)
  270. {
  271. return hr;
  272. }
  273. CFolder* pFolder = reinterpret_cast<CFolder*>(currentCookie);
  274. FOLDER_TYPES type = pFolder->GetType();
  275. if(type == CA_CERT_TYPE)
  276. {
  277. hr = myAddToCATemplateList(
  278. hCAInfo,
  279. TemplateList,
  280. pFolder->m_hCertType,
  281. FALSE);
  282. _PrintIfError(hr, "myAddToCATemplateList");
  283. if(S_OK != hr)
  284. {
  285. return hr;
  286. }
  287. }
  288. }
  289. }
  290. }
  291. else
  292. {
  293. for(i=pDO->QueryCookieCount()-1; i >= 0; i--)
  294. {
  295. hr = pDO->GetCookieAt(i, &currentCookie);
  296. if(hr != S_OK)
  297. {
  298. return hr;
  299. }
  300. CFolder* pFolder = reinterpret_cast<CFolder*>(currentCookie);
  301. FOLDER_TYPES type = pFolder->GetType();
  302. if(type == CA_CERT_TYPE)
  303. {
  304. hr = m_pResult->FindItemByLParam ((LPARAM)pFolder, &itemID);
  305. if(hr != S_OK)
  306. {
  307. return hr;
  308. }
  309. // We must delete the actual CFolder backing this item if we do this
  310. // or we leak.
  311. hr = m_pResult->DeleteItem (itemID, 0);
  312. if(hr != S_OK)
  313. {
  314. return hr;
  315. }
  316. hr = pDO->RemoveCookieAt(i);
  317. if(hr != S_OK)
  318. {
  319. return hr;
  320. }
  321. // Note, since this is a type folder, the CAInfo will not be closed on delete
  322. delete pFolder;
  323. }
  324. }
  325. }
  326. }
  327. }
  328. }
  329. return S_OK;
  330. }
  331. HRESULT CSnapin::OnActivate(MMC_COOKIE cookie, LPARAM arg, LPARAM param)
  332. {
  333. return S_OK;
  334. }
  335. HRESULT CSnapin::OnResultItemClk(DATA_OBJECT_TYPES type, MMC_COOKIE cookie)
  336. {
  337. RESULT_DATA* pResult;
  338. DWORD* pdw = reinterpret_cast<DWORD*>(cookie);
  339. if (*pdw == RESULT_ITEM)
  340. {
  341. pResult = reinterpret_cast<RESULT_DATA*>(cookie);
  342. }
  343. return S_OK;
  344. }
  345. HRESULT CSnapin::OnMinimize(MMC_COOKIE cookie, LPARAM arg, LPARAM param)
  346. {
  347. return S_OK;
  348. }
  349. HRESULT CSnapin::OnPropertyChange(LPDATAOBJECT lpDataObject)
  350. {
  351. return S_OK;
  352. }
  353. HRESULT CSnapin::Enumerate(MMC_COOKIE cookie, HSCOPEITEM pParent)
  354. {
  355. return EnumerateResultPane(cookie);
  356. }
  357. HRESULT CSnapin::EnumerateResultPane(MMC_COOKIE cookie)
  358. {
  359. ASSERT(m_pResult != NULL); // make sure we QI'ed for the interface
  360. ASSERT(m_pComponentData != NULL);
  361. // don't bother enumerating base
  362. if (cookie == NULL)
  363. return S_FALSE;
  364. // Our static folders must be displayed in the result pane
  365. // by use because the console doesn't do it.
  366. CFolder* pFolder = dynamic_cast<CComponentDataImpl*>(m_pComponentData)->FindObject(cookie);
  367. // The dynamic folder must be in our list
  368. ASSERT(pFolder != NULL);
  369. FOLDER_TYPES type = pFolder->GetType();
  370. switch(type)
  371. {
  372. case STATIC:
  373. break;
  374. case POLICYSETTINGS:
  375. return AddCACertTypesToResults(pFolder);
  376. break;
  377. default:
  378. break;
  379. }
  380. return S_FALSE;
  381. }
  382. void CSnapin::RemoveResultItems(MMC_COOKIE cookie)
  383. {
  384. if (cookie == NULL)
  385. return;
  386. // Our static folders must be displayed in the result pane
  387. // by use because the console doesn't do it.
  388. CFolder* pFolder = dynamic_cast<CComponentDataImpl*>(m_pComponentData)->FindObject(cookie);
  389. // The dynamic folder must be in our list
  390. ASSERT(pFolder != NULL);
  391. FOLDER_TYPES type = pFolder->GetType();
  392. RESULTDATAITEM resultItem;
  393. ZeroMemory(&resultItem, sizeof(RESULTDATAITEM));
  394. // look for first rdi by index
  395. resultItem.mask = RDI_INDEX | RDI_PARAM; // fill in index & param
  396. resultItem.nIndex = 0;
  397. switch (type)
  398. {
  399. case POLICYSETTINGS:
  400. case SCE_EXTENSION:
  401. while (S_OK == m_pResult->GetItem(&resultItem))
  402. {
  403. CFolder* pResult = reinterpret_cast<CFolder*>(resultItem.lParam);
  404. resultItem.lParam = NULL;
  405. delete pResult;
  406. // next item
  407. resultItem.nIndex++;
  408. }
  409. break;
  410. default:
  411. break;
  412. }
  413. return;
  414. }
  415. HRESULT CSnapin::AddCACertTypesToResults(CFolder* pFolder)
  416. {
  417. HRESULT hr = S_OK;
  418. CFolder *pNewFolder;
  419. WCHAR ** aszCertTypeName;
  420. HWND hwndConsole;
  421. BOOL fMachine = FALSE;
  422. CTemplateList CATemplateList;
  423. CTemplateListEnum CATemplateListEnum(CATemplateList);
  424. CTemplateInfo *pTemplateInfo;
  425. HCERTTYPE hCertType;
  426. bool fNoCacheLookup = true;
  427. CWaitCursor WaitCursor;
  428. m_pConsole->GetMainWindow(&hwndConsole);
  429. hr = myRetrieveCATemplateList(pFolder->m_hCAInfo, FALSE, CATemplateList);
  430. if(FAILED(hr))
  431. {
  432. m_CustomViewID = VIEW_ERROR_OCX; // change the view type
  433. m_pConsole->SelectScopeItem(m_pCurrentlySelectedScopeFolder->m_pScopeItem->ID); // select this node again
  434. // done, let's bail and let the error page do its job
  435. return S_OK;
  436. }
  437. CATemplateListEnum.Reset();
  438. for(pTemplateInfo=CATemplateListEnum.Next();
  439. pTemplateInfo;
  440. pTemplateInfo=CATemplateListEnum.Next())
  441. {
  442. CString strCert;
  443. hCertType = pTemplateInfo->GetCertType();
  444. if(!hCertType)
  445. {
  446. CSASSERT(pTemplateInfo->GetName());
  447. hr = CAFindCertTypeByName(
  448. pTemplateInfo->GetName(),
  449. NULL,
  450. CT_ENUM_MACHINE_TYPES |
  451. CT_ENUM_USER_TYPES |
  452. (fNoCacheLookup?CT_FLAG_NO_CACHE_LOOKUP:0),
  453. &hCertType);
  454. fNoCacheLookup = false;
  455. if(FAILED(hr))
  456. {
  457. // continue on errors
  458. strCert = pTemplateInfo->GetName()?
  459. pTemplateInfo->GetName():pTemplateInfo->GetOID();
  460. }
  461. }
  462. if(hCertType)
  463. {
  464. hr = CAGetCertTypeProperty(
  465. hCertType,
  466. CERTTYPE_PROP_FRIENDLY_NAME,
  467. &aszCertTypeName);
  468. if (FAILED(hr) || (aszCertTypeName == NULL))
  469. {
  470. strCert = pTemplateInfo->GetName()?
  471. pTemplateInfo->GetName():pTemplateInfo->GetOID();
  472. }
  473. else
  474. {
  475. strCert = aszCertTypeName[0];
  476. }
  477. }
  478. pNewFolder = new CFolder();
  479. _JumpIfAllocFailed(pNewFolder, error);
  480. if(strCert.IsEmpty())
  481. strCert.LoadString(IDS_UNKNOWN);
  482. pNewFolder->Create(
  483. strCert,
  484. IMGINDEX_CERTTYPE,
  485. IMGINDEX_CERTTYPE,
  486. RESULT_ITEM,
  487. CA_CERT_TYPE,
  488. FALSE);
  489. pNewFolder->m_dwRoles = pFolder->GetRoles();
  490. if(aszCertTypeName)
  491. {
  492. CAFreeCertTypeProperty(
  493. hCertType,
  494. aszCertTypeName);
  495. }
  496. //
  497. // get the key usage string
  498. //
  499. if(hCertType)
  500. {
  501. GetIntendedUsagesString(hCertType, &(pNewFolder->m_szIntendedUsages));
  502. if (pNewFolder->m_szIntendedUsages == L"")
  503. {
  504. pNewFolder->m_szIntendedUsages.LoadString(IDS_ALL);
  505. }
  506. pNewFolder->m_hCAInfo = pFolder->m_hCAInfo;
  507. }
  508. else
  509. {
  510. pNewFolder->m_szIntendedUsages.LoadString(IDS_UNKNOWN);
  511. pNewFolder->m_hCAInfo = NULL;
  512. }
  513. pNewFolder->m_hCertType = hCertType;
  514. RESULTDATAITEM resultItem;
  515. ZeroMemory(&resultItem, sizeof(RESULTDATAITEM));
  516. resultItem.mask = RDI_STR | RDI_IMAGE | RDI_PARAM;
  517. resultItem.bScopeItem = FALSE;
  518. resultItem.itemID = (LONG_PTR) pNewFolder;
  519. resultItem.str = MMC_CALLBACK;
  520. resultItem.nImage = hCertType?IMGINDEX_CERTTYPE:IMGINDEX_UNKNOWNCERT;
  521. resultItem.lParam = reinterpret_cast<LPARAM>(pNewFolder);
  522. // add to result pane
  523. resultItem.nCol = 0;
  524. hr = m_pResult->InsertItem(&resultItem);
  525. _JumpIfError(hr, error, "InsertItem");
  526. }
  527. error:
  528. return hr;
  529. }