You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Connection.php 20KB

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