18 Commits
0.0.5 ... 1.0.0

Author SHA1 Message Date
400c65eec8 Change version to 1.0.0 and introduce beta status 2020-02-11 09:01:10 +01:00
f0931daa67 Fix email address verification regex 2020-02-11 08:54:57 +01:00
e8265e45a5 Catch exceptions of get_body function 2020-02-11 08:53:29 +01:00
365fa9cd6d Fix debug log message and rename quarantines.py to storages.py 2020-02-03 12:07:16 +01:00
a10e341056 Use python library to extract body of emails 2020-02-03 02:57:50 +01:00
ab16c9f83e Restructure code and rename some config variables 2020-02-03 02:03:59 +01:00
182ca2bad7 Change version to 0.0.9 2020-02-01 21:08:12 +01:00
1508d39ed8 Fix encoding issues 2020-02-01 19:53:07 +01:00
42536befdb Fix typo in notifications.py 2020-01-29 22:26:30 +01:00
d09a453f3d Extend notification.template
Signed-off-by: Thomas Oettli <spacefreak@noop.ch>
2020-01-29 21:42:27 +01:00
983362a69a Add decoding of mail headers 2020-01-29 21:35:37 +01:00
f4399312b4 Add url encoded email template variables 2020-01-29 19:58:08 +01:00
b40e835215 Fix error during decoding of mail body 2020-01-27 22:47:57 +01:00
057e66f945 Fix CLI once more 2020-01-21 16:23:59 +01:00
49bc12f93b Change version to 0.0.7 2020-01-21 16:13:51 +01:00
0dd09e2d5a Fix syntax error in CLI 2020-01-21 16:12:40 +01:00
ec9a2e875b Change version to 0.0.6 2020-01-21 15:58:04 +01:00
7a31c01955 Fix handling of mails with missing headers 2020-01-21 15:56:08 +01:00
12 changed files with 866 additions and 784 deletions

View File

@@ -36,8 +36,8 @@ The following configuration options are mandatory in the global section:
The following configuration options are mandatory in each quarantine section: The following configuration options are mandatory in each quarantine section:
* **regex** * **regex**
Case insensitive regular expression to filter e-mail headers. Case insensitive regular expression to filter e-mail headers.
* **quarantine_type** * **storage_type**
One of the quarantine-types described below. One of the storage types described below.
* **action** * **action**
One of the actions described below. One of the actions described below.
* **notification_type** * **notification_type**
@@ -50,13 +50,13 @@ The following configuration options are mandatory in each quarantine section:
SMTP port SMTP port
The following configuration options are optional in each quarantine section: The following configuration options are optional in each quarantine section:
* **ignore_hosts** * **host_whitelist**
Comma-separated list of host and network addresses to be ignored by this quarantine. Comma-separated list of host and network addresses to be ignored by this quarantine.
* **reject_reason** * **reject_reason**
Reason to return to the client if action is set to reject. Reason to return to the client if action is set to reject.
### Quarantine types ### Storage types
* **NONE** * **NONE**
Original e-mails scrapped, sent to nirvana, black-holed or however you want to call it. Original e-mails scrapped, sent to nirvana, black-holed or however you want to call it.
@@ -64,7 +64,7 @@ The following configuration options are optional in each quarantine section:
Original e-mails are stored on the filesystem with a unique filename. The filename is available as a Original e-mails are stored on the filesystem with a unique filename. The filename is available as a
template variable used in notifiaction templates. template variable used in notifiaction templates.
The following configuration options are mandatory for this quarantine type: The following configuration options are mandatory for this quarantine type:
* **quarantine_directory** * **storage_directory**
The directory in which quarantined e-mails are stored. The directory in which quarantined e-mails are stored.
@@ -77,11 +77,17 @@ The following configuration options are optional in each quarantine section:
The following template variables are available: The following template variables are available:
* **{EMAIL_ENVELOPE_FROM}** * **{EMAIL_ENVELOPE_FROM}**
E-mail from-address received by the milter. E-mail sender address received by the milter.
* **{EMAIL_ENVELOPE_FROM_URL}**
Like EMAIL_ENVELOPE_FROM, but URL encoded
* **{EMAIL_FROM}** * **{EMAIL_FROM}**
Value of the from header of the original e-mail. Value of the FROM header of the original e-mail.
* **{EMAIL_TO}** * **{EMAIL_ENVELOPE_TO}**
E-mail recipient address of this notification. E-mail recipient address of this notification.
* **{EMAIL_ENVELOPE_TO_URL}**
Like EMAIL_ENVELOPE_TO, but URL encoded
* **{EMAIL_TO}**
Value of the TO header of the original e-mail.
* **{EMAIL_SUBJECT}** * **{EMAIL_SUBJECT}**
Configured e-mail subject. Configured e-mail subject.
* **{EMAIL_QUARANTINE_ID}** * **{EMAIL_QUARANTINE_ID}**

View File

@@ -30,12 +30,12 @@ preferred_quarantine_action = last
[spam] [spam]
# Option: ignore_hosts # Option: host_whitelist
# Notes: Set a list of host and network addresses to be ignored by this quarantine. # Notes: Set a list of host and network addresses to be ignored by this quarantine.
# All the common host/network notations are supported, including IPv6. # All the common host/network notations are supported, including IPv6.
# Value: [ HOST ] # Value: [ HOST ]
# #
ignore_hosts = 10.0.0.0/8,172.16.0.0/12,192.168.0.0/16 host_whitelist = 10.0.0.0/8,172.16.0.0/12,192.168.0.0/16
# Option: regex # Option: regex
# Notes: Set the case insensitive regular expression to match against email headers. # Notes: Set the case insensitive regular expression to match against email headers.
@@ -57,18 +57,18 @@ smtp_host = 127.0.0.1
# #
smtp_port = 25 smtp_port = 25
# Option: quarantine_type # Option: storage_type
# Notes: Set the quarantine type. # Notes: Set the storage type.
# Values: [ file | none ] # Values: [ file | none ]
# #
quarantine_type = file storage_type = file
# Option: quarantine_directory # Option: storage_directory
# Notes: Set the directory to store quarantined emails. # Notes: Set the directory to store quarantined emails.
# This option is needed by quarantine type 'file'. # This option is needed by quarantine type 'file'.
# Values: [ DIRECTORY ] # Values: [ DIRECTORY ]
# #
quarantine_directory = /var/lib/pyquarantine/spam storage_directory = /var/lib/pyquarantine/spam
# Option: action # Option: action
# Notes: Set the milter action to perform if email is processed by this quarantine. # Notes: Set the milter action to perform if email is processed by this quarantine.

View File

@@ -10,6 +10,10 @@
<td><b>From:</b></td> <td><b>From:</b></td>
<td>{EMAIL_FROM}</td> <td>{EMAIL_FROM}</td>
</tr> </tr>
<tr>
<td><b>Envelope-To:</b></td>
<td>{EMAIL_ENVELOPE_TO}</td>
</tr>
<tr> <tr>
<td><b>To:</b></td> <td><b>To:</b></td>
<td>{EMAIL_TO}</td> <td>{EMAIL_TO}</td>

View File

@@ -13,13 +13,14 @@
# #
__all__ = [ __all__ = [
"Quarantine",
"QuarantineMilter", "QuarantineMilter",
"generate_milter_config", "setup_milter",
"reload_config", "reload_config",
"cli", "cli",
"mailer", "mailer",
"notifications", "notifications",
"quarantines", "storages",
"run", "run",
"version", "version",
"whitelists"] "whitelists"]
@@ -38,82 +39,300 @@ from collections import defaultdict
from io import BytesIO from io import BytesIO
from itertools import groupby from itertools import groupby
from netaddr import IPAddress, IPNetwork from netaddr import IPAddress, IPNetwork
from pyquarantine import quarantines from pyquarantine import mailer
from pyquarantine import notifications from pyquarantine import notifications
from pyquarantine import storages
from pyquarantine import whitelists from pyquarantine import whitelists
class QuarantineMilter(Milter.Base): class Quarantine(object):
"""QuarantineMilter based on Milter.Base to implement milter communication """Quarantine class suitable for QuarantineMilter
The class variable config needs to be filled with the result of the generate_milter_config function. The class holds all the objects and functions needed for QuarantineMilter quarantine.
""" """
config = None
global_config = None
# list of default config files
_config_files = [
"/etc/pyquarantine/pyquarantine.conf",
os.path.expanduser('~/pyquarantine.conf'),
"pyquarantine.conf"]
# list of possible actions # list of possible actions
_actions = { _actions = {
"ACCEPT": Milter.ACCEPT, "ACCEPT": Milter.ACCEPT,
"REJECT": Milter.REJECT, "REJECT": Milter.REJECT,
"DISCARD": Milter.DISCARD} "DISCARD": Milter.DISCARD}
def __init__(self, name, index=0, regex=None, storage=None, whitelist=None,
host_whitelist=[], notification=None, action="ACCEPT",
reject_reason=None):
self.logger = logging.getLogger(__name__)
self.name = name
self.index = index
if regex:
self.regex = re.compile(
regex, re.MULTILINE + re.DOTALL + re.IGNORECASE)
self.storage = storage
self.whitelist = whitelist
self.host_whitelist = host_whitelist
self.notification = notification
action = action.upper()
assert action in self._actions
self.action = action
self.milter_action = self._actions[action]
self.reject_reason = reject_reason
def setup_from_cfg(self, global_cfg, cfg, test=False):
defaults = {
"action": "accept",
"reject_reason": "Message rejected",
"storage_type": "none",
"notification_type": "none",
"whitelist_type": "none",
"host_whitelist": ""
}
# check config
for opt in ["regex", "smtp_host", "smtp_port"] + list(defaults.keys()):
if opt in cfg:
continue
if opt in global_cfg:
cfg[opt] = global_cfg[opt]
elif opt in defaults:
cfg[opt] = defaults[opt]
else:
raise RuntimeError(
"mandatory option '{}' not present in config section '{}' or 'global'".format(
opt, self.name))
# pre-compile regex
self.logger.debug(
"{}: compiling regex '{}'".format(
self.name, cfg["regex"]))
self.regex = re.compile(
cfg["regex"], re.MULTILINE + re.DOTALL + re.IGNORECASE)
self.smtp_host = cfg["smtp_host"]
self.smtp_port = cfg["smtp_port"]
# create storage instance
storage_type = cfg["storage_type"].lower()
if storage_type in storages.TYPES:
self.logger.debug(
"{}: initializing storage type '{}'".format(
self.name,
storage_type.upper()))
self.storage = storages.TYPES[storage_type](
self.name, global_cfg, cfg, test)
elif storage_type == "none":
self.logger.debug("{}: storage is NONE".format(self.name))
self.storage = None
else:
raise RuntimeError(
"{}: unknown storage type '{}'".format(
self.name, storage_type))
# create whitelist instance
whitelist_type = cfg["whitelist_type"].lower()
if whitelist_type in whitelists.TYPES:
self.logger.debug(
"{}: initializing whitelist type '{}'".format(
self.name,
whitelist_type.upper()))
self.whitelist = whitelists.TYPES[whitelist_type](
self.name, global_cfg, cfg, test)
elif whitelist_type == "none":
logger.debug("{}: whitelist is NONE".format(self.name))
self.whitelist = None
else:
raise RuntimeError(
"{}: unknown whitelist type '{}'".format(
self.name, whitelist_type))
# create notification instance
notification_type = cfg["notification_type"].lower()
if notification_type in notifications.TYPES:
self.logger.debug(
"{}: initializing notification type '{}'".format(
self.name,
notification_type.upper()))
self.notification = notifications.TYPES[notification_type](
self.name, global_cfg, cfg, test)
elif notification_type == "none":
self.logger.debug("{}: notification is NONE".format(self.name))
self.notification = None
else:
raise RuntimeError(
"{}: unknown notification type '{}'".format(
self.name, notification_type))
# determining milter action for this quarantine
action = cfg["action"].upper()
if action in self._actions:
self.logger.debug("{}: action is {}".format(self.name, action))
self.action = action
self.milter_action = self._actions[action]
else:
raise RuntimeError(
"{}: unknown action '{}'".format(self._name, action))
self.reject_reason = cfg["reject_reason"]
# create host/network whitelist
self.host_whitelist = []
host_whitelist = set([p.strip()
for p in cfg["host_whitelist"].split(",") if p])
for host in host_whitelist:
if not host:
continue
# parse network notation
try:
net = IPNetwork(host)
except AddrFormatError as e:
raise RuntimeError("{}: error parsing host_whitelist: {}".format(
self.name, e))
else:
self.host_whitelist.append(net)
if self.host_whitelist:
self.logger.debug(
"{}: host whitelist: {}".format(
self.name,
", ".join([str(ip) for ip in host_whitelist])))
def notify(self, storage_id, recipient=None, synchronous=True):
"Notify recipient about email in storage."
if not self.storage:
raise RuntimeError(
"storage type is set to None, unable to send notification")
if not self.notification:
raise RuntimeError(
"notification type is set to None, unable to send notification")
fp, metadata = self.storage.get_mail(storage_id)
if recipient is not None:
if recipient not in metadata["recipients"]:
raise RuntimeError("invalid recipient '{}'".format(recipient))
recipients = [recipient]
else:
recipients = metadata["recipients"]
self.notification.notify(
metadata["queue_id"], storage_id, metadata["mailfrom"],
recipients, metadata["headers"], fp,
metadata["subgroups"], metadata["named_subgroups"],
synchronous)
fp.close()
def release(self, storage_id, recipients=None):
"Release email from storage."
if not self.storage:
raise RuntimeError(
"storage type is set to None, unable to release email")
fp, metadata = self.storage.get_mail(storage_id)
try:
mail = fp.read()
fp.close()
except IOError as e:
raise RuntimeError("unable to read data file: {}".format(e))
if recipients and type(recipients) == str:
recipients = [recipients]
else:
recipients = metadata["recipients"]
for recipient in recipients:
if recipient not in metadata["recipients"]:
raise RuntimeError("invalid recipient '{}'".format(recipient))
try:
mailer.smtp_send(
self.smtp_host,
self.smtp_port,
metadata["mailfrom"],
recipient,
mail)
except Exception as e:
raise RuntimeError(
"error while sending email to '{}': {}".format(
recipient, e))
self.storage.delete(storage_id, recipient)
def get_storage(self):
return self.storage
def get_notification(self):
return self.notification
def get_whitelist(self):
return self.whitelist
def host_in_whitelist(self, hostaddr):
ip = IPAddress(hostaddr[0])
for entry in self.host_whitelist:
if ip in entry:
return true
return False
def match(self, header):
return self.regex.search(header)
class QuarantineMilter(Milter.Base):
"""QuarantineMilter based on Milter.Base to implement milter communication
The class variable quarantines needs to be filled by runng the setup_milter function.
"""
quarantines = []
preferred_quarantine_action = "first"
# list of default config files
_cfg_files = [
"/etc/pyquarantine/pyquarantine.conf",
os.path.expanduser('~/pyquarantine.conf'),
"pyquarantine.conf"]
def __init__(self): def __init__(self):
self.logger = logging.getLogger(__name__) self.logger = logging.getLogger(__name__)
# save config, it must not change during runtime # save runtime config, it must not change during runtime
self.global_config = QuarantineMilter.global_config self.quarantines = QuarantineMilter.quarantines
self.config = QuarantineMilter.config
def _get_preferred_quarantine(self): def _get_preferred_quarantine(self):
matching_quarantines = [ matching_quarantines = [
q for q in self.recipients_quarantines.values() if q] q for q in self.recipients_quarantines.values() if q]
if self.global_config["preferred_quarantine_action"] == "first": if self.preferred_quarantine_action == "first":
quarantine = sorted( quarantine = sorted(
matching_quarantines, matching_quarantines,
key=lambda x: x["index"])[0] key=lambda q: q.index)[0]
else: else:
quarantine = sorted( quarantine = sorted(
matching_quarantines, matching_quarantines,
key=lambda x: x["index"], key=lambda q: q.index,
reverse=True)[0] reverse=True)[0]
return quarantine return quarantine
@staticmethod @staticmethod
def get_configfiles(): def get_cfg_files():
return QuarantineMilter._config_files return QuarantineMilter._cfg_files
@staticmethod @staticmethod
def get_actions(): def set_cfg_files(cfg_files):
return QuarantineMilter._actions QuarantineMilter._cfg_files = cfg_files
@staticmethod def connect(self, hostname, family, hostaddr):
def set_configfiles(config_files): self.hostaddr = hostaddr
QuarantineMilter._config_files = config_files
def connect(self, IPname, family, hostaddr):
self.logger.debug( self.logger.debug(
"accepted milter connection from {} port {}".format( "accepted milter connection from {} port {}".format(
*hostaddr)) *hostaddr))
ip = IPAddress(hostaddr[0]) for quarantine in self.quarantines.copy():
for quarantine in self.config.copy(): if quarantine.host_in_whitelist(hostaddr):
for ignore in quarantine["ignore_hosts_list"]:
if ip in ignore:
self.logger.debug(
"host {} is ignored by quarantine {}".format(
hostaddr[0], quarantine["name"]))
self.config.remove(quarantine)
break
if not self.config:
self.logger.debug( self.logger.debug(
"host {} is ignored by all quarantines, " "host {} is in whitelist of quarantine {}".format(
"skip further processing", hostaddr[0], quarantine["name"]))
hostaddr[0]) self.quarantines.remove(quarantine)
return Milter.ACCEPT if not self.quarantines:
self.logger.debug(
"host {} is in whitelist of all quarantines, "
"skip further processing",
hostaddr[0])
return Milter.ACCEPT
return Milter.CONTINUE return Milter.CONTINUE
@Milter.noreply @Milter.noreply
@@ -134,15 +353,33 @@ class QuarantineMilter(Milter.Base):
"{}: received queue-id from MTA".format(self.queueid)) "{}: received queue-id from MTA".format(self.queueid))
self.recipients = list(self.recipients) self.recipients = list(self.recipients)
self.headers = [] self.headers = []
self.logger.debug(
"{}: initializing memory buffer to save email data".format(
self.queueid))
# initialize memory buffer to save email data
self.fp = BytesIO()
return Milter.CONTINUE return Milter.CONTINUE
@Milter.noreply @Milter.noreply
def header(self, name, value): def header(self, name, value):
self.headers.append((name, value)) try:
# write email header to memory buffer
self.fp.write("{}: {}\r\n".format(name, value).encode(
encoding="ascii", errors="surrogateescape"))
# keep copy of header without surrogates for later use
self.headers.append((
name.encode(errors="surrogateescape").decode(errors="replace"),
value.encode(errors="surrogateescape").decode(errors="replace")))
except Exception as e:
self.logger.exception(
"an exception occured in header function: {}".format(e))
return Milter.TEMPFAIL
return Milter.CONTINUE return Milter.CONTINUE
def eoh(self): def eoh(self):
try: try:
self.fp.write("\r\n".encode(encoding="ascii"))
self.whitelist_cache = whitelists.WhitelistCache() self.whitelist_cache = whitelists.WhitelistCache()
# initialize dicts to set quaranines per recipient and keep matches # initialize dicts to set quaranines per recipient and keep matches
@@ -157,39 +394,39 @@ class QuarantineMilter(Milter.Base):
"{}: checking header against configured quarantines: {}".format( "{}: checking header against configured quarantines: {}".format(
self.queueid, header)) self.queueid, header))
# iterate quarantines # iterate quarantines
for quarantine in self.config: for quarantine in self.quarantines:
if len(self.recipients_quarantines) == len( if len(self.recipients_quarantines) == len(
self.recipients): self.recipients):
# every recipient matched a quarantine already # every recipient matched a quarantine already
if quarantine["index"] >= max( if quarantine.index >= max(
[q["index"] for q in self.recipients_quarantines.values()]): [q.index for q in self.recipients_quarantines.values()]):
# all recipients matched a quarantine with at least # all recipients matched a quarantine with at least
# the same precedence already, skip checks against # the same precedence already, skip checks against
# quarantines with lower precedence # quarantines with lower precedence
self.logger.debug( self.logger.debug(
"{}: {}: skip further checks of this header".format( "{}: {}: skip further checks of this header".format(
self.queueid, quarantine["name"])) self.queueid, quarantine.name))
break break
# check email header against quarantine regex # check email header against quarantine regex
self.logger.debug( self.logger.debug(
"{}: {}: checking header against regex '{}'".format( "{}: {}: checking header against regex '{}'".format(
self.queueid, quarantine["name"], quarantine["regex"])) self.queueid, quarantine.name, quarantine.regex))
match = quarantine["regex_compiled"].search(header) match = quarantine.match(header)
if match: if match:
self.logger.debug( self.logger.debug(
"{}: {}: header matched regex".format( "{}: {}: header matched regex".format(
self.queueid, quarantine["name"])) self.queueid, quarantine.name))
# check for whitelisted recipients # check for whitelisted recipients
whitelist = quarantine["whitelist_obj"] whitelist = quarantine.get_whitelist()
if whitelist is not None: if whitelist:
try: try:
whitelisted_recipients = self.whitelist_cache.get_whitelisted_recipients( whitelisted_recipients = self.whitelist_cache.get_whitelisted_recipients(
whitelist, self.mailfrom, recipients_to_check) whitelist, self.mailfrom, recipients_to_check)
except RuntimeError as e: except RuntimeError as e:
self.logger.error( self.logger.error(
"{}: {}: unable to query whitelist: {}".format( "{}: {}: unable to query whitelist: {}".format(
self.queueid, quarantine["name"], e)) self.queueid, quarantine.name, e))
return Milter.TEMPFAIL return Milter.TEMPFAIL
else: else:
whitelisted_recipients = {} whitelisted_recipients = {}
@@ -200,19 +437,19 @@ class QuarantineMilter(Milter.Base):
# recipient is whitelisted in this quarantine # recipient is whitelisted in this quarantine
self.logger.debug( self.logger.debug(
"{}: {}: recipient '{}' is whitelisted".format( "{}: {}: recipient '{}' is whitelisted".format(
self.queueid, quarantine["name"], recipient)) self.queueid, quarantine.name, recipient))
continue continue
if recipient not in self.recipients_quarantines.keys() or \ if recipient not in self.recipients_quarantines.keys() or \
self.recipients_quarantines[recipient]["index"] > quarantine["index"]: self.recipients_quarantines[recipient].index > quarantine.index:
self.logger.debug( self.logger.debug(
"{}: {}: set quarantine for recipient '{}'".format( "{}: {}: set quarantine for recipient '{}'".format(
self.queueid, quarantine["name"], recipient)) self.queueid, quarantine.name, recipient))
# save match for later use as template # save match for later use as template
# variables # variables
self.quarantines_matches[quarantine["name"]] = match self.quarantines_matches[quarantine.name] = match
self.recipients_quarantines[recipient] = quarantine self.recipients_quarantines[recipient] = quarantine
if quarantine["index"] == 0: if quarantine.index == 0:
# we do not need to check recipients which # we do not need to check recipients which
# matched the quarantine with the highest # matched the quarantine with the highest
# precedence already # precedence already
@@ -221,7 +458,7 @@ class QuarantineMilter(Milter.Base):
self.logger.debug( self.logger.debug(
"{}: {}: a quarantine with same or higher precedence " "{}: {}: a quarantine with same or higher precedence "
"matched already for recipient '{}'".format( "matched already for recipient '{}'".format(
self.queueid, quarantine["name"], recipient)) self.queueid, quarantine.name, recipient))
if not recipients_to_check: if not recipients_to_check:
self.logger.debug( self.logger.debug(
@@ -238,38 +475,24 @@ class QuarantineMilter(Milter.Base):
self.queueid)) self.queueid))
return Milter.ACCEPT return Milter.ACCEPT
# check if the email body is needed # check if the mail body is needed
keep_body = False
for recipient, quarantine in self.recipients_quarantines.items(): for recipient, quarantine in self.recipients_quarantines.items():
if quarantine["quarantine_obj"] or quarantine["notification_obj"]: if quarantine.get_storage() or quarantine.get_notification():
keep_body = True # mail body is needed, continue processing
break return Milter.CONTINUE
if keep_body: # quarantine and notification are disabled on all matching
self.logger.debug( # quarantines, just return configured action
"{}: initializing memory buffer to save email data".format( quarantine = self._get_preferred_quarantine()
self.queueid)) self.logger.info(
# initialize memory buffer to save email data "{}: {} matching quarantine is '{}', performing milter action {}".format(
self.fp = BytesIO() self.queueid,
# write email headers to memory buffer self.preferred_quarantine_action,
for name, value in self.headers: quarantine.name,
self.fp.write("{}: {}\n".format(name, value).encode()) quarantine.action))
self.fp.write("\n".encode()) if quarantine.action == "REJECT":
else: self.setreply("554", "5.7.0", quarantine.reject_reason)
# quarantine and notification are disabled on all matching return quarantine.milter_action
# quarantines, return configured action
quarantine = self._get_preferred_quarantine()
self.logger.info(
"{}: {} matching quarantine is '{}', performing milter action {}".format(
self.queueid,
self.global_config["preferred_quarantine_action"],
quarantine["name"],
quarantine["action"].upper()))
if quarantine["action"] == "reject":
self.setreply("554", "5.7.0", quarantine["reject_reason"])
return quarantine["milter_action"]
return Milter.CONTINUE
except Exception as e: except Exception as e:
self.logger.exception( self.logger.exception(
@@ -292,52 +515,54 @@ class QuarantineMilter(Milter.Base):
quarantines = [] quarantines = []
for quarantine, recipients in groupby( for quarantine, recipients in groupby(
sorted(self.recipients_quarantines, sorted(self.recipients_quarantines,
key=lambda x: self.recipients_quarantines[x]["index"]), key=lambda x: self.recipients_quarantines[x].index),
lambda x: self.recipients_quarantines[x]): lambda x: self.recipients_quarantines[x]):
quarantines.append((quarantine, list(recipients))) quarantines.append((quarantine, list(recipients)))
# iterate quarantines sorted by index # iterate quarantines sorted by index
for quarantine, recipients in sorted( for quarantine, recipients in sorted(
quarantines, key=lambda x: x[0]["index"]): quarantines, key=lambda x: x[0].index):
quarantine_id = ""
headers = defaultdict(str) headers = defaultdict(str)
for name, value in self.headers: for name, value in self.headers:
headers[name.lower()] = value headers[name.lower()] = value
subgroups = self.quarantines_matches[quarantine["name"]].groups( subgroups = self.quarantines_matches[quarantine.name].groups(
default="") default="")
named_subgroups = self.quarantines_matches[quarantine["name"]].groupdict( named_subgroups = self.quarantines_matches[quarantine.name].groupdict(
default="") default="")
# check if a quarantine is configured # check if a storage is configured
if quarantine["quarantine_obj"] is not None: storage_id = ""
storage = quarantine.get_storage()
if storage:
# add email to quarantine # add email to quarantine
self.logger.info("{}: adding to quarantine '{}' for: {}".format( self.logger.info("{}: adding to quarantine '{}' for: {}".format(
self.queueid, quarantine["name"], ", ".join(recipients))) self.queueid, quarantine.name, ", ".join(recipients)))
try: try:
quarantine_id = quarantine["quarantine_obj"].add( storage_id = storage.add(
self.queueid, self.mailfrom, recipients, headers, self.fp, self.queueid, self.mailfrom, recipients, headers, self.fp,
subgroups, named_subgroups) subgroups, named_subgroups)
except RuntimeError as e: except RuntimeError as e:
self.logger.error( self.logger.error(
"{}: unable to add to quarantine '{}': {}".format( "{}: unable to add to quarantine '{}': {}".format(
self.queueid, quarantine["name"], e)) self.queueid, quarantine.name, e))
return Milter.TEMPFAIL return Milter.TEMPFAIL
# check if a notification is configured # check if a notification is configured
if quarantine["notification_obj"] is not None: notification = quarantine.get_notification()
if notification:
# notify # notify
self.logger.info( self.logger.info(
"{}: sending notification for quarantine '{}' to: {}".format( "{}: sending notification for quarantine '{}' to: {}".format(
self.queueid, quarantine["name"], ", ".join(recipients))) self.queueid, quarantine.name, ", ".join(recipients)))
try: try:
quarantine["notification_obj"].notify( notification.notify(
self.queueid, quarantine_id, self.queueid, storage_id,
self.mailfrom, recipients, headers, self.fp, self.mailfrom, recipients, headers, self.fp,
subgroups, named_subgroups) subgroups, named_subgroups)
except RuntimeError as e: except RuntimeError as e:
self.logger.error( self.logger.error(
"{}: unable to send notification for quarantine '{}': {}".format( "{}: unable to send notification for quarantine '{}': {}".format(
self.queueid, quarantine["name"], e)) self.queueid, quarantine.name, e))
return Milter.TEMPFAIL return Milter.TEMPFAIL
# remove processed recipient # remove processed recipient
@@ -360,34 +585,40 @@ class QuarantineMilter(Milter.Base):
self.logger.info( self.logger.info(
"{}: {} matching quarantine is '{}', performing milter action {}".format( "{}: {} matching quarantine is '{}', performing milter action {}".format(
self.queueid, self.queueid,
self.global_config["preferred_quarantine_action"], self.preferred_quarantine_action,
quarantine["name"], quarantine.name,
quarantine["action"].upper())) quarantine.action))
if quarantine["action"] == "reject": if quarantine.action == "REJECT":
self.setreply("554", "5.7.0", quarantine["reject_reason"]) self.setreply("554", "5.7.0", quarantine.reject_reason)
return quarantine["milter_action"] return quarantine.milter_action
except Exception as e: except Exception as e:
self.logger.exception( self.logger.exception(
"an exception occured in eom function: {}".format(e)) "an exception occured in eom function: {}".format(e))
return Milter.TEMPFAIL return Milter.TEMPFAIL
def close(self):
self.logger.debug(
"disconnect from {} port {}".format(
*self.hostaddr))
return Milter.CONTINUE
def generate_milter_config(configtest=False, config_files=[]):
def setup_milter(test=False, cfg_files=[]):
"Generate the configuration for QuarantineMilter class." "Generate the configuration for QuarantineMilter class."
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# read config file # read config file
parser = configparser.ConfigParser() parser = configparser.ConfigParser()
if not config_files: if not cfg_files:
config_files = parser.read(QuarantineMilter.get_configfiles()) cfg_files = parser.read(QuarantineMilter.get_cfg_files())
else: else:
config_files = parser.read(config_files) cfg_files = parser.read(cfg_files)
if not config_files: if not cfg_files:
raise RuntimeError("config file not found") raise RuntimeError("config file not found")
QuarantineMilter.set_configfiles(config_files) QuarantineMilter.set_cfg_files(cfg_files)
os.chdir(os.path.dirname(config_files[0])) os.chdir(os.path.dirname(cfg_files[0]))
# check if mandatory config options in global section are present # check if mandatory config options in global section are present
if "global" not in parser.sections(): if "global" not in parser.sections():
@@ -399,164 +630,41 @@ def generate_milter_config(configtest=False, config_files=[]):
"mandatory option '{}' not present in config section 'global'".format(option)) "mandatory option '{}' not present in config section 'global'".format(option))
# read global config section # read global config section
global_config = dict(parser.items("global")) global_cfg = dict(parser.items("global"))
global_config["preferred_quarantine_action"] = global_config["preferred_quarantine_action"].lower() preferred_quarantine_action = global_cfg["preferred_quarantine_action"].lower()
if global_config["preferred_quarantine_action"] not in ["first", "last"]: if preferred_quarantine_action not in ["first", "last"]:
raise RuntimeError( raise RuntimeError(
"option preferred_quarantine_action has illegal value") "option preferred_quarantine_action has illegal value")
# read active quarantine names # read active quarantine names
quarantine_names = [ quarantines = [
q.strip() for q in global_config["quarantines"].split(",")] q.strip() for q in global_cfg["quarantines"].split(",")]
if len(quarantine_names) != len(set(quarantine_names)): if len(quarantines) != len(set(quarantines)):
raise RuntimeError( raise RuntimeError(
"at least one quarantine is specified multiple times in quarantines option") "at least one quarantine is specified multiple times in quarantines option")
if "global" in quarantine_names: if "global" in quarantines:
quarantine_names.remove("global") quarantines.remove("global")
logger.warning( logger.warning(
"removed illegal quarantine name 'global' from list of active quarantines") "removed illegal quarantine name 'global' from list of active quarantines")
if not quarantine_names: if not quarantines:
raise RuntimeError("no quarantines configured") raise RuntimeError("no quarantines configured")
milter_config = [] milter_quarantines = []
logger.debug("preparing milter configuration ...") logger.debug("preparing milter configuration ...")
# iterate quarantine names # iterate quarantine names
for index, quarantine_name in enumerate(quarantine_names): for index, name in enumerate(quarantines):
# check if config section for current quarantine exists # check if config section for current quarantine exists
if quarantine_name not in parser.sections(): if name not in parser.sections():
raise RuntimeError( raise RuntimeError(
"config section '{}' does not exist".format(quarantine_name)) "config section '{}' does not exist".format(name))
config = dict(parser.items(quarantine_name))
# check if mandatory config options are present in config cfg = dict(parser.items(name))
for option in ["regex", "quarantine_type", "notification_type", quarantine = Quarantine(name, index)
"action", "whitelist_type", "smtp_host", "smtp_port"]: quarantine.setup_from_cfg(global_cfg, cfg, test)
if option not in config.keys() and \ milter_quarantines.append(quarantine)
option in global_config.keys():
config[option] = global_config[option]
if option not in config.keys():
raise RuntimeError(
"mandatory option '{}' not present in config section '{}' or 'global'".format(
option, quarantine_name))
# check if optional config options are present in config QuarantineMilter.preferred_quarantine_action = preferred_quarantine_action
defaults = { QuarantineMilter.quarantines = milter_quarantines
"reject_reason": "Message rejected",
"ignore_hosts": ""
}
for option in defaults.keys():
if option not in config.keys() and \
option in global_config.keys():
config[option] = global_config[option]
if option not in config.keys():
config[option] = defaults[option]
# set quarantine name
config["name"] = quarantine_name
# set the index
config["index"] = index
# pre-compile regex
logger.debug(
"{}: compiling regex '{}'".format(
quarantine_name,
config["regex"]))
config["regex_compiled"] = re.compile(
config["regex"], re.MULTILINE + re.DOTALL + re.IGNORECASE)
# create quarantine instance
quarantine_type = config["quarantine_type"].lower()
if quarantine_type in quarantines.TYPES.keys():
logger.debug(
"{}: initializing quarantine type '{}'".format(
quarantine_name,
quarantine_type.upper()))
quarantine = quarantines.TYPES[quarantine_type](
global_config, config, configtest)
elif quarantine_type == "none":
logger.debug("{}: quarantine is NONE".format(quarantine_name))
quarantine = None
else:
raise RuntimeError(
"{}: unknown quarantine type '{}'".format(
quarantine_name, quarantine_type))
config["quarantine_obj"] = quarantine
# create whitelist instance
whitelist_type = config["whitelist_type"].lower()
if whitelist_type in whitelists.TYPES.keys():
logger.debug(
"{}: initializing whitelist type '{}'".format(
quarantine_name,
whitelist_type.upper()))
whitelist = whitelists.TYPES[whitelist_type](
global_config, config, configtest)
elif whitelist_type == "none":
logger.debug("{}: whitelist is NONE".format(quarantine_name))
whitelist = None
else:
raise RuntimeError(
"{}: unknown whitelist type '{}'".format(
quarantine_name, whitelist_type))
config["whitelist_obj"] = whitelist
# create notification instance
notification_type = config["notification_type"].lower()
if notification_type in notifications.TYPES.keys():
logger.debug(
"{}: initializing notification type '{}'".format(
quarantine_name,
notification_type.upper()))
notification = notifications.TYPES[notification_type](
global_config, config, configtest)
elif notification_type == "none":
logger.debug("{}: notification is NONE".format(quarantine_name))
notification = None
else:
raise RuntimeError(
"{}: unknown notification type '{}'".format(
quarantine_name, notification_type))
config["notification_obj"] = notification
# determining milter action for this quarantine
action = config["action"].upper()
if action in QuarantineMilter.get_actions().keys():
logger.debug("{}: action is {}".format(quarantine_name, action))
config["milter_action"] = QuarantineMilter.get_actions()[action]
else:
raise RuntimeError(
"{}: unknown action '{}'".format(
quarantine_name, action))
# create host/network whitelist
config["ignore_hosts_list"] = []
ignored = set([p.strip()
for p in config["ignore_hosts"].split(",") if p])
for ignore in ignored:
if not ignore:
continue
# parse network notation
try:
net = IPNetwork(ignore)
except AddrFormatError as e:
raise RuntimeError("error parsing ignore_hosts: {}".format(e))
else:
config["ignore_hosts_list"].append(net)
if config["ignore_hosts_list"]:
logger.debug(
"{}: ignore hosts: {}".format(
quarantine_name,
", ".join(ignored)))
milter_config.append(config)
return global_config, milter_config
def reload_config(): def reload_config():
@@ -564,11 +672,9 @@ def reload_config():
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
try: try:
global_config, config = generate_milter_config() setup_milter()
except RuntimeError as e: except RuntimeError as e:
logger.info(e) logger.info(e)
logger.info("daemon is still running with previous configuration") logger.info("daemon is still running with previous configuration")
else: else:
logger.info("reloading configuration") logger.info("reloaded configuration")
QuarantineMilter.global_config = global_config
QuarantineMilter.config = config

