xref: /minix3/crypto/external/bsd/heimdal/dist/lib/hdb/hdb.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: hdb.c,v 1.3 2014/04/24 13:45:34 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1997 - 2008 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 "krb5_locl.h"
39ebfedea0SLionel Sambuc #include "hdb_locl.h"
40ebfedea0SLionel Sambuc 
41ebfedea0SLionel Sambuc #ifdef HAVE_DLFCN_H
42ebfedea0SLionel Sambuc #include <dlfcn.h>
43ebfedea0SLionel Sambuc #endif
44ebfedea0SLionel Sambuc 
45ebfedea0SLionel Sambuc /*! @mainpage Heimdal database backend library
46ebfedea0SLionel Sambuc  *
47ebfedea0SLionel Sambuc  * @section intro Introduction
48ebfedea0SLionel Sambuc  *
49ebfedea0SLionel Sambuc  * Heimdal libhdb library provides the backend support for Heimdal kdc
50ebfedea0SLionel Sambuc  * and kadmind. Its here where plugins for diffrent database engines
51ebfedea0SLionel Sambuc  * can be pluged in and extend support for here Heimdal get the
52ebfedea0SLionel Sambuc  * principal and policy data from.
53ebfedea0SLionel Sambuc  *
54ebfedea0SLionel Sambuc  * Example of Heimdal backend are:
55ebfedea0SLionel Sambuc  * - Berkeley DB 1.85
56ebfedea0SLionel Sambuc  * - Berkeley DB 3.0
57ebfedea0SLionel Sambuc  * - Berkeley DB 4.0
58ebfedea0SLionel Sambuc  * - New Berkeley DB
59ebfedea0SLionel Sambuc  * - LDAP
60ebfedea0SLionel Sambuc  *
61ebfedea0SLionel Sambuc  *
62ebfedea0SLionel Sambuc  * The project web page: http://www.h5l.org/
63ebfedea0SLionel Sambuc  *
64ebfedea0SLionel Sambuc  */
65ebfedea0SLionel Sambuc 
66ebfedea0SLionel Sambuc const int hdb_interface_version = HDB_INTERFACE_VERSION;
67ebfedea0SLionel Sambuc 
68ebfedea0SLionel Sambuc static struct hdb_method methods[] = {
69ebfedea0SLionel Sambuc #if HAVE_DB1 || HAVE_DB3
70ebfedea0SLionel Sambuc     { HDB_INTERFACE_VERSION, "db:",	hdb_db_create},
71ebfedea0SLionel Sambuc #endif
72ebfedea0SLionel Sambuc #if HAVE_DB1
73ebfedea0SLionel Sambuc     { HDB_INTERFACE_VERSION, "mit-db:",	hdb_mdb_create},
74ebfedea0SLionel Sambuc #endif
75ebfedea0SLionel Sambuc #if HAVE_NDBM
76ebfedea0SLionel Sambuc     { HDB_INTERFACE_VERSION, "ndbm:",	hdb_ndbm_create},
77ebfedea0SLionel Sambuc #endif
78ebfedea0SLionel Sambuc     { HDB_INTERFACE_VERSION, "keytab:",	hdb_keytab_create},
79ebfedea0SLionel Sambuc #if defined(OPENLDAP) && !defined(OPENLDAP_MODULE)
80ebfedea0SLionel Sambuc     { HDB_INTERFACE_VERSION, "ldap:",	hdb_ldap_create},
81ebfedea0SLionel Sambuc     { HDB_INTERFACE_VERSION, "ldapi:",	hdb_ldapi_create},
82ebfedea0SLionel Sambuc #endif
83*0a6a1f1dSLionel Sambuc #ifdef HAVE_SQLITE3
84ebfedea0SLionel Sambuc     { HDB_INTERFACE_VERSION, "sqlite:", hdb_sqlite_create},
85ebfedea0SLionel Sambuc #endif
86ebfedea0SLionel Sambuc     {0, NULL,	NULL}
87ebfedea0SLionel Sambuc };
88ebfedea0SLionel Sambuc 
89ebfedea0SLionel Sambuc #if HAVE_DB1 || HAVE_DB3
90ebfedea0SLionel Sambuc static struct hdb_method dbmetod =
91ebfedea0SLionel Sambuc     { HDB_INTERFACE_VERSION, "", hdb_db_create };
92ebfedea0SLionel Sambuc #elif defined(HAVE_NDBM)
93ebfedea0SLionel Sambuc static struct hdb_method dbmetod =
94ebfedea0SLionel Sambuc     { HDB_INTERFACE_VERSION, "", hdb_ndbm_create };
95ebfedea0SLionel Sambuc #endif
96ebfedea0SLionel Sambuc 
97ebfedea0SLionel Sambuc 
98ebfedea0SLionel Sambuc krb5_error_code
hdb_next_enctype2key(krb5_context context,const hdb_entry * e,krb5_enctype enctype,Key ** key)99ebfedea0SLionel Sambuc hdb_next_enctype2key(krb5_context context,
100ebfedea0SLionel Sambuc 		     const hdb_entry *e,
101ebfedea0SLionel Sambuc 		     krb5_enctype enctype,
102ebfedea0SLionel Sambuc 		     Key **key)
103ebfedea0SLionel Sambuc {
104ebfedea0SLionel Sambuc     Key *k;
105ebfedea0SLionel Sambuc 
106ebfedea0SLionel Sambuc     for (k = *key ? (*key) + 1 : e->keys.val;
107ebfedea0SLionel Sambuc 	 k < e->keys.val + e->keys.len;
108ebfedea0SLionel Sambuc 	 k++)
109ebfedea0SLionel Sambuc     {
110ebfedea0SLionel Sambuc 	if(k->key.keytype == enctype){
111ebfedea0SLionel Sambuc 	    *key = k;
112ebfedea0SLionel Sambuc 	    return 0;
113ebfedea0SLionel Sambuc 	}
114ebfedea0SLionel Sambuc     }
115ebfedea0SLionel Sambuc     krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
116ebfedea0SLionel Sambuc 			   "No next enctype %d for hdb-entry",
117ebfedea0SLionel Sambuc 			  (int)enctype);
118ebfedea0SLionel Sambuc     return KRB5_PROG_ETYPE_NOSUPP; /* XXX */
119ebfedea0SLionel Sambuc }
120ebfedea0SLionel Sambuc 
121ebfedea0SLionel Sambuc krb5_error_code
hdb_enctype2key(krb5_context context,hdb_entry * e,krb5_enctype enctype,Key ** key)122ebfedea0SLionel Sambuc hdb_enctype2key(krb5_context context,
123ebfedea0SLionel Sambuc 		hdb_entry *e,
124ebfedea0SLionel Sambuc 		krb5_enctype enctype,
125ebfedea0SLionel Sambuc 		Key **key)
126ebfedea0SLionel Sambuc {
127ebfedea0SLionel Sambuc     *key = NULL;
128ebfedea0SLionel Sambuc     return hdb_next_enctype2key(context, e, enctype, key);
129ebfedea0SLionel Sambuc }
130ebfedea0SLionel Sambuc 
131ebfedea0SLionel Sambuc void
hdb_free_key(Key * key)132ebfedea0SLionel Sambuc hdb_free_key(Key *key)
133ebfedea0SLionel Sambuc {
134ebfedea0SLionel Sambuc     memset(key->key.keyvalue.data,
135ebfedea0SLionel Sambuc 	   0,
136ebfedea0SLionel Sambuc 	   key->key.keyvalue.length);
137ebfedea0SLionel Sambuc     free_Key(key);
138ebfedea0SLionel Sambuc     free(key);
139ebfedea0SLionel Sambuc }
140ebfedea0SLionel Sambuc 
141ebfedea0SLionel Sambuc 
142ebfedea0SLionel Sambuc krb5_error_code
hdb_lock(int fd,int operation)143ebfedea0SLionel Sambuc hdb_lock(int fd, int operation)
144ebfedea0SLionel Sambuc {
145ebfedea0SLionel Sambuc     int i, code = 0;
146ebfedea0SLionel Sambuc 
147ebfedea0SLionel Sambuc     for(i = 0; i < 3; i++){
148ebfedea0SLionel Sambuc 	code = flock(fd, (operation == HDB_RLOCK ? LOCK_SH : LOCK_EX) | LOCK_NB);
149ebfedea0SLionel Sambuc 	if(code == 0 || errno != EWOULDBLOCK)
150ebfedea0SLionel Sambuc 	    break;
151ebfedea0SLionel Sambuc 	sleep(1);
152ebfedea0SLionel Sambuc     }
153ebfedea0SLionel Sambuc     if(code == 0)
154ebfedea0SLionel Sambuc 	return 0;
155ebfedea0SLionel Sambuc     if(errno == EWOULDBLOCK)
156ebfedea0SLionel Sambuc 	return HDB_ERR_DB_INUSE;
157ebfedea0SLionel Sambuc     return HDB_ERR_CANT_LOCK_DB;
158ebfedea0SLionel Sambuc }
159ebfedea0SLionel Sambuc 
160ebfedea0SLionel Sambuc krb5_error_code
hdb_unlock(int fd)161ebfedea0SLionel Sambuc hdb_unlock(int fd)
162ebfedea0SLionel Sambuc {
163ebfedea0SLionel Sambuc     int code;
164ebfedea0SLionel Sambuc     code = flock(fd, LOCK_UN);
165ebfedea0SLionel Sambuc     if(code)
166ebfedea0SLionel Sambuc 	return 4711 /* XXX */;
167ebfedea0SLionel Sambuc     return 0;
168ebfedea0SLionel Sambuc }
169ebfedea0SLionel Sambuc 
170ebfedea0SLionel Sambuc void
hdb_free_entry(krb5_context context,hdb_entry_ex * ent)171ebfedea0SLionel Sambuc hdb_free_entry(krb5_context context, hdb_entry_ex *ent)
172ebfedea0SLionel Sambuc {
173*0a6a1f1dSLionel Sambuc     size_t i;
174ebfedea0SLionel Sambuc 
175ebfedea0SLionel Sambuc     if (ent->free_entry)
176ebfedea0SLionel Sambuc 	(*ent->free_entry)(context, ent);
177ebfedea0SLionel Sambuc 
178ebfedea0SLionel Sambuc     for(i = 0; i < ent->entry.keys.len; ++i) {
179ebfedea0SLionel Sambuc 	Key *k = &ent->entry.keys.val[i];
180ebfedea0SLionel Sambuc 
181ebfedea0SLionel Sambuc 	memset (k->key.keyvalue.data, 0, k->key.keyvalue.length);
182ebfedea0SLionel Sambuc     }
183ebfedea0SLionel Sambuc     free_hdb_entry(&ent->entry);
184ebfedea0SLionel Sambuc }
185ebfedea0SLionel Sambuc 
186ebfedea0SLionel Sambuc krb5_error_code
hdb_foreach(krb5_context context,HDB * db,unsigned flags,hdb_foreach_func_t func,void * data)187ebfedea0SLionel Sambuc hdb_foreach(krb5_context context,
188ebfedea0SLionel Sambuc 	    HDB *db,
189ebfedea0SLionel Sambuc 	    unsigned flags,
190ebfedea0SLionel Sambuc 	    hdb_foreach_func_t func,
191ebfedea0SLionel Sambuc 	    void *data)
192ebfedea0SLionel Sambuc {
193ebfedea0SLionel Sambuc     krb5_error_code ret;
194ebfedea0SLionel Sambuc     hdb_entry_ex entry;
195ebfedea0SLionel Sambuc     ret = db->hdb_firstkey(context, db, flags, &entry);
196ebfedea0SLionel Sambuc     if (ret == 0)
197ebfedea0SLionel Sambuc 	krb5_clear_error_message(context);
198ebfedea0SLionel Sambuc     while(ret == 0){
199ebfedea0SLionel Sambuc 	ret = (*func)(context, db, &entry, data);
200ebfedea0SLionel Sambuc 	hdb_free_entry(context, &entry);
201ebfedea0SLionel Sambuc 	if(ret == 0)
202ebfedea0SLionel Sambuc 	    ret = db->hdb_nextkey(context, db, flags, &entry);
203ebfedea0SLionel Sambuc     }
204ebfedea0SLionel Sambuc     if(ret == HDB_ERR_NOENTRY)
205ebfedea0SLionel Sambuc 	ret = 0;
206ebfedea0SLionel Sambuc     return ret;
207ebfedea0SLionel Sambuc }
208ebfedea0SLionel Sambuc 
209ebfedea0SLionel Sambuc krb5_error_code
hdb_check_db_format(krb5_context context,HDB * db)210ebfedea0SLionel Sambuc hdb_check_db_format(krb5_context context, HDB *db)
211ebfedea0SLionel Sambuc {
212ebfedea0SLionel Sambuc     krb5_data tag;
213ebfedea0SLionel Sambuc     krb5_data version;
214ebfedea0SLionel Sambuc     krb5_error_code ret, ret2;
215ebfedea0SLionel Sambuc     unsigned ver;
216ebfedea0SLionel Sambuc     int foo;
217ebfedea0SLionel Sambuc 
218ebfedea0SLionel Sambuc     ret = db->hdb_lock(context, db, HDB_RLOCK);
219ebfedea0SLionel Sambuc     if (ret)
220ebfedea0SLionel Sambuc 	return ret;
221ebfedea0SLionel Sambuc 
222*0a6a1f1dSLionel Sambuc     tag.data = (void *)(intptr_t)HDB_DB_FORMAT_ENTRY;
223ebfedea0SLionel Sambuc     tag.length = strlen(tag.data);
224ebfedea0SLionel Sambuc     ret = (*db->hdb__get)(context, db, tag, &version);
225ebfedea0SLionel Sambuc     ret2 = db->hdb_unlock(context, db);
226ebfedea0SLionel Sambuc     if(ret)
227ebfedea0SLionel Sambuc 	return ret;
228ebfedea0SLionel Sambuc     if (ret2)
229ebfedea0SLionel Sambuc 	return ret2;
230ebfedea0SLionel Sambuc     foo = sscanf(version.data, "%u", &ver);
231ebfedea0SLionel Sambuc     krb5_data_free (&version);
232ebfedea0SLionel Sambuc     if (foo != 1)
233ebfedea0SLionel Sambuc 	return HDB_ERR_BADVERSION;
234ebfedea0SLionel Sambuc     if(ver != HDB_DB_FORMAT)
235ebfedea0SLionel Sambuc 	return HDB_ERR_BADVERSION;
236ebfedea0SLionel Sambuc     return 0;
237ebfedea0SLionel Sambuc }
238ebfedea0SLionel Sambuc 
239ebfedea0SLionel Sambuc krb5_error_code
hdb_init_db(krb5_context context,HDB * db)240ebfedea0SLionel Sambuc hdb_init_db(krb5_context context, HDB *db)
241ebfedea0SLionel Sambuc {
242ebfedea0SLionel Sambuc     krb5_error_code ret, ret2;
243ebfedea0SLionel Sambuc     krb5_data tag;
244ebfedea0SLionel Sambuc     krb5_data version;
245ebfedea0SLionel Sambuc     char ver[32];
246ebfedea0SLionel Sambuc 
247ebfedea0SLionel Sambuc     ret = hdb_check_db_format(context, db);
248ebfedea0SLionel Sambuc     if(ret != HDB_ERR_NOENTRY)
249ebfedea0SLionel Sambuc 	return ret;
250ebfedea0SLionel Sambuc 
251ebfedea0SLionel Sambuc     ret = db->hdb_lock(context, db, HDB_WLOCK);
252ebfedea0SLionel Sambuc     if (ret)
253ebfedea0SLionel Sambuc 	return ret;
254ebfedea0SLionel Sambuc 
255*0a6a1f1dSLionel Sambuc     tag.data = (void *)(intptr_t)HDB_DB_FORMAT_ENTRY;
256ebfedea0SLionel Sambuc     tag.length = strlen(tag.data);
257ebfedea0SLionel Sambuc     snprintf(ver, sizeof(ver), "%u", HDB_DB_FORMAT);
258ebfedea0SLionel Sambuc     version.data = ver;
259ebfedea0SLionel Sambuc     version.length = strlen(version.data) + 1; /* zero terminated */
260ebfedea0SLionel Sambuc     ret = (*db->hdb__put)(context, db, 0, tag, version);
261ebfedea0SLionel Sambuc     ret2 = db->hdb_unlock(context, db);
262ebfedea0SLionel Sambuc     if (ret) {
263ebfedea0SLionel Sambuc 	if (ret2)
264ebfedea0SLionel Sambuc 	    krb5_clear_error_message(context);
265ebfedea0SLionel Sambuc 	return ret;
266ebfedea0SLionel Sambuc     }
267ebfedea0SLionel Sambuc     return ret2;
268ebfedea0SLionel Sambuc }
269ebfedea0SLionel Sambuc 
270ebfedea0SLionel Sambuc #ifdef HAVE_DLOPEN
271ebfedea0SLionel Sambuc 
272ebfedea0SLionel Sambuc  /*
273ebfedea0SLionel Sambuc  * Load a dynamic backend from /usr/heimdal/lib/hdb_NAME.so,
274ebfedea0SLionel Sambuc  * looking for the hdb_NAME_create symbol.
275ebfedea0SLionel Sambuc  */
276ebfedea0SLionel Sambuc 
277ebfedea0SLionel Sambuc static const struct hdb_method *
find_dynamic_method(krb5_context context,const char * filename,const char ** rest)278ebfedea0SLionel Sambuc find_dynamic_method (krb5_context context,
279ebfedea0SLionel Sambuc 		     const char *filename,
280ebfedea0SLionel Sambuc 		     const char **rest)
281ebfedea0SLionel Sambuc {
282ebfedea0SLionel Sambuc     static struct hdb_method method;
283ebfedea0SLionel Sambuc     struct hdb_so_method *mso;
284ebfedea0SLionel Sambuc     char *prefix, *path, *symbol;
285ebfedea0SLionel Sambuc     const char *p;
286ebfedea0SLionel Sambuc     void *dl;
287ebfedea0SLionel Sambuc     size_t len;
288ebfedea0SLionel Sambuc 
289ebfedea0SLionel Sambuc     p = strchr(filename, ':');
290ebfedea0SLionel Sambuc 
291ebfedea0SLionel Sambuc     /* if no prefix, don't know what module to load, just ignore it */
292ebfedea0SLionel Sambuc     if (p == NULL)
293ebfedea0SLionel Sambuc 	return NULL;
294ebfedea0SLionel Sambuc 
295ebfedea0SLionel Sambuc     len = p - filename;
296ebfedea0SLionel Sambuc     *rest = filename + len + 1;
297ebfedea0SLionel Sambuc 
298ebfedea0SLionel Sambuc     prefix = malloc(len + 1);
299ebfedea0SLionel Sambuc     if (prefix == NULL)
300ebfedea0SLionel Sambuc 	krb5_errx(context, 1, "out of memory");
301ebfedea0SLionel Sambuc     strlcpy(prefix, filename, len + 1);
302ebfedea0SLionel Sambuc 
303ebfedea0SLionel Sambuc     if (asprintf(&path, LIBDIR "/hdb_%s.so", prefix) == -1)
304ebfedea0SLionel Sambuc 	krb5_errx(context, 1, "out of memory");
305ebfedea0SLionel Sambuc 
306ebfedea0SLionel Sambuc #ifndef RTLD_NOW
307ebfedea0SLionel Sambuc #define RTLD_NOW 0
308ebfedea0SLionel Sambuc #endif
309ebfedea0SLionel Sambuc #ifndef RTLD_GLOBAL
310ebfedea0SLionel Sambuc #define RTLD_GLOBAL 0
311ebfedea0SLionel Sambuc #endif
312ebfedea0SLionel Sambuc 
313ebfedea0SLionel Sambuc     dl = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
314ebfedea0SLionel Sambuc     if (dl == NULL) {
315ebfedea0SLionel Sambuc 	krb5_warnx(context, "error trying to load dynamic module %s: %s\n",
316ebfedea0SLionel Sambuc 		   path, dlerror());
317ebfedea0SLionel Sambuc 	free(prefix);
318ebfedea0SLionel Sambuc 	free(path);
319ebfedea0SLionel Sambuc 	return NULL;
320ebfedea0SLionel Sambuc     }
321ebfedea0SLionel Sambuc 
322ebfedea0SLionel Sambuc     if (asprintf(&symbol, "hdb_%s_interface", prefix) == -1)
323ebfedea0SLionel Sambuc 	krb5_errx(context, 1, "out of memory");
324ebfedea0SLionel Sambuc 
325ebfedea0SLionel Sambuc     mso = (struct hdb_so_method *) dlsym(dl, symbol);
326ebfedea0SLionel Sambuc     if (mso == NULL) {
327ebfedea0SLionel Sambuc 	krb5_warnx(context, "error finding symbol %s in %s: %s\n",
328ebfedea0SLionel Sambuc 		   symbol, path, dlerror());
329ebfedea0SLionel Sambuc 	dlclose(dl);
330ebfedea0SLionel Sambuc 	free(symbol);
331ebfedea0SLionel Sambuc 	free(prefix);
332ebfedea0SLionel Sambuc 	free(path);
333ebfedea0SLionel Sambuc 	return NULL;
334ebfedea0SLionel Sambuc     }
335ebfedea0SLionel Sambuc     free(path);
336ebfedea0SLionel Sambuc     free(symbol);
337ebfedea0SLionel Sambuc 
338ebfedea0SLionel Sambuc     if (mso->version != HDB_INTERFACE_VERSION) {
339ebfedea0SLionel Sambuc 	krb5_warnx(context,
340ebfedea0SLionel Sambuc 		   "error wrong version in shared module %s "
341ebfedea0SLionel Sambuc 		   "version: %d should have been %d\n",
342ebfedea0SLionel Sambuc 		   prefix, mso->version, HDB_INTERFACE_VERSION);
343ebfedea0SLionel Sambuc 	dlclose(dl);
344ebfedea0SLionel Sambuc 	free(prefix);
345ebfedea0SLionel Sambuc 	return NULL;
346ebfedea0SLionel Sambuc     }
347ebfedea0SLionel Sambuc 
348ebfedea0SLionel Sambuc     if (mso->create == NULL) {
349ebfedea0SLionel Sambuc 	krb5_errx(context, 1,
350ebfedea0SLionel Sambuc 		  "no entry point function in shared mod %s ",
351ebfedea0SLionel Sambuc 		   prefix);
352ebfedea0SLionel Sambuc 	dlclose(dl);
353ebfedea0SLionel Sambuc 	free(prefix);
354ebfedea0SLionel Sambuc 	return NULL;
355ebfedea0SLionel Sambuc     }
356ebfedea0SLionel Sambuc 
357ebfedea0SLionel Sambuc     method.create = mso->create;
358ebfedea0SLionel Sambuc     method.prefix = prefix;
359ebfedea0SLionel Sambuc 
360ebfedea0SLionel Sambuc     return &method;
361ebfedea0SLionel Sambuc }
362ebfedea0SLionel Sambuc #endif /* HAVE_DLOPEN */
363ebfedea0SLionel Sambuc 
364ebfedea0SLionel Sambuc /*
365ebfedea0SLionel Sambuc  * find the relevant method for `filename', returning a pointer to the
366ebfedea0SLionel Sambuc  * rest in `rest'.
367ebfedea0SLionel Sambuc  * return NULL if there's no such method.
368ebfedea0SLionel Sambuc  */
369ebfedea0SLionel Sambuc 
370ebfedea0SLionel Sambuc static const struct hdb_method *
find_method(const char * filename,const char ** rest)371ebfedea0SLionel Sambuc find_method (const char *filename, const char **rest)
372ebfedea0SLionel Sambuc {
373ebfedea0SLionel Sambuc     const struct hdb_method *h;
374ebfedea0SLionel Sambuc 
375ebfedea0SLionel Sambuc     for (h = methods; h->prefix != NULL; ++h) {
376ebfedea0SLionel Sambuc 	if (strncmp (filename, h->prefix, strlen(h->prefix)) == 0) {
377ebfedea0SLionel Sambuc 	    *rest = filename + strlen(h->prefix);
378ebfedea0SLionel Sambuc 	    return h;
379ebfedea0SLionel Sambuc 	}
380ebfedea0SLionel Sambuc     }
381ebfedea0SLionel Sambuc #if defined(HAVE_DB1) || defined(HAVE_DB3) || defined(HAVE_NDBM)
382ebfedea0SLionel Sambuc     if (strncmp(filename, "/", 1) == 0
383ebfedea0SLionel Sambuc 	|| strncmp(filename, "./", 2) == 0
384ebfedea0SLionel Sambuc 	|| strncmp(filename, "../", 3) == 0)
385ebfedea0SLionel Sambuc     {
386ebfedea0SLionel Sambuc 	*rest = filename;
387ebfedea0SLionel Sambuc 	return &dbmetod;
388ebfedea0SLionel Sambuc     }
389ebfedea0SLionel Sambuc #endif
390ebfedea0SLionel Sambuc 
391ebfedea0SLionel Sambuc     return NULL;
392ebfedea0SLionel Sambuc }
393ebfedea0SLionel Sambuc 
394ebfedea0SLionel Sambuc krb5_error_code
hdb_list_builtin(krb5_context context,char ** list)395ebfedea0SLionel Sambuc hdb_list_builtin(krb5_context context, char **list)
396ebfedea0SLionel Sambuc {
397ebfedea0SLionel Sambuc     const struct hdb_method *h;
398ebfedea0SLionel Sambuc     size_t len = 0;
399ebfedea0SLionel Sambuc     char *buf = NULL;
400ebfedea0SLionel Sambuc 
401ebfedea0SLionel Sambuc     for (h = methods; h->prefix != NULL; ++h) {
402ebfedea0SLionel Sambuc 	if (h->prefix[0] == '\0')
403ebfedea0SLionel Sambuc 	    continue;
404ebfedea0SLionel Sambuc 	len += strlen(h->prefix) + 2;
405ebfedea0SLionel Sambuc     }
406ebfedea0SLionel Sambuc 
407ebfedea0SLionel Sambuc     len += 1;
408ebfedea0SLionel Sambuc     buf = malloc(len);
409ebfedea0SLionel Sambuc     if (buf == NULL) {
410ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
411ebfedea0SLionel Sambuc 	return ENOMEM;
412ebfedea0SLionel Sambuc     }
413ebfedea0SLionel Sambuc     buf[0] = '\0';
414ebfedea0SLionel Sambuc 
415ebfedea0SLionel Sambuc     for (h = methods; h->prefix != NULL; ++h) {
416ebfedea0SLionel Sambuc 	if (h != methods)
417ebfedea0SLionel Sambuc 	    strlcat(buf, ", ", len);
418ebfedea0SLionel Sambuc 	strlcat(buf, h->prefix, len);
419ebfedea0SLionel Sambuc     }
420ebfedea0SLionel Sambuc     *list = buf;
421ebfedea0SLionel Sambuc     return 0;
422ebfedea0SLionel Sambuc }
423ebfedea0SLionel Sambuc 
424ebfedea0SLionel Sambuc krb5_error_code
_hdb_keytab2hdb_entry(krb5_context context,const krb5_keytab_entry * ktentry,hdb_entry_ex * entry)425ebfedea0SLionel Sambuc _hdb_keytab2hdb_entry(krb5_context context,
426ebfedea0SLionel Sambuc 		      const krb5_keytab_entry *ktentry,
427ebfedea0SLionel Sambuc 		      hdb_entry_ex *entry)
428ebfedea0SLionel Sambuc {
429ebfedea0SLionel Sambuc     entry->entry.kvno = ktentry->vno;
430ebfedea0SLionel Sambuc     entry->entry.created_by.time = ktentry->timestamp;
431ebfedea0SLionel Sambuc 
432ebfedea0SLionel Sambuc     entry->entry.keys.val = calloc(1, sizeof(entry->entry.keys.val[0]));
433ebfedea0SLionel Sambuc     if (entry->entry.keys.val == NULL)
434ebfedea0SLionel Sambuc 	return ENOMEM;
435ebfedea0SLionel Sambuc     entry->entry.keys.len = 1;
436ebfedea0SLionel Sambuc 
437ebfedea0SLionel Sambuc     entry->entry.keys.val[0].mkvno = NULL;
438ebfedea0SLionel Sambuc     entry->entry.keys.val[0].salt = NULL;
439ebfedea0SLionel Sambuc 
440ebfedea0SLionel Sambuc     return krb5_copy_keyblock_contents(context,
441ebfedea0SLionel Sambuc 				       &ktentry->keyblock,
442ebfedea0SLionel Sambuc 				       &entry->entry.keys.val[0].key);
443ebfedea0SLionel Sambuc }
444ebfedea0SLionel Sambuc 
445ebfedea0SLionel Sambuc /**
446ebfedea0SLionel Sambuc  * Create a handle for a Kerberos database
447ebfedea0SLionel Sambuc  *
448ebfedea0SLionel Sambuc  * Create a handle for a Kerberos database backend specified by a
449ebfedea0SLionel Sambuc  * filename.  Doesn't create a file if its doesn't exists, you have to
450ebfedea0SLionel Sambuc  * use O_CREAT to tell the backend to create the file.
451ebfedea0SLionel Sambuc  */
452ebfedea0SLionel Sambuc 
453ebfedea0SLionel Sambuc krb5_error_code
hdb_create(krb5_context context,HDB ** db,const char * filename)454ebfedea0SLionel Sambuc hdb_create(krb5_context context, HDB **db, const char *filename)
455ebfedea0SLionel Sambuc {
456ebfedea0SLionel Sambuc     const struct hdb_method *h;
457ebfedea0SLionel Sambuc     const char *residual;
458ebfedea0SLionel Sambuc     krb5_error_code ret;
459ebfedea0SLionel Sambuc     struct krb5_plugin *list = NULL, *e;
460ebfedea0SLionel Sambuc 
461ebfedea0SLionel Sambuc     if(filename == NULL)
462ebfedea0SLionel Sambuc 	filename = HDB_DEFAULT_DB;
463ebfedea0SLionel Sambuc     krb5_add_et_list(context, initialize_hdb_error_table_r);
464ebfedea0SLionel Sambuc     h = find_method (filename, &residual);
465ebfedea0SLionel Sambuc 
466ebfedea0SLionel Sambuc     if (h == NULL) {
467ebfedea0SLionel Sambuc 	    ret = _krb5_plugin_find(context, PLUGIN_TYPE_DATA, "hdb", &list);
468ebfedea0SLionel Sambuc 	    if(ret == 0 && list != NULL) {
469ebfedea0SLionel Sambuc 		    for (e = list; e != NULL; e = _krb5_plugin_get_next(e)) {
470ebfedea0SLionel Sambuc 			    h = _krb5_plugin_get_symbol(e);
471ebfedea0SLionel Sambuc 			    if (strncmp (filename, h->prefix, strlen(h->prefix)) == 0
472ebfedea0SLionel Sambuc 				&& h->interface_version == HDB_INTERFACE_VERSION) {
473ebfedea0SLionel Sambuc 				    residual = filename + strlen(h->prefix);
474ebfedea0SLionel Sambuc 				    break;
475ebfedea0SLionel Sambuc 			    }
476ebfedea0SLionel Sambuc 		    }
477ebfedea0SLionel Sambuc 		    if (e == NULL) {
478ebfedea0SLionel Sambuc 			    h = NULL;
479ebfedea0SLionel Sambuc 			    _krb5_plugin_free(list);
480ebfedea0SLionel Sambuc 		    }
481ebfedea0SLionel Sambuc 	    }
482ebfedea0SLionel Sambuc     }
483ebfedea0SLionel Sambuc 
484ebfedea0SLionel Sambuc #ifdef HAVE_DLOPEN
485ebfedea0SLionel Sambuc     if (h == NULL)
486ebfedea0SLionel Sambuc 	h = find_dynamic_method (context, filename, &residual);
487ebfedea0SLionel Sambuc #endif
488ebfedea0SLionel Sambuc     if (h == NULL)
489ebfedea0SLionel Sambuc 	krb5_errx(context, 1, "No database support for %s", filename);
490ebfedea0SLionel Sambuc     return (*h->create)(context, db, residual);
491ebfedea0SLionel Sambuc }
492