2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2006 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
6 * Portions Copyright 2002 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Dmitry Kovalev for inclusion
19 * by OpenLDAP Software. Additional significant contributors include
20 * Pierangelo Masarati.
26 #include <sys/types.h>
27 #include "ac/string.h"
30 #include "proto-sql.h"
36 static char *controls[] = {
38 LDAP_CONTROL_MANAGEDSAIT,
39 #if 0 /* needs improvements */
42 #ifdef SLAP_CONTROL_X_TREE_DELETE
43 SLAP_CONTROL_X_TREE_DELETE,
44 #endif /* SLAP_CONTROL_X_TREE_DELETE */
48 bi->bi_controls = controls;
52 SLAP_BFLAG_INCREMENT |
56 Debug( LDAP_DEBUG_TRACE,"==>sql_back_initialize()\n", 0, 0, 0 );
58 bi->bi_db_init = backsql_db_init;
59 bi->bi_db_config = backsql_db_config;
60 bi->bi_db_open = backsql_db_open;
61 bi->bi_db_close = backsql_db_close;
62 bi->bi_db_destroy = backsql_db_destroy;
64 bi->bi_op_abandon = 0;
65 bi->bi_op_compare = backsql_compare;
66 bi->bi_op_bind = backsql_bind;
68 bi->bi_op_search = backsql_search;
69 bi->bi_op_modify = backsql_modify;
70 bi->bi_op_modrdn = backsql_modrdn;
71 bi->bi_op_add = backsql_add;
72 bi->bi_op_delete = backsql_delete;
74 bi->bi_chk_referrals = 0;
75 bi->bi_operational = backsql_operational;
76 bi->bi_entry_get_rw = backsql_entry_get;
77 bi->bi_entry_release_rw = backsql_entry_release;
79 bi->bi_connection_init = 0;
80 bi->bi_connection_destroy = backsql_connection_destroy;
82 Debug( LDAP_DEBUG_TRACE,"<==sql_back_initialize()\n", 0, 0, 0 );
90 Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
91 Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
102 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
104 bi = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
105 ldap_pvt_thread_mutex_init( &bi->sql_dbconn_mutex );
106 ldap_pvt_thread_mutex_init( &bi->sql_schema_mutex );
108 if ( backsql_init_db_env( bi ) != SQL_SUCCESS ) {
114 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
123 backsql_info *bi = (backsql_info*)bd->be_private;
125 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
127 ldap_pvt_thread_mutex_lock( &bi->sql_dbconn_mutex );
128 backsql_free_db_env( bi );
129 ldap_pvt_thread_mutex_unlock( &bi->sql_dbconn_mutex );
130 ldap_pvt_thread_mutex_destroy( &bi->sql_dbconn_mutex );
131 ldap_pvt_thread_mutex_lock( &bi->sql_schema_mutex );
132 backsql_destroy_schema_map( bi );
133 ldap_pvt_thread_mutex_unlock( &bi->sql_schema_mutex );
134 ldap_pvt_thread_mutex_destroy( &bi->sql_schema_mutex );
136 if ( bi->sql_dbname ) {
137 ch_free( bi->sql_dbname );
139 if ( bi->sql_dbuser ) {
140 ch_free( bi->sql_dbuser );
142 if ( bi->sql_dbpasswd ) {
143 ch_free( bi->sql_dbpasswd );
145 if ( bi->sql_dbhost ) {
146 ch_free( bi->sql_dbhost );
148 if ( bi->sql_upper_func.bv_val ) {
149 ch_free( bi->sql_upper_func.bv_val );
150 ch_free( bi->sql_upper_func_open.bv_val );
151 ch_free( bi->sql_upper_func_close.bv_val );
153 if ( bi->sql_concat_func ) {
154 ber_bvarray_free( bi->sql_concat_func );
156 if ( !BER_BVISNULL( &bi->sql_strcast_func ) ) {
157 ch_free( bi->sql_strcast_func.bv_val );
159 if ( !BER_BVISNULL( &bi->sql_children_cond ) ) {
160 ch_free( bi->sql_children_cond.bv_val );
162 if ( !BER_BVISNULL( &bi->sql_subtree_cond ) ) {
163 ch_free( bi->sql_subtree_cond.bv_val );
165 if ( !BER_BVISNULL( &bi->sql_dn_oc_aliasing ) ) {
166 ch_free( bi->sql_dn_oc_aliasing.bv_val );
168 if ( bi->sql_oc_query ) {
169 ch_free( bi->sql_oc_query );
171 if ( bi->sql_at_query ) {
172 ch_free( bi->sql_at_query );
174 if ( bi->sql_id_query ) {
175 ch_free( bi->sql_id_query );
177 if ( bi->sql_has_children_query ) {
178 ch_free( bi->sql_has_children_query );
180 if ( bi->sql_insentry_stmt ) {
181 ch_free( bi->sql_insentry_stmt );
183 if ( bi->sql_delentry_stmt ) {
184 ch_free( bi->sql_delentry_stmt );
186 if ( bi->sql_renentry_stmt ) {
187 ch_free( bi->sql_renentry_stmt );
189 if ( bi->sql_delobjclasses_stmt ) {
190 ch_free( bi->sql_delobjclasses_stmt );
192 if ( !BER_BVISNULL( &bi->sql_aliasing ) ) {
193 ch_free( bi->sql_aliasing.bv_val );
195 if ( !BER_BVISNULL( &bi->sql_aliasing_quote ) ) {
196 ch_free( bi->sql_aliasing_quote.bv_val );
199 if ( bi->sql_anlist ) {
202 for ( i = 0; !BER_BVISNULL( &bi->sql_anlist[ i ].an_name ); i++ )
204 ch_free( bi->sql_anlist[ i ].an_name.bv_val );
206 ch_free( bi->sql_anlist );
209 if ( bi->sql_baseObject ) {
210 entry_free( bi->sql_baseObject );
215 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
223 backsql_info *bi = (backsql_info*)bd->be_private;
224 SQLHDBC dbh = SQL_NULL_HDBC;
225 struct berbuf bb = BB_NULL;
227 OperationBuffer opbuf;
228 Operation* op = (Operation *) &opbuf;
230 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
231 "testing RDBMS connection\n", 0, 0, 0 );
232 if ( bi->sql_dbname == NULL ) {
233 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
234 "datasource name not specified "
235 "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
239 if ( bi->sql_concat_func == NULL ) {
240 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
241 "concat func not specified (use \"concat_pattern\" "
242 "directive in slapd.conf)\n", 0, 0, 0 );
244 if ( backsql_split_pattern( backsql_def_concat_func,
245 &bi->sql_concat_func, 2 ) ) {
246 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
247 "unable to parse pattern \"%s\"",
248 backsql_def_concat_func, 0, 0 );
254 * see back-sql.h for default values
256 if ( BER_BVISNULL( &bi->sql_aliasing ) ) {
257 ber_str2bv( BACKSQL_ALIASING,
258 STRLENOF( BACKSQL_ALIASING ),
259 1, &bi->sql_aliasing );
262 if ( BER_BVISNULL( &bi->sql_aliasing_quote ) ) {
263 ber_str2bv( BACKSQL_ALIASING_QUOTE,
264 STRLENOF( BACKSQL_ALIASING_QUOTE ),
265 1, &bi->sql_aliasing_quote );
269 * Prepare cast string as required
271 if ( bi->sql_upper_func.bv_val ) {
274 if ( BACKSQL_UPPER_NEEDS_CAST( bi ) ) {
275 snprintf( buf, sizeof( buf ),
276 "%s(cast (" /* ? as varchar(%d))) */ ,
277 bi->sql_upper_func.bv_val );
278 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
280 snprintf( buf, sizeof( buf ),
281 /* (cast(? */ " as varchar(%d)))",
282 BACKSQL_MAX_DN_LEN );
283 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_close );
286 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
287 bi->sql_upper_func.bv_val );
288 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
290 ber_str2bv( /* (? */ ")", 0, 1, &bi->sql_upper_func_close );
294 /* normalize filter values only if necessary */
295 bi->sql_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
296 assert( bi->sql_caseIgnoreMatch != NULL );
298 bi->sql_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
299 assert( bi->sql_telephoneNumberMatch != NULL );
301 if ( bi->sql_dbuser == NULL ) {
302 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
303 "user name not specified "
304 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
308 if ( BER_BVISNULL( &bi->sql_subtree_cond ) ) {
310 * Prepare concat function for subtree search condition
312 struct berval concat;
313 struct berval values[] = {
318 struct berbuf bb = BB_NULL;
320 if ( backsql_prepare_pattern( bi->sql_concat_func, values,
322 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
323 "unable to prepare CONCAT pattern", 0, 0, 0 );
327 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
328 "subtree search SQL condition not specified "
329 "(use \"subtree_cond\" directive in slapd.conf)\n",
332 if ( bi->sql_upper_func.bv_val ) {
335 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
338 backsql_strfcat_x( &bb, NULL, "blbbb",
340 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
341 "(ldap_entries.dn) LIKE ",
342 &bi->sql_upper_func_open,
344 &bi->sql_upper_func_close );
349 * ldap_entries.dn LIKE CONCAT('%',?)
352 backsql_strfcat_x( &bb, NULL, "lb",
353 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
354 "ldap_entries.dn LIKE ",
358 ch_free( concat.bv_val );
360 bi->sql_subtree_cond = bb.bb_val;
362 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
363 "setting \"%s\" as default\n",
364 bi->sql_subtree_cond.bv_val, 0, 0 );
367 if ( bi->sql_children_cond.bv_val == NULL ) {
368 struct berbuf bb = BB_NULL;
370 if ( bi->sql_upper_func.bv_val ) {
373 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
376 backsql_strfcat_x( &bb, NULL, "blbl",
378 (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
379 "(ldap_entries.dn)=",
381 (ber_len_t)STRLENOF( "(?)" ), "(?)" );
386 * ldap_entries.dn LIKE CONCAT('%,',?)
389 backsql_strfcat_x( &bb, NULL, "l",
390 (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
391 "ldap_entries.dn=?");
394 bi->sql_children_cond = bb.bb_val;
396 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
397 "setting \"%s\" as default\n",
398 bi->sql_children_cond.bv_val, 0, 0 );
401 if ( bi->sql_oc_query == NULL ) {
402 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
404 ch_strdup( backsql_def_needs_select_oc_query );
407 bi->sql_oc_query = ch_strdup( backsql_def_oc_query );
410 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
411 "objectclass mapping SQL statement not specified "
412 "(use \"oc_query\" directive in slapd.conf)\n",
414 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
415 "setting \"%s\" by default\n", bi->sql_oc_query, 0, 0 );
418 if ( bi->sql_at_query == NULL ) {
419 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
420 "attribute mapping SQL statement not specified "
421 "(use \"at_query\" directive in slapd.conf)\n",
423 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
424 "setting \"%s\" by default\n",
425 backsql_def_at_query, 0, 0 );
426 bi->sql_at_query = ch_strdup( backsql_def_at_query );
429 if ( bi->sql_insentry_stmt == NULL ) {
430 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
431 "entry insertion SQL statement not specified "
432 "(use \"insentry_stmt\" directive in slapd.conf)\n",
434 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
435 "setting \"%s\" by default\n",
436 backsql_def_insentry_stmt, 0, 0 );
437 bi->sql_insentry_stmt = ch_strdup( backsql_def_insentry_stmt );
440 if ( bi->sql_delentry_stmt == NULL ) {
441 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
442 "entry deletion SQL statement not specified "
443 "(use \"delentry_stmt\" directive in slapd.conf)\n",
445 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
446 "setting \"%s\" by default\n",
447 backsql_def_delentry_stmt, 0, 0 );
448 bi->sql_delentry_stmt = ch_strdup( backsql_def_delentry_stmt );
451 if ( bi->sql_renentry_stmt == NULL ) {
452 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
453 "entry deletion SQL statement not specified "
454 "(use \"renentry_stmt\" directive in slapd.conf)\n",
456 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
457 "setting \"%s\" by default\n",
458 backsql_def_renentry_stmt, 0, 0 );
459 bi->sql_renentry_stmt = ch_strdup( backsql_def_renentry_stmt );
462 if ( bi->sql_delobjclasses_stmt == NULL ) {
463 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
464 "objclasses deletion SQL statement not specified "
465 "(use \"delobjclasses_stmt\" directive in slapd.conf)\n",
467 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
468 "setting \"%s\" by default\n",
469 backsql_def_delobjclasses_stmt, 0, 0 );
470 bi->sql_delobjclasses_stmt = ch_strdup( backsql_def_delobjclasses_stmt );
473 /* This should just be to force schema loading */
474 op->o_hdr = (Opheader *)&op[ 1 ];
475 op->o_connid = (unsigned long)(-1);
477 if ( backsql_get_db_conn( op, &dbh ) != LDAP_SUCCESS ) {
478 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
479 "connection failed, exiting\n", 0, 0, 0 );
483 if ( backsql_free_db_conn( op ) != SQL_SUCCESS ) {
484 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
485 "connection free failed\n", 0, 0, 0 );
487 if ( !BACKSQL_SCHEMA_LOADED( bi ) ) {
488 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
489 "test failed, schema map not loaded - exiting\n",
495 * Prepare ID selection query
497 if ( bi->sql_id_query == NULL ) {
498 /* no custom id_query provided */
499 if ( bi->sql_upper_func.bv_val == NULL ) {
500 backsql_strcat_x( &bb, NULL, backsql_id_query, "dn=?", NULL );
503 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
504 backsql_strcat_x( &bb, NULL, backsql_id_query,
507 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
508 backsql_strfcat_x( &bb, NULL, "sbl",
511 (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
513 backsql_strfcat_x( &bb, NULL, "sblbcb",
516 (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
517 &bi->sql_upper_func_open,
519 &bi->sql_upper_func_close );
523 bi->sql_id_query = bb.bb_val.bv_val;
527 * Prepare children ID selection query
529 BER_BVZERO( &bb.bb_val );
531 backsql_strfcat_x( &bb, NULL, "sbsb",
532 "SELECT COUNT(distinct subordinates.id) "
533 "FROM ldap_entries,ldap_entries ",
534 &bi->sql_aliasing, "subordinates "
535 "WHERE subordinates.parent=ldap_entries.id AND ",
536 &bi->sql_children_cond );
537 bi->sql_has_children_query = bb.bb_val.bv_val;
540 * Prepare DN and objectClass aliasing bit of query
542 BER_BVZERO( &bb.bb_val );
544 backsql_strfcat_x( &bb, NULL, "sbbsbsbbsb",
545 " ", &bi->sql_aliasing, &bi->sql_aliasing_quote,
546 "objectClass", &bi->sql_aliasing_quote,
547 ",ldap_entries.dn ", &bi->sql_aliasing,
548 &bi->sql_aliasing_quote, "dn", &bi->sql_aliasing_quote );
549 bi->sql_dn_oc_aliasing = bb.bb_val;
551 /* should never happen! */
552 assert( bd->be_nsuffix != NULL );
554 if ( BER_BVISNULL( &bd->be_nsuffix[ 1 ] ) ) {
555 /* enable if only one suffix is defined */
556 bi->sql_flags |= BSQLF_USE_SUBTREE_SHORTCUT;
559 bi->sql_flags |= BSQLF_CHECK_SCHEMA;
561 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
562 "test succeeded, schema map loaded\n", 0, 0, 0 );
570 backsql_info *bi = (backsql_info*)bd->be_private;
572 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
574 backsql_conn_destroy( bi );
576 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
582 backsql_connection_destroy( Backend *bd, Connection *c )
584 OperationBuffer opbuf;
585 Operation* op = (Operation *) &opbuf;
587 op->o_hdr = (Opheader *)&op[ 1 ];
588 op->o_connid = c->c_connid;
591 Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
592 backsql_free_db_conn( op );
593 Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
598 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
600 /* conditionally define the init_module() function */
601 SLAP_BACKEND_INIT_MODULE( sql )
603 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */