# Log event function log_event() { # Argument defenition level="$1" event="$2" # Checking logging system if [ "$LOG_SYSTEM" = 'yes' ]; then log=$(echo "$LOG_LEVEL" | grep -w "$level" ) if [ -n "$log" ]; then echo "$event" >> $V_LOG/$level.log fi fi } # Log user history log_history() { event="$1" undo="$2" # Checking logging system if [ "$LOG_HISTORY" = 'yes' ]; then echo "$event [$undo]" >> $V_USERS/$user/history.log fi } # Argument list checker check_args() { sys_args="$1" user_args="$2" usage="$3" if [ "$user_args" -lt "$sys_args" ]; then echo "Error: bad args" echo "Usage: $V_SCRIPT $usage" log_event 'debug' "$E_ARGS $V_EVENT" exit $E_ARGS fi } # Format validator format_validation() { # Defining url function format_url() { val="$1" # Checking url check_http=$( echo "$val" |grep "^https://" ) needed_chars=$(echo "$val" | cut -s -f 2 -d '.') if [ -z "$check_http" ] || [ -z "$needed_chars" ]; then echo "Error: invalid url" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi } # Defining shell function format_sh() { val="$1" # Checking shell check_shell=$(/usr/bin/chsh --list-shells | grep -w "$val" ) if [ -z "$1" ] || [ -z "$check_shell" ]; then echo "Error: shell not found" log_event 'debug' "$E_NOTEXIST $V_EVENT" exit $E_NOTEXIST fi } # Defining password function format_pwd() { val="$1" # Checking password lenght if [ "${#val}" -lt '6' ]; then echo "Error: password is shorter than 6 chars" log_event 'debug' "$E_PASSWORD $V_EVENT" exit $E_PASSWORD fi } # Defining integer function format_int() { val="$1" # Defining exlude mask special_chars=$(echo "$val" | \ grep -c "[!|@|#|$|^|&|*|(|)|-|+|=|{|}|:|_|,|.|<|>|?|/|\|\"|'|;|%| ]" ) if [[ 0 -ne "$special_chars" ]]; then echo "Error: $var out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi # Checking letters letters=$(echo "$val" | grep -c "[a-Z]") if [ 0 -ne "$letters" ]; then echo "Error: $var out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi # Checking -zero if [[ 0 -ne "$val" ]] && [[ 0 -gt "$val" ]]; then echo "Error: $var out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi } # Defining ip function format_ip() { val="$1" oc1=$(echo $val | cut -s -f 1 -d . ) oc2=$(echo $val | cut -s -f 2 -d . ) oc3=$(echo $val | cut -s -f 3 -d . ) oc4=$(echo $val | cut -s -f 4 -d . ) # Checking octets if [ -z "$oc1" ] || [ -z "$oc2" ] || [ -z "$oc3" ] || [ -z "$oc4" ] then echo "Error: $var out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi } # Defining ip_status function format_ips() { val="$1" check_status=$(echo "shared, exclusive" | grep -w "$val" ) # Checking status if [ -z "$check_status" ]; then echo "Error: $var out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi } # Defining email function format_eml() { val="$1" check_at=$(echo "$val" | cut -s -f 1 -d @) check_dt=$(echo "$val" | cut -s -f 2 -d @|cut -s -f 2 -d .) # Checking format if [ -z "$check_at" ] ||\ [ -z "$check_dt" ] ||\ [ "${#check_dt}" -lt 2 ] &&\ [ "$val" != 'admin@localhost' ]; then echo "Error: email format is wrong" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi } # Defining interface function format_ifc() { val="$1" # Parsing ifconfig /sbin/ifconfig "$val" > /dev/null 2>&1 return_val="$?" if [ "$return_val" -ne 0 ]; then echo "Error: intreface not exist" log_event 'debug' "$E_NOTEXIST" exit $E_NOTEXIST fi } # Defining user function format_usr() { val="$1" # Defining exlude mask special_chars=$(echo "$val" | \ grep -c "[!|@|#|$|^|&|*|(|)|+|=|{|}|:| |,|<|>|?|/|\|\"|'|;|%| ]" ) # Checking result if [[ 0 -ne "$special_chars" ]]; then echo "Error: $var is out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi } # Defining domain function format_dom() { val="$1" # Defining exlude mask special_chars=$(echo "$val" | \ grep -c "[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|_|/|\|\"|'|;|%| ]" ) needed_chars=$(echo "$val" | cut -s -f 2 -d '.') # Checking result if [[ 0 -ne "$special_chars" ]] || [ -z "$needed_chars" ]; then echo "Error: $var is out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi } # Defining format_db function format_db() { val="$1" # Defining exlude mask special_chars=$(echo "$val" | \ grep -c "[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|.|<|>|?|/|\|\"|'|;|%| ]" ) # Checking result if [[ 0 -ne "$special_chars" ]] || [ 17 -le ${#val} ]; then echo "Error: $var is out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi } # Defining format_db function format_dbu() { val="$1" # Checking result if [ 17 -le ${#val} ]; then echo "Error: $var is out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi } # Defining dns record function format_rcd() { val="$1" case $val in A) known='yes';; NS) known='yes';; CNAME) known='yes';; AAAA) known='yes';; MX) known='yes';; TXT) known='yes';; SRV) known='yes';; DNSKEY) known='yes';; KEY) known='yes';; IPSECKEY) known='yes';; PTR) known='yes';; SPF) known='yes';; *) known='no';; esac if [[ "$known" != 'yes' ]]; then echo "Error: $var is out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi } # Define minute/hour/day/month validator format_mhd() { val="$1" digit=$val limit=60 if [ "$var" = 'day' ]; then limit=31 fi if [ "$var" = 'month' ]; then limit=12 fi if [ "$var" = 'wday' ]; then limit=7 fi letters=$(echo "$val"|grep -c "[a-Z]") lslash=$(echo "$val"| grep "^/") if [ -z "$val" ] || [ 0 -ne "$letters" ] || [ -n "$lslash" ]; then echo "Error: $var is out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi special_chars=$(echo "$val" | \ grep -c "[!|@|#|$|^|&|(|)|+|=|{|}|:|,|.|<|>|?|\|\"|'|;|%| ]" ) if [[ 0 -ne "$special_chars" ]] || [ 5 -le ${#val} ]; then echo "Error: $var is out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi star=$(echo "$val" |grep '*') if [ ! -z "$star" ]; then slash=$(echo $val |grep "^*/") if [ "$val" != '*' ] && [ -z "$slash" ]; then echo "Error: $var is out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID else if [ -n "$slash" ]; then digit=$(echo $val | cut -f 2 -d / ) fi if [ "$val" = '*' ]; then digit=0 fi fi fi if [ "$limit" -lt "$digit" ]; then echo "Error: $var is out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi } # Defining format_boolean function format_bool() { val="$1" if [ "$val" != 'yes' ] && [ "$val" != 'no' ]; then echo "Error: $var is out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi } # Defining format_bit function format_bit() { val="$1" case $val in 128) known='yes';; 256) known='yes';; 512) known='yes';; 768) known='yes';; 1024) known='yes';; 2048) known='yes';; *) known='no';; esac if [[ "$known" != 'yes' ]]; then echo "Error: $var is out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi } # Defining format_ext function format_ext() { val="$1" # Checking result if [ 200 -le ${#val} ]; then echo "Error: $var is out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi } # Defining format_dvl function format_dvl() { val="$1" # Checking spaces check_spaces="$(echo "$val"|grep ' ')" check_rtype="$(echo "A AAAA NS CNAME" | grep -i -w "$rtype")" if [ ! -z "$check_spaces" ] && [ ! -z "$check_rtype" ]; then echo "Error: $var is out of range" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi # Checking ip if [ "$rtype" = 'A' ]; then format_ip "$val" fi # Checking domain if [ "$rtype" = 'NS' ]; then format_dom "$val" fi } # Lopp on all variables for var in $*; do # Parsing reference eval v=\$$var # Checking variable format case $var in antispam) format_bool "$v" ;; antivirus) format_bool "$v" ;; auth_pass) format_pwd "$v" ;; auth_user) format_usr "$v" ;; database) format_db "$v" ;; day) format_mhd "$v" ;; db_user) format_dbu "$v" ;; domain) format_dom "$v" ;; dom_alias) format_dom "$v" ;; dkim) format_bool "$v" ;; dkim_size) format_bit "$v" ;; dvalue) format_dvl "$v" ;; fname) format_usr "$v" ;; job) format_int "$v" ;; ns) format_dom "$v" ;; ns1) format_dom "$v" ;; ns2) format_dom "$v" ;; ns3) format_dom "$v" ;; ns4) format_dom "$v" ;; ns5) format_dom "$v" ;; ns6) format_dom "$v" ;; ns7) format_dom "$v" ;; ns8) format_dom "$v" ;; email) format_eml "$v" ;; extentions) format_ext "$v" ;; hour) format_mhd "$v" ;; host) format_usr "$v" ;; interface) format_ifc "$v" ;; ip) format_ip "$v" ;; ip_status) format_ips "$v" ;; ip_name) format_dom "$v" ;; id) format_int "$v" ;; mask) format_ip "$v" ;; max_usr) format_int "$v" ;; max_db) format_int "$v" ;; min) format_mhd "$v" ;; month) format_mhd "$v" ;; lname) format_usr "$v" ;; owner) format_usr "$v" ;; package) format_usr "$v" ;; password) format_pwd "$v" ;; port) format_int "$v" ;; rtype) format_rcd "$v" ;; ssl) format_usr "$v" ;; shell) format_sh "$v" ;; soa) format_dom "$v" ;; suspend_url) format_url "$v" ;; template) format_usr "$v" ;; ttl) format_int "$v" ;; user) format_usr "$v" ;; wday) format_mhd "$v" ;; esac done } # Sub system checker is_system_enabled() { eval system=\$$1 if [ -z "$system" ] || [ "$system" = "no" ]; then echo "Error: $system disabled" log_event 'debug' "$E_DISABLED $V_EVENT" exit $E_DISABLED fi } # System user check is_user_valid() { search_user="${1-$user}" check_user=$(grep -w "$search_user" /etc/passwd ) if [ -z "$check_user" ]; then echo "Error: user not found" log_event 'debug' "$E_NOTEXIST $V_EVENT" exit $E_NOTEXIST fi if [ ! -d "$V_USERS/$search_user" ]; then echo "Error: unknown user" log_event 'debug' "$E_USER_NOTEXIST $V_EVENT" exit $E_USER_NOTEXIST fi } # Specific key check is_user_suspended() { check_suspend=$(grep "SUSPENDED='yes'" $V_USERS/$user/user.conf) if [ ! -z "$check_suspend" ]; then echo "Error: User is suspended" log_event 'debug' "$E_SUSPENDED $V_EVENT" exit $E_SUSPENDED fi } # User package check is_package_full() { limit=$(grep "^$1=" $V_USERS/$user/user.conf | cut -f 2 -d \' ) # Switching on case "$1" in WEB_DOMAINS) used=$(wc -l $V_USERS/$user/web.conf|cut -f1 -d \ );; WEB_ALIASES) used=$(grep "DOMAIN='$domain'" $V_USERS/$user/web.conf |\ awk -F "ALIAS='" '{print $2}' | cut -f 1 -d \' |\ tr ',' '\n'| wc -l );; DNS_DOMAINS) used=$(wc -l $V_USERS/$user/dns.conf|cut -f1 -d \ );; DNS_RECORDS) used=$(wc -l $V_USERS/$user/dns/$domain|cut -f1 -d \ );; MAIL_DOMAINS) used=$(wc -l $V_USERS/$user/mail.conf|cut -f1 -d \ );; DATABASES) used=$(wc -l $V_USERS/$user/db.conf|cut -f1 -d \ );; CRON_JOBS) used=$(wc -l $V_USERS/$user/cron.conf|cut -f1 -d \ );; esac if [ "$used" -ge "$limit" ]; then echo "Error: Upgrade package" log_event 'debug' "$E_LIMIT $v_log" exit $E_LIMIT fi } is_template_valid() { stype="$1" web_template() { check_tpl=$(echo "$templates"|sed -e "s/,/\n/g"|grep "^$template$") tpl="$V_WEBTPL/apache_$template.tpl" descr="$V_WEBTPL/apache_$template.descr" ssl="$V_WEBTPL/apache_$template.stpl" if [ -z "$check_tpl" ] || [ ! -e $tpl ] || \ [ ! -e $descr ] || [ ! -e $ssl ]; then echo "Error: template not found" log_event 'debug' "$E_NOTEXIST" exit $E_NOTEXIST fi } proxy_template() { tpl="$V_WEBTPL/ngingx_vhost_$template.tpl" descr="$V_WEBTPL/ngingx_vhost_$template.descr" ssl="$V_WEBTPL/ngingx_vhost_$template.stpl" if [ ! -e $tpl ] || [ ! -e $descr ] || [ ! -e $ssl ]; then echo "Error: template not found" log_event 'debug' "$E_NOTEXIST" exit $E_NOTEXIST fi } dns_template() { tpl="$V_DNSTPL/$template.tpl" descr="$V_DNSTPL/$template.descr" if [ ! -e $tpl ] || [ ! -e $descr ]; then echo "Error: template not found" log_event 'debug' "$E_NOTEXIST" exit $E_NOTEXIST fi } # Switching config case $stype in web) web_template "$template" ;; proxy) proxy_template "$template" ;; dns) dns_template "$template" ;; esac } get_user_value() { key="$1" USER="$user" source $V_USERS/$user/user.conf eval value="$key" echo "$value" } restart_schedule() { type="$1" if [ -z "$RESTART_PERIOD" ]; then $V_BIN/v_restart_"$type" "$V_EVENT" else echo "$type" >> $V_QUEUE/restart.pipe fi } is_package_valid() { if [ ! -e "$V_PKG/$package.pkg" ]; then echo "Error: package is not exist" log_event 'debug' "$E_NOTEXIST $v_log" exit $E_NOTEXIST fi } update_user_value() { USER="$1" key="${2//$}" value="$3" conf="$V_USERS/$USER/user.conf" old=$(grep "$key=" $conf | cut -f 2 -d \') # Updating conf sed -i "s/$key='$old'/$key='$value'/g" $conf } increase_user_value() { USER="$1" key="${2//$}" factor="${3-1}" # Parsing current value conf="$V_USERS/$USER/user.conf" old=$(grep "$key=" $conf | cut -f 2 -d \') if [ -z "$old" ]; then old=0 fi # Increasing new=$((old + factor)) sed -i "s/$key='$old'/$key='$new'/g" $conf } is_type_valid() { # Argument defenition sys="$1" stype="$2" # Switching config case $sys in stat) skey='STATS_SYSTEM=';; db) skey='DB_SYSTEM=' ;; *) skey='UNKNOWN' ;; esac # Parsing domain values check_type=$(grep "$skey" $V_CONF/vesta.conf|grep -w $stype) # Checking result if [ -z "$check_type" ]; then echo "Error: unknown type" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID fi } decrease_user_value() { USER="$1" key="${2//$}" factor="${3-1}" # Parsing current value conf="$V_USERS/$USER/user.conf" old=$(grep "$key=" $conf | cut -f 2 -d \') if [ -z "$old" ]; then old=0 fi # Decreasing if [ "$old" -le 1 ]; then new=0 else new=$((old - factor)) fi sed -i "s/$key='$old'/$key='$new'/g" $conf } # Json listing function json_list() { # Print top bracket echo '{' # Count fields fileds_count=$(echo $fields| wc -w ) # Reading file line by line while read line; do # Assing key=value pair eval $line # Closing bracket if there already was output if [ -n "$data" ]; then echo -e ' },' fi i=1 for field in $fields; do eval value=$field if [ $i -eq 1 ]; then # Printing parrent (( ++i)) echo -e "\t\"$value\": {" else # Printing child if [ $i -lt $fileds_count ]; then (( ++i)) echo -e "\t\t\"${field//$/}\": \"$value\"," else echo -e "\t\t\"${field//$/}\": \"$value\"" data=1 fi fi done done < $conf # Closing bracket if there was output if [ -n "$data" ]; then echo -e ' }' fi # Printing bottom bracket echo -e '}' } # Shell listing function shell_list() { if [ -z "$nohead" ] ; then # Print brief info echo "${fields//$/}" for a in $fields; do echo -e "------ \c" done echo fi # Reading file line by line while read line ; do # Assing key=value pair eval $line # Print result eval echo "$fields" done < $conf } get_usr_disk() { size='0' # Using tricky way to parse configs dir_usage=$(grep 'U_DIR_DISK=' $V_USERS/$user/user.conf |\ cut -f 2 -d "'") size=$((size + dir_usage)) # Checking web if [ -f "$V_USERS/$user/web.conf" ]; then # Using tricky way to parse configs disk_usage=$(grep 'U_DISK=' $V_USERS/$user/web.conf |\ awk -F "U_DISK='" '{print $2}'|cut -f 1 -d "'") for disk in $disk_usage; do size=$((size + disk)) done fi # Checking db if [ -f "$V_USERS/$user/db.conf" ]; then # Using tricky way to parse configs disk_usage=$(grep 'U_DISK=' $V_USERS/$user/db.conf |\ awk -F "U_DISK='" '{print $2}'|cut -f 1 -d "'") for disk in $disk_usage; do size=$((size + disk)) done fi # Checking mail if [ -f "$V_USERS/$user/mail_domains.conf" ]; then # Using tricky way to parse configs disk_usage=$(grep 'U_DISK=' $V_USERS/$user/mail_domains.conf |\ awk -F "U_DISK='" '{print $2}'|cut -f 1 -d "'") for disk in $disk_usage; do size=$((size + disk)) done fi echo "$size" } get_usr_traff() { size='0' conf='web.conf' # Checking web if [ -f "$V_USERS/$user/$conf" ]; then # Using tricky way to parse configs bandwidth_usage=$(grep 'U_BANDWIDTH=' $V_USERS/$user/$conf|\ awk -F "U_BANDWIDTH='" '{print $2}'|cut -f 1 -d "'") for bandwidth in $bandwidth_usage; do size=$((size + bandwidth)) done fi echo "$size" } is_backup_enabled() { BACKUPS=$(grep "BACKUPS='" $V_USERS/$user/user.conf |cut -f 2 -d \') if [ -z "$BACKUPS" ] || [[ "$BACKUPS" -le '0' ]]; then echo "Error: User backups are disabled" log_event 'debug' "$E_DISABLED $V_EVENT" exit $E_DISABLED fi } gen_password() { MATRIX='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' LENGTH=10 while [ ${n:=1} -le $LENGTH ]; do PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}" let n+=1 done echo "$PASS" }