1 /* $NetBSD: list.c,v 1.1.1.2 2014/04/24 12:45:26 pettai Exp $ */ 2 3 /* 4 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan 5 * (Royal Institute of Technology, Stockholm, Sweden). 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * 3. Neither the name of the Institute nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include "ktutil_locl.h" 37 #include <krb5/rtbl.h> 38 39 __RCSID("NetBSD"); 40 41 static int 42 do_list(struct list_options *opt, const char *keytab_str) 43 { 44 krb5_error_code ret; 45 krb5_keytab keytab; 46 krb5_keytab_entry entry; 47 krb5_kt_cursor cursor; 48 rtbl_t table; 49 50 /* XXX specialcase the ANY type */ 51 if(strncasecmp(keytab_str, "ANY:", 4) == 0) { 52 int flag = 0; 53 char buf[1024]; 54 keytab_str += 4; 55 ret = 0; 56 while (strsep_copy((const char**)&keytab_str, ",", 57 buf, sizeof(buf)) != -1) { 58 if(flag) 59 printf("\n"); 60 if(do_list(opt, buf)) 61 ret = 1; 62 flag = 1; 63 } 64 return ret; 65 } 66 67 ret = krb5_kt_resolve(context, keytab_str, &keytab); 68 if (ret) { 69 krb5_warn(context, ret, "resolving keytab %s", keytab_str); 70 return ret; 71 } 72 73 ret = krb5_kt_start_seq_get(context, keytab, &cursor); 74 if(ret) { 75 krb5_warn(context, ret, "krb5_kt_start_seq_get %s", keytab_str); 76 krb5_kt_close(context, keytab); 77 return ret; 78 } 79 80 printf ("%s:\n\n", keytab_str); 81 82 table = rtbl_create(); 83 rtbl_add_column_by_id(table, 0, "Vno", RTBL_ALIGN_RIGHT); 84 rtbl_add_column_by_id(table, 1, "Type", 0); 85 rtbl_add_column_by_id(table, 2, "Principal", 0); 86 if (opt->timestamp_flag) 87 rtbl_add_column_by_id(table, 3, "Date", 0); 88 if(opt->keys_flag) 89 rtbl_add_column_by_id(table, 4, "Key", 0); 90 rtbl_add_column_by_id(table, 5, "Aliases", 0); 91 rtbl_set_separator(table, " "); 92 93 while(krb5_kt_next_entry(context, keytab, &entry, &cursor) == 0){ 94 char buf[1024], *s; 95 96 snprintf(buf, sizeof(buf), "%d", entry.vno); 97 rtbl_add_column_entry_by_id(table, 0, buf); 98 99 ret = krb5_enctype_to_string(context, 100 entry.keyblock.keytype, &s); 101 if (ret != 0) { 102 snprintf(buf, sizeof(buf), "unknown (%d)", entry.keyblock.keytype); 103 rtbl_add_column_entry_by_id(table, 1, buf); 104 } else { 105 rtbl_add_column_entry_by_id(table, 1, s); 106 free(s); 107 } 108 109 krb5_unparse_name_fixed(context, entry.principal, buf, sizeof(buf)); 110 rtbl_add_column_entry_by_id(table, 2, buf); 111 112 if (opt->timestamp_flag) { 113 krb5_format_time(context, entry.timestamp, buf, 114 sizeof(buf), FALSE); 115 rtbl_add_column_entry_by_id(table, 3, buf); 116 } 117 if(opt->keys_flag) { 118 size_t i; 119 s = malloc(2 * entry.keyblock.keyvalue.length + 1); 120 if (s == NULL) { 121 krb5_warnx(context, "malloc failed"); 122 ret = ENOMEM; 123 goto out; 124 } 125 for(i = 0; i < entry.keyblock.keyvalue.length; i++) 126 snprintf(s + 2 * i, 3, "%02x", 127 ((unsigned char*)entry.keyblock.keyvalue.data)[i]); 128 rtbl_add_column_entry_by_id(table, 4, s); 129 free(s); 130 } 131 if (entry.aliases) { 132 unsigned int i; 133 struct rk_strpool *p = NULL; 134 135 for (i = 0; i< entry.aliases->len; i++) { 136 krb5_unparse_name_fixed(context, entry.principal, buf, sizeof(buf)); 137 rk_strpoolprintf(p, "%s%s", buf, 138 i + 1 < entry.aliases->len ? ", " : ""); 139 140 } 141 rtbl_add_column_entry_by_id(table, 5, rk_strpoolcollect(p)); 142 } 143 144 krb5_kt_free_entry(context, &entry); 145 } 146 ret = krb5_kt_end_seq_get(context, keytab, &cursor); 147 rtbl_format(table, stdout); 148 149 out: 150 rtbl_destroy(table); 151 152 krb5_kt_close(context, keytab); 153 return ret; 154 } 155 156 int 157 kt_list(struct list_options *opt, int argc, char **argv) 158 { 159 krb5_error_code ret; 160 char kt[1024]; 161 162 if(verbose_flag) 163 opt->timestamp_flag = 1; 164 165 if (keytab_string == NULL) { 166 if((ret = krb5_kt_default_name(context, kt, sizeof(kt))) != 0) { 167 krb5_warn(context, ret, "getting default keytab name"); 168 return 1; 169 } 170 keytab_string = kt; 171 } 172 return do_list(opt, keytab_string) != 0; 173 } 174