Connection.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Jarkko Lehtoranta <devel@jlranta.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Robin McCorkell <robin@mccorkell.me.uk>
  15. * @author Roeland Jago Douma <roeland@famdouma.nl>
  16. * @author Roger Szabo <roger.szabo@web.de>
  17. * @author root <root@localhost.localdomain>
  18. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  19. * @author Xuanwo <xuanwo@yunify.com>
  20. *
  21. * @license AGPL-3.0
  22. *
  23. * This code is free software: you can redistribute it and/or modify
  24. * it under the terms of the GNU Affero General Public License, version 3,
  25. * as published by the Free Software Foundation.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU Affero General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Affero General Public License, version 3,
  33. * along with this program. If not, see <http://www.gnu.org/licenses/>
  34. *
  35. */
  36. namespace OCA\User_LDAP;
  37. use OC\ServerNotAvailableException;
  38. use OCP\ILogger;
  39. /**
  40. * magic properties (incomplete)
  41. * responsible for LDAP connections in context with the provided configuration
  42. *
  43. * @property string ldapHost
  44. * @property string ldapPort holds the port number
  45. * @property string ldapUserFilter
  46. * @property string ldapUserDisplayName
  47. * @property string ldapUserDisplayName2
  48. * @property string ldapUserAvatarRule
  49. * @property boolean turnOnPasswordChange
  50. * @property string[] ldapBaseUsers
  51. * @property int|null ldapPagingSize holds an integer
  52. * @property bool|mixed|void ldapGroupMemberAssocAttr
  53. * @property string ldapUuidUserAttribute
  54. * @property string ldapUuidGroupAttribute
  55. * @property string ldapExpertUUIDUserAttr
  56. * @property string ldapExpertUUIDGroupAttr
  57. * @property string ldapQuotaAttribute
  58. * @property string ldapQuotaDefault
  59. * @property string ldapEmailAttribute
  60. * @property string ldapExtStorageHomeAttribute
  61. * @property string homeFolderNamingRule
  62. * @property bool|string ldapNestedGroups
  63. * @property string[] ldapBaseGroups
  64. * @property string ldapGroupFilter
  65. * @property string ldapGroupDisplayName
  66. * @property string ldapMatchingRuleInChainState
  67. */
  68. class Connection extends LDAPUtility {
  69. private $ldapConnectionRes = null;
  70. private $configPrefix;
  71. private $configID;
  72. private $configured = false;
  73. //whether connection should be kept on __destruct
  74. private $dontDestruct = false;
  75. /**
  76. * @var bool runtime flag that indicates whether supported primary groups are available
  77. */
  78. public $hasPrimaryGroups = true;
  79. /**
  80. * @var bool runtime flag that indicates whether supported POSIX gidNumber are available
  81. */
  82. public $hasGidNumber = true;
  83. //cache handler
  84. protected $cache;
  85. /** @var Configuration settings handler **/
  86. protected $configuration;
  87. protected $doNotValidate = false;
  88. protected $ignoreValidation = false;
  89. protected $bindResult = [];
  90. /**
  91. * Constructor
  92. * @param ILDAPWrapper $ldap
  93. * @param string $configPrefix a string with the prefix for the configkey column (appconfig table)
  94. * @param string|null $configID a string with the value for the appid column (appconfig table) or null for on-the-fly connections
  95. */
  96. public function __construct(ILDAPWrapper $ldap, $configPrefix = '', $configID = 'user_ldap') {
  97. parent::__construct($ldap);
  98. $this->configPrefix = $configPrefix;
  99. $this->configID = $configID;
  100. $this->configuration = new Configuration($configPrefix,
  101. !is_null($configID));
  102. $memcache = \OC::$server->getMemCacheFactory();
  103. if($memcache->isAvailable()) {
  104. $this->cache = $memcache->createDistributed();
  105. }
  106. $helper = new Helper(\OC::$server->getConfig());
  107. $this->doNotValidate = !in_array($this->configPrefix,
  108. $helper->getServerConfigurationPrefixes());
  109. }
  110. public function __destruct() {
  111. if(!$this->dontDestruct && $this->ldap->isResource($this->ldapConnectionRes)) {
  112. @$this->ldap->unbind($this->ldapConnectionRes);
  113. $this->bindResult = [];
  114. }
  115. }
  116. /**
  117. * defines behaviour when the instance is cloned
  118. */
  119. public function __clone() {
  120. $this->configuration = new Configuration($this->configPrefix,
  121. !is_null($this->configID));
  122. if(count($this->bindResult) !== 0 && $this->bindResult['result'] === true) {
  123. $this->bindResult = [];
  124. }
  125. $this->ldapConnectionRes = null;
  126. $this->dontDestruct = true;
  127. }
  128. /**
  129. * @param string $name
  130. * @return bool|mixed
  131. */
  132. public function __get($name) {
  133. if(!$this->configured) {
  134. $this->readConfiguration();
  135. }
  136. return $this->configuration->$name;
  137. }
  138. /**
  139. * @param string $name
  140. * @param mixed $value
  141. */
  142. public function __set($name, $value) {
  143. $this->doNotValidate = false;
  144. $before = $this->configuration->$name;
  145. $this->configuration->$name = $value;
  146. $after = $this->configuration->$name;
  147. if($before !== $after) {
  148. if ($this->configID !== '' && $this->configID !== null) {
  149. $this->configuration->saveConfiguration();
  150. }
  151. $this->validateConfiguration();
  152. }
  153. }
  154. /**
  155. * @param string $rule
  156. * @return array
  157. * @throws \RuntimeException
  158. */
  159. public function resolveRule($rule) {
  160. return $this->configuration->resolveRule($rule);
  161. }
  162. /**
  163. * sets whether the result of the configuration validation shall
  164. * be ignored when establishing the connection. Used by the Wizard
  165. * in early configuration state.
  166. * @param bool $state
  167. */
  168. public function setIgnoreValidation($state) {
  169. $this->ignoreValidation = (bool)$state;
  170. }
  171. /**
  172. * initializes the LDAP backend
  173. * @param bool $force read the config settings no matter what
  174. */
  175. public function init($force = false) {
  176. $this->readConfiguration($force);
  177. $this->establishConnection();
  178. }
  179. /**
  180. * Returns the LDAP handler
  181. */
  182. public function getConnectionResource() {
  183. if(!$this->ldapConnectionRes) {
  184. $this->init();
  185. } else if(!$this->ldap->isResource($this->ldapConnectionRes)) {
  186. $this->ldapConnectionRes = null;
  187. $this->establishConnection();
  188. }
  189. if(is_null($this->ldapConnectionRes)) {
  190. \OCP\Util::writeLog('user_ldap', 'No LDAP Connection to server ' . $this->configuration->ldapHost, ILogger::ERROR);
  191. throw new ServerNotAvailableException('Connection to LDAP server could not be established');
  192. }
  193. return $this->ldapConnectionRes;
  194. }
  195. /**
  196. * resets the connection resource
  197. */
  198. public function resetConnectionResource() {
  199. if(!is_null($this->ldapConnectionRes)) {
  200. @$this->ldap->unbind($this->ldapConnectionRes);
  201. $this->ldapConnectionRes = null;
  202. $this->bindResult = [];
  203. }
  204. }
  205. /**
  206. * @param string|null $key
  207. * @return string
  208. */
  209. private function getCacheKey($key) {
  210. $prefix = 'LDAP-'.$this->configID.'-'.$this->configPrefix.'-';
  211. if(is_null($key)) {
  212. return $prefix;
  213. }
  214. return $prefix.hash('sha256', $key);
  215. }
  216. /**
  217. * @param string $key
  218. * @return mixed|null
  219. */
  220. public function getFromCache($key) {
  221. if(!$this->configured) {
  222. $this->readConfiguration();
  223. }
  224. if(is_null($this->cache) || !$this->configuration->ldapCacheTTL) {
  225. return null;
  226. }
  227. $key = $this->getCacheKey($key);
  228. return json_decode(base64_decode($this->cache->get($key)), true);
  229. }
  230. /**
  231. * @param string $key
  232. * @param mixed $value
  233. *
  234. * @return string
  235. */
  236. public function writeToCache($key, $value) {
  237. if(!$this->configured) {
  238. $this->readConfiguration();
  239. }
  240. if(is_null($this->cache)
  241. || !$this->configuration->ldapCacheTTL
  242. || !$this->configuration->ldapConfigurationActive) {
  243. return null;
  244. }
  245. $key = $this->getCacheKey($key);
  246. $value = base64_encode(json_encode($value));
  247. $this->cache->set($key, $value, $this->configuration->ldapCacheTTL);
  248. }
  249. public function clearCache() {
  250. if(!is_null($this->cache)) {
  251. $this->cache->clear($this->getCacheKey(null));
  252. }
  253. }
  254. /**
  255. * Caches the general LDAP configuration.
  256. * @param bool $force optional. true, if the re-read should be forced. defaults
  257. * to false.
  258. * @return null
  259. */
  260. private function readConfiguration($force = false) {
  261. if((!$this->configured || $force) && !is_null($this->configID)) {
  262. $this->configuration->readConfiguration();
  263. $this->configured = $this->validateConfiguration();
  264. }
  265. }
  266. /**
  267. * set LDAP configuration with values delivered by an array, not read from configuration
  268. * @param array $config array that holds the config parameters in an associated array
  269. * @param array &$setParameters optional; array where the set fields will be given to
  270. * @return boolean true if config validates, false otherwise. Check with $setParameters for detailed success on single parameters
  271. */
  272. public function setConfiguration($config, &$setParameters = null) {
  273. if(is_null($setParameters)) {
  274. $setParameters = array();
  275. }
  276. $this->doNotValidate = false;
  277. $this->configuration->setConfiguration($config, $setParameters);
  278. if(count($setParameters) > 0) {
  279. $this->configured = $this->validateConfiguration();
  280. }
  281. return $this->configured;
  282. }
  283. /**
  284. * saves the current Configuration in the database and empties the
  285. * cache
  286. * @return null
  287. */
  288. public function saveConfiguration() {
  289. $this->configuration->saveConfiguration();
  290. $this->clearCache();
  291. }
  292. /**
  293. * get the current LDAP configuration
  294. * @return array
  295. */
  296. public function getConfiguration() {
  297. $this->readConfiguration();
  298. $config = $this->configuration->getConfiguration();
  299. $cta = $this->configuration->getConfigTranslationArray();
  300. $result = array();
  301. foreach($cta as $dbkey => $configkey) {
  302. switch($configkey) {
  303. case 'homeFolderNamingRule':
  304. if(strpos($config[$configkey], 'attr:') === 0) {
  305. $result[$dbkey] = substr($config[$configkey], 5);
  306. } else {
  307. $result[$dbkey] = '';
  308. }
  309. break;
  310. case 'ldapBase':
  311. case 'ldapBaseUsers':
  312. case 'ldapBaseGroups':
  313. case 'ldapAttributesForUserSearch':
  314. case 'ldapAttributesForGroupSearch':
  315. if(is_array($config[$configkey])) {
  316. $result[$dbkey] = implode("\n", $config[$configkey]);
  317. break;
  318. } //else follows default
  319. default:
  320. $result[$dbkey] = $config[$configkey];
  321. }
  322. }
  323. return $result;
  324. }
  325. private function doSoftValidation() {
  326. //if User or Group Base are not set, take over Base DN setting
  327. foreach(array('ldapBaseUsers', 'ldapBaseGroups') as $keyBase) {
  328. $val = $this->configuration->$keyBase;
  329. if(empty($val)) {
  330. $this->configuration->$keyBase = $this->configuration->ldapBase;
  331. }
  332. }
  333. foreach(array('ldapExpertUUIDUserAttr' => 'ldapUuidUserAttribute',
  334. 'ldapExpertUUIDGroupAttr' => 'ldapUuidGroupAttribute')
  335. as $expertSetting => $effectiveSetting) {
  336. $uuidOverride = $this->configuration->$expertSetting;
  337. if(!empty($uuidOverride)) {
  338. $this->configuration->$effectiveSetting = $uuidOverride;
  339. } else {
  340. $uuidAttributes = Access::UUID_ATTRIBUTES;
  341. array_unshift($uuidAttributes, 'auto');
  342. if(!in_array($this->configuration->$effectiveSetting,
  343. $uuidAttributes)
  344. && (!is_null($this->configID))) {
  345. $this->configuration->$effectiveSetting = 'auto';
  346. $this->configuration->saveConfiguration();
  347. \OCP\Util::writeLog('user_ldap',
  348. 'Illegal value for the '.
  349. $effectiveSetting.', '.'reset to '.
  350. 'autodetect.', ILogger::INFO);
  351. }
  352. }
  353. }
  354. $backupPort = (int)$this->configuration->ldapBackupPort;
  355. if ($backupPort <= 0) {
  356. $this->configuration->backupPort = $this->configuration->ldapPort;
  357. }
  358. //make sure empty search attributes are saved as simple, empty array
  359. $saKeys = array('ldapAttributesForUserSearch',
  360. 'ldapAttributesForGroupSearch');
  361. foreach($saKeys as $key) {
  362. $val = $this->configuration->$key;
  363. if(is_array($val) && count($val) === 1 && empty($val[0])) {
  364. $this->configuration->$key = array();
  365. }
  366. }
  367. if((stripos($this->configuration->ldapHost, 'ldaps://') === 0)
  368. && $this->configuration->ldapTLS) {
  369. $this->configuration->ldapTLS = false;
  370. \OCP\Util::writeLog(
  371. 'user_ldap',
  372. 'LDAPS (already using secure connection) and TLS do not work together. Switched off TLS.',
  373. ILogger::INFO
  374. );
  375. }
  376. }
  377. /**
  378. * @return bool
  379. */
  380. private function doCriticalValidation() {
  381. $configurationOK = true;
  382. $errorStr = 'Configuration Error (prefix '.
  383. (string)$this->configPrefix .'): ';
  384. //options that shall not be empty
  385. $options = array('ldapHost', 'ldapPort', 'ldapUserDisplayName',
  386. 'ldapGroupDisplayName', 'ldapLoginFilter');
  387. foreach($options as $key) {
  388. $val = $this->configuration->$key;
  389. if(empty($val)) {
  390. switch($key) {
  391. case 'ldapHost':
  392. $subj = 'LDAP Host';
  393. break;
  394. case 'ldapPort':
  395. $subj = 'LDAP Port';
  396. break;
  397. case 'ldapUserDisplayName':
  398. $subj = 'LDAP User Display Name';
  399. break;
  400. case 'ldapGroupDisplayName':
  401. $subj = 'LDAP Group Display Name';
  402. break;
  403. case 'ldapLoginFilter':
  404. $subj = 'LDAP Login Filter';
  405. break;
  406. default:
  407. $subj = $key;
  408. break;
  409. }
  410. $configurationOK = false;
  411. \OCP\Util::writeLog(
  412. 'user_ldap',
  413. $errorStr.'No '.$subj.' given!',
  414. ILogger::WARN
  415. );
  416. }
  417. }
  418. //combinations
  419. $agent = $this->configuration->ldapAgentName;
  420. $pwd = $this->configuration->ldapAgentPassword;
  421. if (
  422. ($agent === '' && $pwd !== '')
  423. || ($agent !== '' && $pwd === '')
  424. ) {
  425. \OCP\Util::writeLog(
  426. 'user_ldap',
  427. $errorStr.'either no password is given for the user ' .
  428. 'agent or a password is given, but not an LDAP agent.',
  429. ILogger::WARN);
  430. $configurationOK = false;
  431. }
  432. $base = $this->configuration->ldapBase;
  433. $baseUsers = $this->configuration->ldapBaseUsers;
  434. $baseGroups = $this->configuration->ldapBaseGroups;
  435. if(empty($base) && empty($baseUsers) && empty($baseGroups)) {
  436. \OCP\Util::writeLog(
  437. 'user_ldap',
  438. $errorStr.'Not a single Base DN given.',
  439. ILogger::WARN
  440. );
  441. $configurationOK = false;
  442. }
  443. if(mb_strpos($this->configuration->ldapLoginFilter, '%uid', 0, 'UTF-8')
  444. === false) {
  445. \OCP\Util::writeLog(
  446. 'user_ldap',
  447. $errorStr.'login filter does not contain %uid place holder.',
  448. ILogger::WARN
  449. );
  450. $configurationOK = false;
  451. }
  452. return $configurationOK;
  453. }
  454. /**
  455. * Validates the user specified configuration
  456. * @return bool true if configuration seems OK, false otherwise
  457. */
  458. private function validateConfiguration() {
  459. if($this->doNotValidate) {
  460. //don't do a validation if it is a new configuration with pure
  461. //default values. Will be allowed on changes via __set or
  462. //setConfiguration
  463. return false;
  464. }
  465. // first step: "soft" checks: settings that are not really
  466. // necessary, but advisable. If left empty, give an info message
  467. $this->doSoftValidation();
  468. //second step: critical checks. If left empty or filled wrong, mark as
  469. //not configured and give a warning.
  470. return $this->doCriticalValidation();
  471. }
  472. /**
  473. * Connects and Binds to LDAP
  474. *
  475. * @throws ServerNotAvailableException
  476. */
  477. private function establishConnection() {
  478. if(!$this->configuration->ldapConfigurationActive) {
  479. return null;
  480. }
  481. static $phpLDAPinstalled = true;
  482. if(!$phpLDAPinstalled) {
  483. return false;
  484. }
  485. if(!$this->ignoreValidation && !$this->configured) {
  486. \OCP\Util::writeLog(
  487. 'user_ldap',
  488. 'Configuration is invalid, cannot connect',
  489. ILogger::WARN
  490. );
  491. return false;
  492. }
  493. if(!$this->ldapConnectionRes) {
  494. if(!$this->ldap->areLDAPFunctionsAvailable()) {
  495. $phpLDAPinstalled = false;
  496. \OCP\Util::writeLog(
  497. 'user_ldap',
  498. 'function ldap_connect is not available. Make sure that the PHP ldap module is installed.',
  499. ILogger::ERROR
  500. );
  501. return false;
  502. }
  503. if($this->configuration->turnOffCertCheck) {
  504. if(putenv('LDAPTLS_REQCERT=never')) {
  505. \OCP\Util::writeLog('user_ldap',
  506. 'Turned off SSL certificate validation successfully.',
  507. ILogger::DEBUG);
  508. } else {
  509. \OCP\Util::writeLog(
  510. 'user_ldap',
  511. 'Could not turn off SSL certificate validation.',
  512. ILogger::WARN
  513. );
  514. }
  515. }
  516. $isOverrideMainServer = ($this->configuration->ldapOverrideMainServer
  517. || $this->getFromCache('overrideMainServer'));
  518. $isBackupHost = (trim($this->configuration->ldapBackupHost) !== "");
  519. $bindStatus = false;
  520. try {
  521. if (!$isOverrideMainServer) {
  522. $this->doConnect($this->configuration->ldapHost,
  523. $this->configuration->ldapPort);
  524. return $this->bind();
  525. }
  526. } catch (ServerNotAvailableException $e) {
  527. if(!$isBackupHost) {
  528. throw $e;
  529. }
  530. }
  531. //if LDAP server is not reachable, try the Backup (Replica!) Server
  532. if($isBackupHost || $isOverrideMainServer) {
  533. $this->doConnect($this->configuration->ldapBackupHost,
  534. $this->configuration->ldapBackupPort);
  535. $this->bindResult = [];
  536. $bindStatus = $this->bind();
  537. $error = $this->ldap->isResource($this->ldapConnectionRes) ?
  538. $this->ldap->errno($this->ldapConnectionRes) : -1;
  539. if($bindStatus && $error === 0 && !$this->getFromCache('overrideMainServer')) {
  540. //when bind to backup server succeeded and failed to main server,
  541. //skip contacting him until next cache refresh
  542. $this->writeToCache('overrideMainServer', true);
  543. }
  544. }
  545. return $bindStatus;
  546. }
  547. return null;
  548. }
  549. /**
  550. * @param string $host
  551. * @param string $port
  552. * @return bool
  553. * @throws \OC\ServerNotAvailableException
  554. */
  555. private function doConnect($host, $port) {
  556. if ($host === '') {
  557. return false;
  558. }
  559. $this->ldapConnectionRes = $this->ldap->connect($host, $port);
  560. if(!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) {
  561. throw new ServerNotAvailableException('Could not set required LDAP Protocol version.');
  562. }
  563. if(!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_REFERRALS, 0)) {
  564. throw new ServerNotAvailableException('Could not disable LDAP referrals.');
  565. }
  566. if($this->configuration->ldapTLS) {
  567. if(!$this->ldap->startTls($this->ldapConnectionRes)) {
  568. throw new ServerNotAvailableException('Start TLS failed, when connecting to LDAP host ' . $host . '.');
  569. }
  570. }
  571. return true;
  572. }
  573. /**
  574. * Binds to LDAP
  575. */
  576. public function bind() {
  577. if(!$this->configuration->ldapConfigurationActive) {
  578. return false;
  579. }
  580. $cr = $this->ldapConnectionRes;
  581. if(!$this->ldap->isResource($cr)) {
  582. $cr = $this->getConnectionResource();
  583. }
  584. if(
  585. count($this->bindResult) !== 0
  586. && $this->bindResult['dn'] === $this->configuration->ldapAgentName
  587. && \OC::$server->getHasher()->verify(
  588. $this->configPrefix . $this->configuration->ldapAgentPassword,
  589. $this->bindResult['hash']
  590. )
  591. ) {
  592. // don't attempt to bind again with the same data as before
  593. // bind might have been invoked via getConnectionResource(),
  594. // but we need results specifically for e.g. user login
  595. return $this->bindResult['result'];
  596. }
  597. $ldapLogin = @$this->ldap->bind($cr,
  598. $this->configuration->ldapAgentName,
  599. $this->configuration->ldapAgentPassword);
  600. $this->bindResult = [
  601. 'dn' => $this->configuration->ldapAgentName,
  602. 'hash' => \OC::$server->getHasher()->hash($this->configPrefix . $this->configuration->ldapAgentPassword),
  603. 'result' => $ldapLogin,
  604. ];
  605. if(!$ldapLogin) {
  606. $errno = $this->ldap->errno($cr);
  607. \OCP\Util::writeLog('user_ldap',
  608. 'Bind failed: ' . $errno . ': ' . $this->ldap->error($cr),
  609. ILogger::WARN);
  610. // Set to failure mode, if LDAP error code is not LDAP_SUCCESS or LDAP_INVALID_CREDENTIALS
  611. // or (needed for Apple Open Directory:) LDAP_INSUFFICIENT_ACCESS
  612. if($errno !== 0 && $errno !== 49 && $errno !== 50) {
  613. $this->ldapConnectionRes = null;
  614. }
  615. return false;
  616. }
  617. return true;
  618. }
  619. }