The idea: a tree you search, and a bind that logs you in
LDAP, the Lightweight Directory Access Protocol, is how programs read a company's directory: its people, groups and service accounts. The directory is a tree of entries, called the DIT (directory information tree). A client does two main things with it. It binds, which means it logs in as some entry. And it searches, which means it asks for the entries in part of the tree that pass a filter. Many web apps, mail servers and VPNs check passwords this way, against OpenLDAP or Active Directory.
DNs and entries
Each entry has a distinguished name (DN). It is written from the entry up to the root, with commas between the parts: uid=alice,ou=people,dc=example,dc=com. The first part, uid=alice, is the RDN, the entry's name among its siblings. dc is a domain component, ou an organizational unit, cn a common name. An entry also has attributes: objectClass says what kind of thing it is (inetOrgPerson, groupOfNames), and other attributes hold the data (cn: Alice Liddell, mail, and userPassword, a salted hash the server keeps to itself).
Messages and message IDs
LDAP runs over one TCP connection. The client sends requests, and every request carries a messageID: 1, 2, 3 and so on. Every answer repeats that number, so the client knows which request it answers. The canvas log writes each message as → #3 SearchRequest or ← #3 SearchResultDone. A search can get many answers with the same ID (one entry each) and always ends with one SearchResultDone. UnbindRequest gets no answer at all: the server just closes the connection.
Bind and TLS: StartTLS on 389, LDAPS on 636
A simple bind sends a DN and a password exactly as typed. So it must run inside TLS. There are two ways:
| StartTLS | LDAPS | |
|---|---|---|
| Port | 389 | 636 |
| How TLS starts | the connection starts in plain text; the client sends an ExtendedRequest named 1.3.6.1.4.1.1466.20037, gets success, then runs the TLS handshake | TLS from the first byte, before any LDAP message |
| Standard | RFC 4511 and RFC 4513 | not in the RFCs, but supported everywhere |
A server can refuse a simple bind without TLS. OpenLDAP does that with security simple_bind=128 and answers confidentialityRequired (13) (Demo 4: no TLS (13)). Note the order: the password has already crossed the network by the time the refusal comes back. So the client must be set up for TLS; the server's rule only catches the mistake.
Search: base, scope and filter
A SearchRequest names a base DN (where to start), a scope and a filter, plus the attributes it wants back. The scope picks the entries the filter is tested on:
With the filter (objectClass=*), which every entry passes, the three scopes under ou=people return 1, 2 and 3 entries (Demo 5: scope base, one, sub). Filters are written in prefix form (RFC 4515), each test in parentheses:
| Filter | Means |
|---|---|
(uid=alice) | uid equals alice (case does not matter for uid) |
(objectClass=*) | the attribute is present: every entry has an objectClass |
(cn=Al*) | cn starts with Al |
(&(objectClass=person)(uid=alice)) | both tests are true |
(|(uid=alice)(uid=bob)) | at least one is true |
(!(uid=alice)) | not |
The server answers with one SearchResultEntry per match (its DN and the attributes asked for) and then a SearchResultDone with a result code. A search that finds nothing still ends with success (0) and zero entries (Demo 3: unknown user). A base that does not exist gives noSuchObject (32) and a matchedDN, the part of the name that does exist (pick base: ou=staff (none) and press Search).
How an app checks a password: search, then bind
A user types alice, not her DN. So a web app checks a login in two steps (Demo 1: log in as alice):
- Bind as the app's own service account (
cn=webapp,ou=services,dc=example,dc=com) and searchou=people, scope sub, for(&(objectClass=person)(uid=alice)). Exactly one entry must come back; its DN isuid=alice,ou=people,dc=example,dc=com. - Bind as that DN with the typed password.
success (0)means the password is right;invalidCredentials (49)means it is wrong (Demo 2: wrong password (49)).
The app never reads userPassword and never compares passwords itself. The server does the check against its own hash. A few rules keep this safe:
- Give the browser the same answer for an unknown user and for a wrong password, so nobody can test which user names exist.
- Refuse an empty password before binding. A bind with a DN and an empty password is an "unauthenticated bind" (RFC 4513 §5.1.2); some servers answer
successto it, which would let anyone in. - Build the filter from the typed name with the special characters escaped (
*,(,),\, NUL become\2a,\28,\29,\5c,\00), or a user name like*changes what the filter means.
Result codes on this page
| resultCode | When |
|---|---|
success (0) | the bind worked; the search ran (even with 0 entries) |
referral (10) | the base is held by another server; the answer carries its LDAP URL |
confidentialityRequired (13) | the server wants TLS for this operation |
noSuchObject (32) | the base DN does not exist |
invalidCredentials (49) | wrong password (or no such DN) on a bind |
Referrals: part of the tree on another server
One tree can be spread over several servers. On this page ou=partners is a referral object: an entry whose ref attribute holds the URL ldap://ldap.partner.example/ou=partners,dc=example,dc=com. When a subtree search reaches it, the server does not search below it. It sends a SearchResultReference with that URL next to the normal entries (Demo 6: referral). When the base itself is a referral, the whole answer is a SearchResultDone with referral (10) and the URL. Either way the client may follow it: open a connection to the other server and send the search again there. The app on this page does, and finds uid=pat.
See also Kerberos for a login that never sends the password to the service (Active Directory uses both Kerberos and LDAP), DNS Resolution for another tree of names read from right to left, HTTPS Connection for the TLS handshake that StartTLS and LDAPS run, and Password Hashing for how the server should store userPassword.
What the page leaves out
SASL binds (GSSAPI/Kerberos, EXTERNAL with a client certificate, SCRAM), the TCP handshake and TLS messages one by one, Modify, Add, Delete, ModifyDN and Compare operations, access control rules (OpenLDAP olcAccess), the schema, aliases and derefAliases, size and time limits, paged results and other controls, replication between servers, and group checks such as memberOf after the login.