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

PowerDNS / pdns / 9829858019

07 Jul 2024 07:44PM UTC coverage: 59.459% (-5.2%) from 64.689%
9829858019

push

github

web-flow
Merge pull request #14433 from PowerDNS/dependabot/pip/pdns/dnsdistdist/docs/certifi-2024.7.4

build(deps): bump certifi from 2024.2.2 to 2024.7.4 in /pdns/dnsdistdist/docs

33010 of 88300 branches covered (37.38%)

Branch coverage included in aggregate %.

115858 of 162069 relevant lines covered (71.49%)

4399707.99 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/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
  {
×
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
  bool doesDNSSEC() override
133
  {
×
134
    return d_dnssec;
×
135
  }
×
136

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

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;
×
187
    }
×
188

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

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

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

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

200
    return true;
×
201
  }
×
202

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

208
    lookup_context_t ctx;
×
209
    if (p != NULL) {
×
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()});
×
212
    }
×
213

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

219
  bool get(DNSResourceRecord& rr) override
220
  {
×
221
    if (d_result.size() == 0)
×
222
      return false;
×
223
    rr = std::move(d_result.front());
×
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
  }
×
252

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

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

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

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

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

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

301
    return true;
×
302
  }
×
303

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

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

319
  bool getAllDomainMetadata(const DNSName& name, std::map<std::string, std::vector<std::string>>& meta) override
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
  }
×
356

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

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

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

368
    for (const auto& row : boost::get<vector<pair<int, keydata_result_t>>>(result)) {
×
369
      DNSBackend::KeyData key;
×
370
      key.published = true;
×
371
      for (const auto& item : row.second) {
×
372
        if (item.first == "content")
×
373
          key.content = boost::get<string>(item.second);
×
374
        else if (item.first == "id")
×
375
          key.id = static_cast<unsigned int>(boost::get<int>(item.second));
×
376
        else if (item.first == "flags")
×
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);
×
382
        else
×
383
          g_log << Logger::Warning << "[" << getPrefix() << "] Unsupported key '" << item.first << "' in keydata result" << endl;
×
384
      }
×
385
      logResult("id=" << key.id << ",flags=" << key.flags << ",active=" << (key.active ? "true" : "false") << ",published=" << (key.published ? "true" : "false"));
×
386
      keys.push_back(key);
×
387
    }
×
388

389
    return true;
×
390
  }
×
391

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)
×
395
      return false;
×
396

397
    logCall("get_before_and_after_names_absolute", "id=<<" << id << ",qname=" << qname);
×
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)
×
401
      return false;
×
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;
×
406
      return false;
×
407
    }
×
408
    for (const auto& item : row) {
×
409
      DNSName value;
×
410
      if (item.second.which() == 0)
×
411
        value = DNSName(boost::get<string>(item.second));
×
412
      else
×
413
        value = DNSName(boost::get<DNSName>(item.second));
×
414
      if (item.first == "unhashed")
×
415
        unhashed = value;
×
416
      else if (item.first == "before")
×
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;
×
422
        return false;
×
423
      }
×
424
    }
×
425

426
    logResult("unhashed=" << unhashed << ",before=" << before << ",after=" << after);
×
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