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