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

PowerDNS / pdns / 19151133429

06 Nov 2025 02:49PM UTC coverage: 60.704% (-12.3%) from 73.0%
19151133429

push

github

web-flow
Merge pull request #16446 from jsoref/contributing-ai-policy

docs: Mention AI Policy in contributing pull requests

68493 of 178556 branches covered (38.36%)

Branch coverage included in aggregate %.

152002 of 184673 relevant lines covered (82.31%)

7226535.04 hits per line

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

37.77
/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
// The following requirement guarantees UnknownDomainID will get output as "-1"
29
// in the Json data for compatibility.
30
static_assert(std::is_signed<domainid_t>::value);
31

32
// The following requirement is a consequency of JsonInt not able to store
33
// anything larger than "int".
34
static_assert(sizeof(domainid_t) <= sizeof(int));
35

36
static const char* kBackendId = "[RemoteBackend]";
644✔
37

644✔
38
/**
644✔
39
 * Forwarder for value. This is just in case
40
 * we need to do some treatment to the value before
41
 * sending it downwards.
42
 */
43
bool Connector::send(Json& value)
44
{
45
  return send_message(value) > 0;
46
}
47

644✔
48
/**
644✔
49
 * Helper for handling receiving of data.
643✔
50
 * Basically what happens here is that we check
643!
51
 * that the receiving happened ok, and extract
52
 * result. Logging is performed here, too.
53
 */
643✔
54
bool Connector::recv(Json& value)
148✔
55
{
148✔
56
  if (recv_message(value) > 0) {
643✔
57
    bool retval = true;
16✔
58
    if (value["result"] == Json()) {
16!
59
      throw PDNSException("No 'result' field in response from remote process");
643✔
60
    }
643✔
61
    if (value["result"].is_bool() && !boolFromJson(value, "result", false)) {
1✔
62
      retval = false;
644✔
63
    }
64
    for (const auto& message : value["log"].array_items()) {
743✔
65
      g_log << Logger::Info << "[remotebackend]: " << message.string_value() << std::endl;
×
66
    }
×
67
    return retval;
×
68
  }
×
69
  throw PDNSException("Unknown error while receiving data");
×
70
}
×
71

72
void RemoteBackend::makeErrorAndThrow(Json& value)
73
{
74
  std::string msg = "Remote process indicated a failure";
75
  for (const auto& message : value["log"].array_items()) {
×
76
    msg += " '" + message.string_value() + "'";
77
  }
100✔
78
  throw PDNSException(msg);
100✔
79
}
80

100✔
81
/**
100✔
82
 * Standard ctor and dtor
83
 */
100✔
84
RemoteBackend::RemoteBackend(const std::string& suffix)
100✔
85
{
86
  setArgPrefix("remote" + suffix);
84✔
87

88
  this->d_connstr = getArg("connection-string");
89
  this->d_dnssec = mustDo("dnssec");
583✔
90

583✔
91
  build();
583!
92
}
93

94
RemoteBackend::~RemoteBackend() = default;
×
95

96
bool RemoteBackend::send(Json& value)
97
{
583✔
98
  try {
583✔
99
    if (!connector->send(value)) {
×
100
      // XXX does this work work even though we throw?
101
      this->connector.reset();
583✔
102
      build();
583✔
103
      throw DBException("Could not send a message to remote process");
104
    }
105
  }
583✔
106
  catch (const PDNSException& ex) {
583✔
107
    throw DBException("Exception caught when sending: " + ex.reason);
583✔
108
  }
583✔
109
  return true;
583✔
110
}
×
111

112
bool RemoteBackend::recv(Json& value)
113
{
×
114
  try {
583✔
115
    return connector->recv(value);
×
116
  }
×
117
  catch (const PDNSException& ex) {
×
118
    this->connector.reset();
×
119
    build();
583✔
120
    throw DBException("Exception caught when receiving: " + ex.reason);
121
  }
122
  catch (const std::exception& e) {
123
    this->connector.reset();
124
    build();
125
    throw DBException("Exception caught when receiving: " + std::string(e.what()));
126
  }
100✔
127
}
100✔
128

100✔
129
/**
100✔
130
 * Builds connector based on options
100✔
131
 * Currently supports unix,pipe and http
132
 */
