v-add-firewall-chain 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/bash
  2. # info: add firewall chain
  3. # options: CHAIN [PORT] [PROTOCOL] [PROTOCOL]
  4. #
  5. # The function adds new rule to system firewall
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Importing system variables
  10. source /etc/profile
  11. # Argument definition
  12. chain=$(echo $1 | tr '[:lower:]' '[:upper:]')
  13. port=$2
  14. protocol=${4-TCP}
  15. protocol=$(echo $protocol|tr '[:lower:]' '[:upper:]')
  16. # Defining absolute path to iptables
  17. iptables="/sbin/iptables"
  18. # Includes
  19. source $VESTA/func/main.sh
  20. source $VESTA/conf/vesta.conf
  21. #----------------------------------------------------------#
  22. # Verifications #
  23. #----------------------------------------------------------#
  24. check_args '1' "$#" 'CHAIN [PORT] [PROTOCOL]'
  25. is_format_valid 'chain'
  26. is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
  27. #----------------------------------------------------------#
  28. # Action #
  29. #----------------------------------------------------------#
  30. # Checking known chains
  31. case $chain in
  32. SSH) port=22; protocol=TCP ;;
  33. FTP) port=21; protocol=TCP ;;
  34. MAIL) port='25,465,587,2525,110,995,143,993'; protocol=TCP ;;
  35. DNS) port=53; protocol=UDP ;;
  36. WEB) port='80,443'; protocol=TCP ;;
  37. DB) port='3306,5432'; protocol=TCP ;;
  38. VESTA) port=8083; protocol=TCP ;;
  39. *) check_args '2' "$#" 'CHAIN PORT' ;;
  40. esac
  41. # Adding chain
  42. $iptables -N fail2ban-$chain 2>/dev/null
  43. if [ $? -eq 0 ]; then
  44. $iptables -A fail2ban-$chain -j RETURN
  45. # Adding multiport module
  46. if [[ "$port" =~ ,|-|: ]] ; then
  47. port_str="-m multiport --dports $port"
  48. else
  49. port_str="--dport $port"
  50. fi
  51. $iptables -I INPUT -p $protocol $port_str -j fail2ban-$chain
  52. fi
  53. # Preserving chain
  54. chains=$VESTA/data/firewall/chains.conf
  55. check_chain=$(grep "CHAIN='$chain'" $chains 2>/dev/null)
  56. if [ -z "$check_chain" ]; then
  57. echo "CHAIN='$chain' PORT='$port' PROTOCOL='$protocol'" >> $chains
  58. fi
  59. # Changing permissions
  60. chmod 660 $chains
  61. #----------------------------------------------------------#
  62. # Vesta #
  63. #----------------------------------------------------------#
  64. # Logging
  65. log_event "$OK" "$ARGUMENTS"
  66. exit