/* * This file is part of PowerDNS or dnsdist. * Copyright -- PowerDNS.COM B.V. and its contributors * * This program is free software; you can redistribute it and/or modify * it under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * In addition, for the avoidance of any doubt, permission is granted to * link this program with OpenSSL and to (re)distribute the binaries * produced as the result of such linking. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "config.h" #include "dnsdist.hh" #include "dnsdist-lua.hh" #include "dnsdist-protobuf.hh" #include "dnstap.hh" #include "fstrm_logger.hh" #include "remote_logger.hh" #ifdef HAVE_LIBCRYPTO #include "ipcipher.hh" #endif /* HAVE_LIBCRYPTO */ #ifdef HAVE_FSTRM static void parseFSTRMOptions(const boost::optional>& params, std::unordered_map& options) { if (!params) { return; } static std::vector const potentialOptions = { "bufferHint", "flushTimeout", "inputQueueSize", "outputQueueSize", "queueNotifyThreshold", "reopenInterval" }; for (const auto& potentialOption : potentialOptions) { if (params->count(potentialOption)) { options[potentialOption] = boost::get(params->at(potentialOption)); } } } #endif /* HAVE_FSTRM */ void setupLuaBindingsProtoBuf(LuaContext& luaCtx, bool client, bool configCheck) { #ifdef HAVE_LIBCRYPTO luaCtx.registerFunction("ipencrypt", [](const ComboAddress& ca, const std::string& key) { return encryptCA(ca, key); }); luaCtx.registerFunction("ipdecrypt", [](const ComboAddress& ca, const std::string& key) { return decryptCA(ca, key); }); luaCtx.writeFunction("makeIPCipherKey", [](const std::string& password) { return makeIPCipherKey(password); }); #endif /* HAVE_LIBCRYPTO */ /* ProtobufMessage */ luaCtx.registerFunction("setTag", [](DNSDistProtoBufMessage& message, const std::string& strValue) { message.addTag(strValue); }); luaCtx.registerFunction>)>("setTagArray", [](DNSDistProtoBufMessage& message, const vector>&tags) { for (const auto& tag : tags) { message.addTag(tag.second); } }); luaCtx.registerFunction sec, boost::optional uSec)>("setProtobufResponseType", [](DNSDistProtoBufMessage& message, boost::optional sec, boost::optional uSec) { message.setType(pdns::ProtoZero::Message::MessageType::DNSResponseType); message.setQueryTime(sec ? *sec : 0, uSec ? *uSec : 0); }); luaCtx.registerFunction("addResponseRR", [](DNSDistProtoBufMessage& message, const std::string& strQueryName, uint16_t uType, uint16_t uClass, uint32_t uTTL, const std::string& strBlob) { message.addRR(DNSName(strQueryName), uType, uClass, uTTL, strBlob); }); luaCtx.registerFunction("setEDNSSubnet", [](DNSDistProtoBufMessage& message, const Netmask& subnet) { message.setEDNSSubnet(subnet); }); luaCtx.registerFunction("setQuestion", [](DNSDistProtoBufMessage& message, const DNSName& qname, uint16_t qtype, uint16_t qclass) { message.setQuestion(qname, qtype, qclass); }); luaCtx.registerFunction("setBytes", [](DNSDistProtoBufMessage& message, size_t bytes) { message.setBytes(bytes); }); luaCtx.registerFunction("setTime", [](DNSDistProtoBufMessage& message, time_t sec, uint32_t usec) { message.setTime(sec, usec); }); luaCtx.registerFunction("setQueryTime", [](DNSDistProtoBufMessage& message, time_t sec, uint32_t usec) { message.setQueryTime(sec, usec); }); luaCtx.registerFunction("setResponseCode", [](DNSDistProtoBufMessage& message, uint8_t rcode) { message.setResponseCode(rcode); }); luaCtx.registerFunction)>("setRequestor", [](DNSDistProtoBufMessage& message, const ComboAddress& addr, boost::optional port) { message.setRequestor(addr); if (port) { message.setRequestorPort(*port); } }); luaCtx.registerFunction)>("setRequestorFromString", [](DNSDistProtoBufMessage& message, const std::string& str, boost::optional port) { message.setRequestor(ComboAddress(str)); if (port) { message.setRequestorPort(*port); } }); luaCtx.registerFunction)>("setResponder", [](DNSDistProtoBufMessage& message, const ComboAddress& addr, boost::optional port) { message.setResponder(addr); if (port) { message.setResponderPort(*port); } }); luaCtx.registerFunction)>("setResponderFromString", [](DNSDistProtoBufMessage& message, const std::string& str, boost::optional port) { message.setResponder(ComboAddress(str)); if (port) { message.setResponderPort(*port); } }); luaCtx.registerFunction("setServerIdentity", [](DNSDistProtoBufMessage& message, const std::string& str) { message.setServerIdentity(str); }); luaCtx.registerFunction("setExtra", [](DnstapMessage& message, const std::string& str) { message.setExtra(str); }); /* RemoteLogger */ luaCtx.writeFunction("newRemoteLogger", [client,configCheck](const std::string& remote, boost::optional timeout, boost::optional maxQueuedEntries, boost::optional reconnectWaitTime) { if (client || configCheck) { return std::shared_ptr(nullptr); } return std::shared_ptr(new RemoteLogger(ComboAddress(remote), timeout ? *timeout : 2, maxQueuedEntries ? (*maxQueuedEntries*100) : 10000, reconnectWaitTime ? *reconnectWaitTime : 1, client)); }); luaCtx.writeFunction("newFrameStreamUnixLogger", [client,configCheck](const std::string& address, boost::optional> params) { #ifdef HAVE_FSTRM if (client || configCheck) { return std::shared_ptr(nullptr); } std::unordered_map options; parseFSTRMOptions(params, options); return std::shared_ptr(new FrameStreamLogger(AF_UNIX, address, !client, options)); #else throw std::runtime_error("fstrm support is required to build an AF_UNIX FrameStreamLogger"); #endif /* HAVE_FSTRM */ }); luaCtx.writeFunction("newFrameStreamTcpLogger", [client,configCheck](const std::string& address, boost::optional> params) { #if defined(HAVE_FSTRM) && defined(HAVE_FSTRM_TCP_WRITER_INIT) if (client || configCheck) { return std::shared_ptr(nullptr); } std::unordered_map options; parseFSTRMOptions(params, options); return std::shared_ptr(new FrameStreamLogger(AF_INET, address, !client, options)); #else throw std::runtime_error("fstrm with TCP support is required to build an AF_INET FrameStreamLogger"); #endif /* HAVE_FSTRM */ }); luaCtx.registerFunction::*)()const>("toString", [](const std::shared_ptr& logger) { if (logger) { return logger->toString(); } return std::string(); }); }