xref: /freebsd/crypto/krb5/src/tests/gssapi/t_imp_cred.c (revision b670c9bafc0e31c7609969bf374b2e80bdc00211)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/gssapi/t_imp_cred.c - krb5_gss_import_cred test harness */
3 /*
4  * Copyright 2011 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 
27 /*
28  * Test program for krb5_gss_import_cred, intended to be run from a Python test
29  * script.  Creates an initiator credential for the default ccache and an
30  * acceptor principal for the default keytab (possibly using a specified keytab
31  * principal), and performs a one-token context exchange using a specified
32  * target principal.  If the exchange is successful, queries the context for
33  * the acceptor name and prints it.  If any call is unsuccessful, displays an
34  * error message.  Exits with status 0 if all operations are successful, or 1
35  * if not.
36  *
37  * Usage: ./t_imp_cred target-princ [keytab-princ]
38  */
39 
40 #include "k5-platform.h"
41 #include <krb5.h>
42 
43 #include "common.h"
44 
45 int
46 main(int argc, char *argv[])
47 {
48     OM_uint32 minor, major, flags;
49     gss_cred_id_t initiator_cred, acceptor_cred;
50     gss_ctx_id_t initiator_context, acceptor_context;
51     gss_name_t target_name;
52     krb5_context context = NULL;
53     krb5_ccache cc;
54     krb5_keytab kt;
55     krb5_principal princ = NULL;
56     krb5_error_code ret;
57 
58     if (argc < 2 || argc > 3) {
59         fprintf(stderr, "Usage: %s targetname [acceptorprinc]\n", argv[0]);
60         return 1;
61     }
62 
63     /* Import the target name. */
64     target_name = import_name(argv[1]);
65 
66     /* Acquire the krb5 objects we need. */
67     ret = krb5_init_context(&context);
68     check_k5err(context, "krb5_init_context", ret);
69     ret = krb5_cc_default(context, &cc);
70     check_k5err(context, "krb5_cc_default", ret);
71     ret = krb5_kt_default(context, &kt);
72     check_k5err(context, "krb5_kt_default", ret);
73     if (argc >= 3) {
74         ret = krb5_parse_name(context, argv[2], &princ);
75         check_k5err(context, "krb5_parse_name", ret);
76     }
77 
78     /* Get initiator cred. */
79     major = gss_krb5_import_cred(&minor, cc, NULL, NULL, &initiator_cred);
80     check_gsserr("gss_krb5_import_cred (initiator)", major, minor);
81 
82     /* Get acceptor cred. */
83     major = gss_krb5_import_cred(&minor, NULL, princ, kt, &acceptor_cred);
84     check_gsserr("gss_krb5_import_cred (acceptor)", major, minor);
85 
86     flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG;
87     establish_contexts(&mech_krb5, initiator_cred, acceptor_cred, target_name,
88                        flags, &initiator_context, &acceptor_context, NULL,
89                        NULL, NULL);
90 
91     krb5_cc_close(context, cc);
92     krb5_kt_close(context, kt);
93     krb5_free_principal(context, princ);
94     krb5_free_context(context);
95     (void)gss_release_name(&minor, &target_name);
96     (void)gss_release_cred(&minor, &initiator_cred);
97     (void)gss_release_cred(&minor, &acceptor_cred);
98     (void)gss_delete_sec_context(&minor, &initiator_context, NULL);
99     (void)gss_delete_sec_context(&minor, &acceptor_context, NULL);
100     return 0;
101 }
102