handle printing mech attributes
[heimdal.git] / lib / gssapi / gsstool.c
blobe5971880a803785ebbc31fffc9fc106221899fd0
1 /*
2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2009 - 2010 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of KTH nor the names of its contributors may be
20 * used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <config.h>
38 #include <stdio.h>
39 #include <gssapi.h>
40 #include <gssapi_krb5.h>
41 #include <gssapi_spnego.h>
42 #include <gssapi_ntlm.h>
43 #include <err.h>
44 #include <roken.h>
45 #include <getarg.h>
46 #include <rtbl.h>
47 #include <gss-commands.h>
50 static int version_flag = 0;
51 static int help_flag = 0;
53 static struct getargs args[] = {
54 {"version", 0, arg_flag, &version_flag, "print version", NULL },
55 {"help", 0, arg_flag, &help_flag, NULL, NULL }
58 static void
59 usage (int ret)
61 arg_printusage (args, sizeof(args)/sizeof(*args),
62 NULL, "service@host");
63 exit (ret);
66 #define COL_OID "OID"
67 #define COL_NAME "Name"
68 #define COL_DESC "Description"
69 #define COL_VALUE "Value"
70 #define COL_MECH "Mech"
71 #define COL_EXPIRE "Expire"
73 int
74 supported_mechanisms(void *argptr, int argc, char **argv)
76 OM_uint32 maj_stat, min_stat;
77 gss_OID_set mechs;
78 rtbl_t ct;
79 size_t i;
81 maj_stat = gss_indicate_mechs(&min_stat, &mechs);
82 if (maj_stat != GSS_S_COMPLETE)
83 errx(1, "gss_indicate_mechs failed");
85 printf("Supported mechanisms:\n");
87 ct = rtbl_create();
88 if (ct == NULL)
89 errx(1, "rtbl_create");
91 rtbl_set_separator(ct, " ");
92 rtbl_add_column(ct, COL_OID, 0);
93 rtbl_add_column(ct, COL_NAME, 0);
95 for (i = 0; i < mechs->count; i++) {
96 gss_buffer_desc str;
97 const char *name = NULL;
99 maj_stat = gss_oid_to_str(&min_stat, &mechs->elements[i], &str);
100 if (maj_stat != GSS_S_COMPLETE)
101 errx(1, "gss_oid_to_str failed");
103 rtbl_add_column_entryv(ct, COL_OID, "%.*s",
104 (int)str.length, (char *)str.value);
105 gss_release_buffer(&min_stat, &str);
107 name = gss_oid_to_name(&mechs->elements[i]);
108 if (name)
109 rtbl_add_column_entry(ct, COL_NAME, name);
111 gss_release_oid_set(&min_stat, &mechs);
113 rtbl_format(ct, stdout);
114 rtbl_destroy(ct);
116 return 0;
119 void static
120 print_mech_attr(const char *mechname, gss_const_OID mech, gss_OID_set set)
122 gss_buffer_desc name, desc;
123 OM_uint32 major, minor;
124 rtbl_t ct;
125 size_t n;
127 ct = rtbl_create();
128 if (ct == NULL)
129 errx(1, "rtbl_create");
131 rtbl_set_separator(ct, " ");
132 rtbl_add_column(ct, COL_OID, 0);
133 rtbl_add_column(ct, COL_DESC, 0);
134 if (mech)
135 rtbl_add_column(ct, COL_VALUE, 0);
137 for (n = 0; n < set->count; n++) {
138 major = gss_display_mech_attr(&minor, &set->elements[n], &name, &desc, NULL);
139 if (major)
140 continue;
142 rtbl_add_column_entryv(ct, COL_OID, "%.*s",
143 (int)name.length, (char *)name.value);
144 rtbl_add_column_entryv(ct, COL_DESC, "%.*s",
145 (int)desc.length, (char *)desc.value);
146 if (mech) {
147 gss_buffer_desc value;
149 if (gss_mo_get(mech, &set->elements[n], &value) != 0)
150 value.length = 0;
152 if (value.length)
153 rtbl_add_column_entryv(ct, COL_VALUE, "%.*s",
154 (int)value.length, (char *)value.value);
155 else
156 rtbl_add_column_entryv(ct, COL_VALUE, "<>");
157 gss_release_buffer(&minor, &value);
160 gss_release_buffer(&minor, &name);
161 gss_release_buffer(&minor, &desc);
164 printf("attributes for: %s\n", mechname);
165 rtbl_format(ct, stdout);
166 rtbl_destroy(ct);
171 attrs_for_mech(struct attrs_for_mech_options *opt, int argc, char **argv)
173 gss_OID_set mech_attr = NULL, known_mech_attrs = NULL;
174 gss_OID mech = GSS_C_NO_OID;
175 OM_uint32 major, minor;
177 if (opt->mech_string) {
178 mech = gss_name_to_oid(opt->mech_string);
179 if (mech == NULL)
180 errx(1, "mech %s is unknown", opt->mech_string);
183 major = gss_inquire_attrs_for_mech(&minor, mech, &mech_attr, &known_mech_attrs);
184 if (major)
185 errx(1, "gss_inquire_attrs_for_mech");
187 if (mech) {
188 print_mech_attr(opt->mech_string, mech, mech_attr);
191 if (opt->all_flag) {
192 print_mech_attr("all mechs", NULL, known_mech_attrs);
195 gss_release_oid_set(&minor, &mech_attr);
196 gss_release_oid_set(&minor, &known_mech_attrs);
198 return 0;
207 help(void *opt, int argc, char **argv)
209 sl_slc_help(commands, argc, argv);
210 return 0;
214 main(int argc, char **argv)
216 int optidx = 0;
218 setprogname(argv[0]);
219 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
220 usage(1);
222 if (help_flag)
223 usage (0);
225 if(version_flag){
226 print_version(NULL);
227 exit(0);
230 argc -= optidx;
231 argv += optidx;
233 if (argc == 0) {
234 help(NULL, argc, argv);
235 return 1;
238 return sl_command (commands, argc, argv);