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