From 8401755f1428fd6526bf7a1c102e1135873a6b40 Mon Sep 17 00:00:00 2001 From: Aki Tuomi Date: Fri, 3 Jun 2022 13:16:04 +0300 Subject: [PATCH] Add and use lua-global-include-path setting --- docs/settings.rst | 12 ++++++++++++ modules/lua2backend/lua2api2.hh | 1 + pdns/auth-main.cc | 1 + pdns/auth-secondarycommunicator.cc | 6 +++--- pdns/lua-record.cc | 2 +- pdns/packethandler.cc | 10 ++++++---- pdns/recursordist/settings/table.py | 10 ++++++++++ 7 files changed, 34 insertions(+), 8 deletions(-) diff --git a/docs/settings.rst b/docs/settings.rst index a8f7c07f70..2cb26b4ead 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -1046,6 +1046,18 @@ Amount of time (in seconds) between subsequent cleanup routines for pre-computed Amount of time (in seconds) a pre-computed hash entry will be considered as expired when unused. See :func:`pickchashed()`. +.. _setting-lua-global-include-dir: + +``lua-global-include-dir`` +--------------------------- + +- String +- Default: empty +- Example: ``/etc/pdns/lua-global/`` + +When creating a Lua context, scan this directory for additional lua files. All files that end with +.lua are loaded in order using ``POSIX`` as locale with Lua scripts. + .. _setting-lua-health-checks-expire-delay: ``lua-health-checks-expire-delay`` diff --git a/modules/lua2backend/lua2api2.hh b/modules/lua2backend/lua2api2.hh index b2f298cfce..857fb1782c 100644 --- a/modules/lua2backend/lua2api2.hh +++ b/modules/lua2backend/lua2api2.hh @@ -68,6 +68,7 @@ private: public: Lua2BackendAPIv2(const string& suffix) { + d_include_path = ::arg()["lua-global-include-dir"]; setArgPrefix("lua2" + suffix); d_debug_log = mustDo("query-logging"); prepareContext(); diff --git a/pdns/auth-main.cc b/pdns/auth-main.cc index 86ffca40c5..691242143f 100644 --- a/pdns/auth-main.cc +++ b/pdns/auth-main.cc @@ -291,6 +291,7 @@ static void declareArguments() ::arg().set("lua-prequery-script", "Lua script with prequery handler (DO NOT USE)") = ""; ::arg().set("lua-dnsupdate-policy-script", "Lua script with DNS update policy handler") = ""; + ::arg().set("lua-global-include-dir", "Include *.lua files from this directory into Lua contexts") = ""; ::arg().setSwitch("traceback-handler", "Enable the traceback handler (Linux only)") = "yes"; ::arg().setSwitch("direct-dnskey", "Fetch DNSKEY, CDS and CDNSKEY RRs from backend during DNSKEY or CDS/CDNSKEY synthesis") = "no"; diff --git a/pdns/auth-secondarycommunicator.cc b/pdns/auth-secondarycommunicator.cc index 9f21e81256..93ccc184e6 100644 --- a/pdns/auth-secondarycommunicator.cc +++ b/pdns/auth-secondarycommunicator.cc @@ -689,8 +689,8 @@ void CommunicatorClass::suck(const DNSName& domain, const ComboAddress& remote, unique_ptr pdl{nullptr}; vector scripts; - string script = ::arg()["lua-axfr-script"]; - if (B.getDomainMetadata(domain, "LUA-AXFR-SCRIPT", scripts) && !scripts.empty()) { + string script= ::arg()["lua-axfr-script"]; + if(B.getDomainMetadata(domain, "LUA-AXFR-SCRIPT", scripts) && !scripts.empty()) { if (pdns_iequals(scripts[0], "NONE")) { script.clear(); } @@ -700,7 +700,7 @@ void CommunicatorClass::suck(const DNSName& domain, const ComboAddress& remote, } if (!script.empty()) { try { - pdl = make_unique(); + pdl = make_unique(::arg()["lua-global-include-dir"]); pdl->loadFile(script); g_log << Logger::Info << logPrefix << "loaded Lua script '" << script << "'" << endl; } diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index e9b08e6623..6c33cf3d51 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -1391,7 +1391,7 @@ std::vector> luaSynth(const std::string& code, cons { if(!LUA || // we don't have a Lua state yet !g_LuaRecordSharedState) { // or we want a new one even if we had one - LUA = make_unique(); + LUA = make_unique(::arg()["lua-global-include-dir"]); setupLuaRecords(*LUA->getLua()); } diff --git a/pdns/packethandler.cc b/pdns/packethandler.cc index 087834afed..797922b387 100644 --- a/pdns/packethandler.cc +++ b/pdns/packethandler.cc @@ -71,14 +71,15 @@ PacketHandler::PacketHandler():B(g_programname), d_dk(&B) d_doExpandALIAS = ::arg().mustDo("expand-alias"); d_logDNSDetails= ::arg().mustDo("log-dns-details"); string fname= ::arg()["lua-prequery-script"]; + if(fname.empty()) { d_pdl = nullptr; } else { - d_pdl = std::make_unique(); - d_pdl->loadFile(fname); // XXX exception handling? + d_pdl = std::make_unique(::arg()["lua-global-include-dir"]); + d_pdl->loadFile(fname); } fname = ::arg()["lua-dnsupdate-policy-script"]; if (fname.empty()) @@ -87,11 +88,12 @@ PacketHandler::PacketHandler():B(g_programname), d_dk(&B) } else { - d_update_policy_lua = std::make_unique(); try { + d_update_policy_lua = std::make_unique(); d_update_policy_lua->loadFile(fname); } - catch (const std::runtime_error&) { + catch (const std::runtime_error& e) { + g_log<