• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PowerDNS / pdns / 13317890644

13 Feb 2025 11:32AM UTC coverage: 59.24% (-5.5%) from 64.714%
13317890644

push

github

web-flow
Merge pull request #15149 from rgacogne/ddist-meson-min-version

dnsdist: Bump the required version of `meson` to 1.3

50429 of 138204 branches covered (36.49%)

Branch coverage included in aggregate %.

140112 of 183436 relevant lines covered (76.38%)

6753565.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.86
/modules/lua2backend/lua2api2.hh
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
 * MERCHANTAPILITY 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
#pragma once
23
#include "boost/algorithm/string/join.hpp"
24
#include "pdns/arguments.hh"
25

26
#include "pdns/dnsbackend.hh"
27
#include "pdns/lua-auth4.hh"
28

29
class Lua2BackendAPIv2 : public DNSBackend, AuthLua4
30
{
31
private:
32
  typedef std::function<void()> init_call_t;
33
  typedef std::function<void()> deinit_call_t;
34

35
  typedef std::vector<std::pair<string, string>> lookup_context_t;
36

37
  typedef std::vector<std::pair<int, std::vector<std::pair<string, boost::variant<bool, int, DNSName, string, QType>>>>> lookup_result_t;
38
  typedef std::function<lookup_result_t(const QType& qtype, const DNSName& qname, int domain_id, const lookup_context_t& ctx)> lookup_call_t;
39

40
  typedef boost::variant<bool, lookup_result_t> list_result_t;
41
  typedef std::function<list_result_t(const DNSName& qname, int domain_id)> list_call_t;
42

43
  typedef vector<pair<string, boost::variant<bool, long, string, vector<string>>>> domaininfo_result_t;
44
  typedef boost::variant<bool, domaininfo_result_t> get_domaininfo_result_t;
45
  typedef vector<pair<DNSName, domaininfo_result_t>> get_all_domains_result_t;
46
  typedef std::function<get_domaininfo_result_t(const DNSName& domain)> get_domaininfo_call_t;
47
  typedef std::function<get_all_domains_result_t()> get_all_domains_call_t;
48

49
  typedef vector<pair<int, string>> domain_metadata_result_t;
50
  typedef boost::variant<bool, domain_metadata_result_t> get_domain_metadata_result_t;
51
  typedef boost::variant<bool, vector<pair<string, domain_metadata_result_t>>> get_all_domain_metadata_result_t;
52
  typedef std::function<get_domain_metadata_result_t(const DNSName& domain, const string& kind)> get_domain_metadata_call_t;
53
  typedef std::function<get_all_domain_metadata_result_t(const DNSName& domain)> get_all_domain_metadata_call_t;
54

55
  typedef vector<pair<string, boost::variant<bool, int, string>>> keydata_result_t;
56
  typedef boost::variant<bool, vector<pair<int, keydata_result_t>>> get_domain_keys_result_t;
57
  typedef std::function<get_domain_keys_result_t(const DNSName& domain)> get_domain_keys_call_t;
58

59
  typedef std::vector<std::pair<string, boost::variant<string, DNSName>>> before_and_after_names_result_t;
60
  typedef boost::variant<bool, before_and_after_names_result_t> get_before_and_after_names_absolute_result_t;
61
  typedef std::function<get_before_and_after_names_absolute_result_t(int id, const DNSName& qname)> get_before_and_after_names_absolute_call_t;
62

63
  typedef std::function<void(int, long)> set_notified_call_t;
64

65
  typedef std::function<string(const string& cmd)> direct_backend_cmd_call_t;
66

67
public:
21✔
68
  Lua2BackendAPIv2(const string& suffix)
21✔
69
  {
21✔
70
    d_include_path = ::arg()["lua-global-include-dir"];
21✔
71
    setArgPrefix("lua2" + suffix);
21✔
72
    d_debug_log = mustDo("query-logging");
21✔
73
    prepareContext();
74
    loadFile(getArg("filename"));
75
  }
76

77
  ~Lua2BackendAPIv2() override;
48✔
78

48✔
79
#define logCall(func, var)                                                                               \
80
  {                                                                                                      \
×
81
    if (d_debug_log) {                                                                                   \
48✔
82
      g_log << Logger::Debug << "[" << getPrefix() << "] Calling " << func << "(" << var << ")" << endl; \
83
    }                                                                                                    \
128✔
84
  }
128✔
85
#define logResult(var)                                                \
86
  {                                                                   \
×
87
    if (d_debug_log) {                                                \
×
88
      g_log << Logger::Debug << "[" << getPrefix() << "] Got result " \
128✔
89
            << "'" << var << "'" << endl;                             \
90
    }                                                                 \
91
  }
21✔
92

21✔
93
  void postPrepareContext() override
21✔
94
  {
95
    AuthLua4::postPrepareContext();
96
  }
21✔
97

21✔
98
  void postLoad() override
21✔
99
  {
21✔
100
    f_lookup = d_lw->readVariable<boost::optional<lookup_call_t>>("dns_lookup").get_value_or(0);
21✔
101
    f_list = d_lw->readVariable<boost::optional<list_call_t>>("dns_list").get_value_or(0);
21✔
102
    f_get_all_domains = d_lw->readVariable<boost::optional<get_all_domains_call_t>>("dns_get_all_domains").get_value_or(0);
21✔
103
    f_get_domaininfo = d_lw->readVariable<boost::optional<get_domaininfo_call_t>>("dns_get_domaininfo").get_value_or(0);
21✔
104
    f_get_domain_metadata = d_lw->readVariable<boost::optional<get_domain_metadata_call_t>>("dns_get_domain_metadata").get_value_or(0);
21✔
105
    f_get_all_domain_metadata = d_lw->readVariable<boost::optional<get_all_domain_metadata_call_t>>("dns_get_all_domain_metadata").get_value_or(0);
21✔
106
    f_get_domain_keys = d_lw->readVariable<boost::optional<get_domain_keys_call_t>>("dns_get_domain_keys").get_value_or(0);
107
    f_get_before_and_after_names_absolute = d_lw->readVariable<boost::optional<get_before_and_after_names_absolute_call_t>>("dns_get_before_and_after_names_absolute").get_value_or(0);
21✔
108
    f_set_notified = d_lw->readVariable<boost::optional<set_notified_call_t>>("dns_set_notified").get_value_or(0);
21!
109

110
    auto init = d_lw->readVariable<boost::optional<init_call_t>>("dns_init").get_value_or(0);
111
    if (init)
21!
112
      init();
113

21!
114
    f_deinit = d_lw->readVariable<boost::optional<deinit_call_t>>("dns_deinit").get_value_or(0);
×
115

116
    if (f_lookup == nullptr)
21!
117
      throw PDNSException("dns_lookup missing");
21✔
118

21✔
119
    /* see if dnssec support is wanted */
15!
120
    d_dnssec = d_lw->readVariable<boost::optional<bool>>("dns_dnssec").get_value_or(false);
×
121
    if (d_dnssec) {
15✔
122
      if (f_get_domain_metadata == nullptr)
×
123
        throw PDNSException("dns_dnssec is true but dns_get_domain_metadata is missing");
124
      if (f_get_before_and_after_names_absolute == nullptr)
15!
125
        throw PDNSException("dns_dnssec is true but dns_get_before_and_after_names_absolute is missing");
×
126
      /* domain keys is not strictly speaking necessary for dnssec backend */
15✔
127
      if (f_get_domain_keys == nullptr)
21!
128
        g_log << Logger::Warning << "dns_get_domain_keys missing - cannot do live signing" << endl;
129
    }
130
  }
×
131

132
  bool doesDNSSEC() override
133
  {
134
    return d_dnssec;
135
  }
29✔
136

103✔
137
  void parseLookup(const lookup_result_t& result)
103✔
138
  {
437✔
139
    for (const auto& row : result) {
437✔
140
      DNSResourceRecord rec;
103!
141
      for (const auto& item : row.second) {
✔
142
        if (item.first == "type") {
103✔
143
          if (item.second.which() == 1)
×
144
            rec.qtype = QType(boost::get<int>(item.second));
103!
145
          else if (item.second.which() == 3)
103!
146
            rec.qtype = boost::get<string>(item.second);
×
147
          else if (item.second.which() == 4)
×
148
            rec.qtype = boost::get<QType>(item.second);
103✔
149
          else
334✔
150
            throw PDNSException("Unsupported value for type");
103!
151
        }
×
152
        else if (item.first == "name") {
103✔
153
          if (item.second.which() == 3)
103!
154
            rec.qname = DNSName(boost::get<string>(item.second));
×
155
          else if (item.second.which() == 2)
×
156
            rec.qname = boost::get<DNSName>(item.second);
103✔
157
          else
231✔
158
            throw PDNSException("Unsupported value for name");
25✔
159
        }
206!
160
        else if (item.first == "domain_id")
✔
161
          rec.domain_id = boost::get<int>(item.second);
206!
162
        else if (item.first == "auth")
×
163
          rec.auth = boost::get<bool>(item.second);
206✔
164
        else if (item.first == "last_modified")
103!
165
          rec.last_modified = static_cast<time_t>(boost::get<int>(item.second));
103!
166
        else if (item.first == "ttl")
103✔
167
          rec.ttl = boost::get<int>(item.second);
×
168
        else if (item.first == "content")
×
169
          rec.setContent(boost::get<string>(item.second));
×
170
        else if (item.first == "scopeMask")
×
171
          rec.scopeMask = boost::get<int>(item.second);
437✔
172
        else
103!
173
          g_log << Logger::Warning << "Unsupported key '" << item.first << "' in lookup or list result" << endl;
103✔
174
      }
103✔
175
      logResult(rec.qname << " IN " << rec.qtype.toString() << " " << rec.ttl << " " << rec.getZoneRepresentation());
29!
176
      d_result.push_back(rec);
×
177
    }
29✔
178
    if (d_result.empty() && d_debug_log)
33!
179
      g_log << Logger::Debug << "[" << getPrefix() << "] Got empty result" << endl;
180
  }
6✔
181

6!
182
  bool list(const DNSName& target, int domain_id, bool /* include_disabled */ = false) override
183
  {
×
184
    if (f_list == nullptr) {
×
185
      g_log << Logger::Error << "[" << getPrefix() << "] dns_list missing - cannot do AXFR" << endl;
186
      return false;
6!
187
    }
×
188

189
    if (d_result.size() != 0)
6!
190
      throw PDNSException("list attempted while another was running");
6✔
191

192
    logCall("list", "target=" << target << ",domain_id=" << domain_id);
6!
193
    list_result_t result = f_list(target, domain_id);
×
194

195
    if (result.which() == 0)
6!
196
      return false;
197

6✔
198
    parseLookup(boost::get<lookup_result_t>(result));
6✔
199

200
    return true;
201
  }
23✔
202

23!
203
  void lookup(const QType& qtype, const DNSName& qname, int domain_id, DNSPacket* p = nullptr) override
204
  {
205
    if (d_result.size() != 0)
23!
206
      throw PDNSException("lookup attempted while another was running");
23✔
207

5✔
208
    lookup_context_t ctx;
5✔
209
    if (p != NULL) {
5✔
210
      ctx.emplace_back(lookup_context_t::value_type{"source_address", p->getInnerRemote().toString()});
211
      ctx.emplace_back(lookup_context_t::value_type{"real_source_address", p->getRealRemote().toString()});
23!
212
    }
23✔
213

23✔
214
    logCall("lookup", "qtype=" << qtype.toString() << ",qname=" << qname << ",domain_id=" << domain_id);
23!
215
    lookup_result_t result = f_lookup(qtype, qname, domain_id, ctx);
216
    parseLookup(result);
217
  }
132✔
218

132✔
219
  bool get(DNSResourceRecord& rr) override
29✔
220
  {
103✔
221
    if (d_result.size() == 0)
103✔
222
      return false;
103✔
223
    rr = std::move(d_result.front());
132✔
224
    d_result.pop_front();
225
    return true;
226
  }
×
227

228
  string directBackendCmd(const string& querystr) override
229
  {
×
230
    string::size_type pos = querystr.find_first_of(" \t");
×
231
    string cmd = querystr;
×
232
    string par = "";
×
233
    if (pos != string::npos) {
×
234
      cmd = querystr.substr(0, pos);
×
235
      par = querystr.substr(pos + 1);
×
236
    }
×
237
    direct_backend_cmd_call_t f = d_lw->readVariable<boost::optional<direct_backend_cmd_call_t>>(cmd).get_value_or(0);
×
238
    if (f == nullptr) {
×
239
      return cmd + "not found";
×
240
    }
×
241
    logCall(cmd, "parameter=" << par);
×
242
    return f(par);
243
  }
×
244

×
245
  void setNotified(uint32_t id, uint32_t serial) override
246
  {
×
247
    if (f_set_notified == NULL)
×
248
      return;
×
249
    logCall("dns_set_notified", "id=" << static_cast<int>(id) << ",serial=" << serial);
×
250
    f_set_notified(static_cast<int>(id), serial);
251
  }
5✔
252

10✔
253
  void parseDomainInfo(const domaininfo_result_t& row, DomainInfo& di)
10!
254
  {
×
255
    for (const auto& item : row) {
10✔
256
      if (item.first == "account")
×
257
        di.account = boost::get<string>(item.second);
10!
258
      else if (item.first == "last_check")
×
259
        di.last_check = static_cast<time_t>(boost::get<long>(item.second));
×
260
      else if (item.first == "masters")
10✔
261
        for (const auto& primary : boost::get<vector<string>>(item.second))
5!
262
          di.primaries.push_back(ComboAddress(primary, 53));
5!
263
      else if (item.first == "id")
✔
264
        di.id = static_cast<int>(boost::get<long>(item.second));
5!
265
      else if (item.first == "notified_serial")
5!
266
        di.notified_serial = static_cast<unsigned int>(boost::get<long>(item.second));
×
267
      else if (item.first == "serial")
×
268
        di.serial = static_cast<unsigned int>(boost::get<long>(item.second));
×
269
      else if (item.first == "kind")
×
270
        di.kind = DomainInfo::stringToKind(boost::get<string>(item.second));
10✔
271
      else
5✔
272
        g_log << Logger::Warning << "Unsupported key '" << item.first << "' in domaininfo result" << endl;
5!
273
    }
5✔
274
    di.backend = this;
275
    logResult("zone=" << di.zone << ",serial=" << di.serial << ",kind=" << di.getKindString());
5!
276
  }
6✔
277

6✔
278
  bool getDomainInfo(const DNSName& domain, DomainInfo& di, bool /* getSerial */ = true) override
279
  {
3✔
280
    if (f_get_domaininfo == nullptr) {
3✔
281
      // use getAuth instead
282
      SOAData sd;
283
      if (!getAuth(domain, &sd))
3!
284
        return false;
3✔
285

3✔
286
      di.zone = domain;
3✔
287
      di.backend = this;
3✔
288
      di.serial = sd.serial;
289
      return true;
3!
290
    }
3✔
291

292
    logCall("get_domaininfo", "domain=" << domain);
3!
293
    get_domaininfo_result_t result = f_get_domaininfo(domain);
×
294

295
    if (result.which() == 0)
3!
296
      return false;
3✔
297

298
    di.zone = domain;
3✔
299
    parseDomainInfo(boost::get<domaininfo_result_t>(result), di);
3✔
300

301
    return true;
302
  }
1✔
303

1!
304
  void getAllDomains(vector<DomainInfo>* domains, bool /* getSerial */, bool /* include_disabled */) override
305
  {
306
    if (f_get_all_domains == nullptr)
1!
307
      return;
2✔
308

2✔
309
    logCall("get_all_domains", "");
2!
310
    for (const auto& row : f_get_all_domains()) {
2✔
311
      DomainInfo di;
2✔
312
      di.zone = row.first;
2✔
313
      logResult(di.zone);
2!
314
      parseDomainInfo(row.second, di);
1✔
315
      domains->push_back(di);
316
    }
317
  }
6✔
318

6!
319
  bool getAllDomainMetadata(const DNSName& name, std::map<std::string, std::vector<std::string>>& meta) override
6✔
320
  {
321
    if (f_get_all_domain_metadata == nullptr)
×
322
      return false;
×
323

×
324
    logCall("get_all_domain_metadata", "name=" << name);
×
325
    get_all_domain_metadata_result_t result = f_get_all_domain_metadata(name);
326
    if (result.which() == 0)
×
327
      return false;
×
328

×
329
    for (const auto& row : boost::get<vector<pair<string, domain_metadata_result_t>>>(result)) {
×
330
      meta[row.first].clear();
×
331
      for (const auto& item : row.second)
×
332
        meta[row.first].push_back(item.second);
333
      logResult("kind=" << row.first << ",value=" << boost::algorithm::join(meta[row.first], ", "));
×
334
    }
×
335

336
    return true;
337
  }
×
338

×
339
  bool getDomainMetadata(const DNSName& name, const std::string& kind, std::vector<std::string>& meta) override
340
  {
341
    if (f_get_domain_metadata == nullptr)
×
342
      return false;
×
343

×
344
    logCall("get_domain_metadata", "name=" << name << ",kind=" << kind);
×
345
    get_domain_metadata_result_t result = f_get_domain_metadata(name, kind);
346
    if (result.which() == 0)
×
347
      return false;
×
348

349
    meta.clear();
350
    for (const auto& item : boost::get<domain_metadata_result_t>(result))
×
351
      meta.push_back(item.second);
×
352

353
    logResult("value=" << boost::algorithm::join(meta, ", "));
×
354
    return true;
355
  }
6✔
356

6✔
357
  bool getDomainKeys(const DNSName& name, std::vector<DNSBackend::KeyData>& keys) override
3✔
358
  {
359
    if (f_get_domain_keys == nullptr)
3✔
360
      return false;
3✔
361

362
    logCall("get_domain_keys", "name=" << name);
3!
363
    get_domain_keys_result_t result = f_get_domain_keys(name);
×
364

365
    if (result.which() == 0)
6✔
366
      return false;
6✔
367

6✔
368
    for (const auto& row : boost::get<vector<pair<int, keydata_result_t>>>(result)) {
24✔
369
      DNSBackend::KeyData key;
24✔
370
      key.published = true;
6✔
371
      for (const auto& item : row.second) {
18✔
372
        if (item.first == "content")
6✔
373
          key.content = boost::get<string>(item.second);
12✔
374
        else if (item.first == "id")
6✔
375
          key.id = static_cast<unsigned int>(boost::get<int>(item.second));
6!
376
        else if (item.first == "flags")
6✔
377
          key.flags = static_cast<unsigned int>(boost::get<int>(item.second));
×
378
        else if (item.first == "active")
×
379
          key.active = boost::get<bool>(item.second);
×
380
        else if (item.first == "published")
×
381
          key.published = boost::get<bool>(item.second);
24✔
382
        else
6!
383
          g_log << Logger::Warning << "[" << getPrefix() << "] Unsupported key '" << item.first << "' in keydata result" << endl;
6✔
384
      }
6✔
385
      logResult("id=" << key.id << ",flags=" << key.flags << ",active=" << (key.active ? "true" : "false") << ",published=" << (key.published ? "true" : "false"));
6!
386
      keys.push_back(key);
3✔
387
    }
3✔
388

389
    return true;
390
  }
12✔
391

12!
392
  bool getBeforeAndAfterNamesAbsolute(uint32_t id, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after) override
393
  {
394
    if (f_get_before_and_after_names_absolute == nullptr)
12!
395
      return false;
12✔
396

397
    logCall("get_before_and_after_names_absolute", "id=<<" << id << ",qname=" << qname);
12!
398
    get_before_and_after_names_absolute_result_t result = f_get_before_and_after_names_absolute(id, qname);
×
399

400
    if (result.which() == 0)
12!
401
      return false;
12!
402

403
    before_and_after_names_result_t row = boost::get<before_and_after_names_result_t>(result);
×
404
    if (row.size() != 3) {
×
405
      g_log << Logger::Error << "Invalid result from dns_get_before_and_after_names_absolute, expected array with 3 items, got " << row.size() << "item(s)" << endl;
36✔
406
      return false;
36✔
407
    }
36!
408
    for (const auto& item : row) {
✔
409
      DNSName value;
36✔
410
      if (item.second.which() == 0)
36!
411
        value = DNSName(boost::get<string>(item.second));
36✔
412
      else
12✔
413
        value = DNSName(boost::get<DNSName>(item.second));
24✔
414
      if (item.first == "unhashed")
12✔
415
        unhashed = value;
12!
416
      else if (item.first == "before")
12✔
417
        before = value;
×
418
      else if (item.first == "after")
×
419
        after = value;
×
420
      else {
×
421
        g_log << Logger::Error << "Invalid result from dns_get_before_and_after_names_absolute, unexpected key " << item.first << endl;
36✔
422
        return false;
423
      }
12!
424
    }
12✔
425

12✔
426
    logResult("unhashed=" << unhashed << ",before=" << before << ",after=" << after);
12!
427
    return true;
428
  }
429

430
private:
431
  std::list<DNSResourceRecord> d_result;
432
  bool d_debug_log{false};
433
  bool d_dnssec{false};
434

435
  lookup_call_t f_lookup;
436
  list_call_t f_list;
437

438
  get_domaininfo_call_t f_get_domaininfo;
439
  get_all_domains_call_t f_get_all_domains;
440

441
  get_domain_metadata_call_t f_get_domain_metadata;
442
  get_all_domain_metadata_call_t f_get_all_domain_metadata;
443

444
  get_domain_keys_call_t f_get_domain_keys;
445

446
  get_before_and_after_names_absolute_call_t f_get_before_and_after_names_absolute;
447

448
  set_notified_call_t f_set_notified;
449

450
  deinit_call_t f_deinit;
451
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc