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