[SAMSRV]
[reactos.git] / reactos / dll / win32 / samsrv / samrpc.c
index ed3ea75..dcfbd13 100644 (file)
@@ -7,17 +7,18 @@
  * PROGRAMMERS:     Eric Kohl
  */
 
-/* INCLUDES ****************************************************************/
+/* INCLUDES ******************************************************************/
 
 #include "samsrv.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(samsrv);
 
-/* GLOBALS ********************************************************************/
+/* GLOBALS *******************************************************************/
 
 static SID_IDENTIFIER_AUTHORITY NtSidAuthority = {SECURITY_NT_AUTHORITY};
 
-/* FUNCTIONS ***************************************************************/
+
+/* FUNCTIONS *****************************************************************/
 
 VOID
 SampStartRpcServer(VOID)
@@ -154,6 +155,7 @@ SamrShutdownSamServer(IN SAMPR_HANDLE ServerHandle)
     return STATUS_NOT_IMPLEMENTED;
 }
 
+
 /* Function 5 */
 NTSTATUS
 NTAPI
@@ -263,6 +265,7 @@ SamrLookupDomainInSamServer(IN SAMPR_HANDLE ServerHandle,
     return Status;
 }
 
+
 /* Function 6 */
 NTSTATUS
 NTAPI
@@ -2925,6 +2928,9 @@ SamrLookupNamesInDomain(IN SAMPR_HANDLE DomainHandle,
             SampRegCloseKey(AccountsKeyHandle);
         }
 
