File: lua2api2.hh

package info (click to toggle)
pdns 5.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,824 kB
  • sloc: cpp: 101,240; sh: 5,616; makefile: 2,318; sql: 860; ansic: 675; python: 635; yacc: 245; perl: 161; lex: 131
file content (463 lines) | stat: -rw-r--r-- 18,933 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
 * This file is part of PowerDNS or dnsdist.
 * Copyright -- PowerDNS.COM B.V. and its contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * In addition, for the avoidance of any doubt, permission is granted to
 * link this program with OpenSSL and to (re)distribute the binaries
 * produced as the result of such linking.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTAPILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#pragma once
#include "boost/algorithm/string/join.hpp"
#include "pdns/arguments.hh"

#include "pdns/dnsbackend.hh"
#include "pdns/lua-auth4.hh"

class Lua2BackendAPIv2 : public DNSBackend, AuthLua4
{
private:
  typedef std::function<void()> init_call_t;
  typedef std::function<void()> deinit_call_t;

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

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

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

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

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

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

  typedef std::vector<std::pair<string, boost::variant<string, DNSName>>> before_and_after_names_result_t;
  typedef boost::variant<bool, before_and_after_names_result_t> get_before_and_after_names_absolute_result_t;
  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;

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

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

public:
  Lua2BackendAPIv2(const string& suffix)
  {
    d_include_path = ::arg()["lua-global-include-dir"];
    setArgPrefix("lua2" + suffix);
    d_debug_log = mustDo("query-logging");
    prepareContext();
    loadFile(getArg("filename"));
  }

  ~Lua2BackendAPIv2() override;

#define logCall(func, var)                                                                               \
  {                                                                                                      \
    if (d_debug_log) {                                                                                   \
      g_log << Logger::Debug << "[" << getPrefix() << "] Calling " << func << "(" << var << ")" << endl; \
    }                                                                                                    \
  }
#define logResult(var)                                                \
  {                                                                   \
    if (d_debug_log) {                                                \
      g_log << Logger::Debug << "[" << getPrefix() << "] Got result " \
            << "'" << var << "'" << endl;                             \
    }                                                                 \
  }

  void postPrepareContext() override
  {
    AuthLua4::postPrepareContext();
  }

