1*d3273b5bSchristos /* $NetBSD: hdb-keytab.c,v 1.2 2017/01/28 21:31:48 christos Exp $ */
2ca1c9b0cSelric
3ca1c9b0cSelric /*
4ca1c9b0cSelric * Copyright (c) 2009 Kungliga Tekniska H�gskolan
5ca1c9b0cSelric * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric * All rights reserved.
7ca1c9b0cSelric *
8ca1c9b0cSelric * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9ca1c9b0cSelric *
10ca1c9b0cSelric * Redistribution and use in source and binary forms, with or without
11ca1c9b0cSelric * modification, are permitted provided that the following conditions
12ca1c9b0cSelric * are met:
13ca1c9b0cSelric *
14ca1c9b0cSelric * 1. Redistributions of source code must retain the above copyright
15ca1c9b0cSelric * notice, this list of conditions and the following disclaimer.
16ca1c9b0cSelric *
17ca1c9b0cSelric * 2. Redistributions in binary form must reproduce the above copyright
18ca1c9b0cSelric * notice, this list of conditions and the following disclaimer in the
19ca1c9b0cSelric * documentation and/or other materials provided with the distribution.
20ca1c9b0cSelric *
21ca1c9b0cSelric * 3. Neither the name of the Institute nor the names of its contributors
22ca1c9b0cSelric * may be used to endorse or promote products derived from this software
23ca1c9b0cSelric * without specific prior written permission.
24ca1c9b0cSelric *
25ca1c9b0cSelric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26ca1c9b0cSelric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27ca1c9b0cSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ca1c9b0cSelric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29ca1c9b0cSelric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30ca1c9b0cSelric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31ca1c9b0cSelric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32ca1c9b0cSelric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33ca1c9b0cSelric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34ca1c9b0cSelric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35ca1c9b0cSelric * SUCH DAMAGE.
36ca1c9b0cSelric */
37ca1c9b0cSelric
38ca1c9b0cSelric #include "hdb_locl.h"
39ca1c9b0cSelric #include <assert.h>
40ca1c9b0cSelric
41ca1c9b0cSelric typedef struct {
42ca1c9b0cSelric char *path;
43ca1c9b0cSelric krb5_keytab keytab;
44ca1c9b0cSelric } *hdb_keytab;
45ca1c9b0cSelric
46ca1c9b0cSelric /*
47ca1c9b0cSelric *
48ca1c9b0cSelric */
49ca1c9b0cSelric
50ca1c9b0cSelric static krb5_error_code
hkt_close(krb5_context context,HDB * db)51ca1c9b0cSelric hkt_close(krb5_context context, HDB *db)
52ca1c9b0cSelric {
53ca1c9b0cSelric hdb_keytab k = (hdb_keytab)db->hdb_db;
54ca1c9b0cSelric krb5_error_code ret;
55ca1c9b0cSelric
56ca1c9b0cSelric assert(k->keytab);
57ca1c9b0cSelric
58ca1c9b0cSelric ret = krb5_kt_close(context, k->keytab);
59ca1c9b0cSelric k->keytab = NULL;
60ca1c9b0cSelric
61ca1c9b0cSelric return ret;
62ca1c9b0cSelric }
63ca1c9b0cSelric
64ca1c9b0cSelric static krb5_error_code
hkt_destroy(krb5_context context,HDB * db)65ca1c9b0cSelric hkt_destroy(krb5_context context, HDB *db)
66ca1c9b0cSelric {
67ca1c9b0cSelric hdb_keytab k = (hdb_keytab)db->hdb_db;
68ca1c9b0cSelric krb5_error_code ret;
69ca1c9b0cSelric
70ca1c9b0cSelric ret = hdb_clear_master_key (context, db);
71ca1c9b0cSelric
72ca1c9b0cSelric free(k->path);
73ca1c9b0cSelric free(k);
74ca1c9b0cSelric
75ca1c9b0cSelric free(db->hdb_name);
76ca1c9b0cSelric free(db);
77ca1c9b0cSelric return ret;
78ca1c9b0cSelric }
79ca1c9b0cSelric
80ca1c9b0cSelric static krb5_error_code
hkt_lock(krb5_context context,HDB * db,int operation)81ca1c9b0cSelric hkt_lock(krb5_context context, HDB *db, int operation)
82ca1c9b0cSelric {
83ca1c9b0cSelric return 0;
84ca1c9b0cSelric }
85ca1c9b0cSelric
86ca1c9b0cSelric static krb5_error_code
hkt_unlock(krb5_context context,HDB * db)87ca1c9b0cSelric hkt_unlock(krb5_context context, HDB *db)
88ca1c9b0cSelric {
89ca1c9b0cSelric return 0;
90ca1c9b0cSelric }
91ca1c9b0cSelric
92ca1c9b0cSelric static krb5_error_code
hkt_firstkey(krb5_context context,HDB * db,unsigned flags,hdb_entry_ex * entry)93ca1c9b0cSelric hkt_firstkey(krb5_context context, HDB *db,
94ca1c9b0cSelric unsigned flags, hdb_entry_ex *entry)
95ca1c9b0cSelric {
96ca1c9b0cSelric return HDB_ERR_DB_INUSE;
97ca1c9b0cSelric }
98ca1c9b0cSelric
99ca1c9b0cSelric static krb5_error_code
hkt_nextkey(krb5_context context,HDB * db,unsigned flags,hdb_entry_ex * entry)100ca1c9b0cSelric hkt_nextkey(krb5_context context, HDB * db, unsigned flags,
101ca1c9b0cSelric hdb_entry_ex * entry)
102ca1c9b0cSelric {
103ca1c9b0cSelric return HDB_ERR_DB_INUSE;
104ca1c9b0cSelric }
105ca1c9b0cSelric
106ca1c9b0cSelric static krb5_error_code
hkt_open(krb5_context context,HDB * db,int flags,mode_t mode)107ca1c9b0cSelric hkt_open(krb5_context context, HDB * db, int flags, mode_t mode)
108ca1c9b0cSelric {
109ca1c9b0cSelric hdb_keytab k = (hdb_keytab)db->hdb_db;
110ca1c9b0cSelric krb5_error_code ret;
111ca1c9b0cSelric
112ca1c9b0cSelric assert(k->keytab == NULL);
113ca1c9b0cSelric
114ca1c9b0cSelric ret = krb5_kt_resolve(context, k->path, &k->keytab);
115ca1c9b0cSelric if (ret)
116ca1c9b0cSelric return ret;
117ca1c9b0cSelric
118ca1c9b0cSelric return 0;
119ca1c9b0cSelric }
120ca1c9b0cSelric
121ca1c9b0cSelric static krb5_error_code
hkt_fetch_kvno(krb5_context context,HDB * db,krb5_const_principal principal,unsigned flags,krb5_kvno kvno,hdb_entry_ex * entry)122ca1c9b0cSelric hkt_fetch_kvno(krb5_context context, HDB * db, krb5_const_principal principal,
123ca1c9b0cSelric unsigned flags, krb5_kvno kvno, hdb_entry_ex * entry)
124ca1c9b0cSelric {
125ca1c9b0cSelric hdb_keytab k = (hdb_keytab)db->hdb_db;
126ca1c9b0cSelric krb5_error_code ret;
127ca1c9b0cSelric krb5_keytab_entry ktentry;
128ca1c9b0cSelric
129ca1c9b0cSelric if (!(flags & HDB_F_KVNO_SPECIFIED)) {
130ca1c9b0cSelric /* Preserve previous behaviour if no kvno specified */
131ca1c9b0cSelric kvno = 0;
132ca1c9b0cSelric }
133ca1c9b0cSelric
134ca1c9b0cSelric memset(&ktentry, 0, sizeof(ktentry));
135ca1c9b0cSelric
136ca1c9b0cSelric entry->entry.flags.server = 1;
137ca1c9b0cSelric entry->entry.flags.forwardable = 1;
138ca1c9b0cSelric entry->entry.flags.renewable = 1;
139ca1c9b0cSelric
140ca1c9b0cSelric /* Not recorded in the OD backend, make something up */
141ca1c9b0cSelric ret = krb5_parse_name(context, "hdb/keytab@WELL-KNOWN:KEYTAB-BACKEND",
142ca1c9b0cSelric &entry->entry.created_by.principal);
143ca1c9b0cSelric if (ret)
144ca1c9b0cSelric goto out;
145ca1c9b0cSelric
146ca1c9b0cSelric /*
147ca1c9b0cSelric * XXX really needs to try all enctypes and just not pick the
148ca1c9b0cSelric * first one, even if that happens to be des3-cbc-sha1 (ie best
149ca1c9b0cSelric * enctype) in the Apple case. A while loop over all known
150ca1c9b0cSelric * enctypes should work.
151ca1c9b0cSelric */
152ca1c9b0cSelric
153ca1c9b0cSelric ret = krb5_kt_get_entry(context, k->keytab, principal, kvno, 0, &ktentry);
154ca1c9b0cSelric if (ret) {
155ca1c9b0cSelric ret = HDB_ERR_NOENTRY;
156ca1c9b0cSelric goto out;
157ca1c9b0cSelric }
158ca1c9b0cSelric
159ca1c9b0cSelric ret = krb5_copy_principal(context, principal, &entry->entry.principal);
160ca1c9b0cSelric if (ret)
161ca1c9b0cSelric goto out;
162ca1c9b0cSelric
163ca1c9b0cSelric ret = _hdb_keytab2hdb_entry(context, &ktentry, entry);
164ca1c9b0cSelric
165ca1c9b0cSelric out:
166ca1c9b0cSelric if (ret) {
167ca1c9b0cSelric free_hdb_entry(&entry->entry);
168ca1c9b0cSelric memset(&entry->entry, 0, sizeof(entry->entry));
169ca1c9b0cSelric }
170ca1c9b0cSelric krb5_kt_free_entry(context, &ktentry);
171ca1c9b0cSelric
172ca1c9b0cSelric return ret;
173ca1c9b0cSelric }
174ca1c9b0cSelric
175ca1c9b0cSelric static krb5_error_code
hkt_store(krb5_context context,HDB * db,unsigned flags,hdb_entry_ex * entry)176ca1c9b0cSelric hkt_store(krb5_context context, HDB * db, unsigned flags,
177ca1c9b0cSelric hdb_entry_ex * entry)
178ca1c9b0cSelric {
179ca1c9b0cSelric return HDB_ERR_DB_INUSE;
180ca1c9b0cSelric }
181ca1c9b0cSelric
182ca1c9b0cSelric
183ca1c9b0cSelric krb5_error_code
hdb_keytab_create(krb5_context context,HDB ** db,const char * arg)184ca1c9b0cSelric hdb_keytab_create(krb5_context context, HDB ** db, const char *arg)
185ca1c9b0cSelric {
186ca1c9b0cSelric hdb_keytab k;
187ca1c9b0cSelric
188ca1c9b0cSelric *db = calloc(1, sizeof(**db));
189ca1c9b0cSelric if (*db == NULL) {
190ca1c9b0cSelric krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
191ca1c9b0cSelric return ENOMEM;
192ca1c9b0cSelric }
193ca1c9b0cSelric memset(*db, 0, sizeof(**db));
194ca1c9b0cSelric
195ca1c9b0cSelric k = calloc(1, sizeof(*k));
196ca1c9b0cSelric if (k == NULL) {
197ca1c9b0cSelric free(*db);
198ca1c9b0cSelric *db = NULL;
199ca1c9b0cSelric krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
200ca1c9b0cSelric return ENOMEM;
201ca1c9b0cSelric }
202ca1c9b0cSelric
203ca1c9b0cSelric k->path = strdup(arg);
204ca1c9b0cSelric if (k->path == NULL) {
205ca1c9b0cSelric free(k);
206ca1c9b0cSelric free(*db);
207ca1c9b0cSelric *db = NULL;
208ca1c9b0cSelric krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
209ca1c9b0cSelric return ENOMEM;
210ca1c9b0cSelric }
211ca1c9b0cSelric
212ca1c9b0cSelric
213ca1c9b0cSelric (*db)->hdb_db = k;
214ca1c9b0cSelric
215ca1c9b0cSelric (*db)->hdb_master_key_set = 0;
216ca1c9b0cSelric (*db)->hdb_openp = 0;
217ca1c9b0cSelric (*db)->hdb_open = hkt_open;
218ca1c9b0cSelric (*db)->hdb_close = hkt_close;
219ca1c9b0cSelric (*db)->hdb_fetch_kvno = hkt_fetch_kvno;
220ca1c9b0cSelric (*db)->hdb_store = hkt_store;
221ca1c9b0cSelric (*db)->hdb_remove = NULL;
222ca1c9b0cSelric (*db)->hdb_firstkey = hkt_firstkey;
223ca1c9b0cSelric (*db)->hdb_nextkey = hkt_nextkey;
224ca1c9b0cSelric (*db)->hdb_lock = hkt_lock;
225ca1c9b0cSelric (*db)->hdb_unlock = hkt_unlock;
226ca1c9b0cSelric (*db)->hdb_rename = NULL;
227ca1c9b0cSelric (*db)->hdb__get = NULL;
228ca1c9b0cSelric (*db)->hdb__put = NULL;
229ca1c9b0cSelric (*db)->hdb__del = NULL;
230ca1c9b0cSelric (*db)->hdb_destroy = hkt_destroy;
231ca1c9b0cSelric
232ca1c9b0cSelric return 0;
233ca1c9b0cSelric }
234