Skip to content

Commit 1f8a3af

Browse files
committed
updated format validator for firewall
1 parent 66bc02d commit 1f8a3af

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

func/main.sh

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ check_args() {
9191
# Subsystem checker
9292
is_system_enabled() {
9393
if [ -z "$1" ] || [ "$1" = no ]; then
94-
echo "Error: $2 is disabled in the vesta.conf"
94+
echo "Error: $2 is not enabled in the $VESTA/conf/vesta.conf"
9595
log_event "$E_DISABLED" "$EVENT"
9696
exit $E_DISABLED
9797
fi
@@ -564,13 +564,25 @@ validate_format_interface() {
564564

565565
# IP address
566566
validate_format_ip() {
567+
t_ip=$(echo $1 |awk -F / '{print $1}')
568+
t_cidr=$(echo $1 |awk -F / '{print $2}')
567569
valid_octets=0
568-
for octet in ${1//./ }; do
570+
valid_cidr=1
571+
for octet in ${t_ip//./ }; do
569572
if [[ $octet =~ ^[0-9]{1,3}$ ]] && [[ $octet -le 255 ]]; then
570573
((++valid_octets))
571574
fi
572575
done
573-
if [ "$valid_octets" -lt 4 ]; then
576+
577+
if [ ! -z "$(echo $1|grep '/')" ]; then
578+
if [[ "$t_cidr" -lt 0 ]] || [[ "$t_cidr" -gt 32 ]]; then
579+
valid_cidr=0
580+
fi
581+
if ! [[ "$t_cidr" =~ ^[0-9]+$ ]]; then
582+
valid_cidr=0
583+
fi
584+
fi
585+
if [ "$valid_octets" -lt 4 ] || [ "$valid_cidr" -eq 0 ]; then
574586
echo "Error: ip $1 is not valid"
575587
log_event "$E_INVALID" "$EVENT"
576588
exit $E_INVALID
@@ -625,12 +637,14 @@ validate_format_username() {
625637
if [ "${#1}" -eq 1 ]; then
626638
if ! [[ "$1" =~ [a-z] ]]; then
627639
echo "Error: $2 $1 is not valid"
640+
log_event "$E_INVALID" "$EVENT"
628641
exit 1
629642
fi
630643
else
631644
if ! [[ "$1" =~ ^[a-zA-Z0-9][-|\.|_|a-zA-Z0-9]{0,28}[a-zA-Z0-9]$ ]]
632645
then
633646
echo "Error: $2 $1 is not valid"
647+
log_event "$E_INVALID" "$EVENT"
634648
exit 1
635649
fi
636650
fi
@@ -792,6 +806,42 @@ validate_format_autoreply() {
792806
fi
793807
}
794808

809+
# Firewall action
810+
validate_format_fw_action() {
811+
if [ "$1" != "ACCEPT" ] && [ "$1" != 'DROP' ] ; then
812+
echo "Error: $1 is not valid action"
813+
log_event "$E_INVALID" "$EVENT"
814+
exit $E_INVALID
815+
fi
816+
}
817+
818+
# Firewall protocol
819+
validate_format_fw_protocol() {
820+
if [ "$1" != "ICMP" ] && [ "$1" != 'UDP' ] && [ "$1" != 'TCP' ] ; then
821+
echo "Error: $1 is not valid protocol"
822+
log_event "$E_INVALID" "$EVENT"
823+
exit $E_INVALID
824+
fi
825+
}
826+
827+
# Firewall port
828+
validate_format_fw_port() {
829+
if [ "${#1}" -eq 1 ]; then
830+
if ! [[ "$1" =~ [0-9] ]]; then
831+
echo "Error: port $1 is not valid"
832+
log_event "$E_INVALID" "$EVENT"
833+
exit 1
834+
fi
835+
else
836+
if ! [[ "$1" =~ ^[0-9][-|,|:|0-9]{0,30}[0-9]$ ]]
837+
then
838+
echo "Error: port $1 is not valid"
839+
log_event "$E_INVALID" "$EVENT"
840+
exit 1
841+
fi
842+
fi
843+
}
844+
795845
# Format validation controller
796846
validate_format(){
797847
for arg_name in $*; do
@@ -804,12 +854,14 @@ validate_format(){
804854

805855
case $arg_name in
806856
account) validate_format_username "$arg" "$arg_name" ;;
857+
action) validate_format_fw_action "$arg";;
807858
antispam) validate_format_boolean "$arg" 'antispam' ;;
808859
antivirus) validate_format_boolean "$arg" 'antivirus' ;;
809860
autoreply) validate_format_autoreply "$arg" ;;
810861
backup) validate_format_domain "$arg" 'backup' ;;
811862
charset) validate_format_name "$arg" "$arg_name" ;;
812863
charsets) validate_format_common "$arg" 'charsets' ;;
864+
comment) validate_format_name "$arg" 'comment' ;;
813865
database) validate_format_database "$arg" 'database';;
814866
day) validate_format_mhdmw "$arg" $arg_name ;;
815867
dbpass) validate_format_password "$arg" ;;
@@ -850,10 +902,13 @@ validate_format(){
850902
package) validate_format_name "$arg" "$arg_name" ;;
851903
password) validate_format_password "$arg" ;;
852904
port) validate_format_int "$arg" 'port' ;;
905+
port_ext) validate_format_fw_port "$arg";;
906+
protocol) validate_format_fw_protocol "$arg" ;;
853907
quota) validate_format_int "$arg" 'quota' ;;
854908
restart) validate_format_boolean "$arg" 'restart' ;;
855909
record) validate_format_common "$arg" 'record';;
856910
rtype) validate_format_dns_type "$arg" ;;
911+
rule) validate_format_int "$arg" "rule id" ;;
857912
shell) validate_format_shell "$arg" ;;
858913
soa) validate_format_domain "$arg" 'soa_record';;
859914
stats_pass) validate_format_password "$arg" ;;

0 commit comments

Comments
 (0)