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

/*
 * Copyright Likewise Software
 * 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
 */

#include "includes.h"


static
BOOLEAN
CallOpenSchannel(
    NETR_BINDING hNetr,
    PWSTR pwszMachineAccount,
    PCWSTR pwszMachineName,
    PWSTR pwszServer,
    PWSTR pwszDomain,
    PWSTR pwszDnsDomain,
    PWSTR pwszComputer,
    PWSTR pwszMachpass,
    NetrCredentials *pCreds,
    NETR_BINDING *phSchn
    );

static
BOOLEAN
CallCloseSchannel(
    NETR_BINDING  hSchannel
    );

static
BOOLEAN
CallNetrSamLogonInteractive(
    NETR_BINDING           hSchannel,
    NetrCredentials       *pCreds,
    PWSTR                  pwszServer,
    PWSTR                  pwszDomain,
    PWSTR                  pwszComputer,
    PWSTR                  pwszWorkstation,
    PWSTR                  pwszUsername,
    PWSTR                  pwszPassword,
    DWORD                  logonLevel,
    PDWORD                 pValidationLevels,
    DWORD                  numValidationLevels,
    NetrValidationInfo  ***pppSamLogonInfo
    );

static
BOOLEAN
CallNetrSamLogonInteractiveEx(
    NETR_BINDING           hSchannel,
    NetrCredentials       *pCreds,
    PWSTR                  pwszServer,
    PWSTR                  pwszDomain,
    PWSTR                  pwszComputer,
    PWSTR                  pwszWorkstation,
    PWSTR                  pwszUsername,
    PWSTR                  pwszPassword,
    DWORD                  logonLevel,
    PDWORD                 pValidationLevels,
    DWORD                  numValidationLevels,
    NetrValidationInfo  ***pppSamLogonInfo
    );

static
BOOLEAN
CallNetrSamLogoff(
    NETR_BINDING      hSchannel,
    NetrCredentials  *pCreds,
    PWSTR             pwszServer,
    PWSTR             pwszDomain,
    PWSTR             pwszComputer,
    PWSTR             pwszWorkstation,
    PWSTR             pwszUsername,
    PWSTR             pwszPassword,
    DWORD             dwLevels
    );

static
BOOLEAN
TestValidateSamLogonInfo(
    NetrValidationInfo ***pLogonInfo,
    DWORD                 dwNumLogonInfos
    );

static
BOOLEAN
TestValidateDomainTrusts(
    NetrDomainTrust      *pTrust,
    DWORD                 dwNumTrusts
    );

static
DWORD
TestNetlogonSamLogonInteractive(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    );

static
DWORD
TestNetlogonEnumTrustedDomains(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    );

static
DWORD
TestNetlogonEnumDomainTrusts(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    );

static
DWORD
TestNetlogonGetDcName(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    );


static
BOOLEAN
CallOpenSchannel(
    NETR_BINDING      hNetr,
    PWSTR             pwszMachineAccount,
    PCWSTR            pwszMachineName,
    PWSTR             pwszServer,
    PWSTR             pwszDomain,
    PWSTR             pwszDnsDomain,
    PWSTR             pwszComputer,
    PWSTR             pwszMachpass,
    NetrCredentials  *pCreds,
    PNETR_BINDING     phSchn
    )
{
    BOOLEAN bRet = TRUE;
    NTSTATUS ntStatus = STATUS_SUCCESS;
    NETR_BINDING hSchn = NULL;

    CALL_NT_API(ntStatus, NetrOpenSchannel(
                                  hNetr,
                                  pwszMachineAccount,
                                  pwszMachineName,
                                  pwszServer,
                                  pwszDomain,
                                  pwszDnsDomain,
                                  pwszComputer,
                                  pwszMachpass,
                                  pCreds,
                                  &hSchn));
    if (ntStatus)
    {
        bRet = FALSE;
    }

    *phSchn = hSchn;

    return bRet;
}


static
BOOLEAN
CallCloseSchannel(
    NETR_BINDING  hSchannel
    )
{
    BOOLEAN bRet = TRUE;

    NetrFreeBinding(&hSchannel);

    LwIoSetThreadCreds(NULL);

    return bRet;
}


static
BOOLEAN
CallNetrSamLogonInteractive(
    NETR_BINDING           hSchannel,
    NetrCredentials       *pCreds,
    PWSTR                  pwszServer,
    PWSTR                  pwszDomain,
    PWSTR                  pwszComputer,
    PWSTR                  pwszWorkstation,
    PWSTR                  pwszUsername,
    PWSTR                  pwszPassword,
    DWORD                  logonLevel,
    PDWORD                 pValidationLevels,
    DWORD                  numValidationLevels,
    NetrValidationInfo  ***pppSamLogonInfo
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    NTSTATUS ntStatus = STATUS_SUCCESS;
    DWORD validationLevel = 0;
    DWORD i = 0;
    NetrValidationInfo *pValidationInfo = NULL;
    NetrValidationInfo **ppSamLogonInfo = NULL;
    BYTE Authoritative = 0;
    DWORD totalNumLevels = 7;

    error = LwAllocateMemory(sizeof(ppSamLogonInfo[0]) * totalNumLevels,
                               OUT_PPVOID(&ppSamLogonInfo));
    BAIL_ON_WIN_ERROR(error);

    for (i = 0; i < numValidationLevels; i++)
    {
        validationLevel = pValidationLevels[i];

        CALL_NT_API(ntStatus, NetrSamLogonInteractive(
                                          hSchannel,
                                          pCreds,
                                          pwszServer,
                                          pwszDomain,
                                          pwszComputer,
                                          pwszWorkstation,
                                          pwszUsername,
                                          pwszPassword,
                                          logonLevel,
                                          validationLevel,
                                          &pValidationInfo,
                                          &Authoritative));
        if (ntStatus)
        {
            bRet = FALSE;
        }

        ppSamLogonInfo[validationLevel] = pValidationInfo;
    }

    *pppSamLogonInfo = ppSamLogonInfo;

error:
    return bRet;
}


static
BOOLEAN
CallNetrSamLogonInteractiveEx(
    NETR_BINDING           hSchannel,
    NetrCredentials      *pNetlogonCreds,
    PWSTR                  pwszServer,
    PWSTR                  pwszDomain,
    PWSTR                  pwszComputer,
    PWSTR                  pwszWorkstation,
    PWSTR                  pwszUsername,
    PWSTR                  pwszPassword,
    DWORD                  logonLevel,
    PDWORD                 pValidationLevels,
    DWORD                  numValidationLevels,
    NetrValidationInfo  ***pppSamLogonInfo
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    NTSTATUS ntStatus = STATUS_SUCCESS;
    DWORD validationLevel = 0;
    DWORD i = 0;
    NetrValidationInfo *pValidationInfo = NULL;
    NetrValidationInfo **ppSamLogonInfo = NULL;
    BYTE Authoritative = 0;
    DWORD totalNumLevels = 7;

    error = LwAllocateMemory(sizeof(ppSamLogonInfo[0]) * totalNumLevels,
                               OUT_PPVOID(&ppSamLogonInfo));
    BAIL_ON_WIN_ERROR(error);

    for (i = 0; i < numValidationLevels; i++)
    {
        validationLevel = pValidationLevels[i];

        CALL_NT_API(ntStatus, NetrSamLogonEx(
                                          hSchannel,
                                          pNetlogonCreds,
                                          pwszServer,
                                          pwszDomain,
                                          pwszComputer,
                                          pwszWorkstation,
                                          pwszUsername,
                                          pwszPassword,
                                          logonLevel,
                                          validationLevel,
                                          &pValidationInfo,
                                          &Authoritative));
        if (ntStatus)
        {
            bRet = FALSE;
        }

        ppSamLogonInfo[validationLevel] = pValidationInfo;
    }

    *pppSamLogonInfo = ppSamLogonInfo;

error:
    return bRet;
}


static
BOOLEAN
CallNetrSamLogoff(
    NETR_BINDING      hSchannel,
    NetrCredentials  *pCreds,
    PWSTR             pwszServer,
    PWSTR             pwszDomain,
    PWSTR             pwszComputer,
    PWSTR             pwszWorkstation,
    PWSTR             pwszUsername,
    PWSTR             pwszPassword,
    DWORD             dwLevel
    )
{
    BOOLEAN bRet = TRUE;
    NTSTATUS ntStatus = STATUS_SUCCESS;

    CALL_NT_API(ntStatus, NetrSamLogoff(
                                 hSchannel,
                                 pCreds,
                                 pwszServer,
                                 pwszDomain,
                                 pwszComputer,
                                 pwszWorkstation,
                                 pwszUsername,
                                 pwszPassword,
                                 dwLevel));
    if (ntStatus)
    {
        bRet = FALSE;
    }

    return bRet;
}


static
BOOLEAN
TestValidateSamLogonInfo(
    NetrValidationInfo ***pppLogonInfo,
    DWORD                 dwNumUsers
    )
{
    BOOLEAN bRet = TRUE;
    DWORD i = 0;

    for (i = 0; i < dwNumUsers; i++)
    {
        NetrValidationInfo **ppLogonInfo = pppLogonInfo[i];

        ASSERT_UNICODE_STRING_VALID_MSG(&ppLogonInfo[2]->sam2->base.account_name, ("(i = %u)\n", i));
        ASSERT_UNICODE_STRING_VALID_MSG(&ppLogonInfo[2]->sam2->base.full_name, ("(i = %u)\n", i));
        ASSERT_UNICODE_STRING_VALID_MSG(&ppLogonInfo[2]->sam2->base.logon_script, ("(i = %u)\n", i));
        ASSERT_UNICODE_STRING_VALID_MSG(&ppLogonInfo[2]->sam2->base.profile_path, ("(i = %u)\n", i));
        ASSERT_UNICODE_STRING_VALID_MSG(&ppLogonInfo[2]->sam2->base.home_directory, ("(i = %u)\n", i));
        ASSERT_UNICODE_STRING_VALID_MSG(&ppLogonInfo[2]->sam2->base.home_drive, ("(i = %u)\n", i));
        ASSERT_UNICODE_STRING_VALID_MSG(&ppLogonInfo[2]->sam2->base.logon_server, ("(i = %u)\n", i));
        ASSERT_UNICODE_STRING_VALID_MSG(&ppLogonInfo[2]->sam2->base.domain, ("(i = %u)\n", i));
        ASSERT_TEST_MSG((ppLogonInfo[2]->sam2->base.domain_sid != NULL &&
                         RtlValidSid(ppLogonInfo[2]->sam2->base.domain_sid)), ("(i = %u)\n", i));
        ASSERT_TEST_MSG(ppLogonInfo[2]->sam2->base.acct_flags & ACB_NORMAL, ("(i = %u)\n", i));
    }

    return bRet;
}


static
BOOLEAN
TestValidateDomainTrusts(
    NetrDomainTrust      *pTrusts,
    DWORD                 dwNumTrusts
    )
{
    BOOLEAN bRet = TRUE;
    DWORD i = 0;

    for (i = 0; i < dwNumTrusts; i++)
    {
        NetrDomainTrust *pTrust = &(pTrusts[i]);

        ASSERT_TEST_MSG(pTrust->netbios_name != NULL, ("(i = %u)\n", i));
        ASSERT_TEST_MSG(pTrust->dns_name != NULL, ("(i = %u)\n", i));
        ASSERT_TEST_MSG((pTrust->trust_type >= 1 &&
                         pTrust->trust_type <= 4), ("(i = %u)\n", i));
        ASSERT_TEST_MSG((pTrust->sid != NULL &&
                         RtlValidSid(pTrust->sid)), ("(i = %u)\n", i));
    }

    return bRet;
}


static
DWORD
TestNetlogonSamLogonInteractive(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    PCSTR pszDefComputer = "TestWks4";
    PCSTR pszDefMachpass = "secret01$";
    const DWORD dwDefLogonLevel = 0;
    const DWORD dwDefValidationLevel = 2;

    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    NETR_BINDING hNetr = NULL;
    NETR_BINDING hSchn = NULL;
    NetrCredentials Creds = {0};
    NetrValidationInfo **ppSamLogonInfo = NULL;
    PSTR pszComputer = NULL;
    PSTR pszMachpass = NULL;
    PWSTR pwszComputer = NULL;
    PWSTR pwszWorkstation = NULL;
    PWSTR pwszMachAcct = NULL;
    PWSTR pwszMachpass = NULL;
    PWSTR pwszServer = NULL;
    PWSTR pwszDomain = NULL;
    PWSTR pwszDnsDomain = NULL;
    PWSTR pwszUsername = NULL;
    PWSTR pwszPassword = NULL;
    DWORD dwLogonLevel = 0;
    DWORD dwValidationLevel = 0;
    DWORD pDefaultLogonLevels[] = { 1, 3, 4};
    DWORD numLogonLevels = (sizeof(pDefaultLogonLevels)
                            /sizeof(pDefaultLogonLevels[0]));
    PDWORD pLogonLevels = NULL;
    DWORD pDefaultValidationLevels[] = { 2, 3, 6 };
    DWORD numValidationLevels = (sizeof(pDefaultValidationLevels)
                                 /sizeof(pDefaultValidationLevels[0]));
    PDWORD pValidationLevels = NULL;
    DWORD iLogonLevel = 0;
    DWORD iValidationLevel = 0;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "computer", PT_WC16STRING, &pwszComputer,
                       &pszDefComputer);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "machpass", PT_WC16STRING, &pwszMachpass,
                       &pszDefMachpass);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "server", PT_WC16STRING, &pwszServer,
                       NULL);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "domain", PT_WC16STRING, &pwszDomain,
                       NULL);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "dnsdomain", PT_WC16STRING, &pwszDnsDomain,
                       NULL);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "workstation", PT_WC16STRING, &pwszWorkstation,
                       &pszDefComputer);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "username", PT_WC16STRING, &pwszUsername,
                       NULL);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "password", PT_WC16STRING, &pwszPassword,
                       NULL);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "logon_level", PT_UINT32, &dwLogonLevel,
                       &dwDefLogonLevel);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "validation_level", PT_UINT32,
                       &dwValidationLevel, &dwDefValidationLevel);
    BAIL_ON_WIN_ERROR(error);

    error = LwWc16sToMbs(pwszComputer, &pszComputer);
    BAIL_ON_WIN_ERROR(error);

    error = LwWc16sToMbs(pwszMachpass, &pszMachpass);
    BAIL_ON_WIN_ERROR(error);

    if (strcmp(pszComputer, pszDefComputer) == 0 &&
        strcmp(pszMachpass, pszDefMachpass) == 0)
    {
        LW_SAFE_FREE_MEMORY(pwszComputer);
        LW_SAFE_FREE_MEMORY(pwszMachpass);

        error = GetMachinePassword(
                        &pwszDnsDomain,
                        &pwszDomain,
                        &pwszMachAcct,
                        &pwszMachpass,
                        &pwszComputer);
        BAIL_ON_WIN_ERROR(error);
    }

    bRet &= CreateRpcBinding(OUT_PPVOID(&hNetr),
                             RPC_NETLOGON_BINDING,
                             Server,
                             pCreds);

    bRet &= CallOpenSchannel(hNetr,
                             pwszMachAcct,
                             Server->Hostname,
                             pwszServer,
                             pwszDomain,
                             pwszDnsDomain,
                             pwszComputer,
                             pwszMachpass,
                             &Creds,
                             &hSchn);

    if (hSchn == NULL)
    {
        bRet = FALSE;
        goto done;
    }

    if (dwLogonLevel)
    {
        pLogonLevels = &dwLogonLevel;
        numLogonLevels = 1;
    }
    else
    {
        pLogonLevels = pDefaultLogonLevels;
    }

    if (dwValidationLevel)
    {
        pValidationLevels = &dwValidationLevel;
        numValidationLevels = 1;
    }
    else
    {
        pValidationLevels = pDefaultValidationLevels;
    }

    for (iLogonLevel = 0; iLogonLevel < numLogonLevels; iLogonLevel++)
    {
        bRet &= CallNetrSamLogonInteractive(
                              hSchn,
                              &Creds,
                              pwszServer,
                              pwszDomain,
                              pwszComputer,
                              pwszWorkstation,
                              pwszUsername,
                              pwszPassword,
                              pLogonLevels[iLogonLevel],
                              pValidationLevels,
                              numValidationLevels,
                              &ppSamLogonInfo);

        if (bRet)
        {
            bRet &= TestValidateSamLogonInfo(&ppSamLogonInfo, 1);

            bRet &= CallNetrSamLogoff(
                                  hSchn,
                                  &Creds,
                                  pwszServer,
                                  pwszDomain,
                                  pwszComputer,
                                  pwszWorkstation,
                                  pwszUsername,
                                  pwszPassword,
                                  pLogonLevels[iLogonLevel]);

            for (iValidationLevel = 0;
                 ppSamLogonInfo && iValidationLevel <= 6;
                 iValidationLevel++)
            {
                if (ppSamLogonInfo[iValidationLevel])
                {
                    NetrFreeMemory(ppSamLogonInfo[iValidationLevel]);
                    ppSamLogonInfo[iValidationLevel] = NULL;
                }
            }
        }
    }

    bRet &= CallCloseSchannel(hSchn);

done:
error:
    for (iValidationLevel = 0;
         ppSamLogonInfo && iValidationLevel <= 6;
         iValidationLevel++)
    {
        if (ppSamLogonInfo[iValidationLevel])
        {
            NetrFreeMemory(ppSamLogonInfo[iValidationLevel]);
            ppSamLogonInfo[iValidationLevel] = NULL;
        }
    }

    NetrFreeBinding(&hNetr);

    LW_SAFE_FREE_MEMORY(pwszMachAcct);
    LW_SECURE_FREE_WSTRING(pwszMachpass);
    LW_SAFE_FREE_MEMORY(pwszComputer);
    LW_SAFE_FREE_MEMORY(pwszServer);
    LW_SAFE_FREE_MEMORY(pwszDomain);
    LW_SAFE_FREE_MEMORY(pwszWorkstation);
    LW_SAFE_FREE_MEMORY(pwszUsername);
    LW_SAFE_FREE_MEMORY(pwszPassword);
    LW_SAFE_FREE_MEMORY(pszComputer);
    LW_SAFE_FREE_MEMORY(pszMachpass);

    return (int)bRet;
}


static
DWORD
TestNetlogonSamLogonInteractiveEx(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    PCSTR pszDefComputer = "TestWks4";
    PCSTR pszDefMachpass = "secret01$";
    const DWORD dwDefLogonLevel = 0;
    const DWORD dwDefValidationLevel = 2;

    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    NETR_BINDING hNetr = NULL;
    NETR_BINDING hSchn = NULL;
    NetrCredentials NetlogonCreds = {0};
    NetrValidationInfo **ppSamLogonInfo = NULL;
    PSTR pszComputer = NULL;
    PSTR pszMachpass = NULL;
    PWSTR pwszComputer = NULL;
    PWSTR pwszWorkstation = NULL;
    PWSTR pwszMachAcct = NULL;
    PWSTR pwszMachpass = NULL;
    PWSTR pwszServer = NULL;
    PWSTR pwszDomain = NULL;
    PWSTR pwszDnsDomain = NULL;
    PWSTR pwszUsername = NULL;
    PWSTR pwszPassword = NULL;
    DWORD dwLogonLevel = 0;
    DWORD dwValidationLevel = 0;
    DWORD pDefaultLogonLevels[] = { 1, 3, 4};
    DWORD numLogonLevels = (sizeof(pDefaultLogonLevels)
                            /sizeof(pDefaultLogonLevels[0]));
    PDWORD pLogonLevels = NULL;
    DWORD pDefaultValidationLevels[] = { 2, 3, 6 };
    DWORD numValidationLevels = (sizeof(pDefaultValidationLevels)
                                 /sizeof(pDefaultValidationLevels[0]));
    PDWORD pValidationLevels = NULL;
    DWORD iLogonLevel = 0;
    DWORD iValidationLevel = 0;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "computer", PT_WC16STRING, &pwszComputer,
                       &pszDefComputer);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "machpass", PT_WC16STRING, &pwszMachpass,
                       &pszDefMachpass);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "server", PT_WC16STRING, &pwszServer,
                       NULL);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "domain", PT_WC16STRING, &pwszDomain,
                       NULL);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "dnsdomain", PT_WC16STRING, &pwszDnsDomain,
                       NULL);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "workstation", PT_WC16STRING, &pwszWorkstation,
                       &pszDefComputer);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "username", PT_WC16STRING, &pwszUsername,
                       NULL);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "password", PT_WC16STRING, &pwszPassword,
                       NULL);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "logon_level", PT_UINT32, &dwLogonLevel,
                       &dwDefLogonLevel);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "validation_level", PT_UINT32,
                       &dwValidationLevel, &dwDefValidationLevel);
    BAIL_ON_WIN_ERROR(error);

    error = LwWc16sToMbs(pwszComputer, &pszComputer);
    BAIL_ON_WIN_ERROR(error);

    error = LwWc16sToMbs(pwszMachpass, &pszMachpass);
    BAIL_ON_WIN_ERROR(error);

    if (strcmp(pszComputer, pszDefComputer) == 0 &&
        strcmp(pszMachpass, pszDefMachpass) == 0)
    {
        LW_SAFE_FREE_MEMORY(pwszComputer);
        LW_SAFE_FREE_MEMORY(pwszMachpass);

        error = GetMachinePassword(
                        &pwszDnsDomain,
                        &pwszDomain,
                        &pwszMachAcct,
                        &pwszMachpass,
                        &pwszComputer);
        BAIL_ON_WIN_ERROR(error);
    }

    bRet &= CreateRpcBinding(OUT_PPVOID(&hNetr),
                             RPC_NETLOGON_BINDING,
                             Server,
                             pCreds);

    bRet &= CallOpenSchannel(hNetr,
                             pwszMachAcct,
                             Server->Hostname,
                             pwszServer,
                             pwszDomain,
                             pwszDnsDomain,
                             pwszComputer,
                             pwszMachpass,
                             &NetlogonCreds,
                             &hSchn);

    if (hSchn == NULL)
    {
        bRet = FALSE;
        goto done;
    }

    if (dwLogonLevel)
    {
        pLogonLevels = &dwLogonLevel;
        numLogonLevels = 1;
    }
    else
    {
        pLogonLevels = pDefaultLogonLevels;
    }

    if (dwValidationLevel)
    {
        pValidationLevels = &dwValidationLevel;
        numValidationLevels = 1;
    }
    else
    {
        pValidationLevels = pDefaultValidationLevels;
    }

    for (iLogonLevel = 0; iLogonLevel < numLogonLevels; iLogonLevel++)
    {
        bRet &= CallNetrSamLogonInteractiveEx(
                              hSchn,
                              &NetlogonCreds,
                              pwszServer,
                              pwszDomain,
                              pwszComputer,
                              pwszWorkstation,
                              pwszUsername,
                              pwszPassword,
                              pLogonLevels[iLogonLevel],
                              pValidationLevels,
                              numValidationLevels,
                              &ppSamLogonInfo);

        if (bRet)
        {
            bRet &= TestValidateSamLogonInfo(&ppSamLogonInfo, 1);

            for (iValidationLevel = 0;
                 ppSamLogonInfo && iValidationLevel <= 6;
                 iValidationLevel++)
            {
                if (ppSamLogonInfo[iValidationLevel])
                {
                    NetrFreeMemory(ppSamLogonInfo[iValidationLevel]);
                    ppSamLogonInfo[iValidationLevel] = NULL;
                }
            }
        }
    }

    bRet &= CallCloseSchannel(hSchn);

done:
error:
    for (iValidationLevel = 0;
         ppSamLogonInfo && iValidationLevel <= 6;
         iValidationLevel++)
    {
        if (ppSamLogonInfo[iValidationLevel])
        {
            NetrFreeMemory(ppSamLogonInfo[iValidationLevel]);
            ppSamLogonInfo[iValidationLevel] = NULL;
        }
    }

    NetrFreeBinding(&hNetr);

    LW_SAFE_FREE_MEMORY(pwszMachAcct);
    LW_SECURE_FREE_WSTRING(pwszMachpass);
    LW_SAFE_FREE_MEMORY(pwszComputer);
    LW_SAFE_FREE_MEMORY(pwszServer);
    LW_SAFE_FREE_MEMORY(pwszDomain);
    LW_SAFE_FREE_MEMORY(pwszWorkstation);
    LW_SAFE_FREE_MEMORY(pwszUsername);
    LW_SAFE_FREE_MEMORY(pwszPassword);
    LW_SAFE_FREE_MEMORY(pszComputer);
    LW_SAFE_FREE_MEMORY(pszMachpass);

    return (int)bRet;
}


static
DWORD
TestNetlogonEnumTrustedDomains(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    PCSTR pszDefServer = "TEST";

    BOOLEAN bRet = TRUE;
    NTSTATUS ntStatus = STATUS_SUCCESS;
    DWORD error = ERROR_SUCCESS;
    NETR_BINDING hNetr = NULL;
    PWSTR pwszServer = NULL;
    DWORD dwCount = 0;
    NetrDomainTrust *pTrusts = NULL;
    DWORD iTrust = 0;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "server", PT_WC16STRING, &pwszServer,
                       &pszDefServer);
    BAIL_ON_WIN_ERROR(error);

    bRet &= CreateRpcBinding(OUT_PPVOID(&hNetr),
                             RPC_NETLOGON_BINDING,
                             Server,
                             pCreds);

    CALL_NT_API(ntStatus, NetrEnumerateTrustedDomainsEx(
                                       hNetr,
                                       pwszServer,
                                       &pTrusts,
                                       &dwCount));
    BAIL_ON_NT_STATUS(ntStatus);

    bRet  &= TestValidateDomainTrusts(pTrusts, dwCount);

    for (iTrust = 0; iTrust < dwCount; iTrust++)
    {
        OUTPUT_ARG_WSTR(pTrusts[iTrust].netbios_name);
        OUTPUT_ARG_WSTR(pTrusts[iTrust].dns_name);
        OUTPUT_ARG_UINT(pTrusts[iTrust].trust_flags);
    }

