Skip to content
Snippets Groups Projects
Commit b2e0b2eb authored by Stuart Gathman's avatar Stuart Gathman
Browse files

Fix CNAME following bug.

parent 04a241f1
Branches
Tags
No related merge requests found
...@@ -70,15 +70,23 @@ class Session(object): ...@@ -70,15 +70,23 @@ class Session(object):
pre: qtype in ['A', 'AAAA', 'MX', 'PTR', 'TXT', 'SPF'] pre: qtype in ['A', 'AAAA', 'MX', 'PTR', 'TXT', 'SPF']
post: isinstance(__return__, types.ListType) post: isinstance(__return__, types.ListType)
""" """
if name.endswith('.'): name = name[:-1]
if not reduce(lambda x,y:x and 0 < len(y) < 64, name.split('.'),True):
return [] # invalid DNS name (too long or empty)
result = self.cache.get( (name, qtype) ) result = self.cache.get( (name, qtype) )
cname = None cname = None
if result: return result
cnamek = (name,'CNAME')
cname = self.cache.get( cnamek )
if not result: if cname:
cname = cname[0]
else:
safe2cache = Session.SAFE2CACHE safe2cache = Session.SAFE2CACHE
for k, v in DNSLookup(name, qtype): for k, v in DNSLookup(name, qtype):
if k == (name, 'CNAME'): if k == cnamek:
cname = v cname = v
if (qtype,k[1]) in safe2cache: if k[1] == 'CNAME' or (qtype,k[1]) in safe2cache:
self.cache.setdefault(k, []).append(v) self.cache.setdefault(k, []).append(v)
result = self.cache.get( (name, qtype), []) result = self.cache.get( (name, qtype), [])
if not result and cname: if not result and cname:
...@@ -89,10 +97,22 @@ class Session(object): ...@@ -89,10 +97,22 @@ class Session(object):
raise DNSError('Length of CNAME chain exceeds %d' % MAX_CNAME) raise DNSError('Length of CNAME chain exceeds %d' % MAX_CNAME)
cnames[name] = cname cnames[name] = cname
if cname in cnames: if cname in cnames:
raise DNSError, 'CNAME loop' raise DNSError('CNAME loop')
result = self.dns(cname, qtype, cnames=cnames) result = self.dns(cname, qtype, cnames=cnames)
if result:
self.cache[(name,qtype)] = result
return result return result
def dns_txt(self, domainname, enc='ascii'):
"Get a list of TXT records for a domain name."
if domainname:
try:
return [''.join(s.decode(enc) for s in a)
for a in self.dns(domainname, 'TXT')]
except UnicodeEncodeError:
raise DNSError('Non-ascii character in SPF TXT record.')
return []
DNS.DiscoverNameServers() DNS.DiscoverNameServers()
if __name__ == '__main__': if __name__ == '__main__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment