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 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
|
/*
* 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 "dnsparser.hh"
#include "dnswriter.hh"
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
#include "namespaces.hh"
#include "noinitvector.hh"
std::atomic<bool> DNSRecordContent::d_locked{false};
UnknownRecordContent::UnknownRecordContent(const string& zone)
{
// parse the input
vector<string> parts;
stringtok(parts, zone);
// we need exactly 3 parts, except if the length field is set to 0 then we only need 2
if (parts.size() != 3 && !(parts.size() == 2 && boost::equals(parts.at(1), "0"))) {
throw MOADNSException("Unknown record was stored incorrectly, need 3 fields, got " + std::to_string(parts.size()) + ": " + zone);
}
if (parts.at(0) != "\\#") {
throw MOADNSException("Unknown record was stored incorrectly, first part should be '\\#', got '" + parts.at(0) + "'");
}
const string& relevant = (parts.size() > 2) ? parts.at(2) : "";
auto total = pdns::checked_stoi<unsigned int>(parts.at(1));
if (relevant.size() % 2 || (relevant.size() / 2) != total) {
throw MOADNSException((boost::format("invalid unknown record length: size not equal to length field (%d != 2 * %d)") % relevant.size() % total).str());
}
string out;
out.reserve(total + 1);
for (unsigned int n = 0; n < total; ++n) {
int c;
if (sscanf(&relevant.at(2*n), "%02x", &c) != 1) {
throw MOADNSException("unable to read data at position " + std::to_string(2 * n) + " from unknown record of size " + std::to_string(relevant.size()));
}
out.append(1, (char)c);
}
d_record.insert(d_record.end(), out.begin(), out.end());
}
string UnknownRecordContent::getZoneRepresentation(bool /* noDot */) const
{
ostringstream str;
str<<"\\# "<<(unsigned int)d_record.size()<<" ";
char hex[4];
for (unsigned char n : d_record) {
snprintf(hex, sizeof(hex), "%02x", n);
str << hex;
}
return str.str();
}
void UnknownRecordContent::toPacket(DNSPacketWriter& pw) const
{
pw.xfrBlob(string(d_record.begin(),d_record.end()));
}
shared_ptr<DNSRecordContent> DNSRecordContent::deserialize(const DNSName& qname, uint16_t qtype, const string& serialized, uint16_t qclass, bool internalRepresentation)
{
dnsheader dnsheader;
memset(&dnsheader, 0, sizeof(dnsheader));
dnsheader.qdcount=htons(1);
dnsheader.ancount=htons(1);
PacketBuffer packet; // build pseudo packet
/* will look like: dnsheader, 5 bytes, encoded qname, dns record header, serialized data */
const auto& encoded = qname.getStorage();
packet.resize(sizeof(dnsheader) + 5 + encoded.size() + sizeof(struct dnsrecordheader) + serialized.size());
uint16_t pos=0;
memcpy(&packet[0], &dnsheader, sizeof(dnsheader)); pos+=sizeof(dnsheader);
constexpr std::array<uint8_t, 5> tmp= {'\x0', '\x0', '\x1', '\x0', '\x1' }; // root question for ns_t_a
memcpy(&packet[pos], tmp.data(), tmp.size()); pos += tmp.size();
memcpy(&packet[pos], encoded.c_str(), encoded.size()); pos+=(uint16_t)encoded.size();
struct dnsrecordheader drh;
drh.d_type=htons(qtype);
drh.d_class=htons(qclass);
drh.d_ttl=0;
drh.d_clen=htons(serialized.size());
memcpy(&packet[pos], &drh, sizeof(drh)); pos+=sizeof(drh);
if (!serialized.empty()) {
memcpy(&packet[pos], serialized.c_str(), serialized.size());
pos += (uint16_t) serialized.size();
(void) pos;
}
DNSRecord dr;
dr.d_class = qclass;
dr.d_type = qtype;
dr.d_name = qname;
dr.d_clen = serialized.size();
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast): packet.data() is uint8_t *
PacketReader reader(std::string_view(reinterpret_cast<const char*>(packet.data()), packet.size()), packet.size() - serialized.size() - sizeof(dnsrecordheader), internalRepresentation);
/* needed to get the record boundaries right */
reader.getDnsrecordheader(drh);
auto content = DNSRecordContent::make(dr, reader, Opcode::Query);
return content;
}
std::shared_ptr<DNSRecordContent> DNSRecordContent::make(const DNSRecord& dr,
PacketReader& pr)
{
uint16_t searchclass = (dr.d_type == QType::OPT) ? 1 : dr.d_class; // class is invalid for OPT
auto i = getTypemap().find(pair(searchclass, dr.d_type));
if(i==getTypemap().end() || !i->second) {
return std::make_shared<UnknownRecordContent>(dr, pr);
}
return i->second(dr, pr);
}
std::shared_ptr<DNSRecordContent> DNSRecordContent::make(uint16_t qtype, uint16_t qclass,
const string& content)
{
auto i = getZmakermap().find(pair(qclass, qtype));
if(i==getZmakermap().end()) {
return std::make_shared<UnknownRecordContent>(content);
}
return i->second(content);
}
std::shared_ptr<DNSRecordContent> DNSRecordContent::make(const DNSRecord& dr, PacketReader& pr, uint16_t oc)
{
// For opcode UPDATE and where the DNSRecord is an answer record, we don't care about content, because this is
// not used within the prerequisite section of RFC2136, so - we can simply use unknownrecordcontent.
// For section 3.2.3, we do need content so we need to get it properly. But only for the correct QClasses.
if (oc == Opcode::Update && dr.d_place == DNSResourceRecord::ANSWER && dr.d_class != 1)
return std::make_shared<UnknownRecordContent>(dr, pr);
uint16_t searchclass = (dr.d_type == QType::OPT) ? 1 : dr.d_class; // class is invalid for OPT
auto i = getTypemap().find(pair(searchclass, dr.d_type));
if(i==getTypemap().end() || !i->second) {
return std::make_shared<UnknownRecordContent>(dr, pr);
}
return i->second(dr, pr);
}
string DNSRecordContent::upgradeContent(const DNSName& qname, const QType& qtype, const string& content) {
// seamless upgrade for previously unsupported but now implemented types.
UnknownRecordContent unknown_content(content);
shared_ptr<DNSRecordContent> rc = DNSRecordContent::deserialize(qname, qtype.getCode(), unknown_content.serialize(qname));
return rc->getZoneRepresentation();
}
DNSRecordContent::typemap_t& DNSRecordContent::getTypemap()
{
static DNSRecordContent::typemap_t typemap;
return typemap;
}
DNSRecordContent::n2typemap_t& DNSRecordContent::getN2Typemap()
{
static DNSRecordContent::n2typemap_t n2typemap;
return n2typemap;
}
DNSRecordContent::t2namemap_t& DNSRecordContent::getT2Namemap()
{
static DNSRecordContent::t2namemap_t t2namemap;
return t2namemap;
}
DNSRecordContent::zmakermap_t& DNSRecordContent::getZmakermap()
{
static DNSRecordContent::zmakermap_t zmakermap;
return zmakermap;
}
bool DNSRecordContent::isRegisteredType(uint16_t rtype, uint16_t rclass)
{
return getTypemap().count(pair(rclass, rtype)) != 0;
}
DNSRecord::DNSRecord(const DNSResourceRecord& rr): d_name(rr.qname)
{
d_type = rr.qtype.getCode();
d_ttl = rr.ttl;
d_class = rr.qclass;
d_place = DNSResourceRecord::ANSWER;
d_clen = 0;
d_content = DNSRecordContent::make(d_type, rr.qclass, rr.content);
}
// If you call this and you are not parsing a packet coming from a socket, you are doing it wrong.
DNSResourceRecord DNSResourceRecord::fromWire(const DNSRecord& wire)
{
DNSResourceRecord resourceRecord;
resourceRecord.qname = wire.d_name;
resourceRecord.qtype = QType(wire.d_type);
resourceRecord.ttl = wire.d_ttl;
resourceRecord.content = wire.getContent()->getZoneRepresentation(true);
resourceRecord.auth = false;
resourceRecord.qclass = wire.d_class;
return resourceRecord;
}
void MOADNSParser::init(bool query, const std::string_view& packet)
{
if (packet.size() < sizeof(dnsheader))
throw MOADNSException("Packet shorter than minimal header");
memcpy(&d_header, packet.data(), sizeof(dnsheader));
if(d_header.opcode != Opcode::Query && d_header.opcode != Opcode::Notify && d_header.opcode != Opcode::Update)
throw MOADNSException("Can't parse non-query packet with opcode="+ std::to_string(d_header.opcode));
d_header.qdcount=ntohs(d_header.qdcount);
d_header.ancount=ntohs(d_header.ancount);
d_header.nscount=ntohs(d_header.nscount);
d_header.arcount=ntohs(d_header.arcount);
if (query && (d_header.qdcount > 1))
throw MOADNSException("Query with QD > 1 ("+std::to_string(d_header.qdcount)+")");
unsigned int n=0;
PacketReader pr(packet);
bool validPacket=false;
try {
d_qtype = d_qclass = 0; // sometimes replies come in with no question, don't present garbage then
for(n=0;n < d_header.qdcount; ++n) {
d_qname=pr.getName();
d_qtype=pr.get16BitInt();
d_qclass=pr.get16BitInt();
}
struct dnsrecordheader ah;
vector<unsigned char> record;
bool seenTSIG = false;
validPacket=true;
d_answers.reserve((unsigned int)(d_header.ancount + d_header.nscount + d_header.arcount));
for(n=0;n < (unsigned int)(d_header.ancount + d_header.nscount + d_header.arcount); ++n) {
DNSRecord dr;
if(n < d_header.ancount)
dr.d_place=DNSResourceRecord::ANSWER;
else if(n < d_header.ancount + d_header.nscount)
dr.d_place=DNSResourceRecord::AUTHORITY;
else
dr.d_place=DNSResourceRecord::ADDITIONAL;
unsigned int recordStartPos=pr.getPosition();
DNSName name=pr.getName();
pr.getDnsrecordheader(ah);
dr.d_ttl=ah.d_ttl;
dr.d_type=ah.d_type;
dr.d_class=ah.d_class;
dr.d_name = std::move(name);
dr.d_clen = ah.d_clen;
if (query &&
!(d_qtype == QType::IXFR && dr.d_place == DNSResourceRecord::AUTHORITY && dr.d_type == QType::SOA) && // IXFR queries have a SOA in their AUTHORITY section
(dr.d_place == DNSResourceRecord::ANSWER || dr.d_place == DNSResourceRecord::AUTHORITY || (dr.d_type != QType::OPT && dr.d_type != QType::TSIG && dr.d_type != QType::SIG && dr.d_type != QType::TKEY) || ((dr.d_type == QType::TSIG || dr.d_type == QType::SIG || dr.d_type == QType::TKEY) && dr.d_class != QClass::ANY))) {
// cerr<<"discarding RR, query is "<<query<<", place is "<<dr.d_place<<", type is "<<dr.d_type<<", class is "<<dr.d_class<<endl;
dr.setContent(std::make_shared<UnknownRecordContent>(dr, pr));
}
else {
// cerr<<"parsing RR, query is "<<query<<", place is "<<dr.d_place<<", type is "<<dr.d_type<<", class is "<<dr.d_class<<endl;
dr.setContent(DNSRecordContent::make(dr, pr, d_header.opcode));
}
if (dr.d_place == DNSResourceRecord::ADDITIONAL && seenTSIG) {
throw MOADNSException("Packet ("+d_qname.toString()+"|#"+std::to_string(d_qtype)+") has an unexpected record ("+std::to_string(dr.d_type)+") after a TSIG one.");
}
if(dr.d_type == QType::TSIG && dr.d_class == QClass::ANY) {
if(seenTSIG || dr.d_place != DNSResourceRecord::ADDITIONAL) {
throw MOADNSException("Packet ("+d_qname.toLogString()+"|#"+std::to_string(d_qtype)+") has a TSIG record in an invalid position.");
}
seenTSIG = true;
d_tsigPos = recordStartPos;
}
d_answers.emplace_back(std::move(dr));
}
#if 0
if(pr.getPosition()!=packet.size()) {
throw MOADNSException("Packet ("+d_qname+"|#"+std::to_string(d_qtype)+") has trailing garbage ("+ std::to_string(pr.getPosition()) + " < " +
std::to_string(packet.size()) + ")");
}
#endif
}
catch(const std::out_of_range &re) {
if(validPacket && d_header.tc) { // don't sweat it over truncated packets, but do adjust an, ns and arcount
if(n < d_header.ancount) {
d_header.ancount=n; d_header.nscount = d_header.arcount = 0;
}
else if(n < d_header.ancount + d_header.nscount) {
d_header.nscount = n - d_header.ancount; d_header.arcount=0;
}
else {
d_header.arcount = n - d_header.ancount - d_header.nscount;
}
}
else {
throw MOADNSException("Error parsing packet of "+std::to_string(packet.size())+" bytes (rd="+
std::to_string(d_header.rd)+
"), out of bounds: "+string(re.what()));
}
}
}
bool MOADNSParser::hasEDNS() const
{
if (d_header.arcount == 0 || d_answers.empty()) {
return false;
}
for (const auto& record : d_answers) {
if (record.d_place == DNSResourceRecord::ADDITIONAL && record.d_type == QType::OPT) {
return true;
}
}
return false;
}
void PacketReader::getDnsrecordheader(struct dnsrecordheader &ah)
{
unsigned char *p = reinterpret_cast<unsigned char*>(&ah);
for(unsigned int n = 0; n < sizeof(dnsrecordheader); ++n) {
p[n] = d_content.at(d_pos++);
}
ah.d_type = ntohs(ah.d_type);
ah.d_class = ntohs(ah.d_class);
ah.d_clen = ntohs(ah.d_clen);
ah.d_ttl = ntohl(ah.d_ttl);
d_startrecordpos = d_pos; // needed for getBlob later on
d_recordlen = ah.d_clen;
}
void PacketReader::copyRecord(vector<unsigned char>& dest, uint16_t len)
{
if (len == 0) {
return;
}
if ((d_pos + len) > d_content.size()) {
throw std::out_of_range("Attempt to copy outside of packet");
}
dest.resize(len);
for (uint16_t n = 0; n < len; ++n) {
dest.at(n) = d_content.at(d_pos++);
}
}
void PacketReader::copyRecord(unsigned char* dest, uint16_t len)
{
if (d_pos + len > d_content.size()) {
throw std::out_of_range("Attempt to copy outside of packet");
}
memcpy(dest, &d_content.at(d_pos), len);
d_pos += len;
}
void PacketReader::xfrNodeOrLocatorID(NodeOrLocatorID& ret)
{
if (d_pos + sizeof(ret) > d_content.size()) {
throw std::out_of_range("Attempt to read 64 bit value outside of packet");
}
memcpy(&ret.content, &d_content.at(d_pos), sizeof(ret.content));
d_pos += sizeof(ret);
}
void PacketReader::xfr48BitInt(uint64_t& ret)
{
ret=0;
ret+=static_cast<uint8_t>(d_content.at(d_pos++));
ret<<=8;
ret+=static_cast<uint8_t>(d_content.at(d_pos++));
ret<<=8;
ret+=static_cast<uint8_t>(d_content.at(d_pos++));
ret<<=8;
ret+=static_cast<uint8_t>(d_content.at(d_pos++));
ret<<=8;
ret+=static_cast<uint8_t>(d_content.at(d_pos++));
ret<<=8;
ret+=static_cast<uint8_t>(d_content.at(d_pos++));
}
uint32_t PacketReader::get32BitInt()
{
uint32_t ret=0;
ret+=static_cast<uint8_t>(d_content.at(d_pos++));
ret<<=8;
ret+=static_cast<uint8_t>(d_content.at(d_pos++));
ret<<=8;
ret+=static_cast<uint8_t>(d_content.at(d_pos++));
ret<<=8;
ret+=static_cast<uint8_t>(d_content.at(d_pos++));
return ret;
}
uint16_t PacketReader::get16BitInt()
{
uint16_t ret=0;
ret+=static_cast<uint8_t>(d_content.at(d_pos++));
ret<<=8;
ret+=static_cast<uint8_t>(d_content.at(d_pos++));
return ret;
}
uint8_t PacketReader::get8BitInt()
{
return d_content.at(d_pos++);
}
DNSName PacketReader::getName()
{
unsigned int consumed;
try {
DNSName dn((const char*) d_content.data(), d_content.size(), d_pos, true /* uncompress */, nullptr /* qtype */, nullptr /* qclass */, &consumed, sizeof(dnsheader));
d_pos+=consumed;
return dn;
}
catch(const std::range_error& re) {
throw std::out_of_range(string("dnsname issue: ")+re.what());
}
catch(...) {
throw std::out_of_range("dnsname issue");
}
throw PDNSException("PacketReader::getName(): name is empty");
}
// FIXME see #6010 and #3503 if you want a proper solution
string txtEscape(const string &name)
{
string ret;
std::array<char, 5> ebuf{};
for (char letter : name) {
const unsigned uch = static_cast<unsigned char>(letter);
if (uch >= 127 || uch < 32) {
snprintf(ebuf.data(), ebuf.size(), "\\%03u", uch);
ret += ebuf.data();
}
else if (letter == '"' || letter == '\\'){
ret += '\\';
ret += letter;
}
else {
ret += letter;
}
}
return ret;
}
// exceptions thrown here do not result in logging in the main pdns auth server - just so you know!
string PacketReader::getText(bool multi, bool lenField)
{
string ret;
ret.reserve(40);
while(d_pos < d_startrecordpos + d_recordlen ) {
if(!ret.empty()) {
ret.append(1,' ');
}
uint16_t labellen;
if(lenField)
labellen=static_cast<uint8_t>(d_content.at(d_pos++));
else
labellen=d_recordlen - (d_pos - d_startrecordpos);
ret.append(1,'"');
if(labellen) { // no need to do anything for an empty string
string val(&d_content.at(d_pos), &d_content.at(d_pos+labellen-1)+1);
ret.append(txtEscape(val)); // the end is one beyond the packet
}
ret.append(1,'"');
d_pos+=labellen;
if(!multi)
break;
}
if (ret.empty() && !lenField) {
// all lenField == false cases (CAA and URI at the time of this writing) want that emptiness to be explicit
return "\"\"";
}
return ret;
}
string PacketReader::getUnquotedText(bool lenField)
{
uint16_t stop_at;
if(lenField)
stop_at = static_cast<uint8_t>(d_content.at(d_pos)) + d_pos + 1;
else
stop_at = d_recordlen;
/* think unsigned overflow */
if (stop_at < d_pos) {
throw std::out_of_range("getUnquotedText out of record range");
}
if(stop_at == d_pos)
return "";
d_pos++;
string ret(d_content.substr(d_pos, stop_at-d_pos));
d_pos = stop_at;
return ret;
}
void PacketReader::xfrBlob(string& blob)
{
try {
if(d_recordlen && !(d_pos == (d_startrecordpos + d_recordlen))) {
if (d_pos > (d_startrecordpos + d_recordlen)) {
throw std::out_of_range("xfrBlob out of record range");
}
blob.assign(&d_content.at(d_pos), &d_content.at(d_startrecordpos + d_recordlen - 1 ) + 1);
}
else {
blob.clear();
}
d_pos = d_startrecordpos + d_recordlen;
}
catch(...)
{
throw std::out_of_range("xfrBlob out of range");
}
}
void PacketReader::xfrBlobNoSpaces(string& blob, int length) {
xfrBlob(blob, length);
}
void PacketReader::xfrBlob(string& blob, int length)
{
if(length) {
if (length < 0) {
throw std::out_of_range("xfrBlob out of range (negative length)");
}
blob.assign(&d_content.at(d_pos), &d_content.at(d_pos + length - 1 ) + 1 );
d_pos += length;
}
else {
blob.clear();
}
}
void PacketReader::xfrSvcParamKeyVals(set<SvcParam> &kvs) {
while (d_pos < (d_startrecordpos + d_recordlen)) {
if (d_pos + 2 > (d_startrecordpos + d_recordlen)) {
throw std::out_of_range("incomplete key");
}
uint16_t keyInt;
xfr16BitInt(keyInt);
auto key = static_cast<SvcParam::SvcParamKey>(keyInt);
uint16_t len;
xfr16BitInt(len);
if (d_pos + len > (d_startrecordpos + d_recordlen)) {
throw std::out_of_range("record is shorter than SVCB lengthfield implies");
}
switch (key)
{
case SvcParam::mandatory: {
if (len % 2 != 0) {
throw std::out_of_range("mandatory SvcParam has invalid length");
}
if (len == 0) {
throw std::out_of_range("empty 'mandatory' values");
}
std::set<SvcParam::SvcParamKey> paramKeys;
size_t stop = d_pos + len;
while (d_pos < stop) {
uint16_t keyval;
xfr16BitInt(keyval);
paramKeys.insert(static_cast<SvcParam::SvcParamKey>(keyval));
}
kvs.insert(SvcParam(key, std::move(paramKeys)));
break;
}
case SvcParam::alpn: {
size_t stop = d_pos + len;
std::vector<string> alpns;
while (d_pos < stop) {
string alpn;
uint8_t alpnLen = 0;
xfr8BitInt(alpnLen);
if (alpnLen == 0) {
throw std::out_of_range("alpn length of 0");
}
xfrBlob(alpn, alpnLen);
alpns.push_back(std::move(alpn));
}
kvs.insert(SvcParam(key, std::move(alpns)));
break;
}
case SvcParam::no_default_alpn: {
if (len != 0) {
throw std::out_of_range("invalid length for no-default-alpn");
}
kvs.insert(SvcParam(key));
break;
}
case SvcParam::port: {
if (len != 2) {
throw std::out_of_range("invalid length for port");
}
uint16_t port;
xfr16BitInt(port);
kvs.insert(SvcParam(key, port));
break;
}
case SvcParam::ipv4hint: /* fall-through */
case SvcParam::ipv6hint: {
size_t addrLen = (key == SvcParam::ipv4hint ? 4 : 16);
if (len % addrLen != 0) {
throw std::out_of_range("invalid length for " + SvcParam::keyToString(key));
}
vector<ComboAddress> addresses;
auto stop = d_pos + len;
while (d_pos < stop)
{
ComboAddress addr;
xfrCAWithoutPort(key, addr);
addresses.push_back(addr);
}
// If there were no addresses, and the input comes from internal
// representation, we can reasonably assume this is the serialization
// of "auto".
bool doAuto{d_internal && len == 0};
auto param = SvcParam(key, std::move(addresses));
param.setAutoHint(doAuto);
kvs.insert(std::move(param));
break;
}
case SvcParam::ech: {
std::string blob;
blob.reserve(len);
xfrBlobNoSpaces(blob, len);
kvs.insert(SvcParam(key, blob));
break;
}
default: {
std::string blob;
blob.reserve(len);
xfrBlob(blob, len);
kvs.insert(SvcParam(key, blob));
break;
}
}
}
}
void PacketReader::xfrHexBlob(string& blob, bool /* keepReading */)
{
xfrBlob(blob);
}
//FIXME400 remove this method completely
string simpleCompress(const string& elabel, const string& root)
{
string label=elabel;
// FIXME400: this relies on the semi-canonical escaped output from getName
if(strchr(label.c_str(), '\\')) {
boost::replace_all(label, "\\.", ".");
boost::replace_all(label, "\\032", " ");
boost::replace_all(label, "\\\\", "\\");
}
typedef vector<pair<unsigned int, unsigned int> > parts_t;
parts_t parts;
vstringtok(parts, label, ".");
string ret;
ret.reserve(label.size()+4);
for(const auto & part : parts) {
if(!root.empty() && !strncasecmp(root.c_str(), label.c_str() + part.first, 1 + label.length() - part.first)) { // also match trailing 0, hence '1 +'
const unsigned char rootptr[2]={0xc0,0x11};
ret.append((const char *) rootptr, 2);
return ret;
}
ret.append(1, (char)(part.second - part.first));
ret.append(label.c_str() + part.first, part.second - part.first);
}
ret.append(1, (char)0);
return ret;
}
// method of operation: silently fail if it doesn't work - we're only trying to be nice, don't fall over on it
void editDNSPacketTTL(char* packet, size_t length, const std::function<uint32_t(uint8_t, uint16_t, uint16_t, uint32_t)>& visitor)
{
if(length < sizeof(dnsheader))
return;
try
{
dnsheader dh;
memcpy((void*)&dh, (const dnsheader*)packet, sizeof(dh));
uint64_t numrecords = ntohs(dh.ancount) + ntohs(dh.nscount) + ntohs(dh.arcount);
DNSPacketMangler dpm(packet, length);
uint64_t n;
for(n=0; n < ntohs(dh.qdcount) ; ++n) {
dpm.skipDomainName();
/* type and class */
dpm.skipBytes(4);
}
for(n=0; n < numrecords; ++n) {
dpm.skipDomainName();
uint8_t section = n < ntohs(dh.ancount) ? 1 : (n < (ntohs(dh.ancount) + ntohs(dh.nscount)) ? 2 : 3);
uint16_t dnstype = dpm.get16BitInt();
uint16_t dnsclass = dpm.get16BitInt();
if(dnstype == QType::OPT) // not getting near that one with a stick
break;
uint32_t dnsttl = dpm.get32BitInt();
uint32_t newttl = visitor(section, dnsclass, dnstype, dnsttl);
if (newttl) {
dpm.rewindBytes(sizeof(newttl));
dpm.setAndSkip32BitInt(newttl);
}
dpm.skipRData();
}
}
catch(...)
{
return;
}
}
static bool checkIfPacketContainsRecords(const PacketBuffer& packet, const std::unordered_set<QType>& qtypes)
{
auto length = packet.size();
if (length < sizeof(dnsheader)) {
return false;
}
try {
const dnsheader_aligned dh(packet.data());
DNSPacketMangler dpm(const_cast<char*>(reinterpret_cast<const char*>(packet.data())), length);
const uint16_t qdcount = ntohs(dh->qdcount);
for (size_t n = 0; n < qdcount; ++n) {
dpm.skipDomainName();
/* type and class */
dpm.skipBytes(4);
}
const size_t recordsCount = static_cast<size_t>(ntohs(dh->ancount)) + ntohs(dh->nscount) + ntohs(dh->arcount);
for (size_t n = 0; n < recordsCount; ++n) {
dpm.skipDomainName();
uint16_t dnstype = dpm.get16BitInt();
uint16_t dnsclass = dpm.get16BitInt();
if (dnsclass == QClass::IN && qtypes.count(dnstype) > 0) {
return true;
}
/* ttl */
dpm.skipBytes(4);
dpm.skipRData();
}
}
catch (...) {
}
return false;
}
static int rewritePacketWithoutRecordTypes(const PacketBuffer& initialPacket, PacketBuffer& newContent, const std::unordered_set<QType>& qtypes)
{
static const std::unordered_set<QType>& safeTypes{QType::A, QType::AAAA, QType::DHCID, QType::TXT, QType::OPT, QType::HINFO, QType::DNSKEY, QType::CDNSKEY, QType::DS, QType::CDS, QType::DLV, QType::SSHFP, QType::KEY, QType::CERT, QType::TLSA, QType::SMIMEA, QType::OPENPGPKEY, QType::SVCB, QType::HTTPS, QType::NSEC3, QType::CSYNC, QType::NSEC3PARAM, QType::LOC, QType::NID, QType::L32, QType::L64, QType::EUI48, QType::EUI64, QType::URI, QType::CAA};
if (initialPacket.size() < sizeof(dnsheader)) {
return EINVAL;
}
try {
const dnsheader_aligned dh(initialPacket.data());
if (ntohs(dh->qdcount) == 0)
return ENOENT;
auto packetView = std::string_view(reinterpret_cast<const char*>(initialPacket.data()), initialPacket.size());
PacketReader pr(packetView);
size_t idx = 0;
DNSName rrname;
uint16_t qdcount = ntohs(dh->qdcount);
uint16_t ancount = ntohs(dh->ancount);
uint16_t nscount = ntohs(dh->nscount);
uint16_t arcount = ntohs(dh->arcount);
uint16_t rrtype;
uint16_t rrclass;
string blob;
struct dnsrecordheader ah;
rrname = pr.getName();
rrtype = pr.get16BitInt();
rrclass = pr.get16BitInt();
GenericDNSPacketWriter<PacketBuffer> pw(newContent, rrname, rrtype, rrclass, dh->opcode);
pw.getHeader()->id=dh->id;
pw.getHeader()->qr=dh->qr;
pw.getHeader()->aa=dh->aa;
pw.getHeader()->tc=dh->tc;
pw.getHeader()->rd=dh->rd;
pw.getHeader()->ra=dh->ra;
pw.getHeader()->ad=dh->ad;
pw.getHeader()->cd=dh->cd;
pw.getHeader()->rcode=dh->rcode;
/* consume remaining qd if any */
if (qdcount > 1) {
for(idx = 1; idx < qdcount; idx++) {
rrname = pr.getName();
rrtype = pr.get16BitInt();
rrclass = pr.get16BitInt();
(void) rrtype;
(void) rrclass;
}
}
/* copy AN */
for (idx = 0; idx < ancount; idx++) {
rrname = pr.getName();
pr.getDnsrecordheader(ah);
pr.xfrBlob(blob);
if (qtypes.find(ah.d_type) == qtypes.end()) {
// if this is not a safe type
if (safeTypes.find(ah.d_type) == safeTypes.end()) {
// "unsafe" types might countain compressed data, so cancel rewrite
newContent.clear();
return EIO;
}
pw.startRecord(rrname, ah.d_type, ah.d_ttl, ah.d_class, DNSResourceRecord::ANSWER, true);
pw.xfrBlob(blob);
}
}
/* copy NS */
for (idx = 0; idx < nscount; idx++) {
rrname = pr.getName();
pr.getDnsrecordheader(ah);
pr.xfrBlob(blob);
if (qtypes.find(ah.d_type) == qtypes.end()) {
if (safeTypes.find(ah.d_type) == safeTypes.end()) {
// "unsafe" types might countain compressed data, so cancel rewrite
newContent.clear();
return EIO;
}
pw.startRecord(rrname, ah.d_type, ah.d_ttl, ah.d_class, DNSResourceRecord::AUTHORITY, true);
pw.xfrBlob(blob);
}
}
/* copy AR */
for (idx = 0; idx < arcount; idx++) {
rrname = pr.getName();
pr.getDnsrecordheader(ah);
pr.xfrBlob(blob);
if (qtypes.find(ah.d_type) == qtypes.end()) {
if (safeTypes.find(ah.d_type) == safeTypes.end()) {
// "unsafe" types might countain compressed data, so cancel rewrite
newContent.clear();
return EIO;
}
pw.startRecord(rrname, ah.d_type, ah.d_ttl, ah.d_class, DNSResourceRecord::ADDITIONAL, true);
pw.xfrBlob(blob);
}
}
pw.commit();
}
catch (...)
{
newContent.clear();
return EIO;
}
return 0;
}
void clearDNSPacketRecordTypes(vector<uint8_t>& packet, const std::unordered_set<QType>& qtypes)
{
return clearDNSPacketRecordTypes(reinterpret_cast<PacketBuffer&>(packet), qtypes);
}
void clearDNSPacketRecordTypes(PacketBuffer& packet, const std::unordered_set<QType>& qtypes)
{
if (!checkIfPacketContainsRecords(packet, qtypes)) {
return;
}
PacketBuffer newContent;
auto result = rewritePacketWithoutRecordTypes(packet, newContent, qtypes);
if (!result) {
packet = std::move(newContent);
}
}
// method of operation: silently fail if it doesn't work - we're only trying to be nice, don't fall over on it
void ageDNSPacket(char* packet, size_t length, uint32_t seconds, const dnsheader_aligned& aligned_dh)
{
if (length < sizeof(dnsheader)) {
return;
}
try {
const dnsheader* dhp = aligned_dh.get();
const uint64_t dqcount = ntohs(dhp->qdcount);
const uint64_t numrecords = ntohs(dhp->ancount) + ntohs(dhp->nscount) + ntohs(dhp->arcount);
DNSPacketMangler dpm(packet, length);
for (uint64_t rec = 0; rec < dqcount; ++rec) {
dpm.skipDomainName();
/* type and class */
dpm.skipBytes(4);
}
for(uint64_t rec = 0; rec < numrecords; ++rec) {
dpm.skipDomainName();
uint16_t dnstype = dpm.get16BitInt();
/* class */
dpm.skipBytes(2);
if (dnstype != QType::OPT) { // not aging that one with a stick
dpm.decreaseAndSkip32BitInt(seconds);
} else {
dpm.skipBytes(4);
}
dpm.skipRData();
}
}
catch(...) {
}
}
void ageDNSPacket(std::string& packet, uint32_t seconds, const dnsheader_aligned& aligned_dh)
{
ageDNSPacket(packet.data(), packet.length(), seconds, aligned_dh);
}
uint32_t getDNSPacketMinTTL(const char* packet, size_t length, bool* seenAuthSOA)
{
uint32_t result = std::numeric_limits<uint32_t>::max();
if(length < sizeof(dnsheader)) {
return result;
}
try
{
const dnsheader_aligned dh(packet);
DNSPacketMangler dpm(const_cast<char*>(packet), length);
const uint16_t qdcount = ntohs(dh->qdcount);
for(size_t n = 0; n < qdcount; ++n) {
dpm.skipDomainName();
/* type and class */
dpm.skipBytes(4);
}
const size_t numrecords = ntohs(dh->ancount) + ntohs(dh->nscount) + ntohs(dh->arcount);
for(size_t n = 0; n < numrecords; ++n) {
dpm.skipDomainName();
const uint16_t dnstype = dpm.get16BitInt();
/* class */
const uint16_t dnsclass = dpm.get16BitInt();
if(dnstype == QType::OPT) {
break;
}
/* report it if we see a SOA record in the AUTHORITY section */
if(dnstype == QType::SOA && dnsclass == QClass::IN && seenAuthSOA != nullptr && n >= ntohs(dh->ancount) && n < (ntohs(dh->ancount) + ntohs(dh->nscount))) {
*seenAuthSOA = true;
}
const uint32_t ttl = dpm.get32BitInt();
result = std::min(result, ttl);
dpm.skipRData();
}
}
catch(...)
{
}
return result;
}
uint32_t getDNSPacketLength(const char* packet, size_t length)
{
uint32_t result = length;
if(length < sizeof(dnsheader)) {
return result;
}
try
{
const dnsheader_aligned dh(packet);
DNSPacketMangler dpm(const_cast<char*>(packet), length);
const uint16_t qdcount = ntohs(dh->qdcount);
for(size_t n = 0; n < qdcount; ++n) {
dpm.skipDomainName();
/* type and class */
dpm.skipBytes(4);
}
const size_t numrecords = ntohs(dh->ancount) + ntohs(dh->nscount) + ntohs(dh->arcount);
for(size_t n = 0; n < numrecords; ++n) {
dpm.skipDomainName();
/* type (2), class (2) and ttl (4) */
dpm.skipBytes(8);
dpm.skipRData();
}
result = dpm.getOffset();
}
catch(...)
{
}
return result;
}
uint16_t getRecordsOfTypeCount(const char* packet, size_t length, uint8_t section, uint16_t type)
{
uint16_t result = 0;
if(length < sizeof(dnsheader)) {
return result;
}
try
{
const dnsheader_aligned dh(packet);
DNSPacketMangler dpm(const_cast<char*>(packet), length);
const uint16_t qdcount = ntohs(dh->qdcount);
for(size_t n = 0; n < qdcount; ++n) {
dpm.skipDomainName();
if (section == 0) {
uint16_t dnstype = dpm.get16BitInt();
if (dnstype == type) {
result++;
}
/* class */
dpm.skipBytes(2);
} else {
/* type and class */
dpm.skipBytes(4);
}
}
const uint16_t ancount = ntohs(dh->ancount);
for(size_t n = 0; n < ancount; ++n) {
dpm.skipDomainName();
if (section == 1) {
uint16_t dnstype = dpm.get16BitInt();
if (dnstype == type) {
result++;
}
/* class */
dpm.skipBytes(2);
} else {
/* type and class */
dpm.skipBytes(4);
}
/* ttl */
dpm.skipBytes(4);
dpm.skipRData();
}
const uint16_t nscount = ntohs(dh->nscount);
for(size_t n = 0; n < nscount; ++n) {
dpm.skipDomainName();
if (section == 2) {
uint16_t dnstype = dpm.get16BitInt();
if (dnstype == type) {
result++;
}
/* class */
dpm.skipBytes(2);
} else {
/* type and class */
dpm.skipBytes(4);
}
/* ttl */
dpm.skipBytes(4);
dpm.skipRData();
}
const uint16_t arcount = ntohs(dh->arcount);
for(size_t n = 0; n < arcount; ++n) {
dpm.skipDomainName();
if (section == 3) {
uint16_t dnstype = dpm.get16BitInt();
if (dnstype == type) {
result++;
}
/* class */
dpm.skipBytes(2);
} else {
/* type and class */
dpm.skipBytes(4);
}
/* ttl */
dpm.skipBytes(4);
dpm.skipRData();
}
}
catch(...)
{
}
return result;
}
bool getEDNSUDPPayloadSizeAndZ(const char* packet, size_t length, uint16_t* payloadSize, uint16_t* z)
{
if (length < sizeof(dnsheader)) {
return false;
}
*payloadSize = 0;
*z = 0;
try
{
const dnsheader_aligned dh(packet);
if (dh->arcount == 0) {
// The OPT pseudo-RR, if present, has to be in the additional section (https://datatracker.ietf.org/doc/html/rfc6891#section-6.1.1)
return false;
}
DNSPacketMangler dpm(const_cast<char*>(packet), length);
const uint16_t qdcount = ntohs(dh->qdcount);
for(size_t n = 0; n < qdcount; ++n) {
dpm.skipDomainName();
/* type and class */
dpm.skipBytes(4);
}
const size_t numrecords = ntohs(dh->ancount) + ntohs(dh->nscount) + ntohs(dh->arcount);
for(size_t n = 0; n < numrecords; ++n) {
dpm.skipDomainName();
const auto dnstype = dpm.get16BitInt();
if (dnstype == QType::OPT) {
const auto dnsclass = dpm.get16BitInt();
/* skip extended rcode and version */
dpm.skipBytes(2);
*z = dpm.get16BitInt();
*payloadSize = dnsclass;
return true;
}
/* skip class */
dpm.skipBytes(2);
/* TTL */
dpm.skipBytes(4);
dpm.skipRData();
}
}
catch(...)
{
}
return false;
}
bool visitDNSPacket(const std::string_view& packet, const std::function<bool(uint8_t, uint16_t, uint16_t, uint32_t, uint16_t, const char*)>& visitor)
{
if (packet.size() < sizeof(dnsheader)) {
return false;
}
try
{
const dnsheader_aligned dh(packet.data());
uint64_t numrecords = ntohs(dh->ancount) + ntohs(dh->nscount) + ntohs(dh->arcount);
PacketReader reader(packet);
uint64_t n;
for (n = 0; n < ntohs(dh->qdcount) ; ++n) {
(void) reader.getName();
/* type and class */
reader.skip(4);
}
for (n = 0; n < numrecords; ++n) {
(void) reader.getName();
uint8_t section = n < ntohs(dh->ancount) ? 1 : (n < (ntohs(dh->ancount) + ntohs(dh->nscount)) ? 2 : 3);
uint16_t dnstype = reader.get16BitInt();
uint16_t dnsclass = reader.get16BitInt();
if (dnstype == QType::OPT) {
// not getting near that one with a stick
break;
}
uint32_t dnsttl = reader.get32BitInt();
uint16_t contentLength = reader.get16BitInt();
uint16_t pos = reader.getPosition();
bool done = visitor(section, dnsclass, dnstype, dnsttl, contentLength, &packet.at(pos));
if (done) {
return true;
}
reader.skip(contentLength);
}
}
catch (...) {
return false;
}
return true;
}
|