mysql.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Michael Göhler <somebody.here@gmx.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. *
  8. * @copyright Copyright (c) 2015, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Setup;
  25. use OC\DB\ConnectionFactory;
  26. use OCP\IDBConnection;
  27. class MySQL extends AbstractDatabase {
  28. public $dbprettyname = 'MySQL/MariaDB';
  29. public function setupDatabase($username) {
  30. //check if the database user has admin right
  31. $connection = $this->connect();
  32. $this->createSpecificUser($username, $connection);
  33. //create the database
  34. $this->createDatabase($connection);
  35. //fill the database if needed
  36. $query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?';
  37. $result = $connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']);
  38. $row = $result->fetch();
  39. if(!$result or $row[0]==0) {
  40. \OC_DB::createDbFromStructure($this->dbDefinitionFile);
  41. }
  42. }
  43. /**
  44. * @param \OC\DB\Connection $connection
  45. */
  46. private function createDatabase($connection) {
  47. try{
  48. $name = $this->dbName;
  49. $user = $this->dbUser;
  50. //we cant use OC_BD functions here because we need to connect as the administrative user.
  51. $query = "CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET utf8 COLLATE utf8_bin;";
  52. $connection->executeUpdate($query);
  53. //this query will fail if there aren't the right permissions, ignore the error
  54. $query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'";
  55. $connection->executeUpdate($query);
  56. } catch (\Exception $ex) {
  57. $this->logger->error('Database creation failed: {error}', [
  58. 'app' => 'mysql.setup',
  59. 'error' => $ex->getMessage()
  60. ]);
  61. }
  62. }
  63. /**
  64. * @param IDbConnection $connection
  65. * @throws \OC\DatabaseSetupException
  66. */
  67. private function createDBUser($connection) {
  68. $name = $this->dbUser;
  69. $password = $this->dbPassword;
  70. // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one,
  71. // the anonymous user would take precedence when there is one.
  72. $query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'";
  73. $connection->executeUpdate($query);
  74. $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'";
  75. $connection->executeUpdate($query);
  76. }
  77. /**
  78. * @return \OC\DB\Connection
  79. * @throws \OC\DatabaseSetupException
  80. */
  81. private function connect() {
  82. $type = 'mysql';
  83. $connectionParams = array(
  84. 'host' => $this->dbHost,
  85. 'user' => $this->dbUser,
  86. 'password' => $this->dbPassword,
  87. 'tablePrefix' => $this->tablePrefix,
  88. );
  89. $cf = new ConnectionFactory();
  90. return $cf->getConnection($type, $connectionParams);
  91. }
  92. /**
  93. * @param $username
  94. * @param IDBConnection $connection
  95. * @return array
  96. */
  97. private function createSpecificUser($username, $connection) {
  98. try {
  99. //user already specified in config
  100. $oldUser = $this->config->getSystemValue('dbuser', false);
  101. //we don't have a dbuser specified in config
  102. if ($this->dbUser !== $oldUser) {
  103. //add prefix to the admin username to prevent collisions
  104. $adminUser = substr('oc_' . $username, 0, 16);
  105. $i = 1;
  106. while (true) {
  107. //this should be enough to check for admin rights in mysql
  108. $query = 'SELECT user FROM mysql.user WHERE user=?';
  109. $result = $connection->executeQuery($query, [$adminUser]);
  110. //current dbuser has admin rights
  111. if ($result) {
  112. $data = $result->fetchAll();
  113. //new dbuser does not exist
  114. if (count($data) === 0) {
  115. //use the admin login data for the new database user
  116. $this->dbUser = $adminUser;
  117. //create a random password so we don't need to store the admin password in the config file
  118. $this->dbPassword = $this->random->getMediumStrengthGenerator()->generate(30);
  119. $this->createDBUser($connection);
  120. break;
  121. } else {
  122. //repeat with different username
  123. $length = strlen((string)$i);
  124. $adminUser = substr('oc_' . $username, 0, 16 - $length) . $i;
  125. $i++;
  126. }
  127. } else {
  128. break;
  129. }
  130. };
  131. }
  132. } catch (\Exception $ex) {
  133. $this->logger->error('Specific user creation failed: {error}', [
  134. 'app' => 'mysql.setup',
  135. 'error' => $ex->getMessage()
  136. ]);
  137. }
  138. $this->config->setSystemValues([
  139. 'dbuser' => $this->dbUser,
  140. 'dbpassword' => $this->dbPassword,
  141. ]);
  142. }
  143. }