ldap.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /**
  3. * @author Alexander Bergolth <leo@strike.wu.ac.at>
  4. * @author Arthur Schiwon <blizzz@owncloud.com>
  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 Robin McCorkell <rmccorkell@karoshi.org.uk>
  9. *
  10. * @copyright Copyright (c) 2015, ownCloud, Inc.
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\user_ldap\lib;
  27. class LDAP implements ILDAPWrapper {
  28. protected $curFunc = '';
  29. protected $curArgs = array();
  30. /**
  31. * @param resource $link
  32. * @param string $dn
  33. * @param string $password
  34. * @return bool|mixed
  35. */
  36. public function bind($link, $dn, $password) {
  37. return $this->invokeLDAPMethod('bind', $link, $dn, $password);
  38. }
  39. /**
  40. * @param string $host
  41. * @param string $port
  42. * @return mixed
  43. */
  44. public function connect($host, $port) {
  45. return $this->invokeLDAPMethod('connect', $host, $port);
  46. }
  47. /**
  48. * @param LDAP $link
  49. * @param LDAP $result
  50. * @param string $cookie
  51. * @return bool|LDAP
  52. */
  53. public function controlPagedResultResponse($link, $result, &$cookie) {
  54. $this->preFunctionCall('ldap_control_paged_result_response',
  55. array($link, $result, $cookie));
  56. $result = ldap_control_paged_result_response($link, $result, $cookie);
  57. $this->postFunctionCall();
  58. return $result;
  59. }
  60. /**
  61. * @param LDAP $link
  62. * @param int $pageSize
  63. * @param bool $isCritical
  64. * @param string $cookie
  65. * @return mixed|true
  66. */
  67. public function controlPagedResult($link, $pageSize, $isCritical, $cookie) {
  68. return $this->invokeLDAPMethod('control_paged_result', $link, $pageSize,
  69. $isCritical, $cookie);
  70. }
  71. /**
  72. * @param LDAP $link
  73. * @param LDAP $result
  74. * @return mixed
  75. */
  76. public function countEntries($link, $result) {
  77. return $this->invokeLDAPMethod('count_entries', $link, $result);
  78. }
  79. /**
  80. * @param LDAP $link
  81. * @return mixed|string
  82. */
  83. public function errno($link) {
  84. return $this->invokeLDAPMethod('errno', $link);
  85. }
  86. /**
  87. * @param LDAP $link
  88. * @return int|mixed
  89. */
  90. public function error($link) {
  91. return $this->invokeLDAPMethod('error', $link);
  92. }
  93. /**
  94. * Splits DN into its component parts
  95. * @param string $dn
  96. * @param int @withAttrib
  97. * @return array|false
  98. * @link http://www.php.net/manual/en/function.ldap-explode-dn.php
  99. */
  100. public function explodeDN($dn, $withAttrib) {
  101. return $this->invokeLDAPMethod('explode_dn', $dn, $withAttrib);
  102. }
  103. /**
  104. * @param LDAP $link
  105. * @param LDAP $result
  106. * @return mixed
  107. */
  108. public function firstEntry($link, $result) {
  109. return $this->invokeLDAPMethod('first_entry', $link, $result);
  110. }
  111. /**
  112. * @param LDAP $link
  113. * @param LDAP $result
  114. * @return array|mixed
  115. */
  116. public function getAttributes($link, $result) {
  117. return $this->invokeLDAPMethod('get_attributes', $link, $result);
  118. }
  119. /**
  120. * @param LDAP $link
  121. * @param LDAP $result
  122. * @return mixed|string
  123. */
  124. public function getDN($link, $result) {
  125. return $this->invokeLDAPMethod('get_dn', $link, $result);
  126. }
  127. /**
  128. * @param LDAP $link
  129. * @param LDAP $result
  130. * @return array|mixed
  131. */
  132. public function getEntries($link, $result) {
  133. return $this->invokeLDAPMethod('get_entries', $link, $result);
  134. }
  135. /**
  136. * @param LDAP $link
  137. * @param resource $result
  138. * @return mixed|an
  139. */
  140. public function nextEntry($link, $result) {
  141. return $this->invokeLDAPMethod('next_entry', $link, $result);
  142. }
  143. /**
  144. * @param LDAP $link
  145. * @param string $baseDN
  146. * @param string $filter
  147. * @param array $attr
  148. * @return mixed
  149. */
  150. public function read($link, $baseDN, $filter, $attr) {
  151. return $this->invokeLDAPMethod('read', $link, $baseDN, $filter, $attr);
  152. }
  153. /**
  154. * @param LDAP $link
  155. * @param string $baseDN
  156. * @param string $filter
  157. * @param array $attr
  158. * @param int $attrsOnly
  159. * @param int $limit
  160. * @return mixed
  161. */
  162. public function search($link, $baseDN, $filter, $attr, $attrsOnly = 0, $limit = 0) {
  163. return $this->invokeLDAPMethod('search', $link, $baseDN, $filter, $attr, $attrsOnly, $limit);
  164. }
  165. /**
  166. * @param LDAP $link
  167. * @param string $option
  168. * @param int $value
  169. * @return bool|mixed
  170. */
  171. public function setOption($link, $option, $value) {
  172. return $this->invokeLDAPMethod('set_option', $link, $option, $value);
  173. }
  174. /**
  175. * @param LDAP $link
  176. * @param LDAP $result
  177. * @param string $sortFilter
  178. * @return mixed
  179. */
  180. public function sort($link, $result, $sortFilter) {
  181. return $this->invokeLDAPMethod('sort', $link, $result, $sortFilter);
  182. }
  183. /**
  184. * @param LDAP $link
  185. * @return mixed|true
  186. */
  187. public function startTls($link) {
  188. return $this->invokeLDAPMethod('start_tls', $link);
  189. }
  190. /**
  191. * @param resource $link
  192. * @return bool|mixed
  193. */
  194. public function unbind($link) {
  195. return $this->invokeLDAPMethod('unbind', $link);
  196. }
  197. /**
  198. * Checks whether the server supports LDAP
  199. * @return boolean if it the case, false otherwise
  200. * */
  201. public function areLDAPFunctionsAvailable() {
  202. return function_exists('ldap_connect');
  203. }
  204. /**
  205. * Checks whether PHP supports LDAP Paged Results
  206. * @return boolean if it the case, false otherwise
  207. * */
  208. public function hasPagedResultSupport() {
  209. $hasSupport = function_exists('ldap_control_paged_result')
  210. && function_exists('ldap_control_paged_result_response');
  211. return $hasSupport;
  212. }
  213. /**
  214. * Checks whether the submitted parameter is a resource
  215. * @param Resource $resource the resource variable to check
  216. * @return bool true if it is a resource, false otherwise
  217. */
  218. public function isResource($resource) {
  219. return is_resource($resource);
  220. }
  221. /**
  222. * @return mixed
  223. */
  224. private function invokeLDAPMethod() {
  225. $arguments = func_get_args();
  226. $func = 'ldap_' . array_shift($arguments);
  227. if(function_exists($func)) {
  228. $this->preFunctionCall($func, $arguments);
  229. $result = call_user_func_array($func, $arguments);
  230. if ($result === FALSE) {
  231. $this->postFunctionCall();
  232. }
  233. return $result;
  234. }
  235. }
  236. /**
  237. * @param string $functionName
  238. * @param array $args
  239. */
  240. private function preFunctionCall($functionName, $args) {
  241. $this->curFunc = $functionName;
  242. $this->curArgs = $args;
  243. }
  244. private function postFunctionCall() {
  245. if($this->isResource($this->curArgs[0])) {
  246. $errorCode = ldap_errno($this->curArgs[0]);
  247. $errorMsg = ldap_error($this->curArgs[0]);
  248. if($errorCode !== 0) {
  249. if($this->curFunc === 'ldap_sort' && $errorCode === -4) {
  250. //You can safely ignore that decoding error.
  251. //… says https://bugs.php.net/bug.php?id=18023
  252. } else if($this->curFunc === 'ldap_get_entries'
  253. && $errorCode === -4) {
  254. } else if ($errorCode === 32) {
  255. //for now
  256. } else if ($errorCode === 10) {
  257. //referrals, we switch them off, but then there is AD :)
  258. } else {
  259. \OCP\Util::writeLog('user_ldap',
  260. 'LDAP error '.$errorMsg.' (' .
  261. $errorCode.') after calling '.
  262. $this->curFunc,
  263. \OCP\Util::DEBUG);
  264. }
  265. }
  266. }
  267. $this->curFunc = '';
  268. $this->curArgs = array();
  269. }
  270. }