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.
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"
31 #include "proto-sql.h"
33 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
42 memset( &bi, '\0', sizeof( bi ) );
44 bi.bi_init = backsql_initialize;
50 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */
56 static char *controls[] = {
57 #if 0 /* needs updating */
58 #ifdef LDAP_CONTROL_NOOP
60 #endif /* LDAP_CONTROL_NOOP */
62 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
63 LDAP_CONTROL_VALUESRETURNFILTER,
64 #endif /* LDAP_CONTROL_VALUESRETURNFILTER */
68 bi->bi_controls = controls;
70 Debug( LDAP_DEBUG_TRACE,"==>backsql_initialize()\n", 0, 0, 0 );
77 bi->bi_db_init = backsql_db_init;
78 bi->bi_db_config = backsql_db_config;
79 bi->bi_db_open = backsql_db_open;
80 bi->bi_db_close = backsql_db_close;
81 bi->bi_db_destroy = backsql_db_destroy;
83 bi->bi_op_abandon = 0;
84 bi->bi_op_compare = backsql_compare;
85 bi->bi_op_bind = backsql_bind;
87 bi->bi_op_search = backsql_search;
88 bi->bi_op_modify = backsql_modify;
89 bi->bi_op_modrdn = backsql_modrdn;
90 bi->bi_op_add = backsql_add;
91 bi->bi_op_delete = backsql_delete;
93 bi->bi_chk_referrals = 0;
94 bi->bi_operational = backsql_operational;
96 bi->bi_connection_init = 0;
97 bi->bi_connection_destroy = backsql_connection_destroy;
99 Debug( LDAP_DEBUG_TRACE,"<==backsql_initialize()\n", 0, 0, 0 );
108 Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
109 Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
119 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
120 si = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
121 memset( si, '\0', sizeof( backsql_info ) );
122 ldap_pvt_thread_mutex_init( &si->dbconn_mutex );
123 ldap_pvt_thread_mutex_init( &si->schema_mutex );
124 backsql_init_db_env( si );
127 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
135 backsql_info *si = (backsql_info*)bd->be_private;
137 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
138 ldap_pvt_thread_mutex_lock( &si->dbconn_mutex );
139 backsql_free_db_env( si );
140 ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex );
141 ldap_pvt_thread_mutex_destroy( &si->dbconn_mutex );
142 ldap_pvt_thread_mutex_lock( &si->schema_mutex );
143 backsql_destroy_schema_map( si );
144 ldap_pvt_thread_mutex_unlock( &si->schema_mutex );
145 ldap_pvt_thread_mutex_destroy( &si->schema_mutex );
148 if ( si->dbpasswd ) {
149 free( si->dbpasswd );
154 if ( si->upper_func.bv_val ) {
155 free( si->upper_func.bv_val );
156 free( si->upper_func_open.bv_val );
157 free( si->upper_func_close.bv_val );
160 free( si->subtree_cond.bv_val );
161 free( si->oc_query );
162 free( si->at_query );
163 free( si->insentry_query );
164 free( si->delentry_query );
165 free( si->delobjclasses_query );
166 free( si->delreferrals_query );
169 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
177 backsql_info *si = (backsql_info*)bd->be_private;
180 struct berbuf bb = BB_NULL;
182 Operation otmp = { 0 };
184 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
185 "testing RDBMS connection\n", 0, 0, 0 );
186 if ( si->dbname == NULL ) {
187 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
188 "datasource name not specified "
189 "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
193 if ( si->concat_func == NULL ) {
194 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
195 "concat func not specified (use \"concat_pattern\" "
196 "directive in slapd.conf)\n", 0, 0, 0 );
198 if ( backsql_split_pattern( backsql_def_concat_func,
199 &si->concat_func, 2 ) ) {
200 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
201 "unable to parse pattern \"%s\"",
202 backsql_def_concat_func, 0, 0 );
208 * Prepare cast string as required
210 if ( si->upper_func.bv_val ) {
213 if ( BACKSQL_UPPER_NEEDS_CAST( si ) ) {
214 snprintf( buf, sizeof( buf ),
215 "%s(cast (" /* ? as varchar(%d))) */ ,
216 si->upper_func.bv_val );
217 ber_str2bv( buf, 0, 1, &si->upper_func_open );
219 snprintf( buf, sizeof( buf ),
220 /* (cast(? */ " as varchar(%d)))",
221 BACKSQL_MAX_DN_LEN );
222 ber_str2bv( buf, 0, 1, &si->upper_func_close );
225 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
226 si->upper_func.bv_val );
227 ber_str2bv( buf, 0, 1, &si->upper_func_open );
229 ber_str2bv( /* (? */ ")", 0, 1, &si->upper_func_close );
233 /* normalize filter values only if necessary */
234 si->bi_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
235 assert( si->bi_caseIgnoreMatch );
237 si->bi_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
238 assert( si->bi_telephoneNumberMatch );
240 if ( si->dbuser == NULL ) {
241 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
242 "user name not specified "
243 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
247 if ( si->subtree_cond.bv_val == NULL ) {
249 * Prepare concat function for subtree search condition
251 struct berval concat;
252 struct berval values[] = {
257 struct berbuf bb = BB_NULL;
259 if ( backsql_prepare_pattern( si->concat_func, values,
261 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
262 "unable to prepare CONCAT pattern", 0, 0, 0 );
266 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
267 "subtree search SQL condition not specified "
268 "(use \"subtree_cond\" directive in slapd.conf)\n",
271 if ( si->upper_func.bv_val ) {
274 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
277 backsql_strfcat( &bb, "blbbb",
279 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
280 "(ldap_entries.dn) LIKE ",
281 &si->upper_func_open,
283 &si->upper_func_close );
288 * ldap_entries.dn LIKE CONCAT('%',?)
291 backsql_strfcat( &bb, "lb",
292 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
293 "ldap_entries.dn LIKE ",
297 si->subtree_cond = bb.bb_val;
299 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
300 "setting \"%s\" as default\n",
301 si->subtree_cond.bv_val, 0, 0 );
304 if ( si->children_cond.bv_val == NULL ) {
305 struct berbuf bb = BB_NULL;
307 if ( si->upper_func.bv_val ) {
310 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
313 backsql_strfcat( &bb, "blbl",
315 (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
316 "(ldap_entries.dn)=",
318 (ber_len_t)STRLENOF( "(?)" ), "(?)" );
323 * ldap_entries.dn LIKE CONCAT('%,',?)
326 backsql_strfcat( &bb, "l",
327 (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
328 "ldap_entries.dn=?");
331 si->children_cond = bb.bb_val;
333 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
334 "setting \"%s\" as default\n",
335 si->children_cond.bv_val, 0, 0 );
338 if ( si->oc_query == NULL ) {
339 if ( BACKSQL_CREATE_NEEDS_SELECT( si ) ) {
341 ch_strdup( backsql_def_needs_select_oc_query );
344 si->oc_query = ch_strdup( backsql_def_oc_query );
347 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
348 "objectclass mapping SQL statement not specified "
349 "(use \"oc_query\" directive in slapd.conf)\n",
351 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
352 "setting \"%s\" by default\n", si->oc_query, 0, 0 );
355 if ( si->at_query == NULL ) {
356 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
357 "attribute mapping SQL statement not specified "
358 "(use \"at_query\" directive in slapd.conf)\n",
360 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
361 "setting \"%s\" by default\n",
362 backsql_def_at_query, 0, 0 );
363 si->at_query = ch_strdup( backsql_def_at_query );
366 if ( si->insentry_query == NULL ) {
367 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
368 "entry insertion SQL statement not specified "
369 "(use \"insentry_query\" directive in slapd.conf)\n",
371 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
372 "setting \"%s\" by default\n",
373 backsql_def_insentry_query, 0, 0 );
374 si->insentry_query = ch_strdup( backsql_def_insentry_query );
377 if ( si->delentry_query == NULL ) {
378 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
379 "entry deletion SQL statement not specified "
380 "(use \"delentry_query\" directive in slapd.conf)\n",
382 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
383 "setting \"%s\" by default\n",
384 backsql_def_delentry_query, 0, 0 );
385 si->delentry_query = ch_strdup( backsql_def_delentry_query );
388 if ( si->delobjclasses_query == NULL ) {
389 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
390 "objclasses deletion SQL statement not specified "
391 "(use \"delobjclasses_query\" directive in slapd.conf)\n",
393 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
394 "setting \"%s\" by default\n",
395 backsql_def_delobjclasses_query, 0, 0 );
396 si->delobjclasses_query = ch_strdup( backsql_def_delobjclasses_query );
399 if ( si->delreferrals_query == NULL ) {
400 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
401 "referrals deletion SQL statement not specified "
402 "(use \"delreferrals_query\" directive in slapd.conf)\n",
404 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
405 "setting \"%s\" by default\n",
406 backsql_def_delreferrals_query, 0, 0 );
407 si->delreferrals_query = ch_strdup( backsql_def_delreferrals_query );
410 otmp.o_connid = (unsigned long)(-1);
412 if ( backsql_get_db_conn( &otmp, &dbh ) != LDAP_SUCCESS ) {
413 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
414 "connection failed, exiting\n", 0, 0, 0 );
419 * Prepare ID selection query
424 if ( si->upper_func.bv_val == NULL ) {
425 backsql_strcat( &bb, backsql_id_query, "dn=?", NULL );
428 if ( BACKSQL_HAS_LDAPINFO_DN_RU( si ) ) {
429 backsql_strcat( &bb, backsql_id_query,
432 if ( BACKSQL_USE_REVERSE_DN( si ) ) {
433 backsql_strfcat( &bb, "sbl",
436 (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
438 backsql_strfcat( &bb, "sblbcb",
441 (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
442 &si->upper_func_open,
444 &si->upper_func_close );
448 si->id_query = bb.bb_val.bv_val;
451 * Prepare children ID selection query
453 si->has_children_query = NULL;
455 bb.bb_val.bv_val = NULL;
456 bb.bb_val.bv_len = 0;
458 backsql_strfcat( &bb, "sb",
459 "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries subordinates WHERE subordinates.parent=ldap_entries.id AND ",
461 &si->children_cond );
462 si->has_children_query = bb.bb_val.bv_val;
464 backsql_free_db_conn( &otmp );
465 if ( !BACKSQL_SCHEMA_LOADED( si ) ) {
466 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
467 "test failed, schema map not loaded - exiting\n",
472 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
473 "test succeeded, schema map loaded\n", 0, 0, 0 );
481 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
482 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
487 backsql_connection_destroy( Backend *bd, Connection *c )
491 o.o_connid = c->c_connid;
493 Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
494 backsql_free_db_conn( &o );
495 Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
500 #endif /* SLAPD_SQL */