DB_CURSOR_BULK requires DB 4.8 or later
[heimdal.git] / lib / hdb / db3.c
blob9c76908488650bd3fdf252656dcf1e4fc0da68c6
1 /*
2 * Copyright (c) 1997 - 2006 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 #include <fcntl.h>
38 #if HAVE_DB3
40 #ifdef HAVE_DBHEADER
41 #include <db.h>
42 #elif HAVE_DB5_DB_H
43 #include <db5/db.h>
44 #elif HAVE_DB4_DB_H
45 #include <db4/db.h>
46 #elif HAVE_DB3_DB_H
47 #include <db3/db.h>
48 #else
49 #include <db.h>
50 #endif
52 typedef struct {
53 HDB hdb; /* generic members */
54 int lock_fd; /* DB3-specific */
55 } DB3_HDB;
58 static krb5_error_code
59 DB_close(krb5_context context, HDB *db)
61 DB3_HDB *db3 = (DB3_HDB *)db;
62 DB *d = (DB*)db->hdb_db;
63 DBC *dbcp;
65 heim_assert(d != 0, "Closing already closed HDB");
67 dbcp = (DBC*)db->hdb_dbc;
68 (*dbcp->c_close)(dbcp);
69 db->hdb_dbc = 0;
70 (*d->close)(d, 0);
71 db->hdb_db = 0;
73 if (db3->lock_fd >= 0) {
74 close(db3->lock_fd);
75 db3->lock_fd = -1;
78 return 0;
81 static krb5_error_code
82 DB_destroy(krb5_context context, HDB *db)
84 krb5_error_code ret;
86 ret = hdb_clear_master_key (context, db);
87 free(db->hdb_name);
88 free(db);
89 return ret;
92 static krb5_error_code
93 DB_lock(krb5_context context, HDB *db, int operation)
96 return 0;
99 static krb5_error_code
100 DB_unlock(krb5_context context, HDB *db)
103 return 0;
107 static krb5_error_code
108 DB_seq(krb5_context context, HDB *db,
109 unsigned flags, hdb_entry_ex *entry, int flag)
111 DBT key, value;
112 DBC *dbcp = db->hdb_dbc;
113 krb5_data key_data, data;
114 int code;
116 memset(&key, 0, sizeof(DBT));
117 memset(&value, 0, sizeof(DBT));
118 code = (*dbcp->c_get)(dbcp, &key, &value, flag);
119 if (code == DB_NOTFOUND)
120 return HDB_ERR_NOENTRY;
121 if (code)
122 return code;
124 key_data.data = key.data;
125 key_data.length = key.size;
126 data.data = value.data;
127 data.length = value.size;
128 memset(entry, 0, sizeof(*entry));
129 if (hdb_value2entry(context, &data, &entry->entry))
130 return DB_seq(context, db, flags, entry, DB_NEXT);
131 if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) {
132 code = hdb_unseal_keys (context, db, &entry->entry);
133 if (code)
134 hdb_free_entry (context, entry);
136 if (entry->entry.principal == NULL) {
137 entry->entry.principal = malloc(sizeof(*entry->entry.principal));
138 if (entry->entry.principal == NULL) {
139 hdb_free_entry (context, entry);
140 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
141 return ENOMEM;
142 } else {
143 hdb_key2principal(context, &key_data, entry->entry.principal);
146 return 0;
150 static krb5_error_code
151 DB_firstkey(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
153 return DB_seq(context, db, flags, entry, DB_FIRST);
157 static krb5_error_code
158 DB_nextkey(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
160 return DB_seq(context, db, flags, entry, DB_NEXT);
163 static krb5_error_code
164 DB_rename(krb5_context context, HDB *db, const char *new_name)
166 int ret;
167 char *old, *new;
169 ret = asprintf(&old, "%s.db", db->hdb_name);
170 if (ret == -1)
171 return ENOMEM;
172 ret = asprintf(&new, "%s.db", new_name);
173 if (ret == -1) {
174 free(old);
175 return ENOMEM;
177 ret = rename(old, new);
178 free(old);
179 if(ret) {
180 free(new);
181 return errno;
184 free(db->hdb_name);
185 new[strlen(new) - 3] = '\0';
186 db->hdb_name = new;
187 return 0;
190 static krb5_error_code
191 DB__get(krb5_context context, HDB *db, krb5_data key, krb5_data *reply)
193 DB *d = (DB*)db->hdb_db;
194 DBT k, v;
195 int code;
197 memset(&k, 0, sizeof(DBT));
198 memset(&v, 0, sizeof(DBT));
199 k.data = key.data;
200 k.size = key.length;
201 k.flags = 0;
202 code = (*d->get)(d, NULL, &k, &v, 0);
203 if(code == DB_NOTFOUND)
204 return HDB_ERR_NOENTRY;
205 if(code)
206 return code;
208 krb5_data_copy(reply, v.data, v.size);
209 return 0;
212 static krb5_error_code
213 DB__put(krb5_context context, HDB *db, int replace,
214 krb5_data key, krb5_data value)
216 DB *d = (DB*)db->hdb_db;
217 DBT k, v;
218 int code;
220 memset(&k, 0, sizeof(DBT));
221 memset(&v, 0, sizeof(DBT));
222 k.data = key.data;
223 k.size = key.length;
224 k.flags = 0;
225 v.data = value.data;
226 v.size = value.length;
227 v.flags = 0;
228 code = (*d->put)(d, NULL, &k, &v, replace ? 0 : DB_NOOVERWRITE);
229 if(code == DB_KEYEXIST)
230 return HDB_ERR_EXISTS;
231 if(code)
232 return errno;
233 return 0;
236 static krb5_error_code
237 DB__del(krb5_context context, HDB *db, krb5_data key)
239 DB *d = (DB*)db->hdb_db;
240 DBT k;
241 krb5_error_code code;
242 memset(&k, 0, sizeof(DBT));
243 k.data = key.data;
244 k.size = key.length;
245 k.flags = 0;
246 code = (*d->del)(d, NULL, &k, 0);
247 if(code == DB_NOTFOUND)
248 return HDB_ERR_NOENTRY;
249 if(code)
250 return code;
251 return 0;
254 #define RD_CACHE_SZ 0x8000 /* Minimal read cache size */
255 #define WR_CACHE_SZ 0x8000 /* Minimal write cache size */
257 static int
258 _open_db(DB *d, char *fn, int myflags, int flags, mode_t mode, int *fd)
260 int ret;
261 int cache_size = (myflags & DB_RDONLY) ? RD_CACHE_SZ : WR_CACHE_SZ;
263 *fd = open(fn, flags, mode);
265 if (*fd == -1)
266 return errno;
269 * Without DB_FCNTL_LOCKING, the DB library complains when initializing
270 * a database in an empty file. Since the database is our lock file,
271 * we create it before Berkeley DB does, so a new DB always starts empty.
273 myflags |= DB_FCNTL_LOCKING;
275 ret = flock(*fd, (myflags&DB_RDONLY) ? LOCK_SH : LOCK_EX);
276 if (ret == -1) {
277 ret = errno;
278 close(*fd);
279 return ret;
282 d->set_cachesize(d, 0, cache_size, 0);
284 #if (DB_VERSION_MAJOR >= 4) && (DB_VERSION_MINOR >= 1)
285 ret = (*d->open)(d, NULL, fn, NULL, DB_BTREE, myflags, mode);
286 #else
287 ret = (*d->open)(d, fn, NULL, DB_BTREE, myflags, mode);
288 #endif
290 if (ret != 0)
291 close(*fd);
293 return ret;
296 static krb5_error_code
297 DB_open(krb5_context context, HDB *db, int flags, mode_t mode)
299 DB3_HDB *db3 = (DB3_HDB *)db;
300 DBC *dbc = NULL;
301 char *fn;
302 krb5_error_code ret;
303 DB *d;
304 int myflags = 0;
305 int aret;
307 heim_assert(db->hdb_db == 0, "Opening already open HDB");
309 if (flags & O_CREAT)
310 myflags |= DB_CREATE;
312 if (flags & O_EXCL)
313 myflags |= DB_EXCL;
315 if((flags & O_ACCMODE) == O_RDONLY)
316 myflags |= DB_RDONLY;
318 if (flags & O_TRUNC)
319 myflags |= DB_TRUNCATE;
321 aret = asprintf(&fn, "%s.db", db->hdb_name);
322 if (aret == -1) {
323 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
324 return ENOMEM;
327 if (db_create(&d, NULL, 0) != 0) {
328 free(fn);
329 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
330 return ENOMEM;
332 db->hdb_db = d;
334 ret = _open_db(d, fn, myflags, flags, mode, &db3->lock_fd);
335 free(fn);
336 if (ret == ENOENT) {
337 /* try to open without .db extension */
338 ret = _open_db(d, db->hdb_name, myflags, flags,
339 mode, &db3->lock_fd);
342 if (ret) {
343 krb5_set_error_message(context, ret, "opening %s: %s",
344 db->hdb_name, strerror(ret));
345 return ret;
348 #ifndef DB_CURSOR_BULK
349 # define DB_CURSOR_BULK 0 /* Missing with DB < 4.8 */
350 #endif
351 ret = (*d->cursor)(d, NULL, &dbc, DB_CURSOR_BULK);
353 if (ret) {
354 krb5_set_error_message(context, ret, "d->cursor: %s", strerror(ret));
355 return ret;
357 db->hdb_dbc = dbc;
359 if((flags & O_ACCMODE) == O_RDONLY)
360 ret = hdb_check_db_format(context, db);
361 else
362 ret = hdb_init_db(context, db);
363 if(ret == HDB_ERR_NOENTRY)
364 return 0;
365 if (ret) {
366 DB_close(context, db);
367 krb5_set_error_message(context, ret, "hdb_open: failed %s database %s",
368 (flags & O_ACCMODE) == O_RDONLY ?
369 "checking format of" : "initialize",
370 db->hdb_name);
373 return ret;
376 krb5_error_code
377 hdb_db_create(krb5_context context, HDB **db,
378 const char *filename)
380 DB3_HDB **db3 = (DB3_HDB **)db;
381 *db = calloc(1, sizeof(**db3)); /* Allocate space for the larger db3 */
382 if (*db == NULL) {
383 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
384 return ENOMEM;
387 (*db)->hdb_db = NULL;
388 (*db)->hdb_name = strdup(filename);
389 if ((*db)->hdb_name == NULL) {
390 free(*db);
391 *db = NULL;
392 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
393 return ENOMEM;
395 (*db)->hdb_master_key_set = 0;
396 (*db)->hdb_openp = 0;
397 (*db)->hdb_capability_flags = HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL;
398 (*db)->hdb_open = DB_open;
399 (*db)->hdb_close = DB_close;
400 (*db)->hdb_fetch_kvno = _hdb_fetch_kvno;
401 (*db)->hdb_store = _hdb_store;
402 (*db)->hdb_remove = _hdb_remove;
403 (*db)->hdb_firstkey = DB_firstkey;
404 (*db)->hdb_nextkey= DB_nextkey;
405 (*db)->hdb_lock = DB_lock;
406 (*db)->hdb_unlock = DB_unlock;
407 (*db)->hdb_rename = DB_rename;
408 (*db)->hdb__get = DB__get;
409 (*db)->hdb__put = DB__put;
410 (*db)->hdb__del = DB__del;
411 (*db)->hdb_destroy = DB_destroy;
413 (*db3)->lock_fd = -1;
414 return 0;
416 #endif /* HAVE_DB3 */