From 786722814682a7279314505ad2a1602e52e997c8 Mon Sep 17 00:00:00 2001 From: sebres Date: Wed, 16 Aug 2017 10:45:37 +0200 Subject: [PATCH] closes part of gh-1865: fixed "Retrieving own IPs of localhost failed: inet_pton() argument 2 must be string, not int" some python-versions resp. host configurations causes returning of integer (instead of ip-string) --- fail2ban/server/ipdns.py | 8 ++++++-- fail2ban/tests/filtertestcase.py | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/fail2ban/server/ipdns.py b/fail2ban/server/ipdns.py index 6ef36888..7110d974 100644 --- a/fail2ban/server/ipdns.py +++ b/fail2ban/server/ipdns.py @@ -69,10 +69,14 @@ class DNSUtils: for fam, ipfam in ((socket.AF_INET, IPAddr.FAM_IPv4), (socket.AF_INET6, IPAddr.FAM_IPv6)): try: for result in socket.getaddrinfo(dns, None, fam, 0, socket.IPPROTO_TCP): - ip = IPAddr(result[4][0], ipfam) + # if getaddrinfo returns something unexpected: + if len(result) < 4 or not len(result[4]): continue + # get ip from `(2, 1, 6, '', ('127.0.0.1', 0))`,be sure we've an ip-string + # (some python-versions resp. host configurations causes returning of integer there): + ip = IPAddr(str(result[4][0]), ipfam) if ip.isValid: ips.append(ip) - except socket.error as e: + except Exception as e: saveerr = e if not ips and saveerr: logSys.warning("Unable to find a corresponding IP address for %s: %s", dns, saveerr) diff --git a/fail2ban/tests/filtertestcase.py b/fail2ban/tests/filtertestcase.py index cb0edb06..fe90f2ee 100644 --- a/fail2ban/tests/filtertestcase.py +++ b/fail2ban/tests/filtertestcase.py @@ -1852,6 +1852,10 @@ class DNSUtilsNetworkTests(unittest.TestCase): self.assertTrue(IPAddr("93.184.216.34").isInNet(ips)) self.assertTrue(IPAddr("2606:2800:220:1:248:1893:25c8:1946").isInNet(ips)) + def testIPAddr_wrongDNS_IP(self): + DNSUtils.dnsToIp('`this`.dns-is-wrong.`wrong-nic`-dummy') + DNSUtils.ipToName('*') + def testIPAddr_Cached(self): ips = [DNSUtils.dnsToIp('example.com'), DNSUtils.dnsToIp('example.com')] for ip1, ip2 in zip(ips, ips):