public function setupDatabase($username) { $datadir = \OC_Config::getValue('datadirectory'); //delete the old sqlite database first, might cause infinte loops otherwise if (file_exists("{$datadir}/owncloud.db")) { unlink("{$datadir}/owncloud.db"); } //in case of sqlite, we can always fill the database error_log("creating sqlite db"); \OC_DB::createDbFromStructure($this->dbDefinitionFile); }
public function setupDatabase($username) { //check if the database user has admin right $connection = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword); if (!$connection) { throw new \OC\DatabaseSetupException($this->trans->t('MySQL/MariaDB username and/or password not valid'), $this->trans->t('You need to enter either an existing account or the administrator.')); } //user already specified in config $oldUser = \OC_Config::getValue('dbuser', false); //we don't have a dbuser specified in config if ($this->dbuser != $oldUser) { //add prefix to the admin username to prevent collisions $adminUser = substr('oc_' . $username, 0, 16); $i = 1; while (true) { //this should be enough to check for admin rights in mysql $query = "SELECT user FROM mysql.user WHERE user='******'"; $result = mysql_query($query, $connection); //current dbuser has admin rights if ($result) { //new dbuser does not exist if (mysql_num_rows($result) === 0) { //use the admin login data for the new database user $this->dbuser = $adminUser; //create a random password so we don't need to store the admin password in the config file $this->dbpassword = \OC_Util::generateRandomBytes(30); $this->createDBUser($connection); break; } else { //repeat with different username $length = strlen((string) $i); $adminUser = substr('oc_' . $username, 0, 16 - $length) . $i; $i++; } } else { break; } } \OC_Config::setValue('dbuser', $this->dbuser); \OC_Config::setValue('dbpassword', $this->dbpassword); } //create the database $this->createDatabase($connection); //fill the database if needed $query = 'select count(*) from information_schema.tables' . " where table_schema='" . $this->dbname . "' AND table_name = '" . $this->tableprefix . "users';"; $result = mysql_query($query, $connection); if ($result) { $row = mysql_fetch_row($result); } if (!$result or $row[0] == 0) { \OC_DB::createDbFromStructure($this->dbDefinitionFile); } mysql_close($connection); }
public function setUp() { $dbfile = OC::$SERVERROOT . '/tests/data/db_structure.xml'; $r = '_' . OC_Util::generate_random_bytes('4') . '_'; $content = file_get_contents($dbfile); $content = str_replace('*dbprefix*', '*dbprefix*' . $r, $content); file_put_contents(self::$schema_file, $content); OC_DB::createDbFromStructure(self::$schema_file); $this->test_prefix = $r; $this->table1 = $this->test_prefix . 'contacts_addressbooks'; $this->table2 = $this->test_prefix . 'contacts_cards'; $this->table3 = $this->test_prefix . 'vcategory'; }
public function setupDatabase($username) { //check if the database user has admin right $connection = $this->connect(); $this->createSpecificUser($username, $connection); //create the database $this->createDatabase($connection); //fill the database if needed $query = 'select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; $result = $connection->executeQuery($query, [$this->dbName, $this->tablePrefix . 'users']); $row = $result->fetch(); if (!$result or $row[0] == 0) { \OC_DB::createDbFromStructure($this->dbDefinitionFile); } }
protected function setUp() { parent::setUp(); $dbfile = OC::$SERVERROOT . '/tests/data/db_structure.xml'; $r = '_' . OC_Util::generateRandomBytes(4) . '_'; $content = file_get_contents($dbfile); $content = str_replace('*dbprefix*', '*dbprefix*' . $r, $content); file_put_contents(self::$schema_file, $content); OC_DB::createDbFromStructure(self::$schema_file); $this->test_prefix = $r; $this->table1 = $this->test_prefix . 'cntcts_addrsbks'; $this->table2 = $this->test_prefix . 'cntcts_cards'; $this->table3 = $this->test_prefix . 'vcategory'; $this->table4 = $this->test_prefix . 'decimal'; }
public static function xsetUpBeforeClass() { $dbfile = __DIR__ . '/../../appinfo/database.xml'; self::$test_prefix = '_' . OC_Util::generateRandomBytes('4') . '_'; $content = file_get_contents($dbfile); $content = str_replace('*dbprefix*', '*dbprefix*' . self::$test_prefix, $content); file_put_contents(self::$schema_file, $content); OC_DB::createDbFromStructure(self::$schema_file); self::$addressBooksTableName = '*PREFIX*' . self::$test_prefix . 'contacts_addressbooks'; self::$cardsTableName = '*PREFIX*' . self::$test_prefix . 'contacts_cards'; OC_User::clearBackends(); OC_User::useBackend('dummy'); self::$user = uniqid('user_'); OC_User::createUser(self::$user, 'pass'); OC_User::setUserId(self::$user); self::$backend = new OCA\Contacts\Backend\Database(self::$user, self::$addressBooksTableName, self::$cardsTableName); }
public function setupDatabase($username) { //check if the database user has admin right $connection = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword); if (!$connection) { throw new \DatabaseSetupException($this->trans->t('MySQL username and/or password not valid'), $this->trans->t('You need to enter either an existing account or the administrator.')); } $oldUser = \OC_Config::getValue('dbuser', false); //this should be enough to check for admin rights in mysql $query = "SELECT user FROM mysql.user WHERE user='******'"; if (mysql_query($query, $connection)) { //use the admin login data for the new database user //add prefix to the mysql user name to prevent collisions $this->dbuser = substr('oc_' . $username, 0, 16); if ($this->dbuser != $oldUser) { //hash the password so we don't need to store the admin config in the config file $this->dbpassword = \OC_Util::generateRandomBytes(30); $this->createDBUser($connection); \OC_Config::setValue('dbuser', $this->dbuser); \OC_Config::setValue('dbpassword', $this->dbpassword); } //create the database $this->createDatabase($connection); } else { if ($this->dbuser != $oldUser) { \OC_Config::setValue('dbuser', $this->dbuser); \OC_Config::setValue('dbpassword', $this->dbpassword); } //create the database $this->createDatabase($connection); } //fill the database if needed $query = 'select count(*) from information_schema.tables' . " where table_schema='" . $this->dbname . "' AND table_name = '" . $this->tableprefix . "users';"; $result = mysql_query($query, $connection); if ($result) { $row = mysql_fetch_row($result); } if (!$result or $row[0] == 0) { \OC_DB::createDbFromStructure($this->dbDefinitionFile); } mysql_close($connection); }
<?php $currentVersion = OC_Appconfig::getValue('gallery', 'installed_version'); if (version_compare($currentVersion, '0.5.0', '<')) { $stmt = OCP\DB::prepare('DROP TABLE IF EXISTS `*PREFIX*gallery_photos`'); $stmt->execute(); $stmt = OCP\DB::prepare('DROP TABLE IF EXISTS `*PREFIX*gallery_albums`'); $stmt->execute(); \OC_DB::createDbFromStructure(OC_App::getAppPath($appid) . '/appinfo/database.xml'); }
<?php $currentVersion = OC_Appconfig::getValue('gallery', 'installed_version'); if (version_compare($currentVersion, '0.5.0', '<')) { $stmt = OCP\DB::prepare('DROP TABLE IF EXISTS *PREFIX*gallery_photos'); $stmt->execute(); $stmt = OCP\DB::prepare('DROP TABLE IF EXISTS *PREFIX*gallery_albums'); $stmt->execute(); \OC_DB::createDbFromStructure(OC::$APPSROOT . '/apps/' . $appid . '/appinfo/database.xml'); }
public function doTestSchemaCreating() { OC_DB::createDbFromStructure($this->schema_file); $this->assertTableExist($this->table1); $this->assertTableExist($this->table2); }
public function testBrokenLastIndexId() { // create test table $this->checkLastIndexId(); \OC_DB::createDbFromStructure(__DIR__ . '/encryption_table.xml'); $this->checkLastIndexId(); }
/** * install an app already placed in the app folder * @param string $app id of the app to install * @returns array see OC_App::getAppInfo */ public static function installShippedApp($app) { //install the database if (is_file(OC::$APPSROOT . "/apps/{$app}/appinfo/database.xml")) { OC_DB::createDbFromStructure(OC::$APPSROOT . "/apps/{$app}/appinfo/database.xml"); } //run appinfo/install.php if (is_file(OC::$APPSROOT . "/apps/{$app}/appinfo/install.php")) { include OC::$APPSROOT . "/apps/{$app}/appinfo/install.php"; } $info = OC_App::getAppInfo($app); OC_Appconfig::setValue($app, 'installed_version', OC_App::getAppVersion($app)); //set remote/public handelers foreach ($info['remote'] as $name => $path) { OCP\CONFIG::setAppValue('core', 'remote_' . $name, '/apps/' . $app . '/' . $path); } foreach ($info['public'] as $name => $path) { OCP\CONFIG::setAppValue('core', 'public_' . $name, '/apps/' . $app . '/' . $path); } OC_App::setAppTypes($info['id']); return $info; }
/** * install an app already placed in the app folder * @param string $app id of the app to install * @returns array see OC_App::getAppInfo */ public static function installShippedApp($app) { //install the database if (is_file(OC::$SERVERROOT . "/apps/{$app}/appinfo/database.xml")) { OC_DB::createDbFromStructure(OC::$SERVERROOT . "/apps/{$app}/appinfo/database.xml"); } //run appinfo/install.php if (is_file(OC::$SERVERROOT . "/apps/{$app}/appinfo/install.php")) { include OC::$SERVERROOT . "/apps/{$app}/appinfo/install.php"; } $info = OC_App::getAppInfo(OC::$SERVERROOT . "/apps/{$app}/appinfo/info.xml"); OC_Appconfig::setValue($app, 'installed_version', $info['version']); return $info; }
public function setupDatabase($username) { $e_host = addslashes($this->dbhost); $e_user = addslashes($this->dbuser); $e_password = addslashes($this->dbpassword); //check if the database user has admin rights $connection_string = "host='{$e_host}' dbname=postgres user='******' password='******'"; $connection = @pg_connect($connection_string); if (!$connection) { // Try if we can connect to the DB with the specified name $e_dbname = addslashes($this->dbname); $connection_string = "host='{$e_host}' dbname='{$e_dbname}' user='******' password='******'"; $connection = @pg_connect($connection_string); if (!$connection) { throw new \DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'), $this->trans->t('You need to enter either an existing account or the administrator.')); } } $e_user = pg_escape_string($this->dbuser); //check for roles creation rights in postgresql $query = "SELECT 1 FROM pg_roles WHERE rolcreaterole=TRUE AND rolname='{$e_user}'"; $result = pg_query($connection, $query); if ($result and pg_num_rows($result) > 0) { //use the admin login data for the new database user //add prefix to the postgresql user name to prevent collisions $this->dbuser = '******' . $username; //create a new password so we don't need to store the admin config in the config file $this->dbpassword = \OC_Util::generateRandomBytes(30); $this->createDBUser($connection); \OC_Config::setValue('dbuser', $this->dbuser); \OC_Config::setValue('dbpassword', $this->dbpassword); //create the database $this->createDatabase($connection); } else { \OC_Config::setValue('dbuser', $this->dbuser); \OC_Config::setValue('dbpassword', $this->dbpassword); //create the database $this->createDatabase($connection); } // the connection to dbname=postgres is not needed anymore pg_close($connection); // connect to the ownCloud database (dbname=$this->dbname) and check if it needs to be filled $this->dbuser = \OC_Config::getValue('dbuser'); $this->dbpassword = \OC_Config::getValue('dbpassword'); $e_host = addslashes($this->dbhost); $e_dbname = addslashes($this->dbname); $e_user = addslashes($this->dbuser); $e_password = addslashes($this->dbpassword); $connection_string = "host='{$e_host}' dbname='{$e_dbname}' user='******' password='******'"; $connection = @pg_connect($connection_string); if (!$connection) { throw new \DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'), $this->trans->t('You need to enter either an existing account or the administrator.')); } $query = "select count(*) FROM pg_class WHERE relname='" . $this->tableprefix . "users' limit 1"; $result = pg_query($connection, $query); if ($result) { $row = pg_fetch_row($result); } if (!$result or $row[0] == 0) { \OC_DB::createDbFromStructure($this->dbDefinitionFile); } }
private static function setupOCIDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $dbtablespace, $username) { $e_host = addslashes($dbhost); $e_dbname = addslashes($dbname); //check if the database user has admin right if ($e_host == '') { $easy_connect_string = $e_dbname; // use dbname as easy connect name } else { $easy_connect_string = '//' . $e_host . '/' . $e_dbname; } $connection = @oci_connect($dbuser, $dbpass, $easy_connect_string); if (!$connection) { $e = oci_error(); throw new Exception('Oracle username and/or password not valid'); } //check for roles creation rights in oracle $query = "SELECT count(*) FROM user_role_privs, role_sys_privs WHERE user_role_privs.granted_role = role_sys_privs.role AND privilege = 'CREATE ROLE'"; $stmt = oci_parse($connection, $query); if (!$stmt) { $entry = 'DB Error: "' . oci_last_error($connection) . '"<br />'; $entry .= 'Offending command was: ' . $query . '<br />'; echo $entry; } $result = oci_execute($stmt); if ($result) { $row = oci_fetch_row($stmt); } if ($result and $row[0] > 0) { //use the admin login data for the new database user //add prefix to the oracle user name to prevent collisions $dbusername = '******' . $username; //create a new password so we don't need to store the admin config in the config file $dbpassword = md5(time() . $dbpass); //oracle passwords are treated as identifiers: // must start with aphanumeric char // needs to be shortened to 30 bytes, as the two " needed to escape the identifier count towards the identifier length. $dbpassword = substr($dbpassword, 0, 30); self::oci_createDBUser($dbusername, $dbpassword, $dbtablespace, $connection); OC_Config::setValue('dbuser', $dbusername); OC_Config::setValue('dbname', $dbusername); OC_Config::setValue('dbpassword', $dbpassword); //create the database not neccessary, oracle implies user = schema //self::oci_createDatabase($dbname, $dbusername, $connection); } else { OC_Config::setValue('dbuser', $dbuser); OC_Config::setValue('dbname', $dbname); OC_Config::setValue('dbpassword', $dbpass); //create the database not neccessary, oracle implies user = schema //self::oci_createDatabase($dbname, $dbuser, $connection); } //FIXME check tablespace exists: select * from user_tablespaces // the connection to dbname=oracle is not needed anymore oci_close($connection); // connect to the oracle database (schema=$dbuser) an check if the schema needs to be filled $dbuser = OC_Config::getValue('dbuser'); //$dbname = OC_Config::getValue('dbname'); $dbpass = OC_Config::getValue('dbpassword'); $e_host = addslashes($dbhost); $e_dbname = addslashes($dbname); if ($e_host == '') { $easy_connect_string = $e_dbname; // use dbname as easy connect name } else { $easy_connect_string = '//' . $e_host . '/' . $e_dbname; } $connection = @oci_connect($dbuser, $dbpass, $easy_connect_string); if (!$connection) { throw new Exception('Oracle username and/or password not valid'); } $query = "SELECT count(*) FROM user_tables WHERE table_name = :un"; $stmt = oci_parse($connection, $query); $un = $dbtableprefix . 'users'; oci_bind_by_name($stmt, ':un', $un); if (!$stmt) { $entry = 'DB Error: "' . oci_last_error($connection) . '"<br />'; $entry .= 'Offending command was: ' . $query . '<br />'; echo $entry; } $result = oci_execute($stmt); if ($result) { $row = oci_fetch_row($stmt); } if (!$result or $row[0] == 0) { OC_DB::createDbFromStructure('db_structure.xml'); } }
private static function mssql_createDatabaseStructure($dbhost, $dbname, $dbuser, $dbpass, $dbtableprefix) { $connectionInfo = array("Database" => $dbname, "UID" => $dbuser, "PWD" => $dbpass); $connection = @sqlsrv_connect($dbhost, $connectionInfo); //fill the database if needed $query = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{$dbname}' AND TABLE_NAME = '{$dbtableprefix}users'"; $result = sqlsrv_query($connection, $query); if ($result === false) { if (($errors = sqlsrv_errors()) != null) { $entry = 'DB Error: "' . print_r(sqlsrv_errors()) . '"<br />'; } else { $entry = ''; } $entry .= 'Offending command was: ' . $query . '<br />'; \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN); } else { $row = sqlsrv_fetch_array($result); if ($row === false) { if (($errors = sqlsrv_errors()) != null) { $entry = 'DB Error: "' . print_r(sqlsrv_errors()) . '"<br />'; } else { $entry = ''; } $entry .= 'Offending command was: ' . $query . '<br />'; \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN); } else { if ($row == null) { OC_DB::createDbFromStructure('db_structure.xml'); } } } sqlsrv_close($connection); }
private function createDatabaseStructure() { $connectionInfo = array("Database" => $this->dbname, "UID" => $this->dbuser, "PWD" => $this->dbpassword); $connection = @sqlsrv_connect($this->dbhost, $connectionInfo); //fill the database if needed $query = "SELECT * FROM INFORMATION_SCHEMA.TABLES" . " WHERE TABLE_SCHEMA = '" . $this->dbname . "'" . " AND TABLE_NAME = '" . $this->tableprefix . "users'"; $result = sqlsrv_query($connection, $query); if ($result === false) { if (($errors = sqlsrv_errors()) != null) { $entry = 'DB Error: "' . print_r(sqlsrv_errors()) . '"<br />'; } else { $entry = ''; } $entry .= 'Offending command was: ' . $query . '<br />'; \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN); } else { $row = sqlsrv_fetch_array($result); if ($row === false) { if (($errors = sqlsrv_errors()) != null) { $entry = 'DB Error: "' . print_r(sqlsrv_errors()) . '"<br />'; } else { $entry = ''; } $entry .= 'Offending command was: ' . $query . '<br />'; \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN); } else { if ($row == null) { \OC_DB::createDbFromStructure($this->dbDefinitionFile); } } } sqlsrv_close($connection); }
/** * install an app already placed in the app folder * @param string $app id of the app to install * @return integer */ public static function installShippedApp($app) { //install the database $appPath = OC_App::getAppPath($app); if (is_file("{$appPath}/appinfo/database.xml")) { OC_DB::createDbFromStructure("{$appPath}/appinfo/database.xml"); } //run appinfo/install.php \OC::$loader->addValidRoot($appPath); self::includeAppScript("{$appPath}/appinfo/install.php"); $info = OC_App::getAppInfo($app); if (is_null($info)) { return false; } $config = \OC::$server->getConfig(); $config->setAppValue($app, 'installed_version', OC_App::getAppVersion($app)); if (array_key_exists('ocsid', $info)) { $config->setAppValue($app, 'ocsid', $info['ocsid']); } //set remote/public handlers foreach ($info['remote'] as $name => $path) { $config->setAppValue('core', 'remote_' . $name, $app . '/' . $path); } foreach ($info['public'] as $name => $path) { $config->setAppValue('core', 'public_' . $name, $app . '/' . $path); } OC_App::setAppTypes($info['id']); return $info['id']; }
public function testDuplicateDataMigration() { // create test table OC_DB::createDbFromStructure(__DIR__ . '/encryption_table.xml'); // in case of duplicate entries we want to preserve 0 on migration status and 1 on recovery $data = array(array('user1', 'server-side', 1, 1), array('user1', 'server-side', 1, 0), array('user1', 'server-side', 0, 1), array('user1', 'server-side', 0, 0)); foreach ($data as $d) { OC_DB::executeAudited('INSERT INTO `*PREFIX*encryption_test` values(?, ?, ?, ?)', $d); } // preform migration $migration = new Migration('encryption_test'); $migration->dropTableEncryption(); // assert $this->assertTableNotExist('encryption_test'); $rec = \OC_Preferences::getValue('user1', 'files_encryption', 'recovery_enabled'); $mig = \OC_Preferences::getValue('user1', 'files_encryption', 'migration_status'); $this->assertEquals(1, $rec); $this->assertEquals(0, $mig); }
public static function install($options) { $error = array(); $dbtype = $options['dbtype']; if (empty($options['adminlogin'])) { $error[] = 'Set an admin username.'; } if (empty($options['adminpass'])) { $error[] = 'Set an admin password.'; } if (empty($options['directory'])) { $error[] = 'Specify a data folder.'; } if ($dbtype == 'mysql' or $dbtype == 'pgsql') { //mysql and postgresql needs more config options if ($dbtype == 'mysql') { $dbprettyname = 'MySQL'; } else { $dbprettyname = 'PostgreSQL'; } if (empty($options['dbuser'])) { $error[] = "{$dbprettyname} enter the database username."; } if (empty($options['dbname'])) { $error[] = "{$dbprettyname} enter the database name."; } if (empty($options['dbhost'])) { $error[] = "{$dbprettyname} set the database host."; } } if (count($error) == 0) { //no errors, good $username = htmlspecialchars_decode($options['adminlogin']); $password = htmlspecialchars_decode($options['adminpass']); $datadir = htmlspecialchars_decode($options['directory']); //use sqlite3 when available, otherise sqlite2 will be used. if ($dbtype == 'sqlite' and class_exists('SQLite3')) { $dbtype = 'sqlite3'; } //generate a random salt that is used to salt the local user passwords $salt = OC_Util::generate_random_bytes(30); OC_Config::setValue('passwordsalt', $salt); //write the config file OC_Config::setValue('datadirectory', $datadir); OC_Config::setValue('dbtype', $dbtype); OC_Config::setValue('version', implode('.', OC_Util::getVersion())); if ($dbtype == 'mysql') { $dbuser = $options['dbuser']; $dbpass = $options['dbpass']; $dbname = $options['dbname']; $dbhost = $options['dbhost']; $dbtableprefix = isset($options['dbtableprefix']) ? $options['dbtableprefix'] : 'oc_'; OC_Config::setValue('dbname', $dbname); OC_Config::setValue('dbhost', $dbhost); OC_Config::setValue('dbtableprefix', $dbtableprefix); //check if the database user has admin right $connection = @mysql_connect($dbhost, $dbuser, $dbpass); if (!$connection) { $error[] = array('error' => 'MySQL username and/or password not valid', 'hint' => 'You need to enter either an existing account or the administrator.'); return $error; } else { $oldUser = OC_Config::getValue('dbuser', false); $oldPassword = OC_Config::getValue('dbpassword', false); $query = "SELECT user FROM mysql.user WHERE user='******'"; //this should be enough to check for admin rights in mysql if (mysql_query($query, $connection)) { //use the admin login data for the new database user //add prefix to the mysql user name to prevent collissions $dbusername = substr('oc_' . $username, 0, 16); if ($dbusername != $oldUser) { //hash the password so we don't need to store the admin config in the config file $dbpassword = md5(time() . $password); self::createDBUser($dbusername, $dbpassword, $connection); OC_Config::setValue('dbuser', $dbusername); OC_Config::setValue('dbpassword', $dbpassword); } //create the database self::createDatabase($dbname, $dbusername, $connection); } else { if ($dbuser != $oldUser) { OC_Config::setValue('dbuser', $dbuser); OC_Config::setValue('dbpassword', $dbpass); } //create the database self::createDatabase($dbname, $dbuser, $connection); } //fill the database if needed $query = "select count(*) from information_schema.tables where table_schema='{$dbname}' AND table_name = '{$dbtableprefix}users';"; $result = mysql_query($query, $connection); if ($result) { $row = mysql_fetch_row($result); } if (!$result or $row[0] == 0) { OC_DB::createDbFromStructure('db_structure.xml'); } mysql_close($connection); } } elseif ($dbtype == 'pgsql') { $dbuser = $options['dbuser']; $dbpass = $options['dbpass']; $dbname = $options['dbname']; $dbhost = $options['dbhost']; $dbtableprefix = isset($options['dbtableprefix']) ? $options['dbtableprefix'] : 'oc_'; OC_CONFIG::setValue('dbname', $dbname); OC_CONFIG::setValue('dbhost', $dbhost); OC_CONFIG::setValue('dbtableprefix', $dbtableprefix); //check if the database user has admin right $connection_string = "host={$dbhost} dbname=postgres user={$dbuser} password={$dbpass}"; $connection = @pg_connect($connection_string); if (!$connection) { $error[] = array('error' => 'PostgreSQL username and/or password not valid', 'hint' => 'You need to enter either an existing account or the administrator.'); return $error; } else { //check for roles creation rights in postgresql $query = "SELECT 1 FROM pg_roles WHERE rolcreaterole=TRUE AND rolname='{$dbuser}'"; $result = pg_query($connection, $query); if ($result and pg_num_rows($result) > 0) { //use the admin login data for the new database user //add prefix to the postgresql user name to prevent collissions $dbusername = '******' . $username; //create a new password so we don't need to store the admin config in the config file $dbpassword = md5(time()); self::pg_createDBUser($dbusername, $dbpassword, $connection); OC_CONFIG::setValue('dbuser', $dbusername); OC_CONFIG::setValue('dbpassword', $dbpassword); //create the database self::pg_createDatabase($dbname, $dbusername, $connection); } else { OC_CONFIG::setValue('dbuser', $dbuser); OC_CONFIG::setValue('dbpassword', $dbpass); //create the database self::pg_createDatabase($dbname, $dbuser, $connection); } // the connection to dbname=postgres is not needed anymore pg_close($connection); // connect to the ownCloud database (dbname=$dbname) an check if it needs to be filled $dbuser = OC_CONFIG::getValue('dbuser'); $dbpass = OC_CONFIG::getValue('dbpassword'); $connection_string = "host={$dbhost} dbname={$dbname} user={$dbuser} password={$dbpass}"; $connection = @pg_connect($connection_string); if (!$connection) { $error[] = array('error' => 'PostgreSQL username and/or password not valid', 'hint' => 'You need to enter either an existing account or the administrator.'); } else { $query = "select count(*) FROM pg_class WHERE relname='{$dbtableprefix}users' limit 1"; $result = pg_query($connection, $query); if ($result) { $row = pg_fetch_row($result); } if (!$result or $row[0] == 0) { OC_DB::createDbFromStructure('db_structure.xml'); } } } } else { //delete the old sqlite database first, might cause infinte loops otherwise if (file_exists("{$datadir}/owncloud.db")) { unlink("{$datadir}/owncloud.db"); } //in case of sqlite, we can always fill the database OC_DB::createDbFromStructure('db_structure.xml'); } //create the user and group try { OC_User::createUser($username, $password); } catch (Exception $exception) { $error[] = $exception->getMessage(); } if (count($error) == 0) { OC_Appconfig::setValue('core', 'installedat', microtime(true)); OC_Appconfig::setValue('core', 'lastupdatedat', microtime(true)); OC_Group::createGroup('admin'); OC_Group::addToGroup($username, 'admin'); OC_User::login($username, $password); //guess what this does OC_Installer::installShippedApps(); //create htaccess files for apache hosts if (strstr($_SERVER['SERVER_SOFTWARE'], 'Apache')) { self::createHtaccess(); } //and we are done OC_Config::setValue('installed', true); } } return $error; }
/** * install an app already placed in the app folder * @param string $app id of the app to install * @return integer */ public static function installShippedApp($app) { //install the database if (is_file(OC_App::getAppPath($app) . "/appinfo/database.xml")) { OC_DB::createDbFromStructure(OC_App::getAppPath($app) . "/appinfo/database.xml"); } //run appinfo/install.php if (is_file(OC_App::getAppPath($app) . "/appinfo/install.php")) { include OC_App::getAppPath($app) . "/appinfo/install.php"; } $info = OC_App::getAppInfo($app); if (is_null($info)) { return false; } OC_Appconfig::setValue($app, 'installed_version', OC_App::getAppVersion($app)); if (array_key_exists('ocsid', $info)) { OC_Appconfig::setValue($app, 'ocsid', $info['ocsid']); } //set remote/public handelers foreach ($info['remote'] as $name => $path) { OCP\CONFIG::setAppValue('core', 'remote_' . $name, $app . '/' . $path); } foreach ($info['public'] as $name => $path) { OCP\CONFIG::setAppValue('core', 'public_' . $name, $app . '/' . $path); } OC_App::setAppTypes($info['id']); return $info['id']; }
public function setupDatabase($username) { $e_host = addslashes($this->dbHost); $e_user = addslashes($this->dbUser); $e_password = addslashes($this->dbPassword); // Fix database with port connection if (strpos($e_host, ':')) { list($e_host, $port) = explode(':', $e_host, 2); } else { $port = false; } //check if the database user has admin rights $connection_string = "host='{$e_host}' dbname=postgres user='******' port='{$port}' password='******'"; $connection = @pg_connect($connection_string); if (!$connection) { // Try if we can connect to the DB with the specified name $e_dbname = addslashes($this->dbName); $connection_string = "host='{$e_host}' dbname='{$e_dbname}' user='******' port='{$port}' password='******'"; $connection = @pg_connect($connection_string); if (!$connection) { throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'), $this->trans->t('You need to enter either an existing account or the administrator.')); } } $e_user = pg_escape_string($this->dbUser); //check for roles creation rights in postgresql $query = "SELECT 1 FROM pg_roles WHERE rolcreaterole=TRUE AND rolname='{$e_user}'"; $result = pg_query($connection, $query); if ($result and pg_num_rows($result) > 0) { //use the admin login data for the new database user //add prefix to the postgresql user name to prevent collisions $this->dbUser = '******' . $username; //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); $this->createDBUser($connection); } $systemConfig = \OC::$server->getSystemConfig(); $systemConfig->setValues(['dbuser' => $this->dbUser, 'dbpassword' => $this->dbPassword]); //create the database $this->createDatabase($connection); // the connection to dbname=postgres is not needed anymore pg_close($connection); // connect to the ownCloud database (dbname=$this->dbname) and check if it needs to be filled $this->dbUser = $systemConfig->getValue('dbuser'); $this->dbPassword = $systemConfig->getValue('dbpassword'); $e_host = addslashes($this->dbHost); $e_dbname = addslashes($this->dbName); $e_user = addslashes($this->dbUser); $e_password = addslashes($this->dbPassword); // Fix database with port connection if (strpos($e_host, ':')) { list($e_host, $port) = explode(':', $e_host, 2); } else { $port = false; } $connection_string = "host='{$e_host}' dbname='{$e_dbname}' user='******' port='{$port}' password='******'"; $connection = @pg_connect($connection_string); if (!$connection) { throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'), $this->trans->t('You need to enter either an existing account or the administrator.')); } $query = "select count(*) FROM pg_class WHERE relname='" . $this->tablePrefix . "users' limit 1"; $result = pg_query($connection, $query); if ($result) { $row = pg_fetch_row($result); } if (!$result or $row[0] == 0) { \OC_DB::createDbFromStructure($this->dbDefinitionFile); } }
public function setupDatabase($username) { $e_host = addslashes($this->dbhost); $e_dbname = addslashes($this->dbname); //check if the database user has admin right if ($e_host == '') { $easy_connect_string = $e_dbname; // use dbname as easy connect name } else { $easy_connect_string = '//' . $e_host . '/' . $e_dbname; } \OC_Log::write('setup oracle', 'connect string: ' . $easy_connect_string, \OC_Log::DEBUG); $connection = @oci_connect($this->dbuser, $this->dbpassword, $easy_connect_string); if (!$connection) { $errorMessage = $this->getLastError(); if ($errorMessage) { throw new \DatabaseSetupException($this->trans->t('Oracle connection could not be established'), $errorMessage . ' Check environment: ORACLE_HOME=' . getenv('ORACLE_HOME') . ' ORACLE_SID=' . getenv('ORACLE_SID') . ' LD_LIBRARY_PATH=' . getenv('LD_LIBRARY_PATH') . ' NLS_LANG=' . getenv('NLS_LANG') . ' tnsnames.ora is ' . (is_readable(getenv('ORACLE_HOME') . '/network/admin/tnsnames.ora') ? '' : 'not ') . 'readable'); } throw new \DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'), 'Check environment: ORACLE_HOME=' . getenv('ORACLE_HOME') . ' ORACLE_SID=' . getenv('ORACLE_SID') . ' LD_LIBRARY_PATH=' . getenv('LD_LIBRARY_PATH') . ' NLS_LANG=' . getenv('NLS_LANG') . ' tnsnames.ora is ' . (is_readable(getenv('ORACLE_HOME') . '/network/admin/tnsnames.ora') ? '' : 'not ') . 'readable'); } //check for roles creation rights in oracle $query = 'SELECT count(*) FROM user_role_privs, role_sys_privs' . " WHERE user_role_privs.granted_role = role_sys_privs.role AND privilege = 'CREATE ROLE'"; $stmt = oci_parse($connection, $query); if (!$stmt) { $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; \OC_Log::write('setup.oci', $entry, \OC_Log::WARN); } $result = oci_execute($stmt); if ($result) { $row = oci_fetch_row($stmt); } if ($result and $row[0] > 0) { //use the admin login data for the new database user //add prefix to the oracle user name to prevent collisions $this->dbuser = '******' . $username; //create a new password so we don't need to store the admin config in the config file $this->dbpassword = \OC_Util::generateRandomBytes(30); //oracle passwords are treated as identifiers: // must start with alphanumeric char // needs to be shortened to 30 bytes, as the two " needed to escape the identifier count towards the identifier length. $this->dbpassword = substr($this->dbpassword, 0, 30); $this->createDBUser($connection); \OC_Config::setValue('dbuser', $this->dbuser); \OC_Config::setValue('dbname', $this->dbuser); \OC_Config::setValue('dbpassword', $this->dbpassword); //create the database not necessary, oracle implies user = schema //$this->createDatabase($this->dbname, $this->dbuser, $connection); } else { \OC_Config::setValue('dbuser', $this->dbuser); \OC_Config::setValue('dbname', $this->dbname); \OC_Config::setValue('dbpassword', $this->dbpassword); //create the database not necessary, oracle implies user = schema //$this->createDatabase($this->dbname, $this->dbuser, $connection); } //FIXME check tablespace exists: select * from user_tablespaces // the connection to dbname=oracle is not needed anymore oci_close($connection); // connect to the oracle database (schema=$this->dbuser) an check if the schema needs to be filled $this->dbuser = \OC_Config::getValue('dbuser'); //$this->dbname = \OC_Config::getValue('dbname'); $this->dbpassword = \OC_Config::getValue('dbpassword'); $e_host = addslashes($this->dbhost); $e_dbname = addslashes($this->dbname); if ($e_host == '') { $easy_connect_string = $e_dbname; // use dbname as easy connect name } else { $easy_connect_string = '//' . $e_host . '/' . $e_dbname; } $connection = @oci_connect($this->dbuser, $this->dbpassword, $easy_connect_string); if (!$connection) { throw new \DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'), $this->trans->t('You need to enter either an existing account or the administrator.')); } $query = "SELECT count(*) FROM user_tables WHERE table_name = :un"; $stmt = oci_parse($connection, $query); $un = $this->tableprefix . 'users'; oci_bind_by_name($stmt, ':un', $un); if (!$stmt) { $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; \OC_Log::write('setup.oci', $entry, \OC_Log::WARN); } $result = oci_execute($stmt); if ($result) { $row = oci_fetch_row($stmt); } if (!$result or $row[0] == 0) { \OC_DB::createDbFromStructure($this->dbDefinitionFile); } }
public static function install($options) { $error = array(); $dbtype = $options['dbtype']; if (empty($options['adminlogin'])) { $error[] = 'Set an admin username.'; } if (empty($options['adminpass'])) { $error[] = 'Set an admin password.'; } if (empty($options['directory'])) { $error[] = 'Specify a data folder.'; } if ($dbtype == 'mysql' or $dbtype == 'pgsql' or $dbtype == 'oci') { //mysql and postgresql needs more config options if ($dbtype == 'mysql') { $dbprettyname = 'MySQL'; } else { if ($dbtype == 'pgsql') { $dbprettyname = 'PostgreSQL'; } else { $dbprettyname = 'Oracle'; } } if (empty($options['dbuser'])) { $error[] = "{$dbprettyname} enter the database username."; } if (empty($options['dbname'])) { $error[] = "{$dbprettyname} enter the database name."; } if ($dbtype != 'oci' && empty($options['dbhost'])) { $error[] = "{$dbprettyname} set the database host."; } } if (count($error) == 0) { //no errors, good $username = htmlspecialchars_decode($options['adminlogin']); $password = htmlspecialchars_decode($options['adminpass']); $datadir = htmlspecialchars_decode($options['directory']); //use sqlite3 when available, otherise sqlite2 will be used. if ($dbtype == 'sqlite' and class_exists('SQLite3')) { $dbtype = 'sqlite3'; } //generate a random salt that is used to salt the local user passwords $salt = OC_Util::generate_random_bytes(30); OC_Config::setValue('passwordsalt', $salt); //write the config file OC_Config::setValue('datadirectory', $datadir); OC_Config::setValue('dbtype', $dbtype); OC_Config::setValue('version', implode('.', OC_Util::getVersion())); if ($dbtype == 'mysql') { $dbuser = $options['dbuser']; $dbpass = $options['dbpass']; $dbname = $options['dbname']; $dbhost = $options['dbhost']; $dbtableprefix = isset($options['dbtableprefix']) ? $options['dbtableprefix'] : 'oc_'; OC_Config::setValue('dbname', $dbname); OC_Config::setValue('dbhost', $dbhost); OC_Config::setValue('dbtableprefix', $dbtableprefix); //check if the database user has admin right $connection = @mysql_connect($dbhost, $dbuser, $dbpass); if (!$connection) { $error[] = array('error' => 'MySQL username and/or password not valid', 'hint' => 'You need to enter either an existing account or the administrator.'); return $error; } else { $oldUser = OC_Config::getValue('dbuser', false); $query = "SELECT user FROM mysql.user WHERE user='******'"; //this should be enough to check for admin rights in mysql if (mysql_query($query, $connection)) { //use the admin login data for the new database user //add prefix to the mysql user name to prevent collisions $dbusername = substr('oc_' . $username, 0, 16); if ($dbusername != $oldUser) { //hash the password so we don't need to store the admin config in the config file $dbpassword = md5(time() . $password); self::createDBUser($dbusername, $dbpassword, $connection); OC_Config::setValue('dbuser', $dbusername); OC_Config::setValue('dbpassword', $dbpassword); } //create the database self::createDatabase($dbname, $dbusername, $connection); } else { if ($dbuser != $oldUser) { OC_Config::setValue('dbuser', $dbuser); OC_Config::setValue('dbpassword', $dbpass); } //create the database self::createDatabase($dbname, $dbuser, $connection); } //fill the database if needed $query = "select count(*) from information_schema.tables where table_schema='{$dbname}' AND table_name = '{$dbtableprefix}users';"; $result = mysql_query($query, $connection); if ($result) { $row = mysql_fetch_row($result); } if (!$result or $row[0] == 0) { OC_DB::createDbFromStructure('db_structure.xml'); } mysql_close($connection); } } elseif ($dbtype == 'pgsql') { $dbuser = $options['dbuser']; $dbpass = $options['dbpass']; $dbname = $options['dbname']; $dbhost = $options['dbhost']; $dbtableprefix = isset($options['dbtableprefix']) ? $options['dbtableprefix'] : 'oc_'; OC_CONFIG::setValue('dbname', $dbname); OC_CONFIG::setValue('dbhost', $dbhost); OC_CONFIG::setValue('dbtableprefix', $dbtableprefix); $e_host = addslashes($dbhost); $e_user = addslashes($dbuser); $e_password = addslashes($dbpass); //check if the database user has admin right $connection_string = "host='{$e_host}' dbname=postgres user='******' password='******'"; $connection = @pg_connect($connection_string); if (!$connection) { $error[] = array('error' => 'PostgreSQL username and/or password not valid', 'hint' => 'You need to enter either an existing account or the administrator.'); return $error; } else { $e_user = pg_escape_string($dbuser); //check for roles creation rights in postgresql $query = "SELECT 1 FROM pg_roles WHERE rolcreaterole=TRUE AND rolname='{$e_user}'"; $result = pg_query($connection, $query); if ($result and pg_num_rows($result) > 0) { //use the admin login data for the new database user //add prefix to the postgresql user name to prevent collisions $dbusername = '******' . $username; //create a new password so we don't need to store the admin config in the config file $dbpassword = md5(time()); self::pg_createDBUser($dbusername, $dbpassword, $connection); OC_CONFIG::setValue('dbuser', $dbusername); OC_CONFIG::setValue('dbpassword', $dbpassword); //create the database self::pg_createDatabase($dbname, $dbusername, $connection); } else { OC_CONFIG::setValue('dbuser', $dbuser); OC_CONFIG::setValue('dbpassword', $dbpass); //create the database self::pg_createDatabase($dbname, $dbuser, $connection); } // the connection to dbname=postgres is not needed anymore pg_close($connection); // connect to the ownCloud database (dbname=$dbname) an check if it needs to be filled $dbuser = OC_CONFIG::getValue('dbuser'); $dbpass = OC_CONFIG::getValue('dbpassword'); $e_host = addslashes($dbhost); $e_dbname = addslashes($dbname); $e_user = addslashes($dbuser); $e_password = addslashes($dbpass); $connection_string = "host='{$e_host}' dbname='{$e_dbname}' user='******' password='******'"; $connection = @pg_connect($connection_string); if (!$connection) { $error[] = array('error' => 'PostgreSQL username and/or password not valid', 'hint' => 'You need to enter either an existing account or the administrator.'); } else { $query = "select count(*) FROM pg_class WHERE relname='{$dbtableprefix}users' limit 1"; $result = pg_query($connection, $query); if ($result) { $row = pg_fetch_row($result); } if (!$result or $row[0] == 0) { OC_DB::createDbFromStructure('db_structure.xml'); } } } } elseif ($dbtype == 'oci') { $dbuser = $options['dbuser']; $dbpass = $options['dbpass']; $dbname = $options['dbname']; $dbtablespace = $options['dbtablespace']; $dbhost = isset($options['dbhost']) ? $options['dbhost'] : ''; $dbtableprefix = isset($options['dbtableprefix']) ? $options['dbtableprefix'] : 'oc_'; OC_CONFIG::setValue('dbname', $dbname); OC_CONFIG::setValue('dbtablespace', $dbtablespace); OC_CONFIG::setValue('dbhost', $dbhost); OC_CONFIG::setValue('dbtableprefix', $dbtableprefix); $e_host = addslashes($dbhost); $e_dbname = addslashes($dbname); //check if the database user has admin right if ($e_host == '') { $easy_connect_string = $e_dbname; // use dbname as easy connect name } else { $easy_connect_string = '//' . $e_host . '/' . $e_dbname; } $connection = @oci_connect($dbuser, $dbpass, $easy_connect_string); if (!$connection) { $e = oci_error(); $error[] = array('error' => 'Oracle username and/or password not valid', 'hint' => 'You need to enter either an existing account or the administrator.'); return $error; } else { //check for roles creation rights in oracle $query = "SELECT count(*) FROM user_role_privs, role_sys_privs WHERE user_role_privs.granted_role = role_sys_privs.role AND privilege = 'CREATE ROLE'"; $stmt = oci_parse($connection, $query); if (!$stmt) { $entry = 'DB Error: "' . oci_last_error($connection) . '"<br />'; $entry .= 'Offending command was: ' . $query . '<br />'; echo $entry; } $result = oci_execute($stmt); if ($result) { $row = oci_fetch_row($stmt); } if ($result and $row[0] > 0) { //use the admin login data for the new database user //add prefix to the oracle user name to prevent collisions $dbusername = '******' . $username; //create a new password so we don't need to store the admin config in the config file $dbpassword = md5(time() . $dbpass); //oracle passwords are treated as identifiers: // must start with aphanumeric char // needs to be shortened to 30 bytes, as the two " needed to escape the identifier count towards the identifier length. $dbpassword = substr($dbpassword, 0, 30); self::oci_createDBUser($dbusername, $dbpassword, $dbtablespace, $connection); OC_CONFIG::setValue('dbuser', $dbusername); OC_CONFIG::setValue('dbname', $dbusername); OC_CONFIG::setValue('dbpassword', $dbpassword); //create the database not neccessary, oracle implies user = schema //self::oci_createDatabase($dbname, $dbusername, $connection); } else { OC_CONFIG::setValue('dbuser', $dbuser); OC_CONFIG::setValue('dbname', $dbname); OC_CONFIG::setValue('dbpassword', $dbpass); //create the database not neccessary, oracle implies user = schema //self::oci_createDatabase($dbname, $dbuser, $connection); } //FIXME check tablespace exists: select * from user_tablespaces // the connection to dbname=oracle is not needed anymore oci_close($connection); // connect to the oracle database (schema=$dbuser) an check if the schema needs to be filled $dbuser = OC_CONFIG::getValue('dbuser'); //$dbname = OC_CONFIG::getValue('dbname'); $dbpass = OC_CONFIG::getValue('dbpassword'); $e_host = addslashes($dbhost); $e_dbname = addslashes($dbname); if ($e_host == '') { $easy_connect_string = $e_dbname; // use dbname as easy connect name } else { $easy_connect_string = '//' . $e_host . '/' . $e_dbname; } $connection = @oci_connect($dbuser, $dbpass, $easy_connect_string); if (!$connection) { $error[] = array('error' => 'Oracle username and/or password not valid', 'hint' => 'You need to enter either an existing account or the administrator.'); return $error; } else { $query = "SELECT count(*) FROM user_tables WHERE table_name = :un"; $stmt = oci_parse($connection, $query); $un = $dbtableprefix . 'users'; oci_bind_by_name($stmt, ':un', $un); if (!$stmt) { $entry = 'DB Error: "' . oci_last_error($connection) . '"<br />'; $entry .= 'Offending command was: ' . $query . '<br />'; echo $entry; } $result = oci_execute($stmt); if ($result) { $row = oci_fetch_row($stmt); } if (!$result or $row[0] == 0) { OC_DB::createDbFromStructure('db_structure.xml'); } } } } else { //delete the old sqlite database first, might cause infinte loops otherwise if (file_exists("{$datadir}/owncloud.db")) { unlink("{$datadir}/owncloud.db"); } //in case of sqlite, we can always fill the database OC_DB::createDbFromStructure('db_structure.xml'); } //create the user and group try { OC_User::createUser($username, $password); } catch (Exception $exception) { $error[] = $exception->getMessage(); } if (count($error) == 0) { OC_Appconfig::setValue('core', 'installedat', microtime(true)); OC_Appconfig::setValue('core', 'lastupdatedat', microtime(true)); OC_Group::createGroup('admin'); OC_Group::addToGroup($username, 'admin'); OC_User::login($username, $password); //guess what this does OC_Installer::installShippedApps(); //create htaccess files for apache hosts if (isset($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache')) { self::createHtaccess(); } //and we are done OC_Config::setValue('installed', true); } } return $error; }