v-add-firewall-chain 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. # Get vesta port by reading nginx.conf
  19. vestaport=$(grep 'listen' /usr/local/vesta/nginx/conf/nginx.conf | awk '{print $2}' | sed "s|;||")
  20. if [ -z "$vestaport" ]; then
  21. vestaport=8083
  22. fi
  23. # Includes
  24. source $VESTA/func/main.sh
  25. source $VESTA/conf/vesta.conf
  26. #----------------------------------------------------------#
  27. # Verifications #
  28. #----------------------------------------------------------#
  29. check_args '1' "$#" 'CHAIN [PORT] [PROTOCOL]'
  30. is_format_valid 'chain'
  31. is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
  32. #----------------------------------------------------------#
  33. # Action #
  34. #----------------------------------------------------------#
  35. # Checking known chains
  36. case $chain in
  37. SSH) # Get ssh port by reading ssh config file.
  38. sshport=$(grep '^Port ' /etc/ssh/sshd_config | head -1 | cut -d ' ' -f 2)
  39. if [ -z "$sshport" ]; then
  40. sshport=22
  41. fi
  42. port=$sshport;
  43. protocol=TCP ;;
  44. FTP) port=21; protocol=TCP ;;
  45. MAIL) port='25,465,587,2525,110,995,143,993'; protocol=TCP ;;
  46. DNS) port=53; protocol=UDP ;;
  47. WEB) port='80,443'; protocol=TCP ;;
  48. DB) port='3306,5432'; protocol=TCP ;;
  49. VESTA) port=$vestaport; protocol=TCP ;;
  50. *) check_args '2' "$#" 'CHAIN PORT' ;;
  51. esac
  52. # Adding chain
  53. $iptables -N fail2ban-$chain 2>/dev/null
  54. if [ $? -eq 0 ]; then
  55. $iptables -A fail2ban-$chain -j RETURN
  56. # Adding multiport module
  57. if [[ "$port" =~ ,|-|: ]] ; then
  58. port_str="-m multiport --dports $port"
  59. else
  60. port_str="--dport $port"
  61. fi
  62. $iptables -I INPUT -p $protocol $port_str -j fail2ban-$chain
  63. fi
  64. # Preserving chain
  65. chains=$VESTA/data/firewall/chains.conf
  66. check_chain=$(grep "CHAIN='$chain'" $chains 2>/dev/null)
  67. if [ -z "$check_chain" ]; then
  68. echo "CHAIN='$chain' PORT='$port' PROTOCOL='$protocol'" >> $chains
  69. fi
  70. # Changing permissions
  71. chmod 660 $chains
  72. #----------------------------------------------------------#
  73. # Vesta #
  74. #----------------------------------------------------------#
  75. # Logging
  76. log_event "$OK" "$ARGUMENTS"
  77. exit