1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
|
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2021 Red Hat, Inc.
* All rights reserved.
*
* License: GPL (version 3 or any later version).
* See LICENSE for details.
* END COPYRIGHT BLOCK **/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "cb.h"
/*
* Build a candidate list for this backentry and scope.
* Could be a BASE, ONELEVEL, or SUBTREE search.
*
* Returns:
* 0 - success
* <0 - fail
*
*/
int
chainingdb_build_candidate_list(Slapi_PBlock *pb)
{
cb_backend_instance *cb = NULL;
cb_outgoing_conn *cnx;
cb_searchContext *ctx = NULL;
Slapi_Backend *be;
Slapi_Operation *op;
LDAPControl **serverctrls = NULL;
LDAPControl **controls = NULL;
LDAPControl **ctrls = NULL;
LDAPMessage *res = NULL;
LDAP *ld = NULL;
Slapi_DN *target_sdn = NULL;
const char *target = NULL;
char *filter;
char **attrs = NULL;
struct timespec expire_time;
struct timeval timeout;
time_t endtime = 0;
char *matched_msg, *error_msg;
char **referrals = NULL;
char *cnxerrbuf = NULL;
int scope, attrsonly, sizelimit, timelimit, searchreferral;
int rc, parse_rc, doit;
slapi_pblock_get(pb, SLAPI_BACKEND, &be);
cb = cb_get_instance(be);
slapi_pblock_get(pb, SLAPI_OPERATION, &op);
slapi_pblock_get(pb, SLAPI_SEARCH_STRFILTER, &filter);
slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
slapi_pblock_get(pb, SLAPI_SEARCH_TARGET_SDN, &target_sdn);
target = slapi_sdn_get_dn(target_sdn);
if (LDAP_SUCCESS != (parse_rc = cb_forward_operation(pb))) {
/* Don't return errors */
if (cb_debug_on()) {
slapi_log_err(SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
"chainingdb_build_candidate_list - local search: base:<%s> scope:<%s> filter:<%s>\n", target,
scope == LDAP_SCOPE_SUBTREE ? "SUBTREE" : scope == LDAP_SCOPE_ONELEVEL ? "ONE-LEVEL" : "BASE", filter);
}
ctx = (cb_searchContext *)slapi_ch_calloc(1, sizeof(cb_searchContext));
ctx->type = CB_SEARCHCONTEXT_ENTRY;
ctx->data = NULL;
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, ctx);
return 0;
}
cb_update_monitor_info(pb, cb, SLAPI_OPERATION_SEARCH);
/* Check wether the chaining BE is available or not */
if (cb_check_availability(cb, pb) == FARMSERVER_UNAVAILABLE) {
return -1;
}
if (cb_debug_on()) {
slapi_log_err(SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
"chainingdb_build_candidate_list - chained search: base:<%s> scope:<%s> filter:<%s>\n", target,
scope == LDAP_SCOPE_SUBTREE ? "SUBTREE" : scope == LDAP_SCOPE_ONELEVEL ? "ONE-LEVEL" : "BASE",
filter);
}
slapi_pblock_get(pb, SLAPI_SEARCH_ATTRS, &attrs);
slapi_pblock_get(pb, SLAPI_SEARCH_ATTRSONLY, &attrsonly);
slapi_pblock_get(pb, SLAPI_REQCONTROLS, &controls);
slapi_pblock_get(pb, SLAPI_SEARCH_SIZELIMIT, &sizelimit);
slapi_pblock_get(pb, SLAPI_SEARCH_TIMELIMIT, &timelimit);
slapi_operation_time_expiry(op, (time_t)timelimit, &expire_time);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, NULL);
if ((scope != LDAP_SCOPE_BASE) && (scope != LDAP_SCOPE_ONELEVEL) && (scope != LDAP_SCOPE_SUBTREE)) {
cb_send_ldap_result(pb, LDAP_PROTOCOL_ERROR, NULL, "Bad scope", 0, NULL);
return 1;
}
searchreferral = cb->searchreferral;
if ((scope != LDAP_SCOPE_BASE) && (searchreferral)) {
int i;
struct berval bv, *bvals[2];
Slapi_Entry **aciArray = (Slapi_Entry **)slapi_ch_malloc(2 * sizeof(Slapi_Entry *));
Slapi_Entry *anEntry = slapi_entry_alloc();
slapi_entry_set_sdn(anEntry, target_sdn);
bvals[1] = NULL;
bvals[0] = &bv;
bv.bv_val = "referral";
bv.bv_len = strlen(bv.bv_val);
slapi_entry_add_values(anEntry, "objectclass", bvals);
slapi_rwlock_rdlock(cb->rwl_config_lock);
for (i = 0; cb->url_array && cb->url_array[i]; i++) {
char *anUrl = slapi_ch_smprintf("%s%s", cb->url_array[i], target);
bv.bv_val = anUrl;
bv.bv_len = strlen(bv.bv_val);
slapi_entry_attr_merge(anEntry, "ref", bvals);
slapi_ch_free((void **)&anUrl);
}
slapi_rwlock_unlock(cb->rwl_config_lock);
aciArray[0] = anEntry;
aciArray[1] = NULL;
ctx = (cb_searchContext *)slapi_ch_calloc(1, sizeof(cb_searchContext));
ctx->type = CB_SEARCHCONTEXT_ENTRY;
ctx->data = aciArray;
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, ctx);
return 0;
}
/*
** Time limit management.
** Make sure the operation has not expired
*/
if (slapi_timespec_expire_check(&expire_time) == TIMER_EXPIRED) {
cb_send_ldap_result(pb, LDAP_TIMELIMIT_EXCEEDED, NULL, NULL, 0, NULL);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, NULL);
return 1;
}
/* Set the timeout for ldap_search_ext */
/* for some reasons, it is an error to pass in a zero'd timeval */
/* to ldap_search_ext() */
if (timelimit == -1 || timelimit == 0) {
timeout.tv_sec = -1;
timeout.tv_usec = -1;
} else {
/* Get the elapsed op time to fix our time limit */
struct timespec elapsed;
slapi_operation_time_elapsed(op, &elapsed);
timeout.tv_sec = ((time_t)timelimit) - elapsed.tv_sec;
timeout.tv_usec = 0;
}
/* Operational attribute support for internal searches: */
/* The front-end relies on the fact that operational attributes */
/* are returned along with standard attrs when the attr list is */
/* NULL. To make it work, we need to explicitly request for all*/
/* possible operational attrs. Too bad. */
if ((attrs == NULL) && operation_is_flag_set(op, OP_FLAG_INTERNAL)) {
attrs = cb->every_attribute;
} else {
int i;
if (attrs != NULL) {
for (i = 0; attrs[i] != NULL; i++) {
if (strcasecmp("nsrole", attrs[i]) == 0) {
attrs = cb->every_attribute;
break;
}
}
}
}
/* Grab a connection handle */
rc = cb_get_connection(cb->pool, &ld, &cnx, &expire_time, &cnxerrbuf);
if (LDAP_SUCCESS != rc) {
static int warned_get_conn = 0;
if (!warned_get_conn) {
slapi_log_err(SLAPI_LOG_ERR, CB_PLUGIN_SUBSYSTEM,
"chainingdb_build_candidate_list - cb_get_connection failed (%d) %s\n",
rc, ldap_err2string(rc));
warned_get_conn = 1;
}
if (rc == LDAP_TIMELIMIT_EXCEEDED) {
cb_send_ldap_result(pb, rc, NULL, cnxerrbuf, 0, NULL);
} else {
cb_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
cnxerrbuf, 0, NULL);
}
slapi_ch_free_string(&cnxerrbuf);
/* ping the farm.
* If the farm is unreachable, we increment the counter */
cb_ping_farm(cb, NULL, 0);
return 1;
}
/*
* Control management
*/
if (LDAP_SUCCESS != (rc = cb_update_controls(pb, ld, &ctrls, CB_UPDATE_CONTROLS_ADDAUTH))) {
cb_send_ldap_result(pb, rc, NULL, NULL, 0, NULL);
cb_release_op_connection(cb->pool, ld, 0);
return 1;
}
if (slapi_op_abandoned(pb)) {
cb_release_op_connection(cb->pool, ld, 0);
ldap_controls_free(ctrls);
return 1;
}
ctx = (cb_searchContext *)slapi_ch_calloc(1, sizeof(cb_searchContext));
/*
** We need to store the connection handle in the search context
** to make sure we reuse it in the next_entry iteration
** Indeed, if another thread on this connection detects a problem
** on this connection, it may reallocate a new connection and
** a call to get_connection may return a new cnx. Too bad.
*/
ctx->ld = ld;
ctx->cnx = cnx;
/* heart-beat management */
if (cb->max_idle_time > 0) {
endtime = slapi_current_rel_time_t() + cb->max_idle_time;
}
rc = ldap_search_ext(ld, target, scope, filter, attrs, attrsonly,
ctrls, NULL, &timeout, sizelimit, &(ctx->msgid));
ldap_controls_free(ctrls);
if (LDAP_SUCCESS != rc) {
cb_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL, ldap_err2string(rc), 0, NULL);
cb_release_op_connection(cb->pool, ld, CB_LDAP_CONN_ERROR(rc));
slapi_ch_free((void **)&ctx);
return 1;
}
/*
** Need to get the very first result to handle
** errors properly, especially no search base.
*/
doit = 1;
while (doit) {
if (cb_check_forward_abandon(cb, pb, ctx->ld, ctx->msgid)) {
slapi_ch_free((void **)&ctx);
return 1;
}
rc = ldap_result(ld, ctx->msgid, LDAP_MSG_ONE, &cb->abandon_timeout, &res);
switch (rc) {
case -1:
/* An error occurred. return now */
rc = slapi_ldap_get_lderrno(ld, NULL, NULL);
/* tuck away some errors in a OPERATION_ERROR */
if (CB_LDAP_CONN_ERROR(rc)) {
cb_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
ldap_err2string(rc), 0, NULL);
} else {
cb_send_ldap_result(pb, rc, NULL, NULL, 0, NULL);
}
cb_release_op_connection(cb->pool, ld, CB_LDAP_CONN_ERROR(rc));
ldap_msgfree(res);
slapi_ch_free((void **)&ctx);
return 1;
case 0:
/* Local timeout management */
if (slapi_timespec_expire_check(&expire_time) == TIMER_EXPIRED) {
slapi_log_err(SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
"chainingdb_build_candidate_list - Local timeout expiration\n");
cb_send_ldap_result(pb, LDAP_TIMELIMIT_EXCEEDED,
NULL, NULL, 0, NULL);
/* Force connection close */
cb_release_op_connection(cb->pool, ld, 1);
ldap_msgfree(res);
slapi_ch_free((void **)&ctx);
return 1;
}
/* heart-beat management */
if ((rc = cb_ping_farm(cb, cnx, endtime)) != LDAP_SUCCESS) {
cb_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
ldap_err2string(rc), 0, NULL);
cb_release_op_connection(cb->pool, ld, CB_LDAP_CONN_ERROR(rc));
ldap_msgfree(res);
slapi_ch_free((void **)&ctx);
return 1;
}
#ifdef CB_YIELD
DS_Sleep(PR_INTERVAL_NO_WAIT);
#endif
break;
case LDAP_RES_SEARCH_ENTRY:
case LDAP_RES_SEARCH_REFERENCE:
/* Some results received */
/* don't parse result here */
ctx->pending_result = res;
ctx->pending_result_type = rc;
doit = 0;
break;
case LDAP_RES_SEARCH_RESULT:
matched_msg = NULL;
error_msg = NULL;
referrals = NULL;
serverctrls = NULL;
parse_rc = ldap_parse_result(ld, res, &rc, &matched_msg,
&error_msg, &referrals, &serverctrls, 0);
if (parse_rc != LDAP_SUCCESS) {
static int warned_parse_rc = 0;
if (!warned_parse_rc && error_msg) {
slapi_log_err(SLAPI_LOG_ERR, CB_PLUGIN_SUBSYSTEM,
"%s%s%s\n",
matched_msg ? matched_msg : "",
(matched_msg && (*matched_msg != '\0')) ? ": " : "",
error_msg);
warned_parse_rc = 1;
}
cb_send_ldap_result(pb, parse_rc, NULL, ENDUSERMSG, 0, NULL);
rc = -1;
} else if (rc != LDAP_SUCCESS) {
static int warned_rc = 0;
if (!warned_rc) {
slapi_ldap_get_lderrno(ctx->ld, &matched_msg, &error_msg);
slapi_log_err(SLAPI_LOG_ERR, CB_PLUGIN_SUBSYSTEM,
"%s%s%s\n",
matched_msg ? matched_msg : "",
(matched_msg && (*matched_msg != '\0')) ? ": " : "",
error_msg);
warned_rc = 1;
}
cb_send_ldap_result(pb, rc, NULL, ENDUSERMSG, 0, NULL);
/* BEWARE: matched_msg points */
/* to ld fields. */
matched_msg = NULL;
rc = -1;
}
slapi_ch_free_string(&matched_msg);
slapi_ch_free_string(&error_msg);
ldap_controls_free(serverctrls);
charray_free(referrals);
if (rc != LDAP_SUCCESS) {
cb_release_op_connection(cb->pool, ld,
CB_LDAP_CONN_ERROR(rc));
ldap_msgfree(res);
slapi_ch_free((void **)&ctx);
return -1;
}
/* Store the msg in the ctx */
/* Parsed in iterate. */
ctx->pending_result = res;
ctx->pending_result_type = LDAP_RES_SEARCH_RESULT;
doit = 0;
}
}
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, ctx);
return 0;
}
/*
* Return the next entry in the result set. The entry is returned
* in the pblock.
* Returns 0 normally. If -1 is returned, it means that some
* exceptional condition, e.g. timelimit exceeded has occurred,
* and this routine has sent a result to the client. If zero
* is returned and no entry is available in the PBlock, then
* we've iterated through all the entries.
*/
int
chainingdb_next_search_entry(Slapi_PBlock *pb)
{
Slapi_DN *target_sdn = NULL;
int sizelimit, timelimit;
int rc, parse_rc, retcode;
int i, attrsonly;
struct timespec expire_time;
LDAPMessage *res = NULL;
char *matched_msg, *error_msg;
cb_searchContext *ctx = NULL;
Slapi_Entry *entry;
LDAPControl **serverctrls = NULL;
char **referrals = NULL;
cb_backend_instance *cb = NULL;
Slapi_Backend *be;
time_t endtime = 0;
Slapi_Operation *op;
matched_msg = error_msg = NULL;
slapi_pblock_get(pb, SLAPI_SEARCH_RESULT_SET, &ctx);
slapi_pblock_get(pb, SLAPI_BACKEND, &be);
slapi_pblock_get(pb, SLAPI_SEARCH_TIMELIMIT, &timelimit);
slapi_pblock_get(pb, SLAPI_SEARCH_SIZELIMIT, &sizelimit);
slapi_pblock_get(pb, SLAPI_SEARCH_TARGET_SDN, &target_sdn);
slapi_pblock_get(pb, SLAPI_OPERATION, &op);
slapi_operation_time_expiry(op, (time_t)timelimit, &expire_time);
slapi_pblock_get(pb, SLAPI_SEARCH_ATTRSONLY, &attrsonly);
cb = cb_get_instance(be);
if (NULL == ctx) {
/* End of local search */
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, NULL);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, NULL);
slapi_log_err(SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
"chainingdb_next_search_entry - Unexpected NULL ctx in chainingdb_next_search_entry\n");
return 0;
}
if (NULL != ctx->readahead) {
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, ctx);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, ctx->readahead);
if (ctx->tobefreed != ctx->readahead) {
slapi_entry_free(ctx->tobefreed);
}
ctx->tobefreed = ctx->readahead;
ctx->readahead = NULL;
cb_set_acl_policy(pb);
return 0;
}
if (NULL != ctx->tobefreed) {
slapi_entry_free(ctx->tobefreed);
ctx->tobefreed = NULL;
}
if (ctx->type == CB_SEARCHCONTEXT_ENTRY) {
int n;
Slapi_Entry **ptr;
if (slapi_timespec_expire_check(&expire_time) == TIMER_EXPIRED) {
cb_send_ldap_result(pb, LDAP_TIMELIMIT_EXCEEDED, NULL, NULL, 0, NULL);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, NULL);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, NULL);
for (n = 0, ptr = (Slapi_Entry **)ctx->data; ptr != NULL && ptr[n] != NULL; n++) {
slapi_entry_free(ptr[n]);
}
if (ctx->data) {
slapi_ch_free((void **)&ctx->data);
}
slapi_ch_free((void **)&ctx);
return -1;
}
/*
** Return the Slapi_Entry of the result set one
** by one
*/
for (n = 0, ptr = (Slapi_Entry **)ctx->data; ptr != NULL && ptr[n] != NULL; n++)
;
if (n != 0) {
Slapi_Entry *anEntry = ptr[n - 1];
ptr[n - 1] = NULL;
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, anEntry);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, ctx);
cb_set_acl_policy(pb);
ctx->tobefreed = anEntry;
} else {
slapi_ch_free((void **)&ctx);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, NULL);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, NULL);
}
return 0;
}
/*
* Grab a connection handle. Should be the same as the one
* used in the build_candidate list. To be certain of that, grab it from
* the context.
*/
/* Poll the server for the results of the search operation.
* Passing LDAP_MSG_ONE indicates that you want to receive
* the entries one at a time, as they come in. If the next
* entry that you retrieve is NULL, there are no more entries.
*/
/* heart-beat management */
if (cb->max_idle_time > 0) {
endtime = slapi_current_rel_time_t() + cb->max_idle_time;
}
while (1) {
if (cb_check_forward_abandon(cb, pb, ctx->ld, ctx->msgid)) {
/* cnx handle released */
ldap_msgfree(ctx->pending_result);
slapi_ch_free((void **)&ctx);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, NULL);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, NULL);
return -1;
}
/* Check for time limit done by the remote farm server */
/* Check for size limit done by the remote farm server */
/* Use pending msg if one is available */
if (ctx->pending_result) {
res = ctx->pending_result;
rc = ctx->pending_result_type;
ctx->pending_result = NULL;
} else {
rc = ldap_result(ctx->ld, ctx->msgid,
LDAP_MSG_ONE, &cb->abandon_timeout, &res);
}
/* The server can return three types of results back to the client,
* and the return value of ldap_result() indicates the result type:
* LDAP_RES_SEARCH_ENTRY identifies an entry found by the search,
* LDAP_RES_SEARCH_REFERENCE identifies a search reference returned
* by the server, and LDAP_RES_SEARCH_RESULT is the last result
* sent from the server to the client after the operation completes.
* We need to check for each of these types of results.
*/
switch (rc) {
case -1:
/* An error occurred. */
rc = slapi_ldap_get_lderrno(ctx->ld, NULL, NULL);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, NULL);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, NULL);
cb_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL, ldap_err2string(rc), 0, NULL);
ldap_msgfree(res);
cb_release_op_connection(cb->pool, ctx->ld, CB_LDAP_CONN_ERROR(rc));
slapi_ch_free((void **)&ctx);
return -1;
case 0:
/* heart-beat management */
if ((rc = cb_ping_farm(cb, ctx->cnx, endtime)) != LDAP_SUCCESS) {
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, NULL);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, NULL);
cb_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
ldap_err2string(rc), 0, NULL);
ldap_msgfree(res);
cb_release_op_connection(cb->pool, ctx->ld, CB_LDAP_CONN_ERROR(rc));
slapi_ch_free((void **)&ctx);
return -1;
}
#ifdef CB_YIELD
DS_Sleep(PR_INTERVAL_NO_WAIT);
#endif
break;
case LDAP_RES_SEARCH_ENTRY:
/* heart-beat management */
if (cb->max_idle_time > 0) {
endtime = slapi_current_rel_time_t() + cb->max_idle_time;
}
/* The server sent one of the entries found by the search */
if ((entry = cb_LDAPMessage2Entry(ctx->ld, res, attrsonly)) == NULL) {
slapi_log_err(SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM, "chainingdb_next_search_entry - Invalid entry received.\n");
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, NULL);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, NULL);
cb_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL, NULL, 0, NULL);
ldap_msgfree(res);
cb_release_op_connection(cb->pool, ctx->ld, 0);
slapi_ch_free((void **)&ctx);
return -1;
}
ctx->tobefreed = entry;
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, ctx);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, entry);
cb_set_acl_policy(pb);
ldap_msgfree(res);
return 0;
case LDAP_RES_SEARCH_REFERENCE:
/* The server sent a search reference encountered during the
* search operation.
*/
/* heart-beat management */
if (cb->max_idle_time > 0) {
endtime = slapi_current_rel_time_t() + cb->max_idle_time;
}
parse_rc = ldap_parse_reference(ctx->ld, res, &referrals, NULL, 1);
if (parse_rc != LDAP_SUCCESS) {
cb_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
ldap_err2string(parse_rc), 0, NULL);
cb_release_op_connection(cb->pool, ctx->ld, CB_LDAP_CONN_ERROR(parse_rc));
slapi_ch_free((void **)&ctx);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, NULL);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, NULL);
return -1;
}
/*
* build a dummy entry on the fly with a ref attribute
*/
{
struct berval bv;
struct berval *bvals[2];
Slapi_Entry *anEntry = slapi_entry_alloc();
slapi_entry_set_sdn(anEntry, target_sdn);
bvals[1] = NULL;
bvals[0] = &bv;
bv.bv_val = "referral";
bv.bv_len = strlen(bv.bv_val);
slapi_entry_add_values(anEntry, "objectclass", bvals);
for (size_t ii = 0; referrals && referrals[ii] != NULL; ii++) {
bv.bv_val = referrals[ii];
bv.bv_len = strlen(bv.bv_val);
slapi_entry_add_values(anEntry, "ref", bvals);
}
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, ctx);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, anEntry);
cb_set_acl_policy(pb);
}
slapi_ldap_value_free(referrals);
return 0;
case LDAP_RES_SEARCH_RESULT:
/* Parse the final result received from the server. Note the last
* argument is a non-zero value, which indicates that the
* LDAPMessage structure will be freed when done.
*/
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, NULL);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, NULL);
parse_rc = ldap_parse_result(ctx->ld, res,
&rc, &matched_msg, &error_msg, &referrals, &serverctrls, 1);
if (parse_rc != LDAP_SUCCESS) {
static int warned_parse_rc = 0;
if (!warned_parse_rc) {
slapi_log_err(SLAPI_LOG_ERR, CB_PLUGIN_SUBSYSTEM,
"%s%s%s\n",
matched_msg ? matched_msg : "",
(matched_msg && (*matched_msg != '\0')) ? ": " : "",
ldap_err2string(parse_rc));
warned_parse_rc = 1;
}
cb_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
ENDUSERMSG, 0, NULL);
retcode = -1;
} else if (rc != LDAP_SUCCESS) {
static int warned_rc = 0;
slapi_ldap_get_lderrno(ctx->ld, &matched_msg, &error_msg);
if (!warned_rc) {
slapi_log_err(SLAPI_LOG_ERR, CB_PLUGIN_SUBSYSTEM,
"%s%s%s\n",
matched_msg ? matched_msg : "",
(matched_msg && (*matched_msg != '\0')) ? ": " : "",
error_msg);
warned_rc = 1;
}
cb_send_ldap_result(pb, rc, matched_msg, ENDUSERMSG, 0, NULL);
/* BEWARE: Don't free matched_msg */
/* Points to the ld fields */
matched_msg = NULL;
retcode = -1;
} else {
/* Add control response sent by the farm server */
for (i = 0; serverctrls && serverctrls[i]; i++)
slapi_pblock_set(pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
retcode = 0;
}
ldap_controls_free(serverctrls);
slapi_ch_free_string(&matched_msg);
slapi_ch_free_string(&error_msg);
charray_free(referrals);
cb_release_op_connection(cb->pool, ctx->ld, 0);
slapi_ch_free((void **)&ctx);
return retcode;
default:
slapi_log_err(SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
"chainingdb_next_search_entry - default case.\n");
}
}
/* Not reached */
/* return 0; */
}
void
chaining_back_search_results_release(void **sr)
{
cb_searchContext *ctx = (cb_searchContext *)(*sr);
slapi_log_err(SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
"chaining_back_search_results_release\n");
if (ctx == NULL) {
/* The paged search is already complete, just return */
/* Could we have a ctx state flag instead? */
return;
}
if (ctx->readahead != ctx->tobefreed) {
slapi_entry_free(ctx->readahead);
}
slapi_entry_free(ctx->tobefreed);
ctx->tobefreed = NULL;
slapi_ch_free((void **)&ctx->data);
slapi_ch_free((void **)&ctx);
return;
}
void
chainingdb_prev_search_results(Slapi_PBlock *pb)
{
cb_searchContext *ctx = NULL;
Slapi_Entry *entry = NULL;
slapi_pblock_get(pb, SLAPI_SEARCH_RESULT_SET, &ctx);
slapi_pblock_get(pb, SLAPI_SEARCH_RESULT_ENTRY, &entry);
if (ctx) {
ctx->readahead = entry;
}
return;
}
|