1 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 * Copyright 1999-2006 The OpenLDAP Foundation.
4 * Portions Copyright 1999 Dmitry Kovalev.
5 * Portions Copyright 2002 Pierangelo Mararati.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
17 * This work was initially developed by Dmitry Kovalev for inclusion
18 * by OpenLDAP Software. Additional significant contributors include
23 * The following changes have been addressed:
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
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
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
64 * - implement a backsql_normalize() function to replace the upper()
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?)
81 int backsql_modify_delete_all_values(
85 backsql_entryID *e_id,
86 backsql_at_map_rec *at );
88 int backsql_modify_internal(
92 backsql_oc_map_rec *oc,
93 backsql_entryID *e_id,
94 Modifications *modlist );
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 );
109 #ifdef BACKSQL_ARBITRARY_KEY
110 extern struct berval backsql_baseObject_bv;
111 #endif /* BACKSQL_ARBITRARY_KEY */
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 );
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 );
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 );
126 /* frees *id and returns next in list */
127 backsql_entryID *backsql_free_entryID( Operation *op, backsql_entryID *id,
130 /* turns an ID into an entry */
131 int backsql_id2entry( backsql_srch_info *bsi, backsql_entryID *id );
137 Attribute *backsql_operational_entryUUID( backsql_info *bi, backsql_entryID *id );
139 Attribute *backsql_operational_entryCSN( Operation *op );
145 int backsql_load_schema_map( backsql_info *si, SQLHDBC dbh );
147 backsql_oc_map_rec *backsql_oc2oc( backsql_info *si, ObjectClass *oc );
149 backsql_oc_map_rec *backsql_id2oc( backsql_info *si, unsigned long id );
151 backsql_oc_map_rec * backsql_name2oc( backsql_info *si,
152 struct berval *oc_name );
154 backsql_at_map_rec *backsql_ad2at( backsql_oc_map_rec *objclass,
155 AttributeDescription *ad );
157 int backsql_supad2at( backsql_oc_map_rec *objclass,
158 AttributeDescription *supad, backsql_at_map_rec ***pret );
160 int backsql_destroy_schema_map( backsql_info *si );
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,
172 void backsql_entry_clean( Operation *op, Entry *e );
178 RETCODE backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, char* query, int timeout );
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 )
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 )
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 )
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 */
206 RETCODE backsql_BindRowAsStrings_x( SQLHSTMT sth, BACKSQL_ROW_NTS *row, void *ctx );
208 RETCODE backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row );
210 RETCODE backsql_FreeRow_x( BACKSQL_ROW_NTS *row, void *ctx );
212 RETCODE backsql_FreeRow( BACKSQL_ROW_NTS *row );
214 void backsql_PrintErrors( SQLHENV henv, SQLHDBC hdbc, SQLHSTMT sth, int rc );
216 int backsql_conn_destroy( backsql_info *bi );
218 int backsql_init_db_env( backsql_info *si );
220 int backsql_free_db_env( backsql_info *si );
222 int backsql_get_db_conn( Operation *op, SQLHDBC *dbh );
224 int backsql_free_db_conn( Operation *op );
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[],
241 backsql_def_concat_func[];
243 backsql_check_dn_ru_query[];
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, ... );
248 int backsql_entry_addattr( Entry *e, AttributeDescription *ad,
249 struct berval *at_val, void *memctx );
251 int backsql_merge_from_clause( backsql_info *bi, struct berbuf *dest_from,
252 struct berval *src_from );
254 int backsql_split_pattern( const char *pattern, BerVarray *split_pattern,
257 int backsql_prepare_pattern( BerVarray split_pattern, BerVarray values,
258 struct berval *res );
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 */
274 extern BI_init sql_back_initialize;
276 extern BI_destroy backsql_destroy;
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;
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;
292 extern BI_operational backsql_operational;
293 extern BI_entry_get_rw backsql_entry_get;
294 extern BI_entry_release_rw backsql_entry_release;
296 extern BI_connection_destroy backsql_connection_destroy;
298 #endif /* PROTO_SQL_H */