Ir al contenido

Centralized Authentication Configuration Manual with OpenLDAP

Author: Full-Stack Innovator Pro (Technical Review) Objective: Configure an OpenLDAP server to enable Single Sign-On (SSO) on Ubuntu clients...

0views

Author: Full-Stack Innovator Pro (Technical Review) Objective: Configure an OpenLDAP server to allow Single Sign-On (SSO) on Ubuntu Desktop clients, including the automatic creation of home directories and SSL/TLS security.

  • Domain: portelcore.local
  • Organization: portelcore
  • Server (Static IP): 172.20.0.153
  • Client: Ubuntu Desktop
  • Base DN: dc=portelcore,dc=local

First, we will install the LDAP daemon (slapd) and management utilities.

Ventana de terminal
sudo apt update
sudo apt install slapd ldap-utils -y

Now we will reconfigure the package to create the database with our specific domain.

Ventana de terminal
sudo dpkg-reconfigure slapd

Setup wizard responses:

  1. Skip configuration? No
  2. DNS domain name? portelcore.local
  3. Organization name? portelcore
  4. Administrator password? (We define a secure password and remember it)
  5. Database engine? MDB
  6. Delete the database to purge? No
  7. Move the old database? Sí

Once finished, we will check that the database has been created correctly by verifying the domain context:

Ventana de terminal
slapcat | grep "dc=portelcore"

This step is crucial for defining the directory architecture. We will create a .ldif file that defines the structure (Organizational Units), groups, and users.

We create the file:

nano usuaris_grups.ldif

We paste the following content into the file:

# Estructura Base: Unidades Organizativas
dn: ou=people,dc=portelcore,dc=local
objectClass: organizationalUnit
ou: people
dn: ou=groups,dc=portelcore,dc=local
objectClass: organizationalUnit
ou: groups
# ---------------------------------------------------------
# DEFINICIÓN DE GRUPOS
# ---------------------------------------------------------
# Grupo 1: Administrador (GID 5000)
dn: cn=Administrador,ou=groups,dc=portelcore,dc=local
objectClass: posixGroup
cn: Administrador
gidNumber: 5000
# Grupo 2: Empleados (GID 5001)
dn: cn=Empleats,ou=groups,dc=portelcore,dc=local
objectClass: posixGroup
cn: Empleats
gidNumber: 5001
# Grupo 3: Helpdesk (GID 5002)
dn: cn=Helpdesk,ou=groups,dc=portelcore,dc=local
objectClass: posixGroup
cn: Helpdesk
gidNumber: 5002
# ---------------------------------------------------------
# DEFINICIÓN DE USUARIOS
# ---------------------------------------------------------
# Usuario 1: admin (Dentro de Administrador)
dn: uid=admin,ou=people,dc=portelcore,dc=local
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uid: admin
sn: System
givenName: Administrator
cn: Admin System
displayName: Admin System
uidNumber: 10000
gidNumber: 5000
userPassword: nolose123
gecos: Admin System
loginShell: /bin/bash
homeDirectory: /home/admin
# Usuario 2: user.portelcore (Dentro de Empleados)
dn: uid=user.portelcore,ou=people,dc=portelcore,dc=local
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uid: user.portelcore
sn: User
givenName: Portelcore
cn: User Portelcore
displayName: User Portelcore
uidNumber: 10001
gidNumber: 5001
userPassword: nolose123
gecos: User Portelcore
loginShell: /bin/bash
homeDirectory: /home/user.portelcore
# Usuario 3: becario (Dentro de Helpdesk)
dn: uid=becario,ou=people,dc=portelcore,dc=local
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uid: becario
sn: Tech
givenName: Becario
cn: Becario Tech
displayName: Becario Tech
uidNumber: 10002
gidNumber: 5002
userPassword: nolose123
gecos: Becario Tech
loginShell: /bin/bash
homeDirectory: /home/becario

We will inject the mapping file into the LDAP server.

ldapadd -x -D "cn=admin,dc=portelcore,dc=local" -W -f usuaris_grups.ldif

It will ask for the administrator password configured in step 1.

Verification: If everything is correct, we will see messages from adding new entry. We verify that the user “becario” exists:

aldapsearch -x -b "dc=portelcore,dc=local" "(uid=becario)"

