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.

789 lines
16 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: winutil.c
  7. //
  8. // Contents: General Utilities to test ginas
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 7-14-94 RichardW Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include "testgina.h"
  18. DWORD DummySCData = sizeof( DWORD ) ;
  19. typedef struct _WindowMapper {
  20. DWORD fMapper;
  21. HWND hWnd;
  22. DLGPROC DlgProc;
  23. struct _WindowMapper * pPrev;
  24. LPARAM InitialParameter;
  25. } WindowMapper, * PWindowMapper;
  26. #define MAPPERFLAG_ACTIVE 1
  27. #define MAPPERFLAG_DIALOG 2
  28. #define MAPPERFLAG_SAS 4
  29. #define MAX_WINDOW_MAPPERS 32
  30. WindowMapper Mappers[MAX_WINDOW_MAPPERS];
  31. DWORD cActiveWindow;
  32. void
  33. InitWindowMappers()
  34. {
  35. ZeroMemory(Mappers, sizeof(WindowMapper) * MAX_WINDOW_MAPPERS);
  36. cActiveWindow = 0;
  37. }
  38. PWindowMapper
  39. LocateTopMappedWindow(void)
  40. {
  41. int i;
  42. for (i = 0; i < MAX_WINDOW_MAPPERS ; i++ )
  43. {
  44. if (Mappers[i].fMapper & MAPPERFLAG_SAS)
  45. {
  46. return(&Mappers[i]);
  47. }
  48. }
  49. return(NULL);
  50. }
  51. PWindowMapper
  52. AllocWindowMapper(void)
  53. {
  54. int i;
  55. PWindowMapper pMap;
  56. for (i = 0 ; i < MAX_WINDOW_MAPPERS ; i++ )
  57. {
  58. if ((Mappers[i].fMapper & MAPPERFLAG_ACTIVE) == 0)
  59. {
  60. cActiveWindow ++;
  61. pMap = LocateTopMappedWindow();
  62. if (pMap)
  63. {
  64. FLAG_OFF(pMap->fMapper, MAPPERFLAG_SAS);
  65. }
  66. FLAG_ON(Mappers[i].fMapper, MAPPERFLAG_ACTIVE | MAPPERFLAG_SAS);
  67. Mappers[i].pPrev = pMap;
  68. return(&Mappers[i]);
  69. }
  70. }
  71. return(NULL);
  72. }
  73. PWindowMapper
  74. LocateWindowMapper(HWND hWnd)
  75. {
  76. int i;
  77. for (i = 0; i < MAX_WINDOW_MAPPERS ; i++ )
  78. {
  79. if (Mappers[i].hWnd == hWnd)
  80. {
  81. return(&Mappers[i]);
  82. }
  83. }
  84. return(NULL);
  85. }
  86. void
  87. FreeWindowMapper(PWindowMapper pMap)
  88. {
  89. pMap->hWnd = NULL;
  90. pMap->DlgProc = NULL;
  91. if (pMap->fMapper & MAPPERFLAG_SAS)
  92. {
  93. if (pMap->pPrev)
  94. {
  95. FLAG_ON(pMap->pPrev->fMapper, MAPPERFLAG_SAS);
  96. }
  97. }
  98. pMap->fMapper = 0;
  99. pMap->pPrev = NULL;
  100. cActiveWindow--;
  101. }
  102. #define CALAIS_PATH TEXT("Software\\Microsoft\\Cryptography\\Calais")
  103. BOOL
  104. IsSmartCardReaderPresent(
  105. VOID
  106. )
  107. {
  108. HKEY hKey ;
  109. int err ;
  110. err = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  111. CALAIS_PATH,
  112. 0,
  113. KEY_READ,
  114. &hKey );
  115. if ( err == 0 )
  116. {
  117. RegCloseKey( hKey );
  118. return TRUE ;
  119. }
  120. else
  121. {
  122. return FALSE ;
  123. }
  124. }
  125. //+---------------------------------------------------------------------------
  126. //
  127. // Function: RootWndProc
  128. //
  129. // Synopsis: This is the base window proc for all testgina windows.
  130. //
  131. // Arguments: [hWnd] --
  132. // [Message] --
  133. // [wParam] --
  134. // [lParam] --
  135. //
  136. // History: 7-18-94 RichardW Created
  137. //
  138. // Notes:
  139. //
  140. //----------------------------------------------------------------------------
  141. BOOL
  142. CALLBACK
  143. RootDlgProc(
  144. HWND hWnd,
  145. UINT Message,
  146. WPARAM wParam,
  147. LPARAM lParam)
  148. {
  149. PWindowMapper pMap;
  150. int res;
  151. BOOL bRet;
  152. //
  153. // If this is a WM_INITDIALOG message, then the parameter is the mapping,
  154. // which needs to have a hwnd associated with it. Otherwise, do the normal
  155. // preprocessing.
  156. //
  157. if (Message == WM_INITDIALOG)
  158. {
  159. pMap = (PWindowMapper) lParam;
  160. pMap->hWnd = hWnd;
  161. lParam = pMap->InitialParameter;
  162. }
  163. else
  164. {
  165. pMap = LocateWindowMapper(hWnd);
  166. if (!pMap)
  167. {
  168. return(FALSE);
  169. }
  170. }
  171. bRet = pMap->DlgProc(hWnd, Message, wParam, lParam);
  172. if (!bRet)
  173. {
  174. if (Message == WM_INITDIALOG)
  175. {
  176. return(bRet);
  177. }
  178. if (Message == WLX_WM_SAS)
  179. {
  180. switch (wParam)
  181. {
  182. case WLX_SAS_TYPE_CTRL_ALT_DEL:
  183. default:
  184. res = WLX_DLG_SAS;
  185. break;
  186. case WLX_SAS_TYPE_TIMEOUT:
  187. res = WLX_DLG_INPUT_TIMEOUT;
  188. break;
  189. case WLX_SAS_TYPE_SCRNSVR_TIMEOUT:
  190. res = WLX_DLG_SCREEN_SAVER_TIMEOUT;
  191. break;
  192. case WLX_SAS_TYPE_USER_LOGOFF:
  193. res = WLX_DLG_USER_LOGOFF;
  194. break;
  195. }
  196. if (res)
  197. {
  198. EndDialog(hWnd, res);
  199. bRet = TRUE;
  200. }
  201. else
  202. {
  203. TestGinaError(GINAERR_INVALID_SAS_CODE, TEXT("RootDlgProc"));
  204. }
  205. }
  206. }
  207. return(bRet);
  208. }
  209. PingSAS(DWORD SasType)
  210. {
  211. PWindowMapper pMap;
  212. if (cActiveWindow)
  213. {
  214. pMap = LocateTopMappedWindow();
  215. if (!pMap)
  216. {
  217. TestGinaError(GINAERR_NO_WINDOW_FOR_SAS, TEXT("PingSAS"));
  218. }
  219. PostMessage(pMap->hWnd, WLX_WM_SAS, (WPARAM) SasType, 0);
  220. }
  221. UpdateGinaState(UPDATE_SAS_RECEIVED);
  222. switch (GinaState)
  223. {
  224. case Winsta_NoOne_SAS:
  225. TestLoggedOutSAS(SasType);
  226. break;
  227. case Winsta_LoggedOn_SAS:
  228. TestLoggedOnSAS(SasType);
  229. break;
  230. case Winsta_Locked_SAS:
  231. TestWkstaLockedSAS(SasType);
  232. break;
  233. default:
  234. TestGinaError(0, TEXT("PingSAS"));
  235. }
  236. return(0);
  237. }
  238. VOID WINAPI
  239. WlxUseCtrlAltDel(HANDLE hWlx)
  240. {
  241. if (!VerifyHandle(hWlx))
  242. {
  243. TestGinaError(GINAERR_INVALID_HANDLE, TEXT("WlxUserCtrlAltDel"));
  244. }
  245. fTestGina |= GINA_USE_CAD;
  246. }
  247. VOID
  248. WINAPI
  249. WlxSetContextPointer(
  250. HANDLE hWlx,
  251. PVOID pvContext)
  252. {
  253. if (!VerifyHandle(hWlx))
  254. {
  255. TestGinaError(GINAERR_INVALID_HANDLE, TEXT("WlxSetContextPointer"));
  256. }
  257. StashContext(pvContext);
  258. }
  259. VOID WINAPI
  260. WlxSasNotify(HANDLE hWlx,
  261. DWORD dwSasType)
  262. {
  263. if (!VerifyHandle(hWlx))
  264. {
  265. TestGinaError(GINAERR_INVALID_HANDLE, TEXT("WlxSasNotify"));
  266. }
  267. if (fTestGina & GINA_USE_CAD)
  268. {
  269. if (dwSasType == WLX_SAS_TYPE_CTRL_ALT_DEL)
  270. {
  271. TestGinaError(GINAERR_IMPROPER_CAD, TEXT("WlxSasNotify"));
  272. }
  273. }
  274. PingSAS(dwSasType);
  275. }
  276. BOOL WINAPI
  277. WlxSetTimeout(
  278. HANDLE hWlx,
  279. DWORD dwTimeout)
  280. {
  281. if (!VerifyHandle(hWlx))
  282. {
  283. TestGinaError(GINAERR_INVALID_HANDLE, TEXT("WlxSetTimeout"));
  284. }
  285. if (dwTimeout < 300)
  286. {
  287. return(TRUE);
  288. }
  289. return(FALSE);
  290. }
  291. int WINAPI
  292. WlxAssignShellProtection(
  293. HANDLE hWlx,
  294. HANDLE hToken,
  295. HANDLE hProcess,
  296. HANDLE hThread)
  297. {
  298. if (!VerifyHandle(hWlx))
  299. {
  300. TestGinaError(GINAERR_INVALID_HANDLE, TEXT("WlxAssignShellProtection"));
  301. }
  302. return(0);
  303. }
  304. int WINAPI
  305. WlxMessageBox(
  306. HANDLE hWlx,
  307. HWND hWnd,
  308. LPWSTR lpsz1,
  309. LPWSTR lpsz2,
  310. UINT fmb)
  311. {
  312. if (!VerifyHandle(hWlx))
  313. {
  314. TestGinaError(GINAERR_INVALID_HANDLE, TEXT("WlxMessageBox"));
  315. }
  316. return MessageBoxW(hWnd, lpsz1, lpsz2, fmb);
  317. }
  318. int WINAPI
  319. WlxDialogBox(
  320. HANDLE hWlx,
  321. HANDLE hInstance,
  322. LPWSTR lpsz1,
  323. HWND hWnd,
  324. DLGPROC dlgproc)
  325. {
  326. return(WlxDialogBoxParam(hWlx, hInstance, lpsz1, hWnd, dlgproc, 0));
  327. }
  328. int WINAPI
  329. WlxDialogBoxIndirect(
  330. HANDLE hWlx,
  331. HANDLE hInstance,
  332. LPCDLGTEMPLATE lpTemplate,
  333. HWND hWnd,
  334. DLGPROC dlgproc)
  335. {
  336. return(WlxDialogBoxIndirectParam(hWlx, hInstance, lpTemplate, hWnd, dlgproc, 0));
  337. }
  338. int WINAPI
  339. WlxDialogBoxParam(
  340. HANDLE hWlx,
  341. HANDLE hInstance,
  342. LPWSTR lpsz1,
  343. HWND hWnd,
  344. DLGPROC dlgproc,
  345. LPARAM lParam)
  346. {
  347. PWindowMapper pMap;
  348. int res;
  349. char buf[256];
  350. if (!VerifyHandle(hWlx))
  351. {
  352. TestGinaError(GINAERR_INVALID_HANDLE, TEXT("WlxDialogBoxParam"));
  353. }
  354. pMap = AllocWindowMapper();
  355. pMap->InitialParameter = lParam;
  356. pMap->DlgProc = dlgproc;
  357. pMap->fMapper |= MAPPERFLAG_DIALOG;
  358. res = DialogBoxParam(hInstance, lpsz1, hWnd, RootDlgProc, (LPARAM) pMap);
  359. if (res == -1)
  360. {
  361. if ((DWORD) lpsz1 > 0x00010000)
  362. {
  363. sprintf( buf, "DialogBoxParam(%#x, %ws, %#x, %#x, %#x) failed, error %d\n",
  364. hInstance, lpsz1, hWnd, dlgproc,
  365. lParam, GetLastError() );
  366. }
  367. else
  368. {
  369. sprintf( buf, "DialogBoxParam(%#x, %#x, %#x, %#x, %#x) failed, error %d\n",
  370. hInstance, lpsz1, hWnd, dlgproc,
  371. lParam, GetLastError() );
  372. }
  373. MessageBoxA( hMainWindow, buf, "Dialog Error", MB_ICONSTOP | MB_OK );
  374. }
  375. FreeWindowMapper(pMap);
  376. return(res);
  377. }
  378. int WINAPI
  379. WlxDialogBoxIndirectParam(
  380. HANDLE hWlx,
  381. HANDLE hInstance,
  382. LPCDLGTEMPLATE lpTemplate,
  383. HWND hWnd,
  384. DLGPROC dlgproc,
  385. LPARAM lParam)
  386. {
  387. if (!VerifyHandle(hWlx))
  388. {
  389. TestGinaError(GINAERR_INVALID_HANDLE, TEXT("WlxUserCtrlAltDel"));
  390. }
  391. return(DialogBoxIndirectParam(hInstance, lpTemplate, hWnd, dlgproc, lParam));
  392. }
  393. int
  394. WINAPI
  395. WlxSwitchDesktopToUser(
  396. HANDLE hWlx)
  397. {
  398. if ( !VerifyHandle( hWlx ) )
  399. {
  400. TestGinaError( GINAERR_INVALID_HANDLE, TEXT("WlxSwitchDesktopToUser"));
  401. }
  402. return( 0 );
  403. }
  404. int
  405. WINAPI
  406. WlxSwitchDesktopToWinlogon(
  407. HANDLE hWlx)
  408. {
  409. if ( !VerifyHandle( hWlx ) )
  410. {
  411. TestGinaError( GINAERR_INVALID_HANDLE, TEXT("WlxSwitchDesktopToWinlogon"));
  412. }
  413. return( 0 );
  414. }
  415. int
  416. WINAPI
  417. WlxChangePasswordNotify(
  418. HANDLE hWlx,
  419. PWLX_MPR_NOTIFY_INFO pMprInfo,
  420. DWORD dwChangeInfo)
  421. {
  422. if ( !VerifyHandle( hWlx ) )
  423. {
  424. TestGinaError( GINAERR_INVALID_HANDLE, TEXT("WlxChangePasswordNotify"));
  425. }
  426. GlobalMprInfo = *pMprInfo;
  427. wcscpy( GlobalProviderName, TEXT("All") );
  428. return( 0 );
  429. }
  430. int
  431. WINAPI
  432. WlxChangePasswordNotifyEx(
  433. HANDLE hWlx,
  434. PWLX_MPR_NOTIFY_INFO pMprInfo,
  435. DWORD dwChangeInfo,
  436. PWSTR ProviderName,
  437. PVOID Reserved)
  438. {
  439. if ( !VerifyHandle( hWlx ) )
  440. {
  441. TestGinaError( GINAERR_INVALID_HANDLE, TEXT("WlxChangePasswordNotifyEx"));
  442. }
  443. GlobalMprInfo = *pMprInfo;
  444. wcscpy( GlobalProviderName, ProviderName );
  445. return( 0 );
  446. }
  447. BOOL
  448. WINAPI
  449. WlxSetOption(
  450. HANDLE hWlx,
  451. DWORD Option,
  452. ULONG_PTR Value,
  453. ULONG_PTR * OldValue)
  454. {
  455. ULONG_PTR * Item ;
  456. ULONG_PTR Dummy ;
  457. if ( !VerifyHandle( hWlx ) )
  458. {
  459. TestGinaError( GINAERR_INVALID_HANDLE, TEXT("WlxSetOption") );
  460. }
  461. Item = &Dummy ;
  462. switch ( Option )
  463. {
  464. case WLX_OPTION_USE_CTRL_ALT_DEL:
  465. Dummy = (BOOL) ((fTestGina & GINA_USE_CAD) != 0);
  466. fTestGina |= GINA_USE_CAD;
  467. break;
  468. case WLX_OPTION_CONTEXT_POINTER:
  469. Item = (PDWORD) &pWlxContext ;
  470. break;
  471. case WLX_OPTION_USE_SMART_CARD:
  472. Dummy = (BOOL) ((fTestGina & GINA_USE_SC) != 0);
  473. fTestGina |= GINA_USE_SC ;
  474. break;
  475. default:
  476. SetLastError( ERROR_INVALID_PARAMETER );
  477. return FALSE ;
  478. }
  479. if ( Item )
  480. {
  481. if ( OldValue )
  482. {
  483. *OldValue = *Item ;
  484. }
  485. *Item = Value ;
  486. }
  487. return TRUE ;
  488. }
  489. BOOL
  490. WINAPI
  491. WlxGetOption(
  492. HANDLE hWlx,
  493. DWORD Option,
  494. ULONG_PTR * Value)
  495. {
  496. ULONG_PTR * Item ;
  497. ULONG_PTR Dummy ;
  498. if ( !VerifyHandle( hWlx ) )
  499. {
  500. TestGinaError( GINAERR_INVALID_HANDLE, TEXT("WlxSetOption") );
  501. }
  502. Item = &Dummy ;
  503. switch ( Option )
  504. {
  505. case WLX_OPTION_USE_CTRL_ALT_DEL:
  506. Dummy = (BOOL) ((fTestGina & GINA_USE_CAD) != 0);
  507. break;
  508. case WLX_OPTION_CONTEXT_POINTER:
  509. Item = (ULONG_PTR *) &pWlxContext ;
  510. break;
  511. case WLX_OPTION_USE_SMART_CARD:
  512. Dummy = (BOOL) ((fTestGina & GINA_USE_SC) != 0);
  513. break;
  514. case WLX_OPTION_SMART_CARD_PRESENT :
  515. Dummy = IsSmartCardReaderPresent() ;
  516. break;
  517. case WLX_OPTION_SMART_CARD_INFO :
  518. Dummy = (ULONG_PTR)& DummySCData ;
  519. Item = (ULONG_PTR *) &Dummy ;
  520. break;
  521. case WLX_OPTION_DISPATCH_TABLE_SIZE:
  522. Dummy = sizeof( WlxDispatchTable );
  523. Item = (ULONG_PTR *) &Dummy ;
  524. break;
  525. default:
  526. SetLastError( ERROR_INVALID_PARAMETER );
  527. return FALSE ;
  528. }
  529. if ( Item )
  530. {
  531. if ( Value )
  532. {
  533. *Value = *Item ;
  534. return TRUE ;
  535. }
  536. }
  537. return FALSE ;
  538. }
  539. VOID
  540. WINAPI
  541. WlxWin31Migrate(
  542. HANDLE hWlx
  543. )
  544. {
  545. if ( !VerifyHandle( hWlx ) )
  546. {
  547. TestGinaError( GINAERR_INVALID_HANDLE, TEXT("WlxSetOption") );
  548. }
  549. }
  550. BOOL
  551. WINAPI
  552. WlxQueryClientCredentials(
  553. PWLX_CLIENT_CREDENTIALS_INFO_V1_0 pCred
  554. )
  555. {
  556. return FALSE ;
  557. }
  558. BOOL
  559. WINAPI
  560. WlxQueryICCredentials(
  561. PWLX_CLIENT_CREDENTIALS_INFO_V1_0 pCred
  562. )
  563. {
  564. return FALSE ;
  565. }
  566. BOOL
  567. WINAPI
  568. WlxDisconnect(
  569. VOID
  570. )
  571. {
  572. return FALSE ;
  573. }
  574. struct _BitsToMenu {
  575. DWORD Bits;
  576. DWORD Menu;
  577. } MenuBarControl[] = {
  578. { GINA_DLL_KNOWN, IDM_DLL_RUN },
  579. { GINA_NEGOTIATE_OK, IDM_WHACK_NEGOTIATE },
  580. { GINA_INITIALIZE_OK, IDM_WHACK_INITIALIZE },
  581. { GINA_DISPLAY_OK, IDM_WHACK_DISPLAY },
  582. { GINA_LOGGEDOUT_OK, IDM_WHACK_LOGGEDOUT },
  583. { GINA_ACTIVATE_OK, IDM_WHACK_STARTSHELL },
  584. { GINA_LOGGEDON_OK, IDM_WHACK_LOGGEDON },
  585. { GINA_DISPLAYLOCK_OK, IDM_WHACK_DISPLAYLOCKED},
  586. { GINA_WKSTALOCK_OK, IDM_WHACK_LOCKED },
  587. { GINA_LOGOFF_OK, IDM_WHACK_LOGOFF },
  588. { GINA_SHUTDOWN_OK, IDM_WHACK_SHUTDOWN },
  589. { GINA_ISLOCKOK_OK, IDM_WHACK_LOCKOK },
  590. { GINA_ISLOGOFFOK_OK, IDM_WHACK_LOGOFFOK },
  591. { GINA_RESTART_OK, IDM_WHACK_RESTARTSHELL },
  592. { GINA_SCREENSAVE_OK, IDM_WHACK_SCREENSAVE },
  593. };
  594. int
  595. UpdateMenuBar(void)
  596. {
  597. HMENU hMenu;
  598. int i;
  599. hMenu = GetMenu(hMainWindow);
  600. for (i = 0; i < (sizeof(MenuBarControl) / sizeof(struct _BitsToMenu)) ; i++ )
  601. {
  602. if (TEST_FLAG(fTestGina, MenuBarControl[i].Bits))
  603. {
  604. EnableMenuItem(hMenu, MenuBarControl[i].Menu, MF_BYCOMMAND | MF_ENABLED);
  605. }
  606. else
  607. {
  608. EnableMenuItem(hMenu, MenuBarControl[i].Menu, MF_BYCOMMAND | MF_GRAYED);
  609. }
  610. }
  611. return(0);
  612. }
  613. VOID
  614. EnableOptions(BOOL Enable)
  615. {
  616. HMENU hMenu;
  617. HMENU hOptions;
  618. UINT uenable;
  619. hMenu = GetMenu( hMainWindow );
  620. hOptions = GetSubMenu( hMenu, 2 );
  621. if (Enable)
  622. {
  623. uenable = MF_ENABLED | MF_BYCOMMAND;
  624. }
  625. else
  626. {
  627. uenable = MF_GRAYED | MF_BYCOMMAND;
  628. }
  629. EnableMenuItem( hMenu, (UINT) hOptions, uenable);
  630. DrawMenuBar( hMainWindow );
  631. }
  632. VOID
  633. UpdateSasMenu(VOID)
  634. {
  635. HMENU hMenu;
  636. HMENU hOptions;
  637. HMENU hSas;
  638. DWORD i;
  639. DWORD MenuItem;
  640. hMenu = GetMenu( hMainWindow );
  641. hOptions = GetSubMenu( hMenu, 2 );
  642. hSas = GetSubMenu( hOptions, 0 );
  643. //
  644. // Clean out existing ones
  645. //
  646. DeleteMenu( hSas, IDM_SAS_USERDEF1, MF_BYCOMMAND );
  647. DeleteMenu( hSas, IDM_SAS_USERDEF2, MF_BYCOMMAND );
  648. DeleteMenu( hSas, IDM_SAS_USERDEF3, MF_BYCOMMAND );
  649. DeleteMenu( hSas, IDM_SAS_USERDEF4, MF_BYCOMMAND );
  650. //
  651. // Add in current ones:
  652. //
  653. for (i = 0, MenuItem = IDM_SAS_USERDEF1; i < UserSases ; i++, MenuItem++ )
  654. {
  655. AppendMenu( hSas, MF_STRING, MenuItem, UserDefSas[i].Name );
  656. }
  657. }