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