From 2d83267b558c3ab9b95f600c9f9e246a6832ae4d Mon Sep 17 00:00:00 2001 From: balki <3070606-balki@users.noreply.gitlab.com> Date: Tue, 25 Dec 2018 15:37:01 -0500 Subject: [PATCH] add pure tls smptp --- mail4one/server.py | 14 +++++++++++--- mail4one/smtp.py | 25 +++++++++++++++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/mail4one/server.py b/mail4one/server.py index c8d0e24..522c7b1 100644 --- a/mail4one/server.py +++ b/mail4one/server.py @@ -10,7 +10,7 @@ import sys from argparse import ArgumentParser from pathlib import Path -from .smtp import create_smtp_server +from .smtp import create_smtp_server_starttls, create_smtp_server_tls from .pop3 import create_pop_server @@ -33,6 +33,8 @@ def parse_args(): # Hardcoded args args.host = '0.0.0.0' args.smtp_port = 25 + args.smtp_port_tls = 465 + args.smtp_port_submission = 587 args.pop_port = 995 args.smtputf8 = True args.debug = True @@ -66,9 +68,15 @@ def drop_privileges(): async def a_main(args, tls_context): pop_server = await create_pop_server( args.mail_dir_path, port=args.pop_port, host=args.host, context=tls_context, password_hash=args.password_hash) - smtp_server = await create_smtp_server(args.mail_dir_path, port=args.smtp_port, host=args.host, context=tls_context) + smtp_server_starttls = await create_smtp_server_starttls( + args.mail_dir_path, port=args.smtp_port, host=args.host, context=tls_context) + smtp_server_tls = await create_smtp_server_tls( + args.mail_dir_path, port=args.smtp_port_tls, host=args.host, context=tls_context) drop_privileges() - await asyncio.gather(pop_server.serve_forever(), smtp_server.serve_forever()) + await asyncio.gather( + pop_server.serve_forever(), + smtp_server_starttls.serve_forever(), + smtp_server_tls.serve_forever()) def main(): diff --git a/mail4one/smtp.py b/mail4one/smtp.py index f69af08..c2acb8b 100644 --- a/mail4one/smtp.py +++ b/mail4one/smtp.py @@ -31,7 +31,7 @@ class MailboxCRLF(Mailbox): self.mailbox = MaildirCRLF(mail_dir) -def protocol_factory(dirpath: Path, context: ssl.SSLContext = None): +def protocol_factory_starttls(dirpath: Path, context: ssl.SSLContext = None): logging.info("Got smtp client cb") try: handler = MailboxCRLF(dirpath) @@ -46,14 +46,31 @@ def protocol_factory(dirpath: Path, context: ssl.SSLContext = None): return smtp -async def create_smtp_server(dirpath: Path, port: int, host="", context: ssl.SSLContext = None): +def protocol_factory(dirpath: Path): + logging.info("Got smtp client cb") + try: + handler = MailboxCRLF(dirpath) + smtp = SMTP(handler=handler, data_size_limit=DATA_SIZE_DEFAULT, enable_SMTPUTF8=True) + except Exception as e: + logging.error("Something went wrong", e) + raise + return smtp + + +async def create_smtp_server_starttls(dirpath: Path, port: int, host="", context: ssl.SSLContext = None): loop = asyncio.get_event_loop() - return await loop.create_server(partial(protocol_factory, dirpath, context), + return await loop.create_server(partial(protocol_factory_starttls, dirpath, context), host=host, port=port, start_serving=False) +async def create_smtp_server_tls(dirpath: Path, port: int, host="", context: ssl.SSLContext = None): + loop = asyncio.get_event_loop() + return await loop.create_server(partial(protocol_factory, dirpath), + host=host, port=port, ssl=context, start_serving=False) + + async def a_main(*args, **kwargs): - server = await create_smtp_server(*args, **kwargs) + server = await create_smtp_server_starttls(*args, **kwargs) await server.serve_forever()