xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-sql/proto-sql.h (revision 466a16a118933bd295a8a104f095714fadf9cf68)
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  */
81 int 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 
88 int 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  */
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 );
105 
106 /*
107  * entry-id.c
108  */
109 #ifdef BACKSQL_ARBITRARY_KEY
110 extern 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 */
114 extern int
115 backsql_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 */
120 extern int
121 backsql_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 */
126 extern int
127 backsql_has_children( Operation *op, SQLHDBC dbh, struct berval *dn );
128 
129 /* free *id and return next in list */
130 extern backsql_entryID *
131 backsql_free_entryID( backsql_entryID *id, int freeit, void *ctx );
132 
133 /* turn an ID into an entry */
134 extern int
135 backsql_id2entry( backsql_srch_info *bsi, backsql_entryID *id );
136 
137 /* duplicate an entryID */
138 extern backsql_entryID *
139 backsql_entryID_dup( backsql_entryID *eid, void *ctx );
140 
141 /*
142  * operational.c
143  */
144 
145 Attribute *backsql_operational_entryUUID( backsql_info *bi, backsql_entryID *id );
146 
147 Attribute *backsql_operational_entryCSN( Operation *op );
148 
149 /*
150  * schema-map.c
151  */
152 
153 int backsql_load_schema_map( backsql_info *si, SQLHDBC dbh );
154 
155 backsql_oc_map_rec *backsql_oc2oc( backsql_info *si, ObjectClass *oc );
156 
157 backsql_oc_map_rec *backsql_id2oc( backsql_info *si, unsigned long id );
158 
159 backsql_oc_map_rec * backsql_name2oc( backsql_info *si,
160 		struct berval *oc_name );
161 
162 backsql_at_map_rec *backsql_ad2at( backsql_oc_map_rec *objclass,
163 		AttributeDescription *ad );
164 
165 int backsql_supad2at( backsql_oc_map_rec *objclass,
166 		AttributeDescription *supad, backsql_at_map_rec ***pret );
167 
168 int backsql_destroy_schema_map( backsql_info *si );
169 
170 /*
171  * search.c
172  */
173 
174 int 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 
180 void backsql_entry_clean( Operation *op, Entry *e );
181 
182 /*
183  * sql-wrap.h
184  */
185 
186 RETCODE 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 
214 RETCODE backsql_BindRowAsStrings_x( SQLHSTMT sth, BACKSQL_ROW_NTS *row, void *ctx );
215 
216 RETCODE backsql_BindRowAsStrings( SQLHSTMT sth, BACKSQL_ROW_NTS *row );
217 
218 RETCODE backsql_FreeRow_x( BACKSQL_ROW_NTS *row, void *ctx );
219 
220 RETCODE backsql_FreeRow( BACKSQL_ROW_NTS *row );
221 
222 void backsql_PrintErrors( SQLHENV henv, SQLHDBC hdbc, SQLHSTMT sth, int rc );
223 
224 int backsql_conn_destroy( backsql_info *bi );
225 
226 int backsql_init_db_env( backsql_info *si );
227 
228 int backsql_free_db_env( backsql_info *si );
229 
230 int backsql_get_db_conn( Operation *op, SQLHDBC	*dbh );
231 
232 int backsql_free_db_conn( Operation *op, SQLHDBC dbh );
233 
234 /*
235  * util.c
236  */
237 
238 extern 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 
252 struct berbuf * backsql_strcat_x( struct berbuf *dest, void *memctx, ... );
253 struct berbuf * backsql_strfcat_x( struct berbuf *dest, void *memctx, const char *fmt, ... );
254 
255 int backsql_entry_addattr( Entry *e, AttributeDescription *ad,
256 		struct berval *at_val, void *memctx );
257 
258 int backsql_merge_from_clause( backsql_info *bi, struct berbuf *dest_from,
259 		struct berval *src_from );
260 
261 int backsql_split_pattern( const char *pattern, BerVarray *split_pattern,
262 		int expected );
263 
264 int backsql_prepare_pattern( BerVarray split_pattern, BerVarray values,
265 		struct berval *res );
266 
267 int backsql_entryUUID( backsql_info *bi, backsql_entryID *id,
268 		struct berval *entryUUID, void *memctx );
269 int 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 
281 extern BI_init			sql_back_initialize;
282 
283 extern BI_destroy		backsql_destroy;
284 
285 extern BI_db_init		backsql_db_init;
286 extern BI_db_open		backsql_db_open;
287 extern BI_db_close		backsql_db_close;
288 extern BI_db_destroy		backsql_db_destroy;
289 extern BI_db_config		backsql_db_config;
290 
291 extern BI_op_bind		backsql_bind;
292 extern BI_op_search		backsql_search;
293 extern BI_op_compare		backsql_compare;
294 extern BI_op_modify		backsql_modify;
295 extern BI_op_modrdn		backsql_modrdn;
296 extern BI_op_add		backsql_add;
297 extern BI_op_delete		backsql_delete;
298 
299 extern BI_operational		backsql_operational;
300 extern BI_entry_get_rw		backsql_entry_get;
301 extern BI_entry_release_rw	backsql_entry_release;
302 
303 extern BI_connection_destroy	backsql_connection_destroy;
304 
305 #endif /* PROTO_SQL_H */
306