mirror of
https://github.com/chatmail/relay.git
synced 2026-05-16 19:18:57 +00:00
Compare commits
1 Commits
fix-cmdepl
...
j4n/initen
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd542ada40 |
@@ -21,8 +21,7 @@ where = ['src']
|
|||||||
[project.scripts]
|
[project.scripts]
|
||||||
doveauth = "chatmaild.doveauth:main"
|
doveauth = "chatmaild.doveauth:main"
|
||||||
chatmail-metadata = "chatmaild.metadata:main"
|
chatmail-metadata = "chatmaild.metadata:main"
|
||||||
chatmail-expire = "chatmaild.expire:daily_expire_main"
|
chatmail-expire = "chatmaild.expire:main"
|
||||||
chatmail-quota-expire = "chatmaild.expire:quota_expire_main"
|
|
||||||
chatmail-fsreport = "chatmaild.fsreport:main"
|
chatmail-fsreport = "chatmaild.fsreport:main"
|
||||||
lastlogin = "chatmaild.lastlogin:main"
|
lastlogin = "chatmaild.lastlogin:main"
|
||||||
turnserver = "chatmaild.turnserver:main"
|
turnserver = "chatmaild.turnserver:main"
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import iniconfig
|
import iniconfig
|
||||||
@@ -46,6 +47,8 @@ class Config:
|
|||||||
)
|
)
|
||||||
self.mtail_address = params.get("mtail_address")
|
self.mtail_address = params.get("mtail_address")
|
||||||
self.disable_ipv6 = params.get("disable_ipv6", "false").lower() == "true"
|
self.disable_ipv6 = params.get("disable_ipv6", "false").lower() == "true"
|
||||||
|
self.addr_v4 = os.environ.get("CHATMAIL_ADDR_V4", "")
|
||||||
|
self.addr_v6 = os.environ.get("CHATMAIL_ADDR_V6", "")
|
||||||
self.acme_email = params.get("acme_email", "")
|
self.acme_email = params.get("acme_email", "")
|
||||||
self.imap_rawlog = params.get("imap_rawlog", "false").lower() == "true"
|
self.imap_rawlog = params.get("imap_rawlog", "false").lower() == "true"
|
||||||
self.imap_compress = params.get("imap_compress", "false").lower() == "true"
|
self.imap_compress = params.get("imap_compress", "false").lower() == "true"
|
||||||
@@ -92,11 +95,6 @@ class Config:
|
|||||||
# old unused option (except for first migration from sqlite to maildir store)
|
# old unused option (except for first migration from sqlite to maildir store)
|
||||||
self.passdb_path = Path(params.get("passdb_path", "/home/vmail/passdb.sqlite"))
|
self.passdb_path = Path(params.get("passdb_path", "/home/vmail/passdb.sqlite"))
|
||||||
|
|
||||||
@property
|
|
||||||
def max_mailbox_size_mb(self):
|
|
||||||
"""Return max_mailbox_size as an integer in megabytes."""
|
|
||||||
return parse_size_mb(self.max_mailbox_size)
|
|
||||||
|
|
||||||
def _getbytefile(self):
|
def _getbytefile(self):
|
||||||
return open(self._inipath, "rb")
|
return open(self._inipath, "rb")
|
||||||
|
|
||||||
@@ -110,16 +108,6 @@ class Config:
|
|||||||
return User(maildir, addr, password_path, uid="vmail", gid="vmail")
|
return User(maildir, addr, password_path, uid="vmail", gid="vmail")
|
||||||
|
|
||||||
|
|
||||||
def parse_size_mb(limit):
|
|
||||||
"""Parse a size string like ``500M`` or ``2G`` and return megabytes."""
|
|
||||||
value = limit.strip().upper().removesuffix("B")
|
|
||||||
if value.endswith("G"):
|
|
||||||
return int(value[:-1]) * 1024
|
|
||||||
if value.endswith("M"):
|
|
||||||
return int(value[:-1])
|
|
||||||
return int(value)
|
|
||||||
|
|
||||||
|
|
||||||
def write_initial_config(inipath, mail_domain, overrides):
|
def write_initial_config(inipath, mail_domain, overrides):
|
||||||
"""Write out default config file, using the specified config value overrides."""
|
"""Write out default config file, using the specified config value overrides."""
|
||||||
content = get_default_config_content(mail_domain, **overrides)
|
content = get_default_config_content(mail_domain, **overrides)
|
||||||
|
|||||||
@@ -4,26 +4,17 @@ Expire old messages and addresses.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import re
|
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
|
||||||
from stat import S_ISREG
|
from stat import S_ISREG
|
||||||
|
|
||||||
from chatmaild.config import read_config
|
from chatmaild.config import read_config
|
||||||
|
|
||||||
FileEntry = namedtuple("FileEntry", ("path", "mtime", "size"))
|
FileEntry = namedtuple("FileEntry", ("path", "mtime", "size"))
|
||||||
QuotaFileEntry = namedtuple("QuotaFileEntry", ("mtime", "quota_size", "path"))
|
|
||||||
|
|
||||||
# Quota cleanup factor of max_mailbox_size. The mailbox is reset to this size.
|
|
||||||
QUOTA_CLEANUP_FACTOR = 0.7
|
|
||||||
|
|
||||||
# e.g. "cur/1775324677.M448978P3029757.exam,S=3235,W=3305:2,S"
|
|
||||||
_dovecot_fn_rex = re.compile(r".+/(\d+)\..+,S=(\d+)")
|
|
||||||
|
|
||||||
|
|
||||||
def iter_mailboxes(basedir, maxnum):
|
def iter_mailboxes(basedir, maxnum):
|
||||||
@@ -83,42 +74,6 @@ class MailboxStat:
|
|||||||
self.extrafiles.sort(key=lambda x: -x.size)
|
self.extrafiles.sort(key=lambda x: -x.size)
|
||||||
|
|
||||||
|
|
||||||
def parse_dovecot_filename(relpath):
|
|
||||||
m = _dovecot_fn_rex.match(relpath)
|
|
||||||
if not m:
|
|
||||||
return None
|
|
||||||
return QuotaFileEntry(int(m.group(1)), int(m.group(2)), relpath)
|
|
||||||
|
|
||||||
|
|
||||||
def scan_mailbox_messages(mbox):
|
|
||||||
messages = []
|
|
||||||
for sub in ("cur", "new"):
|
|
||||||
for name in os_listdir_if_exists(mbox / sub):
|
|
||||||
if entry := parse_dovecot_filename(f"{sub}/{name}"):
|
|
||||||
messages.append(entry)
|
|
||||||
return messages
|
|
||||||
|
|
||||||
|
|
||||||
def expire_to_target(mbox, target_bytes):
|
|
||||||
messages = scan_mailbox_messages(mbox)
|
|
||||||
total_size = sum(m.quota_size for m in messages)
|
|
||||||
# Keep recent 24 hours of messages protected from expiry because
|
|
||||||
# likely something is wrong with interactions on that address
|
|
||||||
# and quota-full signal can help the address owner's device to notice it
|
|
||||||
undeletable_messages_cutoff = time.time() - (3600 * 24)
|
|
||||||
removed = 0
|
|
||||||
for entry in sorted(messages):
|
|
||||||
if total_size <= target_bytes:
|
|
||||||
break
|
|
||||||
if entry.mtime > undeletable_messages_cutoff:
|
|
||||||
break
|
|
||||||
(mbox / entry.path).unlink(missing_ok=True)
|
|
||||||
total_size -= entry.quota_size
|
|
||||||
removed += 1
|
|
||||||
|
|
||||||
return removed
|
|
||||||
|
|
||||||
|
|
||||||
def print_info(msg):
|
def print_info(msg):
|
||||||
print(msg, file=sys.stderr)
|
print(msg, file=sys.stderr)
|
||||||
|
|
||||||
@@ -188,19 +143,6 @@ class Expiry:
|
|||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
target_bytes = (
|
|
||||||
self.config.max_mailbox_size_mb * 1024 * 1024 * QUOTA_CLEANUP_FACTOR
|
|
||||||
)
|
|
||||||
removed = expire_to_target(Path(mbox.basedir), target_bytes)
|
|
||||||
if removed:
|
|
||||||
changed = True
|
|
||||||
self.del_files += removed
|
|
||||||
if self.verbose:
|
|
||||||
print_info(
|
|
||||||
f"quota-expire: removed {removed} message(s) from {mboxname}"
|
|
||||||
)
|
|
||||||
|
|
||||||
if changed:
|
if changed:
|
||||||
self.remove_file(f"{mbox.basedir}/maildirsize")
|
self.remove_file(f"{mbox.basedir}/maildirsize")
|
||||||
|
|
||||||
@@ -212,9 +154,9 @@ class Expiry:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def daily_expire_main(args=None):
|
def main(args=None):
|
||||||
"""Expire mailboxes and messages according to chatmail config"""
|
"""Expire mailboxes and messages according to chatmail config"""
|
||||||
parser = ArgumentParser(description=daily_expire_main.__doc__)
|
parser = ArgumentParser(description=main.__doc__)
|
||||||
ini = "/usr/local/lib/chatmaild/chatmail.ini"
|
ini = "/usr/local/lib/chatmaild/chatmail.ini"
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"chatmail_ini",
|
"chatmail_ini",
|
||||||
@@ -260,33 +202,5 @@ def daily_expire_main(args=None):
|
|||||||
print(exp.get_summary())
|
print(exp.get_summary())
|
||||||
|
|
||||||
|
|
||||||
def quota_expire_main(args=None):
|
if __name__ == "__main__":
|
||||||
"""Remove mailbox messages to stay within a megabyte target.
|
main(sys.argv[1:])
|
||||||
|
|
||||||
This entry point is called by dovecot when a quota threshold is passed.
|
|
||||||
"""
|
|
||||||
|
|
||||||
parser = ArgumentParser(description=quota_expire_main.__doc__)
|
|
||||||
parser.add_argument(
|
|
||||||
"target_mb",
|
|
||||||
type=int,
|
|
||||||
help="target mailbox size in megabytes",
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"mailbox_path",
|
|
||||||
type=Path,
|
|
||||||
help="path to a user mailbox",
|
|
||||||
)
|
|
||||||
args = parser.parse_args(args)
|
|
||||||
|
|
||||||
target_bytes = args.target_mb * 1024 * 1024
|
|
||||||
|
|
||||||
removed_count = expire_to_target(args.mailbox_path, target_bytes)
|
|
||||||
if removed_count:
|
|
||||||
(args.mailbox_path / "maildirsize").unlink(missing_ok=True)
|
|
||||||
print(
|
|
||||||
f"quota-expire: removed {removed_count} message(s)"
|
|
||||||
f" from {args.mailbox_path.name}",
|
|
||||||
file=sys.stderr,
|
|
||||||
)
|
|
||||||
return 0
|
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ max_user_send_per_minute = 60
|
|||||||
max_user_send_burst_size = 10
|
max_user_send_burst_size = 10
|
||||||
|
|
||||||
# maximum mailbox size of a chatmail address
|
# maximum mailbox size of a chatmail address
|
||||||
# Oldest messages will be removed automatically, so mailboxes never run full.
|
|
||||||
max_mailbox_size = 500M
|
max_mailbox_size = 500M
|
||||||
|
|
||||||
# maximum message size for an e-mail in bytes
|
# maximum message size for an e-mail in bytes
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from chatmaild.config import parse_size_mb, read_config
|
from chatmaild.config import read_config
|
||||||
|
|
||||||
|
|
||||||
def test_read_config_basic(example_config):
|
def test_read_config_basic(example_config):
|
||||||
@@ -121,17 +121,3 @@ def test_config_tls_external_bad_format(make_config):
|
|||||||
"tls_external_cert_and_key": "/only/one/path.pem",
|
"tls_external_cert_and_key": "/only/one/path.pem",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_parse_size_mb():
|
|
||||||
assert parse_size_mb("500M") == 500
|
|
||||||
assert parse_size_mb("2G") == 2048
|
|
||||||
assert parse_size_mb(" 1g ") == 1024
|
|
||||||
assert parse_size_mb("100MB") == 100
|
|
||||||
assert parse_size_mb("256") == 256
|
|
||||||
|
|
||||||
|
|
||||||
def test_max_mailbox_size_mb(make_config):
|
|
||||||
config = make_config("chat.example.org")
|
|
||||||
assert config.max_mailbox_size == "500M"
|
|
||||||
assert config.max_mailbox_size_mb == 500
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
from chatmaild.doveauth import AuthDictProxy
|
from chatmaild.doveauth import AuthDictProxy
|
||||||
from chatmaild.expire import daily_expire_main as main_expire
|
from chatmaild.expire import main as main_expire
|
||||||
|
|
||||||
|
|
||||||
def test_login_timestamps(example_config):
|
def test_login_timestamps(example_config):
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import itertools
|
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import time
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from fnmatch import fnmatch
|
from fnmatch import fnmatch
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -11,19 +9,13 @@ import pytest
|
|||||||
from chatmaild.expire import (
|
from chatmaild.expire import (
|
||||||
FileEntry,
|
FileEntry,
|
||||||
MailboxStat,
|
MailboxStat,
|
||||||
expire_to_target,
|
|
||||||
get_file_entry,
|
get_file_entry,
|
||||||
iter_mailboxes,
|
iter_mailboxes,
|
||||||
os_listdir_if_exists,
|
os_listdir_if_exists,
|
||||||
parse_dovecot_filename,
|
|
||||||
quota_expire_main,
|
|
||||||
scan_mailbox_messages,
|
|
||||||
)
|
)
|
||||||
from chatmaild.expire import daily_expire_main as expiry_main
|
from chatmaild.expire import main as expiry_main
|
||||||
from chatmaild.fsreport import main as report_main
|
from chatmaild.fsreport import main as report_main
|
||||||
|
|
||||||
MB = 1024 * 1024
|
|
||||||
|
|
||||||
|
|
||||||
def fill_mbox(folderdir):
|
def fill_mbox(folderdir):
|
||||||
password = folderdir.joinpath("password")
|
password = folderdir.joinpath("password")
|
||||||
@@ -204,51 +196,3 @@ def test_os_listdir_if_exists(tmp_path):
|
|||||||
tmp_path.joinpath("x").write_text("hello")
|
tmp_path.joinpath("x").write_text("hello")
|
||||||
assert len(os_listdir_if_exists(str(tmp_path))) == 1
|
assert len(os_listdir_if_exists(str(tmp_path))) == 1
|
||||||
assert len(os_listdir_if_exists(str(tmp_path.joinpath("123123")))) == 0
|
assert len(os_listdir_if_exists(str(tmp_path.joinpath("123123")))) == 0
|
||||||
|
|
||||||
|
|
||||||
# --- quota expire tests ---
|
|
||||||
|
|
||||||
_msg_counter = itertools.count(1)
|
|
||||||
|
|
||||||
|
|
||||||
def _create_message(basedir, sub, size, days_old=0, disk_size=None):
|
|
||||||
seq = next(_msg_counter)
|
|
||||||
mtime = int(time.time() - days_old * 86400)
|
|
||||||
name = f"{mtime}.M1P1Q{seq}.hostname,S={size},W={size}:2,S"
|
|
||||||
path = basedir / sub / name
|
|
||||||
path.parent.mkdir(parents=True, exist_ok=True)
|
|
||||||
path.write_bytes(b"x" * (disk_size if disk_size is not None else size))
|
|
||||||
os.utime(path, (mtime, mtime))
|
|
||||||
return path
|
|
||||||
|
|
||||||
|
|
||||||
def test_parse_dovecot_filename():
|
|
||||||
e = parse_dovecot_filename("cur/1775324677.M448978P3029757.exam,S=3235,W=3305:2,S")
|
|
||||||
assert e.path == "cur/1775324677.M448978P3029757.exam,S=3235,W=3305:2,S"
|
|
||||||
assert e.mtime == 1775324677
|
|
||||||
assert e.quota_size == 3235
|
|
||||||
assert parse_dovecot_filename("cur/msg_without_structure") is None
|
|
||||||
|
|
||||||
|
|
||||||
def test_expire_to_target(tmp_path):
|
|
||||||
_create_message(tmp_path, "cur", MB, days_old=10, disk_size=100)
|
|
||||||
_create_message(tmp_path, "new", MB, days_old=5)
|
|
||||||
_create_message(tmp_path, "cur", MB, days_old=0) # undeletable (<1 hour)
|
|
||||||
assert len(scan_mailbox_messages(tmp_path)) == 3
|
|
||||||
# removes oldest first, uses S= size not disk size
|
|
||||||
removed = expire_to_target(tmp_path, MB)
|
|
||||||
assert removed == 2
|
|
||||||
msgs = scan_mailbox_messages(tmp_path)
|
|
||||||
assert len(msgs) == 1
|
|
||||||
# the surviving message is the fresh undeletable one
|
|
||||||
assert msgs[0].mtime > time.time() - 3600
|
|
||||||
|
|
||||||
|
|
||||||
def test_quota_expire_main(tmp_path, capsys):
|
|
||||||
mbox = tmp_path / "user@example.org"
|
|
||||||
_create_message(mbox, "cur", 2 * MB, days_old=5)
|
|
||||||
(mbox / "maildirsize").write_text("x")
|
|
||||||
quota_expire_main([str(1), str(mbox)])
|
|
||||||
_, err = capsys.readouterr()
|
|
||||||
assert "quota-expire: removed 1 message(s) from user@example.org" in err
|
|
||||||
assert not (mbox / "maildirsize").exists()
|
|
||||||
|
|||||||
@@ -93,9 +93,7 @@ def run_cmd(args, out):
|
|||||||
strict_tls = args.config.tls_cert_mode == "acme"
|
strict_tls = args.config.tls_cert_mode == "acme"
|
||||||
if not args.dns_check_disabled:
|
if not args.dns_check_disabled:
|
||||||
remote_data = dns.get_initial_remote_data(sshexec, args.config.mail_domain)
|
remote_data = dns.get_initial_remote_data(sshexec, args.config.mail_domain)
|
||||||
if not dns.check_initial_remote_data(
|
if not dns.check_initial_remote_data(remote_data, strict_tls=strict_tls, print=out.red):
|
||||||
remote_data, strict_tls=strict_tls, print=out.red
|
|
||||||
):
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
env = os.environ.copy()
|
env = os.environ.copy()
|
||||||
@@ -103,6 +101,9 @@ def run_cmd(args, out):
|
|||||||
env["CHATMAIL_WEBSITE_ONLY"] = "True" if args.website_only else ""
|
env["CHATMAIL_WEBSITE_ONLY"] = "True" if args.website_only else ""
|
||||||
env["CHATMAIL_DISABLE_MAIL"] = "True" if args.disable_mail else ""
|
env["CHATMAIL_DISABLE_MAIL"] = "True" if args.disable_mail else ""
|
||||||
env["CHATMAIL_REQUIRE_IROH"] = "True" if require_iroh else ""
|
env["CHATMAIL_REQUIRE_IROH"] = "True" if require_iroh else ""
|
||||||
|
if not args.dns_check_disabled:
|
||||||
|
env["CHATMAIL_ADDR_V4"] = remote_data.get("A") or ""
|
||||||
|
env["CHATMAIL_ADDR_V6"] = remote_data.get("AAAA") or ""
|
||||||
deploy_path = importlib.resources.files(__package__).joinpath("run.py").resolve()
|
deploy_path = importlib.resources.files(__package__).joinpath("run.py").resolve()
|
||||||
pyinf = "pyinfra --dry" if args.dry_run else "pyinfra"
|
pyinf = "pyinfra --dry" if args.dry_run else "pyinfra"
|
||||||
|
|
||||||
@@ -118,11 +119,7 @@ def run_cmd(args, out):
|
|||||||
out.check_call(cmd, env=env)
|
out.check_call(cmd, env=env)
|
||||||
if args.website_only:
|
if args.website_only:
|
||||||
out.green("Website deployment completed.")
|
out.green("Website deployment completed.")
|
||||||
elif (
|
elif not args.dns_check_disabled and strict_tls and not remote_data["acme_account_url"]:
|
||||||
not args.dns_check_disabled
|
|
||||||
and strict_tls
|
|
||||||
and not remote_data["acme_account_url"]
|
|
||||||
):
|
|
||||||
out.red("Deploy completed but letsencrypt not configured")
|
out.red("Deploy completed but letsencrypt not configured")
|
||||||
out.red("Run 'cmdeploy run' again")
|
out.red("Run 'cmdeploy run' again")
|
||||||
else:
|
else:
|
||||||
@@ -197,6 +194,12 @@ def status_cmd(args, out):
|
|||||||
|
|
||||||
|
|
||||||
def test_cmd_options(parser):
|
def test_cmd_options(parser):
|
||||||
|
parser.add_argument(
|
||||||
|
"--slow",
|
||||||
|
dest="slow",
|
||||||
|
action="store_true",
|
||||||
|
help="also run slow tests",
|
||||||
|
)
|
||||||
add_ssh_host_option(parser)
|
add_ssh_host_option(parser)
|
||||||
|
|
||||||
|
|
||||||
@@ -218,6 +221,8 @@ def test_cmd(args, out):
|
|||||||
"-v",
|
"-v",
|
||||||
"--durations=5",
|
"--durations=5",
|
||||||
]
|
]
|
||||||
|
if args.slow:
|
||||||
|
pytest_args.append("--slow")
|
||||||
ret = out.run_ret(pytest_args, env=env)
|
ret = out.run_ret(pytest_args, env=env)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|||||||
@@ -591,17 +591,11 @@ def deploy_chatmail(config_path: Path, disable_mail: bool, website_only: bool) -
|
|||||||
return
|
return
|
||||||
|
|
||||||
# Check if mtail_address interface is available (if configured)
|
# Check if mtail_address interface is available (if configured)
|
||||||
if config.mtail_address and config.mtail_address not in (
|
if config.mtail_address and config.mtail_address not in ('127.0.0.1', '::1', 'localhost'):
|
||||||
"127.0.0.1",
|
|
||||||
"::1",
|
|
||||||
"localhost",
|
|
||||||
):
|
|
||||||
ipv4_addrs = host.get_fact(hardware.Ipv4Addrs)
|
ipv4_addrs = host.get_fact(hardware.Ipv4Addrs)
|
||||||
all_addresses = [addr for addrs in ipv4_addrs.values() for addr in addrs]
|
all_addresses = [addr for addrs in ipv4_addrs.values() for addr in addrs]
|
||||||
if config.mtail_address not in all_addresses:
|
if config.mtail_address not in all_addresses:
|
||||||
Out().red(
|
Out().red(f"Deploy failed: mtail_address {config.mtail_address} is not available (VPN up?).\n")
|
||||||
f"Deploy failed: mtail_address {config.mtail_address} is not available (VPN up?).\n"
|
|
||||||
)
|
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
if not is_in_container():
|
if not is_in_container():
|
||||||
|
|||||||
@@ -20,30 +20,12 @@ DOVECOT_ARCHIVE_VERSION = "2.3.21+dfsg1-3"
|
|||||||
DOVECOT_PACKAGE_VERSION = f"1:{DOVECOT_ARCHIVE_VERSION}"
|
DOVECOT_PACKAGE_VERSION = f"1:{DOVECOT_ARCHIVE_VERSION}"
|
||||||
|
|
||||||
DOVECOT_SHA256 = {
|
DOVECOT_SHA256 = {
|
||||||
(
|
("core", "amd64"): "dd060706f52a306fa863d874717210b9fe10536c824afe1790eec247ded5b27d",
|
||||||
"core",
|
("core", "arm64"): "e7548e8a82929722e973629ecc40fcfa886894cef3db88f23535149e7f730dc9",
|
||||||
"amd64",
|
("imapd", "amd64"): "8d8dc6fc00bbb6cdb25d345844f41ce2f1c53f764b79a838eb2a03103eebfa86",
|
||||||
): "dd060706f52a306fa863d874717210b9fe10536c824afe1790eec247ded5b27d",
|
("imapd", "arm64"): "178fa877ddd5df9930e8308b518f4b07df10e759050725f8217a0c1fb3fd707f",
|
||||||
(
|
("lmtpd", "amd64"): "2f69ba5e35363de50962d42cccbfe4ed8495265044e244007d7ccddad77513ab",
|
||||||
"core",
|
("lmtpd", "arm64"): "89f52fb36524f5877a177dff4a713ba771fd3f91f22ed0af7238d495e143b38f",
|
||||||
"arm64",
|
|
||||||
): "e7548e8a82929722e973629ecc40fcfa886894cef3db88f23535149e7f730dc9",
|
|
||||||
(
|
|
||||||
"imapd",
|
|
||||||
"amd64",
|
|
||||||
): "8d8dc6fc00bbb6cdb25d345844f41ce2f1c53f764b79a838eb2a03103eebfa86",
|
|
||||||
(
|
|
||||||
"imapd",
|
|
||||||
"arm64",
|
|
||||||
): "178fa877ddd5df9930e8308b518f4b07df10e759050725f8217a0c1fb3fd707f",
|
|
||||||
(
|
|
||||||
"lmtpd",
|
|
||||||
"amd64",
|
|
||||||
): "2f69ba5e35363de50962d42cccbfe4ed8495265044e244007d7ccddad77513ab",
|
|
||||||
(
|
|
||||||
"lmtpd",
|
|
||||||
"arm64",
|
|
||||||
): "89f52fb36524f5877a177dff4a713ba771fd3f91f22ed0af7238d495e143b38f",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -79,7 +61,11 @@ class DovecotDeployer(Deployer):
|
|||||||
self.need_restart = True
|
self.need_restart = True
|
||||||
files.put(
|
files.put(
|
||||||
name="Pin dovecot packages to block Debian dist-upgrades",
|
name="Pin dovecot packages to block Debian dist-upgrades",
|
||||||
src=io.StringIO("Package: dovecot-*\nPin: version *\nPin-Priority: -1\n"),
|
src=io.StringIO(
|
||||||
|
"Package: dovecot-*\n"
|
||||||
|
"Pin: version *\n"
|
||||||
|
"Pin-Priority: -1\n"
|
||||||
|
),
|
||||||
dest="/etc/apt/preferences.d/pin-dovecot",
|
dest="/etc/apt/preferences.d/pin-dovecot",
|
||||||
user="root",
|
user="root",
|
||||||
group="root",
|
group="root",
|
||||||
@@ -98,7 +84,7 @@ class DovecotDeployer(Deployer):
|
|||||||
if not self.disable_mail and not self.need_restart:
|
if not self.disable_mail and not self.need_restart:
|
||||||
stale = host.get_fact(
|
stale = host.get_fact(
|
||||||
Command,
|
Command,
|
||||||
"pid=$(systemctl show -p MainPID --value dovecot.service 2>/dev/null);"
|
'pid=$(systemctl show -p MainPID --value dovecot.service 2>/dev/null);'
|
||||||
' [ "${pid:-0}" != "0" ] && readlink "/proc/$pid/exe" 2>/dev/null | grep -q "(deleted)"'
|
' [ "${pid:-0}" != "0" ] && readlink "/proc/$pid/exe" 2>/dev/null | grep -q "(deleted)"'
|
||||||
" && echo STALE || true",
|
" && echo STALE || true",
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -149,26 +149,12 @@ plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
plugin {
|
plugin {
|
||||||
|
# for now we define static quota-rules for all users
|
||||||
quota = maildir:User quota
|
quota = maildir:User quota
|
||||||
|
quota_rule = *:storage={{ config.max_mailbox_size }}
|
||||||
quota_max_mail_size={{ config.max_message_size }}
|
quota_max_mail_size={{ config.max_message_size }}
|
||||||
quota_grace = 0
|
quota_grace = 0
|
||||||
|
# quota_over_flag_value = TRUE
|
||||||
quota_rule = *:storage={{ config.max_mailbox_size_mb }}M
|
|
||||||
|
|
||||||
# Trigger at 75%% of quota, expire oldest messages down to 70%%.
|
|
||||||
# The percentages are chosen to prevent current Delta Chat users
|
|
||||||
# from seeing "quota warnings" which trigger at 80% and 95%.
|
|
||||||
|
|
||||||
quota_warning = storage=75%% quota-warning {{ config.max_mailbox_size_mb * 70 // 100 }} {{ config.mailboxes_dir }}/%u
|
|
||||||
}
|
|
||||||
|
|
||||||
service quota-warning {
|
|
||||||
executable = script /usr/local/lib/chatmaild/venv/bin/chatmail-quota-expire
|
|
||||||
user = vmail
|
|
||||||
unix_listener quota-warning {
|
|
||||||
user = vmail
|
|
||||||
mode = 0600
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# push_notification configuration
|
# push_notification configuration
|
||||||
|
|||||||
@@ -78,11 +78,3 @@ counter rejected_unencrypted_mail_count
|
|||||||
/Rejected unencrypted mail/ {
|
/Rejected unencrypted mail/ {
|
||||||
rejected_unencrypted_mail_count++
|
rejected_unencrypted_mail_count++
|
||||||
}
|
}
|
||||||
|
|
||||||
counter quota_expire_runs
|
|
||||||
counter quota_expire_removed_files
|
|
||||||
|
|
||||||
/quota-expire: removed (?P<count>\d+) message\(s\)/ {
|
|
||||||
quota_expire_runs++
|
|
||||||
quota_expire_removed_files += $count
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -69,6 +69,15 @@ mynetworks = 127.0.0.0/8
|
|||||||
{% else %}
|
{% else %}
|
||||||
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
|
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{% if config.addr_v4 %}
|
||||||
|
smtp_bind_address = {{ config.addr_v4 }}
|
||||||
|
{% endif %}
|
||||||
|
{% if config.addr_v6 %}
|
||||||
|
smtp_bind_address6 = {{ config.addr_v6 }}
|
||||||
|
{% endif %}
|
||||||
|
{% if config.addr_v4 or config.addr_v6 %}
|
||||||
|
smtp_bind_address_enforce = yes
|
||||||
|
{% endif %}
|
||||||
mailbox_size_limit = 0
|
mailbox_size_limit = 0
|
||||||
message_size_limit = {{config.max_message_size}}
|
message_size_limit = {{config.max_message_size}}
|
||||||
recipient_delimiter = +
|
recipient_delimiter = +
|
||||||
|
|||||||
@@ -12,27 +12,15 @@ def openssl_selfsigned_args(domain, cert_path, key_path, days=36500):
|
|||||||
``www.<domain>`` and ``mta-sts.<domain>``.
|
``www.<domain>`` and ``mta-sts.<domain>``.
|
||||||
"""
|
"""
|
||||||
return [
|
return [
|
||||||
"openssl",
|
"openssl", "req", "-x509",
|
||||||
"req",
|
"-newkey", "ec", "-pkeyopt", "ec_paramgen_curve:P-256",
|
||||||
"-x509",
|
"-noenc", "-days", str(days),
|
||||||
"-newkey",
|
"-keyout", str(key_path),
|
||||||
"ec",
|
"-out", str(cert_path),
|
||||||
"-pkeyopt",
|
"-subj", f"/CN={domain}",
|
||||||
"ec_paramgen_curve:P-256",
|
|
||||||
"-noenc",
|
|
||||||
"-days",
|
|
||||||
str(days),
|
|
||||||
"-keyout",
|
|
||||||
str(key_path),
|
|
||||||
"-out",
|
|
||||||
str(cert_path),
|
|
||||||
"-subj",
|
|
||||||
f"/CN={domain}",
|
|
||||||
# Mark as end-entity cert so it cannot be used as a CA to sign others.
|
# Mark as end-entity cert so it cannot be used as a CA to sign others.
|
||||||
"-addext",
|
"-addext", "basicConstraints=critical,CA:FALSE",
|
||||||
"basicConstraints=critical,CA:FALSE",
|
"-addext", "extendedKeyUsage=serverAuth,clientAuth",
|
||||||
"-addext",
|
|
||||||
"extendedKeyUsage=serverAuth,clientAuth",
|
|
||||||
"-addext",
|
"-addext",
|
||||||
f"subjectAltName=DNS:{domain},DNS:www.{domain},DNS:mta-sts.{domain}",
|
f"subjectAltName=DNS:{domain},DNS:www.{domain},DNS:mta-sts.{domain}",
|
||||||
]
|
]
|
||||||
@@ -54,9 +42,7 @@ class SelfSignedTlsDeployer(Deployer):
|
|||||||
|
|
||||||
def configure(self):
|
def configure(self):
|
||||||
args = openssl_selfsigned_args(
|
args = openssl_selfsigned_args(
|
||||||
self.mail_domain,
|
self.mail_domain, self.cert_path, self.key_path,
|
||||||
self.cert_path,
|
|
||||||
self.key_path,
|
|
||||||
)
|
)
|
||||||
cmd = shlex.join(args)
|
cmd = shlex.join(args)
|
||||||
server.shell(
|
server.shell(
|
||||||
|
|||||||
@@ -30,15 +30,12 @@ def test_newemail_configure(maildomain, rpc, chatmail_config):
|
|||||||
# set_config_from_qr, so fetch credentials via requests instead
|
# set_config_from_qr, so fetch credentials via requests instead
|
||||||
res = requests.post(f"https://{maildomain}/new", verify=False)
|
res = requests.post(f"https://{maildomain}/new", verify=False)
|
||||||
data = res.json()
|
data = res.json()
|
||||||
rpc.add_or_update_transport(
|
rpc.add_or_update_transport(account_id, {
|
||||||
account_id,
|
"addr": data["email"],
|
||||||
{
|
"password": data["password"],
|
||||||
"addr": data["email"],
|
"imapServer": maildomain,
|
||||||
"password": data["password"],
|
"smtpServer": maildomain,
|
||||||
"imapServer": maildomain,
|
"certificateChecks": "acceptInvalidCertificates",
|
||||||
"smtpServer": maildomain,
|
})
|
||||||
"certificateChecks": "acceptInvalidCertificates",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
rpc.add_transport_from_qr(account_id, url)
|
rpc.add_transport_from_qr(account_id, url)
|
||||||
|
|||||||
@@ -221,6 +221,7 @@ def test_rewrite_subject(cmsetup, maildata):
|
|||||||
assert "Subject: Unencrypted subject" not in rcvd_msg
|
assert "Subject: Unencrypted subject" not in rcvd_msg
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.slow
|
||||||
def test_exceed_rate_limit(cmsetup, gencreds, maildata, chatmail_config):
|
def test_exceed_rate_limit(cmsetup, gencreds, maildata, chatmail_config):
|
||||||
"""Test that the per-account send-mail limit is exceeded."""
|
"""Test that the per-account send-mail limit is exceeded."""
|
||||||
user1, user2 = cmsetup.gen_users(2)
|
user1, user2 = cmsetup.gen_users(2)
|
||||||
@@ -243,6 +244,7 @@ def test_exceed_rate_limit(cmsetup, gencreds, maildata, chatmail_config):
|
|||||||
pytest.fail("Rate limit was not exceeded")
|
pytest.fail("Rate limit was not exceeded")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.slow
|
||||||
def test_expunged(remote, chatmail_config):
|
def test_expunged(remote, chatmail_config):
|
||||||
outdated_days = int(chatmail_config.delete_mails_after) + 1
|
outdated_days = int(chatmail_config.delete_mails_after) + 1
|
||||||
find_cmds = [
|
find_cmds = [
|
||||||
|
|||||||
@@ -23,6 +23,12 @@ def _is_ip(domain):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_addoption(parser):
|
||||||
|
parser.addoption(
|
||||||
|
"--slow", action="store_true", default=False, help="also run slow tests"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def pytest_configure(config):
|
def pytest_configure(config):
|
||||||
config._benchresults = {}
|
config._benchresults = {}
|
||||||
config.addinivalue_line(
|
config.addinivalue_line(
|
||||||
@@ -30,6 +36,13 @@ def pytest_configure(config):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_runtest_setup(item):
|
||||||
|
markers = list(item.iter_markers(name="slow"))
|
||||||
|
if markers:
|
||||||
|
if not item.config.getoption("--slow"):
|
||||||
|
pytest.skip("skipping slow test, use --slow to run")
|
||||||
|
|
||||||
|
|
||||||
def _get_chatmail_config():
|
def _get_chatmail_config():
|
||||||
inipath = os.environ.get("CHATMAIL_INI")
|
inipath = os.environ.get("CHATMAIL_INI")
|
||||||
if inipath:
|
if inipath:
|
||||||
@@ -417,12 +430,9 @@ class Remote:
|
|||||||
getjournal = "journalctl -f" if not logcmd else logcmd
|
getjournal = "journalctl -f" if not logcmd else logcmd
|
||||||
print(self.sshdomain)
|
print(self.sshdomain)
|
||||||
match self.sshdomain:
|
match self.sshdomain:
|
||||||
case "@local":
|
case "@local": command = []
|
||||||
command = []
|
case "localhost": command = []
|
||||||
case "localhost":
|
case _: command = ["ssh", f"root@{self.sshdomain}"]
|
||||||
command = []
|
|
||||||
case _:
|
|
||||||
command = ["ssh", f"root@{self.sshdomain}"]
|
|
||||||
[command.append(arg) for arg in getjournal.split()]
|
[command.append(arg) for arg in getjournal.split()]
|
||||||
popen = subprocess.Popen(
|
popen = subprocess.Popen(
|
||||||
command,
|
command,
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ def make_host(*fact_pairs):
|
|||||||
if cls not in facts:
|
if cls not in facts:
|
||||||
registered = ", ".join(c.__name__ for c in facts)
|
registered = ", ".join(c.__name__ for c in facts)
|
||||||
raise LookupError(
|
raise LookupError(
|
||||||
f"unexpected get_fact({cls.__name__}); only registered: {registered}"
|
f"unexpected get_fact({cls.__name__}); "
|
||||||
|
f"only registered: {registered}"
|
||||||
)
|
)
|
||||||
return facts[cls]
|
return facts[cls]
|
||||||
|
|
||||||
|
|||||||
@@ -4,14 +4,12 @@
|
|||||||
|
|
||||||
You can use the `make` command and `make html` to build web pages.
|
You can use the `make` command and `make html` to build web pages.
|
||||||
|
|
||||||
You need a Python environment with `sphinx` and other
|
You need a Python environment where the following install was excuted:
|
||||||
dependencies, you can create it by running `scripts/initenv.sh`
|
|
||||||
from the repository root.
|
pip install furo sphinx-autobuild
|
||||||
|
|
||||||
To develop/change documentation, you can then do:
|
To develop/change documentation, you can then do:
|
||||||
|
|
||||||
. venv/bin/activate
|
|
||||||
cd doc
|
|
||||||
make auto
|
make auto
|
||||||
|
|
||||||
A page will open at https://127.0.0.1:8000/ serving the docs and it will
|
A page will open at https://127.0.0.1:8000/ serving the docs and it will
|
||||||
|
|||||||
@@ -16,6 +16,5 @@ Contributions and feedback welcome through the https://github.com/chatmail/relay
|
|||||||
proxy
|
proxy
|
||||||
migrate
|
migrate
|
||||||
overview
|
overview
|
||||||
reverse_dns
|
|
||||||
related
|
related
|
||||||
faq
|
faq
|
||||||
|
|||||||
@@ -102,12 +102,8 @@ short overview of ``chatmaild`` services:
|
|||||||
Apple/Google/Huawei.
|
Apple/Google/Huawei.
|
||||||
|
|
||||||
- `chatmail-expire <https://github.com/chatmail/relay/blob/main/chatmaild/src/chatmaild/expire.py>`_
|
- `chatmail-expire <https://github.com/chatmail/relay/blob/main/chatmaild/src/chatmaild/expire.py>`_
|
||||||
deletes old messages, large messages, and entire mailboxes
|
deletes users if they have not logged in for a longer while.
|
||||||
of users who have not logged in for longer than
|
The timeframe can be configured in ``chatmail.ini``.
|
||||||
``delete_inactive_users_after`` days.
|
|
||||||
|
|
||||||
- ``chatmail-quota-expire`` is called by Dovecot's ``quota_warning`` mechanism
|
|
||||||
and will automatically remove oldest messages to keep mailboxes well under ``max_mailbox_size``.
|
|
||||||
|
|
||||||
- `lastlogin <https://github.com/chatmail/relay/blob/main/chatmaild/src/chatmaild/lastlogin.py>`_
|
- `lastlogin <https://github.com/chatmail/relay/blob/main/chatmaild/src/chatmaild/lastlogin.py>`_
|
||||||
is contacted by Dovecot when a user logs in and stores the date of
|
is contacted by Dovecot when a user logs in and stores the date of
|
||||||
@@ -160,8 +156,6 @@ Chatmail relay dependency diagram
|
|||||||
/home/vmail/.../user"];
|
/home/vmail/.../user"];
|
||||||
dovecot --- |lastlogin.socket|lastlogin;
|
dovecot --- |lastlogin.socket|lastlogin;
|
||||||
dovecot --- chatmail-metadata;
|
dovecot --- chatmail-metadata;
|
||||||
dovecot --- |quota-warning|chatmail-quota-expire;
|
|
||||||
chatmail-quota-expire --- maildir;
|
|
||||||
lastlogin --- maildir;
|
lastlogin --- maildir;
|
||||||
doveauth --- maildir;
|
doveauth --- maildir;
|
||||||
chatmail-expire-daily --- maildir;
|
chatmail-expire-daily --- maildir;
|
||||||
|
|||||||
@@ -1,64 +0,0 @@
|
|||||||
Configuring reverse DNS
|
|
||||||
=======================
|
|
||||||
|
|
||||||
Some email servers reject the emails
|
|
||||||
if they don't pass `FCrDNS`_ check, also known as `iprev`_ check.
|
|
||||||
|
|
||||||
.. _FCrDNS: https://en.wikipedia.org/wiki/Forward-confirmed_reverse_DNS
|
|
||||||
.. _iprev: https://datatracker.ietf.org/doc/html/rfc8601#section-3
|
|
||||||
|
|
||||||
Passing the check requires that the IP address that email is sent from
|
|
||||||
should have a ``PTR`` record pointing to the domain name of the server,
|
|
||||||
and domain name record should have an ``A/AAAA`` record
|
|
||||||
pointing to the IP address.
|
|
||||||
|
|
||||||
Modern email relies on DKIM and SPF for authentication,
|
|
||||||
while iprev check exists for
|
|
||||||
`historical reasons <https://datatracker.ietf.org/doc/html/draft-ietf-dnsop-reverse-mapping-considerations-06#section-2.1>`_.
|
|
||||||
Chatmail relays don't resolve ``PTR`` records,
|
|
||||||
so you can ignore this section if configuring ``PTR`` records
|
|
||||||
is difficult and federation with legacy email servers that don't accept
|
|
||||||
valid DKIM signature for authentication is not important.
|
|
||||||
|
|
||||||
Multi-homed setups
|
|
||||||
------------------
|
|
||||||
|
|
||||||
If you have a server with multiple IP addresses,
|
|
||||||
also known as multi-homed setup,
|
|
||||||
and don't publish all IP addresses in DNS,
|
|
||||||
you need to make sure you are using
|
|
||||||
the published address when making outgoing connections.
|
|
||||||
|
|
||||||
For example, your server may have a static IP
|
|
||||||
address, and a so-called Floating IP or Virtual IP
|
|
||||||
that can be moved between servers in case of
|
|
||||||
migration or for failover.
|
|
||||||
By using Floating IP you can avoid downtime
|
|
||||||
and keep the IP address reputation
|
|
||||||
for destinatinons that rely on IP reputation and IP blocklists.
|
|
||||||
In this case you will only publish
|
|
||||||
the Floating IP to DNS and only use the static IP
|
|
||||||
to SSH into the server.
|
|
||||||
|
|
||||||
If you have such setup, make sure that
|
|
||||||
you not only set ``PTR`` records for the Floating IP,
|
|
||||||
but make outgoing connections using the Floating IP.
|
|
||||||
Otherwise reverse DNS check succeed,
|
|
||||||
but forward check making sure your domain name points
|
|
||||||
to the IP address will fail.
|
|
||||||
Such setup is indistinguishable from someone
|
|
||||||
setting IP address ``PTR`` with the domain they don't own
|
|
||||||
and as a result don't succeed.
|
|
||||||
|
|
||||||
On Linux you can configure source IP address with ``ip route`` command,
|
|
||||||
for example:
|
|
||||||
::
|
|
||||||
|
|
||||||
ip route change default via <default-gateway> dev eth0 src <source-address>
|
|
||||||
|
|
||||||
Make sure to persist the change after verifying it is working.
|
|
||||||
You can check what your outgoing IP address is
|
|
||||||
with ``curl icanhazip.com``.
|
|
||||||
Check both the IPv4 and IPv6 addresses.
|
|
||||||
For IPv4 address use ``curl ipv4.icanhazip.com`` or ``curl -4 icanhazip.com``
|
|
||||||
and similarly for IPv6 if you have it.
|
|
||||||
@@ -18,8 +18,15 @@ if command -v lsb_release 2>&1 >/dev/null; then
|
|||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
|
|
||||||
python3 -m venv --upgrade-deps venv
|
if command -v uv >/dev/null 2>&1; then
|
||||||
|
echo "Using uv for faster environment setup..."
|
||||||
venv/bin/pip install -e chatmaild
|
uv venv venv
|
||||||
venv/bin/pip install -e cmdeploy
|
uv pip install --python venv/bin/python -e chatmaild
|
||||||
venv/bin/pip install sphinx sphinxcontrib-mermaid sphinx-autobuild furo # for building the docs
|
uv pip install --python venv/bin/python -e cmdeploy
|
||||||
|
uv pip install --python venv/bin/python sphinx sphinxcontrib-mermaid sphinx-autobuild furo
|
||||||
|
else
|
||||||
|
python3 -m venv --upgrade-deps venv
|
||||||
|
venv/bin/pip install -e chatmaild
|
||||||
|
venv/bin/pip install -e cmdeploy
|
||||||
|
venv/bin/pip install sphinx sphinxcontrib-mermaid sphinx-autobuild furo
|
||||||
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user