View File

@@ -20,27 +20,42 @@ import logging.handlers
import sys import sys
import time import time
from email.header import decode_header, make_header
import pyquarantine import pyquarantine
from pyquarantine.version import __version__ as version from pyquarantine.version import __version__ as version
def _get_quarantine_obj(config, quarantine): def _get_quarantine(quarantines, name):
try: try:
quarantine_obj = next((q["quarantine_obj"] quarantine = next((q for q in quarantines if q.name == name))
for q in config if q["name"] == quarantine))
except StopIteration: except StopIteration:
raise RuntimeError("invalid quarantine '{}'".format(quarantine)) raise RuntimeError("invalid quarantine '{}'".format(name))
return quarantine_obj return quarantine
def _get_storage(quarantines, name):
quarantine = _get_quarantine(quarantines, name)
storage = quarantine.get_storage()
if not storage:
raise RuntimeError(
"storage type is set to NONE")
return storage
def _get_whitelist_obj(config, quarantine): def _get_notification(quarantines, name):
try: quarantine = _get_quarantine(quarantines, name)
whitelist_obj = next((q["whitelist_obj"] notification = quarantine.get_notification()
for q in config if q["name"] == quarantine)) if not notification:
except StopIteration: raise RuntimeError(
raise RuntimeError("invalid quarantine '{}'".format(quarantine)) "notification type is set to NONE")
return whitelist_obj return notification
def _get_whitelist(quarantines, name):
quarantine = _get_quarantine(quarantines, name)
whitelist = quarantine.get_whitelist()
if not whitelist:
raise RuntimeError(
"whitelist type is set to NONE")
return whitelist
def print_table(columns, rows): def print_table(columns, rows):
if not rows: if not rows:
@@ -56,7 +71,7 @@ def print_table(columns, rows):
# get the length of the longest value # get the length of the longest value
lengths.append( lengths.append(
len(str(max(rows, key=lambda x: len(str(x[key])))[key]))) len(str(max(rows, key=lambda x: len(str(x[key])))[key])))
# use the the longer one # use the longer one
length = max(lengths) length = max(lengths)
column_lengths.append(length) column_lengths.append(length)
column_formats.append("{{:<{}}}".format(length)) column_formats.append("{{:<{}}}".format(length))
@@ -83,47 +98,72 @@ def print_table(columns, rows):
print(row_format.format(*row)) print(row_format.format(*row))
def list_quarantines(config, args): def list_quarantines(quarantines, args):
if args.batch: if args.batch:
print("\n".join([quarantine["name"] for quarantine in config])) print("\n".join([q.name for q in quarantines]))
else: else:
qlist = []
for q in quarantines:
storage = q.get_storage()
if storage:
storage_type = q.get_storage().storage_type
else:
storage_type = "NONE"
notification = q.get_notification()
if notification:
notification_type = q.get_notification().notification_type
else:
notification_type = "NONE"
whitelist = q.get_whitelist()
if whitelist:
whitelist_type = q.get_whitelist().whitelist_type
else:
whitelist_type = "NONE"
qlist.append({
"name": q.name,
"storage": storage_type,
"notification": notification_type,
"whitelist": whitelist_type,
"action": q.action})
print_table( print_table(
[("Name", "name"), ("Quarantine", "quarantine_type"), [("Name", "name"),
("Notification", "notification_type"), ("Action", "action")], ("Storage", "storage"),
config ("Notification", "notification"),
("Whitelist", "whitelist"),
("Action", "action")],
qlist
) )
def list_quarantine_emails(config, args): def list_quarantine_emails(quarantines, args):
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
storage = _get_storage(quarantines, args.quarantine)
# get quarantine object
quarantine = _get_quarantine_obj(config, args.quarantine)
if quarantine is None:
raise RuntimeError(
"quarantine type is set to None, unable to list emails")
# find emails and transform some metadata values to strings # find emails and transform some metadata values to strings
rows = [] rows = []
emails = quarantine.find( emails = storage.find(
mailfrom=args.mailfrom, args.mailfrom, args.recipients, args.older_than)
recipients=args.recipients, for storage_id, metadata in emails.items():
older_than=args.older_than) row = emails[storage_id]
for quarantine_id, metadata in emails.items(): row["storage_id"] = storage_id
row = emails[quarantine_id]
row["quarantine_id"] = quarantine_id
row["date"] = time.strftime( row["date"] = time.strftime(
'%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M:%S',
time.localtime( time.localtime(
metadata["date"])) metadata["date"]))
row["mailfrom"] = metadata["mailfrom"] row["mailfrom"] = metadata["mailfrom"]
row["recipient"] = metadata["recipients"].pop(0) row["recipient"] = metadata["recipients"].pop(0)
row["subject"] = emails[quarantine_id]["headers"]["subject"][:60] if "subject" not in emails[storage_id]["headers"].keys():
emails[storage_id]["headers"]["subject"] = ""
row["subject"] = str(make_header(decode_header(
emails[storage_id]["headers"]["subject"])))[:60].replace(
"\r", "").replace("\n", "").strip()
rows.append(row) rows.append(row)
if metadata["recipients"]: if metadata["recipients"]:
row = { row = {
"quarantine_id": "", "storage_id": "",
"date": "", "date": "",
"mailfrom": "", "mailfrom": "",
"recipient": metadata["recipients"].pop(0), "recipient": metadata["recipients"].pop(0),
@@ -139,21 +179,16 @@ def list_quarantine_emails(config, args):
if not emails: if not emails:
logger.info("quarantine '{}' is empty".format(args.quarantine)) logger.info("quarantine '{}' is empty".format(args.quarantine))
print_table( print_table(
[("Quarantine-ID", "quarantine_id"), ("Date", "date"), [("Quarantine-ID", "storage_id"), ("Date", "date"),
("From", "mailfrom"), ("Recipient(s)", "recipient"), ("From", "mailfrom"), ("Recipient(s)", "recipient"),
("Subject", "subject")], ("Subject", "subject")],
rows rows
) )
def list_whitelist(config, args): def list_whitelist(quarantines, args):
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
whitelist = _get_whitelist(quarantines, args.quarantine)
# get whitelist object
whitelist = _get_whitelist_obj(config, args.quarantine)
if whitelist is None:
raise RuntimeError(
"whitelist type is set to None, unable to list entries")
# find whitelist entries # find whitelist entries
entries = whitelist.find( entries = whitelist.find(
@@ -184,14 +219,9 @@ def list_whitelist(config, args):
) )
def add_whitelist_entry(config, args): def add_whitelist_entry(quarantines, args):
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
whitelist = _get_whitelist(quarantines, args.quarantine)
# get whitelist object
whitelist = _get_whitelist_obj(config, args.quarantine)
if whitelist is None:
raise RuntimeError(
"whitelist type is set to None, unable to add entries")
# check existing entries # check existing entries
entries = whitelist.check(args.mailfrom, args.recipient) entries = whitelist.check(args.mailfrom, args.recipient)
@@ -229,50 +259,31 @@ def add_whitelist_entry(config, args):
logger.info("whitelist entry added successfully") logger.info("whitelist entry added successfully")
def delete_whitelist_entry(config, args): def delete_whitelist_entry(quarantines, args):
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
whitelist = _get_whitelist(quarantines, args.quarantine)
whitelist = _get_whitelist_obj(config, args.quarantine)
if whitelist is None:
raise RuntimeError(
"whitelist type is set to None, unable to delete entries")
whitelist.delete(args.whitelist_id) whitelist.delete(args.whitelist_id)
logger.info("whitelist entry deleted successfully") logger.info("whitelist entry deleted successfully")
def notify_email(config, args): def notify(quarantines, args):
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
quarantine = _get_quarantine(quarantines, args.quarantine)
quarantine = _get_quarantine_obj(config, args.quarantine)
if quarantine is None:
raise RuntimeError(
"quarantine type is set to None, unable to send notification")
quarantine.notify(args.quarantine_id, args.recipient) quarantine.notify(args.quarantine_id, args.recipient)
logger.info("sent notification successfully") logger.info("notification sent successfully")
def release_email(config, args): def release(quarantines, args):
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
quarantine = _get_quarantine(quarantines, args.quarantine)
quarantine = _get_quarantine_obj(config, args.quarantine)
if quarantine is None:
raise RuntimeError(
"quarantine type is set to None, unable to release email")
quarantine.release(args.quarantine_id, args.recipient) quarantine.release(args.quarantine_id, args.recipient)
logger.info("quarantined email released successfully") logger.info("quarantined email released successfully")
def delete_email(config, args): def delete(quarantines, args):
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
storage = _get_storage(quarantines, args.quarantine)
quarantine = _get_quarantine_obj(config, args.quarantine) storage.delete(args.quarantine_id, args.recipient)
if quarantine is None:
raise RuntimeError(
"quarantine type is set to None, unable to delete email")
quarantine.delete(args.quarantine_id, args.recipient)
logger.info("quarantined email deleted successfully") logger.info("quarantined email deleted successfully")
@@ -298,7 +309,7 @@ def main():
"-c", "--config", "-c", "--config",
help="Config files to read.", help="Config files to read.",
nargs="+", metavar="CFG", nargs="+", metavar="CFG",
default=pyquarantine.QuarantineMilter.get_configfiles()) default=pyquarantine.QuarantineMilter.get_cfg_files())
parser.add_argument( parser.add_argument(
"-d", "--debug", "-d", "--debug",
help="Log debugging messages.", help="Log debugging messages.",
@@ -388,7 +399,7 @@ def main():
"-a", "--all", "-a", "--all",
help="Release email for all recipients.", help="Release email for all recipients.",
action="store_true") action="store_true")
quarantine_notify_parser.set_defaults(func=notify_email) quarantine_notify_parser.set_defaults(func=notify)
# quarantine release command # quarantine release command
quarantine_release_parser = quarantine_subparsers.add_parser( quarantine_release_parser = quarantine_subparsers.add_parser(
"release", "release",
@@ -415,7 +426,7 @@ def main():
"-a", "--all", "-a", "--all",
help="Release email for all recipients.", help="Release email for all recipients.",
action="store_true") action="store_true")
quarantine_release_parser.set_defaults(func=release_email) quarantine_release_parser.set_defaults(func=release)
# quarantine delete command # quarantine delete command
quarantine_delete_parser = quarantine_subparsers.add_parser( quarantine_delete_parser = quarantine_subparsers.add_parser(
"delete", "delete",
@@ -441,7 +452,7 @@ def main():
"-a", "--all", "-a", "--all",
help="Delete email for all recipients.", help="Delete email for all recipients.",
action="store_true") action="store_true")
quarantine_delete_parser.set_defaults(func=delete_email) quarantine_delete_parser.set_defaults(func=delete)
# whitelist command group # whitelist command group
whitelist_parser = subparsers.add_parser( whitelist_parser = subparsers.add_parser(
@@ -552,8 +563,8 @@ def main():
# try to generate milter configs # try to generate milter configs
try: try:
global_config, config = pyquarantine.generate_milter_config( pyquarantine.setup_milter(
config_files=args.config, configtest=True) cfg_files=args.config, test=True)
except RuntimeError as e: except RuntimeError as e:
logger.error(e) logger.error(e)
sys.exit(255) sys.exit(255)
@@ -574,7 +585,7 @@ def main():
# call the commands function # call the commands function
try: try:
args.func(config, args) args.func(pyquarantine.QuarantineMilter.quarantines, args)
except RuntimeError as e: except RuntimeError as e:
logger.error(e) logger.error(e)
sys.exit(1) sys.exit(1)

View File

@@ -19,24 +19,26 @@ import re
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from cgi import escape from cgi import escape
from collections import defaultdict from collections import defaultdict
from email import policy
from email.header import decode_header, make_header
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
from email.mime.image import MIMEImage from email.mime.image import MIMEImage
from os.path import basename from os.path import basename
from urllib.parse import quote
from pyquarantine import mailer from pyquarantine import mailer
class BaseNotification(object): class BaseNotification(object):
"Notification base class" "Notification base class"
notification_type = "base"
def __init__(self, global_config, config, configtest=False): def __init__(self, name, global_cfg, cfg, test=False):
self.quarantine_name = config["name"] self.name = name
self.global_config = global_config
self.config = config
self.logger = logging.getLogger(__name__) self.logger = logging.getLogger(__name__)
def notify(self, queueid, quarantine_id, mailfrom, recipients, headers, def notify(self, queueid, storage_id, mailfrom, recipients, headers,
fp, subgroups=None, named_subgroups=None, synchronous=False): fp, subgroups=None, named_subgroups=None, synchronous=False):
fp.seek(0) fp.seek(0)
pass pass
@@ -44,8 +46,7 @@ class BaseNotification(object):
class EMailNotification(BaseNotification): class EMailNotification(BaseNotification):
"Notification class to send notifications via mail." "Notification class to send notifications via mail."
_html_text = "text/html" notification_type = "email"
_plain_text = "text/plain"
_bad_tags = [ _bad_tags = [
"applet", "applet",
"embed", "embed",
@@ -109,44 +110,40 @@ class EMailNotification(BaseNotification):
"width" "width"
] ]
def __init__(self, global_config, config, configtest=False): def __init__(self, name, global_cfg, cfg, test=False):
super(EMailNotification, self).__init__( super(EMailNotification, self).__init__(
global_config, config, configtest) name, global_cfg, cfg, test)
# check if mandatory options are present in config defaults = {
for option in [ "notification_email_replacement_img": "",
"notification_email_strip_images": "false",
"notification_email_parser_lib": "lxml"
}
# check config
for opt in [
"notification_email_smtp_host", "notification_email_smtp_host",
"notification_email_smtp_port", "notification_email_smtp_port",
"notification_email_envelope_from", "notification_email_envelope_from",
"notification_email_from", "notification_email_from",
"notification_email_subject", "notification_email_subject",
"notification_email_template", "notification_email_template",
"notification_email_embedded_imgs"]: "notification_email_embedded_imgs"] + list(defaults.keys()):
if option not in self.config.keys() and option in self.global_config.keys(): if opt in cfg:
self.config[option] = self.global_config[option] continue
if option not in self.config.keys(): if opt in global_cfg:
cfg[opt] = global_cfg[opt]
elif opt in defaults:
cfg[opt] = defaults[opt]
else:
raise RuntimeError( raise RuntimeError(
"mandatory option '{}' not present in config section '{}' or 'global'".format( "mandatory option '{}' not present in config section '{}' or 'global'".format(
option, self.quarantine_name)) opt, self.name))
# check if optional config options are present in config self.smtp_host = cfg["notification_email_smtp_host"]
defaults = { self.smtp_port = cfg["notification_email_smtp_port"]
"notification_email_replacement_img": "", self.mailfrom = cfg["notification_email_envelope_from"]
"notification_email_strip_images": "false", self.from_header = cfg["notification_email_from"]
"notification_email_parser_lib": "lxml" self.subject = cfg["notification_email_subject"]
}
for option in defaults.keys():
if option not in config.keys() and \
option in global_config.keys():
config[option] = global_config[option]
if option not in config.keys():
config[option] = defaults[option]
self.smtp_host = self.config["notification_email_smtp_host"]
self.smtp_port = self.config["notification_email_smtp_port"]
self.mailfrom = self.config["notification_email_envelope_from"]
self.from_header = self.config["notification_email_from"]
self.subject = self.config["notification_email_subject"]
testvars = defaultdict(str, test="TEST") testvars = defaultdict(str, test="TEST")
@@ -167,14 +164,14 @@ class EMailNotification(BaseNotification):
# read and parse email notification template # read and parse email notification template
try: try:
self.template = open( self.template = open(
self.config["notification_email_template"], "r").read() cfg["notification_email_template"], "r").read()
self.template.format_map(testvars) self.template.format_map(testvars)
except IOError as e: except IOError as e:
raise RuntimeError("error reading template: {}".format(e)) raise RuntimeError("error reading template: {}".format(e))
except ValueError as e: except ValueError as e:
raise RuntimeError("error parsing template: {}".format(e)) raise RuntimeError("error parsing template: {}".format(e))
strip_images = self.config["notification_email_strip_images"].strip().upper() strip_images = cfg["notification_email_strip_images"].strip().upper()
if strip_images in ["TRUE", "ON", "YES"]: if strip_images in ["TRUE", "ON", "YES"]:
self.strip_images = True self.strip_images = True
elif strip_images in ["FALSE", "OFF", "NO"]: elif strip_images in ["FALSE", "OFF", "NO"]:
@@ -182,12 +179,12 @@ class EMailNotification(BaseNotification):
else: else:
raise RuntimeError("error parsing notification_email_strip_images: unknown value") raise RuntimeError("error parsing notification_email_strip_images: unknown value")
self.parser_lib = self.config["notification_email_parser_lib"].strip() self.parser_lib = cfg["notification_email_parser_lib"].strip()
if self.parser_lib not in ["lxml", "html.parser"]: if self.parser_lib not in ["lxml", "html.parser"]:
raise RuntimeError("error parsing notification_email_parser_lib: unknown value") raise RuntimeError("error parsing notification_email_parser_lib: unknown value")
# read email replacement image if specified # read email replacement image if specified
replacement_img = self.config["notification_email_replacement_img"].strip() replacement_img = cfg["notification_email_replacement_img"].strip()
if not self.strip_images and replacement_img: if not self.strip_images and replacement_img:
try: try:
self.replacement_img = MIMEImage( self.replacement_img = MIMEImage(
@@ -203,7 +200,7 @@ class EMailNotification(BaseNotification):
# read images to embed if specified # read images to embed if specified
embedded_img_paths = [ embedded_img_paths = [
p.strip() for p in self.config["notification_email_embedded_imgs"].split(",") if p] p.strip() for p in cfg["notification_email_embedded_imgs"].split(",") if p]
self.embedded_imgs = [] self.embedded_imgs = []
for img_path in embedded_img_paths: for img_path in embedded_img_paths:
# read image # read image
@@ -215,35 +212,32 @@ class EMailNotification(BaseNotification):
img.add_header("Content-ID", "<{}>".format(basename(img_path))) img.add_header("Content-ID", "<{}>".format(basename(img_path)))
self.embedded_imgs.append(img) self.embedded_imgs.append(img)
def get_decoded_email_body(self, queueid, msg, preferred=_html_text): def get_email_body_soup(self, queueid, msg):
"Find and decode email body." "Extract and decode email body and return it as BeautifulSoup object."
# try to find the body part # try to find the body part
self.logger.debug("{}: trying to find email body".format(queueid)) self.logger.debug("{}: trying to find email body".format(queueid))
body = None try:
for part in msg.walk(): body = msg.get_body(preferencelist=("html", "plain"))
content_type = part.get_content_type() except Exception as e:
if content_type in [EMailNotification._plain_text, self.logger.error("{}: an error occured in email.message.EmailMessage.get_body: {}".format(
EMailNotification._html_text]: queueid, e))
body = part body = None
if content_type == preferred:
break
if body is not None:
# get the character set, fallback to utf-8 if not defined in header
charset = body.get_content_charset()
if charset is None:
charset = "utf-8"
# decode content
content = body.get_payload(decode=True).decode(
encoding=charset, errors="replace")
if body:
charset = body.get_content_charset() or "utf-8"
content = body.get_payload(decode=True)
try:
content = content.decode(encoding=charset, errors="replace")
except LookupError:
self.logger.info("{}: unknown encoding '{}', falling back to UTF-8".format(
queueid, charset))
content = content.decode("utf-8", errors="replace")
content_type = body.get_content_type() content_type = body.get_content_type()
if content_type == EMailNotification._plain_text: if content_type == "text/plain":
# convert text/plain to text/html # convert text/plain to text/html
self.logger.debug( self.logger.debug(
"{}: content type is {}, converting to {}".format( "{}: content type is {}, converting to text/html".format(
queueid, content_type, EMailNotification._html_text)) queueid, content_type))
content = re.sub(r"^(.*)$", r"\1<br/>", content = re.sub(r"^(.*)$", r"\1<br/>",
escape(content), flags=re.MULTILINE) escape(content), flags=re.MULTILINE)
else: else:
@@ -255,7 +249,16 @@ class EMailNotification(BaseNotification):
"{}: unable to find email body".format(queueid)) "{}: unable to find email body".format(queueid))
content = "ERROR: unable to find email body" content = "ERROR: unable to find email body"
return content # create BeautifulSoup object
self.logger.debug(
"{}: trying to create BeatufilSoup object with parser lib {}, "
"text length is {} bytes".format(
queueid, self.parser_lib, len(content)))
soup = BeautifulSoup(content, self.parser_lib)
self.logger.debug(
"{}: sucessfully created BeautifulSoup object".format(queueid))
return soup
def sanitize(self, queueid, soup): def sanitize(self, queueid, soup):
"Sanitize mail html text." "Sanitize mail html text."
@@ -292,14 +295,14 @@ class EMailNotification(BaseNotification):
del(element.attrs[attribute]) del(element.attrs[attribute])
return soup return soup
def notify(self, queueid, quarantine_id, mailfrom, recipients, headers, fp, def notify(self, queueid, storage_id, mailfrom, recipients, headers, fp,
subgroups=None, named_subgroups=None, synchronous=False): subgroups=None, named_subgroups=None, synchronous=False):
"Notify recipients via email." "Notify recipients via email."
super( super(
EMailNotification, EMailNotification,
self).notify( self).notify(
queueid, queueid,
quarantine_id, storage_id,
mailfrom, mailfrom,
recipients, recipients,
headers, headers,
@@ -309,17 +312,8 @@ class EMailNotification(BaseNotification):
synchronous) synchronous)
# extract body from email # extract body from email
content = self.get_decoded_email_body( soup = self.get_email_body_soup(
queueid, email.message_from_binary_file(fp)) queueid, email.message_from_binary_file(fp, policy=policy.default))
# create BeautifulSoup object
self.logger.debug(
"{}: trying to create BeatufilSoup object with parser lib {}, "
"text length is {} bytes".format(
queueid, self.parser_lib, len(content)))
soup = BeautifulSoup(content, self.parser_lib)
self.logger.debug(
"{}: sucessfully created BeautifulSoup object".format(queueid))
# replace picture sources # replace picture sources
image_replaced = False image_replaced = False
@@ -353,15 +347,27 @@ class EMailNotification(BaseNotification):
"{}: generating notification email for '{}'".format( "{}: generating notification email for '{}'".format(
queueid, recipient)) queueid, recipient))
self.logger.debug("{}: parsing email template".format(queueid)) self.logger.debug("{}: parsing email template".format(queueid))
# decode some headers
decoded_headers = {}
for header in ["from", "to", "subject"]:
if header in headers:
decoded_headers[header] = str(
make_header(decode_header(headers[header])))
else:
headers[header] = ""
decoded_headers[header] = ""
# generate dict containing all template variables # generate dict containing all template variables
variables = defaultdict(str, variables = defaultdict(str,
EMAIL_HTML_TEXT=sanitized_text, EMAIL_HTML_TEXT=sanitized_text,
EMAIL_FROM=escape(headers["from"]), EMAIL_FROM=escape(decoded_headers["from"]),
EMAIL_ENVELOPE_FROM=escape(mailfrom), EMAIL_ENVELOPE_FROM=escape(mailfrom),
EMAIL_TO=escape(recipient), EMAIL_ENVELOPE_FROM_URL=escape(quote(mailfrom)),
EMAIL_SUBJECT=escape(headers["subject"]), EMAIL_TO=escape(decoded_headers["to"]),
EMAIL_QUARANTINE_ID=quarantine_id) EMAIL_ENVELOPE_TO=escape(recipient),
EMAIL_ENVELOPE_TO_URL=escape(quote(recipient)),
EMAIL_SUBJECT=escape(decoded_headers["subject"]),
EMAIL_QUARANTINE_ID=storage_id)
if subgroups: if subgroups:
number = 0 number = 0

