From 7f88fa1553a361d5d70a87eeeb7a37e1f3476cc8 Mon Sep 17 00:00:00 2001
From: Jonathan McDowell <noodles@earth.li>
Date: Wed, 16 Mar 2011 03:40:44 +0000
Subject: [PATCH] Add use_keyd config file option to select keyd as the backend

  We want to be able to use a common config file across the various
  tools, so add a config variable to indicate that we're using keyd.
  This allows keyd to override the option and then get the configured
  backend, while everything else knows to use keyd.
---
 keyd.c          |  1 +
 keydb_dynamic.c |  5 +++++
 onak-conf.c     | 23 +++++++++++++++++++++++
 onak-conf.h     |  5 +++++
 onak.conf.in    |  3 +++
 5 files changed, 37 insertions(+)

diff --git a/keyd.c b/keyd.c
index 18d09bf..c802264 100644
--- a/keyd.c
+++ b/keyd.c
@@ -388,6 +388,7 @@ int main(int argc, char *argv[])
 	free(configfile);
 	configfile = NULL;
 	initlogthing("keyd", config.logfile);
+	config.use_keyd = false;
 
 	if (!foreground) {
 		daemonize();
diff --git a/keydb_dynamic.c b/keydb_dynamic.c
index 503f1bf..e22b00d 100644
--- a/keydb_dynamic.c
+++ b/keydb_dynamic.c
@@ -45,6 +45,11 @@ static bool load_backend(void)
 		loaded_backend = NULL;
 	}
 
+	if (config.use_keyd) {
+		free(config.db_backend);
+		config.db_backend = strdup("keyd");
+	}
+
 	if (!config.db_backend) {
 		logthing(LOGTHING_CRITICAL, "No database backend defined.");
 		exit(EXIT_FAILURE);
diff --git a/onak-conf.c b/onak-conf.c
index 70990f8..5f07e02 100644
--- a/onak-conf.c
+++ b/onak-conf.c
@@ -33,6 +33,8 @@ struct onak_config config = {
 	NULL,			/* syncsites */
 	NULL,			/* logfile */
 
+	false,			/* use_keyd */
+
 	/*
 	 * Options for directory backends.
 	 */
@@ -55,6 +57,24 @@ struct onak_config config = {
 	&DBFUNCS,		/* Default dbfuncs struct */
 };
 
+bool parsebool(char *str, bool fallback)
+{
+	if (!strcasecmp(str, "false") || !strcasecmp(str, "no") ||
+			!strcasecmp(str, "0")) {
+		return false;
+	} else if (!strcasecmp(str, "true") || !strcasecmp(str, "yes") ||
+			!strcasecmp(str, "1")) {
+		return true;
+	} else {
+		logthing(LOGTHING_CRITICAL,
+			"Couldn't parse %s as a boolean config variable, "
+			"returning fallback of '%s'.",
+			str,
+			fallback ? "true" : "false");
+		return fallback;
+	}
+}
+
 void readconfig(const char *configfile) {
 	FILE *conffile;
 	char  curline[1024];
@@ -141,6 +161,9 @@ void readconfig(const char *configfile) {
 			config.db_backend = strdup(&curline[11]);
 		} else if (!strncmp("backends_dir ", curline, 13)) {
 			config.backends_dir = strdup(&curline[13]);
+		} else if (!strncmp("use_keyd ", curline, 9)) {
+			config.use_keyd = parsebool(&curline[9],
+						config.use_keyd);
 		} else {
 			logthing(LOGTHING_ERROR,
 				"Unknown config line: %s", curline);
diff --git a/onak-conf.h b/onak-conf.h
index 11bdbdb..16763c6 100644
--- a/onak-conf.h
+++ b/onak-conf.h
@@ -40,6 +40,11 @@ struct onak_config {
 	struct ll *syncsites;
 	char *logfile;
 
+	/*
+	 * Set if we're using keyd as the backend.
+	 */
+	bool use_keyd;
+
 	/*
 	 * Options for any database backend that needs a directory, be it the
 	 * file, db2 or db3 options.
diff --git a/onak.conf.in b/onak.conf.in
index e1b2c4f..2ee3eb4 100644
--- a/onak.conf.in
+++ b/onak.conf.in
@@ -10,6 +10,9 @@ logfile @STATEDIR@/log/onak.log
 # Loglevel : 0 is highest debug, default is 3, nothing is 7+
 loglevel 3
 
+# Should we use the keyd backend?
+use_keyd false
+
 ### Set www_port to the port on which HTTP requests should be accepted.
 ### If you do not want to process HTTP requests, set this to 0.
 
-- 
2.39.5