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

PowerDNS / pdns / 14652478497

pending completion
14652478497

push

github

web-flow
Merge pull request #15439 from nokia/master

dnsdist: Support DSCP marking towards downstream server

56309 of 150030 branches covered (37.53%)

Branch coverage included in aggregate %.

19 of 26 new or added lines in 3 files covered. (73.08%)

9558 existing lines in 155 files now uncovered.

144757 of 188890 relevant lines covered (76.64%)

6616541.25 hits per line

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

60.51
/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)                                                                               \
UNCOV
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)                                                \
UNCOV
86
  {                                                                   \
×
UNCOV
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!
UNCOV
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!
UNCOV
120
    d_dnssec = d_lw->readVariable<boost::optional<bool>>("dns_dnssec").get_value_or(false);
×
121
    if (d_dnssec) {
15✔
UNCOV
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
    }
UNCOV
130
  }
×
131

132
  unsigned int getCapabilities() override
133
  {
134
    unsigned int caps = CAP_DIRECT | CAP_LIST;
135
    if (d_dnssec) {
29!
136
      caps |= CAP_DNSSEC;
103✔
137
    }
103✔
138
    return caps;
437✔
139
  }
437✔
140

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

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

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

6✔
196
    logCall("list", "target=" << target << ",domain_id=" << domain_id);
6!
197
    list_result_t result = f_list(target.operator const DNSName&(), domain_id);
6✔
198

6✔
199
    if (result.which() == 0)
6!
200
      return false;
201

23✔
202
    parseLookup(boost::get<lookup_result_t>(result));
23!
203

204
    return true;
205
  }
23✔
206

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

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

132✔
218
    logCall("lookup", "qtype=" << qtype.toString() << ",qname=" << qname << ",domain_id=" << domain_id);
132✔
219
    lookup_result_t result = f_lookup(qtype, qname, domain_id, ctx);
29✔
220
    parseLookup(result);
103✔
221
  }
103✔
222

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

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

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

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

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

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

3✔
296
    logCall("get_domaininfo", "domain=" << domain);
3!
297
    get_domaininfo_result_t result = f_get_domaininfo(domain.operator const DNSName&());
298

3✔
299
    if (result.which() == 0)
3!
300
      return false;
301

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

305
    return true;
306
  }
1!
307

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

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

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

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

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

340
    return true;
341
  }
×
342

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

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

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

6✔
357
    logResult("value=" << boost::algorithm::join(meta, ", "));
3!
358
    return true;
359
  }
3!
360

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

6✔
366
    logCall("get_domain_keys", "name=" << name);
6!
367
    get_domain_keys_result_t result = f_get_domain_keys(name.operator const DNSName&());
6✔
368

24✔
369
    if (result.which() == 0)
24✔
370
      return false;
6✔
371

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

393
    return true;
394
  }
12!
395

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

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

UNCOV
404
    if (result.which() == 0)
×
405
      return false;
36✔
406

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

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

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

439
  lookup_call_t f_lookup;
440
  list_call_t f_list;
441

442
  get_domaininfo_call_t f_get_domaininfo;
443
  get_all_domains_call_t f_get_all_domains;
444

445
  get_domain_metadata_call_t f_get_domain_metadata;
446
  get_all_domain_metadata_call_t f_get_all_domain_metadata;
447

448
  get_domain_keys_call_t f_get_domain_keys;
449

450
  get_before_and_after_names_absolute_call_t f_get_before_and_after_names_absolute;
451

452
  set_notified_call_t f_set_notified;
453

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