]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnsdist-lua-bindings.cc
dnsdist: Allow wrapping the FFI interface for the existing DNSQuestion object
[thirdparty/pdns.git] / pdns / dnsdist-lua-bindings.cc
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 "dolog.hh"
27
28 void setupLuaBindings(bool client)
29 {
30 g_lua.writeFunction("infolog", [](const string& arg) {
31 infolog("%s", arg);
32 });
33 g_lua.writeFunction("errlog", [](const string& arg) {
34 errlog("%s", arg);
35 });
36 g_lua.writeFunction("warnlog", [](const string& arg) {
37 warnlog("%s", arg);
38 });
39 g_lua.writeFunction("show", [](const string& arg) {
40 g_outputBuffer+=arg;
41 g_outputBuffer+="\n";
42 });
43
44 /* Exceptions */
45 g_lua.registerFunction<string(std::exception_ptr::*)()>("__tostring", [](const std::exception_ptr& eptr) {
46 try {
47 if (eptr) {
48 std::rethrow_exception(eptr);
49 }
50 } catch(const std::exception& e) {
51 return string(e.what());
52 } catch(const PDNSException& e) {
53 return e.reason;
54 } catch(...) {
55 return string("Unknown exception");
56 }
57 return string("No exception");
58 });
59 /* ServerPolicy */
60 g_lua.writeFunction("newServerPolicy", [](string name, policyfunc_t policy) { return ServerPolicy{name, policy, true};});
61 g_lua.registerMember("name", &ServerPolicy::name);
62 g_lua.registerMember("policy", &ServerPolicy::policy);
63 g_lua.registerMember("isLua", &ServerPolicy::isLua);
64 g_lua.registerFunction("toString", &ServerPolicy::toString);
65
66 g_lua.writeVariable("firstAvailable", ServerPolicy{"firstAvailable", firstAvailable, false});
67 g_lua.writeVariable("roundrobin", ServerPolicy{"roundrobin", roundrobin, false});
68 g_lua.writeVariable("wrandom", ServerPolicy{"wrandom", wrandom, false});
69 g_lua.writeVariable("whashed", ServerPolicy{"whashed", whashed, false});
70 g_lua.writeVariable("chashed", ServerPolicy{"chashed", chashed, false});
71 g_lua.writeVariable("leastOutstanding", ServerPolicy{"leastOutstanding", leastOutstanding, false});
72
73 /* ServerPool */
74 g_lua.registerFunction<void(std::shared_ptr<ServerPool>::*)(std::shared_ptr<DNSDistPacketCache>)>("setCache", [](std::shared_ptr<ServerPool> pool, std::shared_ptr<DNSDistPacketCache> cache) {
75 if (pool) {
76 pool->packetCache = cache;
77 }
78 });
79 g_lua.registerFunction("getCache", &ServerPool::getCache);
80 g_lua.registerFunction<void(std::shared_ptr<ServerPool>::*)()>("unsetCache", [](std::shared_ptr<ServerPool> pool) {
81 if (pool) {
82 pool->packetCache = nullptr;
83 }
84 });
85 g_lua.registerFunction("getECS", &ServerPool::getECS);
86 g_lua.registerFunction("setECS", &ServerPool::setECS);
87
88 /* DownstreamState */
89 g_lua.registerFunction<void(DownstreamState::*)(int)>("setQPS", [](DownstreamState& s, int lim) { s.qps = lim ? QPSLimiter(lim, lim) : QPSLimiter(); });
90 g_lua.registerFunction<void(std::shared_ptr<DownstreamState>::*)(string)>("addPool", [](std::shared_ptr<DownstreamState> s, string pool) {
91 auto localPools = g_pools.getCopy();
92 addServerToPool(localPools, pool, s);
93 g_pools.setState(localPools);
94 s->pools.insert(pool);
95 });
96 g_lua.registerFunction<void(std::shared_ptr<DownstreamState>::*)(string)>("rmPool", [](std::shared_ptr<DownstreamState> s, string pool) {
97 auto localPools = g_pools.getCopy();
98 removeServerFromPool(localPools, pool, s);
99 g_pools.setState(localPools);
100 s->pools.erase(pool);
101 });
102 g_lua.registerFunction<uint64_t(DownstreamState::*)()>("getOutstanding", [](const DownstreamState& s) { return s.outstanding.load(); });
103 g_lua.registerFunction("isUp", &DownstreamState::isUp);
104 g_lua.registerFunction("setDown", &DownstreamState::setDown);
105 g_lua.registerFunction("setUp", &DownstreamState::setUp);
106 g_lua.registerFunction<void(DownstreamState::*)(boost::optional<bool> newStatus)>("setAuto", [](DownstreamState& s, boost::optional<bool> newStatus) {
107 if (newStatus) {
108 s.upStatus = *newStatus;
109 }
110 s.setAuto();
111 });
112 g_lua.registerFunction("getName", &DownstreamState::getName);
113 g_lua.registerFunction("getNameWithAddr", &DownstreamState::getNameWithAddr);
114 g_lua.registerMember("upStatus", &DownstreamState::upStatus);
115 g_lua.registerMember<int (DownstreamState::*)>("weight",
116 [](const DownstreamState& s) -> int {return s.weight;},
117 [](DownstreamState& s, int newWeight) {s.setWeight(newWeight);}
118 );
119 g_lua.registerMember("order", &DownstreamState::order);
120 g_lua.registerMember("name", &DownstreamState::name);
121 g_lua.registerFunction<std::string(DownstreamState::*)()>("getID", [](const DownstreamState& s) { return boost::uuids::to_string(s.id); });
122
123 /* dnsheader */
124 g_lua.registerFunction<void(dnsheader::*)(bool)>("setRD", [](dnsheader& dh, bool v) {
125 dh.rd=v;
126 });
127
128 g_lua.registerFunction<bool(dnsheader::*)()>("getRD", [](dnsheader& dh) {
129 return (bool)dh.rd;
130 });
131
132 g_lua.registerFunction<void(dnsheader::*)(bool)>("setRA", [](dnsheader& dh, bool v) {
133 dh.ra=v;
134 });
135
136 g_lua.registerFunction<bool(dnsheader::*)()>("getRA", [](dnsheader& dh) {
137 return (bool)dh.ra;
138 });
139
140 g_lua.registerFunction<void(dnsheader::*)(bool)>("setAD", [](dnsheader& dh, bool v) {
141 dh.ad=v;
142 });
143
144 g_lua.registerFunction<bool(dnsheader::*)()>("getAD", [](dnsheader& dh) {
145 return (bool)dh.ad;
146 });
147
148 g_lua.registerFunction<void(dnsheader::*)(bool)>("setAA", [](dnsheader& dh, bool v) {
149 dh.aa=v;
150 });
151
152 g_lua.registerFunction<bool(dnsheader::*)()>("getAA", [](dnsheader& dh) {
153 return (bool)dh.aa;
154 });
155
156 g_lua.registerFunction<void(dnsheader::*)(bool)>("setCD", [](dnsheader& dh, bool v) {
157 dh.cd=v;
158 });
159
160 g_lua.registerFunction<bool(dnsheader::*)()>("getCD", [](dnsheader& dh) {
161 return (bool)dh.cd;
162 });
163
164 g_lua.registerFunction<void(dnsheader::*)(bool)>("setTC", [](dnsheader& dh, bool v) {
165 dh.tc=v;
166 if(v) dh.ra = dh.rd; // you'll always need this, otherwise TC=1 gets ignored
167 });
168
169 g_lua.registerFunction<void(dnsheader::*)(bool)>("setQR", [](dnsheader& dh, bool v) {
170 dh.qr=v;
171 });
172
173 /* ComboAddress */
174 g_lua.writeFunction("newCA", [](const std::string& name) { return ComboAddress(name); });
175 g_lua.writeFunction("newCAFromRaw", [](const std::string& raw, boost::optional<uint16_t> port) {
176 if (raw.size() == 4) {
177 struct sockaddr_in sin4;
178 memset(&sin4, 0, sizeof(sin4));
179 sin4.sin_family = AF_INET;
180 memcpy(&sin4.sin_addr.s_addr, raw.c_str(), raw.size());
181 if (port) {
182 sin4.sin_port = htons(*port);
183 }
184 return ComboAddress(&sin4);
185 }
186 else if (raw.size() == 16) {
187 struct sockaddr_in6 sin6;
188 memset(&sin6, 0, sizeof(sin6));
189 sin6.sin6_family = AF_INET6;
190 memcpy(&sin6.sin6_addr.s6_addr, raw.c_str(), raw.size());
191 if (port) {
192 sin6.sin6_port = htons(*port);
193 }
194 return ComboAddress(&sin6);
195 }
196 return ComboAddress();
197 });
198 g_lua.registerFunction<string(ComboAddress::*)()>("tostring", [](const ComboAddress& ca) { return ca.toString(); });
199 g_lua.registerFunction<string(ComboAddress::*)()>("tostringWithPort", [](const ComboAddress& ca) { return ca.toStringWithPort(); });
200 g_lua.registerFunction<string(ComboAddress::*)()>("toString", [](const ComboAddress& ca) { return ca.toString(); });
201 g_lua.registerFunction<string(ComboAddress::*)()>("toStringWithPort", [](const ComboAddress& ca) { return ca.toStringWithPort(); });
202 g_lua.registerFunction<uint16_t(ComboAddress::*)()>("getPort", [](const ComboAddress& ca) { return ntohs(ca.sin4.sin_port); } );
203 g_lua.registerFunction<void(ComboAddress::*)(unsigned int)>("truncate", [](ComboAddress& ca, unsigned int bits) { ca.truncate(bits); });
204 g_lua.registerFunction<bool(ComboAddress::*)()>("isIPv4", [](const ComboAddress& ca) { return ca.sin4.sin_family == AF_INET; });
205 g_lua.registerFunction<bool(ComboAddress::*)()>("isIPv6", [](const ComboAddress& ca) { return ca.sin4.sin_family == AF_INET6; });
206 g_lua.registerFunction<bool(ComboAddress::*)()>("isMappedIPv4", [](const ComboAddress& ca) { return ca.isMappedIPv4(); });
207 g_lua.registerFunction<ComboAddress(ComboAddress::*)()>("mapToIPv4", [](const ComboAddress& ca) { return ca.mapToIPv4(); });
208 g_lua.registerFunction<bool(nmts_t::*)(const ComboAddress&)>("match", [](nmts_t& s, const ComboAddress& ca) { return s.match(ca); });
209
210 /* DNSName */
211 g_lua.registerFunction("isPartOf", &DNSName::isPartOf);
212 g_lua.registerFunction<bool(DNSName::*)()>("chopOff", [](DNSName&dn ) { return dn.chopOff(); });
213 g_lua.registerFunction<unsigned int(DNSName::*)()>("countLabels", [](const DNSName& name) { return name.countLabels(); });
214 g_lua.registerFunction<size_t(DNSName::*)()>("wirelength", [](const DNSName& name) { return name.wirelength(); });
215 g_lua.registerFunction<string(DNSName::*)()>("tostring", [](const DNSName&dn ) { return dn.toString(); });
216 g_lua.registerFunction<string(DNSName::*)()>("toString", [](const DNSName&dn ) { return dn.toString(); });
217 g_lua.writeFunction("newDNSName", [](const std::string& name) { return DNSName(name); });
218 g_lua.writeFunction("newDNSNameFromRaw", [](const std::string& name) { return DNSName(name.c_str(), name.size(), 0, false); });
219 g_lua.writeFunction("newSuffixMatchNode", []() { return SuffixMatchNode(); });
220 g_lua.writeFunction("newDNSNameSet", []() { return DNSNameSet(); });
221
222 /* DNSNameSet */
223 g_lua.registerFunction<string(DNSNameSet::*)()>("toString", [](const DNSNameSet&dns ) { return dns.toString(); });
224 g_lua.registerFunction<void(DNSNameSet::*)(DNSName&)>("add", [](DNSNameSet& dns, DNSName& dn) { dns.insert(dn); });
225 g_lua.registerFunction<bool(DNSNameSet::*)(DNSName&)>("check", [](DNSNameSet& dns, DNSName& dn) { return dns.find(dn) != dns.end(); });
226 g_lua.registerFunction("delete",(size_t (DNSNameSet::*)(const DNSName&)) &DNSNameSet::erase);
227 g_lua.registerFunction("size",(size_t (DNSNameSet::*)() const) &DNSNameSet::size);
228 g_lua.registerFunction("clear",(void (DNSNameSet::*)()) &DNSNameSet::clear);
229 g_lua.registerFunction("empty",(bool (DNSNameSet::*)()) &DNSNameSet::empty);
230
231 /* SuffixMatchNode */
232 g_lua.registerFunction<void (SuffixMatchNode::*)(const boost::variant<DNSName, string, vector<pair<int, DNSName>>, vector<pair<int, string>>> &name)>("add", [](SuffixMatchNode &smn, const boost::variant<DNSName, string, vector<pair<int, DNSName>>, vector<pair<int, string>>> &name) {
233 if (name.type() == typeid(DNSName)) {
234 auto n = boost::get<DNSName>(name);
235 smn.add(n);
236 return;
237 }
238 if (name.type() == typeid(string)) {
239 auto n = boost::get<string>(name);
240 smn.add(n);
241 return;
242 }
243 if (name.type() == typeid(vector<pair<int, DNSName>>)) {
244 auto names = boost::get<vector<pair<int, DNSName>>>(name);
245 for (auto const n : names) {
246 smn.add(n.second);
247 }
248 return;
249 }
250 if (name.type() == typeid(vector<pair<int, string>>)) {
251 auto names = boost::get<vector<pair<int, string>>>(name);
252 for (auto const n : names) {
253 smn.add(n.second);
254 }
255 return;
256 }
257 });
258 g_lua.registerFunction("check",(bool (SuffixMatchNode::*)(const DNSName&) const) &SuffixMatchNode::check);
259
260 /* NetmaskGroup */
261 g_lua.writeFunction("newNMG", []() { return NetmaskGroup(); });
262 g_lua.registerFunction<void(NetmaskGroup::*)(const std::string&mask)>("addMask", [](NetmaskGroup&nmg, const std::string& mask)
263 {
264 nmg.addMask(mask);
265 });
266 g_lua.registerFunction<void(NetmaskGroup::*)(const std::map<ComboAddress,int>& map)>("addMasks", [](NetmaskGroup&nmg, const std::map<ComboAddress,int>& map)
267 {
268 for (const auto& entry : map) {
269 nmg.addMask(Netmask(entry.first));
270 }
271 });
272
273 g_lua.registerFunction("match", (bool (NetmaskGroup::*)(const ComboAddress&) const)&NetmaskGroup::match);
274 g_lua.registerFunction("size", &NetmaskGroup::size);
275 g_lua.registerFunction("clear", &NetmaskGroup::clear);
276 g_lua.registerFunction<string(NetmaskGroup::*)()>("toString", [](const NetmaskGroup& nmg ) { return "NetmaskGroup " + nmg.toString(); });
277
278 /* QPSLimiter */
279 g_lua.writeFunction("newQPSLimiter", [](int rate, int burst) { return QPSLimiter(rate, burst); });
280 g_lua.registerFunction("check", &QPSLimiter::check);
281
282 /* ClientState */
283 g_lua.registerFunction<std::string(ClientState::*)()>("toString", [](const ClientState& fe) {
284 setLuaNoSideEffect();
285 return fe.local.toStringWithPort();
286 });
287 g_lua.registerMember("muted", &ClientState::muted);
288 #ifdef HAVE_EBPF
289 g_lua.registerFunction<void(ClientState::*)(std::shared_ptr<BPFFilter>)>("attachFilter", [](ClientState& frontend, std::shared_ptr<BPFFilter> bpf) {
290 if (bpf) {
291 frontend.attachFilter(bpf);
292 }
293 });
294 g_lua.registerFunction<void(ClientState::*)()>("detachFilter", [](ClientState& frontend) {
295 frontend.detachFilter();
296 });
297 #endif /* HAVE_EBPF */
298
299 /* BPF Filter */
300 #ifdef HAVE_EBPF
301 g_lua.writeFunction("newBPFFilter", [client](uint32_t maxV4, uint32_t maxV6, uint32_t maxQNames) {
302 if (client) {
303 return std::shared_ptr<BPFFilter>(nullptr);
304 }
305 return std::make_shared<BPFFilter>(maxV4, maxV6, maxQNames);
306 });
307
308 g_lua.registerFunction<void(std::shared_ptr<BPFFilter>::*)(const ComboAddress& ca)>("block", [](std::shared_ptr<BPFFilter> bpf, const ComboAddress& ca) {
309 if (bpf) {
310 return bpf->block(ca);
311 }
312 });
313
314 g_lua.registerFunction<void(std::shared_ptr<BPFFilter>::*)(const DNSName& qname, boost::optional<uint16_t> qtype)>("blockQName", [](std::shared_ptr<BPFFilter> bpf, const DNSName& qname, boost::optional<uint16_t> qtype) {
315 if (bpf) {
316 return bpf->block(qname, qtype ? *qtype : 255);
317 }
318 });
319
320 g_lua.registerFunction<void(std::shared_ptr<BPFFilter>::*)(const ComboAddress& ca)>("unblock", [](std::shared_ptr<BPFFilter> bpf, const ComboAddress& ca) {
321 if (bpf) {
322 return bpf->unblock(ca);
323 }
324 });
325
326 g_lua.registerFunction<void(std::shared_ptr<BPFFilter>::*)(const DNSName& qname, boost::optional<uint16_t> qtype)>("unblockQName", [](std::shared_ptr<BPFFilter> bpf, const DNSName& qname, boost::optional<uint16_t> qtype) {
327 if (bpf) {
328 return bpf->unblock(qname, qtype ? *qtype : 255);
329 }
330 });
331
332 g_lua.registerFunction<std::string(std::shared_ptr<BPFFilter>::*)()>("getStats", [](const std::shared_ptr<BPFFilter> bpf) {
333 setLuaNoSideEffect();
334 std::string res;
335 if (bpf) {
336 std::vector<std::pair<ComboAddress, uint64_t> > stats = bpf->getAddrStats();
337 for (const auto& value : stats) {
338 if (value.first.sin4.sin_family == AF_INET) {
339 res += value.first.toString() + ": " + std::to_string(value.second) + "\n";
340 }
341 else if (value.first.sin4.sin_family == AF_INET6) {
342 res += "[" + value.first.toString() + "]: " + std::to_string(value.second) + "\n";
343 }
344 }
345 std::vector<std::tuple<DNSName, uint16_t, uint64_t> > qstats = bpf->getQNameStats();
346 for (const auto& value : qstats) {
347 res += std::get<0>(value).toString() + " " + std::to_string(std::get<1>(value)) + ": " + std::to_string(std::get<2>(value)) + "\n";
348 }
349 }
350 return res;
351 });
352
353 g_lua.registerFunction<void(std::shared_ptr<BPFFilter>::*)()>("attachToAllBinds", [](std::shared_ptr<BPFFilter> bpf) {
354 std::string res;
355 if (bpf) {
356 for (const auto& frontend : g_frontends) {
357 frontend->attachFilter(bpf);
358 }
359 }
360 });
361
362 g_lua.writeFunction("newDynBPFFilter", [client](std::shared_ptr<BPFFilter> bpf) {
363 if (client) {
364 return std::shared_ptr<DynBPFFilter>(nullptr);
365 }
366 return std::make_shared<DynBPFFilter>(bpf);
367 });
368
369 g_lua.registerFunction<void(std::shared_ptr<DynBPFFilter>::*)(const ComboAddress& addr, boost::optional<int> seconds)>("block", [](std::shared_ptr<DynBPFFilter> dbpf, const ComboAddress& addr, boost::optional<int> seconds) {
370 if (dbpf) {
371 struct timespec until;
372 clock_gettime(CLOCK_MONOTONIC, &until);
373 until.tv_sec += seconds ? *seconds : 10;
374 dbpf->block(addr, until);
375 }
376 });
377
378 g_lua.registerFunction<void(std::shared_ptr<DynBPFFilter>::*)()>("purgeExpired", [](std::shared_ptr<DynBPFFilter> dbpf) {
379 if (dbpf) {
380 struct timespec now;
381 clock_gettime(CLOCK_MONOTONIC, &now);
382 dbpf->purgeExpired(now);
383 }
384 });
385
386 g_lua.registerFunction<void(std::shared_ptr<DynBPFFilter>::*)(boost::variant<std::string, std::vector<std::pair<int, std::string>>>)>("excludeRange", [](std::shared_ptr<DynBPFFilter> dbpf, boost::variant<std::string, std::vector<std::pair<int, std::string>>> ranges) {
387 if (ranges.type() == typeid(std::vector<std::pair<int, std::string>>)) {
388 for (const auto& range : *boost::get<std::vector<std::pair<int, std::string>>>(&ranges)) {
389 dbpf->excludeRange(Netmask(range.second));
390 }
391 }
392 else {
393 dbpf->excludeRange(Netmask(*boost::get<std::string>(&ranges)));
394 }
395 });
396
397 g_lua.registerFunction<void(std::shared_ptr<DynBPFFilter>::*)(boost::variant<std::string, std::vector<std::pair<int, std::string>>>)>("includeRange", [](std::shared_ptr<DynBPFFilter> dbpf, boost::variant<std::string, std::vector<std::pair<int, std::string>>> ranges) {
398 if (ranges.type() == typeid(std::vector<std::pair<int, std::string>>)) {
399 for (const auto& range : *boost::get<std::vector<std::pair<int, std::string>>>(&ranges)) {
400 dbpf->includeRange(Netmask(range.second));
401 }
402 }
403 else {
404 dbpf->includeRange(Netmask(*boost::get<std::string>(&ranges)));
405 }
406 });
407 #endif /* HAVE_EBPF */
408
409 /* EDNSOptionView */
410 g_lua.registerFunction<size_t(EDNSOptionView::*)()>("count", [](const EDNSOptionView& option) {
411 return option.values.size();
412 });
413 g_lua.registerFunction<std::vector<string>(EDNSOptionView::*)()>("getValues", [] (const EDNSOptionView& option) {
414 std::vector<string> values;
415 for (const auto& value : option.values) {
416 values.push_back(std::string(value.content, value.size));
417 }
418 return values;
419 });
420
421 g_lua.writeFunction("newDOHResponseMapEntry", [](const std::string& regex, uint16_t status, const std::string& content, boost::optional<std::map<std::string, std::string>> customHeaders) {
422 boost::optional<std::vector<std::pair<std::string, std::string>>> headers{boost::none};
423 if (customHeaders) {
424 headers = std::vector<std::pair<std::string, std::string>>();
425 for (const auto& header : *customHeaders) {
426 headers->push_back({ boost::to_lower_copy(header.first), header.second });
427 }
428 }
429 return std::make_shared<DOHResponseMapEntry>(regex, status, content, headers);
430 });
431 }