133
int RemoteBackend::build()
100✔
134
{
100✔
135
  std::vector<std::string> parts;
100!
136
  std::string type;
×
137
  std::string opts;
×
138
  std::map<std::string, std::string> options;
139

100✔
140
  // connstr is of format "type:options"
100✔
141
  size_t pos = 0;
142
  pos = d_connstr.find_first_of(':');
143
  if (pos == std::string::npos) {
100!
144
    throw PDNSException("Invalid connection string: malformed");
145
  }
146

156✔
147
  type = d_connstr.substr(0, pos);
156✔
148
  opts = d_connstr.substr(pos + 1);
156✔
149

150
  // tokenize the string on comma
156!
151
  stringtok(parts, opts, ",");
×
152

153
  // find out some options and parse them while we're at it
154
  for (const auto& opt : parts) {
232✔
155
    std::string key;
156✔
156
    std::string val;
157
    // make sure there is something else than air in the option...
156!
158
    if (opt.find_first_not_of(" ") == std::string::npos) {
×
159
      continue;
×
160
    }
×
161

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

156✔
165
    if (pos == std::string::npos) {
156!
166
      key = opt;
156✔
167
      val = "yes";
168
    }
169
    else {
100✔
170
      key = opt.substr(0, pos);
25✔
171
      val = opt.substr(pos + 1);
25✔
172
    }
75✔
173
    options[key] = std::move(val);
25✔
174
  }
25✔
175

50✔
176
  // connectors know what they are doing
25✔
177
  if (type == "unix") {
25✔
178
    this->connector = std::make_unique<UnixsocketConnector>(options);
179
  }
180
  else if (type == "http") {
69✔
181
    this->connector = std::make_unique<HTTPConnector>(options);
25✔
182
  }
25!
183
  else if (type == "zeromq") {
25✔
184
#ifdef REMOTEBACKEND_ZEROMQ
25✔
185
    this->connector = std::make_unique<ZeroMQConnector>(options);
×
186
#else
187
    throw PDNSException("Invalid connection string: zeromq connector support not enabled. Recompile with --enable-remotebackend-zeromq");
188
#endif
189
  }
100✔
190
  else if (type == "pipe") {
100!
191
    this->connector = std::make_unique<PipeConnector>(options);
192
  }
193
  else {
194
    throw PDNSException("Invalid connection string: unknown connector");
195
  }
196

197
  return -1;
148✔
198
}
148!
199

200
/**
201
 * The functions here are just remote json stubs that send and receive the method call
202
 * data is mainly left alone, some defaults are assumed.
148✔
203
 */
148✔
204
void RemoteBackend::lookup(const QType& qtype, const DNSName& qdomain, domainid_t zoneId, DNSPacket* pkt_p)
148✔
205
{
206
  if (d_index != -1) {
148✔
207
    throw PDNSException("Attempt to lookup while one running");
56✔
208
  }
56✔
209

56✔
210
  string localIP = "0.0.0.0";
56✔
211
  string remoteIP = "0.0.0.0";
212
  string realRemote = "0.0.0.0/0";
148✔
213

148✔
214
  if (pkt_p != nullptr) {
148✔
215
    localIP = pkt_p->getLocal().toString();
216
    realRemote = pkt_p->getRealRemote().toString();
148!
217
    remoteIP = pkt_p->getInnerRemote().toString();
60✔
218
  }
60✔
219

220
  Json query = Json::object{
221
    {"method", "lookup"},
88!
222
    {"parameters", Json::object{{"qtype", qtype.toString()}, {"qname", qdomain.toString()}, {"remote", remoteIP}, {"local", localIP}, {"real-remote", realRemote}, {"zone-id", zoneId}}}};
×
223

224
  if (!this->send(query) || !this->recv(d_result)) {
296!
225
    return;
88✔
226
  }
88✔
227

228
  // OK. we have result parameters in result. do not process empty result.
229
  if (!d_result["result"].is_array() || d_result["result"].array_items().empty()) {
16!
230
    return;
16!
231
  }
×
232

233
  d_index = 0;
234
}
16✔
235

16✔
236
// Similar to lookup above, but passes an extra include_disabled parameter.
16✔
237
void RemoteBackend::APILookup(const QType& qtype, const DNSName& qdomain, domainid_t zoneId, bool include_disabled)
238
{
16!
239
  if (d_index != -1) {
×
240
    throw PDNSException("Attempt to lookup while one running");
×
241
  }
16!
242

243
  string localIP = "0.0.0.0";
×
244
  string remoteIP = "0.0.0.0";
245
  string realRemote = "0.0.0.0/0";
16✔
246

16✔
247
  Json query = Json::object{
16✔
248
    {"method", "APILookup"},
249
    {"parameters", Json::object{{"qtype", qtype.toString()}, {"qname", qdomain.toString()}, {"remote", remoteIP}, {"local", localIP}, {"real-remote", realRemote}, {"zone-id", zoneId}, {"include-disabled", include_disabled}}}};
250

468✔
251
  if (!this->send(query) || !this->recv(d_result)) {
468!
252
    return;
164✔
253
  }
164✔
254

255
  // OK. we have result parameters in result. do not process empty result.
304✔
256
  if (!d_result["result"].is_array() || d_result["result"].array_items().empty()) {
304!
257
    return;
304✔
258
  }
304✔
259

304✔
260
  d_index = 0;
304✔
261
}
304✔
262

