summaryrefslogblamecommitdiffstats
path: root/private/windows/gina/winlogon/sas.c
blob: f28bdd8052151067a56fca70df5325a285638978 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524











































































































































































































































































































































































































































































































































                                                                                                                                
/****************************** Module Header ******************************\
* Module Name: sas.c
*
* Copyright (c) 1991, Microsoft Corporation
*
* Support routines to implement processing of the secure attention sequence
*
* Users must always press the SAS key sequence before entering a password.
* This module catches the key press and forwards a SAS message to the
* correct winlogon window.
*
* History:
* 12-05-91 Davidc       Created.
\***************************************************************************/

#include "precomp.h"
#pragma hdrstop

// Internal Prototypes
LONG SASWndProc(
    HWND hwnd,
    UINT message,
    DWORD wParam,
    LONG lParam);

BOOL SASCreate(
    HWND hwnd);

BOOL SASDestroy(
    HWND hwnd);


// Global used to hold the window handle of the SAS window.
static HWND hwndSAS = NULL;
// LATER this hwndSAS will have to go in instance data when we have multiple threads

// Global for SAS window class name
static PWCHAR  szSASClass = TEXT("SAS window class");


#if DBG
#define DEFAULT_QUICK_REBOOT    1
#else
#define DEFAULT_QUICK_REBOOT    0
#endif

#define SHELL_RESTART_TIMER_ID  100

/***************************************************************************\
* SASInit
*
* Initialises this module.
*
* Creates a window to receive the SAS and registers the
* key sequence as a hot key.
*
* Returns TRUE on success, FALSE on failure.
*
* 12-05-91 Davidc       Created.
\***************************************************************************/

BOOL SASInit(
    PGLOBALS pGlobals)
{
    WNDCLASS wc;

    if (hwndSAS != NULL) {
        DebugLog((DEB_ERROR, "SAS module already initialized !!"));
        return(FALSE);
    }

    //
    // Register the notification window class
    //

    wc.style            = CS_SAVEBITS;
    wc.lpfnWndProc      = (WNDPROC)SASWndProc;
    wc.cbClsExtra       = 0;
    wc.cbWndExtra       = 0;
    wc.hInstance        = pGlobals->hInstance;
    wc.hIcon            = NULL;
    wc.hCursor          = NULL;
    wc.hbrBackground    = NULL;
    wc.lpszMenuName     = NULL;
    wc.lpszClassName    = szSASClass;

    if (!RegisterClass(&wc))
        return FALSE;

    hwndSAS = CreateWindowEx(0L, szSASClass, TEXT("SAS window"),
            WS_OVERLAPPEDWINDOW,
            0, 0, 0, 0,
            NULL, NULL, pGlobals->hInstance, NULL);

    if (hwndSAS == NULL)
        return FALSE;

    //
    // Store our globals pointer in the window user data
    //

    SetWindowLong(hwndSAS, GWL_USERDATA, (LONG)pGlobals);

    //
    // Register this window with windows so we get notified for
    // screen-saver startup and user log-off
    //
    if (!SetLogonNotifyWindow(pGlobals->WindowStation.hwinsta, hwndSAS)) {
        DebugLog((DEB_ERROR, "Failed to set logon notify window"));
        return(FALSE);
    }

    return(TRUE);
}


/***************************************************************************\
* SASTerminate
*
* Terminates this module.
*
* Unregisters the SAS and destroys the SAS windows
*
* 12-05-91 Davidc       Created.
\***************************************************************************/

VOID SASTerminate(VOID)
{
    DestroyWindow(hwndSAS);

    // Reset our globals
    hwndSAS = NULL;
}


/***************************************************************************\
* SASWndProc
*
* Window procedure for the SAS window.
*
* This window registers the SAS hotkey sequence, and forwards any hotkey
* messages to the current winlogon window. It does this using a
* timeout module function. i.e. every window should register a timeout
* even if it's 0 if they want to get SAS messages.
*
* History:
* 12-09-91 Davidc       Created.
\***************************************************************************/

