postgresql.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Christopher Schäpers <kondou@ts.unde.re>
  5. * @author eduardo <eduardo@vnexu.net>
  6. * @author Joas Schilling <nickvergessen@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. class PostgreSQL extends AbstractDatabase {
  26. public $dbprettyname = 'PostgreSQL';
  27. public function setupDatabase($username) {
  28. $e_host = addslashes($this->dbHost);
  29. $e_user = addslashes($this->dbUser);
  30. $e_password = addslashes($this->dbPassword);
  31. // Fix database with port connection
  32. if(strpos($e_host, ':')) {
  33. list($e_host, $port)=explode(':', $e_host, 2);
  34. } else {
  35. $port=false;
  36. }
  37. //check if the database user has admin rights
  38. $connection_string = "host='$e_host' dbname=postgres user='$e_user' port='$port' password='$e_password'";
  39. $connection = @pg_connect($connection_string);
  40. if(!$connection) {
  41. // Try if we can connect to the DB with the specified name
  42. $e_dbname = addslashes($this->dbName);
  43. $connection_string = "host='$e_host' dbname='$e_dbname' user='$e_user' port='$port' password='$e_password'";
  44. $connection = @pg_connect($connection_string);
  45. if(!$connection)
  46. throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'),
  47. $this->trans->t('You need to enter either an existing account or the administrator.'));
  48. }
  49. $e_user = pg_escape_string($this->dbUser);
  50. //check for roles creation rights in postgresql
  51. $query="SELECT 1 FROM pg_roles WHERE rolcreaterole=TRUE AND rolname='$e_user'";
  52. $result = pg_query($connection, $query);
  53. if($result and pg_num_rows($result) > 0) {
  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_'.$username;
  57. //create a new password so we don't need to store the admin config in the config file
  58. $this->dbPassword=\OC_Util::generateRandomBytes(30);
  59. $this->createDBUser($connection);
  60. }
  61. \OC_Config::setValues([
  62. 'dbuser' => $this->dbUser,
  63. 'dbpassword' => $this->dbPassword,
  64. ]);
  65. //create the database
  66. $this->createDatabase($connection);
  67. // the connection to dbname=postgres is not needed anymore
  68. pg_close($connection);
  69. // connect to the ownCloud database (dbname=$this->dbname) and check if it needs to be filled
  70. $this->dbUser = \OC_Config::getValue('dbuser');
  71. $this->dbPassword = \OC_Config::getValue('dbpassword');
  72. $e_host = addslashes($this->dbHost);
  73. $e_dbname = addslashes($this->dbName);
  74. $e_user = addslashes($this->dbUser);
  75. $e_password = addslashes($this->dbPassword);
  76. // Fix database with port connection
  77. if(strpos($e_host, ':')) {
  78. list($e_host, $port)=explode(':', $e_host, 2);
  79. } else {
  80. $port=false;
  81. }
  82. $connection_string = "host='$e_host' dbname='$e_dbname' user='$e_user' port='$port' password='$e_password'";
  83. $connection = @pg_connect($connection_string);
  84. if(!$connection) {
  85. throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'),
  86. $this->trans->t('You need to enter either an existing account or the administrator.'));
  87. }
  88. $query = "select count(*) FROM pg_class WHERE relname='".$this->tablePrefix."users' limit 1";
  89. $result = pg_query($connection, $query);
  90. if($result) {
  91. $row = pg_fetch_row($result);
  92. }
  93. if(!$result or $row[0]==0) {
  94. \OC_DB::createDbFromStructure($this->dbDefinitionFile);
  95. }
  96. }
  97. private function createDatabase($connection) {
  98. //we cant use OC_BD functions here because we need to connect as the administrative user.
  99. $e_name = pg_escape_string($this->dbName);
  100. $e_user = pg_escape_string($this->dbUser);
  101. $query = "select datname from pg_database where datname = '$e_name'";
  102. $result = pg_query($connection, $query);
  103. if(!$result) {
  104. $entry = $this->trans->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
  105. $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
  106. \OCP\Util::writeLog('setup.pg', $entry, \OCP\Util::WARN);
  107. }
  108. if(! pg_fetch_row($result)) {
  109. //The database does not exists... let's create it
  110. $query = "CREATE DATABASE \"$e_name\" OWNER \"$e_user\"";
  111. $result = pg_query($connection, $query);
  112. if(!$result) {
  113. $entry = $this->trans->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
  114. $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
  115. \OCP\Util::writeLog('setup.pg', $entry, \OCP\Util::WARN);
  116. }
  117. else {
  118. $query = "REVOKE ALL PRIVILEGES ON DATABASE \"$e_name\" FROM PUBLIC";
  119. pg_query($connection, $query);
  120. }
  121. }
  122. }
  123. private function createDBUser($connection) {
  124. $e_name = pg_escape_string($this->dbUser);
  125. $e_password = pg_escape_string($this->dbPassword);
  126. $query = "select * from pg_roles where rolname='$e_name';";
  127. $result = pg_query($connection, $query);
  128. if(!$result) {
  129. $entry = $this->trans->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
  130. $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
  131. \OCP\Util::writeLog('setup.pg', $entry, \OCP\Util::WARN);
  132. }
  133. if(! pg_fetch_row($result)) {
  134. //user does not exists let's create it :)
  135. $query = "CREATE USER \"$e_name\" CREATEDB PASSWORD '$e_password';";
  136. $result = pg_query($connection, $query);
  137. if(!$result) {
  138. $entry = $this->trans->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
  139. $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
  140. \OCP\Util::writeLog('setup.pg', $entry, \OCP\Util::WARN);
  141. }
  142. }
  143. else { // change password of the existing role
  144. $query = "ALTER ROLE \"$e_name\" WITH PASSWORD '$e_password';";
  145. $result = pg_query($connection, $query);
  146. if(!$result) {
  147. $entry = $this->trans->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
  148. $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
  149. \OCP\Util::writeLog('setup.pg', $entry, \OCP\Util::WARN);
  150. }
  151. }
  152. }
  153. }