This commit was manufactured by cvs2svn to create tag 'heimdal-0-7'.
[heimdal.git] / admin / list.c
blob9718d29e7acea3983b5696c156649cbded6d0464
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("$Id$");
39 static int
40 do_list(struct list_options *opt, const char *keytab_string)
42 krb5_error_code ret;
43 krb5_keytab keytab;
44 krb5_keytab_entry entry;
45 krb5_kt_cursor cursor;
46 rtbl_t table;
48 /* XXX specialcase the ANY type */
49 if(strncasecmp(keytab_string, "ANY:", 4) == 0) {
50 int flag = 0;
51 char buf[1024];
52 keytab_string += 4;
53 ret = 0;
54 while (strsep_copy((const char**)&keytab_string, ",",
55 buf, sizeof(buf)) != -1) {
56 if(flag)
57 printf("\n");
58 if(do_list(opt, buf))
59 ret = 1;
60 flag = 1;
62 return ret;
65 ret = krb5_kt_resolve(context, keytab_string, &keytab);
66 if (ret) {
67 krb5_warn(context, ret, "resolving keytab %s", keytab_string);
68 return ret;
71 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
72 if(ret) {
73 krb5_warn(context, ret, "krb5_kt_start_seq_get %s", keytab_string);
74 krb5_kt_close(context, keytab);
75 return ret;
78 printf ("%s:\n\n", keytab_string);
80 table = rtbl_create();
81 rtbl_add_column_by_id(table, 0, "Vno", RTBL_ALIGN_RIGHT);
82 rtbl_add_column_by_id(table, 1, "Type", 0);
83 rtbl_add_column_by_id(table, 2, "Principal", 0);
84 if (opt->timestamp_flag)
85 rtbl_add_column_by_id(table, 3, "Date", 0);
86 if(opt->keys_flag)
87 rtbl_add_column_by_id(table, 4, "Key", 0);
88 rtbl_set_separator(table, " ");
90 while((ret = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0){
91 char buf[1024], *s;
93 snprintf(buf, sizeof(buf), "%d", entry.vno);
94 rtbl_add_column_entry_by_id(table, 0, buf);
96 ret = krb5_enctype_to_string(context,
97 entry.keyblock.keytype, &s);
98 if (ret != 0) {
99 snprintf(buf, sizeof(buf), "unknown (%d)", entry.keyblock.keytype);
100 rtbl_add_column_entry_by_id(table, 1, buf);
101 } else {
102 rtbl_add_column_entry_by_id(table, 1, s);
103 free(s);
106 krb5_unparse_name_fixed(context, entry.principal, buf, sizeof(buf));
107 rtbl_add_column_entry_by_id(table, 2, buf);
109 if (opt->timestamp_flag) {
110 krb5_format_time(context, entry.timestamp, buf,
111 sizeof(buf), FALSE);
112 rtbl_add_column_entry_by_id(table, 3, buf);
114 if(opt->keys_flag) {
115 int i;
116 s = malloc(2 * entry.keyblock.keyvalue.length + 1);
117 for(i = 0; i < entry.keyblock.keyvalue.length; i++)
118 snprintf(s + 2 * i, 3, "%02x",
119 ((unsigned char*)entry.keyblock.keyvalue.data)[i]);
120 rtbl_add_column_entry_by_id(table, 4, s);
121 free(s);
123 krb5_kt_free_entry(context, &entry);
125 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
126 rtbl_format(table, stdout);
127 rtbl_destroy(table);
129 krb5_kt_close(context, keytab);
130 return ret;
134 kt_list(struct list_options *opt, int argc, char **argv)
136 krb5_error_code ret;
137 char kt[1024];
139 if(verbose_flag)
140 opt->timestamp_flag = 1;
142 if (keytab_string == NULL) {
143 if((ret = krb5_kt_default_name(context, kt, sizeof(kt))) != 0) {
144 krb5_warn(context, ret, "getting default keytab name");
145 return 1;
147 keytab_string = kt;
149 return do_list(opt, keytab_string) != 0;