1/*	$NetBSD: proto-sql.h,v 1.3 2021/08/14 16:15:01 christos Exp $	*/
2
3/* $OpenLDAP$ */
4/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 *
6 * Copyright 1999-2021 The OpenLDAP Foundation.
7 * Portions Copyright 1999 Dmitry Kovalev.
8 * Portions Copyright 2002 Pierangelo Mararati.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted only as authorized by the OpenLDAP
13 * Public License.
14 *
15 * A copy of this license is available in the file LICENSE in the
16 * top-level directory of the distribution or, alternatively, at
17 * <http://www.OpenLDAP.org/license.html>.
18 */
19/* ACKNOWLEDGEMENTS:
20 * This work was initially developed by Dmitry Kovalev for inclusion
21 * by OpenLDAP Software.  Additional significant contributors include
22 * Pierangelo Masarati
23 */
24
25/*
26 * The following changes have been addressed:
27 *
28 * Enhancements:
29 *   - re-styled code for better readability
30 *   - upgraded backend API to reflect recent changes
31 *   - LDAP schema is checked when loading SQL/LDAP mapping
32 *   - AttributeDescription/ObjectClass pointers used for more efficient
33 *     mapping lookup
34 *   - bervals used where string length is required often
35 *   - atomized write operations by committing at the end of each operation
36 *     and defaulting connection closure to rollback
37 *   - added LDAP access control to write operations
38 *   - fully implemented modrdn (with rdn attrs change, deleteoldrdn,
39 *     access check, parent/children check and more)
40 *   - added parent access control, children control to delete operation
41 *   - added structuralObjectClass operational attribute check and
42 *     value return on search
43 *   - added hasSubordinate operational attribute on demand
44 *   - search limits are appropriately enforced
45 *   - function backsql_strcat() has been made more efficient
46 *   - concat function has been made configurable by means of a pattern
47 *   - added config switches:
48 *       - fail_if_no_mapping	write operations fail if there is no mapping
49 *       - has_ldapinfo_dn_ru	overrides autodetect
50 *       - concat_pattern	a string containing two '?' is used
51 * 				(note that "?||?" should be more portable
52 * 				than builtin function "CONCAT(?,?)")
53 *       - strcast_func		cast of string constants in "SELECT DISTINCT
54 *				statements (needed by PostgreSQL)
55 *       - upper_needs_cast	cast the argument of upper when required
56 * 				(basically when building dn substring queries)
57 *   - added noop control
58 *   - added values return filter control
59 *   - hasSubordinate can be used in search filters (with limitations)
60 *   - eliminated oc->name; use oc->oc->soc_cname instead
61 *
62 * Todo:
63 *   - add security checks for SQL statements that can be injected (?)
64 *   - re-test with previously supported RDBMs
65 *   - replace dn_ru and so with normalized dn (no need for upper() and so
66 *     in dn match)
67 *   - implement a backsql_normalize() function to replace the upper()
68 *     conversion routines
69 *   - note that subtree deletion, subtree renaming and so could be easily
70 *     implemented (rollback and consistency checks are available :)
71 *   - implement "lastmod" and other operational stuff (ldap_entries table ?)
72 *   - check how to allow multiple operations with one statement, to remove
73 *     BACKSQL_REALLOC_STMT from modify.c (a more recent unixODBC lib?)
74 */
75
76#ifndef PROTO_SQL_H
77#define PROTO_SQL_H
78
79#include "back-sql.h"
80
81/*
82 * add.c
83 */
84int backsql_modify_delete_all_values(
85	Operation 		*op,
86	SlapReply		*rs,
87	SQLHDBC			dbh,
88	backsql_entryID		*e_id,
89	backsql_at_map_rec	*at );
90
91int backsql_modify_internal(
92	Operation 		*op,
93	SlapReply		*rs,
94	SQLHDBC			dbh,
95	backsql_oc_map_rec	*oc,
96	backsql_entryID		*e_id,
97	Modifications		*modlist );
98
99/*
100 * api.c
101 */
102int backsql_api_config( backsql_info *bi, const char *name,
103		int argc, char *argv[] );
104int backsql_api_destroy( backsql_info *bi );
105int backsql_api_register( backsql_api *ba );
106int backsql_api_dn2odbc( Operation *op, SlapReply *rs, struct berval *dn );
107int backsql_api_odbc2dn( Operation *op, SlapReply *rs, struct berval *dn );
108
109/*
110 * entry-id.c
111 */
112#ifdef BACKSQL_ARBITRARY_KEY
113extern struct berval	backsql_baseObject_bv;
114#endif /* BACKSQL_ARBITRARY_KEY */
115
116/* stores in *id the ID in table ldap_entries corresponding to DN, if any */
117extern int
118backsql_dn2id( Operation *op, SlapReply *rs, SQLHDBC dbh,
119		struct berval *ndn, backsql_entryID *id,
120		int matched, int muck );
121
122/* stores in *nchildren the count of children for an entry */
123extern int
124backsql_count_children( Operation *op, SQLHDBC dbh,
125		struct berval *dn, unsigned long *nchildren );
126
127/* returns LDAP_COMPARE_TRUE/LDAP_COMPARE_FALSE if the entry corresponding
128 * to DN has/has not children */
129extern int
130backsql_has_children( Operation *op, SQLHDBC dbh, struct berval *dn );
131
132/* free *id and return next in list */
133extern backsql_entryID *
134backsql_free_entryID( backsql_entryID *id, int freeit, void *ctx );
135
136/* turn an ID into an entry */
137extern int
138backsql_id2entry( backsql_srch_info *bsi, backsql_entryID *id );
139
140/* duplicate an entryID */
141extern backsql_entryID *
142backsql_entryID_dup( backsql_entryID *eid, void *ctx );
143
144/*
145 * operational.c
146 */
147
148Attribute *backsql_operational_entryUUID( backsql_info *bi, backsql_entryID *id );
149
150Attribute *backsql_operational_entryCSN( Operation *op );
151
152/*
153 * schema-map.c
154 */
155
156int backsql_load_schema_map( backsql_info *si, SQLHDBC dbh );
157
158backsql_oc_map_rec *backsql_oc2oc( backsql_info *si, ObjectClass *oc );
159
160backsql_oc_map_rec *backsql_id2oc( backsql_info *si, unsigned long id );
161
162backsql_oc_map_rec * backsql_name2oc( backsql_info *si,
163		struct berval *oc_name );
164
165backsql_at_map_rec *backsql_ad2at( backsql_oc_map_rec *objclass,
166		AttributeDescription *ad );
167
168int backsql_supad2at( backsql_oc_map_rec *objclass,
169		AttributeDescription *supad, backsql_at_map_rec ***pret );
170
171int backsql_destroy_schema_map( backsql_info *si );
172
173/*
174 * search.c
175 */
176
177int backsql_init_search( backsql_srch_info *bsi,
178		struct berval *nbase, int scope,
179		time_t stoptime, Filter *filter, SQLHDBC dbh,
180		Operation *op, SlapReply *rs, AttributeName *attrs,
181		unsigned flags );
182
183void backsql_entry_clean( Operation *op, Entry *e );
184
185/*
186 * sql-wrap.h
187 */
188
189RETCODE backsql_Prepare( SQLHDBC dbh, SQLHSTMT *sth, const char* query, int timeout );
190
191#define backsql_BindParamStr( sth, par_ind, io, str, maxlen ) 		\
192	SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind), 		\
193			(io), SQL_C_CHAR, SQL_VARCHAR,			\
194         		(SQLULEN)(maxlen), 0, (SQLPOINTER)(str),	\
195			(SQLLEN)(maxlen), NULL )
196
197#define backsql_BindParamBerVal( sth, par_ind, io, bv ) 		\
198	SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind), 		\
199			(io), SQL_C_CHAR, SQL_VARCHAR,			\
200         		(SQLULEN)(bv)->bv_len, 0,			\
201			(SQLPOINTER)(bv)->bv_val,			\
202			(SQLLEN)(bv)->bv_len, NULL )
203
204#define backsql_BindParamInt( sth, par_ind, io, val )			\
205	SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind),		\
206			(io), SQL_C_ULONG, SQL_INTEGER,			\
207			0, 0, (SQLPOINTER)(val), 0, (SQLLEN*)NULL )
208
209#define backsql_BindParamNumID( sth, par_ind, io, val )			\
210	SQLBindParameter( (sth), (SQLUSMALLINT)(par_ind),		\
211			(io), BACKSQL_C_NUMID, SQL_INTEGER,		\
212			0, 0, (SQLPOINTER)(val), 0, (SQLLEN*)NULL )
213
214#ifdef BACKSQL_ARBITRARY_KEY
215#define backsql_BindParamID( sth, par_ind, io, id )			\
216	backsql_BindParamBerVal( (sth), (par_ind), (io), (id) )
217#else /* ! BACKSQL_ARBITRARY_KEY */
218#define backsql_BindParamID( sth, par_ind, io, id )			\
219	backsql_BindParamNumID( (sth), (par_ind), (io), (id) )
220#endif /* ! BACKSQL_ARBITRARY_KEY */
221
222RETCODE backsql_BindRowAsStrings_x( SQLHSTMT sth, BACKSQL_ROW_NTS *row, void *ctx );
223
224RETCODE backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row );
225
226RETCODE backsql_FreeRow_x( BACKSQL_ROW_NTS *row, void *ctx );
227
228RETCODE backsql_FreeRow( BACKSQL_ROW_NTS *row );
229
230void backsql_PrintErrors( SQLHENV henv, SQLHDBC hdbc, SQLHSTMT sth, int rc );
231
232int backsql_conn_destroy( backsql_info *bi );
233
234int backsql_init_db_env( backsql_info *si );
235
236int backsql_free_db_env( backsql_info *si );
237
238int backsql_get_db_conn( Operation *op, SQLHDBC	*dbh );
239
240int backsql_free_db_conn( Operation *op, SQLHDBC dbh );
241
242/*
243 * util.c
244 */
245
246extern const char
247	backsql_def_oc_query[],
248	backsql_def_needs_select_oc_query[],
249	backsql_def_at_query[],
250	backsql_def_delentry_stmt[],
251	backsql_def_renentry_stmt[],
252	backsql_def_insentry_stmt[],
253	backsql_def_delobjclasses_stmt[],
254	backsql_def_subtree_cond[],
255	backsql_def_upper_subtree_cond[],
256	backsql_id_query[],
257	backsql_def_concat_func[],
258	backsql_check_dn_ru_query[];
259
260struct berbuf * backsql_strcat_x( struct berbuf *dest, void *memctx, ... );
261struct berbuf * backsql_strfcat_x( struct berbuf *dest, void *memctx, const char *fmt, ... );
262
263int backsql_entry_addattr( Entry *e, AttributeDescription *ad,
264		struct berval *at_val, void *memctx );
265
266int backsql_merge_from_clause( backsql_info *bi, struct berbuf *dest_from,
267		struct berval *src_from );
268
269int backsql_split_pattern( const char *pattern, BerVarray *split_pattern,
270		int expected );
271
272int backsql_prepare_pattern( BerVarray split_pattern, BerVarray values,
273		struct berval *res );
274
275int backsql_entryUUID( backsql_info *bi, backsql_entryID *id,
276		struct berval *entryUUID, void *memctx );
277int backsql_entryUUID_decode( struct berval *entryUUID, unsigned long *oc_id,
278#ifdef BACKSQL_ARBITRARY_KEY
279	struct berval	*keyval
280#else /* ! BACKSQL_ARBITRARY_KEY */
281	unsigned long	*keyval
282#endif /* ! BACKSQL_ARBITRARY_KEY */
283	);
284
285/*
286 * former external.h
287 */
288
289extern BI_init			sql_back_initialize;
290
291extern BI_destroy		backsql_destroy;
292
293extern BI_db_init		backsql_db_init;
294extern BI_db_open		backsql_db_open;
295extern BI_db_close		backsql_db_close;
296extern BI_db_destroy		backsql_db_destroy;
297extern BI_db_config		backsql_db_config;
298
299extern BI_op_bind		backsql_bind;
300extern BI_op_search		backsql_search;
301extern BI_op_compare		backsql_compare;
302extern BI_op_modify		backsql_modify;
303extern BI_op_modrdn		backsql_modrdn;
304extern BI_op_add		backsql_add;
305extern BI_op_delete		backsql_delete;
306
307extern BI_operational		backsql_operational;
308extern BI_entry_get_rw		backsql_entry_get;
309extern BI_entry_release_rw	backsql_entry_release;
310
311extern BI_connection_destroy	backsql_connection_destroy;
312
313int backsql_init_cf( BackendInfo * bi );
314
315#endif /* PROTO_SQL_H */
316