1
0
mirror of https://github.com/serghey-rodin/vesta.git synced 2025-03-11 20:26:30 -07:00
vesta/bin/v-change-firewall-rule

86 lines
2.4 KiB
Plaintext
Raw Normal View History

2014-09-17 00:32:25 +03:00
#!/bin/bash
# info: change firewall rule
2014-10-05 14:52:15 +03:00
# options: RULE ACTION IP PORT [PROTOCOL] [COMMENT]
2014-09-17 00:32:25 +03:00
#
# The function is used for changing existing firewall rule.
# It fully replace rule with new one but keeps same id.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
2014-10-05 14:52:15 +03:00
# Importing system variables
source /etc/profile
2015-11-06 17:38:58 +02:00
# Argument definition
2014-09-17 00:32:25 +03:00
rule=$1
action=$(echo $2|tr '[:lower:]' '[:upper:]')
2014-10-05 14:52:15 +03:00
ip=$3
2014-09-17 00:32:25 +03:00
port_ext=$4
2014-10-05 14:52:15 +03:00
protocol=${5-TCP}
protocol=$(echo $protocol|tr '[:lower:]' '[:upper:]')
2014-09-17 00:32:25 +03:00
comment=$6
# Includes
source $VESTA/func/main.sh
source $VESTA/conf/vesta.conf
# Sort function
sort_fw_rules() {
2014-10-05 14:52:15 +03:00
cat $VESTA/data/firewall/rules.conf |\
sort -n -k 2 -t \' > $VESTA/data/firewall/rules.conf.tmp
mv -f $VESTA/data/firewall/rules.conf.tmp \
$VESTA/data/firewall/rules.conf
2014-09-17 00:32:25 +03:00
}
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
2014-10-05 14:52:15 +03:00
check_args '5' "$#" 'RULE ACTION IP PORT [PROTOCOL] [COMMENT]'
is_format_valid 'rule' 'action' 'protocol' 'port_ext' 'ip'
2014-10-05 14:52:15 +03:00
if [ ! -z "$comment" ]; then
is_format_valid 'comment'
2014-10-05 14:52:15 +03:00
fi
2014-09-17 00:32:25 +03:00
is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
2014-10-05 14:52:15 +03:00
is_object_valid '../../data/firewall/rules' 'RULE' "$rule"
2014-09-17 00:32:25 +03:00
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Generating timestamp
time_n_date=$(date +'%T %F')
time=$(echo "$time_n_date" |cut -f 1 -d \ )
date=$(echo "$time_n_date" |cut -f 2 -d \ )
2014-09-17 00:32:25 +03:00
# Concatenating firewall rule
str="RULE='$rule' ACTION='$action' PROTOCOL='$protocol' PORT='$port_ext'"
str="$str IP='$ip' COMMENT='$comment' SUSPENDED='no'"
str="$str TIME='$time' DATE='$date'"
2014-09-17 00:32:25 +03:00
# Deleting old rule
2014-10-05 14:52:15 +03:00
sed -i "/RULE='$rule' /d" $VESTA/data/firewall/rules.conf
2014-09-17 00:32:25 +03:00
# Adding new
2014-10-05 14:52:15 +03:00
echo "$str" >> $VESTA/data/firewall/rules.conf
2014-09-17 00:32:25 +03:00
# Sorting firewall rules by id number
sort_fw_rules
# Updating system firewall
2014-10-05 14:52:15 +03:00
$BIN/v-update-firewall
2014-09-17 00:32:25 +03:00
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Logging
log_event "$OK" "$ARGUMENTS"
2014-09-17 00:32:25 +03:00
exit