[WINLOGON]
[reactos.git] / reactos / base / system / winlogon / winlogon.c
index b4f7d6b..3329848 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS Winlogon
- * FILE:            services/winlogon/winlogon.c
+ * FILE:            base/system/winlogon/winlogon.c
  * PURPOSE:         Logon
  * PROGRAMMERS:     Thomas Weidenmueller (w3seek@users.sourceforge.net)
  *                  Filip Navara
@@ -9,11 +9,13 @@
  */
 
 /* INCLUDES *****************************************************************/
+
 #include "winlogon.h"
 
-#define YDEBUG
 #include <wine/debug.h>
 
+WINE_DEFAULT_DEBUG_CHANNEL(winlogon);
+
 /* GLOBALS ******************************************************************/
 
 HINSTANCE hAppInstance;
@@ -21,414 +23,183 @@ PWLSESSION WLSession = NULL;
 
 /* FUNCTIONS *****************************************************************/
 
-static INT_PTR CALLBACK
-ShutdownComputerWindowProc(
-       IN HWND hwndDlg,
-       IN UINT uMsg,
-       IN WPARAM wParam,
-       IN LPARAM lParam)
+DWORD
+WINAPI
+PlayLogonSoundThread(
+       IN LPVOID lpParameter)
 {
-       switch (uMsg)
+       HKEY hKey;
+       WCHAR szBuffer[MAX_PATH] = {0};
+       WCHAR szDest[MAX_PATH];
+       DWORD dwSize = sizeof(szBuffer);
+       HMODULE hLibrary;
+       SERVICE_STATUS_PROCESS Info;
+       typedef BOOL (WINAPI *PLAYSOUNDW)(LPCWSTR,HMODULE,DWORD);
+       PLAYSOUNDW Play;
+       ULONG Index = 0;
+
+       if (RegOpenKeyExW(HKEY_CURRENT_USER, L"AppEvents\\Schemes\\Apps\\.Default\\WindowsLogon\\.Current", 0, KEY_READ, &hKey) != ERROR_SUCCESS)
        {
-               case WM_COMMAND:
-               {
-                       switch (LOWORD(wParam))
-                       {
-                               case IDC_BTNSHTDOWNCOMPUTER:
-                                       EndDialog(hwndDlg, IDC_BTNSHTDOWNCOMPUTER);
-                                       break;
-                       }
-                       break;
-               }
-               case WM_INITDIALOG:
-               {
-                       RemoveMenu(GetSystemMenu(hwndDlg, FALSE), SC_CLOSE, MF_BYCOMMAND);
-                       SetFocus(GetDlgItem(hwndDlg, IDC_BTNSHTDOWNCOMPUTER));
-                       break;
-               }
+               ExitThread(0);
        }
-       return DefWindowProc(hwndDlg, uMsg, wParam, lParam);
-}
 
