From: bert hubert Date: Tue, 1 Dec 2015 16:42:15 +0000 (+0100) Subject: add default/override policies to RPZ, move RPZ config to the Lua configuration file... X-Git-Tag: dnsdist-1.0.0-alpha1~153 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ad42489c810ed0af7619f448275847e85c9ed642;p=thirdparty%2Fpdns.git add default/override policies to RPZ, move RPZ config to the Lua configuration file, fix up so that the overrides based on IP addresses in the zone actually work --- diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 8f063f28bd..261dbc5109 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -105,8 +105,6 @@ __thread shared_ptr* t_traceRegex; NetmaskGroup g_ednssubnets; SuffixMatchNode g_ednsdomains; -DNSFilterEngine g_dfe; - RecursorControlChannel s_rcc; // only active in thread 0 // for communicating with our threads @@ -611,6 +609,8 @@ void startDoResolve(void *p) vector ret; vector packet; + auto luaconfsLocal = g_luaconfs.getLocal(); + DNSPacketWriter pw(packet, dc->d_mdp.d_qname, dc->d_mdp.d_qtype, dc->d_mdp.d_qclass); pw.getHeader()->aa=0; @@ -672,7 +672,7 @@ void startDoResolve(void *p) // if there is a RecursorLua active, and it 'took' the query in preResolve, we don't launch beginResolve - dfepol = g_dfe.getQueryPolicy(dc->d_mdp.d_qname, dc->d_remote); + dfepol = luaconfsLocal->dfe.getQueryPolicy(dc->d_mdp.d_qname, dc->d_remote); switch(dfepol.d_kind) { case DNSFilterEngine::PolicyKind::NoAction: @@ -721,7 +721,7 @@ void startDoResolve(void *p) res = RCode::ServFail; } - dfepol = g_dfe.getPostPolicy(ret); + dfepol = luaconfsLocal->dfe.getPostPolicy(ret); switch(dfepol.d_kind) { case DNSFilterEngine::PolicyKind::NoAction: break; @@ -750,6 +750,7 @@ void startDoResolve(void *p) break; case DNSFilterEngine::PolicyKind::Custom: + ret.clear(); res=RCode::NoError; spoofed.d_name=dc->d_mdp.d_qname; spoofed.d_type=dfepol.d_custom->getType(); @@ -821,7 +822,7 @@ void startDoResolve(void *p) if(ret.size()) { orderAndShuffle(ret); - if(auto sl = g_luaconfs.getCopy().sortlist.getOrderCmp(dc->d_remote)) { + if(auto sl = luaconfsLocal->sortlist.getOrderCmp(dc->d_remote)) { sort(ret.begin(), ret.end(), *sl); variableAnswer=true; } @@ -2164,6 +2165,8 @@ int serviceMain(int argc, char*argv[]) seedRandom(::arg()["entropy-source"]); g_disthashseed=dns_random(0xffffffff); + loadRecursorLuaConfig(::arg()["lua-config-file"]); + parseACLs(); sortPublicSuffixList(); @@ -2275,8 +2278,6 @@ int serviceMain(int argc, char*argv[]) if(!s_pidfname.empty()) unlink(s_pidfname.c_str()); // remove possible old pid file - loadRPZFiles(); - if(::arg().mustDo("daemon")) { L< +#include #include "namespaces.hh" #include "logger.hh" #include "rec-lua-conf.hh" #include "sortlist.hh" +#include "filterpo.hh" +#include "syncres.hh" +#include "rpzloader.hh" GlobalStateHolder g_luaconfs; @@ -28,6 +32,17 @@ LuaConfigItems::LuaConfigItems() { } +/* DID YOU READ THE STORY ABOVE? */ + +template +typename C::value_type::second_type constGet(const C& c, const std::string& name) +{ + auto iter = c.find(name); + if(iter == c.end()) + return 0; + return iter->second; +} + #ifndef HAVE_LUA void loadRecursorLuaConfig(const std::string& fname) { @@ -56,6 +71,81 @@ void loadRecursorLuaConfig(const std::string& fname) {"1.2.3.4", {"4.5.6.7", "8.9.10.11"}} */ + map pmap{ + {"NoAction", DNSFilterEngine::PolicyKind::NoAction}, + {"Drop", DNSFilterEngine::PolicyKind::Drop}, + {"NXDOMAIN", DNSFilterEngine::PolicyKind::NXDOMAIN}, + {"NODATA", DNSFilterEngine::PolicyKind::NODATA}, + {"Truncate", DNSFilterEngine::PolicyKind::Truncate}, + {"Custom", DNSFilterEngine::PolicyKind::Custom} + }; + Lua.writeVariable("Policy", pmap); + + Lua.writeFunction("rpzFile", [&lci](const string& fname, const boost::optional>>& options) { + try { + boost::optional defpol; + if(options) { + auto& have = *options; + if(have.count("defpol")) { + cout<<"Set a default policy"<d_kind = (DNSFilterEngine::PolicyKind)boost::get(constGet(have, "defpol")); + if(defpol->d_kind == DNSFilterEngine::PolicyKind::Custom) { + cout<<"Setting a custom field even!"<d_custom= + shared_ptr( + DNSRecordContent::mastermake(QType::CNAME, 1, + boost::get(constGet(have,"defcontent")) + ) + ); + } + } + + } + loadRPZFromFile(fname, lci.dfe, defpol, 0); + } + catch(std::exception& e) { + theL()<>>& options) { + try { + boost::optional defpol; + if(options) { + auto& have = *options; + if(have.count("defpol")) { + // cout<<"Set a default policy"<d_kind = (DNSFilterEngine::PolicyKind)boost::get(constGet(have, "defpol")); + if(defpol->d_kind == DNSFilterEngine::PolicyKind::Custom) { + // cout<<"Setting a custom field even!"<d_custom= + shared_ptr( + DNSRecordContent::mastermake(QType::CNAME, 1, + boost::get(constGet(have,"defcontent")) + ) + ); + } + } + + } + ComboAddress master(master_, 53); + DNSName zone(zone_); + auto sr=loadRPZFromServer(master,zone, lci.dfe, defpol, 0); + std::thread t(RPZIXFRTracker, master, zone, sr); + t.detach(); + } + catch(std::exception& e) { + theL()< > > > > argvec_t; Lua.writeFunction("addSortList", [&lci](const std::string& formask_, @@ -87,8 +177,14 @@ void loadRecursorLuaConfig(const std::string& fname) theL()< g_luaconfs; diff --git a/pdns/reczones.cc b/pdns/reczones.cc index 5c364c542e..abcd934985 100644 --- a/pdns/reczones.cc +++ b/pdns/reczones.cc @@ -28,7 +28,7 @@ #include "zoneparser-tng.hh" #include "logger.hh" #include "dnsrecords.hh" - +#include "rec-lua-conf.hh" #include #include "ixfr.hh" #include "rpzloader.hh" @@ -316,7 +316,8 @@ string reloadAuthAndForwards() return "reloading failed, see log\n"; } -void ixfrTracker(const ComboAddress& master, const DNSName& zone, shared_ptr oursr) + +void RPZIXFRTracker(const ComboAddress& master, const DNSName& zone, shared_ptr oursr) { for(;;) { DNSRecord dr; @@ -324,14 +325,14 @@ void ixfrTracker(const ComboAddress& master, const DNSName& zone, shared_ptrd_st.refresh); - L<(dr.d_content)->d_st.serial<d_st.serial<(), 0); } } @@ -362,36 +363,13 @@ void ixfrTracker(const ComboAddress& master, const DNSName& zone, shared_ptr(), 0); } } } - L<d_st.serial< fnames; - stringtok(fnames, ::arg()["rpz-files"]," ,"); - int count=0; - for(const auto& f : fnames) { - loadRPZFromFile(f, g_dfe, count++); - } - - fnames.clear(); - stringtok(fnames, ::arg()["rpz-masters"]," ,"); - - for(const auto& f : fnames) { - auto s = splitField(f, ':'); - ComboAddress master(s.first, 53); - DNSName zone(s.second); - auto sr=loadRPZFromServer(master,zone, g_dfe, count++); - std::thread t(ixfrTracker, master, zone, sr); - t.detach(); + L<d_st.serial< defpol, int place) { static const DNSName drop("rpz-drop."), truncate("rpz-tcp-only."), noaction("rpz-passthru."); static const DNSName rpzClientIP("rpz-client-ip"), rpzIP("rpz-ip"), @@ -24,7 +25,10 @@ void RPZRecordToPolicy(const DNSRecord& dr, DNSFilterEngine& target, bool addOrR if(dr.d_type == QType::CNAME) { auto target=std::dynamic_pointer_cast(dr.d_content)->getTarget(); - if(target.isRoot()) { + if(defpol) { + pol=*defpol; + } + else if(target.isRoot()) { // cerr<<"Wants NXDOMAIN for "< loadRPZFromServer(const ComboAddress& master, const DNSName& zone, DNSFilterEngine& target, int place) +shared_ptr loadRPZFromServer(const ComboAddress& master, const DNSName& zone, DNSFilterEngine& target, boost::optional defpol, int place) { L< loadRPZFromServer(const ComboAddress& master, const continue; } - RPZRecordToPolicy(dr, target, true, place); + RPZRecordToPolicy(dr, target, true, defpol, place); nrecords++; } if(last != time(0)) { @@ -123,7 +127,7 @@ shared_ptr loadRPZFromServer(const ComboAddress& master, const return sr; } -int loadRPZFromFile(const std::string& fname, DNSFilterEngine& target, int place) +int loadRPZFromFile(const std::string& fname, DNSFilterEngine& target, boost::optional defpol, int place) { ZoneParserTNG zpt(fname); DNSResourceRecord drr; @@ -142,7 +146,7 @@ int loadRPZFromFile(const std::string& fname, DNSFilterEngine& target, int place } else { dr.d_name=dr.d_name.makeRelative(domain); - RPZRecordToPolicy(dr, target, true, place); + RPZRecordToPolicy(dr, target, true, defpol, place); } } catch(PDNSException& pe) { diff --git a/pdns/rpzloader.hh b/pdns/rpzloader.hh index 57d7ce07c5..42b8fba11e 100644 --- a/pdns/rpzloader.hh +++ b/pdns/rpzloader.hh @@ -3,6 +3,7 @@ #include #include "dnsrecords.hh" -int loadRPZFromFile(const std::string& fname, DNSFilterEngine& target, int place); -std::shared_ptr loadRPZFromServer(const ComboAddress& master, const DNSName& zone, DNSFilterEngine& target, int place); -void RPZRecordToPolicy(const DNSRecord& dr, DNSFilterEngine& target, bool addOrRemove, int place); +int loadRPZFromFile(const std::string& fname, DNSFilterEngine& target, boost::optional defpol, int place); +std::shared_ptr loadRPZFromServer(const ComboAddress& master, const DNSName& zone, DNSFilterEngine& target, boost::optional defpol, int place); +void RPZRecordToPolicy(const DNSRecord& dr, DNSFilterEngine& target, bool addOrRemove, boost::optional defpol, int place); +void RPZIXFRTracker(const ComboAddress& master, const DNSName& zone, shared_ptr oursr); diff --git a/pdns/syncres.cc b/pdns/syncres.cc index 7684066060..e0e39ca411 100644 --- a/pdns/syncres.cc +++ b/pdns/syncres.cc @@ -49,7 +49,7 @@ #include "lock.hh" #include "ednssubnet.hh" #include "cachecleaner.hh" - +#include "rec-lua-conf.hh" __thread SyncRes::StaticStorage* t_sstorage; unsigned int SyncRes::s_maxnegttl; @@ -956,7 +956,7 @@ int SyncRes::doResolveAt(set nameservers, DNSName auth, bool flawedNSSe ; // XXX NEED TO HANDLE OTHER POLICY KINDS HERE! - if(g_dfe.getProcessingPolicy(*tns).d_kind != DNSFilterEngine::PolicyKind::NoAction) + if(g_luaconfs.getLocal()->dfe.getProcessingPolicy(*tns).d_kind != DNSFilterEngine::PolicyKind::NoAction) throw ImmediateServFailException("Dropped because of policy"); if(!isCanonical(*tns)) { diff --git a/pdns/syncres.hh b/pdns/syncres.hh index 3988c082f1..148b6d86e7 100644 --- a/pdns/syncres.hh +++ b/pdns/syncres.hh @@ -24,8 +24,6 @@ #include "iputils.hh" #include "filterpo.hh" -extern DNSFilterEngine g_dfe; - void primeHints(void); class RecursorLua; @@ -658,8 +656,6 @@ int directResolve(const DNSName& qname, const QType& qtype, int qclass, vector T broadcastAccFunction(const boost::function& func, bool skipSelf=false); SyncRes::domainmap_t* parseAuthAndForwards(); -void loadRPZFiles(); - uint64_t* pleaseGetNsSpeedsSize(); uint64_t* pleaseGetCacheSize(); uint64_t* pleaseGetNegCacheSize();