Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit 456ce84

Browse files
Merge pull request #120 from melanger/patch-6
LDAP startTLS support (port 389)
2 parents 9e0dce6 + 8971128 commit 456ce84

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

config-templates/module_perun.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
*/
3333
//'ldap.username' => '_proxy-idp',
3434
//'ldap.password' => 'password'
35+
36+
/**
37+
* Whether to use startTLS on port 389. Defaults to false.
38+
* SSL/TLS is always used for ldaps: regardless of this setting.
39+
*/
40+
//'ldap.enable_tls' => true,
3541

3642
/**
3743
* Perun group name to eduPersonEntitlement mapping. Mapping is according to the spec in

lib/AdapterLdap.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class AdapterLdap extends Adapter
3030
const LDAP_USER = 'ldap.username';
3131
const LDAP_PASSWORD = 'ldap.password';
3232
const LDAP_BASE = 'ldap.base';
33+
const LDAP_TLS = 'ldap.enable_tls';
3334
const PERUN_FACILITY_ID = 'perunFacilityId';
3435
const CN = 'cn';
3536
const DESCRIPTION = 'description';
@@ -57,8 +58,9 @@ public function __construct($configFileName = null)
5758
$ldapUser = $conf->getString(self::LDAP_USER, null);
5859
$ldapPassword = $conf->getString(self::LDAP_PASSWORD, null);
5960
$this->ldapBase = $conf->getString(self::LDAP_BASE);
61+
$ldapEnableTLS = $conf->getBoolean(self::LDAP_TLS, false);
6062

61-
$this->connector = new LdapConnector($ldapHostname, $ldapUser, $ldapPassword);
63+
$this->connector = new LdapConnector($ldapHostname, $ldapUser, $ldapPassword, $ldapEnableTLS);
6264
$this->fallbackAdapter = new AdapterRpc();
6365
}
6466

lib/LdapConnector.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,21 @@ class LdapConnector
3131
private $hostname;
3232
private $user;
3333
private $password;
34+
private $enableTLS;
3435

3536
/**
3637
* LdapConnector constructor.
3738
* @param $hostname
3839
* @param $user
3940
* @param $password
41+
* @param $enableTLS
4042
*/
41-
public function __construct($hostname, $user, $password)
43+
public function __construct($hostname, $user, $password, $enableTLS = false)
4244
{
4345
$this->hostname = $hostname;
4446
$this->user = $user;
4547
$this->password = $password;
48+
$this->enableTLS = $enableTLS;
4649
}
4750

4851
/**
@@ -108,6 +111,13 @@ protected function search($base, $filter, $attributes = null)
108111
}
109112

110113
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
114+
115+
// Enable TLS, if needed
116+
if ($this->enableTLS && stripos($this->hostname, "ldaps:") === false) {
117+
if (!@ldap_start_tls($conn)) {
118+
throw new Exception('Unable to force TLS on Perun LDAP');
119+
}
120+
}
111121

112122
if (ldap_bind($conn, $this->user, $this->password) === false) {
113123
throw new Exception('Unable to bind user to the Perun LDAP, ' . $this->hostname);

0 commit comments

Comments
 (0)