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 */
60 static krb5_error_code
61 DB_close(krb5_context context
, HDB
*db
)
63 DB3_HDB
*db3
= (DB3_HDB
*)db
;
64 DB
*d
= (DB
*)db
->hdb_db
;
65 DBC
*dbcp
= (DBC
*)db
->hdb_dbc
;
67 heim_assert(d
!= 0, "Closing already closed HDB");
73 if (db3
->lock_fd
>= 0)
83 static krb5_error_code
84 DB_destroy(krb5_context context
, HDB
*db
)
88 ret
= hdb_clear_master_key (context
, db
);
94 static krb5_error_code
95 DB_lock(krb5_context context
, HDB
*db
, int operation
)
101 static krb5_error_code
102 DB_unlock(krb5_context context
, HDB
*db
)
109 static krb5_error_code
110 DB_seq(krb5_context context
, HDB
*db
,
111 unsigned flags
, hdb_entry_ex
*entry
, int flag
)
114 DBC
*dbcp
= db
->hdb_dbc
;
115 krb5_data key_data
, data
;
118 memset(&key
, 0, sizeof(DBT
));
119 memset(&value
, 0, sizeof(DBT
));
120 code
= (*dbcp
->c_get
)(dbcp
, &key
, &value
, flag
);
121 if (code
== DB_NOTFOUND
)
122 return HDB_ERR_NOENTRY
;
126 key_data
.data
= key
.data
;
127 key_data
.length
= key
.size
;
128 data
.data
= value
.data
;
129 data
.length
= value
.size
;
130 memset(entry
, 0, sizeof(*entry
));
131 if (hdb_value2entry(context
, &data
, &entry
->entry
))
132 return DB_seq(context
, db
, flags
, entry
, DB_NEXT
);
133 if (db
->hdb_master_key_set
&& (flags
& HDB_F_DECRYPT
)) {
134 code
= hdb_unseal_keys (context
, db
, &entry
->entry
);
136 hdb_free_entry (context
, entry
);
138 if (entry
->entry
.principal
== NULL
) {
139 entry
->entry
.principal
= malloc(sizeof(*entry
->entry
.principal
));
140 if (entry
->entry
.principal
== NULL
) {
141 hdb_free_entry (context
, entry
);
142 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
145 hdb_key2principal(context
, &key_data
, entry
->entry
.principal
);
152 static krb5_error_code
153 DB_firstkey(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
155 return DB_seq(context
, db
, flags
, entry
, DB_FIRST
);
159 static krb5_error_code
160 DB_nextkey(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
162 return DB_seq(context
, db
, flags
, entry
, DB_NEXT
);
165 static krb5_error_code
166 DB_rename(krb5_context context
, HDB
*db
, const char *new_name
)
171 ret
= asprintf(&old
, "%s.db", db
->hdb_name
);
174 ret
= asprintf(&new, "%s.db", new_name
);
179 ret
= rename(old
, new);
187 new[strlen(new) - 3] = '\0';
192 static krb5_error_code
193 DB__get(krb5_context context
, HDB
*db
, krb5_data key
, krb5_data
*reply
)
195 DB
*d
= (DB
*)db
->hdb_db
;
199 memset(&k
, 0, sizeof(DBT
));
200 memset(&v
, 0, sizeof(DBT
));
204 code
= (*d
->get
)(d
, NULL
, &k
, &v
, 0);
205 if(code
== DB_NOTFOUND
)
206 return HDB_ERR_NOENTRY
;
210 krb5_data_copy(reply
, v
.data
, v
.size
);
214 static krb5_error_code
215 DB__put(krb5_context context
, HDB
*db
, int replace
,
216 krb5_data key
, krb5_data value
)
218 DB
*d
= (DB
*)db
->hdb_db
;
222 memset(&k
, 0, sizeof(DBT
));
223 memset(&v
, 0, sizeof(DBT
));
228 v
.size
= value
.length
;
230 code
= (*d
->put
)(d
, NULL
, &k
, &v
, replace
? 0 : DB_NOOVERWRITE
);
231 if(code
== DB_KEYEXIST
)
232 return HDB_ERR_EXISTS
;
238 static krb5_error_code
239 DB__del(krb5_context context
, HDB
*db
, krb5_data key
)
241 DB
*d
= (DB
*)db
->hdb_db
;
243 krb5_error_code code
;
244 memset(&k
, 0, sizeof(DBT
));
248 code
= (*d
->del
)(d
, NULL
, &k
, 0);
249 if(code
== DB_NOTFOUND
)
250 return HDB_ERR_NOENTRY
;
256 #define RD_CACHE_SZ 0x8000 /* Minimal read cache size */
257 #define WR_CACHE_SZ 0x8000 /* Minimal write cache size */
260 _open_db(DB
*d
, char *fn
, int myflags
, int flags
, mode_t mode
, int *fd
)
263 int cache_size
= (myflags
& DB_RDONLY
) ? RD_CACHE_SZ
: WR_CACHE_SZ
;
265 *fd
= open(fn
, flags
, mode
);
271 * Without DB_FCNTL_LOCKING, the DB library complains when initializing
272 * a database in an empty file. Since the database is our lock file,
273 * we create it before Berkeley DB does, so a new DB always starts empty.
275 myflags
|= DB_FCNTL_LOCKING
;
277 ret
= flock(*fd
, (myflags
&DB_RDONLY
) ? LOCK_SH
: LOCK_EX
);
285 d
->set_cachesize(d
, 0, cache_size
, 0);
287 #if (DB_VERSION_MAJOR > 4) || ((DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR >= 1))
288 ret
= (*d
->open
)(d
, NULL
, fn
, NULL
, DB_BTREE
, myflags
, mode
);
290 ret
= (*d
->open
)(d
, fn
, NULL
, DB_BTREE
, myflags
, mode
);
301 static krb5_error_code
302 DB_open(krb5_context context
, HDB
*db
, int flags
, mode_t mode
)
304 DB3_HDB
*db3
= (DB3_HDB
*)db
;
312 heim_assert(db
->hdb_db
== 0, "Opening already open HDB");
315 myflags
|= DB_CREATE
;
320 if((flags
& O_ACCMODE
) == O_RDONLY
)
321 myflags
|= DB_RDONLY
;
324 myflags
|= DB_TRUNCATE
;
326 aret
= asprintf(&fn
, "%s.db", db
->hdb_name
);
328 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
332 if (db_create(&d
, NULL
, 0) != 0) {
334 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
339 /* From here on out always DB_close() before returning on error */
341 ret
= _open_db(d
, fn
, myflags
, flags
, mode
, &db3
->lock_fd
);
344 /* try to open without .db extension */
345 ret
= _open_db(d
, db
->hdb_name
, myflags
, flags
, mode
, &db3
->lock_fd
);
349 DB_close(context
, db
);
350 krb5_set_error_message(context
, ret
, "opening %s: %s",
351 db
->hdb_name
, strerror(ret
));
355 #ifndef DB_CURSOR_BULK
356 # define DB_CURSOR_BULK 0 /* Missing with DB < 4.8 */
358 ret
= (*d
->cursor
)(d
, NULL
, &dbc
, DB_CURSOR_BULK
);
361 DB_close(context
, db
);
362 krb5_set_error_message(context
, ret
, "d->cursor: %s", strerror(ret
));
367 if((flags
& O_ACCMODE
) == O_RDONLY
)
368 ret
= hdb_check_db_format(context
, db
);
370 ret
= hdb_init_db(context
, db
);
371 if(ret
== HDB_ERR_NOENTRY
)
374 DB_close(context
, db
);
375 krb5_set_error_message(context
, ret
, "hdb_open: failed %s database %s",
376 (flags
& O_ACCMODE
) == O_RDONLY
?
377 "checking format of" : "initialize",
385 hdb_db_create(krb5_context context
, HDB
**db
,
386 const char *filename
)
388 DB3_HDB
**db3
= (DB3_HDB
**)db
;
389 *db
= calloc(1, sizeof(**db3
)); /* Allocate space for the larger db3 */
391 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
395 (*db
)->hdb_db
= NULL
;
396 (*db
)->hdb_name
= strdup(filename
);
397 if ((*db
)->hdb_name
== NULL
) {
400 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
403 (*db
)->hdb_master_key_set
= 0;
404 (*db
)->hdb_openp
= 0;
405 (*db
)->hdb_capability_flags
= HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL
;
406 (*db
)->hdb_open
= DB_open
;
407 (*db
)->hdb_close
= DB_close
;
408 (*db
)->hdb_fetch_kvno
= _hdb_fetch_kvno
;
409 (*db
)->hdb_store
= _hdb_store
;
410 (*db
)->hdb_remove
= _hdb_remove
;
411 (*db
)->hdb_firstkey
= DB_firstkey
;
412 (*db
)->hdb_nextkey
= DB_nextkey
;
413 (*db
)->hdb_lock
= DB_lock
;
414 (*db
)->hdb_unlock
= DB_unlock
;
415 (*db
)->hdb_rename
= DB_rename
;
416 (*db
)->hdb__get
= DB__get
;
417 (*db
)->hdb__put
= DB__put
;
418 (*db
)->hdb__del
= DB__del
;
419 (*db
)->hdb_destroy
= DB_destroy
;
421 (*db3
)->lock_fd
= -1;
424 #endif /* HAVE_DB3 */