208✔
263
bool RemoteBackend::list(const ZoneName& target, domainid_t domain_id, bool include_disabled)
208✔
264
{
96✔
265
  if (d_index != -1) {
96!
266
    throw PDNSException("Attempt to lookup while one running");
96✔
267
  }
304✔
268

304✔
269
  Json query = Json::object{
270
    {"method", "list"},
271
    {"parameters", Json::object{{"zonename", target.toString()}, {"domain_id", domain_id}, {"include_disabled", include_disabled}}}};
304✔
272

104✔
273
  if (!this->send(query) || !this->recv(d_result)) {
104!
274
    return false;
104✔
275
  }
304✔
276
  if (!d_result["result"].is_array() || d_result["result"].array_items().empty()) {
468!
277
    return false;
278
  }
279

263✔
280
  d_index = 0;
281
  return true;
263!
282
}
×
283

284
bool RemoteBackend::get(DNSResourceRecord& rr)
285
{
263✔
286
  if (d_index == -1) {
263✔
287
    return false;
263✔
288
  }
263✔
289

290
  rr.qtype = stringFromJson(d_result["result"][d_index], "qtype");
263!
291
  rr.qname = DNSName(stringFromJson(d_result["result"][d_index], "qname"));
×
292
  rr.qclass = QClass::IN;
×
293
  rr.content = stringFromJson(d_result["result"][d_index], "content");
294
  rr.ttl = d_result["result"][d_index]["ttl"].int_value();
263✔
295
  rr.domain_id = static_cast<domainid_t>(intFromJson(d_result["result"][d_index], "domain_id", UnknownDomainID));
263✔
296
  if (d_dnssec) {
263✔
297
    rr.auth = (intFromJson(d_result["result"][d_index], "auth", 1) != 0);
263✔
298
  }
262✔
299
  else {
262✔
300
    rr.auth = true;
263✔
301
  }
238✔
302
  rr.scopeMask = d_result["result"][d_index]["scopeMask"].int_value();
238✔
303
  d_index++;
304

263✔
305
  // id index is out of bounds, we know the results end here.
263✔
306
  if (d_index == static_cast<int>(d_result["result"].array_items().size())) {
314✔
307
    d_result = Json();
308
    d_index = -1;
48✔
309
  }
48✔
310
  return true;
48✔
311
}
48✔
312

313
// NOLINTNEXTLINE(readability-identifier-length)
48!
314
bool RemoteBackend::getBeforeAndAfterNamesAbsolute(domainid_t id, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after)
315
{
×
316
  // no point doing dnssec if it's not supported
317
  if (!d_dnssec) {
48!
318
    return false;
319
  }
48✔
320

321
  Json query = Json::object{
48!
322
    {"method", "getBeforeAndAfterNamesAbsolute"},
48✔
323
    {"parameters", Json::object{{"id", Json(id)}, {"qname", qname.toString()}}}};
48✔
324
  Json answer;
325

×
326
  if (!this->send(query) || !this->recv(answer)) {
×
327
    return false;
×
328
  }
×
329

330
  unhashed = DNSName(stringFromJson(answer["result"], "unhashed"));
×
331
  before.clear();
×
332
  after.clear();
×
333
  if (answer["result"]["before"] != Json()) {
×
334
    before = DNSName(stringFromJson(answer["result"], "before"));
×
335
  }
336
  if (answer["result"]["after"] != Json()) {
×
337
    after = DNSName(stringFromJson(answer["result"], "after"));
48✔
338
  }
339

340
  return true;
16✔
341
}
16✔
342

16✔
343
bool RemoteBackend::getAllDomainMetadata(const ZoneName& name, std::map<std::string, std::vector<std::string>>& meta)
16✔
344
{
345
  Json query = Json::object{
16!
346
    {"method", "getAllDomainMetadata"},
×
347
    {"parameters", Json::object{{"name", name.toString()}}}};
×
348

349
  if (!this->send(query)) {
16!
350
    return false;
351
  }
16✔
352

353
  meta.clear();
16!
354

16✔
355
  Json answer;
16✔
356
  // not mandatory to implement
357
  if (!this->recv(answer)) {
×
358
    return true;
×
359
  }
×
360

361
  for (const auto& pair : answer["result"].object_items()) {
×
362
    if (pair.second.is_array()) {
×
363
      for (const auto& val : pair.second.array_items()) {
×
364
        meta[pair.first].push_back(asString(val));
×
365
      }
366
    }
×
367
    else {
16✔
368
      meta[pair.first].push_back(asString(pair.second));
369
    }
370
  }
×
371

372
  return true;
×
373
}
×
374

375
bool RemoteBackend::getDomainMetadata(const ZoneName& name, const std::string& kind, std::vector<std::string>& meta)
376
{
×
377
  Json query = Json::object{
×
378
    {"method", "getDomainMetadata"},
×
379
    {"parameters", Json::object{{"name", name.toString()}, {"kind", kind}}}};
380

381
  if (!this->send(query)) {
×
382
    return false;
383
  }
384

48✔
385
  meta.clear();
386

48✔
387
  Json answer;
12✔
388
  // not mandatory to implement
12✔
389
  if (!this->recv(answer)) {
16!
390
    return true;
36✔
391
  }
36✔
392

36✔
393
  if (answer["result"].is_array()) {
16!
394
    for (const auto& row : answer["result"].array_items()) {
36!
395
      meta.push_back(row.string_value());
36!
396
    }
8✔
397
  }
8✔
398
  else if (answer["result"].is_string()) {
×
399
    meta.push_back(answer["result"].string_value());
28✔
400
  }
401

28✔
402
  return true;
28✔
403
}
28✔
404

