1/* 2 * Copyright (c) 1997-2004 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#include <rtbl.h> 36 37static int 38do_list(struct list_options *opt, const char *keytab_str) 39{ 40 krb5_error_code ret; 41 krb5_keytab keytab; 42 krb5_keytab_entry entry; 43 krb5_kt_cursor cursor; 44 rtbl_t table; 45 46 /* XXX specialcase the ANY type */ 47 if(strncasecmp(keytab_str, "ANY:", 4) == 0) { 48 int flag = 0; 49 char buf[1024]; 50 keytab_str += 4; 51 ret = 0; 52 while (strsep_copy((const char**)&keytab_str, ",", 53 buf, sizeof(buf)) != -1) { 54 if(flag) 55 printf("\n"); 56 if(do_list(opt, buf)) 57 ret = 1; 58 flag = 1; 59 } 60 return ret; 61 } 62 63 ret = krb5_kt_resolve(context, keytab_str, &keytab); 64 if (ret) { 65 krb5_warn(context, ret, "resolving keytab %s", keytab_str); 66 return ret; 67 } 68 69 ret = krb5_kt_start_seq_get(context, keytab, &cursor); 70 if(ret) { 71 krb5_warn(context, ret, "krb5_kt_start_seq_get %s", keytab_str); 72 krb5_kt_close(context, keytab); 73 return ret; 74 } 75 76 printf ("%s:\n\n", keytab_str); 77 78 table = rtbl_create(); 79 rtbl_add_column_by_id(table, 0, "Vno", RTBL_ALIGN_RIGHT); 80 rtbl_add_column_by_id(table, 1, "Type", 0); 81 rtbl_add_column_by_id(table, 2, "Principal", 0); 82 if (opt->timestamp_flag) 83 rtbl_add_column_by_id(table, 3, "Date", 0); 84 if(opt->keys_flag) 85 rtbl_add_column_by_id(table, 4, "Key", 0); 86 rtbl_add_column_by_id(table, 5, "Aliases", 0); 87 rtbl_set_separator(table, " "); 88 89 while(krb5_kt_next_entry(context, keytab, &entry, &cursor) == 0){ 90 char buf[1024], *s; 91 92 snprintf(buf, sizeof(buf), "%d", entry.vno); 93 rtbl_add_column_entry_by_id(table, 0, buf); 94 95 ret = krb5_enctype_to_string(context, 96 entry.keyblock.keytype, &s); 97 if (ret != 0) { 98 snprintf(buf, sizeof(buf), "unknown (%d)", entry.keyblock.keytype); 99 rtbl_add_column_entry_by_id(table, 1, buf); 100 } else { 101 rtbl_add_column_entry_by_id(table, 1, s); 102 free(s); 103 } 104 105 krb5_unparse_name_fixed(context, entry.principal, buf, sizeof(buf)); 106 rtbl_add_column_entry_by_id(table, 2, buf); 107 108 if (opt->timestamp_flag) { 109 krb5_format_time(context, entry.timestamp, buf, 110 sizeof(buf), FALSE); 111 rtbl_add_column_entry_by_id(table, 3, buf); 112 } 113 if(opt->keys_flag) { 114 size_t i; 115 s = malloc(2 * entry.keyblock.keyvalue.length + 1); 116 if (s == NULL) { 117 krb5_warnx(context, "malloc failed"); 118 ret = ENOMEM; 119 goto out; 120 } 121 for(i = 0; i < entry.keyblock.keyvalue.length; i++) 122 snprintf(s + 2 * i, 3, "%02x", 123 ((unsigned char*)entry.keyblock.keyvalue.data)[i]); 124 rtbl_add_column_entry_by_id(table, 4, s); 125 free(s); 126 } 127 if (entry.aliases) { 128 unsigned int i; 129 struct rk_strpool *p = NULL; 130 131 for (i = 0; i< entry.aliases->len; i++) { 132 krb5_unparse_name_fixed(context, entry.principal, buf, sizeof(buf)); 133 rk_strpoolprintf(p, "%s%s", buf, 134 i + 1 < entry.aliases->len ? ", " : ""); 135 136 } 137 rtbl_add_column_entry_by_id(table, 5, rk_strpoolcollect(p)); 138 } 139 140 krb5_kt_free_entry(context, &entry); 141 } 142 ret = krb5_kt_end_seq_get(context, keytab, &cursor); 143 rtbl_format(table, stdout); 144 145out: 146 rtbl_destroy(table); 147 148 krb5_kt_close(context, keytab); 149 return ret; 150} 151 152int 153kt_list(struct list_options *opt, int argc, char **argv) 154{ 155 krb5_error_code ret; 156 char kt[1024]; 157 158 if(verbose_flag) 159 opt->timestamp_flag = 1; 160 161 if (keytab_string == NULL) { 162 if((ret = krb5_kt_default_name(context, kt, sizeof(kt))) != 0) { 163 krb5_warn(context, ret, "getting default keytab name"); 164 return 1; 165 } 166 keytab_string = kt; 167 } 168 return do_list(opt, keytab_string) != 0; 169} 170