xref: /freebsd/crypto/heimdal/lib/gssapi/test_names.c (revision 25ecdc7d52770caf1c9b44b5ec11f468f6b636f3)
1 /*
2  * Copyright (c) 2006 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of KTH nor the names of its contributors may be
18  *    used to endorse or promote products derived from this software without
19  *    specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37 
38 #include <roken.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <stdarg.h>
43 #include <gssapi.h>
44 #include <gssapi_krb5.h>
45 #include <gssapi_spnego.h>
46 #include <err.h>
47 #include <getarg.h>
48 
49 static void
50 gss_print_errors (int min_stat)
51 {
52     OM_uint32 new_stat;
53     OM_uint32 msg_ctx = 0;
54     gss_buffer_desc status_string;
55     OM_uint32 ret;
56 
57     do {
58 	ret = gss_display_status (&new_stat,
59 				  min_stat,
60 				  GSS_C_MECH_CODE,
61 				  GSS_C_NO_OID,
62 				  &msg_ctx,
63 				  &status_string);
64 	if (!GSS_ERROR(ret)) {
65 	    fprintf (stderr, "%.*s\n", (int)status_string.length,
66 					(char *)status_string.value);
67 	    gss_release_buffer (&new_stat, &status_string);
68 	}
69     } while (!GSS_ERROR(ret) && msg_ctx != 0);
70 }
71 
72 static void
73 gss_err(int exitval, int status, const char *fmt, ...)
74 {
75     va_list args;
76 
77     va_start(args, fmt);
78     vwarnx (fmt, args);
79     gss_print_errors (status);
80     va_end(args);
81     exit (exitval);
82 }
83 
84 static int version_flag = 0;
85 static int help_flag	= 0;
86 
87 static struct getargs args[] = {
88     {"version",	0,	arg_flag,	&version_flag, "print version", NULL },
89     {"help",	0,	arg_flag,	&help_flag,  NULL, NULL }
90 };
91 
92 static void
93 usage (int ret)
94 {
95     arg_printusage (args, sizeof(args)/sizeof(*args),
96 		    NULL, "service@host");
97     exit (ret);
98 }
99 
100 
101 int
102 main(int argc, char **argv)
103 {
104     gss_buffer_desc name_buffer;
105     OM_uint32 maj_stat, min_stat;
106     gss_name_t name, MNname, MNname2;
107     int optidx = 0;
108     char *str;
109     int len, equal;
110 
111     setprogname(argv[0]);
112     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
113 	usage(1);
114 
115     if (help_flag)
116 	usage (0);
117 
118     if(version_flag){
119 	print_version(NULL);
120 	exit(0);
121     }
122 
123     argc -= optidx;
124     argv += optidx;
125 
126     gsskrb5_set_default_realm("MIT.EDU");
127 
128     /*
129      * test import/export
130      */
131 
132     str = NULL;
133     len = asprintf(&str, "ftp@freeze-arrow.mit.edu");
134     if (len < 0 || str == NULL)
135 	errx(1, "asprintf");
136 
137     name_buffer.value = str;
138     name_buffer.length = len;
139 
140     maj_stat = gss_import_name(&min_stat, &name_buffer,
141 			       GSS_C_NT_HOSTBASED_SERVICE,
142 			       &name);
143     if (maj_stat != GSS_S_COMPLETE)
144 	gss_err(1, min_stat, "import name error");
145     free(str);
146 
147     maj_stat = gss_canonicalize_name (&min_stat,
148 				      name,
149 				      GSS_KRB5_MECHANISM,
150 				      &MNname);
151     if (maj_stat != GSS_S_COMPLETE)
152 	gss_err(1, min_stat, "canonicalize name error");
153 
154     maj_stat = gss_export_name(&min_stat,
155 			       MNname,
156 			       &name_buffer);
157     if (maj_stat != GSS_S_COMPLETE)
158 	gss_err(1, min_stat, "export name error (KRB5)");
159 
160     /*
161      * Import the exported name and compare
162      */
163 
164     maj_stat = gss_import_name(&min_stat, &name_buffer,
165 			       GSS_C_NT_EXPORT_NAME,
166 			       &MNname2);
167     if (maj_stat != GSS_S_COMPLETE)
168 	gss_err(1, min_stat, "import name error (exported KRB5 name)");
169 
170 
171     maj_stat = gss_compare_name(&min_stat, MNname, MNname2, &equal);
172     if (maj_stat != GSS_S_COMPLETE)
173 	errx(1, "gss_compare_name");
174     if (!equal)
175 	errx(1, "names not equal");
176 
177     gss_release_name(&min_stat, &MNname2);
178     gss_release_buffer(&min_stat, &name_buffer);
179     gss_release_name(&min_stat, &MNname);
180     gss_release_name(&min_stat, &name);
181 
182     /*
183      * Import oid less name and compare to mech name.
184      * Dovecot SASL lib does this.
185      */
186 
187     str = NULL;
188     len = asprintf(&str, "lha");
189     if (len < 0 || str == NULL)
190 	errx(1, "asprintf");
191 
192     name_buffer.value = str;
193     name_buffer.length = len;
194 
195     maj_stat = gss_import_name(&min_stat, &name_buffer,
196 			       GSS_C_NO_OID,
197 			       &name);
198     if (maj_stat != GSS_S_COMPLETE)
199 	gss_err(1, min_stat, "import (no oid) name error");
200 
201     maj_stat = gss_import_name(&min_stat, &name_buffer,
202 			       GSS_KRB5_NT_USER_NAME,
203 			       &MNname);
204     if (maj_stat != GSS_S_COMPLETE)
205 	gss_err(1, min_stat, "import (krb5 mn) name error");
206 
207     free(str);
208 
209     maj_stat = gss_compare_name(&min_stat, name, MNname, &equal);
210     if (maj_stat != GSS_S_COMPLETE)
211 	errx(1, "gss_compare_name");
212     if (!equal)
213 	errx(1, "names not equal");
214 
215     gss_release_name(&min_stat, &MNname);
216     gss_release_name(&min_stat, &name);
217 
218 #if 0
219     maj_stat = gss_canonicalize_name (&min_stat,
220 				      name,
221 				      GSS_SPNEGO_MECHANISM,
222 				      &MNname);
223     if (maj_stat != GSS_S_COMPLETE)
224 	gss_err(1, min_stat, "canonicalize name error");
225 
226 
227     maj_stat = gss_export_name(&maj_stat,
228 			       MNname,
229 			       &name_buffer);
230     if (maj_stat != GSS_S_COMPLETE)
231 	gss_err(1, min_stat, "export name error (SPNEGO)");
232 
233     gss_release_name(&min_stat, &MNname);
234     gss_release_buffer(&min_stat, &name_buffer);
235 #endif
236 
237     return 0;
238 }
239