qubes-firewall 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. FIRST_TIME=yes
  13. while true; do
  14. echo "1" > /proc/sys/net/ipv4/ip_forward
  15. if [ "$FIRST_TIME" ]; then
  16. FIRST_TIME=
  17. TRIGGER=reload
  18. else
  19. # Wait for changes in xenstore file
  20. /usr/bin/xenstore-watch-qubes $XENSTORE_IPTABLES
  21. TRIGGER=$(/usr/bin/xenstore-read $XENSTORE_IPTABLES)
  22. fi
  23. if ! [ "$TRIGGER" = "reload" ]; then continue ; fi
  24. # Disable forarding to prevent potential "leaks" that might
  25. # be bypassing the firewall or some proxy service (e.g. tor)
  26. # during the time when the rules are being (re)applied
  27. echo "0" > /proc/sys/net/ipv4/ip_forward
  28. RULES=$(/usr/bin/xenstore-read $XENSTORE_IPTABLES_HEADER)
  29. IPTABLES_SAVE=$(/sbin/iptables-save | sed '/^\*filter/,/^COMMIT/d')
  30. OUT=`echo -e "$RULES\n$IPTABLES_SAVE" | /sbin/iptables-restore 2>&1 || true`
  31. for i in $(xenstore-list qubes-iptables-domainrules) ; do
  32. RULES=$(/usr/bin/xenstore-read qubes-iptables-domainrules/"$i")
  33. ERRS=`echo -e "$RULES" | /sbin/iptables-restore -n 2>&1 || true`
  34. OUT="$OUT$ERRS"
  35. done
  36. /usr/bin/xenstore-write $XENSTORE_ERROR "$OUT"
  37. if [ "$OUT" ]; then
  38. DISPLAY=:0 /usr/bin/notify-send -t 3000 "Firewall loading error ($HOSTNAME)" "$OUT" || :
  39. fi
  40. # Check if user didn't define some custom rules to be applied as well...
  41. [ -x /rw/config/qubes-firewall-user-script ] && /rw/config/qubes-firewall-user-script
  42. # XXX: Backward compatibility
  43. [ -x /rw/config/qubes_firewall_user_script ] && /rw/config/qubes_firewall_user_script
  44. done