Blame src/tests/gcred.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/* tests/gcred.c - Test harness for referrals */
Packit fd8b60
/*
Packit fd8b60
 * Copyright (C) 2012 by the Massachusetts Institute of Technology.
Packit fd8b60
 * All rights reserved.
Packit fd8b60
 *
Packit fd8b60
 * Redistribution and use in source and binary forms, with or without
Packit fd8b60
 * modification, are permitted provided that the following conditions
Packit fd8b60
 * are met:
Packit fd8b60
 *
Packit fd8b60
 * * Redistributions of source code must retain the above copyright
Packit fd8b60
 *   notice, this list of conditions and the following disclaimer.
Packit fd8b60
 *
Packit fd8b60
 * * Redistributions in binary form must reproduce the above copyright
Packit fd8b60
 *   notice, this list of conditions and the following disclaimer in
Packit fd8b60
 *   the documentation and/or other materials provided with the
Packit fd8b60
 *   distribution.
Packit fd8b60
 *
Packit fd8b60
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit fd8b60
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit fd8b60
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit fd8b60
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit fd8b60
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Packit fd8b60
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Packit fd8b60
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit fd8b60
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit fd8b60
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
Packit fd8b60
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit fd8b60
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
Packit fd8b60
 * OF THE POSSIBILITY OF SUCH DAMAGE.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * This program is intended to be run from a python script as:
Packit fd8b60
 *
Packit fd8b60
 *     gcred [-f] [-t] nametype princname
Packit fd8b60
 *
Packit fd8b60
 * where nametype is one of "unknown", "principal", "srv-inst", and "srv-hst",
Packit fd8b60
 * and princname is the name of the service principal.  gcred acquires
Packit fd8b60
 * credentials for the specified server principal.  On success, gcred displays
Packit fd8b60
 * the server principal name of the obtained credentials to stdout and exits
Packit fd8b60
 * with status 0.  On failure, gcred displays the error message for the failed
Packit fd8b60
 * operation to stderr and exits with status 1.
Packit fd8b60
 *
Packit fd8b60
 * The -f and -t flags set the KRB5_GC_FORWARDABLE and KRB5_GC_NO_TRANSIT_CHECK
Packit fd8b60
 * options respectively.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
#include "k5-int.h"
Packit fd8b60
Packit fd8b60
static krb5_context ctx;
Packit fd8b60
Packit fd8b60
static void
Packit fd8b60
check(krb5_error_code code)
Packit fd8b60
{
Packit fd8b60
    const char *errmsg;
Packit fd8b60
Packit fd8b60
    if (code) {
Packit fd8b60
        errmsg = krb5_get_error_message(ctx, code);
Packit fd8b60
        fprintf(stderr, "%s\n", errmsg);
Packit fd8b60
        krb5_free_error_message(ctx, errmsg);
Packit fd8b60
        exit(1);
Packit fd8b60
    }
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
int
Packit fd8b60
main(int argc, char **argv)
Packit fd8b60
{
Packit fd8b60
    krb5_principal client, server;
Packit fd8b60
    krb5_ccache ccache;
Packit fd8b60
    krb5_creds in_creds, *creds;
Packit fd8b60
    krb5_ticket *ticket;
Packit fd8b60
    krb5_flags options = 0;
Packit fd8b60
    char *name;
Packit fd8b60
    int c;
Packit fd8b60
Packit fd8b60
    check(krb5_init_context(&ctx));
Packit fd8b60
Packit fd8b60
    while ((c = getopt(argc, argv, "ft")) != -1) {
Packit fd8b60
        switch (c) {
Packit fd8b60
        case 'f':
Packit fd8b60
            options |= KRB5_GC_FORWARDABLE;
Packit fd8b60
            break;
Packit fd8b60
        case 't':
Packit fd8b60
            options |= KRB5_GC_NO_TRANSIT_CHECK;
Packit fd8b60
            break;
Packit fd8b60
        default:
Packit fd8b60
            abort();
Packit fd8b60
        }
Packit fd8b60
    }
Packit fd8b60
    argc -= optind;
Packit fd8b60
    argv += optind;
Packit fd8b60
    assert(argc == 2);
Packit fd8b60
    check(krb5_parse_name(ctx, argv[1], &server));
Packit fd8b60
    if (strcmp(argv[0], "unknown") == 0)
Packit fd8b60
        server->type = KRB5_NT_UNKNOWN;
Packit fd8b60
    else if (strcmp(argv[0], "principal") == 0)
Packit fd8b60
        server->type = KRB5_NT_PRINCIPAL;
Packit fd8b60
    else if (strcmp(argv[0], "srv-inst") == 0)
Packit fd8b60
        server->type = KRB5_NT_SRV_INST;
Packit fd8b60
    else if (strcmp(argv[0], "srv-hst") == 0)
Packit fd8b60
        server->type = KRB5_NT_SRV_HST;
Packit fd8b60
    else
Packit fd8b60
        abort();
Packit fd8b60
Packit fd8b60
    check(krb5_cc_default(ctx, &ccache));
Packit fd8b60
    check(krb5_cc_get_principal(ctx, ccache, &client));
Packit fd8b60
    memset(&in_creds, 0, sizeof(in_creds));
Packit fd8b60
    in_creds.client = client;
Packit fd8b60
    in_creds.server = server;
Packit fd8b60
    check(krb5_get_credentials(ctx, options, ccache, &in_creds, &creds));
Packit fd8b60
    check(krb5_decode_ticket(&creds->ticket, &ticket));
Packit fd8b60
    check(krb5_unparse_name(ctx, ticket->server, &name));
Packit fd8b60
    printf("%s\n", name);
Packit fd8b60
Packit fd8b60
    krb5_free_ticket(ctx, ticket);
Packit fd8b60
    krb5_free_unparsed_name(ctx, name);
Packit fd8b60
    krb5_free_creds(ctx, creds);
Packit fd8b60
    krb5_free_principal(ctx, client);
Packit fd8b60
    krb5_free_principal(ctx, server);
Packit fd8b60
    krb5_cc_close(ctx, ccache);
Packit fd8b60
    krb5_free_context(ctx);
Packit fd8b60
    return 0;
Packit fd8b60
}