2 * Copyright (c) 1999 - 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
36 /* keytab backend for HDB databases */
45 hdb_entry_ex hdb_entry;
51 * the format for HDB keytabs is:
52 * HDB:[HDBFORMAT:database-specific-data[:mkey=mkey-file]]
55 static krb5_error_code KRB5_CALLCONV
56 hdb_resolve(krb5_context context, const char *name, krb5_keytab id)
59 const char *db, *mkey;
61 d = malloc(sizeof(*d));
63 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
67 mkey = strstr(name, ":mkey=");
68 if(mkey == NULL || mkey[6] == '\0') {
72 d->dbname = strdup(name);
73 if(d->dbname == NULL) {
75 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
81 d->dbname = malloc(mkey - db + 1);
82 if(d->dbname == NULL) {
84 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
87 memmove(d->dbname, db, mkey - db);
88 d->dbname[mkey - db] = '\0';
90 d->mkey = strdup(mkey + 6);
94 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
102 static krb5_error_code KRB5_CALLCONV
103 hdb_close(krb5_context context, krb5_keytab id)
105 struct hdb_data *d = id->data;
113 static krb5_error_code KRB5_CALLCONV
114 hdb_get_name(krb5_context context,
119 struct hdb_data *d = id->data;
121 snprintf(name, namesize, "%s%s%s",
122 d->dbname ? d->dbname : "",
123 (d->dbname || d->mkey) ? ":" : "",
124 d->mkey ? d->mkey : "");
129 * try to figure out the database (`dbname') and master-key (`mkey')
130 * that should be used for `principal'.
133 static krb5_error_code
134 find_db (krb5_context context,
137 krb5_const_principal principal)
139 krb5_const_realm realm = krb5_principal_get_realm(context, principal);
141 struct hdb_dbinfo *head, *dbinfo = NULL;
143 *dbname = *mkey = NULL;
145 ret = hdb_get_dbinfo(context, &head);
149 while ((dbinfo = hdb_dbinfo_get_next(head, dbinfo)) != NULL) {
150 const char *p = hdb_dbinfo_get_realm(context, dbinfo);
151 if (p && strcmp (realm, p) == 0) {
152 p = hdb_dbinfo_get_dbname(context, dbinfo);
155 p = hdb_dbinfo_get_mkey_file(context, dbinfo);
161 hdb_free_dbinfo(context, &head);
162 if (*dbname == NULL &&
163 (*dbname = strdup(hdb_default_db(context))) == NULL)
164 return krb5_enomem(context);
169 * find the keytab entry in `id' for `principal, kvno, enctype' and return
170 * it in `entry'. return 0 or an error code
173 static krb5_error_code KRB5_CALLCONV
174 hdb_get_entry(krb5_context context,
176 krb5_const_principal principal,
178 krb5_enctype enctype,
179 krb5_keytab_entry *entry)
183 struct hdb_data *d = id->data;
184 const char *dbname = d->dbname;
185 const char *mkey = d->mkey;
186 char *fdbname = NULL, *fmkey = NULL;
190 memset(&ent, 0, sizeof(ent));
192 if (dbname == NULL) {
193 ret = find_db(context, &fdbname, &fmkey, principal);
200 ret = hdb_create (context, &db, dbname);
203 ret = hdb_set_master_keyfile (context, db, mkey);
205 (*db->hdb_destroy)(context, db);
209 ret = (*db->hdb_open)(context, db, O_RDONLY, 0);
211 (*db->hdb_destroy)(context, db);
215 ret = hdb_fetch_kvno(context, db, principal,
216 HDB_F_DECRYPT|HDB_F_KVNO_SPECIFIED|
217 HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
220 if(ret == HDB_ERR_NOENTRY) {
221 ret = KRB5_KT_NOTFOUND;
226 if(kvno && (krb5_kvno)ent.entry.kvno != kvno) {
227 hdb_free_entry(context, &ent);
228 ret = KRB5_KT_NOTFOUND;
232 if(ent.entry.keys.len > 0)
233 enctype = ent.entry.keys.val[0].key.keytype;
234 ret = KRB5_KT_NOTFOUND;
235 for(i = 0; i < ent.entry.keys.len; i++) {
236 if(ent.entry.keys.val[i].key.keytype == enctype) {
237 krb5_copy_principal(context, principal, &entry->principal);
238 entry->vno = ent.entry.kvno;
239 krb5_copy_keyblock_contents(context,
240 &ent.entry.keys.val[i].key,
246 hdb_free_entry(context, &ent);
248 (*db->hdb_close)(context, db);
249 (*db->hdb_destroy)(context, db);
257 * find the keytab entry in `id' for `principal, kvno, enctype' and return
258 * it in `entry'. return 0 or an error code
261 static krb5_error_code KRB5_CALLCONV
262 hdb_start_seq_get(krb5_context context,
264 krb5_kt_cursor *cursor)
267 struct hdb_cursor *c;
268 struct hdb_data *d = id->data;
269 const char *dbname = d->dbname;
270 const char *mkey = d->mkey;
273 if (dbname == NULL) {
275 * We don't support enumerating without being told what
276 * backend to enumerate on
278 ret = KRB5_KT_NOTFOUND;
282 ret = hdb_create (context, &db, dbname);
285 ret = hdb_set_master_keyfile (context, db, mkey);
287 (*db->hdb_destroy)(context, db);
291 ret = (*db->hdb_open)(context, db, O_RDONLY, 0);
293 (*db->hdb_destroy)(context, db);
297 cursor->data = c = malloc (sizeof(*c));
299 (*db->hdb_close)(context, db);
300 (*db->hdb_destroy)(context, db);
301 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
314 static int KRB5_CALLCONV
315 hdb_next_entry(krb5_context context,
317 krb5_keytab_entry *entry,
318 krb5_kt_cursor *cursor)
320 struct hdb_cursor *c = cursor->data;
323 memset(entry, 0, sizeof(*entry));
327 ret = (c->db->hdb_firstkey)(context, c->db,
329 HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
331 if (ret == HDB_ERR_NOENTRY)
336 if (c->hdb_entry.entry.keys.len == 0)
337 hdb_free_entry(context, &c->hdb_entry);
343 ret = (c->db->hdb_nextkey)(context, c->db,
345 HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
347 if (ret == HDB_ERR_NOENTRY)
352 /* If no keys on this entry, try again */
353 if (c->hdb_entry.entry.keys.len == 0)
354 hdb_free_entry(context, &c->hdb_entry);
360 * Return next enc type (keytabs are one slot per key, while
361 * hdb is one record per principal.
364 ret = krb5_copy_principal(context,
365 c->hdb_entry.entry.principal,
370 entry->vno = c->hdb_entry.entry.kvno;
371 ret = krb5_copy_keyblock_contents(context,
372 &c->hdb_entry.entry.keys.val[c->key_idx].key,
375 krb5_free_principal(context, entry->principal);
376 memset(entry, 0, sizeof(*entry));
382 * Once we get to the end of the list, signal that we want the
386 if ((size_t)c->key_idx == c->hdb_entry.entry.keys.len) {
387 hdb_free_entry(context, &c->hdb_entry);
396 static int KRB5_CALLCONV
397 hdb_end_seq_get(krb5_context context,
399 krb5_kt_cursor *cursor)
401 struct hdb_cursor *c = cursor->data;
404 hdb_free_entry(context, &c->hdb_entry);
406 (c->db->hdb_close)(context, c->db);
407 (c->db->hdb_destroy)(context, c->db);
413 krb5_kt_ops hdb_kt_ops = {
429 krb5_kt_ops hdb_get_kt_ops = {