1*0a6a1f1dSLionel Sambuc /* $NetBSD: keytab.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1999 - 2002 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include "hdb_locl.h"
37ebfedea0SLionel Sambuc
38ebfedea0SLionel Sambuc /* keytab backend for HDB databases */
39ebfedea0SLionel Sambuc
40ebfedea0SLionel Sambuc struct hdb_data {
41ebfedea0SLionel Sambuc char *dbname;
42ebfedea0SLionel Sambuc char *mkey;
43ebfedea0SLionel Sambuc };
44ebfedea0SLionel Sambuc
45ebfedea0SLionel Sambuc struct hdb_cursor {
46ebfedea0SLionel Sambuc HDB *db;
47ebfedea0SLionel Sambuc hdb_entry_ex hdb_entry;
48ebfedea0SLionel Sambuc int first, next;
49ebfedea0SLionel Sambuc int key_idx;
50ebfedea0SLionel Sambuc };
51ebfedea0SLionel Sambuc
52ebfedea0SLionel Sambuc /*
53ebfedea0SLionel Sambuc * the format for HDB keytabs is:
54ebfedea0SLionel Sambuc * HDB:[HDBFORMAT:database-specific-data[:mkey=mkey-file]]
55ebfedea0SLionel Sambuc */
56ebfedea0SLionel Sambuc
57ebfedea0SLionel Sambuc static krb5_error_code KRB5_CALLCONV
hdb_resolve(krb5_context context,const char * name,krb5_keytab id)58ebfedea0SLionel Sambuc hdb_resolve(krb5_context context, const char *name, krb5_keytab id)
59ebfedea0SLionel Sambuc {
60ebfedea0SLionel Sambuc struct hdb_data *d;
61ebfedea0SLionel Sambuc const char *db, *mkey;
62ebfedea0SLionel Sambuc
63ebfedea0SLionel Sambuc d = malloc(sizeof(*d));
64ebfedea0SLionel Sambuc if(d == NULL) {
65ebfedea0SLionel Sambuc krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
66ebfedea0SLionel Sambuc return ENOMEM;
67ebfedea0SLionel Sambuc }
68ebfedea0SLionel Sambuc db = name;
69ebfedea0SLionel Sambuc mkey = strstr(name, ":mkey=");
70ebfedea0SLionel Sambuc if(mkey == NULL || mkey[5] == '\0') {
71ebfedea0SLionel Sambuc if(*name == '\0')
72ebfedea0SLionel Sambuc d->dbname = NULL;
73ebfedea0SLionel Sambuc else {
74ebfedea0SLionel Sambuc d->dbname = strdup(name);
75ebfedea0SLionel Sambuc if(d->dbname == NULL) {
76ebfedea0SLionel Sambuc free(d);
77ebfedea0SLionel Sambuc krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
78ebfedea0SLionel Sambuc return ENOMEM;
79ebfedea0SLionel Sambuc }
80ebfedea0SLionel Sambuc }
81ebfedea0SLionel Sambuc d->mkey = NULL;
82ebfedea0SLionel Sambuc } else {
83ebfedea0SLionel Sambuc d->dbname = malloc(mkey - db + 1);
84ebfedea0SLionel Sambuc if(d->dbname == NULL) {
85ebfedea0SLionel Sambuc free(d);
86ebfedea0SLionel Sambuc krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
87ebfedea0SLionel Sambuc return ENOMEM;
88ebfedea0SLionel Sambuc }
89ebfedea0SLionel Sambuc memmove(d->dbname, db, mkey - db);
90ebfedea0SLionel Sambuc d->dbname[mkey - db] = '\0';
91ebfedea0SLionel Sambuc
92ebfedea0SLionel Sambuc d->mkey = strdup(mkey + 5);
93ebfedea0SLionel Sambuc if(d->mkey == NULL) {
94ebfedea0SLionel Sambuc free(d->dbname);
95ebfedea0SLionel Sambuc free(d);
96ebfedea0SLionel Sambuc krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
97ebfedea0SLionel Sambuc return ENOMEM;
98ebfedea0SLionel Sambuc }
99ebfedea0SLionel Sambuc }
100ebfedea0SLionel Sambuc id->data = d;
101ebfedea0SLionel Sambuc return 0;
102ebfedea0SLionel Sambuc }
103ebfedea0SLionel Sambuc
104ebfedea0SLionel Sambuc static krb5_error_code KRB5_CALLCONV
hdb_close(krb5_context context,krb5_keytab id)105ebfedea0SLionel Sambuc hdb_close(krb5_context context, krb5_keytab id)
106ebfedea0SLionel Sambuc {
107ebfedea0SLionel Sambuc struct hdb_data *d = id->data;
108ebfedea0SLionel Sambuc
109ebfedea0SLionel Sambuc free(d->dbname);
110ebfedea0SLionel Sambuc free(d->mkey);
111ebfedea0SLionel Sambuc free(d);
112ebfedea0SLionel Sambuc return 0;
113ebfedea0SLionel Sambuc }
114ebfedea0SLionel Sambuc
115ebfedea0SLionel Sambuc static krb5_error_code KRB5_CALLCONV
hdb_get_name(krb5_context context,krb5_keytab id,char * name,size_t namesize)116ebfedea0SLionel Sambuc hdb_get_name(krb5_context context,
117ebfedea0SLionel Sambuc krb5_keytab id,
118ebfedea0SLionel Sambuc char *name,
119ebfedea0SLionel Sambuc size_t namesize)
120ebfedea0SLionel Sambuc {
121ebfedea0SLionel Sambuc struct hdb_data *d = id->data;
122ebfedea0SLionel Sambuc
123ebfedea0SLionel Sambuc snprintf(name, namesize, "%s%s%s",
124ebfedea0SLionel Sambuc d->dbname ? d->dbname : "",
125ebfedea0SLionel Sambuc (d->dbname || d->mkey) ? ":" : "",
126ebfedea0SLionel Sambuc d->mkey ? d->mkey : "");
127ebfedea0SLionel Sambuc return 0;
128ebfedea0SLionel Sambuc }
129ebfedea0SLionel Sambuc
130ebfedea0SLionel Sambuc /*
131ebfedea0SLionel Sambuc * try to figure out the database (`dbname') and master-key (`mkey')
132ebfedea0SLionel Sambuc * that should be used for `principal'.
133ebfedea0SLionel Sambuc */
134ebfedea0SLionel Sambuc
135ebfedea0SLionel Sambuc static krb5_error_code
find_db(krb5_context context,char ** dbname,char ** mkey,krb5_const_principal principal)136ebfedea0SLionel Sambuc find_db (krb5_context context,
137ebfedea0SLionel Sambuc char **dbname,
138ebfedea0SLionel Sambuc char **mkey,
139ebfedea0SLionel Sambuc krb5_const_principal principal)
140ebfedea0SLionel Sambuc {
141ebfedea0SLionel Sambuc krb5_const_realm realm = krb5_principal_get_realm(context, principal);
142ebfedea0SLionel Sambuc krb5_error_code ret;
143ebfedea0SLionel Sambuc struct hdb_dbinfo *head, *dbinfo = NULL;
144ebfedea0SLionel Sambuc
145ebfedea0SLionel Sambuc *dbname = *mkey = NULL;
146ebfedea0SLionel Sambuc
147ebfedea0SLionel Sambuc ret = hdb_get_dbinfo(context, &head);
148ebfedea0SLionel Sambuc if (ret)
149ebfedea0SLionel Sambuc return ret;
150ebfedea0SLionel Sambuc
151ebfedea0SLionel Sambuc while ((dbinfo = hdb_dbinfo_get_next(head, dbinfo)) != NULL) {
152ebfedea0SLionel Sambuc const char *p = hdb_dbinfo_get_realm(context, dbinfo);
153ebfedea0SLionel Sambuc if (p && strcmp (realm, p) == 0) {
154ebfedea0SLionel Sambuc p = hdb_dbinfo_get_dbname(context, dbinfo);
155ebfedea0SLionel Sambuc if (p)
156ebfedea0SLionel Sambuc *dbname = strdup(p);
157ebfedea0SLionel Sambuc p = hdb_dbinfo_get_mkey_file(context, dbinfo);
158ebfedea0SLionel Sambuc if (p)
159ebfedea0SLionel Sambuc *mkey = strdup(p);
160ebfedea0SLionel Sambuc break;
161ebfedea0SLionel Sambuc }
162ebfedea0SLionel Sambuc }
163ebfedea0SLionel Sambuc hdb_free_dbinfo(context, &head);
164ebfedea0SLionel Sambuc if (*dbname == NULL)
165ebfedea0SLionel Sambuc *dbname = strdup(HDB_DEFAULT_DB);
166ebfedea0SLionel Sambuc return 0;
167ebfedea0SLionel Sambuc }
168ebfedea0SLionel Sambuc
169ebfedea0SLionel Sambuc /*
170ebfedea0SLionel Sambuc * find the keytab entry in `id' for `principal, kvno, enctype' and return
171ebfedea0SLionel Sambuc * it in `entry'. return 0 or an error code
172ebfedea0SLionel Sambuc */
173ebfedea0SLionel Sambuc
174ebfedea0SLionel Sambuc static krb5_error_code KRB5_CALLCONV
hdb_get_entry(krb5_context context,krb5_keytab id,krb5_const_principal principal,krb5_kvno kvno,krb5_enctype enctype,krb5_keytab_entry * entry)175ebfedea0SLionel Sambuc hdb_get_entry(krb5_context context,
176ebfedea0SLionel Sambuc krb5_keytab id,
177ebfedea0SLionel Sambuc krb5_const_principal principal,
178ebfedea0SLionel Sambuc krb5_kvno kvno,
179ebfedea0SLionel Sambuc krb5_enctype enctype,
180ebfedea0SLionel Sambuc krb5_keytab_entry *entry)
181ebfedea0SLionel Sambuc {
182ebfedea0SLionel Sambuc hdb_entry_ex ent;
183ebfedea0SLionel Sambuc krb5_error_code ret;
184ebfedea0SLionel Sambuc struct hdb_data *d = id->data;
185ebfedea0SLionel Sambuc const char *dbname = d->dbname;
186ebfedea0SLionel Sambuc const char *mkey = d->mkey;
187ebfedea0SLionel Sambuc char *fdbname = NULL, *fmkey = NULL;
188ebfedea0SLionel Sambuc HDB *db;
189*0a6a1f1dSLionel Sambuc size_t i;
190ebfedea0SLionel Sambuc
191ebfedea0SLionel Sambuc memset(&ent, 0, sizeof(ent));
192ebfedea0SLionel Sambuc
193ebfedea0SLionel Sambuc if (dbname == NULL) {
194ebfedea0SLionel Sambuc ret = find_db(context, &fdbname, &fmkey, principal);
195ebfedea0SLionel Sambuc if (ret)
196ebfedea0SLionel Sambuc return ret;
197ebfedea0SLionel Sambuc dbname = fdbname;
198ebfedea0SLionel Sambuc mkey = fmkey;
199ebfedea0SLionel Sambuc }
200ebfedea0SLionel Sambuc
201ebfedea0SLionel Sambuc ret = hdb_create (context, &db, dbname);
202ebfedea0SLionel Sambuc if (ret)
203ebfedea0SLionel Sambuc goto out2;
204ebfedea0SLionel Sambuc ret = hdb_set_master_keyfile (context, db, mkey);
205ebfedea0SLionel Sambuc if (ret) {
206ebfedea0SLionel Sambuc (*db->hdb_destroy)(context, db);
207ebfedea0SLionel Sambuc goto out2;
208ebfedea0SLionel Sambuc }
209ebfedea0SLionel Sambuc
210ebfedea0SLionel Sambuc ret = (*db->hdb_open)(context, db, O_RDONLY, 0);
211ebfedea0SLionel Sambuc if (ret) {
212ebfedea0SLionel Sambuc (*db->hdb_destroy)(context, db);
213ebfedea0SLionel Sambuc goto out2;
214ebfedea0SLionel Sambuc }
215ebfedea0SLionel Sambuc
216ebfedea0SLionel Sambuc ret = (*db->hdb_fetch_kvno)(context, db, principal,
217ebfedea0SLionel Sambuc HDB_F_DECRYPT|HDB_F_KVNO_SPECIFIED|
218ebfedea0SLionel Sambuc HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
219ebfedea0SLionel Sambuc kvno, &ent);
220ebfedea0SLionel Sambuc
221ebfedea0SLionel Sambuc if(ret == HDB_ERR_NOENTRY) {
222ebfedea0SLionel Sambuc ret = KRB5_KT_NOTFOUND;
223ebfedea0SLionel Sambuc goto out;
224ebfedea0SLionel Sambuc }else if(ret)
225ebfedea0SLionel Sambuc goto out;
226ebfedea0SLionel Sambuc
227*0a6a1f1dSLionel Sambuc if(kvno && (krb5_kvno)ent.entry.kvno != kvno) {
228ebfedea0SLionel Sambuc hdb_free_entry(context, &ent);
229ebfedea0SLionel Sambuc ret = KRB5_KT_NOTFOUND;
230ebfedea0SLionel Sambuc goto out;
231ebfedea0SLionel Sambuc }
232ebfedea0SLionel Sambuc if(enctype == 0)
233ebfedea0SLionel Sambuc if(ent.entry.keys.len > 0)
234ebfedea0SLionel Sambuc enctype = ent.entry.keys.val[0].key.keytype;
235ebfedea0SLionel Sambuc ret = KRB5_KT_NOTFOUND;
236ebfedea0SLionel Sambuc for(i = 0; i < ent.entry.keys.len; i++) {
237ebfedea0SLionel Sambuc if(ent.entry.keys.val[i].key.keytype == enctype) {
238ebfedea0SLionel Sambuc krb5_copy_principal(context, principal, &entry->principal);
239ebfedea0SLionel Sambuc entry->vno = ent.entry.kvno;
240ebfedea0SLionel Sambuc krb5_copy_keyblock_contents(context,
241ebfedea0SLionel Sambuc &ent.entry.keys.val[i].key,
242ebfedea0SLionel Sambuc &entry->keyblock);
243ebfedea0SLionel Sambuc ret = 0;
244ebfedea0SLionel Sambuc break;
245ebfedea0SLionel Sambuc }
246ebfedea0SLionel Sambuc }
247ebfedea0SLionel Sambuc hdb_free_entry(context, &ent);
248ebfedea0SLionel Sambuc out:
249ebfedea0SLionel Sambuc (*db->hdb_close)(context, db);
250ebfedea0SLionel Sambuc (*db->hdb_destroy)(context, db);
251ebfedea0SLionel Sambuc out2:
252ebfedea0SLionel Sambuc free(fdbname);
253ebfedea0SLionel Sambuc free(fmkey);
254ebfedea0SLionel Sambuc return ret;
255ebfedea0SLionel Sambuc }
256ebfedea0SLionel Sambuc
257ebfedea0SLionel Sambuc /*
258ebfedea0SLionel Sambuc * find the keytab entry in `id' for `principal, kvno, enctype' and return
259ebfedea0SLionel Sambuc * it in `entry'. return 0 or an error code
260ebfedea0SLionel Sambuc */
261ebfedea0SLionel Sambuc
262ebfedea0SLionel Sambuc static krb5_error_code KRB5_CALLCONV
hdb_start_seq_get(krb5_context context,krb5_keytab id,krb5_kt_cursor * cursor)263ebfedea0SLionel Sambuc hdb_start_seq_get(krb5_context context,
264ebfedea0SLionel Sambuc krb5_keytab id,
265ebfedea0SLionel Sambuc krb5_kt_cursor *cursor)
266ebfedea0SLionel Sambuc {
267ebfedea0SLionel Sambuc krb5_error_code ret;
268ebfedea0SLionel Sambuc struct hdb_cursor *c;
269ebfedea0SLionel Sambuc struct hdb_data *d = id->data;
270ebfedea0SLionel Sambuc const char *dbname = d->dbname;
271ebfedea0SLionel Sambuc const char *mkey = d->mkey;
272ebfedea0SLionel Sambuc HDB *db;
273ebfedea0SLionel Sambuc
274ebfedea0SLionel Sambuc if (dbname == NULL) {
275ebfedea0SLionel Sambuc /*
276ebfedea0SLionel Sambuc * We don't support enumerating without being told what
277ebfedea0SLionel Sambuc * backend to enumerate on
278ebfedea0SLionel Sambuc */
279ebfedea0SLionel Sambuc ret = KRB5_KT_NOTFOUND;
280ebfedea0SLionel Sambuc return ret;
281ebfedea0SLionel Sambuc }
282ebfedea0SLionel Sambuc
283ebfedea0SLionel Sambuc ret = hdb_create (context, &db, dbname);
284ebfedea0SLionel Sambuc if (ret)
285ebfedea0SLionel Sambuc return ret;
286ebfedea0SLionel Sambuc ret = hdb_set_master_keyfile (context, db, mkey);
287ebfedea0SLionel Sambuc if (ret) {
288ebfedea0SLionel Sambuc (*db->hdb_destroy)(context, db);
289ebfedea0SLionel Sambuc return ret;
290ebfedea0SLionel Sambuc }
291ebfedea0SLionel Sambuc
292ebfedea0SLionel Sambuc ret = (*db->hdb_open)(context, db, O_RDONLY, 0);
293ebfedea0SLionel Sambuc if (ret) {
294ebfedea0SLionel Sambuc (*db->hdb_destroy)(context, db);
295ebfedea0SLionel Sambuc return ret;
296ebfedea0SLionel Sambuc }
297ebfedea0SLionel Sambuc
298ebfedea0SLionel Sambuc cursor->data = c = malloc (sizeof(*c));
299ebfedea0SLionel Sambuc if(c == NULL){
300ebfedea0SLionel Sambuc (*db->hdb_close)(context, db);
301ebfedea0SLionel Sambuc (*db->hdb_destroy)(context, db);
302ebfedea0SLionel Sambuc krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
303ebfedea0SLionel Sambuc return ENOMEM;
304ebfedea0SLionel Sambuc }
305ebfedea0SLionel Sambuc
306ebfedea0SLionel Sambuc c->db = db;
307ebfedea0SLionel Sambuc c->first = TRUE;
308ebfedea0SLionel Sambuc c->next = TRUE;
309ebfedea0SLionel Sambuc c->key_idx = 0;
310ebfedea0SLionel Sambuc
311ebfedea0SLionel Sambuc cursor->data = c;
312ebfedea0SLionel Sambuc return ret;
313ebfedea0SLionel Sambuc }
314ebfedea0SLionel Sambuc
315ebfedea0SLionel Sambuc static int KRB5_CALLCONV
hdb_next_entry(krb5_context context,krb5_keytab id,krb5_keytab_entry * entry,krb5_kt_cursor * cursor)316ebfedea0SLionel Sambuc hdb_next_entry(krb5_context context,
317ebfedea0SLionel Sambuc krb5_keytab id,
318ebfedea0SLionel Sambuc krb5_keytab_entry *entry,
319ebfedea0SLionel Sambuc krb5_kt_cursor *cursor)
320ebfedea0SLionel Sambuc {
321ebfedea0SLionel Sambuc struct hdb_cursor *c = cursor->data;
322ebfedea0SLionel Sambuc krb5_error_code ret;
323ebfedea0SLionel Sambuc
324ebfedea0SLionel Sambuc memset(entry, 0, sizeof(*entry));
325ebfedea0SLionel Sambuc
326ebfedea0SLionel Sambuc if (c->first) {
327ebfedea0SLionel Sambuc c->first = FALSE;
328ebfedea0SLionel Sambuc ret = (c->db->hdb_firstkey)(context, c->db,
329ebfedea0SLionel Sambuc HDB_F_DECRYPT|
330ebfedea0SLionel Sambuc HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
331ebfedea0SLionel Sambuc &c->hdb_entry);
332ebfedea0SLionel Sambuc if (ret == HDB_ERR_NOENTRY)
333ebfedea0SLionel Sambuc return KRB5_KT_END;
334ebfedea0SLionel Sambuc else if (ret)
335ebfedea0SLionel Sambuc return ret;
336ebfedea0SLionel Sambuc
337ebfedea0SLionel Sambuc if (c->hdb_entry.entry.keys.len == 0)
338ebfedea0SLionel Sambuc hdb_free_entry(context, &c->hdb_entry);
339ebfedea0SLionel Sambuc else
340ebfedea0SLionel Sambuc c->next = FALSE;
341ebfedea0SLionel Sambuc }
342ebfedea0SLionel Sambuc
343ebfedea0SLionel Sambuc while (c->next) {
344ebfedea0SLionel Sambuc ret = (c->db->hdb_nextkey)(context, c->db,
345ebfedea0SLionel Sambuc HDB_F_DECRYPT|
346ebfedea0SLionel Sambuc HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
347ebfedea0SLionel Sambuc &c->hdb_entry);
348ebfedea0SLionel Sambuc if (ret == HDB_ERR_NOENTRY)
349ebfedea0SLionel Sambuc return KRB5_KT_END;
350ebfedea0SLionel Sambuc else if (ret)
351ebfedea0SLionel Sambuc return ret;
352ebfedea0SLionel Sambuc
353ebfedea0SLionel Sambuc /* If no keys on this entry, try again */
354ebfedea0SLionel Sambuc if (c->hdb_entry.entry.keys.len == 0)
355ebfedea0SLionel Sambuc hdb_free_entry(context, &c->hdb_entry);
356ebfedea0SLionel Sambuc else
357ebfedea0SLionel Sambuc c->next = FALSE;
358ebfedea0SLionel Sambuc }
359ebfedea0SLionel Sambuc
360ebfedea0SLionel Sambuc /*
361ebfedea0SLionel Sambuc * Return next enc type (keytabs are one slot per key, while
362ebfedea0SLionel Sambuc * hdb is one record per principal.
363ebfedea0SLionel Sambuc */
364ebfedea0SLionel Sambuc
365ebfedea0SLionel Sambuc ret = krb5_copy_principal(context,
366ebfedea0SLionel Sambuc c->hdb_entry.entry.principal,
367ebfedea0SLionel Sambuc &entry->principal);
368ebfedea0SLionel Sambuc if (ret)
369ebfedea0SLionel Sambuc return ret;
370ebfedea0SLionel Sambuc
371ebfedea0SLionel Sambuc entry->vno = c->hdb_entry.entry.kvno;
372ebfedea0SLionel Sambuc ret = krb5_copy_keyblock_contents(context,
373ebfedea0SLionel Sambuc &c->hdb_entry.entry.keys.val[c->key_idx].key,
374ebfedea0SLionel Sambuc &entry->keyblock);
375ebfedea0SLionel Sambuc if (ret) {
376ebfedea0SLionel Sambuc krb5_free_principal(context, entry->principal);
377ebfedea0SLionel Sambuc memset(entry, 0, sizeof(*entry));
378ebfedea0SLionel Sambuc return ret;
379ebfedea0SLionel Sambuc }
380ebfedea0SLionel Sambuc c->key_idx++;
381ebfedea0SLionel Sambuc
382ebfedea0SLionel Sambuc /*
383ebfedea0SLionel Sambuc * Once we get to the end of the list, signal that we want the
384ebfedea0SLionel Sambuc * next entry
385ebfedea0SLionel Sambuc */
386ebfedea0SLionel Sambuc
387*0a6a1f1dSLionel Sambuc if ((size_t)c->key_idx == c->hdb_entry.entry.keys.len) {
388ebfedea0SLionel Sambuc hdb_free_entry(context, &c->hdb_entry);
389ebfedea0SLionel Sambuc c->next = TRUE;
390ebfedea0SLionel Sambuc c->key_idx = 0;
391ebfedea0SLionel Sambuc }
392ebfedea0SLionel Sambuc
393ebfedea0SLionel Sambuc return 0;
394ebfedea0SLionel Sambuc }
395ebfedea0SLionel Sambuc
396ebfedea0SLionel Sambuc
397ebfedea0SLionel Sambuc static int KRB5_CALLCONV
hdb_end_seq_get(krb5_context context,krb5_keytab id,krb5_kt_cursor * cursor)398ebfedea0SLionel Sambuc hdb_end_seq_get(krb5_context context,
399ebfedea0SLionel Sambuc krb5_keytab id,
400ebfedea0SLionel Sambuc krb5_kt_cursor *cursor)
401ebfedea0SLionel Sambuc {
402ebfedea0SLionel Sambuc struct hdb_cursor *c = cursor->data;
403ebfedea0SLionel Sambuc
404ebfedea0SLionel Sambuc if (!c->next)
405ebfedea0SLionel Sambuc hdb_free_entry(context, &c->hdb_entry);
406ebfedea0SLionel Sambuc
407ebfedea0SLionel Sambuc (c->db->hdb_close)(context, c->db);
408ebfedea0SLionel Sambuc (c->db->hdb_destroy)(context, c->db);
409ebfedea0SLionel Sambuc
410ebfedea0SLionel Sambuc free(c);
411ebfedea0SLionel Sambuc return 0;
412ebfedea0SLionel Sambuc }
413ebfedea0SLionel Sambuc
414ebfedea0SLionel Sambuc krb5_kt_ops hdb_kt_ops = {
415ebfedea0SLionel Sambuc "HDB",
416ebfedea0SLionel Sambuc hdb_resolve,
417ebfedea0SLionel Sambuc hdb_get_name,
418ebfedea0SLionel Sambuc hdb_close,
419ebfedea0SLionel Sambuc NULL, /* destroy */
420ebfedea0SLionel Sambuc hdb_get_entry,
421ebfedea0SLionel Sambuc hdb_start_seq_get,
422ebfedea0SLionel Sambuc hdb_next_entry,
423ebfedea0SLionel Sambuc hdb_end_seq_get,
424ebfedea0SLionel Sambuc NULL, /* add */
425ebfedea0SLionel Sambuc NULL /* remove */
426ebfedea0SLionel Sambuc };
427