#!/usr/bin/sh
# suricatavel-fw: the only firewall commands Suricatavel runs as root on an iptables, nftables or pf host.
#
# Installed root-owned in /usr/local/sbin by linux-firewall.yml or bsd-firewall.yml, with a sudoers rule that lets the SSH user run this
# script, and nothing else, without a password. A sudo rule on iptables, nft or pfctl themselves is close to root
# (iptables --modprobe=<program>, nft -f or pfctl -f on any file), so this script takes no options: each operation
# has fixed arguments, every one of them validated, and the firewall binary is called with an argument list.
#
#   suricatavel-fw iptables list <4|6> <chain>
#   suricatavel-fw iptables insert <chain> <address> <DROP|REJECT> [comment]
#   suricatavel-fw iptables delete <4|6> <chain> <rule number>
#   suricatavel-fw nft get <family> <table> <set> <address>
#   suricatavel-fw nft add <family> <table> <set> <address> <timeout seconds, 0: none> [comment]
#   suricatavel-fw nft delete <family> <table> <set> <address>
#   suricatavel-fw nft list-set <family> <table> <set>
#   suricatavel-fw nft list-table <family> <table>
#   suricatavel-fw pf add|delete <table> <address>
#   suricatavel-fw pf show <table>
#   suricatavel-fw pf info
#   suricatavel-fw version
set -eu

PATH=/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

VERSION=1

die() {
    printf 'suricatavel-fw: %s\n' "$*" >&2
    exit 64
}

# Whole-value match of an extended regex; values with a line break never match (grep works line by line).
matches() {
    case $1 in
        *'
'*) return 1 ;;
    esac
    printf '%s\n' "$1" | grep -Eqx -- "$2"
}

identifier() {
    matches "$1" '[A-Za-z0-9_][A-Za-z0-9_.-]{0,63}' || die "invalid $2: $1"
}

address() {
    matches "$1" '([0-9]{1,3}\.){3}[0-9]{1,3}(/[0-9]{1,2})?' \
        || matches "$1" '[0-9A-Fa-f.]*:[0-9A-Fa-f:.]*(/[0-9]{1,3})?' \
        || die "invalid address: $1"
}

comment() {
    matches "$1" '[A-Za-z0-9 .:_-]{0,60}' || die "invalid comment"
}

family() {
    case $1 in
        ip | ip6 | inet | arp | bridge | netdev) ;;
        *) die "invalid nftables family: $1" ;;
    esac
}

ip_version_binary() {
    case $1 in
        4) echo iptables ;;
        6) echo ip6tables ;;
        *) die "invalid IP version: $1" ;;
    esac
}

arguments() {
    [ "$1" -ge "$2" ] && [ "$1" -le "$3" ] || die "wrong number of arguments"
}

[ $# -ge 1 ] || die "usage: suricatavel-fw iptables|nft|pf|version ..."
tool=$1
shift

case $tool in
    version)
        echo "$VERSION"
        ;;

    iptables)
        [ $# -ge 1 ] || die "usage: suricatavel-fw iptables list|insert|delete ..."
        operation=$1
        shift
        case $operation in
            list)
                arguments $# 2 2
                binary=$(ip_version_binary "$1")
                identifier "$2" chain
                exec "$binary" -S "$2"
                ;;
            insert)
                arguments $# 3 4
                identifier "$1" chain
                address "$2"
                case $3 in DROP | REJECT) ;; *) die "invalid target: $3" ;; esac
                case $2 in *:*) binary=ip6tables ;; *) binary=iptables ;; esac
                if [ $# -eq 4 ] && [ -n "$4" ]; then
                    comment "$4"
                    exec "$binary" -I "$1" -s "$2" -j "$3" -m comment --comment "$4"
                fi
                exec "$binary" -I "$1" -s "$2" -j "$3"
                ;;
            delete)
                arguments $# 3 3
                binary=$(ip_version_binary "$1")
                identifier "$2" chain
                matches "$3" '[1-9][0-9]{0,5}' || die "invalid rule number: $3"
                exec "$binary" -D "$2" "$3"
                ;;
            *) die "unknown iptables operation: $operation" ;;
        esac
        ;;

    nft)
        [ $# -ge 1 ] || die "usage: suricatavel-fw nft get|add|delete|list-set|list-table ..."
        operation=$1
        shift
        case $operation in
            get | delete)
                arguments $# 4 4
                family "$1"
                identifier "$2" table
                identifier "$3" set
                address "$4"
                exec nft "$operation" element "$1" "$2" "$3" "{ $4 }"
                ;;
            add)
                arguments $# 5 6
                family "$1"
                identifier "$2" table
                identifier "$3" set
                address "$4"
                matches "$5" '[0-9]{1,10}' || die "invalid timeout: $5"
                element=$4
                if [ "$5" != 0 ]; then
                    element="$element timeout ${5}s"
                fi
                if [ $# -eq 6 ] && [ -n "$6" ]; then
                    comment "$6"
                    element="$element comment \"$6\""
                fi
                exec nft add element "$1" "$2" "$3" "{ $element }"
                ;;
            list-set)
                arguments $# 3 3
                family "$1"
                identifier "$2" table
                identifier "$3" set
                exec nft -j list set "$1" "$2" "$3"
                ;;
            list-table)
                arguments $# 2 2
                family "$1"
                identifier "$2" table
                exec nft -j list table "$1" "$2"
                ;;
            *) die "unknown nft operation: $operation" ;;
        esac
        ;;

    pf)
        [ $# -ge 1 ] || die "usage: suricatavel-fw pf add|delete|show|info ..."
        operation=$1
        shift
        case $operation in
            add | delete)
                arguments $# 2 2
                identifier "$1" table
                address "$2"
                exec pfctl -t "$1" -T "$operation" "$2"
                ;;
            show)
                arguments $# 1 1
                identifier "$1" table
                exec pfctl -t "$1" -T show
                ;;
            info)
                arguments $# 0 0
                exec pfctl -s info
                ;;
            *) die "unknown pf operation: $operation" ;;
        esac
        ;;

    *) die "unknown tool: $tool" ;;
esac
