r8302: import mini HEIMDAL into the tree
[tprouty/samba.git] / source / heimdal / lib / hdb / ndbm.c
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.35 2005/06/23 13:37:57 lha 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->hdb_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->hdb_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->hdb_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->hdb_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->hdb_lock(context, db, HDB_RLOCK);
97     if(ret) return ret;
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);
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->hdb_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->hdb_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->hdb_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->hdb_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->hdb_unlock(context, db);
170         close(lock_fd);
171         return ret;
172     }
173
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);
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->hdb_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->hdb_name);
198     db->hdb_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->hdb_db;
206     datum k, v;
207     int code;
208
209     k.dptr  = key.data;
210     k.dsize = key.length;
211     code = db->hdb_lock(context, db, HDB_RLOCK);
212     if(code)
213         return code;
214     v = dbm_fetch(d->db, k);
215     db->hdb_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->hdb_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->hdb_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->hdb_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->hdb_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->hdb_lock(context, db, HDB_WLOCK);
259     if(ret) return ret;
260     code = dbm_delete(d->db, k);
261     db->hdb_unlock(context, db);
262     if(code < 0)
263         return errno;
264     return 0;
265 }
266
267
268 static krb5_error_code
269 NDBM_close(krb5_context context, HDB *db)
270 {
271     struct ndbm_db *d = db->hdb_db;
272     dbm_close(d->db);
273     close(d->lock_fd);
274     free(d);
275     return 0;
276 }
277
278 static krb5_error_code
279 NDBM_open(krb5_context context, HDB *db, int flags, mode_t mode)
280 {
281     krb5_error_code ret;
282     struct ndbm_db *d = malloc(sizeof(*d));
283     char *lock_file;
284
285     if(d == NULL) {
286         krb5_set_error_string(context, "malloc: out of memory");
287         return ENOMEM;
288     }
289     asprintf(&lock_file, "%s.lock", (char*)db->hdb_name);
290     if(lock_file == NULL) {
291         free(d);
292         krb5_set_error_string(context, "malloc: out of memory");
293         return ENOMEM;
294     }
295     d->db = dbm_open((char*)db->hdb_name, flags, mode);
296     if(d->db == NULL){
297         ret = errno;
298         free(d);
299         free(lock_file);
300         krb5_set_error_string(context, "dbm_open(%s): %s", db->hdb_name,
301                               strerror(ret));
302         return ret;
303     }
304     d->lock_fd = open(lock_file, O_RDWR | O_CREAT, 0600);
305     if(d->lock_fd < 0){
306         ret = errno;
307         dbm_close(d->db);
308         free(d);
309         krb5_set_error_string(context, "open(%s): %s", lock_file,
310                               strerror(ret));
311         free(lock_file);
312         return ret;
313     }
314     free(lock_file);
315     db->hdb_db = d;
316     if((flags & O_ACCMODE) == O_RDONLY)
317         ret = hdb_check_db_format(context, db);
318     else
319         ret = hdb_init_db(context, db);
320     if(ret == HDB_ERR_NOENTRY)
321         return 0;
322     if (ret) {
323         NDBM_close(context, db);
324         krb5_set_error_string(context, "hdb_open: failed %s database %s",
325                               (flags & O_ACCMODE) == O_RDONLY ? 
326                               "checking format of" : "initialize", 
327                               db->hdb_name);
328     }
329     return ret;
330 }
331
332 krb5_error_code
333 hdb_ndbm_create(krb5_context context, HDB **db, 
334                 const char *filename)
335 {
336     *db = malloc(sizeof(**db));
337     if (*db == NULL) {
338         krb5_set_error_string(context, "malloc: out of memory");
339         return ENOMEM;
340     }
341
342     (*db)->hdb_db = NULL;
343     (*db)->hdb_name = strdup(filename);
344     if ((*db)->hdb_name == NULL) {
345         krb5_set_error_string(context, "malloc: out of memory");
346         free(*db);
347         *db = NULL;
348         return ENOMEM;
349     }
350     (*db)->hdb_master_key_set = 0;
351     (*db)->hdb_openp = 0;
352     (*db)->hdb_open = NDBM_open;
353     (*db)->hdb_close = NDBM_close;
354     (*db)->hdb_fetch = _hdb_fetch;
355     (*db)->hdb_store = _hdb_store;
356     (*db)->hdb_remove = _hdb_remove;
357     (*db)->hdb_firstkey = NDBM_firstkey;
358     (*db)->hdb_nextkey= NDBM_nextkey;
359     (*db)->hdb_lock = NDBM_lock;
360     (*db)->hdb_unlock = NDBM_unlock;
361     (*db)->hdb_rename = NDBM_rename;
362     (*db)->hdb__get = NDBM__get;
363     (*db)->hdb__put = NDBM__put;
364     (*db)->hdb__del = NDBM__del;
365     (*db)->hdb_destroy = NDBM_destroy;
366     return 0;
367 }
368
369 #endif /* HAVE_NDBM */