LLI Wiki — My Digital Knowledge Base

OpenLDAP and ldapsearch: Oracle Net directory on Ubuntu, backup and logging

ldapsearch commands, OpenLDAP installation on Ubuntu, Oracle Net schema, backup/restore, admin password and logging.

OpenLDAP can serve as a directory for Oracle Net name resolution (replacing tnsnames.ora) and ldapsearch is the main tool to query it, or an Active Directory. This article collects the essential commands: concepts, queries, installation on Ubuntu, Oracle schema loading, backup and restore, logging and admin password change. Hostnames, domains and credentials are generic.

Concepts

Every unit in LDAP is an entry (object). Common DN attributes (see RFC 2253):

Attribute Meaning
DN Distinguished name of the entry
CN commonName
L localityName
ST stateOrProvinceName
O organizationName
OU organizationalUnitName
C countryName
STREET streetAddress
DC domainComponent
UID userid

The DIT (Directory Information Tree) is navigated either with a full DN to reach one entry, or with a base DN plus a filter (attribute=value) to find entries below it.

Example LDAP tree with domain, organizational units and entries

LDAP information model: DIT tree, entries and attributes

DN example: root entry, organizational unit and leaf entries

Query with ldapsearch

Active Directory, simple bind, base DN and filter:

ldapsearch -x -h ldap.contoso.com -D "user@example.com" -W -b "OU=USERS,DC=contoso,DC=com" "CN=john*" | less
ldapsearch -x -h ldap.contoso.com -D "user@example.com" -W -b "OU=USERS,DC=contoso,DC=com" '(uid=testuser1)'
ldapsearch -x -h ldap.contoso.com -D "user@example.com" -W -b "OU=USERS,DC=contoso,DC=com" -s sub "(cn=*)" cn mail sn
ldapsearch -x -h ldap.contoso.com -D "user@example.com" -W -b "OU=USERS,DC=contoso,DC=com" "(mail=*gue*)" cn mail sn

Count users and members of a group:

ldapsearch -x -h ldap.contoso.com -b "dc=contoso,dc=com" -D "user@example.com" -W -s sub '(&(objectCategory=person)(objectClass=user))' | grep "cn:" | wc -l
ldapsearch -x -h ldap.contoso.com -b "dc=contoso,dc=com" -D "user@example.com" -W -s sub '(&(objectCategory=person)(objectClass=user)(memberOf=CN=app_group,OU=GROUP,DC=contoso,DC=com))' | grep "cn:" | wc -l

Discover naming contexts and read the root:

ldapsearch -x -b '' -s base '(objectclass=*)' namingContexts
ldapsearch -x -b 'dc=example,dc=com' -s base '(objectclass=*)'

Oracle Net service entries (orclNetService):

ldapsearch -x -H ldap://ldapserver01 -b "cn=OracleContext,dc=example,dc=com"
ldapsearch -x -H ldap://ldapserver01 -b "dc=example,dc=com" -s sub "(&(objectclass=orclNetService)(cn=db-test*))"
ldapsearch -x -H ldap://ldapserver01 -b "dc=example,dc=com" -s sub "objectclass=*"

Common errors: error 32 means the referenced object does not exist (wrong DN); error 49 is a generic login failure and the server adds its own sub-codes (an incorrect admin DN is the usual cause).

Client packages: sudo apt-get install ldap-utils (Debian/Ubuntu), yum install openldap-clients (RHEL/CentOS). On Windows, wmic useraccount get name,sid lists local accounts and SIDs.

Install OpenLDAP on Ubuntu

Set the hostname and name resolution:

sudo hostnamectl set-hostname ldapserver01.example.com
sudo hostnamectl
sudo vim /etc/hosts
127.0.0.1 localhost
192.0.2.11 ldapserver02
192.0.2.10 ldapserver01.example.com

Install the packages (the configuration wizard asks for the domain and the admin password; run dpkg-reconfigure slapd to relaunch it):

sudo apt update
sudo apt -y install slapd ldap-utils
sudo slapcat

Base DN, users and groups

basedn.ldif:

dn: ou=people,dc=example,dc=com
objectClass: organizationalUnit
ou: people

dn: ou=groups,dc=example,dc=com
objectClass: organizationalUnit
ou: groups
ldapadd -x -D cn=admin,dc=example,dc=com -W -f basedn.ldif

Generate a password hash with sudo slappasswd, then create ldapusers.ldif:

dn: uid=app_user,ou=people,dc=example,dc=com
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
cn: app_user
sn: user
userPassword: {SSHA}<HASH>
loginShell: /bin/bash
uidNumber: 2000
gidNumber: 2000
homeDirectory: /home/app_user
ldapadd -x -D cn=admin,dc=example,dc=com -W -f ldapusers.ldif

Oracle Net schema

To load schemas, cn=config needs a root DN and password. Add these lines to /etc/ldap/slapd.d/cn=config/olcDatabase={0}config.ldif, then restart slapd:

olcRootDN: cn=admin,cn=config
olcRootPW: <PASSWORD>
sudo systemctl restart slapd
sudo ldapadd -x -W -f alias.ldif -D "cn=admin,cn=config"
sudo ldapadd -x -W -f oidbase.ldif -D "cn=admin,cn=config"
sudo ldapadd -x -W -f oidnet.ldif -D "cn=admin,cn=config"
sudo ldapadd -x -W -f oidrdbms.ldif -D "cn=admin,cn=config"

The LDIF schema files are derived from the Oracle Net LDAP schema (Oracle documentation). Old Oracle clients may need LDAP v2 binds; enable them with olcAllowsbind_v2.ldif:

dn: cn=config
add: olcAllows
olcAllows: bind_v2
ldapmodify -x -W -D "cn=admin,cn=config" -f olcAllowsbind_v2.ldif

To remove it, use delete: olcAllows instead of add: olcAllows in a second LDIF and apply it the same way.

Read the configuration

As root, slapd does not need to be running:

slapcat -b cn=config
slapcat -b cn=config -a "(|(cn=config)(olcDatabase={1}hdb))"

Or through the local socket:

ldapsearch -Y EXTERNAL -H ldapi:/// -b cn=config
ldapsearch -Y EXTERNAL -H ldapi:/// -b cn=config "(|(cn=config)(olcDatabase={1}hdb))"
ldapsearch -D "cn=admin,cn=config" -W -b cn=config

Backup and restore

Backup the tree and the configuration:

sudo slapcat -b dc=example,dc=com -l backup.ldif
sudo tar -cvf ldap_conf.tar /etc/ldap

Restore:

sudo systemctl stop slapd
slapadd -c -b dc=example,dc=com -F /etc/ldap/slapd.d -l backup.ldif
sudo tar -xvf ldap_conf.tar -C /
sudo systemctl start slapd

-c continues on error (for example an entry already present), -F gives the configuration directory and -l the LDIF file.

Change the admin password

Generate a hash (the module path depends on the distribution: /usr/lib/ldap on Debian, /usr/lib64/openldap on CentOS 7 and openSUSE, /usr/local/libexec/openldap for a source build):

slappasswd -h '{SSHA}' -o module-load=pw-sha2.la -o module-path=/usr/lib/ldap

Then apply it to the config database and to the data database (adminPassword_config.ldif and adminPassword.ldif):

dn: olcDatabase={0}config,cn=config
changetype: modify
replace: olcRootPW
olcRootPW: {SSHA}<HASH>
dn: olcDatabase={1}mdb,cn=config
changetype: modify
replace: olcRootPW
olcRootPW: {SSHA}<HASH>
sudo ldapmodify -x -W -D "cn=admin,cn=config" -f adminPassword_config.ldif
sudo ldapmodify -x -W -D "cn=admin,cn=config" -f adminPassword.ldif

Enable logging

Check the current level:

sudo ldapsearch -x -W -D "cn=admin,cn=config" -b cn=config "(objectClass=olcGlobal)" olcLogLevel -LLL

slapdlog.ldif (use olcLogLevel: none to disable):

dn: cn=config
changeType: modify
replace: olcLogLevel
olcLogLevel: stats
sudo ldapmodify -x -W -D "cn=admin,cn=config" -f slapdlog.ldif

Send local4 to a dedicated file with rsyslog (/etc/rsyslog.d/10-slapd.conf):

$template slapdtmpl,"[%$DAY%-%$MONTH%-%$YEAR% %timegenerated:12:19:date-rfc3339%] %app-name% %syslogseverity-text% %msg%\n"
local4.*    /var/log/slapd.log;slapdtmpl
sudo systemctl restart rsyslog
sudo less /var/log/slapd.log

Add /var/log/slapd.log to the file list of /etc/logrotate.d/rsyslog to rotate it. To trace the traffic itself: tcpdump -i <interface> port 389.

Notes

Mastodon LLIMastodon SocialFosstodon