clean files
[heimdal.git] / lib / hdb / db3.c
blob891cf98777b15f1f44f90435e0b23229722c52d4
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_DB6_DB_H
43 #include <db6/db.h>
44 #elif HAVE_DB5_DB_H
45 #include <db5/db.h>
46 #elif HAVE_DB4_DB_H
47 #include <db4/db.h>
48 #elif HAVE_DB3_DB_H
49 #include <db3/db.h>
50 #else
51 #include <db.h>
52 #endif
54 typedef struct {
55 HDB hdb; /* generic members */
56 int lock_fd; /* DB3-specific */
57 } DB3_HDB;
60 static krb5_error_code
61 DB_close(krb5_context context, HDB *db)
63 DB3_HDB *db3 = (DB3_HDB *)db;
64 DB *d = (DB*)db->hdb_db;
65 DBC *dbcp = (DBC*)db->hdb_dbc;
67 heim_assert(d != 0, "Closing already closed HDB");
69 if (dbcp != NULL)
70 dbcp->c_close(dbcp);
71 if (d != NULL)
72 d->close(d, 0);
73 if (db3->lock_fd >= 0)
74 close(db3->lock_fd);
76 db3->lock_fd = -1;
77 db->hdb_dbc = 0;
78 db->hdb_db = 0;
80 return 0;
83 static krb5_error_code
84 DB_destroy(krb5_context context, HDB *db)
86 krb5_error_code ret;
88 ret = hdb_clear_master_key (context, db);
89 free(db->hdb_name);
90 free(db);
91 return ret;
94 static krb5_error_code
95 DB_lock(krb5_context context, HDB *db, int operation)
98 return 0;
101 static krb5_error_code
102 DB_unlock(krb5_context context, HDB *db)
105 return 0;
109 static krb5_error_code
110 DB_seq(krb5_context context, HDB *db,
111 unsigned flags, hdb_entry_ex *entry, int flag)
113 DBT key, value;
114 DBC *dbcp = db->hdb_dbc;
115 krb5_data key_data, data;
116 int code;
118 memset(&key, 0, sizeof(DBT));
119 memset(&value, 0, sizeof(DBT));
120 code = (*dbcp->c_get)(dbcp, &key, &value, flag);
121 if (code == DB_NOTFOUND)
122 return HDB_ERR_NOENTRY;
123 if (code)
124 return code;
126 key_data.data = key.data;
127 key_data.length = key.size;
128 data.data = value.data;
129 data.length = value.size;
130 memset(entry, 0, sizeof(*entry));
131 if (hdb_value2entry(context, &data, &entry->entry))
132 return DB_seq(context, db, flags, entry, DB_NEXT);
133 if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) {
134 code = hdb_unseal_keys (context, db, &entry->entry);
135 if (code)
136 hdb_free_entry (context, entry);
138 if (entry->entry.principal == NULL) {
139 entry->entry.principal = malloc(sizeof(*entry->entry.principal));
140 if (entry->entry.principal == NULL) {
141 hdb_free_entry (context, entry);
142 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
143 return ENOMEM;
144 } else {
145 hdb_key2principal(context, &key_data, entry->entry.principal);
148 return 0;
152 static krb5_error_code
153 DB_firstkey(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
155 return DB_seq(context, db, flags, entry, DB_FIRST);
159 static krb5_error_code
160 DB_nextkey(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
162 return DB_seq(context, db, flags, entry, DB_NEXT);
165 static krb5_error_code
166 DB_rename(krb5_context context, HDB *db, const char *new_name)
168 int ret;
169 char *old, *new;
171 ret = asprintf(&old, "%s.db", db->hdb_name);
172 if (ret == -1)
173 return ENOMEM;
174 ret = asprintf(&new, "%s.db", new_name);
175 if (ret == -1) {
176 free(old);
177 return ENOMEM;
179 ret = rename(old, new);
180 free(old);
181 if(ret) {
182 free(new);
183 return errno;
186 free(db->hdb_name);
187 new[strlen(new) - 3] = '\0';
188 db->hdb_name = new;
189 return 0;
192 static krb5_error_code
193 DB__get(krb5_context context, HDB *db, krb5_data key, krb5_data *reply)
195 DB *d = (DB*)db->hdb_db;
196 DBT k, v;
197 int code;
199 memset(&k, 0, sizeof(DBT));
200 memset(&v, 0, sizeof(DBT));
201 k.data = key.data;
202 k.size = key.length;
203 k.flags = 0;
204 code = (*d->get)(d, NULL, &k, &v, 0);
205 if(code == DB_NOTFOUND)
206 return HDB_ERR_NOENTRY;
207 if(code)
208 return code;
210 krb5_data_copy(reply, v.data, v.size);
211 return 0;
214 static krb5_error_code
215 DB__put(krb5_context context, HDB *db, int replace,
216 krb5_data key, krb5_data value)
218 DB *d = (DB*)db->hdb_db;
219 DBT k, v;
220 int code;
222 memset(&k, 0, sizeof(DBT));
223 memset(&v, 0, sizeof(DBT));
224 k.data = key.data;
225 k.size = key.length;
226 k.flags = 0;
227 v.data = value.data;
228 v.size = value.length;
229 v.flags = 0;
230 code = (*d->put)(d, NULL, &k, &v, replace ? 0 : DB_NOOVERWRITE);
231 if(code == DB_KEYEXIST)
232 return HDB_ERR_EXISTS;
233 if(code)
234 return errno;
235 return 0;
238 static krb5_error_code
239 DB__del(krb5_context context, HDB *db, krb5_data key)
241 DB *d = (DB*)db->hdb_db;
242 DBT k;
243 krb5_error_code code;
244 memset(&k, 0, sizeof(DBT));
245 k.data = key.data;
246 k.size = key.length;
247 k.flags = 0;
248 code = (*d->del)(d, NULL, &k, 0);
249 if(code == DB_NOTFOUND)
250 return HDB_ERR_NOENTRY;
251 if(code)
252 return code;
253 return 0;
256 #define RD_CACHE_SZ 0x8000 /* Minimal read cache size */
257 #define WR_CACHE_SZ 0x8000 /* Minimal write cache size */
259 static int
260 _open_db(DB *d, char *fn, int myflags, int flags, mode_t mode, int *fd)
262 int ret;
263 int cache_size = (myflags & DB_RDONLY) ? RD_CACHE_SZ : WR_CACHE_SZ;
265 *fd = open(fn, flags, mode);
267 if (*fd == -1)
268 return errno;
271 * Without DB_FCNTL_LOCKING, the DB library complains when initializing
272 * a database in an empty file. Since the database is our lock file,
273 * we create it before Berkeley DB does, so a new DB always starts empty.
275 myflags |= DB_FCNTL_LOCKING;
277 ret = flock(*fd, (myflags&DB_RDONLY) ? LOCK_SH : LOCK_EX);
278 if (ret == -1) {
279 ret = errno;
280 close(*fd);
281 *fd = -1;
282 return ret;
285 d->set_cachesize(d, 0, cache_size, 0);
287 #if (DB_VERSION_MAJOR > 4) || ((DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR >= 1))
288 ret = (*d->open)(d, NULL, fn, NULL, DB_BTREE, myflags, mode);
289 #else
290 ret = (*d->open)(d, fn, NULL, DB_BTREE, myflags, mode);
291 #endif
293 if (ret != 0) {
294 close(*fd);
295 *fd = -1;
298 return ret;
301 static krb5_error_code
302 DB_open(krb5_context context, HDB *db, int flags, mode_t mode)
304 DB3_HDB *db3 = (DB3_HDB *)db;
305 DBC *dbc = NULL;
306 char *fn;
307 krb5_error_code ret;
308 DB *d;
309 int myflags = 0;
310 int aret;
312 heim_assert(db->hdb_db == 0, "Opening already open HDB");
314 if (flags & O_CREAT)
315 myflags |= DB_CREATE;
317 if (flags & O_EXCL)
318 myflags |= DB_EXCL;
320 if((flags & O_ACCMODE) == O_RDONLY)
321 myflags |= DB_RDONLY;
323 if (flags & O_TRUNC)
324 myflags |= DB_TRUNCATE;
326 aret = asprintf(&fn, "%s.db", db->hdb_name);
327 if (aret == -1) {
328 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
329 return ENOMEM;
332 if (db_create(&d, NULL, 0) != 0) {
333 free(fn);
334 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
335 return ENOMEM;
337 db->hdb_db = d;
339 /* From here on out always DB_close() before returning on error */
341 ret = _open_db(d, fn, myflags, flags, mode, &db3->lock_fd);
342 free(fn);
343 if (ret == ENOENT) {
344 /* try to open without .db extension */
345 ret = _open_db(d, db->hdb_name, myflags, flags, mode, &db3->lock_fd);
348 if (ret) {
349 DB_close(context, db);
350 krb5_set_error_message(context, ret, "opening %s: %s",
351 db->hdb_name, strerror(ret));
352 return ret;
355 #ifndef DB_CURSOR_BULK
356 # define DB_CURSOR_BULK 0 /* Missing with DB < 4.8 */
357 #endif
358 ret = (*d->cursor)(d, NULL, &dbc, DB_CURSOR_BULK);
360 if (ret) {
361 DB_close(context, db);
362 krb5_set_error_message(context, ret, "d->cursor: %s", strerror(ret));
363 return ret;
365 db->hdb_dbc = dbc;
367 if((flags & O_ACCMODE) == O_RDONLY)
368 ret = hdb_check_db_format(context, db);
369 else
370 ret = hdb_init_db(context, db);
371 if(ret == HDB_ERR_NOENTRY)
372 return 0;
373 if (ret) {
374 DB_close(context, db);
375 krb5_set_error_message(context, ret, "hdb_open: failed %s database %s",
376 (flags & O_ACCMODE) == O_RDONLY ?
377 "checking format of" : "initialize",
378 db->hdb_name);
381 return ret;
384 krb5_error_code
385 hdb_db_create(krb5_context context, HDB **db,
386 const char *filename)
388 DB3_HDB **db3 = (DB3_HDB **)db;
389 *db = calloc(1, sizeof(**db3)); /* Allocate space for the larger db3 */
390 if (*db == NULL) {
391 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
392 return ENOMEM;
395 (*db)->hdb_db = NULL;
396 (*db)->hdb_name = strdup(filename);
397 if ((*db)->hdb_name == NULL) {
398 free(*db);
399 *db = NULL;
400 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
401 return ENOMEM;
403 (*db)->hdb_master_key_set = 0;
404 (*db)->hdb_openp = 0;
405 (*db)->hdb_capability_flags = HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL;
406 (*db)->hdb_open = DB_open;
407 (*db)->hdb_close = DB_close;
408 (*db)->hdb_fetch_kvno = _hdb_fetch_kvno;
409 (*db)->hdb_store = _hdb_store;
410 (*db)->hdb_remove = _hdb_remove;
411 (*db)->hdb_firstkey = DB_firstkey;
412 (*db)->hdb_nextkey= DB_nextkey;
413 (*db)->hdb_lock = DB_lock;
414 (*db)->hdb_unlock = DB_unlock;
415 (*db)->hdb_rename = DB_rename;
416 (*db)->hdb__get = DB__get;
417 (*db)->hdb__put = DB__put;
418 (*db)->hdb__del = DB__del;
419 (*db)->hdb_destroy = DB_destroy;
421 (*db3)->lock_fd = -1;
422 return 0;
424 #endif /* HAVE_DB3 */