2011-03-09 20:50:13 +01:00
|
|
|
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
|
2013-03-13 02:26:40 +01:00
|
|
|
PIDFILE=/var/run/qubes/qubes-firewall.pid
|
2013-03-14 04:23:32 +01: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
|
|
|
|
[[ -e $PIDFILE ]] && kill -s 0 $(<$PIDFILE) 2>/dev/null && exit 0
|
|
|
|
echo $$ >$PIDFILE
|
|
|
|
|
|
|
|
trap 'exit 0' SIGTERM
|
|
|
|
|
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
|
|
|
|
# Wait for changes in xenstore file
|
|
|
|
/usr/bin/xenstore-watch-qubes $XENSTORE_IPTABLES
|
|
|
|
TRIGGER=$(/usr/bin/xenstore-read $XENSTORE_IPTABLES)
|
|
|
|
fi
|
2011-07-29 16:50:12 +02:00
|
|
|
|
|
|
|
if ! [ "$TRIGGER" = "reload" ]; then continue ; fi
|
2011-08-18 18:47:08 +02:00
|
|
|
|
|
|
|
# Disable forarding 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
|
|
|
|
|
2011-07-29 16:50:12 +02:00
|
|
|
RULES=$(/usr/bin/xenstore-read $XENSTORE_IPTABLES_HEADER)
|
|
|
|
IPTABLES_SAVE=$(/sbin/iptables-save | sed '/^\*filter/,/^COMMIT/d')
|
2012-02-13 15:47:34 +01:00
|
|
|
OUT=`echo -e "$RULES\n$IPTABLES_SAVE" | /sbin/iptables-restore 2>&1 || true`
|
2011-07-29 16:50:12 +02:00
|
|
|
|
2013-03-14 04:23:32 +01:00
|
|
|
for i in $(xenstore-list qubes-iptables-domainrules) ; do
|
|
|
|
RULES=$(/usr/bin/xenstore-read qubes-iptables-domainrules/"$i")
|
2012-02-13 15:47:34 +01:00
|
|
|
ERRS=`echo -e "$RULES" | /sbin/iptables-restore -n 2>&1 || true`
|
2014-02-22 01:23:27 +01:00
|
|
|
echo "Failed applying rules for $i: $ERRS" >&2
|
2012-02-13 15:47:34 +01:00
|
|
|
OUT="$OUT$ERRS"
|
2011-07-29 16:50:12 +02:00
|
|
|
done
|
|
|
|
/usr/bin/xenstore-write $XENSTORE_ERROR "$OUT"
|
|
|
|
if [ "$OUT" ]; then
|
|
|
|
DISPLAY=:0 /usr/bin/notify-send -t 3000 "Firewall loading error ($HOSTNAME)" "$OUT" || :
|
|
|
|
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
|