2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
34 #include "krb5_locl.h"
42 static struct hdb_method methods[] = {
43 #if HAVE_DB1 || HAVE_DB3
44 { HDB_INTERFACE_VERSION, "db:", hdb_db_create},
47 { HDB_INTERFACE_VERSION, "ndbm:", hdb_ndbm_create},
49 #if defined(OPENLDAP) && !defined(OPENLDAP_MODULE)
50 { HDB_INTERFACE_VERSION, "ldap:", hdb_ldap_create},
51 { HDB_INTERFACE_VERSION, "ldapi:", hdb_ldapi_create},
56 #if HAVE_DB1 || HAVE_DB3
57 static struct hdb_method dbmetod =
58 { HDB_INTERFACE_VERSION, "", hdb_db_create };
59 #elif defined(HAVE_NDBM)
60 static struct hdb_method dbmetod =
61 { HDB_INTERFACE_VERSION, "", hdb_ndbm_create };
66 hdb_next_enctype2key(krb5_context context,
73 for (k = *key ? (*key) + 1 : e->keys.val;
74 k < e->keys.val + e->keys.len;
77 if(k->key.keytype == enctype){
82 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
83 "No next enctype %d for hdb-entry",
85 return KRB5_PROG_ETYPE_NOSUPP; /* XXX */
89 hdb_enctype2key(krb5_context context,
95 return hdb_next_enctype2key(context, e, enctype, key);
99 hdb_free_key(Key *key)
101 memset(key->key.keyvalue.data,
103 key->key.keyvalue.length);
110 hdb_lock(int fd, int operation)
114 for(i = 0; i < 3; i++){
115 code = flock(fd, (operation == HDB_RLOCK ? LOCK_SH : LOCK_EX) | LOCK_NB);
116 if(code == 0 || errno != EWOULDBLOCK)
122 if(errno == EWOULDBLOCK)
123 return HDB_ERR_DB_INUSE;
124 return HDB_ERR_CANT_LOCK_DB;
131 code = flock(fd, LOCK_UN);
133 return 4711 /* XXX */;
138 hdb_free_entry(krb5_context context, hdb_entry_ex *ent)
143 (*ent->free_entry)(context, ent);
145 for(i = 0; i < ent->entry.keys.len; ++i) {
146 Key *k = &ent->entry.keys.val[i];
148 memset (k->key.keyvalue.data, 0, k->key.keyvalue.length);
150 free_hdb_entry(&ent->entry);
154 hdb_foreach(krb5_context context,
157 hdb_foreach_func_t func,
162 ret = db->hdb_firstkey(context, db, flags, &entry);
164 krb5_clear_error_message(context);
166 ret = (*func)(context, db, &entry, data);
167 hdb_free_entry(context, &entry);
169 ret = db->hdb_nextkey(context, db, flags, &entry);
171 if(ret == HDB_ERR_NOENTRY)
177 hdb_check_db_format(krb5_context context, HDB *db)
181 krb5_error_code ret, ret2;
185 ret = db->hdb_lock(context, db, HDB_RLOCK);
189 tag.data = HDB_DB_FORMAT_ENTRY;
190 tag.length = strlen(tag.data);
191 ret = (*db->hdb__get)(context, db, tag, &version);
192 ret2 = db->hdb_unlock(context, db);
197 foo = sscanf(version.data, "%u", &ver);
198 krb5_data_free (&version);
200 return HDB_ERR_BADVERSION;
201 if(ver != HDB_DB_FORMAT)
202 return HDB_ERR_BADVERSION;
207 hdb_init_db(krb5_context context, HDB *db)
209 krb5_error_code ret, ret2;
214 ret = hdb_check_db_format(context, db);
215 if(ret != HDB_ERR_NOENTRY)
218 ret = db->hdb_lock(context, db, HDB_WLOCK);
222 tag.data = HDB_DB_FORMAT_ENTRY;
223 tag.length = strlen(tag.data);
224 snprintf(ver, sizeof(ver), "%u", HDB_DB_FORMAT);
226 version.length = strlen(version.data) + 1; /* zero terminated */
227 ret = (*db->hdb__put)(context, db, 0, tag, version);
228 ret2 = db->hdb_unlock(context, db);
231 krb5_clear_error_message(context);
240 * Load a dynamic backend from /usr/heimdal/lib/hdb_NAME.so,
241 * looking for the hdb_NAME_create symbol.
244 static const struct hdb_method *
245 find_dynamic_method (krb5_context context,
246 const char *filename,
249 static struct hdb_method method;
250 struct hdb_so_method *mso;
251 char *prefix, *path, *symbol;
256 p = strchr(filename, ':');
258 /* if no prefix, don't know what module to load, just ignore it */
263 *rest = filename + len + 1;
265 prefix = malloc(len + 1);
267 krb5_errx(context, 1, "out of memory");
268 strlcpy(prefix, filename, len + 1);
270 if (asprintf(&path, LIBDIR "/hdb_%s.so", prefix) == -1)
271 krb5_errx(context, 1, "out of memory");
277 #define RTLD_GLOBAL 0
280 dl = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
282 krb5_warnx(context, "error trying to load dynamic module %s: %s\n",
289 if (asprintf(&symbol, "hdb_%s_interface", prefix) == -1)
290 krb5_errx(context, 1, "out of memory");
292 mso = dlsym(dl, symbol);
294 krb5_warnx(context, "error finding symbol %s in %s: %s\n",
295 symbol, path, dlerror());
305 if (mso->version != HDB_INTERFACE_VERSION) {
307 "error wrong version in shared module %s "
308 "version: %d should have been %d\n",
309 prefix, mso->version, HDB_INTERFACE_VERSION);
315 if (mso->create == NULL) {
316 krb5_errx(context, 1,
317 "no entry point function in shared mod %s ",
324 method.create = mso->create;
325 method.prefix = prefix;
329 #endif /* HAVE_DLOPEN */
332 * find the relevant method for `filename', returning a pointer to the
334 * return NULL if there's no such method.
337 static const struct hdb_method *
338 find_method (const char *filename, const char **rest)
340 const struct hdb_method *h;
342 for (h = methods; h->prefix != NULL; ++h) {
343 if (strncmp (filename, h->prefix, strlen(h->prefix)) == 0) {
344 *rest = filename + strlen(h->prefix);
348 #if defined(HAVE_DB1) || defined(HAVE_DB3) || defined(HAVE_NDBM)
349 if (strncmp(filename, "/", 1) == 0
350 || strncmp(filename, "./", 2) == 0
351 || strncmp(filename, "../", 3) == 0)
362 hdb_list_builtin(krb5_context context, char **list)
364 const struct hdb_method *h;
368 for (h = methods; h->prefix != NULL; ++h) {
369 if (h->prefix[0] == '\0')
371 len += strlen(h->prefix) + 2;
377 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
382 for (h = methods; h->prefix != NULL; ++h) {
384 strlcat(buf, ", ", len);
385 strlcat(buf, h->prefix, len);
392 hdb_create(krb5_context context, HDB **db, const char *filename)
394 const struct hdb_method *h;
395 const char *residual;
397 struct krb5_plugin *list = NULL, *e;
400 filename = HDB_DEFAULT_DB;
401 krb5_add_et_list(context, initialize_hdb_error_table_r);
402 h = find_method (filename, &residual);
405 ret = _krb5_plugin_find(context, PLUGIN_TYPE_DATA, "hdb", &list);
406 if(ret == 0 && list != NULL) {
407 for (e = list; e != NULL; e = _krb5_plugin_get_next(e)) {
408 h = _krb5_plugin_get_symbol(e);
409 if (strncmp (filename, h->prefix, strlen(h->prefix)) == 0
410 && h->interface_version == HDB_INTERFACE_VERSION) {
411 residual = filename + strlen(h->prefix);
417 _krb5_plugin_free(list);
424 h = find_dynamic_method (context, filename, &residual);
427 krb5_errx(context, 1, "No database support for %s", filename);
428 return (*h->create)(context, db, residual);