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.

758 lines
23 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000-2002.
  5. //
  6. // File: SaferUtil.cpp
  7. //
  8. // Contents: Utility methods for Software Restriction Policies extension
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include <gpedit.h>
  13. #include <wintrust.h>
  14. #include <crypto\wintrustp.h>
  15. #include <softpub.h>
  16. #include "SaferUtil.h"
  17. #include "SaferEntry.h"
  18. #include <winsaferp.h>
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. extern HKEY g_hkeyLastSaferRegistryScope;
  25. bool g_bIsComputer = false;
  26. extern GUID g_guidExtension;
  27. extern GUID g_guidRegExt;
  28. extern GUID g_guidSnapin;
  29. void InitializeSecurityLevelComboBox (
  30. CComboBox& comboBox,
  31. bool bLimit,
  32. DWORD dwLevelID,
  33. HKEY hGroupPolicyKey,
  34. DWORD* pdwSaferLevels,
  35. bool bIsComputer)
  36. {
  37. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  38. if ( !hGroupPolicyKey ) // is RSOP
  39. {
  40. CString szText = SaferGetLevelFriendlyName (dwLevelID,
  41. hGroupPolicyKey, bIsComputer);
  42. int nItem = comboBox.AddString (szText);
  43. ASSERT (nItem >= 0);
  44. if ( nItem >= 0 )
  45. {
  46. VERIFY (CB_ERR != comboBox.SetItemData (nItem, dwLevelID));
  47. VERIFY (CB_ERR != comboBox.SetCurSel (nItem));
  48. }
  49. return;
  50. }
  51. if ( pdwSaferLevels )
  52. {
  53. for (UINT nIndex = 0;
  54. NO_MORE_SAFER_LEVELS != pdwSaferLevels[nIndex];
  55. nIndex++)
  56. {
  57. CString szText;
  58. int nItem = 0;
  59. switch (pdwSaferLevels[nIndex])
  60. {
  61. case SAFER_LEVELID_FULLYTRUSTED:
  62. szText = SaferGetLevelFriendlyName (pdwSaferLevels[nIndex],
  63. hGroupPolicyKey, bIsComputer);
  64. nItem = comboBox.AddString (szText);
  65. ASSERT (nItem >= 0);
  66. if ( nItem >= 0 )
  67. {
  68. VERIFY (CB_ERR != comboBox.SetItemData (nItem, pdwSaferLevels[nIndex]));
  69. if ( pdwSaferLevels[nIndex] == dwLevelID || AUTHZ_UNKNOWN_LEVEL == dwLevelID)
  70. VERIFY (CB_ERR != comboBox.SetCurSel (nItem));
  71. }
  72. break;
  73. case SAFER_LEVELID_CONSTRAINED:
  74. if ( !bLimit )
  75. {
  76. szText = SaferGetLevelFriendlyName (pdwSaferLevels[nIndex],
  77. hGroupPolicyKey, bIsComputer);
  78. nItem = comboBox.AddString (szText);
  79. ASSERT (nItem >= 0);
  80. if ( nItem >= 0 )
  81. {
  82. VERIFY (CB_ERR != comboBox.SetItemData (nItem, pdwSaferLevels[nIndex]));
  83. if ( pdwSaferLevels[nIndex] == dwLevelID )
  84. VERIFY (CB_ERR != comboBox.SetCurSel (nItem));
  85. }
  86. }
  87. break;
  88. case SAFER_LEVELID_DISALLOWED:
  89. szText = SaferGetLevelFriendlyName (pdwSaferLevels[nIndex],
  90. hGroupPolicyKey, bIsComputer);
  91. nItem = comboBox.AddString (szText);
  92. ASSERT (nItem >= 0);
  93. if ( nItem >= 0 )
  94. {
  95. VERIFY (CB_ERR != comboBox.SetItemData (nItem, pdwSaferLevels[nIndex]));
  96. if ( pdwSaferLevels[nIndex] == dwLevelID )
  97. VERIFY (CB_ERR != comboBox.SetCurSel (nItem));
  98. }
  99. break;
  100. case SAFER_LEVELID_NORMALUSER:
  101. case SAFER_LEVELID_UNTRUSTED:
  102. if ( !bLimit )
  103. {
  104. if ( hGroupPolicyKey )
  105. {
  106. szText = SaferGetLevelFriendlyName (pdwSaferLevels[nIndex],
  107. hGroupPolicyKey, bIsComputer);
  108. nItem = comboBox.AddString (szText);
  109. ASSERT (nItem >= 0);
  110. if ( nItem >= 0 )
  111. {
  112. VERIFY (CB_ERR != comboBox.SetItemData (nItem, pdwSaferLevels[nIndex]));
  113. if ( pdwSaferLevels[nIndex] == dwLevelID )
  114. VERIFY (CB_ERR != comboBox.SetCurSel (nItem));
  115. }
  116. }
  117. }
  118. break;
  119. default:
  120. ASSERT (0);
  121. break;
  122. }
  123. }
  124. }
  125. }
  126. class CLevelPair {
  127. public:
  128. CLevelPair () :
  129. m_dwLevelID ((DWORD) -1)
  130. {
  131. }
  132. virtual ~CLevelPair () {}
  133. DWORD m_dwLevelID;
  134. CString m_szLevelName;
  135. };
  136. CString SaferGetLevelFriendlyName (DWORD dwLevelID, HKEY hGroupPolicyKey, const bool bIsComputer)
  137. {
  138. CString szLevelName;
  139. SAFER_LEVEL_HANDLE hLevel = 0;
  140. BOOL bRVal = FALSE;
  141. const int NUM_LEVEL_PAIRS = 10;
  142. static CLevelPair levelPairs[NUM_LEVEL_PAIRS];
  143. for (int nLevelIndex = 0; nLevelIndex < NUM_LEVEL_PAIRS; nLevelIndex++)
  144. {
  145. if ( -1 == levelPairs[nLevelIndex].m_dwLevelID )
  146. break;
  147. else if ( dwLevelID == levelPairs[nLevelIndex].m_dwLevelID )
  148. {
  149. return levelPairs[nLevelIndex].m_szLevelName;
  150. }
  151. }
  152. if ( hGroupPolicyKey )
  153. {
  154. if ( !g_hkeyLastSaferRegistryScope )
  155. SetRegistryScope (hGroupPolicyKey, bIsComputer);
  156. bRVal = SaferCreateLevel (SAFER_SCOPEID_REGISTRY,
  157. dwLevelID,
  158. SAFER_LEVEL_OPEN,
  159. &hLevel,
  160. hGroupPolicyKey);
  161. }
  162. else
  163. {
  164. bRVal = SaferCreateLevel (SAFER_SCOPEID_MACHINE,
  165. dwLevelID,
  166. SAFER_LEVEL_OPEN,
  167. &hLevel,
  168. 0);
  169. }
  170. if ( bRVal )
  171. {
  172. DWORD dwBufferSize = 0;
  173. DWORD dwErr = 0;
  174. bRVal = SaferGetLevelInformation(hLevel,
  175. SaferObjectFriendlyName,
  176. 0,
  177. dwBufferSize,
  178. &dwBufferSize);
  179. if ( !bRVal && ERROR_INSUFFICIENT_BUFFER == GetLastError () )
  180. {
  181. PWSTR pszLevelName = (PWSTR) LocalAlloc (LPTR, dwBufferSize);
  182. if ( pszLevelName )
  183. {
  184. bRVal = SaferGetLevelInformation(hLevel,
  185. SaferObjectFriendlyName,
  186. pszLevelName,
  187. dwBufferSize,
  188. &dwBufferSize);
  189. ASSERT (bRVal);
  190. if ( bRVal )
  191. {
  192. szLevelName = pszLevelName;
  193. }
  194. else
  195. {
  196. dwErr = GetLastError ();
  197. _TRACE (0, L"SaferGetLevelInformation(SaferObjectFriendlyName) failed: %d\n",
  198. dwErr);
  199. }
  200. LocalFree (pszLevelName);
  201. }
  202. }
  203. else if ( !bRVal )
  204. {
  205. dwErr = GetLastError ();
  206. _TRACE (0, L"SaferGetLevelInformation(SaferObjectFriendlyName) failed: %d\n",
  207. dwErr);
  208. }
  209. VERIFY (SaferCloseLevel (hLevel));
  210. }
  211. else
  212. {
  213. DWORD dwErr = GetLastError ();
  214. _TRACE (0, L"SaferCloseLevel (SAFER_SCOPEID_REGISTRY, 0x%x, SAFER_LEVEL_OPEN) failed: %d\n",
  215. dwLevelID, dwErr);
  216. }
  217. if ( nLevelIndex < NUM_LEVEL_PAIRS && !szLevelName.IsEmpty () )
  218. {
  219. levelPairs[nLevelIndex].m_dwLevelID = dwLevelID;
  220. levelPairs[nLevelIndex].m_szLevelName = szLevelName;
  221. }
  222. return szLevelName;
  223. }
  224. CString SaferGetLevelDescription (DWORD dwLevelID, HKEY hGroupPolicyKey, const bool bIsComputer)
  225. {
  226. CString szDescription;
  227. SAFER_LEVEL_HANDLE hLevel = 0;
  228. BOOL bRVal = FALSE;
  229. if ( hGroupPolicyKey )
  230. {
  231. if ( !g_hkeyLastSaferRegistryScope )
  232. SetRegistryScope (hGroupPolicyKey, bIsComputer);
  233. bRVal = SaferCreateLevel (SAFER_SCOPEID_REGISTRY,
  234. dwLevelID,
  235. SAFER_LEVEL_OPEN,
  236. &hLevel,
  237. hGroupPolicyKey);
  238. }
  239. else
  240. {
  241. if ( g_hkeyLastSaferRegistryScope )
  242. SetRegistryScope (0, bIsComputer);
  243. bRVal = SaferCreateLevel (SAFER_SCOPEID_MACHINE,
  244. dwLevelID,
  245. SAFER_LEVEL_OPEN,
  246. &hLevel,
  247. 0);
  248. }
  249. ASSERT (bRVal);
  250. if ( bRVal )
  251. {
  252. DWORD dwBufferSize = 0;
  253. DWORD dwErr = 0;
  254. bRVal = SaferGetLevelInformation(hLevel,
  255. SaferObjectDescription,
  256. 0,
  257. dwBufferSize,
  258. &dwBufferSize);
  259. if ( !bRVal && ERROR_INSUFFICIENT_BUFFER == GetLastError () )
  260. {
  261. PWSTR pszDescription = (PWSTR) LocalAlloc (LPTR, dwBufferSize);
  262. if ( pszDescription )
  263. {
  264. bRVal = SaferGetLevelInformation(hLevel,
  265. SaferObjectDescription,
  266. pszDescription,
  267. dwBufferSize,
  268. &dwBufferSize);
  269. ASSERT (bRVal);
  270. if ( bRVal )
  271. {
  272. szDescription = pszDescription;
  273. }
  274. else
  275. {
  276. dwErr = GetLastError ();
  277. _TRACE (0, L"SaferGetLevelInformation(SaferObjectFriendlyName) failed: %d\n",
  278. dwErr);
  279. }
  280. LocalFree (pszDescription);
  281. }
  282. }
  283. else if ( !bRVal )
  284. {
  285. dwErr = GetLastError ();
  286. _TRACE (0, L"SaferGetLevelInformation(SaferObjectFriendlyName) failed: %d\n",
  287. dwErr);
  288. }
  289. if ( ERROR_NOT_FOUND == dwErr || szDescription.IsEmpty () )
  290. {
  291. VERIFY (szDescription.LoadString (IDS_NOT_AVAILABLE));
  292. }
  293. VERIFY (SaferCloseLevel (hLevel));
  294. }
  295. else
  296. {
  297. DWORD dwErr = GetLastError ();
  298. _TRACE (0, L"SaferCreateLevel (%s, 0x%x, SAFER_LEVEL_OPEN) failed: %d\n",
  299. hGroupPolicyKey ? L"SAFER_SCOPEID_REGISTRY" : L"SAFER_SCOPEID_MACHINE",
  300. dwLevelID, dwErr);
  301. }
  302. return szDescription;
  303. }
  304. HRESULT SaferGetLevelID (SAFER_LEVEL_HANDLE hLevel, DWORD& dwLevelID)
  305. {
  306. ASSERT (0 != g_hkeyLastSaferRegistryScope);
  307. DWORD dwBufferSize = sizeof (DWORD);
  308. HRESULT hr = S_OK;
  309. BOOL bRVal = SaferGetLevelInformation(hLevel,
  310. SaferObjectLevelId,
  311. &dwLevelID,
  312. dwBufferSize,
  313. &dwBufferSize);
  314. ASSERT (bRVal);
  315. if ( !bRVal )
  316. {
  317. DWORD dwErr = GetLastError ();
  318. hr = HRESULT_FROM_WIN32 (dwErr);
  319. _TRACE (0, L"SaferGetLevelInformation(SaferObjectLevelId) failed: %d\n",
  320. dwErr);
  321. }
  322. return hr;
  323. }
  324. CSaferEntries::CSaferEntries(
  325. bool bIsMachine,
  326. PCWSTR pszMachineName,
  327. PCWSTR pszObjectName,
  328. IGPEInformation* pGPEInformation,
  329. IRSOPInformation* pRSOPInformation,
  330. CRSOPObjectArray& rsopObjectArray,
  331. LPCONSOLE pConsole)
  332. : CCertMgrCookie (bIsMachine ? CERTMGR_SAFER_COMPUTER_ENTRIES : CERTMGR_SAFER_USER_ENTRIES,
  333. pszMachineName, pszObjectName),
  334. m_pTrustedPublishersStore (0),
  335. m_pDisallowedStore (0)
  336. {
  337. if ( pGPEInformation )
  338. {
  339. m_pTrustedPublishersStore = new CCertStoreSafer (
  340. CERT_SYSTEM_STORE_RELOCATE_FLAG,
  341. L"",
  342. SAFER_TRUSTED_PUBLISHER_STORE_FRIENDLY_NAME,
  343. SAFER_TRUSTED_PUBLISHER_STORE_NAME,
  344. L"",
  345. pGPEInformation,
  346. bIsMachine ? NODEID_Machine : NODEID_User,
  347. pConsole);
  348. m_pDisallowedStore = new CCertStoreSafer (
  349. CERT_SYSTEM_STORE_RELOCATE_FLAG,
  350. L"",
  351. SAFER_DISALLOWED_STORE_FRIENDLY_NAME,
  352. SAFER_DISALLOWED_STORE_NAME,
  353. L"",
  354. pGPEInformation,
  355. bIsMachine ? NODEID_Machine : NODEID_User,
  356. pConsole);
  357. }
  358. else if ( pRSOPInformation )
  359. {
  360. m_pTrustedPublishersStore = new CCertStoreRSOP (
  361. CERT_SYSTEM_STORE_RELOCATE_FLAG,
  362. L"",
  363. SAFER_TRUSTED_PUBLISHER_STORE_FRIENDLY_NAME,
  364. SAFER_TRUSTED_PUBLISHER_STORE_NAME,
  365. L"",
  366. rsopObjectArray,
  367. bIsMachine ? NODEID_Machine : NODEID_User,
  368. pConsole);
  369. m_pDisallowedStore = new CCertStoreRSOP (
  370. CERT_SYSTEM_STORE_RELOCATE_FLAG,
  371. L"",
  372. SAFER_DISALLOWED_STORE_FRIENDLY_NAME,
  373. SAFER_DISALLOWED_STORE_NAME,
  374. L"",
  375. rsopObjectArray,
  376. bIsMachine ? NODEID_Machine : NODEID_User,
  377. pConsole);
  378. }
  379. }
  380. CSaferEntries::~CSaferEntries ()
  381. {
  382. if ( m_pTrustedPublishersStore )
  383. {
  384. m_pTrustedPublishersStore->Release ();
  385. m_pTrustedPublishersStore = 0;
  386. }
  387. if ( m_pDisallowedStore )
  388. {
  389. m_pDisallowedStore->Release ();
  390. m_pDisallowedStore = 0;
  391. }
  392. }
  393. HRESULT CSaferEntries::GetTrustedPublishersStore(CCertStore **ppStore)
  394. {
  395. if ( !ppStore )
  396. return E_POINTER;
  397. if ( m_pTrustedPublishersStore )
  398. {
  399. m_pTrustedPublishersStore->AddRef ();
  400. *ppStore = m_pTrustedPublishersStore;
  401. }
  402. else
  403. return E_FAIL;
  404. return S_OK;
  405. }
  406. HRESULT CSaferEntries::GetDisallowedStore(CCertStore **ppStore)
  407. {
  408. if ( !ppStore )
  409. return E_POINTER;
  410. if ( m_pDisallowedStore )
  411. {
  412. m_pDisallowedStore->AddRef ();
  413. *ppStore = m_pDisallowedStore;
  414. }
  415. else
  416. return E_FAIL;
  417. return S_OK;
  418. }
  419. HRESULT SetRegistryScope (HKEY hKey, bool bIsComputer)
  420. {
  421. HRESULT hr = S_OK;
  422. if ( g_hkeyLastSaferRegistryScope != hKey || g_bIsComputer != bIsComputer )
  423. {
  424. BOOL bRVal = SaferiChangeRegistryScope (hKey, REG_OPTION_NON_VOLATILE);
  425. ASSERT (bRVal);
  426. if ( bRVal )
  427. {
  428. g_hkeyLastSaferRegistryScope = hKey;
  429. g_bIsComputer = bIsComputer;
  430. }
  431. else
  432. {
  433. DWORD dwErr = GetLastError ();
  434. hr = HRESULT_FROM_WIN32 (dwErr);
  435. _TRACE (0, L"SaferiChangeRegistryScope (%s) failed: %d\n",
  436. hKey ? L"hKey" : L"0", dwErr);
  437. }
  438. }
  439. return hr;
  440. }
  441. ///////////////////////////////////////////////////////////////////////////////
  442. // Returns S_OK if the file has a valid signed hash
  443. HRESULT GetSignedFileHash(
  444. IN LPCWSTR pwszFilename,
  445. OUT BYTE rgbFileHash[SAFER_MAX_HASH_SIZE],
  446. OUT DWORD *pcbFileHash,
  447. OUT ALG_ID *pHashAlgid
  448. )
  449. {
  450. HRESULT hr = S_OK;
  451. if ( !pwszFilename || !rgbFileHash || !pcbFileHash || !pHashAlgid )
  452. return E_POINTER;
  453. _TRACE (1, L"Entering GetSignedFileHash (%s)\n", pwszFilename);
  454. // Returns S_OK and the hash if the file was signed and contains a valid
  455. // hash
  456. *pcbFileHash = SAFER_MAX_HASH_SIZE;
  457. hr = WTHelperGetFileHash(
  458. pwszFilename,
  459. 0,
  460. NULL,
  461. rgbFileHash,
  462. pcbFileHash,
  463. pHashAlgid);
  464. if ( FAILED (hr) )
  465. {
  466. _TRACE (0, L"WTHelperGetFileHash (%s) failed: 0x%x\n", pwszFilename, hr);
  467. }
  468. _TRACE (-1, L"Leaving GetSignedFileHash (%s): 0x%x\n", pwszFilename, hr);
  469. return hr;
  470. }
  471. HRESULT ComputeMD5Hash(IN HANDLE hFile, BYTE hashResult[SAFER_MAX_HASH_SIZE], DWORD& dwHashSize)
  472. /*++
  473. Routine Description:
  474. Computes the MD5 hash of a given file's contents and prints the
  475. resulting hash value to the screen.
  476. Arguments:
  477. szFilename - filename to compute hash of.
  478. Return Value:
  479. Returns 0 on success, or a non-zero exit code on failure.
  480. --*/
  481. {
  482. _TRACE (1, L"Entering ComputeMD5Hash ()\n");
  483. HRESULT hr = S_OK;
  484. ASSERT (hashResult);
  485. if ( !hashResult )
  486. return E_POINTER;
  487. //
  488. // Open the specified file and map it into memory.
  489. //
  490. // security review 2/25/2002 BryanWal ok
  491. // NOTICE - hFile is from GetOpenFileName ()
  492. HANDLE hMapping = ::CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  493. if ( hMapping )
  494. {
  495. DWORD dwDataLen = GetFileSize (hFile, NULL);
  496. if ( -1 != dwDataLen )
  497. {
  498. // security review 2/25/2002 BryanWal ok
  499. LPBYTE pbData = (LPBYTE) ::MapViewOfFile (hMapping, FILE_MAP_READ, 0, 0, dwDataLen);
  500. if ( pbData )
  501. {
  502. //
  503. // Generate the hash value of the specified file.
  504. //
  505. HCRYPTPROV hProvider = 0;
  506. if ( CryptAcquireContext(&hProvider, NULL, NULL,
  507. PROV_RSA_SIG, CRYPT_VERIFYCONTEXT) ||
  508. CryptAcquireContext(&hProvider, NULL, NULL,
  509. PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) )
  510. {
  511. HCRYPTHASH hHash = 0;
  512. if ( CryptCreateHash(hProvider, CALG_MD5, 0, 0, &hHash) )
  513. {
  514. if ( CryptHashData (hHash, pbData, dwDataLen, 0) )
  515. {
  516. dwHashSize = SAFER_MAX_HASH_SIZE;
  517. if (!CryptGetHashParam(hHash, HP_HASHVAL, hashResult, &dwHashSize, 0))
  518. {
  519. dwHashSize = 0;
  520. DWORD dwErr = GetLastError ();
  521. hr = HRESULT_FROM_WIN32 (dwErr);
  522. _TRACE (0, L"CryptHashData () failed: 0x%x\n", hr);
  523. }
  524. }
  525. else
  526. {
  527. DWORD dwErr = GetLastError ();
  528. hr = HRESULT_FROM_WIN32 (dwErr);
  529. _TRACE (0, L"CryptHashData () failed: 0x%x\n", hr);
  530. }
  531. CryptDestroyHash(hHash);
  532. }
  533. else
  534. {
  535. DWORD dwErr = GetLastError ();
  536. hr = HRESULT_FROM_WIN32 (dwErr);
  537. _TRACE (0, L"CryptCreateHash () failed: 0x%x\n", hr);
  538. }
  539. CryptReleaseContext(hProvider, 0);
  540. }
  541. else
  542. {
  543. DWORD dwErr = GetLastError ();
  544. hr = HRESULT_FROM_WIN32 (dwErr);
  545. _TRACE (0, L"CryptAcquireContext () failed: 0x%x\n", hr);
  546. }
  547. ::UnmapViewOfFile(pbData);
  548. }
  549. else
  550. {
  551. DWORD dwErr = GetLastError ();
  552. _TRACE (0, L"MapViewOfFile () failed: 0x%x\n", dwErr);
  553. hr = HRESULT_FROM_WIN32 (dwErr);
  554. }
  555. }
  556. else
  557. {
  558. DWORD dwErr = GetLastError ();
  559. _TRACE (0, L"GetFileSize () failed: 0x%x\n", dwErr);
  560. hr = HRESULT_FROM_WIN32 (dwErr);
  561. }
  562. VERIFY (CloseHandle(hMapping));
  563. hMapping = 0;
  564. }
  565. else
  566. {
  567. DWORD dwErr = GetLastError ();
  568. _TRACE (0, L"CreateFileMapping () failed: 0x%x\n", dwErr);
  569. hr = HRESULT_FROM_WIN32 (dwErr);
  570. }
  571. _TRACE (-1, L"Leaving ComputeMD5Hash (): 0x%x\n", hr);
  572. return hr;
  573. }
  574. CString GetURLZoneFriendlyName (DWORD dwURLZoneID)
  575. {
  576. CString szFriendlyName;
  577. switch (dwURLZoneID)
  578. {
  579. case URLZONE_LOCAL_MACHINE:
  580. VERIFY (szFriendlyName.LoadString (IDS_URLZONE_LOCAL_MACHINE));
  581. break;
  582. case URLZONE_INTRANET:
  583. VERIFY (szFriendlyName.LoadString (IDS_URLZONE_INTRANET));
  584. break;
  585. case URLZONE_TRUSTED:
  586. VERIFY (szFriendlyName.LoadString (IDS_URLZONE_TRUSTED));
  587. break;
  588. case URLZONE_INTERNET:
  589. VERIFY (szFriendlyName.LoadString (IDS_URLZONE_INTERNET));
  590. break;
  591. case URLZONE_UNTRUSTED:
  592. VERIFY (szFriendlyName.LoadString (IDS_URLZONE_UNTRUSTED));
  593. break;
  594. default:
  595. ASSERT (0);
  596. VERIFY (szFriendlyName.LoadString (IDS_URLZONE_UNKNOWN));
  597. break;
  598. }
  599. return szFriendlyName;
  600. }
  601. //
  602. // Given a GUID in string format it returns a GUID struct
  603. //
  604. // e.g. "{00299570-246d-11d0-a768-00aa006e0529}" to a struct form
  605. //
  606. BOOL GuidFromString(GUID* pGuid, const CString& szGuidString)
  607. {
  608. ASSERT (pGuid);
  609. if ( !pGuid )
  610. return FALSE;
  611. // security review 2/25/2002 BryanWal ok
  612. ::ZeroMemory(pGuid, sizeof(GUID));
  613. // security review 2/25/2002 BryanWal ok
  614. size_t nLen = wcslen (szGuidString);
  615. // the string length should be 38
  616. if (nLen != 38)
  617. return FALSE;
  618. return SUCCEEDED(::CLSIDFromString ((LPOLESTR)((PCWSTR)szGuidString), pGuid));
  619. }
  620. HRESULT SaferSetDefinedFileTypes (
  621. HWND hWnd,
  622. HKEY hGroupPolicyKey,
  623. PCWSTR pszFileTypes,
  624. int nBufLen)
  625. {
  626. HRESULT hr = S_OK;
  627. DWORD dwDisposition = 0;
  628. HKEY hKey = 0;
  629. LONG lResult = ::RegCreateKeyEx (hGroupPolicyKey, // handle of an open key
  630. SAFER_COMPUTER_CODEIDS_REGKEY, // address of subkey name
  631. 0, // reserved
  632. L"", // address of class string
  633. REG_OPTION_NON_VOLATILE, // special options flag
  634. // security review 2/25/2002 BryanWal ok
  635. KEY_SET_VALUE, // desired security access
  636. NULL, // address of key security structure
  637. &hKey, // address of buffer for opened handle
  638. &dwDisposition); // address of disposition value buffer
  639. ASSERT (ERROR_SUCCESS == lResult);
  640. if ( ERROR_SUCCESS == lResult )
  641. {
  642. lResult = ::RegSetValueEx (
  643. hKey, // handle to key
  644. SAFER_EXETYPES_REGVALUE, // value name
  645. 0, // reserved
  646. REG_MULTI_SZ, // value type
  647. (PBYTE) pszFileTypes, // value data
  648. nBufLen); // size of value data
  649. if ( ERROR_SUCCESS != lResult )
  650. {
  651. DisplaySystemError (hWnd, lResult);
  652. _TRACE (0, L"RegSetValueEx (SAFER_EXETYPES_REGVALUE, %s) failed: %d\n",
  653. pszFileTypes, lResult);
  654. hr = HRESULT_FROM_WIN32 (lResult);
  655. }
  656. RegCloseKey (hKey);
  657. }
  658. else
  659. {
  660. DisplaySystemError (hWnd, lResult);
  661. _TRACE (0, L"RegCreateKeyEx (SAFER_CODEID_KEY) failed: %d\n", lResult);
  662. hr = HRESULT_FROM_WIN32 (lResult);
  663. }
  664. return hr;
  665. }