LDAP directory for mail clients

Bongo can answer LDAP searches so a mail client can look up the people of its own domain while the user types an address (Thunderbird, Apple Mail and other clients that have an LDAP address book). It is a small read-only LDAPv3 search service. It is off by default.

It is not a directory server. It cannot be written to, it has no schema and no tree beyond the one below, and it is a different thing from the LDAP authentication of docs/ldap.md (that is Bongo asking your directory; this is your clients asking Bongo). The same accounts are also offered as the CardDAV “Directory” address book (docs/caldav-carddav.md); the two views share one source and one hiding rule.

Turning it on

ldap_directory {
  enabled = true
  # port = 636            # LDAPS (TLS from the first byte). Default 636.
  # starttls_port = 389   # plain port that must upgrade with StartTLS. Default off.
}

Every key is read at start-up; changing one needs a restart. The listener runs on server and dev nodes only (any other role refuses the block). On a dev node with no port set it listens on 10636.

Key Default Meaning
enabled false Switches the listener on.
port 636 The LDAPS port. 0 (or leaving the key out) means the default, 636 (10636 on a dev node); -1 turns LDAPS off. A plain 0 cannot mean “off” because an unset key reads as 0.
starttls_port 0 (off) A plain TCP port on which the client must run StartTLS before it may bind or search.
size_limit 100 At most this many entries per search (1 to 1000). The client’s own limit can only lower it.
max_connections 200 Concurrent connections per listener. Lower than the other listeners’ 1024: directory lookups are short and few clients keep a session open.
max_connections_per_ip 50 Concurrent connections from one source address (the shared default of the other listeners, so an office behind one NAT address is not cut off; four addresses can fill the listener, which the short unbound timeout below keeps brief for anyone without a password).
requests_per_second 10 Requests per connection per second (a burst of twice that is allowed). Over the rate a request answers busy (51) without being processed, and a connection that keeps going is closed.
idle_timeout 5m How long a bound connection may wait between requests. A connection that has not bound (or whose last bind failed) waits at most request_timeout, so connections that never authenticate do not hold a slot for long. An Abandon request does not count as activity.
request_timeout 10s How long reading the rest of a request, writing an answer, a TLS handshake and listing a domain’s accounts may each take. A listing that takes longer answers unavailable (52).

At least one of the two ports must be set, and the ports must not clash with any other listener. /admin/services lists the listener or listeners with their TLS mode and says when a changed key waits for a restart.

The certificate is the one IMAP and POP3 serve (cluster.tls, or the ACME certificate when web.tls.acme is on), even when IMAP and POP3 are off. A renewed certificate is picked up the same way as for them. Port 636 is privileged on Linux, like 993: run the service with the capability to bind it or put it on a higher port.

Mixed versions: an older node does not know the ldap_directory block and refuses to start with it. Upgrade every node before adding the block.

What a client can do

  • Bind with a full email address, or a DN whose first part is mail=address (mail=ann@example.org,ou=people,dc=example,dc=org), and the account password or an app password. The bind is checked exactly like an IMAP login: the same checks, the same lockout (5 failures in 15 minutes block the source address), accounts that sign in through your LDAP directory work as for IMAP, and an app password never forgives earlier failures. A wrong password, an unknown user and a blocked address all answer invalidCredentials (49) with the same text. There is no anonymous bind (inappropriateAuthentication (48)), no bind with a name and an empty password (unwillingToPerform (53)) and no SASL (authMethodNotSupported (7)).
  • Search, after a bind. Without a bind every search, the root DSE included, answers insufficientAccessRights (50).
  • Nothing else. Add, modify, delete, modify DN and compare answer unwillingToPerform (53); an extended operation other than StartTLS answers protocolError (2); abandon is accepted and ignored; a critical request control answers unavailableCriticalExtension (12) (non-critical controls, such as paged results, are ignored).

On the StartTLS port a bind or search before the upgrade answers confidentialityRequired (13). On both ports a client that sends bytes after the StartTLS request without waiting for the answer is disconnected.

The account must still be enabled. Every request re-reads it: a disabled or deleted account, a changed password, a revoked app password, or the “CalDAV and CardDAV” switch on the user’s page (the one this service uses; there is no separate LDAP switch) ends the session with a Notice of Disconnection.

What a search sees

Results are the accounts of the bound user’s own primary-address domain, read from the stored profile on every search and never from the search base. Accounts the CardDAV Directory book hides are not offered here either: a disabled account, any account of a disabled domain, and any local address whose account is hidden even when your LDAP directory lists it. Resource and shared inbox accounts are listed. The listing is cached per domain for 60 seconds, so typing does not rescan the accounts or query your directory on every key.

