/*
 * Copyright Likewise Software
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program 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 General Public License
 * for more details.  You should have received a copy of the GNU 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
 * 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:
 *
 *        test_mapsecurity.c
 *
 * Abstract:
 *
 *        Likewise Security and Authentication Subsystem (LSASS)
 *
 *        Test program for lwmapsecurity
 *
 * Authors: Brian Koropoff (bkoropoff@likewise.com)
 *
 */

#include <lw/base.h>
#include <lw/rtlgoto.h>
#include <unistd.h>
#include <stdio.h>

int main(
    int Argc,
    char** ppArgv
    )
{
    NTSTATUS status = STATUS_SUCCESS;
    PLW_MAP_SECURITY_CONTEXT pContext = NULL;
    PACCESS_TOKEN pToken = NULL;
    uid_t runUid = getuid();
    gid_t runGid = getgid();
    BOOLEAN uidMapped = FALSE;
    BOOLEAN gidMapped = FALSE;
    ULONG gidCount = 0;
    PULONG pGids = NULL;
    ULONG tokenUid = -1;
    ULONG tokenGid = -1;
    ULONG i = 0;

    status = LwMapSecurityCreateContext(&pContext);
    GOTO_CLEANUP_ON_STATUS(status);

    status = LwMapSecurityCreateAccessTokenFromUidGid(pContext, &pToken, runUid, runGid);
    GOTO_CLEANUP_ON_STATUS(status);

    status = LwMapSecurityGetUnixIdsForAccessToken(pContext, pToken, &uidMapped, &tokenUid, &gidMapped, &tokenGid, &gidCount, &pGids);
    GOTO_CLEANUP_ON_STATUS(status);

    if (uidMapped)
    {
        printf("Token uid: %lu\n", (unsigned long) tokenUid);
    }
    else
    {
        printf("Token uid: <unmapped>\n");
    }

    if (gidMapped)
    {
        printf("Token gid: %lu\n", (unsigned long) tokenGid);
    }
    else
    {
        printf("Token gid: <unmapped>\n");
    }

    printf("Token extra gids:");

    for (i = 0; i < gidCount; i++)
    {
        printf(" %lu", (unsigned long) pGids[i]);
    }

    printf("\n");

cleanup:

    if (pGids)
    {
        LwMapSecurityGetUnixIdsForAccessTokenFreeResult(pContext, &pGids);
    }

    if (pContext)
    {
        LwMapSecurityFreeContext(&pContext);
    }

    if (status)
    {
        fprintf(stderr, "Error: %s (0x%lx)\n", LwNtStatusToName(status), (unsigned long) status);
    }

    return status ? 1 : 0;
}
