2 * keydb_dynamic.c - backend that can load the other backends
4 * Brett Parker <iDunno@sommitrealweird.co.uk>
6 * Copyright 2005 Project Purple
13 #include "decodekey.h"
17 #include "keystructs.h"
21 #include "onak-conf.h"
25 static struct dbfuncs *loaded_backend = NULL;
26 static void *backend_handle;
28 static bool close_backend(void)
30 loaded_backend = NULL;
31 dlclose(backend_handle);
32 backend_handle = NULL;
37 static bool load_backend(void)
40 char *funcsname = NULL;
42 if (loaded_backend != NULL) {
44 loaded_backend = NULL;
47 if (config.use_keyd) {
48 free(config.db_backend);
49 config.db_backend = strdup("keyd");
52 if (!config.db_backend) {
53 logthing(LOGTHING_CRITICAL, "No database backend defined.");
57 if (config.backends_dir == NULL) {
58 soname = malloc(strlen(config.db_backend)
59 + strlen("./libkeydb_")
63 sprintf(soname, "./libkeydb_%s.so", config.db_backend);
65 soname = malloc(strlen(config.db_backend)
66 + strlen("/libkeydb_")
68 + strlen(config.backends_dir)
71 sprintf(soname, "%s/libkeydb_%s.so", config.backends_dir,
75 logthing(LOGTHING_INFO, "Loading dynamic backend: %s", soname);
77 backend_handle = dlopen(soname, RTLD_LAZY);
78 if (backend_handle == NULL) {
79 logthing(LOGTHING_CRITICAL,
80 "Failed to open handle to library '%s': %s",
87 funcsname = malloc(strlen(config.db_backend)
91 sprintf(funcsname, "keydb_%s_funcs", config.db_backend);
93 loaded_backend = dlsym(backend_handle, funcsname);
96 if (loaded_backend == NULL) {
97 logthing(LOGTHING_CRITICAL,
98 "Failed to find dbfuncs structure in library "
99 "'%s' : %s", soname, dlerror());
110 static bool dynamic_starttrans()
112 if (loaded_backend == NULL) {
116 if (loaded_backend != NULL) {
117 if (loaded_backend->starttrans != NULL) {
118 return loaded_backend->starttrans();
125 static void dynamic_endtrans()
127 if (loaded_backend == NULL) {
131 if (loaded_backend != NULL) {
132 if (loaded_backend->endtrans != NULL) {
133 loaded_backend->endtrans();
138 static int dynamic_fetch_key(uint64_t keyid,
139 struct openpgp_publickey **publickey, bool intrans)
141 if (loaded_backend == NULL) {
145 if (loaded_backend != NULL) {
146 if (loaded_backend->fetch_key != NULL) {
147 return loaded_backend->fetch_key(keyid,publickey,intrans);
154 static int dynamic_store_key(struct openpgp_publickey *publickey, bool intrans,
157 if (loaded_backend == NULL) {
161 if (loaded_backend != NULL) {
162 if (loaded_backend->store_key != NULL) {
163 return loaded_backend->store_key(publickey,intrans,update);
170 static int dynamic_delete_key(uint64_t keyid, bool intrans)
172 if (loaded_backend == NULL) {
176 if (loaded_backend != NULL) {
177 if (loaded_backend->delete_key != NULL) {
178 return loaded_backend->delete_key(keyid, intrans);
185 static int dynamic_fetch_key_text(const char *search,
186 struct openpgp_publickey **publickey)
188 if (loaded_backend == NULL) {
192 if (loaded_backend != NULL) {
193 if (loaded_backend->fetch_key_text != NULL) {
194 return loaded_backend->fetch_key_text(search, publickey);
201 static int dynamic_fetch_key_skshash(const struct skshash *hash,
202 struct openpgp_publickey **publickey)
204 if (loaded_backend == NULL) {
208 if (loaded_backend != NULL) {
209 if (loaded_backend->fetch_key_skshash != NULL) {
210 return loaded_backend->fetch_key_skshash(hash,
218 static int dynamic_iterate_keys(void (*iterfunc)(void *ctx,
219 struct openpgp_publickey *key), void *ctx)
221 if (loaded_backend == NULL) {
225 if (loaded_backend != NULL) {
226 if (loaded_backend->iterate_keys != NULL) {
227 return loaded_backend->iterate_keys(iterfunc, ctx);
235 * keyid2uid - Takes a keyid and returns the primary UID for it.
236 * @keyid: The keyid to lookup.
238 static char *dynamic_keyid2uid(uint64_t keyid)
240 struct openpgp_publickey *publickey = NULL;
241 struct openpgp_signedpacket_list *curuid = NULL;
244 if (loaded_backend == NULL) {
248 if (loaded_backend != NULL) {
249 if (loaded_backend->keyid2uid != NULL) {
250 return loaded_backend->keyid2uid(keyid);
255 if (dynamic_fetch_key(keyid, &publickey, false) && publickey != NULL) {
256 curuid = publickey->uids;
257 while (curuid != NULL && buf[0] == 0) {
258 if (curuid->packet->tag == 13) {
259 snprintf(buf, 1023, "%.*s",
260 (int) curuid->packet->length,
261 curuid->packet->data);
263 curuid = curuid -> next;
265 free_publickey(publickey);
276 * getkeysigs - Gets a linked list of the signatures on a key.
277 * @keyid: The keyid to get the sigs for.
278 * @revoked: Is the key revoked?
280 * This function gets the list of signatures on a key. Used for key
281 * indexing and doing stats bits. If revoked is non-NULL then if the key
282 * is revoked it's set to true.
284 static struct ll *dynamic_getkeysigs(uint64_t keyid, bool *revoked)
286 struct ll *sigs = NULL;
287 struct openpgp_signedpacket_list *uids = NULL;
288 struct openpgp_publickey *publickey = NULL;
290 if ( loaded_backend == NULL ) {
294 if (loaded_backend != NULL) {
295 if (loaded_backend->getkeysigs != NULL) {
296 return loaded_backend->getkeysigs(keyid,revoked);
300 dynamic_fetch_key(keyid, &publickey, false);
302 if (publickey != NULL) {
303 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
304 sigs = keysigs(sigs, uids->sigs);
306 if (revoked != NULL) {
307 *revoked = publickey->revoked;
309 free_publickey(publickey);
316 * cached_getkeysigs - Gets the signatures on a key.
317 * @keyid: The key we want the signatures for.
319 * This function gets the signatures on a key. It's the same as the
320 * getkeysigs function above except we use the hash module to cache the
321 * data so if we need it again it's already loaded.
323 static struct ll *dynamic_cached_getkeysigs(uint64_t keyid)
325 struct stats_key *key = NULL;
326 struct stats_key *signedkey = NULL;
327 struct ll *cursig = NULL;
328 bool revoked = false;
334 if (loaded_backend == NULL) {
338 if (loaded_backend != NULL) {
339 if (loaded_backend->cached_getkeysigs != NULL) {
340 return loaded_backend->cached_getkeysigs(keyid);
344 key = createandaddtohash(keyid);
346 if (key->gotsigs == false) {
347 key->sigs = dynamic_getkeysigs(key->keyid, &revoked);
348 key->revoked = revoked;
349 for (cursig = key->sigs; cursig != NULL;
350 cursig = cursig->next) {
351 signedkey = (struct stats_key *) cursig->object;
352 signedkey->signs = lladd(signedkey->signs, key);
361 * getfullkeyid - Maps a 32bit key id to a 64bit one.
362 * @keyid: The 32bit keyid.
364 * This function maps a 32bit key id to the full 64bit one. It returns the
365 * full keyid. If the key isn't found a keyid of 0 is returned.
367 static uint64_t dynamic_getfullkeyid(uint64_t keyid)
369 struct openpgp_publickey *publickey = NULL;
371 if (loaded_backend == NULL) {
375 if (loaded_backend != NULL) {
376 if (loaded_backend->getfullkeyid != NULL) {
377 return loaded_backend->getfullkeyid(keyid);
381 if (keyid < 0x100000000LL) {
382 dynamic_fetch_key(keyid, &publickey, false);
383 if (publickey != NULL) {
384 keyid = get_keyid(publickey);
385 free_publickey(publickey);
396 * update_keys - Takes a list of public keys and updates them in the DB.
397 * @keys: The keys to update in the DB.
398 * @sendsync: Should we send a sync mail to our peers.
400 * Takes a list of keys and adds them to the database, merging them with
401 * the key in the database if it's already present there. The key list is
402 * update to contain the minimum set of updates required to get from what
403 * we had before to what we have now (ie the set of data that was added to
404 * the DB). Returns the number of entirely new keys added.
406 static int dynamic_update_keys(struct openpgp_publickey **keys, bool sendsync)
408 struct openpgp_publickey *curkey = NULL;
409 struct openpgp_publickey *oldkey = NULL;
410 struct openpgp_publickey *prev = NULL;
414 if (loaded_backend == NULL) {
418 if (loaded_backend != NULL) {
419 if (loaded_backend->update_keys != NULL) {
420 return loaded_backend->update_keys(keys, sendsync);
424 for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
425 intrans = dynamic_starttrans();
426 logthing(LOGTHING_INFO,
427 "Fetching key 0x%" PRIX64 ", result: %d",
429 dynamic_fetch_key(get_keyid(curkey), &oldkey, intrans));
432 * If we already have the key stored in the DB then merge it
433 * with the new one that's been supplied. Otherwise the key
434 * we've just got is the one that goes in the DB and also the
435 * one that we send out.
437 if (oldkey != NULL) {
438 merge_keys(oldkey, curkey);
439 if (curkey->sigs == NULL &&
440 curkey->uids == NULL &&
441 curkey->subkeys == NULL) {
443 *keys = curkey->next;
445 prev->next = curkey->next;
447 free_publickey(curkey);
452 logthing(LOGTHING_INFO,
453 "Merged key; storing updated key.");
454 dynamic_store_key(oldkey, intrans, true);
456 free_publickey(oldkey);
460 logthing(LOGTHING_INFO,
461 "Storing completely new key.");
462 dynamic_store_key(curkey, intrans, false);
469 if (sendsync && keys != NULL) {
476 static void dynamic_initdb(bool readonly)
478 if (loaded_backend == NULL) {
482 if (loaded_backend != NULL) {
483 if (loaded_backend->initdb != NULL) {
484 loaded_backend->initdb(readonly);
489 static void dynamic_cleanupdb(void)
491 if (loaded_backend != NULL) {
492 if (loaded_backend->cleanupdb != NULL) {
493 loaded_backend->cleanupdb();
500 struct dbfuncs keydb_dynamic_funcs = {
501 .initdb = dynamic_initdb,
502 .cleanupdb = dynamic_cleanupdb,
503 .starttrans = dynamic_starttrans,
504 .endtrans = dynamic_endtrans,
505 .fetch_key = dynamic_fetch_key,
506 .fetch_key_text = dynamic_fetch_key_text,
507 .fetch_key_skshash = dynamic_fetch_key_skshash,
508 .store_key = dynamic_store_key,
509 .update_keys = dynamic_update_keys,
510 .delete_key = dynamic_delete_key,
511 .getkeysigs = dynamic_getkeysigs,
512 .cached_getkeysigs = dynamic_cached_getkeysigs,
513 .keyid2uid = dynamic_keyid2uid,
514 .getfullkeyid = dynamic_getfullkeyid,
515 .iterate_keys = dynamic_iterate_keys,