]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
some lua developments for dnsdist and recursor - wip
authorbert hubert <bert.hubert@netherlabs.nl>
Tue, 15 Dec 2015 18:10:58 +0000 (19:10 +0100)
committerbert hubert <bert.hubert@netherlabs.nl>
Tue, 15 Dec 2015 18:10:58 +0000 (19:10 +0100)
pdns/Makefile.am
pdns/dnsdist-lua.cc
pdns/dnsrulactions.hh
pdns/lua-recursor.hh
pdns/lua-recursor4.cc [new file with mode: 0644]
pdns/lua-recursor4.hh [new file with mode: 0644]
pdns/pdns_recursor.cc

index 98c6b7925977f5f20c7c4f75930cc9397aa7364a..8be5817b31c6cd54f78f1bc59cf79cfd037c8e5b 100644 (file)
@@ -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 \
index c20cf6bbd43de2483f2f8b40995f4b5666dfdef1..4f3ac9de0bd2015b3d2408b23378b48c9bb5c135 100644 (file)
@@ -478,6 +478,10 @@ vector<std::function<void(void)>> setupLua(bool client, const std::string& confi
       return std::shared_ptr<DNSAction>(new DisableValidationAction);
     });
 
+  g_lua.writeFunction("LogAction", [](const std::string& fname) {
+      return std::shared_ptr<DNSAction>(new LogAction(fname));
+    });
+
 
   g_lua.writeFunction("MaxQPSIPRule", [](unsigned int qps, boost::optional<int> ipv4trunc, boost::optional<int> ipv6trunc) {
       return std::shared_ptr<DNSRule>(new MaxQPSIPRule(qps, ipv4trunc.get_value_or(32), ipv6trunc.get_value_or(64)));
index d0e3621701b034e9611413a2e150d3a0d2af81a0..21fe32ea431e6baa1fa4f6ea8d11a9b071a03325 100644 (file)
@@ -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:
index 23b975c23b8da8bf52a167fbc7d204cbaa7f3e36..65d63fb0d3048167edb674e06769d5cac273f9fe 100644 (file)
@@ -44,4 +44,5 @@ private:
 
 };
 
+
 #endif
diff --git a/pdns/lua-recursor4.cc b/pdns/lua-recursor4.cc
new file mode 100644 (file)
index 0000000..2903fff
--- /dev/null
@@ -0,0 +1,33 @@
+#include "lua-recursor4.hh"
+#include <fstream>
+#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<pair<string,int>> 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 '"<<fname<<"': "<<strerror(errno)<<endl;
+    return;
+  }    
+  d_lw->executeCode(ifs);
+}
+
+bool RecursorLua4::preresolve(const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector<DNSRecord>& res, int& ret, bool* variable)
+{
+  const auto function = d_lw->readVariable<std::function<bool(const ComboAddress& remote, const ComboAddress& local, const DNSName& query, uint16_t)>>("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 (file)
index 0000000..15ee5e0
--- /dev/null
@@ -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<DNSRecord>& res, int& ret, bool* variable);
+private:
+  LuaContext* d_lw;
+};
+
index a6cf25a13f92f50d6aec8a0cf3b658d4ece69704..ab7dd54a855245ebf3500ceb22559828a5ca7d09 100644 (file)
@@ -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);
       }