LDAP.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. */
  174. public function search($link, $baseDN, $filter, $attr, $attrsOnly = 0, $limit = 0) {
  175. return $this->invokeLDAPMethod('search', $link, $baseDN, $filter, $attr, $attrsOnly, $limit);
  176. }
  177. /**
  178. * @param LDAP $link
  179. * @param string $userDN
  180. * @param string $password
  181. * @return bool
  182. */
  183. public function modReplace($link, $userDN, $password) {
  184. return $this->invokeLDAPMethod('mod_replace', $link, $userDN, array('userPassword' => $password));
  185. }
  186. /**
  187. * @param LDAP $link
  188. * @param string $option
  189. * @param int $value
  190. * @return bool|mixed
  191. */
  192. public function setOption($link, $option, $value) {
  193. return $this->invokeLDAPMethod('set_option', $link, $option, $value);
  194. }
  195. /**
  196. * @param LDAP $link
  197. * @return mixed|true
  198. */
  199. public function startTls($link) {
  200. return $this->invokeLDAPMethod('start_tls', $link);
  201. }
  202. /**
  203. * @param resource $link
  204. * @return bool|mixed
  205. */
  206. public function unbind($link) {
  207. return $this->invokeLDAPMethod('unbind', $link);
  208. }
  209. /**
  210. * Checks whether the server supports LDAP
  211. * @return boolean if it the case, false otherwise
  212. * */
  213. public function areLDAPFunctionsAvailable() {
  214. return function_exists('ldap_connect');
  215. }
  216. /**
  217. * Checks whether PHP supports LDAP Paged Results
  218. * @return boolean if it the case, false otherwise
  219. * */
  220. public function hasPagedResultSupport() {
  221. $hasSupport = function_exists('ldap_control_paged_result')
  222. && function_exists('ldap_control_paged_result_response');
  223. return $hasSupport;
  224. }
  225. /**
  226. * Checks whether the submitted parameter is a resource
  227. * @param Resource $resource the resource variable to check
  228. * @return bool true if it is a resource, false otherwise
  229. */
  230. public function isResource($resource) {
  231. return is_resource($resource);
  232. }
  233. /**
  234. * Checks whether the return value from LDAP is wrong or not.
  235. *
  236. * When using ldap_search we provide an array, in case multiple bases are
  237. * configured. Thus, we need to check the array elements.
  238. *
  239. * @param $result
  240. * @return bool
  241. */
  242. protected function isResultFalse($result) {
  243. if($result === false) {
  244. return true;
  245. }
  246. if($this->curFunc === 'ldap_search' && is_array($result)) {
  247. foreach ($result as $singleResult) {
  248. if($singleResult === false) {
  249. return true;
  250. }
  251. }
  252. }
  253. return false;
  254. }
  255. /**
  256. * @return mixed
  257. */
  258. protected function invokeLDAPMethod() {
  259. $arguments = func_get_args();
  260. $func = 'ldap_' . array_shift($arguments);
  261. if(function_exists($func)) {
  262. $this->preFunctionCall($func, $arguments);
  263. $result = call_user_func_array($func, $arguments);
  264. if ($this->isResultFalse($result)) {
  265. $this->postFunctionCall();
  266. }
  267. return $result;
  268. }
  269. return null;
  270. }
  271. /**
  272. * @param string $functionName
  273. * @param array $args
  274. */
  275. private function preFunctionCall($functionName, $args) {
  276. $this->curFunc = $functionName;
  277. $this->curArgs = $args;
  278. }
  279. /**
  280. * Analyzes the returned LDAP error and acts accordingly if not 0
  281. *
  282. * @param resource $resource the LDAP Connection resource
  283. * @throws ConstraintViolationException
  284. * @throws ServerNotAvailableException
  285. * @throws \Exception
  286. */
  287. private function processLDAPError($resource) {
  288. $errorCode = ldap_errno($resource);
  289. if($errorCode === 0) {
  290. return;
  291. }
  292. $errorMsg = ldap_error($resource);
  293. if($this->curFunc === 'ldap_get_entries'
  294. && $errorCode === -4) {
  295. } else if ($errorCode === 32) {
  296. //for now
  297. } else if ($errorCode === 10) {
  298. //referrals, we switch them off, but then there is AD :)
  299. } else if ($errorCode === -1) {
  300. throw new ServerNotAvailableException('Lost connection to LDAP server.');
  301. } else if ($errorCode === 52) {
  302. throw new ServerNotAvailableException('LDAP server is shutting down.');
  303. } else if ($errorCode === 48) {
  304. throw new \Exception('LDAP authentication method rejected', $errorCode);
  305. } else if ($errorCode === 1) {
  306. throw new \Exception('LDAP Operations error', $errorCode);
  307. } else if ($errorCode === 19) {
  308. ldap_get_option($this->curArgs[0], LDAP_OPT_ERROR_STRING, $extended_error);
  309. throw new ConstraintViolationException(!empty($extended_error)?$extended_error:$errorMsg, $errorCode);
  310. } else {
  311. \OC::$server->getLogger()->debug('LDAP error {message} ({code}) after calling {func}', [
  312. 'app' => 'user_ldap',
  313. 'message' => $errorMsg,
  314. 'code' => $errorCode,
  315. 'func' => $this->curFunc,
  316. ]);
  317. }
  318. }
  319. /**
  320. * Called after an ldap method is run to act on LDAP error if necessary
  321. */
  322. private function postFunctionCall() {
  323. if($this->isResource($this->curArgs[0])) {
  324. $resource = $this->curArgs[0];
  325. } else if(
  326. $this->curFunc === 'ldap_search'
  327. && is_array($this->curArgs[0])
  328. && $this->isResource($this->curArgs[0][0])
  329. ) {
  330. // we use always the same LDAP connection resource, is enough to
  331. // take the first one.
  332. $resource = $this->curArgs[0][0];
  333. } else {
  334. return;
  335. }
  336. $this->processLDAPError($resource);
  337. $this->curFunc = '';
  338. $this->curArgs = [];
  339. }
  340. }