1 /* 2 * Copyright (c) 1999 - 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 "hdb_locl.h" 35 36 /* keytab backend for HDB databases */ 37 38 RCSID("$Id: keytab.c,v 1.3 2000/08/27 04:31:42 assar Exp $"); 39 40 struct hdb_data { 41 char *dbname; 42 char *mkey; 43 }; 44 45 /* 46 * the format for HDB keytabs is: 47 * HDB:[database:mkey] 48 */ 49 50 static krb5_error_code 51 hdb_resolve(krb5_context context, const char *name, krb5_keytab id) 52 { 53 struct hdb_data *d; 54 const char *db, *mkey; 55 56 d = malloc(sizeof(*d)); 57 if(d == NULL) 58 return ENOMEM; 59 db = name; 60 mkey = strchr(name, ':'); 61 if(mkey == NULL || mkey[1] == '\0') { 62 if(*name == '\0') 63 d->dbname = NULL; 64 else { 65 d->dbname = strdup(name); 66 if(d->dbname == NULL) { 67 free(d); 68 return ENOMEM; 69 } 70 } 71 d->mkey = NULL; 72 } else { 73 if((mkey - db) == 0) { 74 d->dbname = NULL; 75 } else { 76 d->dbname = malloc(mkey - db); 77 if(d->dbname == NULL) { 78 free(d); 79 return ENOMEM; 80 } 81 memmove(d->dbname, db, mkey - db); 82 d->dbname[mkey - db] = '\0'; 83 } 84 d->mkey = strdup(mkey + 1); 85 if(d->mkey == NULL) { 86 free(d->dbname); 87 free(d); 88 return ENOMEM; 89 } 90 } 91 id->data = d; 92 return 0; 93 } 94 95 static krb5_error_code 96 hdb_close(krb5_context context, krb5_keytab id) 97 { 98 struct hdb_data *d = id->data; 99 100 free(d->dbname); 101 free(d->mkey); 102 free(d); 103 return 0; 104 } 105 106 static krb5_error_code 107 hdb_get_name(krb5_context context, 108 krb5_keytab id, 109 char *name, 110 size_t namesize) 111 { 112 struct hdb_data *d = id->data; 113 114 snprintf(name, namesize, "%s%s%s", 115 d->dbname ? d->dbname : "", 116 (d->dbname || d->mkey) ? ":" : "", 117 d->mkey ? d->mkey : ""); 118 return 0; 119 } 120 121 static void 122 set_config (krb5_context context, 123 krb5_config_binding *binding, 124 const char **dbname, 125 const char **mkey) 126 { 127 *dbname = krb5_config_get_string(context, binding, "dbname", NULL); 128 *mkey = krb5_config_get_string(context, binding, "mkey_file", NULL); 129 } 130 131 /* 132 * try to figure out the database (`dbname') and master-key (`mkey') 133 * that should be used for `principal'. 134 */ 135 136 static void 137 find_db (krb5_context context, 138 const char **dbname, 139 const char **mkey, 140 krb5_const_principal principal) 141 { 142 krb5_config_binding *top_bind = NULL; 143 krb5_config_binding *default_binding = NULL; 144 krb5_config_binding *db; 145 krb5_realm *prealm = krb5_princ_realm(context, (krb5_principal)principal); 146 147 *dbname = *mkey = NULL; 148 149 while ((db = (krb5_config_binding *) 150 krb5_config_get_next(context, 151 NULL, 152 &top_bind, 153 krb5_config_list, 154 "kdc", 155 "database", 156 NULL)) != NULL) { 157 const char *p; 158 159 p = krb5_config_get_string (context, db, "realm", NULL); 160 if (p == NULL) { 161 if(default_binding) { 162 krb5_warnx(context, "WARNING: more than one realm-less " 163 "database specification"); 164 krb5_warnx(context, "WARNING: using the first encountered"); 165 } else 166 default_binding = db; 167 } else if (strcmp (*prealm, p) == 0) { 168 set_config (context, db, dbname, mkey); 169 break; 170 } 171 } 172 if (*dbname == NULL && default_binding != NULL) 173 set_config (context, default_binding, dbname, mkey); 174 if (*dbname == NULL) 175 *dbname = HDB_DEFAULT_DB; 176 } 177 178 /* 179 * find the keytab entry in `id' for `principal, kvno, enctype' and return 180 * it in `entry'. return 0 or an error code 181 */ 182 183 static krb5_error_code 184 hdb_get_entry(krb5_context context, 185 krb5_keytab id, 186 krb5_const_principal principal, 187 krb5_kvno kvno, 188 krb5_enctype enctype, 189 krb5_keytab_entry *entry) 190 { 191 hdb_entry ent; 192 krb5_error_code ret; 193 struct hdb_data *d = id->data; 194 int i; 195 HDB *db; 196 const char *dbname = d->dbname; 197 const char *mkey = d->mkey; 198 199 if (dbname == NULL) 200 find_db (context, &dbname, &mkey, principal); 201 202 ret = hdb_create (context, &db, dbname); 203 if (ret) 204 return ret; 205 ret = hdb_set_master_keyfile (context, db, mkey); 206 if (ret) { 207 (*db->destroy)(context, db); 208 return ret; 209 } 210 211 ret = (*db->open)(context, db, O_RDONLY, 0); 212 if (ret) { 213 (*db->destroy)(context, db); 214 return ret; 215 } 216 ent.principal = (krb5_principal)principal; 217 ret = (*db->fetch)(context, db, HDB_F_DECRYPT, &ent); 218 (*db->close)(context, db); 219 (*db->destroy)(context, db); 220 221 if(ret == HDB_ERR_NOENTRY) 222 return KRB5_KT_NOTFOUND; 223 else if(ret) 224 return ret; 225 if(kvno && ent.kvno != kvno) { 226 hdb_free_entry(context, &ent); 227 return KRB5_KT_NOTFOUND; 228 } 229 if(enctype == 0) 230 if(ent.keys.len > 0) 231 enctype = ent.keys.val[0].key.keytype; 232 ret = KRB5_KT_NOTFOUND; 233 for(i = 0; i < ent.keys.len; i++) { 234 if(ent.keys.val[i].key.keytype == enctype) { 235 krb5_copy_principal(context, principal, &entry->principal); 236 entry->vno = ent.kvno; 237 krb5_copy_keyblock_contents(context, 238 &ent.keys.val[i].key, 239 &entry->keyblock); 240 ret = 0; 241 break; 242 } 243 } 244 hdb_free_entry(context, &ent); 245 return ret; 246 } 247 248 krb5_kt_ops hdb_kt_ops = { 249 "HDB", 250 hdb_resolve, 251 hdb_get_name, 252 hdb_close, 253 hdb_get_entry, 254 NULL, /* start_seq_get */ 255 NULL, /* next_entry */ 256 NULL, /* end_seq_get */ 257 NULL, /* add */ 258 NULL /* remove */ 259 }; 260