[Pdns-dev] [PATCH 2/3] Classless in-addr.arpa delegation for autogenerated records

Peter Collingbourne peter at pcc.me.uk
Sat Jan 2 20:49:40 CET 2010


This patch allows the LDAP (and other) backends to support classless
in-addr.arpa delegation for autogenerated PTR records. This is achieved
by modifying the ptr2ip4 function to discard non-numeric domain parts.
The patch also moves the ptr2ip* functions out of the LDAP backend
and into the server proper so they may be reused by other backends.
---
 modules/ldapbackend/ldapbackend.cc |    6 +-
 modules/ldapbackend/utils.hh       |   63 ------------------------------
 pdns/misc.hh                       |   74 ++++++++++++++++++++++++++++++++++++
 3 files changed, 77 insertions(+), 66 deletions(-)

diff --git a/modules/ldapbackend/ldapbackend.cc b/modules/ldapbackend/ldapbackend.cc
index 26748a3..86f4c68 100644
--- a/modules/ldapbackend/ldapbackend.cc
+++ b/modules/ldapbackend/ldapbackend.cc
@@ -225,7 +225,7 @@ void LdapBackend::lookup_strict( const QType &qtype, const string &qname, DNSPac
 {
         int len;
         vector<string> parts;
-        string filter, attr, qesc;
+        string filter, attr, qesc, ip;
         const char** attributes = ldap_attrany + 1;   // skip associatedDomain
         const char* attronly[] = { NULL, "dNSTTL", "modifyTimestamp", NULL };
 
@@ -234,9 +234,9 @@ void LdapBackend::lookup_strict( const QType &qtype, const string &qname, DNSPac
         stringtok( parts, qesc, "." );
         len = qesc.length();
 
-         if( parts.size() == 6 && len > 13 && qesc.substr( len - 13, 13 ) == ".in-addr.arpa" )   // IPv4 reverse lookups
+         if( len > 13 && qesc.substr( len - 13, 13 ) == ".in-addr.arpa" && ptr2ip4( parts, ip ) )   // IPv4 reverse lookups
         {
-        	filter = "aRecord=" + ptr2ip4( parts );
+        	filter = "aRecord=" + ip;
         	attronly[0] = "associatedDomain";
         	attributes = attronly;
         }
diff --git a/modules/ldapbackend/utils.hh b/modules/ldapbackend/utils.hh
index 17e3b60..5f434d6 100644
--- a/modules/ldapbackend/utils.hh
+++ b/modules/ldapbackend/utils.hh
@@ -12,69 +12,6 @@ using std::string;
 using std::vector;
 
 
-inline string ptr2ip4( vector<string>& parts )
-{
-        string ip;
-        parts.pop_back();
-        parts.pop_back();
-
-
-        ip = parts.back();
-        parts.pop_back();
-
-        while( !parts.empty() )
-        {
-        	ip += "." + parts.back();
-        	parts.pop_back();
-        }
-
-        return ip;
-}
-
-
-inline string ptr2ip6( vector<string>& parts )
-{
-        int i = 0;
-        string ip;
-
-
-        parts.pop_back();
-        parts.pop_back();
-
-        while( i < 3 && parts.size() > 1 && parts.back() == "0" )
-        {
-        	parts.pop_back();
-        	i++;
-        }
-
-        while( i++ < 4 && !parts.empty() )
-        {
-        	ip += parts.back();
-        	parts.pop_back();
-        }
-
-        while( !parts.empty() )
-        {
-        	i = 0;
-        	ip += ":";
-
-        	while( i < 3 && parts.size() > 1 && parts.back() == "0" )
-        	{
-        		parts.pop_back();
-        		i++;
-        	}
-
-        	while( i++ < 4 && !parts.empty() )
-        	{
-        		ip += parts.back();
-        		parts.pop_back();
-        	}
-        }
-
-        return ip;
-}
-
-
 inline string ip2ptr4( const string& ip )
 {
         string ptr;
diff --git a/pdns/misc.hh b/pdns/misc.hh
index d22c749..5ba01ff 100644
--- a/pdns/misc.hh
+++ b/pdns/misc.hh
@@ -378,6 +378,80 @@ inline void setSocketReusable(int fd)
   setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&tmp, static_cast<unsigned>(sizeof tmp));
 }
 
+inline bool isdigitstr(const string& str)
+{
+        for (string::const_iterator i = str.begin(); i != str.end(); i++)
+        {
+        	if (!isdigit(*i))
+        		return false;
+        }
+        return true;
+}
+
+inline bool ptr2ip4( const vector<string>& parts, string& ip )
+{
+        int count = 1;
+
+        vector<string>::const_reverse_iterator i = parts.rbegin();
+        ++i; ++i;
+
+        ip = *i;
+
+        while( count <= 4 && ++i != parts.rend() )
+        {
+        	if (isdigitstr(*i))
+        	{
+        		count++;
+        		ip += "." + *i;
+        	}
+        }
+
+        return ( count == 4 );
+}
+
+
+inline string ptr2ip6( vector<string>& parts )
+{
+        int i = 0;
+        string ip;
+
+
+        parts.pop_back();
+        parts.pop_back();
+
+        while( i < 3 && parts.size() > 1 && parts.back() == "0" )
+        {
+        	parts.pop_back();
+        	i++;
+        }
+
+        while( i++ < 4 && !parts.empty() )
+        {
+        	ip += parts.back();
+        	parts.pop_back();
+        }
+
+        while( !parts.empty() )
+        {
+        	i = 0;
+        	ip += ":";
+
+        	while( i < 3 && parts.size() > 1 && parts.back() == "0" )
+        	{
+        		parts.pop_back();
+        		i++;
+        	}
+
+        	while( i++ < 4 && !parts.empty() )
+        	{
+        		ip += parts.back();
+        		parts.pop_back();
+        	}
+        }
+
+        return ip;
+}
+
 string stripDot(const string& dom);
 void seedRandom(const string& source);
 #endif
-- 
1.6.5



More information about the Pdns-dev mailing list