You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PostgreSQL.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. try {
  36. $connection = $this->connect([
  37. 'dbname' => 'postgres'
  38. ]);
  39. //check for roles creation rights in postgresql
  40. $builder = $connection->getQueryBuilder();
  41. $builder->automaticTablePrefix(false);
  42. $query = $builder
  43. ->select('rolname')
  44. ->from('pg_roles')
  45. ->where($builder->expr()->eq('rolcreaterole', new Literal('TRUE')))
  46. ->andWhere($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser)));
  47. try {
  48. $result = $query->execute();
  49. $canCreateRoles = $result->rowCount() > 0;
  50. } catch (DatabaseException $e) {
  51. $canCreateRoles = false;
  52. }
  53. if ($canCreateRoles) {
  54. //use the admin login data for the new database user
  55. //add prefix to the postgresql user name to prevent collisions
  56. $this->dbUser = 'oc_' . strtolower($username);
  57. //create a new password so we don't need to store the admin config in the config file
  58. $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_DIGITS);
  59. $this->createDBUser($connection);
  60. }
  61. $this->config->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. } catch (\Exception $e) {
  73. $this->logger->logException($e);
  74. $this->logger->warning('Error trying to connect as "postgres", assuming database is setup and tables need to be created');
  75. $tablesSetup = false;
  76. $this->config->setValues([
  77. 'dbuser' => $this->dbUser,
  78. 'dbpassword' => $this->dbPassword,
  79. ]);
  80. }
  81. // connect to the ownCloud database (dbname=$this->dbname) and check if it needs to be filled
  82. $this->dbUser = $this->config->getValue('dbuser');
  83. $this->dbPassword = $this->config->getValue('dbpassword');
  84. $connection = $this->connect();
  85. try {
  86. $connection->connect();
  87. } catch (\Exception $e) {
  88. $this->logger->logException($e);
  89. throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'),
  90. $this->trans->t('You need to enter either an existing account or the administrator.'));
  91. }
  92. if (!$tablesSetup) {
  93. \OC_DB::createDbFromStructure($this->dbDefinitionFile);
  94. }
  95. }
  96. private function createDatabase(IDBConnection $connection) {
  97. if (!$this->databaseExists($connection)) {
  98. //The database does not exists... let's create it
  99. $query = $connection->prepare("CREATE DATABASE " . addslashes($this->dbName) . " OWNER " . addslashes($this->dbUser));
  100. try {
  101. $query->execute();
  102. } catch (DatabaseException $e) {
  103. $this->logger->error('Error while trying to create database');
  104. $this->logger->logException($e);
  105. }
  106. } else {
  107. $query = $connection->prepare("REVOKE ALL PRIVILEGES ON DATABASE " . addslashes($this->dbName) . " FROM PUBLIC");
  108. try {
  109. $query->execute();
  110. } catch (DatabaseException $e) {
  111. $this->logger->error('Error while trying to restrict database permissions');
  112. $this->logger->logException($e);
  113. }
  114. }
  115. }
  116. private function userExists(IDBConnection $connection) {
  117. $builder = $connection->getQueryBuilder();
  118. $builder->automaticTablePrefix(false);
  119. $query = $builder->select('*')
  120. ->from('pg_roles')
  121. ->where($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser)));
  122. $result = $query->execute();
  123. return $result->rowCount() > 0;
  124. }
  125. private function databaseExists(IDBConnection $connection) {
  126. $builder = $connection->getQueryBuilder();
  127. $builder->automaticTablePrefix(false);
  128. $query = $builder->select('datname')
  129. ->from('pg_database')
  130. ->where($builder->expr()->eq('datname', $builder->createNamedParameter($this->dbName)));
  131. $result = $query->execute();
  132. return $result->rowCount() > 0;
  133. }
  134. private function createDBUser(IDBConnection $connection) {
  135. $dbUser = $this->dbUser;
  136. try {
  137. $i = 1;
  138. while ($this->userExists($connection)) {
  139. $i++;
  140. $this->dbUser = $dbUser . $i;
  141. };
  142. // create the user
  143. $query = $connection->prepare("CREATE USER " . addslashes($this->dbUser) . " CREATEDB PASSWORD '" . addslashes($this->dbPassword) . "'");
  144. $query->execute();
  145. } catch (DatabaseException $e) {
  146. $this->logger->error('Error while trying to create database user');
  147. $this->logger->logException($e);
  148. }
  149. }
  150. }