3 namespace BookStack\Auth\Access\Ldap;
5 use BookStack\Exceptions\LdapException;
6 use BookStack\Exceptions\LdapFailedBindException;
8 use Illuminate\Support\Facades\Log;
10 class LdapConnectionManager
12 protected array $connectionCache = [];
15 * Attempt to start and bind to a new LDAP connection as the configured LDAP system user.
17 public function startSystemBind(LdapConfig $config): LdapConnection
19 // Incoming options are string|false
20 $dn = $config->get('dn');
21 $pass = $config->get('pass');
23 $isAnonymous = ($dn === false || $pass === false);
26 return $this->startBind($dn ?: null, $pass ?: null, $config);
27 } catch (LdapFailedBindException $exception) {
28 $msg = ($isAnonymous ? trans('errors.ldap_fail_anonymous') : trans('errors.ldap_fail_authed'));
29 throw new LdapFailedBindException($msg);
34 * Attempt to start and bind to a new LDAP connection.
35 * Will attempt against multiple defined fail-over hosts if set in the provided config.
37 * Throws a LdapFailedBindException error if the bind connected but failed.
38 * Otherwise, generic LdapException errors would be thrown.
40 * @throws LdapException
42 public function startBind(?string $dn, ?string $password, LdapConfig $config): LdapConnection
44 // Check LDAP extension in installed
45 if (!function_exists('ldap_connect') && config('app.env') !== 'testing') {
46 throw new LdapException(trans('errors.ldap_extension_not_installed'));
49 // Disable certificate verification.
50 // This option works globally and must be set before a connection is created.
51 if ($config->get('tls_insecure')) {
52 LdapConnection::setGlobalOption(LDAP_OPT_X_TLS_REQUIRE_CERT, LDAP_OPT_X_TLS_NEVER);
55 $serverDetails = $config->getServers();
56 $lastException = null;
58 foreach ($serverDetails as $server) {
60 $connection = $this->startServerConnection($server['host'], $server['port'], $config);
61 } catch (LdapException $exception) {
62 $lastException = $exception;
67 $bound = $connection->bind($dn, $password);
69 throw new LdapFailedBindException('Failed to perform LDAP bind');
71 } catch (ErrorException $exception) {
72 Log::error('LDAP bind error: ' . $exception->getMessage());
73 $lastException = new LdapException('Encountered error during LDAP bind');
77 $this->connectionCache[$server['host'] . ':' . $server['port']] = $connection;
85 * Attempt to start a server connection from the provided details.
86 * @throws LdapException
88 protected function startServerConnection(string $host, int $port, LdapConfig $config): LdapConnection
90 if (isset($this->connectionCache[$host . ':' . $port])) {
91 return $this->connectionCache[$host . ':' . $port];
94 /** @var LdapConnection $ldapConnection */
95 $ldapConnection = app()->make(LdapConnection::class, [$host, $port]);
97 if (!$ldapConnection) {
98 throw new LdapException(trans('errors.ldap_cannot_connect'));
101 // Set any required options
102 if ($config->get('version')) {
103 $ldapConnection->setVersion($config->get('version'));
106 // Start and verify TLS if it's enabled
107 if ($config->get('start_tls')) {
109 $tlsStarted = $ldapConnection->startTls();
110 } catch (ErrorException $exception) {
115 throw new LdapException('Could not start TLS connection');
119 return $ldapConnection;