2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
6 * Portions Copyright 2002 Pierangelo Mararati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Dmitry Kovalev for inclusion
19 * by OpenLDAP Software. Additional significant contributors include
24 * The following changes have been addressed:
27 * - re-styled code for better readability
28 * - upgraded backend API to reflect recent changes
29 * - LDAP schema is checked when loading SQL/LDAP mapping
30 * - AttributeDescription/ObjectClass pointers used for more efficient
32 * - bervals used where string length is required often
33 * - atomized write operations by committing at the end of each operation
34 * and defaulting connection closure to rollback
35 * - added LDAP access control to write operations
36 * - fully implemented modrdn (with rdn attrs change, deleteoldrdn,
37 * access check, parent/children check and more)
38 * - added parent access control, children control to delete operation
39 * - added structuralObjectClass operational attribute check and
40 * value return on search
41 * - added hasSubordinate operational attribute on demand
42 * - search limits are appropriately enforced
43 * - function backsql_strcat() has been made more efficient
44 * - concat function has been made configurable by means of a pattern
45 * - added config switches:
46 * - fail_if_no_mapping write operations fail if there is no mapping
47 * - has_ldapinfo_dn_ru overrides autodetect
48 * - concat_pattern a string containing two '?' is used
49 * (note that "?||?" should be more portable
50 * than builtin function "CONCAT(?,?)")
51 * - strcast_func cast of string constants in "SELECT DISTINCT
52 * statements (needed by PostgreSQL)
53 * - upper_needs_cast cast the argument of upper when required
54 * (basically when building dn substring queries)
55 * - added noop control
56 * - added values return filter control
57 * - hasSubordinate can be used in search filters (with limitations)
58 * - eliminated oc->name; use oc->oc->soc_cname instead
61 * - add security checks for SQL statements that can be injected (?)
62 * - re-test with previously supported RDBMs
63 * - replace dn_ru and so with normalized dn (no need for upper() and so
65 * - implement a backsql_normalize() function to replace the upper()
67 * - note that subtree deletion, subtree renaming and so could be easily
68 * implemented (rollback and consistency checks are available :)
69 * - implement "lastmod" and other operational stuff (ldap_entries table ?)
70 * - check how to allow multiple operations with one statement, to remove
71 * BACKSQL_REALLOC_STMT from modify.c (a more recent unixODBC lib?)
78 #include "sql-types.h"
81 * Better use the standard length of 8192 (as of slap.h)?
83 * NOTE: must be consistent with definition in ldap_entries table
85 /* #define BACKSQL_MAX_DN_LEN SLAP_LDAPDN_MAXLEN */
86 #define BACKSQL_MAX_DN_LEN 255
89 * define to enable very extensive trace logging (debug only)
95 * define to enable varchars as unique keys in user tables
97 * by default integers are used (and recommended)
98 * for performances. Integers are used anyway in back-sql
101 #undef BACKSQL_ARBITRARY_KEY
104 * define to the appropriate aliasing string
106 * some RDBMSes tolerate (or require) that " AS " is not used
107 * when aliasing tables/columns
109 #define BACKSQL_ALIASING "AS "
110 /* #define BACKSQL_ALIASING "" */
113 * define to the appropriate quoting char
115 * some RDBMSes tolerate/require that the aliases be enclosed
116 * in quotes. This is especially true for those that do not
117 * allow keywords used as aliases.
119 /* #define BACKSQL_ALIASING_QUOTE '"' */
120 /* #define BACKSQL_ALIASING_QUOTE '\'' */
125 * a simple mechanism to allow DN mucking between the LDAP
126 * and the stored string representation.
128 typedef struct backsql_api {
130 int (*ba_dn2odbc)( Operation *op, SlapReply *rs, struct berval *dn );
131 int (*ba_odbc2dn)( Operation *op, SlapReply *rs, struct berval *dn );
132 struct backsql_api *ba_next;
138 typedef struct backsql_entryID {
139 /* #define BACKSQL_ARBITRARY_KEY to allow a non-numeric key.
140 * It is required by some special applications that use
141 * strings as keys for the main table.
142 * In this case, #define BACKSQL_MAX_KEY_LEN consistently
143 * with the key size definition */
144 #ifdef BACKSQL_ARBITRARY_KEY
145 struct berval eid_id;
146 struct berval eid_keyval;
147 #define BACKSQL_MAX_KEY_LEN 64
148 #else /* ! BACKSQL_ARBITRARY_KEY */
149 /* The original numeric key is maintained as default. */
150 unsigned long eid_id;
151 unsigned long eid_keyval;
152 #endif /* ! BACKSQL_ARBITRARY_KEY */
154 unsigned long eid_oc_id;
155 struct berval eid_dn;
156 struct berval eid_ndn;
157 struct backsql_entryID *eid_next;
160 #ifdef BACKSQL_ARBITRARY_KEY
161 #define BACKSQL_ENTRYID_INIT { BER_BVNULL, BER_BVNULL, 0, BER_BVNULL, BER_BVNULL, NULL }
162 #else /* ! BACKSQL_ARBITRARY_KEY */
163 #define BACKSQL_ENTRYID_INIT { 0, 0, 0, BER_BVNULL, BER_BVNULL, NULL }
164 #endif /* BACKSQL_ARBITRARY_KEY */
167 * "structural" objectClass mapping structure
169 typedef struct backsql_oc_map_rec {
171 * Structure of corresponding LDAP objectClass definition
174 #define BACKSQL_OC_NAME(ocmap) ((ocmap)->bom_oc->soc_cname.bv_val)
176 struct berval bom_keytbl;
177 struct berval bom_keycol;
178 /* expected to return keyval of newly created entry */
179 char *bom_create_proc;
180 /* in case create_proc does not return the keyval of the newly
182 char *bom_create_keyval;
183 /* supposed to expect keyval as parameter and delete
184 * all the attributes as well */
185 char *bom_delete_proc;
186 /* flags whether delete_proc is a function (whether back-sql
187 * should bind first parameter as output for return code) */
188 int bom_expect_return;
189 unsigned long bom_id;
191 } backsql_oc_map_rec;
194 * attributeType mapping structure
196 typedef struct backsql_at_map_rec {
197 /* Description of corresponding LDAP attribute type */
198 AttributeDescription *bam_ad;
199 /* ObjectClass if bam_ad is objectClass */
202 struct berval bam_from_tbls;
203 struct berval bam_join_where;
204 struct berval bam_sel_expr;
206 /* TimesTen, or, if a uppercase function is defined,
207 * an uppercased version of bam_sel_expr */
208 struct berval bam_sel_expr_u;
210 /* supposed to expect 2 binded values: entry keyval
211 * and attr. value to add, like "add_name(?,?,?)" */
213 /* supposed to expect 2 binded values: entry keyval
214 * and attr. value to delete */
215 char *bam_delete_proc;
216 /* for optimization purposes attribute load query
217 * is preconstructed from parts on schemamap load time */
219 /* following flags are bitmasks (first bit used for add_proc,
220 * second - for delete_proc) */
221 /* order of parameters for procedures above;
222 * 1 means "data then keyval", 0 means "keyval then data" */
224 /* flags whether one or more of procedures is a function
225 * (whether back-sql should bind first parameter as output
226 * for return code) */
227 int bam_expect_return;
229 /* next mapping for attribute */
230 struct backsql_at_map_rec *bam_next;
231 } backsql_at_map_rec;
233 #define BACKSQL_AT_MAP_REC_INIT { NULL, NULL, BER_BVC(""), BER_BVC(""), BER_BVNULL, BER_BVNULL, NULL, NULL, NULL, 0, 0, NULL }
235 /* define to uppercase filters only if the matching rule requires it
236 * (currently broken) */
237 /* #define BACKSQL_UPPERCASE_FILTER */
239 #define BACKSQL_AT_CANUPPERCASE(at) ((at)->bam_sel_expr_u.bv_val)
241 /* defines to support bitmasks above */
242 #define BACKSQL_ADD 0x1
243 #define BACKSQL_DEL 0x2
245 #define BACKSQL_IS_ADD(x) ( BACKSQL_ADD & (x) )
246 #define BACKSQL_IS_DEL(x) ( BACKSQL_DEL & (x) )
248 #define BACKSQL_NCMP(v1,v2) ber_bvcmp((v1),(v2))
250 #define BACKSQL_CONCAT
252 * berbuf structure: a berval with a buffer size associated
254 typedef struct berbuf {
255 struct berval bb_val;
259 #define BB_NULL { { 0, NULL }, 0 }
261 typedef struct backsql_srch_info {
266 #define BSQL_SF_ALL_OPER 0x0001
267 #define BSQL_SF_FILTER_HASSUBORDINATE 0x0002
269 struct berval *bsi_base_ndn;
270 backsql_entryID bsi_base_id;
272 /* BACKSQL_SCOPE_BASE_LIKE can be set by API in ors_scope
273 * whenever the search base DN contains chars that cannot
274 * be mapped into the charset used in the RDBMS; so they're
275 * turned into '%' and an approximate ('LIKE') condition
277 #define BACKSQL_SCOPE_BASE_LIKE ( LDAP_SCOPE_BASE | 0x1000 )
283 backsql_entryID *bsi_id_list,
286 int bsi_n_candidates;
290 backsql_oc_map_rec *bsi_oc;
291 struct berbuf bsi_sel,
295 ObjectClass *bsi_filter_oc;
297 AttributeName *bsi_attrs;
303 * Backend private data structure
313 * SQL condition for subtree searches differs in syntax:
314 * "LIKE CONCAT('%',?)" or "LIKE '%'+?" or "LIKE '%'||?"
317 struct berval sql_subtree_cond;
318 struct berval sql_children_cond;
321 char *sql_insentry_query,
323 *sql_delobjclasses_query,
324 *sql_delreferrals_query;
326 char *sql_has_children_query;
328 MatchingRule *sql_caseIgnoreMatch;
329 MatchingRule *sql_telephoneNumberMatch;
331 struct berval sql_upper_func;
332 struct berval sql_upper_func_open;
333 struct berval sql_upper_func_close;
334 BerVarray sql_concat_func;
336 struct berval sql_strcast_func;
338 unsigned int sql_flags;
339 #define BSQLF_SCHEMA_LOADED 0x0001
340 #define BSQLF_UPPER_NEEDS_CAST 0x0002
341 #define BSQLF_CREATE_NEEDS_SELECT 0x0004
342 #define BSQLF_FAIL_IF_NO_MAPPING 0x0008
343 #define BSQLF_HAS_LDAPINFO_DN_RU 0x0010
344 #define BSQLF_DONTCHECK_LDAPINFO_DN_RU 0x0020
345 #define BSQLF_USE_REVERSE_DN 0x0040
346 #define BSQLF_ALLOW_ORPHANS 0x0080
348 #define BACKSQL_SCHEMA_LOADED(si) \
349 ((si)->sql_flags & BSQLF_SCHEMA_LOADED)
350 #define BACKSQL_UPPER_NEEDS_CAST(si) \
351 ((si)->sql_flags & BSQLF_UPPER_NEEDS_CAST)
352 #define BACKSQL_CREATE_NEEDS_SELECT(si) \
353 ((si)->sql_flags & BSQLF_CREATE_NEEDS_SELECT)
354 #define BACKSQL_FAIL_IF_NO_MAPPING(si) \
355 ((si)->sql_flags & BSQLF_FAIL_IF_NO_MAPPING)
356 #define BACKSQL_HAS_LDAPINFO_DN_RU(si) \
357 ((si)->sql_flags & BSQLF_HAS_LDAPINFO_DN_RU)
358 #define BACKSQL_DONTCHECK_LDAPINFO_DN_RU(si) \
359 ((si)->sql_flags & BSQLF_DONTCHECK_LDAPINFO_DN_RU)
360 #define BACKSQL_USE_REVERSE_DN(si) \
361 ((si)->sql_flags & BSQLF_USE_REVERSE_DN)
362 #define BACKSQL_CANUPPERCASE(si) \
363 (!BER_BVISNULL( &(si)->sql_upper_func ))
364 #define BACKSQL_ALLOW_ORPHANS(si) \
365 ((si)->sql_flags & BSQLF_ALLOW_ORPHANS)
367 Entry *sql_baseObject;
368 #ifdef BACKSQL_ARBITRARY_KEY
369 #define BACKSQL_BASEOBJECT_IDSTR "baseObject"
370 #define BACKSQL_BASEOBJECT_KEYVAL BACKSQL_BASEOBJECT_IDSTR
371 #define BACKSQL_IS_BASEOBJECT_ID(id) (bvmatch((id), &backsql_baseObject_bv)
372 #else /* ! BACKSQL_ARBITRARY_KEY */
373 #define BACKSQL_BASEOBJECT_ID 0
374 #define BACKSQL_BASEOBJECT_IDSTR "0"
375 #define BACKSQL_BASEOBJECT_KEYVAL 0
376 #define BACKSQL_IS_BASEOBJECT_ID(id) (*(id) == BACKSQL_BASEOBJECT_ID)
377 #endif /* ! BACKSQL_ARBITRARY_KEY */
378 #define BACKSQL_BASEOBJECT_OC 0
380 Avlnode *sql_db_conns;
381 Avlnode *sql_oc_by_oc;
382 Avlnode *sql_oc_by_id;
383 ldap_pvt_thread_mutex_t sql_dbconn_mutex;
384 ldap_pvt_thread_mutex_t sql_schema_mutex;
387 backsql_api *sql_api;
390 #define BACKSQL_SUCCESS( rc ) \
391 ( (rc) == SQL_SUCCESS || (rc) == SQL_SUCCESS_WITH_INFO )
393 #define BACKSQL_AVL_STOP 0
394 #define BACKSQL_AVL_CONTINUE 1
396 #endif /* __BACKSQL_H__ */