error:
    if (pTrusts)
    {
        NetrFreeMemory(pTrusts);
    }

    NetrFreeBinding(&hNetr);

    LW_SAFE_FREE_MEMORY(pwszServer);

    if (ntStatus != STATUS_SUCCESS)
    {
        bRet = FALSE;
    }

    return bRet;
}


static
DWORD
TestNetlogonEnumDomainTrusts(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    const DWORD dwDefTrustFlags = NETR_TRUST_FLAG_IN_FOREST |
                                  NETR_TRUST_FLAG_OUTBOUND |
                                  NETR_TRUST_FLAG_TREEROOT |
                                  NETR_TRUST_FLAG_PRIMARY |
                                  NETR_TRUST_FLAG_NATIVE |
                                  NETR_TRUST_FLAG_INBOUND;

    BOOLEAN bRet = TRUE;
    NTSTATUS ntStatus = STATUS_SUCCESS;
    DWORD error = ERROR_SUCCESS;
    WINERROR err = ERROR_SUCCESS;
    NETR_BINDING hNetr = NULL;
    DWORD dwTrustFlags = 0;
    DWORD dwCount = 0;
    NetrDomainTrust *pTrusts = NULL;
    DWORD iTrust = 0;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "trustflags", PT_UINT32, &dwTrustFlags,
                       &dwDefTrustFlags);
    BAIL_ON_WIN_ERROR(error);

    bRet &= CreateRpcBinding(OUT_PPVOID(&hNetr),
                             RPC_NETLOGON_BINDING,
                             Server,
                             pCreds);

    CALL_WIN_API(err, DsrEnumerateDomainTrusts(
                                  hNetr,
                                  Server->Hostname,
                                  dwTrustFlags,
                                  &pTrusts,
                                  &dwCount));
    BAIL_ON_WIN_ERROR(err);

    bRet  &= TestValidateDomainTrusts(pTrusts, dwCount);

    for (iTrust = 0; iTrust < dwCount; iTrust++)
    {
        OUTPUT_ARG_WSTR(pTrusts[iTrust].netbios_name);
        OUTPUT_ARG_WSTR(pTrusts[iTrust].dns_name);
        OUTPUT_ARG_UINT(pTrusts[iTrust].trust_flags);
    }