-static BOOL
-StartServicesManager(VOID)
-{
-   HANDLE ServicesInitEvent;
-   BOOLEAN Result;
-   STARTUPINFO StartupInfo;
-   PROCESS_INFORMATION ProcessInformation;
-   DWORD Count;
-   WCHAR ServiceString[] = L"services.exe";
-
-   /* Start the service control manager (services.exe) */
-
-   StartupInfo.cb = sizeof(StartupInfo);
-   StartupInfo.lpReserved = NULL;
-   StartupInfo.lpDesktop = NULL;
-   StartupInfo.lpTitle = NULL;
-   StartupInfo.dwFlags = 0;
-   StartupInfo.cbReserved2 = 0;
-   StartupInfo.lpReserved2 = 0;
+       if (RegQueryValueExW(hKey, NULL, NULL, NULL, (LPBYTE)szBuffer, &dwSize) != ERROR_SUCCESS)
+       {
+               RegCloseKey(hKey);
+               ExitThread(0);
+       }
 
-#if 0
-   DPRINT1(L"WL: Creating new process - \"services.exe\".\n");
-#endif
 
-   Result = CreateProcess(NULL,
-                          ServiceString,
-                          NULL,
-                          NULL,
-                          FALSE,
-                          DETACHED_PROCESS,
-                          NULL,
-                          NULL,
-                          &StartupInfo,
-                          &ProcessInformation);
-   if (!Result)
-     {
-        DPRINT1("WL: Failed to execute services\n");
-        return FALSE;
-     }
-
-   /* wait for event creation (by SCM) for max. 20 seconds */
-   for (Count = 0; Count < 20; Count++)
-     {
-        Sleep(1000);
-
-        DPRINT("WL: Attempting to open event \"SvcctrlStartEvent_A3725DX\"\n");
-        ServicesInitEvent = OpenEvent(EVENT_ALL_ACCESS, //SYNCHRONIZE,
-                                      FALSE,
-                                      L"SvcctrlStartEvent_A3725DX");
-        if (ServicesInitEvent != NULL)
-          {
-             break;
-          }
-     }
-
-   if (ServicesInitEvent == NULL)
-     {
-        DPRINT1("WL: Failed to open event \"SvcctrlStartEvent_A3725DX\"\n");
-        return FALSE;
-     }
-
-   /* wait for event signalization */
-   DPRINT("WL: Waiting forever on event handle: %x\n", ServicesInitEvent);
-   WaitForSingleObject(ServicesInitEvent, INFINITE);
-   DPRINT("WL: Closing event object \"SvcctrlStartEvent_A3725DX\"\n");
-   CloseHandle(ServicesInitEvent);
-   DPRINT("WL: StartServicesManager() Done.\n");
-
-   return TRUE;
-}
+       RegCloseKey(hKey);
 
-static BOOL
-StartCustomService(
-       IN LPCWSTR ServiceName)
-{
-       SC_HANDLE hSCManager = NULL;
-       SC_HANDLE hService = NULL;
-       BOOL ret = FALSE;
+       if (!szBuffer[0])
+               ExitThread(0);
 
-       hSCManager = OpenSCManager(NULL, NULL, 0);
-       if (!hSCManager)
-               goto cleanup;
 
-       hService = OpenService(hSCManager, ServiceName, SERVICE_START);
-       if (!hService)
-               goto cleanup;
-#if 0
-       if (!StartService(hService, 0, NULL))
-               goto cleanup;
-#endif
+       szBuffer[MAX_PATH-1] = L'\0';
+       if (ExpandEnvironmentStringsW(szBuffer, szDest, MAX_PATH))
+       {
+               SC_HANDLE hSCManager, hService;
 
-       ret = TRUE;
+               hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
+               if (!hSCManager)
+                       ExitThread(0);;
+
+               hService = OpenServiceW(hSCManager, L"wdmaud", GENERIC_READ);
+               if (!hService)
+               {
+                       CloseServiceHandle(hSCManager);
+                       TRACE("WL: failed to open sysaudio Status %x\n", GetLastError());
+                       ExitThread(0);
+               }
+
+               do
+               {
+                       if (!QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, (LPBYTE)&Info, sizeof(SERVICE_STATUS_PROCESS), &dwSize))
+                       {
+                               TRACE("WL: QueryServiceStatusEx failed %x\n", GetLastError());
+                               break;
+                       }
+
+                       if (Info.dwCurrentState == SERVICE_RUNNING)
+                               break;
+
+                       Sleep(1000);
+
+               }while(Index++ < 20);
 
-cleanup:
-       if (hService)
                CloseServiceHandle(hService);
-       if (hSCManager)
                CloseServiceHandle(hSCManager);
-       return ret;
+
+               if (Info.dwCurrentState != SERVICE_RUNNING)
+                       ExitThread(0);
+
+
+               hLibrary = LoadLibraryW(L"winmm.dll");
+               if (hLibrary)
+               {
+                       Play = (PLAYSOUNDW)GetProcAddress(hLibrary, "PlaySoundW");
+                       if (Play)
+                       {
+                               Play(szDest, NULL, SND_FILENAME);
+                       }
+                       FreeLibrary(hLibrary);
+               }
+       }
+       ExitThread(0);
 }
 
+
+
 static BOOL
-StartLsass(VOID)
+StartServicesManager(VOID)
 {
-       HANDLE LsassInitEvent;
-
-       LsassInitEvent = CreateEvent(
+       STARTUPINFOW StartupInfo;
+       PROCESS_INFORMATION ProcessInformation;
+       LPCWSTR ServiceString = L"services.exe";
+       BOOL res;
+
+       /* Start the service control manager (services.exe) */
+       ZeroMemory(&StartupInfo, sizeof(STARTUPINFOW));
+       StartupInfo.cb = sizeof(StartupInfo);
+       StartupInfo.lpReserved = NULL;
+       StartupInfo.lpDesktop = NULL;
+       StartupInfo.lpTitle = NULL;
+       StartupInfo.dwFlags = 0;
+       StartupInfo.cbReserved2 = 0;
+       StartupInfo.lpReserved2 = 0;
+
+       TRACE("WL: Creating new process - %S\n", ServiceString);
+
+       res = CreateProcessW(
+               ServiceString,
+               NULL,
+               NULL,
                NULL,
-               TRUE,
                FALSE,
-               L"Global\\SECURITY_SERVICES_STARTED");
-       if (!LsassInitEvent)
+               DETACHED_PROCESS,
+               NULL,
+               NULL,
+               &StartupInfo,
+               &ProcessInformation);
+       if (!res)
        {
-               ERR("WL: Failed to create lsass notification event (error %lu)\n", GetLastError());
+               ERR("WL: Failed to execute services (error %lu)\n", GetLastError());
                return FALSE;
        }
 
-       /* Start the local security authority subsystem (Netlogon service) */
-       if (!StartCustomService(L"Netlogon"))
-       {
-               ERR("WL: Failed to start NetLogon service (error %lu)\n", GetLastError());
-               return FALSE;
-       }
+       TRACE("WL: Created new process - %S\n", ServiceString);
 
-#if 0
-       WaitForSingleObject(LsassInitEvent, INFINITE);
-#endif
-       CloseHandle(LsassInitEvent);
+       CloseHandle(ProcessInformation.hThread);
+       CloseHandle(ProcessInformation.hProcess);
+
+       TRACE("WL: StartServicesManager() done.\n");
 
        return TRUE;
 }
 
-#if 0
-static BOOL
-OpenRegistryKey(
-       OUT HKEY *WinLogonKey)
-{
-   return ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,
-                                        L"SOFTWARE\\ReactOS\\Windows NT\\CurrentVersion\\WinLogon",
-                                        0,
-                                        KEY_QUERY_VALUE,
-                                        WinLogonKey);
-}
-#endif
 
-#if 0
 static BOOL
-StartProcess(
-       IN PWCHAR ValueName)
+StartLsass(VOID)
 {
-   BOOL StartIt;
-   HKEY WinLogonKey;
-   DWORD Type;
-   DWORD Size;
-   DWORD StartValue;
-
-   StartIt = TRUE;
-   if (OpenRegistryKey(&WinLogonKey))
-     {
-       Size = sizeof(DWORD);
-       if (ERROR_SUCCESS == RegQueryValueEx(WinLogonKey,
-                                             ValueName,
-                                            NULL,
-                                            &Type,
-                                            (LPBYTE) &StartValue,
-                                             &Size))
-          {
-          if (REG_DWORD == Type)
-            {
-               StartIt = (0 != StartValue);
-            }
-          }
-       RegCloseKey(WinLogonKey);
-     }
-
-   return StartIt;
-}
-#endif
+       STARTUPINFOW StartupInfo;
+       PROCESS_INFORMATION ProcessInformation;
+       LPCWSTR ServiceString = L"lsass.exe";
+       BOOL res;
+
+       /* Start the service control manager (services.exe) */
+       ZeroMemory(&StartupInfo, sizeof(STARTUPINFOW));
+       StartupInfo.cb = sizeof(StartupInfo);
+       StartupInfo.lpReserved = NULL;
+       StartupInfo.lpDesktop = NULL;
+       StartupInfo.lpTitle = NULL;
+       StartupInfo.dwFlags = 0;
+       StartupInfo.cbReserved2 = 0;
+       StartupInfo.lpReserved2 = 0;
+
+       TRACE("WL: Creating new process - %S\n", ServiceString);
+
+       res = CreateProcessW(
+               ServiceString,
+               NULL,
+               NULL,
+               NULL,
+               FALSE,
+               DETACHED_PROCESS,
+               NULL,
+               NULL,
+               &StartupInfo,
+               &ProcessInformation);
 
