(kerberos-with-openldap-backend)= # How to set up Kerberos with OpenLDAP backend Kerberos supports a few different database backends. The default one (which we have used in our other Kerberos guides so far) is called `db2`. The [DB types documentation](https://web.mit.edu/kerberos/krb5-latest/doc/admin/dbtypes.html) shows all the options, one of which is LDAP. ## Why use LDAP? There are several reasons why one would want to have the Kerberos principals stored in LDAP as opposed to a local on-disk database. There are also cases when it is not a good idea. Each site has to evaluate the pros and cons. Here are a few: - Pros: - OpenLDAP replication is faster and more robust than the native Kerberos one, based on a cron job - If you already have OpenLDAP set up for other things, such as storing users and groups, adding the Kerberos attributes can be beneficial, providing an integrated story - Cons: - Setting up the LDAP backend isn't a trivial task and shouldn't be attempted by administrators without prior knowledge of OpenLDAP - As highlighted in the [LDAP section of DB types](https://web.mit.edu/kerberos/krb5-latest/doc/admin/dbtypes.html#ldap-module-kldap), since `krb5kdc` is single-threaded there may be higher latency in servicing requests when using the OpenLDAP backend ## In this guide In this section we'll configure a primary and secondary Kerberos server to use OpenLDAP for the principal database. Note that as of version 1.18, the Key Distribution Center (KDC) from MIT Kerberos [does not support](https://krbdev.mit.edu/rt/Ticket/Display.html?id=7754#) a primary KDC using a read-only consumer (secondary) LDAP server. What we have to consider here is that a primary KDC is read-write, and it needs a read-write backend. The secondary KDCs can use both a read-write and read-only backend, because they are expected to be read-only. Therefore there are only some possible layouts we can use: 1. **Simple case**: - Primary KDC connected to primary OpenLDAP - Secondary KDC connected to both primary and secondary OpenLDAP 1. **Extended simple case**: - Multiple primary KDCs connected to one primary OpenLDAP - Multiple secondary KDCs connected to primary and secondary OpenLDAP 1. **OpenLDAP with multi-master replication**: - Multiple primary KDCs connected to all primary OpenLDAP servers We haven't covered OpenLDAP multi-master replication in this guide, so we will show the **simple case** only. The second scenario is an extension: just add another primary KDC to the mix, talking to the same primary OpenLDAP server. ## Configure OpenLDAP We are going to install the OpenLDAP server on the same host as the KDC, to simplify the communication between them. In such a setup, we can use the `ldapi:///` transport, which is via a UNIX socket, and we don't need to set up SSL certificates to secure the communication between the Kerberos services and OpenLDAP. Note, however, that SSL is still needed for the OpenLDAP replication. See {ref}`LDAP with TLS ` for details. If you want to use an existing OpenLDAP server, that's also possible, but keep in mind that you should then use SSL for the communication between the KDC and this OpenLDAP server. First, the necessary **schema** needs to be loaded on an OpenLDAP server that has network connectivity to both the **primary** and **secondary** KDCs. The rest of this section assumes that you also have LDAP replication configured between at least two servers. For information on setting up OpenLDAP see {ref}`OpenLDAP Server `. > **Note**: > `cn=admin,dc=example,dc=com` is a default admin user that is created during the installation of the `slapd` package (the OpenLDAP server). The domain component will change for your server, so adjust accordingly. - Install the necessary packages (it's assumed that OpenLDAP is already installed): ```bash sudo apt install krb5-kdc-ldap krb5-admin-server ``` - Next, extract the `kerberos.schema.gz` file: ```bash sudo cp /usr/share/doc/krb5-kdc-ldap/kerberos.schema.gz /etc/ldap/schema/ sudo gunzip /etc/ldap/schema/kerberos.schema.gz ``` - The **Kerberos schema** needs to be added to the `cn=config` tree. This schema file needs to be converted to LDIF format before it can be added. For that we will use a helper tool, called `schema2ldif`, provided by the package of the same name which is available in the Universe archive: ```bash sudo apt install schema2ldif ``` - To import the Kerberos schema, run: ```bash $ sudo ldap-schema-manager -i kerberos.schema SASL/EXTERNAL authentication started SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth SASL SSF: 0 executing 'ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/ldap/schema/kerberos.ldif' SASL/EXTERNAL authentication started SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth SASL SSF: 0 adding new entry "cn=kerberos,cn=schema,cn=config" ``` - With the new schema loaded, let's index an attribute often used in searches: ```bash $ sudo ldapmodify -Q -Y EXTERNAL -H ldapi:/// < **Note**: > The `/etc/krb5kdc/service.keyfile` file now contains clear text versions of the passwords used by the KDC to contact the LDAP server! - Create a `/etc/krb5kdc/kadm5.acl` file for the admin server, if you haven't already: ```text */admin@EXAMPLE.COM * ``` - Start the Kerberos KDC and admin server: ```bash sudo systemctl start krb5-kdc.service krb5-admin-server.service ``` You can now add Kerberos principals to the LDAP database, and they will be copied to any other LDAP servers configured for replication. To add a principal using the `kadmin.local` utility enter: ```bash $ sudo kadmin.local Authenticating as principal root/admin@EXAMPLE.COM with password. kadmin.local: addprinc ubuntu WARNING: no policy specified for ubuntu@EXAMPLE.COM; defaulting to no policy Enter password for principal "ubuntu@EXAMPLE.COM": Re-enter password for principal "ubuntu@EXAMPLE.COM": Principal "ubuntu@EXAMPLE.COM" created. kadmin.local: ``` The above will create an `ubuntu` principal with a DN of `krbPrincipalName=ubuntu@EXAMPLE.COM,cn=EXAMPLE.COM,cn=krbContainer,dc=example,dc=com`. Let's say, however, that you already have a user in your directory, and it's in `uid=testuser1,ou=People,dc=example,dc=com`. How can you add the Kerberos attributes to it? You use the `-x` parameter to specify the location. For the `ldap_kadmin_dn` to be able to write to it, we first need to update the ACLs: ```bash $ sudo ldapmodify -Q -Y EXTERNAL -H ldapi:/// < **Note**: > The `ldap_kadmin_dn` DN (`uid=kadmin-service` in our example) does not have write access to the location specified by the `-x` parameter, you will get an `Insufficient access` error. Both places are visible for `kinit`, since, when the realm was created with `kdb5_ldap_util`, the default value for the search scope and base were taken: `subtree`, and `dc=example,dc=com`. ## Secondary KDC configuration (LDAP) The setup of the secondary KDC (and its OpenLDAP replica) is very similar. Once you have the OpenLDAP replication setup, repeat these steps on the secondary: - Install `krb5-kdc-ldap`, `ldap-utils`. Do **not** install `krb5-admin-server`. - Load the Kerberos schema using `schema2ldif`. - Add the index for `krbPrincipalName`. - Add the ACLs. - Configure `krb5.conf` in the same way, initially. If you want to, and if you configured SSL properly, you can add `ldaps://kdc01.example.com` to the `ldap_servers` list after `ldapi:///`, so that the secondary KDC can have two LDAP backends at its disposal. - **DO NOT** run `kdb5_ldap_util`. There is no need to create the database since it's being replicated from the primary. - Copy over the following files from the primary KDC and place them in the same location on the secondary: - `/etc/krb5kdc/stash` - `/etc/krb5kdc/service.keyfile` - Start the KDC: `sudo systemctl start krb5-kdc.service` ## Resources - [Configuring Kerberos with OpenLDAP back-end](https://web.mit.edu/kerberos/krb5-latest/doc/admin/conf_ldap.html#conf-ldap) - [MIT Kerberos backend types](https://web.mit.edu/kerberos/krb5-latest/doc/admin/dbtypes.html)