error:
    if (pTrusts)
    {
        NetrFreeMemory(pTrusts);
    }

    NetrFreeBinding(&hNetr);

    if (ntStatus != STATUS_SUCCESS ||
        err != ERROR_SUCCESS)
    {
        bRet = FALSE;
    }

    return bRet;
}


static
DWORD
TestNetlogonGetDcName(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    const DWORD dwDefGetDcFlags = DS_FORCE_REDISCOVERY;
    const PSTR pszDefDomainName = "DOMAIN";

    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    NETR_BINDING hNetr = NULL;
    DWORD dwGetDcFlags = 0;
    PWSTR pwszDomainName = NULL;
    DsrDcNameInfo *pInfo = NULL;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "getdcflags", PT_UINT32, &dwGetDcFlags,
                       &dwDefGetDcFlags);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "domainname", PT_WC16STRING, &pwszDomainName,
                       &pszDefDomainName);
    BAIL_ON_WIN_ERROR(error);

    bRet &= CreateRpcBinding(OUT_PPVOID(&hNetr),
                             RPC_NETLOGON_BINDING,
                             Server,
                             pCreds);

    CALL_WIN_API(error, DsrGetDcName(
                                hNetr,
                                Server->Hostname,
                                pwszDomainName,
                                NULL,
                                NULL,
                                dwGetDcFlags,
                                &pInfo));
    BAIL_ON_WIN_ERROR(error);

error:
    if (pInfo)
    {
        NetrFreeMemory(pInfo);
    }

    NetrFreeBinding(&hNetr);

    if (error != ERROR_SUCCESS)
    {
        bRet = FALSE;
    }

    return bRet;
}


VOID
SetupNetlogonTests(PTEST t)
{
    AddTest(t, "NETR-ENUM-TRUSTED-DOM" , TestNetlogonEnumTrustedDomains);
    AddTest(t, "NETR-DSR-ENUM-DOMTRUSTS", TestNetlogonEnumDomainTrusts);
    AddTest(t, "NETR-SAM-LOGON-INTERACTIVE", TestNetlogonSamLogonInteractive);
    AddTest(t, "NETR-SAM-LOGON-INTERACTIVE-EX", TestNetlogonSamLogonInteractiveEx);
    AddTest(t, "NETR-DSR-GET-DC-NAME", TestNetlogonGetDcName);
}


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