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

PowerDNS / pdns / 14619223771

23 Apr 2025 01:20PM UTC coverage: 61.343% (-0.02%) from 61.367%
14619223771

push

github

web-flow
Merge pull request #15299 from miodvallat/all_inclusive

API: allow fetching disabled RRsets

33020 of 78928 branches covered (41.84%)

Branch coverage included in aggregate %.

52 of 75 new or added lines in 11 files covered. (69.33%)

89 existing lines in 13 files now uncovered.

106179 of 147992 relevant lines covered (71.75%)

5098480.6 hits per line

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

34.72
/modules/remotebackend/remotebackend.cc
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
 * MERCHANTABILITY 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
#include <limits>
23
#ifdef HAVE_CONFIG_H
24
#include "config.h"
25
#endif
26
#include "remotebackend.hh"
27

28
static const char* kBackendId = "[RemoteBackend]";
29

30
/**
31
 * Forwarder for value. This is just in case
32
 * we need to do some treatment to the value before
33
 * sending it downwards.
34
 */
35
bool Connector::send(Json& value)
36
{
640✔
37
  return send_message(value) > 0;
640✔
38
}
640✔
39

40
/**
41
 * Helper for handling receiving of data.
42
 * Basically what happens here is that we check
43
 * that the receiving happened ok, and extract
44
 * result. Logging is performed here, too.
45
 */
46
bool Connector::recv(Json& value)
47
{
640✔
48
  if (recv_message(value) > 0) {
640!
49
    bool retval = true;
640✔
50
    if (value["result"] == Json()) {
640!
51
      throw PDNSException("No 'result' field in response from remote process");
×
52
    }
×
53
    if (value["result"].is_bool() && !boolFromJson(value, "result", false)) {
640✔
54
      retval = false;
24✔
55
    }
24✔
56
    for (const auto& message : value["log"].array_items()) {
640✔
57
      g_log << Logger::Info << "[remotebackend]: " << message.string_value() << std::endl;
102✔
58
    }
102✔
59
    return retval;
640✔
60
  }
640✔
61
  throw PDNSException("Unknown error while receiving data");
×
62
}
640✔
63

64
void RemoteBackend::makeErrorAndThrow(Json& value)
65
{
×
66
  std::string msg = "Remote process indicated a failure";
×
67
  for (const auto& message : value["log"].array_items()) {
×
68
    msg += " '" + message.string_value() + "'";
×
69
  }
×
70
  throw PDNSException(msg);
×
71
}
×
72

73
/**
74
 * Standard ctor and dtor
75
 */
76
RemoteBackend::RemoteBackend(const std::string& suffix)
77
{
92✔
78
  setArgPrefix("remote" + suffix);
92✔
79

80
  this->d_connstr = getArg("connection-string");
92✔
81
  this->d_dnssec = mustDo("dnssec");
92✔
82

83
  build();
92✔
84
}
92✔
85

86
RemoteBackend::~RemoteBackend() = default;
76✔
87

88
bool RemoteBackend::send(Json& value)
89
{
581✔
90
  try {
581✔
91
    if (!connector->send(value)) {
581!
92
      // XXX does this work work even though we throw?
93
      this->connector.reset();
×
94
      build();
×
95
      throw DBException("Could not send a message to remote process");
×
96
    }
×
97
  }
581✔
98
  catch (const PDNSException& ex) {
581✔
99
    throw DBException("Exception caught when sending: " + ex.reason);
×
100
  }
×
101
  return true;
581✔
102
}
581✔
103

104
bool RemoteBackend::recv(Json& value)
105
{
581✔
106
  try {
581✔
107
    return connector->recv(value);
581✔
108
  }
581✔
109
  catch (const PDNSException& ex) {
581✔
110
    this->connector.reset();
×
111
    build();
×
112
    throw DBException("Exception caught when receiving: " + ex.reason);
×
113
  }
×
114
  catch (const std::exception& e) {
581✔
115
    this->connector.reset();
×
116
    build();
×
117
    throw DBException("Exception caught when receiving: " + std::string(e.what()));
×
118
  }
×
119
}
581✔
120

121
/**
122
 * Builds connector based on options
123
 * Currently supports unix,pipe and http
124
 */
125
int RemoteBackend::build()
126
{
92✔
127
  std::vector<std::string> parts;
92✔
128
  std::string type;
92✔
129
  std::string opts;
92✔
130
  std::map<std::string, std::string> options;
92✔
131

132
  // connstr is of format "type:options"
133
  size_t pos = 0;
92✔
134
  pos = d_connstr.find_first_of(':');
92✔
135
  if (pos == std::string::npos) {
92!
136
    throw PDNSException("Invalid connection string: malformed");
×
137
  }
×
138

139
  type = d_connstr.substr(0, pos);
92✔
140
  opts = d_connstr.substr(pos + 1);
92✔
141

142
  // tokenize the string on comma
143
  stringtok(parts, opts, ",");
92✔
144

145
  // find out some options and parse them while we're at it
146
  for (const auto& opt : parts) {
140✔
147
    std::string key;
140✔
148
    std::string val;
140✔
149
    // make sure there is something else than air in the option...
150
    if (opt.find_first_not_of(" ") == std::string::npos) {
140!
151
      continue;
×
152
    }
×
153

154
    // split it on '='. if not found, we treat it as "yes"
155
    pos = opt.find_first_of("=");
140✔
156

157
    if (pos == std::string::npos) {
140!
158
      key = opt;
×
159
      val = "yes";
×
160
    }
×
161
    else {
140✔
162
      key = opt.substr(0, pos);
140✔
163
      val = opt.substr(pos + 1);
140✔
164
    }
140✔
165
    options[key] = std::move(val);
140✔
166
  }
140✔
167

168
  // connectors know what they are doing
169
  if (type == "unix") {
92✔
170
    this->connector = std::make_unique<UnixsocketConnector>(options);
23✔
171
  }
23✔
172
  else if (type == "http") {
69✔
173
    this->connector = std::make_unique<HTTPConnector>(options);
23✔
174
  }
23✔
175
  else if (type == "zeromq") {
46✔
176
#ifdef REMOTEBACKEND_ZEROMQ
23✔
177
    this->connector = std::make_unique<ZeroMQConnector>(options);
23✔
178
#else
179
    throw PDNSException("Invalid connection string: zeromq connector support not enabled. Recompile with --enable-remotebackend-zeromq");
180
#endif
181
  }
23✔
182
  else if (type == "pipe") {
23!
183
    this->connector = std::make_unique<PipeConnector>(options);
23✔
184
  }
23✔
185
  else {
×
186
    throw PDNSException("Invalid connection string: unknown connector");
×
187
  }
×
188

189
  return -1;
92✔
190
}
92✔
191

192
/**
193
 * The functions here are just remote json stubs that send and receive the method call
194
 * data is mainly left alone, some defaults are assumed.
195
 */
196
void RemoteBackend::lookup(const QType& qtype, const DNSName& qdomain, int zoneId, DNSPacket* pkt_p)
197
{
148✔
198
  if (d_index != -1) {
148!
199
    throw PDNSException("Attempt to lookup while one running");
×
200
  }
×
201

202
  string localIP = "0.0.0.0";
148✔
203
  string remoteIP = "0.0.0.0";
148✔
204
  string realRemote = "0.0.0.0/0";
148✔
205

206
  if (pkt_p != nullptr) {
148✔
207
    localIP = pkt_p->getLocal().toString();
56✔
208
    realRemote = pkt_p->getRealRemote().toString();
56✔
209
    remoteIP = pkt_p->getInnerRemote().toString();
56✔
210
  }
56✔
211

212
  Json query = Json::object{
148✔
213
    {"method", "lookup"},
148✔
214
    {"parameters", Json::object{{"qtype", qtype.toString()}, {"qname", qdomain.toString()}, {"remote", remoteIP}, {"local", localIP}, {"real-remote", realRemote}, {"zone-id", zoneId}}}};
148✔
215

216
  if (!this->send(query) || !this->recv(d_result)) {
148!
217
    return;
×
218
  }
×
219

220
  // OK. we have result parameters in result. do not process empty result.
221
  if (!d_result["result"].is_array() || d_result["result"].array_items().empty()) {
148!
222
    return;
60✔
223
  }
60✔
224

225
  d_index = 0;
88✔
226
}
88✔
227

228
// Similar to lookup above, but passes an extra include_disabled parameter.
229
void RemoteBackend::APILookup(const QType& qtype, const DNSName& qdomain, int zoneId, bool include_disabled)
NEW
230
{
×
NEW
231
  if (d_index != -1) {
×
NEW
232
    throw PDNSException("Attempt to lookup while one running");
×
NEW
233
  }
×
234

NEW
235
  string localIP = "0.0.0.0";
×
NEW
236
  string remoteIP = "0.0.0.0";
×
NEW
237
  string realRemote = "0.0.0.0/0";
×
238

NEW
239
  Json query = Json::object{
×
NEW
240
    {"method", "APILookup"},
×
NEW
241
    {"parameters", Json::object{{"qtype", qtype.toString()}, {"qname", qdomain.toString()}, {"remote", remoteIP}, {"local", localIP}, {"real-remote", realRemote}, {"zone-id", zoneId}, {"include-disabled", include_disabled}}}};
×
242

NEW
243
  if (!this->send(query) || !this->recv(d_result)) {
×
NEW
244
    return;
×
NEW
245
  }
×
246

247
  // OK. we have result parameters in result. do not process empty result.
NEW
248
  if (!d_result["result"].is_array() || d_result["result"].array_items().empty()) {
×
NEW
249
    return;
×
NEW
250
  }
×
251

NEW
252
  d_index = 0;
×
NEW
253
}
×
254

255
bool RemoteBackend::list(const ZoneName& target, int domain_id, bool include_disabled)
256
{
16✔
257
  if (d_index != -1) {
16!
258
    throw PDNSException("Attempt to lookup while one running");
×
259
  }
×
260

261
  Json query = Json::object{
16✔
262
    {"method", "list"},
16✔
263
    {"parameters", Json::object{{"zonename", target.toString()}, {"domain_id", domain_id}, {"include_disabled", include_disabled}}}};
16✔
264

265
  if (!this->send(query) || !this->recv(d_result)) {
16!
266
    return false;
×
267
  }
×
268
  if (!d_result["result"].is_array() || d_result["result"].array_items().empty()) {
16!
269
    return false;
×
270
  }
×
271

272
  d_index = 0;
16✔
273
  return true;
16✔
274
}
16✔
275

