refactoring: log_event(), is_format_valid()

This commit is contained in:
Serghey Rodin 2016-06-24 16:33:24 +03:00
commit 5d30b67fb1
6 changed files with 344 additions and 92 deletions

View file

@ -112,17 +112,18 @@ is_system_enabled() {
# User package check
is_package_full() {
case "$1" in
WEB_DOMAINS) used=$(wc -l $USER_DATA/web.conf |cut -f1 -d \ );;
WEB_DOMAINS) used=$(wc -l $USER_DATA/web.conf);;
WEB_ALIASES) used=$(echo $aliases |tr ',' '\n' |wc -l);;
DNS_DOMAINS) used=$(wc -l $USER_DATA/dns.conf |cut -f1 -d \ );;
DNS_RECORDS) used=$(wc -l $USER_DATA/dns/$domain.conf|cut -f1 -d \ );;
MAIL_DOMAINS) used=$(wc -l $USER_DATA/mail.conf |cut -f1 -d \ );;
MAIL_USER) used=$(wc -l $USER_DATA/mail/$domain.conf |cut -f1 -d \ );;
DATABASES) used=$(wc -l $USER_DATA/db.conf |cut -f1 -d \ );;
CRON_JOBS) used=$(wc -l $USER_DATA/cron.conf |cut -f1 -d \ );;
DNS_DOMAINS) used=$(wc -l $USER_DATA/dns.conf);;
DNS_RECORDS) used=$(wc -l $USER_DATA/dns/$domain.conf);;
MAIL_DOMAINS) used=$(wc -l $USER_DATA/mail.conf);;
MAIL_ACCOUNTS) used=$(wc -l $USER_DATA/mail/$domain.conf);;
DATABASES) used=$(wc -l $USER_DATA/db.conf);;
CRON_JOBS) used=$(wc -l $USER_DATA/cron.conf);;
esac
used=$(echo "$used"| cut -f 1 -d \ )
limit=$(grep "^$1=" $USER_DATA/user.conf |cut -f 2 -d \')
if [ "$limit" != 'unlimited' ] && [ "$used" -ge "$limit" ]; then
if [ "$limit" != 'unlimited' ] && [[ "$used" -ge "$limit" ]]; then
check_result $E_LIMIT "$1 limit is reached :: upgrade user package"
fi
}
@ -140,14 +141,19 @@ get_user_owner() {
}
# Random password generator
gen_password() {
PW_MATRIX='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
PW_LENGHT='10'
pw_matrix=${1-$PW_MATRIX}
pw_lenght=${2-$PW_LENGHT}
while [ ${n:=1} -le $pw_lenght ]; do
pass="$pass${pw_matrix:$(($RANDOM%${#pw_matrix})):1}"
let n+=1
generate_password() {
matrix=$1
lenght=$2
if [ -z "$matrix" ]; then
matrix=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
fi
if [ -z "$lenght" ]; then
lenght=10
fi
i=1
while [ $i -le $lenght ]; do
pass="$pass${matrix:$(($RANDOM%${#matrix})):1}"
((i++))
done
echo "$pass"
}
@ -169,15 +175,6 @@ is_type_valid() {
fi
}
# Check if backup is available for user
is_backup_available() {
b_owner=$(echo $user |\
sed -e "s/\.[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].tar//")
if [ "$user" != "$b_owner" ]; then
check_result $E_FORBIDEN "permission denied"
fi
}
# Check user backup settings
is_backup_enabled() {
BACKUPS=$(grep "^BACKUPS=" $USER_DATA/user.conf | cut -f2 -d \')
@ -450,7 +447,7 @@ sync_cron_jobs() {
fi
rm -f $crontab
if [ "$CRON_REPORTS" = 'yes' ]; then
echo "MAILTO=$CONTACT" > $sys_cron
echo "MAILTO=$CONTACT" > $crontab
fi
while read line; do
eval $line
@ -467,15 +464,16 @@ sync_cron_jobs() {
# User format validator
is_user_format_valid() {
if ! [[ "$1" =~ ^[a-zA-Z0-9][-|\.|_|a-zA-Z0-9]{0,28}[a-zA-Z0-9]$ ]]; then
check_result $E_INVALID "invalid user format :: $1"
check_result $E_INVALID "invalid $2 format :: $1"
fi
}
# Domain format validator
is_domain_format_valid() {
object_name=${2-domain}
exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|_|/|\|\"|'|;|%|\`| ]"
if [[ "$1" =~ $exclude ]]; then
check_result $E_INVALID "invalid domain format :: $1"
check_result $E_INVALID "invalid $object_name format :: $1"
fi
}
@ -494,9 +492,10 @@ is_alias_format_valid() {
# IP format validator
is_ip_format_valid() {
object_name=${2-ip}
ip_regex='([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
if ! [[ $1 =~ ^$ip_regex\.$ip_regex\.$ip_regex\.$ip_regex$ ]]; then
check_result $E_INVALID "invalid IP format :: $1"
check_result $E_INVALID "invalid $object_name format :: $1"
fi
}
@ -512,7 +511,203 @@ is_extention_format_valid() {
is_number_format_valid() {
object_name=${2-number}
if ! [[ "$1" =~ ^[0-9]+$ ]] ; then
check_result $E_INVALID "invalid $object_name format :: $1"
check_result $E_INVALID "invalid $object_name format :: $1"
fi
}
# Autoreply format validator
is_autoreply_format_valid() {
if [[ "$1" =~ [$|\`] ]] || [ 10240 -le ${#1} ]; then
check_result $E_INVALID "invalid autoreply format :: $1"
fi
}
# Boolean format validator
is_boolean_format_valid() {
if [ "$1" != 'yes' ] && [ "$1" != 'no' ]; then
check_result $E_INVALID "invalid $2 format :: $1"
fi
}
# Common format validator
is_common_format_valid() {
exclude="[!|#|$|^|&|(|)|+|=|{|}|:|<|>|?|/|\|\"|'|;|%|\`| ]"
if [[ "$1" =~ $exclude ]]; then
check_result $E_INVALID "invalid $2 format :: $1"
fi
if [ 400 -le ${#1} ]; then
check_result $E_INVALID "invalid $2 format :: $1"
fi
if [[ "$1" =~ @ ]] && [ ${#1} -gt 1 ] ; then
check_result $E_INVALID "invalid $2 format :: $1"
fi
if [[ $1 =~ \* ]]; then
if [ "$(echo $1 | grep -o '*'|wc -l)" -gt 1 ]; then
check_result $E_INVALID "invalid $2 format :: $1"
fi
fi
}
# Database format validator
is_database_format_valid() {
exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|/|\|\"|'|;|%|\`| ]"
if [[ "$1" =~ $exclude ]] || [ 65 -le ${#1} ]; then
check_result $E_INVALID "invalid $2 format :: $1"
fi
}
# Date format validator
is_date_format_valid() {
if ! [[ "$1" =~ ^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$ ]]; then
check_result $E_INVALID "invalid date format :: $1"
fi
}
# Database user validator
is_dbuser_format_valid() {
exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|/|\|\"|'|;|%|\`| ]"
if [[ "$1" =~ $exclude ]] || [ 17 -le ${#1} ]; then
check_result $E_INVALID "invalid $2 format :: $1"
fi
}
# DNS record type validator
is_dns_type_format_valid() {
known_dnstype='A,AAAA,NS,CNAME,MX,TXT,SRV,DNSKEY,KEY,IPSECKEY,PTR,SPF'
if [ -z "$(echo $known_dnstype |grep -w $1)" ]; then
check_result $E_INVALID "invalid dns record type format :: $1"
fi
}
# DNS record validator
is_dns_record_format_valid() {
if [ "$rtype" = 'A' ]; then
is_ip_format_valid "$1"
fi
if [ "$rtype" = 'NS' ]; then
is_domain_format_valid "$1" 'ns_record'
fi
if [ "$rtype" = 'MX' ]; then
is_domain_format_valid "$1" 'mx_record'
is_int_format_valid "$priority" 'priority_record'
fi
}
# Email format validator
is_email_format_valid() {
if [[ ! "$1" =~ "@" ]] ; then
check_result $E_INVALID "invalid email format :: $1"
fi
}
# Firewall action validator
is_fw_action_format_valid() {
if [ "$1" != "ACCEPT" ] && [ "$1" != 'DROP' ] ; then
check_result $E_INVALID "invalid action format :: $1"
fi
}
# Firewall protocol validator
is_fw_protocol_format_valid() {
if [ "$1" != "ICMP" ] && [ "$1" != 'UDP' ] && [ "$1" != 'TCP' ] ; then
check_result $E_INVALID "invalid protocol format :: $1"
fi
}
# Firewall port validator
is_fw_port_format_valid() {
if [ "${#1}" -eq 1 ]; then
if ! [[ "$1" =~ [0-9] ]]; then
check_result $E_INVALID "invalid port format :: $1"
fi
else
if ! [[ "$1" =~ ^[0-9][-|,|:|0-9]{0,30}[0-9]$ ]]
then
check_result $E_INVALID "invalid port format :: $1"
fi
fi
}
# Integer validator
is_int_format_valid() {
if ! [[ "$1" =~ ^[0-9]+$ ]] ; then
check_result $E_INVALID "invalid $2 format :: $1"
fi
}
# Interface validator
is_interface_format_valid() {
netdevices=$(cat /proc/net/dev |grep : |cut -f 1 -d : |tr -d ' ')
if [ -z $(echo "$netdevices" |grep -x $1) ]; then
check_result $E_INVALID "invalid interface format :: $1"
fi
}
# IP status validator
is_ip_status_format_valid() {
if [ -z "$(echo shared,dedicated | grep -w $1 )" ]; then
check_result $E_INVALID "invalid status format :: $1"
fi
}
# Cron validator
is_cron_format_valid() {
limit=60
check_format=''
if [ "$2" = 'day' ]; then
limit=31
fi
if [ "$2" = 'month' ]; then
limit=12
fi
if [ "$2" = 'wday' ]; then
limit=7
fi
if [ "$1" = '*' ]; then
check_format='ok'
fi
if [[ "$1" =~ ^[\*]+[/]+[0-9] ]]; then
if [ "$(echo $1 |cut -f 2 -d /)" -lt $limit ]; then
check_format='ok'
fi
fi
if [[ "$1" =~ ^[0-9][-|,|0-9]{0,28}[0-9]$ ]]; then
check_format='ok'
crn_values=${1//,/ }
crn_values=${crn_values//-/ }
for crn_vl in $crn_values; do
if [ "$crn_vl" -gt $limit ]; then
check_format='invalid'
fi
done
fi
if [[ "$1" =~ ^[0-9]+$ ]] && [ "$1" -lt $limit ]; then
check_format='ok'
fi
if [ "$check_format" != 'ok' ]; then
check_result $E_INVALID "invalid $2 format :: $1"
fi
}
# Name validator
is_name_format_valid() {
if ! [[ "$1" =~ ^[[:alnum:]][-|\ |\.|_[:alnum:]]{0,28}[[:alnum:]]$ ]]; then
check_result $E_INVALID "invalid $2 format :: $1"
fi
}
# Object validator
is_object_format_valid() {
if ! [[ "$1" =~ ^[[:alnum:]][-|\.|_[:alnum:]]{0,28}[[:alnum:]]$ ]]; then
check_result $E_INVALID "invalid $2 format :: $1"
fi
}
# Password validator
is_password_format_valid() {
if [ "${#1}" -lt '6' ]; then
check_result $E_INVALID "invalid password format :: $1"
fi
}
@ -522,11 +717,73 @@ is_format_valid() {
eval arg=\$$arg_name
if [ ! -z "$arg" ]; then
case $arg_name in
aliases) is_alias_format_valid "$arg" ;;
domain) is_domain_format_valid "$arg" ;;
proxy_ext) is_extention_format_valid "$arg" ;;
ip) is_ip_format_valid "$arg" ;;
user) is_user_format_valid "$arg" ;;
account) is_user_format_valid "$arg" "$arg_name";;
action) is_fw_action_format_valid "$arg";;
aliases) is_alias_format_valid "$arg" ;;
antispam) is_boolean_format_valid "$arg" 'antispam' ;;
antivirus) is_boolean_format_valid "$arg" 'antivirus' ;;
autoreply) is_autoreply_format_valid "$arg" ;;
backup) is_user_format_valid "$arg" 'backup' ;;
charset) is_object_format_valid "$arg" "$arg_name" ;;
charsets) is_common_format_valid "$arg" 'charsets' ;;
comment) is_object_format_valid "$arg" 'comment' ;;
database) is_database_format_valid "$arg" 'database';;
day) is_cron_format_valid "$arg" $arg_name ;;
dbpass) is_password_format_valid "$arg" ;;
dbuser) is_dbuser_format_valid "$arg" 'dbuser';;
dkim) is_boolean_format_valid "$arg" 'dkim' ;;
dkim_size) is_int_format_valid "$arg" ;;
domain) is_domain_format_valid "$arg" ;;
dvalue) is_dns_record_format_valid "$arg";;
email) is_email_format_valid "$arg" ;;
exp) is_date_format_valid "$arg" ;;
extentions) is_common_format_valid "$arg" 'extentions' ;;
fname) is_name_format_valid "$arg" "first name" ;;
ftp_password) is_password_format_valid "$arg" ;;
ftp_user) is_user_format_valid "$arg" "$arg_name" ;;
host) is_domain_format_valid "$arg" "$arg_name" ;;
hour) is_cron_format_valid "$arg" $arg_name ;;
id) is_int_format_valid "$arg" 'id' ;;
ip) is_ip_format_valid "$arg" ;;
ip_name) is_domain_format_valid "$arg" 'IP name';;
ip_status) is_ip_status_format_valid "$arg" ;;
job) is_int_format_valid "$arg" 'job' ;;
key) is_user_format_valid "$arg" "$arg_name" ;;
lname) is_name_format_valid "$arg" "last name" ;;
malias) is_user_format_valid "$arg" "$arg_name" ;;
max_db) is_int_format_valid "$arg" 'max db';;
min) is_cron_format_valid "$arg" $arg_name ;;
month) is_cron_format_valid "$arg" $arg_name ;;
nat_ip) is_ip_format_valid "$arg" ;;
netmask) is_ip_format_valid "$arg" 'netmask' ;;
newid) is_int_format_valid "$arg" 'id' ;;
ns1) is_domain_format_valid "$arg" 'ns1' ;;
ns2) is_domain_format_valid "$arg" 'ns2' ;;
ns3) is_domain_format_valid "$arg" 'ns3' ;;
ns4) is_domain_format_valid "$arg" 'ns4' ;;
ns5) is_domain_format_valid "$arg" 'ns5' ;;
ns6) is_domain_format_valid "$arg" 'ns6' ;;
ns7) is_domain_format_valid "$arg" 'ns7' ;;
ns8) is_domain_format_valid "$arg" 'ns8' ;;
object) is_name_format_valid "$arg" 'object';;
package) is_object_format_valid "$arg" "$arg_name" ;;
password) is_password_format_valid "$arg" ;;
port) is_int_format_valid "$arg" 'port' ;;
port_ext) is_fw_protocol_format_valid "$arg";;
protocol) is_fw_port_format_valid "$arg" ;;
proxy_ext) is_extention_format_valid "$arg" ;;
quota) is_int_format_valid "$arg" 'quota' ;;
record) is_common_format_valid "$arg" 'record';;
restart) is_boolean_format_valid "$arg" 'restart' ;;
rtype) is_dns_type_format_valid "$arg" ;;
rule) is_int_format_valid "$arg" "rule id" ;;
soa) is_domain_format_valid "$arg" 'SOA' ;;
stats_pass) is_password_format_valid "$arg" ;;
stats_user) is_user_format_valid "$arg" "$arg_name" ;;
template) is_object_format_valid "$arg" "$arg_name" ;;
ttl) is_int_format_valid "$arg" 'ttl';;
user) is_user_format_valid "$arg" $arg_name;;
wday) is_cron_format_valid "$arg" $arg_name ;;
esac
fi
done