LONG SASWndProc(
    HWND hwnd,
    UINT message,
    DWORD wParam,
    LONG lParam)
{
    PGLOBALS pGlobals = (PGLOBALS)GetWindowLong(hwnd, GWL_USERDATA);

    switch (message)
    {

        case WM_CREATE:
            if (!SASCreate(hwnd))
            {
                return(TRUE);   // Fail creation
            }
            return(FALSE); // Continue creating window

        case WM_DESTROY:
            DebugLog(( DEB_TRACE, "SAS Window Shutting down?\n"));
#if DBG
            DebugBreak();
#endif
            SASDestroy(hwnd);
            return(0);

        case WM_HOTKEY:
            if (wParam == 1)
            {
                QuickReboot(pGlobals, TRUE);
                return(0);
            }

#if DBG
            if (wParam == 2)
            {
                switch (pGlobals->WindowStation.ActiveDesktop)
                {
                    case Desktop_Winlogon:
                        SetActiveDesktop(&pGlobals->WindowStation, Desktop_Application);
                        break;
                    case Desktop_Application:
                        SetActiveDesktop(&pGlobals->WindowStation, Desktop_Winlogon);
                        break;
                }
                return(0);
            }
            if (wParam == 3)
            {
                DebugBreak();
                return(0);
            }
#endif

            if (wParam == 4)
            {
                PGINASESSION pGina = pGlobals->pGina;
                WCHAR szTaskMgr[] = L"taskmgr.exe";

                DebugLog((DEB_TRACE, "Starting taskmgr.exe.\n"));

                if (pGlobals->UserLoggedOn ) {
                    pGina->pWlxStartApplication(pGina->pGinaContext,
                                                APPLICATION_DESKTOP_PATH,
                                                pGlobals->UserProcessData.pEnvironment,
                                                szTaskMgr);
                }
                return(0);
            }

            CADNotify(pGlobals, WLX_SAS_TYPE_CTRL_ALT_DEL);
            return(0);

        case WM_LOGONNOTIFY: // A private notification from Windows

            DebugLog((DEB_TRACE_SAS, "LOGONNOTIFY message %d\n", wParam ));

            switch (wParam)
            {


                case LOGON_LOGOFF:

#if DBG
                    DebugLog((DEB_TRACE_SAS, "\tWINLOGON     : %s\n", (lParam & EWX_WINLOGON_CALLER) ? "True" : "False"));
                    DebugLog((DEB_TRACE_SAS, "\tSYSTEM       : %s\n", (lParam & EWX_SYSTEM_CALLER) ? "True" : "False"));
                    DebugLog((DEB_TRACE_SAS, "\tSHUTDOWN     : %s\n", (lParam & EWX_SHUTDOWN) ? "True" : "False"));
                    DebugLog((DEB_TRACE_SAS, "\tREBOOT       : %s\n", (lParam & EWX_REBOOT) ? "True" : "False"));
                    DebugLog((DEB_TRACE_SAS, "\tPOWEROFF     : %s\n", (lParam & EWX_POWEROFF) ? "True" : "False"));
                    DebugLog((DEB_TRACE_SAS, "\tFORCE        : %s\n", (lParam & EWX_FORCE) ? "True" : "False"));
                    DebugLog((DEB_TRACE_SAS, "\tOLD_SYSTEM   : %s\n", (lParam & EWX_WINLOGON_OLD_SYSTEM) ? "True" : "False"));
                    DebugLog((DEB_TRACE_SAS, "\tOLD_SHUTDOWN : %s\n", (lParam & EWX_WINLOGON_OLD_SHUTDOWN) ? "True" : "False"));
                    DebugLog((DEB_TRACE_SAS, "\tOLD_REBOOT   : %s\n", (lParam & EWX_WINLOGON_OLD_REBOOT) ? "True" : "False"));
                    DebugLog((DEB_TRACE_SAS, "\tOLD_POWEROFF : %s\n", (lParam & EWX_WINLOGON_OLD_POWEROFF) ? "True" : "False"));
#endif

                    //
                    // If there is an exit windows in progress, reject this
                    // message if it is not our own call coming back.  This
                    // prevents people from calling ExitWindowsEx repeatedly
                    //

                    if ( ExitWindowsInProgress &&
                         ( !( lParam & EWX_WINLOGON_CALLER ) ) )
                    {
                        break;

                    }
                    pGlobals->LogoffFlags = lParam;
                    CADNotify(pGlobals, WLX_SAS_TYPE_USER_LOGOFF);
                    break;

                case LOGON_INPUT_TIMEOUT:
                    //
                    // Notify the current window
                    //
                    // ForwardMessage(pGlobals, WM_SCREEN_SAVER_TIMEOUT, 0, 0);
                    CADNotify(pGlobals, WLX_SAS_TYPE_SCRNSVR_TIMEOUT);
                    break;

                case LOGON_RESTARTSHELL:
                    //
                    // Restart the shell after X seconds
                    //
                    // We don't restart the shell for the following conditions:
                    //
                    // 1) No one is logged on
                    // 2) We are in the process of logging off
                    //    (logoffflags will be non-zero)
                    // 3) The shell exiting gracefully
                    //    (Exit status is in lParam.  1 = graceful)
                    // 4) A new user has logged on after the request
                    //    to restart the shell.
                    //    (in the case of autoadminlogon, the new
                    //     user could be logged on before the restart
                    //     request comes through).
                    //

                    if (!pGlobals->UserLoggedOn  ||
                        pGlobals->LogoffFlags    ||
                        (lParam == 1)            ||
                        (pGlobals->TickCount > (DWORD)GetMessageTime())) {

                        break;
                    }

                    SetTimer (hwnd, SHELL_RESTART_TIMER_ID, 2000, NULL);
                    break;
            }

            return(0);

        case WM_TIMER:
            {
            PGINASESSION pGina;
            LONG lResult;
            HKEY hKey;
            BOOL bRestart = TRUE;
            DWORD dwType, dwSize;


            //
            //  Restart the shell
            //

            if (wParam != SHELL_RESTART_TIMER_ID) {
                break;
            }

            KillTimer (hwnd, SHELL_RESTART_TIMER_ID);


            //
            // Check if we should restart the shell
            //

            lResult = RegOpenKeyEx (HKEY_LOCAL_MACHINE,
                                    WINLOGON_KEY,
                                    0,
                                    KEY_READ,
                                    &hKey);

            if (lResult == ERROR_SUCCESS) {

                dwSize = sizeof(bRestart);
                RegQueryValueEx (hKey,
                                 TEXT("AutoRestartShell"),
                                 NULL,
                                 &dwType,
                                 (LPBYTE) &bRestart,
                                 &dwSize);

                RegCloseKey (hKey);
            }


            if (bRestart) {
                PWCH  pchData;
                PWSTR pszTok;

                DebugLog((DEB_TRACE, "Restarting user's shell.\n"));


                pGina = pGlobals->pGina;

                pchData = AllocAndGetPrivateProfileString(APPLICATION_NAME,
                                                          SHELL_KEY,
                                                          TEXT("explorer.exe"),
                                                          NULL);

                if (!pchData) {
                    break;
                }

                pszTok = wcstok(pchData, TEXT(","));
                while (pszTok)
                {
                    if (*pszTok == TEXT(' '))
                    {
                        while (*pszTok++ == TEXT(' '))
                            ;
                    }


                    if (pGina->pWlxStartApplication(pGina->pGinaContext,
                                                    APPLICATION_DESKTOP_PATH,
                                                    pGlobals->UserProcessData.pEnvironment,
                                                    pszTok)) {

                        ReportWinlogonEvent(pGlobals,
                                EVENTLOG_INFORMATION_TYPE,
                                EVENT_SHELL_RESTARTED,
                                0,
                                NULL,
                                1,
                                pszTok);
                    }

                    pszTok = wcstok(NULL, TEXT(","));
                }

                Free(pchData);
            }

            }
            break;

        default:
            return DefWindowProc(hwnd, message, wParam, lParam);

    }

    return 0L;
}