The tree has one naming context, the user’s domain as dc parts (dc=example,dc=org), and one unit under it:

dc=example,dc=org
  ou=people
    mail=ann@example.org,ou=people,dc=example,dc=org

A search base must be the domain DN, ou=people under it, or empty. Any other base, another domain’s DN included, answers noSuchObject (32). A base-scope search of the domain or unit returns no entries; one-level and subtree searches return the accounts. An empty base with base scope is the root DSE; an empty base with one-level or subtree scope (Apple Mail and Outlook allow the search base to be left blank) searches the user’s ou=people unit, so it returns the same accounts.

The root DSE (after a bind) holds supportedLDAPVersion 3 and namingContexts (the user’s domain). StartTLS is not advertised there (supportedExtension is absent): the root DSE needs a bind, and a bind needs TLS already, so clients are configured for StartTLS or LDAPS rather than discovering it.

Entries carry these attributes only:

Attribute Value
mail the address
cn the display name, or the address when there is none
displayName the display name (absent when there is none)
sn the last word of the display name, or the whole name, or the part of the address before the @
givenName the words before the last one (only when the name has two or more)
objectClass top, person, organizationalPerson, inetOrgPerson

An empty attribute list or * returns all of them, 1.1 none, an unknown name is ignored, and “types only” is honoured.

Filters: equality, substring, presence, and, or and not; approximate match is treated as equality. Matching ignores case. Ordering matches (>=, <=), extensible matches and unknown attributes are “undefined” in the sense of RFC 4511, not errors, so a client that adds an odd clause inside an or still gets the other branches. A filter may nest ten deep, have 64 nodes, at most 8 substring parts of at most 256 bytes; a larger one answers unwillingToPerform (53). Over size_limit the server sends that many entries, then sizeLimitExceeded (4); clients treat the entries as a normal result.

Client setup

Thunderbird (Settings, Composition, Addressing, “Directory Server”, or Address Book, New, “New LDAP Directory”):

  • Name: any. Hostname: your mail host (for example mail.example.org).
  • Base DN: dc=example,dc=org (your domain).
  • Port number: 636. Tick “Use secure connection (SSL)”. Thunderbird speaks LDAPS, so no StartTLS port is needed.
  • Bind DN: your full address, ann@example.org. Thunderbird asks for the password when it first searches; use the account password or an app password.
  • In the composition settings choose the directory for address autocomplete.

Apple Mail (macOS: Contacts, Settings, Accounts, +, “Other Contacts Account”, “LDAP”):

  • Server address: your mail host. Search base: dc=example,dc=org (or leave it empty).
  • Scope: Subtree. Authentication: Password (simple). Username: your full address. Password: the account password or an app password.
  • Port: 636 with “Use SSL” on.
  • In Mail, Settings, Composing, tick “Automatically complete addresses” and “Use LDAP / Directories”.

Other clients (Outlook through an LDAP address book plug-in, Evolution, KMail) work with the same values: LDAPS, port 636, base DN your domain, simple bind with your address. A client that can only do plain LDAP on port 389 needs starttls_port and must support StartTLS; if it cannot, it cannot use this service, because no bind or search is allowed on an unencrypted connection.

If the certificate is the self-signed fallback (no certificate configured), the client will warn; install a real certificate (cluster.tls, or ACME).

Operations

  • Logs use the component ldap-directory. They carry the user id, the number of entries, the result code, the number of nodes in the filter and the source address. Passwords, the bind name (a client that swapped its fields could have put a password in it), the base DN and the search text are never logged.
  • Limits are per node and in memory (the per-address cap, the lockout and the request rate), so on a cluster each node counts for itself. There is no PROXY protocol support: behind a TCP proxy the per-address cap and the lockout see the proxy’s address.
  • Parser. The BER decoder is hand-written in internal/ldapd/ber.go. The go-asn1-ber package under the LDAP client has no nesting limit, builds an object per element and takes its size limit from a global that the client used for directory authentication also reads, so it was not used for untrusted input. A request is at most 16 KiB, 16 levels deep and 512 elements, with a definite length only; anything else ends the connection with a Notice of Disconnection. No new dependency was added.
  • Staleness. A new or disabled account shows up within about a minute; an entry removed from your upstream directory can stay for up to that long plus the upstream cache.

View this page's source on GitHub