/* 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
 */

#include "includes.h"


static
PTEST
FindTest(
    PTEST pFirstTest,
    PCSTR pszName
    );

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

VOID
RpctFreeCredentials(
    PCREDENTIALS Creds
    );


VOID
AddTest(
    PTEST   pFt,
    PCSTR   pszName,
    test_fn Function
    )
{
    DWORD dwError = ERROR_SUCCESS;
    PTEST pNextTest = NULL;
    PTEST pLastTest = NULL;

    if (pFt == NULL) return;

    pLastTest = pFt;
    while (pLastTest && pLastTest->pNext)
    {
        pLastTest = pLastTest->pNext;
    }

    if (pLastTest->pszName && pLastTest->Function)
    {
        /* allocate the new test */
        dwError = LwAllocateMemory(sizeof(TEST),
                                   OUT_PPVOID(&pNextTest));
        BAIL_ON_WIN_ERROR(dwError);

        /* append the test to the list */
        pLastTest->pNext = pNextTest;
    }
    else
    {
        /* this is the very first test so use already
           allocated node */
        pNextTest = pLastTest;
    }

    /* init the new test */
    pNextTest->pszName    = pszName;
    pNextTest->Function   = Function;
    pNextTest->pNext      = NULL;

error:
    return;
}


static
PTEST
FindTest(
    PTEST pFirstTest,
    PCSTR pszName
    )
{
    PTEST pTest = pFirstTest;

    /* search through the tests and try to find
       a matching name */
    while (pTest)
    {
        if (strcasecmp(pTest->pszName, pszName) == 0)
        {
            return pTest;
        }

        pTest = pTest->pNext;
    }

    return NULL;
}


static
DWORD
StartTest(
    PTEST pTest,
    PRPCSERVER Server,
    PCREDENTIALS pCreds,
    PPARAMETER pOptions,
    DWORD dwOptcount
    )
{
    DWORD dwRet = 0;

    if (pTest == NULL) return -1;

    dwRet = pTest->Function(pTest,
                            Server,
                            pCreds,
                            pOptions,
                            dwOptcount);
    printf("%s\n", (dwRet) ? "SUCCEEDED" : "FAILED");
    return dwRet;
}


static
VOID
DisplayUsage(
    PCSTR ProgramName
    )
{
    printf("Usage: %s [-v] [-k] -h hostname\n"
           "\t[-u username]\n"
           "\t[-p password]\n"
           "\t[-d domain]\n"
           "\t[-w workstation]\n"
           "\t[-u principal]\n"
           "\t[-c creds cache]\n"
           "\t[-b binding string]\n"
           "\t[-r realm]\n"
           "\t[-o options]\n"
           "\ttestname\n",
           ProgramName);
    printf("hostname\t\t- host to connect when performing a test\n");
    printf("username\t\t- user name (NTLM authentication)\n");
    printf("password\t\t- password (NTLM authentication)\n");
    printf("domain\t\t- domain name (NTLM authentication)\n");
    printf("workstation\t\t- workstation name (NTLM authentication)\n");
    printf("principal\t\t- user principal name (Kerberos authentication)\n");
    printf("creds cache\t\t- kerberos credentials cache path (Kerberos authentication)\n");
    printf("binding string\t- binding string specifying rpc endpoint (rpc client functions)\n");
    printf("realm\t\t- realm name the rpc server is in (may be useful when testing gss-spnego)\n");
    printf("options\t\t\t- \"key1=value1,key2=value2\" options passed to the test\n");
}


extern char *optarg;
BOOLEAN verboseMode= FALSE;
BOOLEAN helpMode = FALSE;


