diff --git a/dkimpy_milter/__init__.py b/dkimpy_milter/__init__.py index facae23..9391bb8 100644 --- a/dkimpy_milter/__init__.py +++ b/dkimpy_milter/__init__.py @@ -101,15 +101,26 @@ class dkimMilter(Milter.Base): if self.conf.get('Syslog') and self.conf.get('debugLevel') >= 1: syslog.syslog("connect from {0} at {1} {2}" .format(hostname, hostaddr, connecttype)) - return Milter.CONTINUE + if self.conf.get('Syslog') and self.conf.get('debugLevel') >= 3: + syslog.syslog("internal_conn: {0}, external_conn: {1}" + .format(self.internal_connection, self.external_connection)) + + return Milter.CONTINUE # multiple messages can be received on a single connection # envfrom (MAIL FROM in the SMTP protocol) seems to mark the start # of each message. @Milter.noreply def envfrom(self, f, *moredata): - f = str(codecs.encode(f, 'UTF-8', 'replace'), 'UTF-8', 'ignore') - moredata = str(codecs.encode(str(moredata), 'UTF-8', 'replace'), 'UTF-8', 'ignore') + try: + f = str(codecs.encode(f, 'UTF-8', 'replace'), 'UTF-8', 'ignore') + except TypeError: + f = codecs.encode(f, 'UTF-8', 'replace').decode() + try: + moredata = str(codecs.encode(str(moredata), 'UTF-8', 'replace'), 'UTF-8', 'ignore') + except TypeError: + moredata = codecs.encode(str(moredata), 'UTF-8', 'replace').decode() + if self.conf.get('Syslog') and self.conf.get('debugLevel') >= 2: syslog.syslog("mail from: {0} {1}".format(f, moredata)) self.fp = io.BytesIO() @@ -144,7 +155,10 @@ class dkimMilter(Milter.Base): except IndexError as er: pass # self.author was not a proper email address # This keeps non-ascii characters out of the From domain - self.fdomain = str(codecs.encode(self.fdomain, 'ascii', 'replace'), 'ascii', 'ignore') + try: + self.fdomain = str(codecs.encode(self.fdomain, 'ascii', 'replace'), 'ascii', 'ignore') + except TypeError: + self.fdomain = codecs.encode(self.fdomain, 'ascii', 'replace').decode('ascii','ignore') if (self.conf.get('Syslog') and self.conf.get('debugLevel') >= 1): syslog.syslog("{0}: {1}".format(name, val)) @@ -203,6 +217,8 @@ class dkimMilter(Milter.Base): syslog.syslog('self.domain: {0}, self.fdomain: {1}, self.iequals: {2}'.format(self.domain, self.fdomain, self.iequals)) if ((self.fdomain in self.domain) and not self.conf.get('Mode') == 'v' and not self.external_connection): + if (self.conf.get('Syslog') and self.conf.get('debugLevel') >= 3): + syslog.syslog("Signing DKIM") self.sign_dkim(txt) if ((self.has_dkim) and (not self.internal_connection) and (self.conf.get('Mode') == 'v' or diff --git a/dkimpy_milter/config.py b/dkimpy_milter/config.py index cbbfaea..d52b5a4 100644 --- a/dkimpy_milter/config.py +++ b/dkimpy_milter/config.py @@ -89,16 +89,23 @@ class HostsDataset(object): self.item = item[1:] self.negative = True try: - self.item = ipaddress.ip_address(str(self.item, "utf-8")) + try: + self.item = ipaddress.ip_address(str(self.item, "utf-8")) + except TypeError: + self.item = ipaddress.ip_address(self.item) if isinstance(self.item, ipaddress.IPv4Address): self.isipv4 = True elif isinstance(self.item, ipaddress.IPv6Address): self.isipv6 = True except ValueError as e: try: - self.item = ipaddress.ip_network(str - (self.item, "utf-8"), - strict=False) + try: + self.item = ipaddress.ip_network(str + (self.item, "utf-8"), + strict=False) + except TypeError: + self.item = ipaddress.ip_network(self.item, + strict=False) if isinstance(self.item, ipaddress.IPv4Network): self.isipv4cidr = True elif isinstance(self.item, ipaddress.IPv6Network): @@ -114,7 +121,10 @@ class HostsDataset(object): def match(self, connectip): '''Check if the connect IP is part of the dataset''' - source = ipaddress.ip_address(str(connectip, "utf-8")) + try: + source = ipaddress.ip_address(str(connectip, "utf-8")) + except TypeError: + source = ipaddress.ip_address(connectip) for item in self.dataset: if item.isdomain or item.ishostname: result = self.matchname(source) # Match host/domains first @@ -164,13 +174,19 @@ class HostsDataset(object): if isinstance(source, ipaddress.IPv4Address): ips = s.dns(name, 'A') for ip in ips: - ip = ipaddress.IPv4Address(str(ip, 'UTF-8')) + try: + ip = ipaddress.IPv4Address(str(ip, 'UTF-8')) + except TypeError: + ip = ipaddress.IPv4Address(ip) if ip == source: results.append(name) if isinstance(source, ipaddress.IPv6Address): ips = s.dns(name, 'AAAA') for ip in ips: - ip = ipaddress.IPv6Address(str(ip, 'UTF-8')) + try: + ip = ipaddress.IPv6Address(str(ip, 'UTF-8')) + except TypeError: + ip = ipaddress.IPv6Address(ip) if ip == source: results.append(name) return results @@ -439,8 +455,14 @@ def _readConfigFile(path, configData=None, configGlobal={}): fp.close() try: configData['AuthservID'] = _make_authserv_id(configData.get('AuthservID', 'HOSTNAME')) + except Exception as e: + syslog.syslog("Could not make AuthservID: {}".format(e)) + pass + + try: configData['IntHosts'] = HostsDataset(configData['InternalHosts']) - except: + except Exception as e: + syslog.syslog("Could not make HostDataset from InternalHosts: {}".format(e)) pass return(configData)