Product SiteDocumentation Site

11.7. Directory LDAP

OpenLDAP is an implementation of the LDAP protocol; in other words, it is a special-purpose database designed for storing directories. In the most common use case, using an LDAP server allows centralizing management of user accounts and the related permissions. Moreover, an LDAP database is easily replicated, which allows setting up multiple synchronized LDAP servers. When the network and the user base grows quickly, the load can then be balanced across several servers.
I dati di LDAP sono strutturati e gerarchici. La struttura è definita attraverso «schemi» che descrivono il tipo di oggetti che il database può contenere, con una lista di tutti i loro possibili attributi. La sintassi usata per riferirsi ad un particolare oggetto nel database è basata su questa struttura, cosa che spiega la sua complessità.

11.7.1. Installazione

Il pacchetto slapd contiene il server OpenLDAP. Il pacchetto ldap-utils include strumenti a riga di comando per interagire con i server LDAP.
Installing slapd usually asks very few questions and the resulting database is unlikely to suit your needs. Fortunately a simple dpkg-reconfigure slapd will let you reconfigure the LDAP database with more details:
  • Omettere la configurazione del server OpenLDAP? No, naturalmente vogliamo configurare questo servizio.
  • Nome di dominio DNS: “falcot.com”.
  • Nome dell'organizzazione: “Falcot Corp”.
  • Dev'essere inserita una password amministrativa.
  • Database backend to use: “MDB”.
  • Eliminare il database in caso di rimozione completa di slapd? No. Non ha senso rischiare di perdere il database in caso di uno sbaglio.
  • Spostare il vecchio database? Questa domanda è posta solamente quando viene tentata un configurazione ed un database è già presente. Rispondere «sì» se si desidera ricominciare da un database pulito, per esempio se si esegue dpkg-reconfigure slapd subito dopo la prima installazione.
  • Allow LDAPv2 protocol? No, there is no point in that. All the tools we are going to use understand the LDAPv3 protocol.
Un database minimale è attualmente configurato, come dimostra la seguente richiesta:
$ ldapsearch -x -b dc=falcot,dc=com
# extended LDIF
#
# LDAPv3
# base <dc=falcot,dc=com> with scope sub
# filter: (objectclass=*)
# requesting: ALL
#

# falcot.com
dn: dc=falcot,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
o: Falcot Corp
dc: falcot

# admin, falcot.com
dn: cn=admin,dc=falcot,dc=com
objectClass: simpleSecurityObject
objectClass: organizationalRole
cn: admin
description: LDAP administrator

# search result
search: 2
result: 0 Success

# numResponses: 3
# numEntries: 2
La richiesta restituisce due oggetti: l'organizzazione stessa e l'utente amministrativo.

11.7.2. Riempire la directory

Since an empty database is not particularly useful, we are going to inject into it all the existing directories; this includes the users, groups, services and hosts databases.
Il pacchetto migrationtools fornisce un insieme di script dedicati ad estrarre i dati dalle directory standard di Unix (/etc/passwd, /etc/group, /etc/services, /etc/hosts e così via), convertire questi dati ed inserirli all'interno del database LDAP.
Once the package is installed, the /etc/migrationtools/migrate_common.ph must be edited; the IGNORE_UID_BELOW and IGNORE_GID_BELOW options need to be enabled (uncommenting them is enough), and DEFAULT_MAIL_DOMAIN/DEFAULT_BASE need to be updated.
La reale operazione di migrazione è gestita dal comando migrate_all_online.sh come segue:
# cd /usr/share/migrationtools
# LDAPADD="/usr/bin/ldapadd -c" ETC_ALIASES=/dev/null ./migrate_all_online.sh
migrate_all_online.sh rivolge alcune domande a proposito del database LDAP nel quale si vogliono migrare i dati. Tabella 11.1 riassume le risposte fornite nel caso d'uso della Falcot.

Tabella 11.1. Le risposte fornite alle domande poste dallo script migrate_all_online.sh

DomandaRisposta
X.500 naming contextdc=falcot,dc=com
Nome host del server LDAPlocalhost
Manager DNcn=admin,dc=falcot,dc=com
Bind credentialsla password amministrativa
Create DUAConfigProfileno
Abbiamo deliberatamente ignorato la migrazione del file /etc/aliases dato che lo schema standard fornito da Debian non include le strutture che utilizza questo script per gli alias email. Se dovessimo integrare questi dati nella directory il file /etc/ldap/schema/misc.schema dovrebbe essere aggiunto allo schema standard.
Notare altresì l'uso dell'opzione -c con il comando ldapadd: questa opzione richiede che l'elaborazione non si interrompa in caso di errori. Utilizzare questa opzione è necessario poiché convertire il database /etc/services genera spesso qualche errore che può essere ignorato senza conseguenze.

11.7.3. Gestire gli account con LDAP