-/*
-static BOOL RestartShell(
-       IN OUT PWLSESSION Session)
-{
-  HKEY WinLogonKey;
-  DWORD Type, Size, Value;
-
-  if(OpenRegistryKey(&WinLogonKey))
-  {
-    Size = sizeof(DWORD);
-    if(ERROR_SUCCESS == RegQueryValueEx(WinLogonKey,
-                                        L"AutoRestartShell",
-                                        NULL,
-                                        &Type,
-                                        (LPBYTE)&Value,
-                                        &Size))
-    {
-      if(Type == REG_DWORD)
-      {
-        RegCloseKey(WinLogonKey);
-        return (Value != 0);
-      }
-    }
-    RegCloseKey(WinLogonKey);
-  }
-  return FALSE;
-}
-*/
+       TRACE("WL: Created new process - %S\n", ServiceString);
 
-#if 0
-static PWCHAR
-GetUserInit(
-       OUT WCHAR *CommandLine,
-       IN DWORD BufferLength)
-{
-   HKEY WinLogonKey;
-   BOOL GotCommandLine;
-   DWORD Type;
-   DWORD Size;
-   WCHAR Shell[_MAX_PATH];
-
-   GotCommandLine = FALSE;
-   if (OpenRegistryKey(&WinLogonKey))
-     {
-       Size = MAX_PATH;
-       if (ERROR_SUCCESS == RegQueryValueEx(WinLogonKey,
-                                         L"UserInit",
-                                            NULL,
-                                            &Type,
-                                            (LPBYTE) Shell,
-                                         &Size))
-          {
-          if (REG_EXPAND_SZ == Type)
-            {
-               ExpandEnvironmentStrings(Shell, CommandLine, _MAX_PATH);
-               GotCommandLine = TRUE;
-            }
-          else if (REG_SZ == Type)
-            {
-               wcscpy(CommandLine, Shell);
-               GotCommandLine = TRUE;
-            }
-          }
-       RegCloseKey(WinLogonKey);
-     }
-
-   if (! GotCommandLine)
-     {
-       GetSystemDirectory(CommandLine, MAX_PATH - 15);
-       wcscat(CommandLine, L"\\userinit.exe");
-     }
-
-   return CommandLine;
-}
+       CloseHandle(ProcessInformation.hThread);
+       CloseHandle(ProcessInformation.hProcess);
 
