| 1 |
<HTML> |
| 2 |
<HEAD> |
| 3 |
<TITLE>DBD::LDAP - Perl extension for DBI, providing an SQL/Perl DBI interface |
| 4 |
to LDAP databases. LDAP stands for the "Lightweight Directory Access |
| 5 |
Protocol". For more information, see: http://www.ogre.com/ldap/docs.html</TITLE> |
| 6 |
<LINK REV="made" HREF="mailto:[email protected]"> |
| 7 |
</HEAD> |
| 8 |
|
| 9 |
<BODY> |
| 10 |
<CENTER> |
| 11 |
</CENTER> |
| 12 |
|
| 13 |
<A NAME="__index__"></A> |
| 14 |
<!-- INDEX BEGIN --> |
| 15 |
|
| 16 |
<UL> |
| 17 |
|
| 18 |
<LI><A HREF="#name">NAME</A></LI> |
| 19 |
<LI><A HREF="#author">AUTHOR</A></LI> |
| 20 |
<LI><A HREF="#prerequisites">PREREQUISITES</A></LI> |
| 21 |
<LI><A HREF="#synopsis">SYNOPSIS</A></LI> |
| 22 |
<LI><A HREF="#description">DESCRIPTION</A></LI> |
| 23 |
<LI><A HREF="#installation">INSTALLATION</A></LI> |
| 24 |
<LI><A HREF="#getting started:">GETTING STARTED:</A></LI> |
| 25 |
<LI><A HREF="#inserting, fetching and modifying data">INSERTING, FETCHING AND MODIFYING DATA</A></LI> |
| 26 |
<LI><A HREF="#metadata">METADATA</A></LI> |
| 27 |
<LI><A HREF="#other supporting utilities">OTHER SUPPORTING UTILITIES</A></LI> |
| 28 |
<LI><A HREF="#restrictions">RESTRICTIONS</A></LI> |
| 29 |
<LI><A HREF="#todo">TODO</A></LI> |
| 30 |
<LI><A HREF="#known bugs">KNOWN BUGS</A></LI> |
| 31 |
</UL> |
| 32 |
<!-- INDEX END --> |
| 33 |
|
| 34 |
<HR> |
| 35 |
<P> |
| 36 |
<H1><A NAME="name">NAME</A></H1> |
| 37 |
<PRE> |
| 38 |
DBD::LDAP - Perl extension for DBI, providing an SQL/Perl DBI interface |
| 39 |
to LDAP databases. LDAP stands for the "Lightweight Directory Access |
| 40 |
Protocol". For more information, see: <A HREF="http://www.ogre.com/ldap/docs.html">http://www.ogre.com/ldap/docs.html</A></PRE> |
| 41 |
<P> |
| 42 |
<HR> |
| 43 |
<H1><A NAME="author">AUTHOR</A></H1> |
| 44 |
<PRE> |
| 45 |
This module is Copyright (C) 2000-2004 by</PRE> |
| 46 |
<PRE> |
| 47 |
Jim Turner |
| 48 |
</PRE> |
| 49 |
<PRE> |
| 50 |
|
| 51 |
Email: <A "mailto:[email protected]">[email protected]</A></PRE> |
| 52 |
<PRE> |
| 53 |
All rights reserved Without Prejudice.</PRE> |
| 54 |
<PRE> |
| 55 |
You may distribute this module under the terms of either the GNU General |
| 56 |
Public License or the Artistic License, as specified in the Perl README |
| 57 |
file.</PRE> |
| 58 |
<P> |
| 59 |
<HR> |
| 60 |
<H1><A NAME="prerequisites">PREREQUISITES</A></H1> |
| 61 |
<PRE> |
| 62 |
Convert::ANS1 (required by Net::LDAP) |
| 63 |
Net::LDAP |
| 64 |
DBI |
| 65 |
- an LDAP database to connect to.</PRE> |
| 66 |
<P> |
| 67 |
<HR> |
| 68 |
<H1><A NAME="synopsis">SYNOPSIS</A></H1> |
| 69 |
<PRE> |
| 70 |
use DBI; |
| 71 |
$dbh = DBI->connect("DBI:LDAP:ldapdb",'user','password') #USER LOGIN. |
| 72 |
or die "Cannot connect as user: " . $DBI::errstr;</PRE> |
| 73 |
<PRE> |
| 74 |
$dbh = DBI->connect("DBI:LDAP:ldapdb") #ANONYMOUS LOGIN (Read-only). |
| 75 |
or die "Cannot connect as guest (readonly): " . $DBI::errstr;</PRE> |
| 76 |
<PRE> |
| 77 |
$sth = $dbh->prepare("select * from people where (cn like 'Smith%')") |
| 78 |
or die "Cannot prepare: " . $dbh->errstr(); |
| 79 |
$sth->execute() or die "Cannot execute: " . $sth->errstr(); |
| 80 |
while ((@results) = $sth->fetchrow_array) |
| 81 |
{ |
| 82 |
print "--------------------------------------------------------\n"; |
| 83 |
++$cnt; |
| 84 |
while (@results) |
| 85 |
{ |
| 86 |
print "------>".join('|',split(/\0/, shift(@results)))."\n"; |
| 87 |
} |
| 88 |
} |
| 89 |
$sth->finish(); |
| 90 |
$dbh->disconnect();</PRE> |
| 91 |
<P> |
| 92 |
<HR> |
| 93 |
<H1><A NAME="description">DESCRIPTION</A></H1> |
| 94 |
<P>DBD::LDAP is a DBI extension module adding an SQL database interface to |
| 95 |
standard LDAP databases to Perl's database-independent database interface. |
| 96 |
You will need access to an existing LDAP database or set up your own using |
| 97 |
an LDAP server, ie. ``OpenLDAP'', see (http://www.openldap.org).</P> |
| 98 |
<P>JLdap.pm is included in this module as a separate file, and is required.</P> |
| 99 |
<P>The main advantage of DBD::LDAP is the ability to query LDAP databases via |
| 100 |
standard SQL queries in leu of cryptic LDAP ``filters''. LDAP is optimized for |
| 101 |
quick lookup of existing data, but DBD::LDAP does support entry inserts, |
| 102 |
updates, and deletes with commit/rollback via the standard SQL commands!</P> |
| 103 |
<P>LDAP databases are ``heirarchical'' in structure, whereas other DBD-supported |
| 104 |
databases are ``relational'' and there is no LDAP-equivalent to SQL ``tables'', so |
| 105 |
DBD::LDAP maps a ``table'' interface over the LDAP ``tree'' via a configuration |
| 106 |
file you must set up. Each ``table'' is mapped to a common ``base DN''. For |
| 107 |
example, consider a typical LDAP database of employees within different |
| 108 |
departments within a company. You might have a ``company'' names ``Acme'' and |
| 109 |
the root ``dn'' of ``dc=Acme, dc=com'' (Acme.com). Below the company level, are |
| 110 |
divisions, ie. ``Widgets'', and ``Blivets''. Each division would have an entry |
| 111 |
with a ``dn'' of ``ou=Widgets, dc=Acme, dc=com''. Employees within each division |
| 112 |
could have a ``dn'' of ``cn=John Doe, ou=Widgets, dc=Acme, dc=com''. |
| 113 |
With DBD::LDAP, we could create tables to access these different levels, |
| 114 |
ie. ``top'', which would have a ``DN'' of ``dc=Acme, dc=com'', ``WidgetDivision'' for |
| 115 |
``dc=Acme, dc=com''. Tables can also be constained by additional |
| 116 |
attribute specifications (filters), ie constraining by ``objectclass'', ie. |
| 117 |
``(objectclass=person)''. Then, doing a ``select * from WidgetDivision'' would |
| 118 |
display all ``person''s with a ``dn'' containing ``''ou=Widgets, dc=Acme, dc=com``.</P> |
| 119 |
<P> |
| 120 |
<HR> |
| 121 |
<H1><A NAME="installation">INSTALLATION</A></H1> |
| 122 |
<PRE> |
| 123 |
Installing this module (and the prerequisites from above) is quite |
| 124 |
simple. You just fetch the archive, extract it with</PRE> |
| 125 |
<PRE> |
| 126 |
gzip -cd DBD-LDAP-0.1000.tar.gz | tar xf -</PRE> |
| 127 |
<PRE> |
| 128 |
(this is for Unix users, Windows users would prefer WinZip or something |
| 129 |
similar) and then enter the following:</PRE> |
| 130 |
<PRE> |
| 131 |
cd DBD-LDAP-#.### |
| 132 |
perl Makefile.PL |
| 133 |
make |
| 134 |
make test</PRE> |
| 135 |
<PRE> |
| 136 |
If any tests fail, let me know. Otherwise go on with</PRE> |
| 137 |
<PRE> |
| 138 |
make install</PRE> |
| 139 |
<PRE> |
| 140 |
Note that you almost definitely need root or administrator permissions. |
| 141 |
If you don't have them, read the ExtUtils::MakeMaker man page for |
| 142 |
details on installing in your own directories. </PRE> |
| 143 |
<P> |
| 144 |
<HR> |
| 145 |
<H1><A NAME="getting started:">GETTING STARTED:</A></H1> |
| 146 |
<PRE> |
| 147 |
1) Create a "database", ie. "foo" by creating a text file "foo.ldb". The |
| 148 |
general format of this file is: |
| 149 |
</PRE> |
| 150 |
<PRE> |
| 151 |
|
| 152 |
---------------------------------------------------------- |
| 153 |
hostname[;port][:[root-dn][:[loginrule]]] |
| 154 |
tablename1:[basedn]:[basefilter]:dnattrs:[visableattrs]:[insertattrs]:[ldap_options] |
| 155 |
tablename2:[basedn]:[basefilter]:dnattrs:[visableattrs]:[insertattrs]:[ldap_options] |
| 156 |
... |
| 157 |
----------------------------------------------------------</PRE> |
| 158 |
<PRE> |
| 159 |
<hostname> represents the ldap server host name. |
| 160 |
<port> represents the server's port, default is 389. |
| 161 |
<root-dn> if specified, is appended to the end of each tablename's |
| 162 |
base-dn. |
| 163 |
<loginrule> if specified, converts single word "usernames" to the |
| 164 |
appropriate DN, ie:</PRE> |
| 165 |
<PRE> |
| 166 |
"cn=*,<ROOT>" would convert user name "foo" to "cn=foo, " and |
| 167 |
append the "<root-dn>" onto that. The asterisk is converted to |
| 168 |
the user-name specified in the "connect" method. If not specified, |
| 169 |
the username specified in the "connect" method must be a full DN. |
| 170 |
If the "<root-dn>" is not specified, then the "<loginrule>" would |
| 171 |
need to be a full DN.</PRE> |
| 172 |
<PRE> |
| 173 |
tablename - represents the name to be used in SQL statements for a given |
| 174 |
set of entries which make up a virtual "table". |
| 175 |
basedn - if specified, is appended to the "<root-dn>" to make up the |
| 176 |
common base DN for all entries in this table. If "<root-dn>" is |
| 177 |
not specified, then a full DN must be specified; otherwise, the |
| 178 |
default is the root-dn. |
| 179 |
basefilter - if specified, specifies a filter to be used if no "where"- |
| 180 |
clause is specified in SQL queries. If a "where"-clause is |
| 181 |
specified, the resulting filter is "and"-ed with this one. The |
| 182 |
default is "(objectclass=*)". |
| 183 |
dnattrs - specifies which attributes that values for which are to be |
| 184 |
appended to the left of the basedn to create DNs for new entries |
| 185 |
being inserted into the table. |
| 186 |
visableattrs - if specified, one or more attributes separated by commas |
| 187 |
which will be sought when the SQL statement does not specify |
| 188 |
attributes, ie. "select * from tablename". If not specified, the |
| 189 |
attributes of the first matching entry are returned and used for |
| 190 |
all entries matching a given query. |
| 191 |
insertattrs - if specified, one or more attribute/value combinations to be |
| 192 |
added to any new entry inserted into the table, usually needed for |
| 193 |
objectclass values. The attributes and values usually correspond |
| 194 |
to those specivied in the "<basefilter>". The general format is: |
| 195 |
attr1=value1[|value2...],attr2=value1...,... |
| 196 |
These attributes and values will be joined with any user-specified |
| 197 |
values for these attributes. |
| 198 |
ldap_options - if specified, can be any one or more of the following:</PRE> |
| 199 |
<PRE> |
| 200 |
ldap_sizelimit - Limit the number of entries fetch by a query to this |
| 201 |
number (0 = no limit) - default: 0. |
| 202 |
ldap_timelimit - Limit the search to this number of seconds per query. |
| 203 |
(0 = no limit) - default: 0. |
| 204 |
ldap_scope - specify the "scope" of the search. Values are: "base", |
| 205 |
"one", and "sub", see Net::LDAP docs. Default is "one", |
| 206 |
meaning the set of records one level below the basedn. "base" |
| 207 |
means search only the basedn, and "sub" means the union |
| 208 |
of entries at the "base" level and "one" level below. |
| 209 |
ldap_inseparator - specify the separator character/string to be used |
| 210 |
in queries to separate multiple values being specified for |
| 211 |
a given attribute. Default is "|". |
| 212 |
ldap_outseparator - specify the separator character/string to be used |
| 213 |
in queryies to separate multiple values displayed as a result |
| 214 |
of a query. Default is "|". |
| 215 |
ldap_firstonly - only display the 1st value fetched for each attribute |
| 216 |
per entry. This makes "ldap_outseparator" unnecessary.</PRE> |
| 217 |
<PRE> |
| 218 |
2) write your script to use DBI, ie: |
| 219 |
</PRE> |
| 220 |
<PRE> |
| 221 |
|
| 222 |
#!/usr/bin/perl |
| 223 |
use DBI;</PRE> |
| 224 |
<PRE> |
| 225 |
|
| 226 |
$dbh = DBI->connect('DBD:LDAP:mydb','me','mypassword') || |
| 227 |
die "Could not connect (".$DBI->err.':'.$DBI->errstr.")!"; |
| 228 |
... |
| 229 |
#CREATE A TABLE, INSERT SOME RECORDS, HAVE SOME FUN!</PRE> |
| 230 |
<PRE> |
| 231 |
|
| 232 |
3) get your application working.</PRE> |
| 233 |
<P> |
| 234 |
<HR> |
| 235 |
<H1><A NAME="inserting, fetching and modifying data">INSERTING, FETCHING AND MODIFYING DATA</A></H1> |
| 236 |
<PRE> |
| 237 |
1st, we'll create a database called "ldapdb" with the tables previously |
| 238 |
mentioned in the example in the DESCRIPTION section: |
| 239 |
</PRE> |
| 240 |
<PRE> |
| 241 |
|
| 242 |
----------------- file "ldapdb.ldb" ---------------- |
| 243 |
ldapserver:dc=Acme, dc=com:cn=*,<ROOT> |
| 244 |
top:::dc |
| 245 |
WidgetDivision:ou=Widgets, :&(objectclass=top)(objectclass=person):cn:cn,sn,ou,title,telephonenumber,description,objectclass,dn:objectclass=top|person|organizationalPerson:ldap_outseparator => ":" |
| 246 |
----------------------------------------------------</PRE> |
| 247 |
<PRE> |
| 248 |
The following examples insert some data in a table and fetch it back: |
| 249 |
First all data in the string:</PRE> |
| 250 |
<PRE> |
| 251 |
$dbh->do(<<END_SQL); |
| 252 |
INSERT INTO top (ou, cn, objectclass) |
| 253 |
VALUES ('Widgets', 'WidgetDivision', 'top|organizationalUnit') |
| 254 |
END_SQL</PRE> |
| 255 |
<PRE> |
| 256 |
Next an example using parameters:</PRE> |
| 257 |
<PRE> |
| 258 |
$dbh->do("INSERT INTO WidgetDivision (cn,sn,title,telephonenumber) VALUES (?, ?, ?, ?)", |
| 259 |
'John Doe','DoeJ','Manager','123-1111'); |
| 260 |
END_SQL |
| 261 |
$dbh->commit();</PRE> |
| 262 |
<PRE> |
| 263 |
NOTE: Unlike most other DBD modules which support transactions, changes |
| 264 |
made do NOT show up until the "commit" function is called, unless |
| 265 |
"AutoCommit" is set. This is due to the fact that fetches are done |
| 266 |
from the LDAP server and changes do not take effect there until the |
| 267 |
Net::LDAP "update" function is called, which is called by "commit".</PRE> |
| 268 |
<PRE> |
| 269 |
NOTE: The "dn" field is generated automatically from the base "dn" and the |
| 270 |
dn component fields specified by "dnattrs", If you try to insert a |
| 271 |
value directly into it, it will be ignored. Also, if not specified, |
| 272 |
any attribute/value combinations specified in the "insertattrs" |
| 273 |
option will be added automatically.</PRE> |
| 274 |
<PRE> |
| 275 |
To retrieve data, you can use the following:</PRE> |
| 276 |
<PRE> |
| 277 |
my($query) = "SELECT * FROM WidgetDivision WHERE cn like 'John%' ORDER BY cn"; |
| 278 |
my($sth) = $dbh->prepare($query); |
| 279 |
$sth->execute(); |
| 280 |
while (my $entry = $sth->fetchrow_hashref) { |
| 281 |
print("Found result record: cn = ", $entry->{'cn'}, |
| 282 |
", phone = ", $row->{'telephonenumber'}); |
| 283 |
} |
| 284 |
$sth->finish();</PRE> |
| 285 |
<PRE> |
| 286 |
The SQL "SELECT" statement above (combined with the table information |
| 287 |
in the "ldapdb.ldb" database file would generate and execute the following |
| 288 |
equivalent LDAP Search:</PRE> |
| 289 |
<PRE> |
| 290 |
base => 'ou=Widgets, dc=Acme, dc=com', |
| 291 |
filter => '(&(&(objectclass=top)(objectclass=person))(cn=John*))', |
| 292 |
scope => 'one', |
| 293 |
attrs => 'cn,sn,ou,title,telephonenumber,description,objectclass,dn'</PRE> |
| 294 |
<PRE> |
| 295 |
See the DBI(3) manpage for details on these methods. See the</PRE> |
| 296 |
<PRE> |
| 297 |
Data rows are modified with the UPDATE statement:</PRE> |
| 298 |
<PRE> |
| 299 |
$dbh->do("UPDATE WidgetDivision SET description = 'Outstanding Employee' WHERE cn = 'John Doe'");</PRE> |
| 300 |
<PRE> |
| 301 |
NOTE: You can NOT change the "dn" field directly - direct changes will be |
| 302 |
ignored. You change the "rdn" component of the "dn" field by changing |
| 303 |
the value of the other field(s) which are appended to the base "dn". |
| 304 |
Also, if not specified, any attribute/value combinations specified in |
| 305 |
the "insertattrs" option will be added automatically.</PRE> |
| 306 |
<PRE> |
| 307 |
Likewise you use the DELETE statement for removing entries:</PRE> |
| 308 |
<PRE> |
| 309 |
$dbh->do("DELETE FROM WidgetDivision WHERE description = 'Outstanding Employee'");</PRE> |
| 310 |
<P> |
| 311 |
<HR> |
| 312 |
<H1><A NAME="metadata">METADATA</A></H1> |
| 313 |
<PRE> |
| 314 |
The following attributes are handled by DBI itself and not by DBD::LDAP, |
| 315 |
thus they should all work as expected.</PRE> |
| 316 |
<PRE> |
| 317 |
PrintError |
| 318 |
RaiseError |
| 319 |
Warn</PRE> |
| 320 |
<PRE> |
| 321 |
The following DBI attributes are handled by DBD::LDAP:</PRE> |
| 322 |
<PRE> |
| 323 |
AutoCommit |
| 324 |
Works</PRE> |
| 325 |
<PRE> |
| 326 |
NUM_OF_FIELDS |
| 327 |
Valid after '$sth->execute'</PRE> |
| 328 |
<PRE> |
| 329 |
NUM_OF_PARAMS |
| 330 |
Valid after '$sth->prepare'</PRE> |
| 331 |
<PRE> |
| 332 |
NAME |
| 333 |
Valid after '$sth->execute'; undef for Non-Select statements.</PRE> |
| 334 |
<PRE> |
| 335 |
NULLABLE |
| 336 |
Not really working. Always returns an array ref of one's, as |
| 337 |
DBD::LDAP always allows NULL (handled as an empty string). |
| 338 |
Valid after `$sth->execute'.</PRE> |
| 339 |
<PRE> |
| 340 |
LongReadLen |
| 341 |
Should work</PRE> |
| 342 |
<PRE> |
| 343 |
LongTruncOk |
| 344 |
Should work</PRE> |
| 345 |
<PRE> |
| 346 |
These attributes and methods are not supported:</PRE> |
| 347 |
<PRE> |
| 348 |
bind_param_inout |
| 349 |
CursorName</PRE> |
| 350 |
<PRE> |
| 351 |
In addition to the DBI attributes, you can use the following dbh |
| 352 |
attributes. These attributes are read-only after "connect".</PRE> |
| 353 |
<PRE> |
| 354 |
ldap_dbuser |
| 355 |
Current database user. |
| 356 |
</PRE> |
| 357 |
<PRE> |
| 358 |
|
| 359 |
ldap_HOME |
| 360 |
Environment variable specifying a path to search for LDAP |
| 361 |
databases (*.sdb) files.</PRE> |
| 362 |
<P></P> |
| 363 |
<PRE> |
| 364 |
|
| 365 |
=head1 DRIVER PRIVATE METHODS</PRE> |
| 366 |
<PRE> |
| 367 |
DBI->data_sources() |
| 368 |
The `data_sources' method returns a list of "databases" (.ldb files) |
| 369 |
found in the current directory and, if specified, the path in |
| 370 |
the ldap_HOME environment variable. |
| 371 |
</PRE> |
| 372 |
<PRE> |
| 373 |
|
| 374 |
$dbh->tables() |
| 375 |
This method returns a list of table names specified in the current |
| 376 |
database. |
| 377 |
Example:</PRE> |
| 378 |
<PRE> |
| 379 |
my($dbh) = DBI->connect("DBI:LDAP:mydatabase",'me','mypswd'); |
| 380 |
my(@list) = $dbh->func('tables');</PRE> |
| 381 |
<P> |
| 382 |
<HR> |
| 383 |
<H1><A NAME="other supporting utilities">OTHER SUPPORTING UTILITIES</A></H1> |
| 384 |
<P> |
| 385 |
<HR> |
| 386 |
<H1><A NAME="restrictions">RESTRICTIONS</A></H1> |
| 387 |
<PRE> |
| 388 |
DBD::LDAP currently treats all data as strings and all fields as |
| 389 |
VARCHAR(255).</PRE> |
| 390 |
<PRE> |
| 391 |
Currently, you must define tables manually in the "<database>.ldb" file |
| 392 |
using your favorite text editor. I hope to add support for the SQL |
| 393 |
"Create Table", "Alter Table", and "Drop Table" functions to handle this |
| 394 |
eventually.</PRE> |
| 395 |
<P> |
| 396 |
<HR> |
| 397 |
<H1><A NAME="todo">TODO</A></H1> |
| 398 |
<PRE> |
| 399 |
"Create Table", "Alter Table", and "Drop Table" SQL functions for |
| 400 |
creating, altering, and deleting the tables defined in the |
| 401 |
"<database>.ldb" file.</PRE> |
| 402 |
<PRE> |
| 403 |
Some kind of datatype support, ie. numeric (for sorting), CHAR for padding, |
| 404 |
Long/Blob - for >255 chars per field, etc.</PRE> |
| 405 |
<P> |
| 406 |
<HR> |
| 407 |
<H1><A NAME="known bugs">KNOWN BUGS</A></H1> |
| 408 |
<PRE> |
| 409 |
none - (yet). |
| 410 |
</PRE> |
| 411 |
<PRE> |
| 412 |
|
| 413 |
=head1 SEE ALSO</PRE> |
| 414 |
<PRE> |
| 415 |
Net::LDAP(3), DBI(3), perl(1)</PRE> |
| 416 |
<IMG SRC="http://turnerville.wwol.com/cgi-bin/access_log.pl?DBDLdap"> |
| 417 |
</BODY> |
| 418 |
</HTML> |