Skip to content

Commit f48bed1

Browse files
authored
Add check v-add-database-host and v-add-database (hestiacp#3094)
* Check if user can create database or not * Add check on drop / create database * Restore deleted line * Drop failed due to database doesn't exsits
1 parent 69ea06c commit f48bed1

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

bin/v-add-database-host

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,30 @@ is_mysql_host_alive() {
4545

4646
chmod 600 $mycnf
4747
mysql --defaults-file=$mycnf -e 'SELECT VERSION()' > /dev/null 2>&1
48-
rm $mycnf
48+
if [ "$?" -ne '0' ]; then
49+
echo "Error: MySQL connection to $host failed"
50+
rm $mycnf
51+
log_event "$E_CONNECT" "$ARGUMENTS"
52+
exit "$E_CONNECT"
53+
fi
4954

55+
grants=$(mysql --defaults-file=$mycnf -e 'SHOW GRANTS FOR CURRENT_USER();')
5056
if [ "$?" -ne '0' ]; then
5157
echo "Error: MySQL connection to $host failed"
58+
rm $mycnf
5259
log_event "$E_CONNECT" "$ARGUMENTS"
5360
exit "$E_CONNECT"
5461
fi
62+
63+
# Check allow to grant user
64+
check_grants=$(echo $grants | grep "WITH GRANT OPTION")
65+
if [ -z "$check_grants" ]; then
66+
echo "Error: MySQL connection to $host failed. Unable to grant other users"
67+
rm $mycnf
68+
log_event "$E_CONNECT" "$ARGUMENTS"
69+
exit "$E_CONNECT"
70+
fi
71+
rm $mycnf
5572
}
5673

5774
is_pgsql_host_alive() {

func/db.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ mysql_query() {
9191
sql_tmp=$(mktemp)
9292
echo "$1" > $sql_tmp
9393
mysql --defaults-file=$mycnf < "$sql_tmp" 2> /dev/null
94+
return_code=$?
9495
rm -f "$sql_tmp"
96+
return $return_code
9597
}
9698

9799
mysql_dump() {
@@ -258,7 +260,8 @@ add_mysql_database() {
258260
mysql_ver_sub_sub=$(echo $mysql_ver | cut -d '.' -f2)
259261

260262
query="CREATE DATABASE \`$database\` CHARACTER SET $charset"
261-
mysql_query "$query" > /dev/null
263+
mysql_query "$query"
264+
check_result $? "Unable to create database $database"
262265

263266
if [ "$mysql_fork" = "mysql" ] && [ "$mysql_ver_sub" -ge 8 ]; then
264267
query="CREATE USER \`$dbuser\`@\`%\`
@@ -458,7 +461,7 @@ delete_mysql_database() {
458461
mysql_connect $HOST
459462

460463
query="DROP DATABASE \`$database\`"
461-
mysql_query "$query" > /dev/null
464+
mysql_query "$query"
462465

463466
query="REVOKE ALL ON \`$database\`.* FROM \`$DBUSER\`@\`%\`"
464467
mysql_query "$query" > /dev/null

0 commit comments

Comments
 (0)