text
stringlengths
1
93.6k
if not result and not ignore_void:
self.void_lookups += 1
if self.void_lookups > MAX_VOID_LOOKUPS:
raise PermError('Void lookup limit of %d exceeded' % MAX_VOID_LOOKUPS)
return result
def cidrmatch(self, ipaddrs, n):
"""Match connect IP against a CIDR network of other IP addresses.
Examples:
>>> c = query(s='strong-bad@email.example.com',
... h='mx.example.org', i='192.0.2.3')
>>> c.p = 'mx.example.org'
>>> c.r = 'example.com'
>>> c.cidrmatch(['192.0.2.3'],32)
True
>>> c.cidrmatch(['192.0.2.2'],32)
False
>>> c.cidrmatch(['192.0.2.2'],31)
True
>>> six = query(s='strong-bad@email.example.com',
... h='mx.example.org', i='2001:0db8:0:0:0:0:0:0001')
>>> six.p = 'mx.example.org'
>>> six.r = 'example.com'
>>> six.cidrmatch(['2001:0DB8::'],127)
True
>>> six.cidrmatch(['2001:0DB8::'],128)
False
>>> six.cidrmatch(['2001:0DB8:0:0:0:0:0:0001'],128)
True
"""
try:
try:
for netwrk in [ipaddress.ip_network(ip) for ip in ipaddrs]:
network = netwrk.supernet(new_prefix=n)
if isinstance(self.iplist, bool):
if network.__contains__(self.ipaddr):
return True
else:
if n < self.cidrmax:
self.iplist.append(network)
else:
self.iplist.append(network.ip_address)
except AttributeError:
for netwrk in [ipaddress.ip_network(ip,strict=False) for ip in ipaddrs]:
network = netwrk.supernet(new_prefix=n)
if isinstance(self.iplist, bool):
if network.__contains__(self.ipaddr):
return True
else:
if n < self.cidrmax:
self.iplist.append(network)
else:
self.iplist.append(network.network_address)
except ValueError as x:
raise PermError(str(x))
return False
def parse_header_ar(self, val):
"""Set SPF values from RFC 5451 Authentication Results header.
Useful when SPF has already been run on a trusted gateway machine.
Expects the entire header as an input.
Examples:
>>> q = query('192.0.2.3','strong-bad@email.example.com','mx.example.org')
>>> q.mechanism = 'unknown'
>>> p = q.parse_header_ar('''Authentication-Results: bmsi.com; spf=neutral \\n (abuse@kitterman.com: 192.0.2.3 is neither permitted nor denied by domain of email.example.com) \\n smtp.mailfrom=email.example.com \\n (sender=strong-bad@email.example.com; helo=mx.example.org; client-ip=192.0.2.3; receiver=abuse@kitterman.com; mechanism=?all)''')
>>> q.get_header(q.result, header_type='authres', aid='bmsi.com')
'Authentication-Results: bmsi.com; spf=neutral (unknown: 192.0.2.3 is neither permitted nor denied by domain of email.example.com) smtp.mailfrom=email.example.com (sender=email.example.com; helo=mx.example.org; client-ip=192.0.2.3; receiver=unknown; mechanism=unknown)'
>>> p = q.parse_header_ar('''Authentication-Results: bmsi.com; spf=None (mail.bmsi.com: test; client-ip=163.247.46.150) smtp.mailfrom=admin@squiebras.cl (helo=mail.squiebras.cl; receiver=mail.bmsi.com;\\n mechanism=mx/24)''')
>>> q.get_header(q.result, header_type='authres', aid='bmsi.com')
'Authentication-Results: bmsi.com; spf=none (unknown: 192.0.2.3 is neither permitted nor denied by domain of email.example.com) smtp.mailfrom=admin@squiebras.cl (sender=admin@squiebras.cl; helo=mx.example.org; client-ip=192.0.2.3; receiver=unknown; mechanism=unknown)'
"""
import authres
# Authres expects unwrapped headers according to docs
val = ' '.join(s.strip() for s in val.split('\n'))
arobj = authres.AuthenticationResultsHeader.parse(val)
# TODO extract and parse comments (not supported by authres)
for resobj in arobj.results:
if resobj.method == 'spf':
self.authserv = arobj.authserv_id
self.result = resobj.result
if resobj.properties[0].name == 'mailfrom':
self.d = resobj.properties[0].value
self.s = resobj.properties[0].value
if resobj.properties[0].name == 'helo':
self.h = resobj.properties[0].value
return
def parse_header_spf(self, val):
"""Set SPF values from Received-SPF header.
Useful when SPF has already been run on a trusted gateway machine.
Examples: