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
53 HDB hdb
; /* generic members */
54 int lock_fd
; /* DB3-specific */
58 static krb5_error_code
59 DB_close(krb5_context context
, HDB
*db
)
61 DB3_HDB
*db3
= (DB3_HDB
*)db
;
62 DB
*d
= (DB
*)db
->hdb_db
;
65 heim_assert(d
!= 0, "Closing already closed HDB");
67 dbcp
= (DBC
*)db
->hdb_dbc
;
68 (*dbcp
->c_close
)(dbcp
);
73 if (db3
->lock_fd
>= 0) {
81 static krb5_error_code
82 DB_destroy(krb5_context context
, HDB
*db
)
86 ret
= hdb_clear_master_key (context
, db
);
92 static krb5_error_code
93 DB_lock(krb5_context context
, HDB
*db
, int operation
)
99 static krb5_error_code
100 DB_unlock(krb5_context context
, HDB
*db
)
107 static krb5_error_code
108 DB_seq(krb5_context context
, HDB
*db
,
109 unsigned flags
, hdb_entry_ex
*entry
, int flag
)
112 DBC
*dbcp
= db
->hdb_dbc
;
113 krb5_data key_data
, data
;
116 memset(&key
, 0, sizeof(DBT
));
117 memset(&value
, 0, sizeof(DBT
));
118 code
= (*dbcp
->c_get
)(dbcp
, &key
, &value
, flag
);
119 if (code
== DB_NOTFOUND
)
120 return HDB_ERR_NOENTRY
;
124 key_data
.data
= key
.data
;
125 key_data
.length
= key
.size
;
126 data
.data
= value
.data
;
127 data
.length
= value
.size
;
128 memset(entry
, 0, sizeof(*entry
));
129 if (hdb_value2entry(context
, &data
, &entry
->entry
))
130 return DB_seq(context
, db
, flags
, entry
, DB_NEXT
);
131 if (db
->hdb_master_key_set
&& (flags
& HDB_F_DECRYPT
)) {
132 code
= hdb_unseal_keys (context
, db
, &entry
->entry
);
134 hdb_free_entry (context
, entry
);
136 if (entry
->entry
.principal
== NULL
) {
137 entry
->entry
.principal
= malloc(sizeof(*entry
->entry
.principal
));
138 if (entry
->entry
.principal
== NULL
) {
139 hdb_free_entry (context
, entry
);
140 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
143 hdb_key2principal(context
, &key_data
, entry
->entry
.principal
);
150 static krb5_error_code
151 DB_firstkey(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
153 return DB_seq(context
, db
, flags
, entry
, DB_FIRST
);
157 static krb5_error_code
158 DB_nextkey(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
160 return DB_seq(context
, db
, flags
, entry
, DB_NEXT
);
163 static krb5_error_code
164 DB_rename(krb5_context context
, HDB
*db
, const char *new_name
)
169 ret
= asprintf(&old
, "%s.db", db
->hdb_name
);
172 ret
= asprintf(&new, "%s.db", new_name
);
177 ret
= rename(old
, new);
185 new[strlen(new) - 3] = '\0';
190 static krb5_error_code
191 DB__get(krb5_context context
, HDB
*db
, krb5_data key
, krb5_data
*reply
)
193 DB
*d
= (DB
*)db
->hdb_db
;
197 memset(&k
, 0, sizeof(DBT
));
198 memset(&v
, 0, sizeof(DBT
));
202 code
= (*d
->get
)(d
, NULL
, &k
, &v
, 0);
203 if(code
== DB_NOTFOUND
)
204 return HDB_ERR_NOENTRY
;
208 krb5_data_copy(reply
, v
.data
, v
.size
);
212 static krb5_error_code
213 DB__put(krb5_context context
, HDB
*db
, int replace
,
214 krb5_data key
, krb5_data value
)
216 DB
*d
= (DB
*)db
->hdb_db
;
220 memset(&k
, 0, sizeof(DBT
));
221 memset(&v
, 0, sizeof(DBT
));
226 v
.size
= value
.length
;
228 code
= (*d
->put
)(d
, NULL
, &k
, &v
, replace
? 0 : DB_NOOVERWRITE
);
229 if(code
== DB_KEYEXIST
)
230 return HDB_ERR_EXISTS
;
236 static krb5_error_code
237 DB__del(krb5_context context
, HDB
*db
, krb5_data key
)
239 DB
*d
= (DB
*)db
->hdb_db
;
241 krb5_error_code code
;
242 memset(&k
, 0, sizeof(DBT
));
246 code
= (*d
->del
)(d
, NULL
, &k
, 0);
247 if(code
== DB_NOTFOUND
)
248 return HDB_ERR_NOENTRY
;
254 #define RD_CACHE_SZ 0x8000 /* Minimal read cache size */
255 #define WR_CACHE_SZ 0x8000 /* Minimal write cache size */
258 _open_db(DB
*d
, char *fn
, int myflags
, int flags
, mode_t mode
, int *fd
)
261 int cache_size
= (myflags
& DB_RDONLY
) ? RD_CACHE_SZ
: WR_CACHE_SZ
;
263 *fd
= open(fn
, flags
, mode
);
269 * Without DB_FCNTL_LOCKING, the DB library complains when initializing
270 * a database in an empty file. Since the database is our lock file,
271 * we create it before Berkeley DB does, so a new DB always starts empty.
273 myflags
|= DB_FCNTL_LOCKING
;
275 ret
= flock(*fd
, (myflags
&DB_RDONLY
) ? LOCK_SH
: LOCK_EX
);
282 d
->set_cachesize(d
, 0, cache_size
, 0);
284 #if (DB_VERSION_MAJOR >= 4) && (DB_VERSION_MINOR >= 1)
285 ret
= (*d
->open
)(d
, NULL
, fn
, NULL
, DB_BTREE
, myflags
, mode
);
287 ret
= (*d
->open
)(d
, fn
, NULL
, DB_BTREE
, myflags
, mode
);
296 static krb5_error_code
297 DB_open(krb5_context context
, HDB
*db
, int flags
, mode_t mode
)
299 DB3_HDB
*db3
= (DB3_HDB
*)db
;
307 heim_assert(db
->hdb_db
== 0, "Opening already open HDB");
310 myflags
|= DB_CREATE
;
315 if((flags
& O_ACCMODE
) == O_RDONLY
)
316 myflags
|= DB_RDONLY
;
319 myflags
|= DB_TRUNCATE
;
321 aret
= asprintf(&fn
, "%s.db", db
->hdb_name
);
323 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
327 if (db_create(&d
, NULL
, 0) != 0) {
329 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
334 ret
= _open_db(d
, fn
, myflags
, flags
, mode
, &db3
->lock_fd
);
337 /* try to open without .db extension */
338 ret
= _open_db(d
, db
->hdb_name
, myflags
, flags
,
339 mode
, &db3
->lock_fd
);
343 krb5_set_error_message(context
, ret
, "opening %s: %s",
344 db
->hdb_name
, strerror(ret
));
348 #ifndef DB_CURSOR_BULK
349 # define DB_CURSOR_BULK 0 /* Missing with DB < 4.8 */
351 ret
= (*d
->cursor
)(d
, NULL
, &dbc
, DB_CURSOR_BULK
);
354 krb5_set_error_message(context
, ret
, "d->cursor: %s", strerror(ret
));
359 if((flags
& O_ACCMODE
) == O_RDONLY
)
360 ret
= hdb_check_db_format(context
, db
);
362 ret
= hdb_init_db(context
, db
);
363 if(ret
== HDB_ERR_NOENTRY
)
366 DB_close(context
, db
);
367 krb5_set_error_message(context
, ret
, "hdb_open: failed %s database %s",
368 (flags
& O_ACCMODE
) == O_RDONLY
?
369 "checking format of" : "initialize",
377 hdb_db_create(krb5_context context
, HDB
**db
,
378 const char *filename
)
380 DB3_HDB
**db3
= (DB3_HDB
**)db
;
381 *db
= calloc(1, sizeof(**db3
)); /* Allocate space for the larger db3 */
383 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
387 (*db
)->hdb_db
= NULL
;
388 (*db
)->hdb_name
= strdup(filename
);
389 if ((*db
)->hdb_name
== NULL
) {
392 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
395 (*db
)->hdb_master_key_set
= 0;
396 (*db
)->hdb_openp
= 0;
397 (*db
)->hdb_capability_flags
= HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL
;
398 (*db
)->hdb_open
= DB_open
;
399 (*db
)->hdb_close
= DB_close
;
400 (*db
)->hdb_fetch_kvno
= _hdb_fetch_kvno
;
401 (*db
)->hdb_store
= _hdb_store
;
402 (*db
)->hdb_remove
= _hdb_remove
;
403 (*db
)->hdb_firstkey
= DB_firstkey
;
404 (*db
)->hdb_nextkey
= DB_nextkey
;
405 (*db
)->hdb_lock
= DB_lock
;
406 (*db
)->hdb_unlock
= DB_unlock
;
407 (*db
)->hdb_rename
= DB_rename
;
408 (*db
)->hdb__get
= DB__get
;
409 (*db
)->hdb__put
= DB__put
;
410 (*db
)->hdb__del
= DB__del
;
411 (*db
)->hdb_destroy
= DB_destroy
;
413 (*db3
)->lock_fd
= -1;
416 #endif /* HAVE_DB3 */