+        if (!NT_SUCCESS(Status) && Status != STATUS_OBJECT_NAME_NOT_FOUND)
+            break;
+
         /* Return alias account */
         if (NT_SUCCESS(Status) && RelativeId != 0)
         {
@@ -2961,6 +2967,9 @@ SamrLookupNamesInDomain(IN SAMPR_HANDLE DomainHandle,
             SampRegCloseKey(AccountsKeyHandle);
         }
 
+        if (!NT_SUCCESS(Status) && Status != STATUS_OBJECT_NAME_NOT_FOUND)
+            break;
+
         /* Return group account */
         if (NT_SUCCESS(Status) && RelativeId != 0)
         {
@@ -2997,6 +3006,9 @@ SamrLookupNamesInDomain(IN SAMPR_HANDLE DomainHandle,
             SampRegCloseKey(AccountsKeyHandle);
         }
 
+        if (!NT_SUCCESS(Status) && Status != STATUS_OBJECT_NAME_NOT_FOUND)
+            break;
+
         /* Return user account */
         if (NT_SUCCESS(Status) && RelativeId != 0)
         {
@@ -3042,6 +3054,8 @@ done:
         Use->Count = 0;
     }
 
+    TRACE("Returned Status %lx\n", Status);
+
     return Status;
 }
 
@@ -3050,13 +3064,276 @@ done:
 NTSTATUS
 NTAPI
 SamrLookupIdsInDomain(IN SAMPR_HANDLE DomainHandle,
-                      IN unsigned long Count,
-                      IN unsigned long *RelativeIds,
+                      IN ULONG Count,
+                      IN ULONG *RelativeIds,
                       OUT PSAMPR_RETURNED_USTRING_ARRAY Names,
                       OUT PSAMPR_ULONG_ARRAY Use)
 {
-    UNIMPLEMENTED;
-    return STATUS_NOT_IMPLEMENTED;
+    PSAM_DB_OBJECT DomainObject;
+    WCHAR RidString[9];
+    HANDLE AccountsKeyHandle;
+    HANDLE AccountKeyHandle;
+    ULONG MappedCount = 0;
+    ULONG DataLength;
+    ULONG i;
+    NTSTATUS Status;
+
+    TRACE("SamrLookupIdsInDomain(%p %lu %p %p %p)\n",
+          DomainHandle, Count, RelativeIds, Names, Use);
+
+    /* Validate the domain handle */
+    Status = SampValidateDbObject(DomainHandle,
+                                  SamDbDomainObject,
+                                  DOMAIN_LOOKUP,
+                                  &DomainObject);
+    if (!NT_SUCCESS(Status))
+    {
+        TRACE("failed with status 0x%08lx\n", Status);
+        return Status;
+    }
+
+    Names->Count = 0;
+    Use->Count = 0;
+
+    if (Count == 0)
+        return STATUS_SUCCESS;
+
+    /* Allocate the names array */
+    Names->Element = midl_user_allocate(Count * sizeof(ULONG));
+    if (Names->Element == NULL)
+    {
+        Status = STATUS_INSUFFICIENT_RESOURCES;
+        goto done;
+    }
+
+    /* Allocate the use array */
+    Use->Element = midl_user_allocate(Count * sizeof(ULONG));
+    if (Use->Element == NULL)
+    {
+        Status = STATUS_INSUFFICIENT_RESOURCES;
+        goto done;
+    }
+
+    Names->Count = Count;
+    Use->Count = Count;
+
+    for (i = 0; i < Count; i++)
+    {
+        TRACE("RID: %lu\n", RelativeIds[i]);
+
+        swprintf(RidString, L"%08lx", RelativeIds[i]);
+
+        /* Lookup aliases */
+        Status = SampRegOpenKey(DomainObject->KeyHandle,
+                                L"Aliases",
+                                KEY_READ,
+                                &AccountsKeyHandle);
+        if (NT_SUCCESS(Status))
+        {
+            Status = SampRegOpenKey(AccountsKeyHandle,
+                                    RidString,
+                                    KEY_READ,
+                                    &AccountKeyHandle);
+            if (NT_SUCCESS(Status))
+            {
+                DataLength = 0;
+                Status = SampRegQueryValue(AccountKeyHandle,
+                                           L"Name",
+                                           NULL,
+                                           NULL,
+                                           &DataLength);
+                if (NT_SUCCESS(Status))
+                {
+                    Names->Element[i].Buffer = midl_user_allocate(DataLength);
+                    if (Names->Element[i].Buffer == NULL)
+                        Status = STATUS_INSUFFICIENT_RESOURCES;
+
+                    if (NT_SUCCESS(Status))
+                    {
+                        Names->Element[i].MaximumLength = (USHORT)DataLength;
+                        Names->Element[i].Length = (USHORT)(DataLength - sizeof(WCHAR));
+
+                        Status = SampRegQueryValue(AccountKeyHandle,
+                                                   L"Name",
+                                                   NULL,
+                                                   Names->Element[i].Buffer,
+                                                   &DataLength);
+                    }
+                }
+
+                SampRegCloseKey(AccountKeyHandle);
+            }
+
+            SampRegCloseKey(AccountsKeyHandle);
+        }
+
+        if (!NT_SUCCESS(Status) && Status != STATUS_OBJECT_NAME_NOT_FOUND)
+            break;
+
+        /* Return alias account */
+        if (NT_SUCCESS(Status) && Names->Element[i].Buffer != NULL)
+        {
+            TRACE("Name: %S\n", Names->Element[i].Buffer);
+            Use->Element[i] = SidTypeAlias;
+            MappedCount++;
+            continue;
+        }
+
+        /* Lookup groups */
+        Status = SampRegOpenKey(DomainObject->KeyHandle,
+                                L"Groups",
+                                KEY_READ,
+                                &AccountsKeyHandle);
+        if (NT_SUCCESS(Status))
+        {
+            Status = SampRegOpenKey(AccountsKeyHandle,
+                                    RidString,
+                                    KEY_READ,
+                                    &AccountKeyHandle);
+            if (NT_SUCCESS(Status))
+            {
+                DataLength = 0;
+                Status = SampRegQueryValue(AccountKeyHandle,
+                                           L"Name",
+                                           NULL,
+                                           NULL,
+                                           &DataLength);
+                if (NT_SUCCESS(Status))
+                {
+                    Names->Element[i].Buffer = midl_user_allocate(DataLength);
+                    if (Names->Element[i].Buffer == NULL)
+                        Status = STATUS_INSUFFICIENT_RESOURCES;
+
+                    if (NT_SUCCESS(Status))
+                    {
+                        Names->Element[i].MaximumLength = (USHORT)DataLength;
+                        Names->Element[i].Length = (USHORT)(DataLength - sizeof(WCHAR));
+
+                        Status = SampRegQueryValue(AccountKeyHandle,
+                                                   L"Name",
+                                                   NULL,
+                                                   Names->Element[i].Buffer,
+                                                   &DataLength);
+                    }
+                }
+
+                SampRegCloseKey(AccountKeyHandle);
+            }
+
+            SampRegCloseKey(AccountsKeyHandle);
+        }
+
+        if (!NT_SUCCESS(Status) && Status != STATUS_OBJECT_NAME_NOT_FOUND)
+            break;
+
+        /* Return group account */
+        if (NT_SUCCESS(Status) && Names->Element[i].Buffer != NULL)
+        {
+            TRACE("Name: %S\n", Names->Element[i].Buffer);
+            Use->Element[i] = SidTypeGroup;
+            MappedCount++;
+            continue;
+        }
+
+        /* Lookup users */
+        Status = SampRegOpenKey(DomainObject->KeyHandle,
+                                L"Users",
+                                KEY_READ,
+                                &AccountsKeyHandle);
+        if (NT_SUCCESS(Status))
+        {
+            Status = SampRegOpenKey(AccountsKeyHandle,
+                                    RidString,
+                                    KEY_READ,
+                                    &AccountKeyHandle);
+            if (NT_SUCCESS(Status))
+            {
+                DataLength = 0;
+                Status = SampRegQueryValue(AccountKeyHandle,
+                                           L"Name",
+                                           NULL,
+                                           NULL,
+                                           &DataLength);
+                if (NT_SUCCESS(Status))
+                {
+                    TRACE("DataLength: %lu\n", DataLength);
+
+                    Names->Element[i].Buffer = midl_user_allocate(DataLength);
+                    if (Names->Element[i].Buffer == NULL)
+                        Status = STATUS_INSUFFICIENT_RESOURCES;
+
+                    if (NT_SUCCESS(Status))
+                    {
+                        Names->Element[i].MaximumLength = (USHORT)DataLength;
+                        Names->Element[i].Length = (USHORT)(DataLength - sizeof(WCHAR));
+
+                        Status = SampRegQueryValue(AccountKeyHandle,
+                                                   L"Name",
+                                                   NULL,
+                                                   Names->Element[i].Buffer,
+                                                   &DataLength);
+                    }
+                }
+
+                SampRegCloseKey(AccountKeyHandle);
+            }
+
+            SampRegCloseKey(AccountsKeyHandle);
+        }
+
+        if (!NT_SUCCESS(Status) && Status != STATUS_OBJECT_NAME_NOT_FOUND)
+            break;
+
+        /* Return user account */
+        if (NT_SUCCESS(Status) && Names->Element[i].Buffer != NULL)
+        {
+            TRACE("Name: %S\n", Names->Element[i].Buffer);
+            Use->Element[i] = SidTypeUser;
+            MappedCount++;
+            continue;
+        }
+
+        /* Return unknown account */
+        Use->Element[i] = SidTypeUnknown;
+    }
+
+done:
+    if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
+        Status = STATUS_SUCCESS;
+
+    if (NT_SUCCESS(Status))
+    {
+        if (MappedCount == 0)
+            Status = STATUS_NONE_MAPPED;
+        else if (MappedCount < Count)
+            Status = STATUS_SOME_NOT_MAPPED;
+    }
+    else
+    {
+        if (Names->Element != NULL)
+        {
+            for (i = 0; i < Count; i++)
+            {
+                if (Names->Element[i].Buffer != NULL)
+                    midl_user_free(Names->Element[i].Buffer);
+            }
+
+            midl_user_free(Names->Element);
+            Names->Element = NULL;
+        }
+
+        Names->Count = 0;
+
+        if (Use->Element != NULL)
+        {
+            midl_user_free(Use->Element);
+            Use->Element = NULL;
+        }
+
+        Use->Count = 0;
+    }
+
+    return Status;
 }
 
 
@@ -3550,6 +3827,8 @@ SamrOpenAlias(IN SAMPR_HANDLE DomainHandle,
         return Status;
     }
 
+    AliasObject->Special.Alias.RelativeId = AliasId;
+
     *AliasHandle = (SAMPR_HANDLE)AliasObject;
 
     return STATUS_SUCCESS;
@@ -3593,17 +3872,24 @@ SampQueryAliasGeneral(PSAM_DB_OBJECT AliasObject,
                             L"Members",
                             KEY_READ,
                             &MembersKeyHandle);
-    if (!NT_SUCCESS(Status))
+    if (NT_SUCCESS(Status))
     {
-        TRACE("Status 0x%08lx\n", Status);
-        goto done;
+        /* Retrieve the number of members of the alias */
+        Status = SampRegQueryKeyInfo(MembersKeyHandle,
+                                     NULL,
+                                     &InfoBuffer->General.MemberCount);
+        if (!NT_SUCCESS(Status))
+        {
+            TRACE("Status 0x%08lx\n", Status);
+            goto done;
+        }
     }
-
-    /* Retrieve the number of members of the alias */
-    Status = SampRegQueryKeyInfo(MembersKeyHandle,
-                                 NULL,
-                                 &InfoBuffer->General.MemberCount);
-    if (!NT_SUCCESS(Status))
+    else if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
+    {
+        InfoBuffer->General.MemberCount = 0;
+        Status = STATUS_SUCCESS;
+    }
+    else
     {
         TRACE("Status 0x%08lx\n", Status);
         goto done;
@@ -3912,10 +4198,126 @@ NTAPI
 SamrRemoveMemberFromAlias(IN SAMPR_HANDLE AliasHandle,
                           IN PRPC_SID MemberId)
 {
-    UNIMPLEMENTED;
-    return STATUS_NOT_IMPLEMENTED;
+    PSAM_DB_OBJECT AliasObject;
+    LPWSTR MemberIdString = NULL;
+    HANDLE MembersKeyHandle = NULL;
+    HANDLE MemberKeyHandle = NULL;
+    ULONG ulValueCount;
+    NTSTATUS Status;
+
+    TRACE("SamrRemoveMemberFromAlias(%p %p)\n",
+          AliasHandle, MemberId);
+
+    /* Validate the alias handle */
+    Status = SampValidateDbObject(AliasHandle,
+                                  SamDbAliasObject,
+                                  ALIAS_REMOVE_MEMBER,
+                                  &AliasObject);
+    if (!NT_SUCCESS(Status))
+    {
+        TRACE("failed with status 0x%08lx\n", Status);
+        return Status;
+    }
+
+    ConvertSidToStringSidW(MemberId, &MemberIdString);
+    TRACE("Member SID: %S\n", MemberIdString);
+
+    Status = SampRegOpenKey(AliasObject->MembersKeyHandle,
+                            MemberIdString,
+                            KEY_WRITE | KEY_QUERY_VALUE,
+                            &MemberKeyHandle);
+    if (!NT_SUCCESS(Status))
+    {
+        TRACE("SampRegOpenKey failed with status 0x%08lx\n", Status);
+        goto done;
+    }
+
+    Status = SampRegDeleteValue(MemberKeyHandle,
+                                AliasObject->Name);
+    if (!NT_SUCCESS(Status))
+    {
+        TRACE("SampRegDeleteValue failed with status 0x%08lx\n", Status);
+        goto done;
+    }
+
+    Status = SampRegQueryKeyInfo(MemberKeyHandle,
+                                 NULL,
+                                 &ulValueCount);
+    if (!NT_SUCCESS(Status))
+    {
+        TRACE("SampRegQueryKeyInfo failed with status 0x%08lx\n", Status);
+        goto done;
+    }
+
+    if (ulValueCount == 0)
+    {
+        SampRegCloseKey(MemberKeyHandle);
+        MemberKeyHandle = NULL;
+
+        Status = SampRegDeleteKey(AliasObject->MembersKeyHandle,
+                                  MemberIdString);
+        if (!NT_SUCCESS(Status))
+        {
+            TRACE("SampRegDeleteKey failed with status 0x%08lx\n", Status);
+            goto done;
+        }
+    }
+
+    Status = SampRegOpenKey(AliasObject->KeyHandle,
+                            L"Members",
+                            KEY_WRITE | KEY_QUERY_VALUE,
+                            &MembersKeyHandle);
+    if (!NT_SUCCESS(Status))
+    {
+        TRACE("SampRegOpenKey failed with status 0x%08lx\n", Status);
+        goto done;
+    }
+
+    Status = SampRegDeleteValue(MembersKeyHandle,
+                                MemberIdString);
+    if (!NT_SUCCESS(Status))
+    {
+        TRACE("SampRegDeleteValue failed with status 0x%08lx\n", Status);
+        goto done;
+    }
+
+    Status = SampRegQueryKeyInfo(MembersKeyHandle,
+                                 NULL,
+                                 &ulValueCount);
+    if (!NT_SUCCESS(Status))
+    {
+        TRACE("SampRegQueryKeyInfo failed with status 0x%08lx\n", Status);
+        goto done;
+    }
+
+    if (ulValueCount == 0)
+    {
+        SampRegCloseKey(MembersKeyHandle);
+        MembersKeyHandle = NULL;
+
+        Status = SampRegDeleteKey(AliasObject->KeyHandle,
+                                  L"Members");
+        if (!NT_SUCCESS(Status))
+        {
+            TRACE("SampRegDeleteKey failed with status 0x%08lx\n", Status);
+            goto done;
+        }
+    }
+
+done:
+    if (MemberKeyHandle != NULL)
+        SampRegCloseKey(MemberKeyHandle);
+
+    if (MembersKeyHandle != NULL)
+        SampRegCloseKey(MembersKeyHandle);
+
+    if (MemberIdString != NULL)
+        LocalFree(MemberIdString);
+
+    return Status;
 }
 
+
 /* Function 33 */
 NTSTATUS
 NTAPI
@@ -4047,6 +4449,7 @@ done:
     return Status;
 }
 
+
 /* Function 34 */
 NTSTATUS
 NTAPI
@@ -4095,6 +4498,7 @@ SamrOpenUser(IN SAMPR_HANDLE DomainHandle,
     return STATUS_SUCCESS;
 }
 
+
 /* Function 35 */
 NTSTATUS
 NTAPI
@@ -4406,8 +4810,6 @@ done:
 }
 
 
