add header condition, rework conditions logic and fix logging
This commit is contained in:
@@ -67,10 +67,10 @@ Config options for **action** objects:
|
|||||||
* **store**
|
* **store**
|
||||||
* **conditions** (optional)
|
* **conditions** (optional)
|
||||||
A list of conditions which all have to be true to process the action.
|
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)
|
* **loglevel** (optional)
|
||||||
As described above in the [Global](#Global) section.
|
As described above in the [Global](#Global) section.
|
||||||
|
* **pretend** (optional)
|
||||||
|
As described above in the [Global](#Global) section.
|
||||||
|
|
||||||
Config options for **add_header** actions:
|
Config options for **add_header** actions:
|
||||||
* **field**
|
* **field**
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ import json
|
|||||||
|
|
||||||
from Milter.utils import parse_addr
|
from Milter.utils import parse_addr
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
from copy import copy
|
||||||
from email import message_from_binary_file
|
from email import message_from_binary_file
|
||||||
from email.header import Header, decode_header, make_header
|
from email.header import Header, decode_header, make_header
|
||||||
from email.headerregistry import AddressHeader, _default_header_map
|
from email.headerregistry import AddressHeader, _default_header_map
|
||||||
@@ -239,15 +240,30 @@ class ModifyMilter(Milter.Base):
|
|||||||
self.logger = CustomLogger(self.logger, {"qid": self.qid})
|
self.logger = CustomLogger(self.logger, {"qid": self.qid})
|
||||||
self.logger.debug("received queue-id from MTA")
|
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.rules = []
|
||||||
self._headersonly = True
|
self._headersonly = True
|
||||||
for rule in ModifyMilter._rules:
|
for rule in ModifyMilter._rules:
|
||||||
if not rule.ignores(host=self.IP, envfrom=self.mailfrom,
|
if rule.conditions is None or \
|
||||||
envto=[*self.rcpts]):
|
rule.conditions.match(host=self.IP):
|
||||||
self.rules.append(rule)
|
actions = []
|
||||||
if rule.need_body():
|
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
|
self._headersonly = False
|
||||||
|
|
||||||
|
if actions:
|
||||||
|
rule = copy(rule)
|
||||||
|
rule.actions = actions
|
||||||
|
self.rules.append(rule)
|
||||||
|
|
||||||
if not self.rules:
|
if not self.rules:
|
||||||
self.logger.debug(
|
self.logger.debug(
|
||||||
"message is ignored by all rules, skip further processing")
|
"message is ignored by all rules, skip further processing")
|
||||||
@@ -311,11 +327,11 @@ class ModifyMilter(Milter.Base):
|
|||||||
|
|
||||||
def eom(self):
|
def eom(self):
|
||||||
try:
|
try:
|
||||||
|
# setup msg and msg_info to be read/modified by rules and actions
|
||||||
self.fp.seek(0)
|
self.fp.seek(0)
|
||||||
self.msg = message_from_binary_file(
|
self.msg = message_from_binary_file(
|
||||||
self.fp, _class=MilterMessage, policy=SMTPUTF8.clone(
|
self.fp, _class=MilterMessage, policy=SMTPUTF8.clone(
|
||||||
refold_source='none'))
|
refold_source='none'))
|
||||||
|
|
||||||
self.msg_info = defaultdict(str)
|
self.msg_info = defaultdict(str)
|
||||||
self.msg_info["ip"] = self.IP
|
self.msg_info["ip"] = self.IP
|
||||||
self.msg_info["port"] = self.port
|
self.msg_info["port"] = self.port
|
||||||
@@ -328,7 +344,6 @@ class ModifyMilter(Milter.Base):
|
|||||||
milter_action = None
|
milter_action = None
|
||||||
for rule in self.rules:
|
for rule in self.rules:
|
||||||
milter_action = rule.execute(self)
|
milter_action = rule.execute(self)
|
||||||
|
|
||||||
if milter_action is not None:
|
if milter_action is not None:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|||||||
@@ -518,13 +518,14 @@ class Action:
|
|||||||
"""Return the needs of this action."""
|
"""Return the needs of this action."""
|
||||||
return self._need_body
|
return self._need_body
|
||||||
|
|
||||||
def execute(self, milter, pretend=None):
|
def execute(self, milter):
|
||||||
"""Execute configured action."""
|
"""Execute configured action."""
|
||||||
if pretend is None:
|
|
||||||
pretend = self.pretend
|
|
||||||
|
|
||||||
logger = CustomLogger(
|
logger = CustomLogger(
|
||||||
self.logger, {"name": self._name, "qid": milter.qid})
|
self.logger, {"name": self._name, "qid": milter.qid})
|
||||||
|
|
||||||
return self._func(milter=milter, pretend=pretend,
|
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)
|
logger=logger, **self._args)
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ __all__ = [
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
from netaddr import IPAddress, IPNetwork, AddrFormatError
|
from netaddr import IPAddress, IPNetwork, AddrFormatError
|
||||||
from pymodmilter import BaseConfig
|
from pymodmilter import CustomLogger, BaseConfig
|
||||||
|
|
||||||
|
|
||||||
class ConditionsConfig(BaseConfig):
|
class ConditionsConfig(BaseConfig):
|
||||||
@@ -58,6 +58,15 @@ class ConditionsConfig(BaseConfig):
|
|||||||
except re.error as e:
|
except re.error as e:
|
||||||
raise ValueError(f"{self['name']}: {arg}: {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']}: "
|
self.logger.debug(f"{self['name']}: "
|
||||||
f"loglevel={self['loglevel']}, "
|
f"loglevel={self['loglevel']}, "
|
||||||
f"args={self['args']}")
|
f"args={self['args']}")
|
||||||
@@ -70,11 +79,13 @@ class Conditions:
|
|||||||
self.logger = cfg.logger
|
self.logger = cfg.logger
|
||||||
|
|
||||||
self._local_addrs = milter_cfg["local_addrs"]
|
self._local_addrs = milter_cfg["local_addrs"]
|
||||||
|
self._name = cfg["name"]
|
||||||
self._args = cfg["args"]
|
self._args = cfg["args"]
|
||||||
|
|
||||||
def match(self, args):
|
def match(self, host=None, envfrom=None, envto=None, headers=None):
|
||||||
if "host" in args:
|
logger = CustomLogger(self.logger, {"name": self._name})
|
||||||
ip = IPAddress(args["host"])
|
if host:
|
||||||
|
ip = IPAddress(host)
|
||||||
|
|
||||||
if "local" in self._args:
|
if "local" in self._args:
|
||||||
is_local = False
|
is_local = False
|
||||||
@@ -84,13 +95,13 @@ class Conditions:
|
|||||||
break
|
break
|
||||||
|
|
||||||
if is_local != self._args["local"]:
|
if is_local != self._args["local"]:
|
||||||
self.logger.debug(
|
logger.debug(
|
||||||
f"ignore host {args['host']}, "
|
f"ignore host {host}, "
|
||||||
f"condition local does not match")
|
f"condition local does not match")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.logger.debug(
|
logger.debug(
|
||||||
f"condition local matches for host {args['host']}")
|
f"condition local matches for host {host}")
|
||||||
|
|
||||||
if "hosts" in self._args:
|
if "hosts" in self._args:
|
||||||
found = False
|
found = False
|
||||||
@@ -100,38 +111,55 @@ class Conditions:
|
|||||||
break
|
break
|
||||||
|
|
||||||
if not found:
|
if not found:
|
||||||
self.logger.debug(
|
logger.debug(
|
||||||
f"ignore host {args['host']}, "
|
f"ignore host {host}, "
|
||||||
f"condition hosts does not match")
|
f"condition hosts does not match")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.logger.debug(
|
logger.debug(
|
||||||
f"condition hosts matches for host {args['host']}")
|
f"condition hosts matches for host {host}")
|
||||||
|
|
||||||
if "envfrom" in args and "envfrom" in self._args:
|
if envfrom and "envfrom" in self._args:
|
||||||
if not self._args["envfrom"].match(args["envfrom"]):
|
if not self._args["envfrom"].match(envfrom):
|
||||||
self.logger.debug(
|
logger.debug(
|
||||||
f"ignore envelope-from address {args['envfrom']}, "
|
f"ignore envelope-from address {envfrom}, "
|
||||||
f"condition envfrom does not match")
|
f"condition envfrom does not match")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.logger.debug(
|
logger.debug(
|
||||||
f"condition envfrom matches for "
|
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 envto and "envto" in self._args:
|
||||||
if not isinstance(args["envto"], list):
|
if not isinstance(envto, list):
|
||||||
args["envto"] = [args["envto"]]
|
envto = [envto]
|
||||||
|
|
||||||
for envto in args["envto"]:
|
for to in envto:
|
||||||
if not self._args["envto"].match(envto):
|
if not self._args["envto"].match(to):
|
||||||
self.logger.debug(
|
logger.debug(
|
||||||
f"ignore envelope-to address {args['envto']}, "
|
f"ignore envelope-to address {envto}, "
|
||||||
f"condition envto does not match")
|
f"condition envto does not match")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.logger.debug(
|
logger.debug(
|
||||||
f"condition envto matches for "
|
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
|
return True
|
||||||
|
|||||||
@@ -77,46 +77,19 @@ class Rule:
|
|||||||
else:
|
else:
|
||||||
self.conditions = Conditions(milter_cfg, cfg["conditions"])
|
self.conditions = Conditions(milter_cfg, cfg["conditions"])
|
||||||
|
|
||||||
self._need_body = False
|
|
||||||
|
|
||||||
self.actions = []
|
self.actions = []
|
||||||
for action_cfg in cfg["actions"]:
|
for action_cfg in cfg["actions"]:
|
||||||
action = Action(milter_cfg, action_cfg)
|
self.actions.append(Action(milter_cfg, action_cfg))
|
||||||
self.actions.append(action)
|
|
||||||
if action.need_body():
|
|
||||||
self._need_body = True
|
|
||||||
|
|
||||||
self.pretend = cfg["pretend"]
|
self.pretend = cfg["pretend"]
|
||||||
|
|
||||||
def need_body(self):
|
def execute(self, milter):
|
||||||
"""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):
|
|
||||||
"""Execute all actions of this rule."""
|
"""Execute all actions of this rule."""
|
||||||
if pretend is None:
|
if self.conditions is None or \
|
||||||
pretend = self.pretend
|
self.conditions.match(envfrom=milter.mailfrom,
|
||||||
|
envto=[*milter.rcpts],
|
||||||
|
headers=milter.msg.items()):
|
||||||
for action in self.actions:
|
for action in self.actions:
|
||||||
milter_action = action.execute(milter, pretend=pretend)
|
milter_action = action.execute(milter)
|
||||||
if milter_action is not None:
|
if milter_action is not None:
|
||||||
return milter_action
|
return milter_action
|
||||||
|
|||||||
Reference in New Issue
Block a user