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;
166 struct berbuf bb = BB_NULL;
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 /* normalize filter values only if necessary */
220 si->bi_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
222 if ( si->dbuser == NULL ) {
223 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
224 "user name not specified "
225 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
229 if ( si->subtree_cond.bv_val == NULL ) {
231 * Prepare concat function for subtree search condition
233 struct berval concat;
234 struct berval values[] = {
235 { sizeof( "'%'" ) - 1, "'%'" },
236 { sizeof( "?" ) - 1, "?" },
239 struct berbuf bb = BB_NULL;
241 if ( backsql_prepare_pattern( si->concat_func, values,
243 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
244 "unable to prepare CONCAT pattern", 0, 0, 0 );
248 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
249 "subtree search SQL condition not specified "
250 "(use \"subtree_cond\" directive in slapd.conf)\n",
253 if ( si->upper_func.bv_val ) {
256 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
259 backsql_strfcat( &bb, "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( &bb, "lb",
274 (ber_len_t)sizeof( "ldap_entries.dn LIKE " ) - 1,
275 "ldap_entries.dn LIKE ",
279 si->subtree_cond = bb.bb_val;
281 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
282 "setting '%s' as default\n",
283 si->subtree_cond.bv_val, 0, 0 );
286 if ( si->children_cond.bv_val == NULL ) {
287 struct berbuf bb = BB_NULL;
289 if ( si->upper_func.bv_val ) {
292 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
295 backsql_strfcat( &bb, "blbl",
297 (ber_len_t)sizeof( "(ldap_entries.dn)=" ) - 1,
298 "(ldap_entries.dn)=",
300 (ber_len_t)sizeof( "(?)" ) - 1, "(?)" );
305 * ldap_entries.dn LIKE CONCAT('%,',?)
308 backsql_strfcat( &bb, "l",
309 (ber_len_t)sizeof( "ldap_entries.dn=?" ) - 1,
310 "ldap_entries.dn=?");
313 si->children_cond = bb.bb_val;
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( &otmp, &dbh ) != LDAP_SUCCESS ) {
373 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
374 "connection failed, exiting\n", 0, 0, 0 );
379 * Prepare ID selection query
384 if ( si->upper_func.bv_val == NULL ) {
385 backsql_strcat( &bb, backsql_id_query, "dn=?", NULL );
387 if ( BACKSQL_HAS_LDAPINFO_DN_RU( si ) ) {
388 backsql_strcat( &bb, backsql_id_query,
391 if ( BACKSQL_USE_REVERSE_DN( si ) ) {
392 backsql_strfcat( &bb, "sbl",
395 (ber_len_t)sizeof( "(dn)=?" ) - 1, "(dn)=?" );
397 backsql_strfcat( &bb, "sblbcb",
400 (ber_len_t)sizeof( "(dn)=" ) - 1, "(dn)=",
401 &si->upper_func_open,
403 &si->upper_func_close );
407 si->id_query = bb.bb_val.bv_val;
410 * Prepare children ID selection query
412 si->has_children_query = NULL;
414 bb.bb_val.bv_val = NULL;
415 bb.bb_val.bv_len = 0;
417 backsql_strfcat( &bb, "sb",
418 "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries AS subordinates WHERE subordinates.parent=ldap_entries.id AND ",
420 &si->children_cond );
421 si->has_children_query = bb.bb_val.bv_val;
423 backsql_free_db_conn( &otmp );
424 if ( !BACKSQL_SCHEMA_LOADED( si ) ) {
425 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
426 "test failed, schema map not loaded - exiting\n",
431 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
432 "test succeeded, schema map loaded\n", 0, 0, 0 );
440 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
441 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
446 backsql_connection_destroy( Backend *bd, Connection *c )
450 o.o_connid = c->c_connid;
452 Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
453 backsql_free_db_conn( &o );
454 Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
458 #endif /* SLAPD_SQL */