Skip to content

Commit

Permalink
updated settings based on new understanding of TLS and STARTTLS
Browse files Browse the repository at this point in the history
  • Loading branch information
geek-at committed Nov 28, 2023
1 parent d6d37d1 commit fab8497
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,2 +1,3 @@
config.ini
web/version.txt
web/version.txt
python/*.pem
2 changes: 1 addition & 1 deletion docker/rootfs/start.sh
Expand Up @@ -40,7 +40,7 @@ _buildConfig() {
echo "MAILPORT=${MAILPORT:-25}"
echo "DISCARD_UNKNOWN=${DISCARD_UNKNOWN:-true}"
echo "ATTACHMENTS_MAX_SIZE=${ATTACHMENTS_MAX_SIZE:-0}"
echo "MAILPORT_STARTTLS=${MAILPORT_STARTTLS:-0}"
echo "MAILPORT_TLS=${MAILPORT_TLS:-0}"
echo "TLS_CERTIFICATE=${TLS_CERTIFICATE:-}"
echo "TLS_PRIVATE_KEY=${TLS_PRIVATE_KEY:-0}"
echo ""
Expand Down
2 changes: 1 addition & 1 deletion example.config.ini
Expand Up @@ -33,7 +33,7 @@ MAILPORT=25

; TLS settings
;
; MAILPORT_STARTTLS=587
; MAILPORT_TLS=587
; TLS_CERTIFICATE=/path/to/your/fullchain.pem
; TLS_PRIVATE_KEY=/path/to/your/privkey.pem

Expand Down
34 changes: 20 additions & 14 deletions python/mailserver3.py
Expand Up @@ -23,7 +23,7 @@
DOMAINS = []
LAST_CLEANUP = 0
URL = ""
MAILPORT_STARTTLS = 0
MAILPORT_TLS = 0
TLS_CERTIFICATE = ""
TLS_PRIVATE_KEY = ""

Expand All @@ -34,9 +34,9 @@ async def handle_DATA(self, server, session, envelope):
for rcpt in envelope.rcpt_tos:
rcpts.append(rcpt)
if(server.tls_context != None):
logger.debug('Receiving message from: %s:%d (TLS)' % peer)
logger.debug('Receiving message from: %s:%d (STARTTLS)' % peer)
else:
logger.debug('Receiving message from: %s:%d (Plaintext)' % peer)
logger.debug('Receiving message from: %s:%d (Plaintext (or TLS))' % peer)
logger.debug('Message addressed from: %s' % envelope.mail_from)
logger.debug('Message addressed to: %s' % str(rcpts))

Expand Down Expand Up @@ -183,17 +183,23 @@ def cleanup():

async def run(port):

if MAILPORT_STARTTLS > 0 and TLS_CERTIFICATE != "" and TLS_PRIVATE_KEY != "":
if TLS_CERTIFICATE != "" and TLS_PRIVATE_KEY != "":
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain(TLS_CERTIFICATE, TLS_PRIVATE_KEY)
controller_starttls = Controller(CustomHandler(), hostname='0.0.0.0', port=MAILPORT_STARTTLS, tls_context=context)
controller_starttls.start()
logger.info("[i] Starting TLS Mailserver on port " + str(MAILPORT_STARTTLS))
if MAILPORT_TLS > 0:
controller_tls = Controller(CustomHandler(), hostname='0.0.0.0', port=MAILPORT_TLS, ssl_context=context)
controller_tls.start()

controller_plaintext = Controller(CustomHandler(), hostname='0.0.0.0', port=port)
controller_plaintext.start()
controller_plaintext = Controller(CustomHandler(), hostname='0.0.0.0', port=port,tls_context=context)
controller_plaintext.start()

logger.info("[i] Starting plaintext Mailserver on port " + str(port))
logger.info("[i] Starting TLS only Mailserver on port " + str(MAILPORT_TLS))
logger.info("[i] Starting plaintext Mailserver (with STARTTLS support) on port " + str(port))
else:
controller_plaintext = Controller(CustomHandler(), hostname='0.0.0.0', port=port)
controller_plaintext.start()

logger.info("[i] Starting plaintext Mailserver on port " + str(port))


logger.info("[i] Ready to receive Emails")
Expand All @@ -204,8 +210,8 @@ async def run(port):
await asyncio.sleep(1)
except KeyboardInterrupt:
controller_plaintext.stop()
if(MAILPORT_STARTTLS > 0 and TLS_CERTIFICATE != "" and TLS_PRIVATE_KEY != ""):
controller_starttls.stop()
if(MAILPORT_TLS > 0 and TLS_CERTIFICATE != "" and TLS_PRIVATE_KEY != ""):
controller_tls.stop()

if __name__ == '__main__':
ch = logging.StreamHandler()
Expand All @@ -232,8 +238,8 @@ async def run(port):
if("CLEANUP" in Config.sections() and "delete_older_than_days" in Config.options("CLEANUP")):
DELETE_OLDER_THAN_DAYS = (Config.get("CLEANUP", "DELETE_OLDER_THAN_DAYS").lower() == "true")

if("mailport_starttls" in Config.options("MAILSERVER")):
MAILPORT_STARTTLS = int(Config.get("MAILSERVER", "MAILPORT_STARTTLS"))
if("mailport_tls" in Config.options("MAILSERVER")):
MAILPORT_TLS = int(Config.get("MAILSERVER", "MAILPORT_TLS"))
if("tls_certificate" in Config.options("MAILSERVER")):
TLS_CERTIFICATE = Config.get("MAILSERVER", "TLS_CERTIFICATE")
if("tls_private_key" in Config.options("MAILSERVER")):
Expand Down

0 comments on commit fab8497

Please sign in to comment.