]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/lua-recursor4.cc
Merge pull request #8874 from rgacogne/ddist-proxy-protocol
[thirdparty/pdns.git] / pdns / lua-recursor4.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 "lua-recursor4.hh"
23 #include <fstream>
24 #include "logger.hh"
25 #include "dnsparser.hh"
26 #include "syncres.hh"
27 #include "namespaces.hh"
28 #include "rec_channel.hh"
29 #include "ednsoptions.hh"
30 #include "ednssubnet.hh"
31 #include "filterpo.hh"
32 #include "rec-snmp.hh"
33 #include <unordered_set>
34
35 RecursorLua4::RecursorLua4() { prepareContext(); }
36
37 static int getFakeAAAARecords(const DNSName& qname, const std::string& prefix, vector<DNSRecord>& ret)
38 {
39 int rcode=directResolve(qname, QType(QType::A), 1, ret);
40
41 ComboAddress prefixAddress(prefix);
42
43 // Remove double CNAME records
44 std::set<DNSName> seenCNAMEs;
45 ret.erase(std::remove_if(
46 ret.begin(),
47 ret.end(),
48 [&seenCNAMEs](DNSRecord& rr) {
49 if (rr.d_type == QType::CNAME) {
50 auto target = getRR<CNAMERecordContent>(rr);
51 if (target == nullptr) {
52 return false;
53 }
54 if (seenCNAMEs.count(target->getTarget()) > 0) {
55 // We've had this CNAME before, remove it
56 return true;
57 }
58 seenCNAMEs.insert(target->getTarget());
59 }
60 return false;
61 }),
62 ret.end());
63
64 bool seenA = false;
65 for(DNSRecord& rr : ret)
66 {
67 if(rr.d_type == QType::A && rr.d_place==DNSResourceRecord::ANSWER) {
68 if(auto rec = getRR<ARecordContent>(rr)) {
69 ComboAddress ipv4(rec->getCA());
70 uint32_t tmp;
71 memcpy((void*)&tmp, &ipv4.sin4.sin_addr.s_addr, 4);
72 // tmp=htonl(tmp);
73 memcpy(((char*)&prefixAddress.sin6.sin6_addr.s6_addr)+12, &tmp, 4);
74 rr.d_content = std::make_shared<AAAARecordContent>(prefixAddress);
75 rr.d_type = QType::AAAA;
76 }
77 seenA = true;
78 }
79 }
80
81 if (seenA) {
82 // We've seen an A in the ANSWER section, so there is no need to keep any
83 // SOA in the AUTHORITY section as this is not a NODATA response.
84 ret.erase(std::remove_if(
85 ret.begin(),
86 ret.end(),
87 [](DNSRecord& rr) {
88 return (rr.d_type == QType::SOA && rr.d_place==DNSResourceRecord::AUTHORITY);
89 }),
90 ret.end());
91 }
92 return rcode;
93 }
94
95 static int getFakePTRRecords(const DNSName& qname, const std::string& prefix, vector<DNSRecord>& ret)
96 {
97 /* qname has a reverse ordered IPv6 address, need to extract the underlying IPv4 address from it
98 and turn it into an IPv4 in-addr.arpa query */
99 ret.clear();
100 vector<string> parts = qname.getRawLabels();
101
102 if(parts.size() < 8)
103 return -1;
104
105 string newquery;
106 for(int n = 0; n < 4; ++n) {
107 newquery +=
108 std::to_string(stoll(parts[n*2], 0, 16) + 16*stoll(parts[n*2+1], 0, 16));
109 newquery.append(1,'.');
110 }
111 newquery += "in-addr.arpa.";
112
113
114 DNSRecord rr;
115 rr.d_name = qname;
116 rr.d_type = QType::CNAME;
117 rr.d_content = std::make_shared<CNAMERecordContent>(newquery);
118 ret.push_back(rr);
119
120 int rcode = directResolve(DNSName(newquery), QType(QType::PTR), 1, ret);
121
122 return rcode;
123
124 }
125
126 boost::optional<dnsheader> RecursorLua4::DNSQuestion::getDH() const
127 {
128 if (dh)
129 return *dh;
130 return boost::optional<dnsheader>();
131 }
132
133 vector<string> RecursorLua4::DNSQuestion::getEDNSFlags() const
134 {
135 vector<string> ret;
136 if (ednsFlags) {
137 if (*ednsFlags & EDNSOpts::DNSSECOK)
138 ret.push_back("DO");
139 }
140 return ret;
141 }
142
143 bool RecursorLua4::DNSQuestion::getEDNSFlag(string flag) const
144 {
145 if (ednsFlags) {
146 if (flag == "DO" && (*ednsFlags & EDNSOpts::DNSSECOK))
147 return true;
148 }
149 return false;
150 }
151
152 vector<pair<uint16_t, string> > RecursorLua4::DNSQuestion::getEDNSOptions() const
153 {
154 if(ednsOptions)
155 return *ednsOptions;
156 else
157 return vector<pair<uint16_t,string>>();
158 }
159
160 boost::optional<string> RecursorLua4::DNSQuestion::getEDNSOption(uint16_t code) const
161 {
162 if(ednsOptions)
163 for(const auto& o : *ednsOptions)
164 if(o.first==code)
165 return o.second;
166
167 return boost::optional<string>();
168 }
169
170 boost::optional<Netmask> RecursorLua4::DNSQuestion::getEDNSSubnet() const
171 {
172 if(ednsOptions) {
173 for(const auto& o : *ednsOptions) {
174 if(o.first==EDNSOptionCode::ECS) {
175 EDNSSubnetOpts eso;
176 if(getEDNSSubnetOptsFromString(o.second, &eso))
177 return eso.source;
178 else
179 break;
180 }
181 }
182 }
183 return boost::optional<Netmask>();
184 }
185
186 std::vector<std::pair<int, ProxyProtocolValue>> RecursorLua4::DNSQuestion::getProxyProtocolValues() const
187 {
188 std::vector<std::pair<int, ProxyProtocolValue>> result;
189 if (proxyProtocolValues) {
190 result.reserve(proxyProtocolValues->size());
191
192 int idx = 1;
193 for (const auto& value: *proxyProtocolValues) {
194 result.push_back({ idx++, value });
195 }
196 }
197
198 return result;
199 }
200
201 vector<pair<int, DNSRecord> > RecursorLua4::DNSQuestion::getRecords() const
202 {
203 vector<pair<int, DNSRecord> > ret;
204 int num=1;
205 for(const auto& r : records) {
206 ret.push_back({num++, r});
207 }
208 return ret;
209 }
210 void RecursorLua4::DNSQuestion::setRecords(const vector<pair<int, DNSRecord> >& recs)
211 {
212 records.clear();
213 for(const auto& p : recs) {
214 records.push_back(p.second);
215 }
216 }
217
218 void RecursorLua4::DNSQuestion::addRecord(uint16_t type, const std::string& content, DNSResourceRecord::Place place, boost::optional<int> ttl, boost::optional<string> name)
219 {
220 DNSRecord dr;
221 dr.d_name=name ? DNSName(*name) : qname;
222 dr.d_ttl=ttl.get_value_or(3600);
223 dr.d_type = type;
224 dr.d_place = place;
225 dr.d_content = DNSRecordContent::mastermake(type, 1, content);
226 records.push_back(dr);
227 }
228
229 void RecursorLua4::DNSQuestion::addAnswer(uint16_t type, const std::string& content, boost::optional<int> ttl, boost::optional<string> name)
230 {
231 addRecord(type, content, DNSResourceRecord::ANSWER, ttl, name);
232 }
233
234 struct DynMetric
235 {
236 std::atomic<unsigned long>* ptr;
237 void inc() { (*ptr)++; }
238 void incBy(unsigned int by) { (*ptr)+= by; }
239 unsigned long get() { return *ptr; }
240 void set(unsigned long val) { *ptr =val; }
241 };
242
243 void RecursorLua4::postPrepareContext()
244 {
245 d_lw->registerMember<const DNSName (DNSQuestion::*)>("qname", [](const DNSQuestion& dq) -> const DNSName& { return dq.qname; }, [](DNSQuestion& dq, const DNSName& newName) { (void) newName; });
246 d_lw->registerMember<uint16_t (DNSQuestion::*)>("qtype", [](const DNSQuestion& dq) -> uint16_t { return dq.qtype; }, [](DNSQuestion& dq, uint16_t newType) { (void) newType; });
247 d_lw->registerMember<bool (DNSQuestion::*)>("isTcp", [](const DNSQuestion& dq) -> bool { return dq.isTcp; }, [](DNSQuestion& dq, bool newTcp) { (void) newTcp; });
248 d_lw->registerMember<const ComboAddress (DNSQuestion::*)>("localaddr", [](const DNSQuestion& dq) -> const ComboAddress& { return dq.local; }, [](DNSQuestion& dq, const ComboAddress& newLocal) { (void) newLocal; });
249 d_lw->registerMember<const ComboAddress (DNSQuestion::*)>("remoteaddr", [](const DNSQuestion& dq) -> const ComboAddress& { return dq.remote; }, [](DNSQuestion& dq, const ComboAddress& newRemote) { (void) newRemote; });
250 d_lw->registerMember<vState (DNSQuestion::*)>("validationState", [](const DNSQuestion& dq) -> vState { return dq.validationState; }, [](DNSQuestion& dq, vState newState) { (void) newState; });
251
252 d_lw->registerMember<bool (DNSQuestion::*)>("variable", [](const DNSQuestion& dq) -> bool { return dq.variable; }, [](DNSQuestion& dq, bool newVariable) { dq.variable = newVariable; });
253 d_lw->registerMember<bool (DNSQuestion::*)>("wantsRPZ", [](const DNSQuestion& dq) -> bool { return dq.wantsRPZ; }, [](DNSQuestion& dq, bool newWantsRPZ) { dq.wantsRPZ = newWantsRPZ; });
254 d_lw->registerMember<bool (DNSQuestion::*)>("logResponse", [](const DNSQuestion& dq) -> bool { return dq.logResponse; }, [](DNSQuestion& dq, bool newLogResponse) { dq.logResponse = newLogResponse; });
255
256 d_lw->registerMember("rcode", &DNSQuestion::rcode);
257 d_lw->registerMember("tag", &DNSQuestion::tag);
258 d_lw->registerMember("requestorId", &DNSQuestion::requestorId);
259 d_lw->registerMember("deviceId", &DNSQuestion::deviceId);
260 d_lw->registerMember("deviceName", &DNSQuestion::deviceName);
261 d_lw->registerMember("followupFunction", &DNSQuestion::followupFunction);
262 d_lw->registerMember("followupPrefix", &DNSQuestion::followupPrefix);
263 d_lw->registerMember("followupName", &DNSQuestion::followupName);
264 d_lw->registerMember("data", &DNSQuestion::data);
265 d_lw->registerMember("udpQuery", &DNSQuestion::udpQuery);
266 d_lw->registerMember("udpAnswer", &DNSQuestion::udpAnswer);
267 d_lw->registerMember("udpQueryDest", &DNSQuestion::udpQueryDest);
268 d_lw->registerMember("udpCallback", &DNSQuestion::udpCallback);
269 d_lw->registerMember("appliedPolicy", &DNSQuestion::appliedPolicy);
270 d_lw->registerMember<DNSFilterEngine::Policy, std::string>("policyName",
271 [](const DNSFilterEngine::Policy& pol) -> std::string {
272 if(pol.d_name)
273 return *pol.d_name;
274 return std::string();
275 },
276 [](DNSFilterEngine::Policy& pol, const std::string& name) {
277 pol.d_name = std::make_shared<std::string>(name);
278 });
279 d_lw->registerMember("policyKind", &DNSFilterEngine::Policy::d_kind);
280 d_lw->registerMember("policyType", &DNSFilterEngine::Policy::d_type);
281 d_lw->registerMember("policyTTL", &DNSFilterEngine::Policy::d_ttl);
282 d_lw->registerMember<DNSFilterEngine::Policy, std::string>("policyCustom",
283 [](const DNSFilterEngine::Policy& pol) -> std::string {
284 std::string result;
285 if (pol.d_kind != DNSFilterEngine::PolicyKind::Custom) {
286 return result;
287 }
288
289 for (const auto& dr : pol.d_custom) {
290 if (!result.empty()) {
291 result += "\n";
292 }
293 result += dr->getZoneRepresentation();
294 }
295
296 return result;
297 },
298 [](DNSFilterEngine::Policy& pol, const std::string& content) {
299 // Only CNAMES for now, when we ever add a d_custom_type, there will be pain
300 pol.d_custom.clear();
301 pol.d_custom.push_back(DNSRecordContent::mastermake(QType::CNAME, QClass::IN, content));
302 }
303 );
304 d_lw->registerFunction("getDH", &DNSQuestion::getDH);
305 d_lw->registerFunction("getEDNSOptions", &DNSQuestion::getEDNSOptions);
306 d_lw->registerFunction("getEDNSOption", &DNSQuestion::getEDNSOption);
307 d_lw->registerFunction("getEDNSSubnet", &DNSQuestion::getEDNSSubnet);
308 d_lw->registerFunction("getProxyProtocolValues", &DNSQuestion::getProxyProtocolValues);
309 d_lw->registerFunction("getEDNSFlags", &DNSQuestion::getEDNSFlags);
310 d_lw->registerFunction("getEDNSFlag", &DNSQuestion::getEDNSFlag);
311 d_lw->registerMember("name", &DNSRecord::d_name);
312 d_lw->registerMember("type", &DNSRecord::d_type);
313 d_lw->registerMember("ttl", &DNSRecord::d_ttl);
314 d_lw->registerMember("place", &DNSRecord::d_place);
315
316 d_lw->registerMember("size", &EDNSOptionViewValue::size);
317 d_lw->registerFunction<std::string(EDNSOptionViewValue::*)()>("getContent", [](const EDNSOptionViewValue& value) { return std::string(value.content, value.size); });
318 d_lw->registerFunction<size_t(EDNSOptionView::*)()>("count", [](const EDNSOptionView& option) { return option.values.size(); });
319 d_lw->registerFunction<std::vector<string>(EDNSOptionView::*)()>("getValues", [] (const EDNSOptionView& option) {
320 std::vector<string> values;
321 for (const auto& value : option.values) {
322 values.push_back(std::string(value.content, value.size));
323 }
324 return values;
325 });
326
327 /* pre 4.2 API compatibility, when we had only one value for a given EDNS option */
328 d_lw->registerMember<uint16_t(EDNSOptionView::*)>("size", [](const EDNSOptionView& option) -> uint16_t {
329 uint16_t result = 0;
330
331 if (!option.values.empty()) {
332 result = option.values.at(0).size;
333 }
334 return result;
335 },
336 [](EDNSOptionView& option, uint16_t newSize) { (void) newSize; });
337 d_lw->registerFunction<std::string(EDNSOptionView::*)()>("getContent", [](const EDNSOptionView& option) {
338 if (option.values.empty()) {
339 return std::string();
340 }
341 return std::string(option.values.at(0).content, option.values.at(0).size); });
342
343 d_lw->registerFunction<string(DNSRecord::*)()>("getContent", [](const DNSRecord& dr) { return dr.d_content->getZoneRepresentation(); });
344 d_lw->registerFunction<boost::optional<ComboAddress>(DNSRecord::*)()>("getCA", [](const DNSRecord& dr) {
345 boost::optional<ComboAddress> ret;
346
347 if(auto rec = std::dynamic_pointer_cast<ARecordContent>(dr.d_content))
348 ret=rec->getCA(53);
349 else if(auto aaaarec = std::dynamic_pointer_cast<AAAARecordContent>(dr.d_content))
350 ret=aaaarec->getCA(53);
351 return ret;
352 });
353
354 d_lw->registerFunction<const ProxyProtocolValue, std::string()>("getContent", [](const ProxyProtocolValue& value) { return value.content; });
355 d_lw->registerFunction<const ProxyProtocolValue, uint8_t()>("getType", [](const ProxyProtocolValue& value) { return value.type; });
356
357 d_lw->registerFunction<void(DNSRecord::*)(const std::string&)>("changeContent", [](DNSRecord& dr, const std::string& newContent) { dr.d_content = DNSRecordContent::mastermake(dr.d_type, 1, newContent); });
358 d_lw->registerFunction("addAnswer", &DNSQuestion::addAnswer);
359 d_lw->registerFunction("addRecord", &DNSQuestion::addRecord);
360 d_lw->registerFunction("getRecords", &DNSQuestion::getRecords);
361 d_lw->registerFunction("setRecords", &DNSQuestion::setRecords);
362
363 d_lw->registerFunction<void(DNSQuestion::*)(const std::string&)>("addPolicyTag", [](DNSQuestion& dq, const std::string& tag) { if (dq.policyTags) { dq.policyTags->push_back(tag); } });
364 d_lw->registerFunction<void(DNSQuestion::*)(const std::vector<std::pair<int, std::string> >&)>("setPolicyTags", [](DNSQuestion& dq, const std::vector<std::pair<int, std::string> >& tags) {
365 if (dq.policyTags) {
366 dq.policyTags->clear();
367 for (const auto& tag : tags) {
368 dq.policyTags->push_back(tag.second);
369 }
370 }
371 });
372 d_lw->registerFunction<std::vector<std::pair<int, std::string> >(DNSQuestion::*)()>("getPolicyTags", [](const DNSQuestion& dq) {
373 std::vector<std::pair<int, std::string> > ret;
374 if (dq.policyTags) {
375 int count = 1;
376 for (const auto& tag : *dq.policyTags) {
377 ret.push_back({count++, tag});
378 }
379 }
380 return ret;
381 });
382
383 d_lw->registerFunction<void(DNSQuestion::*)(const std::string&)>("discardPolicy", [](DNSQuestion& dq, const std::string& policy) {
384 if (dq.discardedPolicies) {
385 (*dq.discardedPolicies)[policy] = true;
386 }
387 });
388
389 d_lw->writeFunction("newDS", []() { return SuffixMatchNode(); });
390 d_lw->registerFunction<void(SuffixMatchNode::*)(boost::variant<string,DNSName, vector<pair<unsigned int,string> > >)>(
391 "add",
392 [](SuffixMatchNode&smn, const boost::variant<string,DNSName,vector<pair<unsigned int,string> > >& in){
393 try {
394 if(auto s = boost::get<string>(&in)) {
395 smn.add(DNSName(*s));
396 }
397 else if(auto v = boost::get<vector<pair<unsigned int, string> > >(&in)) {
398 for(const auto& entry : *v)
399 smn.add(DNSName(entry.second));
400 }
401 else {
402 smn.add(boost::get<DNSName>(in));
403 }
404 }
405 catch(std::exception& e) {
406 g_log <<Logger::Error<<e.what()<<endl;
407 }
408 }
409 );
410
411 d_lw->registerFunction("check",(bool (SuffixMatchNode::*)(const DNSName&) const) &SuffixMatchNode::check);
412 d_lw->registerFunction("toString",(string (SuffixMatchNode::*)() const) &SuffixMatchNode::toString);
413
414 d_pd.push_back({"policykinds", in_t {
415 {"NoAction", (int)DNSFilterEngine::PolicyKind::NoAction},
416 {"Drop", (int)DNSFilterEngine::PolicyKind::Drop },
417 {"NXDOMAIN", (int)DNSFilterEngine::PolicyKind::NXDOMAIN},
418 {"NODATA", (int)DNSFilterEngine::PolicyKind::NODATA },
419 {"Truncate", (int)DNSFilterEngine::PolicyKind::Truncate},
420 {"Custom", (int)DNSFilterEngine::PolicyKind::Custom }
421 }});
422
423 for(const auto& n : QType::names)
424 d_pd.push_back({n.first, n.second});
425
426 d_pd.push_back({"validationstates", in_t{
427 {"Indeterminate", Indeterminate },
428 {"Bogus", Bogus },
429 {"Insecure", Insecure },
430 {"Secure", Secure },
431 }});
432
433 d_pd.push_back({"now", &g_now});
434
435 d_lw->writeFunction("getMetric", [](const std::string& str) {
436 return DynMetric{getDynMetric(str)};
437 });
438
439 d_lw->registerFunction("inc", &DynMetric::inc);
440 d_lw->registerFunction("incBy", &DynMetric::incBy);
441 d_lw->registerFunction("set", &DynMetric::set);
442 d_lw->registerFunction("get", &DynMetric::get);
443
444 d_lw->writeFunction("getStat", [](const std::string& str) {
445 uint64_t result = 0;
446 optional<uint64_t> value = getStatByName(str);
447 if (value) {
448 result = *value;
449 }
450 return result;
451 });
452
453 d_lw->writeFunction("getRecursorThreadId", []() {
454 return getRecursorThreadId();
455 });
456
457 d_lw->writeFunction("sendCustomSNMPTrap", [](const std::string& str) {
458 if (g_snmpAgent) {
459 g_snmpAgent->sendCustomTrap(str);
460 }
461 });
462
463 d_lw->writeFunction("getregisteredname", [](const DNSName &dname) {
464 return getRegisteredName(dname);
465 });
466 }
467
468 void RecursorLua4::postLoad() {
469 d_prerpz = d_lw->readVariable<boost::optional<luacall_t>>("prerpz").get_value_or(0);
470 d_preresolve = d_lw->readVariable<boost::optional<luacall_t>>("preresolve").get_value_or(0);
471 d_nodata = d_lw->readVariable<boost::optional<luacall_t>>("nodata").get_value_or(0);
472 d_nxdomain = d_lw->readVariable<boost::optional<luacall_t>>("nxdomain").get_value_or(0);
473 d_postresolve = d_lw->readVariable<boost::optional<luacall_t>>("postresolve").get_value_or(0);
474 d_preoutquery = d_lw->readVariable<boost::optional<luacall_t>>("preoutquery").get_value_or(0);
475 d_maintenance = d_lw->readVariable<boost::optional<luamaintenance_t>>("maintenance").get_value_or(0);
476
477 d_ipfilter = d_lw->readVariable<boost::optional<ipfilter_t>>("ipfilter").get_value_or(0);
478 d_gettag = d_lw->readVariable<boost::optional<gettag_t>>("gettag").get_value_or(0);
479 d_gettag_ffi = d_lw->readVariable<boost::optional<gettag_ffi_t>>("gettag_ffi").get_value_or(0);
480 }
481
482 void RecursorLua4::getFeatures(Features & features) {
483 // Add key-values pairs below.
484 // Make sure you add string values explicity converted to string.
485 // e.g. features.push_back(make_pair("somekey", string("stringvalue"));
486 // Both int and double end up as a lua number type.
487 features.push_back(make_pair("PR8001_devicename", true));
488 }
489
490 void RecursorLua4::maintenance() const
491 {
492 if (d_maintenance) {
493 d_maintenance();
494 }
495 }
496
497 bool RecursorLua4::prerpz(DNSQuestion& dq, int& ret) const
498 {
499 return genhook(d_prerpz, dq, ret);
500 }
501
502 bool RecursorLua4::preresolve(DNSQuestion& dq, int& ret) const
503 {
504 return genhook(d_preresolve, dq, ret);
505 }
506
507 bool RecursorLua4::nxdomain(DNSQuestion& dq, int& ret) const
508 {
509 return genhook(d_nxdomain, dq, ret);
510 }
511
512 bool RecursorLua4::nodata(DNSQuestion& dq, int& ret) const
513 {
514 return genhook(d_nodata, dq, ret);
515 }
516
517 bool RecursorLua4::postresolve(DNSQuestion& dq, int& ret) const
518 {
519 return genhook(d_postresolve, dq, ret);
520 }
521
522 bool RecursorLua4::preoutquery(const ComboAddress& ns, const ComboAddress& requestor, const DNSName& query, const QType& qtype, bool isTcp, vector<DNSRecord>& res, int& ret) const
523 {
524 bool variableAnswer = false;
525 bool wantsRPZ = false;
526 bool logQuery = false;
527 RecursorLua4::DNSQuestion dq(ns, requestor, query, qtype.getCode(), isTcp, variableAnswer, wantsRPZ, logQuery);
528 dq.currentRecords = &res;
529
530 return genhook(d_preoutquery, dq, ret);
531 }
532
533 bool RecursorLua4::ipfilter(const ComboAddress& remote, const ComboAddress& local, const struct dnsheader& dh) const
534 {
535 if(d_ipfilter)
536 return d_ipfilter(remote, local, dh);
537 return false; // don't block
538 }
539
540 unsigned int RecursorLua4::gettag(const ComboAddress& remote, const Netmask& ednssubnet, const ComboAddress& local, const DNSName& qname, uint16_t qtype, std::vector<std::string>* policyTags, LuaContext::LuaObject& data, const EDNSOptionViewMap& ednsOptions, bool tcp, std::string& requestorId, std::string& deviceId, std::string& deviceName, const std::vector<ProxyProtocolValue>& proxyProtocolValues) const
541 {
542 if(d_gettag) {
543 std::vector<std::pair<int, const ProxyProtocolValue*>> proxyProtocolValuesMap;
544 proxyProtocolValuesMap.reserve(proxyProtocolValues.size());
545 int num = 1;
546 for (const auto& value : proxyProtocolValues) {
547 proxyProtocolValuesMap.emplace_back(num++, &value);
548 }
549
550 auto ret = d_gettag(remote, ednssubnet, local, qname, qtype, ednsOptions, tcp, proxyProtocolValuesMap);
551
552 if (policyTags) {
553 const auto& tags = std::get<1>(ret);
554 if (tags) {
555 for (const auto& tag : *tags) {
556 policyTags->push_back(tag.second);
557 }
558 }
559 }
560 const auto dataret = std::get<2>(ret);
561 if (dataret) {
562 data = *dataret;
563 }
564 const auto reqIdret = std::get<3>(ret);
565 if (reqIdret) {
566 requestorId = *reqIdret;
567 }
568 const auto deviceIdret = std::get<4>(ret);
569 if (deviceIdret) {
570 deviceId = *deviceIdret;
571 }
572
573 const auto deviceNameret = std::get<5>(ret);
574 if (deviceNameret) {
575 deviceName = *deviceNameret;
576 }
577 return std::get<0>(ret);
578 }
579 return 0;
580 }
581
582 struct pdns_ffi_param
583 {
584 public:
585 pdns_ffi_param(const DNSName& qname_, uint16_t qtype_, const ComboAddress& local_, const ComboAddress& remote_, const Netmask& ednssubnet_, std::vector<std::string>& policyTags_, std::vector<DNSRecord>& records_, const EDNSOptionViewMap& ednsOptions_, const std::vector<ProxyProtocolValue>& proxyProtocolValues_, std::string& requestorId_, std::string& deviceId_, std::string& deviceName_, boost::optional<int>& rcode_, uint32_t& ttlCap_, bool& variable_, bool tcp_, bool& logQuery_, bool& logResponse_, bool& followCNAMERecords_): qname(qname_), local(local_), remote(remote_), ednssubnet(ednssubnet_), policyTags(policyTags_), records(records_), ednsOptions(ednsOptions_), proxyProtocolValues(proxyProtocolValues_), requestorId(requestorId_), deviceId(deviceId_), deviceName(deviceName_), rcode(rcode_), ttlCap(ttlCap_), variable(variable_), logQuery(logQuery_), logResponse(logResponse_), followCNAMERecords(followCNAMERecords_), qtype(qtype_), tcp(tcp_)
586 {
587 }
588
589 std::unique_ptr<std::string> qnameStr{nullptr};
590 std::unique_ptr<std::string> localStr{nullptr};
591 std::unique_ptr<std::string> remoteStr{nullptr};
592 std::unique_ptr<std::string> ednssubnetStr{nullptr};
593 std::vector<pdns_ednsoption_t> ednsOptionsVect;
594 std::vector<pdns_proxyprotocol_value_t> proxyProtocolValuesVect;
595
596 const DNSName& qname;
597 const ComboAddress& local;
598 const ComboAddress& remote;
599 const Netmask& ednssubnet;
600 std::vector<std::string>& policyTags;
601 std::vector<DNSRecord>& records;
602 const EDNSOptionViewMap& ednsOptions;
603 const std::vector<ProxyProtocolValue>& proxyProtocolValues;
604 std::string& requestorId;
605 std::string& deviceId;
606 std::string& deviceName;
607 boost::optional<int>& rcode;
608 uint32_t& ttlCap;
609 bool& variable;
610 bool& logQuery;
611 bool& logResponse;
612 bool& followCNAMERecords;
613
614 unsigned int tag{0};
615 uint16_t qtype;
616 bool tcp;
617 };
618
619 unsigned int RecursorLua4::gettag_ffi(const ComboAddress& remote, const Netmask& ednssubnet, const ComboAddress& local, const DNSName& qname, uint16_t qtype, std::vector<std::string>* policyTags, std::vector<DNSRecord>& records, LuaContext::LuaObject& data, const EDNSOptionViewMap& ednsOptions, bool tcp, const std::vector<ProxyProtocolValue>& proxyProtocolValues, std::string& requestorId, std::string& deviceId, std::string& deviceName, boost::optional<int>& rcode, uint32_t& ttlCap, bool& variable, bool& logQuery, bool& logResponse, bool& followCNAMERecords) const
620 {
621 if (d_gettag_ffi) {
622 pdns_ffi_param_t param(qname, qtype, local, remote, ednssubnet, *policyTags, records, ednsOptions, proxyProtocolValues, requestorId, deviceId, deviceName, rcode, ttlCap, variable, tcp, logQuery, logResponse, followCNAMERecords);
623
624 auto ret = d_gettag_ffi(&param);
625 if (ret) {
626 data = *ret;
627 }
628
629 return param.tag;
630 }
631 return 0;
632 }
633
634 bool RecursorLua4::genhook(const luacall_t& func, DNSQuestion& dq, int& ret) const
635 {
636 if(!func)
637 return false;
638
639 if (dq.currentRecords) {
640 dq.records = *dq.currentRecords;
641 } else {
642 dq.records.clear();
643 }
644
645 dq.followupFunction.clear();
646 dq.followupPrefix.clear();
647 dq.followupName.clear();
648 dq.udpQuery.clear();
649 dq.udpAnswer.clear();
650 dq.udpCallback.clear();
651
652 dq.rcode = ret;
653 bool handled=func(&dq);
654
655 if(handled) {
656 loop:;
657 ret=dq.rcode;
658
659 if(!dq.followupFunction.empty()) {
660 if(dq.followupFunction=="followCNAMERecords") {
661 ret = followCNAMERecords(dq.records, QType(dq.qtype));
662 }
663 else if(dq.followupFunction=="getFakeAAAARecords") {
664 ret=getFakeAAAARecords(dq.followupName, dq.followupPrefix, dq.records);
665 }
666 else if(dq.followupFunction=="getFakePTRRecords") {
667 ret=getFakePTRRecords(dq.followupName, dq.followupPrefix, dq.records);
668 }
669 else if(dq.followupFunction=="udpQueryResponse") {
670 dq.udpAnswer = GenUDPQueryResponse(dq.udpQueryDest, dq.udpQuery);
671 auto cbFunc = d_lw->readVariable<boost::optional<luacall_t>>(dq.udpCallback).get_value_or(0);
672 if(!cbFunc) {
673 g_log<<Logger::Error<<"Attempted callback for Lua UDP Query/Response which could not be found"<<endl;
674 return false;
675 }
676 bool result=cbFunc(&dq);
677 if(!result) {
678 return false;
679 }
680 goto loop;
681 }
682 }
683 if (dq.currentRecords) {
684 *dq.currentRecords = dq.records;
685 }
686 }
687
688 // see if they added followup work for us too
689 return handled;
690 }
691
692 RecursorLua4::~RecursorLua4(){}
693
694 const char* pdns_ffi_param_get_qname(pdns_ffi_param_t* ref)
695 {
696 if (!ref->qnameStr) {
697 ref->qnameStr = std::unique_ptr<std::string>(new std::string(ref->qname.toStringNoDot()));
698 }
699
700 return ref->qnameStr->c_str();
701 }
702
703 void pdns_ffi_param_get_qname_raw(pdns_ffi_param_t* ref, const char** qname, size_t* qnameSize)
704 {
705 const auto& storage = ref->qname.getStorage();
706 *qname = storage.data();
707 *qnameSize = storage.size();
708 }
709
710 uint16_t pdns_ffi_param_get_qtype(const pdns_ffi_param_t* ref)
711 {
712 return ref->qtype;
713 }
714
715 const char* pdns_ffi_param_get_remote(pdns_ffi_param_t* ref)
716 {
717 if (!ref->remoteStr) {
718 ref->remoteStr = std::unique_ptr<std::string>(new std::string(ref->remote.toString()));
719 }
720
721 return ref->remoteStr->c_str();
722 }
723
724 static void pdns_ffi_comboaddress_to_raw(const ComboAddress& ca, const void** addr, size_t* addrSize)
725 {
726 if (ca.isIPv4()) {
727 *addr = &ca.sin4.sin_addr.s_addr;
728 *addrSize = sizeof(ca.sin4.sin_addr.s_addr);
729 }
730 else {
731 *addr = &ca.sin6.sin6_addr.s6_addr;
732 *addrSize = sizeof(ca.sin6.sin6_addr.s6_addr);
733 }
734 }
735
736 void pdns_ffi_param_get_remote_raw(pdns_ffi_param_t* ref, const void** addr, size_t* addrSize)
737 {
738 pdns_ffi_comboaddress_to_raw(ref->remote, addr, addrSize);
739 }
740
741 uint16_t pdns_ffi_param_get_remote_port(const pdns_ffi_param_t* ref)
742 {
743 return ref->remote.getPort();
744 }
745
746 const char* pdns_ffi_param_get_local(pdns_ffi_param_t* ref)
747 {
748 if (!ref->localStr) {
749 ref->localStr = std::unique_ptr<std::string>(new std::string(ref->local.toString()));
750 }
751
752 return ref->localStr->c_str();
753 }
754
755 void pdns_ffi_param_get_local_raw(pdns_ffi_param_t* ref, const void** addr, size_t* addrSize)
756 {
757 pdns_ffi_comboaddress_to_raw(ref->local, addr, addrSize);
758 }
759
760 uint16_t pdns_ffi_param_get_local_port(const pdns_ffi_param_t* ref)
761 {
762 return ref->local.getPort();
763 }
764
765 const char* pdns_ffi_param_get_edns_cs(pdns_ffi_param_t* ref)
766 {
767 if (ref->ednssubnet.empty()) {
768 return nullptr;
769 }
770
771 if (!ref->ednssubnetStr) {
772 ref->ednssubnetStr = std::unique_ptr<std::string>(new std::string(ref->ednssubnet.toStringNoMask()));
773 }
774
775 return ref->ednssubnetStr->c_str();
776 }
777
778 void pdns_ffi_param_get_edns_cs_raw(pdns_ffi_param_t* ref, const void** net, size_t* netSize)
779 {
780 if (ref->ednssubnet.empty()) {
781 *net = nullptr;
782 *netSize = 0;
783 return;
784 }
785
786 pdns_ffi_comboaddress_to_raw(ref->ednssubnet.getNetwork(), net, netSize);
787 }
788
789 uint8_t pdns_ffi_param_get_edns_cs_source_mask(const pdns_ffi_param_t* ref)
790 {
791 return ref->ednssubnet.getBits();
792 }
793
794 static void fill_edns_option(const EDNSOptionViewValue& value, pdns_ednsoption_t& option)
795 {
796 option.len = value.size;
797 option.data = nullptr;
798
799 if (value.size > 0) {
800 option.data = value.content;
801 }
802 }
803
804 size_t pdns_ffi_param_get_edns_options(pdns_ffi_param_t* ref, const pdns_ednsoption_t** out)
805 {
806 if (ref->ednsOptions.empty()) {
807 return 0;
808 }
809
810 size_t totalCount = 0;
811 for (const auto& option : ref->ednsOptions) {
812 totalCount += option.second.values.size();
813 }
814
815 ref->ednsOptionsVect.resize(totalCount);
816
817 size_t pos = 0;
818 for (const auto& option : ref->ednsOptions) {
819 for (const auto& entry : option.second.values) {
820 fill_edns_option(entry, ref->ednsOptionsVect.at(pos));
821 ref->ednsOptionsVect.at(pos).optionCode = option.first;
822 pos++;
823 }
824 }
825
826 *out = ref->ednsOptionsVect.data();
827
828 return totalCount;
829 }
830
831 size_t pdns_ffi_param_get_edns_options_by_code(pdns_ffi_param_t* ref, uint16_t optionCode, const pdns_ednsoption_t** out)
832 {
833 const auto& it = ref->ednsOptions.find(optionCode);
834 if (it == ref->ednsOptions.cend() || it->second.values.empty()) {
835 return 0;
836 }
837
838 ref->ednsOptionsVect.resize(it->second.values.size());
839
840 size_t pos = 0;
841 for (const auto& entry : it->second.values) {
842 fill_edns_option(entry, ref->ednsOptionsVect.at(pos));
843 ref->ednsOptionsVect.at(pos).optionCode = optionCode;
844 pos++;
845 }
846
847 *out = ref->ednsOptionsVect.data();
848
849 return pos;
850 }
851
852 size_t pdns_ffi_param_get_proxy_protocol_values(pdns_ffi_param_t* ref, const pdns_proxyprotocol_value_t** out)
853 {
854 if (ref->proxyProtocolValues.empty()) {
855 return 0;
856 }
857
858 ref->proxyProtocolValuesVect.resize(ref->proxyProtocolValues.size());
859
860 size_t pos = 0;
861 for (const auto& value : ref->proxyProtocolValues) {
862 auto& dest = ref->proxyProtocolValuesVect.at(pos);
863 dest.type = value.type;
864 dest.len = value.content.size();
865 if (dest.len > 0) {
866 dest.data = value.content.data();
867 }
868 pos++;
869 }
870
871 *out = ref->proxyProtocolValuesVect.data();
872
873 return ref->proxyProtocolValuesVect.size();
874 }
875
876 void pdns_ffi_param_set_tag(pdns_ffi_param_t* ref, unsigned int tag)
877 {
878 ref->tag = tag;
879 }
880
881 void pdns_ffi_param_add_policytag(pdns_ffi_param_t *ref, const char* name)
882 {
883 ref->policyTags.push_back(std::string(name));
884 }
885
886 void pdns_ffi_param_set_requestorid(pdns_ffi_param_t* ref, const char* name)
887 {
888 ref->requestorId = std::string(name);
889 }
890
891 void pdns_ffi_param_set_devicename(pdns_ffi_param_t* ref, const char* name)
892 {
893 ref->deviceName = std::string(name);
894 }
895
896 void pdns_ffi_param_set_deviceid(pdns_ffi_param_t* ref, size_t len, const void* name)
897 {
898 ref->deviceId = std::string(reinterpret_cast<const char*>(name), len);
899 }
900
901 void pdns_ffi_param_set_variable(pdns_ffi_param_t* ref, bool variable)
902 {
903 ref->variable = variable;
904 }
905
906 void pdns_ffi_param_set_ttl_cap(pdns_ffi_param_t* ref, uint32_t ttl)
907 {
908 ref->ttlCap = ttl;
909 }
910
911 void pdns_ffi_param_set_log_query(pdns_ffi_param_t* ref, bool logQuery)
912 {
913 ref->logQuery = logQuery;
914 }
915
916 void pdns_ffi_param_set_log_response(pdns_ffi_param_t* ref, bool logResponse)
917 {
918 ref->logResponse = logResponse;
919 }
920
921 void pdns_ffi_param_set_rcode(pdns_ffi_param_t* ref, int rcode)
922 {
923 ref->rcode = rcode;
924 }
925
926 void pdns_ffi_param_set_follow_cname_records(pdns_ffi_param_t* ref, bool follow)
927 {
928 ref->followCNAMERecords = follow;
929 }
930
931 bool pdns_ffi_param_add_record(pdns_ffi_param_t *ref, const char* name, uint16_t type, uint32_t ttl, const char* content, size_t contentSize, pdns_record_place_t place)
932 {
933 try {
934 DNSRecord dr;
935 dr.d_name = name != nullptr ? DNSName(name) : ref->qname;
936 dr.d_ttl = ttl;
937 dr.d_type = type;
938 dr.d_class = QClass::IN;
939 dr.d_place = DNSResourceRecord::Place(place);
940 dr.d_content = DNSRecordContent::mastermake(type, QClass::IN, std::string(content, contentSize));
941 ref->records.push_back(std::move(dr));
942
943 return true;
944 }
945 catch (const std::exception& e) {
946 g_log<<Logger::Error<<"Error attempting to add a record from Lua via pdns_ffi_param_add_record(): "<<e.what()<<endl;
947 return false;
948 }
949 }