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 * PostgreSQL 7.0 doesn't work without :(
83 #define BACKSQL_REALLOC_STMT
86 * Better use the standard length of 8192 (as of slap.h)?
88 /* #define BACKSQL_MAX_DN_LEN SLAP_LDAPDN_MAXLEN */
89 #define BACKSQL_MAX_DN_LEN 255
92 * define to enable very extensive trace logging (debug only)
99 typedef struct backsql_entryID {
101 unsigned long keyval;
104 struct backsql_entryID *next;
108 * "structural" objectClass mapping structure
110 typedef struct backsql_oc_map_rec {
112 * Structure of corresponding LDAP objectClass definition
115 #define BACKSQL_OC_NAME(ocmap) ((ocmap)->bom_oc->soc_cname.bv_val)
117 struct berval bom_keytbl;
118 struct berval bom_keycol;
119 /* expected to return keyval of newly created entry */
120 char *bom_create_proc;
121 /* in case create_proc does not return the keyval of the newly
123 char *bom_create_keyval;
124 /* supposed to expect keyval as parameter and delete
125 * all the attributes as well */
126 char *bom_delete_proc;
127 /* flags whether delete_proc is a function (whether back-sql
128 * should bind first parameter as output for return code) */
129 int bom_expect_return;
130 unsigned long bom_id;
132 } backsql_oc_map_rec;
135 * attributeType mapping structure
137 typedef struct backsql_at_map_rec {
138 /* Description of corresponding LDAP attribute type */
139 AttributeDescription *bam_ad;
140 /* ObjectClass if bam_ad is objectClass */
143 struct berval bam_from_tbls;
144 struct berval bam_join_where;
145 struct berval bam_sel_expr;
147 /* TimesTen, or, if a uppercase function is defined,
148 * an uppercased version of bam_sel_expr */
149 struct berval bam_sel_expr_u;
151 /* supposed to expect 2 binded values: entry keyval
152 * and attr. value to add, like "add_name(?,?,?)" */
154 /* supposed to expect 2 binded values: entry keyval
155 * and attr. value to delete */
156 char *bam_delete_proc;
157 /* for optimization purposes attribute load query
158 * is preconstructed from parts on schemamap load time */
160 /* following flags are bitmasks (first bit used for add_proc,
161 * second - for delete_proc) */
162 /* order of parameters for procedures above;
163 * 1 means "data then keyval", 0 means "keyval then data" */
165 /* flags whether one or more of procedures is a function
166 * (whether back-sql should bind first parameter as output
167 * for return code) */
168 int bam_expect_return;
170 /* next mapping for attribute */
171 struct backsql_at_map_rec *bam_next;
172 } backsql_at_map_rec;
174 #define BACKSQL_AT_MAP_REC_INIT { NULL, NULL, BER_BVC(""), BER_BVC(""), BER_BVNULL, BER_BVNULL, NULL, NULL, NULL, 0, 0, NULL }
176 /* define to uppercase filters only if the matching rule requires it
177 * (currently broken) */
178 /* #define BACKSQL_UPPERCASE_FILTER */
180 #define BACKSQL_AT_CANUPPERCASE(at) ((at)->bam_sel_expr_u.bv_val)
182 /* defines to support bitmasks above */
183 #define BACKSQL_ADD 0x1
184 #define BACKSQL_DEL 0x2
186 #define BACKSQL_IS_ADD(x) ( BACKSQL_ADD & (x) )
187 #define BACKSQL_IS_DEL(x) ( BACKSQL_DEL & (x) )
189 #define BACKSQL_NCMP(v1,v2) ber_bvcmp((v1),(v2))
191 #define BACKSQL_CONCAT
193 * berbuf structure: a berval with a buffer size associated
195 typedef struct berbuf {
196 struct berval bb_val;
200 #define BB_NULL { { 0, NULL }, 0 }
202 typedef struct backsql_srch_info {
206 #define BSQL_SF_ALL_OPER 0x0001
207 #define BSQL_SF_FILTER_HASSUBORDINATE 0x0002
209 struct berval *bsi_base_dn;
216 backsql_entryID *bsi_id_list,
218 int bsi_n_candidates;
222 backsql_oc_map_rec *bsi_oc;
223 struct berbuf bsi_sel,
227 ObjectClass *bsi_filter_oc;
229 AttributeName *bsi_attrs;
235 * Backend private data structure
244 * SQL condition for subtree searches differs in syntax:
245 * "LIKE CONCAT('%',?)" or "LIKE '%'+?" or "LIKE '%'||?"
248 struct berval subtree_cond;
249 struct berval children_cond;
250 char *oc_query, *at_query;
251 char *insentry_query,*delentry_query;
253 char *has_children_query;
255 MatchingRule *bi_caseIgnoreMatch;
257 struct berval upper_func;
258 struct berval upper_func_open;
259 struct berval upper_func_close;
260 BerVarray concat_func;
262 unsigned int bsql_flags;
263 #define BSQLF_SCHEMA_LOADED 0x0001
264 #define BSQLF_UPPER_NEEDS_CAST 0x0002
265 #define BSQLF_CREATE_NEEDS_SELECT 0x0004
266 #define BSQLF_FAIL_IF_NO_MAPPING 0x0008
267 #define BSQLF_HAS_LDAPINFO_DN_RU 0x0010
268 #define BSQLF_DONTCHECK_LDAPINFO_DN_RU 0x0020
269 #define BSQLF_USE_REVERSE_DN 0x0040
271 #define BACKSQL_SCHEMA_LOADED(si) \
272 ((si)->bsql_flags & BSQLF_SCHEMA_LOADED)
273 #define BACKSQL_UPPER_NEEDS_CAST(si) \
274 ((si)->bsql_flags & BSQLF_UPPER_NEEDS_CAST)
275 #define BACKSQL_CREATE_NEEDS_SELECT(si) \
276 ((si)->bsql_flags & BSQLF_CREATE_NEEDS_SELECT)
277 #define BACKSQL_FAIL_IF_NO_MAPPING(si) \
278 ((si)->bsql_flags & BSQLF_FAIL_IF_NO_MAPPING)
279 #define BACKSQL_HAS_LDAPINFO_DN_RU(si) \
280 ((si)->bsql_flags & BSQLF_HAS_LDAPINFO_DN_RU)
281 #define BACKSQL_DONTCHECK_LDAPINFO_DN_RU(si) \
282 ((si)->bsql_flags & BSQLF_DONTCHECK_LDAPINFO_DN_RU)
283 #define BACKSQL_USE_REVERSE_DN(si) \
284 ((si)->bsql_flags & BSQLF_USE_REVERSE_DN)
286 struct berval strcast_func;
290 ldap_pvt_thread_mutex_t dbconn_mutex;
291 ldap_pvt_thread_mutex_t schema_mutex;
295 #define BACKSQL_SUCCESS( rc ) \
296 ( (rc) == SQL_SUCCESS || (rc) == SQL_SUCCESS_WITH_INFO )
298 #endif /* __BACKSQL_H__ */