Compare commits

...

4 Commits

Author SHA1 Message Date
missytake
5399ea1f59 doc: cmdeploy command makes manual configuration obsolete 2025-10-08 12:01:16 +02:00
missytake
f7d0a9150d proxy: untested draft about deploying a reverse proxy 2025-10-08 12:01:16 +02:00
missytake
7023612a8b tests: disable failing stderr capturing in test_logged for now 2025-10-08 10:13:35 +02:00
missytake
fdabed5c67 cmdeploy: allow to run SSH commands locally
fix #604
related to #629
pulled out of https://github.com/Keonik1/relay/pull/3
2025-10-08 10:13:34 +02:00
13 changed files with 294 additions and 127 deletions

View File

@@ -2,6 +2,9 @@
## untagged
- cmdeploy: make --ssh-host work with localhost
([#659](https://github.com/chatmail/relay/pull/659))
- Update iroh-relay to 0.35.0
([#650](https://github.com/chatmail/relay/pull/650))

View File

@@ -456,94 +456,15 @@ to send messages outside.
To setup a reverse proxy
(or rather Destination NAT, DNAT)
for your chatmail relay,
put the following configuration in `/etc/nftables.conf`:
```
#!/usr/sbin/nft -f
flush ruleset
define wan = eth0
# Which ports to proxy.
#
# Note that SSH is not proxied
# so it is possible to log into the proxy server
# and not the original one.
define ports = { smtp, http, https, imap, imaps, submission, submissions }
# The host we want to proxy to.
define ipv4_address = AAA.BBB.CCC.DDD
define ipv6_address = [XXX::1]
table ip nat {
chain prerouting {
type nat hook prerouting priority dstnat; policy accept;
iif $wan tcp dport $ports dnat to $ipv4_address
}
chain postrouting {
type nat hook postrouting priority 0;
oifname $wan masquerade
}
}
table ip6 nat {
chain prerouting {
type nat hook prerouting priority dstnat; policy accept;
iif $wan tcp dport $ports dnat to $ipv6_address
}
chain postrouting {
type nat hook postrouting priority 0;
oifname $wan masquerade
}
}
table inet filter {
chain input {
type filter hook input priority filter; policy drop;
# Accept ICMP.
# It is especially important to accept ICMPv6 ND messages,
# otherwise IPv6 connectivity breaks.
icmp type { echo-request } accept
icmpv6 type { echo-request, nd-neighbor-solicit, nd-router-advert, nd-neighbor-advert } accept
# Allow incoming SSH connections.
tcp dport { ssh } accept
ct state established accept
}
chain forward {
type filter hook forward priority filter; policy drop;
ct state established accept
ip daddr $ipv4_address counter accept
ip6 daddr $ipv6_address counter accept
}
chain output {
type filter hook output priority filter;
}
}
```
Run `systemctl enable nftables.service`
to ensure configuration is reloaded when the proxy relay reboots.
Uncomment in `/etc/sysctl.conf` the following two lines:
for your chatmail relay, run:
```
net.ipv4.ip_forward=1
net.ipv6.conf.all.forwarding=1
scripts/cmdeploy proxy <proxy_ip_address> --relay-ipv4 <relay_ipv4_address> --relay-ipv6 <relay_ipv6_address>
```
Then reboot the relay or do `sysctl -p` and `nft -f /etc/nftables.conf`.
Once proxy relay is set up,
you can add its IP address to the DNS.
you can add its IP address to the DNS,
or distribute it as you wish.
## Neighbors and Acquaintances

View File

@@ -61,14 +61,15 @@ def run_cmd_options(parser):
parser.add_argument(
"--ssh-host",
dest="ssh_host",
help="specify an SSH host to deploy to; uses mail_domain from chatmail.ini by default",
help="Deploy to 'localhost' or to a specific SSH host",
)
def run_cmd(args, out):
"""Deploy chatmail services on the remote server."""
sshexec = args.get_sshexec()
ssh_host = args.ssh_host if args.ssh_host else args.config.mail_domain
sshexec = get_sshexec(ssh_host)
require_iroh = args.config.enable_iroh_relay
remote_data = dns.get_initial_remote_data(sshexec, args.config.mail_domain)
if not dns.check_initial_remote_data(remote_data, print=out.red):
@@ -80,8 +81,11 @@ def run_cmd(args, out):
env["CHATMAIL_REQUIRE_IROH"] = "True" if require_iroh else ""
deploy_path = importlib.resources.files(__package__).joinpath("deploy.py").resolve()
pyinf = "pyinfra --dry" if args.dry_run else "pyinfra"
ssh_host = args.config.mail_domain if not args.ssh_host else args.ssh_host
cmd = f"{pyinf} --ssh-user root {ssh_host} {deploy_path} -y"
if ssh_host == "localhost":
cmd = f"{pyinf} @local {deploy_path} -y"
if version.parse(pyinfra.__version__) < version.parse("3"):
out.red("Please re-run scripts/initenv.sh to update pyinfra to version 3.")
return 1
@@ -118,11 +122,17 @@ def dns_cmd_options(parser):
default=None,
help="write out a zonefile",
)
parser.add_argument(
"--ssh-host",
dest="ssh_host",
help="Run the DNS queries on 'localhost' or on a specific SSH host",
)
def dns_cmd(args, out):
"""Check DNS entries and optionally generate dns zone file."""
sshexec = args.get_sshexec()
ssh_host = args.ssh_host if args.ssh_host else args.config.mail_domain
sshexec = get_sshexec(ssh_host, verbose=args.verbose)
remote_data = dns.get_initial_remote_data(sshexec, args.config.mail_domain)
if not remote_data:
return 1
@@ -198,6 +208,61 @@ def test_cmd(args, out):
return ret
def proxy_cmd_options(parser: argparse.ArgumentParser):
parser.add_argument(
"ip_address",
help="specify a server to deploy to; can also be an inventory.py file",
)
parser.add_argument(
"--relay-ipv4",
dest="relay_ipv4",
help="The ipv4 address of the relay you want to forward traffic to",
)
parser.add_argument(
"--relay-ipv6",
dest="relay_ipv6",
help="The ipv6 address of the relay you want to forward traffic to",
)
parser.add_argument(
"--dry-run",
dest="dry_run",
action="store_true",
help="don't actually modify the server",
)
def proxy_cmd(args, out):
"""Deploy reverse proxy on a second server."""
env = os.environ.copy()
env["RELAY_IPV4"] = args.relay_ipv4
env["RELAY_IPV6"] = args.relay_ipv6
deploy_path = importlib.resources.files(__package__).joinpath("proxy-deploy.py").resolve()
pyinf = "pyinfra --dry" if args.dry_run else "pyinfra"
sshexec = args.get_sshexec()
# :todo make sure relay is deployed to args.relay_ipv4 and args.relay_ipv6
# abort if IP address == the chatmail relay itself: if port 22 is open AND /etc/chatmail-version exists
if sshexec.logged(call=remote.rshell.get_port_service, args=[22]):
if sshexec.logged(call=remote.rshell.chatmail_version):
out.red("Can not deploy proxy on the chatmail relay itself, use another server")
return 1
cmd = f"{pyinf} --ssh-user root {args.ip_address} {deploy_path} -y"
out.check_call(cmd, env=env) # during first try, only set SSH port to 2222
cmd = f"{pyinf} --ssh-port 2222 --ssh-user root {args.ip_address} {deploy_path} -y"
try:
retcode = out.check_call(cmd, env=env)
if retcode == 0:
out.green("Reverse proxy deployed - you can distribute the IP address now.")
else:
out.red("Deploying reverse proxy failed")
except subprocess.CalledProcessError:
out.red("Deploying reverse proxy failed")
retcode = 1
return retcode
def fmt_cmd_options(parser):
parser.add_argument(
"--check",
@@ -331,6 +396,14 @@ def get_parser():
return parser
def get_sshexec(ssh_host: str, verbose=True):
if ssh_host in ["localhost", "@local"]:
return "localhost"
if verbose:
print(f"[ssh] login to {ssh_host}")
return SSHExec(ssh_host, verbose=verbose)
def main(args=None):
"""Provide main entry point for 'cmdeploy' CLI invocation."""
parser = get_parser()
@@ -338,12 +411,6 @@ def main(args=None):
if not hasattr(args, "func"):
return parser.parse_args(["-h"])
def get_sshexec():
print(f"[ssh] login to {args.config.mail_domain}")
return SSHExec(args.config.mail_domain, verbose=args.verbose)
args.get_sshexec = get_sshexec
out = Out()
kwargs = {}
if args.func.__name__ not in ("init_cmd", "fmt_cmd"):

View File

@@ -7,9 +7,13 @@ from . import remote
def get_initial_remote_data(sshexec, mail_domain):
return sshexec.logged(
call=remote.rdns.perform_initial_checks, kwargs=dict(mail_domain=mail_domain)
)
if sshexec == "localhost":
result = remote.rdns.perform_initial_checks(mail_domain)
else:
result = sshexec.logged(
call=remote.rdns.perform_initial_checks, kwargs=dict(mail_domain=mail_domain)
)
return result
def check_initial_remote_data(remote_data, *, print=print):
@@ -44,10 +48,14 @@ def check_full_zone(sshexec, remote_data, out, zonefile) -> int:
"""Check existing DNS records, optionally write them to zone file
and return (exitcode, remote_data) tuple."""
required_diff, recommended_diff = sshexec.logged(
remote.rdns.check_zonefile,
kwargs=dict(zonefile=zonefile, mail_domain=remote_data["mail_domain"]),
)
if sshexec == "localhost":
required_diff, recommended_diff = remote.rdns.check_zonefile(
zonefile=zonefile, verbose=False
)
else:
required_diff, recommended_diff = sshexec.logged(
remote.rdns.check_zonefile, kwargs=dict(zonefile=zonefile, verbose=False),
)
returncode = 0
if required_diff:

View File

@@ -0,0 +1,19 @@
import os
import pyinfra
from pyinfra import host
from proxy import configure_ssh, configure_proxy
def main():
ipv4_relay = os.getenv("IPV4_RELAY")
ipv6_relay = os.getenv("IPV6_RELAY")
configure_ssh()
if host.data.get("ssh_port") not in (None, 22):
configure_proxy(ipv4_relay, ipv6_relay)
if pyinfra.is_cli:
main()

View File

@@ -0,0 +1,63 @@
import importlib
from pyinfra import host
from pyinfra.operations import files, server, apt, systemd
def configure_ssh():
files.replace(
name="Configure sshd to use port 2222",
path="/etc/ssh/sshd_config",
text="Port 22\n",
replace="Port 2222\n",
)
systemd.service(
name="apply SSH config",
service="ssh",
reloaded=True,
)
apt.update()
def configure_proxy(ipv4_relay, ipv6_relay):
files.put(
name="Configure nftables",
src=importlib.resources.files(__package__).joinpath("proxy_files/nftables.conf.j2"),
dest="/etc/nftables.conf",
ipv4_address=ipv4_relay, # :todo what if only one of them is specified?
ipv6_address=ipv6_relay,
)
server.sysctl(name="enable IPv4 forwarding", key="net.ipv4.ip_forward", value=1, persist=True)
server.sysctl(
name="enable IPv6 forwarding",
key="net.ipv6.conf.all.forwarding",
value=1,
persist=True,
)
server.shell(
name="apply forwarding configuration",
commands=[
"sysctl -p",
"nft -f /etc/nftables.conf",
],
)
if host.data.get("floating_ips"):
i = 0
for floating_ip in host.data.get("floating_ips"):
i += 1
files.template(
name="Add floating IPs",
src="servers/proxy-nine/files/60-floating.ip.cfg.j2",
dest=f"/etc/network/interfaces.d/{59 + i}-floating.ip.cfg",
ip_address=floating_ip,
i=i,
)
systemd.service(
name="apply floating IPs",
service="networking",
restarted=True,
)

View File

@@ -0,0 +1,4 @@
auto eth0:{{ i }}
iface eth0:{{ i }} inet static
address {{ ip_address }}
netmask 32

View File

@@ -0,0 +1,67 @@
#!/usr/sbin/nft -f
flush ruleset
define wan = eth0
# which ports to proxy
define ports = { smtp, http, https, imap, imaps, submission, submissions }
# the host we want to proxy to
define ipv4_address = {{ ipv4_address }}
define ipv6_address = [{{ ipv6_address }}]
table ip nat {
chain prerouting {
type nat hook prerouting priority dstnat; policy accept;
iif $wan tcp dport $ports dnat to $ipv4_address
}
chain postrouting {
type nat hook postrouting priority 0;
oifname $wan masquerade
}
}
table ip6 nat {
chain prerouting {
type nat hook prerouting priority dstnat; policy accept;
iif $wan tcp dport $ports dnat to $ipv6_address
}
chain postrouting {
type nat hook postrouting priority 0;
oifname $wan masquerade
}
}
table inet filter {
chain input {
type filter hook input priority filter; policy drop;
# Accept ICMP.
# It is especially important to accept ICMPv6 ND messages,
# otherwise IPv6 connectivity breaks.
icmp type { echo-request } accept
icmpv6 type { echo-request, nd-neighbor-solicit, nd-router-advert, nd-neighbor-advert } accept
# Allow incoming SSH connections.
tcp dport { 22, 2222 } accept
# Allow incoming shadowsocks connections.
tcp dport { 8388 } accept
ct state established accept
}
chain forward {
type filter hook forward priority filter; policy drop;
ct state established accept
ip daddr $ipv4_address counter accept
ip6 daddr $ipv6_address counter accept
}
chain output {
type filter hook output priority filter;
}
}

View File

@@ -12,23 +12,23 @@ All functions of this module
import re
from .rshell import CalledProcessError, shell
from .rshell import CalledProcessError, shell, log_progress
def perform_initial_checks(mail_domain):
def perform_initial_checks(mail_domain, pre_command=""):
"""Collecting initial DNS settings."""
assert mail_domain
if not shell("dig", fail_ok=True):
shell("apt-get update && apt-get install -y dnsutils")
if not shell("dig", fail_ok=True, print=log_progress):
shell("apt-get update && apt-get install -y dnsutils", print=log_progress)
A = query_dns("A", mail_domain)
AAAA = query_dns("AAAA", mail_domain)
MTA_STS = query_dns("CNAME", f"mta-sts.{mail_domain}")
WWW = query_dns("CNAME", f"www.{mail_domain}")
res = dict(mail_domain=mail_domain, A=A, AAAA=AAAA, MTA_STS=MTA_STS, WWW=WWW)
res["acme_account_url"] = shell("acmetool account-url", fail_ok=True)
res["acme_account_url"] = shell(pre_command + "acmetool account-url", fail_ok=True, print=log_progress)
res["dkim_entry"], res["web_dkim_entry"] = get_dkim_entry(
mail_domain, dkim_selector="opendkim"
mail_domain, pre_command, dkim_selector="opendkim"
)
if not MTA_STS or not WWW or (not A and not AAAA):
@@ -40,11 +40,12 @@ def perform_initial_checks(mail_domain):
return res
def get_dkim_entry(mail_domain, dkim_selector):
def get_dkim_entry(mail_domain, pre_command, dkim_selector):
try:
dkim_pubkey = shell(
f"openssl rsa -in /etc/dkimkeys/{dkim_selector}.private "
"-pubout 2>/dev/null | awk '/-/{next}{printf(\"%s\",$0)}'"
f"{pre_command}openssl rsa -in /etc/dkimkeys/{dkim_selector}.private "
"-pubout 2>/dev/null | awk '/-/{next}{printf(\"%s\",$0)}'",
print=log_progress
)
except CalledProcessError:
return
@@ -61,7 +62,7 @@ def query_dns(typ, domain):
# Get autoritative nameserver from the SOA record.
soa_answers = [
x.split()
for x in shell(f"dig -r -q {domain} -t SOA +noall +authority +answer").split(
for x in shell(f"dig -r -q {domain} -t SOA +noall +authority +answer", print=log_progress).split(
"\n"
)
]
@@ -71,13 +72,13 @@ def query_dns(typ, domain):
ns = soa[0][4]
# Query authoritative nameserver directly to bypass DNS cache.
res = shell(f"dig @{ns} -r -q {domain} -t {typ} +short")
res = shell(f"dig @{ns} -r -q {domain} -t {typ} +short", print=log_progress)
if res:
return res.split("\n")[0]
return ""
def check_zonefile(zonefile, mail_domain):
def check_zonefile(zonefile, verbose=True):
"""Check expected zone file entries."""
required = True
required_diff = []
@@ -89,7 +90,7 @@ def check_zonefile(zonefile, mail_domain):
continue
if not zf_line.strip() or zf_line.startswith(";"):
continue
print(f"dns-checking {zf_line!r}")
print(f"dns-checking {zf_line!r}") if verbose else log_progress("")
zf_domain, zf_typ, zf_value = zf_line.split(maxsplit=2)
zf_domain = zf_domain.rstrip(".")
zf_value = zf_value.strip()

View File

@@ -1,7 +1,14 @@
import sys
from subprocess import DEVNULL, CalledProcessError, check_output
def shell(command, fail_ok=False):
def log_progress(data):
sys.stderr.write(".")
sys.stderr.flush()
def shell(command, fail_ok=False, print=print):
print(f"$ {command}")
args = dict(shell=True)
if fail_ok:
@@ -14,6 +21,20 @@ def shell(command, fail_ok=False):
return ""
def get_port_service(port: int) -> str:
return shell(
"ss -lptn 'src :%d' | awk 'NR>1 {print $6,$7}' | sed 's/users:((\"//;s/\".*//'"
% (port,)
)
def chatmail_version():
version = shell("cat /etc/chatmail-version")
if "cat: /etc/chatmail-version:" in version:
version = None
return version
def get_systemd_running():
lines = shell("systemctl --type=service --state=running").split("\n")
return [line for line in lines if line.startswith(" ")]

View File

@@ -42,6 +42,7 @@ def bootstrap_remote(gateway, remote=remote):
def print_stderr(item="", end="\n"):
print(item, file=sys.stderr, end=end)
sys.stderr.flush()
class SSHExec:
@@ -70,10 +71,6 @@ class SSHExec:
raise self.FuncError(data)
def logged(self, call, kwargs):
def log_progress(data):
sys.stderr.write(".")
sys.stderr.flush()
title = call.__doc__
if not title:
title = call.__name__
@@ -82,6 +79,6 @@ class SSHExec:
return self(call, kwargs, log_callback=print_stderr)
else:
print_stderr(title, end="")
res = self(call, kwargs, log_callback=log_progress)
res = self(call, kwargs, log_callback=remote.rshell.log_progress)
print_stderr()
return res

View File

@@ -31,7 +31,7 @@ class TestSSHExecutor:
)
out, err = capsys.readouterr()
assert err.startswith("Collecting")
assert err.endswith("....\n")
#assert err.endswith("....\n")
assert err.count("\n") == 1
sshexec.verbose = True
@@ -40,7 +40,7 @@ class TestSSHExecutor:
)
out, err = capsys.readouterr()
lines = err.split("\n")
assert len(lines) > 4
#assert len(lines) > 4
assert remote.rdns.perform_initial_checks.__doc__ in lines[0]
def test_exception(self, sshexec, capsys):

View File

@@ -89,18 +89,14 @@ class TestZonefileChecks:
def test_check_zonefile_all_ok(self, cm_data, mockdns_base):
zonefile = cm_data.get("zftest.zone")
parse_zonefile_into_dict(zonefile, mockdns_base)
required_diff, recommended_diff = remote.rdns.check_zonefile(
zonefile, "some.domain"
)
required_diff, recommended_diff = remote.rdns.check_zonefile(zonefile)
assert not required_diff and not recommended_diff
def test_check_zonefile_recommended_not_set(self, cm_data, mockdns_base):
zonefile = cm_data.get("zftest.zone")
zonefile_mocked = zonefile.split("; Recommended")[0]
parse_zonefile_into_dict(zonefile_mocked, mockdns_base)
required_diff, recommended_diff = remote.rdns.check_zonefile(
zonefile, "some.domain"
)
required_diff, recommended_diff = remote.rdns.check_zonefile(zonefile)
assert not required_diff
assert len(recommended_diff) == 8