From: Pieter Lexis Date: Tue, 21 Feb 2017 12:08:49 +0000 (+0100) Subject: Check for valid hostnames in SRV, NS and MX records X-Git-Tag: rec-4.1.0-alpha1~200^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=32cd4eb113ea3c832bf717c2e3f895655af158cd;p=thirdparty%2Fpdns.git Check for valid hostnames in SRV, NS and MX records Fixes #512 --- diff --git a/pdns/dnsname.cc b/pdns/dnsname.cc index 8c6bf150ff..10dbbf8b8c 100644 --- a/pdns/dnsname.cc +++ b/pdns/dnsname.cc @@ -345,6 +345,16 @@ bool DNSName::isWildcard() const return (*p == 0x01 && *++p == '*'); } +/* + * Returns true if the DNSName is a valid RFC 1123 hostname, this function uses + * a regex on the string, so it is probably best not used when speed is essential. + */ +bool DNSName::isHostname() const +{ + static Regex hostNameRegex = Regex("^(([A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?)\\.)+$"); + return hostNameRegex.match(this->toString()); +} + unsigned int DNSName::countLabels() const { unsigned int count=0; diff --git a/pdns/dnsname.hh b/pdns/dnsname.hh index 21b8d84680..a2a8dccd65 100644 --- a/pdns/dnsname.hh +++ b/pdns/dnsname.hh @@ -96,6 +96,7 @@ public: void makeUsRelative(const DNSName& zone); DNSName labelReverse() const; bool isWildcard() const; + bool isHostname() const; unsigned int countLabels() const; size_t wirelength() const; //!< Number of total bytes in the name bool empty() const { return d_storage.empty(); } diff --git a/pdns/pdnsutil.cc b/pdns/pdnsutil.cc index b6b86387a2..2179ec6b64 100644 --- a/pdns/pdnsutil.cc +++ b/pdns/pdnsutil.cc @@ -579,13 +579,27 @@ int checkZone(DNSSECKeeper &dk, UeberBackend &B, const DNSName& zone, const vect } } - if(rr.qtype.getCode() == QType::A || rr.qtype.getCode() == QType::AAAA) - { - Regex hostnameRegex=Regex("^(([A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?)\\.)+$"); - if (!hostnameRegex.match(rr.qname.toString())) - { - cout<<"[Info] A or AAAA record found at '"< parts; + stringtok(parts, rr.getZoneRepresentation()); + toCheck = DNSName(parts[3]); + } else if (rr.qtype.getCode() == QType::MX) { + vector parts; + stringtok(parts, rr.getZoneRepresentation()); + toCheck = DNSName(parts[1]); + } else { + toCheck = DNSName(rr.content); + } + + if (!toCheck.isHostname()) { + cout<<"[Warning] "<