ufw.initd 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/openrc-run
  2. # Copyright 1999-2011 Gentoo Foundation
  3. # Distributed under the terms of the GNU General Public License v2
  4. # $Header: /var/cvsroot/gentoo-x86/net-firewall/ufw/files/ufw-2.initd,v 1.1 2011/07/24 11:18:22 pva Exp $
  5. depend() {
  6. before net
  7. provide firewall
  8. }
  9. start() {
  10. ebegin "Starting ufw"
  11. _source_file || { eend $?; return $?; }
  12. local enabled_in_cfg ret
  13. _check_if_enabled_in_cfg
  14. enabled_in_cfg=$?
  15. # Avoid "Firewall already started, use 'force-reload'" message that
  16. # appears if `ufw enable' had been run before start().
  17. if _status_quiet; then
  18. eend 0
  19. return
  20. fi
  21. # The ufw_start function does the same: if ufw is disabled using `ufw disable',
  22. # ufw_start would not start ufw and return 0, so let's handle this case.
  23. case $enabled_in_cfg in
  24. 0)
  25. ufw_start
  26. ret=$?
  27. eend $ret "Failed to start ufw."
  28. ;;
  29. 1)
  30. # see /etc/conf.d/<name>
  31. if [ "${ufw_nonfatal_if_disabled:-no}" != "yes" ]; then
  32. ret=1
  33. eend $ret "Not starting firewall (not enabled), use \"ufw enable\" first."
  34. else
  35. ret=0
  36. eend 0
  37. fi
  38. ;;
  39. 2)
  40. ret=1
  41. eend $ret "Failed to start ufw."
  42. ;;
  43. esac
  44. return $ret
  45. }
  46. stop() {
  47. ebegin "Stopping ufw"
  48. _source_file || { eend $?; return $?; }
  49. local enabled_in_cfg ret
  50. _check_if_enabled_in_cfg
  51. enabled_in_cfg=$?
  52. # Same as above (unless --force is passed to ufw_stop).
  53. case $enabled_in_cfg in
  54. 0)
  55. ufw_stop
  56. ret=$?
  57. ;;
  58. 1)
  59. einfo "INFO: ufw is configured to be disabled"
  60. ufw_stop --force
  61. ret=$?
  62. ;;
  63. 2)
  64. ret=1
  65. ;;
  66. esac
  67. eend $ret "Failed to stop ufw."
  68. return $ret
  69. }
  70. _status_quiet() {
  71. # return values: 0 - started, 1 - stopped, 2 - error
  72. # Does not execute _source_file.
  73. local ret
  74. ufw_status > /dev/null
  75. ret=$?
  76. # Return values for ufw_status come from /usr/lib/ufw/ufw-init-functions.
  77. case $ret in
  78. 0) return 0 ;;
  79. 3) return 1 ;;
  80. *) return 2 ;;
  81. esac
  82. }
  83. _source_file() {
  84. local sourced_f="/usr/lib/ufw/ufw-init-functions"
  85. if [ ! -f "$sourced_f" ]; then
  86. eerror "Cannot find file $sourced_f!"
  87. return 1
  88. fi
  89. local _path=$PATH
  90. if ! source "$sourced_f"; then
  91. # PATH can be broken here, fix it...
  92. PATH=$_path
  93. eerror "Error sourcing file $sourced_f"
  94. return 1
  95. fi
  96. if [ -z "$PATH" ]; then
  97. PATH=$_path
  98. else
  99. PATH="${PATH}:${_path}"
  100. fi
  101. return 0
  102. }
  103. _check_if_enabled_in_cfg() {
  104. # Check if user has enabled the firewall with "ufw enable".
  105. # Return 0 if firewall enabled in configuration file, 1 otherwise, 2 on error.
  106. local sourced_f="/etc/ufw/ufw.conf"
  107. if [ ! -f "$sourced_f" ]; then
  108. eerror "Cannot find file $sourced_f!"
  109. return 2
  110. fi
  111. if ! source "$sourced_f"; then
  112. eerror "Error sourcing file $sourced_f"
  113. return 2
  114. fi
  115. if [ "$ENABLED" = "yes" ] || [ "$ENABLED" = "YES" ]; then
  116. return 0
  117. else
  118. return 1
  119. fi
  120. }