1
0

Connection.php 19 KB

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