X-Git-Url: https://the.earth.li/gitweb/?a=blobdiff_plain;ds=sidebyside;f=onak-conf.c;h=f0768a0f11657f008ccf8623e7f3a9a82493233d;hb=a94e29c9a3763f96b1c773f0818b97e4fd0777b0;hp=038ad55a254d96f7d8ddfb5fea35336c28e3ff12;hpb=2e969db9d32b8fba8a22125fe5b66323b6d1f71e;p=onak.git
diff --git a/onak-conf.c b/onak-conf.c
index 038ad55..f0768a0 100644
--- a/onak-conf.c
+++ b/onak-conf.c
@@ -13,22 +13,24 @@
* more details.
*
* You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 51
- * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * this program. If not, see .
*/
-
-#include "config.h"
-
#include
#include
#include
#include
+#include
+
+#include "build-config.h"
+#include "cleankey.h"
#include "ll.h"
#include "log.h"
#include "onak-conf.h"
+#ifdef DBINIT
extern struct onak_dbctx *DBINIT(struct onak_db_config *dbcfg, bool readonly);
+#endif
/*
* config - Runtime configuration for onak.
@@ -50,14 +52,31 @@ struct onak_config config = {
.backends = NULL,
.backends_dir = NULL,
+#ifdef DBINIT
.dbinit = DBINIT,
+#else
+ .dbinit = NULL,
+#endif
- .check_sighash = true,
+ .clean_policies = ONAK_CLEAN_DROP_V3_KEYS | ONAK_CLEAN_CHECK_SIGHASH,
.bin_dir = NULL,
.mail_dir = NULL,
};
+struct onak_db_config *find_db_backend_config(struct ll *backends, char *name)
+{
+ struct ll *cur;
+
+ cur = backends;
+ while (cur != NULL && strcmp(name,
+ ((struct onak_db_config *) cur->object)->name)) {
+ cur = cur->next;
+ }
+
+ return (cur != NULL) ? (struct onak_db_config *) cur->object : NULL;
+}
+
bool parsebool(char *str, bool fallback)
{
if (!strcasecmp(str, "false") || !strcasecmp(str, "no") ||
@@ -76,7 +95,10 @@ bool parsebool(char *str, bool fallback)
}
}
-static bool parseconfigline(char *line)
+/*
+ * Parse an old pksd style config line, as used in onak 0.4.6 and earlier.
+ */
+static bool parseoldconfigline(char *line)
{
if (line[0] == '#' || line[0] == 0) {
/*
@@ -153,8 +175,147 @@ static bool parseconfigline(char *line)
} else if (!strncmp("sock_dir ", line, 9)) {
config.sock_dir = strdup(&line[9]);
} else if (!strncmp("check_sighash ", line, 9)) {
- config.check_sighash = parsebool(&line[9],
- config.check_sighash);
+ if (parsebool(&line[9], config.clean_policies &
+ ONAK_CLEAN_CHECK_SIGHASH)) {
+ config.clean_policies |=
+ ONAK_CLEAN_CHECK_SIGHASH;
+ } else {
+ config.clean_policies &=
+ ~ONAK_CLEAN_CHECK_SIGHASH;
+ }
+ } else {
+ return false;
+ }
+
+ return true;
+}
+
+/*
+ * Parse a new style .ini config line, supporting [sections] and name=value
+ * format.
+ */
+static bool parseconfigline(char *line)
+{
+ /* Yes, this means we're not re-entrant. */
+ static char section[32] = "";
+ struct onak_db_config *backend;
+ size_t len;
+ char *name, *value;
+
+ if (line[0] == '#' || line[0] == ';' ||
+ line[0] == 0) {
+ /*
+ * Comment line, ignore.
+ */
+ } else if (line[0] == '[') {
+ /* Section name */
+ len = strlen(line);
+ if (line[len - 1] != ']') {
+ logthing(LOGTHING_CRITICAL,
+ "Malformed section header '%s' in "
+ "config file.", line);
+ return false;
+ }
+ if (len > sizeof(section)) {
+ logthing(LOGTHING_CRITICAL,
+ "Section header '%s' is too long in "
+ "config file.", line);
+ return false;
+ }
+ line[len - 1] = 0;
+ strncpy(section, &line[1], len);
+ } else if ((value = strchr(line, '=')) != NULL) {
+ name = line;
+ *value++ = 0;
+
+ /* We can have multiple backend: sections */
+ if (!strncmp(section, "backend:", 8)) {
+ backend = find_db_backend_config(
+ config.backends, §ion[8]);
+ if (backend == NULL) {
+ backend = calloc(1,
+ sizeof(struct onak_db_config));
+ backend->name = strdup(§ion[8]);
+ config.backends = lladd(config.backends,
+ backend);
+ }
+
+ if (!strcmp(name, "type")) {
+ backend->type = strdup(value);
+ } else if (!strcmp(name, "location")) {
+ backend->location = strdup(value);
+ } else if (!strcmp(name, "hostname")) {
+ backend->location = strdup(value);
+ } else if (!strcmp(name, "username")) {
+ backend->location = strdup(value);
+ } else if (!strcmp(name, "password")) {
+ backend->location = strdup(value);
+ }
+
+#define MATCH(s, n) !strcmp(section, s) && !strcmp(name, n)
+ /* [main] section */
+ } else if (MATCH("main", "backend")) {
+ config.db_backend = strdup(value);
+ } else if (MATCH("main", "backends_dir")) {
+ config.backends_dir = strdup(value);
+ } else if (MATCH("main", "logfile")) {
+ config.logfile = strdup(value);
+ } else if (MATCH("main", "loglevel")) {
+ setlogthreshold(atoi(value));
+ } else if (MATCH("main", "use_keyd")) {
+ config.use_keyd = parsebool(value,
+ config.use_keyd);
+ } else if (MATCH("main", "sock_dir")) {
+ config.sock_dir = strdup(value);
+ } else if (MATCH("main", "max_reply_keys")) {
+ config.maxkeys = atoi(value);
+ /* [mail] section */
+ } else if (MATCH("mail", "maintainer_email")) {
+ config.adminemail = strdup(value);
+ } else if (MATCH("mail", "mail_dir")) {
+ config.mail_dir = strdup(value);
+ } else if (MATCH("mail", "mta")) {
+ config.mta = strdup(value);
+ } else if (MATCH("mail", "bin_dir")) {
+ config.bin_dir = strdup(value);
+ } else if (MATCH("mail", "this_site")) {
+ config.thissite = strdup(value);
+ } else if (MATCH("mail", "syncsite")) {
+ config.syncsites = lladd(config.syncsites,
+ strdup(value));
+ /* [verification] section */
+ } else if (MATCH("verification", "blacklist")) {
+ array_load(&config.blacklist, value);
+ } else if (MATCH("verification", "drop_v3")) {
+ if (parsebool(value, config.clean_policies &
+ ONAK_CLEAN_DROP_V3_KEYS)) {
+ config.clean_policies |=
+ ONAK_CLEAN_DROP_V3_KEYS;
+ } else {
+ config.clean_policies &=
+ ~ONAK_CLEAN_DROP_V3_KEYS;
+ }
+ } else if (MATCH("verification", "check_sighash")) {
+ if (parsebool(value, config.clean_policies &
+ ONAK_CLEAN_CHECK_SIGHASH)) {
+ config.clean_policies |=
+ ONAK_CLEAN_CHECK_SIGHASH;
+ } else {
+ config.clean_policies &=
+ ~ONAK_CLEAN_CHECK_SIGHASH;
+ }
+ } else if (MATCH("verification", "check_packet_size")) {
+ if (parsebool(value, config.clean_policies &
+ ONAK_CLEAN_LARGE_PACKETS)) {
+ config.clean_policies |=
+ ONAK_CLEAN_LARGE_PACKETS;
+ } else {
+ config.clean_policies &=
+ ~ONAK_CLEAN_LARGE_PACKETS;
+ }
+ } else {
+ return false;
+ }
} else {
return false;
}
@@ -169,29 +330,75 @@ void readconfig(const char *configfile) {
char *dir, *conf;
size_t len;
struct onak_db_config *backend;
+ bool oldstyle = false;
+ bool res;
curline[1023] = 0;
+
+ /*
+ * Try to find a config file to use. If one is explicitly provided,
+ * use that. Otherwise look in $XDG_CONFIG_HOME, $HOME and finally
+ * the build in configuration directory. We try an old style onak.conf
+ * first and then the new style onak.ini if that wasn't found - this
+ * is to prevent breaking existing installs on upgrade.
+ */
if (configfile == NULL) {
conffile = NULL;
if ((dir = getenv("XDG_CONFIG_HOME")) != NULL) {
- len = strlen(dir) + 1 + 9 + 1; /* dir + / + onak.conf + NUL */
+ /* dir + / + onak.conf + NUL */
+ len = strlen(dir) + 1 + 9 + 1;
conf = malloc(len);
snprintf(conf, len, "%s/onak.conf", dir);
conffile = fopen(conf, "r");
+ if (conffile == NULL) {
+ /* Conveniently .ini is shorter than .conf */
+ snprintf(conf, len, "%s/onak.ini", dir);
+ conffile = fopen(conf, "r");
+ } else {
+ oldstyle = true;
+ }
free(conf);
} else if ((dir = getenv("HOME")) != NULL) {
- len = strlen(dir) + 18 + 1; /* dir + /.config/onak.conf + NUL */
+ /* dir + /.config/onak.conf + NUL */
+ len = strlen(dir) + 18 + 1;
conf = malloc(len);
snprintf(conf, len, "%s/.config/onak.conf", dir);
conffile = fopen(conf, "r");
+ if (conffile == NULL) {
+ /* Conveniently .ini is shorter than .conf */
+ snprintf(conf, len, "%s/onak.ini", dir);
+ conffile = fopen(conf, "r");
+ } else {
+ oldstyle = true;
+ }
free(conf);
}
if (conffile == NULL) {
- conffile = fopen(CONFIGFILE, "r");
+ conffile = fopen(CONFIGDIR "/onak.conf", "r");
+ if (conffile == NULL) {
+ conffile = fopen(CONFIGDIR "/onak.ini", "r");
+ } else {
+ oldstyle = true;
+ }
}
} else {
+ /*
+ * Explicitly provided config file; if the filename ends .conf
+ * assume it's old style.
+ */
+ len = strlen(configfile);
+ if (!strcmp(&configfile[len - 5], ".conf")) {
+ oldstyle = true;
+ }
conffile = fopen(configfile, "r");
}
+
+ if (oldstyle) {
+ logthing(LOGTHING_CRITICAL, "Reading deprecated old-style "
+ "configuration file. This will not be "
+ "supported in the next release.");
+ }
+
if (conffile != NULL) {
if (!fgets(curline, 1023, conffile)) {
logthing(LOGTHING_CRITICAL,
@@ -200,10 +407,12 @@ void readconfig(const char *configfile) {
return;
}
- /* Add a single DB configuration */
- backend = calloc(1, sizeof(*backend));
- config.backend = backend;
- config.backends = lladd(NULL, backend);
+ if (oldstyle) {
+ /* Add a single DB configuration */
+ backend = calloc(1, sizeof(*backend));
+ config.backend = backend;
+ config.backends = lladd(NULL, backend);
+ }
while (!feof(conffile)) {
/* Strip any trailing white space */
@@ -219,7 +428,12 @@ void readconfig(const char *configfile) {
i++;
}
- if (!parseconfigline(&curline[i])) {
+ if (oldstyle) {
+ res = parseoldconfigline(&curline[i]);
+ } else {
+ res = parseconfigline(&curline[i]);
+ }
+ if (!res) {
logthing(LOGTHING_ERROR,
"Unknown config line: %s", curline);
}
@@ -232,12 +446,87 @@ void readconfig(const char *configfile) {
}
}
fclose(conffile);
+
+ if (config.db_backend == NULL) {
+ logthing(LOGTHING_CRITICAL,
+ "No database backend configured.");
+ } else if (!oldstyle) {
+ config.backend = find_db_backend_config(
+ config.backends, config.db_backend);
+ if (config.backend == NULL) {
+ logthing(LOGTHING_NOTICE,
+ "Couldn't find configuration for %s "
+ "backend.", config.db_backend);
+ }
+ }
} else {
logthing(LOGTHING_NOTICE,
"Couldn't open config file; using defaults.");
}
}
+void writeconfig(const char *configfile)
+{
+ FILE *conffile;
+ struct ll *cur;
+
+ if (configfile) {
+ conffile = fopen(configfile, "w");
+ } else {
+ conffile = stdout;
+ }
+
+#define WRITE_IF_NOT_NULL(c, s) if (c != NULL) { \
+ fprintf(conffile, s "=%s\n", c); \
+}
+#define WRITE_BOOL(c, s) fprintf(conffile, s "=%s\n", s ? "true" : "false");
+
+ fprintf(conffile, "[main]\n");
+ WRITE_IF_NOT_NULL(config.backend->name, "backend");
+ WRITE_IF_NOT_NULL(config.backends_dir, "backends_dir");
+ WRITE_IF_NOT_NULL(config.logfile, "logfile");
+ fprintf(conffile, "loglevel=%d\n", getlogthreshold());
+ WRITE_BOOL(config.use_keyd, "use_keyd");
+ WRITE_IF_NOT_NULL(config.sock_dir, "sock_dir");
+ fprintf(conffile, "max_reply_keys=%d\n", config.maxkeys);
+ fprintf(conffile, "\n");
+
+ fprintf(conffile, "[verification]\n");
+ WRITE_BOOL(config.clean_policies & ONAK_CLEAN_CHECK_SIGHASH,
+ "check_sighash");
+ fprintf(conffile, "\n");
+
+ fprintf(conffile, "[mail]\n");
+ WRITE_IF_NOT_NULL(config.adminemail, "maintainer_email");
+ WRITE_IF_NOT_NULL(config.mail_dir, "mail_dir");
+ WRITE_IF_NOT_NULL(config.mta, "mta");
+ WRITE_IF_NOT_NULL(config.bin_dir, "bin_dir");
+ WRITE_IF_NOT_NULL(config.thissite, "this_site");
+
+ cur = config.syncsites;
+ while (cur != NULL) {
+ fprintf(conffile, "syncsite=%s\n", (char *) cur->object);
+ cur = cur->next;
+ }
+
+ cur = config.backends;
+ while (cur != NULL) {
+ struct onak_db_config *backend =
+ (struct onak_db_config *) cur->object;
+ fprintf(conffile, "\n[backend:%s]\n", backend->name);
+ WRITE_IF_NOT_NULL(backend->type, "type");
+ WRITE_IF_NOT_NULL(backend->location, "location");
+ WRITE_IF_NOT_NULL(backend->hostname, "hostname");
+ WRITE_IF_NOT_NULL(backend->username, "username");
+ WRITE_IF_NOT_NULL(backend->password, "password");
+ cur = cur->next;
+ }
+
+ if (configfile) {
+ fclose(conffile);
+ }
+}
+
void cleanupdbconfig(void *object)
{
struct onak_db_config *dbconfig = (struct onak_db_config *) object;
@@ -315,4 +604,7 @@ void cleanupconfig(void) {
free(config.mail_dir);
config.mail_dir = NULL;
}
+ if (config.blacklist.count != 0) {
+ array_free(&config.blacklist);
+ }
}