Debian Codegraph @

DBDLdap.htm

raw
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 &quot;Lightweight Directory Access
40 Protocol&quot;. 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-&gt;connect(&quot;DBI:LDAP:ldapdb&quot;,'user','password') #USER LOGIN.
72 or die &quot;Cannot connect as user: &quot; . $DBI::errstr;</PRE>
73 <PRE>
74 $dbh = DBI-&gt;connect(&quot;DBI:LDAP:ldapdb&quot;) #ANONYMOUS LOGIN (Read-only).
75 or die &quot;Cannot connect as guest (readonly): &quot; . $DBI::errstr;</PRE>
76 <PRE>
77 $sth = $dbh-&gt;prepare(&quot;select * from people where (cn like 'Smith%')&quot;)
78 or die &quot;Cannot prepare: &quot; . $dbh-&gt;errstr();
79 $sth-&gt;execute() or die &quot;Cannot execute: &quot; . $sth-&gt;errstr();
80 while ((@results) = $sth-&gt;fetchrow_array)
81 {
82 print &quot;--------------------------------------------------------\n&quot;;
83 ++$cnt;
84 while (@results)
85 {
86 print &quot;------&gt;&quot;.join('|',split(/\0/, shift(@results))).&quot;\n&quot;;
87 }
88 }
89 $sth-&gt;finish();
90 $dbh-&gt;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 &quot;database&quot;, ie. &quot;foo&quot; by creating a text file &quot;foo.ldb&quot;. 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 &lt;hostname&gt; represents the ldap server host name.
160 &lt;port&gt; represents the server's port, default is 389.
161 &lt;root-dn&gt; if specified, is appended to the end of each tablename's
162 base-dn.
163 &lt;loginrule&gt; if specified, converts single word &quot;usernames&quot; to the
164 appropriate DN, ie:</PRE>
165 <PRE>
166 &quot;cn=*,&lt;ROOT&gt;&quot; would convert user name &quot;foo&quot; to &quot;cn=foo, &quot; and
167 append the &quot;&lt;root-dn&gt;&quot; onto that. The asterisk is converted to
168 the user-name specified in the &quot;connect&quot; method. If not specified,
169 the username specified in the &quot;connect&quot; method must be a full DN.
170 If the &quot;&lt;root-dn&gt;&quot; is not specified, then the &quot;&lt;loginrule&gt;&quot; 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 &quot;table&quot;.
175 basedn - if specified, is appended to the &quot;&lt;root-dn&gt;&quot; to make up the
176 common base DN for all entries in this table. If &quot;&lt;root-dn&gt;&quot; 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 &quot;where&quot;-
180 clause is specified in SQL queries. If a &quot;where&quot;-clause is
181 specified, the resulting filter is &quot;and&quot;-ed with this one. The
182 default is &quot;(objectclass=*)&quot;.
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. &quot;select * from tablename&quot;. 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 &quot;&lt;basefilter&gt;&quot;. 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 &quot;scope&quot; of the search. Values are: &quot;base&quot;,
205 &quot;one&quot;, and &quot;sub&quot;, see Net::LDAP docs. Default is &quot;one&quot;,
206 meaning the set of records one level below the basedn. &quot;base&quot;
207 means search only the basedn, and &quot;sub&quot; means the union
208 of entries at the &quot;base&quot; level and &quot;one&quot; 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 &quot;|&quot;.
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 &quot;|&quot;.
215 ldap_firstonly - only display the 1st value fetched for each attribute
216 per entry. This makes &quot;ldap_outseparator&quot; 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-&gt;connect('DBD:LDAP:mydb','me','mypassword') ||
227 die &quot;Could not connect (&quot;.$DBI-&gt;err.':'.$DBI-&gt;errstr.&quot;)!&quot;;
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 &quot;ldapdb&quot; with the tables previously
238 mentioned in the example in the DESCRIPTION section:
239 </PRE>
240 <PRE>
241
242 ----------------- file &quot;ldapdb.ldb&quot; ----------------
243 ldapserver:dc=Acme, dc=com:cn=*,&lt;ROOT&gt;
244 top:::dc
245 WidgetDivision:ou=Widgets, :&amp;(objectclass=top)(objectclass=person):cn:cn,sn,ou,title,telephonenumber,description,objectclass,dn:objectclass=top|person|organizationalPerson:ldap_outseparator =&gt; &quot;:&quot;
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-&gt;do(&lt;&lt;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-&gt;do(&quot;INSERT INTO WidgetDivision (cn,sn,title,telephonenumber) VALUES (?, ?, ?, ?)&quot;,
259 'John Doe','DoeJ','Manager','123-1111');
260 END_SQL
261 $dbh-&gt;commit();</PRE>
262 <PRE>
263 NOTE: Unlike most other DBD modules which support transactions, changes
264 made do NOT show up until the &quot;commit&quot; function is called, unless
265 &quot;AutoCommit&quot; 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 &quot;update&quot; function is called, which is called by &quot;commit&quot;.</PRE>
268 <PRE>
269 NOTE: The &quot;dn&quot; field is generated automatically from the base &quot;dn&quot; and the
270 dn component fields specified by &quot;dnattrs&quot;, 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 &quot;insertattrs&quot;
273 option will be added automatically.</PRE>
274 <PRE>
275 To retrieve data, you can use the following:</PRE>
276 <PRE>
277 my($query) = &quot;SELECT * FROM WidgetDivision WHERE cn like 'John%' ORDER BY cn&quot;;
278 my($sth) = $dbh-&gt;prepare($query);
279 $sth-&gt;execute();
280 while (my $entry = $sth-&gt;fetchrow_hashref) {
281 print(&quot;Found result record: cn = &quot;, $entry-&gt;{'cn'},
282 &quot;, phone = &quot;, $row-&gt;{'telephonenumber'});
283 }
284 $sth-&gt;finish();</PRE>
285 <PRE>
286 The SQL &quot;SELECT&quot; statement above (combined with the table information
287 in the &quot;ldapdb.ldb&quot; database file would generate and execute the following
288 equivalent LDAP Search:</PRE>
289 <PRE>
290 base =&gt; 'ou=Widgets, dc=Acme, dc=com',
291 filter =&gt; '(&amp;(&amp;(objectclass=top)(objectclass=person))(cn=John*))',
292 scope =&gt; 'one',
293 attrs =&gt; '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-&gt;do(&quot;UPDATE WidgetDivision SET description = 'Outstanding Employee' WHERE cn = 'John Doe'&quot;);</PRE>
300 <PRE>
301 NOTE: You can NOT change the &quot;dn&quot; field directly - direct changes will be
302 ignored. You change the &quot;rdn&quot; component of the &quot;dn&quot; field by changing
303 the value of the other field(s) which are appended to the base &quot;dn&quot;.
304 Also, if not specified, any attribute/value combinations specified in
305 the &quot;insertattrs&quot; option will be added automatically.</PRE>
306 <PRE>
307 Likewise you use the DELETE statement for removing entries:</PRE>
308 <PRE>
309 $dbh-&gt;do(&quot;DELETE FROM WidgetDivision WHERE description = 'Outstanding Employee'&quot;);</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-&gt;execute'</PRE>
328 <PRE>
329 NUM_OF_PARAMS
330 Valid after '$sth-&gt;prepare'</PRE>
331 <PRE>
332 NAME
333 Valid after '$sth-&gt;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-&gt;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 &quot;connect&quot;.</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-&gt;data_sources()
368 The `data_sources' method returns a list of &quot;databases&quot; (.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-&gt;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-&gt;connect(&quot;DBI:LDAP:mydatabase&quot;,'me','mypswd');
380 my(@list) = $dbh-&gt;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 &quot;&lt;database&gt;.ldb&quot; file
392 using your favorite text editor. I hope to add support for the SQL
393 &quot;Create Table&quot;, &quot;Alter Table&quot;, and &quot;Drop Table&quot; functions to handle this
394 eventually.</PRE>
395 <P>
396 <HR>
397 <H1><A NAME="todo">TODO</A></H1>
398 <PRE>
399 &quot;Create Table&quot;, &quot;Alter Table&quot;, and &quot;Drop Table&quot; SQL functions for
400 creating, altering, and deleting the tables defined in the
401 &quot;&lt;database&gt;.ldb&quot; file.</PRE>
402 <PRE>
403 Some kind of datatype support, ie. numeric (for sorting), CHAR for padding,
404 Long/Blob - for &gt;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>