-
-
 static
 NTSTATUS
 SampQueryUserAccount(PSAM_DB_OBJECT UserObject,
@@ -6011,6 +6413,7 @@ SamrCreateUser2InDomain(IN SAMPR_HANDLE DomainHandle,
     return Status;
 }
 
+
 /* Function 51 */
 NTSTATUS
 NTAPI
@@ -6027,26 +6430,63 @@ SamrQueryDisplayInformation3(IN SAMPR_HANDLE DomainHandle,
     return STATUS_NOT_IMPLEMENTED;
 }
 
+
 /* Function 52 */
 NTSTATUS
 NTAPI
 SamrAddMultipleMembersToAlias(IN SAMPR_HANDLE AliasHandle,
                               IN PSAMPR_PSID_ARRAY MembersBuffer)
 {
-    UNIMPLEMENTED;
-    return STATUS_NOT_IMPLEMENTED;
+    ULONG i;
+    NTSTATUS Status = STATUS_SUCCESS;
+
+    TRACE("SamrAddMultipleMembersToAlias(%p %p)\n",
+          AliasHandle, MembersBuffer);
+
+    for (i = 0; i < MembersBuffer->Count; i++)
+    {
+        Status = SamrAddMemberToAlias(AliasHandle,
+                                      ((PSID *)MembersBuffer->Sids)[i]);
+
+        if (Status == STATUS_MEMBER_IN_ALIAS)
+            Status = STATUS_SUCCESS;
+
+        if (!NT_SUCCESS(Status))
+            break;
+    }
+
+    return Status;
 }
 
+
 /* Function 53 */
 NTSTATUS
 NTAPI
 SamrRemoveMultipleMembersFromAlias(IN SAMPR_HANDLE AliasHandle,
                                    IN PSAMPR_PSID_ARRAY MembersBuffer)
 {
-    UNIMPLEMENTED;
-    return STATUS_NOT_IMPLEMENTED;
+    ULONG i;
+    NTSTATUS Status = STATUS_SUCCESS;
+
+    TRACE("SamrRemoveMultipleMembersFromAlias(%p %p)\n",
+          AliasHandle, MembersBuffer);
+
+    for (i = 0; i < MembersBuffer->Count; i++)
+    {
+        Status = SamrRemoveMemberFromAlias(AliasHandle,
+                                           ((PSID *)MembersBuffer->Sids)[i]);
+
+        if (Status == STATUS_MEMBER_IN_ALIAS)
+            Status = STATUS_SUCCESS;
+
+        if (!NT_SUCCESS(Status))
+            break;
+    }
+
+    return Status;
 }
 
+
 /* Function 54 */
 NTSTATUS
 NTAPI