r19604: This is a massive commit, and I appologise in advance for it's size.
[samba.git] / source4 / heimdal / lib / hdb / db.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: db.c,v 1.36 2006/09/12 18:12:37 lha Exp $");
37
38 #if HAVE_DB1
39
40 #if defined(HAVE_DB_185_H)
41 #include <db_185.h>
42 #elif defined(HAVE_DB_H)
43 #include <db.h>
44 #endif
45
46 static krb5_error_code
47 DB_close(krb5_context context, HDB *db)
48 {
49     DB *d = (DB*)db->hdb_db;
50     (*d->close)(d);
51     return 0;
52 }
53
54 static krb5_error_code
55 DB_destroy(krb5_context context, HDB *db)
56 {
57     krb5_error_code ret;
58
59     ret = hdb_clear_master_key (context, db);
60     free(db->hdb_name);
61     free(db);
62     return ret;
63 }
64
65 static krb5_error_code
66 DB_lock(krb5_context context, HDB *db, int operation)
67 {
68     DB *d = (DB*)db->hdb_db;
69     int fd = (*d->fd)(d);
70     if(fd < 0)
71         return HDB_ERR_CANT_LOCK_DB;
72     return hdb_lock(fd, operation);
73 }
74
75 static krb5_error_code
76 DB_unlock(krb5_context context, HDB *db)
77 {
78     DB *d = (DB*)db->hdb_db;
79     int fd = (*d->fd)(d);
80     if(fd < 0)
81         return HDB_ERR_CANT_LOCK_DB;
82     return hdb_unlock(fd);
83 }
84
85
86 static krb5_error_code
87 DB_seq(krb5_context context, HDB *db,
88        unsigned flags, hdb_entry_ex *entry, int flag)
89 {
90     DB *d = (DB*)db->hdb_db;
91     DBT key, value;
92     krb5_data key_data, data;
93     int code;
94
95     code = db->hdb_lock(context, db, HDB_RLOCK);
96     if(code == -1)
97         return HDB_ERR_DB_INUSE;
98     code = (*d->seq)(d, &key, &value, flag);
99     db->hdb_unlock(context, db); /* XXX check value */
100     if(code == -1)
101         return errno;
102     if(code == 1)
103         return HDB_ERR_NOENTRY;
104
105     key_data.data = key.data;
106     key_data.length = key.size;
107     data.data = value.data;
108     data.length = value.size;
109     memset(entry, 0, sizeof(*entry));
110     if (hdb_value2entry(context, &data, &entry->entry))
111         return DB_seq(context, db, flags, entry, R_NEXT);
112     if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) {
113         code = hdb_unseal_keys (context, db, &entry->entry);
114         if (code)
115             hdb_free_entry (context, entry);
116     }
117     if (code == 0 && entry->entry.principal == NULL) {
118         entry->entry.principal = malloc(sizeof(*entry->entry.principal));
119         if (entry->entry.principal == NULL) {
120             krb5_set_error_string(context, "malloc: out of memory");
121             code = ENOMEM;
122             hdb_free_entry (context, entry);
123         } else {
124             hdb_key2principal(context, &key_data, entry->entry.principal);
125         }
126     }
127     return code;
128 }
129
130
131 static krb5_error_code
132 DB_firstkey(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
133 {
134     return DB_seq(context, db, flags, entry, R_FIRST);
135 }
136
137
138 static krb5_error_code
139 DB_nextkey(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
140 {
141     return DB_seq(context, db, flags, entry, R_NEXT);
142 }
143
144 static krb5_error_code
145 DB_rename(krb5_context context, HDB *db, const char *new_name)
146 {
147     int ret;
148     char *old, *new;
149
150     asprintf(&old, "%s.db", db->hdb_name);
151     asprintf(&new, "%s.db", new_name);
152     ret = rename(old, new);
153     free(old);
154     free(new);
155     if(ret)
156         return errno;
157     
158     free(db->hdb_name);
159     db->hdb_name = strdup(new_name);
160     return 0;
161 }
162
163 static krb5_error_code
164 DB__get(krb5_context context, HDB *db, krb5_data key, krb5_data *reply)
165 {
166     DB *d = (DB*)db->hdb_db;
167     DBT k, v;
168     int code;
169
170     k.data = key.data;
171     k.size = key.length;
172     code = db->hdb_lock(context, db, HDB_RLOCK);
173     if(code)
174         return code;
175     code = (*d->get)(d, &k, &v, 0);
176     db->hdb_unlock(context, db);
177     if(code < 0)
178         return errno;
179     if(code == 1)
180         return HDB_ERR_NOENTRY;
181     
182     krb5_data_copy(reply, v.data, v.size);
183     return 0;
184 }
185
186 static krb5_error_code
187 DB__put(krb5_context context, HDB *db, int replace, 
188         krb5_data key, krb5_data value)
189 {
190     DB *d = (DB*)db->hdb_db;
191     DBT k, v;
192     int code;
193
194     k.data = key.data;
195     k.size = key.length;
196     v.data = value.data;
197     v.size = value.length;
198     code = db->hdb_lock(context, db, HDB_WLOCK);
199     if(code)
200         return code;
201     code = (*d->put)(d, &k, &v, replace ? 0 : R_NOOVERWRITE);
202     db->hdb_unlock(context, db);
203     if(code < 0)
204         return errno;
205     if(code == 1)
206         return HDB_ERR_EXISTS;
207     return 0;
208 }
209
210 static krb5_error_code
211 DB__del(krb5_context context, HDB *db, krb5_data key)
212 {
213     DB *d = (DB*)db->hdb_db;
214     DBT k;
215     krb5_error_code code;
216     k.data = key.data;
217     k.size = key.length;
218     code = db->hdb_lock(context, db, HDB_WLOCK);
219     if(code)
220         return code;
221     code = (*d->del)(d, &k, 0);
222     db->hdb_unlock(context, db);
223     if(code == 1)
224         return HDB_ERR_NOENTRY;
225     if(code < 0)
226         return errno;
227     return 0;
228 }
229
230 static krb5_error_code
231 DB_open(krb5_context context, HDB *db, int flags, mode_t mode)
232 {
233     char *fn;
234     krb5_error_code ret;
235
236     asprintf(&fn, "%s.db", db->hdb_name);
237     if (fn == NULL) {
238         krb5_set_error_string(context, "malloc: out of memory");
239         return ENOMEM;
240     }
241     db->hdb_db = dbopen(fn, flags, mode, DB_BTREE, NULL);
242     free(fn);
243     /* try to open without .db extension */
244     if(db->hdb_db == NULL && errno == ENOENT)
245         db->hdb_db = dbopen(db->hdb_name, flags, mode, DB_BTREE, NULL);
246     if(db->hdb_db == NULL) {
247         ret = errno;
248         krb5_set_error_string(context, "dbopen (%s): %s",
249                               db->hdb_name, strerror(ret));
250         return ret;
251     }
252     if((flags & O_ACCMODE) == O_RDONLY)
253         ret = hdb_check_db_format(context, db);
254     else
255         ret = hdb_init_db(context, db);
256     if(ret == HDB_ERR_NOENTRY) {
257         krb5_clear_error_string(context);
258         return 0;
259     }
260     if (ret) {
261         DB_close(context, db);
262         krb5_set_error_string(context, "hdb_open: failed %s database %s",
263                               (flags & O_ACCMODE) == O_RDONLY ? 
264                               "checking format of" : "initialize", 
265                               db->hdb_name);
266     }
267     return ret;
268 }
269
270 krb5_error_code
271 hdb_db_create(krb5_context context, HDB **db, 
272               const char *filename)
273 {
274     *db = calloc(1, sizeof(**db));
275     if (*db == NULL) {
276         krb5_set_error_string(context, "malloc: out of memory");
277         return ENOMEM;
278     }
279
280     (*db)->hdb_db = NULL;
281     (*db)->hdb_name = strdup(filename);
282     if ((*db)->hdb_name == NULL) {
283         krb5_set_error_string(context, "malloc: out of memory");
284         free(*db);
285         *db = NULL;
286         return ENOMEM;
287     }
288     (*db)->hdb_master_key_set = 0;
289     (*db)->hdb_openp = 0;
290     (*db)->hdb_open = DB_open;
291     (*db)->hdb_close = DB_close;
292     (*db)->hdb_fetch = _hdb_fetch;
293     (*db)->hdb_store = _hdb_store;
294     (*db)->hdb_remove = _hdb_remove;
295     (*db)->hdb_firstkey = DB_firstkey;
296     (*db)->hdb_nextkey= DB_nextkey;
297     (*db)->hdb_lock = DB_lock;
298     (*db)->hdb_unlock = DB_unlock;
299     (*db)->hdb_rename = DB_rename;
300     (*db)->hdb__get = DB__get;
301     (*db)->hdb__put = DB__put;
302     (*db)->hdb__del = DB__del;
303     (*db)->hdb_destroy = DB_destroy;
304     return 0;
305 }
306
307 #endif /* HAVE_DB1 */