xref: /freebsd/crypto/krb5/src/tests/gssapi/t_ccselect.c (revision b670c9bafc0e31c7609969bf374b2e80bdc00211)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/gssapi/t_ccselect.c - Test program for GSSAPI cred selection */
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 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "common.h"
32 
33 /*
34  * Test program for client credential selection, intended to be run from a
35  * Python test script.  Establishes contexts with an optionally specified
36  * initiator name, a specified target name, and the default acceptor cred.  If
37  * the exchange is successful, prints the initiator name as seen by the
38  * acceptor.  If any call is unsuccessful, displays an error message.  Exits
39  * with status 0 if all operations are successful, or 1 if not.
40  *
41  * Usage: ./t_ccselect targetname [initiatorname|-]
42  */
43 
44 int
45 main(int argc, char *argv[])
46 {
47     OM_uint32 minor, major, flags;
48     gss_cred_id_t initiator_cred = GSS_C_NO_CREDENTIAL;
49     gss_name_t target_name, initiator_name = GSS_C_NO_NAME;
50     gss_name_t real_initiator_name;
51     gss_buffer_desc namebuf;
52     gss_ctx_id_t initiator_context, acceptor_context;
53 
54     if (argc < 2 || argc > 3) {
55         fprintf(stderr, "Usage: %s targetname [initiatorname|-]\n", argv[0]);
56         return 1;
57     }
58 
59     target_name = import_name(argv[1]);
60 
61     if (argc >= 3) {
62         /* Get initiator cred. */
63         if (strcmp(argv[2], "-") != 0)
64             initiator_name = import_name(argv[2]);
65         major = gss_acquire_cred(&minor, initiator_name, GSS_C_INDEFINITE,
66                                  GSS_C_NO_OID_SET, GSS_C_INITIATE,
67                                  &initiator_cred, NULL, NULL);
68         check_gsserr("gss_acquire_cred", major, minor);
69     }
70 
71     flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG;
72     establish_contexts(&mech_krb5, initiator_cred, GSS_C_NO_CREDENTIAL,
73                        target_name, flags, &initiator_context,
74                        &acceptor_context, &real_initiator_name, NULL, NULL);
75 
76     namebuf.value = NULL;
77     namebuf.length = 0;
78     major = gss_display_name(&minor, real_initiator_name, &namebuf, NULL);
79     check_gsserr("gss_display_name(initiator)", major, minor);
80     printf("%.*s\n", (int)namebuf.length, (char *)namebuf.value);
81 
82     (void)gss_release_name(&minor, &target_name);
83     (void)gss_release_name(&minor, &initiator_name);
84     (void)gss_release_name(&minor, &real_initiator_name);
85     (void)gss_release_cred(&minor, &initiator_cred);
86     (void)gss_delete_sec_context(&minor, &initiator_context, NULL);
87     (void)gss_delete_sec_context(&minor, &acceptor_context, NULL);
88     (void)gss_release_buffer(&minor, &namebuf);
89     return 0;
90 }
91