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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
|
/*
* 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
* MERCHANTABILITY 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.
*/
#include <limits>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "remotebackend.hh"
// The following requirement guarantees UnknownDomainID will get output as "-1"
// in the Json data for compatibility.
static_assert(std::is_signed<domainid_t>::value);
// The following requirement is a consequency of JsonInt not able to store
// anything larger than "int".
static_assert(sizeof(domainid_t) <= sizeof(int));
static const char* kBackendId = "[RemoteBackend]";
/**
* Forwarder for value. This is just in case
* we need to do some treatment to the value before
* sending it downwards.
*/
bool Connector::send(Json& value)
{
return send_message(value) > 0;
}
/**
* Helper for handling receiving of data.
* Basically what happens here is that we check
* that the receiving happened ok, and extract
* result. Logging is performed here, too.
*/
bool Connector::recv(Json& value)
{
if (recv_message(value) > 0) {
bool retval = true;
if (value["result"] == Json()) {
throw PDNSException("No 'result' field in response from remote process");
}
if (value["result"].is_bool() && !boolFromJson(value, "result", false)) {
retval = false;
}
for (const auto& message : value["log"].array_items()) {
g_log << Logger::Info << "[remotebackend]: " << message.string_value() << std::endl;
}
return retval;
}
throw PDNSException("Unknown error while receiving data");
}
void RemoteBackend::makeErrorAndThrow(Json& value)
{
std::string msg = "Remote process indicated a failure";
for (const auto& message : value["log"].array_items()) {
msg += " '" + message.string_value() + "'";
}
throw PDNSException(msg);
}
/**
* Standard ctor and dtor
*/
RemoteBackend::RemoteBackend(const std::string& suffix)
{
setArgPrefix("remote" + suffix);
this->d_connstr = getArg("connection-string");
this->d_dnssec = mustDo("dnssec");
build();
}
RemoteBackend::~RemoteBackend() = default;
bool RemoteBackend::send(Json& value)
{
try {
if (!connector->send(value)) {
// XXX does this work work even though we throw?
this->connector.reset();
build();
throw DBException("Could not send a message to remote process");
}
}
catch (const PDNSException& ex) {
throw DBException("Exception caught when sending: " + ex.reason);
}
return true;
}
bool RemoteBackend::recv(Json& value)
{
try {
return connector->recv(value);
}
catch (const PDNSException& ex) {
this->connector.reset();
build();
throw DBException("Exception caught when receiving: " + ex.reason);
}
catch (const std::exception& e) {
this->connector.reset();
build();
throw DBException("Exception caught when receiving: " + std::string(e.what()));
}
}
/**
* Builds connector based on options
* Currently supports unix,pipe and http
*/
int RemoteBackend::build()
{
std::vector<std::string> parts;
std::string type;
std::string opts;
std::map<std::string, std::string> options;
// connstr is of format "type:options"
size_t pos = 0;
pos = d_connstr.find_first_of(':');
if (pos == std::string::npos) {
throw PDNSException("Invalid connection string: malformed");
}
type = d_connstr.substr(0, pos);
opts = d_connstr.substr(pos + 1);
// tokenize the string on comma
stringtok(parts, opts, ",");
// find out some options and parse them while we're at it
for (const auto& opt : parts) {
std::string key;
std::string val;
// make sure there is something else than air in the option...
if (opt.find_first_not_of(" ") == std::string::npos) {
continue;
}
// split it on '='. if not found, we treat it as "yes"
pos = opt.find_first_of("=");
if (pos == std::string::npos) {
key = opt;
val = "yes";
}
else {
key = opt.substr(0, pos);
val = opt.substr(pos + 1);
}
options[key] = std::move(val);
}
// connectors know what they are doing
if (type == "unix") {
this->connector = std::make_unique<UnixsocketConnector>(options);
}
else if (type == "http") {
this->connector = std::make_unique<HTTPConnector>(options);
}
else if (type == "zeromq") {
#ifdef REMOTEBACKEND_ZEROMQ
this->connector = std::make_unique<ZeroMQConnector>(options);
#else
throw PDNSException("Invalid connection string: zeromq connector support not enabled. Recompile with --enable-remotebackend-zeromq");
#endif
}
else if (type == "pipe") {
this->connector = std::make_unique<PipeConnector>(options);
}
else {
throw PDNSException("Invalid connection string: unknown connector");
}
return -1;
}
/**
* The functions here are just remote json stubs that send and receive the method call
* data is mainly left alone, some defaults are assumed.
*/
void RemoteBackend::lookup(const QType& qtype, const DNSName& qdomain, domainid_t zoneId, DNSPacket* pkt_p)
{
if (d_index != -1) {
throw PDNSException("Attempt to lookup while one running");
}
string localIP = "0.0.0.0";
string remoteIP = "0.0.0.0";
string realRemote = "0.0.0.0/0";
if (pkt_p != nullptr) {
localIP = pkt_p->getLocal().toString();
realRemote = pkt_p->getRealRemote().toString();
remoteIP = pkt_p->getInnerRemote().toString();
}
Json query = Json::object{
{"method", "lookup"},
{"parameters", Json::object{{"qtype", qtype.toString()}, {"qname", qdomain.toString()}, {"remote", remoteIP}, {"local", localIP}, {"real-remote", realRemote}, {"zone-id", zoneId}}}};
if (!this->send(query) || !this->recv(d_result)) {
return;
}
// OK. we have result parameters in result. do not process empty result.
if (!d_result["result"].is_array() || d_result["result"].array_items().empty()) {
return;
}
d_index = 0;
}
// Similar to lookup above, but passes an extra include_disabled parameter.
void RemoteBackend::APILookup(const QType& qtype, const DNSName& qdomain, domainid_t zoneId, bool include_disabled)
{
if (d_index != -1) {
throw PDNSException("Attempt to lookup while one running");
}
string localIP = "0.0.0.0";
string remoteIP = "0.0.0.0";
string realRemote = "0.0.0.0/0";
Json query = Json::object{
{"method", "APILookup"},
{"parameters", Json::object{{"qtype", qtype.toString()}, {"qname", qdomain.toString()}, {"remote", remoteIP}, {"local", localIP}, {"real-remote", realRemote}, {"zone-id", zoneId}, {"include-disabled", include_disabled}}}};
if (!this->send(query) || !this->recv(d_result)) {
return;
}
// OK. we have result parameters in result. do not process empty result.
if (!d_result["result"].is_array() || d_result["result"].array_items().empty()) {
return;
}
d_index = 0;
}
bool RemoteBackend::list(const ZoneName& target, domainid_t domain_id, bool include_disabled)
{
if (d_index != -1) {
throw PDNSException("Attempt to lookup while one running");
}
Json query = Json::object{
{"method", "list"},
{"parameters", Json::object{{"zonename", target.toString()}, {"domain_id", domain_id}, {"include_disabled", include_disabled}}}};
if (!this->send(query) || !this->recv(d_result)) {
return false;
}
if (!d_result["result"].is_array() || d_result["result"].array_items().empty()) {
return false;
}
d_index = 0;
return true;
}
bool RemoteBackend::get(DNSResourceRecord& rr)
{
if (d_index == -1) {
return false;
}
rr.qtype = stringFromJson(d_result["result"][d_index], "qtype");
rr.qname = DNSName(stringFromJson(d_result["result"][d_index], "qname"));
rr.qclass = QClass::IN;
rr.content = stringFromJson(d_result["result"][d_index], "content");
rr.ttl = d_result["result"][d_index]["ttl"].int_value();
rr.domain_id = static_cast<domainid_t>(intFromJson(d_result["result"][d_index], "domain_id", UnknownDomainID));
if (d_dnssec) {
rr.auth = (intFromJson(d_result["result"][d_index], "auth", 1) != 0);
}
else {
rr.auth = true;
}
rr.scopeMask = d_result["result"][d_index]["scopeMask"].int_value();
d_index++;
// id index is out of bounds, we know the results end here.
if (d_index == static_cast<int>(d_result["result"].array_items().size())) {
d_result = Json();
d_index = -1;
}
return true;
}
// NOLINTNEXTLINE(readability-identifier-length)
bool RemoteBackend::getBeforeAndAfterNamesAbsolute(domainid_t id, const DNSName& qname, DNSName& unhashed, DNSName& before, DNSName& after)
{
// no point doing dnssec if it's not supported
if (!d_dnssec) {
return false;
}
Json query = Json::object{
{"method", "getBeforeAndAfterNamesAbsolute"},
{"parameters", Json::object{{"id", Json(id)}, {"qname", qname.toString()}}}};
Json answer;
if (!this->send(query) || !this->recv(answer)) {
return false;
}
unhashed = DNSName(stringFromJson(answer["result"], "unhashed"));
before.clear();
after.clear();
if (answer["result"]["before"] != Json()) {
before = DNSName(stringFromJson(answer["result"], "before"));
}
if (answer["result"]["after"] != Json()) {
after = DNSName(stringFromJson(answer["result"], "after"));
}
return true;
}
bool RemoteBackend::getAllDomainMetadata(const ZoneName& name, std::map<std::string, std::vector<std::string>>& meta)
{
Json query = Json::object{
{"method", "getAllDomainMetadata"},
{"parameters", Json::object{{"name", name.toString()}}}};
if (!this->send(query)) {
return false;
}
meta.clear();
Json answer;
// not mandatory to implement
if (!this->recv(answer)) {
return true;
}
for (const auto& pair : answer["result"].object_items()) {
if (pair.second.is_array()) {
for (const auto& val : pair.second.array_items()) {
meta[pair.first].push_back(asString(val));
}
}
else {
meta[pair.first].push_back(asString(pair.second));
}
}
return true;
}
bool RemoteBackend::getDomainMetadata(const ZoneName& name, const std::string& kind, std::vector<std::string>& meta)
{
Json query = Json::object{
{"method", "getDomainMetadata"},
{"parameters", Json::object{{"name", name.toString()}, {"kind", kind}}}};
if (!this->send(query)) {
return false;
}
meta.clear();
Json answer;
// not mandatory to implement
if (!this->recv(answer)) {
return true;
}
if (answer["result"].is_array()) {
for (const auto& row : answer["result"].array_items()) {
meta.push_back(row.string_value());
}
}
else if (answer["result"].is_string()) {
meta.push_back(answer["result"].string_value());
}
return true;
}
bool RemoteBackend::setDomainMetadata(const ZoneName& name, const std::string& kind, const std::vector<std::string>& meta)
{
Json query = Json::object{
{"method", "setDomainMetadata"},
{"parameters", Json::object{{"name", name.toString()}, {"kind", kind}, {"value", meta}}}};
Json answer;
if (!this->send(query) || !this->recv(answer)) {
return false;
}
return boolFromJson(answer, "result", false);
}
bool RemoteBackend::getDomainKeys(const ZoneName& name, std::vector<DNSBackend::KeyData>& keys)
{
// no point doing dnssec if it's not supported
if (!d_dnssec) {
return false;
}
Json query = Json::object{
{"method", "getDomainKeys"},
{"parameters", Json::object{{"name", name.toString()}}}};
Json answer;
if (!this->send(query) || !this->recv(answer)) {
return false;
}
keys.clear();
for (const auto& jsonKey : answer["result"].array_items()) {
DNSBackend::KeyData key;
key.id = intFromJson(jsonKey, "id");
key.flags = intFromJson(jsonKey, "flags");
key.active = asBool(jsonKey["active"]);
key.published = boolFromJson(jsonKey, "published", true);
key.content = stringFromJson(jsonKey, "content");
keys.emplace_back(std::move(key));
}
return true;
}
bool RemoteBackend::removeDomainKey(const ZoneName& name, unsigned int keyId)
{
// no point doing dnssec if it's not supported
if (!d_dnssec) {
return false;
}
Json query = Json::object{
{"method", "removeDomainKey"},
{"parameters", Json::object{{"name", name.toString()}, {"id", static_cast<int>(keyId)}}}};
Json answer;
return this->send(query) && this->recv(answer);
}
bool RemoteBackend::addDomainKey(const ZoneName& name, const KeyData& key, int64_t& keyId)
{
// no point doing dnssec if it's not supported
if (!d_dnssec) {
return false;
}
Json query = Json::object{
{"method", "addDomainKey"},
{"parameters", Json::object{{"name", name.toString()}, {"key", Json::object{{"flags", static_cast<int>(key.flags)}, {"active", key.active}, {"published", key.published}, {"content", key.content}}}}}};
Json answer;
if (!this->send(query) || !this->recv(answer)) {
return false;
}
keyId = answer["result"].int_value();
return keyId >= 0;
}
bool RemoteBackend::activateDomainKey(const ZoneName& name, unsigned int keyId)
{
// no point doing dnssec if it's not supported
if (!d_dnssec) {
return false;
}
Json query = Json::object{
{"method", "activateDomainKey"},
{"parameters", Json::object{{"name", name.toString()}, {"id", static_cast<int>(keyId)}}}};
Json answer;
return this->send(query) && this->recv(answer);
}
bool RemoteBackend::deactivateDomainKey(const ZoneName& name, unsigned int keyId)
{
// no point doing dnssec if it's not supported
if (!d_dnssec) {
return false;
}
Json query = Json::object{
{"method", "deactivateDomainKey"},
{"parameters", Json::object{{"name", name.toString()}, {"id", static_cast<int>(keyId)}}}};
Json answer;
return this->send(query) && this->recv(answer);
}
bool RemoteBackend::publishDomainKey(const ZoneName& name, unsigned int keyId)
{
// no point doing dnssec if it's not supported
if (!d_dnssec) {
return false;
}
Json query = Json::object{
{"method", "publishDomainKey"},
{"parameters", Json::object{{"name", name.toString()}, {"id", static_cast<int>(keyId)}}}};
Json answer;
return this->send(query) && this->recv(answer);
}
bool RemoteBackend::unpublishDomainKey(const ZoneName& name, unsigned int keyId)
{
// no point doing dnssec if it's not supported
if (!d_dnssec) {
return false;
}
Json query = Json::object{
{"method", "unpublishDomainKey"},
{"parameters", Json::object{{"name", name.toString()}, {"id", static_cast<int>(keyId)}}}};
Json answer;
return this->send(query) && this->recv(answer);
}
unsigned int RemoteBackend::getCapabilities()
{
unsigned int caps = CAP_DIRECT | CAP_LIST;
if (d_dnssec) {
caps |= CAP_DNSSEC;
}
return caps;
}
bool RemoteBackend::getTSIGKey(const DNSName& name, DNSName& algorithm, std::string& content)
{
// no point doing dnssec if it's not supported
if (!d_dnssec) {
return false;
}
Json query = Json::object{
{"method", "getTSIGKey"},
{"parameters", Json::object{{"name", name.toString()}}}};
Json answer;
if (!this->send(query) || !this->recv(answer)) {
return false;
}
algorithm = DNSName(stringFromJson(answer["result"], "algorithm"));
content = stringFromJson(answer["result"], "content");
return true;
}
bool RemoteBackend::setTSIGKey(const DNSName& name, const DNSName& algorithm, const std::string& content)
{
// no point doing dnssec if it's not supported
if (!d_dnssec) {
return false;
}
Json query = Json::object{
{"method", "setTSIGKey"},
{"parameters", Json::object{{"name", name.toString()}, {"algorithm", algorithm.toString()}, {"content", content}}}};
Json answer;
return connector->send(query) && connector->recv(answer);
}
bool RemoteBackend::deleteTSIGKey(const DNSName& name)
{
// no point doing dnssec if it's not supported
if (!d_dnssec) {
return false;
}
Json query = Json::object{
{"method", "deleteTSIGKey"},
{"parameters", Json::object{{"name", name.toString()}}}};
Json answer;
return connector->send(query) && connector->recv(answer);
}
bool RemoteBackend::getTSIGKeys(std::vector<struct TSIGKey>& keys)
{
// no point doing dnssec if it's not supported
if (!d_dnssec) {
return false;
}
Json query = Json::object{
{"method", "getTSIGKeys"},
{"parameters", Json::object{}}};
Json answer;
if (!connector->send(query) || !connector->recv(answer)) {
return false;
}
for (const auto& jsonKey : answer["result"].array_items()) {
struct TSIGKey key;
key.name = DNSName(stringFromJson(jsonKey, "name"));
key.algorithm = DNSName(stringFromJson(jsonKey, "algorithm"));
key.key = stringFromJson(jsonKey, "content");
keys.push_back(key);
}
return true;
}
void RemoteBackend::parseDomainInfo(const Json& obj, DomainInfo& di)
{
di.id = static_cast<domainid_t>(intFromJson(obj, "id", UnknownDomainID));
di.zone = ZoneName(stringFromJson(obj, "zone"));
for (const auto& primary : obj["masters"].array_items()) {
di.primaries.emplace_back(primary.string_value(), 53);
}
di.notified_serial = static_cast<unsigned int>(doubleFromJson(obj, "notified_serial", 0));
di.serial = static_cast<unsigned int>(obj["serial"].number_value());
di.last_check = static_cast<time_t>(obj["last_check"].number_value());
string kind;
if (obj["kind"].is_string()) {
kind = stringFromJson(obj, "kind");
}
if (kind == "master") {
di.kind = DomainInfo::Primary;
}
else if (kind == "slave") {
di.kind = DomainInfo::Secondary;
}
else {
di.kind = DomainInfo::Native;
}
di.backend = this;
}
bool RemoteBackend::getDomainInfo(const ZoneName& domain, DomainInfo& info, bool /* getSerial */)
{
if (domain.empty()) {
return false;
}
Json query = Json::object{
{"method", "getDomainInfo"},
{"parameters", Json::object{{"name", domain.toString()}}}};
Json answer;
if (!this->send(query) || !this->recv(answer)) {
return false;
}
this->parseDomainInfo(answer["result"], info);
return true;
}
// NOLINTNEXTLINE(readability-identifier-length)
void RemoteBackend::setNotified(domainid_t id, uint32_t serial)
{
Json query = Json::object{
{"method", "setNotified"},
{"parameters", Json::object{{"id", id}, {"serial", static_cast<double>(serial)}}}};
Json answer;
if (!this->send(query) || !this->recv(answer)) {
g_log << Logger::Error << kBackendId << " Failed to execute RPC for RemoteBackend::setNotified(" << id << "," << serial << ")" << endl;
}
}
bool RemoteBackend::autoPrimaryBackend(const string& ipAddress, const ZoneName& domain, const vector<DNSResourceRecord>& nsset, string* nameserver, string* account, DNSBackend** ddb)
{
Json::array rrset;
for (const auto& ns : nsset) {
rrset.push_back(Json::object{
{"qtype", ns.qtype.toString()},
{"qname", ns.qname.toString()},
{"qclass", QClass::IN.getCode()},
{"content", ns.content},
{"ttl", static_cast<int>(ns.ttl)},
{"auth", ns.auth}});
}
Json query = Json::object{
{"method", "superMasterBackend"},
{"parameters", Json::object{{"ip", ipAddress}, {"domain", domain.toString()}, {"nsset", rrset}}}};
*ddb = nullptr;
Json answer;
if (!this->send(query) || !this->recv(answer)) {
return false;
}
// we are the backend
*ddb = this;
// we allow simple true as well...
if (answer["result"].is_object()) {
*account = stringFromJson(answer["result"], "account");
*nameserver = stringFromJson(answer["result"], "nameserver");
}
return true;
}
bool RemoteBackend::createSecondaryDomain(const string& ipAddress, const ZoneName& domain, const string& nameserver, const string& account)
{
Json query = Json::object{
{"method", "createSlaveDomain"},
{"parameters", Json::object{
{"ip", ipAddress},
{"domain", domain.toString()},
{"nameserver", nameserver},
{"account", account},
}}};
Json answer;
return this->send(query) && this->recv(answer);
}
bool RemoteBackend::replaceRRSet(domainid_t domain_id, const DNSName& qname, const QType& qtype, const vector<DNSResourceRecord>& rrset)
{
Json::array json_rrset;
for (const auto& rr : rrset) {
json_rrset.push_back(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}});
}
Json query = Json::object{
{"method", "replaceRRSet"},
{"parameters", Json::object{{"domain_id", domain_id}, {"qname", qname.toString()}, {"qtype", qtype.toString()}, {"trxid", static_cast<double>(d_trxid)}, {"rrset", json_rrset}}}};
Json answer;
return this->send(query) && this->recv(answer);
}
bool RemoteBackend::feedRecord(const DNSResourceRecord& rr, const DNSName& ordername, bool /* ordernameIsNSEC3 */)
{
Json query = Json::object{
{"method", "feedRecord"},
{"parameters", Json::object{
{"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())}}},
{"trxid", static_cast<double>(d_trxid)},
}}};
Json answer;
return this->send(query) && this->recv(answer); // XXX FIXME this API should not return 'true' I think -ahu
}
bool RemoteBackend::feedEnts(domainid_t domain_id, map<DNSName, bool>& nonterm)
{
Json::array nts;
for (const auto& t : nonterm) {
nts.push_back(Json::object{
{"nonterm", t.first.toString()},
{"auth", t.second}});
}
Json query = Json::object{
{"method", "feedEnts"},
{"parameters", Json::object{{"domain_id", domain_id}, {"trxid", static_cast<double>(d_trxid)}, {"nonterm", nts}}},
};
Json answer;
return this->send(query) && this->recv(answer);
}
bool RemoteBackend::feedEnts3(domainid_t domain_id, const DNSName& domain, map<DNSName, bool>& nonterm, const NSEC3PARAMRecordContent& ns3prc, bool narrow)
{
Json::array nts;
for (const auto& t : nonterm) {
nts.push_back(Json::object{
{"nonterm", t.first.toString()},
{"auth", t.second}});
}
Json query = Json::object{
{"method", "feedEnts3"},
{"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}}},
};
Json answer;
return this->send(query) && this->recv(answer);
}
bool RemoteBackend::startTransaction(const ZoneName& domain, domainid_t domain_id)
{
this->d_trxid = time((time_t*)nullptr);
Json query = Json::object{
{"method", "startTransaction"},
{"parameters", Json::object{{"domain", domain.toString()}, {"domain_id", domain_id}, {"trxid", static_cast<double>(d_trxid)}}}};
Json answer;
if (!this->send(query) || !this->recv(answer)) {
d_trxid = -1;
return false;
}
return true;
}
bool RemoteBackend::commitTransaction()
{
if (d_trxid == -1) {
return false;
}
Json query = Json::object{
{"method", "commitTransaction"},
{"parameters", Json::object{{"trxid", static_cast<double>(d_trxid)}}}};
d_trxid = -1;
Json answer;
return this->send(query) && this->recv(answer);
}
bool RemoteBackend::abortTransaction()
{
if (d_trxid == -1) {
return false;
}
Json query = Json::object{
{"method", "abortTransaction"},
{"parameters", Json::object{{"trxid", static_cast<double>(d_trxid)}}}};
d_trxid = -1;
Json answer;
return this->send(query) && this->recv(answer);
}
string RemoteBackend::directBackendCmd(const string& querystr)
{
Json query = Json::object{
{"method", "directBackendCmd"},
{"parameters", Json::object{{"query", querystr}}}};
Json answer;
if (!this->send(query) || !this->recv(answer)) {
return "backend command failed";
}
return asString(answer["result"]);
}
bool RemoteBackend::searchRecords(const string& pattern, size_t maxResults, vector<DNSResourceRecord>& result)
{
const auto intMax = static_cast<decltype(maxResults)>(std::numeric_limits<int>::max());
if (maxResults > intMax) {
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) + ")");
}
Json query = Json::object{
{"method", "searchRecords"},
{"parameters", Json::object{{"pattern", pattern}, {"maxResults", static_cast<int>(maxResults)}}}};
Json answer;
if (!this->send(query) || !this->recv(answer)) {
return false;
}
if (!answer["result"].is_array()) {
return false;
}
for (const auto& row : answer["result"].array_items()) {
DNSResourceRecord rr;
rr.qtype = stringFromJson(row, "qtype");
rr.qname = DNSName(stringFromJson(row, "qname"));
rr.qclass = QClass::IN;
rr.content = stringFromJson(row, "content");
rr.ttl = row["ttl"].int_value();
rr.domain_id = static_cast<domainid_t>(intFromJson(row, "domain_id", UnknownDomainID));
if (d_dnssec) {
rr.auth = (intFromJson(row, "auth", 1) != 0);
}
else {
rr.auth = 1;
}
rr.scopeMask = row["scopeMask"].int_value();
result.push_back(rr);
}
return true;
}
bool RemoteBackend::searchComments(const string& /* pattern */, size_t /* maxResults */, vector<Comment>& /* result */)
{
// FIXME: Implement Comment API
return false;
}
void RemoteBackend::getAllDomains(vector<DomainInfo>* domains, bool /* getSerial */, bool include_disabled)
{
Json query = Json::object{
{"method", "getAllDomains"},
{"parameters", Json::object{{"include_disabled", include_disabled}}}};
Json answer;
if (!this->send(query) || !this->recv(answer)) {
return;
}
if (!answer["result"].is_array()) {
return;
}
for (const auto& row : answer["result"].array_items()) {
DomainInfo di;
this->parseDomainInfo(row, di);
domains->push_back(di);
}
}
void RemoteBackend::getUpdatedPrimaries(vector<DomainInfo>& domains, std::unordered_set<DNSName>& /* catalogs */, CatalogHashMap& /* catalogHashes */)
{
Json query = Json::object{
{"method", "getUpdatedMasters"},
{"parameters", Json::object{}},
};
Json answer;
if (!this->send(query) || !this->recv(answer)) {
return;
}
if (!answer["result"].is_array()) {
return;
}
for (const auto& row : answer["result"].array_items()) {
DomainInfo di;
this->parseDomainInfo(row, di);
domains.push_back(di);
}
}
void RemoteBackend::getUnfreshSecondaryInfos(vector<DomainInfo>* domains)
{
Json query = Json::object{
{"method", "getUnfreshSlaveInfos"},
{"parameters", Json::object{}},
};
Json answer;
if (!this->send(query) || !this->recv(answer)) {
return;
}
if (!answer["result"].is_array()) {
return;
}
for (const auto& row : answer["result"].array_items()) {
DomainInfo di;
this->parseDomainInfo(row, di);
domains->push_back(di);
}
}
void RemoteBackend::setStale(domainid_t domain_id)
{
Json query = Json::object{
{"method", "setStale"},
{"parameters", Json::object{{"id", domain_id}}}};
Json answer;
if (!this->send(query) || !this->recv(answer)) {
g_log << Logger::Error << kBackendId << " Failed to execute RPC for RemoteBackend::setStale(" << domain_id << ")" << endl;
}
}
void RemoteBackend::setFresh(domainid_t domain_id)
{
Json query = Json::object{
{"method", "setFresh"},
{"parameters", Json::object{{"id", domain_id}}}};
Json answer;
if (!this->send(query) || !this->recv(answer)) {
g_log << Logger::Error << kBackendId << " Failed to execute RPC for RemoteBackend::setFresh(" << domain_id << ")" << endl;
}
}
DNSBackend* RemoteBackend::maker()
{
try {
return new RemoteBackend();
}
catch (...) {
g_log << Logger::Error << kBackendId << " Unable to instantiate a remotebackend!" << endl;
return nullptr;
};
}
class RemoteBackendFactory : public BackendFactory
{
public:
RemoteBackendFactory() :
BackendFactory("remote") {}
void declareArguments(const std::string& suffix = "") override
{
declare(suffix, "dnssec", "Enable dnssec support", "no");
declare(suffix, "connection-string", "Connection string", "");
}
DNSBackend* make(const std::string& suffix = "") override
{
return new RemoteBackend(suffix);
}
};
class RemoteLoader
{
public:
RemoteLoader();
};
RemoteLoader::RemoteLoader()
{
BackendMakers().report(std::make_unique<RemoteBackendFactory>());
g_log << Logger::Info << kBackendId << " This is the remote backend version " VERSION
#ifndef REPRODUCIBLE
<< " (" __DATE__ " " __TIME__ ")"
#endif
<< " reporting" << endl;
}
static RemoteLoader remoteloader;
|