From 808c5ef79f09eeafcbcfcd39c95d7f355b7c4b84 Mon Sep 17 00:00:00 2001 From: bert hubert Date: Tue, 15 Dec 2015 19:10:58 +0100 Subject: [PATCH] some lua developments for dnsdist and recursor - wip --- pdns/Makefile.am | 1 + pdns/dnsdist-lua.cc | 4 ++++ pdns/dnsrulactions.hh | 41 +++++++++++++++++++++++++++++++++++++++++ pdns/lua-recursor.hh | 1 + pdns/lua-recursor4.cc | 33 +++++++++++++++++++++++++++++++++ pdns/lua-recursor4.hh | 16 ++++++++++++++++ pdns/pdns_recursor.cc | 5 ++++- 7 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 pdns/lua-recursor4.cc create mode 100644 pdns/lua-recursor4.hh diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 98c6b79259..8be5817b31 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -1107,6 +1107,7 @@ pdns_recursor_SOURCES = \ logger.cc \ lua-pdns.cc lua-pdns.hh lua-iputils.cc \ lua-recursor.cc lua-recursor.hh \ + lua-recursor4.cc lua-recursor4.hh \ lwres.cc lwres.hh \ mbedtlscompat.hh \ mbedtlssigners.cc \ diff --git a/pdns/dnsdist-lua.cc b/pdns/dnsdist-lua.cc index c20cf6bbd4..4f3ac9de0b 100644 --- a/pdns/dnsdist-lua.cc +++ b/pdns/dnsdist-lua.cc @@ -478,6 +478,10 @@ vector> setupLua(bool client, const std::string& confi return std::shared_ptr(new DisableValidationAction); }); + g_lua.writeFunction("LogAction", [](const std::string& fname) { + return std::shared_ptr(new LogAction(fname)); + }); + g_lua.writeFunction("MaxQPSIPRule", [](unsigned int qps, boost::optional ipv4trunc, boost::optional ipv6trunc) { return std::shared_ptr(new MaxQPSIPRule(qps, ipv4trunc.get_value_or(32), ipv6trunc.get_value_or(64))); diff --git a/pdns/dnsrulactions.hh b/pdns/dnsrulactions.hh index d0e3621701..21fe32ea43 100644 --- a/pdns/dnsrulactions.hh +++ b/pdns/dnsrulactions.hh @@ -1,5 +1,6 @@ #include "dnsdist.hh" #include "dnsname.hh" +#include "dolog.hh" class MaxQPSIPRule : public DNSRule { @@ -406,6 +407,46 @@ public: } }; +class LogAction : public DNSAction, public boost::noncopyable +{ +public: + LogAction() : d_fp(0) + { + } + LogAction(const std::string& str) : d_fname(str) + { + if(str.empty()) + return; + d_fp = fopen(str.c_str(), "w"); + if(!d_fp) + throw std::runtime_error("Unable to open file '"+str+"' for logging: "+string(strerror(errno))); + } + ~LogAction() + { + if(d_fp) + fclose(d_fp); + } + DNSAction::Action operator()(const ComboAddress& remote, const DNSName& qname, uint16_t qtype, dnsheader* dh, uint16_t& len, string* ruleresult) const override + { + if(!d_fp) + infolog("Packet from %s for %s %s with id %d", remote.toStringWithPort(), qname.toString(), QType(qtype).getName(), dh->id); + else { + string out = qname.toDNSString(); + fwrite(out.c_str(), 1, out.size(), d_fp); + fwrite((void*)&qtype, 1, 2, d_fp); + } + return Action::None; + } + string toString() const override + { + return "log"; + } +private: + string d_fname; + FILE* d_fp; +}; + + class DisableValidationAction : public DNSAction { public: diff --git a/pdns/lua-recursor.hh b/pdns/lua-recursor.hh index 23b975c23b..65d63fb0d3 100644 --- a/pdns/lua-recursor.hh +++ b/pdns/lua-recursor.hh @@ -44,4 +44,5 @@ private: }; + #endif diff --git a/pdns/lua-recursor4.cc b/pdns/lua-recursor4.cc new file mode 100644 index 0000000000..2903fffded --- /dev/null +++ b/pdns/lua-recursor4.cc @@ -0,0 +1,33 @@ +#include "lua-recursor4.hh" +#include +#undef L +#include "ext/luawrapper/include/LuaContext.hpp" +#include "logger.hh" + +RecursorLua4::RecursorLua4(const std::string& fname) +{ + d_lw = new LuaContext; + d_lw->writeFunction("newDN", [](const std::string& dom){ return DNSName(dom); }); + d_lw->registerFunction("isPartOf", &DNSName::isPartOf); + d_lw->registerFunction("toString", &ComboAddress::toString); + d_lw->registerFunction("toString", &DNSName::toString); + + vector> pd{{"PASS", PolicyDecision::PASS}, {"DROP", PolicyDecision::DROP}, {"TRUNCATE", PolicyDecision::TRUNCATE}}; + d_lw->writeVariable("pdns", pd); + + ifstream ifs(fname); + if(!ifs) { + theL()<<"Unable to read configuration file from '"<executeCode(ifs); +} + +bool RecursorLua4::preresolve(const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector& res, int& ret, bool* variable) +{ + const auto function = d_lw->readVariable>("preresolve"); + if(!function) + return false; + ret = function(remote, local, query, qtype.getCode()); + return true; +} \ No newline at end of file diff --git a/pdns/lua-recursor4.hh b/pdns/lua-recursor4.hh new file mode 100644 index 0000000000..15ee5e07a8 --- /dev/null +++ b/pdns/lua-recursor4.hh @@ -0,0 +1,16 @@ +#pragma once +#include "iputils.hh" +#include "dnsname.hh" +#include "namespaces.hh" + +class LuaContext; +class RecursorLua4 +{ +public: + explicit RecursorLua4(const std::string& fname); + // ~RecursorLua(); + bool preresolve(const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector& res, int& ret, bool* variable); +private: + LuaContext* d_lw; +}; + diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index a6cf25a13f..ab7dd54a85 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -73,6 +73,7 @@ extern SortList g_sortlist; #include "mplexer.hh" #include "config.h" #include "lua-recursor.hh" +#include "lua-recursor4.hh" #include "version.hh" #include "responsestats.hh" #include "secpoll-recursor.hh" @@ -595,6 +596,7 @@ catch(...) void startDoResolve(void *p) { + RecursorLua4 rl4("./recursor4.lua"); DNSComboWriter* dc=(DNSComboWriter *)p; try { t_queryring->push_back(make_pair(dc->d_mdp.d_qname, dc->d_mdp.d_qtype)); @@ -714,7 +716,8 @@ void startDoResolve(void *p) break; } - if(!t_pdl->get() || !(*t_pdl)->preresolve(dc->d_remote, local, dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), ret, res, &variableAnswer)) { + + if(/* !t_pdl->get() ||*/ !rl4.preresolve(dc->d_remote, local, dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), ret, res, &variableAnswer)) { try { res = sr.beginResolve(dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), dc->d_mdp.d_qclass, ret); } -- 2.47.2