4e3a340fe55504a17da848a13aa6be1f29a0b903
[samba.git] / source4 / 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 #if HAVE_NDBM
37
38 #if defined(HAVE_GDBM_NDBM_H)
39 #include <gdbm/ndbm.h>
40 #define WRITE_SUPPORT 1
41 #elif defined(HAVE_NDBM_H)
42 #include <ndbm.h>
43 #elif defined(HAVE_DBM_H)
44 #define WRITE_SUPPORT 1
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     hdb_clear_master_key(context, db);
57     krb5_config_free_strings(db->virtual_hostbased_princ_svcs);
58     free(db->hdb_name);
59     free(db);
60     return 0;
61 }
62
63 static krb5_error_code
64 NDBM_lock(krb5_context context, HDB *db, int operation)
65 {
66     struct ndbm_db *d = db->hdb_db;
67     return hdb_lock(d->lock_fd, operation);
68 }
69
70 static krb5_error_code
71 NDBM_unlock(krb5_context context, HDB *db)
72 {
73     struct ndbm_db *d = db->hdb_db;
74     return hdb_unlock(d->lock_fd);
75 }
76
77 static krb5_error_code
78 NDBM_seq(krb5_context context, HDB *db,
79          unsigned flags, hdb_entry_ex *entry, int first)
80
81 {
82     struct ndbm_db *d = (struct ndbm_db *)db->hdb_db;
83     datum key, value;
84     krb5_data key_data, data;
85     krb5_error_code ret = 0;
86
87     if(first)
88         key = dbm_firstkey(d->db);
89     else
90         key = dbm_nextkey(d->db);
91     if(key.dptr == NULL)
92         return HDB_ERR_NOENTRY;
93     key_data.data = key.dptr;
94     key_data.length = key.dsize;
95     ret = db->hdb_lock(context, db, HDB_RLOCK);
96     if(ret) return ret;
97     value = dbm_fetch(d->db, key);
98     db->hdb_unlock(context, db);
99     data.data = value.dptr;
100     data.length = value.dsize;
101     memset(entry, 0, sizeof(*entry));
102     if(hdb_value2entry(context, &data, &entry->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->entry);
106         if (ret)
107             hdb_free_entry (context, entry);
108     }
109     if (ret == 0 && entry->entry.principal == NULL) {
110         entry->entry.principal = malloc (sizeof(*entry->entry.principal));
111         if (entry->entry.principal == NULL) {
112             hdb_free_entry (context, entry);
113             ret = ENOMEM;
114             krb5_set_error_message(context, ret, "malloc: out of memory");
115         } else {
116             hdb_key2principal (context, &key_data, entry->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_ex *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_ex *entry)
132 {
133     return NDBM_seq(context, db, flags, entry, 0);
134 }
135
136 static krb5_error_code
137 open_lock_file(krb5_context context, const char *db_name, int *fd)
138 {
139     char *lock_file;
140     int ret = 0;
141
142     /* lock old and new databases */
143     asprintf(&lock_file, "%s.lock", db_name);
144     if(lock_file == NULL) {
145         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
146         return ENOMEM;
147     }
148
149     *fd = open(lock_file, O_RDWR | O_CREAT, 0600);
150     if(*fd < 0) {
151         ret = errno;
152         krb5_set_error_message(context, ret, "open(%s): %s", lock_file,
153                                strerror(ret));
154     }
155     free(lock_file);
156     return ret;
157 }
158
159
160 static krb5_error_code
161 NDBM_rename(krb5_context context, HDB *db, const char *new_name)
162 {
163     int ret;
164     char *old_dir, *old_pag, *new_dir, *new_pag;
165     int old_lock_fd, new_lock_fd;
166
167     /* lock old and new databases */
168     ret = open_lock_file(context, db->hdb_name, &old_lock_fd);
169     if (ret)
170         return ret;
171
172     ret = hdb_lock(old_lock_fd, HDB_WLOCK);
173     if(ret) {
174         close(old_lock_fd);
175         return ret;
176     }
177
178     ret = open_lock_file(context, new_name, &new_lock_fd);
179     if (ret) {
180         hdb_unlock(old_lock_fd);
181         close(old_lock_fd);
182         return ret;
183     }
184
185     ret = hdb_lock(new_lock_fd, HDB_WLOCK);
186     if(ret) {
187         hdb_unlock(old_lock_fd);
188         close(old_lock_fd);
189         close(new_lock_fd);
190         return ret;
191     }
192
193     asprintf(&old_dir, "%s.dir", db->hdb_name);
194     asprintf(&old_pag, "%s.pag", db->hdb_name);
195     asprintf(&new_dir, "%s.dir", new_name);
196     asprintf(&new_pag, "%s.pag", new_name);
197
198     ret = rename(old_dir, new_dir) || rename(old_pag, new_pag);
199     if (ret) {
200         ret = errno;
201         if (ret == 0)
202             ret = EPERM;
203         krb5_set_error_message(context, ret, "rename: %s", strerror(ret));
204     }
205
206     free(old_dir);
207     free(old_pag);
208     free(new_dir);
209     free(new_pag);
210
211     hdb_unlock(new_lock_fd);
212     hdb_unlock(old_lock_fd);
213     close(new_lock_fd);
214     close(old_lock_fd);
215
216     if(ret)
217         return ret;
218
219     free(db->hdb_name);
220     db->hdb_name = strdup(new_name);
221     return 0;
222 }
223
224 static krb5_error_code
225 NDBM__get(krb5_context context, HDB *db, krb5_data key, krb5_data *reply)
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     code = db->hdb_lock(context, db, HDB_RLOCK);
234     if(code)
235         return code;
236     v = dbm_fetch(d->db, k);
237     db->hdb_unlock(context, db);
238     if(v.dptr == NULL)
239         return HDB_ERR_NOENTRY;
240
241     krb5_data_copy(reply, v.dptr, v.dsize);
242     return 0;
243 }
244
245 static krb5_error_code
246 NDBM__put(krb5_context context, HDB *db, int replace,
247         krb5_data key, krb5_data value)
248 {
249 #ifdef WRITE_SUPPORT
250     struct ndbm_db *d = (struct ndbm_db *)db->hdb_db;
251     datum k, v;
252     int code;
253
254     k.dptr  = key.data;
255     k.dsize = key.length;
256     v.dptr  = value.data;
257     v.dsize = value.length;
258
259     code = db->hdb_lock(context, db, HDB_WLOCK);
260     if(code)
261         return code;
262     code = dbm_store(d->db, k, v, replace ? DBM_REPLACE : DBM_INSERT);
263     db->hdb_unlock(context, db);
264     if(code == 1)
265         return HDB_ERR_EXISTS;
266     if (code < 0)
267         return code;
268     return 0;
269 #else
270     return HDB_ERR_NO_WRITE_SUPPORT;
271 #endif
272 }
273
274 static krb5_error_code
275 NDBM__del(krb5_context context, HDB *db, krb5_data key)
276 {
277     struct ndbm_db *d = (struct ndbm_db *)db->hdb_db;
278     datum k;
279     int code;
280     krb5_error_code ret;
281
282     k.dptr = key.data;
283     k.dsize = key.length;
284     ret = db->hdb_lock(context, db, HDB_WLOCK);
285     if(ret) return ret;
286     code = dbm_delete(d->db, k);
287     db->hdb_unlock(context, db);
288     if(code < 0)
289         return errno;
290     return 0;
291 }
292
293
294 static krb5_error_code
295 NDBM_close(krb5_context context, HDB *db)
296 {
297     struct ndbm_db *d = db->hdb_db;
298     dbm_close(d->db);
299     close(d->lock_fd);
300     free(d);
301     return 0;
302 }
303
304 static krb5_error_code
305 NDBM_open(krb5_context context, HDB *db, int flags, mode_t mode)
306 {
307     krb5_error_code ret;
308     struct ndbm_db *d = malloc(sizeof(*d));
309
310     if(d == NULL) {
311         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
312         return ENOMEM;
313     }
314
315     d->db = dbm_open((char*)db->hdb_name, flags, mode);
316     if(d->db == NULL){
317         ret = errno;
318         free(d);
319         krb5_set_error_message(context, ret, "dbm_open(%s): %s", db->hdb_name,
320                                strerror(ret));
321         return ret;
322     }
323
324     ret = open_lock_file(context, db->hdb_name, &d->lock_fd);
325     if (ret) {
326         ret = errno;
327         dbm_close(d->db);
328         free(d);
329         krb5_set_error_message(context, ret, "open(lock file): %s",
330                                strerror(ret));
331         return ret;
332     }
333
334     db->hdb_db = d;
335     if((flags & O_ACCMODE) == O_RDONLY)
336         ret = hdb_check_db_format(context, db);
337     else
338         ret = hdb_init_db(context, db);
339     if(ret == HDB_ERR_NOENTRY)
340         return 0;
341     if (ret) {
342         NDBM_close(context, db);
343         krb5_set_error_message(context, ret, "hdb_open: failed %s database %s",
344                                (flags & O_ACCMODE) == O_RDONLY ?
345                                "checking format of" : "initialize",
346                                db->hdb_name);
347     }
348     return ret;
349 }
350
351 krb5_error_code
352 hdb_ndbm_create(krb5_context context, HDB **db,
353                 const char *filename)
354 {
355     *db = calloc(1, sizeof(**db));
356     if (*db == NULL) {
357         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
358         return ENOMEM;
359     }
360
361     (*db)->hdb_db = NULL;
362     (*db)->hdb_name = strdup(filename);
363     if ((*db)->hdb_name == NULL) {
364         free(*db);
365         *db = NULL;
366         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
367         return ENOMEM;
368     }
369     (*db)->hdb_master_key_set = 0;
370     (*db)->hdb_openp = 0;
371     (*db)->hdb_capability_flags = HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL;
372     (*db)->hdb_open = NDBM_open;
373     (*db)->hdb_close = NDBM_close;
374     (*db)->hdb_fetch_kvno = _hdb_fetch_kvno;
375     (*db)->hdb_store = _hdb_store;
376     (*db)->hdb_remove = _hdb_remove;
377     (*db)->hdb_firstkey = NDBM_firstkey;
378     (*db)->hdb_nextkey= NDBM_nextkey;
379     (*db)->hdb_lock = NDBM_lock;
380     (*db)->hdb_unlock = NDBM_unlock;
381     (*db)->hdb_rename = NDBM_rename;
382     (*db)->hdb__get = NDBM__get;
383     (*db)->hdb__put = NDBM__put;
384     (*db)->hdb__del = NDBM__del;
385     (*db)->hdb_destroy = NDBM_destroy;
386     return 0;
387 }
388
389 #endif /* HAVE_NDBM */