v-change-firewall-rule 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. validate_format 'rule' 'action' 'protocol' 'port_ext' 'ip'
  35. if [ ! -z "$comment" ]; then
  36. validate_format '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. # Concatenating firewall rule
  44. str="RULE='$rule' ACTION='$action' PROTOCOL='$protocol' PORT='$port_ext'"
  45. str="$str IP='$ip' COMMENT='$comment' SUSPENDED='no'"
  46. str="$str TIME='$TIME' DATE='$DATE'"
  47. # Deleting old rule
  48. sed -i "/RULE='$rule' /d" $VESTA/data/firewall/rules.conf
  49. # Adding new
  50. echo "$str" >> $VESTA/data/firewall/rules.conf
  51. # Sorting firewall rules by id number
  52. sort_fw_rules
  53. # Updating system firewall
  54. $BIN/v-update-firewall
  55. #----------------------------------------------------------#
  56. # Vesta #
  57. #----------------------------------------------------------#
  58. # Logging
  59. log_event "$OK" "$EVENT"
  60. exit