2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Dmitry Kovalev for inclusion
18 * by OpenLDAP Software.
26 #include <sys/types.h>
27 #include "ac/string.h"
31 #include "proto-sql.h"
33 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
42 memset( &bi, '\0', sizeof( bi ) );
44 bi.bi_init = sql_back_initialize;
50 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */
56 static char *controls[] = {
57 #if 0 /* needs updating */
58 #ifdef LDAP_CONTROL_NOOP
60 #endif /* LDAP_CONTROL_NOOP */
62 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
63 LDAP_CONTROL_VALUESRETURNFILTER,
64 #endif /* LDAP_CONTROL_VALUESRETURNFILTER */
68 bi->bi_controls = controls;
70 Debug( LDAP_DEBUG_TRACE,"==>backsql_initialize()\n", 0, 0, 0 );
77 bi->bi_db_init = backsql_db_init;
78 bi->bi_db_config = backsql_db_config;
79 bi->bi_db_open = backsql_db_open;
80 bi->bi_db_close = backsql_db_close;
81 bi->bi_db_destroy = backsql_db_destroy;
83 bi->bi_op_abandon = 0;
84 bi->bi_op_compare = backsql_compare;
85 bi->bi_op_bind = backsql_bind;
87 bi->bi_op_search = backsql_search;
88 bi->bi_op_modify = backsql_modify;
89 bi->bi_op_modrdn = backsql_modrdn;
90 bi->bi_op_add = backsql_add;
91 bi->bi_op_delete = backsql_delete;
93 bi->bi_chk_referrals = 0;
94 bi->bi_operational = backsql_operational;
96 bi->bi_connection_init = 0;
97 bi->bi_connection_destroy = backsql_connection_destroy;
99 Debug( LDAP_DEBUG_TRACE,"<==backsql_initialize()\n", 0, 0, 0 );
108 Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
109 Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
119 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
120 si = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
121 memset( si, '\0', sizeof( backsql_info ) );
122 ldap_pvt_thread_mutex_init( &si->dbconn_mutex );
123 ldap_pvt_thread_mutex_init( &si->schema_mutex );
124 backsql_init_db_env( si );
127 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
135 backsql_info *si = (backsql_info*)bd->be_private;
137 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
138 ldap_pvt_thread_mutex_lock( &si->dbconn_mutex );
139 backsql_free_db_env( si );
140 ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex );
141 ldap_pvt_thread_mutex_destroy( &si->dbconn_mutex );
142 ldap_pvt_thread_mutex_lock( &si->schema_mutex );
143 backsql_destroy_schema_map( si );
144 ldap_pvt_thread_mutex_unlock( &si->schema_mutex );
145 ldap_pvt_thread_mutex_destroy( &si->schema_mutex );
148 if ( si->dbpasswd ) {
149 free( si->dbpasswd );
154 if ( si->upper_func.bv_val ) {
155 free( si->upper_func.bv_val );
156 free( si->upper_func_open.bv_val );
157 free( si->upper_func_close.bv_val );
160 free( si->subtree_cond.bv_val );
161 free( si->oc_query );
162 free( si->at_query );
163 free( si->insentry_query );
164 free( si->delentry_query );
167 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
175 backsql_info *si = (backsql_info*)bd->be_private;
178 struct berbuf bb = BB_NULL;
182 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
183 "testing RDBMS connection\n", 0, 0, 0 );
184 if ( si->dbname == NULL ) {
185 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
186 "datasource name not specified "
187 "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
191 if ( si->concat_func == NULL ) {
192 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
193 "concat func not specified (use \"concat_pattern\" "
194 "directive in slapd.conf)\n", 0, 0, 0 );
196 if ( backsql_split_pattern( backsql_def_concat_func,
197 &si->concat_func, 2 ) ) {
198 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
199 "unable to parse pattern \"%s\"",
200 backsql_def_concat_func, 0, 0 );
206 * Prepare cast string as required
208 if ( si->upper_func.bv_val ) {
211 if ( BACKSQL_UPPER_NEEDS_CAST( si ) ) {
212 snprintf( buf, sizeof( buf ),
213 "%s(cast (" /* ? as varchar(%d))) */ ,
214 si->upper_func.bv_val );
215 ber_str2bv( buf, 0, 1, &si->upper_func_open );
217 snprintf( buf, sizeof( buf ),
218 /* (cast(? */ " as varchar(%d)))",
219 BACKSQL_MAX_DN_LEN );
220 ber_str2bv( buf, 0, 1, &si->upper_func_close );
223 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
224 si->upper_func.bv_val );
225 ber_str2bv( buf, 0, 1, &si->upper_func_open );
227 ber_str2bv( /* (? */ ")", 0, 1, &si->upper_func_close );
231 /* normalize filter values only if necessary */
232 si->bi_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
233 assert( si->bi_caseIgnoreMatch );
235 si->bi_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
236 assert( si->bi_telephoneNumberMatch );
238 if ( si->dbuser == NULL ) {
239 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
240 "user name not specified "
241 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
245 if ( si->subtree_cond.bv_val == NULL ) {
247 * Prepare concat function for subtree search condition
249 struct berval concat;
250 struct berval values[] = {
255 struct berbuf bb = BB_NULL;
257 if ( backsql_prepare_pattern( si->concat_func, values,
259 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
260 "unable to prepare CONCAT pattern", 0, 0, 0 );
264 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
265 "subtree search SQL condition not specified "
266 "(use \"subtree_cond\" directive in slapd.conf)\n",
269 if ( si->upper_func.bv_val ) {
272 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
275 backsql_strfcat( &bb, "blbbb",
277 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
278 "(ldap_entries.dn) LIKE ",
279 &si->upper_func_open,
281 &si->upper_func_close );
286 * ldap_entries.dn LIKE CONCAT('%',?)
289 backsql_strfcat( &bb, "lb",
290 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
291 "ldap_entries.dn LIKE ",
295 si->subtree_cond = bb.bb_val;
297 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
298 "setting \"%s\" as default\n",
299 si->subtree_cond.bv_val, 0, 0 );
302 if ( si->children_cond.bv_val == NULL ) {
303 struct berbuf bb = BB_NULL;
305 if ( si->upper_func.bv_val ) {
308 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
311 backsql_strfcat( &bb, "blbl",
313 (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
314 "(ldap_entries.dn)=",
316 (ber_len_t)STRLENOF( "(?)" ), "(?)" );
321 * ldap_entries.dn LIKE CONCAT('%,',?)
324 backsql_strfcat( &bb, "l",
325 (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
326 "ldap_entries.dn=?");
329 si->children_cond = bb.bb_val;
331 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
332 "setting \"%s\" as default\n",
333 si->children_cond.bv_val, 0, 0 );
336 if ( si->oc_query == NULL ) {
337 if ( BACKSQL_CREATE_NEEDS_SELECT( si ) ) {
339 ch_strdup( backsql_def_needs_select_oc_query );
342 si->oc_query = ch_strdup( backsql_def_oc_query );
345 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
346 "objectclass mapping SQL statement not specified "
347 "(use \"oc_query\" directive in slapd.conf)\n",
349 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
350 "setting \"%s\" by default\n", si->oc_query, 0, 0 );
353 if ( si->at_query == NULL ) {
354 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
355 "attribute mapping SQL statement not specified "
356 "(use \"at_query\" directive in slapd.conf)\n",
358 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
359 "setting \"%s\" by default\n",
360 backsql_def_at_query, 0, 0 );
361 si->at_query = ch_strdup( backsql_def_at_query );
364 if ( si->insentry_query == NULL ) {
365 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
366 "entry insertion SQL statement not specified "
367 "(use \"insentry_query\" directive in slapd.conf)\n",
369 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
370 "setting \"%s\" by default\n",
371 backsql_def_insentry_query, 0, 0 );
372 si->insentry_query = ch_strdup( backsql_def_insentry_query );
375 if ( si->delentry_query == NULL ) {
376 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
377 "entry deletion SQL statement not specified "
378 "(use \"delentry_query\" directive in slapd.conf)\n",
380 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
381 "setting \"%s\" by default\n",
382 backsql_def_delentry_query, 0, 0 );
383 si->delentry_query = ch_strdup( backsql_def_delentry_query );
388 if ( backsql_get_db_conn( &otmp, &dbh ) != LDAP_SUCCESS ) {
389 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
390 "connection failed, exiting\n", 0, 0, 0 );
395 * Prepare ID selection query
400 if ( si->upper_func.bv_val == NULL ) {
401 backsql_strcat( &bb, backsql_id_query, "dn=?", NULL );
403 if ( BACKSQL_HAS_LDAPINFO_DN_RU( si ) ) {
404 backsql_strcat( &bb, backsql_id_query,
407 if ( BACKSQL_USE_REVERSE_DN( si ) ) {
408 backsql_strfcat( &bb, "sbl",
411 (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
413 backsql_strfcat( &bb, "sblbcb",
416 (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
417 &si->upper_func_open,
419 &si->upper_func_close );
423 si->id_query = bb.bb_val.bv_val;
426 * Prepare children ID selection query
428 si->has_children_query = NULL;
430 bb.bb_val.bv_val = NULL;
431 bb.bb_val.bv_len = 0;
433 backsql_strfcat( &bb, "sb",
434 "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries subordinates WHERE subordinates.parent=ldap_entries.id AND ",
436 &si->children_cond );
437 si->has_children_query = bb.bb_val.bv_val;
439 backsql_free_db_conn( &otmp );
440 if ( !BACKSQL_SCHEMA_LOADED( si ) ) {
441 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
442 "test failed, schema map not loaded - exiting\n",
447 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
448 "test succeeded, schema map loaded\n", 0, 0, 0 );
456 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
457 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
462 backsql_connection_destroy( Backend *bd, Connection *c )
466 o.o_connid = c->c_connid;
468 Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
469 backsql_free_db_conn( &o );
470 Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
474 #endif /* SLAPD_SQL */