2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Dmitry Kovalev for inclusion
18 * by OpenLDAP Software.
26 #include <sys/types.h>
27 #include "ac/string.h"
30 #include "proto-sql.h"
32 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
41 memset( &bi, '\0', sizeof( bi ) );
43 bi.bi_init = backsql_initialize;
49 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */
55 static char *controls[] = {
56 #if 0 /* needs updating */
57 #ifdef LDAP_CONTROL_NOOP
59 #endif /* LDAP_CONTROL_NOOP */
61 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
62 LDAP_CONTROL_VALUESRETURNFILTER,
63 #endif /* LDAP_CONTROL_VALUESRETURNFILTER */
67 bi->bi_controls = controls;
69 Debug( LDAP_DEBUG_TRACE,"==>backsql_initialize()\n", 0, 0, 0 );
76 bi->bi_db_init = backsql_db_init;
77 bi->bi_db_config = backsql_db_config;
78 bi->bi_db_open = backsql_db_open;
79 bi->bi_db_close = backsql_db_close;
80 bi->bi_db_destroy = backsql_db_destroy;
82 bi->bi_op_abandon = 0;
83 bi->bi_op_compare = backsql_compare;
84 bi->bi_op_bind = backsql_bind;
86 bi->bi_op_search = backsql_search;
87 bi->bi_op_modify = backsql_modify;
88 bi->bi_op_modrdn = backsql_modrdn;
89 bi->bi_op_add = backsql_add;
90 bi->bi_op_delete = backsql_delete;
92 bi->bi_chk_referrals = 0;
93 bi->bi_operational = backsql_operational;
95 bi->bi_connection_init = 0;
96 bi->bi_connection_destroy = backsql_connection_destroy;
98 Debug( LDAP_DEBUG_TRACE,"<==backsql_initialize()\n", 0, 0, 0 );
107 Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
108 Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
118 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
119 bi = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
120 memset( bi, '\0', sizeof( backsql_info ) );
121 ldap_pvt_thread_mutex_init( &bi->sql_dbconn_mutex );
122 ldap_pvt_thread_mutex_init( &bi->sql_schema_mutex );
123 backsql_init_db_env( bi );
126 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
134 backsql_info *bi = (backsql_info*)bd->be_private;
136 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
137 ldap_pvt_thread_mutex_lock( &bi->sql_dbconn_mutex );
138 backsql_free_db_env( bi );
139 ldap_pvt_thread_mutex_unlock( &bi->sql_dbconn_mutex );
140 ldap_pvt_thread_mutex_destroy( &bi->sql_dbconn_mutex );
141 ldap_pvt_thread_mutex_lock( &bi->sql_schema_mutex );
142 backsql_destroy_schema_map( bi );
143 ldap_pvt_thread_mutex_unlock( &bi->sql_schema_mutex );
144 ldap_pvt_thread_mutex_destroy( &bi->sql_schema_mutex );
145 free( bi->sql_dbname );
146 free( bi->sql_dbuser );
147 if ( bi->sql_dbpasswd ) {
148 free( bi->sql_dbpasswd );
150 if ( bi->sql_dbhost ) {
151 free( bi->sql_dbhost );
153 if ( bi->sql_upper_func.bv_val ) {
154 free( bi->sql_upper_func.bv_val );
155 free( bi->sql_upper_func_open.bv_val );
156 free( bi->sql_upper_func_close.bv_val );
159 free( bi->sql_subtree_cond.bv_val );
160 free( bi->sql_oc_query );
161 free( bi->sql_at_query );
162 free( bi->sql_insentry_query );
163 free( bi->sql_delentry_query );
164 free( bi->sql_delobjclasses_query );
165 free( bi->sql_delreferrals_query );
168 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
176 backsql_info *bi = (backsql_info*)bd->be_private;
179 struct berbuf bb = BB_NULL;
181 Operation otmp = { 0 };
183 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
184 "testing RDBMS connection\n", 0, 0, 0 );
185 if ( bi->sql_dbname == NULL ) {
186 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
187 "datasource name not specified "
188 "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
192 if ( bi->sql_concat_func == NULL ) {
193 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
194 "concat func not specified (use \"concat_pattern\" "
195 "directive in slapd.conf)\n", 0, 0, 0 );
197 if ( backsql_split_pattern( backsql_def_concat_func,
198 &bi->sql_concat_func, 2 ) ) {
199 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
200 "unable to parse pattern \"%s\"",
201 backsql_def_concat_func, 0, 0 );
207 * Prepare cast string as required
209 if ( bi->sql_upper_func.bv_val ) {
212 if ( BACKSQL_UPPER_NEEDS_CAST( bi ) ) {
213 snprintf( buf, sizeof( buf ),
214 "%s(cast (" /* ? as varchar(%d))) */ ,
215 bi->sql_upper_func.bv_val );
216 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
218 snprintf( buf, sizeof( buf ),
219 /* (cast(? */ " as varchar(%d)))",
220 BACKSQL_MAX_DN_LEN );
221 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_close );
224 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
225 bi->sql_upper_func.bv_val );
226 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
228 ber_str2bv( /* (? */ ")", 0, 1, &bi->sql_upper_func_close );
232 /* normalize filter values only if necessary */
233 bi->sql_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
234 assert( bi->sql_caseIgnoreMatch );
236 bi->sql_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
237 assert( bi->sql_telephoneNumberMatch );
239 if ( bi->sql_dbuser == NULL ) {
240 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
241 "user name not specified "
242 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
246 if ( bi->sql_subtree_cond.bv_val == NULL ) {
248 * Prepare concat function for subtree search condition
250 struct berval concat;
251 struct berval values[] = {
256 struct berbuf bb = BB_NULL;
258 if ( backsql_prepare_pattern( bi->sql_concat_func, values,
260 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
261 "unable to prepare CONCAT pattern", 0, 0, 0 );
265 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
266 "subtree search SQL condition not specified "
267 "(use \"subtree_cond\" directive in slapd.conf)\n",
270 if ( bi->sql_upper_func.bv_val ) {
273 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
276 backsql_strfcat( &bb, "blbbb",
278 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
279 "(ldap_entries.dn) LIKE ",
280 &bi->sql_upper_func_open,
282 &bi->sql_upper_func_close );
287 * ldap_entries.dn LIKE CONCAT('%',?)
290 backsql_strfcat( &bb, "lb",
291 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
292 "ldap_entries.dn LIKE ",
296 bi->sql_subtree_cond = bb.bb_val;
298 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
299 "setting \"%s\" as default\n",
300 bi->sql_subtree_cond.bv_val, 0, 0 );
303 if ( bi->sql_children_cond.bv_val == NULL ) {
304 struct berbuf bb = BB_NULL;
306 if ( bi->sql_upper_func.bv_val ) {
309 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
312 backsql_strfcat( &bb, "blbl",
314 (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
315 "(ldap_entries.dn)=",
317 (ber_len_t)STRLENOF( "(?)" ), "(?)" );
322 * ldap_entries.dn LIKE CONCAT('%,',?)
325 backsql_strfcat( &bb, "l",
326 (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
327 "ldap_entries.dn=?");
330 bi->sql_children_cond = bb.bb_val;
332 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
333 "setting \"%s\" as default\n",
334 bi->sql_children_cond.bv_val, 0, 0 );
337 if ( bi->sql_oc_query == NULL ) {
338 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
340 ch_strdup( backsql_def_needs_select_oc_query );
343 bi->sql_oc_query = ch_strdup( backsql_def_oc_query );
346 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
347 "objectclass mapping SQL statement not specified "
348 "(use \"oc_query\" directive in slapd.conf)\n",
350 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
351 "setting \"%s\" by default\n", bi->sql_oc_query, 0, 0 );
354 if ( bi->sql_at_query == NULL ) {
355 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
356 "attribute mapping SQL statement not specified "
357 "(use \"at_query\" directive in slapd.conf)\n",
359 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
360 "setting \"%s\" by default\n",
361 backsql_def_at_query, 0, 0 );
362 bi->sql_at_query = ch_strdup( backsql_def_at_query );
365 if ( bi->sql_insentry_query == NULL ) {
366 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
367 "entry insertion SQL statement not specified "
368 "(use \"insentry_query\" directive in slapd.conf)\n",
370 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
371 "setting \"%s\" by default\n",
372 backsql_def_insentry_query, 0, 0 );
373 bi->sql_insentry_query = ch_strdup( backsql_def_insentry_query );
376 if ( bi->sql_delentry_query == NULL ) {
377 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
378 "entry deletion SQL statement not specified "
379 "(use \"delentry_query\" directive in slapd.conf)\n",
381 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
382 "setting \"%s\" by default\n",
383 backsql_def_delentry_query, 0, 0 );
384 bi->sql_delentry_query = ch_strdup( backsql_def_delentry_query );
387 if ( bi->sql_delobjclasses_query == NULL ) {
388 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
389 "objclasses deletion SQL statement not specified "
390 "(use \"delobjclasses_query\" directive in slapd.conf)\n",
392 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
393 "setting \"%s\" by default\n",
394 backsql_def_delobjclasses_query, 0, 0 );
395 bi->sql_delobjclasses_query = ch_strdup( backsql_def_delobjclasses_query );
398 if ( bi->sql_delreferrals_query == NULL ) {
399 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
400 "referrals deletion SQL statement not specified "
401 "(use \"delreferrals_query\" directive in slapd.conf)\n",
403 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
404 "setting \"%s\" by default\n",
405 backsql_def_delreferrals_query, 0, 0 );
406 bi->sql_delreferrals_query = ch_strdup( backsql_def_delreferrals_query );
409 otmp.o_connid = (unsigned long)(-1);
411 if ( backsql_get_db_conn( &otmp, &dbh ) != LDAP_SUCCESS ) {
412 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
413 "connection failed, exiting\n", 0, 0, 0 );
418 * Prepare ID selection query
420 bi->sql_id_query = NULL;
423 if ( bi->sql_upper_func.bv_val == NULL ) {
424 backsql_strcat( &bb, backsql_id_query, "dn=?", NULL );
427 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
428 backsql_strcat( &bb, backsql_id_query,
431 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
432 backsql_strfcat( &bb, "sbl",
435 (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
437 backsql_strfcat( &bb, "sblbcb",
440 (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
441 &bi->sql_upper_func_open,
443 &bi->sql_upper_func_close );
447 bi->sql_id_query = bb.bb_val.bv_val;
450 * Prepare children ID selection query
452 bi->sql_has_children_query = NULL;
454 bb.bb_val.bv_val = NULL;
455 bb.bb_val.bv_len = 0;
457 backsql_strfcat( &bb, "sb",
458 "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries subordinates 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( &otmp );
464 if ( !BACKSQL_SCHEMA_LOADED( bi ) ) {
465 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
466 "test failed, schema map not loaded - exiting\n",
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 )
490 o.o_connid = c->c_connid;
492 Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
493 backsql_free_db_conn( &o );
494 Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
499 #endif /* SLAPD_SQL */