View File

@@ -1,309 +0,0 @@
# PyQuarantine-Milter is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyQuarantine-Milter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyQuarantineMilter. If not, see <http://www.gnu.org/licenses/>.
#
import json
import logging
import os
from calendar import timegm
from datetime import datetime
from glob import glob
from shutil import copyfileobj
from time import gmtime
from pyquarantine import mailer
class BaseQuarantine(object):
"Quarantine base class"
def __init__(self, global_config, config, configtest=False):
self.name = config["name"]
self.global_config = global_config
self.config = config
self.logger = logging.getLogger(__name__)
def add(self, queueid, mailfrom, recipients, headers,
fp, subgroups=None, named_subgroups=None):
"Add email to quarantine."
fp.seek(0)
return ""
def find(self, mailfrom=None, recipients=None, older_than=None):
"Find emails in quarantine."
return
def get_metadata(self, quarantine_id):
"Return metadata of quarantined email."
return
def delete(self, quarantine_id, recipient=None):
"Delete email from quarantine."
return
def notify(self, quarantine_id, recipient=None):
"Notify recipient about email in quarantine."
if not self.config["notification_obj"]:
raise RuntimeError(
"notification type is set to None, unable to send notifications")
return
def release(self, quarantine_id, recipient=None):
"Release email from quarantine."
return
class FileQuarantine(BaseQuarantine):
"Quarantine class to store mails on filesystem."
def __init__(self, global_config, config, configtest=False):
super(FileQuarantine, self).__init__(global_config, config, configtest)
# check if mandatory options are present in config
for option in ["quarantine_directory"]:
if option not in self.config.keys() and option in self.global_config.keys():
self.config[option] = self.global_config[option]
if option not in self.config.keys():
raise RuntimeError(
"mandatory option '{}' not present in config section '{}' or 'global'".format(
option, self.name))
self.directory = self.config["quarantine_directory"]
# check if quarantine directory exists and is writable
if not os.path.isdir(self.directory) or not os.access(
self.directory, os.W_OK):
raise RuntimeError(
"file quarantine directory '{}' does not exist or is not writable".format(
self.directory))
self._metadata_suffix = ".metadata"
def _save_datafile(self, quarantine_id, fp):
datafile = os.path.join(self.directory, quarantine_id)
try:
with open(datafile, "wb") as f:
copyfileobj(fp, f)
except IOError as e:
raise RuntimeError("unable save data file: {}".format(e))
def _save_metafile(self, quarantine_id, metadata):
metafile = os.path.join(
self.directory, "{}{}".format(
quarantine_id, self._metadata_suffix))
try:
with open(metafile, "w") as f:
json.dump(metadata, f, indent=2)
except IOError as e:
raise RuntimeError("unable to save metadata file: {}".format(e))
def _remove(self, quarantine_id):
datafile = os.path.join(self.directory, quarantine_id)
metafile = "{}{}".format(datafile, self._metadata_suffix)
try:
os.remove(metafile)
except IOError as e:
raise RuntimeError("unable to remove metadata file: {}".format(e))
try:
os.remove(datafile)
except IOError as e:
raise RuntimeError("unable to remove data file: {}".format(e))
def add(self, queueid, mailfrom, recipients, headers,
fp, subgroups=None, named_subgroups=None):
"Add email to file quarantine and return quarantine-id."
super(
FileQuarantine,
self).add(
queueid,
mailfrom,
recipients,
headers,
fp,
subgroups,
named_subgroups)
quarantine_id = "{}_{}".format(
datetime.now().strftime("%Y%m%d%H%M%S"), queueid)
# save mail
self._save_datafile(quarantine_id, fp)
# save metadata
metadata = {
"mailfrom": mailfrom,
"recipients": recipients,
"headers": headers,
"date": timegm(gmtime()),
"queue_id": queueid,
"subgroups": subgroups,
"named_subgroups": named_subgroups
}
try:
self._save_metafile(quarantine_id, metadata)
except RuntimeError as e:
datafile = os.path.join(self.directory, quarantine_id)
os.remove(datafile)
raise e
return quarantine_id
def get_metadata(self, quarantine_id):
"Return metadata of quarantined email."
super(FileQuarantine, self).get_metadata(quarantine_id)
metafile = os.path.join(
self.directory, "{}{}".format(
quarantine_id, self._metadata_suffix))
if not os.path.isfile(metafile):
raise RuntimeError(
"invalid quarantine id '{}'".format(quarantine_id))
try:
with open(metafile, "r") as f:
metadata = json.load(f)
except IOError as e:
raise RuntimeError("unable to read metadata file: {}".format(e))
except json.JSONDecodeError as e:
raise RuntimeError(
"invalid meta file '{}': {}".format(
metafile, e))
return metadata
def find(self, mailfrom=None, recipients=None, older_than=None):
"Find emails in quarantine."
super(FileQuarantine, self).find(mailfrom, recipients, older_than)
if isinstance(mailfrom, str):
mailfrom = [mailfrom]
if isinstance(recipients, str):
recipients = [recipients]
emails = {}
metafiles = glob(os.path.join(
self.directory, "*{}".format(self._metadata_suffix)))
for metafile in metafiles:
if not os.path.isfile(metafile):
continue
quarantine_id = os.path.basename(
metafile[:-len(self._metadata_suffix)])
metadata = self.get_metadata(quarantine_id)
if older_than is not None:
if timegm(gmtime()) - metadata["date"] < (older_than * 86400):
continue
if mailfrom is not None:
if metadata["mailfrom"] not in mailfrom:
continue
if recipients is not None:
if len(recipients) == 1 and \
recipients[0] not in metadata["recipients"]:
continue
elif len(set(recipients + metadata["recipients"])) == len(recipients + metadata["recipients"]):
continue
emails[quarantine_id] = metadata
return emails
def delete(self, quarantine_id, recipient=None):
"Delete email in quarantine."
super(FileQuarantine, self).delete(quarantine_id, recipient)
try:
metadata = self.get_metadata(quarantine_id)
except RuntimeError as e:
raise RuntimeError("unable to delete email: {}".format(e))
if recipient is None:
self._remove(quarantine_id)
else:
if recipient not in metadata["recipients"]:
raise RuntimeError("invalid recipient '{}'".format(recipient))
metadata["recipients"].remove(recipient)
if not metadata["recipients"]:
self._remove(quarantine_id)
else:
self._save_metafile(quarantine_id, metadata)
def notify(self, quarantine_id, recipient=None):
"Notify recipient about email in quarantine."
super(FileQuarantine, self).notify(quarantine_id, recipient)
try:
metadata = self.get_metadata(quarantine_id)
except RuntimeError as e:
raise RuntimeError("unable to release email: {}".format(e))
if recipient is not None:
if recipient not in metadata["recipients"]:
raise RuntimeError("invalid recipient '{}'".format(recipient))
recipients = [recipient]
else:
recipients = metadata["recipients"]
datafile = os.path.join(self.directory, quarantine_id)
try:
with open(datafile, "rb") as fp:
self.config["notification_obj"].notify(
metadata["queue_id"], quarantine_id, metadata["mailfrom"],
recipients, metadata["headers"], fp,
metadata["subgroups"], metadata["named_subgroups"],
synchronous=True)
except IOError as e:
raise RuntimeError
def release(self, quarantine_id, recipient=None):
"Release email from quarantine."
super(FileQuarantine, self).release(quarantine_id, recipient)
try:
metadata = self.get_metadata(quarantine_id)
except RuntimeError as e:
raise RuntimeError("unable to release email: {}".format(e))
if recipient is not None:
if recipient not in metadata["recipients"]:
raise RuntimeError("invalid recipient '{}'".format(recipient))
recipients = [recipient]
else:
recipients = metadata["recipients"]
datafile = os.path.join(self.directory, quarantine_id)
try:
with open(datafile, "rb") as f:
mail = f.read()
except IOError as e:
raise RuntimeError("unable to read data file: {}".format(e))
for recipient in recipients:
try:
mailer.smtp_send(
self.config["smtp_host"],
self.config["smtp_port"],
metadata["mailfrom"],
recipient,
mail)
except Exception as e:
raise RuntimeError(
"error while sending email to '{}': {}".format(
recipient, e))
self.delete(quarantine_id, recipient)
# list of quarantine types and their related quarantine classes
TYPES = {"file": FileQuarantine}

View File

@@ -35,11 +35,11 @@ def main():
"-c", "--config", "-c", "--config",
help="List of config files to read.", help="List of config files to read.",
nargs="+", nargs="+",
default=pyquarantine.QuarantineMilter.get_configfiles()) default=pyquarantine.QuarantineMilter.get_cfg_files())
parser.add_argument( parser.add_argument(
"-s", "--socket", "-s", "--socket",
help="Socket used to communicate with the MTA.", help="Socket used to communicate with the MTA.",
required=True) default="inet:8899@127.0.0.1")
parser.add_argument( parser.add_argument(
"-d", "--debug", "-d", "--debug",
help="Log debugging messages.", help="Log debugging messages.",
@@ -65,7 +65,7 @@ def main():
syslog_name = "{}: [%(name)s] %(levelname)s".format(syslog_name) syslog_name = "{}: [%(name)s] %(levelname)s".format(syslog_name)
# set config files for milter class # set config files for milter class
pyquarantine.QuarantineMilter.set_configfiles(args.config) pyquarantine.QuarantineMilter.set_cfg_files(args.config)
root_logger = logging.getLogger() root_logger = logging.getLogger()
root_logger.setLevel(loglevel) root_logger.setLevel(loglevel)
@@ -78,7 +78,7 @@ def main():
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
if args.test: if args.test:
try: try:
pyquarantine.generate_milter_config(args.test) pyquarantine.setup_milter(test=args.test)
print("Configuration ok") print("Configuration ok")
except RuntimeError as e: except RuntimeError as e:
logger.error(e) logger.error(e)
@@ -101,18 +101,14 @@ def main():
logger.info("PyQuarantine-Milter starting") logger.info("PyQuarantine-Milter starting")
try: try:
# generate milter config # generate milter config
global_config, config = pyquarantine.generate_milter_config() pyquarantine.setup_milter()
except RuntimeError as e: except RuntimeError as e:
logger.error(e) logger.error(e)
sys.exit(255) sys.exit(255)
pyquarantine.QuarantineMilter.global_config = global_config
pyquarantine.QuarantineMilter.config = config
# register to have the Milter factory create instances of your class: # register to have the Milter factory create instances of your class:
Milter.factory = pyquarantine.QuarantineMilter Milter.factory = pyquarantine.QuarantineMilter
Milter.set_exception_policy(Milter.TEMPFAIL) Milter.set_exception_policy(Milter.TEMPFAIL)
# Milter.set_flags(0) # tell sendmail which features we use
# run milter # run milter
rc = 0 rc = 0
@@ -122,6 +118,7 @@ def main():
except Milter.milter.error as e: except Milter.milter.error as e:
logger.error(e) logger.error(e)
rc = 255 rc = 255
pyquarantine.mailer.queue.put(None) pyquarantine.mailer.queue.put(None)
logger.info("PyQuarantine-Milter terminated") logger.info("PyQuarantine-Milter terminated")
sys.exit(rc) sys.exit(rc)

254
pyquarantine/storages.py Normal file
View File

@@ -0,0 +1,254 @@
# PyQuarantine-Milter is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyQuarantine-Milter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyQuarantineMilter. If not, see <http://www.gnu.org/licenses/>.
#
import json
import logging
import os
from calendar import timegm
from datetime import datetime
from glob import glob
from shutil import copyfileobj
from time import gmtime
class BaseMailStorage(object):
"Mail storage base class"
storage_type = "base"
def __init__(self, name, global_cfg, cfg, test=False):
self.name = name
self.logger = logging.getLogger(__name__)
def add(self, queueid, mailfrom, recipients, headers,
fp, subgroups=None, named_subgroups=None):
"Add email to storage."
fp.seek(0)
return ""
def find(self, mailfrom=None, recipients=None, older_than=None):
"Find emails in storage."
return
def get_metadata(self, storage_id):
"Return metadata of email in storage."
return
def delete(self, storage_id, recipients=None):
"Delete email from storage."
return
def get_mail(self, storage_id):
"Return a file pointer to the email and metadata."
return
class FileMailStorage(BaseMailStorage):
"Storage class to store mails on filesystem."
storage_type = "file"
def __init__(self, name, global_cfg, cfg, test=False):
super(FileMailStorage, self).__init__(name, global_cfg, cfg, test)
defaults = {}
# check config
for opt in ["storage_directory"] + list(defaults.keys()):
if opt in cfg:
continue
if opt in global_cfg:
cfg[opt] = global_cfg[opt]
elif opt in defaults:
cfg[opt] = defaults[opt]
else:
raise RuntimeError(
"mandatory option '{}' not present in config section '{}' or 'global'".format(
opt, self.name))
self.directory = cfg["storage_directory"]
# check if quarantine directory exists and is writable
if not os.path.isdir(self.directory) or not os.access(
self.directory, os.W_OK):
raise RuntimeError(
"file quarantine directory '{}' does not exist or is not writable".format(
self.directory))
self._metadata_suffix = ".metadata"
def _save_datafile(self, storage_id, fp):
datafile = os.path.join(self.directory, storage_id)
try:
with open(datafile, "wb") as f:
copyfileobj(fp, f)
except IOError as e:
raise RuntimeError("unable save data file: {}".format(e))
def _save_metafile(self, storage_id, metadata):
metafile = os.path.join(
self.directory, "{}{}".format(
storage_id, self._metadata_suffix))
try:
with open(metafile, "w") as f:
json.dump(metadata, f, indent=2)
except IOError as e:
raise RuntimeError("unable to save metadata file: {}".format(e))
def _remove(self, storage_id):
datafile = os.path.join(self.directory, storage_id)
metafile = "{}{}".format(datafile, self._metadata_suffix)
try:
os.remove(metafile)
except IOError as e:
raise RuntimeError("unable to remove metadata file: {}".format(e))
try:
os.remove(datafile)
except IOError as e:
raise RuntimeError("unable to remove data file: {}".format(e))
def add(self, queueid, mailfrom, recipients, headers,
fp, subgroups=None, named_subgroups=None):
"Add email to file storage and return storage id."
super(
FileMailStorage,
self).add(
queueid,
mailfrom,
recipients,
headers,
fp,
subgroups,
named_subgroups)
storage_id = "{}_{}".format(
datetime.now().strftime("%Y%m%d%H%M%S"), queueid)
# save mail
self._save_datafile(storage_id, fp)
# save metadata
metadata = {
"mailfrom": mailfrom,
"recipients": recipients,
"headers": headers,
"date": timegm(gmtime()),
"queue_id": queueid,
"subgroups": subgroups,
"named_subgroups": named_subgroups
}
try:
self._save_metafile(storage_id, metadata)
except RuntimeError as e:
datafile = os.path.join(self.directory, storage_id)
os.remove(datafile)
raise e
return storage_id
def get_metadata(self, storage_id):
"Return metadata of email in storage."
super(FileMailStorage, self).get_metadata(storage_id)
metafile = os.path.join(
self.directory, "{}{}".format(
storage_id, self._metadata_suffix))
if not os.path.isfile(metafile):
raise RuntimeError(
"invalid storage id '{}'".format(storage_id))
try:
with open(metafile, "r") as f:
metadata = json.load(f)
except IOError as e:
raise RuntimeError("unable to read metadata file: {}".format(e))
except json.JSONDecodeError as e:
raise RuntimeError(
"invalid meta file '{}': {}".format(
metafile, e))
return metadata
def find(self, mailfrom=None, recipients=None, older_than=None):
"Find emails in storage."
super(FileMailStorage, self).find(mailfrom, recipients, older_than)
if isinstance(mailfrom, str):
mailfrom = [mailfrom]
if isinstance(recipients, str):
recipients = [recipients]
emails = {}
metafiles = glob(os.path.join(
self.directory, "*{}".format(self._metadata_suffix)))
for metafile in metafiles:
if not os.path.isfile(metafile):
continue
storage_id = os.path.basename(
metafile[:-len(self._metadata_suffix)])
metadata = self.get_metadata(storage_id)
if older_than is not None:
if timegm(gmtime()) - metadata["date"] < (older_than * 86400):
continue
if mailfrom is not None:
if metadata["mailfrom"] not in mailfrom:
continue
if recipients is not None:
if len(recipients) == 1 and \
recipients[0] not in metadata["recipients"]:
continue
elif len(set(recipients + metadata["recipients"])) == len(recipients + metadata["recipients"]):
continue
emails[storage_id] = metadata
return emails
def delete(self, storage_id, recipients=None):
"Delete email from storage."
super(FileMailStorage, self).delete(storage_id, recipients)
try:
metadata = self.get_metadata(storage_id)
except RuntimeError as e:
raise RuntimeError("unable to delete email: {}".format(e))
if not recipients:
self._remove(storage_id)
else:
if type(recipients) == str:
recipients = [recipients]
for recipient in recipients:
if recipient not in metadata["recipients"]:
raise RuntimeError("invalid recipient '{}'".format(recipient))
metadata["recipients"].remove(recipient)
if not metadata["recipients"]:
self._remove(storage_id)
else:
self._save_metafile(storage_id, metadata)
def get_mail(self, storage_id):
super(FileMailStorage, self).get_mail(storage_id)
metadata = self.get_metadata(storage_id)
datafile = os.path.join(self.directory, storage_id)
try:
fp = open(datafile, "rb")
except IOError as e:
raise RuntimeError("unable to open email data file: {}".format(e))
return (fp, metadata)
# list of storage types and their related storage classes
TYPES = {"file": FileMailStorage}

View File

@@ -1 +1 @@
__version__ = "0.0.5" __version__ = "1.0.0"

View File

@@ -24,14 +24,14 @@ from playhouse.db_url import connect
class WhitelistBase(object): class WhitelistBase(object):
"Whitelist base class" "Whitelist base class"
def __init__(self, global_config, config, configtest=False): whitelist_type = "base"
self.global_config = global_config
self.config = config def __init__(self, name, global_cfg, cfg, test=False):
self.configtest = configtest self.name = name
self.name = config["name"] self.test = test
self.logger = logging.getLogger(__name__) self.logger = logging.getLogger(__name__)
self.valid_entry_regex = re.compile( self.valid_entry_regex = re.compile(
r"^[a-zA-Z0-9_.+-]*?(@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)?$") r"^[a-zA-Z0-9_.=+-]*?(@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)?$")
def check(self, mailfrom, recipient): def check(self, mailfrom, recipient):
"Check if mailfrom/recipient combination is whitelisted." "Check if mailfrom/recipient combination is whitelisted."
@@ -73,28 +73,35 @@ class Meta(object):
class DatabaseWhitelist(WhitelistBase): class DatabaseWhitelist(WhitelistBase):
"Whitelist class to store whitelist in a database" "Whitelist class to store whitelist in a database"
whitelist_type = "db"
_db_connections = {} _db_connections = {}
_db_tables = {} _db_tables = {}
def __init__(self, global_config, config, configtest=False): def __init__(self, name, global_cfg, cfg, test=False):
super( super(
DatabaseWhitelist, DatabaseWhitelist,
self).__init__( self).__init__(
global_config, global_cfg,
config, cfg,
configtest) test)
# check if mandatory options are present in config defaults = {}
for option in ["whitelist_db_connection", "whitelist_db_table"]:
if option not in self.config.keys() and option in self.global_config.keys(): # check config
self.config[option] = self.global_config[option] for opt in ["whitelist_db_connection", "whitelist_db_table"] + list(defaults.keys()):
if option not in self.config.keys(): if opt in cfg:
continue
if opt in global_cfg:
cfg[opt] = global_cfg[opt]
elif opt in defaults:
cfg[opt] = defaults[opt]
else:
raise RuntimeError( raise RuntimeError(
"mandatory option '{}' not present in config section '{}' or 'global'".format( "mandatory option '{}' not present in config section '{}' or 'global'".format(
option, self.name)) opt, self.name))
tablename = self.config["whitelist_db_table"] tablename = cfg["whitelist_db_table"]
connection_string = self.config["whitelist_db_connection"] connection_string = cfg["whitelist_db_connection"]
if connection_string in DatabaseWhitelist._db_connections.keys(): if connection_string in DatabaseWhitelist._db_connections.keys():
db = DatabaseWhitelist._db_connections[connection_string] db = DatabaseWhitelist._db_connections[connection_string]
@@ -127,7 +134,7 @@ class DatabaseWhitelist(WhitelistBase):
if tablename not in DatabaseWhitelist._db_tables[connection_string]: if tablename not in DatabaseWhitelist._db_tables[connection_string]:
DatabaseWhitelist._db_tables[connection_string].append(tablename) DatabaseWhitelist._db_tables[connection_string].append(tablename)
if not self.configtest: if not self.test:
try: try:
db.create_tables([self.model]) db.create_tables([self.model])
except Exception as e: except Exception as e:

View File

@@ -22,7 +22,7 @@ setup(name = "pyquarantine",
# 3 - Alpha # 3 - Alpha
# 4 - Beta # 4 - Beta
# 5 - Production/Stable # 5 - Production/Stable
"Development Status :: 3 - Alpha", "Development Status :: 4 - Beta",
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
"Operating System :: OS Independent", "Operating System :: OS Independent",
"Programming Language :: Python", "Programming Language :: Python",
@@ -36,5 +36,5 @@ setup(name = "pyquarantine",
] ]
}, },
install_requires = ["pymilter", "netaddr", "beautifulsoup4[lxml]", "peewee"], install_requires = ["pymilter", "netaddr", "beautifulsoup4[lxml]", "peewee"],
python_requires = ">=3" python_requires = ">=3.6"
) )