2012-07-25 10:37:39 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2013-09-17 17:25:15 +00:00
|
|
|
|
* ownCloud – LDAP Connection
|
2012-07-25 10:37:39 +00:00
|
|
|
|
*
|
|
|
|
|
* @author Arthur Schiwon
|
2013-01-10 22:30:26 +00:00
|
|
|
|
* @copyright 2012, 2013 Arthur Schiwon blizzz@owncloud.com
|
2012-07-25 10:37:39 +00:00
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace OCA\user_ldap\lib;
|
|
|
|
|
|
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-08-14 14:03:18 +00:00
|
|
|
|
//whether connection should be kept on __destruct
|
|
|
|
|
private $dontDestruct = false;
|
2013-09-26 20:31:57 +00:00
|
|
|
|
private $hasPagedResultSupport = true;
|
2013-08-14 14:03:18 +00:00
|
|
|
|
|
2012-07-26 14:11:23 +00:00
|
|
|
|
//cache handler
|
|
|
|
|
protected $cache;
|
|
|
|
|
|
2013-09-26 20:31:57 +00:00
|
|
|
|
//settings handler
|
|
|
|
|
protected $configuration;
|
2012-07-25 10:37:39 +00:00
|
|
|
|
|
2013-10-24 18:26:05 +00:00
|
|
|
|
protected $doNotValidate = false;
|
|
|
|
|
|
2013-01-10 22:34:24 +00:00
|
|
|
|
/**
|
|
|
|
|
* @brief Constructor
|
|
|
|
|
* @param $configPrefix a string with the prefix for the configkey column (appconfig table)
|
|
|
|
|
* @param $configID a string with the value for the appid column (appconfig table) or null for on-the-fly connections
|
|
|
|
|
*/
|
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();
|
|
|
|
|
} else {
|
|
|
|
|
$this->cache = \OC_Cache::getGlobalCache();
|
|
|
|
|
}
|
2013-09-26 20:31:57 +00:00
|
|
|
|
$this->hasPagedResultSupport =
|
2013-08-18 11:33:59 +00:00
|
|
|
|
$this->ldap->hasPagedResultSupport();
|
2013-10-24 18:26:05 +00:00
|
|
|
|
$this->doNotValidate = !in_array($this->configPrefix,
|
|
|
|
|
Helper::getServerConfigurationPrefixes());
|
2012-07-25 10:37:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function __destruct() {
|
2013-08-20 10:39:24 +00:00
|
|
|
|
if(!$this->dontDestruct &&
|
|
|
|
|
$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
|
|
|
|
/**
|
|
|
|
|
* @brief defines behaviour when the instance is cloned
|
|
|
|
|
*/
|
|
|
|
|
public function __clone() {
|
|
|
|
|
//a cloned instance inherits the connection resource. It may use it,
|
|
|
|
|
//but it may not disconnect it
|
|
|
|
|
$this->dontDestruct = true;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-25 10:37:39 +00:00
|
|
|
|
/**
|
|
|
|
|
* @brief initializes the LDAP backend
|
|
|
|
|
* @param $force read the config settings no matter what
|
|
|
|
|
*
|
|
|
|
|
* initializes the LDAP backend
|
|
|
|
|
*/
|
|
|
|
|
public function init($force = false) {
|
|
|
|
|
$this->readConfiguration($force);
|
|
|
|
|
$this->establishConnection();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the LDAP handler
|
|
|
|
|
*/
|
|
|
|
|
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)) {
|
|
|
|
|
\OCP\Util::writeLog('user_ldap', 'Connection could not be established', \OCP\Util::ERROR);
|
|
|
|
|
}
|
|
|
|
|
return $this->ldapConnectionRes;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
/**
|
|
|
|
|
* @param string $key
|
|
|
|
|
*/
|
2012-07-26 14:11:23 +00:00
|
|
|
|
public function getFromCache($key) {
|
|
|
|
|
if(!$this->configured) {
|
|
|
|
|
$this->readConfiguration();
|
|
|
|
|
}
|
2013-09-26 20:31:57 +00:00
|
|
|
|
if(!$this->configuration->ldapCacheTTL) {
|
2012-07-26 14:11:23 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if(!$this->isCached($key)) {
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
$key = $this->getCacheKey($key);
|
|
|
|
|
|
|
|
|
|
return unserialize(base64_decode($this->cache->get($key)));
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-19 08:31:54 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param string $key
|
|
|
|
|
*/
|
2012-07-26 14:11:23 +00:00
|
|
|
|
public function isCached($key) {
|
|
|
|
|
if(!$this->configured) {
|
|
|
|
|
$this->readConfiguration();
|
|
|
|
|
}
|
2013-09-26 20:31:57 +00:00
|
|
|
|
if(!$this->configuration->ldapCacheTTL) {
|
2012-07-26 14:11:23 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$key = $this->getCacheKey($key);
|
|
|
|
|
return $this->cache->hasKey($key);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param string $key
|
|
|
|
|
*/
|
2012-07-26 14:11:23 +00:00
|
|
|
|
public function writeToCache($key, $value) {
|
|
|
|
|
if(!$this->configured) {
|
|
|
|
|
$this->readConfiguration();
|
|
|
|
|
}
|
2013-09-26 20:31:57 +00:00
|
|
|
|
if(!$this->configuration->ldapCacheTTL
|
|
|
|
|
|| !$this->configuration->ldapConfigurationActive) {
|
2012-07-26 14:11:23 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$key = $this->getCacheKey($key);
|
|
|
|
|
$value = base64_encode(serialize($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() {
|
|
|
|
|
$this->cache->clear($this->getCacheKey(null));
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-21 21:58:44 +00:00
|
|
|
|
/**
|
2013-09-26 20:31:57 +00:00
|
|
|
|
* @brief Caches the general LDAP configuration.
|
|
|
|
|
* @param $force optional. true, if the re-read should be forced. defaults
|
|
|
|
|
* 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
|
|
|
|
/**
|
2012-07-25 10:37:39 +00:00
|
|
|
|
* @brief set LDAP configuration with values delivered by an array, not read from configuration
|
|
|
|
|
* @param $config array that holds the config parameters in an associated array
|
|
|
|
|
* @param &$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
|
|
|
|
/**
|
2013-09-26 20:31:57 +00:00
|
|
|
|
* @brief saves the current Configuration in the database and empties the
|
|
|
|
|
* 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
|
|
|
|
/**
|
|
|
|
|
* @brief get the current LDAP configuration
|
|
|
|
|
* @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) {
|
|
|
|
|
$val = $this->configuration->$keyBase;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
$groupFilter = $this->configuration->ldapGroupFilter;
|
|
|
|
|
if(empty($groupFilter)) {
|
2013-02-14 21:16:48 +00:00
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
2013-09-26 20:31:57 +00:00
|
|
|
|
'No group filter is specified, LDAP group '.
|
|
|
|
|
'feature will not be used.',
|
|
|
|
|
\OCP\Util::INFO);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
$sakeys = array('ldapAttributesForUserSearch',
|
|
|
|
|
'ldapAttributesForGroupSearch');
|
|
|
|
|
foreach($sakeys as $key) {
|
|
|
|
|
$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
|
|
|
|
|
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
|
|
|
|
/**
|
2013-09-26 20:31:57 +00:00
|
|
|
|
* @brief Validates the user specified configuration
|
|
|
|
|
* @returns 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();
|
|
|
|
|
|
|
|
|
|
//second step: critical checks. If left empty or filled wrong, set as
|
|
|
|
|
//unconfigured and give a warning.
|
|
|
|
|
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;
|
|
|
|
|
}
|
2012-07-25 10:37:39 +00:00
|
|
|
|
if(!$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.',
|
|
|
|
|
\OCP\Util::WARN);
|
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
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-26 20:31:57 +00:00
|
|
|
|
if(!$this->configuration->ldapOverrideMainServer
|
|
|
|
|
&& !$this->getFromCache('overrideMainServer')) {
|
|
|
|
|
$this->doConnect($this->configuration->ldapHost,
|
|
|
|
|
$this->configuration->ldapPort);
|
2013-01-17 12:31:14 +00:00
|
|
|
|
$bindStatus = $this->bind();
|
2013-09-11 17:42:08 +00:00
|
|
|
|
$error = $this->ldap->isResource($this->ldapConnectionRes) ?
|
|
|
|
|
$this->ldap->errno($this->ldapConnectionRes) : -1;
|
2013-01-17 12:46:32 +00:00
|
|
|
|
} else {
|
|
|
|
|
$bindStatus = false;
|
2013-01-17 12:56:37 +00:00
|
|
|
|
$error = null;
|
2013-01-17 12:31:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//if LDAP server is not reachable, try the Backup (Replica!) Server
|
2013-05-25 09:02:51 +00:00
|
|
|
|
if((!$bindStatus && ($error !== 0))
|
2013-09-26 20:31:57 +00:00
|
|
|
|
|| $this->configuration->ldapOverrideMainServer
|
2013-01-17 12:31:14 +00:00
|
|
|
|
|| $this->getFromCache('overrideMainServer')) {
|
2013-09-26 20:31:57 +00:00
|
|
|
|
$this->doConnect($this->configuration->ldapBackupHost,
|
|
|
|
|
$this->configuration->ldapBackupPort);
|
2013-01-17 12:31:14 +00:00
|
|
|
|
$bindStatus = $this->bind();
|
2013-05-25 09:02:51 +00:00
|
|
|
|
if(!$bindStatus && $error === -1) {
|
2013-01-17 12:56:37 +00:00
|
|
|
|
//when bind to backup server succeeded and failed to main server,
|
|
|
|
|
//skip contacting him until next cache refresh
|
2013-01-17 12:31:14 +00:00
|
|
|
|
$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
|
|
|
|
|
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-06-03 21:07:32 +00:00
|
|
|
|
if(strpos($host, '://') !== false) {
|
|
|
|
|
//ldap_connect ignores port paramater when URLs are passed
|
|
|
|
|
$host .= ':' . $port;
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|
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),
|
2013-02-14 21:16:48 +00:00
|
|
|
|
\OCP\Util::ERROR);
|
2012-07-25 10:37:39 +00:00
|
|
|
|
$this->ldapConnectionRes = null;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 13:35:50 +00:00
|
|
|
|
}
|