Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for grant permission before adding database user, fixes #13628 #13629

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 39 additions & 30 deletions lib/private/Setup/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,37 +126,46 @@ private function createSpecificUser($username, $connection) {

//we don't have a dbuser specified in config
if ($this->dbUser !== $oldUser) {
//add prefix to the admin username to prevent collisions
$adminUser = substr('oc_' . $username, 0, 16);

$i = 1;
while (true) {
//this should be enough to check for admin rights in mysql
$query = 'SELECT user FROM mysql.user WHERE user=?';
$result = $connection->executeQuery($query, [$adminUser]);

//current dbuser has admin rights
if ($result) {
$data = $result->fetchAll();
//new dbuser does not exist
if (count($data) === 0) {
//use the admin login data for the new database user
$this->dbUser = $adminUser;

//create a random password so we don't need to store the admin password in the config file
$this->dbPassword = $this->random->generate(30);

$this->createDBUser($connection);

break;
} else {
//repeat with different username
$length = strlen((string)$i);
$adminUser = substr('oc_' . $username, 0, 16 - $length) . $i;
$i++;
// check if the current user actually has all required rights
$query = "SELECT user FROM mysql.user WHERE user=? AND Create_priv='Y' AND Grant_priv='Y' AND Create_user_priv='Y'";
$result = $connection->executeQuery($query, [$this->dbUser]);

if ($result) {
$data = $result->fetchAll();

if (count($data) === 1) {
//add prefix to the admin username to prevent collisions
$adminUser = substr('oc_' . $username, 0, 16);

$i = 1;
while (true) {
// check if proposed username already exists
$query = 'SELECT user FROM mysql.user WHERE user=?';
$result = $connection->executeQuery($query, [$adminUser]);

if ($result) {
$data = $result->fetchAll();
//new dbuser does not exist
if (count($data) === 0) {
//use the admin login data for the new database user
$this->dbUser = $adminUser;

//create a random password so we don't need to store the admin password in the config file
$this->dbPassword = $this->random->generate(30);

$this->createDBUser($connection);

break;
} else {
//repeat with different username
$length = strlen((string)$i);
$adminUser = substr('oc_' . $username, 0, 16 - $length) . $i;
$i++;
}
} else {
break;
}
}
} else {
break;
}
}
}
Expand Down