mirror of
https://github.com/serghey-rodin/vesta.git
synced 2025-08-21 13:54:26 -07:00
Firewall with Fail2ban support
This commit is contained in:
parent
f6926670fe
commit
357eb42647
27 changed files with 936 additions and 50 deletions
89
bin/v-add-firewall-rule
Executable file
89
bin/v-add-firewall-rule
Executable file
|
@ -0,0 +1,89 @@
|
|||
#!/bin/bash
|
||||
# info: add firewall rule
|
||||
# options: ACTION IP PORT [PROTOCOL] [COMMENT] [RULE]
|
||||
#
|
||||
# The function adds new rule to system firewall
|
||||
|
||||
|
||||
#----------------------------------------------------------#
|
||||
# Variable&Function #
|
||||
#----------------------------------------------------------#
|
||||
|
||||
# Importing system variables
|
||||
source /etc/profile
|
||||
|
||||
# Argument defenition
|
||||
action=$(echo $1|tr '[:lower:]' '[:upper:]')
|
||||
ip=$2
|
||||
port_ext=$3
|
||||
protocol=${4-TCP}
|
||||
protocol=$(echo $protocol|tr '[:lower:]' '[:upper:]')
|
||||
comment=$5
|
||||
rule=$6
|
||||
|
||||
# Includes
|
||||
source $VESTA/func/main.sh
|
||||
source $VESTA/conf/vesta.conf
|
||||
|
||||
# Get next firewall rule id
|
||||
get_next_fw_rule() {
|
||||
if [ -z "$rule" ]; then
|
||||
curr_str=$(grep "RULE=" $VESTA/data/firewall/rules.conf |\
|
||||
cut -f 2 -d \' | sort -n | tail -n1)
|
||||
rule="$((curr_str +1))"
|
||||
fi
|
||||
}
|
||||
|
||||
sort_fw_rules() {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
#----------------------------------------------------------#
|
||||
# Verifications #
|
||||
#----------------------------------------------------------#
|
||||
|
||||
check_args '3' "$#" 'ACTION IP PORT [PROTOCOL] [COMMENT] [RULE]'
|
||||
validate_format 'action' 'protocol' 'port_ext' 'ip'
|
||||
is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
|
||||
get_next_fw_rule
|
||||
validate_format 'rule'
|
||||
is_object_new '../../data/firewall/rules' 'RULE' "$rule"
|
||||
if [ ! -z "$comment"]; then
|
||||
validate_format 'comment'
|
||||
fi
|
||||
|
||||
|
||||
#----------------------------------------------------------#
|
||||
# Action #
|
||||
#----------------------------------------------------------#
|
||||
|
||||
# Concatenating 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'"
|
||||
|
||||
# Adding to config
|
||||
echo "$str" >> $VESTA/data/firewall/rules.conf
|
||||
|
||||
# Changing permissions
|
||||
chmod 660 $VESTA/data/firewall/rules.conf
|
||||
|
||||
# Sorting firewall rules by id number
|
||||
sort_fw_rules
|
||||
|
||||
# Updating system firewall
|
||||
$BIN/v-update-firewall
|
||||
|
||||
|
||||
#----------------------------------------------------------#
|
||||
# Vesta #
|
||||
#----------------------------------------------------------#
|
||||
|
||||
# Logging
|
||||
log_event "$OK" "$EVENT"
|
||||
|
||||
exit
|
Loading…
Add table
Add a link
Reference in a new issue