Sync with trunk (r48545)
[reactos.git] / base / system / winlogon / winlogon.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Winlogon
4 * FILE: base/system/winlogon/winlogon.c
5 * PURPOSE: Logon
6 * PROGRAMMERS: Thomas Weidenmueller (w3seek@users.sourceforge.net)
7 * Filip Navara
8 * Hervé Poussineau (hpoussin@reactos.org)
9 */
10
11 /* INCLUDES *****************************************************************/
12
13 #include "winlogon.h"
14
15 #include <wine/debug.h>
16
17 WINE_DEFAULT_DEBUG_CHANNEL(winlogon);
18
19 /* GLOBALS ******************************************************************/
20
21 HINSTANCE hAppInstance;
22 PWLSESSION WLSession = NULL;
23
24 /* FUNCTIONS *****************************************************************/
25
26 DWORD
27 WINAPI
28 PlayLogonSoundThread(
29 IN LPVOID lpParameter)
30 {
31 HKEY hKey;
32 WCHAR szBuffer[MAX_PATH] = {0};
33 WCHAR szDest[MAX_PATH];
34 DWORD dwSize = sizeof(szBuffer);
35 HMODULE hLibrary;
36 SERVICE_STATUS_PROCESS Info;
37 typedef BOOL (WINAPI *PLAYSOUNDW)(LPCWSTR,HMODULE,DWORD);
38 PLAYSOUNDW Play;
39 ULONG Index = 0;
40
41 if (RegOpenKeyExW(HKEY_CURRENT_USER, L"AppEvents\\Schemes\\Apps\\.Default\\WindowsLogon\\.Current", 0, KEY_READ, &hKey) != ERROR_SUCCESS)
42 {
43 ExitThread(0);
44 }
45
46 if (RegQueryValueExW(hKey, NULL, NULL, NULL, (LPBYTE)szBuffer, &dwSize) != ERROR_SUCCESS)
47 {
48 RegCloseKey(hKey);
49 ExitThread(0);
50 }
51
52
53 RegCloseKey(hKey);
54
55 if (!szBuffer[0])
56 ExitThread(0);
57
58
59 szBuffer[MAX_PATH-1] = L'\0';
60 if (ExpandEnvironmentStringsW(szBuffer, szDest, MAX_PATH))
61 {
62 SC_HANDLE hSCManager, hService;
63
64 hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
65 if (!hSCManager)
66 ExitThread(0);;
67
68 hService = OpenServiceW(hSCManager, L"wdmaud", GENERIC_READ);
69 if (!hService)
70 {
71 CloseServiceHandle(hSCManager);
72 TRACE("WL: failed to open sysaudio Status %x\n", GetLastError());
73 ExitThread(0);
74 }
75
76 do
77 {
78 if (!QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, (LPBYTE)&Info, sizeof(SERVICE_STATUS_PROCESS), &dwSize))
79 {
80 TRACE("WL: QueryServiceStatusEx failed %x\n", GetLastError());
81 break;
82 }
83
84 if (Info.dwCurrentState == SERVICE_RUNNING)
85 break;
86
87 Sleep(1000);
88
89 }while(Index++ < 20);
90
91 CloseServiceHandle(hService);
92 CloseServiceHandle(hSCManager);
93
94 if (Info.dwCurrentState != SERVICE_RUNNING)
95 ExitThread(0);
96
97
98 hLibrary = LoadLibraryW(L"winmm.dll");
99 if (hLibrary)
100 {
101 Play = (PLAYSOUNDW)GetProcAddress(hLibrary, "PlaySoundW");
102 if (Play)
103 {
104 Play(szDest, NULL, SND_FILENAME);
105 }
106 FreeLibrary(hLibrary);
107 }
108 }
109 ExitThread(0);
110 }
111
112
113
114 static BOOL
115 StartServicesManager(VOID)
116 {
117 STARTUPINFOW StartupInfo;
118 PROCESS_INFORMATION ProcessInformation;
119 LPCWSTR ServiceString = L"services.exe";
120 BOOL res;
121
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;
131
132 TRACE("WL: Creating new process - %S\n", ServiceString);
133
134 res = CreateProcessW(
135 ServiceString,
136 NULL,
137 NULL,
138 NULL,
139 FALSE,
140 DETACHED_PROCESS,
141 NULL,
142 NULL,
143 &StartupInfo,
144 &ProcessInformation);
145 if (!res)
146 {
147 ERR("WL: Failed to execute services (error %lu)\n", GetLastError());
148 return FALSE;
149 }
150
151 TRACE("WL: Created new process - %S\n", ServiceString);
152
153 CloseHandle(ProcessInformation.hThread);
154 CloseHandle(ProcessInformation.hProcess);
155
156 TRACE("WL: StartServicesManager() done.\n");
157
158 return TRUE;
159 }
160
161
162 static BOOL
163 StartLsass(VOID)
164 {
165 STARTUPINFOW StartupInfo;
166 PROCESS_INFORMATION ProcessInformation;
167 LPCWSTR ServiceString = L"lsass.exe";
168 BOOL res;
169
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;
179
180 TRACE("WL: Creating new process - %S\n", ServiceString);
181
182 res = CreateProcessW(
183 ServiceString,
184 NULL,
185 NULL,
186 NULL,
187 FALSE,
188 DETACHED_PROCESS,
189 NULL,
190 NULL,
191 &StartupInfo,
192 &ProcessInformation);
193
194 TRACE("WL: Created new process - %S\n", ServiceString);
195
196 CloseHandle(ProcessInformation.hThread);
197 CloseHandle(ProcessInformation.hProcess);
198
199 return res;
200 }
201
202 BOOL
203 DisplayStatusMessage(
204 IN PWLSESSION Session,
205 IN HDESK hDesktop,
206 IN UINT ResourceId)
207 {
208 WCHAR StatusMsg[MAX_PATH];
209
210 if (Session->Gina.Version < WLX_VERSION_1_3)
211 return TRUE;
212
213 if (Session->SuppressStatus)
214 return TRUE;
215
216 if (LoadStringW(hAppInstance, ResourceId, StatusMsg, MAX_PATH) == 0)
217 return FALSE;
218
219 return Session->Gina.Functions.WlxDisplayStatusMessage(Session->Gina.Context, hDesktop, 0, NULL, StatusMsg);
220 }
221
222 BOOL
223 RemoveStatusMessage(
224 IN PWLSESSION Session)
225 {
226 if (Session->Gina.Version < WLX_VERSION_1_3)
227 return TRUE;
228
229 return Session->Gina.Functions.WlxRemoveStatusMessage(Session->Gina.Context);
230 }
231
232 static INT_PTR CALLBACK
233 GinaLoadFailedWindowProc(
234 IN HWND hwndDlg,
235 IN UINT uMsg,
236 IN WPARAM wParam,
237 IN LPARAM lParam)
238 {
239 switch (uMsg)
240 {
241 case WM_COMMAND:
242 {
243 switch (LOWORD(wParam))
244 {
245 case IDOK:
246 EndDialog(hwndDlg, IDOK);
247 return TRUE;
248 }
249 break;
250 }
251 case WM_INITDIALOG:
252 {
253 int len;
254 WCHAR templateText[MAX_PATH], text[MAX_PATH];
255
256 len = GetDlgItemTextW(hwndDlg, IDC_GINALOADFAILED, templateText, MAX_PATH);
257 if (len)
258 {
259 wsprintfW(text, templateText, (LPWSTR)lParam);
260 SetDlgItemTextW(hwndDlg, IDC_GINALOADFAILED, text);
261 }
262 SetFocus(GetDlgItem(hwndDlg, IDOK));
263 return TRUE;
264 }
265 case WM_CLOSE:
266 {
267 EndDialog(hwndDlg, IDCANCEL);
268 return TRUE;
269 }
270 }
271
272 return FALSE;
273 }
274
275 int WINAPI
276 WinMain(
277 IN HINSTANCE hInstance,
278 IN HINSTANCE hPrevInstance,
279 IN LPSTR lpCmdLine,
280 IN int nShowCmd)
281 {
282 #if 0
283 LSA_STRING ProcessName, PackageName;
284 HANDLE LsaHandle;
285 LSA_OPERATIONAL_MODE Mode;
286 BOOLEAN Old;
287 ULONG AuthenticationPackage;
288 NTSTATUS Status;
289 #endif
290 ULONG HardErrorResponse;
291 MSG Msg;
292 HANDLE hThread;
293
294 UNREFERENCED_PARAMETER(hPrevInstance);
295 UNREFERENCED_PARAMETER(lpCmdLine);
296 UNREFERENCED_PARAMETER(nShowCmd);
297
298 hAppInstance = hInstance;
299
300 if (!RegisterLogonProcess(GetCurrentProcessId(), TRUE))
301 {
302 ERR("WL: Could not register logon process\n");
303 HandleShutdown(NULL, WLX_SAS_ACTION_SHUTDOWN_POWER_OFF);
304 NtShutdownSystem(ShutdownNoReboot);
305 ExitProcess(0);
306 }
307
308 WLSession = (PWLSESSION)HeapAlloc(GetProcessHeap(), 0, sizeof(WLSESSION));
309 if (!WLSession)
310 {
311 ERR("WL: Could not allocate memory for winlogon instance\n");
312 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED, 0, 0, NULL, OptionOk, &HardErrorResponse);
313 ExitProcess(1);
314 }
315 ZeroMemory(WLSession, sizeof(WLSESSION));
316 WLSession->DialogTimeout = 120; /* 2 minutes */
317
318 if (!CreateWindowStationAndDesktops(WLSession))
319 {
320 ERR("WL: Could not create window station and desktops\n");
321 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED, 0, 0, NULL, OptionOk, &HardErrorResponse);
322 ExitProcess(1);
323 }
324 LockWorkstation(WLSession);
325
326 if (!StartServicesManager())
327 {
328 ERR("WL: Could not start services.exe\n");
329 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED, 0, 0, NULL, OptionOk, &HardErrorResponse);
330 ExitProcess(1);
331 }
332
333 if (!StartLsass())
334 {
335 ERR("WL: Failed to start lsass.exe service (error %lu)\n", GetLastError());
336 NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED, 0, 0, 0, OptionOk, &HardErrorResponse);
337 ExitProcess(1);
338 }
339
340 /* Load and initialize gina */
341 if (!GinaInit(WLSession))
342 {
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);
346 ExitProcess(1);
347 }
348
349 DisplayStatusMessage(WLSession, WLSession->WinlogonDesktop, IDS_REACTOSISSTARTINGUP);
350
351 #if 0
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)
357 {
358 /* Add the 'SeTcbPrivilege' privilege and try again */
359 Status = RtlAdjustPrivilege(SE_TCB_PRIVILEGE, TRUE, TRUE, &Old);
360 if (!NT_SUCCESS(Status))
361 {
362 ERR("RtlAdjustPrivilege() failed with error %lu\n", LsaNtStatusToWinError(Status));
363 return 1;
364 }
365 Status = LsaRegisterLogonProcess(&ProcessName, &LsaHandle, &Mode);
366 }
367 if (!NT_SUCCESS(Status))
368 {
369 ERR("LsaRegisterLogonProcess() failed with error %lu\n", LsaNtStatusToWinError(Status));
370 return 1;
371 }
372
373 RtlInitUnicodeString((PUNICODE_STRING)&PackageName, MICROSOFT_KERBEROS_NAME_W);
374 Status = LsaLookupAuthenticationPackage(LsaHandle, &PackageName, &AuthenticationPackage);
375 if (!NT_SUCCESS(Status))
376 {
377 ERR("LsaLookupAuthenticationPackage() failed with error %lu\n", LsaNtStatusToWinError(Status));
378 LsaDeregisterLogonProcess(LsaHandle);
379 return 1;
380 }
381 #endif
382
383 /* Create a hidden window to get SAS notifications */
384 if (!InitializeSAS(WLSession))
385 {
386 ERR("WL: Failed to initialize SAS\n");
387 ExitProcess(2);
388 }
389
390 //DisplayStatusMessage(Session, Session->WinlogonDesktop, IDS_PREPARENETWORKCONNECTIONS);
391 //DisplayStatusMessage(Session, Session->WinlogonDesktop, IDS_APPLYINGCOMPUTERSETTINGS);
392
393 /* Display logged out screen */
394 WLSession->LogonStatus = WKSTA_IS_LOGGED_OFF;
395 RemoveStatusMessage(WLSession);
396
397 /* Check for pending setup */
398 if (GetSetupType() != 0)
399 {
400 TRACE("WL: Setup mode detected\n");
401
402 /* Run setup and reboot when done */
403 SwitchDesktop(WLSession->ApplicationDesktop);
404 RunSetup();
405 }
406 else
407 PostMessageW(WLSession->SASWindow, WLX_WM_SAS, WLX_SAS_TYPE_TIMEOUT, 0);
408
409 /* Play logon sound */
410 hThread = CreateThread(NULL, 0, PlayLogonSoundThread, NULL, 0, NULL);
411 if (hThread)
412 {
413 CloseHandle(hThread);
414 }
415
416 /* Tell kernel that CurrentControlSet is good (needed
417 * to support Last good known configuration boot) */
418 NtInitializeRegistry(CM_BOOT_FLAG_ACCEPTED | 1);
419
420 /* Message loop for the SAS window */
421 while (GetMessageW(&Msg, WLSession->SASWindow, 0, 0))
422 {
423 TranslateMessage(&Msg);
424 DispatchMessageW(&Msg);
425 }
426
427 /* We never go there */
428
429 return 0;
430 }