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.

1174 lines
45 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997-2002.
  5. //
  6. // File: cmponent.cpp
  7. //
  8. // Contents: Implementation of CCertMgrComponent
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include <gpedit.h>
  13. #include "compdata.h" // CCertMgrComponentData
  14. #include "cmponent.h" // CCertMgrComponent
  15. #include "SaferLevel.h"
  16. #include "SaferEntry.h"
  17. #include "storeGPE.h"
  18. #include "SaferUtil.h"
  19. #include "PolicyKey.h"
  20. #ifdef _DEBUG
  21. #ifndef ALPHA
  22. #define new DEBUG_NEW
  23. #endif
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27. HKEY g_hkeyLastSaferRegistryScope = 0;
  28. extern PCWSTR pcszNEWLINE;
  29. HRESULT CCertMgrComponent::AddSaferLevels(
  30. bool bIsComputer,
  31. PCWSTR pszServerName,
  32. HKEY hGroupPolicyKey)
  33. {
  34. _TRACE (1, L"Entering CCertMgrComponent::AddSaferLevels ()\n");
  35. HRESULT hr = S_OK;
  36. CWaitCursor cursor;
  37. CCertMgrComponentData& dataRef = QueryComponentDataRef ();
  38. if ( dataRef.m_pdwSaferLevels )
  39. {
  40. for (UINT nIndex = 0;
  41. NO_MORE_SAFER_LEVELS != dataRef.m_pdwSaferLevels[nIndex] && SUCCEEDED (hr);
  42. nIndex++)
  43. {
  44. CString szLevel;
  45. switch (dataRef.m_pdwSaferLevels[nIndex])
  46. {
  47. case SAFER_LEVELID_FULLYTRUSTED:
  48. case SAFER_LEVELID_CONSTRAINED:
  49. case SAFER_LEVELID_DISALLOWED:
  50. case SAFER_LEVELID_NORMALUSER:
  51. case SAFER_LEVELID_UNTRUSTED:
  52. szLevel = SaferGetLevelFriendlyName (dataRef.m_pdwSaferLevels[nIndex],
  53. hGroupPolicyKey, bIsComputer);
  54. hr = AddLevel (szLevel, dataRef.m_pdwSaferLevels[nIndex],
  55. bIsComputer,
  56. pszServerName);
  57. break;
  58. default:
  59. ASSERT (0);
  60. _TRACE (0, L"Unexpected safer level while enumerating levels: 0x%x\n",
  61. dataRef.m_pdwSaferLevels[nIndex]);
  62. break;
  63. }
  64. }
  65. }
  66. else
  67. hr = E_FAIL;
  68. _TRACE (-1, L"Leaving CCertMgrComponent::AddSaferLevels (): 0x%x\n", hr);
  69. return hr;
  70. }
  71. HRESULT CCertMgrComponent::AddLevel (
  72. const CString& szLevel,
  73. DWORD dwLevel,
  74. bool fIsMachine,
  75. PCWSTR pszServerName)
  76. {
  77. _TRACE (1, L"Entering CCertMgrComponent::AddLevel ()\n");
  78. HRESULT hr = S_OK;
  79. CCertMgrComponentData& dataRef = QueryComponentDataRef ();
  80. CCookie& rootCookie = dataRef.QueryBaseRootCookie ();
  81. RESULTDATAITEM rdItem;
  82. // security review 2/22/2002 BryanWal ok
  83. ::ZeroMemory (&rdItem, sizeof (rdItem));
  84. rdItem.mask = RDI_STR | RDI_PARAM | RDI_IMAGE;
  85. rdItem.nCol = 0;
  86. CSaferLevel* pNewLevel = new CSaferLevel (
  87. dwLevel,
  88. fIsMachine,
  89. pszServerName,
  90. szLevel,
  91. dataRef.m_pGPEInformation,
  92. fIsMachine ?
  93. dataRef.m_rsopObjectArrayComputer :
  94. dataRef.m_rsopObjectArrayUser);
  95. if ( pNewLevel )
  96. {
  97. if ( pNewLevel->IsDefault () )
  98. {
  99. rdItem.nImage = iIconDefaultSaferLevel;
  100. dataRef.m_dwDefaultSaferLevel = pNewLevel->GetLevel ();
  101. }
  102. else
  103. rdItem.nImage = iIconSaferLevel;
  104. rootCookie.m_listResultCookieBlocks.AddHead (pNewLevel);
  105. rdItem.str = MMC_TEXTCALLBACK ;
  106. rdItem.lParam = (LPARAM) pNewLevel;
  107. pNewLevel->m_resultDataID = m_pResultData;
  108. hr = m_pResultData->InsertItem (&rdItem);
  109. if ( FAILED (hr) )
  110. {
  111. _TRACE (0, L"IResultData::InsertItem () failed: 0x%x\n", hr);
  112. }
  113. }
  114. else
  115. {
  116. hr = E_OUTOFMEMORY;
  117. }
  118. _TRACE (-1, L"Leaving CCertMgrComponent::AddLevel () : 0x%x\n", hr);
  119. return hr;
  120. }
  121. HRESULT CCertMgrComponent::SaferEnumerateEntries (
  122. bool bIsComputer,
  123. CSaferEntries* pSaferEntries)
  124. {
  125. if ( !pSaferEntries )
  126. return E_POINTER;
  127. _TRACE (1, L"Entering CCertMgrComponent::SaferEnumerateEntries ()\n");
  128. HRESULT hr = S_OK;
  129. CCertMgrComponentData& dataRef = QueryComponentDataRef ();
  130. if ( dataRef.m_pGPEInformation )
  131. {
  132. CPolicyKey policyKey (dataRef.m_pGPEInformation,
  133. SAFER_HKLM_REGBASE,
  134. bIsComputer);
  135. hr = SetRegistryScope (policyKey.GetKey (), bIsComputer);
  136. if ( SUCCEEDED (hr) )
  137. {
  138. hr = SaferEnumerateNonCertEntries (policyKey.GetKey (),
  139. bIsComputer);
  140. hr = SaferEnumerateCertEntries (
  141. bIsComputer,
  142. pSaferEntries);
  143. }
  144. }
  145. else if ( dataRef.m_bIsRSOP )
  146. {
  147. hr = SaferEnumerateRSOPNonCertEntries (bIsComputer, pSaferEntries);
  148. hr = SaferEnumerateCertEntries (bIsComputer,
  149. pSaferEntries);
  150. }
  151. else
  152. hr = E_UNEXPECTED;
  153. _TRACE (-1, L"Leaving CCertMgrComponent::SaferEnumerateEntries () : 0x%x\n", hr);
  154. return hr;
  155. }
  156. HRESULT CCertMgrComponent::SaferEnumerateRSOPNonCertEntries (
  157. bool bIsComputer,
  158. CSaferEntries* pSaferEntries)
  159. {
  160. if ( !pSaferEntries )
  161. return E_POINTER;
  162. _TRACE (1, L"Entering CCertMgrComponent::SaferEnumerateRSOPNonCertEntries\n");
  163. HRESULT hr = S_OK;
  164. int nIndex = 0;
  165. CCertMgrComponentData& dataRef = QueryComponentDataRef ();
  166. CString szKeyStart (SAFER_HKLM_REGBASE);
  167. const CRSOPObjectArray* pObjectArray = bIsComputer ?
  168. dataRef.GetRSOPObjectArrayComputer () : dataRef.GetRSOPObjectArrayUser ();
  169. INT_PTR nUpperBound = pObjectArray->GetUpperBound ();
  170. szKeyStart += L"\\";
  171. szKeyStart += SAFER_CODEIDS_REGSUBKEY;
  172. // security review 2/22/2002 BryanWal ok
  173. size_t nKeyStartLen = wcslen (szKeyStart);
  174. // Skip the common text and get the level
  175. long dwLevel = 0;
  176. long dwPreviousLevel = 0;
  177. CString szLevel;
  178. CString szType; // hash, url, path
  179. SAFER_ENTRY_TYPE type = SAFER_ENTRY_TYPE_UNKNOWN; // hash, url, path
  180. SAFER_ENTRY_TYPE previousType = SAFER_ENTRY_TYPE_UNKNOWN;
  181. CString szGUID;
  182. GUID guid;
  183. // security review 2/22/2002 BryanWal ok
  184. ::ZeroMemory (&guid, sizeof (guid));
  185. bool bCreateNew = false;
  186. PSAFER_IDENTIFICATION_HEADER pCaiCommon = 0;
  187. CString szPreviousKey;
  188. CString szKey;
  189. CRSOPObject* pCurrObject = 0;
  190. while ( nUpperBound >= nIndex )
  191. {
  192. if ( pCurrObject )
  193. szPreviousKey = pCurrObject->GetRegistryKey ();
  194. pCurrObject = pObjectArray->GetAt (nIndex);
  195. if ( pCurrObject )
  196. {
  197. szKey = pCurrObject->GetRegistryKey ();
  198. // security review 2/22/2002 BryanWal ok
  199. if ( !_wcsnicmp (szKey, szKeyStart, nKeyStartLen) )
  200. {
  201. // security review 2/22/2002 BryanWal ok
  202. szKey = szKey.Right ((int) (wcslen (szKey) - (nKeyStartLen + 1)));
  203. if ( !szKey.IsEmpty () )
  204. {
  205. // get level
  206. int nPos = szKey.Find (L'\\');
  207. szLevel = szKey.Left (nPos);
  208. dwPreviousLevel = dwLevel;
  209. dwLevel = wcstol(szLevel, 0, 10);
  210. // security review 2/22/2002 BryanWal ok
  211. szKey = szKey.Right ((int)(wcslen (szKey) - (nPos + 1)));
  212. // get type
  213. nPos = szKey.Find (L'\\');
  214. szType = szKey.Left (nPos);
  215. if ( SAFER_PATHS_REGSUBKEY == szType )
  216. type = SAFER_ENTRY_TYPE_PATH;
  217. else if ( SAFER_HASHMD5_REGSUBKEY == szType )
  218. type = SAFER_ENTRY_TYPE_HASH;
  219. else if ( SAFER_SOURCEURL_REGSUBKEY == szType )
  220. type = SAFER_ENTRY_TYPE_URLZONE;
  221. else
  222. {
  223. ASSERT (0);
  224. }
  225. if ( SAFER_ENTRY_TYPE_UNKNOWN == previousType )
  226. previousType = type;
  227. // security review 2/22/2002 BryanWal ok
  228. szKey = szKey.Right ((int) (wcslen (szKey) - (nPos + 1)));
  229. // get guid
  230. if ( szKey != szGUID )
  231. {
  232. szGUID = szKey;
  233. if ( !GuidFromString (&guid, szGUID) )
  234. continue;
  235. bCreateNew = true;
  236. }
  237. else
  238. bCreateNew = false;
  239. if ( bCreateNew && pCaiCommon )
  240. {
  241. // If we were working on an old one, create the
  242. // CSaferEntry with the available information and add
  243. // it to the result pane
  244. hr = SaferFinishEntryAndAdd (previousType, pCaiCommon,
  245. bIsComputer, dwPreviousLevel, pSaferEntries,
  246. szPreviousKey);
  247. if ( SUCCEEDED (hr) )
  248. {
  249. pCaiCommon = 0;
  250. }
  251. else if ( E_OUTOFMEMORY == hr )
  252. {
  253. ::LocalFree (pCaiCommon);
  254. pCaiCommon = 0;
  255. }
  256. previousType = type;
  257. }
  258. switch (type)
  259. {
  260. case SAFER_ENTRY_TYPE_PATH:
  261. if ( bCreateNew )
  262. {
  263. ASSERT (!pCaiCommon);
  264. pCaiCommon = (PSAFER_IDENTIFICATION_HEADER)
  265. ::LocalAlloc (LPTR, sizeof (SAFER_PATHNAME_IDENTIFICATION));
  266. if ( pCaiCommon )
  267. {
  268. ((SAFER_PATHNAME_IDENTIFICATION*)pCaiCommon)->header.cbStructSize =
  269. sizeof (SAFER_PATHNAME_IDENTIFICATION);
  270. // security review 2/22/2002 BryanWal ok
  271. memcpy (&((SAFER_PATHNAME_IDENTIFICATION*)pCaiCommon)->header.IdentificationGuid,
  272. &guid, sizeof (((SAFER_PATHNAME_IDENTIFICATION*)pCaiCommon)->header.IdentificationGuid));
  273. ((SAFER_PATHNAME_IDENTIFICATION*)pCaiCommon)->header.dwIdentificationType =
  274. SaferIdentityTypeImageName;
  275. }
  276. else
  277. hr = E_OUTOFMEMORY;
  278. }
  279. if ( SUCCEEDED (hr) && pCaiCommon )
  280. {
  281. if ( SAFER_IDS_DESCRIPTION_REGVALUE == pCurrObject->GetValueName () )
  282. {
  283. BSTR bstr = 0;
  284. if ( SUCCEEDED (pCurrObject->GetBSTR (&bstr)) )
  285. {
  286. // security review 2/22/2002 BryanWal ok
  287. // security review 2/22/2002 BryanWal ok
  288. // will be null-terminated since
  289. // length of bstr is less than target
  290. // buffer
  291. if ( SysStringLen (bstr) < SAFER_MAX_DESCRIPTION_SIZE)
  292. {
  293. wcsncpy (((SAFER_PATHNAME_IDENTIFICATION*)pCaiCommon)->Description,
  294. bstr,
  295. SAFER_MAX_DESCRIPTION_SIZE);
  296. }
  297. else
  298. {
  299. ASSERT (0);
  300. }
  301. SysFreeString (bstr);
  302. }
  303. }
  304. else if ( SAFER_IDS_ITEMDATA_REGVALUE == pCurrObject->GetValueName () )
  305. {
  306. if ( MAX_PATH * sizeof (WCHAR) >= pCurrObject->GetBlobLength () )
  307. {
  308. ((SAFER_PATHNAME_IDENTIFICATION*)pCaiCommon)->ImageName =
  309. (PWCHAR) pCurrObject->GetBlob ();
  310. }
  311. else
  312. {
  313. ASSERT (0);
  314. }
  315. }
  316. else if ( SAFER_IDS_LASTMODIFIED_REGVALUE == pCurrObject->GetValueName () )
  317. {
  318. pCurrObject->GetFileTime (
  319. ((SAFER_PATHNAME_IDENTIFICATION*)pCaiCommon)->header.lastModified);
  320. }
  321. else if ( SAFER_IDS_SAFERFLAGS_REGVALUE == pCurrObject->GetValueName () )
  322. {
  323. ((SAFER_PATHNAME_IDENTIFICATION*)pCaiCommon)->dwSaferFlags =
  324. pCurrObject->GetDWORDValue ();
  325. }
  326. }
  327. break;
  328. case SAFER_ENTRY_TYPE_HASH:
  329. if ( bCreateNew )
  330. {
  331. ASSERT (!pCaiCommon);
  332. pCaiCommon = (PSAFER_IDENTIFICATION_HEADER)
  333. ::LocalAlloc (LPTR, sizeof (SAFER_HASH_IDENTIFICATION));
  334. if ( pCaiCommon )
  335. {
  336. ((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->header.cbStructSize =
  337. sizeof (SAFER_HASH_IDENTIFICATION);
  338. // security review 2/22/2002 BryanWal ok
  339. memcpy (&((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->header.IdentificationGuid,
  340. &guid, sizeof (((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->header.IdentificationGuid));
  341. ((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->header.dwIdentificationType =
  342. SaferIdentityTypeImageHash;
  343. // NTRAID# 424997 SAFER: File hash not displayed on hash rule property sheets in RSOP mode.
  344. ((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->HashSize = 16;
  345. }
  346. else
  347. hr = E_OUTOFMEMORY;
  348. }
  349. if ( SUCCEEDED (hr) && pCaiCommon && !pCurrObject->GetValueName ().IsEmpty () )
  350. {
  351. if ( SAFER_IDS_ITEMDATA_REGVALUE == pCurrObject->GetValueName () )
  352. {
  353. ASSERT (SAFER_MAX_HASH_SIZE >= pCurrObject->GetBlobLength ());
  354. // security review 2/22/2002 BryanWal ok
  355. if ( pCurrObject->GetBlobLength () <= sizeof (((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->ImageHash) )
  356. {
  357. ::ZeroMemory (&((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->ImageHash,
  358. sizeof (((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->ImageHash));
  359. memcpy (&((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->ImageHash,
  360. pCurrObject->GetBlob (),
  361. pCurrObject->GetBlobLength ());
  362. }
  363. else
  364. {
  365. ASSERT (0);
  366. }
  367. }
  368. else if ( SAFER_IDS_LASTMODIFIED_REGVALUE == pCurrObject->GetValueName () )
  369. {
  370. pCurrObject->GetFileTime (
  371. ((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->header.lastModified);
  372. }
  373. else if ( SAFER_IDS_SAFERFLAGS_REGVALUE == pCurrObject->GetValueName () )
  374. {
  375. ((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->dwSaferFlags =
  376. pCurrObject->GetDWORDValue ();
  377. }
  378. else if ( SAFER_IDS_FRIENDLYNAME_REGVALUE == pCurrObject->GetValueName () )
  379. {
  380. BSTR bstr = 0;
  381. if ( SUCCEEDED (pCurrObject->GetBSTR (&bstr)) )
  382. {
  383. if ( SysStringLen (bstr) < SAFER_MAX_FRIENDLYNAME_SIZE )
  384. {
  385. // security review 2/22/2002 BryanWal ok
  386. // will be null-terminated since
  387. // length of bstr is less than target
  388. // buffer
  389. wcsncpy (((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->FriendlyName,
  390. bstr,
  391. SAFER_MAX_FRIENDLYNAME_SIZE);
  392. }
  393. else
  394. {
  395. ASSERT (0);
  396. }
  397. SysFreeString (bstr);
  398. }
  399. }
  400. else if ( SAFER_IDS_HASHALG_REGVALUE == pCurrObject->GetValueName () )
  401. {
  402. // security review 2/22/2002 BryanWal ok
  403. ASSERT (sizeof (((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->HashAlgorithm)
  404. == pCurrObject->GetBlobLength ());
  405. if ( sizeof (((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->HashAlgorithm)
  406. == pCurrObject->GetBlobLength () )
  407. {
  408. memcpy (&((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->HashAlgorithm,
  409. pCurrObject->GetBlob (),
  410. pCurrObject->GetBlobLength ());
  411. }
  412. }
  413. else if ( SAFER_IDS_ITEMSIZE_REGVALUE == pCurrObject->GetValueName () )
  414. {
  415. // security review 2/22/2002 BryanWal ok
  416. ASSERT (sizeof (((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->ImageSize)
  417. == pCurrObject->GetBlobLength ());
  418. if ( sizeof (((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->ImageSize)
  419. == pCurrObject->GetBlobLength () )
  420. {
  421. memcpy (&((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->ImageSize,
  422. pCurrObject->GetBlob (),
  423. pCurrObject->GetBlobLength ());
  424. }
  425. }
  426. else if ( SAFER_IDS_DESCRIPTION_REGVALUE == pCurrObject->GetValueName () )
  427. {
  428. BSTR bstr = 0;
  429. if ( SUCCEEDED (pCurrObject->GetBSTR (&bstr)) )
  430. {
  431. // security review 2/22/2002 BryanWal ok
  432. // security review 2/22/2002 BryanWal ok
  433. // will be null-terminated since
  434. // length of bstr is less than target
  435. // buffer
  436. if ( SysStringLen (bstr) < SAFER_MAX_DESCRIPTION_SIZE )
  437. {
  438. wcsncpy (((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->Description,
  439. bstr,
  440. SAFER_MAX_DESCRIPTION_SIZE);
  441. }
  442. else
  443. {
  444. ASSERT (0);
  445. }
  446. SysFreeString (bstr);
  447. }
  448. }
  449. else if ( SAFER_VALUE_NAME_HASH_SIZE == pCurrObject->GetValueName () )
  450. {
  451. ASSERT (sizeof (LARGE_INTEGER) == pCurrObject->GetBlobLength ());
  452. if ( sizeof (LARGE_INTEGER) == pCurrObject->GetBlobLength () )
  453. {
  454. // security review 2/22/2002 BryanWal ok
  455. memcpy (&((SAFER_HASH_IDENTIFICATION*)pCaiCommon)->HashSize,
  456. pCurrObject->GetBlob (),
  457. pCurrObject->GetBlobLength ());
  458. }
  459. else
  460. {
  461. ASSERT (0);
  462. }
  463. }
  464. }
  465. break;
  466. case SAFER_ENTRY_TYPE_URLZONE:
  467. if ( bCreateNew )
  468. {
  469. ASSERT (!pCaiCommon);
  470. pCaiCommon = (PSAFER_IDENTIFICATION_HEADER)
  471. ::LocalAlloc (LPTR, sizeof (SAFER_URLZONE_IDENTIFICATION));
  472. if ( pCaiCommon )
  473. {
  474. ((SAFER_URLZONE_IDENTIFICATION*)pCaiCommon)->header.cbStructSize =
  475. sizeof (SAFER_URLZONE_IDENTIFICATION);
  476. // security review 2/22/2002 BryanWal ok
  477. memcpy (&((SAFER_URLZONE_IDENTIFICATION*)pCaiCommon)->header.IdentificationGuid,
  478. &guid, sizeof (GUID));
  479. ((SAFER_URLZONE_IDENTIFICATION*)pCaiCommon)->header.dwIdentificationType =
  480. SaferIdentityTypeUrlZone;
  481. }
  482. else
  483. hr = E_OUTOFMEMORY;
  484. }
  485. if ( SUCCEEDED (hr) && pCaiCommon )
  486. {
  487. if ( SAFER_IDS_ITEMDATA_REGVALUE == pCurrObject->GetValueName () )
  488. {
  489. if ( pCurrObject->GetBlobLength () == sizeof (((SAFER_URLZONE_IDENTIFICATION*)pCaiCommon)->UrlZoneId) )
  490. {
  491. // security review 2/22/2002 BryanWal ok
  492. memcpy (&((SAFER_URLZONE_IDENTIFICATION*)pCaiCommon)->UrlZoneId,
  493. pCurrObject->GetBlob (),
  494. pCurrObject->GetBlobLength ());
  495. }
  496. else
  497. {
  498. ASSERT (0);
  499. }
  500. #if DBG
  501. SAFER_URLZONE_IDENTIFICATION* pCaiUrlZone =
  502. (SAFER_URLZONE_IDENTIFICATION*) pCaiCommon;
  503. ASSERT (URLZONE_LOCAL_MACHINE == pCaiUrlZone->UrlZoneId ||
  504. URLZONE_INTRANET == pCaiUrlZone->UrlZoneId ||
  505. URLZONE_TRUSTED == pCaiUrlZone->UrlZoneId ||
  506. URLZONE_INTERNET == pCaiUrlZone->UrlZoneId ||
  507. URLZONE_UNTRUSTED == pCaiUrlZone->UrlZoneId);
  508. #endif DBG
  509. }
  510. else if ( SAFER_IDS_LASTMODIFIED_REGVALUE == pCurrObject->GetValueName () )
  511. {
  512. pCurrObject->GetFileTime (
  513. ((SAFER_URLZONE_IDENTIFICATION*)pCaiCommon)->header.lastModified);
  514. }
  515. else if ( SAFER_IDS_SAFERFLAGS_REGVALUE == pCurrObject->GetValueName () )
  516. {
  517. ((SAFER_URLZONE_IDENTIFICATION*)pCaiCommon)->dwSaferFlags =
  518. pCurrObject->GetDWORDValue ();
  519. }
  520. }
  521. break;
  522. default:
  523. ASSERT (0);
  524. break;
  525. }
  526. }
  527. }
  528. }
  529. else
  530. break;
  531. nIndex++;
  532. }
  533. if ( pCaiCommon )
  534. {
  535. // If we were working on an old one, create the
  536. // CSaferEntry with the available information and add
  537. // it to the result pane
  538. hr = SaferFinishEntryAndAdd (previousType, pCaiCommon,
  539. bIsComputer, dwLevel, pSaferEntries,
  540. szPreviousKey);
  541. if ( SUCCEEDED (hr) )
  542. {
  543. pCaiCommon = 0;
  544. }
  545. else if ( E_OUTOFMEMORY == hr )
  546. {
  547. ::LocalFree (pCaiCommon);
  548. pCaiCommon = 0;
  549. }
  550. }
  551. _TRACE (-1, L"Leaving CCertMgrComponent::SaferEnumerateRSOPNonCertEntries: 0x%x\n", hr);
  552. return hr;
  553. }
  554. HRESULT CCertMgrComponent::SaferFinishEntryAndAdd (SAFER_ENTRY_TYPE previousType,
  555. PSAFER_IDENTIFICATION_HEADER pCaiCommon,
  556. bool bIsComputer,
  557. long dwLevel,
  558. CSaferEntries* pSaferEntries,
  559. const CString& szPreviousKey)
  560. {
  561. HRESULT hr = S_OK;
  562. CString szSubjectName;
  563. switch (previousType)
  564. {
  565. case SAFER_ENTRY_TYPE_PATH:
  566. szSubjectName = ((SAFER_PATHNAME_IDENTIFICATION*)pCaiCommon)->ImageName;
  567. break;
  568. case SAFER_ENTRY_TYPE_HASH:
  569. szSubjectName = ((PSAFER_HASH_IDENTIFICATION) (pCaiCommon))->FriendlyName;
  570. szSubjectName.Replace (pcszNEWLINE, L" ");
  571. break;
  572. case SAFER_ENTRY_TYPE_URLZONE:
  573. szSubjectName = GetURLZoneFriendlyName (
  574. ((SAFER_URLZONE_IDENTIFICATION*) pCaiCommon)->UrlZoneId);
  575. break;
  576. default:
  577. ASSERT (0);
  578. break;
  579. }
  580. ASSERT (pCaiCommon);
  581. if ( pCaiCommon )
  582. {
  583. hr = InsertNewSaferEntry (
  584. previousType,
  585. bIsComputer,
  586. szSubjectName,
  587. pCaiCommon,
  588. dwLevel,
  589. pSaferEntries,
  590. QueryComponentDataRef ().m_pGPEInformation,
  591. 0,
  592. szPreviousKey);
  593. }
  594. return hr;
  595. }
  596. HRESULT CCertMgrComponent::InsertNewSaferEntry (
  597. SAFER_ENTRY_TYPE type,
  598. bool bIsComputer,
  599. PCWSTR pwcszObjectName,
  600. PSAFER_IDENTIFICATION_HEADER pCaiCommon,
  601. DWORD dwLevel,
  602. CSaferEntries* pSaferEntries,
  603. IGPEInformation* pGPEInformation,
  604. CCertificate* pCert,
  605. PCWSTR pszRSOPRegistryKey)
  606. {
  607. _TRACE (1, L"Entering CCertMgrComponent::InsertNewSaferEntry (%s)\n", pwcszObjectName);
  608. HRESULT hr = S_OK;
  609. switch (type)
  610. {
  611. case SAFER_ENTRY_TYPE_PATH:
  612. ASSERT (pCaiCommon &&
  613. SaferIdentityTypeImageName == pCaiCommon->dwIdentificationType);
  614. break;
  615. case SAFER_ENTRY_TYPE_HASH:
  616. ASSERT (pCaiCommon &&
  617. SaferIdentityTypeImageHash == pCaiCommon->dwIdentificationType);
  618. break;
  619. case SAFER_ENTRY_TYPE_URLZONE:
  620. ASSERT (pCaiCommon &&
  621. SaferIdentityTypeUrlZone == pCaiCommon->dwIdentificationType);
  622. break;
  623. case SAFER_ENTRY_TYPE_CERT:
  624. ASSERT (pCert);
  625. break;
  626. default:
  627. ASSERT (0);
  628. break;
  629. }
  630. CSaferEntry* pNewEntry = new CSaferEntry (
  631. type,
  632. bIsComputer,
  633. L"",
  634. pwcszObjectName,
  635. pCaiCommon,
  636. dwLevel,
  637. pGPEInformation,
  638. pCert,
  639. pSaferEntries,
  640. bIsComputer ? QueryComponentDataRef ().m_rsopObjectArrayComputer :
  641. QueryComponentDataRef ().m_rsopObjectArrayUser,
  642. pszRSOPRegistryKey);
  643. if ( pNewEntry )
  644. {
  645. RESULTDATAITEM rdItem;
  646. // security review 2/22/2002 BryanWal ok
  647. ::ZeroMemory (&rdItem, sizeof (rdItem));
  648. rdItem.mask = RDI_STR | RDI_IMAGE | RDI_PARAM;
  649. switch (type)
  650. {
  651. case SAFER_ENTRY_TYPE_HASH:
  652. rdItem.nImage = iIconSaferHashEntry;
  653. break;
  654. case SAFER_ENTRY_TYPE_PATH:
  655. rdItem.nImage = iIconSaferNameEntry;
  656. break;
  657. case SAFER_ENTRY_TYPE_CERT:
  658. rdItem.nImage = iIconSaferCertEntry;
  659. break;
  660. case SAFER_ENTRY_TYPE_URLZONE:
  661. rdItem.nImage = iIconSaferURLEntry;
  662. break;
  663. default:
  664. ASSERT (0);
  665. break;
  666. }
  667. rdItem.nCol = 0;
  668. rdItem.str = MMC_TEXTCALLBACK;
  669. QueryComponentDataRef ().QueryBaseRootCookie ().m_listResultCookieBlocks.AddHead (pNewEntry);
  670. rdItem.lParam = (LPARAM) pNewEntry;
  671. pNewEntry->m_resultDataID = m_pResultData;
  672. hr = m_pResultData->InsertItem (&rdItem);
  673. if ( FAILED (hr) )
  674. {
  675. _TRACE (0, L"IResultData::InsertItem () failed: 0x%x\n", hr);
  676. }
  677. }
  678. else
  679. hr = E_OUTOFMEMORY;
  680. _TRACE (-1, L"Leaving CCertMgrComponent::InsertNewSaferEntry (%s): 0x%x\n", pwcszObjectName, hr);
  681. return hr;
  682. }
  683. HRESULT CCertMgrComponent::SaferEnumerateNonCertEntries (
  684. HKEY hGroupPolicyKey,
  685. bool bIsComputer)
  686. {
  687. _TRACE (1, L"Entering CCertMgrComponent::SaferEnumerateNonCertEntries ()\n");
  688. ASSERT (0 != g_hkeyLastSaferRegistryScope);
  689. HRESULT hr = S_OK;
  690. DWORD* pdwLevels = 0;
  691. DWORD cbBuffer = 0;
  692. SetRegistryScope (hGroupPolicyKey, bIsComputer);
  693. BOOL bRVal = SaferGetPolicyInformation (
  694. SAFER_SCOPEID_REGISTRY,
  695. SaferPolicyLevelList,
  696. cbBuffer,
  697. pdwLevels,
  698. &cbBuffer,
  699. 0);
  700. if ( !bRVal && GetLastError () == ERROR_INSUFFICIENT_BUFFER )
  701. {
  702. DWORD nLevels = cbBuffer/sizeof (DWORD);
  703. pdwLevels = new DWORD[nLevels];
  704. if ( pdwLevels )
  705. {
  706. bRVal = SaferGetPolicyInformation (
  707. SAFER_SCOPEID_REGISTRY,
  708. SaferPolicyLevelList,
  709. cbBuffer,
  710. pdwLevels,
  711. &cbBuffer,
  712. 0);
  713. ASSERT (bRVal);
  714. if ( bRVal )
  715. {
  716. for (DWORD dwIndex = 0; dwIndex < nLevels; dwIndex++)
  717. {
  718. hr = SaferEnumerateEntriesAtLevel (
  719. bIsComputer,
  720. hGroupPolicyKey,
  721. pdwLevels[dwIndex]);
  722. }
  723. }
  724. else
  725. {
  726. DWORD dwErr = GetLastError ();
  727. hr = HRESULT_FROM_WIN32 (dwErr);
  728. _TRACE (0, L"SaferGetPolicyInformation (SAFER_SCOPEID_REGISTRY, SaferPolicyLevelList) failed: %d\n",
  729. dwErr);
  730. }
  731. delete [] pdwLevels;
  732. }
  733. else
  734. hr = E_OUTOFMEMORY;
  735. }
  736. else if ( !bRVal )
  737. {
  738. ASSERT (0);
  739. DWORD dwErr = GetLastError ();
  740. hr = HRESULT_FROM_WIN32 (dwErr);
  741. _TRACE (0, L"SaferGetPolicyInformation (SAFER_SCOPEID_REGISTRY, SaferPolicyLevelList) failed: %d\n",
  742. dwErr);
  743. }
  744. _TRACE (-1, L"Leaving CCertMgrComponent::SaferEnumerateNonCertEntries (): 0x%x\n", hr);
  745. return hr;
  746. }
  747. HRESULT CCertMgrComponent::SaferEnumerateCertEntries (
  748. bool bIsComputer,
  749. CSaferEntries* pSaferEntries)
  750. {
  751. if ( !pSaferEntries )
  752. return E_POINTER;
  753. _TRACE (1, L"Entering CCertMgrComponent::SaferEnumerateCertEntries ()\n");
  754. HRESULT hr = S_OK;
  755. CCertStore* pTrustedPublisherStore = 0;
  756. hr = pSaferEntries->GetTrustedPublishersStore (&pTrustedPublisherStore);
  757. if ( pTrustedPublisherStore )
  758. {
  759. hr = EnumSaferCertificates (
  760. bIsComputer,
  761. *pTrustedPublisherStore,
  762. pSaferEntries);
  763. if ( SUCCEEDED (hr) )
  764. {
  765. m_currResultNodeType = bIsComputer ?
  766. CERTMGR_SAFER_COMPUTER_ENTRY : CERTMGR_SAFER_USER_ENTRY;
  767. m_pResultData->Sort (m_nSelectedSaferEntryColumn, 0,
  768. (long) m_currResultNodeType);
  769. }
  770. pTrustedPublisherStore->Release ();
  771. }
  772. else
  773. hr = E_OUTOFMEMORY;
  774. CCertStore* pDisallowedStore = 0;
  775. hr = pSaferEntries->GetDisallowedStore (&pDisallowedStore);
  776. if ( pDisallowedStore )
  777. {
  778. hr = EnumSaferCertificates (
  779. bIsComputer,
  780. *pDisallowedStore,
  781. pSaferEntries);
  782. if ( SUCCEEDED (hr) )
  783. {
  784. m_currResultNodeType = bIsComputer ?
  785. CERTMGR_SAFER_COMPUTER_ENTRY : CERTMGR_SAFER_USER_ENTRY;
  786. m_pResultData->Sort (m_nSelectedSaferEntryColumn, 0,
  787. (long) m_currResultNodeType);
  788. }
  789. pDisallowedStore->Release ();
  790. pDisallowedStore = 0;
  791. }
  792. else
  793. hr = E_OUTOFMEMORY;
  794. _TRACE (-1, L"Leaving CCertMgrComponent::SaferEnumerateCertEntries (): 0x%x\n", hr);
  795. return hr;
  796. }
  797. HRESULT CCertMgrComponent::EnumSaferCertificates (
  798. bool bIsComputer,
  799. CCertStore& rCertStore,
  800. CSaferEntries* pSaferEntries)
  801. {
  802. if ( !pSaferEntries )
  803. return E_POINTER;
  804. _TRACE (1, L"Entering CCertMgrComponent::EnumCertificates\n");
  805. CWaitCursor cursor;
  806. PCCERT_CONTEXT pCertContext = 0;
  807. HRESULT hr = 0;
  808. CCertificate* pCert = 0;
  809. DWORD dwLevelID = SAFER_TRUSTED_PUBLISHER_STORE == rCertStore.GetStoreType () ?
  810. SAFER_LEVELID_FULLYTRUSTED : SAFER_LEVELID_DISALLOWED;
  811. // Iterate through the list of certificates in the system store,
  812. // allocate new certificates with the CERT_CONTEXT returned,
  813. // and store them in the certificate list.
  814. while ( 1 )
  815. {
  816. pCertContext = rCertStore.EnumCertificates (pCertContext);
  817. if ( !pCertContext )
  818. break;
  819. pCert =
  820. new CCertificate (pCertContext, &rCertStore);
  821. if ( pCert )
  822. {
  823. hr = InsertNewSaferEntry (SAFER_ENTRY_TYPE_CERT,
  824. bIsComputer,
  825. pCert->GetSubjectName (),
  826. 0,
  827. dwLevelID,
  828. pSaferEntries,
  829. QueryComponentDataRef ().m_pGPEInformation,
  830. pCert);
  831. if ( FAILED (hr) )
  832. break;
  833. pCert->Release ();
  834. }
  835. else
  836. {
  837. hr = E_OUTOFMEMORY;
  838. break;
  839. }
  840. }
  841. rCertStore.Close ();
  842. _TRACE (-1, L"Leaving CCertMgrComponent::EnumCertificates: 0x%x\n", hr);
  843. return hr;
  844. }
  845. HRESULT CCertMgrComponent::SaferEnumerateEntriesAtLevel (
  846. bool bIsComputer,
  847. HKEY hGroupPolicyKey,
  848. DWORD dwLevel)
  849. {
  850. _TRACE (1, L"Entering CCertMgrComponent::SaferEnumerateEntriesAtLevel (%d)\n", dwLevel);
  851. SetRegistryScope (hGroupPolicyKey, bIsComputer);
  852. HRESULT hr = S_OK;
  853. SAFER_LEVEL_HANDLE hLevel = 0;
  854. BOOL bRVal = SaferCreateLevel (SAFER_SCOPEID_REGISTRY,
  855. dwLevel,
  856. SAFER_LEVEL_OPEN,
  857. &hLevel,
  858. hGroupPolicyKey);
  859. ASSERT (bRVal);
  860. if ( bRVal )
  861. {
  862. DWORD dwBufferSize = 0;
  863. bRVal = SaferGetLevelInformation(hLevel,
  864. SaferObjectAllIdentificationGuids,
  865. 0,
  866. dwBufferSize,
  867. &dwBufferSize);
  868. if ( !bRVal && ERROR_INSUFFICIENT_BUFFER == GetLastError () )
  869. {
  870. DWORD nGuids = dwBufferSize/sizeof (GUID);
  871. GUID* pGuids = new GUID[nGuids];
  872. if ( pGuids )
  873. {
  874. bRVal = SaferGetLevelInformation(hLevel,
  875. SaferObjectAllIdentificationGuids,
  876. pGuids,
  877. dwBufferSize,
  878. &dwBufferSize);
  879. ASSERT (bRVal);
  880. if ( bRVal )
  881. {
  882. for (DWORD dwIndex = 0; dwIndex < nGuids; dwIndex++)
  883. {
  884. hr = SaferGetSingleEntry (bIsComputer, hLevel,
  885. pGuids[dwIndex], dwLevel);
  886. }
  887. }
  888. else
  889. {
  890. DWORD dwErr = GetLastError ();
  891. hr = HRESULT_FROM_WIN32 (dwErr);
  892. _TRACE (0, L"SaferGetLevelInformation (SaferObjectAllIdentificationGuids) failed: %d\n",
  893. dwErr);
  894. }
  895. delete [] pGuids;
  896. }
  897. else
  898. hr = E_OUTOFMEMORY;
  899. }
  900. else if ( !bRVal )
  901. {
  902. DWORD dwErr = GetLastError ();
  903. ASSERT (ERROR_NOT_FOUND == dwErr);
  904. hr = HRESULT_FROM_WIN32 (dwErr);
  905. _TRACE (0, L"SaferGetLevelInformation (SaferObjectAllIdentificationGuids) failed: %d\n",
  906. dwErr);
  907. }
  908. VERIFY (SaferCloseLevel (hLevel));
  909. }
  910. else
  911. {
  912. DWORD dwErr = GetLastError ();
  913. hr = HRESULT_FROM_WIN32 (dwErr);
  914. _TRACE (0, L"SaferCreateLevel (SAFER_SCOPEID_REGISTRY, 0x%x, SAFER_LEVEL_OPEN) failed: %d\n",
  915. dwLevel, dwErr);
  916. }
  917. _TRACE (-1, L"Leaving CCertMgrComponent::SaferEnumerateEntriesAtLevel () : 0x%x\n", hr);
  918. return hr;
  919. }
  920. HRESULT CCertMgrComponent::SaferGetSingleEntry(
  921. bool bIsComputer,
  922. SAFER_LEVEL_HANDLE hLevel,
  923. GUID &rEntryGuid,
  924. DWORD dwLevelID)
  925. {
  926. _TRACE (1, L"Entering CCertMgrComponent::SaferGetSingleEntry ()\n");
  927. ASSERT (0 != g_hkeyLastSaferRegistryScope);
  928. HRESULT hr = S_OK;
  929. DWORD dwBufferSize = sizeof (SAFER_IDENTIFICATION_HEADER);
  930. SAFER_IDENTIFICATION_HEADER caiCommon;
  931. // security review 2/22/2002 BryanWal ok
  932. ::ZeroMemory (&caiCommon, sizeof (caiCommon));
  933. caiCommon.cbStructSize = dwBufferSize;
  934. // security review 2/22/2002 BryanWal ok
  935. memcpy (&caiCommon.IdentificationGuid, &rEntryGuid, sizeof (GUID));
  936. BOOL bRVal = SaferGetLevelInformation(hLevel,
  937. SaferObjectSingleIdentification,
  938. &caiCommon,
  939. dwBufferSize,
  940. &dwBufferSize);
  941. if ( !bRVal && ERROR_INSUFFICIENT_BUFFER == GetLastError () )
  942. {
  943. PBYTE pBytes = (PBYTE) LocalAlloc (LPTR, dwBufferSize);
  944. if ( pBytes )
  945. {
  946. SAFER_ENTRY_TYPE saferEntryType = SAFER_ENTRY_TYPE_UNKNOWN;
  947. PSAFER_IDENTIFICATION_HEADER pCommon = (PSAFER_IDENTIFICATION_HEADER) pBytes;
  948. pCommon->cbStructSize = dwBufferSize;
  949. // security review 2/22/2002 BryanWal ok
  950. memcpy (&pCommon->IdentificationGuid, &rEntryGuid, sizeof (GUID));
  951. bRVal = SaferGetLevelInformation(hLevel,
  952. SaferObjectSingleIdentification,
  953. pBytes,
  954. dwBufferSize,
  955. &dwBufferSize);
  956. ASSERT (bRVal);
  957. if ( bRVal )
  958. {
  959. CString szObjectName;
  960. switch (pCommon->dwIdentificationType)
  961. {
  962. case SaferIdentityTypeImageName:
  963. szObjectName = ((PSAFER_PATHNAME_IDENTIFICATION) (pCommon))->ImageName;
  964. _TRACE (0, L"Reading safer path entry: %s\n", ((PSAFER_PATHNAME_IDENTIFICATION) (pCommon))->ImageName);
  965. saferEntryType = SAFER_ENTRY_TYPE_PATH;
  966. break;
  967. case SaferIdentityTypeImageHash:
  968. szObjectName = ((PSAFER_HASH_IDENTIFICATION) (pCommon))->FriendlyName;
  969. // replace new-line characters with spaces
  970. szObjectName.Replace (pcszNEWLINE, L" ");
  971. saferEntryType = SAFER_ENTRY_TYPE_HASH;
  972. break;
  973. case SaferIdentityTypeUrlZone:
  974. szObjectName = GetURLZoneFriendlyName (
  975. ((PSAFER_URLZONE_IDENTIFICATION) (pCommon))->UrlZoneId);
  976. saferEntryType = SAFER_ENTRY_TYPE_URLZONE;
  977. break;
  978. default:
  979. ASSERT (0);
  980. break;
  981. }
  982. hr = InsertNewSaferEntry (
  983. saferEntryType,
  984. bIsComputer,
  985. szObjectName,
  986. pCommon,
  987. dwLevelID,
  988. 0,
  989. QueryComponentDataRef ().m_pGPEInformation,
  990. 0);
  991. if ( E_OUTOFMEMORY == hr )
  992. ::LocalFree (pBytes);
  993. }
  994. else
  995. {
  996. DWORD dwErr = GetLastError ();
  997. hr = HRESULT_FROM_WIN32 (dwErr);
  998. _TRACE (0, L"SaferGetLevelInformation (SaferObjectSingleIdentification) failed: %d\n",
  999. dwErr);
  1000. }
  1001. // pBytes is freed in ~CSaferEntry
  1002. }
  1003. else
  1004. hr = E_OUTOFMEMORY;
  1005. }
  1006. else if ( !bRVal )
  1007. {
  1008. ASSERT (0);
  1009. DWORD dwErr = GetLastError ();
  1010. hr = HRESULT_FROM_WIN32 (dwErr);
  1011. _TRACE (0, L"SaferGetLevelInformation (SaferObjectSingleIdentification) failed: %d\n",
  1012. dwErr);
  1013. }
  1014. _TRACE (-1, L"Leaving CCertMgrComponent::SaferGetSingleEntry (): 0x%x\n", hr);
  1015. return hr;
  1016. }
  1017. HRESULT CCertMgrComponent::DeleteSaferEntryFromResultPane (
  1018. CSaferEntry * pSaferEntry,
  1019. LPDATAOBJECT pDataObject,
  1020. bool bDoCommit)
  1021. {
  1022. _TRACE (1, L"Entering CCertMgrComponent::DeleteSaferEntryFromResultPane\n");
  1023. HRESULT hr = S_OK;
  1024. hr = pSaferEntry->Delete (bDoCommit);
  1025. if ( SUCCEEDED (hr) )
  1026. {
  1027. HRESULTITEM itemID = 0;
  1028. hr = m_pResultData->FindItemByLParam ( (LPARAM) pSaferEntry, &itemID);
  1029. if ( SUCCEEDED (hr) )
  1030. {
  1031. hr = m_pResultData->DeleteItem (itemID, 0);
  1032. }
  1033. // If we can't succeed in removing this one item, then update the whole panel.
  1034. if ( !SUCCEEDED (hr) )
  1035. {
  1036. hr = m_pConsole->UpdateAllViews (pDataObject, 0, 0);
  1037. }
  1038. }
  1039. else
  1040. {
  1041. CString text;
  1042. CString caption;
  1043. VERIFY (caption.LoadString (IDS_SAFER_WINDOWS_NODE_NAME));
  1044. // security review 2/22/2002 BryanWal ok
  1045. text.FormatMessage (IDS_CANT_DELETE_SAFER_ENTRY, GetSystemMessage (hr));
  1046. int iRetVal = 0;
  1047. VERIFY (SUCCEEDED (m_pConsole->MessageBox (text, caption,
  1048. MB_OK, &iRetVal)));
  1049. }
  1050. _TRACE (-1, L"Leaving CCertMgrComponent::DeleteSaferEntryFromResultPane: 0x%x\n", hr);
  1051. return hr;
  1052. }
  1053. HRESULT CCertMgrComponent::OnNotifyCanPasteOutOfProc (LPBOOL pbCanHandle)
  1054. {
  1055. if ( !pbCanHandle )
  1056. return E_POINTER;
  1057. CCertMgrComponentData& dataRef = QueryComponentDataRef ();
  1058. CLSID clsID;
  1059. if ( SUCCEEDED (dataRef.GetClassID (&clsID)) )
  1060. {
  1061. if ( CLSID_SaferWindowsExtension == clsID )
  1062. *pbCanHandle = TRUE;
  1063. }
  1064. return S_OK; // snapins should redefine this
  1065. }