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

#include "includes.h"


NTSTATUS
SamrSrvOpenDomain(
    IN  handle_t        hBinding,
    IN  CONNECT_HANDLE  hConn,
    IN  DWORD           dwAccessMask,
    IN  PSID            pSid,
    OUT DOMAIN_HANDLE  *phDomain
    )
{
    NTSTATUS ntStatus = STATUS_SUCCESS;
    DWORD dwError = ERROR_SUCCESS;
    PCONNECT_CONTEXT pConnCtx = NULL;
    SAMDB_QUERY SamDbQuery = { .Sid = NULL };
    PSAMDB_OBJECT_INFO *ppObjects = NULL;
    PSAMDB_OBJECT_INFO pObject = NULL;
    PDOMAIN_CONTEXT pDomainCtx = NULL;
    SECURITY_INFORMATION securityInformation =
        OWNER_SECURITY_INFORMATION |
        GROUP_SECURITY_INFORMATION |
        DACL_SECURITY_INFORMATION;
    PSECURITY_DESCRIPTOR_RELATIVE securityDescriptorRelative = NULL;
    PSECURITY_DESCRIPTOR_ABSOLUTE securityDescriptor = NULL;
    GENERIC_MAPPING genericMapping = {0};
    DWORD accessGranted = 0;
    PWSTR domainName = NULL;
    PSID domainSid = NULL;

    BAIL_ON_INVALID_PTR(hBinding);
    BAIL_ON_INVALID_PTR(hConn);
    BAIL_ON_INVALID_PTR(pSid);

    pConnCtx = (PCONNECT_CONTEXT)hConn;

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

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

    SamDbQuery.Sid = pSid;
    dwError = SamDbFindObjects(SAMDB_OBJECT_TYPE_DOMAIN,
                               SAMDB_QUERY_TYPE_SID,
                               1,
                               &SamDbQuery,
                               &ppObjects);
    BAIL_ON_LSA_ERROR(dwError);

    pObject = ppObjects[0];
    if (pObject == NULL)
    {
        ntStatus = STATUS_NO_SUCH_DOMAIN;
        BAIL_ON_NT_STATUS(ntStatus);
    }

    dwError = LwAllocateMemory(sizeof(*pDomainCtx),
                               OUT_PPVOID(&pDomainCtx));
    BAIL_ON_NT_STATUS(ntStatus);

    dwError = SamDbQuerySecurityDescriptor(
                               pSid,
                               securityInformation,
                               &securityDescriptorRelative);
    BAIL_ON_LSA_ERROR(dwError);

    dwError = SamrSrvAllocateAbsoluteFromSelfRelativeSD(
                               securityDescriptorRelative,
                               &securityDescriptor);
    BAIL_ON_LSA_ERROR(dwError);

    if (!RtlAccessCheck(securityDescriptor,
                        pConnCtx->pUserToken,
                        dwAccessMask,
                        pDomainCtx->dwAccessGranted,
                        &genericMapping,
                        &accessGranted,
                        &ntStatus))
    {
        BAIL_ON_NT_STATUS(ntStatus);
    }

    dwError = LwAllocateWc16String(&domainName,
                                   pObject->Name);
    BAIL_ON_LSA_ERROR(dwError);

    ntStatus = RtlDuplicateSid(&domainSid, pObject->Sid);
    BAIL_ON_NT_STATUS(ntStatus);

    pDomainCtx->Type =  SamrContextDomain;
    pDomainCtx->refcount = 1;
    pDomainCtx->dwAccessGranted = accessGranted;
    pDomainCtx->pwszDomainName = domainName;
    pDomainCtx->pDomainSid = domainSid;
    pDomainCtx->ntMinPasswordAge = pObject->Info.Domain.MinPasswordAge;
    pDomainCtx->ntMaxPasswordAge = pObject->Info.Domain.MaxPasswordAge;
    pDomainCtx->dwMinPasswordLen = pObject->Info.Domain.MinPasswordLength;

    pDomainCtx->pConnCtx = pConnCtx;
    InterlockedIncrement(&pConnCtx->refcount);

    *phDomain = (DOMAIN_HANDLE)pDomainCtx;

error:
    if (ntStatus || dwError)
    {
        *phDomain = NULL;
    }

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

    if (securityDescriptor)
    {
        SamrSrvFreeSecurityDescriptor(&securityDescriptor);
    }

    if (securityDescriptorRelative)
    {
        SamDbFree(securityDescriptorRelative);
    }

    return ntStatus;
}


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