v-change-firewall-rule 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/bash
  2. # info: change firewall rule
  3. # options: RULE ACTION IP PORT [PROTOCOL] [COMMENT]
  4. #
  5. # The function is used for changing existing firewall rule.
  6. # It fully replace rule with new one but keeps same id.
  7. #----------------------------------------------------------#
  8. # Variable&Function #
  9. #----------------------------------------------------------#
  10. # Importing system variables
  11. source /etc/profile
  12. # Argument definition
  13. rule=$1
  14. action=$(echo $2|tr '[:lower:]' '[:upper:]')
  15. ip=$3
  16. port_ext=$4
  17. protocol=${5-TCP}
  18. protocol=$(echo $protocol|tr '[:lower:]' '[:upper:]')
  19. comment=$6
  20. # Includes
  21. source $VESTA/func/main.sh
  22. source $VESTA/conf/vesta.conf
  23. # Sort function
  24. sort_fw_rules() {
  25. cat $VESTA/data/firewall/rules.conf |\
  26. sort -n -k 2 -t \' > $VESTA/data/firewall/rules.conf.tmp
  27. mv -f $VESTA/data/firewall/rules.conf.tmp \
  28. $VESTA/data/firewall/rules.conf
  29. }
  30. #----------------------------------------------------------#
  31. # Verifications #
  32. #----------------------------------------------------------#
  33. check_args '5' "$#" 'RULE ACTION IP PORT [PROTOCOL] [COMMENT]'
  34. is_format_valid 'rule' 'action' 'protocol' 'port_ext' 'ip'
  35. if [ ! -z "$comment" ]; then
  36. is_format_valid 'comment'
  37. fi
  38. is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
  39. is_object_valid '../../data/firewall/rules' 'RULE' "$rule"
  40. #----------------------------------------------------------#
  41. # Action #
  42. #----------------------------------------------------------#
  43. # Generating timestamp
  44. time_n_date=$(date +'%T %F')
  45. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  46. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  47. # Concatenating firewall rule
  48. str="RULE='$rule' ACTION='$action' PROTOCOL='$protocol' PORT='$port_ext'"
  49. str="$str IP='$ip' COMMENT='$comment' SUSPENDED='no'"
  50. str="$str TIME='$time' DATE='$date'"
  51. # Deleting old rule
  52. sed -i "/RULE='$rule' /d" $VESTA/data/firewall/rules.conf
  53. # Adding new
  54. echo "$str" >> $VESTA/data/firewall/rules.conf
  55. # Sorting firewall rules by id number
  56. sort_fw_rules
  57. # Updating system firewall
  58. $BIN/v-update-firewall
  59. #----------------------------------------------------------#
  60. # Vesta #
  61. #----------------------------------------------------------#
  62. # Logging
  63. log_event "$OK" "$ARGUMENTS"
  64. exit