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
50 static krb5_error_code
51 DB_close(krb5_context context
, HDB
*db
)
53 DB
*d
= (DB
*)db
->hdb_db
;
54 DBC
*dbcp
= (DBC
*)db
->hdb_dbc
;
56 (*dbcp
->c_close
)(dbcp
);
62 static krb5_error_code
63 DB_destroy(krb5_context context
, HDB
*db
)
67 ret
= hdb_clear_master_key (context
, db
);
73 static krb5_error_code
74 DB_lock(krb5_context context
, HDB
*db
, int operation
)
76 DB
*d
= (DB
*)db
->hdb_db
;
79 return HDB_ERR_CANT_LOCK_DB
;
80 return hdb_lock(fd
, operation
);
83 static krb5_error_code
84 DB_unlock(krb5_context context
, HDB
*db
)
86 DB
*d
= (DB
*)db
->hdb_db
;
89 return HDB_ERR_CANT_LOCK_DB
;
90 return hdb_unlock(fd
);
94 static krb5_error_code
95 DB_seq(krb5_context context
, HDB
*db
,
96 unsigned flags
, hdb_entry_ex
*entry
, int flag
)
99 DBC
*dbcp
= db
->hdb_dbc
;
100 krb5_data key_data
, data
;
103 memset(&key
, 0, sizeof(DBT
));
104 memset(&value
, 0, sizeof(DBT
));
105 if ((*db
->hdb_lock
)(context
, db
, HDB_RLOCK
))
106 return HDB_ERR_DB_INUSE
;
107 code
= (*dbcp
->c_get
)(dbcp
, &key
, &value
, flag
);
108 (*db
->hdb_unlock
)(context
, db
); /* XXX check value */
109 if (code
== DB_NOTFOUND
)
110 return HDB_ERR_NOENTRY
;
114 key_data
.data
= key
.data
;
115 key_data
.length
= key
.size
;
116 data
.data
= value
.data
;
117 data
.length
= value
.size
;
118 memset(entry
, 0, sizeof(*entry
));
119 if (hdb_value2entry(context
, &data
, &entry
->entry
))
120 return DB_seq(context
, db
, flags
, entry
, DB_NEXT
);
121 if (db
->hdb_master_key_set
&& (flags
& HDB_F_DECRYPT
)) {
122 code
= hdb_unseal_keys (context
, db
, &entry
->entry
);
124 hdb_free_entry (context
, entry
);
126 if (entry
->entry
.principal
== NULL
) {
127 entry
->entry
.principal
= malloc(sizeof(*entry
->entry
.principal
));
128 if (entry
->entry
.principal
== NULL
) {
129 hdb_free_entry (context
, entry
);
130 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
133 hdb_key2principal(context
, &key_data
, entry
->entry
.principal
);
140 static krb5_error_code
141 DB_firstkey(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
143 return DB_seq(context
, db
, flags
, entry
, DB_FIRST
);
147 static krb5_error_code
148 DB_nextkey(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
150 return DB_seq(context
, db
, flags
, entry
, DB_NEXT
);
153 static krb5_error_code
154 DB_rename(krb5_context context
, HDB
*db
, const char *new_name
)
159 asprintf(&old
, "%s.db", db
->hdb_name
);
160 asprintf(&new, "%s.db", new_name
);
161 ret
= rename(old
, new);
168 db
->hdb_name
= strdup(new_name
);
172 static krb5_error_code
173 DB__get(krb5_context context
, HDB
*db
, krb5_data key
, krb5_data
*reply
)
175 DB
*d
= (DB
*)db
->hdb_db
;
179 memset(&k
, 0, sizeof(DBT
));
180 memset(&v
, 0, sizeof(DBT
));
184 if ((code
= (*db
->hdb_lock
)(context
, db
, HDB_RLOCK
)))
186 code
= (*d
->get
)(d
, NULL
, &k
, &v
, 0);
187 (*db
->hdb_unlock
)(context
, db
);
188 if(code
== DB_NOTFOUND
)
189 return HDB_ERR_NOENTRY
;
193 krb5_data_copy(reply
, v
.data
, v
.size
);
197 static krb5_error_code
198 DB__put(krb5_context context
, HDB
*db
, int replace
,
199 krb5_data key
, krb5_data value
)
201 DB
*d
= (DB
*)db
->hdb_db
;
205 memset(&k
, 0, sizeof(DBT
));
206 memset(&v
, 0, sizeof(DBT
));
211 v
.size
= value
.length
;
213 if ((code
= (*db
->hdb_lock
)(context
, db
, HDB_WLOCK
)))
215 code
= (*d
->put
)(d
, NULL
, &k
, &v
, replace
? 0 : DB_NOOVERWRITE
);
216 (*db
->hdb_unlock
)(context
, db
);
217 if(code
== DB_KEYEXIST
)
218 return HDB_ERR_EXISTS
;
224 static krb5_error_code
225 DB__del(krb5_context context
, HDB
*db
, krb5_data key
)
227 DB
*d
= (DB
*)db
->hdb_db
;
229 krb5_error_code code
;
230 memset(&k
, 0, sizeof(DBT
));
234 code
= (*db
->hdb_lock
)(context
, db
, HDB_WLOCK
);
237 code
= (*d
->del
)(d
, NULL
, &k
, 0);
238 (*db
->hdb_unlock
)(context
, db
);
239 if(code
== DB_NOTFOUND
)
240 return HDB_ERR_NOENTRY
;
246 static krb5_error_code
247 DB_open(krb5_context context
, HDB
*db
, int flags
, mode_t mode
)
256 myflags
|= DB_CREATE
;
261 if((flags
& O_ACCMODE
) == O_RDONLY
)
262 myflags
|= DB_RDONLY
;
265 myflags
|= DB_TRUNCATE
;
267 asprintf(&fn
, "%s.db", db
->hdb_name
);
269 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
272 if (db_create(&d
, NULL
, 0) != 0) {
274 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
279 #if (DB_VERSION_MAJOR >= 4) && (DB_VERSION_MINOR >= 1)
280 ret
= (*d
->open
)(db
->hdb_db
, NULL
, fn
, NULL
, DB_BTREE
, myflags
, mode
);
282 ret
= (*d
->open
)(db
->hdb_db
, fn
, NULL
, DB_BTREE
, myflags
, mode
);
286 /* try to open without .db extension */
287 #if (DB_VERSION_MAJOR >= 4) && (DB_VERSION_MINOR >= 1)
288 ret
= (*d
->open
)(db
->hdb_db
, NULL
, db
->hdb_name
, NULL
, DB_BTREE
,
291 ret
= (*d
->open
)(db
->hdb_db
, db
->hdb_name
, NULL
, DB_BTREE
,
298 krb5_set_error_message(context
, ret
, "opening %s: %s",
299 db
->hdb_name
, strerror(ret
));
304 ret
= (*d
->cursor
)(d
, NULL
, &dbc
, 0);
306 krb5_set_error_message(context
, ret
, "d->cursor: %s", strerror(ret
));
311 if((flags
& O_ACCMODE
) == O_RDONLY
)
312 ret
= hdb_check_db_format(context
, db
);
314 ret
= hdb_init_db(context
, db
);
315 if(ret
== HDB_ERR_NOENTRY
)
318 DB_close(context
, db
);
319 krb5_set_error_message(context
, ret
, "hdb_open: failed %s database %s",
320 (flags
& O_ACCMODE
) == O_RDONLY
?
321 "checking format of" : "initialize",
329 hdb_db_create(krb5_context context
, HDB
**db
,
330 const char *filename
)
332 *db
= calloc(1, sizeof(**db
));
334 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
338 (*db
)->hdb_db
= NULL
;
339 (*db
)->hdb_name
= strdup(filename
);
340 if ((*db
)->hdb_name
== NULL
) {
343 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
346 (*db
)->hdb_master_key_set
= 0;
347 (*db
)->hdb_openp
= 0;
348 (*db
)->hdb_capability_flags
= HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL
;
349 (*db
)->hdb_open
= DB_open
;
350 (*db
)->hdb_close
= DB_close
;
351 (*db
)->hdb_fetch
= _hdb_fetch
;
352 (*db
)->hdb_store
= _hdb_store
;
353 (*db
)->hdb_remove
= _hdb_remove
;
354 (*db
)->hdb_firstkey
= DB_firstkey
;
355 (*db
)->hdb_nextkey
= DB_nextkey
;
356 (*db
)->hdb_lock
= DB_lock
;
357 (*db
)->hdb_unlock
= DB_unlock
;
358 (*db
)->hdb_rename
= DB_rename
;
359 (*db
)->hdb__get
= DB__get
;
360 (*db
)->hdb__put
= DB__put
;
361 (*db
)->hdb__del
= DB__del
;
362 (*db
)->hdb_destroy
= DB_destroy
;
365 #endif /* HAVE_DB3 */