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