2013-03-29 19:28:48 +04:00
|
|
|
<?php
|
2015-03-26 13:44:34 +03:00
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author eduardo <eduardo@vnexu.net>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
2013-03-29 19:28:48 +04:00
|
|
|
namespace OC\Setup;
|
|
|
|
|
2016-07-12 14:50:54 +03:00
|
|
|
use OC\DatabaseException;
|
|
|
|
use OC\DB\QueryBuilder\Literal;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
|
2013-04-03 00:01:23 +04:00
|
|
|
class PostgreSQL extends AbstractDatabase {
|
2013-04-03 19:52:18 +04:00
|
|
|
public $dbprettyname = 'PostgreSQL';
|
|
|
|
|
2017-07-20 23:48:13 +03:00
|
|
|
/**
|
|
|
|
* @param string $username
|
|
|
|
* @throws \OC\DatabaseSetupException
|
|
|
|
* @suppress SqlInjectionChecker
|
|
|
|
*/
|
2013-04-03 00:01:23 +04:00
|
|
|
public function setupDatabase($username) {
|
2016-07-12 14:50:54 +03:00
|
|
|
try {
|
2016-07-21 13:44:02 +03:00
|
|
|
$connection = $this->connect([
|
|
|
|
'dbname' => 'postgres'
|
|
|
|
]);
|
|
|
|
//check for roles creation rights in postgresql
|
|
|
|
$builder = $connection->getQueryBuilder();
|
|
|
|
$builder->automaticTablePrefix(false);
|
|
|
|
$query = $builder
|
|
|
|
->select('rolname')
|
|
|
|
->from('pg_roles')
|
|
|
|
->where($builder->expr()->eq('rolcreaterole', new Literal('TRUE')))
|
|
|
|
->andWhere($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser)));
|
2014-01-05 01:23:25 +04:00
|
|
|
|
2016-07-21 13:44:02 +03:00
|
|
|
try {
|
|
|
|
$result = $query->execute();
|
|
|
|
$canCreateRoles = $result->rowCount() > 0;
|
|
|
|
} catch (DatabaseException $e) {
|
|
|
|
$canCreateRoles = false;
|
|
|
|
}
|
2013-03-29 19:28:48 +04:00
|
|
|
|
2016-07-21 13:44:02 +03:00
|
|
|
if ($canCreateRoles) {
|
|
|
|
//use the admin login data for the new database user
|
2013-03-29 19:28:48 +04:00
|
|
|
|
2016-07-21 13:44:02 +03:00
|
|
|
//add prefix to the postgresql user name to prevent collisions
|
2016-12-09 18:41:01 +03:00
|
|
|
$this->dbUser = 'oc_' . strtolower($username);
|
2016-07-21 13:44:02 +03:00
|
|
|
//create a new password so we don't need to store the admin config in the config file
|
|
|
|
$this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_DIGITS);
|
2013-03-29 19:28:48 +04:00
|
|
|
|
2016-07-21 13:44:02 +03:00
|
|
|
$this->createDBUser($connection);
|
|
|
|
}
|
2015-01-23 13:13:47 +03:00
|
|
|
|
2017-03-18 01:37:48 +03:00
|
|
|
$this->config->setValues([
|
2016-07-21 13:44:02 +03:00
|
|
|
'dbuser' => $this->dbUser,
|
|
|
|
'dbpassword' => $this->dbPassword,
|
|
|
|
]);
|
2013-03-29 19:28:48 +04:00
|
|
|
|
2016-07-21 13:44:02 +03:00
|
|
|
//create the database
|
|
|
|
$this->createDatabase($connection);
|
|
|
|
// the connection to dbname=postgres is not needed anymore
|
|
|
|
$connection->close();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->logException($e);
|
|
|
|
$this->logger->warning('Error trying to connect as "postgres", assuming database is setup and tables need to be created');
|
2017-03-18 01:37:48 +03:00
|
|
|
$this->config->setValues([
|
2016-07-21 13:44:02 +03:00
|
|
|
'dbuser' => $this->dbUser,
|
|
|
|
'dbpassword' => $this->dbPassword,
|
|
|
|
]);
|
|
|
|
}
|
2013-03-29 19:28:48 +04:00
|
|
|
|
2019-03-06 16:51:17 +03:00
|
|
|
// connect to the database (dbname=$this->dbname) and check if it needs to be filled
|
2017-03-18 01:37:48 +03:00
|
|
|
$this->dbUser = $this->config->getValue('dbuser');
|
|
|
|
$this->dbPassword = $this->config->getValue('dbpassword');
|
2016-07-12 14:50:54 +03:00
|
|
|
$connection = $this->connect();
|
|
|
|
try {
|
|
|
|
$connection->connect();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->logException($e);
|
2014-11-26 14:30:07 +03:00
|
|
|
throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'),
|
2017-04-18 10:53:02 +03:00
|
|
|
$this->trans->t('You need to enter details of an existing account.'));
|
2013-03-29 19:28:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 14:50:54 +03:00
|
|
|
private function createDatabase(IDBConnection $connection) {
|
2016-07-21 13:44:02 +03:00
|
|
|
if (!$this->databaseExists($connection)) {
|
2013-03-29 19:28:48 +04:00
|
|
|
//The database does not exists... let's create it
|
2016-12-09 17:36:14 +03:00
|
|
|
$query = $connection->prepare("CREATE DATABASE " . addslashes($this->dbName) . " OWNER " . addslashes($this->dbUser));
|
2016-07-12 14:50:54 +03:00
|
|
|
try {
|
|
|
|
$query->execute();
|
|
|
|
} catch (DatabaseException $e) {
|
|
|
|
$this->logger->error('Error while trying to create database');
|
|
|
|
$this->logger->logException($e);
|
2013-03-29 19:28:48 +04:00
|
|
|
}
|
2016-07-12 14:50:54 +03:00
|
|
|
} else {
|
2016-12-09 17:36:14 +03:00
|
|
|
$query = $connection->prepare("REVOKE ALL PRIVILEGES ON DATABASE " . addslashes($this->dbName) . " FROM PUBLIC");
|
2016-07-12 14:50:54 +03:00
|
|
|
try {
|
|
|
|
$query->execute();
|
|
|
|
} catch (DatabaseException $e) {
|
|
|
|
$this->logger->error('Error while trying to restrict database permissions');
|
|
|
|
$this->logger->logException($e);
|
2013-03-29 19:28:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 14:50:54 +03:00
|
|
|
private function userExists(IDBConnection $connection) {
|
|
|
|
$builder = $connection->getQueryBuilder();
|
|
|
|
$builder->automaticTablePrefix(false);
|
|
|
|
$query = $builder->select('*')
|
|
|
|
->from('pg_roles')
|
|
|
|
->where($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser)));
|
|
|
|
$result = $query->execute();
|
|
|
|
return $result->rowCount() > 0;
|
|
|
|
}
|
2013-03-29 19:28:48 +04:00
|
|
|
|
2016-07-12 14:50:54 +03:00
|
|
|
private function databaseExists(IDBConnection $connection) {
|
|
|
|
$builder = $connection->getQueryBuilder();
|
|
|
|
$builder->automaticTablePrefix(false);
|
|
|
|
$query = $builder->select('datname')
|
|
|
|
->from('pg_database')
|
|
|
|
->where($builder->expr()->eq('datname', $builder->createNamedParameter($this->dbName)));
|
|
|
|
$result = $query->execute();
|
|
|
|
return $result->rowCount() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createDBUser(IDBConnection $connection) {
|
2016-12-06 21:04:57 +03:00
|
|
|
$dbUser = $this->dbUser;
|
2016-07-12 14:50:54 +03:00
|
|
|
try {
|
2016-12-06 21:04:57 +03:00
|
|
|
$i = 1;
|
|
|
|
while ($this->userExists($connection)) {
|
|
|
|
$i++;
|
|
|
|
$this->dbUser = $dbUser . $i;
|
2018-01-27 01:46:40 +03:00
|
|
|
}
|
2016-12-06 21:04:57 +03:00
|
|
|
|
|
|
|
// create the user
|
|
|
|
$query = $connection->prepare("CREATE USER " . addslashes($this->dbUser) . " CREATEDB PASSWORD '" . addslashes($this->dbPassword) . "'");
|
2016-07-12 14:50:54 +03:00
|
|
|
$query->execute();
|
2019-04-21 20:50:31 +03:00
|
|
|
$query = $connection->prepare('GRANT CONNECT ON DATABASE ' . $this->dbName . ' TO '.addslashes($this->dbUser));
|
|
|
|
$query->execute();
|
2016-07-12 14:50:54 +03:00
|
|
|
} catch (DatabaseException $e) {
|
|
|
|
$this->logger->error('Error while trying to create database user');
|
|
|
|
$this->logger->logException($e);
|
2013-03-29 19:28:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|