28✔
405
bool RemoteBackend::setDomainMetadata(const ZoneName& name, const std::string& kind, const std::vector<std::string>& meta)
28✔
406
{
28✔
407
  Json query = Json::object{
28✔
408
    {"method", "setDomainMetadata"},
28✔
409
    {"parameters", Json::object{{"name", name.toString()}, {"kind", kind}, {"value", meta}}}};
28✔
410

411
  Json answer;
28✔
412
  if (!this->send(query) || !this->recv(answer)) {
36!
413
    return false;
414
  }
415

416
  return boolFromJson(answer, "result", false);
417
}
×
418

419
bool RemoteBackend::getDomainKeys(const ZoneName& name, std::vector<DNSBackend::KeyData>& keys)
420
{
421
  // no point doing dnssec if it's not supported
422
  if (!d_dnssec) {
✔
423
    return false;
×
424
  }
425

426
  Json query = Json::object{
×
427
    {"method", "getDomainKeys"},
×
428
    {"parameters", Json::object{{"name", name.toString()}}}};
429

430
  Json answer;
8✔
431
  if (!this->send(query) || !this->recv(answer)) {
72!
432
    return false;
8!
433
  }
×
434

435
  keys.clear();
436

8✔
437
  for (const auto& jsonKey : answer["result"].array_items()) {
8✔
438
    DNSBackend::KeyData key;
8✔
439
    key.id = intFromJson(jsonKey, "id");
440
    key.flags = intFromJson(jsonKey, "flags");
8✔
441
    key.active = asBool(jsonKey["active"]);
8!
442
    key.published = boolFromJson(jsonKey, "published", true);
×
443
    key.content = stringFromJson(jsonKey, "content");
×
444
    keys.emplace_back(std::move(key));
445
  }
8✔
446

8✔
447
  return true;
8✔
448
}
449

450
bool RemoteBackend::removeDomainKey(const ZoneName& name, unsigned int keyId)
451
{
452
  // no point doing dnssec if it's not supported
×
453
  if (!d_dnssec) {
×
454
    return false;
×
455
  }
456

457
  Json query = Json::object{
×
458
    {"method", "removeDomainKey"},
×
459
    {"parameters", Json::object{{"name", name.toString()}, {"id", static_cast<int>(keyId)}}}};
460

461
  Json answer;
×
462
  return this->send(query) && this->recv(answer);
×
463
}
464

465
bool RemoteBackend::addDomainKey(const ZoneName& name, const KeyData& key, int64_t& keyId)
466
{
467
  // no point doing dnssec if it's not supported
×
468
  if (!d_dnssec) {
×
469
    return false;
×
470
  }
471

472
  Json query = Json::object{
×
473
    {"method", "addDomainKey"},
×
474
    {"parameters", Json::object{{"name", name.toString()}, {"key", Json::object{{"flags", static_cast<int>(key.flags)}, {"active", key.active}, {"published", key.published}, {"content", key.content}}}}}};
475

476
  Json answer;
×
477
  if (!this->send(query) || !this->recv(answer)) {
×
478
    return false;
479
  }
480

481
  keyId = answer["result"].int_value();
482
  return keyId >= 0;
×
483
}
×
484

485
bool RemoteBackend::activateDomainKey(const ZoneName& name, unsigned int keyId)
486
{
×
487
  // no point doing dnssec if it's not supported
488
  if (!d_dnssec) {
×
489
    return false;
490
  }
×
491

×
492
  Json query = Json::object{
×
493
    {"method", "activateDomainKey"},
494
    {"parameters", Json::object{{"name", name.toString()}, {"id", static_cast<int>(keyId)}}}};
495

496
  Json answer;
497
  return this->send(query) && this->recv(answer);
×
498
}
×
499

500
bool RemoteBackend::deactivateDomainKey(const ZoneName& name, unsigned int keyId)
501
{
×
502
  // no point doing dnssec if it's not supported
503
  if (!d_dnssec) {
×
504
    return false;
505
  }
×
506

×
507
  Json query = Json::object{
×
508
    {"method", "deactivateDomainKey"},
509
    {"parameters", Json::object{{"name", name.toString()}, {"id", static_cast<int>(keyId)}}}};
510

68✔
511
  Json answer;
68✔
512
  return this->send(query) && this->recv(answer);
68!
513
}
514

