1
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
/* tests/gssapi/t_ccselect.c - Test program for GSSAPI cred selection */
4
* Copyright 2011 by the Massachusetts Institute of Technology.
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.
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.
31
#include <gssapi/gssapi_krb5.h>
34
* Test program for client credential selection, intended to be run from a
35
* Python test script. Performs a one-token
36
* gss_init_sec_context/gss_accept_sec_context exchange, optionally with a
37
* specified principal as the initiator name, a specified principal name as
38
* target name, the default acceptor cred. If the exchange is successful,
39
* prints the initiator name as seen by the acceptor. If any call is
40
* unsuccessful, displays an error message. Exits with status 0 if all
41
* operations are successful, or 1 if not.
43
* Usage: ./t_ccselect [targetprinc|gss:service@host] [initiatorprinc|-]
47
display_status_1(const char *m, OM_uint32 code, int type)
49
OM_uint32 maj_stat, min_stat;
55
maj_stat = gss_display_status(&min_stat, code,
58
fprintf(stderr, "%s: %s\n", m, (char *)msg.value);
59
(void) gss_release_buffer(&min_stat, &msg);
67
gsserr(const char *msg, OM_uint32 maj_stat, OM_uint32 min_stat)
69
display_status_1(msg, maj_stat, GSS_C_GSS_CODE);
70
display_status_1(msg, min_stat, GSS_C_MECH_CODE);
75
main(int argc, char *argv[])
77
OM_uint32 minor, major;
78
gss_cred_id_t initiator_cred = GSS_C_NO_CREDENTIAL;
80
gss_name_t target_name, initiator_name = GSS_C_NO_NAME;
81
gss_name_t real_initiator_name;
82
gss_buffer_desc token, tmp, namebuf;
83
gss_ctx_id_t initiator_context = GSS_C_NO_CONTEXT;
84
gss_ctx_id_t acceptor_context = GSS_C_NO_CONTEXT;
86
if (argc < 2 || argc > 3) {
87
fprintf(stderr, "Usage: %s targetprinc [initiatorprinc|-]\n", argv[0]);
91
/* Import the target name. */
92
if (strncmp(argv[1], "gss:", 4) == 0) {
93
/* Import as host-based service. */
94
buf.value = argv[1] + 4;
95
buf.length = strlen((char *)buf.value);
96
major = gss_import_name(&minor, &buf,
97
(gss_OID)GSS_C_NT_HOSTBASED_SERVICE,
100
/* Import as krb5 principal name. */
102
buf.length = strlen((char *)buf.value);
103
major = gss_import_name(&minor, &buf,
104
(gss_OID)GSS_KRB5_NT_PRINCIPAL_NAME,
107
if (GSS_ERROR(major))
108
gsserr("gss_import_name(target_name)", major, minor);
110
/* Import the initiator name as a krb5 principal and get creds, maybe. */
112
if (strcmp(argv[2], "-") != 0) {
114
buf.length = strlen((char *)buf.value);
115
major = gss_import_name(&minor, &buf,
116
(gss_OID)GSS_KRB5_NT_PRINCIPAL_NAME,
118
if (GSS_ERROR(major))
119
gsserr("gss_import_name(initiator_name)", major, minor);
122
/* Get acceptor cred. */
123
major = gss_acquire_cred(&minor, initiator_name, GSS_C_INDEFINITE,
124
GSS_C_NO_OID_SET, GSS_C_INITIATE,
125
&initiator_cred, NULL, NULL);
126
if (GSS_ERROR(major))
127
gsserr("gss_acquire_cred", major, minor);
131
/* Create krb5 initiator context and get the first token. */
134
major = gss_init_sec_context(&minor, initiator_cred, &initiator_context,
135
target_name, (gss_OID)gss_mech_krb5,
136
GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG,
137
GSS_C_INDEFINITE, GSS_C_NO_CHANNEL_BINDINGS,
138
GSS_C_NO_BUFFER, NULL, &token, NULL, NULL);
139
if (GSS_ERROR(major))
140
gsserr("gss_init_sec_context", major, minor);
142
/* Pass the token to gss_accept_sec_context. */
145
major = gss_accept_sec_context(&minor, &acceptor_context,
146
GSS_C_NO_CREDENTIAL, &token,
147
GSS_C_NO_CHANNEL_BINDINGS,
148
&real_initiator_name, NULL, &tmp,
150
if (major != GSS_S_COMPLETE)
151
gsserr("gss_accept_sec_context", major, minor);
153
namebuf.value = NULL;
155
major = gss_display_name(&minor, real_initiator_name, &namebuf, NULL);
156
if (GSS_ERROR(major))
157
gsserr("gss_display_name(initiator)", major, minor);
158
printf("%.*s\n", (int)namebuf.length, (char *)namebuf.value);
160
(void)gss_release_name(&minor, &target_name);
161
(void)gss_release_name(&minor, &initiator_name);
162
(void)gss_release_name(&minor, &real_initiator_name);
163
(void)gss_release_cred(&minor, &initiator_cred);
164
(void)gss_delete_sec_context(&minor, &initiator_context, NULL);
165
(void)gss_delete_sec_context(&minor, &acceptor_context, NULL);
166
(void)gss_release_buffer(&minor, &token);
167
(void)gss_release_buffer(&minor, &tmp);
168
(void)gss_release_buffer(&minor, &namebuf);