-static BOOL
-DoBrokenLogonUser(
-       IN PWLSESSION WLSession,
-       IN PWLX_MPR_NOTIFY_INFO pMprNotifyInfo)
-{
-  PROCESS_INFORMATION ProcessInformation;
-  STARTUPINFO StartupInfo;
-  WCHAR CommandLine[MAX_PATH];
-  WCHAR CurrentDirectory[MAX_PATH];
-  PROFILEINFOW ProfileInfo;
-  BOOL Result;
-  LPVOID lpEnvironment = NULL;
-  MSG Msg;
-  BOOLEAN Old;
-
-  SwitchDesktop(WLSession->ApplicationDesktop);
-
-  /* Load the user profile */
-  ProfileInfo.dwSize = sizeof(PROFILEINFOW);
-  ProfileInfo.dwFlags = 0;
-  ProfileInfo.lpUserName = pMprNotifyInfo->pszUserName;
-  ProfileInfo.lpProfilePath = NULL;
-  ProfileInfo.lpDefaultPath = NULL;
-  ProfileInfo.lpServerName = NULL;
-  ProfileInfo.lpPolicyPath = NULL;
-  ProfileInfo.hProfile = NULL;
-
-  if (!LoadUserProfileW (WLSession->UserToken,
-                        &ProfileInfo))
-    {
-      DPRINT1 ("WL: LoadUserProfileW() failed\n");
-      CloseHandle (WLSession->UserToken);
-      RtlDestroyEnvironment (lpEnvironment);
-      return FALSE;
-    }
-
-  if (!CreateEnvironmentBlock (&lpEnvironment,
-                              WLSession->UserToken,
-                              TRUE))
-    {
-      DPRINT1("WL: CreateEnvironmentBlock() failed\n");
-      return FALSE;
-    }
-
-  if (ImpersonateLoggedOnUser(WLSession->UserToken))
-    {
-      UpdatePerUserSystemParameters(0, TRUE);
-      RevertToSelf();
-    }
-
-  GetWindowsDirectoryW (CurrentDirectory, MAX_PATH);
-
-  StartupInfo.cb = sizeof(StartupInfo);
-  StartupInfo.lpReserved = NULL;
-  StartupInfo.lpDesktop = NULL;
-  StartupInfo.lpTitle = NULL;
-  StartupInfo.dwFlags = 0;
-  StartupInfo.cbReserved2 = 0;
-  StartupInfo.lpReserved2 = 0;
-  
-  /* Get privilege */
-  RtlAdjustPrivilege(SE_ASSIGNPRIMARYTOKEN_PRIVILEGE, TRUE, FALSE, &Old);
-
-  Result = CreateProcessAsUserW(
-                                WLSession->UserToken,
-                                NULL,
-                                GetUserInit (CommandLine, MAX_PATH),
-                                NULL,
-                                NULL,
-                                FALSE,
-                                CREATE_UNICODE_ENVIRONMENT,
-                                lpEnvironment,
-                                CurrentDirectory,
-                                &StartupInfo,
-                                &ProcessInformation);
-  if (!Result)
-    {
-      DPRINT1("WL: Failed to execute user shell %ws\n", CommandLine);
-      if (ImpersonateLoggedOnUser(WLSession->UserToken))
-        {
-          UpdatePerUserSystemParameters(0, FALSE);
-          RevertToSelf();
-        }
-      UnloadUserProfile (WLSession->UserToken,
-                        ProfileInfo.hProfile);
-      CloseHandle (WLSession->UserToken);
-      DestroyEnvironmentBlock (lpEnvironment);
-      return FALSE;
-    }
-  /*WLSession->MsGina.Functions.WlxActivateUserShell(WLSession->MsGina.Context,
-                                                   L"WinSta0\\Default",
-                                                   NULL,
-                                                   NULL);*/
-
-  while (WaitForSingleObject (ProcessInformation.hProcess, 100) != WAIT_OBJECT_0)
-  {
-    if (PeekMessage(&Msg, WLSession->SASWindow, 0, 0, PM_REMOVE))
-    {
-      TranslateMessage(&Msg);
-      DispatchMessage(&Msg);
-    }
-  }
-
-  CloseHandle (ProcessInformation.hProcess);
-  CloseHandle (ProcessInformation.hThread);
-
-  if (ImpersonateLoggedOnUser(WLSession->UserToken))
-    {
-      UpdatePerUserSystemParameters(0, FALSE);
-      RevertToSelf();
-    }
-
-  /* Unload user profile */
-  UnloadUserProfile (WLSession->UserToken,
-                    ProfileInfo.hProfile);
-
-  CloseHandle (WLSession->UserToken);
-
-  RtlDestroyEnvironment (lpEnvironment);
-
-  return TRUE;
+       return res;
 }
-#endif
 
