home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / security / winnt / gina / gina.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-09  |  13.6 KB  |  534 lines

  1. //+---------------------------------------------------------------------------
  2. //
  3. //  Microsoft Windows
  4. //  Copyright 1992 - 1997 Microsoft Corporation.
  5. //
  6. //  File:       gina.c
  7. //
  8. //  Contents:
  9. //
  10. //  Classes:
  11. //
  12. //  Functions:
  13. //
  14. //  History:    4-20-95   RichardW   Created
  15. //
  16. //----------------------------------------------------------------------------
  17.  
  18.  
  19. #include "gina.h"
  20. #pragma hdrstop
  21.  
  22.  
  23. HINSTANCE                   hDllInstance;   // My instance, for resource loading
  24. HANDLE                      hGlobalWlx;     // Handle to tell winlogon who's calling
  25. PWLX_DISPATCH_VERSION_1_0   pWlxFuncs;      // Ptr to table of functions
  26.  
  27. #define WINLOGON_APP        TEXT("Winlogon")
  28. #define USERINIT            TEXT("Userinit")
  29. #define USERINIT_DEFAULT    TEXT("Userinit.exe")
  30.  
  31.  
  32.  
  33. //+---------------------------------------------------------------------------
  34. //
  35. //  Function:   DllMain
  36. //
  37. //  Synopsis:   DLL Entrance point
  38. //
  39. //  Arguments:  [hInstance]  --
  40. //              [dwReason]   --
  41. //              [lpReserved] --
  42. //
  43. //  History:    4-20-95   RichardW   Created
  44. //
  45. //  Notes:
  46. //
  47. //----------------------------------------------------------------------------
  48. BOOL
  49. WINAPI
  50. DllMain(
  51.     HINSTANCE       hInstance,
  52.     DWORD           dwReason,
  53.     LPVOID          lpReserved)
  54. {
  55.     switch (dwReason)
  56.     {
  57.         case DLL_PROCESS_ATTACH:
  58.             DisableThreadLibraryCalls ( hInstance );
  59.             hDllInstance = hInstance;
  60. #if DBG
  61.             InitDebugSupport();
  62. #endif
  63.         case DLL_PROCESS_DETACH:
  64.         default:
  65.             return(TRUE);
  66.     }
  67. }
  68.  
  69. //+---------------------------------------------------------------------------
  70. //
  71. //  Function:   WlxNegotiate
  72. //
  73. //  Synopsis:   Negotiate version of interface with Winlogon
  74. //
  75. //  Arguments:  [dwWinlogonVersion] --
  76. //              [pdwDllVersion]     --
  77. //
  78. //  Algorithm:
  79. //
  80. //  History:    4-20-95   RichardW   Created
  81. //
  82. //  Notes:
  83. //
  84. //----------------------------------------------------------------------------
  85. BOOL
  86. WINAPI
  87. WlxNegotiate(
  88.     DWORD                   dwWinlogonVersion,
  89.     DWORD                   *pdwDllVersion
  90.     )
  91. {
  92.     if (dwWinlogonVersion < WLX_CURRENT_VERSION)
  93.     {
  94.         DebugLog((DEB_ERROR, "Unknown version: %d\n", dwWinlogonVersion));
  95.         return(FALSE);
  96.     }
  97.  
  98.     *pdwDllVersion = WLX_CURRENT_VERSION;
  99.  
  100.     DebugLog((DEB_TRACE, "Negotiate:  successful!\n"));
  101.  
  102.     return(TRUE);
  103.  
  104. }
  105.  
  106.  
  107. //+---------------------------------------------------------------------------
  108. //
  109. //  Function:   WlxInitialize
  110. //
  111. //  Synopsis:   Initialize entrypoint from winlogon
  112. //
  113. //  Arguments:  [lpWinsta]           --
  114. //              [hWlx]               --
  115. //              [pvReserved]         --
  116. //              [pWinlogonFunctions] --
  117. //              [pWlxContext]        --
  118. //
  119. //  History:    4-20-95   RichardW   Created
  120. //
  121. //  Notes:
  122. //
  123. //----------------------------------------------------------------------------
  124. BOOL
  125. WINAPI
  126. WlxInitialize(
  127.     LPWSTR                  lpWinsta,
  128.     HANDLE                  hWlx,
  129.     PVOID                   pvReserved,
  130.     PVOID                   pWinlogonFunctions,
  131.     PVOID                   *pWlxContext
  132.     )
  133. {
  134.     PGlobals  pGlobals;
  135.  
  136.     pWlxFuncs = (PWLX_DISPATCH_VERSION_1_0) pWinlogonFunctions;
  137.  
  138.     hGlobalWlx = hWlx;
  139.  
  140.     pGlobals = LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, sizeof(Globals));
  141.  
  142.     *pWlxContext = (PVOID) pGlobals;
  143.  
  144.     //
  145.     // Read from registry...
  146.     //
  147.  
  148.     pGlobals->fAllowNewUser = TRUE;
  149.  
  150.  
  151.     pWlxFuncs->WlxUseCtrlAltDel(hWlx);
  152.  
  153.     InitCommonControls();
  154.  
  155.     return(TRUE);
  156. }
  157.  
  158.  
  159. //+---------------------------------------------------------------------------
  160. //
  161. //  Function:   WlxDisplaySASNotice
  162. //
  163. //  Synopsis:   Where we display the welcome, we're waiting dialog box
  164. //
  165. //  Arguments:  [pContext] --
  166. //
  167. //  History:    4-20-95   RichardW   Created
  168. //
  169. //  Notes:
  170. //
  171. //----------------------------------------------------------------------------
  172. VOID
  173. WINAPI
  174. WlxDisplaySASNotice(PVOID   pContext)
  175. {
  176.     int Result;
  177.  
  178.     Result = pWlxFuncs->WlxDialogBoxParam(  hGlobalWlx,
  179.                                             hDllInstance,
  180.                                             (LPTSTR) MAKEINTRESOURCE(IDD_WELCOME_DLG),
  181.                                             NULL,
  182.                                             WelcomeDlgProc,
  183.                                             0 );
  184. }
  185.  
  186. //+---------------------------------------------------------------------------
  187. //
  188. //  Function:   WlxLoggedOutSAS
  189. //
  190. //  Synopsis:   Called when no one logged on...
  191. //
  192. //  Arguments:  [pWlxContext]       --
  193. //              [dwSasType]         --
  194. //              [pAuthenticationId] --
  195. //              [pLogonSid]         --
  196. //              [pdwOptions]        --
  197. //              [phToken]           --
  198. //              [pMprNotifyInfo]    --
  199. //              [pProfile]          --
  200. //
  201. //  History:    4-20-95   RichardW   Created
  202. //
  203. //  Notes:
  204. //
  205. //----------------------------------------------------------------------------
  206. int
  207. WINAPI
  208. WlxLoggedOutSAS(
  209.     PVOID                   pWlxContext,
  210.     DWORD                   dwSasType,
  211.     PLUID                   pAuthenticationId,
  212.     PSID                    pLogonSid,
  213.     PDWORD                  pdwOptions,
  214.     PHANDLE                 phToken,
  215.     PWLX_MPR_NOTIFY_INFO    pMprNotifyInfo,
  216.     PVOID *                 pProfile
  217.     )
  218. {
  219.     int         result;
  220.     // PWLX_PROFILE_V1_0   pWlxProfile;
  221.     // PMiniAccount    pAccount;
  222.     PGlobals        pGlobals;
  223.  
  224.     pGlobals = (PGlobals) pWlxContext;
  225.  
  226.     result = pWlxFuncs->WlxDialogBoxParam(  hGlobalWlx,
  227.                                             hDllInstance,
  228.                                             (LPTSTR) MAKEINTRESOURCE(IDD_LOGON_DIALOG),
  229.                                             NULL,
  230.                                             LogonDlgProc,
  231.                                             (LPARAM) pGlobals );
  232.  
  233.     if (result == WLX_SAS_ACTION_LOGON)
  234.     {
  235.         result = AttemptLogon(pGlobals, pGlobals->pAccount,
  236.                                 pLogonSid, pAuthenticationId);
  237.  
  238.         if (result == WLX_SAS_ACTION_LOGON)
  239.         {
  240.             *pdwOptions = 0;
  241.             *phToken = pGlobals->hUserToken;
  242.             *pProfile = NULL;
  243.  
  244.             pMprNotifyInfo->pszUserName = DupString(pGlobals->pAccount->pszUsername);
  245.             pMprNotifyInfo->pszDomain = DupString(pGlobals->pAccount->pszDomain);
  246.             pMprNotifyInfo->pszPassword = DupString(pGlobals->pAccount->pszPassword);
  247.             pMprNotifyInfo->pszOldPassword = NULL;
  248.  
  249.         }
  250.     }
  251.     return(result);
  252. }
  253.  
  254. //+---------------------------------------------------------------------------
  255. //
  256. //  Function:   WlxActivateUserShell
  257. //
  258. //  Synopsis:   Activates progman or whatever for the user
  259. //
  260. //  Arguments:  [pWlxContext]       --
  261. //              [pszDesktop]        --
  262. //              [pszMprLogonScript] --
  263. //              [pEnvironment]      --
  264. //
  265. //  History:    4-20-95   RichardW   Created
  266. //
  267. //  Notes:
  268. //
  269. //----------------------------------------------------------------------------
  270. BOOL
  271. WINAPI
  272. WlxActivateUserShell(
  273.     PVOID                   pWlxContext,
  274.     PWSTR                   pszDesktop,
  275.     PWSTR                   pszMprLogonScript,
  276.     PVOID                   pEnvironment
  277.     )
  278. {
  279.     // BOOL        bExec;
  280.     WCHAR       szText[MAX_PATH];
  281.     PWSTR       pszScan;
  282.     STARTUPINFO si;
  283.     PROCESS_INFORMATION pi;
  284.     PGlobals    pGlobals;
  285.     DWORD       StartCount;
  286.  
  287.     pGlobals = (PGlobals) pWlxContext;
  288.  
  289.     GetProfileString(WINLOGON_APP, USERINIT, USERINIT_DEFAULT, szText, MAX_PATH);
  290.  
  291.     StartCount = 0;
  292.  
  293.     pszScan = wcstok(szText, TEXT(","));
  294.     while (pszScan)
  295.     {
  296.         ZeroMemory(&si, sizeof(si));
  297.         si.cb = sizeof(STARTUPINFO);
  298.         si.lpTitle = pszScan;
  299.         si.dwX = si.dwY = si.dwXSize = si.dwYSize = 0L;
  300.         si.dwFlags = 0;
  301.         si.wShowWindow = SW_SHOW;   // at least let the guy see it
  302.         si.lpReserved2 = NULL;
  303.         si.cbReserved2 = 0;
  304.         si.lpDesktop = pszDesktop;
  305.  
  306.         DebugLog((DEB_TRACE, "Starting '%ws' as user\n", pszScan));
  307.  
  308.         ImpersonateLoggedOnUser(pGlobals->hUserToken);
  309.  
  310.         if (CreateProcessAsUser(pGlobals->hUserToken,   // Token to run as
  311.                             NULL,                   // App name
  312.                             pszScan,                // Command Line
  313.                             NULL,                   // Process SD
  314.                             NULL,                   // Thread SD
  315.                             FALSE,                  // No inherit
  316.                             CREATE_UNICODE_ENVIRONMENT,
  317.                             pEnvironment,
  318.                             NULL,
  319.                             &si,
  320.                             &pi))
  321.         {
  322.             StartCount++;
  323.             CloseHandle(pi.hProcess);
  324.             CloseHandle(pi.hThread);
  325.         }
  326.  
  327.         RevertToSelf();
  328.  
  329.         pszScan = wcstok(NULL, TEXT(","));
  330.     }
  331.  
  332.     return(StartCount > 0);
  333. }
  334.  
  335.  
  336. //+---------------------------------------------------------------------------
  337. //
  338. //  Function:   WlxLoggedOnSAS
  339. //
  340. //  Synopsis:   Called when someone hits CAD when we're logged on
  341. //
  342. //  Arguments:  [pWlxContext] --
  343. //              [dwSasType]   --
  344. //              [pReserved]   --
  345. //
  346. //  Algorithm:
  347. //
  348. //  History:    4-20-95   RichardW   Created
  349. //
  350. //  Notes:
  351. //
  352. //----------------------------------------------------------------------------
  353. int
  354. WINAPI
  355. WlxLoggedOnSAS(
  356.     PVOID                   pWlxContext,
  357.     DWORD                   dwSasType,
  358.     PVOID                   pReserved
  359.     )
  360. {
  361.     int result;
  362.  
  363.     result = pWlxFuncs->WlxDialogBoxParam(  hGlobalWlx,
  364.                                             hDllInstance,
  365.                                             (LPTSTR) MAKEINTRESOURCE(IDD_OPTIONS_DIALOG),
  366.                                             NULL,
  367.                                             OptionsDlgProc,
  368.                                             (LPARAM) pWlxContext );
  369.  
  370.  
  371.  
  372.     return(result);
  373.  
  374. }
  375.  
  376. //+---------------------------------------------------------------------------
  377. //
  378. //  Function:   WlxIsLockOk
  379. //
  380. //  Synopsis:   Called to make sure that locking is ok
  381. //
  382. //  Arguments:  [pWlxContext] --
  383. //
  384. //  History:    4-20-95   RichardW   Created
  385. //
  386. //  Notes:
  387. //
  388. //----------------------------------------------------------------------------
  389. BOOL
  390. WINAPI
  391. WlxIsLockOk(
  392.     PVOID                   pWlxContext
  393.     )
  394. {
  395.     return(TRUE);
  396. }
  397.  
  398.  
  399. //+---------------------------------------------------------------------------
  400. //
  401. //  Function:   WlxDisplayLockedNotice
  402. //
  403. //  Synopsis:   Displays a notice while the workstation is locked
  404. //
  405. //  Arguments:  [pWlxContext] --
  406. //
  407. //  History:    4-20-95   RichardW   Created
  408. //
  409. //  Notes:
  410. //
  411. //----------------------------------------------------------------------------
  412. VOID
  413. WINAPI
  414. WlxDisplayLockedNotice(PVOID   pWlxContext)
  415. {
  416.     int Result;
  417.  
  418.     Result = pWlxFuncs->WlxDialogBoxParam(  hGlobalWlx,
  419.                                             hDllInstance,
  420.                                             (LPTSTR) MAKEINTRESOURCE(IDD_WKSTA_LOCKED),
  421.                                             NULL,
  422.                                             WelcomeDlgProc,
  423.                                             0 );
  424.  
  425.     return;
  426. }
  427.  
  428.  
  429. //+---------------------------------------------------------------------------
  430. //
  431. //  Function:   WlxWkstaLockedSAS
  432. //
  433. //  Synopsis:   Responds during an unlock attempt
  434. //
  435. //  Arguments:  [pWlxContext] --
  436. //              [dwSasType]   --
  437. //
  438. //  History:    4-20-95   RichardW   Created
  439. //
  440. //  Notes:
  441. //
  442. //----------------------------------------------------------------------------
  443. int
  444. WINAPI
  445. WlxWkstaLockedSAS(
  446.     PVOID                   pWlxContext,
  447.     DWORD                   dwSasType
  448.     )
  449. {
  450.     return(WLX_SAS_ACTION_UNLOCK_WKSTA);
  451. }
  452.  
  453.  
  454. //+---------------------------------------------------------------------------
  455. //
  456. //  Function:   WlxIsLogoffOk
  457. //
  458. //  Synopsis:   Called to make sure that logoff is ok
  459. //
  460. //  Arguments:  [pWlxContext] --
  461. //
  462. //  History:    4-20-95   RichardW   Created
  463. //
  464. //  Notes:
  465. //
  466. //----------------------------------------------------------------------------
  467. BOOL
  468. WINAPI
  469. WlxIsLogoffOk(
  470.     PVOID                   pWlxContext
  471.     )
  472. {
  473.     return(TRUE);
  474. }
  475.  
  476.  
  477. //+---------------------------------------------------------------------------
  478. //
  479. //  Function:   WlxLogoff
  480. //
  481. //  Synopsis:   Called when the user logs off
  482. //
  483. //  Arguments:  [pWlxContext] --
  484. //
  485. //  History:    4-20-95   RichardW   Created
  486. //
  487. //  Notes:
  488. //
  489. //----------------------------------------------------------------------------
  490. VOID
  491. WINAPI
  492. WlxLogoff(
  493.     PVOID                   pWlxContext
  494.     )
  495. {
  496.     PGlobals    pGlobals;
  497.  
  498.     pGlobals = (PGlobals) pWlxContext;
  499.  
  500.     //
  501.     // Winlogon has closed it for us..
  502.     //
  503.  
  504.     pGlobals->hUserToken = NULL;
  505.     pGlobals->pAccount = NULL;
  506.  
  507.     return;
  508. }
  509.  
  510.  
  511. //+---------------------------------------------------------------------------
  512. //
  513. //  Function:   WlxShutdown
  514. //
  515. //  Synopsis:   Called before shutdown so that we can unload/clean up.
  516. //
  517. //  Arguments:  [pWlxContext]  --
  518. //              [ShutdownType] --
  519. //
  520. //  History:    4-20-95   RichardW   Created
  521. //
  522. //  Notes:
  523. //
  524. //----------------------------------------------------------------------------
  525. VOID
  526. WINAPI
  527. WlxShutdown(
  528.     PVOID                   pWlxContext,
  529.     DWORD                   ShutdownType
  530.     )
  531. {
  532.     return;
  533. }
  534.