1 .TH SLAPD-SQL 5 "RELEASEDATE" "OpenLDAP LDVERSION"
4 slapd-sql \- SQL backend to slapd
8 The primary purpose of this backend (8) to
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 ;).
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.
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.
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
33 This allows SQL and LDAP applications to inter-operate without
34 replication, and exchange data as needed.
36 The SQL backend is designed to be tunable to virtually any relational
37 schema without having to change source (through that metainformation
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.
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
53 .B dbname <datasource name>
54 The name of the ODBC datasource to use.
60 .B dbpasswd <password>
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.
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).
73 .B oc_query <SQL expression>
75 .B "SELECT id, name, keytbl, keycol, create_proc, delete_proc, expect_return FROM ldap_oc_mappings"
77 .B at_query <SQL expression>
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=?"
81 .B insentry_query <SQL expression>
83 .B "INSERT INTO ldap_entries (dn, oc_map_id, parent, keyval) VALUES (?, ?, ?, ?)"
85 .B delentry_query <SQL expression>
87 .B "DELETE FROM ldap_entries WHERE id=?"
89 These four options specify SQL query templates for loading schema mapping
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.
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.
100 .SH METAINFORMATION USED
102 Almost everything mentioned later is illustrated in examples located
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.
108 The first thing that one must arrange is what set of LDAP
109 object classes can present your RDBMS information.
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.
120 Also you might want your object classes to conform to some of the standard
121 schemas like inetOrgPerson etc.
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.
128 Let's suppose that we store information about persons working in our
129 organization in two tables:
133 ---------- -------------
134 id integer id integer
135 first_name varchar pers_id integer references persons(id)
136 last_name varchar phone
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:
151 MAY telephoneNumber $ firstName $ lastName
155 To fetch all values for cn attribute given person ID, we construct the
159 SELECT CONCAT(persons.first_name,' ',persons.last_name)
160 AS cn FROM persons WHERE persons.id=?
163 for telephoneNumber we can use:
166 SELECT phones.phone AS telephoneNumber FROM persons,phones
167 WHERE persons.id=phones.pers.id AND persons.id=?
170 If we wanted to service LDAP requests with filters like
171 (telephoneNumber=123*), we would construct something like:
174 SELECT ... FROM persons,phones
175 WHERE persons.id=phones.pers.id
177 AND phones.phone like '123%'
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.
185 To store such information, we add three more tables to our schema
186 and fill it with data (see samples):
189 ldap_oc_mappings (some columns are not listed for clarity)
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
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).
208 ldap_attr_mappings (some columns are not listed for clarity)
213 sel_expr="CONCAT(persons.first_name,' ',persons.last_name)"
219 name="telephoneNumber"
220 sel_expr="phones.phone"
221 from_tbls="persons,phones"
222 join_where="phones.pers_id=persons.id"
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
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.
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).
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
251 See below for examples.
259 parent=<parent record id>
260 keyval=<value of primary key>
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.
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.
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):
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;
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
299 It tries to do it for each objectclass registered in ldap_objclasses.
302 for our query with filter (telephoneNumber=123*) we would get the following
303 query generated (which loads candidate IDs)
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%')
316 (for ONELEVEL search)
317 or "... AND dn=?" (for BASE search)
318 or "... AND dn LIKE '%?'" (for SUBTREE)
320 Then, for each candidate, we load the requested attributes using
321 per-attribute queries like
324 SELECT phones.phone AS telephoneNumber
326 WHERE persons.id=? AND phones.pers_id=persons.id
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
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).
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.
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).
358 The first contains any number of objectclass names that corresponding
359 entries will be found by, in addition to that mentioned in
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...
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.
375 There are example SQL modules in the slapd/back-sql/rdbms_depend/
376 directory in the OpenLDAP source tree.
380 default slapd configuration file