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