BOOL bRegisteredQuickReboot;
BOOL bRegisteredDesktopSwitching;
BOOL bRegisteredWinlogonBreakpoint;
BOOL bRegisteredTaskmgr;


/***************************************************************************\
* SASCreate
*
* Does any processing required for WM_CREATE message.
*
* Returns TRUE on success, FALSE on failure
*
* History:
* 12-09-91 Davidc       Created.
\***************************************************************************/

BOOL SASCreate(
    HWND hwnd)
{
    // Register the SAS unless we are told not to.


    if (GetProfileInt( APPNAME_WINLOGON, VARNAME_AUTOLOGON, 0 ) != 2) {
        if (!RegisterHotKey(hwnd, 0, MOD_CONTROL | MOD_ALT, VK_DELETE)) {
            DebugLog((DEB_ERROR, "failed to register SAS"));
            return(FALSE);   // Fail creation
        }
    }


    //
    // (Ctrl+Alt+Shift+Del) hotkey to reboot into DOS directly
    //

    if (GetProfileInt( APPNAME_WINLOGON, VARNAME_ENABLEQUICKREBOOT, DEFAULT_QUICK_REBOOT) != 0) {
        if (!RegisterHotKey(hwnd, 1, MOD_CONTROL | MOD_ALT | MOD_SHIFT, VK_DELETE)) {
            DebugLog((DEB_ERROR, "failed to register quick reboot SAS"));
            bRegisteredQuickReboot = FALSE;
        } else {
            bRegisteredQuickReboot = TRUE;
        }
    }

#if DBG
    //
    // (Ctrl+Alt+Tab) will switch between desktops
    //
    if (GetProfileInt( APPNAME_WINLOGON, VARNAME_ENABLEDESKTOPSWITCHING, 0 ) != 0) {
        if (!RegisterHotKey(hwnd, 2, MOD_CONTROL | MOD_ALT, VK_TAB)) {
            DebugLog((DEB_ERROR, "failed to register desktop switch SAS"));
            bRegisteredDesktopSwitching = FALSE;
        } else {
            bRegisteredDesktopSwitching = TRUE;
        }
    }


    if (WinlogonInfoLevel & DEB_COOL_SWITCH) {
        if (!RegisterHotKey(hwnd, 3, MOD_CONTROL | MOD_ALT | MOD_SHIFT, VK_TAB)) {
            DebugLog((DEB_ERROR, "failed to register breakpoint SAS"));
            bRegisteredWinlogonBreakpoint = FALSE;
        } else {
            bRegisteredWinlogonBreakpoint = TRUE;
        }
    }
#endif

    //
    // (Ctrl+Shift+Esc) will start taskmgr
    //

    if (!RegisterHotKey(hwnd, 4, MOD_CONTROL | MOD_SHIFT, VK_ESCAPE)) {
        DebugLog((DEB_ERROR, "failed to register taskmgr hotkey"));
        bRegisteredTaskmgr = FALSE;
    } else {
        bRegisteredTaskmgr = TRUE;
    }

    return(TRUE);
}


/***************************************************************************\
* SASDestroy
*
* Does any processing required for WM_DESTROY message.
*
* Returns TRUE on success, FALSE on failure
*
* History:
* 12-09-91 Davidc       Created.
\***************************************************************************/

BOOL SASDestroy(
    HWND hwnd)
{
    // Unregister the SAS
    UnregisterHotKey(hwnd, 0);

    if (bRegisteredQuickReboot) {
        UnregisterHotKey(hwnd, 1);
    }
    if (bRegisteredDesktopSwitching) {
        UnregisterHotKey(hwnd, 2);
    }

#if DBG
    if (bRegisteredWinlogonBreakpoint) {
        UnregisterHotKey(hwnd, 3);
    }
#endif

    if (bRegisteredTaskmgr) {
        UnregisterHotKey(hwnd, 4);
    }


    return(TRUE);
}