From f0a01fc742e83b3f4736b5d64af3ab18148afc5a Mon Sep 17 00:00:00 2001 From: Omar Polo Date: Sat, 9 Oct 2021 21:40:55 +0000 Subject: [PATCH] two -n to dump the parsed configuration This adds a barebone dumping of the parsed configuration. It is not complete, but I'm interested in dumping the full path to `cert' and `key' in order to write some scripts that can inspect the configuration, extract the certificates and renew them when expired automatically. It's not easy to parse gmid configuration otherwise because the syntax is flexible and users can use macros. Instead, the idea is to run gmid and let it dump the configuration once it's been parsed in a static and predictable format. Now is possible to parse gmid configuration with, say, awk or perl. --- ChangeLog | 2 ++ gmid.1 | 2 ++ gmid.c | 6 ++++-- gmid.h | 1 + parse.y | 27 +++++++++++++++++++++++++++ 5 files changed, 36 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index e69c108..94403c0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ 2021-10-09 Omar Polo + * parse.y (print_conf): multiple -n to dump the parsed configuration + * contrib/gencert: add gencert, a simple script to generate self-signed certs 2021-10-04 Omar Polo diff --git a/gmid.1 b/gmid.1 index b9944ea..3912103 100644 --- a/gmid.1 +++ b/gmid.1 @@ -62,6 +62,8 @@ in the config file if present. Stays and logs on the foreground. .It Fl n Check that the configuration is valid, but don't start the server. +If specified two or more time, dump the configuration in addition to +verify it. .It Fl P Pa pidfile Write daemon's pid to the given location. .Ar pidfile diff --git a/gmid.c b/gmid.c index f0ffa3e..1b91e29 100644 --- a/gmid.c +++ b/gmid.c @@ -589,7 +589,7 @@ main(int argc, char **argv) return 0; case 'n': - conftest = 1; + conftest++; break; case 'P': @@ -639,7 +639,9 @@ main(int argc, char **argv) if (config_path == NULL) fatal("missing configuration"); parse_conf(config_path); - puts("config OK"); + fprintf(stderr, "config OK\n"); + if (conftest > 1) + print_conf(); return 0; } diff --git a/gmid.h b/gmid.h index a691b75..ecd53a2 100644 --- a/gmid.h +++ b/gmid.h @@ -291,6 +291,7 @@ void drop_priv(void); void yyerror(const char*, ...); void parse_conf(const char*); +void print_conf(void); int cmdline_symset(char *); /* log.c */ diff --git a/parse.y b/parse.y index 54282a3..255be76 100644 --- a/parse.y +++ b/parse.y @@ -811,6 +811,33 @@ parse_conf(const char *filename) exit(1); } +void +print_conf(void) +{ + struct vhost *h; + /* struct location *l; */ + /* struct envlist *e; */ + /* struct alist *a; */ + + if (conf.chroot != NULL) + printf("chroot \"%s\"\n", conf.chroot); + printf("ipv6 %s\n", conf.ipv6 ? "on" : "off"); + /* XXX: defined mimes? */ + printf("port %d\n", conf.port); + printf("prefork %d\n", conf.prefork); + /* XXX: protocols? */ + if (conf.user != NULL) + printf("user \"%s\"\n", conf.user); + + TAILQ_FOREACH(h, &hosts, vhosts) { + printf("\nserver \"%s\" {\n", h->domain); + printf(" cert \"%s\"\n", h->cert); + printf(" key \"%s\"\n", h->key); + /* TODO: print locations... */ + printf("}\n"); + } +} + int symset(const char *name, const char *val, int persist) {