#include <thread>
#include "namespaces.hh"
#include "logger.hh"
+#include "lua-base4.hh"
#include "rec-lua-conf.hh"
#include "sortlist.hh"
#include "filterpo.hh"
auto types = boost::get<std::vector<std::pair<int, std::string>>>((*vars)["exportTypes"]);
for (const auto& pair : types) {
- const auto type = pair.second;
- bool found = false;
-
- for (const auto& entry : QType::names) {
- if (entry.first == type) {
- found = true;
- config.exportTypes.insert(entry.second);
- break;
- }
- }
+ const auto& type = pair.second;
- if (!found) {
- throw std::runtime_error("Unknown QType '" + type + "' in protobuf's export types");
+ QType qtype;
+ try {
+ qtype = std::stoul(type);
}
+ catch (const std::exception& ex) {
+ qtype = QType::chartocode(type.c_str());
+ if (qtype == 0) {
+ throw std::runtime_error("Unknown QType '" + type + "' in protobuf's export types");
+ }
+ }
+ config.exportTypes.insert(qtype);
}
}
}
delayedThreads.rpzPrimaryThreads.push_back(std::make_tuple(primaries, defpol, defpolOverrideLocal, maxTTL, zoneIdx, tt, maxReceivedXFRMBytes, localAddress, axfrTimeout, refresh, sr, dumpFile));
}
+// A wrapper clas that load the snadrad Lua defintions into the conext, so that we can use things
+// like pdns.A
+class RecLuaConfigContext : public BaseLua4
+{
+public:
+ RecLuaConfigContext()
+ {
+ prepareContext();
+ }
+ virtual void postPrepareContext() override
+ {
+ }
+ void postLoad() override
+ {
+ }
+ std::unique_ptr<LuaContext>& operator()()
+ {
+ return d_lw;
+ }
+};
+
void loadRecursorLuaConfig(const std::string& fname, luaConfigDelayedThreads& delayedThreads)
{
LuaConfigItems lci;
- LuaContext Lua;
+ RecLuaConfigContext Lua;
+
if (fname.empty())
return;
ifstream ifs(fname);
auto luaconfsLocal = g_luaconfs.getLocal();
lci.generation = luaconfsLocal->generation + 1;
- // pdnslog here is compatible with pdnslog in lua-base4.cc.
- Lua.writeFunction("pdnslog", [](const std::string& msg, boost::optional<int> loglevel) { g_log << (Logger::Urgency)loglevel.get_value_or(Logger::Warning) << msg << endl; });
- std::unordered_map<string, std::unordered_map<string, int>> pdns_table;
- pdns_table["loglevels"] = std::unordered_map<string, int>{
- {"Alert", LOG_ALERT},
- {"Critical", LOG_CRIT},
- {"Debug", LOG_DEBUG},
- {"Emergency", LOG_EMERG},
- {"Info", LOG_INFO},
- {"Notice", LOG_NOTICE},
- {"Warning", LOG_WARNING},
- {"Error", LOG_ERR}};
- Lua.writeVariable("pdns", pdns_table);
-
- Lua.writeFunction("clearSortlist", [&lci]() { lci.sortlist.clear(); });
+ Lua()->writeFunction("clearSortlist", [&lci]() { lci.sortlist.clear(); });
/* we can get: "1.2.3.4"
{"1.2.3.4", "4.5.6.7"}
{"NODATA", DNSFilterEngine::PolicyKind::NODATA},
{"Truncate", DNSFilterEngine::PolicyKind::Truncate},
{"Custom", DNSFilterEngine::PolicyKind::Custom}};
- Lua.writeVariable("Policy", pmap);
+ Lua()->writeVariable("Policy", pmap);
- Lua.writeFunction("rpzFile", [&lci](const string& filename, boost::optional<rpzOptions_t> options) {
+ Lua()->writeFunction("rpzFile", [&lci](const string& filename, boost::optional<rpzOptions_t> options) {
try {
boost::optional<DNSFilterEngine::Policy> defpol;
bool defpolOverrideLocal = true;
}
});
- Lua.writeFunction("rpzMaster", [&lci, &delayedThreads](const boost::variant<string, std::vector<std::pair<int, string>>>& primaries_, const string& zoneName, boost::optional<rpzOptions_t> options) {
+ Lua()->writeFunction("rpzMaster", [&lci, &delayedThreads](const boost::variant<string, std::vector<std::pair<int, string>>>& primaries_, const string& zoneName, boost::optional<rpzOptions_t> options) {
g_log << Logger::Warning << "'rpzMaster' is deprecated and will be removed in a future release, use 'rpzPrimary' instead" << endl;
rpzPrimary(lci, delayedThreads, primaries_, zoneName, options);
});
- Lua.writeFunction("rpzPrimary", [&lci, &delayedThreads](const boost::variant<string, std::vector<std::pair<int, string>>>& primaries_, const string& zoneName, boost::optional<rpzOptions_t> options) {
+ Lua()->writeFunction("rpzPrimary", [&lci, &delayedThreads](const boost::variant<string, std::vector<std::pair<int, string>>>& primaries_, const string& zoneName, boost::optional<rpzOptions_t> options) {
rpzPrimary(lci, delayedThreads, primaries_, zoneName, options);
});
typedef std::unordered_map<std::string, boost::variant<uint32_t, std::string>> zoneToCacheOptions_t;
- Lua.writeFunction("zoneToCache", [&lci](const string& zoneName, const string& method, const boost::variant<string, std::vector<std::pair<int, string>>>& srcs, boost::optional<zoneToCacheOptions_t> options) {
+ Lua()->writeFunction("zoneToCache", [&lci](const string& zoneName, const string& method, const boost::variant<string, std::vector<std::pair<int, string>>>& srcs, boost::optional<zoneToCacheOptions_t> options) {
try {
RecZoneToCache::Config conf;
DNSName validZoneName(zoneName);
});
typedef vector<pair<int, boost::variant<string, vector<pair<int, string>>>>> argvec_t;
- Lua.writeFunction("addSortList",
+ Lua()->writeFunction("addSortList",
[&lci](const std::string& formask_,
const boost::variant<string, argvec_t>& masks,
boost::optional<int> order_) {
}
});
- Lua.writeFunction("addTA", [&lci](const std::string& who, const std::string& what) {
+ Lua()->writeFunction("addTA", [&lci](const std::string& who, const std::string& what) {
warnIfDNSSECDisabled("Warning: adding Trust Anchor for DNSSEC (addTA), but dnssec is set to 'off'!");
DNSName zone(who);
auto ds = std::dynamic_pointer_cast<DSRecordContent>(DSRecordContent::make(what));
lci.dsAnchors[zone].insert(*ds);
});
- Lua.writeFunction("clearTA", [&lci](boost::optional<string> who) {
+ Lua()->writeFunction("clearTA", [&lci](boost::optional<string> who) {
warnIfDNSSECDisabled("Warning: removing Trust Anchor for DNSSEC (clearTA), but dnssec is set to 'off'!");
if (who)
lci.dsAnchors.erase(DNSName(*who));
});
/* Remove in 4.3 */
- Lua.writeFunction("addDS", [&lci](const std::string& who, const std::string& what) {
+ Lua()->writeFunction("addDS", [&lci](const std::string& who, const std::string& what) {
warnIfDNSSECDisabled("Warning: adding Trust Anchor for DNSSEC (addDS), but dnssec is set to 'off'!");
g_log << Logger::Warning << "addDS is deprecated and will be removed in the future, switch to addTA" << endl;
DNSName zone(who);
});
/* Remove in 4.3 */
- Lua.writeFunction("clearDS", [&lci](boost::optional<string> who) {
+ Lua()->writeFunction("clearDS", [&lci](boost::optional<string> who) {
g_log << Logger::Warning << "clearDS is deprecated and will be removed in the future, switch to clearTA" << endl;
warnIfDNSSECDisabled("Warning: removing Trust Anchor for DNSSEC (clearDS), but dnssec is set to 'off'!");
if (who)
lci.dsAnchors.clear();
});
- Lua.writeFunction("addNTA", [&lci](const std::string& who, const boost::optional<std::string> why) {
+ Lua()->writeFunction("addNTA", [&lci](const std::string& who, const boost::optional<std::string> why) {
warnIfDNSSECDisabled("Warning: adding Negative Trust Anchor for DNSSEC (addNTA), but dnssec is set to 'off'!");
if (why)
lci.negAnchors[DNSName(who)] = static_cast<string>(*why);
lci.negAnchors[DNSName(who)] = "";
});
- Lua.writeFunction("clearNTA", [&lci](boost::optional<string> who) {
+ Lua()->writeFunction("clearNTA", [&lci](boost::optional<string> who) {
warnIfDNSSECDisabled("Warning: removing Negative Trust Anchor for DNSSEC (clearNTA), but dnssec is set to 'off'!");
if (who)
lci.negAnchors.erase(DNSName(*who));
lci.negAnchors.clear();
});
- Lua.writeFunction("readTrustAnchorsFromFile", [&lci](const std::string& fnamearg, const boost::optional<uint32_t> interval) {
+ Lua()->writeFunction("readTrustAnchorsFromFile", [&lci](const std::string& fnamearg, const boost::optional<uint32_t> interval) {
uint32_t realInterval = 24;
if (interval) {
realInterval = static_cast<uint32_t>(*interval);
updateTrustAnchorsFromFile(fnamearg, lci.dsAnchors);
});
- Lua.writeFunction("setProtobufMasks", [&lci](const uint8_t maskV4, uint8_t maskV6) {
+ Lua()->writeFunction("setProtobufMasks", [&lci](const uint8_t maskV4, uint8_t maskV6) {
lci.protobufMaskV4 = maskV4;
lci.protobufMaskV6 = maskV6;
});
- Lua.writeFunction("protobufServer", [&lci](boost::variant<const std::string, const std::unordered_map<int, std::string>> servers, boost::optional<protobufOptions_t> vars) {
+ Lua()->writeFunction("protobufServer", [&lci](boost::variant<const std::string, const std::unordered_map<int, std::string>> servers, boost::optional<protobufOptions_t> vars) {
if (!lci.protobufExportConfig.enabled) {
lci.protobufExportConfig.enabled = true;
}
});
- Lua.writeFunction("outgoingProtobufServer", [&lci](boost::variant<const std::string, const std::unordered_map<int, std::string>> servers, boost::optional<protobufOptions_t> vars) {
+ Lua()->writeFunction("outgoingProtobufServer", [&lci](boost::variant<const std::string, const std::unordered_map<int, std::string>> servers, boost::optional<protobufOptions_t> vars) {
if (!lci.outgoingProtobufExportConfig.enabled) {
lci.outgoingProtobufExportConfig.enabled = true;
});
#ifdef HAVE_FSTRM
- Lua.writeFunction("dnstapFrameStreamServer", [&lci](boost::variant<const std::string, const std::unordered_map<int, std::string>> servers, boost::optional<frameStreamOptions_t> vars) {
+ Lua()->writeFunction("dnstapFrameStreamServer", [&lci](boost::variant<const std::string, const std::unordered_map<int, std::string>> servers, boost::optional<frameStreamOptions_t> vars) {
if (!lci.frameStreamExportConfig.enabled) {
lci.frameStreamExportConfig.enabled = true;
#endif /* HAVE_FSTRM */
try {
- Lua.executeCode(ifs);
+ Lua()->executeCode(ifs);
g_luaconfs.setState(std::move(lci));
}
catch (const LuaContext::ExecutionErrorException& e) {