| 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 |
* Portions Copyright 2004 Mark Adamson. |
| 8 |
* All rights reserved. |
| 9 |
* |
| 10 |
* Redistribution and use in source and binary forms, with or without |
| 11 |
* modification, are permitted only as authorized by the OpenLDAP |
| 12 |
* Public License. |
| 13 |
* |
| 14 |
* A copy of this license is available in the file LICENSE in the |
| 15 |
* top-level directory of the distribution or, alternatively, at |
| 16 |
* <http://www.OpenLDAP.org/license.html>. |
| 17 |
*/ |
| 18 |
/* ACKNOWLEDGEMENTS: |
| 19 |
* This work was initially developed by Dmitry Kovalev for inclusion |
| 20 |
* by OpenLDAP Software. Additional significant contributors include |
| 21 |
* Pierangelo Masarati and Mark Adamson. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "portable.h" |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <sys/types.h> |
| 28 |
#include "ac/string.h" |
| 29 |
#include "ac/ctype.h" |
| 30 |
|
| 31 |
#include "lutil.h" |
| 32 |
#include "slap.h" |
| 33 |
#include "proto-sql.h" |
| 34 |
|
| 35 |
static int backsql_process_filter( backsql_srch_info *bsi, Filter *f ); |
| 36 |
static int backsql_process_filter_eq( backsql_srch_info *bsi, |
| 37 |
backsql_at_map_rec *at, |
| 38 |
int casefold, struct berval *filter_value ); |
| 39 |
static int backsql_process_filter_like( backsql_srch_info *bsi, |
| 40 |
backsql_at_map_rec *at, |
| 41 |
int casefold, struct berval *filter_value ); |
| 42 |
static int backsql_process_filter_attr( backsql_srch_info *bsi, Filter *f, |
| 43 |
backsql_at_map_rec *at ); |
| 44 |
|
| 45 |
/* For LDAP_CONTROL_PAGEDRESULTS, a 32 bit cookie is available to keep track of |
| 46 |
the state of paged results. The ldap_entries.id and oc_map_id values of the |
| 47 |
last entry returned are used as the cookie, so 6 bits are used for the OC id |
| 48 |
and the other 26 for ldap_entries ID number. If your max(oc_map_id) is more |
| 49 |
than 63, you will need to steal more bits from ldap_entries ID number and |
| 50 |
put them into the OC ID part of the cookie. */ |
| 51 |
|
| 52 |
/* NOTE: not supported when BACKSQL_ARBITRARY_KEY is defined */ |
| 53 |
#ifndef BACKSQL_ARBITRARY_KEY |
| 54 |
#define SQL_TO_PAGECOOKIE(id, oc) (((id) << 6 ) | ((oc) & 0x3F)) |
| 55 |
#define PAGECOOKIE_TO_SQL_ID(pc) ((pc) >> 6) |
| 56 |
#define PAGECOOKIE_TO_SQL_OC(pc) ((pc) & 0x3F) |
| 57 |
|
| 58 |
static int parse_paged_cookie( Operation *op, SlapReply *rs ); |
| 59 |
|
| 60 |
static void send_paged_response( |
| 61 |
Operation *op, |
| 62 |
SlapReply *rs, |
| 63 |
ID *lastid ); |
| 64 |
#endif /* ! BACKSQL_ARBITRARY_KEY */ |
| 65 |
|
| 66 |
/* Look for chars that need to be escaped, return count of them. |
| 67 |
* If out is non-NULL, copy escape'd val to it. |
| 68 |
*/ |
| 69 |
static int |
| 70 |
backsql_val_escape( Operation *op, struct berval *in, struct berval *out ) |
| 71 |
{ |
| 72 |
char *ptr, *end; |
| 73 |
int q = 0; |
| 74 |
|
| 75 |
ptr = in->bv_val; |
| 76 |
end = ptr + in->bv_len; |
| 77 |
while (ptr < end) { |
| 78 |
if ( *ptr == '\'' ) |
| 79 |
q++; |
| 80 |
ptr++; |
| 81 |
} |
| 82 |
if ( q && out ) { |
| 83 |
char *dst; |
| 84 |
out->bv_len = in->bv_len + q; |
| 85 |
out->bv_val = op->o_tmpalloc( out->bv_len + 1, op->o_tmpmemctx ); |
| 86 |
ptr = in->bv_val; |
| 87 |
dst = out->bv_val; |
| 88 |
while (ptr < end ) { |
| 89 |
if ( *ptr == '\'' ) |
| 90 |
*dst++ = '\''; |
| 91 |
*dst++ = *ptr++; |
| 92 |
} |
| 93 |
*dst = '\0'; |
| 94 |
} |
| 95 |
return q; |
| 96 |
} |
| 97 |
|
| 98 |
static int |
| 99 |
backsql_attrlist_add( backsql_srch_info *bsi, AttributeDescription *ad ) |
| 100 |
{ |
| 101 |
int n_attrs = 0; |
| 102 |
AttributeName *an = NULL; |
| 103 |
|
| 104 |
if ( bsi->bsi_attrs == NULL ) { |
| 105 |
return 1; |
| 106 |
} |
| 107 |
|
| 108 |
/* |
| 109 |
* clear the list (retrieve all attrs) |
| 110 |
*/ |
| 111 |
if ( ad == NULL ) { |
| 112 |
bsi->bsi_op->o_tmpfree( bsi->bsi_attrs, bsi->bsi_op->o_tmpmemctx ); |
| 113 |
bsi->bsi_attrs = NULL; |
| 114 |
bsi->bsi_flags |= BSQL_SF_ALL_ATTRS; |
| 115 |
return 1; |
| 116 |
} |
| 117 |
|
| 118 |
/* strip ';binary' */ |
| 119 |
if ( slap_ad_is_binary( ad ) ) { |
| 120 |
ad = ad->ad_type->sat_ad; |
| 121 |
} |
| 122 |
|
| 123 |
for ( ; !BER_BVISNULL( &bsi->bsi_attrs[ n_attrs ].an_name ); n_attrs++ ) { |
| 124 |
an = &bsi->bsi_attrs[ n_attrs ]; |
| 125 |
|
| 126 |
Debug( LDAP_DEBUG_TRACE, "==>backsql_attrlist_add(): " |
| 127 |
"attribute \"%s\" is in list\n", |
| 128 |
an->an_name.bv_val ); |
| 129 |
/* |
| 130 |
* We can live with strcmp because the attribute |
| 131 |
* list has been normalized before calling be_search |
| 132 |
*/ |
| 133 |
if ( !BACKSQL_NCMP( &an->an_name, &ad->ad_cname ) ) { |
| 134 |
return 1; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
Debug( LDAP_DEBUG_TRACE, "==>backsql_attrlist_add(): " |
| 139 |
"adding \"%s\" to list\n", ad->ad_cname.bv_val ); |
| 140 |
|
| 141 |
an = (AttributeName *)bsi->bsi_op->o_tmprealloc( bsi->bsi_attrs, |
| 142 |
sizeof( AttributeName ) * ( n_attrs + 2 ), |
| 143 |
bsi->bsi_op->o_tmpmemctx ); |
| 144 |
if ( an == NULL ) { |
| 145 |
return -1; |
| 146 |
} |
| 147 |
|
| 148 |
an[ n_attrs ].an_name = ad->ad_cname; |
| 149 |
an[ n_attrs ].an_desc = ad; |
| 150 |
BER_BVZERO( &an[ n_attrs + 1 ].an_name ); |
| 151 |
|
| 152 |
bsi->bsi_attrs = an; |
| 153 |
|
| 154 |
return 1; |
| 155 |
} |
| 156 |
|
| 157 |
/* |
| 158 |
* Initializes the search structure. |
| 159 |
* |
| 160 |
* If get_base_id != 0, the field bsi_base_id is filled |
| 161 |
* with the entryID of bsi_base_ndn; it must be freed |
| 162 |
* by backsql_free_entryID() when no longer required. |
| 163 |
* |
| 164 |
* NOTE: base must be normalized |
| 165 |
*/ |
| 166 |
int |
| 167 |
backsql_init_search( |
| 168 |
backsql_srch_info *bsi, |
| 169 |
struct berval *nbase, |
| 170 |
int scope, |
| 171 |
time_t stoptime, |
| 172 |
Filter *filter, |
| 173 |
SQLHDBC dbh, |
| 174 |
Operation *op, |
| 175 |
SlapReply *rs, |
| 176 |
AttributeName *attrs, |
| 177 |
unsigned flags ) |
| 178 |
{ |
| 179 |
backsql_info *bi = (backsql_info *)op->o_bd->be_private; |
| 180 |
int rc = LDAP_SUCCESS; |
| 181 |
|
| 182 |
bsi->bsi_base_ndn = nbase; |
| 183 |
bsi->bsi_use_subtree_shortcut = 0; |
| 184 |
BER_BVZERO( &bsi->bsi_base_id.eid_dn ); |
| 185 |
BER_BVZERO( &bsi->bsi_base_id.eid_ndn ); |
| 186 |
bsi->bsi_scope = scope; |
| 187 |
bsi->bsi_filter = filter; |
| 188 |
bsi->bsi_dbh = dbh; |
| 189 |
bsi->bsi_op = op; |
| 190 |
bsi->bsi_rs = rs; |
| 191 |
bsi->bsi_flags = BSQL_SF_NONE; |
| 192 |
|
| 193 |
bsi->bsi_attrs = NULL; |
| 194 |
|
| 195 |
if ( BACKSQL_FETCH_ALL_ATTRS( bi ) ) { |
| 196 |
/* |
| 197 |
* if requested, simply try to fetch all attributes |
| 198 |
*/ |
| 199 |
bsi->bsi_flags |= BSQL_SF_ALL_ATTRS; |
| 200 |
|
| 201 |
} else { |
| 202 |
if ( BACKSQL_FETCH_ALL_USERATTRS( bi ) ) { |
| 203 |
bsi->bsi_flags |= BSQL_SF_ALL_USER; |
| 204 |
|
| 205 |
} else if ( BACKSQL_FETCH_ALL_OPATTRS( bi ) ) { |
| 206 |
bsi->bsi_flags |= BSQL_SF_ALL_OPER; |
| 207 |
} |
| 208 |
|
| 209 |
if ( attrs == NULL ) { |
| 210 |
/* NULL means all user attributes */ |
| 211 |
bsi->bsi_flags |= BSQL_SF_ALL_USER; |
| 212 |
|
| 213 |
} else { |
| 214 |
AttributeName *p; |
| 215 |
int got_oc = 0; |
| 216 |
|
| 217 |
bsi->bsi_attrs = (AttributeName *)bsi->bsi_op->o_tmpalloc( |
| 218 |
sizeof( AttributeName ), |
| 219 |
bsi->bsi_op->o_tmpmemctx ); |
| 220 |
BER_BVZERO( &bsi->bsi_attrs[ 0 ].an_name ); |
| 221 |
|
| 222 |
for ( p = attrs; !BER_BVISNULL( &p->an_name ); p++ ) { |
| 223 |
if ( BACKSQL_NCMP( &p->an_name, slap_bv_all_user_attrs ) == 0 ) { |
| 224 |
/* handle "*" */ |
| 225 |
bsi->bsi_flags |= BSQL_SF_ALL_USER; |
| 226 |
|
| 227 |
/* if all attrs are requested, there's |
| 228 |
* no need to continue */ |
| 229 |
if ( BSQL_ISF_ALL_ATTRS( bsi ) ) { |
| 230 |
bsi->bsi_op->o_tmpfree( bsi->bsi_attrs, |
| 231 |
bsi->bsi_op->o_tmpmemctx ); |
| 232 |
bsi->bsi_attrs = NULL; |
| 233 |
break; |
| 234 |
} |
| 235 |
continue; |
| 236 |
|
| 237 |
} else if ( BACKSQL_NCMP( &p->an_name, slap_bv_all_operational_attrs ) == 0 ) { |
| 238 |
/* handle "+" */ |
| 239 |
bsi->bsi_flags |= BSQL_SF_ALL_OPER; |
| 240 |
|
| 241 |
/* if all attrs are requested, there's |
| 242 |
* no need to continue */ |
| 243 |
if ( BSQL_ISF_ALL_ATTRS( bsi ) ) { |
| 244 |
bsi->bsi_op->o_tmpfree( bsi->bsi_attrs, |
| 245 |
bsi->bsi_op->o_tmpmemctx ); |
| 246 |
bsi->bsi_attrs = NULL; |
| 247 |
break; |
| 248 |
} |
| 249 |
continue; |
| 250 |
|
| 251 |
} else if ( BACKSQL_NCMP( &p->an_name, slap_bv_no_attrs ) == 0 ) { |
| 252 |
/* ignore "1.1" */ |
| 253 |
continue; |
| 254 |
|
| 255 |
} else if ( p->an_desc == slap_schema.si_ad_objectClass ) { |
| 256 |
got_oc = 1; |
| 257 |
} |
| 258 |
|
| 259 |
backsql_attrlist_add( bsi, p->an_desc ); |
| 260 |
} |
| 261 |
|
| 262 |
if ( got_oc == 0 && !( bsi->bsi_flags & BSQL_SF_ALL_USER ) ) { |
| 263 |
/* add objectClass if not present, |
| 264 |
* because it is required to understand |
| 265 |
* if an entry is a referral, an alias |
| 266 |
* or so... */ |
| 267 |
backsql_attrlist_add( bsi, slap_schema.si_ad_objectClass ); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
if ( !BSQL_ISF_ALL_ATTRS( bsi ) && bi->sql_anlist ) { |
| 272 |
AttributeName *p; |
| 273 |
|
| 274 |
/* use hints if available */ |
| 275 |
for ( p = bi->sql_anlist; !BER_BVISNULL( &p->an_name ); p++ ) { |
| 276 |
if ( BACKSQL_NCMP( &p->an_name, slap_bv_all_user_attrs ) == 0 ) { |
| 277 |
/* handle "*" */ |
| 278 |
bsi->bsi_flags |= BSQL_SF_ALL_USER; |
| 279 |
|
| 280 |
/* if all attrs are requested, there's |
| 281 |
* no need to continue */ |
| 282 |
if ( BSQL_ISF_ALL_ATTRS( bsi ) ) { |
| 283 |
bsi->bsi_op->o_tmpfree( bsi->bsi_attrs, |
| 284 |
bsi->bsi_op->o_tmpmemctx ); |
| 285 |
bsi->bsi_attrs = NULL; |
| 286 |
break; |
| 287 |
} |
| 288 |
continue; |
| 289 |
|
| 290 |
} else if ( BACKSQL_NCMP( &p->an_name, slap_bv_all_operational_attrs ) == 0 ) { |
| 291 |
/* handle "+" */ |
| 292 |
bsi->bsi_flags |= BSQL_SF_ALL_OPER; |
| 293 |
|
| 294 |
/* if all attrs are requested, there's |
| 295 |
* no need to continue */ |
| 296 |
if ( BSQL_ISF_ALL_ATTRS( bsi ) ) { |
| 297 |
bsi->bsi_op->o_tmpfree( bsi->bsi_attrs, |
| 298 |
bsi->bsi_op->o_tmpmemctx ); |
| 299 |
bsi->bsi_attrs = NULL; |
| 300 |
break; |
| 301 |
} |
| 302 |
continue; |
| 303 |
} |
| 304 |
|
| 305 |
backsql_attrlist_add( bsi, p->an_desc ); |
| 306 |
} |
| 307 |
|
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
bsi->bsi_id_list = NULL; |
| 312 |
bsi->bsi_id_listtail = &bsi->bsi_id_list; |
| 313 |
bsi->bsi_n_candidates = 0; |
| 314 |
bsi->bsi_stoptime = stoptime; |
| 315 |
BER_BVZERO( &bsi->bsi_sel.bb_val ); |
| 316 |
bsi->bsi_sel.bb_len = 0; |
| 317 |
BER_BVZERO( &bsi->bsi_from.bb_val ); |
| 318 |
bsi->bsi_from.bb_len = 0; |
| 319 |
BER_BVZERO( &bsi->bsi_join_where.bb_val ); |
| 320 |
bsi->bsi_join_where.bb_len = 0; |
| 321 |
BER_BVZERO( &bsi->bsi_flt_where.bb_val ); |
| 322 |
bsi->bsi_flt_where.bb_len = 0; |
| 323 |
bsi->bsi_filter_oc = NULL; |
| 324 |
|
| 325 |
if ( BACKSQL_IS_GET_ID( flags ) ) { |
| 326 |
int matched = BACKSQL_IS_MATCHED( flags ); |
| 327 |
int getentry = BACKSQL_IS_GET_ENTRY( flags ); |
| 328 |
int gotit = 0; |
| 329 |
|
| 330 |
assert( op->o_bd->be_private != NULL ); |
| 331 |
|
| 332 |
rc = backsql_dn2id( op, rs, dbh, nbase, &bsi->bsi_base_id, |
| 333 |
matched, 1 ); |
| 334 |
|
| 335 |
/* the entry is collected either if requested for by getentry |
| 336 |
* or if get noSuchObject and requested to climb the tree, |
| 337 |
* so that a matchedDN or a referral can be returned */ |
| 338 |
if ( ( rc == LDAP_NO_SUCH_OBJECT && matched ) || getentry ) { |
| 339 |
if ( !BER_BVISNULL( &bsi->bsi_base_id.eid_ndn ) ) { |
| 340 |
assert( bsi->bsi_e != NULL ); |
| 341 |
|
| 342 |
if ( dn_match( nbase, &bsi->bsi_base_id.eid_ndn ) ) |
| 343 |
{ |
| 344 |
gotit = 1; |
| 345 |
} |
| 346 |
|
| 347 |
/* |
| 348 |
* let's see if it is a referral and, in case, get it |
| 349 |
*/ |
| 350 |
backsql_attrlist_add( bsi, slap_schema.si_ad_ref ); |
| 351 |
rc = backsql_id2entry( bsi, &bsi->bsi_base_id ); |
| 352 |
if ( rc == LDAP_SUCCESS ) { |
| 353 |
if ( is_entry_referral( bsi->bsi_e ) ) |
| 354 |
{ |
| 355 |
BerVarray erefs = get_entry_referrals( op, bsi->bsi_e ); |
| 356 |
if ( erefs ) { |
| 357 |
rc = rs->sr_err = LDAP_REFERRAL; |
| 358 |
rs->sr_ref = referral_rewrite( erefs, |
| 359 |
&bsi->bsi_e->e_nname, |
| 360 |
&op->o_req_dn, |
| 361 |
scope ); |
| 362 |
ber_bvarray_free( erefs ); |
| 363 |
|
| 364 |
} else { |
| 365 |
rc = rs->sr_err = LDAP_OTHER; |
| 366 |
rs->sr_text = "bad referral object"; |
| 367 |
} |
| 368 |
|
| 369 |
} else if ( !gotit ) { |
| 370 |
rc = rs->sr_err = LDAP_NO_SUCH_OBJECT; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
} else { |
| 375 |
rs->sr_err = rc; |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
if ( gotit && BACKSQL_IS_GET_OC( flags ) ) { |
| 380 |
bsi->bsi_base_id.eid_oc = backsql_id2oc( bi, |
| 381 |
bsi->bsi_base_id.eid_oc_id ); |
| 382 |
if ( bsi->bsi_base_id.eid_oc == NULL ) { |
| 383 |
/* error? */ |
| 384 |
backsql_free_entryID( &bsi->bsi_base_id, 1, |
| 385 |
op->o_tmpmemctx ); |
| 386 |
rc = rs->sr_err = LDAP_OTHER; |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
bsi->bsi_status = rc; |
| 392 |
|
| 393 |
switch ( rc ) { |
| 394 |
case LDAP_SUCCESS: |
| 395 |
case LDAP_REFERRAL: |
| 396 |
break; |
| 397 |
|
| 398 |
default: |
| 399 |
bsi->bsi_op->o_tmpfree( bsi->bsi_attrs, |
| 400 |
bsi->bsi_op->o_tmpmemctx ); |
| 401 |
break; |
| 402 |
} |
| 403 |
|
| 404 |
return rc; |
| 405 |
} |
| 406 |
|
| 407 |
static int |
| 408 |
backsql_process_filter_list( backsql_srch_info *bsi, Filter *f, int op ) |
| 409 |
{ |
| 410 |
int res; |
| 411 |
|
| 412 |
if ( !f ) { |
| 413 |
return 0; |
| 414 |
} |
| 415 |
|
| 416 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 417 |
bsi->bsi_op->o_tmpmemctx, "c", '(' /* ) */ ); |
| 418 |
|
| 419 |
while ( 1 ) { |
| 420 |
res = backsql_process_filter( bsi, f ); |
| 421 |
if ( res < 0 ) { |
| 422 |
/* |
| 423 |
* TimesTen : If the query has no answers, |
| 424 |
* don't bother to run the query. |
| 425 |
*/ |
| 426 |
return -1; |
| 427 |
} |
| 428 |
|
| 429 |
f = f->f_next; |
| 430 |
if ( f == NULL ) { |
| 431 |
break; |
| 432 |
} |
| 433 |
|
| 434 |
switch ( op ) { |
| 435 |
case LDAP_FILTER_AND: |
| 436 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 437 |
bsi->bsi_op->o_tmpmemctx, "l", |
| 438 |
(ber_len_t)STRLENOF( " AND " ), |
| 439 |
" AND " ); |
| 440 |
break; |
| 441 |
|
| 442 |
case LDAP_FILTER_OR: |
| 443 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 444 |
bsi->bsi_op->o_tmpmemctx, "l", |
| 445 |
(ber_len_t)STRLENOF( " OR " ), |
| 446 |
" OR " ); |
| 447 |
break; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 452 |
bsi->bsi_op->o_tmpmemctx, "c", /* ( */ ')' ); |
| 453 |
|
| 454 |
return 1; |
| 455 |
} |
| 456 |
|
| 457 |
static int |
| 458 |
backsql_process_sub_filter( backsql_srch_info *bsi, Filter *f, |
| 459 |
backsql_at_map_rec *at ) |
| 460 |
{ |
| 461 |
backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private; |
| 462 |
int i; |
| 463 |
int casefold = 0; |
| 464 |
int escaped = 0; |
| 465 |
struct berval escval, *fvalue; |
| 466 |
|
| 467 |
if ( !f ) { |
| 468 |
return 0; |
| 469 |
} |
| 470 |
|
| 471 |
/* always uppercase strings by now */ |
| 472 |
#ifdef BACKSQL_UPPERCASE_FILTER |
| 473 |
if ( f->f_sub_desc->ad_type->sat_substr && |
| 474 |
SLAP_MR_ASSOCIATED( f->f_sub_desc->ad_type->sat_substr, |
| 475 |
bi->sql_caseIgnoreMatch ) ) |
| 476 |
#endif /* BACKSQL_UPPERCASE_FILTER */ |
| 477 |
{ |
| 478 |
casefold = 1; |
| 479 |
} |
| 480 |
|
| 481 |
if ( f->f_sub_desc->ad_type->sat_substr && |
| 482 |
SLAP_MR_ASSOCIATED( f->f_sub_desc->ad_type->sat_substr, |
| 483 |
bi->sql_telephoneNumberMatch ) ) |
| 484 |
{ |
| 485 |
|
| 486 |
struct berval bv; |
| 487 |
ber_len_t i, s, a; |
| 488 |
|
| 489 |
/* |
| 490 |
* to check for matching telephone numbers |
| 491 |
* with intermixed chars, e.g. val='1234' |
| 492 |
* use |
| 493 |
* |
| 494 |
* val LIKE '%1%2%3%4%' |
| 495 |
*/ |
| 496 |
|
| 497 |
BER_BVZERO( &bv ); |
| 498 |
if ( f->f_sub_initial.bv_val ) { |
| 499 |
bv.bv_len += f->f_sub_initial.bv_len + backsql_val_escape( NULL, &f->f_sub_initial, NULL ); |
| 500 |
} |
| 501 |
if ( f->f_sub_any != NULL ) { |
| 502 |
for ( a = 0; f->f_sub_any[ a ].bv_val != NULL; a++ ) { |
| 503 |
bv.bv_len += f->f_sub_any[ a ].bv_len + backsql_val_escape( NULL, &f->f_sub_any[ a ], NULL ); |
| 504 |
} |
| 505 |
} |
| 506 |
if ( f->f_sub_final.bv_val ) { |
| 507 |
bv.bv_len += f->f_sub_final.bv_len + backsql_val_escape( NULL, &f->f_sub_final, NULL ); |
| 508 |
} |
| 509 |
bv.bv_len = 2 * bv.bv_len - 1; |
| 510 |
bv.bv_val = ch_malloc( bv.bv_len + 1 ); |
| 511 |
|
| 512 |
s = 0; |
| 513 |
if ( !BER_BVISNULL( &f->f_sub_initial ) ) { |
| 514 |
fvalue = &f->f_sub_initial; |
| 515 |
escaped = backsql_val_escape( bsi->bsi_op, fvalue, &escval ); |
| 516 |
if ( escaped ) |
| 517 |
fvalue = &escval; |
| 518 |
bv.bv_val[ s ] = fvalue->bv_val[ 0 ]; |
| 519 |
for ( i = 1; i < fvalue->bv_len; i++ ) { |
| 520 |
bv.bv_val[ s + 2 * i - 1 ] = '%'; |
| 521 |
bv.bv_val[ s + 2 * i ] = fvalue->bv_val[ i ]; |
| 522 |
} |
| 523 |
bv.bv_val[ s + 2 * i - 1 ] = '%'; |
| 524 |
s += 2 * i; |
| 525 |
if ( escaped ) |
| 526 |
bsi->bsi_op->o_tmpfree( escval.bv_val, bsi->bsi_op->o_tmpmemctx ); |
| 527 |
} |
| 528 |
|
| 529 |
if ( f->f_sub_any != NULL ) { |
| 530 |
for ( a = 0; !BER_BVISNULL( &f->f_sub_any[ a ] ); a++ ) { |
| 531 |
fvalue = &f->f_sub_any[ a ]; |
| 532 |
escaped = backsql_val_escape( bsi->bsi_op, fvalue, &escval ); |
| 533 |
if ( escaped ) |
| 534 |
fvalue = &escval; |
| 535 |
bv.bv_val[ s ] = fvalue->bv_val[ 0 ]; |
| 536 |
for ( i = 1; i < fvalue->bv_len; i++ ) { |
| 537 |
bv.bv_val[ s + 2 * i - 1 ] = '%'; |
| 538 |
bv.bv_val[ s + 2 * i ] = fvalue->bv_val[ i ]; |
| 539 |
} |
| 540 |
bv.bv_val[ s + 2 * i - 1 ] = '%'; |
| 541 |
s += 2 * i; |
| 542 |
if ( escaped ) |
| 543 |
bsi->bsi_op->o_tmpfree( escval.bv_val, bsi->bsi_op->o_tmpmemctx ); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
if ( !BER_BVISNULL( &f->f_sub_final ) ) { |
| 548 |
fvalue = &f->f_sub_final; |
| 549 |
escaped = backsql_val_escape( bsi->bsi_op, fvalue, &escval ); |
| 550 |
if ( escaped ) |
| 551 |
fvalue = &escval; |
| 552 |
bv.bv_val[ s ] = fvalue->bv_val[ 0 ]; |
| 553 |
for ( i = 1; i < fvalue->bv_len; i++ ) { |
| 554 |
bv.bv_val[ s + 2 * i - 1 ] = '%'; |
| 555 |
bv.bv_val[ s + 2 * i ] = fvalue->bv_val[ i ]; |
| 556 |
} |
| 557 |
bv.bv_val[ s + 2 * i - 1 ] = '%'; |
| 558 |
s += 2 * i; |
| 559 |
if ( escaped ) |
| 560 |
bsi->bsi_op->o_tmpfree( escval.bv_val, bsi->bsi_op->o_tmpmemctx ); |
| 561 |
} |
| 562 |
|
| 563 |
bv.bv_val[ s - 1 ] = '\0'; |
| 564 |
|
| 565 |
(void)backsql_process_filter_like( bsi, at, casefold, &bv ); |
| 566 |
ch_free( bv.bv_val ); |
| 567 |
|
| 568 |
return 1; |
| 569 |
} |
| 570 |
|
| 571 |
/* |
| 572 |
* When dealing with case-sensitive strings |
| 573 |
* we may omit normalization; however, normalized |
| 574 |
* SQL filters are more liberal. |
| 575 |
*/ |
| 576 |
|
| 577 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 578 |
bsi->bsi_op->o_tmpmemctx, "c", '(' /* ) */ ); |
| 579 |
|
| 580 |
/* TimesTen */ |
| 581 |
Debug( LDAP_DEBUG_TRACE, "backsql_process_sub_filter(%s):\n", |
| 582 |
at->bam_ad->ad_cname.bv_val ); |
| 583 |
Debug(LDAP_DEBUG_TRACE, " expr: '%s%s%s'\n", at->bam_sel_expr.bv_val, |
| 584 |
at->bam_sel_expr_u.bv_val ? "' '" : "", |
| 585 |
at->bam_sel_expr_u.bv_val ? at->bam_sel_expr_u.bv_val : "" ); |
| 586 |
if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) { |
| 587 |
/* |
| 588 |
* If a pre-upper-cased version of the column |
| 589 |
* or a precompiled upper function exists, use it |
| 590 |
*/ |
| 591 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 592 |
bsi->bsi_op->o_tmpmemctx, |
| 593 |
"bl", |
| 594 |
&at->bam_sel_expr_u, |
| 595 |
(ber_len_t)STRLENOF( " LIKE '" ), |
| 596 |
" LIKE '" ); |
| 597 |
|
| 598 |
} else { |
| 599 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 600 |
bsi->bsi_op->o_tmpmemctx, |
| 601 |
"bl", |
| 602 |
&at->bam_sel_expr, |
| 603 |
(ber_len_t)STRLENOF( " LIKE '" ), " LIKE '" ); |
| 604 |
} |
| 605 |
|
| 606 |
if ( !BER_BVISNULL( &f->f_sub_initial ) ) { |
| 607 |
ber_len_t start; |
| 608 |
|
| 609 |
#ifdef BACKSQL_TRACE |
| 610 |
Debug( LDAP_DEBUG_TRACE, |
| 611 |
"==>backsql_process_sub_filter(%s): " |
| 612 |
"sub_initial=\"%s\"\n", at->bam_ad->ad_cname.bv_val, |
| 613 |
f->f_sub_initial.bv_val ); |
| 614 |
#endif /* BACKSQL_TRACE */ |
| 615 |
|
| 616 |
fvalue = &f->f_sub_initial; |
| 617 |
escaped = backsql_val_escape( bsi->bsi_op, fvalue, &escval ); |
| 618 |
if ( escaped ) |
| 619 |
fvalue = &escval; |
| 620 |
start = bsi->bsi_flt_where.bb_val.bv_len; |
| 621 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 622 |
bsi->bsi_op->o_tmpmemctx, |
| 623 |
"b", |
| 624 |
fvalue ); |
| 625 |
if ( escaped ) |
| 626 |
bsi->bsi_op->o_tmpfree( escval.bv_val, bsi->bsi_op->o_tmpmemctx ); |
| 627 |
if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) { |
| 628 |
ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] ); |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 633 |
bsi->bsi_op->o_tmpmemctx, |
| 634 |
"c", '%' ); |
| 635 |
|
| 636 |
if ( f->f_sub_any != NULL ) { |
| 637 |
for ( i = 0; !BER_BVISNULL( &f->f_sub_any[ i ] ); i++ ) { |
| 638 |
ber_len_t start; |
| 639 |
|
| 640 |
#ifdef BACKSQL_TRACE |
| 641 |
Debug( LDAP_DEBUG_TRACE, |
| 642 |
"==>backsql_process_sub_filter(%s): " |
| 643 |
"sub_any[%d]=\"%s\"\n", at->bam_ad->ad_cname.bv_val, |
| 644 |
i, f->f_sub_any[ i ].bv_val ); |
| 645 |
#endif /* BACKSQL_TRACE */ |
| 646 |
|
| 647 |
fvalue = &f->f_sub_any[ i ]; |
| 648 |
escaped = backsql_val_escape( bsi->bsi_op, fvalue, &escval ); |
| 649 |
if ( escaped ) |
| 650 |
fvalue = &escval; |
| 651 |
start = bsi->bsi_flt_where.bb_val.bv_len; |
| 652 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 653 |
bsi->bsi_op->o_tmpmemctx, |
| 654 |
"bc", |
| 655 |
fvalue, |
| 656 |
'%' ); |
| 657 |
if ( escaped ) |
| 658 |
bsi->bsi_op->o_tmpfree( escval.bv_val, bsi->bsi_op->o_tmpmemctx ); |
| 659 |
if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) { |
| 660 |
/* |
| 661 |
* Note: toupper('%') = '%' |
| 662 |
*/ |
| 663 |
ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] ); |
| 664 |
} |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
if ( !BER_BVISNULL( &f->f_sub_final ) ) { |
| 669 |
ber_len_t start; |
| 670 |
|
| 671 |
#ifdef BACKSQL_TRACE |
| 672 |
Debug( LDAP_DEBUG_TRACE, |
| 673 |
"==>backsql_process_sub_filter(%s): " |
| 674 |
"sub_final=\"%s\"\n", at->bam_ad->ad_cname.bv_val, |
| 675 |
f->f_sub_final.bv_val ); |
| 676 |
#endif /* BACKSQL_TRACE */ |
| 677 |
|
| 678 |
fvalue = &f->f_sub_final; |
| 679 |
escaped = backsql_val_escape( bsi->bsi_op, fvalue, &escval ); |
| 680 |
if ( escaped ) |
| 681 |
fvalue = &escval; |
| 682 |
start = bsi->bsi_flt_where.bb_val.bv_len; |
| 683 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 684 |
bsi->bsi_op->o_tmpmemctx, |
| 685 |
"b", |
| 686 |
fvalue ); |
| 687 |
if ( escaped ) |
| 688 |
bsi->bsi_op->o_tmpfree( escval.bv_val, bsi->bsi_op->o_tmpmemctx ); |
| 689 |
if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) { |
| 690 |
ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] ); |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 695 |
bsi->bsi_op->o_tmpmemctx, |
| 696 |
"l", |
| 697 |
(ber_len_t)STRLENOF( /* (' */ "')" ), /* (' */ "')" ); |
| 698 |
|
| 699 |
return 1; |
| 700 |
} |
| 701 |
|
| 702 |
static int |
| 703 |
backsql_merge_from_tbls( backsql_srch_info *bsi, struct berval *from_tbls ) |
| 704 |
{ |
| 705 |
if ( BER_BVISNULL( from_tbls ) ) { |
| 706 |
return LDAP_SUCCESS; |
| 707 |
} |
| 708 |
|
| 709 |
if ( !BER_BVISNULL( &bsi->bsi_from.bb_val ) ) { |
| 710 |
char *start, *end; |
| 711 |
struct berval tmp; |
| 712 |
|
| 713 |
ber_dupbv_x( &tmp, from_tbls, bsi->bsi_op->o_tmpmemctx ); |
| 714 |
|
| 715 |
for ( start = tmp.bv_val, end = strchr( start, ',' ); start; ) { |
| 716 |
if ( end ) { |
| 717 |
end[0] = '\0'; |
| 718 |
} |
| 719 |
|
| 720 |
if ( strstr( bsi->bsi_from.bb_val.bv_val, start) == NULL ) |
| 721 |
{ |
| 722 |
backsql_strfcat_x( &bsi->bsi_from, |
| 723 |
bsi->bsi_op->o_tmpmemctx, |
| 724 |
"cs", ',', start ); |
| 725 |
} |
| 726 |
|
| 727 |
if ( end ) { |
| 728 |
/* in case there are spaces after the comma... */ |
| 729 |
for ( start = &end[1]; isspace( start[0] ); start++ ); |
| 730 |
if ( start[0] ) { |
| 731 |
end = strchr( start, ',' ); |
| 732 |
} else { |
| 733 |
start = NULL; |
| 734 |
} |
| 735 |
} else { |
| 736 |
start = NULL; |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
bsi->bsi_op->o_tmpfree( tmp.bv_val, bsi->bsi_op->o_tmpmemctx ); |
| 741 |
|
| 742 |
} else { |
| 743 |
backsql_strfcat_x( &bsi->bsi_from, |
| 744 |
bsi->bsi_op->o_tmpmemctx, |
| 745 |
"b", from_tbls ); |
| 746 |
} |
| 747 |
|
| 748 |
return LDAP_SUCCESS; |
| 749 |
} |
| 750 |
|
| 751 |
static int |
| 752 |
backsql_process_filter( backsql_srch_info *bsi, Filter *f ) |
| 753 |
{ |
| 754 |
backsql_at_map_rec **vat = NULL; |
| 755 |
AttributeDescription *ad = NULL; |
| 756 |
unsigned i; |
| 757 |
int done = 0; |
| 758 |
int rc = 0; |
| 759 |
|
| 760 |
Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter()\n" ); |
| 761 |
if ( f->f_choice == SLAPD_FILTER_COMPUTED ) { |
| 762 |
struct berval flt; |
| 763 |
char *msg = NULL; |
| 764 |
|
| 765 |
switch ( f->f_result ) { |
| 766 |
case LDAP_COMPARE_TRUE: |
| 767 |
BER_BVSTR( &flt, "10=10" ); |
| 768 |
msg = "TRUE"; |
| 769 |
break; |
| 770 |
|
| 771 |
case LDAP_COMPARE_FALSE: |
| 772 |
BER_BVSTR( &flt, "11=0" ); |
| 773 |
msg = "FALSE"; |
| 774 |
break; |
| 775 |
|
| 776 |
case SLAPD_COMPARE_UNDEFINED: |
| 777 |
BER_BVSTR( &flt, "12=0" ); |
| 778 |
msg = "UNDEFINED"; |
| 779 |
break; |
| 780 |
|
| 781 |
default: |
| 782 |
rc = -1; |
| 783 |
goto done; |
| 784 |
} |
| 785 |
|
| 786 |
Debug( LDAP_DEBUG_TRACE, "backsql_process_filter(): " |
| 787 |
"filter computed (%s)\n", msg ); |
| 788 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 789 |
bsi->bsi_op->o_tmpmemctx, "b", &flt ); |
| 790 |
rc = 1; |
| 791 |
goto done; |
| 792 |
} |
| 793 |
|
| 794 |
if ( f->f_choice & SLAPD_FILTER_UNDEFINED ) { |
| 795 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 796 |
bsi->bsi_op->o_tmpmemctx, |
| 797 |
"l", |
| 798 |
(ber_len_t)STRLENOF( "1=0" ), "1=0" ); |
| 799 |
done = 1; |
| 800 |
rc = 1; |
| 801 |
goto done; |
| 802 |
} |
| 803 |
|
| 804 |
switch( f->f_choice ) { |
| 805 |
case LDAP_FILTER_OR: |
| 806 |
rc = backsql_process_filter_list( bsi, f->f_or, |
| 807 |
LDAP_FILTER_OR ); |
| 808 |
done = 1; |
| 809 |
break; |
| 810 |
|
| 811 |
case LDAP_FILTER_AND: |
| 812 |
rc = backsql_process_filter_list( bsi, f->f_and, |
| 813 |
LDAP_FILTER_AND ); |
| 814 |
done = 1; |
| 815 |
break; |
| 816 |
|
| 817 |
case LDAP_FILTER_NOT: |
| 818 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 819 |
bsi->bsi_op->o_tmpmemctx, |
| 820 |
"l", |
| 821 |
(ber_len_t)STRLENOF( "NOT (" /* ) */ ), |
| 822 |
"NOT (" /* ) */ ); |
| 823 |
rc = backsql_process_filter( bsi, f->f_not ); |
| 824 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 825 |
bsi->bsi_op->o_tmpmemctx, |
| 826 |
"c", /* ( */ ')' ); |
| 827 |
done = 1; |
| 828 |
break; |
| 829 |
|
| 830 |
case LDAP_FILTER_PRESENT: |
| 831 |
ad = f->f_desc; |
| 832 |
break; |
| 833 |
|
| 834 |
case LDAP_FILTER_EXT: |
| 835 |
ad = f->f_mra->ma_desc; |
| 836 |
if ( f->f_mr_dnattrs ) { |
| 837 |
/* |
| 838 |
* if dn attrs filtering is requested, better return |
| 839 |
* success and let test_filter() deal with candidate |
| 840 |
* selection; otherwise we'd need to set conditions |
| 841 |
* on the contents of the DN, e.g. "SELECT ... FROM |
| 842 |
* ldap_entries AS attributeName WHERE attributeName.dn |
| 843 |
* like '%attributeName=value%'" |
| 844 |
*/ |
| 845 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 846 |
bsi->bsi_op->o_tmpmemctx, |
| 847 |
"l", |
| 848 |
(ber_len_t)STRLENOF( "1=1" ), "1=1" ); |
| 849 |
bsi->bsi_status = LDAP_SUCCESS; |
| 850 |
rc = 1; |
| 851 |
goto done; |
| 852 |
} |
| 853 |
break; |
| 854 |
|
| 855 |
default: |
| 856 |
ad = f->f_av_desc; |
| 857 |
break; |
| 858 |
} |
| 859 |
|
| 860 |
if ( rc == -1 ) { |
| 861 |
goto done; |
| 862 |
} |
| 863 |
|
| 864 |
if ( done ) { |
| 865 |
rc = 1; |
| 866 |
goto done; |
| 867 |
} |
| 868 |
|
| 869 |
/* |
| 870 |
* Turn structuralObjectClass into objectClass |
| 871 |
*/ |
| 872 |
if ( ad == slap_schema.si_ad_objectClass |
| 873 |
|| ad == slap_schema.si_ad_structuralObjectClass ) |
| 874 |
{ |
| 875 |
/* |
| 876 |
* If the filter is LDAP_FILTER_PRESENT, then it's done; |
| 877 |
* otherwise, let's see if we are lucky: filtering |
| 878 |
* for "structural" objectclass or ancestor... |
| 879 |
*/ |
| 880 |
switch ( f->f_choice ) { |
| 881 |
case LDAP_FILTER_EQUALITY: |
| 882 |
{ |
| 883 |
ObjectClass *oc = oc_bvfind( &f->f_av_value ); |
| 884 |
|
| 885 |
if ( oc == NULL ) { |
| 886 |
Debug( LDAP_DEBUG_TRACE, |
| 887 |
"backsql_process_filter(): " |
| 888 |
"unknown objectClass \"%s\" " |
| 889 |
"in filter\n", |
| 890 |
f->f_av_value.bv_val ); |
| 891 |
bsi->bsi_status = LDAP_OTHER; |
| 892 |
rc = -1; |
| 893 |
goto done; |
| 894 |
} |
| 895 |
|
| 896 |
/* |
| 897 |
* "structural" objectClass inheritance: |
| 898 |
* - a search for "person" will also return |
| 899 |
* "inetOrgPerson" |
| 900 |
* - a search for "top" will return everything |
| 901 |
*/ |
| 902 |
if ( is_object_subclass( oc, bsi->bsi_oc->bom_oc ) ) { |
| 903 |
static struct berval ldap_entry_objclasses = BER_BVC( "ldap_entry_objclasses" ); |
| 904 |
|
| 905 |
backsql_merge_from_tbls( bsi, &ldap_entry_objclasses ); |
| 906 |
|
| 907 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 908 |
bsi->bsi_op->o_tmpmemctx, |
| 909 |
"lbl", |
| 910 |
(ber_len_t)STRLENOF( "(2=2 OR (ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='" /* ')) */ ), |
| 911 |
"(2=2 OR (ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='" /* ')) */, |
| 912 |
&bsi->bsi_oc->bom_oc->soc_cname, |
| 913 |
(ber_len_t)STRLENOF( /* ((' */ "'))" ), |
| 914 |
/* ((' */ "'))" ); |
| 915 |
bsi->bsi_status = LDAP_SUCCESS; |
| 916 |
rc = 1; |
| 917 |
goto done; |
| 918 |
} |
| 919 |
|
| 920 |
break; |
| 921 |
} |
| 922 |
|
| 923 |
case LDAP_FILTER_PRESENT: |
| 924 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 925 |
bsi->bsi_op->o_tmpmemctx, |
| 926 |
"l", |
| 927 |
(ber_len_t)STRLENOF( "3=3" ), "3=3" ); |
| 928 |
bsi->bsi_status = LDAP_SUCCESS; |
| 929 |
rc = 1; |
| 930 |
goto done; |
| 931 |
|
| 932 |
/* FIXME: LDAP_FILTER_EXT? */ |
| 933 |
|
| 934 |
default: |
| 935 |
Debug( LDAP_DEBUG_TRACE, |
| 936 |
"backsql_process_filter(): " |
| 937 |
"illegal/unhandled filter " |
| 938 |
"on objectClass attribute" ); |
| 939 |
bsi->bsi_status = LDAP_OTHER; |
| 940 |
rc = -1; |
| 941 |
goto done; |
| 942 |
} |
| 943 |
|
| 944 |
} else if ( ad == slap_schema.si_ad_entryUUID ) { |
| 945 |
unsigned long oc_id; |
| 946 |
#ifdef BACKSQL_ARBITRARY_KEY |
| 947 |
struct berval keyval; |
| 948 |
#else /* ! BACKSQL_ARBITRARY_KEY */ |
| 949 |
unsigned long keyval; |
| 950 |
char keyvalbuf[LDAP_PVT_INTTYPE_CHARS(unsigned long)]; |
| 951 |
#endif /* ! BACKSQL_ARBITRARY_KEY */ |
| 952 |
|
| 953 |
switch ( f->f_choice ) { |
| 954 |
case LDAP_FILTER_EQUALITY: |
| 955 |
backsql_entryUUID_decode( &f->f_av_value, &oc_id, &keyval ); |
| 956 |
|
| 957 |
if ( oc_id != bsi->bsi_oc->bom_id ) { |
| 958 |
bsi->bsi_status = LDAP_SUCCESS; |
| 959 |
rc = -1; |
| 960 |
goto done; |
| 961 |
} |
| 962 |
|
| 963 |
#ifdef BACKSQL_ARBITRARY_KEY |
| 964 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 965 |
bsi->bsi_op->o_tmpmemctx, |
| 966 |
"bcblbc", |
| 967 |
&bsi->bsi_oc->bom_keytbl, '.', |
| 968 |
&bsi->bsi_oc->bom_keycol, |
| 969 |
STRLENOF( " LIKE '" ), " LIKE '", |
| 970 |
&keyval, '\'' ); |
| 971 |
#else /* ! BACKSQL_ARBITRARY_KEY */ |
| 972 |
snprintf( keyvalbuf, sizeof( keyvalbuf ), "%lu", keyval ); |
| 973 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 974 |
bsi->bsi_op->o_tmpmemctx, |
| 975 |
"bcbcs", |
| 976 |
&bsi->bsi_oc->bom_keytbl, '.', |
| 977 |
&bsi->bsi_oc->bom_keycol, '=', keyvalbuf ); |
| 978 |
#endif /* ! BACKSQL_ARBITRARY_KEY */ |
| 979 |
break; |
| 980 |
|
| 981 |
case LDAP_FILTER_PRESENT: |
| 982 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 983 |
bsi->bsi_op->o_tmpmemctx, |
| 984 |
"l", |
| 985 |
(ber_len_t)STRLENOF( "4=4" ), "4=4" ); |
| 986 |
break; |
| 987 |
|
| 988 |
default: |
| 989 |
rc = -1; |
| 990 |
goto done; |
| 991 |
} |
| 992 |
|
| 993 |
bsi->bsi_flags |= BSQL_SF_FILTER_ENTRYUUID; |
| 994 |
rc = 1; |
| 995 |
goto done; |
| 996 |
|
| 997 |
#ifdef BACKSQL_SYNCPROV |
| 998 |
} else if ( ad == slap_schema.si_ad_entryCSN ) { |
| 999 |
/* |
| 1000 |
* support for syncrepl as provider... |
| 1001 |
*/ |
| 1002 |
#if 0 |
| 1003 |
if ( !bsi->bsi_op->o_sync ) { |
| 1004 |
/* unsupported at present... */ |
| 1005 |
bsi->bsi_status = LDAP_OTHER; |
| 1006 |
rc = -1; |
| 1007 |
goto done; |
| 1008 |
} |
| 1009 |
#endif |
| 1010 |
|
| 1011 |
bsi->bsi_flags |= ( BSQL_SF_FILTER_ENTRYCSN | BSQL_SF_RETURN_ENTRYUUID); |
| 1012 |
|
| 1013 |
/* if doing a syncrepl, try to return as much as possible, |
| 1014 |
* and always match the filter */ |
| 1015 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1016 |
bsi->bsi_op->o_tmpmemctx, |
| 1017 |
"l", |
| 1018 |
(ber_len_t)STRLENOF( "5=5" ), "5=5" ); |
| 1019 |
|
| 1020 |
/* save for later use in operational attributes */ |
| 1021 |
/* FIXME: saves only the first occurrence, because |
| 1022 |
* the filter during updates is written as |
| 1023 |
* "(&(entryCSN<={contextCSN})(entryCSN>={oldContextCSN})({filter}))" |
| 1024 |
* so we want our fake entryCSN to match the greatest |
| 1025 |
* value |
| 1026 |
*/ |
| 1027 |
if ( bsi->bsi_op->o_private == NULL ) { |
| 1028 |
bsi->bsi_op->o_private = &f->f_av_value; |
| 1029 |
} |
| 1030 |
bsi->bsi_status = LDAP_SUCCESS; |
| 1031 |
|
| 1032 |
rc = 1; |
| 1033 |
goto done; |
| 1034 |
#endif /* BACKSQL_SYNCPROV */ |
| 1035 |
|
| 1036 |
} else if ( ad == slap_schema.si_ad_hasSubordinates || ad == NULL ) { |
| 1037 |
/* |
| 1038 |
* FIXME: this is not robust; e.g. a filter |
| 1039 |
* '(!(hasSubordinates=TRUE))' fails because |
| 1040 |
* in SQL it would read 'NOT (1=1)' instead |
| 1041 |
* of no condition. |
| 1042 |
* Note however that hasSubordinates is boolean, |
| 1043 |
* so a more appropriate filter would be |
| 1044 |
* '(hasSubordinates=FALSE)' |
| 1045 |
* |
| 1046 |
* A more robust search for hasSubordinates |
| 1047 |
* would * require joining the ldap_entries table |
| 1048 |
* selecting if there are descendants of the |
| 1049 |
* candidate. |
| 1050 |
*/ |
| 1051 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1052 |
bsi->bsi_op->o_tmpmemctx, |
| 1053 |
"l", |
| 1054 |
(ber_len_t)STRLENOF( "6=6" ), "6=6" ); |
| 1055 |
if ( ad == slap_schema.si_ad_hasSubordinates ) { |
| 1056 |
/* |
| 1057 |
* instruct candidate selection algorithm |
| 1058 |
* and attribute list to try to detect |
| 1059 |
* if an entry has subordinates |
| 1060 |
*/ |
| 1061 |
bsi->bsi_flags |= BSQL_SF_FILTER_HASSUBORDINATE; |
| 1062 |
|
| 1063 |
} else { |
| 1064 |
/* |
| 1065 |
* clear attributes to fetch, to require ALL |
| 1066 |
* and try extended match on all attributes |
| 1067 |
*/ |
| 1068 |
backsql_attrlist_add( bsi, NULL ); |
| 1069 |
} |
| 1070 |
rc = 1; |
| 1071 |
goto done; |
| 1072 |
} |
| 1073 |
|
| 1074 |
/* |
| 1075 |
* attribute inheritance: |
| 1076 |
*/ |
| 1077 |
if ( backsql_supad2at( bsi->bsi_oc, ad, &vat ) ) { |
| 1078 |
bsi->bsi_status = LDAP_OTHER; |
| 1079 |
rc = -1; |
| 1080 |
goto done; |
| 1081 |
} |
| 1082 |
|
| 1083 |
if ( vat == NULL ) { |
| 1084 |
/* search anyway; other parts of the filter |
| 1085 |
* may succeed */ |
| 1086 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1087 |
bsi->bsi_op->o_tmpmemctx, |
| 1088 |
"l", |
| 1089 |
(ber_len_t)STRLENOF( "7=7" ), "7=7" ); |
| 1090 |
bsi->bsi_status = LDAP_SUCCESS; |
| 1091 |
rc = 1; |
| 1092 |
goto done; |
| 1093 |
} |
| 1094 |
|
| 1095 |
/* if required, open extra level of parens */ |
| 1096 |
done = 0; |
| 1097 |
if ( vat[0]->bam_next || vat[1] ) { |
| 1098 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1099 |
bsi->bsi_op->o_tmpmemctx, |
| 1100 |
"c", '(' ); |
| 1101 |
done = 1; |
| 1102 |
} |
| 1103 |
|
| 1104 |
i = 0; |
| 1105 |
next:; |
| 1106 |
/* apply attr */ |
| 1107 |
if ( backsql_process_filter_attr( bsi, f, vat[i] ) == -1 ) { |
| 1108 |
return -1; |
| 1109 |
} |
| 1110 |
|
| 1111 |
/* if more definitions of the same attr, apply */ |
| 1112 |
if ( vat[i]->bam_next ) { |
| 1113 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1114 |
bsi->bsi_op->o_tmpmemctx, |
| 1115 |
"l", |
| 1116 |
STRLENOF( " OR " ), " OR " ); |
| 1117 |
vat[i] = vat[i]->bam_next; |
| 1118 |
goto next; |
| 1119 |
} |
| 1120 |
|
| 1121 |
/* if more descendants of the same attr, apply */ |
| 1122 |
i++; |
| 1123 |
if ( vat[i] ) { |
| 1124 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1125 |
bsi->bsi_op->o_tmpmemctx, |
| 1126 |
"l", |
| 1127 |
STRLENOF( " OR " ), " OR " ); |
| 1128 |
goto next; |
| 1129 |
} |
| 1130 |
|
| 1131 |
/* if needed, close extra level of parens */ |
| 1132 |
if ( done ) { |
| 1133 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1134 |
bsi->bsi_op->o_tmpmemctx, |
| 1135 |
"c", ')' ); |
| 1136 |
} |
| 1137 |
|
| 1138 |
rc = 1; |
| 1139 |
|
| 1140 |
done:; |
| 1141 |
if ( vat ) { |
| 1142 |
ch_free( vat ); |
| 1143 |
} |
| 1144 |
|
| 1145 |
Debug( LDAP_DEBUG_TRACE, |
| 1146 |
"<==backsql_process_filter() %s\n", |
| 1147 |
rc == 1 ? "succeeded" : "failed" ); |
| 1148 |
|
| 1149 |
return rc; |
| 1150 |
} |
| 1151 |
|
| 1152 |
static int |
| 1153 |
backsql_process_filter_eq( backsql_srch_info *bsi, backsql_at_map_rec *at, |
| 1154 |
int casefold, struct berval *filter_value ) |
| 1155 |
{ |
| 1156 |
/* |
| 1157 |
* maybe we should check type of at->sel_expr here somehow, |
| 1158 |
* to know whether upper_func is applicable, but for now |
| 1159 |
* upper_func stuff is made for Oracle, where UPPER is |
| 1160 |
* safely applicable to NUMBER etc. |
| 1161 |
*/ |
| 1162 |
if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) { |
| 1163 |
ber_len_t start; |
| 1164 |
|
| 1165 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1166 |
bsi->bsi_op->o_tmpmemctx, |
| 1167 |
"cbl", |
| 1168 |
'(', /* ) */ |
| 1169 |
&at->bam_sel_expr_u, |
| 1170 |
(ber_len_t)STRLENOF( "='" ), |
| 1171 |
"='" ); |
| 1172 |
|
| 1173 |
start = bsi->bsi_flt_where.bb_val.bv_len; |
| 1174 |
|
| 1175 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1176 |
bsi->bsi_op->o_tmpmemctx, |
| 1177 |
"bl", |
| 1178 |
filter_value, |
| 1179 |
(ber_len_t)STRLENOF( /* (' */ "')" ), |
| 1180 |
/* (' */ "')" ); |
| 1181 |
|
| 1182 |
ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] ); |
| 1183 |
|
| 1184 |
} else { |
| 1185 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1186 |
bsi->bsi_op->o_tmpmemctx, |
| 1187 |
"cblbl", |
| 1188 |
'(', /* ) */ |
| 1189 |
&at->bam_sel_expr, |
| 1190 |
(ber_len_t)STRLENOF( "='" ), "='", |
| 1191 |
filter_value, |
| 1192 |
(ber_len_t)STRLENOF( /* (' */ "')" ), |
| 1193 |
/* (' */ "')" ); |
| 1194 |
} |
| 1195 |
|
| 1196 |
return 1; |
| 1197 |
} |
| 1198 |
|
| 1199 |
static int |
| 1200 |
backsql_process_filter_like( backsql_srch_info *bsi, backsql_at_map_rec *at, |
| 1201 |
int casefold, struct berval *filter_value ) |
| 1202 |
{ |
| 1203 |
/* |
| 1204 |
* maybe we should check type of at->sel_expr here somehow, |
| 1205 |
* to know whether upper_func is applicable, but for now |
| 1206 |
* upper_func stuff is made for Oracle, where UPPER is |
| 1207 |
* safely applicable to NUMBER etc. |
| 1208 |
*/ |
| 1209 |
if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) { |
| 1210 |
ber_len_t start; |
| 1211 |
|
| 1212 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1213 |
bsi->bsi_op->o_tmpmemctx, |
| 1214 |
"cbl", |
| 1215 |
'(', /* ) */ |
| 1216 |
&at->bam_sel_expr_u, |
| 1217 |
(ber_len_t)STRLENOF( " LIKE '%" ), |
| 1218 |
" LIKE '%" ); |
| 1219 |
|
| 1220 |
start = bsi->bsi_flt_where.bb_val.bv_len; |
| 1221 |
|
| 1222 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1223 |
bsi->bsi_op->o_tmpmemctx, |
| 1224 |
"bl", |
| 1225 |
filter_value, |
| 1226 |
(ber_len_t)STRLENOF( /* (' */ "%')" ), |
| 1227 |
/* (' */ "%')" ); |
| 1228 |
|
| 1229 |
ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] ); |
| 1230 |
|
| 1231 |
} else { |
| 1232 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1233 |
bsi->bsi_op->o_tmpmemctx, |
| 1234 |
"cblbl", |
| 1235 |
'(', /* ) */ |
| 1236 |
&at->bam_sel_expr, |
| 1237 |
(ber_len_t)STRLENOF( " LIKE '%" ), |
| 1238 |
" LIKE '%", |
| 1239 |
filter_value, |
| 1240 |
(ber_len_t)STRLENOF( /* (' */ "%')" ), |
| 1241 |
/* (' */ "%')" ); |
| 1242 |
} |
| 1243 |
|
| 1244 |
return 1; |
| 1245 |
} |
| 1246 |
|
| 1247 |
static int |
| 1248 |
backsql_process_filter_attr( backsql_srch_info *bsi, Filter *f, backsql_at_map_rec *at ) |
| 1249 |
{ |
| 1250 |
backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private; |
| 1251 |
int casefold = 0; |
| 1252 |
struct berval *filter_value = NULL; |
| 1253 |
MatchingRule *matching_rule = NULL; |
| 1254 |
struct berval ordering = BER_BVC("<="); |
| 1255 |
struct berval escval; |
| 1256 |
int escaped = 0; |
| 1257 |
|
| 1258 |
Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter_attr(%s)\n", |
| 1259 |
at->bam_ad->ad_cname.bv_val ); |
| 1260 |
|
| 1261 |
/* |
| 1262 |
* need to add this attribute to list of attrs to load, |
| 1263 |
* so that we can do test_filter() later |
| 1264 |
*/ |
| 1265 |
backsql_attrlist_add( bsi, at->bam_ad ); |
| 1266 |
|
| 1267 |
backsql_merge_from_tbls( bsi, &at->bam_from_tbls ); |
| 1268 |
|
| 1269 |
if ( !BER_BVISNULL( &at->bam_join_where ) |
| 1270 |
&& strstr( bsi->bsi_join_where.bb_val.bv_val, |
| 1271 |
at->bam_join_where.bv_val ) == NULL ) |
| 1272 |
{ |
| 1273 |
backsql_strfcat_x( &bsi->bsi_join_where, |
| 1274 |
bsi->bsi_op->o_tmpmemctx, |
| 1275 |
"lb", |
| 1276 |
(ber_len_t)STRLENOF( " AND " ), " AND ", |
| 1277 |
&at->bam_join_where ); |
| 1278 |
} |
| 1279 |
|
| 1280 |
if ( f->f_choice & SLAPD_FILTER_UNDEFINED ) { |
| 1281 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1282 |
bsi->bsi_op->o_tmpmemctx, |
| 1283 |
"l", |
| 1284 |
(ber_len_t)STRLENOF( "1=0" ), "1=0" ); |
| 1285 |
return 1; |
| 1286 |
} |
| 1287 |
|
| 1288 |
switch ( f->f_choice ) { |
| 1289 |
case LDAP_FILTER_EQUALITY: |
| 1290 |
filter_value = &f->f_av_value; |
| 1291 |
matching_rule = at->bam_ad->ad_type->sat_equality; |
| 1292 |
|
| 1293 |
goto equality_match; |
| 1294 |
|
| 1295 |
/* fail over into next case */ |
| 1296 |
|
| 1297 |
case LDAP_FILTER_EXT: |
| 1298 |
filter_value = &f->f_mra->ma_value; |
| 1299 |
matching_rule = f->f_mr_rule; |
| 1300 |
|
| 1301 |
equality_match:; |
| 1302 |
/* always uppercase strings by now */ |
| 1303 |
#ifdef BACKSQL_UPPERCASE_FILTER |
| 1304 |
if ( SLAP_MR_ASSOCIATED( matching_rule, |
| 1305 |
bi->sql_caseIgnoreMatch ) ) |
| 1306 |
#endif /* BACKSQL_UPPERCASE_FILTER */ |
| 1307 |
{ |
| 1308 |
casefold = 1; |
| 1309 |
} |
| 1310 |
|
| 1311 |
escaped = backsql_val_escape( bsi->bsi_op, filter_value, &escval ); |
| 1312 |
if ( escaped ) |
| 1313 |
filter_value = &escval; |
| 1314 |
|
| 1315 |
/* FIXME: directoryString filtering should use a similar |
| 1316 |
* approach to deal with non-prettified values like |
| 1317 |
* " A non prettified value ", by using a LIKE |
| 1318 |
* filter with all whitespaces collapsed to a single '%' */ |
| 1319 |
if ( SLAP_MR_ASSOCIATED( matching_rule, |
| 1320 |
bi->sql_telephoneNumberMatch ) ) |
| 1321 |
{ |
| 1322 |
struct berval bv; |
| 1323 |
ber_len_t i; |
| 1324 |
|
| 1325 |
/* |
| 1326 |
* to check for matching telephone numbers |
| 1327 |
* with intermized chars, e.g. val='1234' |
| 1328 |
* use |
| 1329 |
* |
| 1330 |
* val LIKE '%1%2%3%4%' |
| 1331 |
*/ |
| 1332 |
|
| 1333 |
bv.bv_len = 2 * filter_value->bv_len - 1; |
| 1334 |
bv.bv_val = ch_malloc( bv.bv_len + 1 ); |
| 1335 |
|
| 1336 |
bv.bv_val[ 0 ] = filter_value->bv_val[ 0 ]; |
| 1337 |
for ( i = 1; i < filter_value->bv_len; i++ ) { |
| 1338 |
bv.bv_val[ 2 * i - 1 ] = '%'; |
| 1339 |
bv.bv_val[ 2 * i ] = filter_value->bv_val[ i ]; |
| 1340 |
} |
| 1341 |
bv.bv_val[ 2 * i - 1 ] = '\0'; |
| 1342 |
|
| 1343 |
(void)backsql_process_filter_like( bsi, at, casefold, &bv ); |
| 1344 |
ch_free( bv.bv_val ); |
| 1345 |
|
| 1346 |
break; |
| 1347 |
} |
| 1348 |
|
| 1349 |
/* NOTE: this is required by objectClass inheritance |
| 1350 |
* and auxiliary objectClass use in filters for slightly |
| 1351 |
* more efficient candidate selection. */ |
| 1352 |
/* FIXME: a bit too many specializations to deal with |
| 1353 |
* very specific cases... */ |
| 1354 |
if ( at->bam_ad == slap_schema.si_ad_objectClass |
| 1355 |
|| at->bam_ad == slap_schema.si_ad_structuralObjectClass ) |
| 1356 |
{ |
| 1357 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1358 |
bsi->bsi_op->o_tmpmemctx, |
| 1359 |
"lbl", |
| 1360 |
(ber_len_t)STRLENOF( "(ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='" /* ') */ ), |
| 1361 |
"(ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='" /* ') */, |
| 1362 |
filter_value, |
| 1363 |
(ber_len_t)STRLENOF( /* (' */ "')" ), |
| 1364 |
/* (' */ "')" ); |
| 1365 |
break; |
| 1366 |
} |
| 1367 |
|
| 1368 |
/* |
| 1369 |
* maybe we should check type of at->sel_expr here somehow, |
| 1370 |
* to know whether upper_func is applicable, but for now |
| 1371 |
* upper_func stuff is made for Oracle, where UPPER is |
| 1372 |
* safely applicable to NUMBER etc. |
| 1373 |
*/ |
| 1374 |
(void)backsql_process_filter_eq( bsi, at, casefold, filter_value ); |
| 1375 |
break; |
| 1376 |
|
| 1377 |
case LDAP_FILTER_GE: |
| 1378 |
ordering.bv_val = ">="; |
| 1379 |
|
| 1380 |
/* fall thru to next case */ |
| 1381 |
|
| 1382 |
case LDAP_FILTER_LE: |
| 1383 |
filter_value = &f->f_av_value; |
| 1384 |
|
| 1385 |
/* always uppercase strings by now */ |
| 1386 |
#ifdef BACKSQL_UPPERCASE_FILTER |
| 1387 |
if ( at->bam_ad->ad_type->sat_ordering && |
| 1388 |
SLAP_MR_ASSOCIATED( at->bam_ad->ad_type->sat_ordering, |
| 1389 |
bi->sql_caseIgnoreMatch ) ) |
| 1390 |
#endif /* BACKSQL_UPPERCASE_FILTER */ |
| 1391 |
{ |
| 1392 |
casefold = 1; |
| 1393 |
} |
| 1394 |
|
| 1395 |
escaped = backsql_val_escape( bsi->bsi_op, filter_value, &escval ); |
| 1396 |
if ( escaped ) |
| 1397 |
filter_value = &escval; |
| 1398 |
|
| 1399 |
/* |
| 1400 |
* FIXME: should we uppercase the operands? |
| 1401 |
*/ |
| 1402 |
if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) { |
| 1403 |
ber_len_t start; |
| 1404 |
|
| 1405 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1406 |
bsi->bsi_op->o_tmpmemctx, |
| 1407 |
"cbbc", |
| 1408 |
'(', /* ) */ |
| 1409 |
&at->bam_sel_expr_u, |
| 1410 |
&ordering, |
| 1411 |
'\'' ); |
| 1412 |
|
| 1413 |
start = bsi->bsi_flt_where.bb_val.bv_len; |
| 1414 |
|
| 1415 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1416 |
bsi->bsi_op->o_tmpmemctx, |
| 1417 |
"bl", |
| 1418 |
filter_value, |
| 1419 |
(ber_len_t)STRLENOF( /* (' */ "')" ), |
| 1420 |
/* (' */ "')" ); |
| 1421 |
|
| 1422 |
ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] ); |
| 1423 |
|
| 1424 |
} else { |
| 1425 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1426 |
bsi->bsi_op->o_tmpmemctx, |
| 1427 |
"cbbcbl", |
| 1428 |
'(' /* ) */ , |
| 1429 |
&at->bam_sel_expr, |
| 1430 |
&ordering, |
| 1431 |
'\'', |
| 1432 |
filter_value, |
| 1433 |
(ber_len_t)STRLENOF( /* (' */ "')" ), |
| 1434 |
/* ( */ "')" ); |
| 1435 |
} |
| 1436 |
break; |
| 1437 |
|
| 1438 |
case LDAP_FILTER_PRESENT: |
| 1439 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1440 |
bsi->bsi_op->o_tmpmemctx, |
| 1441 |
"lbl", |
| 1442 |
(ber_len_t)STRLENOF( "NOT (" /* ) */), |
| 1443 |
"NOT (", /* ) */ |
| 1444 |
&at->bam_sel_expr, |
| 1445 |
(ber_len_t)STRLENOF( /* ( */ " IS NULL)" ), |
| 1446 |
/* ( */ " IS NULL)" ); |
| 1447 |
break; |
| 1448 |
|
| 1449 |
case LDAP_FILTER_SUBSTRINGS: |
| 1450 |
backsql_process_sub_filter( bsi, f, at ); |
| 1451 |
break; |
| 1452 |
|
| 1453 |
case LDAP_FILTER_APPROX: |
| 1454 |
/* we do our best */ |
| 1455 |
|
| 1456 |
filter_value = &f->f_av_value; |
| 1457 |
escaped = backsql_val_escape( bsi->bsi_op, filter_value, &escval ); |
| 1458 |
if ( escaped ) |
| 1459 |
filter_value = &escval; |
| 1460 |
/* |
| 1461 |
* maybe we should check type of at->sel_expr here somehow, |
| 1462 |
* to know whether upper_func is applicable, but for now |
| 1463 |
* upper_func stuff is made for Oracle, where UPPER is |
| 1464 |
* safely applicable to NUMBER etc. |
| 1465 |
*/ |
| 1466 |
(void)backsql_process_filter_like( bsi, at, 1, filter_value ); |
| 1467 |
break; |
| 1468 |
|
| 1469 |
default: |
| 1470 |
/* unhandled filter type; should not happen */ |
| 1471 |
assert( 0 ); |
| 1472 |
backsql_strfcat_x( &bsi->bsi_flt_where, |
| 1473 |
bsi->bsi_op->o_tmpmemctx, |
| 1474 |
"l", |
| 1475 |
(ber_len_t)STRLENOF( "8=8" ), "8=8" ); |
| 1476 |
break; |
| 1477 |
|
| 1478 |
} |
| 1479 |
|
| 1480 |
if ( escaped ) |
| 1481 |
bsi->bsi_op->o_tmpfree( escval.bv_val, bsi->bsi_op->o_tmpmemctx ); |
| 1482 |
|
| 1483 |
Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter_attr(%s)\n", |
| 1484 |
at->bam_ad->ad_cname.bv_val ); |
| 1485 |
|
| 1486 |
return 1; |
| 1487 |
} |
| 1488 |
|
| 1489 |
static int |
| 1490 |
backsql_srch_query( backsql_srch_info *bsi, struct berval *query ) |
| 1491 |
{ |
| 1492 |
backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private; |
| 1493 |
int rc; |
| 1494 |
|
| 1495 |
assert( query != NULL ); |
| 1496 |
BER_BVZERO( query ); |
| 1497 |
|
| 1498 |
bsi->bsi_use_subtree_shortcut = 0; |
| 1499 |
|
| 1500 |
Debug( LDAP_DEBUG_TRACE, "==>backsql_srch_query()\n" ); |
| 1501 |
BER_BVZERO( &bsi->bsi_sel.bb_val ); |
| 1502 |
BER_BVZERO( &bsi->bsi_sel.bb_val ); |
| 1503 |
bsi->bsi_sel.bb_len = 0; |
| 1504 |
BER_BVZERO( &bsi->bsi_from.bb_val ); |
| 1505 |
bsi->bsi_from.bb_len = 0; |
| 1506 |
BER_BVZERO( &bsi->bsi_join_where.bb_val ); |
| 1507 |
bsi->bsi_join_where.bb_len = 0; |
| 1508 |
BER_BVZERO( &bsi->bsi_flt_where.bb_val ); |
| 1509 |
bsi->bsi_flt_where.bb_len = 0; |
| 1510 |
|
| 1511 |
backsql_strfcat_x( &bsi->bsi_sel, |
| 1512 |
bsi->bsi_op->o_tmpmemctx, |
| 1513 |
"lbcbc", |
| 1514 |
(ber_len_t)STRLENOF( "SELECT DISTINCT ldap_entries.id," ), |
| 1515 |
"SELECT DISTINCT ldap_entries.id,", |
| 1516 |
&bsi->bsi_oc->bom_keytbl, |
| 1517 |
'.', |
| 1518 |
&bsi->bsi_oc->bom_keycol, |
| 1519 |
',' ); |
| 1520 |
|
| 1521 |
if ( !BER_BVISNULL( &bi->sql_strcast_func ) ) { |
| 1522 |
backsql_strfcat_x( &bsi->bsi_sel, |
| 1523 |
bsi->bsi_op->o_tmpmemctx, |
| 1524 |
"blbl", |
| 1525 |
&bi->sql_strcast_func, |
| 1526 |
(ber_len_t)STRLENOF( "('" /* ') */ ), |
| 1527 |
"('" /* ') */ , |
| 1528 |
&bsi->bsi_oc->bom_oc->soc_cname, |
| 1529 |
(ber_len_t)STRLENOF( /* (' */ "')" ), |
| 1530 |
/* (' */ "')" ); |
| 1531 |
} else { |
| 1532 |
backsql_strfcat_x( &bsi->bsi_sel, |
| 1533 |
bsi->bsi_op->o_tmpmemctx, |
| 1534 |
"cbc", |
| 1535 |
'\'', |
| 1536 |
&bsi->bsi_oc->bom_oc->soc_cname, |
| 1537 |
'\'' ); |
| 1538 |
} |
| 1539 |
|
| 1540 |
backsql_strfcat_x( &bsi->bsi_sel, |
| 1541 |
bsi->bsi_op->o_tmpmemctx, |
| 1542 |
"b", |
| 1543 |
&bi->sql_dn_oc_aliasing ); |
| 1544 |
backsql_strfcat_x( &bsi->bsi_from, |
| 1545 |
bsi->bsi_op->o_tmpmemctx, |
| 1546 |
"lb", |
| 1547 |
(ber_len_t)STRLENOF( " FROM ldap_entries," ), |
| 1548 |
" FROM ldap_entries,", |
| 1549 |
&bsi->bsi_oc->bom_keytbl ); |
| 1550 |
|
| 1551 |
backsql_strfcat_x( &bsi->bsi_join_where, |
| 1552 |
bsi->bsi_op->o_tmpmemctx, |
| 1553 |
"lbcbl", |
| 1554 |
(ber_len_t)STRLENOF( " WHERE " ), " WHERE ", |
| 1555 |
&bsi->bsi_oc->bom_keytbl, |
| 1556 |
'.', |
| 1557 |
&bsi->bsi_oc->bom_keycol, |
| 1558 |
(ber_len_t)STRLENOF( "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " ), |
| 1559 |
"=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " ); |
| 1560 |
|
| 1561 |
switch ( bsi->bsi_scope ) { |
| 1562 |
case LDAP_SCOPE_BASE: |
| 1563 |
if ( BACKSQL_CANUPPERCASE( bi ) ) { |
| 1564 |
backsql_strfcat_x( &bsi->bsi_join_where, |
| 1565 |
bsi->bsi_op->o_tmpmemctx, |
| 1566 |
"bl", |
| 1567 |
&bi->sql_upper_func, |
| 1568 |
(ber_len_t)STRLENOF( "(ldap_entries.dn)=?" ), |
| 1569 |
"(ldap_entries.dn)=?" ); |
| 1570 |
} else { |
| 1571 |
backsql_strfcat_x( &bsi->bsi_join_where, |
| 1572 |
bsi->bsi_op->o_tmpmemctx, |
| 1573 |
"l", |
| 1574 |
(ber_len_t)STRLENOF( "ldap_entries.dn=?" ), |
| 1575 |
"ldap_entries.dn=?" ); |
| 1576 |
} |
| 1577 |
break; |
| 1578 |
|
| 1579 |
case BACKSQL_SCOPE_BASE_LIKE: |
| 1580 |
if ( BACKSQL_CANUPPERCASE( bi ) ) { |
| 1581 |
backsql_strfcat_x( &bsi->bsi_join_where, |
| 1582 |
bsi->bsi_op->o_tmpmemctx, |
| 1583 |
"bl", |
| 1584 |
&bi->sql_upper_func, |
| 1585 |
(ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE ?" ), |
| 1586 |
"(ldap_entries.dn) LIKE ?" ); |
| 1587 |
} else { |
| 1588 |
backsql_strfcat_x( &bsi->bsi_join_where, |
| 1589 |
bsi->bsi_op->o_tmpmemctx, |
| 1590 |
"l", |
| 1591 |
(ber_len_t)STRLENOF( "ldap_entries.dn LIKE ?" ), |
| 1592 |
"ldap_entries.dn LIKE ?" ); |
| 1593 |
} |
| 1594 |
break; |
| 1595 |
|
| 1596 |
case LDAP_SCOPE_ONELEVEL: |
| 1597 |
backsql_strfcat_x( &bsi->bsi_join_where, |
| 1598 |
bsi->bsi_op->o_tmpmemctx, |
| 1599 |
"l", |
| 1600 |
(ber_len_t)STRLENOF( "ldap_entries.parent=?" ), |
| 1601 |
"ldap_entries.parent=?" ); |
| 1602 |
break; |
| 1603 |
|
| 1604 |
case LDAP_SCOPE_SUBORDINATE: |
| 1605 |
case LDAP_SCOPE_SUBTREE: |
| 1606 |
if ( BACKSQL_USE_SUBTREE_SHORTCUT( bi ) ) { |
| 1607 |
int i; |
| 1608 |
BackendDB *bd = bsi->bsi_op->o_bd; |
| 1609 |
|
| 1610 |
assert( bd->be_nsuffix != NULL ); |
| 1611 |
|
| 1612 |
for ( i = 0; !BER_BVISNULL( &bd->be_nsuffix[ i ] ); i++ ) |
| 1613 |
{ |
| 1614 |
if ( dn_match( &bd->be_nsuffix[ i ], |
| 1615 |
bsi->bsi_base_ndn ) ) |
| 1616 |
{ |
| 1617 |
/* pass this to the candidate selection |
| 1618 |
* routine so that the DN is not bound |
| 1619 |
* to the select statement */ |
| 1620 |
bsi->bsi_use_subtree_shortcut = 1; |
| 1621 |
break; |
| 1622 |
} |
| 1623 |
} |
| 1624 |
} |
| 1625 |
|
| 1626 |
if ( bsi->bsi_use_subtree_shortcut ) { |
| 1627 |
/* Skip the base DN filter, as every entry will match it */ |
| 1628 |
backsql_strfcat_x( &bsi->bsi_join_where, |
| 1629 |
bsi->bsi_op->o_tmpmemctx, |
| 1630 |
"l", |
| 1631 |
(ber_len_t)STRLENOF( "9=9"), "9=9"); |
| 1632 |
|
| 1633 |
} else if ( !BER_BVISNULL( &bi->sql_subtree_cond ) ) { |
| 1634 |
/* This should always be true... */ |
| 1635 |
backsql_strfcat_x( &bsi->bsi_join_where, |
| 1636 |
bsi->bsi_op->o_tmpmemctx, |
| 1637 |
"b", |
| 1638 |
&bi->sql_subtree_cond ); |
| 1639 |
|
| 1640 |
} else if ( BACKSQL_CANUPPERCASE( bi ) ) { |
| 1641 |
backsql_strfcat_x( &bsi->bsi_join_where, |
| 1642 |
bsi->bsi_op->o_tmpmemctx, |
| 1643 |
"bl", |
| 1644 |
&bi->sql_upper_func, |
| 1645 |
(ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE ?" ), |
| 1646 |
"(ldap_entries.dn) LIKE ?" ); |
| 1647 |
|
| 1648 |
} else { |
| 1649 |
backsql_strfcat_x( &bsi->bsi_join_where, |
| 1650 |
bsi->bsi_op->o_tmpmemctx, |
| 1651 |
"l", |
| 1652 |
(ber_len_t)STRLENOF( "ldap_entries.dn LIKE ?" ), |
| 1653 |
"ldap_entries.dn LIKE ?" ); |
| 1654 |
} |
| 1655 |
|
| 1656 |
break; |
| 1657 |
|
| 1658 |
default: |
| 1659 |
assert( 0 ); |
| 1660 |
} |
| 1661 |
|
| 1662 |
#ifndef BACKSQL_ARBITRARY_KEY |
| 1663 |
/* If paged results are in effect, ignore low ldap_entries.id numbers */ |
| 1664 |
if ( get_pagedresults(bsi->bsi_op) > SLAP_CONTROL_IGNORED ) { |
| 1665 |
unsigned long lowid = 0; |
| 1666 |
|
| 1667 |
/* Pick up the previous ldap_entries.id if the previous page ended in this objectClass */ |
| 1668 |
if ( bsi->bsi_oc->bom_id == PAGECOOKIE_TO_SQL_OC( ((PagedResultsState *)bsi->bsi_op->o_pagedresults_state)->ps_cookie ) ) |
| 1669 |
{ |
| 1670 |
lowid = PAGECOOKIE_TO_SQL_ID( ((PagedResultsState *)bsi->bsi_op->o_pagedresults_state)->ps_cookie ); |
| 1671 |
} |
| 1672 |
|
| 1673 |
if ( lowid ) { |
| 1674 |
char lowidstring[48]; |
| 1675 |
int lowidlen; |
| 1676 |
|
| 1677 |
lowidlen = snprintf( lowidstring, sizeof( lowidstring ), |
| 1678 |
" AND ldap_entries.id>%lu", lowid ); |
| 1679 |
backsql_strfcat_x( &bsi->bsi_join_where, |
| 1680 |
bsi->bsi_op->o_tmpmemctx, |
| 1681 |
"l", |
| 1682 |
(ber_len_t)lowidlen, |
| 1683 |
lowidstring ); |
| 1684 |
} |
| 1685 |
} |
| 1686 |
#endif /* ! BACKSQL_ARBITRARY_KEY */ |
| 1687 |
|
| 1688 |
rc = backsql_process_filter( bsi, bsi->bsi_filter ); |
| 1689 |
if ( rc > 0 ) { |
| 1690 |
struct berbuf bb = BB_NULL; |
| 1691 |
|
| 1692 |
backsql_strfcat_x( &bb, |
| 1693 |
bsi->bsi_op->o_tmpmemctx, |
| 1694 |
"bbblb", |
| 1695 |
&bsi->bsi_sel.bb_val, |
| 1696 |
&bsi->bsi_from.bb_val, |
| 1697 |
&bsi->bsi_join_where.bb_val, |
| 1698 |
(ber_len_t)STRLENOF( " AND " ), " AND ", |
| 1699 |
&bsi->bsi_flt_where.bb_val ); |
| 1700 |
|
| 1701 |
*query = bb.bb_val; |
| 1702 |
|
| 1703 |
} else if ( rc < 0 ) { |
| 1704 |
/* |
| 1705 |
* Indicates that there's no possible way the filter matches |
| 1706 |
* anything. No need to issue the query |
| 1707 |
*/ |
| 1708 |
free( query->bv_val ); |
| 1709 |
BER_BVZERO( query ); |
| 1710 |
} |
| 1711 |
|
| 1712 |
bsi->bsi_op->o_tmpfree( bsi->bsi_sel.bb_val.bv_val, bsi->bsi_op->o_tmpmemctx ); |
| 1713 |
BER_BVZERO( &bsi->bsi_sel.bb_val ); |
| 1714 |
bsi->bsi_sel.bb_len = 0; |
| 1715 |
bsi->bsi_op->o_tmpfree( bsi->bsi_from.bb_val.bv_val, bsi->bsi_op->o_tmpmemctx ); |
| 1716 |
BER_BVZERO( &bsi->bsi_from.bb_val ); |
| 1717 |
bsi->bsi_from.bb_len = 0; |
| 1718 |
bsi->bsi_op->o_tmpfree( bsi->bsi_join_where.bb_val.bv_val, bsi->bsi_op->o_tmpmemctx ); |
| 1719 |
BER_BVZERO( &bsi->bsi_join_where.bb_val ); |
| 1720 |
bsi->bsi_join_where.bb_len = 0; |
| 1721 |
bsi->bsi_op->o_tmpfree( bsi->bsi_flt_where.bb_val.bv_val, bsi->bsi_op->o_tmpmemctx ); |
| 1722 |
BER_BVZERO( &bsi->bsi_flt_where.bb_val ); |
| 1723 |
bsi->bsi_flt_where.bb_len = 0; |
| 1724 |
|
| 1725 |
Debug( LDAP_DEBUG_TRACE, "<==backsql_srch_query() returns %s\n", |
| 1726 |
query->bv_val ? query->bv_val : "NULL" ); |
| 1727 |
|
| 1728 |
return ( rc <= 0 ? 1 : 0 ); |
| 1729 |
} |
| 1730 |
|
| 1731 |
static int |
| 1732 |
backsql_oc_get_candidates( void *v_oc, void *v_bsi ) |
| 1733 |
{ |
| 1734 |
backsql_oc_map_rec *oc = v_oc; |
| 1735 |
backsql_srch_info *bsi = v_bsi; |
| 1736 |
Operation *op = bsi->bsi_op; |
| 1737 |
backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private; |
| 1738 |
struct berval query; |
| 1739 |
SQLHSTMT sth = SQL_NULL_HSTMT; |
| 1740 |
RETCODE rc; |
| 1741 |
int res; |
| 1742 |
BACKSQL_ROW_NTS row; |
| 1743 |
int i; |
| 1744 |
int j; |
| 1745 |
int n_candidates = bsi->bsi_n_candidates; |
| 1746 |
|
| 1747 |
/* |
| 1748 |
* + 1 because we need room for '%'; |
| 1749 |
* + 1 because we need room for ',' for LDAP_SCOPE_SUBORDINATE; |
| 1750 |
* this makes a subtree |
| 1751 |
* search for a DN BACKSQL_MAX_DN_LEN long legal |
| 1752 |
* if it returns that DN only |
| 1753 |
*/ |
| 1754 |
char tmp_base_ndn[ BACKSQL_MAX_DN_LEN + 1 + 1 ]; |
| 1755 |
|
| 1756 |
bsi->bsi_status = LDAP_SUCCESS; |
| 1757 |
|
| 1758 |
Debug( LDAP_DEBUG_TRACE, "==>backsql_oc_get_candidates(): oc=\"%s\"\n", |
| 1759 |
BACKSQL_OC_NAME( oc ) ); |
| 1760 |
|
| 1761 |
/* check for abandon */ |
| 1762 |
if ( op->o_abandon ) { |
| 1763 |
bsi->bsi_status = SLAPD_ABANDON; |
| 1764 |
return BACKSQL_AVL_STOP; |
| 1765 |
} |
| 1766 |
|
| 1767 |
#ifndef BACKSQL_ARBITRARY_KEY |
| 1768 |
/* If paged results have already completed this objectClass, skip it */ |
| 1769 |
if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) { |
| 1770 |
if ( oc->bom_id < PAGECOOKIE_TO_SQL_OC( ((PagedResultsState *)op->o_pagedresults_state)->ps_cookie ) ) |
| 1771 |
{ |
| 1772 |
return BACKSQL_AVL_CONTINUE; |
| 1773 |
} |
| 1774 |
} |
| 1775 |
#endif /* ! BACKSQL_ARBITRARY_KEY */ |
| 1776 |
|
| 1777 |
if ( bsi->bsi_n_candidates == -1 ) { |
| 1778 |
Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): " |
| 1779 |
"unchecked limit has been overcome\n" ); |
| 1780 |
/* should never get here */ |
| 1781 |
assert( 0 ); |
| 1782 |
bsi->bsi_status = LDAP_ADMINLIMIT_EXCEEDED; |
| 1783 |
return BACKSQL_AVL_STOP; |
| 1784 |
} |
| 1785 |
|
| 1786 |
bsi->bsi_oc = oc; |
| 1787 |
res = backsql_srch_query( bsi, &query ); |
| 1788 |
if ( res ) { |
| 1789 |
Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): " |
| 1790 |
"error while constructing query for objectclass \"%s\"\n", |
| 1791 |
oc->bom_oc->soc_cname.bv_val ); |
| 1792 |
/* |
| 1793 |
* FIXME: need to separate errors from legally |
| 1794 |
* impossible filters |
| 1795 |
*/ |
| 1796 |
switch ( bsi->bsi_status ) { |
| 1797 |
case LDAP_SUCCESS: |
| 1798 |
case LDAP_UNDEFINED_TYPE: |
| 1799 |
case LDAP_NO_SUCH_OBJECT: |
| 1800 |
/* we are conservative... */ |
| 1801 |
default: |
| 1802 |
bsi->bsi_status = LDAP_SUCCESS; |
| 1803 |
/* try next */ |
| 1804 |
return BACKSQL_AVL_CONTINUE; |
| 1805 |
|
| 1806 |
case LDAP_ADMINLIMIT_EXCEEDED: |
| 1807 |
case LDAP_OTHER: |
| 1808 |
/* don't try any more */ |
| 1809 |
return BACKSQL_AVL_STOP; |
| 1810 |
} |
| 1811 |
} |
| 1812 |
|
| 1813 |
if ( BER_BVISNULL( &query ) ) { |
| 1814 |
Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): " |
| 1815 |
"could not construct query for objectclass \"%s\"\n", |
| 1816 |
oc->bom_oc->soc_cname.bv_val ); |
| 1817 |
bsi->bsi_status = LDAP_SUCCESS; |
| 1818 |
return BACKSQL_AVL_CONTINUE; |
| 1819 |
} |
| 1820 |
|
| 1821 |
Debug( LDAP_DEBUG_TRACE, "Constructed query: %s\n", |
| 1822 |
query.bv_val ); |
| 1823 |
|
| 1824 |
rc = backsql_Prepare( bsi->bsi_dbh, &sth, query.bv_val, 0 ); |
| 1825 |
bsi->bsi_op->o_tmpfree( query.bv_val, bsi->bsi_op->o_tmpmemctx ); |
| 1826 |
BER_BVZERO( &query ); |
| 1827 |
if ( rc != SQL_SUCCESS ) { |
| 1828 |
Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): " |
| 1829 |
"error preparing query\n" ); |
| 1830 |
backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc ); |
| 1831 |
bsi->bsi_status = LDAP_OTHER; |
| 1832 |
return BACKSQL_AVL_CONTINUE; |
| 1833 |
} |
| 1834 |
|
| 1835 |
Debug( LDAP_DEBUG_TRACE, "id: '" BACKSQL_IDNUMFMT "'\n", |
| 1836 |
bsi->bsi_oc->bom_id ); |
| 1837 |
|
| 1838 |
rc = backsql_BindParamNumID( sth, 1, SQL_PARAM_INPUT, |
| 1839 |
&bsi->bsi_oc->bom_id ); |
| 1840 |
if ( rc != SQL_SUCCESS ) { |
| 1841 |
Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): " |
| 1842 |
"error binding objectclass id parameter\n" ); |
| 1843 |
bsi->bsi_status = LDAP_OTHER; |
| 1844 |
return BACKSQL_AVL_CONTINUE; |
| 1845 |
} |
| 1846 |
|
| 1847 |
switch ( bsi->bsi_scope ) { |
| 1848 |
case LDAP_SCOPE_BASE: |
| 1849 |
case BACKSQL_SCOPE_BASE_LIKE: |
| 1850 |
/* |
| 1851 |
* We do not accept DNs longer than BACKSQL_MAX_DN_LEN; |
| 1852 |
* however this should be handled earlier |
| 1853 |
*/ |
| 1854 |
if ( bsi->bsi_base_ndn->bv_len > BACKSQL_MAX_DN_LEN ) { |
| 1855 |
bsi->bsi_status = LDAP_OTHER; |
| 1856 |
return BACKSQL_AVL_CONTINUE; |
| 1857 |
} |
| 1858 |
|
| 1859 |
AC_MEMCPY( tmp_base_ndn, bsi->bsi_base_ndn->bv_val, |
| 1860 |
bsi->bsi_base_ndn->bv_len + 1 ); |
| 1861 |
|
| 1862 |
/* uppercase DN only if the stored DN can be uppercased |
| 1863 |
* for comparison */ |
| 1864 |
if ( BACKSQL_CANUPPERCASE( bi ) ) { |
| 1865 |
ldap_pvt_str2upper( tmp_base_ndn ); |
| 1866 |
} |
| 1867 |
|
| 1868 |
Debug( LDAP_DEBUG_TRACE, "(base)dn: \"%s\"\n", |
| 1869 |
tmp_base_ndn ); |
| 1870 |
|
| 1871 |
rc = backsql_BindParamStr( sth, 2, SQL_PARAM_INPUT, |
| 1872 |
tmp_base_ndn, BACKSQL_MAX_DN_LEN ); |
| 1873 |
if ( rc != SQL_SUCCESS ) { |
| 1874 |
Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): " |
| 1875 |
"error binding base_ndn parameter\n" ); |
| 1876 |
backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, |
| 1877 |
sth, rc ); |
| 1878 |
bsi->bsi_status = LDAP_OTHER; |
| 1879 |
return BACKSQL_AVL_CONTINUE; |
| 1880 |
} |
| 1881 |
break; |
| 1882 |
|
| 1883 |
case LDAP_SCOPE_SUBORDINATE: |
| 1884 |
case LDAP_SCOPE_SUBTREE: |
| 1885 |
{ |
| 1886 |
/* if short-cutting the search base, |
| 1887 |
* don't bind any parameter */ |
| 1888 |
if ( bsi->bsi_use_subtree_shortcut ) { |
| 1889 |
break; |
| 1890 |
} |
| 1891 |
|
| 1892 |
/* |
| 1893 |
* We do not accept DNs longer than BACKSQL_MAX_DN_LEN; |
| 1894 |
* however this should be handled earlier |
| 1895 |
*/ |
| 1896 |
if ( bsi->bsi_base_ndn->bv_len > BACKSQL_MAX_DN_LEN ) { |
| 1897 |
bsi->bsi_status = LDAP_OTHER; |
| 1898 |
return BACKSQL_AVL_CONTINUE; |
| 1899 |
} |
| 1900 |
|
| 1901 |
/* |
| 1902 |
* Sets the parameters for the SQL built earlier |
| 1903 |
* NOTE that all the databases could actually use |
| 1904 |
* the TimesTen version, which would be cleaner |
| 1905 |
* and would also eliminate the need for the |
| 1906 |
* subtree_cond line in the configuration file. |
| 1907 |
* For now, I'm leaving it the way it is, |
| 1908 |
* so non-TimesTen databases use the original code. |
| 1909 |
* But at some point this should get cleaned up. |
| 1910 |
* |
| 1911 |
* If "dn" is being used, do a suffix search. |
| 1912 |
* If "dn_ru" is being used, do a prefix search. |
| 1913 |
*/ |
| 1914 |
if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) { |
| 1915 |
tmp_base_ndn[ 0 ] = '\0'; |
| 1916 |
|
| 1917 |
for ( i = 0, j = bsi->bsi_base_ndn->bv_len - 1; |
| 1918 |
j >= 0; i++, j--) { |
| 1919 |
tmp_base_ndn[ i ] = bsi->bsi_base_ndn->bv_val[ j ]; |
| 1920 |
} |
| 1921 |
|
| 1922 |
if ( bsi->bsi_scope == LDAP_SCOPE_SUBORDINATE ) { |
| 1923 |
tmp_base_ndn[ i++ ] = ','; |
| 1924 |
} |
| 1925 |
|
| 1926 |
tmp_base_ndn[ i ] = '%'; |
| 1927 |
tmp_base_ndn[ i + 1 ] = '\0'; |
| 1928 |
|
| 1929 |
} else { |
| 1930 |
i = 0; |
| 1931 |
|
| 1932 |
tmp_base_ndn[ i++ ] = '%'; |
| 1933 |
|
| 1934 |
if ( bsi->bsi_scope == LDAP_SCOPE_SUBORDINATE ) { |
| 1935 |
tmp_base_ndn[ i++ ] = ','; |
| 1936 |
} |
| 1937 |
|
| 1938 |
AC_MEMCPY( &tmp_base_ndn[ i ], bsi->bsi_base_ndn->bv_val, |
| 1939 |
bsi->bsi_base_ndn->bv_len + 1 ); |
| 1940 |
} |
| 1941 |
|
| 1942 |
/* uppercase DN only if the stored DN can be uppercased |
| 1943 |
* for comparison */ |
| 1944 |
if ( BACKSQL_CANUPPERCASE( bi ) ) { |
| 1945 |
ldap_pvt_str2upper( tmp_base_ndn ); |
| 1946 |
} |
| 1947 |
|
| 1948 |
if ( bsi->bsi_scope == LDAP_SCOPE_SUBORDINATE ) { |
| 1949 |
Debug( LDAP_DEBUG_TRACE, "(children)dn: \"%s\"\n", |
| 1950 |
tmp_base_ndn ); |
| 1951 |
} else { |
| 1952 |
Debug( LDAP_DEBUG_TRACE, "(sub)dn: \"%s\"\n", |
| 1953 |
tmp_base_ndn ); |
| 1954 |
} |
| 1955 |
|
| 1956 |
rc = backsql_BindParamStr( sth, 2, SQL_PARAM_INPUT, |
| 1957 |
tmp_base_ndn, BACKSQL_MAX_DN_LEN ); |
| 1958 |
if ( rc != SQL_SUCCESS ) { |
| 1959 |
Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): " |
| 1960 |
"error binding base_ndn parameter (2)\n" ); |
| 1961 |
backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, |
| 1962 |
sth, rc ); |
| 1963 |
bsi->bsi_status = LDAP_OTHER; |
| 1964 |
return BACKSQL_AVL_CONTINUE; |
| 1965 |
} |
| 1966 |
break; |
| 1967 |
} |
| 1968 |
|
| 1969 |
case LDAP_SCOPE_ONELEVEL: |
| 1970 |
assert( !BER_BVISNULL( &bsi->bsi_base_id.eid_ndn ) ); |
| 1971 |
|
| 1972 |
Debug( LDAP_DEBUG_TRACE, "(one)id=" BACKSQL_IDFMT "\n", |
| 1973 |
BACKSQL_IDARG(bsi->bsi_base_id.eid_id) ); |
| 1974 |
rc = backsql_BindParamID( sth, 2, SQL_PARAM_INPUT, |
| 1975 |
&bsi->bsi_base_id.eid_id ); |
| 1976 |
if ( rc != SQL_SUCCESS ) { |
| 1977 |
Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): " |
| 1978 |
"error binding base id parameter\n" ); |
| 1979 |
bsi->bsi_status = LDAP_OTHER; |
| 1980 |
return BACKSQL_AVL_CONTINUE; |
| 1981 |
} |
| 1982 |
break; |
| 1983 |
} |
| 1984 |
|
| 1985 |
rc = SQLExecute( sth ); |
| 1986 |
if ( !BACKSQL_SUCCESS( rc ) ) { |
| 1987 |
Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): " |
| 1988 |
"error executing query\n" ); |
| 1989 |
backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc ); |
| 1990 |
SQLFreeStmt( sth, SQL_DROP ); |
| 1991 |
bsi->bsi_status = LDAP_OTHER; |
| 1992 |
return BACKSQL_AVL_CONTINUE; |
| 1993 |
} |
| 1994 |
|
| 1995 |
backsql_BindRowAsStrings_x( sth, &row, bsi->bsi_op->o_tmpmemctx ); |
| 1996 |
rc = SQLFetch( sth ); |
| 1997 |
for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) { |
| 1998 |
struct berval dn, pdn, ndn; |
| 1999 |
backsql_entryID *c_id = NULL; |
| 2000 |
int ret; |
| 2001 |
|
| 2002 |
ber_str2bv( row.cols[ 3 ], 0, 0, &dn ); |
| 2003 |
|
| 2004 |
if ( backsql_api_odbc2dn( bsi->bsi_op, bsi->bsi_rs, &dn ) ) { |
| 2005 |
continue; |
| 2006 |
} |
| 2007 |
|
| 2008 |
ret = dnPrettyNormal( NULL, &dn, &pdn, &ndn, op->o_tmpmemctx ); |
| 2009 |
if ( dn.bv_val != row.cols[ 3 ] ) { |
| 2010 |
free( dn.bv_val ); |
| 2011 |
} |
| 2012 |
|
| 2013 |
if ( ret != LDAP_SUCCESS ) { |
| 2014 |
continue; |
| 2015 |
} |
| 2016 |
|
| 2017 |
if ( bi->sql_baseObject && dn_match( &ndn, &bi->sql_baseObject->e_nname ) ) { |
| 2018 |
goto cleanup; |
| 2019 |
} |
| 2020 |
|
| 2021 |
c_id = (backsql_entryID *)op->o_tmpcalloc( 1, |
| 2022 |
sizeof( backsql_entryID ), op->o_tmpmemctx ); |
| 2023 |
#ifdef BACKSQL_ARBITRARY_KEY |
| 2024 |
ber_str2bv_x( row.cols[ 0 ], 0, 1, &c_id->eid_id, |
| 2025 |
op->o_tmpmemctx ); |
| 2026 |
ber_str2bv_x( row.cols[ 1 ], 0, 1, &c_id->eid_keyval, |
| 2027 |
op->o_tmpmemctx ); |
| 2028 |
#else /* ! BACKSQL_ARBITRARY_KEY */ |
| 2029 |
if ( BACKSQL_STR2ID( &c_id->eid_id, row.cols[ 0 ], 0 ) != 0 ) { |
| 2030 |
goto cleanup; |
| 2031 |
} |
| 2032 |
if ( BACKSQL_STR2ID( &c_id->eid_keyval, row.cols[ 1 ], 0 ) != 0 ) { |
| 2033 |
goto cleanup; |
| 2034 |
} |
| 2035 |
#endif /* ! BACKSQL_ARBITRARY_KEY */ |
| 2036 |
c_id->eid_oc = bsi->bsi_oc; |
| 2037 |
c_id->eid_oc_id = bsi->bsi_oc->bom_id; |
| 2038 |
|
| 2039 |
c_id->eid_dn = pdn; |
| 2040 |
c_id->eid_ndn = ndn; |
| 2041 |
|
| 2042 |
/* append at end of list ... */ |
| 2043 |
c_id->eid_next = NULL; |
| 2044 |
*bsi->bsi_id_listtail = c_id; |
| 2045 |
bsi->bsi_id_listtail = &c_id->eid_next; |
| 2046 |
|
| 2047 |
Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): " |
| 2048 |
"added entry id=" BACKSQL_IDFMT " keyval=" BACKSQL_IDFMT " dn=\"%s\"\n", |
| 2049 |
BACKSQL_IDARG(c_id->eid_id), |
| 2050 |
BACKSQL_IDARG(c_id->eid_keyval), |
| 2051 |
row.cols[ 3 ] ); |
| 2052 |
|
| 2053 |
/* count candidates, for unchecked limit */ |
| 2054 |
bsi->bsi_n_candidates--; |
| 2055 |
if ( bsi->bsi_n_candidates == -1 ) { |
| 2056 |
break; |
| 2057 |
} |
| 2058 |
continue; |
| 2059 |
|
| 2060 |
cleanup:; |
| 2061 |
if ( !BER_BVISNULL( &pdn ) ) { |
| 2062 |
op->o_tmpfree( pdn.bv_val, op->o_tmpmemctx ); |
| 2063 |
} |
| 2064 |
if ( !BER_BVISNULL( &ndn ) ) { |
| 2065 |
op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx ); |
| 2066 |
} |
| 2067 |
if ( c_id != NULL ) { |
| 2068 |
ch_free( c_id ); |
| 2069 |
} |
| 2070 |
} |
| 2071 |
backsql_FreeRow_x( &row, bsi->bsi_op->o_tmpmemctx ); |
| 2072 |
SQLFreeStmt( sth, SQL_DROP ); |
| 2073 |
|
| 2074 |
Debug( LDAP_DEBUG_TRACE, "<==backsql_oc_get_candidates(): %d\n", |
| 2075 |
n_candidates - bsi->bsi_n_candidates ); |
| 2076 |
|
| 2077 |
return ( bsi->bsi_n_candidates == -1 ? BACKSQL_AVL_STOP : BACKSQL_AVL_CONTINUE ); |
| 2078 |
} |
| 2079 |
|
| 2080 |
int |
| 2081 |
backsql_search( Operation *op, SlapReply *rs ) |
| 2082 |
{ |
| 2083 |
backsql_info *bi = (backsql_info *)op->o_bd->be_private; |
| 2084 |
SQLHDBC dbh = SQL_NULL_HDBC; |
| 2085 |
int sres; |
| 2086 |
Entry user_entry = { 0 }, |
| 2087 |
base_entry = { 0 }; |
| 2088 |
int manageDSAit = get_manageDSAit( op ); |
| 2089 |
time_t stoptime = 0; |
| 2090 |
backsql_srch_info bsi = { 0 }; |
| 2091 |
backsql_entryID *eid = NULL; |
| 2092 |
struct berval nbase = BER_BVNULL; |
| 2093 |
#ifndef BACKSQL_ARBITRARY_KEY |
| 2094 |
ID lastid = 0; |
| 2095 |
#endif /* ! BACKSQL_ARBITRARY_KEY */ |
| 2096 |
|
| 2097 |
Debug( LDAP_DEBUG_TRACE, "==>backsql_search(): " |
| 2098 |
"base=\"%s\", filter=\"%s\", scope=%d,", |
| 2099 |
op->o_req_ndn.bv_val, |
| 2100 |
op->ors_filterstr.bv_val, |
| 2101 |
op->ors_scope ); |
| 2102 |
Debug( LDAP_DEBUG_TRACE, " deref=%d, attrsonly=%d, " |
| 2103 |
"attributes to load: %s\n", |
| 2104 |
op->ors_deref, |
| 2105 |
op->ors_attrsonly, |
| 2106 |
op->ors_attrs == NULL ? "all" : "custom list" ); |
| 2107 |
|
| 2108 |
if ( op->o_req_ndn.bv_len > BACKSQL_MAX_DN_LEN ) { |
| 2109 |
Debug( LDAP_DEBUG_TRACE, "backsql_search(): " |
| 2110 |
"search base length (%ld) exceeds max length (%d)\n", |
| 2111 |
op->o_req_ndn.bv_len, BACKSQL_MAX_DN_LEN ); |
| 2112 |
/* |
| 2113 |
* FIXME: a LDAP_NO_SUCH_OBJECT could be appropriate |
| 2114 |
* since it is impossible that such a long DN exists |
| 2115 |
* in the backend |
| 2116 |
*/ |
| 2117 |
rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED; |
| 2118 |
send_ldap_result( op, rs ); |
| 2119 |
return 1; |
| 2120 |
} |
| 2121 |
|
| 2122 |
sres = backsql_get_db_conn( op, &dbh ); |
| 2123 |
if ( sres != LDAP_SUCCESS ) { |
| 2124 |
Debug( LDAP_DEBUG_TRACE, "backsql_search(): " |
| 2125 |
"could not get connection handle - exiting\n" ); |
| 2126 |
rs->sr_err = sres; |
| 2127 |
rs->sr_text = sres == LDAP_OTHER ? "SQL-backend error" : NULL; |
| 2128 |
send_ldap_result( op, rs ); |
| 2129 |
return 1; |
| 2130 |
} |
| 2131 |
|
| 2132 |
/* compute it anyway; root does not use it */ |
| 2133 |
stoptime = op->o_time + op->ors_tlimit; |
| 2134 |
|
| 2135 |
/* init search */ |
| 2136 |
bsi.bsi_e = &base_entry; |
| 2137 |
rs->sr_err = backsql_init_search( &bsi, &op->o_req_ndn, |
| 2138 |
op->ors_scope, |
| 2139 |
stoptime, op->ors_filter, |
| 2140 |
dbh, op, rs, op->ors_attrs, |
| 2141 |
( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY ) ); |
| 2142 |
switch ( rs->sr_err ) { |
| 2143 |
case LDAP_SUCCESS: |
| 2144 |
break; |
| 2145 |
|
| 2146 |
case LDAP_REFERRAL: |
| 2147 |
if ( manageDSAit && !BER_BVISNULL( &bsi.bsi_e->e_nname ) && |
| 2148 |
dn_match( &op->o_req_ndn, &bsi.bsi_e->e_nname ) ) |
| 2149 |
{ |
| 2150 |
rs->sr_err = LDAP_SUCCESS; |
| 2151 |
rs->sr_text = NULL; |
| 2152 |
rs->sr_matched = NULL; |
| 2153 |
if ( rs->sr_ref ) { |
| 2154 |
ber_bvarray_free( rs->sr_ref ); |
| 2155 |
rs->sr_ref = NULL; |
| 2156 |
} |
| 2157 |
break; |
| 2158 |
} |
| 2159 |
|
| 2160 |
/* an entry was created; free it */ |
| 2161 |
entry_clean( bsi.bsi_e ); |
| 2162 |
|
| 2163 |
/* fall thru */ |
| 2164 |
|
| 2165 |
default: |
| 2166 |
if ( !BER_BVISNULL( &base_entry.e_nname ) |
| 2167 |
&& !access_allowed( op, &base_entry, |
| 2168 |
slap_schema.si_ad_entry, NULL, |
| 2169 |
ACL_DISCLOSE, NULL ) ) |
| 2170 |
{ |
| 2171 |
rs->sr_err = LDAP_NO_SUCH_OBJECT; |
| 2172 |
if ( rs->sr_ref ) { |
| 2173 |
ber_bvarray_free( rs->sr_ref ); |
| 2174 |
rs->sr_ref = NULL; |
| 2175 |
} |
| 2176 |
rs->sr_matched = NULL; |
| 2177 |
rs->sr_text = NULL; |
| 2178 |
} |
| 2179 |
|
| 2180 |
send_ldap_result( op, rs ); |
| 2181 |
|
| 2182 |
if ( rs->sr_ref ) { |
| 2183 |
ber_bvarray_free( rs->sr_ref ); |
| 2184 |
rs->sr_ref = NULL; |
| 2185 |
} |
| 2186 |
|
| 2187 |
if ( !BER_BVISNULL( &base_entry.e_nname ) ) { |
| 2188 |
entry_clean( &base_entry ); |
| 2189 |
} |
| 2190 |
|
| 2191 |
goto done; |
| 2192 |
} |
| 2193 |
/* NOTE: __NEW__ "search" access is required |
| 2194 |
* on searchBase object */ |
| 2195 |
{ |
| 2196 |
slap_mask_t mask; |
| 2197 |
|
| 2198 |
if ( get_assert( op ) && |
| 2199 |
( test_filter( op, &base_entry, get_assertion( op ) ) |
| 2200 |
!= LDAP_COMPARE_TRUE ) ) |
| 2201 |
{ |
| 2202 |
rs->sr_err = LDAP_ASSERTION_FAILED; |
| 2203 |
|
| 2204 |
} |
| 2205 |
if ( ! access_allowed_mask( op, &base_entry, |
| 2206 |
slap_schema.si_ad_entry, |
| 2207 |
NULL, ACL_SEARCH, NULL, &mask ) ) |
| 2208 |
{ |
| 2209 |
if ( rs->sr_err == LDAP_SUCCESS ) { |
| 2210 |
rs->sr_err = LDAP_INSUFFICIENT_ACCESS; |
| 2211 |
} |
| 2212 |
} |
| 2213 |
|
| 2214 |
if ( rs->sr_err != LDAP_SUCCESS ) { |
| 2215 |
if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) { |
| 2216 |
rs->sr_err = LDAP_NO_SUCH_OBJECT; |
| 2217 |
rs->sr_text = NULL; |
| 2218 |
} |
| 2219 |
send_ldap_result( op, rs ); |
| 2220 |
goto done; |
| 2221 |
} |
| 2222 |
} |
| 2223 |
|
| 2224 |
bsi.bsi_e = NULL; |
| 2225 |
|
| 2226 |
bsi.bsi_n_candidates = |
| 2227 |
( op->ors_limit == NULL /* isroot == TRUE */ ? -2 : |
| 2228 |
( op->ors_limit->lms_s_unchecked == -1 ? -2 : |
| 2229 |
( op->ors_limit->lms_s_unchecked ) ) ); |
| 2230 |
|
| 2231 |
#ifndef BACKSQL_ARBITRARY_KEY |
| 2232 |
/* If paged results are in effect, check the paging cookie */ |
| 2233 |
if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) { |
| 2234 |
rs->sr_err = parse_paged_cookie( op, rs ); |
| 2235 |
if ( rs->sr_err != LDAP_SUCCESS ) { |
| 2236 |
send_ldap_result( op, rs ); |
| 2237 |
goto done; |
| 2238 |
} |
| 2239 |
} |
| 2240 |
#endif /* ! BACKSQL_ARBITRARY_KEY */ |
| 2241 |
|
| 2242 |
switch ( bsi.bsi_scope ) { |
| 2243 |
case LDAP_SCOPE_BASE: |
| 2244 |
case BACKSQL_SCOPE_BASE_LIKE: |
| 2245 |
/* |
| 2246 |
* probably already found... |
| 2247 |
*/ |
| 2248 |
bsi.bsi_id_list = &bsi.bsi_base_id; |
| 2249 |
bsi.bsi_id_listtail = &bsi.bsi_base_id.eid_next; |
| 2250 |
break; |
| 2251 |
|
| 2252 |
case LDAP_SCOPE_SUBTREE: |
| 2253 |
/* |
| 2254 |
* if baseObject is defined, and if it is the root |
| 2255 |
* of the search, add it to the candidate list |
| 2256 |
*/ |
| 2257 |
if ( bi->sql_baseObject && BACKSQL_IS_BASEOBJECT_ID( &bsi.bsi_base_id.eid_id ) ) |
| 2258 |
{ |
| 2259 |
bsi.bsi_id_list = &bsi.bsi_base_id; |
| 2260 |
bsi.bsi_id_listtail = &bsi.bsi_base_id.eid_next; |
| 2261 |
} |
| 2262 |
|
| 2263 |
/* FALLTHRU */ |
| 2264 |
default: |
| 2265 |
|
| 2266 |
/* |
| 2267 |
* for each objectclass we try to construct query which gets IDs |
| 2268 |
* of entries matching LDAP query filter and scope (or at least |
| 2269 |
* candidates), and get the IDs. Do this in ID order for paging. |
| 2270 |
*/ |
| 2271 |
ldap_avl_apply( bi->sql_oc_by_id, backsql_oc_get_candidates, |
| 2272 |
&bsi, BACKSQL_AVL_STOP, AVL_INORDER ); |
| 2273 |
|
| 2274 |
/* check for abandon */ |
| 2275 |
if ( op->o_abandon ) { |
| 2276 |
eid = bsi.bsi_id_list; |
| 2277 |
rs->sr_err = SLAPD_ABANDON; |
| 2278 |
goto send_results; |
| 2279 |
} |
| 2280 |
} |
| 2281 |
|
| 2282 |
if ( op->ors_limit != NULL /* isroot == FALSE */ |
| 2283 |
&& op->ors_limit->lms_s_unchecked != -1 |
| 2284 |
&& bsi.bsi_n_candidates == -1 ) |
| 2285 |
{ |
| 2286 |
rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED; |
| 2287 |
send_ldap_result( op, rs ); |
| 2288 |
goto done; |
| 2289 |
} |
| 2290 |
|
| 2291 |
/* |
| 2292 |
* now we load candidate entries (only those attributes |
| 2293 |
* mentioned in attrs and filter), test it against full filter |
| 2294 |
* and then send to client; don't free entry_id if baseObject... |
| 2295 |
*/ |
| 2296 |
for ( eid = bsi.bsi_id_list; |
| 2297 |
eid != NULL; |
| 2298 |
eid = backsql_free_entryID( |
| 2299 |
eid, eid == &bsi.bsi_base_id ? 0 : 1, op->o_tmpmemctx ) ) |
| 2300 |
{ |
| 2301 |
int rc; |
| 2302 |
Attribute *a_hasSubordinate = NULL, |
| 2303 |
*a_entryUUID = NULL, |
| 2304 |
*a_entryCSN = NULL, |
| 2305 |
**ap = NULL; |
| 2306 |
Entry *e = NULL; |
| 2307 |
|
| 2308 |
/* check for abandon */ |
| 2309 |
if ( op->o_abandon ) { |
| 2310 |
rs->sr_err = SLAPD_ABANDON; |
| 2311 |
goto send_results; |
| 2312 |
} |
| 2313 |
|
| 2314 |
/* check time limit */ |
| 2315 |
if ( op->ors_tlimit != SLAP_NO_LIMIT |
| 2316 |
&& slap_get_time() > stoptime ) |
| 2317 |
{ |
| 2318 |
rs->sr_err = LDAP_TIMELIMIT_EXCEEDED; |
| 2319 |
rs->sr_ctrls = NULL; |
| 2320 |
rs->sr_ref = rs->sr_v2ref; |
| 2321 |
goto send_results; |
| 2322 |
} |
| 2323 |
|
| 2324 |
Debug(LDAP_DEBUG_TRACE, "backsql_search(): loading data " |
| 2325 |
"for entry id=" BACKSQL_IDFMT " oc_id=" BACKSQL_IDNUMFMT ", keyval=" BACKSQL_IDFMT "\n", |
| 2326 |
BACKSQL_IDARG(eid->eid_id), |
| 2327 |
eid->eid_oc_id, |
| 2328 |
BACKSQL_IDARG(eid->eid_keyval) ); |
| 2329 |
|
| 2330 |
/* check scope */ |
| 2331 |
switch ( op->ors_scope ) { |
| 2332 |
case LDAP_SCOPE_BASE: |
| 2333 |
case BACKSQL_SCOPE_BASE_LIKE: |
| 2334 |
if ( !dn_match( &eid->eid_ndn, &op->o_req_ndn ) ) { |
| 2335 |
goto next_entry2; |
| 2336 |
} |
| 2337 |
break; |
| 2338 |
|
| 2339 |
case LDAP_SCOPE_ONE: |
| 2340 |
{ |
| 2341 |
struct berval rdn = eid->eid_ndn; |
| 2342 |
|
| 2343 |
rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," ); |
| 2344 |
if ( !dnIsOneLevelRDN( &rdn ) ) { |
| 2345 |
goto next_entry2; |
| 2346 |
} |
| 2347 |
/* fall thru */ |
| 2348 |
} |
| 2349 |
|
| 2350 |
case LDAP_SCOPE_SUBORDINATE: |
| 2351 |
/* discard the baseObject entry */ |
| 2352 |
if ( dn_match( &eid->eid_ndn, &op->o_req_ndn ) ) { |
| 2353 |
goto next_entry2; |
| 2354 |
} |
| 2355 |
/* FALLTHRU */ |
| 2356 |
case LDAP_SCOPE_SUBTREE: |
| 2357 |
/* FIXME: this should never fail... */ |
| 2358 |
if ( !dnIsSuffix( &eid->eid_ndn, &op->o_req_ndn ) ) { |
| 2359 |
goto next_entry2; |
| 2360 |
} |
| 2361 |
break; |
| 2362 |
} |
| 2363 |
|
| 2364 |
if ( BACKSQL_IS_BASEOBJECT_ID( &eid->eid_id ) ) { |
| 2365 |
/* don't recollect baseObject... */ |
| 2366 |
e = bi->sql_baseObject; |
| 2367 |
|
| 2368 |
} else if ( eid == &bsi.bsi_base_id ) { |
| 2369 |
/* don't recollect searchBase object... */ |
| 2370 |
e = &base_entry; |
| 2371 |
|
| 2372 |
} else { |
| 2373 |
bsi.bsi_e = &user_entry; |
| 2374 |
rc = backsql_id2entry( &bsi, eid ); |
| 2375 |
if ( rc != LDAP_SUCCESS ) { |
| 2376 |
Debug( LDAP_DEBUG_TRACE, "backsql_search(): " |
| 2377 |
"error %d in backsql_id2entry() " |
| 2378 |
"- skipping\n", rc ); |
| 2379 |
continue; |
| 2380 |
} |
| 2381 |
e = &user_entry; |
| 2382 |
} |
| 2383 |
|
| 2384 |
if ( !manageDSAit && |
| 2385 |
op->ors_scope != LDAP_SCOPE_BASE && |
| 2386 |
op->ors_scope != BACKSQL_SCOPE_BASE_LIKE && |
| 2387 |
is_entry_referral( e ) ) |
| 2388 |
{ |
| 2389 |
BerVarray refs; |
| 2390 |
|
| 2391 |
refs = get_entry_referrals( op, e ); |
| 2392 |
if ( !refs ) { |
| 2393 |
backsql_srch_info bsi2 = { 0 }; |
| 2394 |
Entry user_entry2 = { 0 }; |
| 2395 |
|
| 2396 |
/* retry with the full entry... */ |
| 2397 |
bsi2.bsi_e = &user_entry2; |
| 2398 |
rc = backsql_init_search( &bsi2, |
| 2399 |
&e->e_nname, |
| 2400 |
LDAP_SCOPE_BASE, |
| 2401 |
(time_t)(-1), NULL, |
| 2402 |
dbh, op, rs, NULL, |
| 2403 |
BACKSQL_ISF_GET_ENTRY ); |
| 2404 |
if ( rc == LDAP_SUCCESS ) { |
| 2405 |
if ( is_entry_referral( &user_entry2 ) ) |
| 2406 |
{ |
| 2407 |
refs = get_entry_referrals( op, |
| 2408 |
&user_entry2 ); |
| 2409 |
} else { |
| 2410 |
rs->sr_err = LDAP_OTHER; |
| 2411 |
} |
| 2412 |
backsql_entry_clean( op, &user_entry2 ); |
| 2413 |
} |
| 2414 |
if ( bsi2.bsi_attrs != NULL ) { |
| 2415 |
op->o_tmpfree( bsi2.bsi_attrs, |
| 2416 |
op->o_tmpmemctx ); |
| 2417 |
} |
| 2418 |
} |
| 2419 |
|
| 2420 |
if ( refs ) { |
| 2421 |
rs->sr_ref = referral_rewrite( refs, |
| 2422 |
&e->e_name, |
| 2423 |
&op->o_req_dn, |
| 2424 |
op->ors_scope ); |
| 2425 |
ber_bvarray_free( refs ); |
| 2426 |
} |
| 2427 |
|
| 2428 |
if ( rs->sr_ref ) { |
| 2429 |
rs->sr_err = LDAP_REFERRAL; |
| 2430 |
|
| 2431 |
} else { |
| 2432 |
rs->sr_text = "bad referral object"; |
| 2433 |
} |
| 2434 |
|
| 2435 |
rs->sr_entry = e; |
| 2436 |
rs->sr_matched = user_entry.e_name.bv_val; |
| 2437 |
send_search_reference( op, rs ); |
| 2438 |
|
| 2439 |
ber_bvarray_free( rs->sr_ref ); |
| 2440 |
rs->sr_ref = NULL; |
| 2441 |
rs->sr_matched = NULL; |
| 2442 |
rs->sr_entry = NULL; |
| 2443 |
if ( rs->sr_err == LDAP_REFERRAL ) { |
| 2444 |
rs->sr_err = LDAP_SUCCESS; |
| 2445 |
} |
| 2446 |
|
| 2447 |
goto next_entry; |
| 2448 |
} |
| 2449 |
|
| 2450 |
/* |
| 2451 |
* We use this flag since we need to parse the filter |
| 2452 |
* anyway; we should have used the frontend API function |
| 2453 |
* filter_has_subordinates() |
| 2454 |
*/ |
| 2455 |
if ( bsi.bsi_flags & BSQL_SF_FILTER_HASSUBORDINATE ) { |
| 2456 |
rc = backsql_has_children( op, dbh, &e->e_nname ); |
| 2457 |
|
| 2458 |
switch ( rc ) { |
| 2459 |
case LDAP_COMPARE_TRUE: |
| 2460 |
case LDAP_COMPARE_FALSE: |
| 2461 |
a_hasSubordinate = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE ); |
| 2462 |
if ( a_hasSubordinate != NULL ) { |
| 2463 |
for ( ap = &user_entry.e_attrs; |
| 2464 |
*ap; |
| 2465 |
ap = &(*ap)->a_next ); |
| 2466 |
|
| 2467 |
*ap = a_hasSubordinate; |
| 2468 |
} |
| 2469 |
rc = 0; |
| 2470 |
break; |
| 2471 |
|
| 2472 |
default: |
| 2473 |
Debug(LDAP_DEBUG_TRACE, |
| 2474 |
"backsql_search(): " |
| 2475 |
"has_children failed( %d)\n", |
| 2476 |
rc ); |
| 2477 |
rc = 1; |
| 2478 |
goto next_entry; |
| 2479 |
} |
| 2480 |
} |
| 2481 |
|
| 2482 |
if ( bsi.bsi_flags & BSQL_SF_FILTER_ENTRYUUID ) { |
| 2483 |
a_entryUUID = backsql_operational_entryUUID( bi, eid ); |
| 2484 |
if ( a_entryUUID != NULL ) { |
| 2485 |
if ( ap == NULL ) { |
| 2486 |
ap = &user_entry.e_attrs; |
| 2487 |
} |
| 2488 |
|
| 2489 |
for ( ; *ap; ap = &(*ap)->a_next ); |
| 2490 |
|
| 2491 |
*ap = a_entryUUID; |
| 2492 |
} |
| 2493 |
} |
| 2494 |
|
| 2495 |
#ifdef BACKSQL_SYNCPROV |
| 2496 |
if ( bsi.bsi_flags & BSQL_SF_FILTER_ENTRYCSN ) { |
| 2497 |
a_entryCSN = backsql_operational_entryCSN( op ); |
| 2498 |
if ( a_entryCSN != NULL ) { |
| 2499 |
if ( ap == NULL ) { |
| 2500 |
ap = &user_entry.e_attrs; |
| 2501 |
} |
| 2502 |
|
| 2503 |
for ( ; *ap; ap = &(*ap)->a_next ); |
| 2504 |
|
| 2505 |
*ap = a_entryCSN; |
| 2506 |
} |
| 2507 |
} |
| 2508 |
#endif /* BACKSQL_SYNCPROV */ |
| 2509 |
|
| 2510 |
if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) |
| 2511 |
{ |
| 2512 |
#ifndef BACKSQL_ARBITRARY_KEY |
| 2513 |
/* If paged results are in effect, see if the page limit was exceeded */ |
| 2514 |
if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) { |
| 2515 |
if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) |
| 2516 |
{ |
| 2517 |
e = NULL; |
| 2518 |
send_paged_response( op, rs, &lastid ); |
| 2519 |
goto done; |
| 2520 |
} |
| 2521 |
lastid = SQL_TO_PAGECOOKIE( eid->eid_id, eid->eid_oc_id ); |
| 2522 |
} |
| 2523 |
#endif /* ! BACKSQL_ARBITRARY_KEY */ |
| 2524 |
rs->sr_attrs = op->ors_attrs; |
| 2525 |
rs->sr_operational_attrs = NULL; |
| 2526 |
rs->sr_entry = e; |
| 2527 |
e->e_private = (void *)eid; |
| 2528 |
rs->sr_flags = ( e == &user_entry ) ? REP_ENTRY_MODIFIABLE : 0; |
| 2529 |
/* FIXME: need the whole entry (ITS#3480) */ |
| 2530 |
rs->sr_err = send_search_entry( op, rs ); |
| 2531 |
e->e_private = NULL; |
| 2532 |
rs->sr_entry = NULL; |
| 2533 |
rs->sr_attrs = NULL; |
| 2534 |
rs->sr_operational_attrs = NULL; |
| 2535 |
|
| 2536 |
switch ( rs->sr_err ) { |
| 2537 |
case LDAP_UNAVAILABLE: |
| 2538 |
/* |
| 2539 |
* FIXME: send_search_entry failed; |
| 2540 |
* better stop |
| 2541 |
*/ |
| 2542 |
Debug( LDAP_DEBUG_TRACE, "backsql_search(): " |
| 2543 |
"connection lost\n" ); |
| 2544 |
goto end_of_search; |
| 2545 |
|
| 2546 |
case LDAP_SIZELIMIT_EXCEEDED: |
| 2547 |
case LDAP_BUSY: |
| 2548 |
goto send_results; |
| 2549 |
} |
| 2550 |
} |
| 2551 |
|
| 2552 |
next_entry:; |
| 2553 |
if ( e == &user_entry ) { |
| 2554 |
backsql_entry_clean( op, &user_entry ); |
| 2555 |
} |
| 2556 |
|
| 2557 |
next_entry2:; |
| 2558 |
} |
| 2559 |
|
| 2560 |
end_of_search:; |
| 2561 |
if ( rs->sr_nentries > 0 ) { |
| 2562 |
rs->sr_ref = rs->sr_v2ref; |
| 2563 |
rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS |
| 2564 |
: LDAP_REFERRAL; |
| 2565 |
|
| 2566 |
} else { |
| 2567 |
rs->sr_err = bsi.bsi_status; |
| 2568 |
} |
| 2569 |
|
| 2570 |
send_results:; |
| 2571 |
if ( rs->sr_err != SLAPD_ABANDON ) { |
| 2572 |
#ifndef BACKSQL_ARBITRARY_KEY |
| 2573 |
if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) { |
| 2574 |
send_paged_response( op, rs, NULL ); |
| 2575 |
} else |
| 2576 |
#endif /* ! BACKSQL_ARBITRARY_KEY */ |
| 2577 |
{ |
| 2578 |
send_ldap_result( op, rs ); |
| 2579 |
} |
| 2580 |
} |
| 2581 |
|
| 2582 |
/* cleanup in case of abandon */ |
| 2583 |
for ( ; eid != NULL; |
| 2584 |
eid = backsql_free_entryID( |
| 2585 |
eid, eid == &bsi.bsi_base_id ? 0 : 1, op->o_tmpmemctx ) ) |
| 2586 |
; |
| 2587 |
|
| 2588 |
backsql_entry_clean( op, &base_entry ); |
| 2589 |
|
| 2590 |
/* in case we got here accidentally */ |
| 2591 |
backsql_entry_clean( op, &user_entry ); |
| 2592 |
|
| 2593 |
if ( rs->sr_v2ref ) { |
| 2594 |
ber_bvarray_free( rs->sr_v2ref ); |
| 2595 |
rs->sr_v2ref = NULL; |
| 2596 |
} |
| 2597 |
|
| 2598 |
#ifdef BACKSQL_SYNCPROV |
| 2599 |
if ( op->o_sync ) { |
| 2600 |
Operation op2 = *op; |
| 2601 |
SlapReply rs2 = { REP_RESULT }; |
| 2602 |
Entry *e = entry_alloc(); |
| 2603 |
slap_callback cb = { 0 }; |
| 2604 |
|
| 2605 |
op2.o_tag = LDAP_REQ_ADD; |
| 2606 |
op2.o_bd = select_backend( &op->o_bd->be_nsuffix[0], 0 ); |
| 2607 |
op2.ora_e = e; |
| 2608 |
op2.o_callback = &cb; |
| 2609 |
|
| 2610 |
ber_dupbv( &e->e_name, op->o_bd->be_suffix ); |
| 2611 |
ber_dupbv( &e->e_nname, op->o_bd->be_nsuffix ); |
| 2612 |
|
| 2613 |
cb.sc_response = slap_null_cb; |
| 2614 |
|
| 2615 |
op2.o_bd->be_add( &op2, &rs2 ); |
| 2616 |
|
| 2617 |
if ( op2.ora_e == e ) |
| 2618 |
entry_free( e ); |
| 2619 |
} |
| 2620 |
#endif /* BACKSQL_SYNCPROV */ |
| 2621 |
|
| 2622 |
done:; |
| 2623 |
(void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx ); |
| 2624 |
|
| 2625 |
if ( bsi.bsi_attrs != NULL ) { |
| 2626 |
op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx ); |
| 2627 |
} |
| 2628 |
|
| 2629 |
if ( !BER_BVISNULL( &nbase ) |
| 2630 |
&& nbase.bv_val != op->o_req_ndn.bv_val ) |
| 2631 |
{ |
| 2632 |
ch_free( nbase.bv_val ); |
| 2633 |
} |
| 2634 |
|
| 2635 |
/* restore scope ... FIXME: this should be done before ANY |
| 2636 |
* frontend call that uses op */ |
| 2637 |
if ( op->ors_scope == BACKSQL_SCOPE_BASE_LIKE ) { |
| 2638 |
op->ors_scope = LDAP_SCOPE_BASE; |
| 2639 |
} |
| 2640 |
|
| 2641 |
SQLTransact( SQL_NULL_HENV, dbh, SQL_ROLLBACK ); |
| 2642 |
Debug( LDAP_DEBUG_TRACE, "<==backsql_search()\n" ); |
| 2643 |
|
| 2644 |
return rs->sr_err; |
| 2645 |
} |
| 2646 |
|
| 2647 |
/* return LDAP_SUCCESS IFF we can retrieve the specified entry. |
| 2648 |
*/ |
| 2649 |
int |
| 2650 |
backsql_entry_get( |
| 2651 |
Operation *op, |
| 2652 |
struct berval *ndn, |
| 2653 |
ObjectClass *oc, |
| 2654 |
AttributeDescription *at, |
| 2655 |
int rw, |
| 2656 |
Entry **ent ) |
| 2657 |
{ |
| 2658 |
backsql_srch_info bsi = { 0 }; |
| 2659 |
SQLHDBC dbh = SQL_NULL_HDBC; |
| 2660 |
int rc; |
| 2661 |
SlapReply rs = { 0 }; |
| 2662 |
AttributeName anlist[ 2 ]; |
| 2663 |
|
| 2664 |
*ent = NULL; |
| 2665 |
|
| 2666 |
rc = backsql_get_db_conn( op, &dbh ); |
| 2667 |
if ( rc != LDAP_SUCCESS ) { |
| 2668 |
return rc; |
| 2669 |
} |
| 2670 |
|
| 2671 |
if ( at ) { |
| 2672 |
anlist[ 0 ].an_name = at->ad_cname; |
| 2673 |
anlist[ 0 ].an_desc = at; |
| 2674 |
BER_BVZERO( &anlist[ 1 ].an_name ); |
| 2675 |
} |
| 2676 |
|
| 2677 |
bsi.bsi_e = entry_alloc(); |
| 2678 |
rc = backsql_init_search( &bsi, |
| 2679 |
ndn, |
| 2680 |
LDAP_SCOPE_BASE, |
| 2681 |
(time_t)(-1), NULL, |
| 2682 |
dbh, op, &rs, at ? anlist : NULL, |
| 2683 |
BACKSQL_ISF_GET_ENTRY ); |
| 2684 |
|
| 2685 |
if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) { |
| 2686 |
(void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx ); |
| 2687 |
} |
| 2688 |
|
| 2689 |
if ( rc == LDAP_SUCCESS ) { |
| 2690 |
|
| 2691 |
#if 0 /* not supported at present */ |
| 2692 |
/* find attribute values */ |
| 2693 |
if ( is_entry_alias( bsi.bsi_e ) ) { |
| 2694 |
Debug( LDAP_DEBUG_ACL, |
| 2695 |
"<= backsql_entry_get: entry is an alias\n" ); |
| 2696 |
rc = LDAP_ALIAS_PROBLEM; |
| 2697 |
goto return_results; |
| 2698 |
} |
| 2699 |
#endif |
| 2700 |
|
| 2701 |
if ( is_entry_referral( bsi.bsi_e ) ) { |
| 2702 |
Debug( LDAP_DEBUG_ACL, |
| 2703 |
"<= backsql_entry_get: entry is a referral\n" ); |
| 2704 |
rc = LDAP_REFERRAL; |
| 2705 |
goto return_results; |
| 2706 |
} |
| 2707 |
|
| 2708 |
if ( oc && !is_entry_objectclass( bsi.bsi_e, oc, 0 ) ) { |
| 2709 |
Debug( LDAP_DEBUG_ACL, |
| 2710 |
"<= backsql_entry_get: " |
| 2711 |
"failed to find objectClass\n" ); |
| 2712 |
rc = LDAP_NO_SUCH_ATTRIBUTE; |
| 2713 |
goto return_results; |
| 2714 |
} |
| 2715 |
|
| 2716 |
*ent = bsi.bsi_e; |
| 2717 |
} |
| 2718 |
|
| 2719 |
return_results:; |
| 2720 |
if ( bsi.bsi_attrs != NULL ) { |
| 2721 |
op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx ); |
| 2722 |
} |
| 2723 |
|
| 2724 |
if ( rc != LDAP_SUCCESS ) { |
| 2725 |
if ( bsi.bsi_e ) { |
| 2726 |
entry_free( bsi.bsi_e ); |
| 2727 |
} |
| 2728 |
} |
| 2729 |
|
| 2730 |
return rc; |
| 2731 |
} |
| 2732 |
|
| 2733 |
void |
| 2734 |
backsql_entry_clean( |
| 2735 |
Operation *op, |
| 2736 |
Entry *e ) |
| 2737 |
{ |
| 2738 |
void *ctx; |
| 2739 |
|
| 2740 |
ctx = ldap_pvt_thread_pool_context(); |
| 2741 |
|
| 2742 |
if ( ctx == NULL || ctx != op->o_tmpmemctx ) { |
| 2743 |
if ( !BER_BVISNULL( &e->e_name ) ) { |
| 2744 |
op->o_tmpfree( e->e_name.bv_val, op->o_tmpmemctx ); |
| 2745 |
BER_BVZERO( &e->e_name ); |
| 2746 |
} |
| 2747 |
|
| 2748 |
if ( !BER_BVISNULL( &e->e_nname ) ) { |
| 2749 |
op->o_tmpfree( e->e_nname.bv_val, op->o_tmpmemctx ); |
| 2750 |
BER_BVZERO( &e->e_nname ); |
| 2751 |
} |
| 2752 |
} |
| 2753 |
|
| 2754 |
entry_clean( e ); |
| 2755 |
} |
| 2756 |
|
| 2757 |
int |
| 2758 |
backsql_entry_release( |
| 2759 |
Operation *op, |
| 2760 |
Entry *e, |
| 2761 |
int rw ) |
| 2762 |
{ |
| 2763 |
backsql_entry_clean( op, e ); |
| 2764 |
|
| 2765 |
entry_free( e ); |
| 2766 |
|
| 2767 |
return 0; |
| 2768 |
} |
| 2769 |
|
| 2770 |
#ifndef BACKSQL_ARBITRARY_KEY |
| 2771 |
/* This function is copied verbatim from back-bdb/search.c */ |
| 2772 |
static int |
| 2773 |
parse_paged_cookie( Operation *op, SlapReply *rs ) |
| 2774 |
{ |
| 2775 |
int rc = LDAP_SUCCESS; |
| 2776 |
PagedResultsState *ps = op->o_pagedresults_state; |
| 2777 |
|
| 2778 |
/* this function must be invoked only if the pagedResults |
| 2779 |
* control has been detected, parsed and partially checked |
| 2780 |
* by the frontend */ |
| 2781 |
assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ); |
| 2782 |
|
| 2783 |
/* cookie decoding/checks deferred to backend... */ |
| 2784 |
if ( ps->ps_cookieval.bv_len ) { |
| 2785 |
PagedResultsCookie reqcookie; |
| 2786 |
if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) { |
| 2787 |
/* bad cookie */ |
| 2788 |
rs->sr_text = "paged results cookie is invalid"; |
| 2789 |
rc = LDAP_PROTOCOL_ERROR; |
| 2790 |
goto done; |
| 2791 |
} |
| 2792 |
|
| 2793 |
AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie )); |
| 2794 |
|
| 2795 |
if ( reqcookie > ps->ps_cookie ) { |
| 2796 |
/* bad cookie */ |
| 2797 |
rs->sr_text = "paged results cookie is invalid"; |
| 2798 |
rc = LDAP_PROTOCOL_ERROR; |
| 2799 |
goto done; |
| 2800 |
|
| 2801 |
} else if ( reqcookie < ps->ps_cookie ) { |
| 2802 |
rs->sr_text = "paged results cookie is invalid or old"; |
| 2803 |
rc = LDAP_UNWILLING_TO_PERFORM; |
| 2804 |
goto done; |
| 2805 |
} |
| 2806 |
|
| 2807 |
} else { |
| 2808 |
/* Initial request. Initialize state. */ |
| 2809 |
ps->ps_cookie = 0; |
| 2810 |
ps->ps_count = 0; |
| 2811 |
} |
| 2812 |
|
| 2813 |
done:; |
| 2814 |
|
| 2815 |
return rc; |
| 2816 |
} |
| 2817 |
|
| 2818 |
/* This function is copied nearly verbatim from back-bdb/search.c */ |
| 2819 |
static void |
| 2820 |
send_paged_response( |
| 2821 |
Operation *op, |
| 2822 |
SlapReply *rs, |
| 2823 |
ID *lastid ) |
| 2824 |
{ |
| 2825 |
LDAPControl ctrl, *ctrls[2]; |
| 2826 |
BerElementBuffer berbuf; |
| 2827 |
BerElement *ber = (BerElement *)&berbuf; |
| 2828 |
PagedResultsCookie respcookie; |
| 2829 |
struct berval cookie; |
| 2830 |
|
| 2831 |
Debug(LDAP_DEBUG_ARGS, |
| 2832 |
"send_paged_response: lastid=0x%08lx nentries=%d\n", |
| 2833 |
lastid ? *lastid : 0, rs->sr_nentries ); |
| 2834 |
|
| 2835 |
BER_BVZERO( &ctrl.ldctl_value ); |
| 2836 |
ctrls[0] = &ctrl; |
| 2837 |
ctrls[1] = NULL; |
| 2838 |
|
| 2839 |
ber_init2( ber, NULL, LBER_USE_DER ); |
| 2840 |
|
| 2841 |
if ( lastid ) { |
| 2842 |
respcookie = ( PagedResultsCookie )(*lastid); |
| 2843 |
cookie.bv_len = sizeof( respcookie ); |
| 2844 |
cookie.bv_val = (char *)&respcookie; |
| 2845 |
|
| 2846 |
} else { |
| 2847 |
respcookie = ( PagedResultsCookie )0; |
| 2848 |
BER_BVSTR( &cookie, "" ); |
| 2849 |
} |
| 2850 |
|
| 2851 |
op->o_conn->c_pagedresults_state.ps_cookie = respcookie; |
| 2852 |
op->o_conn->c_pagedresults_state.ps_count = |
| 2853 |
((PagedResultsState *)op->o_pagedresults_state)->ps_count + |
| 2854 |
rs->sr_nentries; |
| 2855 |
|
| 2856 |
/* return size of 0 -- no estimate */ |
| 2857 |
ber_printf( ber, "{iO}", 0, &cookie ); |
| 2858 |
|
| 2859 |
if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) { |
| 2860 |
goto done; |
| 2861 |
} |
| 2862 |
|
| 2863 |
ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS; |
| 2864 |
ctrls[0]->ldctl_iscritical = 0; |
| 2865 |
|
| 2866 |
rs->sr_ctrls = ctrls; |
| 2867 |
rs->sr_err = LDAP_SUCCESS; |
| 2868 |
send_ldap_result( op, rs ); |
| 2869 |
rs->sr_ctrls = NULL; |
| 2870 |
|
| 2871 |
done: |
| 2872 |
(void) ber_free_buf( ber ); |
| 2873 |
} |
| 2874 |
#endif /* ! BACKSQL_ARBITRARY_KEY */ |