Skip to main content

Cómo añadir índices en OpenLDAP

Si encuentras en el log de OpenLDAP entradas de este tipo cuando se realizan búsquedas de los objetos significa que las búsquedas se realizan sobre un atributo que no tiene índice en LDAP:

slapd[14318]: <= mdb_equality_candidates: (associatedDomain) not indexed

Estas búsquedas podrían ser bastante costosas si tenemos un LDAP con muchos datos por lo que si este tipo de búsquedas son muy frecuentes conviene añadir un índice al atributo. Para ello crearemos un fichero add-index.ldif con el siguiente contenido:

dn: olcDatabase={1}mdb,cn=config
changetype: modify
replace: olcDbIndex
olcDbIndex: associatedDomain eq

En este caso creamos un índice de tipo de eq como se puede observar en la última línea ya que las búsquedas son del tipo ‘associatedDomain=dominio’, pero existen otro tipo de índices dependiendo del tipo de búsqueda a realizar sobre el atributo:

pres should be used if use searches of the form ‘objectclass=person’ or ‘attribute=mail’ will be used.
approx MUST be used if use searches of the form ‘sn~=person’ (a ‘sounds-like’ search) will be used.
eq should be used if searches of the form ‘sn=smith’ will be used i.e no wildcards are included (uses the EQUALITY rule only).
sub should be used if use searches of the form ‘sn=sm*’ i.e wildcards are included (uses the SUBSTR rule). This rule may be enhanced by a using subinitial (optimised for ‘sn=*s’), subany (optimised for ‘sn=*n*’) or subfinal (optimised for ‘sn=th*’). One or more sub parameters may be included.

También podríamos combinar varios tipos de índices de esta forma:

olcDbIndex: associatedDomain eq,sub

Una vez generado el fichero solo tenemos que ejecutar el siguiente comando e introducir la clave del usuario admin del esquema:

$ ldapadd -h localhost -D "cn=admin,cn=config" -f add-index.ldif -W
Enter LDAP Password: 
modifying entry "olcDatabase={1}mdb,cn=config"

Si no sabemos la clave del usuario cn=admin,cn=config podemos cambiar la contraseña tal como se explica en este post Cambiar la clave de cn=admin,cn=config o utilizar el siguiente comando que no requiere una autenticación explícita ya que se basa en la autenticación del propio usuario:

ldapmodify -Y EXTERNAL -H ldapi:/// -f add-index.ldif

slapd[20001]: <= bdb_equality_candidates: (mail) not indexed

This message is showed because the ldap server does not have mail attribute indexed.
To create an index for that attribute create a file with this content:

file mail_add_index.ldif

dn: olcDatabase={1}hdb,cn=config
add: olcDbIndex
olcDbIndex: mail eq,sub

Now, simply add the index with the following command:

# ldapmodify -Y EXTERNAL -H ldapi:/// -f mail_add_index_ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "olcDatabase={1}hdb,cn=config"