cf/largefile.m4: Fix build with autoconf-2.72
[heimdal.git] / admin / list.c
blob22ccdcac8cb20dd1ebff79af9f24b4ddaea0a8e2
1 /*
2 * Copyright (c) 1997-2022 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "ktutil_locl.h"
35 #include <heimbase.h>
36 #include <rtbl.h>
38 RCSID("$Id$");
40 static int
41 do_list(struct list_options *opt, const char *keytab_str)
43 krb5_error_code ret;
44 krb5_keytab keytab;
45 krb5_keytab_entry entry;
46 krb5_kt_cursor cursor;
47 rtbl_t table;
49 /* XXX specialcase the ANY type */
50 if(strncasecmp(keytab_str, "ANY:", 4) == 0) {
51 int flag = 0;
52 char buf[1024];
53 keytab_str += 4;
54 ret = 0;
55 while (strsep_copy((const char**)&keytab_str, ",",
56 buf, sizeof(buf)) != -1) {
57 if(flag)
58 printf("\n");
59 if(do_list(opt, buf))
60 ret = 1;
61 flag = 1;
63 return ret;
66 ret = krb5_kt_resolve(context, keytab_str, &keytab);
67 if (ret) {
68 krb5_warn(context, ret, "resolving keytab %s", keytab_str);
69 return ret;
72 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
73 if(ret) {
74 krb5_warn(context, ret, "krb5_kt_start_seq_get %s", keytab_str);
75 krb5_kt_close(context, keytab);
76 return ret;
79 printf ("%s:\n\n", keytab_str);
81 table = rtbl_create();
82 rtbl_add_column_by_id(table, 0, "Vno", RTBL_ALIGN_RIGHT);
83 rtbl_add_column_by_id(table, 1, "Type", 0);
84 rtbl_add_column_by_id(table, 2, "Principal", 0);
85 if (opt->timestamp_flag)
86 rtbl_add_column_by_id(table, 3, "Date", 0);
87 if(opt->keys_flag)
88 rtbl_add_column_by_id(table, 4, "Key", 0);
89 rtbl_add_column_by_id(table, 5, "Aliases", 0);
90 rtbl_set_separator(table, " ");
92 while(krb5_kt_next_entry(context, keytab, &entry, &cursor) == 0){
93 char buf[1024], *s;
95 snprintf(buf, sizeof(buf), "%d", entry.vno);
96 rtbl_add_column_entry_by_id(table, 0, buf);
98 ret = krb5_enctype_to_string(context,
99 entry.keyblock.keytype, &s);
100 if (ret != 0) {
101 snprintf(buf, sizeof(buf), "unknown (%d)", entry.keyblock.keytype);
102 rtbl_add_column_entry_by_id(table, 1, buf);
103 } else {
104 rtbl_add_column_entry_by_id(table, 1, s);
105 free(s);
108 krb5_unparse_name_fixed(context, entry.principal, buf, sizeof(buf));
109 rtbl_add_column_entry_by_id(table, 2, buf);
111 if (opt->timestamp_flag) {
112 krb5_format_time(context, entry.timestamp, buf,
113 sizeof(buf), FALSE);
114 rtbl_add_column_entry_by_id(table, 3, buf);
116 if(opt->keys_flag) {
117 size_t i;
118 s = malloc(2 * entry.keyblock.keyvalue.length + 1);
119 if (s == NULL) {
120 krb5_warnx(context, "malloc failed");
121 ret = ENOMEM;
122 goto out;
124 for(i = 0; i < entry.keyblock.keyvalue.length; i++)
125 snprintf(s + 2 * i, 3, "%02x",
126 ((unsigned char*)entry.keyblock.keyvalue.data)[i]);
127 rtbl_add_column_entry_by_id(table, 4, s);
128 free(s);
130 if (entry.aliases) {
131 unsigned int i;
132 struct rk_strpool *p = NULL;
134 for (i = 0; i< entry.aliases->len; i++) {
135 krb5_unparse_name_fixed(context, &entry.aliases->val[i],
136 buf, sizeof(buf));
137 p = rk_strpoolprintf(p, "%s%s", buf,
138 i + 1 < entry.aliases->len ? ", " : "");
141 rtbl_add_column_entry_by_id(table, 5, (s = rk_strpoolcollect(p)));
142 free(s);
145 krb5_kt_free_entry(context, &entry);
147 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
148 rtbl_format(table, stdout);
150 out:
151 rtbl_destroy(table);
153 krb5_kt_close(context, keytab);
154 return ret;
157 static int
158 do_list1_json(struct list_options *opt,
159 const char *keytab_str,
160 heim_array_t a)
162 krb5_error_code ret;
163 krb5_keytab keytab;
164 krb5_keytab_entry entry;
165 krb5_kt_cursor cursor;
167 ret = krb5_kt_resolve(context, keytab_str, &keytab);
168 if (ret) {
169 krb5_warn(context, ret, "resolving keytab %s", keytab_str);
170 return ret;
173 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
174 if(ret) {
175 krb5_warn(context, ret, "krb5_kt_start_seq_get %s", keytab_str);
176 krb5_kt_close(context, keytab);
177 return ret;
180 //if (opt->timestamp_flag)
181 //if (opt->keys_flag)
183 while (krb5_kt_next_entry(context, keytab, &entry, &cursor) == 0) {
184 heim_dict_t d = heim_dict_create(5);
185 heim_object_t o;
186 char *s;
188 heim_array_append_value(a, d);
189 heim_dict_set_value(d, HSTR("keytab"),
190 o = heim_string_create(keytab_str)); heim_release(o);
191 heim_dict_set_value(d, HSTR("kvno"), o = heim_number_create(entry.vno));
192 heim_release(o);
193 heim_dict_set_value(d, HSTR("enctype_number"),
194 o = heim_number_create(entry.keyblock.keytype));
195 heim_release(o);
196 heim_dict_set_value(d, HSTR("flags"),
197 o = heim_number_create(entry.flags));
198 heim_release(o);
199 ret = krb5_enctype_to_string(context, entry.keyblock.keytype, &s);
200 if (ret == 0) {
201 heim_dict_set_value(d, HSTR("enctype"), o = heim_string_create(s));
202 heim_release(o);
203 free(s);
205 heim_dict_set_value(d, HSTR("timestamp"),
206 o = heim_number_create(entry.timestamp));
207 heim_release(o);
209 ret = krb5_unparse_name(context, entry.principal, &s);
210 if (ret)
211 krb5_err(context, 1, ret, "Could not format principal");
212 heim_dict_set_value(d, HSTR("principal"), o = heim_string_create(s));
213 heim_release(o);
214 free(s);
216 if (opt->keys_flag) {
217 o = heim_data_create(entry.keyblock.keyvalue.data,
218 entry.keyblock.keyvalue.length);
219 heim_dict_set_value(d, HSTR("key"), o);
220 heim_release(o);
222 if (entry.aliases) {
223 heim_array_t aliases = heim_array_create();
224 unsigned int i;
226 for (i = 0; i< entry.aliases->len; i++) {
227 ret = krb5_unparse_name(context, &entry.aliases->val[i], &s);
228 if (ret)
229 krb5_err(context, 1, ret, "Could not format principal");
230 heim_array_append_value(aliases, o = heim_string_create(s));
231 heim_release(o);
232 free(s);
234 heim_dict_set_value(d, HSTR("aliases"), aliases);
235 heim_release(aliases);
238 krb5_kt_free_entry(context, &entry);
239 heim_release(d);
242 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
243 krb5_kt_close(context, keytab);
244 return ret;
247 static int
248 do_list_json(struct list_options *opt, const char *keytab_str)
250 krb5_error_code ret = 0;
251 heim_json_flags_t flags =
252 (HEIM_JSON_F_STRICT | HEIM_JSON_F_INDENT2 | HEIM_JSON_F_NO_DATA_DICT) &
253 ~HEIM_JSON_F_NO_DATA;
254 heim_array_t a = heim_array_create();
255 heim_string_t s;
258 * Special-case the ANY: keytab type. What do we get from this? We get to
259 * include the actual keytab name for each entry in its JSON
260 * representation. Otherwise there would be no point because the ANY:
261 * keytab type iterates all the keytabs it joins.
263 * Why strncasecmp() though? Because do_list() uses it, though it arguably
264 * never should have.
266 if (strncasecmp(keytab_str, "ANY:", 4) == 0) {
267 char buf[1024];
269 keytab_str += 4;
270 ret = 0;
271 while (strsep_copy((const char**)&keytab_str, ",",
272 buf, sizeof(buf)) != -1) {
273 if (do_list1_json(opt, buf, a))
274 ret = 1;
276 } else {
277 ret = do_list1_json(opt, keytab_str, a);
280 s = heim_json_copy_serialize(a, flags, NULL);
281 printf("%s", heim_string_get_utf8(s));
282 heim_release(a);
283 heim_release(s);
284 return ret;
288 kt_list(struct list_options *opt, int argc, char **argv)
290 krb5_error_code ret;
291 char kt[1024];
293 if(verbose_flag)
294 opt->timestamp_flag = 1;
296 if (keytab_string == NULL) {
297 if((ret = krb5_kt_default_name(context, kt, sizeof(kt))) != 0) {
298 krb5_warn(context, ret, "getting default keytab name");
299 return 1;
301 keytab_string = kt;
303 if (opt->json_flag)
304 return do_list_json(opt, keytab_string) != 0;
305 return do_list(opt, keytab_string) != 0;