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

PowerDNS / pdns / 9776942173

03 Jul 2024 11:23AM UTC coverage: 62.998% (-1.5%) from 64.521%
9776942173

Pull #14327

github

web-flow
Merge bb202aac1 into 53bcb9f9a
Pull Request #14327: dnsdist: add support for a callback when a new tickets key is added

29689 of 69080 branches covered (42.98%)

Branch coverage included in aggregate %.

0 of 23 new or added lines in 2 files covered. (0.0%)

8596 existing lines in 89 files now uncovered.

104563 of 144026 relevant lines covered (72.6%)

5604700.49 hits per line

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

58.58
/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/lexical_cast.hpp"
24
#include "boost/algorithm/string/join.hpp"
25
#include "pdns/arguments.hh"
26

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

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

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

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

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

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

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

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

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

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

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

68
public:
69
  Lua2BackendAPIv2(const string& suffix)
70
  {
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✔
UNCOV
82
      g_log << Logger::Debug << "[" << getPrefix() << "] Calling " << func << "(" << var << ")" << endl; \
×
UNCOV
83
    }                                                                                                    \
×
84
  }
48✔
85
#define logResult(var)                                                \
86
  {                                                                   \
128✔
87
    if (d_debug_log) {                                                \
128✔
UNCOV
88
      g_log << Logger::Debug << "[" << getPrefix() << "] Got result " \
×
UNCOV
89
            << "'" << var << "'" << endl;                             \
×
UNCOV
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!
UNCOV
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!
UNCOV
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)
15!
UNCOV
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 */
127
      if (f_get_domain_keys == nullptr)
15!
UNCOV
128
        g_log << Logger::Warning << "dns_get_domain_keys missing - cannot do live signing" << endl;
×
129
    }
15✔
130
  }
21✔
131

132
  bool doesDNSSEC() override
UNCOV
133
  {
×
UNCOV
134
    return d_dnssec;
×
UNCOV
135
  }
×
136

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

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

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

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

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

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

200
    return true;
6✔
201
  }
6✔
202

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

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

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);
23✔
216
    parseLookup(result);
23✔
217
  }
23✔
218

219
  bool get(DNSResourceRecord& rr) override
220
  {
132✔
221
    if (d_result.size() == 0)
132✔
222
      return false;
29✔
223
    rr = std::move(d_result.front());
103✔
224
    d_result.pop_front();
103✔
225
    return true;
103✔
226
  }
132✔
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
    }
×
UNCOV
241
    logCall(cmd, "parameter=" << par);
×
UNCOV
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;
×
UNCOV
249
    logCall("dns_set_notified", "id=" << static_cast<int>(id) << ",serial=" << serial);
×
UNCOV
250
    f_set_notified(static_cast<int>(id), serial);
×
UNCOV
251
  }
×
252

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

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

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

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

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

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

301
    return true;
3✔
302
  }
3✔
303

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

309
    logCall("get_all_domains", "");
1!
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);
2✔
315
      domains->push_back(di);
2✔
316
    }
2✔
317
  }
1✔
318

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

324
    logCall("get_all_domain_metadata", "name=" << name);
×
UNCOV
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)
×
UNCOV
332
        meta[row.first].push_back(item.second);
×
333
      logResult("kind=" << row.first << ",value=" << boost::algorithm::join(meta[row.first], ", "));
×
334
    }
×
335

UNCOV
336
    return true;
×
337
  }
×
338

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

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

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

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

357
  bool getDomainKeys(const DNSName& name, std::vector<DNSBackend::KeyData>& keys) override
358
  {
6✔
359
    if (f_get_domain_keys == nullptr)
6✔
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);
3✔
364

365
    if (result.which() == 0)
3!
UNCOV
366
      return false;
×
367

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

389
    return true;
3✔
390
  }
3✔
391

392
  bool getBeforeAndAfterNamesAbsolute(uint32_t id, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after) override
393
  {
12✔
394
    if (f_get_before_and_after_names_absolute == nullptr)
12!
UNCOV
395
      return false;
×
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);
12✔
399

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

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

426
    logResult("unhashed=" << unhashed << ",before=" << before << ",after=" << after);
12!
427
    return true;
12✔
428
  }
12✔
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

© 2026 Coveralls, Inc