Skip to content
Tutorials Center
My learning

Topics

Lesson 56 of 79 ยท Application Layer: Names and Web

LDAP and Directory Services

Learn what a directory service is, how LDAP organizes entries in a tree with distinguished names, how bind and search work, how filters match, and how to secure it.

A company with 5,000 employees needs one place that answers questions such as "does this username exist?", "what is this person's e-mail address?", "which groups is she in?" and "is this password correct?". Every application could keep its own copy of the answers, but then each employee would have 20 accounts, and when somebody leaves, 20 systems would need cleaning up. A directory service is one central, read-mostly database designed to hold exactly this kind of information, so that mail servers, VPNs, wikis, printers and Linux logins can all ask it.

LDAP, the Lightweight Directory Access Protocol, is the standard way to talk to a directory. It started as a simpler alternative to the heavy X.500 directory protocols of the OSI era (hence "lightweight") and is now the common language of OpenLDAP, Microsoft Active Directory, 389 Directory Server and many identity products. In this lesson you will learn the directory data model, the main operations (especially bind and search), how search filters are written, what LDAP looks like on the wire, and how to use it safely.

What a directory is (and is not)

A directory looks like a database but is optimized differently. It is built for many reads and few writes, for fast lookups by attribute, and for a hierarchical shape. It is not meant for transactions with joins, such as an order system. You can compare it with DNS, another distributed, hierarchical, read-heavy naming system: DNS maps names to addresses, while a directory maps identities to rich attribute sets.

What lives in a directory

People Name, e-mail, phone, password hash
Groups Who belongs to which team
Devices Computers, printers, services
Structure Departments and locations

One source of truth for identity

1 / 2

The directory model

An LDAP directory is a tree, the Directory Information Tree (DIT). Each node is an entry, and each entry is a set of attributes, each with one or more values.

TEXT
dc=example,dc=com                               (the root, the "base")
โ”œโ”€โ”€ ou=people
โ”‚   โ”œโ”€โ”€ uid=ada       cn=Ada Lovelace, [email protected]
โ”‚   โ””โ”€โ”€ uid=grace     cn=Grace Hopper, [email protected]
โ””โ”€โ”€ ou=groups
    โ””โ”€โ”€ cn=admins     member=uid=ada,ou=people,dc=example,dc=com

The key terms:

Term Meaning Example
Entry One object in the tree A person, a group
Attribute A named property with one or more values mail, cn, member
objectClass Says what kind of entry it is and which attributes it may or must have inetOrgPerson, groupOfNames
RDN (relative distinguished name) The entry's name inside its parent uid=ada
DN (distinguished name) The RDN followed by all ancestors; unique in the whole directory uid=ada,ou=people,dc=example,dc=com
Base DN The DN where a search or a whole directory starts dc=example,dc=com
Schema The rules for object classes and attribute types

Common attribute abbreviations are dc (domain component), ou (organizational unit), cn (common name), sn (surname), uid (user id) and o (organization). A DN reads from the most specific name on the left to the root on the right, the opposite of a file path but like a host name in DNS (www.example.com). The usual convention maps a domain name to dc parts: example.com becomes dc=example,dc=com.

Operations

LDAP is a request/response protocol. The client opens a TCP connection, sends numbered requests, and the server answers each one. The main operations are:

Operation Purpose
Bind Authenticate; set who the connection acts as
Search Find entries and return chosen attributes
Compare Test whether an entry has an attribute value (for example, is this password correct?)
Add Create a new entry
Modify Add, replace or delete attribute values of an entry
Delete Remove a (leaf) entry
Modify DN Rename or move an entry
Unbind Close the session
Abandon Cancel a running request
Extended Open-ended extras, such as StartTLS or password change

Bind: logging in

Bind is the first step. There are three kinds:

  • Anonymous bind: no credentials; the server may allow read access to some public data.
  • Simple bind: a DN and a password. The password is sent as plain text inside the protocol, so a simple bind must only run over an encrypted connection.
  • SASL bind: a pluggable framework supporting Kerberos (GSSAPI), certificates (EXTERNAL) and others.

A typical application login works like this: the application first binds as a low-privileged service account, searches for the user's entry by uid to find the DN, then binds again as that user with the password the person typed. If the second bind succeeds, the password is right. The directory never gives the application the stored password hash.

