#!/bin/sh # Urutau host kit: egress logging (nftables + rsyslog), the container map and, # optionally, the fail2ban jail of the "second firewall". # # sudo sh install.sh # egress logging + container map # sudo sh install.sh --enforce # also the fail2ban jail for URUTAU_ENFORCE=on # sudo sh install.sh --remove # undo everything # # Idempotent. Urutau itself never gets privileges: it only reads these logs and # writes its ban decisions to /var/log/urutau-enforce/decisions.log. set -eu here=$(cd "$(dirname "$0")" && pwd) uid=${URUTAU_UID:-10001} [ "$(id -u)" = 0 ] || { echo "run as root" >&2; exit 1; } if [ "${1:-}" = "--remove" ]; then systemctl disable --now urutau-egress.service urutau-containers-map.timer urutau-logrotate.timer urutau-unban.path 2>/dev/null || true rm -f /etc/systemd/system/urutau-egress.service /etc/systemd/system/urutau-containers-map.service \ /etc/systemd/system/urutau-containers-map.timer /usr/local/sbin/urutau-containers-map \ /etc/rsyslog.d/41-urutau-egress.conf /etc/logrotate.d/urutau-egress \ /etc/fail2ban/jail.d/urutau.local /etc/fail2ban/filter.d/urutau.conf /etc/fail2ban/action.d/urutau-allports.conf \ /etc/systemd/system/urutau-logrotate.service /etc/systemd/system/urutau-logrotate.timer \ /etc/systemd/system/urutau-unban.path /etc/systemd/system/urutau-unban.service /usr/local/sbin/urutau-unban rm -rf /etc/urutau systemctl daemon-reload systemctl restart rsyslog fail2ban-client reload >/dev/null 2>&1 || true echo "removed (logs under /var/log/urutau kept)" exit 0 fi wan=${URUTAU_WAN:-$(ip -o route get 1.1.1.1 | sed -n 's/.* dev \([^ ]*\).*/\1/p')} [ -n "$wan" ] || { echo "cannot detect the uplink interface; set URUTAU_WAN" >&2; exit 1; } case "$wan" in *[!A-Za-z0-9_.-]*) echo "bad interface name: $wan" >&2; exit 1 ;; esac install -d -m 0750 -o syslog -g adm /var/log/urutau 2>/dev/null || install -d -m 0750 -g adm /var/log/urutau install -d -m 0755 /etc/urutau sed "s/@WAN@/$wan/g" "$here/urutau-egress.nft" > /etc/urutau/egress.nft nft -c -f /etc/urutau/egress.nft install -m 0644 "$here/41-urutau-egress.conf" /etc/rsyslog.d/41-urutau-egress.conf install -m 0644 "$here/urutau.logrotate" /etc/logrotate.d/urutau-egress install -m 0755 "$here/urutau-containers-map" /usr/local/sbin/urutau-containers-map install -m 0644 "$here/urutau-egress.service" "$here/urutau-containers-map.service" \ "$here/urutau-containers-map.timer" "$here/urutau-logrotate.service" "$here/urutau-logrotate.timer" /etc/systemd/system/ systemctl daemon-reload systemctl restart rsyslog systemctl enable urutau-egress.service urutau-containers-map.timer urutau-logrotate.timer >/dev/null systemctl start urutau-logrotate.timer systemctl restart urutau-egress.service systemctl start urutau-containers-map.service urutau-containers-map.timer echo "egress logging on $wan: /var/log/urutau/egress.log, map: /var/log/urutau/containers.map" if [ "${1:-}" = "--enforce" ]; then install -d -m 0750 -o "$uid" -g adm /var/log/urutau-enforce [ -e /var/log/urutau-enforce/decisions.log ] || install -m 0640 -o "$uid" -g adm /dev/null /var/log/urutau-enforce/decisions.log install -m 0644 "$here/fail2ban-filter-urutau.conf" /etc/fail2ban/filter.d/urutau.conf install -m 0644 "$here/fail2ban-action-urutau.conf" /etc/fail2ban/action.d/urutau-allports.conf install -m 0644 "$here/fail2ban-jail-urutau.local" /etc/fail2ban/jail.d/urutau.local [ -e /var/log/urutau-enforce/unban.log ] || install -m 0640 -o "$uid" -g adm /dev/null /var/log/urutau-enforce/unban.log install -m 0755 "$here/urutau-unban" /usr/local/sbin/urutau-unban install -m 0644 "$here/urutau-unban.path" "$here/urutau-unban.service" /etc/systemd/system/ systemctl daemon-reload systemctl enable --now urutau-unban.path >/dev/null fail2ban-client -t >/dev/null fail2ban-client reload echo "fail2ban jail 'urutau' on; mount /var/log/urutau-enforce:/logs/enforce (rw) in the container" fi