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.

263 lines
10 KiB

9 years ago
9 years ago
9 years ago
9 years ago
13 years ago
13 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Manish Bisht <manish.bisht490@gmail.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Thomas Pulzer <t.pulzer@kniel.de>
  14. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC\Setup;
  32. class OCI extends AbstractDatabase {
  33. public $dbprettyname = 'Oracle';
  34. protected $dbtablespace;
  35. public function initialize($config) {
  36. parent::initialize($config);
  37. if (array_key_exists('dbtablespace', $config)) {
  38. $this->dbtablespace = $config['dbtablespace'];
  39. } else {
  40. $this->dbtablespace = 'USERS';
  41. }
  42. // allow empty hostname for oracle
  43. $this->dbHost = $config['dbhost'];
  44. $this->config->setValues([
  45. 'dbhost' => $this->dbHost,
  46. 'dbtablespace' => $this->dbtablespace,
  47. ]);
  48. }
  49. public function validate($config) {
  50. $errors = array();
  51. if(empty($config['dbuser']) && empty($config['dbname'])) {
  52. $errors[] = $this->trans->t("%s enter the database username and name.", array($this->dbprettyname));
  53. } else if(empty($config['dbuser'])) {
  54. $errors[] = $this->trans->t("%s enter the database username.", array($this->dbprettyname));
  55. } else if(empty($config['dbname'])) {
  56. $errors[] = $this->trans->t("%s enter the database name.", array($this->dbprettyname));
  57. }
  58. return $errors;
  59. }
  60. public function setupDatabase($username) {
  61. $e_host = addslashes($this->dbHost);
  62. // casting to int to avoid malicious input
  63. $e_port = (int)$this->dbPort;
  64. $e_dbname = addslashes($this->dbName);
  65. //check if the database user has admin right
  66. if ($e_host == '') {
  67. $easy_connect_string = $e_dbname; // use dbname as easy connect name
  68. } else {
  69. $easy_connect_string = '//'.$e_host.(!empty($e_port) ? ":{$e_port}" : "").'/'.$e_dbname;
  70. }
  71. $this->logger->debug('connect string: ' . $easy_connect_string, ['app' => 'setup.oci']);
  72. $connection = @oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string);
  73. if(!$connection) {
  74. $errorMessage = $this->getLastError();
  75. if ($errorMessage) {
  76. throw new \OC\DatabaseSetupException($this->trans->t('Oracle connection could not be established'),
  77. $errorMessage.' Check environment: ORACLE_HOME='.getenv('ORACLE_HOME')
  78. .' ORACLE_SID='.getenv('ORACLE_SID')
  79. .' LD_LIBRARY_PATH='.getenv('LD_LIBRARY_PATH')
  80. .' NLS_LANG='.getenv('NLS_LANG')
  81. .' tnsnames.ora is '.(is_readable(getenv('ORACLE_HOME').'/network/admin/tnsnames.ora')?'':'not ').'readable');
  82. }
  83. throw new \OC\DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'),
  84. 'Check environment: ORACLE_HOME='.getenv('ORACLE_HOME')
  85. .' ORACLE_SID='.getenv('ORACLE_SID')
  86. .' LD_LIBRARY_PATH='.getenv('LD_LIBRARY_PATH')
  87. .' NLS_LANG='.getenv('NLS_LANG')
  88. .' tnsnames.ora is '.(is_readable(getenv('ORACLE_HOME').'/network/admin/tnsnames.ora')?'':'not ').'readable');
  89. }
  90. //check for roles creation rights in oracle
  91. $query='SELECT count(*) FROM user_role_privs, role_sys_privs'
  92. ." WHERE user_role_privs.granted_role = role_sys_privs.role AND privilege = 'CREATE ROLE'";
  93. $stmt = oci_parse($connection, $query);
  94. if (!$stmt) {
  95. $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />';
  96. $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
  97. $this->logger->warning($entry, ['app' => 'setup.oci']);
  98. }
  99. $result = oci_execute($stmt);
  100. if($result) {
  101. $row = oci_fetch_row($stmt);
  102. if ($row[0] > 0) {
  103. //use the admin login data for the new database user
  104. //add prefix to the oracle user name to prevent collisions
  105. $this->dbUser='oc_'.$username;
  106. //create a new password so we don't need to store the admin config in the config file
  107. $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS);
  108. //oracle passwords are treated as identifiers:
  109. // must start with alphanumeric char
  110. // needs to be shortened to 30 bytes, as the two " needed to escape the identifier count towards the identifier length.
  111. $this->dbPassword=substr($this->dbPassword, 0, 30);
  112. $this->createDBUser($connection);
  113. }
  114. }
  115. $this->config->setValues([
  116. 'dbuser' => $this->dbUser,
  117. 'dbname' => $this->dbName,
  118. 'dbpassword' => $this->dbPassword,
  119. ]);
  120. //create the database not necessary, oracle implies user = schema
  121. //$this->createDatabase($this->dbname, $this->dbuser, $connection);
  122. //FIXME check tablespace exists: select * from user_tablespaces
  123. // the connection to dbname=oracle is not needed anymore
  124. oci_close($connection);
  125. // connect to the oracle database (schema=$this->dbuser) an check if the schema needs to be filled
  126. $this->dbUser = $this->config->getValue('dbuser');
  127. //$this->dbname = \OC_Config::getValue('dbname');
  128. $this->dbPassword = $this->config->getValue('dbpassword');
  129. $e_host = addslashes($this->dbHost);
  130. $e_dbname = addslashes($this->dbName);
  131. if ($e_host == '') {
  132. $easy_connect_string = $e_dbname; // use dbname as easy connect name
  133. } else {
  134. $easy_connect_string = '//' . $e_host . (!empty($e_port) ? ":{$e_port}" : "") . '/' . $e_dbname;
  135. }
  136. $connection = @oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string);
  137. if(!$connection) {
  138. throw new \OC\DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'),
  139. $this->trans->t('You need to enter details of an existing account.'));
  140. }
  141. $query = "SELECT count(*) FROM user_tables WHERE table_name = :un";
  142. $stmt = oci_parse($connection, $query);
  143. $un = $this->tablePrefix.'users';
  144. oci_bind_by_name($stmt, ':un', $un);
  145. if (!$stmt) {
  146. $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />';
  147. $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
  148. $this->logger->warning( $entry, ['app' => 'setup.oci']);
  149. }
  150. oci_execute($stmt);
  151. }
  152. /**
  153. * @param resource $connection
  154. */
  155. private function createDBUser($connection) {
  156. $name = $this->dbUser;
  157. $password = $this->dbPassword;
  158. $query = "SELECT * FROM all_users WHERE USERNAME = :un";
  159. $stmt = oci_parse($connection, $query);
  160. if (!$stmt) {
  161. $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />';
  162. $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
  163. $this->logger->warning($entry, ['app' => 'setup.oci']);
  164. }
  165. oci_bind_by_name($stmt, ':un', $name);
  166. $result = oci_execute($stmt);
  167. if(!$result) {
  168. $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />';
  169. $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
  170. $this->logger->warning($entry, ['app' => 'setup.oci']);
  171. }
  172. if(! oci_fetch_row($stmt)) {
  173. //user does not exists let's create it :)
  174. //password must start with alphabetic character in oracle
  175. $query = 'CREATE USER '.$name.' IDENTIFIED BY "'.$password.'" DEFAULT TABLESPACE '.$this->dbtablespace;
  176. $stmt = oci_parse($connection, $query);
  177. if (!$stmt) {
  178. $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />';
  179. $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
  180. $this->logger->warning($entry, ['app' => 'setup.oci']);
  181. }
  182. //oci_bind_by_name($stmt, ':un', $name);
  183. $result = oci_execute($stmt);
  184. if(!$result) {
  185. $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />';
  186. $entry .= $this->trans->t('Offending command was: "%s", name: %s, password: %s',
  187. array($query, $name, $password)) . '<br />';
  188. $this->logger->warning($entry, ['app' => 'setup.oci']);
  189. }
  190. } else { // change password of the existing role
  191. $query = "ALTER USER :un IDENTIFIED BY :pw";
  192. $stmt = oci_parse($connection, $query);
  193. if (!$stmt) {
  194. $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />';
  195. $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
  196. $this->logger->warning($entry, ['app' => 'setup.oci']);
  197. }
  198. oci_bind_by_name($stmt, ':un', $name);
  199. oci_bind_by_name($stmt, ':pw', $password);
  200. $result = oci_execute($stmt);
  201. if(!$result) {
  202. $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />';
  203. $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
  204. $this->logger->warning($entry, ['app' => 'setup.oci']);
  205. }
  206. }
  207. // grant necessary roles
  208. $query = 'GRANT CREATE SESSION, CREATE TABLE, CREATE SEQUENCE, CREATE TRIGGER, UNLIMITED TABLESPACE TO '.$name;
  209. $stmt = oci_parse($connection, $query);
  210. if (!$stmt) {
  211. $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />';
  212. $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
  213. $this->logger->warning($entry, ['app' => 'setup.oci']);
  214. }
  215. $result = oci_execute($stmt);
  216. if(!$result) {
  217. $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />';
  218. $entry .= $this->trans->t('Offending command was: "%s", name: %s, password: %s',
  219. array($query, $name, $password)) . '<br />';
  220. $this->logger->warning($entry, ['app' => 'setup.oci']);
  221. }
  222. }
  223. /**
  224. * @param resource $connection
  225. * @return string
  226. */
  227. protected function getLastError($connection = null) {
  228. if ($connection) {
  229. $error = oci_error($connection);
  230. } else {
  231. $error = oci_error();
  232. }
  233. foreach (array('message', 'code') as $key) {
  234. if (isset($error[$key])) {
  235. return $error[$key];
  236. }
  237. }
  238. return '';
  239. }
  240. }