Debian Codegraph @

lib/hdb/db3.c

raw
1 /*
2 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
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.
16 *
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.
20 *
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.
32 */
33
34 #include "hdb_locl.h"
35
36 #include <fcntl.h>
37
38 #if HAVE_DB3
39
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
53
54 typedef struct {
55 HDB hdb; /* generic members */
56 int lock_fd; /* DB3-specific */
57 int do_sync; /* DB3-specific */
58 } DB3_HDB;
59
60
61 static krb5_error_code
62 DB_close(krb5_context context, HDB *db)
63 {
64 DB3_HDB *db3 = (DB3_HDB *)db;
65 DB *d = (DB*)db->hdb_db;
66 DBC *dbcp = (DBC*)db->hdb_dbc;
67
68 heim_assert(d != 0, "Closing already closed HDB");
69
70 if (dbcp != NULL)
71 dbcp->c_close(dbcp);
72 if (d != NULL)
73 d->close(d, 0);
74 if (db3->lock_fd >= 0)
75 close(db3->lock_fd);
76
77 db3->lock_fd = -1;
78 db->hdb_dbc = 0;
79 db->hdb_db = 0;
80
81 return 0;
82 }
83
84 static krb5_error_code
85 DB_destroy(krb5_context context, HDB *db)
86 {
87 krb5_error_code ret;
88
89 ret = hdb_clear_master_key (context, db);
90 free(db->hdb_name);
91 free(db);
92 return ret;
93 }
94
95 static krb5_error_code
96 DB_set_sync(krb5_context context, HDB *db, int on)
97 {
98 DB3_HDB *db3 = (DB3_HDB *)db;
99 DB *d = (DB*)db->hdb_db;
100 krb5_error_code ret = 0;
101
102 db3->do_sync = on;
103 if (on) {
104 ret = (*d->sync)(d, 0);
105 if (ret) {
106 if (ret == EACCES || ret == ENOSPC || ret == EINVAL) {
107 krb5_set_error_message(context, ret,
108 "Database %s put sync error: %s",
109 db->hdb_name, strerror(ret));
110 } else {
111 ret = HDB_ERR_UK_SERROR;
112 krb5_set_error_message(context, ret,
113 "Database %s put sync error: unknown (%d)",
114 db->hdb_name, ret);
115 }
116 }
117 }
118 return ret;
119 }
120
121 static krb5_error_code
122 DB_lock(krb5_context context, HDB *db, int operation)
123 {
124
125 return 0;
126 }
127
128 static krb5_error_code
129 DB_unlock(krb5_context context, HDB *db)
130 {
131
132 return 0;
133 }
134
135
136 static krb5_error_code
137 DB_seq(krb5_context context, HDB *db,
138 unsigned flags, hdb_entry_ex *entry, int flag)
139 {
140 DBT key, value;
141 DBC *dbcp = db->hdb_dbc;
142 krb5_data key_data, data;
143 int code;
144
145 memset(&key, 0, sizeof(DBT));
146 memset(&value, 0, sizeof(DBT));
147 code = (*dbcp->c_get)(dbcp, &key, &value, flag);
148 if (code == DB_NOTFOUND)
149 return HDB_ERR_NOENTRY;
150 if (code)
151 return code;
152
153 key_data.data = key.data;
154 key_data.length = key.size;
155 data.data = value.data;
156 data.length = value.size;
157 memset(entry, 0, sizeof(*entry));
158 if (hdb_value2entry(context, &data, &entry->entry))
159 return DB_seq(context, db, flags, entry, DB_NEXT);
160 if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) {
161 code = hdb_unseal_keys (context, db, &entry->entry);
162 if (code)
163 hdb_free_entry (context, entry);
164 }
165 if (entry->entry.principal == NULL) {
166 entry->entry.principal = malloc(sizeof(*entry->entry.principal));
167 if (entry->entry.principal == NULL) {
168 hdb_free_entry (context, entry);
169 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
170 return ENOMEM;
171 } else {
172 hdb_key2principal(context, &key_data, entry->entry.principal);
173 }
174 }
175 return 0;
176 }
177
178
179 static krb5_error_code
180 DB_firstkey(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
181 {
182 return DB_seq(context, db, flags, entry, DB_FIRST);
183 }
184
185
186 static krb5_error_code
187 DB_nextkey(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
188 {
189 return DB_seq(context, db, flags, entry, DB_NEXT);
190 }
191
192 static krb5_error_code
193 DB_rename(krb5_context context, HDB *db, const char *new_name)
194 {
195 int ret;
196 char *old, *new;
197
198 if (strncmp(new_name, "db:", sizeof("db:") - 1) == 0)
199 new_name += sizeof("db:") - 1;
200 else if (strncmp(new_name, "db3:", sizeof("db3:") - 1) == 0)
201 new_name += sizeof("db3:") - 1;
202
203 ret = asprintf(&old, "%s.db", db->hdb_name);
204 if (ret == -1)
205 return ENOMEM;
206 ret = asprintf(&new, "%s.db", new_name);
207 if (ret == -1) {
208 free(old);
209 return ENOMEM;
210 }
211 ret = rename(old, new);
212 free(old);
213 if(ret) {
214 free(new);
215 return errno;
216 }
217
218 free(db->hdb_name);
219 new[strlen(new) - 3] = '\0';
220 db->hdb_name = new;
221 return 0;
222 }
223
224 static krb5_error_code
225 DB__get(krb5_context context, HDB *db, krb5_data key, krb5_data *reply)
226 {
227 DB *d = (DB*)db->hdb_db;
228 DBT k, v;
229 int code;
230
231 memset(&k, 0, sizeof(DBT));
232 memset(&v, 0, sizeof(DBT));
233 k.data = key.data;
234 k.size = key.length;
235 k.flags = 0;
236 code = (*d->get)(d, NULL, &k, &v, 0);
237 if(code == DB_NOTFOUND)
238 return HDB_ERR_NOENTRY;
239 if(code)
240 return code;
241
242 krb5_data_copy(reply, v.data, v.size);
243 return 0;
244 }
245
246 static krb5_error_code
247 DB__put(krb5_context context, HDB *db, int replace,
248 krb5_data key, krb5_data value)
249 {
250 DB3_HDB *db3 = (DB3_HDB *)db;
251 DB *d = (DB*)db->hdb_db;
252 DBT k, v;
253 int code;
254
255 memset(&k, 0, sizeof(DBT));
256 memset(&v, 0, sizeof(DBT));
257 k.data = key.data;
258 k.size = key.length;
259 k.flags = 0;
260 v.data = value.data;
261 v.size = value.length;
262 v.flags = 0;
263 code = (*d->put)(d, NULL, &k, &v, replace ? 0 : DB_NOOVERWRITE);
264 if(code == DB_KEYEXIST)
265 return HDB_ERR_EXISTS;
266 if (code) {
267 /*
268 * Berkeley DB 3 and up have a terrible error reporting
269 * interface...
270 *
271 * DB->err() doesn't output a string.
272 * DB->set_errcall()'s callback function doesn't have a void *
273 * argument that can be used to place the error somewhere.
274 *
275 * The only thing we could do is fopen()/fdopen() a file, set it
276 * with DB->set_errfile(), then call DB->err(), then read the
277 * message from the file, unset it with DB->set_errfile(), close
278 * it and delete it. That's a lot of work... so we don't do it.
279 */
280 if (code == EACCES || code == ENOSPC || code == EINVAL) {
281 krb5_set_error_message(context, code,
282 "Database %s put error: %s",
283 db->hdb_name, strerror(code));
284 } else {
285 code = HDB_ERR_UK_SERROR;
286 krb5_set_error_message(context, code,
287 "Database %s put error: unknown (%d)",
288 db->hdb_name, code);
289 }
290 return code;
291 }
292 return db->hdb_set_sync(context, db, db3->do_sync);
293 }
294
295 static krb5_error_code
296 DB__del(krb5_context context, HDB *db, krb5_data key)
297 {
298 DB3_HDB *db3 = (DB3_HDB *)db;
299 DB *d = (DB*)db->hdb_db;
300 DBT k;
301 krb5_error_code code;
302 memset(&k, 0, sizeof(DBT));
303 k.data = key.data;
304 k.size = key.length;
305 k.flags = 0;
306 code = (*d->del)(d, NULL, &k, 0);
307 if(code == DB_NOTFOUND)
308 return HDB_ERR_NOENTRY;
309 if (code) {
310 if (code == EACCES || code == ENOSPC || code == EINVAL) {
311 krb5_set_error_message(context, code,
312 "Database %s del error: %s",
313 db->hdb_name, strerror(code));
314 } else {
315 code = HDB_ERR_UK_SERROR;
316 krb5_set_error_message(context, code,
317 "Database %s del error: unknown (%d)",
318 db->hdb_name, code);
319 }
320 return code;
321 }
322 return db->hdb_set_sync(context, db, db3->do_sync);
323 }
324
325 #define RD_CACHE_SZ 0x8000 /* Minimal read cache size */
326 #define WR_CACHE_SZ 0x8000 /* Minimal write cache size */
327
328 static int
329 _open_db(DB *d, char *fn, int myflags, int flags, mode_t mode, int *fd)
330 {
331 int ret;
332 int cache_size = (myflags & DB_RDONLY) ? RD_CACHE_SZ : WR_CACHE_SZ;
333
334 *fd = open(fn, flags, mode);
335
336 if (*fd == -1)
337 return errno;
338
339 /*
340 * Without DB_FCNTL_LOCKING, the DB library complains when initializing
341 * a database in an empty file. Since the database is our lock file,
342 * we create it before Berkeley DB does, so a new DB always starts empty.
343 */
344 myflags |= DB_FCNTL_LOCKING;
345
346 ret = flock(*fd, (myflags&DB_RDONLY) ? LOCK_SH : LOCK_EX);
347 if (ret == -1) {
348 ret = errno;
349 close(*fd);
350 *fd = -1;
351 return ret;
352 }
353
354 d->set_cachesize(d, 0, cache_size, 0);
355
356 #if (DB_VERSION_MAJOR > 4) || ((DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR >= 1))
357 ret = (*d->open)(d, NULL, fn, NULL, DB_BTREE, myflags, mode);
358 #else
359 ret = (*d->open)(d, fn, NULL, DB_BTREE, myflags, mode);
360 #endif
361
362 if (ret != 0) {
363 close(*fd);
364 *fd = -1;
365 }
366
367 return ret;
368 }
369
370 static krb5_error_code
371 DB_open(krb5_context context, HDB *db, int flags, mode_t mode)
372 {
373 DB3_HDB *db3 = (DB3_HDB *)db;
374 DBC *dbc = NULL;
375 char *fn;
376 krb5_error_code ret;
377 DB *d;
378 int myflags = 0;
379 int aret;
380
381 heim_assert(db->hdb_db == 0, "Opening already open HDB");
382
383 if (flags & O_CREAT)
384 myflags |= DB_CREATE;
385
386 if (flags & O_EXCL)
387 myflags |= DB_EXCL;
388
389 if((flags & O_ACCMODE) == O_RDONLY)
390 myflags |= DB_RDONLY;
391
392 if (flags & O_TRUNC)
393 myflags |= DB_TRUNCATE;
394
395 aret = asprintf(&fn, "%s.db", db->hdb_name);
396 if (aret == -1) {
397 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
398 return ENOMEM;
399 }
400
401 if (db_create(&d, NULL, 0) != 0) {
402 free(fn);
403 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
404 return ENOMEM;
405 }
406 db->hdb_db = d;
407
408 /* From here on out always DB_close() before returning on error */
409
410 ret = _open_db(d, fn, myflags, flags, mode, &db3->lock_fd);
411 free(fn);
412 if (ret == ENOENT) {
413 /* try to open without .db extension */
414 ret = _open_db(d, db->hdb_name, myflags, flags, mode, &db3->lock_fd);
415 }
416
417 if (ret) {
418 DB_close(context, db);
419 krb5_set_error_message(context, ret, "opening %s: %s",
420 db->hdb_name, strerror(ret));
421 return ret;
422 }
423
424 #ifndef DB_CURSOR_BULK
425 # define DB_CURSOR_BULK 0 /* Missing with DB < 4.8 */
426 #endif
427 ret = (*d->cursor)(d, NULL, &dbc, DB_CURSOR_BULK);
428
429 if (ret) {
430 DB_close(context, db);
431 krb5_set_error_message(context, ret, "d->cursor: %s", strerror(ret));
432 return ret;
433 }
434 db->hdb_dbc = dbc;
435
436 if((flags & O_ACCMODE) == O_RDONLY)
437 ret = hdb_check_db_format(context, db);
438 else
439 ret = hdb_init_db(context, db);
440 if(ret == HDB_ERR_NOENTRY)
441 return 0;
442 if (ret) {
443 DB_close(context, db);
444 krb5_set_error_message(context, ret, "hdb_open: failed %s database %s",
445 (flags & O_ACCMODE) == O_RDONLY ?
446 "checking format of" : "initialize",
447 db->hdb_name);
448 }
449
450 return ret;
451 }
452
453 krb5_error_code
454 hdb_db3_create(krb5_context context, HDB **db,
455 const char *filename)
456 {
457 DB3_HDB **db3 = (DB3_HDB **)db;
458 *db3 = calloc(1, sizeof(**db3)); /* Allocate space for the larger db3 */
459 if (*db == NULL) {
460 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
461 return ENOMEM;
462 }
463
464 (*db)->hdb_db = NULL;
465 (*db)->hdb_name = strdup(filename);
466 if ((*db)->hdb_name == NULL) {
467 free(*db);
468 *db = NULL;
469 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
470 return ENOMEM;
471 }
472 (*db)->hdb_master_key_set = 0;
473 (*db)->hdb_openp = 0;
474 (*db)->hdb_capability_flags = HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL;
475 (*db)->hdb_open = DB_open;
476 (*db)->hdb_close = DB_close;
477 (*db)->hdb_fetch_kvno = _hdb_fetch_kvno;
478 (*db)->hdb_store = _hdb_store;
479 (*db)->hdb_remove = _hdb_remove;
480 (*db)->hdb_firstkey = DB_firstkey;
481 (*db)->hdb_nextkey= DB_nextkey;
482 (*db)->hdb_lock = DB_lock;
483 (*db)->hdb_unlock = DB_unlock;
484 (*db)->hdb_rename = DB_rename;
485 (*db)->hdb__get = DB__get;
486 (*db)->hdb__put = DB__put;
487 (*db)->hdb__del = DB__del;
488 (*db)->hdb_destroy = DB_destroy;
489 (*db)->hdb_set_sync = DB_set_sync;
490
491 (*db3)->lock_fd = -1;
492 return 0;
493 }
494 #endif /* HAVE_DB3 */