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.

563 lines
18 KiB

  1. #include "asctlpch.h"
  2. #include "globals.h"
  3. #include "resource.h"
  4. #include "util.h"
  5. #include "util2.h"
  6. #include "sdsutils.h"
  7. #ifdef TESTCERT
  8. #define TESTCERTVALUE 0xA0
  9. #endif
  10. const char c_gszMSTrustRegKey[] = "Software\\Microsoft\\Windows\\CurrentVersion\\WinTrust\\Trust Providers\\Software Publishing\\Trust Database\\0";
  11. const char c_gszMSTrust[] = "bhhphijojgfcdocagmhjgjbhmieinfap jpjmcfmhckkdfknkfemjikfiodeelkbd";
  12. const char c_gszMSTrust2[] = "bhhphijojgfcdocagmhjgjbhmieinfap immbkmbpjfdkajbkncahcedfmndgehba";
  13. const char c_gszMSTrust3[] = "bhhphijojgfcdocagmhjgjbhmieinfap doamnolbnlpmdlpnkcnpckgfimpaaicl"; // New MS Europe
  14. const char c_gszMSTrust4[] = "bhhphijojgfcdocagmhjgjbhmieinfap hbgflemajngobcablgnalaidgojggghj";
  15. const char c_gszMSTrust5[] = "bhhphijojgfcdocagmhjgjbhmieinfap kefdggbdmbmgbogjdcnmkoodcknmmghc"; // New MS Europe effective from 4/16/99
  16. const char c_gszMSTrust6[] = "bhhphijojgfcdocagmhjgjbhmieinfap debgjcefniaahdamnhbggedppfiianff"; // new MS effective from 4/16/99
  17. const char c_gszMSTrust7[] = "bhhphijojgfcdocagmhjgjbhmieinfap fmgfeljfbejhfcbbgnokplkipiiopchf"; // new MS effective from 4/16/2000
  18. const char c_gszMSTrust8[] = "bhhphijojgfcdocagmhjgjbhmieinfap jcphiillknjhbelgeadhnbgpjajjkhdh"; // New MS Europe effective from 4/16/2000
  19. const char c_gszMSTrust9[] = "klhfnkecpinogjmfaoamiabmhafnjldh adaaaaaaaaaahihcddgb"; // New MS effective 12/22/2000
  20. const char c_gszMSTrust10[] = "klhfnkecpinogjmfaoamiabmhafnjldh alaaaaaaaaaainckaggb"; // New MS effective 3/29/2001
  21. const char c_gszMSTrust11[] = "klhfnkecpinogjmfaoamiabmhafnjldh aeaaaaaaaaaafpnldegb"; // New MS Europe effective from 12/22/2000
  22. const char c_gszMSTrustValue[] = "Microsoft Corporation";
  23. const char c_gszMSTrustValue3[] = "Microsoft Corporation (Europe)";
  24. #ifdef TESTCERT
  25. const char c_gszTrustStateRegKey[] = "Software\\Microsoft\\Windows\\CurrentVersion\\WinTrust\\Trust Providers\\Software Publishing";
  26. const char c_gszState[] = "State";
  27. #endif
  28. typedef struct _TRUSTEDPROVIDER
  29. {
  30. DWORD dwFlag;
  31. LPCSTR pValue; // Value part in the registeru
  32. LPCSTR pData; // Data part in the registry
  33. } TRUSTEDPROVIDER;
  34. TRUSTEDPROVIDER pTrustedProvider[] = { {MSTRUSTKEY1, c_gszMSTrust, c_gszMSTrustValue}, // MS US
  35. {MSTRUSTKEY2, c_gszMSTrust2, c_gszMSTrustValue}, // MS US
  36. {MSTRUSTKEY3, c_gszMSTrust3, c_gszMSTrustValue3}, // MS Europa
  37. {MSTRUSTKEY4, c_gszMSTrust4, c_gszMSTrustValue}, // MS US
  38. {MSTRUSTKEY5, c_gszMSTrust5, c_gszMSTrustValue3}, // New MS Europe effective from 4/16/99
  39. {MSTRUSTKEY6, c_gszMSTrust6, c_gszMSTrustValue}, // new MS effective from 4/16/99
  40. {MSTRUSTKEY7, c_gszMSTrust7, c_gszMSTrustValue}, // new MS effective from 4/16/2000
  41. {MSTRUSTKEY8, c_gszMSTrust8, c_gszMSTrustValue3}, // New MS Europe effective from 4/16/2000
  42. {MSTRUSTKEY9, c_gszMSTrust9, c_gszMSTrustValue3}, // New MS Europe effective from 4/16/2000
  43. {MSTRUSTKEY10, c_gszMSTrust10, c_gszMSTrustValue3}, // New MS Europe effective from 4/16/2000
  44. {MSTRUSTKEY11, c_gszMSTrust11, c_gszMSTrustValue3}, // New MS Europe effective from 12/22/2000
  45. {0,NULL, NULL} }; // Terminates the array.
  46. HFONT g_hFont = NULL;
  47. #define ACTIVESETUP_KEY "Software\\Microsoft\\Active Setup"
  48. #define TRUSTKEYREG "AllowMSTrustKey"
  49. // NT reboot
  50. //
  51. BOOL MyNTReboot()
  52. {
  53. HANDLE hToken;
  54. TOKEN_PRIVILEGES tkp;
  55. // get a token from this process
  56. if ( !OpenProcessToken( GetCurrentProcess(),
  57. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )
  58. {
  59. return FALSE;
  60. }
  61. // get the LUID for the shutdown privilege
  62. LookupPrivilegeValue( NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid );
  63. tkp.PrivilegeCount = 1;
  64. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  65. //get the shutdown privilege for this proces
  66. if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0))
  67. {
  68. return FALSE;
  69. }
  70. // shutdown the system and force all applications to close
  71. if (!ExitWindowsEx( EWX_REBOOT, 0 ) )
  72. {
  73. return FALSE;
  74. }
  75. return TRUE;
  76. }
  77. //=--------------------------------------------------------------------------=
  78. // Function name here
  79. //=--------------------------------------------------------------------------=
  80. // Function description
  81. //
  82. // Parameters:
  83. //
  84. // Returns:
  85. //
  86. // Notes:
  87. //
  88. HRESULT LaunchProcess(LPCSTR pszCmd, HANDLE *phProc, LPCSTR pszDir, UINT uShow)
  89. {
  90. STARTUPINFO startInfo;
  91. PROCESS_INFORMATION processInfo;
  92. HRESULT hr = S_OK;
  93. BOOL fRet;
  94. if(phProc)
  95. *phProc = NULL;
  96. // Create process on pszCmd
  97. ZeroMemory(&startInfo, sizeof(startInfo));
  98. startInfo.cb = sizeof(startInfo);
  99. startInfo.dwFlags |= STARTF_USESHOWWINDOW;
  100. startInfo.wShowWindow = (WORD)uShow;
  101. fRet = CreateProcess(NULL, (LPSTR) pszCmd, NULL, NULL, FALSE,
  102. NORMAL_PRIORITY_CLASS, NULL, pszDir, &startInfo, &processInfo);
  103. if(!fRet)
  104. return E_FAIL;
  105. if(phProc)
  106. *phProc = processInfo.hProcess;
  107. else
  108. CloseHandle(processInfo.hProcess);
  109. CloseHandle(processInfo.hThread);
  110. return S_OK;
  111. }
  112. #define SOFTBOOT_CMDLINE "softboot.exe /s:,60"
  113. // Display a dialog asking the user to restart Windows, with a button that
  114. // will do it for them if possible.
  115. //
  116. BOOL MyRestartDialog(HWND hParent, BOOL bShowPrompt)
  117. {
  118. char szBuf[256];
  119. char szTitle[256];
  120. UINT id = IDYES;
  121. if(bShowPrompt)
  122. {
  123. LoadSz(IDS_TITLE, szTitle, sizeof(szTitle));
  124. LoadSz(IDS_REBOOT, szBuf, sizeof(szBuf));
  125. id = MessageBox(hParent, szBuf, szTitle, MB_ICONQUESTION | MB_YESNO | MB_TASKMODAL | MB_SETFOREGROUND);
  126. }
  127. if ( id == IDYES )
  128. {
  129. // path to softboot plus a little slop for the command line
  130. char szBuf[MAX_PATH + 10];
  131. szBuf[0] = 0;
  132. GetSystemDirectory(szBuf, sizeof(szBuf));
  133. AddPath(szBuf, SOFTBOOT_CMDLINE);
  134. if(FAILED(LaunchProcess(szBuf, NULL, NULL, SW_SHOWNORMAL)))
  135. {
  136. if(g_fSysWin95)
  137. {
  138. ExitWindowsEx( EWX_REBOOT , 0 );
  139. }
  140. else
  141. {
  142. MyNTReboot();
  143. }
  144. }
  145. }
  146. return (id == IDYES);
  147. }
  148. int ErrMsgBox(LPSTR pszText, LPCSTR pszTitle, UINT mbFlags)
  149. {
  150. HWND hwndActive;
  151. int id;
  152. hwndActive = GetActiveWindow();
  153. id = MessageBox(hwndActive, pszText, pszTitle, mbFlags | MB_ICONERROR | MB_TASKMODAL);
  154. return id;
  155. }
  156. int LoadSz(UINT id, LPSTR pszBuf, UINT cMaxSize)
  157. {
  158. if(cMaxSize == 0)
  159. return 0;
  160. pszBuf[0] = 0;
  161. return LoadString(g_hInstance, id, pszBuf, cMaxSize);
  162. }
  163. void WriteMSTrustKey(BOOL bSet, DWORD dwSetMSTrustKey, BOOL bForceMSTrust /*= FALSE*/)
  164. {
  165. char szTmp[512];
  166. HKEY hKey;
  167. int i = 0;
  168. static BOOL fAllowMSTrustKey = 42;
  169. if(fAllowMSTrustKey == 42)
  170. {
  171. fAllowMSTrustKey = FALSE;
  172. if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, ACTIVESETUP_KEY,0, KEY_READ | KEY_WRITE, &hKey) == ERROR_SUCCESS)
  173. {
  174. DWORD dwSize = sizeof(DWORD);
  175. DWORD dwValue = 1;
  176. if(RegQueryValueEx(hKey, TRUSTKEYREG, NULL, NULL, (LPBYTE) &dwValue, &dwSize) == ERROR_SUCCESS)
  177. {
  178. fAllowMSTrustKey = (dwValue ? TRUE : FALSE);
  179. }
  180. RegCloseKey(hKey);
  181. }
  182. }
  183. if(!fAllowMSTrustKey && !bForceMSTrust && bSet)
  184. return;
  185. lstrcpy(szTmp, ".Default\\");
  186. lstrcat(szTmp, c_gszMSTrustRegKey); // build the key for HKEY_USERS
  187. if (RegCreateKeyEx(HKEY_USERS, szTmp, 0, NULL, REG_OPTION_NON_VOLATILE,
  188. KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
  189. {
  190. if (bSet)
  191. {
  192. while (pTrustedProvider[i].dwFlag > 0)
  193. {
  194. if (dwSetMSTrustKey & pTrustedProvider[i].dwFlag)
  195. RegSetValueEx( hKey, pTrustedProvider[i].pValue, 0, REG_SZ, (LPBYTE)pTrustedProvider[i].pData, lstrlen(pTrustedProvider[i].pData) + 1 );
  196. i++;
  197. }
  198. }
  199. else
  200. {
  201. while (pTrustedProvider[i].dwFlag > 0)
  202. {
  203. if (dwSetMSTrustKey & pTrustedProvider[i].dwFlag)
  204. RegDeleteValue(hKey, pTrustedProvider[i].pValue);
  205. i++;
  206. }
  207. }
  208. RegCloseKey(hKey);
  209. }
  210. i = 0;
  211. if (RegCreateKeyEx(HKEY_CURRENT_USER, c_gszMSTrustRegKey, 0, NULL, REG_OPTION_NON_VOLATILE,
  212. KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
  213. {
  214. if (bSet)
  215. {
  216. while (pTrustedProvider[i].dwFlag > 0)
  217. {
  218. if (dwSetMSTrustKey & pTrustedProvider[i].dwFlag)
  219. {
  220. RegSetValueEx( hKey, pTrustedProvider[i].pValue, 0, REG_SZ, (LPBYTE)pTrustedProvider[i].pData, lstrlen(pTrustedProvider[i].pData) + 1 );
  221. }
  222. i++;
  223. }
  224. }
  225. else
  226. {
  227. while (pTrustedProvider[i].dwFlag > 0)
  228. {
  229. if (dwSetMSTrustKey & pTrustedProvider[i].dwFlag)
  230. RegDeleteValue(hKey, pTrustedProvider[i].pValue);
  231. i++;
  232. }
  233. }
  234. RegCloseKey(hKey);
  235. }
  236. }
  237. DWORD MsTrustKeyCheck()
  238. {
  239. DWORD dwTmp;
  240. DWORD dwValue;
  241. HKEY hKey;
  242. DWORD dwMSTrustKeyToSet = 0;
  243. int i = 0;
  244. // Check MS Vendor trust key and set
  245. if (RegOpenKeyEx(HKEY_CURRENT_USER, c_gszMSTrustRegKey, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
  246. {
  247. while (pTrustedProvider[i].dwFlag > 0)
  248. {
  249. if (RegQueryValueEx( hKey, pTrustedProvider[i].pValue, 0, NULL, NULL, &dwTmp ) != ERROR_SUCCESS)
  250. dwMSTrustKeyToSet |= pTrustedProvider[i].dwFlag;
  251. i++;
  252. }
  253. RegCloseKey(hKey);
  254. }
  255. else
  256. dwMSTrustKeyToSet = MSTRUST_ALL;
  257. return dwMSTrustKeyToSet;
  258. }
  259. BOOL KeepTransparent(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT *lres)
  260. {
  261. *lres = 0;
  262. HWND hwndParent;
  263. hwndParent = GetParent(hwnd);
  264. if (hwndParent)
  265. {
  266. POINT pt = {0,0};
  267. MapWindowPoints(hwnd, hwndParent, &pt, 1);
  268. OffsetWindowOrgEx((HDC)wParam, pt.x, pt.y, &pt);
  269. *lres = SendMessage(hwndParent, msg, wParam, lParam);
  270. SetWindowOrgEx((HDC)wParam, pt.x, pt.y, NULL);
  271. if (*lres)
  272. return TRUE; // we handled it!
  273. }
  274. return FALSE;
  275. }
  276. #ifdef TESTCERT
  277. void UpdateTrustState()
  278. {
  279. HKEY hKey;
  280. DWORD dwState;
  281. DWORD dwType;
  282. DWORD dwSize = sizeof(dwState);
  283. if (RegCreateKeyEx(HKEY_CURRENT_USER, c_gszTrustStateRegKey, 0, NULL, REG_OPTION_NON_VOLATILE,
  284. KEY_READ | KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
  285. {
  286. if (RegQueryValueEx(hKey, c_gszState, 0, &dwType, (LPBYTE)&dwState, &dwSize) == ERROR_SUCCESS)
  287. {
  288. dwState |= TESTCERTVALUE;
  289. }
  290. else
  291. dwState = TESTCERTVALUE;
  292. RegSetValueEx( hKey, c_gszState, 0, REG_DWORD, (LPBYTE)&dwState, sizeof(dwState));
  293. RegCloseKey(hKey);
  294. }
  295. }
  296. void ResetTestrootCertInTrustState()
  297. {
  298. HKEY hKey;
  299. DWORD dwState;
  300. DWORD dwType;
  301. DWORD dwSize = sizeof(dwState);
  302. if (RegOpenKeyEx(HKEY_CURRENT_USER, c_gszTrustStateRegKey, 0,
  303. KEY_READ | KEY_WRITE, &hKey) == ERROR_SUCCESS)
  304. {
  305. if (RegQueryValueEx(hKey, c_gszState, 0, &dwType, (LPBYTE)&dwState, &dwSize) == ERROR_SUCCESS)
  306. {
  307. // Clear the bits for trusting test root certs
  308. dwState &= (DWORD)~TESTCERTVALUE;
  309. RegSetValueEx( hKey, c_gszState, 0, REG_DWORD, (LPBYTE)&dwState, sizeof(dwState));
  310. }
  311. RegCloseKey(hKey);
  312. }
  313. }
  314. #endif
  315. void WriteActiveSetupValue(BOOL bSet)
  316. // If bSet is TRUE, add a reg value so that if IE4 base is installed, it would think that it is
  317. // being run from Active Setup. This would prevent softboot from being kicked off by IE4 base.
  318. // If bSet is FALSE, delete the reg value.
  319. {
  320. static const char c_szIE4Options[] = "Software\\Microsoft\\IE Setup\\Options";
  321. static const char c_szActiveSetup[] = "ActiveSetup";
  322. HKEY hk;
  323. LONG lErr;
  324. lErr = bSet ?
  325. RegCreateKeyEx(HKEY_LOCAL_MACHINE, c_szIE4Options, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hk, NULL) :
  326. RegOpenKeyEx(HKEY_LOCAL_MACHINE, c_szIE4Options, 0, KEY_WRITE, &hk);
  327. if (lErr == ERROR_SUCCESS)
  328. {
  329. if (bSet)
  330. {
  331. DWORD dwData = 1;
  332. RegSetValueEx(hk, c_szActiveSetup, 0, REG_DWORD, (CONST BYTE *) &dwData, sizeof(dwData));
  333. }
  334. else
  335. RegDeleteValue(hk, c_szActiveSetup);
  336. RegCloseKey(hk);
  337. }
  338. }
  339. DWORD WaitForEvent(HANDLE hEvent, HWND hwnd)
  340. {
  341. BOOL fQuit = FALSE;
  342. BOOL fDone = FALSE;
  343. DWORD dwRet;
  344. while(!fQuit && !fDone)
  345. {
  346. dwRet = MsgWaitForMultipleObjects(1, &hEvent, FALSE,
  347. INFINITE, QS_ALLINPUT);
  348. // Give abort the highest priority
  349. if(dwRet == WAIT_OBJECT_0)
  350. {
  351. fDone = TRUE;
  352. }
  353. else
  354. {
  355. MSG msg;
  356. // read all of the messages in this next loop
  357. // removing each message as we read it
  358. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  359. {
  360. if(!hwnd || !IsDialogMessage(hwnd, &msg))
  361. {
  362. // if it's a quit message we're out of here
  363. if (msg.message == WM_QUIT)
  364. fQuit = TRUE;
  365. else
  366. {
  367. // otherwise dispatch it
  368. TranslateMessage(&msg);
  369. DispatchMessage(&msg);
  370. }
  371. } // end of PeekMessage while loop
  372. }
  373. }
  374. }
  375. return (fQuit ? EVENTWAIT_QUIT : EVENTWAIT_DONE);
  376. }
  377. #define SHFREE_ORDINAL 195 // Required for BrowseForDir
  378. const char achSHBrowseForFolder[] = "SHBrowseForFolder";
  379. const char achSHGetPathFromIDList[] = "SHGetPathFromIDList";
  380. const char achShell32Lib[] = "Shell32.dll";
  381. //***************************************************************************
  382. //***************************************************************************
  383. // Required for BrowseForDir()
  384. int CALLBACK BrowseCallback(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
  385. {
  386. switch(uMsg)
  387. {
  388. case BFFM_INITIALIZED:
  389. // lpData is the path string
  390. SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
  391. break;
  392. }
  393. return 0;
  394. }
  395. typedef WINSHELLAPI LPITEMIDLIST (WINAPI *SHBROWSEFORFOLDER)(LPBROWSEINFO);
  396. typedef WINSHELLAPI void (WINAPI *SHFREE)(LPVOID);
  397. typedef WINSHELLAPI BOOL (WINAPI *SHGETPATHFROMIDLIST)( LPCITEMIDLIST, LPTSTR );
  398. //***************************************************************************
  399. //* *
  400. //* NAME: BrowseForDir *
  401. //* *
  402. //* SYNOPSIS: Let user browse for a directory on their system or network. *
  403. //* *
  404. //* REQUIRES: hwndParent: *
  405. //* *
  406. //* RETURNS: BOOL: *
  407. //* *
  408. //* NOTES: It would be really cool to set the status line of the *
  409. //* browse window to display "Yes, there's enough space", or *
  410. //* "no there is not". *
  411. //* *
  412. //***************************************************************************
  413. BOOL BrowseForDir( HWND hwndParent, LPSTR pszFolder, LPSTR pszTitle)
  414. {
  415. BROWSEINFO bi;
  416. LPITEMIDLIST pidl;
  417. HINSTANCE hShell32Lib;
  418. SHFREE pfSHFree;
  419. SHGETPATHFROMIDLIST pfSHGetPathFromIDList;
  420. SHBROWSEFORFOLDER pfSHBrowseForFolder;
  421. BOOL fChange = FALSE;
  422. // Load the Shell 32 Library to get the SHBrowseForFolder() features
  423. if ( ( hShell32Lib = LoadLibrary( achShell32Lib ) ) != NULL )
  424. {
  425. if ( ( !( pfSHBrowseForFolder = (SHBROWSEFORFOLDER)
  426. GetProcAddress( hShell32Lib, achSHBrowseForFolder ) ) )
  427. || ( ! ( pfSHFree = (SHFREE) GetProcAddress( hShell32Lib,
  428. MAKEINTRESOURCE(SHFREE_ORDINAL) ) ) )
  429. || ( ! ( pfSHGetPathFromIDList = (SHGETPATHFROMIDLIST)
  430. GetProcAddress( hShell32Lib, achSHGetPathFromIDList ) ) ) )
  431. {
  432. FreeLibrary( hShell32Lib );
  433. return FALSE;
  434. }
  435. }
  436. else
  437. {
  438. return FALSE;
  439. }
  440. bi.hwndOwner = hwndParent;
  441. bi.pidlRoot = NULL;
  442. bi.pszDisplayName = NULL;
  443. bi.lpszTitle = pszTitle;
  444. bi.ulFlags = BIF_RETURNONLYFSDIRS;
  445. bi.lpfn = BrowseCallback;
  446. bi.lParam = (LPARAM)pszFolder;
  447. pidl = pfSHBrowseForFolder( &bi );
  448. if ( pidl )
  449. {
  450. pfSHGetPathFromIDList( pidl, pszFolder );
  451. pfSHFree( pidl );
  452. fChange = TRUE;
  453. }
  454. FreeLibrary( hShell32Lib );
  455. return fChange;
  456. }
  457. BOOL IsSiteInRegion(IDownloadSite *pISite, LPSTR pszRegion)
  458. {
  459. BOOL bInRegion = FALSE;
  460. DOWNLOADSITE *psite;
  461. if(SUCCEEDED(pISite->GetData(&psite)))
  462. {
  463. if(lstrcmpi(psite->pszRegion, pszRegion) == 0)
  464. bInRegion = TRUE;
  465. }
  466. return bInRegion;
  467. }
  468. void SetControlFont()
  469. {
  470. LOGFONT lFont;
  471. if (GetSystemMetrics(SM_DBCSENABLED) &&
  472. (GetObject(GetStockObject(DEFAULT_GUI_FONT), sizeof (lFont), &lFont) > 0))
  473. {
  474. g_hFont = CreateFontIndirect((LPLOGFONT)&lFont);
  475. }
  476. }
  477. void SetFontForControl(HWND hwnd, UINT uiID)
  478. {
  479. if (g_hFont)
  480. {
  481. SendDlgItemMessage(hwnd, uiID, WM_SETFONT, (WPARAM)g_hFont ,0L);
  482. }
  483. }