-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspfcheck.py
executable file
·46 lines (37 loc) · 1.02 KB
/
spfcheck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python2
import os
import spf
import sys
# RELAYCLIENTs and TRUSTCLIENTs can pass
if "RELAYCLIENT" in os.environ or "TRUSTCLIENT" in os.environ:
print
exit()
# Get the client's IP from %ENV
remoteip = ""
if os.environ.get('TCPREMOTEIP'):
remoteip = os.environ['TCPREMOTEIP']
if os.environ.get('TCP6REMOTEIP'):
remoteip = os.environ['TCP6REMOTEIP']
if os.environ.get('SSLREMOTEIP'):
remoteip = os.environ['SSLREMOTEIP']
# Exit if we can not get client IP
if len(remoteip) == 0:
print
exit()
# Exit if the host did not say hel(l)o
if not os.environ.get('SMTPHELOHOST'):
print
exit()
# Exit if there is no sender
if not os.environ.get('SMTPMAILFROM'):
print
exit()
# Run the SPF check
result, explanation = spf.check2(i=remoteip, s=os.environ['SMTPMAILFROM'], h=os.environ['SMTPHELOHOST'])
# Only fails will be rejected
if result == "fail":
print >> sys.stderr, "%d SPF check failed for %s" % (os.getppid(), os.environ['SMTPMAILFROM'])
print "E451 SPF check failed [%s]" % explanation
else:
print
exit()