]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/back-sql.h
af8be8ceba199a6aff07ad5c38f3d274c405512c
[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 "sql-types.h"
78
79 /*
80  * Better use the standard length of 8192 (as of slap.h)?
81  *
82  * NOTE: must be consistent with definition in ldap_entries table
83  */
84 /* #define BACKSQL_MAX_DN_LEN   SLAP_LDAPDN_MAXLEN */
85 #define BACKSQL_MAX_DN_LEN      255
86
87 /*
88  * define to enable very extensive trace logging (debug only)
89  */
90 #undef BACKSQL_TRACE
91 #define BACKSQL_TRACE
92
93 /*
94  * define to enable varchars as unique keys in user tables
95  *
96  * by default integers are used (and recommended)
97  * for performances.  Integers are used anyway in back-sql
98  * related tables.
99  */
100 #undef BACKSQL_ARBITRARY_KEY
101
102 /*
103  * define to the appropriate aliasing string
104  *
105  * some RDBMSes tolerate (or require) that " AS " is not used
106  * when aliasing tables/columns
107  */
108 #define BACKSQL_ALIASING        "AS "
109 /* #define      BACKSQL_ALIASING        "" */
110
111 /*
112  * define to the appropriate quoting char
113  *
114  * some RDBMSes tolerate/require that the aliases be enclosed
115  * in quotes.  This is especially true for those that do not
116  * allow keywords used as aliases.
117  */
118 /* #define BACKSQL_ALIASING_QUOTE       '"' */
119 /* #define BACKSQL_ALIASING_QUOTE       '\'' */
120
121 /*
122  * API
123  *
124  * a simple mechanism to allow DN mucking between the LDAP
125  * and the stored string representation.
126  */
127 typedef struct backsql_api {
128         char                    *ba_name;
129         int                     (*ba_dn2odbc)( Operation *op, SlapReply *rs, struct berval *dn );
130         int                     (*ba_odbc2dn)( Operation *op, SlapReply *rs, struct berval *dn );
131         struct backsql_api *ba_next;
132 } backsql_api;
133
134 /*
135  * Entry ID structure
136  */
137 typedef struct backsql_entryID {
138         /* #define BACKSQL_ARBITRARY_KEY to allow a non-numeric key.
139          * It is required by some special applications that use
140          * strings as keys for the main table.
141          * In this case, #define BACKSQL_MAX_KEY_LEN consistently
142          * with the key size definition */
143 #ifdef BACKSQL_ARBITRARY_KEY
144         struct berval           eid_id;
145         struct berval           eid_keyval;
146 #define BACKSQL_MAX_KEY_LEN     64
147 #else /* ! BACKSQL_ARBITRARY_KEY */
148         /* The original numeric key is maintained as default. */
149         unsigned long           eid_id;
150         unsigned long           eid_keyval;
151 #endif /* ! BACKSQL_ARBITRARY_KEY */
152
153         unsigned long           eid_oc_id;
154         struct berval           eid_dn;
155         struct berval           eid_ndn;
156         struct backsql_entryID  *eid_next;
157 } backsql_entryID;
158
159 #ifdef BACKSQL_ARBITRARY_KEY
160 #define BACKSQL_ENTRYID_INIT { BER_BVNULL, BER_BVNULL, 0, BER_BVNULL, BER_BVNULL, NULL }
161 #else /* ! BACKSQL_ARBITRARY_KEY */
162 #define BACKSQL_ENTRYID_INIT { 0, 0, 0, BER_BVNULL, BER_BVNULL, NULL }
163 #endif /* BACKSQL_ARBITRARY_KEY */
164
165 /*
166  * "structural" objectClass mapping structure
167  */
168 typedef struct backsql_oc_map_rec {
169         /*
170          * Structure of corresponding LDAP objectClass definition
171          */
172         ObjectClass     *bom_oc;
173 #define BACKSQL_OC_NAME(ocmap)  ((ocmap)->bom_oc->soc_cname.bv_val)
174         
175         struct berval   bom_keytbl;
176         struct berval   bom_keycol;
177         /* expected to return keyval of newly created entry */
178         char            *bom_create_proc;
179         /* in case create_proc does not return the keyval of the newly
180          * created row */
181         char            *bom_create_keyval;
182         /* supposed to expect keyval as parameter and delete 
183          * all the attributes as well */
184         char            *bom_delete_proc;
185         /* flags whether delete_proc is a function (whether back-sql 
186          * should bind first parameter as output for return code) */
187         int             bom_expect_return;
188         unsigned long   bom_id;
189         Avlnode         *bom_attrs;
190 } backsql_oc_map_rec;
191
192 /*
193  * attributeType mapping structure
194  */
195 typedef struct backsql_at_map_rec {
196         /* Description of corresponding LDAP attribute type */
197         AttributeDescription    *bam_ad;
198         /* ObjectClass if bam_ad is objectClass */
199         ObjectClass             *bam_oc;
200
201         struct berval   bam_from_tbls;
202         struct berval   bam_join_where;
203         struct berval   bam_sel_expr;
204
205         /* TimesTen, or, if a uppercase function is defined,
206          * an uppercased version of bam_sel_expr */
207         struct berval   bam_sel_expr_u;
208
209         /* supposed to expect 2 binded values: entry keyval 
210          * and attr. value to add, like "add_name(?,?,?)" */
211         char            *bam_add_proc;
212         /* supposed to expect 2 binded values: entry keyval 
213          * and attr. value to delete */
214         char            *bam_delete_proc;
215         /* for optimization purposes attribute load query 
216          * is preconstructed from parts on schemamap load time */
217         char            *bam_query;
218         /* following flags are bitmasks (first bit used for add_proc, 
219          * second - for delete_proc) */
220         /* order of parameters for procedures above; 
221          * 1 means "data then keyval", 0 means "keyval then data" */
222         int             bam_param_order;
223         /* flags whether one or more of procedures is a function 
224          * (whether back-sql should bind first parameter as output 
225          * for return code) */
226         int             bam_expect_return;
227
228         /* next mapping for attribute */
229         struct backsql_at_map_rec       *bam_next;
230 } backsql_at_map_rec;
231
232 #define BACKSQL_AT_MAP_REC_INIT { NULL, NULL, BER_BVC(""), BER_BVC(""), BER_BVNULL, BER_BVNULL, NULL, NULL, NULL, 0, 0, NULL }
233
234 /* define to uppercase filters only if the matching rule requires it
235  * (currently broken) */
236 /* #define      BACKSQL_UPPERCASE_FILTER */
237
238 #define BACKSQL_AT_CANUPPERCASE(at)     ((at)->bam_sel_expr_u.bv_val)
239
240 /* defines to support bitmasks above */
241 #define BACKSQL_ADD     0x1
242 #define BACKSQL_DEL     0x2
243
244 #define BACKSQL_IS_ADD(x)       ( BACKSQL_ADD & (x) )
245 #define BACKSQL_IS_DEL(x)       ( BACKSQL_DEL & (x) )
246
247 #define BACKSQL_NCMP(v1,v2)     ber_bvcmp((v1),(v2))
248
249 #define BACKSQL_CONCAT
250 /*
251  * berbuf structure: a berval with a buffer size associated
252  */
253 typedef struct berbuf {
254         struct berval   bb_val;
255         ber_len_t       bb_len;
256 } BerBuffer;
257
258 #define BB_NULL         { { 0, NULL }, 0 }
259
260 typedef struct backsql_srch_info {
261         Operation               *bsi_op;
262         SlapReply               *bsi_rs;
263
264         unsigned                bsi_flags;
265 #define BSQL_SF_NONE                    0x0000U
266 #define BSQL_SF_ALL_OPER                0x0001U
267 #define BSQL_SF_FILTER_HASSUBORDINATE   0x0002U
268 #define BSQL_SF_FILTER_ENTRYUUID        0x0004U
269 #define BSQL_SF_FILTER_ENTRYCSN         0x0008U
270
271         struct berval           *bsi_base_ndn;
272         backsql_entryID         bsi_base_id;
273         int                     bsi_scope;
274 /* BACKSQL_SCOPE_BASE_LIKE can be set by API in ors_scope
275  * whenever the search base DN contains chars that cannot
276  * be mapped into the charset used in the RDBMS; so they're
277  * turned into '%' and an approximate ('LIKE') condition
278  * is used */
279 #define BACKSQL_SCOPE_BASE_LIKE         ( LDAP_SCOPE_BASE | 0x1000 )
280         Filter                  *bsi_filter;
281         int                     bsi_slimit,
282                                 bsi_tlimit;
283         time_t                  bsi_stoptime;
284
285         backsql_entryID         *bsi_id_list,
286                                 **bsi_id_listtail,
287                                 *bsi_c_eid;
288         int                     bsi_n_candidates;
289         int                     bsi_abandon;
290         int                     bsi_status;
291
292         backsql_oc_map_rec      *bsi_oc;
293         struct berbuf           bsi_sel,
294                                 bsi_from,
295                                 bsi_join_where,
296                                 bsi_flt_where;
297         ObjectClass             *bsi_filter_oc;
298         SQLHDBC                 bsi_dbh;
299         AttributeName           *bsi_attrs;
300
301         Entry                   *bsi_e;
302 } backsql_srch_info;
303
304 /*
305  * Backend private data structure
306  */
307 typedef struct {
308         char            *sql_dbhost;
309         int             sql_dbport;
310         char            *sql_dbuser;
311         char            *sql_dbpasswd;
312         char            *sql_dbname;
313
314         /*
315          * SQL condition for subtree searches differs in syntax:
316          * "LIKE CONCAT('%',?)" or "LIKE '%'+?" or "LIKE '%'||?"
317          * or smtg else 
318          */
319         struct berval   sql_subtree_cond;
320         struct berval   sql_children_cond;
321         char            *sql_oc_query,
322                         *sql_at_query;
323         char            *sql_insentry_query,
324                         *sql_delentry_query,
325                         *sql_delobjclasses_query,
326                         *sql_delreferrals_query;
327         char            *sql_id_query;
328         char            *sql_has_children_query;
329
330         MatchingRule    *sql_caseIgnoreMatch;
331         MatchingRule    *sql_telephoneNumberMatch;
332
333         struct berval   sql_upper_func;
334         struct berval   sql_upper_func_open;
335         struct berval   sql_upper_func_close;
336         BerVarray       sql_concat_func;
337
338         struct berval   sql_strcast_func;
339
340         unsigned int    sql_flags;
341 #define BSQLF_SCHEMA_LOADED             0x0001
342 #define BSQLF_UPPER_NEEDS_CAST          0x0002
343 #define BSQLF_CREATE_NEEDS_SELECT       0x0004
344 #define BSQLF_FAIL_IF_NO_MAPPING        0x0008
345 #define BSQLF_HAS_LDAPINFO_DN_RU        0x0010
346 #define BSQLF_DONTCHECK_LDAPINFO_DN_RU  0x0020
347 #define BSQLF_USE_REVERSE_DN            0x0040
348 #define BSQLF_ALLOW_ORPHANS             0x0080
349
350 #define BACKSQL_SCHEMA_LOADED(si) \
351         ((si)->sql_flags & BSQLF_SCHEMA_LOADED)
352 #define BACKSQL_UPPER_NEEDS_CAST(si) \
353         ((si)->sql_flags & BSQLF_UPPER_NEEDS_CAST)
354 #define BACKSQL_CREATE_NEEDS_SELECT(si) \
355         ((si)->sql_flags & BSQLF_CREATE_NEEDS_SELECT)
356 #define BACKSQL_FAIL_IF_NO_MAPPING(si) \
357         ((si)->sql_flags & BSQLF_FAIL_IF_NO_MAPPING)
358 #define BACKSQL_HAS_LDAPINFO_DN_RU(si) \
359         ((si)->sql_flags & BSQLF_HAS_LDAPINFO_DN_RU)
360 #define BACKSQL_DONTCHECK_LDAPINFO_DN_RU(si) \
361         ((si)->sql_flags & BSQLF_DONTCHECK_LDAPINFO_DN_RU)
362 #define BACKSQL_USE_REVERSE_DN(si) \
363         ((si)->sql_flags & BSQLF_USE_REVERSE_DN)
364 #define BACKSQL_CANUPPERCASE(si) \
365         (!BER_BVISNULL( &(si)->sql_upper_func ))
366 #define BACKSQL_ALLOW_ORPHANS(si) \
367         ((si)->sql_flags & BSQLF_ALLOW_ORPHANS)
368
369         Entry           *sql_baseObject;
370 #ifdef BACKSQL_ARBITRARY_KEY
371 #define BACKSQL_BASEOBJECT_IDSTR        "baseObject"
372 #define BACKSQL_BASEOBJECT_KEYVAL       BACKSQL_BASEOBJECT_IDSTR
373 #define BACKSQL_IS_BASEOBJECT_ID(id)    (bvmatch((id), &backsql_baseObject_bv))
374 #else /* ! BACKSQL_ARBITRARY_KEY */
375 #define BACKSQL_BASEOBJECT_ID           0
376 #define BACKSQL_BASEOBJECT_IDSTR        "0"
377 #define BACKSQL_BASEOBJECT_KEYVAL       0
378 #define BACKSQL_IS_BASEOBJECT_ID(id)    (*(id) == BACKSQL_BASEOBJECT_ID)
379 #endif /* ! BACKSQL_ARBITRARY_KEY */
380 #define BACKSQL_BASEOBJECT_OC           0
381         
382         Avlnode         *sql_db_conns;
383         Avlnode         *sql_oc_by_oc;
384         Avlnode         *sql_oc_by_id;
385         ldap_pvt_thread_mutex_t         sql_dbconn_mutex;
386         ldap_pvt_thread_mutex_t         sql_schema_mutex;
387         SQLHENV         sql_db_env;
388
389         backsql_api     *sql_api;
390 } backsql_info;
391
392 #define BACKSQL_SUCCESS( rc ) \
393         ( (rc) == SQL_SUCCESS || (rc) == SQL_SUCCESS_WITH_INFO )
394
395 #define BACKSQL_AVL_STOP                0
396 #define BACKSQL_AVL_CONTINUE            1
397
398 #endif /* __BACKSQL_H__ */
399
400
401