This commit was manufactured by cvs2svn to create tag
[heimdal.git] / lib / hdb / db3.c
blob5406fac6d468d4a990498e42abe11089d6831203
1 /*
2 * Copyright (c) 1997 - 2005 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 RCSID("$Id$");
38 #if HAVE_DB3
40 #ifdef HAVE_DB4_DB_H
41 #include <db4/db.h>
42 #elif defined(HAVE_DB3_DB_H)
43 #include <db3/db.h>
44 #else
45 #include <db.h>
46 #endif
48 static krb5_error_code
49 DB_close(krb5_context context, HDB *db)
51 DB *d = (DB*)db->hdb_db;
52 DBC *dbcp = (DBC*)db->hdb_dbc;
54 dbcp->c_close(dbcp);
55 db->hdb_dbc = 0;
56 d->close(d, 0);
57 return 0;
60 static krb5_error_code
61 DB_destroy(krb5_context context, HDB *db)
63 krb5_error_code ret;
65 ret = hdb_clear_master_key (context, db);
66 free(db->hdb_name);
67 free(db);
68 return ret;
71 static krb5_error_code
72 DB_lock(krb5_context context, HDB *db, int operation)
74 DB *d = (DB*)db->hdb_db;
75 int fd;
76 if ((*d->fd)(d, &fd))
77 return HDB_ERR_CANT_LOCK_DB;
78 return hdb_lock(fd, operation);
81 static krb5_error_code
82 DB_unlock(krb5_context context, HDB *db)
84 DB *d = (DB*)db->hdb_db;
85 int fd;
86 if ((*d->fd)(d, &fd))
87 return HDB_ERR_CANT_LOCK_DB;
88 return hdb_unlock(fd);
92 static krb5_error_code
93 DB_seq(krb5_context context, HDB *db,
94 unsigned flags, hdb_entry *entry, int flag)
96 DBT key, value;
97 DBC *dbcp = db->hdb_dbc;
98 krb5_data key_data, data;
99 int code;
101 memset(&key, 0, sizeof(DBT));
102 memset(&value, 0, sizeof(DBT));
103 if (db->hdb_lock(context, db, HDB_RLOCK))
104 return HDB_ERR_DB_INUSE;
105 code = dbcp->c_get(dbcp, &key, &value, flag);
106 db->hdb_unlock(context, db); /* XXX check value */
107 if (code == DB_NOTFOUND)
108 return HDB_ERR_NOENTRY;
109 if (code)
110 return code;
112 key_data.data = key.data;
113 key_data.length = key.size;
114 data.data = value.data;
115 data.length = value.size;
116 if (hdb_value2entry(context, &data, entry))
117 return DB_seq(context, db, flags, entry, DB_NEXT);
118 if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) {
119 code = hdb_unseal_keys (context, db, entry);
120 if (code)
121 hdb_free_entry (context, entry);
123 if (entry->principal == NULL) {
124 entry->principal = malloc(sizeof(*entry->principal));
125 if (entry->principal == NULL) {
126 hdb_free_entry (context, entry);
127 krb5_set_error_string(context, "malloc: out of memory");
128 return ENOMEM;
129 } else {
130 hdb_key2principal(context, &key_data, entry->principal);
133 return 0;
137 static krb5_error_code
138 DB_firstkey(krb5_context context, HDB *db, unsigned flags, hdb_entry *entry)
140 return DB_seq(context, db, flags, entry, DB_FIRST);
144 static krb5_error_code
145 DB_nextkey(krb5_context context, HDB *db, unsigned flags, hdb_entry *entry)
147 return DB_seq(context, db, flags, entry, DB_NEXT);
150 static krb5_error_code
151 DB_rename(krb5_context context, HDB *db, const char *new_name)
153 int ret;
154 char *old, *new;
156 asprintf(&old, "%s.db", db->hdb_name);
157 asprintf(&new, "%s.db", new_name);
158 ret = rename(old, new);
159 free(old);
160 free(new);
161 if(ret)
162 return errno;
164 free(db->hdb_name);
165 db->hdb_name = strdup(new_name);
166 return 0;
169 static krb5_error_code
170 DB__get(krb5_context context, HDB *db, krb5_data key, krb5_data *reply)
172 DB *d = (DB*)db->hdb_db;
173 DBT k, v;
174 int code;
176 memset(&k, 0, sizeof(DBT));
177 memset(&v, 0, sizeof(DBT));
178 k.data = key.data;
179 k.size = key.length;
180 k.flags = 0;
181 if ((code = db->hdb_lock(context, db, HDB_RLOCK)))
182 return code;
183 code = d->get(d, NULL, &k, &v, 0);
184 db->hdb_unlock(context, db);
185 if(code == DB_NOTFOUND)
186 return HDB_ERR_NOENTRY;
187 if(code)
188 return code;
190 krb5_data_copy(reply, v.data, v.size);
191 return 0;
194 static krb5_error_code
195 DB__put(krb5_context context, HDB *db, int replace,
196 krb5_data key, krb5_data value)
198 DB *d = (DB*)db->hdb_db;
199 DBT k, v;
200 int code;
202 memset(&k, 0, sizeof(DBT));
203 memset(&v, 0, sizeof(DBT));
204 k.data = key.data;
205 k.size = key.length;
206 k.flags = 0;
207 v.data = value.data;
208 v.size = value.length;
209 v.flags = 0;
210 if ((code = db->hdb_lock(context, db, HDB_WLOCK)))
211 return code;
212 code = d->put(d, NULL, &k, &v, replace ? 0 : DB_NOOVERWRITE);
213 db->hdb_unlock(context, db);
214 if(code == DB_KEYEXIST)
215 return HDB_ERR_EXISTS;
216 if(code)
217 return errno;
218 return 0;
221 static krb5_error_code
222 DB__del(krb5_context context, HDB *db, krb5_data key)
224 DB *d = (DB*)db->hdb_db;
225 DBT k;
226 krb5_error_code code;
227 memset(&k, 0, sizeof(DBT));
228 k.data = key.data;
229 k.size = key.length;
230 k.flags = 0;
231 code = db->hdb_lock(context, db, HDB_WLOCK);
232 if(code)
233 return code;
234 code = d->del(d, NULL, &k, 0);
235 db->hdb_unlock(context, db);
236 if(code == DB_NOTFOUND)
237 return HDB_ERR_NOENTRY;
238 if(code)
239 return code;
240 return 0;
243 static krb5_error_code
244 DB_open(krb5_context context, HDB *db, int flags, mode_t mode)
246 char *fn;
247 krb5_error_code ret;
248 DB *d;
249 int myflags = 0;
251 if (flags & O_CREAT)
252 myflags |= DB_CREATE;
254 if (flags & O_EXCL)
255 myflags |= DB_EXCL;
257 if((flags & O_ACCMODE) == O_RDONLY)
258 myflags |= DB_RDONLY;
260 if (flags & O_TRUNC)
261 myflags |= DB_TRUNCATE;
263 asprintf(&fn, "%s.db", db->hdb_name);
264 if (fn == NULL) {
265 krb5_set_error_string(context, "malloc: out of memory");
266 return ENOMEM;
268 db_create(&d, NULL, 0);
269 db->hdb_db = d;
271 #if (DB_VERSION_MAJOR >= 4) && (DB_VERSION_MINOR >= 1)
272 ret = d->open(db->hdb_db, NULL, fn, NULL, DB_BTREE, myflags, mode);
273 #else
274 ret = d->open(db->hdb_db, fn, NULL, DB_BTREE, myflags, mode);
275 #endif
277 if (ret == ENOENT) {
278 /* try to open without .db extension */
279 #if (DB_VERSION_MAJOR >= 4) && (DB_VERSION_MINOR >= 1)
280 ret = d->open(db->hdb_db, NULL, db->hdb_name, NULL, DB_BTREE, myflags, mode);
281 #else
282 ret = d->open(db->hdb_db, db->hdb_name, NULL, DB_BTREE, myflags, mode);
283 #endif
286 if (ret) {
287 free(fn);
288 krb5_set_error_string(context, "opening %s: %s",
289 db->hdb_name, strerror(ret));
290 return ret;
292 free(fn);
294 ret = d->cursor(d, NULL, (DBC **)&db->hdb_dbc, 0);
295 if (ret) {
296 krb5_set_error_string(context, "d->cursor: %s", strerror(ret));
297 return ret;
300 if((flags & O_ACCMODE) == O_RDONLY)
301 ret = hdb_check_db_format(context, db);
302 else
303 ret = hdb_init_db(context, db);
304 if(ret == HDB_ERR_NOENTRY)
305 return 0;
306 if (ret) {
307 DB_close(context, db);
308 krb5_set_error_string(context, "hdb_open: failed %s database %s",
309 (flags & O_ACCMODE) == O_RDONLY ?
310 "checking format of" : "initialize",
311 db->hdb_name);
314 return ret;
317 krb5_error_code
318 hdb_db_create(krb5_context context, HDB **db,
319 const char *filename)
321 *db = malloc(sizeof(**db));
322 if (*db == NULL) {
323 krb5_set_error_string(context, "malloc: out of memory");
324 return ENOMEM;
327 (*db)->hdb_db = NULL;
328 (*db)->hdb_name = strdup(filename);
329 if ((*db)->hdb_name == NULL) {
330 krb5_set_error_string(context, "malloc: out of memory");
331 free(*db);
332 *db = NULL;
333 return ENOMEM;
335 (*db)->hdb_master_key_set = 0;
336 (*db)->hdb_openp = 0;
337 (*db)->hdb_open = DB_open;
338 (*db)->hdb_close = DB_close;
339 (*db)->hdb_fetch = _hdb_fetch;
340 (*db)->hdb_store = _hdb_store;
341 (*db)->hdb_remove = _hdb_remove;
342 (*db)->hdb_firstkey = DB_firstkey;
343 (*db)->hdb_nextkey= DB_nextkey;
344 (*db)->hdb_lock = DB_lock;
345 (*db)->hdb_unlock = DB_unlock;
346 (*db)->hdb_rename = DB_rename;
347 (*db)->hdb__get = DB__get;
348 (*db)->hdb__put = DB__put;
349 (*db)->hdb__del = DB__del;
350 (*db)->hdb_destroy = DB_destroy;
351 return 0;
353 #endif /* HAVE_DB3 */