Let's go to the client machine. This machine does not have users created locally; it will have to read them from the server.

We will install the necessary packages to connect the authentication system (PAM) and the name service (NSS) with LDAP.

Ventana de terminal
sudo apt update
sudo apt install libnss-ldapd libpam-ldapd nscd -y

Graphical wizard configuration:

  1. LDAP server URI: ldap://172.20.0.153
  2. Search Base: dc=portelcore,dc=local
  3. Services: Make sure to check passwd, group, and shadow using the spacebar.

⚠️ IMPORTANT: Verify nsswitch.conf If users do not work after installation, we manually verify that the system searches in LDAP:

Ventana de terminal
cat /etc/nsswitch.conf | grep passwd

If the word ldap does not appear at the end of the line, we edit the file sudo nano /etc/nsswitch.conf and add ldap to the end of lines passwd, group, and shadow.

The client must be able to "see" the users from the server.

getent passwd user.portelcore

Expected result: user.portelcore:x:10001:5001:User Portelcore:/home/user.portelcore:/bin/bash

We will configure PAM to automatically create the /home folder upon the first login.

Edit the common-session file:

Ventana de terminal
sudo nano /etc/pam.d/common-session

Add this line at the end of the file:

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

Restart the nscd service to clear the cache:

Ventana de terminal
sudo systemctl restart nscd

Log in with user admin (password: nolose123) via a Desktop terminal.

id

Result: It must show uid=10000(admin) and gid=5000(Administrador).

Log out and log in with becario / nolose123.

To comply with security regulations, we will encrypt the connection.

We will install OpenSSL and create the certificates. Important: When asked for "Common Name (CN)", we will enter portelcore.local.

Ventana de terminal
sudo apt install openssl -y
sudo mkdir /etc/ldap/certs
# 1. Crear clave privada CA
sudo openssl genrsa -out /etc/ldap/certs/ca.key 2048
# 2. Crear certificado CA (Autoridad)
sudo openssl req -x509 -new -nodes -key /etc/ldap/certs/ca.key -sha256 -days 3650 -out /etc/ldap/certs/ca.crt -subj "/C=ES/ST=Barcelona/L=Granollers/O=Portelcore/CN=portelcore.local"
# 3. Crear clave privada del Servidor
sudo openssl genrsa -out /etc/ldap/certs/ldap_server.key 2048
# 4. Crear petición de firma (CSR)
sudo openssl req -new -key /etc/ldap/certs/ldap_server.key -out /etc/ldap/certs/ldap_server.csr -subj "/C=ES/ST=Barcelona/L=Granollers/O=Portelcore/CN=portelcore.local"
# 5. Firmar el certificado del servidor con nuestra CA
sudo openssl x509 -req -in /etc/ldap/certs/ldap_server.csr -CA /etc/ldap/certs/ca.crt -CAkey /etc/ldap/certs/ca.key -CAcreateserial -out /etc/ldap/certs/ldap_server.crt -days 365 -sha256

We will assign permissions so that LDAP can read the keys:

Ventana de terminal
sudo chown -R openldap:openldap /etc/ldap/certs/
sudo chmod 600 /etc/ldap/certs/*.key

We will create a tls_config.ldif file to indicate where the certificates are located:

nano tls_config.ldif

Content:

dn: cn=config
changetype: modify
replace: olcTLSCACertificateFile
olcTLSCACertificateFile: /etc/ldap/certs/ca.crt
-
replace: olcTLSCertificateFile
olcTLSCertificateFile: /etc/ldap/certs/ldap_server.crt
-
replace: olcTLSCertificateKeyFile
olcTLSCertificateKeyFile: /etc/ldap/certs/ldap_server.key

We will apply the configuration:

Ventana de terminal
sudo ldapmodify -Y EXTERNAL -H ldapi:/// -f tls_config.ldif

We will edit /etc/default/slapd to ensure it accepts secure connections:

Ventana de terminal
sudo nano /etc/default/slapd

We will look for SLAPD_SERVICES and leave it like this: SLAPD_SERVICES="ldap:/// ldapi:/// ldaps:///"

We will restart:

Ventana de terminal
sudo systemctl restart slapd

⚠️ CRITICAL PREVIOUS STEP: DNS Configuration Since the certificate is issued to portelcore.local, we cannot use the IP directly in the SSL configuration.

  1. We will edit the client's hosts file:

    Ventana de terminal
    sudo nano /etc/hosts
  2. We will add this line at the end:

    172.20.0.153 portelcore.local

SSL configuration steps:

  1. Transfer the CA: We need to download the authority certificate from the server to the client.

    We will open a terminal on the Client and execute: (We will replace usuari_remot with our server user, for example, alex)

    Ventana de terminal
    # 1. Descargar archivo
    scp user.portelcore@172.20.0.153:/etc/ldap/certs/ca.crt .
    # 2. Mover al lugar correcto
    sudo mv ca.crt /etc/ssl/certs/
    # 3. Asignar permisos de lectura para todos
    sudo chmod 644 /etc/ssl/certs/ca.crt
  2. Configure the LDAP daemon: We will edit the configuration file:

    Ventana de terminal
    sudo nano /etc/nslcd.conf

    We will make TWO important changes:

    Change A: We will modify the URI to use the domain name, not the IP: uri ldap://portelcore.local

    Change B: We will enable TLS and the certificate:

    ssl start_tls
    tls_reqcert demand
    tls_cacertfile /etc/ssl/certs/ca.crt
  3. Restart client services:

    Ventana de terminal
    sudo systemctl restart nslcd
  4. Verify Connection:

    getent passwd admin

In production environments (Servers without GUI), the best practice is to capture the traffic on the server and analyze it later on a desktop machine.

  1. We will install the capture tool:

    Ventana de terminal
    sudo apt install tcpdump -y
  2. We will start the capture by filtering the LDAP port (389) and saving it to a file .pcap:

    Ventana de terminal
    sudo tcpdump -i any port 389 -w /tmp/ldap_capture.pcap

    (We will leave the terminal open while we perform the next step).

  1. We will open a terminal on the Client (Ubuntu Desktop).

  2. We will perform a query to generate traffic:

    getent passwd user.portelcore
  3. We will return to the Server and stop the capture with Ctrl + C.

Now we will transfer the capture file to the Client to analyze it with Wireshark (which has a graphical interface).

  1. On the Client, we will download the file:

    scp usuari@172.20.0.153:/tmp/ldap_capture.pcap .
  2. We will install and open Wireshark:

    Ventana de terminal
    sudo apt install wireshark -y
    wireshark ldap_capture.pcap

What should we look for?

  • We will look for packets with protocol LDAP.
  • We will look for the “startTLSRequest” packet.
  • Right after, TLSv1.2 or TLSv1.3 packets should appear.
  • Success test: We will attempt to read the content of an “Application Data” packet. If we only see unreadable (encrypted) characters, the security is working correctly.

If we already have the client configured and the server IP changes (for example, from 192.168.1.100 to 172.20.0.153), reinstallation is not necessary.

Option A (Recommended - Assistant): We will run the reconfiguration of the nslcd package:

Ventana de terminal
sudo dpkg-reconfigure nslcd

We will enter the new IP when requested for the URI (ldap://172.20.0.153).

Option B (Manual):

  1. We will edit the nslcd daemon configuration file:

    Ventana de terminal
    sudo nano /etc/nslcd.conf
  2. We will modify the URI line: We will look for the line that begins with uri and change the old IP for the new one. Before: uri ldap://192.168.1.100Now: uri ldap://172.20.0.153

    (We will save with Ctrl+O and exit with Ctrl+X)

  3. We will restart the services to apply changes:

    Ventana de terminal
    sudo systemctl restart nslcd
    sudo systemctl restart nscd
  4. We will verify the connection:

    getent passwd user.portelcore

If getent passwd returns nothing, but we have a network connection, the problem is usually SSL.

Step 1: Real-time diagnosis We will stop the daemon and run it manually in debug mode to see why it rejects the connection.

Ventana de terminal
sudo systemctl stop nslcd
sudo nslcd -d

We will read the last lines. If we see “hostname does not match CN”, it means we have the IP in the configuration file instead of the name.

Step 2: URI Verification We will ensure that in /etc/nslcd.conf we have exactly the following: uri ldap://portelcore.local

(If we have ldap://172.20.0.153, the certificate will fail).