Skip to content

Commit

Permalink
Merge pull request #33515 from nextcloud/backport/33513/stable23
Browse files Browse the repository at this point in the history
[stable23] Recover installation when creating the database user fails and improve password strength
  • Loading branch information
blizzz authored Aug 30, 2022
2 parents 518378b + 2a206cd commit 1e80b33
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/private/Setup/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ private function createDBUser($connection) {
'exception' => $ex,
'app' => 'mysql.setup',
]);
throw $ex;
}
}

Expand All @@ -138,6 +139,19 @@ private function createDBUser($connection) {
* @return array
*/
private function createSpecificUser($username, $connection) {
$rootUser = $this->dbUser;
$rootPassword = $this->dbPassword;

//create a random password so we don't need to store the admin password in the config file
$saveSymbols = str_replace(['\"', '\\', '\'', '`'], '', ISecureRandom::CHAR_SYMBOLS);
$password = $this->random->generate(22, ISecureRandom::CHAR_ALPHANUMERIC . $saveSymbols)
. $this->random->generate(2, ISecureRandom::CHAR_UPPER)
. $this->random->generate(2, ISecureRandom::CHAR_LOWER)
. $this->random->generate(2, ISecureRandom::CHAR_DIGITS)
. $this->random->generate(2, $saveSymbols)
;
$this->dbPassword = str_shuffle($password);

try {
//user already specified in config
$oldUser = $this->config->getValue('dbuser', false);
Expand All @@ -160,10 +174,6 @@ private function createSpecificUser($username, $connection) {
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, ISecureRandom::CHAR_ALPHANUMERIC);

$this->createDBUser($connection);

break;
Expand All @@ -180,6 +190,9 @@ private function createSpecificUser($username, $connection) {
'exception' => $ex,
'app' => 'mysql.setup',
]);
// Restore the original credentials
$this->dbUser = $rootUser;
$this->dbPassword = $rootPassword;
}

$this->config->setValues([
Expand Down

0 comments on commit 1e80b33

Please sign in to comment.