Skip to content

Commit a6adf2d

Browse files
authored
allow whitelist_dnswl to check ipv6 addresses
1 parent f3f35f4 commit a6adf2d

1 file changed

Lines changed: 25 additions & 16 deletions

File tree

filters/pythonfilter/whitelist_dnswl.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import sys
2121
import socket
22+
import ipaddress
2223
import courier.control
2324
import courier.config
2425

@@ -45,22 +46,30 @@ def do_filter(body_path, control_paths):
4546
except:
4647
return '451 Internal failure locating control files'
4748

48-
if senders_ip and '.' in senders_ip:
49-
# '.' must be in senders_ip until there are DNSWLs that support IPv6
50-
octets = senders_ip.split('.')
51-
octets.reverse()
52-
octets_r = '.'.join(octets)
53-
for zone in dnswl_zone:
54-
lookup = '%s.%s' % (octets_r, zone)
55-
try:
56-
lookup_result = socket.gethostbyname(lookup)
57-
except:
58-
lookup_result = None
59-
if lookup_result:
60-
# For now, any result is good enough.
61-
return '200 Ok'
62-
63-
# Return no decision for everyone else.
49+
try:
50+
sender = ipaddress.ip_address(senders_ip)
51+
except ValueError:
52+
sys.stderr.write(f'whitelist_dnswl: unparsable senders_ip: {senders_ip}\n')
53+
return ''
54+
55+
# sender is either IPV4Address or IPV6Address object,
56+
reverse = sender.reverse_pointer.replace('.in-addr.arpa', '').replace('.ip6.arpa', '')
57+
58+
for zone in dnswl_zone:
59+
lookup = '%s.%s' % (reverse, zone)
60+
try:
61+
lookup_result = socket.gethostbyname(lookup)
62+
except:
63+
lookup_result = None
64+
if lookup_result:
65+
# special case for access blocked on dnswl.org
66+
if lookup_result == '127.0.0.255':
67+
# access blocked, no nonspam indicator
68+
continue
69+
# For now, any other result is good enough.
70+
return '200 Ok'
71+
72+
# Return no decision if reaching this
6473
return ''
6574

6675

0 commit comments

Comments
 (0)