[MSGINA]
[reactos.git] / reactos / dll / win32 / msgina / msgina.c
index fbd9ef5..04482f3 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
@@ -86,6 +93,28 @@ ReadRegSzKey(
     return ERROR_SUCCESS;
 }
 
+static LONG
+ReadRegDwordKey(
+    IN HKEY hKey,
+    IN LPCWSTR pszKey,
+    OUT LPDWORD pValue)
+{
+    LONG rc;
+    DWORD dwType;
+    DWORD cbData;
+    DWORD dwValue;
+
+    if (!pValue)
+        return ERROR_INVALID_PARAMETER;
+
+    cbData = sizeof(DWORD);
+    rc = RegQueryValueExW(hKey, pszKey, NULL, &dwType, (LPBYTE)&dwValue, &cbData);
+    if (rc == ERROR_SUCCESS && dwType == REG_DWORD)
+        *pValue = dwValue;
+
+    return ERROR_SUCCESS;
+}
+
 static VOID
 ChooseGinaUI(VOID)
 {
@@ -133,6 +162,103 @@ cleanup:
     HeapFree(GetProcessHeap(), 0, SystemStartOptions);
 }
 
+
+static
+BOOL
+GetRegistrySettings(PGINA_CONTEXT pgContext)
+{
+    HKEY hKey = NULL;
+    LPWSTR lpAutoAdminLogon = NULL;
+    LPWSTR lpDontDisplayLastUserName = NULL;
+    LPWSTR lpShutdownWithoutLogon = NULL;
+    DWORD dwDisableCAD = 0;
+    DWORD dwSize;
+    LONG rc;
+
+    rc = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
+                       L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon",
+                       0,
+                       KEY_QUERY_VALUE,
+                       &hKey);
+    if (rc != ERROR_SUCCESS)
+    {
+        WARN("RegOpenKeyExW() failed with error %lu\n", rc);
+        return FALSE;
+    }
+
+    rc = ReadRegSzKey(hKey,
+                      L"AutoAdminLogon",
+                      &lpAutoAdminLogon);
+    if (rc == ERROR_SUCCESS)
+    {
+        if (wcscmp(lpAutoAdminLogon, L"1") == 0)
+            pgContext->bAutoAdminLogon = TRUE;
+    }
+
+    TRACE("bAutoAdminLogon: %s\n", pgContext->bAutoAdminLogon ? "TRUE" : "FALSE");
+
+    rc = ReadRegDwordKey(hKey,
+                         L"DisableCAD",
+                         &dwDisableCAD);
+    if (rc == ERROR_SUCCESS)
+    {
+        if (dwDisableCAD != 0)
+            pgContext->bDisableCAD = TRUE;
+    }
+
+    TRACE("bDisableCAD: %s\n", pgContext->bDisableCAD ? "TRUE" : "FALSE");
+
+    pgContext->bShutdownWithoutLogon = TRUE;
+    rc = ReadRegSzKey(hKey,
+                      L"ShutdownWithoutLogon",
+                      &lpShutdownWithoutLogon);
+    if (rc == ERROR_SUCCESS)
+    {
+        if (wcscmp(lpShutdownWithoutLogon, L"0") == 0)
+            pgContext->bShutdownWithoutLogon = FALSE;
+    }
+
+    rc = ReadRegSzKey(hKey,
+                      L"DontDisplayLastUserName",
+                      &lpDontDisplayLastUserName);
+    if (rc == ERROR_SUCCESS)
+    {
+        if (wcscmp(lpDontDisplayLastUserName, L"1") == 0)
+            pgContext->bDontDisplayLastUserName = TRUE;
+    }
+
+    dwSize = 256 * sizeof(WCHAR);
+    rc = RegQueryValueExW(hKey,
+                          L"DefaultUserName",
+                          NULL,
+                          NULL,
+                          (LPBYTE)&pgContext->UserName,
+                          &dwSize);
+
+    dwSize = 256 * sizeof(WCHAR);
+    rc = RegQueryValueExW(hKey,
+                          L"DefaultDomainName",
+                          NULL,
+                          NULL,
+                          (LPBYTE)&pgContext->Domain,
+                          &dwSize);
+
+    if (lpShutdownWithoutLogon != NULL)
+        HeapFree(GetProcessHeap(), 0, lpShutdownWithoutLogon);
+
+    if (lpDontDisplayLastUserName != NULL)
+        HeapFree(GetProcessHeap(), 0, lpDontDisplayLastUserName);
+
+    if (lpAutoAdminLogon != NULL)
+        HeapFree(GetProcessHeap(), 0, lpAutoAdminLogon);
+
+    if (hKey != NULL)
+        RegCloseKey(hKey);
+
+    return TRUE;
+}
+
+
 /*
  * @implemented
  */