515
bool RemoteBackend::publishDomainKey(const ZoneName& name, unsigned int keyId)
516
{
517
  // no point doing dnssec if it's not supported
×
518
  if (!d_dnssec) {
×
519
    return false;
×
520
  }
521

522
  Json query = Json::object{
×
523
    {"method", "publishDomainKey"},
×
524
    {"parameters", Json::object{{"name", name.toString()}, {"id", static_cast<int>(keyId)}}}};
525

526
  Json answer;
×
527
  return this->send(query) && this->recv(answer);
×
528
}
×
529

530
bool RemoteBackend::unpublishDomainKey(const ZoneName& name, unsigned int keyId)
531
{
×
532
  // no point doing dnssec if it's not supported
533
  if (!d_dnssec) {
×
534
    return false;
×
535
  }
536

537
  Json query = Json::object{
×
538
    {"method", "unpublishDomainKey"},
539
    {"parameters", Json::object{{"name", name.toString()}, {"id", static_cast<int>(keyId)}}}};
×
540

541
  Json answer;
×
542
  return this->send(query) && this->recv(answer);
×
543
}
×
544

545
unsigned int RemoteBackend::getCapabilities()
546
{
547
  unsigned int caps = CAP_DIRECT | CAP_LIST | CAP_SEARCH;
×
548
  if (d_dnssec) {
×
549
    caps |= CAP_DNSSEC;
×
550
  }
551
  return caps;
552
}
×
553

554
bool RemoteBackend::getTSIGKey(const DNSName& name, DNSName& algorithm, std::string& content)
×
555
{
×
556
  // no point doing dnssec if it's not supported
557
  if (!d_dnssec) {
×
558
    return false;
×
559
  }
×
560

561
  Json query = Json::object{
×
562
    {"method", "getTSIGKey"},
×
563
    {"parameters", Json::object{{"name", name.toString()}}}};
×
564

565
  Json answer;
566
  if (!this->send(query) || !this->recv(answer)) {
×
567
    return false;
568
  }
×
569

570
  algorithm = DNSName(stringFromJson(answer["result"], "algorithm"));
×
571
  content = stringFromJson(answer["result"], "content");
×
572

573
  return true;
×
574
}
575

576
bool RemoteBackend::setTSIGKey(const DNSName& name, const DNSName& algorithm, const std::string& content)
×
577
{
×
578
  // no point doing dnssec if it's not supported
579
  if (!d_dnssec) {
×
580
    return false;
×
581
  }
×
582

583
  Json query = Json::object{
×
584
    {"method", "setTSIGKey"},
×
585
    {"parameters", Json::object{{"name", name.toString()}, {"algorithm", algorithm.toString()}, {"content", content}}}};
×
586

587
  Json answer;
588
  return connector->send(query) && connector->recv(answer);
×
589
}
×
590

591
bool RemoteBackend::deleteTSIGKey(const DNSName& name)
592
{
24✔
593
  // no point doing dnssec if it's not supported
24✔
594
  if (!d_dnssec) {
24!
595
    return false;
24!
596
  }
×
597
  Json query = Json::object{
×
598
    {"method", "deleteTSIGKey"},
599
    {"parameters", Json::object{{"name", name.toString()}}}};
24✔
600

24✔
601
  Json answer;
24✔
602
  return connector->send(query) && connector->recv(answer);
×
603
}
24✔
604

24!
605
bool RemoteBackend::getTSIGKeys(std::vector<struct TSIGKey>& keys)
24✔
606
{
24✔
607
  // no point doing dnssec if it's not supported
24!
608
  if (!d_dnssec) {
×
609
    return false;
×
610
  }
24!
611
  Json query = Json::object{
×
612
    {"method", "getTSIGKeys"},
×
613
    {"parameters", Json::object{}}};
24✔
614

24✔
615
  Json answer;
24✔
616
  if (!connector->send(query) || !connector->recv(answer)) {
24!
617
    return false;
24✔
618
  }
619

620
  for (const auto& jsonKey : answer["result"].array_items()) {
24!
621
    struct TSIGKey key;
24!
622
    key.name = DNSName(stringFromJson(jsonKey, "name"));
×
623
    key.algorithm = DNSName(stringFromJson(jsonKey, "algorithm"));
×
624
    key.key = stringFromJson(jsonKey, "content");
625
    keys.push_back(key);
24✔
626
  }
24✔
627

24✔
628
  return true;
629
}
24✔
630

24!
631
void RemoteBackend::parseDomainInfo(const Json& obj, DomainInfo& di)
632
{
×
633
  di.id = static_cast<domainid_t>(intFromJson(obj, "id", UnknownDomainID));
634
  di.zone = ZoneName(stringFromJson(obj, "zone"));
24✔
635
  for (const auto& primary : obj["masters"].array_items()) {
24!
636
    di.primaries.emplace_back(primary.string_value(), 53);
24✔
637
  }
638

639
  di.notified_serial = static_cast<unsigned int>(doubleFromJson(obj, "notified_serial", 0));
×
640
  di.serial = static_cast<unsigned int>(obj["serial"].number_value());
×
641
  di.last_check = static_cast<time_t>(obj["last_check"].number_value());
×
642

643
  string kind;
644
  if (obj["kind"].is_string()) {
×
645
    kind = stringFromJson(obj, "kind");
×
646
  }
×
647
  if (kind == "master") {
×
648
    di.kind = DomainInfo::Primary;
×
649
  }
650
  else if (kind == "slave") {
24!
651
    di.kind = DomainInfo::Secondary;
×
652
  }
×
653
  else {
654
    di.kind = DomainInfo::Native;
×
655
  }
×
656
  di.backend = this;
×
657
}
×
658

