mirror of
https://github.com/serghey-rodin/vesta.git
synced 2025-08-21 05:44:07 -07:00
export from svn
This commit is contained in:
commit
641ed97fdd
340 changed files with 32404 additions and 0 deletions
128
func/cert_func.sh
Normal file
128
func/cert_func.sh
Normal file
|
@ -0,0 +1,128 @@
|
|||
is_cert_free() {
|
||||
# Defining path
|
||||
user_cert="$V_USERS/$user/cert/$cert"
|
||||
|
||||
# Checking file existance
|
||||
if [ -e "$user_cert.crt" ] || [ -e "$user_cert.key" ]; then
|
||||
echo "Error: certificate exist"
|
||||
log_event 'debug' "$E_CERT_EXIST $V_EVENT"
|
||||
exit $E_CERT_EXIST
|
||||
fi
|
||||
}
|
||||
|
||||
is_cert_valid() {
|
||||
path="$1"
|
||||
|
||||
# Checking file existance
|
||||
if [ ! -e "$path/$cert.crt" ] || [ ! -e "$path/$cert.key" ]; then
|
||||
echo "Error: certificate not exist"
|
||||
log_event 'debug' "$E_CERT_NOTEXIST $V_EVENT"
|
||||
exit $E_CERT_NOTEXIST
|
||||
fi
|
||||
|
||||
# Checking crt file
|
||||
crt=$(openssl verify "$path/$cert.crt" 2>/dev/null|tail -n 1|grep -w 'OK')
|
||||
if [ -z "$crt" ]; then
|
||||
echo "Error: certificate invalid"
|
||||
log_event 'debug' "$E_CERT_INVALID $V_EVENT"
|
||||
exit $E_CERT_INVALID
|
||||
fi
|
||||
|
||||
# Checking key file
|
||||
key=$(openssl rsa -in "$path/$cert.key" -check 2>/dev/null|\
|
||||
head -n1|grep -w 'ok')
|
||||
if [ -z "$key" ]; then
|
||||
echo "Error: key invalid"
|
||||
log_event 'debug' "$E_KEY_INVALID $V_EVENT"
|
||||
exit $E_KEY_INVALID
|
||||
fi
|
||||
|
||||
# FIXME we should run server on free port
|
||||
# Checking server
|
||||
cmd="openssl s_server -quiet -cert $path/$cert.crt -key $path/$cert.key"
|
||||
$cmd &
|
||||
|
||||
# Defining pid
|
||||
pid=$!
|
||||
|
||||
# Sleep 1 second
|
||||
sleep 1
|
||||
|
||||
# Disown background process
|
||||
disown > /dev/null 2>&1
|
||||
|
||||
# Killing ssl server
|
||||
kill $pid > /dev/null 2>&1
|
||||
|
||||
# Checking result
|
||||
result=$?
|
||||
if [ "$result" -ne '0' ]; then
|
||||
echo "Error: certificate key pair invalid"
|
||||
log_event 'debug' "$E_CERTKEY_INVALID $V_EVENT"
|
||||
exit $E_CERTKEY_INVALID
|
||||
fi
|
||||
}
|
||||
|
||||
is_cert_used() {
|
||||
# Parsing config
|
||||
check_cert=$(grep "SSL_CERT='$cert'" $V_USERS/$user/web_domains.conf)
|
||||
|
||||
# Checking result
|
||||
if [ ! -z "$check_cert" ]; then
|
||||
echo "Error: certificate used"
|
||||
log_event 'debug' "$E_CERT_USED $V_EVENT"
|
||||
exit $E_CERT_USED
|
||||
fi
|
||||
}
|
||||
|
||||
cert_json_list() {
|
||||
|
||||
# Definigng variables
|
||||
i='1' # iterator
|
||||
j='1' # iterator
|
||||
end=$(($limit + $offset)) # last string
|
||||
|
||||
# Print top bracket
|
||||
echo '['
|
||||
|
||||
# Checking certificates number
|
||||
last=$(ls $V_USERS/$user/cert/|grep '.crt' | wc -l)
|
||||
|
||||
# Listing files by mask
|
||||
for cert in $(ls $V_USERS/$user/cert/|grep '.crt'); do
|
||||
# Checking offset and limit
|
||||
if [ "$i" -ge "$offset" ] && [ "$i" -lt "$end" ] && [ "$offset" -gt 0 ]
|
||||
then
|
||||
if [ "$i" -ne "$last" ] && [ "$j" -ne "$limit" ]; then
|
||||
echo -e "\t\"${cert//.crt/}\","
|
||||
else
|
||||
echo -e "\t\"${cert//.crt/}\""
|
||||
fi
|
||||
j=$(($j + 1))
|
||||
fi
|
||||
i=$(($i + 1))
|
||||
done
|
||||
|
||||
# Printing bottom bracket
|
||||
echo -e "]"
|
||||
}
|
||||
|
||||
cert_shell_list() {
|
||||
i='1' # iterator
|
||||
end=$(($limit + $offset)) # last string
|
||||
|
||||
# Print brief info
|
||||
echo "Certificate"
|
||||
echo "----------"
|
||||
|
||||
# Listing files by mask
|
||||
for cert in $(ls $V_USERS/$user/cert/|grep '.crt'); do
|
||||
# Checking offset and limit
|
||||
if [ "$i" -ge "$offset" ] && [ "$i" -lt "$end" ] && [ "$offset" -gt 0 ]
|
||||
then
|
||||
# Print result
|
||||
echo "${cert//.crt/}"
|
||||
fi
|
||||
i=$(($i + 1))
|
||||
done
|
||||
}
|
237
func/cron_func.sh
Normal file
237
func/cron_func.sh
Normal file
|
@ -0,0 +1,237 @@
|
|||
get_next_cron_string() {
|
||||
# Parsing config
|
||||
curr_str=$(grep "JOB=" $V_USERS/$user/crontab.conf|cut -f 2 -d \'|\
|
||||
sort -n|tail -n1)
|
||||
|
||||
# Print result
|
||||
echo "$((curr_str +1))"
|
||||
}
|
||||
|
||||
is_cron_job_free() {
|
||||
# Checking record id
|
||||
check_job=$(grep "JOB='$job'" $V_USERS/$user/crontab.conf)
|
||||
|
||||
if [ ! -z "$check_job" ]; then
|
||||
echo "Error: job exist"
|
||||
log_event 'debug' "$E_JOB_EXIST $V_EVENT"
|
||||
exit $E_JOB_EXIST
|
||||
fi
|
||||
}
|
||||
|
||||
sort_cron_jobs() {
|
||||
# Defining conf
|
||||
conf="$V_USERS/$user/crontab.conf"
|
||||
cat $conf |sort -n -k 2 -t \' >$conf.tmp
|
||||
mv -f $conf.tmp $conf
|
||||
}
|
||||
|
||||
sync_cron_jobs() {
|
||||
conf="/var/spool/cron/$user"
|
||||
|
||||
# Deleting old data
|
||||
rm -f $conf
|
||||
|
||||
# Checking reporting system
|
||||
rep=$(grep 'REPORTS=' $V_USERS/$user/user.conf | cut -f 2 -d \')
|
||||
if [ "$rep" = 'yes' ]; then
|
||||
email=$(grep 'CONTACT=' $V_USERS/$user/user.conf | cut -f 2 -d \')
|
||||
echo "MAILTO=$email" >$conf
|
||||
fi
|
||||
|
||||
# Reading user crontab.conf
|
||||
while read line ; do
|
||||
# Defining new delimeter
|
||||
IFS=$'\n'
|
||||
|
||||
# Parsing key=value
|
||||
for key in $(echo $line|sed -e "s/' /'\n/g"); do
|
||||
eval ${key%%=*}="${key#*=}"
|
||||
done
|
||||
|
||||
if [ "$SUSPEND" = 'no' ] ; then
|
||||
# Adding line to system cron
|
||||
echo "$MIN $HOUR $DAY $MONTH $WDAY $CMD" |\
|
||||
sed -e "s/%quote%/'/g" -e "s/%dots%/:/g" >> $conf
|
||||
fi
|
||||
done <$V_USERS/$user/crontab.conf
|
||||
}
|
||||
|
||||
|
||||
is_job_valid() {
|
||||
result=$(grep "JOB='$job'" $V_USERS/$user/crontab.conf)
|
||||
|
||||
if [ -z "$result" ]; then
|
||||
echo "Error: job not exists"
|
||||
log_event 'debug' "$E_JOB_NOTEXIST $V_EVENT"
|
||||
exit $E_JOB_NOTEXIST
|
||||
fi
|
||||
}
|
||||
|
||||
del_cron_job() {
|
||||
str=$(grep -n "JOB='$job'" $V_USERS/$user/crontab.conf|cut -f 1 -d :)
|
||||
sed -i "$str d" $V_USERS/$user/crontab.conf
|
||||
}
|
||||
|
||||
crn_json_list() {
|
||||
i='1' # iterator
|
||||
end=$(($limit + $offset)) # last string
|
||||
|
||||
# Print top bracket
|
||||
echo '{'
|
||||
|
||||
# Reading file line by line
|
||||
while read line ; do
|
||||
# Checking offset and limit
|
||||
if [ "$i" -ge "$offset" ] && [ "$i" -lt "$end" ] && [ "$offset" -gt 0 ]
|
||||
then
|
||||
# Defining new delimeter
|
||||
IFS=$'\n'
|
||||
# Parsing key=value
|
||||
for key in $(echo $line|sed -e "s/' /'\n/g"); do
|
||||
eval ${key%%=*}="${key#*=}"
|
||||
done
|
||||
|
||||
# Checking !first line to print bracket
|
||||
if [ "$i" -ne "$offset" ]; then
|
||||
echo -e "\t},"
|
||||
fi
|
||||
|
||||
j=1 # local loop iterator
|
||||
last_word=$(echo "$fields" | wc -w)
|
||||
|
||||
# Restoring old delimeter
|
||||
IFS=' '
|
||||
# Print data
|
||||
for field in $fields; do
|
||||
eval value=\"$field\"
|
||||
value=$(echo "$value"|sed -e 's/"/\\"/g' -e "s/%quote%/'/g")
|
||||
|
||||
# Checking parrent key
|
||||
if [ "$j" -eq 1 ]; then
|
||||
echo -e "\t\"$value\": {"
|
||||
else
|
||||
if [ "$j" -eq "$last_word" ]; then
|
||||
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
|
||||
else
|
||||
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
|
||||
fi
|
||||
fi
|
||||
j=$(($j + 1))
|
||||
done
|
||||
fi
|
||||
i=$(($i + 1))
|
||||
done < $conf
|
||||
|
||||
# If there was any output
|
||||
if [ -n "$value" ]; then
|
||||
echo -e "\t}"
|
||||
fi
|
||||
|
||||
# Printing bottom json bracket
|
||||
echo -e "}"
|
||||
}
|
||||
|
||||
crn_shell_list() {
|
||||
i='1' # iterator
|
||||
end=$(($limit + $offset)) # last string
|
||||
|
||||
# Print brief info
|
||||
echo "${fields//$/}"
|
||||
for a in $(echo ${fields//\~/ /}); do
|
||||
echo -e "-----~\c"
|
||||
done
|
||||
echo
|
||||
|
||||
|
||||
# Reading file line by line
|
||||
while read line ; do
|
||||
# Checking offset and limit
|
||||
if [ "$i" -ge "$offset" ] && [ "$i" -lt "$end" ] && [ "$offset" -gt 0 ]
|
||||
then
|
||||
# Defining new delimeter
|
||||
IFS=$'\n'
|
||||
# Parsing key=value
|
||||
for key in $(echo $line|sed -e "s/' /'\n/g"); do
|
||||
eval ${key%%=*}="${key#*=}"
|
||||
done
|
||||
# Print result line
|
||||
eval echo "\"$fields\""|sed -e "s/%quote%/'/g"
|
||||
fi
|
||||
i=$(($i + 1))
|
||||
done < $conf
|
||||
}
|
||||
|
||||
is_job_suspended() {
|
||||
# Parsing jobs
|
||||
str=$(grep "JOB='$job'" $V_USERS/$user/crontab.conf|grep "SUSPEND='yes'" )
|
||||
|
||||
# Checkng key
|
||||
if [ ! -z "$str" ]; then
|
||||
echo "Error: job suspended"
|
||||
log_event 'debug' "$E_JOB_SUSPENDED $V_EVENT"
|
||||
exit $E_JOB_SUSPENDED
|
||||
fi
|
||||
}
|
||||
|
||||
is_job_unsuspended() {
|
||||
# Parsing jobs
|
||||
str=$(grep "JOB='$job'" $V_USERS/$user/crontab.conf|grep "SUSPEND='no'" )
|
||||
|
||||
# Checkng key
|
||||
if [ ! -z "$str" ]; then
|
||||
echo "Error: job unsuspended"
|
||||
log_event 'debug' "$E_JOB_UNSUSPENDED $V_EVENT"
|
||||
exit $E_JOB_UNSUSPENDED
|
||||
fi
|
||||
}
|
||||
|
||||
update_cron_job_value() {
|
||||
key="$1"
|
||||
value="$2"
|
||||
|
||||
# Defining conf
|
||||
conf="$V_USERS/$user/crontab.conf"
|
||||
|
||||
# Parsing conf
|
||||
job_str=$(grep -n "JOB='$job'" $conf)
|
||||
str_number=$(echo $job_str | cut -f 1 -d ':')
|
||||
str=$(echo $job_str | cut -f 2 -d ':')
|
||||
|
||||
# Parsing key=value
|
||||
IFS=$'\n'
|
||||
for keys in $(echo $str|sed -e "s/' /'\n/g"); do
|
||||
eval ${keys%%=*}=${keys#*=}
|
||||
done
|
||||
|
||||
# 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
|
||||
}
|
||||
|
||||
cron_clear_search() {
|
||||
# Defining delimeter
|
||||
IFS=$'\n'
|
||||
|
||||
# Reading file line by line
|
||||
for line in $(grep $search_string $conf); do
|
||||
|
||||
# Defining new delimeter
|
||||
IFS=$'\n'
|
||||
# Parsing key=value
|
||||
for key in $(echo $line|sed -e "s/' /'\n/g"); do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Print result line
|
||||
eval echo "$field"
|
||||
done
|
||||
}
|
965
func/db_func.sh
Normal file
965
func/db_func.sh
Normal file
|
@ -0,0 +1,965 @@
|
|||
is_db_valid() {
|
||||
config="$V_USERS/$user/db.conf"
|
||||
check_db=$(grep "DB='$database'" $config)
|
||||
|
||||
# Checking result
|
||||
if [ -z "$check_db" ]; then
|
||||
echo "Error: db not added"
|
||||
log_event 'debug' "$E_DB_NOTEXIST $V_EVENT"
|
||||
exit $E_DB_NOTEXIST
|
||||
fi
|
||||
}
|
||||
|
||||
is_db_new() {
|
||||
# Parsing domain values
|
||||
check_db=$(grep -F "DB='$database'" $V_USERS/$user/db.conf)
|
||||
|
||||
# Checking result
|
||||
if [ ! -z "$check_db" ]; then
|
||||
echo "Error: db exist"
|
||||
log_event 'debug' "$E_DB_EXIST $V_EVENT"
|
||||
exit $E_DB_EXIST
|
||||
fi
|
||||
}
|
||||
|
||||
# Shell list for single database
|
||||
db_shell_single_list() {
|
||||
|
||||
# Reading file line by line
|
||||
line=$(grep "DB='$database'" $conf)
|
||||
|
||||
# Parsing key=value
|
||||
for key in $line; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Print result line
|
||||
for field in $fields; do
|
||||
eval key="$field"
|
||||
echo "${field//$/}: $key "
|
||||
done
|
||||
}
|
||||
|
||||
# Json single list
|
||||
db_json_single_list() {
|
||||
i=1
|
||||
|
||||
# Define words number
|
||||
last_word=$(echo "$fields" | wc -w)
|
||||
|
||||
# Reading file line by line
|
||||
line=$(grep "DB='$database'" $conf)
|
||||
|
||||
# Print top bracket
|
||||
echo '{'
|
||||
|
||||
# Parsing key=value
|
||||
for key in $line; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Starting output loop
|
||||
for field in $fields; do
|
||||
|
||||
# Parsing key=value
|
||||
eval value=$field
|
||||
|
||||
# Checking first field
|
||||
if [ "$i" -eq 1 ]; then
|
||||
echo -e "\t\"$value\": {"
|
||||
else
|
||||
if [ "$last_word" -eq "$i" ]; then
|
||||
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
|
||||
else
|
||||
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
|
||||
fi
|
||||
fi
|
||||
|
||||
# Updating iterator
|
||||
i=$(( i + 1))
|
||||
done
|
||||
|
||||
# If there was any output
|
||||
if [ -n "$value" ]; then
|
||||
echo -e "\t}"
|
||||
fi
|
||||
|
||||
# Printing bottom json bracket
|
||||
echo -e "}"
|
||||
}
|
||||
|
||||
# Shell list for single database host
|
||||
dbhost_shell_single_list() {
|
||||
|
||||
# Reading file line by line
|
||||
line=$(grep "HOST='$host'" $conf)
|
||||
|
||||
# Parsing key=value
|
||||
for key in $line; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Print result line
|
||||
for field in $fields; do
|
||||
eval key="$field"
|
||||
echo "${field//$/}: $key"
|
||||
done
|
||||
}
|
||||
|
||||
# Json list for single db host
|
||||
dbhost_json_single_list() {
|
||||
|
||||
# Definigng variables
|
||||
i=1 # iterator
|
||||
|
||||
# Define words number
|
||||
last_word=$(echo "$fields" | wc -w)
|
||||
|
||||
# Reading file line by line
|
||||
line=$(grep "HOST='$host'" $conf)
|
||||
|
||||
# Print top bracket
|
||||
echo '{'
|
||||
|
||||
# Parsing key=value
|
||||
for key in $line; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Starting output loop
|
||||
for field in $fields; do
|
||||
|
||||
# Parsing key=value
|
||||
eval value=$field
|
||||
|
||||
# Checking first field
|
||||
if [ "$i" -eq 1 ]; then
|
||||
echo -e "\t\"$value\": {"
|
||||
else
|
||||
if [ "$last_word" -eq "$i" ]; then
|
||||
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
|
||||
else
|
||||
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
|
||||
fi
|
||||
fi
|
||||
|
||||
# Updating iterator
|
||||
i=$(( i + 1))
|
||||
done
|
||||
# If there was any output
|
||||
if [ -n "$value" ]; then
|
||||
echo -e "\t}"
|
||||
fi
|
||||
|
||||
# Printing bottom json bracket
|
||||
echo -e "}"
|
||||
}
|
||||
|
||||
# Checking database host existance
|
||||
is_db_host_valid() {
|
||||
config="$V_DB/$type.conf"
|
||||
check_db=$(grep "HOST='$host'" $config)
|
||||
|
||||
# Checking result
|
||||
if [ -z "$check_db" ]; then
|
||||
echo "Error: host not added"
|
||||
log_event 'debug' "$E_DBHOST_NOTEXIST $V_EVENT"
|
||||
exit $E_DBHOST_NOTEXIST
|
||||
fi
|
||||
}
|
||||
|
||||
get_next_db_host() {
|
||||
# Defining vars
|
||||
config="$V_DB/$type.conf"
|
||||
host="empty"
|
||||
host_str=$(grep "ACTIVE='yes'" $config)
|
||||
|
||||
# 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" ] && [ $MAX_USERS -gt "$users" ];then
|
||||
host=$HOST
|
||||
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" ] && [ $MAX_USERS -gt "$users" ]; 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" ] && [ $MAX_USERS -gt "$users" ]
|
||||
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" ] && [ $MAX_USERS -gt "$users" ]
|
||||
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"
|
||||
host_str=$(grep "HOST='$host'" $conf)
|
||||
|
||||
for key in $host_str; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Increasing db_bases usage value
|
||||
U_DB_BASES=$((U_DB_BASES + 1))
|
||||
# Adding user to SYS_USERS pool
|
||||
if [ -z "$U_SYS_USERS" ]; then
|
||||
U_SYS_USERS="$user"
|
||||
else
|
||||
check_users=$(echo $U_SYS_USERS|sed -e "s/,/\n/g"|grep -w "$user")
|
||||
if [ -z "$check_users" ]; then
|
||||
U_SYS_USERS="$U_SYS_USERS,$user"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Concatenating db string
|
||||
case $type in
|
||||
mysql) new_str="HOST='$HOST' USER='$USER' PASSWORD='$PASSWORD'";
|
||||
new_str="$new_str PORT='$PORT' MAX_USERS='$MAX_USERS'";
|
||||
new_str="$new_str MAX_DB='$MAX_DB' U_SYS_USERS='$U_SYS_USERS'";
|
||||
new_str="$new_str U_DB_BASES='$U_DB_BASES' ACTIVE='$ACTIVE'";
|
||||
new_str="$new_str DATE='$DATE'";;
|
||||
pgsql) new_str="HOST='$HOST' USER='$USER' PASSWORD='$PASSWORD'";
|
||||
new_str="$new_str PORT='$PORT' TPL='$TPL'";
|
||||
new_str="$new_str MAX_USERS='$MAX_USERS' MAX_DB='$MAX_DB'";
|
||||
new_str="$new_str U_SYS_USERS='$U_SYS_USERS'";
|
||||
new_str="$new_str U_DB_BASES='$U_DB_BASES' ACTIVE='$ACTIVE'";
|
||||
new_str="$new_str DATE='$DATE'";;
|
||||
esac
|
||||
|
||||
# Changing config
|
||||
sed -i "s/$host_str/$new_str/g" $conf
|
||||
}
|
||||
|
||||
decrease_db_value() {
|
||||
# Defining vars
|
||||
conf="$V_DB/$type.conf"
|
||||
host_str=$(grep "HOST='$host'" $conf)
|
||||
|
||||
for key in $host_str; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Decreasing db_bases usage 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)
|
||||
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')
|
||||
fi
|
||||
|
||||
# Concatenating db string
|
||||
case $type in
|
||||
mysql) new_str="HOST='$HOST' USER='$USER' PASSWORD='$PASSWORD'";
|
||||
new_str="$new_str PORT='$PORT' MAX_USERS='$MAX_USERS'";
|
||||
new_str="$new_str MAX_DB='$MAX_DB' U_SYS_USERS='$U_SYS_USERS'";
|
||||
new_str="$new_str U_DB_BASES='$U_DB_BASES' ACTIVE='$ACTIVE'";
|
||||
new_str="$new_str DATE='$DATE'";;
|
||||
pgsql) new_str="HOST='$HOST' USER='$USER' PASSWORD='$PASSWORD'";
|
||||
new_str="$new_str PORT='$PORT' TPL='$TPL'";
|
||||
new_str="$new_str MAX_USERS='$MAX_USERS' MAX_DB='$MAX_DB'";
|
||||
new_str="$new_str U_SYS_USERS='$U_SYS_USERS'";
|
||||
new_str="$new_str U_DB_BASES='$U_DB_BASES' ACTIVE='$ACTIVE'";
|
||||
new_str="$new_str DATE='$DATE'";;
|
||||
esac
|
||||
|
||||
# Changing config
|
||||
sed -i "s/$host_str/$new_str/g" $conf
|
||||
}
|
||||
|
||||
create_db_mysql() {
|
||||
# Defining vars
|
||||
host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
|
||||
for key in $host_str; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
|
||||
|
||||
# Checking empty vars
|
||||
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
|
||||
echo "Error: config is broken"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# Checking connection
|
||||
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
|
||||
# Adding database & checking result
|
||||
$sql "CREATE DATABASE $database" >/dev/null 2>&1;code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
|
||||
# Adding user with password (% will give access to db from any ip)
|
||||
$sql "GRANT ALL ON $database.* TO '$db_user'@'%' \
|
||||
IDENTIFIED BY '$db_password'"
|
||||
|
||||
# Adding grant for localhost (% doesn't do that )
|
||||
if [ "$host" = 'localhost' ]; then
|
||||
$sql "GRANT ALL ON $database.* TO '$db_user'@'localhost' \
|
||||
IDENTIFIED BY '$db_password'"
|
||||
fi
|
||||
|
||||
# Flushing priveleges
|
||||
$sql "FLUSH PRIVILEGES"
|
||||
}
|
||||
|
||||
create_db_pgsql() {
|
||||
# Defining vars
|
||||
host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
|
||||
for key in $host_str; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
export PGPASSWORD="$PASSWORD"
|
||||
sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
|
||||
|
||||
# Checking empty vars
|
||||
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
|
||||
echo "Error: config is broken"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# Checking connection
|
||||
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
|
||||
# Adding database & checking result
|
||||
$sql "CREATE DATABASE $database" >/dev/null 2>&1;code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
|
||||
$sql "CREATE ROLE $db_user WITH LOGIN PASSWORD '$db_password'"
|
||||
$sql "GRANT ALL PRIVILEGES ON DATABASE $database TO $db_user"
|
||||
export PGPASSWORD='pgsqk'
|
||||
}
|
||||
|
||||
is_db_host_new() {
|
||||
if [ -e "$V_DB/$type.conf" ]; then
|
||||
check_host=$(grep "HOST='$host'" $V_DB/$type.conf)
|
||||
if [ ! -z "$check_host" ]; then
|
||||
echo "Error: db host exist"
|
||||
log_event 'debug' "$E_DBHOST_EXIST $V_EVENT"
|
||||
exit $E_DBHOST_EXIST
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
is_mysql_host_alive() {
|
||||
# Checking connection
|
||||
sql="mysql -h $host -u $db_user -p$db_password -P$port -e"
|
||||
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
}
|
||||
|
||||
is_pgsql_host_alive() {
|
||||
# Checking connection
|
||||
export PGPASSWORD="$db_password"
|
||||
sql="psql -h $host -U $db_user -d $template -p $port -c"
|
||||
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
}
|
||||
|
||||
is_db_suspended() {
|
||||
config="$V_USERS/$user/db.conf"
|
||||
check_db=$(grep "DB='$database'" $config|grep "SUSPEND='yes'")
|
||||
|
||||
# Checking result
|
||||
if [ ! -z "$check_db" ]; then
|
||||
echo "Error: db suspended"
|
||||
log_event 'debug' "$E_DB_SUSPENDED $V_EVENT"
|
||||
exit $E_DB_SUSPENDED
|
||||
fi
|
||||
}
|
||||
|
||||
is_db_unsuspended() {
|
||||
config="$V_USERS/$user/db.conf"
|
||||
check_db=$(grep "DB='$database'" $config|grep "SUSPEND='yes'")
|
||||
|
||||
# Checking result
|
||||
if [ -z "$check_db" ]; then
|
||||
echo "Error: db unsuspended"
|
||||
log_event 'debug' "$E_DB_UNSUSPENDED $V_EVENT"
|
||||
exit $E_DB_UNSUSPENDED
|
||||
fi
|
||||
}
|
||||
|
||||
is_db_user_valid() {
|
||||
config="$V_USERS/$user/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_DBUSER_NOTEXIST $V_EVENT"
|
||||
exit $E_DBUSER_NOTEXIST
|
||||
fi
|
||||
}
|
||||
|
||||
change_db_mysql_password() {
|
||||
# Defining vars
|
||||
host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
|
||||
for key in $host_str; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
|
||||
|
||||
# Checking empty vars
|
||||
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
|
||||
echo "Error: config is broken"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# Checking connection
|
||||
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
|
||||
# Changing user password
|
||||
$sql "GRANT ALL ON $database.* TO '$db_user'@'%' \
|
||||
IDENTIFIED BY '$db_password'"
|
||||
#$sql "SET PASSWORD FOR '$db_user'@'%' = PASSWORD('$db_password');"
|
||||
$sql "FLUSH PRIVILEGES"
|
||||
}
|
||||
|
||||
change_db_pgsql_password() {
|
||||
# Defining vars
|
||||
host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
|
||||
for key in $host_str; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
export PGPASSWORD="$PASSWORD"
|
||||
sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
|
||||
|
||||
# Checking empty vars
|
||||
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
|
||||
echo "Error: config is broken"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# Checking connection
|
||||
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
|
||||
$sql "ALTER ROLE $db_user WITH LOGIN PASSWORD '$db_password'" >/dev/null
|
||||
export PGPASSWORD='pgsqk'
|
||||
}
|
||||
|
||||
get_db_value() {
|
||||
# Defining vars
|
||||
key="$1"
|
||||
db_str=$(grep "DB='$database'" $V_USERS/$user/db.conf)
|
||||
|
||||
# Parsing key=value
|
||||
for keys in $db_str; do
|
||||
eval ${keys%%=*}=${keys#*=}
|
||||
done
|
||||
|
||||
# Self reference
|
||||
eval value="$key"
|
||||
|
||||
# Print value
|
||||
echo "$value"
|
||||
}
|
||||
|
||||
del_db_mysql() {
|
||||
# Defining vars
|
||||
host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
|
||||
for key in $host_str; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
|
||||
|
||||
# Checking empty vars
|
||||
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
|
||||
echo "Error: config is broken"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# Checking connection
|
||||
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
|
||||
# Deleting database & checking result
|
||||
$sql "DROP DATABASE $database" >/dev/null 2>&1;code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
|
||||
# Deleting user
|
||||
check_users=$(grep "USER='$db_user'" $V_USERS/$user/db.conf |wc -l)
|
||||
if [ 1 -ge "$check_users" ]; then
|
||||
$sql "DROP USER '$db_user'@'%'"
|
||||
if [ "$host" = 'localhost' ]; then
|
||||
$sql "DROP USER '$db_user'@'localhost'"
|
||||
fi
|
||||
else
|
||||
$sql "REVOKE ALL ON $database.* from '$db_user'@'%'"
|
||||
if [ "$host" = 'localhost' ]; then
|
||||
$sql "REVOKE ALL ON $database.* from '$db_user'@'localhost'"
|
||||
fi
|
||||
fi
|
||||
$sql "FLUSH PRIVILEGES"
|
||||
}
|
||||
|
||||
del_db_pgsql() {
|
||||
# Defining vars
|
||||
host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
|
||||
for key in $host_str; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
export PGPASSWORD="$PASSWORD"
|
||||
sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
|
||||
|
||||
# Checking empty vars
|
||||
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
|
||||
echo "Error: config is broken"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# Checking connection
|
||||
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
|
||||
# Deleting database & checking result
|
||||
$sql "DROP DATABASE $database" >/dev/null 2>&1;code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
|
||||
# Deleting user
|
||||
check_users=$(grep "USER='$db_user'" $V_USERS/$user/db.conf |wc -l)
|
||||
if [ 1 -ge "$check_users" ]; then
|
||||
$sql "DROP ROLE $db_user" >/dev/null 2>&1
|
||||
else
|
||||
$sql "REVOKE ALL PRIVILEGES ON $database FROM $db_user">/dev/null
|
||||
fi
|
||||
export PGPASSWORD='pgsqk'
|
||||
}
|
||||
|
||||
|
||||
del_db_vesta() {
|
||||
conf="$V_USERS/$user/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_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
sed -i "$string d" $conf
|
||||
}
|
||||
|
||||
is_db_host_free() {
|
||||
# Defining vars
|
||||
host_str=$(grep "HOST='$host'" $V_DB/$type.conf)
|
||||
for key in $host_str; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Checking U_DB_BASES
|
||||
if [ 0 -ne "$U_DB_BASES" ]; then
|
||||
echo "Error: host is used"
|
||||
log_event 'debug' "$E_DBHOST_BUSY $V_EVENT"
|
||||
exit $E_DBHOST_BUSY
|
||||
fi
|
||||
}
|
||||
|
||||
del_dbhost_vesta() {
|
||||
conf="$V_DB/$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_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
sed -i "$string d" $conf
|
||||
}
|
||||
|
||||
update_db_base_value() {
|
||||
key="$1"
|
||||
value="$2"
|
||||
|
||||
# Defining conf
|
||||
conf="$V_USERS/$user/db.conf"
|
||||
|
||||
# Parsing conf
|
||||
db_str=$(grep -n "DB='$database'" $conf)
|
||||
str_number=$(echo $db_str | cut -f 1 -d ':')
|
||||
str=$(echo $db_str | cut -f 2 -d ':')
|
||||
|
||||
# Reading key=values
|
||||
for keys in $str; do
|
||||
eval ${keys%%=*}=${keys#*=}
|
||||
done
|
||||
|
||||
# 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
|
||||
}
|
||||
|
||||
suspend_db_mysql() {
|
||||
# Defining vars
|
||||
host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
|
||||
for key in $host_str; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
|
||||
|
||||
# Checking empty vars
|
||||
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
|
||||
echo "Error: config is broken"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# Checking connection
|
||||
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
|
||||
# Suspending user
|
||||
$sql "REVOKE ALL ON $database.* FROM '$db_user'@'%'"
|
||||
$sql "FLUSH PRIVILEGES"
|
||||
}
|
||||
|
||||
suspend_db_pgsql() {
|
||||
# Defining vars
|
||||
host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
|
||||
for key in $host_str; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
export PGPASSWORD="$PASSWORD"
|
||||
sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
|
||||
|
||||
# Checking empty vars
|
||||
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
|
||||
echo "Error: config is broken"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# Checking connection
|
||||
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
|
||||
# Suspending user
|
||||
$sql "REVOKE ALL PRIVILEGES ON $database FROM $db_user">/dev/null
|
||||
export PGPASSWORD='pgsqk'
|
||||
}
|
||||
|
||||
unsuspend_db_mysql() {
|
||||
# Defining vars
|
||||
host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
|
||||
for key in $host_str; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
|
||||
|
||||
# Checking empty vars
|
||||
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
|
||||
echo "Error: config is broken"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# Checking connection
|
||||
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
|
||||
# Unsuspending user
|
||||
$sql "GRANT ALL ON $database.* to '$db_user'@'%'"
|
||||
$sql "FLUSH PRIVILEGES"
|
||||
}
|
||||
|
||||
unsuspend_db_pgsql() {
|
||||
# Defining vars
|
||||
host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
|
||||
for key in $host_str; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
export PGPASSWORD="$PASSWORD"
|
||||
sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
|
||||
|
||||
# Checking empty vars
|
||||
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
|
||||
echo "Error: config is broken"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# Checking connection
|
||||
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
|
||||
# Unsuspending user
|
||||
$sql "GRANT ALL PRIVILEGES ON DATABASE $database TO $db_user" >/dev/null
|
||||
export PGPASSWORD='pgsqk'
|
||||
}
|
||||
|
||||
db_clear_search() {
|
||||
# Defining delimeter
|
||||
IFS=$'\n'
|
||||
|
||||
# Reading file line by line
|
||||
for line in $(grep $search_string $conf); do
|
||||
# Parsing key=val
|
||||
for key in $line; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
# Print result line
|
||||
eval echo "$field"
|
||||
done
|
||||
}
|
||||
|
||||
get_disk_db_mysql() {
|
||||
# Defining vars
|
||||
host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
|
||||
for key in $host_str; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
|
||||
|
||||
# Checking empty vars
|
||||
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
|
||||
echo "Error: config is broken"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# Checking connection
|
||||
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
|
||||
# Deleting database & checking result
|
||||
query="SELECT sum( data_length + index_length ) / 1024 / 1024 \"Size\"
|
||||
FROM information_schema.TABLES WHERE table_schema='$database'"
|
||||
raw_size=$($sql "$query" |tail -n 1)
|
||||
|
||||
# Checking null output (this means error btw)
|
||||
if [ "$raw_size" == 'NULL' ]; then
|
||||
raw_size='0'
|
||||
fi
|
||||
|
||||
# Rounding zero size
|
||||
if [ "${raw_size:0:1}" -eq '0' ]; then
|
||||
raw_size='1'
|
||||
fi
|
||||
|
||||
# Printing round size in mb
|
||||
printf "%0.f\n" $raw_size
|
||||
|
||||
}
|
||||
|
||||
get_disk_db_pgsql() {
|
||||
# Defining vars
|
||||
host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
|
||||
for key in $host_str; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
export PGPASSWORD="$PASSWORD"
|
||||
sql="psql -h $HOST -U $USER -d $TPL -p $PORT -c"
|
||||
|
||||
# Checking empty vars
|
||||
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
|
||||
echo "Error: config is broken"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# Checking connection
|
||||
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
|
||||
if [ '0' -ne "$code" ]; then
|
||||
echo "Error: Connect failed"
|
||||
log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
|
||||
exit $E_DBHOST_UNAVAILABLE
|
||||
fi
|
||||
|
||||
# Raw query
|
||||
raq_query=$($sql "SELECT pg_database_size('$database');")
|
||||
raw_size=$(echo raq_query | grep -v "-" | grep -v 'row' | sed -e "/^$/d"|\
|
||||
awk '{print $1}')
|
||||
|
||||
# Checking null output (this means error btw)
|
||||
if [ -z "$raw_size" ]; then
|
||||
raw_size='0'
|
||||
fi
|
||||
|
||||
# Converting to MB
|
||||
size=$(expr $raw_size \ 1048576)
|
||||
|
||||
# Rounding zero size
|
||||
if [ "$size" -eq '0' ]; then
|
||||
echo '1'
|
||||
else
|
||||
echo "$size"
|
||||
fi
|
||||
}
|
918
func/domain_func.sh
Normal file
918
func/domain_func.sh
Normal file
|
@ -0,0 +1,918 @@
|
|||
# Checking domain existance
|
||||
is_domain_new() {
|
||||
output_mode="$1"
|
||||
search_dom=${2-$domain}
|
||||
|
||||
# Parsing domain values
|
||||
check_domain=$(grep -F "DOMAIN='$search_dom'" $V_USERS/*/*.conf| \
|
||||
grep -v crontab.conf)
|
||||
|
||||
# Parsing alias values
|
||||
check_alias=$(grep -F 'ALIAS=' $V_USERS/*/*.conf | \
|
||||
grep -v crontab.conf | \
|
||||
awk -F "ALIAS=" '{print $2}' | \
|
||||
cut -f 2 -d \' | \
|
||||
sed -e "s/,/\n/g" | \
|
||||
grep "^$search_dom$" )
|
||||
|
||||
# Checking result
|
||||
if [ ! -z "$check_domain" ] || [ ! -z "$check_alias" ]; then
|
||||
if [ "$output_mode" != 'quiet' ]; then
|
||||
echo "Error: domain exist"
|
||||
log_event 'debug' "$E_DOM_EXIST $V_EVENT"
|
||||
exit $E_DOM_EXIST
|
||||
fi
|
||||
return $E_DOM_EXIST
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
is_domain_owner() {
|
||||
search_dom=${1-$domain}
|
||||
|
||||
# Parsing domain values
|
||||
check_domain=$(grep "DOMAIN='$search_dom'" $V_USERS/$user/*.conf)
|
||||
|
||||
# Parsing alias values
|
||||
check_alias=$(grep 'ALIAS=' $V_USERS/$user/*.conf | \
|
||||
awk -F "ALIAS=" '{print $2}' | \
|
||||
cut -f 2 -d \' | \
|
||||
sed -e "s/,/\n/g" | \
|
||||
grep "^$search_dom$" )
|
||||
|
||||
# Checking result
|
||||
if [ -z "$check_domain" ] && [ -z "$check_alias" ]; then
|
||||
echo "Error: domain not owned"
|
||||
log_event 'debug' "$E_DOM_NOTOWNED $V_EVENT"
|
||||
exit $E_DOM_NOTOWNED
|
||||
fi
|
||||
}
|
||||
|
||||
is_dns_domain_free() {
|
||||
# Parsing domain values
|
||||
check_domain=$(grep -F "DOMAIN='$domain'" $V_USERS/$user/dns.conf)
|
||||
|
||||
# Checking result
|
||||
if [ ! -z "$check_domain" ]; then
|
||||
echo "Error: domain exist"
|
||||
log_event 'debug' "$E_DOM_EXIST $V_EVENT"
|
||||
exit $E_DOM_EXIST
|
||||
fi
|
||||
}
|
||||
|
||||
is_web_domain_free() {
|
||||
search_dom=${1-$domain}
|
||||
# Parsing domain values
|
||||
check_domain=$(grep -F "IN='$search_dom'" $V_USERS/$user/web_domains.conf)
|
||||
|
||||
# Parsing alias values
|
||||
check_alias=$(grep -F 'ALIAS=' $V_USERS/$user/web_domains.conf | \
|
||||
awk -F "ALIAS=" '{print $2}' | \
|
||||
cut -f 2 -d \' | \
|
||||
sed -e "s/,/\n/g" | \
|
||||
grep "^$check_domain$" )
|
||||
|
||||
# Checking result
|
||||
if [ ! -z "$check_domain" ] || [ ! -z "$check_alias" ]; then
|
||||
echo "Error: domain exist"
|
||||
log_event 'debug' "$E_DOM_EXIST $V_EVENT"
|
||||
exit $E_DOM_EXIST
|
||||
fi
|
||||
}
|
||||
|
||||
is_dns_domain_valid() {
|
||||
# Parsing domain values
|
||||
check_domain=$(grep -F "DOMAIN='$domain'" $V_USERS/$user/dns.conf)
|
||||
|
||||
# Checking result
|
||||
if [ -z "$check_domain" ]; then
|
||||
echo "Error: domain not exist"
|
||||
log_event 'debug' "$E_DOM_NOTEXIST $V_EVENT"
|
||||
exit $E_DOM_NOTEXIST
|
||||
fi
|
||||
}
|
||||
|
||||
is_web_domain_valid() {
|
||||
# Parsing domain values
|
||||
check_domain=$(grep -F "DOMAIN='$domain'" $V_USERS/$user/web_domains.conf)
|
||||
|
||||
# Checking result
|
||||
if [ -z "$check_domain" ]; then
|
||||
echo "Error: domain not exist"
|
||||
log_event 'debug' "$E_DOM_NOTEXIST $V_EVENT"
|
||||
exit $E_DOM_NOTEXIST
|
||||
fi
|
||||
}
|
||||
|
||||
is_domain_suspended() {
|
||||
config_type="$1"
|
||||
# Parsing domain values
|
||||
check_domain=$(grep "DOMAIN='$domain'" $V_USERS/$user/$config_type.conf|\
|
||||
grep "SUSPEND='yes'")
|
||||
|
||||
# Checking result
|
||||
if [ ! -z "$check_domain" ]; then
|
||||
echo "Error: domain suspended"
|
||||
log_event 'debug' "$E_DOM_SUSPENDED $V_EVENT"
|
||||
exit $E_DOM_SUSPENDED
|
||||
fi
|
||||
}
|
||||
|
||||
is_domain_unsuspended() {
|
||||
config_type="$1"
|
||||
# Parsing domain values
|
||||
check_domain=$(grep "DOMAIN='$domain'" $V_USERS/$user/$config_type.conf|\
|
||||
grep "SUSPEND='no'")
|
||||
|
||||
# Checking result
|
||||
if [ ! -z "$check_domain" ]; then
|
||||
echo "Error: domain unsuspended"
|
||||
log_event 'debug' "$E_DOM_UNSUSPENDED $V_EVENT"
|
||||
exit $E_DOM_UNSUSPENDED
|
||||
fi
|
||||
}
|
||||
|
||||
update_domain_zone() {
|
||||
# Definigng variables
|
||||
line=$(grep "DOMAIN='$domain'" $V_USERS/$user/dns.conf)
|
||||
fields='$RECORD\t$TTL\tIN\t$TYPE\t$VALUE'
|
||||
conf="/etc/namedb/$domain.db"
|
||||
|
||||
# Checking serial
|
||||
if [ -e $conf ]; then
|
||||
zn_serial=$(head $conf|grep 'SOA' -A1|tail -n 1|sed -e "s/ //g")
|
||||
s_date=$(echo ${zn_serial:0:8})
|
||||
c_date=$(date +'%Y%m%d')
|
||||
if [ "$s_date" == "$c_date" ]; then
|
||||
cur_value=$(echo ${zn_serial:8} )
|
||||
new_value=$(expr $cur_value + 1 )
|
||||
len_value=$(expr length $new_value)
|
||||
if [ 1 -eq "$len_value" ]; then
|
||||
new_value='0'$new_value
|
||||
fi
|
||||
serial="$c_date""$new_value"
|
||||
else
|
||||
serial="$(date +'%Y%m%d01')"
|
||||
fi
|
||||
else
|
||||
serial="$(date +'%Y%m%d01')"
|
||||
fi
|
||||
|
||||
# Parsing dns domains conf
|
||||
for key in $line; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Converting SOA to ascii
|
||||
SOA=$(idn --quiet -a -t "$SOA")
|
||||
# Adding zone header
|
||||
echo "\$TTL $TTL
|
||||
@ IN SOA $SOA. root.$domain_idn. (
|
||||
$serial
|
||||
7200
|
||||
3600
|
||||
1209600
|
||||
180 )
|
||||
" > $conf
|
||||
|
||||
# Adding zone records
|
||||
while read line ; do
|
||||
# Defining new delimeter
|
||||
IFS=$'\n'
|
||||
# Parsing key=value
|
||||
for key in $(echo $line|sed -e "s/' /'\n/g"); do
|
||||
eval ${key%%=*}="${key#*=}"
|
||||
done
|
||||
|
||||
# Converting utf records to ascii
|
||||
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/zones/$domain
|
||||
|
||||
}
|
||||
|
||||
get_next_dns_record() {
|
||||
# Parsing config
|
||||
curr_str=$(grep "ID=" $V_USERS/$user/zones/$domain|cut -f 2 -d \'|\
|
||||
sort -n|tail -n1)
|
||||
|
||||
# Print result
|
||||
echo "$((curr_str +1))"
|
||||
}
|
||||
|
||||
is_dns_record_free() {
|
||||
# Checking record id
|
||||
check_id=$(grep "ID='$id'" $V_USERS/$user/zones/$domain)
|
||||
|
||||
if [ ! -z "$check_id" ]; then
|
||||
echo "Error: ID exist"
|
||||
log_event 'debug' "$E_ID_EXIST $V_EVENT"
|
||||
exit $E_ID_EXIST
|
||||
fi
|
||||
}
|
||||
|
||||
sort_dns_records() {
|
||||
# Defining conf
|
||||
conf="$V_USERS/$user/zones/$domain"
|
||||
cat $conf |sort -n -k 2 -t \' >$conf.tmp
|
||||
mv -f $conf.tmp $conf
|
||||
}
|
||||
|
||||
get_web_port() {
|
||||
proxy_disabled='80'
|
||||
proxy_enabled='8080'
|
||||
|
||||
# Parsing conf
|
||||
proxy=$(grep 'PROXY_SYSTEM=' $V_CONF/vesta.conf|cut -f 2 -d \')
|
||||
|
||||
# Checking result
|
||||
if [ -z "$proxy" ] || [ "$proxy" = 'off' ]; then
|
||||
echo "$proxy_disabled"
|
||||
else
|
||||
echo "$proxy_enabled"
|
||||
fi
|
||||
}
|
||||
|
||||
get_web_port_ssl() {
|
||||
proxy_disabled='443'
|
||||
proxy_enabled='8443'
|
||||
|
||||
# Parsing conf
|
||||
proxy=$(grep 'PROXY_SYSTEM=' $V_CONF/vesta.conf|cut -f 2 -d \')
|
||||
|
||||
# Checking result
|
||||
if [ -z "$proxy" ] || [ "$proxy" = 'off' ]; then
|
||||
echo "$proxy_disabled"
|
||||
else
|
||||
echo "$proxy_enabled"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
httpd_add_config() {
|
||||
# Adding template to config
|
||||
cat $tpl_file | \
|
||||
sed -e "s/%ip%/$ip/g" \
|
||||
-e "s/%port%/$port/g" \
|
||||
-e "s/%domain_idn%/$domain_idn/g" \
|
||||
-e "s/%domain%/$domain/g" \
|
||||
-e "s/%user%/$user/g" \
|
||||
-e "s/%group%/$group/g" \
|
||||
-e "s/%home%/${V_HOME////\/}/g" \
|
||||
-e "s/%docroot%/${docroot////\/}/g" \
|
||||
-e "s/%email%/$email/g" \
|
||||
-e "s/%alias_idn%/${aliases_idn//,/ }/g" \
|
||||
-e "s/%alias%/${aliases//,/ }/g" \
|
||||
-e "s/%ssl_cert%/${ssl_cert////\/}/g" \
|
||||
-e "s/%ssl_key%/${ssl_key////\/}/g" \
|
||||
>> $conf
|
||||
}
|
||||
|
||||
httpd_change_config() {
|
||||
# Get ServerName line
|
||||
serv_line=$(grep -n 'ServerName %domain_idn%' "$tpl_file" | cut -f 1 -d :)
|
||||
|
||||
# Get tpl_file last line
|
||||
last_line=$(wc -l $tpl_file | cut -f 1 -d ' ')
|
||||
|
||||
# Get before line
|
||||
bfr_line=$((serv_line - 1))
|
||||
|
||||
# Get after line
|
||||
aftr_line=$((last_line - serv_line - 1))
|
||||
|
||||
# Parsing httpd.conf
|
||||
vhost=$(grep -A $aftr_line -B $bfr_line -n "ServerName $domain_idn" $conf)
|
||||
|
||||
# Searching prhase
|
||||
str=$(echo "$vhost" | grep -F "$search_phrase" | head -n 1)
|
||||
|
||||
# Checking parsing result
|
||||
if [ -z "$str" ] || [ -z "$serv_line" ] || [ -z "$aftr_line" ]; then
|
||||
echo "Error: httpd parsing error"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# Parsing string position and content
|
||||
str_numb=$(echo "$str" | sed -e "s/-/=/" | cut -f 1 -d '=')
|
||||
str_cont=$(echo "$str" | sed -e "s/-/=/" | cut -f 2 -d '=')
|
||||
|
||||
# Escaping chars
|
||||
str_repl=$(echo "$str_repl" | sed \
|
||||
-e 's/\\/\\\\/g' \
|
||||
-e 's/&/\\&/g' \
|
||||
-e 's/\//\\\//g')
|
||||
|
||||
# Changing config
|
||||
sed -i "$str_numb s/.*/$str_repl/" $conf
|
||||
}
|
||||
|
||||
get_web_domain_value() {
|
||||
key="$1"
|
||||
|
||||
# Parsing domains
|
||||
string=$( grep "DOMAIN='$domain'" $V_USERS/$user/web_domains.conf )
|
||||
|
||||
# Parsing key=value
|
||||
for keys in $string; do
|
||||
eval ${keys%%=*}=${keys#*=}
|
||||
done
|
||||
|
||||
# Self reference
|
||||
eval value="$key"
|
||||
|
||||
# Print value
|
||||
echo "$value"
|
||||
}
|
||||
|
||||
get_dns_domain_value() {
|
||||
key="$1"
|
||||
|
||||
# Parsing domains
|
||||
string=$( grep "DOMAIN='$domain'" $V_USERS/$user/dns.conf )
|
||||
|
||||
# Parsing key=value
|
||||
for keys in $string; do
|
||||
eval ${keys%%=*}=${keys#*=}
|
||||
done
|
||||
|
||||
# Self reference
|
||||
eval value="$key"
|
||||
|
||||
# Print value
|
||||
echo "$value"
|
||||
}
|
||||
|
||||
update_web_domain_value() {
|
||||
key="$1"
|
||||
value="$2"
|
||||
|
||||
# Defining conf
|
||||
conf="$V_USERS/$user/web_domains.conf"
|
||||
|
||||
# Parsing conf
|
||||
domain_str=$(grep -n "DOMAIN='$domain'" $conf)
|
||||
str_number=$(echo $domain_str | cut -f 1 -d ':')
|
||||
str=$(echo $domain_str | cut -f 2 -d ':')
|
||||
|
||||
# Reading key=values
|
||||
for keys in $str; do
|
||||
eval ${keys%%=*}=${keys#*=}
|
||||
done
|
||||
|
||||
# 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
|
||||
}
|
||||
|
||||
update_dns_domain_value() {
|
||||
key="$1"
|
||||
value="$2"
|
||||
|
||||
# Defining conf
|
||||
conf="$V_USERS/$user/dns.conf"
|
||||
|
||||
# Parsing conf
|
||||
domain_str=$(grep -n "DOMAIN='$domain'" $conf)
|
||||
str_number=$(echo $domain_str | cut -f 1 -d ':')
|
||||
str=$(echo $domain_str | cut -f 2 -d ':')
|
||||
|
||||
# Reading key=values
|
||||
for keys in $str; do
|
||||
eval ${keys%%=*}=${keys#*=}
|
||||
done
|
||||
|
||||
# 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
|
||||
}
|
||||
|
||||
is_web_domain_key_empty() {
|
||||
key="$1"
|
||||
|
||||
# Parsing domains
|
||||
string=$( grep "DOMAIN='$domain'" $V_USERS/$user/web_domains.conf )
|
||||
|
||||
# Parsing key=value
|
||||
for keys in $string; do
|
||||
eval ${keys%%=*}=${keys#*=}
|
||||
done
|
||||
|
||||
# Self reference
|
||||
eval value="$key"
|
||||
|
||||
# Checkng key
|
||||
if [ ! -z "$value" ] && [ "$value" != 'no' ]; then
|
||||
echo "Error: value is not empty = $value"
|
||||
log_event 'debug' "$E_VALUE_EXIST $V_EVENT"
|
||||
exit $E_VALUE_EXIST
|
||||
fi
|
||||
}
|
||||
|
||||
is_dns_record_valid() {
|
||||
# Checking record id
|
||||
check_id=$(grep "^ID='$id'" $V_USERS/$user/zones/$domain)
|
||||
|
||||
if [ -z "$check_id" ]; then
|
||||
echo "Error: ID not exist"
|
||||
log_event 'debug' "$E_ID_NOTEXIST $V_EVENT"
|
||||
exit $E_ID_NOTEXIST
|
||||
fi
|
||||
}
|
||||
|
||||
is_web_domain_value_exist() {
|
||||
key="$1"
|
||||
|
||||
# Parsing domains
|
||||
string=$( grep "DOMAIN='$domain'" $V_USERS/$user/web_domains.conf )
|
||||
|
||||
# Parsing key=value
|
||||
for keys in $string; do
|
||||
eval ${keys%%=*}=${keys#*=}
|
||||
done
|
||||
|
||||
# Self reference
|
||||
eval value="$key"
|
||||
|
||||
# Checking result
|
||||
if [ -z "$value" ] || [ "$value" = 'no' ]; then
|
||||
echo "Error: ${key//$/} is empty"
|
||||
log_event 'debug' "$E_VALUE_EMPTY $V_EVENT"
|
||||
exit $E_VALUE_EMPTY
|
||||
fi
|
||||
}
|
||||
|
||||
is_dns_domain_value_exist() {
|
||||
key="$1"
|
||||
|
||||
# Parsing domains
|
||||
string=$( grep "DOMAIN='$domain'" $V_USERS/$user/dns.conf )
|
||||
|
||||
# Parsing key=value
|
||||
for keys in $string; do
|
||||
eval ${keys%%=*}=${keys#*=}
|
||||
done
|
||||
|
||||
# Self reference
|
||||
eval value="$key"
|
||||
|
||||
# Checking result
|
||||
if [ -z "$value" ] || [ "$value" = 'no' ]; then
|
||||
echo "Error: ${key//$/} is empty"
|
||||
log_event 'debug' "$E_VALUE_EMPTY $V_EVENT"
|
||||
exit $E_VALUE_EXIST
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
httpd_del_config() {
|
||||
# Get ServerName line
|
||||
serv_line=$(grep -n 'ServerName %domain_idn%' "$tpl_file" |cut -f 1 -d :)
|
||||
|
||||
# Get tpl_file last line
|
||||
last_line=$(wc -l $tpl_file|cut -f 1 -d ' ')
|
||||
|
||||
# Get before line
|
||||
bfr_line=$((serv_line - 1))
|
||||
|
||||
# Parsing httpd.conf
|
||||
str=$(grep -B $bfr_line -n "ServerName $domain_idn" $conf |\
|
||||
grep '<VirtualHost')
|
||||
|
||||
# Checking result
|
||||
if [ -z "$str" ] || [ -z "$serv_line" ] || [ -z "$bfr_line" ]; then
|
||||
echo "Error: httpd parsing error"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# String number
|
||||
top_line=$(echo $str | sed -e "s/-/+/" | cut -f 1 -d '+')
|
||||
bottom_line=$((top_line + last_line - 1))
|
||||
sed -i "$top_line,$bottom_line d" $conf
|
||||
}
|
||||
|
||||
del_dns_domain() {
|
||||
conf="$V_USERS/$user/dns.conf"
|
||||
|
||||
# Parsing domains
|
||||
string=$( grep -n "DOMAIN='$domain'" $conf | cut -f 1 -d : )
|
||||
if [ -z "$string" ]; then
|
||||
echo "Error: parse error"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
sed -i "$string d" $conf
|
||||
rm -f $V_USERS/$user/zones/$domain
|
||||
}
|
||||
|
||||
del_web_domain() {
|
||||
conf="$V_USERS/$user/web_domains.conf"
|
||||
|
||||
# Parsing domains
|
||||
string=$( grep -n "DOMAIN='$domain'" $conf | cut -f 1 -d : )
|
||||
if [ -z "$string" ]; then
|
||||
echo "Error: parse error"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
sed -i "$string d" $conf
|
||||
}
|
||||
|
||||
|
||||
dns_shell_list() {
|
||||
i='1' # iterator
|
||||
end=$(($limit + $offset)) # last string
|
||||
|
||||
# Print brief info
|
||||
echo "${fields//$/}"
|
||||
for a in $fields; do
|
||||
echo -e "------ \c"
|
||||
done
|
||||
echo
|
||||
|
||||
# Reading file line by line
|
||||
while read line ; do
|
||||
# Checking offset and limit
|
||||
if [ "$i" -ge "$offset" ] && [ "$i" -lt "$end" ] && [ "$offset" -gt 0 ]
|
||||
then
|
||||
# Defining new delimeter
|
||||
IFS=$'\n'
|
||||
# Parsing key=value
|
||||
for key in $(echo $line|sed -e "s/' /'\n/g"); do
|
||||
eval ${key%%=*}="${key#*=}"
|
||||
done
|
||||
# Print result line
|
||||
eval echo "\"$fields\""|sed -e "s/%quote%/'/g"
|
||||
fi
|
||||
i=$(($i + 1))
|
||||
done < $conf
|
||||
}
|
||||
|
||||
dns_json_list() {
|
||||
i='1' # iterator
|
||||
end=$(($limit + $offset)) # last string
|
||||
|
||||
# Print top bracket
|
||||
echo '{'
|
||||
|
||||
# Reading file line by line
|
||||
while read line ; do
|
||||
# Checking offset and limit
|
||||
if [ "$i" -ge "$offset" ] && [ "$i" -lt "$end" ] && [ "$offset" -gt 0 ]
|
||||
then
|
||||
# Defining new delimeter
|
||||
IFS=$'\n'
|
||||
# Parsing key=value
|
||||
for key in $(echo $line|sed -e "s/' /'\n/g"); do
|
||||
eval ${key%%=*}="${key#*=}"
|
||||
done
|
||||
|
||||
# Checking !first line to print bracket
|
||||
if [ "$i" -ne "$offset" ]; then
|
||||
echo -e "\t},"
|
||||
fi
|
||||
|
||||
j=1 # local loop iterator
|
||||
last_word=$(echo "$fields" | wc -w)
|
||||
|
||||
# Restoring old delimeter
|
||||
IFS=' '
|
||||
# Print data
|
||||
for field in $fields; do
|
||||
eval value=\"$field\"
|
||||
value=$(echo "$value"|sed -e 's/"/\\"/g' -e "s/%quote%/'/g")
|
||||
|
||||
# Checking parrent key
|
||||
if [ "$j" -eq 1 ]; then
|
||||
echo -e "\t\"$value\": {"
|
||||
else
|
||||
if [ "$j" -eq "$last_word" ]; then
|
||||
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
|
||||
else
|
||||
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
|
||||
fi
|
||||
fi
|
||||
j=$(($j + 1))
|
||||
done
|
||||
fi
|
||||
i=$(($i + 1))
|
||||
done < $conf
|
||||
|
||||
# If there was any output
|
||||
if [ -n "$value" ]; then
|
||||
echo -e "\t}"
|
||||
fi
|
||||
|
||||
# Printing bottom json bracket
|
||||
echo -e "}"
|
||||
}
|
||||
|
||||
# Shell list for dns domain templates
|
||||
dnstpl_shell_list() {
|
||||
# Definigng variables
|
||||
i='1' # iterator
|
||||
end=$(($limit + $offset)) # last string
|
||||
|
||||
# Listing files by mask
|
||||
for template in $(ls $V_DNSTPL/| grep '.descr'); do
|
||||
|
||||
# Defining template name
|
||||
tpl_name="${template//.descr/}"
|
||||
|
||||
# Defining template description
|
||||
tpl_descr=$(cat $V_DNSTPL/$template |grep '#')
|
||||
|
||||
# Checking offset and limit
|
||||
if [ "$i" -ge "$offset" ] && [ "$i" -lt "$end" ] && [ "$offset" -gt 0 ]
|
||||
then
|
||||
# Print result
|
||||
echo "----------"
|
||||
echo "TEMPLATE: $tpl_name"
|
||||
echo "${tpl_descr//# /}"
|
||||
fi
|
||||
i=$(($i + 1))
|
||||
done
|
||||
}
|
||||
|
||||
# Json list for dns domain templates
|
||||
dnstpl_json_list() {
|
||||
i=1 # iterator
|
||||
end=$(($limit + $offset)) # last string
|
||||
|
||||
# Print top bracket
|
||||
echo '{'
|
||||
|
||||
# Listing files by mask
|
||||
for template in $(ls $V_DNSTPL/| grep '.descr'); do
|
||||
|
||||
# Defining template description
|
||||
descr=$(cat $V_DNSTPL/$template |grep '#'|sed -e ':a;N;$!ba;s/\n/ /g')
|
||||
|
||||
# Checking offset and limit
|
||||
if [ "$i" -ge "$offset" ] && [ "$i" -lt "$end" ] && [ "$offset" -gt 0 ]
|
||||
then
|
||||
# Checking !first line to print bracket
|
||||
if [ "$i" -ne "$offset" ]; then
|
||||
echo -e "\t},"
|
||||
fi
|
||||
|
||||
# Defining template name
|
||||
tpl_name="${template//.descr/}"
|
||||
|
||||
# Print result
|
||||
echo -e "\t\"$tpl_name\": {"
|
||||
echo -e "\t\t\"DESCR\": \"${descr//# /}\""
|
||||
fi
|
||||
i=$(($i + 1))
|
||||
done
|
||||
|
||||
# If there was any output
|
||||
if [ -n "$tpl_name" ]; then
|
||||
echo -e "\t}"
|
||||
fi
|
||||
|
||||
echo "}"
|
||||
}
|
||||
|
||||
dom_json_single_list() {
|
||||
i=1 # iterator
|
||||
|
||||
# Define words number
|
||||
last_word=$(echo "$fields" | wc -w)
|
||||
|
||||
# Reading file line by line
|
||||
line=$(grep "DOMAIN='$domain'" $conf)
|
||||
|
||||
# Print top bracket
|
||||
echo '{'
|
||||
|
||||
# Parsing key=value
|
||||
for key in $line; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Starting output loop
|
||||
for field in $fields; do
|
||||
# Parsing key=value
|
||||
eval value=$field
|
||||
|
||||
# Checking first field
|
||||
if [ "$i" -eq 1 ]; then
|
||||
echo -e "\t\"$value\": {"
|
||||
else
|
||||
if [ "$last_word" -eq "$i" ]; then
|
||||
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
|
||||
else
|
||||
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
|
||||
fi
|
||||
fi
|
||||
# Updating iterator
|
||||
i=$(( i + 1))
|
||||
done
|
||||
|
||||
# If there was any output
|
||||
if [ -n "$value" ]; then
|
||||
echo -e "\t}"
|
||||
fi
|
||||
# Printing bottom json bracket
|
||||
echo -e "}"
|
||||
}
|
||||
|
||||
dom_shell_single_list() {
|
||||
|
||||
# Reading file line by line
|
||||
line=$(grep "DOMAIN='$domain'" $conf)
|
||||
|
||||
# Parsing key=value
|
||||
for key in $line; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Print result line
|
||||
for field in $fields; do
|
||||
eval key="$field"
|
||||
echo "${field//$/}: $key "
|
||||
done
|
||||
}
|
||||
|
||||
webtpl_json_list() {
|
||||
i='1' # iterator
|
||||
end=$(($limit + $offset)) # last string
|
||||
|
||||
# Print top bracket
|
||||
echo '{'
|
||||
|
||||
# Listing files by mask
|
||||
for template in $(echo "$templates" |sed -e "s/,/\n/g"); do
|
||||
# Defining template description
|
||||
descr=$(cat $V_WEBTPL/apache_$template.descr|grep '#'|\
|
||||
sed -e ':a;N;$!ba;s/\n/ /g')
|
||||
# Checking offset and limit
|
||||
if [ "$i" -ge "$offset" ] && [ "$i" -lt "$end" ] && [ "$offset" -gt 0 ]
|
||||
then
|
||||
# Checking !first line to print bracket
|
||||
if [ "$i" -ne "$offset" ]; then
|
||||
echo -e "\t},"
|
||||
fi
|
||||
|
||||
# Print result
|
||||
echo -e "\t\"$template\": {"
|
||||
echo -e "\t\t\"DESCR\": \"${descr//# /}\""
|
||||
fi
|
||||
i=$(($i + 1))
|
||||
done
|
||||
|
||||
# If there was any output
|
||||
if [ -n "$template" ]; then
|
||||
echo -e "\t}"
|
||||
fi
|
||||
echo "}"
|
||||
}
|
||||
|
||||
webtpl_shell_list() {
|
||||
i='1' # iterator
|
||||
end=$(($limit + $offset)) # last string
|
||||
|
||||
# Listing files by mask
|
||||
for template in $(echo "$templates" |sed -e "s/,/\n/g"); do
|
||||
# Defining template description
|
||||
tpl_descr=$(cat $V_WEBTPL/apache_$template.descr |grep '#')
|
||||
# Checking offset and limit
|
||||
if [ "$i" -ge "$offset" ] && [ "$i" -lt "$end" ] && [ "$offset" -gt 0 ]
|
||||
then
|
||||
# Print result
|
||||
echo "----------"
|
||||
echo "TEMPLATE: $template"
|
||||
echo "${tpl_descr//# /}"
|
||||
fi
|
||||
i=$(($i + 1))
|
||||
done
|
||||
}
|
||||
|
||||
dom_clear_search(){
|
||||
# Defining delimeter
|
||||
IFS=$'\n'
|
||||
|
||||
# Reading file line by line
|
||||
for line in $(grep $search_string $conf); do
|
||||
# Parsing key=val
|
||||
for key in $line; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
# Print result line
|
||||
eval echo "$field"
|
||||
done
|
||||
}
|
||||
|
||||
dom_clear_list() {
|
||||
# Reading file line by line
|
||||
while read line ; do
|
||||
|
||||
# Parsing key=value
|
||||
for key in $line; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Print result line
|
||||
eval echo "$field"
|
||||
done < $conf
|
||||
}
|
||||
|
||||
namehost_ip_support() {
|
||||
#Checking web system
|
||||
if [ "$WEB_SYSTEM" = 'apache' ]; then
|
||||
# Checking httpd config for NameHost string number
|
||||
conf_line=$(grep -n "NameVirtual" $conf|tail -n 1|cut -f 1 -d ':')
|
||||
if [ ! -z "$conf_line" ]; then
|
||||
conf_ins=$((conf_line + 1)) # inster into next line
|
||||
else
|
||||
conf_ins='1' # insert into first line
|
||||
fi
|
||||
|
||||
# Checking ssl support
|
||||
if [ "$WEB_SSL" = 'mod_ssl' ]; then
|
||||
ssl_port=$(get_web_port_ssl) # calling internal function
|
||||
sed -i "$conf_ins i NameVirtualHost $ip:$ssl_port" $conf
|
||||
fi
|
||||
port=$(get_web_port) # calling internal function
|
||||
sed -i "$conf_ins i NameVirtualHost $ip:$port" $conf
|
||||
|
||||
# Checking proxy support
|
||||
if [ "$PROXY_SYSTEM" = 'nginx' ]; then
|
||||
cat $V_WEBTPL/ngingx_ip.tpl | sed -e "s/%ip%/$ip/g" \
|
||||
-e "s/%web_port%/$port/g" -e "s/%proxy_port%/80/g" >>$nconf
|
||||
|
||||
# Adding to rpaf ip pool as well
|
||||
ips=$(grep 'RPAFproxy_ips' $rconf)
|
||||
sed -i "s/$ips/$ips $ip/g" $rconf
|
||||
fi
|
||||
|
||||
# Scheduling restart
|
||||
web_restart='yes'
|
||||
fi
|
||||
}
|
||||
|
||||
namehost_ip_disable() {
|
||||
#Checking web system
|
||||
if [ "$WEB_SYSTEM" = 'apache' ]; then
|
||||
sed -i "/NameVirtualHost $ip:/d" $conf
|
||||
|
||||
# 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 |\
|
||||
cut -f 1 -d :)
|
||||
|
||||
conf_line=$(grep -n -w $ip $nconf|head -n1|cut -f 1 -d :)
|
||||
|
||||
# Checking parsed lines
|
||||
if [ -z "$tpl_ln" ] || [ -z "$ip_line" ] || [ -z "$conf_line" ]
|
||||
then
|
||||
echo "Error: nginx config paring error"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
up_line=$((ip_line - 1))
|
||||
first_line=$((conf_line - up_line))
|
||||
last_line=$((conf_line - ip_line + tpl_ln))
|
||||
|
||||
# Checking parsed lines
|
||||
if [ -z "$first_line" ] || [ -z "$last_line" ]; then
|
||||
echo "Error: nginx config paring error"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
sed -i "$first_line,$last_line d" $nconf
|
||||
|
||||
# Deleting from rpaf ip pool as well
|
||||
ips=$(grep 'RPAFproxy_ips' $rconf)
|
||||
new_ips=$(echo "$ips"|sed -e "s/$ip//")
|
||||
sed -i "s/$ips/$new_ips/g" $rconf
|
||||
fi
|
||||
|
||||
# Scheduling restart
|
||||
web_restart='yes'
|
||||
fi
|
||||
}
|
593
func/ip_func.sh
Normal file
593
func/ip_func.sh
Normal file
|
@ -0,0 +1,593 @@
|
|||
is_sys_ip_free() {
|
||||
# Parsing system ips
|
||||
ip_list=$(ifconfig|grep 'inet addr:'|cut -f 2 -d ':'|cut -f 1 -d " ")
|
||||
|
||||
# Checking ip existance
|
||||
ip_check=$(echo "$ip_list"|grep -w "$ip")
|
||||
if [ -n "$ip_check" ] || [ -e "$V_IPS/$ip" ]; then
|
||||
echo "Error: IP exist"
|
||||
log_event 'debug' "$E_IP_EXIST $V_EVENT"
|
||||
exit $E_IP_EXIST
|
||||
fi
|
||||
}
|
||||
|
||||
get_next_interface_number() {
|
||||
# Parsing ifconfig
|
||||
i=$(ifconfig -a |grep -w "$interface"|cut -f1 -d ' '|\
|
||||
tail -n 1|cut -f 2 -d :)
|
||||
|
||||
# Checking result
|
||||
if [ "$i" = "$interface" ]; then
|
||||
n=0
|
||||
else
|
||||
n=$((i + 1))
|
||||
fi
|
||||
echo ":$n"
|
||||
}
|
||||
|
||||
is_sys_ip_valid() {
|
||||
# Parsing ifconfig
|
||||
check_ifc=$(/sbin/ifconfig |grep "inet addr:$ip")
|
||||
|
||||
# Checking ip existance
|
||||
if [ ! -e "$V_IPS/$ip" ] || [ -z "$check_ifc" ]; then
|
||||
echo "Error: IP not exist"
|
||||
log_event 'debug' "$E_IP_NOTEXIST $V_EVENT"
|
||||
exit $E_IP_NOTEXIST
|
||||
fi
|
||||
}
|
||||
|
||||
is_ip_key_empty() {
|
||||
key="$1"
|
||||
|
||||
# Parsing ip
|
||||
string=$(cat $V_IPS/$ip )
|
||||
|
||||
# Parsing key=value
|
||||
for keys in $string; do
|
||||
eval ${keys%%=*}=${keys#*=}
|
||||
done
|
||||
|
||||
# Self reference
|
||||
eval value="$key"
|
||||
|
||||
# Checkng key
|
||||
if [ ! -z "$value" ] && [ "$value" != '0' ]; then
|
||||
echo "Error: value is not empty = $value "
|
||||
log_event 'debug' "$E_VALUE_EXIST $V_EVENT"
|
||||
exit $E_VALUE_EXIST
|
||||
fi
|
||||
}
|
||||
|
||||
update_sys_ip_value() {
|
||||
key="$1"
|
||||
value="$2"
|
||||
|
||||
# Defining conf
|
||||
conf="$V_IPS/$ip"
|
||||
|
||||
# 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 "$str_number s/$c_key='${old//\*/\\*}'/$c_key='${new//\*/\\*}'/g"\
|
||||
$conf
|
||||
}
|
||||
|
||||
is_ip_avalable() {
|
||||
# Checking ip existance
|
||||
if [ ! -e "$V_IPS/$ip" ]; then
|
||||
echo "Error: IP not exist"
|
||||
log_event 'debug' "$E_IP_NOTEXIST $V_EVENT"
|
||||
exit $E_IP_NOTEXIST
|
||||
fi
|
||||
|
||||
# Parsing ip data
|
||||
ip_data=$(cat $V_IPS/$ip)
|
||||
ip_owner=$(echo "$ip_data" | grep 'OWNER=' | cut -f 2 -d \' )
|
||||
ip_status=$(echo "$ip_data" | grep 'STATUS=' | cut -f 2 -d \' )
|
||||
|
||||
# Parsing user data
|
||||
user_owner=$(grep 'OWNER=' $V_USERS/$user/user.conf | cut -f 2 -d \')
|
||||
if [ "$user_owner" = "$ip_owner" ] && [ "$ip_status" = 'shared' ]; then
|
||||
ip_shared='yes'
|
||||
else
|
||||
ip_shared='no'
|
||||
fi
|
||||
|
||||
if [ "$ip_owner" != "$user" ] && [ "$ip_shared" != 'yes' ]; then
|
||||
echo "Error: ip not owned by user"
|
||||
log_event 'debug' "$E_IP_NOTOWNED $V_EVENT"
|
||||
exit $E_IP_NOTOWNED
|
||||
fi
|
||||
}
|
||||
|
||||
is_sys_ip_owner() {
|
||||
# Parsing ip
|
||||
ip_owner=$(grep 'OWNER=' $V_IPS/$ip|cut -f 2 -d \')
|
||||
if [ "$ip_owner" != "$user" ]; then
|
||||
echo "Error: IP not owned"
|
||||
log_event 'debug' "$E_IP_NOTOWNED $V_EVENT"
|
||||
exit $E_IP_NOTOWNED
|
||||
fi
|
||||
}
|
||||
|
||||
get_ip_name() {
|
||||
# Prinitng name
|
||||
grep "NAME=" $V_IPS/$ip |cut -f 2 -d \'
|
||||
}
|
||||
|
||||
increase_ip_value() {
|
||||
USER=$user
|
||||
web_key='U_WEB_DOMAINS'
|
||||
usr_key='U_SYS_USERS'
|
||||
|
||||
# Parsing values
|
||||
current_web=$(grep "$web_key=" $V_IPS/$ip |cut -f 2 -d \')
|
||||
current_usr=$(grep "$usr_key=" $V_IPS/$ip |cut -f 2 -d \')
|
||||
|
||||
# Checking result
|
||||
if [ -z "$current_web" ]; then
|
||||
echo "Error: Parsing error"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# +1 webdomain
|
||||
new_web=$((current_web + 1))
|
||||
|
||||
# +1 user
|
||||
if [ -z "$current_usr" ]; then
|
||||
new_usr="$USER"
|
||||
else
|
||||
check_usr=$(echo -e "${current_usr//,/\n}" |grep -w $USER)
|
||||
if [ -z "$check_usr" ]; then
|
||||
new_usr="$current_usr,$USER"
|
||||
else
|
||||
new_usr="$current_usr"
|
||||
fi
|
||||
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
|
||||
}
|
||||
|
||||
decrease_ip_value() {
|
||||
sip=${1-ip}
|
||||
USER=$user
|
||||
web_key='U_WEB_DOMAINS'
|
||||
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 \')
|
||||
|
||||
# Checking result
|
||||
if [ -z "$current_web" ]; then
|
||||
echo "Error: Parsing error"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# -1 webdomain
|
||||
new_web=$((current_web - 1))
|
||||
|
||||
# -1 user
|
||||
check_ip=$(grep $sip $V_USERS/$user/web_domains.conf |wc -l)
|
||||
if [ "$check_ip" -lt 2 ]; then
|
||||
new_usr=$(echo "$current_usr" |\
|
||||
sed -e "s/,/\n/g"|\
|
||||
sed -e "s/^$user$//g"|\
|
||||
sed -e "/^$/d"|\
|
||||
sed -e ':a;N;$!ba;s/\n/,/g')
|
||||
else
|
||||
new_usr="$current_usr"
|
||||
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
|
||||
}
|
||||
|
||||
get_sys_ip_value() {
|
||||
key="$1"
|
||||
|
||||
# Parsing domains
|
||||
string=$( cat $V_IPS/$ip )
|
||||
|
||||
# Parsing key=value
|
||||
for keys in $string; do
|
||||
eval ${keys%%=*}=${keys#*=}
|
||||
done
|
||||
|
||||
# Self reference
|
||||
eval value="$key"
|
||||
|
||||
# Print value
|
||||
echo "$value"
|
||||
}
|
||||
|
||||
change_domain_ip() {
|
||||
# Defining vars
|
||||
conf="$1"
|
||||
domain="$2"
|
||||
ip="$3"
|
||||
old_ip="$4"
|
||||
tpl_file="$5"
|
||||
|
||||
# Get ServerName line
|
||||
serv_line=$(grep -n 'ServerName %domain%' "$tpl_file" |cut -f 1 -d :)
|
||||
|
||||
# Get tpl_file last line
|
||||
last_line=$(wc -l $tpl_file|cut -f 1 -d ' ')
|
||||
|
||||
# Get before line
|
||||
bfr_line=$((serv_line - 1))
|
||||
|
||||
# Parsing httpd.conf
|
||||
str=$(grep -B $bfr_line -n "ServerName $domain" $conf|grep '<VirtualHost')
|
||||
|
||||
# Checking integrity
|
||||
if [ -z "$str" ] || [ -z "$serv_line" ] || [ -z "$bfr_line" ]; then
|
||||
echo "Error: httpd parsing error"
|
||||
log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
|
||||
exit $E_PARSE_ERROR
|
||||
fi
|
||||
|
||||
# String number
|
||||
str_number=$(echo $str | sed -e "s/-/+/" | cut -f 1 -d '+')
|
||||
|
||||
# Changing elog in config
|
||||
sed -i "$str_number s/$old_ip/$ip/g" $conf
|
||||
}
|
||||
|
||||
get_current_interface() {
|
||||
# Parsing ifconfig
|
||||
i=$(/sbin/ifconfig |grep -B1 "addr:$ip "|head -n 1 |cut -f 1 -d ' ')
|
||||
|
||||
# Checking result
|
||||
if [ -z "$i" ]; then
|
||||
echo "Error: IP not exist"
|
||||
log_event 'debug' "$E_IP_NOTEXIST $V_EVENT"
|
||||
exit $E_IP_NOTEXIST
|
||||
fi
|
||||
|
||||
# Checking ip is alias
|
||||
check_alias=$(echo $i| cut -s -f 2 -d :)
|
||||
if [ -z "$check_alias" ]; then
|
||||
echo "Error: IP is first on interface"
|
||||
log_event 'debug' "$E_IP_FIRST $V_EVENT"
|
||||
exit $E_IP_FIRST
|
||||
fi
|
||||
echo "$i"
|
||||
}
|
||||
|
||||
ip_json_single_list() {
|
||||
# Definigng variables
|
||||
IP="$ip" # ip
|
||||
i=1 # iterator
|
||||
|
||||
# Define words number
|
||||
last_word=$(echo "$fields" | wc -w)
|
||||
|
||||
# Reading file line by line
|
||||
line=$(cat $V_IPS/$IP)
|
||||
|
||||
# Print top bracket
|
||||
echo '{'
|
||||
|
||||
# Parsing key=value
|
||||
for key in $line; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Starting output loop
|
||||
for field in $fields; do
|
||||
# Parsing key=value
|
||||
eval value=$field
|
||||
# Checking first field
|
||||
if [ "$i" -eq 1 ]; then
|
||||
echo -e "\t\"$value\": {"
|
||||
else
|
||||
if [ "$last_word" -eq "$i" ]; then
|
||||
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
|
||||
else
|
||||
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
|
||||
fi
|
||||
fi
|
||||
# Updating iterator
|
||||
i=$(( i + 1))
|
||||
done
|
||||
|
||||
# If there was any output
|
||||
if [ -n "$value" ]; then
|
||||
echo -e "\t}"
|
||||
fi
|
||||
|
||||
# Printing bottom json bracket
|
||||
echo -e "}"
|
||||
}
|
||||
|
||||
ip_shell_single_list() {
|
||||
# Definigng variables
|
||||
IP="$ip" # ip
|
||||
|
||||
# Reading file line by line
|
||||
line=$(cat $V_IPS/$IP)
|
||||
|
||||
# Parsing key=value
|
||||
for key in $line; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Print result line
|
||||
for field in $fields; do
|
||||
eval key="$field"
|
||||
echo "${field//$/}: $key "
|
||||
done
|
||||
}
|
||||
|
||||
ip_json_list() {
|
||||
i='1' # iterator
|
||||
end=$(($limit + $offset)) # last string
|
||||
|
||||
# Definining user list
|
||||
ip_list=$(ls $V_IPS/)
|
||||
|
||||
# Print top bracket
|
||||
echo '{'
|
||||
|
||||
# Starting main loop
|
||||
for IP in $ip_list; do
|
||||
|
||||
# Checking offset and limit
|
||||
if [ "$i" -ge "$offset" ] && [ "$i" -lt "$end" ] && [ "$offset" -gt 0 ]
|
||||
then
|
||||
# Reading user data
|
||||
ip_data=$(cat $V_IPS/$IP)
|
||||
|
||||
# Parsing key/value config
|
||||
for key in $ip_data; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Checking !first line to print bracket with coma
|
||||
if [ "$i" -ne "$offset" ]; then
|
||||
echo -e "\t},"
|
||||
fi
|
||||
|
||||
# Defining local iterator and words count
|
||||
j='1'
|
||||
last_word=$(echo "$fields" | wc -w)
|
||||
|
||||
# Print data
|
||||
for field in $fields; do
|
||||
eval value=$field
|
||||
# Checking parrent key
|
||||
if [ "$j" -eq 1 ]; then
|
||||
echo -e "\t\"$value\": {"
|
||||
else
|
||||
if [ "$j" -eq "$last_word" ]; then
|
||||
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
|
||||
else
|
||||
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
|
||||
fi
|
||||
fi
|
||||
j=$(($j + 1))
|
||||
done
|
||||
fi
|
||||
i=$(($i + 1))
|
||||
done
|
||||
|
||||
# If there was any output
|
||||
if [ -n "$value" ]; then
|
||||
echo -e "\t}"
|
||||
fi
|
||||
|
||||
# Printing bottom json bracket
|
||||
echo '}'
|
||||
}
|
||||
|
||||
ip_shell_list() {
|
||||
i='1' # iterator
|
||||
end=$(($limit + $offset)) # last string
|
||||
|
||||
# Definining ip list
|
||||
ip_list=$(ls $V_IPS/)
|
||||
|
||||
# Print brief info
|
||||
echo "${fields//$/}"
|
||||
for a in $fields; do
|
||||
echo -e "--------- \c"
|
||||
done
|
||||
echo # new line
|
||||
|
||||
# Starting main loop
|
||||
for IP in $ip_list; do
|
||||
# Checking offset and limit
|
||||
if [ "$i" -ge "$offset" ] && [ "$i" -lt "$end" ] && [ "$offset" -gt 0 ]
|
||||
then
|
||||
# Reading user data
|
||||
ip_data=$(cat $V_IPS/$IP)
|
||||
|
||||
# Parsing key/value config
|
||||
for key in $ip_data; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Print result line
|
||||
eval echo "$fields"
|
||||
fi
|
||||
i=$(($i + 1))
|
||||
done
|
||||
}
|
||||
|
||||
ip_user_json_list() {
|
||||
i='1' # iterator
|
||||
end=$(($limit + $offset)) # last string
|
||||
user_ip=$(grep -l "OWNER='$user'" $V_IPS/*)
|
||||
owner_ip=$(grep -l -A2 "OWNER='$owner'" $V_IPS/*|grep "STATUS='shared'"|\
|
||||
cut -f 1 -d -)
|
||||
|
||||
# Definining ip list
|
||||
ip_list=$(echo -e "$user_ip\n$owner_ip"|sort|uniq)
|
||||
|
||||
# Print top bracket
|
||||
echo '{'
|
||||
|
||||
# Starting main loop
|
||||
for IP in ${ip_list//$V_IPS\//}; do
|
||||
# Checking offset and limit
|
||||
if [ "$i" -ge "$offset" ] && [ "$i" -lt "$end" ] && [ "$offset" -gt 0 ]
|
||||
then
|
||||
# Reading user data
|
||||
ip_data=$(cat $V_IPS/$IP)
|
||||
|
||||
# Parsing key/value config
|
||||
for key in $ip_data; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Checking !first line to print bracket with coma
|
||||
if [ "$i" -ne "$offset" ]; then
|
||||
echo -e "\t},"
|
||||
fi
|
||||
|
||||
# Defining local iterator and words count
|
||||
j='1'
|
||||
last_word=$(echo "$fields"| wc -w)
|
||||
|
||||
# Print data
|
||||
for field in $fields; do
|
||||
eval value=$field
|
||||
# Checking parrent key
|
||||
if [ "$j" -eq 1 ]; then
|
||||
echo -e "\t\"$value\": {"
|
||||
else
|
||||
if [ "$j" -eq "$last_word" ]; then
|
||||
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
|
||||
else
|
||||
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
|
||||
fi
|
||||
fi
|
||||
j=$(($j + 1))
|
||||
done
|
||||
fi
|
||||
i=$(($i + 1))
|
||||
done
|
||||
|
||||
# If there was any output
|
||||
if [ -n "$value" ]; then
|
||||
echo -e "\t}"
|
||||
fi
|
||||
|
||||
# Printing bottom json bracket
|
||||
echo '}'
|
||||
}
|
||||
|
||||
ip_user_shell_list() {
|
||||
i='1' # iterator
|
||||
end=$(($limit + $offset)) # last string
|
||||
user_ip=$(grep -l "OWNER='$user'" $V_IPS/*)
|
||||
owner_ip=$(grep -A2 "OWNER='$owner'" $V_IPS/* |grep "STATUS='shared'" |\
|
||||
cut -f 1 -d -)
|
||||
|
||||
# Definining ip list
|
||||
ip_list=$(echo -e "$user_ip\n$owner_ip"|sort|uniq)
|
||||
|
||||
# Print brief info
|
||||
echo "${fields//$/}"
|
||||
for a in $fields; do
|
||||
echo -e "--------- \c"
|
||||
done
|
||||
echo # new line
|
||||
|
||||
# Starting main loop
|
||||
for IP in ${ip_list//$V_IPS\//}; do
|
||||
# Checking offset and limit
|
||||
if [ "$i" -ge "$offset" ] && [ "$i" -lt "$end" ] && [ "$offset" -gt 0 ]
|
||||
then
|
||||
# Reading user data
|
||||
ip_data=$(cat $V_IPS/$IP)
|
||||
|
||||
# Parsing key/value config
|
||||
for key in $ip_data; do
|
||||
eval ${key%%=*}=${key#*=}
|
||||
done
|
||||
|
||||
# Print result line
|
||||
eval echo "$fields"
|
||||
fi
|
||||
i=$(($i + 1))
|
||||
done
|
||||
}
|
||||
|
||||
ip_add_vesta() {
|
||||
# Filling ip values
|
||||
ip_data="OWNER='$owner'"
|
||||
ip_data="$ip_data\nSTATUS='$ip_status'"
|
||||
ip_data="$ip_data\nNAME='$ip_name'"
|
||||
ip_data="$ip_data\nU_SYS_USERS=''"
|
||||
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'"
|
||||
|
||||
# Adding ip
|
||||
echo -e "$ip_data" >$V_IPS/$ip
|
||||
}
|
||||
|
||||
ip_add_startup() {
|
||||
# Filling ip values
|
||||
ip_data="# Added by vesta $V_SCRIPT"
|
||||
ip_data="$ip_data\nDEVICE=$iface"
|
||||
ip_data="$ip_data\nBOOTPROTO=static\nONBOOT=yes"
|
||||
ip_data="$ip_data\nIPADDR=$ip"
|
||||
ip_data="$ip_data\nNETMASK=$mask"
|
||||
|
||||
# Adding ip
|
||||
echo -e "$ip_data" >$iconf-$iface
|
||||
}
|
||||
|
||||
ipint_json_list() {
|
||||
interfaces=$(cat /proc/net/dev|grep :|cut -f 1 -d :|sed -e "s/ //g")
|
||||
int_counter=$(echo "$interfaces"|wc -l)
|
||||
i=1
|
||||
# Print top bracket
|
||||
echo '['
|
||||
# Listing servers
|
||||
for interface in $interfaces; do
|
||||
if [ "$i" -lt "$int_counter" ]; then
|
||||
echo -e "\t\"$interface\","
|
||||
else
|
||||
echo -e "\t\"$interface\""
|
||||
fi
|
||||
i=$((i + 1))
|
||||
done
|
||||
echo "]"
|
||||
}
|
||||
|
||||
ipint_shell_list() {
|
||||
interfaces=$(cat /proc/net/dev|grep :|cut -f 1 -d :|sed -e "s/ //g")
|
||||
# Print result
|
||||
echo "INTERFACES"
|
||||
echo "----------"
|
||||
for interface in $interfaces; do
|
||||
echo "$interface"
|
||||
done
|
||||
}
|
24
func/restart_cron
Executable file
24
func/restart_cron
Executable file
|
@ -0,0 +1,24 @@
|
|||
#!/bin/bash
|
||||
# Internal vesta function
|
||||
# cron system restart
|
||||
|
||||
# Importing variables
|
||||
source $VESTA/conf/vars.conf
|
||||
|
||||
crond() {
|
||||
/etc/init.d/crond 'reload' >/dev/null 2>&1
|
||||
if [ $? -ne 0 ]; then
|
||||
#$V_FUNC/report_issue 'sys' 'cron'
|
||||
echo "$E_RESTART_FAILED $V_EVENT"
|
||||
fi
|
||||
}
|
||||
|
||||
# Parsing config / or just source config
|
||||
cron_system=$(grep 'CRON_SYSTEM=' $V_CONF/vesta.conf | cut -f 2 -d \' )
|
||||
|
||||
if [ "$cron_system" = 'crond' ]; then
|
||||
crond
|
||||
fi
|
||||
|
||||
# Logging
|
||||
exit $OK
|
23
func/restart_dns
Executable file
23
func/restart_dns
Executable file
|
@ -0,0 +1,23 @@
|
|||
#!/bin/bash
|
||||
# Internal vesta function
|
||||
# dns system restart
|
||||
|
||||
# Importing variables
|
||||
source $VESTA/conf/vars.conf
|
||||
|
||||
bind() {
|
||||
/etc/init.d/named 'reload' >/dev/null 2>&1
|
||||
if [ $? -ne 0 ]; then
|
||||
#$V_FUNC/report_issue 'sys' 'cron'
|
||||
echo "$E_RESTART_FAILED $V_EVENT"
|
||||
fi
|
||||
}
|
||||
|
||||
# Parsing config / or just source config
|
||||
dns_system=$(grep 'DNS_SYSTEM=' $V_CONF/vesta.conf | cut -f 2 -d \' )
|
||||
|
||||
if [ "$dns_system" = 'bind' ]; then
|
||||
bind
|
||||
fi
|
||||
|
||||
exit $OK
|
39
func/restart_web
Executable file
39
func/restart_web
Executable file
|
@ -0,0 +1,39 @@
|
|||
#!/bin/bash
|
||||
# Internal vesta function
|
||||
# web system restart
|
||||
|
||||
# Importing variables
|
||||
source $VESTA/conf/vars.conf
|
||||
|
||||
# Restart functions
|
||||
apache() {
|
||||
/etc/init.d/httpd 'graceful' >/dev/null 2>&1
|
||||
if [ $? -ne 0 ]; then
|
||||
#$V_FUNC/report_issue 'web' 'apache'
|
||||
echo "$E_RESTART_FAILED $V_EVENT"
|
||||
fi
|
||||
}
|
||||
|
||||
nginx() {
|
||||
/etc/init.d/nginx 'restart' >/dev/null 2>&1
|
||||
if [ $? -ne 0 ]; then
|
||||
#$V_FUNC/report_issue 'web' 'nginx'
|
||||
echo "$E_RESTART_FAILED $V_EVENT"
|
||||
fi
|
||||
}
|
||||
|
||||
# Parsing config
|
||||
web_system=$(grep 'WEB_SYSTEM=' $V_CONF/vesta.conf | cut -f 2 -d \' )
|
||||
proxy_system=$(grep 'PROXY_SYSTEM=' $V_CONF/vesta.conf | cut -f 2 -d \' )
|
||||
|
||||
# Checking values
|
||||
if [ "$web_system" = 'apache' ]; then
|
||||
apache
|
||||
fi
|
||||
|
||||
if [ "$proxy_system" = 'nginx' ]; then
|
||||
nginx
|
||||
fi
|
||||
|
||||
# Logging
|
||||
exit $OK
|
1336
func/shared_func.sh
Normal file
1336
func/shared_func.sh
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue