v-update-firewall 4.3 KB

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