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.
24 #include <sys/types.h>
25 #include "ac/string.h"
28 #include "proto-sql.h"
34 static char *controls[] = {
35 #if 0 /* needs improvements */
36 #ifdef LDAP_CONTROL_NOOP
38 #endif /* LDAP_CONTROL_NOOP */
40 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
41 LDAP_CONTROL_VALUESRETURNFILTER,
42 #endif /* LDAP_CONTROL_VALUESRETURNFILTER */
46 bi->bi_controls = controls;
48 Debug( LDAP_DEBUG_TRACE,"==>sql_back_initialize()\n", 0, 0, 0 );
50 bi->bi_db_init = backsql_db_init;
51 bi->bi_db_config = backsql_db_config;
52 bi->bi_db_open = backsql_db_open;
53 bi->bi_db_close = backsql_db_close;
54 bi->bi_db_destroy = backsql_db_destroy;
56 bi->bi_op_abandon = 0;
57 bi->bi_op_compare = backsql_compare;
58 bi->bi_op_bind = backsql_bind;
60 bi->bi_op_search = backsql_search;
61 bi->bi_op_modify = backsql_modify;
62 bi->bi_op_modrdn = backsql_modrdn;
63 bi->bi_op_add = backsql_add;
64 bi->bi_op_delete = backsql_delete;
66 bi->bi_chk_referrals = 0;
67 bi->bi_operational = backsql_operational;
68 bi->bi_entry_get_rw = backsql_entry_get;
70 bi->bi_connection_init = 0;
71 bi->bi_connection_destroy = backsql_connection_destroy;
73 Debug( LDAP_DEBUG_TRACE,"<==sql_back_initialize()\n", 0, 0, 0 );
81 Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
82 Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
92 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
93 bi = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
94 memset( bi, '\0', sizeof( backsql_info ) );
95 ldap_pvt_thread_mutex_init( &bi->sql_dbconn_mutex );
96 ldap_pvt_thread_mutex_init( &bi->sql_schema_mutex );
97 backsql_init_db_env( bi );
100 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
108 backsql_info *bi = (backsql_info*)bd->be_private;
110 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
111 ldap_pvt_thread_mutex_lock( &bi->sql_dbconn_mutex );
112 backsql_free_db_env( bi );
113 ldap_pvt_thread_mutex_unlock( &bi->sql_dbconn_mutex );
114 ldap_pvt_thread_mutex_destroy( &bi->sql_dbconn_mutex );
115 ldap_pvt_thread_mutex_lock( &bi->sql_schema_mutex );
116 backsql_destroy_schema_map( bi );
117 ldap_pvt_thread_mutex_unlock( &bi->sql_schema_mutex );
118 ldap_pvt_thread_mutex_destroy( &bi->sql_schema_mutex );
119 free( bi->sql_dbname );
120 free( bi->sql_dbuser );
121 if ( bi->sql_dbpasswd ) {
122 free( bi->sql_dbpasswd );
124 if ( bi->sql_dbhost ) {
125 free( bi->sql_dbhost );
127 if ( bi->sql_upper_func.bv_val ) {
128 free( bi->sql_upper_func.bv_val );
129 free( bi->sql_upper_func_open.bv_val );
130 free( bi->sql_upper_func_close.bv_val );
133 free( bi->sql_subtree_cond.bv_val );
134 free( bi->sql_oc_query );
135 free( bi->sql_at_query );
136 free( bi->sql_insentry_query );
137 free( bi->sql_delentry_query );
138 free( bi->sql_delobjclasses_query );
139 free( bi->sql_delreferrals_query );
141 if ( bi->sql_baseObject ) {
142 entry_free( bi->sql_baseObject );
147 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
155 backsql_info *bi = (backsql_info*)bd->be_private;
158 struct berbuf bb = BB_NULL;
160 char opbuf[ OPERATION_BUFFER_SIZE ];
161 Operation* op = (Operation *)opbuf;
163 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
164 "testing RDBMS connection\n", 0, 0, 0 );
165 if ( bi->sql_dbname == NULL ) {
166 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
167 "datasource name not specified "
168 "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
172 if ( bi->sql_concat_func == NULL ) {
173 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
174 "concat func not specified (use \"concat_pattern\" "
175 "directive in slapd.conf)\n", 0, 0, 0 );
177 if ( backsql_split_pattern( backsql_def_concat_func,
178 &bi->sql_concat_func, 2 ) ) {
179 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
180 "unable to parse pattern \"%s\"",
181 backsql_def_concat_func, 0, 0 );
187 * Prepare cast string as required
189 if ( bi->sql_upper_func.bv_val ) {
192 if ( BACKSQL_UPPER_NEEDS_CAST( bi ) ) {
193 snprintf( buf, sizeof( buf ),
194 "%s(cast (" /* ? as varchar(%d))) */ ,
195 bi->sql_upper_func.bv_val );
196 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
198 snprintf( buf, sizeof( buf ),
199 /* (cast(? */ " as varchar(%d)))",
200 BACKSQL_MAX_DN_LEN );
201 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_close );
204 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
205 bi->sql_upper_func.bv_val );
206 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
208 ber_str2bv( /* (? */ ")", 0, 1, &bi->sql_upper_func_close );
212 /* normalize filter values only if necessary */
213 bi->sql_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
214 assert( bi->sql_caseIgnoreMatch );
216 bi->sql_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
217 assert( bi->sql_telephoneNumberMatch );
219 if ( bi->sql_dbuser == NULL ) {
220 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
221 "user name not specified "
222 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
226 if ( bi->sql_subtree_cond.bv_val == NULL ) {
228 * Prepare concat function for subtree search condition
230 struct berval concat;
231 struct berval values[] = {
236 struct berbuf bb = BB_NULL;
238 if ( backsql_prepare_pattern( bi->sql_concat_func, values,
240 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
241 "unable to prepare CONCAT pattern", 0, 0, 0 );
245 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
246 "subtree search SQL condition not specified "
247 "(use \"subtree_cond\" directive in slapd.conf)\n",
250 if ( bi->sql_upper_func.bv_val ) {
253 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
256 backsql_strfcat( &bb, "blbbb",
258 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
259 "(ldap_entries.dn) LIKE ",
260 &bi->sql_upper_func_open,
262 &bi->sql_upper_func_close );
267 * ldap_entries.dn LIKE CONCAT('%',?)
270 backsql_strfcat( &bb, "lb",
271 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
272 "ldap_entries.dn LIKE ",
276 bi->sql_subtree_cond = bb.bb_val;
278 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
279 "setting \"%s\" as default\n",
280 bi->sql_subtree_cond.bv_val, 0, 0 );
283 if ( bi->sql_children_cond.bv_val == NULL ) {
284 struct berbuf bb = BB_NULL;
286 if ( bi->sql_upper_func.bv_val ) {
289 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
292 backsql_strfcat( &bb, "blbl",
294 (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
295 "(ldap_entries.dn)=",
297 (ber_len_t)STRLENOF( "(?)" ), "(?)" );
302 * ldap_entries.dn LIKE CONCAT('%,',?)
305 backsql_strfcat( &bb, "l",
306 (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
307 "ldap_entries.dn=?");
310 bi->sql_children_cond = bb.bb_val;
312 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
313 "setting \"%s\" as default\n",
314 bi->sql_children_cond.bv_val, 0, 0 );
317 if ( bi->sql_oc_query == NULL ) {
318 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
320 ch_strdup( backsql_def_needs_select_oc_query );
323 bi->sql_oc_query = ch_strdup( backsql_def_oc_query );
326 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
327 "objectclass mapping SQL statement not specified "
328 "(use \"oc_query\" directive in slapd.conf)\n",
330 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
331 "setting \"%s\" by default\n", bi->sql_oc_query, 0, 0 );
334 if ( bi->sql_at_query == NULL ) {
335 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
336 "attribute mapping SQL statement not specified "
337 "(use \"at_query\" directive in slapd.conf)\n",
339 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
340 "setting \"%s\" by default\n",
341 backsql_def_at_query, 0, 0 );
342 bi->sql_at_query = ch_strdup( backsql_def_at_query );
345 if ( bi->sql_insentry_query == NULL ) {
346 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
347 "entry insertion SQL statement not specified "
348 "(use \"insentry_query\" directive in slapd.conf)\n",
350 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
351 "setting \"%s\" by default\n",
352 backsql_def_insentry_query, 0, 0 );
353 bi->sql_insentry_query = ch_strdup( backsql_def_insentry_query );
356 if ( bi->sql_delentry_query == NULL ) {
357 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
358 "entry deletion SQL statement not specified "
359 "(use \"delentry_query\" directive in slapd.conf)\n",
361 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
362 "setting \"%s\" by default\n",
363 backsql_def_delentry_query, 0, 0 );
364 bi->sql_delentry_query = ch_strdup( backsql_def_delentry_query );
367 if ( bi->sql_delobjclasses_query == NULL ) {
368 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
369 "objclasses deletion SQL statement not specified "
370 "(use \"delobjclasses_query\" directive in slapd.conf)\n",
372 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
373 "setting \"%s\" by default\n",
374 backsql_def_delobjclasses_query, 0, 0 );
375 bi->sql_delobjclasses_query = ch_strdup( backsql_def_delobjclasses_query );
378 if ( bi->sql_delreferrals_query == NULL ) {
379 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
380 "referrals deletion SQL statement not specified "
381 "(use \"delreferrals_query\" directive in slapd.conf)\n",
383 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
384 "setting \"%s\" by default\n",
385 backsql_def_delreferrals_query, 0, 0 );
386 bi->sql_delreferrals_query = ch_strdup( backsql_def_delreferrals_query );
389 op->o_hdr = (Opheader *)&op[ 1 ];
390 op->o_connid = (unsigned long)(-1);
392 if ( backsql_get_db_conn( op, &dbh ) != LDAP_SUCCESS ) {
393 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
394 "connection failed, exiting\n", 0, 0, 0 );
399 * Prepare ID selection query
401 bi->sql_id_query = NULL;
404 if ( bi->sql_upper_func.bv_val == NULL ) {
405 backsql_strcat( &bb, backsql_id_query, "dn=?", NULL );
408 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
409 backsql_strcat( &bb, backsql_id_query,
412 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
413 backsql_strfcat( &bb, "sbl",
416 (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
418 backsql_strfcat( &bb, "sblbcb",
421 (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
422 &bi->sql_upper_func_open,
424 &bi->sql_upper_func_close );
428 bi->sql_id_query = bb.bb_val.bv_val;
431 * Prepare children ID selection query
433 bi->sql_has_children_query = NULL;
435 bb.bb_val.bv_val = NULL;
436 bb.bb_val.bv_len = 0;
438 backsql_strfcat( &bb, "sb",
439 "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries subordinates WHERE subordinates.parent=ldap_entries.id AND ",
441 &bi->sql_children_cond );
442 bi->sql_has_children_query = bb.bb_val.bv_val;
444 backsql_free_db_conn( op );
445 if ( !BACKSQL_SCHEMA_LOADED( bi ) ) {
446 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
447 "test failed, schema map not loaded - exiting\n",
452 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
453 "test succeeded, schema map loaded\n", 0, 0, 0 );
461 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
462 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
467 backsql_connection_destroy( Backend *bd, Connection *c )
469 char opbuf[ OPERATION_BUFFER_SIZE ];
470 Operation* op = (Operation *)opbuf;
472 op->o_hdr = (Opheader *)&op[ 1 ];
473 op->o_connid = c->c_connid;
476 Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
477 backsql_free_db_conn( op );
478 Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
483 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
485 /* conditionally define the init_module() function */
486 SLAP_BACKEND_INIT_MODULE( sql )
488 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */