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

PowerDNS / pdns / 17757454770

16 Sep 2025 07:00AM UTC coverage: 65.89% (-0.2%) from 66.052%
17757454770

push

github

web-flow
Merge pull request #15613 from rgacogne/dco

Require Developer Certificate of Origin in pull requests

42249 of 92974 branches covered (45.44%)

Branch coverage included in aggregate %.

128711 of 166490 relevant lines covered (77.31%)

6588530.27 hits per line

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

0.0
/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, domainid_t 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, domainid_t 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(domainid_t id, const DNSName& qname)> get_before_and_after_names_absolute_call_t;
62

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

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

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

77
  ~Lua2BackendAPIv2() override;
78

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

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

98
  void postLoad() override
99
  {
×
100
    f_lookup = d_lw->readVariable<boost::optional<lookup_call_t>>("dns_lookup").get_value_or(0);
×
101
    f_list = d_lw->readVariable<boost::optional<list_call_t>>("dns_list").get_value_or(0);
×
102
    f_get_all_domains = d_lw->readVariable<boost::optional<get_all_domains_call_t>>("dns_get_all_domains").get_value_or(0);
×
103
    f_get_domaininfo = d_lw->readVariable<boost::optional<get_domaininfo_call_t>>("dns_get_domaininfo").get_value_or(0);
×
104
    f_get_domain_metadata = d_lw->readVariable<boost::optional<get_domain_metadata_call_t>>("dns_get_domain_metadata").get_value_or(0);
×
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);
×
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);
×
108
    f_set_notified = d_lw->readVariable<boost::optional<set_notified_call_t>>("dns_set_notified").get_value_or(0);
×
109

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

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

116
    if (f_lookup == nullptr)
×
117
      throw PDNSException("dns_lookup missing");
×
118

119
    /* see if dnssec support is wanted */
120
    d_dnssec = d_lw->readVariable<boost::optional<bool>>("dns_dnssec").get_value_or(false);
×
121
    if (d_dnssec) {
×
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)
×
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 */
127
      if (f_get_domain_keys == nullptr)
×
128
        g_log << Logger::Warning << "dns_get_domain_keys missing - cannot do live signing" << endl;
×
129
    }
×
130
  }
×
131

132
  unsigned int getCapabilities() override
133
  {
×
134
    unsigned int caps = CAP_DIRECT | CAP_LIST;
×
135
    if (d_dnssec) {
×
136
      caps |= CAP_DNSSEC;
×
137
    }
×
138
    if (f_get_all_domains != nullptr) {
×
139
      caps |= CAP_SEARCH;
×
140
    }
×
141
    return caps;
×
142
  }
×
143

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

189
  bool list(const ZoneName& target, domainid_t domain_id, bool /* include_disabled */ = false) override
190
  {
×
191
    if (f_list == nullptr) {
×
192
      g_log << Logger::Error << "[" << getPrefix() << "] dns_list missing - cannot do AXFR" << endl;
×
193
      return false;
×
194
    }
×
195

196
    if (d_result.size() != 0)
×
197
      throw PDNSException("list attempted while another was running");
×
198

199
    logCall("list", "target=" << target << ",domain_id=" << domain_id);
×
200
    list_result_t result = f_list(target.operator const DNSName&(), domain_id);
×
201

202
    if (result.which() == 0)
×
203
      return false;
×
204

205
    parseLookup(boost::get<lookup_result_t>(result));
×
206

207
    return true;
×
208
  }
×
209

210
  void lookup(const QType& qtype, const DNSName& qname, domainid_t domain_id, DNSPacket* p = nullptr) override
211
  {
×
212
    if (d_result.size() != 0)
×
213
      throw PDNSException("lookup attempted while another was running");
×
214

215
    lookup_context_t ctx;
×
216
    if (p != NULL) {
×
217
      ctx.emplace_back(lookup_context_t::value_type{"source_address", p->getInnerRemote().toString()});
×
218
      ctx.emplace_back(lookup_context_t::value_type{"real_source_address", p->getRealRemote().toString()});
×
219
    }
×
220

221
    logCall("lookup", "qtype=" << qtype.toString() << ",qname=" << qname << ",domain_id=" << domain_id);
×
222
    lookup_result_t result = f_lookup(qtype, qname, domain_id, ctx);
×
223
    parseLookup(result);
×
224
  }
×
225

226
  bool get(DNSResourceRecord& rr) override
227
  {
×
228
    if (d_result.size() == 0)
×
229
      return false;
×
230
    rr = std::move(d_result.front());
×
231
    d_result.pop_front();
×
232
    return true;
×
233
  }
×
234

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

252
  void setNotified(domainid_t id, uint32_t serial) override
253
  {
×
254
    if (f_set_notified == NULL)
×
255
      return;
×
256
    logCall("dns_set_notified", "id=" << id << ",serial=" << serial);
×
257
    f_set_notified(id, serial);
×
258
  }
×
259

260
  void parseDomainInfo(const domaininfo_result_t& row, DomainInfo& di)
261
  {
×
262
    di.id = UnknownDomainID;
×
263
    for (const auto& item : row) {
×
264
      if (item.first == "account")
×
265
        di.account = boost::get<string>(item.second);
×
266
      else if (item.first == "last_check")
×
267
        di.last_check = static_cast<time_t>(boost::get<long>(item.second));
×
268
      else if (item.first == "masters")
×
269
        for (const auto& primary : boost::get<vector<string>>(item.second))
×
270
          di.primaries.push_back(ComboAddress(primary, 53));
×
271
      else if (item.first == "id")
×
272
        di.id = static_cast<domainid_t>(boost::get<long>(item.second));
×
273
      else if (item.first == "notified_serial")
×
274
        di.notified_serial = static_cast<unsigned int>(boost::get<long>(item.second));
×
275
      else if (item.first == "serial")
×
276
        di.serial = static_cast<unsigned int>(boost::get<long>(item.second));
×
277
      else if (item.first == "kind")
×
278
        di.kind = DomainInfo::stringToKind(boost::get<string>(item.second));
×
279
      else
×
280
        g_log << Logger::Warning << "Unsupported key '" << item.first << "' in domaininfo result" << endl;
×
281
    }
×
282
    di.backend = this;
×
283
    logResult("zone=" << di.zone << ",serial=" << di.serial << ",kind=" << di.getKindString());
×
284
  }
×
285

286
  bool getDomainInfo(const ZoneName& domain, DomainInfo& di, bool /* getSerial */ = true) override
287
  {
×
288
    if (f_get_domaininfo == nullptr) {
×
289
      // use getAuth instead... but getAuth wraps getSOA which will call
290
      // getDomainInfo if this is a domain variant, so protect against this
291
      // would-be infinite recursion.
292
      if (domain.hasVariant()) {
×
293
        g_log << Logger::Info << "Unable to return domain information for '" << domain.toLogString() << "' due to unimplemented dns_get_domaininfo" << endl;
×
294
        return false;
×
295
      }
×
296
      SOAData sd;
×
297
      if (!getAuth(domain, &sd))
×
298
        return false;
×
299

300
      di.id = sd.domain_id;
×
301
      di.zone = domain;
×
302
      di.backend = this;
×
303
      di.serial = sd.serial;
×
304
      return true;
×
305
    }
×
306

307
    logCall("get_domaininfo", "domain=" << domain);
×
308
    get_domaininfo_result_t result = f_get_domaininfo(domain.operator const DNSName&());
×
309

310
    if (result.which() == 0)
×
311
      return false;
×
312

313
    di.zone = domain;
×
314
    parseDomainInfo(boost::get<domaininfo_result_t>(result), di);
×
315

316
    return true;
×
317
  }
×
318

319
  void getAllDomains(vector<DomainInfo>* domains, bool /* getSerial */, bool /* include_disabled */) override
320
  {
×
321
    if (f_get_all_domains == nullptr)
×
322
      return;
×
323

324
    logCall("get_all_domains", "");
×
325
    for (const auto& row : f_get_all_domains()) {
×
326
      DomainInfo di;
×
327
      di.zone = ZoneName(row.first);
×
328
      logResult(di.zone);
×
329
      parseDomainInfo(row.second, di);
×
330
      domains->push_back(di);
×
331
    }
×
332
  }
×
333

334
  bool getAllDomainMetadata(const ZoneName& name, std::map<std::string, std::vector<std::string>>& meta) override
335
  {
×
336
    if (f_get_all_domain_metadata == nullptr)
×
337
      return false;
×
338

339
    logCall("get_all_domain_metadata", "name=" << name);
×
340
    get_all_domain_metadata_result_t result = f_get_all_domain_metadata(name.operator const DNSName&());
×
341
    if (result.which() == 0)
×
342
      return false;
×
343

344
    for (const auto& row : boost::get<vector<pair<string, domain_metadata_result_t>>>(result)) {
×
345
      meta[row.first].clear();
×
346
      for (const auto& item : row.second)
×
347
        meta[row.first].push_back(item.second);
×
348
      logResult("kind=" << row.first << ",value=" << boost::algorithm::join(meta[row.first], ", "));
×
349
    }
×
350

351
    return true;
×
352
  }
×
353

354
  bool getDomainMetadata(const ZoneName& name, const std::string& kind, std::vector<std::string>& meta) override
