@@ -41,1043 +41,1043 @@ |
||
| 41 | 41 | use OC\Cache\CappedMemoryCache; |
| 42 | 42 | |
| 43 | 43 | class Group_LDAP extends BackendUtility implements \OCP\GroupInterface { |
| 44 | - protected $enabled = false; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var string[] $cachedGroupMembers array of users with gid as key |
|
| 48 | - */ |
|
| 49 | - protected $cachedGroupMembers; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var string[] $cachedGroupsByMember array of groups with uid as key |
|
| 53 | - */ |
|
| 54 | - protected $cachedGroupsByMember; |
|
| 55 | - |
|
| 56 | - public function __construct(Access $access) { |
|
| 57 | - parent::__construct($access); |
|
| 58 | - $filter = $this->access->connection->ldapGroupFilter; |
|
| 59 | - $gassoc = $this->access->connection->ldapGroupMemberAssocAttr; |
|
| 60 | - if(!empty($filter) && !empty($gassoc)) { |
|
| 61 | - $this->enabled = true; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - $this->cachedGroupMembers = new CappedMemoryCache(); |
|
| 65 | - $this->cachedGroupsByMember = new CappedMemoryCache(); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * is user in group? |
|
| 70 | - * @param string $uid uid of the user |
|
| 71 | - * @param string $gid gid of the group |
|
| 72 | - * @return bool |
|
| 73 | - * |
|
| 74 | - * Checks whether the user is member of a group or not. |
|
| 75 | - */ |
|
| 76 | - public function inGroup($uid, $gid) { |
|
| 77 | - if(!$this->enabled) { |
|
| 78 | - return false; |
|
| 79 | - } |
|
| 80 | - $cacheKey = 'inGroup'.$uid.':'.$gid; |
|
| 81 | - $inGroup = $this->access->connection->getFromCache($cacheKey); |
|
| 82 | - if(!is_null($inGroup)) { |
|
| 83 | - return (bool)$inGroup; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - $userDN = $this->access->username2dn($uid); |
|
| 87 | - |
|
| 88 | - if(isset($this->cachedGroupMembers[$gid])) { |
|
| 89 | - $isInGroup = in_array($userDN, $this->cachedGroupMembers[$gid]); |
|
| 90 | - return $isInGroup; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - $cacheKeyMembers = 'inGroup-members:'.$gid; |
|
| 94 | - $members = $this->access->connection->getFromCache($cacheKeyMembers); |
|
| 95 | - if(!is_null($members)) { |
|
| 96 | - $this->cachedGroupMembers[$gid] = $members; |
|
| 97 | - $isInGroup = in_array($userDN, $members); |
|
| 98 | - $this->access->connection->writeToCache($cacheKey, $isInGroup); |
|
| 99 | - return $isInGroup; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - $groupDN = $this->access->groupname2dn($gid); |
|
| 103 | - // just in case |
|
| 104 | - if(!$groupDN || !$userDN) { |
|
| 105 | - $this->access->connection->writeToCache($cacheKey, false); |
|
| 106 | - return false; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - //check primary group first |
|
| 110 | - if($gid === $this->getUserPrimaryGroup($userDN)) { |
|
| 111 | - $this->access->connection->writeToCache($cacheKey, true); |
|
| 112 | - return true; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course. |
|
| 116 | - $members = $this->_groupMembers($groupDN); |
|
| 117 | - $members = array_keys($members); // uids are returned as keys |
|
| 118 | - if(!is_array($members) || count($members) === 0) { |
|
| 119 | - $this->access->connection->writeToCache($cacheKey, false); |
|
| 120 | - return false; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - //extra work if we don't get back user DNs |
|
| 124 | - if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 125 | - $dns = array(); |
|
| 126 | - $filterParts = array(); |
|
| 127 | - $bytes = 0; |
|
| 128 | - foreach($members as $mid) { |
|
| 129 | - $filter = str_replace('%uid', $mid, $this->access->connection->ldapLoginFilter); |
|
| 130 | - $filterParts[] = $filter; |
|
| 131 | - $bytes += strlen($filter); |
|
| 132 | - if($bytes >= 9000000) { |
|
| 133 | - // AD has a default input buffer of 10 MB, we do not want |
|
| 134 | - // to take even the chance to exceed it |
|
| 135 | - $filter = $this->access->combineFilterWithOr($filterParts); |
|
| 136 | - $bytes = 0; |
|
| 137 | - $filterParts = array(); |
|
| 138 | - $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
|
| 139 | - $dns = array_merge($dns, $users); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - if(count($filterParts) > 0) { |
|
| 143 | - $filter = $this->access->combineFilterWithOr($filterParts); |
|
| 144 | - $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
|
| 145 | - $dns = array_merge($dns, $users); |
|
| 146 | - } |
|
| 147 | - $members = $dns; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - $isInGroup = in_array($userDN, $members); |
|
| 151 | - $this->access->connection->writeToCache($cacheKey, $isInGroup); |
|
| 152 | - $this->access->connection->writeToCache($cacheKeyMembers, $members); |
|
| 153 | - $this->cachedGroupMembers[$gid] = $members; |
|
| 154 | - |
|
| 155 | - return $isInGroup; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * @param string $dnGroup |
|
| 160 | - * @return array |
|
| 161 | - * |
|
| 162 | - * For a group that has user membership defined by an LDAP search url attribute returns the users |
|
| 163 | - * that match the search url otherwise returns an empty array. |
|
| 164 | - */ |
|
| 165 | - public function getDynamicGroupMembers($dnGroup) { |
|
| 166 | - $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL); |
|
| 167 | - |
|
| 168 | - if (empty($dynamicGroupMemberURL)) { |
|
| 169 | - return array(); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - $dynamicMembers = array(); |
|
| 173 | - $memberURLs = $this->access->readAttribute( |
|
| 174 | - $dnGroup, |
|
| 175 | - $dynamicGroupMemberURL, |
|
| 176 | - $this->access->connection->ldapGroupFilter |
|
| 177 | - ); |
|
| 178 | - if ($memberURLs !== false) { |
|
| 179 | - // this group has the 'memberURL' attribute so this is a dynamic group |
|
| 180 | - // example 1: ldap:///cn=users,cn=accounts,dc=dcsubbase,dc=dcbase??one?(o=HeadOffice) |
|
| 181 | - // example 2: ldap:///cn=users,cn=accounts,dc=dcsubbase,dc=dcbase??one?(&(o=HeadOffice)(uidNumber>=500)) |
|
| 182 | - $pos = strpos($memberURLs[0], '('); |
|
| 183 | - if ($pos !== false) { |
|
| 184 | - $memberUrlFilter = substr($memberURLs[0], $pos); |
|
| 185 | - $foundMembers = $this->access->searchUsers($memberUrlFilter,'dn'); |
|
| 186 | - $dynamicMembers = array(); |
|
| 187 | - foreach($foundMembers as $value) { |
|
| 188 | - $dynamicMembers[$value['dn'][0]] = 1; |
|
| 189 | - } |
|
| 190 | - } else { |
|
| 191 | - \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
|
| 192 | - 'of group ' . $dnGroup, \OCP\Util::DEBUG); |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - return $dynamicMembers; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * @param string $dnGroup |
|
| 200 | - * @param array|null &$seen |
|
| 201 | - * @return array|mixed|null |
|
| 202 | - */ |
|
| 203 | - private function _groupMembers($dnGroup, &$seen = null) { |
|
| 204 | - if ($seen === null) { |
|
| 205 | - $seen = array(); |
|
| 206 | - } |
|
| 207 | - $allMembers = array(); |
|
| 208 | - if (array_key_exists($dnGroup, $seen)) { |
|
| 209 | - // avoid loops |
|
| 210 | - return array(); |
|
| 211 | - } |
|
| 212 | - // used extensively in cron job, caching makes sense for nested groups |
|
| 213 | - $cacheKey = '_groupMembers'.$dnGroup; |
|
| 214 | - $groupMembers = $this->access->connection->getFromCache($cacheKey); |
|
| 215 | - if(!is_null($groupMembers)) { |
|
| 216 | - return $groupMembers; |
|
| 217 | - } |
|
| 218 | - $seen[$dnGroup] = 1; |
|
| 219 | - $members = $this->access->readAttribute($dnGroup, $this->access->connection->ldapGroupMemberAssocAttr, |
|
| 220 | - $this->access->connection->ldapGroupFilter); |
|
| 221 | - if (is_array($members)) { |
|
| 222 | - foreach ($members as $memberDN) { |
|
| 223 | - $allMembers[$memberDN] = 1; |
|
| 224 | - $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 225 | - if (!empty($nestedGroups)) { |
|
| 226 | - $subMembers = $this->_groupMembers($memberDN, $seen); |
|
| 227 | - if ($subMembers) { |
|
| 228 | - $allMembers = array_merge($allMembers, $subMembers); |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - $allMembers = array_merge($allMembers, $this->getDynamicGroupMembers($dnGroup)); |
|
| 235 | - |
|
| 236 | - $this->access->connection->writeToCache($cacheKey, $allMembers); |
|
| 237 | - return $allMembers; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * @param string $DN |
|
| 242 | - * @param array|null &$seen |
|
| 243 | - * @return array |
|
| 244 | - */ |
|
| 245 | - private function _getGroupDNsFromMemberOf($DN, &$seen = null) { |
|
| 246 | - if ($seen === null) { |
|
| 247 | - $seen = array(); |
|
| 248 | - } |
|
| 249 | - if (array_key_exists($DN, $seen)) { |
|
| 250 | - // avoid loops |
|
| 251 | - return array(); |
|
| 252 | - } |
|
| 253 | - $seen[$DN] = 1; |
|
| 254 | - $groups = $this->access->readAttribute($DN, 'memberOf'); |
|
| 255 | - if (!is_array($groups)) { |
|
| 256 | - return array(); |
|
| 257 | - } |
|
| 258 | - $groups = $this->access->groupsMatchFilter($groups); |
|
| 259 | - $allGroups = $groups; |
|
| 260 | - $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 261 | - if (intval($nestedGroups) === 1) { |
|
| 262 | - foreach ($groups as $group) { |
|
| 263 | - $subGroups = $this->_getGroupDNsFromMemberOf($group, $seen); |
|
| 264 | - $allGroups = array_merge($allGroups, $subGroups); |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - return $allGroups; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * translates a gidNumber into an ownCloud internal name |
|
| 272 | - * @param string $gid as given by gidNumber on POSIX LDAP |
|
| 273 | - * @param string $dn a DN that belongs to the same domain as the group |
|
| 274 | - * @return string|bool |
|
| 275 | - */ |
|
| 276 | - public function gidNumber2Name($gid, $dn) { |
|
| 277 | - $cacheKey = 'gidNumberToName' . $gid; |
|
| 278 | - $groupName = $this->access->connection->getFromCache($cacheKey); |
|
| 279 | - if(!is_null($groupName) && isset($groupName)) { |
|
| 280 | - return $groupName; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - //we need to get the DN from LDAP |
|
| 284 | - $filter = $this->access->combineFilterWithAnd([ |
|
| 285 | - $this->access->connection->ldapGroupFilter, |
|
| 286 | - 'objectClass=posixGroup', |
|
| 287 | - $this->access->connection->ldapGidNumber . '=' . $gid |
|
| 288 | - ]); |
|
| 289 | - $result = $this->access->searchGroups($filter, array('dn'), 1); |
|
| 290 | - if(empty($result)) { |
|
| 291 | - return false; |
|
| 292 | - } |
|
| 293 | - $dn = $result[0]['dn'][0]; |
|
| 294 | - |
|
| 295 | - //and now the group name |
|
| 296 | - //NOTE once we have separate ownCloud group IDs and group names we can |
|
| 297 | - //directly read the display name attribute instead of the DN |
|
| 298 | - $name = $this->access->dn2groupname($dn); |
|
| 299 | - |
|
| 300 | - $this->access->connection->writeToCache($cacheKey, $name); |
|
| 301 | - |
|
| 302 | - return $name; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * returns the entry's gidNumber |
|
| 307 | - * @param string $dn |
|
| 308 | - * @param string $attribute |
|
| 309 | - * @return string|bool |
|
| 310 | - */ |
|
| 311 | - private function getEntryGidNumber($dn, $attribute) { |
|
| 312 | - $value = $this->access->readAttribute($dn, $attribute); |
|
| 313 | - if(is_array($value) && !empty($value)) { |
|
| 314 | - return $value[0]; |
|
| 315 | - } |
|
| 316 | - return false; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * returns the group's primary ID |
|
| 321 | - * @param string $dn |
|
| 322 | - * @return string|bool |
|
| 323 | - */ |
|
| 324 | - public function getGroupGidNumber($dn) { |
|
| 325 | - return $this->getEntryGidNumber($dn, 'gidNumber'); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * returns the user's gidNumber |
|
| 330 | - * @param string $dn |
|
| 331 | - * @return string|bool |
|
| 332 | - */ |
|
| 333 | - public function getUserGidNumber($dn) { |
|
| 334 | - $gidNumber = false; |
|
| 335 | - if($this->access->connection->hasGidNumber) { |
|
| 336 | - $gidNumber = $this->getEntryGidNumber($dn, $this->access->connection->ldapGidNumber); |
|
| 337 | - if($gidNumber === false) { |
|
| 338 | - $this->access->connection->hasGidNumber = false; |
|
| 339 | - } |
|
| 340 | - } |
|
| 341 | - return $gidNumber; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * returns a filter for a "users has specific gid" search or count operation |
|
| 346 | - * |
|
| 347 | - * @param string $groupDN |
|
| 348 | - * @param string $search |
|
| 349 | - * @return string |
|
| 350 | - * @throws \Exception |
|
| 351 | - */ |
|
| 352 | - private function prepareFilterForUsersHasGidNumber($groupDN, $search = '') { |
|
| 353 | - $groupID = $this->getGroupGidNumber($groupDN); |
|
| 354 | - if($groupID === false) { |
|
| 355 | - throw new \Exception('Not a valid group'); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - $filterParts = []; |
|
| 359 | - $filterParts[] = $this->access->getFilterForUserCount(); |
|
| 360 | - if ($search !== '') { |
|
| 361 | - $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
|
| 362 | - } |
|
| 363 | - $filterParts[] = $this->access->connection->ldapGidNumber .'=' . $groupID; |
|
| 364 | - |
|
| 365 | - $filter = $this->access->combineFilterWithAnd($filterParts); |
|
| 366 | - |
|
| 367 | - return $filter; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * returns a list of users that have the given group as gid number |
|
| 372 | - * |
|
| 373 | - * @param string $groupDN |
|
| 374 | - * @param string $search |
|
| 375 | - * @param int $limit |
|
| 376 | - * @param int $offset |
|
| 377 | - * @return string[] |
|
| 378 | - */ |
|
| 379 | - public function getUsersInGidNumber($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 380 | - try { |
|
| 381 | - $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search); |
|
| 382 | - $users = $this->access->fetchListOfUsers( |
|
| 383 | - $filter, |
|
| 384 | - [$this->access->connection->ldapUserDisplayName, 'dn'], |
|
| 385 | - $limit, |
|
| 386 | - $offset |
|
| 387 | - ); |
|
| 388 | - return $this->access->nextcloudUserNames($users); |
|
| 389 | - } catch (\Exception $e) { |
|
| 390 | - return []; |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - /** |
|
| 395 | - * returns the number of users that have the given group as gid number |
|
| 396 | - * |
|
| 397 | - * @param string $groupDN |
|
| 398 | - * @param string $search |
|
| 399 | - * @param int $limit |
|
| 400 | - * @param int $offset |
|
| 401 | - * @return int |
|
| 402 | - */ |
|
| 403 | - public function countUsersInGidNumber($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 404 | - try { |
|
| 405 | - $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search); |
|
| 406 | - $users = $this->access->countUsers($filter, ['dn'], $limit, $offset); |
|
| 407 | - return (int)$users; |
|
| 408 | - } catch (\Exception $e) { |
|
| 409 | - return 0; |
|
| 410 | - } |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * gets the gidNumber of a user |
|
| 415 | - * @param string $dn |
|
| 416 | - * @return string |
|
| 417 | - */ |
|
| 418 | - public function getUserGroupByGid($dn) { |
|
| 419 | - $groupID = $this->getUserGidNumber($dn); |
|
| 420 | - if($groupID !== false) { |
|
| 421 | - $groupName = $this->gidNumber2Name($groupID, $dn); |
|
| 422 | - if($groupName !== false) { |
|
| 423 | - return $groupName; |
|
| 424 | - } |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - return false; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * translates a primary group ID into an Nextcloud internal name |
|
| 432 | - * @param string $gid as given by primaryGroupID on AD |
|
| 433 | - * @param string $dn a DN that belongs to the same domain as the group |
|
| 434 | - * @return string|bool |
|
| 435 | - */ |
|
| 436 | - public function primaryGroupID2Name($gid, $dn) { |
|
| 437 | - $cacheKey = 'primaryGroupIDtoName'; |
|
| 438 | - $groupNames = $this->access->connection->getFromCache($cacheKey); |
|
| 439 | - if(!is_null($groupNames) && isset($groupNames[$gid])) { |
|
| 440 | - return $groupNames[$gid]; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - $domainObjectSid = $this->access->getSID($dn); |
|
| 444 | - if($domainObjectSid === false) { |
|
| 445 | - return false; |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - //we need to get the DN from LDAP |
|
| 449 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 450 | - $this->access->connection->ldapGroupFilter, |
|
| 451 | - 'objectsid=' . $domainObjectSid . '-' . $gid |
|
| 452 | - )); |
|
| 453 | - $result = $this->access->searchGroups($filter, array('dn'), 1); |
|
| 454 | - if(empty($result)) { |
|
| 455 | - return false; |
|
| 456 | - } |
|
| 457 | - $dn = $result[0]['dn'][0]; |
|
| 458 | - |
|
| 459 | - //and now the group name |
|
| 460 | - //NOTE once we have separate Nextcloud group IDs and group names we can |
|
| 461 | - //directly read the display name attribute instead of the DN |
|
| 462 | - $name = $this->access->dn2groupname($dn); |
|
| 463 | - |
|
| 464 | - $this->access->connection->writeToCache($cacheKey, $name); |
|
| 465 | - |
|
| 466 | - return $name; |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * returns the entry's primary group ID |
|
| 471 | - * @param string $dn |
|
| 472 | - * @param string $attribute |
|
| 473 | - * @return string|bool |
|
| 474 | - */ |
|
| 475 | - private function getEntryGroupID($dn, $attribute) { |
|
| 476 | - $value = $this->access->readAttribute($dn, $attribute); |
|
| 477 | - if(is_array($value) && !empty($value)) { |
|
| 478 | - return $value[0]; |
|
| 479 | - } |
|
| 480 | - return false; |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * returns the group's primary ID |
|
| 485 | - * @param string $dn |
|
| 486 | - * @return string|bool |
|
| 487 | - */ |
|
| 488 | - public function getGroupPrimaryGroupID($dn) { |
|
| 489 | - return $this->getEntryGroupID($dn, 'primaryGroupToken'); |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * returns the user's primary group ID |
|
| 494 | - * @param string $dn |
|
| 495 | - * @return string|bool |
|
| 496 | - */ |
|
| 497 | - public function getUserPrimaryGroupIDs($dn) { |
|
| 498 | - $primaryGroupID = false; |
|
| 499 | - if($this->access->connection->hasPrimaryGroups) { |
|
| 500 | - $primaryGroupID = $this->getEntryGroupID($dn, 'primaryGroupID'); |
|
| 501 | - if($primaryGroupID === false) { |
|
| 502 | - $this->access->connection->hasPrimaryGroups = false; |
|
| 503 | - } |
|
| 504 | - } |
|
| 505 | - return $primaryGroupID; |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - /** |
|
| 509 | - * returns a filter for a "users in primary group" search or count operation |
|
| 510 | - * |
|
| 511 | - * @param string $groupDN |
|
| 512 | - * @param string $search |
|
| 513 | - * @return string |
|
| 514 | - * @throws \Exception |
|
| 515 | - */ |
|
| 516 | - private function prepareFilterForUsersInPrimaryGroup($groupDN, $search = '') { |
|
| 517 | - $groupID = $this->getGroupPrimaryGroupID($groupDN); |
|
| 518 | - if($groupID === false) { |
|
| 519 | - throw new \Exception('Not a valid group'); |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - $filterParts = []; |
|
| 523 | - $filterParts[] = $this->access->getFilterForUserCount(); |
|
| 524 | - if ($search !== '') { |
|
| 525 | - $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
|
| 526 | - } |
|
| 527 | - $filterParts[] = 'primaryGroupID=' . $groupID; |
|
| 528 | - |
|
| 529 | - $filter = $this->access->combineFilterWithAnd($filterParts); |
|
| 530 | - |
|
| 531 | - return $filter; |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - /** |
|
| 535 | - * returns a list of users that have the given group as primary group |
|
| 536 | - * |
|
| 537 | - * @param string $groupDN |
|
| 538 | - * @param string $search |
|
| 539 | - * @param int $limit |
|
| 540 | - * @param int $offset |
|
| 541 | - * @return string[] |
|
| 542 | - */ |
|
| 543 | - public function getUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 544 | - try { |
|
| 545 | - $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
|
| 546 | - $users = $this->access->fetchListOfUsers( |
|
| 547 | - $filter, |
|
| 548 | - array($this->access->connection->ldapUserDisplayName, 'dn'), |
|
| 549 | - $limit, |
|
| 550 | - $offset |
|
| 551 | - ); |
|
| 552 | - return $this->access->nextcloudUserNames($users); |
|
| 553 | - } catch (\Exception $e) { |
|
| 554 | - return array(); |
|
| 555 | - } |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - /** |
|
| 559 | - * returns the number of users that have the given group as primary group |
|
| 560 | - * |
|
| 561 | - * @param string $groupDN |
|
| 562 | - * @param string $search |
|
| 563 | - * @param int $limit |
|
| 564 | - * @param int $offset |
|
| 565 | - * @return int |
|
| 566 | - */ |
|
| 567 | - public function countUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 568 | - try { |
|
| 569 | - $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
|
| 570 | - $users = $this->access->countUsers($filter, array('dn'), $limit, $offset); |
|
| 571 | - return (int)$users; |
|
| 572 | - } catch (\Exception $e) { |
|
| 573 | - return 0; |
|
| 574 | - } |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * gets the primary group of a user |
|
| 579 | - * @param string $dn |
|
| 580 | - * @return string |
|
| 581 | - */ |
|
| 582 | - public function getUserPrimaryGroup($dn) { |
|
| 583 | - $groupID = $this->getUserPrimaryGroupIDs($dn); |
|
| 584 | - if($groupID !== false) { |
|
| 585 | - $groupName = $this->primaryGroupID2Name($groupID, $dn); |
|
| 586 | - if($groupName !== false) { |
|
| 587 | - return $groupName; |
|
| 588 | - } |
|
| 589 | - } |
|
| 590 | - |
|
| 591 | - return false; |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - /** |
|
| 595 | - * Get all groups a user belongs to |
|
| 596 | - * @param string $uid Name of the user |
|
| 597 | - * @return array with group names |
|
| 598 | - * |
|
| 599 | - * This function fetches all groups a user belongs to. It does not check |
|
| 600 | - * if the user exists at all. |
|
| 601 | - * |
|
| 602 | - * This function includes groups based on dynamic group membership. |
|
| 603 | - */ |
|
| 604 | - public function getUserGroups($uid) { |
|
| 605 | - if(!$this->enabled) { |
|
| 606 | - return array(); |
|
| 607 | - } |
|
| 608 | - $cacheKey = 'getUserGroups'.$uid; |
|
| 609 | - $userGroups = $this->access->connection->getFromCache($cacheKey); |
|
| 610 | - if(!is_null($userGroups)) { |
|
| 611 | - return $userGroups; |
|
| 612 | - } |
|
| 613 | - $userDN = $this->access->username2dn($uid); |
|
| 614 | - if(!$userDN) { |
|
| 615 | - $this->access->connection->writeToCache($cacheKey, array()); |
|
| 616 | - return array(); |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - $groups = []; |
|
| 620 | - $primaryGroup = $this->getUserPrimaryGroup($userDN); |
|
| 621 | - $gidGroupName = $this->getUserGroupByGid($userDN); |
|
| 622 | - |
|
| 623 | - $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL); |
|
| 624 | - |
|
| 625 | - if (!empty($dynamicGroupMemberURL)) { |
|
| 626 | - // look through dynamic groups to add them to the result array if needed |
|
| 627 | - $groupsToMatch = $this->access->fetchListOfGroups( |
|
| 628 | - $this->access->connection->ldapGroupFilter,array('dn',$dynamicGroupMemberURL)); |
|
| 629 | - foreach($groupsToMatch as $dynamicGroup) { |
|
| 630 | - if (!array_key_exists($dynamicGroupMemberURL, $dynamicGroup)) { |
|
| 631 | - continue; |
|
| 632 | - } |
|
| 633 | - $pos = strpos($dynamicGroup[$dynamicGroupMemberURL][0], '('); |
|
| 634 | - if ($pos !== false) { |
|
| 635 | - $memberUrlFilter = substr($dynamicGroup[$dynamicGroupMemberURL][0],$pos); |
|
| 636 | - // apply filter via ldap search to see if this user is in this |
|
| 637 | - // dynamic group |
|
| 638 | - $userMatch = $this->access->readAttribute( |
|
| 639 | - $userDN, |
|
| 640 | - $this->access->connection->ldapUserDisplayName, |
|
| 641 | - $memberUrlFilter |
|
| 642 | - ); |
|
| 643 | - if ($userMatch !== false) { |
|
| 644 | - // match found so this user is in this group |
|
| 645 | - $groupName = $this->access->dn2groupname($dynamicGroup['dn'][0]); |
|
| 646 | - if(is_string($groupName)) { |
|
| 647 | - // be sure to never return false if the dn could not be |
|
| 648 | - // resolved to a name, for whatever reason. |
|
| 649 | - $groups[] = $groupName; |
|
| 650 | - } |
|
| 651 | - } |
|
| 652 | - } else { |
|
| 653 | - \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
|
| 654 | - 'of group ' . print_r($dynamicGroup, true), \OCP\Util::DEBUG); |
|
| 655 | - } |
|
| 656 | - } |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - // if possible, read out membership via memberOf. It's far faster than |
|
| 660 | - // performing a search, which still is a fallback later. |
|
| 661 | - // memberof doesn't support memberuid, so skip it here. |
|
| 662 | - if(intval($this->access->connection->hasMemberOfFilterSupport) === 1 |
|
| 663 | - && intval($this->access->connection->useMemberOfToDetectMembership) === 1 |
|
| 664 | - && strtolower($this->access->connection->ldapGroupMemberAssocAttr) !== 'memberuid' |
|
| 665 | - ) { |
|
| 666 | - $groupDNs = $this->_getGroupDNsFromMemberOf($userDN); |
|
| 667 | - if (is_array($groupDNs)) { |
|
| 668 | - foreach ($groupDNs as $dn) { |
|
| 669 | - $groupName = $this->access->dn2groupname($dn); |
|
| 670 | - if(is_string($groupName)) { |
|
| 671 | - // be sure to never return false if the dn could not be |
|
| 672 | - // resolved to a name, for whatever reason. |
|
| 673 | - $groups[] = $groupName; |
|
| 674 | - } |
|
| 675 | - } |
|
| 676 | - } |
|
| 677 | - |
|
| 678 | - if($primaryGroup !== false) { |
|
| 679 | - $groups[] = $primaryGroup; |
|
| 680 | - } |
|
| 681 | - if($gidGroupName !== false) { |
|
| 682 | - $groups[] = $gidGroupName; |
|
| 683 | - } |
|
| 684 | - $this->access->connection->writeToCache($cacheKey, $groups); |
|
| 685 | - return $groups; |
|
| 686 | - } |
|
| 687 | - |
|
| 688 | - //uniqueMember takes DN, memberuid the uid, so we need to distinguish |
|
| 689 | - if((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember') |
|
| 690 | - || (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'member') |
|
| 691 | - ) { |
|
| 692 | - $uid = $userDN; |
|
| 693 | - } else if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 694 | - $result = $this->access->readAttribute($userDN, 'uid'); |
|
| 695 | - if ($result === false) { |
|
| 696 | - \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN ' . $userDN . ' on '. |
|
| 697 | - $this->access->connection->ldapHost, \OCP\Util::DEBUG); |
|
| 698 | - } |
|
| 699 | - $uid = $result[0]; |
|
| 700 | - } else { |
|
| 701 | - // just in case |
|
| 702 | - $uid = $userDN; |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - if(isset($this->cachedGroupsByMember[$uid])) { |
|
| 706 | - $groups = array_merge($groups, $this->cachedGroupsByMember[$uid]); |
|
| 707 | - } else { |
|
| 708 | - $groupsByMember = array_values($this->getGroupsByMember($uid)); |
|
| 709 | - $groupsByMember = $this->access->nextcloudGroupNames($groupsByMember); |
|
| 710 | - $this->cachedGroupsByMember[$uid] = $groupsByMember; |
|
| 711 | - $groups = array_merge($groups, $groupsByMember); |
|
| 712 | - } |
|
| 713 | - |
|
| 714 | - if($primaryGroup !== false) { |
|
| 715 | - $groups[] = $primaryGroup; |
|
| 716 | - } |
|
| 717 | - if($gidGroupName !== false) { |
|
| 718 | - $groups[] = $gidGroupName; |
|
| 719 | - } |
|
| 720 | - |
|
| 721 | - $groups = array_unique($groups, SORT_LOCALE_STRING); |
|
| 722 | - $this->access->connection->writeToCache($cacheKey, $groups); |
|
| 723 | - |
|
| 724 | - return $groups; |
|
| 725 | - } |
|
| 726 | - |
|
| 727 | - /** |
|
| 728 | - * @param string $dn |
|
| 729 | - * @param array|null &$seen |
|
| 730 | - * @return array |
|
| 731 | - */ |
|
| 732 | - private function getGroupsByMember($dn, &$seen = null) { |
|
| 733 | - if ($seen === null) { |
|
| 734 | - $seen = array(); |
|
| 735 | - } |
|
| 736 | - $allGroups = array(); |
|
| 737 | - if (array_key_exists($dn, $seen)) { |
|
| 738 | - // avoid loops |
|
| 739 | - return array(); |
|
| 740 | - } |
|
| 741 | - $seen[$dn] = true; |
|
| 742 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 743 | - $this->access->connection->ldapGroupFilter, |
|
| 744 | - $this->access->connection->ldapGroupMemberAssocAttr.'='.$dn |
|
| 745 | - )); |
|
| 746 | - $groups = $this->access->fetchListOfGroups($filter, |
|
| 747 | - array($this->access->connection->ldapGroupDisplayName, 'dn')); |
|
| 748 | - if (is_array($groups)) { |
|
| 749 | - foreach ($groups as $groupobj) { |
|
| 750 | - $groupDN = $groupobj['dn'][0]; |
|
| 751 | - $allGroups[$groupDN] = $groupobj; |
|
| 752 | - $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 753 | - if (!empty($nestedGroups)) { |
|
| 754 | - $supergroups = $this->getGroupsByMember($groupDN, $seen); |
|
| 755 | - if (is_array($supergroups) && (count($supergroups)>0)) { |
|
| 756 | - $allGroups = array_merge($allGroups, $supergroups); |
|
| 757 | - } |
|
| 758 | - } |
|
| 759 | - } |
|
| 760 | - } |
|
| 761 | - return $allGroups; |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - /** |
|
| 765 | - * get a list of all users in a group |
|
| 766 | - * |
|
| 767 | - * @param string $gid |
|
| 768 | - * @param string $search |
|
| 769 | - * @param int $limit |
|
| 770 | - * @param int $offset |
|
| 771 | - * @return array with user ids |
|
| 772 | - */ |
|
| 773 | - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 774 | - if(!$this->enabled) { |
|
| 775 | - return array(); |
|
| 776 | - } |
|
| 777 | - if(!$this->groupExists($gid)) { |
|
| 778 | - return array(); |
|
| 779 | - } |
|
| 780 | - $search = $this->access->escapeFilterPart($search, true); |
|
| 781 | - $cacheKey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset; |
|
| 782 | - // check for cache of the exact query |
|
| 783 | - $groupUsers = $this->access->connection->getFromCache($cacheKey); |
|
| 784 | - if(!is_null($groupUsers)) { |
|
| 785 | - return $groupUsers; |
|
| 786 | - } |
|
| 787 | - |
|
| 788 | - // check for cache of the query without limit and offset |
|
| 789 | - $groupUsers = $this->access->connection->getFromCache('usersInGroup-'.$gid.'-'.$search); |
|
| 790 | - if(!is_null($groupUsers)) { |
|
| 791 | - $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
| 792 | - $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 793 | - return $groupUsers; |
|
| 794 | - } |
|
| 795 | - |
|
| 796 | - if($limit === -1) { |
|
| 797 | - $limit = null; |
|
| 798 | - } |
|
| 799 | - $groupDN = $this->access->groupname2dn($gid); |
|
| 800 | - if(!$groupDN) { |
|
| 801 | - // group couldn't be found, return empty resultset |
|
| 802 | - $this->access->connection->writeToCache($cacheKey, array()); |
|
| 803 | - return array(); |
|
| 804 | - } |
|
| 805 | - |
|
| 806 | - $primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $search, $limit, $offset); |
|
| 807 | - $posixGroupUsers = $this->getUsersInGidNumber($groupDN, $search, $limit, $offset); |
|
| 808 | - $members = array_keys($this->_groupMembers($groupDN)); |
|
| 809 | - if(!$members && empty($posixGroupUsers) && empty($primaryUsers)) { |
|
| 810 | - //in case users could not be retrieved, return empty result set |
|
| 811 | - $this->access->connection->writeToCache($cacheKey, []); |
|
| 812 | - return []; |
|
| 813 | - } |
|
| 814 | - |
|
| 815 | - $groupUsers = array(); |
|
| 816 | - $isMemberUid = (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid'); |
|
| 817 | - $attrs = $this->access->userManager->getAttributes(true); |
|
| 818 | - foreach($members as $member) { |
|
| 819 | - if($isMemberUid) { |
|
| 820 | - //we got uids, need to get their DNs to 'translate' them to user names |
|
| 821 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 822 | - str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
|
| 823 | - $this->access->getFilterPartForUserSearch($search) |
|
| 824 | - )); |
|
| 825 | - $ldap_users = $this->access->fetchListOfUsers($filter, $attrs, 1); |
|
| 826 | - if(count($ldap_users) < 1) { |
|
| 827 | - continue; |
|
| 828 | - } |
|
| 829 | - $groupUsers[] = $this->access->dn2username($ldap_users[0]['dn'][0]); |
|
| 830 | - } else { |
|
| 831 | - //we got DNs, check if we need to filter by search or we can give back all of them |
|
| 832 | - if ($search !== '') { |
|
| 833 | - if(!$this->access->readAttribute($member, |
|
| 834 | - $this->access->connection->ldapUserDisplayName, |
|
| 835 | - $this->access->getFilterPartForUserSearch($search))) { |
|
| 836 | - continue; |
|
| 837 | - } |
|
| 838 | - } |
|
| 839 | - // dn2username will also check if the users belong to the allowed base |
|
| 840 | - if($ocname = $this->access->dn2username($member)) { |
|
| 841 | - $groupUsers[] = $ocname; |
|
| 842 | - } |
|
| 843 | - } |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - $groupUsers = array_unique(array_merge($groupUsers, $primaryUsers, $posixGroupUsers)); |
|
| 847 | - natsort($groupUsers); |
|
| 848 | - $this->access->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers); |
|
| 849 | - $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
| 850 | - |
|
| 851 | - $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 852 | - |
|
| 853 | - return $groupUsers; |
|
| 854 | - } |
|
| 855 | - |
|
| 856 | - /** |
|
| 857 | - * returns the number of users in a group, who match the search term |
|
| 858 | - * @param string $gid the internal group name |
|
| 859 | - * @param string $search optional, a search string |
|
| 860 | - * @return int|bool |
|
| 861 | - */ |
|
| 862 | - public function countUsersInGroup($gid, $search = '') { |
|
| 863 | - $cacheKey = 'countUsersInGroup-'.$gid.'-'.$search; |
|
| 864 | - if(!$this->enabled || !$this->groupExists($gid)) { |
|
| 865 | - return false; |
|
| 866 | - } |
|
| 867 | - $groupUsers = $this->access->connection->getFromCache($cacheKey); |
|
| 868 | - if(!is_null($groupUsers)) { |
|
| 869 | - return $groupUsers; |
|
| 870 | - } |
|
| 871 | - |
|
| 872 | - $groupDN = $this->access->groupname2dn($gid); |
|
| 873 | - if(!$groupDN) { |
|
| 874 | - // group couldn't be found, return empty result set |
|
| 875 | - $this->access->connection->writeToCache($cacheKey, false); |
|
| 876 | - return false; |
|
| 877 | - } |
|
| 878 | - |
|
| 879 | - $members = array_keys($this->_groupMembers($groupDN)); |
|
| 880 | - $primaryUserCount = $this->countUsersInPrimaryGroup($groupDN, ''); |
|
| 881 | - if(!$members && $primaryUserCount === 0) { |
|
| 882 | - //in case users could not be retrieved, return empty result set |
|
| 883 | - $this->access->connection->writeToCache($cacheKey, false); |
|
| 884 | - return false; |
|
| 885 | - } |
|
| 886 | - |
|
| 887 | - if ($search === '') { |
|
| 888 | - $groupUsers = count($members) + $primaryUserCount; |
|
| 889 | - $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 890 | - return $groupUsers; |
|
| 891 | - } |
|
| 892 | - $search = $this->access->escapeFilterPart($search, true); |
|
| 893 | - $isMemberUid = |
|
| 894 | - (strtolower($this->access->connection->ldapGroupMemberAssocAttr) |
|
| 895 | - === 'memberuid'); |
|
| 896 | - |
|
| 897 | - //we need to apply the search filter |
|
| 898 | - //alternatives that need to be checked: |
|
| 899 | - //a) get all users by search filter and array_intersect them |
|
| 900 | - //b) a, but only when less than 1k 10k ?k users like it is |
|
| 901 | - //c) put all DNs|uids in a LDAP filter, combine with the search string |
|
| 902 | - // and let it count. |
|
| 903 | - //For now this is not important, because the only use of this method |
|
| 904 | - //does not supply a search string |
|
| 905 | - $groupUsers = array(); |
|
| 906 | - foreach($members as $member) { |
|
| 907 | - if($isMemberUid) { |
|
| 908 | - //we got uids, need to get their DNs to 'translate' them to user names |
|
| 909 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 910 | - str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
|
| 911 | - $this->access->getFilterPartForUserSearch($search) |
|
| 912 | - )); |
|
| 913 | - $ldap_users = $this->access->fetchListOfUsers($filter, 'dn', 1); |
|
| 914 | - if(count($ldap_users) < 1) { |
|
| 915 | - continue; |
|
| 916 | - } |
|
| 917 | - $groupUsers[] = $this->access->dn2username($ldap_users[0]); |
|
| 918 | - } else { |
|
| 919 | - //we need to apply the search filter now |
|
| 920 | - if(!$this->access->readAttribute($member, |
|
| 921 | - $this->access->connection->ldapUserDisplayName, |
|
| 922 | - $this->access->getFilterPartForUserSearch($search))) { |
|
| 923 | - continue; |
|
| 924 | - } |
|
| 925 | - // dn2username will also check if the users belong to the allowed base |
|
| 926 | - if($ocname = $this->access->dn2username($member)) { |
|
| 927 | - $groupUsers[] = $ocname; |
|
| 928 | - } |
|
| 929 | - } |
|
| 930 | - } |
|
| 931 | - |
|
| 932 | - //and get users that have the group as primary |
|
| 933 | - $primaryUsers = $this->countUsersInPrimaryGroup($groupDN, $search); |
|
| 934 | - |
|
| 935 | - return count($groupUsers) + $primaryUsers; |
|
| 936 | - } |
|
| 937 | - |
|
| 938 | - /** |
|
| 939 | - * get a list of all groups |
|
| 940 | - * |
|
| 941 | - * @param string $search |
|
| 942 | - * @param $limit |
|
| 943 | - * @param int $offset |
|
| 944 | - * @return array with group names |
|
| 945 | - * |
|
| 946 | - * Returns a list with all groups (used by getGroups) |
|
| 947 | - */ |
|
| 948 | - protected function getGroupsChunk($search = '', $limit = -1, $offset = 0) { |
|
| 949 | - if(!$this->enabled) { |
|
| 950 | - return array(); |
|
| 951 | - } |
|
| 952 | - $cacheKey = 'getGroups-'.$search.'-'.$limit.'-'.$offset; |
|
| 953 | - |
|
| 954 | - //Check cache before driving unnecessary searches |
|
| 955 | - \OCP\Util::writeLog('user_ldap', 'getGroups '.$cacheKey, \OCP\Util::DEBUG); |
|
| 956 | - $ldap_groups = $this->access->connection->getFromCache($cacheKey); |
|
| 957 | - if(!is_null($ldap_groups)) { |
|
| 958 | - return $ldap_groups; |
|
| 959 | - } |
|
| 960 | - |
|
| 961 | - // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
| 962 | - // error. With a limit of 0, we get 0 results. So we pass null. |
|
| 963 | - if($limit <= 0) { |
|
| 964 | - $limit = null; |
|
| 965 | - } |
|
| 966 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 967 | - $this->access->connection->ldapGroupFilter, |
|
| 968 | - $this->access->getFilterPartForGroupSearch($search) |
|
| 969 | - )); |
|
| 970 | - \OCP\Util::writeLog('user_ldap', 'getGroups Filter '.$filter, \OCP\Util::DEBUG); |
|
| 971 | - $ldap_groups = $this->access->fetchListOfGroups($filter, |
|
| 972 | - array($this->access->connection->ldapGroupDisplayName, 'dn'), |
|
| 973 | - $limit, |
|
| 974 | - $offset); |
|
| 975 | - $ldap_groups = $this->access->nextcloudGroupNames($ldap_groups); |
|
| 976 | - |
|
| 977 | - $this->access->connection->writeToCache($cacheKey, $ldap_groups); |
|
| 978 | - return $ldap_groups; |
|
| 979 | - } |
|
| 980 | - |
|
| 981 | - /** |
|
| 982 | - * get a list of all groups using a paged search |
|
| 983 | - * |
|
| 984 | - * @param string $search |
|
| 985 | - * @param int $limit |
|
| 986 | - * @param int $offset |
|
| 987 | - * @return array with group names |
|
| 988 | - * |
|
| 989 | - * Returns a list with all groups |
|
| 990 | - * Uses a paged search if available to override a |
|
| 991 | - * server side search limit. |
|
| 992 | - * (active directory has a limit of 1000 by default) |
|
| 993 | - */ |
|
| 994 | - public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
| 995 | - if(!$this->enabled) { |
|
| 996 | - return array(); |
|
| 997 | - } |
|
| 998 | - $search = $this->access->escapeFilterPart($search, true); |
|
| 999 | - $pagingSize = intval($this->access->connection->ldapPagingSize); |
|
| 1000 | - if (!$this->access->connection->hasPagedResultSupport || $pagingSize <= 0) { |
|
| 1001 | - return $this->getGroupsChunk($search, $limit, $offset); |
|
| 1002 | - } |
|
| 1003 | - $maxGroups = 100000; // limit max results (just for safety reasons) |
|
| 1004 | - if ($limit > -1) { |
|
| 1005 | - $overallLimit = min($limit + $offset, $maxGroups); |
|
| 1006 | - } else { |
|
| 1007 | - $overallLimit = $maxGroups; |
|
| 1008 | - } |
|
| 1009 | - $chunkOffset = $offset; |
|
| 1010 | - $allGroups = array(); |
|
| 1011 | - while ($chunkOffset < $overallLimit) { |
|
| 1012 | - $chunkLimit = min($pagingSize, $overallLimit - $chunkOffset); |
|
| 1013 | - $ldapGroups = $this->getGroupsChunk($search, $chunkLimit, $chunkOffset); |
|
| 1014 | - $nread = count($ldapGroups); |
|
| 1015 | - \OCP\Util::writeLog('user_ldap', 'getGroups('.$search.'): read '.$nread.' at offset '.$chunkOffset.' (limit: '.$chunkLimit.')', \OCP\Util::DEBUG); |
|
| 1016 | - if ($nread) { |
|
| 1017 | - $allGroups = array_merge($allGroups, $ldapGroups); |
|
| 1018 | - $chunkOffset += $nread; |
|
| 1019 | - } |
|
| 1020 | - if ($nread < $chunkLimit) { |
|
| 1021 | - break; |
|
| 1022 | - } |
|
| 1023 | - } |
|
| 1024 | - return $allGroups; |
|
| 1025 | - } |
|
| 1026 | - |
|
| 1027 | - /** |
|
| 1028 | - * @param string $group |
|
| 1029 | - * @return bool |
|
| 1030 | - */ |
|
| 1031 | - public function groupMatchesFilter($group) { |
|
| 1032 | - return (strripos($group, $this->groupSearch) !== false); |
|
| 1033 | - } |
|
| 1034 | - |
|
| 1035 | - /** |
|
| 1036 | - * check if a group exists |
|
| 1037 | - * @param string $gid |
|
| 1038 | - * @return bool |
|
| 1039 | - */ |
|
| 1040 | - public function groupExists($gid) { |
|
| 1041 | - $groupExists = $this->access->connection->getFromCache('groupExists'.$gid); |
|
| 1042 | - if(!is_null($groupExists)) { |
|
| 1043 | - return (bool)$groupExists; |
|
| 1044 | - } |
|
| 1045 | - |
|
| 1046 | - //getting dn, if false the group does not exist. If dn, it may be mapped |
|
| 1047 | - //only, requires more checking. |
|
| 1048 | - $dn = $this->access->groupname2dn($gid); |
|
| 1049 | - if(!$dn) { |
|
| 1050 | - $this->access->connection->writeToCache('groupExists'.$gid, false); |
|
| 1051 | - return false; |
|
| 1052 | - } |
|
| 1053 | - |
|
| 1054 | - //if group really still exists, we will be able to read its objectclass |
|
| 1055 | - if(!is_array($this->access->readAttribute($dn, ''))) { |
|
| 1056 | - $this->access->connection->writeToCache('groupExists'.$gid, false); |
|
| 1057 | - return false; |
|
| 1058 | - } |
|
| 1059 | - |
|
| 1060 | - $this->access->connection->writeToCache('groupExists'.$gid, true); |
|
| 1061 | - return true; |
|
| 1062 | - } |
|
| 1063 | - |
|
| 1064 | - /** |
|
| 1065 | - * Check if backend implements actions |
|
| 1066 | - * @param int $actions bitwise-or'ed actions |
|
| 1067 | - * @return boolean |
|
| 1068 | - * |
|
| 1069 | - * Returns the supported actions as int to be |
|
| 1070 | - * compared with \OC\User\Backend::CREATE_USER etc. |
|
| 1071 | - */ |
|
| 1072 | - public function implementsActions($actions) { |
|
| 1073 | - return (bool)(\OC\Group\Backend::COUNT_USERS & $actions); |
|
| 1074 | - } |
|
| 1075 | - |
|
| 1076 | - /** |
|
| 1077 | - * Return access for LDAP interaction. |
|
| 1078 | - * @return Access instance of Access for LDAP interaction |
|
| 1079 | - */ |
|
| 1080 | - public function getLDAPAccess() { |
|
| 1081 | - return $this->access; |
|
| 1082 | - } |
|
| 44 | + protected $enabled = false; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var string[] $cachedGroupMembers array of users with gid as key |
|
| 48 | + */ |
|
| 49 | + protected $cachedGroupMembers; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var string[] $cachedGroupsByMember array of groups with uid as key |
|
| 53 | + */ |
|
| 54 | + protected $cachedGroupsByMember; |
|
| 55 | + |
|
| 56 | + public function __construct(Access $access) { |
|
| 57 | + parent::__construct($access); |
|
| 58 | + $filter = $this->access->connection->ldapGroupFilter; |
|
| 59 | + $gassoc = $this->access->connection->ldapGroupMemberAssocAttr; |
|
| 60 | + if(!empty($filter) && !empty($gassoc)) { |
|
| 61 | + $this->enabled = true; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + $this->cachedGroupMembers = new CappedMemoryCache(); |
|
| 65 | + $this->cachedGroupsByMember = new CappedMemoryCache(); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * is user in group? |
|
| 70 | + * @param string $uid uid of the user |
|
| 71 | + * @param string $gid gid of the group |
|
| 72 | + * @return bool |
|
| 73 | + * |
|
| 74 | + * Checks whether the user is member of a group or not. |
|
| 75 | + */ |
|
| 76 | + public function inGroup($uid, $gid) { |
|
| 77 | + if(!$this->enabled) { |
|
| 78 | + return false; |
|
| 79 | + } |
|
| 80 | + $cacheKey = 'inGroup'.$uid.':'.$gid; |
|
| 81 | + $inGroup = $this->access->connection->getFromCache($cacheKey); |
|
| 82 | + if(!is_null($inGroup)) { |
|
| 83 | + return (bool)$inGroup; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + $userDN = $this->access->username2dn($uid); |
|
| 87 | + |
|
| 88 | + if(isset($this->cachedGroupMembers[$gid])) { |
|
| 89 | + $isInGroup = in_array($userDN, $this->cachedGroupMembers[$gid]); |
|
| 90 | + return $isInGroup; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + $cacheKeyMembers = 'inGroup-members:'.$gid; |
|
| 94 | + $members = $this->access->connection->getFromCache($cacheKeyMembers); |
|
| 95 | + if(!is_null($members)) { |
|
| 96 | + $this->cachedGroupMembers[$gid] = $members; |
|
| 97 | + $isInGroup = in_array($userDN, $members); |
|
| 98 | + $this->access->connection->writeToCache($cacheKey, $isInGroup); |
|
| 99 | + return $isInGroup; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + $groupDN = $this->access->groupname2dn($gid); |
|
| 103 | + // just in case |
|
| 104 | + if(!$groupDN || !$userDN) { |
|
| 105 | + $this->access->connection->writeToCache($cacheKey, false); |
|
| 106 | + return false; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + //check primary group first |
|
| 110 | + if($gid === $this->getUserPrimaryGroup($userDN)) { |
|
| 111 | + $this->access->connection->writeToCache($cacheKey, true); |
|
| 112 | + return true; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course. |
|
| 116 | + $members = $this->_groupMembers($groupDN); |
|
| 117 | + $members = array_keys($members); // uids are returned as keys |
|
| 118 | + if(!is_array($members) || count($members) === 0) { |
|
| 119 | + $this->access->connection->writeToCache($cacheKey, false); |
|
| 120 | + return false; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + //extra work if we don't get back user DNs |
|
| 124 | + if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 125 | + $dns = array(); |
|
| 126 | + $filterParts = array(); |
|
| 127 | + $bytes = 0; |
|
| 128 | + foreach($members as $mid) { |
|
| 129 | + $filter = str_replace('%uid', $mid, $this->access->connection->ldapLoginFilter); |
|
| 130 | + $filterParts[] = $filter; |
|
| 131 | + $bytes += strlen($filter); |
|
| 132 | + if($bytes >= 9000000) { |
|
| 133 | + // AD has a default input buffer of 10 MB, we do not want |
|
| 134 | + // to take even the chance to exceed it |
|
| 135 | + $filter = $this->access->combineFilterWithOr($filterParts); |
|
| 136 | + $bytes = 0; |
|
| 137 | + $filterParts = array(); |
|
| 138 | + $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
|
| 139 | + $dns = array_merge($dns, $users); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + if(count($filterParts) > 0) { |
|
| 143 | + $filter = $this->access->combineFilterWithOr($filterParts); |
|
| 144 | + $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
|
| 145 | + $dns = array_merge($dns, $users); |
|
| 146 | + } |
|
| 147 | + $members = $dns; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + $isInGroup = in_array($userDN, $members); |
|
| 151 | + $this->access->connection->writeToCache($cacheKey, $isInGroup); |
|
| 152 | + $this->access->connection->writeToCache($cacheKeyMembers, $members); |
|
| 153 | + $this->cachedGroupMembers[$gid] = $members; |
|
| 154 | + |
|
| 155 | + return $isInGroup; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * @param string $dnGroup |
|
| 160 | + * @return array |
|
| 161 | + * |
|
| 162 | + * For a group that has user membership defined by an LDAP search url attribute returns the users |
|
| 163 | + * that match the search url otherwise returns an empty array. |
|
| 164 | + */ |
|
| 165 | + public function getDynamicGroupMembers($dnGroup) { |
|
| 166 | + $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL); |
|
| 167 | + |
|
| 168 | + if (empty($dynamicGroupMemberURL)) { |
|
| 169 | + return array(); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + $dynamicMembers = array(); |
|
| 173 | + $memberURLs = $this->access->readAttribute( |
|
| 174 | + $dnGroup, |
|
| 175 | + $dynamicGroupMemberURL, |
|
| 176 | + $this->access->connection->ldapGroupFilter |
|
| 177 | + ); |
|
| 178 | + if ($memberURLs !== false) { |
|
| 179 | + // this group has the 'memberURL' attribute so this is a dynamic group |
|
| 180 | + // example 1: ldap:///cn=users,cn=accounts,dc=dcsubbase,dc=dcbase??one?(o=HeadOffice) |
|
| 181 | + // example 2: ldap:///cn=users,cn=accounts,dc=dcsubbase,dc=dcbase??one?(&(o=HeadOffice)(uidNumber>=500)) |
|
| 182 | + $pos = strpos($memberURLs[0], '('); |
|
| 183 | + if ($pos !== false) { |
|
| 184 | + $memberUrlFilter = substr($memberURLs[0], $pos); |
|
| 185 | + $foundMembers = $this->access->searchUsers($memberUrlFilter,'dn'); |
|
| 186 | + $dynamicMembers = array(); |
|
| 187 | + foreach($foundMembers as $value) { |
|
| 188 | + $dynamicMembers[$value['dn'][0]] = 1; |
|
| 189 | + } |
|
| 190 | + } else { |
|
| 191 | + \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
|
| 192 | + 'of group ' . $dnGroup, \OCP\Util::DEBUG); |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + return $dynamicMembers; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * @param string $dnGroup |
|
| 200 | + * @param array|null &$seen |
|
| 201 | + * @return array|mixed|null |
|
| 202 | + */ |
|
| 203 | + private function _groupMembers($dnGroup, &$seen = null) { |
|
| 204 | + if ($seen === null) { |
|
| 205 | + $seen = array(); |
|
| 206 | + } |
|
| 207 | + $allMembers = array(); |
|
| 208 | + if (array_key_exists($dnGroup, $seen)) { |
|
| 209 | + // avoid loops |
|
| 210 | + return array(); |
|
| 211 | + } |
|
| 212 | + // used extensively in cron job, caching makes sense for nested groups |
|
| 213 | + $cacheKey = '_groupMembers'.$dnGroup; |
|
| 214 | + $groupMembers = $this->access->connection->getFromCache($cacheKey); |
|
| 215 | + if(!is_null($groupMembers)) { |
|
| 216 | + return $groupMembers; |
|
| 217 | + } |
|
| 218 | + $seen[$dnGroup] = 1; |
|
| 219 | + $members = $this->access->readAttribute($dnGroup, $this->access->connection->ldapGroupMemberAssocAttr, |
|
| 220 | + $this->access->connection->ldapGroupFilter); |
|
| 221 | + if (is_array($members)) { |
|
| 222 | + foreach ($members as $memberDN) { |
|
| 223 | + $allMembers[$memberDN] = 1; |
|
| 224 | + $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 225 | + if (!empty($nestedGroups)) { |
|
| 226 | + $subMembers = $this->_groupMembers($memberDN, $seen); |
|
| 227 | + if ($subMembers) { |
|
| 228 | + $allMembers = array_merge($allMembers, $subMembers); |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + $allMembers = array_merge($allMembers, $this->getDynamicGroupMembers($dnGroup)); |
|
| 235 | + |
|
| 236 | + $this->access->connection->writeToCache($cacheKey, $allMembers); |
|
| 237 | + return $allMembers; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * @param string $DN |
|
| 242 | + * @param array|null &$seen |
|
| 243 | + * @return array |
|
| 244 | + */ |
|
| 245 | + private function _getGroupDNsFromMemberOf($DN, &$seen = null) { |
|
| 246 | + if ($seen === null) { |
|
| 247 | + $seen = array(); |
|
| 248 | + } |
|
| 249 | + if (array_key_exists($DN, $seen)) { |
|
| 250 | + // avoid loops |
|
| 251 | + return array(); |
|
| 252 | + } |
|
| 253 | + $seen[$DN] = 1; |
|
| 254 | + $groups = $this->access->readAttribute($DN, 'memberOf'); |
|
| 255 | + if (!is_array($groups)) { |
|
| 256 | + return array(); |
|
| 257 | + } |
|
| 258 | + $groups = $this->access->groupsMatchFilter($groups); |
|
| 259 | + $allGroups = $groups; |
|
| 260 | + $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 261 | + if (intval($nestedGroups) === 1) { |
|
| 262 | + foreach ($groups as $group) { |
|
| 263 | + $subGroups = $this->_getGroupDNsFromMemberOf($group, $seen); |
|
| 264 | + $allGroups = array_merge($allGroups, $subGroups); |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + return $allGroups; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * translates a gidNumber into an ownCloud internal name |
|
| 272 | + * @param string $gid as given by gidNumber on POSIX LDAP |
|
| 273 | + * @param string $dn a DN that belongs to the same domain as the group |
|
| 274 | + * @return string|bool |
|
| 275 | + */ |
|
| 276 | + public function gidNumber2Name($gid, $dn) { |
|
| 277 | + $cacheKey = 'gidNumberToName' . $gid; |
|
| 278 | + $groupName = $this->access->connection->getFromCache($cacheKey); |
|
| 279 | + if(!is_null($groupName) && isset($groupName)) { |
|
| 280 | + return $groupName; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + //we need to get the DN from LDAP |
|
| 284 | + $filter = $this->access->combineFilterWithAnd([ |
|
| 285 | + $this->access->connection->ldapGroupFilter, |
|
| 286 | + 'objectClass=posixGroup', |
|
| 287 | + $this->access->connection->ldapGidNumber . '=' . $gid |
|
| 288 | + ]); |
|
| 289 | + $result = $this->access->searchGroups($filter, array('dn'), 1); |
|
| 290 | + if(empty($result)) { |
|
| 291 | + return false; |
|
| 292 | + } |
|
| 293 | + $dn = $result[0]['dn'][0]; |
|
| 294 | + |
|
| 295 | + //and now the group name |
|
| 296 | + //NOTE once we have separate ownCloud group IDs and group names we can |
|
| 297 | + //directly read the display name attribute instead of the DN |
|
| 298 | + $name = $this->access->dn2groupname($dn); |
|
| 299 | + |
|
| 300 | + $this->access->connection->writeToCache($cacheKey, $name); |
|
| 301 | + |
|
| 302 | + return $name; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * returns the entry's gidNumber |
|
| 307 | + * @param string $dn |
|
| 308 | + * @param string $attribute |
|
| 309 | + * @return string|bool |
|
| 310 | + */ |
|
| 311 | + private function getEntryGidNumber($dn, $attribute) { |
|
| 312 | + $value = $this->access->readAttribute($dn, $attribute); |
|
| 313 | + if(is_array($value) && !empty($value)) { |
|
| 314 | + return $value[0]; |
|
| 315 | + } |
|
| 316 | + return false; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * returns the group's primary ID |
|
| 321 | + * @param string $dn |
|
| 322 | + * @return string|bool |
|
| 323 | + */ |
|
| 324 | + public function getGroupGidNumber($dn) { |
|
| 325 | + return $this->getEntryGidNumber($dn, 'gidNumber'); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * returns the user's gidNumber |
|
| 330 | + * @param string $dn |
|
| 331 | + * @return string|bool |
|
| 332 | + */ |
|
| 333 | + public function getUserGidNumber($dn) { |
|
| 334 | + $gidNumber = false; |
|
| 335 | + if($this->access->connection->hasGidNumber) { |
|
| 336 | + $gidNumber = $this->getEntryGidNumber($dn, $this->access->connection->ldapGidNumber); |
|
| 337 | + if($gidNumber === false) { |
|
| 338 | + $this->access->connection->hasGidNumber = false; |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | + return $gidNumber; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * returns a filter for a "users has specific gid" search or count operation |
|
| 346 | + * |
|
| 347 | + * @param string $groupDN |
|
| 348 | + * @param string $search |
|
| 349 | + * @return string |
|
| 350 | + * @throws \Exception |
|
| 351 | + */ |
|
| 352 | + private function prepareFilterForUsersHasGidNumber($groupDN, $search = '') { |
|
| 353 | + $groupID = $this->getGroupGidNumber($groupDN); |
|
| 354 | + if($groupID === false) { |
|
| 355 | + throw new \Exception('Not a valid group'); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + $filterParts = []; |
|
| 359 | + $filterParts[] = $this->access->getFilterForUserCount(); |
|
| 360 | + if ($search !== '') { |
|
| 361 | + $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
|
| 362 | + } |
|
| 363 | + $filterParts[] = $this->access->connection->ldapGidNumber .'=' . $groupID; |
|
| 364 | + |
|
| 365 | + $filter = $this->access->combineFilterWithAnd($filterParts); |
|
| 366 | + |
|
| 367 | + return $filter; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * returns a list of users that have the given group as gid number |
|
| 372 | + * |
|
| 373 | + * @param string $groupDN |
|
| 374 | + * @param string $search |
|
| 375 | + * @param int $limit |
|
| 376 | + * @param int $offset |
|
| 377 | + * @return string[] |
|
| 378 | + */ |
|
| 379 | + public function getUsersInGidNumber($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 380 | + try { |
|
| 381 | + $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search); |
|
| 382 | + $users = $this->access->fetchListOfUsers( |
|
| 383 | + $filter, |
|
| 384 | + [$this->access->connection->ldapUserDisplayName, 'dn'], |
|
| 385 | + $limit, |
|
| 386 | + $offset |
|
| 387 | + ); |
|
| 388 | + return $this->access->nextcloudUserNames($users); |
|
| 389 | + } catch (\Exception $e) { |
|
| 390 | + return []; |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + /** |
|
| 395 | + * returns the number of users that have the given group as gid number |
|
| 396 | + * |
|
| 397 | + * @param string $groupDN |
|
| 398 | + * @param string $search |
|
| 399 | + * @param int $limit |
|
| 400 | + * @param int $offset |
|
| 401 | + * @return int |
|
| 402 | + */ |
|
| 403 | + public function countUsersInGidNumber($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 404 | + try { |
|
| 405 | + $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search); |
|
| 406 | + $users = $this->access->countUsers($filter, ['dn'], $limit, $offset); |
|
| 407 | + return (int)$users; |
|
| 408 | + } catch (\Exception $e) { |
|
| 409 | + return 0; |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * gets the gidNumber of a user |
|
| 415 | + * @param string $dn |
|
| 416 | + * @return string |
|
| 417 | + */ |
|
| 418 | + public function getUserGroupByGid($dn) { |
|
| 419 | + $groupID = $this->getUserGidNumber($dn); |
|
| 420 | + if($groupID !== false) { |
|
| 421 | + $groupName = $this->gidNumber2Name($groupID, $dn); |
|
| 422 | + if($groupName !== false) { |
|
| 423 | + return $groupName; |
|
| 424 | + } |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + return false; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * translates a primary group ID into an Nextcloud internal name |
|
| 432 | + * @param string $gid as given by primaryGroupID on AD |
|
| 433 | + * @param string $dn a DN that belongs to the same domain as the group |
|
| 434 | + * @return string|bool |
|
| 435 | + */ |
|
| 436 | + public function primaryGroupID2Name($gid, $dn) { |
|
| 437 | + $cacheKey = 'primaryGroupIDtoName'; |
|
| 438 | + $groupNames = $this->access->connection->getFromCache($cacheKey); |
|
| 439 | + if(!is_null($groupNames) && isset($groupNames[$gid])) { |
|
| 440 | + return $groupNames[$gid]; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + $domainObjectSid = $this->access->getSID($dn); |
|
| 444 | + if($domainObjectSid === false) { |
|
| 445 | + return false; |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + //we need to get the DN from LDAP |
|
| 449 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 450 | + $this->access->connection->ldapGroupFilter, |
|
| 451 | + 'objectsid=' . $domainObjectSid . '-' . $gid |
|
| 452 | + )); |
|
| 453 | + $result = $this->access->searchGroups($filter, array('dn'), 1); |
|
| 454 | + if(empty($result)) { |
|
| 455 | + return false; |
|
| 456 | + } |
|
| 457 | + $dn = $result[0]['dn'][0]; |
|
| 458 | + |
|
| 459 | + //and now the group name |
|
| 460 | + //NOTE once we have separate Nextcloud group IDs and group names we can |
|
| 461 | + //directly read the display name attribute instead of the DN |
|
| 462 | + $name = $this->access->dn2groupname($dn); |
|
| 463 | + |
|
| 464 | + $this->access->connection->writeToCache($cacheKey, $name); |
|
| 465 | + |
|
| 466 | + return $name; |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * returns the entry's primary group ID |
|
| 471 | + * @param string $dn |
|
| 472 | + * @param string $attribute |
|
| 473 | + * @return string|bool |
|
| 474 | + */ |
|
| 475 | + private function getEntryGroupID($dn, $attribute) { |
|
| 476 | + $value = $this->access->readAttribute($dn, $attribute); |
|
| 477 | + if(is_array($value) && !empty($value)) { |
|
| 478 | + return $value[0]; |
|
| 479 | + } |
|
| 480 | + return false; |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * returns the group's primary ID |
|
| 485 | + * @param string $dn |
|
| 486 | + * @return string|bool |
|
| 487 | + */ |
|
| 488 | + public function getGroupPrimaryGroupID($dn) { |
|
| 489 | + return $this->getEntryGroupID($dn, 'primaryGroupToken'); |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + /** |
|
| 493 | + * returns the user's primary group ID |
|
| 494 | + * @param string $dn |
|
| 495 | + * @return string|bool |
|
| 496 | + */ |
|
| 497 | + public function getUserPrimaryGroupIDs($dn) { |
|
| 498 | + $primaryGroupID = false; |
|
| 499 | + if($this->access->connection->hasPrimaryGroups) { |
|
| 500 | + $primaryGroupID = $this->getEntryGroupID($dn, 'primaryGroupID'); |
|
| 501 | + if($primaryGroupID === false) { |
|
| 502 | + $this->access->connection->hasPrimaryGroups = false; |
|
| 503 | + } |
|
| 504 | + } |
|
| 505 | + return $primaryGroupID; |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + /** |
|
| 509 | + * returns a filter for a "users in primary group" search or count operation |
|
| 510 | + * |
|
| 511 | + * @param string $groupDN |
|
| 512 | + * @param string $search |
|
| 513 | + * @return string |
|
| 514 | + * @throws \Exception |
|
| 515 | + */ |
|
| 516 | + private function prepareFilterForUsersInPrimaryGroup($groupDN, $search = '') { |
|
| 517 | + $groupID = $this->getGroupPrimaryGroupID($groupDN); |
|
| 518 | + if($groupID === false) { |
|
| 519 | + throw new \Exception('Not a valid group'); |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + $filterParts = []; |
|
| 523 | + $filterParts[] = $this->access->getFilterForUserCount(); |
|
| 524 | + if ($search !== '') { |
|
| 525 | + $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
|
| 526 | + } |
|
| 527 | + $filterParts[] = 'primaryGroupID=' . $groupID; |
|
| 528 | + |
|
| 529 | + $filter = $this->access->combineFilterWithAnd($filterParts); |
|
| 530 | + |
|
| 531 | + return $filter; |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + /** |
|
| 535 | + * returns a list of users that have the given group as primary group |
|
| 536 | + * |
|
| 537 | + * @param string $groupDN |
|
| 538 | + * @param string $search |
|
| 539 | + * @param int $limit |
|
| 540 | + * @param int $offset |
|
| 541 | + * @return string[] |
|
| 542 | + */ |
|
| 543 | + public function getUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 544 | + try { |
|
| 545 | + $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
|
| 546 | + $users = $this->access->fetchListOfUsers( |
|
| 547 | + $filter, |
|
| 548 | + array($this->access->connection->ldapUserDisplayName, 'dn'), |
|
| 549 | + $limit, |
|
| 550 | + $offset |
|
| 551 | + ); |
|
| 552 | + return $this->access->nextcloudUserNames($users); |
|
| 553 | + } catch (\Exception $e) { |
|
| 554 | + return array(); |
|
| 555 | + } |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + /** |
|
| 559 | + * returns the number of users that have the given group as primary group |
|
| 560 | + * |
|
| 561 | + * @param string $groupDN |
|
| 562 | + * @param string $search |
|
| 563 | + * @param int $limit |
|
| 564 | + * @param int $offset |
|
| 565 | + * @return int |
|
| 566 | + */ |
|
| 567 | + public function countUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 568 | + try { |
|
| 569 | + $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
|
| 570 | + $users = $this->access->countUsers($filter, array('dn'), $limit, $offset); |
|
| 571 | + return (int)$users; |
|
| 572 | + } catch (\Exception $e) { |
|
| 573 | + return 0; |
|
| 574 | + } |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * gets the primary group of a user |
|
| 579 | + * @param string $dn |
|
| 580 | + * @return string |
|
| 581 | + */ |
|
| 582 | + public function getUserPrimaryGroup($dn) { |
|
| 583 | + $groupID = $this->getUserPrimaryGroupIDs($dn); |
|
| 584 | + if($groupID !== false) { |
|
| 585 | + $groupName = $this->primaryGroupID2Name($groupID, $dn); |
|
| 586 | + if($groupName !== false) { |
|
| 587 | + return $groupName; |
|
| 588 | + } |
|
| 589 | + } |
|
| 590 | + |
|
| 591 | + return false; |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + /** |
|
| 595 | + * Get all groups a user belongs to |
|
| 596 | + * @param string $uid Name of the user |
|
| 597 | + * @return array with group names |
|
| 598 | + * |
|
| 599 | + * This function fetches all groups a user belongs to. It does not check |
|
| 600 | + * if the user exists at all. |
|
| 601 | + * |
|
| 602 | + * This function includes groups based on dynamic group membership. |
|
| 603 | + */ |
|
| 604 | + public function getUserGroups($uid) { |
|
| 605 | + if(!$this->enabled) { |
|
| 606 | + return array(); |
|
| 607 | + } |
|
| 608 | + $cacheKey = 'getUserGroups'.$uid; |
|
| 609 | + $userGroups = $this->access->connection->getFromCache($cacheKey); |
|
| 610 | + if(!is_null($userGroups)) { |
|
| 611 | + return $userGroups; |
|
| 612 | + } |
|
| 613 | + $userDN = $this->access->username2dn($uid); |
|
| 614 | + if(!$userDN) { |
|
| 615 | + $this->access->connection->writeToCache($cacheKey, array()); |
|
| 616 | + return array(); |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + $groups = []; |
|
| 620 | + $primaryGroup = $this->getUserPrimaryGroup($userDN); |
|
| 621 | + $gidGroupName = $this->getUserGroupByGid($userDN); |
|
| 622 | + |
|
| 623 | + $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL); |
|
| 624 | + |
|
| 625 | + if (!empty($dynamicGroupMemberURL)) { |
|
| 626 | + // look through dynamic groups to add them to the result array if needed |
|
| 627 | + $groupsToMatch = $this->access->fetchListOfGroups( |
|
| 628 | + $this->access->connection->ldapGroupFilter,array('dn',$dynamicGroupMemberURL)); |
|
| 629 | + foreach($groupsToMatch as $dynamicGroup) { |
|
| 630 | + if (!array_key_exists($dynamicGroupMemberURL, $dynamicGroup)) { |
|
| 631 | + continue; |
|
| 632 | + } |
|
| 633 | + $pos = strpos($dynamicGroup[$dynamicGroupMemberURL][0], '('); |
|
| 634 | + if ($pos !== false) { |
|
| 635 | + $memberUrlFilter = substr($dynamicGroup[$dynamicGroupMemberURL][0],$pos); |
|
| 636 | + // apply filter via ldap search to see if this user is in this |
|
| 637 | + // dynamic group |
|
| 638 | + $userMatch = $this->access->readAttribute( |
|
| 639 | + $userDN, |
|
| 640 | + $this->access->connection->ldapUserDisplayName, |
|
| 641 | + $memberUrlFilter |
|
| 642 | + ); |
|
| 643 | + if ($userMatch !== false) { |
|
| 644 | + // match found so this user is in this group |
|
| 645 | + $groupName = $this->access->dn2groupname($dynamicGroup['dn'][0]); |
|
| 646 | + if(is_string($groupName)) { |
|
| 647 | + // be sure to never return false if the dn could not be |
|
| 648 | + // resolved to a name, for whatever reason. |
|
| 649 | + $groups[] = $groupName; |
|
| 650 | + } |
|
| 651 | + } |
|
| 652 | + } else { |
|
| 653 | + \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
|
| 654 | + 'of group ' . print_r($dynamicGroup, true), \OCP\Util::DEBUG); |
|
| 655 | + } |
|
| 656 | + } |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + // if possible, read out membership via memberOf. It's far faster than |
|
| 660 | + // performing a search, which still is a fallback later. |
|
| 661 | + // memberof doesn't support memberuid, so skip it here. |
|
| 662 | + if(intval($this->access->connection->hasMemberOfFilterSupport) === 1 |
|
| 663 | + && intval($this->access->connection->useMemberOfToDetectMembership) === 1 |
|
| 664 | + && strtolower($this->access->connection->ldapGroupMemberAssocAttr) !== 'memberuid' |
|
| 665 | + ) { |
|
| 666 | + $groupDNs = $this->_getGroupDNsFromMemberOf($userDN); |
|
| 667 | + if (is_array($groupDNs)) { |
|
| 668 | + foreach ($groupDNs as $dn) { |
|
| 669 | + $groupName = $this->access->dn2groupname($dn); |
|
| 670 | + if(is_string($groupName)) { |
|
| 671 | + // be sure to never return false if the dn could not be |
|
| 672 | + // resolved to a name, for whatever reason. |
|
| 673 | + $groups[] = $groupName; |
|
| 674 | + } |
|
| 675 | + } |
|
| 676 | + } |
|
| 677 | + |
|
| 678 | + if($primaryGroup !== false) { |
|
| 679 | + $groups[] = $primaryGroup; |
|
| 680 | + } |
|
| 681 | + if($gidGroupName !== false) { |
|
| 682 | + $groups[] = $gidGroupName; |
|
| 683 | + } |
|
| 684 | + $this->access->connection->writeToCache($cacheKey, $groups); |
|
| 685 | + return $groups; |
|
| 686 | + } |
|
| 687 | + |
|
| 688 | + //uniqueMember takes DN, memberuid the uid, so we need to distinguish |
|
| 689 | + if((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember') |
|
| 690 | + || (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'member') |
|
| 691 | + ) { |
|
| 692 | + $uid = $userDN; |
|
| 693 | + } else if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 694 | + $result = $this->access->readAttribute($userDN, 'uid'); |
|
| 695 | + if ($result === false) { |
|
| 696 | + \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN ' . $userDN . ' on '. |
|
| 697 | + $this->access->connection->ldapHost, \OCP\Util::DEBUG); |
|
| 698 | + } |
|
| 699 | + $uid = $result[0]; |
|
| 700 | + } else { |
|
| 701 | + // just in case |
|
| 702 | + $uid = $userDN; |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + if(isset($this->cachedGroupsByMember[$uid])) { |
|
| 706 | + $groups = array_merge($groups, $this->cachedGroupsByMember[$uid]); |
|
| 707 | + } else { |
|
| 708 | + $groupsByMember = array_values($this->getGroupsByMember($uid)); |
|
| 709 | + $groupsByMember = $this->access->nextcloudGroupNames($groupsByMember); |
|
| 710 | + $this->cachedGroupsByMember[$uid] = $groupsByMember; |
|
| 711 | + $groups = array_merge($groups, $groupsByMember); |
|
| 712 | + } |
|
| 713 | + |
|
| 714 | + if($primaryGroup !== false) { |
|
| 715 | + $groups[] = $primaryGroup; |
|
| 716 | + } |
|
| 717 | + if($gidGroupName !== false) { |
|
| 718 | + $groups[] = $gidGroupName; |
|
| 719 | + } |
|
| 720 | + |
|
| 721 | + $groups = array_unique($groups, SORT_LOCALE_STRING); |
|
| 722 | + $this->access->connection->writeToCache($cacheKey, $groups); |
|
| 723 | + |
|
| 724 | + return $groups; |
|
| 725 | + } |
|
| 726 | + |
|
| 727 | + /** |
|
| 728 | + * @param string $dn |
|
| 729 | + * @param array|null &$seen |
|
| 730 | + * @return array |
|
| 731 | + */ |
|
| 732 | + private function getGroupsByMember($dn, &$seen = null) { |
|
| 733 | + if ($seen === null) { |
|
| 734 | + $seen = array(); |
|
| 735 | + } |
|
| 736 | + $allGroups = array(); |
|
| 737 | + if (array_key_exists($dn, $seen)) { |
|
| 738 | + // avoid loops |
|
| 739 | + return array(); |
|
| 740 | + } |
|
| 741 | + $seen[$dn] = true; |
|
| 742 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 743 | + $this->access->connection->ldapGroupFilter, |
|
| 744 | + $this->access->connection->ldapGroupMemberAssocAttr.'='.$dn |
|
| 745 | + )); |
|
| 746 | + $groups = $this->access->fetchListOfGroups($filter, |
|
| 747 | + array($this->access->connection->ldapGroupDisplayName, 'dn')); |
|
| 748 | + if (is_array($groups)) { |
|
| 749 | + foreach ($groups as $groupobj) { |
|
| 750 | + $groupDN = $groupobj['dn'][0]; |
|
| 751 | + $allGroups[$groupDN] = $groupobj; |
|
| 752 | + $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 753 | + if (!empty($nestedGroups)) { |
|
| 754 | + $supergroups = $this->getGroupsByMember($groupDN, $seen); |
|
| 755 | + if (is_array($supergroups) && (count($supergroups)>0)) { |
|
| 756 | + $allGroups = array_merge($allGroups, $supergroups); |
|
| 757 | + } |
|
| 758 | + } |
|
| 759 | + } |
|
| 760 | + } |
|
| 761 | + return $allGroups; |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + /** |
|
| 765 | + * get a list of all users in a group |
|
| 766 | + * |
|
| 767 | + * @param string $gid |
|
| 768 | + * @param string $search |
|
| 769 | + * @param int $limit |
|
| 770 | + * @param int $offset |
|
| 771 | + * @return array with user ids |
|
| 772 | + */ |
|
| 773 | + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 774 | + if(!$this->enabled) { |
|
| 775 | + return array(); |
|
| 776 | + } |
|
| 777 | + if(!$this->groupExists($gid)) { |
|
| 778 | + return array(); |
|
| 779 | + } |
|
| 780 | + $search = $this->access->escapeFilterPart($search, true); |
|
| 781 | + $cacheKey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset; |
|
| 782 | + // check for cache of the exact query |
|
| 783 | + $groupUsers = $this->access->connection->getFromCache($cacheKey); |
|
| 784 | + if(!is_null($groupUsers)) { |
|
| 785 | + return $groupUsers; |
|
| 786 | + } |
|
| 787 | + |
|
| 788 | + // check for cache of the query without limit and offset |
|
| 789 | + $groupUsers = $this->access->connection->getFromCache('usersInGroup-'.$gid.'-'.$search); |
|
| 790 | + if(!is_null($groupUsers)) { |
|
| 791 | + $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
| 792 | + $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 793 | + return $groupUsers; |
|
| 794 | + } |
|
| 795 | + |
|
| 796 | + if($limit === -1) { |
|
| 797 | + $limit = null; |
|
| 798 | + } |
|
| 799 | + $groupDN = $this->access->groupname2dn($gid); |
|
| 800 | + if(!$groupDN) { |
|
| 801 | + // group couldn't be found, return empty resultset |
|
| 802 | + $this->access->connection->writeToCache($cacheKey, array()); |
|
| 803 | + return array(); |
|
| 804 | + } |
|
| 805 | + |
|
| 806 | + $primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $search, $limit, $offset); |
|
| 807 | + $posixGroupUsers = $this->getUsersInGidNumber($groupDN, $search, $limit, $offset); |
|
| 808 | + $members = array_keys($this->_groupMembers($groupDN)); |
|
| 809 | + if(!$members && empty($posixGroupUsers) && empty($primaryUsers)) { |
|
| 810 | + //in case users could not be retrieved, return empty result set |
|
| 811 | + $this->access->connection->writeToCache($cacheKey, []); |
|
| 812 | + return []; |
|
| 813 | + } |
|
| 814 | + |
|
| 815 | + $groupUsers = array(); |
|
| 816 | + $isMemberUid = (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid'); |
|
| 817 | + $attrs = $this->access->userManager->getAttributes(true); |
|
| 818 | + foreach($members as $member) { |
|
| 819 | + if($isMemberUid) { |
|
| 820 | + //we got uids, need to get their DNs to 'translate' them to user names |
|
| 821 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 822 | + str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
|
| 823 | + $this->access->getFilterPartForUserSearch($search) |
|
| 824 | + )); |
|
| 825 | + $ldap_users = $this->access->fetchListOfUsers($filter, $attrs, 1); |
|
| 826 | + if(count($ldap_users) < 1) { |
|
| 827 | + continue; |
|
| 828 | + } |
|
| 829 | + $groupUsers[] = $this->access->dn2username($ldap_users[0]['dn'][0]); |
|
| 830 | + } else { |
|
| 831 | + //we got DNs, check if we need to filter by search or we can give back all of them |
|
| 832 | + if ($search !== '') { |
|
| 833 | + if(!$this->access->readAttribute($member, |
|
| 834 | + $this->access->connection->ldapUserDisplayName, |
|
| 835 | + $this->access->getFilterPartForUserSearch($search))) { |
|
| 836 | + continue; |
|
| 837 | + } |
|
| 838 | + } |
|
| 839 | + // dn2username will also check if the users belong to the allowed base |
|
| 840 | + if($ocname = $this->access->dn2username($member)) { |
|
| 841 | + $groupUsers[] = $ocname; |
|
| 842 | + } |
|
| 843 | + } |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + $groupUsers = array_unique(array_merge($groupUsers, $primaryUsers, $posixGroupUsers)); |
|
| 847 | + natsort($groupUsers); |
|
| 848 | + $this->access->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers); |
|
| 849 | + $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
| 850 | + |
|
| 851 | + $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 852 | + |
|
| 853 | + return $groupUsers; |
|
| 854 | + } |
|
| 855 | + |
|
| 856 | + /** |
|
| 857 | + * returns the number of users in a group, who match the search term |
|
| 858 | + * @param string $gid the internal group name |
|
| 859 | + * @param string $search optional, a search string |
|
| 860 | + * @return int|bool |
|
| 861 | + */ |
|
| 862 | + public function countUsersInGroup($gid, $search = '') { |
|
| 863 | + $cacheKey = 'countUsersInGroup-'.$gid.'-'.$search; |
|
| 864 | + if(!$this->enabled || !$this->groupExists($gid)) { |
|
| 865 | + return false; |
|
| 866 | + } |
|
| 867 | + $groupUsers = $this->access->connection->getFromCache($cacheKey); |
|
| 868 | + if(!is_null($groupUsers)) { |
|
| 869 | + return $groupUsers; |
|
| 870 | + } |
|
| 871 | + |
|
| 872 | + $groupDN = $this->access->groupname2dn($gid); |
|
| 873 | + if(!$groupDN) { |
|
| 874 | + // group couldn't be found, return empty result set |
|
| 875 | + $this->access->connection->writeToCache($cacheKey, false); |
|
| 876 | + return false; |
|
| 877 | + } |
|
| 878 | + |
|
| 879 | + $members = array_keys($this->_groupMembers($groupDN)); |
|
| 880 | + $primaryUserCount = $this->countUsersInPrimaryGroup($groupDN, ''); |
|
| 881 | + if(!$members && $primaryUserCount === 0) { |
|
| 882 | + //in case users could not be retrieved, return empty result set |
|
| 883 | + $this->access->connection->writeToCache($cacheKey, false); |
|
| 884 | + return false; |
|
| 885 | + } |
|
| 886 | + |
|
| 887 | + if ($search === '') { |
|
| 888 | + $groupUsers = count($members) + $primaryUserCount; |
|
| 889 | + $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 890 | + return $groupUsers; |
|
| 891 | + } |
|
| 892 | + $search = $this->access->escapeFilterPart($search, true); |
|
| 893 | + $isMemberUid = |
|
| 894 | + (strtolower($this->access->connection->ldapGroupMemberAssocAttr) |
|
| 895 | + === 'memberuid'); |
|
| 896 | + |
|
| 897 | + //we need to apply the search filter |
|
| 898 | + //alternatives that need to be checked: |
|
| 899 | + //a) get all users by search filter and array_intersect them |
|
| 900 | + //b) a, but only when less than 1k 10k ?k users like it is |
|
| 901 | + //c) put all DNs|uids in a LDAP filter, combine with the search string |
|
| 902 | + // and let it count. |
|
| 903 | + //For now this is not important, because the only use of this method |
|
| 904 | + //does not supply a search string |
|
| 905 | + $groupUsers = array(); |
|
| 906 | + foreach($members as $member) { |
|
| 907 | + if($isMemberUid) { |
|
| 908 | + //we got uids, need to get their DNs to 'translate' them to user names |
|
| 909 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 910 | + str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
|
| 911 | + $this->access->getFilterPartForUserSearch($search) |
|
| 912 | + )); |
|
| 913 | + $ldap_users = $this->access->fetchListOfUsers($filter, 'dn', 1); |
|
| 914 | + if(count($ldap_users) < 1) { |
|
| 915 | + continue; |
|
| 916 | + } |
|
| 917 | + $groupUsers[] = $this->access->dn2username($ldap_users[0]); |
|
| 918 | + } else { |
|
| 919 | + //we need to apply the search filter now |
|
| 920 | + if(!$this->access->readAttribute($member, |
|
| 921 | + $this->access->connection->ldapUserDisplayName, |
|
| 922 | + $this->access->getFilterPartForUserSearch($search))) { |
|
| 923 | + continue; |
|
| 924 | + } |
|
| 925 | + // dn2username will also check if the users belong to the allowed base |
|
| 926 | + if($ocname = $this->access->dn2username($member)) { |
|
| 927 | + $groupUsers[] = $ocname; |
|
| 928 | + } |
|
| 929 | + } |
|
| 930 | + } |
|
| 931 | + |
|
| 932 | + //and get users that have the group as primary |
|
| 933 | + $primaryUsers = $this->countUsersInPrimaryGroup($groupDN, $search); |
|
| 934 | + |
|
| 935 | + return count($groupUsers) + $primaryUsers; |
|
| 936 | + } |
|
| 937 | + |
|
| 938 | + /** |
|
| 939 | + * get a list of all groups |
|
| 940 | + * |
|
| 941 | + * @param string $search |
|
| 942 | + * @param $limit |
|
| 943 | + * @param int $offset |
|
| 944 | + * @return array with group names |
|
| 945 | + * |
|
| 946 | + * Returns a list with all groups (used by getGroups) |
|
| 947 | + */ |
|
| 948 | + protected function getGroupsChunk($search = '', $limit = -1, $offset = 0) { |
|
| 949 | + if(!$this->enabled) { |
|
| 950 | + return array(); |
|
| 951 | + } |
|
| 952 | + $cacheKey = 'getGroups-'.$search.'-'.$limit.'-'.$offset; |
|
| 953 | + |
|
| 954 | + //Check cache before driving unnecessary searches |
|
| 955 | + \OCP\Util::writeLog('user_ldap', 'getGroups '.$cacheKey, \OCP\Util::DEBUG); |
|
| 956 | + $ldap_groups = $this->access->connection->getFromCache($cacheKey); |
|
| 957 | + if(!is_null($ldap_groups)) { |
|
| 958 | + return $ldap_groups; |
|
| 959 | + } |
|
| 960 | + |
|
| 961 | + // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
| 962 | + // error. With a limit of 0, we get 0 results. So we pass null. |
|
| 963 | + if($limit <= 0) { |
|
| 964 | + $limit = null; |
|
| 965 | + } |
|
| 966 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 967 | + $this->access->connection->ldapGroupFilter, |
|
| 968 | + $this->access->getFilterPartForGroupSearch($search) |
|
| 969 | + )); |
|
| 970 | + \OCP\Util::writeLog('user_ldap', 'getGroups Filter '.$filter, \OCP\Util::DEBUG); |
|
| 971 | + $ldap_groups = $this->access->fetchListOfGroups($filter, |
|
| 972 | + array($this->access->connection->ldapGroupDisplayName, 'dn'), |
|
| 973 | + $limit, |
|
| 974 | + $offset); |
|
| 975 | + $ldap_groups = $this->access->nextcloudGroupNames($ldap_groups); |
|
| 976 | + |
|
| 977 | + $this->access->connection->writeToCache($cacheKey, $ldap_groups); |
|
| 978 | + return $ldap_groups; |
|
| 979 | + } |
|
| 980 | + |
|
| 981 | + /** |
|
| 982 | + * get a list of all groups using a paged search |
|
| 983 | + * |
|
| 984 | + * @param string $search |
|
| 985 | + * @param int $limit |
|
| 986 | + * @param int $offset |
|
| 987 | + * @return array with group names |
|
| 988 | + * |
|
| 989 | + * Returns a list with all groups |
|
| 990 | + * Uses a paged search if available to override a |
|
| 991 | + * server side search limit. |
|
| 992 | + * (active directory has a limit of 1000 by default) |
|
| 993 | + */ |
|
| 994 | + public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
| 995 | + if(!$this->enabled) { |
|
| 996 | + return array(); |
|
| 997 | + } |
|
| 998 | + $search = $this->access->escapeFilterPart($search, true); |
|
| 999 | + $pagingSize = intval($this->access->connection->ldapPagingSize); |
|
| 1000 | + if (!$this->access->connection->hasPagedResultSupport || $pagingSize <= 0) { |
|
| 1001 | + return $this->getGroupsChunk($search, $limit, $offset); |
|
| 1002 | + } |
|
| 1003 | + $maxGroups = 100000; // limit max results (just for safety reasons) |
|
| 1004 | + if ($limit > -1) { |
|
| 1005 | + $overallLimit = min($limit + $offset, $maxGroups); |
|
| 1006 | + } else { |
|
| 1007 | + $overallLimit = $maxGroups; |
|
| 1008 | + } |
|
| 1009 | + $chunkOffset = $offset; |
|
| 1010 | + $allGroups = array(); |
|
| 1011 | + while ($chunkOffset < $overallLimit) { |
|
| 1012 | + $chunkLimit = min($pagingSize, $overallLimit - $chunkOffset); |
|
| 1013 | + $ldapGroups = $this->getGroupsChunk($search, $chunkLimit, $chunkOffset); |
|
| 1014 | + $nread = count($ldapGroups); |
|
| 1015 | + \OCP\Util::writeLog('user_ldap', 'getGroups('.$search.'): read '.$nread.' at offset '.$chunkOffset.' (limit: '.$chunkLimit.')', \OCP\Util::DEBUG); |
|
| 1016 | + if ($nread) { |
|
| 1017 | + $allGroups = array_merge($allGroups, $ldapGroups); |
|
| 1018 | + $chunkOffset += $nread; |
|
| 1019 | + } |
|
| 1020 | + if ($nread < $chunkLimit) { |
|
| 1021 | + break; |
|
| 1022 | + } |
|
| 1023 | + } |
|
| 1024 | + return $allGroups; |
|
| 1025 | + } |
|
| 1026 | + |
|
| 1027 | + /** |
|
| 1028 | + * @param string $group |
|
| 1029 | + * @return bool |
|
| 1030 | + */ |
|
| 1031 | + public function groupMatchesFilter($group) { |
|
| 1032 | + return (strripos($group, $this->groupSearch) !== false); |
|
| 1033 | + } |
|
| 1034 | + |
|
| 1035 | + /** |
|
| 1036 | + * check if a group exists |
|
| 1037 | + * @param string $gid |
|
| 1038 | + * @return bool |
|
| 1039 | + */ |
|
| 1040 | + public function groupExists($gid) { |
|
| 1041 | + $groupExists = $this->access->connection->getFromCache('groupExists'.$gid); |
|
| 1042 | + if(!is_null($groupExists)) { |
|
| 1043 | + return (bool)$groupExists; |
|
| 1044 | + } |
|
| 1045 | + |
|
| 1046 | + //getting dn, if false the group does not exist. If dn, it may be mapped |
|
| 1047 | + //only, requires more checking. |
|
| 1048 | + $dn = $this->access->groupname2dn($gid); |
|
| 1049 | + if(!$dn) { |
|
| 1050 | + $this->access->connection->writeToCache('groupExists'.$gid, false); |
|
| 1051 | + return false; |
|
| 1052 | + } |
|
| 1053 | + |
|
| 1054 | + //if group really still exists, we will be able to read its objectclass |
|
| 1055 | + if(!is_array($this->access->readAttribute($dn, ''))) { |
|
| 1056 | + $this->access->connection->writeToCache('groupExists'.$gid, false); |
|
| 1057 | + return false; |
|
| 1058 | + } |
|
| 1059 | + |
|
| 1060 | + $this->access->connection->writeToCache('groupExists'.$gid, true); |
|
| 1061 | + return true; |
|
| 1062 | + } |
|
| 1063 | + |
|
| 1064 | + /** |
|
| 1065 | + * Check if backend implements actions |
|
| 1066 | + * @param int $actions bitwise-or'ed actions |
|
| 1067 | + * @return boolean |
|
| 1068 | + * |
|
| 1069 | + * Returns the supported actions as int to be |
|
| 1070 | + * compared with \OC\User\Backend::CREATE_USER etc. |
|
| 1071 | + */ |
|
| 1072 | + public function implementsActions($actions) { |
|
| 1073 | + return (bool)(\OC\Group\Backend::COUNT_USERS & $actions); |
|
| 1074 | + } |
|
| 1075 | + |
|
| 1076 | + /** |
|
| 1077 | + * Return access for LDAP interaction. |
|
| 1078 | + * @return Access instance of Access for LDAP interaction |
|
| 1079 | + */ |
|
| 1080 | + public function getLDAPAccess() { |
|
| 1081 | + return $this->access; |
|
| 1082 | + } |
|
| 1083 | 1083 | } |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | parent::__construct($access); |
| 58 | 58 | $filter = $this->access->connection->ldapGroupFilter; |
| 59 | 59 | $gassoc = $this->access->connection->ldapGroupMemberAssocAttr; |
| 60 | - if(!empty($filter) && !empty($gassoc)) { |
|
| 60 | + if (!empty($filter) && !empty($gassoc)) { |
|
| 61 | 61 | $this->enabled = true; |
| 62 | 62 | } |
| 63 | 63 | |
@@ -74,25 +74,25 @@ discard block |
||
| 74 | 74 | * Checks whether the user is member of a group or not. |
| 75 | 75 | */ |
| 76 | 76 | public function inGroup($uid, $gid) { |
| 77 | - if(!$this->enabled) { |
|
| 77 | + if (!$this->enabled) { |
|
| 78 | 78 | return false; |
| 79 | 79 | } |
| 80 | 80 | $cacheKey = 'inGroup'.$uid.':'.$gid; |
| 81 | 81 | $inGroup = $this->access->connection->getFromCache($cacheKey); |
| 82 | - if(!is_null($inGroup)) { |
|
| 83 | - return (bool)$inGroup; |
|
| 82 | + if (!is_null($inGroup)) { |
|
| 83 | + return (bool) $inGroup; |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | $userDN = $this->access->username2dn($uid); |
| 87 | 87 | |
| 88 | - if(isset($this->cachedGroupMembers[$gid])) { |
|
| 88 | + if (isset($this->cachedGroupMembers[$gid])) { |
|
| 89 | 89 | $isInGroup = in_array($userDN, $this->cachedGroupMembers[$gid]); |
| 90 | 90 | return $isInGroup; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | $cacheKeyMembers = 'inGroup-members:'.$gid; |
| 94 | 94 | $members = $this->access->connection->getFromCache($cacheKeyMembers); |
| 95 | - if(!is_null($members)) { |
|
| 95 | + if (!is_null($members)) { |
|
| 96 | 96 | $this->cachedGroupMembers[$gid] = $members; |
| 97 | 97 | $isInGroup = in_array($userDN, $members); |
| 98 | 98 | $this->access->connection->writeToCache($cacheKey, $isInGroup); |
@@ -101,13 +101,13 @@ discard block |
||
| 101 | 101 | |
| 102 | 102 | $groupDN = $this->access->groupname2dn($gid); |
| 103 | 103 | // just in case |
| 104 | - if(!$groupDN || !$userDN) { |
|
| 104 | + if (!$groupDN || !$userDN) { |
|
| 105 | 105 | $this->access->connection->writeToCache($cacheKey, false); |
| 106 | 106 | return false; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | //check primary group first |
| 110 | - if($gid === $this->getUserPrimaryGroup($userDN)) { |
|
| 110 | + if ($gid === $this->getUserPrimaryGroup($userDN)) { |
|
| 111 | 111 | $this->access->connection->writeToCache($cacheKey, true); |
| 112 | 112 | return true; |
| 113 | 113 | } |
@@ -115,21 +115,21 @@ discard block |
||
| 115 | 115 | //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course. |
| 116 | 116 | $members = $this->_groupMembers($groupDN); |
| 117 | 117 | $members = array_keys($members); // uids are returned as keys |
| 118 | - if(!is_array($members) || count($members) === 0) { |
|
| 118 | + if (!is_array($members) || count($members) === 0) { |
|
| 119 | 119 | $this->access->connection->writeToCache($cacheKey, false); |
| 120 | 120 | return false; |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | //extra work if we don't get back user DNs |
| 124 | - if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 124 | + if (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 125 | 125 | $dns = array(); |
| 126 | 126 | $filterParts = array(); |
| 127 | 127 | $bytes = 0; |
| 128 | - foreach($members as $mid) { |
|
| 128 | + foreach ($members as $mid) { |
|
| 129 | 129 | $filter = str_replace('%uid', $mid, $this->access->connection->ldapLoginFilter); |
| 130 | 130 | $filterParts[] = $filter; |
| 131 | 131 | $bytes += strlen($filter); |
| 132 | - if($bytes >= 9000000) { |
|
| 132 | + if ($bytes >= 9000000) { |
|
| 133 | 133 | // AD has a default input buffer of 10 MB, we do not want |
| 134 | 134 | // to take even the chance to exceed it |
| 135 | 135 | $filter = $this->access->combineFilterWithOr($filterParts); |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | $dns = array_merge($dns, $users); |
| 140 | 140 | } |
| 141 | 141 | } |
| 142 | - if(count($filterParts) > 0) { |
|
| 142 | + if (count($filterParts) > 0) { |
|
| 143 | 143 | $filter = $this->access->combineFilterWithOr($filterParts); |
| 144 | 144 | $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
| 145 | 145 | $dns = array_merge($dns, $users); |
@@ -182,14 +182,14 @@ discard block |
||
| 182 | 182 | $pos = strpos($memberURLs[0], '('); |
| 183 | 183 | if ($pos !== false) { |
| 184 | 184 | $memberUrlFilter = substr($memberURLs[0], $pos); |
| 185 | - $foundMembers = $this->access->searchUsers($memberUrlFilter,'dn'); |
|
| 185 | + $foundMembers = $this->access->searchUsers($memberUrlFilter, 'dn'); |
|
| 186 | 186 | $dynamicMembers = array(); |
| 187 | - foreach($foundMembers as $value) { |
|
| 187 | + foreach ($foundMembers as $value) { |
|
| 188 | 188 | $dynamicMembers[$value['dn'][0]] = 1; |
| 189 | 189 | } |
| 190 | 190 | } else { |
| 191 | 191 | \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
| 192 | - 'of group ' . $dnGroup, \OCP\Util::DEBUG); |
|
| 192 | + 'of group '.$dnGroup, \OCP\Util::DEBUG); |
|
| 193 | 193 | } |
| 194 | 194 | } |
| 195 | 195 | return $dynamicMembers; |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | // used extensively in cron job, caching makes sense for nested groups |
| 213 | 213 | $cacheKey = '_groupMembers'.$dnGroup; |
| 214 | 214 | $groupMembers = $this->access->connection->getFromCache($cacheKey); |
| 215 | - if(!is_null($groupMembers)) { |
|
| 215 | + if (!is_null($groupMembers)) { |
|
| 216 | 216 | return $groupMembers; |
| 217 | 217 | } |
| 218 | 218 | $seen[$dnGroup] = 1; |
@@ -256,7 +256,7 @@ discard block |
||
| 256 | 256 | return array(); |
| 257 | 257 | } |
| 258 | 258 | $groups = $this->access->groupsMatchFilter($groups); |
| 259 | - $allGroups = $groups; |
|
| 259 | + $allGroups = $groups; |
|
| 260 | 260 | $nestedGroups = $this->access->connection->ldapNestedGroups; |
| 261 | 261 | if (intval($nestedGroups) === 1) { |
| 262 | 262 | foreach ($groups as $group) { |
@@ -274,9 +274,9 @@ discard block |
||
| 274 | 274 | * @return string|bool |
| 275 | 275 | */ |
| 276 | 276 | public function gidNumber2Name($gid, $dn) { |
| 277 | - $cacheKey = 'gidNumberToName' . $gid; |
|
| 277 | + $cacheKey = 'gidNumberToName'.$gid; |
|
| 278 | 278 | $groupName = $this->access->connection->getFromCache($cacheKey); |
| 279 | - if(!is_null($groupName) && isset($groupName)) { |
|
| 279 | + if (!is_null($groupName) && isset($groupName)) { |
|
| 280 | 280 | return $groupName; |
| 281 | 281 | } |
| 282 | 282 | |
@@ -284,10 +284,10 @@ discard block |
||
| 284 | 284 | $filter = $this->access->combineFilterWithAnd([ |
| 285 | 285 | $this->access->connection->ldapGroupFilter, |
| 286 | 286 | 'objectClass=posixGroup', |
| 287 | - $this->access->connection->ldapGidNumber . '=' . $gid |
|
| 287 | + $this->access->connection->ldapGidNumber.'='.$gid |
|
| 288 | 288 | ]); |
| 289 | 289 | $result = $this->access->searchGroups($filter, array('dn'), 1); |
| 290 | - if(empty($result)) { |
|
| 290 | + if (empty($result)) { |
|
| 291 | 291 | return false; |
| 292 | 292 | } |
| 293 | 293 | $dn = $result[0]['dn'][0]; |
@@ -310,7 +310,7 @@ discard block |
||
| 310 | 310 | */ |
| 311 | 311 | private function getEntryGidNumber($dn, $attribute) { |
| 312 | 312 | $value = $this->access->readAttribute($dn, $attribute); |
| 313 | - if(is_array($value) && !empty($value)) { |
|
| 313 | + if (is_array($value) && !empty($value)) { |
|
| 314 | 314 | return $value[0]; |
| 315 | 315 | } |
| 316 | 316 | return false; |
@@ -332,9 +332,9 @@ discard block |
||
| 332 | 332 | */ |
| 333 | 333 | public function getUserGidNumber($dn) { |
| 334 | 334 | $gidNumber = false; |
| 335 | - if($this->access->connection->hasGidNumber) { |
|
| 335 | + if ($this->access->connection->hasGidNumber) { |
|
| 336 | 336 | $gidNumber = $this->getEntryGidNumber($dn, $this->access->connection->ldapGidNumber); |
| 337 | - if($gidNumber === false) { |
|
| 337 | + if ($gidNumber === false) { |
|
| 338 | 338 | $this->access->connection->hasGidNumber = false; |
| 339 | 339 | } |
| 340 | 340 | } |
@@ -351,7 +351,7 @@ discard block |
||
| 351 | 351 | */ |
| 352 | 352 | private function prepareFilterForUsersHasGidNumber($groupDN, $search = '') { |
| 353 | 353 | $groupID = $this->getGroupGidNumber($groupDN); |
| 354 | - if($groupID === false) { |
|
| 354 | + if ($groupID === false) { |
|
| 355 | 355 | throw new \Exception('Not a valid group'); |
| 356 | 356 | } |
| 357 | 357 | |
@@ -360,7 +360,7 @@ discard block |
||
| 360 | 360 | if ($search !== '') { |
| 361 | 361 | $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
| 362 | 362 | } |
| 363 | - $filterParts[] = $this->access->connection->ldapGidNumber .'=' . $groupID; |
|
| 363 | + $filterParts[] = $this->access->connection->ldapGidNumber.'='.$groupID; |
|
| 364 | 364 | |
| 365 | 365 | $filter = $this->access->combineFilterWithAnd($filterParts); |
| 366 | 366 | |
@@ -404,7 +404,7 @@ discard block |
||
| 404 | 404 | try { |
| 405 | 405 | $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search); |
| 406 | 406 | $users = $this->access->countUsers($filter, ['dn'], $limit, $offset); |
| 407 | - return (int)$users; |
|
| 407 | + return (int) $users; |
|
| 408 | 408 | } catch (\Exception $e) { |
| 409 | 409 | return 0; |
| 410 | 410 | } |
@@ -417,9 +417,9 @@ discard block |
||
| 417 | 417 | */ |
| 418 | 418 | public function getUserGroupByGid($dn) { |
| 419 | 419 | $groupID = $this->getUserGidNumber($dn); |
| 420 | - if($groupID !== false) { |
|
| 420 | + if ($groupID !== false) { |
|
| 421 | 421 | $groupName = $this->gidNumber2Name($groupID, $dn); |
| 422 | - if($groupName !== false) { |
|
| 422 | + if ($groupName !== false) { |
|
| 423 | 423 | return $groupName; |
| 424 | 424 | } |
| 425 | 425 | } |
@@ -436,22 +436,22 @@ discard block |
||
| 436 | 436 | public function primaryGroupID2Name($gid, $dn) { |
| 437 | 437 | $cacheKey = 'primaryGroupIDtoName'; |
| 438 | 438 | $groupNames = $this->access->connection->getFromCache($cacheKey); |
| 439 | - if(!is_null($groupNames) && isset($groupNames[$gid])) { |
|
| 439 | + if (!is_null($groupNames) && isset($groupNames[$gid])) { |
|
| 440 | 440 | return $groupNames[$gid]; |
| 441 | 441 | } |
| 442 | 442 | |
| 443 | 443 | $domainObjectSid = $this->access->getSID($dn); |
| 444 | - if($domainObjectSid === false) { |
|
| 444 | + if ($domainObjectSid === false) { |
|
| 445 | 445 | return false; |
| 446 | 446 | } |
| 447 | 447 | |
| 448 | 448 | //we need to get the DN from LDAP |
| 449 | 449 | $filter = $this->access->combineFilterWithAnd(array( |
| 450 | 450 | $this->access->connection->ldapGroupFilter, |
| 451 | - 'objectsid=' . $domainObjectSid . '-' . $gid |
|
| 451 | + 'objectsid='.$domainObjectSid.'-'.$gid |
|
| 452 | 452 | )); |
| 453 | 453 | $result = $this->access->searchGroups($filter, array('dn'), 1); |
| 454 | - if(empty($result)) { |
|
| 454 | + if (empty($result)) { |
|
| 455 | 455 | return false; |
| 456 | 456 | } |
| 457 | 457 | $dn = $result[0]['dn'][0]; |
@@ -474,7 +474,7 @@ discard block |
||
| 474 | 474 | */ |
| 475 | 475 | private function getEntryGroupID($dn, $attribute) { |
| 476 | 476 | $value = $this->access->readAttribute($dn, $attribute); |
| 477 | - if(is_array($value) && !empty($value)) { |
|
| 477 | + if (is_array($value) && !empty($value)) { |
|
| 478 | 478 | return $value[0]; |
| 479 | 479 | } |
| 480 | 480 | return false; |
@@ -496,9 +496,9 @@ discard block |
||
| 496 | 496 | */ |
| 497 | 497 | public function getUserPrimaryGroupIDs($dn) { |
| 498 | 498 | $primaryGroupID = false; |
| 499 | - if($this->access->connection->hasPrimaryGroups) { |
|
| 499 | + if ($this->access->connection->hasPrimaryGroups) { |
|
| 500 | 500 | $primaryGroupID = $this->getEntryGroupID($dn, 'primaryGroupID'); |
| 501 | - if($primaryGroupID === false) { |
|
| 501 | + if ($primaryGroupID === false) { |
|
| 502 | 502 | $this->access->connection->hasPrimaryGroups = false; |
| 503 | 503 | } |
| 504 | 504 | } |
@@ -515,7 +515,7 @@ discard block |
||
| 515 | 515 | */ |
| 516 | 516 | private function prepareFilterForUsersInPrimaryGroup($groupDN, $search = '') { |
| 517 | 517 | $groupID = $this->getGroupPrimaryGroupID($groupDN); |
| 518 | - if($groupID === false) { |
|
| 518 | + if ($groupID === false) { |
|
| 519 | 519 | throw new \Exception('Not a valid group'); |
| 520 | 520 | } |
| 521 | 521 | |
@@ -524,7 +524,7 @@ discard block |
||
| 524 | 524 | if ($search !== '') { |
| 525 | 525 | $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
| 526 | 526 | } |
| 527 | - $filterParts[] = 'primaryGroupID=' . $groupID; |
|
| 527 | + $filterParts[] = 'primaryGroupID='.$groupID; |
|
| 528 | 528 | |
| 529 | 529 | $filter = $this->access->combineFilterWithAnd($filterParts); |
| 530 | 530 | |
@@ -568,7 +568,7 @@ discard block |
||
| 568 | 568 | try { |
| 569 | 569 | $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
| 570 | 570 | $users = $this->access->countUsers($filter, array('dn'), $limit, $offset); |
| 571 | - return (int)$users; |
|
| 571 | + return (int) $users; |
|
| 572 | 572 | } catch (\Exception $e) { |
| 573 | 573 | return 0; |
| 574 | 574 | } |
@@ -581,9 +581,9 @@ discard block |
||
| 581 | 581 | */ |
| 582 | 582 | public function getUserPrimaryGroup($dn) { |
| 583 | 583 | $groupID = $this->getUserPrimaryGroupIDs($dn); |
| 584 | - if($groupID !== false) { |
|
| 584 | + if ($groupID !== false) { |
|
| 585 | 585 | $groupName = $this->primaryGroupID2Name($groupID, $dn); |
| 586 | - if($groupName !== false) { |
|
| 586 | + if ($groupName !== false) { |
|
| 587 | 587 | return $groupName; |
| 588 | 588 | } |
| 589 | 589 | } |
@@ -602,16 +602,16 @@ discard block |
||
| 602 | 602 | * This function includes groups based on dynamic group membership. |
| 603 | 603 | */ |
| 604 | 604 | public function getUserGroups($uid) { |
| 605 | - if(!$this->enabled) { |
|
| 605 | + if (!$this->enabled) { |
|
| 606 | 606 | return array(); |
| 607 | 607 | } |
| 608 | 608 | $cacheKey = 'getUserGroups'.$uid; |
| 609 | 609 | $userGroups = $this->access->connection->getFromCache($cacheKey); |
| 610 | - if(!is_null($userGroups)) { |
|
| 610 | + if (!is_null($userGroups)) { |
|
| 611 | 611 | return $userGroups; |
| 612 | 612 | } |
| 613 | 613 | $userDN = $this->access->username2dn($uid); |
| 614 | - if(!$userDN) { |
|
| 614 | + if (!$userDN) { |
|
| 615 | 615 | $this->access->connection->writeToCache($cacheKey, array()); |
| 616 | 616 | return array(); |
| 617 | 617 | } |
@@ -625,14 +625,14 @@ discard block |
||
| 625 | 625 | if (!empty($dynamicGroupMemberURL)) { |
| 626 | 626 | // look through dynamic groups to add them to the result array if needed |
| 627 | 627 | $groupsToMatch = $this->access->fetchListOfGroups( |
| 628 | - $this->access->connection->ldapGroupFilter,array('dn',$dynamicGroupMemberURL)); |
|
| 629 | - foreach($groupsToMatch as $dynamicGroup) { |
|
| 628 | + $this->access->connection->ldapGroupFilter, array('dn', $dynamicGroupMemberURL)); |
|
| 629 | + foreach ($groupsToMatch as $dynamicGroup) { |
|
| 630 | 630 | if (!array_key_exists($dynamicGroupMemberURL, $dynamicGroup)) { |
| 631 | 631 | continue; |
| 632 | 632 | } |
| 633 | 633 | $pos = strpos($dynamicGroup[$dynamicGroupMemberURL][0], '('); |
| 634 | 634 | if ($pos !== false) { |
| 635 | - $memberUrlFilter = substr($dynamicGroup[$dynamicGroupMemberURL][0],$pos); |
|
| 635 | + $memberUrlFilter = substr($dynamicGroup[$dynamicGroupMemberURL][0], $pos); |
|
| 636 | 636 | // apply filter via ldap search to see if this user is in this |
| 637 | 637 | // dynamic group |
| 638 | 638 | $userMatch = $this->access->readAttribute( |
@@ -643,7 +643,7 @@ discard block |
||
| 643 | 643 | if ($userMatch !== false) { |
| 644 | 644 | // match found so this user is in this group |
| 645 | 645 | $groupName = $this->access->dn2groupname($dynamicGroup['dn'][0]); |
| 646 | - if(is_string($groupName)) { |
|
| 646 | + if (is_string($groupName)) { |
|
| 647 | 647 | // be sure to never return false if the dn could not be |
| 648 | 648 | // resolved to a name, for whatever reason. |
| 649 | 649 | $groups[] = $groupName; |
@@ -651,7 +651,7 @@ discard block |
||
| 651 | 651 | } |
| 652 | 652 | } else { |
| 653 | 653 | \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
| 654 | - 'of group ' . print_r($dynamicGroup, true), \OCP\Util::DEBUG); |
|
| 654 | + 'of group '.print_r($dynamicGroup, true), \OCP\Util::DEBUG); |
|
| 655 | 655 | } |
| 656 | 656 | } |
| 657 | 657 | } |
@@ -659,7 +659,7 @@ discard block |
||
| 659 | 659 | // if possible, read out membership via memberOf. It's far faster than |
| 660 | 660 | // performing a search, which still is a fallback later. |
| 661 | 661 | // memberof doesn't support memberuid, so skip it here. |
| 662 | - if(intval($this->access->connection->hasMemberOfFilterSupport) === 1 |
|
| 662 | + if (intval($this->access->connection->hasMemberOfFilterSupport) === 1 |
|
| 663 | 663 | && intval($this->access->connection->useMemberOfToDetectMembership) === 1 |
| 664 | 664 | && strtolower($this->access->connection->ldapGroupMemberAssocAttr) !== 'memberuid' |
| 665 | 665 | ) { |
@@ -667,7 +667,7 @@ discard block |
||
| 667 | 667 | if (is_array($groupDNs)) { |
| 668 | 668 | foreach ($groupDNs as $dn) { |
| 669 | 669 | $groupName = $this->access->dn2groupname($dn); |
| 670 | - if(is_string($groupName)) { |
|
| 670 | + if (is_string($groupName)) { |
|
| 671 | 671 | // be sure to never return false if the dn could not be |
| 672 | 672 | // resolved to a name, for whatever reason. |
| 673 | 673 | $groups[] = $groupName; |
@@ -675,10 +675,10 @@ discard block |
||
| 675 | 675 | } |
| 676 | 676 | } |
| 677 | 677 | |
| 678 | - if($primaryGroup !== false) { |
|
| 678 | + if ($primaryGroup !== false) { |
|
| 679 | 679 | $groups[] = $primaryGroup; |
| 680 | 680 | } |
| 681 | - if($gidGroupName !== false) { |
|
| 681 | + if ($gidGroupName !== false) { |
|
| 682 | 682 | $groups[] = $gidGroupName; |
| 683 | 683 | } |
| 684 | 684 | $this->access->connection->writeToCache($cacheKey, $groups); |
@@ -686,14 +686,14 @@ discard block |
||
| 686 | 686 | } |
| 687 | 687 | |
| 688 | 688 | //uniqueMember takes DN, memberuid the uid, so we need to distinguish |
| 689 | - if((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember') |
|
| 689 | + if ((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember') |
|
| 690 | 690 | || (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'member') |
| 691 | 691 | ) { |
| 692 | 692 | $uid = $userDN; |
| 693 | - } else if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 693 | + } else if (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 694 | 694 | $result = $this->access->readAttribute($userDN, 'uid'); |
| 695 | 695 | if ($result === false) { |
| 696 | - \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN ' . $userDN . ' on '. |
|
| 696 | + \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN '.$userDN.' on '. |
|
| 697 | 697 | $this->access->connection->ldapHost, \OCP\Util::DEBUG); |
| 698 | 698 | } |
| 699 | 699 | $uid = $result[0]; |
@@ -702,7 +702,7 @@ discard block |
||
| 702 | 702 | $uid = $userDN; |
| 703 | 703 | } |
| 704 | 704 | |
| 705 | - if(isset($this->cachedGroupsByMember[$uid])) { |
|
| 705 | + if (isset($this->cachedGroupsByMember[$uid])) { |
|
| 706 | 706 | $groups = array_merge($groups, $this->cachedGroupsByMember[$uid]); |
| 707 | 707 | } else { |
| 708 | 708 | $groupsByMember = array_values($this->getGroupsByMember($uid)); |
@@ -711,10 +711,10 @@ discard block |
||
| 711 | 711 | $groups = array_merge($groups, $groupsByMember); |
| 712 | 712 | } |
| 713 | 713 | |
| 714 | - if($primaryGroup !== false) { |
|
| 714 | + if ($primaryGroup !== false) { |
|
| 715 | 715 | $groups[] = $primaryGroup; |
| 716 | 716 | } |
| 717 | - if($gidGroupName !== false) { |
|
| 717 | + if ($gidGroupName !== false) { |
|
| 718 | 718 | $groups[] = $gidGroupName; |
| 719 | 719 | } |
| 720 | 720 | |
@@ -752,7 +752,7 @@ discard block |
||
| 752 | 752 | $nestedGroups = $this->access->connection->ldapNestedGroups; |
| 753 | 753 | if (!empty($nestedGroups)) { |
| 754 | 754 | $supergroups = $this->getGroupsByMember($groupDN, $seen); |
| 755 | - if (is_array($supergroups) && (count($supergroups)>0)) { |
|
| 755 | + if (is_array($supergroups) && (count($supergroups) > 0)) { |
|
| 756 | 756 | $allGroups = array_merge($allGroups, $supergroups); |
| 757 | 757 | } |
| 758 | 758 | } |
@@ -771,33 +771,33 @@ discard block |
||
| 771 | 771 | * @return array with user ids |
| 772 | 772 | */ |
| 773 | 773 | public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
| 774 | - if(!$this->enabled) { |
|
| 774 | + if (!$this->enabled) { |
|
| 775 | 775 | return array(); |
| 776 | 776 | } |
| 777 | - if(!$this->groupExists($gid)) { |
|
| 777 | + if (!$this->groupExists($gid)) { |
|
| 778 | 778 | return array(); |
| 779 | 779 | } |
| 780 | 780 | $search = $this->access->escapeFilterPart($search, true); |
| 781 | 781 | $cacheKey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset; |
| 782 | 782 | // check for cache of the exact query |
| 783 | 783 | $groupUsers = $this->access->connection->getFromCache($cacheKey); |
| 784 | - if(!is_null($groupUsers)) { |
|
| 784 | + if (!is_null($groupUsers)) { |
|
| 785 | 785 | return $groupUsers; |
| 786 | 786 | } |
| 787 | 787 | |
| 788 | 788 | // check for cache of the query without limit and offset |
| 789 | 789 | $groupUsers = $this->access->connection->getFromCache('usersInGroup-'.$gid.'-'.$search); |
| 790 | - if(!is_null($groupUsers)) { |
|
| 790 | + if (!is_null($groupUsers)) { |
|
| 791 | 791 | $groupUsers = array_slice($groupUsers, $offset, $limit); |
| 792 | 792 | $this->access->connection->writeToCache($cacheKey, $groupUsers); |
| 793 | 793 | return $groupUsers; |
| 794 | 794 | } |
| 795 | 795 | |
| 796 | - if($limit === -1) { |
|
| 796 | + if ($limit === -1) { |
|
| 797 | 797 | $limit = null; |
| 798 | 798 | } |
| 799 | 799 | $groupDN = $this->access->groupname2dn($gid); |
| 800 | - if(!$groupDN) { |
|
| 800 | + if (!$groupDN) { |
|
| 801 | 801 | // group couldn't be found, return empty resultset |
| 802 | 802 | $this->access->connection->writeToCache($cacheKey, array()); |
| 803 | 803 | return array(); |
@@ -806,7 +806,7 @@ discard block |
||
| 806 | 806 | $primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $search, $limit, $offset); |
| 807 | 807 | $posixGroupUsers = $this->getUsersInGidNumber($groupDN, $search, $limit, $offset); |
| 808 | 808 | $members = array_keys($this->_groupMembers($groupDN)); |
| 809 | - if(!$members && empty($posixGroupUsers) && empty($primaryUsers)) { |
|
| 809 | + if (!$members && empty($posixGroupUsers) && empty($primaryUsers)) { |
|
| 810 | 810 | //in case users could not be retrieved, return empty result set |
| 811 | 811 | $this->access->connection->writeToCache($cacheKey, []); |
| 812 | 812 | return []; |
@@ -815,29 +815,29 @@ discard block |
||
| 815 | 815 | $groupUsers = array(); |
| 816 | 816 | $isMemberUid = (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid'); |
| 817 | 817 | $attrs = $this->access->userManager->getAttributes(true); |
| 818 | - foreach($members as $member) { |
|
| 819 | - if($isMemberUid) { |
|
| 818 | + foreach ($members as $member) { |
|
| 819 | + if ($isMemberUid) { |
|
| 820 | 820 | //we got uids, need to get their DNs to 'translate' them to user names |
| 821 | 821 | $filter = $this->access->combineFilterWithAnd(array( |
| 822 | 822 | str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
| 823 | 823 | $this->access->getFilterPartForUserSearch($search) |
| 824 | 824 | )); |
| 825 | 825 | $ldap_users = $this->access->fetchListOfUsers($filter, $attrs, 1); |
| 826 | - if(count($ldap_users) < 1) { |
|
| 826 | + if (count($ldap_users) < 1) { |
|
| 827 | 827 | continue; |
| 828 | 828 | } |
| 829 | 829 | $groupUsers[] = $this->access->dn2username($ldap_users[0]['dn'][0]); |
| 830 | 830 | } else { |
| 831 | 831 | //we got DNs, check if we need to filter by search or we can give back all of them |
| 832 | 832 | if ($search !== '') { |
| 833 | - if(!$this->access->readAttribute($member, |
|
| 833 | + if (!$this->access->readAttribute($member, |
|
| 834 | 834 | $this->access->connection->ldapUserDisplayName, |
| 835 | 835 | $this->access->getFilterPartForUserSearch($search))) { |
| 836 | 836 | continue; |
| 837 | 837 | } |
| 838 | 838 | } |
| 839 | 839 | // dn2username will also check if the users belong to the allowed base |
| 840 | - if($ocname = $this->access->dn2username($member)) { |
|
| 840 | + if ($ocname = $this->access->dn2username($member)) { |
|
| 841 | 841 | $groupUsers[] = $ocname; |
| 842 | 842 | } |
| 843 | 843 | } |
@@ -861,16 +861,16 @@ discard block |
||
| 861 | 861 | */ |
| 862 | 862 | public function countUsersInGroup($gid, $search = '') { |
| 863 | 863 | $cacheKey = 'countUsersInGroup-'.$gid.'-'.$search; |
| 864 | - if(!$this->enabled || !$this->groupExists($gid)) { |
|
| 864 | + if (!$this->enabled || !$this->groupExists($gid)) { |
|
| 865 | 865 | return false; |
| 866 | 866 | } |
| 867 | 867 | $groupUsers = $this->access->connection->getFromCache($cacheKey); |
| 868 | - if(!is_null($groupUsers)) { |
|
| 868 | + if (!is_null($groupUsers)) { |
|
| 869 | 869 | return $groupUsers; |
| 870 | 870 | } |
| 871 | 871 | |
| 872 | 872 | $groupDN = $this->access->groupname2dn($gid); |
| 873 | - if(!$groupDN) { |
|
| 873 | + if (!$groupDN) { |
|
| 874 | 874 | // group couldn't be found, return empty result set |
| 875 | 875 | $this->access->connection->writeToCache($cacheKey, false); |
| 876 | 876 | return false; |
@@ -878,7 +878,7 @@ discard block |
||
| 878 | 878 | |
| 879 | 879 | $members = array_keys($this->_groupMembers($groupDN)); |
| 880 | 880 | $primaryUserCount = $this->countUsersInPrimaryGroup($groupDN, ''); |
| 881 | - if(!$members && $primaryUserCount === 0) { |
|
| 881 | + if (!$members && $primaryUserCount === 0) { |
|
| 882 | 882 | //in case users could not be retrieved, return empty result set |
| 883 | 883 | $this->access->connection->writeToCache($cacheKey, false); |
| 884 | 884 | return false; |
@@ -903,27 +903,27 @@ discard block |
||
| 903 | 903 | //For now this is not important, because the only use of this method |
| 904 | 904 | //does not supply a search string |
| 905 | 905 | $groupUsers = array(); |
| 906 | - foreach($members as $member) { |
|
| 907 | - if($isMemberUid) { |
|
| 906 | + foreach ($members as $member) { |
|
| 907 | + if ($isMemberUid) { |
|
| 908 | 908 | //we got uids, need to get their DNs to 'translate' them to user names |
| 909 | 909 | $filter = $this->access->combineFilterWithAnd(array( |
| 910 | 910 | str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
| 911 | 911 | $this->access->getFilterPartForUserSearch($search) |
| 912 | 912 | )); |
| 913 | 913 | $ldap_users = $this->access->fetchListOfUsers($filter, 'dn', 1); |
| 914 | - if(count($ldap_users) < 1) { |
|
| 914 | + if (count($ldap_users) < 1) { |
|
| 915 | 915 | continue; |
| 916 | 916 | } |
| 917 | 917 | $groupUsers[] = $this->access->dn2username($ldap_users[0]); |
| 918 | 918 | } else { |
| 919 | 919 | //we need to apply the search filter now |
| 920 | - if(!$this->access->readAttribute($member, |
|
| 920 | + if (!$this->access->readAttribute($member, |
|
| 921 | 921 | $this->access->connection->ldapUserDisplayName, |
| 922 | 922 | $this->access->getFilterPartForUserSearch($search))) { |
| 923 | 923 | continue; |
| 924 | 924 | } |
| 925 | 925 | // dn2username will also check if the users belong to the allowed base |
| 926 | - if($ocname = $this->access->dn2username($member)) { |
|
| 926 | + if ($ocname = $this->access->dn2username($member)) { |
|
| 927 | 927 | $groupUsers[] = $ocname; |
| 928 | 928 | } |
| 929 | 929 | } |
@@ -946,7 +946,7 @@ discard block |
||
| 946 | 946 | * Returns a list with all groups (used by getGroups) |
| 947 | 947 | */ |
| 948 | 948 | protected function getGroupsChunk($search = '', $limit = -1, $offset = 0) { |
| 949 | - if(!$this->enabled) { |
|
| 949 | + if (!$this->enabled) { |
|
| 950 | 950 | return array(); |
| 951 | 951 | } |
| 952 | 952 | $cacheKey = 'getGroups-'.$search.'-'.$limit.'-'.$offset; |
@@ -954,13 +954,13 @@ discard block |
||
| 954 | 954 | //Check cache before driving unnecessary searches |
| 955 | 955 | \OCP\Util::writeLog('user_ldap', 'getGroups '.$cacheKey, \OCP\Util::DEBUG); |
| 956 | 956 | $ldap_groups = $this->access->connection->getFromCache($cacheKey); |
| 957 | - if(!is_null($ldap_groups)) { |
|
| 957 | + if (!is_null($ldap_groups)) { |
|
| 958 | 958 | return $ldap_groups; |
| 959 | 959 | } |
| 960 | 960 | |
| 961 | 961 | // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
| 962 | 962 | // error. With a limit of 0, we get 0 results. So we pass null. |
| 963 | - if($limit <= 0) { |
|
| 963 | + if ($limit <= 0) { |
|
| 964 | 964 | $limit = null; |
| 965 | 965 | } |
| 966 | 966 | $filter = $this->access->combineFilterWithAnd(array( |
@@ -992,7 +992,7 @@ discard block |
||
| 992 | 992 | * (active directory has a limit of 1000 by default) |
| 993 | 993 | */ |
| 994 | 994 | public function getGroups($search = '', $limit = -1, $offset = 0) { |
| 995 | - if(!$this->enabled) { |
|
| 995 | + if (!$this->enabled) { |
|
| 996 | 996 | return array(); |
| 997 | 997 | } |
| 998 | 998 | $search = $this->access->escapeFilterPart($search, true); |
@@ -1039,20 +1039,20 @@ discard block |
||
| 1039 | 1039 | */ |
| 1040 | 1040 | public function groupExists($gid) { |
| 1041 | 1041 | $groupExists = $this->access->connection->getFromCache('groupExists'.$gid); |
| 1042 | - if(!is_null($groupExists)) { |
|
| 1043 | - return (bool)$groupExists; |
|
| 1042 | + if (!is_null($groupExists)) { |
|
| 1043 | + return (bool) $groupExists; |
|
| 1044 | 1044 | } |
| 1045 | 1045 | |
| 1046 | 1046 | //getting dn, if false the group does not exist. If dn, it may be mapped |
| 1047 | 1047 | //only, requires more checking. |
| 1048 | 1048 | $dn = $this->access->groupname2dn($gid); |
| 1049 | - if(!$dn) { |
|
| 1049 | + if (!$dn) { |
|
| 1050 | 1050 | $this->access->connection->writeToCache('groupExists'.$gid, false); |
| 1051 | 1051 | return false; |
| 1052 | 1052 | } |
| 1053 | 1053 | |
| 1054 | 1054 | //if group really still exists, we will be able to read its objectclass |
| 1055 | - if(!is_array($this->access->readAttribute($dn, ''))) { |
|
| 1055 | + if (!is_array($this->access->readAttribute($dn, ''))) { |
|
| 1056 | 1056 | $this->access->connection->writeToCache('groupExists'.$gid, false); |
| 1057 | 1057 | return false; |
| 1058 | 1058 | } |
@@ -1070,7 +1070,7 @@ discard block |
||
| 1070 | 1070 | * compared with \OC\User\Backend::CREATE_USER etc. |
| 1071 | 1071 | */ |
| 1072 | 1072 | public function implementsActions($actions) { |
| 1073 | - return (bool)(\OC\Group\Backend::COUNT_USERS & $actions); |
|
| 1073 | + return (bool) (\OC\Group\Backend::COUNT_USERS & $actions); |
|
| 1074 | 1074 | } |
| 1075 | 1075 | |
| 1076 | 1076 | /** |
@@ -54,1580 +54,1580 @@ discard block |
||
| 54 | 54 | * @package OCA\User_LDAP |
| 55 | 55 | */ |
| 56 | 56 | class Access extends LDAPUtility implements IUserTools { |
| 57 | - /** |
|
| 58 | - * @var \OCA\User_LDAP\Connection |
|
| 59 | - */ |
|
| 60 | - public $connection; |
|
| 61 | - /** @var Manager */ |
|
| 62 | - public $userManager; |
|
| 63 | - //never ever check this var directly, always use getPagedSearchResultState |
|
| 64 | - protected $pagedSearchedSuccessful; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @var string[] $cookies an array of returned Paged Result cookies |
|
| 68 | - */ |
|
| 69 | - protected $cookies = array(); |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @var string $lastCookie the last cookie returned from a Paged Results |
|
| 73 | - * operation, defaults to an empty string |
|
| 74 | - */ |
|
| 75 | - protected $lastCookie = ''; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @var AbstractMapping $userMapper |
|
| 79 | - */ |
|
| 80 | - protected $userMapper; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @var AbstractMapping $userMapper |
|
| 84 | - */ |
|
| 85 | - protected $groupMapper; |
|
| 57 | + /** |
|
| 58 | + * @var \OCA\User_LDAP\Connection |
|
| 59 | + */ |
|
| 60 | + public $connection; |
|
| 61 | + /** @var Manager */ |
|
| 62 | + public $userManager; |
|
| 63 | + //never ever check this var directly, always use getPagedSearchResultState |
|
| 64 | + protected $pagedSearchedSuccessful; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @var string[] $cookies an array of returned Paged Result cookies |
|
| 68 | + */ |
|
| 69 | + protected $cookies = array(); |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @var string $lastCookie the last cookie returned from a Paged Results |
|
| 73 | + * operation, defaults to an empty string |
|
| 74 | + */ |
|
| 75 | + protected $lastCookie = ''; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @var AbstractMapping $userMapper |
|
| 79 | + */ |
|
| 80 | + protected $userMapper; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @var AbstractMapping $userMapper |
|
| 84 | + */ |
|
| 85 | + protected $groupMapper; |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * @var \OCA\User_LDAP\Helper |
|
| 89 | - */ |
|
| 90 | - private $helper; |
|
| 91 | - |
|
| 92 | - public function __construct(Connection $connection, ILDAPWrapper $ldap, |
|
| 93 | - Manager $userManager, Helper $helper) { |
|
| 94 | - parent::__construct($ldap); |
|
| 95 | - $this->connection = $connection; |
|
| 96 | - $this->userManager = $userManager; |
|
| 97 | - $this->userManager->setLdapAccess($this); |
|
| 98 | - $this->helper = $helper; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * sets the User Mapper |
|
| 103 | - * @param AbstractMapping $mapper |
|
| 104 | - */ |
|
| 105 | - public function setUserMapper(AbstractMapping $mapper) { |
|
| 106 | - $this->userMapper = $mapper; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * returns the User Mapper |
|
| 111 | - * @throws \Exception |
|
| 112 | - * @return AbstractMapping |
|
| 113 | - */ |
|
| 114 | - public function getUserMapper() { |
|
| 115 | - if(is_null($this->userMapper)) { |
|
| 116 | - throw new \Exception('UserMapper was not assigned to this Access instance.'); |
|
| 117 | - } |
|
| 118 | - return $this->userMapper; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * sets the Group Mapper |
|
| 123 | - * @param AbstractMapping $mapper |
|
| 124 | - */ |
|
| 125 | - public function setGroupMapper(AbstractMapping $mapper) { |
|
| 126 | - $this->groupMapper = $mapper; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * returns the Group Mapper |
|
| 131 | - * @throws \Exception |
|
| 132 | - * @return AbstractMapping |
|
| 133 | - */ |
|
| 134 | - public function getGroupMapper() { |
|
| 135 | - if(is_null($this->groupMapper)) { |
|
| 136 | - throw new \Exception('GroupMapper was not assigned to this Access instance.'); |
|
| 137 | - } |
|
| 138 | - return $this->groupMapper; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @return bool |
|
| 143 | - */ |
|
| 144 | - private function checkConnection() { |
|
| 145 | - return ($this->connection instanceof Connection); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * returns the Connection instance |
|
| 150 | - * @return \OCA\User_LDAP\Connection |
|
| 151 | - */ |
|
| 152 | - public function getConnection() { |
|
| 153 | - return $this->connection; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * reads a given attribute for an LDAP record identified by a DN |
|
| 158 | - * @param string $dn the record in question |
|
| 159 | - * @param string $attr the attribute that shall be retrieved |
|
| 160 | - * if empty, just check the record's existence |
|
| 161 | - * @param string $filter |
|
| 162 | - * @return array|false an array of values on success or an empty |
|
| 163 | - * array if $attr is empty, false otherwise |
|
| 164 | - */ |
|
| 165 | - public function readAttribute($dn, $attr, $filter = 'objectClass=*') { |
|
| 166 | - if(!$this->checkConnection()) { |
|
| 167 | - \OCP\Util::writeLog('user_ldap', |
|
| 168 | - 'No LDAP Connector assigned, access impossible for readAttribute.', |
|
| 169 | - \OCP\Util::WARN); |
|
| 170 | - return false; |
|
| 171 | - } |
|
| 172 | - $cr = $this->connection->getConnectionResource(); |
|
| 173 | - if(!$this->ldap->isResource($cr)) { |
|
| 174 | - //LDAP not available |
|
| 175 | - \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
|
| 176 | - return false; |
|
| 177 | - } |
|
| 178 | - //Cancel possibly running Paged Results operation, otherwise we run in |
|
| 179 | - //LDAP protocol errors |
|
| 180 | - $this->abandonPagedSearch(); |
|
| 181 | - // openLDAP requires that we init a new Paged Search. Not needed by AD, |
|
| 182 | - // but does not hurt either. |
|
| 183 | - $pagingSize = intval($this->connection->ldapPagingSize); |
|
| 184 | - // 0 won't result in replies, small numbers may leave out groups |
|
| 185 | - // (cf. #12306), 500 is default for paging and should work everywhere. |
|
| 186 | - $maxResults = $pagingSize > 20 ? $pagingSize : 500; |
|
| 187 | - $attr = mb_strtolower($attr, 'UTF-8'); |
|
| 188 | - // the actual read attribute later may contain parameters on a ranged |
|
| 189 | - // request, e.g. member;range=99-199. Depends on server reply. |
|
| 190 | - $attrToRead = $attr; |
|
| 191 | - |
|
| 192 | - $values = []; |
|
| 193 | - $isRangeRequest = false; |
|
| 194 | - do { |
|
| 195 | - $result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults); |
|
| 196 | - if(is_bool($result)) { |
|
| 197 | - // when an exists request was run and it was successful, an empty |
|
| 198 | - // array must be returned |
|
| 199 | - return $result ? [] : false; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - if (!$isRangeRequest) { |
|
| 203 | - $values = $this->extractAttributeValuesFromResult($result, $attr); |
|
| 204 | - if (!empty($values)) { |
|
| 205 | - return $values; |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - $isRangeRequest = false; |
|
| 210 | - $result = $this->extractRangeData($result, $attr); |
|
| 211 | - if (!empty($result)) { |
|
| 212 | - $normalizedResult = $this->extractAttributeValuesFromResult( |
|
| 213 | - [ $attr => $result['values'] ], |
|
| 214 | - $attr |
|
| 215 | - ); |
|
| 216 | - $values = array_merge($values, $normalizedResult); |
|
| 217 | - |
|
| 218 | - if($result['rangeHigh'] === '*') { |
|
| 219 | - // when server replies with * as high range value, there are |
|
| 220 | - // no more results left |
|
| 221 | - return $values; |
|
| 222 | - } else { |
|
| 223 | - $low = $result['rangeHigh'] + 1; |
|
| 224 | - $attrToRead = $result['attributeName'] . ';range=' . $low . '-*'; |
|
| 225 | - $isRangeRequest = true; |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - } while($isRangeRequest); |
|
| 229 | - |
|
| 230 | - \OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, \OCP\Util::DEBUG); |
|
| 231 | - return false; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Runs an read operation against LDAP |
|
| 236 | - * |
|
| 237 | - * @param resource $cr the LDAP connection |
|
| 238 | - * @param string $dn |
|
| 239 | - * @param string $attribute |
|
| 240 | - * @param string $filter |
|
| 241 | - * @param int $maxResults |
|
| 242 | - * @return array|bool false if there was any error, true if an exists check |
|
| 243 | - * was performed and the requested DN found, array with the |
|
| 244 | - * returned data on a successful usual operation |
|
| 245 | - */ |
|
| 246 | - public function executeRead($cr, $dn, $attribute, $filter, $maxResults) { |
|
| 247 | - $this->initPagedSearch($filter, array($dn), array($attribute), $maxResults, 0); |
|
| 248 | - $dn = $this->helper->DNasBaseParameter($dn); |
|
| 249 | - $rr = @$this->invokeLDAPMethod('read', $cr, $dn, $filter, array($attribute)); |
|
| 250 | - if (!$this->ldap->isResource($rr)) { |
|
| 251 | - if ($attribute !== '') { |
|
| 252 | - //do not throw this message on userExists check, irritates |
|
| 253 | - \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, \OCP\Util::DEBUG); |
|
| 254 | - } |
|
| 255 | - //in case an error occurs , e.g. object does not exist |
|
| 256 | - return false; |
|
| 257 | - } |
|
| 258 | - if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) { |
|
| 259 | - \OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', \OCP\Util::DEBUG); |
|
| 260 | - return true; |
|
| 261 | - } |
|
| 262 | - $er = $this->invokeLDAPMethod('firstEntry', $cr, $rr); |
|
| 263 | - if (!$this->ldap->isResource($er)) { |
|
| 264 | - //did not match the filter, return false |
|
| 265 | - return false; |
|
| 266 | - } |
|
| 267 | - //LDAP attributes are not case sensitive |
|
| 268 | - $result = \OCP\Util::mb_array_change_key_case( |
|
| 269 | - $this->invokeLDAPMethod('getAttributes', $cr, $er), MB_CASE_LOWER, 'UTF-8'); |
|
| 270 | - |
|
| 271 | - return $result; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Normalizes a result grom getAttributes(), i.e. handles DNs and binary |
|
| 276 | - * data if present. |
|
| 277 | - * |
|
| 278 | - * @param array $result from ILDAPWrapper::getAttributes() |
|
| 279 | - * @param string $attribute the attribute name that was read |
|
| 280 | - * @return string[] |
|
| 281 | - */ |
|
| 282 | - public function extractAttributeValuesFromResult($result, $attribute) { |
|
| 283 | - $values = []; |
|
| 284 | - if(isset($result[$attribute]) && $result[$attribute]['count'] > 0) { |
|
| 285 | - $lowercaseAttribute = strtolower($attribute); |
|
| 286 | - for($i=0;$i<$result[$attribute]['count'];$i++) { |
|
| 287 | - if($this->resemblesDN($attribute)) { |
|
| 288 | - $values[] = $this->helper->sanitizeDN($result[$attribute][$i]); |
|
| 289 | - } elseif($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { |
|
| 290 | - $values[] = $this->convertObjectGUID2Str($result[$attribute][$i]); |
|
| 291 | - } else { |
|
| 292 | - $values[] = $result[$attribute][$i]; |
|
| 293 | - } |
|
| 294 | - } |
|
| 295 | - } |
|
| 296 | - return $values; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Attempts to find ranged data in a getAttribute results and extracts the |
|
| 301 | - * returned values as well as information on the range and full attribute |
|
| 302 | - * name for further processing. |
|
| 303 | - * |
|
| 304 | - * @param array $result from ILDAPWrapper::getAttributes() |
|
| 305 | - * @param string $attribute the attribute name that was read. Without ";range=…" |
|
| 306 | - * @return array If a range was detected with keys 'values', 'attributeName', |
|
| 307 | - * 'attributeFull' and 'rangeHigh', otherwise empty. |
|
| 308 | - */ |
|
| 309 | - public function extractRangeData($result, $attribute) { |
|
| 310 | - $keys = array_keys($result); |
|
| 311 | - foreach($keys as $key) { |
|
| 312 | - if($key !== $attribute && strpos($key, $attribute) === 0) { |
|
| 313 | - $queryData = explode(';', $key); |
|
| 314 | - if(strpos($queryData[1], 'range=') === 0) { |
|
| 315 | - $high = substr($queryData[1], 1 + strpos($queryData[1], '-')); |
|
| 316 | - $data = [ |
|
| 317 | - 'values' => $result[$key], |
|
| 318 | - 'attributeName' => $queryData[0], |
|
| 319 | - 'attributeFull' => $key, |
|
| 320 | - 'rangeHigh' => $high, |
|
| 321 | - ]; |
|
| 322 | - return $data; |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - return []; |
|
| 327 | - } |
|
| 87 | + /** |
|
| 88 | + * @var \OCA\User_LDAP\Helper |
|
| 89 | + */ |
|
| 90 | + private $helper; |
|
| 91 | + |
|
| 92 | + public function __construct(Connection $connection, ILDAPWrapper $ldap, |
|
| 93 | + Manager $userManager, Helper $helper) { |
|
| 94 | + parent::__construct($ldap); |
|
| 95 | + $this->connection = $connection; |
|
| 96 | + $this->userManager = $userManager; |
|
| 97 | + $this->userManager->setLdapAccess($this); |
|
| 98 | + $this->helper = $helper; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * sets the User Mapper |
|
| 103 | + * @param AbstractMapping $mapper |
|
| 104 | + */ |
|
| 105 | + public function setUserMapper(AbstractMapping $mapper) { |
|
| 106 | + $this->userMapper = $mapper; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * returns the User Mapper |
|
| 111 | + * @throws \Exception |
|
| 112 | + * @return AbstractMapping |
|
| 113 | + */ |
|
| 114 | + public function getUserMapper() { |
|
| 115 | + if(is_null($this->userMapper)) { |
|
| 116 | + throw new \Exception('UserMapper was not assigned to this Access instance.'); |
|
| 117 | + } |
|
| 118 | + return $this->userMapper; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * sets the Group Mapper |
|
| 123 | + * @param AbstractMapping $mapper |
|
| 124 | + */ |
|
| 125 | + public function setGroupMapper(AbstractMapping $mapper) { |
|
| 126 | + $this->groupMapper = $mapper; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * returns the Group Mapper |
|
| 131 | + * @throws \Exception |
|
| 132 | + * @return AbstractMapping |
|
| 133 | + */ |
|
| 134 | + public function getGroupMapper() { |
|
| 135 | + if(is_null($this->groupMapper)) { |
|
| 136 | + throw new \Exception('GroupMapper was not assigned to this Access instance.'); |
|
| 137 | + } |
|
| 138 | + return $this->groupMapper; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @return bool |
|
| 143 | + */ |
|
| 144 | + private function checkConnection() { |
|
| 145 | + return ($this->connection instanceof Connection); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * returns the Connection instance |
|
| 150 | + * @return \OCA\User_LDAP\Connection |
|
| 151 | + */ |
|
| 152 | + public function getConnection() { |
|
| 153 | + return $this->connection; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * reads a given attribute for an LDAP record identified by a DN |
|
| 158 | + * @param string $dn the record in question |
|
| 159 | + * @param string $attr the attribute that shall be retrieved |
|
| 160 | + * if empty, just check the record's existence |
|
| 161 | + * @param string $filter |
|
| 162 | + * @return array|false an array of values on success or an empty |
|
| 163 | + * array if $attr is empty, false otherwise |
|
| 164 | + */ |
|
| 165 | + public function readAttribute($dn, $attr, $filter = 'objectClass=*') { |
|
| 166 | + if(!$this->checkConnection()) { |
|
| 167 | + \OCP\Util::writeLog('user_ldap', |
|
| 168 | + 'No LDAP Connector assigned, access impossible for readAttribute.', |
|
| 169 | + \OCP\Util::WARN); |
|
| 170 | + return false; |
|
| 171 | + } |
|
| 172 | + $cr = $this->connection->getConnectionResource(); |
|
| 173 | + if(!$this->ldap->isResource($cr)) { |
|
| 174 | + //LDAP not available |
|
| 175 | + \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
|
| 176 | + return false; |
|
| 177 | + } |
|
| 178 | + //Cancel possibly running Paged Results operation, otherwise we run in |
|
| 179 | + //LDAP protocol errors |
|
| 180 | + $this->abandonPagedSearch(); |
|
| 181 | + // openLDAP requires that we init a new Paged Search. Not needed by AD, |
|
| 182 | + // but does not hurt either. |
|
| 183 | + $pagingSize = intval($this->connection->ldapPagingSize); |
|
| 184 | + // 0 won't result in replies, small numbers may leave out groups |
|
| 185 | + // (cf. #12306), 500 is default for paging and should work everywhere. |
|
| 186 | + $maxResults = $pagingSize > 20 ? $pagingSize : 500; |
|
| 187 | + $attr = mb_strtolower($attr, 'UTF-8'); |
|
| 188 | + // the actual read attribute later may contain parameters on a ranged |
|
| 189 | + // request, e.g. member;range=99-199. Depends on server reply. |
|
| 190 | + $attrToRead = $attr; |
|
| 191 | + |
|
| 192 | + $values = []; |
|
| 193 | + $isRangeRequest = false; |
|
| 194 | + do { |
|
| 195 | + $result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults); |
|
| 196 | + if(is_bool($result)) { |
|
| 197 | + // when an exists request was run and it was successful, an empty |
|
| 198 | + // array must be returned |
|
| 199 | + return $result ? [] : false; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + if (!$isRangeRequest) { |
|
| 203 | + $values = $this->extractAttributeValuesFromResult($result, $attr); |
|
| 204 | + if (!empty($values)) { |
|
| 205 | + return $values; |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + $isRangeRequest = false; |
|
| 210 | + $result = $this->extractRangeData($result, $attr); |
|
| 211 | + if (!empty($result)) { |
|
| 212 | + $normalizedResult = $this->extractAttributeValuesFromResult( |
|
| 213 | + [ $attr => $result['values'] ], |
|
| 214 | + $attr |
|
| 215 | + ); |
|
| 216 | + $values = array_merge($values, $normalizedResult); |
|
| 217 | + |
|
| 218 | + if($result['rangeHigh'] === '*') { |
|
| 219 | + // when server replies with * as high range value, there are |
|
| 220 | + // no more results left |
|
| 221 | + return $values; |
|
| 222 | + } else { |
|
| 223 | + $low = $result['rangeHigh'] + 1; |
|
| 224 | + $attrToRead = $result['attributeName'] . ';range=' . $low . '-*'; |
|
| 225 | + $isRangeRequest = true; |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + } while($isRangeRequest); |
|
| 229 | + |
|
| 230 | + \OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, \OCP\Util::DEBUG); |
|
| 231 | + return false; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Runs an read operation against LDAP |
|
| 236 | + * |
|
| 237 | + * @param resource $cr the LDAP connection |
|
| 238 | + * @param string $dn |
|
| 239 | + * @param string $attribute |
|
| 240 | + * @param string $filter |
|
| 241 | + * @param int $maxResults |
|
| 242 | + * @return array|bool false if there was any error, true if an exists check |
|
| 243 | + * was performed and the requested DN found, array with the |
|
| 244 | + * returned data on a successful usual operation |
|
| 245 | + */ |
|
| 246 | + public function executeRead($cr, $dn, $attribute, $filter, $maxResults) { |
|
| 247 | + $this->initPagedSearch($filter, array($dn), array($attribute), $maxResults, 0); |
|
| 248 | + $dn = $this->helper->DNasBaseParameter($dn); |
|
| 249 | + $rr = @$this->invokeLDAPMethod('read', $cr, $dn, $filter, array($attribute)); |
|
| 250 | + if (!$this->ldap->isResource($rr)) { |
|
| 251 | + if ($attribute !== '') { |
|
| 252 | + //do not throw this message on userExists check, irritates |
|
| 253 | + \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, \OCP\Util::DEBUG); |
|
| 254 | + } |
|
| 255 | + //in case an error occurs , e.g. object does not exist |
|
| 256 | + return false; |
|
| 257 | + } |
|
| 258 | + if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) { |
|
| 259 | + \OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', \OCP\Util::DEBUG); |
|
| 260 | + return true; |
|
| 261 | + } |
|
| 262 | + $er = $this->invokeLDAPMethod('firstEntry', $cr, $rr); |
|
| 263 | + if (!$this->ldap->isResource($er)) { |
|
| 264 | + //did not match the filter, return false |
|
| 265 | + return false; |
|
| 266 | + } |
|
| 267 | + //LDAP attributes are not case sensitive |
|
| 268 | + $result = \OCP\Util::mb_array_change_key_case( |
|
| 269 | + $this->invokeLDAPMethod('getAttributes', $cr, $er), MB_CASE_LOWER, 'UTF-8'); |
|
| 270 | + |
|
| 271 | + return $result; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Normalizes a result grom getAttributes(), i.e. handles DNs and binary |
|
| 276 | + * data if present. |
|
| 277 | + * |
|
| 278 | + * @param array $result from ILDAPWrapper::getAttributes() |
|
| 279 | + * @param string $attribute the attribute name that was read |
|
| 280 | + * @return string[] |
|
| 281 | + */ |
|
| 282 | + public function extractAttributeValuesFromResult($result, $attribute) { |
|
| 283 | + $values = []; |
|
| 284 | + if(isset($result[$attribute]) && $result[$attribute]['count'] > 0) { |
|
| 285 | + $lowercaseAttribute = strtolower($attribute); |
|
| 286 | + for($i=0;$i<$result[$attribute]['count'];$i++) { |
|
| 287 | + if($this->resemblesDN($attribute)) { |
|
| 288 | + $values[] = $this->helper->sanitizeDN($result[$attribute][$i]); |
|
| 289 | + } elseif($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { |
|
| 290 | + $values[] = $this->convertObjectGUID2Str($result[$attribute][$i]); |
|
| 291 | + } else { |
|
| 292 | + $values[] = $result[$attribute][$i]; |
|
| 293 | + } |
|
| 294 | + } |
|
| 295 | + } |
|
| 296 | + return $values; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Attempts to find ranged data in a getAttribute results and extracts the |
|
| 301 | + * returned values as well as information on the range and full attribute |
|
| 302 | + * name for further processing. |
|
| 303 | + * |
|
| 304 | + * @param array $result from ILDAPWrapper::getAttributes() |
|
| 305 | + * @param string $attribute the attribute name that was read. Without ";range=…" |
|
| 306 | + * @return array If a range was detected with keys 'values', 'attributeName', |
|
| 307 | + * 'attributeFull' and 'rangeHigh', otherwise empty. |
|
| 308 | + */ |
|
| 309 | + public function extractRangeData($result, $attribute) { |
|
| 310 | + $keys = array_keys($result); |
|
| 311 | + foreach($keys as $key) { |
|
| 312 | + if($key !== $attribute && strpos($key, $attribute) === 0) { |
|
| 313 | + $queryData = explode(';', $key); |
|
| 314 | + if(strpos($queryData[1], 'range=') === 0) { |
|
| 315 | + $high = substr($queryData[1], 1 + strpos($queryData[1], '-')); |
|
| 316 | + $data = [ |
|
| 317 | + 'values' => $result[$key], |
|
| 318 | + 'attributeName' => $queryData[0], |
|
| 319 | + 'attributeFull' => $key, |
|
| 320 | + 'rangeHigh' => $high, |
|
| 321 | + ]; |
|
| 322 | + return $data; |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + return []; |
|
| 327 | + } |
|
| 328 | 328 | |
| 329 | - /** |
|
| 330 | - * Set password for an LDAP user identified by a DN |
|
| 331 | - * |
|
| 332 | - * @param string $userDN the user in question |
|
| 333 | - * @param string $password the new password |
|
| 334 | - * @return bool |
|
| 335 | - * @throws HintException |
|
| 336 | - * @throws \Exception |
|
| 337 | - */ |
|
| 338 | - public function setPassword($userDN, $password) { |
|
| 339 | - if(intval($this->connection->turnOnPasswordChange) !== 1) { |
|
| 340 | - throw new \Exception('LDAP password changes are disabled.'); |
|
| 341 | - } |
|
| 342 | - $cr = $this->connection->getConnectionResource(); |
|
| 343 | - if(!$this->ldap->isResource($cr)) { |
|
| 344 | - //LDAP not available |
|
| 345 | - \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
|
| 346 | - return false; |
|
| 347 | - } |
|
| 348 | - try { |
|
| 349 | - return @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password); |
|
| 350 | - } catch(ConstraintViolationException $e) { |
|
| 351 | - throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ').$e->getMessage(), $e->getCode()); |
|
| 352 | - } |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * checks whether the given attributes value is probably a DN |
|
| 357 | - * @param string $attr the attribute in question |
|
| 358 | - * @return boolean if so true, otherwise false |
|
| 359 | - */ |
|
| 360 | - private function resemblesDN($attr) { |
|
| 361 | - $resemblingAttributes = array( |
|
| 362 | - 'dn', |
|
| 363 | - 'uniquemember', |
|
| 364 | - 'member', |
|
| 365 | - // memberOf is an "operational" attribute, without a definition in any RFC |
|
| 366 | - 'memberof' |
|
| 367 | - ); |
|
| 368 | - return in_array($attr, $resemblingAttributes); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * checks whether the given string is probably a DN |
|
| 373 | - * @param string $string |
|
| 374 | - * @return boolean |
|
| 375 | - */ |
|
| 376 | - public function stringResemblesDN($string) { |
|
| 377 | - $r = $this->ldap->explodeDN($string, 0); |
|
| 378 | - // if exploding a DN succeeds and does not end up in |
|
| 379 | - // an empty array except for $r[count] being 0. |
|
| 380 | - return (is_array($r) && count($r) > 1); |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * returns a DN-string that is cleaned from not domain parts, e.g. |
|
| 385 | - * cn=foo,cn=bar,dc=foobar,dc=server,dc=org |
|
| 386 | - * becomes dc=foobar,dc=server,dc=org |
|
| 387 | - * @param string $dn |
|
| 388 | - * @return string |
|
| 389 | - */ |
|
| 390 | - public function getDomainDNFromDN($dn) { |
|
| 391 | - $allParts = $this->ldap->explodeDN($dn, 0); |
|
| 392 | - if($allParts === false) { |
|
| 393 | - //not a valid DN |
|
| 394 | - return ''; |
|
| 395 | - } |
|
| 396 | - $domainParts = array(); |
|
| 397 | - $dcFound = false; |
|
| 398 | - foreach($allParts as $part) { |
|
| 399 | - if(!$dcFound && strpos($part, 'dc=') === 0) { |
|
| 400 | - $dcFound = true; |
|
| 401 | - } |
|
| 402 | - if($dcFound) { |
|
| 403 | - $domainParts[] = $part; |
|
| 404 | - } |
|
| 405 | - } |
|
| 406 | - $domainDN = implode(',', $domainParts); |
|
| 407 | - return $domainDN; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * returns the LDAP DN for the given internal Nextcloud name of the group |
|
| 412 | - * @param string $name the Nextcloud name in question |
|
| 413 | - * @return string|false LDAP DN on success, otherwise false |
|
| 414 | - */ |
|
| 415 | - public function groupname2dn($name) { |
|
| 416 | - return $this->groupMapper->getDNByName($name); |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * returns the LDAP DN for the given internal Nextcloud name of the user |
|
| 421 | - * @param string $name the Nextcloud name in question |
|
| 422 | - * @return string|false with the LDAP DN on success, otherwise false |
|
| 423 | - */ |
|
| 424 | - public function username2dn($name) { |
|
| 425 | - $fdn = $this->userMapper->getDNByName($name); |
|
| 426 | - |
|
| 427 | - //Check whether the DN belongs to the Base, to avoid issues on multi- |
|
| 428 | - //server setups |
|
| 429 | - if(is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 430 | - return $fdn; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - return false; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * returns the internal Nextcloud name for the given LDAP DN of the group, false on DN outside of search DN or failure |
|
| 438 | - * @param string $fdn the dn of the group object |
|
| 439 | - * @param string $ldapName optional, the display name of the object |
|
| 440 | - * @return string|false with the name to use in Nextcloud, false on DN outside of search DN |
|
| 441 | - */ |
|
| 442 | - public function dn2groupname($fdn, $ldapName = null) { |
|
| 443 | - //To avoid bypassing the base DN settings under certain circumstances |
|
| 444 | - //with the group support, check whether the provided DN matches one of |
|
| 445 | - //the given Bases |
|
| 446 | - if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { |
|
| 447 | - return false; |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - return $this->dn2ocname($fdn, $ldapName, false); |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * accepts an array of group DNs and tests whether they match the user |
|
| 455 | - * filter by doing read operations against the group entries. Returns an |
|
| 456 | - * array of DNs that match the filter. |
|
| 457 | - * |
|
| 458 | - * @param string[] $groupDNs |
|
| 459 | - * @return string[] |
|
| 460 | - */ |
|
| 461 | - public function groupsMatchFilter($groupDNs) { |
|
| 462 | - $validGroupDNs = []; |
|
| 463 | - foreach($groupDNs as $dn) { |
|
| 464 | - $cacheKey = 'groupsMatchFilter-'.$dn; |
|
| 465 | - $groupMatchFilter = $this->connection->getFromCache($cacheKey); |
|
| 466 | - if(!is_null($groupMatchFilter)) { |
|
| 467 | - if($groupMatchFilter) { |
|
| 468 | - $validGroupDNs[] = $dn; |
|
| 469 | - } |
|
| 470 | - continue; |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - // Check the base DN first. If this is not met already, we don't |
|
| 474 | - // need to ask the server at all. |
|
| 475 | - if(!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { |
|
| 476 | - $this->connection->writeToCache($cacheKey, false); |
|
| 477 | - continue; |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - $result = $this->readAttribute($dn, 'cn', $this->connection->ldapGroupFilter); |
|
| 481 | - if(is_array($result)) { |
|
| 482 | - $this->connection->writeToCache($cacheKey, true); |
|
| 483 | - $validGroupDNs[] = $dn; |
|
| 484 | - } else { |
|
| 485 | - $this->connection->writeToCache($cacheKey, false); |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - } |
|
| 489 | - return $validGroupDNs; |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * returns the internal Nextcloud name for the given LDAP DN of the user, false on DN outside of search DN or failure |
|
| 494 | - * @param string $dn the dn of the user object |
|
| 495 | - * @param string $ldapName optional, the display name of the object |
|
| 496 | - * @return string|false with with the name to use in Nextcloud |
|
| 497 | - */ |
|
| 498 | - public function dn2username($fdn, $ldapName = null) { |
|
| 499 | - //To avoid bypassing the base DN settings under certain circumstances |
|
| 500 | - //with the group support, check whether the provided DN matches one of |
|
| 501 | - //the given Bases |
|
| 502 | - if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 503 | - return false; |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - return $this->dn2ocname($fdn, $ldapName, true); |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - /** |
|
| 510 | - * returns an internal Nextcloud name for the given LDAP DN, false on DN outside of search DN |
|
| 511 | - * @param string $dn the dn of the user object |
|
| 512 | - * @param string $ldapName optional, the display name of the object |
|
| 513 | - * @param bool $isUser optional, whether it is a user object (otherwise group assumed) |
|
| 514 | - * @return string|false with with the name to use in Nextcloud |
|
| 515 | - */ |
|
| 516 | - public function dn2ocname($fdn, $ldapName = null, $isUser = true) { |
|
| 517 | - if($isUser) { |
|
| 518 | - $mapper = $this->getUserMapper(); |
|
| 519 | - $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 520 | - } else { |
|
| 521 | - $mapper = $this->getGroupMapper(); |
|
| 522 | - $nameAttribute = $this->connection->ldapGroupDisplayName; |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - //let's try to retrieve the Nextcloud name from the mappings table |
|
| 526 | - $ocName = $mapper->getNameByDN($fdn); |
|
| 527 | - if(is_string($ocName)) { |
|
| 528 | - return $ocName; |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - //second try: get the UUID and check if it is known. Then, update the DN and return the name. |
|
| 532 | - $uuid = $this->getUUID($fdn, $isUser); |
|
| 533 | - if(is_string($uuid)) { |
|
| 534 | - $ocName = $mapper->getNameByUUID($uuid); |
|
| 535 | - if(is_string($ocName)) { |
|
| 536 | - $mapper->setDNbyUUID($fdn, $uuid); |
|
| 537 | - return $ocName; |
|
| 538 | - } |
|
| 539 | - } else { |
|
| 540 | - //If the UUID can't be detected something is foul. |
|
| 541 | - \OCP\Util::writeLog('user_ldap', 'Cannot determine UUID for '.$fdn.'. Skipping.', \OCP\Util::INFO); |
|
| 542 | - return false; |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - if(is_null($ldapName)) { |
|
| 546 | - $ldapName = $this->readAttribute($fdn, $nameAttribute); |
|
| 547 | - if(!isset($ldapName[0]) && empty($ldapName[0])) { |
|
| 548 | - \OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.'.', \OCP\Util::INFO); |
|
| 549 | - return false; |
|
| 550 | - } |
|
| 551 | - $ldapName = $ldapName[0]; |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - if($isUser) { |
|
| 555 | - $usernameAttribute = strval($this->connection->ldapExpertUsernameAttr); |
|
| 556 | - if ($usernameAttribute !== '') { |
|
| 557 | - $username = $this->readAttribute($fdn, $usernameAttribute); |
|
| 558 | - $username = $username[0]; |
|
| 559 | - } else { |
|
| 560 | - $username = $uuid; |
|
| 561 | - } |
|
| 562 | - $intName = $this->sanitizeUsername($username); |
|
| 563 | - } else { |
|
| 564 | - $intName = $ldapName; |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - //a new user/group! Add it only if it doesn't conflict with other backend's users or existing groups |
|
| 568 | - //disabling Cache is required to avoid that the new user is cached as not-existing in fooExists check |
|
| 569 | - //NOTE: mind, disabling cache affects only this instance! Using it |
|
| 570 | - // outside of core user management will still cache the user as non-existing. |
|
| 571 | - $originalTTL = $this->connection->ldapCacheTTL; |
|
| 572 | - $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
|
| 573 | - if(($isUser && !\OCP\User::userExists($intName)) |
|
| 574 | - || (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))) { |
|
| 575 | - if($mapper->map($fdn, $intName, $uuid)) { |
|
| 576 | - $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 577 | - return $intName; |
|
| 578 | - } |
|
| 579 | - } |
|
| 580 | - $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 581 | - |
|
| 582 | - $altName = $this->createAltInternalOwnCloudName($intName, $isUser); |
|
| 583 | - if(is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { |
|
| 584 | - return $altName; |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - //if everything else did not help.. |
|
| 588 | - \OCP\Util::writeLog('user_ldap', 'Could not create unique name for '.$fdn.'.', \OCP\Util::INFO); |
|
| 589 | - return false; |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - /** |
|
| 593 | - * gives back the user names as they are used ownClod internally |
|
| 594 | - * @param array $ldapUsers as returned by fetchList() |
|
| 595 | - * @return array an array with the user names to use in Nextcloud |
|
| 596 | - * |
|
| 597 | - * gives back the user names as they are used ownClod internally |
|
| 598 | - */ |
|
| 599 | - public function nextcloudUserNames($ldapUsers) { |
|
| 600 | - return $this->ldap2NextcloudNames($ldapUsers, true); |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - /** |
|
| 604 | - * gives back the group names as they are used ownClod internally |
|
| 605 | - * @param array $ldapGroups as returned by fetchList() |
|
| 606 | - * @return array an array with the group names to use in Nextcloud |
|
| 607 | - * |
|
| 608 | - * gives back the group names as they are used ownClod internally |
|
| 609 | - */ |
|
| 610 | - public function nextcloudGroupNames($ldapGroups) { |
|
| 611 | - return $this->ldap2NextcloudNames($ldapGroups, false); |
|
| 612 | - } |
|
| 613 | - |
|
| 614 | - /** |
|
| 615 | - * @param array $ldapObjects as returned by fetchList() |
|
| 616 | - * @param bool $isUsers |
|
| 617 | - * @return array |
|
| 618 | - */ |
|
| 619 | - private function ldap2NextcloudNames($ldapObjects, $isUsers) { |
|
| 620 | - if($isUsers) { |
|
| 621 | - $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 622 | - $sndAttribute = $this->connection->ldapUserDisplayName2; |
|
| 623 | - } else { |
|
| 624 | - $nameAttribute = $this->connection->ldapGroupDisplayName; |
|
| 625 | - } |
|
| 626 | - $nextcloudNames = array(); |
|
| 627 | - |
|
| 628 | - foreach($ldapObjects as $ldapObject) { |
|
| 629 | - $nameByLDAP = null; |
|
| 630 | - if( isset($ldapObject[$nameAttribute]) |
|
| 631 | - && is_array($ldapObject[$nameAttribute]) |
|
| 632 | - && isset($ldapObject[$nameAttribute][0]) |
|
| 633 | - ) { |
|
| 634 | - // might be set, but not necessarily. if so, we use it. |
|
| 635 | - $nameByLDAP = $ldapObject[$nameAttribute][0]; |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - $ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers); |
|
| 639 | - if($ncName) { |
|
| 640 | - $nextcloudNames[] = $ncName; |
|
| 641 | - if($isUsers) { |
|
| 642 | - //cache the user names so it does not need to be retrieved |
|
| 643 | - //again later (e.g. sharing dialogue). |
|
| 644 | - if(is_null($nameByLDAP)) { |
|
| 645 | - continue; |
|
| 646 | - } |
|
| 647 | - $sndName = isset($ldapObject[$sndAttribute][0]) |
|
| 648 | - ? $ldapObject[$sndAttribute][0] : ''; |
|
| 649 | - $this->cacheUserDisplayName($ncName, $nameByLDAP, $sndName); |
|
| 650 | - } |
|
| 651 | - } |
|
| 652 | - } |
|
| 653 | - return $nextcloudNames; |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - /** |
|
| 657 | - * caches the user display name |
|
| 658 | - * @param string $ocName the internal Nextcloud username |
|
| 659 | - * @param string|false $home the home directory path |
|
| 660 | - */ |
|
| 661 | - public function cacheUserHome($ocName, $home) { |
|
| 662 | - $cacheKey = 'getHome'.$ocName; |
|
| 663 | - $this->connection->writeToCache($cacheKey, $home); |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - /** |
|
| 667 | - * caches a user as existing |
|
| 668 | - * @param string $ocName the internal Nextcloud username |
|
| 669 | - */ |
|
| 670 | - public function cacheUserExists($ocName) { |
|
| 671 | - $this->connection->writeToCache('userExists'.$ocName, true); |
|
| 672 | - } |
|
| 673 | - |
|
| 674 | - /** |
|
| 675 | - * caches the user display name |
|
| 676 | - * @param string $ocName the internal Nextcloud username |
|
| 677 | - * @param string $displayName the display name |
|
| 678 | - * @param string $displayName2 the second display name |
|
| 679 | - */ |
|
| 680 | - public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { |
|
| 681 | - $user = $this->userManager->get($ocName); |
|
| 682 | - if($user === null) { |
|
| 683 | - return; |
|
| 684 | - } |
|
| 685 | - $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
| 686 | - $cacheKeyTrunk = 'getDisplayName'; |
|
| 687 | - $this->connection->writeToCache($cacheKeyTrunk.$ocName, $displayName); |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - /** |
|
| 691 | - * creates a unique name for internal Nextcloud use for users. Don't call it directly. |
|
| 692 | - * @param string $name the display name of the object |
|
| 693 | - * @return string|false with with the name to use in Nextcloud or false if unsuccessful |
|
| 694 | - * |
|
| 695 | - * Instead of using this method directly, call |
|
| 696 | - * createAltInternalOwnCloudName($name, true) |
|
| 697 | - */ |
|
| 698 | - private function _createAltInternalOwnCloudNameForUsers($name) { |
|
| 699 | - $attempts = 0; |
|
| 700 | - //while loop is just a precaution. If a name is not generated within |
|
| 701 | - //20 attempts, something else is very wrong. Avoids infinite loop. |
|
| 702 | - while($attempts < 20){ |
|
| 703 | - $altName = $name . '_' . rand(1000,9999); |
|
| 704 | - if(!\OCP\User::userExists($altName)) { |
|
| 705 | - return $altName; |
|
| 706 | - } |
|
| 707 | - $attempts++; |
|
| 708 | - } |
|
| 709 | - return false; |
|
| 710 | - } |
|
| 711 | - |
|
| 712 | - /** |
|
| 713 | - * creates a unique name for internal Nextcloud use for groups. Don't call it directly. |
|
| 714 | - * @param string $name the display name of the object |
|
| 715 | - * @return string|false with with the name to use in Nextcloud or false if unsuccessful. |
|
| 716 | - * |
|
| 717 | - * Instead of using this method directly, call |
|
| 718 | - * createAltInternalOwnCloudName($name, false) |
|
| 719 | - * |
|
| 720 | - * Group names are also used as display names, so we do a sequential |
|
| 721 | - * numbering, e.g. Developers_42 when there are 41 other groups called |
|
| 722 | - * "Developers" |
|
| 723 | - */ |
|
| 724 | - private function _createAltInternalOwnCloudNameForGroups($name) { |
|
| 725 | - $usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%'); |
|
| 726 | - if(!($usedNames) || count($usedNames) === 0) { |
|
| 727 | - $lastNo = 1; //will become name_2 |
|
| 728 | - } else { |
|
| 729 | - natsort($usedNames); |
|
| 730 | - $lastName = array_pop($usedNames); |
|
| 731 | - $lastNo = intval(substr($lastName, strrpos($lastName, '_') + 1)); |
|
| 732 | - } |
|
| 733 | - $altName = $name.'_'.strval($lastNo+1); |
|
| 734 | - unset($usedNames); |
|
| 735 | - |
|
| 736 | - $attempts = 1; |
|
| 737 | - while($attempts < 21){ |
|
| 738 | - // Check to be really sure it is unique |
|
| 739 | - // while loop is just a precaution. If a name is not generated within |
|
| 740 | - // 20 attempts, something else is very wrong. Avoids infinite loop. |
|
| 741 | - if(!\OC::$server->getGroupManager()->groupExists($altName)) { |
|
| 742 | - return $altName; |
|
| 743 | - } |
|
| 744 | - $altName = $name . '_' . ($lastNo + $attempts); |
|
| 745 | - $attempts++; |
|
| 746 | - } |
|
| 747 | - return false; |
|
| 748 | - } |
|
| 749 | - |
|
| 750 | - /** |
|
| 751 | - * creates a unique name for internal Nextcloud use. |
|
| 752 | - * @param string $name the display name of the object |
|
| 753 | - * @param boolean $isUser whether name should be created for a user (true) or a group (false) |
|
| 754 | - * @return string|false with with the name to use in Nextcloud or false if unsuccessful |
|
| 755 | - */ |
|
| 756 | - private function createAltInternalOwnCloudName($name, $isUser) { |
|
| 757 | - $originalTTL = $this->connection->ldapCacheTTL; |
|
| 758 | - $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
|
| 759 | - if($isUser) { |
|
| 760 | - $altName = $this->_createAltInternalOwnCloudNameForUsers($name); |
|
| 761 | - } else { |
|
| 762 | - $altName = $this->_createAltInternalOwnCloudNameForGroups($name); |
|
| 763 | - } |
|
| 764 | - $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 765 | - |
|
| 766 | - return $altName; |
|
| 767 | - } |
|
| 768 | - |
|
| 769 | - /** |
|
| 770 | - * fetches a list of users according to a provided loginName and utilizing |
|
| 771 | - * the login filter. |
|
| 772 | - * |
|
| 773 | - * @param string $loginName |
|
| 774 | - * @param array $attributes optional, list of attributes to read |
|
| 775 | - * @return array |
|
| 776 | - */ |
|
| 777 | - public function fetchUsersByLoginName($loginName, $attributes = array('dn')) { |
|
| 778 | - $loginName = $this->escapeFilterPart($loginName); |
|
| 779 | - $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); |
|
| 780 | - $users = $this->fetchListOfUsers($filter, $attributes); |
|
| 781 | - return $users; |
|
| 782 | - } |
|
| 783 | - |
|
| 784 | - /** |
|
| 785 | - * counts the number of users according to a provided loginName and |
|
| 786 | - * utilizing the login filter. |
|
| 787 | - * |
|
| 788 | - * @param string $loginName |
|
| 789 | - * @return array |
|
| 790 | - */ |
|
| 791 | - public function countUsersByLoginName($loginName) { |
|
| 792 | - $loginName = $this->escapeFilterPart($loginName); |
|
| 793 | - $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); |
|
| 794 | - $users = $this->countUsers($filter); |
|
| 795 | - return $users; |
|
| 796 | - } |
|
| 797 | - |
|
| 798 | - /** |
|
| 799 | - * @param string $filter |
|
| 800 | - * @param string|string[] $attr |
|
| 801 | - * @param int $limit |
|
| 802 | - * @param int $offset |
|
| 803 | - * @return array |
|
| 804 | - */ |
|
| 805 | - public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null) { |
|
| 806 | - $ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset); |
|
| 807 | - $this->batchApplyUserAttributes($ldapRecords); |
|
| 808 | - return $this->fetchList($ldapRecords, (count($attr) > 1)); |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - /** |
|
| 812 | - * provided with an array of LDAP user records the method will fetch the |
|
| 813 | - * user object and requests it to process the freshly fetched attributes and |
|
| 814 | - * and their values |
|
| 815 | - * @param array $ldapRecords |
|
| 816 | - */ |
|
| 817 | - public function batchApplyUserAttributes(array $ldapRecords){ |
|
| 818 | - $displayNameAttribute = strtolower($this->connection->ldapUserDisplayName); |
|
| 819 | - foreach($ldapRecords as $userRecord) { |
|
| 820 | - if(!isset($userRecord[$displayNameAttribute])) { |
|
| 821 | - // displayName is obligatory |
|
| 822 | - continue; |
|
| 823 | - } |
|
| 824 | - $ocName = $this->dn2ocname($userRecord['dn'][0]); |
|
| 825 | - if($ocName === false) { |
|
| 826 | - continue; |
|
| 827 | - } |
|
| 828 | - $this->cacheUserExists($ocName); |
|
| 829 | - $user = $this->userManager->get($ocName); |
|
| 830 | - if($user instanceof OfflineUser) { |
|
| 831 | - $user->unmark(); |
|
| 832 | - $user = $this->userManager->get($ocName); |
|
| 833 | - } |
|
| 834 | - if ($user !== null) { |
|
| 835 | - $user->processAttributes($userRecord); |
|
| 836 | - } else { |
|
| 837 | - \OC::$server->getLogger()->debug( |
|
| 838 | - "The ldap user manager returned null for $ocName", |
|
| 839 | - ['app'=>'user_ldap'] |
|
| 840 | - ); |
|
| 841 | - } |
|
| 842 | - } |
|
| 843 | - } |
|
| 844 | - |
|
| 845 | - /** |
|
| 846 | - * @param string $filter |
|
| 847 | - * @param string|string[] $attr |
|
| 848 | - * @param int $limit |
|
| 849 | - * @param int $offset |
|
| 850 | - * @return array |
|
| 851 | - */ |
|
| 852 | - public function fetchListOfGroups($filter, $attr, $limit = null, $offset = null) { |
|
| 853 | - return $this->fetchList($this->searchGroups($filter, $attr, $limit, $offset), (count($attr) > 1)); |
|
| 854 | - } |
|
| 855 | - |
|
| 856 | - /** |
|
| 857 | - * @param array $list |
|
| 858 | - * @param bool $manyAttributes |
|
| 859 | - * @return array |
|
| 860 | - */ |
|
| 861 | - private function fetchList($list, $manyAttributes) { |
|
| 862 | - if(is_array($list)) { |
|
| 863 | - if($manyAttributes) { |
|
| 864 | - return $list; |
|
| 865 | - } else { |
|
| 866 | - $list = array_reduce($list, function($carry, $item) { |
|
| 867 | - $attribute = array_keys($item)[0]; |
|
| 868 | - $carry[] = $item[$attribute][0]; |
|
| 869 | - return $carry; |
|
| 870 | - }, array()); |
|
| 871 | - return array_unique($list, SORT_LOCALE_STRING); |
|
| 872 | - } |
|
| 873 | - } |
|
| 874 | - |
|
| 875 | - //error cause actually, maybe throw an exception in future. |
|
| 876 | - return array(); |
|
| 877 | - } |
|
| 878 | - |
|
| 879 | - /** |
|
| 880 | - * executes an LDAP search, optimized for Users |
|
| 881 | - * @param string $filter the LDAP filter for the search |
|
| 882 | - * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
|
| 883 | - * @param integer $limit |
|
| 884 | - * @param integer $offset |
|
| 885 | - * @return array with the search result |
|
| 886 | - * |
|
| 887 | - * Executes an LDAP search |
|
| 888 | - */ |
|
| 889 | - public function searchUsers($filter, $attr = null, $limit = null, $offset = null) { |
|
| 890 | - return $this->search($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset); |
|
| 891 | - } |
|
| 892 | - |
|
| 893 | - /** |
|
| 894 | - * @param string $filter |
|
| 895 | - * @param string|string[] $attr |
|
| 896 | - * @param int $limit |
|
| 897 | - * @param int $offset |
|
| 898 | - * @return false|int |
|
| 899 | - */ |
|
| 900 | - public function countUsers($filter, $attr = array('dn'), $limit = null, $offset = null) { |
|
| 901 | - return $this->count($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset); |
|
| 902 | - } |
|
| 903 | - |
|
| 904 | - /** |
|
| 905 | - * executes an LDAP search, optimized for Groups |
|
| 906 | - * @param string $filter the LDAP filter for the search |
|
| 907 | - * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
|
| 908 | - * @param integer $limit |
|
| 909 | - * @param integer $offset |
|
| 910 | - * @return array with the search result |
|
| 911 | - * |
|
| 912 | - * Executes an LDAP search |
|
| 913 | - */ |
|
| 914 | - public function searchGroups($filter, $attr = null, $limit = null, $offset = null) { |
|
| 915 | - return $this->search($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset); |
|
| 916 | - } |
|
| 917 | - |
|
| 918 | - /** |
|
| 919 | - * returns the number of available groups |
|
| 920 | - * @param string $filter the LDAP search filter |
|
| 921 | - * @param string[] $attr optional |
|
| 922 | - * @param int|null $limit |
|
| 923 | - * @param int|null $offset |
|
| 924 | - * @return int|bool |
|
| 925 | - */ |
|
| 926 | - public function countGroups($filter, $attr = array('dn'), $limit = null, $offset = null) { |
|
| 927 | - return $this->count($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset); |
|
| 928 | - } |
|
| 929 | - |
|
| 930 | - /** |
|
| 931 | - * returns the number of available objects on the base DN |
|
| 932 | - * |
|
| 933 | - * @param int|null $limit |
|
| 934 | - * @param int|null $offset |
|
| 935 | - * @return int|bool |
|
| 936 | - */ |
|
| 937 | - public function countObjects($limit = null, $offset = null) { |
|
| 938 | - return $this->count('objectclass=*', $this->connection->ldapBase, array('dn'), $limit, $offset); |
|
| 939 | - } |
|
| 940 | - |
|
| 941 | - /** |
|
| 942 | - * Returns the LDAP handler |
|
| 943 | - * @throws \OC\ServerNotAvailableException |
|
| 944 | - */ |
|
| 945 | - |
|
| 946 | - /** |
|
| 947 | - * @return mixed |
|
| 948 | - * @throws \OC\ServerNotAvailableException |
|
| 949 | - */ |
|
| 950 | - private function invokeLDAPMethod() { |
|
| 951 | - $arguments = func_get_args(); |
|
| 952 | - $command = array_shift($arguments); |
|
| 953 | - $cr = array_shift($arguments); |
|
| 954 | - if (!method_exists($this->ldap, $command)) { |
|
| 955 | - return null; |
|
| 956 | - } |
|
| 957 | - array_unshift($arguments, $cr); |
|
| 958 | - // php no longer supports call-time pass-by-reference |
|
| 959 | - // thus cannot support controlPagedResultResponse as the third argument |
|
| 960 | - // is a reference |
|
| 961 | - $doMethod = function () use ($command, &$arguments) { |
|
| 962 | - if ($command == 'controlPagedResultResponse') { |
|
| 963 | - throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.'); |
|
| 964 | - } else { |
|
| 965 | - return call_user_func_array(array($this->ldap, $command), $arguments); |
|
| 966 | - } |
|
| 967 | - }; |
|
| 968 | - try { |
|
| 969 | - $ret = $doMethod(); |
|
| 970 | - } catch (ServerNotAvailableException $e) { |
|
| 971 | - /* Server connection lost, attempt to reestablish it |
|
| 329 | + /** |
|
| 330 | + * Set password for an LDAP user identified by a DN |
|
| 331 | + * |
|
| 332 | + * @param string $userDN the user in question |
|
| 333 | + * @param string $password the new password |
|
| 334 | + * @return bool |
|
| 335 | + * @throws HintException |
|
| 336 | + * @throws \Exception |
|
| 337 | + */ |
|
| 338 | + public function setPassword($userDN, $password) { |
|
| 339 | + if(intval($this->connection->turnOnPasswordChange) !== 1) { |
|
| 340 | + throw new \Exception('LDAP password changes are disabled.'); |
|
| 341 | + } |
|
| 342 | + $cr = $this->connection->getConnectionResource(); |
|
| 343 | + if(!$this->ldap->isResource($cr)) { |
|
| 344 | + //LDAP not available |
|
| 345 | + \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
|
| 346 | + return false; |
|
| 347 | + } |
|
| 348 | + try { |
|
| 349 | + return @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password); |
|
| 350 | + } catch(ConstraintViolationException $e) { |
|
| 351 | + throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ').$e->getMessage(), $e->getCode()); |
|
| 352 | + } |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * checks whether the given attributes value is probably a DN |
|
| 357 | + * @param string $attr the attribute in question |
|
| 358 | + * @return boolean if so true, otherwise false |
|
| 359 | + */ |
|
| 360 | + private function resemblesDN($attr) { |
|
| 361 | + $resemblingAttributes = array( |
|
| 362 | + 'dn', |
|
| 363 | + 'uniquemember', |
|
| 364 | + 'member', |
|
| 365 | + // memberOf is an "operational" attribute, without a definition in any RFC |
|
| 366 | + 'memberof' |
|
| 367 | + ); |
|
| 368 | + return in_array($attr, $resemblingAttributes); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * checks whether the given string is probably a DN |
|
| 373 | + * @param string $string |
|
| 374 | + * @return boolean |
|
| 375 | + */ |
|
| 376 | + public function stringResemblesDN($string) { |
|
| 377 | + $r = $this->ldap->explodeDN($string, 0); |
|
| 378 | + // if exploding a DN succeeds and does not end up in |
|
| 379 | + // an empty array except for $r[count] being 0. |
|
| 380 | + return (is_array($r) && count($r) > 1); |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * returns a DN-string that is cleaned from not domain parts, e.g. |
|
| 385 | + * cn=foo,cn=bar,dc=foobar,dc=server,dc=org |
|
| 386 | + * becomes dc=foobar,dc=server,dc=org |
|
| 387 | + * @param string $dn |
|
| 388 | + * @return string |
|
| 389 | + */ |
|
| 390 | + public function getDomainDNFromDN($dn) { |
|
| 391 | + $allParts = $this->ldap->explodeDN($dn, 0); |
|
| 392 | + if($allParts === false) { |
|
| 393 | + //not a valid DN |
|
| 394 | + return ''; |
|
| 395 | + } |
|
| 396 | + $domainParts = array(); |
|
| 397 | + $dcFound = false; |
|
| 398 | + foreach($allParts as $part) { |
|
| 399 | + if(!$dcFound && strpos($part, 'dc=') === 0) { |
|
| 400 | + $dcFound = true; |
|
| 401 | + } |
|
| 402 | + if($dcFound) { |
|
| 403 | + $domainParts[] = $part; |
|
| 404 | + } |
|
| 405 | + } |
|
| 406 | + $domainDN = implode(',', $domainParts); |
|
| 407 | + return $domainDN; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * returns the LDAP DN for the given internal Nextcloud name of the group |
|
| 412 | + * @param string $name the Nextcloud name in question |
|
| 413 | + * @return string|false LDAP DN on success, otherwise false |
|
| 414 | + */ |
|
| 415 | + public function groupname2dn($name) { |
|
| 416 | + return $this->groupMapper->getDNByName($name); |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * returns the LDAP DN for the given internal Nextcloud name of the user |
|
| 421 | + * @param string $name the Nextcloud name in question |
|
| 422 | + * @return string|false with the LDAP DN on success, otherwise false |
|
| 423 | + */ |
|
| 424 | + public function username2dn($name) { |
|
| 425 | + $fdn = $this->userMapper->getDNByName($name); |
|
| 426 | + |
|
| 427 | + //Check whether the DN belongs to the Base, to avoid issues on multi- |
|
| 428 | + //server setups |
|
| 429 | + if(is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 430 | + return $fdn; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + return false; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * returns the internal Nextcloud name for the given LDAP DN of the group, false on DN outside of search DN or failure |
|
| 438 | + * @param string $fdn the dn of the group object |
|
| 439 | + * @param string $ldapName optional, the display name of the object |
|
| 440 | + * @return string|false with the name to use in Nextcloud, false on DN outside of search DN |
|
| 441 | + */ |
|
| 442 | + public function dn2groupname($fdn, $ldapName = null) { |
|
| 443 | + //To avoid bypassing the base DN settings under certain circumstances |
|
| 444 | + //with the group support, check whether the provided DN matches one of |
|
| 445 | + //the given Bases |
|
| 446 | + if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { |
|
| 447 | + return false; |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + return $this->dn2ocname($fdn, $ldapName, false); |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * accepts an array of group DNs and tests whether they match the user |
|
| 455 | + * filter by doing read operations against the group entries. Returns an |
|
| 456 | + * array of DNs that match the filter. |
|
| 457 | + * |
|
| 458 | + * @param string[] $groupDNs |
|
| 459 | + * @return string[] |
|
| 460 | + */ |
|
| 461 | + public function groupsMatchFilter($groupDNs) { |
|
| 462 | + $validGroupDNs = []; |
|
| 463 | + foreach($groupDNs as $dn) { |
|
| 464 | + $cacheKey = 'groupsMatchFilter-'.$dn; |
|
| 465 | + $groupMatchFilter = $this->connection->getFromCache($cacheKey); |
|
| 466 | + if(!is_null($groupMatchFilter)) { |
|
| 467 | + if($groupMatchFilter) { |
|
| 468 | + $validGroupDNs[] = $dn; |
|
| 469 | + } |
|
| 470 | + continue; |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + // Check the base DN first. If this is not met already, we don't |
|
| 474 | + // need to ask the server at all. |
|
| 475 | + if(!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { |
|
| 476 | + $this->connection->writeToCache($cacheKey, false); |
|
| 477 | + continue; |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + $result = $this->readAttribute($dn, 'cn', $this->connection->ldapGroupFilter); |
|
| 481 | + if(is_array($result)) { |
|
| 482 | + $this->connection->writeToCache($cacheKey, true); |
|
| 483 | + $validGroupDNs[] = $dn; |
|
| 484 | + } else { |
|
| 485 | + $this->connection->writeToCache($cacheKey, false); |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + } |
|
| 489 | + return $validGroupDNs; |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + /** |
|
| 493 | + * returns the internal Nextcloud name for the given LDAP DN of the user, false on DN outside of search DN or failure |
|
| 494 | + * @param string $dn the dn of the user object |
|
| 495 | + * @param string $ldapName optional, the display name of the object |
|
| 496 | + * @return string|false with with the name to use in Nextcloud |
|
| 497 | + */ |
|
| 498 | + public function dn2username($fdn, $ldapName = null) { |
|
| 499 | + //To avoid bypassing the base DN settings under certain circumstances |
|
| 500 | + //with the group support, check whether the provided DN matches one of |
|
| 501 | + //the given Bases |
|
| 502 | + if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 503 | + return false; |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + return $this->dn2ocname($fdn, $ldapName, true); |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + /** |
|
| 510 | + * returns an internal Nextcloud name for the given LDAP DN, false on DN outside of search DN |
|
| 511 | + * @param string $dn the dn of the user object |
|
| 512 | + * @param string $ldapName optional, the display name of the object |
|
| 513 | + * @param bool $isUser optional, whether it is a user object (otherwise group assumed) |
|
| 514 | + * @return string|false with with the name to use in Nextcloud |
|
| 515 | + */ |
|
| 516 | + public function dn2ocname($fdn, $ldapName = null, $isUser = true) { |
|
| 517 | + if($isUser) { |
|
| 518 | + $mapper = $this->getUserMapper(); |
|
| 519 | + $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 520 | + } else { |
|
| 521 | + $mapper = $this->getGroupMapper(); |
|
| 522 | + $nameAttribute = $this->connection->ldapGroupDisplayName; |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + //let's try to retrieve the Nextcloud name from the mappings table |
|
| 526 | + $ocName = $mapper->getNameByDN($fdn); |
|
| 527 | + if(is_string($ocName)) { |
|
| 528 | + return $ocName; |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + //second try: get the UUID and check if it is known. Then, update the DN and return the name. |
|
| 532 | + $uuid = $this->getUUID($fdn, $isUser); |
|
| 533 | + if(is_string($uuid)) { |
|
| 534 | + $ocName = $mapper->getNameByUUID($uuid); |
|
| 535 | + if(is_string($ocName)) { |
|
| 536 | + $mapper->setDNbyUUID($fdn, $uuid); |
|
| 537 | + return $ocName; |
|
| 538 | + } |
|
| 539 | + } else { |
|
| 540 | + //If the UUID can't be detected something is foul. |
|
| 541 | + \OCP\Util::writeLog('user_ldap', 'Cannot determine UUID for '.$fdn.'. Skipping.', \OCP\Util::INFO); |
|
| 542 | + return false; |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + if(is_null($ldapName)) { |
|
| 546 | + $ldapName = $this->readAttribute($fdn, $nameAttribute); |
|
| 547 | + if(!isset($ldapName[0]) && empty($ldapName[0])) { |
|
| 548 | + \OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.'.', \OCP\Util::INFO); |
|
| 549 | + return false; |
|
| 550 | + } |
|
| 551 | + $ldapName = $ldapName[0]; |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + if($isUser) { |
|
| 555 | + $usernameAttribute = strval($this->connection->ldapExpertUsernameAttr); |
|
| 556 | + if ($usernameAttribute !== '') { |
|
| 557 | + $username = $this->readAttribute($fdn, $usernameAttribute); |
|
| 558 | + $username = $username[0]; |
|
| 559 | + } else { |
|
| 560 | + $username = $uuid; |
|
| 561 | + } |
|
| 562 | + $intName = $this->sanitizeUsername($username); |
|
| 563 | + } else { |
|
| 564 | + $intName = $ldapName; |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + //a new user/group! Add it only if it doesn't conflict with other backend's users or existing groups |
|
| 568 | + //disabling Cache is required to avoid that the new user is cached as not-existing in fooExists check |
|
| 569 | + //NOTE: mind, disabling cache affects only this instance! Using it |
|
| 570 | + // outside of core user management will still cache the user as non-existing. |
|
| 571 | + $originalTTL = $this->connection->ldapCacheTTL; |
|
| 572 | + $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
|
| 573 | + if(($isUser && !\OCP\User::userExists($intName)) |
|
| 574 | + || (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))) { |
|
| 575 | + if($mapper->map($fdn, $intName, $uuid)) { |
|
| 576 | + $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 577 | + return $intName; |
|
| 578 | + } |
|
| 579 | + } |
|
| 580 | + $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 581 | + |
|
| 582 | + $altName = $this->createAltInternalOwnCloudName($intName, $isUser); |
|
| 583 | + if(is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { |
|
| 584 | + return $altName; |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + //if everything else did not help.. |
|
| 588 | + \OCP\Util::writeLog('user_ldap', 'Could not create unique name for '.$fdn.'.', \OCP\Util::INFO); |
|
| 589 | + return false; |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + /** |
|
| 593 | + * gives back the user names as they are used ownClod internally |
|
| 594 | + * @param array $ldapUsers as returned by fetchList() |
|
| 595 | + * @return array an array with the user names to use in Nextcloud |
|
| 596 | + * |
|
| 597 | + * gives back the user names as they are used ownClod internally |
|
| 598 | + */ |
|
| 599 | + public function nextcloudUserNames($ldapUsers) { |
|
| 600 | + return $this->ldap2NextcloudNames($ldapUsers, true); |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + /** |
|
| 604 | + * gives back the group names as they are used ownClod internally |
|
| 605 | + * @param array $ldapGroups as returned by fetchList() |
|
| 606 | + * @return array an array with the group names to use in Nextcloud |
|
| 607 | + * |
|
| 608 | + * gives back the group names as they are used ownClod internally |
|
| 609 | + */ |
|
| 610 | + public function nextcloudGroupNames($ldapGroups) { |
|
| 611 | + return $this->ldap2NextcloudNames($ldapGroups, false); |
|
| 612 | + } |
|
| 613 | + |
|
| 614 | + /** |
|
| 615 | + * @param array $ldapObjects as returned by fetchList() |
|
| 616 | + * @param bool $isUsers |
|
| 617 | + * @return array |
|
| 618 | + */ |
|
| 619 | + private function ldap2NextcloudNames($ldapObjects, $isUsers) { |
|
| 620 | + if($isUsers) { |
|
| 621 | + $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 622 | + $sndAttribute = $this->connection->ldapUserDisplayName2; |
|
| 623 | + } else { |
|
| 624 | + $nameAttribute = $this->connection->ldapGroupDisplayName; |
|
| 625 | + } |
|
| 626 | + $nextcloudNames = array(); |
|
| 627 | + |
|
| 628 | + foreach($ldapObjects as $ldapObject) { |
|
| 629 | + $nameByLDAP = null; |
|
| 630 | + if( isset($ldapObject[$nameAttribute]) |
|
| 631 | + && is_array($ldapObject[$nameAttribute]) |
|
| 632 | + && isset($ldapObject[$nameAttribute][0]) |
|
| 633 | + ) { |
|
| 634 | + // might be set, but not necessarily. if so, we use it. |
|
| 635 | + $nameByLDAP = $ldapObject[$nameAttribute][0]; |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + $ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers); |
|
| 639 | + if($ncName) { |
|
| 640 | + $nextcloudNames[] = $ncName; |
|
| 641 | + if($isUsers) { |
|
| 642 | + //cache the user names so it does not need to be retrieved |
|
| 643 | + //again later (e.g. sharing dialogue). |
|
| 644 | + if(is_null($nameByLDAP)) { |
|
| 645 | + continue; |
|
| 646 | + } |
|
| 647 | + $sndName = isset($ldapObject[$sndAttribute][0]) |
|
| 648 | + ? $ldapObject[$sndAttribute][0] : ''; |
|
| 649 | + $this->cacheUserDisplayName($ncName, $nameByLDAP, $sndName); |
|
| 650 | + } |
|
| 651 | + } |
|
| 652 | + } |
|
| 653 | + return $nextcloudNames; |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + /** |
|
| 657 | + * caches the user display name |
|
| 658 | + * @param string $ocName the internal Nextcloud username |
|
| 659 | + * @param string|false $home the home directory path |
|
| 660 | + */ |
|
| 661 | + public function cacheUserHome($ocName, $home) { |
|
| 662 | + $cacheKey = 'getHome'.$ocName; |
|
| 663 | + $this->connection->writeToCache($cacheKey, $home); |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + /** |
|
| 667 | + * caches a user as existing |
|
| 668 | + * @param string $ocName the internal Nextcloud username |
|
| 669 | + */ |
|
| 670 | + public function cacheUserExists($ocName) { |
|
| 671 | + $this->connection->writeToCache('userExists'.$ocName, true); |
|
| 672 | + } |
|
| 673 | + |
|
| 674 | + /** |
|
| 675 | + * caches the user display name |
|
| 676 | + * @param string $ocName the internal Nextcloud username |
|
| 677 | + * @param string $displayName the display name |
|
| 678 | + * @param string $displayName2 the second display name |
|
| 679 | + */ |
|
| 680 | + public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { |
|
| 681 | + $user = $this->userManager->get($ocName); |
|
| 682 | + if($user === null) { |
|
| 683 | + return; |
|
| 684 | + } |
|
| 685 | + $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
| 686 | + $cacheKeyTrunk = 'getDisplayName'; |
|
| 687 | + $this->connection->writeToCache($cacheKeyTrunk.$ocName, $displayName); |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + /** |
|
| 691 | + * creates a unique name for internal Nextcloud use for users. Don't call it directly. |
|
| 692 | + * @param string $name the display name of the object |
|
| 693 | + * @return string|false with with the name to use in Nextcloud or false if unsuccessful |
|
| 694 | + * |
|
| 695 | + * Instead of using this method directly, call |
|
| 696 | + * createAltInternalOwnCloudName($name, true) |
|
| 697 | + */ |
|
| 698 | + private function _createAltInternalOwnCloudNameForUsers($name) { |
|
| 699 | + $attempts = 0; |
|
| 700 | + //while loop is just a precaution. If a name is not generated within |
|
| 701 | + //20 attempts, something else is very wrong. Avoids infinite loop. |
|
| 702 | + while($attempts < 20){ |
|
| 703 | + $altName = $name . '_' . rand(1000,9999); |
|
| 704 | + if(!\OCP\User::userExists($altName)) { |
|
| 705 | + return $altName; |
|
| 706 | + } |
|
| 707 | + $attempts++; |
|
| 708 | + } |
|
| 709 | + return false; |
|
| 710 | + } |
|
| 711 | + |
|
| 712 | + /** |
|
| 713 | + * creates a unique name for internal Nextcloud use for groups. Don't call it directly. |
|
| 714 | + * @param string $name the display name of the object |
|
| 715 | + * @return string|false with with the name to use in Nextcloud or false if unsuccessful. |
|
| 716 | + * |
|
| 717 | + * Instead of using this method directly, call |
|
| 718 | + * createAltInternalOwnCloudName($name, false) |
|
| 719 | + * |
|
| 720 | + * Group names are also used as display names, so we do a sequential |
|
| 721 | + * numbering, e.g. Developers_42 when there are 41 other groups called |
|
| 722 | + * "Developers" |
|
| 723 | + */ |
|
| 724 | + private function _createAltInternalOwnCloudNameForGroups($name) { |
|
| 725 | + $usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%'); |
|
| 726 | + if(!($usedNames) || count($usedNames) === 0) { |
|
| 727 | + $lastNo = 1; //will become name_2 |
|
| 728 | + } else { |
|
| 729 | + natsort($usedNames); |
|
| 730 | + $lastName = array_pop($usedNames); |
|
| 731 | + $lastNo = intval(substr($lastName, strrpos($lastName, '_') + 1)); |
|
| 732 | + } |
|
| 733 | + $altName = $name.'_'.strval($lastNo+1); |
|
| 734 | + unset($usedNames); |
|
| 735 | + |
|
| 736 | + $attempts = 1; |
|
| 737 | + while($attempts < 21){ |
|
| 738 | + // Check to be really sure it is unique |
|
| 739 | + // while loop is just a precaution. If a name is not generated within |
|
| 740 | + // 20 attempts, something else is very wrong. Avoids infinite loop. |
|
| 741 | + if(!\OC::$server->getGroupManager()->groupExists($altName)) { |
|
| 742 | + return $altName; |
|
| 743 | + } |
|
| 744 | + $altName = $name . '_' . ($lastNo + $attempts); |
|
| 745 | + $attempts++; |
|
| 746 | + } |
|
| 747 | + return false; |
|
| 748 | + } |
|
| 749 | + |
|
| 750 | + /** |
|
| 751 | + * creates a unique name for internal Nextcloud use. |
|
| 752 | + * @param string $name the display name of the object |
|
| 753 | + * @param boolean $isUser whether name should be created for a user (true) or a group (false) |
|
| 754 | + * @return string|false with with the name to use in Nextcloud or false if unsuccessful |
|
| 755 | + */ |
|
| 756 | + private function createAltInternalOwnCloudName($name, $isUser) { |
|
| 757 | + $originalTTL = $this->connection->ldapCacheTTL; |
|
| 758 | + $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
|
| 759 | + if($isUser) { |
|
| 760 | + $altName = $this->_createAltInternalOwnCloudNameForUsers($name); |
|
| 761 | + } else { |
|
| 762 | + $altName = $this->_createAltInternalOwnCloudNameForGroups($name); |
|
| 763 | + } |
|
| 764 | + $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 765 | + |
|
| 766 | + return $altName; |
|
| 767 | + } |
|
| 768 | + |
|
| 769 | + /** |
|
| 770 | + * fetches a list of users according to a provided loginName and utilizing |
|
| 771 | + * the login filter. |
|
| 772 | + * |
|
| 773 | + * @param string $loginName |
|
| 774 | + * @param array $attributes optional, list of attributes to read |
|
| 775 | + * @return array |
|
| 776 | + */ |
|
| 777 | + public function fetchUsersByLoginName($loginName, $attributes = array('dn')) { |
|
| 778 | + $loginName = $this->escapeFilterPart($loginName); |
|
| 779 | + $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); |
|
| 780 | + $users = $this->fetchListOfUsers($filter, $attributes); |
|
| 781 | + return $users; |
|
| 782 | + } |
|
| 783 | + |
|
| 784 | + /** |
|
| 785 | + * counts the number of users according to a provided loginName and |
|
| 786 | + * utilizing the login filter. |
|
| 787 | + * |
|
| 788 | + * @param string $loginName |
|
| 789 | + * @return array |
|
| 790 | + */ |
|
| 791 | + public function countUsersByLoginName($loginName) { |
|
| 792 | + $loginName = $this->escapeFilterPart($loginName); |
|
| 793 | + $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); |
|
| 794 | + $users = $this->countUsers($filter); |
|
| 795 | + return $users; |
|
| 796 | + } |
|
| 797 | + |
|
| 798 | + /** |
|
| 799 | + * @param string $filter |
|
| 800 | + * @param string|string[] $attr |
|
| 801 | + * @param int $limit |
|
| 802 | + * @param int $offset |
|
| 803 | + * @return array |
|
| 804 | + */ |
|
| 805 | + public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null) { |
|
| 806 | + $ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset); |
|
| 807 | + $this->batchApplyUserAttributes($ldapRecords); |
|
| 808 | + return $this->fetchList($ldapRecords, (count($attr) > 1)); |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + /** |
|
| 812 | + * provided with an array of LDAP user records the method will fetch the |
|
| 813 | + * user object and requests it to process the freshly fetched attributes and |
|
| 814 | + * and their values |
|
| 815 | + * @param array $ldapRecords |
|
| 816 | + */ |
|
| 817 | + public function batchApplyUserAttributes(array $ldapRecords){ |
|
| 818 | + $displayNameAttribute = strtolower($this->connection->ldapUserDisplayName); |
|
| 819 | + foreach($ldapRecords as $userRecord) { |
|
| 820 | + if(!isset($userRecord[$displayNameAttribute])) { |
|
| 821 | + // displayName is obligatory |
|
| 822 | + continue; |
|
| 823 | + } |
|
| 824 | + $ocName = $this->dn2ocname($userRecord['dn'][0]); |
|
| 825 | + if($ocName === false) { |
|
| 826 | + continue; |
|
| 827 | + } |
|
| 828 | + $this->cacheUserExists($ocName); |
|
| 829 | + $user = $this->userManager->get($ocName); |
|
| 830 | + if($user instanceof OfflineUser) { |
|
| 831 | + $user->unmark(); |
|
| 832 | + $user = $this->userManager->get($ocName); |
|
| 833 | + } |
|
| 834 | + if ($user !== null) { |
|
| 835 | + $user->processAttributes($userRecord); |
|
| 836 | + } else { |
|
| 837 | + \OC::$server->getLogger()->debug( |
|
| 838 | + "The ldap user manager returned null for $ocName", |
|
| 839 | + ['app'=>'user_ldap'] |
|
| 840 | + ); |
|
| 841 | + } |
|
| 842 | + } |
|
| 843 | + } |
|
| 844 | + |
|
| 845 | + /** |
|
| 846 | + * @param string $filter |
|
| 847 | + * @param string|string[] $attr |
|
| 848 | + * @param int $limit |
|
| 849 | + * @param int $offset |
|
| 850 | + * @return array |
|
| 851 | + */ |
|
| 852 | + public function fetchListOfGroups($filter, $attr, $limit = null, $offset = null) { |
|
| 853 | + return $this->fetchList($this->searchGroups($filter, $attr, $limit, $offset), (count($attr) > 1)); |
|
| 854 | + } |
|
| 855 | + |
|
| 856 | + /** |
|
| 857 | + * @param array $list |
|
| 858 | + * @param bool $manyAttributes |
|
| 859 | + * @return array |
|
| 860 | + */ |
|
| 861 | + private function fetchList($list, $manyAttributes) { |
|
| 862 | + if(is_array($list)) { |
|
| 863 | + if($manyAttributes) { |
|
| 864 | + return $list; |
|
| 865 | + } else { |
|
| 866 | + $list = array_reduce($list, function($carry, $item) { |
|
| 867 | + $attribute = array_keys($item)[0]; |
|
| 868 | + $carry[] = $item[$attribute][0]; |
|
| 869 | + return $carry; |
|
| 870 | + }, array()); |
|
| 871 | + return array_unique($list, SORT_LOCALE_STRING); |
|
| 872 | + } |
|
| 873 | + } |
|
| 874 | + |
|
| 875 | + //error cause actually, maybe throw an exception in future. |
|
| 876 | + return array(); |
|
| 877 | + } |
|
| 878 | + |
|
| 879 | + /** |
|
| 880 | + * executes an LDAP search, optimized for Users |
|
| 881 | + * @param string $filter the LDAP filter for the search |
|
| 882 | + * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
|
| 883 | + * @param integer $limit |
|
| 884 | + * @param integer $offset |
|
| 885 | + * @return array with the search result |
|
| 886 | + * |
|
| 887 | + * Executes an LDAP search |
|
| 888 | + */ |
|
| 889 | + public function searchUsers($filter, $attr = null, $limit = null, $offset = null) { |
|
| 890 | + return $this->search($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset); |
|
| 891 | + } |
|
| 892 | + |
|
| 893 | + /** |
|
| 894 | + * @param string $filter |
|
| 895 | + * @param string|string[] $attr |
|
| 896 | + * @param int $limit |
|
| 897 | + * @param int $offset |
|
| 898 | + * @return false|int |
|
| 899 | + */ |
|
| 900 | + public function countUsers($filter, $attr = array('dn'), $limit = null, $offset = null) { |
|
| 901 | + return $this->count($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset); |
|
| 902 | + } |
|
| 903 | + |
|
| 904 | + /** |
|
| 905 | + * executes an LDAP search, optimized for Groups |
|
| 906 | + * @param string $filter the LDAP filter for the search |
|
| 907 | + * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
|
| 908 | + * @param integer $limit |
|
| 909 | + * @param integer $offset |
|
| 910 | + * @return array with the search result |
|
| 911 | + * |
|
| 912 | + * Executes an LDAP search |
|
| 913 | + */ |
|
| 914 | + public function searchGroups($filter, $attr = null, $limit = null, $offset = null) { |
|
| 915 | + return $this->search($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset); |
|
| 916 | + } |
|
| 917 | + |
|
| 918 | + /** |
|
| 919 | + * returns the number of available groups |
|
| 920 | + * @param string $filter the LDAP search filter |
|
| 921 | + * @param string[] $attr optional |
|
| 922 | + * @param int|null $limit |
|
| 923 | + * @param int|null $offset |
|
| 924 | + * @return int|bool |
|
| 925 | + */ |
|
| 926 | + public function countGroups($filter, $attr = array('dn'), $limit = null, $offset = null) { |
|
| 927 | + return $this->count($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset); |
|
| 928 | + } |
|
| 929 | + |
|
| 930 | + /** |
|
| 931 | + * returns the number of available objects on the base DN |
|
| 932 | + * |
|
| 933 | + * @param int|null $limit |
|
| 934 | + * @param int|null $offset |
|
| 935 | + * @return int|bool |
|
| 936 | + */ |
|
| 937 | + public function countObjects($limit = null, $offset = null) { |
|
| 938 | + return $this->count('objectclass=*', $this->connection->ldapBase, array('dn'), $limit, $offset); |
|
| 939 | + } |
|
| 940 | + |
|
| 941 | + /** |
|
| 942 | + * Returns the LDAP handler |
|
| 943 | + * @throws \OC\ServerNotAvailableException |
|
| 944 | + */ |
|
| 945 | + |
|
| 946 | + /** |
|
| 947 | + * @return mixed |
|
| 948 | + * @throws \OC\ServerNotAvailableException |
|
| 949 | + */ |
|
| 950 | + private function invokeLDAPMethod() { |
|
| 951 | + $arguments = func_get_args(); |
|
| 952 | + $command = array_shift($arguments); |
|
| 953 | + $cr = array_shift($arguments); |
|
| 954 | + if (!method_exists($this->ldap, $command)) { |
|
| 955 | + return null; |
|
| 956 | + } |
|
| 957 | + array_unshift($arguments, $cr); |
|
| 958 | + // php no longer supports call-time pass-by-reference |
|
| 959 | + // thus cannot support controlPagedResultResponse as the third argument |
|
| 960 | + // is a reference |
|
| 961 | + $doMethod = function () use ($command, &$arguments) { |
|
| 962 | + if ($command == 'controlPagedResultResponse') { |
|
| 963 | + throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.'); |
|
| 964 | + } else { |
|
| 965 | + return call_user_func_array(array($this->ldap, $command), $arguments); |
|
| 966 | + } |
|
| 967 | + }; |
|
| 968 | + try { |
|
| 969 | + $ret = $doMethod(); |
|
| 970 | + } catch (ServerNotAvailableException $e) { |
|
| 971 | + /* Server connection lost, attempt to reestablish it |
|
| 972 | 972 | * Maybe implement exponential backoff? |
| 973 | 973 | * This was enough to get solr indexer working which has large delays between LDAP fetches. |
| 974 | 974 | */ |
| 975 | - \OCP\Util::writeLog('user_ldap', "Connection lost on $command, attempting to reestablish.", \OCP\Util::DEBUG); |
|
| 976 | - $this->connection->resetConnectionResource(); |
|
| 977 | - $cr = $this->connection->getConnectionResource(); |
|
| 978 | - |
|
| 979 | - if(!$this->ldap->isResource($cr)) { |
|
| 980 | - // Seems like we didn't find any resource. |
|
| 981 | - \OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", \OCP\Util::DEBUG); |
|
| 982 | - throw $e; |
|
| 983 | - } |
|
| 984 | - |
|
| 985 | - $arguments[0] = array_pad([], count($arguments[0]), $cr); |
|
| 986 | - $ret = $doMethod(); |
|
| 987 | - } |
|
| 988 | - return $ret; |
|
| 989 | - } |
|
| 990 | - |
|
| 991 | - /** |
|
| 992 | - * retrieved. Results will according to the order in the array. |
|
| 993 | - * @param int $limit optional, maximum results to be counted |
|
| 994 | - * @param int $offset optional, a starting point |
|
| 995 | - * @return array|false array with the search result as first value and pagedSearchOK as |
|
| 996 | - * second | false if not successful |
|
| 997 | - * @throws \OC\ServerNotAvailableException |
|
| 998 | - */ |
|
| 999 | - private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) { |
|
| 1000 | - if(!is_null($attr) && !is_array($attr)) { |
|
| 1001 | - $attr = array(mb_strtolower($attr, 'UTF-8')); |
|
| 1002 | - } |
|
| 1003 | - |
|
| 1004 | - // See if we have a resource, in case not cancel with message |
|
| 1005 | - $cr = $this->connection->getConnectionResource(); |
|
| 1006 | - if(!$this->ldap->isResource($cr)) { |
|
| 1007 | - // Seems like we didn't find any resource. |
|
| 1008 | - // Return an empty array just like before. |
|
| 1009 | - \OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', \OCP\Util::DEBUG); |
|
| 1010 | - return false; |
|
| 1011 | - } |
|
| 1012 | - |
|
| 1013 | - //check whether paged search should be attempted |
|
| 1014 | - $pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, intval($limit), $offset); |
|
| 1015 | - |
|
| 1016 | - $linkResources = array_pad(array(), count($base), $cr); |
|
| 1017 | - $sr = $this->invokeLDAPMethod('search', $linkResources, $base, $filter, $attr); |
|
| 1018 | - // cannot use $cr anymore, might have changed in the previous call! |
|
| 1019 | - $error = $this->ldap->errno($this->connection->getConnectionResource()); |
|
| 1020 | - if(!is_array($sr) || $error !== 0) { |
|
| 1021 | - \OCP\Util::writeLog('user_ldap', 'Attempt for Paging? '.print_r($pagedSearchOK, true), \OCP\Util::ERROR); |
|
| 1022 | - return false; |
|
| 1023 | - } |
|
| 1024 | - |
|
| 1025 | - return array($sr, $pagedSearchOK); |
|
| 1026 | - } |
|
| 1027 | - |
|
| 1028 | - /** |
|
| 1029 | - * processes an LDAP paged search operation |
|
| 1030 | - * @param array $sr the array containing the LDAP search resources |
|
| 1031 | - * @param string $filter the LDAP filter for the search |
|
| 1032 | - * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1033 | - * @param int $iFoundItems number of results in the single search operation |
|
| 1034 | - * @param int $limit maximum results to be counted |
|
| 1035 | - * @param int $offset a starting point |
|
| 1036 | - * @param bool $pagedSearchOK whether a paged search has been executed |
|
| 1037 | - * @param bool $skipHandling required for paged search when cookies to |
|
| 1038 | - * prior results need to be gained |
|
| 1039 | - * @return bool cookie validity, true if we have more pages, false otherwise. |
|
| 1040 | - */ |
|
| 1041 | - private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) { |
|
| 1042 | - $cookie = null; |
|
| 1043 | - if($pagedSearchOK) { |
|
| 1044 | - $cr = $this->connection->getConnectionResource(); |
|
| 1045 | - foreach($sr as $key => $res) { |
|
| 1046 | - if($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) { |
|
| 1047 | - $this->setPagedResultCookie($base[$key], $filter, $limit, $offset, $cookie); |
|
| 1048 | - } |
|
| 1049 | - } |
|
| 1050 | - |
|
| 1051 | - //browsing through prior pages to get the cookie for the new one |
|
| 1052 | - if($skipHandling) { |
|
| 1053 | - return false; |
|
| 1054 | - } |
|
| 1055 | - // if count is bigger, then the server does not support |
|
| 1056 | - // paged search. Instead, he did a normal search. We set a |
|
| 1057 | - // flag here, so the callee knows how to deal with it. |
|
| 1058 | - if($iFoundItems <= $limit) { |
|
| 1059 | - $this->pagedSearchedSuccessful = true; |
|
| 1060 | - } |
|
| 1061 | - } else { |
|
| 1062 | - if(!is_null($limit)) { |
|
| 1063 | - \OCP\Util::writeLog('user_ldap', 'Paged search was not available', \OCP\Util::INFO); |
|
| 1064 | - } |
|
| 1065 | - } |
|
| 1066 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 975 | + \OCP\Util::writeLog('user_ldap', "Connection lost on $command, attempting to reestablish.", \OCP\Util::DEBUG); |
|
| 976 | + $this->connection->resetConnectionResource(); |
|
| 977 | + $cr = $this->connection->getConnectionResource(); |
|
| 978 | + |
|
| 979 | + if(!$this->ldap->isResource($cr)) { |
|
| 980 | + // Seems like we didn't find any resource. |
|
| 981 | + \OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", \OCP\Util::DEBUG); |
|
| 982 | + throw $e; |
|
| 983 | + } |
|
| 984 | + |
|
| 985 | + $arguments[0] = array_pad([], count($arguments[0]), $cr); |
|
| 986 | + $ret = $doMethod(); |
|
| 987 | + } |
|
| 988 | + return $ret; |
|
| 989 | + } |
|
| 990 | + |
|
| 991 | + /** |
|
| 992 | + * retrieved. Results will according to the order in the array. |
|
| 993 | + * @param int $limit optional, maximum results to be counted |
|
| 994 | + * @param int $offset optional, a starting point |
|
| 995 | + * @return array|false array with the search result as first value and pagedSearchOK as |
|
| 996 | + * second | false if not successful |
|
| 997 | + * @throws \OC\ServerNotAvailableException |
|
| 998 | + */ |
|
| 999 | + private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) { |
|
| 1000 | + if(!is_null($attr) && !is_array($attr)) { |
|
| 1001 | + $attr = array(mb_strtolower($attr, 'UTF-8')); |
|
| 1002 | + } |
|
| 1003 | + |
|
| 1004 | + // See if we have a resource, in case not cancel with message |
|
| 1005 | + $cr = $this->connection->getConnectionResource(); |
|
| 1006 | + if(!$this->ldap->isResource($cr)) { |
|
| 1007 | + // Seems like we didn't find any resource. |
|
| 1008 | + // Return an empty array just like before. |
|
| 1009 | + \OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', \OCP\Util::DEBUG); |
|
| 1010 | + return false; |
|
| 1011 | + } |
|
| 1012 | + |
|
| 1013 | + //check whether paged search should be attempted |
|
| 1014 | + $pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, intval($limit), $offset); |
|
| 1015 | + |
|
| 1016 | + $linkResources = array_pad(array(), count($base), $cr); |
|
| 1017 | + $sr = $this->invokeLDAPMethod('search', $linkResources, $base, $filter, $attr); |
|
| 1018 | + // cannot use $cr anymore, might have changed in the previous call! |
|
| 1019 | + $error = $this->ldap->errno($this->connection->getConnectionResource()); |
|
| 1020 | + if(!is_array($sr) || $error !== 0) { |
|
| 1021 | + \OCP\Util::writeLog('user_ldap', 'Attempt for Paging? '.print_r($pagedSearchOK, true), \OCP\Util::ERROR); |
|
| 1022 | + return false; |
|
| 1023 | + } |
|
| 1024 | + |
|
| 1025 | + return array($sr, $pagedSearchOK); |
|
| 1026 | + } |
|
| 1027 | + |
|
| 1028 | + /** |
|
| 1029 | + * processes an LDAP paged search operation |
|
| 1030 | + * @param array $sr the array containing the LDAP search resources |
|
| 1031 | + * @param string $filter the LDAP filter for the search |
|
| 1032 | + * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1033 | + * @param int $iFoundItems number of results in the single search operation |
|
| 1034 | + * @param int $limit maximum results to be counted |
|
| 1035 | + * @param int $offset a starting point |
|
| 1036 | + * @param bool $pagedSearchOK whether a paged search has been executed |
|
| 1037 | + * @param bool $skipHandling required for paged search when cookies to |
|
| 1038 | + * prior results need to be gained |
|
| 1039 | + * @return bool cookie validity, true if we have more pages, false otherwise. |
|
| 1040 | + */ |
|
| 1041 | + private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) { |
|
| 1042 | + $cookie = null; |
|
| 1043 | + if($pagedSearchOK) { |
|
| 1044 | + $cr = $this->connection->getConnectionResource(); |
|
| 1045 | + foreach($sr as $key => $res) { |
|
| 1046 | + if($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) { |
|
| 1047 | + $this->setPagedResultCookie($base[$key], $filter, $limit, $offset, $cookie); |
|
| 1048 | + } |
|
| 1049 | + } |
|
| 1050 | + |
|
| 1051 | + //browsing through prior pages to get the cookie for the new one |
|
| 1052 | + if($skipHandling) { |
|
| 1053 | + return false; |
|
| 1054 | + } |
|
| 1055 | + // if count is bigger, then the server does not support |
|
| 1056 | + // paged search. Instead, he did a normal search. We set a |
|
| 1057 | + // flag here, so the callee knows how to deal with it. |
|
| 1058 | + if($iFoundItems <= $limit) { |
|
| 1059 | + $this->pagedSearchedSuccessful = true; |
|
| 1060 | + } |
|
| 1061 | + } else { |
|
| 1062 | + if(!is_null($limit)) { |
|
| 1063 | + \OCP\Util::writeLog('user_ldap', 'Paged search was not available', \OCP\Util::INFO); |
|
| 1064 | + } |
|
| 1065 | + } |
|
| 1066 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1067 | 1067 | * Return cookie status. If we don't have more pages, with RHDS |
| 1068 | 1068 | * cookie is null, with openldap cookie is an empty string and |
| 1069 | 1069 | * to 386ds '0' is a valid cookie. Even if $iFoundItems == 0 |
| 1070 | 1070 | */ |
| 1071 | - return !empty($cookie) || $cookie === '0'; |
|
| 1072 | - } |
|
| 1073 | - |
|
| 1074 | - /** |
|
| 1075 | - * executes an LDAP search, but counts the results only |
|
| 1076 | - * @param string $filter the LDAP filter for the search |
|
| 1077 | - * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1078 | - * @param string|string[] $attr optional, array, one or more attributes that shall be |
|
| 1079 | - * retrieved. Results will according to the order in the array. |
|
| 1080 | - * @param int $limit optional, maximum results to be counted |
|
| 1081 | - * @param int $offset optional, a starting point |
|
| 1082 | - * @param bool $skipHandling indicates whether the pages search operation is |
|
| 1083 | - * completed |
|
| 1084 | - * @return int|false Integer or false if the search could not be initialized |
|
| 1085 | - * |
|
| 1086 | - */ |
|
| 1087 | - private function count($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
|
| 1088 | - \OCP\Util::writeLog('user_ldap', 'Count filter: '.print_r($filter, true), \OCP\Util::DEBUG); |
|
| 1089 | - |
|
| 1090 | - $limitPerPage = intval($this->connection->ldapPagingSize); |
|
| 1091 | - if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1092 | - $limitPerPage = $limit; |
|
| 1093 | - } |
|
| 1094 | - |
|
| 1095 | - $counter = 0; |
|
| 1096 | - $count = null; |
|
| 1097 | - $this->connection->getConnectionResource(); |
|
| 1098 | - |
|
| 1099 | - do { |
|
| 1100 | - $search = $this->executeSearch($filter, $base, $attr, |
|
| 1101 | - $limitPerPage, $offset); |
|
| 1102 | - if($search === false) { |
|
| 1103 | - return $counter > 0 ? $counter : false; |
|
| 1104 | - } |
|
| 1105 | - list($sr, $pagedSearchOK) = $search; |
|
| 1106 | - |
|
| 1107 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1071 | + return !empty($cookie) || $cookie === '0'; |
|
| 1072 | + } |
|
| 1073 | + |
|
| 1074 | + /** |
|
| 1075 | + * executes an LDAP search, but counts the results only |
|
| 1076 | + * @param string $filter the LDAP filter for the search |
|
| 1077 | + * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1078 | + * @param string|string[] $attr optional, array, one or more attributes that shall be |
|
| 1079 | + * retrieved. Results will according to the order in the array. |
|
| 1080 | + * @param int $limit optional, maximum results to be counted |
|
| 1081 | + * @param int $offset optional, a starting point |
|
| 1082 | + * @param bool $skipHandling indicates whether the pages search operation is |
|
| 1083 | + * completed |
|
| 1084 | + * @return int|false Integer or false if the search could not be initialized |
|
| 1085 | + * |
|
| 1086 | + */ |
|
| 1087 | + private function count($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
|
| 1088 | + \OCP\Util::writeLog('user_ldap', 'Count filter: '.print_r($filter, true), \OCP\Util::DEBUG); |
|
| 1089 | + |
|
| 1090 | + $limitPerPage = intval($this->connection->ldapPagingSize); |
|
| 1091 | + if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1092 | + $limitPerPage = $limit; |
|
| 1093 | + } |
|
| 1094 | + |
|
| 1095 | + $counter = 0; |
|
| 1096 | + $count = null; |
|
| 1097 | + $this->connection->getConnectionResource(); |
|
| 1098 | + |
|
| 1099 | + do { |
|
| 1100 | + $search = $this->executeSearch($filter, $base, $attr, |
|
| 1101 | + $limitPerPage, $offset); |
|
| 1102 | + if($search === false) { |
|
| 1103 | + return $counter > 0 ? $counter : false; |
|
| 1104 | + } |
|
| 1105 | + list($sr, $pagedSearchOK) = $search; |
|
| 1106 | + |
|
| 1107 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1108 | 1108 | * countEntriesInSearchResults() method signature changed |
| 1109 | 1109 | * by removing $limit and &$hasHitLimit parameters |
| 1110 | 1110 | */ |
| 1111 | - $count = $this->countEntriesInSearchResults($sr); |
|
| 1112 | - $counter += $count; |
|
| 1111 | + $count = $this->countEntriesInSearchResults($sr); |
|
| 1112 | + $counter += $count; |
|
| 1113 | 1113 | |
| 1114 | - $hasMorePages = $this->processPagedSearchStatus($sr, $filter, $base, $count, $limitPerPage, |
|
| 1115 | - $offset, $pagedSearchOK, $skipHandling); |
|
| 1116 | - $offset += $limitPerPage; |
|
| 1117 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1114 | + $hasMorePages = $this->processPagedSearchStatus($sr, $filter, $base, $count, $limitPerPage, |
|
| 1115 | + $offset, $pagedSearchOK, $skipHandling); |
|
| 1116 | + $offset += $limitPerPage; |
|
| 1117 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1118 | 1118 | * Continue now depends on $hasMorePages value |
| 1119 | 1119 | */ |
| 1120 | - $continue = $pagedSearchOK && $hasMorePages; |
|
| 1121 | - } while($continue && (is_null($limit) || $limit <= 0 || $limit > $counter)); |
|
| 1122 | - |
|
| 1123 | - return $counter; |
|
| 1124 | - } |
|
| 1125 | - |
|
| 1126 | - /** |
|
| 1127 | - * @param array $searchResults |
|
| 1128 | - * @return int |
|
| 1129 | - */ |
|
| 1130 | - private function countEntriesInSearchResults($searchResults) { |
|
| 1131 | - $counter = 0; |
|
| 1132 | - |
|
| 1133 | - foreach($searchResults as $res) { |
|
| 1134 | - $count = intval($this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res)); |
|
| 1135 | - $counter += $count; |
|
| 1136 | - } |
|
| 1137 | - |
|
| 1138 | - return $counter; |
|
| 1139 | - } |
|
| 1140 | - |
|
| 1141 | - /** |
|
| 1142 | - * Executes an LDAP search |
|
| 1143 | - * @param string $filter the LDAP filter for the search |
|
| 1144 | - * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1145 | - * @param string|string[] $attr optional, array, one or more attributes that shall be |
|
| 1146 | - * @param int $limit |
|
| 1147 | - * @param int $offset |
|
| 1148 | - * @param bool $skipHandling |
|
| 1149 | - * @return array with the search result |
|
| 1150 | - */ |
|
| 1151 | - public function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
|
| 1152 | - $limitPerPage = intval($this->connection->ldapPagingSize); |
|
| 1153 | - if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1154 | - $limitPerPage = $limit; |
|
| 1155 | - } |
|
| 1156 | - |
|
| 1157 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1120 | + $continue = $pagedSearchOK && $hasMorePages; |
|
| 1121 | + } while($continue && (is_null($limit) || $limit <= 0 || $limit > $counter)); |
|
| 1122 | + |
|
| 1123 | + return $counter; |
|
| 1124 | + } |
|
| 1125 | + |
|
| 1126 | + /** |
|
| 1127 | + * @param array $searchResults |
|
| 1128 | + * @return int |
|
| 1129 | + */ |
|
| 1130 | + private function countEntriesInSearchResults($searchResults) { |
|
| 1131 | + $counter = 0; |
|
| 1132 | + |
|
| 1133 | + foreach($searchResults as $res) { |
|
| 1134 | + $count = intval($this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res)); |
|
| 1135 | + $counter += $count; |
|
| 1136 | + } |
|
| 1137 | + |
|
| 1138 | + return $counter; |
|
| 1139 | + } |
|
| 1140 | + |
|
| 1141 | + /** |
|
| 1142 | + * Executes an LDAP search |
|
| 1143 | + * @param string $filter the LDAP filter for the search |
|
| 1144 | + * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1145 | + * @param string|string[] $attr optional, array, one or more attributes that shall be |
|
| 1146 | + * @param int $limit |
|
| 1147 | + * @param int $offset |
|
| 1148 | + * @param bool $skipHandling |
|
| 1149 | + * @return array with the search result |
|
| 1150 | + */ |
|
| 1151 | + public function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
|
| 1152 | + $limitPerPage = intval($this->connection->ldapPagingSize); |
|
| 1153 | + if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1154 | + $limitPerPage = $limit; |
|
| 1155 | + } |
|
| 1156 | + |
|
| 1157 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1158 | 1158 | * As we can have pages with zero results and/or pages with less |
| 1159 | 1159 | * than $limit results but with a still valid server 'cookie', |
| 1160 | 1160 | * loops through until we get $continue equals true and |
| 1161 | 1161 | * $findings['count'] < $limit |
| 1162 | 1162 | */ |
| 1163 | - $findings = array(); |
|
| 1164 | - $savedoffset = $offset; |
|
| 1165 | - do { |
|
| 1166 | - $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
|
| 1167 | - if($search === false) { |
|
| 1168 | - return array(); |
|
| 1169 | - } |
|
| 1170 | - list($sr, $pagedSearchOK) = $search; |
|
| 1171 | - $cr = $this->connection->getConnectionResource(); |
|
| 1172 | - |
|
| 1173 | - if($skipHandling) { |
|
| 1174 | - //i.e. result do not need to be fetched, we just need the cookie |
|
| 1175 | - //thus pass 1 or any other value as $iFoundItems because it is not |
|
| 1176 | - //used |
|
| 1177 | - $this->processPagedSearchStatus($sr, $filter, $base, 1, $limitPerPage, |
|
| 1178 | - $offset, $pagedSearchOK, |
|
| 1179 | - $skipHandling); |
|
| 1180 | - return array(); |
|
| 1181 | - } |
|
| 1182 | - |
|
| 1183 | - $iFoundItems = 0; |
|
| 1184 | - foreach($sr as $res) { |
|
| 1185 | - $findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $res)); |
|
| 1186 | - $iFoundItems = max($iFoundItems, $findings['count']); |
|
| 1187 | - unset($findings['count']); |
|
| 1188 | - } |
|
| 1189 | - |
|
| 1190 | - $continue = $this->processPagedSearchStatus($sr, $filter, $base, $iFoundItems, |
|
| 1191 | - $limitPerPage, $offset, $pagedSearchOK, |
|
| 1192 | - $skipHandling); |
|
| 1193 | - $offset += $limitPerPage; |
|
| 1194 | - } while ($continue && $pagedSearchOK && ($limit === null || count($findings) < $limit)); |
|
| 1195 | - // reseting offset |
|
| 1196 | - $offset = $savedoffset; |
|
| 1197 | - |
|
| 1198 | - // if we're here, probably no connection resource is returned. |
|
| 1199 | - // to make Nextcloud behave nicely, we simply give back an empty array. |
|
| 1200 | - if(is_null($findings)) { |
|
| 1201 | - return array(); |
|
| 1202 | - } |
|
| 1203 | - |
|
| 1204 | - if(!is_null($attr)) { |
|
| 1205 | - $selection = array(); |
|
| 1206 | - $i = 0; |
|
| 1207 | - foreach($findings as $item) { |
|
| 1208 | - if(!is_array($item)) { |
|
| 1209 | - continue; |
|
| 1210 | - } |
|
| 1211 | - $item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8'); |
|
| 1212 | - foreach($attr as $key) { |
|
| 1213 | - $key = mb_strtolower($key, 'UTF-8'); |
|
| 1214 | - if(isset($item[$key])) { |
|
| 1215 | - if(is_array($item[$key]) && isset($item[$key]['count'])) { |
|
| 1216 | - unset($item[$key]['count']); |
|
| 1217 | - } |
|
| 1218 | - if($key !== 'dn') { |
|
| 1219 | - $selection[$i][$key] = $this->resemblesDN($key) ? |
|
| 1220 | - $this->helper->sanitizeDN($item[$key]) |
|
| 1221 | - : $item[$key]; |
|
| 1222 | - } else { |
|
| 1223 | - $selection[$i][$key] = [$this->helper->sanitizeDN($item[$key])]; |
|
| 1224 | - } |
|
| 1225 | - } |
|
| 1226 | - |
|
| 1227 | - } |
|
| 1228 | - $i++; |
|
| 1229 | - } |
|
| 1230 | - $findings = $selection; |
|
| 1231 | - } |
|
| 1232 | - //we slice the findings, when |
|
| 1233 | - //a) paged search unsuccessful, though attempted |
|
| 1234 | - //b) no paged search, but limit set |
|
| 1235 | - if((!$this->getPagedSearchResultState() |
|
| 1236 | - && $pagedSearchOK) |
|
| 1237 | - || ( |
|
| 1238 | - !$pagedSearchOK |
|
| 1239 | - && !is_null($limit) |
|
| 1240 | - ) |
|
| 1241 | - ) { |
|
| 1242 | - $findings = array_slice($findings, intval($offset), $limit); |
|
| 1243 | - } |
|
| 1244 | - return $findings; |
|
| 1245 | - } |
|
| 1246 | - |
|
| 1247 | - /** |
|
| 1248 | - * @param string $name |
|
| 1249 | - * @return bool|mixed|string |
|
| 1250 | - */ |
|
| 1251 | - public function sanitizeUsername($name) { |
|
| 1252 | - if($this->connection->ldapIgnoreNamingRules) { |
|
| 1253 | - return $name; |
|
| 1254 | - } |
|
| 1255 | - |
|
| 1256 | - // Transliteration |
|
| 1257 | - // latin characters to ASCII |
|
| 1258 | - $name = iconv('UTF-8', 'ASCII//TRANSLIT', $name); |
|
| 1259 | - |
|
| 1260 | - // Replacements |
|
| 1261 | - $name = str_replace(' ', '_', $name); |
|
| 1262 | - |
|
| 1263 | - // Every remaining disallowed characters will be removed |
|
| 1264 | - $name = preg_replace('/[^a-zA-Z0-9_.@-]/u', '', $name); |
|
| 1265 | - |
|
| 1266 | - return $name; |
|
| 1267 | - } |
|
| 1268 | - |
|
| 1269 | - /** |
|
| 1270 | - * escapes (user provided) parts for LDAP filter |
|
| 1271 | - * @param string $input, the provided value |
|
| 1272 | - * @param bool $allowAsterisk whether in * at the beginning should be preserved |
|
| 1273 | - * @return string the escaped string |
|
| 1274 | - */ |
|
| 1275 | - public function escapeFilterPart($input, $allowAsterisk = false) { |
|
| 1276 | - $asterisk = ''; |
|
| 1277 | - if($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { |
|
| 1278 | - $asterisk = '*'; |
|
| 1279 | - $input = mb_substr($input, 1, null, 'UTF-8'); |
|
| 1280 | - } |
|
| 1281 | - $search = array('*', '\\', '(', ')'); |
|
| 1282 | - $replace = array('\\*', '\\\\', '\\(', '\\)'); |
|
| 1283 | - return $asterisk . str_replace($search, $replace, $input); |
|
| 1284 | - } |
|
| 1285 | - |
|
| 1286 | - /** |
|
| 1287 | - * combines the input filters with AND |
|
| 1288 | - * @param string[] $filters the filters to connect |
|
| 1289 | - * @return string the combined filter |
|
| 1290 | - */ |
|
| 1291 | - public function combineFilterWithAnd($filters) { |
|
| 1292 | - return $this->combineFilter($filters, '&'); |
|
| 1293 | - } |
|
| 1294 | - |
|
| 1295 | - /** |
|
| 1296 | - * combines the input filters with OR |
|
| 1297 | - * @param string[] $filters the filters to connect |
|
| 1298 | - * @return string the combined filter |
|
| 1299 | - * Combines Filter arguments with OR |
|
| 1300 | - */ |
|
| 1301 | - public function combineFilterWithOr($filters) { |
|
| 1302 | - return $this->combineFilter($filters, '|'); |
|
| 1303 | - } |
|
| 1304 | - |
|
| 1305 | - /** |
|
| 1306 | - * combines the input filters with given operator |
|
| 1307 | - * @param string[] $filters the filters to connect |
|
| 1308 | - * @param string $operator either & or | |
|
| 1309 | - * @return string the combined filter |
|
| 1310 | - */ |
|
| 1311 | - private function combineFilter($filters, $operator) { |
|
| 1312 | - $combinedFilter = '('.$operator; |
|
| 1313 | - foreach($filters as $filter) { |
|
| 1314 | - if ($filter !== '' && $filter[0] !== '(') { |
|
| 1315 | - $filter = '('.$filter.')'; |
|
| 1316 | - } |
|
| 1317 | - $combinedFilter.=$filter; |
|
| 1318 | - } |
|
| 1319 | - $combinedFilter.=')'; |
|
| 1320 | - return $combinedFilter; |
|
| 1321 | - } |
|
| 1322 | - |
|
| 1323 | - /** |
|
| 1324 | - * creates a filter part for to perform search for users |
|
| 1325 | - * @param string $search the search term |
|
| 1326 | - * @return string the final filter part to use in LDAP searches |
|
| 1327 | - */ |
|
| 1328 | - public function getFilterPartForUserSearch($search) { |
|
| 1329 | - return $this->getFilterPartForSearch($search, |
|
| 1330 | - $this->connection->ldapAttributesForUserSearch, |
|
| 1331 | - $this->connection->ldapUserDisplayName); |
|
| 1332 | - } |
|
| 1333 | - |
|
| 1334 | - /** |
|
| 1335 | - * creates a filter part for to perform search for groups |
|
| 1336 | - * @param string $search the search term |
|
| 1337 | - * @return string the final filter part to use in LDAP searches |
|
| 1338 | - */ |
|
| 1339 | - public function getFilterPartForGroupSearch($search) { |
|
| 1340 | - return $this->getFilterPartForSearch($search, |
|
| 1341 | - $this->connection->ldapAttributesForGroupSearch, |
|
| 1342 | - $this->connection->ldapGroupDisplayName); |
|
| 1343 | - } |
|
| 1344 | - |
|
| 1345 | - /** |
|
| 1346 | - * creates a filter part for searches by splitting up the given search |
|
| 1347 | - * string into single words |
|
| 1348 | - * @param string $search the search term |
|
| 1349 | - * @param string[] $searchAttributes needs to have at least two attributes, |
|
| 1350 | - * otherwise it does not make sense :) |
|
| 1351 | - * @return string the final filter part to use in LDAP searches |
|
| 1352 | - * @throws \Exception |
|
| 1353 | - */ |
|
| 1354 | - private function getAdvancedFilterPartForSearch($search, $searchAttributes) { |
|
| 1355 | - if(!is_array($searchAttributes) || count($searchAttributes) < 2) { |
|
| 1356 | - throw new \Exception('searchAttributes must be an array with at least two string'); |
|
| 1357 | - } |
|
| 1358 | - $searchWords = explode(' ', trim($search)); |
|
| 1359 | - $wordFilters = array(); |
|
| 1360 | - foreach($searchWords as $word) { |
|
| 1361 | - $word = $this->prepareSearchTerm($word); |
|
| 1362 | - //every word needs to appear at least once |
|
| 1363 | - $wordMatchOneAttrFilters = array(); |
|
| 1364 | - foreach($searchAttributes as $attr) { |
|
| 1365 | - $wordMatchOneAttrFilters[] = $attr . '=' . $word; |
|
| 1366 | - } |
|
| 1367 | - $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters); |
|
| 1368 | - } |
|
| 1369 | - return $this->combineFilterWithAnd($wordFilters); |
|
| 1370 | - } |
|
| 1371 | - |
|
| 1372 | - /** |
|
| 1373 | - * creates a filter part for searches |
|
| 1374 | - * @param string $search the search term |
|
| 1375 | - * @param string[]|null $searchAttributes |
|
| 1376 | - * @param string $fallbackAttribute a fallback attribute in case the user |
|
| 1377 | - * did not define search attributes. Typically the display name attribute. |
|
| 1378 | - * @return string the final filter part to use in LDAP searches |
|
| 1379 | - */ |
|
| 1380 | - private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) { |
|
| 1381 | - $filter = array(); |
|
| 1382 | - $haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0); |
|
| 1383 | - if($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { |
|
| 1384 | - try { |
|
| 1385 | - return $this->getAdvancedFilterPartForSearch($search, $searchAttributes); |
|
| 1386 | - } catch(\Exception $e) { |
|
| 1387 | - \OCP\Util::writeLog( |
|
| 1388 | - 'user_ldap', |
|
| 1389 | - 'Creating advanced filter for search failed, falling back to simple method.', |
|
| 1390 | - \OCP\Util::INFO |
|
| 1391 | - ); |
|
| 1392 | - } |
|
| 1393 | - } |
|
| 1394 | - |
|
| 1395 | - $search = $this->prepareSearchTerm($search); |
|
| 1396 | - if(!is_array($searchAttributes) || count($searchAttributes) === 0) { |
|
| 1397 | - if ($fallbackAttribute === '') { |
|
| 1398 | - return ''; |
|
| 1399 | - } |
|
| 1400 | - $filter[] = $fallbackAttribute . '=' . $search; |
|
| 1401 | - } else { |
|
| 1402 | - foreach($searchAttributes as $attribute) { |
|
| 1403 | - $filter[] = $attribute . '=' . $search; |
|
| 1404 | - } |
|
| 1405 | - } |
|
| 1406 | - if(count($filter) === 1) { |
|
| 1407 | - return '('.$filter[0].')'; |
|
| 1408 | - } |
|
| 1409 | - return $this->combineFilterWithOr($filter); |
|
| 1410 | - } |
|
| 1411 | - |
|
| 1412 | - /** |
|
| 1413 | - * returns the search term depending on whether we are allowed |
|
| 1414 | - * list users found by ldap with the current input appended by |
|
| 1415 | - * a * |
|
| 1416 | - * @return string |
|
| 1417 | - */ |
|
| 1418 | - private function prepareSearchTerm($term) { |
|
| 1419 | - $config = \OC::$server->getConfig(); |
|
| 1420 | - |
|
| 1421 | - $allowEnum = $config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes'); |
|
| 1422 | - |
|
| 1423 | - $result = $term; |
|
| 1424 | - if ($term === '') { |
|
| 1425 | - $result = '*'; |
|
| 1426 | - } else if ($allowEnum !== 'no') { |
|
| 1427 | - $result = $term . '*'; |
|
| 1428 | - } |
|
| 1429 | - return $result; |
|
| 1430 | - } |
|
| 1431 | - |
|
| 1432 | - /** |
|
| 1433 | - * returns the filter used for counting users |
|
| 1434 | - * @return string |
|
| 1435 | - */ |
|
| 1436 | - public function getFilterForUserCount() { |
|
| 1437 | - $filter = $this->combineFilterWithAnd(array( |
|
| 1438 | - $this->connection->ldapUserFilter, |
|
| 1439 | - $this->connection->ldapUserDisplayName . '=*' |
|
| 1440 | - )); |
|
| 1441 | - |
|
| 1442 | - return $filter; |
|
| 1443 | - } |
|
| 1444 | - |
|
| 1445 | - /** |
|
| 1446 | - * @param string $name |
|
| 1447 | - * @param string $password |
|
| 1448 | - * @return bool |
|
| 1449 | - */ |
|
| 1450 | - public function areCredentialsValid($name, $password) { |
|
| 1451 | - $name = $this->helper->DNasBaseParameter($name); |
|
| 1452 | - $testConnection = clone $this->connection; |
|
| 1453 | - $credentials = array( |
|
| 1454 | - 'ldapAgentName' => $name, |
|
| 1455 | - 'ldapAgentPassword' => $password |
|
| 1456 | - ); |
|
| 1457 | - if(!$testConnection->setConfiguration($credentials)) { |
|
| 1458 | - return false; |
|
| 1459 | - } |
|
| 1460 | - return $testConnection->bind(); |
|
| 1461 | - } |
|
| 1462 | - |
|
| 1463 | - /** |
|
| 1464 | - * reverse lookup of a DN given a known UUID |
|
| 1465 | - * |
|
| 1466 | - * @param string $uuid |
|
| 1467 | - * @return string |
|
| 1468 | - * @throws \Exception |
|
| 1469 | - */ |
|
| 1470 | - public function getUserDnByUuid($uuid) { |
|
| 1471 | - $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1472 | - $filter = $this->connection->ldapUserFilter; |
|
| 1473 | - $base = $this->connection->ldapBaseUsers; |
|
| 1474 | - |
|
| 1475 | - if ($this->connection->ldapUuidUserAttribute === 'auto' && $uuidOverride === '') { |
|
| 1476 | - // Sacrebleu! The UUID attribute is unknown :( We need first an |
|
| 1477 | - // existing DN to be able to reliably detect it. |
|
| 1478 | - $result = $this->search($filter, $base, ['dn'], 1); |
|
| 1479 | - if(!isset($result[0]) || !isset($result[0]['dn'])) { |
|
| 1480 | - throw new \Exception('Cannot determine UUID attribute'); |
|
| 1481 | - } |
|
| 1482 | - $dn = $result[0]['dn'][0]; |
|
| 1483 | - if(!$this->detectUuidAttribute($dn, true)) { |
|
| 1484 | - throw new \Exception('Cannot determine UUID attribute'); |
|
| 1485 | - } |
|
| 1486 | - } else { |
|
| 1487 | - // The UUID attribute is either known or an override is given. |
|
| 1488 | - // By calling this method we ensure that $this->connection->$uuidAttr |
|
| 1489 | - // is definitely set |
|
| 1490 | - if(!$this->detectUuidAttribute('', true)) { |
|
| 1491 | - throw new \Exception('Cannot determine UUID attribute'); |
|
| 1492 | - } |
|
| 1493 | - } |
|
| 1494 | - |
|
| 1495 | - $uuidAttr = $this->connection->ldapUuidUserAttribute; |
|
| 1496 | - if($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { |
|
| 1497 | - $uuid = $this->formatGuid2ForFilterUser($uuid); |
|
| 1498 | - } |
|
| 1499 | - |
|
| 1500 | - $filter = $uuidAttr . '=' . $uuid; |
|
| 1501 | - $result = $this->searchUsers($filter, ['dn'], 2); |
|
| 1502 | - if(is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { |
|
| 1503 | - // we put the count into account to make sure that this is |
|
| 1504 | - // really unique |
|
| 1505 | - return $result[0]['dn'][0]; |
|
| 1506 | - } |
|
| 1507 | - |
|
| 1508 | - throw new \Exception('Cannot determine UUID attribute'); |
|
| 1509 | - } |
|
| 1510 | - |
|
| 1511 | - /** |
|
| 1512 | - * auto-detects the directory's UUID attribute |
|
| 1513 | - * @param string $dn a known DN used to check against |
|
| 1514 | - * @param bool $isUser |
|
| 1515 | - * @param bool $force the detection should be run, even if it is not set to auto |
|
| 1516 | - * @return bool true on success, false otherwise |
|
| 1517 | - */ |
|
| 1518 | - private function detectUuidAttribute($dn, $isUser = true, $force = false) { |
|
| 1519 | - if($isUser) { |
|
| 1520 | - $uuidAttr = 'ldapUuidUserAttribute'; |
|
| 1521 | - $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1522 | - } else { |
|
| 1523 | - $uuidAttr = 'ldapUuidGroupAttribute'; |
|
| 1524 | - $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
|
| 1525 | - } |
|
| 1526 | - |
|
| 1527 | - if(($this->connection->$uuidAttr !== 'auto') && !$force) { |
|
| 1528 | - return true; |
|
| 1529 | - } |
|
| 1530 | - |
|
| 1531 | - if (is_string($uuidOverride) && trim($uuidOverride) !== '' && !$force) { |
|
| 1532 | - $this->connection->$uuidAttr = $uuidOverride; |
|
| 1533 | - return true; |
|
| 1534 | - } |
|
| 1535 | - |
|
| 1536 | - // for now, supported attributes are entryUUID, nsuniqueid, objectGUID, ipaUniqueID |
|
| 1537 | - $testAttributes = array('entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid'); |
|
| 1538 | - |
|
| 1539 | - foreach($testAttributes as $attribute) { |
|
| 1540 | - $value = $this->readAttribute($dn, $attribute); |
|
| 1541 | - if(is_array($value) && isset($value[0]) && !empty($value[0])) { |
|
| 1542 | - \OCP\Util::writeLog('user_ldap', |
|
| 1543 | - 'Setting '.$attribute.' as '.$uuidAttr, |
|
| 1544 | - \OCP\Util::DEBUG); |
|
| 1545 | - $this->connection->$uuidAttr = $attribute; |
|
| 1546 | - return true; |
|
| 1547 | - } |
|
| 1548 | - } |
|
| 1549 | - \OCP\Util::writeLog('user_ldap', |
|
| 1550 | - 'Could not autodetect the UUID attribute', |
|
| 1551 | - \OCP\Util::ERROR); |
|
| 1552 | - |
|
| 1553 | - return false; |
|
| 1554 | - } |
|
| 1555 | - |
|
| 1556 | - /** |
|
| 1557 | - * @param string $dn |
|
| 1558 | - * @param bool $isUser |
|
| 1559 | - * @return string|bool |
|
| 1560 | - */ |
|
| 1561 | - public function getUUID($dn, $isUser = true) { |
|
| 1562 | - if($isUser) { |
|
| 1563 | - $uuidAttr = 'ldapUuidUserAttribute'; |
|
| 1564 | - $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1565 | - } else { |
|
| 1566 | - $uuidAttr = 'ldapUuidGroupAttribute'; |
|
| 1567 | - $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
|
| 1568 | - } |
|
| 1569 | - |
|
| 1570 | - $uuid = false; |
|
| 1571 | - if($this->detectUuidAttribute($dn, $isUser)) { |
|
| 1572 | - $uuid = $this->readAttribute($dn, $this->connection->$uuidAttr); |
|
| 1573 | - if( !is_array($uuid) |
|
| 1574 | - && $uuidOverride !== '' |
|
| 1575 | - && $this->detectUuidAttribute($dn, $isUser, true)) { |
|
| 1576 | - $uuid = $this->readAttribute($dn, |
|
| 1577 | - $this->connection->$uuidAttr); |
|
| 1578 | - } |
|
| 1579 | - if(is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { |
|
| 1580 | - $uuid = $uuid[0]; |
|
| 1581 | - } |
|
| 1582 | - } |
|
| 1583 | - |
|
| 1584 | - return $uuid; |
|
| 1585 | - } |
|
| 1586 | - |
|
| 1587 | - /** |
|
| 1588 | - * converts a binary ObjectGUID into a string representation |
|
| 1589 | - * @param string $oguid the ObjectGUID in it's binary form as retrieved from AD |
|
| 1590 | - * @return string |
|
| 1591 | - * @link http://www.php.net/manual/en/function.ldap-get-values-len.php#73198 |
|
| 1592 | - */ |
|
| 1593 | - private function convertObjectGUID2Str($oguid) { |
|
| 1594 | - $hex_guid = bin2hex($oguid); |
|
| 1595 | - $hex_guid_to_guid_str = ''; |
|
| 1596 | - for($k = 1; $k <= 4; ++$k) { |
|
| 1597 | - $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2); |
|
| 1598 | - } |
|
| 1599 | - $hex_guid_to_guid_str .= '-'; |
|
| 1600 | - for($k = 1; $k <= 2; ++$k) { |
|
| 1601 | - $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2); |
|
| 1602 | - } |
|
| 1603 | - $hex_guid_to_guid_str .= '-'; |
|
| 1604 | - for($k = 1; $k <= 2; ++$k) { |
|
| 1605 | - $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2); |
|
| 1606 | - } |
|
| 1607 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4); |
|
| 1608 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20); |
|
| 1609 | - |
|
| 1610 | - return strtoupper($hex_guid_to_guid_str); |
|
| 1611 | - } |
|
| 1612 | - |
|
| 1613 | - /** |
|
| 1614 | - * the first three blocks of the string-converted GUID happen to be in |
|
| 1615 | - * reverse order. In order to use it in a filter, this needs to be |
|
| 1616 | - * corrected. Furthermore the dashes need to be replaced and \\ preprended |
|
| 1617 | - * to every two hax figures. |
|
| 1618 | - * |
|
| 1619 | - * If an invalid string is passed, it will be returned without change. |
|
| 1620 | - * |
|
| 1621 | - * @param string $guid |
|
| 1622 | - * @return string |
|
| 1623 | - */ |
|
| 1624 | - public function formatGuid2ForFilterUser($guid) { |
|
| 1625 | - if(!is_string($guid)) { |
|
| 1626 | - throw new \InvalidArgumentException('String expected'); |
|
| 1627 | - } |
|
| 1628 | - $blocks = explode('-', $guid); |
|
| 1629 | - if(count($blocks) !== 5) { |
|
| 1630 | - /* |
|
| 1163 | + $findings = array(); |
|
| 1164 | + $savedoffset = $offset; |
|
| 1165 | + do { |
|
| 1166 | + $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
|
| 1167 | + if($search === false) { |
|
| 1168 | + return array(); |
|
| 1169 | + } |
|
| 1170 | + list($sr, $pagedSearchOK) = $search; |
|
| 1171 | + $cr = $this->connection->getConnectionResource(); |
|
| 1172 | + |
|
| 1173 | + if($skipHandling) { |
|
| 1174 | + //i.e. result do not need to be fetched, we just need the cookie |
|
| 1175 | + //thus pass 1 or any other value as $iFoundItems because it is not |
|
| 1176 | + //used |
|
| 1177 | + $this->processPagedSearchStatus($sr, $filter, $base, 1, $limitPerPage, |
|
| 1178 | + $offset, $pagedSearchOK, |
|
| 1179 | + $skipHandling); |
|
| 1180 | + return array(); |
|
| 1181 | + } |
|
| 1182 | + |
|
| 1183 | + $iFoundItems = 0; |
|
| 1184 | + foreach($sr as $res) { |
|
| 1185 | + $findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $res)); |
|
| 1186 | + $iFoundItems = max($iFoundItems, $findings['count']); |
|
| 1187 | + unset($findings['count']); |
|
| 1188 | + } |
|
| 1189 | + |
|
| 1190 | + $continue = $this->processPagedSearchStatus($sr, $filter, $base, $iFoundItems, |
|
| 1191 | + $limitPerPage, $offset, $pagedSearchOK, |
|
| 1192 | + $skipHandling); |
|
| 1193 | + $offset += $limitPerPage; |
|
| 1194 | + } while ($continue && $pagedSearchOK && ($limit === null || count($findings) < $limit)); |
|
| 1195 | + // reseting offset |
|
| 1196 | + $offset = $savedoffset; |
|
| 1197 | + |
|
| 1198 | + // if we're here, probably no connection resource is returned. |
|
| 1199 | + // to make Nextcloud behave nicely, we simply give back an empty array. |
|
| 1200 | + if(is_null($findings)) { |
|
| 1201 | + return array(); |
|
| 1202 | + } |
|
| 1203 | + |
|
| 1204 | + if(!is_null($attr)) { |
|
| 1205 | + $selection = array(); |
|
| 1206 | + $i = 0; |
|
| 1207 | + foreach($findings as $item) { |
|
| 1208 | + if(!is_array($item)) { |
|
| 1209 | + continue; |
|
| 1210 | + } |
|
| 1211 | + $item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8'); |
|
| 1212 | + foreach($attr as $key) { |
|
| 1213 | + $key = mb_strtolower($key, 'UTF-8'); |
|
| 1214 | + if(isset($item[$key])) { |
|
| 1215 | + if(is_array($item[$key]) && isset($item[$key]['count'])) { |
|
| 1216 | + unset($item[$key]['count']); |
|
| 1217 | + } |
|
| 1218 | + if($key !== 'dn') { |
|
| 1219 | + $selection[$i][$key] = $this->resemblesDN($key) ? |
|
| 1220 | + $this->helper->sanitizeDN($item[$key]) |
|
| 1221 | + : $item[$key]; |
|
| 1222 | + } else { |
|
| 1223 | + $selection[$i][$key] = [$this->helper->sanitizeDN($item[$key])]; |
|
| 1224 | + } |
|
| 1225 | + } |
|
| 1226 | + |
|
| 1227 | + } |
|
| 1228 | + $i++; |
|
| 1229 | + } |
|
| 1230 | + $findings = $selection; |
|
| 1231 | + } |
|
| 1232 | + //we slice the findings, when |
|
| 1233 | + //a) paged search unsuccessful, though attempted |
|
| 1234 | + //b) no paged search, but limit set |
|
| 1235 | + if((!$this->getPagedSearchResultState() |
|
| 1236 | + && $pagedSearchOK) |
|
| 1237 | + || ( |
|
| 1238 | + !$pagedSearchOK |
|
| 1239 | + && !is_null($limit) |
|
| 1240 | + ) |
|
| 1241 | + ) { |
|
| 1242 | + $findings = array_slice($findings, intval($offset), $limit); |
|
| 1243 | + } |
|
| 1244 | + return $findings; |
|
| 1245 | + } |
|
| 1246 | + |
|
| 1247 | + /** |
|
| 1248 | + * @param string $name |
|
| 1249 | + * @return bool|mixed|string |
|
| 1250 | + */ |
|
| 1251 | + public function sanitizeUsername($name) { |
|
| 1252 | + if($this->connection->ldapIgnoreNamingRules) { |
|
| 1253 | + return $name; |
|
| 1254 | + } |
|
| 1255 | + |
|
| 1256 | + // Transliteration |
|
| 1257 | + // latin characters to ASCII |
|
| 1258 | + $name = iconv('UTF-8', 'ASCII//TRANSLIT', $name); |
|
| 1259 | + |
|
| 1260 | + // Replacements |
|
| 1261 | + $name = str_replace(' ', '_', $name); |
|
| 1262 | + |
|
| 1263 | + // Every remaining disallowed characters will be removed |
|
| 1264 | + $name = preg_replace('/[^a-zA-Z0-9_.@-]/u', '', $name); |
|
| 1265 | + |
|
| 1266 | + return $name; |
|
| 1267 | + } |
|
| 1268 | + |
|
| 1269 | + /** |
|
| 1270 | + * escapes (user provided) parts for LDAP filter |
|
| 1271 | + * @param string $input, the provided value |
|
| 1272 | + * @param bool $allowAsterisk whether in * at the beginning should be preserved |
|
| 1273 | + * @return string the escaped string |
|
| 1274 | + */ |
|
| 1275 | + public function escapeFilterPart($input, $allowAsterisk = false) { |
|
| 1276 | + $asterisk = ''; |
|
| 1277 | + if($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { |
|
| 1278 | + $asterisk = '*'; |
|
| 1279 | + $input = mb_substr($input, 1, null, 'UTF-8'); |
|
| 1280 | + } |
|
| 1281 | + $search = array('*', '\\', '(', ')'); |
|
| 1282 | + $replace = array('\\*', '\\\\', '\\(', '\\)'); |
|
| 1283 | + return $asterisk . str_replace($search, $replace, $input); |
|
| 1284 | + } |
|
| 1285 | + |
|
| 1286 | + /** |
|
| 1287 | + * combines the input filters with AND |
|
| 1288 | + * @param string[] $filters the filters to connect |
|
| 1289 | + * @return string the combined filter |
|
| 1290 | + */ |
|
| 1291 | + public function combineFilterWithAnd($filters) { |
|
| 1292 | + return $this->combineFilter($filters, '&'); |
|
| 1293 | + } |
|
| 1294 | + |
|
| 1295 | + /** |
|
| 1296 | + * combines the input filters with OR |
|
| 1297 | + * @param string[] $filters the filters to connect |
|
| 1298 | + * @return string the combined filter |
|
| 1299 | + * Combines Filter arguments with OR |
|
| 1300 | + */ |
|
| 1301 | + public function combineFilterWithOr($filters) { |
|
| 1302 | + return $this->combineFilter($filters, '|'); |
|
| 1303 | + } |
|
| 1304 | + |
|
| 1305 | + /** |
|
| 1306 | + * combines the input filters with given operator |
|
| 1307 | + * @param string[] $filters the filters to connect |
|
| 1308 | + * @param string $operator either & or | |
|
| 1309 | + * @return string the combined filter |
|
| 1310 | + */ |
|
| 1311 | + private function combineFilter($filters, $operator) { |
|
| 1312 | + $combinedFilter = '('.$operator; |
|
| 1313 | + foreach($filters as $filter) { |
|
| 1314 | + if ($filter !== '' && $filter[0] !== '(') { |
|
| 1315 | + $filter = '('.$filter.')'; |
|
| 1316 | + } |
|
| 1317 | + $combinedFilter.=$filter; |
|
| 1318 | + } |
|
| 1319 | + $combinedFilter.=')'; |
|
| 1320 | + return $combinedFilter; |
|
| 1321 | + } |
|
| 1322 | + |
|
| 1323 | + /** |
|
| 1324 | + * creates a filter part for to perform search for users |
|
| 1325 | + * @param string $search the search term |
|
| 1326 | + * @return string the final filter part to use in LDAP searches |
|
| 1327 | + */ |
|
| 1328 | + public function getFilterPartForUserSearch($search) { |
|
| 1329 | + return $this->getFilterPartForSearch($search, |
|
| 1330 | + $this->connection->ldapAttributesForUserSearch, |
|
| 1331 | + $this->connection->ldapUserDisplayName); |
|
| 1332 | + } |
|
| 1333 | + |
|
| 1334 | + /** |
|
| 1335 | + * creates a filter part for to perform search for groups |
|
| 1336 | + * @param string $search the search term |
|
| 1337 | + * @return string the final filter part to use in LDAP searches |
|
| 1338 | + */ |
|
| 1339 | + public function getFilterPartForGroupSearch($search) { |
|
| 1340 | + return $this->getFilterPartForSearch($search, |
|
| 1341 | + $this->connection->ldapAttributesForGroupSearch, |
|
| 1342 | + $this->connection->ldapGroupDisplayName); |
|
| 1343 | + } |
|
| 1344 | + |
|
| 1345 | + /** |
|
| 1346 | + * creates a filter part for searches by splitting up the given search |
|
| 1347 | + * string into single words |
|
| 1348 | + * @param string $search the search term |
|
| 1349 | + * @param string[] $searchAttributes needs to have at least two attributes, |
|
| 1350 | + * otherwise it does not make sense :) |
|
| 1351 | + * @return string the final filter part to use in LDAP searches |
|
| 1352 | + * @throws \Exception |
|
| 1353 | + */ |
|
| 1354 | + private function getAdvancedFilterPartForSearch($search, $searchAttributes) { |
|
| 1355 | + if(!is_array($searchAttributes) || count($searchAttributes) < 2) { |
|
| 1356 | + throw new \Exception('searchAttributes must be an array with at least two string'); |
|
| 1357 | + } |
|
| 1358 | + $searchWords = explode(' ', trim($search)); |
|
| 1359 | + $wordFilters = array(); |
|
| 1360 | + foreach($searchWords as $word) { |
|
| 1361 | + $word = $this->prepareSearchTerm($word); |
|
| 1362 | + //every word needs to appear at least once |
|
| 1363 | + $wordMatchOneAttrFilters = array(); |
|
| 1364 | + foreach($searchAttributes as $attr) { |
|
| 1365 | + $wordMatchOneAttrFilters[] = $attr . '=' . $word; |
|
| 1366 | + } |
|
| 1367 | + $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters); |
|
| 1368 | + } |
|
| 1369 | + return $this->combineFilterWithAnd($wordFilters); |
|
| 1370 | + } |
|
| 1371 | + |
|
| 1372 | + /** |
|
| 1373 | + * creates a filter part for searches |
|
| 1374 | + * @param string $search the search term |
|
| 1375 | + * @param string[]|null $searchAttributes |
|
| 1376 | + * @param string $fallbackAttribute a fallback attribute in case the user |
|
| 1377 | + * did not define search attributes. Typically the display name attribute. |
|
| 1378 | + * @return string the final filter part to use in LDAP searches |
|
| 1379 | + */ |
|
| 1380 | + private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) { |
|
| 1381 | + $filter = array(); |
|
| 1382 | + $haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0); |
|
| 1383 | + if($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { |
|
| 1384 | + try { |
|
| 1385 | + return $this->getAdvancedFilterPartForSearch($search, $searchAttributes); |
|
| 1386 | + } catch(\Exception $e) { |
|
| 1387 | + \OCP\Util::writeLog( |
|
| 1388 | + 'user_ldap', |
|
| 1389 | + 'Creating advanced filter for search failed, falling back to simple method.', |
|
| 1390 | + \OCP\Util::INFO |
|
| 1391 | + ); |
|
| 1392 | + } |
|
| 1393 | + } |
|
| 1394 | + |
|
| 1395 | + $search = $this->prepareSearchTerm($search); |
|
| 1396 | + if(!is_array($searchAttributes) || count($searchAttributes) === 0) { |
|
| 1397 | + if ($fallbackAttribute === '') { |
|
| 1398 | + return ''; |
|
| 1399 | + } |
|
| 1400 | + $filter[] = $fallbackAttribute . '=' . $search; |
|
| 1401 | + } else { |
|
| 1402 | + foreach($searchAttributes as $attribute) { |
|
| 1403 | + $filter[] = $attribute . '=' . $search; |
|
| 1404 | + } |
|
| 1405 | + } |
|
| 1406 | + if(count($filter) === 1) { |
|
| 1407 | + return '('.$filter[0].')'; |
|
| 1408 | + } |
|
| 1409 | + return $this->combineFilterWithOr($filter); |
|
| 1410 | + } |
|
| 1411 | + |
|
| 1412 | + /** |
|
| 1413 | + * returns the search term depending on whether we are allowed |
|
| 1414 | + * list users found by ldap with the current input appended by |
|
| 1415 | + * a * |
|
| 1416 | + * @return string |
|
| 1417 | + */ |
|
| 1418 | + private function prepareSearchTerm($term) { |
|
| 1419 | + $config = \OC::$server->getConfig(); |
|
| 1420 | + |
|
| 1421 | + $allowEnum = $config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes'); |
|
| 1422 | + |
|
| 1423 | + $result = $term; |
|
| 1424 | + if ($term === '') { |
|
| 1425 | + $result = '*'; |
|
| 1426 | + } else if ($allowEnum !== 'no') { |
|
| 1427 | + $result = $term . '*'; |
|
| 1428 | + } |
|
| 1429 | + return $result; |
|
| 1430 | + } |
|
| 1431 | + |
|
| 1432 | + /** |
|
| 1433 | + * returns the filter used for counting users |
|
| 1434 | + * @return string |
|
| 1435 | + */ |
|
| 1436 | + public function getFilterForUserCount() { |
|
| 1437 | + $filter = $this->combineFilterWithAnd(array( |
|
| 1438 | + $this->connection->ldapUserFilter, |
|
| 1439 | + $this->connection->ldapUserDisplayName . '=*' |
|
| 1440 | + )); |
|
| 1441 | + |
|
| 1442 | + return $filter; |
|
| 1443 | + } |
|
| 1444 | + |
|
| 1445 | + /** |
|
| 1446 | + * @param string $name |
|
| 1447 | + * @param string $password |
|
| 1448 | + * @return bool |
|
| 1449 | + */ |
|
| 1450 | + public function areCredentialsValid($name, $password) { |
|
| 1451 | + $name = $this->helper->DNasBaseParameter($name); |
|
| 1452 | + $testConnection = clone $this->connection; |
|
| 1453 | + $credentials = array( |
|
| 1454 | + 'ldapAgentName' => $name, |
|
| 1455 | + 'ldapAgentPassword' => $password |
|
| 1456 | + ); |
|
| 1457 | + if(!$testConnection->setConfiguration($credentials)) { |
|
| 1458 | + return false; |
|
| 1459 | + } |
|
| 1460 | + return $testConnection->bind(); |
|
| 1461 | + } |
|
| 1462 | + |
|
| 1463 | + /** |
|
| 1464 | + * reverse lookup of a DN given a known UUID |
|
| 1465 | + * |
|
| 1466 | + * @param string $uuid |
|
| 1467 | + * @return string |
|
| 1468 | + * @throws \Exception |
|
| 1469 | + */ |
|
| 1470 | + public function getUserDnByUuid($uuid) { |
|
| 1471 | + $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1472 | + $filter = $this->connection->ldapUserFilter; |
|
| 1473 | + $base = $this->connection->ldapBaseUsers; |
|
| 1474 | + |
|
| 1475 | + if ($this->connection->ldapUuidUserAttribute === 'auto' && $uuidOverride === '') { |
|
| 1476 | + // Sacrebleu! The UUID attribute is unknown :( We need first an |
|
| 1477 | + // existing DN to be able to reliably detect it. |
|
| 1478 | + $result = $this->search($filter, $base, ['dn'], 1); |
|
| 1479 | + if(!isset($result[0]) || !isset($result[0]['dn'])) { |
|
| 1480 | + throw new \Exception('Cannot determine UUID attribute'); |
|
| 1481 | + } |
|
| 1482 | + $dn = $result[0]['dn'][0]; |
|
| 1483 | + if(!$this->detectUuidAttribute($dn, true)) { |
|
| 1484 | + throw new \Exception('Cannot determine UUID attribute'); |
|
| 1485 | + } |
|
| 1486 | + } else { |
|
| 1487 | + // The UUID attribute is either known or an override is given. |
|
| 1488 | + // By calling this method we ensure that $this->connection->$uuidAttr |
|
| 1489 | + // is definitely set |
|
| 1490 | + if(!$this->detectUuidAttribute('', true)) { |
|
| 1491 | + throw new \Exception('Cannot determine UUID attribute'); |
|
| 1492 | + } |
|
| 1493 | + } |
|
| 1494 | + |
|
| 1495 | + $uuidAttr = $this->connection->ldapUuidUserAttribute; |
|
| 1496 | + if($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { |
|
| 1497 | + $uuid = $this->formatGuid2ForFilterUser($uuid); |
|
| 1498 | + } |
|
| 1499 | + |
|
| 1500 | + $filter = $uuidAttr . '=' . $uuid; |
|
| 1501 | + $result = $this->searchUsers($filter, ['dn'], 2); |
|
| 1502 | + if(is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { |
|
| 1503 | + // we put the count into account to make sure that this is |
|
| 1504 | + // really unique |
|
| 1505 | + return $result[0]['dn'][0]; |
|
| 1506 | + } |
|
| 1507 | + |
|
| 1508 | + throw new \Exception('Cannot determine UUID attribute'); |
|
| 1509 | + } |
|
| 1510 | + |
|
| 1511 | + /** |
|
| 1512 | + * auto-detects the directory's UUID attribute |
|
| 1513 | + * @param string $dn a known DN used to check against |
|
| 1514 | + * @param bool $isUser |
|
| 1515 | + * @param bool $force the detection should be run, even if it is not set to auto |
|
| 1516 | + * @return bool true on success, false otherwise |
|
| 1517 | + */ |
|
| 1518 | + private function detectUuidAttribute($dn, $isUser = true, $force = false) { |
|
| 1519 | + if($isUser) { |
|
| 1520 | + $uuidAttr = 'ldapUuidUserAttribute'; |
|
| 1521 | + $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1522 | + } else { |
|
| 1523 | + $uuidAttr = 'ldapUuidGroupAttribute'; |
|
| 1524 | + $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
|
| 1525 | + } |
|
| 1526 | + |
|
| 1527 | + if(($this->connection->$uuidAttr !== 'auto') && !$force) { |
|
| 1528 | + return true; |
|
| 1529 | + } |
|
| 1530 | + |
|
| 1531 | + if (is_string($uuidOverride) && trim($uuidOverride) !== '' && !$force) { |
|
| 1532 | + $this->connection->$uuidAttr = $uuidOverride; |
|
| 1533 | + return true; |
|
| 1534 | + } |
|
| 1535 | + |
|
| 1536 | + // for now, supported attributes are entryUUID, nsuniqueid, objectGUID, ipaUniqueID |
|
| 1537 | + $testAttributes = array('entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid'); |
|
| 1538 | + |
|
| 1539 | + foreach($testAttributes as $attribute) { |
|
| 1540 | + $value = $this->readAttribute($dn, $attribute); |
|
| 1541 | + if(is_array($value) && isset($value[0]) && !empty($value[0])) { |
|
| 1542 | + \OCP\Util::writeLog('user_ldap', |
|
| 1543 | + 'Setting '.$attribute.' as '.$uuidAttr, |
|
| 1544 | + \OCP\Util::DEBUG); |
|
| 1545 | + $this->connection->$uuidAttr = $attribute; |
|
| 1546 | + return true; |
|
| 1547 | + } |
|
| 1548 | + } |
|
| 1549 | + \OCP\Util::writeLog('user_ldap', |
|
| 1550 | + 'Could not autodetect the UUID attribute', |
|
| 1551 | + \OCP\Util::ERROR); |
|
| 1552 | + |
|
| 1553 | + return false; |
|
| 1554 | + } |
|
| 1555 | + |
|
| 1556 | + /** |
|
| 1557 | + * @param string $dn |
|
| 1558 | + * @param bool $isUser |
|
| 1559 | + * @return string|bool |
|
| 1560 | + */ |
|
| 1561 | + public function getUUID($dn, $isUser = true) { |
|
| 1562 | + if($isUser) { |
|
| 1563 | + $uuidAttr = 'ldapUuidUserAttribute'; |
|
| 1564 | + $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1565 | + } else { |
|
| 1566 | + $uuidAttr = 'ldapUuidGroupAttribute'; |
|
| 1567 | + $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
|
| 1568 | + } |
|
| 1569 | + |
|
| 1570 | + $uuid = false; |
|
| 1571 | + if($this->detectUuidAttribute($dn, $isUser)) { |
|
| 1572 | + $uuid = $this->readAttribute($dn, $this->connection->$uuidAttr); |
|
| 1573 | + if( !is_array($uuid) |
|
| 1574 | + && $uuidOverride !== '' |
|
| 1575 | + && $this->detectUuidAttribute($dn, $isUser, true)) { |
|
| 1576 | + $uuid = $this->readAttribute($dn, |
|
| 1577 | + $this->connection->$uuidAttr); |
|
| 1578 | + } |
|
| 1579 | + if(is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { |
|
| 1580 | + $uuid = $uuid[0]; |
|
| 1581 | + } |
|
| 1582 | + } |
|
| 1583 | + |
|
| 1584 | + return $uuid; |
|
| 1585 | + } |
|
| 1586 | + |
|
| 1587 | + /** |
|
| 1588 | + * converts a binary ObjectGUID into a string representation |
|
| 1589 | + * @param string $oguid the ObjectGUID in it's binary form as retrieved from AD |
|
| 1590 | + * @return string |
|
| 1591 | + * @link http://www.php.net/manual/en/function.ldap-get-values-len.php#73198 |
|
| 1592 | + */ |
|
| 1593 | + private function convertObjectGUID2Str($oguid) { |
|
| 1594 | + $hex_guid = bin2hex($oguid); |
|
| 1595 | + $hex_guid_to_guid_str = ''; |
|
| 1596 | + for($k = 1; $k <= 4; ++$k) { |
|
| 1597 | + $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2); |
|
| 1598 | + } |
|
| 1599 | + $hex_guid_to_guid_str .= '-'; |
|
| 1600 | + for($k = 1; $k <= 2; ++$k) { |
|
| 1601 | + $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2); |
|
| 1602 | + } |
|
| 1603 | + $hex_guid_to_guid_str .= '-'; |
|
| 1604 | + for($k = 1; $k <= 2; ++$k) { |
|
| 1605 | + $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2); |
|
| 1606 | + } |
|
| 1607 | + $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4); |
|
| 1608 | + $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20); |
|
| 1609 | + |
|
| 1610 | + return strtoupper($hex_guid_to_guid_str); |
|
| 1611 | + } |
|
| 1612 | + |
|
| 1613 | + /** |
|
| 1614 | + * the first three blocks of the string-converted GUID happen to be in |
|
| 1615 | + * reverse order. In order to use it in a filter, this needs to be |
|
| 1616 | + * corrected. Furthermore the dashes need to be replaced and \\ preprended |
|
| 1617 | + * to every two hax figures. |
|
| 1618 | + * |
|
| 1619 | + * If an invalid string is passed, it will be returned without change. |
|
| 1620 | + * |
|
| 1621 | + * @param string $guid |
|
| 1622 | + * @return string |
|
| 1623 | + */ |
|
| 1624 | + public function formatGuid2ForFilterUser($guid) { |
|
| 1625 | + if(!is_string($guid)) { |
|
| 1626 | + throw new \InvalidArgumentException('String expected'); |
|
| 1627 | + } |
|
| 1628 | + $blocks = explode('-', $guid); |
|
| 1629 | + if(count($blocks) !== 5) { |
|
| 1630 | + /* |
|
| 1631 | 1631 | * Why not throw an Exception instead? This method is a utility |
| 1632 | 1632 | * called only when trying to figure out whether a "missing" known |
| 1633 | 1633 | * LDAP user was or was not renamed on the LDAP server. And this |
@@ -1638,275 +1638,275 @@ discard block |
||
| 1638 | 1638 | * an exception here would kill the experience for a valid, acting |
| 1639 | 1639 | * user. Instead we write a log message. |
| 1640 | 1640 | */ |
| 1641 | - \OC::$server->getLogger()->info( |
|
| 1642 | - 'Passed string does not resemble a valid GUID. Known UUID ' . |
|
| 1643 | - '({uuid}) probably does not match UUID configuration.', |
|
| 1644 | - [ 'app' => 'user_ldap', 'uuid' => $guid ] |
|
| 1645 | - ); |
|
| 1646 | - return $guid; |
|
| 1647 | - } |
|
| 1648 | - for($i=0; $i < 3; $i++) { |
|
| 1649 | - $pairs = str_split($blocks[$i], 2); |
|
| 1650 | - $pairs = array_reverse($pairs); |
|
| 1651 | - $blocks[$i] = implode('', $pairs); |
|
| 1652 | - } |
|
| 1653 | - for($i=0; $i < 5; $i++) { |
|
| 1654 | - $pairs = str_split($blocks[$i], 2); |
|
| 1655 | - $blocks[$i] = '\\' . implode('\\', $pairs); |
|
| 1656 | - } |
|
| 1657 | - return implode('', $blocks); |
|
| 1658 | - } |
|
| 1659 | - |
|
| 1660 | - /** |
|
| 1661 | - * gets a SID of the domain of the given dn |
|
| 1662 | - * @param string $dn |
|
| 1663 | - * @return string|bool |
|
| 1664 | - */ |
|
| 1665 | - public function getSID($dn) { |
|
| 1666 | - $domainDN = $this->getDomainDNFromDN($dn); |
|
| 1667 | - $cacheKey = 'getSID-'.$domainDN; |
|
| 1668 | - $sid = $this->connection->getFromCache($cacheKey); |
|
| 1669 | - if(!is_null($sid)) { |
|
| 1670 | - return $sid; |
|
| 1671 | - } |
|
| 1672 | - |
|
| 1673 | - $objectSid = $this->readAttribute($domainDN, 'objectsid'); |
|
| 1674 | - if(!is_array($objectSid) || empty($objectSid)) { |
|
| 1675 | - $this->connection->writeToCache($cacheKey, false); |
|
| 1676 | - return false; |
|
| 1677 | - } |
|
| 1678 | - $domainObjectSid = $this->convertSID2Str($objectSid[0]); |
|
| 1679 | - $this->connection->writeToCache($cacheKey, $domainObjectSid); |
|
| 1680 | - |
|
| 1681 | - return $domainObjectSid; |
|
| 1682 | - } |
|
| 1683 | - |
|
| 1684 | - /** |
|
| 1685 | - * converts a binary SID into a string representation |
|
| 1686 | - * @param string $sid |
|
| 1687 | - * @return string |
|
| 1688 | - */ |
|
| 1689 | - public function convertSID2Str($sid) { |
|
| 1690 | - // The format of a SID binary string is as follows: |
|
| 1691 | - // 1 byte for the revision level |
|
| 1692 | - // 1 byte for the number n of variable sub-ids |
|
| 1693 | - // 6 bytes for identifier authority value |
|
| 1694 | - // n*4 bytes for n sub-ids |
|
| 1695 | - // |
|
| 1696 | - // Example: 010400000000000515000000a681e50e4d6c6c2bca32055f |
|
| 1697 | - // Legend: RRNNAAAAAAAAAAAA11111111222222223333333344444444 |
|
| 1698 | - $revision = ord($sid[0]); |
|
| 1699 | - $numberSubID = ord($sid[1]); |
|
| 1700 | - |
|
| 1701 | - $subIdStart = 8; // 1 + 1 + 6 |
|
| 1702 | - $subIdLength = 4; |
|
| 1703 | - if (strlen($sid) !== $subIdStart + $subIdLength * $numberSubID) { |
|
| 1704 | - // Incorrect number of bytes present. |
|
| 1705 | - return ''; |
|
| 1706 | - } |
|
| 1707 | - |
|
| 1708 | - // 6 bytes = 48 bits can be represented using floats without loss of |
|
| 1709 | - // precision (see https://gist.github.com/bantu/886ac680b0aef5812f71) |
|
| 1710 | - $iav = number_format(hexdec(bin2hex(substr($sid, 2, 6))), 0, '', ''); |
|
| 1711 | - |
|
| 1712 | - $subIDs = array(); |
|
| 1713 | - for ($i = 0; $i < $numberSubID; $i++) { |
|
| 1714 | - $subID = unpack('V', substr($sid, $subIdStart + $subIdLength * $i, $subIdLength)); |
|
| 1715 | - $subIDs[] = sprintf('%u', $subID[1]); |
|
| 1716 | - } |
|
| 1717 | - |
|
| 1718 | - // Result for example above: S-1-5-21-249921958-728525901-1594176202 |
|
| 1719 | - return sprintf('S-%d-%s-%s', $revision, $iav, implode('-', $subIDs)); |
|
| 1720 | - } |
|
| 1721 | - |
|
| 1722 | - /** |
|
| 1723 | - * checks if the given DN is part of the given base DN(s) |
|
| 1724 | - * @param string $dn the DN |
|
| 1725 | - * @param string[] $bases array containing the allowed base DN or DNs |
|
| 1726 | - * @return bool |
|
| 1727 | - */ |
|
| 1728 | - public function isDNPartOfBase($dn, $bases) { |
|
| 1729 | - $belongsToBase = false; |
|
| 1730 | - $bases = $this->helper->sanitizeDN($bases); |
|
| 1731 | - |
|
| 1732 | - foreach($bases as $base) { |
|
| 1733 | - $belongsToBase = true; |
|
| 1734 | - if(mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8')-mb_strlen($base, 'UTF-8'))) { |
|
| 1735 | - $belongsToBase = false; |
|
| 1736 | - } |
|
| 1737 | - if($belongsToBase) { |
|
| 1738 | - break; |
|
| 1739 | - } |
|
| 1740 | - } |
|
| 1741 | - return $belongsToBase; |
|
| 1742 | - } |
|
| 1743 | - |
|
| 1744 | - /** |
|
| 1745 | - * resets a running Paged Search operation |
|
| 1746 | - */ |
|
| 1747 | - private function abandonPagedSearch() { |
|
| 1748 | - if($this->connection->hasPagedResultSupport) { |
|
| 1749 | - $cr = $this->connection->getConnectionResource(); |
|
| 1750 | - $this->invokeLDAPMethod('controlPagedResult', $cr, 0, false, $this->lastCookie); |
|
| 1751 | - $this->getPagedSearchResultState(); |
|
| 1752 | - $this->lastCookie = ''; |
|
| 1753 | - $this->cookies = array(); |
|
| 1754 | - } |
|
| 1755 | - } |
|
| 1756 | - |
|
| 1757 | - /** |
|
| 1758 | - * get a cookie for the next LDAP paged search |
|
| 1759 | - * @param string $base a string with the base DN for the search |
|
| 1760 | - * @param string $filter the search filter to identify the correct search |
|
| 1761 | - * @param int $limit the limit (or 'pageSize'), to identify the correct search well |
|
| 1762 | - * @param int $offset the offset for the new search to identify the correct search really good |
|
| 1763 | - * @return string containing the key or empty if none is cached |
|
| 1764 | - */ |
|
| 1765 | - private function getPagedResultCookie($base, $filter, $limit, $offset) { |
|
| 1766 | - if($offset === 0) { |
|
| 1767 | - return ''; |
|
| 1768 | - } |
|
| 1769 | - $offset -= $limit; |
|
| 1770 | - //we work with cache here |
|
| 1771 | - $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . intval($limit) . '-' . intval($offset); |
|
| 1772 | - $cookie = ''; |
|
| 1773 | - if(isset($this->cookies[$cacheKey])) { |
|
| 1774 | - $cookie = $this->cookies[$cacheKey]; |
|
| 1775 | - if(is_null($cookie)) { |
|
| 1776 | - $cookie = ''; |
|
| 1777 | - } |
|
| 1778 | - } |
|
| 1779 | - return $cookie; |
|
| 1780 | - } |
|
| 1781 | - |
|
| 1782 | - /** |
|
| 1783 | - * checks whether an LDAP paged search operation has more pages that can be |
|
| 1784 | - * retrieved, typically when offset and limit are provided. |
|
| 1785 | - * |
|
| 1786 | - * Be very careful to use it: the last cookie value, which is inspected, can |
|
| 1787 | - * be reset by other operations. Best, call it immediately after a search(), |
|
| 1788 | - * searchUsers() or searchGroups() call. count-methods are probably safe as |
|
| 1789 | - * well. Don't rely on it with any fetchList-method. |
|
| 1790 | - * @return bool |
|
| 1791 | - */ |
|
| 1792 | - public function hasMoreResults() { |
|
| 1793 | - if(!$this->connection->hasPagedResultSupport) { |
|
| 1794 | - return false; |
|
| 1795 | - } |
|
| 1796 | - |
|
| 1797 | - if(empty($this->lastCookie) && $this->lastCookie !== '0') { |
|
| 1798 | - // as in RFC 2696, when all results are returned, the cookie will |
|
| 1799 | - // be empty. |
|
| 1800 | - return false; |
|
| 1801 | - } |
|
| 1802 | - |
|
| 1803 | - return true; |
|
| 1804 | - } |
|
| 1805 | - |
|
| 1806 | - /** |
|
| 1807 | - * set a cookie for LDAP paged search run |
|
| 1808 | - * @param string $base a string with the base DN for the search |
|
| 1809 | - * @param string $filter the search filter to identify the correct search |
|
| 1810 | - * @param int $limit the limit (or 'pageSize'), to identify the correct search well |
|
| 1811 | - * @param int $offset the offset for the run search to identify the correct search really good |
|
| 1812 | - * @param string $cookie string containing the cookie returned by ldap_control_paged_result_response |
|
| 1813 | - * @return void |
|
| 1814 | - */ |
|
| 1815 | - private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) { |
|
| 1816 | - // allow '0' for 389ds |
|
| 1817 | - if(!empty($cookie) || $cookie === '0') { |
|
| 1818 | - $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' .intval($limit) . '-' . intval($offset); |
|
| 1819 | - $this->cookies[$cacheKey] = $cookie; |
|
| 1820 | - $this->lastCookie = $cookie; |
|
| 1821 | - } |
|
| 1822 | - } |
|
| 1823 | - |
|
| 1824 | - /** |
|
| 1825 | - * Check whether the most recent paged search was successful. It flushed the state var. Use it always after a possible paged search. |
|
| 1826 | - * @return boolean|null true on success, null or false otherwise |
|
| 1827 | - */ |
|
| 1828 | - public function getPagedSearchResultState() { |
|
| 1829 | - $result = $this->pagedSearchedSuccessful; |
|
| 1830 | - $this->pagedSearchedSuccessful = null; |
|
| 1831 | - return $result; |
|
| 1832 | - } |
|
| 1833 | - |
|
| 1834 | - /** |
|
| 1835 | - * Prepares a paged search, if possible |
|
| 1836 | - * @param string $filter the LDAP filter for the search |
|
| 1837 | - * @param string[] $bases an array containing the LDAP subtree(s) that shall be searched |
|
| 1838 | - * @param string[] $attr optional, when a certain attribute shall be filtered outside |
|
| 1839 | - * @param int $limit |
|
| 1840 | - * @param int $offset |
|
| 1841 | - * @return bool|true |
|
| 1842 | - */ |
|
| 1843 | - private function initPagedSearch($filter, $bases, $attr, $limit, $offset) { |
|
| 1844 | - $pagedSearchOK = false; |
|
| 1845 | - if($this->connection->hasPagedResultSupport && ($limit !== 0)) { |
|
| 1846 | - $offset = intval($offset); //can be null |
|
| 1847 | - \OCP\Util::writeLog('user_ldap', |
|
| 1848 | - 'initializing paged search for Filter '.$filter.' base '.print_r($bases, true) |
|
| 1849 | - .' attr '.print_r($attr, true). ' limit ' .$limit.' offset '.$offset, |
|
| 1850 | - \OCP\Util::DEBUG); |
|
| 1851 | - //get the cookie from the search for the previous search, required by LDAP |
|
| 1852 | - foreach($bases as $base) { |
|
| 1853 | - |
|
| 1854 | - $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset); |
|
| 1855 | - if(empty($cookie) && $cookie !== "0" && ($offset > 0)) { |
|
| 1856 | - // no cookie known, although the offset is not 0. Maybe cache run out. We need |
|
| 1857 | - // to start all over *sigh* (btw, Dear Reader, did you know LDAP paged |
|
| 1858 | - // searching was designed by MSFT?) |
|
| 1859 | - // Lukas: No, but thanks to reading that source I finally know! |
|
| 1860 | - // '0' is valid, because 389ds |
|
| 1861 | - $reOffset = ($offset - $limit) < 0 ? 0 : $offset - $limit; |
|
| 1862 | - //a bit recursive, $offset of 0 is the exit |
|
| 1863 | - \OCP\Util::writeLog('user_ldap', 'Looking for cookie L/O '.$limit.'/'.$reOffset, \OCP\Util::INFO); |
|
| 1864 | - $this->search($filter, array($base), $attr, $limit, $reOffset, true); |
|
| 1865 | - $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset); |
|
| 1866 | - //still no cookie? obviously, the server does not like us. Let's skip paging efforts. |
|
| 1867 | - //TODO: remember this, probably does not change in the next request... |
|
| 1868 | - if(empty($cookie) && $cookie !== '0') { |
|
| 1869 | - // '0' is valid, because 389ds |
|
| 1870 | - $cookie = null; |
|
| 1871 | - } |
|
| 1872 | - } |
|
| 1873 | - if(!is_null($cookie)) { |
|
| 1874 | - //since offset = 0, this is a new search. We abandon other searches that might be ongoing. |
|
| 1875 | - $this->abandonPagedSearch(); |
|
| 1876 | - $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
|
| 1877 | - $this->connection->getConnectionResource(), $limit, |
|
| 1878 | - false, $cookie); |
|
| 1879 | - if(!$pagedSearchOK) { |
|
| 1880 | - return false; |
|
| 1881 | - } |
|
| 1882 | - \OCP\Util::writeLog('user_ldap', 'Ready for a paged search', \OCP\Util::DEBUG); |
|
| 1883 | - } else { |
|
| 1884 | - \OCP\Util::writeLog('user_ldap', |
|
| 1885 | - 'No paged search for us, Cpt., Limit '.$limit.' Offset '.$offset, |
|
| 1886 | - \OCP\Util::INFO); |
|
| 1887 | - } |
|
| 1888 | - |
|
| 1889 | - } |
|
| 1890 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1641 | + \OC::$server->getLogger()->info( |
|
| 1642 | + 'Passed string does not resemble a valid GUID. Known UUID ' . |
|
| 1643 | + '({uuid}) probably does not match UUID configuration.', |
|
| 1644 | + [ 'app' => 'user_ldap', 'uuid' => $guid ] |
|
| 1645 | + ); |
|
| 1646 | + return $guid; |
|
| 1647 | + } |
|
| 1648 | + for($i=0; $i < 3; $i++) { |
|
| 1649 | + $pairs = str_split($blocks[$i], 2); |
|
| 1650 | + $pairs = array_reverse($pairs); |
|
| 1651 | + $blocks[$i] = implode('', $pairs); |
|
| 1652 | + } |
|
| 1653 | + for($i=0; $i < 5; $i++) { |
|
| 1654 | + $pairs = str_split($blocks[$i], 2); |
|
| 1655 | + $blocks[$i] = '\\' . implode('\\', $pairs); |
|
| 1656 | + } |
|
| 1657 | + return implode('', $blocks); |
|
| 1658 | + } |
|
| 1659 | + |
|
| 1660 | + /** |
|
| 1661 | + * gets a SID of the domain of the given dn |
|
| 1662 | + * @param string $dn |
|
| 1663 | + * @return string|bool |
|
| 1664 | + */ |
|
| 1665 | + public function getSID($dn) { |
|
| 1666 | + $domainDN = $this->getDomainDNFromDN($dn); |
|
| 1667 | + $cacheKey = 'getSID-'.$domainDN; |
|
| 1668 | + $sid = $this->connection->getFromCache($cacheKey); |
|
| 1669 | + if(!is_null($sid)) { |
|
| 1670 | + return $sid; |
|
| 1671 | + } |
|
| 1672 | + |
|
| 1673 | + $objectSid = $this->readAttribute($domainDN, 'objectsid'); |
|
| 1674 | + if(!is_array($objectSid) || empty($objectSid)) { |
|
| 1675 | + $this->connection->writeToCache($cacheKey, false); |
|
| 1676 | + return false; |
|
| 1677 | + } |
|
| 1678 | + $domainObjectSid = $this->convertSID2Str($objectSid[0]); |
|
| 1679 | + $this->connection->writeToCache($cacheKey, $domainObjectSid); |
|
| 1680 | + |
|
| 1681 | + return $domainObjectSid; |
|
| 1682 | + } |
|
| 1683 | + |
|
| 1684 | + /** |
|
| 1685 | + * converts a binary SID into a string representation |
|
| 1686 | + * @param string $sid |
|
| 1687 | + * @return string |
|
| 1688 | + */ |
|
| 1689 | + public function convertSID2Str($sid) { |
|
| 1690 | + // The format of a SID binary string is as follows: |
|
| 1691 | + // 1 byte for the revision level |
|
| 1692 | + // 1 byte for the number n of variable sub-ids |
|
| 1693 | + // 6 bytes for identifier authority value |
|
| 1694 | + // n*4 bytes for n sub-ids |
|
| 1695 | + // |
|
| 1696 | + // Example: 010400000000000515000000a681e50e4d6c6c2bca32055f |
|
| 1697 | + // Legend: RRNNAAAAAAAAAAAA11111111222222223333333344444444 |
|
| 1698 | + $revision = ord($sid[0]); |
|
| 1699 | + $numberSubID = ord($sid[1]); |
|
| 1700 | + |
|
| 1701 | + $subIdStart = 8; // 1 + 1 + 6 |
|
| 1702 | + $subIdLength = 4; |
|
| 1703 | + if (strlen($sid) !== $subIdStart + $subIdLength * $numberSubID) { |
|
| 1704 | + // Incorrect number of bytes present. |
|
| 1705 | + return ''; |
|
| 1706 | + } |
|
| 1707 | + |
|
| 1708 | + // 6 bytes = 48 bits can be represented using floats without loss of |
|
| 1709 | + // precision (see https://gist.github.com/bantu/886ac680b0aef5812f71) |
|
| 1710 | + $iav = number_format(hexdec(bin2hex(substr($sid, 2, 6))), 0, '', ''); |
|
| 1711 | + |
|
| 1712 | + $subIDs = array(); |
|
| 1713 | + for ($i = 0; $i < $numberSubID; $i++) { |
|
| 1714 | + $subID = unpack('V', substr($sid, $subIdStart + $subIdLength * $i, $subIdLength)); |
|
| 1715 | + $subIDs[] = sprintf('%u', $subID[1]); |
|
| 1716 | + } |
|
| 1717 | + |
|
| 1718 | + // Result for example above: S-1-5-21-249921958-728525901-1594176202 |
|
| 1719 | + return sprintf('S-%d-%s-%s', $revision, $iav, implode('-', $subIDs)); |
|
| 1720 | + } |
|
| 1721 | + |
|
| 1722 | + /** |
|
| 1723 | + * checks if the given DN is part of the given base DN(s) |
|
| 1724 | + * @param string $dn the DN |
|
| 1725 | + * @param string[] $bases array containing the allowed base DN or DNs |
|
| 1726 | + * @return bool |
|
| 1727 | + */ |
|
| 1728 | + public function isDNPartOfBase($dn, $bases) { |
|
| 1729 | + $belongsToBase = false; |
|
| 1730 | + $bases = $this->helper->sanitizeDN($bases); |
|
| 1731 | + |
|
| 1732 | + foreach($bases as $base) { |
|
| 1733 | + $belongsToBase = true; |
|
| 1734 | + if(mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8')-mb_strlen($base, 'UTF-8'))) { |
|
| 1735 | + $belongsToBase = false; |
|
| 1736 | + } |
|
| 1737 | + if($belongsToBase) { |
|
| 1738 | + break; |
|
| 1739 | + } |
|
| 1740 | + } |
|
| 1741 | + return $belongsToBase; |
|
| 1742 | + } |
|
| 1743 | + |
|
| 1744 | + /** |
|
| 1745 | + * resets a running Paged Search operation |
|
| 1746 | + */ |
|
| 1747 | + private function abandonPagedSearch() { |
|
| 1748 | + if($this->connection->hasPagedResultSupport) { |
|
| 1749 | + $cr = $this->connection->getConnectionResource(); |
|
| 1750 | + $this->invokeLDAPMethod('controlPagedResult', $cr, 0, false, $this->lastCookie); |
|
| 1751 | + $this->getPagedSearchResultState(); |
|
| 1752 | + $this->lastCookie = ''; |
|
| 1753 | + $this->cookies = array(); |
|
| 1754 | + } |
|
| 1755 | + } |
|
| 1756 | + |
|
| 1757 | + /** |
|
| 1758 | + * get a cookie for the next LDAP paged search |
|
| 1759 | + * @param string $base a string with the base DN for the search |
|
| 1760 | + * @param string $filter the search filter to identify the correct search |
|
| 1761 | + * @param int $limit the limit (or 'pageSize'), to identify the correct search well |
|
| 1762 | + * @param int $offset the offset for the new search to identify the correct search really good |
|
| 1763 | + * @return string containing the key or empty if none is cached |
|
| 1764 | + */ |
|
| 1765 | + private function getPagedResultCookie($base, $filter, $limit, $offset) { |
|
| 1766 | + if($offset === 0) { |
|
| 1767 | + return ''; |
|
| 1768 | + } |
|
| 1769 | + $offset -= $limit; |
|
| 1770 | + //we work with cache here |
|
| 1771 | + $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . intval($limit) . '-' . intval($offset); |
|
| 1772 | + $cookie = ''; |
|
| 1773 | + if(isset($this->cookies[$cacheKey])) { |
|
| 1774 | + $cookie = $this->cookies[$cacheKey]; |
|
| 1775 | + if(is_null($cookie)) { |
|
| 1776 | + $cookie = ''; |
|
| 1777 | + } |
|
| 1778 | + } |
|
| 1779 | + return $cookie; |
|
| 1780 | + } |
|
| 1781 | + |
|
| 1782 | + /** |
|
| 1783 | + * checks whether an LDAP paged search operation has more pages that can be |
|
| 1784 | + * retrieved, typically when offset and limit are provided. |
|
| 1785 | + * |
|
| 1786 | + * Be very careful to use it: the last cookie value, which is inspected, can |
|
| 1787 | + * be reset by other operations. Best, call it immediately after a search(), |
|
| 1788 | + * searchUsers() or searchGroups() call. count-methods are probably safe as |
|
| 1789 | + * well. Don't rely on it with any fetchList-method. |
|
| 1790 | + * @return bool |
|
| 1791 | + */ |
|
| 1792 | + public function hasMoreResults() { |
|
| 1793 | + if(!$this->connection->hasPagedResultSupport) { |
|
| 1794 | + return false; |
|
| 1795 | + } |
|
| 1796 | + |
|
| 1797 | + if(empty($this->lastCookie) && $this->lastCookie !== '0') { |
|
| 1798 | + // as in RFC 2696, when all results are returned, the cookie will |
|
| 1799 | + // be empty. |
|
| 1800 | + return false; |
|
| 1801 | + } |
|
| 1802 | + |
|
| 1803 | + return true; |
|
| 1804 | + } |
|
| 1805 | + |
|
| 1806 | + /** |
|
| 1807 | + * set a cookie for LDAP paged search run |
|
| 1808 | + * @param string $base a string with the base DN for the search |
|
| 1809 | + * @param string $filter the search filter to identify the correct search |
|
| 1810 | + * @param int $limit the limit (or 'pageSize'), to identify the correct search well |
|
| 1811 | + * @param int $offset the offset for the run search to identify the correct search really good |
|
| 1812 | + * @param string $cookie string containing the cookie returned by ldap_control_paged_result_response |
|
| 1813 | + * @return void |
|
| 1814 | + */ |
|
| 1815 | + private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) { |
|
| 1816 | + // allow '0' for 389ds |
|
| 1817 | + if(!empty($cookie) || $cookie === '0') { |
|
| 1818 | + $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' .intval($limit) . '-' . intval($offset); |
|
| 1819 | + $this->cookies[$cacheKey] = $cookie; |
|
| 1820 | + $this->lastCookie = $cookie; |
|
| 1821 | + } |
|
| 1822 | + } |
|
| 1823 | + |
|
| 1824 | + /** |
|
| 1825 | + * Check whether the most recent paged search was successful. It flushed the state var. Use it always after a possible paged search. |
|
| 1826 | + * @return boolean|null true on success, null or false otherwise |
|
| 1827 | + */ |
|
| 1828 | + public function getPagedSearchResultState() { |
|
| 1829 | + $result = $this->pagedSearchedSuccessful; |
|
| 1830 | + $this->pagedSearchedSuccessful = null; |
|
| 1831 | + return $result; |
|
| 1832 | + } |
|
| 1833 | + |
|
| 1834 | + /** |
|
| 1835 | + * Prepares a paged search, if possible |
|
| 1836 | + * @param string $filter the LDAP filter for the search |
|
| 1837 | + * @param string[] $bases an array containing the LDAP subtree(s) that shall be searched |
|
| 1838 | + * @param string[] $attr optional, when a certain attribute shall be filtered outside |
|
| 1839 | + * @param int $limit |
|
| 1840 | + * @param int $offset |
|
| 1841 | + * @return bool|true |
|
| 1842 | + */ |
|
| 1843 | + private function initPagedSearch($filter, $bases, $attr, $limit, $offset) { |
|
| 1844 | + $pagedSearchOK = false; |
|
| 1845 | + if($this->connection->hasPagedResultSupport && ($limit !== 0)) { |
|
| 1846 | + $offset = intval($offset); //can be null |
|
| 1847 | + \OCP\Util::writeLog('user_ldap', |
|
| 1848 | + 'initializing paged search for Filter '.$filter.' base '.print_r($bases, true) |
|
| 1849 | + .' attr '.print_r($attr, true). ' limit ' .$limit.' offset '.$offset, |
|
| 1850 | + \OCP\Util::DEBUG); |
|
| 1851 | + //get the cookie from the search for the previous search, required by LDAP |
|
| 1852 | + foreach($bases as $base) { |
|
| 1853 | + |
|
| 1854 | + $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset); |
|
| 1855 | + if(empty($cookie) && $cookie !== "0" && ($offset > 0)) { |
|
| 1856 | + // no cookie known, although the offset is not 0. Maybe cache run out. We need |
|
| 1857 | + // to start all over *sigh* (btw, Dear Reader, did you know LDAP paged |
|
| 1858 | + // searching was designed by MSFT?) |
|
| 1859 | + // Lukas: No, but thanks to reading that source I finally know! |
|
| 1860 | + // '0' is valid, because 389ds |
|
| 1861 | + $reOffset = ($offset - $limit) < 0 ? 0 : $offset - $limit; |
|
| 1862 | + //a bit recursive, $offset of 0 is the exit |
|
| 1863 | + \OCP\Util::writeLog('user_ldap', 'Looking for cookie L/O '.$limit.'/'.$reOffset, \OCP\Util::INFO); |
|
| 1864 | + $this->search($filter, array($base), $attr, $limit, $reOffset, true); |
|
| 1865 | + $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset); |
|
| 1866 | + //still no cookie? obviously, the server does not like us. Let's skip paging efforts. |
|
| 1867 | + //TODO: remember this, probably does not change in the next request... |
|
| 1868 | + if(empty($cookie) && $cookie !== '0') { |
|
| 1869 | + // '0' is valid, because 389ds |
|
| 1870 | + $cookie = null; |
|
| 1871 | + } |
|
| 1872 | + } |
|
| 1873 | + if(!is_null($cookie)) { |
|
| 1874 | + //since offset = 0, this is a new search. We abandon other searches that might be ongoing. |
|
| 1875 | + $this->abandonPagedSearch(); |
|
| 1876 | + $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
|
| 1877 | + $this->connection->getConnectionResource(), $limit, |
|
| 1878 | + false, $cookie); |
|
| 1879 | + if(!$pagedSearchOK) { |
|
| 1880 | + return false; |
|
| 1881 | + } |
|
| 1882 | + \OCP\Util::writeLog('user_ldap', 'Ready for a paged search', \OCP\Util::DEBUG); |
|
| 1883 | + } else { |
|
| 1884 | + \OCP\Util::writeLog('user_ldap', |
|
| 1885 | + 'No paged search for us, Cpt., Limit '.$limit.' Offset '.$offset, |
|
| 1886 | + \OCP\Util::INFO); |
|
| 1887 | + } |
|
| 1888 | + |
|
| 1889 | + } |
|
| 1890 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1891 | 1891 | * We coudn't get paged searches working with our RHDS for login ($limit = 0), |
| 1892 | 1892 | * due to pages with zero results. |
| 1893 | 1893 | * So we added "&& !empty($this->lastCookie)" to this test to ignore pagination |
| 1894 | 1894 | * if we don't have a previous paged search. |
| 1895 | 1895 | */ |
| 1896 | - } else if($this->connection->hasPagedResultSupport && $limit === 0 && !empty($this->lastCookie)) { |
|
| 1897 | - // a search without limit was requested. However, if we do use |
|
| 1898 | - // Paged Search once, we always must do it. This requires us to |
|
| 1899 | - // initialize it with the configured page size. |
|
| 1900 | - $this->abandonPagedSearch(); |
|
| 1901 | - // in case someone set it to 0 … use 500, otherwise no results will |
|
| 1902 | - // be returned. |
|
| 1903 | - $pageSize = intval($this->connection->ldapPagingSize) > 0 ? intval($this->connection->ldapPagingSize) : 500; |
|
| 1904 | - $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
|
| 1905 | - $this->connection->getConnectionResource(), |
|
| 1906 | - $pageSize, false, ''); |
|
| 1907 | - } |
|
| 1908 | - |
|
| 1909 | - return $pagedSearchOK; |
|
| 1910 | - } |
|
| 1896 | + } else if($this->connection->hasPagedResultSupport && $limit === 0 && !empty($this->lastCookie)) { |
|
| 1897 | + // a search without limit was requested. However, if we do use |
|
| 1898 | + // Paged Search once, we always must do it. This requires us to |
|
| 1899 | + // initialize it with the configured page size. |
|
| 1900 | + $this->abandonPagedSearch(); |
|
| 1901 | + // in case someone set it to 0 … use 500, otherwise no results will |
|
| 1902 | + // be returned. |
|
| 1903 | + $pageSize = intval($this->connection->ldapPagingSize) > 0 ? intval($this->connection->ldapPagingSize) : 500; |
|
| 1904 | + $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
|
| 1905 | + $this->connection->getConnectionResource(), |
|
| 1906 | + $pageSize, false, ''); |
|
| 1907 | + } |
|
| 1908 | + |
|
| 1909 | + return $pagedSearchOK; |
|
| 1910 | + } |
|
| 1911 | 1911 | |
| 1912 | 1912 | } |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | * @return AbstractMapping |
| 113 | 113 | */ |
| 114 | 114 | public function getUserMapper() { |
| 115 | - if(is_null($this->userMapper)) { |
|
| 115 | + if (is_null($this->userMapper)) { |
|
| 116 | 116 | throw new \Exception('UserMapper was not assigned to this Access instance.'); |
| 117 | 117 | } |
| 118 | 118 | return $this->userMapper; |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | * @return AbstractMapping |
| 133 | 133 | */ |
| 134 | 134 | public function getGroupMapper() { |
| 135 | - if(is_null($this->groupMapper)) { |
|
| 135 | + if (is_null($this->groupMapper)) { |
|
| 136 | 136 | throw new \Exception('GroupMapper was not assigned to this Access instance.'); |
| 137 | 137 | } |
| 138 | 138 | return $this->groupMapper; |
@@ -163,14 +163,14 @@ discard block |
||
| 163 | 163 | * array if $attr is empty, false otherwise |
| 164 | 164 | */ |
| 165 | 165 | public function readAttribute($dn, $attr, $filter = 'objectClass=*') { |
| 166 | - if(!$this->checkConnection()) { |
|
| 166 | + if (!$this->checkConnection()) { |
|
| 167 | 167 | \OCP\Util::writeLog('user_ldap', |
| 168 | 168 | 'No LDAP Connector assigned, access impossible for readAttribute.', |
| 169 | 169 | \OCP\Util::WARN); |
| 170 | 170 | return false; |
| 171 | 171 | } |
| 172 | 172 | $cr = $this->connection->getConnectionResource(); |
| 173 | - if(!$this->ldap->isResource($cr)) { |
|
| 173 | + if (!$this->ldap->isResource($cr)) { |
|
| 174 | 174 | //LDAP not available |
| 175 | 175 | \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
| 176 | 176 | return false; |
@@ -193,7 +193,7 @@ discard block |
||
| 193 | 193 | $isRangeRequest = false; |
| 194 | 194 | do { |
| 195 | 195 | $result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults); |
| 196 | - if(is_bool($result)) { |
|
| 196 | + if (is_bool($result)) { |
|
| 197 | 197 | // when an exists request was run and it was successful, an empty |
| 198 | 198 | // array must be returned |
| 199 | 199 | return $result ? [] : false; |
@@ -210,22 +210,22 @@ discard block |
||
| 210 | 210 | $result = $this->extractRangeData($result, $attr); |
| 211 | 211 | if (!empty($result)) { |
| 212 | 212 | $normalizedResult = $this->extractAttributeValuesFromResult( |
| 213 | - [ $attr => $result['values'] ], |
|
| 213 | + [$attr => $result['values']], |
|
| 214 | 214 | $attr |
| 215 | 215 | ); |
| 216 | 216 | $values = array_merge($values, $normalizedResult); |
| 217 | 217 | |
| 218 | - if($result['rangeHigh'] === '*') { |
|
| 218 | + if ($result['rangeHigh'] === '*') { |
|
| 219 | 219 | // when server replies with * as high range value, there are |
| 220 | 220 | // no more results left |
| 221 | 221 | return $values; |
| 222 | 222 | } else { |
| 223 | - $low = $result['rangeHigh'] + 1; |
|
| 224 | - $attrToRead = $result['attributeName'] . ';range=' . $low . '-*'; |
|
| 223 | + $low = $result['rangeHigh'] + 1; |
|
| 224 | + $attrToRead = $result['attributeName'].';range='.$low.'-*'; |
|
| 225 | 225 | $isRangeRequest = true; |
| 226 | 226 | } |
| 227 | 227 | } |
| 228 | - } while($isRangeRequest); |
|
| 228 | + } while ($isRangeRequest); |
|
| 229 | 229 | |
| 230 | 230 | \OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, \OCP\Util::DEBUG); |
| 231 | 231 | return false; |
@@ -250,13 +250,13 @@ discard block |
||
| 250 | 250 | if (!$this->ldap->isResource($rr)) { |
| 251 | 251 | if ($attribute !== '') { |
| 252 | 252 | //do not throw this message on userExists check, irritates |
| 253 | - \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, \OCP\Util::DEBUG); |
|
| 253 | + \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN '.$dn, \OCP\Util::DEBUG); |
|
| 254 | 254 | } |
| 255 | 255 | //in case an error occurs , e.g. object does not exist |
| 256 | 256 | return false; |
| 257 | 257 | } |
| 258 | 258 | if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) { |
| 259 | - \OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', \OCP\Util::DEBUG); |
|
| 259 | + \OCP\Util::writeLog('user_ldap', 'readAttribute: '.$dn.' found', \OCP\Util::DEBUG); |
|
| 260 | 260 | return true; |
| 261 | 261 | } |
| 262 | 262 | $er = $this->invokeLDAPMethod('firstEntry', $cr, $rr); |
@@ -281,12 +281,12 @@ discard block |
||
| 281 | 281 | */ |
| 282 | 282 | public function extractAttributeValuesFromResult($result, $attribute) { |
| 283 | 283 | $values = []; |
| 284 | - if(isset($result[$attribute]) && $result[$attribute]['count'] > 0) { |
|
| 284 | + if (isset($result[$attribute]) && $result[$attribute]['count'] > 0) { |
|
| 285 | 285 | $lowercaseAttribute = strtolower($attribute); |
| 286 | - for($i=0;$i<$result[$attribute]['count'];$i++) { |
|
| 287 | - if($this->resemblesDN($attribute)) { |
|
| 286 | + for ($i = 0; $i < $result[$attribute]['count']; $i++) { |
|
| 287 | + if ($this->resemblesDN($attribute)) { |
|
| 288 | 288 | $values[] = $this->helper->sanitizeDN($result[$attribute][$i]); |
| 289 | - } elseif($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { |
|
| 289 | + } elseif ($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { |
|
| 290 | 290 | $values[] = $this->convertObjectGUID2Str($result[$attribute][$i]); |
| 291 | 291 | } else { |
| 292 | 292 | $values[] = $result[$attribute][$i]; |
@@ -308,10 +308,10 @@ discard block |
||
| 308 | 308 | */ |
| 309 | 309 | public function extractRangeData($result, $attribute) { |
| 310 | 310 | $keys = array_keys($result); |
| 311 | - foreach($keys as $key) { |
|
| 312 | - if($key !== $attribute && strpos($key, $attribute) === 0) { |
|
| 311 | + foreach ($keys as $key) { |
|
| 312 | + if ($key !== $attribute && strpos($key, $attribute) === 0) { |
|
| 313 | 313 | $queryData = explode(';', $key); |
| 314 | - if(strpos($queryData[1], 'range=') === 0) { |
|
| 314 | + if (strpos($queryData[1], 'range=') === 0) { |
|
| 315 | 315 | $high = substr($queryData[1], 1 + strpos($queryData[1], '-')); |
| 316 | 316 | $data = [ |
| 317 | 317 | 'values' => $result[$key], |
@@ -336,18 +336,18 @@ discard block |
||
| 336 | 336 | * @throws \Exception |
| 337 | 337 | */ |
| 338 | 338 | public function setPassword($userDN, $password) { |
| 339 | - if(intval($this->connection->turnOnPasswordChange) !== 1) { |
|
| 339 | + if (intval($this->connection->turnOnPasswordChange) !== 1) { |
|
| 340 | 340 | throw new \Exception('LDAP password changes are disabled.'); |
| 341 | 341 | } |
| 342 | 342 | $cr = $this->connection->getConnectionResource(); |
| 343 | - if(!$this->ldap->isResource($cr)) { |
|
| 343 | + if (!$this->ldap->isResource($cr)) { |
|
| 344 | 344 | //LDAP not available |
| 345 | 345 | \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
| 346 | 346 | return false; |
| 347 | 347 | } |
| 348 | 348 | try { |
| 349 | 349 | return @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password); |
| 350 | - } catch(ConstraintViolationException $e) { |
|
| 350 | + } catch (ConstraintViolationException $e) { |
|
| 351 | 351 | throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ').$e->getMessage(), $e->getCode()); |
| 352 | 352 | } |
| 353 | 353 | } |
@@ -389,17 +389,17 @@ discard block |
||
| 389 | 389 | */ |
| 390 | 390 | public function getDomainDNFromDN($dn) { |
| 391 | 391 | $allParts = $this->ldap->explodeDN($dn, 0); |
| 392 | - if($allParts === false) { |
|
| 392 | + if ($allParts === false) { |
|
| 393 | 393 | //not a valid DN |
| 394 | 394 | return ''; |
| 395 | 395 | } |
| 396 | 396 | $domainParts = array(); |
| 397 | 397 | $dcFound = false; |
| 398 | - foreach($allParts as $part) { |
|
| 399 | - if(!$dcFound && strpos($part, 'dc=') === 0) { |
|
| 398 | + foreach ($allParts as $part) { |
|
| 399 | + if (!$dcFound && strpos($part, 'dc=') === 0) { |
|
| 400 | 400 | $dcFound = true; |
| 401 | 401 | } |
| 402 | - if($dcFound) { |
|
| 402 | + if ($dcFound) { |
|
| 403 | 403 | $domainParts[] = $part; |
| 404 | 404 | } |
| 405 | 405 | } |
@@ -426,7 +426,7 @@ discard block |
||
| 426 | 426 | |
| 427 | 427 | //Check whether the DN belongs to the Base, to avoid issues on multi- |
| 428 | 428 | //server setups |
| 429 | - if(is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 429 | + if (is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 430 | 430 | return $fdn; |
| 431 | 431 | } |
| 432 | 432 | |
@@ -443,7 +443,7 @@ discard block |
||
| 443 | 443 | //To avoid bypassing the base DN settings under certain circumstances |
| 444 | 444 | //with the group support, check whether the provided DN matches one of |
| 445 | 445 | //the given Bases |
| 446 | - if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { |
|
| 446 | + if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { |
|
| 447 | 447 | return false; |
| 448 | 448 | } |
| 449 | 449 | |
@@ -460,11 +460,11 @@ discard block |
||
| 460 | 460 | */ |
| 461 | 461 | public function groupsMatchFilter($groupDNs) { |
| 462 | 462 | $validGroupDNs = []; |
| 463 | - foreach($groupDNs as $dn) { |
|
| 463 | + foreach ($groupDNs as $dn) { |
|
| 464 | 464 | $cacheKey = 'groupsMatchFilter-'.$dn; |
| 465 | 465 | $groupMatchFilter = $this->connection->getFromCache($cacheKey); |
| 466 | - if(!is_null($groupMatchFilter)) { |
|
| 467 | - if($groupMatchFilter) { |
|
| 466 | + if (!is_null($groupMatchFilter)) { |
|
| 467 | + if ($groupMatchFilter) { |
|
| 468 | 468 | $validGroupDNs[] = $dn; |
| 469 | 469 | } |
| 470 | 470 | continue; |
@@ -472,13 +472,13 @@ discard block |
||
| 472 | 472 | |
| 473 | 473 | // Check the base DN first. If this is not met already, we don't |
| 474 | 474 | // need to ask the server at all. |
| 475 | - if(!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { |
|
| 475 | + if (!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { |
|
| 476 | 476 | $this->connection->writeToCache($cacheKey, false); |
| 477 | 477 | continue; |
| 478 | 478 | } |
| 479 | 479 | |
| 480 | 480 | $result = $this->readAttribute($dn, 'cn', $this->connection->ldapGroupFilter); |
| 481 | - if(is_array($result)) { |
|
| 481 | + if (is_array($result)) { |
|
| 482 | 482 | $this->connection->writeToCache($cacheKey, true); |
| 483 | 483 | $validGroupDNs[] = $dn; |
| 484 | 484 | } else { |
@@ -499,7 +499,7 @@ discard block |
||
| 499 | 499 | //To avoid bypassing the base DN settings under certain circumstances |
| 500 | 500 | //with the group support, check whether the provided DN matches one of |
| 501 | 501 | //the given Bases |
| 502 | - if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 502 | + if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 503 | 503 | return false; |
| 504 | 504 | } |
| 505 | 505 | |
@@ -514,7 +514,7 @@ discard block |
||
| 514 | 514 | * @return string|false with with the name to use in Nextcloud |
| 515 | 515 | */ |
| 516 | 516 | public function dn2ocname($fdn, $ldapName = null, $isUser = true) { |
| 517 | - if($isUser) { |
|
| 517 | + if ($isUser) { |
|
| 518 | 518 | $mapper = $this->getUserMapper(); |
| 519 | 519 | $nameAttribute = $this->connection->ldapUserDisplayName; |
| 520 | 520 | } else { |
@@ -524,15 +524,15 @@ discard block |
||
| 524 | 524 | |
| 525 | 525 | //let's try to retrieve the Nextcloud name from the mappings table |
| 526 | 526 | $ocName = $mapper->getNameByDN($fdn); |
| 527 | - if(is_string($ocName)) { |
|
| 527 | + if (is_string($ocName)) { |
|
| 528 | 528 | return $ocName; |
| 529 | 529 | } |
| 530 | 530 | |
| 531 | 531 | //second try: get the UUID and check if it is known. Then, update the DN and return the name. |
| 532 | 532 | $uuid = $this->getUUID($fdn, $isUser); |
| 533 | - if(is_string($uuid)) { |
|
| 533 | + if (is_string($uuid)) { |
|
| 534 | 534 | $ocName = $mapper->getNameByUUID($uuid); |
| 535 | - if(is_string($ocName)) { |
|
| 535 | + if (is_string($ocName)) { |
|
| 536 | 536 | $mapper->setDNbyUUID($fdn, $uuid); |
| 537 | 537 | return $ocName; |
| 538 | 538 | } |
@@ -542,16 +542,16 @@ discard block |
||
| 542 | 542 | return false; |
| 543 | 543 | } |
| 544 | 544 | |
| 545 | - if(is_null($ldapName)) { |
|
| 545 | + if (is_null($ldapName)) { |
|
| 546 | 546 | $ldapName = $this->readAttribute($fdn, $nameAttribute); |
| 547 | - if(!isset($ldapName[0]) && empty($ldapName[0])) { |
|
| 547 | + if (!isset($ldapName[0]) && empty($ldapName[0])) { |
|
| 548 | 548 | \OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.'.', \OCP\Util::INFO); |
| 549 | 549 | return false; |
| 550 | 550 | } |
| 551 | 551 | $ldapName = $ldapName[0]; |
| 552 | 552 | } |
| 553 | 553 | |
| 554 | - if($isUser) { |
|
| 554 | + if ($isUser) { |
|
| 555 | 555 | $usernameAttribute = strval($this->connection->ldapExpertUsernameAttr); |
| 556 | 556 | if ($usernameAttribute !== '') { |
| 557 | 557 | $username = $this->readAttribute($fdn, $usernameAttribute); |
@@ -570,9 +570,9 @@ discard block |
||
| 570 | 570 | // outside of core user management will still cache the user as non-existing. |
| 571 | 571 | $originalTTL = $this->connection->ldapCacheTTL; |
| 572 | 572 | $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
| 573 | - if(($isUser && !\OCP\User::userExists($intName)) |
|
| 573 | + if (($isUser && !\OCP\User::userExists($intName)) |
|
| 574 | 574 | || (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))) { |
| 575 | - if($mapper->map($fdn, $intName, $uuid)) { |
|
| 575 | + if ($mapper->map($fdn, $intName, $uuid)) { |
|
| 576 | 576 | $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
| 577 | 577 | return $intName; |
| 578 | 578 | } |
@@ -580,7 +580,7 @@ discard block |
||
| 580 | 580 | $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
| 581 | 581 | |
| 582 | 582 | $altName = $this->createAltInternalOwnCloudName($intName, $isUser); |
| 583 | - if(is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { |
|
| 583 | + if (is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { |
|
| 584 | 584 | return $altName; |
| 585 | 585 | } |
| 586 | 586 | |
@@ -617,7 +617,7 @@ discard block |
||
| 617 | 617 | * @return array |
| 618 | 618 | */ |
| 619 | 619 | private function ldap2NextcloudNames($ldapObjects, $isUsers) { |
| 620 | - if($isUsers) { |
|
| 620 | + if ($isUsers) { |
|
| 621 | 621 | $nameAttribute = $this->connection->ldapUserDisplayName; |
| 622 | 622 | $sndAttribute = $this->connection->ldapUserDisplayName2; |
| 623 | 623 | } else { |
@@ -625,9 +625,9 @@ discard block |
||
| 625 | 625 | } |
| 626 | 626 | $nextcloudNames = array(); |
| 627 | 627 | |
| 628 | - foreach($ldapObjects as $ldapObject) { |
|
| 628 | + foreach ($ldapObjects as $ldapObject) { |
|
| 629 | 629 | $nameByLDAP = null; |
| 630 | - if( isset($ldapObject[$nameAttribute]) |
|
| 630 | + if (isset($ldapObject[$nameAttribute]) |
|
| 631 | 631 | && is_array($ldapObject[$nameAttribute]) |
| 632 | 632 | && isset($ldapObject[$nameAttribute][0]) |
| 633 | 633 | ) { |
@@ -636,12 +636,12 @@ discard block |
||
| 636 | 636 | } |
| 637 | 637 | |
| 638 | 638 | $ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers); |
| 639 | - if($ncName) { |
|
| 639 | + if ($ncName) { |
|
| 640 | 640 | $nextcloudNames[] = $ncName; |
| 641 | - if($isUsers) { |
|
| 641 | + if ($isUsers) { |
|
| 642 | 642 | //cache the user names so it does not need to be retrieved |
| 643 | 643 | //again later (e.g. sharing dialogue). |
| 644 | - if(is_null($nameByLDAP)) { |
|
| 644 | + if (is_null($nameByLDAP)) { |
|
| 645 | 645 | continue; |
| 646 | 646 | } |
| 647 | 647 | $sndName = isset($ldapObject[$sndAttribute][0]) |
@@ -679,7 +679,7 @@ discard block |
||
| 679 | 679 | */ |
| 680 | 680 | public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { |
| 681 | 681 | $user = $this->userManager->get($ocName); |
| 682 | - if($user === null) { |
|
| 682 | + if ($user === null) { |
|
| 683 | 683 | return; |
| 684 | 684 | } |
| 685 | 685 | $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
@@ -699,9 +699,9 @@ discard block |
||
| 699 | 699 | $attempts = 0; |
| 700 | 700 | //while loop is just a precaution. If a name is not generated within |
| 701 | 701 | //20 attempts, something else is very wrong. Avoids infinite loop. |
| 702 | - while($attempts < 20){ |
|
| 703 | - $altName = $name . '_' . rand(1000,9999); |
|
| 704 | - if(!\OCP\User::userExists($altName)) { |
|
| 702 | + while ($attempts < 20) { |
|
| 703 | + $altName = $name.'_'.rand(1000, 9999); |
|
| 704 | + if (!\OCP\User::userExists($altName)) { |
|
| 705 | 705 | return $altName; |
| 706 | 706 | } |
| 707 | 707 | $attempts++; |
@@ -723,25 +723,25 @@ discard block |
||
| 723 | 723 | */ |
| 724 | 724 | private function _createAltInternalOwnCloudNameForGroups($name) { |
| 725 | 725 | $usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%'); |
| 726 | - if(!($usedNames) || count($usedNames) === 0) { |
|
| 726 | + if (!($usedNames) || count($usedNames) === 0) { |
|
| 727 | 727 | $lastNo = 1; //will become name_2 |
| 728 | 728 | } else { |
| 729 | 729 | natsort($usedNames); |
| 730 | 730 | $lastName = array_pop($usedNames); |
| 731 | 731 | $lastNo = intval(substr($lastName, strrpos($lastName, '_') + 1)); |
| 732 | 732 | } |
| 733 | - $altName = $name.'_'.strval($lastNo+1); |
|
| 733 | + $altName = $name.'_'.strval($lastNo + 1); |
|
| 734 | 734 | unset($usedNames); |
| 735 | 735 | |
| 736 | 736 | $attempts = 1; |
| 737 | - while($attempts < 21){ |
|
| 737 | + while ($attempts < 21) { |
|
| 738 | 738 | // Check to be really sure it is unique |
| 739 | 739 | // while loop is just a precaution. If a name is not generated within |
| 740 | 740 | // 20 attempts, something else is very wrong. Avoids infinite loop. |
| 741 | - if(!\OC::$server->getGroupManager()->groupExists($altName)) { |
|
| 741 | + if (!\OC::$server->getGroupManager()->groupExists($altName)) { |
|
| 742 | 742 | return $altName; |
| 743 | 743 | } |
| 744 | - $altName = $name . '_' . ($lastNo + $attempts); |
|
| 744 | + $altName = $name.'_'.($lastNo + $attempts); |
|
| 745 | 745 | $attempts++; |
| 746 | 746 | } |
| 747 | 747 | return false; |
@@ -756,7 +756,7 @@ discard block |
||
| 756 | 756 | private function createAltInternalOwnCloudName($name, $isUser) { |
| 757 | 757 | $originalTTL = $this->connection->ldapCacheTTL; |
| 758 | 758 | $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
| 759 | - if($isUser) { |
|
| 759 | + if ($isUser) { |
|
| 760 | 760 | $altName = $this->_createAltInternalOwnCloudNameForUsers($name); |
| 761 | 761 | } else { |
| 762 | 762 | $altName = $this->_createAltInternalOwnCloudNameForGroups($name); |
@@ -814,20 +814,20 @@ discard block |
||
| 814 | 814 | * and their values |
| 815 | 815 | * @param array $ldapRecords |
| 816 | 816 | */ |
| 817 | - public function batchApplyUserAttributes(array $ldapRecords){ |
|
| 817 | + public function batchApplyUserAttributes(array $ldapRecords) { |
|
| 818 | 818 | $displayNameAttribute = strtolower($this->connection->ldapUserDisplayName); |
| 819 | - foreach($ldapRecords as $userRecord) { |
|
| 820 | - if(!isset($userRecord[$displayNameAttribute])) { |
|
| 819 | + foreach ($ldapRecords as $userRecord) { |
|
| 820 | + if (!isset($userRecord[$displayNameAttribute])) { |
|
| 821 | 821 | // displayName is obligatory |
| 822 | 822 | continue; |
| 823 | 823 | } |
| 824 | - $ocName = $this->dn2ocname($userRecord['dn'][0]); |
|
| 825 | - if($ocName === false) { |
|
| 824 | + $ocName = $this->dn2ocname($userRecord['dn'][0]); |
|
| 825 | + if ($ocName === false) { |
|
| 826 | 826 | continue; |
| 827 | 827 | } |
| 828 | 828 | $this->cacheUserExists($ocName); |
| 829 | 829 | $user = $this->userManager->get($ocName); |
| 830 | - if($user instanceof OfflineUser) { |
|
| 830 | + if ($user instanceof OfflineUser) { |
|
| 831 | 831 | $user->unmark(); |
| 832 | 832 | $user = $this->userManager->get($ocName); |
| 833 | 833 | } |
@@ -859,8 +859,8 @@ discard block |
||
| 859 | 859 | * @return array |
| 860 | 860 | */ |
| 861 | 861 | private function fetchList($list, $manyAttributes) { |
| 862 | - if(is_array($list)) { |
|
| 863 | - if($manyAttributes) { |
|
| 862 | + if (is_array($list)) { |
|
| 863 | + if ($manyAttributes) { |
|
| 864 | 864 | return $list; |
| 865 | 865 | } else { |
| 866 | 866 | $list = array_reduce($list, function($carry, $item) { |
@@ -958,7 +958,7 @@ discard block |
||
| 958 | 958 | // php no longer supports call-time pass-by-reference |
| 959 | 959 | // thus cannot support controlPagedResultResponse as the third argument |
| 960 | 960 | // is a reference |
| 961 | - $doMethod = function () use ($command, &$arguments) { |
|
| 961 | + $doMethod = function() use ($command, &$arguments) { |
|
| 962 | 962 | if ($command == 'controlPagedResultResponse') { |
| 963 | 963 | throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.'); |
| 964 | 964 | } else { |
@@ -976,7 +976,7 @@ discard block |
||
| 976 | 976 | $this->connection->resetConnectionResource(); |
| 977 | 977 | $cr = $this->connection->getConnectionResource(); |
| 978 | 978 | |
| 979 | - if(!$this->ldap->isResource($cr)) { |
|
| 979 | + if (!$this->ldap->isResource($cr)) { |
|
| 980 | 980 | // Seems like we didn't find any resource. |
| 981 | 981 | \OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", \OCP\Util::DEBUG); |
| 982 | 982 | throw $e; |
@@ -997,13 +997,13 @@ discard block |
||
| 997 | 997 | * @throws \OC\ServerNotAvailableException |
| 998 | 998 | */ |
| 999 | 999 | private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) { |
| 1000 | - if(!is_null($attr) && !is_array($attr)) { |
|
| 1000 | + if (!is_null($attr) && !is_array($attr)) { |
|
| 1001 | 1001 | $attr = array(mb_strtolower($attr, 'UTF-8')); |
| 1002 | 1002 | } |
| 1003 | 1003 | |
| 1004 | 1004 | // See if we have a resource, in case not cancel with message |
| 1005 | 1005 | $cr = $this->connection->getConnectionResource(); |
| 1006 | - if(!$this->ldap->isResource($cr)) { |
|
| 1006 | + if (!$this->ldap->isResource($cr)) { |
|
| 1007 | 1007 | // Seems like we didn't find any resource. |
| 1008 | 1008 | // Return an empty array just like before. |
| 1009 | 1009 | \OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', \OCP\Util::DEBUG); |
@@ -1017,7 +1017,7 @@ discard block |
||
| 1017 | 1017 | $sr = $this->invokeLDAPMethod('search', $linkResources, $base, $filter, $attr); |
| 1018 | 1018 | // cannot use $cr anymore, might have changed in the previous call! |
| 1019 | 1019 | $error = $this->ldap->errno($this->connection->getConnectionResource()); |
| 1020 | - if(!is_array($sr) || $error !== 0) { |
|
| 1020 | + if (!is_array($sr) || $error !== 0) { |
|
| 1021 | 1021 | \OCP\Util::writeLog('user_ldap', 'Attempt for Paging? '.print_r($pagedSearchOK, true), \OCP\Util::ERROR); |
| 1022 | 1022 | return false; |
| 1023 | 1023 | } |
@@ -1040,26 +1040,26 @@ discard block |
||
| 1040 | 1040 | */ |
| 1041 | 1041 | private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) { |
| 1042 | 1042 | $cookie = null; |
| 1043 | - if($pagedSearchOK) { |
|
| 1043 | + if ($pagedSearchOK) { |
|
| 1044 | 1044 | $cr = $this->connection->getConnectionResource(); |
| 1045 | - foreach($sr as $key => $res) { |
|
| 1046 | - if($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) { |
|
| 1045 | + foreach ($sr as $key => $res) { |
|
| 1046 | + if ($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) { |
|
| 1047 | 1047 | $this->setPagedResultCookie($base[$key], $filter, $limit, $offset, $cookie); |
| 1048 | 1048 | } |
| 1049 | 1049 | } |
| 1050 | 1050 | |
| 1051 | 1051 | //browsing through prior pages to get the cookie for the new one |
| 1052 | - if($skipHandling) { |
|
| 1052 | + if ($skipHandling) { |
|
| 1053 | 1053 | return false; |
| 1054 | 1054 | } |
| 1055 | 1055 | // if count is bigger, then the server does not support |
| 1056 | 1056 | // paged search. Instead, he did a normal search. We set a |
| 1057 | 1057 | // flag here, so the callee knows how to deal with it. |
| 1058 | - if($iFoundItems <= $limit) { |
|
| 1058 | + if ($iFoundItems <= $limit) { |
|
| 1059 | 1059 | $this->pagedSearchedSuccessful = true; |
| 1060 | 1060 | } |
| 1061 | 1061 | } else { |
| 1062 | - if(!is_null($limit)) { |
|
| 1062 | + if (!is_null($limit)) { |
|
| 1063 | 1063 | \OCP\Util::writeLog('user_ldap', 'Paged search was not available', \OCP\Util::INFO); |
| 1064 | 1064 | } |
| 1065 | 1065 | } |
@@ -1088,7 +1088,7 @@ discard block |
||
| 1088 | 1088 | \OCP\Util::writeLog('user_ldap', 'Count filter: '.print_r($filter, true), \OCP\Util::DEBUG); |
| 1089 | 1089 | |
| 1090 | 1090 | $limitPerPage = intval($this->connection->ldapPagingSize); |
| 1091 | - if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1091 | + if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1092 | 1092 | $limitPerPage = $limit; |
| 1093 | 1093 | } |
| 1094 | 1094 | |
@@ -1099,7 +1099,7 @@ discard block |
||
| 1099 | 1099 | do { |
| 1100 | 1100 | $search = $this->executeSearch($filter, $base, $attr, |
| 1101 | 1101 | $limitPerPage, $offset); |
| 1102 | - if($search === false) { |
|
| 1102 | + if ($search === false) { |
|
| 1103 | 1103 | return $counter > 0 ? $counter : false; |
| 1104 | 1104 | } |
| 1105 | 1105 | list($sr, $pagedSearchOK) = $search; |
@@ -1118,7 +1118,7 @@ discard block |
||
| 1118 | 1118 | * Continue now depends on $hasMorePages value |
| 1119 | 1119 | */ |
| 1120 | 1120 | $continue = $pagedSearchOK && $hasMorePages; |
| 1121 | - } while($continue && (is_null($limit) || $limit <= 0 || $limit > $counter)); |
|
| 1121 | + } while ($continue && (is_null($limit) || $limit <= 0 || $limit > $counter)); |
|
| 1122 | 1122 | |
| 1123 | 1123 | return $counter; |
| 1124 | 1124 | } |
@@ -1130,7 +1130,7 @@ discard block |
||
| 1130 | 1130 | private function countEntriesInSearchResults($searchResults) { |
| 1131 | 1131 | $counter = 0; |
| 1132 | 1132 | |
| 1133 | - foreach($searchResults as $res) { |
|
| 1133 | + foreach ($searchResults as $res) { |
|
| 1134 | 1134 | $count = intval($this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res)); |
| 1135 | 1135 | $counter += $count; |
| 1136 | 1136 | } |
@@ -1150,7 +1150,7 @@ discard block |
||
| 1150 | 1150 | */ |
| 1151 | 1151 | public function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
| 1152 | 1152 | $limitPerPage = intval($this->connection->ldapPagingSize); |
| 1153 | - if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1153 | + if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1154 | 1154 | $limitPerPage = $limit; |
| 1155 | 1155 | } |
| 1156 | 1156 | |
@@ -1164,13 +1164,13 @@ discard block |
||
| 1164 | 1164 | $savedoffset = $offset; |
| 1165 | 1165 | do { |
| 1166 | 1166 | $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
| 1167 | - if($search === false) { |
|
| 1167 | + if ($search === false) { |
|
| 1168 | 1168 | return array(); |
| 1169 | 1169 | } |
| 1170 | 1170 | list($sr, $pagedSearchOK) = $search; |
| 1171 | 1171 | $cr = $this->connection->getConnectionResource(); |
| 1172 | 1172 | |
| 1173 | - if($skipHandling) { |
|
| 1173 | + if ($skipHandling) { |
|
| 1174 | 1174 | //i.e. result do not need to be fetched, we just need the cookie |
| 1175 | 1175 | //thus pass 1 or any other value as $iFoundItems because it is not |
| 1176 | 1176 | //used |
@@ -1181,7 +1181,7 @@ discard block |
||
| 1181 | 1181 | } |
| 1182 | 1182 | |
| 1183 | 1183 | $iFoundItems = 0; |
| 1184 | - foreach($sr as $res) { |
|
| 1184 | + foreach ($sr as $res) { |
|
| 1185 | 1185 | $findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $res)); |
| 1186 | 1186 | $iFoundItems = max($iFoundItems, $findings['count']); |
| 1187 | 1187 | unset($findings['count']); |
@@ -1197,25 +1197,25 @@ discard block |
||
| 1197 | 1197 | |
| 1198 | 1198 | // if we're here, probably no connection resource is returned. |
| 1199 | 1199 | // to make Nextcloud behave nicely, we simply give back an empty array. |
| 1200 | - if(is_null($findings)) { |
|
| 1200 | + if (is_null($findings)) { |
|
| 1201 | 1201 | return array(); |
| 1202 | 1202 | } |
| 1203 | 1203 | |
| 1204 | - if(!is_null($attr)) { |
|
| 1204 | + if (!is_null($attr)) { |
|
| 1205 | 1205 | $selection = array(); |
| 1206 | 1206 | $i = 0; |
| 1207 | - foreach($findings as $item) { |
|
| 1208 | - if(!is_array($item)) { |
|
| 1207 | + foreach ($findings as $item) { |
|
| 1208 | + if (!is_array($item)) { |
|
| 1209 | 1209 | continue; |
| 1210 | 1210 | } |
| 1211 | 1211 | $item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8'); |
| 1212 | - foreach($attr as $key) { |
|
| 1212 | + foreach ($attr as $key) { |
|
| 1213 | 1213 | $key = mb_strtolower($key, 'UTF-8'); |
| 1214 | - if(isset($item[$key])) { |
|
| 1215 | - if(is_array($item[$key]) && isset($item[$key]['count'])) { |
|
| 1214 | + if (isset($item[$key])) { |
|
| 1215 | + if (is_array($item[$key]) && isset($item[$key]['count'])) { |
|
| 1216 | 1216 | unset($item[$key]['count']); |
| 1217 | 1217 | } |
| 1218 | - if($key !== 'dn') { |
|
| 1218 | + if ($key !== 'dn') { |
|
| 1219 | 1219 | $selection[$i][$key] = $this->resemblesDN($key) ? |
| 1220 | 1220 | $this->helper->sanitizeDN($item[$key]) |
| 1221 | 1221 | : $item[$key]; |
@@ -1232,7 +1232,7 @@ discard block |
||
| 1232 | 1232 | //we slice the findings, when |
| 1233 | 1233 | //a) paged search unsuccessful, though attempted |
| 1234 | 1234 | //b) no paged search, but limit set |
| 1235 | - if((!$this->getPagedSearchResultState() |
|
| 1235 | + if ((!$this->getPagedSearchResultState() |
|
| 1236 | 1236 | && $pagedSearchOK) |
| 1237 | 1237 | || ( |
| 1238 | 1238 | !$pagedSearchOK |
@@ -1249,7 +1249,7 @@ discard block |
||
| 1249 | 1249 | * @return bool|mixed|string |
| 1250 | 1250 | */ |
| 1251 | 1251 | public function sanitizeUsername($name) { |
| 1252 | - if($this->connection->ldapIgnoreNamingRules) { |
|
| 1252 | + if ($this->connection->ldapIgnoreNamingRules) { |
|
| 1253 | 1253 | return $name; |
| 1254 | 1254 | } |
| 1255 | 1255 | |
@@ -1274,13 +1274,13 @@ discard block |
||
| 1274 | 1274 | */ |
| 1275 | 1275 | public function escapeFilterPart($input, $allowAsterisk = false) { |
| 1276 | 1276 | $asterisk = ''; |
| 1277 | - if($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { |
|
| 1277 | + if ($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { |
|
| 1278 | 1278 | $asterisk = '*'; |
| 1279 | 1279 | $input = mb_substr($input, 1, null, 'UTF-8'); |
| 1280 | 1280 | } |
| 1281 | 1281 | $search = array('*', '\\', '(', ')'); |
| 1282 | 1282 | $replace = array('\\*', '\\\\', '\\(', '\\)'); |
| 1283 | - return $asterisk . str_replace($search, $replace, $input); |
|
| 1283 | + return $asterisk.str_replace($search, $replace, $input); |
|
| 1284 | 1284 | } |
| 1285 | 1285 | |
| 1286 | 1286 | /** |
@@ -1310,13 +1310,13 @@ discard block |
||
| 1310 | 1310 | */ |
| 1311 | 1311 | private function combineFilter($filters, $operator) { |
| 1312 | 1312 | $combinedFilter = '('.$operator; |
| 1313 | - foreach($filters as $filter) { |
|
| 1313 | + foreach ($filters as $filter) { |
|
| 1314 | 1314 | if ($filter !== '' && $filter[0] !== '(') { |
| 1315 | 1315 | $filter = '('.$filter.')'; |
| 1316 | 1316 | } |
| 1317 | - $combinedFilter.=$filter; |
|
| 1317 | + $combinedFilter .= $filter; |
|
| 1318 | 1318 | } |
| 1319 | - $combinedFilter.=')'; |
|
| 1319 | + $combinedFilter .= ')'; |
|
| 1320 | 1320 | return $combinedFilter; |
| 1321 | 1321 | } |
| 1322 | 1322 | |
@@ -1352,17 +1352,17 @@ discard block |
||
| 1352 | 1352 | * @throws \Exception |
| 1353 | 1353 | */ |
| 1354 | 1354 | private function getAdvancedFilterPartForSearch($search, $searchAttributes) { |
| 1355 | - if(!is_array($searchAttributes) || count($searchAttributes) < 2) { |
|
| 1355 | + if (!is_array($searchAttributes) || count($searchAttributes) < 2) { |
|
| 1356 | 1356 | throw new \Exception('searchAttributes must be an array with at least two string'); |
| 1357 | 1357 | } |
| 1358 | 1358 | $searchWords = explode(' ', trim($search)); |
| 1359 | 1359 | $wordFilters = array(); |
| 1360 | - foreach($searchWords as $word) { |
|
| 1360 | + foreach ($searchWords as $word) { |
|
| 1361 | 1361 | $word = $this->prepareSearchTerm($word); |
| 1362 | 1362 | //every word needs to appear at least once |
| 1363 | 1363 | $wordMatchOneAttrFilters = array(); |
| 1364 | - foreach($searchAttributes as $attr) { |
|
| 1365 | - $wordMatchOneAttrFilters[] = $attr . '=' . $word; |
|
| 1364 | + foreach ($searchAttributes as $attr) { |
|
| 1365 | + $wordMatchOneAttrFilters[] = $attr.'='.$word; |
|
| 1366 | 1366 | } |
| 1367 | 1367 | $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters); |
| 1368 | 1368 | } |
@@ -1380,10 +1380,10 @@ discard block |
||
| 1380 | 1380 | private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) { |
| 1381 | 1381 | $filter = array(); |
| 1382 | 1382 | $haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0); |
| 1383 | - if($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { |
|
| 1383 | + if ($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { |
|
| 1384 | 1384 | try { |
| 1385 | 1385 | return $this->getAdvancedFilterPartForSearch($search, $searchAttributes); |
| 1386 | - } catch(\Exception $e) { |
|
| 1386 | + } catch (\Exception $e) { |
|
| 1387 | 1387 | \OCP\Util::writeLog( |
| 1388 | 1388 | 'user_ldap', |
| 1389 | 1389 | 'Creating advanced filter for search failed, falling back to simple method.', |
@@ -1393,17 +1393,17 @@ discard block |
||
| 1393 | 1393 | } |
| 1394 | 1394 | |
| 1395 | 1395 | $search = $this->prepareSearchTerm($search); |
| 1396 | - if(!is_array($searchAttributes) || count($searchAttributes) === 0) { |
|
| 1396 | + if (!is_array($searchAttributes) || count($searchAttributes) === 0) { |
|
| 1397 | 1397 | if ($fallbackAttribute === '') { |
| 1398 | 1398 | return ''; |
| 1399 | 1399 | } |
| 1400 | - $filter[] = $fallbackAttribute . '=' . $search; |
|
| 1400 | + $filter[] = $fallbackAttribute.'='.$search; |
|
| 1401 | 1401 | } else { |
| 1402 | - foreach($searchAttributes as $attribute) { |
|
| 1403 | - $filter[] = $attribute . '=' . $search; |
|
| 1402 | + foreach ($searchAttributes as $attribute) { |
|
| 1403 | + $filter[] = $attribute.'='.$search; |
|
| 1404 | 1404 | } |
| 1405 | 1405 | } |
| 1406 | - if(count($filter) === 1) { |
|
| 1406 | + if (count($filter) === 1) { |
|
| 1407 | 1407 | return '('.$filter[0].')'; |
| 1408 | 1408 | } |
| 1409 | 1409 | return $this->combineFilterWithOr($filter); |
@@ -1424,7 +1424,7 @@ discard block |
||
| 1424 | 1424 | if ($term === '') { |
| 1425 | 1425 | $result = '*'; |
| 1426 | 1426 | } else if ($allowEnum !== 'no') { |
| 1427 | - $result = $term . '*'; |
|
| 1427 | + $result = $term.'*'; |
|
| 1428 | 1428 | } |
| 1429 | 1429 | return $result; |
| 1430 | 1430 | } |
@@ -1436,7 +1436,7 @@ discard block |
||
| 1436 | 1436 | public function getFilterForUserCount() { |
| 1437 | 1437 | $filter = $this->combineFilterWithAnd(array( |
| 1438 | 1438 | $this->connection->ldapUserFilter, |
| 1439 | - $this->connection->ldapUserDisplayName . '=*' |
|
| 1439 | + $this->connection->ldapUserDisplayName.'=*' |
|
| 1440 | 1440 | )); |
| 1441 | 1441 | |
| 1442 | 1442 | return $filter; |
@@ -1454,7 +1454,7 @@ discard block |
||
| 1454 | 1454 | 'ldapAgentName' => $name, |
| 1455 | 1455 | 'ldapAgentPassword' => $password |
| 1456 | 1456 | ); |
| 1457 | - if(!$testConnection->setConfiguration($credentials)) { |
|
| 1457 | + if (!$testConnection->setConfiguration($credentials)) { |
|
| 1458 | 1458 | return false; |
| 1459 | 1459 | } |
| 1460 | 1460 | return $testConnection->bind(); |
@@ -1476,30 +1476,30 @@ discard block |
||
| 1476 | 1476 | // Sacrebleu! The UUID attribute is unknown :( We need first an |
| 1477 | 1477 | // existing DN to be able to reliably detect it. |
| 1478 | 1478 | $result = $this->search($filter, $base, ['dn'], 1); |
| 1479 | - if(!isset($result[0]) || !isset($result[0]['dn'])) { |
|
| 1479 | + if (!isset($result[0]) || !isset($result[0]['dn'])) { |
|
| 1480 | 1480 | throw new \Exception('Cannot determine UUID attribute'); |
| 1481 | 1481 | } |
| 1482 | 1482 | $dn = $result[0]['dn'][0]; |
| 1483 | - if(!$this->detectUuidAttribute($dn, true)) { |
|
| 1483 | + if (!$this->detectUuidAttribute($dn, true)) { |
|
| 1484 | 1484 | throw new \Exception('Cannot determine UUID attribute'); |
| 1485 | 1485 | } |
| 1486 | 1486 | } else { |
| 1487 | 1487 | // The UUID attribute is either known or an override is given. |
| 1488 | 1488 | // By calling this method we ensure that $this->connection->$uuidAttr |
| 1489 | 1489 | // is definitely set |
| 1490 | - if(!$this->detectUuidAttribute('', true)) { |
|
| 1490 | + if (!$this->detectUuidAttribute('', true)) { |
|
| 1491 | 1491 | throw new \Exception('Cannot determine UUID attribute'); |
| 1492 | 1492 | } |
| 1493 | 1493 | } |
| 1494 | 1494 | |
| 1495 | 1495 | $uuidAttr = $this->connection->ldapUuidUserAttribute; |
| 1496 | - if($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { |
|
| 1496 | + if ($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { |
|
| 1497 | 1497 | $uuid = $this->formatGuid2ForFilterUser($uuid); |
| 1498 | 1498 | } |
| 1499 | 1499 | |
| 1500 | - $filter = $uuidAttr . '=' . $uuid; |
|
| 1500 | + $filter = $uuidAttr.'='.$uuid; |
|
| 1501 | 1501 | $result = $this->searchUsers($filter, ['dn'], 2); |
| 1502 | - if(is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { |
|
| 1502 | + if (is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { |
|
| 1503 | 1503 | // we put the count into account to make sure that this is |
| 1504 | 1504 | // really unique |
| 1505 | 1505 | return $result[0]['dn'][0]; |
@@ -1516,7 +1516,7 @@ discard block |
||
| 1516 | 1516 | * @return bool true on success, false otherwise |
| 1517 | 1517 | */ |
| 1518 | 1518 | private function detectUuidAttribute($dn, $isUser = true, $force = false) { |
| 1519 | - if($isUser) { |
|
| 1519 | + if ($isUser) { |
|
| 1520 | 1520 | $uuidAttr = 'ldapUuidUserAttribute'; |
| 1521 | 1521 | $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
| 1522 | 1522 | } else { |
@@ -1524,7 +1524,7 @@ discard block |
||
| 1524 | 1524 | $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
| 1525 | 1525 | } |
| 1526 | 1526 | |
| 1527 | - if(($this->connection->$uuidAttr !== 'auto') && !$force) { |
|
| 1527 | + if (($this->connection->$uuidAttr !== 'auto') && !$force) { |
|
| 1528 | 1528 | return true; |
| 1529 | 1529 | } |
| 1530 | 1530 | |
@@ -1536,9 +1536,9 @@ discard block |
||
| 1536 | 1536 | // for now, supported attributes are entryUUID, nsuniqueid, objectGUID, ipaUniqueID |
| 1537 | 1537 | $testAttributes = array('entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid'); |
| 1538 | 1538 | |
| 1539 | - foreach($testAttributes as $attribute) { |
|
| 1539 | + foreach ($testAttributes as $attribute) { |
|
| 1540 | 1540 | $value = $this->readAttribute($dn, $attribute); |
| 1541 | - if(is_array($value) && isset($value[0]) && !empty($value[0])) { |
|
| 1541 | + if (is_array($value) && isset($value[0]) && !empty($value[0])) { |
|
| 1542 | 1542 | \OCP\Util::writeLog('user_ldap', |
| 1543 | 1543 | 'Setting '.$attribute.' as '.$uuidAttr, |
| 1544 | 1544 | \OCP\Util::DEBUG); |
@@ -1559,7 +1559,7 @@ discard block |
||
| 1559 | 1559 | * @return string|bool |
| 1560 | 1560 | */ |
| 1561 | 1561 | public function getUUID($dn, $isUser = true) { |
| 1562 | - if($isUser) { |
|
| 1562 | + if ($isUser) { |
|
| 1563 | 1563 | $uuidAttr = 'ldapUuidUserAttribute'; |
| 1564 | 1564 | $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
| 1565 | 1565 | } else { |
@@ -1568,15 +1568,15 @@ discard block |
||
| 1568 | 1568 | } |
| 1569 | 1569 | |
| 1570 | 1570 | $uuid = false; |
| 1571 | - if($this->detectUuidAttribute($dn, $isUser)) { |
|
| 1571 | + if ($this->detectUuidAttribute($dn, $isUser)) { |
|
| 1572 | 1572 | $uuid = $this->readAttribute($dn, $this->connection->$uuidAttr); |
| 1573 | - if( !is_array($uuid) |
|
| 1573 | + if (!is_array($uuid) |
|
| 1574 | 1574 | && $uuidOverride !== '' |
| 1575 | 1575 | && $this->detectUuidAttribute($dn, $isUser, true)) { |
| 1576 | 1576 | $uuid = $this->readAttribute($dn, |
| 1577 | 1577 | $this->connection->$uuidAttr); |
| 1578 | 1578 | } |
| 1579 | - if(is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { |
|
| 1579 | + if (is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { |
|
| 1580 | 1580 | $uuid = $uuid[0]; |
| 1581 | 1581 | } |
| 1582 | 1582 | } |
@@ -1593,19 +1593,19 @@ discard block |
||
| 1593 | 1593 | private function convertObjectGUID2Str($oguid) { |
| 1594 | 1594 | $hex_guid = bin2hex($oguid); |
| 1595 | 1595 | $hex_guid_to_guid_str = ''; |
| 1596 | - for($k = 1; $k <= 4; ++$k) { |
|
| 1596 | + for ($k = 1; $k <= 4; ++$k) { |
|
| 1597 | 1597 | $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2); |
| 1598 | 1598 | } |
| 1599 | 1599 | $hex_guid_to_guid_str .= '-'; |
| 1600 | - for($k = 1; $k <= 2; ++$k) { |
|
| 1600 | + for ($k = 1; $k <= 2; ++$k) { |
|
| 1601 | 1601 | $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2); |
| 1602 | 1602 | } |
| 1603 | 1603 | $hex_guid_to_guid_str .= '-'; |
| 1604 | - for($k = 1; $k <= 2; ++$k) { |
|
| 1604 | + for ($k = 1; $k <= 2; ++$k) { |
|
| 1605 | 1605 | $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2); |
| 1606 | 1606 | } |
| 1607 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4); |
|
| 1608 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20); |
|
| 1607 | + $hex_guid_to_guid_str .= '-'.substr($hex_guid, 16, 4); |
|
| 1608 | + $hex_guid_to_guid_str .= '-'.substr($hex_guid, 20); |
|
| 1609 | 1609 | |
| 1610 | 1610 | return strtoupper($hex_guid_to_guid_str); |
| 1611 | 1611 | } |
@@ -1622,11 +1622,11 @@ discard block |
||
| 1622 | 1622 | * @return string |
| 1623 | 1623 | */ |
| 1624 | 1624 | public function formatGuid2ForFilterUser($guid) { |
| 1625 | - if(!is_string($guid)) { |
|
| 1625 | + if (!is_string($guid)) { |
|
| 1626 | 1626 | throw new \InvalidArgumentException('String expected'); |
| 1627 | 1627 | } |
| 1628 | 1628 | $blocks = explode('-', $guid); |
| 1629 | - if(count($blocks) !== 5) { |
|
| 1629 | + if (count($blocks) !== 5) { |
|
| 1630 | 1630 | /* |
| 1631 | 1631 | * Why not throw an Exception instead? This method is a utility |
| 1632 | 1632 | * called only when trying to figure out whether a "missing" known |
@@ -1639,20 +1639,20 @@ discard block |
||
| 1639 | 1639 | * user. Instead we write a log message. |
| 1640 | 1640 | */ |
| 1641 | 1641 | \OC::$server->getLogger()->info( |
| 1642 | - 'Passed string does not resemble a valid GUID. Known UUID ' . |
|
| 1642 | + 'Passed string does not resemble a valid GUID. Known UUID '. |
|
| 1643 | 1643 | '({uuid}) probably does not match UUID configuration.', |
| 1644 | - [ 'app' => 'user_ldap', 'uuid' => $guid ] |
|
| 1644 | + ['app' => 'user_ldap', 'uuid' => $guid] |
|
| 1645 | 1645 | ); |
| 1646 | 1646 | return $guid; |
| 1647 | 1647 | } |
| 1648 | - for($i=0; $i < 3; $i++) { |
|
| 1648 | + for ($i = 0; $i < 3; $i++) { |
|
| 1649 | 1649 | $pairs = str_split($blocks[$i], 2); |
| 1650 | 1650 | $pairs = array_reverse($pairs); |
| 1651 | 1651 | $blocks[$i] = implode('', $pairs); |
| 1652 | 1652 | } |
| 1653 | - for($i=0; $i < 5; $i++) { |
|
| 1653 | + for ($i = 0; $i < 5; $i++) { |
|
| 1654 | 1654 | $pairs = str_split($blocks[$i], 2); |
| 1655 | - $blocks[$i] = '\\' . implode('\\', $pairs); |
|
| 1655 | + $blocks[$i] = '\\'.implode('\\', $pairs); |
|
| 1656 | 1656 | } |
| 1657 | 1657 | return implode('', $blocks); |
| 1658 | 1658 | } |
@@ -1666,12 +1666,12 @@ discard block |
||
| 1666 | 1666 | $domainDN = $this->getDomainDNFromDN($dn); |
| 1667 | 1667 | $cacheKey = 'getSID-'.$domainDN; |
| 1668 | 1668 | $sid = $this->connection->getFromCache($cacheKey); |
| 1669 | - if(!is_null($sid)) { |
|
| 1669 | + if (!is_null($sid)) { |
|
| 1670 | 1670 | return $sid; |
| 1671 | 1671 | } |
| 1672 | 1672 | |
| 1673 | 1673 | $objectSid = $this->readAttribute($domainDN, 'objectsid'); |
| 1674 | - if(!is_array($objectSid) || empty($objectSid)) { |
|
| 1674 | + if (!is_array($objectSid) || empty($objectSid)) { |
|
| 1675 | 1675 | $this->connection->writeToCache($cacheKey, false); |
| 1676 | 1676 | return false; |
| 1677 | 1677 | } |
@@ -1729,12 +1729,12 @@ discard block |
||
| 1729 | 1729 | $belongsToBase = false; |
| 1730 | 1730 | $bases = $this->helper->sanitizeDN($bases); |
| 1731 | 1731 | |
| 1732 | - foreach($bases as $base) { |
|
| 1732 | + foreach ($bases as $base) { |
|
| 1733 | 1733 | $belongsToBase = true; |
| 1734 | - if(mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8')-mb_strlen($base, 'UTF-8'))) { |
|
| 1734 | + if (mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8') - mb_strlen($base, 'UTF-8'))) { |
|
| 1735 | 1735 | $belongsToBase = false; |
| 1736 | 1736 | } |
| 1737 | - if($belongsToBase) { |
|
| 1737 | + if ($belongsToBase) { |
|
| 1738 | 1738 | break; |
| 1739 | 1739 | } |
| 1740 | 1740 | } |
@@ -1745,7 +1745,7 @@ discard block |
||
| 1745 | 1745 | * resets a running Paged Search operation |
| 1746 | 1746 | */ |
| 1747 | 1747 | private function abandonPagedSearch() { |
| 1748 | - if($this->connection->hasPagedResultSupport) { |
|
| 1748 | + if ($this->connection->hasPagedResultSupport) { |
|
| 1749 | 1749 | $cr = $this->connection->getConnectionResource(); |
| 1750 | 1750 | $this->invokeLDAPMethod('controlPagedResult', $cr, 0, false, $this->lastCookie); |
| 1751 | 1751 | $this->getPagedSearchResultState(); |
@@ -1763,16 +1763,16 @@ discard block |
||
| 1763 | 1763 | * @return string containing the key or empty if none is cached |
| 1764 | 1764 | */ |
| 1765 | 1765 | private function getPagedResultCookie($base, $filter, $limit, $offset) { |
| 1766 | - if($offset === 0) { |
|
| 1766 | + if ($offset === 0) { |
|
| 1767 | 1767 | return ''; |
| 1768 | 1768 | } |
| 1769 | 1769 | $offset -= $limit; |
| 1770 | 1770 | //we work with cache here |
| 1771 | - $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . intval($limit) . '-' . intval($offset); |
|
| 1771 | + $cacheKey = 'lc'.crc32($base).'-'.crc32($filter).'-'.intval($limit).'-'.intval($offset); |
|
| 1772 | 1772 | $cookie = ''; |
| 1773 | - if(isset($this->cookies[$cacheKey])) { |
|
| 1773 | + if (isset($this->cookies[$cacheKey])) { |
|
| 1774 | 1774 | $cookie = $this->cookies[$cacheKey]; |
| 1775 | - if(is_null($cookie)) { |
|
| 1775 | + if (is_null($cookie)) { |
|
| 1776 | 1776 | $cookie = ''; |
| 1777 | 1777 | } |
| 1778 | 1778 | } |
@@ -1790,11 +1790,11 @@ discard block |
||
| 1790 | 1790 | * @return bool |
| 1791 | 1791 | */ |
| 1792 | 1792 | public function hasMoreResults() { |
| 1793 | - if(!$this->connection->hasPagedResultSupport) { |
|
| 1793 | + if (!$this->connection->hasPagedResultSupport) { |
|
| 1794 | 1794 | return false; |
| 1795 | 1795 | } |
| 1796 | 1796 | |
| 1797 | - if(empty($this->lastCookie) && $this->lastCookie !== '0') { |
|
| 1797 | + if (empty($this->lastCookie) && $this->lastCookie !== '0') { |
|
| 1798 | 1798 | // as in RFC 2696, when all results are returned, the cookie will |
| 1799 | 1799 | // be empty. |
| 1800 | 1800 | return false; |
@@ -1814,8 +1814,8 @@ discard block |
||
| 1814 | 1814 | */ |
| 1815 | 1815 | private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) { |
| 1816 | 1816 | // allow '0' for 389ds |
| 1817 | - if(!empty($cookie) || $cookie === '0') { |
|
| 1818 | - $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' .intval($limit) . '-' . intval($offset); |
|
| 1817 | + if (!empty($cookie) || $cookie === '0') { |
|
| 1818 | + $cacheKey = 'lc'.crc32($base).'-'.crc32($filter).'-'.intval($limit).'-'.intval($offset); |
|
| 1819 | 1819 | $this->cookies[$cacheKey] = $cookie; |
| 1820 | 1820 | $this->lastCookie = $cookie; |
| 1821 | 1821 | } |
@@ -1842,17 +1842,17 @@ discard block |
||
| 1842 | 1842 | */ |
| 1843 | 1843 | private function initPagedSearch($filter, $bases, $attr, $limit, $offset) { |
| 1844 | 1844 | $pagedSearchOK = false; |
| 1845 | - if($this->connection->hasPagedResultSupport && ($limit !== 0)) { |
|
| 1845 | + if ($this->connection->hasPagedResultSupport && ($limit !== 0)) { |
|
| 1846 | 1846 | $offset = intval($offset); //can be null |
| 1847 | 1847 | \OCP\Util::writeLog('user_ldap', |
| 1848 | 1848 | 'initializing paged search for Filter '.$filter.' base '.print_r($bases, true) |
| 1849 | - .' attr '.print_r($attr, true). ' limit ' .$limit.' offset '.$offset, |
|
| 1849 | + .' attr '.print_r($attr, true).' limit '.$limit.' offset '.$offset, |
|
| 1850 | 1850 | \OCP\Util::DEBUG); |
| 1851 | 1851 | //get the cookie from the search for the previous search, required by LDAP |
| 1852 | - foreach($bases as $base) { |
|
| 1852 | + foreach ($bases as $base) { |
|
| 1853 | 1853 | |
| 1854 | 1854 | $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset); |
| 1855 | - if(empty($cookie) && $cookie !== "0" && ($offset > 0)) { |
|
| 1855 | + if (empty($cookie) && $cookie !== "0" && ($offset > 0)) { |
|
| 1856 | 1856 | // no cookie known, although the offset is not 0. Maybe cache run out. We need |
| 1857 | 1857 | // to start all over *sigh* (btw, Dear Reader, did you know LDAP paged |
| 1858 | 1858 | // searching was designed by MSFT?) |
@@ -1865,18 +1865,18 @@ discard block |
||
| 1865 | 1865 | $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset); |
| 1866 | 1866 | //still no cookie? obviously, the server does not like us. Let's skip paging efforts. |
| 1867 | 1867 | //TODO: remember this, probably does not change in the next request... |
| 1868 | - if(empty($cookie) && $cookie !== '0') { |
|
| 1868 | + if (empty($cookie) && $cookie !== '0') { |
|
| 1869 | 1869 | // '0' is valid, because 389ds |
| 1870 | 1870 | $cookie = null; |
| 1871 | 1871 | } |
| 1872 | 1872 | } |
| 1873 | - if(!is_null($cookie)) { |
|
| 1873 | + if (!is_null($cookie)) { |
|
| 1874 | 1874 | //since offset = 0, this is a new search. We abandon other searches that might be ongoing. |
| 1875 | 1875 | $this->abandonPagedSearch(); |
| 1876 | 1876 | $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
| 1877 | 1877 | $this->connection->getConnectionResource(), $limit, |
| 1878 | 1878 | false, $cookie); |
| 1879 | - if(!$pagedSearchOK) { |
|
| 1879 | + if (!$pagedSearchOK) { |
|
| 1880 | 1880 | return false; |
| 1881 | 1881 | } |
| 1882 | 1882 | \OCP\Util::writeLog('user_ldap', 'Ready for a paged search', \OCP\Util::DEBUG); |
@@ -1893,7 +1893,7 @@ discard block |
||
| 1893 | 1893 | * So we added "&& !empty($this->lastCookie)" to this test to ignore pagination |
| 1894 | 1894 | * if we don't have a previous paged search. |
| 1895 | 1895 | */ |
| 1896 | - } else if($this->connection->hasPagedResultSupport && $limit === 0 && !empty($this->lastCookie)) { |
|
| 1896 | + } else if ($this->connection->hasPagedResultSupport && $limit === 0 && !empty($this->lastCookie)) { |
|
| 1897 | 1897 | // a search without limit was requested. However, if we do use |
| 1898 | 1898 | // Paged Search once, we always must do it. This requires us to |
| 1899 | 1899 | // initialize it with the configured page size. |