Skip to content

Commit 0291178

Browse files
authored
LDAP: Allow TLS certificate checking policy to be configured (#180)
1 parent 79ad994 commit 0291178

4 files changed

Lines changed: 42 additions & 2 deletions

File tree

.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ LDAP_AUTH_URL="ldap://127.0.0.1"
5959
LDAP_DN_PATTERN="mail=%u"
6060
LDAP_MAIL_ATTRIBUTE="mail"
6161
LDAP_AUTH_USER_AUTOCREATE=false
62+
# See https://www.php.net/manual/en/ldap.constants.php#constant.ldap-opt-x-tls-require-cert
63+
# Allowed values are: never, hard, demand, allow or try.
64+
# "try" is the default if left unspecified
65+
LDAP_CERTIFICATE_CHECKING_STRATEGY="try"
6266

6367
# Do we enable caldav and carddav ?
6468
CALDAV_ENABLED=true

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ LDAP_AUTH_URL="ldap://127.0.0.1"
193193
LDAP_DN_PATTERN="mail=%u"
194194
LDAP_MAIL_ATTRIBUTE="mail"
195195
LDAP_AUTH_USER_AUTOCREATE=true # false by default
196+
LDAP_CERTIFICATE_CHECKING_STRATEGY="try" # try by default.
196197
```
197198
198199
> Ex: for [Zimbra LDAP](https://zimbra.github.io/adminguide/latest/#zimbra_ldap_service), you might want to use the `zimbraMailDeliveryAddress` attribute to retrieve the principal user email:

config/services.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ services:
3535
$LDAPDnPattern: "%env(LDAP_DN_PATTERN)%"
3636
$LDAPMailAttribute: "%env(LDAP_MAIL_ATTRIBUTE)%"
3737
$autoCreate: "%env(bool:LDAP_AUTH_USER_AUTOCREATE)%"
38+
$LDAPCertificateCheckingStrategy: "%env(LDAP_CERTIFICATE_CHECKING_STRATEGY)%"
3839

3940
# controllers are imported separately to make sure services can be injected
4041
# as action arguments even if you don't extend any base controller class
@@ -70,4 +71,4 @@ when@dev:
7071

7172
when@test:
7273
services:
73-
Symfony\Component\HttpKernel\Profiler\Profiler: '@profiler'
74+
Symfony\Component\HttpKernel\Profiler\Profiler: '@profiler'

src/Services/LDAPAuth.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,23 @@ final class LDAPAuth extends AbstractBasic
6262
*/
6363
private $autoCreate;
6464

65+
66+
/**
67+
* Indicates what to do with certificate.
68+
* see https://www.php.net/manual/en/ldap.constants.php#constant.ldap-opt-x-tls-require-cert
69+
*/
70+
private $cert_checking_strat;
71+
6572
/**
6673
* Creates the backend object.
6774
*/
68-
public function __construct(ManagerRegistry $doctrine, Utils $utils, string $LDAPAuthUrl, string $LDAPDnPattern, string $LDAPMailAttribute, bool $autoCreate)
75+
public function __construct(ManagerRegistry $doctrine, Utils $utils, string $LDAPAuthUrl, string $LDAPDnPattern, string $LDAPMailAttribute, bool $autoCreate, string $LDAPCertificateCheckingStrategy)
6976
{
7077
$this->LDAPAuthUrl = $LDAPAuthUrl;
7178
$this->LDAPDnPattern = $LDAPDnPattern;
7279
$this->LDAPMailAttribute = $LDAPMailAttribute ?? 'mail';
7380
$this->autoCreate = $autoCreate;
81+
$this->cert_checking_strat = $LDAPCertificateCheckingStrategy ?? "try";
7482

7583
$this->doctrine = $doctrine;
7684
$this->utils = $utils;
@@ -86,6 +94,32 @@ public function __construct(ManagerRegistry $doctrine, Utils $utils, string $LDA
8694
*/
8795
protected function ldapOpen($username, $password)
8896
{
97+
switch ($this->cert_checking_strat) {
98+
case 'never':
99+
$cert_strategy = LDAP_OPT_X_TLS_NEVER;
100+
break;
101+
case 'hard':
102+
$cert_strategy = LDAP_OPT_X_TLS_HARD;
103+
break;
104+
case 'demand':
105+
$cert_strategy = LDAP_OPT_X_TLS_DEMAND;
106+
break;
107+
case 'allow':
108+
$cert_strategy = LDAP_OPT_X_TLS_ALLOW;
109+
break;
110+
case 'try':
111+
$cert_strategy = LDAP_OPT_X_TLS_TRY;
112+
break;
113+
default:
114+
error_log('Invalid certificate checking strategy: ' . $this->cert_checking_strat);
115+
return false;
116+
}
117+
118+
if (false === ldap_set_option(null, LDAP_OPT_X_TLS_REQUIRE_CERT, $cert_strategy)) {
119+
error_log('LDAP Error (ldap_set_option with '.$cert_strategy.'): failed');
120+
121+
return false;
122+
}
89123
try {
90124
$ldap = ldap_connect($this->LDAPAuthUrl);
91125
} catch (\Exception $e) {

0 commit comments

Comments
 (0)