0 Votes

KerberosAndLDAP

Last modified by Sirius Rayner-Karlsson on 2024/05/09 10:54

Debian

The guide I followed was https://wiki.debian.org/LDAP/OpenLDAPSetup#Kerberos which while it worked required some minor tweaks. I obtained edit privileges for the Debian Wiki and updated the guide with the fixes that I found. I however have a Synology NAS and that can run an LDAP Server. So this guide differs from the upstream Debian Guide.

Assumption is that you have installed the LDAP Server package on your NAS and gone through initial configuration steps, so it has a domain, there is a DN you can bind as and so forth. It is also assumed you have a Debian system (12.5 or later, though this guide should work with 11.x and likely 10.x as well) that will become your KDC and KAdmin server.

Recommendation is that you create actual .ldif files rather than use here-documents as used in this guide. It is far easier to make adjustments to things if you have a file to edit rather than having to type it all out again or paste it and then have to try and make edits to it without making mistakes.

The guide is for illustration. Expectation is that you do not follow it verbatim but adapt it to your needs.

Install packages

The packages you need are krb5-kdc-ldap, krb5-admin-server for the actual KDC and schema2ldif  plus slapd for adding the schema and slappasswd. They are to be installed on your designated Debian host.

$ sudo apt install krb5-kdc-ldap krb5-admin-server schema2ldif

Load kerberos LDAP schema

Now you need to load the kerberos schema into the LDAP server on the Synology. Use the cn=config DN.

$ zcat /usr/share/doc/krb5-kdc-ldap/kerberos.openldap.ldif.gz | ldapadd -H ldaps://nas.example.com/ -D cn=config -W Enter LDAP Password: adding new entry "cn=kerberos,cn=schema,cn=config" $

Create Index on krbPrincipalName

Having an index on the krbPrincipalName improves performance. Synology OpenLDAP does not use mdb format, it uses bdb. Debian slapd uses mdb format. It is different database format, but the principle is the same. Again, as you are modifying config, the DN is cn=config. Use the main password you set for the Synology LDAP server. Create a file step1.ldif with the following content:

dn: olcDatabase={1}bdb,cn=config add: olcDbIndex olcDbIndex: krbPrincipalName eq,pres,sub

and apply it with

$ ldapmodify -H ldaps://nas.example.com/ -D cn=config -W -f step1.ldif Enter LDAP Password: modifying entry "olcDatabase={1}bdb,cn=config" $

Create principals kadmin and kdc

Next, you create and configure two entries which will be used by the Kerberos servers to connect to OpenLDAP. Not running the Kerberos KDC and Admin Server on the same host as OpenLDAP, these steps are required. Keeping things confined, everything will be created under a separate organizationalUnit. My guide differs from the official Debian guide here. Due to Synology OpenLDAP having a strict password policy, it was necessary to adjust the DNs of kdc and kadmin. The official guide use placeholder passwords which does not work with the Synology LDAP server.
Generate the passwords upfront with slappasswd -h {SSHA}. Then create a file step2.ldif with the following content:

dn: ou=kerberos,dc=example,dc=com objectClass: organizationalUnit objectClass: top ou: kerberos dn: cn=kdc,ou=kerberos,dc=example,dc=com cn: kdc sn: kdc objectClass: person objectClass: pwdPolicy pwdAttribute: userPassword pwdMinLength: 8 pwdCheckQuality: 2 pwdPolicySubentry: cn=kdc,ou=kerberos,dc=example,dc=com userPassword: {SSHA}<password-hash> description: Kerberos KDC Account dn: cn=kadmin,ou=kerberos,dc=example,dc=com cn: kadmin sn: kadmin objectClass: person objectClass: pwdPolicy pwdAttribute: userPassword pwdMinLength: 8 pwdCheckQuality: 2 pwdPolicySubentry: cn=kadmin,ou=kerberos,dc=example,dc=com userPassword: {SSHA}<password-hash> description: Kerberos KDC Account

Apply it with

$ ldapadd -H ldaps://nas.example.com/ -D uid=root,cn=users,dc=example,dc=com -W -f step2.ldif Enter LDAP Password: adding new entry "ou=kerberos,dc=example,dc=com" adding new entry "cn=kdc,ou=kerberos,dc=example,dc=com" adding new entry "cn=kadmin,ou=kerberos,dc=example,dc=com" $

A small note on this section:
The objectClass: pwdPolicy must be added to a, to LDAP, physical thing. objectClass: person fits the criteria, but can not have uid. So to make it work, the uid is replaced with sn and cn (yes, both are needed). Then you can set the other four attributes and add the hashed password you got from slappasswd.

Grant kdc and kadmin permissions

This switches back to the cn=config DN as you are changing the permissions. Note that we now reference our kdc and kadmin accounts and we grant them permission to the krbContainer which will house all our kerberos principals. Give both of them write access, because we do want to have the ability to track last login and lock accounts if there are  too many login failures. We like security.

Create step3.ldif with the following content:

dn: olcDatabase={1}bdb,cn=config add: olcAccess olcAccess: {0}to attrs=krbPrincipalKey by anonymous auth by dn.exact="cn=kdc,ou=kerberos,dc=example,dc=com" write by dn.exact="cn=kadmin,ou=kerberos,dc=example,dc=com" write by self write by * none - add: olcAccess olcAccess: {1}to dn.subtree="cn=krbContainer,ou=kerberos,dc=example,dc=com" by dn.exact="cn=kdc,ou=kerberos,dc=example,dc=com" write by dn.exact="cn=kadmin,ou=kerberos,dc=example,dc=com" write by * none

Do not get the domain part above wrong. If you do, you may not be able to use kadmin or kinit and fixing the permissions without breaking something else is a nervous task. Trust me on this (as I screwed them up).

Apply it with

$ ldapmodify -H ldaps://nas.example.com -W -D cn=config -f step3.ldif Enter LDAP Password: modifying entry "olcDatabase={1}bdb,cn=config" $

Create krb5.conf

Next we create (or modify)  /etc/krb5.conf so that it will point to the right thing later. It should look something like this:

[libdefaults] default_realm = EXAMPLE.COM dns_lookup_realm = false dns_lookup_kdc = false ticket_lifetime = 24h forwardable = true proxiable = true rdns = false [realms] EXAMPLE.COM = { kdc = debian.example.com admin_server = debian.example.com default_domain = example.com } [domain_realm] .example.com = EXAMPLE.COM example.com = EXAMPLE.COM

Make sure your designated debian server have ports 88, 464 and 749 open, both for TCP and UDP, in its firewall. 88 is for the kdc, 464 and 749 is for kadmin.

Create kdc.conf

Now we do /etc/krb5kdc/kdc.conf. Something like this should work

[libdefaults] [realms] EXAMPLE.COM = { database_module = openldap_ldapconf max_life = 7d max_renewable_life = 6d } [dbdefaults] ldap_kerberos_container_dn = cn=krbContainer,ou=kerberos,dc=example,dc=com [dbmodules] openldap_ldapconf = { db_library = kldap disable_last_success = false disable_lockout = false ldap_conns_per_server = 5 ldap_servers = ldaps:~/~/nas.example.com ldap_kdc_dn = "cn=kdc,ou=kerberos,dc=example,dc=com" ldap_kadmind_dn = "cn=kadmin,ou=kerberos,dc=example,dc=com" ldap_service_password_file = /etc/krb5kdc/service.keyfile }

Create kadm5.acl

Create /etc/krb5kdc/kadm5.acl with the following content so that kerberos administrator principals can run kadmin

*/admin@EXAMPLE.COM *

Create the kerberos domain

Now we are ready to create the domain. And that we do with kdb5_ldap_util as root. Note that this commandline is deceptive and you need all of it.

# kdb5_ldap_util -D uid=root,cn=users,dc=example,dc=com -H ldaps://nas.example.com -r EXAMPLE.COM create -subtrees dc=example,dc=com -maxtktlife '7 Days' -maxrenewlife '6 Days' -s Password for "uid=root,cn=users,dc=trudheim,dc=com": Initializing database for realm 'EXAMPLE.COM' You will be prompted for the database Master Password. It is important that you NOT FORGET this password. Enter KDC database master key: Re-enter KDC database master key to verify: #

Stash the passwords for kdc and kadmin

Most likely, you will want your KDC and KAdmin server to start at boot, and for that, we can stash the passwords (into LDAP) for cn=kdc,ou=kerberos,dc=example,dc=com and cn=kdc,ou=kerberos,dc=example,dc=com using the same tool as in the previous step.

# kdb5_ldap_util -D uid=root,cn=users,dc=example,dc=com -H ldaps://nas.example.com stashsrvpw -f /etc/krb5kdc/service.keyfile cn=kdc,ou=kerberos,dc=example,dc=com Password for "uid=root,cn=users,dc=example,dc=com": Password for "cn=kdc,ou=kerberos,dc=example,dc=com": Re-enter password for "cn=kdc,ou=kerberos,dc=example,dc=com": # kdb5_ldap_util -D uid=root,cn=users,dc=example,dc=com -H ldaps://nas.example.com stashsrvpw -f /etc/krb5kdc/service.keyfile cn=kadmin,ou=kerberos,dc=example,dc=com Password for "uid=root,cn=users,dc=example,dc=com": Password for "cn=kadmin,ou=kerberos,dc=example,dc=com": Re-enter password for "cn=kadmin,ou=kerberos,dc=example,dc=com": #

Start the services

We are now ready to start the services. If this does not work, you need to backtrack and find out where you made a mistake, rectify it, and then walk forward again.

# systemctl enable --now krb5-kdc krb5-admin-server #

Check that they are running and not giving off errors with systemctl status krb5-kdc and systemctl status krb5-admin-server.

Create the first regular principals

Providing you got to here without issues it is now time to generate your principals that you will use to authenticate into systems with. Run kadmin.local as root to create first a regular user, and then an admin version of that user.

# kadmin.local Authenticating as principal root/admin@EXAMPLE.COM with password. kadmin.local:  addprinc user No policy specified for user@EXAMPLE.COM; defaulting to no policy Enter password for principal "user@EXAMPLE.COM": Re-enter password for principal "user@EXAMPLE.COM": Principal "user@EXAMPLE.COM" created. kadmin.local:  addprinc user/admin No policy specified for user/admin@EXAMPLE.COM; defaulting to no policy Enter password for principal "user/admin@EXAMPLE.COM": Re-enter password for principal "user/admin@EXAMPLE.COM": Principal "user/admin@EXAMPLE.COM" created. kadmin.local:  q #

Worth to note here is that user@EXAMPLE.COM and user/admin@EXAMPLE.COM can have (and probably should have) different passwords. The admin variant is allowed to do things to the kerberos database and should therefore have greater security. This is why you want to have the registering of failures to login enabled. Should you have the system exposed to the internet, you can and should expect intrusion attempts. Having Kerberos deployed makes it harder for perpetrators to gain access, but not impossible.

If you later kerberise your storage and leverage it for NFS4 mounts from your NAS, you can have NFS exposed to the internet as well. Unless someone has a valid kerberos ticket, even if they somehow could mount a share, they see nothing on it without the krbtgt.

Test your new principal

Acid test is, can you authenticate with kinit?

$ kinit user@EXAMPLE.COM Password for user@EXAMPLE.COM: $ klist Ticket cache: FILE:/tmp/krb5cc_1000 Default principal: user@EXAMPLE.COM Valid starting     Expires            Service principal 09/05/24 08:07:35  10/05/24 08:07:35  krbtgt/EXAMPLE.COM@EXAMPLE.COM $

Congratulations - you now have Kerberos working, and to boot, the database sits in LDAP. Which you can inspect with something like:

$ ldapsearch -H ldaps://nas.example.com -D uid=root,cn=users,dc=example,dc=com -W -b ou=kerberos,dc=example,dc=com

Set up pam and sssd

 To fully leverage your shiny new KDC, you will want to install the Kerberos authentication pieces for PAM and also sssd to facilitate caching of authentication, in case your KDC is offline for some reason when you try to authenticate into another system.

$ sudo apt install krb5-user libpam-krb5 sssd-krb5 ... $ sudo pam-auth-update

With pam-auth-update you want to enable Kerberos and SSS authentication (and flip on the auto-creation of home directories while you are there). If you now want to test login on another system with kerberos, you need /etc/krb5.conf and the krb5-user, libpam-krb5 and sssd-krb5 packages installed on this new system and you need to run pam-auth-update to enable Kerberos and SSS. After that, the system does not need local user (in /etc/passwd) - the kerberos user will work instead.

/S