2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2009 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
6 * Portions Copyright 2002 Pierangelo Mararati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * This work was initially developed by Dmitry Kovalev for inclusion
19 * by OpenLDAP Software. Additional significant contributors include
24 * The following changes have been addressed:
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
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
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
65 * - implement a backsql_normalize() function to replace the upper()
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?)
82 int backsql_modify_delete_all_values(
86 backsql_entryID *e_id,
87 backsql_at_map_rec *at );
89 int backsql_modify_internal(
93 backsql_oc_map_rec *oc,
94 backsql_entryID *e_id,
95 Modifications *modlist );
100 int backsql_api_config( backsql_info *bi, const char *name,
101 int argc, char *argv[] );
102 int backsql_api_destroy( backsql_info *bi );
103 int backsql_api_register( backsql_api *ba );
104 int backsql_api_dn2odbc( Operation *op, SlapReply *rs, struct berval *dn );
105 int backsql_api_odbc2dn( Operation *op, SlapReply *rs, struct berval *dn );
110 #ifdef BACKSQL_ARBITRARY_KEY
111 extern struct berval backsql_baseObject_bv;
112 #endif /* BACKSQL_ARBITRARY_KEY */
114 /* stores in *id the ID in table ldap_entries corresponding to DN, if any */
116 backsql_dn2id( Operation *op, SlapReply *rs, SQLHDBC dbh,
117 struct berval *ndn, backsql_entryID *id,
118 int matched, int muck );
120 /* stores in *nchildren the count of children for an entry */
122 backsql_count_children( Operation *op, SQLHDBC dbh,
123 struct berval *dn, unsigned long *nchildren );
125 /* returns LDAP_COMPARE_TRUE/LDAP_COMPARE_FALSE if the entry corresponding
126 * to DN has/has not children */
128 backsql_has_children( Operation *op, SQLHDBC dbh, struct berval *dn );
130 /* free *id and return next in list */
131 extern backsql_entryID *
132 backsql_free_entryID( backsql_entryID *id, int freeit, void *ctx );
134 /* turn an ID into an entry */
136 backsql_id2entry( backsql_srch_info *bsi, backsql_entryID *id );
138 /* duplicate an entryID */
139 extern backsql_entryID *
140 backsql_entryID_dup( backsql_entryID *eid, void *ctx );
146 Attribute *backsql_operational_entryUUID( backsql_info *bi, backsql_entryID *id );
148 Attribute *backsql_operational_entryCSN( Operation *op );
154 int backsql_load_schema_map( backsql_info *si, SQLHDBC dbh );
156 backsql_oc_map_rec *backsql_oc2oc( backsql_info *si, ObjectClass *oc );
158 backsql_oc_map_rec *backsql_id2oc( backsql_info *si, unsigned long id );
160 backsql_oc_map_rec * backsql_name2oc( backsql_info *si,
161 struct berval *oc_name );
163 backsql_at_map_rec *backsql_ad2at( backsql_oc_map_rec *objclass,
164 AttributeDescription *ad );
166 int backsql_supad2at( backsql_oc_map_rec *objclass,
167 AttributeDescription *supad, backsql_at_map_rec ***pret );
169 int backsql_destroy_schema_map( backsql_info *si );
175 int backsql_init_search( backsql_srch_info *bsi,
176 struct berval *nbase, int scope,
177 time_t stoptime, Filter *filter, SQLHDBC dbh,
178 Operation *op, SlapReply *rs, AttributeName *attrs,
181 void backsql_entry_clean( Operation *op, Entry *e );
187 RETCODE backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, const char* query, int timeout );
189 #define backsql_BindParamStr( sth, par_ind, io, str, maxlen ) \
190 SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind), \
191 (io), SQL_C_CHAR, SQL_VARCHAR, \
192 (SQLUINTEGER)(maxlen), 0, (SQLPOINTER)(str), \
193 (SQLUINTEGER)(maxlen), NULL )
195 #define backsql_BindParamBerVal( sth, par_ind, io, bv ) \
196 SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind), \
197 (io), SQL_C_CHAR, SQL_VARCHAR, \
198 (SQLUINTEGER)(bv)->bv_len, 0, \
199 (SQLPOINTER)(bv)->bv_val, \
200 (SQLUINTEGER)(bv)->bv_len, NULL )
202 #define backsql_BindParamInt( sth, par_ind, io, val ) \
203 SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind), \
204 (io), SQL_C_ULONG, SQL_INTEGER, \
205 0, 0, (SQLPOINTER)(val), 0, (SQLINTEGER*)NULL )
207 #ifdef BACKSQL_ARBITRARY_KEY
208 #define backsql_BindParamID( sth, par_ind, io, id ) \
209 backsql_BindParamBerVal( (sth), (par_ind), (io), (id) )
210 #else /* ! BACKSQL_ARBITRARY_KEY */
211 #define backsql_BindParamID( sth, par_ind, io, id ) \
212 backsql_BindParamInt( (sth), (par_ind), (io), (id) )
213 #endif /* ! BACKSQL_ARBITRARY_KEY */
215 RETCODE backsql_BindRowAsStrings_x( SQLHSTMT sth, BACKSQL_ROW_NTS *row, void *ctx );
217 RETCODE backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row );
219 RETCODE backsql_FreeRow_x( BACKSQL_ROW_NTS *row, void *ctx );
221 RETCODE backsql_FreeRow( BACKSQL_ROW_NTS *row );
223 void backsql_PrintErrors( SQLHENV henv, SQLHDBC hdbc, SQLHSTMT sth, int rc );
225 int backsql_conn_destroy( backsql_info *bi );
227 int backsql_init_db_env( backsql_info *si );
229 int backsql_free_db_env( backsql_info *si );
231 int backsql_get_db_conn( Operation *op, SQLHDBC *dbh );
233 int backsql_free_db_conn( Operation *op, SQLHDBC dbh );
240 backsql_def_oc_query[],
241 backsql_def_needs_select_oc_query[],
242 backsql_def_at_query[],
243 backsql_def_delentry_stmt[],
244 backsql_def_renentry_stmt[],
245 backsql_def_insentry_stmt[],
246 backsql_def_delobjclasses_stmt[],
247 backsql_def_subtree_cond[],
248 backsql_def_upper_subtree_cond[],
250 backsql_def_concat_func[],
251 backsql_check_dn_ru_query[];
253 struct berbuf * backsql_strcat_x( struct berbuf *dest, void *memctx, ... );
254 struct berbuf * backsql_strfcat_x( struct berbuf *dest, void *memctx, const char *fmt, ... );
256 int backsql_entry_addattr( Entry *e, AttributeDescription *ad,
257 struct berval *at_val, void *memctx );
259 int backsql_merge_from_clause( backsql_info *bi, struct berbuf *dest_from,
260 struct berval *src_from );
262 int backsql_split_pattern( const char *pattern, BerVarray *split_pattern,
265 int backsql_prepare_pattern( BerVarray split_pattern, BerVarray values,
266 struct berval *res );
268 int backsql_entryUUID( backsql_info *bi, backsql_entryID *id,
269 struct berval *entryUUID, void *memctx );
270 int backsql_entryUUID_decode( struct berval *entryUUID, unsigned long *oc_id,
271 #ifdef BACKSQL_ARBITRARY_KEY
272 struct berval *keyval
273 #else /* ! BACKSQL_ARBITRARY_KEY */
274 unsigned long *keyval
275 #endif /* ! BACKSQL_ARBITRARY_KEY */
282 extern BI_init sql_back_initialize;
284 extern BI_destroy backsql_destroy;
286 extern BI_db_init backsql_db_init;
287 extern BI_db_open backsql_db_open;
288 extern BI_db_close backsql_db_close;
289 extern BI_db_destroy backsql_db_destroy;
290 extern BI_db_config backsql_db_config;
292 extern BI_op_bind backsql_bind;
293 extern BI_op_search backsql_search;
294 extern BI_op_compare backsql_compare;
295 extern BI_op_modify backsql_modify;
296 extern BI_op_modrdn backsql_modrdn;
297 extern BI_op_add backsql_add;
298 extern BI_op_delete backsql_delete;
300 extern BI_operational backsql_operational;
301 extern BI_entry_get_rw backsql_entry_get;
302 extern BI_entry_release_rw backsql_entry_release;
304 extern BI_connection_destroy backsql_connection_destroy;
306 #endif /* PROTO_SQL_H */