Example #1
0
	public function setupDatabase($username) {
		//check if the database user has admin right
		$masterConnectionInfo = array( "Database" => "master", "UID" => $this->dbuser, "PWD" => $this->dbpassword);

		$masterConnection = @sqlsrv_connect($this->dbhost, $masterConnectionInfo);
		if(!$masterConnection) {
			$entry = '';
			if( ($errors = sqlsrv_errors() ) != null) {
				$entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
			}
			throw new \OC\DatabaseSetupException($this->trans->t('MS SQL username and/or password not valid: %s', array($entry)),
					$this->trans->t('You need to enter either an existing account or the administrator.'));
		}

		\OC_Config::setValues([
			'dbuser'		=> $this->dbuser,
			'dbpassword'	=> $this->dbpassword,
		]);

		$this->createDBLogin($masterConnection);

		$this->createDatabase($masterConnection);

		$this->createDBUser($masterConnection);

		sqlsrv_close($masterConnection);

		$this->createDatabaseStructure();
	}
Example #2
0
 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::setValues(['dbuser' => $this->dbuser, '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 initialize($config)
 {
     $dbUser = $config['dbuser'];
     $dbPass = $config['dbpass'];
     $dbName = $config['dbname'];
     $dbHost = !empty($config['dbhost']) ? $config['dbhost'] : 'localhost';
     $dbTablePrefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_';
     \OC_Config::setValues(['dbname' => $dbName, 'dbhost' => $dbHost, 'dbtableprefix' => $dbTablePrefix]);
     $this->dbuser = $dbUser;
     $this->dbpassword = $dbPass;
     $this->dbname = $dbName;
     $this->dbhost = $dbHost;
     $this->tableprefix = $dbTablePrefix;
 }
Example #4
0
 /**
  * Sets and deletes values and writes the config.php
  *
  * @param array $configs Associative array with `key => value` pairs
  *                       If value is null, the config key will be deleted
  */
 public function setValues(array $configs)
 {
     \OC_Config::setValues($configs);
 }
Example #5
0
 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 \OC\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 \OC\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 ($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::setValues(['dbuser' => $this->dbuser, 'dbname' => $this->dbname, '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 \OC\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);
     }
 }
Example #6
0
 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_Util::generateRandomBytes(30);
         $this->createDBUser($connection);
     }
     \OC_Config::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 = \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);
     // 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);
     }
 }