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.

The directory tree: dc=example,dc=com at the top; below it ou=people, ou=groups, ou=services and ou=partners; under ou=people the entries uid=alice and uid=bob. Next to uid=alice its full DN uid=alice,ou=people,dc=example,dc=com, built by reading the path from the entry up to the root. ou=partners is a referral to ldap.partner.example.
An entry's DN is its own name followed by the names of every entry above it.

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:

StartTLSLDAPS
Port389636
How TLS startsthe 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 handshakeTLS from the first byte, before any LDAP message
StandardRFC 4511 and RFC 4513not 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.

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:

Three copies of the ou=people subtree. Scope base: only ou=people is in scope. Scope one: uid=alice and uid=bob, not ou=people. Scope sub: ou=people, uid=alice and uid=bob.
Scope base is one entry, one is the children only, sub is the whole subtree with the base.

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:

FilterMeans
(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):

  1. Bind as the app's own service account (cn=webapp,ou=services,dc=example,dc=com) and search ou=people, scope sub, for (&(objectClass=person)(uid=alice)). Exactly one entry must come back; its DN is uid=alice,ou=people,dc=example,dc=com.
  2. 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)).
Time-down sequence between the web app and ldap.example.com: StartTLS and TLS, BindRequest as cn=webapp and success, SearchRequest for uid=alice, SearchResultEntry with alice's DN and SearchResultDone, BindRequest as uid=alice's DN with the typed password, BindResponse success or invalidCredentials 49, UnbindRequest.
The search finds the DN; the second bind is the actual password check.

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 success to 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

resultCodeWhen
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.

On the mind map

Open the Web & Security mind mapWeb & SecurityCrypto building blocks: keys, hashes, signaturesCrypto building blocksStoring passwords: slow salted hash, never plaintextStoring passwordsBrowser rules: which request carries whatBrowser rulesRequest attacks: trick a browser or a serverRequest attacksLogin & identity: prove who you areLogin & identityOWASP Top 10 2025: common flaws, then the fixOWASP Top 10 2025OWASP: supply chain & ops: dependencies, logs and error pathsOWASP: supply chain & opsOAuth 2.0 with PKCEOAuth 2.0 with PKCEapp gets a scoped token, not the passwordOpenID ConnectOpenID ConnectID token says who; check iss, aud, nonceSAML Single Sign-OnSAML Single Sign-OnSP checks the IdP's signed assertionJWT vs session cookieJWT vs session cookieJWT: signed, stateless, hard to revokeKerberosKerberosTGT from the KDC, then a ticket per serviceLDAP directorySearch for the DN, then bind as the userMutual TLSMutual TLSboth sides show and prove a certificateEmail SPF, DKIM, DMARCEmail SPF, DKIM, DMARCDMARC: SPF or DKIM must align with From:TOTP two-factor codesTOTP two-factor codesHMAC(secret, time/30s) → 6 digitsRBAC vs ABACRBAC vs ABACRoles grant rights; attributes decide liveBell-LaPadula and BibaBell-LaPadula and BibaNo read up, no write down; Biba flips itGRANT and REVOKEGRANT and REVOKEMariaDB REVOKE never cascadesOpen the Web & Security mind mapWeb & Securityprove who you areLogin & identityOAuth 2.0 with PKCEOAuth 2.0 with PKCEapp gets a scoped token, not the passwordOpenID ConnectOpenID ConnectID token says who; check iss, aud, nonceSAML Single Sign-OnSAML Single Sign-OnSP checks the IdP's signed assertionJWT vs session cookieJWT vs session cookieJWT: signed, stateless, hard to revokeKerberosKerberosTGT from the KDC, then a ticket per serviceLDAP directorySearch for the DN, then bind as the userMutual TLSMutual TLSboth sides show and prove a certificateEmail SPF, DKIM, DMARCEmail SPF, DKIM, DMARCDMARC: SPF or DKIM must align with From:TOTP two-factor codesTOTP two-factor codesHMAC(secret, time/30s) → 6 digitsRBAC vs ABACRBAC vs ABACRoles grant rights; attributes decide liveBell-LaPadula and BibaBell-LaPadula and BibaNo read up, no write down; Biba flips itGRANT and REVOKEGRANT and REVOKEMariaDB REVOKE never cascades
This page on the Web & Security mind map · 45 pages in 7 branches · open it to see the whole map and test yourself in recall mode.