qubes-firewall 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 forwarding 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. echo "Failed applying rules for $i: $ERRS" >&2
  35. OUT="$OUT$ERRS"
  36. done
  37. /usr/bin/xenstore-write $XENSTORE_ERROR "$OUT"
  38. if [ "$OUT" ]; then
  39. DISPLAY=:0 /usr/bin/notify-send -t 3000 "Firewall loading error ($HOSTNAME)" "$OUT" || :
  40. fi
  41. # Check if user didn't define some custom rules to be applied as well...
  42. [ -x /rw/config/qubes-firewall-user-script ] && /rw/config/qubes-firewall-user-script
  43. # XXX: Backward compatibility
  44. [ -x /rw/config/qubes_firewall_user_script ] && /rw/config/qubes_firewall_user_script
  45. done