1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* tests/gssapi/t_export_name.c - Test program for gss_export_name behavior */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright 2012 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert * All Rights Reserved.
6*7f2fe78bSCy Schubert *
7*7f2fe78bSCy Schubert * Export of this software from the United States of America may
8*7f2fe78bSCy Schubert * require a specific license from the United States Government.
9*7f2fe78bSCy Schubert * It is the responsibility of any person or organization contemplating
10*7f2fe78bSCy Schubert * export to obtain such a license before exporting.
11*7f2fe78bSCy Schubert *
12*7f2fe78bSCy Schubert * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13*7f2fe78bSCy Schubert * distribute this software and its documentation for any purpose and
14*7f2fe78bSCy Schubert * without fee is hereby granted, provided that the above copyright
15*7f2fe78bSCy Schubert * notice appear in all copies and that both that copyright notice and
16*7f2fe78bSCy Schubert * this permission notice appear in supporting documentation, and that
17*7f2fe78bSCy Schubert * the name of M.I.T. not be used in advertising or publicity pertaining
18*7f2fe78bSCy Schubert * to distribution of the software without specific, written prior
19*7f2fe78bSCy Schubert * permission. Furthermore if you modify this software you must label
20*7f2fe78bSCy Schubert * your software as modified software and not distribute it in such a
21*7f2fe78bSCy Schubert * fashion that it might be confused with the original M.I.T. software.
22*7f2fe78bSCy Schubert * M.I.T. makes no representations about the suitability of
23*7f2fe78bSCy Schubert * this software for any purpose. It is provided "as is" without express
24*7f2fe78bSCy Schubert * or implied warranty.
25*7f2fe78bSCy Schubert */
26*7f2fe78bSCy Schubert
27*7f2fe78bSCy Schubert /*
28*7f2fe78bSCy Schubert * Test program for gss_export_name, intended to be run from a Python test
29*7f2fe78bSCy Schubert * script. Imports a name, canonicalizes it to a mech, exports it,
30*7f2fe78bSCy Schubert * re-imports/exports it to compare results, and then prints the hex form of
31*7f2fe78bSCy Schubert * the exported name followed by a newline.
32*7f2fe78bSCy Schubert *
33*7f2fe78bSCy Schubert * Usage: ./t_export_name [-k|-s] user:username|krb5:princ|host:service@host
34*7f2fe78bSCy Schubert *
35*7f2fe78bSCy Schubert * The name is imported as a username, krb5 principal, or hostbased name.
36*7f2fe78bSCy Schubert * By default or with -k, the name is canonicalized to the krb5 mech; -s
37*7f2fe78bSCy Schubert * indicates SPNEGO instead.
38*7f2fe78bSCy Schubert */
39*7f2fe78bSCy Schubert
40*7f2fe78bSCy Schubert #include <stdio.h>
41*7f2fe78bSCy Schubert #include <stdlib.h>
42*7f2fe78bSCy Schubert #include <string.h>
43*7f2fe78bSCy Schubert
44*7f2fe78bSCy Schubert #include "common.h"
45*7f2fe78bSCy Schubert
46*7f2fe78bSCy Schubert static void
usage(void)47*7f2fe78bSCy Schubert usage(void)
48*7f2fe78bSCy Schubert {
49*7f2fe78bSCy Schubert fprintf(stderr, "Usage: t_export_name [-k|-s] name\n");
50*7f2fe78bSCy Schubert exit(1);
51*7f2fe78bSCy Schubert }
52*7f2fe78bSCy Schubert
53*7f2fe78bSCy Schubert int
main(int argc,char * argv[])54*7f2fe78bSCy Schubert main(int argc, char *argv[])
55*7f2fe78bSCy Schubert {
56*7f2fe78bSCy Schubert OM_uint32 minor, major;
57*7f2fe78bSCy Schubert gss_OID mech = (gss_OID)gss_mech_krb5;
58*7f2fe78bSCy Schubert gss_name_t name, mechname, impname;
59*7f2fe78bSCy Schubert gss_buffer_desc buf, buf2;
60*7f2fe78bSCy Schubert krb5_boolean use_composite = FALSE;
61*7f2fe78bSCy Schubert gss_OID ntype;
62*7f2fe78bSCy Schubert const char *name_arg;
63*7f2fe78bSCy Schubert char opt;
64*7f2fe78bSCy Schubert
65*7f2fe78bSCy Schubert /* Parse arguments. */
66*7f2fe78bSCy Schubert while (argc > 1 && argv[1][0] == '-') {
67*7f2fe78bSCy Schubert opt = argv[1][1];
68*7f2fe78bSCy Schubert argc--, argv++;
69*7f2fe78bSCy Schubert if (opt == 'k')
70*7f2fe78bSCy Schubert mech = &mech_krb5;
71*7f2fe78bSCy Schubert else if (opt == 's')
72*7f2fe78bSCy Schubert mech = &mech_spnego;
73*7f2fe78bSCy Schubert else if (opt == 'c')
74*7f2fe78bSCy Schubert use_composite = TRUE;
75*7f2fe78bSCy Schubert else
76*7f2fe78bSCy Schubert usage();
77*7f2fe78bSCy Schubert }
78*7f2fe78bSCy Schubert if (argc != 2)
79*7f2fe78bSCy Schubert usage();
80*7f2fe78bSCy Schubert name_arg = argv[1];
81*7f2fe78bSCy Schubert
82*7f2fe78bSCy Schubert /* Import the name. */
83*7f2fe78bSCy Schubert name = import_name(name_arg);
84*7f2fe78bSCy Schubert
85*7f2fe78bSCy Schubert /* Canonicalize and export the name. */
86*7f2fe78bSCy Schubert major = gss_canonicalize_name(&minor, name, mech, &mechname);
87*7f2fe78bSCy Schubert check_gsserr("gss_canonicalize_name", major, minor);
88*7f2fe78bSCy Schubert if (use_composite)
89*7f2fe78bSCy Schubert major = gss_export_name_composite(&minor, mechname, &buf);
90*7f2fe78bSCy Schubert else
91*7f2fe78bSCy Schubert major = gss_export_name(&minor, mechname, &buf);
92*7f2fe78bSCy Schubert check_gsserr("gss_export_name", major, minor);
93*7f2fe78bSCy Schubert
94*7f2fe78bSCy Schubert /* Import and re-export the name, and compare the results. */
95*7f2fe78bSCy Schubert ntype = use_composite ? GSS_C_NT_COMPOSITE_EXPORT : GSS_C_NT_EXPORT_NAME;
96*7f2fe78bSCy Schubert major = gss_import_name(&minor, &buf, ntype, &impname);
97*7f2fe78bSCy Schubert check_gsserr("gss_import_name", major, minor);
98*7f2fe78bSCy Schubert if (use_composite)
99*7f2fe78bSCy Schubert major = gss_export_name_composite(&minor, impname, &buf2);
100*7f2fe78bSCy Schubert else
101*7f2fe78bSCy Schubert major = gss_export_name(&minor, impname, &buf2);
102*7f2fe78bSCy Schubert check_gsserr("gss_export_name", major, minor);
103*7f2fe78bSCy Schubert if (buf.length != buf2.length ||
104*7f2fe78bSCy Schubert memcmp(buf.value, buf2.value, buf.length) != 0) {
105*7f2fe78bSCy Schubert fprintf(stderr, "Mismatched results:\n");
106*7f2fe78bSCy Schubert print_hex(stderr, &buf);
107*7f2fe78bSCy Schubert print_hex(stderr, &buf2);
108*7f2fe78bSCy Schubert return 1;
109*7f2fe78bSCy Schubert }
110*7f2fe78bSCy Schubert
111*7f2fe78bSCy Schubert print_hex(stdout, &buf);
112*7f2fe78bSCy Schubert
113*7f2fe78bSCy Schubert (void)gss_release_name(&minor, &name);
114*7f2fe78bSCy Schubert (void)gss_release_name(&minor, &mechname);
115*7f2fe78bSCy Schubert (void)gss_release_name(&minor, &impname);
116*7f2fe78bSCy Schubert (void)gss_release_buffer(&minor, &buf);
117*7f2fe78bSCy Schubert (void)gss_release_buffer(&minor, &buf2);
118*7f2fe78bSCy Schubert return 0;
119*7f2fe78bSCy Schubert }
120