xref: /minix3/crypto/external/bsd/heimdal/dist/lib/gssapi/krb5/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 #include "gsskrb5_locl.h"
37ebfedea0SLionel Sambuc #include <err.h>
38ebfedea0SLionel Sambuc #include <krb5/getarg.h>
39ebfedea0SLionel Sambuc 
40ebfedea0SLionel Sambuc static void
gss_print_errors(int min_stat)41ebfedea0SLionel Sambuc gss_print_errors (int min_stat)
42ebfedea0SLionel Sambuc {
43ebfedea0SLionel Sambuc     OM_uint32 new_stat;
44ebfedea0SLionel Sambuc     OM_uint32 msg_ctx = 0;
45ebfedea0SLionel Sambuc     gss_buffer_desc status_string;
46ebfedea0SLionel Sambuc     OM_uint32 ret;
47ebfedea0SLionel Sambuc 
48ebfedea0SLionel Sambuc     do {
49ebfedea0SLionel Sambuc 	ret = gss_display_status (&new_stat,
50ebfedea0SLionel Sambuc 				  min_stat,
51ebfedea0SLionel Sambuc 				  GSS_C_MECH_CODE,
52ebfedea0SLionel Sambuc 				  GSS_C_NO_OID,
53ebfedea0SLionel Sambuc 				  &msg_ctx,
54ebfedea0SLionel Sambuc 				  &status_string);
55ebfedea0SLionel Sambuc 	fprintf (stderr, "%.*s\n", (int)status_string.length,
56ebfedea0SLionel Sambuc 				(char *)status_string.value);
57ebfedea0SLionel Sambuc 	gss_release_buffer (&new_stat, &status_string);
58ebfedea0SLionel Sambuc     } while (!GSS_ERROR(ret) && msg_ctx != 0);
59ebfedea0SLionel Sambuc }
60ebfedea0SLionel Sambuc 
61ebfedea0SLionel Sambuc static void
gss_err(int exitval,int status,const char * fmt,...)62ebfedea0SLionel Sambuc gss_err(int exitval, int status, const char *fmt, ...)
63ebfedea0SLionel Sambuc {
64ebfedea0SLionel Sambuc     va_list args;
65ebfedea0SLionel Sambuc 
66ebfedea0SLionel Sambuc     va_start(args, fmt);
67ebfedea0SLionel Sambuc     vwarnx (fmt, args);
68ebfedea0SLionel Sambuc     gss_print_errors (status);
69ebfedea0SLionel Sambuc     va_end(args);
70ebfedea0SLionel Sambuc     exit (exitval);
71ebfedea0SLionel Sambuc }
72ebfedea0SLionel Sambuc 
73ebfedea0SLionel Sambuc static void
acquire_release_loop(gss_name_t name,int counter,gss_cred_usage_t usage)74ebfedea0SLionel Sambuc acquire_release_loop(gss_name_t name, int counter, gss_cred_usage_t usage)
75ebfedea0SLionel Sambuc {
76ebfedea0SLionel Sambuc     OM_uint32 maj_stat, min_stat;
77ebfedea0SLionel Sambuc     gss_cred_id_t cred;
78ebfedea0SLionel Sambuc     int i;
79ebfedea0SLionel Sambuc 
80ebfedea0SLionel Sambuc     for (i = 0; i < counter; i++) {
81ebfedea0SLionel Sambuc 	maj_stat = gss_acquire_cred(&min_stat, name,
82ebfedea0SLionel Sambuc 				    GSS_C_INDEFINITE,
83ebfedea0SLionel Sambuc 				    GSS_C_NO_OID_SET,
84ebfedea0SLionel Sambuc 				    usage,
85ebfedea0SLionel Sambuc 				    &cred,
86ebfedea0SLionel Sambuc 				    NULL,
87ebfedea0SLionel Sambuc 				    NULL);
88ebfedea0SLionel Sambuc 	if (maj_stat != GSS_S_COMPLETE)
89ebfedea0SLionel Sambuc 	    gss_err(1, min_stat, "aquire %d %d != GSS_S_COMPLETE",
90ebfedea0SLionel Sambuc 		    i, (int)maj_stat);
91ebfedea0SLionel Sambuc 
92ebfedea0SLionel Sambuc 	maj_stat = gss_release_cred(&min_stat, &cred);
93ebfedea0SLionel Sambuc 	if (maj_stat != GSS_S_COMPLETE)
94ebfedea0SLionel Sambuc 	    gss_err(1, min_stat, "release %d %d != GSS_S_COMPLETE",
95ebfedea0SLionel Sambuc 		    i, (int)maj_stat);
96ebfedea0SLionel Sambuc     }
97ebfedea0SLionel Sambuc }
98ebfedea0SLionel Sambuc 
99ebfedea0SLionel Sambuc 
100ebfedea0SLionel Sambuc static void
acquire_add_release_add(gss_name_t name,gss_cred_usage_t usage)101ebfedea0SLionel Sambuc acquire_add_release_add(gss_name_t name, gss_cred_usage_t usage)
102ebfedea0SLionel Sambuc {
103ebfedea0SLionel Sambuc     OM_uint32 maj_stat, min_stat;
104ebfedea0SLionel Sambuc     gss_cred_id_t cred, cred2, cred3;
105ebfedea0SLionel Sambuc 
106ebfedea0SLionel Sambuc     maj_stat = gss_acquire_cred(&min_stat, name,
107ebfedea0SLionel Sambuc 				GSS_C_INDEFINITE,
108ebfedea0SLionel Sambuc 				GSS_C_NO_OID_SET,
109ebfedea0SLionel Sambuc 				usage,
110ebfedea0SLionel Sambuc 				&cred,
111ebfedea0SLionel Sambuc 				NULL,
112ebfedea0SLionel Sambuc 				NULL);
113ebfedea0SLionel Sambuc     if (maj_stat != GSS_S_COMPLETE)
114ebfedea0SLionel Sambuc 	gss_err(1, min_stat, "aquire %d != GSS_S_COMPLETE", (int)maj_stat);
115ebfedea0SLionel Sambuc 
116ebfedea0SLionel Sambuc     maj_stat = gss_add_cred(&min_stat,
117ebfedea0SLionel Sambuc 			    cred,
118ebfedea0SLionel Sambuc 			    GSS_C_NO_NAME,
119ebfedea0SLionel Sambuc 			    GSS_KRB5_MECHANISM,
120ebfedea0SLionel Sambuc 			    usage,
121ebfedea0SLionel Sambuc 			    GSS_C_INDEFINITE,
122ebfedea0SLionel Sambuc 			    GSS_C_INDEFINITE,
123ebfedea0SLionel Sambuc 			    &cred2,
124ebfedea0SLionel Sambuc 			    NULL,
125ebfedea0SLionel Sambuc 			    NULL,
126ebfedea0SLionel Sambuc 			    NULL);
127ebfedea0SLionel Sambuc 
128ebfedea0SLionel Sambuc     if (maj_stat != GSS_S_COMPLETE)
129ebfedea0SLionel Sambuc 	gss_err(1, min_stat, "add_cred %d != GSS_S_COMPLETE", (int)maj_stat);
130ebfedea0SLionel Sambuc 
131ebfedea0SLionel Sambuc     maj_stat = gss_release_cred(&min_stat, &cred);
132ebfedea0SLionel Sambuc     if (maj_stat != GSS_S_COMPLETE)
133ebfedea0SLionel Sambuc 	gss_err(1, min_stat, "release %d != GSS_S_COMPLETE", (int)maj_stat);
134ebfedea0SLionel Sambuc 
135ebfedea0SLionel Sambuc     maj_stat = gss_add_cred(&min_stat,
136ebfedea0SLionel Sambuc 			    cred2,
137ebfedea0SLionel Sambuc 			    GSS_C_NO_NAME,
138ebfedea0SLionel Sambuc 			    GSS_KRB5_MECHANISM,
139ebfedea0SLionel Sambuc 			    GSS_C_BOTH,
140ebfedea0SLionel Sambuc 			    GSS_C_INDEFINITE,
141ebfedea0SLionel Sambuc 			    GSS_C_INDEFINITE,
142ebfedea0SLionel Sambuc 			    &cred3,
143ebfedea0SLionel Sambuc 			    NULL,
144ebfedea0SLionel Sambuc 			    NULL,
145ebfedea0SLionel Sambuc 			    NULL);
146ebfedea0SLionel Sambuc 
147ebfedea0SLionel Sambuc     maj_stat = gss_release_cred(&min_stat, &cred2);
148ebfedea0SLionel Sambuc     if (maj_stat != GSS_S_COMPLETE)
149ebfedea0SLionel Sambuc 	gss_err(1, min_stat, "release 2 %d != GSS_S_COMPLETE", (int)maj_stat);
150ebfedea0SLionel Sambuc 
151ebfedea0SLionel Sambuc     maj_stat = gss_release_cred(&min_stat, &cred3);
152ebfedea0SLionel Sambuc     if (maj_stat != GSS_S_COMPLETE)
153ebfedea0SLionel Sambuc 	gss_err(1, min_stat, "release 2 %d != GSS_S_COMPLETE", (int)maj_stat);
154ebfedea0SLionel Sambuc }
155ebfedea0SLionel Sambuc 
156ebfedea0SLionel Sambuc static int version_flag = 0;
157ebfedea0SLionel Sambuc static int help_flag	= 0;
158ebfedea0SLionel Sambuc 
159ebfedea0SLionel Sambuc static struct getargs args[] = {
160ebfedea0SLionel Sambuc     {"version",	0,	arg_flag,	&version_flag, "print version", NULL },
161ebfedea0SLionel Sambuc     {"help",	0,	arg_flag,	&help_flag,  NULL, NULL }
162ebfedea0SLionel Sambuc };
163ebfedea0SLionel Sambuc 
164ebfedea0SLionel Sambuc static void
usage(int ret)165ebfedea0SLionel Sambuc usage (int ret)
166ebfedea0SLionel Sambuc {
167ebfedea0SLionel Sambuc     arg_printusage (args, sizeof(args)/sizeof(*args),
168ebfedea0SLionel Sambuc 		    NULL, "service@host");
169ebfedea0SLionel Sambuc     exit (ret);
170ebfedea0SLionel Sambuc }
171ebfedea0SLionel Sambuc 
172ebfedea0SLionel Sambuc 
173ebfedea0SLionel Sambuc int
main(int argc,char ** argv)174ebfedea0SLionel Sambuc main(int argc, char **argv)
175ebfedea0SLionel Sambuc {
176ebfedea0SLionel Sambuc     struct gss_buffer_desc_struct name_buffer;
177ebfedea0SLionel Sambuc     OM_uint32 maj_stat, min_stat;
178ebfedea0SLionel Sambuc     gss_name_t name;
179ebfedea0SLionel Sambuc     int optidx = 0;
180ebfedea0SLionel Sambuc 
181ebfedea0SLionel Sambuc     setprogname(argv[0]);
182ebfedea0SLionel Sambuc     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
183ebfedea0SLionel Sambuc 	usage(1);
184ebfedea0SLionel Sambuc 
185ebfedea0SLionel Sambuc     if (help_flag)
186ebfedea0SLionel Sambuc 	usage (0);
187ebfedea0SLionel Sambuc 
188ebfedea0SLionel Sambuc     if(version_flag){
189ebfedea0SLionel Sambuc 	print_version(NULL);
190ebfedea0SLionel Sambuc 	exit(0);
191ebfedea0SLionel Sambuc     }
192ebfedea0SLionel Sambuc 
193ebfedea0SLionel Sambuc     argc -= optidx;
194ebfedea0SLionel Sambuc     argv += optidx;
195ebfedea0SLionel Sambuc 
196ebfedea0SLionel Sambuc     if (argc < 1)
197ebfedea0SLionel Sambuc 	errx(1, "argc < 1");
198ebfedea0SLionel Sambuc 
199ebfedea0SLionel Sambuc     name_buffer.value = argv[0];
200ebfedea0SLionel Sambuc     name_buffer.length = strlen(argv[0]);
201ebfedea0SLionel Sambuc 
202ebfedea0SLionel Sambuc     maj_stat = gss_import_name(&min_stat, &name_buffer,
203ebfedea0SLionel Sambuc 			       GSS_C_NT_HOSTBASED_SERVICE,
204ebfedea0SLionel Sambuc 			       &name);
205ebfedea0SLionel Sambuc     if (maj_stat != GSS_S_COMPLETE)
206ebfedea0SLionel Sambuc 	errx(1, "import name error");
207ebfedea0SLionel Sambuc 
208ebfedea0SLionel Sambuc     acquire_release_loop(name, 100, GSS_C_ACCEPT);
209ebfedea0SLionel Sambuc     acquire_release_loop(name, 100, GSS_C_INITIATE);
210ebfedea0SLionel Sambuc     acquire_release_loop(name, 100, GSS_C_BOTH);
211ebfedea0SLionel Sambuc 
212ebfedea0SLionel Sambuc     acquire_add_release_add(name, GSS_C_ACCEPT);
213ebfedea0SLionel Sambuc     acquire_add_release_add(name, GSS_C_INITIATE);
214ebfedea0SLionel Sambuc     acquire_add_release_add(name, GSS_C_BOTH);
215ebfedea0SLionel Sambuc 
216ebfedea0SLionel Sambuc     gss_release_name(&min_stat, &name);
217ebfedea0SLionel Sambuc 
218ebfedea0SLionel Sambuc     return 0;
219ebfedea0SLionel Sambuc }
220