Search: the main event

A search has these parameters:

  • Base DN: where to start.
  • Scope: base (just the base entry), one (its direct children only), or sub (the base and everything below it).
  • Filter: which entries match.
  • Attributes: which attributes to return (everything if empty).
  • Limits for size and time, and alias dereferencing.

Filters use a prefix notation in parentheses, defined in RFC 4515:

Filter Meaning
(uid=ada) The attribute equals a value
(cn=Ada*) Wildcard match
(mail=*) The attribute is present
(&(objectClass=person)(ou=sales)) AND
(|(uid=ada)(uid=grace)) OR
(!(title=intern)) NOT

The program below implements a tiny directory with a scope-aware search and a real recursive filter parser, so you can see how DNs, scopes and filters interact.

Python A miniature LDAP search

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the third search: scope one from the root returns only the two organizational units, not the people below them. The fourth uses NOT to exclude the intern. Real servers add indexes so that equality and prefix filters on common attributes (uid, mail) are fast even with millions of entries.

LDAP on the wire

LDAP messages are encoded with ASN.1 BER (Basic Encoding Rules): each item is a tag, a length and a value (TLV). Every message is wrapped in an LDAPMessage sequence with a message ID that lets the client match answers to requests, even when many requests are in flight on one connection. This binary encoding is the kind of presentation-layer work described in Session and Presentation Layers.

The next program encodes a real simple BindRequest by hand, decodes it again as a tree, and decodes two server answers: success and "invalid credentials" (result code 49).

Python A BindRequest in BER

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the hex: the password appears in the bytes as ordinary text. That is why a simple bind over an unencrypted connection leaks credentials to anyone who can capture the traffic.

Security

LDAP's default port is 389, with 636 for LDAPS (LDAP inside TLS from the first byte). On port 389 a client can also upgrade the connection with the StartTLS extended operation. Always do one of them before a simple bind. In addition:

  • Disallow anonymous reads of sensitive attributes, and use access control lists so that people can read their own entry but not everyone's password hashes.
  • Store passwords as salted, slow hashes (for example or better, PBKDF2 or argon2 where supported), never in plain text. See cryptography for networks.
  • Prevent LDAP injection. If an application builds a filter by pasting user input into a string, a user can type *)(uid=*))(|(uid=* and change its meaning. Escape the special characters * ( ) \ and NUL in filter values, or use a library function for it.
  • Use replication (multi-provider or primary/replica) so that a failure of one server does not stop every login.

Directory services also appear in other forms. Active Directory combines LDAP, Kerberos authentication and DNS; it is the reason LDAP is on port 389 in nearly every corporate network (and port 3268 for its global catalog). Cloud identity providers expose similar data through other APIs, but many apps still speak LDAP to them.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is a DN in LDAP?

    The distinguished name is the entry's RDN followed by its ancestors up to the root. It is unique across the directory.
  2. 2. A search with base ou=people,dc=example,dc=com and scope one returns what?

    Scope base means the entry itself, one means its immediate children, and sub means the entire subtree including the base.
  3. 3. Which filter finds entries that are people but not interns?

    LDAP filters use prefix notation: an AND with two subfilters, the second wrapped in NOT. The other forms are not valid filters or have another meaning.
  4. 4. How does a typical application check a user's password against LDAP?

    The bind operation is the authentication check. A search first finds the right DN (the user types a username, not a DN).
  5. 5. Why should a simple bind use LDAPS or StartTLS?

    Simple bind has no protection of its own; anyone capturing the packets could read the password. TLS encrypts the connection.

Summary

  • A directory service is a central, read-optimized store of identity data; LDAP (TCP 389, LDAPS 636) is the standard protocol to query and change it.
  • Data forms a tree of entries; each entry has attributes and an objectClass, and is named by a unique DN built from RDNs.
  • Core operations are bind (authenticate), search (base DN, scope, filter, attributes), add, modify, delete and unbind.
  • Filters use prefix notation with &, |, ! and wildcards; messages are BER-encoded TLV structures with message IDs.
  • Protect it with TLS before any simple bind, access controls, salted password hashes, filter escaping and replication.

Next: other application layer protocols such as SNMP and syslog also manage devices and services, and security fundamentals covers authentication in general.