# 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 "$check_shell" ]; then echo "Error: shell not found" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID 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" != 'vesta@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 } # 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 dom_alias) format_dom "$v" ;; auth_pass) format_pwd "$v" ;; auth_user) format_usr "$v" ;; ssl) format_usr "$v" ;; domain) format_dom "$v" ;; database) format_db "$v" ;; db_user) format_dbu "$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" ;; 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" ;; lname) format_usr "$v" ;; owner) format_usr "$v" ;; package) format_usr "$v" ;; password) format_pwd "$v" ;; port) format_int "$v" ;; rtype) format_rcd "$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" ;; esac done } # Sub system checker is_system_enabled() { stype="$1" web_function() { if [ -z "$WEB_SYSTEM" ] || [ "$WEB_SYSTEM" = "no" ]; then echo "Error: web hosting support disabled" log_event 'debug' "$E_DISABLED $V_EVENT" exit $E_DISABLED fi } proxy_function() { if [ "$PROXY_SYSTEM" != 'nginx' ]; then echo "Error: proxy hosting support disabled" log_event 'debug' "$E_DISABLED $V_EVENT" exit $E_DISABLED fi } dns_function() { if [ -z "$DNS_SYSTEM" ] || [ "$DNS_SYSTEM" = "no" ]; then echo "Error: dns support disabled" log_event 'debug' "$E_DISABLED $V_EVENT" exit $E_DISABLED fi } cron_function() { if [ -z "$CRON_SYSTEM" ] || [ "$CRON_SYSTEM" = "no" ]; then echo "Error: crond support disabled" log_event 'debug' "$E_DISABLED $V_EVENT" exit $E_DISABLED fi } db_function() { if [ -z "$DB_SYSTEM" ] || [ "$DB_SYSTEM" = "no" ]; then echo "Error: db support disabled" log_event 'debug' "$E_DISABLED $V_EVENT" exit $E_DISABLED fi } backup_function() { if [ -z "$BACKUP_SYSTEM" ] || [ "$BACKUP_SYSTEM" = "no" ]; then echo "Error: backup support disabled" log_event 'debug' "$E_DISABLED $V_EVENT" exit $E_DISABLED fi } case $stype in web) web_function ;; proxy) proxy_function ;; dns) dns_function ;; cron) cron_function ;; db) db_function ;; backup) backup_function ;; *) check_args '1' '0' 'system' esac } # 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() { stype="$1" web_domain() { # Checking zero domains domain_number=$(wc -l $V_USERS/$user/web.conf|cut -f 1 -d ' ') # Comparing current val with conf val=$(grep '^WEB_DOMAINS=' $V_USERS/$user/user.conf|cut -f 2 -d \' ) if [ "$domain_number" -ge "$val" ]; then echo "Error: Upgrade package" log_event 'debug' "$E_LIMIT $v_log" exit $E_LIMIT fi } web_alias() { # Parsing aliases alias_nmb=$(grep "DOMAIN='$domain'" $V_USERS/$user/web.conf|\ awk -F "ALIAS=" '{print $2}' | cut -f 2 -d \' |\ sed -e "s/,/\n/g" | wc -l ) # Parsing config val=$(grep 'WEB_ALIASES=' $V_USERS/$user/user.conf | cut -f 2 -d \' ) if [ "$alias_nmb" -ge "$val" ]; then echo "Error: Upgrade package" log_event 'debug' "$E_LIMIT $v_log" exit $E_LIMIT fi } web_ssl() { # Parsing config val=$(grep '^WEB_SSL=' $V_USERS/$user/user.conf | cut -f 2 -d \' ) if [ "$val" -eq '0' ]; then echo "Error: Upgrade package" log_event 'debug' "$E_LIMIT $v_log" exit $E_LIMIT fi # Checking domains domain_nmb=$(grep "SSL='yes'" $V_USERS/$user/web.conf | wc -l) # Comparing current val with conf if [ "$domain_nmb" -ge "$val" ]; then echo "Error: Upgrade package" log_event 'debug' "$E_LIMIT $v_log" exit $E_LIMIT fi } dns_domain() { # Checking zero domains domain_number=$(wc -l $V_USERS/$user/dns.conf | cut -f 1 -d " ") # Comparing current val with conf val=$(grep '^DNS_DOMAINS=' $V_USERS/$user/user.conf | cut -f 2 -d \' ) if [ "$domain_number" -ge "$val" ]; then echo "Error: Upgrade package" log_event 'debug' "$E_LIMIT $v_log" exit $E_LIMIT fi } db_base() { # Checking zero domains db_number=$(wc -l $V_USERS/$user/db.conf | cut -f 1 -d " ") # Comparing current val with conf val=$(grep '^DATABASES=' $V_USERS/$user/user.conf | cut -f 2 -d \' ) if [ "$db_number" -ge "$val" ]; then echo "Error: Upgrade package" log_event 'debug' "$E_LIMIT $v_log" exit $E_LIMIT fi } # FIXME - should finish other functions # Switching case "$stype" in web_domain) web_domain "$user" ;; web_alias) web_alias "$user" "$domain" ;; web_ssl) web_ssl "$user" ;; dns) dns_domain "$user" ;; db_base) db_base "$user" ;; mail_domain) mail_domain "$user" ;; mail_box) mail_box "$user" "$domain";; mail_forwarder) mail_forwarder "$user" "$domain";; *) echo "Error: bad type" log_event 'debug' "$E_INVALID $V_EVENT" exit $E_INVALID ;; esac } 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" # Parsing domains string=$( cat $V_USERS/$user/user.conf ) # Parsing key=value for keys in $string; do eval ${keys%%=*}=${keys#*=} done # Self reference eval value="$key" # Print value echo "$value" } restart_schedule() { type="$1" period="$2" # Checking period if [ -z "$period" ]; then period=$(grep 'RESTART_PERIOD=' $V_CONF/vesta.conf | cut -f 2 -d \') fi if [ "$period" -le 0 ]; then $V_FUNC/restart_"$type" 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" # Defining conf conf="$V_USERS/$USER/user.conf" # Parsing conf str=$(cat $conf) # Reading key=values for keys in $str; do eval ${keys%%=*}=${keys#*=} done # Define clean key c_key=$(echo "${key//$/}") eval old="${key}" # Escaping slashes old=$(echo "$old" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g') new=$(echo "$value" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g') # Updating conf sed -i "s/$c_key='${old//\*/\\*}'/$c_key='${new//\*/\\*}'/g" $conf } increase_user_value() { USER="$1" key="$2" # Defining conf conf="$V_USERS/$USER/user.conf" # Deleting $ key=$(echo "${key//$/}") # Parsing current value current_value=$(grep "$key=" $conf |cut -f 2 -d \') # Checking result if [ -z "$current_value" ]; then echo "Error: Parsing error" log_event 'debug' "$E_PARSING $V_EVENT" exit $E_PARSING fi # Plus one new_value=$(expr $current_value + 1 ) # Changing config sed -i "s/$key='$current_value'/$key='$new_value'/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" conf="$V_USERS/$USER/user.conf" # Deleting $ key=$(echo "${key//$/}") # Parsing current value current_value=$(grep "$key=" $conf |cut -f 2 -d \') # Checking result if [ -z "$current_value" ]; then echo "Error: Parsing error" log_event 'debug' "$E_PARSING $V_EVENT" exit $E_PARSING fi # Checking zero val if [ "$current_value" -gt 0 ]; then # Minus one new_value=$(expr $current_value - 1 ) # Changing config sed -i "s/$key='$current_value'/$key='$new_value'/g" $conf fi } # 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" }