v-add-firewall-rule 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/bash
  2. # info: add firewall rule
  3. # options: ACTION IP PORT [PROTOCOL] [COMMENT] [RULE]
  4. #
  5. # The function adds new rule to system firewall
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Importing system variables
  10. source /etc/profile
  11. # Argument definition
  12. action=$(echo $1|tr '[:lower:]' '[:upper:]')
  13. ip=$2
  14. port_ext=$3
  15. protocol=${4-TCP}
  16. protocol=$(echo $protocol|tr '[:lower:]' '[:upper:]')
  17. comment=$5
  18. rule=$6
  19. # Includes
  20. source $VESTA/func/main.sh
  21. source $VESTA/conf/vesta.conf
  22. # Get next firewall rule id
  23. get_next_fw_rule() {
  24. if [ -z "$rule" ]; then
  25. curr_str=$(grep "RULE=" $VESTA/data/firewall/rules.conf |\
  26. cut -f 2 -d \' | sort -n | tail -n1)
  27. rule="$((curr_str +1))"
  28. fi
  29. }
  30. sort_fw_rules() {
  31. cat $VESTA/data/firewall/rules.conf |\
  32. sort -n -k 2 -t \' > $VESTA/data/firewall/rules.conf.tmp
  33. mv -f $VESTA/data/firewall/rules.conf.tmp \
  34. $VESTA/data/firewall/rules.conf
  35. }
  36. #----------------------------------------------------------#
  37. # Verifications #
  38. #----------------------------------------------------------#
  39. check_args '3' "$#" 'ACTION IP PORT [PROTOCOL] [COMMENT] [RULE]'
  40. is_format_valid 'action' 'protocol' 'port_ext' 'ip'
  41. is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
  42. get_next_fw_rule
  43. is_format_valid 'rule'
  44. is_object_new '../../data/firewall/rules' 'RULE' "$rule"
  45. if [ ! -z "$comment" ]; then
  46. is_format_valid 'comment'
  47. fi
  48. #----------------------------------------------------------#
  49. # Action #
  50. #----------------------------------------------------------#
  51. # Generating timestamp
  52. time_n_date=$(date +'%T %F')
  53. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  54. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  55. # Concatenating rule
  56. str="RULE='$rule' ACTION='$action' PROTOCOL='$protocol' PORT='$port_ext'"
  57. str="$str IP='$ip' COMMENT='$comment' SUSPENDED='no'"
  58. str="$str TIME='$time' DATE='$date'"
  59. # Adding to config
  60. echo "$str" >> $VESTA/data/firewall/rules.conf
  61. # Changing permissions
  62. chmod 660 $VESTA/data/firewall/rules.conf
  63. # Sorting firewall rules by id number
  64. sort_fw_rules
  65. # Updating system firewall
  66. $BIN/v-update-firewall
  67. #----------------------------------------------------------#
  68. # Vesta #
  69. #----------------------------------------------------------#
  70. # Logging
  71. log_event "$OK" "$ARGUMENTS"
  72. exit