#!/bin/sh
# sshg-fw-null
# This file is part of SSHGuard.

fw_init() {
    echo "===>>> Initializing (null) firewall"
}

fw_block() {
    echo "===>>> Blocking $1/$3 (null)"
}

fw_release() {
    echo "===>>> Releasing $1/$3 (null)"
}

fw_flush() {
    echo "===>>> Flushing blocked addresses (null)"
}

fw_fin() {
    echo "===>>> Cleaning up (null) firewall"
}
die() {
    echo "$(basename "$0"): $2" >&2
    exit "$1"
}

fw_init || die 69 "Could not initialize firewall"

cleanup() {
    trap "" EXIT
    if [ "YES" = "$flushonexit" ]; then
        fw_flush
    fi
    fw_fin
    exit
}

trap cleanup EXIT INT TERM

while read -r cmd address addrtype cidr; do
    case $cmd in
        block)
            fw_block "$address" "$addrtype" "$cidr";;
        release)
            fw_release "$address" "$addrtype" "$cidr";;
        flush)
            fw_flush;;
        flushonexit)
            flushonexit=YES;;
        *)
            die 65 "Invalid command";;
    esac
done
