Refactoring stage I

This commit is contained in:
Serghey Rodin 2012-03-06 22:07:06 +02:00
commit 83196f310b
173 changed files with 3132 additions and 3342 deletions

View file

@ -1,23 +1,20 @@
is_db_valid() {
config="$V_USERS/$user/db.conf"
config="$USER_DATA/db.conf"
check_db=$(grep "DB='$database'" $config)
# Checking result
if [ -z "$check_db" ]; then
echo "Error: db not added"
log_event 'debug' "$E_NOTEXIST $V_EVENT"
log_event 'debug' "$E_NOTEXIST $EVENT"
exit $E_NOTEXIST
fi
}
is_db_new() {
# Parsing domain values
check_db=$(grep -F "DB='$database'" $V_USERS/$user/db.conf)
# Checking result
check_db=$(grep "DB='$database'" $USER_DATA/db.conf)
if [ ! -z "$check_db" ]; then
echo "Error: db exist"
log_event 'debug' "$E_EXISTS $V_EVENT"
log_event 'debug' "$E_EXISTS $EVENT"
exit $E_EXISTS
fi
}
@ -25,124 +22,51 @@ is_db_new() {
# Checking database host existance
is_db_host_valid() {
config="$V_DB/$type.conf"
config="$VESTA/conf/$type.conf"
check_db=$(grep "HOST='$host'" $config)
# Checking result
if [ -z "$check_db" ]; then
echo "Error: host not added"
log_event 'debug' "$E_NOTEXIST $V_EVENT"
log_event 'debug' "$E_NOTEXIST $EVENT"
exit $E_NOTEXIST
fi
}
get_next_db_host() {
# Defining vars
config="$V_DB/$type.conf"
host="empty"
host_str=$(grep "ACTIVE='yes'" $config)
get_next_dbhost() {
if [ -z "$host" ]; then
IFS=$'\n'
host='NULL_DB_HOST'
config="$VESTA/conf/$type.conf"
host_str=$(grep "SUSPENDED='no'" $config)
check_row=$(echo "$host_str"|wc -l)
# Checking rows count
check_row=$(echo "$host_str"|wc -l)
# Checking empty result
if [ 0 -eq "$check_row" ]; then
echo "$host"
exit
fi
# Checking one host
if [ 1 -eq "$check_row" ]; then
for key in $host_str; do
eval ${key%%=*}="${key#*=}"
done
users=$(echo -e "${U_SYS_USERS//,/\n}"|wc -l)
if [ "$MAX_DB" -gt "$U_DB_BASES" ]; then
host=$HOST
if [ 0 -lt "$check_row" ]; then
if [ 1 -eq "$check_row" ]; then
for db in $host_str; do
eval $db
if [ "$MAX_DB" -gt "$U_DB_BASES" ]; then
host=$HOST
fi
done
else
old_weight='100'
for db in $host_str; do
eval $db
let weight="$U_DB_BASES * 100 / $MAX_DB" &>/dev/null
if [ "$old_weight" -gt "$weight" ]; then
host="$HOST"
old_weight="$weight"
fi
done
fi
fi
echo "$host"
exit
fi
# Defining balancing function
weight_balance() {
ow='100' # old_weght
IFS=$'\n'
for db in $host_str; do
for key in $(echo $db|sed -e "s/' /'\n/g"); do
eval ${key%%=*}="${key#*=}"
done
weight=$(echo "$U_DB_BASES * 100 / $MAX_DB"|bc)
users=$(echo -e "${U_SYS_USERS//,/\n}"|wc -l)
if [ "$ow" -gt "$weight" ]; then
host="$HOST"
ow="$weight"
fi
done
}
# Defining random balancing function
random_balance() {
# Parsing host pool
HOST_LIST=''
IFS=$'\n'
for db in $host_str; do
for key in $(echo $db|sed -e "s/' /'\n/g"); do
eval ${key%%=*}="${key#*=}"
done
users=$(echo -e "${U_SYS_USERS//,/\n}"|wc -l)
if [ "$MAX_DB" -gt "$U_DB_BASES" ]; then
HOST_LIST="$HOST_LIST$HOST "
fi
done
# Checking one host
if [ 2 -eq $(echo -e "${HOST_LIST// /\n}"|wc -l) ]; then
host="${HOST_LIST// /\n}"# should test with disabled host
else
# Selecting all hosts
HOSTS=($(echo -e "${HOST_LIST// /\n}"))
num=${#HOSTS[*]}
host="${HOSTS[$((RANDOM%num))]}"
fi
}
# Defining first balancing function
first_balance() {
# Parsing host pool
IFS=$'\n'
for db in $host_str; do
for key in $(echo $db|sed -e "s/' /'\n/g"); do
eval ${key%%=*}="${key#*=}"
done
users=$(echo -e "${U_SYS_USERS//,/\n}"|wc -l)
if [ "$MAX_DB" -gt "$U_DB_BASES" ]; then
host="$HOST"
break
fi
done
}
# Parsing domain values
db_balance=$(grep "DB_BALANCE='" $V_CONF/vesta.conf|cut -f 2 -d \')
case $db_balance in
weight) weight_balance "$config" ;;
random) random_balance "$config" ;;
first) first_balance "$config" ;;
*) random_balance "$config" ;;
esac
echo "$host"
}
increase_db_value() {
# Defining vars
conf="$V_DB/$type.conf"
conf="$VESTA/conf/$type.conf"
host_str=$(grep "HOST='$host'" $conf)
for key in $host_str; do
@ -181,7 +105,7 @@ increase_db_value() {
decrease_db_value() {
# Defining vars
conf="$V_DB/$type.conf"
conf="$VESTA/conf/$type.conf"
host_str=$(grep "HOST='$host'" $conf)
for key in $host_str; do
@ -192,7 +116,7 @@ decrease_db_value() {
U_DB_BASES=$((U_DB_BASES - 1))
# Checking user databases on that host
udb=$(grep "TYPE='$type'" $V_USERS/$user/db.conf|grep "HOST='$host'"|wc -l)
udb=$(grep "TYPE='$type'" $USER_DATA/db.conf|grep "HOST='$host'"|wc -l)
if [ 2 -gt "$udb" ]; then
U_SYS_USERS=$(echo "$U_SYS_USERS" | sed -e "s/,/\n/g" |\
sed -e "/^$user$/d" | sed -e :a -e '$!N;s/\n/,/;ta')
@ -219,7 +143,7 @@ decrease_db_value() {
create_db_mysql() {
# Defining vars
host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
host_str=$(grep "HOST='$host'" $VESTA/conf/mysql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
@ -228,7 +152,7 @@ create_db_mysql() {
# Checking empty vars
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -236,7 +160,7 @@ create_db_mysql() {
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
@ -245,7 +169,7 @@ create_db_mysql() {
code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
@ -265,7 +189,7 @@ create_db_mysql() {
create_db_pgsql() {
# Defining vars
host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
@ -276,7 +200,7 @@ create_db_pgsql() {
# Checking empty vars
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -284,7 +208,7 @@ create_db_pgsql() {
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
@ -293,7 +217,7 @@ create_db_pgsql() {
code=$?
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
@ -313,11 +237,11 @@ create_db_pgsql() {
}
is_db_host_new() {
if [ -e "$V_DB/$type.conf" ]; then
check_host=$(grep "HOST='$host'" $V_DB/$type.conf)
if [ -e "$VESTA/conf/$type.conf" ]; then
check_host=$(grep "HOST='$host'" $VESTA/conf/$type.conf)
if [ ! -z "$check_host" ]; then
echo "Error: db host exist"
log_event 'debug' "$E_EXISTS $V_EVENT"
log_event 'debug' "$E_EXISTS $EVENT"
exit $E_EXISTS
fi
fi
@ -329,7 +253,7 @@ is_mysql_host_alive() {
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
}
@ -341,50 +265,50 @@ is_pgsql_host_alive() {
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
}
is_db_suspended() {
config="$V_USERS/$user/db.conf"
config="$USER_DATA/db.conf"
check_db=$(grep "DB='$database'" $config|grep "SUSPENDED='yes'")
# Checking result
if [ ! -z "$check_db" ]; then
echo "Error: db suspended"
log_event 'debug' "$E_SUSPENDED $V_EVENT"
log_event 'debug' "$E_SUSPENDED $EVENT"
exit $E_SUSPENDED
fi
}
is_db_unsuspended() {
config="$V_USERS/$user/db.conf"
config="$USER_DATA/db.conf"
check_db=$(grep "DB='$database'" $config|grep "SUSPENDED='yes'")
# Checking result
if [ -z "$check_db" ]; then
echo "Error: db unsuspended"
log_event 'debug' "$E_UNSUSPENDED $V_EVENT"
log_event 'debug' "$E_UNSUSPENDED $EVENT"
exit $E_UNSUSPENDED
fi
}
is_db_user_valid() {
config="$V_USERS/$user/db.conf"
config="$USER_DATA/db.conf"
check_db=$(grep "DB='$database'" $config|grep "USER='$db_user'")
# Checking result
if [ -z "$check_db" ]; then
echo "Error: dbuser not exist"
log_event 'debug' "$E_NOTEXIST $V_EVENT"
log_event 'debug' "$E_NOTEXIST $EVENT"
exit $E_NOTEXIST
fi
}
change_db_mysql_password() {
# Defining vars
host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
host_str=$(grep "HOST='$host'" $VESTA/conf/mysql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
@ -393,7 +317,7 @@ change_db_mysql_password() {
# Checking empty vars
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -401,7 +325,7 @@ change_db_mysql_password() {
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
@ -416,7 +340,7 @@ change_db_mysql_password() {
change_db_pgsql_password() {
# Defining vars
host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
@ -427,7 +351,7 @@ change_db_pgsql_password() {
# Checking empty vars
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -435,7 +359,7 @@ change_db_pgsql_password() {
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
@ -446,7 +370,7 @@ change_db_pgsql_password() {
get_db_value() {
# Defining vars
key="$1"
db_str=$(grep "DB='$database'" $V_USERS/$user/db.conf)
db_str=$(grep "DB='$database'" $USER_DATA/db.conf)
# Parsing key=value
for keys in $db_str; do
@ -462,7 +386,7 @@ get_db_value() {
del_db_mysql() {
# Defining vars
host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
host_str=$(grep "HOST='$host'" $VESTA/conf/mysql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
@ -471,7 +395,7 @@ del_db_mysql() {
# Checking empty vars
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -479,7 +403,7 @@ del_db_mysql() {
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
@ -487,12 +411,12 @@ del_db_mysql() {
$sql "DROP DATABASE $database" >/dev/null 2>&1;code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
# Deleting user
check_users=$(grep "USER='$db_user'" $V_USERS/$user/db.conf |wc -l)
check_users=$(grep "USER='$db_user'" $USER_DATA/db.conf |wc -l)
if [ 1 -ge "$check_users" ]; then
$sql "DROP USER '$db_user'@'%'"
if [ "$host" = 'localhost' ]; then
@ -509,7 +433,7 @@ del_db_mysql() {
del_db_pgsql() {
# Defining vars
host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
@ -520,7 +444,7 @@ del_db_pgsql() {
# Checking empty vars
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -528,7 +452,7 @@ del_db_pgsql() {
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
@ -537,12 +461,12 @@ del_db_pgsql() {
$sql "DROP DATABASE $database" >/dev/null 2>&1;code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
# Deleting user
check_users=$(grep "USER='$db_user'" $V_USERS/$user/db.conf |wc -l)
check_users=$(grep "USER='$db_user'" $USER_DATA/db.conf |wc -l)
if [ 1 -ge "$check_users" ]; then
$sql "REVOKE CONNECT ON DATABASE template1 FROM $db_user" >/dev/null
$sql "DROP ROLE $db_user" >/dev/null
@ -553,13 +477,13 @@ del_db_pgsql() {
del_db_vesta() {
conf="$V_USERS/$user/db.conf"
conf="$USER_DATA/db.conf"
# Parsing domains
string=$( grep -n "DB='$database'" $conf | cut -f 1 -d : )
if [ -z "$string" ]; then
echo "Error: parse error"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
sed -i "$string d" $conf
@ -567,7 +491,7 @@ del_db_vesta() {
dump_db_mysql() {
# Defining vars
host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
host_str=$(grep "HOST='$host'" $VESTA/conf/mysql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
@ -577,7 +501,7 @@ dump_db_mysql() {
# Checking empty vars
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -585,7 +509,7 @@ dump_db_mysql() {
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
@ -599,7 +523,7 @@ dump_db_mysql() {
dump_db_pgsql() {
# Defining vars
host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
@ -610,7 +534,7 @@ dump_db_pgsql() {
# Checking empty vars
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -618,7 +542,7 @@ dump_db_pgsql() {
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
@ -638,7 +562,7 @@ dump_db_pgsql() {
is_db_host_free() {
# Defining vars
host_str=$(grep "HOST='$host'" $V_DB/$type.conf)
host_str=$(grep "HOST='$host'" $VESTA/conf/$type.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
@ -646,19 +570,19 @@ is_db_host_free() {
# Checking U_DB_BASES
if [ 0 -ne "$U_DB_BASES" ]; then
echo "Error: host is used"
log_event 'debug' "$E_INUSE $V_EVENT"
log_event 'debug' "$E_INUSE $EVENT"
exit $E_INUSE
fi
}
del_dbhost_vesta() {
conf="$V_DB/$type.conf"
conf="$VESTA/conf/$type.conf"
# Parsing domains
string=$( grep -n "HOST='$host'" $conf | cut -f 1 -d : )
if [ -z "$string" ]; then
echo "Error: parse error"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
sed -i "$string d" $conf
@ -669,7 +593,7 @@ update_db_base_value() {
value="$2"
# Defining conf
conf="$V_USERS/$user/db.conf"
conf="$USER_DATA/db.conf"
# Parsing conf
db_str=$(grep -n "DB='$database'" $conf)
@ -697,7 +621,7 @@ update_db_base_value() {
suspend_db_mysql() {
# Defining vars
host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
host_str=$(grep "HOST='$host'" $VESTA/conf/mysql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
@ -706,7 +630,7 @@ suspend_db_mysql() {
# Checking empty vars
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -714,7 +638,7 @@ suspend_db_mysql() {
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
@ -725,7 +649,7 @@ suspend_db_mysql() {
suspend_db_pgsql() {
# Defining vars
host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
@ -736,7 +660,7 @@ suspend_db_pgsql() {
# Checking empty vars
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -744,7 +668,7 @@ suspend_db_pgsql() {
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
@ -755,7 +679,7 @@ suspend_db_pgsql() {
unsuspend_db_mysql() {
# Defining vars
host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
host_str=$(grep "HOST='$host'" $VESTA/conf/mysql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
@ -764,7 +688,7 @@ unsuspend_db_mysql() {
# Checking empty vars
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -772,7 +696,7 @@ unsuspend_db_mysql() {
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
@ -783,7 +707,7 @@ unsuspend_db_mysql() {
unsuspend_db_pgsql() {
# Defining vars
host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
@ -794,7 +718,7 @@ unsuspend_db_pgsql() {
# Checking empty vars
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -802,7 +726,7 @@ unsuspend_db_pgsql() {
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
@ -828,7 +752,7 @@ db_clear_search() {
get_disk_db_mysql() {
# Defining vars
host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
host_str=$(grep "HOST='$host'" $VESTA/conf/mysql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
@ -837,7 +761,7 @@ get_disk_db_mysql() {
# Checking empty vars
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -845,7 +769,7 @@ get_disk_db_mysql() {
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
@ -871,7 +795,7 @@ get_disk_db_mysql() {
get_disk_db_pgsql() {
# Defining vars
host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
@ -882,7 +806,7 @@ get_disk_db_pgsql() {
# Checking empty vars
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -890,7 +814,7 @@ get_disk_db_pgsql() {
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $V_EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
@ -915,3 +839,29 @@ get_disk_db_pgsql() {
echo "$size"
fi
}
increase_dbhost_values() {
conf="$VESTA/conf/$type.conf"
# Parsing conf
dbhost_str=$(grep -n "HOST='$host'" $conf)
str_number=$(echo $dbhost_str | cut -f 1 -d ':')
str=$(echo $dbhost_str | cut -f 2 -d ':')
# Reading key=values
eval $str
# Defining 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 "$str_number s/$c_key='${old//\*/\\*}'/$c_key='${new//\*/\\*}'/g"\
$conf
}

View file

@ -4,44 +4,32 @@ is_domain_new() {
dom=${2-$domain}
check_all=$(grep -w $dom $V_USERS/*/*.conf)
if [ ! -z "$check_all" ]; then
check_ownership=$(grep -w $dom $V_USERS/$user/*.conf)
check_ownership=$(grep -w $dom $USER_DATA/*.conf)
if [ ! -z "$check_ownership" ]; then
check_type=$(grep -w $dom $V_USERS/$user/$config_type.conf)
check_type=$(grep -w $dom $USER_DATA/$config_type.conf)
if [ ! -z "$check_type" ]; then
echo "Error: domain $dom exist"
log_event 'debug' "$E_EXISTS $V_EVENT"
log_event 'debug' "$E_EXISTS $EVENT"
exit $E_EXISTS
fi
else
echo "Error: domain $dom exist"
log_event 'debug' "$E_EXISTS $V_EVENT"
log_event 'debug' "$E_EXISTS $EVENT"
exit $E_EXISTS
fi
fi
}
is_domain_valid() {
# Parsing domain values
check_domain=$(grep -F "DOMAIN='$domain'" $V_USERS/$user/$1.conf)
# Checking result
if [ -z "$check_domain" ]; then
echo "Error: domain $domain not exist"
log_event 'debug' "$E_NOTEXIST $V_EVENT"
exit $E_NOTEXIST
fi
}
is_domain_suspended() {
config_type="$1"
# Parsing domain values
check_domain=$(grep "DOMAIN='$domain'" $V_USERS/$user/$config_type.conf|\
check_domain=$(grep "DOMAIN='$domain'" $USER_DATA/$config_type.conf|\
grep "SUSPENDED='yes'")
# Checking result
if [ ! -z "$check_domain" ]; then
echo "Error: domain $domain is suspended"
log_event 'debug' "$E_SUSPENDED $V_EVENT"
log_event 'debug' "$E_SUSPENDED $EVENT"
exit $E_SUSPENDED
fi
}
@ -49,20 +37,20 @@ is_domain_suspended() {
is_domain_unsuspended() {
config_type="$1"
# Parsing domain values
check_domain=$(grep "DOMAIN='$domain'" $V_USERS/$user/$config_type.conf|\
check_domain=$(grep "DOMAIN='$domain'" $USER_DATA/$config_type.conf|\
grep "SUSPENDED='no'")
# Checking result
if [ ! -z "$check_domain" ]; then
echo "Error: domain unsuspended"
log_event 'debug' "$E_UNSUSPENDED $V_EVENT"
log_event 'debug' "$E_UNSUSPENDED $EVENT"
exit $E_UNSUSPENDED
fi
}
update_domain_zone() {
# Definigng variables
line=$(grep "DOMAIN='$domain'" $V_USERS/$user/dns.conf)
line=$(grep "DOMAIN='$domain'" $USER_DATA/dns.conf)
fields='$RECORD\t$TTL\tIN\t$TYPE\t$VALUE'
# Checking serial
@ -113,12 +101,12 @@ update_domain_zone() {
RECORD=$(idn --quiet -a -t "$RECORD")
#VALUE=$(idn --quiet -a -t "$VALUE")
eval echo -e "\"$fields\""|sed -e "s/%quote%/'/g" >> $conf
done < $V_USERS/$user/dns/$domain
done < $USER_DATA/dns/$domain
}
get_next_dns_record() {
# Parsing config
curr_str=$(grep "ID=" $V_USERS/$user/dns/$domain|cut -f 2 -d \'|\
curr_str=$(grep "ID=" $USER_DATA/dns/$domain|cut -f 2 -d \'|\
sort -n|tail -n1)
# Print result
@ -127,18 +115,18 @@ get_next_dns_record() {
is_dns_record_free() {
# Checking record id
check_id=$(grep "ID='$id'" $V_USERS/$user/dns/$domain)
check_id=$(grep "ID='$id'" $USER_DATA/dns/$domain)
if [ ! -z "$check_id" ]; then
echo "Error: ID exist"
log_event 'debug' "$E_EXISTS $V_EVENT"
log_event 'debug' "$E_EXISTS $EVENT"
exit $E_EXISTS
fi
}
sort_dns_records() {
# Defining conf
conf="$V_USERS/$user/dns/$domain"
conf="$USER_DATA/dns/$domain"
cat $conf |sort -n -k 2 -t \' >$conf.tmp
mv -f $conf.tmp $conf
}
@ -156,7 +144,7 @@ add_web_config() {
-e "s/%domain%/$domain/g" \
-e "s/%user%/$user/g" \
-e "s/%group%/$group/g" \
-e "s/%home%/${V_HOME////\/}/g" \
-e "s/%home%/${HOMEDIR////\/}/g" \
-e "s/%docroot%/${docroot////\/}/g" \
-e "s/%docroot_string%/${docroot_string////\/}/g" \
-e "s/%email%/$email/g" \
@ -178,7 +166,7 @@ get_web_config_brds() {
# Defining template borders
serv_line=$(grep -ni 'Name %domain_idn%' "$tpl_file" |cut -f 1 -d :)
if [ -z "$serv_line" ]; then
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
return $E_PARSING
fi
@ -244,11 +232,13 @@ replace_web_config() {
}
get_domain_value() {
domain_type="$1"
conf_type="$1"
key="$2"
default_str="DOMAIN='$domain'"
search_str="${3-DOMAIN=$search_str}"
# Parsing domains
string=$( grep "DOMAIN='$domain'" $V_USERS/$user/$domain_type.conf )
# Parsing config
string=$(grep "$search_str" $USER_DATA/$conf_type.conf )
# Parsing key=value
eval $string
@ -262,22 +252,24 @@ get_domain_value() {
get_domain_values() {
# Defining domain parameters
for line in $(grep "DOMAIN='$domain'" $V_USERS/$user/$1.conf); do
for line in $(grep "DOMAIN='$domain'" $USER_DATA/$1.conf); do
# Assing key=value
eval $line
done
}
update_domain_value() {
domain_type="$1"
conf_type="$1"
key="$2"
value="$3"
default_str="DOMAIN='$domain'"
search_str=${4-$default_str}
# Defining conf
conf="$V_USERS/$user/$domain_type.conf"
conf="$USER_DATA/$conf_type.conf"
# Parsing conf
domain_str=$(grep -n "DOMAIN='$domain'" $conf)
domain_str=$(grep -n "$search_str" $conf)
str_number=$(echo $domain_str | cut -f 1 -d ':')
str=$(echo $domain_str | cut -f 2 -d ':')
@ -299,11 +291,11 @@ update_domain_value() {
}
is_domain_key_empty() {
domain_type="$1"
conf_type="$1"
key="$2"
# Parsing domains
string=$( grep "DOMAIN='$domain'" $V_USERS/$user/$domain_type.conf )
string=$( grep "DOMAIN='$domain'" $USER_DATA/$conf_type.conf )
# Parsing key=value
eval $string
@ -314,7 +306,7 @@ is_domain_key_empty() {
# Checkng key
if [ ! -z "$value" ] && [ "$value" != 'no' ]; then
echo "Error: ${key//$} is not empty = $value"
log_event 'debug' "$E_EXISTS $V_EVENT"
log_event 'debug' "$E_EXISTS $EVENT"
exit $E_EXISTS
fi
}
@ -324,7 +316,7 @@ is_web_domain_cert_valid() {
# Checking file existance
if [ ! -e "$ssl_dir/$domain.crt" ] || [ ! -e "$ssl_dir/$domain.key" ]; then
echo "Error: ssl certificate not exist"
log_event 'debug' "$E_NOTEXIST $V_EVENT"
log_event 'debug' "$E_NOTEXIST $EVENT"
exit $E_NOTEXIST
fi
@ -332,7 +324,7 @@ is_web_domain_cert_valid() {
crt=$(openssl verify $ssl_dir/$domain.crt 2>/dev/null |grep '/C=')
if [ -z "$crt" ]; then
echo "Error: ssl certificate invalid"
log_event 'debug' "$E_INVALID $V_EVENT"
log_event 'debug' "$E_INVALID $EVENT"
exit $E_INVALID
fi
@ -340,7 +332,7 @@ is_web_domain_cert_valid() {
openssl rsa -in "$ssl_dir/$domain.key" -check >/dev/null 2>/dev/null
if [ "$?" -ne 0 ]; then
echo "Error: ssl key invalid"
log_event 'debug' "$E_INVALID $V_EVENT"
log_event 'debug' "$E_INVALID $EVENT"
exit $E_INVALID
fi
@ -349,7 +341,7 @@ is_web_domain_cert_valid() {
ca=$(openssl verify $ssl_dir/$domain.ca 2>/dev/null |grep '/C=')
if [ -z "$ca" ]; then
echo "Error: ssl certificate invalid"
log_event 'debug' "$E_INVALID $V_EVENT"
log_event 'debug' "$E_INVALID $EVENT"
exit $E_INVALID
fi
fi
@ -364,18 +356,18 @@ is_web_domain_cert_valid() {
result=$?
if [ "$result" -ne '0' ]; then
echo "Error: ssl certificate key pair invalid"
log_event 'debug' "$E_INVALID $V_EVENT"
log_event 'debug' "$E_INVALID $EVENT"
exit $E_INVALID
fi
}
is_dns_record_valid() {
# Checking record id
check_id=$(grep "^ID='$id'" $V_USERS/$user/dns/$domain)
check_id=$(grep "^ID='$id'" $USER_DATA/dns/$domain)
if [ -z "$check_id" ]; then
echo "Error: ID not exist"
log_event 'debug' "$E_NOTEXIST $V_EVENT"
log_event 'debug' "$E_NOTEXIST $EVENT"
exit $E_NOTEXIST
fi
}
@ -385,7 +377,7 @@ is_domain_value_exist() {
key="$2"
# Parsing domains
string=$( grep "DOMAIN='$domain'" $V_USERS/$user/$domain_type.conf )
string=$( grep "DOMAIN='$domain'" $USER_DATA/$domain_type.conf )
# Parsing key=value
eval $string
@ -396,7 +388,7 @@ is_domain_value_exist() {
# Checking result
if [ -z "$value" ] || [ "$value" = 'no' ]; then
echo "Error: ${key//$/} is empty"
log_event 'debug' "$E_NOTEXIST $V_EVENT"
log_event 'debug' "$E_NOTEXIST $EVENT"
exit $E_NOTEXIST
fi
}
@ -455,7 +447,7 @@ namehost_ip_support() {
# Checking proxy support
if [ "$PROXY_SYSTEM" = 'nginx' ]; then
cat $V_WEBTPL/ngingx_ip.tpl | sed -e "s/%ip%/$ip/g" \
cat $WEBTPL/ngingx_ip.tpl | sed -e "s/%ip%/$ip/g" \
-e "s/%web_port%/$WEB_PORT/g" \
-e "s/%proxy_port%/$PROXY_PORT/g" >>$nconf
@ -477,8 +469,8 @@ namehost_ip_disable() {
# Checking proxy support
if [ "$PROXY_SYSTEM" = 'nginx' ]; then
tpl_ln=$(wc -l $V_WEBTPL/ngingx_ip.tpl | cut -f 1 -d ' ')
ip_line=$(grep -n "%ip%" $V_WEBTPL/ngingx_ip.tpl |head -n1 |\
tpl_ln=$(wc -l $WEBTPL/ngingx_ip.tpl | cut -f 1 -d ' ')
ip_line=$(grep -n "%ip%" $WEBTPL/ngingx_ip.tpl |head -n1 |\
cut -f 1 -d :)
conf_line=$(grep -n -w $ip $nconf|head -n1|cut -f 1 -d :)
@ -487,7 +479,7 @@ namehost_ip_disable() {
if [ -z "$tpl_ln" ] || [ -z "$ip_line" ] || [ -z "$conf_line" ]
then
echo "Error: nginx config paring error"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -498,7 +490,7 @@ namehost_ip_disable() {
# Checking parsed lines
if [ -z "$first_line" ] || [ -z "$last_line" ]; then
echo "Error: nginx config paring error"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
sed -i "$first_line,$last_line d" $nconf
@ -518,7 +510,7 @@ upd_web_domain_values() {
ip=$IP
group="$user"
email="$user@$domain"
docroot="$V_HOME/$user/web/$domain/public_html"
docroot="$HOMEDIR/$user/web/$domain/public_html"
docroot_string="DocumentRoot $docroot"
proxy_string="proxy_pass http://$ip:$WEB_PORT;"
@ -584,17 +576,36 @@ upd_web_domain_values() {
fi
# Defining SSL vars
ssl_crt="$V_HOME/$user/conf/web/ssl.$domain.crt"
ssl_key="$V_HOME/$user/conf/web/ssl.$domain.key"
ssl_pem="$V_HOME/$user/conf/web/ssl.$domain.pem"
ssl_ca="$V_HOME/$user/conf/web/ssl.$domain.ca"
if [ ! -e "$V_USERS/$user/ssl/$domain.ca" ]; then
ssl_crt="$HOMEDIR/$user/conf/web/ssl.$domain.crt"
ssl_key="$HOMEDIR/$user/conf/web/ssl.$domain.key"
ssl_pem="$HOMEDIR/$user/conf/web/ssl.$domain.pem"
ssl_ca="$HOMEDIR/$user/conf/web/ssl.$domain.ca"
if [ ! -e "$USER_DATA/ssl/$domain.ca" ]; then
ssl_ca_str='#'
fi
case $SSL_HOME in
single) docroot="$V_HOME/$user/web/$domain/public_shtml" ;;
same) docroot="$V_HOME/$user/web/$domain/public_html" ;;
single) docroot="$HOMEDIR/$user/web/$domain/public_shtml" ;;
same) docroot="$HOMEDIR/$user/web/$domain/public_html" ;;
esac
}
is_mail_account_free() {
acc=${1-$account}
check_acc=$(grep -w $acc $USER_DATA/mail/$domain.conf)
if [ ! -z "$check_acc" ]; then
echo "Error: account $acc exists"
log_event 'debug' "$E_EXISTS $EVENT"
exit $E_EXISTS
fi
}
is_mail_account_valid() {
acc=${1-$account}
check_acc=$(grep -w $acc $USER_DATA/mail/$domain.conf)
if [ -z "$check_acc" ]; then
echo "Error: account $acc not exist"
log_event 'debug' "$E_NOTEXIST $EVENT"
exit $E_NOTEXIST
fi
}

View file

@ -4,9 +4,9 @@ is_sys_ip_free() {
# Checking ip existance
ip_check=$(echo "$ip_list"|grep -w "$ip")
if [ -n "$ip_check" ] || [ -e "$V_IPS/$ip" ]; then
if [ -n "$ip_check" ] || [ -e "$VESTA/data/ips/$ip" ]; then
echo "Error: IP exist"
log_event 'debug' "$E_EXISTS $V_EVENT"
log_event 'debug' "$E_EXISTS $EVENT"
exit $E_EXISTS
fi
}
@ -30,9 +30,9 @@ is_sys_ip_valid() {
check_ifc=$(/sbin/ifconfig |grep "inet addr:$ip")
# Checking ip existance
if [ ! -e "$V_IPS/$ip" ] || [ -z "$check_ifc" ]; then
if [ ! -e "$VESTA/data/ips/$ip" ] || [ -z "$check_ifc" ]; then
echo "Error: IP not exist"
log_event 'debug' "$E_NOTEXIST $V_EVENT"
log_event 'debug' "$E_NOTEXIST $EVENT"
exit $E_NOTEXIST
fi
}
@ -41,7 +41,7 @@ is_ip_key_empty() {
key="$1"
# Parsing ip
string=$(cat $V_IPS/$ip )
string=$(cat $VESTA/data/ips/$ip )
# Parsing key=value
for keys in $string; do
@ -54,7 +54,7 @@ is_ip_key_empty() {
# Checkng key
if [ ! -z "$value" ] && [ "$value" != '0' ]; then
echo "Error: value is not empty = $value "
log_event 'debug' "$E_EXISTS $V_EVENT"
log_event 'debug' "$E_EXISTS $EVENT"
exit $E_EXISTS
fi
}
@ -64,7 +64,7 @@ update_sys_ip_value() {
value="$2"
# Defining conf
conf="$V_IPS/$ip"
conf="$VESTA/data/ips/$ip"
# Parsing conf
str=$(cat $conf)
@ -90,14 +90,14 @@ update_sys_ip_value() {
is_ip_avalable() {
# Checking ip existance
if [ ! -e "$V_IPS/$ip" ]; then
if [ ! -e "$VESTA/data/ips/$ip" ]; then
echo "Error: IP not exist"
log_event 'debug' "$E_NOTEXIST $V_EVENT"
log_event 'debug' "$E_NOTEXIST $EVENT"
exit $E_NOTEXIST
fi
# Parsing ip data
ip_data=$(cat $V_IPS/$ip)
ip_data=$(cat $VESTA/data/ips/$ip)
ip_owner=$(echo "$ip_data" | grep 'OWNER=' | cut -f 2 -d \' )
ip_status=$(echo "$ip_data" | grep 'STATUS=' | cut -f 2 -d \' )
@ -110,7 +110,7 @@ is_ip_avalable() {
if [ "$ip_owner" != "$user" ] && [ "$ip_shared" != 'yes' ]; then
echo "Error: ip not owned by user"
log_event 'debug' "$E_FORBIDEN $V_EVENT"
log_event 'debug' "$E_FORBIDEN $EVENT"
exit $E_FORBIDEN
fi
}
@ -118,17 +118,17 @@ is_ip_avalable() {
is_sys_ip_owner() {
# Parsing ip
ip="$IP"
ip_owner=$(grep 'OWNER=' $V_IPS/$ip|cut -f 2 -d \')
ip_owner=$(grep 'OWNER=' $VESTA/data/ips/$ip|cut -f 2 -d \')
if [ "$ip_owner" != "$user" ]; then
echo "Error: IP not owned"
log_event 'debug' "$E_FORBIDEN $V_EVENT"
log_event 'debug' "$E_FORBIDEN $EVENT"
exit $E_FORBIDEN
fi
}
get_ip_name() {
# Prinitng name
grep "NAME=" $V_IPS/$ip |cut -f 2 -d \'
grep "NAME=" $VESTA/data/ips/$ip |cut -f 2 -d \'
}
increase_ip_value() {
@ -138,13 +138,13 @@ increase_ip_value() {
usr_key='U_SYS_USERS'
# Parsing values
current_web=$(grep "$web_key=" $V_IPS/$sip |cut -f 2 -d \')
current_usr=$(grep "$usr_key=" $V_IPS/$sip |cut -f 2 -d \')
current_web=$(grep "$web_key=" $VESTA/data/ips/$sip |cut -f 2 -d \')
current_usr=$(grep "$usr_key=" $VESTA/data/ips/$sip |cut -f 2 -d \')
# Checking result
if [ -z "$current_web" ]; then
echo "Error: Parsing error"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -164,8 +164,10 @@ increase_ip_value() {
fi
# Changing config
sed -i "s/$web_key='$current_web'/$web_key='$new_web'/g" $V_IPS/$ip
sed -i "s/$usr_key='$current_usr'/$usr_key='$new_usr'/g" $V_IPS/$ip
sed -i "s/$web_key='$current_web'/$web_key='$new_web'/g" \
$VESTA/data/ips/$ip
sed -i "s/$usr_key='$current_usr'/$usr_key='$new_usr'/g" \
$VESTA/data/ips/$ip
}
decrease_ip_value() {
@ -175,13 +177,13 @@ decrease_ip_value() {
usr_key='U_SYS_USERS'
# Parsing values
current_web=$(grep "$web_key=" $V_IPS/$sip |cut -f 2 -d \')
current_usr=$(grep "$usr_key=" $V_IPS/$sip |cut -f 2 -d \')
current_web=$(grep "$web_key=" $VESTA/data/ips/$sip |cut -f 2 -d \')
current_usr=$(grep "$usr_key=" $VESTA/data/ips/$sip |cut -f 2 -d \')
# Checking result
if [ -z "$current_web" ]; then
echo "Error: Parsing error"
log_event 'debug' "$E_PARSING $V_EVENT"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
@ -189,7 +191,7 @@ decrease_ip_value() {
new_web=$((current_web - 1))
# -1 user
check_ip=$(grep $sip $V_USERS/$user/web.conf |wc -l)
check_ip=$(grep $sip $USER_DATA/web.conf |wc -l)
if [ "$check_ip" -lt 2 ]; then
new_usr=$(echo "$current_usr" |\
sed -e "s/,/\n/g"|\
@ -201,15 +203,17 @@ decrease_ip_value() {
fi
# Changing config
sed -i "s/$web_key='$current_web'/$web_key='$new_web'/g" $V_IPS/$sip
sed -i "s/$usr_key='$current_usr'/$usr_key='$new_usr'/g" $V_IPS/$sip
sed -i "s/$web_key='$current_web'/$web_key='$new_web'/g" \
$VESTA/data/ips/$sip
sed -i "s/$usr_key='$current_usr'/$usr_key='$new_usr'/g" \
$VESTA/data/ips/$sip
}
get_sys_ip_value() {
key="$1"
# Parsing domains
string=$( cat $V_IPS/$ip )
string=$( cat $VESTA/data/ips/$ip )
# Parsing key=value
for keys in $string; do
@ -230,7 +234,7 @@ get_current_interface() {
# Checking result
if [ -z "$i" ]; then
echo "Error: IP not exist"
log_event 'debug' "$E_NOTEXIST $V_EVENT"
log_event 'debug' "$E_NOTEXIST $EVENT"
exit $E_NOTEXIST
fi
@ -238,7 +242,7 @@ get_current_interface() {
check_alias=$(echo $i| cut -s -f 2 -d :)
if [ -z "$check_alias" ]; then
echo "Error: Main IP on interface"
log_event 'debug' "$E_FORBIDEN $V_EVENT"
log_event 'debug' "$E_FORBIDEN $EVENT"
exit $E_FORBIDEN
fi
echo "$i"
@ -253,16 +257,16 @@ ip_add_vesta() {
ip_data="$ip_data\nU_WEB_DOMAINS='0'"
ip_data="$ip_data\nINTERFACE='$interface'"
ip_data="$ip_data\nNETMASK='$mask'"
ip_data="$ip_data\nDATE='$V_DATE'"
ip_data="$ip_data\nDATE='$DATE'"
# Adding ip
echo -e "$ip_data" >$V_IPS/$ip
chmod 660 $V_IPS/$ip
echo -e "$ip_data" >$VESTA/data/ips/$ip
chmod 660 $VESTA/data/ips/$ip
}
ip_add_startup() {
# Filling ip values
ip_data="# Added by vesta $V_SCRIPT"
ip_data="# Added by vesta $SCRIPT"
ip_data="$ip_data\nDEVICE=$iface"
ip_data="$ip_data\nBOOTPROTO=static\nONBOOT=yes"
ip_data="$ip_data\nIPADDR=$ip"
@ -273,8 +277,8 @@ ip_add_startup() {
}
ip_owner_search(){
for ip in $(ls $V_IPS/); do
check_owner=$(grep "OWNER='$user'" $V_IPS/$ip)
for ip in $(ls $VESTA/data/ips/); do
check_owner=$(grep "OWNER='$user'" $VESTA/data/ips/$ip)
if [ ! -z "$check_owner" ]; then
echo "$ip"
fi

View file

@ -1,804 +0,0 @@
# 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"
}

694
func/shared.sh Normal file
View file

@ -0,0 +1,694 @@
# Internal variables
DATE=$(date +%F)
TIME=$(date +%T)
SCRIPT=$(basename $0)
EVENT="DATE='$DATE' TIME='$TIME' COMMAND='$SCRIPT' ARGUMENTS='$*'"
BACKUP_GZIP=5
BACKUP_DISK_LIMIT=95
BACKUP_LA_LIMIT=5
RRD_STEP=300
RRD_IFACE_EXCLUDE=lo
BIN=$VESTA/bin
USER_DATA=$VESTA/data/users/$user
WEBTPL=$VESTA/data/templates/web
DNSTPL=$VESTA/data/templates/dns
RRD=$VESTA/web/rrd
# Return codes
OK=0
E_ARGS=1
E_INVALID=2
E_NOTEXIST=3
E_EXISTS=4
E_SUSPENDED=5
E_UNSUSPENDED=6
E_INUSE=7
E_LIMIT=8
E_PASSWORD=9
E_FORBIDEN=10
E_DISABLED=11
E_PARSING=12
E_DISK=13
E_LA=14
E_FTP=15
E_SSH=16
E_DB=17
E_RRD=18
E_UPDATE=19
E_RESTART=20
# Log event function
log_event() {
echo "$1 $2" >> $VESTA/log/system.log
}
# Log user history
log_history() {
echo "$1" >> $USER_DATA/history.log
}
# Argument list checker
check_args() {
if [ "$1" -gt "$2" ]; then
echo "Error: bad args"
echo "Usage: $SCRIPT $3"
log_event "$E_ARGS" "$EVENT"
exit $E_ARGS
fi
}
# Subsystem checker
is_system_enabled() {
if [ -z "$1" ] || [ "$1" = no ]; then
echo "Error: subsystem disabled"
log_event "$E_DISABLED" "$EVENT"
exit $E_DISABLED
fi
}
# User package check
is_package_full() {
case "$1" in
WEB_DOMAINS) used=$(wc -l $USER_DATA/web.conf|cut -f1 -d \ );;
WEB_ALIASES) used=$(grep "DOMAIN='$domain'" $USER_DATA/web.conf |\
awk -F "ALIAS='" '{print $2}' | cut -f 1 -d \' | 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 |cut -f1 -d \ );;
MAIL_DOMAINS) used=$(wc -l $USER_DATA/mail.conf |cut -f1 -d \ );;
MAIL_ACCOUNTS) used=$(wc -l $USER_DATA/mail/$domain |\
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 \ );;
esac
limit=$(grep "^$1=" $USER_DATA/user.conf | cut -f 2 -d \' )
if [ "$used" -ge "$limit" ]; then
echo "Error: Upgrade package"
log_event "$E_LIMIT" "$EVENT"
exit $E_LIMIT
fi
}
# Random password generator
gen_password() {
matrix='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
lenght=10
while [ ${n:=1} -le $lenght ]; do
pass="$pass${matrix:$(($RANDOM%${#matrix})):1}"
let n+=1
done
echo "$pass"
}
# Web template check
is_apache_template_valid() {
c=$(echo "$templates" | grep -w "$template")
t="$WEBTPL/apache_$template.tpl"
d="$WEBTPL/apache_$template.descr"
s="$WEBTPL/apache_$template.stpl"
if [ -z "$c" ] || [ ! -e $t ] || [ ! -e $d ] || [ ! -e $s ]; then
echo "Error: $template not found"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
}
# Nginx template check
is_nginx_template_valid() {
t="$WEBTPL/ngingx_vhost_$template.tpl"
d="$WEBTPL/ngingx_vhost_$template.descr"
s="$WEBTPL/ngingx_vhost_$template.stpl"
if [ ! -e $t ] || [ ! -e $d ] || [ ! -e $s ]; then
echo "Error: $template not found"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
}
# DNS template check
is_dns_template_valid() {
tpl="$DNSTPL/$template.tpl"
descr="$DNSTPL/$template.descr"
if [ ! -e $tpl ] || [ ! -e $descr ]; then
echo "Error: template not found"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
}
# Package existance check
is_package_valid() {
if [ ! -e "$VESTA/data/packages/$package.pkg" ]; then
echo "Error: $package is not exist"
log_event "$E_NOTEXIST $EVENT"
exit $E_NOTEXIST
fi
}
# Validate system type
is_type_valid() {
if [ -z "$(echo $1 | grep -w $2)" ]; then
echo "Error: unknown type"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# Checkk user backup settings
is_backup_enabled() {
BACKUPS=$(grep "BACKUPS='" $USER_DATA/user.conf | cut -f 2 -d \')
if [ -z "$BACKUPS" ] || [[ "$BACKUPS" -le '0' ]]; then
echo "Error: user backup disabled"
log_event "$E_DISABLED" "$EVENT"
exit $E_DISABLED
fi
}
# Check if object is free and can be created
is_object_free() {
if [ $2 = 'USER' ]; then
if [ -d "$USER_DATA" ]; then
object="OK"
fi
else
object=$(grep "$2='$3'" $USER_DATA/$1.conf)
fi
if [ ! -z "$object" ]; then
echo "Error: $3 exists"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
}
# Check if object exists and can be used
is_object_valid() {
if [ $2 = 'USER' ]; then
if [ -d "$VESTA/data/users/$user" ]; then
object="OK"
fi
else
if [ $2 = 'DBHOST' ]; then
object=$(grep "HOST='$host'" $VESTA/conf/$type.conf)
else
object=$(grep "$2='$3'" $VESTA/data/users/$user/$1.conf)
fi
fi
if [ -z "$object" ]; then
echo "Error: $3 not exist"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
}
# Check if object is supended
is_object_suspended() {
if [ $2 = 'USER' ]; then
spnd=$(cat $USER_DATA/$1.conf|grep "SUSPENDED='yes'")
else
spnd=$(grep "$2='$3'" $USER_DATA/$1.conf|grep "SUSPENDED='yes'")
fi
if [ ! -z "$spnd" ]; then
echo "Error: $3 is suspended"
log_event "$E_SUSPENDED" "$EVENT"
exit $E_SUSPENDED
fi
}
# Check if object is unsupended
is_object_unsuspended() {
if [ $2 = 'USER' ]; then
spnd=$(cat $USER_DATA/$1.conf|grep "SUSPENDED='yes'")
else
spnd=$(grep "$2='$3'" $USER_DATA/$1.conf|grep "SUSPENDED='yes'")
fi
if [ -z "$spnd" ]; then
echo "Error: $3 is not suspended"
log_event "$E_UNSUSPENDED" "$EVENT"
exit $E_UNSUSPENDED
fi
}
# Update object value
update_object_value() {
row=$(grep -n "$2='$3'" $USER_DATA/$1.conf)
lnr=$(echo $row | cut -f 1 -d ':')
object=$(echo $row | sed -e "s/^$lnr://")
eval "$object"
eval old="$4"
old=$(echo "$old" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
new=$(echo "$5" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
sed -i "$lnr s/${4//$/}='${old//\*/\\*}'/${4//$/}='${new//\*/\\*}'/g" \
$USER_DATA/$1.conf
}
# Search objects
search_objects() {
OLD_IFS="$IFS"
IFS=$'\n'
for line in $(grep $2=\'$3\' $USER_DATA/$1.conf); do
eval $line
eval echo \$$4
done
IFS="$OLD_IFS"
}
# Get user value
get_user_value() {
grep "^$1=" $USER_DATA/user.conf| cut -f 2 -d \'
}
# Update user value in user.conf
update_user_value() {
key="${2//$}"
conf="$VESTA/data/users/$1/user.conf"
old=$(grep "$key=" $conf | cut -f 2 -d \')
sed -i "s/$key='$old'/$key='$3'/g" $conf
}
# Increase user counter
increase_user_value() {
key="${2//$}"
factor="${3-1}"
conf="$VESTA/data/users/$1/user.conf"
old=$(grep "$key=" $conf | cut -f 2 -d \')
if [ -z "$old" ]; then
old=0
fi
new=$((old + factor))
sed -i "s/$key='$old'/$key='$new'/g" $conf
}
# Decrease user counter
decrease_user_value() {
key="${2//$}"
factor="${3-1}"
conf="$VESTA/data/users/$1/user.conf"
old=$(grep "$key=" $conf | cut -f 2 -d \')
if [ -z "$old" ]; then
old=0
fi
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() {
echo '{'
fileds_count=$(echo $fields| wc -w )
while read line; do
eval $line
if [ -n "$data_output" ]; then
echo -e ' },'
fi
i=1
for field in $fields; do
eval value=$field
if [ $i -eq 1 ]; then
(( ++i))
echo -e "\t\"$value\": {"
else
if [ $i -lt $fileds_count ]; then
(( ++i))
echo -e "\t\t\"${field//$/}\": \"$value\","
else
echo -e "\t\t\"${field//$/}\": \"$value\""
data_output=yes
fi
fi
done
done < $conf
if [ "$data_output" = 'yes' ]; then
echo -e ' }'
fi
echo -e '}'
}
# Shell listing function
shell_list() {
if [ -z "$nohead" ] ; then
echo "${fields//$/}"
for a in $fields; do
echo -e "------ \c"
done
echo
fi
while read line ; do
eval $line
for field in $fields; do
eval value=$field
if [ -z "$value" ]; then
value='NULL'
fi
echo -n "$value "
done
echo
done < $conf
}
# Recalculate U_DISK value
recalc_user_disk_usage() {
usage=$(grep 'U_DIR_DISK=' $USER_DATA/user.conf | cut -f 2 -d "'")
for conf_type in mail db web; do
if [ -f "$USER_DATA/$conf_type.conf" ]; then
dusage=$(grep 'U_DISK=' $USER_DATA/$conf_type.conf |\
awk -F "U_DISK='" '{print $2}' | cut -f 1 -d \')
for disk in $dusage; do
usage=$((usage + disk))
done
fi
done
old=$(grep "U_DISK='" $USER_DATA/user.conf | cut -f 2 -d \')
sed -i "s/U_DISK='$old'/U_DISK='$usage'/g" $USER_DATA/user.conf
}
# Recalculate U_BANDWIDTH value
recalc_user_bandwidth_usage() {
usage=0
bandwidth_usage=$(grep 'U_BANDWIDTH=' $USER_DATA/web.conf |\
awk -F "U_BANDWIDTH='" '{print $2}'|cut -f 1 -d \')
for bandwidth in $bandwidth_usage; do
usage=$((usage + bandwidth))
done
old=$(grep "U_BANDWIDTH='" $USER_DATA/user.conf | cut -f 2 -d \')
sed -i "s/U_BANDWIDTH='$old'/U_BANDWIDTH='$usage'/g" $USER_DATA/user.conf
}
# Get next cron job id
get_next_cronjob() {
if [ -z "$job" ]; then
curr_str=$(grep "JOB=" $USER_DATA/cron.conf|cut -f 2 -d \'|\
sort -n|tail -n1)
job="$((curr_str +1))"
fi
}
# Sort cron jobs by id
sort_cron_jobs() {
cat $USER_DATA/cron.conf |sort -n -k 2 -t \' > $USER_DATA/cron.tmp
mv -f $USER_DATA/cron.tmp $USER_DATA/cron.conf
}
# Sync cronjobs with system cron
sync_cron_jobs() {
source $USER_DATA/user.conf
rm -f /var/spool/cron/$user
if [ "$CRON_REPORTS" = 'yes' ]; then
echo "MAILTO=$CONTACT" > /var/spool/cron/$user
fi
while read line ; do
eval $line
if [ "$SUSPENDED" = 'no' ] ; then
echo "$MIN $HOUR $DAY $MONTH $WDAY $CMD" |\
sed -e "s/%quote%/'/g" -e "s/%dots%/:/g" |\
>> /var/spool/cron/$user
fi
done < $USER_DATA/cron.conf
}
### Format Validators ###
# URL
validate_format_url() {
check_http=$(echo "$1" | grep "^http://" )
needed_chars=$(echo "$1" | cut -f 2 -d \.)
if [ -z "$check_http" ] || [ -z "$needed_chars" ]; then
echo "Error: url $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# Shell
validate_format_shell() {
if [ -z "$(grep -x $1 /etc/shells)" ]; then
echo "Error: shell $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# Password
validate_format_password() {
if [ "${#1}" -lt '6' ]; then
echo "Error: password is too short"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# Integer
validate_format_int() {
if ! [[ "$1" =~ ^[0-9]+$ ]] ; then
echo "Error: int $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# Boolean
validate_format_boolean() {
if [ "$1" != 'yes' ] || [ "$1" != 'no' ]; then
echo "Error: boolean $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# Network interface
validate_format_interface() {
netdevices=$(cat /proc/net/dev | grep : | cut -f 1 -d : | tr -d ' ')
if [ -z $(echo "$netdevices"| grep -x $1) ]; then
echo "Error: intreface $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# IP address
validate_format_ip() {
valid_octets=0
for octet in ${1//./ }; do
if [[ $octet =~ ^[0-9]{1,3}$ ]] && [[ $octet -le 255 ]]; then
((++valid_octets))
fi
done
if [ "$valid_octets" -lt 4 ]; then
echo "Error: ip $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# IP address status
validate_format_ip_status() {
if [ -z "$(echo shared,dedicated | grep -w $1 )" ]; then
echo "Error: ip_status $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# Email address
validate_format_email() {
local_part=$(echo $1 | cut -s -f1 -d\@)
remote_host=$(echo $1 | cut -s -f2 -d\@)
mx_failed=1
if [ ! -z "$remote_host" ] && [ ! -z "$local_part" ]; then
/usr/bin/host -t mx "$remote_host" &> /dev/null
mx_failed="$?"
fi
if [ "$mx_failed" -eq 1 ]; then
echo "Error: email $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# Username
validate_format_username() {
if ! [[ "$1" =~ ^[0-Z]+(\.[0-Z]+)?$ ]] || [[ "${#1}" -gt 28 ]]; then
echo "Error: usernmae $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# Domain
validate_format_domain() {
exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|_|/|\|\"|'|;|%| ]"
dpart1=$(echo $1 | cut -f 1 -d .)
if [[ "$1" =~ $exclude ]] || [ -z "$dpart1" ]; then
echo "Error: domain $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# Database
validate_format_database() {
exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|.|<|>|?|/|\|\"|'|;|%| ]"
if [[ "$1" =~ $exclude ]] || [ 17 -le ${#1} ]; then
echo "Error: database $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# DNS type
validate_format_dns_type() {
known_dnstype='A,AAAA,NS,CNAME,MX,TXT,SRV,DNSKEY,KEY,IPSECKEY,PTR,SPF'
if [ -z "$(echo $known_dnstype | grep -w $1)" ]; then
echo "Error: dnstype $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# DKIM key size
validate_format_key_size() {
known_size='128,256,512,768,1024,2048'
if [ -z "$(echo $known_size | grep -w $1)" ]; then
echo "Error: key_size $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# Minute / Hour / Day / Month / Day of Week
validate_format_mhdmw() {
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]+$ ]] && [ "$1" -lt $limit ]; then
check_format='ok'
fi
if [ "$check_format" != 'ok' ]; then
echo "Error: $2 $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# Nginx static extention or DNS record
validate_format_extentions() {
exclude="[!|@|#|$|^|&|(|)|+|=|{|}|:|<|>|?|/|\|\"|'|;|%| ]"
if [[ "$1" =~ $exclude ]] || [ 200 -le ${#1} ]; then
echo "Error: extention $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# DNS record value
validate_format_dvalue() {
record_types="$(echo A,AAAA,NS,CNAME | grep -w "$rtype")"
if [[ "$1" =~ [\ ] ]] && [ ! -z "$record_types" ]; then
echo "Error: dvalue $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
if [ "$rtype" = 'A' ]; then
validate_format_ip "$1"
fi
if [ "$rtype" = 'NS' ]; then
validate_format_domain "$1"
fi
}
# Date
validate_format_date() {
if ! [[ "$1" =~ ^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$ ]]; then
echo "Error: date $1 is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# Format validation controller
validate_format(){
for arg_name in $*; do
eval arg=\$$argument_name
case $var in
account) validate_format_username "$arg" ;;
antispam) validate_format_boolean "$arg" ;;
antivirus) validate_format_boolean "$arg" ;;
auth_pass) validate_format_password "$arg" ;;
auth_user) validate_format_username "$arg" ;;
backup) validate_format_date "$arg" ;;
database) validate_format_database "$arg" ;;
day) validate_format_mhdmw "$arg" $arg_name ;;
db_password) validate_format_password "$arg" ;;
db_user) validate_format_database "$arg" ;;
dkim) validate_format_boolean "$arg" ;;
dkim_size) validate_format_key_size "$arg" ;;
domain) validate_format_domain "$arg" ;;
dom_alias) validate_format_domain "$arg" ;;
dvalue) validate_format_dvalue "$arg";;
email) validate_format_email "$arg" ;;
encoding) validate_format_username "$arg" ;;
exp) validate_format_date "$arg" ;;
extentions) validate_format_extentions "$arg" ;;
fname) validate_format_username "$arg" ;;
host) validate_format_username "$arg" ;;
hour) validate_format_mhdmw "$arg" $arg_name ;;
id) validate_format_int "$arg" ;;
interface) validate_format_interface "$arg" ;;
ip) validate_format_ip "$arg" ;;
ip_name) validate_format_domain "$arg" ;;
ip_status) validate_format_ip_status "$arg" ;;
job) validate_format_int "$arg" ;;
key) validate_format_username "$arg" ;;
lname) validate_format_username "$arg" ;;
malias) validate_format_username "$arg" ;;
mask) validate_format_ip "$arg" ;;
max_db) validate_format_int "$arg" ;;
min) validate_format_mhdmw "$arg" $arg_name ;;
month) validate_format_mhdmw "$arg" $arg_name ;;
ns1) validate_format_domain "$arg" ;;
ns2) validate_format_domain "$arg" ;;
ns3) validate_format_domain "$arg" ;;
ns4) validate_format_domain "$arg" ;;
ns5) validate_format_domain "$arg" ;;
ns6) validate_format_domain "$arg" ;;
ns7) validate_format_domain "$arg" ;;
ns8) validate_format_domain "$arg" ;;
package) validate_format_username "$arg" ;;
password) validate_format_password "$arg" ;;
port) validate_format_int "$arg" ;;
quota) validate_format_int "$arg" ;;
restart) validate_format_boolean "$arg" ;;
record) validate_format_extentions "$arg" ;;
rtype) validate_format_dns_type "$arg" ;;
shell) validate_format_shell "$arg" ;;
soa) validate_format_domain "$arg" ;;
template) validate_format_username "$arg" ;;
ttl) validate_format_int "$arg" ;;
url) validate_format_url "$arg" ;;
user) validate_format_username "$arg" ;;
wday) validate_format_mhdmw "$arg" $arg_name ;;
esac
done
}