qubes-netwatcher 974 B

12345678910111213141516171819202122232425262728293031
  1. #!/bin/bash
  2. set -e
  3. PIDFILE=/var/run/qubes/qubes-netwatcher.pid
  4. CURR_NETCFG=""
  5. # PIDfile handling
  6. [[ -e $PIDFILE ]] && kill -s 0 $(<$PIDFILE) 2>/dev/null && exit 0
  7. echo $$ >$PIDFILE
  8. trap 'exit 0' SIGTERM
  9. while true; do
  10. NET_DOMID=$(/usr/bin/xenstore-read qubes-netvm-domid || :)
  11. if [[ -n "$NET_DOMID" ]] && [[ $NET_DOMID -gt 0 ]]; then
  12. UNTRUSTED_NETCFG=$(/usr/bin/xenstore-read /local/domain/$NET_DOMID/qubes-netvm-external-ip || :)
  13. # UNTRUSTED_NETCFG is not parsed in any way
  14. # thus, no sanitization ready
  15. # but be careful when passing it to other shell scripts
  16. if [[ "$UNTRUSTED_NETCFG" != "$CURR_NETCFG" ]]; then
  17. /sbin/service qubes-firewall stop
  18. /sbin/service qubes-firewall start
  19. CURR_NETCFG="$UNTRUSTED_NETCFG"
  20. /usr/bin/xenstore-write qubes-netvm-external-ip "$CURR_NETCFG"
  21. fi
  22. /usr/bin/xenstore-watch -n 3 /local/domain/$NET_DOMID/qubes-netvm-external-ip qubes-netvm-domid
  23. else
  24. /usr/bin/xenstore-watch -n 2 qubes-netvm-domid
  25. fi
  26. done