Last modified: October 10, 2024

This article is written in: 🇺🇸

LDAP (Lightweight Directory Access Protocol)

LDAP is a protocol used to access and manage directory information over an IP network. It is open, vendor-neutral, and an industry standard. LDAP is commonly used for centralized authentication, where user credentials and permissions are managed in a single directory and applied across multiple systems and applications.

Understanding LDAP Concepts

Directory

Entries and Attributes

Distinguished Names (DN)

Component Description
uid=jdoe User ID
ou=users Organizational Unit
dc=example,dc=com Domain Components representing example.com

Schema

Network Topology Diagram

The network topology illustrates how the LDAP server interacts with multiple client hosts across the network.

+--------------------+
                 |    LDAP Server     |
                 | (ldap.example.com) |
                 +----------+---------+
                            |
             ---------------------------------
             |               |               |
     +-------+-----+   +-----+-------+   +---+-------+
     |             |   |             |   |           |
+------v------+ +----v-----+     +-----v----+     +----v-----+
| Client Host | | Client   |     | Client   |     | Client   |
|    (Web)    | | Host     |     | Host     |     | Host     |
|             | | (Email)  |     | (SSH)    |     | (FTP)    |
+-------------+ +----------+     +----------+     +----------+

LDAP Directory Structure

LDAP directories are organized hierarchically in a structure known as the Directory Information Tree (DIT).

Visual Representation of a DIT:

(Root)
                      |
           +----------+----------+
           |                     |
        dc=com                 dc=org
           |                     |
     +-----+-----+               |
     |           |               |
  dc=example   dc=company       ...
     |
 +---+---+
 |       |
ou=users ou=groups
 |         |
 |         +----------------+
 |                          |
+--+--+                   +---+---+
|     |                   |       |
uid=alice uid=bob       cn=admins cn=users

User Authentication Sequence Diagram

User                Client Host             LDAP Server
|                       |                      |
|---Login Request------>|                      |
|                       |---Authenticate------>|
|                       |                      |
|                       |<--Authentication-----|
|<--Access Granted------|                      |

  1. User sends login request to Client Host.
  2. Client Host sends authentication request to LDAP Server.
  3. LDAP Server processes the request and sends back the authentication result.
  4. Client Host grants or denies access to the User based on the result.

Common LDAP Operations

LDAP defines a set of operations that clients can perform on the directory.

Bind

Example Command:

ldapwhoami -x -D "uid=jdoe,ou=users,dc=example,dc=com" -W

Options:

Option Description
-x Use simple authentication.
-D Bind DN (the user's distinguished name).
-W Prompt for the password.

Expected Output:

Enter LDAP Password:
dn:uid=jdoe,ou=users,dc=example,dc=com

Search & Compare

Example Search Command:

ldapsearch -x -b "dc=example,dc=com" "(uid=jdoe)"

Option Description
-x Use simple authentication.
-b Base DN to search.
"(uid=jdoe)" Search filter.

Expected Output:

# extended LDIF
#
# LDAPv3
# base <dc=example,dc=com> with scope subtree
# filter: (uid=jdoe)
# requesting: ALL
#

# jdoe, users, example.com
dn: uid=jdoe,ou=users,dc=example,dc=com
uid: jdoe
cn: John Doe
sn: Doe
mail: jdoe@example.com
...

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1

Add, Delete & Modify

Example Add Command:

ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f new_user.ldif

Example Delete Command:

ldapdelete -x -D "cn=admin,dc=example,dc=com" -W "uid=jdoe,ou=users,dc=example,dc=com"

Unbind

LDAP Search Filters

Search filters control what entries are returned in a search operation.

Syntax:

Examples:

Find users with uid 'jdoe':

(uid=jdoe)

Find entries that are persons and have an email:

(&(objectClass=person)(mail=*))

Find users not in the 'admins' group:

(!(memberOf=cn=admins,ou=groups,dc=example,dc=com))

LDAP Tools and Utilities

Command-Line Tools

  1. ldapsearch: Search for entries.

ldapsearch -x -b "dc=example,dc=com" "(objectClass=*)"

  1. ldapadd/ldapmodify: Add or modify entries.

ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f entry.ldif

  1. ldapdelete: Delete entries.

ldapdelete -x -D "cn=admin,dc=example,dc=com" -W "uid=jdoe,ou=users,dc=example,dc=com"

  1. ldapwhoami: Display the DN bound to the session.

ldapwhoami -x -D "uid=jdoe,ou=users,dc=example,dc=com" -W

GUI-Based Tools

  1. Apache Directory Studio is an Eclipse-based LDAP browser and editor, offering a user-friendly interface for browsing and managing LDAP directories.
  2. phpLDAPadmin serves as a web-based LDAP administration tool, allowing administrators to manage directory entries through a convenient browser interface.
  3. JXplorer is a Java-based LDAP client, providing cross-platform support for accessing and managing LDAP directories with various customization options.

Implementing LDAP for Centralized Authentication

Centralized authentication via LDAP allows multiple servers and applications to use a single directory for user authentication and authorization.

Prerequisites:

Step-by-Step Guide

1. Install and Configure the LDAP Server

Install OpenLDAP and Utilities:

sudo apt-get update
sudo apt-get install slapd ldap-utils

Configure slapd:

During installation, you may not be prompted for configuration. Run the following to reconfigure:

sudo dpkg-reconfigure slapd

Configuration Prompts:

Setting Value
Omit OpenLDAP server configuration? No
DNS domain name example.com
Organization name Example Company
Administrator password [Set a strong password]
Database backend MDB
Remove the database when slapd is purged? No
Move old database? Yes

2. Define the Directory Structure (Schema)

Create Base LDIF File (base.ldif):

dn: dc=example,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
o: Example Company
dc: example

dn: ou=users,dc=example,dc=com
objectClass: top
objectClass: organizationalUnit
ou: users

dn: ou=groups,dc=example,dc=com
objectClass: top
objectClass: organizationalUnit
ou: groups

Load the Schema into LDAP:

sudo ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f base.ldif

Expected Output:

adding new entry "dc=example,dc=com"

adding new entry "ou=users,dc=example,dc=com"

adding new entry "ou=groups,dc=example,dc=com"

3. Add Users to the Directory

Create User LDIF File (user.ldif):

dn: uid=jdoe,ou=users,dc=example,dc=com
objectClass: inetOrgPerson
uid: jdoe
cn: John Doe
sn: Doe
givenName: John
mail: jdoe@example.com
userPassword: {SSHA}encrypted_password_here

Note: Use slappasswd to generate an encrypted password.

slappasswd

Step Action
Enter Password [Type password]
Re-enter Password [Retype password]
Output {SSHA}encrypted_password_here

Load the User into LDAP:

sudo ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f user.ldif

adding new entry "uid=jdoe,ou=users,dc=example,dc=com"

4. Install and Configure LDAP Client on Other Servers

Install Required Packages:

sudo apt-get install libnss-ldap libpam-ldap ldap-utils nscd

Configuration Prompts:

Setting Value
LDAP server URI ldap://ldapserver.example.com
Distinguished name of the search base dc=example,dc=com
LDAP version 3
Make local root Database admin Yes
Does the LDAP database require login? No
LDAP account for root cn=admin,dc=example,dc=com
LDAP root account password [Enter admin password]

Configure NSS to Use LDAP:

Edit /etc/nsswitch.conf:

passwd:         compat systemd ldap
group:          compat systemd ldap
shadow:         compat ldap

Configure PAM for LDAP Authentication:

Ensure that /etc/pam.d/common-auth includes:

auth    sufficient      pam_ldap.so
auth    required        pam_unix.so nullok_secure try_first_pass

Restart NSS Service:

sudo service nscd restart

5. Enable Home Directory Creation

Install libpam-mkhomedir:

sudo apt-get install libpam-mkhomedir

Configure PAM to create home directories:

Edit /etc/pam.d/common-session and add:

session required        pam_mkhomedir.so skel=/etc/skel umask=077

Verification and Testing

Test LDAP Lookup:

getent passwd jdoe

Expected Output:

jdoe:x:10000:10000:John Doe:/home/jdoe:/bin/bash

Test Login as LDAP User:

Use SSH or local terminal:

ssh jdoe@localhost

Maintenance and Management

Adding More Users

Create LDIF File for New User (user2.ldif):

dn: uid=asmith,ou=users,dc=example,dc=com
objectClass: inetOrgPerson
uid: asmith
cn: Alice Smith
sn: Smith
givenName: Alice
mail: asmith@example.com
userPassword: {SSHA}encrypted_password_here

Add User to LDAP:

sudo ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f user2.ldif

Modifying User Attributes

Create Modify LDIF File (modify_jdoe.ldif):

dn: uid=jdoe,ou=users,dc=example,dc=com
changetype: modify
replace: mail
mail: john.doe@example.com

Apply Changes:

sudo ldapmodify -x -D "cn=admin,dc=example,dc=com" -W -f modify_jdoe.ldif

Deleting Users

Delete User Entry:

sudo ldapdelete -x -D "cn=admin,dc=example,dc=com" -W "uid=jdoe,ou=users,dc=example,dc=com"

Challenges

  1. Install and configure an LDAP server on a Linux system. Set up the basic directory structure and include at least three organizational units (OUs).
  2. Add entries to the LDAP directory, including users and groups. Practice creating at least 10 user entries and 3 groups, assigning users to different groups.
  3. Configure a Linux system to use LDAP for user authentication. Test this by logging into the system with user credentials stored in the LDAP directory.
  4. Develop a strategy for backing up the LDAP directory. Perform a backup, then restore from this backup to ensure the integrity and completeness of your backup method.
  5. Use the ldapsearch command to perform various queries on the LDAP directory. Try to search for specific users, groups, and other entities based on different attributes.
  6. Secure your LDAP communications with TLS/SSL. Configure the server for encrypted connections and verify the security by connecting to it with an LDAP client.
  7. Choose an application or service (such as email or web service) that supports LDAP integration. Configure it to authenticate users against your LDAP directory.
  8. Design and implement a custom LDAP schema for a specific use case (like managing inventory or tracking software licenses). Add attributes and object classes that are not available in the default schema.
  9. Set up LDAP replication. Configure a secondary LDAP server and ensure that it synchronizes correctly with your primary LDAP server.
  10. Simulate common LDAP connectivity issues and practice troubleshooting. Document each issue simulated, your diagnostic process, and the steps taken to resolve the issues.

Table of Contents

  1. Understanding LDAP Concepts
    1. Directory
    2. Entries and Attributes
    3. Distinguished Names (DN)
    4. Schema
    5. Network Topology Diagram
  2. LDAP Directory Structure
    1. User Authentication Sequence Diagram
    2. Common LDAP Operations
      1. Bind
      2. Search & Compare
      3. Add, Delete & Modify
      4. Unbind
    3. LDAP Search Filters
    4. LDAP Tools and Utilities
      1. Command-Line Tools
      2. GUI-Based Tools
    5. Implementing LDAP for Centralized Authentication
    6. Step-by-Step Guide
      1. 1. Install and Configure the LDAP Server
      2. 2. Define the Directory Structure (Schema)
      3. 3. Add Users to the Directory
      4. 4. Install and Configure LDAP Client on Other Servers
      5. 5. Enable Home Directory Creation
    7. Verification and Testing
    8. Maintenance and Management
      1. Adding More Users
      2. Modifying User Attributes
      3. Deleting Users
    9. Challenges