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;
156 SQLHDBC dbh = SQL_NULL_HDBC;
157 struct berbuf bb = BB_NULL;
159 char opbuf[ OPERATION_BUFFER_SIZE ];
160 Operation* op = (Operation *)opbuf;
162 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
163 "testing RDBMS connection\n", 0, 0, 0 );
164 if ( bi->sql_dbname == NULL ) {
165 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
166 "datasource name not specified "
167 "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
171 if ( bi->sql_concat_func == NULL ) {
172 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
173 "concat func not specified (use \"concat_pattern\" "
174 "directive in slapd.conf)\n", 0, 0, 0 );
176 if ( backsql_split_pattern( backsql_def_concat_func,
177 &bi->sql_concat_func, 2 ) ) {
178 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
179 "unable to parse pattern \"%s\"",
180 backsql_def_concat_func, 0, 0 );
186 * Prepare cast string as required
188 if ( bi->sql_upper_func.bv_val ) {
191 if ( BACKSQL_UPPER_NEEDS_CAST( bi ) ) {
192 snprintf( buf, sizeof( buf ),
193 "%s(cast (" /* ? as varchar(%d))) */ ,
194 bi->sql_upper_func.bv_val );
195 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
197 snprintf( buf, sizeof( buf ),
198 /* (cast(? */ " as varchar(%d)))",
199 BACKSQL_MAX_DN_LEN );
200 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_close );
203 snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
204 bi->sql_upper_func.bv_val );
205 ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
207 ber_str2bv( /* (? */ ")", 0, 1, &bi->sql_upper_func_close );
211 /* normalize filter values only if necessary */
212 bi->sql_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
213 assert( bi->sql_caseIgnoreMatch );
215 bi->sql_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
216 assert( bi->sql_telephoneNumberMatch );
218 if ( bi->sql_dbuser == NULL ) {
219 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
220 "user name not specified "
221 "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
225 if ( bi->sql_subtree_cond.bv_val == NULL ) {
227 * Prepare concat function for subtree search condition
229 struct berval concat;
230 struct berval values[] = {
235 struct berbuf bb = BB_NULL;
237 if ( backsql_prepare_pattern( bi->sql_concat_func, values,
239 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
240 "unable to prepare CONCAT pattern", 0, 0, 0 );
244 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
245 "subtree search SQL condition not specified "
246 "(use \"subtree_cond\" directive in slapd.conf)\n",
249 if ( bi->sql_upper_func.bv_val ) {
252 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
255 backsql_strfcat( &bb, "blbbb",
257 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
258 "(ldap_entries.dn) LIKE ",
259 &bi->sql_upper_func_open,
261 &bi->sql_upper_func_close );
266 * ldap_entries.dn LIKE CONCAT('%',?)
269 backsql_strfcat( &bb, "lb",
270 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
271 "ldap_entries.dn LIKE ",
275 bi->sql_subtree_cond = bb.bb_val;
277 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
278 "setting \"%s\" as default\n",
279 bi->sql_subtree_cond.bv_val, 0, 0 );
282 if ( bi->sql_children_cond.bv_val == NULL ) {
283 struct berbuf bb = BB_NULL;
285 if ( bi->sql_upper_func.bv_val ) {
288 * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
291 backsql_strfcat( &bb, "blbl",
293 (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
294 "(ldap_entries.dn)=",
296 (ber_len_t)STRLENOF( "(?)" ), "(?)" );
301 * ldap_entries.dn LIKE CONCAT('%,',?)
304 backsql_strfcat( &bb, "l",
305 (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
306 "ldap_entries.dn=?");
309 bi->sql_children_cond = bb.bb_val;
311 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
312 "setting \"%s\" as default\n",
313 bi->sql_children_cond.bv_val, 0, 0 );
316 if ( bi->sql_oc_query == NULL ) {
317 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
319 ch_strdup( backsql_def_needs_select_oc_query );
322 bi->sql_oc_query = ch_strdup( backsql_def_oc_query );
325 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
326 "objectclass mapping SQL statement not specified "
327 "(use \"oc_query\" directive in slapd.conf)\n",
329 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
330 "setting \"%s\" by default\n", bi->sql_oc_query, 0, 0 );
333 if ( bi->sql_at_query == NULL ) {
334 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
335 "attribute mapping SQL statement not specified "
336 "(use \"at_query\" directive in slapd.conf)\n",
338 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
339 "setting \"%s\" by default\n",
340 backsql_def_at_query, 0, 0 );
341 bi->sql_at_query = ch_strdup( backsql_def_at_query );
344 if ( bi->sql_insentry_query == NULL ) {
345 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
346 "entry insertion SQL statement not specified "
347 "(use \"insentry_query\" directive in slapd.conf)\n",
349 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
350 "setting \"%s\" by default\n",
351 backsql_def_insentry_query, 0, 0 );
352 bi->sql_insentry_query = ch_strdup( backsql_def_insentry_query );
355 if ( bi->sql_delentry_query == NULL ) {
356 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
357 "entry deletion SQL statement not specified "
358 "(use \"delentry_query\" directive in slapd.conf)\n",
360 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
361 "setting \"%s\" by default\n",
362 backsql_def_delentry_query, 0, 0 );
363 bi->sql_delentry_query = ch_strdup( backsql_def_delentry_query );
366 if ( bi->sql_delobjclasses_query == NULL ) {
367 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
368 "objclasses deletion SQL statement not specified "
369 "(use \"delobjclasses_query\" directive in slapd.conf)\n",
371 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
372 "setting \"%s\" by default\n",
373 backsql_def_delobjclasses_query, 0, 0 );
374 bi->sql_delobjclasses_query = ch_strdup( backsql_def_delobjclasses_query );
377 if ( bi->sql_delreferrals_query == NULL ) {
378 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
379 "referrals deletion SQL statement not specified "
380 "(use \"delreferrals_query\" directive in slapd.conf)\n",
382 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
383 "setting \"%s\" by default\n",
384 backsql_def_delreferrals_query, 0, 0 );
385 bi->sql_delreferrals_query = ch_strdup( backsql_def_delreferrals_query );
388 op->o_hdr = (Opheader *)&op[ 1 ];
389 op->o_connid = (unsigned long)(-1);
391 if ( backsql_get_db_conn( op, &dbh ) != LDAP_SUCCESS ) {
392 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
393 "connection failed, exiting\n", 0, 0, 0 );
398 * Prepare ID selection query
400 if ( bi->sql_id_query == NULL ) {
401 /* no custom id_query provided */
402 if ( bi->sql_upper_func.bv_val == NULL ) {
403 backsql_strcat( &bb, backsql_id_query, "dn=?", NULL );
406 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
407 backsql_strcat( &bb, backsql_id_query,
410 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
411 backsql_strfcat( &bb, "sbl",
414 (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
416 backsql_strfcat( &bb, "sblbcb",
419 (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
420 &bi->sql_upper_func_open,
422 &bi->sql_upper_func_close );
426 bi->sql_id_query = bb.bb_val.bv_val;
430 * Prepare children ID selection query
432 bi->sql_has_children_query = NULL;
434 bb.bb_val.bv_val = NULL;
435 bb.bb_val.bv_len = 0;
437 backsql_strfcat( &bb, "sb",
438 "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries subordinates WHERE subordinates.parent=ldap_entries.id AND ",
440 &bi->sql_children_cond );
441 bi->sql_has_children_query = bb.bb_val.bv_val;
443 backsql_free_db_conn( op );
444 if ( !BACKSQL_SCHEMA_LOADED( bi ) ) {
445 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
446 "test failed, schema map not loaded - exiting\n",
451 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
452 "test succeeded, schema map loaded\n", 0, 0, 0 );
460 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
461 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
466 backsql_connection_destroy( Backend *bd, Connection *c )
468 char opbuf[ OPERATION_BUFFER_SIZE ];
469 Operation* op = (Operation *)opbuf;
471 op->o_hdr = (Opheader *)&op[ 1 ];
472 op->o_connid = c->c_connid;
475 Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
476 backsql_free_db_conn( op );
477 Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
482 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
484 /* conditionally define the init_module() function */
485 SLAP_BACKEND_INIT_MODULE( sql )
487 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */