LuaLDAP Reference Manual |
A Lua interface to the OpenLDAP library |
LuaLDAP is a simple interface from Lua to an LDAP client (in fact it is a bind to OpenLDAP client).
LuaLDAP defines one single global variable: a table called lualdap. This table holds the functions used to create an LDAP connection object.
A connection object offers methods to perform any operation on the directory such as comparing values, adding new entries, modifying attributes on existing entries, removing entries, and the most common of all: searching. Entries are represented as Lua tables; attributes are its fields. The attribute values can be strings or tables of strings (used to represent multiple values).
LuaLDAP is free software and uses the same license as Lua 5.0.
Many LDAP operations manage sets of attributes and values. LuaLDAP provides a uniform way of representing them: using Lua tables. A table of attributes is indexed by the name of the attribute and its value can be a string (be careful: it can also be a "binary string") or a list/table of values indexed by numbers from 1 to n. Some operations have different approaches that will be explained when necessary.
Follows a small example:
entry = { an_attribute = "a value", other_attribute = { "first value of other attribute", "another value of other attribute", }, }Attribute names cannot contain zeroes ('\0')
The distinguished name (DN) is the term used to identify an entry on the directory information tree. It is formed by the relative distinguished name (RDN) of the entry and the distinguished name of its parent. LuaLDAP will always use a string to represent the DN of any entry.
A more precise definition can be found on the LDAP documentation. A list of some of these files can be found in section Related documentation.
LuaLDAP provides a single way to connect to an LDAP server:
A connection object offers methods which implement LDAP operations. Almost all of them need a distinguished name to identify the entry on which the operation will be executed.
These methods execute asynchronous operations and return a function that should be called to obtain the result(s). These functions will return true indicating success of the operation; the only exception is the function compare which can return either true or false (which is the result of the comparison) on a successful operation.
There are two types of errors: API errors, such as wrong parameters, absent connection etc.; and LDAP errors, such as mal-formed DN, unknown attribute etc. API errors will raise a Lua error, while LDAP errors will be reported by the function/method returning nil followed by the error message provided by the OpenLDAP client.
A connection object can be created by calling the Initialization function.
require "lualdap" ld = assert (lualdap.open_simple ("ldap.server", "mydn=manoeljoaquim,ou=people,dc=ldap,dc=world", "mysecurepassword")) for dn, attribs in ld:search { base = "ou=people,dc=ldap,dc=world" } do io.write (string.format ("\t[%s]\n", dn)) for name, values in pairs (attribs) do io.write ("["..name.."] : ") if type (values) == "string" then io.write (values) elseif type (values) == "table" then local n = table.getn(values) for i = 1, (n-1) do io.write (values[i]..",") end io.write (values[n]) end io.write ("\n") end end ld:add ("mydn=newuser,ou=people,dc=ldap,dc=world", { objectClass = { "", "", }, mydn = "newuser", abc = "qwerty", tel = { "123456758", "98765432", }, givenName = "New User", })() ld:modify {"mydn=newuser,ou=people,dc=ldp,dc=world", { '=', givenName = "New", cn = "New", sn = "User", }, { '+', o = { "University", "College", }, mail = "newuser@university.edu", }, { '-', abc = true, tel = "123456758", }, { '+', tel = "13579113", }, }() ld:delete ("mydn=newuser,ou=people,dc=ldp,dc=world")()