OpenLDAP: Unterschied zwischen den Versionen

Aus Foxwiki
Die Seite wurde neu angelegt: „'''topic''' kurze Beschreibung ==Beschreibung== ==Installation== ==Anwendungen== ==Syntax== ===Optionen=== ===Parameter=== ===Umgebungsvariablen=== ===Exit-Status=== ==Konfiguration== ===Dateien=== ==Sicherheit== ==Dokumentation== ===RFC=== ===Man-Pages=== ===Info-Pages=== ==Siehe auch== ==Links== ===Projekt-Homepage=== ===Weblinks=== ===Einzelnachweise=== <references /> ==Testfragen== <div class="toccolours mw-collapsible mw-collapsed"> ''Testfrage 1'' <…“
 
Zeile 46: Zeile 46:


=TMP=
=TMP=
= OpenLDAP =
[https://www.openldap.org/ OpenLDAP] is an open-source implementation of the LDAP protocol. An LDAP server basically is a non-relational database which is optimised for accessing, but not writing, data. It is mainly used as an address book (for e.g. email clients) or authentication backend to various services (such as Samba, where it is used to emulate a domain controller, or [https://wiki.archlinux.org/title/LDAP_authentication Linux system authentication], where it replaces <tt>/etc/passwd</tt>) and basically holds the user data.
'''Note:''' Commands related to OpenLDAP that begin with <tt>ldap</tt> (like <tt>ldapsearch</tt>) are client-side utilities, while commands that begin with <tt>slap</tt> (like <tt>slapcat</tt>) are server-side.
This page is a starting point for a basic OpenLDAP installation and a sanity check.
'''Tip:''' Directory services are an enormous topic. Configuration can therefore be complex. If you are totally new to those concepts, [https://www.brennan.id.au/20-Shared_Address_Book_LDAP.html this] is a good introduction that is easy to understand and that will get you started, even if you are new to LDAP.
== Installation ==
OpenLDAP contains both a LDAP server and client. [https://wiki.archlinux.org/title/Install Install] it with the package [https://archlinux.org/packages/?name=openldap openldap].
== Configuration ==
=== The server ===
'''Note:''' * If you have an obsolete <tt>slapd.conf</tt> configuration, you can simply convert it into the new <tt>cn=config</tt> database.
* If you already have an OpenLDAP database on your machine and would like to remove it, then it can be removed by deleting everything inside of <tt>/var/lib/openldap/openldap-data/</tt>. So, backup your <tt>DB_CONFIG</tt>.
Slapd, the server, stores its configuration directly inside its database. Thus, we need to write our configuration as an LDIF file and import it.
First, create a file <tt>config.ldif</tt> containing the following minimal sane configuration. This file declares the basic configuration structure and a database stored at <tt>/var/lib/openldap/openldap-data</tt>.
config.ldif
<nowiki># The root config entry</nowiki>
dn: cn=config
objectClass: olcGlobal
cn: config
olcArgsFile: /run/openldap/slapd.args
olcPidFile: /run/openldap/slapd.pid
<nowiki># Schemas</nowiki>
dn: cn=schema,cn=config
objectClass: olcSchemaConfig
cn: schema
<nowiki># TODO: Include further schemas as necessary</nowiki>
include: file:///etc/openldap/schema/core.ldif
<nowiki># The config database</nowiki>
dn: olcDatabase=config,cn=config
objectClass: olcDatabaseConfig
olcDatabase: config
olcRootDN: cn=Manager,$BASEDN
<nowiki># The database for our entries</nowiki>
dn: olcDatabase=mdb,cn=config
objectClass: olcDatabaseConfig
objectClass: olcMdbConfig
olcDatabase: mdb
olcSuffix: $BASEDN
olcRootDN: cn=Manager,$BASEDN
olcRootPW: $PASSWD
olcDbDirectory: /var/lib/openldap/openldap-data
<nowiki># TODO: Create further indexes</nowiki>
olcDbIndex: objectClass eq
There are a few options you will need to change: * Every occurence of <tt>$BASEDN</tt> must be replaced with a valid DN. If you own a domain <tt>example.com</tt> you will most likely want to choose <tt>dc=example,dc=com</tt>.
* <tt>$PASSWD</tt> must be replaced by a salted and hashed password, which you may generate by running <tt>slappasswd</tt>.
Additionally, you might consider to add further [https://www.openldap.org/doc/admin24/schema.html schemas] and create additional [https://www.openldap.org/doc/admin24/tuning.html#Indexes indexes] to tune the performance of your database. The specifics will depend on your use case, but here are a few recommendations. For [https://wiki.archlinux.org/title/LDAP_authentication LDAP authentication], you should include the three schemas below to be able to use the <tt>posixAccount</tt> object class used for storing users.
<nowiki># Additional schemas</nowiki>
<nowiki># RFC1274: Cosine and Internet X.500 schema</nowiki>
include: file:///etc/openldap/schema/cosine.ldif
<nowiki># RFC2798: Internet Organizational Person</nowiki>
include: file:///etc/openldap/schema/inetorgperson.ldif
<nowiki># RFC2307: An Approach for Using LDAP as a Network Information Service</nowiki>
include: file:///etc/openldap/schema/nis.ldif
<nowiki># Additional indexes</nowiki>
olcDbIndex: objectClass eq
olcDbIndex: uid pres,eq
olcDbIndex: mail pres,sub,eq
olcDbIndex: cn,sn pres,sub,eq
olcDbIndex: dc eq
To import these settings as the <tt>ldap</tt> user using [https://wiki.archlinux.org/title/Sudo sudo]:
<nowiki># sudo -u ldap slapadd -n 0 -F /etc/openldap/slapd.d/ -l ./config.ldif</nowiki>
Alternatively, you may also run this directly as <tt>root</tt>. However, if you do, make sure <tt>/etc/openldap/slapd.d/</tt> is accessible by <tt>ldap</tt>:
<nowiki># slapadd -n 0 -F /etc/openldap/slapd.d/ -l ./config.ldif</nowiki>
<nowiki># chown -R ldap:ldap /etc/openldap/*</nowiki>
By default, OpenLDAP will listen unencrypted on all interfaces. To make it only listen on local IP interfaces, you may edit the environment file read by <tt>slapd.service</tt>:
/etc/conf.d/slapd
SLAPD_URLS="ldap://127.0.0.1/ ldap://[::1]"
SLAPD_OPTIONS=
Finally, start the slapd daemon by [https://wiki.archlinux.org/title/Start starting] <tt>slapd.service</tt>.
'''Note:''' * If you want to have your directory accept requests from the network, you should consider using TLS. See [https://wiki.archlinux.org/title/OpenLDAP#OpenLDAP_over_TLS #OpenLDAP over TLS] for details.
* If you plan to use your LDAP server for authentication, you might want to check access control configuration in [https://wiki.archlinux.org/title/LDAP_authentication#LDAP_Server_Setup LDAP authentication#LDAP Server Setup].
* Berkeley DB (BDB) should no longer be used. The mdb backend to [https://man.archlinux.org/man/slapd.8 slapd(8)] is the recommended primary backend for a normal slapd database. It uses OpenLDAP's own Lightning Memory-Mapped Database (LMDB) library to store data and is intended to replace the Berkeley DB backend. The OpenLDAP package in the [https://wiki.archlinux.org/title/Official_repositories official repositories] defaults to mdb.
=== The client ===
The client configuration file is located at <tt>/etc/openldap/ldap.conf</tt>.
It is quite simple: you will only have to alter <tt>BASE</tt> to reflect the suffix of the server, and <tt>URI</tt> to reflect the address of the server, like:
/etc/openldap/ldap.conf
BASE            dc=example,dc=com
URI            ldap://localhost
If you decide to use SSL: * The protocol (ldap or ldaps) in the <tt>URI</tt> entry has to conform with the slapd configuration
* If you decide to use TLS, add a <tt>TLS_REQCERT allow</tt> line to <tt>ldap.conf</tt>
* If you use a signed certificate from a CA, add the line <tt>TLS_CACERTDIR /usr/share/ca-certificates/trust-source</tt> in <tt>ldap.conf</tt>.
=== Create initial entry ===
'''Note:''' If you plan to use your LDAP server for authentication, you should import the <tt>base.ldif</tt> in the [https://wiki.archlinux.org/title/LDAP_authentication#Populate_LDAP_Tree_with_Base_Data LDAP authentication] article instead of following the instructions here.
Once your client is configured, you probably want to create the root entry, and an entry for the Manager role:
$ ldapadd -x -D 'cn=Manager,dc=example,dc=com' -W
dn: dc=example,dc=com
objectClass: dcObject
objectClass: organization
dc: example
o: Example
description: Example directory
dn: cn=Manager,dc=example,dc=com
objectClass: organizationalRole
cn: Manager
description: Directory Manager
^D
The text after the first line is entered on stdin, or could be read from a file either with the <tt>-f</tt> option or a file redirect.
=== Test your new OpenLDAP installation ===
This is easy, just run the command below:
$ ldapsearch -x '(objectclass=*)' -b 'dc=example,dc=com'
Or authenticating as the rootdn (replacing <tt>-x</tt> by <tt>-D user -W</tt>), using the example configuration we had above:
$ ldapsearch -D "cn=Manager,dc=example,dc=com" -W '(objectclass=*)' -b 'dc=example,dc=com'
Now you should see some information about your database.
=== OpenLDAP over TLS ===
'''Note:''' [https://www.openldap.org/doc/admin24/ upstream documentation] is much more useful/complete than this section
If you access the OpenLDAP server over the network and especially if you have sensitive data stored on the server you run the risk of someone sniffing your data which is sent clear-text. The next part will guide you on how to setup an SSL connection between the LDAP server and the client so the data will be sent encrypted.
In order to use TLS, you must have a certificate. For testing purposes, a self-signed certificate will suffice. To learn more about certificates, see [https://wiki.archlinux.org/title/OpenSSL OpenSSL].
'''Warning:''' OpenLDAP cannot use a certificate that has a password associated to it.
==== Create a self-signed certificate ====
To create a self-signed certificate, type the following:
$ openssl req -new -x509 -nodes -out slapdcert.pem -keyout slapdkey.pem -days 365
You will be prompted for information about your LDAP server. Much of the information can be left blank. The most important information is the common name. This must be set to the DNS name of your LDAP server. If your LDAP server's IP address resolves to example.org but its server certificate shows a CN of bad.example.org, LDAP clients will reject the certificate and will be unable to negotiate TLS connections (apparently the results are wholly unpredictable).
Now that the certificate files have been created copy them to <tt>/etc/openldap/ssl/</tt> (create this directory if it does not exist) and secure them. <tt>slapdcert.pem</tt> must be world readable because it contains the public key. <tt>slapdkey.pem</tt> on the other hand should only be readable for the ldap user for security reasons:
<nowiki># mv slapdcert.pem slapdkey.pem /etc/openldap/ssl/</nowiki>
<nowiki># chmod -R 755 /etc/openldap/ssl/</nowiki>
<nowiki># chmod 400 /etc/openldap/ssl/slapdkey.pem</nowiki>
<nowiki># chmod 444 /etc/openldap/ssl/slapdcert.pem</nowiki>
<nowiki># chown ldap /etc/openldap/ssl/slapdkey.pem</nowiki>
==== Configure slapd for SSL ====
Edit the configuration to tell LDAP where the certificate files reside by executing the following command:
'''Note:''' The latest version of OpenLDAP (2.4.45) uses OpenSSL and not GnuTLS. This means that current versions of OpenLDAP do in fact know how to handle the [https://www.openssl.org/docs/man1.1.1/man1/ciphers.html#CIPHER-STRINGS DEFAULT TLSCipherSuite]. To prove this one could run <tt>ldd /usr/bin/slapd</tt>.
ldapmodify -D 'cn=Manager,dc=example,dc=com' -W
dn: cn=config
add: olcTLSCertificateFile
olcTLSCertificateFile: /etc/openldap/ssl/slapdcert.pem
-
add: olcTLSCertificateKeyFile
olcTLSCertificateKeyFile: /etc/openldap/ssl/slapdkey.pem
If you are using a signed SSL Certificate from a certification authority such as [https://wiki.archlinux.org/title/Let’s_Encrypt Let’s Encrypt], you will also need to specify the path to the root certificates database and your intermediary certificate. You will also need to change ownership of the .pem files and intermediary directories to make them readable to the user <tt>ldap</tt>:
ldapmodify -D 'cn=Manager,dc=example,dc=com' -W
dn: cn=config
add: olcTLSCACertificateFile
olcTLSCACertificateFile: /etc/letsencrypt/live/ldap.my-domain.com/chain.pem
-
add: olcTLSCertificateFile
olcTLSCertificateFile: /etc/letsencrypt/live/ldap.my-domain.com/cert.pem
-
add: olcTLSCertificateKeyFile
olcTLSCertificateKeyFile: /etc/letsencrypt/live/ldap.my-domain.com/privkey.pem
-
add: olcTLSCACertificatePath
olcTLSCACertificatePath: /usr/share/ca-certificates/trust-source
SSLv2/v3
Disable SSLv2/v3 and use strong ciphers.
ldapmodify -D 'cn=Manager,dc=example,dc=com' -W
dn: cn=config
add: olcTLSProtocolMin
olcTLSProtocolMin: 3.3
-
add: olcTLSCipherSuite
olcTLSCipherSuite: DEFAULT:!kRSA:!kDHE
-
TLSProtocolMin specifies the minimum version in wire format, so "3.3" actually means TLSv1.2.
The TLSCipherSuite specifies a list of OpenSSL ciphers from which slapd will choose when negotiating TLS connections, in decreasing order of preference. In addition to those specific ciphers, you can use any of the wildcards supported by OpenSSL. Note: DEFAULT is a wildcard. See [https://man.archlinux.org/man/ciphers.1ssl ciphers(1ssl)] for description of ciphers, wildcards and options supported.
'''Note:''' To see which ciphers are supported by your local OpenSSL installation, type the following: <tt>openssl ciphers -v ALL:COMPLEMENTOFALL</tt>. Always test which ciphers will actually be enabled by TLSCipherSuite by providing it to OpenSSL command, like this: <tt>openssl ciphers -v 'DEFAULT'</tt>.
==== Start slapd with SSL ====
'''Note:''' This is not needed for StartTLS which listens on the same port as unencrypted LDAP.
You will have to edit the environment file read by <tt>slapd.service</tt> to change the protocol slapd listens on:
/etc/conf.d/slapd
SLAPD_URLS="ldaps:///"
SLAPD_OPTIONS=
Localhost connections do not need to use SSL. So, if you want to access the server locally you should change the <tt>SLAPD_URLS</tt> line to:
SLAPD_URLS="ldap://127.0.0.1 ldaps:///"
Then [https://wiki.archlinux.org/title/Restart restart] <tt>slapd.service</tt>. If it was enabled before, reenable it now.
'''Note:''' If you created a self-signed certificate above, be sure to add <tt>TLS_REQCERT allow</tt> to <tt>/etc/openldap/ldap.conf</tt> on the client, or it will not be able connect to the server.
== Next steps ==
You now have a basic LDAP installation. The next step is to design your directory. The design is heavily dependent on what you are using it for. If you are new to LDAP, consider starting with a directory design recommended by the specific client services that will use the directory ([https://wiki.archlinux.org/title/PAM PAM], [https://wiki.archlinux.org/title/Postfix Postfix], etc).
A directory for system authentication is the [https://wiki.archlinux.org/title/LDAP_authentication LDAP authentication] article.
A nice web frontend is [https://wiki.archlinux.org/title/PhpLDAPadmin phpLDAPadmin].
=== Backup LDAP ===
It is imperative that we have a backup of our LDAP database and configuration in case we ever need to restore for any number of reasons.
==== Export configuration ====
$ sudo -u ldap slapcat -vF /etc/openldap/slapd.d -n 0 -l "$(hostname)-ldap-mdb-config-$(date '+%F').ldif"
==== Export database ====
$ sudo -u ldap slapcat -v -n 1 -l "$(hostname)-ldap-database-$(date '+%F').ldif"
=== Restore LDAP ===
==== Import configuration ====
$ sudo -u ldap slapadd -v -n 0 -F /etc/openldap/slapd.d -l <filename from config export>
==== Import database ====
$ sudo -u ldap slapadd -v -n 1 -F /etc/openldap/slapd.d -l <filename from database export>
== Troubleshooting ==
=== slapd configuration checking ===
You can check configuration settings with
$ slaptest -F /etc/openldap/slapd.d/ -v
=== Client authentication checking ===
If you cannot connect to your server for non-secure authentication:
$ ldapsearch -x -H ldap://ldaservername:389 -D cn=Manager,dc=example,dc=exampledomain
and for TLS secured authentication with:
$ ldapsearch -x -H ldaps://ldaservername:636 -D cn=Manager,dc=example,dc=exampledomain
=== LDAP server stops suddenly ===
If you notice that slapd seems to start but then stops, try running:
<nowiki># chown -R ldap:ldap /var/lib/openldap</nowiki>
to allow slapd write access to its data directory as the user "ldap".
=== LDAP server does not start ===
Try starting the server from the command line with debugging output enabled:
<nowiki># slapd -u ldap -g ldap -h ldaps://ldaservername:636 -d Config,Stats</nowiki>
== Related articles ==
* [https://wiki.archlinux.org/title/LDAP_Authentication LDAP Authentication]
* [https://wiki.archlinux.org/title/LDAP_Hosts LDAP Hosts]
== See also ==
* [https://www.openldap.org/doc/admin24/ Official OpenLDAP Software 2.4 Administrator's Guide]
* [https://wiki.archlinux.org/title/PhpLDAPadmin phpLDAPadmin] is a web interface tool in the style of phpMyAdmin.
* [https://wiki.archlinux.org/title/LDAP_authentication LDAP authentication]
* [https://aur.archlinux.org/packages/apachedirectorystudio/ apachedirectorystudio]AUR from the [https://wiki.archlinux.org/title/Arch_User_Repository Arch User Repository] is an Eclipse-based LDAP viewer. Works perfect with OpenLDAP installations.
https://wiki.archlinux.org/title/OpenLDAP

Version vom 23. August 2022, 10:32 Uhr

topic kurze Beschreibung

Beschreibung

Installation

Anwendungen

Syntax

Optionen

Parameter

Umgebungsvariablen

Exit-Status

Konfiguration

Dateien

Sicherheit

Dokumentation

RFC

Man-Pages

Info-Pages

Siehe auch

Links

Projekt-Homepage

Weblinks

Einzelnachweise

Testfragen

Testfrage 1

Antwort1

Testfrage 2

Antwort2

Testfrage 3

Antwort3

Testfrage 4

Antwort4

Testfrage 5

Antwort5

TMP

OpenLDAP

OpenLDAP is an open-source implementation of the LDAP protocol. An LDAP server basically is a non-relational database which is optimised for accessing, but not writing, data. It is mainly used as an address book (for e.g. email clients) or authentication backend to various services (such as Samba, where it is used to emulate a domain controller, or Linux system authentication, where it replaces /etc/passwd) and basically holds the user data.

Note: Commands related to OpenLDAP that begin with ldap (like ldapsearch) are client-side utilities, while commands that begin with slap (like slapcat) are server-side.

This page is a starting point for a basic OpenLDAP installation and a sanity check.

Tip: Directory services are an enormous topic. Configuration can therefore be complex. If you are totally new to those concepts, this is a good introduction that is easy to understand and that will get you started, even if you are new to LDAP.

Installation

OpenLDAP contains both a LDAP server and client. Install it with the package openldap.

Configuration

The server

Note: * If you have an obsolete slapd.conf configuration, you can simply convert it into the new cn=config database.

  • If you already have an OpenLDAP database on your machine and would like to remove it, then it can be removed by deleting everything inside of /var/lib/openldap/openldap-data/. So, backup your DB_CONFIG.

Slapd, the server, stores its configuration directly inside its database. Thus, we need to write our configuration as an LDIF file and import it.

First, create a file config.ldif containing the following minimal sane configuration. This file declares the basic configuration structure and a database stored at /var/lib/openldap/openldap-data.

config.ldif # The root config entry dn: cn=config objectClass: olcGlobal cn: config olcArgsFile: /run/openldap/slapd.args olcPidFile: /run/openldap/slapd.pid

# Schemas dn: cn=schema,cn=config objectClass: olcSchemaConfig cn: schema

# TODO: Include further schemas as necessary include: file:///etc/openldap/schema/core.ldif

# The config database dn: olcDatabase=config,cn=config objectClass: olcDatabaseConfig olcDatabase: config olcRootDN: cn=Manager,$BASEDN

# The database for our entries dn: olcDatabase=mdb,cn=config objectClass: olcDatabaseConfig objectClass: olcMdbConfig olcDatabase: mdb olcSuffix: $BASEDN olcRootDN: cn=Manager,$BASEDN olcRootPW: $PASSWD olcDbDirectory: /var/lib/openldap/openldap-data # TODO: Create further indexes olcDbIndex: objectClass eq

There are a few options you will need to change: * Every occurence of $BASEDN must be replaced with a valid DN. If you own a domain example.com you will most likely want to choose dc=example,dc=com.

  • $PASSWD must be replaced by a salted and hashed password, which you may generate by running slappasswd.

Additionally, you might consider to add further schemas and create additional indexes to tune the performance of your database. The specifics will depend on your use case, but here are a few recommendations. For LDAP authentication, you should include the three schemas below to be able to use the posixAccount object class used for storing users.

# Additional schemas # RFC1274: Cosine and Internet X.500 schema include: file:///etc/openldap/schema/cosine.ldif # RFC2798: Internet Organizational Person include: file:///etc/openldap/schema/inetorgperson.ldif # RFC2307: An Approach for Using LDAP as a Network Information Service include: file:///etc/openldap/schema/nis.ldif

# Additional indexes olcDbIndex: objectClass eq olcDbIndex: uid pres,eq olcDbIndex: mail pres,sub,eq olcDbIndex: cn,sn pres,sub,eq olcDbIndex: dc eq

To import these settings as the ldap user using sudo:

# sudo -u ldap slapadd -n 0 -F /etc/openldap/slapd.d/ -l ./config.ldif

Alternatively, you may also run this directly as root. However, if you do, make sure /etc/openldap/slapd.d/ is accessible by ldap:

# slapadd -n 0 -F /etc/openldap/slapd.d/ -l ./config.ldif # chown -R ldap:ldap /etc/openldap/*

By default, OpenLDAP will listen unencrypted on all interfaces. To make it only listen on local IP interfaces, you may edit the environment file read by slapd.service:

/etc/conf.d/slapd SLAPD_URLS="ldap://127.0.0.1/ ldap://[::1]" SLAPD_OPTIONS= Finally, start the slapd daemon by starting slapd.service.

Note: * If you want to have your directory accept requests from the network, you should consider using TLS. See #OpenLDAP over TLS for details.

  • If you plan to use your LDAP server for authentication, you might want to check access control configuration in LDAP authentication#LDAP Server Setup.
  • Berkeley DB (BDB) should no longer be used. The mdb backend to slapd(8) is the recommended primary backend for a normal slapd database. It uses OpenLDAP's own Lightning Memory-Mapped Database (LMDB) library to store data and is intended to replace the Berkeley DB backend. The OpenLDAP package in the official repositories defaults to mdb.

The client

The client configuration file is located at /etc/openldap/ldap.conf.

It is quite simple: you will only have to alter BASE to reflect the suffix of the server, and URI to reflect the address of the server, like:

/etc/openldap/ldap.conf BASE dc=example,dc=com URI ldap://localhost

If you decide to use SSL: * The protocol (ldap or ldaps) in the URI entry has to conform with the slapd configuration

  • If you decide to use TLS, add a TLS_REQCERT allow line to ldap.conf
  • If you use a signed certificate from a CA, add the line TLS_CACERTDIR /usr/share/ca-certificates/trust-source in ldap.conf.

Create initial entry

Note: If you plan to use your LDAP server for authentication, you should import the base.ldif in the LDAP authentication article instead of following the instructions here.

Once your client is configured, you probably want to create the root entry, and an entry for the Manager role:

$ ldapadd -x -D 'cn=Manager,dc=example,dc=com' -W dn: dc=example,dc=com objectClass: dcObject objectClass: organization dc: example o: Example description: Example directory

dn: cn=Manager,dc=example,dc=com objectClass: organizationalRole cn: Manager description: Directory Manager ^D

The text after the first line is entered on stdin, or could be read from a file either with the -f option or a file redirect.

Test your new OpenLDAP installation

This is easy, just run the command below:

$ ldapsearch -x '(objectclass=*)' -b 'dc=example,dc=com'

Or authenticating as the rootdn (replacing -x by -D user -W), using the example configuration we had above:

$ ldapsearch -D "cn=Manager,dc=example,dc=com" -W '(objectclass=*)' -b 'dc=example,dc=com'

Now you should see some information about your database.

OpenLDAP over TLS

Note: upstream documentation is much more useful/complete than this section

If you access the OpenLDAP server over the network and especially if you have sensitive data stored on the server you run the risk of someone sniffing your data which is sent clear-text. The next part will guide you on how to setup an SSL connection between the LDAP server and the client so the data will be sent encrypted.

In order to use TLS, you must have a certificate. For testing purposes, a self-signed certificate will suffice. To learn more about certificates, see OpenSSL.

Warning: OpenLDAP cannot use a certificate that has a password associated to it.

Create a self-signed certificate

To create a self-signed certificate, type the following:

$ openssl req -new -x509 -nodes -out slapdcert.pem -keyout slapdkey.pem -days 365

You will be prompted for information about your LDAP server. Much of the information can be left blank. The most important information is the common name. This must be set to the DNS name of your LDAP server. If your LDAP server's IP address resolves to example.org but its server certificate shows a CN of bad.example.org, LDAP clients will reject the certificate and will be unable to negotiate TLS connections (apparently the results are wholly unpredictable).

Now that the certificate files have been created copy them to /etc/openldap/ssl/ (create this directory if it does not exist) and secure them. slapdcert.pem must be world readable because it contains the public key. slapdkey.pem on the other hand should only be readable for the ldap user for security reasons:

# mv slapdcert.pem slapdkey.pem /etc/openldap/ssl/ # chmod -R 755 /etc/openldap/ssl/ # chmod 400 /etc/openldap/ssl/slapdkey.pem # chmod 444 /etc/openldap/ssl/slapdcert.pem # chown ldap /etc/openldap/ssl/slapdkey.pem

Configure slapd for SSL

Edit the configuration to tell LDAP where the certificate files reside by executing the following command:

Note: The latest version of OpenLDAP (2.4.45) uses OpenSSL and not GnuTLS. This means that current versions of OpenLDAP do in fact know how to handle the DEFAULT TLSCipherSuite. To prove this one could run ldd /usr/bin/slapd.

ldapmodify -D 'cn=Manager,dc=example,dc=com' -W dn: cn=config add: olcTLSCertificateFile olcTLSCertificateFile: /etc/openldap/ssl/slapdcert.pem - add: olcTLSCertificateKeyFile olcTLSCertificateKeyFile: /etc/openldap/ssl/slapdkey.pem

If you are using a signed SSL Certificate from a certification authority such as Let’s Encrypt, you will also need to specify the path to the root certificates database and your intermediary certificate. You will also need to change ownership of the .pem files and intermediary directories to make them readable to the user ldap:

ldapmodify -D 'cn=Manager,dc=example,dc=com' -W dn: cn=config add: olcTLSCACertificateFile olcTLSCACertificateFile: /etc/letsencrypt/live/ldap.my-domain.com/chain.pem - add: olcTLSCertificateFile olcTLSCertificateFile: /etc/letsencrypt/live/ldap.my-domain.com/cert.pem - add: olcTLSCertificateKeyFile olcTLSCertificateKeyFile: /etc/letsencrypt/live/ldap.my-domain.com/privkey.pem - add: olcTLSCACertificatePath olcTLSCACertificatePath: /usr/share/ca-certificates/trust-source

SSLv2/v3

Disable SSLv2/v3 and use strong ciphers.

ldapmodify -D 'cn=Manager,dc=example,dc=com' -W dn: cn=config add: olcTLSProtocolMin olcTLSProtocolMin: 3.3 - add: olcTLSCipherSuite olcTLSCipherSuite: DEFAULT:!kRSA:!kDHE -

TLSProtocolMin specifies the minimum version in wire format, so "3.3" actually means TLSv1.2.

The TLSCipherSuite specifies a list of OpenSSL ciphers from which slapd will choose when negotiating TLS connections, in decreasing order of preference. In addition to those specific ciphers, you can use any of the wildcards supported by OpenSSL. Note: DEFAULT is a wildcard. See ciphers(1ssl) for description of ciphers, wildcards and options supported.

Note: To see which ciphers are supported by your local OpenSSL installation, type the following: openssl ciphers -v ALL:COMPLEMENTOFALL. Always test which ciphers will actually be enabled by TLSCipherSuite by providing it to OpenSSL command, like this: openssl ciphers -v 'DEFAULT'.

Start slapd with SSL

Note: This is not needed for StartTLS which listens on the same port as unencrypted LDAP.

You will have to edit the environment file read by slapd.service to change the protocol slapd listens on:

/etc/conf.d/slapd SLAPD_URLS="ldaps:///" SLAPD_OPTIONS= Localhost connections do not need to use SSL. So, if you want to access the server locally you should change the SLAPD_URLS line to:

SLAPD_URLS="ldap://127.0.0.1 ldaps:///"

Then restart slapd.service. If it was enabled before, reenable it now.

Note: If you created a self-signed certificate above, be sure to add TLS_REQCERT allow to /etc/openldap/ldap.conf on the client, or it will not be able connect to the server.

Next steps

You now have a basic LDAP installation. The next step is to design your directory. The design is heavily dependent on what you are using it for. If you are new to LDAP, consider starting with a directory design recommended by the specific client services that will use the directory (PAM, Postfix, etc).

A directory for system authentication is the LDAP authentication article.

A nice web frontend is phpLDAPadmin.

Backup LDAP

It is imperative that we have a backup of our LDAP database and configuration in case we ever need to restore for any number of reasons.

Export configuration

$ sudo -u ldap slapcat -vF /etc/openldap/slapd.d -n 0 -l "$(hostname)-ldap-mdb-config-$(date '+%F').ldif"

Export database

$ sudo -u ldap slapcat -v -n 1 -l "$(hostname)-ldap-database-$(date '+%F').ldif"

Restore LDAP

Import configuration

$ sudo -u ldap slapadd -v -n 0 -F /etc/openldap/slapd.d -l <filename from config export>

Import database

$ sudo -u ldap slapadd -v -n 1 -F /etc/openldap/slapd.d -l <filename from database export>

Troubleshooting

slapd configuration checking

You can check configuration settings with

$ slaptest -F /etc/openldap/slapd.d/ -v

Client authentication checking

If you cannot connect to your server for non-secure authentication:

$ ldapsearch -x -H ldap://ldaservername:389 -D cn=Manager,dc=example,dc=exampledomain

and for TLS secured authentication with:

$ ldapsearch -x -H ldaps://ldaservername:636 -D cn=Manager,dc=example,dc=exampledomain

LDAP server stops suddenly

If you notice that slapd seems to start but then stops, try running:

# chown -R ldap:ldap /var/lib/openldap

to allow slapd write access to its data directory as the user "ldap".

LDAP server does not start

Try starting the server from the command line with debugging output enabled:

# slapd -u ldap -g ldap -h ldaps://ldaservername:636 -d Config,Stats

Related articles

See also

https://wiki.archlinux.org/title/OpenLDAP