659
bool RemoteBackend::getDomainInfo(const ZoneName& domain, DomainInfo& info, bool /* getSerial */)
660
{
×
661
  if (domain.empty()) {
×
662
    return false;
×
663
  }
664

665
  Json query = Json::object{
×
666
    {"method", "getDomainInfo"},
×
667
    {"parameters", Json::object{{"name", domain.toString()}}}};
668

669
  Json answer;
670
  if (!this->send(query) || !this->recv(answer)) {
×
671
    return false;
×
672
  }
×
673

674
  this->parseDomainInfo(answer["result"], info);
675
  return true;
676
}
×
677

678
// NOLINTNEXTLINE(readability-identifier-length)
679
void RemoteBackend::setNotified(domainid_t id, uint32_t serial)
×
680
{
×
681
  Json query = Json::object{
×
682
    {"method", "setNotified"},
×
683
    {"parameters", Json::object{{"id", id}, {"serial", static_cast<double>(serial)}}}};
684

685
  Json answer;
×
686
  if (!this->send(query) || !this->recv(answer)) {
×
687
    g_log << Logger::Error << kBackendId << " Failed to execute RPC for RemoteBackend::setNotified(" << id << "," << serial << ")" << endl;
688
  }
×
689
}
×
690

691
bool RemoteBackend::autoPrimaryBackend(const string& ipAddress, const ZoneName& domain, const vector<DNSResourceRecord>& nsset, string* nameserver, string* account, DNSBackend** ddb)
692
{
×
693
  Json::array rrset;
×
694

695
  for (const auto& ns : nsset) {
×
696
    rrset.push_back(Json::object{
×
697
      {"qtype", ns.qtype.toString()},
698
      {"qname", ns.qname.toString()},
×
699
      {"qclass", QClass::IN.getCode()},
×
700
      {"content", ns.content},
×
701
      {"ttl", static_cast<int>(ns.ttl)},
702
      {"auth", ns.auth}});
703
  }
×
704

705
  Json query = Json::object{
×
706
    {"method", "superMasterBackend"},
×
707
    {"parameters", Json::object{{"ip", ipAddress}, {"domain", domain.toString()}, {"nsset", rrset}}}};
×
708

709
  *ddb = nullptr;
×
710

711
  Json answer;
×
712
  if (!this->send(query) || !this->recv(answer)) {
×
713
    return false;
×
714
  }
715

716
  // we are the backend
717
  *ddb = this;
×
718

719
  // we allow simple true as well...
720
  if (answer["result"].is_object()) {
×
721
    *account = stringFromJson(answer["result"], "account");
×
722
    *nameserver = stringFromJson(answer["result"], "nameserver");
723
  }
724

725
  return true;
×
726
}
×
727

728
bool RemoteBackend::createSecondaryDomain(const string& ipAddress, const ZoneName& domain, const string& nameserver, const string& account)
×
729
{
×
730
  Json query = Json::object{
×
731
    {"method", "createSlaveDomain"},
732
    {"parameters", Json::object{
×
733
                     {"ip", ipAddress},
×
734
                     {"domain", domain.toString()},
×
735
                     {"nameserver", nameserver},
736
                     {"account", account},
737
                   }}};
×
738

739
  Json answer;
740
  return this->send(query) && this->recv(answer);
×
741
}
×
742

743
bool RemoteBackend::replaceRRSet(domainid_t domain_id, const DNSName& qname, const QType& qtype, const vector<DNSResourceRecord>& rrset)
744
{
×
745
  Json::array json_rrset;
746
  for (const auto& rr : rrset) {
×
747
    json_rrset.push_back(Json::object{
×
748
      {"qtype", rr.qtype.toString()},
×
749
      {"qname", rr.qname.toString()},
×
750
      {"qclass", QClass::IN.getCode()},
751
      {"content", rr.content},
×
752
      {"ttl", static_cast<int>(rr.ttl)},
×
753
      {"auth", rr.auth}});
×
754
  }
755

756
  Json query = Json::object{
×
757
    {"method", "replaceRRSet"},
×
758
    {"parameters", Json::object{{"domain_id", domain_id}, {"qname", qname.toString()}, {"qtype", qtype.toString()}, {"trxid", static_cast<double>(d_trxid)}, {"rrset", json_rrset}}}};
759

×
760
  Json answer;
×
761
  return this->send(query) && this->recv(answer);
×
762
}
×
763

