core-agent-linux/network/qubes-firewall

59 lines
1.8 KiB
Plaintext
Raw Normal View History

2015-02-05 03:14:41 +01:00
#!/bin/sh
2011-03-09 20:50:13 +01:00
set -e
PIDFILE=/var/run/qubes/qubes-firewall.pid
2013-06-07 05:20:55 +02:00
XENSTORE_IPTABLES=/qubes-iptables
XENSTORE_IPTABLES_HEADER=/qubes-iptables-header
XENSTORE_ERROR=/qubes-iptables-error
2011-03-09 20:50:13 +01:00
OLD_RULES=""
# PIDfile handling
2015-02-05 03:14:41 +01:00
[ -e "$PIDFILE" ] && kill -s 0 $(cat "$PIDFILE") 2>/dev/null && exit 0
2011-03-09 20:50:13 +01:00
echo $$ >$PIDFILE
2015-02-05 03:14:41 +01:00
trap 'exit 0' TERM
2011-03-09 20:50:13 +01:00
FIRST_TIME=yes
2011-03-09 20:50:13 +01:00
while true; do
echo "1" > /proc/sys/net/ipv4/ip_forward
if [ "$FIRST_TIME" ]; then
FIRST_TIME=
TRIGGER=reload
else
2013-06-07 05:20:55 +02:00
# Wait for changes in qubesdb file
/usr/bin/qubesdb-watch $XENSTORE_IPTABLES
TRIGGER=$(/usr/bin/qubesdb-read $XENSTORE_IPTABLES)
fi
if ! [ "$TRIGGER" = "reload" ]; then continue ; fi
# Disable forwarding to prevent potential "leaks" that might
# be bypassing the firewall or some proxy service (e.g. tor)
# during the time when the rules are being (re)applied
echo "0" > /proc/sys/net/ipv4/ip_forward
RULES=$(qubesdb-read $XENSTORE_IPTABLES_HEADER)
IPTABLES_SAVE=$(iptables-save | sed '/^\*filter/,/^COMMIT/d')
OUT=$(printf '%s\n%s\n' "$RULES" "$IPTABLES_SAVE" | sed 's/\\n\|\\x0a/\n/g' | iptables-restore 2>&1 || true)
2013-06-07 05:20:55 +02:00
for i in $(qubesdb-list -f /qubes-iptables-domainrules) ; do
RULES=$(qubesdb-read "$i")
ERRS=$(printf '%s\n' "$RULES" | sed 's/\\n\|\\x0a/\n/g' | /sbin/iptables-restore -n 2>&1 || true)
if [ -n "$ERRS" ]; then
echo "Failed applying rules for $i: $ERRS" >&2
OUT="$OUT$ERRS"
fi
done
qubesdb-write $XENSTORE_ERROR "$OUT"
if [ -n "$OUT" ]; then
2015-02-05 03:14:41 +01:00
DISPLAY=:0 /usr/bin/notify-send -t 3000 "Firewall loading error ($(hostname))" "$OUT" || :
fi
# Check if user didn't define some custom rules to be applied as well...
[ -x /rw/config/qubes-firewall-user-script ] && /rw/config/qubes-firewall-user-script
# XXX: Backward compatibility
[ -x /rw/config/qubes_firewall_user_script ] && /rw/config/qubes_firewall_user_script
2011-03-09 20:50:13 +01:00
done