]> git.sur5r.net Git - openldap/blob - doc/man/man5/slapd-sql.5
3ca8a186780d466bfb88c3577d77fbd0481d5293
[openldap] / doc / man / man5 / slapd-sql.5
1 .TH SLAPD-SQL 5 "RELEASEDATE" "OpenLDAP LDVERSION"
2 .\" $OpenLDAP$
3 .SH NAME
4 slapd-sql \- SQL backend to slapd
5 .SH SYNOPSIS
6 ETCDIR/slapd.conf
7 .SH DESCRIPTION
8 The primary purpose of this
9 .BR slapd (8)
10 backend is to PRESENT information stored in some RDBMS as an LDAP subtree
11 without any programming (some SQL and maybe stored procedures can't be
12 considered programming, anyway ;).
13 .LP
14 That is, for example, when you (some ISP) have account information you
15 use in an RDBMS, and want to use modern solutions that expect such
16 information in LDAP (to authenticate users, make email lookups etc.).
17 Or you want to synchronize or distribute information between different
18 sites/applications that use RDBMSes and/or LDAP.
19 Or whatever else...
20 .LP
21 It is NOT designed as a general-purpose backend that uses RDBMS instead
22 of BerkeleyDB (as the standard BDB backend does), though it can be
23 used as such with several limitations.
24 You can take a look at
25 .B http://www.openldap.org/faq/index.cgi?file=378 
26 (OpenLDAP FAQ-O-Matic/General LDAP FAQ/Directories vs. conventional
27 databases) to find out more on this point.
28 .LP
29 The idea (detailed below) is to use some metainformation to translate
30 LDAP queries to SQL queries, leaving relational schema untouched, so
31 that old applications can continue using it without any
32 modifications.
33 This allows SQL and LDAP applications to inter-operate without
34 replication, and exchange data as needed.
35 .LP
36 The SQL backend is designed to be tunable to virtually any relational
37 schema without having to change source (through that metainformation
38 mentioned).
39 Also, it uses ODBC to connect to RDBMSes, and is highly configurable
40 for SQL dialects RDBMSes may use, so it may be used for integration
41 and distribution of data on different RDBMSes, OSes, hosts etc., in
42 other words, in highly heterogeneous environment.
43 .LP
44 This backend is experimental.
45 .SH CONFIGURATION
46 These
47 .B slapd.conf
48 options apply to the SQL backend database.
49 That is, they must follow a "database sql" line and come before any
50 subsequent "backend" or "database" lines.
51 Other database options are described in the
52 .BR slapd.conf (5)
53 manual page.
54 .TP
55 .B dbname <datasource name>
56 The name of the ODBC datasource to use.
57 .LP
58 .B dbhost <hostname>
59 .br
60 .B dbuser <username>
61 .br
62 .B dbpasswd <password>
63 .RS
64 These three options are generally unneeded, because this information is already
65 taken from the datasource.
66 Use them if you need to override datasource settings.
67 Also, several RDBMS' drivers tend to require explicit passing of user/password,
68 even if those are given in datasource (Note:
69 .B dbhost
70 is currently ignored).
71 .RE
72 .TP
73 .B subtree_cond <SQL expression>
74 Specifies a where-clause template used to form a subtree search condition
75 (dn=".*<dn>").
76 It may differ from one SQL dialect to another (see samples).
77 .TP
78 .B children_cond <SQL expression>
79 Specifies a where-clause template used to form a children search condition
80 (dn="\.+,<dn>").
81 It may differ from one SQL dialect to another (see samples).
82 .TP
83 .B oc_query <SQL expression>
84 The default is
85 .B "SELECT id, name, keytbl, keycol, create_proc, delete_proc, expect_return FROM ldap_oc_mappings"
86 .TP
87 .B at_query <SQL expression>
88 The default is
89 .B "SELECT name, sel_expr, from_tbls, join_where, add_proc, delete_proc, param_order, expect_return FROM ldap_attr_mappings WHERE oc_map_id=?"
90 .TP
91 .B insentry_query <SQL expression>
92 The default is
93 .B "INSERT INTO ldap_entries (dn, oc_map_id, parent, keyval) VALUES (?, ?, ?, ?)"
94 .TP
95 .B delentry_query <SQL expression>
96 The default is
97 .B "DELETE FROM ldap_entries WHERE id=?"
98
99 These four options specify SQL query templates for loading schema mapping
100 metainformation,
101 adding and deleting entries to ldap_entries, etc.
102 All these and subtree_cond should have the given default values.
103 For the current value it is recommended to look at the sources,
104 or in the log output when slapd starts with "-d 5" or greater.
105 Note that the parameter number and order must not be changed.
106 .TP
107 .B upper_func <SQL function name>
108 Specifies the name of a function that converts a given value to uppercase.
109 This is used for CIS matching when the RDBMS is case sensitive.
110 .TP
111 .B upper_needs_cast { yes | no }
112 Set this directive to 
113 .B yes
114 if 
115 .B upper_func
116 needs an explicit cast when applied to literal strings.  The form
117 .B cast (<arg> as varchar(<max DN length>))
118 is used, where
119 .B <max DN length>
120 is builtin.
121 This is
122 .B experimental 
123 and may change in future releases.
124 .TP
125 .B concat_pattern <pattern>
126 This statement defines the
127 .B pattern 
128 to be used to concatenate strings.  The
129 .B pattern
130 MUST contain two question marks, '?', that will be replaced 
131 by the two strings that must be concatenated.  The default value is
132 .BR "CONCAT(?,?)";
133 a form that is known to be highly portable is 
134 .BR "?||?",
135 but an explicit cast may be required when operating on literal strings:
136 .BR "cast(?||? as varchar(<length>))".
137 On some RDBMSes the form
138 .B "?+?"
139 is known to work.
140 Carefully check the documentation of your RDBMS or stay with the examples
141 for supported ones.
142 This is
143 .B experimental 
144 and may change in future releases.
145 .TP
146 .B strcast_func <SQL function name>
147 Specifies the name of a function that converts a given value to a string
148 for appropriate ordering.  This is used in "SELECT DISTINCT" statements
149 for strongly typed RDBMSes with little implicit casting (like PostgreSQL),
150 when a literal string is specified.
151 This is
152 .B experimental 
153 and may change in future releases.
154 .TP
155 .B has_ldapinfo_dn_ru { yes | no }
156 Explicitly inform the backend whether the SQL schema has dn_ru column
157 (dn in reverse uppercased form) or not.
158 Overrides automatic check (required by PostgreSQL/unixODBC).
159 This is
160 .B experimental 
161 and may change in future releases.
162
163 .TP
164 .B fail_if_no_mapping { yes | no }
165 When set to
166 .B yes
167 it forces write operations to fail if no appropriate mapping between LDAP
168 attributes and SQL data is available.  The default behavior is to ignore
169 those changes that cannot be mapped correctly.
170 This is
171 .B experimental 
172 and may change in future releases.
173
174 .SH METAINFORMATION USED
175 .LP
176 Almost everything mentioned later is illustrated in examples located
177 in the
178 .B servers/slapd/back-sql/rdbms_depend/
179 directory in the OpenLDAP source tree, and contains scripts for
180 generating sample database for Oracle, MS SQL Server, mySQL and more
181 (including PostgreSQL and IBM db2).
182 .LP
183 The first thing that one must arrange is what set of LDAP
184 object classes can present your RDBMS information.
185 .LP
186 The easiest way is to create an objectclass for each entity you had in
187 ER-diagram when designing your relational schema.
188 Any relational schema, no matter how normalized it is, was designed
189 after some model of your application's domain (for instance, accounts,
190 services etc. in ISP), and is used in terms of its entities, not just
191 tables of normalized schema.
192 It means that for every attribute of every such instance there is an
193 effective SQL query that loads its values.
194 .LP
195 Also you might want your object classes to conform to some of the standard
196 schemas like inetOrgPerson etc.
197 .LP
198 Nevertheless, when you think it out, we must define a way to translate
199 LDAP operation requests to (a series of) SQL queries.
200 Let us deal with the SEARCH operation.
201 .LP
202 Example:
203 Let's suppose that we store information about persons working in our 
204 organization in two tables:
205 .LP
206 .nf
207   PERSONS              PHONES
208   ----------           -------------
209   id integer           id integer
210   first_name varchar   pers_id integer references persons(id)
211   last_name varchar    phone
212   middle_name varchar
213   ...
214 .fi
215 .LP
216 (PHONES contains telephone numbers associated with persons).
217 A person can have several numbers, then PHONES contains several
218 records with corresponding pers_id, or no numbers (and no records in
219 PHONES with such pers_id).
220 An LDAP objectclass to present such information could look like this:
221 .LP
222 .nf
223   person
224   -------
225   MUST cn
226   MAY telephoneNumber $ firstName $ lastName
227   ...
228 .fi
229 .LP
230 To fetch all values for cn attribute given person ID, we construct the
231 query:
232 .LP
233 .nf
234   SELECT CONCAT(persons.first_name,' ',persons.last_name)
235       AS cn FROM persons WHERE persons.id=?
236 .fi
237 .LP
238 for telephoneNumber we can use:
239 .LP
240 .nf
241   SELECT phones.phone AS telephoneNumber FROM persons,phones
242    WHERE persons.id=phones.pers_id AND persons.id=?
243 .fi
244 .LP
245 If we wanted to service LDAP requests with filters like
246 (telephoneNumber=123*), we would construct something like:
247 .LP
248 .nf
249   SELECT ... FROM persons,phones
250    WHERE persons.id=phones.pers_id
251      AND persons.id=?
252      AND phones.phone like '123%'
253 .fi
254 .LP
255 So, if we had information about what tables contain values for each
256 attribute, how to join these tables and arrange these values, we could
257 try to automatically generate such statements, and translate search
258 filters to SQL WHERE clauses.
259 .LP
260 To store such information, we add three more tables to our schema
261 and fill it with data (see samples):
262 .LP
263 .nf
264   ldap_oc_mappings (some columns are not listed for clarity)
265   ---------------
266   id=1
267   name="person"
268   keytbl="persons"
269   keycol="id"
270 .fi
271 .LP
272 This table defines a mapping between objectclass (its name held in the
273 "name" column), and a table that holds the primary key for corresponding
274 entities.
275 For instance, in our example, the person entity, which we are trying
276 to present as "person" objectclass, resides in two tables (persons and
277 phones), and is identified by the persons.id column (that we will call
278 the primary key for this entity).
279 Keytbl and keycol thus contain "persons" (name of the table), and "id"
280 (name of the column).
281 .LP
282 .nf
283   ldap_attr_mappings (some columns are not listed for clarity)
284   -----------
285   id=1
286   oc_map_id=1
287   name="cn"
288   sel_expr="CONCAT(persons.first_name,' ',persons.last_name)"
289   from_tbls="persons"
290   join_where=NULL
291   ************
292   id=<n>
293   oc_map_id=1
294   name="telephoneNumber"
295   sel_expr="phones.phone"
296   from_tbls="persons,phones"
297   join_where="phones.pers_id=persons.id"
298 .fi
299 .LP
300 This table defines mappings between LDAP attributes and SQL queries
301 that load their values.
302 Note that, unlike LDAP schema, these are not
303 .B attribute types
304 - the attribute "cn" for "person" objectclass can
305 have its values in different tables than "cn" for some other objectclass,
306 so attribute mappings depend on objectclass mappings (unlike attribute
307 types in LDAP schema, which are indifferent to objectclasses).
308 Thus, we have oc_map_id column with link to oc_mappings table.
309 .LP
310 Now we cut the SQL query that loads values for a given attribute into 3 parts.
311 First goes into sel_expr column - this is the expression we had
312 between SELECT and FROM keywords, which defines WHAT to load.
313 Next is table list - text between FROM and WHERE keywords.
314 It may contain aliases for convenience (see examples).
315 The last is part of the where clause, which (if it exists at all) expresses the
316 condition for joining the table containing values with the table
317 containing the primary key (foreign key equality and such).
318 If values are in the same table as the primary key, then this column is
319 left NULL (as for cn attribute above).
320 .LP
321 Having this information in parts, we are able to not only construct
322 queries that load attribute values by id of entry (for this we could
323 store SQL query as a whole), but to construct queries that load id's
324 of objects that correspond to a given search filter (or at least part of
325 it).
326 See below for examples.
327 .LP
328 .nf
329   ldap_entries
330   ------------
331   id=1
332   dn=<dn you choose>
333   oc_map_id=...
334   parent=<parent record id>
335   keyval=<value of primary key>
336 .fi
337 .LP
338 This table defines mappings between DNs of entries in your LDAP tree,
339 and values of primary keys for corresponding relational data.
340 It has recursive structure (parent column references id column of the
341 same table), which allows you to add any tree structure(s) to your
342 flat relational data.
343 Having id of objectclass mapping, we can determine table and column
344 for primary key, and keyval stores value of it, thus defining the exact
345 tuple corresponding to the LDAP entry with this DN.
346 .LP
347 Note that such design (see exact SQL table creation query) implies one
348 important constraint - the key must be an integer.
349 But all that I know about well-designed schemas makes me think that it's
350 not very narrow ;) If anyone needs support for different types for
351 keys - he may want to write a patch, and submit it to OpenLDAP ITS,
352 then I'll include it.
353 .LP
354 Also, several people complained that they don't really need very
355 structured trees, and they don't want to update one more table every
356 time they add or delete an instance in the relational schema.
357 Those people can use a view instead of a real table for ldap_entries, something
358 like this (by Robin Elfrink):
359 .LP
360 .nf
361   CREATE VIEW ldap_entries (id, dn, oc_map_id, parent, keyval)
362       AS SELECT (1000000000+userid),
363   UPPER(CONCAT(CONCAT('cn=',gecos),',o=MyCompany,c=NL')),
364   1, 0, userid FROM unixusers UNION
365           SELECT (2000000000+groupnummer),
366   UPPER(CONCAT(CONCAT('cn=',groupnaam),',o=MyCompany,c=NL')),
367   2, 0, groupnummer FROM groups;
368 .fi
369 .LP
370 .SH Typical SQL backend operation
371 Having metainformation loaded, the SQL backend uses these tables to
372 determine a set of primary keys of candidates (depending on search
373 scope and filter).
374 It tries to do it for each objectclass registered in ldap_objclasses.
375 .LP
376 Example:
377 for our query with filter (telephoneNumber=123*) we would get the following 
378 query generated (which loads candidate IDs)
379 .LP
380 .nf
381   SELECT ldap_entries.id,persons.id, 'person' AS objectClass,
382          ldap_entries.dn AS dn
383     FROM ldap_entries,persons,phones
384    WHERE persons.id=ldap_entries.keyval
385      AND ldap_entries.objclass=?
386      AND ldap_entries.parent=?
387      AND phones.pers_id=persons.id
388      AND (phones.phone LIKE '123%')
389 .fi
390 .LP
391 (for ONELEVEL search)
392 or "... AND dn=?" (for BASE search)
393 or "... AND dn LIKE '%?'" (for SUBTREE)
394 .LP
395 Then, for each candidate, we load the requested attributes using
396 per-attribute queries like
397 .LP
398 .nf
399   SELECT phones.phone AS telephoneNumber
400     FROM persons,phones
401    WHERE persons.id=? AND phones.pers_id=persons.id
402 .fi
403 .LP
404 Then, we use test_filter() from the frontend API to test the entry for a full
405 LDAP search filter match (since we cannot effectively make sense of
406 SYNTAX of corresponding LDAP schema attribute, we translate the filter
407 into the most relaxed SQL condition to filter candidates), and send it to
408 the user.
409 .LP
410 ADD, DELETE, MODIFY operations are also performed on per-attribute
411 metainformation (add_proc etc.).
412 In those fields one can specify an SQL statement or stored procedure
413 call which can add, or delete given values of a given attribute, using
414 the given entry keyval (see examples -- mostly ORACLE and MSSQL - since
415 there're no stored procs in mySQL).
416 .LP
417 We just add more columns to oc_mappings and attr_mappings, holding
418 statements to execute (like create_proc, add_proc, del_proc etc.), and
419 flags governing the order of parameters passed to those statements.
420 Please see samples to find out what are the parameters passed, and other
421 information on this matter - they are self-explanatory for those familiar
422 with concept expressed above.
423 .LP
424 .SH common techniques (referrals, multiclassing etc.)
425 First of all, let's remember that among other major differences to the
426 complete LDAP data model, the concept above does not directly support
427 such things as multiple objectclasses per entry, and referrals.
428 Fortunately, they are easy to adopt in this scheme.
429 The SQL backend suggests two more tables being added to the schema -
430 ldap_entry_objectclasses(entry_id,oc_name), and
431 ldap_referrals(entry_id,url).
432 .LP
433 The first contains any number of objectclass names that corresponding
434 entries will be found by, in addition to that mentioned in
435 mapping.
436 The SQL backend automatically adds attribute mapping for the "objectclass"
437 attribute to each objectclass mapping that loads values from this table.
438 So, you may, for instance, have a mapping for inetOrgPerson, and use it
439 for queries for "person" objectclass...
440 .LP
441 The second table contains any number of referrals associated with a given entry.
442 The SQL backend automatically adds attribute mapping for "ref" attribute
443 to each objectclass mapping that loads values from this table.
444 So, if you add objectclass "referral" to this entry, and make one or
445 more tuples in ldap_referrals for this entry (they will be seen as
446 values of "ref" attribute), you will have slapd return a referral, as
447 described in the Administrators Guide.
448 .LP
449 .SH EXAMPLES
450 There are example SQL modules in the slapd/back-sql/rdbms_depend/
451 directory in the OpenLDAP source tree.
452 .SH FILES
453 .TP
454 ETCDIR/slapd.conf
455 default slapd configuration file
456 .SH SEE ALSO
457 .BR slapd.conf (5),
458 .BR slapd (8).