276
bool RemoteBackend::get(DNSResourceRecord& rr)
277
{
478✔
278
  if (d_index == -1) {
478✔
279
    return false;
164✔
280
  }
164✔
281

282
  rr.qtype = stringFromJson(d_result["result"][d_index], "qtype");
314✔
283
  rr.qname = DNSName(stringFromJson(d_result["result"][d_index], "qname"));
314✔
284
  rr.qclass = QClass::IN;
314✔
285
  rr.content = stringFromJson(d_result["result"][d_index], "content");
314✔
286
  rr.ttl = d_result["result"][d_index]["ttl"].int_value();
314✔
287
  rr.domain_id = intFromJson(d_result["result"][d_index], "domain_id", -1);
314✔
288
  if (d_dnssec) {
314✔
289
    rr.auth = (intFromJson(d_result["result"][d_index], "auth", 1) != 0);
216✔
290
  }
216✔
291
  else {
98✔
292
    rr.auth = true;
98✔
293
  }
98✔
294
  rr.scopeMask = d_result["result"][d_index]["scopeMask"].int_value();
314✔
295
  d_index++;
314✔
296

297
  // id index is out of bounds, we know the results end here.
298
  if (d_index == static_cast<int>(d_result["result"].array_items().size())) {
314✔
299
    d_result = Json();
104✔
300
    d_index = -1;
104✔
301
  }
104✔
302
  return true;
314✔
303
}
478✔
304

305
bool RemoteBackend::getBeforeAndAfterNamesAbsolute(uint32_t id, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after)
306
{
261✔
307
  // no point doing dnssec if it's not supported
308
  if (!d_dnssec) {
261!
309
    return false;
×
310
  }
×
311

312
  Json query = Json::object{
261✔
313
    {"method", "getBeforeAndAfterNamesAbsolute"},
261✔
314
    {"parameters", Json::object{{"id", Json(static_cast<double>(id))}, {"qname", qname.toString()}}}};
261✔
315
  Json answer;
261✔
316

317
  if (!this->send(query) || !this->recv(answer)) {
261!
318
    return false;
×
319
  }
×
320

321
  unhashed = DNSName(stringFromJson(answer["result"], "unhashed"));
261✔
322
  before.clear();
261✔
323
  after.clear();
261✔
324
  if (answer["result"]["before"] != Json()) {
261!
325
    before = DNSName(stringFromJson(answer["result"], "before"));
261✔
326
  }
261✔
327
  if (answer["result"]["after"] != Json()) {
261!
328
    after = DNSName(stringFromJson(answer["result"], "after"));
261✔
329
  }
261✔
330

331
  return true;
261✔
332
}
261✔
333

334
bool RemoteBackend::getAllDomainMetadata(const ZoneName& name, std::map<std::string, std::vector<std::string>>& meta)
335
{
48✔
336
  Json query = Json::object{
48✔
337
    {"method", "getAllDomainMetadata"},
48✔
338
    {"parameters", Json::object{{"name", name.toString()}}}};
48✔
339

340
  if (!this->send(query)) {
48!
341
    return false;
×
342
  }
×
343

344
  meta.clear();
48✔
345

346
  Json answer;
48✔
347
  // not mandatory to implement
348
  if (!this->recv(answer)) {
48!
349
    return true;
×
350
  }
×
351

352
  for (const auto& pair : answer["result"].object_items()) {
48!
353
    if (pair.second.is_array()) {
×
354
      for (const auto& val : pair.second.array_items()) {
×
355
        meta[pair.first].push_back(asString(val));
×
356
      }
×
357
    }
×
358
    else {
×
359
      meta[pair.first].push_back(asString(pair.second));
×
360
    }
×
361
  }
×
362

363
  return true;
48✔
364
}
48✔
365

366
bool RemoteBackend::getDomainMetadata(const ZoneName& name, const std::string& kind, std::vector<std::string>& meta)
367
{
16✔
368
  Json query = Json::object{
16✔
369
    {"method", "getDomainMetadata"},
16✔
370
    {"parameters", Json::object{{"name", name.toString()}, {"kind", kind}}}};
16✔
371

372
  if (!this->send(query)) {
16!
373
    return false;
×
374
  }
×
375

376
  meta.clear();
16✔
377

378
  Json answer;
16✔
379
  // not mandatory to implement
380
  if (!this->recv(answer)) {
16!
381
    return true;
×
382
  }
×
383

384
  if (answer["result"].is_array()) {
16!
385
    for (const auto& row : answer["result"].array_items()) {
16!
386
      meta.push_back(row.string_value());
×
387
    }
×
388
  }
16✔
389
  else if (answer["result"].is_string()) {
×
390
    meta.push_back(answer["result"].string_value());
×
391
  }
×
392

393
  return true;
16✔
394
}
16✔
395

396
bool RemoteBackend::setDomainMetadata(const ZoneName& name, const std::string& kind, const std::vector<std::string>& meta)
397
{
×
398
  Json query = Json::object{
×
399
    {"method", "setDomainMetadata"},
×
400
    {"parameters", Json::object{{"name", name.toString()}, {"kind", kind}, {"value", meta}}}};
×
401

402
  Json answer;
×
403
  if (!this->send(query) || !this->recv(answer)) {
×
404
    return false;
×
405
  }
×
406

407
  return boolFromJson(answer, "result", false);
×
408
}
×
409

