No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / admin / list.c
blobaaf4294eb0dcecdb7edfed247c17a048ef718396
1 /*
2 * Copyright (c) 1997-2004 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 <rtbl.h>
37 __RCSID("$Heimdal: list.c 21745 2007-07-31 16:11:25Z lha $"
38 "$NetBSD$");
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_set_separator(table, " ");
91 while((ret = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0){
92 char buf[1024], *s;
94 snprintf(buf, sizeof(buf), "%d", entry.vno);
95 rtbl_add_column_entry_by_id(table, 0, buf);
97 ret = krb5_enctype_to_string(context,
98 entry.keyblock.keytype, &s);
99 if (ret != 0) {
100 snprintf(buf, sizeof(buf), "unknown (%d)", entry.keyblock.keytype);
101 rtbl_add_column_entry_by_id(table, 1, buf);
102 } else {
103 rtbl_add_column_entry_by_id(table, 1, s);
104 free(s);
107 krb5_unparse_name_fixed(context, entry.principal, buf, sizeof(buf));
108 rtbl_add_column_entry_by_id(table, 2, buf);
110 if (opt->timestamp_flag) {
111 krb5_format_time(context, entry.timestamp, buf,
112 sizeof(buf), FALSE);
113 rtbl_add_column_entry_by_id(table, 3, buf);
115 if(opt->keys_flag) {
116 int i;
117 s = malloc(2 * entry.keyblock.keyvalue.length + 1);
118 if (s == NULL) {
119 krb5_warnx(context, "malloc failed");
120 ret = ENOMEM;
121 goto out;
123 for(i = 0; i < entry.keyblock.keyvalue.length; i++)
124 snprintf(s + 2 * i, 3, "%02x",
125 ((unsigned char*)entry.keyblock.keyvalue.data)[i]);
126 rtbl_add_column_entry_by_id(table, 4, s);
127 free(s);
129 krb5_kt_free_entry(context, &entry);
131 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
132 rtbl_format(table, stdout);
134 out:
135 rtbl_destroy(table);
137 krb5_kt_close(context, keytab);
138 return ret;
142 kt_list(struct list_options *opt, int argc, char **argv)
144 krb5_error_code ret;
145 char kt[1024];
147 if(verbose_flag)
148 opt->timestamp_flag = 1;
150 if (keytab_string == NULL) {
151 if((ret = krb5_kt_default_name(context, kt, sizeof(kt))) != 0) {
152 krb5_warn(context, ret, "getting default keytab name");
153 return 1;
155 keytab_string = kt;
157 return do_list(opt, keytab_string) != 0;