]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/dnsdistdist/dnsdist-lua-bindings-protobuf.cc
Merge pull request #9229 from rgacogne/dnsdist-webserver-allow-from
[thirdparty/pdns.git] / pdns / dnsdistdist / dnsdist-lua-bindings-protobuf.cc
CommitLineData
4d4d5623
RG
1/*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22#include "config.h"
23#include "dnsdist.hh"
24#include "dnsdist-lua.hh"
25
26#include "dnsdist-protobuf.hh"
27#include "dnstap.hh"
28#include "fstrm_logger.hh"
29#include "remote_logger.hh"
30
31#ifdef HAVE_LIBCRYPTO
32#include "ipcipher.hh"
33#endif /* HAVE_LIBCRYPTO */
34
d12c21ab
RG
35#ifdef HAVE_FSTRM
36static void parseFSTRMOptions(const boost::optional<std::unordered_map<std::string, unsigned int>>& params, std::unordered_map<string, unsigned int>& options)
37{
38 if (!params) {
39 return;
40 }
41
42 static std::vector<std::string> const potentialOptions = { "bufferHint", "flushTimeout", "inputQueueSize", "outputQueueSize", "queueNotifyThreshold", "reopenInterval" };
43
44 for (const auto& potentialOption : potentialOptions) {
45 if (params->count(potentialOption)) {
46 options[potentialOption] = boost::get<unsigned int>(params->at(potentialOption));
47 }
48 }
49}
50#endif /* HAVE_FSTRM */
51
203b5348 52void setupLuaBindingsProtoBuf(bool client, bool configCheck)
4d4d5623
RG
53{
54#ifdef HAVE_LIBCRYPTO
55 g_lua.registerFunction<ComboAddress(ComboAddress::*)(const std::string& key)>("ipencrypt", [](const ComboAddress& ca, const std::string& key) {
56 return encryptCA(ca, key);
57 });
58 g_lua.registerFunction<ComboAddress(ComboAddress::*)(const std::string& key)>("ipdecrypt", [](const ComboAddress& ca, const std::string& key) {
59 return decryptCA(ca, key);
60 });
61
62 g_lua.writeFunction("makeIPCipherKey", [](const std::string& password) {
63 return makeIPCipherKey(password);
64 });
65#endif /* HAVE_LIBCRYPTO */
66
67 /* ProtobufMessage */
68 g_lua.registerFunction<void(DNSDistProtoBufMessage::*)(std::string)>("setTag", [](DNSDistProtoBufMessage& message, const std::string& strValue) {
69 message.addTag(strValue);
70 });
71 g_lua.registerFunction<void(DNSDistProtoBufMessage::*)(vector<pair<int, string>>)>("setTagArray", [](DNSDistProtoBufMessage& message, const vector<pair<int, string>>&tags) {
72 for (const auto& tag : tags) {
73 message.addTag(tag.second);
74 }
75 });
76 g_lua.registerFunction<void(DNSDistProtoBufMessage::*)(boost::optional <time_t> sec, boost::optional <uint32_t> uSec)>("setProtobufResponseType",
77 [](DNSDistProtoBufMessage& message, boost::optional <time_t> sec, boost::optional <uint32_t> uSec) {
78 message.setType(DNSProtoBufMessage::Response);
79 message.setQueryTime(sec?*sec:0, uSec?*uSec:0);
80 });
81 g_lua.registerFunction<void(DNSDistProtoBufMessage::*)(const std::string& strQueryName, uint16_t uType, uint16_t uClass, uint32_t uTTL, const std::string& strBlob)>("addResponseRR", [](DNSDistProtoBufMessage& message,
82 const std::string& strQueryName, uint16_t uType, uint16_t uClass, uint32_t uTTL, const std::string& strBlob) {
83 message.addRR(DNSName(strQueryName), uType, uClass, uTTL, strBlob);
84 });
85 g_lua.registerFunction<void(DNSDistProtoBufMessage::*)(const Netmask&)>("setEDNSSubnet", [](DNSDistProtoBufMessage& message, const Netmask& subnet) { message.setEDNSSubnet(subnet); });
86 g_lua.registerFunction<void(DNSDistProtoBufMessage::*)(const DNSName&, uint16_t, uint16_t)>("setQuestion", [](DNSDistProtoBufMessage& message, const DNSName& qname, uint16_t qtype, uint16_t qclass) { message.setQuestion(qname, qtype, qclass); });
87 g_lua.registerFunction<void(DNSDistProtoBufMessage::*)(size_t)>("setBytes", [](DNSDistProtoBufMessage& message, size_t bytes) { message.setBytes(bytes); });
88 g_lua.registerFunction<void(DNSDistProtoBufMessage::*)(time_t, uint32_t)>("setTime", [](DNSDistProtoBufMessage& message, time_t sec, uint32_t usec) { message.setTime(sec, usec); });
89 g_lua.registerFunction<void(DNSDistProtoBufMessage::*)(time_t, uint32_t)>("setQueryTime", [](DNSDistProtoBufMessage& message, time_t sec, uint32_t usec) { message.setQueryTime(sec, usec); });
90 g_lua.registerFunction<void(DNSDistProtoBufMessage::*)(uint8_t)>("setResponseCode", [](DNSDistProtoBufMessage& message, uint8_t rcode) { message.setResponseCode(rcode); });
91 g_lua.registerFunction<std::string(DNSDistProtoBufMessage::*)()>("toDebugString", [](const DNSDistProtoBufMessage& message) { return message.toDebugString(); });
36de3330 92 g_lua.registerFunction<void(DNSDistProtoBufMessage::*)(const ComboAddress&, boost::optional<uint16_t>)>("setRequestor", [](DNSDistProtoBufMessage& message, const ComboAddress& addr, boost::optional<uint16_t> port) {
4d4d5623 93 message.setRequestor(addr);
36de3330
RG
94 if (port) {
95 message.setRequestorPort(*port);
96 }
4d4d5623 97 });
36de3330 98 g_lua.registerFunction<void(DNSDistProtoBufMessage::*)(const std::string&, boost::optional<uint16_t>)>("setRequestorFromString", [](DNSDistProtoBufMessage& message, const std::string& str, boost::optional<uint16_t> port) {
4d4d5623 99 message.setRequestor(str);
36de3330
RG
100 if (port) {
101 message.setRequestorPort(*port);
102 }
4d4d5623 103 });
36de3330 104 g_lua.registerFunction<void(DNSDistProtoBufMessage::*)(const ComboAddress&, boost::optional<uint16_t>)>("setResponder", [](DNSDistProtoBufMessage& message, const ComboAddress& addr, boost::optional<uint16_t> port) {
4d4d5623 105 message.setResponder(addr);
36de3330
RG
106 if (port) {
107 message.setResponderPort(*port);
108 }
4d4d5623 109 });
36de3330 110 g_lua.registerFunction<void(DNSDistProtoBufMessage::*)(const std::string&, boost::optional<uint16_t>)>("setResponderFromString", [](DNSDistProtoBufMessage& message, const std::string& str, boost::optional<uint16_t> port) {
4d4d5623 111 message.setResponder(str);
36de3330
RG
112 if (port) {
113 message.setResponderPort(*port);
114 }
4d4d5623
RG
115 });
116 g_lua.registerFunction<void(DNSDistProtoBufMessage::*)(const std::string&)>("setServerIdentity", [](DNSDistProtoBufMessage& message, const std::string& str) {
117 message.setServerIdentity(str);
118 });
119
120 g_lua.registerFunction<std::string(DnstapMessage::*)()>("toDebugString", [](const DnstapMessage& message) { return message.toDebugString(); });
121 g_lua.registerFunction<void(DnstapMessage::*)(const std::string&)>("setExtra", [](DnstapMessage& message, const std::string& str) {
122 message.setExtra(str);
123 });
124
125 /* RemoteLogger */
203b5348
RG
126 g_lua.writeFunction("newRemoteLogger", [client,configCheck](const std::string& remote, boost::optional<uint16_t> timeout, boost::optional<uint64_t> maxQueuedEntries, boost::optional<uint8_t> reconnectWaitTime) {
127 if (client || configCheck) {
7f29529f
RG
128 return std::shared_ptr<RemoteLoggerInterface>(nullptr);
129 }
4d4d5623
RG
130 return std::shared_ptr<RemoteLoggerInterface>(new RemoteLogger(ComboAddress(remote), timeout ? *timeout : 2, maxQueuedEntries ? (*maxQueuedEntries*100) : 10000, reconnectWaitTime ? *reconnectWaitTime : 1, client));
131 });
132
d12c21ab 133 g_lua.writeFunction("newFrameStreamUnixLogger", [client,configCheck](const std::string& address, boost::optional<std::unordered_map<std::string, unsigned int>> params) {
4d4d5623 134#ifdef HAVE_FSTRM
203b5348 135 if (client || configCheck) {
7f29529f
RG
136 return std::shared_ptr<RemoteLoggerInterface>(nullptr);
137 }
d12c21ab
RG
138
139 std::unordered_map<string, unsigned int> options;
140 parseFSTRMOptions(params, options);
141 return std::shared_ptr<RemoteLoggerInterface>(new FrameStreamLogger(AF_UNIX, address, !client, options));
4d4d5623
RG
142#else
143 throw std::runtime_error("fstrm support is required to build an AF_UNIX FrameStreamLogger");
144#endif /* HAVE_FSTRM */
145 });
146
d12c21ab 147 g_lua.writeFunction("newFrameStreamTcpLogger", [client,configCheck](const std::string& address, boost::optional<std::unordered_map<std::string, unsigned int>> params) {
4d4d5623 148#if defined(HAVE_FSTRM) && defined(HAVE_FSTRM_TCP_WRITER_INIT)
203b5348 149 if (client || configCheck) {
7f29529f
RG
150 return std::shared_ptr<RemoteLoggerInterface>(nullptr);
151 }
d12c21ab
RG
152
153 std::unordered_map<string, unsigned int> options;
154 parseFSTRMOptions(params, options);
155 return std::shared_ptr<RemoteLoggerInterface>(new FrameStreamLogger(AF_INET, address, !client, options));
4d4d5623
RG
156#else
157 throw std::runtime_error("fstrm with TCP support is required to build an AF_INET FrameStreamLogger");
158#endif /* HAVE_FSTRM */
159 });
160
7f29529f
RG
161 g_lua.registerFunction<std::string(std::shared_ptr<RemoteLoggerInterface>::*)()>("toString", [](const std::shared_ptr<RemoteLoggerInterface>& logger) {
162 if (logger) {
163 return logger->toString();
164 }
165 return std::string();
166 });
4d4d5623 167}