2 * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "ktutil_locl.h"
36 RCSID("$Id: list.c,v 1.10 2002/01/30 10:12:21 joda Exp $");
40 static int list_timestamp;
42 static struct getargs args[] = {
43 { "help", 'h', arg_flag, &help_flag },
44 { "keys", 0, arg_flag, &list_keys, "show key value" },
45 { "timestamp", 0, arg_flag, &list_timestamp, "show timestamp" },
48 static int num_args = sizeof(args) / sizeof(args[0]);
56 struct key_info *next;
60 do_list(const char *keytab_string)
64 krb5_keytab_entry entry;
65 krb5_kt_cursor cursor;
66 struct key_info *ki, **kie = &ki, *kp;
68 int max_version = sizeof("Vno") - 1;
69 int max_etype = sizeof("Type") - 1;
70 int max_principal = sizeof("Principal") - 1;
71 int max_timestamp = sizeof("Date") - 1;
72 int max_key = sizeof("Key") - 1;
74 /* XXX specialcase the ANY type */
75 if(strncasecmp(keytab_string, "ANY:", 4) == 0) {
79 while (strsep_copy((const char**)&keytab_string, ",",
80 buf, sizeof(buf)) != -1) {
89 ret = krb5_kt_resolve(context, keytab_string, &keytab);
91 krb5_warn(context, ret, "resolving keytab %s", keytab_string);
95 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
97 krb5_warn(context, ret, "krb5_kt_start_seq_get %s", keytab_string);
101 printf ("%s:\n\n", keytab_string);
103 while((ret = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0){
104 #define CHECK_MAX(F) if(max_##F < strlen(kp->F)) max_##F = strlen(kp->F)
106 kp = malloc(sizeof(*kp));
108 krb5_kt_free_entry(context, &entry);
109 krb5_kt_end_seq_get(context, keytab, &cursor);
110 krb5_warn(context, ret, "malloc failed");
114 asprintf(&kp->version, "%d", entry.vno);
116 ret = krb5_enctype_to_string(context,
117 entry.keyblock.keytype, &kp->etype);
119 asprintf(&kp->etype, "unknown (%d)", entry.keyblock.keytype);
121 krb5_unparse_name(context, entry.principal, &kp->principal);
122 CHECK_MAX(principal);
123 if (list_timestamp) {
126 krb5_format_time(context, entry.timestamp,
127 tstamp, sizeof(tstamp), FALSE);
129 kp->timestamp = strdup(tstamp);
130 CHECK_MAX(timestamp);
134 kp->key = malloc(2 * entry.keyblock.keyvalue.length + 1);
135 for(i = 0; i < entry.keyblock.keyvalue.length; i++)
136 snprintf(kp->key + 2 * i, 3, "%02x",
137 ((unsigned char*)entry.keyblock.keyvalue.data)[i]);
142 krb5_kt_free_entry(context, &entry);
144 *kie = NULL; /* termiate list */
145 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
147 printf("%-*s %-*s %-*s", max_version, "Vno",
149 max_principal, "Principal");
151 printf(" %-*s", max_timestamp, "Date");
153 printf(" %s", "Key");
157 printf("%*s %-*s %-*s", max_version, kp->version,
158 max_etype, kp->etype,
159 max_principal, kp->principal);
161 printf(" %-*s", max_timestamp, kp->timestamp);
163 printf(" %s", kp->key);
173 memset(kp->key, 0, strlen(kp->key));
181 krb5_kt_close(context, keytab);
186 kt_list(int argc, char **argv)
195 if(getarg(args, num_args, argc, argv, &optind)){
196 arg_printusage(args, num_args, "ktutil list", "");
200 arg_printusage(args, num_args, "ktutil list", "");
204 if (keytab_string == NULL) {
205 if((ret = krb5_kt_default_name(context, kt, sizeof(kt))) != 0) {
206 krb5_warn(context, ret, "getting default keytab name");
211 do_list(keytab_string);