text
stringlengths
1
93.6k
"""Return a best guess based on a default SPF record.
>>> q = query('1.2.3.4','','SUPERVISION1',receiver='example.com')
>>> q.best_guess()[0]
'none'
"""
if RE_TOPLAB.split(self.d)[-1]:
return ('none', 250, '')
pe = self.perm_error
r,c,e = self.check(spf)
if r == 'permerror': # permerror not useful for bestguess
if self.perm_error and self.perm_error.ext:
r,c,e = self.perm_error.ext
else:
r,c = 'neutral',250
self.perm_error = pe
return r,c,e
def check(self, spf=None):
"""
Returns (result, mta-status-code, explanation) where result
in ['fail', 'softfail', 'neutral' 'permerror', 'pass', 'temperror', 'none']
Examples:
>>> q = query(s='strong-bad@email.example.com',
... h='mx.example.org', i='192.0.2.3')
>>> q.check(spf='v=spf1 ?all')
('neutral', 250, 'access neither permitted nor denied')
>>> q.check(spf='v=spf1 redirect=controlledmail.com exp=_exp.controlledmail.com')
('fail', 550, 'SPF fail - not authorized')
>>> q.check(spf='v=spf1 ip4:192.0.0.0/8 ?all moo')
('permerror', 550, 'SPF Permanent Error: Unknown mechanism found: moo')
>>> q.check(spf='v=spf1 ip4:192.0.0.n ?all')
('permerror', 550, 'SPF Permanent Error: Invalid IP4 address: ip4:192.0.0.n')
>>> q.check(spf='v=spf1 ip4:192.0.2.3 ip4:192.0.0.n ?all')
('permerror', 550, 'SPF Permanent Error: Invalid IP4 address: ip4:192.0.0.n')
>>> q.check(spf='v=spf1 ip6:2001:db8:ZZZZ:: ?all')
('permerror', 550, 'SPF Permanent Error: Invalid IP6 address: ip6:2001:db8:ZZZZ::')
>>> q.check(spf='v=spf1 =a ?all moo')
('permerror', 550, 'SPF Permanent Error: Unknown qualifier, RFC 4408 para 4.6.1, found in: =a')
>>> q.check(spf='v=spf1 ip4:192.0.0.0/8 ~all')
('pass', 250, 'sender SPF authorized')
>>> q.check(spf='v=spf1 ip4:192.0.0.0/8 -all moo=')
('pass', 250, 'sender SPF authorized')
>>> q.check(spf='v=spf1 ip4:192.0.0.0/8 -all match.sub-domains_9=yes')
('pass', 250, 'sender SPF authorized')
>>> q.strict = False
>>> q.check(spf='v=spf1 ip4:192.0.0.0/8 -all moo')
('permerror', 550, 'SPF Permanent Error: Unknown mechanism found: moo')
>>> q.perm_error.ext
('pass', 250, 'sender SPF authorized')
>>> q.strict = True
>>> q.check(spf='v=spf1 ip4:192.1.0.0/16 moo -all')
('permerror', 550, 'SPF Permanent Error: Unknown mechanism found: moo')
>>> q.check(spf='v=spf1 ip4:192.1.0.0/16 ~all')
('softfail', 250, 'domain owner discourages use of this host')
>>> q.check(spf='v=spf1 -ip4:192.1.0.0/6 ~all')
('fail', 550, 'SPF fail - not authorized')
# Assumes DNS available
>>> q.check()
('none', 250, '')
>>> q.check(spf='v=spf1 ip4:1.2.3.4 -a:example.net -all')
('fail', 550, 'SPF fail - not authorized')
>>> q.libspf_local='ip4:192.0.2.3 a:example.org'
>>> q.check(spf='v=spf1 ip4:1.2.3.4 -a:example.net -all')
('pass', 250, 'sender SPF authorized')
>>> q.check(spf='v=spf1 ip4:1.2.3.4 -all exp=_exp.controlledmail.com')
('fail', 550, 'Controlledmail.com does not send mail from itself.')
>>> q.check(spf='v=spf1 ip4:1.2.3.4 ?all exp=_exp.controlledmail.com')
('neutral', 250, 'access neither permitted nor denied')
>>> r = query(i='list', s='office@kitterman.com', h=None)
>>> r.check()
('fail', 550, 'SPF fail - not authorized')
"""
self.mech = [] # unknown mechanisms
# If not strict, certain PermErrors (mispelled
# mechanisms, strict processing limits exceeded)
# will continue processing. However, the exception
# that strict processing would raise is saved here
self.perm_error = None
self.mechanism = None
self.void_lookups = 0