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

PowerDNS / pdns / 14177253744

31 Mar 2025 05:09PM UTC coverage: 63.457% (+0.002%) from 63.455%
14177253744

Pull #15382

github

web-flow
Merge f0b7d8319 into 9c613b278
Pull Request #15382: [auth] Backend capabilities

41624 of 100246 branches covered (41.52%)

Branch coverage included in aggregate %.

16 of 32 new or added lines in 11 files covered. (50.0%)

41 existing lines in 11 files now uncovered.

128481 of 167816 relevant lines covered (76.56%)

4331167.27 hits per line

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

57.81
/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:
68
  Lua2BackendAPIv2(const string& suffix)
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();
21✔
74
    loadFile(getArg("filename"));
21✔
75
  }
21✔
76

77
  ~Lua2BackendAPIv2() override;
78

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

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

98
  void postLoad() override
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);
21✔
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);
21✔
111
    if (init)
21!
112
      init();
×
113

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

116
    if (f_lookup == nullptr)
21!
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);
21✔
121
    if (d_dnssec) {
21✔
122
      if (f_get_domain_metadata == nullptr)
14!
123
        throw PDNSException("dns_dnssec is true but dns_get_domain_metadata is missing");
×
124
      if (f_get_before_and_after_names_absolute == nullptr)
14!
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)
14!
128
        g_log << Logger::Warning << "dns_get_domain_keys missing - cannot do live signing" << endl;
×
129
    }
14✔
130
  }
21✔
131

132
  unsigned int getCapabilities() override
133
  {
×
NEW
134
    if (d_dnssec) {
×
NEW
135
      return CAP_DNSSEC | CAP_DIRECT | CAP_LIST;
×
NEW
136
    }
×
NEW
137
    else {
×
NEW
138
      return CAP_DIRECT | CAP_LIST;
×
NEW
139
    }
×
UNCOV
140
  }
×
141

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

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

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

197
    logCall("list", "target=" << target << ",domain_id=" << domain_id);
6!
198
    list_result_t result = f_list(target, domain_id);
6✔
199

200
    if (result.which() == 0)
6!
201
      return false;
×
202

203
    parseLookup(boost::get<lookup_result_t>(result));
6✔
204

205
    return true;
6✔
206
  }
6✔
207

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

213
    lookup_context_t ctx;
23✔
214
    if (p != NULL) {
23✔
215
      ctx.emplace_back(lookup_context_t::value_type{"source_address", p->getInnerRemote().toString()});
5✔
216
      ctx.emplace_back(lookup_context_t::value_type{"real_source_address", p->getRealRemote().toString()});
5✔
217
    }
5✔
218

219
    logCall("lookup", "qtype=" << qtype.toString() << ",qname=" << qname << ",domain_id=" << domain_id);
23!
220
    lookup_result_t result = f_lookup(qtype, qname, domain_id, ctx);
23✔
221
    parseLookup(result);
23✔
222
  }
23✔
223

224
  bool get(DNSResourceRecord& rr) override
225
  {
132✔
226
    if (d_result.size() == 0)
132✔
227
      return false;
29✔
228
    rr = std::move(d_result.front());
103✔
229
    d_result.pop_front();
103✔
230
    return true;
103✔
231
  }
132✔
232

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

250
  void setNotified(uint32_t id, uint32_t serial) override
251
  {
×
252
    if (f_set_notified == NULL)
×
253
      return;
×
254
    logCall("dns_set_notified", "id=" << static_cast<int>(id) << ",serial=" << serial);
×
255
    f_set_notified(static_cast<int>(id), serial);
×
256
  }
×
257

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

283
  bool getDomainInfo(const DNSName& domain, DomainInfo& di, bool /* getSerial */ = true) override
284
  {
6✔
285
    if (f_get_domaininfo == nullptr) {
6✔
286
      // use getAuth instead
287
      SOAData sd;
3✔
288
      if (!getAuth(domain, &sd))
3!
289
        return false;
×
290

291
      di.zone = domain;
3✔
292
      di.backend = this;
3✔
293
      di.serial = sd.serial;
3✔
294
      return true;
3✔
295
    }
3✔
296

297
    logCall("get_domaininfo", "domain=" << domain);
3!
298
    get_domaininfo_result_t result = f_get_domaininfo(domain);
3✔
299

300
    if (result.which() == 0)
3!
301
      return false;
×
302

303
    di.zone = domain;
3✔
304
    parseDomainInfo(boost::get<domaininfo_result_t>(result), di);
3✔
305

306
    return true;
3✔
307
  }
3✔
308

309
  void getAllDomains(vector<DomainInfo>* domains, bool /* getSerial */, bool /* include_disabled */) override
