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