Compare commits

..

4 Commits

Author SHA1 Message Date
holger krekel
9877c06bf1 some reformatting and striking overall 2023-10-20 11:03:02 +02:00
holger krekel
3a403d07de some improvements, adding a bnech 2023-10-20 10:59:26 +02:00
holger krekel
115a07aecf add ping-pong bench and formatting 2023-10-20 10:28:16 +02:00
holger krekel
ae2651c441 better benchmarking and reporting 2023-10-19 02:00:02 +02:00
16 changed files with 144 additions and 193 deletions

View File

@@ -1,18 +0,0 @@
name: CI
on:
pull_request:
push:
jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Lint chatmaild
working-directory: chatmaild
run: pipx run tox
- name: Lint deploy-chatmail
working-directory: deploy-chatmail
run: pipx run tox

View File

@@ -2,13 +2,13 @@ import logging
import os import os
import sys import sys
import json import json
import crypt
from socketserver import ( from socketserver import (
UnixStreamServer, UnixStreamServer,
StreamRequestHandler, StreamRequestHandler,
ThreadingMixIn, ThreadingMixIn,
) )
import pwd import pwd
import subprocess
from .database import Database from .database import Database
@@ -16,9 +16,17 @@ NOCREATE_FILE = "/etc/chatmail-nocreate"
def encrypt_password(password: str): def encrypt_password(password: str):
password = password.encode("ascii")
# https://doc.dovecot.org/configuration_manual/authentication/password_schemes/ # https://doc.dovecot.org/configuration_manual/authentication/password_schemes/
passhash = crypt.crypt(password, crypt.METHOD_SHA512) process = subprocess.Popen(
return "{SHA512-CRYPT}" + passhash ["doveadm", "pw", "-s", "SHA512-CRYPT"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
stdout_data, _stderr_data = process.communicate(
input=password + b"\n" + password + b"\n"
)
return stdout_data.decode("ascii").strip()
def create_user(db, user, password): def create_user(db, user, password):

View File

@@ -1,14 +1,12 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import asyncio import asyncio
import logging import logging
import time
import sys
from email.parser import BytesParser from email.parser import BytesParser
from email import policy from email import policy
from email.utils import parseaddr from email.utils import parseaddr
from aiosmtpd.smtp import SMTP from aiosmtpd.lmtp import LMTP
from aiosmtpd.controller import Controller from aiosmtpd.controller import UnixSocketController
from smtplib import SMTP as SMTPClient from smtplib import SMTP as SMTPClient
@@ -34,93 +32,94 @@ def check_encrypted(message):
return True return True
class SMTPController(Controller): class ExampleController(UnixSocketController):
def factory(self): def factory(self):
return SMTP(self.handler, **self.SMTP_kwargs) return LMTP(self.handler, **self.SMTP_kwargs)
class BeforeQueueHandler: class ExampleHandler:
def __init__(self): async def handle_RCPT(self, server, session, envelope, address, rcpt_options):
self.send_rate_limiter = SendRateLimiter() envelope.rcpt_tos.append(address)
async def handle_MAIL(self, server, session, envelope, address, mail_options):
logging.info(f"handle_MAIL from {address}")
envelope.mail_from = address
if not self.send_rate_limiter.is_sending_allowed(address):
return f"450 4.7.1: Too much mail from {address}"
parts = envelope.mail_from.split("@")
if len(parts) != 2:
return f"500 Invalid from address <{envelope.mail_from!r}>"
return "250 OK" return "250 OK"
async def handle_DATA(self, server, session, envelope): async def handle_DATA(self, server, session, envelope):
logging.info("handle_DATA before-queue") valid_recipients, res = lmtp_handle_DATA(envelope)
error = check_DATA(envelope) # Reinject the mail back into Postfix.
if error: if valid_recipients:
return error logging.info("Reinjecting the mail")
logging.info("re-injecting the mail that passed checks") client = SMTPClient("localhost", "10026")
client = SMTPClient("localhost", "10025") client.sendmail(envelope.mail_from, valid_recipients, envelope.content)
client.sendmail(envelope.mail_from, envelope.rcpt_tos, envelope.content) else:
return "250 OK" logging.info("no valid recipients, ignoring mail")
return "\r\n".join(res)
async def asyncmain_beforequeue(port): async def asyncmain(loop):
Controller(BeforeQueueHandler(), hostname="127.0.0.1", port=port).start() controller = ExampleController(
ExampleHandler(), unix_socket="/var/spool/postfix/private/filtermail"
)
controller.start()
def check_DATA(envelope): def lmtp_handle_DATA(envelope):
"""the central filtering function for e-mails.""" """the central filtering function for e-mails."""
logging.info(f"Processing DATA message from {envelope.mail_from}") logging.info(f"Processing DATA message from {envelope.mail_from}")
message = BytesParser(policy=policy.default).parsebytes(envelope.content) message = BytesParser(policy=policy.default).parsebytes(envelope.content)
mail_encrypted = check_encrypted(message) mail_encrypted = check_encrypted(message)
_, from_addr = parseaddr(message.get("from").strip()) valid_recipients = []
logging.info(f"mime-from: {from_addr} envelope-from: {envelope.mail_from!r}") res = []
if envelope.mail_from.lower() != from_addr.lower():
return f"500 Invalid FROM <{from_addr!r}> for <{envelope.mail_from!r}>"
envelope_from_domain = from_addr.split("@").pop()
for recipient in envelope.rcpt_tos: for recipient in envelope.rcpt_tos:
my_local_domain = envelope.mail_from.split("@")
if len(my_local_domain) != 2:
res += [f"500 Invalid from address <{envelope.mail_from}>"]
continue
_, from_addr = parseaddr(message.get("from").strip())
logging.info(f"mime-from: {from_addr} envelope-from: {envelope.mail_from}")
if envelope.mail_from.lower() != from_addr.lower():
res += [f"500 Invalid FROM <{from_addr}> for <{envelope.mail_from}>"]
continue
if envelope.mail_from == recipient: if envelope.mail_from == recipient:
# Always allow sending emails to self. # Always allow sending emails to self.
valid_recipients += [recipient]
res += ["250 OK"]
continue continue
res = recipient.split("@")
if len(res) != 2:
return f"500 Invalid address <{recipient}>"
_recipient_addr, recipient_domain = res
is_outgoing = recipient_domain != envelope_from_domain recipient_local_domain = recipient.split("@")
if is_outgoing and not mail_encrypted: if len(recipient_local_domain) != 2:
is_securejoin = message.get("secure-join") in ["vc-request", "vg-request"] res += [f"500 Invalid address <{recipient}>"]
if not is_securejoin: continue
return f"500 Invalid unencrypted mail to <{recipient}>"
is_outgoing = recipient_local_domain[1] != my_local_domain[1]
class SendRateLimiter: if (
MAX_USER_SEND_PER_MINUTE = 80 is_outgoing
and not mail_encrypted
and message.get("secure-join") != "vc-request"
and message.get("secure-join") != "vg-request"
):
res += ["500 Outgoing mail must be encrypted"]
continue
def __init__(self): valid_recipients += [recipient]
self.addr2timestamps = {} res += ["250 OK"]
def is_sending_allowed(self, mail_from): assert len(envelope.rcpt_tos) == len(res)
last = self.addr2timestamps.setdefault(mail_from, []) assert len(valid_recipients) <= len(res)
now = time.time() return valid_recipients, res
last[:] = [ts for ts in last if ts >= (now - 60)]
if len(last) <= self.MAX_USER_SEND_PER_MINUTE:
last.append(now)
return True
return False
def main(): def main():
args = sys.argv[1:]
assert len(args) == 1
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
task = asyncmain_beforequeue(port=int(args[0])) loop.create_task(asyncmain(loop=loop))
loop.create_task(task)
loop.run_forever() loop.run_forever()
if __name__ == "__main__":
main()

View File

@@ -1,8 +1,8 @@
[Unit] [Unit]
Description=Chatmail Postfix BeforeQeue filter Description=Email filter for chatmail servers
[Service] [Service]
ExecStart=/usr/local/bin/filtermail 10080 ExecStart=/usr/local/bin/filtermail
Restart=always Restart=always
RestartSec=30 RestartSec=30

View File

@@ -1,7 +1,6 @@
from .filtermail import check_encrypted, check_DATA, SendRateLimiter from .filtermail import check_encrypted, lmtp_handle_DATA
from email.parser import BytesParser from email.parser import BytesParser
from email import policy from email import policy
import pytest
def test_reject_forged_from(): def test_reject_forged_from():
@@ -31,12 +30,15 @@ def test_reject_forged_from():
# test that the filter lets good mail through # test that the filter lets good mail through
envelope.content = makemail(envelope.mail_from).as_bytes() envelope.content = makemail(envelope.mail_from).as_bytes()
assert not check_DATA(envelope=envelope) valid_recipients, res = lmtp_handle_DATA(envelope=envelope)
assert valid_recipients == envelope.rcpt_tos
assert len(res) == 1 and "250" in res[0]
# test that the filter rejects forged mail # test that the filter rejects forged mail
envelope.content = makemail("forged@c3.testrun.org").as_bytes() envelope.content = makemail("forged@c3.testrun.org").as_bytes()
error = check_DATA(envelope=envelope) valid_recipients, res = lmtp_handle_DATA(envelope=envelope)
assert "500" in error assert not valid_recipients
assert len(res) == 1 and "500" in res[0]
def test_filtermail(): def test_filtermail():
@@ -324,15 +326,3 @@ def test_filtermail():
] ]
).encode() ).encode()
) )
def test_send_rate_limiter():
limiter = SendRateLimiter()
for i in range(100):
if limiter.is_sending_allowed("some@example.org"):
if i <= SendRateLimiter.MAX_USER_SEND_PER_MINUTE:
continue
pytest.fail("limiter didn't work")
else:
assert i == SendRateLimiter.MAX_USER_SEND_PER_MINUTE + 1
break

View File

@@ -34,28 +34,43 @@ def _install_chatmaild() -> None:
commands=[f"pip install --break-system-packages {remote_path}"], commands=[f"pip install --break-system-packages {remote_path}"],
) )
for fn in ( files.put(
"doveauth-dictproxy", name="upload doveauth-dictproxy.service",
"filtermail", src=importlib.resources.files("chatmaild")
): .joinpath("doveauth-dictproxy.service")
files.put( .open("rb"),
name=f"Upload {fn}.service", dest="/etc/systemd/system/doveauth-dictproxy.service",
src=importlib.resources.files("chatmaild") user="root",
.joinpath(f"{fn}.service") group="root",
.open("rb"), mode="644",
dest=f"/etc/systemd/system/{fn}.service", )
user="root", systemd.service(
group="root", name="Setup doveauth-dictproxy service",
mode="644", service="doveauth-dictproxy.service",
) running=True,
systemd.service( enabled=True,
name=f"Setup {fn} service", restarted=True,
service=f"{fn}.service", daemon_reload=True,
running=True, )
enabled=True,
restarted=True, files.put(
daemon_reload=True, name="upload filtermail.service",
) src=importlib.resources.files("chatmaild")
.joinpath("filtermail.service")
.open("rb"),
dest="/etc/systemd/system/filtermail.service",
user="root",
group="root",
mode="644",
)
systemd.service(
name="Setup filtermail service",
service="filtermail.service",
running=True,
enabled=True,
restarted=True,
daemon_reload=True,
)
def _configure_opendkim(domain: str, dkim_selector: str) -> bool: def _configure_opendkim(domain: str, dkim_selector: str) -> bool:
@@ -277,22 +292,6 @@ def deploy_chatmail(mail_domain: str, mail_server: str, dkim_selector: str) -> N
commands=[f"echo {mail_domain} >/etc/mailname; chmod 644 /etc/mailname"], commands=[f"echo {mail_domain} >/etc/mailname; chmod 644 /etc/mailname"],
) )
journald_conf = files.put(
name="Configure journald",
src=importlib.resources.files(__package__).joinpath("journald.conf"),
dest="/etc/systemd/journald.conf",
user="root",
group="root",
mode="644",
)
systemd.service(
name="Start and enable journald",
service="systemd-journald.service",
running=True,
enabled=True,
restarted=journald_conf,
)
def callback(): def callback():
result = server.shell( result = server.shell(
commands=[ commands=[

View File

@@ -1,6 +1,6 @@
import importlib.resources import importlib.resources
from pyinfra.operations import apt, files, server from pyinfra.operations import apt, files, systemd, server
def deploy_acmetool(nginx_hook=False, email="", domains=[]): def deploy_acmetool(nginx_hook=False, email="", domains=[]):

View File

@@ -1,2 +0,0 @@
[Journal]
MaxRetentionSec=3d

View File

@@ -32,7 +32,7 @@ submission inet n - y - - smtpd
-o smtpd_recipient_restrictions= -o smtpd_recipient_restrictions=
-o smtpd_relay_restrictions=permit_sasl_authenticated,reject -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING -o milter_macro_daemon_name=ORIGINATING
-o smtpd_proxy_filter=127.0.0.1:10080 -o content_filter=filter:unix:private/filtermail
smtps inet n - y - - smtpd smtps inet n - y - - smtpd
-o syslog_name=postfix/smtps -o syslog_name=postfix/smtps
-o smtpd_tls_wrappermode=yes -o smtpd_tls_wrappermode=yes
@@ -47,7 +47,7 @@ smtps inet n - y - - smtpd
-o smtpd_recipient_restrictions= -o smtpd_recipient_restrictions=
-o smtpd_relay_restrictions=permit_sasl_authenticated,reject -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING -o milter_macro_daemon_name=ORIGINATING
-o smtpd_proxy_filter=127.0.0.1:10080 -o content_filter=filter:unix:private/filtermail
#628 inet n - y - - qmqpd #628 inet n - y - - qmqpd
pickup unix n - y 60 1 pickup pickup unix n - y 60 1 pickup
cleanup unix n - y - 0 cleanup cleanup unix n - y - 0 cleanup
@@ -76,5 +76,5 @@ scache unix - - y - 1 scache
postlog unix-dgram n - n - 1 postlogd postlog unix-dgram n - n - 1 postlogd
filter unix - n n - - lmtp filter unix - n n - - lmtp
# Local SMTP server for reinjecting filered mail. # Local SMTP server for reinjecting filered mail.
localhost:10025 inet n - n - 10 smtpd localhost:10026 inet n - n - 10 smtpd
-o syslog_name=postfix/reinject -o content_filter=

View File

@@ -45,7 +45,7 @@ class TestDC:
msg.chat.send_text("pong") msg.chat.send_text("pong")
ac1.wait_next_incoming_message() ac1.wait_next_incoming_message()
benchmark(ping_pong, 5) benchmark(ping_pong, 3)
def test_send_10_receive_10(self, benchmark, cmfactory, lp): def test_send_10_receive_10(self, benchmark, cmfactory, lp):
ac1, ac2 = cmfactory.get_online_accounts(2) ac1, ac2 = cmfactory.get_online_accounts(2)
@@ -57,4 +57,4 @@ class TestDC:
for i in range(10): for i in range(10):
ac2.wait_next_incoming_message() ac2.wait_next_incoming_message()
benchmark(send_10_receive_10, 5) benchmark(send_10_receive_10, 1)

View File

@@ -6,7 +6,6 @@ import subprocess
import imaplib import imaplib
import smtplib import smtplib
import itertools import itertools
from math import ceil
import pytest import pytest
@@ -18,9 +17,6 @@ def pytest_addoption(parser):
def pytest_configure(config): def pytest_configure(config):
config._benchresults = {} config._benchresults = {}
config.addinivalue_line(
"markers", "slow: mark test to require --slow option to run"
)
def pytest_runtest_setup(item): def pytest_runtest_setup(item):
@@ -82,28 +78,14 @@ def benchmark(request):
def pytest_terminal_summary(terminalreporter): def pytest_terminal_summary(terminalreporter):
tr = terminalreporter tr = terminalreporter
results = tr.config._benchresults results = tr.config._benchresults
if not results:
return
tr.section("benchmark results") tr.section("benchmark results")
float_names = 'median min max'.split() headers = f"{'benchmark name': <30} {'median': >6}"
width = max(map(len, float_names))
def fcol(parts):
return " ".join(part.rjust(width) for part in parts)
headers = f"{'benchmark name': <30} " + fcol(float_names)
tr.write_line(headers) tr.write_line(headers)
tr.write_line("-" * len(headers)) tr.write_line("-" * len(headers))
for name, durations in results.items(): for name, durations in results.items():
measures = [ median = sorted(durations)[len(durations) // 2]
sorted(durations)[len(durations) // 2], median = f"{median:2.4f}"
min(durations), tr.write_line(f"{name: <30} {median: >6}")
max(durations),
]
line = f"{name: <30} "
line += fcol(f"{float: 2.2f}" for float in measures)
tr.write_line(line)
@pytest.fixture @pytest.fixture

View File

@@ -1,2 +1,3 @@
[pytest] [pytest]
addopts = -vrsx --strict-markers addopts = -vrsx --strict-markers
markers = slow: mark test as slow (requires --slow option to run)

View File

@@ -20,7 +20,7 @@ def test_use_two_chatmailservers(cmfactory, maildomain2):
@pytest.mark.parametrize("forgeaddr", ["internal", "someone@example.org"]) @pytest.mark.parametrize("forgeaddr", ["internal", "someone@example.org"])
def test_reject_forged_from(cmsetup, mailgen, lp, forgeaddr): def test_reject_forged_from(cmsetup, mailgen, lp, remote, forgeaddr):
user1, user3 = cmsetup.gen_users(2) user1, user3 = cmsetup.gen_users(2)
lp.sec("send encrypted message with forged from") lp.sec("send encrypted message with forged from")
@@ -36,25 +36,17 @@ def test_reject_forged_from(cmsetup, mailgen, lp, forgeaddr):
print(f" {line}") print(f" {line}")
lp.sec("Send forged mail and check remote postfix lmtp processing result") lp.sec("Send forged mail and check remote postfix lmtp processing result")
with pytest.raises(smtplib.SMTPException) as e: remote_log = remote.iter_output("journalctl -t postfix/lmtp")
user1.smtp.sendmail(from_addr=user1.addr, to_addrs=[user3.addr], msg=msg) user1.smtp.sendmail(from_addr=user1.addr, to_addrs=[user3.addr], msg=msg)
assert "500" in str(e.value) for line in remote_log:
# print(line)
if "500 invalid from" in line and user3.addr in line:
break
else:
pytest.fail("remote postfix/filtermail failed to reject message")
# check that the logged in user (who sent the forged msg) got a non-delivery notice
@pytest.mark.slow for message in user1.imap.fetch_all_messages():
def test_exceed_rate_limit(cmsetup, gencreds, mailgen): if "Invalid FROM" in message and addr_to_forge in message:
"""Test that the per-account send-mail limit is exceeded."""
user1, user2 = cmsetup.gen_users(2)
mail = mailgen.get_encrypted(user1.addr, user2.addr)
for i in range(100):
print("Sending mail", str(i))
try:
user1.smtp.sendmail(user1.addr, [user2.addr], mail)
except smtplib.SMTPException as e:
if i < 80:
pytest.fail(f"rate limit was exceeded too early with msg {i}")
outcome = e.recipients[user2.addr]
assert outcome[0] == 450
assert b'4.7.1: Too much mail from' in outcome[1]
return return
pytest.fail("Rate limit was not exceeded") pytest.fail(f"forged From={addr_to_forge} did not cause non-delivery notice")

View File

@@ -1,5 +1,4 @@
import pytest import pytest
import smtplib
def test_login_basic_functioning(imap_or_smtp, gencreds, lp): def test_login_basic_functioning(imap_or_smtp, gencreds, lp):

View File

@@ -6,6 +6,7 @@ deploy-chatmail/venv/bin/pip install -e deploy-chatmail
deploy-chatmail/venv/bin/pip install -e chatmaild deploy-chatmail/venv/bin/pip install -e chatmaild
python3 -m venv chatmaild/venv python3 -m venv chatmaild/venv
sudo apt install -y dovecot-core && sudo systemctl disable --now dovecot
chatmaild/venv/bin/pip install --upgrade pytest build 'setuptools>=68' chatmaild/venv/bin/pip install --upgrade pytest build 'setuptools>=68'
chatmaild/venv/bin/pip install -e chatmaild chatmaild/venv/bin/pip install -e chatmaild