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.

helper.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Brice Maron <brice@bmaron.net>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Lukas Reschke <lukas@owncloud.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\user_ldap\lib;
  28. use OCA\user_ldap\lib\LDAP;
  29. use OCA\user_ldap\User_Proxy;
  30. class Helper {
  31. /**
  32. * returns prefixes for each saved LDAP/AD server configuration.
  33. * @param bool $activeConfigurations optional, whether only active configuration shall be
  34. * retrieved, defaults to false
  35. * @return array with a list of the available prefixes
  36. *
  37. * Configuration prefixes are used to set up configurations for n LDAP or
  38. * AD servers. Since configuration is stored in the database, table
  39. * appconfig under appid user_ldap, the common identifiers in column
  40. * 'configkey' have a prefix. The prefix for the very first server
  41. * configuration is empty.
  42. * Configkey Examples:
  43. * Server 1: ldap_login_filter
  44. * Server 2: s1_ldap_login_filter
  45. * Server 3: s2_ldap_login_filter
  46. *
  47. * The prefix needs to be passed to the constructor of Connection class,
  48. * except the default (first) server shall be connected to.
  49. *
  50. */
  51. public function getServerConfigurationPrefixes($activeConfigurations = false) {
  52. $referenceConfigkey = 'ldap_configuration_active';
  53. $sql = '
  54. SELECT DISTINCT `configkey`
  55. FROM `*PREFIX*appconfig`
  56. WHERE `appid` = \'user_ldap\'
  57. AND `configkey` LIKE ?
  58. ';
  59. if($activeConfigurations) {
  60. if (\OC_Config::getValue( 'dbtype', 'sqlite' ) === 'oci') {
  61. //FIXME oracle hack: need to explicitly cast CLOB to CHAR for comparison
  62. $sql .= ' AND to_char(`configvalue`)=\'1\'';
  63. } else {
  64. $sql .= ' AND `configvalue` = \'1\'';
  65. }
  66. }
  67. $stmt = \OCP\DB::prepare($sql);
  68. $serverConfigs = $stmt->execute(array('%'.$referenceConfigkey))->fetchAll();
  69. $prefixes = array();
  70. foreach($serverConfigs as $serverConfig) {
  71. $len = strlen($serverConfig['configkey']) - strlen($referenceConfigkey);
  72. $prefixes[] = substr($serverConfig['configkey'], 0, $len);
  73. }
  74. return $prefixes;
  75. }
  76. /**
  77. *
  78. * determines the host for every configured connection
  79. * @return array an array with configprefix as keys
  80. *
  81. */
  82. public function getServerConfigurationHosts() {
  83. $referenceConfigkey = 'ldap_host';
  84. $query = '
  85. SELECT DISTINCT `configkey`, `configvalue`
  86. FROM `*PREFIX*appconfig`
  87. WHERE `appid` = \'user_ldap\'
  88. AND `configkey` LIKE ?
  89. ';
  90. $query = \OCP\DB::prepare($query);
  91. $configHosts = $query->execute(array('%'.$referenceConfigkey))->fetchAll();
  92. $result = array();
  93. foreach($configHosts as $configHost) {
  94. $len = strlen($configHost['configkey']) - strlen($referenceConfigkey);
  95. $prefix = substr($configHost['configkey'], 0, $len);
  96. $result[$prefix] = $configHost['configvalue'];
  97. }
  98. return $result;
  99. }
  100. /**
  101. * deletes a given saved LDAP/AD server configuration.
  102. * @param string $prefix the configuration prefix of the config to delete
  103. * @return bool true on success, false otherwise
  104. */
  105. public function deleteServerConfiguration($prefix) {
  106. if(!in_array($prefix, self::getServerConfigurationPrefixes())) {
  107. return false;
  108. }
  109. $saveOtherConfigurations = '';
  110. if(empty($prefix)) {
  111. $saveOtherConfigurations = 'AND `configkey` NOT LIKE \'s%\'';
  112. }
  113. $query = \OCP\DB::prepare('
  114. DELETE
  115. FROM `*PREFIX*appconfig`
  116. WHERE `configkey` LIKE ?
  117. '.$saveOtherConfigurations.'
  118. AND `appid` = \'user_ldap\'
  119. AND `configkey` NOT IN (\'enabled\', \'installed_version\', \'types\', \'bgjUpdateGroupsLastRun\')
  120. ');
  121. $delRows = $query->execute(array($prefix.'%'));
  122. if(\OCP\DB::isError($delRows)) {
  123. return false;
  124. }
  125. if($delRows === 0) {
  126. return false;
  127. }
  128. return true;
  129. }
  130. /**
  131. * checks whether there is one or more disabled LDAP configurations
  132. * @throws \Exception
  133. * @return bool
  134. */
  135. public function haveDisabledConfigurations() {
  136. $all = $this->getServerConfigurationPrefixes(false);
  137. $active = $this->getServerConfigurationPrefixes(true);
  138. if(!is_array($all) || !is_array($active)) {
  139. throw new \Exception('Unexpected Return Value');
  140. }
  141. return count($all) !== count($active) || count($all) === 0;
  142. }
  143. /**
  144. * extracts the domain from a given URL
  145. * @param string $url the URL
  146. * @return string|false domain as string on success, false otherwise
  147. */
  148. public function getDomainFromURL($url) {
  149. $uinfo = parse_url($url);
  150. if(!is_array($uinfo)) {
  151. return false;
  152. }
  153. $domain = false;
  154. if(isset($uinfo['host'])) {
  155. $domain = $uinfo['host'];
  156. } else if(isset($uinfo['path'])) {
  157. $domain = $uinfo['path'];
  158. }
  159. return $domain;
  160. }
  161. /**
  162. * listens to a hook thrown by server2server sharing and replaces the given
  163. * login name by a username, if it matches an LDAP user.
  164. *
  165. * @param array $param
  166. * @throws \Exception
  167. */
  168. public static function loginName2UserName($param) {
  169. if(!isset($param['uid'])) {
  170. throw new \Exception('key uid is expected to be set in $param');
  171. }
  172. //ain't it ironic?
  173. $helper = new Helper();
  174. $configPrefixes = $helper->getServerConfigurationPrefixes(true);
  175. $ldapWrapper = new LDAP();
  176. $ocConfig = \OC::$server->getConfig();
  177. $userBackend = new User_Proxy(
  178. $configPrefixes, $ldapWrapper, $ocConfig
  179. );
  180. $uid = $userBackend->loginName2UserName($param['uid'] );
  181. if($uid !== false) {
  182. $param['uid'] = $uid;
  183. }
  184. }
  185. }