Debian Codegraph @

lib/hdb/hdb-mdb.c

raw
1 /*
2 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * Copyright (c) 2011 - Howard Chu, Symas Corp.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * 3. Neither the name of the Institute nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include "hdb_locl.h"
36
37 #if HAVE_LMDB
38
39 /* LMDB */
40
41 #include <lmdb.h>
42
43 #define KILO 1024
44
45 typedef struct mdb_info {
46 MDB_env *e;
47 MDB_txn *t;
48 MDB_dbi d;
49 MDB_cursor *c;
50 } mdb_info;
51
52 static krb5_error_code
53 DB_close(krb5_context context, HDB *db)
54 {
55 mdb_info *mi = (mdb_info *)db->hdb_db;
56
57 mdb_cursor_close(mi->c);
58 mdb_txn_abort(mi->t);
59 mdb_env_close(mi->e);
60 mi->c = 0;
61 mi->t = 0;
62 mi->e = 0;
63 return 0;
64 }
65
66 static krb5_error_code
67 DB_destroy(krb5_context context, HDB *db)
68 {
69 krb5_error_code ret;
70
71 ret = hdb_clear_master_key (context, db);
72 free(db->hdb_name);
73 free(db->hdb_db);
74 free(db);
75 return ret;
76 }
77
78 static krb5_error_code
79 DB_set_sync(krb5_context context, HDB *db, int on)
80 {
81 mdb_info *mi = (mdb_info *)db->hdb_db;
82
83 mdb_env_set_flags(mi->e, MDB_NOSYNC, !on);
84 return mdb_env_sync(mi->e, 0);
85 }
86
87 static krb5_error_code
88 DB_lock(krb5_context context, HDB *db, int operation)
89 {
90 db->lock_count++;
91 return 0;
92 }
93
94 static krb5_error_code
95 DB_unlock(krb5_context context, HDB *db)
96 {
97 if (db->lock_count > 1) {
98 db->lock_count--;
99 return 0;
100 }
101 heim_assert(db->lock_count == 1, "HDB lock/unlock sequence does not match");
102 db->lock_count--;
103 return 0;
104 }
105
106
107 static krb5_error_code
108 DB_seq(krb5_context context, HDB *db,
109 unsigned flags, hdb_entry_ex *entry, int flag)
110 {
111 mdb_info *mi = db->hdb_db;
112 MDB_val key, value;
113 krb5_data key_data, data;
114 int code;
115
116 key.mv_size = 0;
117 value.mv_size = 0;
118 code = mdb_cursor_get(mi->c, &key, &value, flag);
119 if (code == MDB_NOTFOUND)
120 return HDB_ERR_NOENTRY;
121 if (code)
122 return code;
123
124 key_data.data = key.mv_data;
125 key_data.length = key.mv_size;
126 data.data = value.mv_data;
127 data.length = value.mv_size;
128 memset(entry, 0, sizeof(*entry));
129 if (hdb_value2entry(context, &data, &entry->entry))
130 return DB_seq(context, db, flags, entry, MDB_NEXT);
131 if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) {
132 code = hdb_unseal_keys (context, db, &entry->entry);
133 if (code)
134 hdb_free_entry (context, entry);
135 }
136 if (entry->entry.principal == NULL) {
137 entry->entry.principal = malloc(sizeof(*entry->entry.principal));
138 if (entry->entry.principal == NULL) {
139 hdb_free_entry (context, entry);
140 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
141 return ENOMEM;
142 } else {
143 hdb_key2principal(context, &key_data, entry->entry.principal);
144 }
145 }
146 return 0;
147 }
148
149
150 static krb5_error_code
151 DB_firstkey(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
152 {
153 mdb_info *mi = db->hdb_db;
154 int code;
155
156 /* Always start with a fresh cursor to pick up latest DB state */
157 if (mi->t)
158 mdb_txn_abort(mi->t);
159
160 code = mdb_txn_begin(mi->e, NULL, MDB_RDONLY, &mi->t);
161 if (code)
162 return code;
163
164 code = mdb_cursor_open(mi->t, mi->d, &mi->c);
165 if (code)
166 return code;
167
168 return DB_seq(context, db, flags, entry, MDB_FIRST);
169 }
170
171
172 static krb5_error_code
173 DB_nextkey(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
174 {
175 return DB_seq(context, db, flags, entry, MDB_NEXT);
176 }
177
178 static krb5_error_code
179 DB_rename(krb5_context context, HDB *db, const char *new_name)
180 {
181 int ret;
182 char *old, *new;
183
184 if (strncmp(new_name, "mdb:", sizeof("mdb:") - 1) == 0)
185 new_name += sizeof("mdb:") - 1;
186 else if (strncmp(new_name, "lmdb:", sizeof("lmdb:") - 1) == 0)
187 new_name += sizeof("lmdb:") - 1;
188 if (asprintf(&old, "%s.mdb", db->hdb_name) == -1)
189 return ENOMEM;
190 if (asprintf(&new, "%s.mdb", new_name) == -1) {
191 free(old);
192 return ENOMEM;
193 }
194 ret = rename(old, new);
195 free(old);
196 free(new);
197 if(ret)
198 return errno;
199
200 free(db->hdb_name);
201 db->hdb_name = strdup(new_name);
202 return 0;
203 }
204
205 static krb5_error_code
206 DB__get(krb5_context context, HDB *db, krb5_data key, krb5_data *reply)
207 {
208 mdb_info *mi = (mdb_info*)db->hdb_db;
209 MDB_txn *txn;
210 MDB_val k, v;
211 int code;
212
213 k.mv_data = key.data;
214 k.mv_size = key.length;
215
216 code = mdb_txn_begin(mi->e, NULL, MDB_RDONLY, &txn);
217 if (code)
218 return code;
219
220 code = mdb_get(txn, mi->d, &k, &v);
221 if (code == 0)
222 krb5_data_copy(reply, v.mv_data, v.mv_size);
223 mdb_txn_abort(txn);
224 if(code == MDB_NOTFOUND)
225 return HDB_ERR_NOENTRY;
226 return code;
227 }
228
229 static krb5_error_code
230 DB__put(krb5_context context, HDB *db, int replace,
231 krb5_data key, krb5_data value)
232 {
233 mdb_info *mi = (mdb_info*)db->hdb_db;
234 MDB_txn *txn;
235 MDB_val k, v;
236 int code;
237
238 k.mv_data = key.data;
239 k.mv_size = key.length;
240 v.mv_data = value.data;
241 v.mv_size = value.length;
242
243 code = mdb_txn_begin(mi->e, NULL, 0, &txn);
244 if (code)
245 return code;
246
247 code = mdb_put(txn, mi->d, &k, &v, replace ? 0 : MDB_NOOVERWRITE);
248 if (code)
249 mdb_txn_abort(txn);
250 else
251 code = mdb_txn_commit(txn);
252 /*
253 * No need to call mdb_env_sync(); it's done automatically if MDB_NOSYNC is
254 * not set.
255 */
256 if(code == MDB_KEYEXIST)
257 return HDB_ERR_EXISTS;
258 return code;
259 }
260
261 static krb5_error_code
262 DB__del(krb5_context context, HDB *db, krb5_data key)
263 {
264 mdb_info *mi = (mdb_info*)db->hdb_db;
265 MDB_txn *txn;
266 MDB_val k;
267 krb5_error_code code;
268
269 k.mv_data = key.data;
270 k.mv_size = key.length;
271
272 code = mdb_txn_begin(mi->e, NULL, 0, &txn);
273 if (code)
274 return code;
275
276 code = mdb_del(txn, mi->d, &k, NULL);
277 if (code)
278 mdb_txn_abort(txn);
279 else
280 code = mdb_txn_commit(txn);
281 /*
282 * No need to call mdb_env_sync(); it's done automatically if MDB_NOSYNC is
283 * not set.
284 */
285 if(code == MDB_NOTFOUND)
286 return HDB_ERR_NOENTRY;
287 return code;
288 }
289
290 static krb5_error_code
291 DB_open(krb5_context context, HDB *db, int flags, mode_t mode)
292 {
293 mdb_info *mi = (mdb_info *)db->hdb_db;
294 MDB_txn *txn;
295 char *fn;
296 krb5_error_code ret;
297 int myflags = MDB_NOSUBDIR, tmp;
298
299 if((flags & O_ACCMODE) == O_RDONLY)
300 myflags |= MDB_RDONLY;
301
302 if (asprintf(&fn, "%s.mdb", db->hdb_name) == -1)
303 return krb5_enomem(context);
304 if (mdb_env_create(&mi->e)) {
305 free(fn);
306 return krb5_enomem(context);
307 }
308
309 tmp = krb5_config_get_int_default(context, NULL, 0, "kdc",
310 "hdb-mdb-maxreaders", NULL);
311 if (tmp) {
312 ret = mdb_env_set_maxreaders(mi->e, tmp);
313 if (ret) {
314 free(fn);
315 krb5_set_error_message(context, ret, "setting maxreaders on %s: %s",
316 db->hdb_name, mdb_strerror(ret));
317 return ret;
318 }
319 }
320
321 tmp = krb5_config_get_int_default(context, NULL, 0, "kdc",
322 "hdb-mdb-mapsize", NULL);
323 if (tmp) {
324 size_t maps = tmp;
325 maps *= KILO;
326 ret = mdb_env_set_mapsize(mi->e, maps);
327 if (ret) {
328 free(fn);
329 krb5_set_error_message(context, ret, "setting mapsize on %s: %s",
330 db->hdb_name, mdb_strerror(ret));
331 return ret;
332 }
333 }
334
335 ret = mdb_env_open(mi->e, fn, myflags, mode);
336 free(fn);
337 if (ret) {
338 fail:
339 mdb_env_close(mi->e);
340 mi->e = 0;
341 krb5_set_error_message(context, ret, "opening %s: %s",
342 db->hdb_name, mdb_strerror(ret));
343 return ret;
344 }
345
346 ret = mdb_txn_begin(mi->e, NULL, MDB_RDONLY, &txn);
347 if (ret)
348 goto fail;
349
350 ret = mdb_open(txn, NULL, 0, &mi->d);
351 mdb_txn_abort(txn);
352 if (ret)
353 goto fail;
354
355 if((flags & O_ACCMODE) == O_RDONLY)
356 ret = hdb_check_db_format(context, db);
357 else
358 ret = hdb_init_db(context, db);
359 if(ret == HDB_ERR_NOENTRY)
360 return 0;
361 if (ret) {
362 DB_close(context, db);
363 krb5_set_error_message(context, ret, "hdb_open: failed %s database %s",
364 (flags & O_ACCMODE) == O_RDONLY ?
365 "checking format of" : "initialize",
366 db->hdb_name);
367 }
368
369 return ret;
370 }
371
372 krb5_error_code
373 hdb_mdb_create(krb5_context context, HDB **db,
374 const char *filename)
375 {
376 *db = calloc(1, sizeof(**db));
377 if (*db == NULL) {
378 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
379 return ENOMEM;
380 }
381
382 (*db)->hdb_db = calloc(1, sizeof(mdb_info));
383 if ((*db)->hdb_db == NULL) {
384 free(*db);
385 *db = NULL;
386 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
387 return ENOMEM;
388 }
389 (*db)->hdb_name = strdup(filename);
390 if ((*db)->hdb_name == NULL) {
391 free((*db)->hdb_db);
392 free(*db);
393 *db = NULL;
394 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
395 return ENOMEM;
396 }
397 (*db)->hdb_master_key_set = 0;
398 (*db)->hdb_openp = 0;
399 (*db)->hdb_capability_flags = HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL;
400 (*db)->hdb_open = DB_open;
401 (*db)->hdb_close = DB_close;
402 (*db)->hdb_fetch_kvno = _hdb_fetch_kvno;
403 (*db)->hdb_store = _hdb_store;
404 (*db)->hdb_remove = _hdb_remove;
405 (*db)->hdb_firstkey = DB_firstkey;
406 (*db)->hdb_nextkey= DB_nextkey;
407 (*db)->hdb_lock = DB_lock;
408 (*db)->hdb_unlock = DB_unlock;
409 (*db)->hdb_rename = DB_rename;
410 (*db)->hdb__get = DB__get;
411 (*db)->hdb__put = DB__put;
412 (*db)->hdb__del = DB__del;
413 (*db)->hdb_destroy = DB_destroy;
414 (*db)->hdb_set_sync = DB_set_sync;
415 return 0;
416 }
417 #endif /* HAVE_LMDB */