-static BOOL
+BOOL
 DisplayStatusMessage(
        IN PWLSESSION Session,
        IN HDESK hDesktop,
@@ -436,75 +207,69 @@ DisplayStatusMessage(
 {
        WCHAR StatusMsg[MAX_PATH];
 
+       if (Session->Gina.Version < WLX_VERSION_1_3)
+               return TRUE;
+
        if (Session->SuppressStatus)
                return TRUE;
 
-       if (LoadString(hAppInstance, ResourceId, StatusMsg, MAX_PATH) == 0)
+       if (LoadStringW(hAppInstance, ResourceId, StatusMsg, MAX_PATH) == 0)
                return FALSE;
 
-       return Session->MsGina.Functions.WlxDisplayStatusMessage(Session->MsGina.Context, hDesktop, 0, NULL, StatusMsg);
+       return Session->Gina.Functions.WlxDisplayStatusMessage(Session->Gina.Context, hDesktop, 0, NULL, StatusMsg);
 }
 
-static VOID
-SessionLoop(
-       IN OUT PWLSESSION Session)
+BOOL
+RemoveStatusMessage(
+       IN PWLSESSION Session)
 {
-       //WCHAR StatusMsg[256];
-       //HANDLE hShutdownEvent;
-       MSG Msg;
+       if (Session->Gina.Version < WLX_VERSION_1_3)
+               return TRUE;
 
-       Session->LogonStatus = WKSTA_IS_LOGGED_OFF;
-       RemoveStatusMessage(Session);
-       DispatchSAS(Session, WLX_SAS_TYPE_TIMEOUT);
+       return Session->Gina.Functions.WlxRemoveStatusMessage(Session->Gina.Context);
+}
 
-       /* Message loop for the SAS window */
-       while (GetMessage(&Msg, WLSession->SASWindow, 0, 0))
+static INT_PTR CALLBACK
+GinaLoadFailedWindowProc(
+       IN HWND hwndDlg,
+       IN UINT uMsg,
+       IN WPARAM wParam,
+       IN LPARAM lParam)
+{
+       switch (uMsg)
        {
-               TranslateMessage(&Msg);
-               DispatchMessage(&Msg);
-       }
-
-       /* Don't go there! */
-
-   /*
-   DisplayStatusMessage(Session, Session->WinlogonDesktop, IDS_PREPARENETWORKCONNECTIONS);
-   Sleep(150);
-
-   DisplayStatusMessage(Session, Session->WinlogonDesktop, IDS_APPLYINGCOMPUTERSETTINGS);
-   Sleep(150);
-
-   DisplayStatusMessage(Session, Session->WinlogonDesktop, IDS_LOADINGYOURPERSONALSETTINGS);
-   Sleep(150);
-
-   DisplayStatusMessage(Session, Session->WinlogonDesktop, IDS_APPLYINGYOURPERSONALSETTINGS);
-   Sleep(150);
-
-   RemoveStatusMessage(Session);
-
-   if(!MsGinaInst->Functions->WlxActivateUserShell(MsGinaInst->Context,
-                                                   L"WinSta0\\Default",
-                                                   NULL,
-                                                   NULL))
-   {
-     LoadString(hAppInstance, IDS_FAILEDACTIVATEUSERSHELL, StatusMsg, 256 * sizeof(WCHAR));
-     MessageBox(0, StatusMsg, NULL, MB_ICONERROR);
-     SetEvent(hShutdownEvent);
-   }
-
-   WaitForSingleObject(hShutdownEvent, INFINITE);
-   CloseHandle(hShutdownEvent);
-
-   DisplayStatusMessage(Session, Session->WinlogonDesktop, IDS_SAVEYOURSETTINGS);
-
-   Sleep(150);
-
-   MsGinaInst->Functions->WlxShutdown(MsGinaInst->Context, WLX_SAS_ACTION_SHUTDOWN);
-   DisplayStatusMessage(Session, Session->WinlogonDesktop, IDS_REACTOSISSHUTTINGDOWN);
+               case WM_COMMAND:
+               {
+                       switch (LOWORD(wParam))
+                       {
+                               case IDOK:
+                                       EndDialog(hwndDlg, IDOK);
+                                       return TRUE;
+                       }
+                       break;
+               }
+               case WM_INITDIALOG:
+               {
+                       int len;
+                       WCHAR templateText[MAX_PATH], text[MAX_PATH];
 
-   Sleep(250);
+                       len = GetDlgItemTextW(hwndDlg, IDC_GINALOADFAILED, templateText, MAX_PATH);
+                       if (len)
+                       {
+                               wsprintfW(text, templateText, (LPWSTR)lParam);
+                               SetDlgItemTextW(hwndDlg, IDC_GINALOADFAILED, text);
+                       }
+                       SetFocus(GetDlgItem(hwndDlg, IDOK));
+                       return TRUE;
+               }
+               case WM_CLOSE:
+               {
+                       EndDialog(hwndDlg, IDCANCEL);
+                       return TRUE;
+               }
+       }
 
-   RemoveStatusMessage(Session);
-   */
+       return FALSE;
 }
 
 int WINAPI
@@ -522,73 +287,67 @@ WinMain(
        ULONG AuthenticationPackage;
        NTSTATUS Status;
 #endif
+       ULONG HardErrorResponse;
+       MSG Msg;
+       HANDLE hThread;
+
+       UNREFERENCED_PARAMETER(hPrevInstance);
+       UNREFERENCED_PARAMETER(lpCmdLine);
+       UNREFERENCED_PARAMETER(nShowCmd);
 
        hAppInstance = hInstance;
 
        if (!RegisterLogonProcess(GetCurrentProcessId(), TRUE))
        {
                ERR("WL: Could not register logon process\n");
+               HandleShutdown(NULL, WLX_SAS_ACTION_SHUTDOWN_POWER_OFF);
                NtShutdownSystem(ShutdownNoReboot);
                ExitProcess(0);
-               return 0;
        }
 
-       WLSession = (PWLSESSION)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WLSESSION));
+       WLSession = (PWLSESSION)HeapAlloc(GetProcessHeap(), 0, sizeof(WLSESSION));
        if (!WLSession)
        {
-               NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED, 0, 0, 0, 0, 0);
+               ERR("WL: Could not allocate memory for winlogon instance\n");
+               NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED, 0, 0, NULL, OptionOk, &HardErrorResponse);
                ExitProcess(1);
