s4:heimdal: import lorikeet-heimdal-202201172009 (commit 5a0b45cd723628b3690ea848548b...
[samba.git] / source4 / heimdal / lib / hdb / keytab.c
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(context))) == NULL)
164         return krb5_enomem(context);
165     return 0;
166 }
167
168 /*
169  * find the keytab entry in `id' for `principal, kvno, enctype' and return
170  * it in `entry'.  return 0 or an error code
171  */
172
173 static krb5_error_code KRB5_CALLCONV
174 hdb_get_entry(krb5_context context,
175               krb5_keytab id,
176               krb5_const_principal principal,
177               krb5_kvno kvno,
178               krb5_enctype enctype,
179               krb5_keytab_entry *entry)
180 {
181     hdb_entry_ex ent;
182     krb5_error_code ret;
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;
187     HDB *db;
188     size_t i;
189
190     memset(&ent, 0, sizeof(ent));
191
192     if (dbname == NULL) {
193         ret = find_db(context, &fdbname, &fmkey, principal);
194         if (ret)
195             return ret;
196         dbname = fdbname;
197         mkey = fmkey;
198     }
199
200     ret = hdb_create (context, &db, dbname);
201     if (ret)
202         goto out2;
203     ret = hdb_set_master_keyfile (context, db, mkey);
204     if (ret) {
205         (*db->hdb_destroy)(context, db);
206         goto out2;
207     }
208
209     ret = (*db->hdb_open)(context, db, O_RDONLY, 0);
210     if (ret) {
211         (*db->hdb_destroy)(context, db);
212         goto out2;
213     }
214
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,
218                          0, 0, kvno, &ent);
219
220     if(ret == HDB_ERR_NOENTRY) {
221         ret = KRB5_KT_NOTFOUND;
222         goto out;
223     }else if(ret)
224         goto out;
225
226     if(kvno && (krb5_kvno)ent.entry.kvno != kvno) {
227         hdb_free_entry(context, &ent);
228         ret = KRB5_KT_NOTFOUND;
229         goto out;
230     }
231     if(enctype == 0)
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,
241                                         &entry->keyblock);
242             ret = 0;
243             break;
244         }
245     }
246     hdb_free_entry(context, &ent);
247  out:
248     (*db->hdb_close)(context, db);
249     (*db->hdb_destroy)(context, db);
250  out2:
251     free(fdbname);
252     free(fmkey);
253     return ret;
254 }
255
256 /*
257  * find the keytab entry in `id' for `principal, kvno, enctype' and return
258  * it in `entry'.  return 0 or an error code
259  */
260
261 static krb5_error_code KRB5_CALLCONV
262 hdb_start_seq_get(krb5_context context,
263                   krb5_keytab id,
264                   krb5_kt_cursor *cursor)
265 {
266     krb5_error_code ret;
267     struct hdb_cursor *c;
268     struct hdb_data *d = id->data;
269     const char *dbname = d->dbname;
270     const char *mkey   = d->mkey;
271     HDB *db;
272
273     if (dbname == NULL) {
274         /*
275          * We don't support enumerating without being told what
276          * backend to enumerate on
277          */
278         ret = KRB5_KT_NOTFOUND;
279         return ret;
280     }
281
282     ret = hdb_create (context, &db, dbname);
283     if (ret)
284         return ret;
285     ret = hdb_set_master_keyfile (context, db, mkey);
286     if (ret) {
287         (*db->hdb_destroy)(context, db);
288         return ret;
289     }
290
291     ret = (*db->hdb_open)(context, db, O_RDONLY, 0);
292     if (ret) {
293         (*db->hdb_destroy)(context, db);
294         return ret;
295     }
296
297     cursor->data = c = malloc (sizeof(*c));
298     if(c == NULL){
299         (*db->hdb_close)(context, db);
300         (*db->hdb_destroy)(context, db);
301         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
302         return ENOMEM;
303     }
304
305     c->db = db;
306     c->first = TRUE;
307     c->next = TRUE;
308     c->key_idx = 0;
309
310     cursor->data = c;
311     return ret;
312 }
313
314 static int KRB5_CALLCONV
315 hdb_next_entry(krb5_context context,
316                krb5_keytab id,
317                krb5_keytab_entry *entry,
318                krb5_kt_cursor *cursor)
319 {
320     struct hdb_cursor *c = cursor->data;
321     krb5_error_code ret;
322
323     memset(entry, 0, sizeof(*entry));
324
325     if (c->first) {
326         c->first = FALSE;
327         ret = (c->db->hdb_firstkey)(context, c->db,
328                                     HDB_F_DECRYPT|
329                                     HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
330                                     &c->hdb_entry);
331         if (ret == HDB_ERR_NOENTRY)
332             return KRB5_KT_END;
333         else if (ret)
334             return ret;
335
336         if (c->hdb_entry.entry.keys.len == 0)
337             hdb_free_entry(context, &c->hdb_entry);
338         else
339             c->next = FALSE;
340     }
341
342     while (c->next) {
343         ret = (c->db->hdb_nextkey)(context, c->db,
344                                    HDB_F_DECRYPT|
345                                    HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
346                                    &c->hdb_entry);
347         if (ret == HDB_ERR_NOENTRY)
348             return KRB5_KT_END;
349         else if (ret)
350             return ret;
351
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);
355         else
356             c->next = FALSE;
357     }
358
359     /*
360      * Return next enc type (keytabs are one slot per key, while
361      * hdb is one record per principal.
362      */
363
364     ret = krb5_copy_principal(context,
365                               c->hdb_entry.entry.principal,
366                               &entry->principal);
367     if (ret)
368         return ret;
369
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,
373                                       &entry->keyblock);
374     if (ret) {
375         krb5_free_principal(context, entry->principal);
376         memset(entry, 0, sizeof(*entry));
377         return ret;
378     }
379     c->key_idx++;
380
381     /*
382      * Once we get to the end of the list, signal that we want the
383      * next entry
384      */
385
386     if ((size_t)c->key_idx == c->hdb_entry.entry.keys.len) {
387         hdb_free_entry(context, &c->hdb_entry);
388         c->next = TRUE;
389         c->key_idx = 0;
390     }
391
392     return 0;
393 }
394
395
396 static int KRB5_CALLCONV
397 hdb_end_seq_get(krb5_context context,
398                 krb5_keytab id,
399                 krb5_kt_cursor *cursor)
400 {
401     struct hdb_cursor *c = cursor->data;
402
403     if (!c->next)
404         hdb_free_entry(context, &c->hdb_entry);
405
406     (c->db->hdb_close)(context, c->db);
407     (c->db->hdb_destroy)(context, c->db);
408
409     free(c);
410     return 0;
411 }
412
413 krb5_kt_ops hdb_kt_ops = {
414     "HDB",
415     hdb_resolve,
416     hdb_get_name,
417     hdb_close,
418     NULL,               /* destroy */
419     hdb_get_entry,
420     hdb_start_seq_get,
421     hdb_next_entry,
422     hdb_end_seq_get,
423     NULL,               /* add */
424     NULL,               /* remove */
425     NULL,
426     0
427 };
428
429 krb5_kt_ops hdb_get_kt_ops = {
430     "HDBGET",
431     hdb_resolve,
432     hdb_get_name,
433     hdb_close,
434     NULL,
435     hdb_get_entry,
436     NULL,
437     NULL,
438     NULL,
439     NULL,
440     NULL,
441     NULL,
442     0
443 };