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 bi->bi_op_abandon = 0;
72 bi->bi_op_compare = backsql_compare;
73 bi->bi_op_bind = backsql_bind;
75 bi->bi_op_search = backsql_search;
76 bi->bi_op_modify = backsql_modify;
77 bi->bi_op_modrdn = backsql_modrdn;
78 bi->bi_op_add = backsql_add;
79 bi->bi_op_delete = backsql_delete;
81 bi->bi_chk_referrals = 0;
82 bi->bi_operational = backsql_operational;
84 bi->bi_connection_init = 0;
85 bi->bi_connection_destroy = backsql_connection_destroy;
87 Debug( LDAP_DEBUG_TRACE,"<==backsql_initialize()\n", 0, 0, 0 );
96 Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
97 Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
107 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
108 si = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
109 memset( si, '\0', sizeof( backsql_info ) );
110 ldap_pvt_thread_mutex_init( &si->dbconn_mutex );
111 ldap_pvt_thread_mutex_init( &si->schema_mutex );
112 backsql_init_db_env( si );
115 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
123 backsql_info *si = (backsql_info*)bd->be_private;
125 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
126 ldap_pvt_thread_mutex_lock( &si->dbconn_mutex );
127 backsql_free_db_env( si );
128 ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex );
129 ldap_pvt_thread_mutex_destroy( &si->dbconn_mutex );
130 ldap_pvt_thread_mutex_lock( &si->schema_mutex );
131 backsql_destroy_schema_map( si );
132 ldap_pvt_thread_mutex_unlock( &si->schema_mutex );
133 ldap_pvt_thread_mutex_destroy( &si->schema_mutex );
136 if ( si->dbpasswd ) {
137 free( si->dbpasswd );
142 if ( si->upper_func.bv_val ) {
143 free( si->upper_func.bv_val );
144 free( si->upper_func_open.bv_val );
145 free( si->upper_func_close.bv_val );
148 free( si->subtree_cond.bv_val );
149 free( si->oc_query );
150 free( si->at_query );
151 free( si->insentry_query );
152 free( si->delentry_query );
155 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
163 backsql_info *si = (backsql_info*)bd->be_private;
170 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
171 "testing RDBMS connection\n", 0, 0, 0 );
172 if ( si->dbname == NULL ) {
173 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
174 "datasource name not specified "
175 "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
179 if ( si->concat_func == NULL ) {
180 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
181 "concat func not specified (use \"concat_pattern\" "
182 "directive in slapd.conf)\n", 0, 0, 0 );
184 if ( backsql_split_pattern( backsql_def_concat_func,
185 &si->concat_func, 2 ) ) {
186 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
187 "unable to parse pattern '%s'",
188 backsql_def_concat_func, 0, 0 );
194 * Prepare cast string as required
196 if ( si->upper_func.bv_val ) {
199 if ( BACKSQL_UPPER_NEEDS_CAST( si ) ) {
200 snprintf( buf, sizeof( buf ),
201 "%s(cast (" /* ? as varchar(%d))) */ ,
202 si->upper_func.bv_val );
203 ber_str2bv( buf, 0, 1, &si->upper_func_open );
205 snprintf( buf, sizeof( buf ),
206 /* (cast(? */ " as varchar(%d)))",
207 BACKSQL_MAX_DN_LEN );
208 ber_str2bv( buf, 0, 1, &si->upper_func_close );
211 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
212 si->upper_func.bv_val );
213 ber_str2bv( buf, 0, 1, &si->upper_func_open );
215 ber_str2bv( /* (? */ ")", 0, 1, &si->upper_func_close );
219 if ( si->dbuser == NULL ) {
220 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
221 "user name not specified "
222 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
226 if ( si->subtree_cond.bv_val == NULL ) {
228 * Prepare concat function for subtree search condition
230 struct berval concat;
232 struct berval values[] = {
233 { sizeof( "'%'" ) - 1, "'%'" },
234 { sizeof( "?" ) - 1, "?" },
238 if ( backsql_prepare_pattern( si->concat_func, values,
240 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
241 "unable to prepare CONCAT pattern", 0, 0, 0 );
245 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
246 "subtree search SQL condition not specified "
247 "(use \"subtree_cond\" directive in slapd.conf)\n",
250 si->subtree_cond.bv_val = NULL;
251 si->subtree_cond.bv_len = 0;
253 if ( si->upper_func.bv_val ) {
256 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
259 backsql_strfcat( &si->subtree_cond, &len, "blbbb",
261 (ber_len_t)sizeof( "(ldap_entries.dn) LIKE " ) - 1,
262 "(ldap_entries.dn) LIKE ",
263 &si->upper_func_open,
265 &si->upper_func_close );
270 * ldap_entries.dn LIKE CONCAT('%',?)
273 backsql_strfcat( &si->subtree_cond, &len, "lb",
274 (ber_len_t)sizeof( "ldap_entries.dn LIKE " ) - 1,
275 "ldap_entries.dn LIKE ",
279 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
280 "setting '%s' as default\n",
281 si->subtree_cond.bv_val, 0, 0 );
284 if ( si->children_cond.bv_val == NULL ) {
287 if ( si->upper_func.bv_val ) {
290 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
293 backsql_strfcat( &si->children_cond, &len, "blbl",
295 (ber_len_t)sizeof( "(ldap_entries.dn)=" ) - 1,
296 "(ldap_entries.dn)=",
298 (ber_len_t)sizeof( "(?)" ) - 1, "(?)" );
303 * ldap_entries.dn LIKE CONCAT('%,',?)
306 backsql_strfcat( &si->children_cond, &len, "l",
307 (ber_len_t)sizeof( "ldap_entries.dn=?" ) - 1,
308 "ldap_entries.dn=?");
311 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
312 "setting '%s' as default\n",
313 si->children_cond.bv_val, 0, 0 );
316 if ( si->oc_query == NULL ) {
317 if ( BACKSQL_CREATE_NEEDS_SELECT( si ) ) {
319 ch_strdup( backsql_def_needs_select_oc_query );
322 si->oc_query = ch_strdup( backsql_def_oc_query );
325 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
326 "objectclass mapping SQL statement not specified "
327 "(use \"oc_query\" directive in slapd.conf)\n",
329 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
330 "setting '%s' by default\n", si->oc_query, 0, 0 );
333 if ( si->at_query == NULL ) {
334 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
335 "attribute mapping SQL statement not specified "
336 "(use \"at_query\" directive in slapd.conf)\n",
338 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
339 "setting '%s' by default\n",
340 backsql_def_at_query, 0, 0 );
341 si->at_query = ch_strdup( backsql_def_at_query );
344 if ( si->insentry_query == NULL ) {
345 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
346 "entry insertion SQL statement not specified "
347 "(use \"insentry_query\" directive in slapd.conf)\n",
349 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
350 "setting '%s' by default\n",
351 backsql_def_insentry_query, 0, 0 );
352 si->insentry_query = ch_strdup( backsql_def_insentry_query );
355 if ( si->delentry_query == NULL ) {
356 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
357 "entry deletion SQL statement not specified "
358 "(use \"delentry_query\" directive in slapd.conf)\n",
360 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
361 "setting '%s' by default\n",
362 backsql_def_delentry_query, 0, 0 );
363 si->delentry_query = ch_strdup( backsql_def_delentry_query );
368 if ( backsql_get_db_conn( &otmp, &dbh ) != LDAP_SUCCESS ) {
369 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
370 "connection failed, exiting\n", 0, 0, 0 );
375 * Prepare ID selection query
382 if ( si->upper_func.bv_val == NULL ) {
383 backsql_strcat( &bv, &idq_len, backsql_id_query,
386 if ( BACKSQL_HAS_LDAPINFO_DN_RU( si ) ) {
387 backsql_strcat( &bv, &idq_len, backsql_id_query,
390 if ( BACKSQL_USE_REVERSE_DN( si ) ) {
391 backsql_strfcat( &bv, &idq_len, "sbl",
394 (ber_len_t)sizeof( "(dn)=?" ) - 1, "(dn)=?" );
396 backsql_strfcat( &bv, &idq_len, "sblbcb",
399 (ber_len_t)sizeof( "(dn)=" ) - 1, "(dn)=",
400 &si->upper_func_open,
402 &si->upper_func_close );
406 si->id_query = bv.bv_val;
409 * Prepare children ID selection query
411 si->has_children_query = NULL;
416 backsql_strfcat( &bv, &idq_len, "sb",
417 "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries AS subordinates WHERE subordinates.parent=ldap_entries.id AND ",
419 &si->children_cond );
420 si->has_children_query = bv.bv_val;
422 backsql_free_db_conn( &otmp );
423 if ( !BACKSQL_SCHEMA_LOADED( si ) ) {
424 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
425 "test failed, schema map not loaded - exiting\n",
430 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
431 "test succeeded, schema map loaded\n", 0, 0, 0 );
439 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
440 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
445 backsql_connection_destroy( Backend *bd, Connection *c )
449 o.o_connid = c->c_connid;
451 Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
452 backsql_free_db_conn( &o );
453 Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
457 #endif /* SLAPD_SQL */