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