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.

1008 lines
45 KiB

  1. //
  2. // SECZONES.CPP
  3. //
  4. #include "precomp.h"
  5. #include <urlmon.h>
  6. #include <wininet.h>
  7. #ifdef WINNT
  8. #include <winineti.h>
  9. #endif // WINNT
  10. #include "SComPtr.h"
  11. #define REGSTR_PATH_SECURITY_LOCKOUT TEXT("Software\\Policies\\Microsoft\\Windows\\CurrentVersion\\Internet Settings")
  12. #define REGSTR_VAL_HKLM_ONLY TEXT("Security_HKLM_only")
  13. // prototype declarations
  14. static BOOL importZonesHelper(LPCTSTR pcszInsFile, LPCTSTR pcszZonesWorkDir, LPCTSTR pcszZonesInf, BOOL fImportZones);
  15. static BOOL importRatingsHelper(LPCTSTR pcszInsFile, LPCTSTR pcszRatingsWorkDir, LPCTSTR pcszRatingsInf, BOOL fImportRatings);
  16. static BOOL ratingsInRegistry(VOID);
  17. BOOL WINAPI ImportZonesA(LPCSTR pcszInsFile, LPCSTR pcszZonesWorkDir, LPCSTR pcszZonesInf, BOOL fImportZones)
  18. {
  19. USES_CONVERSION;
  20. return importZonesHelper(A2CT(pcszInsFile), A2CT(pcszZonesWorkDir), A2CT(pcszZonesInf), fImportZones);
  21. }
  22. BOOL WINAPI ImportZonesW(LPCWSTR pcwszInsFile, LPCWSTR pcwszZonesWorkDir, LPCWSTR pcwszZonesInf, BOOL fImportZones)
  23. {
  24. USES_CONVERSION;
  25. return importZonesHelper(W2CT(pcwszInsFile), W2CT(pcwszZonesWorkDir), W2CT(pcwszZonesInf), fImportZones);
  26. }
  27. BOOL WINAPI ModifyZones(HWND hDlg)
  28. {
  29. typedef HRESULT (WINAPI * ZONESREINIT)(DWORD);
  30. //typedef VOID (WINAPI * LAUNCHSECURITYDIALOGEX)(HWND, DWORD, DWORD);
  31. BOOL fRet;
  32. HINSTANCE hUrlmon, hInetCpl;
  33. ZONESREINIT pfnZonesReInit;
  34. //LAUNCHSECURITYDIALOGEX pfnLaunchSecurityDialogEx;
  35. HKEY hkPol;
  36. DWORD dwOldHKLM, dwOldOptEdit, dwOldZoneMap;
  37. fRet = FALSE;
  38. hUrlmon = NULL;
  39. hInetCpl = NULL;
  40. hkPol = NULL;
  41. dwOldHKLM = 0;
  42. dwOldOptEdit = 0;
  43. dwOldZoneMap = 0;
  44. if ((hUrlmon = LoadLibrary(TEXT("urlmon.dll"))) == NULL)
  45. goto Exit;
  46. if ((hInetCpl = LoadLibrary(TEXT("inetcpl.cpl"))) == NULL)
  47. goto Exit;
  48. if ((pfnZonesReInit = (ZONESREINIT) GetProcAddress(hUrlmon, "ZonesReInit")) == NULL)
  49. goto Exit;
  50. // if ((pfnLaunchSecurityDialogEx = (LAUNCHSECURITYDIALOGEX) GetProcAddress(hInetCpl, "LaunchSecurityDialogEx")) == NULL)
  51. // goto Exit;
  52. fRet = TRUE;
  53. SHOpenKeyHKLM(REG_KEY_INET_POLICIES, KEY_QUERY_VALUE | KEY_SET_VALUE, &hkPol);
  54. // if zones related restrictions are set, save the values and then delete them
  55. if (hkPol != NULL)
  56. {
  57. dwOldHKLM = RegSaveRestoreDWORD(hkPol, REG_VAL_HKLM_ONLY, 0);
  58. dwOldOptEdit = RegSaveRestoreDWORD(hkPol, REG_VAL_OPT_EDIT, 0);
  59. dwOldZoneMap = RegSaveRestoreDWORD(hkPol, REG_VAL_ZONE_MAP, 0);
  60. pfnZonesReInit(0); // call into URLMON.DLL to force it to read the current settings
  61. }
  62. // call into INETCPL.CPL to modify the zones settings
  63. //pfnLaunchSecurityDialogEx(hDlg, 1, LSDFLAG_FORCEUI);
  64. ShowInetcpl(hDlg,INET_PAGE_SECURITY|INET_PAGE_PRIVACY);
  65. // restore the original values
  66. if (hkPol != NULL)
  67. {
  68. RegSaveRestoreDWORD(hkPol, REG_VAL_HKLM_ONLY, dwOldHKLM);
  69. RegSaveRestoreDWORD(hkPol, REG_VAL_OPT_EDIT, dwOldOptEdit);
  70. RegSaveRestoreDWORD(hkPol, REG_VAL_ZONE_MAP, dwOldZoneMap);
  71. pfnZonesReInit(0); // call into URLMON.DLL to force it to read the current settings
  72. }
  73. Exit:
  74. if (hUrlmon != NULL)
  75. FreeLibrary(hUrlmon);
  76. if (hInetCpl != NULL)
  77. FreeLibrary(hInetCpl);
  78. if (hkPol != NULL)
  79. SHCloseKey(hkPol);
  80. return fRet;
  81. }
  82. BOOL WINAPI ImportRatingsA(LPCSTR pcszInsFile, LPCSTR pcszRatingsWorkDir, LPCSTR pcszRatingsInf, BOOL fImportRatings)
  83. {
  84. USES_CONVERSION;
  85. return importRatingsHelper(A2CT(pcszInsFile), A2CT(pcszRatingsWorkDir), A2CT(pcszRatingsInf), fImportRatings);
  86. }
  87. BOOL WINAPI ImportRatingsW(LPCWSTR pcwszInsFile, LPCWSTR pcwszRatingsWorkDir, LPCWSTR pcwszRatingsInf, BOOL fImportRatings)
  88. {
  89. USES_CONVERSION;
  90. return importRatingsHelper(W2CT(pcwszInsFile), W2CT(pcwszRatingsWorkDir), W2CT(pcwszRatingsInf), fImportRatings);
  91. }
  92. BOOL WINAPI ModifyRatings(HWND hDlg)
  93. {
  94. typedef HRESULT (WINAPI * RATINGSETUPUI)(HWND, LPCSTR);
  95. BOOL fRet;
  96. HINSTANCE hMSRating;
  97. RATINGSETUPUI pfnRatingSetupUI;
  98. fRet = FALSE;
  99. hMSRating = NULL;
  100. if ((hMSRating = LoadLibrary(TEXT("msrating.dll"))) == NULL)
  101. goto Exit;
  102. if ((pfnRatingSetupUI = (RATINGSETUPUI) GetProcAddress(hMSRating, "RatingSetupUI")) == NULL)
  103. goto Exit;
  104. fRet = TRUE;
  105. // call into msrating.dll to modify the ratings
  106. pfnRatingSetupUI(hDlg, NULL);
  107. Exit:
  108. if (hMSRating != NULL)
  109. FreeLibrary(hMSRating);
  110. return fRet;
  111. }
  112. /////////////////////////////////////////////////////////////////////
  113. static void importPrivacyForRSOP(LPCTSTR szFile)
  114. {
  115. __try
  116. {
  117. BOOL fAdvanced = FALSE;
  118. DWORD dwTemplate;
  119. DWORD dwError = PrivacyGetZonePreferenceW(
  120. URLZONE_INTERNET,
  121. PRIVACY_TYPE_FIRST_PARTY,
  122. &dwTemplate,
  123. NULL,
  124. NULL);
  125. if(ERROR_SUCCESS == dwError && PRIVACY_TEMPLATE_ADVANCED == dwTemplate)
  126. fAdvanced = TRUE;
  127. // AdvancedSettings
  128. TCHAR szInt[32];
  129. wnsprintf(szInt, countof(szInt), TEXT("%d"), fAdvanced ? 1 : 0);
  130. WritePrivateProfileString(IK_PRIVACY, IK_PRIV_ADV_SETTINGS, szInt, szFile);
  131. //
  132. // Figure out first party setting and session
  133. //
  134. dwTemplate = PRIVACY_TEMPLATE_CUSTOM;
  135. WCHAR szBuffer[MAX_PATH];
  136. // MAX_PATH is sufficent for advanced mode setting strings, MaxPrivacySettings is overkill.
  137. DWORD dwBufferSize = ARRAYSIZE(szBuffer);
  138. dwError = PrivacyGetZonePreferenceW(
  139. URLZONE_INTERNET,
  140. PRIVACY_TYPE_FIRST_PARTY,
  141. &dwTemplate,
  142. szBuffer,
  143. &dwBufferSize);
  144. if (ERROR_SUCCESS != dwError)
  145. dwTemplate = PRIVACY_TEMPLATE_CUSTOM;
  146. // store settings in INF file
  147. // FirstPartyType
  148. wnsprintf(szInt, countof(szInt), TEXT("%lu"), dwTemplate);
  149. WritePrivateProfileString(IK_PRIVACY, IK_PRIV_1PARTY_TYPE, szInt, szFile);
  150. // FirstPartyTypeText
  151. if (ERROR_SUCCESS == dwError && fAdvanced && dwBufferSize > 0)
  152. WritePrivateProfileString(IK_PRIVACY, IK_PRIV_1PARTY_TYPE_TEXT, szBuffer, szFile);
  153. //
  154. // Figure out third party setting
  155. //
  156. dwTemplate = PRIVACY_TEMPLATE_CUSTOM;
  157. dwBufferSize = ARRAYSIZE(szBuffer);
  158. dwBufferSize = ARRAYSIZE( szBuffer);
  159. dwError = PrivacyGetZonePreferenceW(
  160. URLZONE_INTERNET,
  161. PRIVACY_TYPE_THIRD_PARTY,
  162. &dwTemplate,
  163. szBuffer,
  164. &dwBufferSize);
  165. if(dwError != ERROR_SUCCESS)
  166. dwTemplate = PRIVACY_TEMPLATE_CUSTOM;
  167. // ThirdPartyType
  168. wnsprintf(szInt, countof(szInt), TEXT("%lu"), dwTemplate);
  169. WritePrivateProfileString(IK_PRIVACY, IK_PRIV_3PARTY_TYPE, szInt, szFile);
  170. // ThirdPartyTypeText
  171. if (ERROR_SUCCESS == dwError && fAdvanced && dwBufferSize > 0)
  172. WritePrivateProfileString(IK_PRIVACY, IK_PRIV_3PARTY_TYPE_TEXT, szBuffer, szFile);
  173. }
  174. __except(TRUE)
  175. {
  176. }
  177. }
  178. /////////////////////////////////////////////////////////////////////
  179. static void importZonesForRSOP(LPCTSTR szFile)
  180. {
  181. __try
  182. {
  183. // both the security mgr & the zone mgr must be created
  184. ComPtr<IInternetZoneManager> pZoneMgr = NULL;
  185. ComPtr<IInternetSecurityManager> pSecMan = NULL;
  186. HRESULT hr = CoCreateInstance(CLSID_InternetZoneManager, NULL, CLSCTX_INPROC_SERVER,
  187. IID_IInternetZoneManager, (void**) &pZoneMgr);
  188. if (SUCCEEDED(hr))
  189. {
  190. hr = CoCreateInstance(CLSID_InternetSecurityManager, NULL, CLSCTX_INPROC_SERVER,
  191. IID_IInternetSecurityManager, (void**) &pSecMan);
  192. }
  193. // Write out zone mappings & attributes
  194. if (SUCCEEDED(hr))
  195. {
  196. DWORD dwEnum = 0, dwCount = 0;
  197. hr = pZoneMgr->CreateZoneEnumerator(&dwEnum, &dwCount, 0L);
  198. if (SUCCEEDED(hr) && dwCount > 0)
  199. {
  200. TCHAR szSection[32];
  201. TCHAR szMapping[32];
  202. TCHAR szInt[32];
  203. for (UINT nZone = 0; nZone < dwCount; nZone++)
  204. {
  205. for (int nHKLM = 0; nHKLM < 2; nHKLM++)
  206. {
  207. HKEY hkZones = NULL;
  208. TCHAR szZIndex[MAX_PATH];
  209. wnsprintf(szZIndex, countof(szZIndex), REG_KEY_ZONES TEXT("\\%lu"), nZone);
  210. if (0 == nHKLM)
  211. {
  212. SHOpenKeyHKLM(szZIndex, KEY_READ, &hkZones);
  213. wnsprintf(szSection, countof(szSection), IK_ZONE_HKCU_FMT, nZone);
  214. }
  215. else
  216. {
  217. SHOpenKeyHKCU(szZIndex, KEY_READ, &hkZones);
  218. wnsprintf(szSection, countof(szSection), IK_ZONE_HKLM_FMT, nZone);
  219. }
  220. // write out zone attributes
  221. TCHAR szTemp[MAX_PATH]; // MAX_ZONE_PATH && MAX_ZONE_DESCRIPTION = MAX_PATH = 260
  222. DWORD dwSize = sizeof(szTemp);
  223. if (NULL != hkZones)
  224. {
  225. if (ERROR_SUCCESS == RegQueryValueEx(hkZones, IK_DISPLAYNAME, NULL, NULL, (LPBYTE)szTemp, &dwSize))
  226. {
  227. WritePrivateProfileString(szSection, IK_DISPLAYNAME, szTemp, szFile);
  228. dwSize = sizeof(szTemp);
  229. }
  230. if (ERROR_SUCCESS == RegQueryValueEx(hkZones, IK_DESCRIPTION, NULL, NULL, (LPBYTE)szTemp, &dwSize))
  231. {
  232. WritePrivateProfileString(szSection, IK_DESCRIPTION, szTemp, szFile);
  233. dwSize = sizeof(szTemp);
  234. }
  235. if (ERROR_SUCCESS == RegQueryValueEx(hkZones, IK_ICONPATH, NULL, NULL, (LPBYTE)szTemp, &dwSize))
  236. {
  237. WritePrivateProfileString(szSection, IK_ICONPATH, szTemp, szFile);
  238. dwSize = sizeof(szTemp);
  239. }
  240. DWORD dwTemp = 0;
  241. dwSize = sizeof(dwTemp);
  242. if (ERROR_SUCCESS == RegQueryValueEx(hkZones, IK_MINLEVEL, NULL, NULL, (LPBYTE)&dwTemp, &dwSize))
  243. {
  244. wnsprintf(szInt, countof(szInt), TEXT("%lu"), dwTemp);
  245. WritePrivateProfileString(szSection, IK_MINLEVEL, szInt, szFile);
  246. }
  247. if (ERROR_SUCCESS == RegQueryValueEx(hkZones, IK_RECOMMENDLEVEL, NULL, NULL, (LPBYTE)&dwTemp, &dwSize))
  248. {
  249. wnsprintf(szInt, countof(szInt), TEXT("%lu"), dwTemp);
  250. WritePrivateProfileString(szSection, IK_RECOMMENDLEVEL, szInt, szFile);
  251. }
  252. if (ERROR_SUCCESS == RegQueryValueEx(hkZones, IK_CURLEVEL, NULL, NULL, (LPBYTE)&dwTemp, &dwSize))
  253. {
  254. wnsprintf(szInt, countof(szInt), TEXT("%lu"), dwTemp);
  255. WritePrivateProfileString(szSection, IK_CURLEVEL, szInt, szFile);
  256. }
  257. if (ERROR_SUCCESS == RegQueryValueEx(hkZones, IK_FLAGS, NULL, NULL, (LPBYTE)&dwTemp, &dwSize))
  258. {
  259. wnsprintf(szInt, countof(szInt), TEXT("%lu"), dwTemp);
  260. WritePrivateProfileString(szSection, IK_FLAGS, szInt, szFile);
  261. }
  262. }
  263. // write out action values
  264. if (NULL != hkZones)
  265. {
  266. TCHAR szActKey[32];
  267. TCHAR szActValue[64];
  268. DWORD dwURLAction[] =
  269. { URLACTION_ACTIVEX_OVERRIDE_OBJECT_SAFETY,
  270. URLACTION_ACTIVEX_RUN,
  271. URLACTION_CHANNEL_SOFTDIST_PERMISSIONS,
  272. URLACTION_COOKIES,
  273. URLACTION_COOKIES_SESSION,
  274. URLACTION_CREDENTIALS_USE,
  275. URLACTION_CLIENT_CERT_PROMPT,
  276. URLACTION_CROSS_DOMAIN_DATA,
  277. URLACTION_DOWNLOAD_SIGNED_ACTIVEX,
  278. URLACTION_DOWNLOAD_UNSIGNED_ACTIVEX,
  279. URLACTION_HTML_FONT_DOWNLOAD,
  280. URLACTION_HTML_SUBFRAME_NAVIGATE,
  281. URLACTION_HTML_SUBMIT_FORMS,
  282. URLACTION_HTML_JAVA_RUN,
  283. URLACTION_HTML_USERDATA_SAVE,
  284. URLACTION_JAVA_PERMISSIONS,
  285. URLACTION_SCRIPT_JAVA_USE,
  286. URLACTION_SCRIPT_PASTE,
  287. URLACTION_SCRIPT_RUN,
  288. URLACTION_SCRIPT_SAFE_ACTIVEX,
  289. URLACTION_SHELL_FILE_DOWNLOAD,
  290. URLACTION_SHELL_INSTALL_DTITEMS,
  291. URLACTION_SHELL_MOVE_OR_COPY,
  292. URLACTION_SHELL_VERB,
  293. URLACTION_SHELL_WEBVIEW_VERB,
  294. 0 };
  295. DWORD dwSetting = 0;
  296. DWORD dwSetSize = sizeof(dwSetting);
  297. long nAction = 0;
  298. long nStoredAction = 0;
  299. while (0 != dwURLAction[nAction])
  300. {
  301. wnsprintf(szTemp, countof(szTemp), TEXT("%lX"), dwURLAction[nAction]);
  302. if (ERROR_SUCCESS == RegQueryValueEx(hkZones, szTemp, NULL, NULL,
  303. (LPBYTE)&dwSetting, &dwSetSize))
  304. {
  305. wnsprintf(szActKey, countof(szActKey), IK_ACTIONVALUE_FMT, nStoredAction);
  306. wnsprintf(szActValue, countof(szActValue), TEXT("%s:%lu"), szTemp, dwSetting);
  307. WritePrivateProfileString(szSection, szActKey, szActValue, szFile);
  308. nStoredAction++;
  309. }
  310. nAction++;
  311. }
  312. }
  313. // write out zone mappings
  314. DWORD dwZone = 0;
  315. hr = pZoneMgr->GetZoneAt(dwEnum, nZone, &dwZone);
  316. ComPtr<IEnumString> pEnumString = NULL;
  317. hr = pSecMan->GetZoneMappings(dwZone, &pEnumString, 0);
  318. if (SUCCEEDED(hr))
  319. {
  320. UINT nMapping = 0;
  321. _bstr_t bstrSetting;
  322. for(int i = 0; ;i++)
  323. {
  324. TCHAR szBuffer[MAX_PATH];
  325. wnsprintf(szMapping, countof(szMapping), IK_MAPPING_FMT, i);
  326. if(GetPrivateProfileString(szSection, szMapping, TEXT(""), szBuffer, MAX_PATH, szFile))
  327. {
  328. WritePrivateProfileString(szSection, szMapping, NULL, szFile);
  329. }
  330. else
  331. {
  332. break;
  333. }
  334. }
  335. while (S_OK == hr)
  336. {
  337. wnsprintf(szMapping, countof(szMapping), IK_MAPPING_FMT, nMapping);
  338. nMapping++;
  339. // There should only be one object returned from this query.
  340. BSTR bstrVal = NULL;
  341. ULONG uReturned = (ULONG)-1L;
  342. hr = pEnumString->Next(1L, &bstrVal, &uReturned);
  343. if (SUCCEEDED(hr) && 1 == uReturned)
  344. {
  345. bstrSetting = bstrVal;
  346. WritePrivateProfileString(szSection, szMapping, (LPCTSTR)bstrSetting, szFile);
  347. }
  348. }
  349. }
  350. }
  351. }
  352. wnsprintf(szInt, countof(szInt), TEXT("%lu"), dwCount);
  353. WritePrivateProfileString(SECURITY_IMPORTS, IK_ZONES, szInt, szFile);
  354. if(IEHardened())
  355. {
  356. wnsprintf(szInt, countof(szInt), TEXT("%lu"), 1);
  357. WritePrivateProfileString(SECURITY_IMPORTS, IK_IEESC, szInt, szFile);
  358. }
  359. else
  360. {
  361. wnsprintf(szInt, countof(szInt), TEXT("%lu"), 0);
  362. WritePrivateProfileString(SECURITY_IMPORTS, IK_IEESC, szInt, szFile);
  363. }
  364. }
  365. }
  366. }
  367. __except(TRUE)
  368. {
  369. }
  370. }
  371. /////////////////////////////////////////////////////////////////////
  372. static BOOL importZonesHelper(LPCTSTR pcszInsFile, LPCTSTR pcszZonesWorkDir, LPCTSTR pcszZonesInf, BOOL fImportZones)
  373. {
  374. BOOL bRet = FALSE;
  375. HKEY hkZones = NULL, hkZoneMap = NULL;
  376. HKEY hkInetSettings = NULL, hkP3P = NULL;
  377. if (pcszInsFile == NULL || pcszZonesInf == NULL)
  378. return FALSE;
  379. // Before processing anything, first clear out the entries in the INS file and delete work dirs
  380. // clear out the entries in the INS file that correspond to importing security zones
  381. InsDeleteKey(SECURITY_IMPORTS, TEXT("ImportSecZones"), pcszInsFile);
  382. InsDeleteKey(IS_EXTREGINF, TEXT("SecZones"), pcszInsFile);
  383. InsDeleteKey(IS_EXTREGINF_HKLM, TEXT("SecZones"), pcszInsFile);
  384. InsDeleteKey(IS_EXTREGINF_HKCU, TEXT("SecZones"), pcszInsFile);
  385. InsDeleteKey(IS_EXTREGINF_ESC, TEXT("SecZones"), pcszInsFile);
  386. InsDeleteKey(IS_EXTREGINF_ESC_HKLM, TEXT("SecZones"), pcszInsFile);
  387. InsDeleteKey(IS_EXTREGINF_ESC_HKCU, TEXT("SecZones"), pcszInsFile);
  388. // blow away the pcszZonesWorkDir and pcszZonesInf
  389. if (pcszZonesWorkDir != NULL)
  390. PathRemovePath(pcszZonesWorkDir);
  391. PathRemovePath(pcszZonesInf);
  392. if (!fImportZones)
  393. return TRUE;
  394. // looks like there is some problem with setting the REG_VAL_HKLM_ONLY key;
  395. // so we'll import the settings from HKCU
  396. SHOpenKeyHKCU(REG_KEY_ZONES, KEY_DEFAULT_ACCESS, &hkZones);
  397. SHOpenKeyHKCU(REG_KEY_ZONEMAP, KEY_DEFAULT_ACCESS, &hkZoneMap);
  398. SHOpenKeyHKCU(KEY_INET_SETTINGS, KEY_DEFAULT_ACCESS, &hkInetSettings);
  399. SHOpenKeyHKCU(REG_KEY_P3P, KEY_DEFAULT_ACCESS, &hkP3P);
  400. if (hkZones != NULL && hkZoneMap != NULL)
  401. {
  402. TCHAR szFullInfName[MAX_PATH];
  403. HANDLE hInf;
  404. if (pcszZonesWorkDir != NULL && PathIsFileSpec(pcszZonesInf)) // create SECZONES.INF under pcszZonesWorkDir
  405. PathCombine(szFullInfName, pcszZonesWorkDir, pcszZonesInf);
  406. else
  407. StrCpy(szFullInfName, pcszZonesInf);
  408. // create SECZONES.INF file
  409. if ((hInf = CreateNewFile(szFullInfName)) != INVALID_HANDLE_VALUE)
  410. {
  411. TCHAR szBuf[MAX_PATH];
  412. // first, write the standard goo - [Version], [DefaultInstall], etc. - to SECZONES.INF
  413. WriteStringToFile(hInf, (LPCVOID) ZONES_INF_ADD, StrLen(ZONES_INF_ADD));
  414. ExportRegTree2Inf(hkZones, TEXT("HKLM"), REG_KEY_ZONES, hInf);
  415. ExportRegTree2Inf(hkZoneMap, TEXT("HKLM"), REG_KEY_ZONEMAP, hInf);
  416. // write [AddReg.HKCU]
  417. WriteStringToFile(hInf, (LPCVOID) ZONES_INF_ADDREG_HKCU, StrLen(ZONES_INF_ADDREG_HKCU));
  418. ExportRegTree2Inf(hkZones, TEXT("HKCU"), REG_KEY_ZONES, hInf);
  419. ExportRegTree2Inf(hkZoneMap, TEXT("HKCU"), REG_KEY_ZONEMAP, hInf);
  420. // Import P3P settings
  421. if (hkInetSettings != NULL && hkP3P != NULL)
  422. {
  423. ExportRegValue2Inf(hkInetSettings, TEXT("PrivacyAdvanced"), TEXT("HKCU"), KEY_INET_SETTINGS, hInf);
  424. ExportRegTree2Inf(hkP3P, TEXT("HKCU"), REG_KEY_P3P, hInf);
  425. }
  426. CloseHandle(hInf);
  427. BOOL fHarden = IEHardened();
  428. // update the INS file
  429. InsWriteBool(SECURITY_IMPORTS, TEXT("ImportSecZones"), TRUE, pcszInsFile);
  430. wnsprintf(szBuf, countof(szBuf), TEXT("*,%s,") IS_DEFAULTINSTALL, PathFindFileName(pcszZonesInf));
  431. if(fHarden)
  432. {
  433. WritePrivateProfileString(IS_EXTREGINF_ESC, TEXT("SecZones"), szBuf, pcszInsFile);
  434. }
  435. else
  436. {
  437. WritePrivateProfileString(IS_EXTREGINF, TEXT("SecZones"), szBuf, pcszInsFile);
  438. }
  439. // write to new ExtRegInf.HKLM and ExtRegInf.HKCU sections
  440. if (!InsIsSectionEmpty(IS_IEAKADDREG_HKLM, szFullInfName))
  441. {
  442. wnsprintf(szBuf, countof(szBuf), TEXT("%s,") IS_IEAKINSTALL_HKLM, PathFindFileName(pcszZonesInf));
  443. if(fHarden)
  444. {
  445. WritePrivateProfileString(IS_EXTREGINF_ESC_HKLM, TEXT("SecZones"), szBuf, pcszInsFile);
  446. }
  447. else
  448. {
  449. WritePrivateProfileString(IS_EXTREGINF_HKLM, TEXT("SecZones"), szBuf, pcszInsFile);
  450. }
  451. }
  452. if (!InsIsSectionEmpty(IS_IEAKADDREG_HKCU, szFullInfName))
  453. {
  454. wnsprintf(szBuf, countof(szBuf), TEXT("%s,") IS_IEAKINSTALL_HKCU, PathFindFileName(pcszZonesInf));
  455. if(fHarden)
  456. {
  457. WritePrivateProfileString(IS_EXTREGINF_ESC_HKCU, TEXT("SecZones"), szBuf, pcszInsFile);
  458. }
  459. else
  460. {
  461. WritePrivateProfileString(IS_EXTREGINF_HKCU, TEXT("SecZones"), szBuf, pcszInsFile);
  462. }
  463. }
  464. bRet = TRUE;
  465. }
  466. // create SECZRSOP.INF file
  467. TCHAR szZRSOPInfFile[MAX_PATH];
  468. StrCpy(szZRSOPInfFile, szFullInfName);
  469. PathRemoveFileSpec(szZRSOPInfFile);
  470. StrCat(szZRSOPInfFile, TEXT("\\seczrsop.inf"));
  471. importZonesForRSOP(szZRSOPInfFile);
  472. importPrivacyForRSOP(szZRSOPInfFile);
  473. }
  474. SHCloseKey(hkZones);
  475. SHCloseKey(hkZoneMap);
  476. SHCloseKey(hkInetSettings);
  477. SHCloseKey(hkP3P);
  478. return bRet;
  479. }
  480. #define PICSRULES_APPROVEDSITES 0
  481. #define PICSRULES_ALWAYS 1
  482. #define PICSRULES_NEVER 0
  483. //This indicates which member is valid in a PICSRulesPolicy
  484. //Class
  485. enum PICSRulesPolicyAttribute
  486. {
  487. PR_POLICY_NONEVALID,
  488. PR_POLICY_REJECTBYURL,
  489. PR_POLICY_ACCEPTBYURL,
  490. PR_POLICY_REJECTIF,
  491. PR_POLICY_ACCEPTIF,
  492. PR_POLICY_REJECTUNLESS,
  493. PR_POLICY_ACCEPTUNLESS
  494. };
  495. /////////////////////////////////////////////////////////////////////
  496. static void importRatingsForRSOP(HKEY hkRat, LPCTSTR szFile)
  497. {
  498. __try
  499. {
  500. TCHAR szSection[32] = IK_FF_GENERAL;
  501. TCHAR szKey[32];
  502. TCHAR szInt[32];
  503. // write out ratings system filenames
  504. // not sure why, but code below only loops through 10
  505. TCHAR szTemp[MAX_PATH];
  506. DWORD cbSize = 0;
  507. for (int nFile = 0; nFile < 10; nFile++)
  508. {
  509. wnsprintf(szKey, countof(szKey), IK_FILENAME_FMT, nFile);
  510. cbSize = sizeof(szTemp);
  511. if (RegQueryValueEx(hkRat, szKey, NULL, NULL, (LPBYTE) szTemp, &cbSize) != ERROR_SUCCESS)
  512. break;
  513. WritePrivateProfileString(szSection, szKey, szTemp, szFile);
  514. }
  515. // write out checked values from General tab
  516. HKEY hkDef = NULL;
  517. DWORD dwTemp = 0;
  518. if (ERROR_SUCCESS == SHOpenKey(hkRat, TEXT(".Default"), KEY_DEFAULT_ACCESS, &hkDef))
  519. {
  520. cbSize = sizeof(dwTemp);
  521. if (ERROR_SUCCESS == RegQueryValueEx(hkDef, VIEW_UNKNOWN_RATED_SITES,
  522. NULL, NULL, (LPBYTE)&dwTemp, &cbSize))
  523. {
  524. wnsprintf(szInt, countof(szInt), TEXT("%lu"), dwTemp);
  525. WritePrivateProfileString(szSection, VIEW_UNKNOWN_RATED_SITES, szInt, szFile);
  526. }
  527. cbSize = sizeof(dwTemp);
  528. if (ERROR_SUCCESS == RegQueryValueEx(hkDef, PASSWORD_OVERRIDE_ENABLED,
  529. NULL, NULL, (LPBYTE)&dwTemp, &cbSize))
  530. {
  531. wnsprintf(szInt, countof(szInt), TEXT("%lu"), dwTemp);
  532. WritePrivateProfileString(szSection, PASSWORD_OVERRIDE_ENABLED, szInt, szFile);
  533. }
  534. }
  535. // write out always viewable & never viewable sites from the approved sites tab
  536. // See msrating.dll for src
  537. HKEY hkUser = NULL;
  538. HKEY hkPRPolicy = NULL;
  539. DWORD nPolicies = 0;
  540. cbSize = sizeof(dwTemp);
  541. HRESULT hr = SHOpenKey(hkRat, TEXT("PICSRules\\.Default"), KEY_DEFAULT_ACCESS, &hkUser);
  542. if (ERROR_SUCCESS == hr)
  543. {
  544. hr = SHOpenKey(hkUser, TEXT("0\\PRPolicy"), KEY_DEFAULT_ACCESS, &hkPRPolicy);
  545. if (ERROR_SUCCESS == hr)
  546. {
  547. hr = RegQueryValueEx(hkPRPolicy, TEXT("PRNumPolicy"), NULL, NULL,
  548. (LPBYTE)&nPolicies, &cbSize);
  549. }
  550. }
  551. if (ERROR_SUCCESS == hr)
  552. {
  553. TCHAR szNumber[MAX_PATH];
  554. HKEY hkItem = NULL;
  555. HKEY hkPolicySub = NULL;
  556. DWORD dwAttrib = PR_POLICY_NONEVALID;
  557. DWORD nExpressions = 0;
  558. long nApproved = 0;
  559. long nDisapproved = 0;
  560. for (DWORD nItem = 0; nItem < nPolicies; nItem++)
  561. {
  562. wnsprintf(szNumber, countof(szNumber), TEXT("%d"), nItem);
  563. hr = SHOpenKey(hkPRPolicy, szNumber, KEY_DEFAULT_ACCESS, &hkItem);
  564. if (ERROR_SUCCESS == hr)
  565. {
  566. cbSize = sizeof(dwAttrib);
  567. hr = RegQueryValueEx(hkItem, TEXT("PRPPolicyAttribute"), NULL, NULL,
  568. (LPBYTE)&dwAttrib, &cbSize);
  569. }
  570. if (ERROR_SUCCESS == hr)
  571. hr = SHOpenKey(hkItem, TEXT("PRPPolicySub"), KEY_DEFAULT_ACCESS, &hkPolicySub);
  572. if (ERROR_SUCCESS == hr)
  573. {
  574. cbSize = sizeof(nExpressions);
  575. hr = RegQueryValueEx(hkPolicySub, TEXT("PRNumURLExpressions"), NULL, NULL,
  576. (LPBYTE)&nExpressions, &cbSize);
  577. }
  578. if (ERROR_SUCCESS == hr)
  579. {
  580. HKEY hByURLKey = NULL;
  581. TCHAR szURL[INTERNET_MAX_URL_LENGTH];
  582. for (DWORD nExp = 0; nExp < nExpressions; nExp++)
  583. {
  584. wnsprintf(szNumber, countof(szNumber), TEXT("%d"), nExp);
  585. hr = SHOpenKey(hkPolicySub, szNumber, KEY_DEFAULT_ACCESS, &hByURLKey);
  586. cbSize = sizeof(szURL);
  587. if (ERROR_SUCCESS == hr)
  588. {
  589. hr = RegQueryValueEx(hByURLKey, TEXT("PRBUUrl"), NULL, NULL,
  590. (LPBYTE)szURL, &cbSize);
  591. }
  592. if (ERROR_SUCCESS == hr)
  593. {
  594. if (PR_POLICY_REJECTBYURL == dwAttrib)
  595. {
  596. wnsprintf(szKey, countof(szKey), IK_DISAPPROVED_FMT, nDisapproved++);
  597. WritePrivateProfileString(szSection, szKey, szURL, szFile);
  598. }
  599. else if (PR_POLICY_ACCEPTBYURL == dwAttrib)
  600. {
  601. wnsprintf(szKey, countof(szKey), IK_APPROVED_FMT, nApproved++);
  602. WritePrivateProfileString(szSection, szKey, szURL, szFile);
  603. }
  604. }
  605. }
  606. }
  607. }
  608. }
  609. // write out select ratings bureau
  610. cbSize = sizeof(szTemp);
  611. if (ERROR_SUCCESS == RegQueryValueEx(hkRat, IK_BUREAU, NULL, NULL,
  612. (LPBYTE)szTemp, &cbSize))
  613. {
  614. WritePrivateProfileString(szSection, IK_BUREAU, szTemp, szFile);
  615. }
  616. }
  617. __except(TRUE)
  618. {
  619. }
  620. }
  621. /////////////////////////////////////////////////////////////////////
  622. static BOOL importRatingsHelper(LPCTSTR pcszInsFile, LPCTSTR pcszRatingsWorkDir, LPCTSTR pcszRatingsInf, BOOL fImportRatings)
  623. {
  624. BOOL bRet = FALSE;
  625. HKEY hkRat = NULL;
  626. BOOL bRatLoadedAsHive = FALSE;
  627. if (pcszInsFile == NULL || pcszRatingsInf == NULL)
  628. return FALSE;
  629. // Before processing anything, first clear out the entries in the INS file and delete work dirs
  630. // clear out the entries in the INS file that correspond to importing ratings
  631. InsDeleteKey(SECURITY_IMPORTS, TEXT("ImportRatings"), pcszInsFile);
  632. InsDeleteKey(IS_EXTREGINF, TEXT("Ratings"), pcszInsFile);
  633. InsDeleteKey(IS_EXTREGINF_HKLM, TEXT("Ratings"), pcszInsFile);
  634. // blow away the pcszRatingsWorkDir and pcszRatingsInf
  635. if (pcszRatingsWorkDir != NULL)
  636. PathRemovePath(pcszRatingsWorkDir);
  637. PathRemovePath(pcszRatingsInf);
  638. if (!fImportRatings)
  639. return TRUE;
  640. if (ratingsInRegistry())
  641. {
  642. SHOpenKeyHKLM(REG_KEY_RATINGS, KEY_DEFAULT_ACCESS, &hkRat);
  643. }
  644. else
  645. {
  646. TCHAR szRatFile[MAX_PATH];
  647. GetSystemDirectory(szRatFile, countof(szRatFile));
  648. PathAppend(szRatFile, TEXT("ratings.pol"));
  649. if (RegLoadKey(HKEY_LOCAL_MACHINE, POLICYDATA, szRatFile) == ERROR_SUCCESS)
  650. {
  651. bRatLoadedAsHive = TRUE;
  652. SHOpenKeyHKLM(REG_KEY_POLICY_DATA, KEY_DEFAULT_ACCESS, &hkRat);
  653. }
  654. }
  655. if (hkRat != NULL)
  656. {
  657. TCHAR szFullInfName[MAX_PATH];
  658. HANDLE hInf;
  659. if (pcszRatingsWorkDir != NULL && PathIsFileSpec(pcszRatingsInf)) // create RATINGS.INF under pcszRatingsWorkDir
  660. PathCombine(szFullInfName, pcszRatingsWorkDir, pcszRatingsInf);
  661. else
  662. StrCpy(szFullInfName, pcszRatingsInf);
  663. // create RATINGS.INF file
  664. if ((hInf = CreateNewFile(szFullInfName)) != INVALID_HANDLE_VALUE)
  665. {
  666. INT i;
  667. HKEY hkDef;
  668. TCHAR szSysDir[MAX_PATH];
  669. WriteStringToFile(hInf, RATINGS_INF_ADD, StrLen(RATINGS_INF_ADD));
  670. // convert the system path to %11% ldid
  671. for (i = 0; i < 10; i++)
  672. {
  673. TCHAR szNameParm[16];
  674. TCHAR szFileName[MAX_PATH];
  675. DWORD cbSize;
  676. wnsprintf(szNameParm, countof(szNameParm), TEXT("FileName%i"), i);
  677. cbSize = sizeof(szFileName);
  678. if (RegQueryValueEx(hkRat, szNameParm, NULL, NULL, (LPBYTE) szFileName, &cbSize) != ERROR_SUCCESS)
  679. break;
  680. if (PathIsFullPath(szFileName))
  681. {
  682. TCHAR szEncFileName[MAX_PATH];
  683. // BUBBUG: Should we check if the path is really the system dir?
  684. wnsprintf(szEncFileName, countof(szEncFileName), TEXT("%%11%%\\%s"), PathFindFileName(szFileName));
  685. RegSetValueEx(hkRat, szNameParm, 0, REG_SZ, (CONST BYTE *)szEncFileName, (DWORD)StrCbFromSz(szEncFileName));
  686. }
  687. }
  688. RegFlushKey(hkRat);
  689. ExportRegKey2Inf(hkRat, TEXT("HKLM"), REG_KEY_RATINGS, hInf);
  690. WriteStringToFile(hInf, (LPCVOID) TEXT("\r\n"), 2);
  691. if (SHOpenKey(hkRat, TEXT(".Default"), KEY_DEFAULT_ACCESS, &hkDef) == ERROR_SUCCESS)
  692. {
  693. TCHAR szDefault[MAX_PATH];
  694. wnsprintf(szDefault, countof(szDefault), TEXT("%s\\.Default"), REG_KEY_RATINGS);
  695. ExportRegTree2Inf(hkDef, TEXT("HKLM"), szDefault, hInf);
  696. SHCloseKey(hkDef);
  697. }
  698. // new IE5 specific key
  699. if (SHOpenKey(hkRat, TEXT("PICSRules"), KEY_DEFAULT_ACCESS, &hkDef) == ERROR_SUCCESS)
  700. {
  701. TCHAR szRules[MAX_PATH];
  702. wnsprintf(szRules, countof(szRules), TEXT("%s\\PICSRules"), REG_KEY_RATINGS);
  703. ExportRegTree2Inf(hkDef, TEXT("HKLM"), szRules, hInf);
  704. SHCloseKey(hkDef);
  705. }
  706. if (bRatLoadedAsHive)
  707. {
  708. HKEY hkRatsInReg;
  709. // eventhough ratings has been loaded as a hive, the password is still in the registry
  710. if (SHOpenKeyHKLM(REG_KEY_RATINGS, KEY_DEFAULT_ACCESS, &hkRatsInReg) == ERROR_SUCCESS)
  711. {
  712. ExportRegKey2Inf(hkRatsInReg, TEXT("HKLM"), REG_KEY_RATINGS, hInf);
  713. SHCloseKey(hkRatsInReg);
  714. }
  715. // browser ratings code does some weird stuff with their hive, so we have to go to
  716. // the right level to get the new IE5 PICSRules key
  717. if (SHOpenKey(hkRat, REG_KEY_RATINGS TEXT("\\PICSRules"), KEY_DEFAULT_ACCESS, &hkRatsInReg) == ERROR_SUCCESS)
  718. {
  719. TCHAR szRules[MAX_PATH];
  720. wnsprintf(szRules, countof(szRules), TEXT("%s\\PICSRules"), REG_KEY_RATINGS);
  721. ExportRegTree2Inf(hkDef, TEXT("HKLM"), szRules, hInf);
  722. SHCloseKey(hkDef);
  723. }
  724. }
  725. CloseHandle(hInf);
  726. // update the INS file
  727. InsWriteBool(SECURITY_IMPORTS, TEXT("ImportRatings"), TRUE, pcszInsFile);
  728. wnsprintf(szSysDir, countof(szSysDir), TEXT("*,%s,") IS_DEFAULTINSTALL, PathFindFileName(pcszRatingsInf));
  729. WritePrivateProfileString(IS_EXTREGINF, TEXT("Ratings"), szSysDir, pcszInsFile);
  730. // write to new ExtRegInf.HKLM section
  731. if (!InsIsSectionEmpty(TEXT("AddReg.HKLM"), szFullInfName))
  732. {
  733. wnsprintf(szSysDir, countof(szSysDir), TEXT("%s,IEAKInstall.HKLM"), PathFindFileName(pcszRatingsInf));
  734. WritePrivateProfileString(IS_EXTREGINF_HKLM, TEXT("Ratings"), szSysDir, pcszInsFile);
  735. }
  736. bRet = TRUE;
  737. // restore the %11% ldid paths to the system dir
  738. GetSystemDirectory(szSysDir, countof(szSysDir));
  739. for (i = 0; i < 10; i++)
  740. {
  741. TCHAR szNameParm[16];
  742. TCHAR szEncFileName[MAX_PATH];
  743. DWORD cbSize;
  744. wnsprintf(szNameParm, countof(szNameParm), TEXT("FileName%i"), i);
  745. cbSize = sizeof(szEncFileName);
  746. if (RegQueryValueEx(hkRat, szNameParm, NULL, NULL, (LPBYTE) szEncFileName, &cbSize) != ERROR_SUCCESS)
  747. break;
  748. if (PathIsFullPath(szEncFileName))
  749. {
  750. TCHAR szFileName[MAX_PATH];
  751. PathCombine(szFileName, szSysDir, PathFindFileName(szEncFileName));
  752. RegSetValueEx(hkRat, szNameParm, 0, REG_SZ, (CONST BYTE *)szFileName, (DWORD)StrCbFromSz(szFileName));
  753. }
  754. }
  755. RegFlushKey(hkRat);
  756. }
  757. // create RATRSOP.INF file
  758. TCHAR szRRSOPInfFile[MAX_PATH];
  759. StrCpy(szRRSOPInfFile, szFullInfName);
  760. PathRemoveFileSpec(szRRSOPInfFile);
  761. StrCat(szRRSOPInfFile, TEXT("\\ratrsop.inf"));
  762. importRatingsForRSOP(hkRat, szRRSOPInfFile);
  763. SHCloseKey(hkRat);
  764. }
  765. if (bRatLoadedAsHive)
  766. RegUnLoadKey(HKEY_LOCAL_MACHINE, POLICYDATA);
  767. return bRet;
  768. }
  769. static BOOL ratingsInRegistry(VOID)
  770. {
  771. BOOL fRet = TRUE;
  772. if (g_fRunningOnNT)
  773. return fRet;
  774. if (fRet)
  775. {
  776. HKEY hk;
  777. fRet = FALSE;
  778. if (SHOpenKeyHKLM(TEXT("System\\CurrentControlSet\\Control\\Update"), KEY_DEFAULT_ACCESS, &hk) == ERROR_SUCCESS)
  779. {
  780. DWORD dwData, cbSize;
  781. cbSize = sizeof(dwData);
  782. if (RegQueryValueEx(hk, TEXT("UpdateMode"), 0, NULL, (LPBYTE) &dwData, &cbSize) == ERROR_SUCCESS && dwData)
  783. fRet = TRUE;
  784. SHCloseKey(hk);
  785. }
  786. }
  787. if (fRet)
  788. {
  789. HKEY hk;
  790. fRet = FALSE;
  791. if (SHOpenKeyHKLM(TEXT("Network\\Logon"), KEY_DEFAULT_ACCESS, &hk) == ERROR_SUCCESS)
  792. {
  793. DWORD dwData, cbSize;
  794. cbSize = sizeof(dwData);
  795. if (RegQueryValueEx(hk, TEXT("UserProfiles"), 0, NULL, (LPBYTE) &dwData, &cbSize) == ERROR_SUCCESS && dwData)
  796. fRet = TRUE;
  797. SHCloseKey(hk);
  798. }
  799. }
  800. if (fRet)
  801. {
  802. HKEY hk;
  803. fRet = FALSE;
  804. if (SHOpenKeyHKLM(REG_KEY_RATINGS, KEY_DEFAULT_ACCESS, &hk) == ERROR_SUCCESS)
  805. {
  806. HKEY hkRatDef;
  807. if (SHOpenKey(hk, TEXT(".Default"), KEY_DEFAULT_ACCESS, &hkRatDef) == ERROR_SUCCESS)
  808. {
  809. fRet = TRUE;
  810. SHCloseKey(hkRatDef);
  811. }
  812. SHCloseKey(hk);
  813. }
  814. }
  815. return fRet;
  816. }