PostgreSQL.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author eduardo <eduardo@vnexu.net>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Setup;
  29. use OC\DatabaseException;
  30. use OC\DB\QueryBuilder\Literal;
  31. use OCP\IDBConnection;
  32. class PostgreSQL extends AbstractDatabase {
  33. public $dbprettyname = 'PostgreSQL';
  34. public function setupDatabase($username) {
  35. $connection = $this->connect([
  36. 'dbname' => 'postgres'
  37. ]);
  38. //check for roles creation rights in postgresql
  39. $builder = $connection->getQueryBuilder();
  40. $builder->automaticTablePrefix(false);
  41. $query = $builder
  42. ->select('rolname')
  43. ->from('pg_roles')
  44. ->where($builder->expr()->eq('rolcreaterole', new Literal('TRUE')))
  45. ->andWhere($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser)));
  46. try {
  47. $result = $query->execute();
  48. $canCreateRoles = $result->rowCount() > 0;
  49. } catch (DatabaseException $e) {
  50. $canCreateRoles = false;
  51. }
  52. if($canCreateRoles) {
  53. //use the admin login data for the new database user
  54. //add prefix to the postgresql user name to prevent collisions
  55. $this->dbUser='oc_'.$username;
  56. //create a new password so we don't need to store the admin config in the config file
  57. $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS);
  58. $this->createDBUser($connection);
  59. }
  60. $systemConfig = $this->config->getSystemConfig();
  61. $systemConfig->setValues([
  62. 'dbuser' => $this->dbUser,
  63. 'dbpassword' => $this->dbPassword,
  64. ]);
  65. //create the database
  66. $this->createDatabase($connection);
  67. $query = $connection->prepare("select count(*) FROM pg_class WHERE relname=? limit 1");
  68. $query->execute([$this->tablePrefix . "users"]);
  69. $tablesSetup = $query->fetchColumn() > 0;
  70. // the connection to dbname=postgres is not needed anymore
  71. $connection->close();
  72. // connect to the ownCloud database (dbname=$this->dbname) and check if it needs to be filled
  73. $this->dbUser = $systemConfig->getValue('dbuser');
  74. $this->dbPassword = $systemConfig->getValue('dbpassword');
  75. $connection = $this->connect();
  76. try {
  77. $connection->connect();
  78. } catch (\Exception $e) {
  79. $this->logger->logException($e);
  80. throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'),
  81. $this->trans->t('You need to enter either an existing account or the administrator.'));
  82. }
  83. if(!$tablesSetup) {
  84. \OC_DB::createDbFromStructure($this->dbDefinitionFile);
  85. }
  86. }
  87. private function createDatabase(IDBConnection $connection) {
  88. if(!$this->databaseExists($connection)) {
  89. //The database does not exists... let's create it
  90. $query = $connection->prepare("CREATE DATABASE " . addslashes($this->dbName) . " OWNER " . addslashes($this->dbUser));
  91. try {
  92. $query->execute();
  93. } catch (DatabaseException $e) {
  94. $this->logger->error('Error while trying to create database');
  95. $this->logger->logException($e);
  96. }
  97. } else {
  98. $query = $connection->prepare("REVOKE ALL PRIVILEGES ON DATABASE " . addslashes($this->dbName) . " FROM PUBLIC");
  99. try {
  100. $query->execute();
  101. } catch (DatabaseException $e) {
  102. $this->logger->error('Error while trying to restrict database permissions');
  103. $this->logger->logException($e);
  104. }
  105. }
  106. }
  107. private function userExists(IDBConnection $connection) {
  108. $builder = $connection->getQueryBuilder();
  109. $builder->automaticTablePrefix(false);
  110. $query = $builder->select('*')
  111. ->from('pg_roles')
  112. ->where($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser)));
  113. $result = $query->execute();
  114. return $result->rowCount() > 0;
  115. }
  116. private function databaseExists(IDBConnection $connection) {
  117. $builder = $connection->getQueryBuilder();
  118. $builder->automaticTablePrefix(false);
  119. $query = $builder->select('datname')
  120. ->from('pg_database')
  121. ->where($builder->expr()->eq('datname', $builder->createNamedParameter($this->dbName)));
  122. $result = $query->execute();
  123. return $result->rowCount() > 0;
  124. }
  125. private function createDBUser(IDBConnection $connection) {
  126. try {
  127. if ($this->userExists($connection)) {
  128. // change the password
  129. $query = $connection->prepare("ALTER ROLE " . addslashes($this->dbUser) . " WITH CREATEDB PASSWORD '" . addslashes($this->dbPassword) . "'");
  130. } else {
  131. // create the user
  132. $query = $connection->prepare("CREATE USER " . addslashes($this->dbUser) . " CREATEDB PASSWORD '" . addslashes($this->dbPassword) . "'");
  133. }
  134. $query->execute();
  135. } catch (DatabaseException $e) {
  136. $this->logger->error('Error while trying to create database user');
  137. $this->logger->logException($e);
  138. }
  139. }
  140. }