[LSASRV]
[reactos.git] / reactos / dll / win32 / lsasrv / authpackage.c
index a5af21f..9922031 100644 (file)
@@ -31,6 +31,9 @@ typedef struct _LSA_TOKEN_INFORMATION_V1
 
 typedef PVOID PLSA_CLIENT_REQUEST;
 
+typedef NTSTATUS (NTAPI *PLSA_CREATE_LOGON_SESSION)(PLUID);
+typedef NTSTATUS (NTAPI *PLSA_DELETE_LOGON_SESSION)(PLUID);
+
 typedef PVOID (NTAPI *PLSA_ALLOCATE_LSA_HEAP)(ULONG);
 typedef VOID (NTAPI *PLSA_FREE_LSA_HEAP)(PVOID);
 typedef NTSTATUS (NTAPI *PLSA_ALLOCATE_CLIENT_BUFFER)(PLSA_CLIENT_REQUEST, ULONG, PVOID*);
@@ -42,8 +45,8 @@ typedef NTSTATUS (NTAPI *PLSA_COPY_FROM_CLIENT_BUFFER)(PLSA_CLIENT_REQUEST,
 
 typedef struct LSA_DISPATCH_TABLE
 {
-    PVOID /*PLSA_CREATE_LOGON_SESSION */ CreateLogonSession;
-    PVOID /*PLSA_DELETE_LOGON_SESSION */ DeleteLogonSession;
+    PLSA_CREATE_LOGON_SESSION CreateLogonSession;
+    PLSA_DELETE_LOGON_SESSION DeleteLogonSession;
     PVOID /*PLSA_ADD_CREDENTIAL */ AddCredential;
     PVOID /*PLSA_GET_CREDENTIALS */ GetCredentials;
     PVOID /*PLSA_DELETE_CREDENTIAL */ DeleteCredential;
@@ -294,26 +297,6 @@ LsapGetAuthenticationPackage(IN ULONG PackageId)
 }
 
 
-static
-NTSTATUS
-NTAPI
-LsapCreateLogonSession(IN PLUID LogonId)
-{
-    TRACE("()\n");
-    return STATUS_SUCCESS;
-}
-
-
-static
-NTSTATUS
-NTAPI
-LsapDeleteLogonSession(IN PLUID LogonId)
-{
-    TRACE("()\n");
-    return STATUS_SUCCESS;
-}
-
-
 static
 PVOID
 NTAPI
@@ -454,8 +437,7 @@ LsapInitAuthPackages(VOID)
                                     &PackageId,
                                     NULL);
 
-
-    return STATUS_SUCCESS;
+    return Status;
 }
 
 
@@ -557,6 +539,112 @@ LsapCallAuthenticationPackage(PLSA_API_MSG RequestMsg,
 }
 
 
