2 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
55 HDB hdb
; /* generic members */
56 int lock_fd
; /* DB3-specific */
57 int do_sync
; /* DB3-specific */
61 static krb5_error_code
62 DB_close(krb5_context context
, HDB
*db
)
64 DB3_HDB
*db3
= (DB3_HDB
*)db
;
65 DB
*d
= (DB
*)db
->hdb_db
;
66 DBC
*dbcp
= (DBC
*)db
->hdb_dbc
;
68 heim_assert(d
!= 0, "Closing already closed HDB");
74 if (db3
->lock_fd
>= 0)
84 static krb5_error_code
85 DB_destroy(krb5_context context
, HDB
*db
)
89 ret
= hdb_clear_master_key(context
, db
);
90 krb5_config_free_strings(db
->virtual_hostbased_princ_svcs
);
96 static krb5_error_code
97 DB_set_sync(krb5_context context
, HDB
*db
, int on
)
99 DB3_HDB
*db3
= (DB3_HDB
*)db
;
100 DB
*d
= (DB
*)db
->hdb_db
;
101 krb5_error_code ret
= 0;
105 ret
= (*d
->sync
)(d
, 0);
107 if (ret
== EACCES
|| ret
== ENOSPC
|| ret
== EINVAL
) {
108 krb5_set_error_message(context
, ret
,
109 "Database %s put sync error: %s",
110 db
->hdb_name
, strerror(ret
));
112 ret
= HDB_ERR_UK_SERROR
;
113 krb5_set_error_message(context
, ret
,
114 "Database %s put sync error: unknown (%d)",
122 static krb5_error_code
123 DB_lock(krb5_context context
, HDB
*db
, int operation
)
129 static krb5_error_code
130 DB_unlock(krb5_context context
, HDB
*db
)
137 static krb5_error_code
138 DB_seq(krb5_context context
, HDB
*db
,
139 unsigned flags
, hdb_entry
*entry
, int flag
)
142 DBC
*dbcp
= db
->hdb_dbc
;
143 krb5_data key_data
, data
;
146 memset(&key
, 0, sizeof(DBT
));
147 memset(&value
, 0, sizeof(DBT
));
148 code
= (*dbcp
->c_get
)(dbcp
, &key
, &value
, flag
);
149 if (code
== DB_NOTFOUND
)
150 return HDB_ERR_NOENTRY
;
154 key_data
.data
= key
.data
;
155 key_data
.length
= key
.size
;
156 data
.data
= value
.data
;
157 data
.length
= value
.size
;
158 memset(entry
, 0, sizeof(*entry
));
159 if (hdb_value2entry(context
, &data
, entry
))
160 return DB_seq(context
, db
, flags
, entry
, DB_NEXT
);
161 if (db
->hdb_master_key_set
&& (flags
& HDB_F_DECRYPT
)) {
162 code
= hdb_unseal_keys (context
, db
, entry
);
164 hdb_free_entry (context
, db
, entry
);
166 if (entry
->principal
== NULL
) {
167 entry
->principal
= malloc(sizeof(*entry
->principal
));
168 if (entry
->principal
== NULL
) {
169 hdb_free_entry (context
, db
, entry
);
170 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
173 hdb_key2principal(context
, &key_data
, entry
->principal
);
180 static krb5_error_code
181 DB_firstkey(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry
*entry
)
183 return DB_seq(context
, db
, flags
, entry
, DB_FIRST
);
187 static krb5_error_code
188 DB_nextkey(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry
*entry
)
190 return DB_seq(context
, db
, flags
, entry
, DB_NEXT
);
193 static krb5_error_code
194 DB_rename(krb5_context context
, HDB
*db
, const char *new_name
)
199 if (strncmp(new_name
, "db:", sizeof("db:") - 1) == 0)
200 new_name
+= sizeof("db:") - 1;
201 else if (strncmp(new_name
, "db3:", sizeof("db3:") - 1) == 0)
202 new_name
+= sizeof("db3:") - 1;
204 ret
= asprintf(&old
, "%s.db", db
->hdb_name
);
207 ret
= asprintf(&new, "%s.db", new_name
);
212 ret
= rename(old
, new);
220 new[strlen(new) - 3] = '\0';
225 static krb5_error_code
226 DB__get(krb5_context context
, HDB
*db
, krb5_data key
, krb5_data
*reply
)
228 DB
*d
= (DB
*)db
->hdb_db
;
232 memset(&k
, 0, sizeof(DBT
));
233 memset(&v
, 0, sizeof(DBT
));
237 code
= (*d
->get
)(d
, NULL
, &k
, &v
, 0);
238 if(code
== DB_NOTFOUND
)
239 return HDB_ERR_NOENTRY
;
243 krb5_data_copy(reply
, v
.data
, v
.size
);
247 static krb5_error_code
248 DB__put(krb5_context context
, HDB
*db
, int replace
,
249 krb5_data key
, krb5_data value
)
251 DB3_HDB
*db3
= (DB3_HDB
*)db
;
252 DB
*d
= (DB
*)db
->hdb_db
;
256 memset(&k
, 0, sizeof(DBT
));
257 memset(&v
, 0, sizeof(DBT
));
262 v
.size
= value
.length
;
264 code
= (*d
->put
)(d
, NULL
, &k
, &v
, replace
? 0 : DB_NOOVERWRITE
);
265 if(code
== DB_KEYEXIST
)
266 return HDB_ERR_EXISTS
;
269 * Berkeley DB 3 and up have a terrible error reporting
272 * DB->err() doesn't output a string.
273 * DB->set_errcall()'s callback function doesn't have a void *
274 * argument that can be used to place the error somewhere.
276 * The only thing we could do is fopen()/fdopen() a file, set it
277 * with DB->set_errfile(), then call DB->err(), then read the
278 * message from the file, unset it with DB->set_errfile(), close
279 * it and delete it. That's a lot of work... so we don't do it.
281 if (code
== EACCES
|| code
== ENOSPC
|| code
== EINVAL
) {
282 krb5_set_error_message(context
, code
,
283 "Database %s put error: %s",
284 db
->hdb_name
, strerror(code
));
286 code
= HDB_ERR_UK_SERROR
;
287 krb5_set_error_message(context
, code
,
288 "Database %s put error: unknown (%d)",
293 return db
->hdb_set_sync(context
, db
, db3
->do_sync
);
296 static krb5_error_code
297 DB__del(krb5_context context
, HDB
*db
, krb5_data key
)
299 DB3_HDB
*db3
= (DB3_HDB
*)db
;
300 DB
*d
= (DB
*)db
->hdb_db
;
302 krb5_error_code code
;
303 memset(&k
, 0, sizeof(DBT
));
307 code
= (*d
->del
)(d
, NULL
, &k
, 0);
308 if(code
== DB_NOTFOUND
)
309 return HDB_ERR_NOENTRY
;
311 if (code
== EACCES
|| code
== ENOSPC
|| code
== EINVAL
) {
312 krb5_set_error_message(context
, code
,
313 "Database %s del error: %s",
314 db
->hdb_name
, strerror(code
));
316 code
= HDB_ERR_UK_SERROR
;
317 krb5_set_error_message(context
, code
,
318 "Database %s del error: unknown (%d)",
323 return db
->hdb_set_sync(context
, db
, db3
->do_sync
);
326 #define RD_CACHE_SZ 0x8000 /* Minimal read cache size */
327 #define WR_CACHE_SZ 0x8000 /* Minimal write cache size */
330 _open_db(DB
*d
, char *fn
, int myflags
, int flags
, mode_t mode
, int *fd
)
333 int cache_size
= (myflags
& DB_RDONLY
) ? RD_CACHE_SZ
: WR_CACHE_SZ
;
335 *fd
= open(fn
, flags
, mode
);
341 * Without DB_FCNTL_LOCKING, the DB library complains when initializing
342 * a database in an empty file. Since the database is our lock file,
343 * we create it before Berkeley DB does, so a new DB always starts empty.
345 myflags
|= DB_FCNTL_LOCKING
;
347 ret
= flock(*fd
, (myflags
&DB_RDONLY
) ? LOCK_SH
: LOCK_EX
);
355 d
->set_cachesize(d
, 0, cache_size
, 0);
357 #if (DB_VERSION_MAJOR > 4) || ((DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR >= 1))
358 ret
= (*d
->open
)(d
, NULL
, fn
, NULL
, DB_BTREE
, myflags
, mode
);
360 ret
= (*d
->open
)(d
, fn
, NULL
, DB_BTREE
, myflags
, mode
);
371 static krb5_error_code
372 DB_open(krb5_context context
, HDB
*db
, int flags
, mode_t mode
)
374 DB3_HDB
*db3
= (DB3_HDB
*)db
;
382 heim_assert(db
->hdb_db
== 0, "Opening already open HDB");
385 myflags
|= DB_CREATE
;
390 if((flags
& O_ACCMODE
) == O_RDONLY
)
391 myflags
|= DB_RDONLY
;
394 myflags
|= DB_TRUNCATE
;
396 aret
= asprintf(&fn
, "%s.db", db
->hdb_name
);
398 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
402 if (db_create(&d
, NULL
, 0) != 0) {
404 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
409 /* From here on out always DB_close() before returning on error */
411 ret
= _open_db(d
, fn
, myflags
, flags
, mode
, &db3
->lock_fd
);
414 /* try to open without .db extension */
415 ret
= _open_db(d
, db
->hdb_name
, myflags
, flags
, mode
, &db3
->lock_fd
);
419 DB_close(context
, db
);
420 krb5_set_error_message(context
, ret
, "opening %s: %s",
421 db
->hdb_name
, strerror(ret
));
425 #ifndef DB_CURSOR_BULK
426 # define DB_CURSOR_BULK 0 /* Missing with DB < 4.8 */
428 ret
= (*d
->cursor
)(d
, NULL
, &dbc
, DB_CURSOR_BULK
);
431 DB_close(context
, db
);
432 krb5_set_error_message(context
, ret
, "d->cursor: %s", strerror(ret
));
437 if((flags
& O_ACCMODE
) == O_RDONLY
)
438 ret
= hdb_check_db_format(context
, db
);
440 ret
= hdb_init_db(context
, db
);
441 if(ret
== HDB_ERR_NOENTRY
)
444 DB_close(context
, db
);
445 krb5_set_error_message(context
, ret
, "hdb_open: failed %s database %s",
446 (flags
& O_ACCMODE
) == O_RDONLY
?
447 "checking format of" : "initialize",
455 hdb_db3_create(krb5_context context
, HDB
**db
,
456 const char *filename
)
458 DB3_HDB
**db3
= (DB3_HDB
**)db
;
459 *db3
= calloc(1, sizeof(**db3
)); /* Allocate space for the larger db3 */
461 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
465 (*db
)->hdb_db
= NULL
;
466 (*db
)->hdb_name
= strdup(filename
);
467 if ((*db
)->hdb_name
== NULL
) {
470 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
473 (*db
)->hdb_master_key_set
= 0;
474 (*db
)->hdb_openp
= 0;
475 (*db
)->hdb_capability_flags
= HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL
;
476 (*db
)->hdb_open
= DB_open
;
477 (*db
)->hdb_close
= DB_close
;
478 (*db
)->hdb_fetch_kvno
= _hdb_fetch_kvno
;
479 (*db
)->hdb_store
= _hdb_store
;
480 (*db
)->hdb_remove
= _hdb_remove
;
481 (*db
)->hdb_firstkey
= DB_firstkey
;
482 (*db
)->hdb_nextkey
= DB_nextkey
;
483 (*db
)->hdb_lock
= DB_lock
;
484 (*db
)->hdb_unlock
= DB_unlock
;
485 (*db
)->hdb_rename
= DB_rename
;
486 (*db
)->hdb__get
= DB__get
;
487 (*db
)->hdb__put
= DB__put
;
488 (*db
)->hdb__del
= DB__del
;
489 (*db
)->hdb_destroy
= DB_destroy
;
490 (*db
)->hdb_set_sync
= DB_set_sync
;
492 (*db3
)->lock_fd
= -1;
495 #endif /* HAVE_DB3 */