/* 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_getaliasmembership.c
 *
 * Abstract:
 *
 *        Remote Procedure Call (RPC) Server Interface
 *
 *        SamrGetAliasMembership function
 *
 * Authors: Rafal Szczesniak (rafal@likewise.com)
 */

#include "includes.h"


static
VOID
FreeSid(
    PVOID pData,
    PVOID pUserData
    );


static
VOID
SetRidsArray(
    PVOID pData,
    PVOID pUserData
    );


NTSTATUS
SamrSrvGetAliasMembership(
    IN  handle_t       hBinding,
    IN  DOMAIN_HANDLE  hDomain,
    IN  SID_ARRAY     *pSids,
    OUT IDS           *pRids
    )
{
    NTSTATUS ntStatus = STATUS_SUCCESS;
    PCONNECT_CONTEXT pConnCtx = NULL;
    PDOMAIN_CONTEXT pDomCtx = NULL;
    DWORD dwError = 0;
    PSID pSid = NULL;
    PSID* aliases = NULL;
    DWORD i = 0;
    DWORD j = 0;
    PDLINKEDLIST pSidList = NULL;
    DWORD aliasesCount = 0;
    DWORD dwSidCount = 0;
    IDS Rids = {0};

    pDomCtx = (PDOMAIN_CONTEXT)hDomain;

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

    /* Check access rights required */
    if (!(pDomCtx->dwAccessGranted & DOMAIN_ACCESS_OPEN_ACCOUNT))
    {
        ntStatus = STATUS_ACCESS_DENIED;
        BAIL_ON_NT_STATUS(ntStatus);
    }

    pConnCtx   = pDomCtx->pConnCtx;

    // For each group, grab the SIDs and add them to the list
    for (i = 0; i < pSids->dwNumSids; i++)
    {
        dwError = SamDbQueryMemberOf(pSids->pSids[i].pSid,
                                     &aliasesCount,
                                     &aliases);
        BAIL_ON_LSA_ERROR(dwError);

        for (j = 0; j < aliasesCount; j++)
        {
            if (RtlIsPrefixSid(pDomCtx->pDomainSid, aliases[j]))
            {
                ntStatus = RtlDuplicateSid(&pSid, aliases[j]);
                BAIL_ON_NT_STATUS(ntStatus);

                dwError = LsaDLinkedListAppend(&pSidList, pSid);
                BAIL_ON_LSA_ERROR(dwError);
            }
        }

        if (aliases)
        {
            SamDbFreeSidList(aliasesCount, aliases);
            aliases = NULL;
            aliasesCount = 0;
        }
    }

    dwSidCount = LsaDLinkedListLength(pSidList);

    ntStatus = SamrSrvAllocateMemory(
                           OUT_PPVOID(&Rids.pIds),
                           sizeof(Rids.pIds[0]) * dwSidCount);
    BAIL_ON_NT_STATUS(ntStatus);

    LsaDLinkedListForEach(pSidList, SetRidsArray, (PVOID)&Rids);

    pRids->dwCount = Rids.dwCount;
    pRids->pIds    = Rids.pIds;

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

    if (ntStatus)
    {
        if (Rids.pIds)
        {
            SamrSrvFreeMemory(Rids.pIds);
        }
        pRids->dwCount = 0;
        pRids->pIds    = NULL;

    }

    if (aliases)
    {
        SamDbFreeSidList(aliasesCount, aliases);
    }

    LsaDLinkedListForEach(pSidList, FreeSid, NULL);
    LsaDLinkedListFree(pSidList);

    return ntStatus;
}


static
VOID
FreeSid(
    PVOID pData,
    PVOID pUserData
    )
{
    PSID pSid = (PSID)pData;
    RTL_FREE(&pSid);
}


static
VOID
SetRidsArray(
    PVOID pData,
    PVOID pUserData
    )
{
    PSID pAliasSid = (PSID)pData;
    IDS *pRids = (IDS*)pUserData;
    DWORD i = pRids->dwCount;

    pRids->pIds[i]  = pAliasSid->SubAuthority[pAliasSid->SubAuthorityCount - 1];
    pRids->dwCount  = ++i;
}


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