Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions apps/user_ldap/lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ private function validateConfiguration() {

/**
* Connects and Binds to LDAP
*
* @throws ServerNotAvailableException
*/
private function establishConnection() {
if(!$this->configuration->ldapConfigurationActive) {
Expand Down Expand Up @@ -545,26 +547,20 @@ private function establishConnection() {
|| $this->getFromCache('overrideMainServer'));
$isBackupHost = (trim($this->configuration->ldapBackupHost) !== "");
$bindStatus = false;
$error = -1;
try {
if (!$isOverrideMainServer) {
$this->doConnect($this->configuration->ldapHost,
$this->configuration->ldapPort);
$bindStatus = $this->bind();
$error = $this->ldap->isResource($this->ldapConnectionRes) ?
$this->ldap->errno($this->ldapConnectionRes) : -1;
}
if($bindStatus === true) {
return $bindStatus;
}
return $this->bind();
} catch (ServerNotAvailableException $e) {
if(!$isBackupHost) {
throw $e;
}
}

//if LDAP server is not reachable, try the Backup (Replica!) Server
if($isBackupHost && ($error !== 0 || $isOverrideMainServer)) {
if($isBackupHost || $isOverrideMainServer) {
$this->doConnect($this->configuration->ldapBackupHost,
$this->configuration->ldapBackupPort);
$this->bindResult = [];
Expand Down
53 changes: 50 additions & 3 deletions apps/user_ldap/tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @package OCA\User_LDAP\Tests
*/
class ConnectionTest extends \Test\TestCase {
/** @var \OCA\User_LDAP\ILDAPWrapper */
/** @var \OCA\User_LDAP\ILDAPWrapper|\PHPUnit_Framework_MockObject_MockObject */
protected $ldap;

/** @var Connection */
Expand Down Expand Up @@ -110,7 +110,7 @@ public function testUseBackupServer() {
->method('setOption')
->will($this->returnValue(true));

$this->ldap->expects($this->exactly(3))
$this->ldap->expects($this->exactly(2))
->method('connect')
->will($this->returnValue('ldapResource'));

Expand All @@ -119,7 +119,7 @@ public function testUseBackupServer() {
->will($this->returnValue(0));

// Not called often enough? Then, the fallback to the backup server is broken.
$this->connection->expects($this->exactly(4))
$this->connection->expects($this->exactly(3))
->method('getFromCache')
->with('overrideMainServer')
->will($this->onConsecutiveCalls(false, false, true, true));
Expand All @@ -145,6 +145,53 @@ public function testUseBackupServer() {
$this->connection->init();
}

public function testDontUseBackupServerOnFailedAuth() {
$mainHost = 'ldap://nixda.ldap';
$backupHost = 'ldap://fallback.ldap';
$config = [
'ldapConfigurationActive' => true,
'ldapHost' => $mainHost,
'ldapPort' => 389,
'ldapBackupHost' => $backupHost,
'ldapBackupPort' => 389,
'ldapAgentName' => 'uid=agent',
'ldapAgentPassword' => 'SuchASecret'
];

$this->connection->setIgnoreValidation(true);
$this->connection->setConfiguration($config);

$this->ldap->expects($this->any())
->method('isResource')
->will($this->returnValue(true));

$this->ldap->expects($this->any())
->method('setOption')
->will($this->returnValue(true));

$this->ldap->expects($this->once())
->method('connect')
->will($this->returnValue('ldapResource'));

$this->ldap->expects($this->any())
->method('errno')
->will($this->returnValue(49));

$this->connection->expects($this->any())
->method('getFromCache')
->with('overrideMainServer')
->willReturn(false);

$this->connection->expects($this->never())
->method('writeToCache');

$this->ldap->expects($this->exactly(1))
->method('bind')
->willReturn(false);

$this->connection->init();
}

public function testBindWithInvalidCredentials() {
// background: Bind with invalid credentials should return false
// and not throw a ServerNotAvailableException.
Expand Down