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 ) {
293 if ( si->upper_func.bv_val ) {
296 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
299 backsql_strfcat( &si->children_cond, &len, "blbl",
301 (ber_len_t)sizeof( "(ldap_entries.dn)=" ) - 1,
302 "(ldap_entries.dn)=",
304 (ber_len_t)sizeof( "(?)" ) - 1, "(?)" );
309 * ldap_entries.dn LIKE CONCAT('%,',?)
312 backsql_strfcat( &si->children_cond, &len, "l",
313 (ber_len_t)sizeof( "ldap_entries.dn=?" ) - 1,
314 "ldap_entries.dn=?");
317 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
318 "setting '%s' as default\n",
319 si->children_cond.bv_val, 0, 0 );
322 if ( si->oc_query == NULL ) {
323 if ( BACKSQL_CREATE_NEEDS_SELECT( si ) ) {
325 ch_strdup( backsql_def_needs_select_oc_query );
328 si->oc_query = ch_strdup( backsql_def_oc_query );
331 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
332 "objectclass mapping SQL statement not specified "
333 "(use \"oc_query\" directive in slapd.conf)\n",
335 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
336 "setting '%s' by default\n", si->oc_query, 0, 0 );
339 if ( si->at_query == NULL ) {
340 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
341 "attribute mapping SQL statement not specified "
342 "(use \"at_query\" directive in slapd.conf)\n",
344 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
345 "setting '%s' by default\n",
346 backsql_def_at_query, 0, 0 );
347 si->at_query = ch_strdup( backsql_def_at_query );
350 if ( si->insentry_query == NULL ) {
351 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
352 "entry insertion SQL statement not specified "
353 "(use \"insentry_query\" directive in slapd.conf)\n",
355 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
356 "setting '%s' by default\n",
357 backsql_def_insentry_query, 0, 0 );
358 si->insentry_query = ch_strdup( backsql_def_insentry_query );
361 if ( si->delentry_query == NULL ) {
362 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
363 "entry deletion SQL statement not specified "
364 "(use \"delentry_query\" directive in slapd.conf)\n",
366 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
367 "setting '%s' by default\n",
368 backsql_def_delentry_query, 0, 0 );
369 si->delentry_query = ch_strdup( backsql_def_delentry_query );
374 if ( backsql_get_db_conn( bd, &tmp, &dbh ) != LDAP_SUCCESS ) {
375 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
376 "connection failed, exiting\n", 0, 0, 0 );
381 * Prepare ID selection query
388 if ( si->upper_func.bv_val == NULL ) {
389 backsql_strcat( &bv, &idq_len, backsql_id_query,
392 if ( BACKSQL_HAS_LDAPINFO_DN_RU( si ) ) {
393 backsql_strcat( &bv, &idq_len, backsql_id_query,
396 if ( BACKSQL_USE_REVERSE_DN( si ) ) {
397 backsql_strfcat( &bv, &idq_len, "sbl",
400 (ber_len_t)sizeof( "(dn)=?" ) - 1, "(dn)=?" );
402 backsql_strfcat( &bv, &idq_len, "sblbcb",
405 (ber_len_t)sizeof( "(dn)=" ) - 1, "(dn)=",
406 &si->upper_func_open,
408 &si->upper_func_close );
412 si->id_query = bv.bv_val;
415 * Prepare children ID selection query
417 si->has_children_query = NULL;
422 backsql_strfcat( &bv, &idq_len, "sb",
423 "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries AS subordinates WHERE subordinates.parent=ldap_entries.id AND ",
425 &si->children_cond );
426 si->has_children_query = bv.bv_val;
428 backsql_free_db_conn( bd, &tmp );
429 if ( !BACKSQL_SCHEMA_LOADED( si ) ) {
430 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
431 "test failed, schema map not loaded - exiting\n",
436 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
437 "test succeeded, schema map loaded\n", 0, 0, 0 );
445 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
446 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
451 backsql_connection_destroy(
455 Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
456 backsql_free_db_conn( be, conn );
457 Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
461 #endif /* SLAPD_SQL */