keepcalculator

06 Numbers and Powers

LDAP: A Practical Guide to Directory Services

Card No. 06·41266 · Updated · Standard formula, shown below

LDAP (Lightweight Directory Access Protocol) is the standard protocol for accessing and managing directory services—specialized databases optimized for reading, searching, and updating information like user accounts, groups, and network resources. This guide explains what LDAP is, how directories are structured, how to write queries, and how to troubleshoot common issues. It is intended for IT administrators, developers, and anyone who needs to integrate applications with a central identity store.

What Is a Directory Service and Where Does LDAP Fit?

A directory service is a shared, network-accessible database that stores information in a hierarchical, tree-like structure. Unlike a relational database, it is designed for frequent reads and occasional writes, making it ideal for authentication and lookup tasks. The most common directory service is Microsoft Active Directory (AD), but open-source options like OpenLDAP and ApacheDS are also widely used.

LDAP is the protocol that clients use to talk to these directory services. It defines how to connect, authenticate, search, and modify entries. LDAP itself is not a database; it is a protocol that operates over TCP/IP, typically on port 389 for plaintext and 636 for LDAPS (LDAP over SSL/TLS). The protocol is defined in RFC 4511, and the data model is defined in related RFCs.

Understanding the LDAP Data Model

LDAP data is organized as a tree, called the Directory Information Tree (DIT). Each node in the tree is an entry, which contains one or more attributes. Entries are identified by a globally unique Distinguished Name (DN), which is a sequence of Relative Distinguished Names (RDNs) that trace the path from the root to the entry.

For example, in the DN cn=jdoe,ou=People,dc=example,dc=com:

  • dc=example,dc=com is the base DN, representing the domain.
  • ou=People is an organizational unit that groups user entries.
  • cn=jdoe is the common name of the user entry.

Each entry has an objectClass attribute that defines its type and the required/optional attributes it can have. For instance, a user entry typically has the objectClass inetOrgPerson, which requires sn (surname) and cn, and can have mail, telephoneNumber, etc. Attributes can be single-valued or multi-valued, and they have a defined syntax (e.g., case-insensitive string, integer, binary).

How LDAP Queries Work

LDAP queries are performed using the search operation. You specify a base DN to start the search, a scope (base, onelevel, or subtree), and a filter that selects entries. The server returns matching entries with the requested attributes.

The filter syntax is defined in RFC 4515. The basic form is (attribute=value), and you can combine filters with & (AND), | (OR), and ! (NOT). For example:

  • (objectClass=inetOrgPerson) – all user entries.
  • (&(objectClass=inetOrgPerson)(mail=*@example.com)) – users with an email in the example.com domain.
  • (|(cn=John)(sn=Smith)) – entries with cn=John or sn=Smith.
  • (!(&(objectClass=person)(uid=admin))) – entries that are not the admin user, but this is rarely used.

You can use wildcards: * matches zero or more characters, and ? matches exactly one character. For example, (cn=Jo*) matches all common names starting with “Jo�.

Common LDAP Operations and Tools

Besides search, LDAP supports add, delete, modify, and bind (authenticate). These operations are used to manage entries programmatically or via command-line tools.

Common command-line tools include:

  • ldapsearch – for searching entries (part of OpenLDAP client tools).
  • ldapadd and ldapmodify – for adding and modifying entries.
  • ldapdelete – for removing entries.
  • ldapwhoami – for testing authentication.

For example, a simple search query might be:

ldapsearch -x -H ldap://ldap.example.com -b "dc=example,dc=com" "(uid=jdoe)" mail

This connects to the LDAP server, binds anonymously (-x), searches the base DN, and returns the mail attribute for the entry with uid=jdoe.

How to Choose Between LDAP and Other Directories

If you are starting a project, you might wonder whether to use LDAP, Active Directory, or a relational database. Here are some general guidelines:

  • Use LDAP when you need a lightweight, standards-based directory for authentication and authorization, especially in Linux/Unix environments or when you need to integrate with many applications that support LDAP.
  • Use Active Directory when you are in a Windows-centric environment and need features like Group Policy, Kerberos, and integration with Microsoft services. AD uses LDAP as its access protocol, so LDAP clients can still connect.
  • Use a relational database when your data is highly relational, requires complex transactions, or needs frequent updates. LDAP is not designed for complex queries or high write throughput.

In practice, many organizations use LDAP as the backend for a single sign-on (SSO) system, with applications binding to the directory to verify user credentials.

Common Problems and Troubleshooting

LDAP issues often fall into a few categories. Here are common ones and how to resolve them:

  • Connection refused or timeout: Check that the server is running and the port is open. Use telnet ldap.example.com 389 to test connectivity. If using LDAPS, ensure the certificate is trusted.
  • Bind failures: The distinguished name or password is wrong. Verify the DN is correct (e.g., cn=admin,dc=example,dc=com) and that the password is not expired.
  • Search returns no results: Check the base DN and scope. The filter may be too restrictive; try a broader filter like (objectClass=*) to see if any entries exist.
  • Attribute not returned: You may need to request the attribute explicitly or check if the entry has that attribute. Also, some attributes are operational (like createTimestamp) and require a special request.
  • Referrals: If the directory is distributed, the server may return a referral to another server. Your client may need to follow referrals automatically (e.g., -C in ldapsearch).

Security Considerations

LDAP directories often contain sensitive data, so security is critical. Always use LDAPS (LDAP over TLS) or StartTLS to encrypt traffic, especially when transmitting credentials. Never bind with an admin account from an application; use a service account with the least privileges needed. Also, be careful with filters that allow user input—LDAP injection is possible if you do not escape special characters like *, (, ), and \. Use proper escaping or parameterized queries in your LDAP library.

Additionally, set appropriate access controls on the directory server to restrict who can read or modify entries. Regular audits of logs can help detect unauthorized access attempts.

Frequently Asked Questions

What is the difference between LDAP and Active Directory?

LDAP is a protocol, while Active Directory is a directory service that implements LDAP (among other protocols). Active Directory adds features like Kerberos authentication, Group Policy, and a schema for Windows-specific objects. You can connect to AD using LDAP, but you cannot use AD features over LDAP alone.

How do I find the base DN for my LDAP server?

Often the base DN matches your domain name, e.g., dc=example,dc=com. You can also ask your administrator or use a tool like ldapsearch with a null base DN and scope base to see the root DSE, which often includes namingContexts that list the base DNs.

What is a bind DN and why do I need it?

A bind DN is the distinguished name of an entry that you use to authenticate to the directory. It is like a username. For example, cn=admin,dc=example,dc=com. You need it to perform operations that require authentication, such as modifying entries or searching with access to protected attributes.

Can I use LDAP to authenticate users on a website?

Yes, many web applications support LDAP authentication. You would typically configure the LDAP server URL, base DN, and a search filter to find the user, then attempt a bind with the user’s DN and password. This is a common approach for single sign-on in enterprise environments.

What does the filter (&(objectClass=user)(memberOf=CN=Group,OU=Groups,DC=example,DC=com)) do?

This filter finds all entries with objectClass user that are members of the specified group. It is often used in AD to retrieve users in a particular group. Note that in AD, the memberOf attribute is multi-valued and contains the DNs of groups the user belongs to.

Is LDAP still relevant today?

Yes, LDAP remains widely used, especially in enterprise environments for authentication and directory services. While newer protocols like SCIM and OAuth are popular for specific use cases, LDAP is still the backbone of many identity management systems and is supported by virtually all major platforms.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →