Skip to content

Commit 414d4bf

Browse files
committed
Fix executeRead when connection is reset because of a timeout
Signed-off-by: Côme Chilliet <[email protected]>
1 parent 1c0e871 commit 414d4bf

File tree

1 file changed

+24
-36
lines changed

1 file changed

+24
-36
lines changed

apps/user_ldap/lib/Access.php

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public function readAttribute($dn, $attr, $filter = 'objectClass=*') {
218218
$values = [];
219219
$isRangeRequest = false;
220220
do {
221-
$result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults);
221+
$result = $this->executeRead($dn, $attrToRead, $filter, $maxResults);
222222
if (is_bool($result)) {
223223
// when an exists request was run and it was successful, an empty
224224
// array must be returned
@@ -260,17 +260,12 @@ public function readAttribute($dn, $attr, $filter = 'objectClass=*') {
260260
/**
261261
* Runs an read operation against LDAP
262262
*
263-
* @param resource $cr the LDAP connection
264-
* @param string $dn
265-
* @param string $attribute
266-
* @param string $filter
267-
* @param int $maxResults
268263
* @return array|bool false if there was any error, true if an exists check
269264
* was performed and the requested DN found, array with the
270265
* returned data on a successful usual operation
271266
* @throws ServerNotAvailableException
272267
*/
273-
public function executeRead($cr, $dn, $attribute, $filter, $maxResults) {
268+
public function executeRead(string $dn, string $attribute, string $filter, int $maxResults) {
274269
try {
275270
$this->initPagedSearch($filter, $dn, [$attribute], $maxResults, 0);
276271
} catch (NoMoreResults $e) {
@@ -280,7 +275,7 @@ public function executeRead($cr, $dn, $attribute, $filter, $maxResults) {
280275
return false;
281276
}
282277
$dn = $this->helper->DNasBaseParameter($dn);
283-
$rr = @$this->invokeLDAPMethod('read', $cr, $dn, $filter, [$attribute]);
278+
$rr = @$this->invokeLDAPMethod('read', $dn, $filter, [$attribute]);
284279
if (!$this->ldap->isResource($rr)) {
285280
if ($attribute !== '') {
286281
//do not throw this message on userExists check, irritates
@@ -289,18 +284,18 @@ public function executeRead($cr, $dn, $attribute, $filter, $maxResults) {
289284
//in case an error occurs , e.g. object does not exist
290285
return false;
291286
}
292-
if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) {
287+
if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $rr) === 1)) {
293288
$this->logger->debug('readAttribute: ' . $dn . ' found', ['app' => 'user_ldap']);
294289
return true;
295290
}
296-
$er = $this->invokeLDAPMethod('firstEntry', $cr, $rr);
291+
$er = $this->invokeLDAPMethod('firstEntry', $rr);
297292
if (!$this->ldap->isResource($er)) {
298293
//did not match the filter, return false
299294
return false;
300295
}
301296
//LDAP attributes are not case sensitive
302297
$result = \OCP\Util::mb_array_change_key_case(
303-
$this->invokeLDAPMethod('getAttributes', $cr, $er), MB_CASE_LOWER, 'UTF-8');
298+
$this->invokeLDAPMethod('getAttributes', $er), MB_CASE_LOWER, 'UTF-8');
304299

305300
return $result;
306301
}
@@ -381,10 +376,10 @@ public function setPassword($userDN, $password) {
381376
}
382377
try {
383378
// try PASSWD extended operation first
384-
return @$this->invokeLDAPMethod('exopPasswd', $cr, $userDN, '', $password) ||
385-
@$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password);
379+
return @$this->invokeLDAPMethod('exopPasswd', $userDN, '', $password) ||
380+
@$this->invokeLDAPMethod('modReplace', $userDN, $password);
386381
} catch (ConstraintViolationException $e) {
387-
throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ') . $e->getMessage(), $e->getCode());
382+
throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ') . $e->getMessage(), (int)$e->getCode());
388383
}
389384
}
390385

@@ -1092,26 +1087,23 @@ public function countObjects($limit = null, $offset = null) {
10921087
*/
10931088

10941089
/**
1090+
* @param mixed[] $arguments
10951091
* @return mixed
10961092
* @throws \OC\ServerNotAvailableException
10971093
*/
1098-
private function invokeLDAPMethod() {
1099-
$arguments = func_get_args();
1100-
$command = array_shift($arguments);
1101-
$cr = array_shift($arguments);
1094+
private function invokeLDAPMethod(string $command, ...$arguments) {
1095+
if ($command == 'controlPagedResultResponse') {
1096+
// php no longer supports call-time pass-by-reference
1097+
// thus cannot support controlPagedResultResponse as the third argument
1098+
// is a reference
1099+
throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.');
1100+
}
11021101
if (!method_exists($this->ldap, $command)) {
11031102
return null;
11041103
}
1105-
array_unshift($arguments, $cr);
1106-
// php no longer supports call-time pass-by-reference
1107-
// thus cannot support controlPagedResultResponse as the third argument
1108-
// is a reference
1104+
array_unshift($arguments, $this->connection->getConnectionResource());
11091105
$doMethod = function () use ($command, &$arguments) {
1110-
if ($command == 'controlPagedResultResponse') {
1111-
throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.');
1112-
} else {
1113-
return call_user_func_array([$this->ldap, $command], $arguments);
1114-
}
1106+
return call_user_func_array([$this->ldap, $command], $arguments);
11151107
};
11161108
try {
11171109
$ret = $doMethod();
@@ -1172,8 +1164,7 @@ private function executeSearch(
11721164
return false;
11731165
}
11741166

1175-
$sr = $this->invokeLDAPMethod('search', $cr, $base, $filter, $attr);
1176-
// cannot use $cr anymore, might have changed in the previous call!
1167+
$sr = $this->invokeLDAPMethod('search', $base, $filter, $attr);
11771168
$error = $this->ldap->errno($this->connection->getConnectionResource());
11781169
if (!$this->ldap->isResource($sr) || $error !== 0) {
11791170
$this->logger->error('Attempt for Paging? ' . print_r($pagedSearchOK, true), ['app' => 'user_ldap']);
@@ -1308,7 +1299,7 @@ private function count(
13081299
* @throws ServerNotAvailableException
13091300
*/
13101301
private function countEntriesInSearchResults($sr): int {
1311-
return (int)$this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $sr);
1302+
return (int)$this->invokeLDAPMethod('countEntries', $sr);
13121303
}
13131304

13141305
/**
@@ -1349,7 +1340,6 @@ public function search(
13491340
return [];
13501341
}
13511342
[$sr, $pagedSearchOK] = $search;
1352-
$cr = $this->connection->getConnectionResource();
13531343

13541344
if ($skipHandling) {
13551345
//i.e. result do not need to be fetched, we just need the cookie
@@ -1359,7 +1349,7 @@ public function search(
13591349
return [];
13601350
}
13611351

1362-
$findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $sr));
1352+
$findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $sr));
13631353
$iFoundItems = max($iFoundItems, $findings['count']);
13641354
unset($findings['count']);
13651355

@@ -1994,8 +1984,7 @@ private function abandonPagedSearch() {
19941984
if ($this->lastCookie === '') {
19951985
return;
19961986
}
1997-
$cr = $this->connection->getConnectionResource();
1998-
$this->invokeLDAPMethod('controlPagedResult', $cr, 0, false);
1987+
$this->invokeLDAPMethod('controlPagedResult', 0, false);
19991988
$this->getPagedSearchResultState();
20001989
$this->lastCookie = '';
20011990
}
@@ -2082,7 +2071,7 @@ private function initPagedSearch(
20822071
$this->abandonPagedSearch();
20832072
}
20842073
$pagedSearchOK = true === $this->invokeLDAPMethod(
2085-
'controlPagedResult', $this->connection->getConnectionResource(), $limit, false
2074+
'controlPagedResult', $limit, false
20862075
);
20872076
if ($pagedSearchOK) {
20882077
$this->logger->debug('Ready for a paged search', ['app' => 'user_ldap']);
@@ -2102,7 +2091,6 @@ private function initPagedSearch(
21022091
// be returned.
21032092
$pageSize = (int)$this->connection->ldapPagingSize > 0 ? (int)$this->connection->ldapPagingSize : 500;
21042093
$pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult',
2105-
$this->connection->getConnectionResource(),
21062094
$pageSize, false);
21072095
}
21082096

0 commit comments

Comments
 (0)