From: Fred Morcos Date: Tue, 6 Sep 2022 13:18:37 +0000 (+0200) Subject: rec: Implement --config[=default|=check|=diff] X-Git-Tag: rec-4.8.0-alpha1~18^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c5eab72fce3cb0bce90f7930d3da1eff13ce6c3d;p=thirdparty%2Fpdns.git rec: Implement --config[=default|=check|=diff] Closes #9435 --- diff --git a/pdns/recursordist/Makefile.am b/pdns/recursordist/Makefile.am index 8be3d49b44..d0a9a18ac5 100644 --- a/pdns/recursordist/Makefile.am +++ b/pdns/recursordist/Makefile.am @@ -508,7 +508,7 @@ pubsuffix.cc: $(srcdir)/effective_tld_names.dat sysconf_DATA = recursor.conf-dist recursor.conf-dist: pdns_recursor - $(AM_V_GEN)./pdns_recursor --config > $@ + $(AM_V_GEN)./pdns_recursor --config=default > $@ ## Manpages MANPAGES=pdns_recursor.1 \ diff --git a/pdns/recursordist/docs/manpages/pdns_recursor.1.rst b/pdns/recursordist/docs/manpages/pdns_recursor.1.rst index 47103dce87..496f4a4462 100644 --- a/pdns/recursordist/docs/manpages/pdns_recursor.1.rst +++ b/pdns/recursordist/docs/manpages/pdns_recursor.1.rst @@ -46,6 +46,11 @@ at `` chroot the process to *directory*. --client-tcp-timeout= Timeout in seconds when talking to TCP clients. +--config + Show the current configuration. There are three optional values: + ``--config=default`` to show the default configuration. + ``--config=diff`` show modified options in the current configuration. + ``--config=check`` to check the current configuration for errors. --config-dir= Location of configuration directory (recursor.conf), the default depends on the SYSCONFDIR option at build-time, which is usually diff --git a/pdns/recursordist/rec-main.cc b/pdns/recursordist/rec-main.cc index 033abae93d..0f1e624372 100644 --- a/pdns/recursordist/rec-main.cc +++ b/pdns/recursordist/rec-main.cc @@ -2814,7 +2814,7 @@ int main(int argc, char** argv) ::arg().setCmd("help", "Provide a helpful message"); ::arg().setCmd("version", "Print version string"); - ::arg().setCmd("config", "Output blank configuration"); + ::arg().setCmd("config", "Output blank configuration. You can use --config=check to test the config file and command line arguments."); ::arg().setDefaults(); g_log.toConsole(Logger::Info); ::arg().laxParse(argc, argv); // do a lax parse @@ -2871,11 +2871,6 @@ int main(int argc, char** argv) exit(99); } - if (::arg().mustDo("config")) { - cout << ::arg().configstring(false, true); - exit(0); - } - if (s_structured_logger_backend == "systemd-journal") { #ifdef HAVE_SYSTEMD if (int fd = sd_journal_stream_fd("pdns-recusor", LOG_DEBUG, 0); fd >= 0) { @@ -2899,6 +2894,49 @@ int main(int argc, char** argv) g_slogout = g_slog->withName("out"); ::arg().setSLog(startupLog); + + if (::arg().mustDo("config")) { + string config = ::arg()["config"]; + if (config == "check") { + try { + if (!::arg().file(configname.c_str())) { + SLOG(g_log << Logger::Warning << "Unable to open configuration file '" << configname << "'" << endl, + startupLog->error("No such file", "Unable to open configuration file", "config_file", Logging::Loggable(configname))); + exit(1); + } + ::arg().parse(argc, argv); + exit(0); + } + catch (const ArgException& argException) { + SLOG(g_log << Logger::Warning << "Unable to parse configuration file '" << configname << "': " << argException.reason << endl, + startupLog->error("Cannot parse configuration", "Unable to parse configuration file", "config_file", Logging::Loggable(configname), "reason", Logging::Loggable(argException.reason))); + exit(1); + } + } + else if (config == "default") { + cout << ::arg().configstring(false, true); + } + else if (config == "diff") { + if (!::arg().laxFile(configname.c_str())) { + SLOG(g_log << Logger::Warning << "Unable to open configuration file '" << configname << "'" << endl, + startupLog->error("No such file", "Unable to open configuration file", "config_file", Logging::Loggable(configname))); + exit(1); + } + ::arg().laxParse(argc, argv); + cout << ::arg().configstring(true, false); + } + else { + if (!::arg().laxFile(configname.c_str())) { + SLOG(g_log << Logger::Warning << "Unable to open configuration file '" << configname << "'" << endl, + startupLog->error("No such file", "Unable to open configuration file", "config_file", Logging::Loggable(configname))); + exit(1); + } + ::arg().laxParse(argc, argv); + cout << ::arg().configstring(true, true); + } + exit(0); + } + if (!::arg().file(configname.c_str())) { SLOG(g_log << Logger::Warning << "Unable to open configuration file '" << configname << "'" << endl, startupLog->error("No such file", "Unable to open configuration file", "config_file", Logging::Loggable(configname)));