]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnsrecords.hh
Merge pull request #7015 from rgacogne/dnsdist-protobuf-serverid
[thirdparty/pdns.git] / pdns / dnsrecords.hh
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #ifndef PDNS_DNSRECORDS_HH
23 #define PDNS_DNSRECORDS_HH
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "dnsparser.hh"
30 #include "dnswriter.hh"
31 #include "rcpgenerator.hh"
32 #include <set>
33 #include <bitset>
34 #include "namespaces.hh"
35 #include "iputils.hh"
36
37 #define includeboilerplate(RNAME) RNAME##RecordContent(const DNSRecord& dr, PacketReader& pr); \
38 RNAME##RecordContent(const string& zoneData); \
39 static void report(void); \
40 static void unreport(void); \
41 static DNSRecordContent* make(const DNSRecord &dr, PacketReader& pr); \
42 static DNSRecordContent* make(const string& zonedata); \
43 string getZoneRepresentation(bool noDot=false) const override; \
44 void toPacket(DNSPacketWriter& pw) override; \
45 uint16_t getType() const override { return QType::RNAME; } \
46 template<class Convertor> void xfrPacket(Convertor& conv, bool noDot=false);
47
48 class NAPTRRecordContent : public DNSRecordContent
49 {
50 public:
51 NAPTRRecordContent(uint16_t order, uint16_t preference, string flags, string services, string regexp, DNSName replacement);
52
53 includeboilerplate(NAPTR)
54 template<class Convertor> void xfrRecordContent(Convertor& conv);
55 private:
56 uint16_t d_order, d_preference;
57 string d_flags, d_services, d_regexp;
58 DNSName d_replacement;
59 };
60
61
62 class ARecordContent : public DNSRecordContent
63 {
64 public:
65 explicit ARecordContent(const ComboAddress& ca);
66 explicit ARecordContent(uint32_t ip);
67 includeboilerplate(A)
68 void doRecordCheck(const DNSRecord& dr);
69 ComboAddress getCA(int port=0) const;
70 bool operator==(const DNSRecordContent& rhs) const override
71 {
72 if(typeid(*this) != typeid(rhs))
73 return false;
74 return d_ip == dynamic_cast<const ARecordContent&>(rhs).d_ip;
75 }
76 private:
77 uint32_t d_ip;
78 };
79
80 class AAAARecordContent : public DNSRecordContent
81 {
82 public:
83 AAAARecordContent(std::string &val);
84 explicit AAAARecordContent(const ComboAddress& ca);
85 includeboilerplate(AAAA)
86 ComboAddress getCA(int port=0) const;
87 bool operator==(const DNSRecordContent& rhs) const override
88 {
89 if(typeid(*this) != typeid(rhs))
90 return false;
91 return d_ip6 == dynamic_cast<const decltype(this)>(&rhs)->d_ip6;
92 }
93 private:
94 string d_ip6; // why??
95 };
96
97 class MXRecordContent : public DNSRecordContent
98 {
99 public:
100 MXRecordContent(uint16_t preference, const DNSName& mxname);
101
102 includeboilerplate(MX)
103
104 uint16_t d_preference;
105 DNSName d_mxname;
106
107 bool operator==(const DNSRecordContent& rhs) const override
108 {
109 if(typeid(*this) != typeid(rhs))
110 return false;
111 auto rrhs =dynamic_cast<const decltype(this)>(&rhs);
112 return std::tie(d_preference, d_mxname) == std::tie(rrhs->d_preference, rrhs->d_mxname);
113 }
114
115 };
116
117 class KXRecordContent : public DNSRecordContent
118 {
119 public:
120 KXRecordContent(uint16_t preference, const DNSName& exchanger);
121
122 includeboilerplate(KX)
123
124 private:
125 uint16_t d_preference;
126 DNSName d_exchanger;
127 };
128
129 class IPSECKEYRecordContent : public DNSRecordContent
130 {
131 public:
132 IPSECKEYRecordContent(uint16_t preference, uint8_t gatewaytype, uint8_t algo, const DNSName& gateway, const string& publickey);
133
134 includeboilerplate(IPSECKEY)
135
136 private:
137 uint32_t d_ip4;
138 DNSName d_gateway;
139 string d_publickey;
140 string d_ip6;
141 uint8_t d_preference, d_gatewaytype, d_algorithm;
142 };
143
144 class DHCIDRecordContent : public DNSRecordContent
145 {
146 public:
147 includeboilerplate(DHCID)
148
149 private:
150 string d_content;
151 };
152
153
154 class SRVRecordContent : public DNSRecordContent
155 {
156 public:
157 SRVRecordContent(uint16_t preference, uint16_t weight, uint16_t port, const DNSName& target);
158
159 includeboilerplate(SRV)
160
161 uint16_t d_weight, d_port;
162 DNSName d_target;
163 uint16_t d_preference;
164 };
165
166 class TSIGRecordContent : public DNSRecordContent
167 {
168 public:
169 includeboilerplate(TSIG)
170 TSIGRecordContent() {}
171
172 uint16_t d_origID{0};
173 uint16_t d_fudge{0};
174
175 DNSName d_algoName;
176 string d_mac;
177 string d_otherData;
178 uint64_t d_time{0};
179 // uint16_t d_macSize;
180 uint16_t d_eRcode{0};
181 // uint16_t d_otherLen
182 };
183
184
185 class TXTRecordContent : public DNSRecordContent
186 {
187 public:
188 includeboilerplate(TXT)
189
190 string d_text;
191 };
192
193 #ifdef HAVE_LUA_RECORDS
194 class LUARecordContent : public DNSRecordContent
195 {
196 public:
197 includeboilerplate(LUA)
198 string getCode();
199 uint16_t d_type;
200 string d_code;
201 };
202 #endif
203
204 class ENTRecordContent : public DNSRecordContent
205 {
206 public:
207 includeboilerplate(ENT)
208 };
209
210 class SPFRecordContent : public DNSRecordContent
211 {
212 public:
213 includeboilerplate(SPF)
214 const std::string& getText() const
215 {
216 return d_text;
217 }
218
219 private:
220 string d_text;
221 };
222
223
224 class NSRecordContent : public DNSRecordContent
225 {
226 public:
227 includeboilerplate(NS)
228 explicit NSRecordContent(const DNSName& content) : d_content(content){}
229 const DNSName& getNS() const { return d_content; }
230 bool operator==(const DNSRecordContent& rhs) const override
231 {
232 if(typeid(*this) != typeid(rhs))
233 return false;
234 auto rrhs =dynamic_cast<const decltype(this)>(&rhs);
235 return d_content == rrhs->d_content;
236 }
237
238 private:
239 DNSName d_content;
240 };
241
242 class PTRRecordContent : public DNSRecordContent
243 {
244 public:
245 includeboilerplate(PTR)
246 explicit PTRRecordContent(const DNSName& content) : d_content(content){}
247 const DNSName& getContent() const { return d_content; }
248 private:
249 DNSName d_content;
250 };
251
252 class CNAMERecordContent : public DNSRecordContent
253 {
254 public:
255 includeboilerplate(CNAME)
256 CNAMERecordContent(const DNSName& content) : d_content(content){}
257 DNSName getTarget() const { return d_content; }
258 private:
259 DNSName d_content;
260 };
261
262 class ALIASRecordContent : public DNSRecordContent
263 {
264 public:
265 includeboilerplate(ALIAS)
266
267 DNSName d_content;
268 };
269
270
271 class DNAMERecordContent : public DNSRecordContent
272 {
273 public:
274 includeboilerplate(DNAME)
275 DNSName d_content;
276 };
277
278
279 class MBRecordContent : public DNSRecordContent
280 {
281 public:
282 includeboilerplate(MB)
283
284 private:
285 DNSName d_madname;
286 };
287
288 class MGRecordContent : public DNSRecordContent
289 {
290 public:
291 includeboilerplate(MG)
292
293 private:
294 DNSName d_mgmname;
295 };
296
297 class MRRecordContent : public DNSRecordContent
298 {
299 public:
300 includeboilerplate(MR)
301
302 private:
303 DNSName d_alias;
304 };
305
306 class MINFORecordContent : public DNSRecordContent
307 {
308 public:
309 includeboilerplate(MINFO)
310
311 private:
312 DNSName d_rmailbx;
313 DNSName d_emailbx;
314 };
315
316 class OPTRecordContent : public DNSRecordContent
317 {
318 public:
319 OPTRecordContent(){}
320 includeboilerplate(OPT)
321 void getData(vector<pair<uint16_t, string> > &opts);
322 private:
323 string d_data;
324 };
325
326
327 class HINFORecordContent : public DNSRecordContent
328 {
329 public:
330 includeboilerplate(HINFO)
331
332 private:
333 string d_cpu, d_host;
334 };
335
336 class RPRecordContent : public DNSRecordContent
337 {
338 public:
339 includeboilerplate(RP)
340
341 private:
342 DNSName d_mbox, d_info;
343 };
344
345
346 class DNSKEYRecordContent : public DNSRecordContent
347 {
348 public:
349 DNSKEYRecordContent();
350 includeboilerplate(DNSKEY)
351 uint16_t getTag() const;
352 uint16_t getTag();
353
354 uint16_t d_flags{0};
355 uint8_t d_protocol{0};
356 uint8_t d_algorithm{0};
357 string d_key;
358 bool operator<(const DNSKEYRecordContent& rhs) const
359 {
360 return tie(d_flags, d_protocol, d_algorithm, d_key) <
361 tie(rhs.d_flags, rhs.d_protocol, rhs.d_algorithm, rhs.d_key);
362 }
363 };
364
365 class CDNSKEYRecordContent : public DNSRecordContent
366 {
367 public:
368 CDNSKEYRecordContent();
369 includeboilerplate(CDNSKEY)
370 uint16_t getTag();
371
372 uint16_t d_flags{0};
373 uint8_t d_protocol{0};
374 uint8_t d_algorithm{0};
375 string d_key;
376 };
377
378 class DSRecordContent : public DNSRecordContent
379 {
380 public:
381 DSRecordContent();
382 bool operator==(const DNSRecordContent& rhs) const override
383 {
384 if(typeid(*this) != typeid(rhs))
385 return false;
386 auto rrhs =dynamic_cast<const decltype(this)>(&rhs);
387 return tie(d_tag, d_algorithm, d_digesttype, d_digest) ==
388 tie(rrhs->d_tag, rrhs->d_algorithm, rrhs->d_digesttype, rrhs->d_digest);
389 }
390 bool operator<(const DSRecordContent& rhs) const
391 {
392 return tie(d_tag, d_algorithm, d_digesttype, d_digest) <
393 tie(rhs.d_tag, rhs.d_algorithm, rhs.d_digesttype, rhs.d_digest);
394 }
395
396 includeboilerplate(DS)
397
398 uint16_t d_tag{0};
399 uint8_t d_algorithm{0}, d_digesttype{0};
400 string d_digest;
401 };
402
403 class CDSRecordContent : public DNSRecordContent
404 {
405 public:
406 CDSRecordContent();
407 includeboilerplate(CDS)
408
409 uint16_t d_tag{0};
410 uint8_t d_algorithm{0}, d_digesttype{0};
411 string d_digest;
412 };
413
414 class DLVRecordContent : public DNSRecordContent
415 {
416 public:
417 DLVRecordContent();
418 includeboilerplate(DLV)
419
420 uint16_t d_tag{0};
421 uint8_t d_algorithm{0}, d_digesttype{0};
422 string d_digest;
423 };
424
425
426 class SSHFPRecordContent : public DNSRecordContent
427 {
428 public:
429 includeboilerplate(SSHFP)
430
431 private:
432 uint8_t d_algorithm, d_fptype;
433 string d_fingerprint;
434 };
435
436 class KEYRecordContent : public DNSRecordContent
437 {
438 public:
439 includeboilerplate(KEY)
440
441 private:
442 uint16_t d_flags;
443 uint8_t d_protocol, d_algorithm;
444 string d_certificate;
445 };
446
447 class AFSDBRecordContent : public DNSRecordContent
448 {
449 public:
450 includeboilerplate(AFSDB)
451
452 private:
453 uint16_t d_subtype;
454 DNSName d_hostname;
455 };
456
457
458 class CERTRecordContent : public DNSRecordContent
459 {
460 public:
461 includeboilerplate(CERT)
462
463 private:
464 uint16_t d_type, d_tag;
465 string d_certificate;
466 uint8_t d_algorithm;
467 };
468
469 class TLSARecordContent : public DNSRecordContent
470 {
471 public:
472 includeboilerplate(TLSA)
473
474 private:
475 uint8_t d_certusage, d_selector, d_matchtype;
476 string d_cert;
477 };
478
479 class SMIMEARecordContent : public DNSRecordContent
480 {
481 public:
482 includeboilerplate(SMIMEA)
483
484 private:
485 uint8_t d_certusage, d_selector, d_matchtype;
486 string d_cert;
487 };
488
489 class OPENPGPKEYRecordContent : public DNSRecordContent
490 {
491 public:
492 includeboilerplate(OPENPGPKEY)
493
494 private:
495 string d_keyring;
496 };
497
498
499 class RRSIGRecordContent : public DNSRecordContent
500 {
501 public:
502 RRSIGRecordContent();
503 includeboilerplate(RRSIG)
504
505 uint16_t d_type{0};
506 uint16_t d_tag{0};
507 DNSName d_signer;
508 string d_signature;
509 uint32_t d_originalttl{0}, d_sigexpire{0}, d_siginception{0};
510 uint8_t d_algorithm{0}, d_labels{0};
511 };
512
513 //namespace {
514 struct soatimes
515 {
516 uint32_t serial;
517 uint32_t refresh;
518 uint32_t retry;
519 uint32_t expire;
520 uint32_t minimum;
521 };
522 //}
523
524 class RKEYRecordContent : public DNSRecordContent
525 {
526 public:
527 RKEYRecordContent();
528 includeboilerplate(RKEY)
529 uint16_t d_flags{0};
530 uint8_t d_protocol{0}, d_algorithm{0};
531 string d_key;
532 };
533
534 class SOARecordContent : public DNSRecordContent
535 {
536 public:
537 includeboilerplate(SOA)
538 SOARecordContent(const DNSName& mname, const DNSName& rname, const struct soatimes& st);
539
540 DNSName d_mname;
541 DNSName d_rname;
542 struct soatimes d_st;
543 };
544
545 class NSECRecordContent : public DNSRecordContent
546 {
547 public:
548 static void report(void);
549 NSECRecordContent()
550 {}
551 NSECRecordContent(const string& content, const string& zone=""); //FIXME400: DNSName& zone?
552
553 static DNSRecordContent* make(const DNSRecord &dr, PacketReader& pr);
554 static DNSRecordContent* make(const string& content);
555 string getZoneRepresentation(bool noDot=false) const override;
556 void toPacket(DNSPacketWriter& pw) override;
557 uint16_t getType() const override
558 {
559 return QType::NSEC;
560 }
561 DNSName d_next;
562 std::set<uint16_t> d_set;
563 private:
564 };
565
566 class NSEC3RecordContent : public DNSRecordContent
567 {
568 public:
569 static void report(void);
570 NSEC3RecordContent()
571 {}
572 NSEC3RecordContent(const string& content, const string& zone=""); //FIXME400: DNSName& zone?
573
574 static DNSRecordContent* make(const DNSRecord &dr, PacketReader& pr);
575 static DNSRecordContent* make(const string& content);
576 string getZoneRepresentation(bool noDot=false) const override;
577 void toPacket(DNSPacketWriter& pw) override;
578
579 uint8_t d_algorithm{0}, d_flags{0};
580 uint16_t d_iterations{0};
581 string d_salt;
582 string d_nexthash;
583 std::set<uint16_t> d_set;
584
585 uint16_t getType() const override
586 {
587 return QType::NSEC3;
588 }
589
590
591 private:
592 };
593
594
595 class NSEC3PARAMRecordContent : public DNSRecordContent
596 {
597 public:
598 static void report(void);
599 NSEC3PARAMRecordContent()
600 {}
601 NSEC3PARAMRecordContent(const string& content, const string& zone=""); // FIXME400: DNSName& zone?
602
603 static DNSRecordContent* make(const DNSRecord &dr, PacketReader& pr);
604 static DNSRecordContent* make(const string& content);
605 string getZoneRepresentation(bool noDot=false) const override;
606 void toPacket(DNSPacketWriter& pw) override;
607
608 uint16_t getType() const override
609 {
610 return QType::NSEC3PARAM;
611 }
612
613
614 uint8_t d_algorithm{0}, d_flags{0};
615 uint16_t d_iterations{0};
616 string d_salt;
617 };
618
619
620 class LOCRecordContent : public DNSRecordContent
621 {
622 public:
623 static void report(void);
624 LOCRecordContent()
625 {}
626 LOCRecordContent(const string& content, const string& zone="");
627
628 static DNSRecordContent* make(const DNSRecord &dr, PacketReader& pr);
629 static DNSRecordContent* make(const string& content);
630 string getZoneRepresentation(bool noDot=false) const override;
631 void toPacket(DNSPacketWriter& pw) override;
632
633 uint8_t d_version{0}, d_size{0}, d_horizpre{0}, d_vertpre{0};
634 uint32_t d_latitude{0}, d_longitude{0}, d_altitude{0};
635 uint16_t getType() const override
636 {
637 return QType::LOC;
638 }
639
640 private:
641 };
642
643
644 class WKSRecordContent : public DNSRecordContent
645 {
646 public:
647 static void report(void);
648 WKSRecordContent()
649 {}
650 WKSRecordContent(const string& content, const string& zone=""); // FIXME400: DNSName& zone?
651
652 static DNSRecordContent* make(const DNSRecord &dr, PacketReader& pr);
653 static DNSRecordContent* make(const string& content);
654 string getZoneRepresentation(bool noDot=false) const override;
655 void toPacket(DNSPacketWriter& pw) override;
656
657 uint32_t d_ip{0};
658 std::bitset<65535> d_services;
659 private:
660 };
661
662 class EUI48RecordContent : public DNSRecordContent
663 {
664 public:
665 EUI48RecordContent() {};
666 static void report(void);
667 static DNSRecordContent* make(const DNSRecord &dr, PacketReader& pr);
668 static DNSRecordContent* make(const string& zone); // FIXME400: DNSName& zone?
669 string getZoneRepresentation(bool noDot=false) const override;
670 void toPacket(DNSPacketWriter& pw) override;
671 uint16_t getType() const override { return QType::EUI48; }
672 private:
673 // storage for the bytes
674 uint8_t d_eui48[6];
675 };
676
677 class EUI64RecordContent : public DNSRecordContent
678 {
679 public:
680 EUI64RecordContent() {};
681 static void report(void);
682 static DNSRecordContent* make(const DNSRecord &dr, PacketReader& pr);
683 static DNSRecordContent* make(const string& zone); // FIXME400: DNSName& zone?
684 string getZoneRepresentation(bool noDot=false) const override;
685 void toPacket(DNSPacketWriter& pw) override;
686 uint16_t getType() const override { return QType::EUI64; }
687 private:
688 // storage for the bytes
689 uint8_t d_eui64[8];
690 };
691
692 class TKEYRecordContent : public DNSRecordContent
693 {
694 public:
695 TKEYRecordContent();
696 includeboilerplate(TKEY)
697
698 // storage for the bytes
699 uint16_t d_othersize{0};
700 uint16_t d_mode{0};
701 uint32_t d_inception{0};
702 uint32_t d_expiration{0};
703
704 DNSName d_algo;
705 string d_key;
706 string d_other;
707
708 uint16_t d_error{0};
709 uint16_t d_keysize{0};
710 private:
711 };
712
713 class URIRecordContent : public DNSRecordContent {
714 public:
715 includeboilerplate(URI)
716 private:
717 uint16_t d_priority, d_weight;
718 string d_target;
719 };
720
721 class CAARecordContent : public DNSRecordContent {
722 public:
723 includeboilerplate(CAA)
724 private:
725 uint8_t d_flags;
726 string d_tag, d_value;
727 };
728
729 #define boilerplate(RNAME, RTYPE) \
730 RNAME##RecordContent::DNSRecordContent* RNAME##RecordContent::make(const DNSRecord& dr, PacketReader& pr) \
731 { \
732 return new RNAME##RecordContent(dr, pr); \
733 } \
734 \
735 RNAME##RecordContent::RNAME##RecordContent(const DNSRecord& dr, PacketReader& pr) \
736 { \
737 doRecordCheck(dr); \
738 xfrPacket(pr); \
739 } \
740 \
741 RNAME##RecordContent::DNSRecordContent* RNAME##RecordContent::make(const string& zonedata) \
742 { \
743 return new RNAME##RecordContent(zonedata); \
744 } \
745 \
746 void RNAME##RecordContent::toPacket(DNSPacketWriter& pw) \
747 { \
748 this->xfrPacket(pw); \
749 } \
750 \
751 void RNAME##RecordContent::report(void) \
752 { \
753 regist(1, RTYPE, &RNAME##RecordContent::make, &RNAME##RecordContent::make, #RNAME); \
754 regist(254, RTYPE, &RNAME##RecordContent::make, &RNAME##RecordContent::make, #RNAME); \
755 } \
756 void RNAME##RecordContent::unreport(void) \
757 { \
758 unregist(1, RTYPE); \
759 unregist(254, RTYPE); \
760 } \
761 \
762 RNAME##RecordContent::RNAME##RecordContent(const string& zoneData) \
763 { \
764 try { \
765 RecordTextReader rtr(zoneData); \
766 xfrPacket(rtr); \
767 } \
768 catch(RecordTextException& rte) { \
769 throw MOADNSException("Parsing record content (try 'pdnsutil check-zone'): "+string(rte.what())); \
770 } \
771 } \
772 \
773 string RNAME##RecordContent::getZoneRepresentation(bool noDot) const \
774 { \
775 string ret; \
776 RecordTextWriter rtw(ret, noDot); \
777 const_cast<RNAME##RecordContent*>(this)->xfrPacket(rtw); \
778 return ret; \
779 }
780
781
782 #define boilerplate_conv(RNAME, TYPE, CONV) \
783 boilerplate(RNAME, TYPE) \
784 template<class Convertor> \
785 void RNAME##RecordContent::xfrPacket(Convertor& conv, bool noDot) \
786 { \
787 CONV; \
788 if (conv.eof() == false) throw MOADNSException("When parsing " #RNAME " trailing data was not parsed: '" + conv.getRemaining() + "'"); \
789 } \
790
791 struct EDNSOpts
792 {
793 enum zFlags { DNSSECOK=32768 };
794 vector<pair<uint16_t, string> > d_options;
795 uint16_t d_packetsize{0};
796 uint16_t d_extFlags{0};
797 uint8_t d_extRCode, d_version;
798 };
799 //! Convenience function that fills out EDNS0 options, and returns true if there are any
800
801 class MOADNSParser;
802 bool getEDNSOpts(const MOADNSParser& mdp, EDNSOpts* eo);
803 DNSRecord makeOpt(const uint16_t udpsize, const uint16_t extRCode, const uint16_t extFlags);
804 void reportBasicTypes();
805 void reportOtherTypes();
806 void reportAllTypes();
807 ComboAddress getAddr(const DNSRecord& dr, uint16_t defport=0);
808 #endif