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 #include <wine/debug.h>
17 WINE_DEFAULT_DEBUG_CHANNEL(winlogon
);
19 /* GLOBALS ******************************************************************/
21 HINSTANCE hAppInstance
;
22 PWLSESSION WLSession
= NULL
;
24 /* FUNCTIONS *****************************************************************/
32 typedef BOOL (WINAPI
*PLAYSOUNDW
)(LPCWSTR
,HMODULE
,DWORD
);
33 typedef UINT (WINAPI
*WAVEOUTGETNUMDEVS
)(VOID
);
35 WAVEOUTGETNUMDEVS waveOutGetNumDevs
;
40 hLibrary
= LoadLibraryW(L
"winmm.dll");
43 waveOutGetNumDevs
= (WAVEOUTGETNUMDEVS
)GetProcAddress(hLibrary
, "waveOutGetNumDevs");
44 if (waveOutGetNumDevs
)
46 NumDevs
= waveOutGetNumDevs();
53 FreeLibrary(hLibrary
);
58 Play
= (PLAYSOUNDW
)GetProcAddress(hLibrary
, "PlaySoundW");
61 Ret
= Play(FileName
, NULL
, Flags
);
63 FreeLibrary(hLibrary
);
72 IN LPVOID lpParameter
)
75 WCHAR szBuffer
[MAX_PATH
] = {0};
76 WCHAR szDest
[MAX_PATH
];
77 DWORD dwSize
= sizeof(szBuffer
);
78 SERVICE_STATUS_PROCESS Info
;
82 if (RegOpenKeyExW(HKEY_CURRENT_USER
, L
"AppEvents\\Schemes\\Apps\\.Default\\WindowsLogon\\.Current", 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
)
87 if (RegQueryValueExW(hKey
, NULL
, NULL
, NULL
, (LPBYTE
)szBuffer
, &dwSize
) != ERROR_SUCCESS
)
100 szBuffer
[MAX_PATH
-1] = L
'\0';
101 if (ExpandEnvironmentStringsW(szBuffer
, szDest
, MAX_PATH
))
103 SC_HANDLE hSCManager
, hService
;
105 hSCManager
= OpenSCManager(NULL
, NULL
, SC_MANAGER_CONNECT
);
109 hService
= OpenServiceW(hSCManager
, L
"wdmaud", GENERIC_READ
);
112 CloseServiceHandle(hSCManager
);
113 TRACE("WL: failed to open sysaudio Status %x\n", GetLastError());
119 if (!QueryServiceStatusEx(hService
, SC_STATUS_PROCESS_INFO
, (LPBYTE
)&Info
, sizeof(SERVICE_STATUS_PROCESS
), &dwSize
))
121 TRACE("WL: QueryServiceStatusEx failed %x\n", GetLastError());
125 if (Info
.dwCurrentState
== SERVICE_RUNNING
)
130 }while(Index
++ < 20);
132 CloseServiceHandle(hService
);
133 CloseServiceHandle(hSCManager
);
135 if (Info
.dwCurrentState
!= SERVICE_RUNNING
)
138 PlaySoundRoutine(szDest
, TRUE
, SND_FILENAME
);
146 StartServicesManager(VOID
)
148 STARTUPINFOW StartupInfo
;
149 PROCESS_INFORMATION ProcessInformation
;
150 LPCWSTR ServiceString
= L
"services.exe";
153 /* Start the service control manager (services.exe) */
154 ZeroMemory(&StartupInfo
, sizeof(STARTUPINFOW
));
155 StartupInfo
.cb
= sizeof(StartupInfo
);
156 StartupInfo
.lpReserved
= NULL
;
157 StartupInfo
.lpDesktop
= NULL
;
158 StartupInfo
.lpTitle
= NULL
;
159 StartupInfo
.dwFlags
= 0;
160 StartupInfo
.cbReserved2
= 0;
161 StartupInfo
.lpReserved2
= 0;
163 TRACE("WL: Creating new process - %S\n", ServiceString
);
165 res
= CreateProcessW(
175 &ProcessInformation
);
178 ERR("WL: Failed to execute services (error %lu)\n", GetLastError());
182 TRACE("WL: Created new process - %S\n", ServiceString
);
184 CloseHandle(ProcessInformation
.hThread
);
185 CloseHandle(ProcessInformation
.hProcess
);
187 TRACE("WL: StartServicesManager() done.\n");
196 STARTUPINFOW StartupInfo
;
197 PROCESS_INFORMATION ProcessInformation
;
198 LPCWSTR ServiceString
= L
"lsass.exe";
201 /* Start the local security authority subsystem (lsass.exe) */
202 ZeroMemory(&StartupInfo
, sizeof(STARTUPINFOW
));
203 StartupInfo
.cb
= sizeof(StartupInfo
);
204 StartupInfo
.lpReserved
= NULL
;
205 StartupInfo
.lpDesktop
= NULL
;
206 StartupInfo
.lpTitle
= NULL
;
207 StartupInfo
.dwFlags
= 0;
208 StartupInfo
.cbReserved2
= 0;
209 StartupInfo
.lpReserved2
= 0;
211 TRACE("WL: Creating new process - %S\n", ServiceString
);
213 res
= CreateProcessW(
223 &ProcessInformation
);
225 TRACE("WL: Created new process - %S\n", ServiceString
);
227 CloseHandle(ProcessInformation
.hThread
);
228 CloseHandle(ProcessInformation
.hProcess
);
240 hEvent
= CreateEventW(NULL
,
243 L
"LSA_RPC_SERVER_ACTIVE");
246 dwError
= GetLastError();
247 TRACE("WL: Failed to create the notication event (Error %lu)\n", dwError
);
249 if (dwError
== ERROR_ALREADY_EXISTS
)
251 hEvent
= OpenEventW(SYNCHRONIZE
,
253 L
"LSA_RPC_SERVER_ACTIVE");
256 ERR("WL: Could not open the notification event (Error %lu)\n", GetLastError());
262 TRACE("WL: Wait for the LSA server!\n");
263 WaitForSingleObject(hEvent
, INFINITE
);
264 TRACE("WL: LSA server running!\n");
271 DisplayStatusMessage(
272 IN PWLSESSION Session
,
276 WCHAR StatusMsg
[MAX_PATH
];
278 if (Session
->Gina
.Version
< WLX_VERSION_1_3
)
281 if (Session
->SuppressStatus
)
284 if (LoadStringW(hAppInstance
, ResourceId
, StatusMsg
, MAX_PATH
) == 0)
287 return Session
->Gina
.Functions
.WlxDisplayStatusMessage(Session
->Gina
.Context
, hDesktop
, 0, NULL
, StatusMsg
);
292 IN PWLSESSION Session
)
294 if (Session
->Gina
.Version
< WLX_VERSION_1_3
)
297 return Session
->Gina
.Functions
.WlxRemoveStatusMessage(Session
->Gina
.Context
);
300 static INT_PTR CALLBACK
301 GinaLoadFailedWindowProc(
311 switch (LOWORD(wParam
))
314 EndDialog(hwndDlg
, IDOK
);
322 WCHAR templateText
[MAX_PATH
], text
[MAX_PATH
];
324 len
= GetDlgItemTextW(hwndDlg
, IDC_GINALOADFAILED
, templateText
, MAX_PATH
);
327 wsprintfW(text
, templateText
, (LPWSTR
)lParam
);
328 SetDlgItemTextW(hwndDlg
, IDC_GINALOADFAILED
, text
);
330 SetFocus(GetDlgItem(hwndDlg
, IDOK
));
335 EndDialog(hwndDlg
, IDCANCEL
);
345 IN HINSTANCE hInstance
,
346 IN HINSTANCE hPrevInstance
,
351 LSA_STRING ProcessName
, PackageName
;
353 LSA_OPERATIONAL_MODE Mode
;
355 ULONG AuthenticationPackage
;
358 ULONG HardErrorResponse
;
362 UNREFERENCED_PARAMETER(hPrevInstance
);
363 UNREFERENCED_PARAMETER(lpCmdLine
);
364 UNREFERENCED_PARAMETER(nShowCmd
);
366 hAppInstance
= hInstance
;
368 if (!RegisterLogonProcess(GetCurrentProcessId(), TRUE
))
370 ERR("WL: Could not register logon process\n");
371 HandleShutdown(NULL
, WLX_SAS_ACTION_SHUTDOWN_POWER_OFF
);
372 NtShutdownSystem(ShutdownNoReboot
);
376 WLSession
= (PWLSESSION
)HeapAlloc(GetProcessHeap(), 0, sizeof(WLSESSION
));
379 ERR("WL: Could not allocate memory for winlogon instance\n");
380 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED
, 0, 0, NULL
, OptionOk
, &HardErrorResponse
);
383 ZeroMemory(WLSession
, sizeof(WLSESSION
));
384 WLSession
->DialogTimeout
= 120; /* 2 minutes */
386 if (!CreateWindowStationAndDesktops(WLSession
))
388 ERR("WL: Could not create window station and desktops\n");
389 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED
, 0, 0, NULL
, OptionOk
, &HardErrorResponse
);
392 LockWorkstation(WLSession
);
394 if (!StartServicesManager())
396 ERR("WL: Could not start services.exe\n");
397 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED
, 0, 0, NULL
, OptionOk
, &HardErrorResponse
);
403 ERR("WL: Failed to start lsass.exe service (error %lu)\n", GetLastError());
404 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED
, 0, 0, 0, OptionOk
, &HardErrorResponse
);
408 /* Load and initialize gina */
409 if (!GinaInit(WLSession
))
411 ERR("WL: Failed to initialize Gina\n");
412 DialogBoxParam(hAppInstance
, MAKEINTRESOURCE(IDD_GINALOADFAILED
), GetDesktopWindow(), GinaLoadFailedWindowProc
, (LPARAM
)L
"");
413 HandleShutdown(WLSession
, WLX_SAS_ACTION_SHUTDOWN_REBOOT
);
417 DisplayStatusMessage(WLSession
, WLSession
->WinlogonDesktop
, IDS_REACTOSISSTARTINGUP
);
420 /* Wait for the LSA server */
424 /* Connect to NetLogon service (lsass.exe) */
425 /* Real winlogon uses "Winlogon" */
426 RtlInitUnicodeString((PUNICODE_STRING
)&ProcessName
, L
"Winlogon");
427 Status
= LsaRegisterLogonProcess(&ProcessName
, &LsaHandle
, &Mode
);
428 if (Status
== STATUS_PORT_CONNECTION_REFUSED
)
430 /* Add the 'SeTcbPrivilege' privilege and try again */
431 Status
= RtlAdjustPrivilege(SE_TCB_PRIVILEGE
, TRUE
, TRUE
, &Old
);
432 if (!NT_SUCCESS(Status
))
434 ERR("RtlAdjustPrivilege() failed with error %lu\n", LsaNtStatusToWinError(Status
));
437 Status
= LsaRegisterLogonProcess(&ProcessName
, &LsaHandle
, &Mode
);
439 if (!NT_SUCCESS(Status
))
441 ERR("LsaRegisterLogonProcess() failed with error %lu\n", LsaNtStatusToWinError(Status
));
445 RtlInitUnicodeString((PUNICODE_STRING
)&PackageName
, MICROSOFT_KERBEROS_NAME_W
);
446 Status
= LsaLookupAuthenticationPackage(LsaHandle
, &PackageName
, &AuthenticationPackage
);
447 if (!NT_SUCCESS(Status
))
449 ERR("LsaLookupAuthenticationPackage() failed with error %lu\n", LsaNtStatusToWinError(Status
));
450 LsaDeregisterLogonProcess(LsaHandle
);
455 /* Create a hidden window to get SAS notifications */
456 if (!InitializeSAS(WLSession
))
458 ERR("WL: Failed to initialize SAS\n");
462 //DisplayStatusMessage(Session, Session->WinlogonDesktop, IDS_PREPARENETWORKCONNECTIONS);
463 //DisplayStatusMessage(Session, Session->WinlogonDesktop, IDS_APPLYINGCOMPUTERSETTINGS);
465 /* Display logged out screen */
466 WLSession
->LogonStatus
= WKSTA_IS_LOGGED_OFF
;
467 RemoveStatusMessage(WLSession
);
469 /* Check for pending setup */
470 if (GetSetupType() != 0)
472 TRACE("WL: Setup mode detected\n");
474 /* Run setup and reboot when done */
475 SwitchDesktop(WLSession
->ApplicationDesktop
);
479 PostMessageW(WLSession
->SASWindow
, WLX_WM_SAS
, WLX_SAS_TYPE_TIMEOUT
, 0);
481 /* Play logon sound */
482 hThread
= CreateThread(NULL
, 0, PlayLogonSoundThread
, NULL
, 0, NULL
);
485 CloseHandle(hThread
);
488 /* Tell kernel that CurrentControlSet is good (needed
489 * to support Last good known configuration boot) */
490 NtInitializeRegistry(CM_BOOT_FLAG_ACCEPTED
| 1);
492 /* Message loop for the SAS window */
493 while (GetMessageW(&Msg
, WLSession
->SASWindow
, 0, 0))
495 TranslateMessage(&Msg
);
496 DispatchMessageW(&Msg
);
499 /* We never go there */