@@ -66,1762 +66,1762 @@ discard block |
||
| 66 | 66 | * @package OCA\User_LDAP |
| 67 | 67 | */ |
| 68 | 68 | class Access extends LDAPUtility { |
| 69 | - public const UUID_ATTRIBUTES = ['entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid']; |
|
| 70 | - |
|
| 71 | - /** @var \OCA\User_LDAP\Connection */ |
|
| 72 | - public $connection; |
|
| 73 | - /** @var Manager */ |
|
| 74 | - public $userManager; |
|
| 75 | - //never ever check this var directly, always use getPagedSearchResultState |
|
| 76 | - protected $pagedSearchedSuccessful; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @var UserMapping $userMapper |
|
| 80 | - */ |
|
| 81 | - protected $userMapper; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @var AbstractMapping $userMapper |
|
| 85 | - */ |
|
| 86 | - protected $groupMapper; |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @var \OCA\User_LDAP\Helper |
|
| 90 | - */ |
|
| 91 | - private $helper; |
|
| 92 | - /** @var IConfig */ |
|
| 93 | - private $config; |
|
| 94 | - /** @var IUserManager */ |
|
| 95 | - private $ncUserManager; |
|
| 96 | - /** @var string */ |
|
| 97 | - private $lastCookie = ''; |
|
| 98 | - |
|
| 99 | - public function __construct( |
|
| 100 | - Connection $connection, |
|
| 101 | - ILDAPWrapper $ldap, |
|
| 102 | - Manager $userManager, |
|
| 103 | - Helper $helper, |
|
| 104 | - IConfig $config, |
|
| 105 | - IUserManager $ncUserManager |
|
| 106 | - ) { |
|
| 107 | - parent::__construct($ldap); |
|
| 108 | - $this->connection = $connection; |
|
| 109 | - $this->userManager = $userManager; |
|
| 110 | - $this->userManager->setLdapAccess($this); |
|
| 111 | - $this->helper = $helper; |
|
| 112 | - $this->config = $config; |
|
| 113 | - $this->ncUserManager = $ncUserManager; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * sets the User Mapper |
|
| 118 | - * |
|
| 119 | - * @param AbstractMapping $mapper |
|
| 120 | - */ |
|
| 121 | - public function setUserMapper(AbstractMapping $mapper) { |
|
| 122 | - $this->userMapper = $mapper; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @throws \Exception |
|
| 127 | - */ |
|
| 128 | - public function getUserMapper(): UserMapping { |
|
| 129 | - if (is_null($this->userMapper)) { |
|
| 130 | - throw new \Exception('UserMapper was not assigned to this Access instance.'); |
|
| 131 | - } |
|
| 132 | - return $this->userMapper; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * sets the Group Mapper |
|
| 137 | - * |
|
| 138 | - * @param AbstractMapping $mapper |
|
| 139 | - */ |
|
| 140 | - public function setGroupMapper(AbstractMapping $mapper) { |
|
| 141 | - $this->groupMapper = $mapper; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * returns the Group Mapper |
|
| 146 | - * |
|
| 147 | - * @return AbstractMapping |
|
| 148 | - * @throws \Exception |
|
| 149 | - */ |
|
| 150 | - public function getGroupMapper() { |
|
| 151 | - if (is_null($this->groupMapper)) { |
|
| 152 | - throw new \Exception('GroupMapper was not assigned to this Access instance.'); |
|
| 153 | - } |
|
| 154 | - return $this->groupMapper; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * @return bool |
|
| 159 | - */ |
|
| 160 | - private function checkConnection() { |
|
| 161 | - return ($this->connection instanceof Connection); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * returns the Connection instance |
|
| 166 | - * |
|
| 167 | - * @return \OCA\User_LDAP\Connection |
|
| 168 | - */ |
|
| 169 | - public function getConnection() { |
|
| 170 | - return $this->connection; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * reads a given attribute for an LDAP record identified by a DN |
|
| 175 | - * |
|
| 176 | - * @param string $dn the record in question |
|
| 177 | - * @param string $attr the attribute that shall be retrieved |
|
| 178 | - * if empty, just check the record's existence |
|
| 179 | - * @param string $filter |
|
| 180 | - * @return array|false an array of values on success or an empty |
|
| 181 | - * array if $attr is empty, false otherwise |
|
| 182 | - * @throws ServerNotAvailableException |
|
| 183 | - */ |
|
| 184 | - public function readAttribute($dn, $attr, $filter = 'objectClass=*') { |
|
| 185 | - if (!$this->checkConnection()) { |
|
| 186 | - \OCP\Util::writeLog('user_ldap', |
|
| 187 | - 'No LDAP Connector assigned, access impossible for readAttribute.', |
|
| 188 | - ILogger::WARN); |
|
| 189 | - return false; |
|
| 190 | - } |
|
| 191 | - $cr = $this->connection->getConnectionResource(); |
|
| 192 | - if (!$this->ldap->isResource($cr)) { |
|
| 193 | - //LDAP not available |
|
| 194 | - \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', ILogger::DEBUG); |
|
| 195 | - return false; |
|
| 196 | - } |
|
| 197 | - //Cancel possibly running Paged Results operation, otherwise we run in |
|
| 198 | - //LDAP protocol errors |
|
| 199 | - $this->abandonPagedSearch(); |
|
| 200 | - // openLDAP requires that we init a new Paged Search. Not needed by AD, |
|
| 201 | - // but does not hurt either. |
|
| 202 | - $pagingSize = (int)$this->connection->ldapPagingSize; |
|
| 203 | - // 0 won't result in replies, small numbers may leave out groups |
|
| 204 | - // (cf. #12306), 500 is default for paging and should work everywhere. |
|
| 205 | - $maxResults = $pagingSize > 20 ? $pagingSize : 500; |
|
| 206 | - $attr = mb_strtolower($attr, 'UTF-8'); |
|
| 207 | - // the actual read attribute later may contain parameters on a ranged |
|
| 208 | - // request, e.g. member;range=99-199. Depends on server reply. |
|
| 209 | - $attrToRead = $attr; |
|
| 210 | - |
|
| 211 | - $values = []; |
|
| 212 | - $isRangeRequest = false; |
|
| 213 | - do { |
|
| 214 | - $result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults); |
|
| 215 | - if (is_bool($result)) { |
|
| 216 | - // when an exists request was run and it was successful, an empty |
|
| 217 | - // array must be returned |
|
| 218 | - return $result ? [] : false; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - if (!$isRangeRequest) { |
|
| 222 | - $values = $this->extractAttributeValuesFromResult($result, $attr); |
|
| 223 | - if (!empty($values)) { |
|
| 224 | - return $values; |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - $isRangeRequest = false; |
|
| 229 | - $result = $this->extractRangeData($result, $attr); |
|
| 230 | - if (!empty($result)) { |
|
| 231 | - $normalizedResult = $this->extractAttributeValuesFromResult( |
|
| 232 | - [$attr => $result['values']], |
|
| 233 | - $attr |
|
| 234 | - ); |
|
| 235 | - $values = array_merge($values, $normalizedResult); |
|
| 236 | - |
|
| 237 | - if ($result['rangeHigh'] === '*') { |
|
| 238 | - // when server replies with * as high range value, there are |
|
| 239 | - // no more results left |
|
| 240 | - return $values; |
|
| 241 | - } else { |
|
| 242 | - $low = $result['rangeHigh'] + 1; |
|
| 243 | - $attrToRead = $result['attributeName'] . ';range=' . $low . '-*'; |
|
| 244 | - $isRangeRequest = true; |
|
| 245 | - } |
|
| 246 | - } |
|
| 247 | - } while ($isRangeRequest); |
|
| 248 | - |
|
| 249 | - \OCP\Util::writeLog('user_ldap', 'Requested attribute ' . $attr . ' not found for ' . $dn, ILogger::DEBUG); |
|
| 250 | - return false; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Runs an read operation against LDAP |
|
| 255 | - * |
|
| 256 | - * @param resource $cr the LDAP connection |
|
| 257 | - * @param string $dn |
|
| 258 | - * @param string $attribute |
|
| 259 | - * @param string $filter |
|
| 260 | - * @param int $maxResults |
|
| 261 | - * @return array|bool false if there was any error, true if an exists check |
|
| 262 | - * was performed and the requested DN found, array with the |
|
| 263 | - * returned data on a successful usual operation |
|
| 264 | - * @throws ServerNotAvailableException |
|
| 265 | - */ |
|
| 266 | - public function executeRead($cr, $dn, $attribute, $filter, $maxResults) { |
|
| 267 | - $this->initPagedSearch($filter, $dn, [$attribute], $maxResults, 0); |
|
| 268 | - $dn = $this->helper->DNasBaseParameter($dn); |
|
| 269 | - $rr = @$this->invokeLDAPMethod('read', $cr, $dn, $filter, [$attribute]); |
|
| 270 | - if (!$this->ldap->isResource($rr)) { |
|
| 271 | - if ($attribute !== '') { |
|
| 272 | - //do not throw this message on userExists check, irritates |
|
| 273 | - \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, ILogger::DEBUG); |
|
| 274 | - } |
|
| 275 | - //in case an error occurs , e.g. object does not exist |
|
| 276 | - return false; |
|
| 277 | - } |
|
| 278 | - if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) { |
|
| 279 | - \OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', ILogger::DEBUG); |
|
| 280 | - return true; |
|
| 281 | - } |
|
| 282 | - $er = $this->invokeLDAPMethod('firstEntry', $cr, $rr); |
|
| 283 | - if (!$this->ldap->isResource($er)) { |
|
| 284 | - //did not match the filter, return false |
|
| 285 | - return false; |
|
| 286 | - } |
|
| 287 | - //LDAP attributes are not case sensitive |
|
| 288 | - $result = \OCP\Util::mb_array_change_key_case( |
|
| 289 | - $this->invokeLDAPMethod('getAttributes', $cr, $er), MB_CASE_LOWER, 'UTF-8'); |
|
| 290 | - |
|
| 291 | - return $result; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Normalizes a result grom getAttributes(), i.e. handles DNs and binary |
|
| 296 | - * data if present. |
|
| 297 | - * |
|
| 298 | - * @param array $result from ILDAPWrapper::getAttributes() |
|
| 299 | - * @param string $attribute the attribute name that was read |
|
| 300 | - * @return string[] |
|
| 301 | - */ |
|
| 302 | - public function extractAttributeValuesFromResult($result, $attribute) { |
|
| 303 | - $values = []; |
|
| 304 | - if (isset($result[$attribute]) && $result[$attribute]['count'] > 0) { |
|
| 305 | - $lowercaseAttribute = strtolower($attribute); |
|
| 306 | - for ($i = 0; $i < $result[$attribute]['count']; $i++) { |
|
| 307 | - if ($this->resemblesDN($attribute)) { |
|
| 308 | - $values[] = $this->helper->sanitizeDN($result[$attribute][$i]); |
|
| 309 | - } elseif ($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { |
|
| 310 | - $values[] = $this->convertObjectGUID2Str($result[$attribute][$i]); |
|
| 311 | - } else { |
|
| 312 | - $values[] = $result[$attribute][$i]; |
|
| 313 | - } |
|
| 314 | - } |
|
| 315 | - } |
|
| 316 | - return $values; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Attempts to find ranged data in a getAttribute results and extracts the |
|
| 321 | - * returned values as well as information on the range and full attribute |
|
| 322 | - * name for further processing. |
|
| 323 | - * |
|
| 324 | - * @param array $result from ILDAPWrapper::getAttributes() |
|
| 325 | - * @param string $attribute the attribute name that was read. Without ";range=…" |
|
| 326 | - * @return array If a range was detected with keys 'values', 'attributeName', |
|
| 327 | - * 'attributeFull' and 'rangeHigh', otherwise empty. |
|
| 328 | - */ |
|
| 329 | - public function extractRangeData($result, $attribute) { |
|
| 330 | - $keys = array_keys($result); |
|
| 331 | - foreach ($keys as $key) { |
|
| 332 | - if ($key !== $attribute && strpos($key, $attribute) === 0) { |
|
| 333 | - $queryData = explode(';', $key); |
|
| 334 | - if (strpos($queryData[1], 'range=') === 0) { |
|
| 335 | - $high = substr($queryData[1], 1 + strpos($queryData[1], '-')); |
|
| 336 | - $data = [ |
|
| 337 | - 'values' => $result[$key], |
|
| 338 | - 'attributeName' => $queryData[0], |
|
| 339 | - 'attributeFull' => $key, |
|
| 340 | - 'rangeHigh' => $high, |
|
| 341 | - ]; |
|
| 342 | - return $data; |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - } |
|
| 346 | - return []; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * Set password for an LDAP user identified by a DN |
|
| 351 | - * |
|
| 352 | - * @param string $userDN the user in question |
|
| 353 | - * @param string $password the new password |
|
| 354 | - * @return bool |
|
| 355 | - * @throws HintException |
|
| 356 | - * @throws \Exception |
|
| 357 | - */ |
|
| 358 | - public function setPassword($userDN, $password) { |
|
| 359 | - if ((int)$this->connection->turnOnPasswordChange !== 1) { |
|
| 360 | - throw new \Exception('LDAP password changes are disabled.'); |
|
| 361 | - } |
|
| 362 | - $cr = $this->connection->getConnectionResource(); |
|
| 363 | - if (!$this->ldap->isResource($cr)) { |
|
| 364 | - //LDAP not available |
|
| 365 | - \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', ILogger::DEBUG); |
|
| 366 | - return false; |
|
| 367 | - } |
|
| 368 | - try { |
|
| 369 | - // try PASSWD extended operation first |
|
| 370 | - return @$this->invokeLDAPMethod('exopPasswd', $cr, $userDN, '', $password) || |
|
| 371 | - @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password); |
|
| 372 | - } catch (ConstraintViolationException $e) { |
|
| 373 | - throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ') . $e->getMessage(), $e->getCode()); |
|
| 374 | - } |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * checks whether the given attributes value is probably a DN |
|
| 379 | - * |
|
| 380 | - * @param string $attr the attribute in question |
|
| 381 | - * @return boolean if so true, otherwise false |
|
| 382 | - */ |
|
| 383 | - private function resemblesDN($attr) { |
|
| 384 | - $resemblingAttributes = [ |
|
| 385 | - 'dn', |
|
| 386 | - 'uniquemember', |
|
| 387 | - 'member', |
|
| 388 | - // memberOf is an "operational" attribute, without a definition in any RFC |
|
| 389 | - 'memberof' |
|
| 390 | - ]; |
|
| 391 | - return in_array($attr, $resemblingAttributes); |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - /** |
|
| 395 | - * checks whether the given string is probably a DN |
|
| 396 | - * |
|
| 397 | - * @param string $string |
|
| 398 | - * @return boolean |
|
| 399 | - */ |
|
| 400 | - public function stringResemblesDN($string) { |
|
| 401 | - $r = $this->ldap->explodeDN($string, 0); |
|
| 402 | - // if exploding a DN succeeds and does not end up in |
|
| 403 | - // an empty array except for $r[count] being 0. |
|
| 404 | - return (is_array($r) && count($r) > 1); |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * returns a DN-string that is cleaned from not domain parts, e.g. |
|
| 409 | - * cn=foo,cn=bar,dc=foobar,dc=server,dc=org |
|
| 410 | - * becomes dc=foobar,dc=server,dc=org |
|
| 411 | - * |
|
| 412 | - * @param string $dn |
|
| 413 | - * @return string |
|
| 414 | - */ |
|
| 415 | - public function getDomainDNFromDN($dn) { |
|
| 416 | - $allParts = $this->ldap->explodeDN($dn, 0); |
|
| 417 | - if ($allParts === false) { |
|
| 418 | - //not a valid DN |
|
| 419 | - return ''; |
|
| 420 | - } |
|
| 421 | - $domainParts = []; |
|
| 422 | - $dcFound = false; |
|
| 423 | - foreach ($allParts as $part) { |
|
| 424 | - if (!$dcFound && strpos($part, 'dc=') === 0) { |
|
| 425 | - $dcFound = true; |
|
| 426 | - } |
|
| 427 | - if ($dcFound) { |
|
| 428 | - $domainParts[] = $part; |
|
| 429 | - } |
|
| 430 | - } |
|
| 431 | - return implode(',', $domainParts); |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * returns the LDAP DN for the given internal Nextcloud name of the group |
|
| 436 | - * |
|
| 437 | - * @param string $name the Nextcloud name in question |
|
| 438 | - * @return string|false LDAP DN on success, otherwise false |
|
| 439 | - */ |
|
| 440 | - public function groupname2dn($name) { |
|
| 441 | - return $this->groupMapper->getDNByName($name); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * returns the LDAP DN for the given internal Nextcloud name of the user |
|
| 446 | - * |
|
| 447 | - * @param string $name the Nextcloud name in question |
|
| 448 | - * @return string|false with the LDAP DN on success, otherwise false |
|
| 449 | - */ |
|
| 450 | - public function username2dn($name) { |
|
| 451 | - $fdn = $this->userMapper->getDNByName($name); |
|
| 452 | - |
|
| 453 | - //Check whether the DN belongs to the Base, to avoid issues on multi- |
|
| 454 | - //server setups |
|
| 455 | - if (is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 456 | - return $fdn; |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - return false; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * returns the internal Nextcloud name for the given LDAP DN of the group, false on DN outside of search DN or failure |
|
| 464 | - * |
|
| 465 | - * @param string $fdn the dn of the group object |
|
| 466 | - * @param string $ldapName optional, the display name of the object |
|
| 467 | - * @return string|false with the name to use in Nextcloud, false on DN outside of search DN |
|
| 468 | - * @throws \Exception |
|
| 469 | - */ |
|
| 470 | - public function dn2groupname($fdn, $ldapName = null) { |
|
| 471 | - //To avoid bypassing the base DN settings under certain circumstances |
|
| 472 | - //with the group support, check whether the provided DN matches one of |
|
| 473 | - //the given Bases |
|
| 474 | - if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { |
|
| 475 | - return false; |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - return $this->dn2ocname($fdn, $ldapName, false); |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - /** |
|
| 482 | - * returns the internal Nextcloud name for the given LDAP DN of the user, false on DN outside of search DN or failure |
|
| 483 | - * |
|
| 484 | - * @param string $dn the dn of the user object |
|
| 485 | - * @param string $ldapName optional, the display name of the object |
|
| 486 | - * @return string|false with with the name to use in Nextcloud |
|
| 487 | - * @throws \Exception |
|
| 488 | - */ |
|
| 489 | - public function dn2username($fdn, $ldapName = null) { |
|
| 490 | - //To avoid bypassing the base DN settings under certain circumstances |
|
| 491 | - //with the group support, check whether the provided DN matches one of |
|
| 492 | - //the given Bases |
|
| 493 | - if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 494 | - return false; |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - return $this->dn2ocname($fdn, $ldapName, true); |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - /** |
|
| 501 | - * returns an internal Nextcloud name for the given LDAP DN, false on DN outside of search DN |
|
| 502 | - * |
|
| 503 | - * @param string $fdn the dn of the user object |
|
| 504 | - * @param string|null $ldapName optional, the display name of the object |
|
| 505 | - * @param bool $isUser optional, whether it is a user object (otherwise group assumed) |
|
| 506 | - * @param bool|null $newlyMapped |
|
| 507 | - * @param array|null $record |
|
| 508 | - * @return false|string with with the name to use in Nextcloud |
|
| 509 | - * @throws \Exception |
|
| 510 | - */ |
|
| 511 | - public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped = null, array $record = null) { |
|
| 512 | - $newlyMapped = false; |
|
| 513 | - if ($isUser) { |
|
| 514 | - $mapper = $this->getUserMapper(); |
|
| 515 | - $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 516 | - $filter = $this->connection->ldapUserFilter; |
|
| 517 | - } else { |
|
| 518 | - $mapper = $this->getGroupMapper(); |
|
| 519 | - $nameAttribute = $this->connection->ldapGroupDisplayName; |
|
| 520 | - $filter = $this->connection->ldapGroupFilter; |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - //let's try to retrieve the Nextcloud name from the mappings table |
|
| 524 | - $ncName = $mapper->getNameByDN($fdn); |
|
| 525 | - if (is_string($ncName)) { |
|
| 526 | - return $ncName; |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - //second try: get the UUID and check if it is known. Then, update the DN and return the name. |
|
| 530 | - $uuid = $this->getUUID($fdn, $isUser, $record); |
|
| 531 | - if (is_string($uuid)) { |
|
| 532 | - $ncName = $mapper->getNameByUUID($uuid); |
|
| 533 | - if (is_string($ncName)) { |
|
| 534 | - $mapper->setDNbyUUID($fdn, $uuid); |
|
| 535 | - return $ncName; |
|
| 536 | - } |
|
| 537 | - } else { |
|
| 538 | - //If the UUID can't be detected something is foul. |
|
| 539 | - \OCP\Util::writeLog('user_ldap', 'Cannot determine UUID for ' . $fdn . '. Skipping.', ILogger::INFO); |
|
| 540 | - return false; |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - if (is_null($ldapName)) { |
|
| 544 | - $ldapName = $this->readAttribute($fdn, $nameAttribute, $filter); |
|
| 545 | - if (!isset($ldapName[0]) && empty($ldapName[0])) { |
|
| 546 | - \OCP\Util::writeLog('user_ldap', 'No or empty name for ' . $fdn . ' with filter ' . $filter . '.', ILogger::DEBUG); |
|
| 547 | - return false; |
|
| 548 | - } |
|
| 549 | - $ldapName = $ldapName[0]; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - if ($isUser) { |
|
| 553 | - $usernameAttribute = (string)$this->connection->ldapExpertUsernameAttr; |
|
| 554 | - if ($usernameAttribute !== '') { |
|
| 555 | - $username = $this->readAttribute($fdn, $usernameAttribute); |
|
| 556 | - $username = $username[0]; |
|
| 557 | - } else { |
|
| 558 | - $username = $uuid; |
|
| 559 | - } |
|
| 560 | - try { |
|
| 561 | - $intName = $this->sanitizeUsername($username); |
|
| 562 | - } catch (\InvalidArgumentException $e) { |
|
| 563 | - \OC::$server->getLogger()->logException($e, [ |
|
| 564 | - 'app' => 'user_ldap', |
|
| 565 | - 'level' => ILogger::WARN, |
|
| 566 | - ]); |
|
| 567 | - // we don't attempt to set a username here. We can go for |
|
| 568 | - // for an alternative 4 digit random number as we would append |
|
| 569 | - // otherwise, however it's likely not enough space in bigger |
|
| 570 | - // setups, and most importantly: this is not intended. |
|
| 571 | - return false; |
|
| 572 | - } |
|
| 573 | - } else { |
|
| 574 | - $intName = $ldapName; |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - //a new user/group! Add it only if it doesn't conflict with other backend's users or existing groups |
|
| 578 | - //disabling Cache is required to avoid that the new user is cached as not-existing in fooExists check |
|
| 579 | - //NOTE: mind, disabling cache affects only this instance! Using it |
|
| 580 | - // outside of core user management will still cache the user as non-existing. |
|
| 581 | - $originalTTL = $this->connection->ldapCacheTTL; |
|
| 582 | - $this->connection->setConfiguration(['ldapCacheTTL' => 0]); |
|
| 583 | - if ($intName !== '' |
|
| 584 | - && (($isUser && !$this->ncUserManager->userExists($intName)) |
|
| 585 | - || (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName)) |
|
| 586 | - ) |
|
| 587 | - ) { |
|
| 588 | - $this->connection->setConfiguration(['ldapCacheTTL' => $originalTTL]); |
|
| 589 | - $newlyMapped = $this->mapAndAnnounceIfApplicable($mapper, $fdn, $intName, $uuid, $isUser); |
|
| 590 | - if ($newlyMapped) { |
|
| 591 | - return $intName; |
|
| 592 | - } |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - $this->connection->setConfiguration(['ldapCacheTTL' => $originalTTL]); |
|
| 596 | - $altName = $this->createAltInternalOwnCloudName($intName, $isUser); |
|
| 597 | - if (is_string($altName)) { |
|
| 598 | - if ($this->mapAndAnnounceIfApplicable($mapper, $fdn, $altName, $uuid, $isUser)) { |
|
| 599 | - $newlyMapped = true; |
|
| 600 | - return $altName; |
|
| 601 | - } |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - //if everything else did not help.. |
|
| 605 | - \OCP\Util::writeLog('user_ldap', 'Could not create unique name for ' . $fdn . '.', ILogger::INFO); |
|
| 606 | - return false; |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - public function mapAndAnnounceIfApplicable( |
|
| 610 | - AbstractMapping $mapper, |
|
| 611 | - string $fdn, |
|
| 612 | - string $name, |
|
| 613 | - string $uuid, |
|
| 614 | - bool $isUser |
|
| 615 | - ): bool { |
|
| 616 | - if ($mapper->map($fdn, $name, $uuid)) { |
|
| 617 | - if ($this->ncUserManager instanceof PublicEmitter && $isUser) { |
|
| 618 | - $this->cacheUserExists($name); |
|
| 619 | - $this->ncUserManager->emit('\OC\User', 'assignedUserId', [$name]); |
|
| 620 | - } elseif (!$isUser) { |
|
| 621 | - $this->cacheGroupExists($name); |
|
| 622 | - } |
|
| 623 | - return true; |
|
| 624 | - } |
|
| 625 | - return false; |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * gives back the user names as they are used ownClod internally |
|
| 630 | - * |
|
| 631 | - * @param array $ldapUsers as returned by fetchList() |
|
| 632 | - * @return array an array with the user names to use in Nextcloud |
|
| 633 | - * |
|
| 634 | - * gives back the user names as they are used ownClod internally |
|
| 635 | - * @throws \Exception |
|
| 636 | - */ |
|
| 637 | - public function nextcloudUserNames($ldapUsers) { |
|
| 638 | - return $this->ldap2NextcloudNames($ldapUsers, true); |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - /** |
|
| 642 | - * gives back the group names as they are used ownClod internally |
|
| 643 | - * |
|
| 644 | - * @param array $ldapGroups as returned by fetchList() |
|
| 645 | - * @return array an array with the group names to use in Nextcloud |
|
| 646 | - * |
|
| 647 | - * gives back the group names as they are used ownClod internally |
|
| 648 | - * @throws \Exception |
|
| 649 | - */ |
|
| 650 | - public function nextcloudGroupNames($ldapGroups) { |
|
| 651 | - return $this->ldap2NextcloudNames($ldapGroups, false); |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - /** |
|
| 655 | - * @param array $ldapObjects as returned by fetchList() |
|
| 656 | - * @param bool $isUsers |
|
| 657 | - * @return array |
|
| 658 | - * @throws \Exception |
|
| 659 | - */ |
|
| 660 | - private function ldap2NextcloudNames($ldapObjects, $isUsers) { |
|
| 661 | - if ($isUsers) { |
|
| 662 | - $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 663 | - $sndAttribute = $this->connection->ldapUserDisplayName2; |
|
| 664 | - } else { |
|
| 665 | - $nameAttribute = $this->connection->ldapGroupDisplayName; |
|
| 666 | - } |
|
| 667 | - $nextcloudNames = []; |
|
| 668 | - |
|
| 669 | - foreach ($ldapObjects as $ldapObject) { |
|
| 670 | - $nameByLDAP = null; |
|
| 671 | - if (isset($ldapObject[$nameAttribute]) |
|
| 672 | - && is_array($ldapObject[$nameAttribute]) |
|
| 673 | - && isset($ldapObject[$nameAttribute][0]) |
|
| 674 | - ) { |
|
| 675 | - // might be set, but not necessarily. if so, we use it. |
|
| 676 | - $nameByLDAP = $ldapObject[$nameAttribute][0]; |
|
| 677 | - } |
|
| 678 | - |
|
| 679 | - $ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers); |
|
| 680 | - if ($ncName) { |
|
| 681 | - $nextcloudNames[] = $ncName; |
|
| 682 | - if ($isUsers) { |
|
| 683 | - $this->updateUserState($ncName); |
|
| 684 | - //cache the user names so it does not need to be retrieved |
|
| 685 | - //again later (e.g. sharing dialogue). |
|
| 686 | - if (is_null($nameByLDAP)) { |
|
| 687 | - continue; |
|
| 688 | - } |
|
| 689 | - $sndName = isset($ldapObject[$sndAttribute][0]) |
|
| 690 | - ? $ldapObject[$sndAttribute][0] : ''; |
|
| 691 | - $this->cacheUserDisplayName($ncName, $nameByLDAP, $sndName); |
|
| 692 | - } elseif ($nameByLDAP !== null) { |
|
| 693 | - $this->cacheGroupDisplayName($ncName, $nameByLDAP); |
|
| 694 | - } |
|
| 695 | - } |
|
| 696 | - } |
|
| 697 | - return $nextcloudNames; |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - /** |
|
| 701 | - * removes the deleted-flag of a user if it was set |
|
| 702 | - * |
|
| 703 | - * @param string $ncname |
|
| 704 | - * @throws \Exception |
|
| 705 | - */ |
|
| 706 | - public function updateUserState($ncname) { |
|
| 707 | - $user = $this->userManager->get($ncname); |
|
| 708 | - if ($user instanceof OfflineUser) { |
|
| 709 | - $user->unmark(); |
|
| 710 | - } |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - /** |
|
| 714 | - * caches the user display name |
|
| 715 | - * |
|
| 716 | - * @param string $ocName the internal Nextcloud username |
|
| 717 | - * @param string|false $home the home directory path |
|
| 718 | - */ |
|
| 719 | - public function cacheUserHome($ocName, $home) { |
|
| 720 | - $cacheKey = 'getHome' . $ocName; |
|
| 721 | - $this->connection->writeToCache($cacheKey, $home); |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - /** |
|
| 725 | - * caches a user as existing |
|
| 726 | - * |
|
| 727 | - * @param string $ocName the internal Nextcloud username |
|
| 728 | - */ |
|
| 729 | - public function cacheUserExists($ocName) { |
|
| 730 | - $this->connection->writeToCache('userExists' . $ocName, true); |
|
| 731 | - } |
|
| 732 | - |
|
| 733 | - /** |
|
| 734 | - * caches a group as existing |
|
| 735 | - */ |
|
| 736 | - public function cacheGroupExists(string $gid): void { |
|
| 737 | - $this->connection->writeToCache('groupExists' . $gid, true); |
|
| 738 | - } |
|
| 739 | - |
|
| 740 | - /** |
|
| 741 | - * caches the user display name |
|
| 742 | - * |
|
| 743 | - * @param string $ocName the internal Nextcloud username |
|
| 744 | - * @param string $displayName the display name |
|
| 745 | - * @param string $displayName2 the second display name |
|
| 746 | - * @throws \Exception |
|
| 747 | - */ |
|
| 748 | - public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { |
|
| 749 | - $user = $this->userManager->get($ocName); |
|
| 750 | - if ($user === null) { |
|
| 751 | - return; |
|
| 752 | - } |
|
| 753 | - $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
| 754 | - $cacheKeyTrunk = 'getDisplayName'; |
|
| 755 | - $this->connection->writeToCache($cacheKeyTrunk . $ocName, $displayName); |
|
| 756 | - } |
|
| 757 | - |
|
| 758 | - public function cacheGroupDisplayName(string $ncName, string $displayName): void { |
|
| 759 | - $cacheKey = 'group_getDisplayName' . $ncName; |
|
| 760 | - $this->connection->writeToCache($cacheKey, $displayName); |
|
| 761 | - } |
|
| 762 | - |
|
| 763 | - /** |
|
| 764 | - * creates a unique name for internal Nextcloud use for users. Don't call it directly. |
|
| 765 | - * |
|
| 766 | - * @param string $name the display name of the object |
|
| 767 | - * @return string|false with with the name to use in Nextcloud or false if unsuccessful |
|
| 768 | - * |
|
| 769 | - * Instead of using this method directly, call |
|
| 770 | - * createAltInternalOwnCloudName($name, true) |
|
| 771 | - */ |
|
| 772 | - private function _createAltInternalOwnCloudNameForUsers($name) { |
|
| 773 | - $attempts = 0; |
|
| 774 | - //while loop is just a precaution. If a name is not generated within |
|
| 775 | - //20 attempts, something else is very wrong. Avoids infinite loop. |
|
| 776 | - while ($attempts < 20) { |
|
| 777 | - $altName = $name . '_' . rand(1000, 9999); |
|
| 778 | - if (!$this->ncUserManager->userExists($altName)) { |
|
| 779 | - return $altName; |
|
| 780 | - } |
|
| 781 | - $attempts++; |
|
| 782 | - } |
|
| 783 | - return false; |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - /** |
|
| 787 | - * creates a unique name for internal Nextcloud use for groups. Don't call it directly. |
|
| 788 | - * |
|
| 789 | - * @param string $name the display name of the object |
|
| 790 | - * @return string|false with with the name to use in Nextcloud or false if unsuccessful. |
|
| 791 | - * |
|
| 792 | - * Instead of using this method directly, call |
|
| 793 | - * createAltInternalOwnCloudName($name, false) |
|
| 794 | - * |
|
| 795 | - * Group names are also used as display names, so we do a sequential |
|
| 796 | - * numbering, e.g. Developers_42 when there are 41 other groups called |
|
| 797 | - * "Developers" |
|
| 798 | - */ |
|
| 799 | - private function _createAltInternalOwnCloudNameForGroups($name) { |
|
| 800 | - $usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%'); |
|
| 801 | - if (!$usedNames || count($usedNames) === 0) { |
|
| 802 | - $lastNo = 1; //will become name_2 |
|
| 803 | - } else { |
|
| 804 | - natsort($usedNames); |
|
| 805 | - $lastName = array_pop($usedNames); |
|
| 806 | - $lastNo = (int)substr($lastName, strrpos($lastName, '_') + 1); |
|
| 807 | - } |
|
| 808 | - $altName = $name . '_' . (string)($lastNo + 1); |
|
| 809 | - unset($usedNames); |
|
| 810 | - |
|
| 811 | - $attempts = 1; |
|
| 812 | - while ($attempts < 21) { |
|
| 813 | - // Check to be really sure it is unique |
|
| 814 | - // while loop is just a precaution. If a name is not generated within |
|
| 815 | - // 20 attempts, something else is very wrong. Avoids infinite loop. |
|
| 816 | - if (!\OC::$server->getGroupManager()->groupExists($altName)) { |
|
| 817 | - return $altName; |
|
| 818 | - } |
|
| 819 | - $altName = $name . '_' . ($lastNo + $attempts); |
|
| 820 | - $attempts++; |
|
| 821 | - } |
|
| 822 | - return false; |
|
| 823 | - } |
|
| 824 | - |
|
| 825 | - /** |
|
| 826 | - * creates a unique name for internal Nextcloud use. |
|
| 827 | - * |
|
| 828 | - * @param string $name the display name of the object |
|
| 829 | - * @param boolean $isUser whether name should be created for a user (true) or a group (false) |
|
| 830 | - * @return string|false with with the name to use in Nextcloud or false if unsuccessful |
|
| 831 | - */ |
|
| 832 | - private function createAltInternalOwnCloudName($name, $isUser) { |
|
| 833 | - $originalTTL = $this->connection->ldapCacheTTL; |
|
| 834 | - $this->connection->setConfiguration(['ldapCacheTTL' => 0]); |
|
| 835 | - if ($isUser) { |
|
| 836 | - $altName = $this->_createAltInternalOwnCloudNameForUsers($name); |
|
| 837 | - } else { |
|
| 838 | - $altName = $this->_createAltInternalOwnCloudNameForGroups($name); |
|
| 839 | - } |
|
| 840 | - $this->connection->setConfiguration(['ldapCacheTTL' => $originalTTL]); |
|
| 841 | - |
|
| 842 | - return $altName; |
|
| 843 | - } |
|
| 844 | - |
|
| 845 | - /** |
|
| 846 | - * fetches a list of users according to a provided loginName and utilizing |
|
| 847 | - * the login filter. |
|
| 848 | - */ |
|
| 849 | - public function fetchUsersByLoginName(string $loginName, array $attributes = ['dn']): array { |
|
| 850 | - $loginName = $this->escapeFilterPart($loginName); |
|
| 851 | - $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); |
|
| 852 | - return $this->fetchListOfUsers($filter, $attributes); |
|
| 853 | - } |
|
| 854 | - |
|
| 855 | - /** |
|
| 856 | - * counts the number of users according to a provided loginName and |
|
| 857 | - * utilizing the login filter. |
|
| 858 | - * |
|
| 859 | - * @param string $loginName |
|
| 860 | - * @return int |
|
| 861 | - */ |
|
| 862 | - public function countUsersByLoginName($loginName) { |
|
| 863 | - $loginName = $this->escapeFilterPart($loginName); |
|
| 864 | - $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); |
|
| 865 | - return $this->countUsers($filter); |
|
| 866 | - } |
|
| 867 | - |
|
| 868 | - /** |
|
| 869 | - * @throws \Exception |
|
| 870 | - */ |
|
| 871 | - public function fetchListOfUsers(string $filter, array $attr, int $limit = null, int $offset = null, bool $forceApplyAttributes = false): array { |
|
| 872 | - $ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset); |
|
| 873 | - $recordsToUpdate = $ldapRecords; |
|
| 874 | - if (!$forceApplyAttributes) { |
|
| 875 | - $isBackgroundJobModeAjax = $this->config |
|
| 876 | - ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; |
|
| 877 | - $listOfDNs = array_reduce($ldapRecords, function ($listOfDNs, $entry) { |
|
| 878 | - $listOfDNs[] = $entry['dn'][0]; |
|
| 879 | - return $listOfDNs; |
|
| 880 | - }, []); |
|
| 881 | - $idsByDn = $this->userMapper->getListOfIdsByDn($listOfDNs); |
|
| 882 | - $recordsToUpdate = array_filter($ldapRecords, function ($record) use ($isBackgroundJobModeAjax, $idsByDn) { |
|
| 883 | - $newlyMapped = false; |
|
| 884 | - $uid = $idsByDn[$record['dn'][0]] ?? null; |
|
| 885 | - if ($uid === null) { |
|
| 886 | - $uid = $this->dn2ocname($record['dn'][0], null, true, $newlyMapped, $record); |
|
| 887 | - } |
|
| 888 | - if (is_string($uid)) { |
|
| 889 | - $this->cacheUserExists($uid); |
|
| 890 | - } |
|
| 891 | - return ($uid !== false) && ($newlyMapped || $isBackgroundJobModeAjax); |
|
| 892 | - }); |
|
| 893 | - } |
|
| 894 | - $this->batchApplyUserAttributes($recordsToUpdate); |
|
| 895 | - return $this->fetchList($ldapRecords, $this->manyAttributes($attr)); |
|
| 896 | - } |
|
| 897 | - |
|
| 898 | - /** |
|
| 899 | - * provided with an array of LDAP user records the method will fetch the |
|
| 900 | - * user object and requests it to process the freshly fetched attributes and |
|
| 901 | - * and their values |
|
| 902 | - * |
|
| 903 | - * @param array $ldapRecords |
|
| 904 | - * @throws \Exception |
|
| 905 | - */ |
|
| 906 | - public function batchApplyUserAttributes(array $ldapRecords) { |
|
| 907 | - $displayNameAttribute = strtolower($this->connection->ldapUserDisplayName); |
|
| 908 | - foreach ($ldapRecords as $userRecord) { |
|
| 909 | - if (!isset($userRecord[$displayNameAttribute])) { |
|
| 910 | - // displayName is obligatory |
|
| 911 | - continue; |
|
| 912 | - } |
|
| 913 | - $ocName = $this->dn2ocname($userRecord['dn'][0], null, true); |
|
| 914 | - if ($ocName === false) { |
|
| 915 | - continue; |
|
| 916 | - } |
|
| 917 | - $this->updateUserState($ocName); |
|
| 918 | - $user = $this->userManager->get($ocName); |
|
| 919 | - if ($user !== null) { |
|
| 920 | - $user->processAttributes($userRecord); |
|
| 921 | - } else { |
|
| 922 | - \OC::$server->getLogger()->debug( |
|
| 923 | - "The ldap user manager returned null for $ocName", |
|
| 924 | - ['app' => 'user_ldap'] |
|
| 925 | - ); |
|
| 926 | - } |
|
| 927 | - } |
|
| 928 | - } |
|
| 929 | - |
|
| 930 | - /** |
|
| 931 | - * @param string $filter |
|
| 932 | - * @param string|string[] $attr |
|
| 933 | - * @param int $limit |
|
| 934 | - * @param int $offset |
|
| 935 | - * @return array |
|
| 936 | - */ |
|
| 937 | - public function fetchListOfGroups($filter, $attr, $limit = null, $offset = null) { |
|
| 938 | - $groupRecords = $this->searchGroups($filter, $attr, $limit, $offset); |
|
| 939 | - |
|
| 940 | - $listOfDNs = array_reduce($groupRecords, function ($listOfDNs, $entry) { |
|
| 941 | - $listOfDNs[] = $entry['dn'][0]; |
|
| 942 | - return $listOfDNs; |
|
| 943 | - }, []); |
|
| 944 | - $idsByDn = $this->groupMapper->getListOfIdsByDn($listOfDNs); |
|
| 945 | - |
|
| 946 | - array_walk($groupRecords, function ($record) use ($idsByDn) { |
|
| 947 | - $newlyMapped = false; |
|
| 948 | - $gid = $idsByDn[$record['dn'][0]] ?? null; |
|
| 949 | - if ($gid === null) { |
|
| 950 | - $gid = $this->dn2ocname($record['dn'][0], null, false, $newlyMapped, $record); |
|
| 951 | - } |
|
| 952 | - if (!$newlyMapped && is_string($gid)) { |
|
| 953 | - $this->cacheGroupExists($gid); |
|
| 954 | - } |
|
| 955 | - }); |
|
| 956 | - return $this->fetchList($groupRecords, $this->manyAttributes($attr)); |
|
| 957 | - } |
|
| 958 | - |
|
| 959 | - /** |
|
| 960 | - * @param array $list |
|
| 961 | - * @param bool $manyAttributes |
|
| 962 | - * @return array |
|
| 963 | - */ |
|
| 964 | - private function fetchList($list, $manyAttributes) { |
|
| 965 | - if (is_array($list)) { |
|
| 966 | - if ($manyAttributes) { |
|
| 967 | - return $list; |
|
| 968 | - } else { |
|
| 969 | - $list = array_reduce($list, function ($carry, $item) { |
|
| 970 | - $attribute = array_keys($item)[0]; |
|
| 971 | - $carry[] = $item[$attribute][0]; |
|
| 972 | - return $carry; |
|
| 973 | - }, []); |
|
| 974 | - return array_unique($list, SORT_LOCALE_STRING); |
|
| 975 | - } |
|
| 976 | - } |
|
| 977 | - |
|
| 978 | - //error cause actually, maybe throw an exception in future. |
|
| 979 | - return []; |
|
| 980 | - } |
|
| 981 | - |
|
| 982 | - /** |
|
| 983 | - * @throws ServerNotAvailableException |
|
| 984 | - */ |
|
| 985 | - public function searchUsers(string $filter, array $attr = null, int $limit = null, int $offset = null): array { |
|
| 986 | - $result = []; |
|
| 987 | - foreach ($this->connection->ldapBaseUsers as $base) { |
|
| 988 | - $result = array_merge($result, $this->search($filter, $base, $attr, $limit, $offset)); |
|
| 989 | - } |
|
| 990 | - return $result; |
|
| 991 | - } |
|
| 992 | - |
|
| 993 | - /** |
|
| 994 | - * @param string $filter |
|
| 995 | - * @param string|string[] $attr |
|
| 996 | - * @param int $limit |
|
| 997 | - * @param int $offset |
|
| 998 | - * @return false|int |
|
| 999 | - * @throws ServerNotAvailableException |
|
| 1000 | - */ |
|
| 1001 | - public function countUsers($filter, $attr = ['dn'], $limit = null, $offset = null) { |
|
| 1002 | - $result = false; |
|
| 1003 | - foreach ($this->connection->ldapBaseUsers as $base) { |
|
| 1004 | - $count = $this->count($filter, [$base], $attr, $limit, $offset); |
|
| 1005 | - $result = is_int($count) ? (int)$result + $count : $result; |
|
| 1006 | - } |
|
| 1007 | - return $result; |
|
| 1008 | - } |
|
| 1009 | - |
|
| 1010 | - /** |
|
| 1011 | - * executes an LDAP search, optimized for Groups |
|
| 1012 | - * |
|
| 1013 | - * @param string $filter the LDAP filter for the search |
|
| 1014 | - * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
|
| 1015 | - * @param integer $limit |
|
| 1016 | - * @param integer $offset |
|
| 1017 | - * @return array with the search result |
|
| 1018 | - * |
|
| 1019 | - * Executes an LDAP search |
|
| 1020 | - * @throws ServerNotAvailableException |
|
| 1021 | - */ |
|
| 1022 | - public function searchGroups($filter, $attr = null, $limit = null, $offset = null) { |
|
| 1023 | - $result = []; |
|
| 1024 | - foreach ($this->connection->ldapBaseGroups as $base) { |
|
| 1025 | - $result = array_merge($result, $this->search($filter, $base, $attr, $limit, $offset)); |
|
| 1026 | - } |
|
| 1027 | - return $result; |
|
| 1028 | - } |
|
| 1029 | - |
|
| 1030 | - /** |
|
| 1031 | - * returns the number of available groups |
|
| 1032 | - * |
|
| 1033 | - * @param string $filter the LDAP search filter |
|
| 1034 | - * @param string[] $attr optional |
|
| 1035 | - * @param int|null $limit |
|
| 1036 | - * @param int|null $offset |
|
| 1037 | - * @return int|bool |
|
| 1038 | - * @throws ServerNotAvailableException |
|
| 1039 | - */ |
|
| 1040 | - public function countGroups($filter, $attr = ['dn'], $limit = null, $offset = null) { |
|
| 1041 | - $result = false; |
|
| 1042 | - foreach ($this->connection->ldapBaseGroups as $base) { |
|
| 1043 | - $count = $this->count($filter, [$base], $attr, $limit, $offset); |
|
| 1044 | - $result = is_int($count) ? (int)$result + $count : $result; |
|
| 1045 | - } |
|
| 1046 | - return $result; |
|
| 1047 | - } |
|
| 1048 | - |
|
| 1049 | - /** |
|
| 1050 | - * returns the number of available objects on the base DN |
|
| 1051 | - * |
|
| 1052 | - * @param int|null $limit |
|
| 1053 | - * @param int|null $offset |
|
| 1054 | - * @return int|bool |
|
| 1055 | - * @throws ServerNotAvailableException |
|
| 1056 | - */ |
|
| 1057 | - public function countObjects($limit = null, $offset = null) { |
|
| 1058 | - $result = false; |
|
| 1059 | - foreach ($this->connection->ldapBase as $base) { |
|
| 1060 | - $count = $this->count('objectclass=*', [$base], ['dn'], $limit, $offset); |
|
| 1061 | - $result = is_int($count) ? (int)$result + $count : $result; |
|
| 1062 | - } |
|
| 1063 | - return $result; |
|
| 1064 | - } |
|
| 1065 | - |
|
| 1066 | - /** |
|
| 1067 | - * Returns the LDAP handler |
|
| 1068 | - * |
|
| 1069 | - * @throws \OC\ServerNotAvailableException |
|
| 1070 | - */ |
|
| 1071 | - |
|
| 1072 | - /** |
|
| 1073 | - * @return mixed |
|
| 1074 | - * @throws \OC\ServerNotAvailableException |
|
| 1075 | - */ |
|
| 1076 | - private function invokeLDAPMethod() { |
|
| 1077 | - $arguments = func_get_args(); |
|
| 1078 | - $command = array_shift($arguments); |
|
| 1079 | - $cr = array_shift($arguments); |
|
| 1080 | - if (!method_exists($this->ldap, $command)) { |
|
| 1081 | - return null; |
|
| 1082 | - } |
|
| 1083 | - array_unshift($arguments, $cr); |
|
| 1084 | - // php no longer supports call-time pass-by-reference |
|
| 1085 | - // thus cannot support controlPagedResultResponse as the third argument |
|
| 1086 | - // is a reference |
|
| 1087 | - $doMethod = function () use ($command, &$arguments) { |
|
| 1088 | - if ($command == 'controlPagedResultResponse') { |
|
| 1089 | - throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.'); |
|
| 1090 | - } else { |
|
| 1091 | - return call_user_func_array([$this->ldap, $command], $arguments); |
|
| 1092 | - } |
|
| 1093 | - }; |
|
| 1094 | - try { |
|
| 1095 | - $ret = $doMethod(); |
|
| 1096 | - } catch (ServerNotAvailableException $e) { |
|
| 1097 | - /* Server connection lost, attempt to reestablish it |
|
| 69 | + public const UUID_ATTRIBUTES = ['entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid']; |
|
| 70 | + |
|
| 71 | + /** @var \OCA\User_LDAP\Connection */ |
|
| 72 | + public $connection; |
|
| 73 | + /** @var Manager */ |
|
| 74 | + public $userManager; |
|
| 75 | + //never ever check this var directly, always use getPagedSearchResultState |
|
| 76 | + protected $pagedSearchedSuccessful; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @var UserMapping $userMapper |
|
| 80 | + */ |
|
| 81 | + protected $userMapper; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @var AbstractMapping $userMapper |
|
| 85 | + */ |
|
| 86 | + protected $groupMapper; |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @var \OCA\User_LDAP\Helper |
|
| 90 | + */ |
|
| 91 | + private $helper; |
|
| 92 | + /** @var IConfig */ |
|
| 93 | + private $config; |
|
| 94 | + /** @var IUserManager */ |
|
| 95 | + private $ncUserManager; |
|
| 96 | + /** @var string */ |
|
| 97 | + private $lastCookie = ''; |
|
| 98 | + |
|
| 99 | + public function __construct( |
|
| 100 | + Connection $connection, |
|
| 101 | + ILDAPWrapper $ldap, |
|
| 102 | + Manager $userManager, |
|
| 103 | + Helper $helper, |
|
| 104 | + IConfig $config, |
|
| 105 | + IUserManager $ncUserManager |
|
| 106 | + ) { |
|
| 107 | + parent::__construct($ldap); |
|
| 108 | + $this->connection = $connection; |
|
| 109 | + $this->userManager = $userManager; |
|
| 110 | + $this->userManager->setLdapAccess($this); |
|
| 111 | + $this->helper = $helper; |
|
| 112 | + $this->config = $config; |
|
| 113 | + $this->ncUserManager = $ncUserManager; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * sets the User Mapper |
|
| 118 | + * |
|
| 119 | + * @param AbstractMapping $mapper |
|
| 120 | + */ |
|
| 121 | + public function setUserMapper(AbstractMapping $mapper) { |
|
| 122 | + $this->userMapper = $mapper; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @throws \Exception |
|
| 127 | + */ |
|
| 128 | + public function getUserMapper(): UserMapping { |
|
| 129 | + if (is_null($this->userMapper)) { |
|
| 130 | + throw new \Exception('UserMapper was not assigned to this Access instance.'); |
|
| 131 | + } |
|
| 132 | + return $this->userMapper; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * sets the Group Mapper |
|
| 137 | + * |
|
| 138 | + * @param AbstractMapping $mapper |
|
| 139 | + */ |
|
| 140 | + public function setGroupMapper(AbstractMapping $mapper) { |
|
| 141 | + $this->groupMapper = $mapper; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * returns the Group Mapper |
|
| 146 | + * |
|
| 147 | + * @return AbstractMapping |
|
| 148 | + * @throws \Exception |
|
| 149 | + */ |
|
| 150 | + public function getGroupMapper() { |
|
| 151 | + if (is_null($this->groupMapper)) { |
|
| 152 | + throw new \Exception('GroupMapper was not assigned to this Access instance.'); |
|
| 153 | + } |
|
| 154 | + return $this->groupMapper; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * @return bool |
|
| 159 | + */ |
|
| 160 | + private function checkConnection() { |
|
| 161 | + return ($this->connection instanceof Connection); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * returns the Connection instance |
|
| 166 | + * |
|
| 167 | + * @return \OCA\User_LDAP\Connection |
|
| 168 | + */ |
|
| 169 | + public function getConnection() { |
|
| 170 | + return $this->connection; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * reads a given attribute for an LDAP record identified by a DN |
|
| 175 | + * |
|
| 176 | + * @param string $dn the record in question |
|
| 177 | + * @param string $attr the attribute that shall be retrieved |
|
| 178 | + * if empty, just check the record's existence |
|
| 179 | + * @param string $filter |
|
| 180 | + * @return array|false an array of values on success or an empty |
|
| 181 | + * array if $attr is empty, false otherwise |
|
| 182 | + * @throws ServerNotAvailableException |
|
| 183 | + */ |
|
| 184 | + public function readAttribute($dn, $attr, $filter = 'objectClass=*') { |
|
| 185 | + if (!$this->checkConnection()) { |
|
| 186 | + \OCP\Util::writeLog('user_ldap', |
|
| 187 | + 'No LDAP Connector assigned, access impossible for readAttribute.', |
|
| 188 | + ILogger::WARN); |
|
| 189 | + return false; |
|
| 190 | + } |
|
| 191 | + $cr = $this->connection->getConnectionResource(); |
|
| 192 | + if (!$this->ldap->isResource($cr)) { |
|
| 193 | + //LDAP not available |
|
| 194 | + \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', ILogger::DEBUG); |
|
| 195 | + return false; |
|
| 196 | + } |
|
| 197 | + //Cancel possibly running Paged Results operation, otherwise we run in |
|
| 198 | + //LDAP protocol errors |
|
| 199 | + $this->abandonPagedSearch(); |
|
| 200 | + // openLDAP requires that we init a new Paged Search. Not needed by AD, |
|
| 201 | + // but does not hurt either. |
|
| 202 | + $pagingSize = (int)$this->connection->ldapPagingSize; |
|
| 203 | + // 0 won't result in replies, small numbers may leave out groups |
|
| 204 | + // (cf. #12306), 500 is default for paging and should work everywhere. |
|
| 205 | + $maxResults = $pagingSize > 20 ? $pagingSize : 500; |
|
| 206 | + $attr = mb_strtolower($attr, 'UTF-8'); |
|
| 207 | + // the actual read attribute later may contain parameters on a ranged |
|
| 208 | + // request, e.g. member;range=99-199. Depends on server reply. |
|
| 209 | + $attrToRead = $attr; |
|
| 210 | + |
|
| 211 | + $values = []; |
|
| 212 | + $isRangeRequest = false; |
|
| 213 | + do { |
|
| 214 | + $result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults); |
|
| 215 | + if (is_bool($result)) { |
|
| 216 | + // when an exists request was run and it was successful, an empty |
|
| 217 | + // array must be returned |
|
| 218 | + return $result ? [] : false; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + if (!$isRangeRequest) { |
|
| 222 | + $values = $this->extractAttributeValuesFromResult($result, $attr); |
|
| 223 | + if (!empty($values)) { |
|
| 224 | + return $values; |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + $isRangeRequest = false; |
|
| 229 | + $result = $this->extractRangeData($result, $attr); |
|
| 230 | + if (!empty($result)) { |
|
| 231 | + $normalizedResult = $this->extractAttributeValuesFromResult( |
|
| 232 | + [$attr => $result['values']], |
|
| 233 | + $attr |
|
| 234 | + ); |
|
| 235 | + $values = array_merge($values, $normalizedResult); |
|
| 236 | + |
|
| 237 | + if ($result['rangeHigh'] === '*') { |
|
| 238 | + // when server replies with * as high range value, there are |
|
| 239 | + // no more results left |
|
| 240 | + return $values; |
|
| 241 | + } else { |
|
| 242 | + $low = $result['rangeHigh'] + 1; |
|
| 243 | + $attrToRead = $result['attributeName'] . ';range=' . $low . '-*'; |
|
| 244 | + $isRangeRequest = true; |
|
| 245 | + } |
|
| 246 | + } |
|
| 247 | + } while ($isRangeRequest); |
|
| 248 | + |
|
| 249 | + \OCP\Util::writeLog('user_ldap', 'Requested attribute ' . $attr . ' not found for ' . $dn, ILogger::DEBUG); |
|
| 250 | + return false; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Runs an read operation against LDAP |
|
| 255 | + * |
|
| 256 | + * @param resource $cr the LDAP connection |
|
| 257 | + * @param string $dn |
|
| 258 | + * @param string $attribute |
|
| 259 | + * @param string $filter |
|
| 260 | + * @param int $maxResults |
|
| 261 | + * @return array|bool false if there was any error, true if an exists check |
|
| 262 | + * was performed and the requested DN found, array with the |
|
| 263 | + * returned data on a successful usual operation |
|
| 264 | + * @throws ServerNotAvailableException |
|
| 265 | + */ |
|
| 266 | + public function executeRead($cr, $dn, $attribute, $filter, $maxResults) { |
|
| 267 | + $this->initPagedSearch($filter, $dn, [$attribute], $maxResults, 0); |
|
| 268 | + $dn = $this->helper->DNasBaseParameter($dn); |
|
| 269 | + $rr = @$this->invokeLDAPMethod('read', $cr, $dn, $filter, [$attribute]); |
|
| 270 | + if (!$this->ldap->isResource($rr)) { |
|
| 271 | + if ($attribute !== '') { |
|
| 272 | + //do not throw this message on userExists check, irritates |
|
| 273 | + \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, ILogger::DEBUG); |
|
| 274 | + } |
|
| 275 | + //in case an error occurs , e.g. object does not exist |
|
| 276 | + return false; |
|
| 277 | + } |
|
| 278 | + if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) { |
|
| 279 | + \OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', ILogger::DEBUG); |
|
| 280 | + return true; |
|
| 281 | + } |
|
| 282 | + $er = $this->invokeLDAPMethod('firstEntry', $cr, $rr); |
|
| 283 | + if (!$this->ldap->isResource($er)) { |
|
| 284 | + //did not match the filter, return false |
|
| 285 | + return false; |
|
| 286 | + } |
|
| 287 | + //LDAP attributes are not case sensitive |
|
| 288 | + $result = \OCP\Util::mb_array_change_key_case( |
|
| 289 | + $this->invokeLDAPMethod('getAttributes', $cr, $er), MB_CASE_LOWER, 'UTF-8'); |
|
| 290 | + |
|
| 291 | + return $result; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Normalizes a result grom getAttributes(), i.e. handles DNs and binary |
|
| 296 | + * data if present. |
|
| 297 | + * |
|
| 298 | + * @param array $result from ILDAPWrapper::getAttributes() |
|
| 299 | + * @param string $attribute the attribute name that was read |
|
| 300 | + * @return string[] |
|
| 301 | + */ |
|
| 302 | + public function extractAttributeValuesFromResult($result, $attribute) { |
|
| 303 | + $values = []; |
|
| 304 | + if (isset($result[$attribute]) && $result[$attribute]['count'] > 0) { |
|
| 305 | + $lowercaseAttribute = strtolower($attribute); |
|
| 306 | + for ($i = 0; $i < $result[$attribute]['count']; $i++) { |
|
| 307 | + if ($this->resemblesDN($attribute)) { |
|
| 308 | + $values[] = $this->helper->sanitizeDN($result[$attribute][$i]); |
|
| 309 | + } elseif ($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { |
|
| 310 | + $values[] = $this->convertObjectGUID2Str($result[$attribute][$i]); |
|
| 311 | + } else { |
|
| 312 | + $values[] = $result[$attribute][$i]; |
|
| 313 | + } |
|
| 314 | + } |
|
| 315 | + } |
|
| 316 | + return $values; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Attempts to find ranged data in a getAttribute results and extracts the |
|
| 321 | + * returned values as well as information on the range and full attribute |
|
| 322 | + * name for further processing. |
|
| 323 | + * |
|
| 324 | + * @param array $result from ILDAPWrapper::getAttributes() |
|
| 325 | + * @param string $attribute the attribute name that was read. Without ";range=…" |
|
| 326 | + * @return array If a range was detected with keys 'values', 'attributeName', |
|
| 327 | + * 'attributeFull' and 'rangeHigh', otherwise empty. |
|
| 328 | + */ |
|
| 329 | + public function extractRangeData($result, $attribute) { |
|
| 330 | + $keys = array_keys($result); |
|
| 331 | + foreach ($keys as $key) { |
|
| 332 | + if ($key !== $attribute && strpos($key, $attribute) === 0) { |
|
| 333 | + $queryData = explode(';', $key); |
|
| 334 | + if (strpos($queryData[1], 'range=') === 0) { |
|
| 335 | + $high = substr($queryData[1], 1 + strpos($queryData[1], '-')); |
|
| 336 | + $data = [ |
|
| 337 | + 'values' => $result[$key], |
|
| 338 | + 'attributeName' => $queryData[0], |
|
| 339 | + 'attributeFull' => $key, |
|
| 340 | + 'rangeHigh' => $high, |
|
| 341 | + ]; |
|
| 342 | + return $data; |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + } |
|
| 346 | + return []; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * Set password for an LDAP user identified by a DN |
|
| 351 | + * |
|
| 352 | + * @param string $userDN the user in question |
|
| 353 | + * @param string $password the new password |
|
| 354 | + * @return bool |
|
| 355 | + * @throws HintException |
|
| 356 | + * @throws \Exception |
|
| 357 | + */ |
|
| 358 | + public function setPassword($userDN, $password) { |
|
| 359 | + if ((int)$this->connection->turnOnPasswordChange !== 1) { |
|
| 360 | + throw new \Exception('LDAP password changes are disabled.'); |
|
| 361 | + } |
|
| 362 | + $cr = $this->connection->getConnectionResource(); |
|
| 363 | + if (!$this->ldap->isResource($cr)) { |
|
| 364 | + //LDAP not available |
|
| 365 | + \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', ILogger::DEBUG); |
|
| 366 | + return false; |
|
| 367 | + } |
|
| 368 | + try { |
|
| 369 | + // try PASSWD extended operation first |
|
| 370 | + return @$this->invokeLDAPMethod('exopPasswd', $cr, $userDN, '', $password) || |
|
| 371 | + @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password); |
|
| 372 | + } catch (ConstraintViolationException $e) { |
|
| 373 | + throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ') . $e->getMessage(), $e->getCode()); |
|
| 374 | + } |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * checks whether the given attributes value is probably a DN |
|
| 379 | + * |
|
| 380 | + * @param string $attr the attribute in question |
|
| 381 | + * @return boolean if so true, otherwise false |
|
| 382 | + */ |
|
| 383 | + private function resemblesDN($attr) { |
|
| 384 | + $resemblingAttributes = [ |
|
| 385 | + 'dn', |
|
| 386 | + 'uniquemember', |
|
| 387 | + 'member', |
|
| 388 | + // memberOf is an "operational" attribute, without a definition in any RFC |
|
| 389 | + 'memberof' |
|
| 390 | + ]; |
|
| 391 | + return in_array($attr, $resemblingAttributes); |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + /** |
|
| 395 | + * checks whether the given string is probably a DN |
|
| 396 | + * |
|
| 397 | + * @param string $string |
|
| 398 | + * @return boolean |
|
| 399 | + */ |
|
| 400 | + public function stringResemblesDN($string) { |
|
| 401 | + $r = $this->ldap->explodeDN($string, 0); |
|
| 402 | + // if exploding a DN succeeds and does not end up in |
|
| 403 | + // an empty array except for $r[count] being 0. |
|
| 404 | + return (is_array($r) && count($r) > 1); |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * returns a DN-string that is cleaned from not domain parts, e.g. |
|
| 409 | + * cn=foo,cn=bar,dc=foobar,dc=server,dc=org |
|
| 410 | + * becomes dc=foobar,dc=server,dc=org |
|
| 411 | + * |
|
| 412 | + * @param string $dn |
|
| 413 | + * @return string |
|
| 414 | + */ |
|
| 415 | + public function getDomainDNFromDN($dn) { |
|
| 416 | + $allParts = $this->ldap->explodeDN($dn, 0); |
|
| 417 | + if ($allParts === false) { |
|
| 418 | + //not a valid DN |
|
| 419 | + return ''; |
|
| 420 | + } |
|
| 421 | + $domainParts = []; |
|
| 422 | + $dcFound = false; |
|
| 423 | + foreach ($allParts as $part) { |
|
| 424 | + if (!$dcFound && strpos($part, 'dc=') === 0) { |
|
| 425 | + $dcFound = true; |
|
| 426 | + } |
|
| 427 | + if ($dcFound) { |
|
| 428 | + $domainParts[] = $part; |
|
| 429 | + } |
|
| 430 | + } |
|
| 431 | + return implode(',', $domainParts); |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * returns the LDAP DN for the given internal Nextcloud name of the group |
|
| 436 | + * |
|
| 437 | + * @param string $name the Nextcloud name in question |
|
| 438 | + * @return string|false LDAP DN on success, otherwise false |
|
| 439 | + */ |
|
| 440 | + public function groupname2dn($name) { |
|
| 441 | + return $this->groupMapper->getDNByName($name); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * returns the LDAP DN for the given internal Nextcloud name of the user |
|
| 446 | + * |
|
| 447 | + * @param string $name the Nextcloud name in question |
|
| 448 | + * @return string|false with the LDAP DN on success, otherwise false |
|
| 449 | + */ |
|
| 450 | + public function username2dn($name) { |
|
| 451 | + $fdn = $this->userMapper->getDNByName($name); |
|
| 452 | + |
|
| 453 | + //Check whether the DN belongs to the Base, to avoid issues on multi- |
|
| 454 | + //server setups |
|
| 455 | + if (is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 456 | + return $fdn; |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + return false; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * returns the internal Nextcloud name for the given LDAP DN of the group, false on DN outside of search DN or failure |
|
| 464 | + * |
|
| 465 | + * @param string $fdn the dn of the group object |
|
| 466 | + * @param string $ldapName optional, the display name of the object |
|
| 467 | + * @return string|false with the name to use in Nextcloud, false on DN outside of search DN |
|
| 468 | + * @throws \Exception |
|
| 469 | + */ |
|
| 470 | + public function dn2groupname($fdn, $ldapName = null) { |
|
| 471 | + //To avoid bypassing the base DN settings under certain circumstances |
|
| 472 | + //with the group support, check whether the provided DN matches one of |
|
| 473 | + //the given Bases |
|
| 474 | + if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { |
|
| 475 | + return false; |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + return $this->dn2ocname($fdn, $ldapName, false); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + /** |
|
| 482 | + * returns the internal Nextcloud name for the given LDAP DN of the user, false on DN outside of search DN or failure |
|
| 483 | + * |
|
| 484 | + * @param string $dn the dn of the user object |
|
| 485 | + * @param string $ldapName optional, the display name of the object |
|
| 486 | + * @return string|false with with the name to use in Nextcloud |
|
| 487 | + * @throws \Exception |
|
| 488 | + */ |
|
| 489 | + public function dn2username($fdn, $ldapName = null) { |
|
| 490 | + //To avoid bypassing the base DN settings under certain circumstances |
|
| 491 | + //with the group support, check whether the provided DN matches one of |
|
| 492 | + //the given Bases |
|
| 493 | + if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 494 | + return false; |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + return $this->dn2ocname($fdn, $ldapName, true); |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + /** |
|
| 501 | + * returns an internal Nextcloud name for the given LDAP DN, false on DN outside of search DN |
|
| 502 | + * |
|
| 503 | + * @param string $fdn the dn of the user object |
|
| 504 | + * @param string|null $ldapName optional, the display name of the object |
|
| 505 | + * @param bool $isUser optional, whether it is a user object (otherwise group assumed) |
|
| 506 | + * @param bool|null $newlyMapped |
|
| 507 | + * @param array|null $record |
|
| 508 | + * @return false|string with with the name to use in Nextcloud |
|
| 509 | + * @throws \Exception |
|
| 510 | + */ |
|
| 511 | + public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped = null, array $record = null) { |
|
| 512 | + $newlyMapped = false; |
|
| 513 | + if ($isUser) { |
|
| 514 | + $mapper = $this->getUserMapper(); |
|
| 515 | + $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 516 | + $filter = $this->connection->ldapUserFilter; |
|
| 517 | + } else { |
|
| 518 | + $mapper = $this->getGroupMapper(); |
|
| 519 | + $nameAttribute = $this->connection->ldapGroupDisplayName; |
|
| 520 | + $filter = $this->connection->ldapGroupFilter; |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + //let's try to retrieve the Nextcloud name from the mappings table |
|
| 524 | + $ncName = $mapper->getNameByDN($fdn); |
|
| 525 | + if (is_string($ncName)) { |
|
| 526 | + return $ncName; |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + //second try: get the UUID and check if it is known. Then, update the DN and return the name. |
|
| 530 | + $uuid = $this->getUUID($fdn, $isUser, $record); |
|
| 531 | + if (is_string($uuid)) { |
|
| 532 | + $ncName = $mapper->getNameByUUID($uuid); |
|
| 533 | + if (is_string($ncName)) { |
|
| 534 | + $mapper->setDNbyUUID($fdn, $uuid); |
|
| 535 | + return $ncName; |
|
| 536 | + } |
|
| 537 | + } else { |
|
| 538 | + //If the UUID can't be detected something is foul. |
|
| 539 | + \OCP\Util::writeLog('user_ldap', 'Cannot determine UUID for ' . $fdn . '. Skipping.', ILogger::INFO); |
|
| 540 | + return false; |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + if (is_null($ldapName)) { |
|
| 544 | + $ldapName = $this->readAttribute($fdn, $nameAttribute, $filter); |
|
| 545 | + if (!isset($ldapName[0]) && empty($ldapName[0])) { |
|
| 546 | + \OCP\Util::writeLog('user_ldap', 'No or empty name for ' . $fdn . ' with filter ' . $filter . '.', ILogger::DEBUG); |
|
| 547 | + return false; |
|
| 548 | + } |
|
| 549 | + $ldapName = $ldapName[0]; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + if ($isUser) { |
|
| 553 | + $usernameAttribute = (string)$this->connection->ldapExpertUsernameAttr; |
|
| 554 | + if ($usernameAttribute !== '') { |
|
| 555 | + $username = $this->readAttribute($fdn, $usernameAttribute); |
|
| 556 | + $username = $username[0]; |
|
| 557 | + } else { |
|
| 558 | + $username = $uuid; |
|
| 559 | + } |
|
| 560 | + try { |
|
| 561 | + $intName = $this->sanitizeUsername($username); |
|
| 562 | + } catch (\InvalidArgumentException $e) { |
|
| 563 | + \OC::$server->getLogger()->logException($e, [ |
|
| 564 | + 'app' => 'user_ldap', |
|
| 565 | + 'level' => ILogger::WARN, |
|
| 566 | + ]); |
|
| 567 | + // we don't attempt to set a username here. We can go for |
|
| 568 | + // for an alternative 4 digit random number as we would append |
|
| 569 | + // otherwise, however it's likely not enough space in bigger |
|
| 570 | + // setups, and most importantly: this is not intended. |
|
| 571 | + return false; |
|
| 572 | + } |
|
| 573 | + } else { |
|
| 574 | + $intName = $ldapName; |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + //a new user/group! Add it only if it doesn't conflict with other backend's users or existing groups |
|
| 578 | + //disabling Cache is required to avoid that the new user is cached as not-existing in fooExists check |
|
| 579 | + //NOTE: mind, disabling cache affects only this instance! Using it |
|
| 580 | + // outside of core user management will still cache the user as non-existing. |
|
| 581 | + $originalTTL = $this->connection->ldapCacheTTL; |
|
| 582 | + $this->connection->setConfiguration(['ldapCacheTTL' => 0]); |
|
| 583 | + if ($intName !== '' |
|
| 584 | + && (($isUser && !$this->ncUserManager->userExists($intName)) |
|
| 585 | + || (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName)) |
|
| 586 | + ) |
|
| 587 | + ) { |
|
| 588 | + $this->connection->setConfiguration(['ldapCacheTTL' => $originalTTL]); |
|
| 589 | + $newlyMapped = $this->mapAndAnnounceIfApplicable($mapper, $fdn, $intName, $uuid, $isUser); |
|
| 590 | + if ($newlyMapped) { |
|
| 591 | + return $intName; |
|
| 592 | + } |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + $this->connection->setConfiguration(['ldapCacheTTL' => $originalTTL]); |
|
| 596 | + $altName = $this->createAltInternalOwnCloudName($intName, $isUser); |
|
| 597 | + if (is_string($altName)) { |
|
| 598 | + if ($this->mapAndAnnounceIfApplicable($mapper, $fdn, $altName, $uuid, $isUser)) { |
|
| 599 | + $newlyMapped = true; |
|
| 600 | + return $altName; |
|
| 601 | + } |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + //if everything else did not help.. |
|
| 605 | + \OCP\Util::writeLog('user_ldap', 'Could not create unique name for ' . $fdn . '.', ILogger::INFO); |
|
| 606 | + return false; |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + public function mapAndAnnounceIfApplicable( |
|
| 610 | + AbstractMapping $mapper, |
|
| 611 | + string $fdn, |
|
| 612 | + string $name, |
|
| 613 | + string $uuid, |
|
| 614 | + bool $isUser |
|
| 615 | + ): bool { |
|
| 616 | + if ($mapper->map($fdn, $name, $uuid)) { |
|
| 617 | + if ($this->ncUserManager instanceof PublicEmitter && $isUser) { |
|
| 618 | + $this->cacheUserExists($name); |
|
| 619 | + $this->ncUserManager->emit('\OC\User', 'assignedUserId', [$name]); |
|
| 620 | + } elseif (!$isUser) { |
|
| 621 | + $this->cacheGroupExists($name); |
|
| 622 | + } |
|
| 623 | + return true; |
|
| 624 | + } |
|
| 625 | + return false; |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * gives back the user names as they are used ownClod internally |
|
| 630 | + * |
|
| 631 | + * @param array $ldapUsers as returned by fetchList() |
|
| 632 | + * @return array an array with the user names to use in Nextcloud |
|
| 633 | + * |
|
| 634 | + * gives back the user names as they are used ownClod internally |
|
| 635 | + * @throws \Exception |
|
| 636 | + */ |
|
| 637 | + public function nextcloudUserNames($ldapUsers) { |
|
| 638 | + return $this->ldap2NextcloudNames($ldapUsers, true); |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + /** |
|
| 642 | + * gives back the group names as they are used ownClod internally |
|
| 643 | + * |
|
| 644 | + * @param array $ldapGroups as returned by fetchList() |
|
| 645 | + * @return array an array with the group names to use in Nextcloud |
|
| 646 | + * |
|
| 647 | + * gives back the group names as they are used ownClod internally |
|
| 648 | + * @throws \Exception |
|
| 649 | + */ |
|
| 650 | + public function nextcloudGroupNames($ldapGroups) { |
|
| 651 | + return $this->ldap2NextcloudNames($ldapGroups, false); |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + /** |
|
| 655 | + * @param array $ldapObjects as returned by fetchList() |
|
| 656 | + * @param bool $isUsers |
|
| 657 | + * @return array |
|
| 658 | + * @throws \Exception |
|
| 659 | + */ |
|
| 660 | + private function ldap2NextcloudNames($ldapObjects, $isUsers) { |
|
| 661 | + if ($isUsers) { |
|
| 662 | + $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 663 | + $sndAttribute = $this->connection->ldapUserDisplayName2; |
|
| 664 | + } else { |
|
| 665 | + $nameAttribute = $this->connection->ldapGroupDisplayName; |
|
| 666 | + } |
|
| 667 | + $nextcloudNames = []; |
|
| 668 | + |
|
| 669 | + foreach ($ldapObjects as $ldapObject) { |
|
| 670 | + $nameByLDAP = null; |
|
| 671 | + if (isset($ldapObject[$nameAttribute]) |
|
| 672 | + && is_array($ldapObject[$nameAttribute]) |
|
| 673 | + && isset($ldapObject[$nameAttribute][0]) |
|
| 674 | + ) { |
|
| 675 | + // might be set, but not necessarily. if so, we use it. |
|
| 676 | + $nameByLDAP = $ldapObject[$nameAttribute][0]; |
|
| 677 | + } |
|
| 678 | + |
|
| 679 | + $ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers); |
|
| 680 | + if ($ncName) { |
|
| 681 | + $nextcloudNames[] = $ncName; |
|
| 682 | + if ($isUsers) { |
|
| 683 | + $this->updateUserState($ncName); |
|
| 684 | + //cache the user names so it does not need to be retrieved |
|
| 685 | + //again later (e.g. sharing dialogue). |
|
| 686 | + if (is_null($nameByLDAP)) { |
|
| 687 | + continue; |
|
| 688 | + } |
|
| 689 | + $sndName = isset($ldapObject[$sndAttribute][0]) |
|
| 690 | + ? $ldapObject[$sndAttribute][0] : ''; |
|
| 691 | + $this->cacheUserDisplayName($ncName, $nameByLDAP, $sndName); |
|
| 692 | + } elseif ($nameByLDAP !== null) { |
|
| 693 | + $this->cacheGroupDisplayName($ncName, $nameByLDAP); |
|
| 694 | + } |
|
| 695 | + } |
|
| 696 | + } |
|
| 697 | + return $nextcloudNames; |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + /** |
|
| 701 | + * removes the deleted-flag of a user if it was set |
|
| 702 | + * |
|
| 703 | + * @param string $ncname |
|
| 704 | + * @throws \Exception |
|
| 705 | + */ |
|
| 706 | + public function updateUserState($ncname) { |
|
| 707 | + $user = $this->userManager->get($ncname); |
|
| 708 | + if ($user instanceof OfflineUser) { |
|
| 709 | + $user->unmark(); |
|
| 710 | + } |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + /** |
|
| 714 | + * caches the user display name |
|
| 715 | + * |
|
| 716 | + * @param string $ocName the internal Nextcloud username |
|
| 717 | + * @param string|false $home the home directory path |
|
| 718 | + */ |
|
| 719 | + public function cacheUserHome($ocName, $home) { |
|
| 720 | + $cacheKey = 'getHome' . $ocName; |
|
| 721 | + $this->connection->writeToCache($cacheKey, $home); |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + /** |
|
| 725 | + * caches a user as existing |
|
| 726 | + * |
|
| 727 | + * @param string $ocName the internal Nextcloud username |
|
| 728 | + */ |
|
| 729 | + public function cacheUserExists($ocName) { |
|
| 730 | + $this->connection->writeToCache('userExists' . $ocName, true); |
|
| 731 | + } |
|
| 732 | + |
|
| 733 | + /** |
|
| 734 | + * caches a group as existing |
|
| 735 | + */ |
|
| 736 | + public function cacheGroupExists(string $gid): void { |
|
| 737 | + $this->connection->writeToCache('groupExists' . $gid, true); |
|
| 738 | + } |
|
| 739 | + |
|
| 740 | + /** |
|
| 741 | + * caches the user display name |
|
| 742 | + * |
|
| 743 | + * @param string $ocName the internal Nextcloud username |
|
| 744 | + * @param string $displayName the display name |
|
| 745 | + * @param string $displayName2 the second display name |
|
| 746 | + * @throws \Exception |
|
| 747 | + */ |
|
| 748 | + public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { |
|
| 749 | + $user = $this->userManager->get($ocName); |
|
| 750 | + if ($user === null) { |
|
| 751 | + return; |
|
| 752 | + } |
|
| 753 | + $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
| 754 | + $cacheKeyTrunk = 'getDisplayName'; |
|
| 755 | + $this->connection->writeToCache($cacheKeyTrunk . $ocName, $displayName); |
|
| 756 | + } |
|
| 757 | + |
|
| 758 | + public function cacheGroupDisplayName(string $ncName, string $displayName): void { |
|
| 759 | + $cacheKey = 'group_getDisplayName' . $ncName; |
|
| 760 | + $this->connection->writeToCache($cacheKey, $displayName); |
|
| 761 | + } |
|
| 762 | + |
|
| 763 | + /** |
|
| 764 | + * creates a unique name for internal Nextcloud use for users. Don't call it directly. |
|
| 765 | + * |
|
| 766 | + * @param string $name the display name of the object |
|
| 767 | + * @return string|false with with the name to use in Nextcloud or false if unsuccessful |
|
| 768 | + * |
|
| 769 | + * Instead of using this method directly, call |
|
| 770 | + * createAltInternalOwnCloudName($name, true) |
|
| 771 | + */ |
|
| 772 | + private function _createAltInternalOwnCloudNameForUsers($name) { |
|
| 773 | + $attempts = 0; |
|
| 774 | + //while loop is just a precaution. If a name is not generated within |
|
| 775 | + //20 attempts, something else is very wrong. Avoids infinite loop. |
|
| 776 | + while ($attempts < 20) { |
|
| 777 | + $altName = $name . '_' . rand(1000, 9999); |
|
| 778 | + if (!$this->ncUserManager->userExists($altName)) { |
|
| 779 | + return $altName; |
|
| 780 | + } |
|
| 781 | + $attempts++; |
|
| 782 | + } |
|
| 783 | + return false; |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + /** |
|
| 787 | + * creates a unique name for internal Nextcloud use for groups. Don't call it directly. |
|
| 788 | + * |
|
| 789 | + * @param string $name the display name of the object |
|
| 790 | + * @return string|false with with the name to use in Nextcloud or false if unsuccessful. |
|
| 791 | + * |
|
| 792 | + * Instead of using this method directly, call |
|
| 793 | + * createAltInternalOwnCloudName($name, false) |
|
| 794 | + * |
|
| 795 | + * Group names are also used as display names, so we do a sequential |
|
| 796 | + * numbering, e.g. Developers_42 when there are 41 other groups called |
|
| 797 | + * "Developers" |
|
| 798 | + */ |
|
| 799 | + private function _createAltInternalOwnCloudNameForGroups($name) { |
|
| 800 | + $usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%'); |
|
| 801 | + if (!$usedNames || count($usedNames) === 0) { |
|
| 802 | + $lastNo = 1; //will become name_2 |
|
| 803 | + } else { |
|
| 804 | + natsort($usedNames); |
|
| 805 | + $lastName = array_pop($usedNames); |
|
| 806 | + $lastNo = (int)substr($lastName, strrpos($lastName, '_') + 1); |
|
| 807 | + } |
|
| 808 | + $altName = $name . '_' . (string)($lastNo + 1); |
|
| 809 | + unset($usedNames); |
|
| 810 | + |
|
| 811 | + $attempts = 1; |
|
| 812 | + while ($attempts < 21) { |
|
| 813 | + // Check to be really sure it is unique |
|
| 814 | + // while loop is just a precaution. If a name is not generated within |
|
| 815 | + // 20 attempts, something else is very wrong. Avoids infinite loop. |
|
| 816 | + if (!\OC::$server->getGroupManager()->groupExists($altName)) { |
|
| 817 | + return $altName; |
|
| 818 | + } |
|
| 819 | + $altName = $name . '_' . ($lastNo + $attempts); |
|
| 820 | + $attempts++; |
|
| 821 | + } |
|
| 822 | + return false; |
|
| 823 | + } |
|
| 824 | + |
|
| 825 | + /** |
|
| 826 | + * creates a unique name for internal Nextcloud use. |
|
| 827 | + * |
|
| 828 | + * @param string $name the display name of the object |
|
| 829 | + * @param boolean $isUser whether name should be created for a user (true) or a group (false) |
|
| 830 | + * @return string|false with with the name to use in Nextcloud or false if unsuccessful |
|
| 831 | + */ |
|
| 832 | + private function createAltInternalOwnCloudName($name, $isUser) { |
|
| 833 | + $originalTTL = $this->connection->ldapCacheTTL; |
|
| 834 | + $this->connection->setConfiguration(['ldapCacheTTL' => 0]); |
|
| 835 | + if ($isUser) { |
|
| 836 | + $altName = $this->_createAltInternalOwnCloudNameForUsers($name); |
|
| 837 | + } else { |
|
| 838 | + $altName = $this->_createAltInternalOwnCloudNameForGroups($name); |
|
| 839 | + } |
|
| 840 | + $this->connection->setConfiguration(['ldapCacheTTL' => $originalTTL]); |
|
| 841 | + |
|
| 842 | + return $altName; |
|
| 843 | + } |
|
| 844 | + |
|
| 845 | + /** |
|
| 846 | + * fetches a list of users according to a provided loginName and utilizing |
|
| 847 | + * the login filter. |
|
| 848 | + */ |
|
| 849 | + public function fetchUsersByLoginName(string $loginName, array $attributes = ['dn']): array { |
|
| 850 | + $loginName = $this->escapeFilterPart($loginName); |
|
| 851 | + $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); |
|
| 852 | + return $this->fetchListOfUsers($filter, $attributes); |
|
| 853 | + } |
|
| 854 | + |
|
| 855 | + /** |
|
| 856 | + * counts the number of users according to a provided loginName and |
|
| 857 | + * utilizing the login filter. |
|
| 858 | + * |
|
| 859 | + * @param string $loginName |
|
| 860 | + * @return int |
|
| 861 | + */ |
|
| 862 | + public function countUsersByLoginName($loginName) { |
|
| 863 | + $loginName = $this->escapeFilterPart($loginName); |
|
| 864 | + $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); |
|
| 865 | + return $this->countUsers($filter); |
|
| 866 | + } |
|
| 867 | + |
|
| 868 | + /** |
|
| 869 | + * @throws \Exception |
|
| 870 | + */ |
|
| 871 | + public function fetchListOfUsers(string $filter, array $attr, int $limit = null, int $offset = null, bool $forceApplyAttributes = false): array { |
|
| 872 | + $ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset); |
|
| 873 | + $recordsToUpdate = $ldapRecords; |
|
| 874 | + if (!$forceApplyAttributes) { |
|
| 875 | + $isBackgroundJobModeAjax = $this->config |
|
| 876 | + ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; |
|
| 877 | + $listOfDNs = array_reduce($ldapRecords, function ($listOfDNs, $entry) { |
|
| 878 | + $listOfDNs[] = $entry['dn'][0]; |
|
| 879 | + return $listOfDNs; |
|
| 880 | + }, []); |
|
| 881 | + $idsByDn = $this->userMapper->getListOfIdsByDn($listOfDNs); |
|
| 882 | + $recordsToUpdate = array_filter($ldapRecords, function ($record) use ($isBackgroundJobModeAjax, $idsByDn) { |
|
| 883 | + $newlyMapped = false; |
|
| 884 | + $uid = $idsByDn[$record['dn'][0]] ?? null; |
|
| 885 | + if ($uid === null) { |
|
| 886 | + $uid = $this->dn2ocname($record['dn'][0], null, true, $newlyMapped, $record); |
|
| 887 | + } |
|
| 888 | + if (is_string($uid)) { |
|
| 889 | + $this->cacheUserExists($uid); |
|
| 890 | + } |
|
| 891 | + return ($uid !== false) && ($newlyMapped || $isBackgroundJobModeAjax); |
|
| 892 | + }); |
|
| 893 | + } |
|
| 894 | + $this->batchApplyUserAttributes($recordsToUpdate); |
|
| 895 | + return $this->fetchList($ldapRecords, $this->manyAttributes($attr)); |
|
| 896 | + } |
|
| 897 | + |
|
| 898 | + /** |
|
| 899 | + * provided with an array of LDAP user records the method will fetch the |
|
| 900 | + * user object and requests it to process the freshly fetched attributes and |
|
| 901 | + * and their values |
|
| 902 | + * |
|
| 903 | + * @param array $ldapRecords |
|
| 904 | + * @throws \Exception |
|
| 905 | + */ |
|
| 906 | + public function batchApplyUserAttributes(array $ldapRecords) { |
|
| 907 | + $displayNameAttribute = strtolower($this->connection->ldapUserDisplayName); |
|
| 908 | + foreach ($ldapRecords as $userRecord) { |
|
| 909 | + if (!isset($userRecord[$displayNameAttribute])) { |
|
| 910 | + // displayName is obligatory |
|
| 911 | + continue; |
|
| 912 | + } |
|
| 913 | + $ocName = $this->dn2ocname($userRecord['dn'][0], null, true); |
|
| 914 | + if ($ocName === false) { |
|
| 915 | + continue; |
|
| 916 | + } |
|
| 917 | + $this->updateUserState($ocName); |
|
| 918 | + $user = $this->userManager->get($ocName); |
|
| 919 | + if ($user !== null) { |
|
| 920 | + $user->processAttributes($userRecord); |
|
| 921 | + } else { |
|
| 922 | + \OC::$server->getLogger()->debug( |
|
| 923 | + "The ldap user manager returned null for $ocName", |
|
| 924 | + ['app' => 'user_ldap'] |
|
| 925 | + ); |
|
| 926 | + } |
|
| 927 | + } |
|
| 928 | + } |
|
| 929 | + |
|
| 930 | + /** |
|
| 931 | + * @param string $filter |
|
| 932 | + * @param string|string[] $attr |
|
| 933 | + * @param int $limit |
|
| 934 | + * @param int $offset |
|
| 935 | + * @return array |
|
| 936 | + */ |
|
| 937 | + public function fetchListOfGroups($filter, $attr, $limit = null, $offset = null) { |
|
| 938 | + $groupRecords = $this->searchGroups($filter, $attr, $limit, $offset); |
|
| 939 | + |
|
| 940 | + $listOfDNs = array_reduce($groupRecords, function ($listOfDNs, $entry) { |
|
| 941 | + $listOfDNs[] = $entry['dn'][0]; |
|
| 942 | + return $listOfDNs; |
|
| 943 | + }, []); |
|
| 944 | + $idsByDn = $this->groupMapper->getListOfIdsByDn($listOfDNs); |
|
| 945 | + |
|
| 946 | + array_walk($groupRecords, function ($record) use ($idsByDn) { |
|
| 947 | + $newlyMapped = false; |
|
| 948 | + $gid = $idsByDn[$record['dn'][0]] ?? null; |
|
| 949 | + if ($gid === null) { |
|
| 950 | + $gid = $this->dn2ocname($record['dn'][0], null, false, $newlyMapped, $record); |
|
| 951 | + } |
|
| 952 | + if (!$newlyMapped && is_string($gid)) { |
|
| 953 | + $this->cacheGroupExists($gid); |
|
| 954 | + } |
|
| 955 | + }); |
|
| 956 | + return $this->fetchList($groupRecords, $this->manyAttributes($attr)); |
|
| 957 | + } |
|
| 958 | + |
|
| 959 | + /** |
|
| 960 | + * @param array $list |
|
| 961 | + * @param bool $manyAttributes |
|
| 962 | + * @return array |
|
| 963 | + */ |
|
| 964 | + private function fetchList($list, $manyAttributes) { |
|
| 965 | + if (is_array($list)) { |
|
| 966 | + if ($manyAttributes) { |
|
| 967 | + return $list; |
|
| 968 | + } else { |
|
| 969 | + $list = array_reduce($list, function ($carry, $item) { |
|
| 970 | + $attribute = array_keys($item)[0]; |
|
| 971 | + $carry[] = $item[$attribute][0]; |
|
| 972 | + return $carry; |
|
| 973 | + }, []); |
|
| 974 | + return array_unique($list, SORT_LOCALE_STRING); |
|
| 975 | + } |
|
| 976 | + } |
|
| 977 | + |
|
| 978 | + //error cause actually, maybe throw an exception in future. |
|
| 979 | + return []; |
|
| 980 | + } |
|
| 981 | + |
|
| 982 | + /** |
|
| 983 | + * @throws ServerNotAvailableException |
|
| 984 | + */ |
|
| 985 | + public function searchUsers(string $filter, array $attr = null, int $limit = null, int $offset = null): array { |
|
| 986 | + $result = []; |
|
| 987 | + foreach ($this->connection->ldapBaseUsers as $base) { |
|
| 988 | + $result = array_merge($result, $this->search($filter, $base, $attr, $limit, $offset)); |
|
| 989 | + } |
|
| 990 | + return $result; |
|
| 991 | + } |
|
| 992 | + |
|
| 993 | + /** |
|
| 994 | + * @param string $filter |
|
| 995 | + * @param string|string[] $attr |
|
| 996 | + * @param int $limit |
|
| 997 | + * @param int $offset |
|
| 998 | + * @return false|int |
|
| 999 | + * @throws ServerNotAvailableException |
|
| 1000 | + */ |
|
| 1001 | + public function countUsers($filter, $attr = ['dn'], $limit = null, $offset = null) { |
|
| 1002 | + $result = false; |
|
| 1003 | + foreach ($this->connection->ldapBaseUsers as $base) { |
|
| 1004 | + $count = $this->count($filter, [$base], $attr, $limit, $offset); |
|
| 1005 | + $result = is_int($count) ? (int)$result + $count : $result; |
|
| 1006 | + } |
|
| 1007 | + return $result; |
|
| 1008 | + } |
|
| 1009 | + |
|
| 1010 | + /** |
|
| 1011 | + * executes an LDAP search, optimized for Groups |
|
| 1012 | + * |
|
| 1013 | + * @param string $filter the LDAP filter for the search |
|
| 1014 | + * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
|
| 1015 | + * @param integer $limit |
|
| 1016 | + * @param integer $offset |
|
| 1017 | + * @return array with the search result |
|
| 1018 | + * |
|
| 1019 | + * Executes an LDAP search |
|
| 1020 | + * @throws ServerNotAvailableException |
|
| 1021 | + */ |
|
| 1022 | + public function searchGroups($filter, $attr = null, $limit = null, $offset = null) { |
|
| 1023 | + $result = []; |
|
| 1024 | + foreach ($this->connection->ldapBaseGroups as $base) { |
|
| 1025 | + $result = array_merge($result, $this->search($filter, $base, $attr, $limit, $offset)); |
|
| 1026 | + } |
|
| 1027 | + return $result; |
|
| 1028 | + } |
|
| 1029 | + |
|
| 1030 | + /** |
|
| 1031 | + * returns the number of available groups |
|
| 1032 | + * |
|
| 1033 | + * @param string $filter the LDAP search filter |
|
| 1034 | + * @param string[] $attr optional |
|
| 1035 | + * @param int|null $limit |
|
| 1036 | + * @param int|null $offset |
|
| 1037 | + * @return int|bool |
|
| 1038 | + * @throws ServerNotAvailableException |
|
| 1039 | + */ |
|
| 1040 | + public function countGroups($filter, $attr = ['dn'], $limit = null, $offset = null) { |
|
| 1041 | + $result = false; |
|
| 1042 | + foreach ($this->connection->ldapBaseGroups as $base) { |
|
| 1043 | + $count = $this->count($filter, [$base], $attr, $limit, $offset); |
|
| 1044 | + $result = is_int($count) ? (int)$result + $count : $result; |
|
| 1045 | + } |
|
| 1046 | + return $result; |
|
| 1047 | + } |
|
| 1048 | + |
|
| 1049 | + /** |
|
| 1050 | + * returns the number of available objects on the base DN |
|
| 1051 | + * |
|
| 1052 | + * @param int|null $limit |
|
| 1053 | + * @param int|null $offset |
|
| 1054 | + * @return int|bool |
|
| 1055 | + * @throws ServerNotAvailableException |
|
| 1056 | + */ |
|
| 1057 | + public function countObjects($limit = null, $offset = null) { |
|
| 1058 | + $result = false; |
|
| 1059 | + foreach ($this->connection->ldapBase as $base) { |
|
| 1060 | + $count = $this->count('objectclass=*', [$base], ['dn'], $limit, $offset); |
|
| 1061 | + $result = is_int($count) ? (int)$result + $count : $result; |
|
| 1062 | + } |
|
| 1063 | + return $result; |
|
| 1064 | + } |
|
| 1065 | + |
|
| 1066 | + /** |
|
| 1067 | + * Returns the LDAP handler |
|
| 1068 | + * |
|
| 1069 | + * @throws \OC\ServerNotAvailableException |
|
| 1070 | + */ |
|
| 1071 | + |
|
| 1072 | + /** |
|
| 1073 | + * @return mixed |
|
| 1074 | + * @throws \OC\ServerNotAvailableException |
|
| 1075 | + */ |
|
| 1076 | + private function invokeLDAPMethod() { |
|
| 1077 | + $arguments = func_get_args(); |
|
| 1078 | + $command = array_shift($arguments); |
|
| 1079 | + $cr = array_shift($arguments); |
|
| 1080 | + if (!method_exists($this->ldap, $command)) { |
|
| 1081 | + return null; |
|
| 1082 | + } |
|
| 1083 | + array_unshift($arguments, $cr); |
|
| 1084 | + // php no longer supports call-time pass-by-reference |
|
| 1085 | + // thus cannot support controlPagedResultResponse as the third argument |
|
| 1086 | + // is a reference |
|
| 1087 | + $doMethod = function () use ($command, &$arguments) { |
|
| 1088 | + if ($command == 'controlPagedResultResponse') { |
|
| 1089 | + throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.'); |
|
| 1090 | + } else { |
|
| 1091 | + return call_user_func_array([$this->ldap, $command], $arguments); |
|
| 1092 | + } |
|
| 1093 | + }; |
|
| 1094 | + try { |
|
| 1095 | + $ret = $doMethod(); |
|
| 1096 | + } catch (ServerNotAvailableException $e) { |
|
| 1097 | + /* Server connection lost, attempt to reestablish it |
|
| 1098 | 1098 | * Maybe implement exponential backoff? |
| 1099 | 1099 | * This was enough to get solr indexer working which has large delays between LDAP fetches. |
| 1100 | 1100 | */ |
| 1101 | - \OCP\Util::writeLog('user_ldap', "Connection lost on $command, attempting to reestablish.", ILogger::DEBUG); |
|
| 1102 | - $this->connection->resetConnectionResource(); |
|
| 1103 | - $cr = $this->connection->getConnectionResource(); |
|
| 1104 | - |
|
| 1105 | - if (!$this->ldap->isResource($cr)) { |
|
| 1106 | - // Seems like we didn't find any resource. |
|
| 1107 | - \OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", ILogger::DEBUG); |
|
| 1108 | - throw $e; |
|
| 1109 | - } |
|
| 1110 | - |
|
| 1111 | - $arguments[0] = $cr; |
|
| 1112 | - $ret = $doMethod(); |
|
| 1113 | - } |
|
| 1114 | - return $ret; |
|
| 1115 | - } |
|
| 1116 | - |
|
| 1117 | - /** |
|
| 1118 | - * retrieved. Results will according to the order in the array. |
|
| 1119 | - * |
|
| 1120 | - * @param string $filter |
|
| 1121 | - * @param string $base |
|
| 1122 | - * @param string[] $attr |
|
| 1123 | - * @param int|null $limit optional, maximum results to be counted |
|
| 1124 | - * @param int|null $offset optional, a starting point |
|
| 1125 | - * @return array|false array with the search result as first value and pagedSearchOK as |
|
| 1126 | - * second | false if not successful |
|
| 1127 | - * @throws ServerNotAvailableException |
|
| 1128 | - */ |
|
| 1129 | - private function executeSearch( |
|
| 1130 | - string $filter, |
|
| 1131 | - string $base, |
|
| 1132 | - ?array &$attr, |
|
| 1133 | - ?int $limit, |
|
| 1134 | - ?int $offset |
|
| 1135 | - ) { |
|
| 1136 | - // See if we have a resource, in case not cancel with message |
|
| 1137 | - $cr = $this->connection->getConnectionResource(); |
|
| 1138 | - if (!$this->ldap->isResource($cr)) { |
|
| 1139 | - // Seems like we didn't find any resource. |
|
| 1140 | - // Return an empty array just like before. |
|
| 1141 | - \OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', ILogger::DEBUG); |
|
| 1142 | - return false; |
|
| 1143 | - } |
|
| 1144 | - |
|
| 1145 | - //check whether paged search should be attempted |
|
| 1146 | - $pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, (int)$limit, (int)$offset); |
|
| 1147 | - |
|
| 1148 | - $sr = $this->invokeLDAPMethod('search', $cr, $base, $filter, $attr); |
|
| 1149 | - // cannot use $cr anymore, might have changed in the previous call! |
|
| 1150 | - $error = $this->ldap->errno($this->connection->getConnectionResource()); |
|
| 1151 | - if (!$this->ldap->isResource($sr) || $error !== 0) { |
|
| 1152 | - \OCP\Util::writeLog('user_ldap', 'Attempt for Paging? ' . print_r($pagedSearchOK, true), ILogger::ERROR); |
|
| 1153 | - return false; |
|
| 1154 | - } |
|
| 1155 | - |
|
| 1156 | - return [$sr, $pagedSearchOK]; |
|
| 1157 | - } |
|
| 1158 | - |
|
| 1159 | - /** |
|
| 1160 | - * processes an LDAP paged search operation |
|
| 1161 | - * |
|
| 1162 | - * @param resource $sr the array containing the LDAP search resources |
|
| 1163 | - * @param int $foundItems number of results in the single search operation |
|
| 1164 | - * @param int $limit maximum results to be counted |
|
| 1165 | - * @param bool $pagedSearchOK whether a paged search has been executed |
|
| 1166 | - * @param bool $skipHandling required for paged search when cookies to |
|
| 1167 | - * prior results need to be gained |
|
| 1168 | - * @return bool cookie validity, true if we have more pages, false otherwise. |
|
| 1169 | - * @throws ServerNotAvailableException |
|
| 1170 | - */ |
|
| 1171 | - private function processPagedSearchStatus( |
|
| 1172 | - $sr, |
|
| 1173 | - int $foundItems, |
|
| 1174 | - int $limit, |
|
| 1175 | - bool $pagedSearchOK, |
|
| 1176 | - bool $skipHandling |
|
| 1177 | - ): bool { |
|
| 1178 | - $cookie = null; |
|
| 1179 | - if ($pagedSearchOK) { |
|
| 1180 | - $cr = $this->connection->getConnectionResource(); |
|
| 1181 | - if ($this->ldap->controlPagedResultResponse($cr, $sr, $cookie)) { |
|
| 1182 | - $this->lastCookie = $cookie; |
|
| 1183 | - } |
|
| 1184 | - |
|
| 1185 | - //browsing through prior pages to get the cookie for the new one |
|
| 1186 | - if ($skipHandling) { |
|
| 1187 | - return false; |
|
| 1188 | - } |
|
| 1189 | - // if count is bigger, then the server does not support |
|
| 1190 | - // paged search. Instead, he did a normal search. We set a |
|
| 1191 | - // flag here, so the callee knows how to deal with it. |
|
| 1192 | - if ($foundItems <= $limit) { |
|
| 1193 | - $this->pagedSearchedSuccessful = true; |
|
| 1194 | - } |
|
| 1195 | - } else { |
|
| 1196 | - if (!is_null($limit) && (int)$this->connection->ldapPagingSize !== 0) { |
|
| 1197 | - \OC::$server->getLogger()->debug( |
|
| 1198 | - 'Paged search was not available', |
|
| 1199 | - ['app' => 'user_ldap'] |
|
| 1200 | - ); |
|
| 1201 | - } |
|
| 1202 | - } |
|
| 1203 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1101 | + \OCP\Util::writeLog('user_ldap', "Connection lost on $command, attempting to reestablish.", ILogger::DEBUG); |
|
| 1102 | + $this->connection->resetConnectionResource(); |
|
| 1103 | + $cr = $this->connection->getConnectionResource(); |
|
| 1104 | + |
|
| 1105 | + if (!$this->ldap->isResource($cr)) { |
|
| 1106 | + // Seems like we didn't find any resource. |
|
| 1107 | + \OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", ILogger::DEBUG); |
|
| 1108 | + throw $e; |
|
| 1109 | + } |
|
| 1110 | + |
|
| 1111 | + $arguments[0] = $cr; |
|
| 1112 | + $ret = $doMethod(); |
|
| 1113 | + } |
|
| 1114 | + return $ret; |
|
| 1115 | + } |
|
| 1116 | + |
|
| 1117 | + /** |
|
| 1118 | + * retrieved. Results will according to the order in the array. |
|
| 1119 | + * |
|
| 1120 | + * @param string $filter |
|
| 1121 | + * @param string $base |
|
| 1122 | + * @param string[] $attr |
|
| 1123 | + * @param int|null $limit optional, maximum results to be counted |
|
| 1124 | + * @param int|null $offset optional, a starting point |
|
| 1125 | + * @return array|false array with the search result as first value and pagedSearchOK as |
|
| 1126 | + * second | false if not successful |
|
| 1127 | + * @throws ServerNotAvailableException |
|
| 1128 | + */ |
|
| 1129 | + private function executeSearch( |
|
| 1130 | + string $filter, |
|
| 1131 | + string $base, |
|
| 1132 | + ?array &$attr, |
|
| 1133 | + ?int $limit, |
|
| 1134 | + ?int $offset |
|
| 1135 | + ) { |
|
| 1136 | + // See if we have a resource, in case not cancel with message |
|
| 1137 | + $cr = $this->connection->getConnectionResource(); |
|
| 1138 | + if (!$this->ldap->isResource($cr)) { |
|
| 1139 | + // Seems like we didn't find any resource. |
|
| 1140 | + // Return an empty array just like before. |
|
| 1141 | + \OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', ILogger::DEBUG); |
|
| 1142 | + return false; |
|
| 1143 | + } |
|
| 1144 | + |
|
| 1145 | + //check whether paged search should be attempted |
|
| 1146 | + $pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, (int)$limit, (int)$offset); |
|
| 1147 | + |
|
| 1148 | + $sr = $this->invokeLDAPMethod('search', $cr, $base, $filter, $attr); |
|
| 1149 | + // cannot use $cr anymore, might have changed in the previous call! |
|
| 1150 | + $error = $this->ldap->errno($this->connection->getConnectionResource()); |
|
| 1151 | + if (!$this->ldap->isResource($sr) || $error !== 0) { |
|
| 1152 | + \OCP\Util::writeLog('user_ldap', 'Attempt for Paging? ' . print_r($pagedSearchOK, true), ILogger::ERROR); |
|
| 1153 | + return false; |
|
| 1154 | + } |
|
| 1155 | + |
|
| 1156 | + return [$sr, $pagedSearchOK]; |
|
| 1157 | + } |
|
| 1158 | + |
|
| 1159 | + /** |
|
| 1160 | + * processes an LDAP paged search operation |
|
| 1161 | + * |
|
| 1162 | + * @param resource $sr the array containing the LDAP search resources |
|
| 1163 | + * @param int $foundItems number of results in the single search operation |
|
| 1164 | + * @param int $limit maximum results to be counted |
|
| 1165 | + * @param bool $pagedSearchOK whether a paged search has been executed |
|
| 1166 | + * @param bool $skipHandling required for paged search when cookies to |
|
| 1167 | + * prior results need to be gained |
|
| 1168 | + * @return bool cookie validity, true if we have more pages, false otherwise. |
|
| 1169 | + * @throws ServerNotAvailableException |
|
| 1170 | + */ |
|
| 1171 | + private function processPagedSearchStatus( |
|
| 1172 | + $sr, |
|
| 1173 | + int $foundItems, |
|
| 1174 | + int $limit, |
|
| 1175 | + bool $pagedSearchOK, |
|
| 1176 | + bool $skipHandling |
|
| 1177 | + ): bool { |
|
| 1178 | + $cookie = null; |
|
| 1179 | + if ($pagedSearchOK) { |
|
| 1180 | + $cr = $this->connection->getConnectionResource(); |
|
| 1181 | + if ($this->ldap->controlPagedResultResponse($cr, $sr, $cookie)) { |
|
| 1182 | + $this->lastCookie = $cookie; |
|
| 1183 | + } |
|
| 1184 | + |
|
| 1185 | + //browsing through prior pages to get the cookie for the new one |
|
| 1186 | + if ($skipHandling) { |
|
| 1187 | + return false; |
|
| 1188 | + } |
|
| 1189 | + // if count is bigger, then the server does not support |
|
| 1190 | + // paged search. Instead, he did a normal search. We set a |
|
| 1191 | + // flag here, so the callee knows how to deal with it. |
|
| 1192 | + if ($foundItems <= $limit) { |
|
| 1193 | + $this->pagedSearchedSuccessful = true; |
|
| 1194 | + } |
|
| 1195 | + } else { |
|
| 1196 | + if (!is_null($limit) && (int)$this->connection->ldapPagingSize !== 0) { |
|
| 1197 | + \OC::$server->getLogger()->debug( |
|
| 1198 | + 'Paged search was not available', |
|
| 1199 | + ['app' => 'user_ldap'] |
|
| 1200 | + ); |
|
| 1201 | + } |
|
| 1202 | + } |
|
| 1203 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1204 | 1204 | * Return cookie status. If we don't have more pages, with RHDS |
| 1205 | 1205 | * cookie is null, with openldap cookie is an empty string and |
| 1206 | 1206 | * to 386ds '0' is a valid cookie. Even if $iFoundItems == 0 |
| 1207 | 1207 | */ |
| 1208 | - return !empty($cookie) || $cookie === '0'; |
|
| 1209 | - } |
|
| 1210 | - |
|
| 1211 | - /** |
|
| 1212 | - * executes an LDAP search, but counts the results only |
|
| 1213 | - * |
|
| 1214 | - * @param string $filter the LDAP filter for the search |
|
| 1215 | - * @param array $bases an array containing the LDAP subtree(s) that shall be searched |
|
| 1216 | - * @param string|string[] $attr optional, array, one or more attributes that shall be |
|
| 1217 | - * retrieved. Results will according to the order in the array. |
|
| 1218 | - * @param int $limit optional, maximum results to be counted |
|
| 1219 | - * @param int $offset optional, a starting point |
|
| 1220 | - * @param bool $skipHandling indicates whether the pages search operation is |
|
| 1221 | - * completed |
|
| 1222 | - * @return int|false Integer or false if the search could not be initialized |
|
| 1223 | - * @throws ServerNotAvailableException |
|
| 1224 | - */ |
|
| 1225 | - private function count( |
|
| 1226 | - string $filter, |
|
| 1227 | - array $bases, |
|
| 1228 | - $attr = null, |
|
| 1229 | - ?int $limit = null, |
|
| 1230 | - ?int $offset = null, |
|
| 1231 | - bool $skipHandling = false |
|
| 1232 | - ) { |
|
| 1233 | - \OC::$server->getLogger()->debug('Count filter: {filter}', [ |
|
| 1234 | - 'app' => 'user_ldap', |
|
| 1235 | - 'filter' => $filter |
|
| 1236 | - ]); |
|
| 1237 | - |
|
| 1238 | - if (!is_null($attr) && !is_array($attr)) { |
|
| 1239 | - $attr = [mb_strtolower($attr, 'UTF-8')]; |
|
| 1240 | - } |
|
| 1241 | - |
|
| 1242 | - $limitPerPage = (int)$this->connection->ldapPagingSize; |
|
| 1243 | - if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1244 | - $limitPerPage = $limit; |
|
| 1245 | - } |
|
| 1246 | - |
|
| 1247 | - $counter = 0; |
|
| 1248 | - $count = null; |
|
| 1249 | - $this->connection->getConnectionResource(); |
|
| 1250 | - |
|
| 1251 | - foreach ($bases as $base) { |
|
| 1252 | - do { |
|
| 1253 | - $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
|
| 1254 | - if ($search === false) { |
|
| 1255 | - return $counter > 0 ? $counter : false; |
|
| 1256 | - } |
|
| 1257 | - list($sr, $pagedSearchOK) = $search; |
|
| 1258 | - |
|
| 1259 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1208 | + return !empty($cookie) || $cookie === '0'; |
|
| 1209 | + } |
|
| 1210 | + |
|
| 1211 | + /** |
|
| 1212 | + * executes an LDAP search, but counts the results only |
|
| 1213 | + * |
|
| 1214 | + * @param string $filter the LDAP filter for the search |
|
| 1215 | + * @param array $bases an array containing the LDAP subtree(s) that shall be searched |
|
| 1216 | + * @param string|string[] $attr optional, array, one or more attributes that shall be |
|
| 1217 | + * retrieved. Results will according to the order in the array. |
|
| 1218 | + * @param int $limit optional, maximum results to be counted |
|
| 1219 | + * @param int $offset optional, a starting point |
|
| 1220 | + * @param bool $skipHandling indicates whether the pages search operation is |
|
| 1221 | + * completed |
|
| 1222 | + * @return int|false Integer or false if the search could not be initialized |
|
| 1223 | + * @throws ServerNotAvailableException |
|
| 1224 | + */ |
|
| 1225 | + private function count( |
|
| 1226 | + string $filter, |
|
| 1227 | + array $bases, |
|
| 1228 | + $attr = null, |
|
| 1229 | + ?int $limit = null, |
|
| 1230 | + ?int $offset = null, |
|
| 1231 | + bool $skipHandling = false |
|
| 1232 | + ) { |
|
| 1233 | + \OC::$server->getLogger()->debug('Count filter: {filter}', [ |
|
| 1234 | + 'app' => 'user_ldap', |
|
| 1235 | + 'filter' => $filter |
|
| 1236 | + ]); |
|
| 1237 | + |
|
| 1238 | + if (!is_null($attr) && !is_array($attr)) { |
|
| 1239 | + $attr = [mb_strtolower($attr, 'UTF-8')]; |
|
| 1240 | + } |
|
| 1241 | + |
|
| 1242 | + $limitPerPage = (int)$this->connection->ldapPagingSize; |
|
| 1243 | + if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1244 | + $limitPerPage = $limit; |
|
| 1245 | + } |
|
| 1246 | + |
|
| 1247 | + $counter = 0; |
|
| 1248 | + $count = null; |
|
| 1249 | + $this->connection->getConnectionResource(); |
|
| 1250 | + |
|
| 1251 | + foreach ($bases as $base) { |
|
| 1252 | + do { |
|
| 1253 | + $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
|
| 1254 | + if ($search === false) { |
|
| 1255 | + return $counter > 0 ? $counter : false; |
|
| 1256 | + } |
|
| 1257 | + list($sr, $pagedSearchOK) = $search; |
|
| 1258 | + |
|
| 1259 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1260 | 1260 | * countEntriesInSearchResults() method signature changed |
| 1261 | 1261 | * by removing $limit and &$hasHitLimit parameters |
| 1262 | 1262 | */ |
| 1263 | - $count = $this->countEntriesInSearchResults($sr); |
|
| 1264 | - $counter += $count; |
|
| 1263 | + $count = $this->countEntriesInSearchResults($sr); |
|
| 1264 | + $counter += $count; |
|
| 1265 | 1265 | |
| 1266 | - $hasMorePages = $this->processPagedSearchStatus($sr, $count, $limitPerPage, $pagedSearchOK, $skipHandling); |
|
| 1267 | - $offset += $limitPerPage; |
|
| 1268 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1266 | + $hasMorePages = $this->processPagedSearchStatus($sr, $count, $limitPerPage, $pagedSearchOK, $skipHandling); |
|
| 1267 | + $offset += $limitPerPage; |
|
| 1268 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1269 | 1269 | * Continue now depends on $hasMorePages value |
| 1270 | 1270 | */ |
| 1271 | - $continue = $pagedSearchOK && $hasMorePages; |
|
| 1272 | - } while ($continue && (is_null($limit) || $limit <= 0 || $limit > $counter)); |
|
| 1273 | - } |
|
| 1274 | - |
|
| 1275 | - return $counter; |
|
| 1276 | - } |
|
| 1277 | - |
|
| 1278 | - /** |
|
| 1279 | - * @param resource $sr |
|
| 1280 | - * @return int |
|
| 1281 | - * @throws ServerNotAvailableException |
|
| 1282 | - */ |
|
| 1283 | - private function countEntriesInSearchResults($sr): int { |
|
| 1284 | - return (int)$this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $sr); |
|
| 1285 | - } |
|
| 1286 | - |
|
| 1287 | - /** |
|
| 1288 | - * Executes an LDAP search |
|
| 1289 | - * |
|
| 1290 | - * @throws ServerNotAvailableException |
|
| 1291 | - */ |
|
| 1292 | - public function search( |
|
| 1293 | - string $filter, |
|
| 1294 | - string $base, |
|
| 1295 | - ?array $attr = null, |
|
| 1296 | - ?int $limit = null, |
|
| 1297 | - ?int $offset = null, |
|
| 1298 | - bool $skipHandling = false |
|
| 1299 | - ): array { |
|
| 1300 | - $limitPerPage = (int)$this->connection->ldapPagingSize; |
|
| 1301 | - if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1302 | - $limitPerPage = $limit; |
|
| 1303 | - } |
|
| 1304 | - |
|
| 1305 | - if (!is_null($attr) && !is_array($attr)) { |
|
| 1306 | - $attr = [mb_strtolower($attr, 'UTF-8')]; |
|
| 1307 | - } |
|
| 1308 | - |
|
| 1309 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1271 | + $continue = $pagedSearchOK && $hasMorePages; |
|
| 1272 | + } while ($continue && (is_null($limit) || $limit <= 0 || $limit > $counter)); |
|
| 1273 | + } |
|
| 1274 | + |
|
| 1275 | + return $counter; |
|
| 1276 | + } |
|
| 1277 | + |
|
| 1278 | + /** |
|
| 1279 | + * @param resource $sr |
|
| 1280 | + * @return int |
|
| 1281 | + * @throws ServerNotAvailableException |
|
| 1282 | + */ |
|
| 1283 | + private function countEntriesInSearchResults($sr): int { |
|
| 1284 | + return (int)$this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $sr); |
|
| 1285 | + } |
|
| 1286 | + |
|
| 1287 | + /** |
|
| 1288 | + * Executes an LDAP search |
|
| 1289 | + * |
|
| 1290 | + * @throws ServerNotAvailableException |
|
| 1291 | + */ |
|
| 1292 | + public function search( |
|
| 1293 | + string $filter, |
|
| 1294 | + string $base, |
|
| 1295 | + ?array $attr = null, |
|
| 1296 | + ?int $limit = null, |
|
| 1297 | + ?int $offset = null, |
|
| 1298 | + bool $skipHandling = false |
|
| 1299 | + ): array { |
|
| 1300 | + $limitPerPage = (int)$this->connection->ldapPagingSize; |
|
| 1301 | + if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1302 | + $limitPerPage = $limit; |
|
| 1303 | + } |
|
| 1304 | + |
|
| 1305 | + if (!is_null($attr) && !is_array($attr)) { |
|
| 1306 | + $attr = [mb_strtolower($attr, 'UTF-8')]; |
|
| 1307 | + } |
|
| 1308 | + |
|
| 1309 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1310 | 1310 | * As we can have pages with zero results and/or pages with less |
| 1311 | 1311 | * than $limit results but with a still valid server 'cookie', |
| 1312 | 1312 | * loops through until we get $continue equals true and |
| 1313 | 1313 | * $findings['count'] < $limit |
| 1314 | 1314 | */ |
| 1315 | - $findings = []; |
|
| 1316 | - $savedoffset = $offset; |
|
| 1317 | - $iFoundItems = 0; |
|
| 1318 | - |
|
| 1319 | - do { |
|
| 1320 | - $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
|
| 1321 | - if ($search === false) { |
|
| 1322 | - return []; |
|
| 1323 | - } |
|
| 1324 | - list($sr, $pagedSearchOK) = $search; |
|
| 1325 | - $cr = $this->connection->getConnectionResource(); |
|
| 1326 | - |
|
| 1327 | - if ($skipHandling) { |
|
| 1328 | - //i.e. result do not need to be fetched, we just need the cookie |
|
| 1329 | - //thus pass 1 or any other value as $iFoundItems because it is not |
|
| 1330 | - //used |
|
| 1331 | - $this->processPagedSearchStatus($sr, 1, $limitPerPage, $pagedSearchOK, $skipHandling); |
|
| 1332 | - return []; |
|
| 1333 | - } |
|
| 1334 | - |
|
| 1335 | - $findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $sr)); |
|
| 1336 | - $iFoundItems = max($iFoundItems, $findings['count']); |
|
| 1337 | - unset($findings['count']); |
|
| 1338 | - |
|
| 1339 | - $continue = $this->processPagedSearchStatus($sr, $iFoundItems, $limitPerPage, $pagedSearchOK, $skipHandling); |
|
| 1340 | - $offset += $limitPerPage; |
|
| 1341 | - } while ($continue && $pagedSearchOK && ($limit === null || count($findings) < $limit)); |
|
| 1342 | - |
|
| 1343 | - // resetting offset |
|
| 1344 | - $offset = $savedoffset; |
|
| 1345 | - |
|
| 1346 | - // if we're here, probably no connection resource is returned. |
|
| 1347 | - // to make Nextcloud behave nicely, we simply give back an empty array. |
|
| 1348 | - if (is_null($findings)) { |
|
| 1349 | - return []; |
|
| 1350 | - } |
|
| 1351 | - |
|
| 1352 | - if (!is_null($attr)) { |
|
| 1353 | - $selection = []; |
|
| 1354 | - $i = 0; |
|
| 1355 | - foreach ($findings as $item) { |
|
| 1356 | - if (!is_array($item)) { |
|
| 1357 | - continue; |
|
| 1358 | - } |
|
| 1359 | - $item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8'); |
|
| 1360 | - foreach ($attr as $key) { |
|
| 1361 | - if (isset($item[$key])) { |
|
| 1362 | - if (is_array($item[$key]) && isset($item[$key]['count'])) { |
|
| 1363 | - unset($item[$key]['count']); |
|
| 1364 | - } |
|
| 1365 | - if ($key !== 'dn') { |
|
| 1366 | - if ($this->resemblesDN($key)) { |
|
| 1367 | - $selection[$i][$key] = $this->helper->sanitizeDN($item[$key]); |
|
| 1368 | - } elseif ($key === 'objectguid' || $key === 'guid') { |
|
| 1369 | - $selection[$i][$key] = [$this->convertObjectGUID2Str($item[$key][0])]; |
|
| 1370 | - } else { |
|
| 1371 | - $selection[$i][$key] = $item[$key]; |
|
| 1372 | - } |
|
| 1373 | - } else { |
|
| 1374 | - $selection[$i][$key] = [$this->helper->sanitizeDN($item[$key])]; |
|
| 1375 | - } |
|
| 1376 | - } |
|
| 1377 | - } |
|
| 1378 | - $i++; |
|
| 1379 | - } |
|
| 1380 | - $findings = $selection; |
|
| 1381 | - } |
|
| 1382 | - //we slice the findings, when |
|
| 1383 | - //a) paged search unsuccessful, though attempted |
|
| 1384 | - //b) no paged search, but limit set |
|
| 1385 | - if ((!$this->getPagedSearchResultState() |
|
| 1386 | - && $pagedSearchOK) |
|
| 1387 | - || ( |
|
| 1388 | - !$pagedSearchOK |
|
| 1389 | - && !is_null($limit) |
|
| 1390 | - ) |
|
| 1391 | - ) { |
|
| 1392 | - $findings = array_slice($findings, (int)$offset, $limit); |
|
| 1393 | - } |
|
| 1394 | - return $findings; |
|
| 1395 | - } |
|
| 1396 | - |
|
| 1397 | - /** |
|
| 1398 | - * @param string $name |
|
| 1399 | - * @return string |
|
| 1400 | - * @throws \InvalidArgumentException |
|
| 1401 | - */ |
|
| 1402 | - public function sanitizeUsername($name) { |
|
| 1403 | - $name = trim($name); |
|
| 1404 | - |
|
| 1405 | - if ($this->connection->ldapIgnoreNamingRules) { |
|
| 1406 | - return $name; |
|
| 1407 | - } |
|
| 1408 | - |
|
| 1409 | - // Transliteration to ASCII |
|
| 1410 | - $transliterated = @iconv('UTF-8', 'ASCII//TRANSLIT', $name); |
|
| 1411 | - if ($transliterated !== false) { |
|
| 1412 | - // depending on system config iconv can work or not |
|
| 1413 | - $name = $transliterated; |
|
| 1414 | - } |
|
| 1415 | - |
|
| 1416 | - // Replacements |
|
| 1417 | - $name = str_replace(' ', '_', $name); |
|
| 1418 | - |
|
| 1419 | - // Every remaining disallowed characters will be removed |
|
| 1420 | - $name = preg_replace('/[^a-zA-Z0-9_.@-]/u', '', $name); |
|
| 1421 | - |
|
| 1422 | - if ($name === '') { |
|
| 1423 | - throw new \InvalidArgumentException('provided name template for username does not contain any allowed characters'); |
|
| 1424 | - } |
|
| 1425 | - |
|
| 1426 | - return $name; |
|
| 1427 | - } |
|
| 1428 | - |
|
| 1429 | - /** |
|
| 1430 | - * escapes (user provided) parts for LDAP filter |
|
| 1431 | - * |
|
| 1432 | - * @param string $input , the provided value |
|
| 1433 | - * @param bool $allowAsterisk whether in * at the beginning should be preserved |
|
| 1434 | - * @return string the escaped string |
|
| 1435 | - */ |
|
| 1436 | - public function escapeFilterPart($input, $allowAsterisk = false): string { |
|
| 1437 | - $asterisk = ''; |
|
| 1438 | - if ($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { |
|
| 1439 | - $asterisk = '*'; |
|
| 1440 | - $input = mb_substr($input, 1, null, 'UTF-8'); |
|
| 1441 | - } |
|
| 1442 | - $search = ['*', '\\', '(', ')']; |
|
| 1443 | - $replace = ['\\*', '\\\\', '\\(', '\\)']; |
|
| 1444 | - return $asterisk . str_replace($search, $replace, $input); |
|
| 1445 | - } |
|
| 1446 | - |
|
| 1447 | - /** |
|
| 1448 | - * combines the input filters with AND |
|
| 1449 | - * |
|
| 1450 | - * @param string[] $filters the filters to connect |
|
| 1451 | - * @return string the combined filter |
|
| 1452 | - */ |
|
| 1453 | - public function combineFilterWithAnd($filters): string { |
|
| 1454 | - return $this->combineFilter($filters, '&'); |
|
| 1455 | - } |
|
| 1456 | - |
|
| 1457 | - /** |
|
| 1458 | - * combines the input filters with OR |
|
| 1459 | - * |
|
| 1460 | - * @param string[] $filters the filters to connect |
|
| 1461 | - * @return string the combined filter |
|
| 1462 | - * Combines Filter arguments with OR |
|
| 1463 | - */ |
|
| 1464 | - public function combineFilterWithOr($filters) { |
|
| 1465 | - return $this->combineFilter($filters, '|'); |
|
| 1466 | - } |
|
| 1467 | - |
|
| 1468 | - /** |
|
| 1469 | - * combines the input filters with given operator |
|
| 1470 | - * |
|
| 1471 | - * @param string[] $filters the filters to connect |
|
| 1472 | - * @param string $operator either & or | |
|
| 1473 | - * @return string the combined filter |
|
| 1474 | - */ |
|
| 1475 | - private function combineFilter($filters, $operator) { |
|
| 1476 | - $combinedFilter = '(' . $operator; |
|
| 1477 | - foreach ($filters as $filter) { |
|
| 1478 | - if ($filter !== '' && $filter[0] !== '(') { |
|
| 1479 | - $filter = '(' . $filter . ')'; |
|
| 1480 | - } |
|
| 1481 | - $combinedFilter .= $filter; |
|
| 1482 | - } |
|
| 1483 | - $combinedFilter .= ')'; |
|
| 1484 | - return $combinedFilter; |
|
| 1485 | - } |
|
| 1486 | - |
|
| 1487 | - /** |
|
| 1488 | - * creates a filter part for to perform search for users |
|
| 1489 | - * |
|
| 1490 | - * @param string $search the search term |
|
| 1491 | - * @return string the final filter part to use in LDAP searches |
|
| 1492 | - */ |
|
| 1493 | - public function getFilterPartForUserSearch($search) { |
|
| 1494 | - return $this->getFilterPartForSearch($search, |
|
| 1495 | - $this->connection->ldapAttributesForUserSearch, |
|
| 1496 | - $this->connection->ldapUserDisplayName); |
|
| 1497 | - } |
|
| 1498 | - |
|
| 1499 | - /** |
|
| 1500 | - * creates a filter part for to perform search for groups |
|
| 1501 | - * |
|
| 1502 | - * @param string $search the search term |
|
| 1503 | - * @return string the final filter part to use in LDAP searches |
|
| 1504 | - */ |
|
| 1505 | - public function getFilterPartForGroupSearch($search) { |
|
| 1506 | - return $this->getFilterPartForSearch($search, |
|
| 1507 | - $this->connection->ldapAttributesForGroupSearch, |
|
| 1508 | - $this->connection->ldapGroupDisplayName); |
|
| 1509 | - } |
|
| 1510 | - |
|
| 1511 | - /** |
|
| 1512 | - * creates a filter part for searches by splitting up the given search |
|
| 1513 | - * string into single words |
|
| 1514 | - * |
|
| 1515 | - * @param string $search the search term |
|
| 1516 | - * @param string[] $searchAttributes needs to have at least two attributes, |
|
| 1517 | - * otherwise it does not make sense :) |
|
| 1518 | - * @return string the final filter part to use in LDAP searches |
|
| 1519 | - * @throws DomainException |
|
| 1520 | - */ |
|
| 1521 | - private function getAdvancedFilterPartForSearch($search, $searchAttributes) { |
|
| 1522 | - if (!is_array($searchAttributes) || count($searchAttributes) < 2) { |
|
| 1523 | - throw new DomainException('searchAttributes must be an array with at least two string'); |
|
| 1524 | - } |
|
| 1525 | - $searchWords = explode(' ', trim($search)); |
|
| 1526 | - $wordFilters = []; |
|
| 1527 | - foreach ($searchWords as $word) { |
|
| 1528 | - $word = $this->prepareSearchTerm($word); |
|
| 1529 | - //every word needs to appear at least once |
|
| 1530 | - $wordMatchOneAttrFilters = []; |
|
| 1531 | - foreach ($searchAttributes as $attr) { |
|
| 1532 | - $wordMatchOneAttrFilters[] = $attr . '=' . $word; |
|
| 1533 | - } |
|
| 1534 | - $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters); |
|
| 1535 | - } |
|
| 1536 | - return $this->combineFilterWithAnd($wordFilters); |
|
| 1537 | - } |
|
| 1538 | - |
|
| 1539 | - /** |
|
| 1540 | - * creates a filter part for searches |
|
| 1541 | - * |
|
| 1542 | - * @param string $search the search term |
|
| 1543 | - * @param string[]|null $searchAttributes |
|
| 1544 | - * @param string $fallbackAttribute a fallback attribute in case the user |
|
| 1545 | - * did not define search attributes. Typically the display name attribute. |
|
| 1546 | - * @return string the final filter part to use in LDAP searches |
|
| 1547 | - */ |
|
| 1548 | - private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) { |
|
| 1549 | - $filter = []; |
|
| 1550 | - $haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0); |
|
| 1551 | - if ($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { |
|
| 1552 | - try { |
|
| 1553 | - return $this->getAdvancedFilterPartForSearch($search, $searchAttributes); |
|
| 1554 | - } catch (DomainException $e) { |
|
| 1555 | - // Creating advanced filter for search failed, falling back to simple method. Edge case, but valid. |
|
| 1556 | - } |
|
| 1557 | - } |
|
| 1558 | - |
|
| 1559 | - $search = $this->prepareSearchTerm($search); |
|
| 1560 | - if (!is_array($searchAttributes) || count($searchAttributes) === 0) { |
|
| 1561 | - if ($fallbackAttribute === '') { |
|
| 1562 | - return ''; |
|
| 1563 | - } |
|
| 1564 | - $filter[] = $fallbackAttribute . '=' . $search; |
|
| 1565 | - } else { |
|
| 1566 | - foreach ($searchAttributes as $attribute) { |
|
| 1567 | - $filter[] = $attribute . '=' . $search; |
|
| 1568 | - } |
|
| 1569 | - } |
|
| 1570 | - if (count($filter) === 1) { |
|
| 1571 | - return '(' . $filter[0] . ')'; |
|
| 1572 | - } |
|
| 1573 | - return $this->combineFilterWithOr($filter); |
|
| 1574 | - } |
|
| 1575 | - |
|
| 1576 | - /** |
|
| 1577 | - * returns the search term depending on whether we are allowed |
|
| 1578 | - * list users found by ldap with the current input appended by |
|
| 1579 | - * a * |
|
| 1580 | - * |
|
| 1581 | - * @return string |
|
| 1582 | - */ |
|
| 1583 | - private function prepareSearchTerm($term) { |
|
| 1584 | - $config = \OC::$server->getConfig(); |
|
| 1585 | - |
|
| 1586 | - $allowEnum = $config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes'); |
|
| 1587 | - |
|
| 1588 | - $result = $term; |
|
| 1589 | - if ($term === '') { |
|
| 1590 | - $result = '*'; |
|
| 1591 | - } elseif ($allowEnum !== 'no') { |
|
| 1592 | - $result = $term . '*'; |
|
| 1593 | - } |
|
| 1594 | - return $result; |
|
| 1595 | - } |
|
| 1596 | - |
|
| 1597 | - /** |
|
| 1598 | - * returns the filter used for counting users |
|
| 1599 | - * |
|
| 1600 | - * @return string |
|
| 1601 | - */ |
|
| 1602 | - public function getFilterForUserCount() { |
|
| 1603 | - $filter = $this->combineFilterWithAnd([ |
|
| 1604 | - $this->connection->ldapUserFilter, |
|
| 1605 | - $this->connection->ldapUserDisplayName . '=*' |
|
| 1606 | - ]); |
|
| 1607 | - |
|
| 1608 | - return $filter; |
|
| 1609 | - } |
|
| 1610 | - |
|
| 1611 | - /** |
|
| 1612 | - * @param string $name |
|
| 1613 | - * @param string $password |
|
| 1614 | - * @return bool |
|
| 1615 | - */ |
|
| 1616 | - public function areCredentialsValid($name, $password) { |
|
| 1617 | - $name = $this->helper->DNasBaseParameter($name); |
|
| 1618 | - $testConnection = clone $this->connection; |
|
| 1619 | - $credentials = [ |
|
| 1620 | - 'ldapAgentName' => $name, |
|
| 1621 | - 'ldapAgentPassword' => $password |
|
| 1622 | - ]; |
|
| 1623 | - if (!$testConnection->setConfiguration($credentials)) { |
|
| 1624 | - return false; |
|
| 1625 | - } |
|
| 1626 | - return $testConnection->bind(); |
|
| 1627 | - } |
|
| 1628 | - |
|
| 1629 | - /** |
|
| 1630 | - * reverse lookup of a DN given a known UUID |
|
| 1631 | - * |
|
| 1632 | - * @param string $uuid |
|
| 1633 | - * @return string |
|
| 1634 | - * @throws \Exception |
|
| 1635 | - */ |
|
| 1636 | - public function getUserDnByUuid($uuid) { |
|
| 1637 | - $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1638 | - $filter = $this->connection->ldapUserFilter; |
|
| 1639 | - $bases = $this->connection->ldapBaseUsers; |
|
| 1640 | - |
|
| 1641 | - if ($this->connection->ldapUuidUserAttribute === 'auto' && $uuidOverride === '') { |
|
| 1642 | - // Sacrebleu! The UUID attribute is unknown :( We need first an |
|
| 1643 | - // existing DN to be able to reliably detect it. |
|
| 1644 | - foreach ($bases as $base) { |
|
| 1645 | - $result = $this->search($filter, $base, ['dn'], 1); |
|
| 1646 | - if (!isset($result[0]) || !isset($result[0]['dn'])) { |
|
| 1647 | - continue; |
|
| 1648 | - } |
|
| 1649 | - $dn = $result[0]['dn'][0]; |
|
| 1650 | - if ($hasFound = $this->detectUuidAttribute($dn, true)) { |
|
| 1651 | - break; |
|
| 1652 | - } |
|
| 1653 | - } |
|
| 1654 | - if (!isset($hasFound) || !$hasFound) { |
|
| 1655 | - throw new \Exception('Cannot determine UUID attribute'); |
|
| 1656 | - } |
|
| 1657 | - } else { |
|
| 1658 | - // The UUID attribute is either known or an override is given. |
|
| 1659 | - // By calling this method we ensure that $this->connection->$uuidAttr |
|
| 1660 | - // is definitely set |
|
| 1661 | - if (!$this->detectUuidAttribute('', true)) { |
|
| 1662 | - throw new \Exception('Cannot determine UUID attribute'); |
|
| 1663 | - } |
|
| 1664 | - } |
|
| 1665 | - |
|
| 1666 | - $uuidAttr = $this->connection->ldapUuidUserAttribute; |
|
| 1667 | - if ($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { |
|
| 1668 | - $uuid = $this->formatGuid2ForFilterUser($uuid); |
|
| 1669 | - } |
|
| 1670 | - |
|
| 1671 | - $filter = $uuidAttr . '=' . $uuid; |
|
| 1672 | - $result = $this->searchUsers($filter, ['dn'], 2); |
|
| 1673 | - if (is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { |
|
| 1674 | - // we put the count into account to make sure that this is |
|
| 1675 | - // really unique |
|
| 1676 | - return $result[0]['dn'][0]; |
|
| 1677 | - } |
|
| 1678 | - |
|
| 1679 | - throw new \Exception('Cannot determine UUID attribute'); |
|
| 1680 | - } |
|
| 1681 | - |
|
| 1682 | - /** |
|
| 1683 | - * auto-detects the directory's UUID attribute |
|
| 1684 | - * |
|
| 1685 | - * @param string $dn a known DN used to check against |
|
| 1686 | - * @param bool $isUser |
|
| 1687 | - * @param bool $force the detection should be run, even if it is not set to auto |
|
| 1688 | - * @param array|null $ldapRecord |
|
| 1689 | - * @return bool true on success, false otherwise |
|
| 1690 | - * @throws ServerNotAvailableException |
|
| 1691 | - */ |
|
| 1692 | - private function detectUuidAttribute($dn, $isUser = true, $force = false, array $ldapRecord = null) { |
|
| 1693 | - if ($isUser) { |
|
| 1694 | - $uuidAttr = 'ldapUuidUserAttribute'; |
|
| 1695 | - $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1696 | - } else { |
|
| 1697 | - $uuidAttr = 'ldapUuidGroupAttribute'; |
|
| 1698 | - $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
|
| 1699 | - } |
|
| 1700 | - |
|
| 1701 | - if (!$force) { |
|
| 1702 | - if ($this->connection->$uuidAttr !== 'auto') { |
|
| 1703 | - return true; |
|
| 1704 | - } elseif (is_string($uuidOverride) && trim($uuidOverride) !== '') { |
|
| 1705 | - $this->connection->$uuidAttr = $uuidOverride; |
|
| 1706 | - return true; |
|
| 1707 | - } |
|
| 1708 | - |
|
| 1709 | - $attribute = $this->connection->getFromCache($uuidAttr); |
|
| 1710 | - if (!$attribute === null) { |
|
| 1711 | - $this->connection->$uuidAttr = $attribute; |
|
| 1712 | - return true; |
|
| 1713 | - } |
|
| 1714 | - } |
|
| 1715 | - |
|
| 1716 | - foreach (self::UUID_ATTRIBUTES as $attribute) { |
|
| 1717 | - if ($ldapRecord !== null) { |
|
| 1718 | - // we have the info from LDAP already, we don't need to talk to the server again |
|
| 1719 | - if (isset($ldapRecord[$attribute])) { |
|
| 1720 | - $this->connection->$uuidAttr = $attribute; |
|
| 1721 | - return true; |
|
| 1722 | - } |
|
| 1723 | - } |
|
| 1724 | - |
|
| 1725 | - $value = $this->readAttribute($dn, $attribute); |
|
| 1726 | - if (is_array($value) && isset($value[0]) && !empty($value[0])) { |
|
| 1727 | - \OC::$server->getLogger()->debug( |
|
| 1728 | - 'Setting {attribute} as {subject}', |
|
| 1729 | - [ |
|
| 1730 | - 'app' => 'user_ldap', |
|
| 1731 | - 'attribute' => $attribute, |
|
| 1732 | - 'subject' => $uuidAttr |
|
| 1733 | - ] |
|
| 1734 | - ); |
|
| 1735 | - $this->connection->$uuidAttr = $attribute; |
|
| 1736 | - $this->connection->writeToCache($uuidAttr, $attribute); |
|
| 1737 | - return true; |
|
| 1738 | - } |
|
| 1739 | - } |
|
| 1740 | - \OC::$server->getLogger()->debug('Could not autodetect the UUID attribute', ['app' => 'user_ldap']); |
|
| 1741 | - |
|
| 1742 | - return false; |
|
| 1743 | - } |
|
| 1744 | - |
|
| 1745 | - /** |
|
| 1746 | - * @param string $dn |
|
| 1747 | - * @param bool $isUser |
|
| 1748 | - * @param null $ldapRecord |
|
| 1749 | - * @return bool|string |
|
| 1750 | - * @throws ServerNotAvailableException |
|
| 1751 | - */ |
|
| 1752 | - public function getUUID($dn, $isUser = true, $ldapRecord = null) { |
|
| 1753 | - if ($isUser) { |
|
| 1754 | - $uuidAttr = 'ldapUuidUserAttribute'; |
|
| 1755 | - $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1756 | - } else { |
|
| 1757 | - $uuidAttr = 'ldapUuidGroupAttribute'; |
|
| 1758 | - $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
|
| 1759 | - } |
|
| 1760 | - |
|
| 1761 | - $uuid = false; |
|
| 1762 | - if ($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) { |
|
| 1763 | - $attr = $this->connection->$uuidAttr; |
|
| 1764 | - $uuid = isset($ldapRecord[$attr]) ? $ldapRecord[$attr] : $this->readAttribute($dn, $attr); |
|
| 1765 | - if (!is_array($uuid) |
|
| 1766 | - && $uuidOverride !== '' |
|
| 1767 | - && $this->detectUuidAttribute($dn, $isUser, true, $ldapRecord)) { |
|
| 1768 | - $uuid = isset($ldapRecord[$this->connection->$uuidAttr]) |
|
| 1769 | - ? $ldapRecord[$this->connection->$uuidAttr] |
|
| 1770 | - : $this->readAttribute($dn, $this->connection->$uuidAttr); |
|
| 1771 | - } |
|
| 1772 | - if (is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { |
|
| 1773 | - $uuid = $uuid[0]; |
|
| 1774 | - } |
|
| 1775 | - } |
|
| 1776 | - |
|
| 1777 | - return $uuid; |
|
| 1778 | - } |
|
| 1779 | - |
|
| 1780 | - /** |
|
| 1781 | - * converts a binary ObjectGUID into a string representation |
|
| 1782 | - * |
|
| 1783 | - * @param string $oguid the ObjectGUID in it's binary form as retrieved from AD |
|
| 1784 | - * @return string |
|
| 1785 | - * @link https://www.php.net/manual/en/function.ldap-get-values-len.php#73198 |
|
| 1786 | - */ |
|
| 1787 | - private function convertObjectGUID2Str($oguid) { |
|
| 1788 | - $hex_guid = bin2hex($oguid); |
|
| 1789 | - $hex_guid_to_guid_str = ''; |
|
| 1790 | - for ($k = 1; $k <= 4; ++$k) { |
|
| 1791 | - $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2); |
|
| 1792 | - } |
|
| 1793 | - $hex_guid_to_guid_str .= '-'; |
|
| 1794 | - for ($k = 1; $k <= 2; ++$k) { |
|
| 1795 | - $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2); |
|
| 1796 | - } |
|
| 1797 | - $hex_guid_to_guid_str .= '-'; |
|
| 1798 | - for ($k = 1; $k <= 2; ++$k) { |
|
| 1799 | - $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2); |
|
| 1800 | - } |
|
| 1801 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4); |
|
| 1802 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20); |
|
| 1803 | - |
|
| 1804 | - return strtoupper($hex_guid_to_guid_str); |
|
| 1805 | - } |
|
| 1806 | - |
|
| 1807 | - /** |
|
| 1808 | - * the first three blocks of the string-converted GUID happen to be in |
|
| 1809 | - * reverse order. In order to use it in a filter, this needs to be |
|
| 1810 | - * corrected. Furthermore the dashes need to be replaced and \\ preprended |
|
| 1811 | - * to every two hax figures. |
|
| 1812 | - * |
|
| 1813 | - * If an invalid string is passed, it will be returned without change. |
|
| 1814 | - * |
|
| 1815 | - * @param string $guid |
|
| 1816 | - * @return string |
|
| 1817 | - */ |
|
| 1818 | - public function formatGuid2ForFilterUser($guid) { |
|
| 1819 | - if (!is_string($guid)) { |
|
| 1820 | - throw new \InvalidArgumentException('String expected'); |
|
| 1821 | - } |
|
| 1822 | - $blocks = explode('-', $guid); |
|
| 1823 | - if (count($blocks) !== 5) { |
|
| 1824 | - /* |
|
| 1315 | + $findings = []; |
|
| 1316 | + $savedoffset = $offset; |
|
| 1317 | + $iFoundItems = 0; |
|
| 1318 | + |
|
| 1319 | + do { |
|
| 1320 | + $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
|
| 1321 | + if ($search === false) { |
|
| 1322 | + return []; |
|
| 1323 | + } |
|
| 1324 | + list($sr, $pagedSearchOK) = $search; |
|
| 1325 | + $cr = $this->connection->getConnectionResource(); |
|
| 1326 | + |
|
| 1327 | + if ($skipHandling) { |
|
| 1328 | + //i.e. result do not need to be fetched, we just need the cookie |
|
| 1329 | + //thus pass 1 or any other value as $iFoundItems because it is not |
|
| 1330 | + //used |
|
| 1331 | + $this->processPagedSearchStatus($sr, 1, $limitPerPage, $pagedSearchOK, $skipHandling); |
|
| 1332 | + return []; |
|
| 1333 | + } |
|
| 1334 | + |
|
| 1335 | + $findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $sr)); |
|
| 1336 | + $iFoundItems = max($iFoundItems, $findings['count']); |
|
| 1337 | + unset($findings['count']); |
|
| 1338 | + |
|
| 1339 | + $continue = $this->processPagedSearchStatus($sr, $iFoundItems, $limitPerPage, $pagedSearchOK, $skipHandling); |
|
| 1340 | + $offset += $limitPerPage; |
|
| 1341 | + } while ($continue && $pagedSearchOK && ($limit === null || count($findings) < $limit)); |
|
| 1342 | + |
|
| 1343 | + // resetting offset |
|
| 1344 | + $offset = $savedoffset; |
|
| 1345 | + |
|
| 1346 | + // if we're here, probably no connection resource is returned. |
|
| 1347 | + // to make Nextcloud behave nicely, we simply give back an empty array. |
|
| 1348 | + if (is_null($findings)) { |
|
| 1349 | + return []; |
|
| 1350 | + } |
|
| 1351 | + |
|
| 1352 | + if (!is_null($attr)) { |
|
| 1353 | + $selection = []; |
|
| 1354 | + $i = 0; |
|
| 1355 | + foreach ($findings as $item) { |
|
| 1356 | + if (!is_array($item)) { |
|
| 1357 | + continue; |
|
| 1358 | + } |
|
| 1359 | + $item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8'); |
|
| 1360 | + foreach ($attr as $key) { |
|
| 1361 | + if (isset($item[$key])) { |
|
| 1362 | + if (is_array($item[$key]) && isset($item[$key]['count'])) { |
|
| 1363 | + unset($item[$key]['count']); |
|
| 1364 | + } |
|
| 1365 | + if ($key !== 'dn') { |
|
| 1366 | + if ($this->resemblesDN($key)) { |
|
| 1367 | + $selection[$i][$key] = $this->helper->sanitizeDN($item[$key]); |
|
| 1368 | + } elseif ($key === 'objectguid' || $key === 'guid') { |
|
| 1369 | + $selection[$i][$key] = [$this->convertObjectGUID2Str($item[$key][0])]; |
|
| 1370 | + } else { |
|
| 1371 | + $selection[$i][$key] = $item[$key]; |
|
| 1372 | + } |
|
| 1373 | + } else { |
|
| 1374 | + $selection[$i][$key] = [$this->helper->sanitizeDN($item[$key])]; |
|
| 1375 | + } |
|
| 1376 | + } |
|
| 1377 | + } |
|
| 1378 | + $i++; |
|
| 1379 | + } |
|
| 1380 | + $findings = $selection; |
|
| 1381 | + } |
|
| 1382 | + //we slice the findings, when |
|
| 1383 | + //a) paged search unsuccessful, though attempted |
|
| 1384 | + //b) no paged search, but limit set |
|
| 1385 | + if ((!$this->getPagedSearchResultState() |
|
| 1386 | + && $pagedSearchOK) |
|
| 1387 | + || ( |
|
| 1388 | + !$pagedSearchOK |
|
| 1389 | + && !is_null($limit) |
|
| 1390 | + ) |
|
| 1391 | + ) { |
|
| 1392 | + $findings = array_slice($findings, (int)$offset, $limit); |
|
| 1393 | + } |
|
| 1394 | + return $findings; |
|
| 1395 | + } |
|
| 1396 | + |
|
| 1397 | + /** |
|
| 1398 | + * @param string $name |
|
| 1399 | + * @return string |
|
| 1400 | + * @throws \InvalidArgumentException |
|
| 1401 | + */ |
|
| 1402 | + public function sanitizeUsername($name) { |
|
| 1403 | + $name = trim($name); |
|
| 1404 | + |
|
| 1405 | + if ($this->connection->ldapIgnoreNamingRules) { |
|
| 1406 | + return $name; |
|
| 1407 | + } |
|
| 1408 | + |
|
| 1409 | + // Transliteration to ASCII |
|
| 1410 | + $transliterated = @iconv('UTF-8', 'ASCII//TRANSLIT', $name); |
|
| 1411 | + if ($transliterated !== false) { |
|
| 1412 | + // depending on system config iconv can work or not |
|
| 1413 | + $name = $transliterated; |
|
| 1414 | + } |
|
| 1415 | + |
|
| 1416 | + // Replacements |
|
| 1417 | + $name = str_replace(' ', '_', $name); |
|
| 1418 | + |
|
| 1419 | + // Every remaining disallowed characters will be removed |
|
| 1420 | + $name = preg_replace('/[^a-zA-Z0-9_.@-]/u', '', $name); |
|
| 1421 | + |
|
| 1422 | + if ($name === '') { |
|
| 1423 | + throw new \InvalidArgumentException('provided name template for username does not contain any allowed characters'); |
|
| 1424 | + } |
|
| 1425 | + |
|
| 1426 | + return $name; |
|
| 1427 | + } |
|
| 1428 | + |
|
| 1429 | + /** |
|
| 1430 | + * escapes (user provided) parts for LDAP filter |
|
| 1431 | + * |
|
| 1432 | + * @param string $input , the provided value |
|
| 1433 | + * @param bool $allowAsterisk whether in * at the beginning should be preserved |
|
| 1434 | + * @return string the escaped string |
|
| 1435 | + */ |
|
| 1436 | + public function escapeFilterPart($input, $allowAsterisk = false): string { |
|
| 1437 | + $asterisk = ''; |
|
| 1438 | + if ($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { |
|
| 1439 | + $asterisk = '*'; |
|
| 1440 | + $input = mb_substr($input, 1, null, 'UTF-8'); |
|
| 1441 | + } |
|
| 1442 | + $search = ['*', '\\', '(', ')']; |
|
| 1443 | + $replace = ['\\*', '\\\\', '\\(', '\\)']; |
|
| 1444 | + return $asterisk . str_replace($search, $replace, $input); |
|
| 1445 | + } |
|
| 1446 | + |
|
| 1447 | + /** |
|
| 1448 | + * combines the input filters with AND |
|
| 1449 | + * |
|
| 1450 | + * @param string[] $filters the filters to connect |
|
| 1451 | + * @return string the combined filter |
|
| 1452 | + */ |
|
| 1453 | + public function combineFilterWithAnd($filters): string { |
|
| 1454 | + return $this->combineFilter($filters, '&'); |
|
| 1455 | + } |
|
| 1456 | + |
|
| 1457 | + /** |
|
| 1458 | + * combines the input filters with OR |
|
| 1459 | + * |
|
| 1460 | + * @param string[] $filters the filters to connect |
|
| 1461 | + * @return string the combined filter |
|
| 1462 | + * Combines Filter arguments with OR |
|
| 1463 | + */ |
|
| 1464 | + public function combineFilterWithOr($filters) { |
|
| 1465 | + return $this->combineFilter($filters, '|'); |
|
| 1466 | + } |
|
| 1467 | + |
|
| 1468 | + /** |
|
| 1469 | + * combines the input filters with given operator |
|
| 1470 | + * |
|
| 1471 | + * @param string[] $filters the filters to connect |
|
| 1472 | + * @param string $operator either & or | |
|
| 1473 | + * @return string the combined filter |
|
| 1474 | + */ |
|
| 1475 | + private function combineFilter($filters, $operator) { |
|
| 1476 | + $combinedFilter = '(' . $operator; |
|
| 1477 | + foreach ($filters as $filter) { |
|
| 1478 | + if ($filter !== '' && $filter[0] !== '(') { |
|
| 1479 | + $filter = '(' . $filter . ')'; |
|
| 1480 | + } |
|
| 1481 | + $combinedFilter .= $filter; |
|
| 1482 | + } |
|
| 1483 | + $combinedFilter .= ')'; |
|
| 1484 | + return $combinedFilter; |
|
| 1485 | + } |
|
| 1486 | + |
|
| 1487 | + /** |
|
| 1488 | + * creates a filter part for to perform search for users |
|
| 1489 | + * |
|
| 1490 | + * @param string $search the search term |
|
| 1491 | + * @return string the final filter part to use in LDAP searches |
|
| 1492 | + */ |
|
| 1493 | + public function getFilterPartForUserSearch($search) { |
|
| 1494 | + return $this->getFilterPartForSearch($search, |
|
| 1495 | + $this->connection->ldapAttributesForUserSearch, |
|
| 1496 | + $this->connection->ldapUserDisplayName); |
|
| 1497 | + } |
|
| 1498 | + |
|
| 1499 | + /** |
|
| 1500 | + * creates a filter part for to perform search for groups |
|
| 1501 | + * |
|
| 1502 | + * @param string $search the search term |
|
| 1503 | + * @return string the final filter part to use in LDAP searches |
|
| 1504 | + */ |
|
| 1505 | + public function getFilterPartForGroupSearch($search) { |
|
| 1506 | + return $this->getFilterPartForSearch($search, |
|
| 1507 | + $this->connection->ldapAttributesForGroupSearch, |
|
| 1508 | + $this->connection->ldapGroupDisplayName); |
|
| 1509 | + } |
|
| 1510 | + |
|
| 1511 | + /** |
|
| 1512 | + * creates a filter part for searches by splitting up the given search |
|
| 1513 | + * string into single words |
|
| 1514 | + * |
|
| 1515 | + * @param string $search the search term |
|
| 1516 | + * @param string[] $searchAttributes needs to have at least two attributes, |
|
| 1517 | + * otherwise it does not make sense :) |
|
| 1518 | + * @return string the final filter part to use in LDAP searches |
|
| 1519 | + * @throws DomainException |
|
| 1520 | + */ |
|
| 1521 | + private function getAdvancedFilterPartForSearch($search, $searchAttributes) { |
|
| 1522 | + if (!is_array($searchAttributes) || count($searchAttributes) < 2) { |
|
| 1523 | + throw new DomainException('searchAttributes must be an array with at least two string'); |
|
| 1524 | + } |
|
| 1525 | + $searchWords = explode(' ', trim($search)); |
|
| 1526 | + $wordFilters = []; |
|
| 1527 | + foreach ($searchWords as $word) { |
|
| 1528 | + $word = $this->prepareSearchTerm($word); |
|
| 1529 | + //every word needs to appear at least once |
|
| 1530 | + $wordMatchOneAttrFilters = []; |
|
| 1531 | + foreach ($searchAttributes as $attr) { |
|
| 1532 | + $wordMatchOneAttrFilters[] = $attr . '=' . $word; |
|
| 1533 | + } |
|
| 1534 | + $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters); |
|
| 1535 | + } |
|
| 1536 | + return $this->combineFilterWithAnd($wordFilters); |
|
| 1537 | + } |
|
| 1538 | + |
|
| 1539 | + /** |
|
| 1540 | + * creates a filter part for searches |
|
| 1541 | + * |
|
| 1542 | + * @param string $search the search term |
|
| 1543 | + * @param string[]|null $searchAttributes |
|
| 1544 | + * @param string $fallbackAttribute a fallback attribute in case the user |
|
| 1545 | + * did not define search attributes. Typically the display name attribute. |
|
| 1546 | + * @return string the final filter part to use in LDAP searches |
|
| 1547 | + */ |
|
| 1548 | + private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) { |
|
| 1549 | + $filter = []; |
|
| 1550 | + $haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0); |
|
| 1551 | + if ($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { |
|
| 1552 | + try { |
|
| 1553 | + return $this->getAdvancedFilterPartForSearch($search, $searchAttributes); |
|
| 1554 | + } catch (DomainException $e) { |
|
| 1555 | + // Creating advanced filter for search failed, falling back to simple method. Edge case, but valid. |
|
| 1556 | + } |
|
| 1557 | + } |
|
| 1558 | + |
|
| 1559 | + $search = $this->prepareSearchTerm($search); |
|
| 1560 | + if (!is_array($searchAttributes) || count($searchAttributes) === 0) { |
|
| 1561 | + if ($fallbackAttribute === '') { |
|
| 1562 | + return ''; |
|
| 1563 | + } |
|
| 1564 | + $filter[] = $fallbackAttribute . '=' . $search; |
|
| 1565 | + } else { |
|
| 1566 | + foreach ($searchAttributes as $attribute) { |
|
| 1567 | + $filter[] = $attribute . '=' . $search; |
|
| 1568 | + } |
|
| 1569 | + } |
|
| 1570 | + if (count($filter) === 1) { |
|
| 1571 | + return '(' . $filter[0] . ')'; |
|
| 1572 | + } |
|
| 1573 | + return $this->combineFilterWithOr($filter); |
|
| 1574 | + } |
|
| 1575 | + |
|
| 1576 | + /** |
|
| 1577 | + * returns the search term depending on whether we are allowed |
|
| 1578 | + * list users found by ldap with the current input appended by |
|
| 1579 | + * a * |
|
| 1580 | + * |
|
| 1581 | + * @return string |
|
| 1582 | + */ |
|
| 1583 | + private function prepareSearchTerm($term) { |
|
| 1584 | + $config = \OC::$server->getConfig(); |
|
| 1585 | + |
|
| 1586 | + $allowEnum = $config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes'); |
|
| 1587 | + |
|
| 1588 | + $result = $term; |
|
| 1589 | + if ($term === '') { |
|
| 1590 | + $result = '*'; |
|
| 1591 | + } elseif ($allowEnum !== 'no') { |
|
| 1592 | + $result = $term . '*'; |
|
| 1593 | + } |
|
| 1594 | + return $result; |
|
| 1595 | + } |
|
| 1596 | + |
|
| 1597 | + /** |
|
| 1598 | + * returns the filter used for counting users |
|
| 1599 | + * |
|
| 1600 | + * @return string |
|
| 1601 | + */ |
|
| 1602 | + public function getFilterForUserCount() { |
|
| 1603 | + $filter = $this->combineFilterWithAnd([ |
|
| 1604 | + $this->connection->ldapUserFilter, |
|
| 1605 | + $this->connection->ldapUserDisplayName . '=*' |
|
| 1606 | + ]); |
|
| 1607 | + |
|
| 1608 | + return $filter; |
|
| 1609 | + } |
|
| 1610 | + |
|
| 1611 | + /** |
|
| 1612 | + * @param string $name |
|
| 1613 | + * @param string $password |
|
| 1614 | + * @return bool |
|
| 1615 | + */ |
|
| 1616 | + public function areCredentialsValid($name, $password) { |
|
| 1617 | + $name = $this->helper->DNasBaseParameter($name); |
|
| 1618 | + $testConnection = clone $this->connection; |
|
| 1619 | + $credentials = [ |
|
| 1620 | + 'ldapAgentName' => $name, |
|
| 1621 | + 'ldapAgentPassword' => $password |
|
| 1622 | + ]; |
|
| 1623 | + if (!$testConnection->setConfiguration($credentials)) { |
|
| 1624 | + return false; |
|
| 1625 | + } |
|
| 1626 | + return $testConnection->bind(); |
|
| 1627 | + } |
|
| 1628 | + |
|
| 1629 | + /** |
|
| 1630 | + * reverse lookup of a DN given a known UUID |
|
| 1631 | + * |
|
| 1632 | + * @param string $uuid |
|
| 1633 | + * @return string |
|
| 1634 | + * @throws \Exception |
|
| 1635 | + */ |
|
| 1636 | + public function getUserDnByUuid($uuid) { |
|
| 1637 | + $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1638 | + $filter = $this->connection->ldapUserFilter; |
|
| 1639 | + $bases = $this->connection->ldapBaseUsers; |
|
| 1640 | + |
|
| 1641 | + if ($this->connection->ldapUuidUserAttribute === 'auto' && $uuidOverride === '') { |
|
| 1642 | + // Sacrebleu! The UUID attribute is unknown :( We need first an |
|
| 1643 | + // existing DN to be able to reliably detect it. |
|
| 1644 | + foreach ($bases as $base) { |
|
| 1645 | + $result = $this->search($filter, $base, ['dn'], 1); |
|
| 1646 | + if (!isset($result[0]) || !isset($result[0]['dn'])) { |
|
| 1647 | + continue; |
|
| 1648 | + } |
|
| 1649 | + $dn = $result[0]['dn'][0]; |
|
| 1650 | + if ($hasFound = $this->detectUuidAttribute($dn, true)) { |
|
| 1651 | + break; |
|
| 1652 | + } |
|
| 1653 | + } |
|
| 1654 | + if (!isset($hasFound) || !$hasFound) { |
|
| 1655 | + throw new \Exception('Cannot determine UUID attribute'); |
|
| 1656 | + } |
|
| 1657 | + } else { |
|
| 1658 | + // The UUID attribute is either known or an override is given. |
|
| 1659 | + // By calling this method we ensure that $this->connection->$uuidAttr |
|
| 1660 | + // is definitely set |
|
| 1661 | + if (!$this->detectUuidAttribute('', true)) { |
|
| 1662 | + throw new \Exception('Cannot determine UUID attribute'); |
|
| 1663 | + } |
|
| 1664 | + } |
|
| 1665 | + |
|
| 1666 | + $uuidAttr = $this->connection->ldapUuidUserAttribute; |
|
| 1667 | + if ($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { |
|
| 1668 | + $uuid = $this->formatGuid2ForFilterUser($uuid); |
|
| 1669 | + } |
|
| 1670 | + |
|
| 1671 | + $filter = $uuidAttr . '=' . $uuid; |
|
| 1672 | + $result = $this->searchUsers($filter, ['dn'], 2); |
|
| 1673 | + if (is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { |
|
| 1674 | + // we put the count into account to make sure that this is |
|
| 1675 | + // really unique |
|
| 1676 | + return $result[0]['dn'][0]; |
|
| 1677 | + } |
|
| 1678 | + |
|
| 1679 | + throw new \Exception('Cannot determine UUID attribute'); |
|
| 1680 | + } |
|
| 1681 | + |
|
| 1682 | + /** |
|
| 1683 | + * auto-detects the directory's UUID attribute |
|
| 1684 | + * |
|
| 1685 | + * @param string $dn a known DN used to check against |
|
| 1686 | + * @param bool $isUser |
|
| 1687 | + * @param bool $force the detection should be run, even if it is not set to auto |
|
| 1688 | + * @param array|null $ldapRecord |
|
| 1689 | + * @return bool true on success, false otherwise |
|
| 1690 | + * @throws ServerNotAvailableException |
|
| 1691 | + */ |
|
| 1692 | + private function detectUuidAttribute($dn, $isUser = true, $force = false, array $ldapRecord = null) { |
|
| 1693 | + if ($isUser) { |
|
| 1694 | + $uuidAttr = 'ldapUuidUserAttribute'; |
|
| 1695 | + $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1696 | + } else { |
|
| 1697 | + $uuidAttr = 'ldapUuidGroupAttribute'; |
|
| 1698 | + $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
|
| 1699 | + } |
|
| 1700 | + |
|
| 1701 | + if (!$force) { |
|
| 1702 | + if ($this->connection->$uuidAttr !== 'auto') { |
|
| 1703 | + return true; |
|
| 1704 | + } elseif (is_string($uuidOverride) && trim($uuidOverride) !== '') { |
|
| 1705 | + $this->connection->$uuidAttr = $uuidOverride; |
|
| 1706 | + return true; |
|
| 1707 | + } |
|
| 1708 | + |
|
| 1709 | + $attribute = $this->connection->getFromCache($uuidAttr); |
|
| 1710 | + if (!$attribute === null) { |
|
| 1711 | + $this->connection->$uuidAttr = $attribute; |
|
| 1712 | + return true; |
|
| 1713 | + } |
|
| 1714 | + } |
|
| 1715 | + |
|
| 1716 | + foreach (self::UUID_ATTRIBUTES as $attribute) { |
|
| 1717 | + if ($ldapRecord !== null) { |
|
| 1718 | + // we have the info from LDAP already, we don't need to talk to the server again |
|
| 1719 | + if (isset($ldapRecord[$attribute])) { |
|
| 1720 | + $this->connection->$uuidAttr = $attribute; |
|
| 1721 | + return true; |
|
| 1722 | + } |
|
| 1723 | + } |
|
| 1724 | + |
|
| 1725 | + $value = $this->readAttribute($dn, $attribute); |
|
| 1726 | + if (is_array($value) && isset($value[0]) && !empty($value[0])) { |
|
| 1727 | + \OC::$server->getLogger()->debug( |
|
| 1728 | + 'Setting {attribute} as {subject}', |
|
| 1729 | + [ |
|
| 1730 | + 'app' => 'user_ldap', |
|
| 1731 | + 'attribute' => $attribute, |
|
| 1732 | + 'subject' => $uuidAttr |
|
| 1733 | + ] |
|
| 1734 | + ); |
|
| 1735 | + $this->connection->$uuidAttr = $attribute; |
|
| 1736 | + $this->connection->writeToCache($uuidAttr, $attribute); |
|
| 1737 | + return true; |
|
| 1738 | + } |
|
| 1739 | + } |
|
| 1740 | + \OC::$server->getLogger()->debug('Could not autodetect the UUID attribute', ['app' => 'user_ldap']); |
|
| 1741 | + |
|
| 1742 | + return false; |
|
| 1743 | + } |
|
| 1744 | + |
|
| 1745 | + /** |
|
| 1746 | + * @param string $dn |
|
| 1747 | + * @param bool $isUser |
|
| 1748 | + * @param null $ldapRecord |
|
| 1749 | + * @return bool|string |
|
| 1750 | + * @throws ServerNotAvailableException |
|
| 1751 | + */ |
|
| 1752 | + public function getUUID($dn, $isUser = true, $ldapRecord = null) { |
|
| 1753 | + if ($isUser) { |
|
| 1754 | + $uuidAttr = 'ldapUuidUserAttribute'; |
|
| 1755 | + $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1756 | + } else { |
|
| 1757 | + $uuidAttr = 'ldapUuidGroupAttribute'; |
|
| 1758 | + $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
|
| 1759 | + } |
|
| 1760 | + |
|
| 1761 | + $uuid = false; |
|
| 1762 | + if ($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) { |
|
| 1763 | + $attr = $this->connection->$uuidAttr; |
|
| 1764 | + $uuid = isset($ldapRecord[$attr]) ? $ldapRecord[$attr] : $this->readAttribute($dn, $attr); |
|
| 1765 | + if (!is_array($uuid) |
|
| 1766 | + && $uuidOverride !== '' |
|
| 1767 | + && $this->detectUuidAttribute($dn, $isUser, true, $ldapRecord)) { |
|
| 1768 | + $uuid = isset($ldapRecord[$this->connection->$uuidAttr]) |
|
| 1769 | + ? $ldapRecord[$this->connection->$uuidAttr] |
|
| 1770 | + : $this->readAttribute($dn, $this->connection->$uuidAttr); |
|
| 1771 | + } |
|
| 1772 | + if (is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { |
|
| 1773 | + $uuid = $uuid[0]; |
|
| 1774 | + } |
|
| 1775 | + } |
|
| 1776 | + |
|
| 1777 | + return $uuid; |
|
| 1778 | + } |
|
| 1779 | + |
|
| 1780 | + /** |
|
| 1781 | + * converts a binary ObjectGUID into a string representation |
|
| 1782 | + * |
|
| 1783 | + * @param string $oguid the ObjectGUID in it's binary form as retrieved from AD |
|
| 1784 | + * @return string |
|
| 1785 | + * @link https://www.php.net/manual/en/function.ldap-get-values-len.php#73198 |
|
| 1786 | + */ |
|
| 1787 | + private function convertObjectGUID2Str($oguid) { |
|
| 1788 | + $hex_guid = bin2hex($oguid); |
|
| 1789 | + $hex_guid_to_guid_str = ''; |
|
| 1790 | + for ($k = 1; $k <= 4; ++$k) { |
|
| 1791 | + $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2); |
|
| 1792 | + } |
|
| 1793 | + $hex_guid_to_guid_str .= '-'; |
|
| 1794 | + for ($k = 1; $k <= 2; ++$k) { |
|
| 1795 | + $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2); |
|
| 1796 | + } |
|
| 1797 | + $hex_guid_to_guid_str .= '-'; |
|
| 1798 | + for ($k = 1; $k <= 2; ++$k) { |
|
| 1799 | + $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2); |
|
| 1800 | + } |
|
| 1801 | + $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4); |
|
| 1802 | + $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20); |
|
| 1803 | + |
|
| 1804 | + return strtoupper($hex_guid_to_guid_str); |
|
| 1805 | + } |
|
| 1806 | + |
|
| 1807 | + /** |
|
| 1808 | + * the first three blocks of the string-converted GUID happen to be in |
|
| 1809 | + * reverse order. In order to use it in a filter, this needs to be |
|
| 1810 | + * corrected. Furthermore the dashes need to be replaced and \\ preprended |
|
| 1811 | + * to every two hax figures. |
|
| 1812 | + * |
|
| 1813 | + * If an invalid string is passed, it will be returned without change. |
|
| 1814 | + * |
|
| 1815 | + * @param string $guid |
|
| 1816 | + * @return string |
|
| 1817 | + */ |
|
| 1818 | + public function formatGuid2ForFilterUser($guid) { |
|
| 1819 | + if (!is_string($guid)) { |
|
| 1820 | + throw new \InvalidArgumentException('String expected'); |
|
| 1821 | + } |
|
| 1822 | + $blocks = explode('-', $guid); |
|
| 1823 | + if (count($blocks) !== 5) { |
|
| 1824 | + /* |
|
| 1825 | 1825 | * Why not throw an Exception instead? This method is a utility |
| 1826 | 1826 | * called only when trying to figure out whether a "missing" known |
| 1827 | 1827 | * LDAP user was or was not renamed on the LDAP server. And this |
@@ -1832,246 +1832,246 @@ discard block |
||
| 1832 | 1832 | * an exception here would kill the experience for a valid, acting |
| 1833 | 1833 | * user. Instead we write a log message. |
| 1834 | 1834 | */ |
| 1835 | - \OC::$server->getLogger()->info( |
|
| 1836 | - 'Passed string does not resemble a valid GUID. Known UUID ' . |
|
| 1837 | - '({uuid}) probably does not match UUID configuration.', |
|
| 1838 | - ['app' => 'user_ldap', 'uuid' => $guid] |
|
| 1839 | - ); |
|
| 1840 | - return $guid; |
|
| 1841 | - } |
|
| 1842 | - for ($i = 0; $i < 3; $i++) { |
|
| 1843 | - $pairs = str_split($blocks[$i], 2); |
|
| 1844 | - $pairs = array_reverse($pairs); |
|
| 1845 | - $blocks[$i] = implode('', $pairs); |
|
| 1846 | - } |
|
| 1847 | - for ($i = 0; $i < 5; $i++) { |
|
| 1848 | - $pairs = str_split($blocks[$i], 2); |
|
| 1849 | - $blocks[$i] = '\\' . implode('\\', $pairs); |
|
| 1850 | - } |
|
| 1851 | - return implode('', $blocks); |
|
| 1852 | - } |
|
| 1853 | - |
|
| 1854 | - /** |
|
| 1855 | - * gets a SID of the domain of the given dn |
|
| 1856 | - * |
|
| 1857 | - * @param string $dn |
|
| 1858 | - * @return string|bool |
|
| 1859 | - * @throws ServerNotAvailableException |
|
| 1860 | - */ |
|
| 1861 | - public function getSID($dn) { |
|
| 1862 | - $domainDN = $this->getDomainDNFromDN($dn); |
|
| 1863 | - $cacheKey = 'getSID-' . $domainDN; |
|
| 1864 | - $sid = $this->connection->getFromCache($cacheKey); |
|
| 1865 | - if (!is_null($sid)) { |
|
| 1866 | - return $sid; |
|
| 1867 | - } |
|
| 1868 | - |
|
| 1869 | - $objectSid = $this->readAttribute($domainDN, 'objectsid'); |
|
| 1870 | - if (!is_array($objectSid) || empty($objectSid)) { |
|
| 1871 | - $this->connection->writeToCache($cacheKey, false); |
|
| 1872 | - return false; |
|
| 1873 | - } |
|
| 1874 | - $domainObjectSid = $this->convertSID2Str($objectSid[0]); |
|
| 1875 | - $this->connection->writeToCache($cacheKey, $domainObjectSid); |
|
| 1876 | - |
|
| 1877 | - return $domainObjectSid; |
|
| 1878 | - } |
|
| 1879 | - |
|
| 1880 | - /** |
|
| 1881 | - * converts a binary SID into a string representation |
|
| 1882 | - * |
|
| 1883 | - * @param string $sid |
|
| 1884 | - * @return string |
|
| 1885 | - */ |
|
| 1886 | - public function convertSID2Str($sid) { |
|
| 1887 | - // The format of a SID binary string is as follows: |
|
| 1888 | - // 1 byte for the revision level |
|
| 1889 | - // 1 byte for the number n of variable sub-ids |
|
| 1890 | - // 6 bytes for identifier authority value |
|
| 1891 | - // n*4 bytes for n sub-ids |
|
| 1892 | - // |
|
| 1893 | - // Example: 010400000000000515000000a681e50e4d6c6c2bca32055f |
|
| 1894 | - // Legend: RRNNAAAAAAAAAAAA11111111222222223333333344444444 |
|
| 1895 | - $revision = ord($sid[0]); |
|
| 1896 | - $numberSubID = ord($sid[1]); |
|
| 1897 | - |
|
| 1898 | - $subIdStart = 8; // 1 + 1 + 6 |
|
| 1899 | - $subIdLength = 4; |
|
| 1900 | - if (strlen($sid) !== $subIdStart + $subIdLength * $numberSubID) { |
|
| 1901 | - // Incorrect number of bytes present. |
|
| 1902 | - return ''; |
|
| 1903 | - } |
|
| 1904 | - |
|
| 1905 | - // 6 bytes = 48 bits can be represented using floats without loss of |
|
| 1906 | - // precision (see https://gist.github.com/bantu/886ac680b0aef5812f71) |
|
| 1907 | - $iav = number_format(hexdec(bin2hex(substr($sid, 2, 6))), 0, '', ''); |
|
| 1908 | - |
|
| 1909 | - $subIDs = []; |
|
| 1910 | - for ($i = 0; $i < $numberSubID; $i++) { |
|
| 1911 | - $subID = unpack('V', substr($sid, $subIdStart + $subIdLength * $i, $subIdLength)); |
|
| 1912 | - $subIDs[] = sprintf('%u', $subID[1]); |
|
| 1913 | - } |
|
| 1914 | - |
|
| 1915 | - // Result for example above: S-1-5-21-249921958-728525901-1594176202 |
|
| 1916 | - return sprintf('S-%d-%s-%s', $revision, $iav, implode('-', $subIDs)); |
|
| 1917 | - } |
|
| 1918 | - |
|
| 1919 | - /** |
|
| 1920 | - * checks if the given DN is part of the given base DN(s) |
|
| 1921 | - * |
|
| 1922 | - * @param string $dn the DN |
|
| 1923 | - * @param string[] $bases array containing the allowed base DN or DNs |
|
| 1924 | - * @return bool |
|
| 1925 | - */ |
|
| 1926 | - public function isDNPartOfBase($dn, $bases) { |
|
| 1927 | - $belongsToBase = false; |
|
| 1928 | - $bases = $this->helper->sanitizeDN($bases); |
|
| 1929 | - |
|
| 1930 | - foreach ($bases as $base) { |
|
| 1931 | - $belongsToBase = true; |
|
| 1932 | - if (mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8') - mb_strlen($base, 'UTF-8'))) { |
|
| 1933 | - $belongsToBase = false; |
|
| 1934 | - } |
|
| 1935 | - if ($belongsToBase) { |
|
| 1936 | - break; |
|
| 1937 | - } |
|
| 1938 | - } |
|
| 1939 | - return $belongsToBase; |
|
| 1940 | - } |
|
| 1941 | - |
|
| 1942 | - /** |
|
| 1943 | - * resets a running Paged Search operation |
|
| 1944 | - * |
|
| 1945 | - * @throws ServerNotAvailableException |
|
| 1946 | - */ |
|
| 1947 | - private function abandonPagedSearch() { |
|
| 1948 | - if ($this->lastCookie === '') { |
|
| 1949 | - return; |
|
| 1950 | - } |
|
| 1951 | - $cr = $this->connection->getConnectionResource(); |
|
| 1952 | - $this->invokeLDAPMethod('controlPagedResult', $cr, 0, false); |
|
| 1953 | - $this->getPagedSearchResultState(); |
|
| 1954 | - $this->lastCookie = ''; |
|
| 1955 | - } |
|
| 1956 | - |
|
| 1957 | - /** |
|
| 1958 | - * checks whether an LDAP paged search operation has more pages that can be |
|
| 1959 | - * retrieved, typically when offset and limit are provided. |
|
| 1960 | - * |
|
| 1961 | - * Be very careful to use it: the last cookie value, which is inspected, can |
|
| 1962 | - * be reset by other operations. Best, call it immediately after a search(), |
|
| 1963 | - * searchUsers() or searchGroups() call. count-methods are probably safe as |
|
| 1964 | - * well. Don't rely on it with any fetchList-method. |
|
| 1965 | - * |
|
| 1966 | - * @return bool |
|
| 1967 | - */ |
|
| 1968 | - public function hasMoreResults() { |
|
| 1969 | - if (empty($this->lastCookie) && $this->lastCookie !== '0') { |
|
| 1970 | - // as in RFC 2696, when all results are returned, the cookie will |
|
| 1971 | - // be empty. |
|
| 1972 | - return false; |
|
| 1973 | - } |
|
| 1974 | - |
|
| 1975 | - return true; |
|
| 1976 | - } |
|
| 1977 | - |
|
| 1978 | - /** |
|
| 1979 | - * Check whether the most recent paged search was successful. It flushed the state var. Use it always after a possible paged search. |
|
| 1980 | - * |
|
| 1981 | - * @return boolean|null true on success, null or false otherwise |
|
| 1982 | - */ |
|
| 1983 | - public function getPagedSearchResultState() { |
|
| 1984 | - $result = $this->pagedSearchedSuccessful; |
|
| 1985 | - $this->pagedSearchedSuccessful = null; |
|
| 1986 | - return $result; |
|
| 1987 | - } |
|
| 1988 | - |
|
| 1989 | - /** |
|
| 1990 | - * Prepares a paged search, if possible |
|
| 1991 | - * |
|
| 1992 | - * @param string $filter the LDAP filter for the search |
|
| 1993 | - * @param string[] $bases an array containing the LDAP subtree(s) that shall be searched |
|
| 1994 | - * @param string[] $attr optional, when a certain attribute shall be filtered outside |
|
| 1995 | - * @param int $limit |
|
| 1996 | - * @param int $offset |
|
| 1997 | - * @return bool|true |
|
| 1998 | - * @throws ServerNotAvailableException |
|
| 1999 | - */ |
|
| 2000 | - private function initPagedSearch( |
|
| 2001 | - string $filter, |
|
| 2002 | - string $base, |
|
| 2003 | - ?array $attr, |
|
| 2004 | - int $limit, |
|
| 2005 | - int $offset |
|
| 2006 | - ): bool { |
|
| 2007 | - $pagedSearchOK = false; |
|
| 2008 | - if ($limit !== 0) { |
|
| 2009 | - \OC::$server->getLogger()->debug( |
|
| 2010 | - 'initializing paged search for filter {filter}, base {base}, attr {attr}, limit {limit}, offset {offset}', |
|
| 2011 | - [ |
|
| 2012 | - 'app' => 'user_ldap', |
|
| 2013 | - 'filter' => $filter, |
|
| 2014 | - 'base' => $base, |
|
| 2015 | - 'attr' => $attr, |
|
| 2016 | - 'limit' => $limit, |
|
| 2017 | - 'offset' => $offset |
|
| 2018 | - ] |
|
| 2019 | - ); |
|
| 2020 | - //get the cookie from the search for the previous search, required by LDAP |
|
| 2021 | - if (empty($this->lastCookie) && $this->lastCookie !== "0" && ($offset > 0)) { |
|
| 2022 | - // no cookie known from a potential previous search. We need |
|
| 2023 | - // to start from 0 to come to the desired page. cookie value |
|
| 2024 | - // of '0' is valid, because 389ds |
|
| 2025 | - $reOffset = ($offset - $limit) < 0 ? 0 : $offset - $limit; |
|
| 2026 | - $this->search($filter, $base, $attr, $limit, $reOffset, true); |
|
| 2027 | - if (!$this->hasMoreResults()) { |
|
| 2028 | - // when the cookie is reset with != 0 offset, there are no further |
|
| 2029 | - // results, so stop. |
|
| 2030 | - return false; |
|
| 2031 | - } |
|
| 2032 | - } |
|
| 2033 | - if ($this->lastCookie !== '' && $offset === 0) { |
|
| 2034 | - //since offset = 0, this is a new search. We abandon other searches that might be ongoing. |
|
| 2035 | - $this->abandonPagedSearch(); |
|
| 2036 | - } |
|
| 2037 | - $pagedSearchOK = true === $this->invokeLDAPMethod( |
|
| 2038 | - 'controlPagedResult', $this->connection->getConnectionResource(), $limit, false |
|
| 2039 | - ); |
|
| 2040 | - if ($pagedSearchOK) { |
|
| 2041 | - \OC::$server->getLogger()->debug('Ready for a paged search', ['app' => 'user_ldap']); |
|
| 2042 | - } |
|
| 2043 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1835 | + \OC::$server->getLogger()->info( |
|
| 1836 | + 'Passed string does not resemble a valid GUID. Known UUID ' . |
|
| 1837 | + '({uuid}) probably does not match UUID configuration.', |
|
| 1838 | + ['app' => 'user_ldap', 'uuid' => $guid] |
|
| 1839 | + ); |
|
| 1840 | + return $guid; |
|
| 1841 | + } |
|
| 1842 | + for ($i = 0; $i < 3; $i++) { |
|
| 1843 | + $pairs = str_split($blocks[$i], 2); |
|
| 1844 | + $pairs = array_reverse($pairs); |
|
| 1845 | + $blocks[$i] = implode('', $pairs); |
|
| 1846 | + } |
|
| 1847 | + for ($i = 0; $i < 5; $i++) { |
|
| 1848 | + $pairs = str_split($blocks[$i], 2); |
|
| 1849 | + $blocks[$i] = '\\' . implode('\\', $pairs); |
|
| 1850 | + } |
|
| 1851 | + return implode('', $blocks); |
|
| 1852 | + } |
|
| 1853 | + |
|
| 1854 | + /** |
|
| 1855 | + * gets a SID of the domain of the given dn |
|
| 1856 | + * |
|
| 1857 | + * @param string $dn |
|
| 1858 | + * @return string|bool |
|
| 1859 | + * @throws ServerNotAvailableException |
|
| 1860 | + */ |
|
| 1861 | + public function getSID($dn) { |
|
| 1862 | + $domainDN = $this->getDomainDNFromDN($dn); |
|
| 1863 | + $cacheKey = 'getSID-' . $domainDN; |
|
| 1864 | + $sid = $this->connection->getFromCache($cacheKey); |
|
| 1865 | + if (!is_null($sid)) { |
|
| 1866 | + return $sid; |
|
| 1867 | + } |
|
| 1868 | + |
|
| 1869 | + $objectSid = $this->readAttribute($domainDN, 'objectsid'); |
|
| 1870 | + if (!is_array($objectSid) || empty($objectSid)) { |
|
| 1871 | + $this->connection->writeToCache($cacheKey, false); |
|
| 1872 | + return false; |
|
| 1873 | + } |
|
| 1874 | + $domainObjectSid = $this->convertSID2Str($objectSid[0]); |
|
| 1875 | + $this->connection->writeToCache($cacheKey, $domainObjectSid); |
|
| 1876 | + |
|
| 1877 | + return $domainObjectSid; |
|
| 1878 | + } |
|
| 1879 | + |
|
| 1880 | + /** |
|
| 1881 | + * converts a binary SID into a string representation |
|
| 1882 | + * |
|
| 1883 | + * @param string $sid |
|
| 1884 | + * @return string |
|
| 1885 | + */ |
|
| 1886 | + public function convertSID2Str($sid) { |
|
| 1887 | + // The format of a SID binary string is as follows: |
|
| 1888 | + // 1 byte for the revision level |
|
| 1889 | + // 1 byte for the number n of variable sub-ids |
|
| 1890 | + // 6 bytes for identifier authority value |
|
| 1891 | + // n*4 bytes for n sub-ids |
|
| 1892 | + // |
|
| 1893 | + // Example: 010400000000000515000000a681e50e4d6c6c2bca32055f |
|
| 1894 | + // Legend: RRNNAAAAAAAAAAAA11111111222222223333333344444444 |
|
| 1895 | + $revision = ord($sid[0]); |
|
| 1896 | + $numberSubID = ord($sid[1]); |
|
| 1897 | + |
|
| 1898 | + $subIdStart = 8; // 1 + 1 + 6 |
|
| 1899 | + $subIdLength = 4; |
|
| 1900 | + if (strlen($sid) !== $subIdStart + $subIdLength * $numberSubID) { |
|
| 1901 | + // Incorrect number of bytes present. |
|
| 1902 | + return ''; |
|
| 1903 | + } |
|
| 1904 | + |
|
| 1905 | + // 6 bytes = 48 bits can be represented using floats without loss of |
|
| 1906 | + // precision (see https://gist.github.com/bantu/886ac680b0aef5812f71) |
|
| 1907 | + $iav = number_format(hexdec(bin2hex(substr($sid, 2, 6))), 0, '', ''); |
|
| 1908 | + |
|
| 1909 | + $subIDs = []; |
|
| 1910 | + for ($i = 0; $i < $numberSubID; $i++) { |
|
| 1911 | + $subID = unpack('V', substr($sid, $subIdStart + $subIdLength * $i, $subIdLength)); |
|
| 1912 | + $subIDs[] = sprintf('%u', $subID[1]); |
|
| 1913 | + } |
|
| 1914 | + |
|
| 1915 | + // Result for example above: S-1-5-21-249921958-728525901-1594176202 |
|
| 1916 | + return sprintf('S-%d-%s-%s', $revision, $iav, implode('-', $subIDs)); |
|
| 1917 | + } |
|
| 1918 | + |
|
| 1919 | + /** |
|
| 1920 | + * checks if the given DN is part of the given base DN(s) |
|
| 1921 | + * |
|
| 1922 | + * @param string $dn the DN |
|
| 1923 | + * @param string[] $bases array containing the allowed base DN or DNs |
|
| 1924 | + * @return bool |
|
| 1925 | + */ |
|
| 1926 | + public function isDNPartOfBase($dn, $bases) { |
|
| 1927 | + $belongsToBase = false; |
|
| 1928 | + $bases = $this->helper->sanitizeDN($bases); |
|
| 1929 | + |
|
| 1930 | + foreach ($bases as $base) { |
|
| 1931 | + $belongsToBase = true; |
|
| 1932 | + if (mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8') - mb_strlen($base, 'UTF-8'))) { |
|
| 1933 | + $belongsToBase = false; |
|
| 1934 | + } |
|
| 1935 | + if ($belongsToBase) { |
|
| 1936 | + break; |
|
| 1937 | + } |
|
| 1938 | + } |
|
| 1939 | + return $belongsToBase; |
|
| 1940 | + } |
|
| 1941 | + |
|
| 1942 | + /** |
|
| 1943 | + * resets a running Paged Search operation |
|
| 1944 | + * |
|
| 1945 | + * @throws ServerNotAvailableException |
|
| 1946 | + */ |
|
| 1947 | + private function abandonPagedSearch() { |
|
| 1948 | + if ($this->lastCookie === '') { |
|
| 1949 | + return; |
|
| 1950 | + } |
|
| 1951 | + $cr = $this->connection->getConnectionResource(); |
|
| 1952 | + $this->invokeLDAPMethod('controlPagedResult', $cr, 0, false); |
|
| 1953 | + $this->getPagedSearchResultState(); |
|
| 1954 | + $this->lastCookie = ''; |
|
| 1955 | + } |
|
| 1956 | + |
|
| 1957 | + /** |
|
| 1958 | + * checks whether an LDAP paged search operation has more pages that can be |
|
| 1959 | + * retrieved, typically when offset and limit are provided. |
|
| 1960 | + * |
|
| 1961 | + * Be very careful to use it: the last cookie value, which is inspected, can |
|
| 1962 | + * be reset by other operations. Best, call it immediately after a search(), |
|
| 1963 | + * searchUsers() or searchGroups() call. count-methods are probably safe as |
|
| 1964 | + * well. Don't rely on it with any fetchList-method. |
|
| 1965 | + * |
|
| 1966 | + * @return bool |
|
| 1967 | + */ |
|
| 1968 | + public function hasMoreResults() { |
|
| 1969 | + if (empty($this->lastCookie) && $this->lastCookie !== '0') { |
|
| 1970 | + // as in RFC 2696, when all results are returned, the cookie will |
|
| 1971 | + // be empty. |
|
| 1972 | + return false; |
|
| 1973 | + } |
|
| 1974 | + |
|
| 1975 | + return true; |
|
| 1976 | + } |
|
| 1977 | + |
|
| 1978 | + /** |
|
| 1979 | + * Check whether the most recent paged search was successful. It flushed the state var. Use it always after a possible paged search. |
|
| 1980 | + * |
|
| 1981 | + * @return boolean|null true on success, null or false otherwise |
|
| 1982 | + */ |
|
| 1983 | + public function getPagedSearchResultState() { |
|
| 1984 | + $result = $this->pagedSearchedSuccessful; |
|
| 1985 | + $this->pagedSearchedSuccessful = null; |
|
| 1986 | + return $result; |
|
| 1987 | + } |
|
| 1988 | + |
|
| 1989 | + /** |
|
| 1990 | + * Prepares a paged search, if possible |
|
| 1991 | + * |
|
| 1992 | + * @param string $filter the LDAP filter for the search |
|
| 1993 | + * @param string[] $bases an array containing the LDAP subtree(s) that shall be searched |
|
| 1994 | + * @param string[] $attr optional, when a certain attribute shall be filtered outside |
|
| 1995 | + * @param int $limit |
|
| 1996 | + * @param int $offset |
|
| 1997 | + * @return bool|true |
|
| 1998 | + * @throws ServerNotAvailableException |
|
| 1999 | + */ |
|
| 2000 | + private function initPagedSearch( |
|
| 2001 | + string $filter, |
|
| 2002 | + string $base, |
|
| 2003 | + ?array $attr, |
|
| 2004 | + int $limit, |
|
| 2005 | + int $offset |
|
| 2006 | + ): bool { |
|
| 2007 | + $pagedSearchOK = false; |
|
| 2008 | + if ($limit !== 0) { |
|
| 2009 | + \OC::$server->getLogger()->debug( |
|
| 2010 | + 'initializing paged search for filter {filter}, base {base}, attr {attr}, limit {limit}, offset {offset}', |
|
| 2011 | + [ |
|
| 2012 | + 'app' => 'user_ldap', |
|
| 2013 | + 'filter' => $filter, |
|
| 2014 | + 'base' => $base, |
|
| 2015 | + 'attr' => $attr, |
|
| 2016 | + 'limit' => $limit, |
|
| 2017 | + 'offset' => $offset |
|
| 2018 | + ] |
|
| 2019 | + ); |
|
| 2020 | + //get the cookie from the search for the previous search, required by LDAP |
|
| 2021 | + if (empty($this->lastCookie) && $this->lastCookie !== "0" && ($offset > 0)) { |
|
| 2022 | + // no cookie known from a potential previous search. We need |
|
| 2023 | + // to start from 0 to come to the desired page. cookie value |
|
| 2024 | + // of '0' is valid, because 389ds |
|
| 2025 | + $reOffset = ($offset - $limit) < 0 ? 0 : $offset - $limit; |
|
| 2026 | + $this->search($filter, $base, $attr, $limit, $reOffset, true); |
|
| 2027 | + if (!$this->hasMoreResults()) { |
|
| 2028 | + // when the cookie is reset with != 0 offset, there are no further |
|
| 2029 | + // results, so stop. |
|
| 2030 | + return false; |
|
| 2031 | + } |
|
| 2032 | + } |
|
| 2033 | + if ($this->lastCookie !== '' && $offset === 0) { |
|
| 2034 | + //since offset = 0, this is a new search. We abandon other searches that might be ongoing. |
|
| 2035 | + $this->abandonPagedSearch(); |
|
| 2036 | + } |
|
| 2037 | + $pagedSearchOK = true === $this->invokeLDAPMethod( |
|
| 2038 | + 'controlPagedResult', $this->connection->getConnectionResource(), $limit, false |
|
| 2039 | + ); |
|
| 2040 | + if ($pagedSearchOK) { |
|
| 2041 | + \OC::$server->getLogger()->debug('Ready for a paged search', ['app' => 'user_ldap']); |
|
| 2042 | + } |
|
| 2043 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 2044 | 2044 | * We coudn't get paged searches working with our RHDS for login ($limit = 0), |
| 2045 | 2045 | * due to pages with zero results. |
| 2046 | 2046 | * So we added "&& !empty($this->lastCookie)" to this test to ignore pagination |
| 2047 | 2047 | * if we don't have a previous paged search. |
| 2048 | 2048 | */ |
| 2049 | - } elseif ($limit === 0 && !empty($this->lastCookie)) { |
|
| 2050 | - // a search without limit was requested. However, if we do use |
|
| 2051 | - // Paged Search once, we always must do it. This requires us to |
|
| 2052 | - // initialize it with the configured page size. |
|
| 2053 | - $this->abandonPagedSearch(); |
|
| 2054 | - // in case someone set it to 0 … use 500, otherwise no results will |
|
| 2055 | - // be returned. |
|
| 2056 | - $pageSize = (int)$this->connection->ldapPagingSize > 0 ? (int)$this->connection->ldapPagingSize : 500; |
|
| 2057 | - $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
|
| 2058 | - $this->connection->getConnectionResource(), |
|
| 2059 | - $pageSize, false); |
|
| 2060 | - } |
|
| 2061 | - |
|
| 2062 | - return $pagedSearchOK; |
|
| 2063 | - } |
|
| 2064 | - |
|
| 2065 | - /** |
|
| 2066 | - * Is more than one $attr used for search? |
|
| 2067 | - * |
|
| 2068 | - * @param string|string[]|null $attr |
|
| 2069 | - * @return bool |
|
| 2070 | - */ |
|
| 2071 | - private function manyAttributes($attr): bool { |
|
| 2072 | - if (\is_array($attr)) { |
|
| 2073 | - return \count($attr) > 1; |
|
| 2074 | - } |
|
| 2075 | - return false; |
|
| 2076 | - } |
|
| 2049 | + } elseif ($limit === 0 && !empty($this->lastCookie)) { |
|
| 2050 | + // a search without limit was requested. However, if we do use |
|
| 2051 | + // Paged Search once, we always must do it. This requires us to |
|
| 2052 | + // initialize it with the configured page size. |
|
| 2053 | + $this->abandonPagedSearch(); |
|
| 2054 | + // in case someone set it to 0 … use 500, otherwise no results will |
|
| 2055 | + // be returned. |
|
| 2056 | + $pageSize = (int)$this->connection->ldapPagingSize > 0 ? (int)$this->connection->ldapPagingSize : 500; |
|
| 2057 | + $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
|
| 2058 | + $this->connection->getConnectionResource(), |
|
| 2059 | + $pageSize, false); |
|
| 2060 | + } |
|
| 2061 | + |
|
| 2062 | + return $pagedSearchOK; |
|
| 2063 | + } |
|
| 2064 | + |
|
| 2065 | + /** |
|
| 2066 | + * Is more than one $attr used for search? |
|
| 2067 | + * |
|
| 2068 | + * @param string|string[]|null $attr |
|
| 2069 | + * @return bool |
|
| 2070 | + */ |
|
| 2071 | + private function manyAttributes($attr): bool { |
|
| 2072 | + if (\is_array($attr)) { |
|
| 2073 | + return \count($attr) > 1; |
|
| 2074 | + } |
|
| 2075 | + return false; |
|
| 2076 | + } |
|
| 2077 | 2077 | } |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | $this->abandonPagedSearch(); |
| 200 | 200 | // openLDAP requires that we init a new Paged Search. Not needed by AD, |
| 201 | 201 | // but does not hurt either. |
| 202 | - $pagingSize = (int)$this->connection->ldapPagingSize; |
|
| 202 | + $pagingSize = (int) $this->connection->ldapPagingSize; |
|
| 203 | 203 | // 0 won't result in replies, small numbers may leave out groups |
| 204 | 204 | // (cf. #12306), 500 is default for paging and should work everywhere. |
| 205 | 205 | $maxResults = $pagingSize > 20 ? $pagingSize : 500; |
@@ -240,13 +240,13 @@ discard block |
||
| 240 | 240 | return $values; |
| 241 | 241 | } else { |
| 242 | 242 | $low = $result['rangeHigh'] + 1; |
| 243 | - $attrToRead = $result['attributeName'] . ';range=' . $low . '-*'; |
|
| 243 | + $attrToRead = $result['attributeName'].';range='.$low.'-*'; |
|
| 244 | 244 | $isRangeRequest = true; |
| 245 | 245 | } |
| 246 | 246 | } |
| 247 | 247 | } while ($isRangeRequest); |
| 248 | 248 | |
| 249 | - \OCP\Util::writeLog('user_ldap', 'Requested attribute ' . $attr . ' not found for ' . $dn, ILogger::DEBUG); |
|
| 249 | + \OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, ILogger::DEBUG); |
|
| 250 | 250 | return false; |
| 251 | 251 | } |
| 252 | 252 | |
@@ -270,13 +270,13 @@ discard block |
||
| 270 | 270 | if (!$this->ldap->isResource($rr)) { |
| 271 | 271 | if ($attribute !== '') { |
| 272 | 272 | //do not throw this message on userExists check, irritates |
| 273 | - \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, ILogger::DEBUG); |
|
| 273 | + \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN '.$dn, ILogger::DEBUG); |
|
| 274 | 274 | } |
| 275 | 275 | //in case an error occurs , e.g. object does not exist |
| 276 | 276 | return false; |
| 277 | 277 | } |
| 278 | 278 | if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) { |
| 279 | - \OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', ILogger::DEBUG); |
|
| 279 | + \OCP\Util::writeLog('user_ldap', 'readAttribute: '.$dn.' found', ILogger::DEBUG); |
|
| 280 | 280 | return true; |
| 281 | 281 | } |
| 282 | 282 | $er = $this->invokeLDAPMethod('firstEntry', $cr, $rr); |
@@ -356,7 +356,7 @@ discard block |
||
| 356 | 356 | * @throws \Exception |
| 357 | 357 | */ |
| 358 | 358 | public function setPassword($userDN, $password) { |
| 359 | - if ((int)$this->connection->turnOnPasswordChange !== 1) { |
|
| 359 | + if ((int) $this->connection->turnOnPasswordChange !== 1) { |
|
| 360 | 360 | throw new \Exception('LDAP password changes are disabled.'); |
| 361 | 361 | } |
| 362 | 362 | $cr = $this->connection->getConnectionResource(); |
@@ -370,7 +370,7 @@ discard block |
||
| 370 | 370 | return @$this->invokeLDAPMethod('exopPasswd', $cr, $userDN, '', $password) || |
| 371 | 371 | @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password); |
| 372 | 372 | } catch (ConstraintViolationException $e) { |
| 373 | - throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ') . $e->getMessage(), $e->getCode()); |
|
| 373 | + throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ').$e->getMessage(), $e->getCode()); |
|
| 374 | 374 | } |
| 375 | 375 | } |
| 376 | 376 | |
@@ -536,21 +536,21 @@ discard block |
||
| 536 | 536 | } |
| 537 | 537 | } else { |
| 538 | 538 | //If the UUID can't be detected something is foul. |
| 539 | - \OCP\Util::writeLog('user_ldap', 'Cannot determine UUID for ' . $fdn . '. Skipping.', ILogger::INFO); |
|
| 539 | + \OCP\Util::writeLog('user_ldap', 'Cannot determine UUID for '.$fdn.'. Skipping.', ILogger::INFO); |
|
| 540 | 540 | return false; |
| 541 | 541 | } |
| 542 | 542 | |
| 543 | 543 | if (is_null($ldapName)) { |
| 544 | 544 | $ldapName = $this->readAttribute($fdn, $nameAttribute, $filter); |
| 545 | 545 | if (!isset($ldapName[0]) && empty($ldapName[0])) { |
| 546 | - \OCP\Util::writeLog('user_ldap', 'No or empty name for ' . $fdn . ' with filter ' . $filter . '.', ILogger::DEBUG); |
|
| 546 | + \OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.' with filter '.$filter.'.', ILogger::DEBUG); |
|
| 547 | 547 | return false; |
| 548 | 548 | } |
| 549 | 549 | $ldapName = $ldapName[0]; |
| 550 | 550 | } |
| 551 | 551 | |
| 552 | 552 | if ($isUser) { |
| 553 | - $usernameAttribute = (string)$this->connection->ldapExpertUsernameAttr; |
|
| 553 | + $usernameAttribute = (string) $this->connection->ldapExpertUsernameAttr; |
|
| 554 | 554 | if ($usernameAttribute !== '') { |
| 555 | 555 | $username = $this->readAttribute($fdn, $usernameAttribute); |
| 556 | 556 | $username = $username[0]; |
@@ -602,7 +602,7 @@ discard block |
||
| 602 | 602 | } |
| 603 | 603 | |
| 604 | 604 | //if everything else did not help.. |
| 605 | - \OCP\Util::writeLog('user_ldap', 'Could not create unique name for ' . $fdn . '.', ILogger::INFO); |
|
| 605 | + \OCP\Util::writeLog('user_ldap', 'Could not create unique name for '.$fdn.'.', ILogger::INFO); |
|
| 606 | 606 | return false; |
| 607 | 607 | } |
| 608 | 608 | |
@@ -717,7 +717,7 @@ discard block |
||
| 717 | 717 | * @param string|false $home the home directory path |
| 718 | 718 | */ |
| 719 | 719 | public function cacheUserHome($ocName, $home) { |
| 720 | - $cacheKey = 'getHome' . $ocName; |
|
| 720 | + $cacheKey = 'getHome'.$ocName; |
|
| 721 | 721 | $this->connection->writeToCache($cacheKey, $home); |
| 722 | 722 | } |
| 723 | 723 | |
@@ -727,14 +727,14 @@ discard block |
||
| 727 | 727 | * @param string $ocName the internal Nextcloud username |
| 728 | 728 | */ |
| 729 | 729 | public function cacheUserExists($ocName) { |
| 730 | - $this->connection->writeToCache('userExists' . $ocName, true); |
|
| 730 | + $this->connection->writeToCache('userExists'.$ocName, true); |
|
| 731 | 731 | } |
| 732 | 732 | |
| 733 | 733 | /** |
| 734 | 734 | * caches a group as existing |
| 735 | 735 | */ |
| 736 | 736 | public function cacheGroupExists(string $gid): void { |
| 737 | - $this->connection->writeToCache('groupExists' . $gid, true); |
|
| 737 | + $this->connection->writeToCache('groupExists'.$gid, true); |
|
| 738 | 738 | } |
| 739 | 739 | |
| 740 | 740 | /** |
@@ -752,11 +752,11 @@ discard block |
||
| 752 | 752 | } |
| 753 | 753 | $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
| 754 | 754 | $cacheKeyTrunk = 'getDisplayName'; |
| 755 | - $this->connection->writeToCache($cacheKeyTrunk . $ocName, $displayName); |
|
| 755 | + $this->connection->writeToCache($cacheKeyTrunk.$ocName, $displayName); |
|
| 756 | 756 | } |
| 757 | 757 | |
| 758 | 758 | public function cacheGroupDisplayName(string $ncName, string $displayName): void { |
| 759 | - $cacheKey = 'group_getDisplayName' . $ncName; |
|
| 759 | + $cacheKey = 'group_getDisplayName'.$ncName; |
|
| 760 | 760 | $this->connection->writeToCache($cacheKey, $displayName); |
| 761 | 761 | } |
| 762 | 762 | |
@@ -774,7 +774,7 @@ discard block |
||
| 774 | 774 | //while loop is just a precaution. If a name is not generated within |
| 775 | 775 | //20 attempts, something else is very wrong. Avoids infinite loop. |
| 776 | 776 | while ($attempts < 20) { |
| 777 | - $altName = $name . '_' . rand(1000, 9999); |
|
| 777 | + $altName = $name.'_'.rand(1000, 9999); |
|
| 778 | 778 | if (!$this->ncUserManager->userExists($altName)) { |
| 779 | 779 | return $altName; |
| 780 | 780 | } |
@@ -803,9 +803,9 @@ discard block |
||
| 803 | 803 | } else { |
| 804 | 804 | natsort($usedNames); |
| 805 | 805 | $lastName = array_pop($usedNames); |
| 806 | - $lastNo = (int)substr($lastName, strrpos($lastName, '_') + 1); |
|
| 806 | + $lastNo = (int) substr($lastName, strrpos($lastName, '_') + 1); |
|
| 807 | 807 | } |
| 808 | - $altName = $name . '_' . (string)($lastNo + 1); |
|
| 808 | + $altName = $name.'_'.(string) ($lastNo + 1); |
|
| 809 | 809 | unset($usedNames); |
| 810 | 810 | |
| 811 | 811 | $attempts = 1; |
@@ -816,7 +816,7 @@ discard block |
||
| 816 | 816 | if (!\OC::$server->getGroupManager()->groupExists($altName)) { |
| 817 | 817 | return $altName; |
| 818 | 818 | } |
| 819 | - $altName = $name . '_' . ($lastNo + $attempts); |
|
| 819 | + $altName = $name.'_'.($lastNo + $attempts); |
|
| 820 | 820 | $attempts++; |
| 821 | 821 | } |
| 822 | 822 | return false; |
@@ -874,12 +874,12 @@ discard block |
||
| 874 | 874 | if (!$forceApplyAttributes) { |
| 875 | 875 | $isBackgroundJobModeAjax = $this->config |
| 876 | 876 | ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; |
| 877 | - $listOfDNs = array_reduce($ldapRecords, function ($listOfDNs, $entry) { |
|
| 877 | + $listOfDNs = array_reduce($ldapRecords, function($listOfDNs, $entry) { |
|
| 878 | 878 | $listOfDNs[] = $entry['dn'][0]; |
| 879 | 879 | return $listOfDNs; |
| 880 | 880 | }, []); |
| 881 | 881 | $idsByDn = $this->userMapper->getListOfIdsByDn($listOfDNs); |
| 882 | - $recordsToUpdate = array_filter($ldapRecords, function ($record) use ($isBackgroundJobModeAjax, $idsByDn) { |
|
| 882 | + $recordsToUpdate = array_filter($ldapRecords, function($record) use ($isBackgroundJobModeAjax, $idsByDn) { |
|
| 883 | 883 | $newlyMapped = false; |
| 884 | 884 | $uid = $idsByDn[$record['dn'][0]] ?? null; |
| 885 | 885 | if ($uid === null) { |
@@ -937,13 +937,13 @@ discard block |
||
| 937 | 937 | public function fetchListOfGroups($filter, $attr, $limit = null, $offset = null) { |
| 938 | 938 | $groupRecords = $this->searchGroups($filter, $attr, $limit, $offset); |
| 939 | 939 | |
| 940 | - $listOfDNs = array_reduce($groupRecords, function ($listOfDNs, $entry) { |
|
| 940 | + $listOfDNs = array_reduce($groupRecords, function($listOfDNs, $entry) { |
|
| 941 | 941 | $listOfDNs[] = $entry['dn'][0]; |
| 942 | 942 | return $listOfDNs; |
| 943 | 943 | }, []); |
| 944 | 944 | $idsByDn = $this->groupMapper->getListOfIdsByDn($listOfDNs); |
| 945 | 945 | |
| 946 | - array_walk($groupRecords, function ($record) use ($idsByDn) { |
|
| 946 | + array_walk($groupRecords, function($record) use ($idsByDn) { |
|
| 947 | 947 | $newlyMapped = false; |
| 948 | 948 | $gid = $idsByDn[$record['dn'][0]] ?? null; |
| 949 | 949 | if ($gid === null) { |
@@ -966,7 +966,7 @@ discard block |
||
| 966 | 966 | if ($manyAttributes) { |
| 967 | 967 | return $list; |
| 968 | 968 | } else { |
| 969 | - $list = array_reduce($list, function ($carry, $item) { |
|
| 969 | + $list = array_reduce($list, function($carry, $item) { |
|
| 970 | 970 | $attribute = array_keys($item)[0]; |
| 971 | 971 | $carry[] = $item[$attribute][0]; |
| 972 | 972 | return $carry; |
@@ -1002,7 +1002,7 @@ discard block |
||
| 1002 | 1002 | $result = false; |
| 1003 | 1003 | foreach ($this->connection->ldapBaseUsers as $base) { |
| 1004 | 1004 | $count = $this->count($filter, [$base], $attr, $limit, $offset); |
| 1005 | - $result = is_int($count) ? (int)$result + $count : $result; |
|
| 1005 | + $result = is_int($count) ? (int) $result + $count : $result; |
|
| 1006 | 1006 | } |
| 1007 | 1007 | return $result; |
| 1008 | 1008 | } |
@@ -1041,7 +1041,7 @@ discard block |
||
| 1041 | 1041 | $result = false; |
| 1042 | 1042 | foreach ($this->connection->ldapBaseGroups as $base) { |
| 1043 | 1043 | $count = $this->count($filter, [$base], $attr, $limit, $offset); |
| 1044 | - $result = is_int($count) ? (int)$result + $count : $result; |
|
| 1044 | + $result = is_int($count) ? (int) $result + $count : $result; |
|
| 1045 | 1045 | } |
| 1046 | 1046 | return $result; |
| 1047 | 1047 | } |
@@ -1058,7 +1058,7 @@ discard block |
||
| 1058 | 1058 | $result = false; |
| 1059 | 1059 | foreach ($this->connection->ldapBase as $base) { |
| 1060 | 1060 | $count = $this->count('objectclass=*', [$base], ['dn'], $limit, $offset); |
| 1061 | - $result = is_int($count) ? (int)$result + $count : $result; |
|
| 1061 | + $result = is_int($count) ? (int) $result + $count : $result; |
|
| 1062 | 1062 | } |
| 1063 | 1063 | return $result; |
| 1064 | 1064 | } |
@@ -1084,7 +1084,7 @@ discard block |
||
| 1084 | 1084 | // php no longer supports call-time pass-by-reference |
| 1085 | 1085 | // thus cannot support controlPagedResultResponse as the third argument |
| 1086 | 1086 | // is a reference |
| 1087 | - $doMethod = function () use ($command, &$arguments) { |
|
| 1087 | + $doMethod = function() use ($command, &$arguments) { |
|
| 1088 | 1088 | if ($command == 'controlPagedResultResponse') { |
| 1089 | 1089 | throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.'); |
| 1090 | 1090 | } else { |
@@ -1143,13 +1143,13 @@ discard block |
||
| 1143 | 1143 | } |
| 1144 | 1144 | |
| 1145 | 1145 | //check whether paged search should be attempted |
| 1146 | - $pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, (int)$limit, (int)$offset); |
|
| 1146 | + $pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, (int) $limit, (int) $offset); |
|
| 1147 | 1147 | |
| 1148 | 1148 | $sr = $this->invokeLDAPMethod('search', $cr, $base, $filter, $attr); |
| 1149 | 1149 | // cannot use $cr anymore, might have changed in the previous call! |
| 1150 | 1150 | $error = $this->ldap->errno($this->connection->getConnectionResource()); |
| 1151 | 1151 | if (!$this->ldap->isResource($sr) || $error !== 0) { |
| 1152 | - \OCP\Util::writeLog('user_ldap', 'Attempt for Paging? ' . print_r($pagedSearchOK, true), ILogger::ERROR); |
|
| 1152 | + \OCP\Util::writeLog('user_ldap', 'Attempt for Paging? '.print_r($pagedSearchOK, true), ILogger::ERROR); |
|
| 1153 | 1153 | return false; |
| 1154 | 1154 | } |
| 1155 | 1155 | |
@@ -1193,7 +1193,7 @@ discard block |
||
| 1193 | 1193 | $this->pagedSearchedSuccessful = true; |
| 1194 | 1194 | } |
| 1195 | 1195 | } else { |
| 1196 | - if (!is_null($limit) && (int)$this->connection->ldapPagingSize !== 0) { |
|
| 1196 | + if (!is_null($limit) && (int) $this->connection->ldapPagingSize !== 0) { |
|
| 1197 | 1197 | \OC::$server->getLogger()->debug( |
| 1198 | 1198 | 'Paged search was not available', |
| 1199 | 1199 | ['app' => 'user_ldap'] |
@@ -1239,7 +1239,7 @@ discard block |
||
| 1239 | 1239 | $attr = [mb_strtolower($attr, 'UTF-8')]; |
| 1240 | 1240 | } |
| 1241 | 1241 | |
| 1242 | - $limitPerPage = (int)$this->connection->ldapPagingSize; |
|
| 1242 | + $limitPerPage = (int) $this->connection->ldapPagingSize; |
|
| 1243 | 1243 | if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
| 1244 | 1244 | $limitPerPage = $limit; |
| 1245 | 1245 | } |
@@ -1281,7 +1281,7 @@ discard block |
||
| 1281 | 1281 | * @throws ServerNotAvailableException |
| 1282 | 1282 | */ |
| 1283 | 1283 | private function countEntriesInSearchResults($sr): int { |
| 1284 | - return (int)$this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $sr); |
|
| 1284 | + return (int) $this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $sr); |
|
| 1285 | 1285 | } |
| 1286 | 1286 | |
| 1287 | 1287 | /** |
@@ -1297,7 +1297,7 @@ discard block |
||
| 1297 | 1297 | ?int $offset = null, |
| 1298 | 1298 | bool $skipHandling = false |
| 1299 | 1299 | ): array { |
| 1300 | - $limitPerPage = (int)$this->connection->ldapPagingSize; |
|
| 1300 | + $limitPerPage = (int) $this->connection->ldapPagingSize; |
|
| 1301 | 1301 | if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
| 1302 | 1302 | $limitPerPage = $limit; |
| 1303 | 1303 | } |
@@ -1389,7 +1389,7 @@ discard block |
||
| 1389 | 1389 | && !is_null($limit) |
| 1390 | 1390 | ) |
| 1391 | 1391 | ) { |
| 1392 | - $findings = array_slice($findings, (int)$offset, $limit); |
|
| 1392 | + $findings = array_slice($findings, (int) $offset, $limit); |
|
| 1393 | 1393 | } |
| 1394 | 1394 | return $findings; |
| 1395 | 1395 | } |
@@ -1441,7 +1441,7 @@ discard block |
||
| 1441 | 1441 | } |
| 1442 | 1442 | $search = ['*', '\\', '(', ')']; |
| 1443 | 1443 | $replace = ['\\*', '\\\\', '\\(', '\\)']; |
| 1444 | - return $asterisk . str_replace($search, $replace, $input); |
|
| 1444 | + return $asterisk.str_replace($search, $replace, $input); |
|
| 1445 | 1445 | } |
| 1446 | 1446 | |
| 1447 | 1447 | /** |
@@ -1473,10 +1473,10 @@ discard block |
||
| 1473 | 1473 | * @return string the combined filter |
| 1474 | 1474 | */ |
| 1475 | 1475 | private function combineFilter($filters, $operator) { |
| 1476 | - $combinedFilter = '(' . $operator; |
|
| 1476 | + $combinedFilter = '('.$operator; |
|
| 1477 | 1477 | foreach ($filters as $filter) { |
| 1478 | 1478 | if ($filter !== '' && $filter[0] !== '(') { |
| 1479 | - $filter = '(' . $filter . ')'; |
|
| 1479 | + $filter = '('.$filter.')'; |
|
| 1480 | 1480 | } |
| 1481 | 1481 | $combinedFilter .= $filter; |
| 1482 | 1482 | } |
@@ -1529,7 +1529,7 @@ discard block |
||
| 1529 | 1529 | //every word needs to appear at least once |
| 1530 | 1530 | $wordMatchOneAttrFilters = []; |
| 1531 | 1531 | foreach ($searchAttributes as $attr) { |
| 1532 | - $wordMatchOneAttrFilters[] = $attr . '=' . $word; |
|
| 1532 | + $wordMatchOneAttrFilters[] = $attr.'='.$word; |
|
| 1533 | 1533 | } |
| 1534 | 1534 | $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters); |
| 1535 | 1535 | } |
@@ -1561,14 +1561,14 @@ discard block |
||
| 1561 | 1561 | if ($fallbackAttribute === '') { |
| 1562 | 1562 | return ''; |
| 1563 | 1563 | } |
| 1564 | - $filter[] = $fallbackAttribute . '=' . $search; |
|
| 1564 | + $filter[] = $fallbackAttribute.'='.$search; |
|
| 1565 | 1565 | } else { |
| 1566 | 1566 | foreach ($searchAttributes as $attribute) { |
| 1567 | - $filter[] = $attribute . '=' . $search; |
|
| 1567 | + $filter[] = $attribute.'='.$search; |
|
| 1568 | 1568 | } |
| 1569 | 1569 | } |
| 1570 | 1570 | if (count($filter) === 1) { |
| 1571 | - return '(' . $filter[0] . ')'; |
|
| 1571 | + return '('.$filter[0].')'; |
|
| 1572 | 1572 | } |
| 1573 | 1573 | return $this->combineFilterWithOr($filter); |
| 1574 | 1574 | } |
@@ -1589,7 +1589,7 @@ discard block |
||
| 1589 | 1589 | if ($term === '') { |
| 1590 | 1590 | $result = '*'; |
| 1591 | 1591 | } elseif ($allowEnum !== 'no') { |
| 1592 | - $result = $term . '*'; |
|
| 1592 | + $result = $term.'*'; |
|
| 1593 | 1593 | } |
| 1594 | 1594 | return $result; |
| 1595 | 1595 | } |
@@ -1602,7 +1602,7 @@ discard block |
||
| 1602 | 1602 | public function getFilterForUserCount() { |
| 1603 | 1603 | $filter = $this->combineFilterWithAnd([ |
| 1604 | 1604 | $this->connection->ldapUserFilter, |
| 1605 | - $this->connection->ldapUserDisplayName . '=*' |
|
| 1605 | + $this->connection->ldapUserDisplayName.'=*' |
|
| 1606 | 1606 | ]); |
| 1607 | 1607 | |
| 1608 | 1608 | return $filter; |
@@ -1668,7 +1668,7 @@ discard block |
||
| 1668 | 1668 | $uuid = $this->formatGuid2ForFilterUser($uuid); |
| 1669 | 1669 | } |
| 1670 | 1670 | |
| 1671 | - $filter = $uuidAttr . '=' . $uuid; |
|
| 1671 | + $filter = $uuidAttr.'='.$uuid; |
|
| 1672 | 1672 | $result = $this->searchUsers($filter, ['dn'], 2); |
| 1673 | 1673 | if (is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { |
| 1674 | 1674 | // we put the count into account to make sure that this is |
@@ -1798,8 +1798,8 @@ discard block |
||
| 1798 | 1798 | for ($k = 1; $k <= 2; ++$k) { |
| 1799 | 1799 | $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2); |
| 1800 | 1800 | } |
| 1801 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4); |
|
| 1802 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20); |
|
| 1801 | + $hex_guid_to_guid_str .= '-'.substr($hex_guid, 16, 4); |
|
| 1802 | + $hex_guid_to_guid_str .= '-'.substr($hex_guid, 20); |
|
| 1803 | 1803 | |
| 1804 | 1804 | return strtoupper($hex_guid_to_guid_str); |
| 1805 | 1805 | } |
@@ -1833,7 +1833,7 @@ discard block |
||
| 1833 | 1833 | * user. Instead we write a log message. |
| 1834 | 1834 | */ |
| 1835 | 1835 | \OC::$server->getLogger()->info( |
| 1836 | - 'Passed string does not resemble a valid GUID. Known UUID ' . |
|
| 1836 | + 'Passed string does not resemble a valid GUID. Known UUID '. |
|
| 1837 | 1837 | '({uuid}) probably does not match UUID configuration.', |
| 1838 | 1838 | ['app' => 'user_ldap', 'uuid' => $guid] |
| 1839 | 1839 | ); |
@@ -1846,7 +1846,7 @@ discard block |
||
| 1846 | 1846 | } |
| 1847 | 1847 | for ($i = 0; $i < 5; $i++) { |
| 1848 | 1848 | $pairs = str_split($blocks[$i], 2); |
| 1849 | - $blocks[$i] = '\\' . implode('\\', $pairs); |
|
| 1849 | + $blocks[$i] = '\\'.implode('\\', $pairs); |
|
| 1850 | 1850 | } |
| 1851 | 1851 | return implode('', $blocks); |
| 1852 | 1852 | } |
@@ -1860,7 +1860,7 @@ discard block |
||
| 1860 | 1860 | */ |
| 1861 | 1861 | public function getSID($dn) { |
| 1862 | 1862 | $domainDN = $this->getDomainDNFromDN($dn); |
| 1863 | - $cacheKey = 'getSID-' . $domainDN; |
|
| 1863 | + $cacheKey = 'getSID-'.$domainDN; |
|
| 1864 | 1864 | $sid = $this->connection->getFromCache($cacheKey); |
| 1865 | 1865 | if (!is_null($sid)) { |
| 1866 | 1866 | return $sid; |
@@ -2053,7 +2053,7 @@ discard block |
||
| 2053 | 2053 | $this->abandonPagedSearch(); |
| 2054 | 2054 | // in case someone set it to 0 … use 500, otherwise no results will |
| 2055 | 2055 | // be returned. |
| 2056 | - $pageSize = (int)$this->connection->ldapPagingSize > 0 ? (int)$this->connection->ldapPagingSize : 500; |
|
| 2056 | + $pageSize = (int) $this->connection->ldapPagingSize > 0 ? (int) $this->connection->ldapPagingSize : 500; |
|
| 2057 | 2057 | $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
| 2058 | 2058 | $this->connection->getConnectionResource(), |
| 2059 | 2059 | $pageSize, false); |