355
  {
×
356
    if (f_get_domain_metadata == nullptr)
×
357
      return false;
×
358

359
    logCall("get_domain_metadata", "name=" << name << ",kind=" << kind);
×
360
    get_domain_metadata_result_t result = f_get_domain_metadata(name.operator const DNSName&(), kind);
×
361
    if (result.which() == 0)
×
362
      return false;
×
363

364
    meta.clear();
×
365
    for (const auto& item : boost::get<domain_metadata_result_t>(result))
×
366
      meta.push_back(item.second);
×
367

368
    logResult("value=" << boost::algorithm::join(meta, ", "));
×
369
    return true;
×
370
  }
×
371

372
  bool getDomainKeys(const ZoneName& name, std::vector<DNSBackend::KeyData>& keys) override
373
  {
×
374
    if (f_get_domain_keys == nullptr)
×
375
      return false;
×
376

377
    logCall("get_domain_keys", "name=" << name);
×
378
    get_domain_keys_result_t result = f_get_domain_keys(name.operator const DNSName&());
×
379

380
    if (result.which() == 0)
×
381
      return false;
×
382

383
    for (const auto& row : boost::get<vector<pair<int, keydata_result_t>>>(result)) {
×
384
      DNSBackend::KeyData key;
×
385
      key.published = true;
×
386
      for (const auto& item : row.second) {
×
387
        if (item.first == "content")
×
388
          key.content = boost::get<string>(item.second);
×
389
        else if (item.first == "id")
×
390
          key.id = static_cast<unsigned int>(boost::get<int>(item.second));
×
391
        else if (item.first == "flags")
×
392
          key.flags = static_cast<unsigned int>(boost::get<int>(item.second));
×
393
        else if (item.first == "active")
×
394
          key.active = boost::get<bool>(item.second);
×
395
        else if (item.first == "published")
×
396
          key.published = boost::get<bool>(item.second);
×
397
        else
×
398
          g_log << Logger::Warning << "[" << getPrefix() << "] Unsupported key '" << item.first << "' in keydata result" << endl;
×
399
      }
×
400
      logResult("id=" << key.id << ",flags=" << key.flags << ",active=" << (key.active ? "true" : "false") << ",published=" << (key.published ? "true" : "false"));
×
401
      keys.emplace_back(std::move(key));
×
402
    }
×
403

404
    return true;
×
405
  }
×
406

407
  bool getBeforeAndAfterNamesAbsolute(domainid_t id, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after) override
408
  {
×
409
    if (f_get_before_and_after_names_absolute == nullptr)
×
410
      return false;
×
411

412
    logCall("get_before_and_after_names_absolute", "id=<<" << id << ",qname=" << qname);
×
413
    get_before_and_after_names_absolute_result_t result = f_get_before_and_after_names_absolute(id, qname);
×
414

415
    if (result.which() == 0)
×
416
      return false;
×
417

418
    before_and_after_names_result_t row = boost::get<before_and_after_names_result_t>(result);
×
419
    if (row.size() != 3) {
×
420
      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;
×
421
      return false;
×
422
    }
×
423
    for (const auto& item : row) {
×
424
      DNSName value;
×
425
      if (item.second.which() == 0)
×
426
        value = DNSName(boost::get<string>(item.second));
×
427
      else
×
428
        value = DNSName(boost::get<DNSName>(item.second));
×
429
      if (item.first == "unhashed")
×
430
        unhashed = value;
×
431
      else if (item.first == "before")
×
432
        before = value;
×
433
      else if (item.first == "after")
×
434
        after = value;
×
435
      else {
×
436
        g_log << Logger::Error << "Invalid result from dns_get_before_and_after_names_absolute, unexpected key " << item.first << endl;
×
437
        return false;
×
438
      }
×
439
    }
×
440

441
    logResult("unhashed=" << unhashed << ",before=" << before << ",after=" << after);
×
442
    return true;
×
443
  }
×
444

445
private:
446
  std::list<DNSResourceRecord> d_result;
447
  bool d_debug_log{false};
448
  bool d_dnssec{false};
449

450
  lookup_call_t f_lookup;
451
  list_call_t f_list;
452

453
  get_domaininfo_call_t f_get_domaininfo;
454
  get_all_domains_call_t f_get_all_domains;
455

456
  get_domain_metadata_call_t f_get_domain_metadata;
457
  get_all_domain_metadata_call_t f_get_all_domain_metadata;
458

459
  get_domain_keys_call_t f_get_domain_keys;
460

461
  get_before_and_after_names_absolute_call_t f_get_before_and_after_names_absolute;
462

463
  set_notified_call_t f_set_notified;
464

465
  deinit_call_t f_deinit;
466
};
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