2 * Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
4 * Redistribution and use in source and binary forms are permitted only
5 * as authorized by the OpenLDAP Public License. A copy of this
6 * license is available at http://www.OpenLDAP.org/license.html or
7 * in file LICENSE in the top-level directory of the distribution.
15 #include <sys/types.h>
20 #include "schema-map.h"
23 #ifdef SLAPD_SQL_DYNAMIC
26 backsql_LTX_init_module(
32 memset( &bi, '\0', sizeof( bi ) );
34 bi.bi_init = backbacksql_initialize;
40 #endif /* SLAPD_SHELL_DYNAMIC */
46 Debug( LDAP_DEBUG_TRACE,"==>backsql_initialize()\n", 0, 0, 0 );
53 bi->bi_db_init = backsql_db_init;
54 bi->bi_db_config = backsql_db_config;
55 bi->bi_db_open = backsql_db_open;
56 bi->bi_db_close = backsql_db_close;
57 bi->bi_db_destroy = backsql_db_destroy;
59 #ifdef BACKSQL_ALL_DONE
60 bi->bi_op_abandon = backsql_abandon;
61 bi->bi_op_compare = backsql_compare;
63 bi->bi_op_abandon = 0;
64 bi->bi_op_compare = 0;
66 bi->bi_op_bind = backsql_bind;
67 bi->bi_op_unbind = backsql_unbind;
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;
75 bi->bi_acl_attribute = 0;
76 bi->bi_chk_referrals = 0;
77 bi->bi_operational = backsql_operational;
79 bi->bi_connection_init = 0;
80 bi->bi_connection_destroy = backsql_connection_destroy;
82 Debug( LDAP_DEBUG_TRACE,"<==backsql_initialize()\n", 0, 0, 0 );
91 Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
92 Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
102 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
103 si = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
104 memset( si, '\0', sizeof( backsql_info ) );
105 ldap_pvt_thread_mutex_init( &si->dbconn_mutex );
106 ldap_pvt_thread_mutex_init( &si->schema_mutex );
107 backsql_init_db_env( si );
110 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
118 backsql_info *si = (backsql_info*)bd->be_private;
120 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
121 ldap_pvt_thread_mutex_lock( &si->dbconn_mutex );
122 backsql_free_db_env( si );
123 ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex );
124 ldap_pvt_thread_mutex_destroy( &si->dbconn_mutex );
125 ldap_pvt_thread_mutex_lock( &si->schema_mutex );
126 backsql_destroy_schema_map( si );
127 ldap_pvt_thread_mutex_unlock( &si->schema_mutex );
128 ldap_pvt_thread_mutex_destroy( &si->schema_mutex );
131 if ( si->dbpasswd ) {
132 free( si->dbpasswd );
137 if ( si->upper_func.bv_val ) {
138 free( si->upper_func.bv_val );
139 free( si->upper_func_open.bv_val );
140 free( si->upper_func_close.bv_val );
143 free( si->subtree_cond.bv_val );
144 free( si->oc_query );
145 free( si->at_query );
146 free( si->insentry_query );
147 free( si->delentry_query );
150 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
158 backsql_info *si = (backsql_info*)bd->be_private;
164 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
165 "testing RDBMS connection\n", 0, 0, 0 );
166 if ( si->dbname == NULL ) {
167 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
168 "datasource name not specified "
169 "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
173 if ( si->concat_func == NULL ) {
174 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
175 "concat func not specified (use \"concat_pattern\" "
176 "directive in slapd.conf)\n", 0, 0, 0 );
178 if ( backsql_split_pattern( backsql_def_concat_func,
179 &si->concat_func, 2 ) ) {
180 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
181 "unable to parse pattern '%s'",
182 backsql_def_concat_func, 0, 0 );
188 * Prepare cast string as required
190 if ( si->upper_func.bv_val ) {
193 if ( BACKSQL_UPPER_NEEDS_CAST( si ) ) {
194 snprintf( buf, sizeof( buf ),
195 "%s(cast (" /* ? as varchar(%d))) */ ,
196 si->upper_func.bv_val );
197 ber_str2bv( buf, 0, 1, &si->upper_func_open );
199 snprintf( buf, sizeof( buf ),
200 /* (cast(? */ " as varchar(%d)))",
201 BACKSQL_MAX_DN_LEN );
202 ber_str2bv( buf, 0, 1, &si->upper_func_close );
205 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
206 si->upper_func.bv_val );
207 ber_str2bv( buf, 0, 1, &si->upper_func_open );
209 ber_str2bv( /* (? */ ")", 0, 1, &si->upper_func_close );
213 if ( si->dbuser == NULL ) {
214 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
215 "user name not specified "
216 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
220 if ( si->subtree_cond.bv_val == NULL ) {
222 * Prepare concat function for subtree search condition
224 struct berval concat;
226 struct berval values[] = {
227 { sizeof( "'%'" ) - 1, "'%'" },
228 { sizeof( "?" ) - 1, "?" },
232 if ( backsql_prepare_pattern( si->concat_func, values,
234 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
235 "unable to prepare CONCAT pattern", 0, 0, 0 );
239 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
240 "subtree search SQL condition not specified "
241 "(use \"subtree_cond\" directive in slapd.conf)\n",
244 si->subtree_cond.bv_val = NULL;
245 si->subtree_cond.bv_len = 0;
247 if ( si->upper_func.bv_val ) {
250 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
253 backsql_strfcat( &si->subtree_cond, &len, "blbbb",
255 (ber_len_t)sizeof( "(ldap_entries.dn) LIKE " ) - 1,
256 "(ldap_entries.dn) LIKE ",
257 &si->upper_func_open,
259 &si->upper_func_close );
264 * ldap_entries.dn LIKE CONCAT('%',?)
267 backsql_strfcat( &si->subtree_cond, &len, "lb",
268 (ber_len_t)sizeof( "ldap_entries.dn LIKE " ) - 1,
269 "ldap_entries.dn LIKE ",
273 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
274 "setting '%s' as default\n",
275 si->subtree_cond.bv_val, 0, 0 );
278 if ( si->children_cond.bv_val == NULL ) {
280 * Prepare concat function for children search condition
282 struct berval concat;
284 struct berval values[] = {
285 { sizeof( "'%,'" ) - 1, "'%,'" },
286 { sizeof( "?" ) - 1, "?" },
290 if ( backsql_prepare_pattern( si->concat_func, values,
292 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
293 "unable to prepare CONCAT pattern", 0, 0, 0 );
297 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
298 "children search SQL condition not specified "
299 "(use \"children_cond\" directive in slapd.conf)\n",
302 si->children_cond.bv_val = NULL;
303 si->children_cond.bv_len = 0;
305 if ( si->upper_func.bv_val ) {
308 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
311 backsql_strfcat( &si->children_cond, &len, "blbbb",
313 (ber_len_t)sizeof( "(ldap_entries.dn) LIKE " ) - 1,
314 "(ldap_entries.dn) LIKE ",
315 &si->upper_func_open,
317 &si->upper_func_close );
322 * ldap_entries.dn LIKE CONCAT('%,',?)
325 backsql_strfcat( &si->children_cond, &len, "lb",
326 (ber_len_t)sizeof( "ldap_entries.dn LIKE " ) - 1,
327 "ldap_entries.dn LIKE ",
331 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
332 "setting '%s' as default\n",
333 si->children_cond.bv_val, 0, 0 );
336 if ( si->oc_query == NULL ) {
337 if ( BACKSQL_CREATE_NEEDS_SELECT( si ) ) {
339 ch_strdup( backsql_def_needs_select_oc_query );
342 si->oc_query = ch_strdup( backsql_def_oc_query );
345 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
346 "objectclass mapping SQL statement not specified "
347 "(use \"oc_query\" directive in slapd.conf)\n",
349 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
350 "setting '%s' by default\n", si->oc_query, 0, 0 );
353 if ( si->at_query == NULL ) {
354 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
355 "attribute mapping SQL statement not specified "
356 "(use \"at_query\" directive in slapd.conf)\n",
358 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
359 "setting '%s' by default\n",
360 backsql_def_at_query, 0, 0 );
361 si->at_query = ch_strdup( backsql_def_at_query );
364 if ( si->insentry_query == NULL ) {
365 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
366 "entry insertion SQL statement not specified "
367 "(use \"insentry_query\" directive in slapd.conf)\n",
369 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
370 "setting '%s' by default\n",
371 backsql_def_insentry_query, 0, 0 );
372 si->insentry_query = ch_strdup( backsql_def_insentry_query );
375 if ( si->delentry_query == NULL ) {
376 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
377 "entry deletion SQL statement not specified "
378 "(use \"delentry_query\" directive in slapd.conf)\n",
380 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
381 "setting '%s' by default\n",
382 backsql_def_delentry_query, 0, 0 );
383 si->delentry_query = ch_strdup( backsql_def_delentry_query );
388 if ( backsql_get_db_conn( bd, &tmp, &dbh ) != LDAP_SUCCESS ) {
389 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
390 "connection failed, exiting\n", 0, 0, 0 );
395 * Prepare ID selection query
402 if ( si->upper_func.bv_val == NULL ) {
403 backsql_strcat( &bv, &idq_len, backsql_id_query,
406 if ( BACKSQL_HAS_LDAPINFO_DN_RU( si ) ) {
407 backsql_strcat( &bv, &idq_len, backsql_id_query,
410 if ( BACKSQL_USE_REVERSE_DN( si ) ) {
411 backsql_strfcat( &bv, &idq_len, "sbl",
414 (ber_len_t)sizeof( "(dn)=?" ) - 1, "(dn)=?" );
416 backsql_strfcat( &bv, &idq_len, "sblbcb",
419 (ber_len_t)sizeof( "(dn)=" ) - 1, "(dn)=",
420 &si->upper_func_open,
422 &si->upper_func_close );
426 si->id_query = bv.bv_val;
429 * Prepare children ID selection query
431 si->has_children_query = NULL;
436 backsql_strfcat( &bv, &idq_len, "sb",
437 "select count(*) from ldap_entries where ",
438 &si->children_cond );
439 si->has_children_query = bv.bv_val;
441 backsql_free_db_conn( bd, &tmp );
442 if ( !BACKSQL_SCHEMA_LOADED( si ) ) {
443 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
444 "test failed, schema map not loaded - exiting\n",
449 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
450 "test succeeded, schema map loaded\n", 0, 0, 0 );
458 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
459 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
464 backsql_connection_destroy(
468 Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
469 backsql_free_db_conn( be, conn );
470 Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
474 #endif /* SLAPD_SQL */