text
stringlengths
1
93.6k
if len(a) == 1:
return to_ascii(a[0]) # return TXT if SPF wasn't found
if DELEGATE: # use local record if neither found
a = [t
for t in self.dns_txt(domain+'._spf.'+DELEGATE,ignore_void=True)
if RE_SPF.match(t)
]
if len(a) == 1: return to_ascii(a[0])
return None
## Get list of TXT records for a domain name.
# Any DNS library *must* return bytes (same as str in python2) for TXT
# or SPF since there is no general decoding to unicode. Py3dns-3.0.2
# incorrectly attempts to convert to str using idna encoding by default.
# We work around this by assuming any UnicodeErrors coming from py3dns
# are from a non-ascii SPF record (incorrect in general). Packages
# should require py3dns != 3.0.2.
#
# We cannot check for non-ascii here, because we must ignore non-SPF
# records - even when they are non-ascii. So we return bytes.
# The caller does the ascii check for SPF records and explanations.
#
def dns_txt(self, domainname, rr='TXT',ignore_void=False):
"Get a list of TXT records for a domain name."
if domainname:
try:
dns_list = self.dns(domainname, rr,ignore_void=ignore_void)
if dns_list:
# a[0][:0] is '' for py3dns-3.0.2, otherwise b''
a = [a[0][:0].join(a) for a in dns_list if a]
# FIXME: workaround for error in py3dns-3.0.2
if isinstance(a[0],bytes):
return a
return [s.encode('utf-8') for s in a]
# FIXME: workaround for error in py3dns-3.0.2
except UnicodeError:
raise PermError('Non-ascii characters found in %s record for %s'
%(rr,domainname))
return []
def dns_mx(self, domainname):
"""Get a list of IP addresses for all MX exchanges for a
domain name.
"""
# RFC 4408/7208 section 5.4 "mx"
# To prevent DoS attacks, more than 10 MX names MUST NOT be looked up
# Changed to permerror if more than 10 exist in 7208
mxnames = self.dns(domainname, 'MX')
if self.strict:
max = MAX_MX
if len(mxnames) > MAX_MX:
raise PermError(
'More than %d MX records returned'%MAX_MX)
if self.strict > 1:
if len(mxnames) == 0:
raise AmbiguityWarning(
'No MX records found for mx mechanism', domainname)
else:
max = MAX_MX * 4
mxnames.sort()
return [a for mx in mxnames[:max] for a in self.dns_a(mx[1],self.A)]
def dns_a(self, domainname, A='A'):
"""Get a list of IP addresses for a domainname.
"""
if not domainname: return []
r = self.dns(domainname, A)
if self.strict > 1 and len(r) == 0:
raise AmbiguityWarning(
'No %s records found for'%A, domainname)
if A == 'AAAA' and bytes is str:
# work around pydns inconsistency plus python2 bytes/str ambiguity
return [Bytes(ip) for ip in r]
return r
def validated_ptrs(self):
"""Figure out the validated PTR domain names for the connect IP."""
# To prevent DoS attacks, more than 10 PTR names MUST NOT be looked up
if self.strict:
max = MAX_PTR
if self.strict > 1:
#Break out the number of PTR records returned for testing
try:
ptrnames = self.dns_ptr(self.i)
if len(ptrnames) > max:
warning = 'More than %d PTR records returned' % max
raise AmbiguityWarning(warning, self.c)
else:
if len(ptrnames) == 0:
raise AmbiguityWarning(
'No PTR records found for ptr mechanism', self.c)
except:
raise AmbiguityWarning(
'No PTR records found for ptr mechanism', self.c)
else:
max = MAX_PTR * 4
cidrlength = self.cidrmax
return [p for p in self.dns_ptr(self.i)[:max]
if self.cidrmatch(self.dns_a(p,self.A),cidrlength)]