410
bool RemoteBackend::getDomainKeys(const ZoneName& name, std::vector<DNSBackend::KeyData>& keys)
411
{
48✔
412
  // no point doing dnssec if it's not supported
413
  if (!d_dnssec) {
48✔
414
    return false;
12✔
415
  }
12✔
416

417
  Json query = Json::object{
36✔
418
    {"method", "getDomainKeys"},
36✔
419
    {"parameters", Json::object{{"name", name.toString()}}}};
36✔
420

421
  Json answer;
36✔
422
  if (!this->send(query) || !this->recv(answer)) {
36!
423
    return false;
8✔
424
  }
8✔
425

426
  keys.clear();
28✔
427

428
  for (const auto& jsonKey : answer["result"].array_items()) {
28✔
429
    DNSBackend::KeyData key;
28✔
430
    key.id = intFromJson(jsonKey, "id");
28✔
431
    key.flags = intFromJson(jsonKey, "flags");
28✔
432
    key.active = asBool(jsonKey["active"]);
28✔
433
    key.published = boolFromJson(jsonKey, "published", true);
28✔
434
    key.content = stringFromJson(jsonKey, "content");
28✔
435
    keys.push_back(key);
28✔
436
  }
28✔
437

438
  return true;
28✔
439
}
36✔
440

441
bool RemoteBackend::removeDomainKey(const ZoneName& name, unsigned int keyId)
442
{
×
443
  // no point doing dnssec if it's not supported
444
  if (!d_dnssec) {
×
445
    return false;
×
446
  }
×
447

448
  Json query = Json::object{
×
449
    {"method", "removeDomainKey"},
×
450
    {"parameters", Json::object{{"name", name.toString()}, {"id", static_cast<int>(keyId)}}}};
×
451

452
  Json answer;
×
453
  return this->send(query) && this->recv(answer);
×
454
}
×
455

456
bool RemoteBackend::addDomainKey(const ZoneName& name, const KeyData& key, int64_t& keyId)
457
{
8✔
458
  // no point doing dnssec if it's not supported
459
  if (!d_dnssec) {
8!
460
    return false;
×
461
  }
×
462

463
  Json query = Json::object{
8✔
464
    {"method", "addDomainKey"},
8✔
465
    {"parameters", Json::object{{"name", name.toString()}, {"key", Json::object{{"flags", static_cast<int>(key.flags)}, {"active", key.active}, {"published", key.published}, {"content", key.content}}}}}};
8✔
466

467
  Json answer;
8✔
468
  if (!this->send(query) || !this->recv(answer)) {
8!
469
    return false;
×
470
  }
×
471

472
  keyId = answer["result"].int_value();
8✔
473
  return keyId >= 0;
8✔
474
}
8✔
475

476
bool RemoteBackend::activateDomainKey(const ZoneName& name, unsigned int keyId)
477
{
×
478
  // no point doing dnssec if it's not supported
479
  if (!d_dnssec) {
×
480
    return false;
×
481
  }
×
482

483
  Json query = Json::object{
×
484
    {"method", "activateDomainKey"},
×
485
    {"parameters", Json::object{{"name", name.toString()}, {"id", static_cast<int>(keyId)}}}};
×
486

487
  Json answer;
×
488
  return this->send(query) && this->recv(answer);
×
489
}
×
490

491
bool RemoteBackend::deactivateDomainKey(const ZoneName& name, unsigned int keyId)
492
{
×
493
  // no point doing dnssec if it's not supported
494
  if (!d_dnssec) {
×
495
    return false;
×
496
  }
×
497

498
  Json query = Json::object{
×
499
    {"method", "deactivateDomainKey"},
×
500
    {"parameters", Json::object{{"name", name.toString()}, {"id", static_cast<int>(keyId)}}}};
×
501

502
  Json answer;
×
503
  return this->send(query) && this->recv(answer);
×
504
}
×
505

506
bool RemoteBackend::publishDomainKey(const ZoneName& name, unsigned int keyId)
507
{
×
508
  // no point doing dnssec if it's not supported
509
  if (!d_dnssec) {
×
510
    return false;
×
511
  }
×
512

513
  Json query = Json::object{
×
514
    {"method", "publishDomainKey"},
×
515
    {"parameters", Json::object{{"name", name.toString()}, {"id", static_cast<int>(keyId)}}}};
×
516

517
  Json answer;
×
518
  return this->send(query) && this->recv(answer);
×
519
}
×
520

521
bool RemoteBackend::unpublishDomainKey(const ZoneName& name, unsigned int keyId)
522
{
×
523
  // no point doing dnssec if it's not supported
524
  if (!d_dnssec) {
×
525
    return false;
×
526
  }
×
527

528
  Json query = Json::object{
×
529
    {"method", "unpublishDomainKey"},
×
530
    {"parameters", Json::object{{"name", name.toString()}, {"id", static_cast<int>(keyId)}}}};
×
531

532
  Json answer;
×
533
  return this->send(query) && this->recv(answer);
×
534
}
×
535

