xref: /freebsd/crypto/heimdal/lib/hdb/ndbm.c (revision f0adf7f5cdd241db2f2c817683191a6ef64a4e95)
1 /*
2  * Copyright (c) 1997 - 2001 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33 
34 #include "hdb_locl.h"
35 
36 RCSID("$Id: ndbm.c,v 1.33 2001/09/03 05:03:01 assar Exp $");
37 
38 #if HAVE_NDBM
39 
40 #if defined(HAVE_GDBM_NDBM_H)
41 #include <gdbm/ndbm.h>
42 #elif defined(HAVE_NDBM_H)
43 #include <ndbm.h>
44 #elif defined(HAVE_DBM_H)
45 #include <dbm.h>
46 #endif
47 
48 struct ndbm_db {
49     DBM *db;
50     int lock_fd;
51 };
52 
53 static krb5_error_code
54 NDBM_destroy(krb5_context context, HDB *db)
55 {
56     krb5_error_code ret;
57 
58     ret = hdb_clear_master_key (context, db);
59     free(db->name);
60     free(db);
61     return 0;
62 }
63 
64 static krb5_error_code
65 NDBM_lock(krb5_context context, HDB *db, int operation)
66 {
67     struct ndbm_db *d = db->db;
68     return hdb_lock(d->lock_fd, operation);
69 }
70 
71 static krb5_error_code
72 NDBM_unlock(krb5_context context, HDB *db)
73 {
74     struct ndbm_db *d = db->db;
75     return hdb_unlock(d->lock_fd);
76 }
77 
78 static krb5_error_code
79 NDBM_seq(krb5_context context, HDB *db,
80 	 unsigned flags, hdb_entry *entry, int first)
81 
82 {
83     struct ndbm_db *d = (struct ndbm_db *)db->db;
84     datum key, value;
85     krb5_data key_data, data;
86     krb5_error_code ret = 0;
87 
88     if(first)
89 	key = dbm_firstkey(d->db);
90     else
91 	key = dbm_nextkey(d->db);
92     if(key.dptr == NULL)
93 	return HDB_ERR_NOENTRY;
94     key_data.data = key.dptr;
95     key_data.length = key.dsize;
96     ret = db->lock(context, db, HDB_RLOCK);
97     if(ret) return ret;
98     value = dbm_fetch(d->db, key);
99     db->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->master_key_set && (flags & HDB_F_DECRYPT)) {
105 	ret = hdb_unseal_keys (context, db, entry);
106 	if (ret)
107 	    hdb_free_entry (context, entry);
108     }
109     if (entry->principal == NULL) {
110 	entry->principal = malloc (sizeof(*entry->principal));
111 	if (entry->principal == NULL) {
112 	    ret = ENOMEM;
113 	    hdb_free_entry (context, entry);
114 	    krb5_set_error_string(context, "malloc: out of memory");
115 	} else {
116 	    hdb_key2principal (context, &key_data, entry->principal);
117 	}
118     }
119     return ret;
120 }
121 
122 
123 static krb5_error_code
124 NDBM_firstkey(krb5_context context, HDB *db, unsigned flags, hdb_entry *entry)
125 {
126     return NDBM_seq(context, db, flags, entry, 1);
127 }
128 
129 
130 static krb5_error_code
131 NDBM_nextkey(krb5_context context, HDB *db, unsigned flags, hdb_entry *entry)
132 {
133     return NDBM_seq(context, db, flags, entry, 0);
134 }
135 
136 static krb5_error_code
137 NDBM_rename(krb5_context context, HDB *db, const char *new_name)
138 {
139     /* XXX this function will break */
140     struct ndbm_db *d = db->db;
141 
142     int ret;
143     char *old_dir, *old_pag, *new_dir, *new_pag;
144     char *new_lock;
145     int lock_fd;
146 
147     /* lock old and new databases */
148     ret = db->lock(context, db, HDB_WLOCK);
149     if(ret)
150 	return ret;
151     asprintf(&new_lock, "%s.lock", new_name);
152     if(new_lock == NULL) {
153 	db->unlock(context, db);
154 	krb5_set_error_string(context, "malloc: out of memory");
155 	return ENOMEM;
156     }
157     lock_fd = open(new_lock, O_RDWR | O_CREAT, 0600);
158     if(lock_fd < 0) {
159 	ret = errno;
160 	db->unlock(context, db);
161 	krb5_set_error_string(context, "open(%s): %s", new_lock,
162 			      strerror(ret));
163 	free(new_lock);
164 	return ret;
165     }
166     free(new_lock);
167     ret = hdb_lock(lock_fd, HDB_WLOCK);
168     if(ret) {
169 	db->unlock(context, db);
170 	close(lock_fd);
171 	return ret;
172     }
173 
174     asprintf(&old_dir, "%s.dir", db->name);
175     asprintf(&old_pag, "%s.pag", db->name);
176     asprintf(&new_dir, "%s.dir", new_name);
177     asprintf(&new_pag, "%s.pag", new_name);
178 
179     ret = rename(old_dir, new_dir) || rename(old_pag, new_pag);
180     free(old_dir);
181     free(old_pag);
182     free(new_dir);
183     free(new_pag);
184     hdb_unlock(lock_fd);
185     db->unlock(context, db);
186 
187     if(ret) {
188 	ret = errno;
189 	close(lock_fd);
190 	krb5_set_error_string(context, "rename: %s", strerror(ret));
191 	return ret;
192     }
193 
194     close(d->lock_fd);
195     d->lock_fd = lock_fd;
196 
197     free(db->name);
198     db->name = strdup(new_name);
199     return 0;
200 }
201 
202 static krb5_error_code
203 NDBM__get(krb5_context context, HDB *db, krb5_data key, krb5_data *reply)
204 {
205     struct ndbm_db *d = (struct ndbm_db *)db->db;
206     datum k, v;
207     int code;
208 
209     k.dptr  = key.data;
210     k.dsize = key.length;
211     code = db->lock(context, db, HDB_RLOCK);
212     if(code)
213 	return code;
214     v = dbm_fetch(d->db, k);
215     db->unlock(context, db);
216     if(v.dptr == NULL)
217 	return HDB_ERR_NOENTRY;
218 
219     krb5_data_copy(reply, v.dptr, v.dsize);
220     return 0;
221 }
222 
223 static krb5_error_code
224 NDBM__put(krb5_context context, HDB *db, int replace,
225 	krb5_data key, krb5_data value)
226 {
227     struct ndbm_db *d = (struct ndbm_db *)db->db;
228     datum k, v;
229     int code;
230 
231     k.dptr  = key.data;
232     k.dsize = key.length;
233     v.dptr  = value.data;
234     v.dsize = value.length;
235 
236     code = db->lock(context, db, HDB_WLOCK);
237     if(code)
238 	return code;
239     code = dbm_store(d->db, k, v, replace ? DBM_REPLACE : DBM_INSERT);
240     db->unlock(context, db);
241     if(code == 1)
242 	return HDB_ERR_EXISTS;
243     if (code < 0)
244 	return code;
245     return 0;
246 }
247 
248 static krb5_error_code
249 NDBM__del(krb5_context context, HDB *db, krb5_data key)
250 {
251     struct ndbm_db *d = (struct ndbm_db *)db->db;
252     datum k;
253     int code;
254     krb5_error_code ret;
255 
256     k.dptr = key.data;
257     k.dsize = key.length;
258     ret = db->lock(context, db, HDB_WLOCK);
259     if(ret) return ret;
260     code = dbm_delete(d->db, k);
261     db->unlock(context, db);
262     if(code < 0)
263 	return errno;
264     return 0;
265 }
266 
267 static krb5_error_code
268 NDBM_open(krb5_context context, HDB *db, int flags, mode_t mode)
269 {
270     krb5_error_code ret;
271     struct ndbm_db *d = malloc(sizeof(*d));
272     char *lock_file;
273 
274     if(d == NULL) {
275 	krb5_set_error_string(context, "malloc: out of memory");
276 	return ENOMEM;
277     }
278     asprintf(&lock_file, "%s.lock", (char*)db->name);
279     if(lock_file == NULL) {
280 	free(d);
281 	krb5_set_error_string(context, "malloc: out of memory");
282 	return ENOMEM;
283     }
284     d->db = dbm_open((char*)db->name, flags, mode);
285     if(d->db == NULL){
286 	ret = errno;
287 	free(d);
288 	free(lock_file);
289 	krb5_set_error_string(context, "dbm_open(%s): %s", db->name,
290 			      strerror(ret));
291 	return ret;
292     }
293     d->lock_fd = open(lock_file, O_RDWR | O_CREAT, 0600);
294     if(d->lock_fd < 0){
295 	ret = errno;
296 	dbm_close(d->db);
297 	free(d);
298 	krb5_set_error_string(context, "open(%s): %s", lock_file,
299 			      strerror(ret));
300 	free(lock_file);
301 	return ret;
302     }
303     free(lock_file);
304     db->db = d;
305     if((flags & O_ACCMODE) == O_RDONLY)
306 	ret = hdb_check_db_format(context, db);
307     else
308 	ret = hdb_init_db(context, db);
309     if(ret == HDB_ERR_NOENTRY)
310 	return 0;
311     return ret;
312 }
313 
314 static krb5_error_code
315 NDBM_close(krb5_context context, HDB *db)
316 {
317     struct ndbm_db *d = db->db;
318     dbm_close(d->db);
319     close(d->lock_fd);
320     free(d);
321     return 0;
322 }
323 
324 krb5_error_code
325 hdb_ndbm_create(krb5_context context, HDB **db,
326 		const char *filename)
327 {
328     *db = malloc(sizeof(**db));
329     if (*db == NULL) {
330 	krb5_set_error_string(context, "malloc: out of memory");
331 	return ENOMEM;
332     }
333 
334     (*db)->db = NULL;
335     (*db)->name = strdup(filename);
336     if ((*db)->name == NULL) {
337 	krb5_set_error_string(context, "malloc: out of memory");
338 	free(*db);
339 	*db = NULL;
340 	return ENOMEM;
341     }
342     (*db)->master_key_set = 0;
343     (*db)->openp = 0;
344     (*db)->open = NDBM_open;
345     (*db)->close = NDBM_close;
346     (*db)->fetch = _hdb_fetch;
347     (*db)->store = _hdb_store;
348     (*db)->remove = _hdb_remove;
349     (*db)->firstkey = NDBM_firstkey;
350     (*db)->nextkey= NDBM_nextkey;
351     (*db)->lock = NDBM_lock;
352     (*db)->unlock = NDBM_unlock;
353     (*db)->rename = NDBM_rename;
354     (*db)->_get = NDBM__get;
355     (*db)->_put = NDBM__put;
356     (*db)->_del = NDBM__del;
357     (*db)->destroy = NDBM_destroy;
358     return 0;
359 }
360 
361 #endif /* HAVE_NDBM */
362