1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
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 may
7 * require a specific license from the United States Government.
8 * It is the responsibility of any person or organization contemplating
9 * export to obtain such a license before exporting.
10 *
11 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12 * distribute this software and its documentation for any purpose and
13 * without fee is hereby granted, provided that the above copyright
14 * notice appear in all copies and that both that copyright notice and
15 * this permission notice appear in supporting documentation, and that
16 * the name of M.I.T. not be used in advertising or publicity pertaining
17 * to distribution of the software without specific, written prior
18 * permission. Furthermore if you modify this software you must label
19 * your software as modified software and not distribute it in such a
20 * fashion that it might be confused with the original M.I.T. software.
21 * M.I.T. makes no representations about the suitability of
22 * this software for any purpose. It is provided "as is" without express
23 * or implied warranty.
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "common.h"
30
31 /*
32 * Test program for acceptor names, intended to be run from a Python test
33 * script. Establishes contexts with the default initiator name, a specified
34 * principal name as target name, and a specified host-based name as acceptor
35 * name (or GSS_C_NO_NAME if no acceptor name is given). If the exchange is
36 * successful, queries the context for the acceptor name and prints it. If any
37 * call is unsuccessful, displays an error message. Exits with status 0 if all
38 * operations are successful, or 1 if not.
39 *
40 * Usage: ./t_accname targetname [acceptorname]
41 */
42
43 int
main(int argc,char * argv[])44 main(int argc, char *argv[])
45 {
46 OM_uint32 minor, major, flags;
47 gss_cred_id_t acceptor_cred;
48 gss_name_t target_name, acceptor_name = GSS_C_NO_NAME, real_acceptor_name;
49 gss_buffer_desc namebuf;
50 gss_ctx_id_t initiator_context, acceptor_context;
51
52 if (argc < 2 || argc > 3) {
53 fprintf(stderr, "Usage: %s targetname [acceptorname]\n", argv[0]);
54 return 1;
55 }
56
57 /* Import target and acceptor names. */
58 target_name = import_name(argv[1]);
59 if (argc >= 3)
60 acceptor_name = import_name(argv[2]);
61
62 /* Get acceptor cred. */
63 major = gss_acquire_cred(&minor, acceptor_name, GSS_C_INDEFINITE,
64 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
65 &acceptor_cred, NULL, NULL);
66 check_gsserr("gss_acquire_cred", major, minor);
67
68 flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG;
69 establish_contexts(&mech_krb5, GSS_C_NO_CREDENTIAL, acceptor_cred,
70 target_name, flags, &initiator_context,
71 &acceptor_context, NULL, NULL, NULL);
72
73 major = gss_inquire_context(&minor, acceptor_context, NULL,
74 &real_acceptor_name, NULL, NULL, NULL, NULL,
75 NULL);
76 check_gsserr("gss_inquire_context", major, minor);
77
78 namebuf.value = NULL;
79 namebuf.length = 0;
80 major = gss_display_name(&minor, real_acceptor_name, &namebuf, NULL);
81 check_gsserr("gss_display_name", major, minor);
82
83 printf("%.*s\n", (int)namebuf.length, (char *)namebuf.value);
84
85 (void)gss_release_name(&minor, &target_name);
86 (void)gss_release_name(&minor, &acceptor_name);
87 (void)gss_release_name(&minor, &real_acceptor_name);
88 (void)gss_release_cred(&minor, &acceptor_cred);
89 (void)gss_delete_sec_context(&minor, &initiator_context, NULL);
90 (void)gss_delete_sec_context(&minor, &acceptor_context, NULL);
91 (void)gss_release_buffer(&minor, &namebuf);
92 return 0;
93 }
94