each of the backends, which means we can maintain unique namespaces.
This should allow further cleanups/cunningness, but hopefully this is
enough to get us operating correctly across all platforms to start with.
$(CORE_OBJS) $(KEYDB_OBJ) $(LIBS) $(PROGS_LDFLAGS_EXTRA)
onak-conf.o: onak-conf.c onak-conf.h
- $(CC) $(CFLAGS) -DCONFIGFILE=\"@sysconfdir@/onak.conf\" -c onak-conf.c
+ $(CC) $(CFLAGS) -DCONFIGFILE=\"@sysconfdir@/onak.conf\" \
+ -DDBFUNCS=keydb_@DBTYPE@_funcs -c onak-conf.c
onak-mail.pl: onak-mail.pl.in
sed 's:@CONFIG@:@sysconfdir@/onak.conf:g' < onak-mail.pl.in > onak-mail.pl
fclose(stderr);
}
catchsignals();
- initdb(false);
+ config.dbbackend->initdb(false);
count = cleankeys(keys);
logthing(LOGTHING_INFO, "%d keys cleaned.",
count);
- count = update_keys(&keys, true);
+ count = config.dbbackend->update_keys(&keys, true);
logthing(LOGTHING_NOTICE, "Got %d new keys.",
count);
keys = NULL;
}
- cleanupdb();
+ config.dbbackend->cleanupdb();
} else {
puts("No OpenPGP packets found in input.");
end_html();
int rec;
int pathlen = 0;
- fullhave = getfullkeyid(have);
- fullwant = getfullkeyid(want);
+ fullhave = config.dbbackend->getfullkeyid(have);
+ fullwant = config.dbbackend->getfullkeyid(want);
/*
* Make sure the keys we have and want are in the cache.
*/
- cached_getkeysigs(fullhave);
- cached_getkeysigs(fullwant);
+ config.dbbackend->cached_getkeysigs(fullhave);
+ config.dbbackend->cached_getkeysigs(fullwant);
if ((keyinfoa = findinhash(fullhave)) == NULL) {
return 1;
*/
curkey = findinhash(keyinfoa->parent);
while (curkey != NULL && curkey->keyid != 0) {
- if (curkey->keyid != fullwant && fetch_key(
+ if (curkey->keyid != fullwant &&
+ config.dbbackend->fetch_key(
curkey->keyid,
- &publickey, false)) {
+ &publickey, false)) {
flatten_publickey(publickey,
&packets,
&list_end);
/*
* Add the destination key to the list of returned keys.
*/
- if (fetch_key(fullwant, &publickey, false)) {
+ if (config.dbbackend->fetch_key(fullwant, &publickey, false)) {
flatten_publickey(publickey,
&packets,
&list_end);
readconfig(NULL);
initlogthing("gpgwww", config.logfile);
catchsignals();
- initdb(true);
+ config.dbbackend->initdb(true);
inithash();
logthing(LOGTHING_NOTICE, "Looking for path from 0x%llX to 0x%llX.",
from,
dofindpath(from, to, true, 3);
}
destroyhash();
- cleanupdb();
+ config.dbbackend->cleanupdb();
cleanuplogthing();
cleanupconfig();
logthing(LOGTHING_INFO,
"Fetching 0x%llX, result: %d",
keyid,
+ config.dbbackend->
fetch_key(keyid, &key, false));
if (key != NULL) {
storebuf.size = 8192;
logthing(LOGTHING_INFO,
"Fetching %s, result: %d",
search,
+ config.dbbackend->
fetch_key_text(search, &key));
if (key != NULL) {
storebuf.size = 8192;
&packets,
0);
parse_keys(packets, &key);
- store_key(key, false, false);
+ config.dbbackend->store_key(key, false, false);
free_packet_list(packets);
packets = NULL;
free_publickey(key);
logthing(LOGTHING_INFO,
"Deleting 0x%llX, result: %d",
keyid,
- delete_key(keyid, false));
+ config.dbbackend->delete_key(
+ keyid, false));
}
break;
case KEYD_CMD_GETFULLKEYID:
ret = 1;
}
if (ret == 0) {
- keyid = getfullkeyid(keyid);
+ keyid = config.dbbackend->getfullkeyid(keyid);
write(fd, &keyid, sizeof(keyid));
}
break;
case KEYD_CMD_KEYITER:
cmd = KEYD_REPLY_OK;
write(fd, &cmd, sizeof(cmd));
- iterate_keys(iteratefunc, (void *) fd);
+ config.dbbackend->iterate_keys(iteratefunc,
+ (void *) fd);
bytes = 0;
write(fd, &bytes, sizeof(bytes));
break;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
- initdb(false);
+ config.dbbackend->initdb(false);
logthing(LOGTHING_NOTICE, "Accepting connections.");
while (!cleanup() && select(fd + 1, &rfds, NULL, NULL, NULL) != -1) {
sock_accept(fd);
FD_SET(fd, &rfds);
}
- cleanupdb();
+ config.dbbackend->cleanupdb();
sock_close(fd);
unlink(sockname);
}
* keyid2uid - Takes a keyid and returns the primary UID for it.
* @keyid: The keyid to lookup.
*/
-char *keyid2uid(uint64_t keyid)
+char *generic_keyid2uid(uint64_t keyid)
{
struct openpgp_publickey *publickey = NULL;
struct openpgp_signedpacket_list *curuid = NULL;
char buf[1024];
buf[0]=0;
- if (fetch_key(keyid, &publickey, false) && publickey != NULL) {
+ if (config.dbbackend->fetch_key(keyid, &publickey, false) &&
+ publickey != NULL) {
curuid = publickey->uids;
while (curuid != NULL && buf[0] == 0) {
if (curuid->packet->tag == 13) {
* indexing and doing stats bits. If revoked is non-NULL then if the key
* is revoked it's set to true.
*/
-struct ll *getkeysigs(uint64_t keyid, bool *revoked)
+struct ll *generic_getkeysigs(uint64_t keyid, bool *revoked)
{
struct ll *sigs = NULL;
struct openpgp_signedpacket_list *uids = NULL;
struct openpgp_publickey *publickey = NULL;
- fetch_key(keyid, &publickey, false);
+ config.dbbackend->fetch_key(keyid, &publickey, false);
if (publickey != NULL) {
for (uids = publickey->uids; uids != NULL; uids = uids->next) {
* getkeysigs function above except we use the hash module to cache the
* data so if we need it again it's already loaded.
*/
-struct ll *cached_getkeysigs(uint64_t keyid)
+struct ll *generic_cached_getkeysigs(uint64_t keyid)
{
struct stats_key *key = NULL;
struct stats_key *signedkey = NULL;
key = createandaddtohash(keyid);
if (key->gotsigs == false) {
- key->sigs = getkeysigs(key->keyid, &revoked);
+ key->sigs = config.dbbackend->getkeysigs(key->keyid, &revoked);
key->revoked = revoked;
for (cursig = key->sigs; cursig != NULL;
cursig = cursig->next) {
* This function maps a 32bit key id to the full 64bit one. It returns the
* full keyid. If the key isn't found a keyid of 0 is returned.
*/
-uint64_t getfullkeyid(uint64_t keyid)
+uint64_t generic_getfullkeyid(uint64_t keyid)
{
struct openpgp_publickey *publickey = NULL;
if (keyid < 0x100000000LL) {
- fetch_key(keyid, &publickey, false);
+ config.dbbackend->fetch_key(keyid, &publickey, false);
if (publickey != NULL) {
keyid = get_keyid(publickey);
free_publickey(publickey);
* we had before to what we have now (ie the set of data that was added to
* the DB). Returns the number of entirely new keys added.
*/
-int update_keys(struct openpgp_publickey **keys, bool sendsync)
+int generic_update_keys(struct openpgp_publickey **keys, bool sendsync)
{
struct openpgp_publickey *curkey = NULL;
struct openpgp_publickey *oldkey = NULL;
bool intrans;
for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
- intrans = starttrans();
+ intrans = config.dbbackend->starttrans();
logthing(LOGTHING_INFO,
"Fetching key 0x%llX, result: %d",
get_keyid(curkey),
- fetch_key(get_keyid(curkey), &oldkey, intrans));
+ config.dbbackend->fetch_key(get_keyid(curkey), &oldkey,
+ intrans));
/*
* If we already have the key stored in the DB then merge it
} else {
logthing(LOGTHING_INFO,
"Storing completely new key.");
- store_key(curkey, intrans, false);
+ config.dbbackend->store_key(curkey, intrans, false);
newkeys++;
}
- endtrans();
+ config.dbbackend->endtrans();
intrans = false;
}
#include "keystructs.h"
#include "ll.h"
+/**
+ * struct dbfuncs - All of the functions a DB backend exports.
+ */
+struct dbfuncs {
/**
* initdb - Initialize the key database.
* @readonly: If we'll only be reading the DB, not writing to it.
* this file are called in order to allow the DB to be initialized ready
* for access.
*/
-void initdb(bool readonly);
+ void (*initdb)(bool readonly);
/**
* cleanupdb - De-initialize the key database.
* This function should be called upon program exit to allow the DB to
* cleanup after itself.
*/
-void cleanupdb(void);
+ void (*cleanupdb)(void);
/**
* starttrans - Start a transaction.
* operations on the database to help speed it all up, or if we want
* something to only succeed if all relevant operations are successful.
*/
-bool starttrans(void);
+ bool (*starttrans)(void);
/**
* endtrans - End a transaction.
*
* Ends a transaction.
*/
-void endtrans(void);
+ void (*endtrans)(void);
/**
* fetch_key - Given a keyid fetch the key from storage.
*
* TODO: What about keyid collisions? Should we use fingerprint instead?
*/
-int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey, bool intrans);
+ int (*fetch_key)(uint64_t keyid, struct openpgp_publickey **publickey,
+ bool intrans);
/**
* store_key - Takes a key and stores it.
* TODO: Do we store multiple keys of the same id? Or only one and replace
* it?
*/
-int store_key(struct openpgp_publickey *publickey, bool intrans, bool update);
+ int (*store_key)(struct openpgp_publickey *publickey, bool intrans,
+ bool update);
/**
* delete_key - Given a keyid delete the key from storage.
* This function deletes a public key from whatever storage mechanism we
* are using. Returns 0 if the key existed.
*/
-int delete_key(uint64_t keyid, bool intrans);
+ int (*delete_key)(uint64_t keyid, bool intrans);
/**
* fetch_key_text - Trys to find the keys that contain the supplied text.
* This function searches for the supplied text and returns the keys that
* contain it.
*/
-int fetch_key_text(const char *search, struct openpgp_publickey **publickey);
+ int (*fetch_key_text)(const char *search,
+ struct openpgp_publickey **publickey);
/**
* update_keys - Takes a list of public keys and updates them in the DB.
* If sendsync is true then we send out a keysync mail to our sync peers
* with the update.
*/
-int update_keys(struct openpgp_publickey **keys, bool sendsync);
+ int (*update_keys)(struct openpgp_publickey **keys, bool sendsync);
/**
* keyid2uid - Takes a keyid and returns the primary UID for it.
* This function returns a UID for the given key. Returns NULL if the key
* isn't found.
*/
-char *keyid2uid(uint64_t keyid);
+ char * (*keyid2uid)(uint64_t keyid);
/**
* getkeysigs - Gets a linked list of the signatures on a key.
* indexing and doing stats bits. If revoked is non-NULL then if the key
* is revoked it's set to true.
*/
-struct ll *getkeysigs(uint64_t keyid, bool *revoked);
+ struct ll * (*getkeysigs)(uint64_t keyid, bool *revoked);
/**
* cached_getkeysigs - Gets the signatures on a key.
* This function gets the signatures on a key. It's the same as the
* getkeysigs function above except we use the hash module to cache the
*/
-struct ll *cached_getkeysigs(uint64_t keyid);
+ struct ll * (*cached_getkeysigs)(uint64_t keyid);
/**
* getfullkeyid - Maps a 32bit key id to a 64bit one.
* This function maps a 32bit key id to the full 64bit one. It returns the
* full keyid. If the key isn't found a keyid of 0 is returned.
*/
-uint64_t getfullkeyid(uint64_t keyid);
+ uint64_t (*getfullkeyid)(uint64_t keyid);
/**
* iterate_keys - call a function once for each key in the db.
*
* Returns the number of keys we iterated over.
*/
-int iterate_keys(void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
- void *ctx);
+ int (*iterate_keys)(void (*iterfunc)(void *ctx,
+ struct openpgp_publickey *key), void *ctx);
+};
#endif /* __KEYDB_H__ */
return(dbconns[keytrun % numdbs]);
}
+/**
+ * starttrans - Start a transaction.
+ *
+ * Start a transaction. Intended to be used if we're about to perform many
+ * operations on the database to help speed it all up, or if we want
+ * something to only succeed if all relevant operations are successful.
+ */
+static bool db4_starttrans(void)
+{
+ int ret;
+
+ log_assert(dbenv != NULL);
+ log_assert(txn == NULL);
+
+ ret = dbenv->txn_begin(dbenv,
+ NULL, /* No parent transaction */
+ &txn,
+ 0);
+ if (ret != 0) {
+ logthing(LOGTHING_CRITICAL,
+ "Error starting transaction: %s",
+ db_strerror(ret));
+ exit(1);
+ }
+
+ return true;
+}
+
+/**
+ * endtrans - End a transaction.
+ *
+ * Ends a transaction.
+ */
+static void db4_endtrans(void)
+{
+ int ret;
+
+ log_assert(dbenv != NULL);
+ log_assert(txn != NULL);
+
+ ret = txn->commit(txn,
+ 0);
+ if (ret != 0) {
+ logthing(LOGTHING_CRITICAL,
+ "Error ending transaction: %s",
+ db_strerror(ret));
+ exit(1);
+ }
+ txn = NULL;
+
+ return;
+}
+
/**
* initdb - Initialize the key database.
*
* this file are called in order to allow the DB to be initialized ready
* for access.
*/
-void initdb(bool readonly)
+static void db4_initdb(bool readonly)
{
char buf[1024];
FILE *numdb = NULL;
}
if (ret == 0) {
- starttrans();
+ db4_starttrans();
for (i = 0; !ret && i < numdbs; i++) {
ret = db_create(&dbconns[i], dbenv, 0);
* This function should be called upon program exit to allow the DB to
* cleanup after itself.
*/
-void cleanupdb(void)
+static void db4_cleanupdb(void)
{
int i = 0;
}
}
-/**
- * starttrans - Start a transaction.
- *
- * Start a transaction. Intended to be used if we're about to perform many
- * operations on the database to help speed it all up, or if we want
- * something to only succeed if all relevant operations are successful.
- */
-bool starttrans(void)
-{
- int ret;
-
- log_assert(dbenv != NULL);
- log_assert(txn == NULL);
-
- ret = dbenv->txn_begin(dbenv,
- NULL, /* No parent transaction */
- &txn,
- 0);
- if (ret != 0) {
- logthing(LOGTHING_CRITICAL,
- "Error starting transaction: %s",
- db_strerror(ret));
- exit(1);
- }
-
- return true;
-}
-
-/**
- * endtrans - End a transaction.
- *
- * Ends a transaction.
- */
-void endtrans(void)
-{
- int ret;
-
- log_assert(dbenv != NULL);
- log_assert(txn != NULL);
-
- ret = txn->commit(txn,
- 0);
- if (ret != 0) {
- logthing(LOGTHING_CRITICAL,
- "Error ending transaction: %s",
- db_strerror(ret));
- exit(1);
- }
- txn = NULL;
-
- return;
-}
-
/**
* fetch_key - Given a keyid fetch the key from storage.
* @keyid: The keyid to fetch.
* in and then parse_keys() to parse the packets into a publickey
* structure.
*/
-int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
+static int db4_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
bool intrans)
{
struct openpgp_packet_list *packets = NULL;
key.data = &keyid;
if (!intrans) {
- starttrans();
+ db4_starttrans();
}
ret = keydb(keyid)->get(keydb(keyid),
* This function searches for the supplied text and returns the keys that
* contain it.
*/
-int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
+static int db4_fetch_key_text(const char *search,
+ struct openpgp_publickey **publickey)
{
DBC *cursor = NULL;
DBT key, data;
wordlist = makewordlist(wordlist, searchtext);
for (curword = wordlist; curword != NULL; curword = curword->next) {
- starttrans();
+ db4_starttrans();
ret = worddb->cursor(worddb,
txn,
llfree(wordlist, NULL);
wordlist = NULL;
- starttrans();
+ db4_starttrans();
for (i = 0; i < keylist.count; i++) {
numkeys += fetch_key(keylist.keys[i],
publickey,
* the file. If update is true then we delete the old key first, otherwise
* we trust that it doesn't exist.
*/
-int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
+static int db4_store_key(struct openpgp_publickey *publickey, bool intrans,
+ bool update)
{
struct openpgp_packet_list *packets = NULL;
struct openpgp_packet_list *list_end = NULL;
keyid = get_keyid(publickey);
if (!intrans) {
- starttrans();
+ db4_starttrans();
}
/*
* This function deletes a public key from whatever storage mechanism we
* are using. Returns 0 if the key existed.
*/
-int delete_key(uint64_t keyid, bool intrans)
+static int db4_delete_key(uint64_t keyid, bool intrans)
{
struct openpgp_publickey *publickey = NULL;
DBT key, data;
bool deadlock = false;
if (!intrans) {
- starttrans();
+ db4_starttrans();
}
fetch_key(keyid, &publickey, true);
*
* Returns the number of keys we iterated over.
*/
-int iterate_keys(void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
- void *ctx)
+static int db4_iterate_keys(void (*iterfunc)(void *ctx,
+ struct openpgp_publickey *key), void *ctx)
{
DBT dbkey, data;
DBC *cursor = NULL;
* This function maps a 32bit key id to the full 64bit one. It returns the
* full keyid. If the key isn't found a keyid of 0 is returned.
*/
-uint64_t getfullkeyid(uint64_t keyid)
+static uint64_t db4_getfullkeyid(uint64_t keyid)
{
DBT key, data;
DBC *cursor = NULL;
* Copyright 2005 Project Purple
*/
+#include <dlfcn.h>
#include <stdio.h>
+#include <string.h>
#include "decodekey.h"
#include "hash.h"
#include "keydb.h"
#include "keyid.h"
#include "keystructs.h"
+#include "log.h"
#include "mem.h"
#include "merge.h"
+#include "onak-conf.h"
#include "parsekey.h"
#include "sendsync.h"
-#include "keydb_dynamic.h"
-struct dynamic_backend *get_backend(void)
-{
- return &__dynamicdb_backend__;
-}
+static struct dbfuncs *loaded_backend = NULL;
+static char *backendsoname;
+static void *backend_handle;
-bool backend_loaded(void)
+static bool close_backend(void)
{
- return __dynamicdb_backend__.loaded;
+ loaded_backend = NULL;
+ dlclose(backend_handle);
+ backend_handle = NULL;
+
+ return true;
}
-bool load_backend(void)
+static bool load_backend(void)
{
char *soname = NULL;
- void *handle;
- struct dynamic_backend *backend = get_backend();
+ char *funcsname = NULL;
- if (backend->loaded) {
+ if (loaded_backend != NULL) {
close_backend();
+ loaded_backend = NULL;
}
if (!config.db_backend) {
logthing(LOGTHING_INFO, "Loading dynamic backend: %s", soname);
- handle = dlopen(soname, RTLD_LAZY);
- if (handle == NULL) {
+ backend_handle = dlopen(soname, RTLD_LAZY);
+ if (backend_handle == NULL) {
logthing(LOGTHING_CRITICAL,
"Failed to open handle to library '%s': %s",
soname, dlerror());
soname = NULL;
exit(EXIT_FAILURE);
}
+
+ funcsname = malloc(strlen(config.db_backend)
+ + strlen("keydb_")
+ + strlen("_funcs")
+ + 1);
+ sprintf(funcsname, "keydb_%s_funcs", config.db_backend);
+
+ loaded_backend = dlsym(backend_handle, funcsname);
+ free(funcsname);
+
+ if (loaded_backend == NULL) {
+ logthing(LOGTHING_CRITICAL,
+ "Failed to find dbfuncs structure in library "
+ "'%s' : %s", soname, dlerror());
+ free(soname);
+ soname = NULL;
+ exit(EXIT_FAILURE);
+ }
free(soname);
soname = NULL;
- backend->initdb = (initdbfunc_t) dlsym(handle, "initdb");
- backend->cleanupdb = (cleanupdbfunc_t) dlsym(handle, "cleanupdb");
- backend->starttrans = (starttransfunc_t) dlsym(handle, "starttrans");
- backend->endtrans = (endtransfunc_t) dlsym(handle, "endtrans");
- backend->fetch_key = (fetch_keyfunc_t) dlsym(handle, "fetch_key");
- backend->store_key = (store_keyfunc_t) dlsym(handle, "store_key");
- backend->delete_key = (delete_keyfunc_t) dlsym(handle, "delete_key");
- backend->fetch_key_text = (fetch_key_textfunc_t)
- dlsym (handle, "fetch_key_text");
- backend->update_keys = (update_keysfunc_t)
- dlsym(handle, "update_keys");
- backend->keyid2uid = (keyid2uidfunc_t) dlsym(handle, "keyid2uid");
- backend->cached_getkeysigs = (cached_getkeysigsfunc_t)
- dlsym(handle, "cached_getkeysigs");
- backend->getfullkeyid = (getfullkeyidfunc_t)
- dlsym(handle, "getfullkeyid");
- backend->iterate_keys = (iterate_keysfunc_t)
- dlsym(handle, "iterate_keys");
-
- backend->handle = handle;
- backend->loaded = true;
-
return true;
}
-bool close_backend(void)
+static bool dynamic_starttrans()
{
struct dynamic_backend *backend;
- backend = get_backend();
+
+ if (loaded_backend == NULL) {
+ load_backend();
+ }
- backend->initdb = NULL;
- backend->cleanupdb = NULL;
- backend->starttrans = NULL;
- backend->endtrans = NULL;
- backend->fetch_key = NULL;
- backend->store_key = NULL;
- backend->delete_key = NULL;
- backend->fetch_key_text = NULL;
- backend->update_keys = NULL;
- backend->keyid2uid = NULL;
- backend->cached_getkeysigs = NULL;
- backend->getfullkeyid = NULL;
- backend->iterate_keys = NULL;
- backend->loaded = false;
- dlclose(backend->handle);
- backend->handle = NULL;
+ if (loaded_backend != NULL) {
+ if (loaded_backend->starttrans != NULL) {
+ return loaded_backend->starttrans();
+ }
+ }
- return true;
+ return false;
+}
+
+static void dynamic_endtrans()
+{
+ struct dynamic_backend *backend;
+
+ if (loaded_backend == NULL) {
+ load_backend();
+ }
+
+ if (loaded_backend != NULL) {
+ if (loaded_backend->endtrans != NULL) {
+ loaded_backend->endtrans();
+ }
+ }
+}
+
+static int dynamic_fetch_key(uint64_t keyid,
+ struct openpgp_publickey **publickey, bool intrans)
+{
+ struct dynamic_backend *backend;
+
+ if (loaded_backend == NULL) {
+ load_backend();
+ }
+
+ if (loaded_backend != NULL) {
+ if (loaded_backend->fetch_key != NULL) {
+ return loaded_backend->fetch_key(keyid,publickey,intrans);
+ }
+ }
+
+ return -1;
+}
+
+static int dynamic_store_key(struct openpgp_publickey *publickey, bool intrans,
+ bool update)
+{
+ struct dynamic_backend *backend;
+
+ if (loaded_backend == NULL) {
+ load_backend();
+ }
+
+ if (loaded_backend != NULL) {
+ if (loaded_backend->store_key != NULL) {
+ return loaded_backend->store_key(publickey,intrans,update);
+ }
+ }
+
+ return -1;
+}
+
+static int dynamic_delete_key(uint64_t keyid, bool intrans)
+{
+ struct dynamic_backend *backend;
+
+ if (loaded_backend == NULL) {
+ load_backend();
+ }
+
+ if (loaded_backend != NULL) {
+ if (loaded_backend->delete_key != NULL) {
+ return loaded_backend->delete_key(keyid, intrans);
+ }
+ }
+
+ return -1;
+}
+
+static int dynamic_fetch_key_text(const char *search,
+ struct openpgp_publickey **publickey)
+{
+ struct dynamic_backend *backend;
+
+ if (loaded_backend == NULL) {
+ load_backend();
+ }
+
+ if (loaded_backend != NULL) {
+ if (loaded_backend->fetch_key_text != NULL) {
+ return loaded_backend->fetch_key_text(search, publickey);
+ }
+ }
+
+ return -1;
+}
+
+static int dynamic_iterate_keys(void (*iterfunc)(void *ctx,
+ struct openpgp_publickey *key), void *ctx)
+{
+ struct dynamic_backend *backend;
+
+ if (loaded_backend == NULL) {
+ load_backend();
+ }
+
+ if (loaded_backend != NULL) {
+ if (loaded_backend->iterate_keys != NULL) {
+ return loaded_backend->iterate_keys(iterfunc, ctx);
+ }
+ }
+
+ return -1;
}
/**
* keyid2uid - Takes a keyid and returns the primary UID for it.
* @keyid: The keyid to lookup.
*/
-char *keyid2uid(uint64_t keyid)
+static char *dynamic_keyid2uid(uint64_t keyid)
{
struct openpgp_publickey *publickey = NULL;
struct openpgp_signedpacket_list *curuid = NULL;
char buf[1024];
struct dynamic_backend *backend;
- if (!backend_loaded()) {
+ if (loaded_backend == NULL) {
load_backend();
}
- if (backend_loaded()) {
- backend = get_backend();
- if (backend->keyid2uid != NULL) {
- return backend->keyid2uid(keyid);
+ if (loaded_backend != NULL) {
+ if (loaded_backend->keyid2uid != NULL) {
+ return loaded_backend->keyid2uid(keyid);
}
}
buf[0]=0;
- if (fetch_key(keyid, &publickey, false) && publickey != NULL) {
+ if (dynamic_fetch_key(keyid, &publickey, false) && publickey != NULL) {
curuid = publickey->uids;
while (curuid != NULL && buf[0] == 0) {
if (curuid->packet->tag == 13) {
* indexing and doing stats bits. If revoked is non-NULL then if the key
* is revoked it's set to true.
*/
-struct ll *getkeysigs(uint64_t keyid, bool *revoked)
+static struct ll *dynamic_getkeysigs(uint64_t keyid, bool *revoked)
{
struct ll *sigs = NULL;
struct openpgp_signedpacket_list *uids = NULL;
struct openpgp_publickey *publickey = NULL;
struct dynamic_backend *backend;
- if ( !backend_loaded() ) {
+ if ( loaded_backend == NULL ) {
load_backend();
}
- if (backend_loaded()) {
- backend = get_backend();
- if (backend->getkeysigs != NULL) {
- return backend->getkeysigs(keyid,revoked);
+ if (loaded_backend != NULL) {
+ if (loaded_backend->getkeysigs != NULL) {
+ return loaded_backend->getkeysigs(keyid,revoked);
}
}
- fetch_key(keyid, &publickey, false);
+ dynamic_fetch_key(keyid, &publickey, false);
if (publickey != NULL) {
for (uids = publickey->uids; uids != NULL; uids = uids->next) {
* getkeysigs function above except we use the hash module to cache the
* data so if we need it again it's already loaded.
*/
-struct ll *cached_getkeysigs(uint64_t keyid)
+static struct ll *dynamic_cached_getkeysigs(uint64_t keyid)
{
struct stats_key *key = NULL;
struct stats_key *signedkey = NULL;
return NULL;
}
- if (!backend_loaded()) {
+ if (loaded_backend == NULL) {
load_backend();
}
- if (backend_loaded()) {
- backend = get_backend();
- if (backend->cached_getkeysigs != NULL) {
- return backend->cached_getkeysigs(keyid);
+ if (loaded_backend != NULL) {
+ if (loaded_backend->cached_getkeysigs != NULL) {
+ return loaded_backend->cached_getkeysigs(keyid);
}
}
key = createandaddtohash(keyid);
if (key->gotsigs == false) {
- key->sigs = getkeysigs(key->keyid, &revoked);
+ key->sigs = dynamic_getkeysigs(key->keyid, &revoked);
key->revoked = revoked;
for (cursig = key->sigs; cursig != NULL;
cursig = cursig->next) {
* This function maps a 32bit key id to the full 64bit one. It returns the
* full keyid. If the key isn't found a keyid of 0 is returned.
*/
-uint64_t getfullkeyid(uint64_t keyid)
+static uint64_t dynamic_getfullkeyid(uint64_t keyid)
{
struct openpgp_publickey *publickey = NULL;
struct dynamic_backend *backend;
- if (!backend_loaded()) {
+ if (loaded_backend == NULL) {
load_backend();
}
- if (backend_loaded()) {
- backend = get_backend();
- if (backend->getfullkeyid != NULL) {
- return backend->getfullkeyid(keyid);
+ if (loaded_backend != NULL) {
+ if (loaded_backend->getfullkeyid != NULL) {
+ return loaded_backend->getfullkeyid(keyid);
}
}
if (keyid < 0x100000000LL) {
- fetch_key(keyid, &publickey, false);
+ dynamic_fetch_key(keyid, &publickey, false);
if (publickey != NULL) {
keyid = get_keyid(publickey);
free_publickey(publickey);
* we had before to what we have now (ie the set of data that was added to
* the DB). Returns the number of entirely new keys added.
*/
-int update_keys(struct openpgp_publickey **keys, bool sendsync)
+static int dynamic_update_keys(struct openpgp_publickey **keys, bool sendsync)
{
struct openpgp_publickey *curkey = NULL;
struct openpgp_publickey *oldkey = NULL;
int newkeys = 0;
bool intrans;
- if (!backend_loaded()) {
+ if (loaded_backend == NULL) {
load_backend();
}
- if (backend_loaded()) {
- backend = get_backend();
- if (backend->update_keys != NULL) {
- return backend->update_keys(keys, sendsync);
+ if (loaded_backend != NULL) {
+ if (loaded_backend->update_keys != NULL) {
+ return loaded_backend->update_keys(keys, sendsync);
}
}
for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
- intrans = starttrans();
+ intrans = dynamic_starttrans();
logthing(LOGTHING_INFO,
"Fetching key 0x%llX, result: %d",
get_keyid(curkey),
- fetch_key(get_keyid(curkey), &oldkey, intrans));
+ dynamic_fetch_key(get_keyid(curkey), &oldkey, intrans));
/*
* If we already have the key stored in the DB then merge it
prev = curkey;
logthing(LOGTHING_INFO,
"Merged key; storing updated key.");
- store_key(oldkey, intrans, true);
+ dynamic_store_key(oldkey, intrans, true);
}
free_publickey(oldkey);
oldkey = NULL;
} else {
logthing(LOGTHING_INFO,
"Storing completely new key.");
- store_key(curkey, intrans, false);
+ dynamic_store_key(curkey, intrans, false);
newkeys++;
}
- endtrans();
+ dynamic_endtrans();
intrans = false;
}
return newkeys;
}
-void initdb(bool readonly)
+static void dynamic_initdb(bool readonly)
{
struct dynamic_backend *backend;
- backend = get_backend();
- if (!backend_loaded()) {
+ if (loaded_backend == NULL) {
load_backend();
}
- if (backend->loaded) {
- if (backend->initdb != NULL) {
- backend->initdb(readonly);
+ if (loaded_backend != NULL) {
+ if (loaded_backend->initdb != NULL) {
+ loaded_backend->initdb(readonly);
}
}
}
-void cleanupdb(void)
+static void dynamic_cleanupdb(void)
{
struct dynamic_backend *backend;
- backend = get_backend();
- if (backend->loaded) {
- if (backend->cleanupdb != NULL) {
- backend->cleanupdb();
+ if (loaded_backend != NULL) {
+ if (loaded_backend->cleanupdb != NULL) {
+ loaded_backend->cleanupdb();
}
}
close_backend();
}
-bool starttrans()
-{
- struct dynamic_backend *backend;
- backend = get_backend();
-
- if (!backend_loaded()) {
- load_backend();
- }
-
- if (backend->loaded) {
- if (backend->starttrans != NULL) {
- return backend->starttrans();
- }
- }
-
- return false;
-}
-
-void endtrans()
-{
- struct dynamic_backend *backend;
- backend = get_backend();
-
- if (!backend_loaded()) {
- load_backend();
- }
-
- if (backend->loaded) {
- if (backend->endtrans != NULL) {
- backend->endtrans();
- }
- }
-}
-
-int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
- bool intrans)
-{
- struct dynamic_backend *backend;
- backend = get_backend();
-
- if (!backend_loaded()) {
- load_backend();
- }
-
- if (backend->loaded) {
- if (backend->fetch_key != NULL) {
- return backend->fetch_key(keyid,publickey,intrans);
- }
- }
-
- return -1;
-}
-
-int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
-{
- struct dynamic_backend *backend;
- backend = get_backend();
-
- if (!backend_loaded()) {
- load_backend();
- }
-
- if (backend->loaded) {
- if (backend->store_key != NULL) {
- return backend->store_key(publickey,intrans,update);
- }
- }
-
- return -1;
-}
-
-int delete_key(uint64_t keyid, bool intrans)
-{
- struct dynamic_backend *backend;
- backend = get_backend();
-
- if (!backend_loaded()) {
- load_backend();
- }
-
- if (backend->loaded) {
- if (backend->delete_key != NULL) {
- return backend->delete_key(keyid, intrans);
- }
- }
-
- return -1;
-}
-
-int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
-{
- struct dynamic_backend *backend;
- backend = get_backend();
-
- if (!backend_loaded()) {
- load_backend();
- }
-
- if (backend->loaded) {
- if (backend->fetch_key_text != NULL) {
- return backend->fetch_key_text(search, publickey);
- }
- }
-
- return -1;
-}
-
-int iterate_keys(void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
- void *ctx)
-{
- struct dynamic_backend *backend;
- backend = get_backend();
-
- if (!backend_loaded()) {
- load_backend();
- }
-
- if (backend->loaded) {
- if (backend->iterate_keys != NULL) {
- return backend->iterate_keys(iterfunc, ctx);
- }
- }
-
- return -1;
-}
+struct dbfuncs keydb_dynamic_funcs = {
+ .initdb = dynamic_initdb,
+ .cleanupdb = dynamic_cleanupdb,
+ .starttrans = dynamic_starttrans,
+ .endtrans = dynamic_endtrans,
+ .fetch_key = dynamic_fetch_key,
+ .fetch_key_text = dynamic_fetch_key_text,
+ .store_key = dynamic_store_key,
+ .update_keys = dynamic_update_keys,
+ .delete_key = dynamic_delete_key,
+ .getkeysigs = dynamic_getkeysigs,
+ .cached_getkeysigs = dynamic_cached_getkeysigs,
+ .keyid2uid = dynamic_keyid2uid,
+ .getfullkeyid = dynamic_getfullkeyid,
+ .iterate_keys = dynamic_iterate_keys,
+};
*
* This is just a no-op for flat file access.
*/
-void initdb(bool readonly)
+static void file_initdb(bool readonly)
{
}
*
* This is just a no-op for flat file access.
*/
-void cleanupdb(void)
+static void file_cleanupdb(void)
{
}
*
* This is just a no-op for flat file access.
*/
-bool starttrans(void)
+static bool file_starttrans(void)
{
return true;
}
*
* This is just a no-op for flat file access.
*/
-void endtrans(void)
+static void file_endtrans(void)
{
return;
}
* in and then parse_keys() to parse the packets into a publickey
* structure.
*/
-int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
+static int file_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
bool intrans)
{
struct openpgp_packet_list *packets = NULL;
* packets and then use write_openpgp_stream() to write the stream out to
* the file.
*/
-int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
+static int file_store_key(struct openpgp_publickey *publickey, bool intrans,
+ bool update)
{
struct openpgp_packet_list *packets = NULL;
struct openpgp_packet_list *list_end = NULL;
* This function deletes a public key from whatever storage mechanism we
* are using. Returns 0 if the key existed.
*/
-int delete_key(uint64_t keyid, bool intrans)
+static int file_delete_key(uint64_t keyid, bool intrans)
{
char keyfile[1024];
*
* TODO: Write for flat file access. Some sort of grep?
*/
-int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
+static int file_fetch_key_text(const char *search,
+ struct openpgp_publickey **publickey)
{
return 0;
}
*
* Returns the number of keys we iterated over.
*/
-int iterate_keys(void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
- void *ctx)
+static int file_iterate_keys(void (*iterfunc)(void *ctx,
+ struct openpgp_publickey *key), void *ctx)
{
int numkeys = 0;
struct openpgp_packet_list *packets = NULL;
#define NEED_GETFULLKEYID 1
#define NEED_UPDATEKEYS 1
#include "keydb.c"
+
+struct dbfuncs keydb_file_funcs = {
+ .initdb = file_initdb,
+ .cleanupdb = file_cleanupdb,
+ .starttrans = file_starttrans,
+ .endtrans = file_endtrans,
+ .fetch_key = file_fetch_key,
+ .fetch_key_text = file_fetch_key_text,
+ .store_key = file_store_key,
+ .update_keys = generic_update_keys,
+ .delete_key = file_delete_key,
+ .getkeysigs = generic_getkeysigs,
+ .cached_getkeysigs = generic_cached_getkeysigs,
+ .keyid2uid = generic_keyid2uid,
+ .getfullkeyid = generic_getfullkeyid,
+ .iterate_keys = file_iterate_keys,
+};
}
-void keypath(char *buffer, uint64_t _keyid)
+static void keypath(char *buffer, uint64_t _keyid)
{
uint64_t keyid = _keyid << 32;
snprintf(buffer, PATH_MAX, "%s/key/%02X/%02X/%08X/%016llX",
(uint32_t) (keyid >> 32), _keyid);
}
-void keydir(char *buffer, uint64_t _keyid)
+static void keydir(char *buffer, uint64_t _keyid)
{
uint64_t keyid = _keyid << 32;
snprintf(buffer, PATH_MAX, "%s/key/%02X/%02X/%08X", config.db_dir,
(uint32_t) (keyid >> 32));
}
-void prove_path_to(uint64_t keyid, char *what)
+static void prove_path_to(uint64_t keyid, char *what)
{
static char buffer[1024];
snprintf(buffer, PATH_MAX, "%s/%s", config.db_dir, what);
mkdir(buffer, 0777);
}
-void wordpath(char *buffer, char *word, uint32_t hash, uint64_t keyid)
+static void wordpath(char *buffer, char *word, uint32_t hash, uint64_t keyid)
{
snprintf(buffer, PATH_MAX, "%s/words/%02X/%02X/%08X/%s/%016llX",
config.db_dir, (uint8_t) ((hash >> 24) & 0xFF),
(uint8_t) ((hash >> 16) & 0xFF), hash, word, keyid);
}
-void worddir(char *buffer, char *word, uint32_t hash)
+static void worddir(char *buffer, char *word, uint32_t hash)
{
snprintf(buffer, PATH_MAX, "%s/words/%02X/%02X/%08X/%s", config.db_dir,
(uint8_t) ((hash >> 24) & 0xFF),
(uint8_t) ((hash >> 16) & 0xFF), hash, word);
}
-void subkeypath(char *buffer, uint64_t subkey, uint64_t keyid)
+static void subkeypath(char *buffer, uint64_t subkey, uint64_t keyid)
{
snprintf(buffer, PATH_MAX, "%s/subkeys/%02X/%02X/%08X/%016llX",
config.db_dir,
keyid);
}
-void subkeydir(char *buffer, uint64_t subkey)
+static void subkeydir(char *buffer, uint64_t subkey)
{
snprintf(buffer, PATH_MAX, "%s/subkeys/%02X/%02X/%08X",
config.db_dir,
/**
* initdb - Initialize the key database.
*/
-void initdb(bool readonly)
+static void fs_initdb(bool readonly)
{
char buffer[PATH_MAX];
/**
* cleanupdb - De-initialize the key database.
*/
-void cleanupdb(void)
+static void fs_cleanupdb(void)
{
/* Mmmm nothing to do here? */
close(keydb_lockfile_fd);
/**
* starttrans - Start a transaction.
*/
-bool starttrans(void)
+static bool fs_starttrans(void)
{
struct flock lockstruct;
int remaining = 20;
/**
* endtrans - End a transaction.
*/
-void endtrans(void)
+static void fs_endtrans(void)
{
struct flock lockstruct;
* @publickey: A pointer to a structure to return the key in.
* @intrans: If we're already in a transaction.
*/
-int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
+static int fs_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
bool intrans)
{
static char buffer[PATH_MAX];
* @intrans: If we're already in a transaction.
* @update: If true the key exists and should be updated.
*/
-int store_key(struct openpgp_publickey *publickey, bool intrans,
+static int fs_store_key(struct openpgp_publickey *publickey, bool intrans,
bool update)
{
static char buffer[PATH_MAX];
* @keyid: The keyid to delete.
* @intrans: If we're already in a transaction.
*/
-int delete_key(uint64_t keyid, bool intrans)
+static int fs_delete_key(uint64_t keyid, bool intrans)
{
static char buffer[PATH_MAX];
int ret;
* @search: The text to search for.
* @publickey: A pointer to a structure to return the key in.
*/
-int fetch_key_text(const char *search,
+static int fs_fetch_key_text(const char *search,
struct openpgp_publickey **publickey)
{
struct ll *wordlist = NULL, *wl = NULL;
return addedkeys;
}
-uint64_t getfullkeyid(uint64_t keyid)
+static uint64_t fs_getfullkeyid(uint64_t keyid)
{
static char buffer[PATH_MAX];
DIR *d = NULL;
*
* Returns the number of keys we iterated over.
*/
-int iterate_keys(void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
- void *ctx)
+static int fs_iterate_keys(void (*iterfunc)(void *ctx,
+ struct openpgp_publickey *key), void *ctx)
{
return 0;
}
#define NEED_GETKEYSIGS 1
#define NEED_UPDATEKEYS 1
#include "keydb.c"
+
+struct dbfuncs keydb_fs_funcs = {
+ .initdb = fs_initdb,
+ .cleanupdb = fs_cleanupdb,
+ .starttrans = fs_starttrans,
+ .endtrans = fs_endtrans,
+ .fetch_key = fs_fetch_key,
+ .fetch_key_text = fs_fetch_key_text,
+ .store_key = fs_store_key,
+ .update_keys = generic_update_keys,
+ .delete_key = fs_delete_key,
+ .getkeysigs = generic_getkeysigs,
+ .cached_getkeysigs = generic_cached_getkeysigs,
+ .keyid2uid = generic_keyid2uid,
+ .getfullkeyid = fs_getfullkeyid,
+ .iterate_keys = fs_iterate_keys,
+};
* this file are called in order to allow the DB to be initialized ready
* for access.
*/
-void initdb(bool readonly)
+static void keyd_initdb(bool readonly)
{
struct sockaddr_un sock;
int cmd = KEYD_CMD_UNKNOWN;
* This function should be called upon program exit to allow the DB to
* cleanup after itself.
*/
-void cleanupdb(void)
+static void keyd_cleanupdb(void)
{
if (shutdown(keyd_fd, SHUT_RDWR) < 0) {
logthing(LOGTHING_NOTICE, "Error shutting down socket: %d",
* operations on the database to help speed it all up, or if we want
* something to only succeed if all relevant operations are successful.
*/
-bool starttrans(void)
+static bool keyd_starttrans(void)
{
return true;
}
*
* Ends a transaction.
*/
-void endtrans(void)
+static void keyd_endtrans(void)
{
return;
}
*
* TODO: What about keyid collisions? Should we use fingerprint instead?
*/
-int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
+static int keyd_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
bool intrans)
{
struct buffer_ctx keybuf;
* TODO: Do we store multiple keys of the same id? Or only one and replace
* it?
*/
-int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
+static int keyd_store_key(struct openpgp_publickey *publickey, bool intrans,
+ bool update)
{
struct buffer_ctx keybuf;
struct openpgp_packet_list *packets = NULL;
* This function deletes a public key from whatever storage mechanism we
* are using. Returns 0 if the key existed.
*/
-int delete_key(uint64_t keyid, bool intrans)
+static int keyd_delete_key(uint64_t keyid, bool intrans)
{
int cmd = KEYD_CMD_DELETE;
* This function searches for the supplied text and returns the keys that
* contain it.
*/
-int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
+static int keyd_fetch_key_text(const char *search,
+ struct openpgp_publickey **publickey)
{
struct buffer_ctx keybuf;
struct openpgp_packet_list *packets = NULL;
* This function maps a 32bit key id to the full 64bit one. It returns the
* full keyid. If the key isn't found a keyid of 0 is returned.
*/
-uint64_t getfullkeyid(uint64_t keyid)
+static uint64_t keyd_getfullkeyid(uint64_t keyid)
{
int cmd = KEYD_CMD_GETFULLKEYID;
*
* Returns the number of keys we iterated over.
*/
-int iterate_keys(void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
- void *ctx)
+static int keyd_iterate_keys(void (*iterfunc)(void *ctx,
+ struct openpgp_publickey *key), void *ctx)
{
struct buffer_ctx keybuf;
struct openpgp_packet_list *packets = NULL;
#define NEED_GETKEYSIGS 1
#define NEED_UPDATEKEYS 1
#include "keydb.c"
+
+struct dbfuncs keydb_keyd_funcs = {
+ .initdb = keyd_initdb,
+ .cleanupdb = keyd_cleanupdb,
+ .starttrans = keyd_starttrans,
+ .endtrans = keyd_endtrans,
+ .fetch_key = keyd_fetch_key,
+ .fetch_key_text = keyd_fetch_key_text,
+ .store_key = keyd_store_key,
+ .update_keys = generic_update_keys,
+ .delete_key = keyd_delete_key,
+ .getkeysigs = generic_getkeysigs,
+ .cached_getkeysigs = generic_cached_getkeysigs,
+ .keyid2uid = generic_keyid2uid,
+ .getfullkeyid = keyd_getfullkeyid,
+ .iterate_keys = keyd_iterate_keys,
+};
* this file are called in order to allow the DB to be initialized ready
* for access.
*/
-void initdb(bool readonly)
+static void pg_initdb(bool readonly)
{
dbconn = PQsetdbLogin(config.pg_dbhost, // host
NULL, // port
* This function should be called upon program exit to allow the DB to
* cleanup after itself.
*/
-void cleanupdb(void)
+static void pg_cleanupdb(void)
{
PQfinish(dbconn);
dbconn = NULL;
* operations on the database to help speed it all up, or if we want
* something to only succeed if all relevant operations are successful.
*/
-bool starttrans(void)
+static bool pg_starttrans(void)
{
PGresult *result = NULL;
*
* Ends a transaction.
*/
-void endtrans(void)
+static void pg_endtrans(void)
{
PGresult *result = NULL;
* in and then parse_keys() to parse the packets into a publickey
* structure.
*/
-int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
+static int pg_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
bool intrans)
{
struct openpgp_packet_list *packets = NULL;
* This function searches for the supplied text and returns the keys that
* contain it.
*/
-int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
+static int pg_fetch_key_text(const char *search,
+ struct openpgp_publickey **publickey)
{
struct openpgp_packet_list *packets = NULL;
PGresult *result = NULL;
* the file. If update is true then we delete the old key first, otherwise
* we trust that it doesn't exist.
*/
-int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
+static int pg_store_key(struct openpgp_publickey *publickey, bool intrans,
+ bool update)
{
struct openpgp_packet_list *packets = NULL;
struct openpgp_packet_list *list_end = NULL;
* it definitely needs updated.
*/
if (update) {
- delete_key(get_keyid(publickey), true);
+ pg_delete_key(get_keyid(publickey), true);
}
next = publickey->next;
* This function deletes a public key from whatever storage mechanism we
* are using. Returns 0 if the key existed.
*/
-int delete_key(uint64_t keyid, bool intrans)
+static int pg_delete_key(uint64_t keyid, bool intrans)
{
PGresult *result = NULL;
char *oids = NULL;
* keyid2uid - Takes a keyid and returns the primary UID for it.
* @keyid: The keyid to lookup.
*/
-char *keyid2uid(uint64_t keyid)
+static char *pg_keyid2uid(uint64_t keyid)
{
PGresult *result = NULL;
char statement[1024];
* This function gets the list of signatures on a key. Used for key
* indexing and doing stats bits.
*/
-struct ll *getkeysigs(uint64_t keyid, bool *revoked)
+static struct ll *pg_getkeysigs(uint64_t keyid, bool *revoked)
{
struct ll *sigs = NULL;
PGresult *result = NULL;
*
* Returns the number of keys we iterated over.
*/
-int iterate_keys(void (*iterfunc)(void *ctx, struct openpgp_publickey *key),
- void *ctx)
+static int pg_iterate_keys(void (*iterfunc)(void *ctx,
+ struct openpgp_publickey *key), void *ctx)
{
struct openpgp_packet_list *packets = NULL;
struct openpgp_publickey *key = NULL;
#define NEED_GETFULLKEYID 1
#define NEED_UPDATEKEYS 1
#include "keydb.c"
+
+struct dbfuncs keydb_pg_funcs = {
+ .initdb = pg_initdb,
+ .cleanupdb = pg_cleanupdb,
+ .starttrans = pg_starttrans,
+ .endtrans = pg_endtrans,
+ .fetch_key = pg_fetch_key,
+ .fetch_key_text = pg_fetch_key_text,
+ .store_key = pg_store_key,
+ .update_keys = generic_update_keys,
+ .delete_key = pg_delete_key,
+ .getkeysigs = pg_getkeysigs,
+ .cached_getkeysigs = generic_cached_getkeysigs,
+ .keyid2uid = pg_keyid2uid,
+ .getfullkeyid = generic_getfullkeyid,
+ .iterate_keys = pg_iterate_keys,
+};
#include "keyindex.h"
#include "keystructs.h"
#include "log.h"
+#include "onak-conf.h"
int list_sigs(struct openpgp_packet_list *sigs, bool html)
{
while (sigs != NULL) {
sigid = sig_keyid(sigs->packet);
- uid = keyid2uid(sigid);
+ uid = config.dbbackend->keyid2uid(sigid);
if (sigs->packet->data[0] == 4 &&
sigs->packet->data[1] == 0x30) {
/* It's a Type 4 sig revocation */
int count = 0;
if (ishex) {
- count = fetch_key(keyid, &publickey, false);
+ count = config.dbbackend->fetch_key(keyid, &publickey, false);
} else {
- count = fetch_key_text(search, &publickey);
+ count = config.dbbackend->fetch_key_text(search, &publickey);
}
if (publickey != NULL) {
if (mrhkp) {
readconfig(NULL);
initlogthing("lookup", config.logfile);
catchsignals();
- initdb(true);
+ config.dbbackend->initdb(true);
switch (op) {
case OP_GET:
logthing(LOGTHING_NOTICE, "Getting keyid 0x%llX",
keyid);
- if (fetch_key(keyid, &publickey, false)) {
+ if (config.dbbackend->fetch_key(keyid, &publickey,
+ false)) {
puts("<pre>");
cleankeys(publickey);
flatten_publickey(publickey,
true, mrhkp);
break;
case OP_PHOTO:
- if (fetch_key(keyid, &publickey, false)) {
+ if (config.dbbackend->fetch_key(keyid, &publickey,
+ false)) {
unsigned char *photo = NULL;
size_t length = 0;
default:
puts("Unknown operation!");
}
- cleanupdb();
+ config.dbbackend->cleanupdb();
cleanuplogthing();
cleanupconfig();
}
* My (noodles@earth.li, DSA) key is in the strongly connected set of
* keys, so we use it as a suitable starting seed.
*/
- cached_getkeysigs(0xF1BD4BE45B430367);
+ config.dbbackend->cached_getkeysigs(0xF1BD4BE45B430367);
/*
* Loop through the hash examining each key present and finding the
for (loop = 0; (loop < HASHSIZE) && (distance < max); loop++) {
curkey = gethashtableentry(loop);
while (curkey != NULL && distance < max) {
- cached_getkeysigs(((struct stats_key *)
+ config.dbbackend->cached_getkeysigs(
+ ((struct stats_key *)
curkey->object)->keyid);
initcolour(false);
tmp = furthestkey((struct stats_key *)
{
readconfig(NULL);
initlogthing("maxpath", config.logfile);
- initdb(true);
+ config.dbbackend->initdb(true);
inithash();
findmaxpath(30);
printf("--------\n");
findmaxpath(30);
destroyhash();
- cleanupdb();
+ config.dbbackend->cleanupdb();
cleanuplogthing();
cleanupconfig();
#include "log.h"
#include "onak-conf.h"
+extern struct dbfuncs DBFUNCS;
+
/*
* config - Runtime configuration for onak.
*
*/
NULL, /* db_backend */
NULL, /* backends_dir */
+
+ &DBFUNCS, /* Default dbfuncs struct */
};
void readconfig(const char *configfile) {
#ifndef __ONAK_CONF_H_
#define __ONAK_CONF_H_
+#include "keydb.h"
+
/*
* struct onak_config - Runtime configuration for onak.
* @maxkeys: The maximum number of keys a query should return.
*/
char *db_backend;
char *backends_dir;
+
+ struct dbfuncs *dbbackend;
};
/*
int count = 0;
if (ishex) {
- count = fetch_key(keyid, &publickey, false);
+ count = config.dbbackend->fetch_key(keyid, &publickey, false);
} else {
- count = fetch_key_text(search, &publickey);
+ count = config.dbbackend->fetch_key_text(search, &publickey);
}
if (publickey != NULL) {
key_index(publickey, verbose, fingerprint, false);
puts("\tCommands:\n");
puts("\tadd - read armored OpenPGP keys from stdin and add to the"
" keyserver");
- puts("\tclean - read armored OpenPGP keys from stdin, run the "
+ puts("\tclean - read armored OpenPGP keys from stdin, run the"
" cleaning\n\t routines against them and dump to"
" stdout");
puts("\tdelete - delete a given key from the keyserver");
if ((argc - optind) < 1) {
usage();
} else if (!strcmp("dump", argv[optind])) {
- initdb(true);
+ config.dbbackend->initdb(true);
dumpstate.count = dumpstate.filenum = 0;
dumpstate.maxcount = 1000000;
dumpstate.fd = -1;
dumpstate.filebase = "keydump.%d.pgp";
- iterate_keys(dump_func, &dumpstate);
+ config.dbbackend->iterate_keys(dump_func, &dumpstate);
if (dumpstate.fd != -1) {
close(dumpstate.fd);
dumpstate.fd = -1;
}
- cleanupdb();
+ config.dbbackend->cleanupdb();
} else if (!strcmp("add", argv[optind])) {
if (binary) {
result = read_openpgp_stream(stdin_getchar, NULL,
logthing(LOGTHING_INFO, "%d keys cleaned.",
result);
- initdb(false);
+ config.dbbackend->initdb(false);
logthing(LOGTHING_NOTICE, "Got %d new keys.",
- update_keys(&keys, false));
+ config.dbbackend->update_keys(&keys,
+ false));
if (keys != NULL && update) {
flatten_publickey(keys,
&packets,
free_packet_list(packets);
packets = NULL;
}
- cleanupdb();
+ config.dbbackend->cleanupdb();
} else {
rc = 1;
logthing(LOGTHING_NOTICE, "No keys read.");
ishex = true;
}
}
- initdb(false);
+ config.dbbackend->initdb(false);
if (!strcmp("index", argv[optind])) {
find_keys(search, keyid, ishex, fingerprint,
false, false);
if (!ishex) {
puts("Can't get a key on uid text."
" You must supply a keyid.");
- } else if (fetch_key(keyid, &keys, false)) {
+ } else if (config.dbbackend->fetch_key(keyid, &keys,
+ false)) {
unsigned char *photo = NULL;
size_t length = 0;
puts("Key not found");
}
} else if (!strcmp("delete", argv[optind])) {
- delete_key(getfullkeyid(keyid), false);
+ config.dbbackend->delete_key(
+ config.dbbackend->getfullkeyid(keyid),
+ false);
} else if (!strcmp("get", argv[optind])) {
if (!ishex) {
puts("Can't get a key on uid text."
" You must supply a keyid.");
- } else if (fetch_key(keyid, &keys, false)) {
+ } else if (config.dbbackend->fetch_key(keyid, &keys,
+ false)) {
logthing(LOGTHING_INFO, "Got key.");
flatten_publickey(keys,
&packets,
puts("Key not found");
}
}
- cleanupdb();
+ config.dbbackend->cleanupdb();
} else {
usage();
}
while (curll != NULL && curdegree <= maxdegree) {
if (sigs) {
- sigll = cached_getkeysigs(((struct stats_key *)
+ sigll = config.dbbackend->cached_getkeysigs(
+ ((struct stats_key *)
curll->object)->keyid);
} else {
sigll = NULL;
long degree;
char *uid;
- cached_getkeysigs(keyid);
+ config.dbbackend->cached_getkeysigs(keyid);
if ((keyinfo = findinhash(keyid)) == NULL) {
printf("Couldn't find key 0x%llX.\n", keyid);
return;
}
- uid = keyid2uid(keyinfo->keyid);
+ uid = config.dbbackend->keyid2uid(keyinfo->keyid);
printf("Six degrees for 0x%llX (%s):\n", keyinfo->keyid, uid);
free(uid);
uid = NULL;
readconfig(NULL);
initlogthing("sixdegrees", config.logfile);
- initdb(true);
+ config.dbbackend->initdb(true);
inithash();
- sixdegrees(getfullkeyid(keyid));
+ sixdegrees(config.dbbackend->getfullkeyid(keyid));
destroyhash();
- cleanupdb();
+ config.dbbackend->cleanupdb();
cleanuplogthing();
cleanupconfig();
#include "hash.h"
#include "keydb.h"
#include "ll.h"
+#include "onak-conf.h"
#include "stats.h"
/**
oldkeys = keys;
while ((!cleanup()) && keys != NULL && have->colour == 0) {
- sigs = cached_getkeysigs(((struct stats_key *)
+ sigs = config.dbbackend->cached_getkeysigs(((struct stats_key *)
keys->object)->keyid);
while ((!cleanup()) && sigs != NULL && have->colour == 0) {
/*
int pathnum;
char *uid;
- fullhave = getfullkeyid(have);
- fullwant = getfullkeyid(want);
+ fullhave = config.dbbackend->getfullkeyid(have);
+ fullwant = config.dbbackend->getfullkeyid(want);
/*
* Make sure the keys we have and want are in the cache.
*/
- (void) cached_getkeysigs(fullhave);
- (void) cached_getkeysigs(fullwant);
+ (void) config.dbbackend->cached_getkeysigs(fullhave);
+ (void) config.dbbackend->cached_getkeysigs(fullwant);
if ((keyinfoa = findinhash(fullhave)) == NULL) {
printf("Couldn't find key 0x%llX.\n", have);
html ? "<BR>" : "");
curkey = keyinfoa;
while (curkey != NULL && curkey->keyid != 0) {
- uid = keyid2uid(curkey->keyid);
+ uid = config.dbbackend->keyid2uid(
+ curkey->keyid);
if (html && uid == NULL) {
printf("<a href=\"lookup?op=get&search="
"0x%08llX\">0x%08llX</a> (["
curll = lladd(NULL, have);
while (curll != NULL) {
- sigs = cached_getkeysigs(((struct stats_key *)
+ sigs = config.dbbackend->cached_getkeysigs(((struct stats_key *)
curll->object)->keyid);
while (sigs != NULL) {
if (((struct stats_key *) sigs->object)->colour == 0) {