diff --git a/deploy/systemd/coulobre-mail-apply.socket b/deploy/systemd/coulobre-mail-apply.socket new file mode 100644 index 0000000..af52628 --- /dev/null +++ b/deploy/systemd/coulobre-mail-apply.socket @@ -0,0 +1,14 @@ +[Unit] +Description=Coulobre local mailbox provisioning socket + +[Socket] +ListenStream=/run/coulobre-mail-apply.sock +SocketUser=root +SocketGroup=coulobre-mail-provisioning +SocketMode=0660 +Accept=yes +MaxConnections=4 +RemoveOnStop=true + +[Install] +WantedBy=sockets.target diff --git a/deploy/systemd/coulobre-mail-apply@.service b/deploy/systemd/coulobre-mail-apply@.service new file mode 100644 index 0000000..d092357 --- /dev/null +++ b/deploy/systemd/coulobre-mail-apply@.service @@ -0,0 +1,20 @@ +[Unit] +Description=Coulobre local mailbox provisioning request +After=postfix.service dovecot.service +CollectMode=inactive-or-failed + +[Service] +Type=exec +User=root +WorkingDirectory=/opt/coulobre +StateDirectory=coulobre/mail-node +StateDirectoryMode=0700 +UMask=0022 +ExecStart=/opt/coulobre/bin/coulobre-mail-node apply-request +StandardInput=socket +StandardOutput=socket +StandardError=journal +RuntimeMaxSec=3min +NoNewPrivileges=true +PrivateTmp=true +ProtectHome=true diff --git a/docs/development.md b/docs/development.md index d257c6a..038fb0d 100644 --- a/docs/development.md +++ b/docs/development.md @@ -414,6 +414,27 @@ installation needs attention. Exit code `1` indicates an invalid manifest or an invocation error such as running the check without root privileges. +### Enable local mailbox provisioning + +On a mail node with the same Postfix and Dovecot setup, install Coulobre in a +root-owned virtual environment at `/opt/coulobre` and enable the provisioning +socket. + +```sh +getent group coulobre-mail-provisioning >/dev/null || sudo groupadd --system coulobre-mail-provisioning +sudo install -m 0644 deploy/systemd/coulobre-mail-apply.socket \ + deploy/systemd/coulobre-mail-apply@.service /etc/systemd/system/ +sudo systemctl daemon-reload +sudo systemctl enable --now coulobre-mail-apply.socket +``` + +Grant the socket group only to the service identity responsible for mailbox +provisioning. Send one version 1 mailbox manifest as UTF-8 JSON to +`/run/coulobre-mail-apply.sock`, with a limit of 4 KiB. Shut down the writing +half of the connection and read the JSON reply to EOF. For a valid manifest, +the reply identifies the mailbox and reports `configured` or `failed`. +The generated password stays in a root-only credentials file on the mail node. + ## Inspect a DirectAdmin backup Use an account archive from DirectAdmin's Site Backup, Manage User Backups diff --git a/src/coulobre/hosting/test_mail_node_apply.py b/src/coulobre/hosting/test_mail_node_apply.py index fcc9ea5..dc0e060 100644 --- a/src/coulobre/hosting/test_mail_node_apply.py +++ b/src/coulobre/hosting/test_mail_node_apply.py @@ -2,7 +2,7 @@ import json import os import stat from contextlib import redirect_stdout -from io import StringIO +from io import BytesIO, StringIO from pathlib import Path from subprocess import CompletedProcess from tempfile import TemporaryDirectory @@ -21,6 +21,42 @@ from coulobre.mail_node_apply import ( ) +class MailboxApplyRequestTests(SimpleTestCase): + def setUp(self): + self.mailbox = MailboxSpec("example.net", "support") + self.apply = self.enterContext( + patch( + "coulobre.mail_node_apply.apply_mailbox", + return_value=Path("/private/credentials.json"), + ) + ) + + def request(self, manifest): + output = StringIO() + with ( + patch( + "sys.stdin", + SimpleNamespace(buffer=BytesIO(json.dumps(manifest).encode())), + ), + redirect_stdout(output), + ): + code = main(["apply-request"]) + return code, json.loads(output.getvalue()) + + def test_request_reports_the_mailbox_identity_only(self): + manifest = self.mailbox.manifest() + code, reply = self.request(manifest) + self.apply.assert_called_once_with(self.mailbox) + self.assertEqual(code, 0) + self.assertEqual(reply, manifest | {"state": "configured", "problem": None}) + + def test_request_rejects_extra_credentials_before_acting(self): + code, reply = self.request(self.mailbox.manifest() | {"password": "untrusted"}) + self.assertEqual(code, 1) + self.assertEqual(reply["state"], "failed") + self.apply.assert_not_called() + + class MailboxApplyTests(SimpleTestCase): def setUp(self): directory = Path(self.enterContext(TemporaryDirectory())) diff --git a/src/coulobre/mail_node.py b/src/coulobre/mail_node.py index 9440986..468f9d5 100644 --- a/src/coulobre/mail_node.py +++ b/src/coulobre/mail_node.py @@ -5,6 +5,7 @@ from dataclasses import dataclass from pathlib import Path from .domain_names import canonical_domain +from .node_requests import serve_apply_request MANIFEST_VERSION = 1 MANIFEST_KIND = "virtual_mailbox" @@ -74,10 +75,21 @@ def main(argv=None): "apply", help="Install a virtual mailbox on the mail node." ) apply.add_argument("manifest", type=Path) + commands.add_parser( + "apply-request", + help="Apply one mailbox manifest from standard input and report JSON.", + ) status = commands.add_parser("status", help="Check an installed virtual mailbox.") status.add_argument("manifest", type=Path) args = parser.parse_args(argv) + if args.command == "apply-request": + from .mail_node_apply import apply_mailbox + + return serve_apply_request( + MANIFEST_VERSION, MailboxSpec.from_manifest, apply_mailbox + ) + try: payload = json.loads(args.manifest.read_text(encoding="utf-8")) mailbox = MailboxSpec.from_manifest(payload)