2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2011 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 ) {
118 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
128 backsql_info *bi = (backsql_info*)bd->be_private;
130 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
132 backsql_free_db_env( bi );
133 ldap_pvt_thread_mutex_destroy( &bi->sql_dbconn_mutex );
134 backsql_destroy_schema_map( bi );
135 ldap_pvt_thread_mutex_destroy( &bi->sql_schema_mutex );
137 if ( bi->sql_dbname ) {
138 ch_free( bi->sql_dbname );
140 if ( bi->sql_dbuser ) {
141 ch_free( bi->sql_dbuser );
143 if ( bi->sql_dbpasswd ) {
144 ch_free( bi->sql_dbpasswd );
146 if ( bi->sql_dbhost ) {
147 ch_free( bi->sql_dbhost );
149 if ( bi->sql_upper_func.bv_val ) {
150 ch_free( bi->sql_upper_func.bv_val );
151 ch_free( bi->sql_upper_func_open.bv_val );
152 ch_free( bi->sql_upper_func_close.bv_val );
154 if ( bi->sql_concat_func ) {
155 ber_bvarray_free( bi->sql_concat_func );
157 if ( !BER_BVISNULL( &bi->sql_strcast_func ) ) {
158 ch_free( bi->sql_strcast_func.bv_val );
160 if ( !BER_BVISNULL( &bi->sql_children_cond ) ) {
161 ch_free( bi->sql_children_cond.bv_val );
163 if ( !BER_BVISNULL( &bi->sql_dn_match_cond ) ) {
164 ch_free( bi->sql_dn_match_cond.bv_val );
166 if ( !BER_BVISNULL( &bi->sql_subtree_cond ) ) {
167 ch_free( bi->sql_subtree_cond.bv_val );
169 if ( !BER_BVISNULL( &bi->sql_dn_oc_aliasing ) ) {
170 ch_free( bi->sql_dn_oc_aliasing.bv_val );
172 if ( bi->sql_oc_query ) {
173 ch_free( bi->sql_oc_query );
175 if ( bi->sql_at_query ) {
176 ch_free( bi->sql_at_query );
178 if ( bi->sql_id_query ) {
179 ch_free( bi->sql_id_query );
181 if ( bi->sql_has_children_query ) {
182 ch_free( bi->sql_has_children_query );
184 if ( bi->sql_insentry_stmt ) {
185 ch_free( bi->sql_insentry_stmt );
187 if ( bi->sql_delentry_stmt ) {
188 ch_free( bi->sql_delentry_stmt );
190 if ( bi->sql_renentry_stmt ) {
191 ch_free( bi->sql_renentry_stmt );
193 if ( bi->sql_delobjclasses_stmt ) {
194 ch_free( bi->sql_delobjclasses_stmt );
196 if ( !BER_BVISNULL( &bi->sql_aliasing ) ) {
197 ch_free( bi->sql_aliasing.bv_val );
199 if ( !BER_BVISNULL( &bi->sql_aliasing_quote ) ) {
200 ch_free( bi->sql_aliasing_quote.bv_val );
203 if ( bi->sql_anlist ) {
206 for ( i = 0; !BER_BVISNULL( &bi->sql_anlist[ i ].an_name ); i++ )
208 ch_free( bi->sql_anlist[ i ].an_name.bv_val );
210 ch_free( bi->sql_anlist );
213 if ( bi->sql_baseObject ) {
214 entry_free( bi->sql_baseObject );
219 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
228 backsql_info *bi = (backsql_info*)bd->be_private;
229 struct berbuf bb = BB_NULL;
231 Connection conn = { 0 };
232 OperationBuffer opbuf;
234 SQLHDBC dbh = SQL_NULL_HDBC;
235 void *thrctx = ldap_pvt_thread_pool_context();
237 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
238 "testing RDBMS connection\n", 0, 0, 0 );
239 if ( bi->sql_dbname == NULL ) {
240 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
241 "datasource name not specified "
242 "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
246 if ( bi->sql_concat_func == NULL ) {
247 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
248 "concat func not specified (use \"concat_pattern\" "
249 "directive in slapd.conf)\n", 0, 0, 0 );
251 if ( backsql_split_pattern( backsql_def_concat_func,
252 &bi->sql_concat_func, 2 ) ) {
253 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
254 "unable to parse pattern \"%s\"",
255 backsql_def_concat_func, 0, 0 );
261 * see back-sql.h for default values
263 if ( BER_BVISNULL( &bi->sql_aliasing ) ) {
264 ber_str2bv( BACKSQL_ALIASING,
265 STRLENOF( BACKSQL_ALIASING ),
266 1, &bi->sql_aliasing );
269 if ( BER_BVISNULL( &bi->sql_aliasing_quote ) ) {
270 ber_str2bv( BACKSQL_ALIASING_QUOTE,
271 STRLENOF( BACKSQL_ALIASING_QUOTE ),
272 1, &bi->sql_aliasing_quote );
276 * Prepare cast string as required
278 if ( bi->sql_upper_func.bv_val ) {
281 if ( BACKSQL_UPPER_NEEDS_CAST( bi ) ) {
282 snprintf( buf, sizeof( buf ),
283 "%s(cast (" /* ? as varchar(%d))) */ ,
284 bi->sql_upper_func.bv_val );
285 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
287 snprintf( buf, sizeof( buf ),
288 /* (cast(? */ " as varchar(%d)))",
289 BACKSQL_MAX_DN_LEN );
290 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_close );
293 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
294 bi->sql_upper_func.bv_val );
295 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
297 ber_str2bv( /* (? */ ")", 0, 1, &bi->sql_upper_func_close );
301 /* normalize filter values only if necessary */
302 bi->sql_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
303 assert( bi->sql_caseIgnoreMatch != NULL );
305 bi->sql_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
306 assert( bi->sql_telephoneNumberMatch != NULL );
308 if ( bi->sql_dbuser == NULL ) {
309 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
310 "user name not specified "
311 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
315 if ( BER_BVISNULL( &bi->sql_subtree_cond ) ) {
317 * Prepare concat function for subtree search condition
319 struct berval concat;
320 struct berval values[] = {
325 struct berbuf bb = BB_NULL;
327 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
328 "subtree search SQL condition not specified "
329 "(use \"subtree_cond\" directive in slapd.conf); "
330 "preparing default\n",
333 if ( backsql_prepare_pattern( bi->sql_concat_func, values,
335 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
336 "unable to prepare CONCAT pattern for subtree search",
341 if ( bi->sql_upper_func.bv_val ) {
344 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
347 backsql_strfcat_x( &bb, NULL, "blbbb",
349 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
350 "(ldap_entries.dn) LIKE ",
351 &bi->sql_upper_func_open,
353 &bi->sql_upper_func_close );
358 * ldap_entries.dn LIKE CONCAT('%',?)
361 backsql_strfcat_x( &bb, NULL, "lb",
362 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
363 "ldap_entries.dn LIKE ",
367 ch_free( concat.bv_val );
369 bi->sql_subtree_cond = bb.bb_val;
371 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
372 "setting \"%s\" as default \"subtree_cond\"\n",
373 bi->sql_subtree_cond.bv_val, 0, 0 );
376 if ( bi->sql_children_cond.bv_val == NULL ) {
378 * Prepare concat function for children search condition
380 struct berval concat;
381 struct berval values[] = {
386 struct berbuf bb = BB_NULL;
388 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
389 "children search SQL condition not specified "
390 "(use \"children_cond\" directive in slapd.conf); "
391 "preparing default\n",
394 if ( backsql_prepare_pattern( bi->sql_concat_func, values,
396 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
397 "unable to prepare CONCAT pattern for children search", 0, 0, 0 );
401 if ( bi->sql_upper_func.bv_val ) {
404 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
407 backsql_strfcat_x( &bb, NULL, "blbbb",
409 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
410 "(ldap_entries.dn) LIKE ",
411 &bi->sql_upper_func_open,
413 &bi->sql_upper_func_close );
418 * ldap_entries.dn LIKE CONCAT('%,',?)
421 backsql_strfcat_x( &bb, NULL, "lb",
422 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
423 "ldap_entries.dn LIKE ",
427 ch_free( concat.bv_val );
429 bi->sql_children_cond = bb.bb_val;
431 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
432 "setting \"%s\" as default \"children_cond\"\n",
433 bi->sql_children_cond.bv_val, 0, 0 );
436 if ( bi->sql_dn_match_cond.bv_val == NULL ) {
438 * Prepare concat function for dn match search condition
440 struct berbuf bb = BB_NULL;
442 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
443 "DN match search SQL condition not specified "
444 "(use \"dn_match_cond\" directive in slapd.conf); "
445 "preparing default\n",
448 if ( bi->sql_upper_func.bv_val ) {
451 * UPPER(ldap_entries.dn)=?
454 backsql_strfcat_x( &bb, NULL, "blbcb",
456 (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
457 "(ldap_entries.dn)=",
458 &bi->sql_upper_func_open,
460 &bi->sql_upper_func_close );
468 backsql_strfcat_x( &bb, NULL, "l",
469 (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
470 "ldap_entries.dn=?" );
473 bi->sql_dn_match_cond = bb.bb_val;
475 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
476 "setting \"%s\" as default \"dn_match_cond\"\n",
477 bi->sql_dn_match_cond.bv_val, 0, 0 );
480 if ( bi->sql_oc_query == NULL ) {
481 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
483 ch_strdup( backsql_def_needs_select_oc_query );
486 bi->sql_oc_query = ch_strdup( backsql_def_oc_query );
489 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
490 "objectclass mapping SQL statement not specified "
491 "(use \"oc_query\" directive in slapd.conf)\n",
493 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
494 "setting \"%s\" by default\n", bi->sql_oc_query, 0, 0 );
497 if ( bi->sql_at_query == NULL ) {
498 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
499 "attribute mapping SQL statement not specified "
500 "(use \"at_query\" directive in slapd.conf)\n",
502 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
503 "setting \"%s\" by default\n",
504 backsql_def_at_query, 0, 0 );
505 bi->sql_at_query = ch_strdup( backsql_def_at_query );
508 if ( bi->sql_insentry_stmt == NULL ) {
509 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
510 "entry insertion SQL statement not specified "
511 "(use \"insentry_stmt\" directive in slapd.conf)\n",
513 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
514 "setting \"%s\" by default\n",
515 backsql_def_insentry_stmt, 0, 0 );
516 bi->sql_insentry_stmt = ch_strdup( backsql_def_insentry_stmt );
519 if ( bi->sql_delentry_stmt == NULL ) {
520 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
521 "entry deletion SQL statement not specified "
522 "(use \"delentry_stmt\" directive in slapd.conf)\n",
524 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
525 "setting \"%s\" by default\n",
526 backsql_def_delentry_stmt, 0, 0 );
527 bi->sql_delentry_stmt = ch_strdup( backsql_def_delentry_stmt );
530 if ( bi->sql_renentry_stmt == NULL ) {
531 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
532 "entry deletion SQL statement not specified "
533 "(use \"renentry_stmt\" directive in slapd.conf)\n",
535 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
536 "setting \"%s\" by default\n",
537 backsql_def_renentry_stmt, 0, 0 );
538 bi->sql_renentry_stmt = ch_strdup( backsql_def_renentry_stmt );
541 if ( bi->sql_delobjclasses_stmt == NULL ) {
542 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
543 "objclasses deletion SQL statement not specified "
544 "(use \"delobjclasses_stmt\" directive in slapd.conf)\n",
546 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
547 "setting \"%s\" by default\n",
548 backsql_def_delobjclasses_stmt, 0, 0 );
549 bi->sql_delobjclasses_stmt = ch_strdup( backsql_def_delobjclasses_stmt );
552 /* This should just be to force schema loading */
553 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
556 if ( backsql_get_db_conn( op, &dbh ) != LDAP_SUCCESS ) {
557 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
558 "connection failed, exiting\n", 0, 0, 0 );
561 if ( backsql_load_schema_map( bi, dbh ) != LDAP_SUCCESS ) {
562 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
563 "schema mapping failed, exiting\n", 0, 0, 0 );
566 if ( backsql_free_db_conn( op, dbh ) != SQL_SUCCESS ) {
567 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
568 "connection free failed\n", 0, 0, 0 );
570 if ( !BACKSQL_SCHEMA_LOADED( bi ) ) {
571 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
572 "test failed, schema map not loaded - exiting\n",
578 * Prepare ID selection query
580 if ( bi->sql_id_query == NULL ) {
581 /* no custom id_query provided */
582 if ( bi->sql_upper_func.bv_val == NULL ) {
583 backsql_strcat_x( &bb, NULL, backsql_id_query, "dn=?", NULL );
586 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
587 backsql_strcat_x( &bb, NULL, backsql_id_query,
590 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
591 backsql_strfcat_x( &bb, NULL, "sbl",
594 (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
596 backsql_strfcat_x( &bb, NULL, "sblbcb",
599 (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
600 &bi->sql_upper_func_open,
602 &bi->sql_upper_func_close );
606 bi->sql_id_query = bb.bb_val.bv_val;
610 * Prepare children count query
612 BER_BVZERO( &bb.bb_val );
614 backsql_strfcat_x( &bb, NULL, "sbsb",
615 "SELECT COUNT(distinct subordinates.id) "
616 "FROM ldap_entries,ldap_entries ",
617 &bi->sql_aliasing, "subordinates "
618 "WHERE subordinates.parent=ldap_entries.id AND ",
619 &bi->sql_dn_match_cond );
620 bi->sql_has_children_query = bb.bb_val.bv_val;
623 * Prepare DN and objectClass aliasing bit of query
625 BER_BVZERO( &bb.bb_val );
627 backsql_strfcat_x( &bb, NULL, "sbbsbsbbsb",
628 " ", &bi->sql_aliasing, &bi->sql_aliasing_quote,
629 "objectClass", &bi->sql_aliasing_quote,
630 ",ldap_entries.dn ", &bi->sql_aliasing,
631 &bi->sql_aliasing_quote, "dn", &bi->sql_aliasing_quote );
632 bi->sql_dn_oc_aliasing = bb.bb_val;
634 /* should never happen! */
635 assert( bd->be_nsuffix != NULL );
637 if ( BER_BVISNULL( &bd->be_nsuffix[ 1 ] ) ) {
638 /* enable if only one suffix is defined */
639 bi->sql_flags |= BSQLF_USE_SUBTREE_SHORTCUT;
642 bi->sql_flags |= BSQLF_CHECK_SCHEMA;
644 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
645 "test succeeded, schema map loaded\n", 0, 0, 0 );
654 backsql_info *bi = (backsql_info*)bd->be_private;
656 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
658 backsql_conn_destroy( bi );
660 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
665 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
667 /* conditionally define the init_module() function */
668 SLAP_BACKEND_INIT_MODULE( sql )
670 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */