ufw-init 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/sh
  2. #
  3. # ufw-init: helper script to be used by ufw itself
  4. #
  5. # Copyright 2008-2015 Canonical Ltd.
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License version 3,
  9. # as published by the Free Software Foundation.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. set -e
  20. # FIXME: this shouldn't be ordered
  21. rootdir=
  22. if [ "$1" = "--rootdir" ] && [ -s "$2" ]; then
  23. rootdir="$2/" # ensure trailing slash
  24. shift 2
  25. fi
  26. datadir=
  27. if [ "$1" = "--datadir" ] && [ -s "$2" ]; then
  28. datadir="$2/" # ensure trailing slash
  29. shift 2
  30. fi
  31. export DATA_DIR="$datadir"
  32. # Debian/Ubuntu: small boot speed improvement
  33. . "${rootdir}/etc/ufw/ufw.conf"
  34. if [ "$1" = "start" ] && [ "$2" = "quiet" ] && [ "$ENABLED" = "no" ]; then
  35. exit 0
  36. fi
  37. if [ -s "${rootdir}/lib/ufw/ufw-init-functions" ]; then
  38. . "${rootdir}/lib/ufw/ufw-init-functions"
  39. else
  40. echo "Could not find ${rootdir}/lib/ufw/ufw-init-functions (aborting)"
  41. exit 1
  42. fi
  43. case "$1" in
  44. start)
  45. # process multiple error strings
  46. ret=0
  47. output=`ufw_start` || ret="$?"
  48. test -n "$output" && echo "$output" | while read line ; do
  49. if [ "$2" = "quiet" ] || [ "$QUIET" = "yes" ]; then
  50. echo "$line" | grep -q "Skip starting" && continue
  51. fi
  52. echo "$line"
  53. done
  54. exit "$ret"
  55. ;;
  56. stop)
  57. ufw_stop || exit "$?"
  58. ;;
  59. force-stop)
  60. ufw_stop --force || exit "$?"
  61. ;;
  62. restart|force-reload)
  63. ufw_reload || exit "$?"
  64. ;;
  65. status)
  66. ufw_status || exit "$?"
  67. # If before.init and after.init support 'status', just display them after
  68. # ufw_status() so it is prettier
  69. if [ -x "$RULES_PATH/before.init" ]; then
  70. "$RULES_PATH/before.init" status || exit "$?"
  71. fi
  72. if [ -x "$RULES_PATH/after.init" ]; then
  73. "$RULES_PATH/after.init" status || exit "$?"
  74. fi
  75. ;;
  76. flush-all)
  77. # Use sparingly. It flushes the built-in chains, deletes all non-builtin
  78. # chains and resets the policy to ACCEPT
  79. if [ -x "$RULES_PATH/before.init" ]; then
  80. "$RULES_PATH/before.init" flush-all || exit "$?"
  81. fi
  82. flush_builtins || exit "$?"
  83. if [ -x "$RULES_PATH/after.init" ]; then
  84. "$RULES_PATH/after.init" flush-all || exit "$?"
  85. fi
  86. ;;
  87. *)
  88. echo "Usage: /lib/ufw/ufw-init {start|stop|restart|force-reload|force-stop|flush-all|status}"
  89. exit 1
  90. ;;
  91. esac