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 = sql_back_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;
86 bi->bi_chk_referrals = 0;
87 bi->bi_operational = backsql_operational;
89 bi->bi_connection_init = 0;
90 bi->bi_connection_destroy = backsql_connection_destroy;
92 Debug( LDAP_DEBUG_TRACE,"<==backsql_initialize()\n", 0, 0, 0 );
101 Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
102 Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
112 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
113 si = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
114 memset( si, '\0', sizeof( backsql_info ) );
115 ldap_pvt_thread_mutex_init( &si->dbconn_mutex );
116 ldap_pvt_thread_mutex_init( &si->schema_mutex );
117 backsql_init_db_env( si );
120 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
128 backsql_info *si = (backsql_info*)bd->be_private;
130 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
131 ldap_pvt_thread_mutex_lock( &si->dbconn_mutex );
132 backsql_free_db_env( si );
133 ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex );
134 ldap_pvt_thread_mutex_destroy( &si->dbconn_mutex );
135 ldap_pvt_thread_mutex_lock( &si->schema_mutex );
136 backsql_destroy_schema_map( si );
137 ldap_pvt_thread_mutex_unlock( &si->schema_mutex );
138 ldap_pvt_thread_mutex_destroy( &si->schema_mutex );
141 if ( si->dbpasswd ) {
142 free( si->dbpasswd );
147 if ( si->upper_func.bv_val ) {
148 free( si->upper_func.bv_val );
149 free( si->upper_func_open.bv_val );
150 free( si->upper_func_close.bv_val );
153 free( si->subtree_cond.bv_val );
154 free( si->oc_query );
155 free( si->at_query );
156 free( si->insentry_query );
157 free( si->delentry_query );
160 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
168 backsql_info *si = (backsql_info*)bd->be_private;
174 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
175 "testing RDBMS connection\n", 0, 0, 0 );
176 if ( si->dbname == NULL ) {
177 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
178 "datasource name not specified "
179 "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
183 if ( si->concat_func == NULL ) {
184 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
185 "concat func not specified (use \"concat_pattern\" "
186 "directive in slapd.conf)\n", 0, 0, 0 );
188 if ( backsql_split_pattern( backsql_def_concat_func,
189 &si->concat_func, 2 ) ) {
190 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
191 "unable to parse pattern '%s'",
192 backsql_def_concat_func, 0, 0 );
198 * Prepare cast string as required
200 if ( si->upper_func.bv_val ) {
203 if ( BACKSQL_UPPER_NEEDS_CAST( si ) ) {
204 snprintf( buf, sizeof( buf ),
205 "%s(cast (" /* ? as varchar(%d))) */ ,
206 si->upper_func.bv_val );
207 ber_str2bv( buf, 0, 1, &si->upper_func_open );
209 snprintf( buf, sizeof( buf ),
210 /* (cast(? */ " as varchar(%d)))",
211 BACKSQL_MAX_DN_LEN );
212 ber_str2bv( buf, 0, 1, &si->upper_func_close );
215 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
216 si->upper_func.bv_val );
217 ber_str2bv( buf, 0, 1, &si->upper_func_open );
219 ber_str2bv( /* (? */ ")", 0, 1, &si->upper_func_close );
223 if ( si->dbuser == NULL ) {
224 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
225 "user name not specified "
226 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
230 if ( si->subtree_cond.bv_val == NULL ) {
232 * Prepare concat function for subtree search condition
234 struct berval concat;
236 struct berval values[] = {
237 { sizeof( "'%'" ) - 1, "'%'" },
238 { sizeof( "?" ) - 1, "?" },
242 if ( backsql_prepare_pattern( si->concat_func, values,
244 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
245 "unable to prepare CONCAT pattern", 0, 0, 0 );
249 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
250 "subtree search SQL condition not specified "
251 "(use \"subtree_cond\" directive in slapd.conf)\n",
254 si->subtree_cond.bv_val = NULL;
255 si->subtree_cond.bv_len = 0;
257 if ( si->upper_func.bv_val ) {
260 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
263 backsql_strfcat( &si->subtree_cond, &len, "blbbb",
265 (ber_len_t)sizeof( "(ldap_entries.dn) LIKE " ) - 1,
266 "(ldap_entries.dn) LIKE ",
267 &si->upper_func_open,
269 &si->upper_func_close );
274 * ldap_entries.dn LIKE CONCAT('%',?)
277 backsql_strfcat( &si->subtree_cond, &len, "lb",
278 (ber_len_t)sizeof( "ldap_entries.dn LIKE " ) - 1,
279 "ldap_entries.dn LIKE ",
283 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
284 "setting '%s' as default\n",
285 si->subtree_cond.bv_val, 0, 0 );
288 if ( si->children_cond.bv_val == NULL ) {
291 if ( si->upper_func.bv_val ) {
294 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
297 backsql_strfcat( &si->children_cond, &len, "blbl",
299 (ber_len_t)sizeof( "(ldap_entries.dn)=" ) - 1,
300 "(ldap_entries.dn)=",
302 (ber_len_t)sizeof( "(?)" ) - 1, "(?)" );
307 * ldap_entries.dn LIKE CONCAT('%,',?)
310 backsql_strfcat( &si->children_cond, &len, "l",
311 (ber_len_t)sizeof( "ldap_entries.dn=?" ) - 1,
312 "ldap_entries.dn=?");
315 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
316 "setting '%s' as default\n",
317 si->children_cond.bv_val, 0, 0 );
320 if ( si->oc_query == NULL ) {
321 if ( BACKSQL_CREATE_NEEDS_SELECT( si ) ) {
323 ch_strdup( backsql_def_needs_select_oc_query );
326 si->oc_query = ch_strdup( backsql_def_oc_query );
329 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
330 "objectclass mapping SQL statement not specified "
331 "(use \"oc_query\" directive in slapd.conf)\n",
333 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
334 "setting '%s' by default\n", si->oc_query, 0, 0 );
337 if ( si->at_query == NULL ) {
338 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
339 "attribute mapping SQL statement not specified "
340 "(use \"at_query\" directive in slapd.conf)\n",
342 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
343 "setting '%s' by default\n",
344 backsql_def_at_query, 0, 0 );
345 si->at_query = ch_strdup( backsql_def_at_query );
348 if ( si->insentry_query == NULL ) {
349 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
350 "entry insertion SQL statement not specified "
351 "(use \"insentry_query\" directive in slapd.conf)\n",
353 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
354 "setting '%s' by default\n",
355 backsql_def_insentry_query, 0, 0 );
356 si->insentry_query = ch_strdup( backsql_def_insentry_query );
359 if ( si->delentry_query == NULL ) {
360 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
361 "entry deletion SQL statement not specified "
362 "(use \"delentry_query\" directive in slapd.conf)\n",
364 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
365 "setting '%s' by default\n",
366 backsql_def_delentry_query, 0, 0 );
367 si->delentry_query = ch_strdup( backsql_def_delentry_query );
372 if ( backsql_get_db_conn( bd, &tmp, &dbh ) != LDAP_SUCCESS ) {
373 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
374 "connection failed, exiting\n", 0, 0, 0 );
379 * Prepare ID selection query
386 if ( si->upper_func.bv_val == NULL ) {
387 backsql_strcat( &bv, &idq_len, backsql_id_query,
390 if ( BACKSQL_HAS_LDAPINFO_DN_RU( si ) ) {
391 backsql_strcat( &bv, &idq_len, backsql_id_query,
394 if ( BACKSQL_USE_REVERSE_DN( si ) ) {
395 backsql_strfcat( &bv, &idq_len, "sbl",
398 (ber_len_t)sizeof( "(dn)=?" ) - 1, "(dn)=?" );
400 backsql_strfcat( &bv, &idq_len, "sblbcb",
403 (ber_len_t)sizeof( "(dn)=" ) - 1, "(dn)=",
404 &si->upper_func_open,
406 &si->upper_func_close );
410 si->id_query = bv.bv_val;
413 * Prepare children ID selection query
415 si->has_children_query = NULL;
420 backsql_strfcat( &bv, &idq_len, "sb",
421 "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries AS subordinates WHERE subordinates.parent=ldap_entries.id AND ",
423 &si->children_cond );
424 si->has_children_query = bv.bv_val;
426 backsql_free_db_conn( bd, &tmp );
427 if ( !BACKSQL_SCHEMA_LOADED( si ) ) {
428 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
429 "test failed, schema map not loaded - exiting\n",
434 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
435 "test succeeded, schema map loaded\n", 0, 0, 0 );
443 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
444 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
449 backsql_connection_destroy(
453 Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
454 backsql_free_db_conn( be, conn );
455 Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
459 #endif /* SLAPD_SQL */