-               return 1;
        }
+       ZeroMemory(WLSession, sizeof(WLSESSION));
        WLSession->DialogTimeout = 120; /* 2 minutes */
 
        if (!CreateWindowStationAndDesktops(WLSession))
        {
-               NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED, 0, 0, 0, 0, 0);
+               ERR("WL: Could not create window station and desktops\n");
+               NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED, 0, 0, NULL, OptionOk, &HardErrorResponse);
                ExitProcess(1);
-               return 1;
        }
        LockWorkstation(WLSession);
 
-       /* Check for pending setup */
-       if (GetSetupType() != 0)
+       if (!StartServicesManager())
        {
-               DPRINT("Winlogon: CheckForSetup() in setup mode\n");
-
-               /* Run setup and reboot when done */
-               SwitchDesktop(WLSession->ApplicationDesktop);
-               RunSetup();
+               ERR("WL: Could not start services.exe\n");
+               NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED, 0, 0, NULL, OptionOk, &HardErrorResponse);
+               ExitProcess(1);
+       }
 
-               NtShutdownSystem(ShutdownReboot);
-               ExitProcess(0);
-               return 0;
+       if (!StartLsass())
+       {
+               ERR("WL: Failed to start lsass.exe service (error %lu)\n", GetLastError());
+               NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED, 0, 0, 0, OptionOk, &HardErrorResponse);
+               ExitProcess(1);
        }
 
        /* Load and initialize gina */
        if (!GinaInit(WLSession))
        {
                ERR("WL: Failed to initialize Gina\n");
-               NtShutdownSystem(ShutdownNoReboot);
-               ExitProcess(0);
-               return 0;
+               DialogBoxParam(hAppInstance, MAKEINTRESOURCE(IDD_GINALOADFAILED), GetDesktopWindow(), GinaLoadFailedWindowProc, (LPARAM)L"");
+               HandleShutdown(WLSession, WLX_SAS_ACTION_SHUTDOWN_REBOOT);
+               ExitProcess(1);
        }
 
        DisplayStatusMessage(WLSession, WLSession->WinlogonDesktop, IDS_REACTOSISSTARTINGUP);
 