764
bool RemoteBackend::feedRecord(const DNSResourceRecord& rr, const DNSName& ordername, bool /* ordernameIsNSEC3 */)
765
{
×
766
  Json query = Json::object{
×
767
    {"method", "feedRecord"},
×
768
    {"parameters", Json::object{
×
769
                     {"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())}}},
×
770
                     {"trxid", static_cast<double>(d_trxid)},
×
771
                   }}};
×
772

773
  Json answer;
774
  return this->send(query) && this->recv(answer); // XXX FIXME this API should not return 'true' I think -ahu
×
775
}
16✔
776

16✔
777
bool RemoteBackend::feedEnts(domainid_t domain_id, map<DNSName, bool>& nonterm)
778
{
16✔
779
  Json::array nts;
16✔
780

16✔
781
  for (const auto& t : nonterm) {
×
782
    nts.push_back(Json::object{
16✔
783
      {"nonterm", t.first.toString()},
16!
784
      {"auth", t.second}});
16✔
785
  }
16✔
786

16✔
787
  Json query = Json::object{
×
788
    {"method", "feedEnts"},
16✔
789
    {"parameters", Json::object{{"domain_id", domain_id}, {"trxid", static_cast<double>(d_trxid)}, {"nonterm", nts}}},
790
  };
791

16✔
792
  Json answer;
16!
793
  return this->send(query) && this->recv(answer);
16!
794
}
16✔
795

796
bool RemoteBackend::feedEnts3(domainid_t domain_id, const DNSName& domain, map<DNSName, bool>& nonterm, const NSEC3PARAMRecordContent& ns3prc, bool narrow)
797
{
×
798
  Json::array nts;
×
799

800
  for (const auto& t : nonterm) {
×
801
    nts.push_back(Json::object{
×
802
      {"nonterm", t.first.toString()},
×
803
      {"auth", t.second}});
16✔
804
  }
805

806
  Json query = Json::object{
×
807
    {"method", "feedEnts3"},
×
808
    {"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}}},
×
809
  };
×
810

811
  Json answer;
×
812
  return this->send(query) && this->recv(answer);
×
813
}
×
814

815
bool RemoteBackend::startTransaction(const ZoneName& domain, domainid_t domain_id)
816
{
×
817
  this->d_trxid = time((time_t*)nullptr);
×
818

819
  Json query = Json::object{
820
    {"method", "startTransaction"},
821
    {"parameters", Json::object{{"domain", domain.toString()}, {"domain_id", domain_id}, {"trxid", static_cast<double>(d_trxid)}}}};
8✔
822

8✔
823
  Json answer;
8✔
824
  if (!this->send(query) || !this->recv(answer)) {
8!
825
    d_trxid = -1;
826
    return false;
8✔
827
  }
8!
828
  return true;
×
829
}
×
830

831
bool RemoteBackend::commitTransaction()
8✔
832
{
8✔
833
  if (d_trxid == -1) {
16!
834
    return false;
835
  }
×
836

837
  Json query = Json::object{
×
838
    {"method", "commitTransaction"},
×
839
    {"parameters", Json::object{{"trxid", static_cast<double>(d_trxid)}}}};
×
840

841
  d_trxid = -1;
×
842
  Json answer;
×
843
  return this->send(query) && this->recv(answer);
×
844
}
845

846
bool RemoteBackend::abortTransaction()
×
847
{
×
848
  if (d_trxid == -1) {
×
849
    return false;
850
  }
×
851

852
  Json query = Json::object{
×
853
    {"method", "abortTransaction"},
854
    {"parameters", Json::object{{"trxid", static_cast<double>(d_trxid)}}}};
×
855

856
  d_trxid = -1;
×
857
  Json answer;
×
858
  return this->send(query) && this->recv(answer);
×
859
}
×
860

861
string RemoteBackend::directBackendCmd(const string& querystr)
862
{
×
863
  Json query = Json::object{
×
864
    {"method", "directBackendCmd"},
×
865
    {"parameters", Json::object{{"query", querystr}}}};
×
866

867
  Json answer;
×
868
  if (!this->send(query) || !this->recv(answer)) {
×
869
    return "backend command failed";
×
870
  }
×
871

872
  return asString(answer["result"]);
×
873
}
×
874

875
bool RemoteBackend::searchRecords(const string& pattern, size_t maxResults, vector<DNSResourceRecord>& result)
876
{
×
877
  const auto intMax = static_cast<decltype(maxResults)>(std::numeric_limits<int>::max());
878
  if (maxResults > intMax) {
×
879
    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) + ")");
×
880
  }
881

882
  Json query = Json::object{
×
883
    {"method", "searchRecords"},
×
884
    {"parameters", Json::object{{"pattern", pattern}, {"maxResults", static_cast<int>(maxResults)}}}};
×
885

886
  Json answer;
887
  if (!this->send(query) || !this->recv(answer)) {
×
888
    return false;
×
889
  }
×
890

891
  if (!answer["result"].is_array()) {
×
892
    return false;
×
893
  }
×
894

895
  for (const auto& row : answer["result"].array_items()) {
×
896
    DNSResourceRecord rr;
×
897
    rr.qtype = stringFromJson(row, "qtype");
×
898
    rr.qname = DNSName(stringFromJson(row, "qname"));
×
899
    rr.qclass = QClass::IN;
×
900
    rr.content = stringFromJson(row, "content");
×
901
    rr.ttl = row["ttl"].int_value();
×
902
    rr.domain_id = static_cast<domainid_t>(intFromJson(row, "domain_id", UnknownDomainID));
903
    if (d_dnssec) {
×
904
      rr.auth = (intFromJson(row, "auth", 1) != 0);
×
905
    }
×
906
    else {
×
907
      rr.auth = 1;
×
908
    }
×
909
    rr.scopeMask = row["scopeMask"].int_value();
910
    result.push_back(rr);
×
911
  }
×
912

913
  return true;
×
914
}
915

