| 1 |
/* $OpenLDAP$ */ |
| 2 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 3 |
* |
| 4 |
* Copyright 1999-2024 The OpenLDAP Foundation. |
| 5 |
* Portions Copyright 1999 Dmitry Kovalev. |
| 6 |
* Portions Copyright 2002 Pierangelo Masarati. |
| 7 |
* All rights reserved. |
| 8 |
* |
| 9 |
* Redistribution and use in source and binary forms, with or without |
| 10 |
* modification, are permitted only as authorized by the OpenLDAP |
| 11 |
* Public License. |
| 12 |
* |
| 13 |
* A copy of this license is available in the file LICENSE in the |
| 14 |
* top-level directory of the distribution or, alternatively, at |
| 15 |
* <http://www.OpenLDAP.org/license.html>. |
| 16 |
*/ |
| 17 |
/* ACKNOWLEDGEMENTS: |
| 18 |
* This work was initially developed by Dmitry Kovalev for inclusion |
| 19 |
* by OpenLDAP Software. Additional significant contributors include |
| 20 |
* Pierangelo Masarati. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "portable.h" |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <sys/types.h> |
| 27 |
#include "ac/string.h" |
| 28 |
|
| 29 |
#include "slap.h" |
| 30 |
#include "slap-config.h" |
| 31 |
#include "proto-sql.h" |
| 32 |
|
| 33 |
int |
| 34 |
sql_back_initialize( |
| 35 |
BackendInfo *bi ) |
| 36 |
{ |
| 37 |
static char *controls[] = { |
| 38 |
LDAP_CONTROL_ASSERT, |
| 39 |
LDAP_CONTROL_MANAGEDSAIT, |
| 40 |
LDAP_CONTROL_NOOP, |
| 41 |
#ifdef SLAP_CONTROL_X_TREE_DELETE |
| 42 |
SLAP_CONTROL_X_TREE_DELETE, |
| 43 |
#endif /* SLAP_CONTROL_X_TREE_DELETE */ |
| 44 |
#ifndef BACKSQL_ARBITRARY_KEY |
| 45 |
LDAP_CONTROL_PAGEDRESULTS, |
| 46 |
#endif /* ! BACKSQL_ARBITRARY_KEY */ |
| 47 |
NULL |
| 48 |
}; |
| 49 |
int rc; |
| 50 |
|
| 51 |
bi->bi_controls = controls; |
| 52 |
|
| 53 |
bi->bi_flags |= |
| 54 |
#if 0 |
| 55 |
SLAP_BFLAG_INCREMENT | |
| 56 |
#endif |
| 57 |
SLAP_BFLAG_REFERRALS; |
| 58 |
|
| 59 |
Debug( LDAP_DEBUG_TRACE,"==>sql_back_initialize()\n" ); |
| 60 |
|
| 61 |
bi->bi_db_init = backsql_db_init; |
| 62 |
bi->bi_db_config = config_generic_wrapper; |
| 63 |
bi->bi_db_open = backsql_db_open; |
| 64 |
bi->bi_db_close = backsql_db_close; |
| 65 |
bi->bi_db_destroy = backsql_db_destroy; |
| 66 |
|
| 67 |
bi->bi_op_abandon = 0; |
| 68 |
bi->bi_op_compare = backsql_compare; |
| 69 |
bi->bi_op_bind = backsql_bind; |
| 70 |
bi->bi_op_unbind = 0; |
| 71 |
bi->bi_op_search = backsql_search; |
| 72 |
bi->bi_op_modify = backsql_modify; |
| 73 |
bi->bi_op_modrdn = backsql_modrdn; |
| 74 |
bi->bi_op_add = backsql_add; |
| 75 |
bi->bi_op_delete = backsql_delete; |
| 76 |
|
| 77 |
bi->bi_chk_referrals = 0; |
| 78 |
bi->bi_operational = backsql_operational; |
| 79 |
bi->bi_entry_get_rw = backsql_entry_get; |
| 80 |
bi->bi_entry_release_rw = backsql_entry_release; |
| 81 |
|
| 82 |
bi->bi_connection_init = 0; |
| 83 |
|
| 84 |
rc = backsql_init_cf( bi ); |
| 85 |
Debug( LDAP_DEBUG_TRACE,"<==sql_back_initialize()\n" ); |
| 86 |
return rc; |
| 87 |
} |
| 88 |
|
| 89 |
int |
| 90 |
backsql_destroy( |
| 91 |
BackendInfo *bi ) |
| 92 |
{ |
| 93 |
Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n" ); |
| 94 |
Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n" ); |
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
int |
| 99 |
backsql_db_init( |
| 100 |
BackendDB *bd, |
| 101 |
ConfigReply *cr ) |
| 102 |
{ |
| 103 |
backsql_info *bi; |
| 104 |
int rc = 0; |
| 105 |
|
| 106 |
Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n" ); |
| 107 |
|
| 108 |
bi = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) ); |
| 109 |
ldap_pvt_thread_mutex_init( &bi->sql_dbconn_mutex ); |
| 110 |
ldap_pvt_thread_mutex_init( &bi->sql_schema_mutex ); |
| 111 |
|
| 112 |
if ( backsql_init_db_env( bi ) != SQL_SUCCESS ) { |
| 113 |
rc = -1; |
| 114 |
} |
| 115 |
|
| 116 |
bd->be_private = bi; |
| 117 |
bd->be_cf_ocs = bd->bd_info->bi_cf_ocs; |
| 118 |
|
| 119 |
Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n" ); |
| 120 |
|
| 121 |
return rc; |
| 122 |
} |
| 123 |
|
| 124 |
int |
| 125 |
backsql_db_destroy( |
| 126 |
BackendDB *bd, |
| 127 |
ConfigReply *cr ) |
| 128 |
{ |
| 129 |
backsql_info *bi = (backsql_info*)bd->be_private; |
| 130 |
|
| 131 |
Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n" ); |
| 132 |
|
| 133 |
backsql_free_db_env( bi ); |
| 134 |
ldap_pvt_thread_mutex_destroy( &bi->sql_dbconn_mutex ); |
| 135 |
backsql_destroy_schema_map( bi ); |
| 136 |
ldap_pvt_thread_mutex_destroy( &bi->sql_schema_mutex ); |
| 137 |
|
| 138 |
if ( bi->sql_dbname ) { |
| 139 |
ch_free( bi->sql_dbname ); |
| 140 |
} |
| 141 |
if ( bi->sql_dbuser ) { |
| 142 |
ch_free( bi->sql_dbuser ); |
| 143 |
} |
| 144 |
if ( bi->sql_dbpasswd ) { |
| 145 |
ch_free( bi->sql_dbpasswd ); |
| 146 |
} |
| 147 |
if ( bi->sql_dbhost ) { |
| 148 |
ch_free( bi->sql_dbhost ); |
| 149 |
} |
| 150 |
if ( bi->sql_upper_func.bv_val ) { |
| 151 |
ch_free( bi->sql_upper_func.bv_val ); |
| 152 |
ch_free( bi->sql_upper_func_open.bv_val ); |
| 153 |
ch_free( bi->sql_upper_func_close.bv_val ); |
| 154 |
} |
| 155 |
if ( bi->sql_concat_func ) { |
| 156 |
ber_bvarray_free( bi->sql_concat_func ); |
| 157 |
} |
| 158 |
if ( !BER_BVISNULL( &bi->sql_strcast_func ) ) { |
| 159 |
ch_free( bi->sql_strcast_func.bv_val ); |
| 160 |
} |
| 161 |
if ( !BER_BVISNULL( &bi->sql_children_cond ) ) { |
| 162 |
ch_free( bi->sql_children_cond.bv_val ); |
| 163 |
} |
| 164 |
if ( !BER_BVISNULL( &bi->sql_dn_match_cond ) ) { |
| 165 |
ch_free( bi->sql_dn_match_cond.bv_val ); |
| 166 |
} |
| 167 |
if ( !BER_BVISNULL( &bi->sql_subtree_cond ) ) { |
| 168 |
ch_free( bi->sql_subtree_cond.bv_val ); |
| 169 |
} |
| 170 |
if ( !BER_BVISNULL( &bi->sql_dn_oc_aliasing ) ) { |
| 171 |
ch_free( bi->sql_dn_oc_aliasing.bv_val ); |
| 172 |
} |
| 173 |
if ( bi->sql_oc_query ) { |
| 174 |
ch_free( bi->sql_oc_query ); |
| 175 |
} |
| 176 |
if ( bi->sql_at_query ) { |
| 177 |
ch_free( bi->sql_at_query ); |
| 178 |
} |
| 179 |
if ( bi->sql_id_query ) { |
| 180 |
ch_free( bi->sql_id_query ); |
| 181 |
} |
| 182 |
if ( bi->sql_has_children_query ) { |
| 183 |
ch_free( bi->sql_has_children_query ); |
| 184 |
} |
| 185 |
if ( bi->sql_insentry_stmt ) { |
| 186 |
ch_free( bi->sql_insentry_stmt ); |
| 187 |
} |
| 188 |
if ( bi->sql_delentry_stmt ) { |
| 189 |
ch_free( bi->sql_delentry_stmt ); |
| 190 |
} |
| 191 |
if ( bi->sql_renentry_stmt ) { |
| 192 |
ch_free( bi->sql_renentry_stmt ); |
| 193 |
} |
| 194 |
if ( bi->sql_delobjclasses_stmt ) { |
| 195 |
ch_free( bi->sql_delobjclasses_stmt ); |
| 196 |
} |
| 197 |
if ( !BER_BVISNULL( &bi->sql_aliasing ) ) { |
| 198 |
ch_free( bi->sql_aliasing.bv_val ); |
| 199 |
} |
| 200 |
if ( !BER_BVISNULL( &bi->sql_aliasing_quote ) ) { |
| 201 |
ch_free( bi->sql_aliasing_quote.bv_val ); |
| 202 |
} |
| 203 |
|
| 204 |
if ( bi->sql_anlist ) { |
| 205 |
int i; |
| 206 |
|
| 207 |
for ( i = 0; !BER_BVISNULL( &bi->sql_anlist[ i ].an_name ); i++ ) |
| 208 |
{ |
| 209 |
ch_free( bi->sql_anlist[ i ].an_name.bv_val ); |
| 210 |
} |
| 211 |
ch_free( bi->sql_anlist ); |
| 212 |
} |
| 213 |
|
| 214 |
if ( bi->sql_baseObject ) { |
| 215 |
entry_free( bi->sql_baseObject ); |
| 216 |
} |
| 217 |
|
| 218 |
ch_free( bi ); |
| 219 |
|
| 220 |
Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n" ); |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
int |
| 225 |
backsql_db_open( |
| 226 |
BackendDB *bd, |
| 227 |
ConfigReply *cr ) |
| 228 |
{ |
| 229 |
backsql_info *bi = (backsql_info*)bd->be_private; |
| 230 |
struct berbuf bb = BB_NULL; |
| 231 |
|
| 232 |
Connection conn = { 0 }; |
| 233 |
OperationBuffer opbuf; |
| 234 |
Operation* op; |
| 235 |
SQLHDBC dbh = SQL_NULL_HDBC; |
| 236 |
void *thrctx = ldap_pvt_thread_pool_context(); |
| 237 |
|
| 238 |
Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): " |
| 239 |
"testing RDBMS connection\n" ); |
| 240 |
if ( bi->sql_dbname == NULL ) { |
| 241 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 242 |
"datasource name not specified " |
| 243 |
"(use \"dbname\" directive in slapd.conf)\n" ); |
| 244 |
return 1; |
| 245 |
} |
| 246 |
|
| 247 |
if ( bi->sql_concat_func == NULL ) { |
| 248 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 249 |
"concat func not specified (use \"concat_pattern\" " |
| 250 |
"directive in slapd.conf)\n" ); |
| 251 |
|
| 252 |
if ( backsql_split_pattern( backsql_def_concat_func, |
| 253 |
&bi->sql_concat_func, 2 ) ) { |
| 254 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 255 |
"unable to parse pattern \"%s\"", |
| 256 |
backsql_def_concat_func ); |
| 257 |
return 1; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
/* |
| 262 |
* see back-sql.h for default values |
| 263 |
*/ |
| 264 |
if ( BER_BVISNULL( &bi->sql_aliasing ) ) { |
| 265 |
ber_str2bv( BACKSQL_ALIASING, |
| 266 |
STRLENOF( BACKSQL_ALIASING ), |
| 267 |
1, &bi->sql_aliasing ); |
| 268 |
} |
| 269 |
|
| 270 |
if ( BER_BVISNULL( &bi->sql_aliasing_quote ) ) { |
| 271 |
ber_str2bv( BACKSQL_ALIASING_QUOTE, |
| 272 |
STRLENOF( BACKSQL_ALIASING_QUOTE ), |
| 273 |
1, &bi->sql_aliasing_quote ); |
| 274 |
} |
| 275 |
|
| 276 |
/* |
| 277 |
* Prepare cast string as required |
| 278 |
*/ |
| 279 |
if ( bi->sql_upper_func.bv_val ) { |
| 280 |
char buf[1024]; |
| 281 |
|
| 282 |
if ( BACKSQL_UPPER_NEEDS_CAST( bi ) ) { |
| 283 |
snprintf( buf, sizeof( buf ), |
| 284 |
"%s(cast (" /* ? as varchar(%d))) */ , |
| 285 |
bi->sql_upper_func.bv_val ); |
| 286 |
ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open ); |
| 287 |
|
| 288 |
snprintf( buf, sizeof( buf ), |
| 289 |
/* (cast(? */ " as varchar(%d)))", |
| 290 |
BACKSQL_MAX_DN_LEN ); |
| 291 |
ber_str2bv( buf, 0, 1, &bi->sql_upper_func_close ); |
| 292 |
|
| 293 |
} else { |
| 294 |
snprintf( buf, sizeof( buf ), "%s(" /* ?) */ , |
| 295 |
bi->sql_upper_func.bv_val ); |
| 296 |
ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open ); |
| 297 |
|
| 298 |
ber_str2bv( /* (? */ ")", 0, 1, &bi->sql_upper_func_close ); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
/* normalize filter values only if necessary */ |
| 303 |
bi->sql_caseIgnoreMatch = mr_find( "caseIgnoreMatch" ); |
| 304 |
assert( bi->sql_caseIgnoreMatch != NULL ); |
| 305 |
|
| 306 |
bi->sql_telephoneNumberMatch = mr_find( "telephoneNumberMatch" ); |
| 307 |
assert( bi->sql_telephoneNumberMatch != NULL ); |
| 308 |
|
| 309 |
if ( bi->sql_dbuser == NULL ) { |
| 310 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 311 |
"user name not specified " |
| 312 |
"(use \"dbuser\" directive in slapd.conf)\n" ); |
| 313 |
return 1; |
| 314 |
} |
| 315 |
|
| 316 |
if ( BER_BVISNULL( &bi->sql_subtree_cond ) ) { |
| 317 |
/* |
| 318 |
* Prepare concat function for subtree search condition |
| 319 |
*/ |
| 320 |
struct berval concat; |
| 321 |
struct berval values[] = { |
| 322 |
BER_BVC( "'%'" ), |
| 323 |
BER_BVC( "?" ), |
| 324 |
BER_BVNULL |
| 325 |
}; |
| 326 |
struct berbuf bb = BB_NULL; |
| 327 |
|
| 328 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 329 |
"subtree search SQL condition not specified " |
| 330 |
"(use \"subtree_cond\" directive in slapd.conf); " |
| 331 |
"preparing default\n" ); |
| 332 |
|
| 333 |
if ( backsql_prepare_pattern( bi->sql_concat_func, values, |
| 334 |
&concat ) ) { |
| 335 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 336 |
"unable to prepare CONCAT pattern for subtree search" ); |
| 337 |
return 1; |
| 338 |
} |
| 339 |
|
| 340 |
if ( bi->sql_upper_func.bv_val ) { |
| 341 |
|
| 342 |
/* |
| 343 |
* UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?)) |
| 344 |
*/ |
| 345 |
|
| 346 |
backsql_strfcat_x( &bb, NULL, "blbbb", |
| 347 |
&bi->sql_upper_func, |
| 348 |
(ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ), |
| 349 |
"(ldap_entries.dn) LIKE ", |
| 350 |
&bi->sql_upper_func_open, |
| 351 |
&concat, |
| 352 |
&bi->sql_upper_func_close ); |
| 353 |
|
| 354 |
} else { |
| 355 |
|
| 356 |
/* |
| 357 |
* ldap_entries.dn LIKE CONCAT('%',?) |
| 358 |
*/ |
| 359 |
|
| 360 |
backsql_strfcat_x( &bb, NULL, "lb", |
| 361 |
(ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ), |
| 362 |
"ldap_entries.dn LIKE ", |
| 363 |
&concat ); |
| 364 |
} |
| 365 |
|
| 366 |
ch_free( concat.bv_val ); |
| 367 |
|
| 368 |
bi->sql_subtree_cond = bb.bb_val; |
| 369 |
|
| 370 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 371 |
"setting \"%s\" as default \"subtree_cond\"\n", |
| 372 |
bi->sql_subtree_cond.bv_val ); |
| 373 |
} |
| 374 |
|
| 375 |
if ( bi->sql_children_cond.bv_val == NULL ) { |
| 376 |
/* |
| 377 |
* Prepare concat function for children search condition |
| 378 |
*/ |
| 379 |
struct berval concat; |
| 380 |
struct berval values[] = { |
| 381 |
BER_BVC( "'%,'" ), |
| 382 |
BER_BVC( "?" ), |
| 383 |
BER_BVNULL |
| 384 |
}; |
| 385 |
struct berbuf bb = BB_NULL; |
| 386 |
|
| 387 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 388 |
"children search SQL condition not specified " |
| 389 |
"(use \"children_cond\" directive in slapd.conf); " |
| 390 |
"preparing default\n" ); |
| 391 |
|
| 392 |
if ( backsql_prepare_pattern( bi->sql_concat_func, values, |
| 393 |
&concat ) ) { |
| 394 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 395 |
"unable to prepare CONCAT pattern for children search" ); |
| 396 |
return 1; |
| 397 |
} |
| 398 |
|
| 399 |
if ( bi->sql_upper_func.bv_val ) { |
| 400 |
|
| 401 |
/* |
| 402 |
* UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?)) |
| 403 |
*/ |
| 404 |
|
| 405 |
backsql_strfcat_x( &bb, NULL, "blbbb", |
| 406 |
&bi->sql_upper_func, |
| 407 |
(ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ), |
| 408 |
"(ldap_entries.dn) LIKE ", |
| 409 |
&bi->sql_upper_func_open, |
| 410 |
&concat, |
| 411 |
&bi->sql_upper_func_close ); |
| 412 |
|
| 413 |
} else { |
| 414 |
|
| 415 |
/* |
| 416 |
* ldap_entries.dn LIKE CONCAT('%,',?) |
| 417 |
*/ |
| 418 |
|
| 419 |
backsql_strfcat_x( &bb, NULL, "lb", |
| 420 |
(ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ), |
| 421 |
"ldap_entries.dn LIKE ", |
| 422 |
&concat ); |
| 423 |
} |
| 424 |
|
| 425 |
ch_free( concat.bv_val ); |
| 426 |
|
| 427 |
bi->sql_children_cond = bb.bb_val; |
| 428 |
|
| 429 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 430 |
"setting \"%s\" as default \"children_cond\"\n", |
| 431 |
bi->sql_children_cond.bv_val ); |
| 432 |
} |
| 433 |
|
| 434 |
if ( bi->sql_dn_match_cond.bv_val == NULL ) { |
| 435 |
/* |
| 436 |
* Prepare concat function for dn match search condition |
| 437 |
*/ |
| 438 |
struct berbuf bb = BB_NULL; |
| 439 |
|
| 440 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 441 |
"DN match search SQL condition not specified " |
| 442 |
"(use \"dn_match_cond\" directive in slapd.conf); " |
| 443 |
"preparing default\n" ); |
| 444 |
|
| 445 |
if ( bi->sql_upper_func.bv_val ) { |
| 446 |
|
| 447 |
/* |
| 448 |
* UPPER(ldap_entries.dn)=? |
| 449 |
*/ |
| 450 |
|
| 451 |
backsql_strfcat_x( &bb, NULL, "blbcb", |
| 452 |
&bi->sql_upper_func, |
| 453 |
(ber_len_t)STRLENOF( "(ldap_entries.dn)=" ), |
| 454 |
"(ldap_entries.dn)=", |
| 455 |
&bi->sql_upper_func_open, |
| 456 |
'?', |
| 457 |
&bi->sql_upper_func_close ); |
| 458 |
|
| 459 |
} else { |
| 460 |
|
| 461 |
/* |
| 462 |
* ldap_entries.dn=? |
| 463 |
*/ |
| 464 |
|
| 465 |
backsql_strfcat_x( &bb, NULL, "l", |
| 466 |
(ber_len_t)STRLENOF( "ldap_entries.dn=?" ), |
| 467 |
"ldap_entries.dn=?" ); |
| 468 |
} |
| 469 |
|
| 470 |
bi->sql_dn_match_cond = bb.bb_val; |
| 471 |
|
| 472 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 473 |
"setting \"%s\" as default \"dn_match_cond\"\n", |
| 474 |
bi->sql_dn_match_cond.bv_val ); |
| 475 |
} |
| 476 |
|
| 477 |
if ( bi->sql_oc_query == NULL ) { |
| 478 |
if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) { |
| 479 |
bi->sql_oc_query = |
| 480 |
ch_strdup( backsql_def_needs_select_oc_query ); |
| 481 |
|
| 482 |
} else { |
| 483 |
bi->sql_oc_query = ch_strdup( backsql_def_oc_query ); |
| 484 |
} |
| 485 |
|
| 486 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 487 |
"objectclass mapping SQL statement not specified " |
| 488 |
"(use \"oc_query\" directive in slapd.conf)\n" ); |
| 489 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 490 |
"setting \"%s\" by default\n", bi->sql_oc_query ); |
| 491 |
} |
| 492 |
|
| 493 |
if ( bi->sql_at_query == NULL ) { |
| 494 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 495 |
"attribute mapping SQL statement not specified " |
| 496 |
"(use \"at_query\" directive in slapd.conf)\n" ); |
| 497 |
Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 498 |
"setting \"%s\" by default\n", |
| 499 |
backsql_def_at_query ); |
| 500 |
bi->sql_at_query = ch_strdup( backsql_def_at_query ); |
| 501 |
} |
| 502 |
|
| 503 |
if ( bi->sql_insentry_stmt == NULL ) { |
| 504 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 505 |
"entry insertion SQL statement not specified " |
| 506 |
"(use \"insentry_stmt\" directive in slapd.conf)\n" ); |
| 507 |
Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 508 |
"setting \"%s\" by default\n", |
| 509 |
backsql_def_insentry_stmt ); |
| 510 |
bi->sql_insentry_stmt = ch_strdup( backsql_def_insentry_stmt ); |
| 511 |
} |
| 512 |
|
| 513 |
if ( bi->sql_delentry_stmt == NULL ) { |
| 514 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 515 |
"entry deletion SQL statement not specified " |
| 516 |
"(use \"delentry_stmt\" directive in slapd.conf)\n" ); |
| 517 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 518 |
"setting \"%s\" by default\n", |
| 519 |
backsql_def_delentry_stmt ); |
| 520 |
bi->sql_delentry_stmt = ch_strdup( backsql_def_delentry_stmt ); |
| 521 |
} |
| 522 |
|
| 523 |
if ( bi->sql_renentry_stmt == NULL ) { |
| 524 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 525 |
"entry deletion SQL statement not specified " |
| 526 |
"(use \"renentry_stmt\" directive in slapd.conf)\n" ); |
| 527 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 528 |
"setting \"%s\" by default\n", |
| 529 |
backsql_def_renentry_stmt ); |
| 530 |
bi->sql_renentry_stmt = ch_strdup( backsql_def_renentry_stmt ); |
| 531 |
} |
| 532 |
|
| 533 |
if ( bi->sql_delobjclasses_stmt == NULL ) { |
| 534 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 535 |
"objclasses deletion SQL statement not specified " |
| 536 |
"(use \"delobjclasses_stmt\" directive in slapd.conf)\n" ); |
| 537 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 538 |
"setting \"%s\" by default\n", |
| 539 |
backsql_def_delobjclasses_stmt ); |
| 540 |
bi->sql_delobjclasses_stmt = ch_strdup( backsql_def_delobjclasses_stmt ); |
| 541 |
} |
| 542 |
|
| 543 |
/* This should just be to force schema loading */ |
| 544 |
connection_fake_init2( &conn, &opbuf, thrctx, 0 ); |
| 545 |
op = &opbuf.ob_op; |
| 546 |
op->o_bd = bd; |
| 547 |
if ( backsql_get_db_conn( op, &dbh ) != LDAP_SUCCESS ) { |
| 548 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 549 |
"connection failed, exiting\n" ); |
| 550 |
return 1; |
| 551 |
} |
| 552 |
if ( backsql_load_schema_map( bi, dbh ) != LDAP_SUCCESS ) { |
| 553 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 554 |
"schema mapping failed, exiting\n" ); |
| 555 |
return 1; |
| 556 |
} |
| 557 |
if ( backsql_free_db_conn( op, dbh ) != SQL_SUCCESS ) { |
| 558 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 559 |
"connection free failed\n" ); |
| 560 |
} |
| 561 |
if ( !BACKSQL_SCHEMA_LOADED( bi ) ) { |
| 562 |
Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): " |
| 563 |
"test failed, schema map not loaded - exiting\n" ); |
| 564 |
return 1; |
| 565 |
} |
| 566 |
|
| 567 |
/* |
| 568 |
* Prepare ID selection query |
| 569 |
*/ |
| 570 |
if ( bi->sql_id_query == NULL ) { |
| 571 |
/* no custom id_query provided */ |
| 572 |
if ( bi->sql_upper_func.bv_val == NULL ) { |
| 573 |
backsql_strcat_x( &bb, NULL, backsql_id_query, "dn=?", NULL ); |
| 574 |
|
| 575 |
} else { |
| 576 |
if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) { |
| 577 |
backsql_strcat_x( &bb, NULL, backsql_id_query, |
| 578 |
"dn_ru=?", NULL ); |
| 579 |
} else { |
| 580 |
if ( BACKSQL_USE_REVERSE_DN( bi ) ) { |
| 581 |
backsql_strfcat_x( &bb, NULL, "sbl", |
| 582 |
backsql_id_query, |
| 583 |
&bi->sql_upper_func, |
| 584 |
(ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" ); |
| 585 |
} else { |
| 586 |
backsql_strfcat_x( &bb, NULL, "sblbcb", |
| 587 |
backsql_id_query, |
| 588 |
&bi->sql_upper_func, |
| 589 |
(ber_len_t)STRLENOF( "(dn)=" ), "(dn)=", |
| 590 |
&bi->sql_upper_func_open, |
| 591 |
'?', |
| 592 |
&bi->sql_upper_func_close ); |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
bi->sql_id_query = bb.bb_val.bv_val; |
| 597 |
} |
| 598 |
|
| 599 |
/* |
| 600 |
* Prepare children count query |
| 601 |
*/ |
| 602 |
BER_BVZERO( &bb.bb_val ); |
| 603 |
bb.bb_len = 0; |
| 604 |
backsql_strfcat_x( &bb, NULL, "sbsb", |
| 605 |
"SELECT COUNT(distinct subordinates.id) " |
| 606 |
"FROM ldap_entries,ldap_entries ", |
| 607 |
&bi->sql_aliasing, "subordinates " |
| 608 |
"WHERE subordinates.parent=ldap_entries.id AND ", |
| 609 |
&bi->sql_dn_match_cond ); |
| 610 |
bi->sql_has_children_query = bb.bb_val.bv_val; |
| 611 |
|
| 612 |
/* |
| 613 |
* Prepare DN and objectClass aliasing bit of query |
| 614 |
*/ |
| 615 |
BER_BVZERO( &bb.bb_val ); |
| 616 |
bb.bb_len = 0; |
| 617 |
backsql_strfcat_x( &bb, NULL, "sbbsbsbbsb", |
| 618 |
" ", &bi->sql_aliasing, &bi->sql_aliasing_quote, |
| 619 |
"objectClass", &bi->sql_aliasing_quote, |
| 620 |
",ldap_entries.dn ", &bi->sql_aliasing, |
| 621 |
&bi->sql_aliasing_quote, "dn", &bi->sql_aliasing_quote ); |
| 622 |
bi->sql_dn_oc_aliasing = bb.bb_val; |
| 623 |
|
| 624 |
/* should never happen! */ |
| 625 |
assert( bd->be_nsuffix != NULL ); |
| 626 |
|
| 627 |
if ( BER_BVISNULL( &bd->be_nsuffix[ 1 ] ) ) { |
| 628 |
/* enable if only one suffix is defined */ |
| 629 |
bi->sql_flags |= BSQLF_USE_SUBTREE_SHORTCUT; |
| 630 |
} |
| 631 |
|
| 632 |
bi->sql_flags |= BSQLF_CHECK_SCHEMA; |
| 633 |
|
| 634 |
Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): " |
| 635 |
"test succeeded, schema map loaded\n" ); |
| 636 |
return 0; |
| 637 |
} |
| 638 |
|
| 639 |
int |
| 640 |
backsql_db_close( |
| 641 |
BackendDB *bd, |
| 642 |
ConfigReply *cr ) |
| 643 |
{ |
| 644 |
backsql_info *bi = (backsql_info*)bd->be_private; |
| 645 |
|
| 646 |
Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n" ); |
| 647 |
|
| 648 |
backsql_conn_destroy( bi ); |
| 649 |
|
| 650 |
Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n" ); |
| 651 |
|
| 652 |
return 0; |
| 653 |
} |
| 654 |
|
| 655 |
#if SLAPD_SQL == SLAPD_MOD_DYNAMIC |
| 656 |
|
| 657 |
/* conditionally define the init_module() function */ |
| 658 |
SLAP_BACKEND_INIT_MODULE( sql ) |
| 659 |
|
| 660 |
#endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */ |
| 661 |
|