536
unsigned int RemoteBackend::getCapabilities()
537
{
76✔
538
  unsigned int caps = CAP_DIRECT | CAP_LIST;
76✔
539
  if (d_dnssec) {
76✔
540
    caps |= CAP_DNSSEC;
72✔
541
  }
72✔
542
  return caps;
76✔
543
}
76✔
544

545
bool RemoteBackend::getTSIGKey(const DNSName& name, DNSName& algorithm, std::string& content)
546
{
×
547
  // no point doing dnssec if it's not supported
548
  if (!d_dnssec) {
×
549
    return false;
×
550
  }
×
551

552
  Json query = Json::object{
×
553
    {"method", "getTSIGKey"},
×
554
    {"parameters", Json::object{{"name", name.toString()}}}};
×
555

556
  Json answer;
×
557
  if (!this->send(query) || !this->recv(answer)) {
×
558
    return false;
×
559
  }
×
560

561
  algorithm = DNSName(stringFromJson(answer["result"], "algorithm"));
×
562
  content = stringFromJson(answer["result"], "content");
×
563

564
  return true;
×
565
}
×
566

567
bool RemoteBackend::setTSIGKey(const DNSName& name, const DNSName& algorithm, const std::string& content)
568
{
×
569
  // no point doing dnssec if it's not supported
570
  if (!d_dnssec) {
×
571
    return false;
×
572
  }
×
573

574
  Json query = Json::object{
×
575
    {"method", "setTSIGKey"},
×
576
    {"parameters", Json::object{{"name", name.toString()}, {"algorithm", algorithm.toString()}, {"content", content}}}};
×
577

578
  Json answer;
×
579
  return connector->send(query) && connector->recv(answer);
×
580
}
×
581

582
bool RemoteBackend::deleteTSIGKey(const DNSName& name)
583
{
×
584
  // no point doing dnssec if it's not supported
585
  if (!d_dnssec) {
×
586
    return false;
×
587
  }
×
588
  Json query = Json::object{
×
589
    {"method", "deleteTSIGKey"},
×
590
    {"parameters", Json::object{{"name", name.toString()}}}};
×
591

592
  Json answer;
×
593
  return connector->send(query) && connector->recv(answer);
×
594
}
×
595

596
bool RemoteBackend::getTSIGKeys(std::vector<struct TSIGKey>& keys)
597
{
×
598
  // no point doing dnssec if it's not supported
599
  if (!d_dnssec) {
×
600
    return false;
×
601
  }
×
602
  Json query = Json::object{
×
603
    {"method", "getTSIGKeys"},
×
604
    {"parameters", Json::object{}}};
×
605

606
  Json answer;
×
607
  if (!connector->send(query) || !connector->recv(answer)) {
×
608
    return false;
×
609
  }
×
610

611
  for (const auto& jsonKey : answer["result"].array_items()) {
×
612
    struct TSIGKey key;
×
613
    key.name = DNSName(stringFromJson(jsonKey, "name"));
×
614
    key.algorithm = DNSName(stringFromJson(jsonKey, "algorithm"));
×
615
    key.key = stringFromJson(jsonKey, "content");
×
616
    keys.push_back(key);
×
617
  }
×
618

619
  return true;
×
620
}
×
621

622
void RemoteBackend::parseDomainInfo(const Json& obj, DomainInfo& di)
623
{
24✔
624
  di.id = intFromJson(obj, "id", -1);
24✔
625
  di.zone = ZoneName(stringFromJson(obj, "zone"));
24✔
626
  for (const auto& primary : obj["masters"].array_items()) {
24!
627
    di.primaries.emplace_back(primary.string_value(), 53);
×
628
  }
×
629

630
  di.notified_serial = static_cast<unsigned int>(doubleFromJson(obj, "notified_serial", 0));
24✔
631
  di.serial = static_cast<unsigned int>(obj["serial"].number_value());
24✔
632
  di.last_check = static_cast<time_t>(obj["last_check"].number_value());
24✔
633

634
  string kind;
24✔
635
  if (obj["kind"].is_string()) {
24!
636
    kind = stringFromJson(obj, "kind");
24✔
637
  }
24✔
638
  if (kind == "master") {
24!
639
    di.kind = DomainInfo::Primary;
×
640
  }
×
641
  else if (kind == "slave") {
24!
642
    di.kind = DomainInfo::Secondary;
×
643
  }
×
644
  else {
24✔
645
    di.kind = DomainInfo::Native;
24✔
646
  }
24✔
647
  di.backend = this;
24✔
648
}
24✔
649

650
bool RemoteBackend::getDomainInfo(const ZoneName& domain, DomainInfo& info, bool /* getSerial */)
651
{
24✔
652
  if (domain.empty()) {
24!
653
    return false;
×
654
  }
×
655

656
  Json query = Json::object{
24✔
657
    {"method", "getDomainInfo"},
24✔
658
    {"parameters", Json::object{{"name", domain.toString()}}}};
24✔
659

660
  Json answer;
24✔
661
  if (!this->send(query) || !this->recv(answer)) {
24!
662
    return false;
×
663
  }
×
664

665
  this->parseDomainInfo(answer["result"], info);
24✔
666
  return true;
24✔
667
}
24✔
668

