old-firewall/fw.sh

72 lines
1.2 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
2013-03-11 01:24:06 +00:00
set -o errexit
set -o pipefail
set -o nounset
2013-03-11 08:54:58 +00:00
export PATH="/sbin:/usr/sbin:/bin:/usr/bin"
. "$(dirname ${0})"/fw.globals
2013-03-11 08:54:58 +00:00
2013-03-11 01:24:06 +00:00
fw_usage() {
2013-03-11 02:39:18 +00:00
echo "${0} <apply|restore|test>"
2013-03-11 01:24:06 +00:00
}
fw_apply() {
2013-03-11 08:32:52 +00:00
fw_flush
2013-03-11 08:34:51 +00:00
. "$(dirname ${0})"/lib/loadrules.bash
2013-03-11 08:32:52 +00:00
for rule_file in $(dirname ${0})/rules/*; do
echo -n "[rules] $(basename ${rule_file}): "
2013-03-11 08:34:51 +00:00
loadrules ${rule_file}
2013-03-11 08:32:52 +00:00
echo "OK"
done
2013-03-11 01:24:06 +00:00
}
fw_restore() {
2013-03-11 08:39:08 +00:00
echo "!!! Restoring previous firewall state"
2013-03-11 08:54:58 +00:00
iptables-restore --counters < /var/lib/firewall-backups/latest
2013-03-11 01:24:06 +00:00
}
fw_test() {
for test_script in $(dirname ${0})/tests/*; do
2013-03-11 08:32:52 +00:00
if [[ -x ${test_script} ]]; then
echo -n "[test] $(basename ${test_script}): "
2013-03-11 08:34:51 +00:00
${test_script}
2013-03-11 08:32:52 +00:00
echo "OK"
fi
2013-03-11 02:39:18 +00:00
done
2013-03-11 01:24:06 +00:00
}
fw_flush() {
2013-03-23 05:32:56 +00:00
# We don't use (yet?) these tables: raw, security
for table in filter nat mangle; do
2013-03-11 09:00:33 +00:00
iptables --table ${table} --flush
done
2013-03-11 01:24:06 +00:00
}
2013-03-23 05:28:37 +00:00
if [[ ! $# -eq 1 ]]; then
2013-03-11 01:24:06 +00:00
fw_usage
exit 1
fi
case ${1} in
2013-03-11 01:24:06 +00:00
apply)
fw_apply
;;
2013-03-23 05:32:56 +00:00
flush)
fw_flush
;;
2013-03-11 01:24:06 +00:00
restore)
fw_restore
;;
test)
2013-03-11 02:41:22 +00:00
fw_test
2013-03-11 01:24:06 +00:00
;;
*)
fw_usage
exit 1
;;
esac
exit 0
2013-03-11 01:24:06 +00:00