]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/back-sql.h
cleanup error messages
[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  * Entry ID structure
98  */
99 typedef struct backsql_entryID {
100         unsigned long           id;
101         unsigned long           keyval;
102         unsigned long           oc_id;
103         struct berval           dn;
104         struct backsql_entryID  *next;
105 } backsql_entryID;
106
107 /*
108  * "structural" objectClass mapping structure
109  */
110 typedef struct backsql_oc_map_rec {
111         /*
112          * Structure of corresponding LDAP objectClass definition
113          */
114         ObjectClass     *bom_oc;
115 #define BACKSQL_OC_NAME(ocmap)  ((ocmap)->bom_oc->soc_cname.bv_val)
116         
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
122          * created row */
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;
131         Avlnode         *bom_attrs;
132 } backsql_oc_map_rec;
133
134 /*
135  * attributeType mapping structure
136  */
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 */
141         ObjectClass             *bam_oc;
142
143         struct berval   bam_from_tbls;
144         struct berval   bam_join_where;
145         struct berval   bam_sel_expr;
146
147         /* TimesTen, or, if a uppercase function is defined,
148          * an uppercased version of bam_sel_expr */
149         struct berval   bam_sel_expr_u;
150
151         /* supposed to expect 2 binded values: entry keyval 
152          * and attr. value to add, like "add_name(?,?,?)" */
153         char            *bam_add_proc;
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 */
159         char            *bam_query;
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" */
164         int             bam_param_order;
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;
169
170         /* next mapping for attribute */
171         struct backsql_at_map_rec       *bam_next;
172 } backsql_at_map_rec;
173
174 #define BACKSQL_AT_MAP_REC_INIT { NULL, NULL, BER_BVC(""), BER_BVC(""), BER_BVNULL, BER_BVNULL, NULL, NULL, NULL, 0, 0, NULL }
175
176 /* define to uppercase filters only if the matching rule requires it
177  * (currently broken) */
178 /* #define      BACKSQL_UPPERCASE_FILTER */
179
180 #define BACKSQL_AT_CANUPPERCASE(at)     ((at)->bam_sel_expr_u.bv_val)
181
182 /* defines to support bitmasks above */
183 #define BACKSQL_ADD     0x1
184 #define BACKSQL_DEL     0x2
185
186 #define BACKSQL_IS_ADD(x)       ( BACKSQL_ADD & (x) )
187 #define BACKSQL_IS_DEL(x)       ( BACKSQL_DEL & (x) )
188
189 #define BACKSQL_NCMP(v1,v2)     ber_bvcmp((v1),(v2))
190
191 #define BACKSQL_CONCAT
192 /*
193  * berbuf structure: a berval with a buffer size associated
194  */
195 typedef struct berbuf {
196         struct berval   bb_val;
197         ber_len_t       bb_len;
198 } BerBuffer;
199
200 #define BB_NULL         { { 0, NULL }, 0 }
201
202 typedef struct backsql_srch_info {
203         Operation               *bsi_op;
204
205         int                     bsi_flags;
206 #define BSQL_SF_ALL_OPER                0x0001
207 #define BSQL_SF_FILTER_HASSUBORDINATE   0x0002
208
209         struct berval           *bsi_base_dn;
210         int                     bsi_scope;
211         Filter                  *bsi_filter;
212         int                     bsi_slimit,
213                                 bsi_tlimit;
214         time_t                  bsi_stoptime;
215
216         backsql_entryID         *bsi_id_list,
217                                 *bsi_c_eid;
218         int                     bsi_n_candidates;
219         int                     bsi_abandon;
220         int                     bsi_status;
221
222         backsql_oc_map_rec      *bsi_oc;
223         struct berbuf           bsi_sel,
224                                 bsi_from,
225                                 bsi_join_where,
226                                 bsi_flt_where;
227         ObjectClass             *bsi_filter_oc;
228         SQLHDBC                 bsi_dbh;
229         AttributeName           *bsi_attrs;
230
231         Entry                   *bsi_e;
232 } backsql_srch_info;
233
234 /*
235  * Backend private data structure
236  */
237 typedef struct {
238         char            *dbhost;
239         int             dbport;
240         char            *dbuser;
241         char            *dbpasswd;
242         char            *dbname;
243         /*
244          * SQL condition for subtree searches differs in syntax:
245          * "LIKE CONCAT('%',?)" or "LIKE '%'+?" or "LIKE '%'||?"
246          * or smth else 
247          */
248         struct berval   subtree_cond;
249         struct berval   children_cond;
250         char            *oc_query, *at_query;
251         char            *insentry_query,*delentry_query;
252         char            *id_query;
253         char            *has_children_query;
254
255         MatchingRule    *bi_caseIgnoreMatch;
256
257         struct berval   upper_func;
258         struct berval   upper_func_open;
259         struct berval   upper_func_close;
260         BerVarray       concat_func;
261
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
270
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)
285         
286         struct berval   strcast_func;
287         Avlnode         *db_conns;
288         Avlnode         *oc_by_oc;
289         Avlnode         *oc_by_id;
290         ldap_pvt_thread_mutex_t         dbconn_mutex;
291         ldap_pvt_thread_mutex_t         schema_mutex;
292         SQLHENV         db_env;
293 } backsql_info;
294
295 #define BACKSQL_SUCCESS( rc ) \
296         ( (rc) == SQL_SUCCESS || (rc) == SQL_SUCCESS_WITH_INFO )
297
298 #endif /* __BACKSQL_H__ */
299
300
301