LDAP.php 10 KB

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