310
  {
1✔
311
    if (f_get_all_domains == nullptr)
1!
312
      return;
×
313

314
    logCall("get_all_domains", "");
1!
315
    for (const auto& row : f_get_all_domains()) {
2✔
316
      DomainInfo di;
2✔
317
      di.zone = row.first;
2✔
318
      logResult(di.zone);
2!
319
      parseDomainInfo(row.second, di);
2✔
320
      domains->push_back(di);
2✔
321
    }
2✔
322
  }
1✔
323

324
  bool getAllDomainMetadata(const DNSName& name, std::map<std::string, std::vector<std::string>>& meta) override
325
  {
6✔
326
    if (f_get_all_domain_metadata == nullptr)
6!
327
      return false;
6✔
328

329
    logCall("get_all_domain_metadata", "name=" << name);
×
330
    get_all_domain_metadata_result_t result = f_get_all_domain_metadata(name);
×
331
    if (result.which() == 0)
×
332
      return false;
×
333

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

341
    return true;
×
342
  }
×
343

344
  bool getDomainMetadata(const DNSName& name, const std::string& kind, std::vector<std::string>& meta) override
345
  {
×
346
    if (f_get_domain_metadata == nullptr)
×
347
      return false;
×
348

349
    logCall("get_domain_metadata", "name=" << name << ",kind=" << kind);
×
350
    get_domain_metadata_result_t result = f_get_domain_metadata(name, kind);
×
351
    if (result.which() == 0)
×
352
      return false;
×
353

354
    meta.clear();
×
355
    for (const auto& item : boost::get<domain_metadata_result_t>(result))
×
356
      meta.push_back(item.second);
×
357

358
    logResult("value=" << boost::algorithm::join(meta, ", "));
×
359
    return true;
×
360
  }
×
361

362
  bool getDomainKeys(const DNSName& name, std::vector<DNSBackend::KeyData>& keys) override
363
  {
6✔
364
    if (f_get_domain_keys == nullptr)
6✔
365
      return false;
3✔
366

367
    logCall("get_domain_keys", "name=" << name);
3!
368
    get_domain_keys_result_t result = f_get_domain_keys(name);
3✔
369

370
    if (result.which() == 0)
3!
371
      return false;
×
372

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

394
    return true;
3✔
395
  }
3✔
396

397
  bool getBeforeAndAfterNamesAbsolute(uint32_t id, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after) override
398
  {
12✔
399
    if (f_get_before_and_after_names_absolute == nullptr)
12!
400
      return false;
×
401

402
    logCall("get_before_and_after_names_absolute", "id=<<" << id << ",qname=" << qname);
12!
403
    get_before_and_after_names_absolute_result_t result = f_get_before_and_after_names_absolute(id, qname);
12✔
404

405
    if (result.which() == 0)
12!
406
      return false;
×
407

408
    before_and_after_names_result_t row = boost::get<before_and_after_names_result_t>(result);
12✔
409
    if (row.size() != 3) {
12!
410
      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;
×
411
      return false;
×
412
    }
×
413
    for (const auto& item : row) {
36✔
414
      DNSName value;
36✔
415
      if (item.second.which() == 0)
36!
416
        value = DNSName(boost::get<string>(item.second));
×
417
      else
36✔
418
        value = DNSName(boost::get<DNSName>(item.second));
36✔
419
      if (item.first == "unhashed")
36✔
420
        unhashed = value;
12✔
421
      else if (item.first == "before")
24✔
422
        before = value;
12✔
423
      else if (item.first == "after")
12!
424
        after = value;
12✔
425
      else {
×
426
        g_log << Logger::Error << "Invalid result from dns_get_before_and_after_names_absolute, unexpected key " << item.first << endl;
×
427
        return false;
×
428
      }
×
429
    }
36✔
430

431
    logResult("unhashed=" << unhashed << ",before=" << before << ",after=" << after);
12!
432
    return true;
12✔
433
  }
12✔
434

435
private:
436
  std::list<DNSResourceRecord> d_result;
437
  bool d_debug_log{false};
438
  bool d_dnssec{false};
439

440
  lookup_call_t f_lookup;
441
  list_call_t f_list;
442

443
  get_domaininfo_call_t f_get_domaininfo;
444
  get_all_domains_call_t f_get_all_domains;
445

446
  get_domain_metadata_call_t f_get_domain_metadata;
447
  get_all_domain_metadata_call_t f_get_all_domain_metadata;
448

449
  get_domain_keys_call_t f_get_domain_keys;
450

451
  get_before_and_after_names_absolute_call_t f_get_before_and_after_names_absolute;
452

453
  set_notified_call_t f_set_notified;
454

455
  deinit_call_t f_deinit;
456
};
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

© 2026 Coveralls, Inc