#!/bin/sh
# Console Cast's root helper, run through pkexec after the customer approves
# (polkit action app.consolecast.desktop.system). It makes only these changes,
# records them in /var/lib/console-cast, and undoes them when the package is
# removed:
#   firewall SUBNET   let the console on SUBNET reach DNS (53) and RTMP (1935)
#   resolved          turn off systemd-resolved's stub listener (docs/LINUX.md)
#   cleanup           undo both (run by the package's pre-removal step)
set -eu
STATE=/var/lib/console-cast

# valid_subnet NET: exactly one line of the form A.B.C.0/24, each part 0-255.
valid_subnet() {
  case "$1" in "" | *[!0-9./]* | */*/* | *..* | .* ) return 1 ;; */24) ;; *) return 1 ;; esac
  ip="${1%/24}"
  case "$ip" in *.*.*.*.*) return 1 ;; *.*.*.0) ;; *) return 1 ;; esac
  rest="$ip"
  for _ in 1 2 3 4; do
    part="${rest%%.*}" rest="${rest#*.}"
    case "$part" in [0-9] | [0-9][0-9] | [0-9][0-9][0-9]) ;; *) return 1 ;; esac
    [ "$part" -le 255 ] || return 1
  done
}

case "${1:-}" in
firewall)
  NET="${2:-}"
  valid_subnet "$NET" || { echo "invalid subnet" >&2; exit 2; }
  mkdir -p "$STATE"
  if command -v firewall-cmd >/dev/null 2>&1 && firewall-cmd --state >/dev/null 2>&1; then
    for rule in "port port=53 protocol=udp" "port port=53 protocol=tcp" "port port=1935 protocol=tcp"; do
      r="rule family=ipv4 source address=$NET $rule accept"
      firewall-cmd --permanent --add-rich-rule="$r" >/dev/null
      firewall-cmd --add-rich-rule="$r" >/dev/null
      printf 'firewalld\t%s\n' "$r" >>"$STATE/firewall"
    done
  elif command -v ufw >/dev/null 2>&1; then
    ufw allow from "$NET" to any port 53 comment "Console Cast" >/dev/null
    ufw allow proto tcp from "$NET" to any port 1935 comment "Console Cast" >/dev/null
    printf 'ufw\tallow from %s to any port 53\n' "$NET" >>"$STATE/firewall"
    printf 'ufw\tallow proto tcp from %s to any port 1935\n' "$NET" >>"$STATE/firewall"
  else
    echo "no firewall to change" >&2
  fi
  chmod 644 "$STATE/firewall" 2>/dev/null || true
  ;;
resolved)
  mkdir -p "$STATE" /etc/systemd/resolved.conf.d
  printf '[Resolve]\nDNSStubListener=no\n' >/etc/systemd/resolved.conf.d/consolecast.conf
  readlink /etc/resolv.conf >"$STATE/resolv.conf.link" 2>/dev/null || true
  ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
  systemctl restart systemd-resolved
  ;;
cleanup)
  if [ -f "$STATE/firewall" ]; then
    while IFS="$(printf '\t')" read -r kind rule; do
      case "$kind" in
      firewalld)
        firewall-cmd --permanent --remove-rich-rule="$rule" >/dev/null 2>&1 || true
        firewall-cmd --remove-rich-rule="$rule" >/dev/null 2>&1 || true ;;
      ufw)
        # shellcheck disable=SC2086
        ufw delete $rule >/dev/null 2>&1 || true ;;
      esac
    done <"$STATE/firewall"
  fi
  if [ -f /etc/systemd/resolved.conf.d/consolecast.conf ]; then
    rm -f /etc/systemd/resolved.conf.d/consolecast.conf
    link="$(cat "$STATE/resolv.conf.link" 2>/dev/null || true)"
    [ -n "$link" ] && ln -sf "$link" /etc/resolv.conf
    systemctl try-restart systemd-resolved 2>/dev/null || true
  fi
  rm -rf "$STATE"
  ;;
*)
  echo "usage: console-cast-system firewall SUBNET | resolved | cleanup" >&2
  exit 2
  ;;
esac
