proto-sql.h 9.97 KB
Newer Older
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
1
/* $OpenLDAP$ */
2
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
4
 * Copyright 1999-2021 The OpenLDAP Foundation.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 * Portions Copyright 1999 Dmitry Kovalev.
 * Portions Copyright 2002 Pierangelo Mararati.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by Dmitry Kovalev for inclusion
 * by OpenLDAP Software.  Additional significant contributors include
20
 * Pierangelo Masarati
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 */

/*
 * The following changes have been addressed:
 *	 
 * Enhancements:
 *   - re-styled code for better readability
 *   - upgraded backend API to reflect recent changes
 *   - LDAP schema is checked when loading SQL/LDAP mapping
 *   - AttributeDescription/ObjectClass pointers used for more efficient
 *     mapping lookup
 *   - bervals used where string length is required often
 *   - atomized write operations by committing at the end of each operation
 *     and defaulting connection closure to rollback
 *   - added LDAP access control to write operations
 *   - fully implemented modrdn (with rdn attrs change, deleteoldrdn,
 *     access check, parent/children check and more)
 *   - added parent access control, children control to delete operation
 *   - added structuralObjectClass operational attribute check and
 *     value return on search
 *   - added hasSubordinate operational attribute on demand
 *   - search limits are appropriately enforced
 *   - function backsql_strcat() has been made more efficient
 *   - concat function has been made configurable by means of a pattern
 *   - added config switches:
 *       - fail_if_no_mapping	write operations fail if there is no mapping
 *       - has_ldapinfo_dn_ru	overrides autodetect
 *       - concat_pattern	a string containing two '?' is used
 * 				(note that "?||?" should be more portable
 * 				than builtin function "CONCAT(?,?)")
 *       - strcast_func		cast of string constants in "SELECT DISTINCT
 *				statements (needed by PostgreSQL)
 *       - upper_needs_cast	cast the argument of upper when required
 * 				(basically when building dn substring queries)
 *   - added noop control
 *   - added values return filter control
 *   - hasSubordinate can be used in search filters (with limitations)
 *   - eliminated oc->name; use oc->oc->soc_cname instead
 * 
 * Todo:
 *   - add security checks for SQL statements that can be injected (?)
 *   - re-test with previously supported RDBMs
 *   - replace dn_ru and so with normalized dn (no need for upper() and so
 *     in dn match)
 *   - implement a backsql_normalize() function to replace the upper()
 *     conversion routines
 *   - note that subtree deletion, subtree renaming and so could be easily
 *     implemented (rollback and consistency checks are available :)
 *   - implement "lastmod" and other operational stuff (ldap_entries table ?)
 *   - check how to allow multiple operations with one statement, to remove
 *     BACKSQL_REALLOC_STMT from modify.c (a more recent unixODBC lib?)
 */

#ifndef PROTO_SQL_H
#define PROTO_SQL_H

#include "back-sql.h"

/*
 * add.c
 */
82
83
84
85
86
87
88
int backsql_modify_delete_all_values(
	Operation 		*op,
	SlapReply		*rs,
	SQLHDBC			dbh, 
	backsql_entryID		*e_id,
	backsql_at_map_rec	*at );

89
90
91
92
93
94
95
96
int backsql_modify_internal(
	Operation 		*op,
	SlapReply		*rs,
	SQLHDBC			dbh, 
	backsql_oc_map_rec	*oc,
	backsql_entryID		*e_id,
	Modifications		*modlist );

97
98
99
/*
 * api.c
 */
100
101
102
int backsql_api_config( backsql_info *bi, const char *name,
		int argc, char *argv[] );
int backsql_api_destroy( backsql_info *bi );
103
104
105
106
int backsql_api_register( backsql_api *ba );
int backsql_api_dn2odbc( Operation *op, SlapReply *rs, struct berval *dn );
int backsql_api_odbc2dn( Operation *op, SlapReply *rs, struct berval *dn );

107
108
109
/*
 * entry-id.c
 */
