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
|
/*
* 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 <cinttypes>
#include <iostream>
#include "filterpo.hh"
#include "namespaces.hh"
#include "dnsrecords.hh"
// Names below are RPZ Actions and end with a dot (except "Local Data")
static const std::string rpzDropName("rpz-drop."),
rpzTruncateName("rpz-tcp-only."),
rpzNoActionName("rpz-passthru."),
rpzCustomName("Local Data");
// Names below are (part) of RPZ Trigger names and do NOT end with a dot
static const std::string rpzClientIPName("rpz-client-ip"),
rpzIPName("rpz-ip"),
rpzNSDnameName("rpz-nsdname"),
rpzNSIPName("rpz-nsip");
DNSFilterEngine::DNSFilterEngine()
{
}
bool DNSFilterEngine::Zone::findExactQNamePolicy(const DNSName& qname, DNSFilterEngine::Policy& pol) const
{
return findExactNamedPolicy(d_qpolName, qname, pol);
}
bool DNSFilterEngine::Zone::findExactNSPolicy(const DNSName& qname, DNSFilterEngine::Policy& pol) const
{
return findExactNamedPolicy(d_propolName, qname, pol);
}
bool DNSFilterEngine::Zone::findNSIPPolicy(const ComboAddress& addr, DNSFilterEngine::Policy& pol) const
{
if (const auto fnd = d_propolNSAddr.lookup(addr)) {
pol = fnd->second;
return true;
}
return false;
}
bool DNSFilterEngine::Zone::findResponsePolicy(const ComboAddress& addr, DNSFilterEngine::Policy& pol) const
{
if (const auto fnd = d_postpolAddr.lookup(addr)) {
pol = fnd->second;
return true;
}
return false;
}
bool DNSFilterEngine::Zone::findClientPolicy(const ComboAddress& addr, DNSFilterEngine::Policy& pol) const
{
if (const auto fnd = d_qpolAddr.lookup(addr)) {
pol = fnd->second;
return true;
}
return false;
}
bool DNSFilterEngine::Zone::findNamedPolicy(const std::unordered_map<DNSName, DNSFilterEngine::Policy>& polmap, const DNSName& qname, DNSFilterEngine::Policy& pol)
{
if (polmap.empty()) {
return false;
}
/* for www.powerdns.com, we need to check:
www.powerdns.com.
*.powerdns.com.
*.com.
*.
*/
std::unordered_map<DNSName, DNSFilterEngine::Policy>::const_iterator iter;
iter = polmap.find(qname);
if(iter != polmap.end()) {
pol=iter->second;
return true;
}
DNSName s(qname);
while(s.chopOff()){
iter = polmap.find(g_wildcarddnsname+s);
if(iter != polmap.end()) {
pol=iter->second;
pol.d_trigger = iter->first;
pol.d_hit = qname.toStringNoDot();
return true;
}
}
return false;
}
bool DNSFilterEngine::Zone::findExactNamedPolicy(const std::unordered_map<DNSName, DNSFilterEngine::Policy>& polmap, const DNSName& qname, DNSFilterEngine::Policy& pol)
{
if (polmap.empty()) {
return false;
}
const auto& it = polmap.find(qname);
if (it != polmap.end()) {
pol = it->second;
pol.d_trigger = qname;
pol.d_hit = qname.toStringNoDot();
return true;
}
return false;
}
bool DNSFilterEngine::getProcessingPolicy(const DNSName& qname, const std::unordered_map<std::string,bool>& discardedPolicies, Policy& pol) const
{
// cout<<"Got question for nameserver name "<<qname<<endl;
std::vector<bool> zoneEnabled(d_zones.size());
size_t count = 0;
bool allEmpty = true;
for (const auto& z : d_zones) {
bool enabled = true;
const auto& zoneName = z->getName();
if (z->getPriority() >= pol.getPriority()) {
enabled = false;
}
else if (discardedPolicies.find(zoneName) != discardedPolicies.end()) {
enabled = false;
}
else {
if (z->hasNSPolicies()) {
allEmpty = false;
}
else {
enabled = false;
}
}
zoneEnabled[count] = enabled;
++count;
}
if (allEmpty) {
return false;
}
/* prepare the wildcard-based names */
std::vector<DNSName> wcNames;
wcNames.reserve(qname.countLabels());
DNSName s(qname);
while (s.chopOff()){
wcNames.emplace_back(g_wildcarddnsname+s);
}
count = 0;
for(const auto& z : d_zones) {
if (!zoneEnabled[count]) {
++count;
continue;
}
if (z->findExactNSPolicy(qname, pol)) {
// cerr<<"Had a hit on the nameserver ("<<qname<<") used to process the query"<<endl;
pol.d_trigger = qname;
pol.d_trigger.appendRawLabel(rpzNSDnameName);
pol.d_hit = qname.toStringNoDot();
return true;
}
for (const auto& wc : wcNames) {
if (z->findExactNSPolicy(wc, pol)) {
// cerr<<"Had a hit on the nameserver ("<<qname<<") used to process the query"<<endl;
pol.d_trigger = wc;
pol.d_trigger.appendRawLabel(rpzNSDnameName);
pol.d_hit = qname.toStringNoDot();
return true;
}
}
++count;
}
return false;
}
bool DNSFilterEngine::getProcessingPolicy(const ComboAddress& address, const std::unordered_map<std::string,bool>& discardedPolicies, Policy& pol) const
{
// cout<<"Got question for nameserver IP "<<address.toString()<<endl;
for(const auto& z : d_zones) {
if (z->getPriority() >= pol.getPriority()) {
break;
}
const auto& zoneName = z->getName();
if (discardedPolicies.find(zoneName) != discardedPolicies.end()) {
continue;
}
if(z->findNSIPPolicy(address, pol)) {
// cerr<<"Had a hit on the nameserver ("<<address.toString()<<") used to process the query"<<endl;
// XXX should use ns RPZ
pol.d_trigger = Zone::maskToRPZ(address);
pol.d_trigger.appendRawLabel(rpzNSIPName);
pol.d_hit = address.toString();
return true;
}
}
return false;
}
bool DNSFilterEngine::getClientPolicy(const ComboAddress& ca, const std::unordered_map<std::string,bool>& discardedPolicies, Policy& pol) const
{
// cout<<"Got question from "<<ca.toString()<<endl;
for (const auto& z : d_zones) {
if (z->getPriority() >= pol.getPriority()) {
break;
}
const auto& zoneName = z->getName();
if (discardedPolicies.find(zoneName) != discardedPolicies.end()) {
continue;
}
if (z->findClientPolicy(ca, pol)) {
// cerr<<"Had a hit on the IP address ("<<ca.toString()<<") of the client"<<endl;
return true;
}
}
return false;
}
bool DNSFilterEngine::getQueryPolicy(const DNSName& qname, const std::unordered_map<std::string,bool>& discardedPolicies, Policy& pol) const
{
//cerr<<"Got question for "<<qname<<' '<< pol.getPriority()<< endl;
std::vector<bool> zoneEnabled(d_zones.size());
size_t count = 0;
bool allEmpty = true;
for (const auto& z : d_zones) {
bool enabled = true;
if (z->getPriority() >= pol.getPriority()) {
enabled = false;
} else {
const auto& zoneName = z->getName();
if (discardedPolicies.find(zoneName) != discardedPolicies.end()) {
enabled = false;
}
else {
if (z->hasQNamePolicies()) {
allEmpty = false;
}
else {
enabled = false;
}
}
}
zoneEnabled[count] = enabled;
++count;
}
if (allEmpty) {
return false;
}
/* prepare the wildcard-based names */
std::vector<DNSName> wcNames;
wcNames.reserve(qname.countLabels());
DNSName s(qname);
while (s.chopOff()){
wcNames.emplace_back(g_wildcarddnsname+s);
}
count = 0;
for (const auto& z : d_zones) {
if (!zoneEnabled[count]) {
++count;
continue;
}
if (z->findExactQNamePolicy(qname, pol)) {
// cerr<<"Had a hit on the name of the query"<<endl;
pol.d_trigger = qname;
pol.d_hit = qname.toStringNoDot();
return true;
}
for (const auto& wc : wcNames) {
if (z->findExactQNamePolicy(wc, pol)) {
// cerr<<"Had a hit on the name of the query"<<endl;
pol.d_trigger = wc;
pol.d_hit = qname.toStringNoDot();
return true;
}
}
++count;
}
return false;
}
bool DNSFilterEngine::getPostPolicy(const vector<DNSRecord>& records, const std::unordered_map<std::string,bool>& discardedPolicies, Policy& pol) const
{
for (const auto& record : records) {
if (getPostPolicy(record, discardedPolicies, pol)) {
return true;
}
}
return false;
}
bool DNSFilterEngine::getPostPolicy(const DNSRecord& record, const std::unordered_map<std::string,bool>& discardedPolicies, Policy& pol) const
{
ComboAddress ca;
if (record.d_place != DNSResourceRecord::ANSWER) {
return false;
}
if (record.d_type == QType::A) {
if (auto rec = getRR<ARecordContent>(record)) {
ca = rec->getCA();
}
}
else if(record.d_type == QType::AAAA) {
if (auto rec = getRR<AAAARecordContent>(record)) {
ca = rec->getCA();
}
}
else {
return false;
}
for (const auto& z : d_zones) {
if (z->getPriority() >= pol.getPriority()) {
break;
}
const auto& zoneName = z->getName();
if (discardedPolicies.find(zoneName) != discardedPolicies.end()) {
return false;
}
if (z->findResponsePolicy(ca, pol)) {
pol.d_trigger = Zone::maskToRPZ(ca);
pol.d_trigger.appendRawLabel(rpzIPName);
pol.d_hit = ca.toString();
return true;
}
}
return false;
}
void DNSFilterEngine::assureZones(size_t zone)
{
if(d_zones.size() <= zone)
d_zones.resize(zone+1);
}
void DNSFilterEngine::Zone::addNameTrigger(std::unordered_map<DNSName,Policy>& map, const DNSName& n, Policy&& pol, bool ignoreDuplicate, PolicyType ptype)
{
auto it = map.find(n);
if (it != map.end()) {
auto& existingPol = it->second;
if (pol.d_kind != PolicyKind::Custom && !ignoreDuplicate) {
throw std::runtime_error("Adding a " + getTypeToString(ptype) + "-based filter policy of kind " + getKindToString(pol.d_kind) + " but a policy of kind " + getKindToString(existingPol.d_kind) + " already exists for the following name: " + n.toLogString());
}
if (existingPol.d_kind != PolicyKind::Custom && ignoreDuplicate) {
throw std::runtime_error("Adding a " + getTypeToString(ptype) + "-based filter policy of kind " + getKindToString(existingPol.d_kind) + " but there was already an existing policy for the following name: " + n.toLogString());
}
existingPol.d_custom.reserve(existingPol.d_custom.size() + pol.d_custom.size());
std::move(pol.d_custom.begin(), pol.d_custom.end(), std::back_inserter(existingPol.d_custom));
}
else {
auto& qpol = map.insert({n, std::move(pol)}).first->second;
qpol.d_zoneData = d_zoneData;
qpol.d_type = ptype;
}
}
void DNSFilterEngine::Zone::addNetmaskTrigger(NetmaskTree<Policy>& nmt, const Netmask& nm, Policy&& pol, bool ignoreDuplicate, PolicyType ptype)
{
bool exists = nmt.has_key(nm);
if (exists) {
// XXX NetMaskTree's node_type has a non-const second, but lookup() returns a const node_type *, so we cannot modify second
// Should look into making lookup) return a non-const node_type *...
auto& existingPol = const_cast<Policy&>(nmt.lookup(nm)->second);
if (pol.d_kind != PolicyKind::Custom && !ignoreDuplicate) {
throw std::runtime_error("Adding a " + getTypeToString(ptype) + "-based filter policy of kind " + getKindToString(pol.d_kind) + " but a policy of kind " + getKindToString(existingPol.d_kind) + " already exists for the following netmask: " + nm.toString());
}
if (existingPol.d_kind != PolicyKind::Custom && ignoreDuplicate) {
throw std::runtime_error("Adding a " + getTypeToString(ptype) + "-based filter policy of kind " + getKindToString(existingPol.d_kind) + " but there was already an existing policy for the following netmask: " + nm.toString());
}
existingPol.d_custom.reserve(existingPol.d_custom.size() + pol.d_custom.size());
std::move(pol.d_custom.begin(), pol.d_custom.end(), std::back_inserter(existingPol.d_custom));
}
else {
pol.d_zoneData = d_zoneData;
pol.d_type = ptype;
nmt.insert(nm).second = std::move(pol);
}
}
bool DNSFilterEngine::Zone::rmNameTrigger(std::unordered_map<DNSName,Policy>& map, const DNSName& n, const Policy& pol)
{
auto found = map.find(n);
if (found == map.end()) {
return false;
}
auto& existing = found->second;
if (existing.d_kind != DNSFilterEngine::PolicyKind::Custom) {
map.erase(found);
return true;
}
/* for custom types, we might have more than one type,
and then we need to remove only the right ones. */
bool result = false;
for (auto& toRemove : pol.d_custom) {
for (auto it = existing.d_custom.begin(); it != existing.d_custom.end(); ++it) {
if (**it == *toRemove) {
existing.d_custom.erase(it);
result = true;
break;
}
}
}
// No records left for this trigger?
if (existing.d_custom.size() == 0) {
map.erase(found);
return true;
}
return result;
}
bool DNSFilterEngine::Zone::rmNetmaskTrigger(NetmaskTree<Policy>& nmt, const Netmask& nm, const Policy& pol)
{
bool found = nmt.has_key(nm);
if (!found) {
return false;
}
// XXX NetMaskTree's node_type has a non-const second, but lookup() returns a const node_type *, so we cannot modify second
// Should look into making lookup) return a non-const node_type *...
auto& existing = const_cast<Policy&>(nmt.lookup(nm)->second);
if (existing.d_kind != DNSFilterEngine::PolicyKind::Custom) {
nmt.erase(nm);
return true;
}
/* for custom types, we might have more than one type,
and then we need to remove only the right ones. */
bool result = false;
for (auto& toRemove : pol.d_custom) {
for (auto it = existing.d_custom.begin(); it != existing.d_custom.end(); ++it) {
if (**it == *toRemove) {
existing.d_custom.erase(it);
result = true;
break;
}
}
}
// No records left for this trigger?
if (existing.d_custom.size() == 0) {
nmt.erase(nm);
return true;
}
return result;
}
void DNSFilterEngine::Zone::addClientTrigger(const Netmask& nm, Policy&& pol, bool ignoreDuplicate)
{
addNetmaskTrigger(d_qpolAddr, nm, std::move(pol), ignoreDuplicate, PolicyType::ClientIP);
}
void DNSFilterEngine::Zone::addResponseTrigger(const Netmask& nm, Policy&& pol, bool ignoreDuplicate)
{
addNetmaskTrigger(d_postpolAddr, nm, std::move(pol), ignoreDuplicate, PolicyType::ResponseIP);
}
void DNSFilterEngine::Zone::addQNameTrigger(const DNSName& n, Policy&& pol, bool ignoreDuplicate)
{
addNameTrigger(d_qpolName, n, std::move(pol), ignoreDuplicate, PolicyType::QName);
}
void DNSFilterEngine::Zone::addNSTrigger(const DNSName& n, Policy&& pol, bool ignoreDuplicate)
{
addNameTrigger(d_propolName, n, std::move(pol), ignoreDuplicate, PolicyType::NSDName);
}
void DNSFilterEngine::Zone::addNSIPTrigger(const Netmask& nm, Policy&& pol, bool ignoreDuplicate)
{
addNetmaskTrigger(d_propolNSAddr, nm, std::move(pol), ignoreDuplicate, PolicyType::NSIP);
}
bool DNSFilterEngine::Zone::rmClientTrigger(const Netmask& nm, const Policy& pol)
{
return rmNetmaskTrigger(d_qpolAddr, nm, pol);
}
bool DNSFilterEngine::Zone::rmResponseTrigger(const Netmask& nm, const Policy& pol)
{
return rmNetmaskTrigger(d_postpolAddr, nm, pol);
}
bool DNSFilterEngine::Zone::rmQNameTrigger(const DNSName& n, const Policy& pol)
{
return rmNameTrigger(d_qpolName, n, pol);
}
bool DNSFilterEngine::Zone::rmNSTrigger(const DNSName& n, const Policy& pol)
{
return rmNameTrigger(d_propolName, n, pol);
}
bool DNSFilterEngine::Zone::rmNSIPTrigger(const Netmask& nm, const Policy& pol)
{
return rmNetmaskTrigger(d_propolNSAddr, nm, pol);
}
std::string DNSFilterEngine::Policy::getLogString() const {
return ": RPZ Hit; PolicyName=" + getName() + "; Trigger=" + d_trigger.toLogString() + "; Hit=" + d_hit + "; Type=" + getTypeToString(d_type) + "; Kind=" + getKindToString(d_kind);
}
DNSRecord DNSFilterEngine::Policy::getRecordFromCustom(const DNSName& qname, const std::shared_ptr<DNSRecordContent>& custom) const
{
DNSRecord dr;
dr.d_name = qname;
dr.d_type = custom->getType();
dr.d_ttl = d_ttl;
dr.d_class = QClass::IN;
dr.d_place = DNSResourceRecord::ANSWER;
dr.d_content = custom;
if (dr.d_type == QType::CNAME) {
const auto content = std::dynamic_pointer_cast<CNAMERecordContent>(custom);
if (content) {
DNSName target = content->getTarget();
if (target.isWildcard()) {
target.chopOff();
dr.d_content = std::make_shared<CNAMERecordContent>(qname + target);
}
}
}
return dr;
}
std::vector<DNSRecord> DNSFilterEngine::Policy::getCustomRecords(const DNSName& qname, uint16_t qtype) const
{
if (d_kind != PolicyKind::Custom) {
throw std::runtime_error("Asking for a custom record from a filtering policy of a non-custom type");
}
std::vector<DNSRecord> result;
for (const auto& custom : d_custom) {
if (qtype != QType::ANY && qtype != custom->getType() && custom->getType() != QType::CNAME) {
continue;
}
DNSRecord dr;
dr.d_name = qname;
dr.d_type = custom->getType();
dr.d_ttl = d_ttl;
dr.d_class = QClass::IN;
dr.d_place = DNSResourceRecord::ANSWER;
dr.d_content = custom;
if (dr.d_type == QType::CNAME) {
const auto content = std::dynamic_pointer_cast<CNAMERecordContent>(custom);
if (content) {
DNSName target = content->getTarget();
if (target.isWildcard()) {
target.chopOff();
dr.d_content = std::make_shared<CNAMERecordContent>(qname + target);
}
}
}
result.emplace_back(getRecordFromCustom(qname, custom));
}
return result;
}
std::string DNSFilterEngine::getKindToString(DNSFilterEngine::PolicyKind kind)
{
//static const std::string rpzPrefix("rpz-");
switch(kind) {
case DNSFilterEngine::PolicyKind::NoAction:
return rpzNoActionName;
case DNSFilterEngine::PolicyKind::Drop:
return rpzDropName;
case DNSFilterEngine::PolicyKind::NXDOMAIN:
return g_rootdnsname.toString();
case PolicyKind::NODATA:
return g_wildcarddnsname.toString();
case DNSFilterEngine::PolicyKind::Truncate:
return rpzTruncateName;
case DNSFilterEngine::PolicyKind::Custom:
return rpzCustomName;
default:
throw std::runtime_error("Unexpected DNSFilterEngine::Policy kind");
}
}
std::string DNSFilterEngine::getTypeToString(DNSFilterEngine::PolicyType type)
{
switch(type) {
case DNSFilterEngine::PolicyType::None:
return "none";
case DNSFilterEngine::PolicyType::QName:
return "QName";
case DNSFilterEngine::PolicyType::ClientIP:
return "Client IP";
case DNSFilterEngine::PolicyType::ResponseIP:
return "Response IP";
case DNSFilterEngine::PolicyType::NSDName:
return "Name Server Name";
case DNSFilterEngine::PolicyType::NSIP:
return "Name Server IP";
default:
throw std::runtime_error("Unexpected DNSFilterEngine::Policy type");
}
}
std::vector<DNSRecord> DNSFilterEngine::Policy::getRecords(const DNSName& qname) const
{
std::vector<DNSRecord> result;
if (d_kind == PolicyKind::Custom) {
result = getCustomRecords(qname, QType::ANY);
}
else {
DNSRecord dr;
dr.d_name = qname;
dr.d_ttl = static_cast<uint32_t>(d_ttl);
dr.d_type = QType::CNAME;
dr.d_class = QClass::IN;
dr.d_content = DNSRecordContent::mastermake(QType::CNAME, QClass::IN, getKindToString(d_kind));
result.push_back(std::move(dr));
}
return result;
}
void DNSFilterEngine::Zone::dumpNamedPolicy(FILE* fp, const DNSName& name, const Policy& pol)
{
auto records = pol.getRecords(name);
for (const auto& dr : records) {
fprintf(fp, "%s %" PRIu32 " IN %s %s\n", dr.d_name.toString().c_str(), dr.d_ttl, QType(dr.d_type).getName().c_str(), dr.d_content->getZoneRepresentation().c_str());
}
}
DNSName DNSFilterEngine::Zone::maskToRPZ(const Netmask& nm)
{
int bits = nm.getBits();
DNSName res(std::to_string(bits));
const auto& addr = nm.getNetwork();
if (addr.isIPv4()) {
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&addr.sin4.sin_addr.s_addr);
res += DNSName(std::to_string(bytes[3]) + "." + std::to_string(bytes[2]) + "." + std::to_string(bytes[1]) + "." + std::to_string(bytes[0]));
}
else {
DNSName temp;
const auto str = addr.toString();
const auto len = str.size();
std::string::size_type begin = 0;
while (begin < len) {
std::string::size_type end = str.find(":", begin);
std::string sub;
if (end != string::npos) {
sub = str.substr(begin, end - begin);
}
else {
sub = str.substr(begin);
}
if (sub.empty()) {
temp = DNSName("zz") + temp;
}
else {
temp = DNSName(sub) + temp;
}
if (end == string::npos) {
break;
}
begin = end + 1;
}
res += temp;
}
return res;
}
void DNSFilterEngine::Zone::dumpAddrPolicy(FILE* fp, const Netmask& nm, const DNSName& name, const Policy& pol)
{
DNSName full = maskToRPZ(nm);
full += name;
auto records = pol.getRecords(full);
for (const auto& dr : records) {
fprintf(fp, "%s %" PRIu32 " IN %s %s\n", dr.d_name.toString().c_str(), dr.d_ttl, QType(dr.d_type).getName().c_str(), dr.d_content->getZoneRepresentation().c_str());
}
}
void DNSFilterEngine::Zone::dump(FILE* fp) const
{
/* fake the SOA record */
auto soa = DNSRecordContent::mastermake(QType::SOA, QClass::IN, "fake.RPZ. hostmaster.fake.RPZ. " + std::to_string(d_serial) + " " + std::to_string(d_refresh) + " 600 3600000 604800");
fprintf(fp, "%s IN SOA %s\n", d_domain.toString().c_str(), soa->getZoneRepresentation().c_str());
for (const auto& pair : d_qpolName) {
dumpNamedPolicy(fp, pair.first + d_domain, pair.second);
}
for (const auto& pair : d_propolName) {
dumpNamedPolicy(fp, pair.first + DNSName(rpzNSDnameName) + d_domain, pair.second);
}
for (const auto& pair : d_qpolAddr) {
dumpAddrPolicy(fp, pair.first, DNSName(rpzClientIPName) + d_domain, pair.second);
}
for (const auto& pair : d_propolNSAddr) {
dumpAddrPolicy(fp, pair.first, DNSName(rpzNSIPName) + d_domain, pair.second);
}
for (const auto& pair : d_postpolAddr) {
dumpAddrPolicy(fp, pair.first, DNSName(rpzIPName) + d_domain, pair.second);
}
}
void mergePolicyTags(std::unordered_set<std::string>& tags, const std::unordered_set<std::string>& newTags)
{
for (const auto& tag : newTags) {
tags.insert(tag);
}
}
|