2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2014 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"
31 #include "proto-sql.h"
37 static char *controls[] = {
39 LDAP_CONTROL_MANAGEDSAIT,
41 #ifdef SLAP_CONTROL_X_TREE_DELETE
42 SLAP_CONTROL_X_TREE_DELETE,
43 #endif /* SLAP_CONTROL_X_TREE_DELETE */
44 #ifndef BACKSQL_ARBITRARY_KEY
45 LDAP_CONTROL_PAGEDRESULTS,
46 #endif /* ! BACKSQL_ARBITRARY_KEY */
51 bi->bi_controls = controls;
55 SLAP_BFLAG_INCREMENT |
59 Debug( LDAP_DEBUG_TRACE,"==>sql_back_initialize()\n", 0, 0, 0 );
61 bi->bi_db_init = backsql_db_init;
62 bi->bi_db_config = config_generic_wrapper;
63 bi->bi_db_open = backsql_db_open;
64 bi->bi_db_close = backsql_db_close;
65 bi->bi_db_destroy = backsql_db_destroy;
67 bi->bi_op_abandon = 0;
68 bi->bi_op_compare = backsql_compare;
69 bi->bi_op_bind = backsql_bind;
71 bi->bi_op_search = backsql_search;
72 bi->bi_op_modify = backsql_modify;
73 bi->bi_op_modrdn = backsql_modrdn;
74 bi->bi_op_add = backsql_add;
75 bi->bi_op_delete = backsql_delete;
77 bi->bi_chk_referrals = 0;
78 bi->bi_operational = backsql_operational;
79 bi->bi_entry_get_rw = backsql_entry_get;
80 bi->bi_entry_release_rw = backsql_entry_release;
82 bi->bi_connection_init = 0;
84 rc = backsql_init_cf( bi );
85 Debug( LDAP_DEBUG_TRACE,"<==sql_back_initialize()\n", 0, 0, 0 );
93 Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
94 Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
106 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
108 bi = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
109 ldap_pvt_thread_mutex_init( &bi->sql_dbconn_mutex );
110 ldap_pvt_thread_mutex_init( &bi->sql_schema_mutex );
112 if ( backsql_init_db_env( bi ) != SQL_SUCCESS ) {
117 bd->be_cf_ocs = bd->bd_info->bi_cf_ocs;
119 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
129 backsql_info *bi = (backsql_info*)bd->be_private;
131 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
133 backsql_free_db_env( bi );
134 ldap_pvt_thread_mutex_destroy( &bi->sql_dbconn_mutex );
135 backsql_destroy_schema_map( bi );
136 ldap_pvt_thread_mutex_destroy( &bi->sql_schema_mutex );
138 if ( bi->sql_dbname ) {
139 ch_free( bi->sql_dbname );
141 if ( bi->sql_dbuser ) {
142 ch_free( bi->sql_dbuser );
144 if ( bi->sql_dbpasswd ) {
145 ch_free( bi->sql_dbpasswd );
147 if ( bi->sql_dbhost ) {
148 ch_free( bi->sql_dbhost );
150 if ( bi->sql_upper_func.bv_val ) {
151 ch_free( bi->sql_upper_func.bv_val );
152 ch_free( bi->sql_upper_func_open.bv_val );
153 ch_free( bi->sql_upper_func_close.bv_val );
155 if ( bi->sql_concat_func ) {
156 ber_bvarray_free( bi->sql_concat_func );
158 if ( !BER_BVISNULL( &bi->sql_strcast_func ) ) {
159 ch_free( bi->sql_strcast_func.bv_val );
161 if ( !BER_BVISNULL( &bi->sql_children_cond ) ) {
162 ch_free( bi->sql_children_cond.bv_val );
164 if ( !BER_BVISNULL( &bi->sql_dn_match_cond ) ) {
165 ch_free( bi->sql_dn_match_cond.bv_val );
167 if ( !BER_BVISNULL( &bi->sql_subtree_cond ) ) {
168 ch_free( bi->sql_subtree_cond.bv_val );
170 if ( !BER_BVISNULL( &bi->sql_dn_oc_aliasing ) ) {
171 ch_free( bi->sql_dn_oc_aliasing.bv_val );
173 if ( bi->sql_oc_query ) {
174 ch_free( bi->sql_oc_query );
176 if ( bi->sql_at_query ) {
177 ch_free( bi->sql_at_query );
179 if ( bi->sql_id_query ) {
180 ch_free( bi->sql_id_query );
182 if ( bi->sql_has_children_query ) {
183 ch_free( bi->sql_has_children_query );
185 if ( bi->sql_insentry_stmt ) {
186 ch_free( bi->sql_insentry_stmt );
188 if ( bi->sql_delentry_stmt ) {
189 ch_free( bi->sql_delentry_stmt );
191 if ( bi->sql_renentry_stmt ) {
192 ch_free( bi->sql_renentry_stmt );
194 if ( bi->sql_delobjclasses_stmt ) {
195 ch_free( bi->sql_delobjclasses_stmt );
197 if ( !BER_BVISNULL( &bi->sql_aliasing ) ) {
198 ch_free( bi->sql_aliasing.bv_val );
200 if ( !BER_BVISNULL( &bi->sql_aliasing_quote ) ) {
201 ch_free( bi->sql_aliasing_quote.bv_val );
204 if ( bi->sql_anlist ) {
207 for ( i = 0; !BER_BVISNULL( &bi->sql_anlist[ i ].an_name ); i++ )
209 ch_free( bi->sql_anlist[ i ].an_name.bv_val );
211 ch_free( bi->sql_anlist );
214 if ( bi->sql_baseObject ) {
215 entry_free( bi->sql_baseObject );
220 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
229 backsql_info *bi = (backsql_info*)bd->be_private;
230 struct berbuf bb = BB_NULL;
232 Connection conn = { 0 };
233 OperationBuffer opbuf;
235 SQLHDBC dbh = SQL_NULL_HDBC;
236 void *thrctx = ldap_pvt_thread_pool_context();
238 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
239 "testing RDBMS connection\n", 0, 0, 0 );
240 if ( bi->sql_dbname == NULL ) {
241 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
242 "datasource name not specified "
243 "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
247 if ( bi->sql_concat_func == NULL ) {
248 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
249 "concat func not specified (use \"concat_pattern\" "
250 "directive in slapd.conf)\n", 0, 0, 0 );
252 if ( backsql_split_pattern( backsql_def_concat_func,
253 &bi->sql_concat_func, 2 ) ) {
254 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
255 "unable to parse pattern \"%s\"",
256 backsql_def_concat_func, 0, 0 );
262 * see back-sql.h for default values
264 if ( BER_BVISNULL( &bi->sql_aliasing ) ) {
265 ber_str2bv( BACKSQL_ALIASING,
266 STRLENOF( BACKSQL_ALIASING ),
267 1, &bi->sql_aliasing );
270 if ( BER_BVISNULL( &bi->sql_aliasing_quote ) ) {
271 ber_str2bv( BACKSQL_ALIASING_QUOTE,
272 STRLENOF( BACKSQL_ALIASING_QUOTE ),
273 1, &bi->sql_aliasing_quote );
277 * Prepare cast string as required
279 if ( bi->sql_upper_func.bv_val ) {
282 if ( BACKSQL_UPPER_NEEDS_CAST( bi ) ) {
283 snprintf( buf, sizeof( buf ),
284 "%s(cast (" /* ? as varchar(%d))) */ ,
285 bi->sql_upper_func.bv_val );
286 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
288 snprintf( buf, sizeof( buf ),
289 /* (cast(? */ " as varchar(%d)))",
290 BACKSQL_MAX_DN_LEN );
291 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_close );
294 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
295 bi->sql_upper_func.bv_val );
296 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
298 ber_str2bv( /* (? */ ")", 0, 1, &bi->sql_upper_func_close );
302 /* normalize filter values only if necessary */
303 bi->sql_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
304 assert( bi->sql_caseIgnoreMatch != NULL );
306 bi->sql_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
307 assert( bi->sql_telephoneNumberMatch != NULL );
309 if ( bi->sql_dbuser == NULL ) {
310 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
311 "user name not specified "
312 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
316 if ( BER_BVISNULL( &bi->sql_subtree_cond ) ) {
318 * Prepare concat function for subtree search condition
320 struct berval concat;
321 struct berval values[] = {
326 struct berbuf bb = BB_NULL;
328 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
329 "subtree search SQL condition not specified "
330 "(use \"subtree_cond\" directive in slapd.conf); "
331 "preparing default\n",
334 if ( backsql_prepare_pattern( bi->sql_concat_func, values,
336 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
337 "unable to prepare CONCAT pattern for subtree search",
342 if ( bi->sql_upper_func.bv_val ) {
345 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
348 backsql_strfcat_x( &bb, NULL, "blbbb",
350 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
351 "(ldap_entries.dn) LIKE ",
352 &bi->sql_upper_func_open,
354 &bi->sql_upper_func_close );
359 * ldap_entries.dn LIKE CONCAT('%',?)
362 backsql_strfcat_x( &bb, NULL, "lb",
363 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
364 "ldap_entries.dn LIKE ",
368 ch_free( concat.bv_val );
370 bi->sql_subtree_cond = bb.bb_val;
372 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
373 "setting \"%s\" as default \"subtree_cond\"\n",
374 bi->sql_subtree_cond.bv_val, 0, 0 );
377 if ( bi->sql_children_cond.bv_val == NULL ) {
379 * Prepare concat function for children search condition
381 struct berval concat;
382 struct berval values[] = {
387 struct berbuf bb = BB_NULL;
389 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
390 "children search SQL condition not specified "
391 "(use \"children_cond\" directive in slapd.conf); "
392 "preparing default\n",
395 if ( backsql_prepare_pattern( bi->sql_concat_func, values,
397 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
398 "unable to prepare CONCAT pattern for children search", 0, 0, 0 );
402 if ( bi->sql_upper_func.bv_val ) {
405 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
408 backsql_strfcat_x( &bb, NULL, "blbbb",
410 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
411 "(ldap_entries.dn) LIKE ",
412 &bi->sql_upper_func_open,
414 &bi->sql_upper_func_close );
419 * ldap_entries.dn LIKE CONCAT('%,',?)
422 backsql_strfcat_x( &bb, NULL, "lb",
423 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
424 "ldap_entries.dn LIKE ",
428 ch_free( concat.bv_val );
430 bi->sql_children_cond = bb.bb_val;
432 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
433 "setting \"%s\" as default \"children_cond\"\n",
434 bi->sql_children_cond.bv_val, 0, 0 );
437 if ( bi->sql_dn_match_cond.bv_val == NULL ) {
439 * Prepare concat function for dn match search condition
441 struct berbuf bb = BB_NULL;
443 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
444 "DN match search SQL condition not specified "
445 "(use \"dn_match_cond\" directive in slapd.conf); "
446 "preparing default\n",
449 if ( bi->sql_upper_func.bv_val ) {
452 * UPPER(ldap_entries.dn)=?
455 backsql_strfcat_x( &bb, NULL, "blbcb",
457 (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
458 "(ldap_entries.dn)=",
459 &bi->sql_upper_func_open,
461 &bi->sql_upper_func_close );
469 backsql_strfcat_x( &bb, NULL, "l",
470 (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
471 "ldap_entries.dn=?" );
474 bi->sql_dn_match_cond = bb.bb_val;
476 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
477 "setting \"%s\" as default \"dn_match_cond\"\n",
478 bi->sql_dn_match_cond.bv_val, 0, 0 );
481 if ( bi->sql_oc_query == NULL ) {
482 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
484 ch_strdup( backsql_def_needs_select_oc_query );
487 bi->sql_oc_query = ch_strdup( backsql_def_oc_query );
490 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
491 "objectclass mapping SQL statement not specified "
492 "(use \"oc_query\" directive in slapd.conf)\n",
494 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
495 "setting \"%s\" by default\n", bi->sql_oc_query, 0, 0 );
498 if ( bi->sql_at_query == NULL ) {
499 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
500 "attribute mapping SQL statement not specified "
501 "(use \"at_query\" directive in slapd.conf)\n",
503 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
504 "setting \"%s\" by default\n",
505 backsql_def_at_query, 0, 0 );
506 bi->sql_at_query = ch_strdup( backsql_def_at_query );
509 if ( bi->sql_insentry_stmt == NULL ) {
510 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
511 "entry insertion SQL statement not specified "
512 "(use \"insentry_stmt\" directive in slapd.conf)\n",
514 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
515 "setting \"%s\" by default\n",
516 backsql_def_insentry_stmt, 0, 0 );
517 bi->sql_insentry_stmt = ch_strdup( backsql_def_insentry_stmt );
520 if ( bi->sql_delentry_stmt == NULL ) {
521 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
522 "entry deletion SQL statement not specified "
523 "(use \"delentry_stmt\" directive in slapd.conf)\n",
525 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
526 "setting \"%s\" by default\n",
527 backsql_def_delentry_stmt, 0, 0 );
528 bi->sql_delentry_stmt = ch_strdup( backsql_def_delentry_stmt );
531 if ( bi->sql_renentry_stmt == NULL ) {
532 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
533 "entry deletion SQL statement not specified "
534 "(use \"renentry_stmt\" directive in slapd.conf)\n",
536 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
537 "setting \"%s\" by default\n",
538 backsql_def_renentry_stmt, 0, 0 );
539 bi->sql_renentry_stmt = ch_strdup( backsql_def_renentry_stmt );
542 if ( bi->sql_delobjclasses_stmt == NULL ) {
543 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
544 "objclasses deletion SQL statement not specified "
545 "(use \"delobjclasses_stmt\" directive in slapd.conf)\n",
547 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
548 "setting \"%s\" by default\n",
549 backsql_def_delobjclasses_stmt, 0, 0 );
550 bi->sql_delobjclasses_stmt = ch_strdup( backsql_def_delobjclasses_stmt );
553 /* This should just be to force schema loading */
554 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
557 if ( backsql_get_db_conn( op, &dbh ) != LDAP_SUCCESS ) {
558 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
559 "connection failed, exiting\n", 0, 0, 0 );
562 if ( backsql_load_schema_map( bi, dbh ) != LDAP_SUCCESS ) {
563 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
564 "schema mapping failed, exiting\n", 0, 0, 0 );
567 if ( backsql_free_db_conn( op, dbh ) != SQL_SUCCESS ) {
568 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
569 "connection free failed\n", 0, 0, 0 );
571 if ( !BACKSQL_SCHEMA_LOADED( bi ) ) {
572 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
573 "test failed, schema map not loaded - exiting\n",
579 * Prepare ID selection query
581 if ( bi->sql_id_query == NULL ) {
582 /* no custom id_query provided */
583 if ( bi->sql_upper_func.bv_val == NULL ) {
584 backsql_strcat_x( &bb, NULL, backsql_id_query, "dn=?", NULL );
587 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
588 backsql_strcat_x( &bb, NULL, backsql_id_query,
591 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
592 backsql_strfcat_x( &bb, NULL, "sbl",
595 (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
597 backsql_strfcat_x( &bb, NULL, "sblbcb",
600 (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
601 &bi->sql_upper_func_open,
603 &bi->sql_upper_func_close );
607 bi->sql_id_query = bb.bb_val.bv_val;
611 * Prepare children count query
613 BER_BVZERO( &bb.bb_val );
615 backsql_strfcat_x( &bb, NULL, "sbsb",
616 "SELECT COUNT(distinct subordinates.id) "
617 "FROM ldap_entries,ldap_entries ",
618 &bi->sql_aliasing, "subordinates "
619 "WHERE subordinates.parent=ldap_entries.id AND ",
620 &bi->sql_dn_match_cond );
621 bi->sql_has_children_query = bb.bb_val.bv_val;
624 * Prepare DN and objectClass aliasing bit of query
626 BER_BVZERO( &bb.bb_val );
628 backsql_strfcat_x( &bb, NULL, "sbbsbsbbsb",
629 " ", &bi->sql_aliasing, &bi->sql_aliasing_quote,
630 "objectClass", &bi->sql_aliasing_quote,
631 ",ldap_entries.dn ", &bi->sql_aliasing,
632 &bi->sql_aliasing_quote, "dn", &bi->sql_aliasing_quote );
633 bi->sql_dn_oc_aliasing = bb.bb_val;
635 /* should never happen! */
636 assert( bd->be_nsuffix != NULL );
638 if ( BER_BVISNULL( &bd->be_nsuffix[ 1 ] ) ) {
639 /* enable if only one suffix is defined */
640 bi->sql_flags |= BSQLF_USE_SUBTREE_SHORTCUT;
643 bi->sql_flags |= BSQLF_CHECK_SCHEMA;
645 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
646 "test succeeded, schema map loaded\n", 0, 0, 0 );
655 backsql_info *bi = (backsql_info*)bd->be_private;
657 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
659 backsql_conn_destroy( bi );
661 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
666 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
668 /* conditionally define the init_module() function */
669 SLAP_BACKEND_INIT_MODULE( sql )
671 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */