2 * Copyright (c) 1997 - 2007 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
44 krb5_error_code (*create)(krb5_context, HDB **, const char *filename);
47 static struct hdb_method methods[] = {
48 #if HAVE_DB1 || HAVE_DB3
49 {"db:", hdb_db_create},
52 {"ndbm:", hdb_ndbm_create},
54 #if defined(OPENLDAP) && !defined(OPENLDAP_MODULE)
55 {"ldap:", hdb_ldap_create},
56 {"ldapi:", hdb_ldapi_create},
58 #ifdef HAVE_LDB /* Used for integrated samba build */
59 {"ldb:", hdb_ldb_create},
64 #if HAVE_DB1 || HAVE_DB3
65 static struct hdb_method dbmetod = {"", hdb_db_create };
66 #elif defined(HAVE_NDBM)
67 static struct hdb_method dbmetod = {"", hdb_ndbm_create };
72 hdb_next_enctype2key(krb5_context context,
79 for (k = *key ? (*key) + 1 : e->keys.val;
80 k < e->keys.val + e->keys.len;
83 if(k->key.keytype == enctype){
88 krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
89 "No next enctype %d for hdb-entry",
91 return KRB5_PROG_ETYPE_NOSUPP; /* XXX */
95 hdb_enctype2key(krb5_context context,
101 return hdb_next_enctype2key(context, e, enctype, key);
105 hdb_free_key(Key *key)
107 memset(key->key.keyvalue.data,
109 key->key.keyvalue.length);
116 hdb_lock(int fd, int operation)
120 for(i = 0; i < 3; i++){
121 code = flock(fd, (operation == HDB_RLOCK ? LOCK_SH : LOCK_EX) | LOCK_NB);
122 if(code == 0 || errno != EWOULDBLOCK)
128 if(errno == EWOULDBLOCK)
129 return HDB_ERR_DB_INUSE;
130 return HDB_ERR_CANT_LOCK_DB;
137 code = flock(fd, LOCK_UN);
139 return 4711 /* XXX */;
144 hdb_free_entry(krb5_context context, hdb_entry_ex *ent)
149 (*ent->free_entry)(context, ent);
151 for(i = 0; i < ent->entry.keys.len; ++i) {
152 Key *k = &ent->entry.keys.val[i];
154 memset (k->key.keyvalue.data, 0, k->key.keyvalue.length);
156 free_hdb_entry(&ent->entry);
160 hdb_foreach(krb5_context context,
163 hdb_foreach_func_t func,
168 ret = db->hdb_firstkey(context, db, flags, &entry);
170 krb5_clear_error_string(context);
172 ret = (*func)(context, db, &entry, data);
173 hdb_free_entry(context, &entry);
175 ret = db->hdb_nextkey(context, db, flags, &entry);
177 if(ret == HDB_ERR_NOENTRY)
183 hdb_check_db_format(krb5_context context, HDB *db)
187 krb5_error_code ret, ret2;
191 ret = db->hdb_lock(context, db, HDB_RLOCK);
195 tag.data = HDB_DB_FORMAT_ENTRY;
196 tag.length = strlen(tag.data);
197 ret = (*db->hdb__get)(context, db, tag, &version);
198 ret2 = db->hdb_unlock(context, db);
203 foo = sscanf(version.data, "%u", &ver);
204 krb5_data_free (&version);
206 return HDB_ERR_BADVERSION;
207 if(ver != HDB_DB_FORMAT)
208 return HDB_ERR_BADVERSION;
213 hdb_init_db(krb5_context context, HDB *db)
215 krb5_error_code ret, ret2;
220 ret = hdb_check_db_format(context, db);
221 if(ret != HDB_ERR_NOENTRY)
224 ret = db->hdb_lock(context, db, HDB_WLOCK);
228 tag.data = HDB_DB_FORMAT_ENTRY;
229 tag.length = strlen(tag.data);
230 snprintf(ver, sizeof(ver), "%u", HDB_DB_FORMAT);
232 version.length = strlen(version.data) + 1; /* zero terminated */
233 ret = (*db->hdb__put)(context, db, 0, tag, version);
234 ret2 = db->hdb_unlock(context, db);
237 krb5_clear_error_string(context);
246 * Load a dynamic backend from /usr/heimdal/lib/hdb_NAME.so,
247 * looking for the hdb_NAME_create symbol.
250 static const struct hdb_method *
251 find_dynamic_method (krb5_context context,
252 const char *filename,
255 static struct hdb_method method;
256 struct hdb_so_method *mso;
257 char *prefix, *path, *symbol;
262 p = strchr(filename, ':');
264 /* if no prefix, don't know what module to load, just ignore it */
269 *rest = filename + len + 1;
271 prefix = strndup(filename, len);
273 krb5_errx(context, 1, "out of memory");
275 if (asprintf(&path, LIBDIR "/hdb_%s.so", prefix) == -1)
276 krb5_errx(context, 1, "out of memory");
282 #define RTLD_GLOBAL 0
285 dl = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
287 krb5_warnx(context, "error trying to load dynamic module %s: %s\n",
294 if (asprintf(&symbol, "hdb_%s_interface", prefix) == -1)
295 krb5_errx(context, 1, "out of memory");
297 mso = dlsym(dl, symbol);
299 krb5_warnx(context, "error finding symbol %s in %s: %s\n",
300 symbol, path, dlerror());
310 if (mso->version != HDB_INTERFACE_VERSION) {
312 "error wrong version in shared module %s "
313 "version: %d should have been %d\n",
314 prefix, mso->version, HDB_INTERFACE_VERSION);
320 if (mso->create == NULL) {
321 krb5_errx(context, 1,
322 "no entry point function in shared mod %s ",
329 method.create = mso->create;
330 method.prefix = prefix;
334 #endif /* HAVE_DLOPEN */
337 * find the relevant method for `filename', returning a pointer to the
339 * return NULL if there's no such method.
342 static const struct hdb_method *
343 find_method (const char *filename, const char **rest)
345 const struct hdb_method *h;
347 for (h = methods; h->prefix != NULL; ++h) {
348 if (strncmp (filename, h->prefix, strlen(h->prefix)) == 0) {
349 *rest = filename + strlen(h->prefix);
353 #if defined(HAVE_DB1) || defined(HAVE_DB3) || defined(HAVE_NDBM)
354 if (strncmp(filename, "/", 1) == 0
355 || strncmp(filename, "./", 2) == 0
356 || strncmp(filename, "../", 3) == 0)
367 hdb_list_builtin(krb5_context context, char **list)
369 const struct hdb_method *h;
373 for (h = methods; h->prefix != NULL; ++h) {
374 if (h->prefix[0] == '\0')
376 len += strlen(h->prefix) + 2;
382 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
387 for (h = methods; h->prefix != NULL; ++h) {
389 strlcat(buf, ", ", len);
390 strlcat(buf, h->prefix, len);
397 hdb_create(krb5_context context, HDB **db, const char *filename)
399 const struct hdb_method *h;
400 const char *residual;
403 filename = HDB_DEFAULT_DB;
404 krb5_add_et_list(context, initialize_hdb_error_table_r);
405 h = find_method (filename, &residual);
408 h = find_dynamic_method (context, filename, &residual);
411 krb5_errx(context, 1, "No database support for %s", filename);
412 return (*h->create)(context, db, residual);