669
void RemoteBackend::setNotified(uint32_t id, uint32_t serial)
670
{
×
671
  Json query = Json::object{
×
672
    {"method", "setNotified"},
×
673
    {"parameters", Json::object{{"id", static_cast<double>(id)}, {"serial", static_cast<double>(serial)}}}};
×
674

675
  Json answer;
×
676
  if (!this->send(query) || !this->recv(answer)) {
×
677
    g_log << Logger::Error << kBackendId << " Failed to execute RPC for RemoteBackend::setNotified(" << id << "," << serial << ")" << endl;
×
678
  }
×
679
}
×
680

681
bool RemoteBackend::autoPrimaryBackend(const string& ipAddress, const ZoneName& domain, const vector<DNSResourceRecord>& nsset, string* nameserver, string* account, DNSBackend** ddb)
682
{
×
683
  Json::array rrset;
×
684

685
  for (const auto& ns : nsset) {
×
686
    rrset.push_back(Json::object{
×
687
      {"qtype", ns.qtype.toString()},
×
688
      {"qname", ns.qname.toString()},
×
689
      {"qclass", QClass::IN.getCode()},
×
690
      {"content", ns.content},
×
691
      {"ttl", static_cast<int>(ns.ttl)},
×
692
      {"auth", ns.auth}});
×
693
  }
×
694

695
  Json query = Json::object{
×
696
    {"method", "superMasterBackend"},
×
697
    {"parameters", Json::object{{"ip", ipAddress}, {"domain", domain.toString()}, {"nsset", rrset}}}};
×
698

699
  *ddb = nullptr;
×
700

701
  Json answer;
×
702
  if (!this->send(query) || !this->recv(answer)) {
×
703
    return false;
×
704
  }
×
705

706
  // we are the backend
707
  *ddb = this;
×
708

709
  // we allow simple true as well...
710
  if (answer["result"].is_object()) {
×
711
    *account = stringFromJson(answer["result"], "account");
×
712
    *nameserver = stringFromJson(answer["result"], "nameserver");
×
713
  }
×
714

715
  return true;
×
716
}
×
717

718
bool RemoteBackend::createSecondaryDomain(const string& ipAddress, const ZoneName& domain, const string& nameserver, const string& account)
719
{
×
720
  Json query = Json::object{
×
721
    {"method", "createSlaveDomain"},
×
722
    {"parameters", Json::object{
×
723
                     {"ip", ipAddress},
×
724
                     {"domain", domain.toString()},
×
725
                     {"nameserver", nameserver},
×
726
                     {"account", account},
×
727
                   }}};
×
728

729
  Json answer;
×
730
  return this->send(query) && this->recv(answer);
×
731
}
×
732

733
bool RemoteBackend::replaceRRSet(uint32_t domain_id, const DNSName& qname, const QType& qtype, const vector<DNSResourceRecord>& rrset)
734
{
×
735
  Json::array json_rrset;
×
736
  for (const auto& rr : rrset) {
×
737
    json_rrset.push_back(Json::object{
×
738
      {"qtype", rr.qtype.toString()},
×
739
      {"qname", rr.qname.toString()},
×
740
      {"qclass", QClass::IN.getCode()},
×
741
      {"content", rr.content},
×
742
      {"ttl", static_cast<int>(rr.ttl)},
×
743
      {"auth", rr.auth}});
×
744
  }
×
745

746
  Json query = Json::object{
×
747
    {"method", "replaceRRSet"},
×
748
    {"parameters", Json::object{{"domain_id", static_cast<double>(domain_id)}, {"qname", qname.toString()}, {"qtype", qtype.toString()}, {"trxid", static_cast<double>(d_trxid)}, {"rrset", json_rrset}}}};
×
749

750
  Json answer;
×
751
  return this->send(query) && this->recv(answer);
×
752
}
×
753

754
bool RemoteBackend::feedRecord(const DNSResourceRecord& rr, const DNSName& ordername, bool /* ordernameIsNSEC3 */)
755
{
×
756
  Json query = Json::object{
×
757
    {"method", "feedRecord"},
×
758
    {"parameters", Json::object{
×
759
                     {"rr", Json::object{{"qtype", rr.qtype.toString()}, {"qname", rr.qname.toString()}, {"qclass", QClass::IN.getCode()}, {"content", rr.content}, {"ttl", static_cast<int>(rr.ttl)}, {"auth", rr.auth}, {"ordername", (ordername.empty() ? Json() : ordername.toString())}}},
×
760
                     {"trxid", static_cast<double>(d_trxid)},
×
761
                   }}};
×
762

763
  Json answer;
×
764
  return this->send(query) && this->recv(answer); // XXX FIXME this API should not return 'true' I think -ahu
×
765
}
×
766

767
bool RemoteBackend::feedEnts(int domain_id, map<DNSName, bool>& nonterm)
768
{
×
769
  Json::array nts;
×
770

771
  for (const auto& t : nonterm) {
×
772
    nts.push_back(Json::object{
×
773
      {"nonterm", t.first.toString()},
×
774
      {"auth", t.second}});
×
775
  }
×
776

777
  Json query = Json::object{
×
778
    {"method", "feedEnts"},
×
779
    {"parameters", Json::object{{"domain_id", domain_id}, {"trxid", static_cast<double>(d_trxid)}, {"nonterm", nts}}},
×
780
  };
×
781

782
  Json answer;
×
783
  return this->send(query) && this->recv(answer);
×
784
}
×
785

