Blame src/tests/gssapi/t_imp_cred.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/* tests/gssapi/t_imp_cred.c - krb5_gss_import_cred test harness */
Packit fd8b60
/*
Packit fd8b60
 * Copyright 2011 by the Massachusetts Institute of Technology.
Packit fd8b60
 * All Rights Reserved.
Packit fd8b60
 *
Packit fd8b60
 * Export of this software from the United States of America may
Packit fd8b60
 *   require a specific license from the United States Government.
Packit fd8b60
 *   It is the responsibility of any person or organization contemplating
Packit fd8b60
 *   export to obtain such a license before exporting.
Packit fd8b60
 *
Packit fd8b60
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
Packit fd8b60
 * distribute this software and its documentation for any purpose and
Packit fd8b60
 * without fee is hereby granted, provided that the above copyright
Packit fd8b60
 * notice appear in all copies and that both that copyright notice and
Packit fd8b60
 * this permission notice appear in supporting documentation, and that
Packit fd8b60
 * the name of M.I.T. not be used in advertising or publicity pertaining
Packit fd8b60
 * to distribution of the software without specific, written prior
Packit fd8b60
 * permission.  Furthermore if you modify this software you must label
Packit fd8b60
 * your software as modified software and not distribute it in such a
Packit fd8b60
 * fashion that it might be confused with the original M.I.T. software.
Packit fd8b60
 * M.I.T. makes no representations about the suitability of
Packit fd8b60
 * this software for any purpose.  It is provided "as is" without express
Packit fd8b60
 * or implied warranty.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * Test program for krb5_gss_import_cred, intended to be run from a Python test
Packit fd8b60
 * script.  Creates an initiator credential for the default ccache and an
Packit fd8b60
 * acceptor principal for the default keytab (possibly using a specified keytab
Packit fd8b60
 * principal), and performs a one-token context exchange using a specified
Packit fd8b60
 * target principal.  If the exchange is successful, queries the context for
Packit fd8b60
 * the acceptor name and prints it.  If any call is unsuccessful, displays an
Packit fd8b60
 * error message.  Exits with status 0 if all operations are successful, or 1
Packit fd8b60
 * if not.
Packit fd8b60
 *
Packit fd8b60
 * Usage: ./t_imp_cred target-princ [keytab-princ]
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
#include "k5-platform.h"
Packit fd8b60
#include <krb5.h>
Packit fd8b60
Packit fd8b60
#include "common.h"
Packit fd8b60
Packit fd8b60
int
Packit fd8b60
main(int argc, char *argv[])
Packit fd8b60
{
Packit fd8b60
    OM_uint32 minor, major, flags;
Packit fd8b60
    gss_cred_id_t initiator_cred, acceptor_cred;
Packit fd8b60
    gss_ctx_id_t initiator_context, acceptor_context;
Packit fd8b60
    gss_name_t target_name;
Packit fd8b60
    krb5_context context = NULL;
Packit fd8b60
    krb5_ccache cc;
Packit fd8b60
    krb5_keytab kt;
Packit fd8b60
    krb5_principal princ = NULL;
Packit fd8b60
    krb5_error_code ret;
Packit fd8b60
Packit fd8b60
    if (argc < 2 || argc > 3) {
Packit fd8b60
        fprintf(stderr, "Usage: %s targetname [acceptorprinc]\n", argv[0]);
Packit fd8b60
        return 1;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    /* Import the target name. */
Packit fd8b60
    target_name = import_name(argv[1]);
Packit fd8b60
Packit fd8b60
    /* Acquire the krb5 objects we need. */
Packit fd8b60
    ret = krb5_init_context(&context);
Packit fd8b60
    check_k5err(context, "krb5_init_context", ret);
Packit fd8b60
    ret = krb5_cc_default(context, &cc);
Packit fd8b60
    check_k5err(context, "krb5_cc_default", ret);
Packit fd8b60
    ret = krb5_kt_default(context, &kt;;
Packit fd8b60
    check_k5err(context, "krb5_kt_default", ret);
Packit fd8b60
    if (argc >= 3) {
Packit fd8b60
        ret = krb5_parse_name(context, argv[2], &princ);
Packit fd8b60
        check_k5err(context, "krb5_parse_name", ret);
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    /* Get initiator cred. */
Packit fd8b60
    major = gss_krb5_import_cred(&minor, cc, NULL, NULL, &initiator_cred);
Packit fd8b60
    check_gsserr("gss_krb5_import_cred (initiator)", major, minor);
Packit fd8b60
Packit fd8b60
    /* Get acceptor cred. */
Packit fd8b60
    major = gss_krb5_import_cred(&minor, NULL, princ, kt, &acceptor_cred);
Packit fd8b60
    check_gsserr("gss_krb5_import_cred (acceptor)", major, minor);
Packit fd8b60
Packit fd8b60
    flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG;
Packit fd8b60
    establish_contexts(&mech_krb5, initiator_cred, acceptor_cred, target_name,
Packit fd8b60
                       flags, &initiator_context, &acceptor_context, NULL,
Packit fd8b60
                       NULL, NULL);
Packit fd8b60
Packit fd8b60
    krb5_cc_close(context, cc);
Packit fd8b60
    krb5_kt_close(context, kt);
Packit fd8b60
    krb5_free_principal(context, princ);
Packit fd8b60
    krb5_free_context(context);
Packit fd8b60
    (void)gss_release_name(&minor, &target_name);
Packit fd8b60
    (void)gss_release_cred(&minor, &initiator_cred);
Packit fd8b60
    (void)gss_release_cred(&minor, &acceptor_cred);
Packit fd8b60
    (void)gss_delete_sec_context(&minor, &initiator_context, NULL);
Packit fd8b60
    (void)gss_delete_sec_context(&minor, &acceptor_context, NULL);
Packit fd8b60
    return 0;
Packit fd8b60
}