[MSGINA]
[reactos.git] / reactos / dll / win32 / msgina / msgina.c
index fd8da6e..6480cbe 100644 (file)
 
 #include "msgina.h"
 
+#include <winreg.h>
+#include <winsvc.h>
+#include <userenv.h>
+#include <ndk/sefuncs.h>
+
 HINSTANCE hDllInstance;
 
 extern GINA_UI GinaGraphicalUI;
 extern GINA_UI GinaTextUI;
 static PGINA_UI pGinaUI;
+static SID_IDENTIFIER_AUTHORITY SystemAuthority = {SECURITY_NT_AUTHORITY};
+static PSID AdminSid;
 
 /*
  * @implemented
@@ -230,7 +237,7 @@ GetRegistrySettings(PGINA_CONTEXT pgContext)
 
     dwSize = 256 * sizeof(WCHAR);
     rc = RegQueryValueExW(hKey,
-                          L"DefaultDomainName",
+                          L"DefaultDomain",
                           NULL,
                           NULL,
                           (LPBYTE)&pgContext->Domain,
@@ -306,6 +313,8 @@ WlxInitialize(
     /* Check autologon settings the first time */
     pgContext->AutoLogonState = AUTOLOGON_CHECK_REGISTRY;
 
+    pgContext->nShutdownAction = WLX_SAS_ACTION_SHUTDOWN_POWER_OFF;
+
     ChooseGinaUI();
     return pGinaUI->Initialize(pgContext);
 }
@@ -589,6 +598,94 @@ DuplicationString(PWSTR Str)
     return NewStr;
 }
 
