xref: /minix3/crypto/external/bsd/heimdal/dist/lib/gssapi/gsstool.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: gsstool.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2006 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Portions Copyright (c) 2009 - 2010 Apple Inc. All rights reserved.
9ebfedea0SLionel Sambuc  *
10ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
12ebfedea0SLionel Sambuc  * are met:
13ebfedea0SLionel Sambuc  *
14ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
15ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
16ebfedea0SLionel Sambuc  *
17ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
18ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
19ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
20ebfedea0SLionel Sambuc  *
21ebfedea0SLionel Sambuc  * 3. Neither the name of KTH nor the names of its contributors may be
22ebfedea0SLionel Sambuc  *    used to endorse or promote products derived from this software without
23ebfedea0SLionel Sambuc  *    specific prior written permission.
24ebfedea0SLionel Sambuc  *
25ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
26ebfedea0SLionel Sambuc  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
29ebfedea0SLionel Sambuc  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30ebfedea0SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31ebfedea0SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32ebfedea0SLionel Sambuc  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33ebfedea0SLionel Sambuc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34ebfedea0SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35ebfedea0SLionel Sambuc  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36ebfedea0SLionel Sambuc  */
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc #include <config.h>
39ebfedea0SLionel Sambuc #include <krb5/roken.h>
40ebfedea0SLionel Sambuc 
41ebfedea0SLionel Sambuc #include <stdio.h>
42ebfedea0SLionel Sambuc #include <gssapi/gssapi.h>
43ebfedea0SLionel Sambuc #include <gssapi/gssapi_krb5.h>
44ebfedea0SLionel Sambuc #include <gssapi/gssapi_spnego.h>
45ebfedea0SLionel Sambuc #include <gssapi/gssapi_ntlm.h>
46ebfedea0SLionel Sambuc #include <err.h>
47ebfedea0SLionel Sambuc #include <krb5/getarg.h>
48ebfedea0SLionel Sambuc #include <krb5/rtbl.h>
49ebfedea0SLionel Sambuc #include <gss-commands.h>
50ebfedea0SLionel Sambuc 
51ebfedea0SLionel Sambuc 
52ebfedea0SLionel Sambuc static int version_flag = 0;
53ebfedea0SLionel Sambuc static int help_flag	= 0;
54ebfedea0SLionel Sambuc 
55ebfedea0SLionel Sambuc static struct getargs args[] = {
56ebfedea0SLionel Sambuc     {"version",	0,	arg_flag,	&version_flag, "print version", NULL },
57ebfedea0SLionel Sambuc     {"help",	0,	arg_flag,	&help_flag,  NULL, NULL }
58ebfedea0SLionel Sambuc };
59ebfedea0SLionel Sambuc 
60ebfedea0SLionel Sambuc static void
usage(int ret)61ebfedea0SLionel Sambuc usage (int ret)
62ebfedea0SLionel Sambuc {
63ebfedea0SLionel Sambuc     arg_printusage (args, sizeof(args)/sizeof(*args),
64ebfedea0SLionel Sambuc 		    NULL, "service@host");
65ebfedea0SLionel Sambuc     exit (ret);
66ebfedea0SLionel Sambuc }
67ebfedea0SLionel Sambuc 
68ebfedea0SLionel Sambuc #define COL_OID		"OID"
69ebfedea0SLionel Sambuc #define COL_NAME	"Name"
70ebfedea0SLionel Sambuc #define COL_DESC	"Description"
71ebfedea0SLionel Sambuc #define COL_VALUE	"Value"
72ebfedea0SLionel Sambuc #define COL_MECH	"Mech"
73ebfedea0SLionel Sambuc #define COL_EXPIRE	"Expire"
74ebfedea0SLionel Sambuc #define COL_SASL	"SASL"
75ebfedea0SLionel Sambuc 
76ebfedea0SLionel Sambuc int
supported_mechanisms(void * argptr,int argc,char ** argv)77ebfedea0SLionel Sambuc supported_mechanisms(void *argptr, int argc, char **argv)
78ebfedea0SLionel Sambuc {
79ebfedea0SLionel Sambuc     OM_uint32 maj_stat, min_stat;
80ebfedea0SLionel Sambuc     gss_OID_set mechs;
81ebfedea0SLionel Sambuc     rtbl_t ct;
82ebfedea0SLionel Sambuc     size_t i;
83ebfedea0SLionel Sambuc 
84ebfedea0SLionel Sambuc     maj_stat = gss_indicate_mechs(&min_stat, &mechs);
85ebfedea0SLionel Sambuc     if (maj_stat != GSS_S_COMPLETE)
86ebfedea0SLionel Sambuc 	errx(1, "gss_indicate_mechs failed");
87ebfedea0SLionel Sambuc 
88ebfedea0SLionel Sambuc     printf("Supported mechanisms:\n");
89ebfedea0SLionel Sambuc 
90ebfedea0SLionel Sambuc     ct = rtbl_create();
91ebfedea0SLionel Sambuc     if (ct == NULL)
92ebfedea0SLionel Sambuc 	errx(1, "rtbl_create");
93ebfedea0SLionel Sambuc 
94ebfedea0SLionel Sambuc     rtbl_set_separator(ct, "  ");
95ebfedea0SLionel Sambuc     rtbl_add_column(ct, COL_OID, 0);
96ebfedea0SLionel Sambuc     rtbl_add_column(ct, COL_NAME, 0);
97ebfedea0SLionel Sambuc     rtbl_add_column(ct, COL_DESC, 0);
98ebfedea0SLionel Sambuc     rtbl_add_column(ct, COL_SASL, 0);
99ebfedea0SLionel Sambuc 
100ebfedea0SLionel Sambuc     for (i = 0; i < mechs->count; i++) {
101ebfedea0SLionel Sambuc 	gss_buffer_desc str, sasl_name, mech_name, mech_desc;
102ebfedea0SLionel Sambuc 
103ebfedea0SLionel Sambuc 	maj_stat = gss_oid_to_str(&min_stat, &mechs->elements[i], &str);
104ebfedea0SLionel Sambuc 	if (maj_stat != GSS_S_COMPLETE)
105ebfedea0SLionel Sambuc 	    errx(1, "gss_oid_to_str failed");
106ebfedea0SLionel Sambuc 
107ebfedea0SLionel Sambuc 	rtbl_add_column_entryv(ct, COL_OID, "%.*s",
108ebfedea0SLionel Sambuc 			       (int)str.length, (char *)str.value);
109ebfedea0SLionel Sambuc 	gss_release_buffer(&min_stat, &str);
110ebfedea0SLionel Sambuc 
111ebfedea0SLionel Sambuc 	(void)gss_inquire_saslname_for_mech(&min_stat,
112ebfedea0SLionel Sambuc 					    &mechs->elements[i],
113ebfedea0SLionel Sambuc 					    &sasl_name,
114ebfedea0SLionel Sambuc 					    &mech_name,
115ebfedea0SLionel Sambuc 					    &mech_desc);
116ebfedea0SLionel Sambuc 
117ebfedea0SLionel Sambuc 	rtbl_add_column_entryv(ct, COL_NAME, "%.*s",
118ebfedea0SLionel Sambuc 			       (int)mech_name.length, (char *)mech_name.value);
119ebfedea0SLionel Sambuc 	rtbl_add_column_entryv(ct, COL_DESC, "%.*s",
120ebfedea0SLionel Sambuc 			       (int)mech_desc.length, (char *)mech_desc.value);
121ebfedea0SLionel Sambuc 	rtbl_add_column_entryv(ct, COL_SASL, "%.*s",
122ebfedea0SLionel Sambuc 			       (int)sasl_name.length, (char *)sasl_name.value);
123ebfedea0SLionel Sambuc 
124ebfedea0SLionel Sambuc 	gss_release_buffer(&min_stat, &mech_name);
125ebfedea0SLionel Sambuc 	gss_release_buffer(&min_stat, &mech_desc);
126ebfedea0SLionel Sambuc 	gss_release_buffer(&min_stat, &sasl_name);
127ebfedea0SLionel Sambuc 
128ebfedea0SLionel Sambuc     }
129ebfedea0SLionel Sambuc     gss_release_oid_set(&min_stat, &mechs);
130ebfedea0SLionel Sambuc 
131ebfedea0SLionel Sambuc     rtbl_format(ct, stdout);
132ebfedea0SLionel Sambuc     rtbl_destroy(ct);
133ebfedea0SLionel Sambuc 
134ebfedea0SLionel Sambuc     return 0;
135ebfedea0SLionel Sambuc }
136ebfedea0SLionel Sambuc 
137*0a6a1f1dSLionel Sambuc static void
print_mech_attr(const char * mechname,gss_const_OID mech,gss_OID_set set)138ebfedea0SLionel Sambuc print_mech_attr(const char *mechname, gss_const_OID mech, gss_OID_set set)
139ebfedea0SLionel Sambuc {
140ebfedea0SLionel Sambuc     gss_buffer_desc name, desc;
141ebfedea0SLionel Sambuc     OM_uint32 major, minor;
142ebfedea0SLionel Sambuc     rtbl_t ct;
143ebfedea0SLionel Sambuc     size_t n;
144ebfedea0SLionel Sambuc 
145ebfedea0SLionel Sambuc     ct = rtbl_create();
146ebfedea0SLionel Sambuc     if (ct == NULL)
147ebfedea0SLionel Sambuc 	errx(1, "rtbl_create");
148ebfedea0SLionel Sambuc 
149ebfedea0SLionel Sambuc     rtbl_set_separator(ct, "  ");
150ebfedea0SLionel Sambuc     rtbl_add_column(ct, COL_OID, 0);
151ebfedea0SLionel Sambuc     rtbl_add_column(ct, COL_DESC, 0);
152ebfedea0SLionel Sambuc     if (mech)
153ebfedea0SLionel Sambuc 	rtbl_add_column(ct, COL_VALUE, 0);
154ebfedea0SLionel Sambuc 
155ebfedea0SLionel Sambuc     for (n = 0; n < set->count; n++) {
156ebfedea0SLionel Sambuc 	major = gss_display_mech_attr(&minor, &set->elements[n], &name, &desc, NULL);
157ebfedea0SLionel Sambuc 	if (major)
158ebfedea0SLionel Sambuc 	    continue;
159ebfedea0SLionel Sambuc 
160ebfedea0SLionel Sambuc 	rtbl_add_column_entryv(ct, COL_OID, "%.*s",
161ebfedea0SLionel Sambuc 			       (int)name.length, (char *)name.value);
162ebfedea0SLionel Sambuc 	rtbl_add_column_entryv(ct, COL_DESC, "%.*s",
163ebfedea0SLionel Sambuc 			       (int)desc.length, (char *)desc.value);
164ebfedea0SLionel Sambuc 	if (mech) {
165ebfedea0SLionel Sambuc 	    gss_buffer_desc value;
166ebfedea0SLionel Sambuc 
167ebfedea0SLionel Sambuc 	    if (gss_mo_get(mech, &set->elements[n], &value) != 0)
168ebfedea0SLionel Sambuc 		value.length = 0;
169ebfedea0SLionel Sambuc 
170ebfedea0SLionel Sambuc 	    if (value.length)
171ebfedea0SLionel Sambuc 		rtbl_add_column_entryv(ct, COL_VALUE, "%.*s",
172ebfedea0SLionel Sambuc 				       (int)value.length, (char *)value.value);
173ebfedea0SLionel Sambuc 	    else
174ebfedea0SLionel Sambuc 		rtbl_add_column_entryv(ct, COL_VALUE, "<>");
175ebfedea0SLionel Sambuc 	    gss_release_buffer(&minor, &value);
176ebfedea0SLionel Sambuc 	}
177ebfedea0SLionel Sambuc 
178ebfedea0SLionel Sambuc 	gss_release_buffer(&minor, &name);
179ebfedea0SLionel Sambuc 	gss_release_buffer(&minor, &desc);
180ebfedea0SLionel Sambuc     }
181ebfedea0SLionel Sambuc 
182ebfedea0SLionel Sambuc     printf("attributes for: %s\n", mechname);
183ebfedea0SLionel Sambuc     rtbl_format(ct, stdout);
184ebfedea0SLionel Sambuc     rtbl_destroy(ct);
185ebfedea0SLionel Sambuc }
186ebfedea0SLionel Sambuc 
187ebfedea0SLionel Sambuc 
188ebfedea0SLionel Sambuc int
attrs_for_mech(struct attrs_for_mech_options * opt,int argc,char ** argv)189ebfedea0SLionel Sambuc attrs_for_mech(struct attrs_for_mech_options *opt, int argc, char **argv)
190ebfedea0SLionel Sambuc {
191ebfedea0SLionel Sambuc     gss_OID_set mech_attr = NULL, known_mech_attrs = NULL;
192ebfedea0SLionel Sambuc     gss_OID mech = GSS_C_NO_OID;
193ebfedea0SLionel Sambuc     OM_uint32 major, minor;
194ebfedea0SLionel Sambuc 
195ebfedea0SLionel Sambuc     if (opt->mech_string) {
196ebfedea0SLionel Sambuc 	mech = gss_name_to_oid(opt->mech_string);
197ebfedea0SLionel Sambuc 	if (mech == NULL)
198ebfedea0SLionel Sambuc 	    errx(1, "mech %s is unknown", opt->mech_string);
199ebfedea0SLionel Sambuc     }
200ebfedea0SLionel Sambuc 
201ebfedea0SLionel Sambuc     major = gss_inquire_attrs_for_mech(&minor, mech, &mech_attr, &known_mech_attrs);
202ebfedea0SLionel Sambuc     if (major)
203ebfedea0SLionel Sambuc 	errx(1, "gss_inquire_attrs_for_mech");
204ebfedea0SLionel Sambuc 
205ebfedea0SLionel Sambuc     if (mech) {
206ebfedea0SLionel Sambuc 	print_mech_attr(opt->mech_string, mech, mech_attr);
207ebfedea0SLionel Sambuc     }
208ebfedea0SLionel Sambuc 
209ebfedea0SLionel Sambuc     if (opt->all_flag) {
210ebfedea0SLionel Sambuc 	print_mech_attr("all mechs", NULL, known_mech_attrs);
211ebfedea0SLionel Sambuc     }
212ebfedea0SLionel Sambuc 
213ebfedea0SLionel Sambuc     gss_release_oid_set(&minor, &mech_attr);
214ebfedea0SLionel Sambuc     gss_release_oid_set(&minor, &known_mech_attrs);
215ebfedea0SLionel Sambuc 
216ebfedea0SLionel Sambuc     return 0;
217ebfedea0SLionel Sambuc }
218ebfedea0SLionel Sambuc 
219ebfedea0SLionel Sambuc 
220ebfedea0SLionel Sambuc /*
221ebfedea0SLionel Sambuc  *
222ebfedea0SLionel Sambuc  */
223ebfedea0SLionel Sambuc 
224ebfedea0SLionel Sambuc int
help(void * opt,int argc,char ** argv)225ebfedea0SLionel Sambuc help(void *opt, int argc, char **argv)
226ebfedea0SLionel Sambuc {
227ebfedea0SLionel Sambuc     sl_slc_help(commands, argc, argv);
228ebfedea0SLionel Sambuc     return 0;
229ebfedea0SLionel Sambuc }
230ebfedea0SLionel Sambuc 
231ebfedea0SLionel Sambuc int
main(int argc,char ** argv)232ebfedea0SLionel Sambuc main(int argc, char **argv)
233ebfedea0SLionel Sambuc {
234ebfedea0SLionel Sambuc     int optidx = 0;
235ebfedea0SLionel Sambuc 
236ebfedea0SLionel Sambuc     setprogname(argv[0]);
237ebfedea0SLionel Sambuc     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
238ebfedea0SLionel Sambuc 	usage(1);
239ebfedea0SLionel Sambuc 
240ebfedea0SLionel Sambuc     if (help_flag)
241ebfedea0SLionel Sambuc 	usage (0);
242ebfedea0SLionel Sambuc 
243ebfedea0SLionel Sambuc     if(version_flag){
244ebfedea0SLionel Sambuc 	print_version(NULL);
245ebfedea0SLionel Sambuc 	exit(0);
246ebfedea0SLionel Sambuc     }
247ebfedea0SLionel Sambuc 
248ebfedea0SLionel Sambuc     argc -= optidx;
249ebfedea0SLionel Sambuc     argv += optidx;
250ebfedea0SLionel Sambuc 
251ebfedea0SLionel Sambuc     if (argc == 0) {
252ebfedea0SLionel Sambuc 	help(NULL, argc, argv);
253ebfedea0SLionel Sambuc 	return 1;
254ebfedea0SLionel Sambuc     }
255ebfedea0SLionel Sambuc 
256ebfedea0SLionel Sambuc     return sl_command (commands, argc, argv);
257ebfedea0SLionel Sambuc }
258