Debian Codegraph @

lib/hdb/keytab.c

raw
1 /*
2 * Copyright (c) 1999 - 2002 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 struct hdb_data {
39 char *dbname;
40 char *mkey;
41 };
42
43 struct hdb_cursor {
44 HDB *db;
45 hdb_entry_ex hdb_entry;
46 int first, next;
47 int key_idx;
48 };
49
50 /*
51 * the format for HDB keytabs is:
52 * HDB:[HDBFORMAT:database-specific-data[:mkey=mkey-file]]
53 */
54
55 static krb5_error_code KRB5_CALLCONV
56 hdb_resolve(krb5_context context, const char *name, krb5_keytab id)
57 {
58 struct hdb_data *d;
59 const char *db, *mkey;
60
61 d = malloc(sizeof(*d));
62 if(d == NULL) {
63 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
64 return ENOMEM;
65 }
66 db = name;
67 mkey = strstr(name, ":mkey=");
68 if(mkey == NULL || mkey[6] == '\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;
77 }
78 }
79 d->mkey = NULL;
80 } else {
81 d->dbname = malloc(mkey - db + 1);
82 if(d->dbname == NULL) {
83 free(d);
84 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
85 return ENOMEM;
86 }
87 memmove(d->dbname, db, mkey - db);
88 d->dbname[mkey - db] = '\0';
89
90 d->mkey = strdup(mkey + 6);
91 if(d->mkey == NULL) {
92 free(d->dbname);
93 free(d);
94 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
95 return ENOMEM;
96 }
97 }
98 id->data = d;
99 return 0;
100 }
101
102 static krb5_error_code KRB5_CALLCONV
103 hdb_close(krb5_context context, krb5_keytab id)
104 {
105 struct hdb_data *d = id->data;
106
107 free(d->dbname);
108 free(d->mkey);
109 free(d);
110 return 0;
111 }
112
113 static krb5_error_code KRB5_CALLCONV
114 hdb_get_name(krb5_context context,
115 krb5_keytab id,
116 char *name,
117 size_t namesize)
118 {
119 struct hdb_data *d = id->data;
120
121 snprintf(name, namesize, "%s%s%s",
122 d->dbname ? d->dbname : "",
123 (d->dbname || d->mkey) ? ":" : "",
124 d->mkey ? d->mkey : "");
125 return 0;
126 }
127
128 /*
129 * try to figure out the database (`dbname') and master-key (`mkey')
130 * that should be used for `principal'.
131 */
132
133 static krb5_error_code
134 find_db (krb5_context context,
135 char **dbname,
136 char **mkey,
137 krb5_const_principal principal)
138 {
139 krb5_const_realm realm = krb5_principal_get_realm(context, principal);
140 krb5_error_code ret;
141 struct hdb_dbinfo *head, *dbinfo = NULL;
142
143 *dbname = *mkey = NULL;
144
145 ret = hdb_get_dbinfo(context, &head);
146 if (ret)
147 return ret;
148
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);
153 if (p)
154 *dbname = strdup(p);
155 p = hdb_dbinfo_get_mkey_file(context, dbinfo);
156 if (p)
157 *mkey = strdup(p);
158 break;
159 }
160 }
161 hdb_free_dbinfo(context, &head);
162 if (*dbname == NULL &&
163 (*dbname = strdup(HDB_DEFAULT_DB)) == NULL) {
164 free(*mkey);
165 *mkey = NULL;
166 return krb5_enomem(context);
167 }
168 return 0;
169 }
170
171 /*
172 * find the keytab entry in `id' for `principal, kvno, enctype' and return
173 * it in `entry'. return 0 or an error code
174 */
175
176 static krb5_error_code KRB5_CALLCONV
177 hdb_get_entry(krb5_context context,
178 krb5_keytab id,
179 krb5_const_principal principal,
180 krb5_kvno kvno,
181 krb5_enctype enctype,
182 krb5_keytab_entry *entry)
183 {
184 hdb_entry_ex ent;
185 krb5_error_code ret;
186 struct hdb_data *d = id->data;
187 const char *dbname = d->dbname;
188 const char *mkey = d->mkey;
189 char *fdbname = NULL, *fmkey = NULL;
190 HDB *db;
191 size_t i;
192
193 memset(&ent, 0, sizeof(ent));
194
195 if (dbname == NULL) {
196 ret = find_db(context, &fdbname, &fmkey, principal);
197 if (ret)
198 return ret;
199 dbname = fdbname;
200 mkey = fmkey;
201 }
202
203 ret = hdb_create (context, &db, dbname);
204 if (ret)
205 goto out2;
206 ret = hdb_set_master_keyfile (context, db, mkey);
207 if (ret) {
208 (*db->hdb_destroy)(context, db);
209 goto out2;
210 }
211
212 ret = (*db->hdb_open)(context, db, O_RDONLY, 0);
213 if (ret) {
214 (*db->hdb_destroy)(context, db);
215 goto out2;
216 }
217
218 ret = (*db->hdb_fetch_kvno)(context, db, principal,
219 HDB_F_DECRYPT|HDB_F_KVNO_SPECIFIED|
220 HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
221 kvno, &ent);
222
223 if(ret == HDB_ERR_NOENTRY) {
224 ret = KRB5_KT_NOTFOUND;
225 goto out;
226 }else if(ret)
227 goto out;
228
229 if(kvno && (krb5_kvno)ent.entry.kvno != kvno) {
230 hdb_free_entry(context, &ent);
231 ret = KRB5_KT_NOTFOUND;
232 goto out;
233 }
234 if(enctype == 0)
235 if(ent.entry.keys.len > 0)
236 enctype = ent.entry.keys.val[0].key.keytype;
237 ret = KRB5_KT_NOTFOUND;
238 for(i = 0; i < ent.entry.keys.len; i++) {
239 if(ent.entry.keys.val[i].key.keytype == enctype) {
240 krb5_copy_principal(context, principal, &entry->principal);
241 entry->vno = ent.entry.kvno;
242 krb5_copy_keyblock_contents(context,
243 &ent.entry.keys.val[i].key,
244 &entry->keyblock);
245 ret = 0;
246 break;
247 }
248 }
249 hdb_free_entry(context, &ent);
250 out:
251 (*db->hdb_close)(context, db);
252 (*db->hdb_destroy)(context, db);
253 out2:
254 free(fdbname);
255 free(fmkey);
256 return ret;
257 }
258
259 /*
260 * find the keytab entry in `id' for `principal, kvno, enctype' and return
261 * it in `entry'. return 0 or an error code
262 */
263
264 static krb5_error_code KRB5_CALLCONV
265 hdb_start_seq_get(krb5_context context,
266 krb5_keytab id,
267 krb5_kt_cursor *cursor)
268 {
269 krb5_error_code ret;
270 struct hdb_cursor *c;
271 struct hdb_data *d = id->data;
272 const char *dbname = d->dbname;
273 const char *mkey = d->mkey;
274 HDB *db;
275
276 if (dbname == NULL) {
277 /*
278 * We don't support enumerating without being told what
279 * backend to enumerate on
280 */
281 ret = KRB5_KT_NOTFOUND;
282 return ret;
283 }
284
285 ret = hdb_create (context, &db, dbname);
286 if (ret)
287 return ret;
288 ret = hdb_set_master_keyfile (context, db, mkey);
289 if (ret) {
290 (*db->hdb_destroy)(context, db);
291 return ret;
292 }
293
294 ret = (*db->hdb_open)(context, db, O_RDONLY, 0);
295 if (ret) {
296 (*db->hdb_destroy)(context, db);
297 return ret;
298 }
299
300 cursor->data = c = malloc (sizeof(*c));
301 if(c == NULL){
302 (*db->hdb_close)(context, db);
303 (*db->hdb_destroy)(context, db);
304 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
305 return ENOMEM;
306 }
307
308 c->db = db;
309 c->first = TRUE;
310 c->next = TRUE;
311 c->key_idx = 0;
312
313 cursor->data = c;
314 return ret;
315 }
316
317 static int KRB5_CALLCONV
318 hdb_next_entry(krb5_context context,
319 krb5_keytab id,
320 krb5_keytab_entry *entry,
321 krb5_kt_cursor *cursor)
322 {
323 struct hdb_cursor *c = cursor->data;
324 krb5_error_code ret;
325
326 memset(entry, 0, sizeof(*entry));
327
328 if (c->first) {
329 c->first = FALSE;
330 ret = (c->db->hdb_firstkey)(context, c->db,
331 HDB_F_DECRYPT|
332 HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
333 &c->hdb_entry);
334 if (ret == HDB_ERR_NOENTRY)
335 return KRB5_KT_END;
336 else if (ret)
337 return ret;
338
339 if (c->hdb_entry.entry.keys.len == 0)
340 hdb_free_entry(context, &c->hdb_entry);
341 else
342 c->next = FALSE;
343 }
344
345 while (c->next) {
346 ret = (c->db->hdb_nextkey)(context, c->db,
347 HDB_F_DECRYPT|
348 HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
349 &c->hdb_entry);
350 if (ret == HDB_ERR_NOENTRY)
351 return KRB5_KT_END;
352 else if (ret)
353 return ret;
354
355 /* If no keys on this entry, try again */
356 if (c->hdb_entry.entry.keys.len == 0)
357 hdb_free_entry(context, &c->hdb_entry);
358 else
359 c->next = FALSE;
360 }
361
362 /*
363 * Return next enc type (keytabs are one slot per key, while
364 * hdb is one record per principal.
365 */
366
367 ret = krb5_copy_principal(context,
368 c->hdb_entry.entry.principal,
369 &entry->principal);
370 if (ret)
371 return ret;
372
373 entry->vno = c->hdb_entry.entry.kvno;
374 ret = krb5_copy_keyblock_contents(context,
375 &c->hdb_entry.entry.keys.val[c->key_idx].key,
376 &entry->keyblock);
377 if (ret) {
378 krb5_free_principal(context, entry->principal);
379 memset(entry, 0, sizeof(*entry));
380 return ret;
381 }
382 c->key_idx++;
383
384 /*
385 * Once we get to the end of the list, signal that we want the
386 * next entry
387 */
388
389 if ((size_t)c->key_idx == c->hdb_entry.entry.keys.len) {
390 hdb_free_entry(context, &c->hdb_entry);
391 c->next = TRUE;
392 c->key_idx = 0;
393 }
394
395 return 0;
396 }
397
398
399 static int KRB5_CALLCONV
400 hdb_end_seq_get(krb5_context context,
401 krb5_keytab id,
402 krb5_kt_cursor *cursor)
403 {
404 struct hdb_cursor *c = cursor->data;
405
406 if (!c->next)
407 hdb_free_entry(context, &c->hdb_entry);
408
409 (c->db->hdb_close)(context, c->db);
410 (c->db->hdb_destroy)(context, c->db);
411
412 free(c);
413 return 0;
414 }
415
416 krb5_kt_ops hdb_kt_ops = {
417 "HDB",
418 hdb_resolve,
419 hdb_get_name,
420 hdb_close,
421 NULL, /* destroy */
422 hdb_get_entry,
423 hdb_start_seq_get,
424 hdb_next_entry,
425 hdb_end_seq_get,
426 NULL, /* add */
427 NULL, /* remove */
428 NULL,
429 0
430 };
431
432 krb5_kt_ops hdb_get_kt_ops = {
433 "HDBGET",
434 hdb_resolve,
435 hdb_get_name,
436 hdb_close,
437 NULL,
438 hdb_get_entry,
439 NULL,
440 NULL,
441 NULL,
442 NULL,
443 NULL,
444 NULL,
445 0
446 };