786
bool RemoteBackend::feedEnts3(int domain_id, const DNSName& domain, map<DNSName, bool>& nonterm, const NSEC3PARAMRecordContent& ns3prc, bool narrow)
787
{
×
788
  Json::array nts;
×
789

790
  for (const auto& t : nonterm) {
×
791
    nts.push_back(Json::object{
×
792
      {"nonterm", t.first.toString()},
×
793
      {"auth", t.second}});
×
794
  }
×
795

796
  Json query = Json::object{
×
797
    {"method", "feedEnts3"},
×
798
    {"parameters", Json::object{{"domain_id", domain_id}, {"domain", domain.toString()}, {"times", ns3prc.d_iterations}, {"salt", ns3prc.d_salt}, {"narrow", narrow}, {"trxid", static_cast<double>(d_trxid)}, {"nonterm", nts}}},
×
799
  };
×
800

801
  Json answer;
×
802
  return this->send(query) && this->recv(answer);
×
803
}
×
804

805
bool RemoteBackend::startTransaction(const ZoneName& domain, int domain_id)
806
{
16✔
807
  this->d_trxid = time((time_t*)nullptr);
16✔
808

809
  Json query = Json::object{
16✔
810
    {"method", "startTransaction"},
16✔
811
    {"parameters", Json::object{{"domain", domain.toString()}, {"domain_id", domain_id}, {"trxid", static_cast<double>(d_trxid)}}}};
16✔
812

813
  Json answer;
16✔
814
  if (!this->send(query) || !this->recv(answer)) {
16!
815
    d_trxid = -1;
16✔
816
    return false;
16✔
817
  }
16✔
818
  return true;
×
819
}
16✔
820

821
bool RemoteBackend::commitTransaction()
822
{
16✔
823
  if (d_trxid == -1) {
16!
824
    return false;
16✔
825
  }
16✔
826

827
  Json query = Json::object{
×
828
    {"method", "commitTransaction"},
×
829
    {"parameters", Json::object{{"trxid", static_cast<double>(d_trxid)}}}};
×
830

831
  d_trxid = -1;
×
832
  Json answer;
×
833
  return this->send(query) && this->recv(answer);
×
834
}
16✔
835

836
bool RemoteBackend::abortTransaction()
837
{
×
838
  if (d_trxid == -1) {
×
839
    return false;
×
840
  }
×
841

842
  Json query = Json::object{
×
843
    {"method", "abortTransaction"},
×
844
    {"parameters", Json::object{{"trxid", static_cast<double>(d_trxid)}}}};
×
845

846
  d_trxid = -1;
×
847
  Json answer;
×
848
  return this->send(query) && this->recv(answer);
×
849
}
×
850

851
string RemoteBackend::directBackendCmd(const string& querystr)
852
{
8✔
853
  Json query = Json::object{
8✔
854
    {"method", "directBackendCmd"},
8✔
855
    {"parameters", Json::object{{"query", querystr}}}};
8✔
856

857
  Json answer;
8✔
858
  if (!this->send(query) || !this->recv(answer)) {
8!
859
    return "backend command failed";
×
860
  }
×
861

862
  return asString(answer["result"]);
8✔
863
}
8✔
864

865
bool RemoteBackend::searchRecords(const string& pattern, size_t maxResults, vector<DNSResourceRecord>& result)
866
{
×
867
  const auto intMax = static_cast<decltype(maxResults)>(std::numeric_limits<int>::max());
×
868
  if (maxResults > intMax) {
×
869
    throw std::out_of_range("Remote backend: length of list of result (" + std::to_string(maxResults) + ") is larger than what the JSON library supports for serialization (" + std::to_string(intMax) + ")");
×
870
  }
×
871

872
  Json query = Json::object{
×
873
    {"method", "searchRecords"},
×
874
    {"parameters", Json::object{{"pattern", pattern}, {"maxResults", static_cast<int>(maxResults)}}}};
×
875

876
  Json answer;
×
877
  if (!this->send(query) || !this->recv(answer)) {
×
878
    return false;
×
879
  }
×
880

881
  if (!answer["result"].is_array()) {
×
882
    return false;
×
883
  }
×
884

885
  for (const auto& row : answer["result"].array_items()) {
×
886
    DNSResourceRecord rr;
×
887
    rr.qtype = stringFromJson(row, "qtype");
×
888
    rr.qname = DNSName(stringFromJson(row, "qname"));
×
889
    rr.qclass = QClass::IN;
×
890
    rr.content = stringFromJson(row, "content");
×
891
    rr.ttl = row["ttl"].int_value();
×
892
    rr.domain_id = intFromJson(row, "domain_id", -1);
×
893
    if (d_dnssec) {
×
894
      rr.auth = (intFromJson(row, "auth", 1) != 0);
×
895
    }
×
896
    else {
×
897
      rr.auth = 1;
×
898
    }
×
899
    rr.scopeMask = row["scopeMask"].int_value();
×
900
    result.push_back(rr);
×
901
  }
×
902

903
  return true;
×
904
}
×
905

906
bool RemoteBackend::searchComments(const string& /* pattern */, size_t /* maxResults */, vector<Comment>& /* result */)
907
{
×
908
  // FIXME: Implement Comment API
909
  return false;
×
910
}
×
911

