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: ndbm.c 16395 2005-12-13 11:54:10Z lha $");
40 #if defined(HAVE_GDBM_NDBM_H)
41 #include <gdbm/ndbm.h>
42 #elif defined(HAVE_NDBM_H)
44 #elif defined(HAVE_DBM_H)
53 static krb5_error_code
54 NDBM_destroy(krb5_context context, HDB *db)
58 ret = hdb_clear_master_key (context, db);
64 static krb5_error_code
65 NDBM_lock(krb5_context context, HDB *db, int operation)
67 struct ndbm_db *d = db->hdb_db;
68 return hdb_lock(d->lock_fd, operation);
71 static krb5_error_code
72 NDBM_unlock(krb5_context context, HDB *db)
74 struct ndbm_db *d = db->hdb_db;
75 return hdb_unlock(d->lock_fd);
78 static krb5_error_code
79 NDBM_seq(krb5_context context, HDB *db,
80 unsigned flags, hdb_entry_ex *entry, int first)
83 struct ndbm_db *d = (struct ndbm_db *)db->hdb_db;
85 krb5_data key_data, data;
86 krb5_error_code ret = 0;
89 key = dbm_firstkey(d->db);
91 key = dbm_nextkey(d->db);
93 return HDB_ERR_NOENTRY;
94 key_data.data = key.dptr;
95 key_data.length = key.dsize;
96 ret = db->hdb_lock(context, db, HDB_RLOCK);
98 value = dbm_fetch(d->db, key);
99 db->hdb_unlock(context, db);
100 data.data = value.dptr;
101 data.length = value.dsize;
102 memset(entry, 0, sizeof(*entry));
103 if(hdb_value2entry(context, &data, &entry->entry))
104 return NDBM_seq(context, db, flags, entry, 0);
105 if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) {
106 ret = hdb_unseal_keys (context, db, &entry->entry);
108 hdb_free_entry (context, entry);
110 if (ret == 0 && entry->entry.principal == NULL) {
111 entry->entry.principal = malloc (sizeof(*entry->entry.principal));
112 if (entry->entry.principal == NULL) {
114 hdb_free_entry (context, entry);
115 krb5_set_error_string(context, "malloc: out of memory");
117 hdb_key2principal (context, &key_data, entry->entry.principal);
124 static krb5_error_code
125 NDBM_firstkey(krb5_context context, HDB *db,unsigned flags,hdb_entry_ex *entry)
127 return NDBM_seq(context, db, flags, entry, 1);
131 static krb5_error_code
132 NDBM_nextkey(krb5_context context, HDB *db, unsigned flags,hdb_entry_ex *entry)
134 return NDBM_seq(context, db, flags, entry, 0);
137 static krb5_error_code
138 NDBM_rename(krb5_context context, HDB *db, const char *new_name)
140 /* XXX this function will break */
141 struct ndbm_db *d = db->hdb_db;
144 char *old_dir, *old_pag, *new_dir, *new_pag;
148 /* lock old and new databases */
149 ret = db->hdb_lock(context, db, HDB_WLOCK);
152 asprintf(&new_lock, "%s.lock", new_name);
153 if(new_lock == NULL) {
154 db->hdb_unlock(context, db);
155 krb5_set_error_string(context, "malloc: out of memory");
158 lock_fd = open(new_lock, O_RDWR | O_CREAT, 0600);
161 db->hdb_unlock(context, db);
162 krb5_set_error_string(context, "open(%s): %s", new_lock,
168 ret = hdb_lock(lock_fd, HDB_WLOCK);
170 db->hdb_unlock(context, db);
175 asprintf(&old_dir, "%s.dir", db->hdb_name);
176 asprintf(&old_pag, "%s.pag", db->hdb_name);
177 asprintf(&new_dir, "%s.dir", new_name);
178 asprintf(&new_pag, "%s.pag", new_name);
180 ret = rename(old_dir, new_dir) || rename(old_pag, new_pag);
186 db->hdb_unlock(context, db);
191 krb5_set_error_string(context, "rename: %s", strerror(ret));
196 d->lock_fd = lock_fd;
199 db->hdb_name = strdup(new_name);
203 static krb5_error_code
204 NDBM__get(krb5_context context, HDB *db, krb5_data key, krb5_data *reply)
206 struct ndbm_db *d = (struct ndbm_db *)db->hdb_db;
211 k.dsize = key.length;
212 code = db->hdb_lock(context, db, HDB_RLOCK);
215 v = dbm_fetch(d->db, k);
216 db->hdb_unlock(context, db);
218 return HDB_ERR_NOENTRY;
220 krb5_data_copy(reply, v.dptr, v.dsize);
224 static krb5_error_code
225 NDBM__put(krb5_context context, HDB *db, int replace,
226 krb5_data key, krb5_data value)
228 struct ndbm_db *d = (struct ndbm_db *)db->hdb_db;
233 k.dsize = key.length;
235 v.dsize = value.length;
237 code = db->hdb_lock(context, db, HDB_WLOCK);
240 code = dbm_store(d->db, k, v, replace ? DBM_REPLACE : DBM_INSERT);
241 db->hdb_unlock(context, db);
243 return HDB_ERR_EXISTS;
249 static krb5_error_code
250 NDBM__del(krb5_context context, HDB *db, krb5_data key)
252 struct ndbm_db *d = (struct ndbm_db *)db->hdb_db;
258 k.dsize = key.length;
259 ret = db->hdb_lock(context, db, HDB_WLOCK);
261 code = dbm_delete(d->db, k);
262 db->hdb_unlock(context, db);
269 static krb5_error_code
270 NDBM_close(krb5_context context, HDB *db)
272 struct ndbm_db *d = db->hdb_db;
279 static krb5_error_code
280 NDBM_open(krb5_context context, HDB *db, int flags, mode_t mode)
283 struct ndbm_db *d = malloc(sizeof(*d));
287 krb5_set_error_string(context, "malloc: out of memory");
290 asprintf(&lock_file, "%s.lock", (char*)db->hdb_name);
291 if(lock_file == NULL) {
293 krb5_set_error_string(context, "malloc: out of memory");
296 d->db = dbm_open((char*)db->hdb_name, flags, mode);
301 krb5_set_error_string(context, "dbm_open(%s): %s", db->hdb_name,
305 d->lock_fd = open(lock_file, O_RDWR | O_CREAT, 0600);
310 krb5_set_error_string(context, "open(%s): %s", lock_file,
317 if((flags & O_ACCMODE) == O_RDONLY)
318 ret = hdb_check_db_format(context, db);
320 ret = hdb_init_db(context, db);
321 if(ret == HDB_ERR_NOENTRY)
324 NDBM_close(context, db);
325 krb5_set_error_string(context, "hdb_open: failed %s database %s",
326 (flags & O_ACCMODE) == O_RDONLY ?
327 "checking format of" : "initialize",
334 hdb_ndbm_create(krb5_context context, HDB **db,
335 const char *filename)
337 *db = calloc(1, sizeof(**db));
339 krb5_set_error_string(context, "malloc: out of memory");
343 (*db)->hdb_db = NULL;
344 (*db)->hdb_name = strdup(filename);
345 if ((*db)->hdb_name == NULL) {
346 krb5_set_error_string(context, "malloc: out of memory");
351 (*db)->hdb_master_key_set = 0;
352 (*db)->hdb_openp = 0;
353 (*db)->hdb_open = NDBM_open;
354 (*db)->hdb_close = NDBM_close;
355 (*db)->hdb_fetch = _hdb_fetch;
356 (*db)->hdb_store = _hdb_store;
357 (*db)->hdb_remove = _hdb_remove;
358 (*db)->hdb_firstkey = NDBM_firstkey;
359 (*db)->hdb_nextkey= NDBM_nextkey;
360 (*db)->hdb_lock = NDBM_lock;
361 (*db)->hdb_unlock = NDBM_unlock;
362 (*db)->hdb_rename = NDBM_rename;
363 (*db)->hdb__get = NDBM__get;
364 (*db)->hdb__put = NDBM__put;
365 (*db)->hdb__del = NDBM__del;
366 (*db)->hdb_destroy = NDBM_destroy;
370 #endif /* HAVE_NDBM */