2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Winlogon
4 * FILE: base/system/winlogon/winlogon.c
6 * PROGRAMMERS: Thomas Weidenmueller (w3seek@users.sourceforge.net)
8 * Hervé Poussineau (hpoussin@reactos.org)
11 /* INCLUDES *****************************************************************/
15 WINE_DEFAULT_DEBUG_CHANNEL(winlogon
);
17 /* GLOBALS ******************************************************************/
19 HINSTANCE hAppInstance
;
20 PWLSESSION WLSession
= NULL
;
22 /* FUNCTIONS *****************************************************************/
26 StartServicesManager(VOID
)
28 STARTUPINFOW StartupInfo
;
29 PROCESS_INFORMATION ProcessInformation
;
30 LPCWSTR ServiceString
= L
"services.exe";
33 /* Start the service control manager (services.exe) */
34 ZeroMemory(&StartupInfo
, sizeof(STARTUPINFOW
));
35 StartupInfo
.cb
= sizeof(StartupInfo
);
36 StartupInfo
.lpReserved
= NULL
;
37 StartupInfo
.lpDesktop
= NULL
;
38 StartupInfo
.lpTitle
= NULL
;
39 StartupInfo
.dwFlags
= 0;
40 StartupInfo
.cbReserved2
= 0;
41 StartupInfo
.lpReserved2
= 0;
43 TRACE("WL: Creating new process - %S\n", ServiceString
);
45 res
= CreateProcessW(ServiceString
,
57 ERR("WL: Failed to execute services (error %lu)\n", GetLastError());
61 TRACE("WL: Created new process - %S\n", ServiceString
);
63 CloseHandle(ProcessInformation
.hThread
);
64 CloseHandle(ProcessInformation
.hProcess
);
66 TRACE("WL: StartServicesManager() done.\n");
76 STARTUPINFOW StartupInfo
;
77 PROCESS_INFORMATION ProcessInformation
;
78 LPCWSTR ServiceString
= L
"lsass.exe";
81 /* Start the local security authority subsystem (lsass.exe) */
82 ZeroMemory(&StartupInfo
, sizeof(STARTUPINFOW
));
83 StartupInfo
.cb
= sizeof(StartupInfo
);
84 StartupInfo
.lpReserved
= NULL
;
85 StartupInfo
.lpDesktop
= NULL
;
86 StartupInfo
.lpTitle
= NULL
;
87 StartupInfo
.dwFlags
= 0;
88 StartupInfo
.cbReserved2
= 0;
89 StartupInfo
.lpReserved2
= 0;
91 TRACE("WL: Creating new process - %S\n", ServiceString
);
93 res
= CreateProcessW(ServiceString
,
102 &ProcessInformation
);
104 TRACE("WL: Created new process - %S\n", ServiceString
);
106 CloseHandle(ProcessInformation
.hThread
);
107 CloseHandle(ProcessInformation
.hProcess
);
120 hEvent
= CreateEventW(NULL
,
123 L
"LSA_RPC_SERVER_ACTIVE");
126 dwError
= GetLastError();
127 TRACE("WL: Failed to create the notication event (Error %lu)\n", dwError
);
129 if (dwError
== ERROR_ALREADY_EXISTS
)
131 hEvent
= OpenEventW(SYNCHRONIZE
,
133 L
"LSA_RPC_SERVER_ACTIVE");
136 ERR("WL: Could not open the notification event (Error %lu)\n", GetLastError());
142 TRACE("WL: Wait for the LSA server!\n");
143 WaitForSingleObject(hEvent
, INFINITE
);
144 TRACE("WL: LSA server running!\n");
152 InitKeyboardLayouts(VOID
)
154 WCHAR wszKeyName
[12], wszKLID
[10];
155 DWORD dwSize
= sizeof(wszKLID
), dwType
, i
= 1;
160 /* Open registry key with preloaded layouts */
161 if (RegOpenKeyExW(HKEY_CURRENT_USER
, L
"Keyboard Layout\\Preload", 0, KEY_READ
, &hKey
) == ERROR_SUCCESS
)
165 /* Read values with integer names only */
166 swprintf(wszKeyName
, L
"%d", i
++);
167 if (RegQueryValueExW(hKey
, wszKeyName
, NULL
, &dwType
, (LPBYTE
)wszKLID
, &dwSize
) != ERROR_SUCCESS
)
169 /* There is no more entries */
173 /* Only REG_SZ values are valid */
174 if (dwType
!= REG_SZ
)
176 ERR("Wrong type: %ws!\n", wszKLID
);
180 /* Load keyboard layout with given locale id */
181 Flags
= KLF_SUBSTITUTE_OK
;
183 Flags
|= KLF_NOTELLSHELL
|KLF_REPLACELANG
;
185 Flags
|= KLF_ACTIVATE
; // |0x40000000
186 if (!LoadKeyboardLayoutW(wszKLID
, Flags
))
188 ERR("LoadKeyboardLayoutW(%ws) failed!\n", wszKLID
);
193 /* We loaded at least one layout - success */
198 /* Close the key now */
202 WARN("RegOpenKeyExW(Keyboard Layout\\Preload) failed!\n");
206 /* If we failed, load US keyboard layout */
207 if (LoadKeyboardLayoutW(L
"00000409", 0x04090409))
216 DisplayStatusMessage(IN PWLSESSION Session
,
220 WCHAR StatusMsg
[MAX_PATH
];
222 if (Session
->Gina
.Version
< WLX_VERSION_1_3
)
225 if (Session
->SuppressStatus
)
228 if (LoadStringW(hAppInstance
, ResourceId
, StatusMsg
, MAX_PATH
) == 0)
231 return Session
->Gina
.Functions
.WlxDisplayStatusMessage(Session
->Gina
.Context
, hDesktop
, 0, NULL
, StatusMsg
);
236 RemoveStatusMessage(IN PWLSESSION Session
)
238 if (Session
->Gina
.Version
< WLX_VERSION_1_3
)
241 return Session
->Gina
.Functions
.WlxRemoveStatusMessage(Session
->Gina
.Context
);
248 GinaLoadFailedWindowProc(IN HWND hwndDlg
,
257 switch (LOWORD(wParam
))
260 EndDialog(hwndDlg
, IDOK
);
269 WCHAR templateText
[MAX_PATH
], text
[MAX_PATH
];
271 len
= GetDlgItemTextW(hwndDlg
, IDC_GINALOADFAILED
, templateText
, MAX_PATH
);
274 wsprintfW(text
, templateText
, (LPWSTR
)lParam
);
275 SetDlgItemTextW(hwndDlg
, IDC_GINALOADFAILED
, text
);
278 SetFocus(GetDlgItem(hwndDlg
, IDOK
));
284 EndDialog(hwndDlg
, IDCANCEL
);
295 WinMain(IN HINSTANCE hInstance
,
296 IN HINSTANCE hPrevInstance
,
301 LSA_STRING ProcessName
, PackageName
;
303 LSA_OPERATIONAL_MODE Mode
;
305 ULONG AuthenticationPackage
;
308 ULONG HardErrorResponse
;
311 UNREFERENCED_PARAMETER(hPrevInstance
);
312 UNREFERENCED_PARAMETER(lpCmdLine
);
313 UNREFERENCED_PARAMETER(nShowCmd
);
315 hAppInstance
= hInstance
;
317 if (!RegisterLogonProcess(GetCurrentProcessId(), TRUE
))
319 ERR("WL: Could not register logon process\n");
320 NtShutdownSystem(ShutdownNoReboot
);
324 WLSession
= (PWLSESSION
)HeapAlloc(GetProcessHeap(), 0, sizeof(WLSESSION
));
327 ERR("WL: Could not allocate memory for winlogon instance\n");
328 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED
, 0, 0, NULL
, OptionOk
, &HardErrorResponse
);
332 ZeroMemory(WLSession
, sizeof(WLSESSION
));
333 WLSession
->DialogTimeout
= 120; /* 2 minutes */
335 if (!CreateWindowStationAndDesktops(WLSession
))
337 ERR("WL: Could not create window station and desktops\n");
338 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED
, 0, 0, NULL
, OptionOk
, &HardErrorResponse
);
342 LockWorkstation(WLSession
);
344 /* Load default keyboard layouts */
345 if (!InitKeyboardLayouts())
347 ERR("WL: Could not preload keyboard layouts\n");
348 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED
, 0, 0, NULL
, OptionOk
, &HardErrorResponse
);
352 if (!StartServicesManager())
354 ERR("WL: Could not start services.exe\n");
355 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED
, 0, 0, NULL
, OptionOk
, &HardErrorResponse
);
361 ERR("WL: Failed to start lsass.exe service (error %lu)\n", GetLastError());
362 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED
, 0, 0, 0, OptionOk
, &HardErrorResponse
);
366 /* Wait for the LSA server */
369 /* Load and initialize gina */
370 if (!GinaInit(WLSession
))
372 ERR("WL: Failed to initialize Gina\n");
373 DialogBoxParam(hAppInstance
, MAKEINTRESOURCE(IDD_GINALOADFAILED
), GetDesktopWindow(), GinaLoadFailedWindowProc
, (LPARAM
)L
"");
374 HandleShutdown(WLSession
, WLX_SAS_ACTION_SHUTDOWN_REBOOT
);
378 DisplayStatusMessage(WLSession
, WLSession
->WinlogonDesktop
, IDS_REACTOSISSTARTINGUP
);
381 /* Connect to NetLogon service (lsass.exe) */
382 /* Real winlogon uses "Winlogon" */
383 RtlInitUnicodeString((PUNICODE_STRING
)&ProcessName
, L
"Winlogon");
384 Status
= LsaRegisterLogonProcess(&ProcessName
, &LsaHandle
, &Mode
);
385 if (Status
== STATUS_PORT_CONNECTION_REFUSED
)
387 /* Add the 'SeTcbPrivilege' privilege and try again */
388 Status
= RtlAdjustPrivilege(SE_TCB_PRIVILEGE
, TRUE
, TRUE
, &Old
);
389 if (!NT_SUCCESS(Status
))
391 ERR("RtlAdjustPrivilege() failed with error %lu\n", LsaNtStatusToWinError(Status
));
395 Status
= LsaRegisterLogonProcess(&ProcessName
, &LsaHandle
, &Mode
);
398 if (!NT_SUCCESS(Status
))
400 ERR("LsaRegisterLogonProcess() failed with error %lu\n", LsaNtStatusToWinError(Status
));
404 RtlInitUnicodeString((PUNICODE_STRING
)&PackageName
, MICROSOFT_KERBEROS_NAME_W
);
405 Status
= LsaLookupAuthenticationPackage(LsaHandle
, &PackageName
, &AuthenticationPackage
);
406 if (!NT_SUCCESS(Status
))
408 ERR("LsaLookupAuthenticationPackage() failed with error %lu\n", LsaNtStatusToWinError(Status
));
409 LsaDeregisterLogonProcess(LsaHandle
);
414 /* Create a hidden window to get SAS notifications */
415 if (!InitializeSAS(WLSession
))
417 ERR("WL: Failed to initialize SAS\n");
421 //DisplayStatusMessage(Session, Session->WinlogonDesktop, IDS_PREPARENETWORKCONNECTIONS);
422 //DisplayStatusMessage(Session, Session->WinlogonDesktop, IDS_APPLYINGCOMPUTERSETTINGS);
424 /* Display logged out screen */
425 WLSession
->LogonState
= STATE_LOGGED_OFF
;
426 RemoveStatusMessage(WLSession
);
428 /* Check for pending setup */
429 if (GetSetupType() != 0)
431 TRACE("WL: Setup mode detected\n");
433 /* Run setup and reboot when done */
434 SwitchDesktop(WLSession
->ApplicationDesktop
);
438 PostMessageW(WLSession
->SASWindow
, WLX_WM_SAS
, WLX_SAS_TYPE_TIMEOUT
, 0);
440 /* Tell kernel that CurrentControlSet is good (needed
441 * to support Last good known configuration boot) */
442 NtInitializeRegistry(CM_BOOT_FLAG_ACCEPTED
| 1);
444 /* Message loop for the SAS window */
445 while (GetMessageW(&Msg
, WLSession
->SASWindow
, 0, 0))
447 TranslateMessage(&Msg
);
448 DispatchMessageW(&Msg
);
451 /* We never go there */