/* Editor Settings: expandtabs and use 4 spaces for indentation
 * ex: set softtabstop=4 tabstop=8 expandtab shiftwidth=4: *
 */

/*
 * Copyright Likewise Software    2004-2008
 * All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the license, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.  You should have received a copy
 * of the GNU Lesser General Public License along with this program.  If
 * not, see <http://www.gnu.org/licenses/>.
 *
 * LIKEWISE SOFTWARE MAKES THIS SOFTWARE AVAILABLE UNDER OTHER LICENSING
 * TERMS AS WELL.  IF YOU HAVE ENTERED INTO A SEPARATE LICENSE AGREEMENT
 * WITH LIKEWISE SOFTWARE, THEN YOU MAY ELECT TO USE THE SOFTWARE UNDER THE
 * TERMS OF THAT SOFTWARE LICENSE AGREEMENT INSTEAD OF THE TERMS OF THE GNU
 * LESSER GENERAL PUBLIC LICENSE, NOTWITHSTANDING THE ABOVE NOTICE.  IF YOU
 * HAVE QUESTIONS, OR WISH TO REQUEST A COPY OF THE ALTERNATE LICENSING
 * TERMS OFFERED BY LIKEWISE SOFTWARE, PLEASE CONTACT LIKEWISE SOFTWARE AT
 * license@likewisesoftware.com
 */

/*
 * Copyright (C) Likewise Software. All rights reserved.
 *
 * Module Name:
 *
 *        samr_lookuprids.c
 *
 * Abstract:
 *
 *        Remote Procedure Call (RPC) Server Interface
 *
 *        SamrLookupRids function
 *
 * Authors: Rafal Szczesniak (rafal@likewise.com)
 */

#include "includes.h"

// KKJA:  This originally behaved differently from SamrSrvLookupNames().
//        Instead of returning a full-length array with blanks for unknown
//        objects, it counted fewer objects.  This was wrong according to
//        MS-SAMR 3.1.5.11.3

NTSTATUS
SamrSrvLookupRids(
    IN  handle_t IDL_handle,
    IN  DOMAIN_HANDLE hDomain,
    IN  UINT32 dwNumRids,
    IN  UINT32 *pdwRids,
    OUT UNICODE_STRING_ARRAY *pNames,
    OUT IDS *pTypes
    )
{
    NTSTATUS ntStatus = STATUS_SUCCESS;
    DWORD dwError = 0;
    PDOMAIN_CONTEXT pDomCtx = NULL;
    DWORD i = 0;
    PSAMDB_QUERY pQueries = NULL;
    PSAMDB_QUERY pQuery = NULL;
    PSAMDB_OBJECT_INFO *ppObjects = NULL;
    PSAMDB_OBJECT_INFO pObject = NULL;
    LsaSidType Type = SID_TYPE_UNKNOWN;
    PSID pDomSid = NULL;
    DWORD dwSidLen = 0;
    PSID pSid = NULL;
    UNICODE_STRING_ARRAY Names = {0};
    IDS Types = {0};
    DWORD dwUnknownRidsNum = 0;

    if (pNames == NULL || pTypes == NULL)
    {
        ntStatus = STATUS_INVALID_PARAMETER;
        BAIL_ON_NT_STATUS(ntStatus);
    }

    pDomCtx    = (PDOMAIN_CONTEXT)hDomain;
    pDomSid    = pDomCtx->pDomainSid;

    dwSidLen = RtlLengthRequiredSid(pDomSid->SubAuthorityCount + 1);

    ntStatus = SamrSrvAllocateMemory(OUT_PPVOID(&Types.pIds),
                                   sizeof(*Types.pIds) * dwNumRids);
    BAIL_ON_NT_STATUS(ntStatus);
    Types.dwCount = dwNumRids;

    ntStatus = SamrSrvAllocateMemory(OUT_PPVOID(&Names.pNames),
                                   sizeof(*Names.pNames) * dwNumRids);
    BAIL_ON_NT_STATUS(ntStatus);
    Names.dwCount = 0;

    dwError = LwAllocateMemory(
                          sizeof(*pQueries) * dwNumRids,
                          OUT_PPVOID(&pQueries));
    BAIL_ON_LSA_ERROR(dwError);

    for (i = 0; i < dwNumRids; i++)
    {
        dwError = LwAllocateMemory(
                              dwSidLen,
                              OUT_PPVOID(&pSid));
        BAIL_ON_LSA_ERROR(dwError);

        ntStatus = RtlCopySid(dwSidLen,
                              pSid,
                              pDomSid);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = RtlAppendRidSid(
                              dwSidLen,
                              pSid,
                              pdwRids[i]);
        BAIL_ON_NT_STATUS(ntStatus);

        pQuery = &(pQueries[i]);
        pQuery->Sid = pSid;
    }

    dwError = SamDbFindObjects(SAMDB_OBJECT_TYPE_UNKNOWN,
                               SAMDB_QUERY_TYPE_SID,
                               dwNumRids,
                               pQueries,
                               &ppObjects);
    BAIL_ON_LSA_ERROR(dwError);

    for (i = 0; i < dwNumRids; i++)
    {
        pObject = ppObjects[i];

        if (pObject)
        {
            ntStatus = SamrSrvInitUnicodeString(
                                  &Names.pNames[i],
                                  pObject->Name);
            BAIL_ON_NT_STATUS(ntStatus);

            switch (pObject->Type)
            {
            case SAMDB_OBJECT_TYPE_USER:
                Type = SID_TYPE_USER;
                break;

            case SAMDB_OBJECT_TYPE_LOCAL_GROUP:
                Type = SID_TYPE_ALIAS;
                break;

            default:
                Type = SID_TYPE_INVALID;
                break;
            }
        }
        else
        {
            Type = SID_TYPE_UNKNOWN;
            dwUnknownRidsNum++;
        }

        Types.pIds[i] = Type;

    }

    if (dwUnknownRidsNum > 0)
    {
        if (Names.dwCount > 0)
        {
            ntStatus = STATUS_SOME_NOT_MAPPED;
        }
        else
        {
            ntStatus = STATUS_NONE_MAPPED;
        }
    }

    Names.dwCount = dwNumRids;
    Types.dwCount = dwNumRids;

    pNames->pNames   = Names.pNames;
    pNames->dwCount  = Names.dwCount;
    pTypes->pIds     = Types.pIds;
    pTypes->dwCount  = Types.dwCount;

error:
    if (ntStatus == STATUS_SUCCESS &&
        dwError != ERROR_SUCCESS)
    {
        ntStatus = LwWin32ErrorToNtStatus(dwError);
    }

    if (ntStatus &&
        ntStatus != STATUS_SOME_NOT_MAPPED &&
        ntStatus != STATUS_NONE_MAPPED)
    {
        if (Types.pIds)
        {
            SamrSrvFreeMemory(Types.pIds);
        }

        for (i = 0; i < Names.dwCount; i++)
        {
            if (Names.pNames[i].Buffer)
            {
                SamrSrvFreeUnicodeString(&(Names.pNames[i]));
            }
        }

        SamrSrvFreeMemory(Names.pNames);
        SamrSrvFreeMemory(Types.pIds);

        pNames->pNames   = NULL;
        pNames->dwCount  = 0;
        pTypes->pIds     = NULL;
        pTypes->dwCount  = 0;
    }

    if (ppObjects)
    {
        SamDbFreeObjects(dwNumRids, ppObjects);
    }

    if (pQueries)
    {
        for (i = 0; i < dwNumRids; i++)
        {
            LW_SAFE_FREE_MEMORY(pQueries[i].Sid);
        }
    }
    LW_SAFE_FREE_MEMORY(pQueries);

    return ntStatus;
}


/*
local variables:
mode: c
c-basic-offset: 4
indent-tabs-mode: nil
tab-width: 4
end:
*/
