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.

505 lines
17 KiB

  1. //--------------------------------------------------------------------------------
  2. //
  3. // File: sigtab.cpp
  4. //
  5. // Copyright (c) Microsoft Corp. All Rights Reserved
  6. //
  7. //--------------------------------------------------------------------------------
  8. #include "sigtab.h"
  9. HINSTANCE g_hInst = NULL;
  10. //---------------------------------------------------------------------------
  11. // DllMain()
  12. //---------------------------------------------------------------------------
  13. int APIENTRY DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID )
  14. {
  15. if ( dwReason == DLL_PROCESS_ATTACH ) { // Initializing
  16. g_hInst = hInstance;
  17. DisableThreadLibraryCalls(hInstance);
  18. }
  19. return 1;
  20. }
  21. void GetCurrentDriverSigningPolicy( LPDWORD lpdwDefault, LPDWORD lpdwPolicy, LPDWORD lpdwPreference )
  22. {
  23. SYSTEMTIME RealSystemTime;
  24. DWORD dwSize, dwType;
  25. DWORD dwDefault, dwPolicy, dwPreference;
  26. HKEY hKey;
  27. CONST TCHAR pszDrvSignPath[] = REGSTR_PATH_DRIVERSIGN;
  28. CONST TCHAR pszDrvSignPolicyPath[] = REGSTR_PATH_DRIVERSIGN_POLICY;
  29. CONST TCHAR pszDrvSignPolicyValue[] = REGSTR_VAL_POLICY;
  30. CONST TCHAR pszDrvSignBehaviorOnFailedVerifyDS[] = REGSTR_VAL_BEHAVIOR_ON_FAILED_VERIFY;
  31. dwPolicy = dwPreference = (DWORD) -1;
  32. RealSystemTime.wDayOfWeek = LOWORD(&hKey) | 4;
  33. pSetupGetRealSystemTime(&RealSystemTime);
  34. dwDefault = (((RealSystemTime.wMilliseconds+2)&15)^8)/4;
  35. //
  36. // Retrieve the user policy.
  37. //
  38. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER,
  39. pszDrvSignPolicyPath,
  40. 0,
  41. KEY_READ,
  42. &hKey)) {
  43. dwSize = sizeof(dwPolicy);
  44. if (ERROR_SUCCESS == RegQueryValueEx(hKey,
  45. pszDrvSignBehaviorOnFailedVerifyDS,
  46. NULL,
  47. &dwType,
  48. (PBYTE)&dwPolicy,
  49. &dwSize)) {
  50. //
  51. // Finally, make sure a valid policy value was specified.
  52. //
  53. if ((dwType != REG_DWORD) ||
  54. (dwSize != sizeof(DWORD)) ||
  55. !((dwPolicy == DRIVERSIGN_NONE) || (dwPolicy == DRIVERSIGN_WARNING) || (dwPolicy == DRIVERSIGN_BLOCKING))) {
  56. //
  57. // Bogus entry for user policy--ignore it.
  58. //
  59. dwPolicy = DRIVERSIGN_NONE;
  60. }
  61. }
  62. RegCloseKey(hKey);
  63. }
  64. //
  65. // Finally, retrieve the user preference.
  66. //
  67. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER,
  68. pszDrvSignPath,
  69. 0,
  70. KEY_READ,
  71. &hKey)) {
  72. dwSize = sizeof(dwPreference);
  73. if (ERROR_SUCCESS == RegQueryValueEx(hKey,
  74. pszDrvSignPolicyValue,
  75. NULL,
  76. &dwType,
  77. (PBYTE)&dwPreference,
  78. &dwSize)) {
  79. if ((dwType != REG_DWORD) ||
  80. (dwSize != sizeof(DWORD)) ||
  81. !((dwPreference == DRIVERSIGN_NONE) || (dwPreference == DRIVERSIGN_WARNING) || (dwPreference == DRIVERSIGN_BLOCKING))) {
  82. //
  83. // Bogus entry for user preference--ignore it.
  84. //
  85. dwPreference = DRIVERSIGN_NONE;
  86. }
  87. }
  88. RegCloseKey(hKey);
  89. }
  90. //
  91. // Store the values into the user buffer.
  92. //
  93. *lpdwDefault = dwDefault;
  94. *lpdwPolicy = dwPolicy;
  95. *lpdwPreference = dwPreference;
  96. }
  97. DWORD SigTab_UpdateDialog(HWND hwnd)
  98. {
  99. DWORD dwPreference = DRIVERSIGN_NONE;
  100. DWORD dwDefault = DRIVERSIGN_NONE;
  101. DWORD dwPolicy = DRIVERSIGN_NONE;
  102. DWORD dwCurSel;
  103. //
  104. // Get the current policy settings from the registry.
  105. //
  106. GetCurrentDriverSigningPolicy(&dwDefault, &dwPolicy, &dwPreference);
  107. //
  108. // If there is no preference, set it to the policy or the default.
  109. //
  110. if (dwPreference == (DWORD) -1) {
  111. if (dwPolicy != (DWORD) -1)
  112. dwPreference = dwPolicy;
  113. else dwPreference = dwDefault;
  114. }
  115. //
  116. // Figure out which item is really selected and re-select it. This will get rid of any checked && disabled items.
  117. //
  118. dwCurSel = dwPreference;
  119. if (IsDlgButtonChecked(hwnd, IDC_IGNORE) && IsWindowEnabled(GetDlgItem(hwnd, IDC_IGNORE)))
  120. dwCurSel = IDC_IGNORE;
  121. if (IsDlgButtonChecked(hwnd, IDC_WARN) && IsWindowEnabled(GetDlgItem(hwnd, IDC_WARN)))
  122. dwCurSel = IDC_WARN;
  123. if (IsDlgButtonChecked(hwnd, IDC_BLOCK) && IsWindowEnabled(GetDlgItem(hwnd, IDC_BLOCK)))
  124. dwCurSel = IDC_BLOCK;
  125. EnableWindow(GetDlgItem(hwnd, IDC_IGNORE), TRUE);
  126. EnableWindow(GetDlgItem(hwnd, IDC_WARN), TRUE);
  127. EnableWindow(GetDlgItem(hwnd, IDC_BLOCK), TRUE);
  128. CheckRadioButton(hwnd, IDC_IGNORE, IDC_BLOCK, dwCurSel);
  129. //
  130. // If there is a policy for this user, it overrides any preferences so grey everything but the policy setting.
  131. //
  132. if (dwPolicy != (DWORD) -1) {
  133. //
  134. // If the system default is stronger, it will be used instead.
  135. //
  136. if (dwDefault > dwPolicy)
  137. dwPolicy = dwDefault;
  138. EnableWindow(GetDlgItem(hwnd, IDC_IGNORE), FALSE);
  139. EnableWindow(GetDlgItem(hwnd, IDC_WARN), FALSE);
  140. EnableWindow(GetDlgItem(hwnd, IDC_BLOCK), FALSE);
  141. switch (dwPolicy) {
  142. case DRIVERSIGN_WARNING: EnableWindow(GetDlgItem(hwnd, IDC_WARN), TRUE);
  143. CheckRadioButton(hwnd, IDC_IGNORE, IDC_BLOCK, IDC_WARN);
  144. break;
  145. case DRIVERSIGN_BLOCKING: EnableWindow(GetDlgItem(hwnd, IDC_BLOCK), TRUE);
  146. CheckRadioButton(hwnd, IDC_IGNORE, IDC_BLOCK, IDC_BLOCK);
  147. break;
  148. default: EnableWindow(GetDlgItem(hwnd, IDC_IGNORE), TRUE);
  149. CheckRadioButton(hwnd, IDC_IGNORE, IDC_BLOCK, IDC_IGNORE);
  150. break;
  151. }
  152. dwPreference = dwPolicy;
  153. } else {
  154. //
  155. // Grey out the items being over-ridden by the systen policy. Bump the selection down to the first available slot.
  156. //
  157. switch (dwDefault) {
  158. case DRIVERSIGN_BLOCKING: if (IsDlgButtonChecked(hwnd, IDC_WARN) || IsDlgButtonChecked(hwnd, IDC_IGNORE))
  159. CheckRadioButton(hwnd, IDC_IGNORE, IDC_BLOCK, IDC_BLOCK);
  160. EnableWindow(GetDlgItem(hwnd, IDC_IGNORE), FALSE);
  161. EnableWindow(GetDlgItem(hwnd, IDC_WARN), FALSE);
  162. break;
  163. case DRIVERSIGN_WARNING: if (IsDlgButtonChecked(hwnd, IDC_IGNORE))
  164. CheckRadioButton(hwnd, IDC_IGNORE, IDC_BLOCK, IDC_WARN);
  165. EnableWindow(GetDlgItem(hwnd, IDC_IGNORE), FALSE);
  166. break;
  167. }
  168. //
  169. // If the system default is stronger, it will be used instead.
  170. //
  171. if (dwDefault > dwPreference)
  172. dwPreference = dwDefault;
  173. }
  174. if (pSetupIsUserAdmin()) {
  175. //
  176. // If the administrator can set the default, make everything available for selection.
  177. //
  178. if (IsDlgButtonChecked(hwnd, IDC_GLOBAL)) {
  179. EnableWindow(GetDlgItem(hwnd, IDC_IGNORE), TRUE);
  180. EnableWindow(GetDlgItem(hwnd, IDC_WARN), TRUE);
  181. EnableWindow(GetDlgItem(hwnd, IDC_BLOCK), TRUE);
  182. }
  183. }
  184. return dwPreference;
  185. }
  186. //
  187. // Initialization of search dialog.
  188. //
  189. BOOL SigTab_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
  190. {
  191. DWORD dwPreference = DRIVERSIGN_NONE;
  192. DWORD dwDefault = DRIVERSIGN_NONE;
  193. DWORD dwPolicy = DRIVERSIGN_NONE;
  194. BOOL bAdmin;
  195. UNREFERENCED_PARAMETER(hwndFocus);
  196. UNREFERENCED_PARAMETER(lParam);
  197. ShowWindow(hwnd, SW_SHOW);
  198. CheckRadioButton(hwnd, IDC_IGNORE, IDC_BLOCK, IDC_IGNORE);
  199. CheckDlgButton(hwnd, IDC_GLOBAL, BST_UNCHECKED);
  200. bAdmin = pSetupIsUserAdmin();
  201. ShowWindow(GetDlgItem(hwnd, IDC_GLOBAL), bAdmin ? SW_SHOW : SW_HIDE);
  202. ShowWindow(GetDlgItem(hwnd, IDG_ADMIN), bAdmin ? SW_SHOW : SW_HIDE);
  203. GetCurrentDriverSigningPolicy(&dwDefault, &dwPolicy, &dwPreference);
  204. //
  205. // Call SigTab_UpdateDialog to initialize the dialog
  206. //
  207. dwPreference = SigTab_UpdateDialog(hwnd);
  208. //
  209. // Check the radio button for their calculated "preference".
  210. //
  211. switch (dwPreference) {
  212. case DRIVERSIGN_WARNING: CheckRadioButton(hwnd, IDC_IGNORE, IDC_BLOCK, IDC_WARN);
  213. break;
  214. case DRIVERSIGN_BLOCKING: CheckRadioButton(hwnd, IDC_IGNORE, IDC_BLOCK, IDC_BLOCK);
  215. break;
  216. }
  217. //
  218. // If the user is an administrator, check the "Global" box if the preference matches the default setting.
  219. //
  220. if (bAdmin) {
  221. switch (dwDefault) {
  222. case DRIVERSIGN_WARNING: if (IsDlgButtonChecked(hwnd, IDC_WARN))
  223. CheckDlgButton(hwnd, IDC_GLOBAL, BST_CHECKED);
  224. break;
  225. case DRIVERSIGN_BLOCKING: if (IsDlgButtonChecked(hwnd, IDC_BLOCK))
  226. CheckDlgButton(hwnd, IDC_GLOBAL, BST_CHECKED);
  227. break;
  228. case DRIVERSIGN_NONE: if (IsDlgButtonChecked(hwnd, IDC_IGNORE))
  229. CheckDlgButton(hwnd, IDC_GLOBAL, BST_CHECKED);
  230. break;
  231. }
  232. //
  233. // If the administrator can set the default, make everything available for selection.
  234. //
  235. if (IsDlgButtonChecked(hwnd, IDC_GLOBAL)) {
  236. EnableWindow(GetDlgItem(hwnd, IDC_IGNORE), TRUE);
  237. EnableWindow(GetDlgItem(hwnd, IDC_WARN), TRUE);
  238. EnableWindow(GetDlgItem(hwnd, IDC_BLOCK), TRUE);
  239. }
  240. }
  241. return TRUE;
  242. }
  243. void SigTab_Help(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL bContext)
  244. {
  245. static DWORD SigTab_HelpIDs[] =
  246. {
  247. IDC_IGNORE, IDH_CODESIGN_IGNORE,
  248. IDC_WARN, IDH_CODESIGN_WARN,
  249. IDC_BLOCK, IDH_CODESIGN_BLOCK,
  250. IDC_GLOBAL, IDH_CODESIGN_APPLY,
  251. IDG_ADMIN, (DWORD)-1,
  252. 0,0
  253. };
  254. HWND hItem = NULL;
  255. LPHELPINFO lphi = NULL;
  256. POINT point;
  257. switch (uMsg) {
  258. case WM_HELP:
  259. lphi = (LPHELPINFO) lParam;
  260. if (lphi && (lphi->iContextType == HELPINFO_WINDOW)) // must be for a control
  261. hItem = (HWND) lphi->hItemHandle;
  262. break;
  263. case WM_CONTEXTMENU:
  264. hItem = (HWND) wParam;
  265. point.x = GET_X_LPARAM(lParam);
  266. point.y = GET_Y_LPARAM(lParam);
  267. if (ScreenToClient(hwnd, &point)) {
  268. hItem = ChildWindowFromPoint(hwnd, point);
  269. }
  270. break;
  271. }
  272. if (hItem && (GetWindowLong(hItem, GWL_ID) != IDC_STATIC)) {
  273. WinHelp(hItem,
  274. (LPCTSTR) SIGTAB_HELPFILE,
  275. (bContext ? HELP_CONTEXTMENU : HELP_WM_HELP),
  276. (ULONG_PTR) SigTab_HelpIDs);
  277. }
  278. }
  279. //
  280. //
  281. //
  282. void SigTab_ApplySettings(HWND hwnd)
  283. {
  284. HKEY hKey;
  285. LONG lRes;
  286. DWORD dwData, dwSize, dwType, dwDisposition;
  287. lRes = RegCreateKeyEx( HKEY_CURRENT_USER,
  288. SIGTAB_REG_KEY,
  289. NULL,
  290. NULL,
  291. REG_OPTION_NON_VOLATILE,
  292. KEY_WRITE,
  293. NULL,
  294. &hKey,
  295. &dwDisposition);
  296. if (lRes == ERROR_SUCCESS) {
  297. dwType = REG_DWORD;
  298. dwSize = sizeof(dwData);
  299. dwData = DRIVERSIGN_NONE;
  300. if (IsDlgButtonChecked(hwnd, IDC_WARN))
  301. dwData = DRIVERSIGN_WARNING;
  302. else
  303. if (IsDlgButtonChecked(hwnd, IDC_BLOCK))
  304. dwData = DRIVERSIGN_BLOCKING;
  305. lRes = RegSetValueEx( hKey,
  306. SIGTAB_REG_VALUE,
  307. 0,
  308. dwType,
  309. (CONST BYTE *) &dwData,
  310. dwSize);
  311. RegCloseKey(hKey);
  312. if (lRes == ERROR_SUCCESS && IsDlgButtonChecked(hwnd, IDC_GLOBAL) && pSetupIsUserAdmin()) {
  313. SYSTEMTIME RealSystemTime;
  314. if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  315. TEXT("System\\WPA\\PnP"),
  316. 0,
  317. KEY_READ,
  318. &hKey)) {
  319. dwSize = sizeof(dwData);
  320. if((ERROR_SUCCESS != RegQueryValueEx(hKey,
  321. TEXT("seed"),
  322. NULL,
  323. &dwType,
  324. (PBYTE)&dwData,
  325. &dwSize))
  326. || (dwType != REG_DWORD) || (dwSize != sizeof(dwData))) {
  327. dwData = 0;
  328. }
  329. RegCloseKey(hKey);
  330. }
  331. RealSystemTime.wDayOfWeek = LOWORD(&hKey) | 4;
  332. RealSystemTime.wMinute = LOWORD(dwData);
  333. RealSystemTime.wYear = HIWORD(dwData);
  334. dwData = DRIVERSIGN_NONE;
  335. if(IsDlgButtonChecked(hwnd, IDC_WARN)) {
  336. dwData = DRIVERSIGN_WARNING;
  337. } else if(IsDlgButtonChecked(hwnd, IDC_BLOCK)) {
  338. dwData = DRIVERSIGN_BLOCKING;
  339. }
  340. RealSystemTime.wMilliseconds = (LOWORD(&lRes)&~3072)|(WORD)((dwData&3)<<10);
  341. pSetupGetRealSystemTime(&RealSystemTime);
  342. }
  343. }
  344. }
  345. //
  346. // Handle any WM_COMMAND messages sent to the search dialog
  347. //
  348. void SigTab_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
  349. {
  350. UNREFERENCED_PARAMETER(hwndCtl);
  351. UNREFERENCED_PARAMETER(codeNotify);
  352. switch (id) {
  353. case IDCANCEL:
  354. EndDialog(hwnd, 0);
  355. break;
  356. case IDOK:
  357. SigTab_ApplySettings(hwnd);
  358. EndDialog(hwnd, 1);
  359. break;
  360. case IDC_GLOBAL:
  361. SigTab_UpdateDialog(hwnd);
  362. break;
  363. }
  364. return;
  365. }
  366. LRESULT SigTab_NotifyHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  367. {
  368. OSVERSIONINFOEX osVersionInfoEx;
  369. NMHDR *lpnmhdr = (NMHDR *) lParam;
  370. UNREFERENCED_PARAMETER(uMsg);
  371. UNREFERENCED_PARAMETER(wParam);
  372. switch (lpnmhdr->code) {
  373. case NM_RETURN:
  374. case NM_CLICK:
  375. if (lpnmhdr->idFrom == IDC_LINK) {
  376. //
  377. // We need to know if this is a server machine or a workstation
  378. // machine since there are different help topic structures for
  379. // the different products.
  380. //
  381. ZeroMemory(&osVersionInfoEx, sizeof(osVersionInfoEx));
  382. osVersionInfoEx.dwOSVersionInfoSize = sizeof(osVersionInfoEx);
  383. if (!GetVersionEx((LPOSVERSIONINFO)&osVersionInfoEx)) {
  384. //
  385. // If GetVersionEx fails then assume this is a workstation
  386. // machine.
  387. //
  388. osVersionInfoEx.wProductType = VER_NT_WORKSTATION;
  389. }
  390. ShellExecute(hwnd,
  391. TEXT("open"),
  392. TEXT("HELPCTR.EXE"),
  393. (osVersionInfoEx.wProductType == VER_NT_WORKSTATION)
  394. ? TEXT("HELPCTR.EXE -url hcp://services/subsite?node=TopLevelBucket_4/Hardware&topic=MS-ITS%3A%25HELP_LOCATION%25%5Csysdm.chm%3A%3A/logo_testing.htm")
  395. : TEXT("HELPCTR.EXE -url hcp://services/subsite?node=Hardware&topic=MS-ITS%3A%25HELP_LOCATION%25%5Csysdm.chm%3A%3A/logo_testing.htm"),
  396. NULL,
  397. SW_SHOWNORMAL
  398. );
  399. }
  400. break;
  401. default:
  402. break;
  403. }
  404. return 0;
  405. }
  406. INT_PTR CALLBACK SigTab_DlgProc(HWND hwnd, UINT uMsg,
  407. WPARAM wParam, LPARAM lParam)
  408. {
  409. BOOL fProcessed = TRUE;
  410. switch (uMsg) {
  411. HANDLE_MSG(hwnd, WM_INITDIALOG, SigTab_OnInitDialog);
  412. HANDLE_MSG(hwnd, WM_COMMAND, SigTab_OnCommand);
  413. case WM_HELP:
  414. SigTab_Help(hwnd, uMsg, wParam, lParam, FALSE);
  415. break;
  416. case WM_CONTEXTMENU:
  417. SigTab_Help(hwnd, uMsg, wParam, lParam, TRUE);
  418. break;
  419. case WM_NOTIFY:
  420. return SigTab_NotifyHandler(hwnd, uMsg, wParam, lParam);
  421. default: fProcessed = FALSE;
  422. }
  423. return fProcessed;
  424. }
  425. STDAPI DriverSigningDialog(HWND hwnd, DWORD dwFlagsReserved)
  426. {
  427. UNREFERENCED_PARAMETER(dwFlagsReserved);
  428. return((HRESULT)DialogBox(g_hInst, MAKEINTRESOURCE(IDD_SIGTAB), hwnd, SigTab_DlgProc));
  429. }