9b36a268cfd7b34feddf9134c1a334b0bb68382b
[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 /*
44  * the format for HDB keytabs is:
45  * HDB:[database:file:mkey]
46  */
47
48 static krb5_error_code
49 hdb_resolve(krb5_context context, const char *name, krb5_keytab id)
50 {
51     struct hdb_data *d;
52     const char *db, *mkey;
53
54     d = malloc(sizeof(*d));
55     if(d == NULL) {
56         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
57         return ENOMEM;
58     }
59     db = name;
60     mkey = strchr(name, ':');
61     if(mkey == NULL || mkey[1] == '\0') {
62         if(*name == '\0')
63             d->dbname = NULL;
64         else {
65             d->dbname = strdup(name);
66             if(d->dbname == NULL) {
67                 free(d);
68                 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
69                 return ENOMEM;
70             }
71         }
72         d->mkey = NULL;
73     } else {
74         if((mkey - db) == 0) {
75             d->dbname = NULL;
76         } else {
77             d->dbname = malloc(mkey - db + 1);
78             if(d->dbname == NULL) {
79                 free(d);
80                 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
81                 return ENOMEM;
82             }
83             memmove(d->dbname, db, mkey - db);
84             d->dbname[mkey - db] = '\0';
85         }
86         d->mkey = strdup(mkey + 1);
87         if(d->mkey == NULL) {
88             free(d->dbname);
89             free(d);
90             krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
91             return ENOMEM;
92         }
93     }
94     id->data = d;
95     return 0;
96 }
97
98 static krb5_error_code
99 hdb_close(krb5_context context, krb5_keytab id)
100 {
101     struct hdb_data *d = id->data;
102
103     free(d->dbname);
104     free(d->mkey);
105     free(d);
106     return 0;
107 }
108
109 static krb5_error_code
110 hdb_get_name(krb5_context context,
111              krb5_keytab id,
112              char *name,
113              size_t namesize)
114 {
115     struct hdb_data *d = id->data;
116
117     snprintf(name, namesize, "%s%s%s",
118              d->dbname ? d->dbname : "",
119              (d->dbname || d->mkey) ? ":" : "",
120              d->mkey ? d->mkey : "");
121     return 0;
122 }
123
124 /*
125  * try to figure out the database (`dbname') and master-key (`mkey')
126  * that should be used for `principal'.
127  */
128
129 static krb5_error_code
130 find_db (krb5_context context,
131          char **dbname,
132          char **mkey,
133          krb5_const_principal principal)
134 {
135     krb5_const_realm realm = krb5_principal_get_realm(context, principal);
136     krb5_error_code ret;
137     struct hdb_dbinfo *head, *dbinfo = NULL;
138
139     *dbname = *mkey = NULL;
140
141     ret = hdb_get_dbinfo(context, &head);
142     if (ret)
143         return ret;
144
145     while ((dbinfo = hdb_dbinfo_get_next(head, dbinfo)) != NULL) {
146         const char *p = hdb_dbinfo_get_realm(context, dbinfo);
147         if (p && strcmp (realm, p) == 0) {
148             p = hdb_dbinfo_get_dbname(context, dbinfo);
149             if (p)
150                 *dbname = strdup(p);
151             p = hdb_dbinfo_get_mkey_file(context, dbinfo);
152             if (p)
153                 *mkey = strdup(p);
154             break;
155         }
156     }
157     hdb_free_dbinfo(context, &head);
158     if (*dbname == NULL)
159         *dbname = strdup(HDB_DEFAULT_DB);
160     return 0;
161 }
162
163 /*
164  * find the keytab entry in `id' for `principal, kvno, enctype' and return
165  * it in `entry'.  return 0 or an error code
166  */
167
168 static krb5_error_code
169 hdb_get_entry(krb5_context context,
170               krb5_keytab id,
171               krb5_const_principal principal,
172               krb5_kvno kvno,
173               krb5_enctype enctype,
174               krb5_keytab_entry *entry)
175 {
176     hdb_entry_ex ent;
177     krb5_error_code ret;
178     struct hdb_data *d = id->data;
179     const char *dbname = d->dbname;
180     const char *mkey   = d->mkey;
181     char *fdbname = NULL, *fmkey = NULL;
182     HDB *db;
183     int i;
184
185     memset(&ent, 0, sizeof(ent));
186
187     if (dbname == NULL) {
188         ret = find_db(context, &fdbname, &fmkey, principal);
189         if (ret)
190             return ret;
191         dbname = fdbname;
192         mkey = fmkey;
193     }
194
195     ret = hdb_create (context, &db, dbname);
196     if (ret)
197         goto out2;
198     ret = hdb_set_master_keyfile (context, db, mkey);
199     if (ret) {
200         (*db->hdb_destroy)(context, db);
201         goto out2;
202     }
203         
204     ret = (*db->hdb_open)(context, db, O_RDONLY, 0);
205     if (ret) {
206         (*db->hdb_destroy)(context, db);
207         goto out2;
208     }
209     ret = (*db->hdb_fetch)(context, db, principal,
210                            HDB_F_DECRYPT|
211                            HDB_F_GET_CLIENT|HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
212                            &ent);
213
214     if(ret == HDB_ERR_NOENTRY) {
215         ret = KRB5_KT_NOTFOUND;
216         goto out;
217     }else if(ret)
218         goto out;
219
220     if(kvno && ent.entry.kvno != kvno) {
221         hdb_free_entry(context, &ent);
222         ret = KRB5_KT_NOTFOUND;
223         goto out;
224     }
225     if(enctype == 0)
226         if(ent.entry.keys.len > 0)
227             enctype = ent.entry.keys.val[0].key.keytype;
228     ret = KRB5_KT_NOTFOUND;
229     for(i = 0; i < ent.entry.keys.len; i++) {
230         if(ent.entry.keys.val[i].key.keytype == enctype) {
231             krb5_copy_principal(context, principal, &entry->principal);
232             entry->vno = ent.entry.kvno;
233             krb5_copy_keyblock_contents(context,
234                                         &ent.entry.keys.val[i].key,
235                                         &entry->keyblock);
236             ret = 0;
237             break;
238         }
239     }
240     hdb_free_entry(context, &ent);
241  out:
242     (*db->hdb_close)(context, db);
243     (*db->hdb_destroy)(context, db);
244  out2:
245     free(fdbname);
246     free(fmkey);
247     return ret;
248 }
249
250 krb5_kt_ops hdb_kt_ops = {
251     "HDB",
252     hdb_resolve,
253     hdb_get_name,
254     hdb_close,
255     NULL,               /* destroy */
256     hdb_get_entry,
257     NULL,               /* start_seq_get */
258     NULL,               /* next_entry */
259     NULL,               /* end_seq_get */
260     NULL,               /* add */
261     NULL                /* remove */
262 };