2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
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.
24 #include <sys/types.h>
25 #include "ac/string.h"
28 #include "proto-sql.h"
34 static char *controls[] = {
35 #if 0 /* needs improvements */
36 #ifdef LDAP_CONTROL_NOOP
38 #endif /* LDAP_CONTROL_NOOP */
40 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
41 LDAP_CONTROL_VALUESRETURNFILTER,
42 #endif /* LDAP_CONTROL_VALUESRETURNFILTER */
46 bi->bi_controls = controls;
48 Debug( LDAP_DEBUG_TRACE,"==>sql_back_initialize()\n", 0, 0, 0 );
50 bi->bi_db_init = backsql_db_init;
51 bi->bi_db_config = backsql_db_config;
52 bi->bi_db_open = backsql_db_open;
53 bi->bi_db_close = backsql_db_close;
54 bi->bi_db_destroy = backsql_db_destroy;
56 bi->bi_op_abandon = 0;
57 bi->bi_op_compare = backsql_compare;
58 bi->bi_op_bind = backsql_bind;
60 bi->bi_op_search = backsql_search;
61 bi->bi_op_modify = backsql_modify;
62 bi->bi_op_modrdn = backsql_modrdn;
63 bi->bi_op_add = backsql_add;
64 bi->bi_op_delete = backsql_delete;
66 bi->bi_chk_referrals = 0;
67 bi->bi_operational = backsql_operational;
69 bi->bi_connection_init = 0;
70 bi->bi_connection_destroy = backsql_connection_destroy;
72 Debug( LDAP_DEBUG_TRACE,"<==sql_back_initialize()\n", 0, 0, 0 );
80 Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
81 Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
91 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
92 bi = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
93 memset( bi, '\0', sizeof( backsql_info ) );
94 ldap_pvt_thread_mutex_init( &bi->sql_dbconn_mutex );
95 ldap_pvt_thread_mutex_init( &bi->sql_schema_mutex );
96 backsql_init_db_env( bi );
99 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
107 backsql_info *bi = (backsql_info*)bd->be_private;
109 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
110 ldap_pvt_thread_mutex_lock( &bi->sql_dbconn_mutex );
111 backsql_free_db_env( bi );
112 ldap_pvt_thread_mutex_unlock( &bi->sql_dbconn_mutex );
113 ldap_pvt_thread_mutex_destroy( &bi->sql_dbconn_mutex );
114 ldap_pvt_thread_mutex_lock( &bi->sql_schema_mutex );
115 backsql_destroy_schema_map( bi );
116 ldap_pvt_thread_mutex_unlock( &bi->sql_schema_mutex );
117 ldap_pvt_thread_mutex_destroy( &bi->sql_schema_mutex );
118 free( bi->sql_dbname );
119 free( bi->sql_dbuser );
120 if ( bi->sql_dbpasswd ) {
121 free( bi->sql_dbpasswd );
123 if ( bi->sql_dbhost ) {
124 free( bi->sql_dbhost );
126 if ( bi->sql_upper_func.bv_val ) {
127 free( bi->sql_upper_func.bv_val );
128 free( bi->sql_upper_func_open.bv_val );
129 free( bi->sql_upper_func_close.bv_val );
132 free( bi->sql_subtree_cond.bv_val );
133 free( bi->sql_oc_query );
134 free( bi->sql_at_query );
135 free( bi->sql_insentry_query );
136 free( bi->sql_delentry_query );
137 free( bi->sql_delobjclasses_query );
138 free( bi->sql_delreferrals_query );
140 if ( bi->sql_baseObject ) {
141 entry_free( bi->sql_baseObject );
146 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
154 backsql_info *bi = (backsql_info*)bd->be_private;
157 struct berbuf bb = BB_NULL;
159 Operation otmp = { 0 };
161 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
162 "testing RDBMS connection\n", 0, 0, 0 );
163 if ( bi->sql_dbname == NULL ) {
164 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
165 "datasource name not specified "
166 "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
170 if ( bi->sql_concat_func == NULL ) {
171 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
172 "concat func not specified (use \"concat_pattern\" "
173 "directive in slapd.conf)\n", 0, 0, 0 );
175 if ( backsql_split_pattern( backsql_def_concat_func,
176 &bi->sql_concat_func, 2 ) ) {
177 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
178 "unable to parse pattern \"%s\"",
179 backsql_def_concat_func, 0, 0 );
185 * Prepare cast string as required
187 if ( bi->sql_upper_func.bv_val ) {
190 if ( BACKSQL_UPPER_NEEDS_CAST( bi ) ) {
191 snprintf( buf, sizeof( buf ),
192 "%s(cast (" /* ? as varchar(%d))) */ ,
193 bi->sql_upper_func.bv_val );
194 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
196 snprintf( buf, sizeof( buf ),
197 /* (cast(? */ " as varchar(%d)))",
198 BACKSQL_MAX_DN_LEN );
199 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_close );
202 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
203 bi->sql_upper_func.bv_val );
204 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
206 ber_str2bv( /* (? */ ")", 0, 1, &bi->sql_upper_func_close );
210 /* normalize filter values only if necessary */
211 bi->sql_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
212 assert( bi->sql_caseIgnoreMatch );
214 bi->sql_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
215 assert( bi->sql_telephoneNumberMatch );
217 if ( bi->sql_dbuser == NULL ) {
218 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
219 "user name not specified "
220 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
224 if ( bi->sql_subtree_cond.bv_val == NULL ) {
226 * Prepare concat function for subtree search condition
228 struct berval concat;
229 struct berval values[] = {
234 struct berbuf bb = BB_NULL;
236 if ( backsql_prepare_pattern( bi->sql_concat_func, values,
238 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
239 "unable to prepare CONCAT pattern", 0, 0, 0 );
243 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
244 "subtree search SQL condition not specified "
245 "(use \"subtree_cond\" directive in slapd.conf)\n",
248 if ( bi->sql_upper_func.bv_val ) {
251 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
254 backsql_strfcat( &bb, "blbbb",
256 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
257 "(ldap_entries.dn) LIKE ",
258 &bi->sql_upper_func_open,
260 &bi->sql_upper_func_close );
265 * ldap_entries.dn LIKE CONCAT('%',?)
268 backsql_strfcat( &bb, "lb",
269 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
270 "ldap_entries.dn LIKE ",
274 bi->sql_subtree_cond = bb.bb_val;
276 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
277 "setting \"%s\" as default\n",
278 bi->sql_subtree_cond.bv_val, 0, 0 );
281 if ( bi->sql_children_cond.bv_val == NULL ) {
282 struct berbuf bb = BB_NULL;
284 if ( bi->sql_upper_func.bv_val ) {
287 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
290 backsql_strfcat( &bb, "blbl",
292 (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
293 "(ldap_entries.dn)=",
295 (ber_len_t)STRLENOF( "(?)" ), "(?)" );
300 * ldap_entries.dn LIKE CONCAT('%,',?)
303 backsql_strfcat( &bb, "l",
304 (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
305 "ldap_entries.dn=?");
308 bi->sql_children_cond = bb.bb_val;
310 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
311 "setting \"%s\" as default\n",
312 bi->sql_children_cond.bv_val, 0, 0 );
315 if ( bi->sql_oc_query == NULL ) {
316 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
318 ch_strdup( backsql_def_needs_select_oc_query );
321 bi->sql_oc_query = ch_strdup( backsql_def_oc_query );
324 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
325 "objectclass mapping SQL statement not specified "
326 "(use \"oc_query\" directive in slapd.conf)\n",
328 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
329 "setting \"%s\" by default\n", bi->sql_oc_query, 0, 0 );
332 if ( bi->sql_at_query == NULL ) {
333 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
334 "attribute mapping SQL statement not specified "
335 "(use \"at_query\" directive in slapd.conf)\n",
337 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
338 "setting \"%s\" by default\n",
339 backsql_def_at_query, 0, 0 );
340 bi->sql_at_query = ch_strdup( backsql_def_at_query );
343 if ( bi->sql_insentry_query == NULL ) {
344 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
345 "entry insertion SQL statement not specified "
346 "(use \"insentry_query\" directive in slapd.conf)\n",
348 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
349 "setting \"%s\" by default\n",
350 backsql_def_insentry_query, 0, 0 );
351 bi->sql_insentry_query = ch_strdup( backsql_def_insentry_query );
354 if ( bi->sql_delentry_query == NULL ) {
355 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
356 "entry deletion SQL statement not specified "
357 "(use \"delentry_query\" directive in slapd.conf)\n",
359 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
360 "setting \"%s\" by default\n",
361 backsql_def_delentry_query, 0, 0 );
362 bi->sql_delentry_query = ch_strdup( backsql_def_delentry_query );
365 if ( bi->sql_delobjclasses_query == NULL ) {
366 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
367 "objclasses deletion SQL statement not specified "
368 "(use \"delobjclasses_query\" directive in slapd.conf)\n",
370 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
371 "setting \"%s\" by default\n",
372 backsql_def_delobjclasses_query, 0, 0 );
373 bi->sql_delobjclasses_query = ch_strdup( backsql_def_delobjclasses_query );
376 if ( bi->sql_delreferrals_query == NULL ) {
377 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
378 "referrals deletion SQL statement not specified "
379 "(use \"delreferrals_query\" directive in slapd.conf)\n",
381 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
382 "setting \"%s\" by default\n",
383 backsql_def_delreferrals_query, 0, 0 );
384 bi->sql_delreferrals_query = ch_strdup( backsql_def_delreferrals_query );
387 otmp.o_connid = (unsigned long)(-1);
389 if ( backsql_get_db_conn( &otmp, &dbh ) != LDAP_SUCCESS ) {
390 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
391 "connection failed, exiting\n", 0, 0, 0 );
396 * Prepare ID selection query
398 bi->sql_id_query = NULL;
401 if ( bi->sql_upper_func.bv_val == NULL ) {
402 backsql_strcat( &bb, backsql_id_query, "dn=?", NULL );
405 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
406 backsql_strcat( &bb, backsql_id_query,
409 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
410 backsql_strfcat( &bb, "sbl",
413 (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
415 backsql_strfcat( &bb, "sblbcb",
418 (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
419 &bi->sql_upper_func_open,
421 &bi->sql_upper_func_close );
425 bi->sql_id_query = bb.bb_val.bv_val;
428 * Prepare children ID selection query
430 bi->sql_has_children_query = NULL;
432 bb.bb_val.bv_val = NULL;
433 bb.bb_val.bv_len = 0;
435 backsql_strfcat( &bb, "sb",
436 "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries subordinates WHERE subordinates.parent=ldap_entries.id AND ",
438 &bi->sql_children_cond );
439 bi->sql_has_children_query = bb.bb_val.bv_val;
441 backsql_free_db_conn( &otmp );
442 if ( !BACKSQL_SCHEMA_LOADED( bi ) ) {
443 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
444 "test failed, schema map not loaded - exiting\n",
449 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
450 "test succeeded, schema map loaded\n", 0, 0, 0 );
458 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
459 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
464 backsql_connection_destroy( Backend *bd, Connection *c )
468 o.o_connid = c->c_connid;
470 Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
471 backsql_free_db_conn( &o );
472 Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
477 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
479 /* conditionally define the init_module() function */
480 SLAP_BACKEND_INIT_MODULE( sql )
482 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */