Indent the patch from Andrew and make it compile again
[heimdal.git] / lib / hdb / keytab.c
blob4b4114f6cab23150409176e75e74d79ddfbb3e19
1 /*
2 * Copyright (c) 1999 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "hdb_locl.h"
36 /* keytab backend for HDB databases */
38 struct hdb_data {
39 char *dbname;
40 char *mkey;
43 struct hdb_cursor {
44 HDB *db;
45 hdb_entry_ex hdb_entry;
46 int first, next;
47 int key_idx;
51 * the format for HDB keytabs is:
52 * HDB:[database:file:mkey]
55 static krb5_error_code
56 hdb_resolve(krb5_context context, const char *name, krb5_keytab id)
58 struct hdb_data *d;
59 const char *db, *mkey;
61 d = malloc(sizeof(*d));
62 if(d == NULL) {
63 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
64 return ENOMEM;
66 db = name;
67 mkey = strchr(name, ':');
68 if(mkey == NULL || mkey[1] == '\0') {
69 if(*name == '\0')
70 d->dbname = NULL;
71 else {
72 d->dbname = strdup(name);
73 if(d->dbname == NULL) {
74 free(d);
75 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
76 return ENOMEM;
79 d->mkey = NULL;
80 } else {
81 if((mkey - db) == 0) {
82 d->dbname = NULL;
83 } else {
84 d->dbname = malloc(mkey - db + 1);
85 if(d->dbname == NULL) {
86 free(d);
87 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
88 return ENOMEM;
90 memmove(d->dbname, db, mkey - db);
91 d->dbname[mkey - db] = '\0';
93 d->mkey = strdup(mkey + 1);
94 if(d->mkey == NULL) {
95 free(d->dbname);
96 free(d);
97 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
98 return ENOMEM;
101 id->data = d;
102 return 0;
105 static krb5_error_code
106 hdb_close(krb5_context context, krb5_keytab id)
108 struct hdb_data *d = id->data;
110 free(d->dbname);
111 free(d->mkey);
112 free(d);
113 return 0;
116 static krb5_error_code
117 hdb_get_name(krb5_context context,
118 krb5_keytab id,
119 char *name,
120 size_t namesize)
122 struct hdb_data *d = id->data;
124 snprintf(name, namesize, "%s%s%s",
125 d->dbname ? d->dbname : "",
126 (d->dbname || d->mkey) ? ":" : "",
127 d->mkey ? d->mkey : "");
128 return 0;
132 * try to figure out the database (`dbname') and master-key (`mkey')
133 * that should be used for `principal'.
136 static krb5_error_code
137 find_db (krb5_context context,
138 char **dbname,
139 char **mkey,
140 krb5_const_principal principal)
142 krb5_const_realm realm = krb5_principal_get_realm(context, principal);
143 krb5_error_code ret;
144 struct hdb_dbinfo *head, *dbinfo = NULL;
146 *dbname = *mkey = NULL;
148 ret = hdb_get_dbinfo(context, &head);
149 if (ret)
150 return ret;
152 while ((dbinfo = hdb_dbinfo_get_next(head, dbinfo)) != NULL) {
153 const char *p = hdb_dbinfo_get_realm(context, dbinfo);
154 if (p && strcmp (realm, p) == 0) {
155 p = hdb_dbinfo_get_dbname(context, dbinfo);
156 if (p)
157 *dbname = strdup(p);
158 p = hdb_dbinfo_get_mkey_file(context, dbinfo);
159 if (p)
160 *mkey = strdup(p);
161 break;
164 hdb_free_dbinfo(context, &head);
165 if (*dbname == NULL)
166 *dbname = strdup(HDB_DEFAULT_DB);
167 return 0;
171 * find the keytab entry in `id' for `principal, kvno, enctype' and return
172 * it in `entry'. return 0 or an error code
175 static krb5_error_code
176 hdb_get_entry(krb5_context context,
177 krb5_keytab id,
178 krb5_const_principal principal,
179 krb5_kvno kvno,
180 krb5_enctype enctype,
181 krb5_keytab_entry *entry)
183 hdb_entry_ex ent;
184 krb5_error_code ret;
185 struct hdb_data *d = id->data;
186 const char *dbname = d->dbname;
187 const char *mkey = d->mkey;
188 char *fdbname = NULL, *fmkey = NULL;
189 HDB *db;
190 int i;
192 memset(&ent, 0, sizeof(ent));
194 if (dbname == NULL) {
195 ret = find_db(context, &fdbname, &fmkey, principal);
196 if (ret)
197 return ret;
198 dbname = fdbname;
199 mkey = fmkey;
202 ret = hdb_create (context, &db, dbname);
203 if (ret)
204 goto out2;
205 ret = hdb_set_master_keyfile (context, db, mkey);
206 if (ret) {
207 (*db->hdb_destroy)(context, db);
208 goto out2;
211 ret = (*db->hdb_open)(context, db, O_RDONLY, 0);
212 if (ret) {
213 (*db->hdb_destroy)(context, db);
214 goto out2;
216 ret = (*db->hdb_fetch)(context, db, principal,
217 HDB_F_DECRYPT|
218 HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
219 &ent);
221 if(ret == HDB_ERR_NOENTRY) {
222 ret = KRB5_KT_NOTFOUND;
223 goto out;
224 }else if(ret)
225 goto out;
227 if(kvno && ent.entry.kvno != kvno) {
228 hdb_free_entry(context, &ent);
229 ret = KRB5_KT_NOTFOUND;
230 goto out;
232 if(enctype == 0)
233 if(ent.entry.keys.len > 0)
234 enctype = ent.entry.keys.val[0].key.keytype;
235 ret = KRB5_KT_NOTFOUND;
236 for(i = 0; i < ent.entry.keys.len; i++) {
237 if(ent.entry.keys.val[i].key.keytype == enctype) {
238 krb5_copy_principal(context, principal, &entry->principal);
239 entry->vno = ent.entry.kvno;
240 krb5_copy_keyblock_contents(context,
241 &ent.entry.keys.val[i].key,
242 &entry->keyblock);
243 ret = 0;
244 break;
247 hdb_free_entry(context, &ent);
248 out:
249 (*db->hdb_close)(context, db);
250 (*db->hdb_destroy)(context, db);
251 out2:
252 free(fdbname);
253 free(fmkey);
254 return ret;
258 * find the keytab entry in `id' for `principal, kvno, enctype' and return
259 * it in `entry'. return 0 or an error code
262 static krb5_error_code
263 hdb_start_seq_get(krb5_context context,
264 krb5_keytab id,
265 krb5_kt_cursor *cursor)
267 krb5_error_code ret;
268 struct hdb_cursor *c;
269 struct hdb_data *d = id->data;
270 const char *dbname = d->dbname;
271 const char *mkey = d->mkey;
272 HDB *db;
274 if (dbname == NULL) {
276 * We don't support enumerating without being told what
277 * backend to enumerate on
279 ret = KRB5_KT_NOTFOUND;
280 return ret;
283 ret = hdb_create (context, &db, dbname);
284 if (ret)
285 return ret;
286 ret = hdb_set_master_keyfile (context, db, mkey);
287 if (ret) {
288 (*db->hdb_destroy)(context, db);
289 return ret;
292 ret = (*db->hdb_open)(context, db, O_RDONLY, 0);
293 if (ret) {
294 (*db->hdb_destroy)(context, db);
295 return ret;
298 cursor->data = c = malloc (sizeof(*c));
299 if(c == NULL){
300 (*db->hdb_close)(context, db);
301 (*db->hdb_destroy)(context, db);
302 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
303 return ENOMEM;
306 c->db = db;
307 c->first = TRUE;
308 c->next = TRUE;
309 c->key_idx = 0;
311 cursor->data = c;
312 return ret;
315 static int
316 hdb_next_entry(krb5_context context,
317 krb5_keytab id,
318 krb5_keytab_entry *entry,
319 krb5_kt_cursor *cursor)
321 struct hdb_cursor *c = cursor->data;
322 krb5_error_code ret;
324 if (c->first) {
325 c->first = FALSE;
326 ret = (c->db->hdb_firstkey)(context, c->db,
327 HDB_F_DECRYPT|
328 HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
329 &c->hdb_entry);
330 if (ret == HDB_ERR_NOENTRY)
331 return KRB5_KT_END;
332 else if (ret)
333 return ret;
335 if (c->hdb_entry.entry.keys.len == 0)
336 hdb_free_entry(context, &c->hdb_entry);
337 else
338 c->next = FALSE;
341 while (c->next) {
342 ret = (c->db->hdb_nextkey)(context, c->db,
343 HDB_F_DECRYPT|
344 HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
345 &c->hdb_entry);
346 if (ret == HDB_ERR_NOENTRY)
347 return KRB5_KT_END;
348 else if (ret)
349 return ret;
351 /* If no keys on this entry, try again */
352 if (c->hdb_entry.entry.keys.len == 0)
353 hdb_free_entry(context, &c->hdb_entry);
354 else
355 c->next = FALSE;
359 * Return next enc type (keytabs are one slot per key, while
360 * hdb is one record per principal.
363 krb5_copy_principal(context,
364 c->hdb_entry.entry.principal,
365 &entry->principal);
366 entry->vno = c->hdb_entry.entry.kvno;
367 krb5_copy_keyblock_contents(context,
368 &c->hdb_entry.entry.keys.val[c->key_idx].key,
369 &entry->keyblock);
370 c->key_idx++;
373 * Once we get to the end of the list, signal that we want the
374 * next entry
377 if (c->key_idx == c->hdb_entry.entry.keys.len) {
378 hdb_free_entry(context, &c->hdb_entry);
379 c->next = TRUE;
380 c->key_idx = 0;
382 return 0;
386 static int hdb_end_seq_get(krb5_context context,
387 krb5_keytab id,
388 krb5_kt_cursor *cursor) {
389 struct hdb_cursor *c = cursor->data;
390 (c->db->hdb_close)(context, c->db);
391 (c->db->hdb_destroy)(context, c->db);
392 if (!c->next) {
393 hdb_free_entry(context, &c->hdb_entry);
395 free(c);
396 return 0;
399 krb5_kt_ops hdb_kt_ops = {
400 "HDB",
401 hdb_resolve,
402 hdb_get_name,
403 hdb_close,
404 NULL, /* destroy */
405 hdb_get_entry,
406 hdb_start_seq_get,
407 hdb_next_entry,
408 hdb_end_seq_get,
409 NULL, /* add */
410 NULL /* remove */