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"
26 static struct dbfuncs *loaded_backend = NULL;
27 static void *backend_handle;
29 static bool close_backend(void)
31 loaded_backend = NULL;
32 dlclose(backend_handle);
33 backend_handle = NULL;
38 static bool load_backend(void)
41 char *funcsname = NULL;
43 if (loaded_backend != NULL) {
45 loaded_backend = NULL;
48 if (config.use_keyd) {
49 free(config.db_backend);
50 config.db_backend = strdup("keyd");
53 if (!config.db_backend) {
54 logthing(LOGTHING_CRITICAL, "No database backend defined.");
58 if (config.backends_dir == NULL) {
59 soname = malloc(strlen(config.db_backend)
60 + strlen("./libkeydb_")
64 sprintf(soname, "./libkeydb_%s.so", config.db_backend);
66 soname = malloc(strlen(config.db_backend)
67 + strlen("/libkeydb_")
69 + strlen(config.backends_dir)
72 sprintf(soname, "%s/libkeydb_%s.so", config.backends_dir,
76 logthing(LOGTHING_INFO, "Loading dynamic backend: %s", soname);
78 backend_handle = dlopen(soname, RTLD_LAZY);
79 if (backend_handle == NULL) {
80 logthing(LOGTHING_CRITICAL,
81 "Failed to open handle to library '%s': %s",
88 funcsname = malloc(strlen(config.db_backend)
92 sprintf(funcsname, "keydb_%s_funcs", config.db_backend);
94 loaded_backend = dlsym(backend_handle, funcsname);
97 if (loaded_backend == NULL) {
98 logthing(LOGTHING_CRITICAL,
99 "Failed to find dbfuncs structure in library "
100 "'%s' : %s", soname, dlerror());
111 static bool dynamic_starttrans()
113 if (loaded_backend == NULL) {
117 if (loaded_backend != NULL) {
118 if (loaded_backend->starttrans != NULL) {
119 return loaded_backend->starttrans();
126 static void dynamic_endtrans()
128 if (loaded_backend == NULL) {
132 if (loaded_backend != NULL) {
133 if (loaded_backend->endtrans != NULL) {
134 loaded_backend->endtrans();
139 static int dynamic_fetch_key(uint64_t keyid,
140 struct openpgp_publickey **publickey, bool intrans)
142 if (loaded_backend == NULL) {
146 if (loaded_backend != NULL) {
147 if (loaded_backend->fetch_key != NULL) {
148 return loaded_backend->fetch_key(keyid,publickey,intrans);
155 static int dynamic_store_key(struct openpgp_publickey *publickey, bool intrans,
158 if (loaded_backend == NULL) {
162 if (loaded_backend != NULL) {
163 if (loaded_backend->store_key != NULL) {
164 return loaded_backend->store_key(publickey,intrans,update);
171 static int dynamic_delete_key(uint64_t keyid, bool intrans)
173 if (loaded_backend == NULL) {
177 if (loaded_backend != NULL) {
178 if (loaded_backend->delete_key != NULL) {
179 return loaded_backend->delete_key(keyid, intrans);
186 static int dynamic_fetch_key_text(const char *search,
187 struct openpgp_publickey **publickey)
189 if (loaded_backend == NULL) {
193 if (loaded_backend != NULL) {
194 if (loaded_backend->fetch_key_text != NULL) {
195 return loaded_backend->fetch_key_text(search, publickey);
202 static int dynamic_fetch_key_skshash(const struct skshash *hash,
203 struct openpgp_publickey **publickey)
205 if (loaded_backend == NULL) {
209 if (loaded_backend != NULL) {
210 if (loaded_backend->fetch_key_skshash != NULL) {
211 return loaded_backend->fetch_key_skshash(hash,
219 static int dynamic_iterate_keys(void (*iterfunc)(void *ctx,
220 struct openpgp_publickey *key), void *ctx)
222 if (loaded_backend == NULL) {
226 if (loaded_backend != NULL) {
227 if (loaded_backend->iterate_keys != NULL) {
228 return loaded_backend->iterate_keys(iterfunc, ctx);
236 * keyid2uid - Takes a keyid and returns the primary UID for it.
237 * @keyid: The keyid to lookup.
239 static char *dynamic_keyid2uid(uint64_t keyid)
241 struct openpgp_publickey *publickey = NULL;
242 struct openpgp_signedpacket_list *curuid = NULL;
245 if (loaded_backend == NULL) {
249 if (loaded_backend != NULL) {
250 if (loaded_backend->keyid2uid != NULL) {
251 return loaded_backend->keyid2uid(keyid);
256 if (dynamic_fetch_key(keyid, &publickey, false) && publickey != NULL) {
257 curuid = publickey->uids;
258 while (curuid != NULL && buf[0] == 0) {
259 if (curuid->packet->tag == OPENPGP_PACKET_UID) {
260 snprintf(buf, 1023, "%.*s",
261 (int) curuid->packet->length,
262 curuid->packet->data);
264 curuid = curuid -> next;
266 free_publickey(publickey);
277 * getkeysigs - Gets a linked list of the signatures on a key.
278 * @keyid: The keyid to get the sigs for.
279 * @revoked: Is the key revoked?
281 * This function gets the list of signatures on a key. Used for key
282 * indexing and doing stats bits. If revoked is non-NULL then if the key
283 * is revoked it's set to true.
285 static struct ll *dynamic_getkeysigs(uint64_t keyid, bool *revoked)
287 struct ll *sigs = NULL;
288 struct openpgp_signedpacket_list *uids = NULL;
289 struct openpgp_publickey *publickey = NULL;
291 if ( loaded_backend == NULL ) {
295 if (loaded_backend != NULL) {
296 if (loaded_backend->getkeysigs != NULL) {
297 return loaded_backend->getkeysigs(keyid,revoked);
301 dynamic_fetch_key(keyid, &publickey, false);
303 if (publickey != NULL) {
304 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
305 sigs = keysigs(sigs, uids->sigs);
307 if (revoked != NULL) {
308 *revoked = publickey->revoked;
310 free_publickey(publickey);
317 * cached_getkeysigs - Gets the signatures on a key.
318 * @keyid: The key we want the signatures for.
320 * This function gets the signatures on a key. It's the same as the
321 * getkeysigs function above except we use the hash module to cache the
322 * data so if we need it again it's already loaded.
324 static struct ll *dynamic_cached_getkeysigs(uint64_t keyid)
326 struct stats_key *key = NULL;
327 struct stats_key *signedkey = NULL;
328 struct ll *cursig = NULL;
329 bool revoked = false;
335 if (loaded_backend == NULL) {
339 if (loaded_backend != NULL) {
340 if (loaded_backend->cached_getkeysigs != NULL) {
341 return loaded_backend->cached_getkeysigs(keyid);
345 key = createandaddtohash(keyid);
347 if (key->gotsigs == false) {
348 key->sigs = dynamic_getkeysigs(key->keyid, &revoked);
349 key->revoked = revoked;
350 for (cursig = key->sigs; cursig != NULL;
351 cursig = cursig->next) {
352 signedkey = (struct stats_key *) cursig->object;
353 signedkey->signs = lladd(signedkey->signs, key);
362 * getfullkeyid - Maps a 32bit key id to a 64bit one.
363 * @keyid: The 32bit keyid.
365 * This function maps a 32bit key id to the full 64bit one. It returns the
366 * full keyid. If the key isn't found a keyid of 0 is returned.
368 static uint64_t dynamic_getfullkeyid(uint64_t keyid)
370 struct openpgp_publickey *publickey = NULL;
372 if (loaded_backend == NULL) {
376 if (loaded_backend != NULL) {
377 if (loaded_backend->getfullkeyid != NULL) {
378 return loaded_backend->getfullkeyid(keyid);
382 if (keyid < 0x100000000LL) {
383 dynamic_fetch_key(keyid, &publickey, false);
384 if (publickey != NULL) {
385 keyid = get_keyid(publickey);
386 free_publickey(publickey);
397 * update_keys - Takes a list of public keys and updates them in the DB.
398 * @keys: The keys to update in the DB.
399 * @sendsync: Should we send a sync mail to our peers.
401 * Takes a list of keys and adds them to the database, merging them with
402 * the key in the database if it's already present there. The key list is
403 * update to contain the minimum set of updates required to get from what
404 * we had before to what we have now (ie the set of data that was added to
405 * the DB). Returns the number of entirely new keys added.
407 static int dynamic_update_keys(struct openpgp_publickey **keys, bool sendsync)
409 struct openpgp_publickey *curkey = NULL;
410 struct openpgp_publickey *oldkey = NULL;
411 struct openpgp_publickey *prev = NULL;
415 if (loaded_backend == NULL) {
419 if (loaded_backend != NULL) {
420 if (loaded_backend->update_keys != NULL) {
421 return loaded_backend->update_keys(keys, sendsync);
425 for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
426 intrans = dynamic_starttrans();
427 logthing(LOGTHING_INFO,
428 "Fetching key 0x%" PRIX64 ", result: %d",
430 dynamic_fetch_key(get_keyid(curkey), &oldkey, intrans));
433 * If we already have the key stored in the DB then merge it
434 * with the new one that's been supplied. Otherwise the key
435 * we've just got is the one that goes in the DB and also the
436 * one that we send out.
438 if (oldkey != NULL) {
439 merge_keys(oldkey, curkey);
440 if (curkey->sigs == NULL &&
441 curkey->uids == NULL &&
442 curkey->subkeys == NULL) {
444 *keys = curkey->next;
446 prev->next = curkey->next;
448 free_publickey(curkey);
453 logthing(LOGTHING_INFO,
454 "Merged key; storing updated key.");
455 dynamic_store_key(oldkey, intrans, true);
457 free_publickey(oldkey);
461 logthing(LOGTHING_INFO,
462 "Storing completely new key.");
463 dynamic_store_key(curkey, intrans, false);
470 if (sendsync && keys != NULL) {
477 static void dynamic_initdb(bool readonly)
479 if (loaded_backend == NULL) {
483 if (loaded_backend != NULL) {
484 if (loaded_backend->initdb != NULL) {
485 loaded_backend->initdb(readonly);
490 static void dynamic_cleanupdb(void)
492 if (loaded_backend != NULL) {
493 if (loaded_backend->cleanupdb != NULL) {
494 loaded_backend->cleanupdb();
501 struct dbfuncs keydb_dynamic_funcs = {
502 .initdb = dynamic_initdb,
503 .cleanupdb = dynamic_cleanupdb,
504 .starttrans = dynamic_starttrans,
505 .endtrans = dynamic_endtrans,
506 .fetch_key = dynamic_fetch_key,
507 .fetch_key_text = dynamic_fetch_key_text,
508 .fetch_key_skshash = dynamic_fetch_key_skshash,
509 .store_key = dynamic_store_key,
510 .update_keys = dynamic_update_keys,
511 .delete_key = dynamic_delete_key,
512 .getkeysigs = dynamic_getkeysigs,
513 .cached_getkeysigs = dynamic_cached_getkeysigs,
514 .keyid2uid = dynamic_keyid2uid,
515 .getfullkeyid = dynamic_getfullkeyid,
516 .iterate_keys = dynamic_iterate_keys,