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.

458 lines
12 KiB

  1. //=--------------------------------------------------------------------------=
  2. // iodver.cpp
  3. //=--------------------------------------------------------------------------=
  4. // Copyright 1997-1998 Microsoft Corporation. All Rights Reserved.
  5. //
  6. //
  7. //
  8. #include "string.h"
  9. #include "pch.h"
  10. #include "advpub.h"
  11. #include "iesetup.h"
  12. #include <inetreg.h>
  13. #include <shlwapi.h>
  14. #include <wininet.h>
  15. WINUSERAPI HWND WINAPI GetShellWindow(void);
  16. HINSTANCE g_hInstance = NULL;
  17. STDAPI_(BOOL) DllMain(HANDLE hDll, DWORD dwReason, void *lpReserved)
  18. {
  19. DWORD dwThreadID;
  20. switch(dwReason)
  21. {
  22. case DLL_PROCESS_ATTACH:
  23. g_hInstance = (HINSTANCE)hDll;
  24. break;
  25. case DLL_PROCESS_DETACH:
  26. break;
  27. default:
  28. break;
  29. }
  30. return TRUE;
  31. }
  32. STDAPI DllRegisterServer(void)
  33. {
  34. // BUGBUG: pass back return from RegInstall ?
  35. RegInstall(g_hInstance, "DllReg", NULL);
  36. return S_OK;
  37. }
  38. STDAPI DllUnregisterServer(void)
  39. {
  40. RegInstall(g_hInstance, "DllUnreg", NULL);
  41. return S_OK;
  42. }
  43. BOOL IsWinNT4()
  44. {
  45. OSVERSIONINFO VerInfo;
  46. VerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  47. GetVersionEx(&VerInfo);
  48. if (VerInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
  49. {
  50. return (VerInfo.dwMajorVersion == 4) ;
  51. }
  52. return FALSE;
  53. }
  54. BOOL IsWinXP()
  55. {
  56. OSVERSIONINFO VerInfo;
  57. VerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  58. GetVersionEx(&VerInfo);
  59. if (VerInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
  60. {
  61. return (VerInfo.dwMajorVersion > 5) ||
  62. (VerInfo.dwMajorVersion == 5 && VerInfo.dwMinorVersion >= 1);
  63. }
  64. return FALSE;
  65. }
  66. STDAPI DllInstall(BOOL bInstall, LPCWSTR lpCmdLine)
  67. {
  68. // BUGBUG: pass back return from RegInstall ?
  69. if (bInstall)
  70. {
  71. RegInstall(g_hInstance, "DllUninstall", NULL);
  72. if(IsWinNT4())
  73. RegInstall(g_hInstance, "DllInstall.NT4Only", NULL);
  74. else if(IsWinXP())
  75. RegInstall(g_hInstance, "DllInstall.WinXP", NULL);
  76. else
  77. RegInstall(g_hInstance, "DllInstall", NULL);
  78. }
  79. else
  80. RegInstall(g_hInstance, "DllUninstall", NULL);
  81. return S_OK;
  82. }
  83. const TCHAR * const szAdvPack = TEXT("advpack.dll");
  84. const TCHAR * const szExecuteCab = TEXT("ExecuteCab");
  85. const TCHAR * const szKeyComponentAdmin = TEXT("Software\\Microsoft\\Active Setup\\Installed Components\\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}");
  86. const TCHAR * const szKeyComponentUser = TEXT("Software\\Microsoft\\Active Setup\\Installed Components\\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}");
  87. char szSectionHardenAdmin[] = "IEHardenAdmin";
  88. char szSectionSoftenAdmin[] = "IESoftenAdmin";
  89. char szSectionHardenUser[] = "IEHardenUser";
  90. char szSectionSoftenUser[] = "IESoftenUser";
  91. char szSectionHardenMachine[] = "IEHardenMachine";
  92. char szSectionSoftenMachine[] = "IESoftenMachine";
  93. const TCHAR * const szLocale = TEXT("Locale");
  94. const TCHAR * const szVersion = TEXT("Version");
  95. //Copy data from HKLM to HKCU
  96. HRESULT CopyRegValue(LPCTSTR szSubKey, LPCTSTR szValue)
  97. {
  98. BYTE buffer[128];
  99. HKEY hKeyDst = NULL, hKeySrc = NULL;
  100. HRESULT hResult;
  101. DWORD dwSize = sizeof(buffer);
  102. hResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szSubKey, 0, KEY_QUERY_VALUE, &hKeySrc);
  103. if (FAILED(hResult))
  104. goto Cleanup;
  105. hResult = RegQueryValueEx(hKeySrc, szValue, NULL, NULL, (LPBYTE)buffer, &dwSize);
  106. if (FAILED(hResult))
  107. goto Cleanup;
  108. hResult = RegCreateKeyEx(HKEY_CURRENT_USER, szSubKey, 0, NULL,
  109. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKeyDst, NULL);
  110. if (FAILED(hResult))
  111. goto Cleanup;
  112. hResult = RegSetValueEx(hKeyDst, szValue, NULL, REG_SZ, (CONST BYTE *)buffer, dwSize);
  113. Cleanup:
  114. if (hKeySrc)
  115. RegCloseKey(hKeySrc);
  116. if (hKeyDst)
  117. RegCloseKey(hKeyDst);
  118. return hResult;
  119. }
  120. BOOL IsNtSetupRunning()
  121. {
  122. HKEY hKey;
  123. long lRes = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"system\\Setup",
  124. 0, KEY_READ, &hKey);
  125. if(lRes)
  126. return false;
  127. DWORD dwSetupRunning;
  128. DWORD dwLen = sizeof(DWORD);
  129. lRes = RegQueryValueExW(hKey, L"SystemSetupInProgress", NULL, NULL,
  130. (LPBYTE)&dwSetupRunning, &dwLen);
  131. RegCloseKey(hKey);
  132. if(lRes == ERROR_SUCCESS && (dwSetupRunning == 1))
  133. {
  134. return true;
  135. }
  136. return false;
  137. }
  138. // Return value: 1: Installed. 0: Uninstalled. -1: Not installed (component does not exist in registry)
  139. int IsInstalled(const TCHAR * const szKeyComponent)
  140. {
  141. const TCHAR *szIsInstalled = TEXT("IsInstalled");
  142. int bInstalled = -1;
  143. DWORD dwValue, dwSize;
  144. HKEY hKey;
  145. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyComponent, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
  146. {
  147. dwSize = sizeof(dwValue);
  148. if (RegQueryValueEx( hKey, szIsInstalled, NULL, NULL, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS)
  149. {
  150. dwValue = 0;
  151. }
  152. bInstalled = (dwValue != 0);
  153. RegCloseKey(hKey);
  154. }
  155. //else Not installed.
  156. return bInstalled;
  157. }
  158. //Get the inf file from system32 and run this section
  159. HRESULT RunInfSection(const char * const szInfFile, const char * const szSection)
  160. {
  161. CABINFO CabInfo;
  162. char szInfPathName[MAX_PATH];
  163. HRESULT hResult;
  164. memset(&CabInfo, 0, sizeof(CabInfo));
  165. GetSystemDirectory(szInfPathName, sizeof(szInfPathName));
  166. AddPath(szInfPathName, szInfFile);
  167. CabInfo.pszInf = szInfPathName;
  168. CabInfo.dwFlags = ALINF_QUIET;
  169. CabInfo.pszSection = (PSTR)szSection;
  170. hResult = ExecuteCab(NULL, &CabInfo, NULL);
  171. return hResult;
  172. }
  173. HRESULT RunPerUserInfSection(char * szSection)
  174. {
  175. HRESULT hResult;
  176. hResult = RunInfSection("ieuinit.inf", szSection);
  177. return hResult;
  178. }
  179. const char * const szRegValueDefaultHomepage = "Default_Page_URL";
  180. void GetOEMDefaultPageURL(LPTSTR szURL, DWORD cbData)
  181. {
  182. const TCHAR * const szIEDefaultPageURL = "http://www.microsoft.com/isapi/redir.dll?prd=ie&pver=6&ar=msnhome";
  183. const TCHAR * const szIEStartPage = "http://www.microsoft.com/isapi/redir.dll?prd={SUB_PRD}&clcid={SUB_CLSID}&pver={SUB_PVER}&ar=home";
  184. szURL[0] = 0;
  185. if (ERROR_SUCCESS == SHRegGetUSValue(REGSTR_PATH_MAIN, szRegValueDefaultHomepage, NULL,
  186. (LPVOID)szURL, &cbData, TRUE, NULL, NULL))
  187. {
  188. if (0 == StrCmpI(szURL, szIEDefaultPageURL))
  189. {
  190. //Ignore the default page url set by in.inf
  191. szURL[0] = 0;
  192. }
  193. }
  194. if (szURL[0] == 0)
  195. {
  196. if (ERROR_SUCCESS == SHRegGetUSValue(REGSTR_PATH_MAIN, REGSTR_VAL_STARTPAGE, NULL,
  197. (LPVOID)szURL, &cbData, TRUE, NULL, NULL))
  198. {
  199. if (0 == StrCmpI(szURL, szIEStartPage))
  200. {
  201. //Ignore the start page url set by shdocvw.dll selfreg.inf
  202. szURL[0] = 0;
  203. }
  204. }
  205. }
  206. }
  207. //Set IE Hardening hompage, when there is no OEM customized default homepage URL.
  208. HRESULT SetIEHardeningHomepage()
  209. {
  210. const char * const szHomePageFileName = "homepage.inf";
  211. HRESULT hResult = S_OK;
  212. TCHAR szOEMHomepage[INTERNET_MAX_URL_LENGTH] = "";
  213. DWORD cbOEMHomepage;
  214. cbOEMHomepage = sizeof(szOEMHomepage);
  215. GetOEMDefaultPageURL(szOEMHomepage, cbOEMHomepage);
  216. if (szOEMHomepage[0])
  217. {
  218. return hResult;
  219. }
  220. if (IsNTAdmin(0, NULL))
  221. {
  222. switch (IsInstalled(szKeyComponentAdmin))
  223. {
  224. case 1:
  225. hResult = RunInfSection(szHomePageFileName, "Install.HardenAdmin");
  226. break;
  227. case 0:
  228. hResult = RunInfSection(szHomePageFileName, "Install.SoftenAdmin");
  229. break;
  230. }
  231. }
  232. else
  233. {
  234. switch (IsInstalled(szKeyComponentUser))
  235. {
  236. case 1:
  237. hResult = RunInfSection(szHomePageFileName, "Install.HardenUser");
  238. break;
  239. case 0:
  240. hResult = RunInfSection(szHomePageFileName, "Install.SoftenUser");
  241. break;
  242. }
  243. }
  244. return hResult;
  245. }
  246. //Set the homepage for IE peruser stub
  247. HRESULT WINAPI SetFirstHomepage()
  248. {
  249. HRESULT hResult = S_OK;
  250. TCHAR szOEMHomepage[INTERNET_MAX_URL_LENGTH] = "";
  251. DWORD cbOEMHomepage;
  252. HKEY hKey;
  253. cbOEMHomepage = sizeof(szOEMHomepage);
  254. GetOEMDefaultPageURL(szOEMHomepage, cbOEMHomepage);
  255. if (szOEMHomepage[0])
  256. {
  257. //Delete the Default_Page_URL in HKCU
  258. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, REGSTR_PATH_MAIN, 0, KEY_SET_VALUE, &hKey))
  259. {
  260. RegDeleteValue(hKey, szRegValueDefaultHomepage);
  261. RegCloseKey(hKey);
  262. }
  263. hResult = S_OK;
  264. }
  265. else
  266. {
  267. hResult = SetIEHardeningHomepage();
  268. }
  269. return hResult;
  270. }
  271. // Forward declaration:
  272. HRESULT WINAPI IEHardenMachineNow(HWND, HINSTANCE, PSTR pszCmd, INT);
  273. HRESULT WINAPI IEHardenAdmin(HWND, HINSTANCE, PSTR pszCmd, INT)
  274. {
  275. HRESULT hr = S_OK;
  276. if (IsNTAdmin(0, NULL))
  277. {
  278. switch (IsInstalled(szKeyComponentAdmin))
  279. {
  280. case 1:
  281. if(SUCCEEDED(hr = RunPerUserInfSection(szSectionHardenAdmin)))
  282. {
  283. // Harden the machine:
  284. hr = IEHardenMachineNow(NULL, NULL, "i", 0);
  285. }
  286. break;
  287. case 0:
  288. if(SUCCEEDED(hr = RunPerUserInfSection(szSectionSoftenAdmin)))
  289. {
  290. // Soften the machine only if both user and admin are softened:
  291. hr = IEHardenMachineNow(NULL, NULL, "u", 0);
  292. }
  293. break;
  294. default:
  295. hr = E_FAIL;
  296. }
  297. if (SUCCEEDED(hr))
  298. {
  299. hr = SetIEHardeningHomepage();
  300. }
  301. }
  302. return hr;
  303. }
  304. HRESULT WINAPI IEHardenAdminNow(HWND, HINSTANCE, PSTR, INT)
  305. {
  306. HRESULT hr = S_OK;
  307. if (!IsNtSetupRunning())
  308. {
  309. hr = IEHardenAdmin(NULL, NULL, NULL, 0);
  310. if (SUCCEEDED(hr))
  311. {
  312. CopyRegValue(szKeyComponentAdmin, szLocale);
  313. CopyRegValue(szKeyComponentAdmin, szVersion);
  314. }
  315. }
  316. return hr;
  317. }
  318. HRESULT WINAPI IEHardenUser(HWND, HINSTANCE, PSTR pszCmd, INT)
  319. {
  320. HRESULT hr = S_OK;
  321. if (!IsNTAdmin(0, NULL))
  322. {
  323. switch (IsInstalled(szKeyComponentUser))
  324. {
  325. case 1:
  326. hr = RunPerUserInfSection(szSectionHardenUser);
  327. break;
  328. case 0:
  329. hr = RunPerUserInfSection(szSectionSoftenUser);
  330. break;
  331. default:
  332. hr = E_FAIL;
  333. }
  334. if (SUCCEEDED(hr))
  335. {
  336. hr = SetIEHardeningHomepage();
  337. }
  338. }
  339. return hr;
  340. }
  341. HRESULT WINAPI IEHardenMachineNow(HWND, HINSTANCE, PSTR pszCmd, INT)
  342. {
  343. HRESULT hr = E_INVALIDARG;
  344. // Set per-machine inetcpl default settings according to user, not admin
  345. // Requires the command line because this may run during NT setup.
  346. if (pszCmd)
  347. {
  348. //Install or Uninstall
  349. if (pszCmd[0] == 'i' || pszCmd[0] == 'I')
  350. hr = RunPerUserInfSection(szSectionHardenMachine);
  351. else if (pszCmd[0] == 'u' || pszCmd[0] == 'U')
  352. {
  353. // Soften the machine only if both user and admin are softened:
  354. if (1 != IsInstalled(szKeyComponentAdmin) && 1 != IsInstalled(szKeyComponentUser))
  355. hr = RunPerUserInfSection(szSectionSoftenMachine);
  356. else
  357. hr = S_OK;
  358. }
  359. }
  360. return hr;
  361. }
  362. //=--------------------------------------------------------------------------=
  363. // CRT stubs
  364. //=--------------------------------------------------------------------------=
  365. // these two things are here so the CRTs aren't needed. this is good.
  366. //
  367. // basically, the CRTs define this to pull in a bunch of stuff. we'll just
  368. // define them here so we don't get an unresolved external.
  369. //
  370. // TODO: if you are going to use the CRTs, then remove this line.
  371. //
  372. extern "C" int _fltused = 1;
  373. extern "C" int _cdecl _purecall(void)
  374. {
  375. // FAIL("Pure virtual function called.");
  376. return 0;
  377. }
  378. #ifndef _X86_
  379. extern "C" void _fpmath() {}
  380. #endif