text
stringlengths
1
93.6k
Exception.__init__(self, msg, mech)
self.msg = msg
self.mech = mech
self.ext = ext
def __str__(self):
if self.mech:
return '%s: %s'%(self.msg, self.mech)
return self.msg
def check2(i, s, h, local=None, receiver=None, timeout=MAX_PER_LOOKUP_TIME, verbose=False, querytime=20):
"""Test an incoming MAIL FROM:<s>, from a client with ip address i.
h is the HELO/EHLO domain name. This is the RFC4408/7208 compliant
pySPF2.0 interface. The interface returns an SPF result and explanation
only. SMTP response codes are not returned since neither RFC 4408 nor RFC
7208 does specify receiver policy. Applications updated for RFC 4408 and
RFC 7208 should use this interface. The maximum time, in seconds, this
function is allowed to run before a TempError is returned is controlled by
querytime. When set to 0 the timeout parameter (default 20 seconds)
controls the time allowed for each DNS lookup. When set to a non-zero
value, it total time for all processing related to the SPF check is
limited to querytime (default 20 seconds as recommended in RFC 7208,
paragraph 4.6.4).
Returns (result, explanation) where result in
['pass', 'permerror', 'fail', 'temperror', 'softfail', 'none', 'neutral' ].
Example:
#>>> check2(i='61.51.192.42', s='liukebing@bcc.com', h='bmsi.com')
"""
res,_,exp = query(i=i, s=s, h=h, local=local,
receiver=receiver,timeout=timeout,verbose=verbose,querytime=querytime).check()
return res,exp
def check(i, s, h, local=None, receiver=None, verbose=False):
"""Test an incoming MAIL FROM:<s>, from a client with ip address i.
h is the HELO/EHLO domain name. This is the pre-RFC SPF Classic interface.
Applications written for pySPF 1.6/1.7 can use this interface to allow
pySPF2 to be a drop in replacement for older versions. With the exception
of result codes, performance in RFC 4408 compliant.
Returns (result, code, explanation) where result in
['pass', 'unknown', 'fail', 'error', 'softfail', 'none', 'neutral' ].
Example:
#>>> check(i='61.51.192.42', s='liukebing@bcc.com', h='bmsi.com')
"""
res,code,exp = query(i=i, s=s, h=h, local=local, receiver=receiver,
verbose=verbose).check()
if res == 'permerror':
res = 'unknown'
elif res == 'tempfail':
res =='error'
return res, code, exp
class query(object):
"""A query object keeps the relevant information about a single SPF
query:
i: ip address of SMTP client in dotted notation
s: sender declared in MAIL FROM:<>
l: local part of sender s
d: current domain, initially domain part of sender s
h: EHLO/HELO domain
v: 'in-addr' for IPv4 clients and 'ip6' for IPv6 clients
t: current timestamp
p: SMTP client domain name
o: domain part of sender s
r: receiver
c: pretty ip address (different from i for IPv6)
This is also, by design, the same variables used in SPF macro
expansion.
Also keeps cache: DNS cache.
"""
def __init__(self, i, s, h, local=None, receiver=None, strict=True,
timeout=MAX_PER_LOOKUP_TIME,verbose=False,querytime=0):
self.s, self.h = s, h
if not s and h:
self.s = 'postmaster@' + h
self.ident = 'helo'
else:
self.ident = 'mailfrom'
self.l, self.o = split_email(s, h)
self.t = str(int(time.time()))
self.d = self.o
self.p = None # lazy evaluation
if receiver:
self.r = receiver
else:
self.r = 'unknown'
# Since the cache does not track Time To Live, it is created
# fresh for each query. It is important for efficiently using
# multiple results provided in DNS answers.
self.cache = {}
self.defexps = dict(EXPLANATIONS)
self.exps = dict(EXPLANATIONS)
self.libspf_local = local # local policy