]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/proto-sql.h
Happy new year! (belated)
[openldap] / servers / slapd / back-sql / proto-sql.h
1 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
2  *
3  * Copyright 1999-2008 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 Masarati
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
78 /*
79  * add.c
80  */
81 int backsql_modify_delete_all_values(
82         Operation               *op,
83         SlapReply               *rs,
84         SQLHDBC                 dbh, 
85         backsql_entryID         *e_id,
86         backsql_at_map_rec      *at );
87
88 int backsql_modify_internal(
89         Operation               *op,
90         SlapReply               *rs,
91         SQLHDBC                 dbh, 
92         backsql_oc_map_rec      *oc,
93         backsql_entryID         *e_id,
94         Modifications           *modlist );
95
96 /*
97  * api.c
98  */
99 int backsql_api_config( backsql_info *bi, const char *name,
100                 int argc, char *argv[] );
101 int backsql_api_destroy( backsql_info *bi );
102 int backsql_api_register( backsql_api *ba );
103 int backsql_api_dn2odbc( Operation *op, SlapReply *rs, struct berval *dn );
104 int backsql_api_odbc2dn( Operation *op, SlapReply *rs, struct berval *dn );
105
106 /*
107  * entry-id.c
108  */
109 #ifdef BACKSQL_ARBITRARY_KEY
110 extern struct berval    backsql_baseObject_bv;
111 #endif /* BACKSQL_ARBITRARY_KEY */
112
113 /* stores in *id the ID in table ldap_entries corresponding to DN, if any */
114 int backsql_dn2id( Operation *op, SlapReply *rs, SQLHDBC dbh,
115                 struct berval *ndn, backsql_entryID *id,
116                 int matched, int muck );
117
118 /* stores in *nchildren the count of children for an entry */
119 int backsql_count_children( Operation *op, SQLHDBC dbh,
120                 struct berval *dn, unsigned long *nchildren );
121
122 /* returns LDAP_COMPARE_TRUE/LDAP_COMPARE_FALSE if the entry corresponding
123  * to DN has/has not children */
124 int backsql_has_children( Operation *op, SQLHDBC dbh, struct berval *dn );
125
126 /* frees *id and returns next in list */
127 backsql_entryID *backsql_free_entryID( Operation *op, backsql_entryID *id,
128                 int freeit );
129
130 /* turns an ID into an entry */
131 int backsql_id2entry( backsql_srch_info *bsi, backsql_entryID *id );
132
133 /*
134  * operational.c
135  */
136
137 Attribute *backsql_operational_entryUUID( backsql_info *bi, backsql_entryID *id );
138
139 Attribute *backsql_operational_entryCSN( Operation *op );
140
141 /*
142  * schema-map.c
143  */
144
145 int backsql_load_schema_map( backsql_info *si, SQLHDBC dbh );
146
147 backsql_oc_map_rec *backsql_oc2oc( backsql_info *si, ObjectClass *oc );
148
149 backsql_oc_map_rec *backsql_id2oc( backsql_info *si, unsigned long id );
150
151 backsql_oc_map_rec * backsql_name2oc( backsql_info *si,
152                 struct berval *oc_name );
153
154 backsql_at_map_rec *backsql_ad2at( backsql_oc_map_rec *objclass,
155                 AttributeDescription *ad );
156
157 int backsql_supad2at( backsql_oc_map_rec *objclass,
158                 AttributeDescription *supad, backsql_at_map_rec ***pret );
159
160 int backsql_destroy_schema_map( backsql_info *si );
161
162 /*
163  * search.c
164  */
165
166 int backsql_init_search( backsql_srch_info *bsi, 
167                 struct berval *nbase, int scope,
168                 time_t stoptime, Filter *filter, SQLHDBC dbh,
169                 Operation *op, SlapReply *rs, AttributeName *attrs,
170                 unsigned flags );
171
172 void backsql_entry_clean( Operation *op, Entry *e );
173
174 /*
175  * sql-wrap.h
176  */
177
178 RETCODE backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, char* query, int timeout );
179
180 #define backsql_BindParamStr( sth, par_ind, io, str, maxlen )           \
181         SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind),               \
182                         (io), SQL_C_CHAR, SQL_VARCHAR,                  \
183                         (SQLUINTEGER)(maxlen), 0, (SQLPOINTER)(str),    \
184                         (SQLUINTEGER)(maxlen), NULL )
185
186 #define backsql_BindParamBerVal( sth, par_ind, io, bv )                 \
187         SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind),               \
188                         (io), SQL_C_CHAR, SQL_VARCHAR,                  \
189                         (SQLUINTEGER)(bv)->bv_len, 0,                   \
190                         (SQLPOINTER)(bv)->bv_val,                       \
191                         (SQLUINTEGER)(bv)->bv_len, NULL )
192
193 #define backsql_BindParamInt( sth, par_ind, io, val )                   \
194         SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind),               \
195                         (io), SQL_C_ULONG, SQL_INTEGER,                 \
196                         0, 0, (SQLPOINTER)(val), 0, (SQLINTEGER*)NULL )
197
198 #ifdef BACKSQL_ARBITRARY_KEY
199 #define backsql_BindParamID( sth, par_ind, io, id )                     \
200         backsql_BindParamBerVal( (sth), (par_ind), (io), (id) )
201 #else /* ! BACKSQL_ARBITRARY_KEY */
202 #define backsql_BindParamID( sth, par_ind, io, id )                     \
203         backsql_BindParamInt( (sth), (par_ind), (io), (id) )
204 #endif /* ! BACKSQL_ARBITRARY_KEY */
205
206 RETCODE backsql_BindRowAsStrings_x( SQLHSTMT sth, BACKSQL_ROW_NTS *row, void *ctx );
207
208 RETCODE backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row );
209
210 RETCODE backsql_FreeRow_x( BACKSQL_ROW_NTS *row, void *ctx );
211
212 RETCODE backsql_FreeRow( BACKSQL_ROW_NTS *row );
213
214 void backsql_PrintErrors( SQLHENV henv, SQLHDBC hdbc, SQLHSTMT sth, int rc );
215
216 int backsql_conn_destroy( backsql_info *bi );
217
218 int backsql_init_db_env( backsql_info *si );
219
220 int backsql_free_db_env( backsql_info *si );
221
222 int backsql_get_db_conn( Operation *op, SQLHDBC *dbh );
223
224 int backsql_free_db_conn( Operation *op );
225
226 /*
227  * util.c
228  */
229
230 extern char 
231         backsql_def_oc_query[],
232         backsql_def_needs_select_oc_query[],
233         backsql_def_at_query[],
234         backsql_def_delentry_stmt[],
235         backsql_def_renentry_stmt[],
236         backsql_def_insentry_stmt[],
237         backsql_def_delobjclasses_stmt[],
238         backsql_def_subtree_cond[],
239         backsql_def_upper_subtree_cond[],
240         backsql_id_query[],
241         backsql_def_concat_func[];
242 extern char 
243         backsql_check_dn_ru_query[];
244
245 struct berbuf * backsql_strcat_x( struct berbuf *dest, void *memctx, ... );
246 struct berbuf * backsql_strfcat_x( struct berbuf *dest, void *memctx, const char *fmt, ... );
247
248 int backsql_entry_addattr( Entry *e, AttributeDescription *ad, 
249                 struct berval *at_val, void *memctx );
250
251 int backsql_merge_from_clause( backsql_info *bi, struct berbuf *dest_from, 
252                 struct berval *src_from );
253
254 int backsql_split_pattern( const char *pattern, BerVarray *split_pattern,
255                 int expected );
256
257 int backsql_prepare_pattern( BerVarray split_pattern, BerVarray values,
258                 struct berval *res );
259
260 int backsql_entryUUID( backsql_info *bi, backsql_entryID *id,
261                 struct berval *entryUUID, void *memctx );
262 int backsql_entryUUID_decode( struct berval *entryUUID, unsigned long *oc_id,
263 #ifdef BACKSQL_ARBITRARY_KEY
264         struct berval   *keyval
265 #else /* ! BACKSQL_ARBITRARY_KEY */
266         unsigned long   *keyval
267 #endif /* ! BACKSQL_ARBITRARY_KEY */
268         );
269
270 /*
271  * former external.h
272  */
273
274 extern BI_init                  sql_back_initialize;
275
276 extern BI_destroy               backsql_destroy;
277
278 extern BI_db_init               backsql_db_init;
279 extern BI_db_open               backsql_db_open;
280 extern BI_db_close              backsql_db_close;
281 extern BI_db_destroy            backsql_db_destroy;
282 extern BI_db_config             backsql_db_config;
283
284 extern BI_op_bind               backsql_bind;
285 extern BI_op_search             backsql_search;
286 extern BI_op_compare            backsql_compare;
287 extern BI_op_modify             backsql_modify;
288 extern BI_op_modrdn             backsql_modrdn;
289 extern BI_op_add                backsql_add;
290 extern BI_op_delete             backsql_delete;
291
292 extern BI_operational           backsql_operational;
293 extern BI_entry_get_rw          backsql_entry_get;
294 extern BI_entry_release_rw      backsql_entry_release;
295
296 extern BI_connection_destroy    backsql_connection_destroy;
297
298 #endif /* PROTO_SQL_H */