From bf490f1ab8d9e6ccfdaf94814c8e6987994a7cf2 Mon Sep 17 00:00:00 2001 From: Jonathan McDowell Date: Tue, 7 Jun 2016 13:59:56 +0100 Subject: [PATCH] Add "dumpconfig" command to onak to aide configuration file migration onak is capable of parsing both old and new style configuration files, but future improvements will only be added to the new style. To aide users in the migration to the new format add a "dumpconfig" option to the onak binary which will dump the current configuration in new format. --- onak-conf.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ onak-conf.h | 8 +++++++ onak.1 | 6 ++++++ onak.c | 7 ++++++ 4 files changed, 82 insertions(+) diff --git a/onak-conf.c b/onak-conf.c index 60f2097..58940aa 100644 --- a/onak-conf.c +++ b/onak-conf.c @@ -421,6 +421,67 @@ void readconfig(const char *configfile) { } } +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.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; diff --git a/onak-conf.h b/onak-conf.h index d049520..7c15cbb 100644 --- a/onak-conf.h +++ b/onak-conf.h @@ -115,6 +115,14 @@ extern struct onak_config config; */ void readconfig(const char *configfile); +/** + * @brief write the onak config. + * @param configfile the config file to write to. + * + * Write out the config file. If config file is NULL write it to STDOUT. + */ +void writeconfig(const char *configfile); + /** * @brief clean up the config when we're shutting down. */ diff --git a/onak.1 b/onak.1 index 0ad015c..3b33947 100644 --- a/onak.1 +++ b/onak.1 @@ -39,6 +39,12 @@ Read OpenPGP keys from stdin and add them to the keyserver database. Read OpenPGP keys from stdin, run the key cleaning routines against them and dump to stdout. .TP +.B dumpconfig +Dump the running config in new .ini format to stdout, or the provided file. +Intended to help with migration from old style configuration files - onak can +read and parse the old style file and this command will output the equivalent +new style configuration. +.TP .B delete Delete a given key from the keyserver. .TP diff --git a/onak.c b/onak.c index ef71341..17625ba 100644 --- a/onak.c +++ b/onak.c @@ -318,6 +318,13 @@ int main(int argc, char *argv[]) free_publickey(keys); keys = NULL; } + } else if (!strcmp("dumpconfig", argv[optind])) { + if ((argc - optind) == 2) { + writeconfig(argv[optind + 1]); + } else { + /* Dump config to stdout */ + writeconfig(NULL); + } } else if ((argc - optind) == 2) { search = argv[optind+1]; if (search != NULL && strlen(search) == 42 && -- 2.39.5