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