-       if (!StartServicesManager())
-       {
-               ERR("WL: Could not start services.exe\n");
-               NtShutdownSystem(ShutdownNoReboot);
-               ExitProcess(0);
-               return 0;
-       }
-
-       if (!StartLsass())
-       {
-               DPRINT1("WL: Failed to start lsass.exe service (error %lu)\n", GetLastError());
-               return 1;
-       }
-
 #if 0
        /* Connect to NetLogon service (lsass.exe) */
        /* Real winlogon uses "Winlogon" */
@@ -597,7 +356,12 @@ WinMain(
        if (Status == STATUS_PORT_CONNECTION_REFUSED)
        {
                /* Add the 'SeTcbPrivilege' privilege and try again */
-               RtlAdjustPrivilege(SE_TCB_PRIVILEGE, TRUE, TRUE, &Old);
+               Status = RtlAdjustPrivilege(SE_TCB_PRIVILEGE, TRUE, TRUE, &Old);
+               if (!NT_SUCCESS(Status))
+               {
+                       ERR("RtlAdjustPrivilege() failed with error %lu\n", LsaNtStatusToWinError(Status));
+                       return 1;
+               }
                Status = LsaRegisterLogonProcess(&ProcessName, &LsaHandle, &Mode);
        }
        if (!NT_SUCCESS(Status))
@@ -621,33 +385,46 @@ WinMain(
        {
                ERR("WL: Failed to initialize SAS\n");
                ExitProcess(2);
-               return 2;
        }
 
-       /* Main loop */
-       SessionLoop(WLSession);
-
-   /* FIXME - Flush disks and registry, ... */
-
-   if(WLSession->LogonStatus == 0)
-   {
-     /* FIXME - only show this dialog if it's a shutdown and the computer doesn't support APM */
-     switch(DialogBox(hInstance, MAKEINTRESOURCE(IDD_SHUTDOWNCOMPUTER), 0, ShutdownComputerWindowProc))
-     {
-       case IDC_BTNSHTDOWNCOMPUTER:
-         NtShutdownSystem(ShutdownReboot);
-         break;
-       default:
-         NtShutdownSystem(ShutdownNoReboot);
-         break;
-     }
-     ExitProcess(0);
-   }
-   else
-   {
-     DPRINT1("WL: LogonStatus != LOGON_SHUTDOWN!!!\n");
-     ExitProcess(0);
-   }
-
-   return 0;
+       //DisplayStatusMessage(Session, Session->WinlogonDesktop, IDS_PREPARENETWORKCONNECTIONS);
+       //DisplayStatusMessage(Session, Session->WinlogonDesktop, IDS_APPLYINGCOMPUTERSETTINGS);
+
+       /* Display logged out screen */
+       WLSession->LogonStatus = WKSTA_IS_LOGGED_OFF;
+       RemoveStatusMessage(WLSession);
+
+       /* Check for pending setup */
+       if (GetSetupType() != 0)
+       {
+               TRACE("WL: Setup mode detected\n");
+
+               /* Run setup and reboot when done */
+               SwitchDesktop(WLSession->ApplicationDesktop);
+               RunSetup();
+       }
+       else
+               PostMessageW(WLSession->SASWindow, WLX_WM_SAS, WLX_SAS_TYPE_TIMEOUT, 0);
+
+       /* Play logon sound */
+       hThread = CreateThread(NULL, 0, PlayLogonSoundThread, NULL, 0, NULL);
+       if (hThread)
+       {
+               CloseHandle(hThread);
+       }
+
+       /* Tell kernel that CurrentControlSet is good (needed
+        * to support Last good known configuration boot) */
+       NtInitializeRegistry(CM_BOOT_FLAG_ACCEPTED | 1);
+
+       /* Message loop for the SAS window */
+       while (GetMessageW(&Msg, WLSession->SASWindow, 0, 0))
+       {
+               TranslateMessage(&Msg);
+               DispatchMessageW(&Msg);
+       }
+
+       /* We never go there */
+
+       return 0;
 }