Asterisk LDAP module: A set of patchs
By Guillaume Bour. Saturday, December 19 2009, 20:55. asterisk C devel linux | Permalink.
The asterisk ldap module allow querying a LDAP database from the Asterisk dialplan. You can for example get user name from a phone extension (source code can be downloaded at http://www.mezzo.net/asterisk/app_ldap.html, since it is not included in asterisk distributions).
I present here 3 patches I wrote to enhance the way it works.
Memory Bugs
In the original module code, memory allocation is done using alloca glibc function. It cause a segfault because memory can't be reallocated with realloc.
The patch replace alloca calls with ast_alloc and ast_free (a malloc wrapper which generate a log if memory allocation fail).
Configuration file factorization
For each LDAP query, you have to define a section in the configuration file. In each section, you have to repeat all the connection parameters (LDAP host, user and password, base search, ldap version, ...).
Here is an example of the original configuration file:
[cidname] host = samplehost user = cn=root,ou=People,o=sampledomain pass = secret base = ou=Addressbooks,o=sampledomain convert = UTF-8, ISO-8859-1 filter = (&(objectClass=person)(|(telephoneNumber=%s)(mobile=%s)(fax=%s))) attribute = displayName [commonname] host = samplehost user = cn=myuser,ou=People,o=sampledomain pass = usersecret base = ou=Addressbooks,o=sampledomain convert = UTF-8, ISO-8859-1 filter = (&(objectClass=person)(|(telephoneNumber=%s)(mobile=%s)(fax=%s))) attribute = cn
This patch add a [general] section where you set... general LDAP parameters which will be used in each following specific sections. Note than you can override a parameter in a specific section if the value change.
See the modified configuration file:
; [general] section contains global values ; these values are superseded if defined in other sections [general] host = samplehost user = cn=root,ou=People,o=sampledomain pass = secret base = ou=Addressbooks,o=sampledomain convert = UTF-8, ISO-8859-1 [cidname] filter = (&(objectClass=person)(|(telephoneNumber=%s)(mobile=%s)(fax=%s))) attribute = displayName [commonname] ; user & pass supersedes these defined in [general] section user = cn=myuser,ou=People,o=sampledomain pass = usersecret filter = (&(objectClass=person)(|(telephoneNumber=%s)(mobile=%s)(fax=%s))) attribute = cn
Speedup
In the original module, a new LDAP connection is initialised at each LDAPget call. This patch initialize the connection a module load/reload and cache it.
Note that if connection is lost, the module automatically try to reconnect to ldap server when executing a query.
A quick speedup test show a 50% speedup increase!
You can download the patchset on my devel page