xref: /freebsd/crypto/heimdal/admin/list.c (revision 5e9cd1ae3e10592ed70e7575551cba1bbab04d84)
1 /*
2  * Copyright (c) 1997 - 2000 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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.
32  */
33 
34 #include "ktutil_locl.h"
35 
36 RCSID("$Id: list.c,v 1.3 2000/06/29 08:21:40 joda Exp $");
37 
38 static int help_flag;
39 static int list_keys;
40 static int list_timestamp;
41 
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" },
46 };
47 
48 static int num_args = sizeof(args) / sizeof(args[0]);
49 
50 struct key_info {
51     char *version;
52     char *etype;
53     char *principal;
54     char *timestamp;
55     char *key;
56     struct key_info *next;
57 };
58 
59 int
60 kt_list(int argc, char **argv)
61 {
62     krb5_error_code ret;
63     krb5_kt_cursor cursor;
64     krb5_keytab_entry entry;
65     int optind = 0;
66     struct key_info *ki, **kie = &ki, *kp;
67 
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;
73 
74     if(verbose_flag)
75 	list_timestamp = 1;
76 
77     if(getarg(args, num_args, argc, argv, &optind)){
78 	arg_printusage(args, num_args, "ktutil list", "");
79 	return 1;
80     }
81     if(help_flag){
82 	arg_printusage(args, num_args, "ktutil list", "");
83 	return 0;
84     }
85 
86     ret = krb5_kt_start_seq_get(context, keytab, &cursor);
87     if(ret){
88 	krb5_warn(context, ret, "krb5_kt_start_seq_get %s", keytab_string);
89 	return 1;
90     }
91     while((ret = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0){
92 #define CHECK_MAX(F) if(max_##F < strlen(kp->F)) max_##F = strlen(kp->F)
93 
94 	kp = malloc(sizeof(*kp));
95 
96 	asprintf(&kp->version, "%d", entry.vno);
97 	CHECK_MAX(version);
98 	ret = krb5_enctype_to_string(context,
99 				     entry.keyblock.keytype, &kp->etype);
100 	if (ret != 0)
101 	    asprintf(&kp->etype, "unknown (%d)", entry.keyblock.keytype);
102 	CHECK_MAX(etype);
103 	krb5_unparse_name_short(context, entry.principal, &kp->principal);
104 	CHECK_MAX(principal);
105 	if (list_timestamp) {
106 	    char tstamp[256];
107 
108 	    krb5_format_time(context, entry.timestamp,
109 			     tstamp, sizeof(tstamp), FALSE);
110 
111 	    kp->timestamp = strdup(tstamp);
112 	    CHECK_MAX(timestamp);
113 	}
114 	if(list_keys) {
115 	    int i;
116 	    kp->key = malloc(2 * entry.keyblock.keyvalue.length + 1);
117 	    for(i = 0; i < entry.keyblock.keyvalue.length; i++)
118 		snprintf(kp->key + 2 * i, 3, "%02x",
119 			 ((unsigned char*)entry.keyblock.keyvalue.data)[i]);
120 	    CHECK_MAX(key);
121 	}
122 	kp->next = NULL;
123 	*kie = kp;
124 	kie = &kp->next;
125 	krb5_kt_free_entry(context, &entry);
126     }
127     ret = krb5_kt_end_seq_get(context, keytab, &cursor);
128 
129     printf("%-*s  %-*s  %-*s", max_version, "Vno",
130 	   max_etype, "Type",
131 	   max_principal, "Principal");
132     if(list_timestamp)
133 	printf("  %-*s", max_timestamp, "Date");
134     if(list_keys)
135 	printf("  %s", "Key");
136     printf("\n");
137 
138     for(kp = ki; kp; ) {
139 	printf("%*s  %-*s  %-*s", max_version, kp->version,
140 	       max_etype, kp->etype,
141 	       max_principal, kp->principal);
142 	if(list_timestamp)
143 	    printf("  %-*s", max_timestamp, kp->timestamp);
144 	if(list_keys)
145 	    printf("  %s", kp->key);
146 	printf("\n");
147 
148 	/* free entries */
149 	free(kp->version);
150 	free(kp->etype);
151 	free(kp->principal);
152 	if(list_timestamp)
153 	    free(kp->timestamp);
154 	if(list_keys) {
155 	    memset(kp->key, 0, strlen(kp->key));
156 	    free(kp->key);
157 	}
158 	ki = kp;
159 	kp = kp->next;
160 	free(ki);
161     }
162     return 0;
163 }
164