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