912
void RemoteBackend::getAllDomains(vector<DomainInfo>* domains, bool /* getSerial */, bool include_disabled)
913
{
×
914
  Json query = Json::object{
×
915
    {"method", "getAllDomains"},
×
916
    {"parameters", Json::object{{"include_disabled", include_disabled}}}};
×
917

918
  Json answer;
×
919
  if (!this->send(query) || !this->recv(answer)) {
×
920
    return;
×
921
  }
×
922

923
  if (!answer["result"].is_array()) {
×
924
    return;
×
925
  }
×
926

927
  for (const auto& row : answer["result"].array_items()) {
×
928
    DomainInfo di;
×
929
    this->parseDomainInfo(row, di);
×
930
    domains->push_back(di);
×
931
  }
×
932
}
×
933

934
void RemoteBackend::getUpdatedPrimaries(vector<DomainInfo>& domains, std::unordered_set<DNSName>& /* catalogs */, CatalogHashMap& /* catalogHashes */)
935
{
×
936
  Json query = Json::object{
×
937
    {"method", "getUpdatedMasters"},
×
938
    {"parameters", Json::object{}},
×
939
  };
×
940

941
  Json answer;
×
942
  if (!this->send(query) || !this->recv(answer)) {
×
943
    return;
×
944
  }
×
945

946
  if (!answer["result"].is_array()) {
×
947
    return;
×
948
  }
×
949

950
  for (const auto& row : answer["result"].array_items()) {
×
951
    DomainInfo di;
×
952
    this->parseDomainInfo(row, di);
×
953
    domains.push_back(di);
×
954
  }
×
955
}
×
956

957
void RemoteBackend::getUnfreshSecondaryInfos(vector<DomainInfo>* domains)
958
{
×
959
  Json query = Json::object{
×
960
    {"method", "getUnfreshSlaveInfos"},
×
961
    {"parameters", Json::object{}},
×
962
  };
×
963

964
  Json answer;
×
965
  if (!this->send(query) || !this->recv(answer)) {
×
966
    return;
×
967
  }
×
968

969
  if (!answer["result"].is_array()) {
×
970
    return;
×
971
  }
×
972

973
  for (const auto& row : answer["result"].array_items()) {
×
974
    DomainInfo di;
×
975
    this->parseDomainInfo(row, di);
×
976
    domains->push_back(di);
×
977
  }
×
978
}
×
979

980
void RemoteBackend::setStale(uint32_t domain_id)
981
{
×
982
  Json query = Json::object{
×
983
    {"method", "setStale"},
×
984
    {"parameters", Json::object{{"id", static_cast<double>(domain_id)}}}};
×
985

986
  Json answer;
×
987
  if (!this->send(query) || !this->recv(answer)) {
×
988
    g_log << Logger::Error << kBackendId << " Failed to execute RPC for RemoteBackend::setStale(" << domain_id << ")" << endl;
×
989
  }
×
990
}
×
991

992
void RemoteBackend::setFresh(uint32_t domain_id)
993
{
×
994
  Json query = Json::object{
×
995
    {"method", "setFresh"},
×
996
    {"parameters", Json::object{{"id", static_cast<double>(domain_id)}}}};
×
997

998
  Json answer;
×
999
  if (!this->send(query) || !this->recv(answer)) {
×
1000
    g_log << Logger::Error << kBackendId << " Failed to execute RPC for RemoteBackend::setFresh(" << domain_id << ")" << endl;
×
1001
  }
×
1002
}
×
1003

1004
DNSBackend* RemoteBackend::maker()
1005
{
×
1006
  try {
×
1007
    return new RemoteBackend();
×
1008
  }
×
1009
  catch (...) {
×
1010
    g_log << Logger::Error << kBackendId << " Unable to instantiate a remotebackend!" << endl;
×
1011
    return nullptr;
×
1012
  };
×
1013
}
×
1014

1015
class RemoteBackendFactory : public BackendFactory
1016
{
1017
public:
1018
  RemoteBackendFactory() :
1019
    BackendFactory("remote") {}
3,928✔
1020

1021
  void declareArguments(const std::string& suffix = "") override
1022
  {
32✔
1023
    declare(suffix, "dnssec", "Enable dnssec support", "no");
32✔
1024
    declare(suffix, "connection-string", "Connection string", "");
32✔
1025
  }
32✔
1026

1027
  DNSBackend* make(const std::string& suffix = "") override
1028
  {
92✔
1029
    return new RemoteBackend(suffix);
92✔
1030
  }
92✔
1031
};
1032

1033
class RemoteLoader
1034
{
1035
public:
1036
  RemoteLoader();
1037
};
1038

1039
RemoteLoader::RemoteLoader()
1040
{
3,928✔
1041
  BackendMakers().report(std::make_unique<RemoteBackendFactory>());
3,928✔
1042
  g_log << Logger::Info << kBackendId << " This is the remote backend version " VERSION
3,928✔
1043
#ifndef REPRODUCIBLE
3,928✔
1044
        << " (" __DATE__ " " __TIME__ ")"
3,928✔
1045
#endif
3,928✔
1046
        << " reporting" << endl;
3,928✔
1047
}
3,928✔
1048

1049
static RemoteLoader remoteloader;
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