diff options
author | Yasuhito FUTATSUKI at POEM <futatuki@poem.co.jp> | 2018-06-07 15:32:58 +0900 |
---|---|---|
committer | Yasuhito FUTATSUKI at POEM <futatuki@poem.co.jp> | 2018-06-07 15:32:58 +0900 |
commit | 5d70365e84ce546df8244421b8cbe199bb02bd7e (patch) | |
tree | 00b322b06b4d699ce52a679632aa20e919283821 /Mailman | |
parent | 307400452b2d9b702ef36899ce32d0d7405d11ed (diff) | |
parent | 8291c814c54d87c7958304e471a5c5c013417e45 (diff) | |
download | mailman2-5d70365e84ce546df8244421b8cbe199bb02bd7e.tar.gz mailman2-5d70365e84ce546df8244421b8cbe199bb02bd7e.tar.xz mailman2-5d70365e84ce546df8244421b8cbe199bb02bd7e.zip |
merge lp:mailman/2.1 rev. 1766
Diffstat (limited to 'Mailman')
-rw-r--r-- | Mailman/Utils.py | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/Mailman/Utils.py b/Mailman/Utils.py index cdc82366..6038667b 100644 --- a/Mailman/Utils.py +++ b/Mailman/Utils.py @@ -79,6 +79,12 @@ try: except ImportError: dns_resolver = False +try: + import ipaddress + have_ipaddress = True +except ImportError: + have_ipaddress = False + EMPTYSTRING = '' UEMPTYSTRING = u'' CR = '\r' @@ -1498,13 +1504,21 @@ def xml_to_unicode(s, cset): def banned_ip(ip): if not dns_resolver: return False - parts = ip.split('.') - if len(parts) != 4: - return False - lookup = '{}.{}.{}.{}.zen.spamhaus.org'.format(parts[3], - parts[2], - parts[1], - parts[0]) + if have_ipaddress: + try: + uip = unicode(ip, encoding='us-ascii', errors='replace') + ptr = ipaddress.ip_address(uip).reverse_pointer + except ValueError: + return False + lookup = '{0}.zen.spamhaus.org'.format('.'.join(ptr.split('.')[:-2])) + else: + parts = ip.split('.') + if len(parts) != 4: + return False + lookup = '{0}.{1}.{2}.{3}.zen.spamhaus.org'.format(parts[3], + parts[2], + parts[1], + parts[0]) resolver = dns.resolver.Resolver() try: ans = resolver.query(lookup, dns.rdatatype.A) |