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.

1848 lines
63 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997-2002.
  5. //
  6. // File: cmponent.cpp
  7. //
  8. // Contents: Implementation of CCertTmplComponent
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include "dbg.h"
  13. #include "compdata.h" // CCertTmplComponentData
  14. #include "dataobj.h"
  15. #include "cmponent.h" // CCertTmplComponent
  16. #include "utils.h"
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. DECLARE_INFOLEVEL(CertTmplSnapin)
  23. USE_HANDLE_MACROS ("CERTTMPL (cmponent.cpp)")
  24. #pragma warning(push,3)
  25. #include "stdcmpnt.cpp" // CComponent
  26. #pragma warning(pop)
  27. UINT m_aColumnsSnapinSelected[CERT_TEMPLATES_NUM_COLS+1] =
  28. {IDS_COLUMN_CERT_TEMPLATE_OBJECT,
  29. IDS_COLUMN_CERT_TEMPLATE_TYPE,
  30. IDS_COLUMN_CERT_TEMPLATE_VERSION,
  31. IDS_COLUMN_CERT_TEMPLATE_AUTOENROLLMENT,
  32. 0};
  33. UINT* m_Columns[CERTTMPL_NUMTYPES] =
  34. {
  35. m_aColumnsSnapinSelected, // CERTTMPL_SNAPIN (displays certificate templates in the result pane)
  36. 0
  37. };
  38. UINT** g_aColumns = 0; // for framework
  39. int** g_aColumnWidths = 0; // for framework
  40. const int SINGLE_COL_WIDTH = 300;
  41. extern bool g_bDomainIsPresent;
  42. CCertTmplComponent::CCertTmplComponent ()
  43. : m_pViewedCookie (NULL),
  44. m_currResultNodeType (CERTTMPL_INVALID),
  45. m_nSortColumn (COLNUM_CERT_TEMPLATE_OBJECT)
  46. {
  47. _TRACE (1, L"Entering CCertTmplComponent::CCertTmplComponent\n");
  48. AFX_MANAGE_STATE (AfxGetStaticModuleState ( ));
  49. // security review BryanWal 02/20/2002 ok
  50. ::ZeroMemory (m_ColumnWidths, sizeof (m_ColumnWidths));
  51. m_ColumnWidths[CERTTMPL_SNAPIN] = new UINT[CERT_TEMPLATES_NUM_COLS];
  52. if ( m_ColumnWidths[CERTTMPL_SNAPIN] )
  53. {
  54. m_ColumnWidths[CERTTMPL_SNAPIN][COLNUM_CERT_TEMPLATE_OBJECT] = 250;
  55. m_ColumnWidths[CERTTMPL_SNAPIN][COLNUM_CERT_TEMPLATE_TYPE] = 150;
  56. m_ColumnWidths[CERTTMPL_SNAPIN][COLNUM_CERT_TEMPLATE_VERSION] = 50;
  57. m_ColumnWidths[CERTTMPL_SNAPIN][COLNUM_CERT_TEMPLATE_AUTOENROLL_STATUS] = 100;
  58. }
  59. _TRACE (-1, L"Leaving CCertTmplComponent::CCertTmplComponent\n");
  60. }
  61. CCertTmplComponent::~CCertTmplComponent ()
  62. {
  63. _TRACE (1, L"Entering CCertTmplComponent::~CCertTmplComponent\n");
  64. VERIFY ( SUCCEEDED (ReleaseAll ()) );
  65. for (int i = 0; i < CERTTMPL_NUMTYPES; i++)
  66. {
  67. if ( m_ColumnWidths[i] )
  68. delete [] m_ColumnWidths[i];
  69. }
  70. _TRACE (-1, L"Leaving CCertTmplComponent::~CCertTmplComponent\n");
  71. }
  72. HRESULT CCertTmplComponent::ReleaseAll ()
  73. {
  74. return CComponent::ReleaseAll ();
  75. }
  76. /////////////////////////////////////////////////////////////////////////////
  77. // IComponent Implementation
  78. HRESULT CCertTmplComponent::LoadStrings ()
  79. {
  80. return S_OK;
  81. }
  82. HRESULT CCertTmplComponent::LoadColumns ( CCertTmplCookie* pcookie )
  83. {
  84. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  85. _TRACE (1, L"Entering CCertTmplComponent::LoadColumns\n");
  86. HRESULT hr = S_OK;
  87. hr = LoadColumnsFromArrays (pcookie->m_objecttype);
  88. _TRACE (-1, L"Leaving CCertTmplComponent::LoadColumns\n");
  89. return hr;
  90. }
  91. /* This is generated by UpdateAllViews () */
  92. HRESULT CCertTmplComponent::OnViewChange (LPDATAOBJECT pDataObject, LPARAM /*data*/, LPARAM hint)
  93. {
  94. _TRACE (1, L"Entering CCertTmplComponent::OnViewChange\n");
  95. ASSERT (pDataObject);
  96. if ( !pDataObject )
  97. return E_POINTER;
  98. CCertTmplComponentData& dataRef = QueryComponentDataRef ();
  99. HRESULT hr = S_OK;
  100. if ( hint & UPDATE_HINT_ENUM_CERT_TEMPLATES )
  101. {
  102. hr = RefreshResultPane (false);
  103. return hr;
  104. }
  105. hr = RefreshResultPane (false);
  106. CCertTmplCookie* pCookie = dataRef.ConvertCookie (pDataObject);
  107. if ( pCookie )
  108. {
  109. switch (pCookie->m_objecttype)
  110. {
  111. case CERTTMPL_CERT_TEMPLATE:
  112. break;
  113. case CERTTMPL_SNAPIN:
  114. break;
  115. default:
  116. {
  117. IConsole2* pConsole2 = 0;
  118. hr = m_pConsole->QueryInterface (
  119. IID_PPV_ARG(IConsole2, &pConsole2));
  120. if (SUCCEEDED (hr))
  121. {
  122. hr = pConsole2->SetStatusText (L"");
  123. if ( !SUCCEEDED (hr) )
  124. {
  125. _TRACE (0, L"IConsole2::SetStatusText () failed: %x", hr);
  126. }
  127. pConsole2->Release ();
  128. }
  129. }
  130. break;
  131. }
  132. }
  133. _TRACE (-1, L"Leaving CCertTmplComponent::OnViewChange\n");
  134. return hr;
  135. }
  136. HRESULT CCertTmplComponent::Show (
  137. CCookie* pcookie,
  138. LPARAM arg,
  139. HSCOPEITEM /*hScopeItem*/,
  140. LPDATAOBJECT /*pDataObject*/)
  141. {
  142. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  143. HRESULT hr = S_OK;
  144. if ( !arg )
  145. {
  146. if ( !m_pResultData )
  147. {
  148. ASSERT ( FALSE );
  149. return E_UNEXPECTED;
  150. }
  151. m_pViewedCookie = dynamic_cast <CCertTmplCookie*> (pcookie);
  152. ASSERT (m_pViewedCookie);
  153. if ( m_pViewedCookie )
  154. hr = SaveWidths (m_pViewedCookie);
  155. m_pViewedCookie = 0;
  156. return S_OK;
  157. }
  158. if ( m_pResultData )
  159. {
  160. m_pResultData->ModifyViewStyle (
  161. (MMC_RESULT_VIEW_STYLE) (MMC_ENSUREFOCUSVISIBLE | MMC_SHOWSELALWAYS),
  162. MMC_NOSORTHEADER);
  163. }
  164. m_pViewedCookie = dynamic_cast <CCertTmplCookie*> (pcookie);
  165. ASSERT (m_pViewedCookie);
  166. if ( m_pViewedCookie )
  167. {
  168. // Load default columns and widths
  169. LoadColumns (m_pViewedCookie);
  170. // Restore persisted column widths
  171. switch (m_pViewedCookie->m_objecttype)
  172. {
  173. case CERTTMPL_SNAPIN:
  174. break;
  175. case CERTTMPL_CERT_TEMPLATE: // not a scope pane item
  176. ASSERT (0);
  177. break;
  178. default:
  179. ASSERT (0);
  180. break;
  181. }
  182. if ( g_bDomainIsPresent ) //
  183. {
  184. hr = PopulateListbox (m_pViewedCookie);
  185. if ( FAILED (hr) )
  186. {
  187. CString caption;
  188. CString text;
  189. VERIFY (caption.LoadString (IDS_CERTTMPL));
  190. if ( HRESULT_FROM_WIN32 (ERROR_DS_NO_SUCH_OBJECT) == hr )
  191. {
  192. // security review BryanWal 2/20/2002 ok
  193. text.FormatMessage (IDS_CANNOT_ENUM_CERT_TEMPLATES_CONTAINER_NOT_FOUND,
  194. m_szCertTemplatePath);
  195. }
  196. else
  197. {
  198. // security review BryanWal 2/20/2002 ok
  199. text.FormatMessage (IDS_CANNOT_ENUM_CERT_TEMPLATES, GetSystemMessage (hr));
  200. }
  201. int iRetVal = 0;
  202. VERIFY (SUCCEEDED (m_pConsole->MessageBox (text, caption,
  203. MB_ICONWARNING | MB_OK, &iRetVal)));
  204. }
  205. }
  206. }
  207. return hr;
  208. }
  209. HRESULT CCertTmplComponent::Show ( CCookie* pcookie, LPARAM arg, HSCOPEITEM hScopeItem)
  210. {
  211. ASSERT (0);
  212. return Show (pcookie, arg, hScopeItem, 0);
  213. }
  214. HRESULT CCertTmplComponent::OnNotifyAddImages (LPDATAOBJECT /*pDataObject*/,
  215. LPIMAGELIST lpImageList,
  216. HSCOPEITEM /*hSelectedItem*/)
  217. {
  218. long lViewMode = 0;
  219. ASSERT (m_pResultData);
  220. QueryComponentDataRef ().SetResultData (m_pResultData);
  221. HRESULT hr = m_pResultData->GetViewMode (&lViewMode);
  222. ASSERT (SUCCEEDED (hr));
  223. BOOL bLoadLargeIcons = (LVS_ICON == lViewMode);
  224. return QueryComponentDataRef ().LoadIcons (lpImageList, bLoadLargeIcons);
  225. }
  226. HRESULT CCertTmplComponent::PopulateListbox (CCertTmplCookie* pCookie)
  227. {
  228. _TRACE (1, L"Entering CCertTmplComponent::PopulateListbox\n");
  229. HRESULT hr = S_OK;
  230. switch ( pCookie->m_objecttype )
  231. {
  232. case CERTTMPL_SNAPIN:
  233. hr = AddEnterpriseTemplates ();
  234. if ( SUCCEEDED (hr) )
  235. {
  236. m_currResultNodeType = CERTTMPL_CERT_TEMPLATE;
  237. }
  238. break;
  239. case CERTTMPL_CERT_TEMPLATE:
  240. ASSERT (0);
  241. break;
  242. default:
  243. ASSERT (0);
  244. break;
  245. }
  246. _TRACE (-1, L"Leaving CCertTmplComponent::PopulateListbox\n");
  247. return hr;
  248. }
  249. HRESULT CCertTmplComponent::RefreshResultPane (const bool bSilent)
  250. {
  251. _TRACE (1, L"Entering CCertTmplComponent::RefreshResultPane\n");
  252. HRESULT hr = S_OK;
  253. ASSERT (NULL != m_pResultData);
  254. if ( m_pResultData )
  255. {
  256. m_pResultData->DeleteAllRsltItems ();
  257. }
  258. else
  259. hr = E_UNEXPECTED;
  260. if ( m_pViewedCookie )
  261. {
  262. hr = PopulateListbox (m_pViewedCookie);
  263. if ( FAILED (hr) && !bSilent )
  264. {
  265. CString caption;
  266. CString text;
  267. VERIFY (caption.LoadString (IDS_CERTTMPL));
  268. // security review BryanWal 2/20/2002 ok
  269. text.FormatMessage (IDS_CANNOT_ENUM_CERT_TEMPLATES, GetSystemMessage (hr));
  270. int iRetVal = 0;
  271. VERIFY (SUCCEEDED (m_pConsole->MessageBox (text, caption,
  272. MB_ICONWARNING | MB_OK, &iRetVal)));
  273. }
  274. }
  275. _TRACE (-1, L"Leaving CCertTmplComponent::RefreshResultPane\n");
  276. return hr;
  277. }
  278. STDMETHODIMP CCertTmplComponent::GetDisplayInfo (RESULTDATAITEM * pResult)
  279. {
  280. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  281. ASSERT (pResult);
  282. HRESULT hr = S_OK;
  283. if ( pResult && !pResult->bScopeItem ) //&& (pResult->mask & RDI_PARAM) )
  284. {
  285. CCookie* pResultCookie = reinterpret_cast<CCookie*> (pResult->lParam);
  286. ASSERT (pResultCookie);
  287. if ( !pResultCookie || IsBadWritePtr ((LPVOID) pResultCookie, sizeof (CCookie)) )
  288. return E_UNEXPECTED;
  289. CCookie* pActiveCookie = ActiveBaseCookie (pResultCookie);
  290. ASSERT (pActiveCookie);
  291. if ( !pActiveCookie || IsBadWritePtr ((LPVOID) pActiveCookie, sizeof (CCookie)) )
  292. return E_UNEXPECTED;
  293. CCertTmplCookie* pCookie = dynamic_cast <CCertTmplCookie*>(pActiveCookie);
  294. ASSERT (pCookie);
  295. switch (pCookie->m_objecttype)
  296. {
  297. case CERTTMPL_CERT_TEMPLATE:
  298. {
  299. CCertTemplate* pCertTemplate = reinterpret_cast <CCertTemplate*> (pCookie);
  300. ASSERT (pCertTemplate);
  301. if ( pCertTemplate )
  302. {
  303. if (pResult->mask & RDI_STR)
  304. {
  305. // Note: text is first stored in class variable so that the buffer is
  306. // somewhat persistent. Copying the buffer pointer directly to the
  307. // pResult->str would result in the buffer being freed before the pointer
  308. // is used.
  309. switch (pResult->nCol)
  310. {
  311. case COLNUM_CERT_TEMPLATE_OBJECT:
  312. m_szDisplayInfoResult = pCertTemplate->GetDisplayName ();
  313. break;
  314. case COLNUM_CERT_TEMPLATE_TYPE:
  315. {
  316. DWORD dwVersion = pCertTemplate->GetType ();
  317. switch (dwVersion)
  318. {
  319. case 1:
  320. VERIFY (m_szDisplayInfoResult.LoadString (IDS_WINDOWS_2000_AND_LATER));
  321. break;
  322. case 2:
  323. VERIFY (m_szDisplayInfoResult.LoadString (IDS_WINDOWS_2002_AND_LATER));
  324. break;
  325. default:
  326. break;
  327. }
  328. }
  329. break;
  330. case COLNUM_CERT_TEMPLATE_VERSION:
  331. {
  332. DWORD dwMajorVersion = 0;
  333. DWORD dwMinorVersion = 0;
  334. hr = pCertTemplate->GetMajorVersion (dwMajorVersion);
  335. if ( SUCCEEDED (hr) )
  336. {
  337. hr = pCertTemplate->GetMinorVersion (dwMinorVersion);
  338. if ( SUCCEEDED (hr) )
  339. {
  340. WCHAR str[32];
  341. // security review BryanWal 2/20/2002
  342. // str buffer need only be 18 characters to contain a DWORD
  343. m_szDisplayInfoResult = _ultow (dwMajorVersion, str, 10);
  344. m_szDisplayInfoResult += L".";
  345. m_szDisplayInfoResult += _ultow (dwMinorVersion, str, 10);
  346. }
  347. }
  348. }
  349. break;
  350. case COLNUM_CERT_TEMPLATE_AUTOENROLL_STATUS:
  351. if ( pCertTemplate->GoodForAutoEnrollment () )
  352. VERIFY (m_szDisplayInfoResult.LoadString (IDS_VALID_FOR_AUTOENROLLMENT));
  353. else
  354. VERIFY (m_szDisplayInfoResult.LoadString (IDS_INVALID_FOR_AUTOENROLLMENT));
  355. break;
  356. default:
  357. ASSERT (0);
  358. break;
  359. }
  360. pResult->str = const_cast<LPWSTR> ( (LPCWSTR) m_szDisplayInfoResult);
  361. }
  362. if (pResult->mask & RDI_IMAGE)
  363. {
  364. if ( 1 == pCertTemplate->GetType () )
  365. pResult->nImage = iIconCertTemplateV1;
  366. else
  367. pResult->nImage = iIconCertTemplateV2;
  368. }
  369. }
  370. }
  371. break;
  372. default:
  373. ASSERT (0);
  374. break;
  375. }
  376. }
  377. else
  378. hr = CComponent::GetDisplayInfo (pResult);
  379. return hr;
  380. }
  381. ///////////////////////////////////////////////////////////////////////////////
  382. // IExtendContextMenu implementation
  383. //
  384. STDMETHODIMP CCertTmplComponent::AddMenuItems (LPDATAOBJECT pDataObject,
  385. LPCONTEXTMENUCALLBACK pContextMenuCallback,
  386. long *pInsertionAllowed)
  387. {
  388. return QueryComponentDataRef ().AddMenuItems (pDataObject,
  389. pContextMenuCallback, pInsertionAllowed);
  390. }
  391. STDMETHODIMP CCertTmplComponent::Command (long nCommandID, LPDATAOBJECT pDataObject)
  392. {
  393. HRESULT hr = S_OK;
  394. hr = QueryComponentDataRef ().Command (nCommandID, pDataObject);
  395. return hr;
  396. }
  397. HRESULT CCertTmplComponent::OnNotifyDblClick (LPDATAOBJECT pDataObject)
  398. {
  399. _TRACE (1, L"Entering CCertTmplComponent::OnNotifyDblClick\n");
  400. HRESULT hr = S_OK;
  401. ASSERT (pDataObject);
  402. CCertTmplCookie* pParentCookie =
  403. QueryComponentDataRef ().ConvertCookie (pDataObject);
  404. if ( pParentCookie )
  405. {
  406. switch ( pParentCookie->m_objecttype )
  407. {
  408. case CERTTMPL_SNAPIN:
  409. hr = S_FALSE;
  410. break;
  411. case CERTTMPL_CERT_TEMPLATE:
  412. hr = S_FALSE;
  413. break;
  414. default:
  415. _TRACE (0, L"CCertTmplComponentData::OnNotifyDblClick bad parent type\n");
  416. ASSERT (FALSE);
  417. hr = S_OK;
  418. break;
  419. }
  420. }
  421. else
  422. hr = E_UNEXPECTED;
  423. _TRACE (-1, L"Leaving CCertTmplComponent::OnNotifyDblClick\n");
  424. return hr;
  425. }
  426. HRESULT CCertTmplComponent::OnNotifySelect (LPDATAOBJECT pDataObject, BOOL fSelected)
  427. {
  428. ASSERT (m_pConsoleVerb && 0xdddddddd != (UINT_PTR) m_pConsoleVerb);
  429. if ( !m_pConsoleVerb || 0xdddddddd == (UINT_PTR) m_pConsoleVerb )
  430. return E_FAIL;
  431. HRESULT hr = S_OK;
  432. CCertTmplComponentData& dataRef = QueryComponentDataRef ();
  433. // we have to reset the IResultData pointer in the IComponentData object
  434. // so that it points to the correct result pane
  435. if ( fSelected )
  436. {
  437. ASSERT (m_pResultData);
  438. dataRef.SetResultData (m_pResultData);
  439. }
  440. switch (dataRef.GetObjectType (pDataObject))
  441. {
  442. case CERTTMPL_SNAPIN:
  443. m_pConsoleVerb->SetVerbState (MMC_VERB_REFRESH, ENABLED, TRUE);
  444. m_pConsoleVerb->SetDefaultVerb (MMC_VERB_OPEN);
  445. DisplayRootNodeStatusBarText (m_pConsole);
  446. break;
  447. case CERTTMPL_CERT_TEMPLATE:
  448. {
  449. CCertTmplCookie* pCookie = ConvertCookie (pDataObject);
  450. if ( pCookie )
  451. {
  452. CCertTemplate* pCertTemplate = dynamic_cast <CCertTemplate*> (pCookie);
  453. if ( pCertTemplate )
  454. {
  455. if ( !pCertTemplate->IsDefault () )
  456. m_pConsoleVerb->SetVerbState (MMC_VERB_DELETE, ENABLED, TRUE);
  457. // #NTRAID 360650: Cert Server: Cannot rename cert templates
  458. //if ( 1 != pCertTemplate->GetType () && !pCertTemplate->IsDefault () )
  459. // m_pConsoleVerb->SetVerbState (MMC_VERB_RENAME, ENABLED, TRUE);
  460. }
  461. }
  462. }
  463. m_pConsoleVerb->SetVerbState (MMC_VERB_PROPERTIES, ENABLED, TRUE);
  464. m_pConsoleVerb->SetDefaultVerb (MMC_VERB_PROPERTIES);
  465. m_currResultNodeType = CERTTMPL_CERT_TEMPLATE;
  466. DisplayObjectCountInStatusBar (m_pConsole,
  467. QueryComponentDataRef ().m_dwNumCertTemplates);
  468. break;
  469. case CERTTMPL_MULTISEL:
  470. m_pConsoleVerb->SetVerbState (MMC_VERB_DELETE, ENABLED, TRUE);
  471. m_currResultNodeType = CERTTMPL_MULTISEL;
  472. DisplayObjectCountInStatusBar (m_pConsole,
  473. QueryComponentDataRef ().m_dwNumCertTemplates);
  474. break;
  475. default:
  476. m_currResultNodeType = CERTTMPL_INVALID;
  477. hr = E_UNEXPECTED;
  478. break;
  479. }
  480. return hr;
  481. }
  482. STDMETHODIMP CCertTmplComponent::CreatePropertyPages (
  483. LPPROPERTYSHEETCALLBACK pCallBack,
  484. LONG_PTR handle, // This handle must be saved in the property page object to notify the parent when modified
  485. LPDATAOBJECT pDataObject)
  486. {
  487. return QueryComponentDataRef ().CreatePropertyPages (pCallBack, handle, pDataObject);
  488. }
  489. STDMETHODIMP CCertTmplComponent::QueryPagesFor (LPDATAOBJECT pDataObject)
  490. {
  491. return QueryComponentDataRef ().QueryPagesFor (pDataObject);
  492. }
  493. HRESULT CCertTmplComponent::OnNotifyRefresh (LPDATAOBJECT pDataObject)
  494. {
  495. _TRACE (1, L"Entering CCertTmplComponent::OnNotifyRefresh\n");
  496. ASSERT (pDataObject);
  497. if ( !pDataObject )
  498. return E_POINTER;
  499. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  500. HRESULT hr = S_OK;
  501. CWaitCursor waitCursor;
  502. CCertTmplCookie* pCookie = ConvertCookie (pDataObject);
  503. if ( !pCookie )
  504. return E_UNEXPECTED;
  505. CCertTmplComponentData& dataRef = QueryComponentDataRef ();
  506. switch (pCookie->m_objecttype)
  507. {
  508. case CERTTMPL_SNAPIN:
  509. {
  510. // Delete all the result items and force a reexpansion
  511. hr = m_pResultData->DeleteAllRsltItems ();
  512. if ( SUCCEEDED (hr) || E_UNEXPECTED == hr ) // returns E_UNEXPECTED if console shutting down
  513. {
  514. dataRef.RemoveResultCookies (m_pResultData);
  515. }
  516. hr = PopulateListbox (m_pViewedCookie);
  517. if ( FAILED (hr) )
  518. {
  519. CString caption;
  520. CString text;
  521. VERIFY (caption.LoadString (IDS_CERTTMPL));
  522. // security review BryanWal 2/20/2002 ok
  523. text.FormatMessage (IDS_CANNOT_ENUM_CERT_TEMPLATES, GetSystemMessage (hr));
  524. int iRetVal = 0;
  525. VERIFY (SUCCEEDED (m_pConsole->MessageBox (text, caption,
  526. MB_ICONWARNING | MB_OK, &iRetVal)));
  527. }
  528. }
  529. break;
  530. case CERTTMPL_CERT_TEMPLATE:
  531. ASSERT (0);
  532. break;
  533. default:
  534. ASSERT (0);
  535. hr = E_UNEXPECTED;
  536. break;
  537. }
  538. _TRACE (-1, L"Leaving CCertTmplComponent::OnNotifyRefresh\n");
  539. return hr;
  540. }
  541. void CCertTmplComponent::SetTextNotAvailable ()
  542. {
  543. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  544. m_szDisplayInfoResult.LoadString (IDS_NOT_AVAILABLE);
  545. }
  546. HRESULT CCertTmplComponent::DeleteCookie (CCertTmplCookie* pCookie, LPDATAOBJECT pDataObject, bool bRequestConfirmation, bool bIsMultipleSelect)
  547. {
  548. _TRACE (1, L"Entering CCertTmplComponent::DeleteCookie\n");
  549. HRESULT hr = S_OK;
  550. CString text;
  551. CString caption;
  552. int iRetVal = IDYES;
  553. CWaitCursor waitCursor;
  554. switch (pCookie->m_objecttype)
  555. {
  556. case CERTTMPL_CERT_TEMPLATE:
  557. {
  558. CCertTemplate* pCertTemplate = dynamic_cast <CCertTemplate*> (pCookie);
  559. ASSERT (pCertTemplate);
  560. if ( pCertTemplate )
  561. {
  562. if ( bRequestConfirmation )
  563. {
  564. if ( bIsMultipleSelect )
  565. {
  566. VERIFY (text.LoadString (IDS_CONFIRM_DELETE_CERT_TEMPLATE_MULTI));
  567. }
  568. else
  569. VERIFY (text.LoadString (IDS_CONFIRM_DELETE_CERT_TEMPLATE));
  570. VERIFY (caption.LoadString (IDS_CERTTMPL));
  571. hr = m_pConsole->MessageBox (text, caption, MB_ICONWARNING | MB_YESNO, &iRetVal);
  572. ASSERT (SUCCEEDED (hr));
  573. }
  574. if ( IDYES == iRetVal )
  575. {
  576. hr = DeleteCertTemplateFromResultPane (pCertTemplate, pDataObject);
  577. }
  578. else
  579. hr = E_FAIL;
  580. }
  581. }
  582. break;
  583. default:
  584. ASSERT (0);
  585. hr = E_UNEXPECTED;
  586. break;
  587. }
  588. _TRACE (-1, L"Leaving CCertTmplComponent::DeleteCookie\n");
  589. return hr;
  590. }
  591. HRESULT CCertTmplComponent::DeleteCertTemplateFromResultPane (CCertTemplate* pCertTemplate, LPDATAOBJECT /*pDataObject*/)
  592. {
  593. _TRACE (1, L"Entering CCertTmplComponent::DeleteCertTemplateFromResultPane\n");
  594. HRESULT hr = S_OK;
  595. hr = pCertTemplate->Delete ();
  596. if ( SUCCEEDED (hr) )
  597. {
  598. CCertTmplComponentData& dataref = QueryComponentDataRef ();
  599. POSITION prevPos = 0;
  600. POSITION pos = 0;
  601. for (pos = dataref.m_globalFriendlyNameList.GetHeadPosition (); pos;)
  602. {
  603. prevPos = pos;
  604. if ( pCertTemplate->GetDisplayName () ==
  605. dataref.m_globalFriendlyNameList.GetNext (pos) )
  606. {
  607. dataref.m_globalFriendlyNameList.RemoveAt (prevPos);
  608. break;
  609. }
  610. }
  611. for (pos = dataref.m_globalTemplateNameList.GetHeadPosition (); pos;)
  612. {
  613. prevPos = pos;
  614. if ( pCertTemplate->GetTemplateName () ==
  615. dataref.m_globalTemplateNameList.GetNext (pos) )
  616. {
  617. dataref.m_globalTemplateNameList.RemoveAt (prevPos);
  618. break;
  619. }
  620. }
  621. }
  622. else
  623. {
  624. CString caption;
  625. CString text;
  626. VERIFY (caption.LoadString (IDS_CERTTMPL));
  627. // security review BryanWal 2/20/2002 ok
  628. text.FormatMessage (IDS_CANNOT_DELETE_CERT_TEMPLATE, GetSystemMessage (hr));
  629. int iRetVal = 0;
  630. VERIFY (SUCCEEDED (m_pConsole->MessageBox (text, caption,
  631. MB_ICONWARNING | MB_OK, &iRetVal)));
  632. }
  633. _TRACE (-1, L"Leaving CCertTmplComponent::DeleteCertTemplateFromResultPane\n");
  634. return hr;
  635. }
  636. HRESULT CCertTmplComponent::OnNotifyDelete (LPDATAOBJECT pDataObject)
  637. {
  638. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  639. ASSERT (pDataObject);
  640. if ( !pDataObject )
  641. return E_POINTER;
  642. HRESULT hr = S_OK;
  643. long hint = 0;
  644. CWaitCursor waitCursor;
  645. CCertTmplCookie* pCookie =
  646. QueryComponentDataRef ().ConvertCookie (pDataObject);
  647. if ( pCookie )
  648. {
  649. if ( ((CCertTmplCookie*) MMC_MULTI_SELECT_COOKIE) == pCookie )
  650. {
  651. // Is multiple select, get all selected items and paste each one
  652. CCertTemplatesDataObject* pDO = dynamic_cast <CCertTemplatesDataObject*>(pDataObject);
  653. ASSERT (pDO);
  654. if ( pDO )
  655. {
  656. // Is multiple select, get all selected items and delete - confirm
  657. // first deletion only.
  658. bool bRequestConfirmation = true;
  659. pDO->Reset();
  660. while (pDO->Next(1, reinterpret_cast<MMC_COOKIE*>(&pCookie), NULL) != S_FALSE &&
  661. SUCCEEDED (hr) )
  662. {
  663. hr = DeleteCookie (pCookie, pDataObject, bRequestConfirmation, true);
  664. bRequestConfirmation = false;
  665. }
  666. hr = m_pConsole->UpdateAllViews (pDataObject, 0, hint);
  667. }
  668. }
  669. else
  670. {
  671. // In that event, we don't want a confirmation message.
  672. hr = DeleteCookie (pCookie, pDataObject, true, false);
  673. if ( SUCCEEDED (hr) )
  674. hr = m_pConsole->UpdateAllViews (pDataObject, 0, hint);
  675. }
  676. }
  677. return hr;
  678. }
  679. // This compare is used to sort the items in the listview
  680. //
  681. // Parameters:
  682. //
  683. // lUserParam - user param passed in when IResultData::Sort () was called
  684. // cookieA - first item to compare
  685. // cookieB - second item to compare
  686. // pnResult [in, out]- contains the col on entry,
  687. // -1, 0, 1 based on comparison for return value.
  688. //
  689. // Note: Assume sort is ascending when comparing.
  690. STDMETHODIMP CCertTmplComponent::Compare (RDCOMPARE* prdc, int* pnResult)
  691. {
  692. // _TRACE (1, L"Entering CCertTmplComponent::Compare\n");
  693. if ( !prdc || !pnResult )
  694. return E_POINTER;
  695. HRESULT hr = S_OK;
  696. if ( RDCI_ScopeItem & prdc->prdch1->dwFlags )
  697. {
  698. }
  699. else
  700. {
  701. CCertTmplCookie* pCookie = reinterpret_cast <CCertTmplCookie*> (prdc->prdch1->cookie);
  702. ASSERT (pCookie);
  703. if ( !pCookie )
  704. return E_UNEXPECTED;
  705. switch (pCookie->m_objecttype)
  706. {
  707. case CERTTMPL_CERT_TEMPLATE:
  708. {
  709. CCertTemplate* pCertTemplateA = reinterpret_cast <CCertTemplate*> (prdc->prdch1->cookie);
  710. CCertTemplate* pCertTemplateB = reinterpret_cast <CCertTemplate*> (prdc->prdch2->cookie);
  711. switch ( prdc->nColumn )
  712. {
  713. case COLNUM_CERT_TEMPLATE_OBJECT:
  714. *pnResult = LocaleStrCmp (pCertTemplateA->GetDisplayName (), pCertTemplateB->GetDisplayName ());
  715. break;
  716. case COLNUM_CERT_TEMPLATE_TYPE:
  717. if ( pCertTemplateA->GetType () == pCertTemplateB->GetType () )
  718. *pnResult = 0;
  719. else if ( pCertTemplateA->GetType () > pCertTemplateB->GetType () )
  720. *pnResult = 1;
  721. else
  722. *pnResult = -1;
  723. break;
  724. case COLNUM_CERT_TEMPLATE_VERSION:
  725. {
  726. // Sort first on major version, then on minor version
  727. *pnResult = 0;
  728. DWORD dwMajorVersionA = 0;
  729. hr = pCertTemplateA->GetMajorVersion (dwMajorVersionA);
  730. if ( SUCCEEDED (hr) )
  731. {
  732. DWORD dwMajorVersionB = 0;
  733. hr = pCertTemplateB->GetMajorVersion (dwMajorVersionB);
  734. if ( SUCCEEDED (hr) )
  735. {
  736. if ( dwMajorVersionA == dwMajorVersionB )
  737. {
  738. DWORD dwMinorVersionA = 0;
  739. hr = pCertTemplateA->GetMinorVersion (dwMinorVersionA);
  740. if ( SUCCEEDED (hr) )
  741. {
  742. DWORD dwMinorVersionB = 0;
  743. hr = pCertTemplateB->GetMinorVersion (dwMinorVersionB);
  744. if ( SUCCEEDED (hr) )
  745. {
  746. if ( dwMinorVersionA == dwMinorVersionB )
  747. *pnResult = 0;
  748. else if ( dwMinorVersionA > dwMinorVersionB )
  749. *pnResult = 1;
  750. else
  751. *pnResult = -1;
  752. }
  753. }
  754. }
  755. else if ( dwMajorVersionA > dwMajorVersionB )
  756. *pnResult = 1;
  757. else
  758. *pnResult = -1;
  759. }
  760. }
  761. }
  762. break;
  763. case COLNUM_CERT_TEMPLATE_AUTOENROLL_STATUS:
  764. if ( pCertTemplateA->GoodForAutoEnrollment () &&
  765. pCertTemplateB->GoodForAutoEnrollment () )
  766. {
  767. *pnResult = 0;
  768. }
  769. else if ( pCertTemplateA->GoodForAutoEnrollment () &&
  770. !pCertTemplateB->GoodForAutoEnrollment () )
  771. {
  772. *pnResult = 1;
  773. }
  774. else
  775. *pnResult = 0;
  776. break;
  777. default:
  778. ASSERT (0);
  779. break;
  780. }
  781. }
  782. break;
  783. default:
  784. ASSERT (0);
  785. break;
  786. }
  787. }
  788. // _TRACE (-1, L"Leaving CCertTmplComponent::Compare\n");
  789. return hr;
  790. }
  791. /////////////////////////////////////////////////////////////////////
  792. // Virtual function called by CComponent::IComponent::Notify (MMCN_COLUMN_CLICK)
  793. HRESULT CCertTmplComponent::OnNotifyColumnClick (LPDATAOBJECT /*pDataObject*/, LPARAM iColumn, LPARAM uFlags)
  794. {
  795. _TRACE (1, L"Entering CCertTmplComponent::OnNotifyColumnClick\n");
  796. IResultData* pResultData = 0;
  797. HRESULT hr = m_pConsole->QueryInterface (
  798. IID_PPV_ARG (IResultData, &pResultData));
  799. if ( SUCCEEDED (hr) )
  800. {
  801. m_nSortColumn = (int) iColumn;
  802. hr = pResultData->Sort (m_nSortColumn, (DWORD)uFlags, 0);
  803. _TRACE (0, L"IResultData::Sort () returned: 0x%x\n", hr);
  804. pResultData->Release ();
  805. }
  806. _TRACE (-1, L"Leaving CCertTmplComponent::OnNotifyColumnClick\n");
  807. return hr;
  808. }
  809. STDMETHODIMP CCertTmplComponent::Notify (LPDATAOBJECT pDataObject, MMC_NOTIFY_TYPE event, LPARAM arg, LPARAM param)
  810. {
  811. HRESULT hr = S_OK;
  812. switch (event)
  813. {
  814. case MMCN_CUTORMOVE:
  815. hr = OnNotifyCutOrMove (arg);
  816. break;
  817. case MMCN_QUERY_PASTE:
  818. hr = OnNotifyQueryPaste (pDataObject, arg, param);
  819. break;
  820. case MMCN_PASTE:
  821. hr = OnNotifyPaste (pDataObject, arg, param);
  822. break;
  823. case MMCN_SHOW:
  824. {
  825. CCookie* pCookie = NULL;
  826. hr = ::ExtractData (pDataObject,
  827. CDataObject::m_CFRawCookie,
  828. &pCookie,
  829. sizeof(pCookie));
  830. if ( SUCCEEDED (hr) )
  831. {
  832. hr = Show (ActiveBaseCookie (pCookie), arg,
  833. (HSCOPEITEM) param, pDataObject);
  834. }
  835. }
  836. break;
  837. case MMCN_RENAME:
  838. hr = OnNotifyRename (pDataObject, arg, param);
  839. break;
  840. default:
  841. hr = CComponent::Notify (pDataObject, event, arg, param);
  842. break;
  843. }
  844. return hr;
  845. }
  846. HRESULT CCertTmplComponent::OnNotifySnapinHelp (LPDATAOBJECT pDataObject)
  847. {
  848. _TRACE (1, L"Entering CCertTmplComponent::OnNotifySnapinHelp\n");
  849. AFX_MANAGE_STATE (AfxGetStaticModuleState ( ));
  850. CComQIPtr<IDisplayHelp,&IID_IDisplayHelp> spDisplayHelp = m_pConsole;
  851. if ( !spDisplayHelp )
  852. {
  853. ASSERT(FALSE);
  854. return E_UNEXPECTED;
  855. }
  856. CString strHelpTopic;
  857. UINT nLen = ::GetSystemWindowsDirectory (strHelpTopic.GetBufferSetLength(2 * MAX_PATH), 2 * MAX_PATH);
  858. strHelpTopic.ReleaseBuffer();
  859. if (0 == nLen)
  860. {
  861. ASSERT(FALSE);
  862. return E_FAIL;
  863. }
  864. strHelpTopic += CERTTMPL_HELP_PATH;
  865. strHelpTopic += CERTTMPL_CONCEPTS_HELP_FILE;
  866. strHelpTopic += L"::/";
  867. CCertTmplComponentData& dataRef = QueryComponentDataRef ();
  868. CCertTmplCookie* pCookie = dataRef.ConvertCookie (pDataObject);
  869. if ( pCookie )
  870. {
  871. switch (pCookie->m_objecttype)
  872. {
  873. case CERTTMPL_SNAPIN:
  874. case CERTTMPL_CERT_TEMPLATE:
  875. default:
  876. strHelpTopic += CERTTMPL_HTML_TOP_NODE;
  877. break;
  878. }
  879. }
  880. HRESULT hr = spDisplayHelp->ShowTopic (T2OLE ((PWSTR)(PCWSTR) strHelpTopic));
  881. if ( FAILED (hr) )
  882. {
  883. CString caption;
  884. CString text;
  885. VERIFY (caption.LoadString (IDS_CERTTMPL));
  886. // security review BryanWal 2/20/2002 ok
  887. text.FormatMessage (IDS_CANT_DISPLAY_SNAPIN_HELP_TOPIC, strHelpTopic,
  888. GetSystemMessage (hr));
  889. int iRetVal = 0;
  890. VERIFY (SUCCEEDED (m_pConsole->MessageBox (text, caption,
  891. MB_ICONWARNING | MB_OK, &iRetVal)));
  892. }
  893. _TRACE (-1, L"Leaving CCertTmplComponent::OnNotifySnapinHelp\n");
  894. return hr;
  895. }
  896. HRESULT CCertTmplComponent::OnNotifyPaste (LPDATAOBJECT /*pDataObject*/, LPARAM /*arg*/, LPARAM /*param*/)
  897. {
  898. return E_NOTIMPL;
  899. }
  900. HRESULT CCertTmplComponent::OnNotifyQueryPaste(LPDATAOBJECT /*pDataObject*/, LPARAM /*arg*/, LPARAM /*param*/)
  901. {
  902. return E_NOTIMPL;
  903. }
  904. STDMETHODIMP CCertTmplComponent::GetResultViewType(MMC_COOKIE cookie,
  905. BSTR* ppViewType,
  906. long* pViewOptions)
  907. {
  908. CCertTmplCookie* pScopeCookie = reinterpret_cast <CCertTmplCookie*> (cookie);
  909. if ( pScopeCookie )
  910. {
  911. switch (pScopeCookie->m_objecttype)
  912. {
  913. case CERTTMPL_SNAPIN:
  914. *pViewOptions |= MMC_VIEW_OPTIONS_MULTISELECT;
  915. break;
  916. case CERTTMPL_CERT_TEMPLATE:
  917. default:
  918. break;
  919. }
  920. }
  921. else
  922. *pViewOptions |= MMC_VIEW_OPTIONS_MULTISELECT;
  923. *ppViewType = NULL;
  924. return S_FALSE;
  925. }
  926. STDMETHODIMP CCertTmplComponent::Initialize(LPCONSOLE lpConsole)
  927. {
  928. _TRACE (1, L"Entering CCertTmplComponent::Initialize\n");
  929. HRESULT hr = CComponent::Initialize (lpConsole);
  930. if ( SUCCEEDED (hr) )
  931. {
  932. ASSERT (m_pHeader);
  933. QueryComponentDataRef ().m_pHeader = m_pHeader;
  934. if ( lpConsole )
  935. {
  936. if ( QueryComponentDataRef ().m_pComponentConsole )
  937. SAFE_RELEASE (QueryComponentDataRef ().m_pComponentConsole);
  938. QueryComponentDataRef ().m_pComponentConsole = m_pConsole;
  939. QueryComponentDataRef ().m_pComponentConsole->AddRef ();
  940. }
  941. }
  942. _TRACE (-1, L"Leaving CCertTmplComponent::Initialize\n");
  943. return hr;
  944. }
  945. HRESULT CCertTmplComponent::LoadColumnsFromArrays (CertTmplObjectType objecttype )
  946. {
  947. _TRACE (1, L"Entering CCertTmplComponent::LoadColumnsFromArrays\n");
  948. ASSERT (m_pHeader);
  949. CString str;
  950. for ( INT i = 0; 0 != m_Columns[objecttype][i]; i++)
  951. {
  952. VERIFY(str.LoadString (m_Columns[objecttype][i]));
  953. m_pHeader->InsertColumn(i, const_cast<PWSTR>((PCWSTR)str), LVCFMT_LEFT,
  954. m_ColumnWidths[objecttype][i]);
  955. }
  956. _TRACE (-1, L"Leaving CCertTmplComponent::LoadColumnsFromArrays\n");
  957. return S_OK;
  958. }
  959. HRESULT CCertTmplComponent::SaveWidths(CCertTmplCookie * pCookie)
  960. {
  961. _TRACE (1, L"Entering CCertTmplComponent::SaveWidths\n");
  962. HRESULT hr = S_OK;
  963. m_fDirty = TRUE;
  964. ASSERT (pCookie);
  965. if ( pCookie )
  966. {
  967. switch (m_pViewedCookie->m_objecttype)
  968. {
  969. case CERTTMPL_SNAPIN:
  970. {
  971. const UINT* pColumns = m_Columns[m_pViewedCookie->m_objecttype];
  972. ASSERT(pColumns);
  973. int nWidth = 0;
  974. for (UINT iIndex = 0; iIndex < pColumns[iIndex]; iIndex++)
  975. {
  976. hr = m_pHeader->GetColumnWidth ((int) iIndex, &nWidth);
  977. if ( SUCCEEDED (hr) )
  978. {
  979. m_ColumnWidths[m_pViewedCookie->m_objecttype][iIndex] =
  980. (UINT) nWidth;
  981. }
  982. else
  983. break;
  984. }
  985. }
  986. break;
  987. default:
  988. ASSERT (0);
  989. break;
  990. }
  991. }
  992. else
  993. hr = E_POINTER;
  994. _TRACE (-1, L"Leaving CCertTmplComponent::SaveWidths\n");
  995. return hr;
  996. }
  997. ///////////////////////////////////////////////////////////////////////////////
  998. #define _dwMagicword 10000 // Internal version number
  999. STDMETHODIMP CCertTmplComponent::Load(IStream __RPC_FAR *pIStream)
  1000. {
  1001. _TRACE (1, L"Entering CCertTmplComponent::Load\n");
  1002. HRESULT hr = S_OK;
  1003. #ifndef DONT_PERSIST
  1004. ASSERT (pIStream);
  1005. XSafeInterfacePtr<IStream> pIStreamSafePtr( pIStream );
  1006. // Read the magic word from the stream
  1007. DWORD dwMagicword = 0;
  1008. hr = pIStream->Read (&dwMagicword, sizeof(dwMagicword), NULL);
  1009. if ( FAILED(hr) )
  1010. {
  1011. ASSERT( FALSE );
  1012. return hr;
  1013. }
  1014. if (dwMagicword != _dwMagicword)
  1015. {
  1016. // We have a version mismatch
  1017. _TRACE(0, L"INFO: CCertTmplComponentData::Load() - Wrong Magicword. You need to re-save your .msc file.\n");
  1018. return E_FAIL;
  1019. }
  1020. int numCols = 0;
  1021. for (int iIndex = 0; iIndex < CERTTMPL_NUMTYPES && SUCCEEDED (hr); iIndex++)
  1022. {
  1023. switch (iIndex)
  1024. {
  1025. case CERTTMPL_SNAPIN:
  1026. numCols = CERT_TEMPLATES_NUM_COLS;
  1027. break;;
  1028. case CERTTMPL_CERT_TEMPLATE:
  1029. continue;
  1030. default:
  1031. ASSERT (0);
  1032. break;
  1033. }
  1034. for (int colNum = 0; colNum < numCols; colNum++)
  1035. {
  1036. hr = pIStream->Read (&(m_ColumnWidths[iIndex][colNum]),
  1037. sizeof (UINT), NULL);
  1038. ASSERT (SUCCEEDED (hr));
  1039. if ( FAILED(hr) )
  1040. {
  1041. break;
  1042. }
  1043. }
  1044. }
  1045. #endif
  1046. _TRACE (-1, L"Leaving CCertTmplComponent::Load\n");
  1047. return S_OK;
  1048. }
  1049. ///////////////////////////////////////////////////////////////////////////////
  1050. STDMETHODIMP CCertTmplComponent::Save(IStream __RPC_FAR *pIStream, BOOL /*fSameAsLoad*/)
  1051. {
  1052. _TRACE (1, L"Entering CCertTmplComponent::Save\n");
  1053. HRESULT hr = S_OK;
  1054. #ifndef DONT_PERSIST
  1055. ASSERT (pIStream);
  1056. XSafeInterfacePtr<IStream> pIStreamSafePtr (pIStream);
  1057. // Store the magic word to the stream
  1058. DWORD dwMagicword = _dwMagicword;
  1059. hr = pIStream->Write (&dwMagicword, sizeof(dwMagicword), NULL);
  1060. ASSERT (SUCCEEDED (hr));
  1061. if ( FAILED (hr) )
  1062. return hr;
  1063. int numCols = 0;
  1064. for (int iIndex = 0; iIndex < CERTTMPL_NUMTYPES && SUCCEEDED (hr); iIndex++)
  1065. {
  1066. switch (iIndex)
  1067. {
  1068. case CERTTMPL_SNAPIN:
  1069. numCols = CERT_TEMPLATES_NUM_COLS;
  1070. break;;
  1071. case CERTTMPL_CERT_TEMPLATE:
  1072. continue;
  1073. default:
  1074. ASSERT (0);
  1075. break;
  1076. }
  1077. for (int colNum = 0; colNum < numCols; colNum++)
  1078. {
  1079. hr = pIStream->Write (&(m_ColumnWidths[iIndex][colNum]),
  1080. sizeof (UINT), NULL);
  1081. ASSERT (SUCCEEDED (hr));
  1082. if ( FAILED(hr) )
  1083. {
  1084. ASSERT (FALSE);
  1085. break;
  1086. }
  1087. }
  1088. }
  1089. #endif
  1090. _TRACE (-1, L"Leaving CCertTmplComponent::Save\n");
  1091. return S_OK;
  1092. }
  1093. HRESULT CCertTmplComponent::OnNotifyCutOrMove(LPARAM arg)
  1094. {
  1095. _TRACE (1, L"Entering CCertTmplComponent::OnNotifyCutOrMove\n");
  1096. if ( !arg )
  1097. return E_POINTER;
  1098. LPDATAOBJECT pDataObject = reinterpret_cast <IDataObject*> (arg);
  1099. ASSERT (pDataObject);
  1100. if ( !pDataObject )
  1101. return E_UNEXPECTED;
  1102. HRESULT hr = S_OK;
  1103. CCertTmplCookie* pCookie =
  1104. QueryComponentDataRef ().ConvertCookie (pDataObject);
  1105. if ( pCookie )
  1106. {
  1107. if ( ((CCertTmplCookie*) MMC_MULTI_SELECT_COOKIE) == pCookie )
  1108. {
  1109. CCertTemplatesDataObject* pDO = dynamic_cast <CCertTemplatesDataObject*>(pDataObject);
  1110. ASSERT (pDO);
  1111. if ( pDO )
  1112. {
  1113. pDO->Reset();
  1114. while (pDO->Next(1, reinterpret_cast<MMC_COOKIE*>(&pCookie), NULL) != S_FALSE)
  1115. {
  1116. hr = DeleteCookie (pCookie, pDataObject, false, true);
  1117. }
  1118. }
  1119. else
  1120. hr = E_FAIL;
  1121. }
  1122. else
  1123. {
  1124. hr = DeleteCookie (pCookie, pDataObject, false, false);
  1125. }
  1126. if ( SUCCEEDED (hr) )
  1127. RefreshResultPane (false);
  1128. }
  1129. _TRACE (-1, L"Leaving CCertTmplComponent::OnNotifyCutOrMove\n");
  1130. return hr;
  1131. }
  1132. CCertTmplCookie* CCertTmplComponent::ConvertCookie(LPDATAOBJECT pDataObject)
  1133. {
  1134. return QueryComponentDataRef ().ConvertCookie (pDataObject);
  1135. }
  1136. HRESULT CCertTmplComponent::RefreshResultItem (CCertTmplCookie* pCookie)
  1137. {
  1138. _TRACE (1, L"Entering CCertTmplComponent::RefreshResultItem\n");
  1139. ASSERT (pCookie);
  1140. if ( !pCookie )
  1141. return E_POINTER;
  1142. HRESULT hr = S_OK;
  1143. HRESULTITEM itemID = 0;
  1144. ASSERT (m_pResultData);
  1145. if ( m_pResultData )
  1146. {
  1147. pCookie->Refresh ();
  1148. hr = m_pResultData->FindItemByLParam ( (LPARAM) pCookie, &itemID);
  1149. ASSERT (SUCCEEDED (hr));
  1150. if ( SUCCEEDED (hr) )
  1151. {
  1152. hr = m_pResultData->UpdateItem (itemID);
  1153. ASSERT (SUCCEEDED (hr));
  1154. }
  1155. }
  1156. else
  1157. hr = E_FAIL;
  1158. _TRACE (-1, L"Leaving CCertTmplComponent::RefreshResultItem\n");
  1159. return hr;
  1160. }
  1161. /////////////////////////////////////////////////////////////////////
  1162. // Virtual function called by CComponent::IComponent::Notify(MMCN_PROPERTY_CHANGE)
  1163. // OnPropertyChange() is generated by MMCPropertyChangeNotify( param )
  1164. HRESULT CCertTmplComponent::OnPropertyChange (LPARAM param)
  1165. {
  1166. return QueryComponentDataRef ().OnPropertyChange (param);
  1167. }
  1168. HRESULT CCertTmplComponent::AddEnterpriseTemplates ()
  1169. {
  1170. _TRACE (1, L"Entering CCertTmplComponent::AddEnterpriseTemplates\n");
  1171. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  1172. HRESULT hr = S_OK;
  1173. CWaitCursor cursor;
  1174. CComPtr<IADsPathname> spPathname;
  1175. //
  1176. // Constructing the directory paths
  1177. //
  1178. // security review BryanWal 2/20/2002 ok
  1179. hr = CoCreateInstance(
  1180. CLSID_Pathname,
  1181. NULL,
  1182. CLSCTX_ALL,
  1183. IID_PPV_ARG (IADsPathname, &spPathname));
  1184. if ( SUCCEEDED (hr) )
  1185. {
  1186. ASSERT (!!spPathname);
  1187. CComBSTR bstrPathElement = CERTTMPL_LDAP;
  1188. hr = spPathname->Set(bstrPathElement,
  1189. ADS_SETTYPE_PROVIDER);
  1190. if ( SUCCEEDED (hr) )
  1191. {
  1192. hr = spPathname->Set (const_cast <BSTR> ((PCWSTR)QueryComponentDataRef ().m_szThisDomainDns),
  1193. ADS_SETTYPE_SERVER);
  1194. if ( SUCCEEDED (hr) )
  1195. {
  1196. //
  1197. // Open the root DSE object
  1198. //
  1199. bstrPathElement = CERTTMPL_ROOTDSE;
  1200. hr = spPathname->AddLeafElement(bstrPathElement);
  1201. if ( SUCCEEDED (hr) )
  1202. {
  1203. BSTR bstrFullPath = 0;
  1204. hr = spPathname->Retrieve(ADS_FORMAT_X500, &bstrFullPath);
  1205. if ( SUCCEEDED (hr) )
  1206. {
  1207. CComPtr<IADs> spRootDSEObject;
  1208. VARIANT varNamingContext;
  1209. hr = ADsGetObject (
  1210. bstrFullPath,
  1211. IID_PPV_ARG (IADs, &spRootDSEObject));
  1212. if ( SUCCEEDED (hr) )
  1213. {
  1214. ASSERT (!!spRootDSEObject);
  1215. //
  1216. // Get the configuration naming context from the root DSE
  1217. //
  1218. bstrPathElement = CERTTMPL_CONFIG_NAMING_CONTEXT;
  1219. hr = spRootDSEObject->Get(bstrPathElement,
  1220. &varNamingContext);
  1221. if ( SUCCEEDED (hr) )
  1222. {
  1223. hr = spPathname->Set(V_BSTR(&varNamingContext),
  1224. ADS_SETTYPE_DN);
  1225. if ( SUCCEEDED (hr) )
  1226. {
  1227. bstrPathElement = L"CN=Services";
  1228. hr = spPathname->AddLeafElement (bstrPathElement);
  1229. if ( SUCCEEDED (hr) )
  1230. {
  1231. bstrPathElement = L"CN=Public Key Services";
  1232. hr = spPathname->AddLeafElement (bstrPathElement);
  1233. if ( SUCCEEDED (hr) )
  1234. {
  1235. bstrPathElement = L"CN=Certificate Templates";
  1236. hr = spPathname->AddLeafElement (bstrPathElement);
  1237. if ( SUCCEEDED (hr) )
  1238. {
  1239. BSTR bstrCertTemplatePath = 0;
  1240. hr = spPathname->Retrieve(ADS_FORMAT_X500, &bstrCertTemplatePath);
  1241. if ( SUCCEEDED (hr) )
  1242. {
  1243. m_szCertTemplatePath = bstrCertTemplatePath;
  1244. CComPtr<IDirectoryObject> spTemplateContObj;
  1245. hr = ADsGetObject (
  1246. bstrCertTemplatePath,
  1247. IID_PPV_ARG (IDirectoryObject, &spTemplateContObj));
  1248. if ( SUCCEEDED (hr) )
  1249. {
  1250. hr = EnumerateTemplates (spTemplateContObj, bstrCertTemplatePath);
  1251. if ( SUCCEEDED (hr) )
  1252. {
  1253. m_currResultNodeType = CERTTMPL_CERT_TEMPLATE;
  1254. hr = m_pResultData->Sort (m_nSortColumn, 0, 0);
  1255. }
  1256. }
  1257. else
  1258. {
  1259. _TRACE (0, L"ADsGetObject (%s) failed: 0x%x\n", bstrCertTemplatePath, hr);
  1260. }
  1261. SysFreeString (bstrCertTemplatePath);
  1262. }
  1263. }
  1264. }
  1265. }
  1266. }
  1267. }
  1268. else
  1269. {
  1270. _TRACE (0, L"IADs::Get (%s) failed: 0x%x\n", CERTTMPL_CONFIG_NAMING_CONTEXT, hr);
  1271. }
  1272. }
  1273. else
  1274. {
  1275. _TRACE (0, L"ADsGetObject (%s) failed: 0x%x\n", bstrFullPath, hr);
  1276. }
  1277. }
  1278. }
  1279. }
  1280. }
  1281. }
  1282. else
  1283. hr = E_POINTER;
  1284. _TRACE (-1, L"Leaving CCertTmplComponent::AddEnterpriseTemplates\n");
  1285. return hr;
  1286. }
  1287. HRESULT CCertTmplComponent::EnumerateTemplates (
  1288. IDirectoryObject* pTemplateContObj,
  1289. const BSTR bszTemplateContainerPath)
  1290. {
  1291. _TRACE (1, L"Entering CCertTmplComponent::EnumerateTemplates\n");
  1292. CCertTmplComponentData& dataRef = QueryComponentDataRef ();
  1293. CWaitCursor cursor;
  1294. dataRef.m_fUseCache = false;
  1295. // Bug 243609 CertServer: Wrong count of templates displayed in the MMC
  1296. dataRef.m_dwNumCertTemplates = 0;
  1297. dataRef.m_globalTemplateNameList.RemoveAll ();
  1298. dataRef.m_globalFriendlyNameList.RemoveAll ();
  1299. CComPtr<IDirectorySearch> spDsSearch;
  1300. HRESULT hr = pTemplateContObj->QueryInterface (IID_PPV_ARG(IDirectorySearch, &spDsSearch));
  1301. if ( SUCCEEDED (hr) )
  1302. {
  1303. ASSERT (!!spDsSearch);
  1304. ADS_SEARCHPREF_INFO pSearchPref[1];
  1305. DWORD dwNumPref = 1;
  1306. pSearchPref[0].dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
  1307. pSearchPref[0].vValue.dwType = ADSTYPE_INTEGER;
  1308. pSearchPref[0].vValue.Integer = ADS_SCOPE_ONELEVEL;
  1309. hr = spDsSearch->SetSearchPreference(
  1310. pSearchPref,
  1311. dwNumPref
  1312. );
  1313. if ( SUCCEEDED (hr) )
  1314. {
  1315. static const DWORD cAttrs = 2;
  1316. static PWSTR rgszAttrList[cAttrs] = {L"displayName", L"cn"};
  1317. ADS_SEARCH_HANDLE hSearchHandle = 0;
  1318. wstring strQuery;
  1319. ADS_SEARCH_COLUMN Column;
  1320. Column.pszAttrName = 0;
  1321. strQuery = L"objectClass=pKICertificateTemplate";
  1322. hr = spDsSearch->ExecuteSearch(
  1323. const_cast <PWSTR>(strQuery.c_str ()),
  1324. rgszAttrList,
  1325. cAttrs,
  1326. &hSearchHandle
  1327. );
  1328. if ( SUCCEEDED (hr) )
  1329. {
  1330. CCookie& rootCookie = dataRef.QueryBaseRootCookie ();
  1331. while ((hr = spDsSearch->GetNextRow (hSearchHandle)) != S_ADS_NOMORE_ROWS )
  1332. {
  1333. if (FAILED(hr))
  1334. continue;
  1335. //
  1336. // Getting current row's information
  1337. //
  1338. hr = spDsSearch->GetColumn(
  1339. hSearchHandle,
  1340. rgszAttrList[0],
  1341. &Column
  1342. );
  1343. if ( SUCCEEDED (hr) )
  1344. {
  1345. CString strDisplayName = Column.pADsValues->CaseIgnoreString;
  1346. spDsSearch->FreeColumn (&Column);
  1347. Column.pszAttrName = NULL;
  1348. hr = spDsSearch->GetColumn(
  1349. hSearchHandle,
  1350. rgszAttrList[1],
  1351. &Column
  1352. );
  1353. if ( SUCCEEDED (hr) )
  1354. {
  1355. CString strTemplateName = Column.pADsValues->CaseIgnoreString;
  1356. spDsSearch->FreeColumn (&Column);
  1357. Column.pszAttrName = NULL;
  1358. CComPtr<IADsPathname> spPathname;
  1359. //
  1360. // Constructing the directory paths
  1361. //
  1362. // security review BryanWal 2/20/2002 ok
  1363. hr = CoCreateInstance(
  1364. CLSID_Pathname,
  1365. NULL,
  1366. CLSCTX_ALL,
  1367. IID_PPV_ARG (IADsPathname, &spPathname));
  1368. if ( SUCCEEDED (hr) )
  1369. {
  1370. ASSERT (!!spPathname);
  1371. hr = spPathname->Set(bszTemplateContainerPath,
  1372. ADS_SETTYPE_FULL);
  1373. if ( SUCCEEDED (hr) )
  1374. {
  1375. //
  1376. // Open the root DSE object
  1377. //
  1378. CComBSTR bstrPathElement = strTemplateName;
  1379. hr = spPathname->AddLeafElement(bstrPathElement);
  1380. if ( SUCCEEDED (hr) )
  1381. {
  1382. BSTR bstrFullPath = 0;
  1383. hr = spPathname->Retrieve(ADS_FORMAT_X500, &bstrFullPath);
  1384. if ( SUCCEEDED (hr) )
  1385. {
  1386. CCertTemplate* pCertTemplate =
  1387. new CCertTemplate (strDisplayName, strTemplateName,
  1388. bstrFullPath, false,
  1389. dataRef.m_fUseCache);
  1390. if ( pCertTemplate )
  1391. {
  1392. dataRef.m_fUseCache = true;
  1393. rootCookie.m_listResultCookieBlocks.AddHead (pCertTemplate);
  1394. RESULTDATAITEM rdItem;
  1395. // security review BryanWal 02/20/2002 ok
  1396. ::ZeroMemory (&rdItem, sizeof (rdItem));
  1397. rdItem.mask = RDI_STR | RDI_IMAGE | RDI_PARAM;
  1398. rdItem.nCol = 0;
  1399. rdItem.str = MMC_CALLBACK;
  1400. if ( 1 == pCertTemplate->GetType () )
  1401. rdItem.nImage = iIconCertTemplateV1;
  1402. else
  1403. rdItem.nImage = iIconCertTemplateV2;
  1404. rdItem.lParam = (LPARAM) pCertTemplate;
  1405. pCertTemplate->m_resultDataID = m_pResultData;
  1406. hr = m_pResultData->InsertItem (&rdItem);
  1407. if ( FAILED (hr) )
  1408. {
  1409. _TRACE (0, L"IResultData::InsertItem failed: 0x%x\n", hr);
  1410. hr = S_OK;
  1411. break;
  1412. }
  1413. else
  1414. {
  1415. dataRef.m_dwNumCertTemplates++;
  1416. dataRef.m_globalTemplateNameList.AddTail (strTemplateName);
  1417. dataRef.m_globalFriendlyNameList.AddHead (
  1418. pCertTemplate->GetDisplayName ());
  1419. }
  1420. }
  1421. else
  1422. {
  1423. hr = E_OUTOFMEMORY;
  1424. break;
  1425. }
  1426. SysFreeString (bstrFullPath);
  1427. }
  1428. }
  1429. }
  1430. }
  1431. }
  1432. }
  1433. else if ( hr != E_ADS_COLUMN_NOT_SET )
  1434. {
  1435. break;
  1436. }
  1437. else
  1438. {
  1439. _TRACE (0, L"IDirectorySearch::GetColumn () failed: 0x%x\n", hr);
  1440. }
  1441. }
  1442. }
  1443. else
  1444. {
  1445. _TRACE (0, L"IDirectorySearch::ExecuteSearch () failed: 0x%x\n", hr);
  1446. }
  1447. spDsSearch->CloseSearchHandle(hSearchHandle);
  1448. }
  1449. else
  1450. {
  1451. _TRACE (0, L"IDirectorySearch::SetSearchPreference () failed: 0x%x\n", hr);
  1452. }
  1453. }
  1454. else
  1455. {
  1456. _TRACE (0, L"IDirectoryObject::QueryInterface (IDirectorySearch) failed: 0x%x\n", hr);
  1457. }
  1458. _TRACE (-1, L"Leaving CCertTmplComponent::EnumerateTemplates: 0x%x\n", hr);
  1459. dataRef.m_fUseCache = false;
  1460. return hr;
  1461. }
  1462. HRESULT CCertTmplComponent::OnNotifyRename(LPDATAOBJECT pDataObject, LPARAM /*arg*/, LPARAM param)
  1463. {
  1464. _TRACE (1, L"Entering CCertTmplComponent::OnNotifyRename\n");
  1465. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  1466. HRESULT hr = S_FALSE;
  1467. CCertTmplCookie* pCookie = ConvertCookie (pDataObject);
  1468. if ( pCookie )
  1469. {
  1470. switch (pCookie->m_objecttype)
  1471. {
  1472. case CERTTMPL_CERT_TEMPLATE:
  1473. {
  1474. CString newName = (LPOLESTR) param;
  1475. newName.TrimLeft ();
  1476. newName.TrimRight ();
  1477. if ( !newName.IsEmpty () )
  1478. {
  1479. CCertTmplComponentData& dataref = QueryComponentDataRef ();
  1480. POSITION pos = 0;
  1481. bool bFound = false;
  1482. for (pos = dataref.m_globalFriendlyNameList.GetHeadPosition (); pos;)
  1483. {
  1484. if ( !_wcsicmp (newName, dataref.m_globalFriendlyNameList.GetNext (pos)) )
  1485. {
  1486. CString caption;
  1487. CString text;
  1488. VERIFY (caption.LoadString (IDS_CERTTMPL));
  1489. // security review BryanWal 2/20/2002 ok
  1490. text.FormatMessage (IDS_FRIENDLY_NAME_ALREADY_USED, newName);
  1491. int iRetVal = 0;
  1492. VERIFY (SUCCEEDED (m_pConsole->MessageBox (text, caption,
  1493. MB_OK, &iRetVal)));
  1494. bFound = true;
  1495. break;
  1496. }
  1497. }
  1498. if ( !bFound )
  1499. {
  1500. CCertTemplate* pCertTemplate = dynamic_cast<CCertTemplate*> (pCookie);
  1501. if ( pCertTemplate )
  1502. {
  1503. hr = pCertTemplate->SetDisplayName (newName);
  1504. if ( SUCCEEDED (hr) )
  1505. {
  1506. hr = pCertTemplate->SaveChanges ();
  1507. if ( FAILED (hr) )
  1508. {
  1509. CString caption;
  1510. CString text;
  1511. VERIFY (caption.LoadString (IDS_CERTTMPL));
  1512. // security review BryanWal 2/20/2002 ok
  1513. text.FormatMessage (IDS_UNABLE_TO_SAVE_CERT_TEMPLATE_CHANGES, GetSystemMessage (hr));
  1514. int iRetVal = 0;
  1515. VERIFY (SUCCEEDED (m_pConsole->MessageBox (text, caption,
  1516. MB_ICONWARNING | MB_OK, &iRetVal)));
  1517. }
  1518. }
  1519. else
  1520. {
  1521. CString caption;
  1522. CString text;
  1523. VERIFY (caption.LoadString (IDS_CERTTMPL));
  1524. // security review BryanWal 2/20/2002 ok
  1525. text.FormatMessage (IDS_CANNOT_CHANGE_DISPLAY_NAME, hr);
  1526. int iRetVal = 0;
  1527. VERIFY (SUCCEEDED (m_pConsole->MessageBox (text, caption,
  1528. MB_ICONWARNING | MB_OK, &iRetVal)));
  1529. }
  1530. }
  1531. }
  1532. }
  1533. else
  1534. {
  1535. CString caption;
  1536. CString text;
  1537. VERIFY (caption.LoadString (IDS_CERTTMPL));
  1538. VERIFY (text.LoadString (IDS_MUST_TYPE_TEMPLATE_DISPLAY_NAME));
  1539. int iRetVal = 0;
  1540. VERIFY (SUCCEEDED (m_pConsole->MessageBox (text, caption,
  1541. MB_ICONWARNING | MB_OK, &iRetVal)));
  1542. hr = S_FALSE;
  1543. }
  1544. }
  1545. break;
  1546. default:
  1547. break;
  1548. }
  1549. }
  1550. if ( !SUCCEEDED (hr) )
  1551. hr = S_FALSE;
  1552. _TRACE(-1, L"Leaving CCertTmplComponent::OnNotifyRename: 0x%x\n", hr);
  1553. return hr;
  1554. }