| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
| 2 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 3 |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 4 |
|
| 5 |
<head> |
| 6 |
<title>LuaLDAP: A Lua interface to an LDAP library</title> |
| 7 |
<link rel="stylesheet" href="http://www.keplerproject.org/doc.css" type="text/css"/> |
| 8 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> |
| 9 |
</head> |
| 10 |
|
| 11 |
<body> |
| 12 |
|
| 13 |
<div id="container"> |
| 14 |
|
| 15 |
<div id="product"> |
| 16 |
<div id="product_logo"><a href="http://www.keplerproject.org"> |
| 17 |
<img alt="LuaLDAP logo" width="128" height="128" src="lualdap.png"/> |
| 18 |
</a></div> |
| 19 |
<div id="product_name"><big><strong>LuaLDAP</strong></big></div> |
| 20 |
<div id="product_description">A Lua interface to an LDAP library</div> |
| 21 |
</div> <!-- id="product" --> |
| 22 |
|
| 23 |
<div id="main"> |
| 24 |
|
| 25 |
<div id="navigation"> |
| 26 |
<h1>LuaLDAP</h1> |
| 27 |
<ul> |
| 28 |
<li><a href="index.html">Home</a> |
| 29 |
<ul> |
| 30 |
<li><a href="index.html#over">Overview</a></li> |
| 31 |
<li><a href="index.html#status">Status</a></li> |
| 32 |
<li><a href="index.html#download">Download</a></li> |
| 33 |
<li><a href="index.html#history">History</a></li> |
| 34 |
<li><a href="index.html#credits">Credits</a></li> |
| 35 |
<li><a href="index.html#contact">Contact us</a></li> |
| 36 |
</ul> |
| 37 |
</li> |
| 38 |
<li><strong>Manual</strong> |
| 39 |
<ul> |
| 40 |
<li><a href="manual.html#introduction">Introduction</a></li> |
| 41 |
<li><a href="manual.html#building">Building</a></li> |
| 42 |
<li><a href="manual.html#installation">Installation</a></li> |
| 43 |
<li><a href="manual.html#attributes">Attributes</a></li> |
| 44 |
<li><a href="manual.html#dn">DN</a></li> |
| 45 |
<li><a href="manual.html#initialization">Initialization</a></li> |
| 46 |
<li><a href="manual.html#connection">Connection</a></li> |
| 47 |
<li><a href="manual.html#examples">Examples</a></li> |
| 48 |
</ul> |
| 49 |
</li> |
| 50 |
<li><a href="http://luaforge.net/projects/lualdap/">Project</a> |
| 51 |
<ul> |
| 52 |
<li><a href="http://luaforge.net/tracker/?group_id=14">Bug Tracker</a></li> |
| 53 |
<li><a href="http://luaforge.net/scm/?group_id=14">CVS</a></li> |
| 54 |
</ul> |
| 55 |
</li> |
| 56 |
<li><a href="license.html">License</a></li> |
| 57 |
</ul> |
| 58 |
</div> <!-- id="navigation" --> |
| 59 |
|
| 60 |
<div id="content"> |
| 61 |
|
| 62 |
<h2><a name="introduction"></a>Introduction</h2> |
| 63 |
|
| 64 |
<p>LuaLDAP is a simple interface from Lua to an LDAP client, in |
| 65 |
fact it is a bind to <a href="http://www.openldap.org">OpenLDAP</a> |
| 66 |
client or <a href="http://msdn2.microsoft.com/en-us/library/aa772161.aspx">ADSI</a> client</p> |
| 67 |
|
| 68 |
<p>LuaLDAP defines one single global variable: a table called |
| 69 |
<code>lualdap</code>. This table holds the functions used to create an |
| 70 |
LDAP connection object.</p> |
| 71 |
|
| 72 |
<p>A connection object offers methods to perform any operation on |
| 73 |
the directory such as comparing values, adding new entries, |
| 74 |
modifying attributes on existing entries, removing entries, and the |
| 75 |
most common of all: searching. Entries are represented as Lua |
| 76 |
tables; attributes are its fields. The attribute values can be |
| 77 |
strings or tables of strings (used to represent multiple |
| 78 |
values).</p> |
| 79 |
|
| 80 |
<p>LuaLDAP is a bind to the |
| 81 |
<a href="http://www.openldap.org">OpenLDAP</a> library or to <a href="http://msdn2.microsoft.com/en-us/library/aa772161.aspx">ADSI</a> library and it |
| 82 |
depends on a previous installation of any of this libraries. You can |
| 83 |
download OpenLDAP from the |
| 84 |
<a href="http://www.openldap.org/software/download">OpenLDAP download</a> |
| 85 |
page. ADSI is included on Windows 2000 or newer. Windows NT/Me/98/95 |
| 86 |
requires an <a href="http://msdn2.microsoft.com/en-us/library/aa772161.aspx">Active Directory Client Extension</a></p> |
| 87 |
<h2><a name="building"></a>Building</h2> |
| 88 |
|
| 89 |
<p> |
| 90 |
LuaLDAP could be built to Lua 5.0 or to Lua 5.1. |
| 91 |
In both cases, |
| 92 |
the language library and headers files for the target version |
| 93 |
must be installed properly. |
| 94 |
</p> |
| 95 |
<p> |
| 96 |
LuaLDAP offers a Makefile and a separate configuration file, |
| 97 |
<code>config</code>, |
| 98 |
which should be edited to suit your installation before runnig |
| 99 |
<code>make</code>. |
| 100 |
The file has some definitions like paths to the external libraries, |
| 101 |
compiler options and the like. |
| 102 |
One important definition is the version of Lua language, |
| 103 |
which is not obtained from the installed software. |
| 104 |
</p> |
| 105 |
|
| 106 |
<h2><a name="installation"></a>Installation</h2> |
| 107 |
|
| 108 |
<p>The LuaLDAP compiled binary should be copied to a directory in your |
| 109 |
<a href="http://www.lua.org/manual/5.1/manual.html#pdf-package.cpath">C path</a>. |
| 110 |
Lua 5.0 users should install <a href="http://www.keplerproject.org/compat">Compat-5.1</a> also.</p> |
| 111 |
|
| 112 |
<p>Windows users of ADSI can use the binary versions of LuaLDAP |
| 113 |
(<code>lualdap.dll</code>) available at |
| 114 |
<a href="http://luaforge.net/projects/lualdap/files">LuaForge</a>.</p> |
| 115 |
|
| 116 |
<h2><a name="attributes"></a>Representing attributes</h2> |
| 117 |
|
| 118 |
<p>Many LDAP operations manage sets of attributes and values. |
| 119 |
LuaLDAP provides a uniform way of representing them by using Lua |
| 120 |
tables. The table attributes can be Lua string, a binary string (a string of |
| 121 |
bits), or table of <em>n</em> values indexed from 1 to <em>n</em>. |
| 122 |
Some operations have different approaches that will be explained as necessary.</p> |
| 123 |
|
| 124 |
<p>Here is a simple example:</p> |
| 125 |
|
| 126 |
<pre class="example"> |
| 127 |
entry = { |
| 128 |
an_attribute = "a value", |
| 129 |
other_attribute = { |
| 130 |
"first value of other attribute", |
| 131 |
"another value of other attribute", |
| 132 |
}, |
| 133 |
} |
| 134 |
</pre> |
| 135 |
|
| 136 |
Attribute names cannot contain the <code>'\0'</code> character. |
| 137 |
|
| 138 |
<h2><a name="dn"></a>Distinguished names</h2> |
| 139 |
|
| 140 |
<p>The distinguished name (DN) is the term used to identify an |
| 141 |
entry on the directory information tree. It is formed by the |
| 142 |
relative distinguished name (RDN) of the entry and the |
| 143 |
distinguished name of its parent. LuaLDAP will always use a string |
| 144 |
to represent the DN of any entry.</p> |
| 145 |
|
| 146 |
<p>A more precise definition can be found on the LDAP |
| 147 |
documentation. A list of some of these files can be found in |
| 148 |
<a href="#related_docs">Related documentation</a> section.</p> |
| 149 |
|
| 150 |
<h2><a name="initialization"></a>Initialization functions</h2> |
| 151 |
|
| 152 |
<p>LuaLDAP provides a single way to connect to an LDAP server:</p> |
| 153 |
|
| 154 |
<dl> |
| 155 |
<dt><strong><code>lualdap.open_simple (hostname, who, password, |
| 156 |
usetls)</code></strong></dt> |
| 157 |
<dd>Initializes a session with an LDAP server. This function requires a |
| 158 |
hostname, accordingly to the <a href="#related_docs">C LDAP API</a> |
| 159 |
definition (<em>"hostname contains a space-separated list of |
| 160 |
hostnames or dotted strings representing the IP address of hosts |
| 161 |
running an LDAP server to connect to. Each hostname in the list MAY |
| 162 |
include a port number which is separated from the host itself with |
| 163 |
a colon (:) character."</em>). The argument <code>who</code> should be |
| 164 |
the <a href="#dn">distinguished name</a> of the entry that has the |
| 165 |
password to be checked against the third argument, |
| 166 |
<code>password</code>. The optional argument <code>usetls</code> is a |
| 167 |
Boolean flag indicating if Transport Layer Security (TLS) should be |
| 168 |
used.<br/> |
| 169 |
Returns a connection object if the operation was successful. In case of |
| 170 |
error it returns <code>nil</code> followed by an error string.</dd> |
| 171 |
</dl> |
| 172 |
|
| 173 |
<h2><a name="connection"></a>Connection objects</h2> |
| 174 |
|
| 175 |
<p>A connection object offers methods which implement LDAP |
| 176 |
operations. Almost all of them need a |
| 177 |
<a href="#dn">distinguished name</a> to identify the entry on which the |
| 178 |
operation will be executed.</p> |
| 179 |
|
| 180 |
<p>These methods execute asynchronous operations and return a |
| 181 |
function that should be called to obtain the results. The called |
| 182 |
functions will return <code>true</code> indicating the success of the |
| 183 |
operation. The only exception is the <code>compare</code> function |
| 184 |
which can return either <code>true</code> or <code>false</code> |
| 185 |
(as the result of the comparison) on a successful operation.</p> |
| 186 |
|
| 187 |
<p>There are two types of errors: <em>API errors</em>, such as |
| 188 |
wrong parameters, absent connection etc.; and <em>LDAP errors</em>, |
| 189 |
such as malformed DN, unknown attribute etc. API errors will raise |
| 190 |
a Lua error, while LDAP errors will be reported by the |
| 191 |
function/method returning <code>nil</code> plus the error |
| 192 |
message provided by the OpenLDAP client.</p> |
| 193 |
|
| 194 |
<p>A connection object can be created by calling the <a href= |
| 195 |
"#initialization">Initialization function</a>.</p> |
| 196 |
|
| 197 |
<h4>Methods</h4> |
| 198 |
|
| 199 |
<dl> |
| 200 |
<dt><strong><code>conn:add (distinguished_name, |
| 201 |
table_of_attributes)</code></strong></dt> |
| 202 |
<dd>Adds a new entry to the directory with the given attributes and |
| 203 |
values. <a name="conn_close"></a></dd> |
| 204 |
|
| 205 |
<dt><strong><code>conn:close()</code></strong></dt> |
| 206 |
<dd>Closes the connection <code>conn</code>.</dd> |
| 207 |
|
| 208 |
<dt><strong><code>conn:compare (distinguished_name, attribute, |
| 209 |
value)</code></strong></dt> |
| 210 |
<dd>Compares a value to an entry.</dd> |
| 211 |
|
| 212 |
<dt><strong><code>conn:delete (distinguished_name)</code></strong></dt> |
| 213 |
<dd>Deletes an entry from the directory.</dd> |
| 214 |
|
| 215 |
<dt><strong><code>conn:modify (distinguished_name, |
| 216 |
table_of_operations*)</code></strong></dt> |
| 217 |
<dd>Changes the values of attributes in the given entry. The tables of |
| 218 |
operations are <a href="#attributes">tables of attributes</a> |
| 219 |
with the value on index <code>1</code> indicating the operation to be |
| 220 |
performed. The valid operations are: |
| 221 |
<ul> |
| 222 |
<li><strong><code>'+'</code></strong> to add the values to the attributes</li> |
| 223 |
<li><strong><code>'-'</code></strong> to delete the values of the attributes</li> |
| 224 |
<li><strong><code>'='</code></strong> to replace the values of the attributes</li> |
| 225 |
</ul> |
| 226 |
Any number of tables of operations will be used in a single LDAP modify |
| 227 |
operation.</dd> |
| 228 |
|
| 229 |
<dt><strong><code>conn:rename (distinguished_name, new_relative_dn, |
| 230 |
new_parent)</code></strong></dt> |
| 231 |
<dd>Changes an entry name (i.e. change its <a href="#dn">distinguished name</a>).</dd> |
| 232 |
|
| 233 |
<dt><strong><code>conn:search (table_of_search_parameters)</code></strong></dt> |
| 234 |
<dd>Performs a search operation on the directory. The parameters are |
| 235 |
described below:<br/><br/> |
| 236 |
<dl> |
| 237 |
<dt><strong><code>attrs</code></strong></dt> |
| 238 |
<dd>a string or a list of attribute names to |
| 239 |
be retrieved (default is to retrieve all attributes).</dd> |
| 240 |
|
| 241 |
<dt><strong><code>attrsonly</code></strong></dt> |
| 242 |
<dd>a Boolean value that must be either |
| 243 |
<em>false</em> (default) if both attribute names and values are to be |
| 244 |
retrieved, or <em>true</em> if only names are wanted.</dd> |
| 245 |
|
| 246 |
<dt><strong><code>base</code></strong></dt> |
| 247 |
<dd>The <a href="#dn">distinguished name</a> |
| 248 |
of the entry at which to start the search.</dd> |
| 249 |
|
| 250 |
<dt><strong><code>filter</code></strong></dt> |
| 251 |
<dd>A string representing the search filter |
| 252 |
as described in <a href="http://www.ietf.org/rfc/rfc2254.txt">The |
| 253 |
String Representation of LDAP Search Filters (RFC 2254)</a>.</dd> |
| 254 |
|
| 255 |
<dt><strong><code>scope</code></strong></dt> |
| 256 |
<dd>A string indicating the scope of the |
| 257 |
search. The valid strings are: "base", "onelevel" and "subtree". |
| 258 |
The empty string ("") and <code>nil</code> will be treated as the |
| 259 |
default scope.</dd> |
| 260 |
|
| 261 |
<dt><strong><code>sizelimit</code></strong></dt> |
| 262 |
<dd>The maximum number of entries to |
| 263 |
return (default is no limit).</dd> |
| 264 |
|
| 265 |
<dt><strong><code>timeout</code></strong></dt> |
| 266 |
<dd>The timeout in seconds (default is no |
| 267 |
timeout). The precision is microseconds.</dd> |
| 268 |
</dl> |
| 269 |
<br/> |
| 270 |
The search method will return a <em>search iterator</em> which is a |
| 271 |
function that requires no arguments. The search iterator is used to |
| 272 |
get the search result and will return a string representing the <a |
| 273 |
href="#dn">distinguished name</a> and a <a href="#attributes">table |
| 274 |
of attributes</a> as returned by the search request.</dd> |
| 275 |
</dl> |
| 276 |
|
| 277 |
<h2><a name="examples"></a>Example</h2> |
| 278 |
|
| 279 |
<p>here is a some sample code that demonstrate the basic use of the library.</p> |
| 280 |
|
| 281 |
<pre class="example"> |
| 282 |
require "lualdap" |
| 283 |
|
| 284 |
ld = assert (lualdap.open_simple ("ldap.server", |
| 285 |
"mydn=manoeljoaquim,ou=people,dc=ldap,dc=world", |
| 286 |
"mysecurepassword")) |
| 287 |
|
| 288 |
for dn, attribs in ld:search { base = "ou=people,dc=ldap,dc=world" } do |
| 289 |
io.write (string.format ("\t[%s]\n", dn)) |
| 290 |
for name, values in pairs (attribs) do |
| 291 |
io.write ("["..name.."] : ") |
| 292 |
if type (values) == "string" then |
| 293 |
io.write (values) |
| 294 |
elseif type (values) == "table" then |
| 295 |
local n = table.getn(values) |
| 296 |
for i = 1, (n-1) do |
| 297 |
io.write (values[i]..",") |
| 298 |
end |
| 299 |
io.write (values[n]) |
| 300 |
end |
| 301 |
io.write ("\n") |
| 302 |
end |
| 303 |
end |
| 304 |
|
| 305 |
ld:add ("mydn=newuser,ou=people,dc=ldap,dc=world", { |
| 306 |
objectClass = { "", "", }, |
| 307 |
mydn = "newuser", |
| 308 |
abc = "qwerty", |
| 309 |
tel = { "123456758", "98765432", }, |
| 310 |
givenName = "New User", |
| 311 |
})() |
| 312 |
|
| 313 |
ld:modify {"mydn=newuser,ou=people,dc=ldp,dc=world", |
| 314 |
{ '=', givenName = "New", cn = "New", sn = "User", }, |
| 315 |
{ '+', o = { "University", "College", }, |
| 316 |
mail = "[email protected]", }, |
| 317 |
{ '-', abc = true, tel = "123456758", }, |
| 318 |
{ '+', tel = "13579113", }, |
| 319 |
}() |
| 320 |
|
| 321 |
ld:delete ("mydn=newuser,ou=people,dc=ldp,dc=world")() |
| 322 |
|
| 323 |
</pre> |
| 324 |
|
| 325 |
<h2><a name="related_docs"></a>Related documentation</h2> |
| 326 |
|
| 327 |
<ul> |
| 328 |
<li><a href="http://www.ietf.org/rfc/rfc2251.txt">Lightweight |
| 329 |
Directory Access Protocol (v3)</a></li> |
| 330 |
<li><a href="http://www.ietf.org/rfc/rfc3377.txt">LDAPv3 Technical |
| 331 |
Specification</a></li> |
| 332 |
<li><a href="http://www.ietf.org/rfc/rfc2254.txt">The String |
| 333 |
Representation of LDAP Search Filters (RFC 2254)</a></li> |
| 334 |
<li><a href= |
| 335 |
"http://www.ietf.org/proceedings/01aug/I-D/draft-ietf-ldapext-ldap-c-api-05.txt"> |
| 336 |
The C LDAP Application Program Interface</a></li> |
| 337 |
</ul> |
| 338 |
|
| 339 |
</div> <!-- id="content" --> |
| 340 |
|
| 341 |
</div> <!-- id="main" --> |
| 342 |
|
| 343 |
<div id="about"> |
| 344 |
<p><a href="http://validator.w3.org/check?uri=referer"> |
| 345 |
<img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" /></a></p> |
| 346 |
<p><small>$Id: manual.html,v 1.34 2007-12-14 16:46:15 carregal Exp $</small></p> |
| 347 |
</div> <!-- id="about" --> |
| 348 |
|
| 349 |
</div> <!-- id="container" --> |
| 350 |
</body> |
| 351 |
</html> |
| 352 |
|