2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2009 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 */
50 bi->bi_controls = controls;
54 SLAP_BFLAG_INCREMENT |
58 Debug( LDAP_DEBUG_TRACE,"==>sql_back_initialize()\n", 0, 0, 0 );
60 bi->bi_db_init = backsql_db_init;
61 bi->bi_db_config = backsql_db_config;
62 bi->bi_db_open = backsql_db_open;
63 bi->bi_db_close = backsql_db_close;
64 bi->bi_db_destroy = backsql_db_destroy;
66 bi->bi_op_abandon = 0;
67 bi->bi_op_compare = backsql_compare;
68 bi->bi_op_bind = backsql_bind;
70 bi->bi_op_search = backsql_search;
71 bi->bi_op_modify = backsql_modify;
72 bi->bi_op_modrdn = backsql_modrdn;
73 bi->bi_op_add = backsql_add;
74 bi->bi_op_delete = backsql_delete;
76 bi->bi_chk_referrals = 0;
77 bi->bi_operational = backsql_operational;
78 bi->bi_entry_get_rw = backsql_entry_get;
79 bi->bi_entry_release_rw = backsql_entry_release;
81 bi->bi_connection_init = 0;
83 Debug( LDAP_DEBUG_TRACE,"<==sql_back_initialize()\n", 0, 0, 0 );
91 Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
92 Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
104 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
106 bi = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
107 ldap_pvt_thread_mutex_init( &bi->sql_dbconn_mutex );
108 ldap_pvt_thread_mutex_init( &bi->sql_schema_mutex );
110 if ( backsql_init_db_env( bi ) != SQL_SUCCESS ) {
116 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
126 backsql_info *bi = (backsql_info*)bd->be_private;
128 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
130 backsql_free_db_env( bi );
131 ldap_pvt_thread_mutex_destroy( &bi->sql_dbconn_mutex );
132 backsql_destroy_schema_map( bi );
133 ldap_pvt_thread_mutex_destroy( &bi->sql_schema_mutex );
135 if ( bi->sql_dbname ) {
136 ch_free( bi->sql_dbname );
138 if ( bi->sql_dbuser ) {
139 ch_free( bi->sql_dbuser );
141 if ( bi->sql_dbpasswd ) {
142 ch_free( bi->sql_dbpasswd );
144 if ( bi->sql_dbhost ) {
145 ch_free( bi->sql_dbhost );
147 if ( bi->sql_upper_func.bv_val ) {
148 ch_free( bi->sql_upper_func.bv_val );
149 ch_free( bi->sql_upper_func_open.bv_val );
150 ch_free( bi->sql_upper_func_close.bv_val );
152 if ( bi->sql_concat_func ) {
153 ber_bvarray_free( bi->sql_concat_func );
155 if ( !BER_BVISNULL( &bi->sql_strcast_func ) ) {
156 ch_free( bi->sql_strcast_func.bv_val );
158 if ( !BER_BVISNULL( &bi->sql_children_cond ) ) {
159 ch_free( bi->sql_children_cond.bv_val );
161 if ( !BER_BVISNULL( &bi->sql_dn_match_cond ) ) {
162 ch_free( bi->sql_dn_match_cond.bv_val );
164 if ( !BER_BVISNULL( &bi->sql_subtree_cond ) ) {
165 ch_free( bi->sql_subtree_cond.bv_val );
167 if ( !BER_BVISNULL( &bi->sql_dn_oc_aliasing ) ) {
168 ch_free( bi->sql_dn_oc_aliasing.bv_val );
170 if ( bi->sql_oc_query ) {
171 ch_free( bi->sql_oc_query );
173 if ( bi->sql_at_query ) {
174 ch_free( bi->sql_at_query );
176 if ( bi->sql_id_query ) {
177 ch_free( bi->sql_id_query );
179 if ( bi->sql_has_children_query ) {
180 ch_free( bi->sql_has_children_query );
182 if ( bi->sql_insentry_stmt ) {
183 ch_free( bi->sql_insentry_stmt );
185 if ( bi->sql_delentry_stmt ) {
186 ch_free( bi->sql_delentry_stmt );
188 if ( bi->sql_renentry_stmt ) {
189 ch_free( bi->sql_renentry_stmt );
191 if ( bi->sql_delobjclasses_stmt ) {
192 ch_free( bi->sql_delobjclasses_stmt );
194 if ( !BER_BVISNULL( &bi->sql_aliasing ) ) {
195 ch_free( bi->sql_aliasing.bv_val );
197 if ( !BER_BVISNULL( &bi->sql_aliasing_quote ) ) {
198 ch_free( bi->sql_aliasing_quote.bv_val );
201 if ( bi->sql_anlist ) {
204 for ( i = 0; !BER_BVISNULL( &bi->sql_anlist[ i ].an_name ); i++ )
206 ch_free( bi->sql_anlist[ i ].an_name.bv_val );
208 ch_free( bi->sql_anlist );
211 if ( bi->sql_baseObject ) {
212 entry_free( bi->sql_baseObject );
217 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
226 backsql_info *bi = (backsql_info*)bd->be_private;
227 struct berbuf bb = BB_NULL;
229 Connection conn = { 0 };
230 OperationBuffer opbuf;
232 SQLHDBC dbh = SQL_NULL_HDBC;
233 void *thrctx = ldap_pvt_thread_pool_context();
235 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
236 "testing RDBMS connection\n", 0, 0, 0 );
237 if ( bi->sql_dbname == NULL ) {
238 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
239 "datasource name not specified "
240 "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
244 if ( bi->sql_concat_func == NULL ) {
245 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
246 "concat func not specified (use \"concat_pattern\" "
247 "directive in slapd.conf)\n", 0, 0, 0 );
249 if ( backsql_split_pattern( backsql_def_concat_func,
250 &bi->sql_concat_func, 2 ) ) {
251 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
252 "unable to parse pattern \"%s\"",
253 backsql_def_concat_func, 0, 0 );
259 * see back-sql.h for default values
261 if ( BER_BVISNULL( &bi->sql_aliasing ) ) {
262 ber_str2bv( BACKSQL_ALIASING,
263 STRLENOF( BACKSQL_ALIASING ),
264 1, &bi->sql_aliasing );
267 if ( BER_BVISNULL( &bi->sql_aliasing_quote ) ) {
268 ber_str2bv( BACKSQL_ALIASING_QUOTE,
269 STRLENOF( BACKSQL_ALIASING_QUOTE ),
270 1, &bi->sql_aliasing_quote );
274 * Prepare cast string as required
276 if ( bi->sql_upper_func.bv_val ) {
279 if ( BACKSQL_UPPER_NEEDS_CAST( bi ) ) {
280 snprintf( buf, sizeof( buf ),
281 "%s(cast (" /* ? as varchar(%d))) */ ,
282 bi->sql_upper_func.bv_val );
283 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
285 snprintf( buf, sizeof( buf ),
286 /* (cast(? */ " as varchar(%d)))",
287 BACKSQL_MAX_DN_LEN );
288 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_close );
291 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
292 bi->sql_upper_func.bv_val );
293 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
295 ber_str2bv( /* (? */ ")", 0, 1, &bi->sql_upper_func_close );
299 /* normalize filter values only if necessary */
300 bi->sql_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
301 assert( bi->sql_caseIgnoreMatch != NULL );
303 bi->sql_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
304 assert( bi->sql_telephoneNumberMatch != NULL );
306 if ( bi->sql_dbuser == NULL ) {
307 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
308 "user name not specified "
309 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
313 if ( BER_BVISNULL( &bi->sql_subtree_cond ) ) {
315 * Prepare concat function for subtree search condition
317 struct berval concat;
318 struct berval values[] = {
323 struct berbuf bb = BB_NULL;
325 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
326 "subtree search SQL condition not specified "
327 "(use \"subtree_cond\" directive in slapd.conf); "
328 "preparing default\n",
331 if ( backsql_prepare_pattern( bi->sql_concat_func, values,
333 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
334 "unable to prepare CONCAT pattern for subtree search",
339 if ( bi->sql_upper_func.bv_val ) {
342 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
345 backsql_strfcat_x( &bb, NULL, "blbbb",
347 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
348 "(ldap_entries.dn) LIKE ",
349 &bi->sql_upper_func_open,
351 &bi->sql_upper_func_close );
356 * ldap_entries.dn LIKE CONCAT('%',?)
359 backsql_strfcat_x( &bb, NULL, "lb",
360 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
361 "ldap_entries.dn LIKE ",
365 ch_free( concat.bv_val );
367 bi->sql_subtree_cond = bb.bb_val;
369 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
370 "setting \"%s\" as default \"subtree_cond\"\n",
371 bi->sql_subtree_cond.bv_val, 0, 0 );
374 if ( bi->sql_children_cond.bv_val == NULL ) {
376 * Prepare concat function for children search condition
378 struct berval concat;
379 struct berval values[] = {
384 struct berbuf bb = BB_NULL;
386 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
387 "children search SQL condition not specified "
388 "(use \"children_cond\" directive in slapd.conf); "
389 "preparing default\n",
392 if ( backsql_prepare_pattern( bi->sql_concat_func, values,
394 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
395 "unable to prepare CONCAT pattern for children search", 0, 0, 0 );
399 if ( bi->sql_upper_func.bv_val ) {
402 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
405 backsql_strfcat_x( &bb, NULL, "blbbb",
407 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
408 "(ldap_entries.dn) LIKE ",
409 &bi->sql_upper_func_open,
411 &bi->sql_upper_func_close );
416 * ldap_entries.dn LIKE CONCAT('%,',?)
419 backsql_strfcat_x( &bb, NULL, "lb",
420 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
421 "ldap_entries.dn LIKE ",
425 ch_free( concat.bv_val );
427 bi->sql_children_cond = bb.bb_val;
429 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
430 "setting \"%s\" as default \"children_cond\"\n",
431 bi->sql_children_cond.bv_val, 0, 0 );
434 if ( bi->sql_dn_match_cond.bv_val == NULL ) {
436 * Prepare concat function for dn match search condition
438 struct berbuf bb = BB_NULL;
440 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
441 "DN match search SQL condition not specified "
442 "(use \"dn_match_cond\" directive in slapd.conf); "
443 "preparing default\n",
446 if ( bi->sql_upper_func.bv_val ) {
449 * UPPER(ldap_entries.dn)=?
452 backsql_strfcat_x( &bb, NULL, "blbcb",
454 (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
455 "(ldap_entries.dn)=",
456 &bi->sql_upper_func_open,
458 &bi->sql_upper_func_close );
466 backsql_strfcat_x( &bb, NULL, "l",
467 (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
468 "ldap_entries.dn=?" );
471 bi->sql_dn_match_cond = bb.bb_val;
473 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
474 "setting \"%s\" as default \"dn_match_cond\"\n",
475 bi->sql_dn_match_cond.bv_val, 0, 0 );
478 if ( bi->sql_oc_query == NULL ) {
479 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
481 ch_strdup( backsql_def_needs_select_oc_query );
484 bi->sql_oc_query = ch_strdup( backsql_def_oc_query );
487 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
488 "objectclass mapping SQL statement not specified "
489 "(use \"oc_query\" directive in slapd.conf)\n",
491 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
492 "setting \"%s\" by default\n", bi->sql_oc_query, 0, 0 );
495 if ( bi->sql_at_query == NULL ) {
496 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
497 "attribute mapping SQL statement not specified "
498 "(use \"at_query\" directive in slapd.conf)\n",
500 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
501 "setting \"%s\" by default\n",
502 backsql_def_at_query, 0, 0 );
503 bi->sql_at_query = ch_strdup( backsql_def_at_query );
506 if ( bi->sql_insentry_stmt == NULL ) {
507 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
508 "entry insertion SQL statement not specified "
509 "(use \"insentry_stmt\" directive in slapd.conf)\n",
511 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
512 "setting \"%s\" by default\n",
513 backsql_def_insentry_stmt, 0, 0 );
514 bi->sql_insentry_stmt = ch_strdup( backsql_def_insentry_stmt );
517 if ( bi->sql_delentry_stmt == NULL ) {
518 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
519 "entry deletion SQL statement not specified "
520 "(use \"delentry_stmt\" directive in slapd.conf)\n",
522 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
523 "setting \"%s\" by default\n",
524 backsql_def_delentry_stmt, 0, 0 );
525 bi->sql_delentry_stmt = ch_strdup( backsql_def_delentry_stmt );
528 if ( bi->sql_renentry_stmt == NULL ) {
529 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
530 "entry deletion SQL statement not specified "
531 "(use \"renentry_stmt\" directive in slapd.conf)\n",
533 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
534 "setting \"%s\" by default\n",
535 backsql_def_renentry_stmt, 0, 0 );
536 bi->sql_renentry_stmt = ch_strdup( backsql_def_renentry_stmt );
539 if ( bi->sql_delobjclasses_stmt == NULL ) {
540 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
541 "objclasses deletion SQL statement not specified "
542 "(use \"delobjclasses_stmt\" directive in slapd.conf)\n",
544 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
545 "setting \"%s\" by default\n",
546 backsql_def_delobjclasses_stmt, 0, 0 );
547 bi->sql_delobjclasses_stmt = ch_strdup( backsql_def_delobjclasses_stmt );
550 /* This should just be to force schema loading */
551 connection_fake_init( &conn, &opbuf, thrctx );
554 if ( backsql_get_db_conn( op, &dbh ) != LDAP_SUCCESS ) {
555 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
556 "connection failed, exiting\n", 0, 0, 0 );
559 if ( backsql_load_schema_map( bi, dbh ) != LDAP_SUCCESS ) {
560 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
561 "schema mapping failed, exiting\n", 0, 0, 0 );
564 if ( backsql_free_db_conn( op, dbh ) != SQL_SUCCESS ) {
565 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
566 "connection free failed\n", 0, 0, 0 );
568 if ( !BACKSQL_SCHEMA_LOADED( bi ) ) {
569 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
570 "test failed, schema map not loaded - exiting\n",
576 * Prepare ID selection query
578 if ( bi->sql_id_query == NULL ) {
579 /* no custom id_query provided */
580 if ( bi->sql_upper_func.bv_val == NULL ) {
581 backsql_strcat_x( &bb, NULL, backsql_id_query, "dn=?", NULL );
584 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
585 backsql_strcat_x( &bb, NULL, backsql_id_query,
588 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
589 backsql_strfcat_x( &bb, NULL, "sbl",
592 (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
594 backsql_strfcat_x( &bb, NULL, "sblbcb",
597 (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
598 &bi->sql_upper_func_open,
600 &bi->sql_upper_func_close );
604 bi->sql_id_query = bb.bb_val.bv_val;
608 * Prepare children count query
610 BER_BVZERO( &bb.bb_val );
612 backsql_strfcat_x( &bb, NULL, "sbsb",
613 "SELECT COUNT(distinct subordinates.id) "
614 "FROM ldap_entries,ldap_entries ",
615 &bi->sql_aliasing, "subordinates "
616 "WHERE subordinates.parent=ldap_entries.id AND ",
617 &bi->sql_dn_match_cond );
618 bi->sql_has_children_query = bb.bb_val.bv_val;
621 * Prepare DN and objectClass aliasing bit of query
623 BER_BVZERO( &bb.bb_val );
625 backsql_strfcat_x( &bb, NULL, "sbbsbsbbsb",
626 " ", &bi->sql_aliasing, &bi->sql_aliasing_quote,
627 "objectClass", &bi->sql_aliasing_quote,
628 ",ldap_entries.dn ", &bi->sql_aliasing,
629 &bi->sql_aliasing_quote, "dn", &bi->sql_aliasing_quote );
630 bi->sql_dn_oc_aliasing = bb.bb_val;
632 /* should never happen! */
633 assert( bd->be_nsuffix != NULL );
635 if ( BER_BVISNULL( &bd->be_nsuffix[ 1 ] ) ) {
636 /* enable if only one suffix is defined */
637 bi->sql_flags |= BSQLF_USE_SUBTREE_SHORTCUT;
640 bi->sql_flags |= BSQLF_CHECK_SCHEMA;
642 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
643 "test succeeded, schema map loaded\n", 0, 0, 0 );
652 backsql_info *bi = (backsql_info*)bd->be_private;
654 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
656 backsql_conn_destroy( bi );
658 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
663 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
665 /* conditionally define the init_module() function */
666 SLAP_BACKEND_INIT_MODULE( sql )
668 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */