v-update-firewall 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/bash
  2. # info: update system firewall rules
  3. # options: NONE
  4. #
  5. # The function updates iptables rules
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Defining absolute path for iptables and modprobe
  10. iptables="/sbin/iptables"
  11. modprobe="/sbin/modprobe"
  12. # Includes
  13. source /etc/profile.d/vesta.sh
  14. source $VESTA/func/main.sh
  15. source $VESTA/conf/vesta.conf
  16. #----------------------------------------------------------#
  17. # Verifications #
  18. #----------------------------------------------------------#
  19. is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
  20. #----------------------------------------------------------#
  21. # Action #
  22. #----------------------------------------------------------#
  23. # Checking local IPv4 rules
  24. rules="$VESTA/data/firewall/rules.conf"
  25. ports="$VESTA/data/firewall/ports.conf"
  26. if [ ! -e "$rules" ]; then
  27. exit
  28. fi
  29. # Checking conntrack module avaiabilty
  30. $modprobe nf_conntrack >/dev/null 2>&1
  31. $modprobe nf_conntrack_ftp >/dev/null 2>&1
  32. if [ $? -ne 0 ]; then
  33. stateful='no'
  34. fi
  35. # Creating temporary file
  36. tmp=$(mktemp)
  37. # Flushing INPUT chain
  38. echo "$iptables -P INPUT ACCEPT" >> $tmp
  39. echo "$iptables -F INPUT" >> $tmp
  40. # Pasring iptables rules
  41. IFS=$'\n'
  42. for line in $(sort -r -n -k 2 -t \' $rules); do
  43. eval $line
  44. if [ "$SUSPENDED" = 'no' ]; then
  45. proto="-p $PROTOCOL"
  46. port="--dport $PORT"
  47. ip="-s $IP"
  48. state=""
  49. action="-j $ACTION"
  50. # Adding multiport module
  51. if [[ "$PORT" =~ ,|-|: ]] ; then
  52. port="-m multiport --dports ${PORT//-/:}"
  53. fi
  54. # Accepting all dst ports
  55. if [[ "$PORT" = "0" ]] || [ "$PROTOCOL" = 'ICMP' ]; then
  56. port=""
  57. fi
  58. # Checking FTP for contrack module
  59. if [ "$TYPE" = "FTP" ] || [ "$PORT" = '21' ]; then
  60. if [ "$stateful" != 'no' ]; then
  61. state="-m conntrack --ctstate NEW"
  62. else
  63. port="-m multiport --dports 20,21,12000:12100"
  64. fi
  65. ftp="yes"
  66. fi
  67. # Adding firewall rule
  68. echo "$iptables -A INPUT $proto $port $ip $state $action" >> $tmp
  69. fi
  70. done
  71. # Handling local traffic
  72. for ip in $(ls $VESTA/data/ips); do
  73. echo "$iptables -A INPUT -s $ip -j ACCEPT" >> $tmp
  74. done
  75. echo "$iptables -A INPUT -s 127.0.0.1 -j ACCEPT" >> $tmp
  76. IFS=$'\n'
  77. for p_rule in $(cat $ports); do
  78. eval $p_rule
  79. rule="$iptables -A INPUT -p $PROTOCOL"
  80. echo "$rule --sport $PORT -j ACCEPT" >> $tmp
  81. done
  82. # Enabling stateful support
  83. if [ "$stateful" != 'no' ]; then
  84. str="$iptables -A INPUT -p tcp -m state"
  85. str="$str --state ESTABLISHED,RELATED -j ACCEPT"
  86. echo "$str" >> $tmp
  87. fi
  88. # Switching chain policy to DROP
  89. echo "$iptables -P INPUT DROP" >> $tmp
  90. # Adding vesta chain
  91. echo "$iptables -N vesta" >> $tmp
  92. # Applying rules
  93. bash $tmp 2>/dev/null
  94. # Deleting temporary file
  95. rm -f $tmp
  96. # Checking custom trigger
  97. if [ -x "$VESTA/data/firewall/custom.sh" ]; then
  98. bash $VESTA/data/firewall/custom.sh
  99. fi
  100. # Checking fail2ban support
  101. if [ ! -z "$FIREWALL_EXTENSION" ]; then
  102. chains=$(cat $VESTA/data/firewall/chains.conf 2>/dev/null)
  103. fi
  104. for chain in $chains; do
  105. eval $chain
  106. if [[ "$PORT" =~ ,|-|: ]] ; then
  107. port="-m multiport --dports $PORT"
  108. else
  109. port="--dport $PORT"
  110. fi
  111. echo "$iptables -I INPUT -p $PROTOCOL $port -j fail2ban-$CHAIN" > $tmp
  112. bash $tmp
  113. rm -f $tmp
  114. done
  115. # Saving rules to the master iptables file
  116. if [ -e "/etc/redhat-release" ]; then
  117. /sbin/iptables-save > /etc/sysconfig/iptables
  118. if [ -z "$(ls /etc/rc3.d/S*iptables 2>/dev/null)" ]; then
  119. /sbin/chkconfig iptables on
  120. fi
  121. else
  122. /sbin/iptables-save > /etc/iptables.rules
  123. preup="/etc/network/if-pre-up.d/iptables"
  124. if [ ! -e "$preup" ]; then
  125. echo '#!/bin/sh' > $preup
  126. echo "/sbin/iptables-restore < /etc/iptables.rules" >> $preup
  127. echo "exit 0" >> $preup
  128. chmod +x $preup
  129. fi
  130. fi
  131. #----------------------------------------------------------#
  132. # Vesta #
  133. #----------------------------------------------------------#
  134. exit