2 * Copyright (c) 1997 - 2001 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
36 RCSID("$Id: hdb.c,v 1.44 2001/08/09 08:41:48 assar Exp $");
40 krb5_error_code (*create)(krb5_context, HDB **, const char *filename);
43 static struct hdb_method methods[] = {
44 #if HAVE_DB1 || HAVE_DB3
45 {"db:", hdb_db_create},
48 {"ndbm:", hdb_ndbm_create},
51 {"ldap:", hdb_ldap_create},
53 #if HAVE_DB1 || HAVE_DB3
55 #elif defined(HAVE_NDBM)
56 {"", hdb_ndbm_create},
57 #elif defined(OPENLDAP)
58 {"", hdb_ldap_create},
64 hdb_next_enctype2key(krb5_context context,
71 for (k = *key ? (*key) + 1 : e->keys.val;
72 k < e->keys.val + e->keys.len;
74 if(k->key.keytype == enctype){
78 return KRB5_PROG_ETYPE_NOSUPP; /* XXX */
82 hdb_enctype2key(krb5_context context,
88 return hdb_next_enctype2key(context, e, enctype, key);
92 hdb_free_key(Key *key)
94 memset(key->key.keyvalue.data,
96 key->key.keyvalue.length);
103 hdb_lock(int fd, int operation)
107 for(i = 0; i < 3; i++){
108 code = flock(fd, (operation == HDB_RLOCK ? LOCK_SH : LOCK_EX) | LOCK_NB);
109 if(code == 0 || errno != EWOULDBLOCK)
115 if(errno == EWOULDBLOCK)
116 return HDB_ERR_DB_INUSE;
117 return HDB_ERR_CANT_LOCK_DB;
124 code = flock(fd, LOCK_UN);
126 return 4711 /* XXX */;
131 hdb_free_entry(krb5_context context, hdb_entry *ent)
135 for(i = 0; i < ent->keys.len; ++i) {
136 Key *k = &ent->keys.val[i];
138 memset (k->key.keyvalue.data, 0, k->key.keyvalue.length);
144 hdb_foreach(krb5_context context,
147 hdb_foreach_func_t func,
152 ret = db->firstkey(context, db, flags, &entry);
154 ret = (*func)(context, db, &entry, data);
155 hdb_free_entry(context, &entry);
157 ret = db->nextkey(context, db, flags, &entry);
159 if(ret == HDB_ERR_NOENTRY)
165 hdb_check_db_format(krb5_context context, HDB *db)
173 tag.data = HDB_DB_FORMAT_ENTRY;
174 tag.length = strlen(tag.data);
175 ret = (*db->_get)(context, db, tag, &version);
178 foo = sscanf(version.data, "%u", &ver);
179 krb5_data_free (&version);
181 return HDB_ERR_BADVERSION;
182 if(ver != HDB_DB_FORMAT)
183 return HDB_ERR_BADVERSION;
188 hdb_init_db(krb5_context context, HDB *db)
195 ret = hdb_check_db_format(context, db);
196 if(ret != HDB_ERR_NOENTRY)
199 tag.data = HDB_DB_FORMAT_ENTRY;
200 tag.length = strlen(tag.data);
201 snprintf(ver, sizeof(ver), "%u", HDB_DB_FORMAT);
203 version.length = strlen(version.data) + 1; /* zero terminated */
204 ret = (*db->_put)(context, db, 0, tag, version);
209 * find the relevant method for `filename', returning a pointer to the
211 * return NULL if there's no such method.
214 static const struct hdb_method *
215 find_method (const char *filename, const char **rest)
217 const struct hdb_method *h;
219 for (h = methods; h->prefix != NULL; ++h)
220 if (strncmp (filename, h->prefix, strlen(h->prefix)) == 0) {
221 *rest = filename + strlen(h->prefix);
228 hdb_create(krb5_context context, HDB **db, const char *filename)
230 const struct hdb_method *h;
231 const char *residual;
234 filename = HDB_DEFAULT_DB;
235 krb5_add_et_list(context, initialize_hdb_error_table_r);
236 h = find_method (filename, &residual);
238 krb5_errx(context, 1, "No database support! (hdb_create)");
239 return (*h->create)(context, db, residual);