From f25909b34b020e2e9c25c866dcd7aca4c0c38bd0 Mon Sep 17 00:00:00 2001 From: Thomas Oettli Date: Tue, 7 Sep 2021 22:44:12 +0200 Subject: [PATCH] add header condition, rework conditions logic and fix logging --- README.md | 4 +- pymodmilter/__init__.py | 29 ++++++++++---- pymodmilter/actions.py | 13 ++++--- pymodmilter/conditions.py | 82 ++++++++++++++++++++++++++------------- pymodmilter/rules.py | 47 +++++----------------- 5 files changed, 96 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index bbce560..a349d98 100644 --- a/README.md +++ b/README.md @@ -67,10 +67,10 @@ Config options for **action** objects: * **store** * **conditions** (optional) A list of conditions which all have to be true to process the action. -* **pretend** (optional) - Just pretend all actions of this rule, for test purposes. * **loglevel** (optional) As described above in the [Global](#Global) section. +* **pretend** (optional) + As described above in the [Global](#Global) section. Config options for **add_header** actions: * **field** diff --git a/pymodmilter/__init__.py b/pymodmilter/__init__.py index 7eccecf..2eeab12 100644 --- a/pymodmilter/__init__.py +++ b/pymodmilter/__init__.py @@ -32,6 +32,7 @@ import json from Milter.utils import parse_addr from collections import defaultdict +from copy import copy from email import message_from_binary_file from email.header import Header, decode_header, make_header from email.headerregistry import AddressHeader, _default_header_map @@ -239,14 +240,29 @@ class ModifyMilter(Milter.Base): self.logger = CustomLogger(self.logger, {"qid": self.qid}) self.logger.debug("received queue-id from MTA") + # pre-filter rules and actions by the host condition, other + # conditions (headers, envelope-from, envelope-to) may get + # changed by executed actions later on. + # rules are copied to preserve the originally configured actions. + # also check if the mail body is needed by any upcoming action. + self.rules = [] self._headersonly = True for rule in ModifyMilter._rules: - if not rule.ignores(host=self.IP, envfrom=self.mailfrom, - envto=[*self.rcpts]): - self.rules.append(rule) - if rule.need_body(): - self._headersonly = False + if rule.conditions is None or \ + rule.conditions.match(host=self.IP): + actions = [] + for action in rule.actions: + if action.conditions is None or \ + action.conditions.match(host=self.IP): + actions.append(action) + if action.need_body(): + self._headersonly = False + + if actions: + rule = copy(rule) + rule.actions = actions + self.rules.append(rule) if not self.rules: self.logger.debug( @@ -311,11 +327,11 @@ class ModifyMilter(Milter.Base): def eom(self): try: + # setup msg and msg_info to be read/modified by rules and actions self.fp.seek(0) self.msg = message_from_binary_file( self.fp, _class=MilterMessage, policy=SMTPUTF8.clone( refold_source='none')) - self.msg_info = defaultdict(str) self.msg_info["ip"] = self.IP self.msg_info["port"] = self.port @@ -328,7 +344,6 @@ class ModifyMilter(Milter.Base): milter_action = None for rule in self.rules: milter_action = rule.execute(self) - if milter_action is not None: break diff --git a/pymodmilter/actions.py b/pymodmilter/actions.py index 3568cb5..a985434 100644 --- a/pymodmilter/actions.py +++ b/pymodmilter/actions.py @@ -518,13 +518,14 @@ class Action: """Return the needs of this action.""" return self._need_body - def execute(self, milter, pretend=None): + def execute(self, milter): """Execute configured action.""" - if pretend is None: - pretend = self.pretend - logger = CustomLogger( self.logger, {"name": self._name, "qid": milter.qid}) - return self._func(milter=milter, pretend=pretend, - logger=logger, **self._args) + if self.conditions is None or \ + self.conditions.match(envfrom=milter.mailfrom, + envto=[*milter.rcpts], + headers=milter.msg.items()): + return self._func(milter=milter, pretend=self.pretend, + logger=logger, **self._args) diff --git a/pymodmilter/conditions.py b/pymodmilter/conditions.py index 0465a14..b47d78c 100644 --- a/pymodmilter/conditions.py +++ b/pymodmilter/conditions.py @@ -19,7 +19,7 @@ __all__ = [ import re from netaddr import IPAddress, IPNetwork, AddrFormatError -from pymodmilter import BaseConfig +from pymodmilter import CustomLogger, BaseConfig class ConditionsConfig(BaseConfig): @@ -58,6 +58,15 @@ class ConditionsConfig(BaseConfig): except re.error as e: raise ValueError(f"{self['name']}: {arg}: {e}") + if "header" in cfg: + self.add_string_arg(cfg, "header") + try: + self["args"]["header"] = re.compile( + self["args"]["header"], + re.IGNORECASE + re.DOTALL + re.MULTILINE) + except re.error as e: + raise ValueError(f"{self['name']}: header: {e}") + self.logger.debug(f"{self['name']}: " f"loglevel={self['loglevel']}, " f"args={self['args']}") @@ -70,11 +79,13 @@ class Conditions: self.logger = cfg.logger self._local_addrs = milter_cfg["local_addrs"] + self._name = cfg["name"] self._args = cfg["args"] - def match(self, args): - if "host" in args: - ip = IPAddress(args["host"]) + def match(self, host=None, envfrom=None, envto=None, headers=None): + logger = CustomLogger(self.logger, {"name": self._name}) + if host: + ip = IPAddress(host) if "local" in self._args: is_local = False @@ -84,13 +95,13 @@ class Conditions: break if is_local != self._args["local"]: - self.logger.debug( - f"ignore host {args['host']}, " + logger.debug( + f"ignore host {host}, " f"condition local does not match") return False - self.logger.debug( - f"condition local matches for host {args['host']}") + logger.debug( + f"condition local matches for host {host}") if "hosts" in self._args: found = False @@ -100,38 +111,55 @@ class Conditions: break if not found: - self.logger.debug( - f"ignore host {args['host']}, " + logger.debug( + f"ignore host {host}, " f"condition hosts does not match") return False - self.logger.debug( - f"condition hosts matches for host {args['host']}") + logger.debug( + f"condition hosts matches for host {host}") - if "envfrom" in args and "envfrom" in self._args: - if not self._args["envfrom"].match(args["envfrom"]): - self.logger.debug( - f"ignore envelope-from address {args['envfrom']}, " + if envfrom and "envfrom" in self._args: + if not self._args["envfrom"].match(envfrom): + logger.debug( + f"ignore envelope-from address {envfrom}, " f"condition envfrom does not match") return False - self.logger.debug( + logger.debug( f"condition envfrom matches for " - f"envelope-from address {args['envfrom']}") + f"envelope-from address {envfrom}") - if "envto" in args and "envto" in self._args: - if not isinstance(args["envto"], list): - args["envto"] = [args["envto"]] + if envto and "envto" in self._args: + if not isinstance(envto, list): + envto = [envto] - for envto in args["envto"]: - if not self._args["envto"].match(envto): - self.logger.debug( - f"ignore envelope-to address {args['envto']}, " + for to in envto: + if not self._args["envto"].match(to): + logger.debug( + f"ignore envelope-to address {envto}, " f"condition envto does not match") return False - self.logger.debug( + logger.debug( f"condition envto matches for " - f"envelope-to address {args['envto']}") + f"envelope-to address {envto}") + + if headers and "header" in self._args: + match = None + for field, value in headers: + header = f"{field}: {value}" + match = self._args["header"].search(header) + if match: + logger.debug( + f"condition header matches for " + f"header: {header}") + break + + if not match: + logger.debug( + "ignore message, " + "condition header does not match") + return False return True diff --git a/pymodmilter/rules.py b/pymodmilter/rules.py index 9c0c5fb..ef925eb 100644 --- a/pymodmilter/rules.py +++ b/pymodmilter/rules.py @@ -77,46 +77,19 @@ class Rule: else: self.conditions = Conditions(milter_cfg, cfg["conditions"]) - self._need_body = False - self.actions = [] for action_cfg in cfg["actions"]: - action = Action(milter_cfg, action_cfg) - self.actions.append(action) - if action.need_body(): - self._need_body = True + self.actions.append(Action(milter_cfg, action_cfg)) self.pretend = cfg["pretend"] - def need_body(self): - """Return True if this rule needs the message body.""" - return self._need_body - - def ignores(self, host=None, envfrom=None, envto=None): - args = {} - - if host is not None: - args["host"] = host - - if envfrom is not None: - args["envfrom"] = envfrom - - if envto is not None: - args["envto"] = envto - - if self.conditions is None or self.conditions.match(args): - for action in self.actions: - if action.conditions is None or action.conditions.match(args): - return False - - return True - - def execute(self, milter, pretend=None): + def execute(self, milter): """Execute all actions of this rule.""" - if pretend is None: - pretend = self.pretend - - for action in self.actions: - milter_action = action.execute(milter, pretend=pretend) - if milter_action is not None: - return milter_action + if self.conditions is None or \ + self.conditions.match(envfrom=milter.mailfrom, + envto=[*milter.rcpts], + headers=milter.msg.items()): + for action in self.actions: + milter_action = action.execute(milter) + if milter_action is not None: + return milter_action