v-add-firewall-rule 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. validate_format 'action' 'protocol' 'port_ext' 'ip'
  41. is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
  42. get_next_fw_rule
  43. validate_format 'rule'
  44. is_object_new '../../data/firewall/rules' 'RULE' "$rule"
  45. if [ ! -z "$comment" ]; then
  46. validate_format 'comment'
  47. fi
  48. #----------------------------------------------------------#
  49. # Action #
  50. #----------------------------------------------------------#
  51. # Concatenating rule
  52. str="RULE='$rule' ACTION='$action' PROTOCOL='$protocol' PORT='$port_ext'"
  53. str="$str IP='$ip' COMMENT='$comment' SUSPENDED='no'"
  54. str="$str TIME='$TIME' DATE='$DATE'"
  55. # Adding to config
  56. echo "$str" >> $VESTA/data/firewall/rules.conf
  57. # Changing permissions
  58. chmod 660 $VESTA/data/firewall/rules.conf
  59. # Sorting firewall rules by id number
  60. sort_fw_rules
  61. # Updating system firewall
  62. $BIN/v-update-firewall
  63. #----------------------------------------------------------#
  64. # Vesta #
  65. #----------------------------------------------------------#
  66. # Logging
  67. log_event "$OK" "$EVENT"
  68. exit