fclose(stderr);
}
catchsignals();
- dbctx = config.dbinit(false);
+ dbctx = config.dbinit(config.backend, false);
count = cleankeys(keys);
logthing(LOGTHING_INFO, "%d keys cleaned.",
readconfig(NULL);
initlogthing("gpgwww", config.logfile);
catchsignals();
- dbctx = config.dbinit(true);
+ dbctx = config.dbinit(config.backend, true);
inithash();
logthing(LOGTHING_NOTICE, "Looking for path from 0x%016" PRIX64
" to 0x%016"
}
catchsignals();
- dbctx = config.dbinit(false);
+ dbctx = config.dbinit(config.backend, false);
if (dbctx->fetch_key_skshash == NULL) {
dbctx->cleanupdb(dbctx);
maxfd = fd;
memset(clients, -1, sizeof (clients));
- dbctx = config.dbinit(false);
+ dbctx = config.dbinit(config.backend, false);
logthing(LOGTHING_NOTICE, "Accepting connections.");
while (!cleanup() && select(maxfd + 1, &rfds, NULL, NULL, NULL) != -1) {
void (*iterfunc)(void *ctx,
struct openpgp_publickey *key), void *ctx);
+/**
+ * @brief Configuration file information for this backend instance
+ */
+ struct onak_db_config *config;
+
/**
* @brief Private backend context information.
*/
* we're running with a newer version of db4 than the database was
* created with.
*/
-static int db4_upgradedb(struct onak_db4_dbctx *privctx)
+static int db4_upgradedb(struct onak_dbctx *dbctx)
{
+ struct onak_db4_dbctx *privctx = (struct onak_db4_dbctx *) dbctx->priv;
DB *curdb = NULL;
int ret;
int i;
struct stat statbuf;
ssize_t written;
- snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
+ snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location,
DB4_UPGRADE_FILE);
lockfile_fd = open(buf, O_RDWR | O_CREAT | O_EXCL, 0600);
if (lockfile_fd < 0) {
if (written != strlen(buf)) {
logthing(LOGTHING_CRITICAL, "Couldn't write PID to lockfile: "
"%s", strerror(errno));
- snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
+ snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location,
DB4_UPGRADE_FILE);
unlink(buf);
return -1;
ret = db_env_create(&privctx->dbenv, 0);
if (ret == 0) {
privctx->dbenv->set_errcall(privctx->dbenv, &db4_errfunc);
- privctx->dbenv->remove(privctx->dbenv, config.db_dir, 0);
+ privctx->dbenv->remove(privctx->dbenv, dbctx->config->location, 0);
privctx->dbenv = NULL;
}
for (i = 0; i < privctx->numdbs; i++) {
ret = db_create(&curdb, NULL, 0);
if (ret == 0) {
snprintf(buf, sizeof(buf) - 1, "%s/keydb.%d.db",
- config.db_dir, i);
+ dbctx->config->location, i);
logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
curdb->upgrade(curdb, buf, 0);
curdb->close(curdb, 0);
ret = db_create(&curdb, NULL, 0);
if (ret == 0) {
- snprintf(buf, sizeof(buf) - 1, "%s/worddb", config.db_dir);
+ snprintf(buf, sizeof(buf) - 1, "%s/worddb", dbctx->config->location);
logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
curdb->upgrade(curdb, buf, 0);
curdb->close(curdb, 0);
ret = db_create(&curdb, NULL, 0);
if (ret == 0) {
- snprintf(buf, sizeof(buf) - 1, "%s/id32db", config.db_dir);
+ snprintf(buf, sizeof(buf) - 1, "%s/id32db", dbctx->config->location);
logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
curdb->upgrade(curdb, buf, 0);
curdb->close(curdb, 0);
ret = db_create(&curdb, NULL, 0);
if (ret == 0) {
- snprintf(buf, sizeof(buf) - 1, "%s/id64db", config.db_dir);
+ snprintf(buf, sizeof(buf) - 1, "%s/id64db", dbctx->config->location);
logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
curdb->upgrade(curdb, buf, 0);
curdb->close(curdb, 0);
ret = db_create(&curdb, NULL, 0);
if (ret == 0) {
- snprintf(buf, sizeof(buf) - 1, "%s/skshashdb", config.db_dir);
+ snprintf(buf, sizeof(buf) - 1, "%s/skshashdb", dbctx->config->location);
logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
curdb->upgrade(curdb, buf, 0);
curdb->close(curdb, 0);
ret = db_create(&curdb, NULL, 0);
if (ret == 0) {
- snprintf(buf, sizeof(buf) - 1, "%s/subkeydb", config.db_dir);
+ snprintf(buf, sizeof(buf) - 1, "%s/subkeydb", dbctx->config->location);
logthing(LOGTHING_DEBUG, "Upgrading %s", buf);
curdb->upgrade(curdb, buf, 0);
curdb->close(curdb, 0);
db_strerror(ret));
}
- snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
+ snprintf(buf, sizeof(buf) - 1, "%s/%s", dbctx->config->location,
DB4_UPGRADE_FILE);
unlink(buf);
* this file are called in order to allow the DB to be initialized ready
* for access.
*/
-struct onak_dbctx *keydb_db4_init(bool readonly)
+struct onak_dbctx *keydb_db4_init(struct onak_db_config *dbcfg, bool readonly)
{
char buf[1024];
FILE *numdb = NULL;
if (dbctx == NULL) {
return NULL;
}
+ dbctx->config = dbcfg;
dbctx->priv = privctx = calloc(1, sizeof(*privctx));
if (privctx == NULL) {
free(dbctx);
/* Default to 16 key data DBs */
privctx->numdbs = 16;
- snprintf(buf, sizeof(buf) - 1, "%s/%s", config.db_dir,
+ snprintf(buf, sizeof(buf) - 1, "%s/%s", dbcfg->location,
DB4_UPGRADE_FILE);
ret = stat(buf, &statbuf);
while ((ret == 0) || (errno != ENOENT)) {
}
ret = 0;
- snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", config.db_dir);
+ snprintf(buf, sizeof(buf) - 1, "%s/num_keydb", dbcfg->location);
numdb = fopen(buf, "r");
if (numdb != NULL) {
if (fgets(buf, sizeof(buf), numdb) != NULL) {
}
if (ret == 0) {
- ret = privctx->dbenv->open(privctx->dbenv, config.db_dir,
+ ret = privctx->dbenv->open(privctx->dbenv, dbcfg->location,
DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK |
DB_INIT_TXN |
DB_CREATE,
if (ret == DB_VERSION_MISMATCH) {
privctx->dbenv->close(privctx->dbenv, 0);
privctx->dbenv = NULL;
- ret = db4_upgradedb(privctx);
+ ret = db4_upgradedb(dbctx);
if (ret == 0) {
ret = db_env_create(&privctx->dbenv, 0);
}
privctx->dbenv->set_lk_detect(privctx->dbenv,
DB_LOCK_DEFAULT);
ret = privctx->dbenv->open(privctx->dbenv,
- config.db_dir,
+ dbcfg->location,
DB_INIT_LOG | DB_INIT_MPOOL |
DB_INIT_LOCK | DB_INIT_TXN |
DB_CREATE | DB_RECOVER,
if (ret != 0) {
logthing(LOGTHING_CRITICAL,
"Error opening db environment: %s (%s)",
- config.db_dir,
+ dbcfg->location,
db_strerror(ret));
if (privctx->dbenv != NULL) {
privctx->dbenv->close(privctx->dbenv, 0);
}
}
-struct onak_dbctx *keydb_dynamic_init(bool readonly)
+struct onak_dbctx *keydb_dynamic_init(struct onak_db_config *dbcfg,
+ bool readonly)
{
struct onak_dbctx *dbctx;
char *soname;
char *initname;
- struct onak_dbctx *(*backend_init)(bool);
+ struct onak_dbctx *(*backend_init)(struct onak_db_config *, bool);
struct onak_dynamic_dbctx *privctx;
dbctx = malloc(sizeof(struct onak_dbctx));
return NULL;
}
+ dbctx->config = dbcfg;
dbctx->priv = privctx = malloc(sizeof(struct onak_dynamic_dbctx));
if (dbctx->priv == NULL) {
free(dbctx);
}
if (config.backends_dir == NULL) {
- soname = malloc(strlen(config.db_backend)
+ soname = malloc(strlen(dbcfg->type)
+ strlen("./libkeydb_")
+ strlen(".so")
+ 1);
- sprintf(soname, "./libkeydb_%s.so", config.db_backend);
+ sprintf(soname, "./libkeydb_%s.so", dbcfg->type);
} else {
- soname = malloc(strlen(config.db_backend)
+ soname = malloc(strlen(dbcfg->type)
+ strlen("/libkeydb_")
+ strlen(".so")
+ strlen(config.backends_dir)
+ 1);
sprintf(soname, "%s/libkeydb_%s.so", config.backends_dir,
- config.db_backend);
+ dbcfg->type);
}
logthing(LOGTHING_INFO, "Loading dynamic backend: %s", soname);
+ strlen("keydb_")
+ strlen("_init")
+ 1);
- sprintf(initname, "keydb_%s_init", config.db_backend);
+ sprintf(initname, "keydb_%s_init", dbcfg->type);
*(void **) (&backend_init) = dlsym(privctx->backend_handle, initname);
free(initname);
free(soname);
soname = NULL;
- privctx->loadeddbctx = backend_init(readonly);
+ privctx->loadeddbctx = backend_init(dbcfg, readonly);
if (privctx->loadeddbctx != NULL) {
dbctx->cleanupdb = dynamic_cleanupdb;
*
* This is just a no-op for flat file access.
*/
-struct onak_dbctx *keydb_file_init(bool readonly)
+struct onak_dbctx *keydb_file_init(struct onak_db_config *dbcfg, bool readonly)
{
struct onak_dbctx *dbctx;
return NULL;
}
- dbctx->priv = strdup(config.db_dir);
+ dbctx->config = dbcfg;
+ dbctx->priv = strdup(dbcfg->location);
dbctx->cleanupdb = file_cleanupdb;
dbctx->starttrans = file_starttrans;
}
-static void keypath(char *buffer, size_t length, uint64_t _keyid)
+static void keypath(char *buffer, size_t length, uint64_t _keyid,
+ char *basepath)
{
uint64_t keyid = _keyid << 32;
snprintf(buffer, length, "%s/key/%02X/%02X/%08X/%016" PRIX64,
- config.db_dir, (uint8_t) ((keyid >> 56) & 0xFF),
+ basepath, (uint8_t) ((keyid >> 56) & 0xFF),
(uint8_t) ((keyid >> 48) & 0xFF),
(uint32_t) (keyid >> 32), _keyid);
}
-static void keydir(char *buffer, size_t length, uint64_t _keyid)
+static void keydir(char *buffer, size_t length, uint64_t _keyid,
+ char *basepath)
{
uint64_t keyid = _keyid << 32;
- snprintf(buffer, length, "%s/key/%02X/%02X/%08X", config.db_dir,
+ snprintf(buffer, length, "%s/key/%02X/%02X/%08X", basepath,
(uint8_t) ((keyid >> 56) & 0xFF),
(uint8_t) ((keyid >> 48) & 0xFF),
(uint32_t) (keyid >> 32));
}
-static void prove_path_to(uint64_t keyid, char *what)
+static void prove_path_to(uint64_t keyid, char *what, char *basepath)
{
static char buffer[PATH_MAX];
- snprintf(buffer, sizeof(buffer), "%s/%s", config.db_dir, what);
+ snprintf(buffer, sizeof(buffer), "%s/%s", basepath, what);
mkdir(buffer, 0777);
- snprintf(buffer, sizeof(buffer), "%s/%s/%02X", config.db_dir, what,
+ snprintf(buffer, sizeof(buffer), "%s/%s/%02X", basepath, what,
(uint8_t) ((keyid >> 24) & 0xFF));
mkdir(buffer, 0777);
- snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X", config.db_dir,
+ snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X", basepath,
what,
(uint8_t) ((keyid >> 24) & 0xFF),
(uint8_t) ((keyid >> 16) & 0xFF));
mkdir(buffer, 0777);
- snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X/%08X", config.db_dir,
+ snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X/%08X", basepath,
what,
(uint8_t) ((keyid >> 24) & 0xFF),
(uint8_t) ((keyid >> 16) & 0xFF), (uint32_t) (keyid));
}
static void wordpath(char *buffer, size_t length, char *word, uint32_t hash,
- uint64_t keyid)
+ uint64_t keyid, char *basepath)
{
snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s/%016" PRIX64,
- config.db_dir, (uint8_t) ((hash >> 24) & 0xFF),
+ basepath, (uint8_t) ((hash >> 24) & 0xFF),
(uint8_t) ((hash >> 16) & 0xFF), hash, word, keyid);
}
-static void worddir(char *buffer, size_t length, char *word, uint32_t hash)
+static void worddir(char *buffer, size_t length, char *word, uint32_t hash,
+ char *basepath)
{
- snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s", config.db_dir,
+ snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s", basepath,
(uint8_t) ((hash >> 24) & 0xFF),
(uint8_t) ((hash >> 16) & 0xFF), hash, word);
}
-static void subkeypath(char *buffer, size_t length, uint64_t subkey)
+static void subkeypath(char *buffer, size_t length, uint64_t subkey,
+ char *basepath)
{
snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X/%016" PRIX64,
- config.db_dir,
+ basepath,
(uint8_t) ((subkey >> 24) & 0xFF),
(uint8_t) ((subkey >> 16) & 0xFF),
(uint32_t) (subkey & 0xFFFFFFFF),
subkey);
}
-static void subkeydir(char *buffer, size_t length, uint64_t subkey)
+static void subkeydir(char *buffer, size_t length, uint64_t subkey,
+ char *basepath)
{
snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X",
- config.db_dir,
+ basepath,
(uint8_t) ((subkey >> 24) & 0xFF),
(uint8_t) ((subkey >> 16) & 0xFF),
(uint32_t) (subkey & 0xFFFFFFFF));
}
static void skshashpath(char *buffer, size_t length,
- const struct skshash *hash)
+ const struct skshash *hash, char *basepath)
{
snprintf(buffer, length, "%s/skshash/%02X/%02X/%02X%02X%02X%02X/"
"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
- config.db_dir,
+ basepath,
hash->hash[0], hash->hash[1],
hash->hash[0], hash->hash[1], hash->hash[2], hash->hash[3],
hash->hash[4], hash->hash[5], hash->hash[6], hash->hash[7],
struct dirent *de = NULL;
uint64_t ret = 0;
- keydir(buffer, sizeof(buffer), keyid);
+ keydir(buffer, sizeof(buffer), keyid, dbctx->config->location);
d = opendir(buffer);
if (d) {
}
if (ret == 0) {
- subkeydir(buffer, sizeof(buffer), keyid);
+ subkeydir(buffer, sizeof(buffer), keyid,
+ dbctx->config->location);
d = opendir(buffer);
if (d) {
if ((keyid >> 32) == 0)
keyid = fs_getfullkeyid(dbctx, keyid);
- keypath(buffer, sizeof(buffer), keyid);
+ keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
fd = open(buffer, O_RDONLY);
if (fd == -1 && errno == ENOENT) {
- subkeypath(buffer, sizeof(buffer), keyid);
+ subkeypath(buffer, sizeof(buffer), keyid,
+ dbctx->config->location);
fd = open(buffer, O_RDONLY);
}
if (!intrans)
fs_starttrans(dbctx);
- prove_path_to(keyid, "key");
- keypath(buffer, sizeof(buffer), keyid);
+ prove_path_to(keyid, "key", dbctx->config->location);
+ keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
if ((fd =
open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
wl = wordlist = makewordlistfromkey(wordlist, publickey);
while (wl) {
uint32_t hash = calchash((uint8_t *) (wl->object));
- prove_path_to(hash, "words");
+ prove_path_to(hash, "words", dbctx->config->location);
- worddir(wbuffer, sizeof(wbuffer), wl->object, hash);
+ worddir(wbuffer, sizeof(wbuffer), wl->object, hash,
+ dbctx->config->location);
mkdir(wbuffer, 0777);
wordpath(wbuffer, sizeof(wbuffer), wl->object, hash,
- keyid);
+ keyid, dbctx->config->location);
link(buffer, wbuffer);
wl = wl->next;
while (subkeyids != NULL && subkeyids[i].length != 0) {
keyid = fingerprint2keyid(&subkeyids[i]);
- prove_path_to(keyid, "subkeys");
+ prove_path_to(keyid, "subkeys",
+ dbctx->config->location);
- subkeydir(wbuffer, sizeof(wbuffer), keyid);
+ subkeydir(wbuffer, sizeof(wbuffer), keyid,
+ dbctx->config->location);
mkdir(wbuffer, 0777);
- subkeypath(wbuffer, sizeof(wbuffer), keyid);
+ subkeypath(wbuffer, sizeof(wbuffer), keyid,
+ dbctx->config->location);
link(buffer, wbuffer);
i++;
get_skshash(publickey, &hash);
hashid = (hash.hash[0] << 24) + (hash.hash[1] << 16) +
(hash.hash[2] << 8) + hash.hash[3];
- prove_path_to(hashid, "skshash");
- skshashpath(wbuffer, sizeof(wbuffer), &hash);
+ prove_path_to(hashid, "skshash", dbctx->config->location);
+ skshashpath(wbuffer, sizeof(wbuffer), &hash,
+ dbctx->config->location);
link(buffer, wbuffer);
}
"Wordlist for key %016" PRIX64 " done", keyid);
while (wl) {
uint32_t hash = calchash((uint8_t *) (wl->object));
- prove_path_to(hash, "words");
+ prove_path_to(hash, "words", dbctx->config->location);
wordpath(buffer, sizeof(buffer), wl->object, hash,
- keyid);
+ keyid, dbctx->config->location);
unlink(buffer);
wl = wl->next;
i = 0;
while (subkeyids != NULL && subkeyids[i].length != 0) {
subkeyid = fingerprint2keyid(&subkeyids[i]);
- prove_path_to(subkeyid, "subkeys");
+ prove_path_to(subkeyid, "subkeys",
+ dbctx->config->location);
- subkeypath(buffer, sizeof(buffer), subkeyid);
+ subkeypath(buffer, sizeof(buffer), subkeyid,
+ dbctx->config->location);
unlink(buffer);
i++;
}
get_skshash(pk, &hash);
- skshashpath(buffer, sizeof(buffer), &hash);
+ skshashpath(buffer, sizeof(buffer), &hash,
+ dbctx->config->location);
unlink(buffer);
}
- keypath(buffer, sizeof(buffer), keyid);
+ keypath(buffer, sizeof(buffer), keyid, dbctx->config->location);
unlink(buffer);
if (!intrans)
return 1;
}
-static struct ll *internal_get_key_by_word(char *word, struct ll *mct)
+static struct ll *internal_get_key_by_word(char *word, struct ll *mct,
+ char *basepath)
{
struct ll *keys = NULL;
DIR *d = NULL;
uint32_t hash = calchash((uint8_t *) (word));
struct dirent *de;
- worddir(buffer, sizeof(buffer), word, hash);
+ worddir(buffer, sizeof(buffer), word, hash, basepath);
d = opendir(buffer);
logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
buffer);
searchtext = strdup(search);
wl = wordlist = makewordlist(wordlist, searchtext);
- keylist = internal_get_key_by_word(wordlist->object, NULL);
+ keylist = internal_get_key_by_word(wordlist->object, NULL,
+ dbctx->config->location);
if (!keylist) {
llfree(wordlist, NULL);
wl = wl->next;
while (wl) {
struct ll *nkl =
- internal_get_key_by_word(wl->object, keylist);
+ internal_get_key_by_word(wl->object, keylist,
+ dbctx->config->location);
if (!nkl) {
llfree(wordlist, NULL);
llfree(keylist, free);
int ret = 0, fd;
struct openpgp_packet_list *packets = NULL;
- skshashpath(buffer, sizeof(buffer), hash);
+ skshashpath(buffer, sizeof(buffer), hash, dbctx->config->location);
if ((fd = open(buffer, O_RDONLY)) != -1) {
read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
parse_keys(packets, publickey);
/**
* initdb - Initialize the key database.
*/
-struct onak_dbctx *keydb_fs_init(bool readonly)
+struct onak_dbctx *keydb_fs_init(struct onak_db_config *dbcfg, bool readonly)
{
char buffer[PATH_MAX];
struct onak_dbctx *dbctx;
if (dbctx == NULL) {
return NULL;
}
+ dbctx->config = dbcfg;
dbctx->priv = privctx = malloc(sizeof(*privctx));
if (privctx == NULL) {
free(dbctx);
privctx->lockfile_readonly = readonly;
- snprintf(buffer, sizeof(buffer), "%s/.lock", config.db_dir);
+ snprintf(buffer, sizeof(buffer), "%s/.lock", dbcfg->location);
- if (access(config.db_dir, R_OK | W_OK | X_OK) == -1) {
+ if (access(dbcfg->location, R_OK | W_OK | X_OK) == -1) {
if (errno != ENOENT) {
logthing(LOGTHING_CRITICAL,
"Unable to access keydb_fs root of '%s'. (%s)",
- config.db_dir, strerror(errno));
+ dbcfg->location, strerror(errno));
exit(1); /* Lacking rwx on the key dir */
}
- mkdir(config.db_dir, 0777);
+ mkdir(dbcfg->location, 0777);
privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
}
- if (chdir(config.db_dir) == -1) {
+ if (chdir(dbcfg->location) == -1) {
/* Shouldn't happen after the above */
logthing(LOGTHING_CRITICAL,
"Couldn't change to database directory: %s",
#include "version.h"
struct onak_hkp_dbctx {
+ struct onak_db_config *config; /* Our DB config info */
CURL *curl;
char hkpbase[1024];
};
*
* We initialize CURL here.
*/
-struct onak_dbctx *keydb_hkp_init(bool readonly)
+struct onak_dbctx *keydb_hkp_init(struct onak_db_config *dbcfg, bool readonly)
{
struct onak_dbctx *dbctx;
struct onak_hkp_dbctx *privctx;
return NULL;
}
+ dbctx->config = dbcfg;
dbctx->priv = privctx = malloc(sizeof(*privctx));
dbctx->cleanupdb = hkp_cleanupdb;
dbctx->starttrans = hkp_starttrans;
dbctx->getfullkeyid = generic_getfullkeyid;
dbctx->iterate_keys = hkp_iterate_keys;
- if (!hkp_parse_url(privctx, config.db_dir)) {
+ if (!hkp_parse_url(privctx, dbcfg->location)) {
exit(EXIT_FAILURE);
}
curl_global_init(CURL_GLOBAL_DEFAULT);
* this file are called in order to allow the DB to be initialized ready
* for access.
*/
-struct onak_dbctx *keydb_keyd_init(bool readonly)
+struct onak_dbctx *keydb_keyd_init(struct onak_db_config *dbcfg, bool readonly)
{
struct sockaddr_un sock;
uint32_t cmd = KEYD_CMD_UNKNOWN;
if (dbctx == NULL) {
return NULL;
}
+ dbctx->config = dbcfg;
keyd_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (keyd_fd < 0) {
* this file are called in order to allow the DB to be initialized ready
* for access.
*/
-struct onak_dbctx *keydb_pg_init(bool readonly)
+struct onak_dbctx *keydb_pg_init(struct onak_db_config *dbcfg, bool readonly)
{
struct onak_dbctx *dbctx;
PGconn *dbconn;
if (dbctx == NULL) {
return NULL;
}
+ dbctx->config = dbcfg;
dbconn = PQsetdbLogin(config.pg_dbhost, // host
NULL, // port
readconfig(NULL);
initlogthing("lookup", config.logfile);
catchsignals();
- dbctx = config.dbinit(false);
+ dbctx = config.dbinit(config.backend, false);
switch (op) {
case OP_GET:
case OP_HGET:
readconfig(configfile);
free(configfile);
initlogthing("maxpath", config.logfile);
- dbctx = config.dbinit(true);
+ dbctx = config.dbinit(config.backend, true);
if (dbctx != NULL) {
inithash();
findmaxpath(dbctx, 30);
#include "log.h"
#include "onak-conf.h"
-extern struct onak_dbctx *DBINIT(bool readonly);
+extern struct onak_dbctx *DBINIT(struct onak_db_config *dbcfg, bool readonly);
/*
* config - Runtime configuration for onak.
.use_keyd = false,
.sock_dir = ".",
- /*
- * Options for directory backends.
- */
- .db_dir = NULL,
-
- /*
- * Options for the Postgres backend.
- */
- .pg_dbhost = NULL,
- .pg_dbname = NULL,
- .pg_dbuser = NULL,
- .pg_dbpass = NULL,
-
- /*
- * Options for dynamic backends.
- */
- .db_backend = NULL,
+ .backends = NULL,
.backends_dir = NULL,
.dbinit = DBINIT,
.check_sighash = true,
+
+ .bin_dir = NULL,
+ .mail_dir = NULL,
};
bool parsebool(char *str, bool fallback)
int i;
char *dir, *conf;
size_t len;
+ struct onak_db_config *backend;
curline[1023] = 0;
if (configfile == NULL) {
return;
}
+ /* Add a single DB configuration */
+ backend = calloc(1, sizeof(*backend));
+ config.backend = backend;
+ config.backends = lladd(NULL, backend);
+
while (!feof(conffile)) {
for (i = strlen(curline) - 1;
i >= 0 && isspace(curline[i]);
* Comment line, ignore.
*/
} else if (!strncmp("db_dir ", curline, 7)) {
- config.db_dir = strdup(&curline[7]);
+ backend->location = strdup(&curline[7]);
} else if (!strncmp("debug ", curline, 6)) {
/*
* Not supported yet; ignore for compatibility with
} else if (!strncmp("max_reply_keys ", curline, 15)) {
config.maxkeys = atoi(&curline[15]);
} else if (!strncmp("pg_dbhost ", curline, 10)) {
- config.pg_dbhost = strdup(&curline[10]);
+ backend->hostname = strdup(&curline[10]);
} else if (!strncmp("pg_dbname ", curline, 10)) {
- config.pg_dbname = strdup(&curline[10]);
+ backend->location = strdup(&curline[10]);
} else if (!strncmp("pg_dbuser ", curline, 10)) {
- config.pg_dbuser = strdup(&curline[10]);
+ backend->username = strdup(&curline[10]);
} else if (!strncmp("pg_dbpass ", curline, 10)) {
- config.pg_dbpass = strdup(&curline[10]);
+ backend->password = strdup(&curline[10]);
} else if (!strncmp("syncsite ", curline, 9)) {
config.syncsites =
lladd(config.syncsites, strdup(&curline[9]));
* Not applicable; ignored for compatibility with pksd.
*/
} else if (!strncmp("db_backend ", curline, 11)) {
+ backend->type = strdup(&curline[11]);
+ backend->name = strdup(&curline[11]);
config.db_backend = strdup(&curline[11]);
} else if (!strncmp("backends_dir ", curline, 13)) {
config.backends_dir = strdup(&curline[13]);
}
}
+void cleanupdbconfig(void *object)
+{
+ struct onak_db_config *dbconfig = (struct onak_db_config *) object;
+
+ if (dbconfig->name != NULL) {
+ free(dbconfig->name);
+ dbconfig->name = NULL;
+ }
+ if (dbconfig->type != NULL) {
+ free(dbconfig->type);
+ dbconfig->type = NULL;
+ }
+ if (dbconfig->location != NULL) {
+ free(dbconfig->location);
+ dbconfig->location = NULL;
+ }
+ if (dbconfig->hostname != NULL) {
+ free(dbconfig->hostname);
+ dbconfig->hostname = NULL;
+ }
+ if (dbconfig->username != NULL) {
+ free(dbconfig->username);
+ dbconfig->username = NULL;
+ }
+ if (dbconfig->password != NULL) {
+ free(dbconfig->password);
+ dbconfig->password = NULL;
+ }
+}
+
void cleanupconfig(void) {
+ /* Free any defined DB backend configuration first */
+ llfree(config.backends, cleanupdbconfig);
+ config.backends = NULL;
+
if (config.thissite != NULL) {
free(config.thissite);
config.thissite = NULL;
free(config.mta);
config.mta = NULL;
}
- if (config.db_dir != NULL) {
- free(config.db_dir);
- config.db_dir = NULL;
- }
- if (config.pg_dbhost != NULL) {
- free(config.pg_dbhost);
- config.pg_dbhost = NULL;
- }
- if (config.pg_dbname != NULL) {
- free(config.pg_dbname);
- config.pg_dbname = NULL;
- }
- if (config.pg_dbuser != NULL) {
- free(config.pg_dbuser);
- config.pg_dbuser = NULL;
- }
- if (config.pg_dbpass != NULL) {
- free(config.pg_dbpass);
- config.pg_dbpass = NULL;
- }
if (config.syncsites != NULL) {
llfree(config.syncsites, free);
config.syncsites = NULL;
#include "keydb.h"
+/**
+ * @brief Backend database configuration.
+ *
+ */
+struct onak_db_config {
+ /** Name, as used to refer to individual backend instances */
+ char *name;
+ /** Backend type [e.g. db4, pg, fs, file] */
+ char *type;
+ /** Location information; directory for file backed, DB name for DBs */
+ char *location;
+ /** Database backend hostname, if appropriate */
+ char *hostname;
+ /** Database backend username, if appropriate */
+ char *username;
+ /** Database backend password, if appropriate */
+ char *password;
+};
+
/**
* @brief Runtime configuration for onak.
*
/** The path to the directory the keyd socket lives in. */
char *sock_dir;
- /*
- * Options for any database backend that needs a directory, be it the
- * file, fs or db4 options.
- */
- /** The path to the directory containing the database files. */
- char *db_dir;
-
- /*
- * Options for the Postgres backend.
- */
- /** The host that Postgres is running on. */
- char *pg_dbhost;
- /** The database name. */
- char *pg_dbname;
- /** The user we should connect as. */
- char *pg_dbuser;
- /** The password for the user. */
- char *pg_dbpass;
+ /** List of backend configurations */
+ struct ll *backends;
+
+ /* The default backend to use */
+ struct onak_db_config *backend;
/*
* Options for the dynamic backend.
char *backends_dir;
/** Pointer to the initialisation function for our loaded DB backend */
- struct onak_dbctx *(*dbinit)(bool);
+ struct onak_dbctx *(*dbinit)(struct onak_db_config *, bool);
/** Should we verify signature hashes match? */
bool check_sighash;
if ((argc - optind) < 1) {
usage();
} else if (!strcmp("dump", argv[optind])) {
- dbctx = config.dbinit(true);
+ dbctx = config.dbinit(config.backend, true);
dumpstate.count = dumpstate.filenum = 0;
dumpstate.maxcount = 100000;
dumpstate.fd = -1;
logthing(LOGTHING_INFO, "%d keys cleaned.",
result);
- dbctx = config.dbinit(false);
+ dbctx = config.dbinit(config.backend, false);
logthing(LOGTHING_NOTICE, "Got %d new keys.",
dbctx->update_keys(dbctx, &keys,
false));
ishex = true;
}
}
- dbctx = config.dbinit(false);
+ dbctx = config.dbinit(config.backend, false);
if (!strcmp("index", argv[optind])) {
find_keys(dbctx, search, keyid, &fingerprint, ishex,
isfp, dispfp, skshash,
readconfig(configfile);
free(configfile);
initlogthing("sixdegrees", config.logfile);
- dbctx = config.dbinit(true);
+ dbctx = config.dbinit(config.backend, true);
if (dbctx != NULL) {
inithash();
sixdegrees(dbctx, dbctx->getfullkeyid(dbctx, keyid));
readconfig(configfile);
initlogthing("wotsap", config.logfile);
- dbctx = config.dbinit(true);
+ dbctx = config.dbinit(config.backend, true);
if (dbctx != NULL) {
inithash();
wotsap(dbctx, dbctx->getfullkeyid(dbctx, keyid),