+static
+NTSTATUS
+LsapCopyLocalGroups(
+    IN PLSAP_LOGON_CONTEXT LogonContext,
+    IN PTOKEN_GROUPS ClientGroups,
+    IN ULONG ClientGroupsCount,
+    OUT PTOKEN_GROUPS *TokenGroups)
+{
+    ULONG LocalGroupsLength = 0;
+    PTOKEN_GROUPS LocalGroups = NULL;
+    ULONG SidHeaderLength = 0;
+    PSID SidHeader = NULL;
+    PSID Sid;
+    ULONG SidLength;
+    ULONG CopiedSids = 0;
+    ULONG i;
+    NTSTATUS Status;
+
+    LocalGroupsLength = sizeof(TOKEN_GROUPS) +
+                        (ClientGroupsCount - ANYSIZE_ARRAY) * sizeof(SID_AND_ATTRIBUTES);
+    LocalGroups = RtlAllocateHeap(RtlGetProcessHeap(),
+                                  HEAP_ZERO_MEMORY,
+                                  LocalGroupsLength);
+    if (LocalGroups == NULL)
+    {
+        TRACE("RtlAllocateHeap() failed\n");
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+
+    Status = NtReadVirtualMemory(LogonContext->ClientProcessHandle,
+                                 ClientGroups,
+                                 LocalGroups,
+                                 LocalGroupsLength,
+                                 NULL);
+    if (!NT_SUCCESS(Status))
+        goto done;
+
+
+    SidHeaderLength  = RtlLengthRequiredSid(0);
+    SidHeader = RtlAllocateHeap(RtlGetProcessHeap(),
+                                HEAP_ZERO_MEMORY,
+                                SidHeaderLength);
+    if (SidHeader == NULL)
+    {
+        Status = STATUS_INSUFFICIENT_RESOURCES;
+        goto done;
+    }
+
+    for (i = 0; i < ClientGroupsCount; i++)
+    {
+        Status = NtReadVirtualMemory(LogonContext->ClientProcessHandle,
+                                     LocalGroups->Groups[i].Sid,
+                                     SidHeader,
+                                     SidHeaderLength,
+                                     NULL);
+        if (!NT_SUCCESS(Status))
+            goto done;
+
+        SidLength = RtlLengthSid(SidHeader);
+        TRACE("Sid %lu: Length %lu\n", i, SidLength);
+
+        Sid = RtlAllocateHeap(RtlGetProcessHeap(),
+                              HEAP_ZERO_MEMORY,
+                              SidLength);
+        if (SidHeader == NULL)
+        {
+            Status = STATUS_INSUFFICIENT_RESOURCES;
+            goto done;
+        }
+
+        Status = NtReadVirtualMemory(LogonContext->ClientProcessHandle,
+                                     LocalGroups->Groups[i].Sid,
+                                     Sid,
+                                     SidLength,
+                                     NULL);
+        if (!NT_SUCCESS(Status))
+        {
+            RtlFreeHeap(RtlGetProcessHeap(), 0, Sid);
+            goto done;
+        }
+
+        LocalGroups->Groups[i].Sid = Sid;
+        CopiedSids++;
+    }
+
+    *TokenGroups = LocalGroups;
+
+done:
+    if (SidHeader != NULL)
+        RtlFreeHeap(RtlGetProcessHeap(), 0, SidHeader);
+
+    if (!NT_SUCCESS(Status))
+    {
+        if (LocalGroups != NULL)
+        {
+            for (i = 0; i < CopiedSids; i++)
+                RtlFreeHeap(RtlGetProcessHeap(), 0, LocalGroups->Groups[i].Sid);
+
+            RtlFreeHeap(RtlGetProcessHeap(), 0, LocalGroups);
+        }
+    }
+
+    return Status;
+}
+
+
 NTSTATUS
 LsapLogonUser(PLSA_API_MSG RequestMsg,
               PLSAP_LOGON_CONTEXT LogonContext)
@@ -571,6 +659,7 @@ LsapLogonUser(PLSA_API_MSG RequestMsg,
     PUNICODE_STRING AuthenticatingAuthority = NULL;
     PUNICODE_STRING MachineName = NULL;
     PVOID LocalAuthInfo = NULL;
+    PTOKEN_GROUPS LocalGroups = NULL;
     HANDLE TokenHandle = NULL;
     ULONG i;
     ULONG PackageId;
@@ -614,6 +703,18 @@ LsapLogonUser(PLSA_API_MSG RequestMsg,
         }
     }
 
+    if (RequestMsg->LogonUser.Request.LocalGroupsCount > 0)
+    {
+        Status = LsapCopyLocalGroups(LogonContext,
+                                     RequestMsg->LogonUser.Request.LocalGroups,
+                                     RequestMsg->LogonUser.Request.LocalGroupsCount,
+                                     &LocalGroups);
+        if (!NT_SUCCESS(Status))
+            goto done;
+
+        TRACE("GroupCount: %lu\n", LocalGroups->GroupCount);
+    }
+
     if (Package->LsaApLogonUserEx2 != NULL)
     {
         Status = Package->LsaApLogonUserEx2((PLSA_CLIENT_REQUEST)LogonContext,
@@ -732,6 +833,15 @@ LsapLogonUser(PLSA_API_MSG RequestMsg,
 
     TokenHandle = NULL;
 
+#if 0
+    Status = LsapSetLogonSessionData(&RequestMsg->LogonUser.Reply.LogonId);
+    if (!NT_SUCCESS(Status))
+    {
+        TRACE("LsapSetLogonSessionData failed (Status 0x%08lx)\n", Status);
+        goto done;
+    }
+#endif
+
 done:
     if (!NT_SUCCESS(Status))
     {
@@ -739,10 +849,20 @@ done:
             NtClose(TokenHandle);
     }
 
+    /* Free the local groups */
+    if (LocalGroups != NULL)
+    {
+        for (i = 0; i < LocalGroups->GroupCount; i++)
+            RtlFreeHeap(RtlGetProcessHeap(), 0, LocalGroups->Groups[i].Sid);
+
+        RtlFreeHeap(RtlGetProcessHeap(), 0, LocalGroups);
+    }
+
     /* Free the local authentication info buffer */
     if (LocalAuthInfo != NULL)
         RtlFreeHeap(RtlGetProcessHeap(), 0, LocalAuthInfo);
 
+    /* Free the token information */
     if (TokenInformation != NULL)
     {
         if (TokenInformationType == LsaTokenInformationV1)
@@ -786,21 +906,35 @@ done:
         }
     }
 
+    /* Free the account name */
     if (AccountName != NULL)
     {
+        if (AccountName->Buffer != NULL)
+            LsapFreeHeap(AccountName->Buffer);
 
+        LsapFreeHeap(AccountName);
     }
 
+    /* Free the authentication authority */
     if (AuthenticatingAuthority != NULL)
     {
+        if (AuthenticatingAuthority != NULL)
+            LsapFreeHeap(AuthenticatingAuthority->Buffer);
 
+        LsapFreeHeap(AuthenticatingAuthority);
     }
 
+    /* Free the machine name */
     if (MachineName != NULL)
     {
+        if (MachineName->Buffer != NULL)
+            LsapFreeHeap(MachineName->Buffer);
 
+        LsapFreeHeap(MachineName);
     }
 
+    TRACE("LsapLogonUser done (Status 0x%08lx)\n", Status);
+
     return Status;
 }