2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 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 LDAP_CONTROL_X_TREE_DELETE
43 LDAP_CONTROL_X_TREE_DELETE,
44 #endif /* LDAP_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 );
101 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
102 bi = (backsql_info *)ch_malloc( sizeof( backsql_info ) );
103 memset( bi, '\0', sizeof( backsql_info ) );
104 ldap_pvt_thread_mutex_init( &bi->sql_dbconn_mutex );
105 ldap_pvt_thread_mutex_init( &bi->sql_schema_mutex );
106 backsql_init_db_env( bi );
109 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
117 backsql_info *bi = (backsql_info*)bd->be_private;
119 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
120 ldap_pvt_thread_mutex_lock( &bi->sql_dbconn_mutex );
121 backsql_free_db_env( bi );
122 ldap_pvt_thread_mutex_unlock( &bi->sql_dbconn_mutex );
123 ldap_pvt_thread_mutex_destroy( &bi->sql_dbconn_mutex );
124 ldap_pvt_thread_mutex_lock( &bi->sql_schema_mutex );
125 backsql_destroy_schema_map( bi );
126 ldap_pvt_thread_mutex_unlock( &bi->sql_schema_mutex );
127 ldap_pvt_thread_mutex_destroy( &bi->sql_schema_mutex );
128 free( bi->sql_dbname );
129 free( bi->sql_dbuser );
130 if ( bi->sql_dbpasswd ) {
131 free( bi->sql_dbpasswd );
133 if ( bi->sql_dbhost ) {
134 free( bi->sql_dbhost );
136 if ( bi->sql_upper_func.bv_val ) {
137 free( bi->sql_upper_func.bv_val );
138 free( bi->sql_upper_func_open.bv_val );
139 free( bi->sql_upper_func_close.bv_val );
142 free( bi->sql_subtree_cond.bv_val );
143 free( bi->sql_oc_query );
144 free( bi->sql_at_query );
145 free( bi->sql_insentry_stmt );
146 free( bi->sql_delentry_stmt );
147 free( bi->sql_renentry_stmt );
148 free( bi->sql_delobjclasses_stmt );
150 if ( bi->sql_anlist ) {
153 for ( i = 0; !BER_BVISNULL( &bi->sql_anlist[i].an_name ); i++ )
155 ch_free( bi->sql_anlist[i].an_name.bv_val );
157 ch_free( bi->sql_anlist );
160 if ( bi->sql_baseObject ) {
161 entry_free( bi->sql_baseObject );
166 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
174 backsql_info *bi = (backsql_info*)bd->be_private;
175 SQLHDBC dbh = SQL_NULL_HDBC;
176 struct berbuf bb = BB_NULL;
178 char opbuf[ OPERATION_BUFFER_SIZE ];
179 Operation* op = (Operation *)opbuf;
181 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
182 "testing RDBMS connection\n", 0, 0, 0 );
183 if ( bi->sql_dbname == NULL ) {
184 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
185 "datasource name not specified "
186 "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
190 if ( bi->sql_concat_func == NULL ) {
191 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
192 "concat func not specified (use \"concat_pattern\" "
193 "directive in slapd.conf)\n", 0, 0, 0 );
195 if ( backsql_split_pattern( backsql_def_concat_func,
196 &bi->sql_concat_func, 2 ) ) {
197 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
198 "unable to parse pattern \"%s\"",
199 backsql_def_concat_func, 0, 0 );
205 * Prepare cast string as required
207 if ( bi->sql_upper_func.bv_val ) {
210 if ( BACKSQL_UPPER_NEEDS_CAST( bi ) ) {
211 snprintf( buf, sizeof( buf ),
212 "%s(cast (" /* ? as varchar(%d))) */ ,
213 bi->sql_upper_func.bv_val );
214 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
216 snprintf( buf, sizeof( buf ),
217 /* (cast(? */ " as varchar(%d)))",
218 BACKSQL_MAX_DN_LEN );
219 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_close );
222 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
223 bi->sql_upper_func.bv_val );
224 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
226 ber_str2bv( /* (? */ ")", 0, 1, &bi->sql_upper_func_close );
230 /* normalize filter values only if necessary */
231 bi->sql_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
232 assert( bi->sql_caseIgnoreMatch );
234 bi->sql_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
235 assert( bi->sql_telephoneNumberMatch );
237 if ( bi->sql_dbuser == NULL ) {
238 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
239 "user name not specified "
240 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
244 if ( bi->sql_subtree_cond.bv_val == NULL ) {
246 * Prepare concat function for subtree search condition
248 struct berval concat;
249 struct berval values[] = {
254 struct berbuf bb = BB_NULL;
256 if ( backsql_prepare_pattern( bi->sql_concat_func, values,
258 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
259 "unable to prepare CONCAT pattern", 0, 0, 0 );
263 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
264 "subtree search SQL condition not specified "
265 "(use \"subtree_cond\" directive in slapd.conf)\n",
268 if ( bi->sql_upper_func.bv_val ) {
271 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
274 backsql_strfcat( &bb, "blbbb",
276 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
277 "(ldap_entries.dn) LIKE ",
278 &bi->sql_upper_func_open,
280 &bi->sql_upper_func_close );
285 * ldap_entries.dn LIKE CONCAT('%',?)
288 backsql_strfcat( &bb, "lb",
289 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
290 "ldap_entries.dn LIKE ",
294 bi->sql_subtree_cond = bb.bb_val;
296 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
297 "setting \"%s\" as default\n",
298 bi->sql_subtree_cond.bv_val, 0, 0 );
301 if ( bi->sql_children_cond.bv_val == NULL ) {
302 struct berbuf bb = BB_NULL;
304 if ( bi->sql_upper_func.bv_val ) {
307 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
310 backsql_strfcat( &bb, "blbl",
312 (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
313 "(ldap_entries.dn)=",
315 (ber_len_t)STRLENOF( "(?)" ), "(?)" );
320 * ldap_entries.dn LIKE CONCAT('%,',?)
323 backsql_strfcat( &bb, "l",
324 (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
325 "ldap_entries.dn=?");
328 bi->sql_children_cond = bb.bb_val;
330 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
331 "setting \"%s\" as default\n",
332 bi->sql_children_cond.bv_val, 0, 0 );
335 if ( bi->sql_oc_query == NULL ) {
336 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
338 ch_strdup( backsql_def_needs_select_oc_query );
341 bi->sql_oc_query = ch_strdup( backsql_def_oc_query );
344 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
345 "objectclass mapping SQL statement not specified "
346 "(use \"oc_query\" directive in slapd.conf)\n",
348 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
349 "setting \"%s\" by default\n", bi->sql_oc_query, 0, 0 );
352 if ( bi->sql_at_query == NULL ) {
353 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
354 "attribute mapping SQL statement not specified "
355 "(use \"at_query\" directive in slapd.conf)\n",
357 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
358 "setting \"%s\" by default\n",
359 backsql_def_at_query, 0, 0 );
360 bi->sql_at_query = ch_strdup( backsql_def_at_query );
363 if ( bi->sql_insentry_stmt == NULL ) {
364 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
365 "entry insertion SQL statement not specified "
366 "(use \"insentry_stmt\" directive in slapd.conf)\n",
368 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
369 "setting \"%s\" by default\n",
370 backsql_def_insentry_stmt, 0, 0 );
371 bi->sql_insentry_stmt = ch_strdup( backsql_def_insentry_stmt );
374 if ( bi->sql_delentry_stmt == NULL ) {
375 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
376 "entry deletion SQL statement not specified "
377 "(use \"delentry_stmt\" directive in slapd.conf)\n",
379 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
380 "setting \"%s\" by default\n",
381 backsql_def_delentry_stmt, 0, 0 );
382 bi->sql_delentry_stmt = ch_strdup( backsql_def_delentry_stmt );
385 if ( bi->sql_renentry_stmt == NULL ) {
386 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
387 "entry deletion SQL statement not specified "
388 "(use \"renentry_stmt\" directive in slapd.conf)\n",
390 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
391 "setting \"%s\" by default\n",
392 backsql_def_renentry_stmt, 0, 0 );
393 bi->sql_renentry_stmt = ch_strdup( backsql_def_renentry_stmt );
396 if ( bi->sql_delobjclasses_stmt == NULL ) {
397 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
398 "objclasses deletion SQL statement not specified "
399 "(use \"delobjclasses_stmt\" directive in slapd.conf)\n",
401 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
402 "setting \"%s\" by default\n",
403 backsql_def_delobjclasses_stmt, 0, 0 );
404 bi->sql_delobjclasses_stmt = ch_strdup( backsql_def_delobjclasses_stmt );
407 op->o_hdr = (Opheader *)&op[ 1 ];
408 op->o_connid = (unsigned long)(-1);
410 if ( backsql_get_db_conn( op, &dbh ) != LDAP_SUCCESS ) {
411 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
412 "connection failed, exiting\n", 0, 0, 0 );
417 * Prepare ID selection query
419 if ( bi->sql_id_query == NULL ) {
420 /* no custom id_query provided */
421 if ( bi->sql_upper_func.bv_val == NULL ) {
422 backsql_strcat( &bb, backsql_id_query, "dn=?", NULL );
425 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
426 backsql_strcat( &bb, backsql_id_query,
429 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
430 backsql_strfcat( &bb, "sbl",
433 (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
435 backsql_strfcat( &bb, "sblbcb",
438 (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
439 &bi->sql_upper_func_open,
441 &bi->sql_upper_func_close );
445 bi->sql_id_query = bb.bb_val.bv_val;
449 * Prepare children ID selection query
451 bi->sql_has_children_query = NULL;
453 bb.bb_val.bv_val = NULL;
454 bb.bb_val.bv_len = 0;
456 backsql_strfcat( &bb, "sb",
457 "SELECT COUNT(distinct subordinates.id) "
458 "FROM ldap_entries,ldap_entries " BACKSQL_ALIASING "subordinates "
459 "WHERE subordinates.parent=ldap_entries.id AND ",
460 &bi->sql_children_cond );
461 bi->sql_has_children_query = bb.bb_val.bv_val;
463 backsql_free_db_conn( op );
464 if ( !BACKSQL_SCHEMA_LOADED( bi ) ) {
465 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
466 "test failed, schema map not loaded - exiting\n",
471 /* should never happen! */
472 assert( bd->be_nsuffix != NULL );
474 if ( BER_BVISNULL( &bd->be_nsuffix[ 1 ] ) ) {
475 /* enable if only one suffix is defined */
476 bi->sql_flags |= BSQLF_USE_SUBTREE_SHORTCUT;
478 bi->sql_flags |= BSQLF_CHECK_SCHEMA;
480 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
481 "test succeeded, schema map loaded\n", 0, 0, 0 );
489 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
490 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
495 backsql_connection_destroy( Backend *bd, Connection *c )
497 char opbuf[ OPERATION_BUFFER_SIZE ];
498 Operation* op = (Operation *)opbuf;
500 op->o_hdr = (Opheader *)&op[ 1 ];
501 op->o_connid = c->c_connid;
504 Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
505 backsql_free_db_conn( op );
506 Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
511 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
513 /* conditionally define the init_module() function */
514 SLAP_BACKEND_INIT_MODULE( sql )
516 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */