#!/bin/bash # Fence the node off and serve ssh. Runs as the container's root, which in a # rootless container is the node owner's own uid on the host: no host privilege. # # The fence goes up before sshd listens, so there is no moment in which the node # can reach the machine's LAN. set -euo pipefail CONFIG=/config/node.env [ -r "$CONFIG" ] || { echo "midas node: $CONFIG is missing (install.sh writes it)" >&2; exit 1; } # shellcheck source=/dev/null . "$CONFIG" hub=${MIDAS_HUB#http://}; hub_ip=${hub%:*}; hub_port=${hub##*:} # The resolvers podman wrote are usually private addresses (pasta's forwarder): # DNS to them, and only DNS, stays open. dns4=$(awk '$1=="nameserver" && $2 ~ /^[0-9.]+$/ {printf "%s%s", s, $2; s=", "}' /etc/resolv.conf) [ -n "$dns4" ] || { echo "midas node: no IPv4 nameserver in /etc/resolv.conf" >&2; exit 1; } # The machine's own public addresses (install.sh): its web ports, which is what # anybody on the internet reaches, and nothing else on it. The VPS listens on # 22, Forgejo's 2222 and more on every address, and the internet's view of it is # filtered upstream of the machine, so a node going through the machine itself # would otherwise see past that. host_rules="" if [ -n "${MIDAS_HOST_PUBLIC:-}" ]; then host_rules=" ip daddr { $MIDAS_HOST_PUBLIC } tcp dport { 80, 443 } accept ip daddr { $MIDAS_HOST_PUBLIC } drop" fi # Outbound: the internet yes (crates.io, git, and the tile, aerial and register # servers a capture fetches from); the hub's lease service yes; nothing else # private — not this machine, not its LAN, not the carrier's network. # Inbound: ssh, which pasta only ever delivers from the machine's WireGuard # address; replies on connections that already exist. nft -f - < ~/.ssh/config </fdinfo needs CAP_SYS_PTRACE, which the container's root # does not have, and as root the guard saw no process holding GPU memory at all # (2026-10-11). Restarted if it dies. # `set +e` inside: the script runs under -e, and without it the loop itself # exited the first time the guard did (a deploy's stop-guard left both nodes # unguarded until this was found, 2026-10-11). ( set +e; while :; do su build -s /bin/sh -c '/usr/local/bin/midas node guard'; sleep 5; done ) & # The midas the hub runs, taken while nobody holds the node (DESIGN.md §3): # from the owner's copy, as root, which runs the hub's only as `build`. This # script, the fence above and the container around it stay the owner's. ( set +e; sleep 30; while :; do /usr/local/libexec/midas-owner node update; sleep 300; done ) & echo "midas node ${MIDAS_NODE:-?}: fenced, sshd starting" exec /usr/bin/sshd -D -e -f /etc/ssh/sshd_config