2 * keydb_dynamic.c - backend that can load the other backends
4 * Brett Parker <iDunno@sommitrealweird.co.uk>
6 * Copyright 2005 Project Purple
11 #include "decodekey.h"
15 #include "keystructs.h"
20 #include "keydb_dynamic.h"
22 struct dynamic_backend *get_backend(void)
24 return &__dynamicdb_backend__;
27 bool backend_loaded(void)
29 return __dynamicdb_backend__.loaded;
32 bool load_backend(void)
36 struct dynamic_backend *backend = get_backend();
38 if (backend->loaded) {
42 if (!config.db_backend) {
43 logthing(LOGTHING_CRITICAL, "No database backend defined.");
47 if (config.backends_dir == NULL) {
48 soname = malloc(strlen(config.db_backend)
49 + strlen("/libkeydb_")
53 sprintf(soname, "libkeydb_%s.so", config.db_backend);
55 soname = malloc(strlen(config.db_backend)
56 + strlen("/libkeydb_")
58 + strlen(config.backends_dir)
61 sprintf(soname, "%s/libkeydb_%s.so", config.backends_dir,
65 logthing(LOGTHING_INFO, "Loading dynamic backend: %s", soname);
67 handle = dlopen(soname, RTLD_LAZY);
69 logthing(LOGTHING_ERROR,
70 "Failed to open handle to library '%s': %s",
79 backend->initdb = (initdbfunc_t) dlsym(handle, "initdb");
80 backend->cleanupdb = (cleanupdbfunc_t) dlsym(handle, "cleanupdb");
81 backend->starttrans = (starttransfunc_t) dlsym(handle, "starttrans");
82 backend->endtrans = (endtransfunc_t) dlsym(handle, "endtrans");
83 backend->fetch_key = (fetch_keyfunc_t) dlsym(handle, "fetch_key");
84 backend->store_key = (store_keyfunc_t) dlsym(handle, "store_key");
85 backend->delete_key = (delete_keyfunc_t) dlsym(handle, "delete_key");
86 backend->fetch_key_text = (fetch_key_textfunc_t)
87 dlsym (handle, "fetch_key_text");
88 backend->update_keys = (update_keysfunc_t)
89 dlsym(handle, "update_keys");
90 backend->keyid2uid = (keyid2uidfunc_t) dlsym(handle, "keyid2uid");
91 backend->cached_getkeysigs = (cached_getkeysigsfunc_t)
92 dlsym(handle, "cached_getkeysigs");
93 backend->getfullkeyid = (getfullkeyidfunc_t)
94 dlsym(handle, "getfullkeyid");
95 backend->iterate_keys = (iterate_keysfunc_t)
96 dlsym(handle, "iterate_keys");
98 backend->handle = handle;
99 backend->loaded = true;
104 bool close_backend(void)
106 struct dynamic_backend *backend;
107 backend = get_backend();
109 backend->initdb = NULL;
110 backend->cleanupdb = NULL;
111 backend->starttrans = NULL;
112 backend->endtrans = NULL;
113 backend->fetch_key = NULL;
114 backend->store_key = NULL;
115 backend->delete_key = NULL;
116 backend->fetch_key_text = NULL;
117 backend->update_keys = NULL;
118 backend->keyid2uid = NULL;
119 backend->cached_getkeysigs = NULL;
120 backend->getfullkeyid = NULL;
121 backend->iterate_keys = NULL;
122 backend->loaded = false;
123 dlclose(backend->handle);
124 backend->handle = NULL;
130 * keyid2uid - Takes a keyid and returns the primary UID for it.
131 * @keyid: The keyid to lookup.
133 char *keyid2uid(uint64_t keyid)
135 struct openpgp_publickey *publickey = NULL;
136 struct openpgp_signedpacket_list *curuid = NULL;
139 struct dynamic_backend *backend;
140 if (!backend_loaded()) {
144 if (backend_loaded()) {
145 backend = get_backend();
146 if (backend->keyid2uid != NULL) {
147 return backend->keyid2uid(keyid);
152 if (fetch_key(keyid, &publickey, false) && publickey != NULL) {
153 curuid = publickey->uids;
154 while (curuid != NULL && buf[0] == 0) {
155 if (curuid->packet->tag == 13) {
156 snprintf(buf, 1023, "%.*s",
157 (int) curuid->packet->length,
158 curuid->packet->data);
160 curuid = curuid -> next;
162 free_publickey(publickey);
173 * getkeysigs - Gets a linked list of the signatures on a key.
174 * @keyid: The keyid to get the sigs for.
175 * @revoked: Is the key revoked?
177 * This function gets the list of signatures on a key. Used for key
178 * indexing and doing stats bits. If revoked is non-NULL then if the key
179 * is revoked it's set to true.
181 struct ll *getkeysigs(uint64_t keyid, bool *revoked)
183 struct ll *sigs = NULL;
184 struct openpgp_signedpacket_list *uids = NULL;
185 struct openpgp_publickey *publickey = NULL;
187 struct dynamic_backend *backend;
188 if ( !backend_loaded() ) {
192 if (backend_loaded()) {
193 backend = get_backend();
194 if (backend->getkeysigs != NULL) {
195 return backend->getkeysigs(keyid,revoked);
199 fetch_key(keyid, &publickey, false);
201 if (publickey != NULL) {
202 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
203 sigs = keysigs(sigs, uids->sigs);
205 if (revoked != NULL) {
206 *revoked = (publickey->revocations != NULL);
208 free_publickey(publickey);
215 * cached_getkeysigs - Gets the signatures on a key.
216 * @keyid: The key we want the signatures for.
218 * This function gets the signatures on a key. It's the same as the
219 * getkeysigs function above except we use the hash module to cache the
220 * data so if we need it again it's already loaded.
222 struct ll *cached_getkeysigs(uint64_t keyid)
224 struct stats_key *key = NULL;
225 struct stats_key *signedkey = NULL;
226 struct ll *cursig = NULL;
227 bool revoked = false;
229 struct dynamic_backend *backend;
235 if (!backend_loaded()) {
239 if (backend_loaded()) {
240 backend = get_backend();
241 if (backend->cached_getkeysigs != NULL) {
242 return backend->cached_getkeysigs(keyid);
246 key = createandaddtohash(keyid);
248 if (key->gotsigs == false) {
249 key->sigs = getkeysigs(key->keyid, &revoked);
250 key->revoked = revoked;
251 for (cursig = key->sigs; cursig != NULL;
252 cursig = cursig->next) {
253 signedkey = (struct stats_key *) cursig->object;
254 signedkey->signs = lladd(signedkey->signs, key);
263 * getfullkeyid - Maps a 32bit key id to a 64bit one.
264 * @keyid: The 32bit keyid.
266 * This function maps a 32bit key id to the full 64bit one. It returns the
267 * full keyid. If the key isn't found a keyid of 0 is returned.
269 uint64_t getfullkeyid(uint64_t keyid)
271 struct openpgp_publickey *publickey = NULL;
272 struct dynamic_backend *backend;
274 if (!backend_loaded()) {
278 if (backend_loaded()) {
279 backend = get_backend();
280 if (backend->getfullkeyid != NULL) {
281 return backend->getfullkeyid(keyid);
285 if (keyid < 0x100000000LL) {
286 fetch_key(keyid, &publickey, false);
287 if (publickey != NULL) {
288 keyid = get_keyid(publickey);
289 free_publickey(publickey);
300 * update_keys - Takes a list of public keys and updates them in the DB.
301 * @keys: The keys to update in the DB.
302 * @sendsync: Should we send a sync mail to our peers.
304 * Takes a list of keys and adds them to the database, merging them with
305 * the key in the database if it's already present there. The key list is
306 * update to contain the minimum set of updates required to get from what
307 * we had before to what we have now (ie the set of data that was added to
308 * the DB). Returns the number of entirely new keys added.
310 int update_keys(struct openpgp_publickey **keys, bool sendsync)
312 struct openpgp_publickey *curkey = NULL;
313 struct openpgp_publickey *oldkey = NULL;
314 struct openpgp_publickey *prev = NULL;
315 struct dynamic_backend *backend;
319 if (!backend_loaded()) {
323 if (backend_loaded()) {
324 backend = get_backend();
325 if (backend->update_keys != NULL) {
326 return backend->update_keys(keys, sendsync);
330 for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
331 intrans = starttrans();
332 logthing(LOGTHING_INFO,
333 "Fetching key 0x%llX, result: %d",
335 fetch_key(get_keyid(curkey), &oldkey, intrans));
338 * If we already have the key stored in the DB then merge it
339 * with the new one that's been supplied. Otherwise the key
340 * we've just got is the one that goes in the DB and also the
341 * one that we send out.
343 if (oldkey != NULL) {
344 merge_keys(oldkey, curkey);
345 if (curkey->revocations == NULL &&
346 curkey->uids == NULL &&
347 curkey->subkeys == NULL) {
349 *keys = curkey->next;
351 prev->next = curkey->next;
353 free_publickey(curkey);
358 logthing(LOGTHING_INFO,
359 "Merged key; storing updated key.");
360 store_key(oldkey, intrans, true);
362 free_publickey(oldkey);
366 logthing(LOGTHING_INFO,
367 "Storing completely new key.");
368 store_key(curkey, intrans, false);
375 if (sendsync && keys != NULL) {
382 void initdb(bool readonly)
384 struct dynamic_backend *backend;
385 backend = get_backend();
387 if (!backend_loaded()) {
391 if (backend->loaded) {
392 if (backend->initdb != NULL) {
393 backend->initdb(readonly);
400 struct dynamic_backend *backend;
401 backend = get_backend();
403 if (backend->loaded) {
404 if (backend->cleanupdb != NULL) {
405 backend->cleanupdb();
414 struct dynamic_backend *backend;
415 backend = get_backend();
417 if (!backend_loaded()) {
421 if (backend->loaded) {
422 if (backend->starttrans != NULL) {
423 return backend->starttrans();
432 struct dynamic_backend *backend;
433 backend = get_backend();
435 if (!backend_loaded()) {
439 if (backend->loaded) {
440 if (backend->endtrans != NULL) {
446 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
449 struct dynamic_backend *backend;
450 backend = get_backend();
452 if (!backend_loaded()) {
456 if (backend->loaded) {
457 if (backend->fetch_key != NULL) {
458 return backend->fetch_key(keyid,publickey,intrans);
465 int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
467 struct dynamic_backend *backend;
468 backend = get_backend();
470 if (!backend_loaded()) {
474 if (backend->loaded) {
475 if (backend->store_key != NULL) {
476 return backend->store_key(publickey,intrans,update);
483 int delete_key(uint64_t keyid, bool intrans)
485 struct dynamic_backend *backend;
486 backend = get_backend();
488 if (!backend_loaded()) {
492 if (backend->loaded) {
493 if (backend->delete_key != NULL) {
494 return backend->delete_key(keyid, intrans);
501 int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
503 struct dynamic_backend *backend;
504 backend = get_backend();
506 if (!backend_loaded()) {
510 if (backend->loaded) {
511 if (backend->fetch_key_text != NULL) {
512 return backend->fetch_key_text(search, publickey);
519 int iterate_keys(void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
522 struct dynamic_backend *backend;
523 backend = get_backend();
525 if (!backend_loaded()) {
529 if (backend->loaded) {
530 if (backend->iterate_keys != NULL) {
531 return backend->iterate_keys(iterfunc, ctx);