2012-04-14 20:05:29 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Aaron Wood <aaronjwood@gmail.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Alexander Bergolth <leo@strike.wu.ac.at>
|
|
|
|
* @author Andreas Fischer <bantu@owncloud.com>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Benjamin Diele <benjamin@diele.be>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author bline <scottbeck@gmail.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Christopher Schäpers <kondou@ts.unde.re>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Juan Pablo Villafáñez <jvillafanez@solidgear.es>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Lorenzo M. Catucci <lorenzo@sancho.ccd.uniroma2.it>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Lyonel Vincent <lyonel@ezix.org>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Mario Kolling <mario.kolling@serpro.gov.br>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-10-05 21:54:56 +03:00
|
|
|
* @author Nicolas Grekas <nicolas.grekas@gmail.com>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Ralph Krimmel <rkrimme1@gwdg.de>
|
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2016-07-22 11:46:29 +03:00
|
|
|
* @author Roger Szabo <roger.szabo@web.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author root <root@localhost.localdomain>
|
|
|
|
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
|
2012-04-14 20:05:29 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
2012-04-14 20:05:29 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03: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 20:05:29 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2012-04-14 20:05:29 +04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2012-04-14 20:05:29 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03: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 20:05:29 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2016-05-12 17:42:57 +03:00
|
|
|
namespace OCA\User_LDAP;
|
2012-04-15 15:37:35 +04:00
|
|
|
|
2016-08-30 12:43:29 +03:00
|
|
|
use OC\HintException;
|
2018-03-19 17:53:30 +03:00
|
|
|
use OC\Hooks\PublicEmitter;
|
2016-08-30 12:43:29 +03:00
|
|
|
use OCA\User_LDAP\Exceptions\ConstraintViolationException;
|
2016-05-12 17:09:00 +03:00
|
|
|
use OCA\User_LDAP\User\Manager;
|
2016-05-12 12:25:50 +03:00
|
|
|
use OCA\User_LDAP\User\OfflineUser;
|
2014-11-05 15:05:07 +03:00
|
|
|
use OCA\User_LDAP\Mapping\AbstractMapping;
|
2017-05-20 10:53:46 +03:00
|
|
|
use OC\ServerNotAvailableException;
|
2017-11-08 03:51:14 +03:00
|
|
|
use OCP\IConfig;
|
2018-04-25 16:22:28 +03:00
|
|
|
use OCP\ILogger;
|
2018-03-19 17:53:30 +03:00
|
|
|
use OCP\IUserManager;
|
2017-05-20 10:53:46 +03:00
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
/**
|
|
|
|
* Class Access
|
2016-05-12 17:42:57 +03:00
|
|
|
* @package OCA\User_LDAP
|
2014-05-11 17:17:27 +04:00
|
|
|
*/
|
2018-10-31 00:42:24 +03:00
|
|
|
class Access extends LDAPUtility {
|
2017-10-31 21:42:17 +03:00
|
|
|
const UUID_ATTRIBUTES = ['entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid'];
|
|
|
|
|
2017-10-31 14:20:44 +03:00
|
|
|
/** @var \OCA\User_LDAP\Connection */
|
2013-09-10 19:11:02 +04:00
|
|
|
public $connection;
|
2016-10-07 02:39:57 +03:00
|
|
|
/** @var Manager */
|
2014-03-27 21:01:14 +04:00
|
|
|
public $userManager;
|
2012-10-26 23:53:56 +04:00
|
|
|
//never ever check this var directly, always use getPagedSearchResultState
|
|
|
|
protected $pagedSearchedSuccessful;
|
|
|
|
|
2014-08-19 20:01:58 +04:00
|
|
|
/**
|
|
|
|
* @var string[] $cookies an array of returned Paged Result cookies
|
|
|
|
*/
|
2013-10-25 16:15:05 +04:00
|
|
|
protected $cookies = array();
|
|
|
|
|
2014-08-19 20:01:58 +04:00
|
|
|
/**
|
|
|
|
* @var string $lastCookie the last cookie returned from a Paged Results
|
|
|
|
* operation, defaults to an empty string
|
|
|
|
*/
|
|
|
|
protected $lastCookie = '';
|
2014-06-03 15:01:14 +04:00
|
|
|
|
2014-11-05 15:05:07 +03:00
|
|
|
/**
|
|
|
|
* @var AbstractMapping $userMapper
|
|
|
|
*/
|
|
|
|
protected $userMapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var AbstractMapping $userMapper
|
|
|
|
*/
|
|
|
|
protected $groupMapper;
|
2017-10-31 14:20:44 +03:00
|
|
|
|
2016-07-22 11:46:29 +03:00
|
|
|
/**
|
|
|
|
* @var \OCA\User_LDAP\Helper
|
|
|
|
*/
|
|
|
|
private $helper;
|
2017-11-08 03:51:14 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
2018-03-19 17:53:30 +03:00
|
|
|
/** @var IUserManager */
|
|
|
|
private $ncUserManager;
|
2017-10-31 20:13:48 +03:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
Connection $connection,
|
|
|
|
ILDAPWrapper $ldap,
|
|
|
|
Manager $userManager,
|
|
|
|
Helper $helper,
|
2018-03-19 17:53:30 +03:00
|
|
|
IConfig $config,
|
|
|
|
IUserManager $ncUserManager
|
2017-10-31 20:13:48 +03:00
|
|
|
) {
|
2013-09-10 19:11:02 +04:00
|
|
|
parent::__construct($ldap);
|
2012-07-25 14:37:39 +04:00
|
|
|
$this->connection = $connection;
|
2014-03-27 21:01:14 +04:00
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->userManager->setLdapAccess($this);
|
2016-07-22 11:46:29 +03:00
|
|
|
$this->helper = $helper;
|
2017-11-08 03:51:14 +03:00
|
|
|
$this->config = $config;
|
2018-03-19 17:53:30 +03:00
|
|
|
$this->ncUserManager = $ncUserManager;
|
2012-07-25 14:37:39 +04:00
|
|
|
}
|
|
|
|
|
2014-11-05 15:05:07 +03:00
|
|
|
/**
|
|
|
|
* sets the User Mapper
|
|
|
|
* @param AbstractMapping $mapper
|
|
|
|
*/
|
|
|
|
public function setUserMapper(AbstractMapping $mapper) {
|
|
|
|
$this->userMapper = $mapper;
|
|
|
|
}
|
|
|
|
|
2014-12-20 18:09:04 +03: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 15:05:07 +03:00
|
|
|
/**
|
|
|
|
* sets the Group Mapper
|
|
|
|
* @param AbstractMapping $mapper
|
|
|
|
*/
|
|
|
|
public function setGroupMapper(AbstractMapping $mapper) {
|
|
|
|
$this->groupMapper = $mapper;
|
|
|
|
}
|
|
|
|
|
2014-12-20 18:09:04 +03: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 17:17:27 +04:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-07-25 14:37:39 +04:00
|
|
|
private function checkConnection() {
|
|
|
|
return ($this->connection instanceof Connection);
|
2012-04-16 20:03:02 +04:00
|
|
|
}
|
2012-06-07 20:13:41 +04:00
|
|
|
|
2012-05-17 21:33:38 +04:00
|
|
|
/**
|
2014-06-03 15:01:14 +04:00
|
|
|
* returns the Connection instance
|
2016-05-12 17:35:33 +03:00
|
|
|
* @return \OCA\User_LDAP\Connection
|
2014-03-27 21:01:14 +04:00
|
|
|
*/
|
|
|
|
public function getConnection() {
|
|
|
|
return $this->connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-03 15:01:14 +04:00
|
|
|
* reads a given attribute for an LDAP record identified by a DN
|
2018-01-26 14:47:19 +03:00
|
|
|
*
|
2014-07-02 00:02:41 +04:00
|
|
|
* @param string $dn the record in question
|
|
|
|
* @param string $attr the attribute that shall be retrieved
|
2012-11-05 18:38:49 +04:00
|
|
|
* if empty, just check the record's existence
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param string $filter
|
|
|
|
* @return array|false an array of values on success or an empty
|
2012-11-05 20:35:09 +04:00
|
|
|
* array if $attr is empty, false otherwise
|
2018-01-26 14:47:19 +03:00
|
|
|
* @throws ServerNotAvailableException
|
2012-05-17 21:33:38 +04:00
|
|
|
*/
|
2012-10-27 21:15:13 +04:00
|
|
|
public function readAttribute($dn, $attr, $filter = 'objectClass=*') {
|
2012-07-25 14:37:39 +04:00
|
|
|
if(!$this->checkConnection()) {
|
2013-02-15 01:16:48 +04:00
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
'No LDAP Connector assigned, access impossible for readAttribute.',
|
2018-04-25 16:22:28 +03:00
|
|
|
ILogger::WARN);
|
2012-07-25 14:37:39 +04:00
|
|
|
return false;
|
2012-06-07 20:13:41 +04:00
|
|
|
}
|
2017-05-21 03:24:38 +03:00
|
|
|
$cr = $this->connection->getConnectionResource();
|
|
|
|
if(!$this->ldap->isResource($cr)) {
|
|
|
|
//LDAP not available
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', ILogger::DEBUG);
|
2017-05-21 03:24:38 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-19 20:01:58 +04:00
|
|
|
//Cancel possibly running Paged Results operation, otherwise we run in
|
|
|
|
//LDAP protocol errors
|
|
|
|
$this->abandonPagedSearch();
|
2014-09-30 19:00:25 +04:00
|
|
|
// openLDAP requires that we init a new Paged Search. Not needed by AD,
|
|
|
|
// but does not hurt either.
|
2018-01-26 17:01:27 +03:00
|
|
|
$pagingSize = (int)$this->connection->ldapPagingSize;
|
2015-01-12 18:25:11 +03: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 19:50:53 +03:00
|
|
|
$maxResults = $pagingSize > 20 ? $pagingSize : 500;
|
2016-01-07 22:01:24 +03: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 03:24:38 +03:00
|
|
|
$result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults);
|
2016-01-07 22:01:24 +03: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 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, ILogger::DEBUG);
|
2016-01-07 22:01:24 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs an read operation against LDAP
|
|
|
|
*
|
2017-05-21 03:24:38 +03:00
|
|
|
* @param resource $cr the LDAP connection
|
2016-01-07 22:01:24 +03: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 14:47:19 +03:00
|
|
|
* @throws ServerNotAvailableException
|
2016-01-07 22:01:24 +03:00
|
|
|
*/
|
2017-05-21 03:24:38 +03:00
|
|
|
public function executeRead($cr, $dn, $attribute, $filter, $maxResults) {
|
2016-01-07 22:01:24 +03:00
|
|
|
$this->initPagedSearch($filter, array($dn), array($attribute), $maxResults, 0);
|
2016-07-22 11:46:29 +03:00
|
|
|
$dn = $this->helper->DNasBaseParameter($dn);
|
2017-05-21 03:24:38 +03:00
|
|
|
$rr = @$this->invokeLDAPMethod('read', $cr, $dn, $filter, array($attribute));
|
2016-01-07 22:01:24 +03:00
|
|
|
if (!$this->ldap->isResource($rr)) {
|
|
|
|
if ($attribute !== '') {
|
2013-03-19 03:23:59 +04:00
|
|
|
//do not throw this message on userExists check, irritates
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, ILogger::DEBUG);
|
2013-03-19 03:23:59 +04:00
|
|
|
}
|
2012-07-25 14:54:23 +04:00
|
|
|
//in case an error occurs , e.g. object does not exist
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-21 03:24:38 +03:00
|
|
|
if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', ILogger::DEBUG);
|
2016-01-07 22:01:24 +03:00
|
|
|
return true;
|
2012-11-05 18:38:49 +04:00
|
|
|
}
|
2017-05-21 03:24:38 +03:00
|
|
|
$er = $this->invokeLDAPMethod('firstEntry', $cr, $rr);
|
2016-01-07 22:01:24 +03:00
|
|
|
if (!$this->ldap->isResource($er)) {
|
2012-10-27 21:15:13 +04:00
|
|
|
//did not match the filter, return false
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-25 14:37:39 +04:00
|
|
|
//LDAP attributes are not case sensitive
|
2013-09-11 21:42:08 +04:00
|
|
|
$result = \OCP\Util::mb_array_change_key_case(
|
2017-05-21 03:24:38 +03:00
|
|
|
$this->invokeLDAPMethod('getAttributes', $cr, $er), MB_CASE_LOWER, 'UTF-8');
|
2016-01-07 22:01:24 +03:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2012-04-14 20:05:29 +04:00
|
|
|
|
2016-01-07 22:01:24 +03: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 19:12:48 +03:00
|
|
|
$lowercaseAttribute = strtolower($attribute);
|
2016-01-07 22:01:24 +03:00
|
|
|
for($i=0;$i<$result[$attribute]['count'];$i++) {
|
|
|
|
if($this->resemblesDN($attribute)) {
|
2016-12-05 13:36:20 +03:00
|
|
|
$values[] = $this->helper->sanitizeDN($result[$attribute][$i]);
|
2016-10-04 19:12:48 +03:00
|
|
|
} elseif($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') {
|
2016-01-07 22:01:24 +03:00
|
|
|
$values[] = $this->convertObjectGUID2Str($result[$attribute][$i]);
|
2012-11-09 02:01:28 +04:00
|
|
|
} else {
|
2016-01-07 22:01:24 +03:00
|
|
|
$values[] = $result[$attribute][$i];
|
2012-11-09 02:01:28 +04:00
|
|
|
}
|
2012-07-25 14:37:39 +04:00
|
|
|
}
|
|
|
|
}
|
2016-01-07 22:01:24 +03: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 20:03:02 +04:00
|
|
|
}
|
2016-08-30 12:43:29 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set password for an LDAP user identified by a DN
|
2016-11-23 21:58:43 +03:00
|
|
|
*
|
2016-08-30 12:43:29 +03:00
|
|
|
* @param string $userDN the user in question
|
|
|
|
* @param string $password the new password
|
|
|
|
* @return bool
|
2016-11-23 21:58:43 +03:00
|
|
|
* @throws HintException
|
|
|
|
* @throws \Exception
|
2016-08-30 12:43:29 +03:00
|
|
|
*/
|
|
|
|
public function setPassword($userDN, $password) {
|
2018-01-26 17:01:27 +03:00
|
|
|
if((int)$this->connection->turnOnPasswordChange !== 1) {
|
2016-08-30 12:43:29 +03:00
|
|
|
throw new \Exception('LDAP password changes are disabled.');
|
|
|
|
}
|
2017-05-21 03:24:38 +03:00
|
|
|
$cr = $this->connection->getConnectionResource();
|
|
|
|
if(!$this->ldap->isResource($cr)) {
|
|
|
|
//LDAP not available
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', ILogger::DEBUG);
|
2017-05-21 03:24:38 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-08-30 12:43:29 +03:00
|
|
|
try {
|
2019-03-05 16:07:32 +03:00
|
|
|
// try PASSWD extended operation first
|
|
|
|
return @$this->invokeLDAPMethod('exopPasswd', $cr, $userDN, '', $password) ||
|
|
|
|
@$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password);
|
2016-08-30 12:43:29 +03: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 20:03:02 +04:00
|
|
|
|
2012-04-25 22:34:58 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* checks whether the given attributes value is probably a DN
|
2014-02-06 19:30:58 +04:00
|
|
|
* @param string $attr the attribute in question
|
|
|
|
* @return boolean if so true, otherwise false
|
2012-04-25 22:34:58 +04:00
|
|
|
*/
|
2012-07-25 14:37:39 +04:00
|
|
|
private function resemblesDN($attr) {
|
|
|
|
$resemblingAttributes = array(
|
|
|
|
'dn',
|
|
|
|
'uniquemember',
|
2015-09-28 19:38:57 +03:00
|
|
|
'member',
|
|
|
|
// memberOf is an "operational" attribute, without a definition in any RFC
|
|
|
|
'memberof'
|
2012-04-16 15:27:20 +04:00
|
|
|
);
|
2012-07-25 14:37:39 +04:00
|
|
|
return in_array($attr, $resemblingAttributes);
|
|
|
|
}
|
2012-04-15 16:47:33 +04:00
|
|
|
|
2014-08-11 18:40:41 +04: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-02 00:02:41 +04: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 23:55:24 +03:00
|
|
|
return implode(',', $domainParts);
|
2014-07-02 00:02:41 +04:00
|
|
|
}
|
|
|
|
|
2012-04-25 22:34:58 +04:00
|
|
|
/**
|
2017-04-12 07:16:27 +03: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 15:05:07 +03:00
|
|
|
* @return string|false LDAP DN on success, otherwise false
|
2012-04-25 22:34:58 +04:00
|
|
|
*/
|
2012-07-25 14:37:39 +04:00
|
|
|
public function groupname2dn($name) {
|
2016-05-12 17:55:58 +03:00
|
|
|
return $this->groupMapper->getDNByName($name);
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-12 07:16:27 +03: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 21:31:15 +03:00
|
|
|
* @return string|false with the LDAP DN on success, otherwise false
|
2012-04-25 22:34:58 +04:00
|
|
|
*/
|
2012-07-25 14:37:39 +04:00
|
|
|
public function username2dn($name) {
|
2016-05-12 17:55:58 +03:00
|
|
|
$fdn = $this->userMapper->getDNByName($name);
|
2014-11-05 15:05:07 +03:00
|
|
|
|
2013-11-26 01:08:11 +04:00
|
|
|
//Check whether the DN belongs to the Base, to avoid issues on multi-
|
|
|
|
//server setups
|
2014-11-05 15:05:07 +03:00
|
|
|
if(is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) {
|
|
|
|
return $fdn;
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-12 07:16:27 +03:00
|
|
|
* returns the internal Nextcloud name for the given LDAP DN of the group, false on DN outside of search DN or failure
|
2019-05-27 08:54:09 +03:00
|
|
|
*
|
2014-11-05 15:05:07 +03:00
|
|
|
* @param string $fdn the dn of the group object
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param string $ldapName optional, the display name of the object
|
2017-04-12 07:16:27 +03:00
|
|
|
* @return string|false with the name to use in Nextcloud, false on DN outside of search DN
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws \Exception
|
2012-04-25 22:34:58 +04:00
|
|
|
*/
|
2014-11-05 15:05:07 +03:00
|
|
|
public function dn2groupname($fdn, $ldapName = null) {
|
2013-01-08 22:00:49 +04: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 15:05:07 +03:00
|
|
|
if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) {
|
2012-06-19 03:15:58 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-04 00:38:53 +04:00
|
|
|
|
2014-11-05 15:05:07 +03:00
|
|
|
return $this->dn2ocname($fdn, $ldapName, false);
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
|
|
|
|
2015-06-25 13:41:17 +03: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 17:47:44 +03:00
|
|
|
* @throws ServerNotAvailableException
|
2015-06-25 13:41:17 +03:00
|
|
|
*/
|
|
|
|
public function groupsMatchFilter($groupDNs) {
|
|
|
|
$validGroupDNs = [];
|
|
|
|
foreach($groupDNs as $dn) {
|
|
|
|
$cacheKey = 'groupsMatchFilter-'.$dn;
|
2016-04-18 11:32:15 +03:00
|
|
|
$groupMatchFilter = $this->connection->getFromCache($cacheKey);
|
|
|
|
if(!is_null($groupMatchFilter)) {
|
|
|
|
if($groupMatchFilter) {
|
2015-06-25 13:41:17 +03:00
|
|
|
$validGroupDNs[] = $dn;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-07-09 13:19:04 +03: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 17:47:44 +03:00
|
|
|
$result = $this->readAttribute($dn, '', $this->connection->ldapGroupFilter);
|
2015-06-25 13:41:17 +03:00
|
|
|
if(is_array($result)) {
|
2015-07-09 13:19:04 +03:00
|
|
|
$this->connection->writeToCache($cacheKey, true);
|
2015-06-25 13:41:17 +03:00
|
|
|
$validGroupDNs[] = $dn;
|
2015-07-09 13:19:04 +03:00
|
|
|
} else {
|
|
|
|
$this->connection->writeToCache($cacheKey, false);
|
2015-06-25 13:41:17 +03:00
|
|
|
}
|
2015-07-09 13:19:04 +03:00
|
|
|
|
2015-06-25 13:41:17 +03:00
|
|
|
}
|
|
|
|
return $validGroupDNs;
|
|
|
|
}
|
|
|
|
|
2012-04-25 22:34:58 +04:00
|
|
|
/**
|
2017-04-12 07:16:27 +03:00
|
|
|
* returns the internal Nextcloud name for the given LDAP DN of the user, false on DN outside of search DN or failure
|
2019-05-27 08:54:09 +03:00
|
|
|
*
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param string $dn the dn of the user object
|
|
|
|
* @param string $ldapName optional, the display name of the object
|
2017-04-12 07:16:27 +03:00
|
|
|
* @return string|false with with the name to use in Nextcloud
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws \Exception
|
2012-04-25 22:34:58 +04:00
|
|
|
*/
|
2014-11-05 15:05:07 +03:00
|
|
|
public function dn2username($fdn, $ldapName = null) {
|
2013-01-08 22:00:49 +04: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 15:05:07 +03:00
|
|
|
if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) {
|
2012-06-19 03:15:58 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-04 00:38:53 +04:00
|
|
|
|
2014-11-05 15:05:07 +03:00
|
|
|
return $this->dn2ocname($fdn, $ldapName, true);
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
|
|
|
|
2012-07-25 14:37:39 +04:00
|
|
|
/**
|
2017-04-12 07:16:27 +03:00
|
|
|
* returns an internal Nextcloud name for the given LDAP DN, false on DN outside of search DN
|
2017-10-31 21:42:17 +03:00
|
|
|
*
|
2017-10-31 14:20:44 +03:00
|
|
|
* @param string $fdn the dn of the user object
|
2017-11-08 03:51:14 +03:00
|
|
|
* @param string|null $ldapName optional, the display name of the object
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param bool $isUser optional, whether it is a user object (otherwise group assumed)
|
2017-10-31 14:20:44 +03:00
|
|
|
* @param bool|null $newlyMapped
|
2017-10-31 21:42:17 +03:00
|
|
|
* @param array|null $record
|
|
|
|
* @return false|string with with the name to use in Nextcloud
|
2017-12-08 00:02:54 +03:00
|
|
|
* @throws \Exception
|
2012-07-25 14:37:39 +04:00
|
|
|
*/
|
2017-10-31 21:42:17 +03:00
|
|
|
public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped = null, array $record = null) {
|
2017-10-31 14:20:44 +03:00
|
|
|
$newlyMapped = false;
|
2012-04-25 22:34:58 +04:00
|
|
|
if($isUser) {
|
2014-12-20 18:09:04 +03:00
|
|
|
$mapper = $this->getUserMapper();
|
2012-07-25 14:37:39 +04:00
|
|
|
$nameAttribute = $this->connection->ldapUserDisplayName;
|
2018-03-07 14:18:46 +03:00
|
|
|
$filter = $this->connection->ldapUserFilter;
|
2012-04-25 22:34:58 +04:00
|
|
|
} else {
|
2014-12-20 18:09:04 +03:00
|
|
|
$mapper = $this->getGroupMapper();
|
2012-07-25 14:37:39 +04:00
|
|
|
$nameAttribute = $this->connection->ldapGroupDisplayName;
|
2018-03-07 14:18:46 +03:00
|
|
|
$filter = $this->connection->ldapGroupFilter;
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
|
|
|
|
2017-04-12 07:16:27 +03:00
|
|
|
//let's try to retrieve the Nextcloud name from the mappings table
|
2017-10-31 14:20:44 +03:00
|
|
|
$ncName = $mapper->getNameByDN($fdn);
|
|
|
|
if(is_string($ncName)) {
|
|
|
|
return $ncName;
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
|
|
|
|
2012-08-23 20:29:43 +04:00
|
|
|
//second try: get the UUID and check if it is known. Then, update the DN and return the name.
|
2017-10-31 21:42:17 +03:00
|
|
|
$uuid = $this->getUUID($fdn, $isUser, $record);
|
2014-11-05 15:05:07 +03:00
|
|
|
if(is_string($uuid)) {
|
2017-10-31 14:20:44 +03:00
|
|
|
$ncName = $mapper->getNameByUUID($uuid);
|
|
|
|
if(is_string($ncName)) {
|
2014-11-05 15:05:07 +03:00
|
|
|
$mapper->setDNbyUUID($fdn, $uuid);
|
2017-10-31 14:20:44 +03:00
|
|
|
return $ncName;
|
2012-08-23 20:29:43 +04:00
|
|
|
}
|
2013-02-11 00:52:57 +04:00
|
|
|
} else {
|
|
|
|
//If the UUID can't be detected something is foul.
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'Cannot determine UUID for '.$fdn.'. Skipping.', ILogger::INFO);
|
2013-02-11 00:52:57 +04:00
|
|
|
return false;
|
2012-08-23 20:29:43 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
if(is_null($ldapName)) {
|
2018-03-07 14:18:46 +03:00
|
|
|
$ldapName = $this->readAttribute($fdn, $nameAttribute, $filter);
|
2014-05-11 17:17:27 +04:00
|
|
|
if(!isset($ldapName[0]) && empty($ldapName[0])) {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.' with filter '.$filter.'.', ILogger::INFO);
|
2012-07-30 19:42:33 +04:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-11 17:17:27 +04:00
|
|
|
$ldapName = $ldapName[0];
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
2013-05-08 16:56:52 +04:00
|
|
|
|
|
|
|
if($isUser) {
|
2018-01-26 17:01:27 +03:00
|
|
|
$usernameAttribute = (string)$this->connection->ldapExpertUsernameAttr;
|
2016-09-23 01:30:57 +03:00
|
|
|
if ($usernameAttribute !== '') {
|
2014-11-05 15:05:07 +03:00
|
|
|
$username = $this->readAttribute($fdn, $usernameAttribute);
|
2013-05-08 16:56:52 +04:00
|
|
|
$username = $username[0];
|
|
|
|
} else {
|
|
|
|
$username = $uuid;
|
|
|
|
}
|
2018-03-02 19:44:06 +03:00
|
|
|
try {
|
|
|
|
$intName = $this->sanitizeUsername($username);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'app' => 'user_ldap',
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::WARN,
|
2018-03-02 19:44:06 +03:00
|
|
|
]);
|
|
|
|
// we don't attempt to set a username here. We can go for
|
2018-03-05 15:30:28 +03:00
|
|
|
// for an alternative 4 digit random number as we would append
|
2018-03-02 19:44:06 +03:00
|
|
|
// otherwise, however it's likely not enough space in bigger
|
|
|
|
// setups, and most importantly: this is not intended.
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-08 16:56:52 +04:00
|
|
|
} else {
|
2014-05-11 17:17:27 +04:00
|
|
|
$intName = $ldapName;
|
2013-05-08 16:56:52 +04:00
|
|
|
}
|
2012-04-25 22:34:58 +04:00
|
|
|
|
2012-12-06 21:11:14 +04:00
|
|
|
//a new user/group! Add it only if it doesn't conflict with other backend's users or existing groups
|
2013-02-11 00:52:57 +04:00
|
|
|
//disabling Cache is required to avoid that the new user is cached as not-existing in fooExists check
|
2014-11-28 14:08:33 +03: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-11 00:52:57 +04:00
|
|
|
$originalTTL = $this->connection->ldapCacheTTL;
|
2018-04-11 16:33:36 +03:00
|
|
|
$this->connection->setConfiguration(['ldapCacheTTL' => 0]);
|
2018-11-27 19:06:48 +03:00
|
|
|
if( $intName !== ''
|
|
|
|
&& (($isUser && !$this->ncUserManager->userExists($intName))
|
|
|
|
|| (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$this->connection->setConfiguration(['ldapCacheTTL' => $originalTTL]);
|
|
|
|
$newlyMapped = $this->mapAndAnnounceIfApplicable($mapper, $fdn, $intName, $uuid, $isUser);
|
|
|
|
if($newlyMapped) {
|
2014-05-11 17:17:27 +04:00
|
|
|
return $intName;
|
2012-08-29 03:23:36 +04:00
|
|
|
}
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
|
|
|
|
2018-11-27 19:06:48 +03:00
|
|
|
$this->connection->setConfiguration(['ldapCacheTTL' => $originalTTL]);
|
2014-05-11 17:17:27 +04:00
|
|
|
$altName = $this->createAltInternalOwnCloudName($intName, $isUser);
|
2018-11-27 19:06:48 +03:00
|
|
|
if (is_string($altName)) {
|
|
|
|
if($this->mapAndAnnounceIfApplicable($mapper, $fdn, $altName, $uuid, $isUser)) {
|
|
|
|
$newlyMapped = true;
|
|
|
|
return $altName;
|
2018-03-19 17:53:30 +03:00
|
|
|
}
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
|
|
|
|
2012-07-30 19:42:33 +04:00
|
|
|
//if everything else did not help..
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'Could not create unique name for '.$fdn.'.', ILogger::INFO);
|
2012-08-29 03:23:36 +04:00
|
|
|
return false;
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
|
|
|
|
2019-06-14 18:20:42 +03:00
|
|
|
public function mapAndAnnounceIfApplicable(
|
2018-11-27 19:06:48 +03:00
|
|
|
AbstractMapping $mapper,
|
|
|
|
string $fdn,
|
|
|
|
string $name,
|
|
|
|
string $uuid,
|
|
|
|
bool $isUser
|
|
|
|
) :bool {
|
|
|
|
if($mapper->map($fdn, $name, $uuid)) {
|
|
|
|
if ($this->ncUserManager instanceof PublicEmitter && $isUser) {
|
|
|
|
$this->cacheUserExists($name);
|
|
|
|
$this->ncUserManager->emit('\OC\User', 'assignedUserId', [$name]);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-25 22:34:58 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* gives back the user names as they are used ownClod internally
|
2019-05-27 08:54:09 +03:00
|
|
|
*
|
2015-10-08 21:32:15 +03:00
|
|
|
* @param array $ldapUsers as returned by fetchList()
|
2017-04-12 07:16:27 +03:00
|
|
|
* @return array an array with the user names to use in Nextcloud
|
2012-04-25 22:34:58 +04:00
|
|
|
*
|
|
|
|
* gives back the user names as they are used ownClod internally
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws \Exception
|
2012-04-25 22:34:58 +04:00
|
|
|
*/
|
2017-04-12 07:21:04 +03:00
|
|
|
public function nextcloudUserNames($ldapUsers) {
|
|
|
|
return $this->ldap2NextcloudNames($ldapUsers, true);
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* gives back the group names as they are used ownClod internally
|
2019-05-27 08:54:09 +03:00
|
|
|
*
|
2015-10-08 21:32:15 +03:00
|
|
|
* @param array $ldapGroups as returned by fetchList()
|
2017-04-12 07:16:27 +03:00
|
|
|
* @return array an array with the group names to use in Nextcloud
|
2012-04-25 22:34:58 +04:00
|
|
|
*
|
|
|
|
* gives back the group names as they are used ownClod internally
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws \Exception
|
2012-04-25 22:34:58 +04:00
|
|
|
*/
|
2017-04-12 07:21:04 +03:00
|
|
|
public function nextcloudGroupNames($ldapGroups) {
|
|
|
|
return $this->ldap2NextcloudNames($ldapGroups, false);
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
2015-10-08 21:32:15 +03:00
|
|
|
* @param array $ldapObjects as returned by fetchList()
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param bool $isUsers
|
|
|
|
* @return array
|
2018-05-29 02:10:03 +03:00
|
|
|
* @throws \Exception
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2017-04-12 07:21:04 +03:00
|
|
|
private function ldap2NextcloudNames($ldapObjects, $isUsers) {
|
2012-04-25 22:34:58 +04:00
|
|
|
if($isUsers) {
|
2012-07-25 14:37:39 +04:00
|
|
|
$nameAttribute = $this->connection->ldapUserDisplayName;
|
2016-02-03 15:16:55 +03:00
|
|
|
$sndAttribute = $this->connection->ldapUserDisplayName2;
|
2012-04-25 22:34:58 +04:00
|
|
|
} else {
|
2012-07-25 14:37:39 +04:00
|
|
|
$nameAttribute = $this->connection->ldapGroupDisplayName;
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
2018-05-29 02:10:03 +03:00
|
|
|
$nextcloudNames = [];
|
2012-04-25 22:34:58 +04:00
|
|
|
|
|
|
|
foreach($ldapObjects as $ldapObject) {
|
2015-10-08 21:32:15 +03: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 07:21:04 +03:00
|
|
|
$ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers);
|
|
|
|
if($ncName) {
|
|
|
|
$nextcloudNames[] = $ncName;
|
2014-04-16 19:16:51 +04:00
|
|
|
if($isUsers) {
|
2018-05-29 02:10:03 +03:00
|
|
|
$this->updateUserState($ncName);
|
2014-04-16 19:16:51 +04:00
|
|
|
//cache the user names so it does not need to be retrieved
|
|
|
|
//again later (e.g. sharing dialogue).
|
2016-02-03 15:16:55 +03:00
|
|
|
if(is_null($nameByLDAP)) {
|
|
|
|
continue;
|
2015-10-08 21:32:15 +03:00
|
|
|
}
|
2016-02-23 17:12:01 +03:00
|
|
|
$sndName = isset($ldapObject[$sndAttribute][0])
|
|
|
|
? $ldapObject[$sndAttribute][0] : '';
|
2017-04-12 07:21:04 +03:00
|
|
|
$this->cacheUserDisplayName($ncName, $nameByLDAP, $sndName);
|
2019-05-23 17:58:58 +03:00
|
|
|
} else if($nameByLDAP !== null) {
|
|
|
|
$this->cacheGroupDisplayName($ncName, $nameByLDAP);
|
2014-04-16 19:16:51 +04:00
|
|
|
}
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
|
|
|
}
|
2017-04-12 07:21:04 +03:00
|
|
|
return $nextcloudNames;
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
|
|
|
|
2018-05-29 02:10:03 +03: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-21 01:55:42 +03:00
|
|
|
/**
|
|
|
|
* caches the user display name
|
2017-04-12 07:16:27 +03:00
|
|
|
* @param string $ocName the internal Nextcloud username
|
2015-08-21 01:55:42 +03: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 14:08:33 +03:00
|
|
|
/**
|
|
|
|
* caches a user as existing
|
2017-04-12 07:16:27 +03:00
|
|
|
* @param string $ocName the internal Nextcloud username
|
2014-11-28 14:08:33 +03:00
|
|
|
*/
|
|
|
|
public function cacheUserExists($ocName) {
|
|
|
|
$this->connection->writeToCache('userExists'.$ocName, true);
|
|
|
|
}
|
|
|
|
|
2014-04-16 19:16:51 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* caches the user display name
|
2019-05-27 08:54:09 +03:00
|
|
|
*
|
2017-04-12 07:16:27 +03:00
|
|
|
* @param string $ocName the internal Nextcloud username
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param string $displayName the display name
|
2016-02-03 15:16:55 +03:00
|
|
|
* @param string $displayName2 the second display name
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws \Exception
|
2014-04-16 19:16:51 +04:00
|
|
|
*/
|
2016-02-03 15:16:55 +03:00
|
|
|
public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') {
|
|
|
|
$user = $this->userManager->get($ocName);
|
2017-01-25 19:10:51 +03:00
|
|
|
if($user === null) {
|
|
|
|
return;
|
|
|
|
}
|
2016-02-03 15:16:55 +03:00
|
|
|
$displayName = $user->composeAndStoreDisplayName($displayName, $displayName2);
|
2014-04-16 14:56:08 +04:00
|
|
|
$cacheKeyTrunk = 'getDisplayName';
|
2014-05-11 17:17:27 +04:00
|
|
|
$this->connection->writeToCache($cacheKeyTrunk.$ocName, $displayName);
|
2014-04-16 14:56:08 +04:00
|
|
|
}
|
|
|
|
|
2019-05-23 17:58:58 +03:00
|
|
|
public function cacheGroupDisplayName(string $ncName, string $displayName): void {
|
|
|
|
$cacheKey = 'group_getDisplayName' . $ncName;
|
|
|
|
$this->connection->writeToCache($cacheKey, $displayName);
|
|
|
|
}
|
|
|
|
|
2012-04-25 22:34:58 +04:00
|
|
|
/**
|
2017-04-12 07:16:27 +03:00
|
|
|
* creates a unique name for internal Nextcloud use for users. Don't call it directly.
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param string $name the display name of the object
|
2017-04-12 07:16:27 +03:00
|
|
|
* @return string|false with with the name to use in Nextcloud or false if unsuccessful
|
2013-02-11 00:52:57 +04: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 16:14:43 +04:00
|
|
|
$altName = $name . '_' . rand(1000,9999);
|
2018-03-19 17:53:30 +03:00
|
|
|
if(!$this->ncUserManager->userExists($altName)) {
|
2013-02-11 00:52:57 +04:00
|
|
|
return $altName;
|
|
|
|
}
|
|
|
|
$attempts++;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-12 07:16:27 +03:00
|
|
|
* creates a unique name for internal Nextcloud use for groups. Don't call it directly.
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param string $name the display name of the object
|
2017-04-12 07:16:27 +03:00
|
|
|
* @return string|false with with the name to use in Nextcloud or false if unsuccessful.
|
2013-02-11 00:52:57 +04:00
|
|
|
*
|
|
|
|
* Instead of using this method directly, call
|
|
|
|
* createAltInternalOwnCloudName($name, false)
|
2012-04-25 22:34:58 +04:00
|
|
|
*
|
2013-02-11 00:52:57 +04: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 15:20:45 +03:00
|
|
|
$usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%');
|
2018-01-27 01:46:40 +03:00
|
|
|
if(!$usedNames || count($usedNames) === 0) {
|
2013-02-11 00:52:57 +04:00
|
|
|
$lastNo = 1; //will become name_2
|
|
|
|
} else {
|
|
|
|
natsort($usedNames);
|
2014-05-11 17:17:27 +04:00
|
|
|
$lastName = array_pop($usedNames);
|
2018-01-26 17:01:27 +03:00
|
|
|
$lastNo = (int)substr($lastName, strrpos($lastName, '_') + 1);
|
2013-02-11 00:52:57 +04:00
|
|
|
}
|
2018-01-26 17:01:27 +03:00
|
|
|
$altName = $name.'_'. (string)($lastNo+1);
|
2013-02-11 00:52:57 +04:00
|
|
|
unset($usedNames);
|
|
|
|
|
|
|
|
$attempts = 1;
|
|
|
|
while($attempts < 21){
|
2014-05-11 17:17:27 +04: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 10:24:27 +03:00
|
|
|
if(!\OC::$server->getGroupManager()->groupExists($altName)) {
|
2013-02-11 00:52:57 +04:00
|
|
|
return $altName;
|
|
|
|
}
|
2014-07-02 00:02:41 +04:00
|
|
|
$altName = $name . '_' . ($lastNo + $attempts);
|
2013-02-11 00:52:57 +04:00
|
|
|
$attempts++;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-12 07:16:27 +03:00
|
|
|
* creates a unique name for internal Nextcloud use.
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param string $name the display name of the object
|
2014-02-06 19:30:58 +04:00
|
|
|
* @param boolean $isUser whether name should be created for a user (true) or a group (false)
|
2017-04-12 07:16:27 +03:00
|
|
|
* @return string|false with with the name to use in Nextcloud or false if unsuccessful
|
2012-04-25 22:34:58 +04:00
|
|
|
*/
|
2013-02-11 00:52:57 +04: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 22:34:58 +04: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 13:56:03 +03: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 15:55:04 +03:00
|
|
|
$filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter);
|
2018-02-13 23:55:24 +03: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 13:56:03 +03:00
|
|
|
}
|
|
|
|
|
2015-10-30 20:20:50 +03:00
|
|
|
/**
|
|
|
|
* counts the number of users according to a provided loginName and
|
|
|
|
* utilizing the login filter.
|
|
|
|
*
|
|
|
|
* @param string $loginName
|
2017-10-31 21:42:17 +03:00
|
|
|
* @return int
|
2015-10-30 20:20:50 +03:00
|
|
|
*/
|
|
|
|
public function countUsersByLoginName($loginName) {
|
|
|
|
$loginName = $this->escapeFilterPart($loginName);
|
|
|
|
$filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter);
|
2018-02-13 23:55:24 +03:00
|
|
|
return $this->countUsers($filter);
|
2015-10-30 20:20:50 +03:00
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
2014-05-13 15:29:25 +04:00
|
|
|
* @param string $filter
|
2014-05-15 16:12:17 +04:00
|
|
|
* @param string|string[] $attr
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
2017-10-31 21:42:17 +03:00
|
|
|
* @param bool $forceApplyAttributes
|
2014-05-11 17:17:27 +04:00
|
|
|
* @return array
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws \Exception
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2017-10-31 21:42:17 +03:00
|
|
|
public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null, $forceApplyAttributes = false) {
|
2015-08-21 01:55:42 +03:00
|
|
|
$ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset);
|
2017-10-31 21:42:17 +03:00
|
|
|
$recordsToUpdate = $ldapRecords;
|
|
|
|
if(!$forceApplyAttributes) {
|
2017-11-08 03:51:14 +03:00
|
|
|
$isBackgroundJobModeAjax = $this->config
|
2017-10-31 21:42:17 +03: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-08 00:02:54 +03:00
|
|
|
if(is_string($uid)) {
|
|
|
|
$this->cacheUserExists($uid);
|
|
|
|
}
|
2017-10-31 21:42:17 +03:00
|
|
|
return ($uid !== false) && ($newlyMapped || $isBackgroundJobModeAjax);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
$this->batchApplyUserAttributes($recordsToUpdate);
|
2018-11-25 00:06:45 +03:00
|
|
|
return $this->fetchList($ldapRecords, $this->manyAttributes($attr));
|
2015-08-21 01:55:42 +03: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-29 02:10:03 +03:00
|
|
|
*
|
2015-08-21 01:55:42 +03:00
|
|
|
* @param array $ldapRecords
|
2018-05-29 02:10:03 +03:00
|
|
|
* @throws \Exception
|
2015-08-21 01:55:42 +03:00
|
|
|
*/
|
|
|
|
public function batchApplyUserAttributes(array $ldapRecords){
|
2015-11-28 01:46:26 +03:00
|
|
|
$displayNameAttribute = strtolower($this->connection->ldapUserDisplayName);
|
2015-08-21 01:55:42 +03:00
|
|
|
foreach($ldapRecords as $userRecord) {
|
2015-11-28 01:46:26 +03:00
|
|
|
if(!isset($userRecord[$displayNameAttribute])) {
|
|
|
|
// displayName is obligatory
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-31 21:42:17 +03:00
|
|
|
$ocName = $this->dn2ocname($userRecord['dn'][0], null, true);
|
|
|
|
if($ocName === false) {
|
2015-11-28 01:46:26 +03:00
|
|
|
continue;
|
|
|
|
}
|
2018-05-29 02:10:03 +03:00
|
|
|
$this->updateUserState($ocName);
|
2015-08-21 01:55:42 +03:00
|
|
|
$user = $this->userManager->get($ocName);
|
2016-06-21 17:30:48 +03: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-21 01:55:42 +03:00
|
|
|
}
|
2012-05-04 01:38:55 +04:00
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
2014-05-13 15:29:25 +04:00
|
|
|
* @param string $filter
|
2014-05-15 16:12:17 +04:00
|
|
|
* @param string|string[] $attr
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @return array
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2012-10-26 23:53:56 +04:00
|
|
|
public function fetchListOfGroups($filter, $attr, $limit = null, $offset = null) {
|
2018-11-25 00:06:45 +03:00
|
|
|
return $this->fetchList($this->searchGroups($filter, $attr, $limit, $offset), $this->manyAttributes($attr));
|
2012-05-04 01:38:55 +04:00
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
2014-05-13 15:29:25 +04:00
|
|
|
* @param array $list
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param bool $manyAttributes
|
|
|
|
* @return array
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2012-07-25 14:37:39 +04:00
|
|
|
private function fetchList($list, $manyAttributes) {
|
2012-05-04 01:38:55 +04:00
|
|
|
if(is_array($list)) {
|
|
|
|
if($manyAttributes) {
|
|
|
|
return $list;
|
|
|
|
} else {
|
2015-10-07 19:57:49 +03:00
|
|
|
$list = array_reduce($list, function($carry, $item) {
|
2015-10-09 21:07:35 +03:00
|
|
|
$attribute = array_keys($item)[0];
|
|
|
|
$carry[] = $item[$attribute][0];
|
|
|
|
return $carry;
|
2015-10-07 19:57:49 +03:00
|
|
|
}, array());
|
2012-05-04 01:38:55 +04:00
|
|
|
return array_unique($list, SORT_LOCALE_STRING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//error cause actually, maybe throw an exception in future.
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2012-04-23 15:04:58 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* executes an LDAP search, optimized for Users
|
2019-05-27 08:54:09 +03:00
|
|
|
*
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param string $filter the LDAP filter for the search
|
2014-05-15 16:12:17 +04:00
|
|
|
* @param string|string[] $attr optional, when a certain attribute shall be filtered out
|
2014-02-19 12:31:54 +04:00
|
|
|
* @param integer $limit
|
|
|
|
* @param integer $offset
|
2014-05-11 17:17:27 +04:00
|
|
|
* @return array with the search result
|
2012-04-23 15:04:58 +04:00
|
|
|
*
|
|
|
|
* Executes an LDAP search
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws ServerNotAvailableException
|
2012-04-23 15:04:58 +04:00
|
|
|
*/
|
2012-10-26 23:53:56 +04:00
|
|
|
public function searchUsers($filter, $attr = null, $limit = null, $offset = null) {
|
2019-01-28 18:16:52 +03:00
|
|
|
$result = [];
|
|
|
|
foreach($this->connection->ldapBaseUsers as $base) {
|
|
|
|
$result = array_merge($result, $this->search($filter, [$base], $attr, $limit, $offset));
|
|
|
|
}
|
|
|
|
return $result;
|
2012-04-23 15:04:58 +04:00
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $filter
|
2014-05-15 16:12:17 +04:00
|
|
|
* @param string|string[] $attr
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @return false|int
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws ServerNotAvailableException
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2014-01-08 15:07:57 +04:00
|
|
|
public function countUsers($filter, $attr = array('dn'), $limit = null, $offset = null) {
|
2019-01-28 18:16:52 +03:00
|
|
|
$result = false;
|
|
|
|
foreach($this->connection->ldapBaseUsers as $base) {
|
|
|
|
$count = $this->count($filter, [$base], $attr, $limit, $offset);
|
|
|
|
$result = is_int($count) ? (int)$result + $count : $result;
|
|
|
|
}
|
|
|
|
return $result;
|
2014-01-08 15:07:57 +04:00
|
|
|
}
|
|
|
|
|
2012-04-23 15:04:58 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* executes an LDAP search, optimized for Groups
|
2019-05-27 08:54:09 +03:00
|
|
|
*
|
2014-02-19 12:31:54 +04:00
|
|
|
* @param string $filter the LDAP filter for the search
|
2014-05-15 16:12:17 +04:00
|
|
|
* @param string|string[] $attr optional, when a certain attribute shall be filtered out
|
2014-02-19 12:31:54 +04:00
|
|
|
* @param integer $limit
|
|
|
|
* @param integer $offset
|
2014-05-11 17:17:27 +04:00
|
|
|
* @return array with the search result
|
2012-04-23 15:04:58 +04:00
|
|
|
*
|
|
|
|
* Executes an LDAP search
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws ServerNotAvailableException
|
2012-04-23 15:04:58 +04:00
|
|
|
*/
|
2012-10-26 23:53:56 +04:00
|
|
|
public function searchGroups($filter, $attr = null, $limit = null, $offset = null) {
|
2019-01-28 18:16:52 +03:00
|
|
|
$result = [];
|
|
|
|
foreach($this->connection->ldapBaseGroups as $base) {
|
|
|
|
$result = array_merge($result, $this->search($filter, [$base], $attr, $limit, $offset));
|
|
|
|
}
|
|
|
|
return $result;
|
2012-04-23 15:04:58 +04:00
|
|
|
}
|
|
|
|
|
2012-04-14 20:26:46 +04:00
|
|
|
/**
|
2014-06-11 15:35:35 +04:00
|
|
|
* returns the number of available groups
|
2019-05-27 08:54:09 +03:00
|
|
|
*
|
2014-06-11 15:35:35 +04:00
|
|
|
* @param string $filter the LDAP search filter
|
|
|
|
* @param string[] $attr optional
|
|
|
|
* @param int|null $limit
|
|
|
|
* @param int|null $offset
|
|
|
|
* @return int|bool
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws ServerNotAvailableException
|
2014-06-11 15:35:35 +04:00
|
|
|
*/
|
|
|
|
public function countGroups($filter, $attr = array('dn'), $limit = null, $offset = null) {
|
2019-01-28 18:16:52 +03:00
|
|
|
$result = false;
|
|
|
|
foreach($this->connection->ldapBaseGroups as $base) {
|
|
|
|
$count = $this->count($filter, [$base], $attr, $limit, $offset);
|
|
|
|
$result = is_int($count) ? (int)$result + $count : $result;
|
|
|
|
}
|
|
|
|
return $result;
|
2014-06-11 15:35:35 +04: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 13:56:03 +03:00
|
|
|
/**
|
|
|
|
* returns the number of available objects on the base DN
|
|
|
|
*
|
|
|
|
* @param int|null $limit
|
|
|
|
* @param int|null $offset
|
|
|
|
* @return int|bool
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws ServerNotAvailableException
|
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 13:56:03 +03:00
|
|
|
*/
|
|
|
|
public function countObjects($limit = null, $offset = null) {
|
2019-01-28 18:16:52 +03:00
|
|
|
$result = false;
|
|
|
|
foreach($this->connection->ldapBase as $base) {
|
|
|
|
$count = $this->count('objectclass=*', [$base], ['dn'], $limit, $offset);
|
|
|
|
$result = is_int($count) ? (int)$result + $count : $result;
|
|
|
|
}
|
|
|
|
return $result;
|
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 13:56:03 +03:00
|
|
|
}
|
|
|
|
|
2017-05-21 03:24:38 +03:00
|
|
|
/**
|
|
|
|
* Returns the LDAP handler
|
|
|
|
* @throws \OC\ServerNotAvailableException
|
|
|
|
*/
|
|
|
|
|
2017-05-20 10:53:46 +03:00
|
|
|
/**
|
|
|
|
* @return mixed
|
2017-05-21 03:24:38 +03:00
|
|
|
* @throws \OC\ServerNotAvailableException
|
2017-05-20 10:53:46 +03:00
|
|
|
*/
|
|
|
|
private function invokeLDAPMethod() {
|
|
|
|
$arguments = func_get_args();
|
|
|
|
$command = array_shift($arguments);
|
2017-05-21 03:24:38 +03:00
|
|
|
$cr = array_shift($arguments);
|
2017-05-20 10:53:46 +03: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 14:44:43 +03:00
|
|
|
// thus cannot support controlPagedResultResponse as the third argument
|
|
|
|
// is a reference
|
2017-05-20 10:53:46 +03:00
|
|
|
$doMethod = function () use ($command, &$arguments) {
|
|
|
|
if ($command == 'controlPagedResultResponse') {
|
2017-05-26 14:44:43 +03:00
|
|
|
throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.');
|
2017-05-20 10:53:46 +03: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 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('user_ldap', "Connection lost on $command, attempting to reestablish.", ILogger::DEBUG);
|
2017-05-25 01:45:48 +03:00
|
|
|
$this->connection->resetConnectionResource();
|
2017-05-20 10:53:46 +03:00
|
|
|
$cr = $this->connection->getConnectionResource();
|
2017-05-25 01:45:48 +03:00
|
|
|
|
2017-05-20 10:53:46 +03:00
|
|
|
if(!$this->ldap->isResource($cr)) {
|
|
|
|
// Seems like we didn't find any resource.
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", ILogger::DEBUG);
|
2017-05-25 01:45:48 +03:00
|
|
|
throw $e;
|
2017-05-20 10:53:46 +03:00
|
|
|
}
|
2017-05-25 01:45:48 +03:00
|
|
|
|
|
|
|
$arguments[0] = array_pad([], count($arguments[0]), $cr);
|
2017-05-20 10:53:46 +03:00
|
|
|
$ret = $doMethod();
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2014-06-11 15:35:35 +04:00
|
|
|
/**
|
2013-02-27 01:27:09 +04:00
|
|
|
* retrieved. Results will according to the order in the array.
|
2017-12-07 19:49:33 +03:00
|
|
|
*
|
|
|
|
* @param $filter
|
|
|
|
* @param $base
|
2017-12-08 00:02:54 +03:00
|
|
|
* @param string[]|string|null $attr
|
2014-05-11 17:17:27 +04: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 15:07:57 +04:00
|
|
|
* second | false if not successful
|
2017-12-07 19:49:33 +03:00
|
|
|
* @throws ServerNotAvailableException
|
2012-04-14 20:26:46 +04:00
|
|
|
*/
|
2014-01-08 15:07:57 +04:00
|
|
|
private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) {
|
2012-04-25 22:34:58 +04:00
|
|
|
if(!is_null($attr) && !is_array($attr)) {
|
2012-07-02 22:31:07 +04:00
|
|
|
$attr = array(mb_strtolower($attr, 'UTF-8'));
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
2012-05-28 17:54:38 +04:00
|
|
|
|
2012-10-26 23:53:56 +04:00
|
|
|
// See if we have a resource, in case not cancel with message
|
2014-01-08 15:07:57 +04:00
|
|
|
$cr = $this->connection->getConnectionResource();
|
|
|
|
if(!$this->ldap->isResource($cr)) {
|
2012-05-28 17:54:38 +04:00
|
|
|
// Seems like we didn't find any resource.
|
|
|
|
// Return an empty array just like before.
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', ILogger::DEBUG);
|
2014-01-08 15:07:57 +04:00
|
|
|
return false;
|
2012-05-01 14:17:33 +04:00
|
|
|
}
|
2012-04-14 20:05:29 +04:00
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
//check whether paged search should be attempted
|
2018-01-26 17:01:27 +03:00
|
|
|
$pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, (int)$limit, $offset);
|
2012-10-26 23:53:56 +04:00
|
|
|
|
2014-01-08 15:07:57 +04:00
|
|
|
$linkResources = array_pad(array(), count($base), $cr);
|
2017-05-21 03:24:38 +03:00
|
|
|
$sr = $this->invokeLDAPMethod('search', $linkResources, $base, $filter, $attr);
|
2017-05-25 01:45:48 +03:00
|
|
|
// cannot use $cr anymore, might have changed in the previous call!
|
|
|
|
$error = $this->ldap->errno($this->connection->getConnectionResource());
|
2013-04-21 00:45:50 +04:00
|
|
|
if(!is_array($sr) || $error !== 0) {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'Attempt for Paging? '.print_r($pagedSearchOK, true), ILogger::ERROR);
|
2014-01-08 15:07:57 +04:00
|
|
|
return false;
|
2012-10-27 19:35:39 +04:00
|
|
|
}
|
2013-02-27 01:27:09 +04:00
|
|
|
|
2014-01-08 15:07:57 +04:00
|
|
|
return array($sr, $pagedSearchOK);
|
|
|
|
}
|
2013-02-27 01:27:09 +04:00
|
|
|
|
2014-01-08 15:07:57 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* processes an LDAP paged search operation
|
2019-05-27 08:54:09 +03:00
|
|
|
*
|
2014-05-11 17:17:27 +04: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 16:30:44 +03:00
|
|
|
* @param int $iFoundItems number of results in the single search operation
|
2014-05-11 17:17:27 +04: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 15:07:57 +04:00
|
|
|
* prior results need to be gained
|
2015-07-16 23:17:57 +03:00
|
|
|
* @return bool cookie validity, true if we have more pages, false otherwise.
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws ServerNotAvailableException
|
2014-01-08 15:07:57 +04:00
|
|
|
*/
|
|
|
|
private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) {
|
2015-07-16 23:17:57 +03:00
|
|
|
$cookie = null;
|
2012-10-26 23:53:56 +04:00
|
|
|
if($pagedSearchOK) {
|
2014-01-08 15:07:57 +04:00
|
|
|
$cr = $this->connection->getConnectionResource();
|
2013-01-05 02:54:42 +04:00
|
|
|
foreach($sr as $key => $res) {
|
2017-05-26 14:44:43 +03:00
|
|
|
if($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) {
|
2013-01-05 02:54:42 +04:00
|
|
|
$this->setPagedResultCookie($base[$key], $filter, $limit, $offset, $cookie);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-26 23:53:56 +04:00
|
|
|
//browsing through prior pages to get the cookie for the new one
|
|
|
|
if($skipHandling) {
|
2016-08-17 10:46:54 +03:00
|
|
|
return false;
|
2012-10-26 23:53:56 +04:00
|
|
|
}
|
2013-02-15 01:16:48 +04: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 15:07:57 +04:00
|
|
|
if($iFoundItems <= $limit) {
|
2012-10-26 23:53:56 +04:00
|
|
|
$this->pagedSearchedSuccessful = true;
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-26 17:01:27 +03:00
|
|
|
if(!is_null($limit) && (int)$this->connection->ldapPagingSize !== 0) {
|
2017-12-21 16:29:56 +03:00
|
|
|
\OC::$server->getLogger()->debug(
|
|
|
|
'Paged search was not available',
|
|
|
|
[ 'app' => 'user_ldap' ]
|
|
|
|
);
|
2013-01-05 02:54:42 +04:00
|
|
|
}
|
2012-10-26 23:53:56 +04:00
|
|
|
}
|
2015-07-16 23:17:57 +03: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 15:07:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* executes an LDAP search, but counts the results only
|
2017-12-08 00:02:54 +03:00
|
|
|
*
|
2014-02-19 12:31:54 +04:00
|
|
|
* @param string $filter the LDAP filter for the search
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param array $base an array containing the LDAP subtree(s) that shall be searched
|
2014-05-15 16:12:17 +04:00
|
|
|
* @param string|string[] $attr optional, array, one or more attributes that shall be
|
2014-01-08 15:07:57 +04:00
|
|
|
* retrieved. Results will according to the order in the array.
|
2014-05-11 17:17:27 +04: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 15:07:57 +04:00
|
|
|
* completed
|
2014-05-11 18:27:18 +04:00
|
|
|
* @return int|false Integer or false if the search could not be initialized
|
2017-12-08 00:02:54 +03:00
|
|
|
* @throws ServerNotAvailableException
|
2014-01-08 15:07:57 +04:00
|
|
|
*/
|
|
|
|
private function count($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'Count filter: '.print_r($filter, true), ILogger::DEBUG);
|
2014-03-20 03:21:51 +04:00
|
|
|
|
2018-01-26 17:01:27 +03:00
|
|
|
$limitPerPage = (int)$this->connection->ldapPagingSize;
|
2014-11-21 16:51:20 +03:00
|
|
|
if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) {
|
|
|
|
$limitPerPage = $limit;
|
2014-01-08 15:07:57 +04:00
|
|
|
}
|
2014-03-20 03:21:51 +04:00
|
|
|
|
2014-01-08 15:07:57 +04:00
|
|
|
$counter = 0;
|
2014-03-20 12:14:41 +04:00
|
|
|
$count = null;
|
2014-05-11 17:17:27 +04:00
|
|
|
$this->connection->getConnectionResource();
|
2014-03-20 03:21:51 +04:00
|
|
|
|
|
|
|
do {
|
2017-12-08 00:02:54 +03:00
|
|
|
$search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset);
|
2014-03-20 03:21:51 +04:00
|
|
|
if($search === false) {
|
|
|
|
return $counter > 0 ? $counter : false;
|
|
|
|
}
|
|
|
|
list($sr, $pagedSearchOK) = $search;
|
|
|
|
|
2015-07-16 23:17:57 +03: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 14:38:05 +04:00
|
|
|
$counter += $count;
|
2014-01-08 15:07:57 +04:00
|
|
|
|
2015-07-16 23:17:57 +03:00
|
|
|
$hasMorePages = $this->processPagedSearchStatus($sr, $filter, $base, $count, $limitPerPage,
|
2014-01-08 15:07:57 +04:00
|
|
|
$offset, $pagedSearchOK, $skipHandling);
|
2014-10-29 12:24:48 +03:00
|
|
|
$offset += $limitPerPage;
|
2015-07-16 23:17:57 +03:00
|
|
|
/* ++ Fixing RHDS searches with pages with zero results ++
|
|
|
|
* Continue now depends on $hasMorePages value
|
|
|
|
*/
|
|
|
|
$continue = $pagedSearchOK && $hasMorePages;
|
2014-10-29 12:24:48 +03:00
|
|
|
} while($continue && (is_null($limit) || $limit <= 0 || $limit > $counter));
|
2014-01-08 15:07:57 +04:00
|
|
|
|
|
|
|
return $counter;
|
|
|
|
}
|
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
/**
|
2014-05-13 15:29:25 +04:00
|
|
|
* @param array $searchResults
|
2014-05-11 17:17:27 +04:00
|
|
|
* @return int
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws ServerNotAvailableException
|
2014-05-11 17:17:27 +04:00
|
|
|
*/
|
2015-07-16 23:17:57 +03:00
|
|
|
private function countEntriesInSearchResults($searchResults) {
|
2014-05-11 17:17:27 +04:00
|
|
|
$counter = 0;
|
2014-03-20 14:38:05 +04:00
|
|
|
|
|
|
|
foreach($searchResults as $res) {
|
2018-01-26 17:01:27 +03:00
|
|
|
$count = (int)$this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res);
|
2014-03-20 14:38:05 +04:00
|
|
|
$counter += $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $counter;
|
|
|
|
}
|
|
|
|
|
2014-01-08 15:07:57 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Executes an LDAP search
|
2017-12-07 19:49:33 +03:00
|
|
|
*
|
2014-05-11 17:17:27 +04: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 16:12:17 +04:00
|
|
|
* @param string|string[] $attr optional, array, one or more attributes that shall be
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @param bool $skipHandling
|
|
|
|
* @return array with the search result
|
2017-12-07 19:49:33 +03:00
|
|
|
* @throws ServerNotAvailableException
|
2014-01-08 15:07:57 +04:00
|
|
|
*/
|
2017-04-06 11:26:41 +03:00
|
|
|
public function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) {
|
2018-01-26 17:01:27 +03:00
|
|
|
$limitPerPage = (int)$this->connection->ldapPagingSize;
|
2017-09-11 13:10:32 +03:00
|
|
|
if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) {
|
|
|
|
$limitPerPage = $limit;
|
2014-07-02 00:02:41 +04:00
|
|
|
}
|
2014-01-08 15:07:57 +04:00
|
|
|
|
2015-07-16 23:17:57 +03: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 19:49:33 +03:00
|
|
|
$findings = [];
|
2015-07-16 23:17:57 +03:00
|
|
|
$savedoffset = $offset;
|
|
|
|
do {
|
2017-09-11 13:10:32 +03:00
|
|
|
$search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset);
|
2015-07-16 23:17:57 +03:00
|
|
|
if($search === false) {
|
2017-12-07 19:49:33 +03:00
|
|
|
return [];
|
2015-07-16 23:17:57 +03:00
|
|
|
}
|
|
|
|
list($sr, $pagedSearchOK) = $search;
|
|
|
|
$cr = $this->connection->getConnectionResource();
|
2014-01-08 15:07:57 +04:00
|
|
|
|
2015-07-16 23:17:57 +03: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 13:10:32 +03:00
|
|
|
$this->processPagedSearchStatus($sr, $filter, $base, 1, $limitPerPage,
|
2015-07-16 23:17:57 +03:00
|
|
|
$offset, $pagedSearchOK,
|
|
|
|
$skipHandling);
|
|
|
|
return array();
|
2014-01-08 15:07:57 +04:00
|
|
|
}
|
|
|
|
|
2017-09-11 16:30:44 +03:00
|
|
|
$iFoundItems = 0;
|
2015-07-16 23:17:57 +03:00
|
|
|
foreach($sr as $res) {
|
2017-05-21 03:24:38 +03:00
|
|
|
$findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $res));
|
2017-09-11 16:30:44 +03:00
|
|
|
$iFoundItems = max($iFoundItems, $findings['count']);
|
|
|
|
unset($findings['count']);
|
2015-07-16 23:17:57 +03:00
|
|
|
}
|
2014-01-08 15:07:57 +04:00
|
|
|
|
2017-09-11 16:30:44 +03:00
|
|
|
$continue = $this->processPagedSearchStatus($sr, $filter, $base, $iFoundItems,
|
2017-09-11 13:10:32 +03:00
|
|
|
$limitPerPage, $offset, $pagedSearchOK,
|
2014-01-08 15:07:57 +04:00
|
|
|
$skipHandling);
|
2017-09-11 13:10:32 +03:00
|
|
|
$offset += $limitPerPage;
|
2017-09-11 16:30:44 +03:00
|
|
|
} while ($continue && $pagedSearchOK && ($limit === null || count($findings) < $limit));
|
2015-07-16 23:17:57 +03:00
|
|
|
// reseting offset
|
|
|
|
$offset = $savedoffset;
|
2012-10-26 23:53:56 +04:00
|
|
|
|
|
|
|
// if we're here, probably no connection resource is returned.
|
2017-04-12 07:16:27 +03:00
|
|
|
// to make Nextcloud behave nicely, we simply give back an empty array.
|
2012-10-26 23:53:56 +04:00
|
|
|
if(is_null($findings)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2012-04-14 20:05:29 +04:00
|
|
|
if(!is_null($attr)) {
|
2017-12-07 19:49:33 +03:00
|
|
|
$selection = [];
|
2015-10-07 19:57:49 +03:00
|
|
|
$i = 0;
|
2012-04-14 20:05:29 +04:00
|
|
|
foreach($findings as $item) {
|
2012-05-17 15:15:26 +04:00
|
|
|
if(!is_array($item)) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-25 14:37:39 +04:00
|
|
|
$item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8');
|
2015-10-07 19:57:49 +03:00
|
|
|
foreach($attr as $key) {
|
2012-05-11 17:42:05 +04:00
|
|
|
if(isset($item[$key])) {
|
2015-10-08 21:32:15 +03:00
|
|
|
if(is_array($item[$key]) && isset($item[$key]['count'])) {
|
|
|
|
unset($item[$key]['count']);
|
|
|
|
}
|
2015-10-07 19:57:49 +03:00
|
|
|
if($key !== 'dn') {
|
2017-12-13 13:57:46 +03: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 14:25:10 +04:00
|
|
|
} else {
|
2016-07-22 11:46:29 +03:00
|
|
|
$selection[$i][$key] = [$this->helper->sanitizeDN($item[$key])];
|
2012-05-14 14:25:10 +04:00
|
|
|
}
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
2015-10-07 19:57:49 +03:00
|
|
|
|
2012-04-14 20:05:29 +04:00
|
|
|
}
|
2015-10-07 19:57:49 +03:00
|
|
|
$i++;
|
2012-04-14 20:05:29 +04:00
|
|
|
}
|
2012-10-27 14:18:50 +04:00
|
|
|
$findings = $selection;
|
|
|
|
}
|
2012-10-27 19:36:08 +04:00
|
|
|
//we slice the findings, when
|
2014-05-11 17:17:27 +04:00
|
|
|
//a) paged search unsuccessful, though attempted
|
2012-10-27 19:36:08 +04:00
|
|
|
//b) no paged search, but limit set
|
2014-03-20 03:21:51 +04:00
|
|
|
if((!$this->getPagedSearchResultState()
|
2012-12-15 02:50:21 +04:00
|
|
|
&& $pagedSearchOK)
|
2012-10-27 14:18:50 +04:00
|
|
|
|| (
|
2012-10-27 19:36:08 +04:00
|
|
|
!$pagedSearchOK
|
|
|
|
&& !is_null($limit)
|
2012-10-27 14:18:50 +04:00
|
|
|
)
|
|
|
|
) {
|
2018-01-26 17:01:27 +03:00
|
|
|
$findings = array_slice($findings, (int)$offset, $limit);
|
2012-04-14 20:05:29 +04:00
|
|
|
}
|
|
|
|
return $findings;
|
|
|
|
}
|
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
/**
|
2014-05-13 15:29:25 +04:00
|
|
|
* @param string $name
|
2018-03-02 19:44:06 +03:00
|
|
|
* @return string
|
2018-03-05 15:30:28 +03:00
|
|
|
* @throws \InvalidArgumentException
|
2014-05-11 17:17:27 +04:00
|
|
|
*/
|
2012-07-25 14:37:39 +04:00
|
|
|
public function sanitizeUsername($name) {
|
2018-03-05 16:03:08 +03:00
|
|
|
$name = trim($name);
|
|
|
|
|
2012-07-25 14:37:39 +04:00
|
|
|
if($this->connection->ldapIgnoreNamingRules) {
|
2018-03-05 16:03:08 +03:00
|
|
|
return $name;
|
2012-05-17 21:33:38 +04:00
|
|
|
}
|
|
|
|
|
2018-03-05 16:03:08 +03: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 23:32:32 +04:00
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
// Replacements
|
2015-06-19 15:55:04 +03:00
|
|
|
$name = str_replace(' ', '_', $name);
|
2012-05-17 17:14:47 +04:00
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
// Every remaining disallowed characters will be removed
|
2012-07-02 22:31:07 +04:00
|
|
|
$name = preg_replace('/[^a-zA-Z0-9_.@-]/u', '', $name);
|
2012-05-17 17:14:47 +04:00
|
|
|
|
2018-03-02 19:44:06 +03:00
|
|
|
if($name === '') {
|
|
|
|
throw new \InvalidArgumentException('provided name template for username does not contain any allowed characters');
|
|
|
|
}
|
|
|
|
|
2012-05-17 17:14:47 +04:00
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2014-02-19 16:13:01 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* escapes (user provided) parts for LDAP filter
|
2014-04-14 19:39:29 +04:00
|
|
|
* @param string $input, the provided value
|
2015-01-29 02:15:55 +03:00
|
|
|
* @param bool $allowAsterisk whether in * at the beginning should be preserved
|
2014-05-11 17:17:27 +04:00
|
|
|
* @return string the escaped string
|
2014-02-19 16:13:01 +04:00
|
|
|
*/
|
2014-12-08 19:12:13 +03: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 16:13:01 +04:00
|
|
|
$search = array('*', '\\', '(', ')');
|
|
|
|
$replace = array('\\*', '\\\\', '\\(', '\\)');
|
2014-12-08 19:12:13 +03:00
|
|
|
return $asterisk . str_replace($search, $replace, $input);
|
2014-02-19 16:13:01 +04:00
|
|
|
}
|
|
|
|
|
2012-04-15 15:37:35 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* combines the input filters with AND
|
2014-05-15 16:12:17 +04:00
|
|
|
* @param string[] $filters the filters to connect
|
2014-05-11 17:17:27 +04:00
|
|
|
* @return string the combined filter
|
2012-04-15 15:37:35 +04:00
|
|
|
*/
|
2012-07-25 14:37:39 +04:00
|
|
|
public function combineFilterWithAnd($filters) {
|
2012-07-25 15:18:31 +04:00
|
|
|
return $this->combineFilter($filters, '&');
|
2012-04-15 15:37:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-11 15:35:35 +04:00
|
|
|
* combines the input filters with OR
|
2014-05-15 16:12:17 +04:00
|
|
|
* @param string[] $filters the filters to connect
|
2014-05-11 17:17:27 +04:00
|
|
|
* @return string the combined filter
|
2014-06-11 15:35:35 +04:00
|
|
|
* Combines Filter arguments with OR
|
2012-04-15 15:37:35 +04:00
|
|
|
*/
|
2012-07-25 14:37:39 +04:00
|
|
|
public function combineFilterWithOr($filters) {
|
2012-07-25 15:18:31 +04:00
|
|
|
return $this->combineFilter($filters, '|');
|
2012-04-15 15:37:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* combines the input filters with given operator
|
2014-05-15 16:12:17 +04:00
|
|
|
* @param string[] $filters the filters to connect
|
2014-02-06 19:30:58 +04:00
|
|
|
* @param string $operator either & or |
|
2014-05-11 17:17:27 +04:00
|
|
|
* @return string the combined filter
|
2012-04-15 15:37:35 +04:00
|
|
|
*/
|
2012-07-25 14:37:39 +04:00
|
|
|
private function combineFilter($filters, $operator) {
|
2012-04-15 15:37:35 +04:00
|
|
|
$combinedFilter = '('.$operator;
|
|
|
|
foreach($filters as $filter) {
|
2016-09-23 01:30:57 +03:00
|
|
|
if ($filter !== '' && $filter[0] !== '(') {
|
2012-04-15 15:37:35 +04:00
|
|
|
$filter = '('.$filter.')';
|
2014-05-16 00:47:28 +04:00
|
|
|
}
|
|
|
|
$combinedFilter.=$filter;
|
2012-04-15 15:37:35 +04:00
|
|
|
}
|
|
|
|
$combinedFilter.=')';
|
|
|
|
return $combinedFilter;
|
|
|
|
}
|
|
|
|
|
2013-01-31 04:46:34 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* creates a filter part for to perform search for users
|
2013-01-31 04:46:34 +04:00
|
|
|
* @param string $search the search term
|
|
|
|
* @return string the final filter part to use in LDAP searches
|
|
|
|
*/
|
|
|
|
public function getFilterPartForUserSearch($search) {
|
2013-02-15 01:16:48 +04:00
|
|
|
return $this->getFilterPartForSearch($search,
|
|
|
|
$this->connection->ldapAttributesForUserSearch,
|
|
|
|
$this->connection->ldapUserDisplayName);
|
2013-01-31 04:46:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* creates a filter part for to perform search for groups
|
2013-01-31 04:46:34 +04:00
|
|
|
* @param string $search the search term
|
|
|
|
* @return string the final filter part to use in LDAP searches
|
|
|
|
*/
|
|
|
|
public function getFilterPartForGroupSearch($search) {
|
2013-02-15 01:16:48 +04:00
|
|
|
return $this->getFilterPartForSearch($search,
|
|
|
|
$this->connection->ldapAttributesForGroupSearch,
|
|
|
|
$this->connection->ldapGroupDisplayName);
|
2013-01-31 04:46:34 +04:00
|
|
|
}
|
|
|
|
|
2014-11-28 14:08:33 +03: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 12:54:36 +03:00
|
|
|
$word = $this->prepareSearchTerm($word);
|
2014-11-28 14:08:33 +03: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 04:46:34 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* creates a filter part for searches
|
2013-01-31 04:46:34 +04:00
|
|
|
* @param string $search the search term
|
2014-05-15 16:12:17 +04:00
|
|
|
* @param string[]|null $searchAttributes
|
2013-01-31 04:46:34 +04:00
|
|
|
* @param string $fallbackAttribute a fallback attribute in case the user
|
|
|
|
* did not define search attributes. Typically the display name attribute.
|
2014-05-11 17:17:27 +04:00
|
|
|
* @return string the final filter part to use in LDAP searches
|
2013-01-31 04:46:34 +04:00
|
|
|
*/
|
|
|
|
private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) {
|
|
|
|
$filter = array();
|
2014-11-28 14:08:33 +03: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 16:22:28 +03:00
|
|
|
ILogger::INFO
|
2014-11-28 14:08:33 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2015-12-18 12:54:36 +03:00
|
|
|
|
|
|
|
$search = $this->prepareSearchTerm($search);
|
2013-04-21 00:45:50 +04:00
|
|
|
if(!is_array($searchAttributes) || count($searchAttributes) === 0) {
|
2016-09-23 01:30:57 +03:00
|
|
|
if ($fallbackAttribute === '') {
|
2013-01-31 04:46:34 +04:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$filter[] = $fallbackAttribute . '=' . $search;
|
|
|
|
} else {
|
|
|
|
foreach($searchAttributes as $attribute) {
|
|
|
|
$filter[] = $attribute . '=' . $search;
|
|
|
|
}
|
|
|
|
}
|
2013-04-21 00:45:50 +04:00
|
|
|
if(count($filter) === 1) {
|
2013-01-31 04:46:34 +04:00
|
|
|
return '('.$filter[0].')';
|
|
|
|
}
|
|
|
|
return $this->combineFilterWithOr($filter);
|
|
|
|
}
|
|
|
|
|
2015-12-18 12:54:36 +03: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-23 01:30:57 +03:00
|
|
|
$result = $term;
|
|
|
|
if ($term === '') {
|
|
|
|
$result = '*';
|
|
|
|
} else if ($allowEnum !== 'no') {
|
|
|
|
$result = $term . '*';
|
|
|
|
}
|
2015-12-18 12:54:36 +03:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2014-10-28 01:39:30 +03:00
|
|
|
/**
|
|
|
|
* returns the filter used for counting users
|
2014-11-20 20:03:47 +03:00
|
|
|
* @return string
|
2014-10-28 01:39:30 +03:00
|
|
|
*/
|
|
|
|
public function getFilterForUserCount() {
|
|
|
|
$filter = $this->combineFilterWithAnd(array(
|
|
|
|
$this->connection->ldapUserFilter,
|
|
|
|
$this->connection->ldapUserDisplayName . '=*'
|
|
|
|
));
|
|
|
|
|
|
|
|
return $filter;
|
|
|
|
}
|
|
|
|
|
2014-02-19 12:31:54 +04:00
|
|
|
/**
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param string $name
|
2014-02-19 12:31:54 +04:00
|
|
|
* @param string $password
|
2014-05-11 17:17:27 +04:00
|
|
|
* @return bool
|
2014-02-19 12:31:54 +04:00
|
|
|
*/
|
2012-07-25 14:37:39 +04:00
|
|
|
public function areCredentialsValid($name, $password) {
|
2016-07-22 11:46:29 +03:00
|
|
|
$name = $this->helper->DNasBaseParameter($name);
|
2012-07-25 14:37:39 +04:00
|
|
|
$testConnection = clone $this->connection;
|
|
|
|
$credentials = array(
|
|
|
|
'ldapAgentName' => $name,
|
|
|
|
'ldapAgentPassword' => $password
|
|
|
|
);
|
|
|
|
if(!$testConnection->setConfiguration($credentials)) {
|
2012-04-16 19:28:58 +04:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-22 22:43:41 +03:00
|
|
|
return $testConnection->bind();
|
2012-04-25 22:34:58 +04:00
|
|
|
}
|
2012-08-23 20:29:43 +04:00
|
|
|
|
2015-12-11 03:56:53 +03: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-23 01:30:57 +03:00
|
|
|
if ($this->connection->ldapUuidUserAttribute === 'auto' && $uuidOverride === '') {
|
2015-12-11 03:56:53 +03: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-15 00:42:27 +03: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 03:56:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new \Exception('Cannot determine UUID attribute');
|
|
|
|
}
|
|
|
|
|
2012-08-23 20:29:43 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* auto-detects the directory's UUID attribute
|
2017-10-31 21:42:17 +03:00
|
|
|
*
|
2014-05-11 17:17:27 +04: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 21:42:17 +03:00
|
|
|
* @param array|null $ldapRecord
|
2014-05-11 18:27:18 +04:00
|
|
|
* @return bool true on success, false otherwise
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws ServerNotAvailableException
|
2012-08-23 20:29:43 +04:00
|
|
|
*/
|
2017-10-31 21:42:17 +03:00
|
|
|
private function detectUuidAttribute($dn, $isUser = true, $force = false, array $ldapRecord = null) {
|
2013-09-13 21:01:40 +04: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 20:29:43 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-17 04:06:10 +03:00
|
|
|
if (is_string($uuidOverride) && trim($uuidOverride) !== '' && !$force) {
|
2013-09-13 21:01:40 +04:00
|
|
|
$this->connection->$uuidAttr = $uuidOverride;
|
2013-05-08 16:56:52 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-31 21:42:17 +03: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 20:29:43 +04:00
|
|
|
|
2013-02-15 01:16:48 +04:00
|
|
|
$value = $this->readAttribute($dn, $attribute);
|
|
|
|
if(is_array($value) && isset($value[0]) && !empty($value[0])) {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog(
|
|
|
|
'user_ldap',
|
|
|
|
'Setting '.$attribute.' as '.$uuidAttr,
|
|
|
|
ILogger::DEBUG
|
|
|
|
);
|
2013-09-13 21:01:40 +04:00
|
|
|
$this->connection->$uuidAttr = $attribute;
|
2012-08-23 20:29:43 +04:00
|
|
|
return true;
|
2013-02-15 01:16:48 +04:00
|
|
|
}
|
2012-08-23 20:29:43 +04:00
|
|
|
}
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog(
|
|
|
|
'user_ldap',
|
|
|
|
'Could not autodetect the UUID attribute',
|
|
|
|
ILogger::ERROR
|
|
|
|
);
|
2012-08-23 20:29:43 +04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
/**
|
2014-05-13 15:29:25 +04:00
|
|
|
* @param string $dn
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param bool $isUser
|
2017-10-31 21:42:17 +03:00
|
|
|
* @param null $ldapRecord
|
|
|
|
* @return bool|string
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws ServerNotAvailableException
|
2014-05-11 17:17:27 +04:00
|
|
|
*/
|
2017-10-31 21:42:17 +03:00
|
|
|
public function getUUID($dn, $isUser = true, $ldapRecord = null) {
|
2013-09-13 21:01:40 +04:00
|
|
|
if($isUser) {
|
|
|
|
$uuidAttr = 'ldapUuidUserAttribute';
|
|
|
|
$uuidOverride = $this->connection->ldapExpertUUIDUserAttr;
|
|
|
|
} else {
|
|
|
|
$uuidAttr = 'ldapUuidGroupAttribute';
|
|
|
|
$uuidOverride = $this->connection->ldapExpertUUIDGroupAttr;
|
|
|
|
}
|
|
|
|
|
|
|
|
$uuid = false;
|
2017-10-31 21:42:17 +03:00
|
|
|
if($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) {
|
2017-10-31 14:20:44 +03:00
|
|
|
$attr = $this->connection->$uuidAttr;
|
2017-10-31 21:42:17 +03:00
|
|
|
$uuid = isset($ldapRecord[$attr]) ? $ldapRecord[$attr] : $this->readAttribute($dn, $attr);
|
2013-09-13 21:01:40 +04:00
|
|
|
if( !is_array($uuid)
|
2016-09-23 01:30:57 +03:00
|
|
|
&& $uuidOverride !== ''
|
2017-10-31 21:42:17 +03: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 20:29:43 +04:00
|
|
|
}
|
|
|
|
if(is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) {
|
|
|
|
$uuid = $uuid[0];
|
|
|
|
}
|
|
|
|
}
|
2013-09-13 21:01:40 +04:00
|
|
|
|
2012-08-23 20:29:43 +04:00
|
|
|
return $uuid;
|
|
|
|
}
|
2012-10-26 23:53:56 +04:00
|
|
|
|
2012-11-09 02:01:28 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* converts a binary ObjectGUID into a string representation
|
2014-05-11 17:17:27 +04: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-09 02:01:28 +04: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-15 00:42:27 +03: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-02 00:02:41 +04:00
|
|
|
/**
|
|
|
|
* gets a SID of the domain of the given dn
|
2019-05-27 08:54:09 +03:00
|
|
|
*
|
2014-07-02 00:02:41 +04:00
|
|
|
* @param string $dn
|
|
|
|
* @return string|bool
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws ServerNotAvailableException
|
2014-07-02 00:02:41 +04:00
|
|
|
*/
|
|
|
|
public function getSID($dn) {
|
|
|
|
$domainDN = $this->getDomainDNFromDN($dn);
|
|
|
|
$cacheKey = 'getSID-'.$domainDN;
|
2016-04-18 11:32:15 +03:00
|
|
|
$sid = $this->connection->getFromCache($cacheKey);
|
|
|
|
if(!is_null($sid)) {
|
|
|
|
return $sid;
|
2014-07-02 00:02:41 +04: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-19 02:01:57 +04: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-02 00:02:41 +04:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2014-09-19 02:01:57 +04: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-02 00:02:41 +04:00
|
|
|
}
|
|
|
|
|
2013-01-04 00:38:53 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* checks if the given DN is part of the given base DN(s)
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param string $dn the DN
|
2014-05-15 16:12:17 +04:00
|
|
|
* @param string[] $bases array containing the allowed base DN or DNs
|
2014-05-11 17:17:27 +04:00
|
|
|
* @return bool
|
2013-01-04 00:38:53 +04:00
|
|
|
*/
|
2014-10-10 23:29:11 +04:00
|
|
|
public function isDNPartOfBase($dn, $bases) {
|
2014-05-11 17:17:27 +04:00
|
|
|
$belongsToBase = false;
|
2016-07-22 11:46:29 +03:00
|
|
|
$bases = $this->helper->sanitizeDN($bases);
|
2014-05-11 17:17:27 +04:00
|
|
|
|
2013-01-04 00:38:53 +04:00
|
|
|
foreach($bases as $base) {
|
|
|
|
$belongsToBase = true;
|
2013-10-18 16:40:08 +04:00
|
|
|
if(mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8')-mb_strlen($base, 'UTF-8'))) {
|
2013-01-04 00:38:53 +04:00
|
|
|
$belongsToBase = false;
|
2013-01-08 22:00:49 +04:00
|
|
|
}
|
|
|
|
if($belongsToBase) {
|
2013-01-04 00:38:53 +04:00
|
|
|
break;
|
2013-01-08 22:00:49 +04:00
|
|
|
}
|
2013-01-04 00:38:53 +04:00
|
|
|
}
|
|
|
|
return $belongsToBase;
|
|
|
|
}
|
|
|
|
|
2014-08-19 20:01:58 +04:00
|
|
|
/**
|
|
|
|
* resets a running Paged Search operation
|
2018-10-25 23:50:04 +03:00
|
|
|
*
|
|
|
|
* @throws ServerNotAvailableException
|
2014-08-19 20:01:58 +04:00
|
|
|
*/
|
|
|
|
private function abandonPagedSearch() {
|
2018-10-25 23:50:04 +03:00
|
|
|
$cr = $this->connection->getConnectionResource();
|
|
|
|
$this->invokeLDAPMethod('controlPagedResult', $cr, 0, false, $this->lastCookie);
|
|
|
|
$this->getPagedSearchResultState();
|
|
|
|
$this->lastCookie = '';
|
|
|
|
$this->cookies = [];
|
2014-08-19 20:01:58 +04:00
|
|
|
}
|
|
|
|
|
2012-10-26 23:53:56 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* get a cookie for the next LDAP paged search
|
2014-05-11 17:17:27 +04: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 18:27:18 +04:00
|
|
|
* @return string containing the key or empty if none is cached
|
2012-10-26 23:53:56 +04:00
|
|
|
*/
|
2013-01-05 02:54:42 +04:00
|
|
|
private function getPagedResultCookie($base, $filter, $limit, $offset) {
|
2013-04-21 00:45:50 +04:00
|
|
|
if($offset === 0) {
|
2012-10-26 23:53:56 +04:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$offset -= $limit;
|
|
|
|
//we work with cache here
|
2018-01-26 17:01:27 +03:00
|
|
|
$cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . (int)$limit . '-' . (int)$offset;
|
2013-10-25 16:15:05 +04:00
|
|
|
$cookie = '';
|
2014-05-11 17:17:27 +04:00
|
|
|
if(isset($this->cookies[$cacheKey])) {
|
|
|
|
$cookie = $this->cookies[$cacheKey];
|
2013-10-25 16:15:05 +04:00
|
|
|
if(is_null($cookie)) {
|
|
|
|
$cookie = '';
|
|
|
|
}
|
2012-10-26 23:53:56 +04:00
|
|
|
}
|
|
|
|
return $cookie;
|
|
|
|
}
|
|
|
|
|
2015-10-01 01:30:05 +03: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 23:53:56 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* set a cookie for LDAP paged search run
|
2014-05-11 17:17:27 +04: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 23:53:56 +04:00
|
|
|
* @return void
|
|
|
|
*/
|
2013-01-05 02:54:42 +04:00
|
|
|
private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) {
|
2015-03-13 00:47:20 +03:00
|
|
|
// allow '0' for 389ds
|
|
|
|
if(!empty($cookie) || $cookie === '0') {
|
2018-01-26 17:01:27 +03:00
|
|
|
$cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . (int)$limit . '-' . (int)$offset;
|
2014-05-11 17:17:27 +04:00
|
|
|
$this->cookies[$cacheKey] = $cookie;
|
2014-08-19 20:01:58 +04:00
|
|
|
$this->lastCookie = $cookie;
|
2012-10-26 23:53:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04: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 19:30:58 +04:00
|
|
|
* @return boolean|null true on success, null or false otherwise
|
2012-10-26 23:53:56 +04:00
|
|
|
*/
|
|
|
|
public function getPagedSearchResultState() {
|
|
|
|
$result = $this->pagedSearchedSuccessful;
|
|
|
|
$this->pagedSearchedSuccessful = null;
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2012-10-27 13:02:35 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Prepares a paged search, if possible
|
2019-05-27 08:54:09 +03:00
|
|
|
*
|
2014-05-11 17:17:27 +04:00
|
|
|
* @param string $filter the LDAP filter for the search
|
2014-05-15 16:12:17 +04: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 17:17:27 +04:00
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @return bool|true
|
2019-05-27 08:54:09 +03:00
|
|
|
* @throws ServerNotAvailableException
|
2012-10-27 13:02:35 +04:00
|
|
|
*/
|
2013-01-05 02:54:42 +04:00
|
|
|
private function initPagedSearch($filter, $bases, $attr, $limit, $offset) {
|
2012-10-27 13:02:35 +04:00
|
|
|
$pagedSearchOK = false;
|
2018-10-25 23:50:04 +03:00
|
|
|
if ($limit !== 0) {
|
2018-01-26 17:01:27 +03:00
|
|
|
$offset = (int)$offset; //can be null
|
2013-02-15 01:16:48 +04:00
|
|
|
\OCP\Util::writeLog('user_ldap',
|
2013-12-10 20:50:45 +04:00
|
|
|
'initializing paged search for Filter '.$filter.' base '.print_r($bases, true)
|
2013-02-15 01:16:48 +04:00
|
|
|
.' attr '.print_r($attr, true). ' limit ' .$limit.' offset '.$offset,
|
2018-04-25 16:22:28 +03:00
|
|
|
ILogger::DEBUG);
|
2012-10-27 13:02:35 +04:00
|
|
|
//get the cookie from the search for the previous search, required by LDAP
|
2013-01-05 02:54:42 +04:00
|
|
|
foreach($bases as $base) {
|
|
|
|
|
|
|
|
$cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset);
|
2015-03-13 00:47:20 +03:00
|
|
|
if(empty($cookie) && $cookie !== "0" && ($offset > 0)) {
|
2017-10-25 18:23:42 +03: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 17:17:18 +03:00
|
|
|
$reOffset = ($offset - $limit) < 0 ? 0 : $offset - $limit;
|
|
|
|
$this->search($filter, array($base), $attr, $limit, $reOffset, true);
|
2013-01-05 02:54:42 +04: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 18:23:42 +03:00
|
|
|
// '0' is valid, because 389ds
|
2013-01-05 02:54:42 +04:00
|
|
|
//TODO: remember this, probably does not change in the next request...
|
2015-03-13 00:47:20 +03:00
|
|
|
if(empty($cookie) && $cookie !== '0') {
|
2013-01-05 02:54:42 +04:00
|
|
|
$cookie = null;
|
|
|
|
}
|
2012-10-27 13:02:35 +04:00
|
|
|
}
|
2013-01-05 02:54:42 +04:00
|
|
|
if(!is_null($cookie)) {
|
2014-09-18 19:00:51 +04:00
|
|
|
//since offset = 0, this is a new search. We abandon other searches that might be ongoing.
|
|
|
|
$this->abandonPagedSearch();
|
2017-05-21 03:24:38 +03:00
|
|
|
$pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult',
|
|
|
|
$this->connection->getConnectionResource(), $limit,
|
|
|
|
false, $cookie);
|
2013-01-05 02:54:42 +04:00
|
|
|
if(!$pagedSearchOK) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('user_ldap', 'Ready for a paged search', ILogger::DEBUG);
|
2013-01-05 02:54:42 +04:00
|
|
|
} else {
|
2018-01-11 16:17:14 +03:00
|
|
|
$e = new \Exception('No paged search possible, Limit '.$limit.' Offset '.$offset);
|
2018-04-25 16:22:28 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, ['level' => ILogger::DEBUG]);
|
2012-10-27 13:02:35 +04:00
|
|
|
}
|
2013-01-05 02:54:42 +04:00
|
|
|
|
2012-10-27 13:02:35 +04:00
|
|
|
}
|
2015-07-16 23:17:57 +03: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 23:50:04 +03:00
|
|
|
} else if ($limit === 0 && !empty($this->lastCookie)) {
|
2015-03-05 15:15:21 +03: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 17:01:27 +03:00
|
|
|
$pageSize = (int)$this->connection->ldapPagingSize > 0 ? (int)$this->connection->ldapPagingSize : 500;
|
2017-05-21 03:24:38 +03:00
|
|
|
$pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult',
|
|
|
|
$this->connection->getConnectionResource(),
|
|
|
|
$pageSize, false, '');
|
2012-10-27 13:02:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $pagedSearchOK;
|
|
|
|
}
|
|
|
|
|
2018-11-25 00:06:45 +03:00
|
|
|
/**
|
|
|
|
* Is more than one $attr used for search?
|
|
|
|
*
|
|
|
|
* @param string|string[]|null $attr
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-11-25 14:30:54 +03:00
|
|
|
private function manyAttributes($attr): bool {
|
2018-11-25 00:06:45 +03:00
|
|
|
if (\is_array($attr)) {
|
|
|
|
return \count($attr) > 1;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-06 21:11:14 +04:00
|
|
|
}
|