]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/proto-sql.h
more on DN munging; now works for all operations
[openldap] / servers / slapd / back-sql / proto-sql.h
1 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
2  *
3  * Copyright 1999-2004 The OpenLDAP Foundation.
4  * Portions Copyright 1999 Dmitry Kovalev.
5  * Portions Copyright 2002 Pierangelo Mararati.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Dmitry Kovalev for inclusion
18  * by OpenLDAP Software.  Additional significant contributors include
19  *    Pierangelo Mararati
20  */
21
22 /*
23  * The following changes have been addressed:
24  *       
25  * Enhancements:
26  *   - re-styled code for better readability
27  *   - upgraded backend API to reflect recent changes
28  *   - LDAP schema is checked when loading SQL/LDAP mapping
29  *   - AttributeDescription/ObjectClass pointers used for more efficient
30  *     mapping lookup
31  *   - bervals used where string length is required often
32  *   - atomized write operations by committing at the end of each operation
33  *     and defaulting connection closure to rollback
34  *   - added LDAP access control to write operations
35  *   - fully implemented modrdn (with rdn attrs change, deleteoldrdn,
36  *     access check, parent/children check and more)
37  *   - added parent access control, children control to delete operation
38  *   - added structuralObjectClass operational attribute check and
39  *     value return on search
40  *   - added hasSubordinate operational attribute on demand
41  *   - search limits are appropriately enforced
42  *   - function backsql_strcat() has been made more efficient
43  *   - concat function has been made configurable by means of a pattern
44  *   - added config switches:
45  *       - fail_if_no_mapping   write operations fail if there is no mapping
46  *       - has_ldapinfo_dn_ru   overrides autodetect
47  *       - concat_pattern       a string containing two '?' is used
48  *                              (note that "?||?" should be more portable
49  *                              than builtin function "CONCAT(?,?)")
50  *       - strcast_func         cast of string constants in "SELECT DISTINCT
51  *                              statements (needed by PostgreSQL)
52  *       - upper_needs_cast     cast the argument of upper when required
53  *                              (basically when building dn substring queries)
54  *   - added noop control
55  *   - added values return filter control
56  *   - hasSubordinate can be used in search filters (with limitations)
57  *   - eliminated oc->name; use oc->oc->soc_cname instead
58  * 
59  * Todo:
60  *   - add security checks for SQL statements that can be injected (?)
61  *   - re-test with previously supported RDBMs
62  *   - replace dn_ru and so with normalized dn (no need for upper() and so
63  *     in dn match)
64  *   - implement a backsql_normalize() function to replace the upper()
65  *     conversion routines
66  *   - note that subtree deletion, subtree renaming and so could be easily
67  *     implemented (rollback and consistency checks are available :)
68  *   - implement "lastmod" and other operational stuff (ldap_entries table ?)
69  *   - check how to allow multiple operations with one statement, to remove
70  *     BACKSQL_REALLOC_STMT from modify.c (a more recent unixODBC lib?)
71  */
72
73 #ifndef PROTO_SQL_H
74 #define PROTO_SQL_H
75
76 #include "back-sql.h"
77 #include "sql-types.h"
78
79 /*
80  * add.c
81  */
82 int backsql_modify_delete_all_values(
83         Operation               *op,
84         SlapReply               *rs,
85         SQLHDBC                 dbh, 
86         backsql_entryID         *e_id,
87         backsql_at_map_rec      *at );
88
89 int backsql_modify_internal(
90         Operation               *op,
91         SlapReply               *rs,
92         SQLHDBC                 dbh, 
93         backsql_oc_map_rec      *oc,
94         backsql_entryID         *e_id,
95         Modifications           *modlist );
96
97 /*
98  * api.c
99  */
100 int backsql_api_config( backsql_info *si, const char *name );
101 int backsql_api_register( backsql_api *ba );
102 int backsql_api_dn2odbc( Operation *op, SlapReply *rs, struct berval *dn );
103 int backsql_api_odbc2dn( Operation *op, SlapReply *rs, struct berval *dn );
104
105 /*
106  * entry-id.c
107  */
108 #ifdef BACKSQL_ARBITRARY_KEY
109 extern struct berval    backsql_baseObject_bv;
110 #endif /* BACKSQL_ARBITRARY_KEY */
111
112 /* stores in *id the ID in table ldap_entries corresponding to DN, if any */
113 int backsql_dn2id( Operation *op, SlapReply *rs, backsql_entryID *id,
114                 SQLHDBC dbh, struct berval *dn, int muck );
115
116 /* stores in *nchildren the count of children for an entry */
117 int backsql_count_children( backsql_info *bi, SQLHDBC dbh,
118                 struct berval *dn, unsigned long *nchildren );
119
120 /* returns LDAP_COMPARE_TRUE/LDAP_COMPARE_FALSE if the entry corresponding
121  * to DN has/has not children */
122 int backsql_has_children( backsql_info *bi, SQLHDBC dbh, struct berval *dn );
123
124 /* frees *id and returns next in list */
125 backsql_entryID *backsql_free_entryID( backsql_entryID *id, int freeit );
126
127 /* turns an ID into an entry */
128 int backsql_id2entry( backsql_srch_info *bsi, backsql_entryID *id );
129
130 /*
131  * operational.c
132  */
133
134 Attribute *backsql_operational_entryUUID( backsql_info *bi, backsql_entryID *id );
135
136 Attribute *backsql_operational_entryCSN( Operation *op );
137
138 /*
139  * schema-map.c
140  */
141
142 int backsql_load_schema_map( backsql_info *si, SQLHDBC dbh );
143
144 backsql_oc_map_rec *backsql_oc2oc( backsql_info *si, ObjectClass *oc );
145
146 backsql_oc_map_rec *backsql_id2oc( backsql_info *si, unsigned long id );
147
148 backsql_oc_map_rec * backsql_name2oc( backsql_info *si,
149                 struct berval *oc_name );
150
151 backsql_at_map_rec *backsql_ad2at( backsql_oc_map_rec *objclass,
152                 AttributeDescription *ad );
153
154 int backsql_supad2at( backsql_oc_map_rec *objclass,
155                 AttributeDescription *supad, backsql_at_map_rec ***pret );
156
157 int backsql_destroy_schema_map( backsql_info *si );
158
159 /*
160  * search.c
161  */
162
163 #define BACKSQL_ISF_GET_ID      0x1U
164 #define BACKSQL_ISF_MUCK        0x2U
165 int backsql_init_search( backsql_srch_info *bsi, 
166                 struct berval *nbase, int scope, int slimit, int tlimit,
167                 time_t stoptime, Filter *filter, SQLHDBC dbh,
168                 Operation *op, SlapReply *rs, AttributeName *attrs,
169                 unsigned flags );
170
171 /*
172  * sql-wrap.h
173  */
174
175 RETCODE backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, char* query, int timeout );
176
177 #define backsql_BindParamStr( sth, par_ind, io, str, maxlen )           \
178         SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind),               \
179                         (io), SQL_C_CHAR, SQL_VARCHAR,                  \
180                         (SQLUINTEGER)(maxlen), 0, (SQLPOINTER)(str),    \
181                         (SQLUINTEGER)(maxlen), NULL )
182
183 #define backsql_BindParamBerVal( sth, par_ind, io, bv )                 \
184         SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind),               \
185                         (io), SQL_C_CHAR, SQL_VARCHAR,                  \
186                         (SQLUINTEGER)(bv)->bv_len, 0,                   \
187                         (SQLPOINTER)(bv)->bv_val,                       \
188                         (SQLUINTEGER)(bv)->bv_len, NULL )
189
190 #define backsql_BindParamInt( sth, par_ind, io, val )                   \
191         SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind),               \
192                         (io), SQL_C_ULONG, SQL_INTEGER,                 \
193                         0, 0, (SQLPOINTER)(val), 0, (SQLINTEGER*)NULL )
194
195 #ifdef BACKSQL_ARBITRARY_KEY
196 #define backsql_BindParamID( sth, par_ind, io, id )                     \
197         backsql_BindParamBerVal( (sth), (par_ind), (io), (id) )
198 #else /* ! BACKSQL_ARBITRARY_KEY */
199 #define backsql_BindParamID( sth, par_ind, io, id )                     \
200         backsql_BindParamInt( (sth), (par_ind), (io), (id) )
201 #endif /* ! BACKSQL_ARBITRARY_KEY */
202
203 RETCODE backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row );
204
205 RETCODE backsql_FreeRow( BACKSQL_ROW_NTS *row );
206
207 void backsql_PrintErrors( SQLHENV henv, SQLHDBC hdbc, SQLHSTMT sth, int rc );
208
209 int backsql_init_db_env( backsql_info *si );
210
211 int backsql_free_db_env( backsql_info *si );
212
213 int backsql_get_db_conn( Operation *op, SQLHDBC *dbh );
214
215 int backsql_free_db_conn( Operation *op );
216
217 /*
218  * util.c
219  */
220
221 extern char 
222         backsql_def_oc_query[],
223         backsql_def_needs_select_oc_query[],
224         backsql_def_at_query[],
225         backsql_def_delentry_query[],
226         backsql_def_insentry_query[],
227         backsql_def_delobjclasses_query[],
228         backsql_def_delreferrals_query[],
229         backsql_def_subtree_cond[],
230         backsql_def_upper_subtree_cond[],
231         backsql_id_query[],
232         backsql_def_concat_func[];
233 extern char 
234         backsql_check_dn_ru_query[];
235
236 struct berbuf * backsql_strcat( struct berbuf *dest, ... );
237 struct berbuf * backsql_strfcat( struct berbuf *dest, const char *fmt, ... );
238
239 int backsql_entry_addattr( Entry *e, struct berval *at_name, 
240                 struct berval *at_val, void *memctx );
241
242 int backsql_merge_from_clause( struct berbuf *dest_from, 
243                 struct berval *src_from );
244
245 int backsql_split_pattern( const char *pattern, BerVarray *split_pattern,
246                 int expected );
247
248 int backsql_prepare_pattern( BerVarray split_pattern, BerVarray values,
249                 struct berval *res );
250
251 int backsql_entryUUID( backsql_info *bi, backsql_entryID *id,
252                 struct berval *entryUUID, void *memctx );
253 int backsql_entryUUID_decode( struct berval *entryUUID, unsigned long *oc_id,
254 #ifdef BACKSQL_ARBITRARY_KEY
255         struct berval   *keyval
256 #else /* ! BACKSQL_ARBITRARY_KEY */
257         unsigned long   *keyval
258 #endif /* ! BACKSQL_ARBITRARY_KEY */
259         );
260
261 /*
262  * former external.h
263  */
264
265 extern BI_init                  sql_back_initialize;
266
267 extern BI_destroy               backsql_destroy;
268
269 extern BI_db_init               backsql_db_init;
270 extern BI_db_open               backsql_db_open;
271 extern BI_db_close              backsql_db_close;
272 extern BI_db_destroy            backsql_db_destroy;
273 extern BI_db_config             backsql_db_config;
274
275 extern BI_op_bind               backsql_bind;
276 extern BI_op_search             backsql_search;
277 extern BI_op_compare            backsql_compare;
278 extern BI_op_modify             backsql_modify;
279 extern BI_op_modrdn             backsql_modrdn;
280 extern BI_op_add                backsql_add;
281 extern BI_op_delete             backsql_delete;
282
283 extern BI_operational           backsql_operational;
284 extern BI_entry_get_rw          backsql_entry_get;
285
286 extern BI_connection_destroy    backsql_connection_destroy;
287
288 #endif /* PROTO_SQL_H */