drop RCSID
[heimdal.git] / lib / hdb / db3.c
blob3b22c2e0554474d71f74f417029532cc67e0a498
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 #if HAVE_DB3
38 #ifdef HAVE_DB4_DB_H
39 #include <db4/db.h>
40 #elif defined(HAVE_DB3_DB_H)
41 #include <db3/db.h>
42 #else
43 #include <db.h>
44 #endif
46 static krb5_error_code
47 DB_close(krb5_context context, HDB *db)
49 DB *d = (DB*)db->hdb_db;
50 DBC *dbcp = (DBC*)db->hdb_dbc;
52 (*dbcp->c_close)(dbcp);
53 db->hdb_dbc = 0;
54 (*d->close)(d, 0);
55 return 0;
58 static krb5_error_code
59 DB_destroy(krb5_context context, HDB *db)
61 krb5_error_code ret;
63 ret = hdb_clear_master_key (context, db);
64 free(db->hdb_name);
65 free(db);
66 return ret;
69 static krb5_error_code
70 DB_lock(krb5_context context, HDB *db, int operation)
72 DB *d = (DB*)db->hdb_db;
73 int fd;
74 if ((*d->fd)(d, &fd))
75 return HDB_ERR_CANT_LOCK_DB;
76 return hdb_lock(fd, operation);
79 static krb5_error_code
80 DB_unlock(krb5_context context, HDB *db)
82 DB *d = (DB*)db->hdb_db;
83 int fd;
84 if ((*d->fd)(d, &fd))
85 return HDB_ERR_CANT_LOCK_DB;
86 return hdb_unlock(fd);
90 static krb5_error_code
91 DB_seq(krb5_context context, HDB *db,
92 unsigned flags, hdb_entry_ex *entry, int flag)
94 DBT key, value;
95 DBC *dbcp = db->hdb_dbc;
96 krb5_data key_data, data;
97 int code;
99 memset(&key, 0, sizeof(DBT));
100 memset(&value, 0, sizeof(DBT));
101 if ((*db->hdb_lock)(context, db, HDB_RLOCK))
102 return HDB_ERR_DB_INUSE;
103 code = (*dbcp->c_get)(dbcp, &key, &value, flag);
104 (*db->hdb_unlock)(context, db); /* XXX check value */
105 if (code == DB_NOTFOUND)
106 return HDB_ERR_NOENTRY;
107 if (code)
108 return code;
110 key_data.data = key.data;
111 key_data.length = key.size;
112 data.data = value.data;
113 data.length = value.size;
114 memset(entry, 0, sizeof(*entry));
115 if (hdb_value2entry(context, &data, &entry->entry))
116 return DB_seq(context, db, flags, entry, DB_NEXT);
117 if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) {
118 code = hdb_unseal_keys (context, db, &entry->entry);
119 if (code)
120 hdb_free_entry (context, entry);
122 if (entry->entry.principal == NULL) {
123 entry->entry.principal = malloc(sizeof(*entry->entry.principal));
124 if (entry->entry.principal == NULL) {
125 hdb_free_entry (context, entry);
126 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
127 return ENOMEM;
128 } else {
129 hdb_key2principal(context, &key_data, entry->entry.principal);
132 return 0;
136 static krb5_error_code
137 DB_firstkey(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
139 return DB_seq(context, db, flags, entry, DB_FIRST);
143 static krb5_error_code
144 DB_nextkey(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
146 return DB_seq(context, db, flags, entry, DB_NEXT);
149 static krb5_error_code
150 DB_rename(krb5_context context, HDB *db, const char *new_name)
152 int ret;
153 char *old, *new;
155 asprintf(&old, "%s.db", db->hdb_name);
156 asprintf(&new, "%s.db", new_name);
157 ret = rename(old, new);
158 free(old);
159 free(new);
160 if(ret)
161 return errno;
163 free(db->hdb_name);
164 db->hdb_name = strdup(new_name);
165 return 0;
168 static krb5_error_code
169 DB__get(krb5_context context, HDB *db, krb5_data key, krb5_data *reply)
171 DB *d = (DB*)db->hdb_db;
172 DBT k, v;
173 int code;
175 memset(&k, 0, sizeof(DBT));
176 memset(&v, 0, sizeof(DBT));
177 k.data = key.data;
178 k.size = key.length;
179 k.flags = 0;
180 if ((code = (*db->hdb_lock)(context, db, HDB_RLOCK)))
181 return code;
182 code = (*d->get)(d, NULL, &k, &v, 0);
183 (*db->hdb_unlock)(context, db);
184 if(code == DB_NOTFOUND)
185 return HDB_ERR_NOENTRY;
186 if(code)
187 return code;
189 krb5_data_copy(reply, v.data, v.size);
190 return 0;
193 static krb5_error_code
194 DB__put(krb5_context context, HDB *db, int replace,
195 krb5_data key, krb5_data value)
197 DB *d = (DB*)db->hdb_db;
198 DBT k, v;
199 int code;
201 memset(&k, 0, sizeof(DBT));
202 memset(&v, 0, sizeof(DBT));
203 k.data = key.data;
204 k.size = key.length;
205 k.flags = 0;
206 v.data = value.data;
207 v.size = value.length;
208 v.flags = 0;
209 if ((code = (*db->hdb_lock)(context, db, HDB_WLOCK)))
210 return code;
211 code = (*d->put)(d, NULL, &k, &v, replace ? 0 : DB_NOOVERWRITE);
212 (*db->hdb_unlock)(context, db);
213 if(code == DB_KEYEXIST)
214 return HDB_ERR_EXISTS;
215 if(code)
216 return errno;
217 return 0;
220 static krb5_error_code
221 DB__del(krb5_context context, HDB *db, krb5_data key)
223 DB *d = (DB*)db->hdb_db;
224 DBT k;
225 krb5_error_code code;
226 memset(&k, 0, sizeof(DBT));
227 k.data = key.data;
228 k.size = key.length;
229 k.flags = 0;
230 code = (*db->hdb_lock)(context, db, HDB_WLOCK);
231 if(code)
232 return code;
233 code = (*d->del)(d, NULL, &k, 0);
234 (*db->hdb_unlock)(context, db);
235 if(code == DB_NOTFOUND)
236 return HDB_ERR_NOENTRY;
237 if(code)
238 return code;
239 return 0;
242 static krb5_error_code
243 DB_open(krb5_context context, HDB *db, int flags, mode_t mode)
245 DBC *dbc = NULL;
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_message(context, ENOMEM, "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,
281 myflags, mode);
282 #else
283 ret = (*d->open)(db->hdb_db, db->hdb_name, NULL, DB_BTREE,
284 myflags, mode);
285 #endif
288 if (ret) {
289 free(fn);
290 krb5_set_error_message(context, ret, "opening %s: %s",
291 db->hdb_name, strerror(ret));
292 return ret;
294 free(fn);
296 ret = (*d->cursor)(d, NULL, &dbc, 0);
297 if (ret) {
298 krb5_set_error_message(context, ret, "d->cursor: %s", strerror(ret));
299 return ret;
301 db->hdb_dbc = dbc;
303 if((flags & O_ACCMODE) == O_RDONLY)
304 ret = hdb_check_db_format(context, db);
305 else
306 ret = hdb_init_db(context, db);
307 if(ret == HDB_ERR_NOENTRY)
308 return 0;
309 if (ret) {
310 DB_close(context, db);
311 krb5_set_error_message(context, ret, "hdb_open: failed %s database %s",
312 (flags & O_ACCMODE) == O_RDONLY ?
313 "checking format of" : "initialize",
314 db->hdb_name);
317 return ret;
320 krb5_error_code
321 hdb_db_create(krb5_context context, HDB **db,
322 const char *filename)
324 *db = calloc(1, sizeof(**db));
325 if (*db == NULL) {
326 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
327 return ENOMEM;
330 (*db)->hdb_db = NULL;
331 (*db)->hdb_name = strdup(filename);
332 if ((*db)->hdb_name == NULL) {
333 free(*db);
334 *db = NULL;
335 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
336 return ENOMEM;
338 (*db)->hdb_master_key_set = 0;
339 (*db)->hdb_openp = 0;
340 (*db)->hdb_capability_flags = HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL;
341 (*db)->hdb_open = DB_open;
342 (*db)->hdb_close = DB_close;
343 (*db)->hdb_fetch = _hdb_fetch;
344 (*db)->hdb_store = _hdb_store;
345 (*db)->hdb_remove = _hdb_remove;
346 (*db)->hdb_firstkey = DB_firstkey;
347 (*db)->hdb_nextkey= DB_nextkey;
348 (*db)->hdb_lock = DB_lock;
349 (*db)->hdb_unlock = DB_unlock;
350 (*db)->hdb_rename = DB_rename;
351 (*db)->hdb__get = DB__get;
352 (*db)->hdb__put = DB__put;
353 (*db)->hdb__del = DB__del;
354 (*db)->hdb_destroy = DB_destroy;
355 return 0;
357 #endif /* HAVE_DB3 */