diff --git a/bin/v-change-database-user b/bin/v-change-database-user new file mode 100755 index 00000000..e07cf0ca --- /dev/null +++ b/bin/v-change-database-user @@ -0,0 +1,97 @@ +#!/bin/bash +# info: change database username +# options: USER DATABASE DBUSER [DBPASS] +# +# The function for changing database user. It uses the + + +#----------------------------------------------------------# +# Variable&Function # +#----------------------------------------------------------# + +# Argument defenition +user=$1 +database=$2 +dbuser="$user"_"$3" +dbpass=$4 + +# Includes +source $VESTA/func/main.sh +source $VESTA/func/db.sh +source $VESTA/func/rebuild.sh +source $VESTA/conf/vesta.conf + +# Hiding password +A4='******' +EVENT="DATE='$DATE' TIME='$TIME' CMD='$SCRIPT' A1='$A1' A2='$A2' A3='$A3'" +EVENT="$EVENT A4='$A4' A5='$A5' A6='$A6' A7='$A7' A8='$A8' A9='$A9'" + + +#----------------------------------------------------------# +# Verifications # +#----------------------------------------------------------# + +check_args '3' "$#" 'USER DATABASE DBUSER [DBPASS]' +validate_format 'user' 'database' 'dbuser' +if [ ! -z "$dbpass" ]; then + validate_format 'dbpass' +fi +is_system_enabled "$DB_SYSTEM" 'DB_SYSTEM' +is_object_valid 'user' 'USER' "$user" +is_object_unsuspended 'user' 'USER' "$user" +is_object_valid 'db' 'DB' "$database" +is_object_unsuspended 'db' 'DB' "$database" + +# Compare old and new user +old_dbuser=$(get_object_value 'db' 'DB' "$database" '$DBUSER') +if [ "$old_dbuser" = "$dbuser" ]; then + exit +fi + + +#----------------------------------------------------------# +# Action # +#----------------------------------------------------------# + +# Set new dbuser +update_object_value 'db' 'DB' "$database" '$DBUSER' "$dbuser" + +# Get database values +get_database_values + +# Rebuild datbase +case $TYPE in + mysql) rebuild_mysql_database ;; + pgsql) rebuild_pgsql_database ;; +esac + +# Change password +if [ ! -z "$dbpass" ]; then + case $TYPE in + mysql) change_mysql_password ;; + pgsql) change_pgsql_password ;; + esac + + # Update config value + update_object_value 'db' 'DB' "$database" '$MD5' "$md5" +fi + +# Remove old user +check_old_dbuser=$(grep "DBUSER='$old_dbuser'" $USER_DATA/db.conf) +if [ -z "$check_old_dbuser" ]; then + case $TYPE in + mysql) delete_mysql_user ;; + pgsql) delete_pgsql_user ;; + esac +fi + + +#----------------------------------------------------------# +# Vesta # +#----------------------------------------------------------# + +# Logging +log_history "changed $database database user to $dbuser" +log_event "$OK" "$EVENT" + +exit diff --git a/web/edit/db/index.php b/web/edit/db/index.php index b047527a..88d87596 100644 --- a/web/edit/db/index.php +++ b/web/edit/db/index.php @@ -51,15 +51,34 @@ if ($return_var != 0) { // Action if (!empty($_POST['save'])) { $v_username = $user; - // Change password - if (($v_password != $_POST['v_password']) && (empty($_SESSION['error_msg']))) { + + // Change database username + if (($v_dbuser != $_POST['v_dbuser']) && (empty($_SESSION['error_msg']))) { + $v_dbuser = preg_replace("/^".$user."_/", "", $_POST['v_dbuser']); + $v_dbuser = escapeshellarg($v_dbuser); + if ($v_password != $_POST['v_password']) { + // Change username and password + $v_password = escapeshellarg($_POST['v_password']); + exec (VESTA_CMD."v-change-database-user ".$v_username." ".$v_database." ".$v_dbuser." ".$v_password, $output, $return_var); + check_return_code($return_var,$output); + unset($output); + $v_dbuser = $user."_".preg_replace("/^".$user."_/", "", $_POST['v_dbuser']); + $v_password = "••••••••"; + $v_pw_changed = 'yes'; + } else { + // Change only username + exec (VESTA_CMD."v-change-database-user ".$v_username." ".$v_database." ".$v_dbuser, $output, $return_var); + check_return_code($return_var,$output); + unset($output); + $v_dbuser = $user."_".preg_replace("/^".$user."_/", "", $_POST['v_dbuser']); + } + } + + // Change only database password + if (($v_password != $_POST['v_password']) && (!isset($v_pw_changed)) && (empty($_SESSION['error_msg']))) { $v_password = escapeshellarg($_POST['v_password']); exec (VESTA_CMD."v-change-database-password ".$v_username." ".$v_database." ".$v_password, $output, $return_var); - if ($return_var != 0) { - $error = implode('
', $output); - if (empty($error)) $error = __('Error code:',$return_var); - $_SESSION['error_msg'] = $error; - } + check_return_code($return_var,$output); $v_password = "••••••••"; unset($output); } diff --git a/web/inc/main.php b/web/inc/main.php index b560453b..e44ffbcb 100644 --- a/web/inc/main.php +++ b/web/inc/main.php @@ -89,13 +89,21 @@ if (isset($_SESSION['look']) && ( $_SESSION['look'] != 'admin' )) { } -function check_error($return_var){ +function check_error($return_var) { if ( $return_var > 0 ) { header("Location: /error/"); exit; } } +function check_return_code($return_var,$output) { + if ($return_var != 0) { + $error = implode('
', $output); + if (empty($error)) $error = __('Error code:',$return_var); + $_SESSION['error_msg'] = $error; + } +} + function top_panel($user, $TAB) { global $panel; $command = VESTA_CMD."v-list-user '".$user."' 'json'"; diff --git a/web/templates/admin/edit_db.html b/web/templates/admin/edit_db.html index 989f1344..115b973b 100644 --- a/web/templates/admin/edit_db.html +++ b/web/templates/admin/edit_db.html @@ -83,7 +83,7 @@ -