bundlewrap/bundles/postfix/files/check_spam_blocklist
Franzi fdcec012f3
All checks were successful
bundlewrap/pipeline/head This commit looks good
bundles/postfix: add SPAM BLOCKLIST check for every non-private IP attached to the server
2021-03-22 20:24:14 +01:00

81 lines
1.7 KiB
Python

#!/usr/bin/env python3
from ipaddress import ip_address, IPv6Address
from sys import argv, exit
from dns.exception import Timeout
from dns.resolver import Resolver, NoAnswer, NXDOMAIN, NoNameservers
BLOCKLISTS = [
'0spam.fusionzero.com',
'bl.mailspike.Dorg',
'bl.spamcop.net',
'blackholes.brainerd.net',
'dnsbl-1.uceprotect.net',
'dnsbl-2.uceprotect.net',
'dnsbl-3.uceprotect.net',
'l2.spews.dnsbl.sorbs.net',
'list.dsbl.org',
'map.spam-rbl.com',
'multihop.dsbl.org',
'ns1.unsubscore.com',
'opm.blitzed.org',
'psbl.surriel.com',
'rbl.efnet.org',
'rbl.schulte.org',
'spamguard.leadmon.net',
'ubl.unsubscore.com',
'unconfirmed.dsbl.org',
'virbl.dnsbl.bit.nl',
'virbl.dnsbl.bit.nl',
'zen.spamhaus.org',
]
try:
ip = ip_address(argv[1])
except Exception:
print('usage: {} <ip>'.format(argv[0]))
exit(3)
found = False
resolver = Resolver()
resolver.timeout = 5
resolver.lifetime = 5
if isinstance(ip, IPv6Address):
ip_list = list(ip.exploded.replace(':', ''))
else:
ip_list = ip.exploded.split('.')
ip_list.reverse()
for blocklist in BLOCKLISTS:
dns_name = '{}.{}'.format(
'.'.join(ip_list),
blocklist,
)
try:
result = resolver.query(dns_name)
for item in result:
print('{} listed in {} as {}'.format(
ip,
blocklist,
item,
))
found = True
except (NoAnswer, NXDOMAIN, NoNameservers, Timeout):
# Probably fine
pass
except Exception as e:
print(repr(e))
exit(3)
if found:
exit(2)
else:
print('OK')
exit(0)