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
38 #if defined(HAVE_GDBM_NDBM_H)
39 #include <gdbm/ndbm.h>
40 #elif defined(HAVE_NDBM_H)
42 #elif defined(HAVE_DBM_H)
51 static krb5_error_code
52 NDBM_destroy(krb5_context context, HDB *db)
54 hdb_clear_master_key (context, db);
60 static krb5_error_code
61 NDBM_lock(krb5_context context, HDB *db, int operation)
63 struct ndbm_db *d = db->hdb_db;
64 return hdb_lock(d->lock_fd, operation);
67 static krb5_error_code
68 NDBM_unlock(krb5_context context, HDB *db)
70 struct ndbm_db *d = db->hdb_db;
71 return hdb_unlock(d->lock_fd);
74 static krb5_error_code
75 NDBM_seq(krb5_context context, HDB *db,
76 unsigned flags, hdb_entry_ex *entry, int first)
79 struct ndbm_db *d = (struct ndbm_db *)db->hdb_db;
81 krb5_data key_data, data;
82 krb5_error_code ret = 0;
85 key = dbm_firstkey(d->db);
87 key = dbm_nextkey(d->db);
89 return HDB_ERR_NOENTRY;
90 key_data.data = key.dptr;
91 key_data.length = key.dsize;
92 ret = db->hdb_lock(context, db, HDB_RLOCK);
94 value = dbm_fetch(d->db, key);
95 db->hdb_unlock(context, db);
96 data.data = value.dptr;
97 data.length = value.dsize;
98 memset(entry, 0, sizeof(*entry));
99 if(hdb_value2entry(context, &data, &entry->entry))
100 return NDBM_seq(context, db, flags, entry, 0);
101 if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) {
102 ret = hdb_unseal_keys (context, db, &entry->entry);
104 hdb_free_entry (context, entry);
106 if (ret == 0 && entry->entry.principal == NULL) {
107 entry->entry.principal = malloc (sizeof(*entry->entry.principal));
108 if (entry->entry.principal == NULL) {
109 hdb_free_entry (context, entry);
111 krb5_set_error_message(context, ret, "malloc: out of memory");
113 hdb_key2principal (context, &key_data, entry->entry.principal);
120 static krb5_error_code
121 NDBM_firstkey(krb5_context context, HDB *db,unsigned flags,hdb_entry_ex *entry)
123 return NDBM_seq(context, db, flags, entry, 1);
127 static krb5_error_code
128 NDBM_nextkey(krb5_context context, HDB *db, unsigned flags,hdb_entry_ex *entry)
130 return NDBM_seq(context, db, flags, entry, 0);
133 static krb5_error_code
134 NDBM_rename(krb5_context context, HDB *db, const char *new_name)
136 /* XXX this function will break */
137 struct ndbm_db *d = db->hdb_db;
140 char *old_dir, *old_pag, *new_dir, *new_pag;
144 /* lock old and new databases */
145 ret = db->hdb_lock(context, db, HDB_WLOCK);
148 asprintf(&new_lock, "%s.lock", new_name);
149 if(new_lock == NULL) {
150 db->hdb_unlock(context, db);
151 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
154 lock_fd = open(new_lock, O_RDWR | O_CREAT, 0600);
157 db->hdb_unlock(context, db);
158 krb5_set_error_message(context, ret, "open(%s): %s", new_lock,
164 ret = hdb_lock(lock_fd, HDB_WLOCK);
166 db->hdb_unlock(context, db);
171 asprintf(&old_dir, "%s.dir", db->hdb_name);
172 asprintf(&old_pag, "%s.pag", db->hdb_name);
173 asprintf(&new_dir, "%s.dir", new_name);
174 asprintf(&new_pag, "%s.pag", new_name);
176 ret = rename(old_dir, new_dir) || rename(old_pag, new_pag);
182 db->hdb_unlock(context, db);
187 krb5_set_error_message(context, ret, "rename: %s", strerror(ret));
192 d->lock_fd = lock_fd;
195 db->hdb_name = strdup(new_name);
199 static krb5_error_code
200 NDBM__get(krb5_context context, HDB *db, krb5_data key, krb5_data *reply)
202 struct ndbm_db *d = (struct ndbm_db *)db->hdb_db;
207 k.dsize = key.length;
208 code = db->hdb_lock(context, db, HDB_RLOCK);
211 v = dbm_fetch(d->db, k);
212 db->hdb_unlock(context, db);
214 return HDB_ERR_NOENTRY;
216 krb5_data_copy(reply, v.dptr, v.dsize);
220 static krb5_error_code
221 NDBM__put(krb5_context context, HDB *db, int replace,
222 krb5_data key, krb5_data value)
224 struct ndbm_db *d = (struct ndbm_db *)db->hdb_db;
229 k.dsize = key.length;
231 v.dsize = value.length;
233 code = db->hdb_lock(context, db, HDB_WLOCK);
236 code = dbm_store(d->db, k, v, replace ? DBM_REPLACE : DBM_INSERT);
237 db->hdb_unlock(context, db);
239 return HDB_ERR_EXISTS;
245 static krb5_error_code
246 NDBM__del(krb5_context context, HDB *db, krb5_data key)
248 struct ndbm_db *d = (struct ndbm_db *)db->hdb_db;
254 k.dsize = key.length;
255 ret = db->hdb_lock(context, db, HDB_WLOCK);
257 code = dbm_delete(d->db, k);
258 db->hdb_unlock(context, db);
265 static krb5_error_code
266 NDBM_close(krb5_context context, HDB *db)
268 struct ndbm_db *d = db->hdb_db;
275 static krb5_error_code
276 NDBM_open(krb5_context context, HDB *db, int flags, mode_t mode)
279 struct ndbm_db *d = malloc(sizeof(*d));
283 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
286 asprintf(&lock_file, "%s.lock", (char*)db->hdb_name);
287 if(lock_file == NULL) {
289 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
292 d->db = dbm_open((char*)db->hdb_name, flags, mode);
297 krb5_set_error_message(context, ret, "dbm_open(%s): %s", db->hdb_name,
301 d->lock_fd = open(lock_file, O_RDWR | O_CREAT, 0600);
306 krb5_set_error_message(context, ret, "open(%s): %s", lock_file,
313 if((flags & O_ACCMODE) == O_RDONLY)
314 ret = hdb_check_db_format(context, db);
316 ret = hdb_init_db(context, db);
317 if(ret == HDB_ERR_NOENTRY)
320 NDBM_close(context, db);
321 krb5_set_error_message(context, ret, "hdb_open: failed %s database %s",
322 (flags & O_ACCMODE) == O_RDONLY ?
323 "checking format of" : "initialize",
330 hdb_ndbm_create(krb5_context context, HDB **db,
331 const char *filename)
333 *db = calloc(1, sizeof(**db));
335 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
339 (*db)->hdb_db = NULL;
340 (*db)->hdb_name = strdup(filename);
341 if ((*db)->hdb_name == NULL) {
344 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
347 (*db)->hdb_master_key_set = 0;
348 (*db)->hdb_openp = 0;
349 (*db)->hdb_capability_flags = HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL;
350 (*db)->hdb_open = NDBM_open;
351 (*db)->hdb_close = NDBM_close;
352 (*db)->hdb_fetch = _hdb_fetch;
353 (*db)->hdb_store = _hdb_store;
354 (*db)->hdb_remove = _hdb_remove;
355 (*db)->hdb_firstkey = NDBM_firstkey;
356 (*db)->hdb_nextkey= NDBM_nextkey;
357 (*db)->hdb_lock = NDBM_lock;
358 (*db)->hdb_unlock = NDBM_unlock;
359 (*db)->hdb_rename = NDBM_rename;
360 (*db)->hdb__get = NDBM__get;
361 (*db)->hdb__put = NDBM__put;
362 (*db)->hdb__del = NDBM__del;
363 (*db)->hdb_destroy = NDBM_destroy;
367 #endif /* HAVE_NDBM */