Blame src/tests/gssapi/t_export_cred.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
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
#include <stdio.h>
Packit fd8b60
#include <stdlib.h>
Packit fd8b60
Packit fd8b60
#include "common.h"
Packit fd8b60
Packit fd8b60
/* Display a usage error message and exit. */
Packit fd8b60
static void
Packit fd8b60
usage(void)
Packit fd8b60
{
Packit fd8b60
    fprintf(stderr, "Usage: t_export_cred [-k|-s] [-i initiatorname] "
Packit fd8b60
            "[-a acceptorname] targetname\n");
Packit fd8b60
    exit(1);
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
int
Packit fd8b60
main(int argc, char *argv[])
Packit fd8b60
{
Packit fd8b60
    OM_uint32 major, minor, flags;
Packit fd8b60
    gss_name_t initiator_name = GSS_C_NO_NAME, acceptor_name = GSS_C_NO_NAME;
Packit fd8b60
    gss_name_t target_name;
Packit fd8b60
    gss_cred_id_t initiator_cred, acceptor_cred, delegated_cred;
Packit fd8b60
    gss_ctx_id_t initiator_context = GSS_C_NO_CONTEXT;
Packit fd8b60
    gss_ctx_id_t acceptor_context = GSS_C_NO_CONTEXT;
Packit fd8b60
    gss_OID mech = GSS_C_NO_OID;
Packit fd8b60
    gss_OID_set mechs = GSS_C_NO_OID_SET;
Packit fd8b60
    char optchar;
Packit fd8b60
Packit fd8b60
    /* Parse arguments. */
Packit fd8b60
    argv++;
Packit fd8b60
    while (*argv != NULL && **argv == '-') {
Packit fd8b60
        optchar = (*argv)[1];
Packit fd8b60
        argv++;
Packit fd8b60
        if (optchar == 'i') {
Packit fd8b60
            if (*argv == NULL)
Packit fd8b60
                usage();
Packit fd8b60
            initiator_name = import_name(*argv++);
Packit fd8b60
        } else if (optchar == 'a') {
Packit fd8b60
            if (*argv == NULL)
Packit fd8b60
                usage();
Packit fd8b60
            acceptor_name = import_name(*argv++);
Packit fd8b60
        } else if (optchar == 'k') {
Packit fd8b60
            mech = &mech_krb5;
Packit fd8b60
            mechs = &mechset_krb5;
Packit fd8b60
        } else if (optchar == 's') {
Packit fd8b60
            mech = &mech_spnego;
Packit fd8b60
            mechs = &mechset_spnego;
Packit fd8b60
        } else {
Packit fd8b60
            usage();
Packit fd8b60
        }
Packit fd8b60
    }
Packit fd8b60
    if (*argv == NULL || *(argv + 1) != NULL)
Packit fd8b60
        usage();
Packit fd8b60
    target_name = import_name(argv[0]);
Packit fd8b60
Packit fd8b60
    /* Get initiator cred and export/import it. */
Packit fd8b60
    major = gss_acquire_cred(&minor, initiator_name, GSS_C_INDEFINITE, mechs,
Packit fd8b60
                             GSS_C_INITIATE, &initiator_cred, NULL, NULL);
Packit fd8b60
    check_gsserr("gss_acquire_cred(initiator)", major, minor);
Packit fd8b60
    export_import_cred(&initiator_cred);
Packit fd8b60
Packit fd8b60
    /* Get acceptor cred and export/import it. */
Packit fd8b60
    major = gss_acquire_cred(&minor, acceptor_name, GSS_C_INDEFINITE, mechs,
Packit fd8b60
                             GSS_C_ACCEPT, &acceptor_cred, NULL, NULL);
Packit fd8b60
    check_gsserr("gss_acquire_cred(acceptor)", major, minor);
Packit fd8b60
    export_import_cred(&acceptor_cred);
Packit fd8b60
Packit fd8b60
    /* Initiate and accept a security context (one-token exchange only),
Packit fd8b60
     * delegating credentials. */
Packit fd8b60
    flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG | GSS_C_CONF_FLAG |
Packit fd8b60
        GSS_C_INTEG_FLAG | GSS_C_DELEG_FLAG;
Packit fd8b60
    establish_contexts(mech, initiator_cred, acceptor_cred, target_name, flags,
Packit fd8b60
                       &initiator_context, &acceptor_context, NULL, NULL,
Packit fd8b60
                       &delegated_cred);
Packit fd8b60
Packit fd8b60
    /* Import, release, export, and store delegated creds */
Packit fd8b60
    export_import_cred(&delegated_cred);
Packit fd8b60
    major = gss_store_cred(&minor, delegated_cred, GSS_C_INITIATE,
Packit fd8b60
                           GSS_C_NULL_OID, 1, 1, NULL, NULL);
Packit fd8b60
    check_gsserr("gss_store_cred", major, minor);
Packit fd8b60
Packit fd8b60
    (void)gss_release_name(&minor, &initiator_name);
Packit fd8b60
    (void)gss_release_name(&minor, &acceptor_name);
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_release_cred(&minor, &delegated_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
}