Ora il database LDAP contiene diverse informazioni utili ed è giunto il momento di sfruttarle. Questa sezione si concentra su come configurare un sistema Linux per far sì che le varie directory di sistema utilizzino il database LDAP.

11.7.3.1. Configurare NSS

The NSS system (Name Service Switch, see sidebar APPROFONDIMENTI NSS ed i database di sistema) is a modular system designed to define or fetch information for system directories. Using LDAP as a source of data for NSS requires installing the libnss-ldap package. Its installation asks a few questions; the answers are summarized in Tabella 11.2.

Tabella 11.2. Configurare il pacchetto libnss-ldap

DomandaRisposta
L'Uniform Resource Identifier del server LDAPldap://ldap.falcot.com
Il nome distintivo per la base di ricercadc=falcot,dc=com
La versione di LDAP da utilizzare3
Il database LDAP deve richiedere il login?no
Special LDAP privileges for rootyes
Make the configuration file readable/writeable by its owner onlyno
L'account LDAP per rootcn=admin,dc=falcot,dc=com
La password per l'account root di LDAPla password amministrativa
Il file /etc/nsswitch.conf richiede poi di essere modificato per configurare NSS in modo che utilizzi il modulo ldap appena installato.

Esempio 11.26. Il file /etc/nsswitch.conf

# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.

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

hosts: files dns ldap
networks: ldap files

protocols: ldap db files
services: ldap db files
ethers: ldap db files
rpc: ldap db files

netgroup: ldap files
The ldap module is usually inserted before others, and it will therefore be queried first. The notable exception is the hosts service since contacting the LDAP server requires consulting DNS first (to resolve ldap.falcot.com). Without this exception, a hostname query would try to ask the LDAP server; this would trigger a name resolution for the LDAP server, and so on in an infinite loop.
Se il server LDAP dev'essere considerato autoritativo (e i file locali utilizzati dal modulo files ignorati) i servizi possono essere configurati con la seguente sintassi:
servizio: ldap [NOTFOUND=return] files.
Se l'entità richiesta non esiste nel database LDAP, la richiesta restituirà una risposta «non esistente» anche se la risorsa esiste in uno dei file locali: questi file locali saranno utilizzati unicamente quando il servizio LDAP non è raggiungibile.

11.7.3.2. Configurare PAM

This section describes a PAM configuration (see sidebar DIETRO LE QUINTE /etc/environment e /etc/default/locale) that will allow applications to perform the required authentications against the LDAP database.
Il modulo LDAP per PAM è fornito dal pacchetto libpam-ldap. L'installazione di questo pacchetto pone alcune domande molto simili a quelle viste con libnss-ldap: alcuni parametri di configurazione (come l'URI del server LDAP) sono in realtà persino condivisi con il pacchetto libnss-ldap. Le risposte sono riassunte in Tabella 11.3.

Tabella 11.3. Configurazione di libpam-ldap

DomandaRisposta
Permettere all'account amministrativo LDAP di agire come root?Sì. Questo ci consente di utilizzare il comando passwd per cambiare le password conservate nel database LDAP.
Il database LDAP richiede il login?no
L'account LDAP per rootcn=admin,dc=falcot,dc=com
La password per l'account root di LDAPLa password amministrativa del database LDAP
Local encryption algorithm to use for passwordscrypt
L'installazione di libpam-ldap adatta automaticamente la configurazione predefinita di PAM contenuta nei file /etc/pam.d/common-auth, /etc/pam.d/common-password e /etc/pam.d/common-account. Questo meccanismo utilizza lo strumento dedicato pam-auth-update (fornito con il pacchetto libpam-runtime). Questo strumento può anche essere eseguito dall'amministratore qualora desideri abilitare o disabilitare dei moduli PAM.

11.7.3.3. Mettere al sicuro lo scambio dati di LDAP

In via predefinita il protocollo LDAP transita sulla rete come testo in chiaro: questo include le password (cifrate). Poiché le password cifrate possono essere estratte dalla rete rimangono vulnerabili ad attacchi a dizionario. Questo può essere evitato utilizzando uno strato di cifratura addizionale: abilitare questo strato è l'argomento di questa sezione.
11.7.3.3.1. Configurare il server
The first step is to create a key pair (comprising a public key and a private key) for the LDAP server. The Falcot administrators reuse easy-rsa to generate it (see Sezione 10.2.1.1, «Infrastruttura a chiave pubblica: easy-rsa»). Running ./build-server-key ldap.falcot.com asks a few mundane questions (location, organization name and so on). The answer to the “common name” question must be the fully-qualified hostname for the LDAP server; in our case, ldap.falcot.com.
This command creates a certificate in the keys/ldap.falcot.com.crt file; the corresponding private key is stored in keys/ldap.falcot.com.key.
Now these keys have to be installed in their standard location, and we must make sure that the private file is readable by the LDAP server which runs under the openldap user identity:
# adduser openldap ssl-cert
Adding user `openldap' to group `ssl-cert' ...
Adding user openldap to group ssl-cert
Done.
# mv keys/ldap.falcot.com.key /etc/ssl/private/ldap.falcot.com.key
# chown root:ssl-cert /etc/ssl/private/ldap.falcot.com.key
# chmod 0640 /etc/ssl/private/ldap.falcot.com.key
# mv newcert.pem /etc/ssl/certs/ldap.falcot.com.pem
The slapd daemon also needs to be told to use these keys for encryption. The LDAP server configuration is managed dynamically: the configuration can be updated with normal LDAP operations on the cn=config object hierarchy, and the server updates /etc/ldap/slapd.d in real time to make the configuration persistent. ldapmodify is thus the right tool to update the configuration:

Esempio 11.27. Configurare slapd per la cifratura

# cat >ssl.ldif <<END
dn: cn=config
changetype: modify
add: olcTLSCertificateFile
olcTLSCertificateFile: /etc/ssl/certs/ldap.falcot.com.pem
-
add: olcTLSCertificateKeyFile
olcTLSCertificateKeyFile: /etc/ssl/private/ldap.falcot.com.key
-
END
# ldapmodify -Y EXTERNAL -H ldapi:/// -f ssl.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "cn=config"
L'ultimo passo per abilitare la cifratura richiede la modifica della variabile SLAPD_SERVICES nel file /etc/default/slapd. Inoltre, per essere prudenti, si renderà necessario disabilitare l'LDAP non sicuro.

Esempio 11.28. Il file /etc/default/slapd

# Default location of the slapd.conf file or slapd.d cn=config directory. If
# empty, use the compiled-in default (/etc/ldap/slapd.d with a fallback to
# /etc/ldap/slapd.conf).
SLAPD_CONF=

# System account to run the slapd server under. If empty the server
# will run as root.
SLAPD_USER="openldap"

# System group to run the slapd server under. If empty the server will
# run in the primary group of its user.
SLAPD_GROUP="openldap"

# Path to the pid file of the slapd server. If not set the init.d script
# will try to figure it out from $SLAPD_CONF (/etc/ldap/slapd.conf by
# default)
SLAPD_PIDFILE=

# slapd normally serves ldap only on all TCP-ports 389. slapd can also
# service requests on TCP-port 636 (ldaps) and requests via unix
# sockets.
# Example usage:
# SLAPD_SERVICES="ldap://127.0.0.1:389/ ldaps:/// ldapi:///"
SLAPD_SERVICES="ldaps:/// ldapi:///"

# If SLAPD_NO_START is set, the init script will not start or restart
# slapd (but stop will still work).  Uncomment this if you are
# starting slapd via some other means or if you don't want slapd normally
# started at boot.
#SLAPD_NO_START=1

# If SLAPD_SENTINEL_FILE is set to path to a file and that file exists,
# the init script will not start or restart slapd (but stop will still
# work).  Use this for temporarily disabling startup of slapd (when doing
# maintenance, for example, or through a configuration management system)
# when you don't want to edit a configuration file.
SLAPD_SENTINEL_FILE=/etc/ldap/noslapd

# For Kerberos authentication (via SASL), slapd by default uses the system
# keytab file (/etc/krb5.keytab).  To use a different keytab file,
# uncomment this line and change the path.
#export KRB5_KTNAME=/etc/krb5.keytab

# Additional options to pass to slapd
SLAPD_OPTIONS=""
11.7.3.3.2. Configurare il client
On the client side, the configuration for the libpam-ldap and libnss-ldap modules needs to be modified to use an ldaps:// URI.
LDAP clients also need to be able to authenticate the server. In a X.509 public key infrastructure, public certificates are signed by the key of a certificate authority (CA). With easy-rsa, the Falcot administrators have created their own CA and they now need to configure the system to trust the signatures of Falcot's CA. This can be done by putting the CA certificate in /usr/local/share/ca-certificates and running update-ca-certificates.
# cp keys/ca.crt /usr/local/share/ca-certificates/falcot.crt
# update-ca-certificates
Updating certificates in /etc/ssl/certs... 1 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d....
Adding debian:falcot.pem
done.
done.
Last but not least, the default LDAP URI and default base DN used by the various command line tools can be modified in /etc/ldap/ldap.conf. This will save quite some typing.

Esempio 11.29. Il file /etc/ldap/ldap.conf

#
# LDAP Defaults
#

# See ldap.conf(5) for details
# This file should be world readable but not world writable.

BASE   dc=falcot,dc=com
URI    ldaps://ldap.falcot.com

#SIZELIMIT      12
#TIMELIMIT      15
#DEREF          never

# TLS certificates (needed for GnuTLS)
TLS_CACERT      /etc/ssl/certs/ca-certificates.crt