@@ -155,6 +281,13 @@ WlxInitialize(
         return FALSE;
     }
 
+    if (!GetRegistrySettings(pgContext))
+    {
+        WARN("GetRegistrySettings() failed\n");
+        LocalFree(pgContext);
+        return FALSE;
+    }
+
     /* Return the context to winlogon */
     *pWlxContext = (PVOID)pgContext;
     pgContext->hDllInstance = hDllInstance;
@@ -463,6 +596,87 @@ 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;
+
+    TRACE("(%S %S %S)\n", UserName, Domain, Password);
+
+    if (!ConnectToLsa(pgContext))
+        return FALSE;
+
+    if (!MyLogonUser(pgContext->LsaHandle,
+                     pgContext->AuthenticationPackage,
+                     UserName,
+                     Domain,
+                     Password,
+                     &pgContext->UserToken))
+    {
+        WARN("LogonUserW() 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,
@@ -478,10 +692,15 @@ DoLoginTasks(
     DWORD dwLength;
     BOOL bResult;
 
-    if (!LogonUserW(UserName, Domain, Password,
-        LOGON32_LOGON_INTERACTIVE,
-        LOGON32_PROVIDER_DEFAULT,
-        &pgContext->UserToken))
+    if (!ConnectToLsa(pgContext))
+        return FALSE;
+
+    if (!MyLogonUser(pgContext->LsaHandle,
+                     pgContext->AuthenticationPackage,
+                     UserName,
+                     Domain,
+                     Password,
+                     &pgContext->UserToken))
     {
         WARN("LogonUserW() failed\n");
         goto cleanup;
@@ -573,46 +792,6 @@ cleanup:
 }
 
 
-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;
-}
-
-
 static BOOL
 DoAutoLogon(
     IN PGINA_CONTEXT pgContext)
@@ -691,7 +870,15 @@ DoAutoLogon(
         result = DoLoginTasks(pgContext, UserName, DomainName, Password);
 
         if (result == TRUE)
+        {
+            pgContext->Password = HeapAlloc(GetProcessHeap(),
+                                            HEAP_ZERO_MEMORY,
+                                            (wcslen(Password) + 1) * sizeof(WCHAR));
+            if (pgContext->Password != NULL)
+                wcscpy(pgContext->Password, Password);
+
             NotifyBootConfigStatus(TRUE);
+        }
     }
 
 cleanup:
@@ -726,7 +913,7 @@ WlxDisplaySASNotice(
         return;
     }
 
-    if (CheckAutoAdminLogon(pgContext))
+    if (pgContext->bAutoAdminLogon == TRUE)
     {
         /* Don't display the window, we want to do an automatic logon */
         pgContext->AutoLogonState = AUTOLOGON_ONCE;
@@ -736,6 +923,12 @@ WlxDisplaySASNotice(
     else
         pgContext->AutoLogonState = AUTOLOGON_DISABLED;
 
+    if (pgContext->bDisableCAD == TRUE)
+    {
+        pgContext->pWlxFuncs->WlxSasNotify(pgContext->hWlx, WLX_SAS_TYPE_CTRL_ALT_DEL);
+        return;
+    }
+
     pGinaUI->DisplaySASNotice(pgContext);
 
     TRACE("WlxDisplaySASNotice() done\n");
@@ -811,6 +1004,12 @@ WlxDisplayLockedNotice(PVOID pWlxContext)
 
     TRACE("WlxDisplayLockedNotice()\n");
 
+    if (pgContext->bDisableCAD == TRUE)
+    {
+        pgContext->pWlxFuncs->WlxSasNotify(pgContext->hWlx, WLX_SAS_TYPE_CTRL_ALT_DEL);
+        return;
+    }
+
     pGinaUI->DisplayLockedNotice(pgContext);
 }
 
@@ -836,7 +1035,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;
 }