2 * Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
4 * Redistribution and use in source and binary forms are permitted only
5 * as authorized by the OpenLDAP Public License. A copy of this
6 * license is available at http://www.OpenLDAP.org/license.html or
7 * in file LICENSE in the top-level directory of the distribution.
15 #include <sys/types.h>
20 #include "schema-map.h"
23 #ifdef SLAPD_SQL_DYNAMIC
26 backsql_LTX_init_module(
32 memset( &bi, '\0', sizeof( bi ) );
34 bi.bi_init = backbacksql_initialize;
40 #endif /* SLAPD_SHELL_DYNAMIC */
46 static char *controls[] = {
47 #ifdef LDAP_CONTROL_NOOP
50 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
51 LDAP_CONTROL_VALUESRETURNFILTER,
56 bi->bi_controls = controls;
58 Debug( LDAP_DEBUG_TRACE,"==>backsql_initialize()\n", 0, 0, 0 );
65 bi->bi_db_init = backsql_db_init;
66 bi->bi_db_config = backsql_db_config;
67 bi->bi_db_open = backsql_db_open;
68 bi->bi_db_close = backsql_db_close;
69 bi->bi_db_destroy = backsql_db_destroy;
71 #ifdef BACKSQL_ALL_DONE
72 bi->bi_op_abandon = backsql_abandon;
73 bi->bi_op_compare = backsql_compare;
75 bi->bi_op_abandon = 0;
76 bi->bi_op_compare = 0;
78 bi->bi_op_bind = backsql_bind;
79 bi->bi_op_unbind = backsql_unbind;
80 bi->bi_op_search = backsql_search;
81 bi->bi_op_modify = backsql_modify;
82 bi->bi_op_modrdn = backsql_modrdn;
83 bi->bi_op_add = backsql_add;
84 bi->bi_op_delete = backsql_delete;
87 bi->bi_acl_attribute = 0;
88 bi->bi_chk_referrals = 0;
89 bi->bi_operational = backsql_operational;
91 bi->bi_connection_init = 0;
92 bi->bi_connection_destroy = backsql_connection_destroy;
94 Debug( LDAP_DEBUG_TRACE,"<==backsql_initialize()\n", 0, 0, 0 );
103 Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
104 Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
114 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
115 si = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
116 memset( si, '\0', sizeof( backsql_info ) );
117 ldap_pvt_thread_mutex_init( &si->dbconn_mutex );
118 ldap_pvt_thread_mutex_init( &si->schema_mutex );
119 backsql_init_db_env( si );
122 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
130 backsql_info *si = (backsql_info*)bd->be_private;
132 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
133 ldap_pvt_thread_mutex_lock( &si->dbconn_mutex );
134 backsql_free_db_env( si );
135 ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex );
136 ldap_pvt_thread_mutex_destroy( &si->dbconn_mutex );
137 ldap_pvt_thread_mutex_lock( &si->schema_mutex );
138 backsql_destroy_schema_map( si );
139 ldap_pvt_thread_mutex_unlock( &si->schema_mutex );
140 ldap_pvt_thread_mutex_destroy( &si->schema_mutex );
143 if ( si->dbpasswd ) {
144 free( si->dbpasswd );
149 if ( si->upper_func.bv_val ) {
150 free( si->upper_func.bv_val );
151 free( si->upper_func_open.bv_val );
152 free( si->upper_func_close.bv_val );
155 free( si->subtree_cond.bv_val );
156 free( si->oc_query );
157 free( si->at_query );
158 free( si->insentry_query );
159 free( si->delentry_query );
162 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
170 backsql_info *si = (backsql_info*)bd->be_private;
176 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
177 "testing RDBMS connection\n", 0, 0, 0 );
178 if ( si->dbname == NULL ) {
179 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
180 "datasource name not specified "
181 "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
185 if ( si->concat_func == NULL ) {
186 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
187 "concat func not specified (use \"concat_pattern\" "
188 "directive in slapd.conf)\n", 0, 0, 0 );
190 if ( backsql_split_pattern( backsql_def_concat_func,
191 &si->concat_func, 2 ) ) {
192 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
193 "unable to parse pattern '%s'",
194 backsql_def_concat_func, 0, 0 );
200 * Prepare cast string as required
202 if ( si->upper_func.bv_val ) {
205 if ( BACKSQL_UPPER_NEEDS_CAST( si ) ) {
206 snprintf( buf, sizeof( buf ),
207 "%s(cast (" /* ? as varchar(%d))) */ ,
208 si->upper_func.bv_val );
209 ber_str2bv( buf, 0, 1, &si->upper_func_open );
211 snprintf( buf, sizeof( buf ),
212 /* (cast(? */ " as varchar(%d)))",
213 BACKSQL_MAX_DN_LEN );
214 ber_str2bv( buf, 0, 1, &si->upper_func_close );
217 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
218 si->upper_func.bv_val );
219 ber_str2bv( buf, 0, 1, &si->upper_func_open );
221 ber_str2bv( /* (? */ ")", 0, 1, &si->upper_func_close );
225 if ( si->dbuser == NULL ) {
226 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
227 "user name not specified "
228 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
232 if ( si->subtree_cond.bv_val == NULL ) {
234 * Prepare concat function for subtree search condition
236 struct berval concat;
238 struct berval values[] = {
239 { sizeof( "'%'" ) - 1, "'%'" },
240 { sizeof( "?" ) - 1, "?" },
244 if ( backsql_prepare_pattern( si->concat_func, values,
246 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
247 "unable to prepare CONCAT pattern", 0, 0, 0 );
251 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
252 "subtree search SQL condition not specified "
253 "(use \"subtree_cond\" directive in slapd.conf)\n",
256 si->subtree_cond.bv_val = NULL;
257 si->subtree_cond.bv_len = 0;
259 if ( si->upper_func.bv_val ) {
262 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
265 backsql_strfcat( &si->subtree_cond, &len, "blbbb",
267 (ber_len_t)sizeof( "(ldap_entries.dn) LIKE " ) - 1,
268 "(ldap_entries.dn) LIKE ",
269 &si->upper_func_open,
271 &si->upper_func_close );
276 * ldap_entries.dn LIKE CONCAT('%',?)
279 backsql_strfcat( &si->subtree_cond, &len, "lb",
280 (ber_len_t)sizeof( "ldap_entries.dn LIKE " ) - 1,
281 "ldap_entries.dn LIKE ",
285 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
286 "setting '%s' as default\n",
287 si->subtree_cond.bv_val, 0, 0 );
290 if ( si->children_cond.bv_val == NULL ) {
292 * Prepare concat function for children search condition
294 struct berval concat;
296 struct berval values[] = {
297 { sizeof( "'%,'" ) - 1, "'%,'" },
298 { sizeof( "?" ) - 1, "?" },
302 if ( backsql_prepare_pattern( si->concat_func, values,
304 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
305 "unable to prepare CONCAT pattern", 0, 0, 0 );
309 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
310 "children search SQL condition not specified "
311 "(use \"children_cond\" directive in slapd.conf)\n",
314 si->children_cond.bv_val = NULL;
315 si->children_cond.bv_len = 0;
317 if ( si->upper_func.bv_val ) {
320 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
323 backsql_strfcat( &si->children_cond, &len, "blbbb",
325 (ber_len_t)sizeof( "(ldap_entries.dn) LIKE " ) - 1,
326 "(ldap_entries.dn) LIKE ",
327 &si->upper_func_open,
329 &si->upper_func_close );
334 * ldap_entries.dn LIKE CONCAT('%,',?)
337 backsql_strfcat( &si->children_cond, &len, "lb",
338 (ber_len_t)sizeof( "ldap_entries.dn LIKE " ) - 1,
339 "ldap_entries.dn LIKE ",
343 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
344 "setting '%s' as default\n",
345 si->children_cond.bv_val, 0, 0 );
348 if ( si->oc_query == NULL ) {
349 if ( BACKSQL_CREATE_NEEDS_SELECT( si ) ) {
351 ch_strdup( backsql_def_needs_select_oc_query );
354 si->oc_query = ch_strdup( backsql_def_oc_query );
357 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
358 "objectclass mapping SQL statement not specified "
359 "(use \"oc_query\" directive in slapd.conf)\n",
361 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
362 "setting '%s' by default\n", si->oc_query, 0, 0 );
365 if ( si->at_query == NULL ) {
366 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
367 "attribute mapping SQL statement not specified "
368 "(use \"at_query\" directive in slapd.conf)\n",
370 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
371 "setting '%s' by default\n",
372 backsql_def_at_query, 0, 0 );
373 si->at_query = ch_strdup( backsql_def_at_query );
376 if ( si->insentry_query == NULL ) {
377 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
378 "entry insertion SQL statement not specified "
379 "(use \"insentry_query\" directive in slapd.conf)\n",
381 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
382 "setting '%s' by default\n",
383 backsql_def_insentry_query, 0, 0 );
384 si->insentry_query = ch_strdup( backsql_def_insentry_query );
387 if ( si->delentry_query == NULL ) {
388 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
389 "entry deletion SQL statement not specified "
390 "(use \"delentry_query\" directive in slapd.conf)\n",
392 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
393 "setting '%s' by default\n",
394 backsql_def_delentry_query, 0, 0 );
395 si->delentry_query = ch_strdup( backsql_def_delentry_query );
400 if ( backsql_get_db_conn( bd, &tmp, &dbh ) != LDAP_SUCCESS ) {
401 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
402 "connection failed, exiting\n", 0, 0, 0 );
407 * Prepare ID selection query
414 if ( si->upper_func.bv_val == NULL ) {
415 backsql_strcat( &bv, &idq_len, backsql_id_query,
418 if ( BACKSQL_HAS_LDAPINFO_DN_RU( si ) ) {
419 backsql_strcat( &bv, &idq_len, backsql_id_query,
422 if ( BACKSQL_USE_REVERSE_DN( si ) ) {
423 backsql_strfcat( &bv, &idq_len, "sbl",
426 (ber_len_t)sizeof( "(dn)=?" ) - 1, "(dn)=?" );
428 backsql_strfcat( &bv, &idq_len, "sblbcb",
431 (ber_len_t)sizeof( "(dn)=" ) - 1, "(dn)=",
432 &si->upper_func_open,
434 &si->upper_func_close );
438 si->id_query = bv.bv_val;
441 * Prepare children ID selection query
443 si->has_children_query = NULL;
448 backsql_strfcat( &bv, &idq_len, "sb",
449 "select count(*) from ldap_entries where ",
450 &si->children_cond );
451 si->has_children_query = bv.bv_val;
453 backsql_free_db_conn( bd, &tmp );
454 if ( !BACKSQL_SCHEMA_LOADED( si ) ) {
455 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
456 "test failed, schema map not loaded - exiting\n",
461 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
462 "test succeeded, schema map loaded\n", 0, 0, 0 );
470 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
471 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
476 backsql_connection_destroy(
480 Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
481 backsql_free_db_conn( be, conn );
482 Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
486 #endif /* SLAPD_SQL */