Path: blob/main/crypto/krb5/src/tests/gssapi/t_imp_cred.c
34914 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/gssapi/t_imp_cred.c - krb5_gss_import_cred test harness */2/*3* Copyright 2011 by the Massachusetts Institute of Technology.4* All Rights Reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526/*27* Test program for krb5_gss_import_cred, intended to be run from a Python test28* script. Creates an initiator credential for the default ccache and an29* acceptor principal for the default keytab (possibly using a specified keytab30* principal), and performs a one-token context exchange using a specified31* target principal. If the exchange is successful, queries the context for32* the acceptor name and prints it. If any call is unsuccessful, displays an33* error message. Exits with status 0 if all operations are successful, or 134* if not.35*36* Usage: ./t_imp_cred target-princ [keytab-princ]37*/3839#include "k5-platform.h"40#include <krb5.h>4142#include "common.h"4344int45main(int argc, char *argv[])46{47OM_uint32 minor, major, flags;48gss_cred_id_t initiator_cred, acceptor_cred;49gss_ctx_id_t initiator_context, acceptor_context;50gss_name_t target_name;51krb5_context context = NULL;52krb5_ccache cc;53krb5_keytab kt;54krb5_principal princ = NULL;55krb5_error_code ret;5657if (argc < 2 || argc > 3) {58fprintf(stderr, "Usage: %s targetname [acceptorprinc]\n", argv[0]);59return 1;60}6162/* Import the target name. */63target_name = import_name(argv[1]);6465/* Acquire the krb5 objects we need. */66ret = krb5_init_context(&context);67check_k5err(context, "krb5_init_context", ret);68ret = krb5_cc_default(context, &cc);69check_k5err(context, "krb5_cc_default", ret);70ret = krb5_kt_default(context, &kt);71check_k5err(context, "krb5_kt_default", ret);72if (argc >= 3) {73ret = krb5_parse_name(context, argv[2], &princ);74check_k5err(context, "krb5_parse_name", ret);75}7677/* Get initiator cred. */78major = gss_krb5_import_cred(&minor, cc, NULL, NULL, &initiator_cred);79check_gsserr("gss_krb5_import_cred (initiator)", major, minor);8081/* Get acceptor cred. */82major = gss_krb5_import_cred(&minor, NULL, princ, kt, &acceptor_cred);83check_gsserr("gss_krb5_import_cred (acceptor)", major, minor);8485flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG;86establish_contexts(&mech_krb5, initiator_cred, acceptor_cred, target_name,87flags, &initiator_context, &acceptor_context, NULL,88NULL, NULL);8990krb5_cc_close(context, cc);91krb5_kt_close(context, kt);92krb5_free_principal(context, princ);93krb5_free_context(context);94(void)gss_release_name(&minor, &target_name);95(void)gss_release_cred(&minor, &initiator_cred);96(void)gss_release_cred(&minor, &acceptor_cred);97(void)gss_delete_sec_context(&minor, &initiator_context, NULL);98(void)gss_delete_sec_context(&minor, &acceptor_context, NULL);99return 0;100}101102103