diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md new file mode 100644 index 0000000..a4191ca --- /dev/null +++ b/ARCHITECTURE.md @@ -0,0 +1,50 @@ +This diagram shows components of the chatmail server; this is a draft +overview as of mid-August 2025: + +```mermaid +graph LR; + cmdeploy --- sshd; + letsencrypt --- |80|acmetool-redirector; + acmetool-redirector --- |443|nginx-right(["`nginx + (external)`"]); + nginx-external --- |465|postfix; + nginx-external(["`nginx + (external)`"]) --- |8443|nginx-internal["`nginx + (internal)`"]; + nginx-internal --- website["`Website + /var/www/html`"]; + nginx-internal --- newemail.py; + nginx-internal --- autoconfig.xml; + certs-nginx[("`TLS certs + /var/lib/acme`")] --> nginx-internal; + cron --- chatmail-metrics; + cron --- acmetool; + cron --- expunge; + chatmail-metrics --- website; + acmetool --> certs[("`TLS certs + /var/lib/acme`")]; + nginx-external --- |993|dovecot; + autoconfig.xml --- postfix; + autoconfig.xml --- dovecot; + postfix --- echobot; + postfix --- |10080,10081|filtermail; + postfix --- users["`User data + home/vmail/mail`"]; + postfix --- |doveauth.socket|doveauth; + dovecot --- |doveauth.socket|doveauth; + dovecot --- users; + dovecot --- |metadata.socket|chatmail-metadata; + doveauth --- users; + expunge --- users; + chatmail-metadata --- iroh-relay; + certs-nginx --> postfix; + certs-nginx --> dovecot; + style certs fill:#ff6; + style certs-nginx fill:#ff6; + style nginx-external fill:#fc9; + style nginx-right fill:#fc9; +``` + +The edges in this graph should not be taken too literally; they +reflect some sort of communication path or dependency relationship +between components of the chatmail server. diff --git a/CHANGELOG.md b/CHANGELOG.md index e754da0..da339af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## untagged +- Make www upload path configurable + ([#618](https://github.com/chatmail/relay/pull/618)) + +- Check whether GCC is installed in initenv.sh + ([#608](https://github.com/chatmail/relay/pull/608)) + - Expire push notification tokens after 90 days ([#583](https://github.com/chatmail/relay/pull/583)) @@ -26,6 +32,9 @@ - filtermail: respect config message size limit ([#572](https://github.com/chatmail/relay/pull/572)) +- Don't deploy if one of the ports used for chatmail relay services is occupied by an unexpected process + ([#568](https://github.com/chatmail/relay/pull/568)) + - Add config value after how many days large files are deleted ([#555](https://github.com/chatmail/relay/pull/555)) diff --git a/README.md b/README.md index 1fd6a92..b68d1be 100644 --- a/README.md +++ b/README.md @@ -262,6 +262,18 @@ This starts a local live development cycle for chatmail web pages: - Starts a browser window automatically where you can "refresh" as needed. +#### Custom web pages + +You can skip uploading a web page +by setting `www_folder=disabled` in `chatmail.ini`. + +If you want to manage your web pages outside this git repository, +you can set `www_folder` in `chatmail.ini` to a custom directory on your computer. +`cmdeploy run` will upload it as the server's home page, +and if it contains a `src/index.md` file, +will build it with hugo. + + ## Mailbox directory layout Fresh chatmail addresses have a mailbox directory that contains: @@ -548,3 +560,6 @@ Here are some related projects that you may be interested in: progress](https://github.com/mjl-/mox/issues/251) to modify it to support all of the features and configuration settings required to operate as a chatmail relay. +- [Maddy-Chatmail](https://github.com/sadraiiali/maddy_chatmail): a plugin for the + [Maddy email server](https://maddy.email/) which aims to implement the + chatmail relay features and configuration options. diff --git a/chatmaild/src/chatmaild/config.py b/chatmaild/src/chatmaild/config.py index 0cac3ea..ae5f442 100644 --- a/chatmaild/src/chatmaild/config.py +++ b/chatmaild/src/chatmaild/config.py @@ -33,6 +33,7 @@ class Config: self.password_min_length = int(params["password_min_length"]) self.passthrough_senders = params["passthrough_senders"].split() self.passthrough_recipients = params["passthrough_recipients"].split() + self.www_folder = params.get("www_folder", "") self.filtermail_smtp_port = int(params["filtermail_smtp_port"]) self.filtermail_smtp_port_incoming = int( params["filtermail_smtp_port_incoming"] diff --git a/chatmaild/src/chatmaild/ini/chatmail.ini.f b/chatmaild/src/chatmaild/ini/chatmail.ini.f index a99fb50..4fbce46 100644 --- a/chatmaild/src/chatmaild/ini/chatmail.ini.f +++ b/chatmaild/src/chatmaild/ini/chatmail.ini.f @@ -43,7 +43,7 @@ passthrough_senders = # list of e-mail recipients for which to accept outbound un-encrypted mails # (space-separated, item may start with "@" to whitelist whole recipient domains) -passthrough_recipients = xstore@testrun.org echo@{mail_domain} +passthrough_recipients = # # Deployment Details diff --git a/chatmaild/src/chatmaild/user.py b/chatmaild/src/chatmaild/user.py index 934eb51..28ba4d8 100644 --- a/chatmaild/src/chatmaild/user.py +++ b/chatmaild/src/chatmaild/user.py @@ -58,8 +58,8 @@ class User: if not self.addr.startswith("echo@"): logging.error(f"could not write password for: {self.addr}") raise - if not self.addr.startswith("echo@"): - self.enforce_E2EE_path.touch() + # if not self.addr.startswith("echo@"): + self.enforce_E2EE_path.touch() def set_last_login_timestamp(self, timestamp): """Track login time with daily granularity diff --git a/cmdeploy/src/cmdeploy/__init__.py b/cmdeploy/src/cmdeploy/__init__.py index e07f041..f8ad83b 100644 --- a/cmdeploy/src/cmdeploy/__init__.py +++ b/cmdeploy/src/cmdeploy/__init__.py @@ -11,7 +11,7 @@ from io import StringIO from pathlib import Path from chatmaild.config import Config, read_config -from pyinfra import facts, host +from pyinfra import facts, host, logger from pyinfra.api import FactBase from pyinfra.facts.files import File from pyinfra.facts.server import Sysctl @@ -609,20 +609,27 @@ def deploy_iroh_relay(config) -> None: def deploy_website(config_path: Path) -> None: + from .www import build_webpages, get_paths + config = read_config(config_path) check_config(config) mail_domain = config.mail_domain - www_path = importlib.resources.files(__package__).joinpath("../../../www").resolve() + www_path, src_dir, build_dir = get_paths(config) if mail_domain == "arcanechat.me": subprocess.check_output(["pnpm", "build"], cwd=www_path.joinpath("arcanechat")) build_dir = www_path.joinpath("arcanechat/dist") + files.rsync(f"{build_dir}/", "/var/www/html", flags=["-avz"]) else: - from .www import build_webpages - - build_dir = www_path.joinpath("build") - build_webpages(www_path.joinpath("src"), build_dir, config) - files.rsync(f"{build_dir}/", "/var/www/html", flags=["-avz"]) + # if www_folder was set to a non-existing folder, skip upload + if not www_path.is_dir(): + logger.warning("Building web pages is disabled in chatmail.ini, skipping") + else: + # if www_folder is a hugo page, build it + if build_dir: + www_path = build_webpages(src_dir, build_dir, config) + # if it is not a hugo page, upload it as is + files.rsync(f"{www_path}/", "/var/www/html", flags=["-avz"]) def deploy_chatmail(config_path: Path, disable_mail: bool) -> None: @@ -690,10 +697,32 @@ def deploy_chatmail(config_path: Path, disable_mail: bool) -> None: # to use 127.0.0.1 as the resolver. from cmdeploy.cmdeploy import Out - process_on_53 = host.get_fact(Port, port=53) - if process_on_53 not in (None, "unbound"): - Out().red(f"Can't install unbound: port 53 is occupied by: {process_on_53}") - exit(1) + port_services = [ + (["master", "smtpd"], 25), + ("unbound", 53), + ("acmetool", 80), + ("imap-login", 143), + ("nginx", 443), + (["master", "smtpd"], 465), + (["master", "smtpd"], 587), + ("imap-login", 993), + ("iroh-relay", 3340), + ("nginx", 8443), + (["master", "smtpd"], config.postfix_reinject_port), + (["master", "smtpd"], config.postfix_reinject_port_incoming), + ("filtermail", config.filtermail_smtp_port), + ("filtermail", config.filtermail_smtp_port_incoming), + ] + for service, port in port_services: + print(f"Checking if port {port} is available for {service}...") + running_service = host.get_fact(Port, port=port) + if running_service: + if running_service not in service: + Out().red( + f"Deploy failed: port {port} is occupied by: {running_service}" + ) + exit(1) + apt.packages( name="Install unbound", packages=["unbound", "unbound-anchor", "dnsutils"], @@ -746,8 +775,6 @@ def deploy_chatmail(config_path: Path, disable_mail: bool) -> None: packages=["fcgiwrap"], ) - www_path = importlib.resources.files(__package__).joinpath("../../../www").resolve() - deploy_website(config_path) _install_remote_venv_with_chatmaild(config) @@ -828,8 +855,14 @@ def deploy_chatmail(config_path: Path, disable_mail: bool) -> None: name="Ensure cron is installed", packages=["cron"], ) - git_hash = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode() - git_diff = subprocess.check_output(["git", "diff"]).decode() + try: + git_hash = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode() + except Exception: + git_hash = "unknown\n" + try: + git_diff = subprocess.check_output(["git", "diff"]).decode() + except Exception: + git_diff = "" files.put( name="Upload chatmail relay git commiit hash", src=StringIO(git_hash + git_diff), diff --git a/cmdeploy/src/cmdeploy/acmetool/__init__.py b/cmdeploy/src/cmdeploy/acmetool/__init__.py index 19a3795..28d4fd8 100644 --- a/cmdeploy/src/cmdeploy/acmetool/__init__.py +++ b/cmdeploy/src/cmdeploy/acmetool/__init__.py @@ -1,7 +1,5 @@ import importlib.resources -from pyinfra import host -from pyinfra.facts.systemd import SystemdStatus from pyinfra.operations import apt, files, server, systemd @@ -54,12 +52,6 @@ def deploy_acmetool(email="", domains=[]): group="root", mode="644", ) - if host.get_fact(SystemdStatus).get("nginx.service"): - systemd.service( - name="Stop nginx service to free port 80", - service="nginx", - running=False, - ) systemd.service( name="Setup acmetool-redirector service", diff --git a/cmdeploy/src/cmdeploy/cmdeploy.py b/cmdeploy/src/cmdeploy/cmdeploy.py index be33ffe..1d896b9 100644 --- a/cmdeploy/src/cmdeploy/cmdeploy.py +++ b/cmdeploy/src/cmdeploy/cmdeploy.py @@ -89,6 +89,16 @@ def run_cmd(args, out): try: retcode = out.check_call(cmd, env=env) if retcode == 0: + """ + print("\nYou can try out the relay by talking to this echo bot: ") + sshexec = SSHExec(args.config.mail_domain, verbose=args.verbose) + print( + sshexec( + call=remote.rshell.shell, + kwargs=dict(command="cat /var/lib/echobot/invite-link.txt"), + ) + ) + """ out.green("Deploy completed, call `cmdeploy dns` next.") elif not remote_data["acme_account_url"]: out.red("Deploy completed but letsencrypt not configured") diff --git a/cmdeploy/src/cmdeploy/tests/test_cmdeploy.py b/cmdeploy/src/cmdeploy/tests/test_cmdeploy.py index 3084c8e..bdbc3db 100644 --- a/cmdeploy/src/cmdeploy/tests/test_cmdeploy.py +++ b/cmdeploy/src/cmdeploy/tests/test_cmdeploy.py @@ -1,8 +1,10 @@ +import importlib import os import pytest from cmdeploy.cmdeploy import get_parser, main +from cmdeploy.www import get_paths @pytest.fixture(autouse=True) @@ -27,3 +29,28 @@ class TestCmdline: assert main(["init", "chat.example.org"]) == 1 out, err = capsys.readouterr() assert "path exists" in out.lower() + + +def test_www_folder(example_config, tmp_path): + reporoot = importlib.resources.files(__package__).joinpath("../../../../").resolve() + assert not example_config.www_folder + www_path, src_dir, build_dir = get_paths(example_config) + assert www_path.absolute() == reporoot.joinpath("www").absolute() + assert src_dir == reporoot.joinpath("www").joinpath("src") + assert build_dir == reporoot.joinpath("www").joinpath("build") + example_config.www_folder = "disabled" + www_path, _, _ = get_paths(example_config) + assert not www_path.is_dir() + example_config.www_folder = str(tmp_path) + www_path, src_dir, build_dir = get_paths(example_config) + assert www_path == tmp_path + assert not src_dir.exists() + assert not build_dir + src_path = tmp_path.joinpath("src") + os.mkdir(src_path) + with open(src_path / "index.md", "w") as f: + f.write("# Test") + www_path, src_dir, build_dir = get_paths(example_config) + assert www_path == tmp_path + assert src_dir == src_path + assert build_dir == tmp_path.joinpath("build") diff --git a/cmdeploy/src/cmdeploy/www.py b/cmdeploy/src/cmdeploy/www.py index 9dd404b..c013d74 100644 --- a/cmdeploy/src/cmdeploy/www.py +++ b/cmdeploy/src/cmdeploy/www.py @@ -3,6 +3,7 @@ import importlib.resources import time import traceback import webbrowser +from pathlib import Path import markdown from chatmaild.config import read_config @@ -30,9 +31,25 @@ def prepare_template(source): return render_vars, page_layout -def build_webpages(src_dir, build_dir, config): +def get_paths(config) -> (Path, Path, Path): + reporoot = importlib.resources.files(__package__).joinpath("../../../").resolve() + www_path = Path(config.www_folder) + # if www_folder was not set, use default directory + if config.www_folder == "": + www_path = reporoot.joinpath("www") + src_dir = www_path.joinpath("src") + # if www_folder is a hugo page, build it + if src_dir.joinpath("index.md").is_file(): + build_dir = www_path.joinpath("build") + # if it is not a hugo page, upload it as is + else: + build_dir = None + return www_path, src_dir, build_dir + + +def build_webpages(src_dir, build_dir, config) -> Path: try: - _build_webpages(src_dir, build_dir, config) + return _build_webpages(src_dir, build_dir, config) except Exception: print(traceback.format_exc()) @@ -106,15 +123,11 @@ def main(): config = read_config(inipath) config.webdev = True assert config.mail_domain - www_path = reporoot.joinpath("www") - src_path = www_path.joinpath("src") - stats = None - build_dir = www_path.joinpath("build") - src_dir = www_path.joinpath("src") - index_path = build_dir.joinpath("index.html") # start web page generation, open a browser and wait for changes - build_webpages(src_dir, build_dir, config) + www_path, src_path, build_dir = get_paths(config) + build_dir = build_webpages(src_path, build_dir, config) + index_path = build_dir.joinpath("index.html") webbrowser.open(str(index_path)) stats = snapshot_dir_stats(src_path) print(f"\nOpened URL: file://{index_path.resolve()}\n") @@ -135,7 +148,7 @@ def main(): changenum += 1 stats = newstats - build_webpages(src_dir, build_dir, config) + build_webpages(src_path, build_dir, config) print(f"[{changenum}] regenerated web pages at: {index_path}") print(f"URL: file://{index_path.resolve()}\n\n") count = 0 diff --git a/scripts/initenv.sh b/scripts/initenv.sh index b4dab1b..db70250 100755 --- a/scripts/initenv.sh +++ b/scripts/initenv.sh @@ -9,6 +9,11 @@ if command -v lsb_release 2>&1 >/dev/null; then echo "You need to install python3-dev for installing the other dependencies." exit 1 fi + if ! gcc --version 2>&1 >/dev/null + then + echo "You need to install gcc for building Python dependencies." + exit 1 + fi ;; esac fi