slapd:\n Installed: (none)\n Candidate: 2.6.7+dfsg-1~exp1ubuntu1\n Version table:\n 2.6.7+dfsg-1~exp1ubuntu1 500\n 500 http:\/\/archive.ubuntu.com\/ubuntu noble\/main amd64 Packages\n<\/code><\/pre>\n\n\n\nHence, you can install OpenLDAP on Ubuntu 24.04 using the command below;<\/p>\n\n\n\n
sudo apt install slapd ldapscripts<\/code><\/pre>\n\n\n\nDuring the installation, you will be prompted to set the OpenLDAP administrative password.<\/p>\n\n\n\n\n\t\t\t\n\t\t\t\t \n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\nYou can check the default OpenLDAP database settings using the slapcat<\/strong> command.<\/p>\n\n\n\nslapcat<\/code><\/pre>\n\n\n\ndn: dc=nodomain\nobjectClass: top\nobjectClass: dcObject\nobjectClass: organization\no: nodomain\ndc: nodomain\nstructuralObjectClass: organization\nentryUUID: fa7581ea-6787-103e-9c50-bf1e7fcd254f\ncreatorsName: cn=admin,dc=nodomain\ncreateTimestamp: 20240224174335Z\nentryCSN: 20240224174335.131130Z#000000#000#000000\nmodifiersName: cn=admin,dc=nodomain\nmodifyTimestamp: 20240224174335Z\n<\/code><\/pre>\n\n\n\nBased on the SLAPD database configuration output above,<\/p>\n\n\n\n
\nThe Base DN is set to dn: dc=nodomain<\/code>.<\/li>\n\n\n\nThe Organization name is set to o: nodomain<\/code>.<\/li>\n\n\n\nThe LDAP admin Base DN entry is set to cn=admin,dc=nodomain<\/code>.<\/li>\n<\/ul>\n\n\n\nUpdate OpenLDAP Database<\/h3>\n\n\n\n Based on your organization setup, you need to update the OpenLDAP database.<\/p>\n\n\n\n
Thus, you need to reconfigure slapd package as shown below and follow through the prompts.<\/p>\n\n\n\n
dpkg-reconfigure slapd<\/code><\/pre>\n\n\n\nWhen the command runs, you are prompted on whether to omit OpenLDAP server configuration. Select No<\/strong> to have the configuration created for you.<\/p>\n\n\n\n\n\t\t\t\n\t\t\t\t \n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\nUpdate the DNS domain name for constructing the base DN of the LDAP directory.<\/p>\n\n\n\n <\/figure>\n\n\n\nDefine the name of your Organization for use in the base DN.<\/p>\n\n\n\n\n\t\t\t\n\t\t\t\t \n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\nReset the OpenLDAP administrator password.<\/p>\n\n\n\n\n\t\t\t\n\t\t\t\t \n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\nChoose whether to remove the OpenLDAP database whenever you purge the OpenLDAP package, slapd.<\/p>\n\n\n\n\n\t\t\t\n\t\t\t\t \n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\nRemove old OpenLDAP database configuration files to finalize the reconfiguration. The old database is stored on \/var\/backups<\/code>.<\/p>\n\n\n\n\n\t\t\t\n\t\t\t\t \n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\nCheck the OpenLDAP database again after reconfiguration.<\/p>\n\n\n\n
slapcat<\/code><\/pre>\n\n\n\ndn: dc=ldap,dc=kifarunix,dc=com\nobjectClass: top\nobjectClass: dcObject\nobjectClass: organization\no: kifarunix.com\ndc: ldap\nstructuralObjectClass: organization\nentryUUID: 35f90bca-678c-103e-86f7-25bd8e4e56dc\ncreatorsName: cn=admin,dc=ldap,dc=kifarunix,dc=com\ncreateTimestamp: 20240224181352Z\nentryCSN: 20240224181352.965664Z#000000#000#000000\nmodifiersName: cn=admin,dc=ldap,dc=kifarunix,dc=com\nmodifyTimestamp: 20240224181352Z\n<\/code><\/pre>\n\n\n\nYou can also check LDAP Base DN using the ldapsearch<\/code> command as shown below;<\/p>\n\n\n\nldapsearch -H ldapi:\/\/\/ -x -LLL -s base -b \"\" namingContexts<\/code><\/pre>\n\n\n\ndn:\nnamingContexts: dc=ldap,dc=kifarunix,dc=com\n<\/code><\/pre>\n\n\n\nTo view the RootDN, run the command below<\/p>\n\n\n\n
ldapsearch -H ldapi:\/\/\/ -Y EXTERNAL -b \"cn=config\" \"(olcRootDN=*)\"<\/code><\/pre>\n\n\n\nsample output;<\/p>\n\n\n\n
SASL\/EXTERNAL authentication started\nSASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth\nSASL SSF: 0\n# extended LDIF\n#\n# LDAPv3\n# base with scope subtree\n# filter: (olcRootDN=*)\n# requesting: ALL\n#\n\n# {0}config, config\ndn: olcDatabase={0}config,cn=config\nobjectClass: olcDatabaseConfig\nolcDatabase: {0}config\nolcAccess: {0}to * by dn.exact=gidNumber=0+uidNumber=0,cn=peercred,cn=external\n ,cn=auth manage by * break\nolcRootDN: cn=admin,cn=config\n\n# {1}mdb, config\ndn: olcDatabase={1}mdb,cn=config\nobjectClass: olcDatabaseConfig\nobjectClass: olcMdbConfig\nolcDatabase: {1}mdb\nolcDbDirectory: \/var\/lib\/ldap\nolcSuffix: dc=ldap,dc=kifarunix,dc=com\nolcAccess: {0}to attrs=userPassword by self write by anonymous auth by * none\nolcAccess: {1}to attrs=shadowLastChange by self write by * read\nolcAccess: {2}to * by * read\nolcLastMod: TRUE\nolcRootDN: cn=admin,dc=ldap,dc=kifarunix,dc=com\nolcRootPW: {SSHA}+fuCaVAvF5wkhXdzsQzGyj9\/YWu+kVRB\nolcDbCheckpoint: 512 30\nolcDbIndex: objectClass eq\nolcDbIndex: cn,uid eq\nolcDbIndex: uidNumber,gidNumber eq\nolcDbIndex: member,memberUid eq\nolcDbMaxSize: 1073741824\n\n# search result\nsearch: 2\nresult: 0 Success\n\n# numResponses: 3\n# numEntries: 2\n<\/code><\/pre>\n\n\n\nTo test the connection to LDAP server, use the ldapwhoami<\/code> command as shown below.<\/p>\n\n\n\nldapwhoami -H ldapi:\/\/\/ -x<\/code><\/pre>\n\n\n\nanonymous<\/code><\/pre>\n\n\n\nThe expected output is anonymous<\/code> if the connection to LDAP server is fine since the test is run without logging in to LDAP server.<\/p>\n\n\n\nTo search for all the DNs based on the Base DN;<\/p>\n\n\n\n
ldapsearch -H ldapi:\/\/\/ -x -LLL -b dc=ldap,dc=kifarunix,dc=com dn<\/code><\/pre>\n\n\n\ndn: dc=ldap,dc=kifarunix,dc=com<\/code><\/pre>\n\n\n\nConfigure OpenLDAP with SSL\/TLS<\/h3>\n\n\n\n In this guide, we are going to use self signed certificates. You can as well use commercial SSL\/TLS certificates from your trusted CA if you have them.<\/p>\n\n\n\n
To configure OpeLDAP server with SSL\/TLS certificate, you need a CA certificate<\/code>, server certificate<\/code> and server certificate key<\/code> file.<\/p>\n\n\n\nGenerate SSL\/TLS Certificate Files<\/h4>\n\n\n\n Create a directory to store the certificates.<\/p>\n\n\n\n
mkdir -p \/etc\/ssl\/openldap\/{private,certs,newcerts}<\/code><\/pre>\n\n\n\nOnce you have created the directories above, open the \/usr\/lib\/ssl\/openssl.cnf<\/code> configuration file and set the directory for storing SSL\/TLS certificates and keys under the [ CA_default ]<\/code> section. It is set to \/usr<\/p>\n\n\n\nvim \/usr\/lib\/ssl\/openssl.cnf<\/code><\/pre>\n\n\n\n...\n####################################################################\n[ CA_default ]\n\n#dir = .\/demoCA # Where everything is kept\ndir = \/etc\/ssl\/openldap\n<\/strong>certs = $dir\/certs # Where the issued certs are kept\ncrl_dir = $dir\/crl # Where the issued crl are kept\ndatabase = $dir\/index.txt # database index file.\n#unique_subject = no # Set to 'no' to allow creation of\n # several certs with same subject.\nnew_certs_dir = $dir\/newcerts # default place for new certs.\n...\n<\/code><\/pre>\n\n\n\nYou also need some files for tracking the signed certificates.<\/p>\n\n\n\n
echo \"1001\" > \/etc\/ssl\/openldap\/serial<\/code><\/pre>\n\n\n\ntouch \/etc\/ssl\/openldap\/index.txt<\/code><\/pre>\n\n\n\nCreate a CA Key file by running the command below. When prompted, enter the passphrase.<\/p>\n\n\n\n
openssl genrsa -aes256 \\\n\t-out \/etc\/ssl\/openldap\/private\/cakey.pem \\\n\t4096\n<\/code><\/pre>\n\n\n\nTo remove the passphrase from the CA key;<\/p>\n\n\n\n
openssl rsa \\\n\t-in \/etc\/ssl\/openldap\/private\/cakey.pem \\\n\t-out \/etc\/ssl\/openldap\/private\/cakey.pem\n<\/code><\/pre>\n\n\n\nCreate the CA certificate. Be sure to set the common to match your server FQDN.<\/p>\n\n\n\n
openssl req -new -x509 \\\n\t-days 3650 \\\n\t-key \/etc\/ssl\/openldap\/private\/cakey.pem \\\n\t-out \/etc\/ssl\/openldap\/certs\/cacert.pem\n<\/code><\/pre>\n\n\n\nYou can also simplify the process using the -subj option to specify the information. The subject information typically includes details like country<\/strong>, state<\/strong>, locality<\/strong>, organization<\/strong>, organizational unit<\/strong>, common name<\/strong>, and email address<\/strong>. Here’s an example command with the -subj<\/code> option added:<\/p>\n\n\n\nopenssl req -new -x509 \\\n\t-days 3650 \\\n\t-key \/etc\/ssl\/openldap\/private\/cakey.pem \\\n\t-out \/etc\/ssl\/openldap\/certs\/cacert.pem \\\n\t-subj \"\/C=US\/ST=California\/L=SanFrancisco\/O=Kifarunix Inc\/OU=IT Infrastructure\/CN=ldap.kifarunix.com\/emailAddress=admin@kifarunix.com\"<\/strong>\n<\/code><\/pre>\n\n\n\nYou can also use wildcard SSL cert to match your various domain names;<\/p>\n\n\n\n
openssl req -new -x509 \\\n\t-days 3650 \\\n\t-key \/etc\/ssl\/openldap\/private\/cakey.pem \\\n\t-out \/etc\/ssl\/openldap\/certs\/cacert.pem \\\n\t-subj \"\/C=US\/ST=California\/L=SanFrancisco\/O=Kifarunix Inc\/OU=IT Infrastructure\/CN=ldap.kifarunix.com\/emailAddress=admin@kifarunix.com\" \\\n\t-addext \"subjectAltName = DNS:*.kifarunix.com,DNS:kifarunix.com\"\n<\/code><\/pre>\n\n\n\nAlso specify the extensions in the openssl.cnf<\/strong> configuration.<\/p>\n\n\n\nvim \/usr\/lib\/ssl\/openssl.cnf<\/code><\/pre>\n\n\n\n####################################################################\n[ req ]\ndefault_bits = 2048\ndefault_keyfile = privkey.pem\ndistinguished_name = req_distinguished_name\nattributes = req_attributes\nx509_extensions = v3_ca # The extensions to add to the self signed cert\n\n# Passwords for private keys if not present they will be prompted for\n# input_password = secret\n# output_password = secret\n\n# This sets a mask for permitted string types. There are several options.\n# default: PrintableString, T61String, BMPString.\n# pkix : PrintableString, BMPString (PKIX recommendation before 2004)\n# utf8only: only UTF8Strings (PKIX recommendation after 2004).\n# nombstr : PrintableString, T61String (no BMPStrings or UTF8Strings).\n# MASK:XXXX a literal mask value.\n# WARNING: ancient versions of Netscape crash on BMPStrings or UTF8Strings.\nstring_mask = utf8only\n\nreq_extensions = v3_req # The extensions to add to a certificate request <\/strong>\n\n<\/code><\/pre>\n\n\n\nWe define the extensions to add to a certificate request <\/strong>under the section v3_req<\/strong>.<\/p>\n\n\n\n[ v3_req ]\n\n# Extensions to add to a certificate request\n\nbasicConstraints = CA:FALSE\nkeyUsage = nonRepudiation, digitalSignature, keyEncipherment\nsubjectAltName = @alt_names\n\n[alt_names]\nDNS.1 = *.kifarunix.com\nDNS.2 = kifarunix.com<\/strong>\n\n<\/code><\/pre>\n\n\n\nNext generate LDAP server key;<\/p>\n\n\n\n
openssl genrsa -aes256 -out \/etc\/ssl\/openldap\/private\/ldapserver-key.key 4096<\/code><\/pre>\n\n\n\nRemove assigned key passphrase.<\/p>\n\n\n\n
openssl rsa \\\n\t-in \/etc\/ssl\/openldap\/private\/ldapserver-key.key \\\n\t-out \/etc\/ssl\/openldap\/private\/ldapserver-key.key\n<\/code><\/pre>\n\n\n\nGenerate the certificate signing request (CSR). Be sure to configure the same details as you did when generating the CA certificate file above.<\/p>\n\n\n\n
openssl req -new \\\n\t-key \/etc\/ssl\/openldap\/private\/ldapserver-key.key \\\n\t-out \/etc\/ssl\/openldap\/certs\/ldapserver-cert.csr \\\n\t-subj \"\/C=US\/ST=California\/L=SanFrancisco\/O=Kifarunix Inc\/OU=IT Infrastructure\/CN=ldap.kifarunix.com\/emailAddress=admin@kifarunix.com\"\n<\/code><\/pre>\n\n\n\nGenerate the LDAP server certificate and sign it with CA key and certificate generated above.<\/p>\n\n\n\n
openssl ca -keyfile \/etc\/ssl\/openldap\/private\/cakey.pem \\\n\t-cert \/etc\/ssl\/openldap\/certs\/cacert.pem \\\n\t-in \/etc\/ssl\/openldap\/certs\/ldapserver-cert.csr \\\n\t-out \/etc\/ssl\/openldap\/certs\/ldapserver-cert.crt\n<\/code><\/pre>\n\n\n\nSample output;<\/p>\n\n\n\n
Using configuration from \/usr\/lib\/ssl\/openssl.cnf\nCheck that the request matches the signature\nSignature ok\nCertificate Details:\n Serial Number: 4097 (0x1001)\n Validity\n Not Before: Feb 24 20:54:50 2024 GMT\n Not After : Feb 23 20:54:50 2025 GMT\n Subject:\n countryName = US\n stateOrProvinceName = California\n organizationName = Kifarunix Inc\n organizationalUnitName = IT Infrastructure\n commonName = ldap.kifarunix.com\n emailAddress = admin@kifarunix.com\n X509v3 extensions:\n X509v3 Basic Constraints: \n CA:FALSE\n X509v3 Subject Key Identifier: \n 41:68:00:99:97:5D:83:D8:E3:88:2C:43:9D:5B:0A:B7:33:2A:F0:44\n X509v3 Authority Key Identifier: \n BC:3C:1E:3C:D2:C4:BA:FD:5A:DB:AD:9B:90:A8:BD:57:4D:85:C1:B9\nCertificate is to be certified until Feb 23 20:54:50 2025 GMT (365 days)\nSign the certificate? [y\/n]:y\n\n\n1 out of 1 certificate requests certified, commit? [y\/n]y\nWrite out database with 1 new entries\nDatabase updated\n<\/code><\/pre>\n\n\n\nTo verify the LDAP server againt the CA;<\/p>\n\n\n\n
openssl verify -CAfile \/etc\/ssl\/openldap\/certs\/cacert.pem \/etc\/ssl\/openldap\/certs\/ldapserver-cert.crt<\/code><\/pre>\n\n\n\n\/etc\/ssl\/openldap\/certs\/ldapserver-cert.crt: OK<\/strong><\/code><\/pre>\n\n\n\nNow, we have:<\/p>\n\n\n\n
\nthe CA certificate file: \/etc\/ssl\/openldap\/certs\/cacert.pem<\/strong><\/li>\n\n\n\nthe server certificate: \/etc\/ssl\/openldap\/certs\/ldapserver-cert.crt<\/strong><\/li>\n\n\n\nthe server key file: \/etc\/ssl\/openldap\/private\/ldapserver-key.key<\/strong><\/li>\n<\/ul>\n\n\n\nNext, set the ownership of the OpenLDAP certificates directory to openldap<\/code> user.<\/p>\n\n\n\nchown -R openldap: \/etc\/ssl\/openldap\/<\/code><\/pre>\n\n\n\nUpdate OpenLDAP Server TLS Certificates<\/h4>\n\n\n\n Next, you need to update the OpenLDAP Server TLS certificates. Therefore, create the an LDIF file to define the TLS attributes as shown below;<\/p>\n\n\n\n
vim ldap-tls.ldif<\/code><\/pre>\n\n\n\ndn: cn=config\nchangetype: modify\nadd: olcTLSCACertificateFile\nolcTLSCACertificateFile: \/etc\/ssl\/openldap\/certs\/cacert.pem\n-\nreplace: olcTLSCertificateFile\nolcTLSCertificateFile: \/etc\/ssl\/openldap\/certs\/ldapserver-cert.crt\n-\nreplace: olcTLSCertificateKeyFile\nolcTLSCertificateKeyFile: \/etc\/ssl\/openldap\/private\/ldapserver-key.key\n<\/code><\/pre>\n\n\n\nReplace the locations of your certificates and key files accordingly.<\/p>\n\n\n\n
To update the LDAP database, use ldapmodify<\/code> command as shown below;<\/p>\n\n\n\nldapmodify -Y EXTERNAL -H ldapi:\/\/\/ -f ldap-tls.ldif<\/code><\/pre>\n\n\n\nSASL\/EXTERNAL authentication started\nSASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth\nSASL SSF: 0\nmodifying entry \"cn=config\"\n<\/code><\/pre>\n\n\n\nTo verify that the files are in place;<\/p>\n\n\n\n
slapcat -b \"cn=config\" | grep -E \"olcTLS\"<\/code><\/pre>\n\n\n\nolcTLSCACertificateFile: \/etc\/ssl\/openldap\/certs\/cacert.pem\nolcTLSCertificateFile: \/etc\/ssl\/openldap\/certs\/ldapserver-cert.crt\nolcTLSCertificateKeyFile: \/etc\/ssl\/openldap\/private\/ldapserver-key.key\n<\/code><\/pre>\n\n\n\nTo check the validity of the LDAP configuration, run the command below;<\/p>\n\n\n\n
slaptest -u<\/code><\/pre>\n\n\n\nconfig file testing succeeded<\/code><\/pre>\n\n\n\nNext, open the \/etc\/ldap\/ldap.conf<\/code> configuration file and change the location of the CA certificate.<\/p>\n\n\n\nvim \/etc\/ldap\/ldap.conf<\/code><\/pre>\n\n\n\n...\n# TLS certificates (needed for GnuTLS)\n#TLS_CACERT\t\/etc\/ssl\/certs\/ca-certificates.crt\nTLS_CACERT\t\/etc\/ssl\/openldap\/certs\/cacert.pem<\/code><\/pre>\n\n\n\nRestart OpenLDAP daemon.<\/p>\n\n\n\n
systemctl restart slapd<\/code><\/pre>\n\n\n\nConfirm the status;<\/p>\n\n\n\n
systemctl status slapd<\/code><\/pre>\n\n\n\n\u25cf slapd.service - LSB: OpenLDAP standalone server (Lightweight Directory Access Protocol)\n Loaded: loaded (\/etc\/init.d\/slapd; generated)\n Drop-In: \/usr\/lib\/systemd\/system\/slapd.service.d\n \u2514\u2500slapd-remain-after-exit.conf\n Active: active (running) since Sat 2024-02-24 21:03:40 UTC; 7s ago\n Docs: man:systemd-sysv-generator(8)\n Process: 2984 ExecStart=\/etc\/init.d\/slapd start (code=exited, status=0\/SUCCESS)\n Tasks: 3 (limit: 2238)\n Memory: 3.4M (peak: 4.3M)\n CPU: 22ms\n CGroup: \/system.slice\/slapd.service\n \u2514\u25002994 \/usr\/sbin\/slapd -h \"ldap:\/\/\/ ldapi:\/\/\/\" -g openldap -u openldap -F \/etc\/ldap\/slapd.d\n\nFeb 24 21:03:40 noble systemd[1]: Starting slapd.service - LSB: OpenLDAP standalone server (Lightweight Directory Access Protocol)...\nFeb 24 21:03:40 noble slapd[2984]: * Starting OpenLDAP slapd\nFeb 24 21:03:40 noble slapd[2993]: @(#) $OpenLDAP: slapd 2.6.7+dfsg-1~exp1ubuntu1 (Feb 6 2024 19:46:16) $\n Ubuntu Developers \nFeb 24 21:03:40 noble slapd[2994]: slapd starting\nFeb 24 21:03:40 noble slapd[2984]: ...done.\nFeb 24 21:03:40 noble systemd[1]: Started slapd.service - LSB: OpenLDAP standalone server (Lightweight Directory Access Protocol).\n<\/code><\/pre>\n\n\n\nTo verify OpenLDAP TLS connectivity, run the command below. If connection is fine, you should get the output, anonymous<\/code>.<\/p>\n\n\n\nldapwhoami -H ldap:\/\/ldap.kifarunix.com -x -ZZ<\/code><\/pre>\n\n\n\n