Skip to content

Core\Ldap::open

Opens an LDAP directory at a URL your program gives, with the account your program gives. An LDAP directory is a server with users and groups, such as Active Directory.

Use it when the directory is not known in advance, for example when each customer sets up their own. Core\Ldap::open takes the url, the user and the password, and logs in. It returns a Core\Ldap\Connection, which is closed when the request ends.

The connection is encrypted. ldaps:// encrypts it from the start. tls: Tls::None sends the password and every answer as plain text. It works only for a host in [capabilities.ldap] cleartext. If a TLS connection fails, the error names the cause and the fix (TLS connections).

The url cannot be tainted (a value from a request that is not checked yet), because the password is sent to that host. An empty password, or a password with a zero byte (\x00), throws an error before anything is sent.

Good to know: [capabilities.ldap] open in nvs.toml lists the hosts a program may open. Any other host throws a RuntimeError. A private address, such as 10.0.0.5, also throws, unless the operator allows it.

Parameters

  • $settings{ url: string, user?: tainted string, password?: secret tainted string, tls?: Core\Ldap::Tls, tlsCa?: string, tlsPin?: string|array<string>, tlsVerifyHost?: bool, tlsVerify?: bool, tlsMinVersion?: string, timeout?: Core\Time\Duration, base?: Core\Ldap\Dn|string }The directory to open and the account to log in with.
    • urlstringOne ldaps:// or ldap:// URL. It cannot be tainted, because the password is sent to this host.
    • usertainted stringThe account to log in as, as a DN, user@example.test or EXAMPLE\user. Left out, the connection does not log in.
    • passwordsecret tainted stringThe account's password. An empty password throws before anything is sent. A password with a zero byte (\x00) throws before it is sent.
    • tlsTlsTls::Required, the default, encrypts the connection. Tls::None sends everything as plain text, and needs the host in [capabilities.ldap] cleartext.
    • tlsCastringPEM certificates to trust for this connection, in place of [tls.client] roots. It needs the tls.anchors capability for the host.
    • tlsPinstring|array<string>One or more sha256// hashes of the server's public key. A server with a matching key is accepted, and its certificate chain is not checked. It needs the tls.pin capability for the host.
    • tlsVerifyHostboolfalse checks the certificate chain, but not the host name in the certificate. It needs the tls.any_name capability for the host. The default is true.
    • tlsVerifyboolfalse checks neither the certificate chain nor the host name. The connection is still encrypted. Another machine on the network can still pretend to be the server and read the password. Use a real certificate and tlsCa where you can. It needs the tls.insecure capability for the host. The default is true.
    • tlsMinVersionstringThe lowest TLS version to accept: "1.2" or "1.3". It can only raise [tls.client] min_version, so it needs no capability.
    • timeoutDurationHow long one operation may take. Left out, it is 30 seconds.
    • baseDn|stringThe DN a search starts from when it does not give its own base. Text cannot be tainted.

Return value

Returns Core\Ldap\Connection. A Core\Ldap\Connection. It is closed when the request ends.

Errors

  • RuntimeErrorurl is not an ldap:// or ldaps:// URL, ldap.open does not grant its host, or the host's address is in a private range that net.internal does not allow.
  • LogicErrorA TLS setting makes the certificate check less strict, and its grant does not list the host. tlsCa needs tls.anchors, tlsPin needs tls.pin, tlsVerifyHost: false needs tls.any_name and tlsVerify: false needs tls.insecure. It is also thrown when tlsMinVersion is not "1.2" or "1.3", or is lower than [tls.client] min_version. Nothing is connected.
  • Core\Ldap\LdapErrorThe server did not respond, or the server did not accept the user and password. It is also thrown for tls: Core\Ldap::Tls::None when ldap.cleartext does not list the host. The error's kind property says which case it is.

Changelog

VersionChange
0.0.6A failed TLS connection names its cause, who can fix it and how, as TLS connection errors explains. For an expired certificate, the message gives the date it expired and the time on this host's clock. For a wrong pin, it gives the sha256// hash of the server's public key.
0.0.6Core\Ldap::open has the TLS settings of Core\Http\Client: tlsCa, tlsPin, tlsVerifyHost, tlsVerify and tlsMinVersion. A setting that makes the certificate check less strict needs the host in a list under [capabilities.tls], or the call throws a LogicError. The [ldap.<name>], [mail.<name>], [imap.<name>] and [cache.shared] blocks have the same keys as a [db.<name>] block, such as tls_verify.

Examples

The examples below run with this configuration file.

nvs.toml
# The program may open a directory on `127.0.0.1` only.
[capabilities.ldap]
open = ["127.0.0.1"]

Open a directory the program names

<?nvs
// Shows how to open a directory at an address the program chooses.
// `Core\Ldap::open` needs the `ldap.open` capability for the host in the URL.
// No directory server runs at this address, so the call throws a `Core\Ldap\LdapError`.
try {
var $directory = Core\Ldap::open({
url: 'ldaps://127.0.0.1:1',
user: 'CN=Reader,CN=Users,DC=example,DC=test',
password: 'Example-Pw1',
timeout: Core\Time\Duration::seconds(5),
});
echo 'Logged in as ', $directory->whoami(), "\n";
} catch (Core\Ldap\LdapError $error) {
echo "The directory did not answer.\n";
}
Output
The directory did not answer.

A host the operator did not allow

<?nvs
// Shows what happens when a program opens a directory on a host the operator did
// not allow. `[capabilities.ldap] open` in `nvs.toml` lists the hosts. This
// program may open `127.0.0.1` only, so the call throws a `RuntimeError`.
try {
Core\Ldap::open({
url: 'ldaps://dc1.example.test',
user: 'CN=Reader,CN=Users,DC=example,DC=test',
password: 'Example-Pw1',
});
echo "The directory is open.\n";
} catch (RuntimeError $error) {
echo $error->message, "\n";
}
Output
Core\Ldap::open needs the capability `ldap.open` for dc1.example.test, which is not granted
help: add `open = ["dc1.example.test"]` under `[capabilities.ldap]` in nvs.toml

Plain text needs its own grant

<?nvs
// Shows that a connection without encryption needs its own permission.
// `Tls::None` sends the password as plain text. The host must also be listed in
// `[capabilities.ldap] cleartext` in `nvs.toml`, and here it is not.
try {
Core\Ldap::open({
url: 'ldap://127.0.0.1:1',
user: 'CN=Reader,CN=Users,DC=example,DC=test',
password: 'Example-Pw1',
tls: Core\Ldap::Tls::None,
});
echo "The directory is open.\n";
} catch (Core\Ldap\LdapError $error) {
// This prints `EncryptionRequired`.
echo $error->kind == Core\Ldap\LdapError::Kind::EncryptionRequired ? 'EncryptionRequired' : 'another reason', "\n";
}
Output
EncryptionRequired