xref: /freebsd/crypto/heimdal/lib/gssapi/test_names.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
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 <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdarg.h>
42 #include <gssapi.h>
43 #include <err.h>
44 #include <roken.h>
45 #include <getarg.h>
46 
47 RCSID("$Id: test_names.c 17856 2006-07-20 05:13:25Z lha $");
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", (char *)status_string.value);
66 	    gss_release_buffer (&new_stat, &status_string);
67 	}
68     } while (!GSS_ERROR(ret) && msg_ctx != 0);
69 }
70 
71 static void
72 gss_err(int exitval, int status, const char *fmt, ...)
73 {
74     va_list args;
75 
76     va_start(args, fmt);
77     vwarnx (fmt, args);
78     gss_print_errors (status);
79     va_end(args);
80     exit (exitval);
81 }
82 
83 static int version_flag = 0;
84 static int help_flag	= 0;
85 
86 static struct getargs args[] = {
87     {"version",	0,	arg_flag,	&version_flag, "print version", NULL },
88     {"help",	0,	arg_flag,	&help_flag,  NULL, NULL }
89 };
90 
91 static void
92 usage (int ret)
93 {
94     arg_printusage (args, sizeof(args)/sizeof(*args),
95 		    NULL, "service@host");
96     exit (ret);
97 }
98 
99 
100 int
101 main(int argc, char **argv)
102 {
103     gss_buffer_desc name_buffer;
104     OM_uint32 maj_stat, min_stat;
105     gss_name_t name, MNname, MNname2;
106     int optidx = 0;
107     char *str;
108     int len, equal;
109 
110     setprogname(argv[0]);
111     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
112 	usage(1);
113 
114     if (help_flag)
115 	usage (0);
116 
117     if(version_flag){
118 	print_version(NULL);
119 	exit(0);
120     }
121 
122     argc -= optidx;
123     argv += optidx;
124 
125     /*
126      * test import/export
127      */
128 
129     len = asprintf(&str, "ftp@freeze-arrow.mit.edu");
130     if (len == -1)
131 	errx(1, "asprintf");
132 
133     name_buffer.value = str;
134     name_buffer.length = len;
135 
136     maj_stat = gss_import_name(&min_stat, &name_buffer,
137 			       GSS_C_NT_HOSTBASED_SERVICE,
138 			       &name);
139     if (maj_stat != GSS_S_COMPLETE)
140 	gss_err(1, min_stat, "import name error");
141     free(str);
142 
143     maj_stat = gss_canonicalize_name (&min_stat,
144 				      name,
145 				      GSS_KRB5_MECHANISM,
146 				      &MNname);
147     if (maj_stat != GSS_S_COMPLETE)
148 	gss_err(1, min_stat, "canonicalize name error");
149 
150     maj_stat = gss_export_name(&min_stat,
151 			       MNname,
152 			       &name_buffer);
153     if (maj_stat != GSS_S_COMPLETE)
154 	gss_err(1, min_stat, "export name error (KRB5)");
155 
156     /*
157      * Import the exported name and compare
158      */
159 
160     maj_stat = gss_import_name(&min_stat, &name_buffer,
161 			       GSS_C_NT_EXPORT_NAME,
162 			       &MNname2);
163     if (maj_stat != GSS_S_COMPLETE)
164 	gss_err(1, min_stat, "import name error (exported KRB5 name)");
165 
166 
167     maj_stat = gss_compare_name(&min_stat, MNname, MNname2, &equal);
168     if (maj_stat != GSS_S_COMPLETE)
169 	errx(1, "gss_compare_name");
170     if (!equal)
171 	errx(1, "names not equal");
172 
173     gss_release_name(&min_stat, &MNname2);
174     gss_release_buffer(&min_stat, &name_buffer);
175     gss_release_name(&min_stat, &MNname);
176     gss_release_name(&min_stat, &name);
177 
178     /*
179      * Import oid less name and compare to mech name.
180      * Dovecot SASL lib does this.
181      */
182 
183     len = asprintf(&str, "lha");
184     if (len == -1)
185 	errx(1, "asprintf");
186 
187     name_buffer.value = str;
188     name_buffer.length = len;
189 
190     maj_stat = gss_import_name(&min_stat, &name_buffer,
191 			       GSS_C_NO_OID,
192 			       &name);
193     if (maj_stat != GSS_S_COMPLETE)
194 	gss_err(1, min_stat, "import (no oid) name error");
195 
196     maj_stat = gss_import_name(&min_stat, &name_buffer,
197 			       GSS_KRB5_NT_USER_NAME,
198 			       &MNname);
199     if (maj_stat != GSS_S_COMPLETE)
200 	gss_err(1, min_stat, "import (krb5 mn) name error");
201 
202     free(str);
203 
204     maj_stat = gss_compare_name(&min_stat, name, MNname, &equal);
205     if (maj_stat != GSS_S_COMPLETE)
206 	errx(1, "gss_compare_name");
207     if (!equal)
208 	errx(1, "names not equal");
209 
210     gss_release_name(&min_stat, &MNname);
211     gss_release_name(&min_stat, &name);
212 
213 #if 0
214     maj_stat = gss_canonicalize_name (&min_stat,
215 				      name,
216 				      GSS_SPNEGO_MECHANISM,
217 				      &MNname);
218     if (maj_stat != GSS_S_COMPLETE)
219 	gss_err(1, min_stat, "canonicalize name error");
220 
221 
222     maj_stat = gss_export_name(&maj_stat,
223 			       MNname,
224 			       &name_buffer);
225     if (maj_stat != GSS_S_COMPLETE)
226 	gss_err(1, min_stat, "export name error (SPNEGO)");
227 
228     gss_release_name(&min_stat, &MNname);
229     gss_release_buffer(&min_stat, &name_buffer);
230 #endif
231 
232     return 0;
233 }
234