define YY_NULL
[heimdal.git] / lib / hdb / ndbm.c
blobd97a98ed6b5a1261aa3e5cc10bb464fafdc6b0da
1 /*
2 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "hdb_locl.h"
36 #if HAVE_NDBM
38 #if defined(HAVE_GDBM_NDBM_H)
39 #include <gdbm/ndbm.h>
40 #elif defined(HAVE_NDBM_H)
41 #include <ndbm.h>
42 #elif defined(HAVE_DBM_H)
43 #include <dbm.h>
44 #endif
46 struct ndbm_db {
47 DBM *db;
48 int lock_fd;
51 static krb5_error_code
52 NDBM_destroy(krb5_context context, HDB *db)
54 hdb_clear_master_key (context, db);
55 free(db->hdb_name);
56 free(db);
57 return 0;
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;
80 datum key, value;
81 krb5_data key_data, data;
82 krb5_error_code ret = 0;
84 if(first)
85 key = dbm_firstkey(d->db);
86 else
87 key = dbm_nextkey(d->db);
88 if(key.dptr == NULL)
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);
93 if(ret) return ret;
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);
103 if (ret)
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);
110 ret = ENOMEM;
111 krb5_set_error_message(context, ret, "malloc: out of memory");
112 } else {
113 hdb_key2principal (context, &key_data, entry->entry.principal);
116 return ret;
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;
139 int ret;
140 char *old_dir, *old_pag, *new_dir, *new_pag;
141 char *new_lock;
142 int lock_fd;
144 /* lock old and new databases */
145 ret = db->hdb_lock(context, db, HDB_WLOCK);
146 if(ret)
147 return ret;
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");
152 return ENOMEM;
154 lock_fd = open(new_lock, O_RDWR | O_CREAT, 0600);
155 if(lock_fd < 0) {
156 ret = errno;
157 db->hdb_unlock(context, db);
158 krb5_set_error_message(context, ret, "open(%s): %s", new_lock,
159 strerror(ret));
160 free(new_lock);
161 return ret;
163 free(new_lock);
164 ret = hdb_lock(lock_fd, HDB_WLOCK);
165 if(ret) {
166 db->hdb_unlock(context, db);
167 close(lock_fd);
168 return ret;
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);
177 free(old_dir);
178 free(old_pag);
179 free(new_dir);
180 free(new_pag);
181 hdb_unlock(lock_fd);
182 db->hdb_unlock(context, db);
184 if(ret) {
185 ret = errno;
186 close(lock_fd);
187 krb5_set_error_message(context, ret, "rename: %s", strerror(ret));
188 return ret;
191 close(d->lock_fd);
192 d->lock_fd = lock_fd;
194 free(db->hdb_name);
195 db->hdb_name = strdup(new_name);
196 return 0;
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;
203 datum k, v;
204 int code;
206 k.dptr = key.data;
207 k.dsize = key.length;
208 code = db->hdb_lock(context, db, HDB_RLOCK);
209 if(code)
210 return code;
211 v = dbm_fetch(d->db, k);
212 db->hdb_unlock(context, db);
213 if(v.dptr == NULL)
214 return HDB_ERR_NOENTRY;
216 krb5_data_copy(reply, v.dptr, v.dsize);
217 return 0;
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;
225 datum k, v;
226 int code;
228 k.dptr = key.data;
229 k.dsize = key.length;
230 v.dptr = value.data;
231 v.dsize = value.length;
233 code = db->hdb_lock(context, db, HDB_WLOCK);
234 if(code)
235 return code;
236 code = dbm_store(d->db, k, v, replace ? DBM_REPLACE : DBM_INSERT);
237 db->hdb_unlock(context, db);
238 if(code == 1)
239 return HDB_ERR_EXISTS;
240 if (code < 0)
241 return code;
242 return 0;
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;
249 datum k;
250 int code;
251 krb5_error_code ret;
253 k.dptr = key.data;
254 k.dsize = key.length;
255 ret = db->hdb_lock(context, db, HDB_WLOCK);
256 if(ret) return ret;
257 code = dbm_delete(d->db, k);
258 db->hdb_unlock(context, db);
259 if(code < 0)
260 return errno;
261 return 0;
265 static krb5_error_code
266 NDBM_close(krb5_context context, HDB *db)
268 struct ndbm_db *d = db->hdb_db;
269 dbm_close(d->db);
270 close(d->lock_fd);
271 free(d);
272 return 0;
275 static krb5_error_code
276 NDBM_open(krb5_context context, HDB *db, int flags, mode_t mode)
278 krb5_error_code ret;
279 struct ndbm_db *d = malloc(sizeof(*d));
280 char *lock_file;
282 if(d == NULL) {
283 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
284 return ENOMEM;
286 asprintf(&lock_file, "%s.lock", (char*)db->hdb_name);
287 if(lock_file == NULL) {
288 free(d);
289 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
290 return ENOMEM;
292 d->db = dbm_open((char*)db->hdb_name, flags, mode);
293 if(d->db == NULL){
294 ret = errno;
295 free(d);
296 free(lock_file);
297 krb5_set_error_message(context, ret, "dbm_open(%s): %s", db->hdb_name,
298 strerror(ret));
299 return ret;
301 d->lock_fd = open(lock_file, O_RDWR | O_CREAT, 0600);
302 if(d->lock_fd < 0){
303 ret = errno;
304 dbm_close(d->db);
305 free(d);
306 krb5_set_error_message(context, ret, "open(%s): %s", lock_file,
307 strerror(ret));
308 free(lock_file);
309 return ret;
311 free(lock_file);
312 db->hdb_db = d;
313 if((flags & O_ACCMODE) == O_RDONLY)
314 ret = hdb_check_db_format(context, db);
315 else
316 ret = hdb_init_db(context, db);
317 if(ret == HDB_ERR_NOENTRY)
318 return 0;
319 if (ret) {
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",
324 db->hdb_name);
326 return ret;
329 krb5_error_code
330 hdb_ndbm_create(krb5_context context, HDB **db,
331 const char *filename)
333 *db = calloc(1, sizeof(**db));
334 if (*db == NULL) {
335 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
336 return ENOMEM;
339 (*db)->hdb_db = NULL;
340 (*db)->hdb_name = strdup(filename);
341 if ((*db)->hdb_name == NULL) {
342 free(*db);
343 *db = NULL;
344 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
345 return ENOMEM;
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;
364 return 0;
367 #endif /* HAVE_NDBM */