2013-03-29 11:28:48 -04:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2015-03-26 06:44:34 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-03-26 06:44:34 -04:00
|
|
|
*/
|
2013-03-29 11:28:48 -04:00
|
|
|
namespace OC\Setup;
|
|
|
|
|
|
2023-11-23 04:22:34 -05:00
|
|
|
use Doctrine\DBAL\Platforms\MySQL80Platform;
|
2021-01-03 09:28:31 -05:00
|
|
|
use OC\DB\ConnectionAdapter;
|
2017-02-03 10:24:53 -05:00
|
|
|
use OC\DB\MySqlTools;
|
2015-07-29 18:04:30 -04:00
|
|
|
use OCP\IDBConnection;
|
2021-01-18 06:03:52 -05:00
|
|
|
use OCP\Security\ISecureRandom;
|
2015-07-29 12:09:23 -04:00
|
|
|
|
2013-04-02 16:01:23 -04:00
|
|
|
class MySQL extends AbstractDatabase {
|
2014-02-07 10:09:31 -05:00
|
|
|
public $dbprettyname = 'MySQL/MariaDB';
|
2013-04-03 11:52:18 -04:00
|
|
|
|
2013-04-02 16:01:23 -04:00
|
|
|
public function setupDatabase($username) {
|
2013-03-29 11:28:48 -04:00
|
|
|
//check if the database user has admin right
|
2016-12-14 07:52:04 -05:00
|
|
|
$connection = $this->connect(['dbname' => null]);
|
2013-03-29 11:28:48 -04:00
|
|
|
|
2017-02-03 09:38:48 -05:00
|
|
|
// detect mb4
|
2017-03-21 08:08:14 -04:00
|
|
|
$tools = new MySqlTools();
|
2021-01-03 09:28:31 -05:00
|
|
|
if ($tools->supports4ByteCharset(new ConnectionAdapter($connection))) {
|
2017-03-21 08:08:14 -04:00
|
|
|
$this->config->setValue('mysql.utf8mb4', true);
|
2017-05-31 20:12:25 -04:00
|
|
|
$connection = $this->connect(['dbname' => null]);
|
2017-02-03 09:38:48 -05:00
|
|
|
}
|
|
|
|
|
|
2023-01-29 09:54:39 -05:00
|
|
|
if ($this->tryCreateDbUser) {
|
|
|
|
|
$this->createSpecificUser($username, new ConnectionAdapter($connection));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->config->setValues([
|
|
|
|
|
'dbuser' => $this->dbUser,
|
|
|
|
|
'dbpassword' => $this->dbPassword,
|
|
|
|
|
]);
|
2013-03-29 11:28:48 -04:00
|
|
|
|
2014-07-29 04:52:51 -04:00
|
|
|
//create the database
|
|
|
|
|
$this->createDatabase($connection);
|
|
|
|
|
|
2013-03-29 11:28:48 -04:00
|
|
|
//fill the database if needed
|
2020-10-05 09:12:57 -04:00
|
|
|
$query = 'select count(*) from information_schema.tables where table_schema=? AND table_name = ?';
|
2017-07-18 08:20:29 -04:00
|
|
|
$connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']);
|
2020-02-05 05:08:18 -05:00
|
|
|
|
|
|
|
|
$connection->close();
|
|
|
|
|
$connection = $this->connect();
|
|
|
|
|
try {
|
|
|
|
|
$connection->connect();
|
|
|
|
|
} catch (\Exception $e) {
|
2021-04-22 09:22:50 -04:00
|
|
|
$this->logger->error($e->getMessage(), [
|
|
|
|
|
'exception' => $e,
|
|
|
|
|
]);
|
2024-02-13 08:37:09 -05:00
|
|
|
throw new \OC\DatabaseSetupException($this->trans->t('MySQL Login and/or password not valid'),
|
2021-01-07 15:04:11 -05:00
|
|
|
$this->trans->t('You need to enter details of an existing account.'), 0, $e);
|
2020-02-05 05:08:18 -05:00
|
|
|
}
|
2013-03-29 11:28:48 -04:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 12:09:23 -04:00
|
|
|
/**
|
|
|
|
|
* @param \OC\DB\Connection $connection
|
|
|
|
|
*/
|
2013-04-02 16:01:23 -04:00
|
|
|
private function createDatabase($connection) {
|
2020-04-10 08:19:56 -04:00
|
|
|
try {
|
2015-07-29 18:04:30 -04:00
|
|
|
$name = $this->dbName;
|
|
|
|
|
$user = $this->dbUser;
|
2015-07-30 07:57:04 -04:00
|
|
|
//we can't use OC_DB functions here because we need to connect as the administrative user.
|
2017-03-17 18:37:48 -04:00
|
|
|
$characterSet = $this->config->getValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8';
|
2022-06-14 09:12:28 -04:00
|
|
|
$query = "CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET $characterSet COLLATE {$characterSet}_bin;";
|
2015-07-29 18:04:30 -04:00
|
|
|
$connection->executeUpdate($query);
|
2016-07-26 06:38:14 -04:00
|
|
|
} catch (\Exception $ex) {
|
2021-04-16 08:26:43 -04:00
|
|
|
$this->logger->error('Database creation failed.', [
|
|
|
|
|
'exception' => $ex,
|
2016-07-26 06:38:14 -04:00
|
|
|
'app' => 'mysql.setup',
|
|
|
|
|
]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-07-29 18:04:30 -04:00
|
|
|
|
2016-07-26 06:38:14 -04:00
|
|
|
try {
|
2015-07-29 18:04:30 -04:00
|
|
|
//this query will fail if there aren't the right permissions, ignore the error
|
2020-10-05 09:12:57 -04:00
|
|
|
$query = "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `$name` . * TO '$user'";
|
2015-07-29 18:04:30 -04:00
|
|
|
$connection->executeUpdate($query);
|
|
|
|
|
} catch (\Exception $ex) {
|
2021-04-16 08:26:43 -04:00
|
|
|
$this->logger->debug('Could not automatically grant privileges, this can be ignored if database user already had privileges.', [
|
|
|
|
|
'exception' => $ex,
|
2015-07-29 18:04:30 -04:00
|
|
|
'app' => 'mysql.setup',
|
|
|
|
|
]);
|
|
|
|
|
}
|
2013-03-29 11:28:48 -04:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 12:09:23 -04:00
|
|
|
/**
|
2016-02-08 10:43:39 -05:00
|
|
|
* @param IDBConnection $connection
|
2015-07-29 12:09:23 -04:00
|
|
|
* @throws \OC\DatabaseSetupException
|
|
|
|
|
*/
|
2013-04-02 16:01:23 -04:00
|
|
|
private function createDBUser($connection) {
|
2020-04-10 08:19:56 -04:00
|
|
|
try {
|
2016-10-31 07:02:22 -04:00
|
|
|
$name = $this->dbUser;
|
|
|
|
|
$password = $this->dbPassword;
|
|
|
|
|
// we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one,
|
|
|
|
|
// the anonymous user would take precedence when there is one.
|
2020-02-05 04:05:11 -05:00
|
|
|
|
|
|
|
|
if ($connection->getDatabasePlatform() instanceof Mysql80Platform) {
|
|
|
|
|
$query = "CREATE USER '$name'@'localhost' IDENTIFIED WITH mysql_native_password BY '$password'";
|
|
|
|
|
$connection->executeUpdate($query);
|
|
|
|
|
$query = "CREATE USER '$name'@'%' IDENTIFIED WITH mysql_native_password BY '$password'";
|
|
|
|
|
$connection->executeUpdate($query);
|
|
|
|
|
} else {
|
|
|
|
|
$query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'";
|
|
|
|
|
$connection->executeUpdate($query);
|
|
|
|
|
$query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'";
|
|
|
|
|
$connection->executeUpdate($query);
|
|
|
|
|
}
|
2020-04-10 08:19:56 -04:00
|
|
|
} catch (\Exception $ex) {
|
2022-01-12 14:44:38 -05:00
|
|
|
$this->logger->error('Database user creation failed.', [
|
2021-04-16 08:26:43 -04:00
|
|
|
'exception' => $ex,
|
2018-01-17 09:21:56 -05:00
|
|
|
'app' => 'mysql.setup',
|
|
|
|
|
]);
|
2022-08-12 03:43:38 -04:00
|
|
|
throw $ex;
|
2016-10-31 07:02:22 -04:00
|
|
|
}
|
2015-07-29 12:09:23 -04:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 18:04:30 -04:00
|
|
|
/**
|
|
|
|
|
* @param $username
|
|
|
|
|
* @param IDBConnection $connection
|
|
|
|
|
*/
|
2022-05-18 15:50:47 -04:00
|
|
|
private function createSpecificUser($username, $connection): void {
|
2022-08-12 03:43:38 -04:00
|
|
|
$rootUser = $this->dbUser;
|
|
|
|
|
$rootPassword = $this->dbPassword;
|
|
|
|
|
|
2022-08-12 04:03:19 -04:00
|
|
|
//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)
|
2023-01-29 09:54:39 -05:00
|
|
|
. $this->random->generate(2, $saveSymbols);
|
2022-08-12 04:03:19 -04:00
|
|
|
$this->dbPassword = str_shuffle($password);
|
|
|
|
|
|
2015-07-29 18:04:30 -04:00
|
|
|
try {
|
|
|
|
|
//user already specified in config
|
2017-03-17 18:37:48 -04:00
|
|
|
$oldUser = $this->config->getValue('dbuser', false);
|
2015-07-29 18:04:30 -04:00
|
|
|
|
|
|
|
|
//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
|
2021-01-03 09:28:31 -05:00
|
|
|
$data = $result->fetchAll();
|
|
|
|
|
$result->closeCursor();
|
|
|
|
|
//new dbuser does not exist
|
|
|
|
|
if (count($data) === 0) {
|
|
|
|
|
//use the admin login data for the new database user
|
|
|
|
|
$this->dbUser = $adminUser;
|
|
|
|
|
$this->createDBUser($connection);
|
|
|
|
|
|
2015-07-29 18:04:30 -04:00
|
|
|
break;
|
2021-01-03 09:28:31 -05:00
|
|
|
} else {
|
|
|
|
|
//repeat with different username
|
|
|
|
|
$length = strlen((string)$i);
|
|
|
|
|
$adminUser = substr('oc_' . $username, 0, 16 - $length) . $i;
|
|
|
|
|
$i++;
|
2015-07-29 18:04:30 -04:00
|
|
|
}
|
2018-01-26 17:46:40 -05:00
|
|
|
}
|
2022-09-20 02:39:25 -04:00
|
|
|
} else {
|
|
|
|
|
// Reuse existing password if a database config is already present
|
|
|
|
|
$this->dbPassword = $rootPassword;
|
2015-07-29 18:04:30 -04:00
|
|
|
}
|
|
|
|
|
} catch (\Exception $ex) {
|
2021-04-22 09:22:50 -04:00
|
|
|
$this->logger->info('Can not create a new MySQL user, will continue with the provided user.', [
|
|
|
|
|
'exception' => $ex,
|
2015-07-29 18:04:30 -04:00
|
|
|
'app' => 'mysql.setup',
|
|
|
|
|
]);
|
2022-08-12 03:43:38 -04:00
|
|
|
// Restore the original credentials
|
|
|
|
|
$this->dbUser = $rootUser;
|
|
|
|
|
$this->dbPassword = $rootPassword;
|
2015-07-29 18:04:30 -04:00
|
|
|
}
|
|
|
|
|
}
|
2013-03-29 11:28:48 -04:00
|
|
|
}
|