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