1
0

MySQL.php 5.5 KB

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