2 * Copyright (c) 1997 - 2001 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
42 #elif defined(HAVE_DB3_DB_H)
48 static krb5_error_code
49 DB_close(krb5_context context
, HDB
*db
)
52 DBC
*dbcp
= (DBC
*)db
->dbc
;
60 static krb5_error_code
61 DB_destroy(krb5_context context
, HDB
*db
)
65 ret
= hdb_clear_master_key (context
, db
);
71 static krb5_error_code
72 DB_lock(krb5_context context
, HDB
*db
, int operation
)
77 return HDB_ERR_CANT_LOCK_DB
;
78 return hdb_lock(fd
, operation
);
81 static krb5_error_code
82 DB_unlock(krb5_context context
, HDB
*db
)
87 return HDB_ERR_CANT_LOCK_DB
;
88 return hdb_unlock(fd
);
92 static krb5_error_code
93 DB_seq(krb5_context context
, HDB
*db
,
94 unsigned flags
, hdb_entry
*entry
, int flag
)
98 krb5_data key_data
, data
;
101 memset(&key
, 0, sizeof(DBT
));
102 memset(&value
, 0, sizeof(DBT
));
103 if (db
->lock(context
, db
, HDB_RLOCK
))
104 return HDB_ERR_DB_INUSE
;
105 code
= dbcp
->c_get(dbcp
, &key
, &value
, flag
);
106 db
->unlock(context
, db
); /* XXX check value */
107 if (code
== DB_NOTFOUND
)
108 return HDB_ERR_NOENTRY
;
112 key_data
.data
= key
.data
;
113 key_data
.length
= key
.size
;
114 data
.data
= value
.data
;
115 data
.length
= value
.size
;
116 if (hdb_value2entry(context
, &data
, entry
))
117 return DB_seq(context
, db
, flags
, entry
, DB_NEXT
);
118 if (db
->master_key_set
&& (flags
& HDB_F_DECRYPT
)) {
119 code
= hdb_unseal_keys (context
, db
, entry
);
121 hdb_free_entry (context
, entry
);
123 if (entry
->principal
== NULL
) {
124 entry
->principal
= malloc(sizeof(*entry
->principal
));
125 if (entry
->principal
== NULL
) {
126 hdb_free_entry (context
, entry
);
127 krb5_set_error_string(context
, "malloc: out of memory");
130 hdb_key2principal(context
, &key_data
, entry
->principal
);
137 static krb5_error_code
138 DB_firstkey(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry
*entry
)
140 return DB_seq(context
, db
, flags
, entry
, DB_FIRST
);
144 static krb5_error_code
145 DB_nextkey(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry
*entry
)
147 return DB_seq(context
, db
, flags
, entry
, DB_NEXT
);
150 static krb5_error_code
151 DB_rename(krb5_context context
, HDB
*db
, const char *new_name
)
156 asprintf(&old
, "%s.db", db
->name
);
157 asprintf(&new, "%s.db", new_name
);
158 ret
= rename(old
, new);
165 db
->name
= strdup(new_name
);
169 static krb5_error_code
170 DB__get(krb5_context context
, HDB
*db
, krb5_data key
, krb5_data
*reply
)
176 memset(&k
, 0, sizeof(DBT
));
177 memset(&v
, 0, sizeof(DBT
));
181 if ((code
= db
->lock(context
, db
, HDB_RLOCK
)))
183 code
= d
->get(d
, NULL
, &k
, &v
, 0);
184 db
->unlock(context
, db
);
185 if(code
== DB_NOTFOUND
)
186 return HDB_ERR_NOENTRY
;
190 krb5_data_copy(reply
, v
.data
, v
.size
);
194 static krb5_error_code
195 DB__put(krb5_context context
, HDB
*db
, int replace
,
196 krb5_data key
, krb5_data value
)
202 memset(&k
, 0, sizeof(DBT
));
203 memset(&v
, 0, sizeof(DBT
));
208 v
.size
= value
.length
;
210 if ((code
= db
->lock(context
, db
, HDB_WLOCK
)))
212 code
= d
->put(d
, NULL
, &k
, &v
, replace
? 0 : DB_NOOVERWRITE
);
213 db
->unlock(context
, db
);
214 if(code
== DB_KEYEXIST
)
215 return HDB_ERR_EXISTS
;
221 static krb5_error_code
222 DB__del(krb5_context context
, HDB
*db
, krb5_data key
)
226 krb5_error_code code
;
227 memset(&k
, 0, sizeof(DBT
));
231 code
= db
->lock(context
, db
, HDB_WLOCK
);
234 code
= d
->del(d
, NULL
, &k
, 0);
235 db
->unlock(context
, db
);
236 if(code
== DB_NOTFOUND
)
237 return HDB_ERR_NOENTRY
;
243 static krb5_error_code
244 DB_open(krb5_context context
, HDB
*db
, int flags
, mode_t mode
)
252 myflags
|= DB_CREATE
;
257 if (flags
& O_RDONLY
)
258 myflags
|= DB_RDONLY
;
261 myflags
|= DB_TRUNCATE
;
263 asprintf(&fn
, "%s.db", db
->name
);
265 krb5_set_error_string(context
, "malloc: out of memory");
268 db_create(&d
, NULL
, 0);
270 #if (DB_VERSION_MAJOR > 3) && (DB_VERSION_MINOR > 0)
271 if ((ret
= d
->open(db
->db
, NULL
, fn
, NULL
, DB_BTREE
, myflags
, mode
))) {
273 if ((ret
= d
->open(db
->db
, fn
, NULL
, DB_BTREE
, myflags
, mode
))) {
276 /* try to open without .db extension */
277 #if (DB_VERSION_MAJOR > 3) && (DB_VERSION_MINOR > 0)
278 if (d
->open(db
->db
, NULL
, db
->name
, NULL
, DB_BTREE
, myflags
, mode
)) {
280 if (d
->open(db
->db
, db
->name
, NULL
, DB_BTREE
, myflags
, mode
)) {
283 krb5_set_error_string(context
, "opening %s: %s",
284 db
->name
, strerror(ret
));
290 ret
= d
->cursor(d
, NULL
, (DBC
**)&db
->dbc
, 0);
292 krb5_set_error_string(context
, "d->cursor: %s", strerror(ret
));
296 if((flags
& O_ACCMODE
) == O_RDONLY
)
297 ret
= hdb_check_db_format(context
, db
);
299 ret
= hdb_init_db(context
, db
);
300 if(ret
== HDB_ERR_NOENTRY
)
306 hdb_db_create(krb5_context context
, HDB
**db
,
307 const char *filename
)
309 *db
= malloc(sizeof(**db
));
311 krb5_set_error_string(context
, "malloc: out of memory");
316 (*db
)->name
= strdup(filename
);
317 if ((*db
)->name
== NULL
) {
318 krb5_set_error_string(context
, "malloc: out of memory");
323 (*db
)->master_key_set
= 0;
325 (*db
)->open
= DB_open
;
326 (*db
)->close
= DB_close
;
327 (*db
)->fetch
= _hdb_fetch
;
328 (*db
)->store
= _hdb_store
;
329 (*db
)->remove
= _hdb_remove
;
330 (*db
)->firstkey
= DB_firstkey
;
331 (*db
)->nextkey
= DB_nextkey
;
332 (*db
)->lock
= DB_lock
;
333 (*db
)->unlock
= DB_unlock
;
334 (*db
)->rename
= DB_rename
;
335 (*db
)->_get
= DB__get
;
336 (*db
)->_put
= DB__put
;
337 (*db
)->_del
= DB__del
;
338 (*db
)->destroy
= DB_destroy
;
341 #endif /* HAVE_DB3 */