]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/back-sql.h
write test is almost working for ibmdb2 as well...
[openldap] / servers / slapd / back-sql / back-sql.h
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2004 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
6  * Portions Copyright 2002 Pierangelo Mararati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
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>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Dmitry Kovalev for inclusion
19  * by OpenLDAP Software.  Additional significant contributors include
20  *    Pierangelo Mararati
21  */
22
23 /*
24  * The following changes have been addressed:
25  *       
26  * Enhancements:
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
31  *     mapping lookup
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
59  * 
60  * Todo:
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
64  *     in dn match)
65  *   - implement a backsql_normalize() function to replace the upper()
66  *     conversion routines
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?)
72  */
73
74 #ifndef __BACKSQL_H__
75 #define __BACKSQL_H__
76
77 #include "external.h"
78 #include "sql-types.h"
79
80 /*
81  * PostgreSQL 7.0 doesn't work without :(
82  */
83 #define BACKSQL_REALLOC_STMT
84
85 /*
86  * Better use the standard length of 8192 (as of slap.h)?
87  */
88 /* #define BACKSQL_MAX_DN_LEN   SLAP_LDAPDN_MAXLEN */
89 #define BACKSQL_MAX_DN_LEN      255
90
91 /*
92  * define to enable very extensive trace logging (debug only)
93  */
94 #undef BACKSQL_TRACE
95
96 /*
97  * define to enable varchars as unique keys in user tables
98  */
99 #undef BACKSQL_ARBITRARY_KEY
100
101 /*
102  * API
103  */
104 typedef struct backsql_api {
105         char                    *ba_name;
106         int                     (*ba_dn2odbc)( Operation *op, SlapReply *rs, struct berval *dn );
107         int                     (*ba_odbc2dn)( Operation *op, SlapReply *rs, struct berval *dn );
108         struct backsql_api *ba_next;
109 } backsql_api;
110
111 /*
112  * Entry ID structure
113  */
114 typedef struct backsql_entryID {
115         /* #define BACKSQL_ARBITRARY_KEY to allow a non-numeric key.
116          * It is required by some special applications that use
117          * strings as keys for the main table.
118          * In this case, #define BACKSQL_MAX_KEY_LEN consistently
119          * with the key size definition */
120 #ifdef BACKSQL_ARBITRARY_KEY
121         struct berval           eid_id;
122         struct berval           eid_keyval;
123 #define BACKSQL_MAX_KEY_LEN     64
124 #else /* ! BACKSQL_ARBITRARY_KEY */
125         /* The original numeric key is maintained as default. */
126         unsigned long           eid_id;
127         unsigned long           eid_keyval;
128 #endif /* ! BACKSQL_ARBITRARY_KEY */
129
130         unsigned long           eid_oc_id;
131         struct berval           eid_dn;
132         struct backsql_entryID  *eid_next;
133 } backsql_entryID;
134
135 #ifdef BACKSQL_ARBITRARY_KEY
136 #define BACKSQL_ENTRYID_INIT { BER_BVNULL, BER_BVNULL, 0, BER_BVNULL, NULL }
137 #else /* ! BACKSQL_ARBITRARY_KEY */
138 #define BACKSQL_ENTRYID_INIT { 0, 0, 0, BER_BVNULL, NULL }
139 #endif /* BACKSQL_ARBITRARY_KEY */
140
141 /*
142  * "structural" objectClass mapping structure
143  */
144 typedef struct backsql_oc_map_rec {
145         /*
146          * Structure of corresponding LDAP objectClass definition
147          */
148         ObjectClass     *bom_oc;
149 #define BACKSQL_OC_NAME(ocmap)  ((ocmap)->bom_oc->soc_cname.bv_val)
150         
151         struct berval   bom_keytbl;
152         struct berval   bom_keycol;
153         /* expected to return keyval of newly created entry */
154         char            *bom_create_proc;
155         /* in case create_proc does not return the keyval of the newly
156          * created row */
157         char            *bom_create_keyval;
158         /* supposed to expect keyval as parameter and delete 
159          * all the attributes as well */
160         char            *bom_delete_proc;
161         /* flags whether delete_proc is a function (whether back-sql 
162          * should bind first parameter as output for return code) */
163         int             bom_expect_return;
164         unsigned long   bom_id;
165         Avlnode         *bom_attrs;
166 } backsql_oc_map_rec;
167
168 /*
169  * attributeType mapping structure
170  */
171 typedef struct backsql_at_map_rec {
172         /* Description of corresponding LDAP attribute type */
173         AttributeDescription    *bam_ad;
174         /* ObjectClass if bam_ad is objectClass */
175         ObjectClass             *bam_oc;
176
177         struct berval   bam_from_tbls;
178         struct berval   bam_join_where;
179         struct berval   bam_sel_expr;
180
181         /* TimesTen, or, if a uppercase function is defined,
182          * an uppercased version of bam_sel_expr */
183         struct berval   bam_sel_expr_u;
184
185         /* supposed to expect 2 binded values: entry keyval 
186          * and attr. value to add, like "add_name(?,?,?)" */
187         char            *bam_add_proc;
188         /* supposed to expect 2 binded values: entry keyval 
189          * and attr. value to delete */
190         char            *bam_delete_proc;
191         /* for optimization purposes attribute load query 
192          * is preconstructed from parts on schemamap load time */
193         char            *bam_query;
194         /* following flags are bitmasks (first bit used for add_proc, 
195          * second - for delete_proc) */
196         /* order of parameters for procedures above; 
197          * 1 means "data then keyval", 0 means "keyval then data" */
198         int             bam_param_order;
199         /* flags whether one or more of procedures is a function 
200          * (whether back-sql should bind first parameter as output 
201          * for return code) */
202         int             bam_expect_return;
203
204         /* next mapping for attribute */
205         struct backsql_at_map_rec       *bam_next;
206 } backsql_at_map_rec;
207
208 #define BACKSQL_AT_MAP_REC_INIT { NULL, NULL, BER_BVC(""), BER_BVC(""), BER_BVNULL, BER_BVNULL, NULL, NULL, NULL, 0, 0, NULL }
209
210 /* define to uppercase filters only if the matching rule requires it
211  * (currently broken) */
212 /* #define      BACKSQL_UPPERCASE_FILTER */
213
214 #define BACKSQL_AT_CANUPPERCASE(at)     ((at)->bam_sel_expr_u.bv_val)
215
216 /* defines to support bitmasks above */
217 #define BACKSQL_ADD     0x1
218 #define BACKSQL_DEL     0x2
219
220 #define BACKSQL_IS_ADD(x)       ( BACKSQL_ADD & (x) )
221 #define BACKSQL_IS_DEL(x)       ( BACKSQL_DEL & (x) )
222
223 #define BACKSQL_NCMP(v1,v2)     ber_bvcmp((v1),(v2))
224
225 #define BACKSQL_CONCAT
226 /*
227  * berbuf structure: a berval with a buffer size associated
228  */
229 typedef struct berbuf {
230         struct berval   bb_val;
231         ber_len_t       bb_len;
232 } BerBuffer;
233
234 #define BB_NULL         { { 0, NULL }, 0 }
235
236 typedef struct backsql_srch_info {
237         Operation               *bsi_op;
238         SlapReply               *bsi_rs;
239
240         int                     bsi_flags;
241 #define BSQL_SF_ALL_OPER                0x0001
242 #define BSQL_SF_FILTER_HASSUBORDINATE   0x0002
243
244         struct berval           *bsi_base_dn;
245         int                     bsi_scope;
246 #define BACKSQL_SCOPE_BASE_LIKE         ( LDAP_SCOPE_BASE | 0x1000 )
247         Filter                  *bsi_filter;
248         int                     bsi_slimit,
249                                 bsi_tlimit;
250         time_t                  bsi_stoptime;
251
252         backsql_entryID         *bsi_id_list,
253                                 **bsi_id_listtail,
254                                 *bsi_c_eid;
255         int                     bsi_n_candidates;
256         int                     bsi_abandon;
257         int                     bsi_status;
258
259         backsql_oc_map_rec      *bsi_oc;
260         struct berbuf           bsi_sel,
261                                 bsi_from,
262                                 bsi_join_where,
263                                 bsi_flt_where;
264         ObjectClass             *bsi_filter_oc;
265         SQLHDBC                 bsi_dbh;
266         AttributeName           *bsi_attrs;
267
268         Entry                   *bsi_e;
269 } backsql_srch_info;
270
271 /*
272  * Backend private data structure
273  */
274 typedef struct {
275         char            *dbhost;
276         int             dbport;
277         char            *dbuser;
278         char            *dbpasswd;
279         char            *dbname;
280         /*
281          * SQL condition for subtree searches differs in syntax:
282          * "LIKE CONCAT('%',?)" or "LIKE '%'+?" or "LIKE '%'||?"
283          * or smth else 
284          */
285         struct berval   subtree_cond;
286         struct berval   children_cond;
287         char            *oc_query, *at_query;
288         char            *insentry_query,
289                         *delentry_query,
290                         *delobjclasses_query,
291                         *delreferrals_query;
292         char            *id_query;
293         char            *has_children_query;
294
295         MatchingRule    *bi_caseIgnoreMatch;
296         MatchingRule    *bi_telephoneNumberMatch;
297
298         struct berval   upper_func;
299         struct berval   upper_func_open;
300         struct berval   upper_func_close;
301         BerVarray       concat_func;
302
303         unsigned int    bsql_flags;
304 #define BSQLF_SCHEMA_LOADED             0x0001
305 #define BSQLF_UPPER_NEEDS_CAST          0x0002
306 #define BSQLF_CREATE_NEEDS_SELECT       0x0004
307 #define BSQLF_FAIL_IF_NO_MAPPING        0x0008
308 #define BSQLF_HAS_LDAPINFO_DN_RU        0x0010
309 #define BSQLF_DONTCHECK_LDAPINFO_DN_RU  0x0020
310 #define BSQLF_USE_REVERSE_DN            0x0040
311
312 #define BACKSQL_SCHEMA_LOADED(si) \
313         ((si)->bsql_flags & BSQLF_SCHEMA_LOADED)
314 #define BACKSQL_UPPER_NEEDS_CAST(si) \
315         ((si)->bsql_flags & BSQLF_UPPER_NEEDS_CAST)
316 #define BACKSQL_CREATE_NEEDS_SELECT(si) \
317         ((si)->bsql_flags & BSQLF_CREATE_NEEDS_SELECT)
318 #define BACKSQL_FAIL_IF_NO_MAPPING(si) \
319         ((si)->bsql_flags & BSQLF_FAIL_IF_NO_MAPPING)
320 #define BACKSQL_HAS_LDAPINFO_DN_RU(si) \
321         ((si)->bsql_flags & BSQLF_HAS_LDAPINFO_DN_RU)
322 #define BACKSQL_DONTCHECK_LDAPINFO_DN_RU(si) \
323         ((si)->bsql_flags & BSQLF_DONTCHECK_LDAPINFO_DN_RU)
324 #define BACKSQL_USE_REVERSE_DN(si) \
325         ((si)->bsql_flags & BSQLF_USE_REVERSE_DN)
326 #define BACKSQL_CANUPPERCASE(si) \
327         ((si)->upper_func.bv_val)
328         
329         struct berval   strcast_func;
330         Avlnode         *db_conns;
331         Avlnode         *oc_by_oc;
332         Avlnode         *oc_by_id;
333         ldap_pvt_thread_mutex_t         dbconn_mutex;
334         ldap_pvt_thread_mutex_t         schema_mutex;
335         SQLHENV         db_env;
336
337         backsql_api     *si_api;
338 } backsql_info;
339
340 #define BACKSQL_SUCCESS( rc ) \
341         ( (rc) == SQL_SUCCESS || (rc) == SQL_SUCCESS_WITH_INFO )
342
343 #define BACKSQL_AVL_STOP                0
344 #define BACKSQL_AVL_CONTINUE            1
345
346 #endif /* __BACKSQL_H__ */
347
348
349