2015-02-05 03:14:41 +01:00
|
|
|
#!/bin/sh
|
2011-03-09 20:50:13 +01:00
|
|
|
set -e
|
|
|
|
|
2013-03-13 02:26:40 +01:00
|
|
|
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
|
|
|
|
2012-03-09 01:44:27 +01:00
|
|
|
FIRST_TIME=yes
|
|
|
|
|
2011-03-09 20:50:13 +01:00
|
|
|
while true; do
|
2011-08-18 18:47:08 +02:00
|
|
|
|
|
|
|
echo "1" > /proc/sys/net/ipv4/ip_forward
|
|
|
|
|
2012-03-09 01:44:27 +01:00
|
|
|
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)
|
2012-03-09 01:44:27 +01:00
|
|
|
fi
|
2011-07-29 16:50:12 +02:00
|
|
|
|
|
|
|
if ! [ "$TRIGGER" = "reload" ]; then continue ; fi
|
2011-08-18 18:47:08 +02:00
|
|
|
|
2014-03-28 02:56:43 +01:00
|
|
|
# Disable forwarding to prevent potential "leaks" that might
|
2011-08-18 18:47:08 +02:00
|
|
|
# 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
|
|
|
|
|
2015-01-30 00:30:24 +01:00
|
|
|
RULES=$(qubesdb-read $XENSTORE_IPTABLES_HEADER)
|
2014-09-26 19:56:12 +02:00
|
|
|
IPTABLES_SAVE=$(iptables-save | sed '/^\*filter/,/^COMMIT/d')
|
2015-02-11 14:14:27 +01:00
|
|
|
OUT=$(printf '%s\n%s\n' "$RULES" "$IPTABLES_SAVE" | sed 's/\\n\|\\x0a/\n/g' | iptables-restore 2>&1 || true)
|
2011-07-29 16:50:12 +02:00
|
|
|
|
2013-06-07 05:20:55 +02:00
|
|
|
for i in $(qubesdb-list -f /qubes-iptables-domainrules) ; do
|
2015-01-30 00:30:24 +01:00
|
|
|
RULES=$(qubesdb-read "$i")
|
2015-02-11 14:14:27 +01:00
|
|
|
ERRS=$(printf '%s\n' "$RULES" | sed 's/\\n\|\\x0a/\n/g' | /sbin/iptables-restore -n 2>&1 || true)
|
2014-09-03 09:59:59 +02:00
|
|
|
if [ -n "$ERRS" ]; then
|
|
|
|
echo "Failed applying rules for $i: $ERRS" >&2
|
|
|
|
OUT="$OUT$ERRS"
|
|
|
|
fi
|
2011-07-29 16:50:12 +02:00
|
|
|
done
|
2015-01-30 00:30:24 +01:00
|
|
|
qubesdb-write $XENSTORE_ERROR "$OUT"
|
2014-09-03 09:59:59 +02:00
|
|
|
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" || :
|
2011-07-29 16:50:12 +02:00
|
|
|
fi
|
2011-08-18 15:02:30 +02:00
|
|
|
|
|
|
|
# Check if user didn't define some custom rules to be applied as well...
|
2013-03-13 02:26:40 +01:00
|
|
|
[ -x /rw/config/qubes-firewall-user-script ] && /rw/config/qubes-firewall-user-script
|
|
|
|
# XXX: Backward compatibility
|
2011-08-18 15:02:30 +02:00
|
|
|
[ -x /rw/config/qubes_firewall_user_script ] && /rw/config/qubes_firewall_user_script
|
2011-03-09 20:50:13 +01:00
|
|
|
done
|