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_VALUESRETURNFILTER
43 LDAP_CONTROL_VALUESRETURNFILTER,
44 #endif /* LDAP_CONTROL_VALUESRETURNFILTER */
48 bi->bi_controls = controls;
50 Debug( LDAP_DEBUG_TRACE,"==>sql_back_initialize()\n", 0, 0, 0 );
52 bi->bi_db_init = backsql_db_init;
53 bi->bi_db_config = backsql_db_config;
54 bi->bi_db_open = backsql_db_open;
55 bi->bi_db_close = backsql_db_close;
56 bi->bi_db_destroy = backsql_db_destroy;
58 bi->bi_op_abandon = 0;
59 bi->bi_op_compare = backsql_compare;
60 bi->bi_op_bind = backsql_bind;
62 bi->bi_op_search = backsql_search;
63 bi->bi_op_modify = backsql_modify;
64 bi->bi_op_modrdn = backsql_modrdn;
65 bi->bi_op_add = backsql_add;
66 bi->bi_op_delete = backsql_delete;
68 bi->bi_chk_referrals = 0;
69 bi->bi_operational = backsql_operational;
70 bi->bi_entry_get_rw = backsql_entry_get;
72 bi->bi_connection_init = 0;
73 bi->bi_connection_destroy = backsql_connection_destroy;
75 Debug( LDAP_DEBUG_TRACE,"<==sql_back_initialize()\n", 0, 0, 0 );
83 Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
84 Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
94 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
95 bi = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
96 memset( bi, '\0', sizeof( backsql_info ) );
97 ldap_pvt_thread_mutex_init( &bi->sql_dbconn_mutex );
98 ldap_pvt_thread_mutex_init( &bi->sql_schema_mutex );
99 backsql_init_db_env( bi );
102 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
110 backsql_info *bi = (backsql_info*)bd->be_private;
112 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
113 ldap_pvt_thread_mutex_lock( &bi->sql_dbconn_mutex );
114 backsql_free_db_env( bi );
115 ldap_pvt_thread_mutex_unlock( &bi->sql_dbconn_mutex );
116 ldap_pvt_thread_mutex_destroy( &bi->sql_dbconn_mutex );
117 ldap_pvt_thread_mutex_lock( &bi->sql_schema_mutex );
118 backsql_destroy_schema_map( bi );
119 ldap_pvt_thread_mutex_unlock( &bi->sql_schema_mutex );
120 ldap_pvt_thread_mutex_destroy( &bi->sql_schema_mutex );
121 free( bi->sql_dbname );
122 free( bi->sql_dbuser );
123 if ( bi->sql_dbpasswd ) {
124 free( bi->sql_dbpasswd );
126 if ( bi->sql_dbhost ) {
127 free( bi->sql_dbhost );
129 if ( bi->sql_upper_func.bv_val ) {
130 free( bi->sql_upper_func.bv_val );
131 free( bi->sql_upper_func_open.bv_val );
132 free( bi->sql_upper_func_close.bv_val );
135 free( bi->sql_subtree_cond.bv_val );
136 free( bi->sql_oc_query );
137 free( bi->sql_at_query );
138 free( bi->sql_insentry_stmt );
139 free( bi->sql_delentry_stmt );
140 free( bi->sql_delobjclasses_stmt );
141 free( bi->sql_delreferrals_stmt );
143 if ( bi->sql_anlist ) {
146 for ( i = 0; !BER_BVISNULL( &bi->sql_anlist[i].an_name ); i++ )
148 ch_free( bi->sql_anlist[i].an_name.bv_val );
150 ch_free( bi->sql_anlist );
153 if ( bi->sql_baseObject ) {
154 entry_free( bi->sql_baseObject );
159 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
167 backsql_info *bi = (backsql_info*)bd->be_private;
168 SQLHDBC dbh = SQL_NULL_HDBC;
169 struct berbuf bb = BB_NULL;
171 char opbuf[ OPERATION_BUFFER_SIZE ];
172 Operation* op = (Operation *)opbuf;
174 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
175 "testing RDBMS connection\n", 0, 0, 0 );
176 if ( bi->sql_dbname == NULL ) {
177 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
178 "datasource name not specified "
179 "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
183 if ( bi->sql_concat_func == NULL ) {
184 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
185 "concat func not specified (use \"concat_pattern\" "
186 "directive in slapd.conf)\n", 0, 0, 0 );
188 if ( backsql_split_pattern( backsql_def_concat_func,
189 &bi->sql_concat_func, 2 ) ) {
190 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
191 "unable to parse pattern \"%s\"",
192 backsql_def_concat_func, 0, 0 );
198 * Prepare cast string as required
200 if ( bi->sql_upper_func.bv_val ) {
203 if ( BACKSQL_UPPER_NEEDS_CAST( bi ) ) {
204 snprintf( buf, sizeof( buf ),
205 "%s(cast (" /* ? as varchar(%d))) */ ,
206 bi->sql_upper_func.bv_val );
207 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
209 snprintf( buf, sizeof( buf ),
210 /* (cast(? */ " as varchar(%d)))",
211 BACKSQL_MAX_DN_LEN );
212 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_close );
215 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
216 bi->sql_upper_func.bv_val );
217 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
219 ber_str2bv( /* (? */ ")", 0, 1, &bi->sql_upper_func_close );
223 /* normalize filter values only if necessary */
224 bi->sql_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
225 assert( bi->sql_caseIgnoreMatch );
227 bi->sql_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
228 assert( bi->sql_telephoneNumberMatch );
230 if ( bi->sql_dbuser == NULL ) {
231 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
232 "user name not specified "
233 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
237 if ( bi->sql_subtree_cond.bv_val == NULL ) {
239 * Prepare concat function for subtree search condition
241 struct berval concat;
242 struct berval values[] = {
247 struct berbuf bb = BB_NULL;
249 if ( backsql_prepare_pattern( bi->sql_concat_func, values,
251 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
252 "unable to prepare CONCAT pattern", 0, 0, 0 );
256 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
257 "subtree search SQL condition not specified "
258 "(use \"subtree_cond\" directive in slapd.conf)\n",
261 if ( bi->sql_upper_func.bv_val ) {
264 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
267 backsql_strfcat( &bb, "blbbb",
269 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
270 "(ldap_entries.dn) LIKE ",
271 &bi->sql_upper_func_open,
273 &bi->sql_upper_func_close );
278 * ldap_entries.dn LIKE CONCAT('%',?)
281 backsql_strfcat( &bb, "lb",
282 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
283 "ldap_entries.dn LIKE ",
287 bi->sql_subtree_cond = bb.bb_val;
289 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
290 "setting \"%s\" as default\n",
291 bi->sql_subtree_cond.bv_val, 0, 0 );
294 if ( bi->sql_children_cond.bv_val == NULL ) {
295 struct berbuf bb = BB_NULL;
297 if ( bi->sql_upper_func.bv_val ) {
300 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
303 backsql_strfcat( &bb, "blbl",
305 (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
306 "(ldap_entries.dn)=",
308 (ber_len_t)STRLENOF( "(?)" ), "(?)" );
313 * ldap_entries.dn LIKE CONCAT('%,',?)
316 backsql_strfcat( &bb, "l",
317 (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
318 "ldap_entries.dn=?");
321 bi->sql_children_cond = bb.bb_val;
323 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
324 "setting \"%s\" as default\n",
325 bi->sql_children_cond.bv_val, 0, 0 );
328 if ( bi->sql_oc_query == NULL ) {
329 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
331 ch_strdup( backsql_def_needs_select_oc_query );
334 bi->sql_oc_query = ch_strdup( backsql_def_oc_query );
337 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
338 "objectclass mapping SQL statement not specified "
339 "(use \"oc_query\" directive in slapd.conf)\n",
341 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
342 "setting \"%s\" by default\n", bi->sql_oc_query, 0, 0 );
345 if ( bi->sql_at_query == NULL ) {
346 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
347 "attribute mapping SQL statement not specified "
348 "(use \"at_query\" directive in slapd.conf)\n",
350 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
351 "setting \"%s\" by default\n",
352 backsql_def_at_query, 0, 0 );
353 bi->sql_at_query = ch_strdup( backsql_def_at_query );
356 if ( bi->sql_insentry_stmt == NULL ) {
357 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
358 "entry insertion SQL statement not specified "
359 "(use \"insentry_stmt\" directive in slapd.conf)\n",
361 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
362 "setting \"%s\" by default\n",
363 backsql_def_insentry_stmt, 0, 0 );
364 bi->sql_insentry_stmt = ch_strdup( backsql_def_insentry_stmt );
367 if ( bi->sql_delentry_stmt == NULL ) {
368 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
369 "entry deletion SQL statement not specified "
370 "(use \"delentry_stmt\" directive in slapd.conf)\n",
372 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
373 "setting \"%s\" by default\n",
374 backsql_def_delentry_stmt, 0, 0 );
375 bi->sql_delentry_stmt = ch_strdup( backsql_def_delentry_stmt );
378 if ( bi->sql_delobjclasses_stmt == NULL ) {
379 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
380 "objclasses deletion SQL statement not specified "
381 "(use \"delobjclasses_stmt\" directive in slapd.conf)\n",
383 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
384 "setting \"%s\" by default\n",
385 backsql_def_delobjclasses_stmt, 0, 0 );
386 bi->sql_delobjclasses_stmt = ch_strdup( backsql_def_delobjclasses_stmt );
389 if ( bi->sql_delreferrals_stmt == NULL ) {
390 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
391 "referrals deletion SQL statement not specified "
392 "(use \"delreferrals_stmt\" directive in slapd.conf)\n",
394 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
395 "setting \"%s\" by default\n",
396 backsql_def_delreferrals_stmt, 0, 0 );
397 bi->sql_delreferrals_stmt = ch_strdup( backsql_def_delreferrals_stmt );
400 op->o_hdr = (Opheader *)&op[ 1 ];
401 op->o_connid = (unsigned long)(-1);
403 if ( backsql_get_db_conn( op, &dbh ) != LDAP_SUCCESS ) {
404 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
405 "connection failed, exiting\n", 0, 0, 0 );
410 * Prepare ID selection query
412 if ( bi->sql_id_query == NULL ) {
413 /* no custom id_query provided */
414 if ( bi->sql_upper_func.bv_val == NULL ) {
415 backsql_strcat( &bb, backsql_id_query, "dn=?", NULL );
418 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
419 backsql_strcat( &bb, backsql_id_query,
422 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
423 backsql_strfcat( &bb, "sbl",
426 (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
428 backsql_strfcat( &bb, "sblbcb",
431 (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
432 &bi->sql_upper_func_open,
434 &bi->sql_upper_func_close );
438 bi->sql_id_query = bb.bb_val.bv_val;
442 * Prepare children ID selection query
444 bi->sql_has_children_query = NULL;
446 bb.bb_val.bv_val = NULL;
447 bb.bb_val.bv_len = 0;
449 backsql_strfcat( &bb, "sb",
450 "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries subordinates WHERE subordinates.parent=ldap_entries.id AND ",
452 &bi->sql_children_cond );
453 bi->sql_has_children_query = bb.bb_val.bv_val;
455 backsql_free_db_conn( op );
456 if ( !BACKSQL_SCHEMA_LOADED( bi ) ) {
457 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
458 "test failed, schema map not loaded - exiting\n",
463 /* should never happen! */
464 assert( bd->be_nsuffix != NULL );
466 if ( BER_BVISNULL( &bd->be_nsuffix[ 1 ] ) ) {
467 /* enable if only one suffix is defined */
468 bi->sql_flags |= BSQLF_USE_SUBTREE_SHORTCUT;
471 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
472 "test succeeded, schema map loaded\n", 0, 0, 0 );
480 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
481 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
486 backsql_connection_destroy( Backend *bd, Connection *c )
488 char opbuf[ OPERATION_BUFFER_SIZE ];
489 Operation* op = (Operation *)opbuf;
491 op->o_hdr = (Opheader *)&op[ 1 ];
492 op->o_connid = c->c_connid;
495 Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
496 backsql_free_db_conn( op );
497 Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
502 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
504 /* conditionally define the init_module() function */
505 SLAP_BACKEND_INIT_MODULE( sql )
507 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */