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

/*
 * Copyright Likewise Software    2004-2009
 * 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_lookupnames.c
 *
 * Abstract:
 *
 *        Remote Procedure Call (RPC) Server Interface
 *
 *        SamrLookupNames function
 *
 * Authors: Rafal Szczesniak (rafal@likewise.com)
 */

#include "includes.h"


NTSTATUS
SamrSrvLookupNames(
    IN  handle_t        hBinding,
    IN  DOMAIN_HANDLE   hDomain,
    IN  DWORD           dwNumNames,
    IN  UNICODE_STRING *pNames,
    OUT IDS            *pOutIds,
    OUT IDS            *pOutTypes
    )
{
    NTSTATUS ntStatus = STATUS_SUCCESS;
    DWORD dwError = ERROR_SUCCESS;
    PDOMAIN_CONTEXT pDomCtx = NULL;
    PIDS pIds = NULL;
    PIDS pTypes = NULL;
    PSAMDB_QUERY queries = NULL;
    PSAMDB_QUERY query = NULL;
    PSAMDB_OBJECT_INFO *ppObjects = NULL;
    PSAMDB_OBJECT_INFO pObject = NULL;
    DWORD objectIndex = 0;
    DWORD nameIndex = 0;
    UNICODE_STRING *pName = NULL;
    DWORD rid = 0;
    DWORD dwType = 0;
    DWORD dwUnknownNamesNum = 0;
    PSID objectSid = NULL;


    pDomCtx = (PDOMAIN_CONTEXT)hDomain;

    if (pDomCtx == NULL || pDomCtx->Type != SamrContextDomain)
    {
        ntStatus = STATUS_INVALID_HANDLE;
        BAIL_ON_NT_STATUS(ntStatus);
    }
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = SamrSrvAllocateMemory(OUT_PPVOID(&pIds), sizeof(*pIds));
    BAIL_ON_NT_STATUS(ntStatus);

    pIds->dwCount = dwNumNames;
    ntStatus = SamrSrvAllocateMemory(OUT_PPVOID(&pIds->pIds),
                                     pIds->dwCount * sizeof(*pIds->pIds));
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = SamrSrvAllocateMemory(OUT_PPVOID(&pTypes), sizeof(*pTypes));
    BAIL_ON_NT_STATUS(ntStatus);

    pTypes->dwCount = dwNumNames;
    ntStatus = SamrSrvAllocateMemory(OUT_PPVOID(&pTypes->pIds),
                                     pTypes->dwCount * sizeof(*pTypes->pIds));
    BAIL_ON_NT_STATUS(ntStatus);

    dwError = LwAllocateMemory(
                        sizeof(*queries) * dwNumNames,
                        OUT_PPVOID(&queries));
    BAIL_ON_LSA_ERROR(dwError);

    for (objectIndex = 0; objectIndex < dwNumNames; objectIndex++)
    {
        query = &(queries[objectIndex]);
        pName = &(pNames[objectIndex]);

        dwError = LwAllocateWc16StringFromUnicodeString(
                                   &query->Name,
                                   pName);
        BAIL_ON_LSA_ERROR(dwError);
    }

    dwError = SamDbFindObjects(SAMDB_OBJECT_TYPE_UNKNOWN,
                               SAMDB_QUERY_TYPE_NT_NAME,
                               dwNumNames,
                               queries,
                               &ppObjects);
    BAIL_ON_LSA_ERROR(dwError);

    for (objectIndex = 0;
         objectIndex < dwNumNames && nameIndex < dwNumNames;
         objectIndex++)
    {
        pObject = ppObjects[objectIndex];
        if (pObject == NULL)
        {
            rid = 0;
            dwType = SID_TYPE_UNKNOWN;

            dwUnknownNamesNum++;
        }
        else
        {
            objectSid = pObject->Sid;

            //
            // Object found must be in the same domain as the domain
            // of the context passed
            //
            if (!RtlIsStrictPrefixSid(
                           pDomCtx->pDomainSid,
                           objectSid))
            {
                dwUnknownNamesNum++;
                continue;
            }

            rid = objectSid->SubAuthority[objectSid->SubAuthorityCount - 1];
            switch (pObject->Type)
            {
            case SAMDB_OBJECT_TYPE_USER:
                dwType = SID_TYPE_USER;
                break;

            case SAMDB_OBJECT_TYPE_LOCAL_GROUP:
                dwType = SID_TYPE_ALIAS;
                break;

            default:
                dwType = SID_TYPE_INVALID;
                break;
            }
        }

        pIds->pIds[nameIndex] = rid;
        pTypes->pIds[nameIndex] = dwType;

        nameIndex++;
    }

    if (dwUnknownNamesNum > 0)
    {
        if (dwUnknownNamesNum < pTypes->dwCount)
        {
            ntStatus = LW_STATUS_SOME_NOT_MAPPED;
        }
        else
        {
            ntStatus = STATUS_NONE_MAPPED;
        }
    }

    *pOutIds = *pIds;
    *pOutTypes = *pTypes;

error:
    if (ntStatus &&
        ntStatus != STATUS_SOME_NOT_MAPPED &&
        ntStatus != STATUS_NONE_MAPPED)
    {
        if (pIds)
        {
            if (pIds->pIds)
            {
                SamrSrvFreeMemory(pIds->pIds);
            }
            SamrSrvFreeMemory(pIds);
        }

        if (pTypes)
        {
            if (pTypes->pIds)
            {
                SamrSrvFreeMemory(pTypes->pIds);
            }
            SamrSrvFreeMemory(pTypes);
        }

        pOutIds = NULL;
        pOutTypes = NULL;
    }

    if (queries)
    {
        for (objectIndex = 0; objectIndex < dwNumNames; objectIndex++)
        {
            LW_SAFE_FREE_MEMORY(queries[objectIndex].Name);
        }
        LW_SAFE_FREE_MEMORY(queries);
    }

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

    return ntStatus;
}


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