2012-07-25 10:37:39 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Arthur Schiwon <blizzz@owncloud.com>
|
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* @author Lyonel Vincent <lyonel@ezix.org>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2012-07-25 10:37:39 +00:00
|
|
|
*
|
2016-01-12 14:02:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2015-03-26 10:44:34 +00:00
|
|
|
* @license AGPL-3.0
|
2012-07-25 10:37:39 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2012-07-25 10:37:39 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2012-07-25 10:37:39 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 10:44:34 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2012-07-25 10:37:39 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2012-07-25 10:37:39 +00:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2012-07-25 10:37:39 +00:00
|
|
|
namespace OCA\user_ldap\lib;
|
|
|
|
|
2015-04-01 15:12:28 +00:00
|
|
|
use OC\ServerNotAvailableException;
|
2016-05-12 14:25:14 +00:00
|
|
|
use OCA\User_LDAP\ILDAPWrapper;
|
2016-05-12 14:17:39 +00:00
|
|
|
use OCA\User_LDAP\LDAPUtility;
|
2015-04-01 15:12:28 +00:00
|
|
|
|
2014-07-01 20:02:41 +00:00
|
|
|
/**
|
2015-04-09 12:03:30 +00:00
|
|
|
* magic properties (incomplete)
|
2014-07-01 20:02:41 +00:00
|
|
|
* responsible for LDAP connections in context with the provided configuration
|
2015-01-12 15:25:11 +00:00
|
|
|
*
|
2016-03-04 23:18:34 +00:00
|
|
|
* @property string ldapHost
|
|
|
|
* @property string ldapPort holds the port number
|
2014-07-01 20:02:41 +00:00
|
|
|
* @property string ldapUserFilter
|
|
|
|
* @property string ldapUserDisplayName
|
2016-02-03 12:16:55 +00:00
|
|
|
* @property string ldapUserDisplayName2
|
2014-07-01 20:02:41 +00:00
|
|
|
* @property boolean hasPagedResultSupport
|
2014-11-05 12:05:07 +00:00
|
|
|
* @property string[] ldapBaseUsers
|
2015-01-12 15:25:11 +00:00
|
|
|
* @property int|string ldapPagingSize holds an integer
|
2015-01-28 23:15:55 +00:00
|
|
|
* @property bool|mixed|void ldapGroupMemberAssocAttr
|
2015-01-12 15:25:11 +00:00
|
|
|
*/
|
2013-09-10 15:11:02 +00:00
|
|
|
class Connection extends LDAPUtility {
|
2012-07-25 10:37:39 +00:00
|
|
|
private $ldapConnectionRes = null;
|
2013-01-10 22:30:26 +00:00
|
|
|
private $configPrefix;
|
2012-07-25 10:37:39 +00:00
|
|
|
private $configID;
|
|
|
|
private $configured = false;
|
2013-09-26 20:31:57 +00:00
|
|
|
private $hasPagedResultSupport = true;
|
2013-08-14 14:03:18 +00:00
|
|
|
|
2015-06-03 15:37:36 +00:00
|
|
|
/**
|
|
|
|
* @var bool runtime flag that indicates whether supported primary groups are available
|
|
|
|
*/
|
|
|
|
public $hasPrimaryGroups = true;
|
|
|
|
|
2012-07-26 14:11:23 +00:00
|
|
|
//cache handler
|
|
|
|
protected $cache;
|
|
|
|
|
2015-04-02 13:05:37 +00:00
|
|
|
/** @var Configuration settings handler **/
|
2013-09-26 20:31:57 +00:00
|
|
|
protected $configuration;
|
2012-07-25 10:37:39 +00:00
|
|
|
|
2013-10-24 18:26:05 +00:00
|
|
|
protected $doNotValidate = false;
|
|
|
|
|
2014-06-11 11:35:35 +00:00
|
|
|
protected $ignoreValidation = false;
|
|
|
|
|
2013-01-10 22:34:24 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Constructor
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param ILDAPWrapper $ldap
|
|
|
|
* @param string $configPrefix a string with the prefix for the configkey column (appconfig table)
|
2014-06-11 11:35:35 +00:00
|
|
|
* @param string|null $configID a string with the value for the appid column (appconfig table) or null for on-the-fly connections
|
2013-01-10 22:34:24 +00:00
|
|
|
*/
|
2013-09-10 15:11:02 +00:00
|
|
|
public function __construct(ILDAPWrapper $ldap, $configPrefix = '', $configID = 'user_ldap') {
|
|
|
|
parent::__construct($ldap);
|
2013-01-10 22:30:26 +00:00
|
|
|
$this->configPrefix = $configPrefix;
|
2012-07-25 10:37:39 +00:00
|
|
|
$this->configID = $configID;
|
2013-12-13 17:01:42 +00:00
|
|
|
$this->configuration = new Configuration($configPrefix,
|
|
|
|
!is_null($configID));
|
2014-01-09 12:54:50 +00:00
|
|
|
$memcache = \OC::$server->getMemCacheFactory();
|
2013-08-17 15:22:54 +00:00
|
|
|
if($memcache->isAvailable()) {
|
|
|
|
$this->cache = $memcache->create();
|
|
|
|
}
|
2014-08-21 15:59:13 +00:00
|
|
|
$helper = new Helper();
|
2013-10-24 18:26:05 +00:00
|
|
|
$this->doNotValidate = !in_array($this->configPrefix,
|
2014-08-21 15:59:13 +00:00
|
|
|
$helper->getServerConfigurationPrefixes());
|
2015-12-17 11:29:55 +00:00
|
|
|
$this->hasPagedResultSupport =
|
|
|
|
intval($this->configuration->ldapPagingSize) !== 0
|
|
|
|
|| $this->ldap->hasPagedResultSupport();
|
2012-07-25 10:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct() {
|
2016-04-22 19:43:41 +00:00
|
|
|
if($this->ldap->isResource($this->ldapConnectionRes)) {
|
2013-08-18 11:33:59 +00:00
|
|
|
@$this->ldap->unbind($this->ldapConnectionRes);
|
2012-10-26 11:30:07 +00:00
|
|
|
};
|
2012-07-25 10:37:39 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 14:03:18 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* defines behaviour when the instance is cloned
|
2013-08-14 14:03:18 +00:00
|
|
|
*/
|
|
|
|
public function __clone() {
|
2014-03-21 14:27:51 +00:00
|
|
|
$this->configuration = new Configuration($this->configPrefix,
|
|
|
|
!is_null($this->configID));
|
2016-04-22 19:43:41 +00:00
|
|
|
$this->ldapConnectionRes = null;
|
2013-08-14 14:03:18 +00:00
|
|
|
}
|
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
/**
|
2014-05-13 11:29:25 +00:00
|
|
|
* @param string $name
|
2014-05-11 13:17:27 +00:00
|
|
|
* @return bool|mixed|void
|
|
|
|
*/
|
2012-07-25 10:37:39 +00:00
|
|
|
public function __get($name) {
|
|
|
|
if(!$this->configured) {
|
|
|
|
$this->readConfiguration();
|
|
|
|
}
|
|
|
|
|
2013-09-26 20:31:57 +00:00
|
|
|
if($name === 'hasPagedResultSupport') {
|
|
|
|
return $this->hasPagedResultSupport;
|
2012-07-25 10:37:39 +00:00
|
|
|
}
|
2013-09-26 20:31:57 +00:00
|
|
|
|
|
|
|
return $this->configuration->$name;
|
2012-07-25 10:37:39 +00:00
|
|
|
}
|
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
/**
|
2014-05-13 11:29:25 +00:00
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
2014-05-11 13:17:27 +00:00
|
|
|
*/
|
2012-08-23 16:29:43 +00:00
|
|
|
public function __set($name, $value) {
|
2013-10-24 18:26:05 +00:00
|
|
|
$this->doNotValidate = false;
|
2013-09-26 20:31:57 +00:00
|
|
|
$before = $this->configuration->$name;
|
|
|
|
$this->configuration->$name = $value;
|
|
|
|
$after = $this->configuration->$name;
|
|
|
|
if($before !== $after) {
|
2012-08-23 16:29:43 +00:00
|
|
|
if(!empty($this->configID)) {
|
2013-09-26 20:31:57 +00:00
|
|
|
$this->configuration->saveConfiguration();
|
2012-08-23 16:29:43 +00:00
|
|
|
}
|
|
|
|
$this->validateConfiguration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 11:35:35 +00:00
|
|
|
/**
|
|
|
|
* sets whether the result of the configuration validation shall
|
|
|
|
* be ignored when establishing the connection. Used by the Wizard
|
|
|
|
* in early configuration state.
|
|
|
|
* @param bool $state
|
|
|
|
*/
|
|
|
|
public function setIgnoreValidation($state) {
|
|
|
|
$this->ignoreValidation = (bool)$state;
|
|
|
|
}
|
|
|
|
|
2012-07-25 10:37:39 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* initializes the LDAP backend
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param bool $force read the config settings no matter what
|
2012-07-25 10:37:39 +00:00
|
|
|
*/
|
|
|
|
public function init($force = false) {
|
|
|
|
$this->readConfiguration($force);
|
|
|
|
$this->establishConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Returns the LDAP handler
|
2012-07-25 10:37:39 +00:00
|
|
|
*/
|
|
|
|
public function getConnectionResource() {
|
|
|
|
if(!$this->ldapConnectionRes) {
|
|
|
|
$this->init();
|
2013-08-20 10:39:24 +00:00
|
|
|
} else if(!$this->ldap->isResource($this->ldapConnectionRes)) {
|
2012-08-22 13:22:52 +00:00
|
|
|
$this->ldapConnectionRes = null;
|
|
|
|
$this->establishConnection();
|
2012-07-25 10:37:39 +00:00
|
|
|
}
|
|
|
|
if(is_null($this->ldapConnectionRes)) {
|
2015-04-02 13:05:37 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'No LDAP Connection to server ' . $this->configuration->ldapHost, \OCP\Util::ERROR);
|
2015-04-01 15:12:28 +00:00
|
|
|
throw new ServerNotAvailableException('Connection to LDAP server could not be established');
|
2012-07-25 10:37:39 +00:00
|
|
|
}
|
|
|
|
return $this->ldapConnectionRes;
|
|
|
|
}
|
|
|
|
|
2016-03-15 23:45:30 +00:00
|
|
|
/**
|
|
|
|
* resets the connection resource
|
|
|
|
*/
|
|
|
|
public function resetConnectionResource() {
|
|
|
|
if(!is_null($this->ldapConnectionRes)) {
|
|
|
|
@$this->ldap->unbind($this->ldapConnectionRes);
|
|
|
|
$this->ldapConnectionRes = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-14 15:39:29 +00:00
|
|
|
/**
|
2014-05-13 11:29:25 +00:00
|
|
|
* @param string|null $key
|
2014-05-11 13:17:27 +00:00
|
|
|
* @return string
|
2014-04-14 15:39:29 +00:00
|
|
|
*/
|
2012-07-26 14:11:23 +00:00
|
|
|
private function getCacheKey($key) {
|
2013-01-10 22:30:26 +00:00
|
|
|
$prefix = 'LDAP-'.$this->configID.'-'.$this->configPrefix.'-';
|
2012-07-26 14:11:23 +00:00
|
|
|
if(is_null($key)) {
|
|
|
|
return $prefix;
|
|
|
|
}
|
|
|
|
return $prefix.md5($key);
|
|
|
|
}
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
2014-05-13 11:29:25 +00:00
|
|
|
* @param string $key
|
2014-05-11 13:17:27 +00:00
|
|
|
* @return mixed|null
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2012-07-26 14:11:23 +00:00
|
|
|
public function getFromCache($key) {
|
|
|
|
if(!$this->configured) {
|
|
|
|
$this->readConfiguration();
|
|
|
|
}
|
2015-05-07 12:52:44 +00:00
|
|
|
if(is_null($this->cache) || !$this->configuration->ldapCacheTTL) {
|
2012-07-26 14:11:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$key = $this->getCacheKey($key);
|
|
|
|
|
2016-01-29 06:51:16 +00:00
|
|
|
return json_decode(base64_decode($this->cache->get($key)), true);
|
2012-07-26 14:11:23 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
2014-05-13 11:29:25 +00:00
|
|
|
* @param string $key
|
|
|
|
* @param mixed $value
|
2015-05-07 12:52:44 +00:00
|
|
|
*
|
|
|
|
* @return string
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2012-07-26 14:11:23 +00:00
|
|
|
public function writeToCache($key, $value) {
|
|
|
|
if(!$this->configured) {
|
|
|
|
$this->readConfiguration();
|
|
|
|
}
|
2015-05-07 12:52:44 +00:00
|
|
|
if(is_null($this->cache)
|
|
|
|
|| !$this->configuration->ldapCacheTTL
|
2013-09-26 20:31:57 +00:00
|
|
|
|| !$this->configuration->ldapConfigurationActive) {
|
2012-07-26 14:11:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$key = $this->getCacheKey($key);
|
2015-09-02 10:12:31 +00:00
|
|
|
$value = base64_encode(json_encode($value));
|
2013-09-26 20:31:57 +00:00
|
|
|
$this->cache->set($key, $value, $this->configuration->ldapCacheTTL);
|
2012-07-26 14:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function clearCache() {
|
2015-05-07 12:52:44 +00:00
|
|
|
if(!is_null($this->cache)) {
|
|
|
|
$this->cache->clear($this->getCacheKey(null));
|
|
|
|
}
|
2012-07-26 14:11:23 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 21:58:44 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Caches the general LDAP configuration.
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param bool $force optional. true, if the re-read should be forced. defaults
|
2013-09-26 20:31:57 +00:00
|
|
|
* to false.
|
|
|
|
* @return null
|
2012-07-25 10:37:39 +00:00
|
|
|
*/
|
|
|
|
private function readConfiguration($force = false) {
|
|
|
|
if((!$this->configured || $force) && !is_null($this->configID)) {
|
2013-09-26 20:31:57 +00:00
|
|
|
$this->configuration->readConfiguration();
|
2012-07-25 10:37:39 +00:00
|
|
|
$this->configured = $this->validateConfiguration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 02:44:11 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* set LDAP configuration with values delivered by an array, not read from configuration
|
2014-05-13 11:29:25 +00:00
|
|
|
* @param array $config array that holds the config parameters in an associated array
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param array &$setParameters optional; array where the set fields will be given to
|
2014-02-06 15:30:58 +00:00
|
|
|
* @return boolean true if config validates, false otherwise. Check with $setParameters for detailed success on single parameters
|
2012-07-25 10:37:39 +00:00
|
|
|
*/
|
|
|
|
public function setConfiguration($config, &$setParameters = null) {
|
2013-09-26 20:31:57 +00:00
|
|
|
if(is_null($setParameters)) {
|
|
|
|
$setParameters = array();
|
2012-07-25 10:37:39 +00:00
|
|
|
}
|
2013-10-24 18:26:05 +00:00
|
|
|
$this->doNotValidate = false;
|
2013-09-26 20:31:57 +00:00
|
|
|
$this->configuration->setConfiguration($config, $setParameters);
|
|
|
|
if(count($setParameters) > 0) {
|
|
|
|
$this->configured = $this->validateConfiguration();
|
2012-07-25 10:37:39 +00:00
|
|
|
}
|
|
|
|
|
2013-10-24 18:26:05 +00:00
|
|
|
|
2012-07-25 10:37:39 +00:00
|
|
|
return $this->configured;
|
|
|
|
}
|
|
|
|
|
2013-01-30 02:44:11 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* saves the current Configuration in the database and empties the
|
2013-09-26 20:31:57 +00:00
|
|
|
* cache
|
|
|
|
* @return null
|
2013-01-30 02:44:11 +00:00
|
|
|
*/
|
2013-01-20 17:02:44 +00:00
|
|
|
public function saveConfiguration() {
|
2013-09-26 20:31:57 +00:00
|
|
|
$this->configuration->saveConfiguration();
|
2013-01-24 11:44:30 +00:00
|
|
|
$this->clearCache();
|
2013-01-20 17:02:44 +00:00
|
|
|
}
|
|
|
|
|
2013-01-18 12:53:26 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* get the current LDAP configuration
|
2013-01-18 12:53:26 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getConfiguration() {
|
2013-01-23 22:40:21 +00:00
|
|
|
$this->readConfiguration();
|
2013-09-26 20:31:57 +00:00
|
|
|
$config = $this->configuration->getConfiguration();
|
|
|
|
$cta = $this->configuration->getConfigTranslationArray();
|
|
|
|
$result = array();
|
|
|
|
foreach($cta as $dbkey => $configkey) {
|
|
|
|
switch($configkey) {
|
|
|
|
case 'homeFolderNamingRule':
|
|
|
|
if(strpos($config[$configkey], 'attr:') === 0) {
|
|
|
|
$result[$dbkey] = substr($config[$configkey], 5);
|
|
|
|
} else {
|
|
|
|
$result[$dbkey] = '';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'ldapBase':
|
|
|
|
case 'ldapBaseUsers':
|
|
|
|
case 'ldapBaseGroups':
|
|
|
|
case 'ldapAttributesForUserSearch':
|
|
|
|
case 'ldapAttributesForGroupSearch':
|
|
|
|
if(is_array($config[$configkey])) {
|
|
|
|
$result[$dbkey] = implode("\n", $config[$configkey]);
|
|
|
|
break;
|
|
|
|
} //else follows default
|
|
|
|
default:
|
|
|
|
$result[$dbkey] = $config[$configkey];
|
2013-01-23 22:40:21 +00:00
|
|
|
}
|
2013-01-20 17:02:44 +00:00
|
|
|
}
|
2013-09-26 20:31:57 +00:00
|
|
|
return $result;
|
2013-01-18 12:53:26 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 20:31:57 +00:00
|
|
|
private function doSoftValidation() {
|
|
|
|
//if User or Group Base are not set, take over Base DN setting
|
|
|
|
foreach(array('ldapBaseUsers', 'ldapBaseGroups') as $keyBase) {
|
2014-05-15 20:47:28 +00:00
|
|
|
$val = $this->configuration->$keyBase;
|
2013-09-26 20:31:57 +00:00
|
|
|
if(empty($val)) {
|
|
|
|
$obj = strpos('Users', $keyBase) !== false ? 'Users' : 'Groups';
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
'Base tree for '.$obj.
|
|
|
|
' is empty, using Base DN',
|
|
|
|
\OCP\Util::INFO);
|
|
|
|
$this->configuration->$keyBase = $this->configuration->ldapBase;
|
|
|
|
}
|
2012-07-25 10:37:39 +00:00
|
|
|
}
|
2013-09-26 20:31:57 +00:00
|
|
|
|
2013-10-17 17:40:59 +00:00
|
|
|
foreach(array('ldapExpertUUIDUserAttr' => 'ldapUuidUserAttribute',
|
|
|
|
'ldapExpertUUIDGroupAttr' => 'ldapUuidGroupAttribute')
|
|
|
|
as $expertSetting => $effectiveSetting) {
|
|
|
|
$uuidOverride = $this->configuration->$expertSetting;
|
|
|
|
if(!empty($uuidOverride)) {
|
|
|
|
$this->configuration->$effectiveSetting = $uuidOverride;
|
|
|
|
} else {
|
|
|
|
$uuidAttributes = array('auto', 'entryuuid', 'nsuniqueid',
|
|
|
|
'objectguid', 'guid');
|
|
|
|
if(!in_array($this->configuration->$effectiveSetting,
|
|
|
|
$uuidAttributes)
|
|
|
|
&& (!is_null($this->configID))) {
|
|
|
|
$this->configuration->$effectiveSetting = 'auto';
|
|
|
|
$this->configuration->saveConfiguration();
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
'Illegal value for the '.
|
|
|
|
$effectiveSetting.', '.'reset to '.
|
|
|
|
'autodetect.', \OCP\Util::INFO);
|
|
|
|
}
|
2013-09-26 20:31:57 +00:00
|
|
|
|
2013-10-17 17:40:59 +00:00
|
|
|
}
|
2012-08-23 16:29:43 +00:00
|
|
|
}
|
2013-09-13 17:01:40 +00:00
|
|
|
|
2013-09-26 20:31:57 +00:00
|
|
|
$backupPort = $this->configuration->ldapBackupPort;
|
|
|
|
if(empty($backupPort)) {
|
|
|
|
$this->configuration->backupPort = $this->configuration->ldapPort;
|
2013-01-17 12:56:37 +00:00
|
|
|
}
|
2013-09-26 20:31:57 +00:00
|
|
|
|
|
|
|
//make sure empty search attributes are saved as simple, empty array
|
2014-05-11 13:17:27 +00:00
|
|
|
$saKeys = array('ldapAttributesForUserSearch',
|
2013-09-26 20:31:57 +00:00
|
|
|
'ldapAttributesForGroupSearch');
|
2014-05-11 13:17:27 +00:00
|
|
|
foreach($saKeys as $key) {
|
2013-09-26 20:31:57 +00:00
|
|
|
$val = $this->configuration->$key;
|
|
|
|
if(is_array($val) && count($val) === 1 && empty($val[0])) {
|
|
|
|
$this->configuration->$key = array();
|
2013-01-31 00:46:34 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-26 20:31:57 +00:00
|
|
|
|
|
|
|
if((stripos($this->configuration->ldapHost, 'ldaps://') === 0)
|
|
|
|
&& $this->configuration->ldapTLS) {
|
|
|
|
$this->configuration->ldapTLS = false;
|
2013-02-14 21:16:48 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap',
|
2013-09-26 20:31:57 +00:00
|
|
|
'LDAPS (already using secure connection) and '.
|
|
|
|
'TLS do not work together. Switched off TLS.',
|
|
|
|
\OCP\Util::INFO);
|
2013-02-06 13:30:17 +00:00
|
|
|
}
|
2013-09-26 20:31:57 +00:00
|
|
|
}
|
2013-01-31 00:46:34 +00:00
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-09-26 20:31:57 +00:00
|
|
|
private function doCriticalValidation() {
|
2012-07-25 10:37:39 +00:00
|
|
|
$configurationOK = true;
|
2013-09-26 20:31:57 +00:00
|
|
|
$errorStr = 'Configuration Error (prefix '.
|
|
|
|
strval($this->configPrefix).'): ';
|
|
|
|
|
|
|
|
//options that shall not be empty
|
|
|
|
$options = array('ldapHost', 'ldapPort', 'ldapUserDisplayName',
|
|
|
|
'ldapGroupDisplayName', 'ldapLoginFilter');
|
|
|
|
foreach($options as $key) {
|
|
|
|
$val = $this->configuration->$key;
|
|
|
|
if(empty($val)) {
|
|
|
|
switch($key) {
|
|
|
|
case 'ldapHost':
|
|
|
|
$subj = 'LDAP Host';
|
|
|
|
break;
|
|
|
|
case 'ldapPort':
|
|
|
|
$subj = 'LDAP Port';
|
|
|
|
break;
|
|
|
|
case 'ldapUserDisplayName':
|
|
|
|
$subj = 'LDAP User Display Name';
|
|
|
|
break;
|
|
|
|
case 'ldapGroupDisplayName':
|
|
|
|
$subj = 'LDAP Group Display Name';
|
|
|
|
break;
|
|
|
|
case 'ldapLoginFilter':
|
|
|
|
$subj = 'LDAP Login Filter';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$subj = $key;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$configurationOK = false;
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
$errorStr.'No '.$subj.' given!',
|
|
|
|
\OCP\Util::WARN);
|
|
|
|
}
|
2012-07-25 10:37:39 +00:00
|
|
|
}
|
2013-09-26 20:31:57 +00:00
|
|
|
|
|
|
|
//combinations
|
|
|
|
$agent = $this->configuration->ldapAgentName;
|
|
|
|
$pwd = $this->configuration->ldapAgentPassword;
|
|
|
|
if((empty($agent) && !empty($pwd)) || (!empty($agent) && empty($pwd))) {
|
2013-02-14 21:16:48 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap',
|
2013-09-26 20:31:57 +00:00
|
|
|
$errorStr.'either no password is given for the'.
|
|
|
|
'user agent or a password is given, but not an'.
|
|
|
|
'LDAP agent.',
|
2013-02-14 21:16:48 +00:00
|
|
|
\OCP\Util::WARN);
|
2012-07-25 10:37:39 +00:00
|
|
|
$configurationOK = false;
|
|
|
|
}
|
2013-09-26 20:31:57 +00:00
|
|
|
|
|
|
|
$base = $this->configuration->ldapBase;
|
|
|
|
$baseUsers = $this->configuration->ldapBaseUsers;
|
|
|
|
$baseGroups = $this->configuration->ldapBaseGroups;
|
|
|
|
|
|
|
|
if(empty($base) && empty($baseUsers) && empty($baseGroups)) {
|
2013-02-14 21:16:48 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap',
|
2013-09-26 20:31:57 +00:00
|
|
|
$errorStr.'Not a single Base DN given.',
|
|
|
|
\OCP\Util::WARN);
|
2012-07-25 10:37:39 +00:00
|
|
|
$configurationOK = false;
|
|
|
|
}
|
2013-09-26 20:31:57 +00:00
|
|
|
|
|
|
|
if(mb_strpos($this->configuration->ldapLoginFilter, '%uid', 0, 'UTF-8')
|
|
|
|
=== false) {
|
2013-02-14 21:16:48 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap',
|
2013-09-26 20:31:57 +00:00
|
|
|
$errorStr.'login filter does not contain %uid '.
|
|
|
|
'place holder.',
|
|
|
|
\OCP\Util::WARN);
|
2012-07-25 10:37:39 +00:00
|
|
|
$configurationOK = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $configurationOK;
|
|
|
|
}
|
|
|
|
|
2013-01-18 12:35:40 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Validates the user specified configuration
|
2014-05-11 14:29:59 +00:00
|
|
|
* @return bool true if configuration seems OK, false otherwise
|
2013-01-18 12:35:40 +00:00
|
|
|
*/
|
2013-09-26 20:31:57 +00:00
|
|
|
private function validateConfiguration() {
|
2013-10-24 18:26:05 +00:00
|
|
|
|
|
|
|
if($this->doNotValidate) {
|
|
|
|
//don't do a validation if it is a new configuration with pure
|
|
|
|
//default values. Will be allowed on changes via __set or
|
|
|
|
//setConfiguration
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-26 20:31:57 +00:00
|
|
|
// first step: "soft" checks: settings that are not really
|
|
|
|
// necessary, but advisable. If left empty, give an info message
|
|
|
|
$this->doSoftValidation();
|
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
//second step: critical checks. If left empty or filled wrong, mark as
|
|
|
|
//not configured and give a warning.
|
2013-09-26 20:31:57 +00:00
|
|
|
return $this->doCriticalValidation();
|
2013-01-18 12:35:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 20:31:57 +00:00
|
|
|
|
2012-07-25 10:37:39 +00:00
|
|
|
/**
|
|
|
|
* Connects and Binds to LDAP
|
|
|
|
*/
|
|
|
|
private function establishConnection() {
|
2013-09-26 20:31:57 +00:00
|
|
|
if(!$this->configuration->ldapConfigurationActive) {
|
2013-01-24 21:39:05 +00:00
|
|
|
return null;
|
|
|
|
}
|
2012-07-25 16:40:48 +00:00
|
|
|
static $phpLDAPinstalled = true;
|
|
|
|
if(!$phpLDAPinstalled) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-11 11:35:35 +00:00
|
|
|
if(!$this->ignoreValidation && !$this->configured) {
|
2013-09-26 20:31:57 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
'Configuration is invalid, cannot connect',
|
|
|
|
\OCP\Util::WARN);
|
2012-07-25 10:37:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!$this->ldapConnectionRes) {
|
2013-08-18 11:33:59 +00:00
|
|
|
if(!$this->ldap->areLDAPFunctionsAvailable()) {
|
2012-07-25 16:40:48 +00:00
|
|
|
$phpLDAPinstalled = false;
|
2013-02-14 21:16:48 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap',
|
2013-09-26 20:31:57 +00:00
|
|
|
'function ldap_connect is not available. Make '.
|
|
|
|
'sure that the PHP ldap module is installed.',
|
|
|
|
\OCP\Util::ERROR);
|
2012-07-25 16:40:48 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-26 20:31:57 +00:00
|
|
|
if($this->configuration->turnOffCertCheck) {
|
2012-08-05 21:00:47 +00:00
|
|
|
if(putenv('LDAPTLS_REQCERT=never')) {
|
2013-02-14 21:16:48 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
'Turned off SSL certificate validation successfully.',
|
2014-12-24 15:32:27 +00:00
|
|
|
\OCP\Util::DEBUG);
|
2012-08-05 21:00:47 +00:00
|
|
|
} else {
|
2013-09-26 20:31:57 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
'Could not turn off SSL certificate validation.',
|
|
|
|
\OCP\Util::WARN);
|
2012-08-05 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-03 16:17:00 +00:00
|
|
|
|
|
|
|
$bindStatus = false;
|
2016-03-15 23:45:30 +00:00
|
|
|
$error = -1;
|
2015-11-03 16:17:00 +00:00
|
|
|
try {
|
|
|
|
if (!$this->configuration->ldapOverrideMainServer
|
|
|
|
&& !$this->getFromCache('overrideMainServer')
|
|
|
|
) {
|
|
|
|
$this->doConnect($this->configuration->ldapHost,
|
|
|
|
$this->configuration->ldapPort);
|
|
|
|
$bindStatus = $this->bind();
|
|
|
|
$error = $this->ldap->isResource($this->ldapConnectionRes) ?
|
|
|
|
$this->ldap->errno($this->ldapConnectionRes) : -1;
|
|
|
|
}
|
|
|
|
if($bindStatus === true) {
|
|
|
|
return $bindStatus;
|
|
|
|
}
|
|
|
|
} catch (\OC\ServerNotAvailableException $e) {
|
|
|
|
if(trim($this->configuration->ldapBackupHost) === "") {
|
|
|
|
throw $e;
|
|
|
|
}
|
2013-01-17 12:31:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//if LDAP server is not reachable, try the Backup (Replica!) Server
|
2015-11-03 16:17:00 +00:00
|
|
|
if( $error !== 0
|
2013-09-26 20:31:57 +00:00
|
|
|
|| $this->configuration->ldapOverrideMainServer
|
2015-11-03 16:17:00 +00:00
|
|
|
|| $this->getFromCache('overrideMainServer'))
|
|
|
|
{
|
|
|
|
$this->doConnect($this->configuration->ldapBackupHost,
|
|
|
|
$this->configuration->ldapBackupPort);
|
|
|
|
$bindStatus = $this->bind();
|
2016-03-15 23:45:30 +00:00
|
|
|
if($bindStatus && $error === -1 && !$this->getFromCache('overrideMainServer')) {
|
2015-11-03 16:17:00 +00:00
|
|
|
//when bind to backup server succeeded and failed to main server,
|
|
|
|
//skip contacting him until next cache refresh
|
|
|
|
$this->writeToCache('overrideMainServer', true);
|
|
|
|
}
|
2012-07-25 10:37:39 +00:00
|
|
|
}
|
2013-01-17 12:31:14 +00:00
|
|
|
return $bindStatus;
|
|
|
|
}
|
|
|
|
}
|
2012-07-25 10:37:39 +00:00
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
/**
|
2014-05-13 11:29:25 +00:00
|
|
|
* @param string $host
|
|
|
|
* @param string $port
|
2014-05-11 13:17:27 +00:00
|
|
|
* @return false|void
|
2015-12-04 23:43:38 +00:00
|
|
|
* @throws \OC\ServerNotAvailableException
|
2014-05-11 13:17:27 +00:00
|
|
|
*/
|
2013-01-17 12:31:14 +00:00
|
|
|
private function doConnect($host, $port) {
|
2013-02-06 13:32:00 +00:00
|
|
|
if(empty($host)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-18 11:33:59 +00:00
|
|
|
$this->ldapConnectionRes = $this->ldap->connect($host, $port);
|
2013-08-20 12:23:49 +00:00
|
|
|
if($this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) {
|
|
|
|
if($this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_REFERRALS, 0)) {
|
2013-09-26 20:31:57 +00:00
|
|
|
if($this->configuration->ldapTLS) {
|
2013-08-20 12:23:49 +00:00
|
|
|
$this->ldap->startTls($this->ldapConnectionRes);
|
2013-01-17 12:31:14 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-04 23:43:38 +00:00
|
|
|
} else {
|
|
|
|
throw new \OC\ServerNotAvailableException('Could not set required LDAP Protocol version.');
|
2012-07-25 10:37:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds to LDAP
|
|
|
|
*/
|
|
|
|
public function bind() {
|
2013-05-25 09:03:58 +00:00
|
|
|
static $getConnectionResourceAttempt = false;
|
2013-09-26 20:31:57 +00:00
|
|
|
if(!$this->configuration->ldapConfigurationActive) {
|
2013-01-24 21:39:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-25 09:03:58 +00:00
|
|
|
if($getConnectionResourceAttempt) {
|
|
|
|
$getConnectionResourceAttempt = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$getConnectionResourceAttempt = true;
|
2013-02-06 13:32:00 +00:00
|
|
|
$cr = $this->getConnectionResource();
|
2013-05-25 09:03:58 +00:00
|
|
|
$getConnectionResourceAttempt = false;
|
2013-08-20 10:39:24 +00:00
|
|
|
if(!$this->ldap->isResource($cr)) {
|
2013-02-06 13:32:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-09-11 17:42:08 +00:00
|
|
|
$ldapLogin = @$this->ldap->bind($cr,
|
2013-09-26 20:31:57 +00:00
|
|
|
$this->configuration->ldapAgentName,
|
|
|
|
$this->configuration->ldapAgentPassword);
|
2012-07-25 10:37:39 +00:00
|
|
|
if(!$ldapLogin) {
|
2013-02-14 21:16:48 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap',
|
2013-08-18 11:33:59 +00:00
|
|
|
'Bind failed: ' . $this->ldap->errno($cr) . ': ' . $this->ldap->error($cr),
|
2015-02-03 10:44:28 +00:00
|
|
|
\OCP\Util::WARN);
|
2012-07-25 10:37:39 +00:00
|
|
|
$this->ldapConnectionRes = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-19 13:35:50 +00:00
|
|
|
}
|