2012-04-14 16:05:29 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 14:49:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Aaron Wood <aaronjwood@gmail.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Alexander Bergolth <leo@strike.wu.ac.at>
|
|
|
|
* @author Andreas Fischer <bantu@owncloud.com>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Benjamin Diele <benjamin@diele.be>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author bline <scottbeck@gmail.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Christopher Schäpers <kondou@ts.unde.re>
|
2016-07-21 14:49:16 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Juan Pablo Villafáñez <jvillafanez@solidgear.es>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Lorenzo M. Catucci <lorenzo@sancho.ccd.uniroma2.it>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Lyonel Vincent <lyonel@ezix.org>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author Mario Kolling <mario.kolling@serpro.gov.br>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-10-05 18:54:56 +00:00
|
|
|
* @author Nicolas Grekas <nicolas.grekas@gmail.com>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author Ralph Krimmel <rkrimme1@gwdg.de>
|
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2016-07-22 08:46:29 +00:00
|
|
|
* @author Roger Szabo <roger.szabo@web.de>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author root <root@localhost.localdomain>
|
|
|
|
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
|
2012-04-14 16:05:29 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @license AGPL-3.0
|
2012-04-14 16:05:29 +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-04-14 16:05:29 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2012-04-14 16:05:29 +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-04-14 16:05:29 +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-04-14 16:05:29 +00:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2016-05-12 14:42:57 +00:00
|
|
|
namespace OCA\User_LDAP;
|
2012-04-15 11:37:35 +00:00
|
|
|
|
2016-08-30 09:43:29 +00:00
|
|
|
use OC\HintException;
|
2018-03-19 14:53:30 +00:00
|
|
|
use OC\Hooks\PublicEmitter;
|
2016-08-30 09:43:29 +00:00
|
|
|
use OCA\User_LDAP\Exceptions\ConstraintViolationException;
|
2016-05-12 14:09:00 +00:00
|
|
|
use OCA\User_LDAP\User\Manager;
|
2016-05-12 09:25:50 +00:00
|
|
|
use OCA\User_LDAP\User\OfflineUser;
|
2014-11-05 12:05:07 +00:00
|
|
|
use OCA\User_LDAP\Mapping\AbstractMapping;
|
2017-05-20 07:53:46 +00:00
|
|
|
use OC\ServerNotAvailableException;
|
2017-11-08 00:51:14 +00:00
|
|
|
use OCP\IConfig;
|
2018-04-25 13:22:28 +00:00
|
|
|
use OCP\ILogger;
|
2018-03-19 14:53:30 +00:00
|
|
|
use OCP\IUserManager;
|
2017-05-20 07:53:46 +00:00
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
/**
|
|
|
|
* Class Access
|
2016-05-12 14:42:57 +00:00
|
|
|
* @package OCA\User_LDAP
|
2014-05-11 13:17:27 +00:00
|
|
|
*/
|
2018-10-30 21:42:24 +00:00
|
|
|
class Access extends LDAPUtility {
|
2017-10-31 18:42:17 +00:00
|
|
|
const UUID_ATTRIBUTES = ['entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid'];
|
|
|
|
|
2017-10-31 11:20:44 +00:00
|
|
|
/** @var \OCA\User_LDAP\Connection */
|
2013-09-10 15:11:02 +00:00
|
|
|
public $connection;
|
2016-10-06 23:39:57 +00:00
|
|
|
/** @var Manager */
|
2014-03-27 17:01:14 +00:00
|
|
|
public $userManager;
|
2012-10-26 19:53:56 +00:00
|
|
|
//never ever check this var directly, always use getPagedSearchResultState
|
|
|
|
protected $pagedSearchedSuccessful;
|
|
|
|
|
2014-08-19 16:01:58 +00:00
|
|
|
/**
|
|
|
|
* @var string[] $cookies an array of returned Paged Result cookies
|
|
|
|
*/
|
2013-10-25 12:15:05 +00:00
|
|
|
protected $cookies = array();
|
|
|
|
|
2014-08-19 16:01:58 +00:00
|
|
|
/**
|
|
|
|
* @var string $lastCookie the last cookie returned from a Paged Results
|
|
|
|
* operation, defaults to an empty string
|
|
|
|
*/
|
|
|
|
protected $lastCookie = '';
|
2014-06-03 11:01:14 +00:00
|
|
|
|
2014-11-05 12:05:07 +00:00
|
|
|
/**
|
|
|
|
* @var AbstractMapping $userMapper
|
|
|
|
*/
|
|
|
|
protected $userMapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var AbstractMapping $userMapper
|
|
|
|
*/
|
|
|
|
protected $groupMapper;
|
2017-10-31 11:20:44 +00:00
|
|
|
|
2016-07-22 08:46:29 +00:00
|
|
|
/**
|
|
|
|
* @var \OCA\User_LDAP\Helper
|
|
|
|
*/
|
|
|
|
private $helper;
|
2017-11-08 00:51:14 +00:00
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
2018-03-19 14:53:30 +00:00
|
|
|
/** @var IUserManager */
|
|
|
|
private $ncUserManager;
|
2017-10-31 17:13:48 +00:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
Connection $connection,
|
|
|
|
ILDAPWrapper $ldap,
|
|
|
|
Manager $userManager,
|
|
|
|
Helper $helper,
|
2018-03-19 14:53:30 +00:00
|
|
|
IConfig $config,
|
|
|
|
IUserManager $ncUserManager
|
2017-10-31 17:13:48 +00:00
|
|
|
) {
|
2013-09-10 15:11:02 +00:00
|
|
|
parent::__construct($ldap);
|
2012-07-25 10:37:39 +00:00
|
|
|
$this->connection = $connection;
|
2014-03-27 17:01:14 +00:00
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->userManager->setLdapAccess($this);
|
2016-07-22 08:46:29 +00:00
|
|
|
$this->helper = $helper;
|
2017-11-08 00:51:14 +00:00
|
|
|
$this->config = $config;
|
2018-03-19 14:53:30 +00:00
|
|
|
$this->ncUserManager = $ncUserManager;
|
2012-07-25 10:37:39 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 12:05:07 +00:00
|
|
|
/**
|
|
|
|
* sets the User Mapper
|
|
|
|
* @param AbstractMapping $mapper
|
|
|
|
*/
|
|
|
|
public function setUserMapper(AbstractMapping $mapper) {
|
|
|
|
$this->userMapper = $mapper;
|
|
|
|
}
|
|
|
|
|
2014-12-20 15:09:04 +00:00
|
|
|
/**
|
|
|
|
* returns the User Mapper
|
|
|
|
* @throws \Exception
|
|
|
|
* @return AbstractMapping
|
|
|
|
*/
|
|
|
|
public function getUserMapper() {
|
|
|
|
if(is_null($this->userMapper)) {
|
|
|
|
throw new \Exception('UserMapper was not assigned to this Access instance.');
|
|
|
|
}
|
|
|
|
return $this->userMapper;
|
|
|
|
}
|
|
|
|
|
2014-11-05 12:05:07 +00:00
|
|
|
/**
|
|
|
|
* sets the Group Mapper
|
|
|
|
* @param AbstractMapping $mapper
|
|
|
|
*/
|
|
|
|
public function setGroupMapper(AbstractMapping $mapper) {
|
|
|
|
$this->groupMapper = $mapper;
|
|
|
|
}
|
|
|
|
|
2014-12-20 15:09:04 +00:00
|
|
|
/**
|
|
|
|
* returns the Group Mapper
|
|
|
|
* @throws \Exception
|
|
|
|
* @return AbstractMapping
|
|
|
|
*/
|
|
|
|
public function getGroupMapper() {
|
|
|
|
if(is_null($this->groupMapper)) {
|
|
|
|
throw new \Exception('GroupMapper was not assigned to this Access instance.');
|
|
|
|
}
|
|
|
|
return $this->groupMapper;
|
|
|
|
}
|
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-07-25 10:37:39 +00:00
|
|
|
private function checkConnection() {
|
|
|
|
return ($this->connection instanceof Connection);
|
2012-04-16 16:03:02 +00:00
|
|
|
}
|
2012-06-07 16:13:41 +00:00
|
|
|
|
2012-05-17 17:33:38 +00:00
|
|
|
/**
|
2014-06-03 11:01:14 +00:00
|
|
|
* returns the Connection instance
|
2016-05-12 14:35:33 +00:00
|
|
|
* @return \OCA\User_LDAP\Connection
|
2014-03-27 17:01:14 +00:00
|
|
|
*/
|
|
|
|
public function getConnection() {
|
|
|
|
return $this->connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-03 11:01:14 +00:00
|
|
|
* reads a given attribute for an LDAP record identified by a DN
|
2018-01-26 11:47:19 +00:00
|
|
|
*
|
2014-07-01 20:02:41 +00:00
|
|
|
* @param string $dn the record in question
|
|
|
|
* @param string $attr the attribute that shall be retrieved
|
2012-11-05 14:38:49 +00:00
|
|
|
* if empty, just check the record's existence
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param string $filter
|
|
|
|
* @return array|false an array of values on success or an empty
|
2012-11-05 16:35:09 +00:00
|
|
|
* array if $attr is empty, false otherwise
|
2018-01-26 11:47:19 +00:00
|
|
|
* @throws ServerNotAvailableException
|
2012-05-17 17:33:38 +00:00
|
|
|
*/
|
2012-10-27 17:15:13 +00:00
|
|
|
public function readAttribute($dn, $attr, $filter = 'objectClass=*') {
|
2012-07-25 10:37:39 +00:00
|
|
|
if(!$this->checkConnection()) {
|
2013-02-14 21:16:48 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
'No LDAP Connector assigned, access impossible for readAttribute.',
|
2018-04-25 13:22:28 +00:00
|
|
|
ILogger::WARN);
|
2012-07-25 10:37:39 +00:00
|
|
|
return false;
|
2012-06-07 16:13:41 +00:00
|
|
|
}
|
2017-05-21 00:24:38 +00:00
|
|
|
$cr = $this->connection->getConnectionResource();
|
|
|
|
if(!$this->ldap->isResource($cr)) {
|
|
|
|
//LDAP not available
|
2018-04-25 13:22:28 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', ILogger::DEBUG);
|
2017-05-21 00:24:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-19 16:01:58 +00:00
|
|
|
//Cancel possibly running Paged Results operation, otherwise we run in
|
|
|
|
//LDAP protocol errors
|
|
|
|
$this->abandonPagedSearch();
|
2014-09-30 15:00:25 +00:00
|
|
|
// openLDAP requires that we init a new Paged Search. Not needed by AD,
|
|
|
|
// but does not hurt either.
|
2018-01-26 14:01:27 +00:00
|
|
|
$pagingSize = (int)$this->connection->ldapPagingSize;
|
2015-01-12 15:25:11 +00:00
|
|
|
// 0 won't result in replies, small numbers may leave out groups
|
|
|
|
// (cf. #12306), 500 is default for paging and should work everywhere.
|
2015-01-20 16:50:53 +00:00
|
|
|
$maxResults = $pagingSize > 20 ? $pagingSize : 500;
|
2016-01-07 19:01:24 +00:00
|
|
|
$attr = mb_strtolower($attr, 'UTF-8');
|
|
|
|
// the actual read attribute later may contain parameters on a ranged
|
|
|
|
// request, e.g. member;range=99-199. Depends on server reply.
|
|
|
|
$attrToRead = $attr;
|
|
|
|
|
|
|
|
$values = [];
|
|
|
|
$isRangeRequest = false;
|
|
|
|
do {
|
2017-05-21 00:24:38 +00:00
|
|
|
$result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults);
|
2016-01-07 19:01:24 +00:00
|
|
|
if(is_bool($result)) {
|
|
|
|
// when an exists request was run and it was successful, an empty
|
|
|
|
// array must be returned
|
|
|
|
return $result ? [] : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$isRangeRequest) {
|
|
|
|
$values = $this->extractAttributeValuesFromResult($result, $attr);
|
|
|
|
if (!empty($values)) {
|
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$isRangeRequest = false;
|
|
|
|
$result = $this->extractRangeData($result, $attr);
|
|
|
|
if (!empty($result)) {
|
|
|
|
$normalizedResult = $this->extractAttributeValuesFromResult(
|
|
|
|
[ $attr => $result['values'] ],
|
|
|
|
$attr
|
|
|
|
);
|
|
|
|
$values = array_merge($values, $normalizedResult);
|
|
|
|
|
|
|
|
if($result['rangeHigh'] === '*') {
|
|
|
|
// when server replies with * as high range value, there are
|
|
|
|
// no more results left
|
|
|
|
return $values;
|
|
|
|
} else {
|
|
|
|
$low = $result['rangeHigh'] + 1;
|
|
|
|
$attrToRead = $result['attributeName'] . ';range=' . $low . '-*';
|
|
|
|
$isRangeRequest = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while($isRangeRequest);
|
|
|
|
|
2018-04-25 13:22:28 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, ILogger::DEBUG);
|
2016-01-07 19:01:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs an read operation against LDAP
|
|
|
|
*
|
2017-05-21 00:24:38 +00:00
|
|
|
* @param resource $cr the LDAP connection
|
2016-01-07 19:01:24 +00:00
|
|
|
* @param string $dn
|
|
|
|
* @param string $attribute
|
|
|
|
* @param string $filter
|
|
|
|
* @param int $maxResults
|
|
|
|
* @return array|bool false if there was any error, true if an exists check
|
|
|
|
* was performed and the requested DN found, array with the
|
|
|
|
* returned data on a successful usual operation
|
2018-01-26 11:47:19 +00:00
|
|
|
* @throws ServerNotAvailableException
|
2016-01-07 19:01:24 +00:00
|
|
|
*/
|
2017-05-21 00:24:38 +00:00
|
|
|
public function executeRead($cr, $dn, $attribute, $filter, $maxResults) {
|
2016-01-07 19:01:24 +00:00
|
|
|
$this->initPagedSearch($filter, array($dn), array($attribute), $maxResults, 0);
|
2016-07-22 08:46:29 +00:00
|
|
|
$dn = $this->helper->DNasBaseParameter($dn);
|
2017-05-21 00:24:38 +00:00
|
|
|
$rr = @$this->invokeLDAPMethod('read', $cr, $dn, $filter, array($attribute));
|
2016-01-07 19:01:24 +00:00
|
|
|
if (!$this->ldap->isResource($rr)) {
|
|
|
|
if ($attribute !== '') {
|
2013-03-18 23:23:59 +00:00
|
|
|
//do not throw this message on userExists check, irritates
|
2018-04-25 13:22:28 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, ILogger::DEBUG);
|
2013-03-18 23:23:59 +00:00
|
|
|
}
|
2012-07-25 10:54:23 +00:00
|
|
|
//in case an error occurs , e.g. object does not exist
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-21 00:24:38 +00:00
|
|
|
if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) {
|
2018-04-25 13:22:28 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', ILogger::DEBUG);
|
2016-01-07 19:01:24 +00:00
|
|
|
return true;
|
2012-11-05 14:38:49 +00:00
|
|
|
}
|
2017-05-21 00:24:38 +00:00
|
|
|
$er = $this->invokeLDAPMethod('firstEntry', $cr, $rr);
|
2016-01-07 19:01:24 +00:00
|
|
|
if (!$this->ldap->isResource($er)) {
|
2012-10-27 17:15:13 +00:00
|
|
|
//did not match the filter, return false
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-25 10:37:39 +00:00
|
|
|
//LDAP attributes are not case sensitive
|
2013-09-11 17:42:08 +00:00
|
|
|
$result = \OCP\Util::mb_array_change_key_case(
|
2017-05-21 00:24:38 +00:00
|
|
|
$this->invokeLDAPMethod('getAttributes', $cr, $er), MB_CASE_LOWER, 'UTF-8');
|
2016-01-07 19:01:24 +00:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2012-04-14 16:05:29 +00:00
|
|
|
|
2016-01-07 19:01:24 +00:00
|
|
|
/**
|
|
|
|
* Normalizes a result grom getAttributes(), i.e. handles DNs and binary
|
|
|
|
* data if present.
|
|
|
|
*
|
|
|
|
* @param array $result from ILDAPWrapper::getAttributes()
|
|
|
|
* @param string $attribute the attribute name that was read
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function extractAttributeValuesFromResult($result, $attribute) {
|
|
|
|
$values = [];
|
|
|
|
if(isset($result[$attribute]) && $result[$attribute]['count'] > 0) {
|
2016-10-04 16:12:48 +00:00
|
|
|
$lowercaseAttribute = strtolower($attribute);
|
2016-01-07 19:01:24 +00:00
|
|
|
for($i=0;$i<$result[$attribute]['count'];$i++) {
|
|
|
|
if($this->resemblesDN($attribute)) {
|
2016-12-05 10:36:20 +00:00
|
|
|
$values[] = $this->helper->sanitizeDN($result[$attribute][$i]);
|
2016-10-04 16:12:48 +00:00
|
|
|
} elseif($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') {
|
2016-01-07 19:01:24 +00:00
|
|
|
$values[] = $this->convertObjectGUID2Str($result[$attribute][$i]);
|
2012-11-08 22:01:28 +00:00
|
|
|
} else {
|
2016-01-07 19:01:24 +00:00
|
|
|
$values[] = $result[$attribute][$i];
|
2012-11-08 22:01:28 +00:00
|
|
|
}
|
2012-07-25 10:37:39 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-07 19:01:24 +00:00
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to find ranged data in a getAttribute results and extracts the
|
|
|
|
* returned values as well as information on the range and full attribute
|
|
|
|
* name for further processing.
|
|
|
|
*
|
|
|
|
* @param array $result from ILDAPWrapper::getAttributes()
|
|
|
|
* @param string $attribute the attribute name that was read. Without ";range=…"
|
|
|
|
* @return array If a range was detected with keys 'values', 'attributeName',
|
|
|
|
* 'attributeFull' and 'rangeHigh', otherwise empty.
|
|
|
|
*/
|
|
|
|
public function extractRangeData($result, $attribute) {
|
|
|
|
$keys = array_keys($result);
|
|
|
|
foreach($keys as $key) {
|
|
|
|
if($key !== $attribute && strpos($key, $attribute) === 0) {
|
|
|
|
$queryData = explode(';', $key);
|
|
|
|
if(strpos($queryData[1], 'range=') === 0) {
|
|
|
|
$high = substr($queryData[1], 1 + strpos($queryData[1], '-'));
|
|
|
|
$data = [
|
|
|
|
'values' => $result[$key],
|
|
|
|
'attributeName' => $queryData[0],
|
|
|
|
'attributeFull' => $key,
|
|
|
|
'rangeHigh' => $high,
|
|
|
|
];
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [];
|
2012-04-16 16:03:02 +00:00
|
|
|
}
|
2016-08-30 09:43:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set password for an LDAP user identified by a DN
|
2016-11-23 18:58:43 +00:00
|
|
|
*
|
2016-08-30 09:43:29 +00:00
|
|
|
* @param string $userDN the user in question
|
|
|
|
* @param string $password the new password
|
|
|
|
* @return bool
|
2016-11-23 18:58:43 +00:00
|
|
|
* @throws HintException
|
|
|
|
* @throws \Exception
|
2016-08-30 09:43:29 +00:00
|
|
|
*/
|
|
|
|
public function setPassword($userDN, $password) {
|
2018-01-26 14:01:27 +00:00
|
|
|
if((int)$this->connection->turnOnPasswordChange !== 1) {
|
2016-08-30 09:43:29 +00:00
|
|
|
throw new \Exception('LDAP password changes are disabled.');
|
|
|
|
}
|
2017-05-21 00:24:38 +00:00
|
|
|
$cr = $this->connection->getConnectionResource();
|
|
|
|
if(!$this->ldap->isResource($cr)) {
|
|
|
|
//LDAP not available
|
2018-04-25 13:22:28 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', ILogger::DEBUG);
|
2017-05-21 00:24:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-08-30 09:43:29 +00:00
|
|
|
try {
|
2017-06-30 10:36:33 +00:00
|
|
|
return @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password);
|
2016-08-30 09:43:29 +00:00
|
|
|
} catch(ConstraintViolationException $e) {
|
|
|
|
throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ').$e->getMessage(), $e->getCode());
|
|
|
|
}
|
|
|
|
}
|
2012-04-16 16:03:02 +00:00
|
|
|
|
2012-04-25 18:34:58 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* checks whether the given attributes value is probably a DN
|
2014-02-06 15:30:58 +00:00
|
|
|
* @param string $attr the attribute in question
|
|
|
|
* @return boolean if so true, otherwise false
|
2012-04-25 18:34:58 +00:00
|
|
|
*/
|
2012-07-25 10:37:39 +00:00
|
|
|
private function resemblesDN($attr) {
|
|
|
|
$resemblingAttributes = array(
|
|
|
|
'dn',
|
|
|
|
'uniquemember',
|
2015-09-28 16:38:57 +00:00
|
|
|
'member',
|
|
|
|
// memberOf is an "operational" attribute, without a definition in any RFC
|
|
|
|
'memberof'
|
2012-04-16 11:27:20 +00:00
|
|
|
);
|
2012-07-25 10:37:39 +00:00
|
|
|
return in_array($attr, $resemblingAttributes);
|
|
|
|
}
|
2012-04-15 12:47:33 +00:00
|
|
|
|
2014-08-11 14:40:41 +00:00
|
|
|
/**
|
|
|
|
* checks whether the given string is probably a DN
|
|
|
|
* @param string $string
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function stringResemblesDN($string) {
|
|
|
|
$r = $this->ldap->explodeDN($string, 0);
|
|
|
|
// if exploding a DN succeeds and does not end up in
|
|
|
|
// an empty array except for $r[count] being 0.
|
|
|
|
return (is_array($r) && count($r) > 1);
|
|
|
|
}
|
|
|
|
|
2014-07-01 20:02:41 +00:00
|
|
|
/**
|
|
|
|
* returns a DN-string that is cleaned from not domain parts, e.g.
|
|
|
|
* cn=foo,cn=bar,dc=foobar,dc=server,dc=org
|
|
|
|
* becomes dc=foobar,dc=server,dc=org
|
|
|
|
* @param string $dn
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDomainDNFromDN($dn) {
|
|
|
|
$allParts = $this->ldap->explodeDN($dn, 0);
|
|
|
|
if($allParts === false) {
|
|
|
|
//not a valid DN
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$domainParts = array();
|
|
|
|
$dcFound = false;
|
|
|
|
foreach($allParts as $part) {
|
|
|
|
if(!$dcFound && strpos($part, 'dc=') === 0) {
|
|
|
|
$dcFound = true;
|
|
|
|
}
|
|
|
|
if($dcFound) {
|
|
|
|
$domainParts[] = $part;
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 20:55:24 +00:00
|
|
|
return implode(',', $domainParts);
|
2014-07-01 20:02:41 +00:00
|
|
|
}
|
|
|
|
|
2012-04-25 18:34:58 +00:00
|
|
|
/**
|
2017-04-12 04:16:27 +00:00
|
|
|
* returns the LDAP DN for the given internal Nextcloud name of the group
|
|
|
|
* @param string $name the Nextcloud name in question
|
2014-11-05 12:05:07 +00:00
|
|
|
* @return string|false LDAP DN on success, otherwise false
|
2012-04-25 18:34:58 +00:00
|
|
|
*/
|
2012-07-25 10:37:39 +00:00
|
|
|
public function groupname2dn($name) {
|
2016-05-12 14:55:58 +00:00
|
|
|
return $this->groupMapper->getDNByName($name);
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-12 04:16:27 +00:00
|
|
|
* returns the LDAP DN for the given internal Nextcloud name of the user
|
|
|
|
* @param string $name the Nextcloud name in question
|
2015-01-16 18:31:15 +00:00
|
|
|
* @return string|false with the LDAP DN on success, otherwise false
|
2012-04-25 18:34:58 +00:00
|
|
|
*/
|
2012-07-25 10:37:39 +00:00
|
|
|
public function username2dn($name) {
|
2016-05-12 14:55:58 +00:00
|
|
|
$fdn = $this->userMapper->getDNByName($name);
|
2014-11-05 12:05:07 +00:00
|
|
|
|
2013-11-25 21:08:11 +00:00
|
|
|
//Check whether the DN belongs to the Base, to avoid issues on multi-
|
|
|
|
//server setups
|
2014-11-05 12:05:07 +00:00
|
|
|
if(is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) {
|
|
|
|
return $fdn;
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-12 04:16:27 +00:00
|
|
|
* returns the internal Nextcloud name for the given LDAP DN of the group, false on DN outside of search DN or failure
|
2014-11-05 12:05:07 +00:00
|
|
|
* @param string $fdn the dn of the group object
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param string $ldapName optional, the display name of the object
|
2017-04-12 04:16:27 +00:00
|
|
|
* @return string|false with the name to use in Nextcloud, false on DN outside of search DN
|
2012-04-25 18:34:58 +00:00
|
|
|
*/
|
2014-11-05 12:05:07 +00:00
|
|
|
public function dn2groupname($fdn, $ldapName = null) {
|
2013-01-08 18:00:49 +00:00
|
|
|
//To avoid bypassing the base DN settings under certain circumstances
|
|
|
|
//with the group support, check whether the provided DN matches one of
|
|
|
|
//the given Bases
|
2014-11-05 12:05:07 +00:00
|
|
|
if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) {
|
2012-06-18 23:15:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-03 20:38:53 +00:00
|
|
|
|
2014-11-05 12:05:07 +00:00
|
|
|
return $this->dn2ocname($fdn, $ldapName, false);
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 10:41:17 +00:00
|
|
|
/**
|
|
|
|
* accepts an array of group DNs and tests whether they match the user
|
|
|
|
* filter by doing read operations against the group entries. Returns an
|
|
|
|
* array of DNs that match the filter.
|
|
|
|
*
|
|
|
|
* @param string[] $groupDNs
|
|
|
|
* @return string[]
|
2018-03-15 14:47:44 +00:00
|
|
|
* @throws ServerNotAvailableException
|
2015-06-25 10:41:17 +00:00
|
|
|
*/
|
|
|
|
public function groupsMatchFilter($groupDNs) {
|
|
|
|
$validGroupDNs = [];
|
|
|
|
foreach($groupDNs as $dn) {
|
|
|
|
$cacheKey = 'groupsMatchFilter-'.$dn;
|
2016-04-18 08:32:15 +00:00
|
|
|
$groupMatchFilter = $this->connection->getFromCache($cacheKey);
|
|
|
|
if(!is_null($groupMatchFilter)) {
|
|
|
|
if($groupMatchFilter) {
|
2015-06-25 10:41:17 +00:00
|
|
|
$validGroupDNs[] = $dn;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-07-09 10:19:04 +00:00
|
|
|
// Check the base DN first. If this is not met already, we don't
|
|
|
|
// need to ask the server at all.
|
|
|
|
if(!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) {
|
|
|
|
$this->connection->writeToCache($cacheKey, false);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-15 14:47:44 +00:00
|
|
|
$result = $this->readAttribute($dn, '', $this->connection->ldapGroupFilter);
|
2015-06-25 10:41:17 +00:00
|
|
|
if(is_array($result)) {
|
2015-07-09 10:19:04 +00:00
|
|
|
$this->connection->writeToCache($cacheKey, true);
|
2015-06-25 10:41:17 +00:00
|
|
|
$validGroupDNs[] = $dn;
|
2015-07-09 10:19:04 +00:00
|
|
|
} else {
|
|
|
|
$this->connection->writeToCache($cacheKey, false);
|
2015-06-25 10:41:17 +00:00
|
|
|
}
|
2015-07-09 10:19:04 +00:00
|
|
|
|
2015-06-25 10:41:17 +00:00
|
|
|
}
|
|
|
|
return $validGroupDNs;
|
|
|
|
}
|
|
|
|
|
2012-04-25 18:34:58 +00:00
|
|
|
/**
|
2017-04-12 04:16:27 +00:00
|
|
|
* returns the internal Nextcloud name for the given LDAP DN of the user, false on DN outside of search DN or failure
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param string $dn the dn of the user object
|
|
|
|
* @param string $ldapName optional, the display name of the object
|
2017-04-12 04:16:27 +00:00
|
|
|
* @return string|false with with the name to use in Nextcloud
|
2012-04-25 18:34:58 +00:00
|
|
|
*/
|
2014-11-05 12:05:07 +00:00
|
|
|
public function dn2username($fdn, $ldapName = null) {
|
2013-01-08 18:00:49 +00:00
|
|
|
//To avoid bypassing the base DN settings under certain circumstances
|
|
|
|
//with the group support, check whether the provided DN matches one of
|
|
|
|
//the given Bases
|
2014-11-05 12:05:07 +00:00
|
|
|
if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) {
|
2012-06-18 23:15:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-03 20:38:53 +00:00
|
|
|
|
2014-11-05 12:05:07 +00:00
|
|
|
return $this->dn2ocname($fdn, $ldapName, true);
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
|
|
|
|
2012-07-25 10:37:39 +00:00
|
|
|
/**
|
2017-04-12 04:16:27 +00:00
|
|
|
* returns an internal Nextcloud name for the given LDAP DN, false on DN outside of search DN
|
2017-10-31 18:42:17 +00:00
|
|
|
*
|
2017-10-31 11:20:44 +00:00
|
|
|
* @param string $fdn the dn of the user object
|
2017-11-08 00:51:14 +00:00
|
|
|
* @param string|null $ldapName optional, the display name of the object
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param bool $isUser optional, whether it is a user object (otherwise group assumed)
|
2017-10-31 11:20:44 +00:00
|
|
|
* @param bool|null $newlyMapped
|
2017-10-31 18:42:17 +00:00
|
|
|
* @param array|null $record
|
|
|
|
* @return false|string with with the name to use in Nextcloud
|
2017-12-07 21:02:54 +00:00
|
|
|
* @throws \Exception
|
2012-07-25 10:37:39 +00:00
|
|
|
*/
|
2017-10-31 18:42:17 +00:00
|
|
|
public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped = null, array $record = null) {
|
2017-10-31 11:20:44 +00:00
|
|
|
$newlyMapped = false;
|
2012-04-25 18:34:58 +00:00
|
|
|
if($isUser) {
|
2014-12-20 15:09:04 +00:00
|
|
|
$mapper = $this->getUserMapper();
|
2012-07-25 10:37:39 +00:00
|
|
|
$nameAttribute = $this->connection->ldapUserDisplayName;
|
2018-03-07 11:18:46 +00:00
|
|
|
$filter = $this->connection->ldapUserFilter;
|
2012-04-25 18:34:58 +00:00
|
|
|
} else {
|
2014-12-20 15:09:04 +00:00
|
|
|
$mapper = $this->getGroupMapper();
|
2012-07-25 10:37:39 +00:00
|
|
|
$nameAttribute = $this->connection->ldapGroupDisplayName;
|
2018-03-07 11:18:46 +00:00
|
|
|
$filter = $this->connection->ldapGroupFilter;
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 04:16:27 +00:00
|
|
|
//let's try to retrieve the Nextcloud name from the mappings table
|
2017-10-31 11:20:44 +00:00
|
|
|
$ncName = $mapper->getNameByDN($fdn);
|
|
|
|
if(is_string($ncName)) {
|
|
|
|
return $ncName;
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
|
|
|
|
2012-08-23 16:29:43 +00:00
|
|
|
//second try: get the UUID and check if it is known. Then, update the DN and return the name.
|
2017-10-31 18:42:17 +00:00
|
|
|
$uuid = $this->getUUID($fdn, $isUser, $record);
|
2014-11-05 12:05:07 +00:00
|
|
|
if(is_string($uuid)) {
|
2017-10-31 11:20:44 +00:00
|
|
|
$ncName = $mapper->getNameByUUID($uuid);
|
|
|
|
if(is_string($ncName)) {
|
2014-11-05 12:05:07 +00:00
|
|
|
$mapper->setDNbyUUID($fdn, $uuid);
|
2017-10-31 11:20:44 +00:00
|
|
|
return $ncName;
|
2012-08-23 16:29:43 +00:00
|
|
|
}
|
2013-02-10 20:52:57 +00:00
|
|
|
} else {
|
|
|
|
//If the UUID can't be detected something is foul.
|
2018-04-25 13:22:28 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'Cannot determine UUID for '.$fdn.'. Skipping.', ILogger::INFO);
|
2013-02-10 20:52:57 +00:00
|
|
|
return false;
|
2012-08-23 16:29:43 +00:00
|
|
|
}
|
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
if(is_null($ldapName)) {
|
2018-03-07 11:18:46 +00:00
|
|
|
$ldapName = $this->readAttribute($fdn, $nameAttribute, $filter);
|
2014-05-11 13:17:27 +00:00
|
|
|
if(!isset($ldapName[0]) && empty($ldapName[0])) {
|
2018-04-25 13:22:28 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.' with filter '.$filter.'.', ILogger::INFO);
|
2012-07-30 15:42:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-11 13:17:27 +00:00
|
|
|
$ldapName = $ldapName[0];
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
2013-05-08 12:56:52 +00:00
|
|
|
|
|
|
|
if($isUser) {
|
2018-01-26 14:01:27 +00:00
|
|
|
$usernameAttribute = (string)$this->connection->ldapExpertUsernameAttr;
|
2016-09-22 22:30:57 +00:00
|
|
|
if ($usernameAttribute !== '') {
|
2014-11-05 12:05:07 +00:00
|
|
|
$username = $this->readAttribute($fdn, $usernameAttribute);
|
2013-05-08 12:56:52 +00:00
|
|
|
$username = $username[0];
|
|
|
|
} else {
|
|
|
|
$username = $uuid;
|
|
|
|
}
|
2018-03-02 16:44:06 +00:00
|
|
|
try {
|
|
|
|
$intName = $this->sanitizeUsername($username);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'app' => 'user_ldap',
|
2018-04-25 13:22:28 +00:00
|
|
|
'level' => ILogger::WARN,
|
2018-03-02 16:44:06 +00:00
|
|
|
]);
|
|
|
|
// we don't attempt to set a username here. We can go for
|
2018-03-05 12:30:28 +00:00
|
|
|
// for an alternative 4 digit random number as we would append
|
2018-03-02 16:44:06 +00:00
|
|
|
// otherwise, however it's likely not enough space in bigger
|
|
|
|
// setups, and most importantly: this is not intended.
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-08 12:56:52 +00:00
|
|
|
} else {
|
2014-05-11 13:17:27 +00:00
|
|
|
$intName = $ldapName;
|
2013-05-08 12:56:52 +00:00
|
|
|
}
|
2012-04-25 18:34:58 +00:00
|
|
|
|
2012-12-06 17:11:14 +00:00
|
|
|
//a new user/group! Add it only if it doesn't conflict with other backend's users or existing groups
|
2013-02-10 20:52:57 +00:00
|
|
|
//disabling Cache is required to avoid that the new user is cached as not-existing in fooExists check
|
2014-11-28 11:08:33 +00:00
|
|
|
//NOTE: mind, disabling cache affects only this instance! Using it
|
|
|
|
// outside of core user management will still cache the user as non-existing.
|
2013-02-10 20:52:57 +00:00
|
|
|
$originalTTL = $this->connection->ldapCacheTTL;
|
2018-04-11 13:33:36 +00:00
|
|
|
$this->connection->setConfiguration(['ldapCacheTTL' => 0]);
|
2018-03-19 14:53:30 +00:00
|
|
|
if(($isUser && $intName !== '' && !$this->ncUserManager->userExists($intName))
|
2017-03-03 07:24:27 +00:00
|
|
|
|| (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))) {
|
2014-11-05 12:05:07 +00:00
|
|
|
if($mapper->map($fdn, $intName, $uuid)) {
|
2018-04-11 13:33:36 +00:00
|
|
|
$this->connection->setConfiguration(['ldapCacheTTL' => $originalTTL]);
|
|
|
|
if($this->ncUserManager instanceof PublicEmitter && $isUser) {
|
2018-03-19 14:53:30 +00:00
|
|
|
$this->ncUserManager->emit('\OC\User', 'assignedUserId', [$intName]);
|
|
|
|
}
|
2017-10-31 11:20:44 +00:00
|
|
|
$newlyMapped = true;
|
2014-05-11 13:17:27 +00:00
|
|
|
return $intName;
|
2012-08-28 23:23:36 +00:00
|
|
|
}
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
2018-04-11 13:33:36 +00:00
|
|
|
$this->connection->setConfiguration(['ldapCacheTTL' => $originalTTL]);
|
2012-04-25 18:34:58 +00:00
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
$altName = $this->createAltInternalOwnCloudName($intName, $isUser);
|
2018-10-26 16:01:57 +00:00
|
|
|
if (is_string($altName) && $mapper->map($fdn, $altName, $uuid)) {
|
|
|
|
if ($this->ncUserManager instanceof PublicEmitter && $isUser) {
|
|
|
|
$this->ncUserManager->emit('\OC\User', 'assignedUserId', [$altName]);
|
2018-03-19 14:53:30 +00:00
|
|
|
}
|
2017-10-31 11:20:44 +00:00
|
|
|
$newlyMapped = true;
|
2014-05-11 13:17:27 +00:00
|
|
|
return $altName;
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 15:42:33 +00:00
|
|
|
//if everything else did not help..
|
2018-04-25 13:22:28 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'Could not create unique name for '.$fdn.'.', ILogger::INFO);
|
2012-08-28 23:23:36 +00:00
|
|
|
return false;
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* gives back the user names as they are used ownClod internally
|
2015-10-08 18:32:15 +00:00
|
|
|
* @param array $ldapUsers as returned by fetchList()
|
2017-04-12 04:16:27 +00:00
|
|
|
* @return array an array with the user names to use in Nextcloud
|
2012-04-25 18:34:58 +00:00
|
|
|
*
|
|
|
|
* gives back the user names as they are used ownClod internally
|
|
|
|
*/
|
2017-04-12 04:21:04 +00:00
|
|
|
public function nextcloudUserNames($ldapUsers) {
|
|
|
|
return $this->ldap2NextcloudNames($ldapUsers, true);
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* gives back the group names as they are used ownClod internally
|
2015-10-08 18:32:15 +00:00
|
|
|
* @param array $ldapGroups as returned by fetchList()
|
2017-04-12 04:16:27 +00:00
|
|
|
* @return array an array with the group names to use in Nextcloud
|
2012-04-25 18:34:58 +00:00
|
|
|
*
|
|
|
|
* gives back the group names as they are used ownClod internally
|
|
|
|
*/
|
2017-04-12 04:21:04 +00:00
|
|
|
public function nextcloudGroupNames($ldapGroups) {
|
|
|
|
return $this->ldap2NextcloudNames($ldapGroups, false);
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
2015-10-08 18:32:15 +00:00
|
|
|
* @param array $ldapObjects as returned by fetchList()
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param bool $isUsers
|
|
|
|
* @return array
|
2018-05-28 23:10:03 +00:00
|
|
|
* @throws \Exception
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2017-04-12 04:21:04 +00:00
|
|
|
private function ldap2NextcloudNames($ldapObjects, $isUsers) {
|
2012-04-25 18:34:58 +00:00
|
|
|
if($isUsers) {
|
2012-07-25 10:37:39 +00:00
|
|
|
$nameAttribute = $this->connection->ldapUserDisplayName;
|
2016-02-03 12:16:55 +00:00
|
|
|
$sndAttribute = $this->connection->ldapUserDisplayName2;
|
2012-04-25 18:34:58 +00:00
|
|
|
} else {
|
2012-07-25 10:37:39 +00:00
|
|
|
$nameAttribute = $this->connection->ldapGroupDisplayName;
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
2018-05-28 23:10:03 +00:00
|
|
|
$nextcloudNames = [];
|
2012-04-25 18:34:58 +00:00
|
|
|
|
|
|
|
foreach($ldapObjects as $ldapObject) {
|
2015-10-08 18:32:15 +00:00
|
|
|
$nameByLDAP = null;
|
|
|
|
if( isset($ldapObject[$nameAttribute])
|
|
|
|
&& is_array($ldapObject[$nameAttribute])
|
|
|
|
&& isset($ldapObject[$nameAttribute][0])
|
|
|
|
) {
|
|
|
|
// might be set, but not necessarily. if so, we use it.
|
|
|
|
$nameByLDAP = $ldapObject[$nameAttribute][0];
|
|
|
|
}
|
|
|
|
|
2017-04-12 04:21:04 +00:00
|
|
|
$ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers);
|
|
|
|
if($ncName) {
|
|
|
|
$nextcloudNames[] = $ncName;
|
2014-04-16 15:16:51 +00:00
|
|
|
if($isUsers) {
|
2018-05-28 23:10:03 +00:00
|
|
|
$this->updateUserState($ncName);
|
2014-04-16 15:16:51 +00:00
|
|
|
//cache the user names so it does not need to be retrieved
|
|
|
|
//again later (e.g. sharing dialogue).
|
2016-02-03 12:16:55 +00:00
|
|
|
if(is_null($nameByLDAP)) {
|
|
|
|
continue;
|
2015-10-08 18:32:15 +00:00
|
|
|
}
|
2016-02-23 14:12:01 +00:00
|
|
|
$sndName = isset($ldapObject[$sndAttribute][0])
|
|
|
|
? $ldapObject[$sndAttribute][0] : '';
|
2017-04-12 04:21:04 +00:00
|
|
|
$this->cacheUserDisplayName($ncName, $nameByLDAP, $sndName);
|
2014-04-16 15:16:51 +00:00
|
|
|
}
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-12 04:21:04 +00:00
|
|
|
return $nextcloudNames;
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 23:10:03 +00:00
|
|
|
/**
|
|
|
|
* removes the deleted-flag of a user if it was set
|
|
|
|
*
|
|
|
|
* @param string $ncname
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function updateUserState($ncname) {
|
|
|
|
$user = $this->userManager->get($ncname);
|
|
|
|
if($user instanceof OfflineUser) {
|
|
|
|
$user->unmark();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 22:55:42 +00:00
|
|
|
/**
|
|
|
|
* caches the user display name
|
2017-04-12 04:16:27 +00:00
|
|
|
* @param string $ocName the internal Nextcloud username
|
2015-08-20 22:55:42 +00:00
|
|
|
* @param string|false $home the home directory path
|
|
|
|
*/
|
|
|
|
public function cacheUserHome($ocName, $home) {
|
|
|
|
$cacheKey = 'getHome'.$ocName;
|
|
|
|
$this->connection->writeToCache($cacheKey, $home);
|
|
|
|
}
|
|
|
|
|
2014-11-28 11:08:33 +00:00
|
|
|
/**
|
|
|
|
* caches a user as existing
|
2017-04-12 04:16:27 +00:00
|
|
|
* @param string $ocName the internal Nextcloud username
|
2014-11-28 11:08:33 +00:00
|
|
|
*/
|
|
|
|
public function cacheUserExists($ocName) {
|
|
|
|
$this->connection->writeToCache('userExists'.$ocName, true);
|
|
|
|
}
|
|
|
|
|
2014-04-16 15:16:51 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* caches the user display name
|
2017-04-12 04:16:27 +00:00
|
|
|
* @param string $ocName the internal Nextcloud username
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param string $displayName the display name
|
2016-02-03 12:16:55 +00:00
|
|
|
* @param string $displayName2 the second display name
|
2014-04-16 15:16:51 +00:00
|
|
|
*/
|
2016-02-03 12:16:55 +00:00
|
|
|
public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') {
|
|
|
|
$user = $this->userManager->get($ocName);
|
2017-01-25 16:10:51 +00:00
|
|
|
if($user === null) {
|
|
|
|
return;
|
|
|
|
}
|
2016-02-03 12:16:55 +00:00
|
|
|
$displayName = $user->composeAndStoreDisplayName($displayName, $displayName2);
|
2014-04-16 10:56:08 +00:00
|
|
|
$cacheKeyTrunk = 'getDisplayName';
|
2014-05-11 13:17:27 +00:00
|
|
|
$this->connection->writeToCache($cacheKeyTrunk.$ocName, $displayName);
|
2014-04-16 10:56:08 +00:00
|
|
|
}
|
|
|
|
|
2012-04-25 18:34:58 +00:00
|
|
|
/**
|
2017-04-12 04:16:27 +00:00
|
|
|
* creates a unique name for internal Nextcloud use for users. Don't call it directly.
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param string $name the display name of the object
|
2017-04-12 04:16:27 +00:00
|
|
|
* @return string|false with with the name to use in Nextcloud or false if unsuccessful
|
2013-02-10 20:52:57 +00:00
|
|
|
*
|
|
|
|
* Instead of using this method directly, call
|
|
|
|
* createAltInternalOwnCloudName($name, true)
|
|
|
|
*/
|
|
|
|
private function _createAltInternalOwnCloudNameForUsers($name) {
|
|
|
|
$attempts = 0;
|
|
|
|
//while loop is just a precaution. If a name is not generated within
|
|
|
|
//20 attempts, something else is very wrong. Avoids infinite loop.
|
|
|
|
while($attempts < 20){
|
2013-05-30 12:14:43 +00:00
|
|
|
$altName = $name . '_' . rand(1000,9999);
|
2018-03-19 14:53:30 +00:00
|
|
|
if(!$this->ncUserManager->userExists($altName)) {
|
2013-02-10 20:52:57 +00:00
|
|
|
return $altName;
|
|
|
|
}
|
|
|
|
$attempts++;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-12 04:16:27 +00:00
|
|
|
* creates a unique name for internal Nextcloud use for groups. Don't call it directly.
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param string $name the display name of the object
|
2017-04-12 04:16:27 +00:00
|
|
|
* @return string|false with with the name to use in Nextcloud or false if unsuccessful.
|
2013-02-10 20:52:57 +00:00
|
|
|
*
|
|
|
|
* Instead of using this method directly, call
|
|
|
|
* createAltInternalOwnCloudName($name, false)
|
2012-04-25 18:34:58 +00:00
|
|
|
*
|
2013-02-10 20:52:57 +00:00
|
|
|
* Group names are also used as display names, so we do a sequential
|
|
|
|
* numbering, e.g. Developers_42 when there are 41 other groups called
|
|
|
|
* "Developers"
|
|
|
|
*/
|
|
|
|
private function _createAltInternalOwnCloudNameForGroups($name) {
|
2016-07-20 12:20:45 +00:00
|
|
|
$usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%');
|
2018-01-26 22:46:40 +00:00
|
|
|
if(!$usedNames || count($usedNames) === 0) {
|
2013-02-10 20:52:57 +00:00
|
|
|
$lastNo = 1; //will become name_2
|
|
|
|
} else {
|
|
|
|
natsort($usedNames);
|
2014-05-11 13:17:27 +00:00
|
|
|
$lastName = array_pop($usedNames);
|
2018-01-26 14:01:27 +00:00
|
|
|
$lastNo = (int)substr($lastName, strrpos($lastName, '_') + 1);
|
2013-02-10 20:52:57 +00:00
|
|
|
}
|
2018-01-26 14:01:27 +00:00
|
|
|
$altName = $name.'_'. (string)($lastNo+1);
|
2013-02-10 20:52:57 +00:00
|
|
|
unset($usedNames);
|
|
|
|
|
|
|
|
$attempts = 1;
|
|
|
|
while($attempts < 21){
|
2014-05-11 13:17:27 +00:00
|
|
|
// Check to be really sure it is unique
|
|
|
|
// while loop is just a precaution. If a name is not generated within
|
|
|
|
// 20 attempts, something else is very wrong. Avoids infinite loop.
|
2017-03-03 07:24:27 +00:00
|
|
|
if(!\OC::$server->getGroupManager()->groupExists($altName)) {
|
2013-02-10 20:52:57 +00:00
|
|
|
return $altName;
|
|
|
|
}
|
2014-07-01 20:02:41 +00:00
|
|
|
$altName = $name . '_' . ($lastNo + $attempts);
|
2013-02-10 20:52:57 +00:00
|
|
|
$attempts++;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-12 04:16:27 +00:00
|
|
|
* creates a unique name for internal Nextcloud use.
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param string $name the display name of the object
|
2014-02-06 15:30:58 +00:00
|
|
|
* @param boolean $isUser whether name should be created for a user (true) or a group (false)
|
2017-04-12 04:16:27 +00:00
|
|
|
* @return string|false with with the name to use in Nextcloud or false if unsuccessful
|
2012-04-25 18:34:58 +00:00
|
|
|
*/
|
2013-02-10 20:52:57 +00:00
|
|
|
private function createAltInternalOwnCloudName($name, $isUser) {
|
|
|
|
$originalTTL = $this->connection->ldapCacheTTL;
|
|
|
|
$this->connection->setConfiguration(array('ldapCacheTTL' => 0));
|
|
|
|
if($isUser) {
|
|
|
|
$altName = $this->_createAltInternalOwnCloudNameForUsers($name);
|
|
|
|
} else {
|
|
|
|
$altName = $this->_createAltInternalOwnCloudNameForGroups($name);
|
|
|
|
}
|
|
|
|
$this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL));
|
|
|
|
|
|
|
|
return $altName;
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
|
|
|
|
LDAP Wizard Overhaul
wizard refactor
reimplement save spinners and cursor
implement Port detector
introduced detector queue, added base dn detector
disable input fields when detectors are running
introduce spinners for fields that are being updated by detector
cache jq element objects
consolidate processing of detector results in generic / abstract base class
display notification if a detector discovered a problem
don't run base dn detector if a base is configured
reset detector queue on configuration switch
implement functionality check and update of status indicator
document ConfigModel
jsdoc for controller and main view
more documentation
implement the user filter tab view
so far the multiselects get initialized (not filled yet) and the mode can be switched.
mode is also restored.
reintroduce filter switch confirmation in admin XP mode
new detector for user object classes. so we also load user object classes if necessary and are able to save and show the setting.
multiselect trigger save actions now on close only
show spinners automatically, when a detector is running
20k limit for object classes preselection test
adjust wordings, fix grammar
add group (for users tab) detector
also includes wording fixes
error presentation moved from detectors to view, where it belongs
add info label to users page
missing wording changes
show effective LDAP filter in Assisted Mode
add user filter detector
implement count button for users and limit all count actions to 1001 for performance reasons
make port field a bit bigger. not perfect though.
do not detect port automatically
implement login filter tab view
only load features in assisted mode and don't enable assisted fields while in raw mode
add tooltips on login filter checkbox options for better understanding
permanently show filter on login tab
and also compile login filter in assisted mode
test/verify button on login attributes tab, with backend changes.
only run wizard requests if your an active tab. also run compile filter requests when switching to assisted mode
underline toggle filter links to stress that they are clickable
unity user and group tab functionality in common abstract class, add group filter tab view. only detectors and template adjustments left to have group tab implementation complete
add object class and group detector for groups as well as filter composer
show ldap filter permanently on groups tab
introduce input element that can deal better with many groups, will be used with > 40
fix disabling complex group chooser while detection is running
hide complex group chooser on config switch
fix few more issues with complex chooser
make complex group chooser available on Users tab as well
detect base dn improvements/changes:
- do not look for Base DN automatically, offer a button instead
- fix for alternative way to detect a base dn (if agent dn is not given)
- do not trigger filter composers on config switch
Changes with configuration chooser controls
- "New" was removed out of the configuration list
- and split into buttons "add" and "copy"
- delete button is also now an icon
add test button for Base DN
reimplement advanced tab. The save button is gone.
reimplement expert tab
remove unused methods
implement mail attribute detector
implement user display name attribute detection
implement member group association detector
replace text input with textarea for raw filter input
finish functionality check
auto-enable good configurations, as it was before
cleanup
move save confirmation handling to base class, reduces code duplication
enable tabs only if no running save processes are left.
move onConfigLoaded to base class, avoids code duplication
simplify, save LOCs
Test Configuration button to be dealt with in main view as it is a cross-tab element
require detectorQueue in constructor
cleanup
put bootstrap into a function and thus make it testable
get rid of old stuff
2015-03-03 10:56:03 +00:00
|
|
|
/**
|
|
|
|
* fetches a list of users according to a provided loginName and utilizing
|
|
|
|
* the login filter.
|
|
|
|
*
|
|
|
|
* @param string $loginName
|
|
|
|
* @param array $attributes optional, list of attributes to read
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function fetchUsersByLoginName($loginName, $attributes = array('dn')) {
|
|
|
|
$loginName = $this->escapeFilterPart($loginName);
|
2015-06-19 12:55:04 +00:00
|
|
|
$filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter);
|
2018-02-13 20:55:24 +00:00
|
|
|
return $this->fetchListOfUsers($filter, $attributes);
|
LDAP Wizard Overhaul
wizard refactor
reimplement save spinners and cursor
implement Port detector
introduced detector queue, added base dn detector
disable input fields when detectors are running
introduce spinners for fields that are being updated by detector
cache jq element objects
consolidate processing of detector results in generic / abstract base class
display notification if a detector discovered a problem
don't run base dn detector if a base is configured
reset detector queue on configuration switch
implement functionality check and update of status indicator
document ConfigModel
jsdoc for controller and main view
more documentation
implement the user filter tab view
so far the multiselects get initialized (not filled yet) and the mode can be switched.
mode is also restored.
reintroduce filter switch confirmation in admin XP mode
new detector for user object classes. so we also load user object classes if necessary and are able to save and show the setting.
multiselect trigger save actions now on close only
show spinners automatically, when a detector is running
20k limit for object classes preselection test
adjust wordings, fix grammar
add group (for users tab) detector
also includes wording fixes
error presentation moved from detectors to view, where it belongs
add info label to users page
missing wording changes
show effective LDAP filter in Assisted Mode
add user filter detector
implement count button for users and limit all count actions to 1001 for performance reasons
make port field a bit bigger. not perfect though.
do not detect port automatically
implement login filter tab view
only load features in assisted mode and don't enable assisted fields while in raw mode
add tooltips on login filter checkbox options for better understanding
permanently show filter on login tab
and also compile login filter in assisted mode
test/verify button on login attributes tab, with backend changes.
only run wizard requests if your an active tab. also run compile filter requests when switching to assisted mode
underline toggle filter links to stress that they are clickable
unity user and group tab functionality in common abstract class, add group filter tab view. only detectors and template adjustments left to have group tab implementation complete
add object class and group detector for groups as well as filter composer
show ldap filter permanently on groups tab
introduce input element that can deal better with many groups, will be used with > 40
fix disabling complex group chooser while detection is running
hide complex group chooser on config switch
fix few more issues with complex chooser
make complex group chooser available on Users tab as well
detect base dn improvements/changes:
- do not look for Base DN automatically, offer a button instead
- fix for alternative way to detect a base dn (if agent dn is not given)
- do not trigger filter composers on config switch
Changes with configuration chooser controls
- "New" was removed out of the configuration list
- and split into buttons "add" and "copy"
- delete button is also now an icon
add test button for Base DN
reimplement advanced tab. The save button is gone.
reimplement expert tab
remove unused methods
implement mail attribute detector
implement user display name attribute detection
implement member group association detector
replace text input with textarea for raw filter input
finish functionality check
auto-enable good configurations, as it was before
cleanup
move save confirmation handling to base class, reduces code duplication
enable tabs only if no running save processes are left.
move onConfigLoaded to base class, avoids code duplication
simplify, save LOCs
Test Configuration button to be dealt with in main view as it is a cross-tab element
require detectorQueue in constructor
cleanup
put bootstrap into a function and thus make it testable
get rid of old stuff
2015-03-03 10:56:03 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 17:20:50 +00:00
|
|
|
/**
|
|
|
|
* counts the number of users according to a provided loginName and
|
|
|
|
* utilizing the login filter.
|
|
|
|
*
|
|
|
|
* @param string $loginName
|
2017-10-31 18:42:17 +00:00
|
|
|
* @return int
|
2015-10-30 17:20:50 +00:00
|
|
|
*/
|
|
|
|
public function countUsersByLoginName($loginName) {
|
|
|
|
$loginName = $this->escapeFilterPart($loginName);
|
|
|
|
$filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter);
|
2018-02-13 20:55:24 +00:00
|
|
|
return $this->countUsers($filter);
|
2015-10-30 17:20:50 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
2014-05-13 11:29:25 +00:00
|
|
|
* @param string $filter
|
2014-05-15 12:12:17 +00:00
|
|
|
* @param string|string[] $attr
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
2017-10-31 18:42:17 +00:00
|
|
|
* @param bool $forceApplyAttributes
|
2014-05-11 13:17:27 +00:00
|
|
|
* @return array
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2017-10-31 18:42:17 +00:00
|
|
|
public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null, $forceApplyAttributes = false) {
|
2015-08-20 22:55:42 +00:00
|
|
|
$ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset);
|
2017-10-31 18:42:17 +00:00
|
|
|
$recordsToUpdate = $ldapRecords;
|
|
|
|
if(!$forceApplyAttributes) {
|
2017-11-08 00:51:14 +00:00
|
|
|
$isBackgroundJobModeAjax = $this->config
|
2017-10-31 18:42:17 +00:00
|
|
|
->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax';
|
|
|
|
$recordsToUpdate = array_filter($ldapRecords, function($record) use ($isBackgroundJobModeAjax) {
|
|
|
|
$newlyMapped = false;
|
|
|
|
$uid = $this->dn2ocname($record['dn'][0], null, true, $newlyMapped, $record);
|
2017-12-07 21:02:54 +00:00
|
|
|
if(is_string($uid)) {
|
|
|
|
$this->cacheUserExists($uid);
|
|
|
|
}
|
2017-10-31 18:42:17 +00:00
|
|
|
return ($uid !== false) && ($newlyMapped || $isBackgroundJobModeAjax);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
$this->batchApplyUserAttributes($recordsToUpdate);
|
2018-11-24 21:06:45 +00:00
|
|
|
return $this->fetchList($ldapRecords, $this->manyAttributes($attr));
|
2015-08-20 22:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* provided with an array of LDAP user records the method will fetch the
|
|
|
|
* user object and requests it to process the freshly fetched attributes and
|
|
|
|
* and their values
|
2018-05-28 23:10:03 +00:00
|
|
|
*
|
2015-08-20 22:55:42 +00:00
|
|
|
* @param array $ldapRecords
|
2018-05-28 23:10:03 +00:00
|
|
|
* @throws \Exception
|
2015-08-20 22:55:42 +00:00
|
|
|
*/
|
|
|
|
public function batchApplyUserAttributes(array $ldapRecords){
|
2015-11-27 22:46:26 +00:00
|
|
|
$displayNameAttribute = strtolower($this->connection->ldapUserDisplayName);
|
2015-08-20 22:55:42 +00:00
|
|
|
foreach($ldapRecords as $userRecord) {
|
2015-11-27 22:46:26 +00:00
|
|
|
if(!isset($userRecord[$displayNameAttribute])) {
|
|
|
|
// displayName is obligatory
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-31 18:42:17 +00:00
|
|
|
$ocName = $this->dn2ocname($userRecord['dn'][0], null, true);
|
|
|
|
if($ocName === false) {
|
2015-11-27 22:46:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-05-28 23:10:03 +00:00
|
|
|
$this->updateUserState($ocName);
|
2015-08-20 22:55:42 +00:00
|
|
|
$user = $this->userManager->get($ocName);
|
2016-06-21 14:30:48 +00:00
|
|
|
if ($user !== null) {
|
|
|
|
$user->processAttributes($userRecord);
|
|
|
|
} else {
|
|
|
|
\OC::$server->getLogger()->debug(
|
|
|
|
"The ldap user manager returned null for $ocName",
|
|
|
|
['app'=>'user_ldap']
|
|
|
|
);
|
|
|
|
}
|
2015-08-20 22:55:42 +00:00
|
|
|
}
|
2012-05-03 21:38:55 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
2014-05-13 11:29:25 +00:00
|
|
|
* @param string $filter
|
2014-05-15 12:12:17 +00:00
|
|
|
* @param string|string[] $attr
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @return array
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2012-10-26 19:53:56 +00:00
|
|
|
public function fetchListOfGroups($filter, $attr, $limit = null, $offset = null) {
|
2018-11-24 21:06:45 +00:00
|
|
|
return $this->fetchList($this->searchGroups($filter, $attr, $limit, $offset), $this->manyAttributes($attr));
|
2012-05-03 21:38:55 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
2014-05-13 11:29:25 +00:00
|
|
|
* @param array $list
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param bool $manyAttributes
|
|
|
|
* @return array
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2012-07-25 10:37:39 +00:00
|
|
|
private function fetchList($list, $manyAttributes) {
|
2012-05-03 21:38:55 +00:00
|
|
|
if(is_array($list)) {
|
|
|
|
if($manyAttributes) {
|
|
|
|
return $list;
|
|
|
|
} else {
|
2015-10-07 16:57:49 +00:00
|
|
|
$list = array_reduce($list, function($carry, $item) {
|
2015-10-09 18:07:35 +00:00
|
|
|
$attribute = array_keys($item)[0];
|
|
|
|
$carry[] = $item[$attribute][0];
|
|
|
|
return $carry;
|
2015-10-07 16:57:49 +00:00
|
|
|
}, array());
|
2012-05-03 21:38:55 +00:00
|
|
|
return array_unique($list, SORT_LOCALE_STRING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//error cause actually, maybe throw an exception in future.
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2012-04-23 11:04:58 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* executes an LDAP search, optimized for Users
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param string $filter the LDAP filter for the search
|
2014-05-15 12:12:17 +00:00
|
|
|
* @param string|string[] $attr optional, when a certain attribute shall be filtered out
|
2014-02-19 08:31:54 +00:00
|
|
|
* @param integer $limit
|
|
|
|
* @param integer $offset
|
2014-05-11 13:17:27 +00:00
|
|
|
* @return array with the search result
|
2012-04-23 11:04:58 +00:00
|
|
|
*
|
|
|
|
* Executes an LDAP search
|
|
|
|
*/
|
2012-10-26 19:53:56 +00:00
|
|
|
public function searchUsers($filter, $attr = null, $limit = null, $offset = null) {
|
|
|
|
return $this->search($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset);
|
2012-04-23 11:04:58 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
|
|
|
* @param string $filter
|
2014-05-15 12:12:17 +00:00
|
|
|
* @param string|string[] $attr
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @return false|int
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2014-01-08 11:07:57 +00:00
|
|
|
public function countUsers($filter, $attr = array('dn'), $limit = null, $offset = null) {
|
2014-03-19 23:21:51 +00:00
|
|
|
return $this->count($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset);
|
2014-01-08 11:07:57 +00:00
|
|
|
}
|
|
|
|
|
2012-04-23 11:04:58 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* executes an LDAP search, optimized for Groups
|
2014-02-19 08:31:54 +00:00
|
|
|
* @param string $filter the LDAP filter for the search
|
2014-05-15 12:12:17 +00:00
|
|
|
* @param string|string[] $attr optional, when a certain attribute shall be filtered out
|
2014-02-19 08:31:54 +00:00
|
|
|
* @param integer $limit
|
|
|
|
* @param integer $offset
|
2014-05-11 13:17:27 +00:00
|
|
|
* @return array with the search result
|
2012-04-23 11:04:58 +00:00
|
|
|
*
|
|
|
|
* Executes an LDAP search
|
|
|
|
*/
|
2012-10-26 19:53:56 +00:00
|
|
|
public function searchGroups($filter, $attr = null, $limit = null, $offset = null) {
|
|
|
|
return $this->search($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset);
|
2012-04-23 11:04:58 +00:00
|
|
|
}
|
|
|
|
|
2012-04-14 16:26:46 +00:00
|
|
|
/**
|
2014-06-11 11:35:35 +00:00
|
|
|
* returns the number of available groups
|
|
|
|
* @param string $filter the LDAP search filter
|
|
|
|
* @param string[] $attr optional
|
|
|
|
* @param int|null $limit
|
|
|
|
* @param int|null $offset
|
|
|
|
* @return int|bool
|
|
|
|
*/
|
|
|
|
public function countGroups($filter, $attr = array('dn'), $limit = null, $offset = null) {
|
|
|
|
return $this->count($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset);
|
|
|
|
}
|
|
|
|
|
LDAP Wizard Overhaul
wizard refactor
reimplement save spinners and cursor
implement Port detector
introduced detector queue, added base dn detector
disable input fields when detectors are running
introduce spinners for fields that are being updated by detector
cache jq element objects
consolidate processing of detector results in generic / abstract base class
display notification if a detector discovered a problem
don't run base dn detector if a base is configured
reset detector queue on configuration switch
implement functionality check and update of status indicator
document ConfigModel
jsdoc for controller and main view
more documentation
implement the user filter tab view
so far the multiselects get initialized (not filled yet) and the mode can be switched.
mode is also restored.
reintroduce filter switch confirmation in admin XP mode
new detector for user object classes. so we also load user object classes if necessary and are able to save and show the setting.
multiselect trigger save actions now on close only
show spinners automatically, when a detector is running
20k limit for object classes preselection test
adjust wordings, fix grammar
add group (for users tab) detector
also includes wording fixes
error presentation moved from detectors to view, where it belongs
add info label to users page
missing wording changes
show effective LDAP filter in Assisted Mode
add user filter detector
implement count button for users and limit all count actions to 1001 for performance reasons
make port field a bit bigger. not perfect though.
do not detect port automatically
implement login filter tab view
only load features in assisted mode and don't enable assisted fields while in raw mode
add tooltips on login filter checkbox options for better understanding
permanently show filter on login tab
and also compile login filter in assisted mode
test/verify button on login attributes tab, with backend changes.
only run wizard requests if your an active tab. also run compile filter requests when switching to assisted mode
underline toggle filter links to stress that they are clickable
unity user and group tab functionality in common abstract class, add group filter tab view. only detectors and template adjustments left to have group tab implementation complete
add object class and group detector for groups as well as filter composer
show ldap filter permanently on groups tab
introduce input element that can deal better with many groups, will be used with > 40
fix disabling complex group chooser while detection is running
hide complex group chooser on config switch
fix few more issues with complex chooser
make complex group chooser available on Users tab as well
detect base dn improvements/changes:
- do not look for Base DN automatically, offer a button instead
- fix for alternative way to detect a base dn (if agent dn is not given)
- do not trigger filter composers on config switch
Changes with configuration chooser controls
- "New" was removed out of the configuration list
- and split into buttons "add" and "copy"
- delete button is also now an icon
add test button for Base DN
reimplement advanced tab. The save button is gone.
reimplement expert tab
remove unused methods
implement mail attribute detector
implement user display name attribute detection
implement member group association detector
replace text input with textarea for raw filter input
finish functionality check
auto-enable good configurations, as it was before
cleanup
move save confirmation handling to base class, reduces code duplication
enable tabs only if no running save processes are left.
move onConfigLoaded to base class, avoids code duplication
simplify, save LOCs
Test Configuration button to be dealt with in main view as it is a cross-tab element
require detectorQueue in constructor
cleanup
put bootstrap into a function and thus make it testable
get rid of old stuff
2015-03-03 10:56:03 +00:00
|
|
|
/**
|
|
|
|
* returns the number of available objects on the base DN
|
|
|
|
*
|
|
|
|
* @param int|null $limit
|
|
|
|
* @param int|null $offset
|
|
|
|
* @return int|bool
|
|
|
|
*/
|
|
|
|
public function countObjects($limit = null, $offset = null) {
|
|
|
|
return $this->count('objectclass=*', $this->connection->ldapBase, array('dn'), $limit, $offset);
|
|
|
|
}
|
|
|
|
|
2017-05-21 00:24:38 +00:00
|
|
|
/**
|
|
|
|
* Returns the LDAP handler
|
|
|
|
* @throws \OC\ServerNotAvailableException
|
|
|
|
*/
|
|
|
|
|
2017-05-20 07:53:46 +00:00
|
|
|
/**
|
|
|
|
* @return mixed
|
2017-05-21 00:24:38 +00:00
|
|
|
* @throws \OC\ServerNotAvailableException
|
2017-05-20 07:53:46 +00:00
|
|
|
*/
|
|
|
|
private function invokeLDAPMethod() {
|
|
|
|
$arguments = func_get_args();
|
|
|
|
$command = array_shift($arguments);
|
2017-05-21 00:24:38 +00:00
|
|
|
$cr = array_shift($arguments);
|
2017-05-20 07:53:46 +00:00
|
|
|
if (!method_exists($this->ldap, $command)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
array_unshift($arguments, $cr);
|
|
|
|
// php no longer supports call-time pass-by-reference
|
2017-05-26 11:44:43 +00:00
|
|
|
// thus cannot support controlPagedResultResponse as the third argument
|
|
|
|
// is a reference
|
2017-05-20 07:53:46 +00:00
|
|
|
$doMethod = function () use ($command, &$arguments) {
|
|
|
|
if ($command == 'controlPagedResultResponse') {
|
2017-05-26 11:44:43 +00:00
|
|
|
throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.');
|
2017-05-20 07:53:46 +00:00
|
|
|
} else {
|
|
|
|
return call_user_func_array(array($this->ldap, $command), $arguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
$ret = $doMethod();
|
|
|
|
} catch (ServerNotAvailableException $e) {
|
|
|
|
/* Server connection lost, attempt to reestablish it
|
|
|
|
* Maybe implement exponential backoff?
|
|
|
|
* This was enough to get solr indexer working which has large delays between LDAP fetches.
|
|
|
|
*/
|
2018-04-25 13:22:28 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap', "Connection lost on $command, attempting to reestablish.", ILogger::DEBUG);
|
2017-05-24 22:45:48 +00:00
|
|
|
$this->connection->resetConnectionResource();
|
2017-05-20 07:53:46 +00:00
|
|
|
$cr = $this->connection->getConnectionResource();
|
2017-05-24 22:45:48 +00:00
|
|
|
|
2017-05-20 07:53:46 +00:00
|
|
|
if(!$this->ldap->isResource($cr)) {
|
|
|
|
// Seems like we didn't find any resource.
|
2018-04-25 13:22:28 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", ILogger::DEBUG);
|
2017-05-24 22:45:48 +00:00
|
|
|
throw $e;
|
2017-05-20 07:53:46 +00:00
|
|
|
}
|
2017-05-24 22:45:48 +00:00
|
|
|
|
|
|
|
$arguments[0] = array_pad([], count($arguments[0]), $cr);
|
2017-05-20 07:53:46 +00:00
|
|
|
$ret = $doMethod();
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2014-06-11 11:35:35 +00:00
|
|
|
/**
|
2013-02-26 21:27:09 +00:00
|
|
|
* retrieved. Results will according to the order in the array.
|
2017-12-07 16:49:33 +00:00
|
|
|
*
|
|
|
|
* @param $filter
|
|
|
|
* @param $base
|
2017-12-07 21:02:54 +00:00
|
|
|
* @param string[]|string|null $attr
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param int $limit optional, maximum results to be counted
|
|
|
|
* @param int $offset optional, a starting point
|
|
|
|
* @return array|false array with the search result as first value and pagedSearchOK as
|
2014-01-08 11:07:57 +00:00
|
|
|
* second | false if not successful
|
2017-12-07 16:49:33 +00:00
|
|
|
* @throws ServerNotAvailableException
|
2012-04-14 16:26:46 +00:00
|
|
|
*/
|
2014-01-08 11:07:57 +00:00
|
|
|
private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) {
|
2012-04-25 18:34:58 +00:00
|
|
|
if(!is_null($attr) && !is_array($attr)) {
|
2012-07-02 18:31:07 +00:00
|
|
|
$attr = array(mb_strtolower($attr, 'UTF-8'));
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
2012-05-28 13:54:38 +00:00
|
|
|
|
2012-10-26 19:53:56 +00:00
|
|
|
// See if we have a resource, in case not cancel with message
|
2014-01-08 11:07:57 +00:00
|
|
|
$cr = $this->connection->getConnectionResource();
|
|
|
|
if(!$this->ldap->isResource($cr)) {
|
2012-05-28 13:54:38 +00:00
|
|
|
// Seems like we didn't find any resource.
|
|
|
|
// Return an empty array just like before.
|
2018-04-25 13:22:28 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', ILogger::DEBUG);
|
2014-01-08 11:07:57 +00:00
|
|
|
return false;
|
2012-05-01 10:17:33 +00:00
|
|
|
}
|
2012-04-14 16:05:29 +00:00
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
//check whether paged search should be attempted
|
2018-01-26 14:01:27 +00:00
|
|
|
$pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, (int)$limit, $offset);
|
2012-10-26 19:53:56 +00:00
|
|
|
|
2014-01-08 11:07:57 +00:00
|
|
|
$linkResources = array_pad(array(), count($base), $cr);
|
2017-05-21 00:24:38 +00:00
|
|
|
$sr = $this->invokeLDAPMethod('search', $linkResources, $base, $filter, $attr);
|
2017-05-24 22:45:48 +00:00
|
|
|
// cannot use $cr anymore, might have changed in the previous call!
|
|
|
|
$error = $this->ldap->errno($this->connection->getConnectionResource());
|
2013-04-20 20:45:50 +00:00
|
|
|
if(!is_array($sr) || $error !== 0) {
|
2018-04-25 13:22:28 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'Attempt for Paging? '.print_r($pagedSearchOK, true), ILogger::ERROR);
|
2014-01-08 11:07:57 +00:00
|
|
|
return false;
|
2012-10-27 15:35:39 +00:00
|
|
|
}
|
2013-02-26 21:27:09 +00:00
|
|
|
|
2014-01-08 11:07:57 +00:00
|
|
|
return array($sr, $pagedSearchOK);
|
|
|
|
}
|
2013-02-26 21:27:09 +00:00
|
|
|
|
2014-01-08 11:07:57 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* processes an LDAP paged search operation
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param array $sr the array containing the LDAP search resources
|
|
|
|
* @param string $filter the LDAP filter for the search
|
|
|
|
* @param array $base an array containing the LDAP subtree(s) that shall be searched
|
2017-09-11 13:30:44 +00:00
|
|
|
* @param int $iFoundItems number of results in the single search operation
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param int $limit maximum results to be counted
|
|
|
|
* @param int $offset a starting point
|
|
|
|
* @param bool $pagedSearchOK whether a paged search has been executed
|
|
|
|
* @param bool $skipHandling required for paged search when cookies to
|
2014-01-08 11:07:57 +00:00
|
|
|
* prior results need to be gained
|
2015-07-16 20:17:57 +00:00
|
|
|
* @return bool cookie validity, true if we have more pages, false otherwise.
|
2014-01-08 11:07:57 +00:00
|
|
|
*/
|
|
|
|
private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) {
|
2015-07-16 20:17:57 +00:00
|
|
|
$cookie = null;
|
2012-10-26 19:53:56 +00:00
|
|
|
if($pagedSearchOK) {
|
2014-01-08 11:07:57 +00:00
|
|
|
$cr = $this->connection->getConnectionResource();
|
2013-01-04 22:54:42 +00:00
|
|
|
foreach($sr as $key => $res) {
|
2017-05-26 11:44:43 +00:00
|
|
|
if($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) {
|
2013-01-04 22:54:42 +00:00
|
|
|
$this->setPagedResultCookie($base[$key], $filter, $limit, $offset, $cookie);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-26 19:53:56 +00:00
|
|
|
//browsing through prior pages to get the cookie for the new one
|
|
|
|
if($skipHandling) {
|
2016-08-17 07:46:54 +00:00
|
|
|
return false;
|
2012-10-26 19:53:56 +00:00
|
|
|
}
|
2013-02-14 21:16:48 +00:00
|
|
|
// if count is bigger, then the server does not support
|
|
|
|
// paged search. Instead, he did a normal search. We set a
|
|
|
|
// flag here, so the callee knows how to deal with it.
|
2014-01-08 11:07:57 +00:00
|
|
|
if($iFoundItems <= $limit) {
|
2012-10-26 19:53:56 +00:00
|
|
|
$this->pagedSearchedSuccessful = true;
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-26 14:01:27 +00:00
|
|
|
if(!is_null($limit) && (int)$this->connection->ldapPagingSize !== 0) {
|
2017-12-21 13:29:56 +00:00
|
|
|
\OC::$server->getLogger()->debug(
|
|
|
|
'Paged search was not available',
|
|
|
|
[ 'app' => 'user_ldap' ]
|
|
|
|
);
|
2013-01-04 22:54:42 +00:00
|
|
|
}
|
2012-10-26 19:53:56 +00:00
|
|
|
}
|
2015-07-16 20:17:57 +00:00
|
|
|
/* ++ Fixing RHDS searches with pages with zero results ++
|
|
|
|
* Return cookie status. If we don't have more pages, with RHDS
|
|
|
|
* cookie is null, with openldap cookie is an empty string and
|
|
|
|
* to 386ds '0' is a valid cookie. Even if $iFoundItems == 0
|
|
|
|
*/
|
|
|
|
return !empty($cookie) || $cookie === '0';
|
2014-01-08 11:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* executes an LDAP search, but counts the results only
|
2017-12-07 21:02:54 +00:00
|
|
|
*
|
2014-02-19 08:31:54 +00:00
|
|
|
* @param string $filter the LDAP filter for the search
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param array $base an array containing the LDAP subtree(s) that shall be searched
|
2014-05-15 12:12:17 +00:00
|
|
|
* @param string|string[] $attr optional, array, one or more attributes that shall be
|
2014-01-08 11:07:57 +00:00
|
|
|
* retrieved. Results will according to the order in the array.
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param int $limit optional, maximum results to be counted
|
|
|
|
* @param int $offset optional, a starting point
|
|
|
|
* @param bool $skipHandling indicates whether the pages search operation is
|
2014-01-08 11:07:57 +00:00
|
|
|
* completed
|
2014-05-11 14:27:18 +00:00
|
|
|
* @return int|false Integer or false if the search could not be initialized
|
2017-12-07 21:02:54 +00:00
|
|
|
* @throws ServerNotAvailableException
|
2014-01-08 11:07:57 +00:00
|
|
|
*/
|
|
|
|
private function count($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) {
|
2018-04-25 13:22:28 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'Count filter: '.print_r($filter, true), ILogger::DEBUG);
|
2014-03-19 23:21:51 +00:00
|
|
|
|
2018-01-26 14:01:27 +00:00
|
|
|
$limitPerPage = (int)$this->connection->ldapPagingSize;
|
2014-11-21 13:51:20 +00:00
|
|
|
if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) {
|
|
|
|
$limitPerPage = $limit;
|
2014-01-08 11:07:57 +00:00
|
|
|
}
|
2014-03-19 23:21:51 +00:00
|
|
|
|
2014-01-08 11:07:57 +00:00
|
|
|
$counter = 0;
|
2014-03-20 08:14:41 +00:00
|
|
|
$count = null;
|
2014-05-11 13:17:27 +00:00
|
|
|
$this->connection->getConnectionResource();
|
2014-03-19 23:21:51 +00:00
|
|
|
|
|
|
|
do {
|
2017-12-07 21:02:54 +00:00
|
|
|
$search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset);
|
2014-03-19 23:21:51 +00:00
|
|
|
if($search === false) {
|
|
|
|
return $counter > 0 ? $counter : false;
|
|
|
|
}
|
|
|
|
list($sr, $pagedSearchOK) = $search;
|
|
|
|
|
2015-07-16 20:17:57 +00:00
|
|
|
/* ++ Fixing RHDS searches with pages with zero results ++
|
|
|
|
* countEntriesInSearchResults() method signature changed
|
|
|
|
* by removing $limit and &$hasHitLimit parameters
|
|
|
|
*/
|
|
|
|
$count = $this->countEntriesInSearchResults($sr);
|
2014-03-20 10:38:05 +00:00
|
|
|
$counter += $count;
|
2014-01-08 11:07:57 +00:00
|
|
|
|
2015-07-16 20:17:57 +00:00
|
|
|
$hasMorePages = $this->processPagedSearchStatus($sr, $filter, $base, $count, $limitPerPage,
|
2014-01-08 11:07:57 +00:00
|
|
|
$offset, $pagedSearchOK, $skipHandling);
|
2014-10-29 09:24:48 +00:00
|
|
|
$offset += $limitPerPage;
|
2015-07-16 20:17:57 +00:00
|
|
|
/* ++ Fixing RHDS searches with pages with zero results ++
|
|
|
|
* Continue now depends on $hasMorePages value
|
|
|
|
*/
|
|
|
|
$continue = $pagedSearchOK && $hasMorePages;
|
2014-10-29 09:24:48 +00:00
|
|
|
} while($continue && (is_null($limit) || $limit <= 0 || $limit > $counter));
|
2014-01-08 11:07:57 +00:00
|
|
|
|
|
|
|
return $counter;
|
|
|
|
}
|
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
/**
|
2014-05-13 11:29:25 +00:00
|
|
|
* @param array $searchResults
|
2014-05-11 13:17:27 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2015-07-16 20:17:57 +00:00
|
|
|
private function countEntriesInSearchResults($searchResults) {
|
2014-05-11 13:17:27 +00:00
|
|
|
$counter = 0;
|
2014-03-20 10:38:05 +00:00
|
|
|
|
|
|
|
foreach($searchResults as $res) {
|
2018-01-26 14:01:27 +00:00
|
|
|
$count = (int)$this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res);
|
2014-03-20 10:38:05 +00:00
|
|
|
$counter += $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $counter;
|
|
|
|
}
|
|
|
|
|
2014-01-08 11:07:57 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Executes an LDAP search
|
2017-12-07 16:49:33 +00:00
|
|
|
*
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param string $filter the LDAP filter for the search
|
|
|
|
* @param array $base an array containing the LDAP subtree(s) that shall be searched
|
2014-05-15 12:12:17 +00:00
|
|
|
* @param string|string[] $attr optional, array, one or more attributes that shall be
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @param bool $skipHandling
|
|
|
|
* @return array with the search result
|
2017-12-07 16:49:33 +00:00
|
|
|
* @throws ServerNotAvailableException
|
2014-01-08 11:07:57 +00:00
|
|
|
*/
|
2017-04-06 08:26:41 +00:00
|
|
|
public function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) {
|
2018-01-26 14:01:27 +00:00
|
|
|
$limitPerPage = (int)$this->connection->ldapPagingSize;
|
2017-09-11 10:10:32 +00:00
|
|
|
if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) {
|
|
|
|
$limitPerPage = $limit;
|
2014-07-01 20:02:41 +00:00
|
|
|
}
|
2014-01-08 11:07:57 +00:00
|
|
|
|
2015-07-16 20:17:57 +00:00
|
|
|
/* ++ Fixing RHDS searches with pages with zero results ++
|
|
|
|
* As we can have pages with zero results and/or pages with less
|
|
|
|
* than $limit results but with a still valid server 'cookie',
|
|
|
|
* loops through until we get $continue equals true and
|
|
|
|
* $findings['count'] < $limit
|
|
|
|
*/
|
2017-12-07 16:49:33 +00:00
|
|
|
$findings = [];
|
2015-07-16 20:17:57 +00:00
|
|
|
$savedoffset = $offset;
|
|
|
|
do {
|
2017-09-11 10:10:32 +00:00
|
|
|
$search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset);
|
2015-07-16 20:17:57 +00:00
|
|
|
if($search === false) {
|
2017-12-07 16:49:33 +00:00
|
|
|
return [];
|
2015-07-16 20:17:57 +00:00
|
|
|
}
|
|
|
|
list($sr, $pagedSearchOK) = $search;
|
|
|
|
$cr = $this->connection->getConnectionResource();
|
2014-01-08 11:07:57 +00:00
|
|
|
|
2015-07-16 20:17:57 +00:00
|
|
|
if($skipHandling) {
|
|
|
|
//i.e. result do not need to be fetched, we just need the cookie
|
|
|
|
//thus pass 1 or any other value as $iFoundItems because it is not
|
|
|
|
//used
|
2017-09-11 10:10:32 +00:00
|
|
|
$this->processPagedSearchStatus($sr, $filter, $base, 1, $limitPerPage,
|
2015-07-16 20:17:57 +00:00
|
|
|
$offset, $pagedSearchOK,
|
|
|
|
$skipHandling);
|
|
|
|
return array();
|
2014-01-08 11:07:57 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 13:30:44 +00:00
|
|
|
$iFoundItems = 0;
|
2015-07-16 20:17:57 +00:00
|
|
|
foreach($sr as $res) {
|
2017-05-21 00:24:38 +00:00
|
|
|
$findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $res));
|
2017-09-11 13:30:44 +00:00
|
|
|
$iFoundItems = max($iFoundItems, $findings['count']);
|
|
|
|
unset($findings['count']);
|
2015-07-16 20:17:57 +00:00
|
|
|
}
|
2014-01-08 11:07:57 +00:00
|
|
|
|
2017-09-11 13:30:44 +00:00
|
|
|
$continue = $this->processPagedSearchStatus($sr, $filter, $base, $iFoundItems,
|
2017-09-11 10:10:32 +00:00
|
|
|
$limitPerPage, $offset, $pagedSearchOK,
|
2014-01-08 11:07:57 +00:00
|
|
|
$skipHandling);
|
2017-09-11 10:10:32 +00:00
|
|
|
$offset += $limitPerPage;
|
2017-09-11 13:30:44 +00:00
|
|
|
} while ($continue && $pagedSearchOK && ($limit === null || count($findings) < $limit));
|
2015-07-16 20:17:57 +00:00
|
|
|
// reseting offset
|
|
|
|
$offset = $savedoffset;
|
2012-10-26 19:53:56 +00:00
|
|
|
|
|
|
|
// if we're here, probably no connection resource is returned.
|
2017-04-12 04:16:27 +00:00
|
|
|
// to make Nextcloud behave nicely, we simply give back an empty array.
|
2012-10-26 19:53:56 +00:00
|
|
|
if(is_null($findings)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2012-04-14 16:05:29 +00:00
|
|
|
if(!is_null($attr)) {
|
2017-12-07 16:49:33 +00:00
|
|
|
$selection = [];
|
2015-10-07 16:57:49 +00:00
|
|
|
$i = 0;
|
2012-04-14 16:05:29 +00:00
|
|
|
foreach($findings as $item) {
|
2012-05-17 11:15:26 +00:00
|
|
|
if(!is_array($item)) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-25 10:37:39 +00:00
|
|
|
$item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8');
|
2015-10-07 16:57:49 +00:00
|
|
|
foreach($attr as $key) {
|
2012-05-11 13:42:05 +00:00
|
|
|
if(isset($item[$key])) {
|
2015-10-08 18:32:15 +00:00
|
|
|
if(is_array($item[$key]) && isset($item[$key]['count'])) {
|
|
|
|
unset($item[$key]['count']);
|
|
|
|
}
|
2015-10-07 16:57:49 +00:00
|
|
|
if($key !== 'dn') {
|
2017-12-13 10:57:46 +00:00
|
|
|
if($this->resemblesDN($key)) {
|
|
|
|
$selection[$i][$key] = $this->helper->sanitizeDN($item[$key]);
|
|
|
|
} else if($key === 'objectguid' || $key === 'guid') {
|
|
|
|
$selection[$i][$key] = [$this->convertObjectGUID2Str($item[$key][0])];
|
|
|
|
} else {
|
|
|
|
$selection[$i][$key] = $item[$key];
|
|
|
|
}
|
2012-05-14 10:25:10 +00:00
|
|
|
} else {
|
2016-07-22 08:46:29 +00:00
|
|
|
$selection[$i][$key] = [$this->helper->sanitizeDN($item[$key])];
|
2012-05-14 10:25:10 +00:00
|
|
|
}
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
2015-10-07 16:57:49 +00:00
|
|
|
|
2012-04-14 16:05:29 +00:00
|
|
|
}
|
2015-10-07 16:57:49 +00:00
|
|
|
$i++;
|
2012-04-14 16:05:29 +00:00
|
|
|
}
|
2012-10-27 10:18:50 +00:00
|
|
|
$findings = $selection;
|
|
|
|
}
|
2012-10-27 15:36:08 +00:00
|
|
|
//we slice the findings, when
|
2014-05-11 13:17:27 +00:00
|
|
|
//a) paged search unsuccessful, though attempted
|
2012-10-27 15:36:08 +00:00
|
|
|
//b) no paged search, but limit set
|
2014-03-19 23:21:51 +00:00
|
|
|
if((!$this->getPagedSearchResultState()
|
2012-12-14 22:50:21 +00:00
|
|
|
&& $pagedSearchOK)
|
2012-10-27 10:18:50 +00:00
|
|
|
|| (
|
2012-10-27 15:36:08 +00:00
|
|
|
!$pagedSearchOK
|
|
|
|
&& !is_null($limit)
|
2012-10-27 10:18:50 +00:00
|
|
|
)
|
|
|
|
) {
|
2018-01-26 14:01:27 +00:00
|
|
|
$findings = array_slice($findings, (int)$offset, $limit);
|
2012-04-14 16:05:29 +00:00
|
|
|
}
|
|
|
|
return $findings;
|
|
|
|
}
|
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
/**
|
2014-05-13 11:29:25 +00:00
|
|
|
* @param string $name
|
2018-03-02 16:44:06 +00:00
|
|
|
* @return string
|
2018-03-05 12:30:28 +00:00
|
|
|
* @throws \InvalidArgumentException
|
2014-05-11 13:17:27 +00:00
|
|
|
*/
|
2012-07-25 10:37:39 +00:00
|
|
|
public function sanitizeUsername($name) {
|
2018-03-05 13:03:08 +00:00
|
|
|
$name = trim($name);
|
|
|
|
|
2012-07-25 10:37:39 +00:00
|
|
|
if($this->connection->ldapIgnoreNamingRules) {
|
2018-03-05 13:03:08 +00:00
|
|
|
return $name;
|
2012-05-17 17:33:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 13:03:08 +00:00
|
|
|
// Transliteration to ASCII
|
|
|
|
$transliterated = @iconv('UTF-8', 'ASCII//TRANSLIT', $name);
|
|
|
|
if($transliterated !== false) {
|
|
|
|
// depending on system config iconv can work or not
|
|
|
|
$name = $transliterated;
|
|
|
|
}
|
2012-08-27 19:32:32 +00:00
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
// Replacements
|
2015-06-19 12:55:04 +00:00
|
|
|
$name = str_replace(' ', '_', $name);
|
2012-05-17 13:14:47 +00:00
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
// Every remaining disallowed characters will be removed
|
2012-07-02 18:31:07 +00:00
|
|
|
$name = preg_replace('/[^a-zA-Z0-9_.@-]/u', '', $name);
|
2012-05-17 13:14:47 +00:00
|
|
|
|
2018-03-02 16:44:06 +00:00
|
|
|
if($name === '') {
|
|
|
|
throw new \InvalidArgumentException('provided name template for username does not contain any allowed characters');
|
|
|
|
}
|
|
|
|
|
2012-05-17 13:14:47 +00:00
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2014-02-19 12:13:01 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* escapes (user provided) parts for LDAP filter
|
2014-04-14 15:39:29 +00:00
|
|
|
* @param string $input, the provided value
|
2015-01-28 23:15:55 +00:00
|
|
|
* @param bool $allowAsterisk whether in * at the beginning should be preserved
|
2014-05-11 13:17:27 +00:00
|
|
|
* @return string the escaped string
|
2014-02-19 12:13:01 +00:00
|
|
|
*/
|
2014-12-08 16:12:13 +00:00
|
|
|
public function escapeFilterPart($input, $allowAsterisk = false) {
|
|
|
|
$asterisk = '';
|
|
|
|
if($allowAsterisk && strlen($input) > 0 && $input[0] === '*') {
|
|
|
|
$asterisk = '*';
|
|
|
|
$input = mb_substr($input, 1, null, 'UTF-8');
|
|
|
|
}
|
2014-02-19 12:13:01 +00:00
|
|
|
$search = array('*', '\\', '(', ')');
|
|
|
|
$replace = array('\\*', '\\\\', '\\(', '\\)');
|
2014-12-08 16:12:13 +00:00
|
|
|
return $asterisk . str_replace($search, $replace, $input);
|
2014-02-19 12:13:01 +00:00
|
|
|
}
|
|
|
|
|
2012-04-15 11:37:35 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* combines the input filters with AND
|
2014-05-15 12:12:17 +00:00
|
|
|
* @param string[] $filters the filters to connect
|
2014-05-11 13:17:27 +00:00
|
|
|
* @return string the combined filter
|
2012-04-15 11:37:35 +00:00
|
|
|
*/
|
2012-07-25 10:37:39 +00:00
|
|
|
public function combineFilterWithAnd($filters) {
|
2012-07-25 11:18:31 +00:00
|
|
|
return $this->combineFilter($filters, '&');
|
2012-04-15 11:37:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-11 11:35:35 +00:00
|
|
|
* combines the input filters with OR
|
2014-05-15 12:12:17 +00:00
|
|
|
* @param string[] $filters the filters to connect
|
2014-05-11 13:17:27 +00:00
|
|
|
* @return string the combined filter
|
2014-06-11 11:35:35 +00:00
|
|
|
* Combines Filter arguments with OR
|
2012-04-15 11:37:35 +00:00
|
|
|
*/
|
2012-07-25 10:37:39 +00:00
|
|
|
public function combineFilterWithOr($filters) {
|
2012-07-25 11:18:31 +00:00
|
|
|
return $this->combineFilter($filters, '|');
|
2012-04-15 11:37:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* combines the input filters with given operator
|
2014-05-15 12:12:17 +00:00
|
|
|
* @param string[] $filters the filters to connect
|
2014-02-06 15:30:58 +00:00
|
|
|
* @param string $operator either & or |
|
2014-05-11 13:17:27 +00:00
|
|
|
* @return string the combined filter
|
2012-04-15 11:37:35 +00:00
|
|
|
*/
|
2012-07-25 10:37:39 +00:00
|
|
|
private function combineFilter($filters, $operator) {
|
2012-04-15 11:37:35 +00:00
|
|
|
$combinedFilter = '('.$operator;
|
|
|
|
foreach($filters as $filter) {
|
2016-09-22 22:30:57 +00:00
|
|
|
if ($filter !== '' && $filter[0] !== '(') {
|
2012-04-15 11:37:35 +00:00
|
|
|
$filter = '('.$filter.')';
|
2014-05-15 20:47:28 +00:00
|
|
|
}
|
|
|
|
$combinedFilter.=$filter;
|
2012-04-15 11:37:35 +00:00
|
|
|
}
|
|
|
|
$combinedFilter.=')';
|
|
|
|
return $combinedFilter;
|
|
|
|
}
|
|
|
|
|
2013-01-31 00:46:34 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* creates a filter part for to perform search for users
|
2013-01-31 00:46:34 +00:00
|
|
|
* @param string $search the search term
|
|
|
|
* @return string the final filter part to use in LDAP searches
|
|
|
|
*/
|
|
|
|
public function getFilterPartForUserSearch($search) {
|
2013-02-14 21:16:48 +00:00
|
|
|
return $this->getFilterPartForSearch($search,
|
|
|
|
$this->connection->ldapAttributesForUserSearch,
|
|
|
|
$this->connection->ldapUserDisplayName);
|
2013-01-31 00:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* creates a filter part for to perform search for groups
|
2013-01-31 00:46:34 +00:00
|
|
|
* @param string $search the search term
|
|
|
|
* @return string the final filter part to use in LDAP searches
|
|
|
|
*/
|
|
|
|
public function getFilterPartForGroupSearch($search) {
|
2013-02-14 21:16:48 +00:00
|
|
|
return $this->getFilterPartForSearch($search,
|
|
|
|
$this->connection->ldapAttributesForGroupSearch,
|
|
|
|
$this->connection->ldapGroupDisplayName);
|
2013-01-31 00:46:34 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 11:08:33 +00:00
|
|
|
/**
|
|
|
|
* creates a filter part for searches by splitting up the given search
|
|
|
|
* string into single words
|
|
|
|
* @param string $search the search term
|
|
|
|
* @param string[] $searchAttributes needs to have at least two attributes,
|
|
|
|
* otherwise it does not make sense :)
|
|
|
|
* @return string the final filter part to use in LDAP searches
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
private function getAdvancedFilterPartForSearch($search, $searchAttributes) {
|
|
|
|
if(!is_array($searchAttributes) || count($searchAttributes) < 2) {
|
|
|
|
throw new \Exception('searchAttributes must be an array with at least two string');
|
|
|
|
}
|
|
|
|
$searchWords = explode(' ', trim($search));
|
|
|
|
$wordFilters = array();
|
|
|
|
foreach($searchWords as $word) {
|
2015-12-18 09:54:36 +00:00
|
|
|
$word = $this->prepareSearchTerm($word);
|
2014-11-28 11:08:33 +00:00
|
|
|
//every word needs to appear at least once
|
|
|
|
$wordMatchOneAttrFilters = array();
|
|
|
|
foreach($searchAttributes as $attr) {
|
|
|
|
$wordMatchOneAttrFilters[] = $attr . '=' . $word;
|
|
|
|
}
|
|
|
|
$wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters);
|
|
|
|
}
|
|
|
|
return $this->combineFilterWithAnd($wordFilters);
|
|
|
|
}
|
|
|
|
|
2013-01-31 00:46:34 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* creates a filter part for searches
|
2013-01-31 00:46:34 +00:00
|
|
|
* @param string $search the search term
|
2014-05-15 12:12:17 +00:00
|
|
|
* @param string[]|null $searchAttributes
|
2013-01-31 00:46:34 +00:00
|
|
|
* @param string $fallbackAttribute a fallback attribute in case the user
|
|
|
|
* did not define search attributes. Typically the display name attribute.
|
2014-05-11 13:17:27 +00:00
|
|
|
* @return string the final filter part to use in LDAP searches
|
2013-01-31 00:46:34 +00:00
|
|
|
*/
|
|
|
|
private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) {
|
|
|
|
$filter = array();
|
2014-11-28 11:08:33 +00:00
|
|
|
$haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0);
|
|
|
|
if($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) {
|
|
|
|
try {
|
|
|
|
return $this->getAdvancedFilterPartForSearch($search, $searchAttributes);
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
\OCP\Util::writeLog(
|
|
|
|
'user_ldap',
|
|
|
|
'Creating advanced filter for search failed, falling back to simple method.',
|
2018-04-25 13:22:28 +00:00
|
|
|
ILogger::INFO
|
2014-11-28 11:08:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2015-12-18 09:54:36 +00:00
|
|
|
|
|
|
|
$search = $this->prepareSearchTerm($search);
|
2013-04-20 20:45:50 +00:00
|
|
|
if(!is_array($searchAttributes) || count($searchAttributes) === 0) {
|
2016-09-22 22:30:57 +00:00
|
|
|
if ($fallbackAttribute === '') {
|
2013-01-31 00:46:34 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$filter[] = $fallbackAttribute . '=' . $search;
|
|
|
|
} else {
|
|
|
|
foreach($searchAttributes as $attribute) {
|
|
|
|
$filter[] = $attribute . '=' . $search;
|
|
|
|
}
|
|
|
|
}
|
2013-04-20 20:45:50 +00:00
|
|
|
if(count($filter) === 1) {
|
2013-01-31 00:46:34 +00:00
|
|
|
return '('.$filter[0].')';
|
|
|
|
}
|
|
|
|
return $this->combineFilterWithOr($filter);
|
|
|
|
}
|
|
|
|
|
2015-12-18 09:54:36 +00:00
|
|
|
/**
|
|
|
|
* returns the search term depending on whether we are allowed
|
|
|
|
* list users found by ldap with the current input appended by
|
|
|
|
* a *
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function prepareSearchTerm($term) {
|
|
|
|
$config = \OC::$server->getConfig();
|
|
|
|
|
|
|
|
$allowEnum = $config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes');
|
|
|
|
|
2016-09-22 22:30:57 +00:00
|
|
|
$result = $term;
|
|
|
|
if ($term === '') {
|
|
|
|
$result = '*';
|
|
|
|
} else if ($allowEnum !== 'no') {
|
|
|
|
$result = $term . '*';
|
|
|
|
}
|
2015-12-18 09:54:36 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2014-10-27 22:39:30 +00:00
|
|
|
/**
|
|
|
|
* returns the filter used for counting users
|
2014-11-20 17:03:47 +00:00
|
|
|
* @return string
|
2014-10-27 22:39:30 +00:00
|
|
|
*/
|
|
|
|
public function getFilterForUserCount() {
|
|
|
|
$filter = $this->combineFilterWithAnd(array(
|
|
|
|
$this->connection->ldapUserFilter,
|
|
|
|
$this->connection->ldapUserDisplayName . '=*'
|
|
|
|
));
|
|
|
|
|
|
|
|
return $filter;
|
|
|
|
}
|
|
|
|
|
2014-02-19 08:31:54 +00:00
|
|
|
/**
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param string $name
|
2014-02-19 08:31:54 +00:00
|
|
|
* @param string $password
|
2014-05-11 13:17:27 +00:00
|
|
|
* @return bool
|
2014-02-19 08:31:54 +00:00
|
|
|
*/
|
2012-07-25 10:37:39 +00:00
|
|
|
public function areCredentialsValid($name, $password) {
|
2016-07-22 08:46:29 +00:00
|
|
|
$name = $this->helper->DNasBaseParameter($name);
|
2012-07-25 10:37:39 +00:00
|
|
|
$testConnection = clone $this->connection;
|
|
|
|
$credentials = array(
|
|
|
|
'ldapAgentName' => $name,
|
|
|
|
'ldapAgentPassword' => $password
|
|
|
|
);
|
|
|
|
if(!$testConnection->setConfiguration($credentials)) {
|
2012-04-16 15:28:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-22 19:43:41 +00:00
|
|
|
return $testConnection->bind();
|
2012-04-25 18:34:58 +00:00
|
|
|
}
|
2012-08-23 16:29:43 +00:00
|
|
|
|
2015-12-11 00:56:53 +00:00
|
|
|
/**
|
|
|
|
* reverse lookup of a DN given a known UUID
|
|
|
|
*
|
|
|
|
* @param string $uuid
|
|
|
|
* @return string
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function getUserDnByUuid($uuid) {
|
|
|
|
$uuidOverride = $this->connection->ldapExpertUUIDUserAttr;
|
|
|
|
$filter = $this->connection->ldapUserFilter;
|
|
|
|
$base = $this->connection->ldapBaseUsers;
|
|
|
|
|
2016-09-22 22:30:57 +00:00
|
|
|
if ($this->connection->ldapUuidUserAttribute === 'auto' && $uuidOverride === '') {
|
2015-12-11 00:56:53 +00:00
|
|
|
// Sacrebleu! The UUID attribute is unknown :( We need first an
|
|
|
|
// existing DN to be able to reliably detect it.
|
|
|
|
$result = $this->search($filter, $base, ['dn'], 1);
|
|
|
|
if(!isset($result[0]) || !isset($result[0]['dn'])) {
|
|
|
|
throw new \Exception('Cannot determine UUID attribute');
|
|
|
|
}
|
|
|
|
$dn = $result[0]['dn'][0];
|
|
|
|
if(!$this->detectUuidAttribute($dn, true)) {
|
|
|
|
throw new \Exception('Cannot determine UUID attribute');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The UUID attribute is either known or an override is given.
|
|
|
|
// By calling this method we ensure that $this->connection->$uuidAttr
|
|
|
|
// is definitely set
|
|
|
|
if(!$this->detectUuidAttribute('', true)) {
|
|
|
|
throw new \Exception('Cannot determine UUID attribute');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$uuidAttr = $this->connection->ldapUuidUserAttribute;
|
|
|
|
if($uuidAttr === 'guid' || $uuidAttr === 'objectguid') {
|
2015-12-14 21:42:27 +00:00
|
|
|
$uuid = $this->formatGuid2ForFilterUser($uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
$filter = $uuidAttr . '=' . $uuid;
|
|
|
|
$result = $this->searchUsers($filter, ['dn'], 2);
|
|
|
|
if(is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) {
|
|
|
|
// we put the count into account to make sure that this is
|
|
|
|
// really unique
|
|
|
|
return $result[0]['dn'][0];
|
2015-12-11 00:56:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new \Exception('Cannot determine UUID attribute');
|
|
|
|
}
|
|
|
|
|
2012-08-23 16:29:43 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* auto-detects the directory's UUID attribute
|
2017-10-31 18:42:17 +00:00
|
|
|
*
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param string $dn a known DN used to check against
|
|
|
|
* @param bool $isUser
|
|
|
|
* @param bool $force the detection should be run, even if it is not set to auto
|
2017-10-31 18:42:17 +00:00
|
|
|
* @param array|null $ldapRecord
|
2014-05-11 14:27:18 +00:00
|
|
|
* @return bool true on success, false otherwise
|
2012-08-23 16:29:43 +00:00
|
|
|
*/
|
2017-10-31 18:42:17 +00:00
|
|
|
private function detectUuidAttribute($dn, $isUser = true, $force = false, array $ldapRecord = null) {
|
2013-09-13 17:01:40 +00:00
|
|
|
if($isUser) {
|
|
|
|
$uuidAttr = 'ldapUuidUserAttribute';
|
|
|
|
$uuidOverride = $this->connection->ldapExpertUUIDUserAttr;
|
|
|
|
} else {
|
|
|
|
$uuidAttr = 'ldapUuidGroupAttribute';
|
|
|
|
$uuidOverride = $this->connection->ldapExpertUUIDGroupAttr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(($this->connection->$uuidAttr !== 'auto') && !$force) {
|
2012-08-23 16:29:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-17 01:06:10 +00:00
|
|
|
if (is_string($uuidOverride) && trim($uuidOverride) !== '' && !$force) {
|
2013-09-13 17:01:40 +00:00
|
|
|
$this->connection->$uuidAttr = $uuidOverride;
|
2013-05-08 12:56:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-31 18:42:17 +00:00
|
|
|
foreach(self::UUID_ATTRIBUTES as $attribute) {
|
|
|
|
if($ldapRecord !== null) {
|
|
|
|
// we have the info from LDAP already, we don't need to talk to the server again
|
|
|
|
if(isset($ldapRecord[$attribute])) {
|
|
|
|
$this->connection->$uuidAttr = $attribute;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 16:29:43 +00:00
|
|
|
|
2013-02-14 21:16:48 +00:00
|
|
|
$value = $this->readAttribute($dn, $attribute);
|
|
|
|
if(is_array($value) && isset($value[0]) && !empty($value[0])) {
|
2018-04-25 13:22:28 +00:00
|
|
|
\OCP\Util::writeLog(
|
|
|
|
'user_ldap',
|
|
|
|
'Setting '.$attribute.' as '.$uuidAttr,
|
|
|
|
ILogger::DEBUG
|
|
|
|
);
|
2013-09-13 17:01:40 +00:00
|
|
|
$this->connection->$uuidAttr = $attribute;
|
2012-08-23 16:29:43 +00:00
|
|
|
return true;
|
2013-02-14 21:16:48 +00:00
|
|
|
}
|
2012-08-23 16:29:43 +00:00
|
|
|
}
|
2018-04-25 13:22:28 +00:00
|
|
|
\OCP\Util::writeLog(
|
|
|
|
'user_ldap',
|
|
|
|
'Could not autodetect the UUID attribute',
|
|
|
|
ILogger::ERROR
|
|
|
|
);
|
2012-08-23 16:29:43 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
/**
|
2014-05-13 11:29:25 +00:00
|
|
|
* @param string $dn
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param bool $isUser
|
2017-10-31 18:42:17 +00:00
|
|
|
* @param null $ldapRecord
|
|
|
|
* @return bool|string
|
2014-05-11 13:17:27 +00:00
|
|
|
*/
|
2017-10-31 18:42:17 +00:00
|
|
|
public function getUUID($dn, $isUser = true, $ldapRecord = null) {
|
2013-09-13 17:01:40 +00:00
|
|
|
if($isUser) {
|
|
|
|
$uuidAttr = 'ldapUuidUserAttribute';
|
|
|
|
$uuidOverride = $this->connection->ldapExpertUUIDUserAttr;
|
|
|
|
} else {
|
|
|
|
$uuidAttr = 'ldapUuidGroupAttribute';
|
|
|
|
$uuidOverride = $this->connection->ldapExpertUUIDGroupAttr;
|
|
|
|
}
|
|
|
|
|
|
|
|
$uuid = false;
|
2017-10-31 18:42:17 +00:00
|
|
|
if($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) {
|
2017-10-31 11:20:44 +00:00
|
|
|
$attr = $this->connection->$uuidAttr;
|
2017-10-31 18:42:17 +00:00
|
|
|
$uuid = isset($ldapRecord[$attr]) ? $ldapRecord[$attr] : $this->readAttribute($dn, $attr);
|
2013-09-13 17:01:40 +00:00
|
|
|
if( !is_array($uuid)
|
2016-09-22 22:30:57 +00:00
|
|
|
&& $uuidOverride !== ''
|
2017-10-31 18:42:17 +00:00
|
|
|
&& $this->detectUuidAttribute($dn, $isUser, true, $ldapRecord))
|
|
|
|
{
|
|
|
|
$uuid = isset($ldapRecord[$this->connection->$uuidAttr])
|
|
|
|
? $ldapRecord[$this->connection->$uuidAttr]
|
|
|
|
: $this->readAttribute($dn, $this->connection->$uuidAttr);
|
2012-08-23 16:29:43 +00:00
|
|
|
}
|
|
|
|
if(is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) {
|
|
|
|
$uuid = $uuid[0];
|
|
|
|
}
|
|
|
|
}
|
2013-09-13 17:01:40 +00:00
|
|
|
|
2012-08-23 16:29:43 +00:00
|
|
|
return $uuid;
|
|
|
|
}
|
2012-10-26 19:53:56 +00:00
|
|
|
|
2012-11-08 22:01:28 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* converts a binary ObjectGUID into a string representation
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param string $oguid the ObjectGUID in it's binary form as retrieved from AD
|
|
|
|
* @return string
|
|
|
|
* @link http://www.php.net/manual/en/function.ldap-get-values-len.php#73198
|
2012-11-08 22:01:28 +00:00
|
|
|
*/
|
|
|
|
private function convertObjectGUID2Str($oguid) {
|
|
|
|
$hex_guid = bin2hex($oguid);
|
|
|
|
$hex_guid_to_guid_str = '';
|
|
|
|
for($k = 1; $k <= 4; ++$k) {
|
|
|
|
$hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2);
|
|
|
|
}
|
|
|
|
$hex_guid_to_guid_str .= '-';
|
|
|
|
for($k = 1; $k <= 2; ++$k) {
|
|
|
|
$hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2);
|
|
|
|
}
|
|
|
|
$hex_guid_to_guid_str .= '-';
|
|
|
|
for($k = 1; $k <= 2; ++$k) {
|
|
|
|
$hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2);
|
|
|
|
}
|
|
|
|
$hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4);
|
|
|
|
$hex_guid_to_guid_str .= '-' . substr($hex_guid, 20);
|
|
|
|
|
|
|
|
return strtoupper($hex_guid_to_guid_str);
|
|
|
|
}
|
|
|
|
|
2015-12-14 21:42:27 +00:00
|
|
|
/**
|
|
|
|
* the first three blocks of the string-converted GUID happen to be in
|
|
|
|
* reverse order. In order to use it in a filter, this needs to be
|
|
|
|
* corrected. Furthermore the dashes need to be replaced and \\ preprended
|
|
|
|
* to every two hax figures.
|
|
|
|
*
|
|
|
|
* If an invalid string is passed, it will be returned without change.
|
|
|
|
*
|
|
|
|
* @param string $guid
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function formatGuid2ForFilterUser($guid) {
|
|
|
|
if(!is_string($guid)) {
|
|
|
|
throw new \InvalidArgumentException('String expected');
|
|
|
|
}
|
|
|
|
$blocks = explode('-', $guid);
|
|
|
|
if(count($blocks) !== 5) {
|
|
|
|
/*
|
|
|
|
* Why not throw an Exception instead? This method is a utility
|
|
|
|
* called only when trying to figure out whether a "missing" known
|
|
|
|
* LDAP user was or was not renamed on the LDAP server. And this
|
|
|
|
* even on the use case that a reverse lookup is needed (UUID known,
|
|
|
|
* not DN), i.e. when finding users (search dialog, users page,
|
|
|
|
* login, …) this will not be fired. This occurs only if shares from
|
|
|
|
* a users are supposed to be mounted who cannot be found. Throwing
|
|
|
|
* an exception here would kill the experience for a valid, acting
|
|
|
|
* user. Instead we write a log message.
|
|
|
|
*/
|
|
|
|
\OC::$server->getLogger()->info(
|
|
|
|
'Passed string does not resemble a valid GUID. Known UUID ' .
|
|
|
|
'({uuid}) probably does not match UUID configuration.',
|
|
|
|
[ 'app' => 'user_ldap', 'uuid' => $guid ]
|
|
|
|
);
|
|
|
|
return $guid;
|
|
|
|
}
|
|
|
|
for($i=0; $i < 3; $i++) {
|
|
|
|
$pairs = str_split($blocks[$i], 2);
|
|
|
|
$pairs = array_reverse($pairs);
|
|
|
|
$blocks[$i] = implode('', $pairs);
|
|
|
|
}
|
|
|
|
for($i=0; $i < 5; $i++) {
|
|
|
|
$pairs = str_split($blocks[$i], 2);
|
|
|
|
$blocks[$i] = '\\' . implode('\\', $pairs);
|
|
|
|
}
|
|
|
|
return implode('', $blocks);
|
|
|
|
}
|
|
|
|
|
2014-07-01 20:02:41 +00:00
|
|
|
/**
|
|
|
|
* gets a SID of the domain of the given dn
|
|
|
|
* @param string $dn
|
|
|
|
* @return string|bool
|
|
|
|
*/
|
|
|
|
public function getSID($dn) {
|
|
|
|
$domainDN = $this->getDomainDNFromDN($dn);
|
|
|
|
$cacheKey = 'getSID-'.$domainDN;
|
2016-04-18 08:32:15 +00:00
|
|
|
$sid = $this->connection->getFromCache($cacheKey);
|
|
|
|
if(!is_null($sid)) {
|
|
|
|
return $sid;
|
2014-07-01 20:02:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$objectSid = $this->readAttribute($domainDN, 'objectsid');
|
|
|
|
if(!is_array($objectSid) || empty($objectSid)) {
|
|
|
|
$this->connection->writeToCache($cacheKey, false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$domainObjectSid = $this->convertSID2Str($objectSid[0]);
|
|
|
|
$this->connection->writeToCache($cacheKey, $domainObjectSid);
|
|
|
|
|
|
|
|
return $domainObjectSid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* converts a binary SID into a string representation
|
|
|
|
* @param string $sid
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function convertSID2Str($sid) {
|
2014-09-18 22:01:57 +00:00
|
|
|
// The format of a SID binary string is as follows:
|
|
|
|
// 1 byte for the revision level
|
|
|
|
// 1 byte for the number n of variable sub-ids
|
|
|
|
// 6 bytes for identifier authority value
|
|
|
|
// n*4 bytes for n sub-ids
|
|
|
|
//
|
|
|
|
// Example: 010400000000000515000000a681e50e4d6c6c2bca32055f
|
|
|
|
// Legend: RRNNAAAAAAAAAAAA11111111222222223333333344444444
|
|
|
|
$revision = ord($sid[0]);
|
|
|
|
$numberSubID = ord($sid[1]);
|
|
|
|
|
|
|
|
$subIdStart = 8; // 1 + 1 + 6
|
|
|
|
$subIdLength = 4;
|
|
|
|
if (strlen($sid) !== $subIdStart + $subIdLength * $numberSubID) {
|
|
|
|
// Incorrect number of bytes present.
|
2014-07-01 20:02:41 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2014-09-18 22:01:57 +00:00
|
|
|
// 6 bytes = 48 bits can be represented using floats without loss of
|
|
|
|
// precision (see https://gist.github.com/bantu/886ac680b0aef5812f71)
|
|
|
|
$iav = number_format(hexdec(bin2hex(substr($sid, 2, 6))), 0, '', '');
|
|
|
|
|
|
|
|
$subIDs = array();
|
|
|
|
for ($i = 0; $i < $numberSubID; $i++) {
|
|
|
|
$subID = unpack('V', substr($sid, $subIdStart + $subIdLength * $i, $subIdLength));
|
|
|
|
$subIDs[] = sprintf('%u', $subID[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Result for example above: S-1-5-21-249921958-728525901-1594176202
|
|
|
|
return sprintf('S-%d-%s-%s', $revision, $iav, implode('-', $subIDs));
|
2014-07-01 20:02:41 +00:00
|
|
|
}
|
|
|
|
|
2013-01-03 20:38:53 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* checks if the given DN is part of the given base DN(s)
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param string $dn the DN
|
2014-05-15 12:12:17 +00:00
|
|
|
* @param string[] $bases array containing the allowed base DN or DNs
|
2014-05-11 13:17:27 +00:00
|
|
|
* @return bool
|
2013-01-03 20:38:53 +00:00
|
|
|
*/
|
2014-10-10 19:29:11 +00:00
|
|
|
public function isDNPartOfBase($dn, $bases) {
|
2014-05-11 13:17:27 +00:00
|
|
|
$belongsToBase = false;
|
2016-07-22 08:46:29 +00:00
|
|
|
$bases = $this->helper->sanitizeDN($bases);
|
2014-05-11 13:17:27 +00:00
|
|
|
|
2013-01-03 20:38:53 +00:00
|
|
|
foreach($bases as $base) {
|
|
|
|
$belongsToBase = true;
|
2013-10-18 12:40:08 +00:00
|
|
|
if(mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8')-mb_strlen($base, 'UTF-8'))) {
|
2013-01-03 20:38:53 +00:00
|
|
|
$belongsToBase = false;
|
2013-01-08 18:00:49 +00:00
|
|
|
}
|
|
|
|
if($belongsToBase) {
|
2013-01-03 20:38:53 +00:00
|
|
|
break;
|
2013-01-08 18:00:49 +00:00
|
|
|
}
|
2013-01-03 20:38:53 +00:00
|
|
|
}
|
|
|
|
return $belongsToBase;
|
|
|
|
}
|
|
|
|
|
2014-08-19 16:01:58 +00:00
|
|
|
/**
|
|
|
|
* resets a running Paged Search operation
|
2018-10-25 20:50:04 +00:00
|
|
|
*
|
|
|
|
* @throws ServerNotAvailableException
|
2014-08-19 16:01:58 +00:00
|
|
|
*/
|
|
|
|
private function abandonPagedSearch() {
|
2018-10-25 20:50:04 +00:00
|
|
|
$cr = $this->connection->getConnectionResource();
|
|
|
|
$this->invokeLDAPMethod('controlPagedResult', $cr, 0, false, $this->lastCookie);
|
|
|
|
$this->getPagedSearchResultState();
|
|
|
|
$this->lastCookie = '';
|
|
|
|
$this->cookies = [];
|
2014-08-19 16:01:58 +00:00
|
|
|
}
|
|
|
|
|
2012-10-26 19:53:56 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* get a cookie for the next LDAP paged search
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param string $base a string with the base DN for the search
|
|
|
|
* @param string $filter the search filter to identify the correct search
|
|
|
|
* @param int $limit the limit (or 'pageSize'), to identify the correct search well
|
|
|
|
* @param int $offset the offset for the new search to identify the correct search really good
|
2014-05-11 14:27:18 +00:00
|
|
|
* @return string containing the key or empty if none is cached
|
2012-10-26 19:53:56 +00:00
|
|
|
*/
|
2013-01-04 22:54:42 +00:00
|
|
|
private function getPagedResultCookie($base, $filter, $limit, $offset) {
|
2013-04-20 20:45:50 +00:00
|
|
|
if($offset === 0) {
|
2012-10-26 19:53:56 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$offset -= $limit;
|
|
|
|
//we work with cache here
|
2018-01-26 14:01:27 +00:00
|
|
|
$cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . (int)$limit . '-' . (int)$offset;
|
2013-10-25 12:15:05 +00:00
|
|
|
$cookie = '';
|
2014-05-11 13:17:27 +00:00
|
|
|
if(isset($this->cookies[$cacheKey])) {
|
|
|
|
$cookie = $this->cookies[$cacheKey];
|
2013-10-25 12:15:05 +00:00
|
|
|
if(is_null($cookie)) {
|
|
|
|
$cookie = '';
|
|
|
|
}
|
2012-10-26 19:53:56 +00:00
|
|
|
}
|
|
|
|
return $cookie;
|
|
|
|
}
|
|
|
|
|
2015-09-30 22:30:05 +00:00
|
|
|
/**
|
|
|
|
* checks whether an LDAP paged search operation has more pages that can be
|
|
|
|
* retrieved, typically when offset and limit are provided.
|
|
|
|
*
|
|
|
|
* Be very careful to use it: the last cookie value, which is inspected, can
|
|
|
|
* be reset by other operations. Best, call it immediately after a search(),
|
|
|
|
* searchUsers() or searchGroups() call. count-methods are probably safe as
|
|
|
|
* well. Don't rely on it with any fetchList-method.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasMoreResults() {
|
|
|
|
if(empty($this->lastCookie) && $this->lastCookie !== '0') {
|
|
|
|
// as in RFC 2696, when all results are returned, the cookie will
|
|
|
|
// be empty.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-26 19:53:56 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* set a cookie for LDAP paged search run
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param string $base a string with the base DN for the search
|
|
|
|
* @param string $filter the search filter to identify the correct search
|
|
|
|
* @param int $limit the limit (or 'pageSize'), to identify the correct search well
|
|
|
|
* @param int $offset the offset for the run search to identify the correct search really good
|
|
|
|
* @param string $cookie string containing the cookie returned by ldap_control_paged_result_response
|
2012-10-26 19:53:56 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2013-01-04 22:54:42 +00:00
|
|
|
private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) {
|
2015-03-12 21:47:20 +00:00
|
|
|
// allow '0' for 389ds
|
|
|
|
if(!empty($cookie) || $cookie === '0') {
|
2018-01-26 14:01:27 +00:00
|
|
|
$cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . (int)$limit . '-' . (int)$offset;
|
2014-05-11 13:17:27 +00:00
|
|
|
$this->cookies[$cacheKey] = $cookie;
|
2014-08-19 16:01:58 +00:00
|
|
|
$this->lastCookie = $cookie;
|
2012-10-26 19:53:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Check whether the most recent paged search was successful. It flushed the state var. Use it always after a possible paged search.
|
2014-02-06 15:30:58 +00:00
|
|
|
* @return boolean|null true on success, null or false otherwise
|
2012-10-26 19:53:56 +00:00
|
|
|
*/
|
|
|
|
public function getPagedSearchResultState() {
|
|
|
|
$result = $this->pagedSearchedSuccessful;
|
|
|
|
$this->pagedSearchedSuccessful = null;
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2012-10-27 09:02:35 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Prepares a paged search, if possible
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param string $filter the LDAP filter for the search
|
2014-05-15 12:12:17 +00:00
|
|
|
* @param string[] $bases an array containing the LDAP subtree(s) that shall be searched
|
|
|
|
* @param string[] $attr optional, when a certain attribute shall be filtered outside
|
2014-05-11 13:17:27 +00:00
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @return bool|true
|
2012-10-27 09:02:35 +00:00
|
|
|
*/
|
2013-01-04 22:54:42 +00:00
|
|
|
private function initPagedSearch($filter, $bases, $attr, $limit, $offset) {
|
2012-10-27 09:02:35 +00:00
|
|
|
$pagedSearchOK = false;
|
2018-10-25 20:50:04 +00:00
|
|
|
if ($limit !== 0) {
|
2018-01-26 14:01:27 +00:00
|
|
|
$offset = (int)$offset; //can be null
|
2013-02-14 21:16:48 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap',
|
2013-12-10 16:50:45 +00:00
|
|
|
'initializing paged search for Filter '.$filter.' base '.print_r($bases, true)
|
2013-02-14 21:16:48 +00:00
|
|
|
.' attr '.print_r($attr, true). ' limit ' .$limit.' offset '.$offset,
|
2018-04-25 13:22:28 +00:00
|
|
|
ILogger::DEBUG);
|
2012-10-27 09:02:35 +00:00
|
|
|
//get the cookie from the search for the previous search, required by LDAP
|
2013-01-04 22:54:42 +00:00
|
|
|
foreach($bases as $base) {
|
|
|
|
|
|
|
|
$cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset);
|
2015-03-12 21:47:20 +00:00
|
|
|
if(empty($cookie) && $cookie !== "0" && ($offset > 0)) {
|
2017-10-25 15:23:42 +00:00
|
|
|
// no cookie known from a potential previous search. We need
|
|
|
|
// to start from 0 to come to the desired page. cookie value
|
|
|
|
// of '0' is valid, because 389ds
|
2018-01-11 14:17:18 +00:00
|
|
|
$reOffset = ($offset - $limit) < 0 ? 0 : $offset - $limit;
|
|
|
|
$this->search($filter, array($base), $attr, $limit, $reOffset, true);
|
2013-01-04 22:54:42 +00:00
|
|
|
$cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset);
|
|
|
|
//still no cookie? obviously, the server does not like us. Let's skip paging efforts.
|
2017-10-25 15:23:42 +00:00
|
|
|
// '0' is valid, because 389ds
|
2013-01-04 22:54:42 +00:00
|
|
|
//TODO: remember this, probably does not change in the next request...
|
2015-03-12 21:47:20 +00:00
|
|
|
if(empty($cookie) && $cookie !== '0') {
|
2013-01-04 22:54:42 +00:00
|
|
|
$cookie = null;
|
|
|
|
}
|
2012-10-27 09:02:35 +00:00
|
|
|
}
|
2013-01-04 22:54:42 +00:00
|
|
|
if(!is_null($cookie)) {
|
2014-09-18 15:00:51 +00:00
|
|
|
//since offset = 0, this is a new search. We abandon other searches that might be ongoing.
|
|
|
|
$this->abandonPagedSearch();
|
2017-05-21 00:24:38 +00:00
|
|
|
$pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult',
|
|
|
|
$this->connection->getConnectionResource(), $limit,
|
|
|
|
false, $cookie);
|
2013-01-04 22:54:42 +00:00
|
|
|
if(!$pagedSearchOK) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-25 13:22:28 +00:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'Ready for a paged search', ILogger::DEBUG);
|
2013-01-04 22:54:42 +00:00
|
|
|
} else {
|
2018-01-11 13:17:14 +00:00
|
|
|
$e = new \Exception('No paged search possible, Limit '.$limit.' Offset '.$offset);
|
2018-04-25 13:22:28 +00:00
|
|
|
\OC::$server->getLogger()->logException($e, ['level' => ILogger::DEBUG]);
|
2012-10-27 09:02:35 +00:00
|
|
|
}
|
2013-01-04 22:54:42 +00:00
|
|
|
|
2012-10-27 09:02:35 +00:00
|
|
|
}
|
2015-07-16 20:17:57 +00:00
|
|
|
/* ++ Fixing RHDS searches with pages with zero results ++
|
|
|
|
* We coudn't get paged searches working with our RHDS for login ($limit = 0),
|
|
|
|
* due to pages with zero results.
|
|
|
|
* So we added "&& !empty($this->lastCookie)" to this test to ignore pagination
|
|
|
|
* if we don't have a previous paged search.
|
|
|
|
*/
|
2018-10-25 20:50:04 +00:00
|
|
|
} else if ($limit === 0 && !empty($this->lastCookie)) {
|
2015-03-05 12:15:21 +00:00
|
|
|
// a search without limit was requested. However, if we do use
|
|
|
|
// Paged Search once, we always must do it. This requires us to
|
|
|
|
// initialize it with the configured page size.
|
|
|
|
$this->abandonPagedSearch();
|
|
|
|
// in case someone set it to 0 … use 500, otherwise no results will
|
|
|
|
// be returned.
|
2018-01-26 14:01:27 +00:00
|
|
|
$pageSize = (int)$this->connection->ldapPagingSize > 0 ? (int)$this->connection->ldapPagingSize : 500;
|
2017-05-21 00:24:38 +00:00
|
|
|
$pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult',
|
|
|
|
$this->connection->getConnectionResource(),
|
|
|
|
$pageSize, false, '');
|
2012-10-27 09:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $pagedSearchOK;
|
|
|
|
}
|
|
|
|
|
2018-11-24 21:06:45 +00:00
|
|
|
/**
|
|
|
|
* Is more than one $attr used for search?
|
|
|
|
*
|
|
|
|
* @param string|string[]|null $attr
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-11-25 11:30:54 +00:00
|
|
|
private function manyAttributes($attr): bool {
|
2018-11-24 21:06:45 +00:00
|
|
|
if (\is_array($attr)) {
|
|
|
|
return \count($attr) > 1;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-06 17:11:14 +00:00
|
|
|
}
|