LDAP.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Alexander Bergolth <leo@strike.wu.ac.at>
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin McCorkell <robin@mccorkell.me.uk>
  12. * @author Roger Szabo <roger.szabo@web.de>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\User_LDAP;
  30. use OC\ServerNotAvailableException;
  31. use OCA\User_LDAP\Exceptions\ConstraintViolationException;
  32. class LDAP implements ILDAPWrapper {
  33. protected $curFunc = '';
  34. protected $curArgs = array();
  35. /**
  36. * @param resource $link
  37. * @param string $dn
  38. * @param string $password
  39. * @return bool|mixed
  40. */
  41. public function bind($link, $dn, $password) {
  42. return $this->invokeLDAPMethod('bind', $link, $dn, $password);
  43. }
  44. /**
  45. * @param string $host
  46. * @param string $port
  47. * @return mixed
  48. */
  49. public function connect($host, $port) {
  50. if(strpos($host, '://') === false) {
  51. $host = 'ldap://' . $host;
  52. }
  53. if(strpos($host, ':', strpos($host, '://') + 1) === false) {
  54. //ldap_connect ignores port parameter when URLs are passed
  55. $host .= ':' . $port;
  56. }
  57. return $this->invokeLDAPMethod('connect', $host);
  58. }
  59. /**
  60. * @param resource $link
  61. * @param resource $result
  62. * @param string $cookie
  63. * @return bool|LDAP
  64. */
  65. public function controlPagedResultResponse($link, $result, &$cookie) {
  66. $this->preFunctionCall('ldap_control_paged_result_response',
  67. array($link, $result, $cookie));
  68. $result = ldap_control_paged_result_response($link, $result, $cookie);
  69. $this->postFunctionCall();
  70. return $result;
  71. }
  72. /**
  73. * @param LDAP $link
  74. * @param int $pageSize
  75. * @param bool $isCritical
  76. * @param string $cookie
  77. * @return mixed|true
  78. */
  79. public function controlPagedResult($link, $pageSize, $isCritical, $cookie) {
  80. return $this->invokeLDAPMethod('control_paged_result', $link, $pageSize,
  81. $isCritical, $cookie);
  82. }
  83. /**
  84. * @param LDAP $link
  85. * @param LDAP $result
  86. * @return mixed
  87. */
  88. public function countEntries($link, $result) {
  89. return $this->invokeLDAPMethod('count_entries', $link, $result);
  90. }
  91. /**
  92. * @param LDAP $link
  93. * @return integer
  94. */
  95. public function errno($link) {
  96. return $this->invokeLDAPMethod('errno', $link);
  97. }
  98. /**
  99. * @param LDAP $link
  100. * @return string
  101. */
  102. public function error($link) {
  103. return $this->invokeLDAPMethod('error', $link);
  104. }
  105. /**
  106. * Splits DN into its component parts
  107. * @param string $dn
  108. * @param int @withAttrib
  109. * @return array|false
  110. * @link http://www.php.net/manual/en/function.ldap-explode-dn.php
  111. */
  112. public function explodeDN($dn, $withAttrib) {
  113. return $this->invokeLDAPMethod('explode_dn', $dn, $withAttrib);
  114. }
  115. /**
  116. * @param LDAP $link
  117. * @param LDAP $result
  118. * @return mixed
  119. */
  120. public function firstEntry($link, $result) {
  121. return $this->invokeLDAPMethod('first_entry', $link, $result);
  122. }
  123. /**
  124. * @param LDAP $link
  125. * @param LDAP $result
  126. * @return array|mixed
  127. */
  128. public function getAttributes($link, $result) {
  129. return $this->invokeLDAPMethod('get_attributes', $link, $result);
  130. }
  131. /**
  132. * @param LDAP $link
  133. * @param LDAP $result
  134. * @return mixed|string
  135. */
  136. public function getDN($link, $result) {
  137. return $this->invokeLDAPMethod('get_dn', $link, $result);
  138. }
  139. /**
  140. * @param LDAP $link
  141. * @param LDAP $result
  142. * @return array|mixed
  143. */
  144. public function getEntries($link, $result) {
  145. return $this->invokeLDAPMethod('get_entries', $link, $result);
  146. }
  147. /**
  148. * @param LDAP $link
  149. * @param resource $result
  150. * @return mixed
  151. */
  152. public function nextEntry($link, $result) {
  153. return $this->invokeLDAPMethod('next_entry', $link, $result);
  154. }
  155. /**
  156. * @param LDAP $link
  157. * @param string $baseDN
  158. * @param string $filter
  159. * @param array $attr
  160. * @return mixed
  161. */
  162. public function read($link, $baseDN, $filter, $attr) {
  163. return $this->invokeLDAPMethod('read', $link, $baseDN, $filter, $attr);
  164. }
  165. /**
  166. * @param LDAP $link
  167. * @param string $baseDN
  168. * @param string $filter
  169. * @param array $attr
  170. * @param int $attrsOnly
  171. * @param int $limit
  172. * @return mixed
  173. * @throws \Exception
  174. */
  175. public function search($link, $baseDN, $filter, $attr, $attrsOnly = 0, $limit = 0) {
  176. $oldHandler = set_error_handler(function($no, $message, $file, $line) use (&$oldHandler) {
  177. if(strpos($message, 'Partial search results returned: Sizelimit exceeded') !== false) {
  178. return true;
  179. }
  180. $oldHandler($no, $message, $file, $line);
  181. return true;
  182. });
  183. try {
  184. $result = $this->invokeLDAPMethod('search', $link, $baseDN, $filter, $attr, $attrsOnly, $limit);
  185. restore_error_handler();
  186. return $result;
  187. } catch (\Exception $e) {
  188. restore_error_handler();
  189. throw $e;
  190. }
  191. }
  192. /**
  193. * @param LDAP $link
  194. * @param string $userDN
  195. * @param string $password
  196. * @return bool
  197. */
  198. public function modReplace($link, $userDN, $password) {
  199. return $this->invokeLDAPMethod('mod_replace', $link, $userDN, array('userPassword' => $password));
  200. }
  201. /**
  202. * @param LDAP $link
  203. * @param string $option
  204. * @param int $value
  205. * @return bool|mixed
  206. */
  207. public function setOption($link, $option, $value) {
  208. return $this->invokeLDAPMethod('set_option', $link, $option, $value);
  209. }
  210. /**
  211. * @param LDAP $link
  212. * @return mixed|true
  213. */
  214. public function startTls($link) {
  215. return $this->invokeLDAPMethod('start_tls', $link);
  216. }
  217. /**
  218. * @param resource $link
  219. * @return bool|mixed
  220. */
  221. public function unbind($link) {
  222. return $this->invokeLDAPMethod('unbind', $link);
  223. }
  224. /**
  225. * Checks whether the server supports LDAP
  226. * @return boolean if it the case, false otherwise
  227. * */
  228. public function areLDAPFunctionsAvailable() {
  229. return function_exists('ldap_connect');
  230. }
  231. /**
  232. * Checks whether the submitted parameter is a resource
  233. * @param Resource $resource the resource variable to check
  234. * @return bool true if it is a resource, false otherwise
  235. */
  236. public function isResource($resource) {
  237. return is_resource($resource);
  238. }
  239. /**
  240. * Checks whether the return value from LDAP is wrong or not.
  241. *
  242. * When using ldap_search we provide an array, in case multiple bases are
  243. * configured. Thus, we need to check the array elements.
  244. *
  245. * @param $result
  246. * @return bool
  247. */
  248. protected function isResultFalse($result) {
  249. if($result === false) {
  250. return true;
  251. }
  252. if($this->curFunc === 'ldap_search' && is_array($result)) {
  253. foreach ($result as $singleResult) {
  254. if($singleResult === false) {
  255. return true;
  256. }
  257. }
  258. }
  259. return false;
  260. }
  261. /**
  262. * @return mixed
  263. */
  264. protected function invokeLDAPMethod() {
  265. $arguments = func_get_args();
  266. $func = 'ldap_' . array_shift($arguments);
  267. if(function_exists($func)) {
  268. $this->preFunctionCall($func, $arguments);
  269. $result = call_user_func_array($func, $arguments);
  270. if ($this->isResultFalse($result)) {
  271. $this->postFunctionCall();
  272. }
  273. return $result;
  274. }
  275. return null;
  276. }
  277. /**
  278. * @param string $functionName
  279. * @param array $args
  280. */
  281. private function preFunctionCall($functionName, $args) {
  282. $this->curFunc = $functionName;
  283. $this->curArgs = $args;
  284. }
  285. /**
  286. * Analyzes the returned LDAP error and acts accordingly if not 0
  287. *
  288. * @param resource $resource the LDAP Connection resource
  289. * @throws ConstraintViolationException
  290. * @throws ServerNotAvailableException
  291. * @throws \Exception
  292. */
  293. private function processLDAPError($resource) {
  294. $errorCode = ldap_errno($resource);
  295. if($errorCode === 0) {
  296. return;
  297. }
  298. $errorMsg = ldap_error($resource);
  299. if($this->curFunc === 'ldap_get_entries'
  300. && $errorCode === -4) {
  301. } else if ($errorCode === 32) {
  302. //for now
  303. } else if ($errorCode === 10) {
  304. //referrals, we switch them off, but then there is AD :)
  305. } else if ($errorCode === -1) {
  306. throw new ServerNotAvailableException('Lost connection to LDAP server.');
  307. } else if ($errorCode === 52) {
  308. throw new ServerNotAvailableException('LDAP server is shutting down.');
  309. } else if ($errorCode === 48) {
  310. throw new \Exception('LDAP authentication method rejected', $errorCode);
  311. } else if ($errorCode === 1) {
  312. throw new \Exception('LDAP Operations error', $errorCode);
  313. } else if ($errorCode === 19) {
  314. ldap_get_option($this->curArgs[0], LDAP_OPT_ERROR_STRING, $extended_error);
  315. throw new ConstraintViolationException(!empty($extended_error)?$extended_error:$errorMsg, $errorCode);
  316. } else {
  317. \OC::$server->getLogger()->debug('LDAP error {message} ({code}) after calling {func}', [
  318. 'app' => 'user_ldap',
  319. 'message' => $errorMsg,
  320. 'code' => $errorCode,
  321. 'func' => $this->curFunc,
  322. ]);
  323. }
  324. }
  325. /**
  326. * Called after an ldap method is run to act on LDAP error if necessary
  327. * @throw \Exception
  328. */
  329. private function postFunctionCall() {
  330. if($this->isResource($this->curArgs[0])) {
  331. $resource = $this->curArgs[0];
  332. } else if(
  333. $this->curFunc === 'ldap_search'
  334. && is_array($this->curArgs[0])
  335. && $this->isResource($this->curArgs[0][0])
  336. ) {
  337. // we use always the same LDAP connection resource, is enough to
  338. // take the first one.
  339. $resource = $this->curArgs[0][0];
  340. } else {
  341. return;
  342. }
  343. $this->processLDAPError($resource);
  344. $this->curFunc = '';
  345. $this->curArgs = [];
  346. }
  347. }