2019-12-18 14:25:09 +01:00
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import Milter
|
|
|
|
|
2019-12-18 15:12:33 +01:00
|
|
|
import re
|
|
|
|
|
2019-12-18 14:25:09 +01:00
|
|
|
# Basic logger that also logs to stdout
|
|
|
|
# TODO: Improve this a lot.
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
handler = logging.StreamHandler(sys.stdout)
|
|
|
|
handler.setLevel(logging.DEBUG)
|
2019-12-18 15:35:23 +01:00
|
|
|
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
2019-12-18 14:25:09 +01:00
|
|
|
handler.setFormatter(formatter)
|
|
|
|
|
|
|
|
logger.addHandler(handler)
|
|
|
|
|
2019-12-18 15:12:33 +01:00
|
|
|
split_from_regex = re.compile('(?P<from_label>("(.*)")|(.*))(.*)<(?P<from_address>.*)>')
|
2019-12-18 15:35:23 +01:00
|
|
|
address_domain_regex = re.compile('.*@(?P<domain>[\.\w-]+)')
|
|
|
|
|
2019-12-18 15:12:33 +01:00
|
|
|
|
|
|
|
def splitFromHeader(value):
|
2019-12-18 15:35:23 +01:00
|
|
|
"""Split 'From:' header into label and address values."""
|
2019-12-18 15:12:33 +01:00
|
|
|
match = split_from_regex.match(value)
|
2019-12-18 15:35:23 +01:00
|
|
|
return {
|
2019-12-18 15:12:33 +01:00
|
|
|
'label': match.group('from_label').strip(),
|
|
|
|
'address': match.group('from_address').strip()
|
|
|
|
}
|
2019-12-18 15:35:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
def labelContainsAddress(label):
|
|
|
|
""" Check whether given 'From:' header label contains something that looks like an email address."""
|
|
|
|
return address_domain_regex.match(label) is not None
|
|
|
|
|
|
|
|
|
|
|
|
def labelAndAddressDomainsMatch(split):
|
|
|
|
label_domain = address_domain_regex.match(split['label']).group('domain').strip()
|
|
|
|
address_domain = address_domain_regex.match(split['address']).group('domain').strip()
|
|
|
|
return label_domain.lower() == address_domain.lower()
|
|
|
|
|
2019-12-18 15:12:33 +01:00
|
|
|
|
2019-12-18 14:25:09 +01:00
|
|
|
class SuspiciousFrom(Milter.Base):
|
|
|
|
def __init__(self):
|
|
|
|
self.id = Milter.uniqueID()
|
2019-12-18 15:35:23 +01:00
|
|
|
self.final_result = Milter.ACCEPT
|
2019-12-18 15:12:33 +01:00
|
|
|
self.new_headers = []
|
2019-12-18 15:35:23 +01:00
|
|
|
logger.info(f"{self.id} got fired up.")
|
2019-12-18 14:25:09 +01:00
|
|
|
|
|
|
|
def header(self, field, value):
|
2019-12-18 15:35:23 +01:00
|
|
|
"""Header hook gets called for every header within the email processed."""
|
2019-12-18 15:12:33 +01:00
|
|
|
if field.lower() == 'from':
|
|
|
|
logger.info(f"Got \"From:\" header with raw value: '{value}'")
|
|
|
|
split = splitFromHeader(value)
|
|
|
|
logger.info(f"Label: {split['label']}, address: {split['address']}")
|
2019-12-18 15:35:23 +01:00
|
|
|
if labelContainsAddress(split['label']):
|
|
|
|
logger.info()
|
|
|
|
if labelAndAddressDomainsMatch(split):
|
|
|
|
self.new_headers.append({'name': 'X-From-Checked', 'value': 'Maybe multiple domains - no match - BAD!'})
|
|
|
|
self.final_result = Milter.ACCEPT
|
|
|
|
else:
|
|
|
|
self.new_headers.append({'name': 'X-From-Checked', 'value': 'Multiple domains - no match - BAD!'})
|
|
|
|
self.final_result = Milter.ACCEPT
|
2019-12-18 15:12:33 +01:00
|
|
|
else:
|
|
|
|
# Supposedly no additional address in the label, accept it for now
|
|
|
|
# TODO: Also decode utf-8 weirdness and check in there
|
2019-12-18 15:35:23 +01:00
|
|
|
self.new_headers.append({'name': 'X-From-Checked', 'value': 'Yes, no address in label.'})
|
|
|
|
self.final_result = Milter.ACCEPT
|
|
|
|
# Use continue here, so we can reach eom hook.
|
|
|
|
# TODO: Log and react if multiple From-headers are found?
|
|
|
|
return Milter.CONTINUE
|
2019-12-18 15:12:33 +01:00
|
|
|
|
|
|
|
def eom(self):
|
2019-12-18 15:35:23 +01:00
|
|
|
"""EOM hook gets called at the end of message processed. Headers and final verdict are applied only here."""
|
2019-12-18 15:12:33 +01:00
|
|
|
# Finish up message according to results collected on the way.
|
|
|
|
for new_header in self.new_headers:
|
|
|
|
self.addheader(new_header['name'], new_header['value'])
|
2019-12-18 15:35:23 +01:00
|
|
|
return self.final_result
|
2019-12-18 15:12:33 +01:00
|
|
|
|
2019-12-18 14:25:09 +01:00
|
|
|
|
|
|
|
def main():
|
2019-12-18 15:35:23 +01:00
|
|
|
# TODO: Move this into configuration of some sort.
|
2019-12-18 14:25:09 +01:00
|
|
|
milter_socket = "inet:7777@127.0.0.1"
|
|
|
|
milter_timeout = 60
|
|
|
|
Milter.factory = SuspiciousFrom
|
|
|
|
logger.info(f"Starting Milter.")
|
|
|
|
# This call blocks the main thread.
|
2019-12-18 15:35:23 +01:00
|
|
|
# TODO: Improve handling CTRL+C
|
2019-12-18 14:25:09 +01:00
|
|
|
Milter.runmilter("SuspiciousFromMilter", milter_socket, milter_timeout, rmsock=False)
|
|
|
|
logger.info(f"Milter finished running.")
|
|
|
|
|
2019-12-18 15:35:23 +01:00
|
|
|
|
2019-12-18 14:25:09 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|