]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/back-sql.h
fix, rework and optimize search base; allow orphaned entries addition
[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 #define BACKSQL_TRACE
96
97 /*
98  * define to enable varchars as unique keys in user tables
99  */
100 #undef BACKSQL_ARBITRARY_KEY
101
102 /*
103  * define to the appropriate aliasing string
104  */
105 #define BACKSQL_ALIASING        "AS "
106 /* #define      BACKSQL_ALIASING        "" */
107
108 /*
109  * define to the appropriate quoting char
110  */
111 /* #define BACKSQL_ALIASING_QUOTE       '"' */
112 /* #define BACKSQL_ALIASING_QUOTE       '\'' */
113
114 /*
115  * API
116  */
117 typedef struct backsql_api {
118         char                    *ba_name;
119         int                     (*ba_dn2odbc)( Operation *op, SlapReply *rs, struct berval *dn );
120         int                     (*ba_odbc2dn)( Operation *op, SlapReply *rs, struct berval *dn );
121         struct backsql_api *ba_next;
122 } backsql_api;
123
124 /*
125  * Entry ID structure
126  */
127 typedef struct backsql_entryID {
128         /* #define BACKSQL_ARBITRARY_KEY to allow a non-numeric key.
129          * It is required by some special applications that use
130          * strings as keys for the main table.
131          * In this case, #define BACKSQL_MAX_KEY_LEN consistently
132          * with the key size definition */
133 #ifdef BACKSQL_ARBITRARY_KEY
134         struct berval           eid_id;
135         struct berval           eid_keyval;
136 #define BACKSQL_MAX_KEY_LEN     64
137 #else /* ! BACKSQL_ARBITRARY_KEY */
138         /* The original numeric key is maintained as default. */
139         unsigned long           eid_id;
140         unsigned long           eid_keyval;
141 #endif /* ! BACKSQL_ARBITRARY_KEY */
142
143         unsigned long           eid_oc_id;
144         struct berval           eid_dn;
145         struct backsql_entryID  *eid_next;
146 } backsql_entryID;
147
148 #ifdef BACKSQL_ARBITRARY_KEY
149 #define BACKSQL_ENTRYID_INIT { BER_BVNULL, BER_BVNULL, 0, BER_BVNULL, NULL }
150 #else /* ! BACKSQL_ARBITRARY_KEY */
151 #define BACKSQL_ENTRYID_INIT { 0, 0, 0, BER_BVNULL, NULL }
152 #endif /* BACKSQL_ARBITRARY_KEY */
153
154 /*
155  * "structural" objectClass mapping structure
156  */
157 typedef struct backsql_oc_map_rec {
158         /*
159          * Structure of corresponding LDAP objectClass definition
160          */
161         ObjectClass     *bom_oc;
162 #define BACKSQL_OC_NAME(ocmap)  ((ocmap)->bom_oc->soc_cname.bv_val)
163         
164         struct berval   bom_keytbl;
165         struct berval   bom_keycol;
166         /* expected to return keyval of newly created entry */
167         char            *bom_create_proc;
168         /* in case create_proc does not return the keyval of the newly
169          * created row */
170         char            *bom_create_keyval;
171         /* supposed to expect keyval as parameter and delete 
172          * all the attributes as well */
173         char            *bom_delete_proc;
174         /* flags whether delete_proc is a function (whether back-sql 
175          * should bind first parameter as output for return code) */
176         int             bom_expect_return;
177         unsigned long   bom_id;
178         Avlnode         *bom_attrs;
179 } backsql_oc_map_rec;
180
181 /*
182  * attributeType mapping structure
183  */
184 typedef struct backsql_at_map_rec {
185         /* Description of corresponding LDAP attribute type */
186         AttributeDescription    *bam_ad;
187         /* ObjectClass if bam_ad is objectClass */
188         ObjectClass             *bam_oc;
189
190         struct berval   bam_from_tbls;
191         struct berval   bam_join_where;
192         struct berval   bam_sel_expr;
193
194         /* TimesTen, or, if a uppercase function is defined,
195          * an uppercased version of bam_sel_expr */
196         struct berval   bam_sel_expr_u;
197
198         /* supposed to expect 2 binded values: entry keyval 
199          * and attr. value to add, like "add_name(?,?,?)" */
200         char            *bam_add_proc;
201         /* supposed to expect 2 binded values: entry keyval 
202          * and attr. value to delete */
203         char            *bam_delete_proc;
204         /* for optimization purposes attribute load query 
205          * is preconstructed from parts on schemamap load time */
206         char            *bam_query;
207         /* following flags are bitmasks (first bit used for add_proc, 
208          * second - for delete_proc) */
209         /* order of parameters for procedures above; 
210          * 1 means "data then keyval", 0 means "keyval then data" */
211         int             bam_param_order;
212         /* flags whether one or more of procedures is a function 
213          * (whether back-sql should bind first parameter as output 
214          * for return code) */
215         int             bam_expect_return;
216
217         /* next mapping for attribute */
218         struct backsql_at_map_rec       *bam_next;
219 } backsql_at_map_rec;
220
221 #define BACKSQL_AT_MAP_REC_INIT { NULL, NULL, BER_BVC(""), BER_BVC(""), BER_BVNULL, BER_BVNULL, NULL, NULL, NULL, 0, 0, NULL }
222
223 /* define to uppercase filters only if the matching rule requires it
224  * (currently broken) */
225 /* #define      BACKSQL_UPPERCASE_FILTER */
226
227 #define BACKSQL_AT_CANUPPERCASE(at)     ((at)->bam_sel_expr_u.bv_val)
228
229 /* defines to support bitmasks above */
230 #define BACKSQL_ADD     0x1
231 #define BACKSQL_DEL     0x2
232
233 #define BACKSQL_IS_ADD(x)       ( BACKSQL_ADD & (x) )
234 #define BACKSQL_IS_DEL(x)       ( BACKSQL_DEL & (x) )
235
236 #define BACKSQL_NCMP(v1,v2)     ber_bvcmp((v1),(v2))
237
238 #define BACKSQL_CONCAT
239 /*
240  * berbuf structure: a berval with a buffer size associated
241  */
242 typedef struct berbuf {
243         struct berval   bb_val;
244         ber_len_t       bb_len;
245 } BerBuffer;
246
247 #define BB_NULL         { { 0, NULL }, 0 }
248
249 typedef struct backsql_srch_info {
250         Operation               *bsi_op;
251         SlapReply               *bsi_rs;
252
253         int                     bsi_flags;
254 #define BSQL_SF_ALL_OPER                0x0001
255 #define BSQL_SF_FILTER_HASSUBORDINATE   0x0002
256
257         struct berval           *bsi_base_dn;
258         backsql_entryID         bsi_base_id;
259         int                     bsi_scope;
260 #define BACKSQL_SCOPE_BASE_LIKE         ( LDAP_SCOPE_BASE | 0x1000 )
261         Filter                  *bsi_filter;
262         int                     bsi_slimit,
263                                 bsi_tlimit;
264         time_t                  bsi_stoptime;
265
266         backsql_entryID         *bsi_id_list,
267                                 **bsi_id_listtail,
268                                 *bsi_c_eid;
269         int                     bsi_n_candidates;
270         int                     bsi_abandon;
271         int                     bsi_status;
272
273         backsql_oc_map_rec      *bsi_oc;
274         struct berbuf           bsi_sel,
275                                 bsi_from,
276                                 bsi_join_where,
277                                 bsi_flt_where;
278         ObjectClass             *bsi_filter_oc;
279         SQLHDBC                 bsi_dbh;
280         AttributeName           *bsi_attrs;
281
282         Entry                   *bsi_e;
283 } backsql_srch_info;
284
285 /*
286  * Backend private data structure
287  */
288 typedef struct {
289         char            *dbhost;
290         int             dbport;
291         char            *dbuser;
292         char            *dbpasswd;
293         char            *dbname;
294         /*
295          * SQL condition for subtree searches differs in syntax:
296          * "LIKE CONCAT('%',?)" or "LIKE '%'+?" or "LIKE '%'||?"
297          * or smth else 
298          */
299         struct berval   subtree_cond;
300         struct berval   children_cond;
301         char            *oc_query, *at_query;
302         char            *insentry_query,
303                         *delentry_query,
304                         *delobjclasses_query,
305                         *delreferrals_query;
306         char            *id_query;
307         char            *has_children_query;
308
309         MatchingRule    *bi_caseIgnoreMatch;
310         MatchingRule    *bi_telephoneNumberMatch;
311
312         struct berval   upper_func;
313         struct berval   upper_func_open;
314         struct berval   upper_func_close;
315         BerVarray       concat_func;
316
317         unsigned int    bsql_flags;
318 #define BSQLF_SCHEMA_LOADED             0x0001
319 #define BSQLF_UPPER_NEEDS_CAST          0x0002
320 #define BSQLF_CREATE_NEEDS_SELECT       0x0004
321 #define BSQLF_FAIL_IF_NO_MAPPING        0x0008
322 #define BSQLF_HAS_LDAPINFO_DN_RU        0x0010
323 #define BSQLF_DONTCHECK_LDAPINFO_DN_RU  0x0020
324 #define BSQLF_USE_REVERSE_DN            0x0040
325 #define BSQLF_ALLOW_ORPHANS             0x0080
326
327 #define BACKSQL_SCHEMA_LOADED(si) \
328         ((si)->bsql_flags & BSQLF_SCHEMA_LOADED)
329 #define BACKSQL_UPPER_NEEDS_CAST(si) \
330         ((si)->bsql_flags & BSQLF_UPPER_NEEDS_CAST)
331 #define BACKSQL_CREATE_NEEDS_SELECT(si) \
332         ((si)->bsql_flags & BSQLF_CREATE_NEEDS_SELECT)
333 #define BACKSQL_FAIL_IF_NO_MAPPING(si) \
334         ((si)->bsql_flags & BSQLF_FAIL_IF_NO_MAPPING)
335 #define BACKSQL_HAS_LDAPINFO_DN_RU(si) \
336         ((si)->bsql_flags & BSQLF_HAS_LDAPINFO_DN_RU)
337 #define BACKSQL_DONTCHECK_LDAPINFO_DN_RU(si) \
338         ((si)->bsql_flags & BSQLF_DONTCHECK_LDAPINFO_DN_RU)
339 #define BACKSQL_USE_REVERSE_DN(si) \
340         ((si)->bsql_flags & BSQLF_USE_REVERSE_DN)
341 #define BACKSQL_CANUPPERCASE(si) \
342         ((si)->upper_func.bv_val)
343 #define BACKSQL_ALLOW_ORPHANS(si) \
344         ((si)->bsql_flags & BSQLF_ALLOW_ORPHANS)
345         
346         struct berval   strcast_func;
347         Avlnode         *db_conns;
348         Avlnode         *oc_by_oc;
349         Avlnode         *oc_by_id;
350         ldap_pvt_thread_mutex_t         dbconn_mutex;
351         ldap_pvt_thread_mutex_t         schema_mutex;
352         SQLHENV         db_env;
353
354         backsql_api     *si_api;
355 } backsql_info;
356
357 #define BACKSQL_SUCCESS( rc ) \
358         ( (rc) == SQL_SUCCESS || (rc) == SQL_SUCCESS_WITH_INFO )
359
360 #define BACKSQL_AVL_STOP                0
361 #define BACKSQL_AVL_CONTINUE            1
362
363 #endif /* __BACKSQL_H__ */
364
365
366