×
916
bool RemoteBackend::searchComments(const string& /* pattern */, size_t /* maxResults */, vector<Comment>& /* result */)
917
{
×
918
  // FIXME: Implement Comment API
919
  return false;
×
920
}
×
921

922
void RemoteBackend::getAllDomains(vector<DomainInfo>* domains, bool /* getSerial */, bool include_disabled)
923
{
×
924
  Json query = Json::object{
×
925
    {"method", "getAllDomains"},
926
    {"parameters", Json::object{{"include_disabled", include_disabled}}}};
927

928
  Json answer;
×
929
  if (!this->send(query) || !this->recv(answer)) {
×
930
    return;
×
931
  }
×
932

933
  if (!answer["result"].is_array()) {
×
934
    return;
×
935
  }
×
936

937
  for (const auto& row : answer["result"].array_items()) {
×
938
    DomainInfo di;
×
939
    this->parseDomainInfo(row, di);
×
940
    domains->push_back(di);
×
941
  }
942
}
×
943

944
void RemoteBackend::getUpdatedPrimaries(vector<DomainInfo>& domains, std::unordered_set<DNSName>& /* catalogs */, CatalogHashMap& /* catalogHashes */)
945
{
×
946
  Json query = Json::object{
×
947
    {"method", "getUpdatedMasters"},
×
948
    {"parameters", Json::object{}},
949
  };
950

951
  Json answer;
×
952
  if (!this->send(query) || !this->recv(answer)) {
×
953
    return;
×
954
  }
955

956
  if (!answer["result"].is_array()) {
×
957
    return;
×
958
  }
×
959

960
  for (const auto& row : answer["result"].array_items()) {
×
961
    DomainInfo di;
962
    this->parseDomainInfo(row, di);
×
963
    domains.push_back(di);
×
964
  }
×
965
}
×
966

967
void RemoteBackend::getUnfreshSecondaryInfos(vector<DomainInfo>* domains)
968
{
×
969
  Json query = Json::object{
×
970
    {"method", "getUnfreshSlaveInfos"},
×
971
    {"parameters", Json::object{}},
×
972
  };
973

974
  Json answer;
×
975
  if (!this->send(query) || !this->recv(answer)) {
×
976
    return;
×
977
  }
×
978

979
  if (!answer["result"].is_array()) {
×
980
    return;
×
981
  }
×
982

983
  for (const auto& row : answer["result"].array_items()) {
×
984
    DomainInfo di;
985
    this->parseDomainInfo(row, di);
986
    domains->push_back(di);
987
  }
988
}
376✔
989

990
void RemoteBackend::setStale(domainid_t domain_id)
991
{
32✔
992
  Json query = Json::object{
32✔
993
    {"method", "setStale"},
32✔
994
    {"parameters", Json::object{{"id", domain_id}}}};
32✔
995

996
  Json answer;
997
  if (!this->send(query) || !this->recv(answer)) {
100!
998
    g_log << Logger::Error << kBackendId << " Failed to execute RPC for RemoteBackend::setStale(" << domain_id << ")" << endl;
100✔
999
  }
100✔
1000
}
1001

1002
void RemoteBackend::setFresh(domainid_t domain_id)
1003
{
1004
  Json query = Json::object{
1005
    {"method", "setFresh"},
1006
    {"parameters", Json::object{{"id", domain_id}}}};
1007

1008
  Json answer;
1009
  if (!this->send(query) || !this->recv(answer)) {
376!
1010
    g_log << Logger::Error << kBackendId << " Failed to execute RPC for RemoteBackend::setFresh(" << domain_id << ")" << endl;
376✔
1011
  }
376✔
1012
}
376✔
1013

376✔
1014
class RemoteBackendFactory : public BackendFactory
376✔
1015
{
376✔
1016
public:
376✔
1017
  RemoteBackendFactory() :
1018
    BackendFactory("remote") {}
1019

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

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

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

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

1048
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