int main(int argc, char *argv[])
{
    DWORD dwError = ERROR_SUCCESS;
    NTSTATUS ntStatus = STATUS_SUCCESS;
    int i = 0;
    int opt = 0;
    int ret = 0;
    PSTR pszTestname = NULL;
    PSTR pszHost = NULL;
    PSTR pszOptionalArgs = NULL;
    PSTR pszUser = NULL;
    PSTR pszPass = NULL;
    PSTR pszDomain = NULL;
    PSTR pszWorkstation = NULL;
    PSTR pszPrincipal = NULL;
    PSTR pszCredsCache = NULL;
    PSTR pszBindingString = NULL;
    PTEST pTests  = NULL;
    PTEST pRunTest = NULL;
    PSTR targetRealmName = NULL;
    LW_PIO_CREDS pCreds = NULL;
    CREDENTIALS credentials;
    RPCSERVER server = {0};
    PPARAMETER pParams = NULL;
    DWORD dwParamsLen = 0;

    memset(&credentials, 0, sizeof(credentials));

    while ((opt = getopt(argc, argv, "h:o:vHu:p:d:w:r:c:kb:")) != -1) {
        switch (opt) {
        case 'h':
            pszHost = optarg;
            break;

        case 'o':
            pszOptionalArgs = optarg;
            break;

        case 'v':
            verboseMode = TRUE;
            break;

        case 'H':
            helpMode = TRUE;
            break;

        case 'd':
            pszDomain = optarg;
            break;

        case 'w':
            pszWorkstation = optarg;
            break;

        case 'u':
            pszUser = optarg;
            pszPrincipal = optarg;
            break;

        case 'p':
            pszPass = optarg;
            break;

        case 'r':
            targetRealmName = optarg;
            break;

        case 'c':
            pszCredsCache = optarg;
            break;

        case 'b':
            pszBindingString = optarg;
            break;

        default:
            DisplayUsage(argv[0]);
            return -1;
        }
    }

    //
    // Verbose mode can also be enabled by environment variable.
    // This can be useful for investigating problems with testing scripts.
    //
    if (!verboseMode)
    {
        PSTR verboseValue = getenv("verbose");

        if (verboseValue && verboseValue[0] == '1')
        {
            verboseMode = TRUE;
        }
    }

    dwError = LwAllocateMemory(sizeof(pTests[0]),
                               OUT_PPVOID(&pTests));
    if (dwError)
    {
        DISPLAY_ERROR(("Failed to allocate tests\n"));
        BAIL_ON_WIN_ERROR(dwError);
    }

    pTests->pszName   = NULL;
    pTests->Function  = NULL;
    pTests->pNext     = NULL;

    if (pszHost)
    {
        dwError = LwMbsToWc16s(pszHost, &server.Hostname);
        BAIL_ON_WIN_ERROR(dwError);
    }

    if (pszBindingString)
    {
        dwError = LwMbsToWc16s(pszBindingString, &server.BindingString);
        BAIL_ON_WIN_ERROR(dwError);
    }

    if (targetRealmName)
    {
        dwError = LwMbsToWc16s(targetRealmName, &server.Realm);
        BAIL_ON_WIN_ERROR(dwError);
    }

    if (pszPass)
    {
        credentials.Type = CREDENTIALS_TYPE_NTLM;

        dwError = LwMbsToWc16s(pszPass,
                               &credentials.Ntlm.pwszPassword);
        BAIL_ON_WIN_ERROR(dwError);

        if (pszUser)
        {
            dwError = LwMbsToWc16s(pszUser,
                                   &credentials.Ntlm.pwszUsername);
            BAIL_ON_WIN_ERROR(dwError);

            ntStatus = LwIoCreatePlainCredsA(pszUser,
                                             pszDomain,
                                             pszPass,
                                             &pCreds);
            if (ntStatus)
            {
                DISPLAY_ERROR(("Failed to create NTLM credentials\n"));
            }
            BAIL_ON_NT_STATUS(ntStatus);
        }
        else
        {
            DISPLAY_ERROR(("User name missing\n"));
            goto error;
        }

        if (pszDomain)
        {
            dwError = LwMbsToWc16s(pszDomain,
                                   &credentials.Ntlm.pwszDomain);
            BAIL_ON_WIN_ERROR(dwError);
        }

        if (pszWorkstation)
        {
            dwError = LwMbsToWc16s(pszWorkstation,
                                   &credentials.Ntlm.pwszWorkstation);
            BAIL_ON_WIN_ERROR(dwError);
        }
    }

    if (pszCredsCache)
    {
        credentials.Type = CREDENTIALS_TYPE_KRB5;

        dwError = LwMbsToWc16s(pszCredsCache,
                               &credentials.Krb5.pwszCredsCache);
        BAIL_ON_WIN_ERROR(dwError);

        if (pszPrincipal)
        {
            dwError = LwMbsToWc16s(pszPrincipal,
                                   &credentials.Krb5.pwszPrincipal);
            BAIL_ON_WIN_ERROR(dwError);

            ntStatus = LwIoCreateKrb5CredsA(pszPrincipal,
                                            pszCredsCache,
                                            &pCreds);
            if (ntStatus)
            {
                DISPLAY_ERROR(("Failed to create KRB5 credentials\n"));
            }
            BAIL_ON_NT_STATUS(ntStatus);
        }
        else
        {
            DISPLAY_ERROR(("User principal name missing\n"));
            goto error;
        }
    }

    if (pCreds)
    {
        ntStatus = LwIoSetThreadCreds(pCreds);
        if (ntStatus)
        {
            DISPLAY_ERROR(("Failed to set thread credentials\n"));
            BAIL_ON_NT_STATUS(ntStatus);
        }
    }

    pParams = RpctGetOptionalParams(
                      pszOptionalArgs,
                      &dwParamsLen);
    if ((pParams != NULL && dwParamsLen == 0) ||
        (pParams == NULL && dwParamsLen != 0))
    {
        DISPLAY_ERROR(("Error while parsing optional parameters [%s]\n",
                       pszOptionalArgs));
        goto error;
    }

    SetupNetApiTests(pTests);
    SetupLsaTests(pTests);
    SetupSamrTests(pTests);
    SetupNetlogonTests(pTests);
    SetupDsrTests(pTests);
    SetupWkssvcTests(pTests);

    for (i = 1; i < argc; i++)
    {
        pszTestname = argv[i];
        pRunTest = FindTest(pTests, pszTestname);

        if (pRunTest)
        {
            ret = StartTest(pRunTest,
                            &server,
                            &credentials,
                            pParams,
                            dwParamsLen);
            goto done;
        }
    }

    printf("No test name specified. Available tests:\n");
    pRunTest = pTests;
    while (pRunTest)
    {
        printf("%s\n", pRunTest->pszName);
        pRunTest = pRunTest->pNext;
    }
    printf("\n");


done:
error:
    LW_SAFE_FREE_MEMORY(server.Hostname);
    LW_SAFE_FREE_MEMORY(server.BindingString);

    while (pTests)
    {
        PTEST pTest = pTests->pNext;

        LW_SAFE_FREE_MEMORY(pTests);
        pTests = pTest;
    }

    for (i = 0; i < dwParamsLen; i++)
    {
        LW_SAFE_FREE_MEMORY(pParams[i].key);
        LW_SAFE_FREE_MEMORY(pParams[i].val);
    }
    LW_SAFE_FREE_MEMORY(pParams);

    RpctFreeCredentials(&credentials);

    if (pCreds)
    {
        LwIoDeleteCreds(pCreds);
    }

    return ret;
}


VOID
RpctFreeCredentials(
    PCREDENTIALS Creds
    )
{
    if (Creds->Type == CREDENTIALS_TYPE_KRB5)
    {
        LW_SAFE_FREE_MEMORY(Creds->Krb5.pwszPrincipal);
        LW_SAFE_FREE_MEMORY(Creds->Krb5.pwszCredsCache);
    }
    else if (Creds->Type == CREDENTIALS_TYPE_NTLM)
    {
        LW_SAFE_FREE_MEMORY(Creds->Ntlm.pwszDomain);
        LW_SAFE_FREE_MEMORY(Creds->Ntlm.pwszWorkstation);
        LW_SAFE_FREE_MEMORY(Creds->Ntlm.pwszUsername);
        LW_SAFE_FREE_MEMORY(Creds->Ntlm.pwszPassword);
    }
}


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