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
40 #if defined(HAVE_GDBM_NDBM_H)
41 #include <gdbm/ndbm.h>
42 #elif defined(HAVE_NDBM_H)
44 #elif defined(HAVE_DBM_H)
53 static krb5_error_code
54 NDBM_destroy(krb5_context context
, HDB
*db
)
58 ret
= hdb_clear_master_key (context
, db
);
64 static krb5_error_code
65 NDBM_lock(krb5_context context
, HDB
*db
, int operation
)
67 struct ndbm_db
*d
= db
->hdb_db
;
68 return hdb_lock(d
->lock_fd
, operation
);
71 static krb5_error_code
72 NDBM_unlock(krb5_context context
, HDB
*db
)
74 struct ndbm_db
*d
= db
->hdb_db
;
75 return hdb_unlock(d
->lock_fd
);
78 static krb5_error_code
79 NDBM_seq(krb5_context context
, HDB
*db
,
80 unsigned flags
, hdb_entry
*entry
, int first
)
83 struct ndbm_db
*d
= (struct ndbm_db
*)db
->hdb_db
;
85 krb5_data key_data
, data
;
86 krb5_error_code ret
= 0;
89 key
= dbm_firstkey(d
->db
);
91 key
= dbm_nextkey(d
->db
);
93 return HDB_ERR_NOENTRY
;
94 key_data
.data
= key
.dptr
;
95 key_data
.length
= key
.dsize
;
96 ret
= db
->hdb_lock(context
, db
, HDB_RLOCK
);
98 value
= dbm_fetch(d
->db
, key
);
99 db
->hdb_unlock(context
, db
);
100 data
.data
= value
.dptr
;
101 data
.length
= value
.dsize
;
102 if(hdb_value2entry(context
, &data
, entry
))
103 return NDBM_seq(context
, db
, flags
, entry
, 0);
104 if (db
->hdb_master_key_set
&& (flags
& HDB_F_DECRYPT
)) {
105 ret
= hdb_unseal_keys (context
, db
, entry
);
107 hdb_free_entry (context
, entry
);
109 if (entry
->principal
== NULL
) {
110 entry
->principal
= malloc (sizeof(*entry
->principal
));
111 if (entry
->principal
== NULL
) {
113 hdb_free_entry (context
, entry
);
114 krb5_set_error_string(context
, "malloc: out of memory");
116 hdb_key2principal (context
, &key_data
, entry
->principal
);
123 static krb5_error_code
124 NDBM_firstkey(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry
*entry
)
126 return NDBM_seq(context
, db
, flags
, entry
, 1);
130 static krb5_error_code
131 NDBM_nextkey(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry
*entry
)
133 return NDBM_seq(context
, db
, flags
, entry
, 0);
136 static krb5_error_code
137 NDBM_rename(krb5_context context
, HDB
*db
, const char *new_name
)
139 /* XXX this function will break */
140 struct ndbm_db
*d
= db
->hdb_db
;
143 char *old_dir
, *old_pag
, *new_dir
, *new_pag
;
147 /* lock old and new databases */
148 ret
= db
->hdb_lock(context
, db
, HDB_WLOCK
);
151 asprintf(&new_lock
, "%s.lock", new_name
);
152 if(new_lock
== NULL
) {
153 db
->hdb_unlock(context
, db
);
154 krb5_set_error_string(context
, "malloc: out of memory");
157 lock_fd
= open(new_lock
, O_RDWR
| O_CREAT
, 0600);
160 db
->hdb_unlock(context
, db
);
161 krb5_set_error_string(context
, "open(%s): %s", new_lock
,
167 ret
= hdb_lock(lock_fd
, HDB_WLOCK
);
169 db
->hdb_unlock(context
, db
);
174 asprintf(&old_dir
, "%s.dir", db
->hdb_name
);
175 asprintf(&old_pag
, "%s.pag", db
->hdb_name
);
176 asprintf(&new_dir
, "%s.dir", new_name
);
177 asprintf(&new_pag
, "%s.pag", new_name
);
179 ret
= rename(old_dir
, new_dir
) || rename(old_pag
, new_pag
);
185 db
->hdb_unlock(context
, db
);
190 krb5_set_error_string(context
, "rename: %s", strerror(ret
));
195 d
->lock_fd
= lock_fd
;
198 db
->hdb_name
= strdup(new_name
);
202 static krb5_error_code
203 NDBM__get(krb5_context context
, HDB
*db
, krb5_data key
, krb5_data
*reply
)
205 struct ndbm_db
*d
= (struct ndbm_db
*)db
->hdb_db
;
210 k
.dsize
= key
.length
;
211 code
= db
->hdb_lock(context
, db
, HDB_RLOCK
);
214 v
= dbm_fetch(d
->db
, k
);
215 db
->hdb_unlock(context
, db
);
217 return HDB_ERR_NOENTRY
;
219 krb5_data_copy(reply
, v
.dptr
, v
.dsize
);
223 static krb5_error_code
224 NDBM__put(krb5_context context
, HDB
*db
, int replace
,
225 krb5_data key
, krb5_data value
)
227 struct ndbm_db
*d
= (struct ndbm_db
*)db
->hdb_db
;
232 k
.dsize
= key
.length
;
234 v
.dsize
= value
.length
;
236 code
= db
->hdb_lock(context
, db
, HDB_WLOCK
);
239 code
= dbm_store(d
->db
, k
, v
, replace
? DBM_REPLACE
: DBM_INSERT
);
240 db
->hdb_unlock(context
, db
);
242 return HDB_ERR_EXISTS
;
248 static krb5_error_code
249 NDBM__del(krb5_context context
, HDB
*db
, krb5_data key
)
251 struct ndbm_db
*d
= (struct ndbm_db
*)db
->hdb_db
;
257 k
.dsize
= key
.length
;
258 ret
= db
->hdb_lock(context
, db
, HDB_WLOCK
);
260 code
= dbm_delete(d
->db
, k
);
261 db
->hdb_unlock(context
, db
);
267 static krb5_error_code
268 NDBM_open(krb5_context context
, HDB
*db
, int flags
, mode_t mode
)
271 struct ndbm_db
*d
= malloc(sizeof(*d
));
275 krb5_set_error_string(context
, "malloc: out of memory");
278 asprintf(&lock_file
, "%s.lock", (char*)db
->hdb_name
);
279 if(lock_file
== NULL
) {
281 krb5_set_error_string(context
, "malloc: out of memory");
284 d
->db
= dbm_open((char*)db
->hdb_name
, flags
, mode
);
289 krb5_set_error_string(context
, "dbm_open(%s): %s", db
->hdb_name
,
293 d
->lock_fd
= open(lock_file
, O_RDWR
| O_CREAT
, 0600);
298 krb5_set_error_string(context
, "open(%s): %s", lock_file
,
305 if((flags
& O_ACCMODE
) == O_RDONLY
)
306 ret
= hdb_check_db_format(context
, db
);
308 ret
= hdb_init_db(context
, db
);
309 if(ret
== HDB_ERR_NOENTRY
)
314 static krb5_error_code
315 NDBM_close(krb5_context context
, HDB
*db
)
317 struct ndbm_db
*d
= db
->hdb_db
;
325 hdb_ndbm_create(krb5_context context
, HDB
**db
,
326 const char *filename
)
328 *db
= malloc(sizeof(**db
));
330 krb5_set_error_string(context
, "malloc: out of memory");
334 (*db
)->hdb_db
= NULL
;
335 (*db
)->hdb_name
= strdup(filename
);
336 if ((*db
)->hdb_name
== NULL
) {
337 krb5_set_error_string(context
, "malloc: out of memory");
342 (*db
)->hdb_master_key_set
= 0;
343 (*db
)->hdb_openp
= 0;
344 (*db
)->hdb_open
= NDBM_open
;
345 (*db
)->hdb_close
= NDBM_close
;
346 (*db
)->hdb_fetch
= _hdb_fetch
;
347 (*db
)->hdb_store
= _hdb_store
;
348 (*db
)->hdb_remove
= _hdb_remove
;
349 (*db
)->hdb_firstkey
= NDBM_firstkey
;
350 (*db
)->hdb_nextkey
= NDBM_nextkey
;
351 (*db
)->hdb_lock
= NDBM_lock
;
352 (*db
)->hdb_unlock
= NDBM_unlock
;
353 (*db
)->hdb_rename
= NDBM_rename
;
354 (*db
)->hdb__get
= NDBM__get
;
355 (*db
)->hdb__put
= NDBM__put
;
356 (*db
)->hdb__del
= NDBM__del
;
357 (*db
)->hdb_destroy
= NDBM_destroy
;
361 #endif /* HAVE_NDBM */