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