+
+BOOL
+DoAdminUnlock(
+    IN PGINA_CONTEXT pgContext,
+    IN PWSTR UserName,
+    IN PWSTR Domain,
+    IN PWSTR Password)
+{
+    HANDLE hToken = NULL;
+    PTOKEN_GROUPS Groups = NULL;
+    BOOL bIsAdmin = FALSE;
+    ULONG Size;
+    ULONG i;
+    NTSTATUS Status;
+    NTSTATUS SubStatus = STATUS_SUCCESS;
+
+    TRACE("(%S %S %S)\n", UserName, Domain, Password);
+
+    Status = ConnectToLsa(pgContext);
+    if (!NT_SUCCESS(Status))
+    {
+        WARN("ConnectToLsa() failed\n");
+        return FALSE;
+    }
+
+    Status = MyLogonUser(pgContext->LsaHandle,
+                         pgContext->AuthenticationPackage,
+                         UserName,
+                         Domain,
+                         Password,
+                         &pgContext->UserToken,
+                         &SubStatus);
+    if (!NT_SUCCESS(Status))
+    {
+        WARN("MyLogonUser() failed\n");
+        return FALSE;
+    }
+
+    Status = NtQueryInformationToken(hToken,
+                                     TokenGroups,
+                                     NULL,
+                                     0,
+                                     &Size);
+    if ((Status != STATUS_SUCCESS) && (Status != STATUS_BUFFER_TOO_SMALL))
+    {
+        TRACE("NtQueryInformationToken() failed (Status 0x%08lx)\n", Status);
+        goto done;
+    }
+
+    Groups = HeapAlloc(GetProcessHeap(), 0, Size);
+    if (Groups == NULL)
+    {
+        TRACE("HeapAlloc() failed\n");
+        goto done;
+    }
+
+    Status = NtQueryInformationToken(hToken,
+                                     TokenGroups,
+                                     Groups,
+                                     Size,
+                                     &Size);
+    if (!NT_SUCCESS(Status))
+    {
+        TRACE("NtQueryInformationToken() failed (Status 0x%08lx)\n", Status);
+        goto done;
+    }
+
+    for (i = 0; i < Groups->GroupCount; i++)
+    {
+        if (RtlEqualSid(Groups->Groups[i].Sid, AdminSid))
+        {
+            TRACE("Member of Admins group\n");
+            bIsAdmin = TRUE;
+            break;
+        }
+    }
+
+done:
+    if (Groups != NULL)
+        HeapFree(GetProcessHeap(), 0, Groups);
+
+    if (hToken != NULL)
+        CloseHandle(hToken);
+
+    return bIsAdmin;
+}
+
+
 BOOL
 DoLoginTasks(
     IN OUT PGINA_CONTEXT pgContext,
@@ -603,13 +700,26 @@ DoLoginTasks(
     DWORD cbStats, cbSize;
     DWORD dwLength;
     BOOL bResult;
+    NTSTATUS SubStatus;
+    NTSTATUS Status;
+
+    Status = ConnectToLsa(pgContext);
+    if (!NT_SUCCESS(Status))
+    {
+        WARN("ConnectToLsa() failed\n");
+        return FALSE;
+    }
 
-    if (!LogonUserW(UserName, Domain, Password,
-        LOGON32_LOGON_INTERACTIVE,
-        LOGON32_PROVIDER_DEFAULT,
-        &pgContext->UserToken))
+    Status = MyLogonUser(pgContext->LsaHandle,
+                         pgContext->AuthenticationPackage,
+                         UserName,
+                         Domain,
+                         Password,
+                         &pgContext->UserToken,
+                         &SubStatus);
+    if (!NT_SUCCESS(Status))
     {
-        WARN("LogonUserW() failed\n");
+        WARN("MyLogonUser() failed\n");
         goto cleanup;
     }
 
@@ -698,46 +808,6 @@ cleanup:
     return FALSE;
 }
 
-#if 0
-static
-BOOL
-CheckAutoAdminLogon(
-    IN PGINA_CONTEXT pgContext)
-{
-    HKEY WinLogonKey = NULL;
-    LPWSTR AutoLogon = NULL;
-    BOOL result = FALSE;
-    LONG rc;
-
-    if (pgContext->AutoLogonState == AUTOLOGON_DISABLED)
-        return FALSE;
-
-    rc = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
-                       L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\WinLogon",
-                       0,
-                       KEY_QUERY_VALUE,
-                       &WinLogonKey);
-    if (rc != ERROR_SUCCESS)
-        goto cleanup;
-
-    rc = ReadRegSzKey(WinLogonKey,
-                      L"AutoAdminLogon",
-                      &AutoLogon);
-
-    if (rc != ERROR_SUCCESS)
-        goto cleanup;
-
-    if (wcscmp(AutoLogon, L"1") == 0)
-        result = TRUE;
-
-cleanup:
-    if (WinLogonKey != NULL)
-        RegCloseKey(WinLogonKey);
-    HeapFree(GetProcessHeap(), 0, AutoLogon);
-
-    return result;
-}
-#endif
 
 static BOOL
 DoAutoLogon(
@@ -748,7 +818,7 @@ DoAutoLogon(
     LPWSTR AutoCount = NULL;
     LPWSTR IgnoreShiftOverride = NULL;
     LPWSTR UserName = NULL;
-    LPWSTR DomainName = NULL;
+    LPWSTR Domain = NULL;
     LPWSTR Password = NULL;
     BOOL result = FALSE;
     LONG rc;
@@ -807,17 +877,22 @@ DoAutoLogon(
         rc = ReadRegSzKey(WinLogonKey, L"DefaultUserName", &UserName);
         if (rc != ERROR_SUCCESS)
             goto cleanup;
-        rc = ReadRegSzKey(WinLogonKey, L"DefaultDomainName", &DomainName);
+        rc = ReadRegSzKey(WinLogonKey, L"DefaultDomain", &Domain);
         if (rc != ERROR_SUCCESS && rc != ERROR_FILE_NOT_FOUND)
             goto cleanup;
         rc = ReadRegSzKey(WinLogonKey, L"DefaultPassword", &Password);
         if (rc != ERROR_SUCCESS)
             goto cleanup;
 
-        result = DoLoginTasks(pgContext, UserName, DomainName, Password);
+        result = DoLoginTasks(pgContext, UserName, Domain, Password);
 
         if (result == TRUE)
+        {
+            ZeroMemory(pgContext->Password, 256 * sizeof(WCHAR));
+            wcscpy(pgContext->Password, Password);
+
             NotifyBootConfigStatus(TRUE);
+        }
     }
 
 cleanup:
@@ -827,7 +902,7 @@ cleanup:
     HeapFree(GetProcessHeap(), 0, AutoCount);
     HeapFree(GetProcessHeap(), 0, IgnoreShiftOverride);
     HeapFree(GetProcessHeap(), 0, UserName);
-    HeapFree(GetProcessHeap(), 0, DomainName);
+    HeapFree(GetProcessHeap(), 0, Domain);
     HeapFree(GetProcessHeap(), 0, Password);
     TRACE("DoAutoLogon(): AutoLogonState = %lu, returning %d\n",
         pgContext->AutoLogonState, result);
@@ -852,7 +927,6 @@ WlxDisplaySASNotice(
         return;
     }
 
-//    if (CheckAutoAdminLogon(pgContext))
     if (pgContext->bAutoAdminLogon == TRUE)
     {
         /* Don't display the window, we want to do an automatic logon */
@@ -975,7 +1049,27 @@ DllMain(
     UNREFERENCED_PARAMETER(lpvReserved);
 
     if (dwReason == DLL_PROCESS_ATTACH)
+    {
         hDllInstance = hinstDLL;
 
+        RtlAllocateAndInitializeSid(&SystemAuthority,
+                                    2,
+                                    SECURITY_BUILTIN_DOMAIN_RID,
+                                    DOMAIN_ALIAS_RID_ADMINS,
+                                    SECURITY_NULL_RID,
+                                    SECURITY_NULL_RID,
+                                    SECURITY_NULL_RID,
+                                    SECURITY_NULL_RID,
+                                    SECURITY_NULL_RID,
+                                    SECURITY_NULL_RID,
+                                    &AdminSid);
+
+    }
+    else if (dwReason == DLL_PROCESS_DETACH)
+    {
+        if (AdminSid != NULL)
+            RtlFreeSid(AdminSid);
+    }
+
     return TRUE;
 }