  void postLoad() override
  {
    f_lookup = d_lw->readVariable<boost::optional<lookup_call_t>>("dns_lookup").get_value_or(0);
    f_list = d_lw->readVariable<boost::optional<list_call_t>>("dns_list").get_value_or(0);
    f_get_all_domains = d_lw->readVariable<boost::optional<get_all_domains_call_t>>("dns_get_all_domains").get_value_or(0);
    f_get_domaininfo = d_lw->readVariable<boost::optional<get_domaininfo_call_t>>("dns_get_domaininfo").get_value_or(0);
    f_get_domain_metadata = d_lw->readVariable<boost::optional<get_domain_metadata_call_t>>("dns_get_domain_metadata").get_value_or(0);
    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);
    f_get_domain_keys = d_lw->readVariable<boost::optional<get_domain_keys_call_t>>("dns_get_domain_keys").get_value_or(0);
    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);
    f_set_notified = d_lw->readVariable<boost::optional<set_notified_call_t>>("dns_set_notified").get_value_or(0);

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

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

    if (f_lookup == nullptr)
      throw PDNSException("dns_lookup missing");

    /* see if dnssec support is wanted */
    d_dnssec = d_lw->readVariable<boost::optional<bool>>("dns_dnssec").get_value_or(false);
    if (d_dnssec) {
      if (f_get_domain_metadata == nullptr)
        throw PDNSException("dns_dnssec is true but dns_get_domain_metadata is missing");
      if (f_get_before_and_after_names_absolute == nullptr)
        throw PDNSException("dns_dnssec is true but dns_get_before_and_after_names_absolute is missing");
      /* domain keys is not strictly speaking necessary for dnssec backend */
      if (f_get_domain_keys == nullptr)
        g_log << Logger::Warning << "dns_get_domain_keys missing - cannot do live signing" << endl;
    }
  }

  unsigned int getCapabilities() override
  {
    unsigned int caps = CAP_DIRECT | CAP_LIST;
    if (d_dnssec) {
      caps |= CAP_DNSSEC;
    }
    return caps;
  }

  void parseLookup(const lookup_result_t& result)
  {
    for (const auto& row : result) {
      DNSResourceRecord rec;
      for (const auto& item : row.second) {
        if (item.first == "type") {
          if (item.second.which() == 1)
            rec.qtype = QType(boost::get<int>(item.second));
          else if (item.second.which() == 3)
            rec.qtype = boost::get<string>(item.second);
          else if (item.second.which() == 4)
            rec.qtype = boost::get<QType>(item.second);
          else
            throw PDNSException("Unsupported value for type");
        }
        else if (item.first == "name") {
          if (item.second.which() == 3)
            rec.qname = DNSName(boost::get<string>(item.second));
          else if (item.second.which() == 2)
            rec.qname = boost::get<DNSName>(item.second);
          else
            throw PDNSException("Unsupported value for name");
        }
        else if (item.first == "domain_id")
          rec.domain_id = boost::get<int>(item.second);
        else if (item.first == "auth")
          rec.auth = boost::get<bool>(item.second);
        else if (item.first == "last_modified")
          rec.last_modified = static_cast<time_t>(boost::get<int>(item.second));
        else if (item.first == "ttl")
          rec.ttl = boost::get<int>(item.second);
        else if (item.first == "content")
          rec.setContent(boost::get<string>(item.second));
        else if (item.first == "scopeMask")
          rec.scopeMask = boost::get<int>(item.second);
        else
          g_log << Logger::Warning << "Unsupported key '" << item.first << "' in lookup or list result" << endl;
      }
      logResult(rec.qname << " IN " << rec.qtype.toString() << " " << rec.ttl << " " << rec.getZoneRepresentation());
      d_result.push_back(rec);
    }
    if (d_result.empty() && d_debug_log)
      g_log << Logger::Debug << "[" << getPrefix() << "] Got empty result" << endl;
  }

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

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

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

    if (result.which() == 0)
      return false;

    parseLookup(boost::get<lookup_result_t>(result));

    return true;
  }

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

    lookup_context_t ctx;
    if (p != NULL) {
      ctx.emplace_back(lookup_context_t::value_type{"source_address", p->getInnerRemote().toString()});
      ctx.emplace_back(lookup_context_t::value_type{"real_source_address", p->getRealRemote().toString()});
    }

    logCall("lookup", "qtype=" << qtype.toString() << ",qname=" << qname << ",domain_id=" << domain_id);
    lookup_result_t result = f_lookup(qtype, qname, domain_id, ctx);
    parseLookup(result);
  }

  bool get(DNSResourceRecord& rr) override
  {
    if (d_result.size() == 0)
      return false;
    rr = std::move(d_result.front());
    d_result.pop_front();
    return true;
  }

  string directBackendCmd(const string& querystr) override
  {
    string::size_type pos = querystr.find_first_of(" \t");
    string cmd = querystr;
    string par = "";
    if (pos != string::npos) {
      cmd = querystr.substr(0, pos);
      par = querystr.substr(pos + 1);
    }
    direct_backend_cmd_call_t f = d_lw->readVariable<boost::optional<direct_backend_cmd_call_t>>(cmd).get_value_or(0);
    if (f == nullptr) {
      return cmd + "not found";
    }
    logCall(cmd, "parameter=" << par);
    return f(par);
  }

  void setNotified(domainid_t id, uint32_t serial) override
  {
    if (f_set_notified == NULL)
      return;
    logCall("dns_set_notified", "id=" << id << ",serial=" << serial);
    f_set_notified(id, serial);
  }

  void parseDomainInfo(const domaininfo_result_t& row, DomainInfo& di)
  {
    di.id = UnknownDomainID;
    for (const auto& item : row) {
      if (item.first == "account")
        di.account = boost::get<string>(item.second);
      else if (item.first == "last_check")
        di.last_check = static_cast<time_t>(boost::get<long>(item.second));
      else if (item.first == "masters")
        for (const auto& primary : boost::get<vector<string>>(item.second))
          di.primaries.push_back(ComboAddress(primary, 53));
      else if (item.first == "id")
        di.id = static_cast<domainid_t>(boost::get<long>(item.second));
      else if (item.first == "notified_serial")
        di.notified_serial = static_cast<unsigned int>(boost::get<long>(item.second));
      else if (item.first == "serial")
        di.serial = static_cast<unsigned int>(boost::get<long>(item.second));
      else if (item.first == "kind")
        di.kind = DomainInfo::stringToKind(boost::get<string>(item.second));
      else
        g_log << Logger::Warning << "Unsupported key '" << item.first << "' in domaininfo result" << endl;
    }
    di.backend = this;
    logResult("zone=" << di.zone << ",serial=" << di.serial << ",kind=" << di.getKindString());
  }

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

      di.id = sd.domain_id;
      di.zone = domain;
      di.backend = this;
      di.serial = sd.serial;
      return true;
    }

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

    if (result.which() == 0)
      return false;

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

    return true;
  }

  void getAllDomains(vector<DomainInfo>* domains, bool /* getSerial */, bool /* include_disabled */) override
  {
    if (f_get_all_domains == nullptr)
      return;

    logCall("get_all_domains", "");
    for (const auto& row : f_get_all_domains()) {
      DomainInfo di;
      di.zone = ZoneName(row.first);
      logResult(di.zone);
      parseDomainInfo(row.second, di);
      domains->push_back(di);
    }
  }

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

    logCall("get_all_domain_metadata", "name=" << name);
    get_all_domain_metadata_result_t result = f_get_all_domain_metadata(name.operator const DNSName&());
    if (result.which() == 0)
      return false;

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

    return true;
  }

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

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

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

    logResult("value=" << boost::algorithm::join(meta, ", "));
    return true;
  }

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

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

    if (result.which() == 0)
      return false;

    for (const auto& row : boost::get<vector<pair<int, keydata_result_t>>>(result)) {
      DNSBackend::KeyData key;
      key.published = true;
      for (const auto& item : row.second) {
        if (item.first == "content")
          key.content = boost::get<string>(item.second);
        else if (item.first == "id")
          key.id = static_cast<unsigned int>(boost::get<int>(item.second));
        else if (item.first == "flags")
          key.flags = static_cast<unsigned int>(boost::get<int>(item.second));
        else if (item.first == "active")
          key.active = boost::get<bool>(item.second);
        else if (item.first == "published")
          key.published = boost::get<bool>(item.second);
        else
          g_log << Logger::Warning << "[" << getPrefix() << "] Unsupported key '" << item.first << "' in keydata result" << endl;
      }
      logResult("id=" << key.id << ",flags=" << key.flags << ",active=" << (key.active ? "true" : "false") << ",published=" << (key.published ? "true" : "false"));
      keys.emplace_back(std::move(key));
    }

    return true;
  }

  bool getBeforeAndAfterNamesAbsolute(domainid_t id, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after) override
  {
    if (f_get_before_and_after_names_absolute == nullptr)
      return false;

    logCall("get_before_and_after_names_absolute", "id=<<" << id << ",qname=" << qname);
    get_before_and_after_names_absolute_result_t result = f_get_before_and_after_names_absolute(id, qname);

    if (result.which() == 0)
      return false;

    before_and_after_names_result_t row = boost::get<before_and_after_names_result_t>(result);
    if (row.size() != 3) {
      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;
      return false;
    }
    for (const auto& item : row) {
      DNSName value;
      if (item.second.which() == 0)
        value = DNSName(boost::get<string>(item.second));
      else
        value = DNSName(boost::get<DNSName>(item.second));
      if (item.first == "unhashed")
        unhashed = value;
      else if (item.first == "before")
        before = value;
      else if (item.first == "after")
        after = value;
      else {
        g_log << Logger::Error << "Invalid result from dns_get_before_and_after_names_absolute, unexpected key " << item.first << endl;
        return false;
      }
    }

    logResult("unhashed=" << unhashed << ",before=" << before << ",after=" << after);
    return true;
  }

private:
  std::list<DNSResourceRecord> d_result;
  bool d_debug_log{false};
  bool d_dnssec{false};

  lookup_call_t f_lookup;
  list_call_t f_list;

  get_domaininfo_call_t f_get_domaininfo;
  get_all_domains_call_t f_get_all_domains;

  get_domain_metadata_call_t f_get_domain_metadata;
  get_all_domain_metadata_call_t f_get_all_domain_metadata;

  get_domain_keys_call_t f_get_domain_keys;

  get_before_and_after_names_absolute_call_t f_get_before_and_after_names_absolute;

  set_notified_call_t f_set_notified;

  deinit_call_t f_deinit;
};