110
111
112
#ifdef BACKSQL_ARBITRARY_KEY
extern struct berval	backsql_baseObject_bv;
#endif /* BACKSQL_ARBITRARY_KEY */
113
114

/* stores in *id the ID in table ldap_entries corresponding to DN, if any */
115
116
extern int
backsql_dn2id( Operation *op, SlapReply *rs, SQLHDBC dbh,
117
118
		struct berval *ndn, backsql_entryID *id,
		int matched, int muck );
119
120

/* stores in *nchildren the count of children for an entry */
121
122
extern int
backsql_count_children( Operation *op, SQLHDBC dbh,
123
124
125
126
		struct berval *dn, unsigned long *nchildren );

/* returns LDAP_COMPARE_TRUE/LDAP_COMPARE_FALSE if the entry corresponding
 * to DN has/has not children */
127
128
extern int
backsql_has_children( Operation *op, SQLHDBC dbh, struct berval *dn );
129

130
131
132
/* free *id and return next in list */
extern backsql_entryID *
backsql_free_entryID( backsql_entryID *id, int freeit, void *ctx );
133

134
135
136
137
138
139
140
/* turn an ID into an entry */
extern int
backsql_id2entry( backsql_srch_info *bsi, backsql_entryID *id );

/* duplicate an entryID */
extern backsql_entryID *
backsql_entryID_dup( backsql_entryID *eid, void *ctx );
141

142
143
144
145
146
147
148
149
/*
 * operational.c
 */

Attribute *backsql_operational_entryUUID( backsql_info *bi, backsql_entryID *id );

Attribute *backsql_operational_entryCSN( Operation *op );

150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * schema-map.c
 */

int backsql_load_schema_map( backsql_info *si, SQLHDBC dbh );

backsql_oc_map_rec *backsql_oc2oc( backsql_info *si, ObjectClass *oc );

backsql_oc_map_rec *backsql_id2oc( backsql_info *si, unsigned long id );

backsql_oc_map_rec * backsql_name2oc( backsql_info *si,
		struct berval *oc_name );

backsql_at_map_rec *backsql_ad2at( backsql_oc_map_rec *objclass,
		AttributeDescription *ad );

int backsql_supad2at( backsql_oc_map_rec *objclass,
		AttributeDescription *supad, backsql_at_map_rec ***pret );

int backsql_destroy_schema_map( backsql_info *si );

/*
 * search.c
 */

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,
179
		unsigned flags );
180

181
182
void backsql_entry_clean( Operation *op, Entry *e );

183
184
185
186
/*
 * sql-wrap.h
 */

Pierangelo Masarati's avatar
Pierangelo Masarati committed
187
RETCODE backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, const char* query, int timeout );
188

189
#define backsql_BindParamStr( sth, par_ind, io, str, maxlen ) 		\
190
	SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind), 		\
191
			(io), SQL_C_CHAR, SQL_VARCHAR,			\
192
193
         		(SQLULEN)(maxlen), 0, (SQLPOINTER)(str),	\
			(SQLLEN)(maxlen), NULL )
194

195
196
197
#define backsql_BindParamBerVal( sth, par_ind, io, bv ) 		\
	SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind), 		\
			(io), SQL_C_CHAR, SQL_VARCHAR,			\
198
         		(SQLULEN)(bv)->bv_len, 0,			\
199
			(SQLPOINTER)(bv)->bv_val,			\
200
			(SQLLEN)(bv)->bv_len, NULL )
201
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, (SQLLEN*)NULL )
206

207
208
209
#define backsql_BindParamNumID( sth, par_ind, io, val )			\
	SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind),		\
			(io), BACKSQL_C_NUMID, SQL_INTEGER,		\
210
			0, 0, (SQLPOINTER)(val), 0, (SQLLEN*)NULL )
211

212
213
214
215
216
#ifdef BACKSQL_ARBITRARY_KEY
#define backsql_BindParamID( sth, par_ind, io, id )			\
	backsql_BindParamBerVal( (sth), (par_ind), (io), (id) )
#else /* ! BACKSQL_ARBITRARY_KEY */
#define backsql_BindParamID( sth, par_ind, io, id )			\
217
	backsql_BindParamNumID( (sth), (par_ind), (io), (id) )
218
#endif /* ! BACKSQL_ARBITRARY_KEY */
219

220
221
RETCODE backsql_BindRowAsStrings_x( SQLHSTMT sth, BACKSQL_ROW_NTS *row, void *ctx );

222
223
RETCODE backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row );

224
225
RETCODE backsql_FreeRow_x( BACKSQL_ROW_NTS *row, void *ctx );

226
227
228
229
RETCODE backsql_FreeRow( BACKSQL_ROW_NTS *row );

void backsql_PrintErrors( SQLHENV henv, SQLHDBC hdbc, SQLHSTMT sth, int rc );

Pierangelo Masarati's avatar
Pierangelo Masarati committed
230
231
int backsql_conn_destroy( backsql_info *bi );

232
233
234
235
int backsql_init_db_env( backsql_info *si );

int backsql_free_db_env( backsql_info *si );

236
int backsql_get_db_conn( Operation *op, SQLHDBC	*dbh );
237

238
int backsql_free_db_conn( Operation *op, SQLHDBC dbh );
239
240
241
242
243

/*
 * util.c
 */

244
extern const char 
245
246
247
	backsql_def_oc_query[],
	backsql_def_needs_select_oc_query[],
	backsql_def_at_query[],
248
	backsql_def_delentry_stmt[],
249
	backsql_def_renentry_stmt[],
250
251
	backsql_def_insentry_stmt[],
	backsql_def_delobjclasses_stmt[],
252
253
254
	backsql_def_subtree_cond[],
	backsql_def_upper_subtree_cond[],
	backsql_id_query[],
255
	backsql_def_concat_func[],
256
257
	backsql_check_dn_ru_query[];

258
259
struct berbuf * backsql_strcat_x( struct berbuf *dest, void *memctx, ... );
struct berbuf * backsql_strfcat_x( struct berbuf *dest, void *memctx, const char *fmt, ... );
260

261
int backsql_entry_addattr( Entry *e, AttributeDescription *ad, 
262
263
		struct berval *at_val, void *memctx );

264
int backsql_merge_from_clause( backsql_info *bi, struct berbuf *dest_from, 
265
266
267
268
269
270
271
272
		struct berval *src_from );

int backsql_split_pattern( const char *pattern, BerVarray *split_pattern,
		int expected );

int backsql_prepare_pattern( BerVarray split_pattern, BerVarray values,
		struct berval *res );

273
274
275
276
277
278
279
280
281
282
int backsql_entryUUID( backsql_info *bi, backsql_entryID *id,
		struct berval *entryUUID, void *memctx );
int backsql_entryUUID_decode( struct berval *entryUUID, unsigned long *oc_id,
#ifdef BACKSQL_ARBITRARY_KEY
	struct berval	*keyval
#else /* ! BACKSQL_ARBITRARY_KEY */
	unsigned long	*keyval
#endif /* ! BACKSQL_ARBITRARY_KEY */
	);

283
284
285
/*
 * former external.h
 */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
286
287
288

extern BI_init			sql_back_initialize;

289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
extern BI_destroy		backsql_destroy;

extern BI_db_init		backsql_db_init;
extern BI_db_open		backsql_db_open;
extern BI_db_close		backsql_db_close;
extern BI_db_destroy		backsql_db_destroy;
extern BI_db_config		backsql_db_config;

extern BI_op_bind		backsql_bind;
extern BI_op_search		backsql_search;
extern BI_op_compare		backsql_compare;
extern BI_op_modify		backsql_modify;
extern BI_op_modrdn		backsql_modrdn;
extern BI_op_add		backsql_add;
extern BI_op_delete		backsql_delete;

extern BI_operational		backsql_operational;
306
extern BI_entry_get_rw		backsql_entry_get;
307
extern BI_entry_release_rw	backsql_entry_release;
308
309
310

extern BI_connection_destroy	backsql_connection_destroy;

311
312
int backsql_init_cf( BackendInfo * bi );

313
#endif /* PROTO_SQL_H */