Implement Advanced Content Filter #97

Merged
pfm merged 36 commits from daemon into main 2022-10-19 20:48:41 +02:00
3 changed files with 49 additions and 22 deletions
Showing only changes of commit 7849c55d9f - Show all commits

View File

@ -1,22 +1,21 @@
#!/usr/bin/python #!/usr/bin/python
# #
# gpg-mailgate # gpg-mailgate
# #
# This file is part of the gpg-mailgate source code. # This file is part of the gpg-mailgate source code.
# #
# gpg-mailgate is free software: you can redistribute it and/or modify # gpg-mailgate is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or # the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# gpg-mailgate source code is distributed in the hope that it will be useful, # gpg-mailgate source code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with gpg-mailgate source code. If not, see <http://www.gnu.org/licenses/>. # along with gpg-mailgate source code. If not, see <http://www.gnu.org/licenses/>.
# #
import email import email
@ -38,12 +37,12 @@ LOG = logging.getLogger(__name__)
missing_params = conf.validate_config() missing_params = conf.validate_config()
if missing_params: if missing_params:
LOG.error(f"Aborting delivery! Following mandatory config parameters are missing: {missing_params!r}") LOG.error(f"Aborting delivery! Following mandatory config parameters are missing: {missing_params!r}")
sys.exit(lacre.EX_CONFIG) sys.exit(lacre.EX_CONFIG)
# Read e-mail from stdin # Read e-mail from stdin
raw = sys.stdin.read() raw = sys.stdin.read()
raw_message = email.message_from_string( raw ) raw_message = email.message_from_string(raw)
from_addr = raw_message['From'] from_addr = raw_message['From']
to_addrs = sys.argv[1:] to_addrs = sys.argv[1:]

View File

@ -1,15 +1,25 @@
"""Lacre Daemon, the Advanced Mail Filter message dispatcher.""" """Lacre Daemon, the Advanced Mail Filter message dispatcher."""
import logging
import lacre
import lacre.config as conf
from aiosmtpd.controller import Controller from aiosmtpd.controller import Controller
import lacre.config as conf # Mail status constants.
# import lacre.mailgate as gate #
# These are the only values that our mail handler is allowed to return.
RESULT_OK = '250 OK' RESULT_OK = '250 OK'
RESULT_ERROR = '500 Could not process your message' RESULT_ERROR = '500 Could not process your message'
RESULT_NOT_IMPLEMENTED = '500 Not implemented yet' RESULT_NOT_IMPLEMENTED = '500 Not implemented yet'
# Load configuration and init logging, in this order. Only then can we load
# the last Lacre module, i.e. lacre.mailgate.
conf.load_config()
lacre.init_logging(conf.get_item("logging", "config"))
LOG = logging.getLogger(__name__)
import lacre.mailgate as gate
class MailEncryptionProxy: class MailEncryptionProxy:
"""A mail handler dispatching to appropriate mail operation.""" """A mail handler dispatching to appropriate mail operation."""
@ -17,13 +27,21 @@ class MailEncryptionProxy:
async def handle_DATA(self, server, session, envelope): async def handle_DATA(self, server, session, envelope):
"""Accept a message and either encrypt it or forward as-is.""" """Accept a message and either encrypt it or forward as-is."""
# for now, just return an error because we're not ready to handle mail # for now, just return an error because we're not ready to handle mail
for r, s in gate.delivery_plan(envelope.rcpt_tos):
print(r)
return RESULT_NOT_IMPLEMENTED return RESULT_NOT_IMPLEMENTED
if __name__ == '__main__': def _init_controller():
proxy = MailEncryptionProxy() proxy = MailEncryptionProxy()
host, port = conf.relay_params() host, port = conf.relay_params()
controller = Controller(proxy, hostname=host, port=port) return Controller(proxy, hostname=host, port=port)
def _main():
controller = _init_controller()
# starts the controller in a new thread # starts the controller in a new thread
controller.start() controller.start()
@ -32,3 +50,7 @@ if __name__ == '__main__':
# and certificate cache # and certificate cache
controller.stop() controller.stop()
if __name__ == '__main__':
_main()

View File

@ -402,6 +402,12 @@ def _is_encrypted(raw_message):
return text.is_pgp_inline(first_payload) return text.is_pgp_inline(first_payload)
def delivery_plan(recipients):
"""Generate a sequence of pairs: a recipient and their delivery strategy."""
for recipient in recipients:
yield recipient, None
def deliver_message(raw_message, from_address, to_addrs): def deliver_message(raw_message, from_address, to_addrs):
"""Send RAW_MESSAGE to all TO_ADDRS using the best encryption method available.""" """Send RAW_MESSAGE to all TO_ADDRS using the best encryption method available."""
global from_addr global from_addr