Hello ! In this post, I will explain how to add a new schema into OpenLDAP 2.4 and higher. Why ? Because since release 2.4, the structure of the server has a little changed. I’m not an LDAP expert but I’m writing this article because I insulted my OpenLDAP and my Linux several times during one week while I try to add my own schema into OpenLDAP 2.4. I have read a lot of posts but honestly, I never find a good “how-to” which respond to the question in this post’s title.
The configuration in OpenLDAP 2.4 and next versions is now in LDIF format and it follows a pretty logical schema like this :
We can look that schemas must be placed as child of cn=schema,cn=config. Before the release 2.4, all .schema (stored under /etc/ldap/schema/) were included into slapd.conf. Since 2.4, we must “forget” the .schema file. The best way to explain how to add new schema with 2.4 (and higher) release is to start with a real and practical example. I suppose you already have a OpenLDAP server running. For your information, my OpenLDAP runs on a Ubuntu Server 12.04 LTS with a 3.5.0-48 64 bits kernel.
root@ldapserver:~# lsb_release -a && uname -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 12.04.4 LTS Release: 12.04 Codename: precise Linux ldapserver 3.5.0-48-generic #72~precise1-Ubuntu SMP Tue Mar 11 20:09:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
THE PREPARATION
For this example, we can imagine that we want to add a new schema with a custom objectClass for our business application. This new objectClass has the name of nsaEngineer and the schema nsaProject. As a reminder, a schema is a container (package) for one or many objectClass. An objectClass contains references to other objectClass and attributes. Now, the objectClass nsaEngineer has 3 customs mandatory attributes and a optional (description) which is referenced in another base schema.
- mainDoorKey (mandatory)
- safeKey (mandatory)
- nsaID (mandatory)
- description (optional)
Now, let’s go to create this schema (and objectClass and attributes). Note : for more information about LDAP structures, please consult the following link : LDAP structure
CREATION OF CLASSIC .SCHEMA
The .schema format is simple to understand and to write for the beginners. Let’s start by creating a working directory for example /tmp/ldapworkingdir.
root@ldapserver:~# mkdir /tmp/ldapworkingdir
Now, create the file nsaProject.schema which contains the definition of the new and custom attributes and the new objectClass nsaEngineer and save it in /etc/ldap/schema/. I will not explain in detail the content of nsaProject.schema because it is out of the scope for this post.
Convert and add the schema to OpenLDAP
Now it’s time to try to convert our .schema into an LDIF file and add it to OpenLDAP. To begin, create a dummy file into /tmp/ldapworkingdir called ldap.conf for example and write into it the include directive to the nsaProject.schema.
root@ldapserver:~# cd /tmp/ldapworkingdir/ root@ldapserver:/tmp/ldapworkingdir# touch ldap.conf root@ldapserver:/tmp/ldapworkingdir# echo "include /etc/ldap/schema/nsaProject.schema" > ldap.conf
Now try to convert the .schema file with this command
root@ldapserver:/tmp/ldapworkingdir# slaptest -f ldap.conf -F . config file testing succeeded
This will create in place a directory called cn=config and a file cn=config.ldif. Now go to new created sub-directory ./cn=config/cn/schema/ and edit the file cn={0}nsaProject
root@ldapserver:/tmp/ldapworkingdir# cd cn\=config/cn\=schema/
The file must contains the following:
# AUTO-GENERATED FILE - DO NOT EDIT!! Use ldapmodify. # CRC32 50a9d844 dn: cn={0}nsaproject objectClass: olcSchemaConfig cn: {0}nsaproject olcAttributeTypes: {0}( 2.25.896523589646542389.1 NAME 'mainDoorKey' SUP descr iption EQUALITY caseExactMatch SYNTAX 1.3.6.1.4.1.1466.115.121.1.44 SINGLE-VA LUE ) olcAttributeTypes: {1}( 2.25.896523589646542389.2 NAME 'safeKey' SUP descripti on EQUALITY caseExactMatch SYNTAX 1.3.6.1.4.1.1466.115.121.1.44 SINGLE-VALUE ) olcAttributeTypes: {2}( 2.25.896523589646542389.3 NAME 'nsaID' SUP description EQUALITY caseExactMatch SYNTAX 1.3.6.1.4.1.1466.115.121.1.44 SINGLE-VALUE ) olcObjectClasses: {0}( 2.25.896523589646542389.10 NAME 'nsaEngineer' STRUCTURA L MUST ( mainDoorKey $ safeKey $ nsaID ) MAY description ) structuralObjectClass: olcSchemaConfig entryUUID: 51ee4aea-79c9-1033-958d-ef361bf34673 creatorsName: cn=config createTimestamp: 20140527090205Z entryCSN: 20140527090205.718989Z#000000#000#000000 modifiersName: cn=config modifyTimestamp: 20140527090205Z
You must keep only the following things and delete the rest.
- dn: cn={0}nsaproject
- objectClass: olcSchemaConfig
- cn: {0}nsaproject
- olcAttributeTypes:
- olcObjectClasses:
You must modify dn: and cn: (just remove {0} for cn but write the correct dn for the schema ! (dn:cn=nsaproject,cn=schema,cn=config) ). After these modifications, the file is like this:
dn: cn=nsaproject,cn=schema,cn=config objectClass: olcSchemaConfig cn: nsaproject olcAttributeTypes: {0}( 2.25.896523589646542389.1 NAME 'mainDoorKey' SUP descr iption EQUALITY caseExactMatch SYNTAX 1.3.6.1.4.1.1466.115.121.1.44 SINGLE-VA LUE ) olcAttributeTypes: {1}( 2.25.896523589646542389.2 NAME 'safeKey' SUP descripti on EQUALITY caseExactMatch SYNTAX 1.3.6.1.4.1.1466.115.121.1.44 SINGLE-VALUE ) olcAttributeTypes: {2}( 2.25.896523589646542389.3 NAME 'nsaID' SUP description EQUALITY caseExactMatch SYNTAX 1.3.6.1.4.1.1466.115.121.1.44 SINGLE-VALUE ) olcObjectClasses: {0}( 2.25.896523589646542389.10 NAME 'nsaEngineer' STRUCTURA L MUST ( mainDoorKey $ safeKey $ nsaID ) MAY description )
That’s it ! Now we are ready to add this schema to the OpenLDAP server using this command
root@ldapserver:/tmp/ldapworkingdir/cn=config/cn=schema# ldapadd -Q -Y EXTERNAL -H ldapi:/// -W -f /tmp/ldapworkingdir/cn\=config/cn\=schema/cn\=\{0\}nsaproject.ldif Enter LDAP Password: adding new entry "cn=nsaproject,cn=schema,cn=config"
Now you can verify if the new schema is correctly added using a ldapsearch command like this :
root@ldapserver: ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:/// -b cn=config nsa* dn: cn=config dn: cn=module{0},cn=config dn: cn=schema,cn=config dn: cn={0}core,cn=schema,cn=config dn: cn={1}cosine,cn=schema,cn=config dn: cn={2}nis,cn=schema,cn=config dn: cn={3}inetorgperson,cn=schema,cn=config dn: cn={4}nsaproject,cn=schema,cn=config #yeah :-) dn: olcBackend={0}hdb,cn=config dn: olcDatabase={-1}frontend,cn=config dn: olcDatabase={0}config,cn=config dn: olcDatabase={1}hdb,cn=conf