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 *****************************************************************/
29 IN LPVOID lpParameter
)
32 WCHAR szBuffer
[MAX_PATH
] = {0};
33 WCHAR szDest
[MAX_PATH
];
34 DWORD dwSize
= sizeof(szBuffer
);
36 SERVICE_STATUS_PROCESS Info
;
37 typedef BOOL (WINAPI
*PLAYSOUNDW
)(LPCWSTR
,HMODULE
,DWORD
);
41 if (RegOpenKeyExW(HKEY_CURRENT_USER
, L
"AppEvents\\Schemes\\Apps\\.Default\\WindowsLogon\\.Current", 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
)
46 if (RegQueryValueExW(hKey
, NULL
, NULL
, NULL
, (LPBYTE
)szBuffer
, &dwSize
) != ERROR_SUCCESS
)
59 szBuffer
[MAX_PATH
-1] = L
'\0';
60 if (ExpandEnvironmentStringsW(szBuffer
, szDest
, MAX_PATH
))
62 SC_HANDLE hSCManager
, hService
;
64 hSCManager
= OpenSCManager(NULL
, NULL
, SC_MANAGER_CONNECT
);
68 hService
= OpenServiceW(hSCManager
, L
"wdmaud", GENERIC_READ
);
71 CloseServiceHandle(hSCManager
);
72 TRACE("WL: failed to open sysaudio Status %x\n", GetLastError());
78 if (!QueryServiceStatusEx(hService
, SC_STATUS_PROCESS_INFO
, (LPBYTE
)&Info
, sizeof(SERVICE_STATUS_PROCESS
), &dwSize
))
80 TRACE("WL: QueryServiceStatusEx failed %x\n", GetLastError());
84 if (Info
.dwCurrentState
== SERVICE_RUNNING
)
91 CloseServiceHandle(hService
);
92 CloseServiceHandle(hSCManager
);
94 if (Info
.dwCurrentState
!= SERVICE_RUNNING
)
98 hLibrary
= LoadLibraryW(L
"winmm.dll");
101 Play
= (PLAYSOUNDW
)GetProcAddress(hLibrary
, "PlaySoundW");
104 Play(szDest
, NULL
, SND_FILENAME
);
106 FreeLibrary(hLibrary
);
115 StartServicesManager(VOID
)
117 STARTUPINFOW StartupInfo
;
118 PROCESS_INFORMATION ProcessInformation
;
119 LPCWSTR ServiceString
= L
"services.exe";
122 /* Start the service control manager (services.exe) */
123 ZeroMemory(&StartupInfo
, sizeof(STARTUPINFOW
));
124 StartupInfo
.cb
= sizeof(StartupInfo
);
125 StartupInfo
.lpReserved
= NULL
;
126 StartupInfo
.lpDesktop
= NULL
;
127 StartupInfo
.lpTitle
= NULL
;
128 StartupInfo
.dwFlags
= 0;
129 StartupInfo
.cbReserved2
= 0;
130 StartupInfo
.lpReserved2
= 0;
132 TRACE("WL: Creating new process - %S\n", ServiceString
);
134 res
= CreateProcessW(
144 &ProcessInformation
);
147 ERR("WL: Failed to execute services (error %lu)\n", GetLastError());
151 TRACE("WL: Created new process - %S\n", ServiceString
);
153 CloseHandle(ProcessInformation
.hThread
);
154 CloseHandle(ProcessInformation
.hProcess
);
156 TRACE("WL: StartServicesManager() done.\n");
165 STARTUPINFOW StartupInfo
;
166 PROCESS_INFORMATION ProcessInformation
;
167 LPCWSTR ServiceString
= L
"lsass.exe";
170 /* Start the service control manager (services.exe) */
171 ZeroMemory(&StartupInfo
, sizeof(STARTUPINFOW
));
172 StartupInfo
.cb
= sizeof(StartupInfo
);
173 StartupInfo
.lpReserved
= NULL
;
174 StartupInfo
.lpDesktop
= NULL
;
175 StartupInfo
.lpTitle
= NULL
;
176 StartupInfo
.dwFlags
= 0;
177 StartupInfo
.cbReserved2
= 0;
178 StartupInfo
.lpReserved2
= 0;
180 TRACE("WL: Creating new process - %S\n", ServiceString
);
182 res
= CreateProcessW(
192 &ProcessInformation
);
194 TRACE("WL: Created new process - %S\n", ServiceString
);
196 CloseHandle(ProcessInformation
.hThread
);
197 CloseHandle(ProcessInformation
.hProcess
);
203 DisplayStatusMessage(
204 IN PWLSESSION Session
,
208 WCHAR StatusMsg
[MAX_PATH
];
210 if (Session
->Gina
.Version
< WLX_VERSION_1_3
)
213 if (Session
->SuppressStatus
)
216 if (LoadStringW(hAppInstance
, ResourceId
, StatusMsg
, MAX_PATH
) == 0)
219 return Session
->Gina
.Functions
.WlxDisplayStatusMessage(Session
->Gina
.Context
, hDesktop
, 0, NULL
, StatusMsg
);
224 IN PWLSESSION Session
)
226 if (Session
->Gina
.Version
< WLX_VERSION_1_3
)
229 return Session
->Gina
.Functions
.WlxRemoveStatusMessage(Session
->Gina
.Context
);
232 static INT_PTR CALLBACK
233 GinaLoadFailedWindowProc(
243 switch (LOWORD(wParam
))
246 EndDialog(hwndDlg
, IDOK
);
254 WCHAR templateText
[MAX_PATH
], text
[MAX_PATH
];
256 len
= GetDlgItemTextW(hwndDlg
, IDC_GINALOADFAILED
, templateText
, MAX_PATH
);
259 wsprintfW(text
, templateText
, (LPWSTR
)lParam
);
260 SetDlgItemTextW(hwndDlg
, IDC_GINALOADFAILED
, text
);
262 SetFocus(GetDlgItem(hwndDlg
, IDOK
));
267 EndDialog(hwndDlg
, IDCANCEL
);
277 IN HINSTANCE hInstance
,
278 IN HINSTANCE hPrevInstance
,
283 LSA_STRING ProcessName
, PackageName
;
285 LSA_OPERATIONAL_MODE Mode
;
287 ULONG AuthenticationPackage
;
290 ULONG HardErrorResponse
;
294 UNREFERENCED_PARAMETER(hPrevInstance
);
295 UNREFERENCED_PARAMETER(lpCmdLine
);
296 UNREFERENCED_PARAMETER(nShowCmd
);
298 hAppInstance
= hInstance
;
300 if (!RegisterLogonProcess(GetCurrentProcessId(), TRUE
))
302 ERR("WL: Could not register logon process\n");
303 HandleShutdown(NULL
, WLX_SAS_ACTION_SHUTDOWN_POWER_OFF
);
304 NtShutdownSystem(ShutdownNoReboot
);
308 WLSession
= (PWLSESSION
)HeapAlloc(GetProcessHeap(), 0, sizeof(WLSESSION
));
311 ERR("WL: Could not allocate memory for winlogon instance\n");
312 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED
, 0, 0, NULL
, OptionOk
, &HardErrorResponse
);
315 ZeroMemory(WLSession
, sizeof(WLSESSION
));
316 WLSession
->DialogTimeout
= 120; /* 2 minutes */
318 if (!CreateWindowStationAndDesktops(WLSession
))
320 ERR("WL: Could not create window station and desktops\n");
321 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED
, 0, 0, NULL
, OptionOk
, &HardErrorResponse
);
324 LockWorkstation(WLSession
);
326 if (!StartServicesManager())
328 ERR("WL: Could not start services.exe\n");
329 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED
, 0, 0, NULL
, OptionOk
, &HardErrorResponse
);
335 ERR("WL: Failed to start lsass.exe service (error %lu)\n", GetLastError());
336 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED
, 0, 0, 0, OptionOk
, &HardErrorResponse
);
340 /* Load and initialize gina */
341 if (!GinaInit(WLSession
))
343 ERR("WL: Failed to initialize Gina\n");
344 DialogBoxParam(hAppInstance
, MAKEINTRESOURCE(IDD_GINALOADFAILED
), GetDesktopWindow(), GinaLoadFailedWindowProc
, (LPARAM
)L
"");
345 HandleShutdown(WLSession
, WLX_SAS_ACTION_SHUTDOWN_REBOOT
);
349 DisplayStatusMessage(WLSession
, WLSession
->WinlogonDesktop
, IDS_REACTOSISSTARTINGUP
);
352 /* Connect to NetLogon service (lsass.exe) */
353 /* Real winlogon uses "Winlogon" */
354 RtlInitUnicodeString((PUNICODE_STRING
)&ProcessName
, L
"Winlogon");
355 Status
= LsaRegisterLogonProcess(&ProcessName
, &LsaHandle
, &Mode
);
356 if (Status
== STATUS_PORT_CONNECTION_REFUSED
)
358 /* Add the 'SeTcbPrivilege' privilege and try again */
359 Status
= RtlAdjustPrivilege(SE_TCB_PRIVILEGE
, TRUE
, TRUE
, &Old
);
360 if (!NT_SUCCESS(Status
))
362 ERR("RtlAdjustPrivilege() failed with error %lu\n", LsaNtStatusToWinError(Status
));
365 Status
= LsaRegisterLogonProcess(&ProcessName
, &LsaHandle
, &Mode
);
367 if (!NT_SUCCESS(Status
))
369 ERR("LsaRegisterLogonProcess() failed with error %lu\n", LsaNtStatusToWinError(Status
));
373 RtlInitUnicodeString((PUNICODE_STRING
)&PackageName
, MICROSOFT_KERBEROS_NAME_W
);
374 Status
= LsaLookupAuthenticationPackage(LsaHandle
, &PackageName
, &AuthenticationPackage
);
375 if (!NT_SUCCESS(Status
))
377 ERR("LsaLookupAuthenticationPackage() failed with error %lu\n", LsaNtStatusToWinError(Status
));
378 LsaDeregisterLogonProcess(LsaHandle
);
383 /* Create a hidden window to get SAS notifications */
384 if (!InitializeSAS(WLSession
))
386 ERR("WL: Failed to initialize SAS\n");
390 //DisplayStatusMessage(Session, Session->WinlogonDesktop, IDS_PREPARENETWORKCONNECTIONS);
391 //DisplayStatusMessage(Session, Session->WinlogonDesktop, IDS_APPLYINGCOMPUTERSETTINGS);
393 /* Display logged out screen */
394 WLSession
->LogonStatus
= WKSTA_IS_LOGGED_OFF
;
395 RemoveStatusMessage(WLSession
);
397 /* Check for pending setup */
398 if (GetSetupType() != 0)
400 TRACE("WL: Setup mode detected\n");
402 /* Run setup and reboot when done */
403 SwitchDesktop(WLSession
->ApplicationDesktop
);
407 PostMessageW(WLSession
->SASWindow
, WLX_WM_SAS
, WLX_SAS_TYPE_TIMEOUT
, 0);
409 /* Play logon sound */
410 hThread
= CreateThread(NULL
, 0, PlayLogonSoundThread
, NULL
, 0, NULL
);
413 CloseHandle(hThread
);
416 /* Tell kernel that CurrentControlSet is good (needed
417 * to support Last good known configuration boot) */
418 NtInitializeRegistry(CM_BOOT_FLAG_ACCEPTED
);
420 /* Message loop for the SAS window */
421 while (GetMessageW(&Msg
, WLSession
->SASWindow
, 0, 0))
423 TranslateMessage(&Msg
);
424 DispatchMessageW(&Msg
);
427 /* We never go there */