v-update-sys-firewall 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_ipv4.conf"
  25. if [ ! -e "$rules" ]; then
  26. exit
  27. fi
  28. # Checking conntrack module avaiabilty
  29. $modprobe nf_conntrack >/dev/null 2>&1
  30. $modprobe nf_conntrack_ftp >/dev/null 2>&1
  31. if [ $? -ne 0 ]; then
  32. stateful='no'
  33. fi
  34. # Creating temporary file
  35. tmp=$(mktemp)
  36. # Flushing INPUT chain
  37. echo "$iptables -P INPUT ACCEPT" >> $tmp
  38. echo "$iptables -F INPUT" >> $tmp
  39. # Pasring iptables rules
  40. IFS=$'\n'
  41. for line in $(sort -r -n -k 2 -t \' $rules); do
  42. eval $line
  43. if [ "$SUSPENDED" = 'no' ]; then
  44. chain="-A INPUT"
  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 $chain $proto $port $ip $state $action" >> $tmp
  69. fi
  70. done
  71. # Handling DNS replies
  72. proto="-p udp"
  73. port="--sport 53"
  74. action="-j ACCEPT"
  75. echo "$iptables $chain $proto $port $state $action" >> $tmp
  76. # Enabling stateful firewall
  77. if [ "$stateful" != 'no' ]; then
  78. proto="-p tcp"
  79. state="-m state --state ESTABLISHED,RELATED"
  80. action="-j ACCEPT"
  81. echo "$iptables $chain $proto $state $action" >> $tmp
  82. fi
  83. # Switching chain policy to DROP
  84. echo "$iptables -P INPUT DROP" >> $tmp
  85. # Applying rules
  86. bash $tmp
  87. # Saving rules to the master iptables file
  88. if [ -e "/etc/redhat-release" ]; then
  89. /sbin/iptables-save > /etc/sysconfig/iptables
  90. if [ -z "$(ls /etc/rc3.d/S*iptables 2>/dev/null)" ]; then
  91. /sbin/chkconfig iptables on
  92. fi
  93. else
  94. sbin/iptables-save > /etc/iptables.up.rules
  95. fi
  96. #----------------------------------------------------------#
  97. # Vesta #
  98. #----------------------------------------------------------#
  99. exit