qubes_firewall 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/bash
  2. set -e
  3. PIDFILE=/var/run/qubes/qubes_firewall.pid
  4. XENSTORE_IPTABLES=qubes_iptables
  5. XENSTORE_IPTABLES_HEADER=qubes_iptables_header
  6. XENSTORE_ERROR=qubes_iptables_error
  7. OLD_RULES=""
  8. # PIDfile handling
  9. [[ -e $PIDFILE ]] && kill -s 0 $(<$PIDFILE) 2>/dev/null && exit 0
  10. echo $$ >$PIDFILE
  11. trap 'exit 0' SIGTERM
  12. while true; do
  13. echo "1" > /proc/sys/net/ipv4/ip_forward
  14. # Wait for changes in xenstore file
  15. /usr/bin/xenstore-watch-qubes $XENSTORE_IPTABLES
  16. TRIGGER=$(/usr/bin/xenstore-read $XENSTORE_IPTABLES)
  17. if ! [ "$TRIGGER" = "reload" ]; then continue ; fi
  18. # Disable forarding to prevent potential "leaks" that might
  19. # be bypassing the firewall or some proxy service (e.g. tor)
  20. # during the time when the rules are being (re)applied
  21. echo "0" > /proc/sys/net/ipv4/ip_forward
  22. RULES=$(/usr/bin/xenstore-read $XENSTORE_IPTABLES_HEADER)
  23. IPTABLES_SAVE=$(/sbin/iptables-save | sed '/^\*filter/,/^COMMIT/d')
  24. OUT=`echo -e "$RULES\n$IPTABLES_SAVE" | /sbin/iptables-restore 2>&1 || :`
  25. for i in $(xenstore-list qubes_iptables_domainrules) ; do
  26. RULES=$(/usr/bin/xenstore-read qubes_iptables_domainrules/"$i")
  27. ERRS=`echo -e "$RULES" | /sbin/iptables-restore -n 2>&1 || :`
  28. OUT="$OUT""$ERRS"
  29. done
  30. /usr/bin/xenstore-write $XENSTORE_ERROR "$OUT"
  31. if [ "$OUT" ]; then
  32. DISPLAY=:0 /usr/bin/notify-send -t 3000 "Firewall loading error ($HOSTNAME)" "$OUT" || :
  33. fi
  34. # Check if user didn't define some custom rules to be applied as well...
  35. [ -x /rw/config/qubes_firewall_user_script ] && /rw/config/qubes_firewall_user_script
  36. done