2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2003 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>
31 #include "schema-map.h"
34 #ifdef SLAPD_SQL_DYNAMIC
43 memset( &bi, '\0', sizeof( bi ) );
45 bi.bi_init = sql_back_initialize;
51 #endif /* SLAPD_SQL_DYNAMIC */
57 static char *controls[] = {
58 #ifdef LDAP_CONTROL_NOOP
61 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
62 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 si = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
120 memset( si, '\0', sizeof( backsql_info ) );
121 ldap_pvt_thread_mutex_init( &si->dbconn_mutex );
122 ldap_pvt_thread_mutex_init( &si->schema_mutex );
123 backsql_init_db_env( si );
126 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
134 backsql_info *si = (backsql_info*)bd->be_private;
136 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
137 ldap_pvt_thread_mutex_lock( &si->dbconn_mutex );
138 backsql_free_db_env( si );
139 ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex );
140 ldap_pvt_thread_mutex_destroy( &si->dbconn_mutex );
141 ldap_pvt_thread_mutex_lock( &si->schema_mutex );
142 backsql_destroy_schema_map( si );
143 ldap_pvt_thread_mutex_unlock( &si->schema_mutex );
144 ldap_pvt_thread_mutex_destroy( &si->schema_mutex );
147 if ( si->dbpasswd ) {
148 free( si->dbpasswd );
153 if ( si->upper_func.bv_val ) {
154 free( si->upper_func.bv_val );
155 free( si->upper_func_open.bv_val );
156 free( si->upper_func_close.bv_val );
159 free( si->subtree_cond.bv_val );
160 free( si->oc_query );
161 free( si->at_query );
162 free( si->insentry_query );
163 free( si->delentry_query );
166 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
174 backsql_info *si = (backsql_info*)bd->be_private;
177 struct berbuf bb = BB_NULL;
181 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
182 "testing RDBMS connection\n", 0, 0, 0 );
183 if ( si->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 ( si->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 &si->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 ( si->upper_func.bv_val ) {
210 if ( BACKSQL_UPPER_NEEDS_CAST( si ) ) {
211 snprintf( buf, sizeof( buf ),
212 "%s(cast (" /* ? as varchar(%d))) */ ,
213 si->upper_func.bv_val );
214 ber_str2bv( buf, 0, 1, &si->upper_func_open );
216 snprintf( buf, sizeof( buf ),
217 /* (cast(? */ " as varchar(%d)))",
218 BACKSQL_MAX_DN_LEN );
219 ber_str2bv( buf, 0, 1, &si->upper_func_close );
222 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
223 si->upper_func.bv_val );
224 ber_str2bv( buf, 0, 1, &si->upper_func_open );
226 ber_str2bv( /* (? */ ")", 0, 1, &si->upper_func_close );
230 /* normalize filter values only if necessary */
231 si->bi_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
233 if ( si->dbuser == NULL ) {
234 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
235 "user name not specified "
236 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
240 if ( si->subtree_cond.bv_val == NULL ) {
242 * Prepare concat function for subtree search condition
244 struct berval concat;
245 struct berval values[] = {
246 { sizeof( "'%'" ) - 1, "'%'" },
247 { sizeof( "?" ) - 1, "?" },
250 struct berbuf bb = BB_NULL;
252 if ( backsql_prepare_pattern( si->concat_func, values,
254 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
255 "unable to prepare CONCAT pattern", 0, 0, 0 );
259 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
260 "subtree search SQL condition not specified "
261 "(use \"subtree_cond\" directive in slapd.conf)\n",
264 if ( si->upper_func.bv_val ) {
267 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
270 backsql_strfcat( &bb, "blbbb",
272 (ber_len_t)sizeof( "(ldap_entries.dn) LIKE " ) - 1,
273 "(ldap_entries.dn) LIKE ",
274 &si->upper_func_open,
276 &si->upper_func_close );
281 * ldap_entries.dn LIKE CONCAT('%',?)
284 backsql_strfcat( &bb, "lb",
285 (ber_len_t)sizeof( "ldap_entries.dn LIKE " ) - 1,
286 "ldap_entries.dn LIKE ",
290 si->subtree_cond = bb.bb_val;
292 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
293 "setting '%s' as default\n",
294 si->subtree_cond.bv_val, 0, 0 );
297 if ( si->children_cond.bv_val == NULL ) {
298 struct berbuf bb = BB_NULL;
300 if ( si->upper_func.bv_val ) {
303 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
306 backsql_strfcat( &bb, "blbl",
308 (ber_len_t)sizeof( "(ldap_entries.dn)=" ) - 1,
309 "(ldap_entries.dn)=",
311 (ber_len_t)sizeof( "(?)" ) - 1, "(?)" );
316 * ldap_entries.dn LIKE CONCAT('%,',?)
319 backsql_strfcat( &bb, "l",
320 (ber_len_t)sizeof( "ldap_entries.dn=?" ) - 1,
321 "ldap_entries.dn=?");
324 si->children_cond = bb.bb_val;
326 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
327 "setting '%s' as default\n",
328 si->children_cond.bv_val, 0, 0 );
331 if ( si->oc_query == NULL ) {
332 if ( BACKSQL_CREATE_NEEDS_SELECT( si ) ) {
334 ch_strdup( backsql_def_needs_select_oc_query );
337 si->oc_query = ch_strdup( backsql_def_oc_query );
340 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
341 "objectclass mapping SQL statement not specified "
342 "(use \"oc_query\" directive in slapd.conf)\n",
344 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
345 "setting '%s' by default\n", si->oc_query, 0, 0 );
348 if ( si->at_query == NULL ) {
349 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
350 "attribute mapping SQL statement not specified "
351 "(use \"at_query\" directive in slapd.conf)\n",
353 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
354 "setting '%s' by default\n",
355 backsql_def_at_query, 0, 0 );
356 si->at_query = ch_strdup( backsql_def_at_query );
359 if ( si->insentry_query == NULL ) {
360 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
361 "entry insertion SQL statement not specified "
362 "(use \"insentry_query\" directive in slapd.conf)\n",
364 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
365 "setting '%s' by default\n",
366 backsql_def_insentry_query, 0, 0 );
367 si->insentry_query = ch_strdup( backsql_def_insentry_query );
370 if ( si->delentry_query == NULL ) {
371 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
372 "entry deletion SQL statement not specified "
373 "(use \"delentry_query\" directive in slapd.conf)\n",
375 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
376 "setting '%s' by default\n",
377 backsql_def_delentry_query, 0, 0 );
378 si->delentry_query = ch_strdup( backsql_def_delentry_query );
383 if ( backsql_get_db_conn( &otmp, &dbh ) != LDAP_SUCCESS ) {
384 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
385 "connection failed, exiting\n", 0, 0, 0 );
390 * Prepare ID selection query
395 if ( si->upper_func.bv_val == NULL ) {
396 backsql_strcat( &bb, backsql_id_query, "dn=?", NULL );
398 if ( BACKSQL_HAS_LDAPINFO_DN_RU( si ) ) {
399 backsql_strcat( &bb, backsql_id_query,
402 if ( BACKSQL_USE_REVERSE_DN( si ) ) {
403 backsql_strfcat( &bb, "sbl",
406 (ber_len_t)sizeof( "(dn)=?" ) - 1, "(dn)=?" );
408 backsql_strfcat( &bb, "sblbcb",
411 (ber_len_t)sizeof( "(dn)=" ) - 1, "(dn)=",
412 &si->upper_func_open,
414 &si->upper_func_close );
418 si->id_query = bb.bb_val.bv_val;
421 * Prepare children ID selection query
423 si->has_children_query = NULL;
425 bb.bb_val.bv_val = NULL;
426 bb.bb_val.bv_len = 0;
428 backsql_strfcat( &bb, "sb",
429 "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries subordinates WHERE subordinates.parent=ldap_entries.id AND ",
431 &si->children_cond );
432 si->has_children_query = bb.bb_val.bv_val;
434 backsql_free_db_conn( &otmp );
435 if ( !BACKSQL_SCHEMA_LOADED( si ) ) {
436 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
437 "test failed, schema map not loaded - exiting\n",
442 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
443 "test succeeded, schema map loaded\n", 0, 0, 0 );
451 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
452 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
457 backsql_connection_destroy( Backend *bd, Connection *c )
461 o.o_connid = c->c_connid;
463 Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
464 backsql_free_db_conn( &o );
465 Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
469 #endif /* SLAPD_SQL */