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 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
|
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 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
/* stevross@netscape.com June 13 1997 */
#include <stdio.h>
#include <sys/types.h>
#include "slapi-plugin.h"
#include "slap.h"
#include <ssl.h>
/* entry list node */
typedef struct Entry_Node
{
Slapi_Entry *data;
struct Entry_Node *next;
} Entry_Node;
/* referral list node */
typedef struct Referral_Node
{
char *data;
struct Referral_Node *next;
} Referral_Node;
/* data that must be passed to slapi_search_internal_callback */
typedef struct plugin_search_internal_data
{
int rc;
int num_entries;
int num_referrals;
Entry_Node *entry_list_head;
Referral_Node *referral_list_head;
} plugin_search_internal_data;
/* callback functions */
typedef struct callback_fn_ptrs
{
plugin_result_callback p_res_callback;
plugin_search_entry_callback p_srch_entry_callback;
plugin_referral_entry_callback p_ref_entry_callback;
void *callback_data;
} callback_fn_ptrs;
/* forward declarations */
static int seq_internal_callback_pb(Slapi_PBlock *pb, void *callback_data, plugin_result_callback prc, plugin_search_entry_callback psec, plugin_referral_entry_callback prec);
static int search_internal_pb(Slapi_PBlock *pb);
static int search_internal_callback_pb(Slapi_PBlock *pb, void *callback_data, plugin_result_callback prc, plugin_search_entry_callback psec, plugin_referral_entry_callback prec);
void
internal_getresult_callback(struct conn *unused1 __attribute__((unused)),
struct op *op,
int err,
char *unused2 __attribute__((unused)),
char *unused3 __attribute__((unused)),
int unused4 __attribute__((unused)),
struct berval **unused5 __attribute__((unused)))
{
if (op != NULL) {
*((int *)op->o_handler_data) = err;
}
}
void
internal_res_callback(struct conn *unused1 __attribute__((unused)),
struct op *op,
int err,
char *unused2 __attribute__((unused)),
char *unused3 __attribute__((unused)),
int unused4 __attribute__((unused)),
struct berval **unused5 __attribute__((unused)))
{
/* make sure the user has a callback defined, if so do it, otherwise do nothing */
if (((callback_fn_ptrs *)op->o_handler_data) != NULL && ((callback_fn_ptrs *)op->o_handler_data)->p_res_callback != NULL) {
((callback_fn_ptrs *)op->o_handler_data)->p_res_callback(err, ((callback_fn_ptrs *)op->o_handler_data)->callback_data);
}
}
int
internal_srch_entry_callback(Slapi_Backend *be __attribute__((unused)),
Connection *conn __attribute__((unused)),
Operation *op,
Slapi_Entry *e)
{
/* make sure the user has a callback defined, if so do it, otherwise do nothing */
if (((callback_fn_ptrs *)op->o_handler_data) != NULL && ((callback_fn_ptrs *)op->o_handler_data)->p_srch_entry_callback != NULL) {
return (((callback_fn_ptrs *)op->o_handler_data)->p_srch_entry_callback(e, ((callback_fn_ptrs *)op->o_handler_data)->callback_data));
}
return (0);
}
int
internal_ref_entry_callback(Slapi_Backend *be __attribute__((unused)),
Connection *conn __attribute__((unused)),
Operation *op,
struct berval **ireferral)
{
/* make sure the user has a callback defined, if so do it, otherwise do nothing */
if (((callback_fn_ptrs *)op->o_handler_data) != NULL && ((callback_fn_ptrs *)op->o_handler_data)->p_ref_entry_callback != NULL && ireferral != NULL) {
/* loop over referrals calling callback for each one */
for (size_t i = 0; ireferral[i] != NULL; i++) {
((callback_fn_ptrs *)op->o_handler_data)->p_ref_entry_callback(ireferral[i]->bv_val, ((callback_fn_ptrs *)op->o_handler_data)->callback_data);
}
}
return (0);
}
Slapi_Operation *
internal_operation_new(unsigned long op_type, int flags)
{
Slapi_Operation *op = operation_new(flags | OP_FLAG_INTERNAL /*Internal*/);
/* set operation type: add, delete, etc */
operation_set_type(op, op_type);
/* Call the plugin extension constructors */
op->o_extension = factory_create_extension(get_operation_object_type(), op, NULL /* Parent */);
return op;
}
/******************************************************************************
*
* do_disconnect_server
*
*
*
*
*******************************************************************************/
/* this is just a wrapper exposed to the plugins */
void
slapi_disconnect_server(Slapi_Connection *conn)
{
do_disconnect_server(conn, conn->c_connid, -1);
}
static get_disconnect_server_fn_ptr disconnect_server_fn = NULL;
void
do_disconnect_server(Connection *conn, PRUint64 opconnid, int opid)
{
if (NULL == disconnect_server_fn) {
if (get_entry_point(ENTRY_POINT_DISCONNECT_SERVER, (caddr_t *)(&disconnect_server_fn)) < 0) {
return;
}
}
/* It seems that we only call this from result.c when the ber_flush fails. */
(disconnect_server_fn)(conn, opconnid, opid, SLAPD_DISCONNECT_BER_FLUSH, 0);
}
/******************************************************************************
*
* slapi_compare_internal
*
* no plans to implement this, but placeholder incase we change our mind
*
*
*******************************************************************************/
Slapi_PBlock *
slapi_compare_internal(char *dn __attribute__((unused)),
char *attr __attribute__((unused)),
char *value __attribute__((unused)),
LDAPControl **controls __attribute__((unused)))
{
printf("slapi_compare_internal not yet implemented \n");
return (0);
}
int
slapi_seq_callback(const char *ibase,
int type,
char *attrname,
char *val,
char **attrs,
int attrsonly,
void *callback_data,
LDAPControl **controls,
plugin_result_callback res_callback,
plugin_search_entry_callback srch_callback,
plugin_referral_entry_callback ref_callback)
{
int r;
if (ibase == NULL) {
slapi_log_err(SLAPI_LOG_ERR, "slapi_seq_callback",
"NULL parameter\n");
return -1;
}
Slapi_PBlock *pb = slapi_pblock_new();
slapi_seq_internal_set_pb(pb, (char *)ibase, type, attrname, val, attrs, attrsonly, controls,
plugin_get_default_component_id(), 0);
r = seq_internal_callback_pb(pb, callback_data, res_callback, srch_callback, ref_callback);
slapi_pblock_destroy(pb);
return r;
}
/* pblock should contain the following data (can be set via call to slapi_seq_internal_set_pb):
SLAPI_SEARCH_TARGET set to search base
SAPI_SEQ_TYPE set to sequential access type (SLAPI_SEQ_FIRST, SLAPI_SEQ_NEXT, etc.
SLAPI_SEQ_ATTRNAME the next two fields define attribute value assertion
SLAPI_SEQ_VAL relative to which access is performed.
SLAPI_CONTROLS_ARG set to request controls if present
SLAPI_SEARCH_ATTRS set to the list of attributes to return
SLAPI_SEARCH_ATTRSONLY tells whether attribute values should be returned.
*/
int
slapi_seq_internal_callback_pb(Slapi_PBlock *pb, void *callback_data, plugin_result_callback res_callback, plugin_search_entry_callback srch_callback, plugin_referral_entry_callback ref_callback)
{
if (pb == NULL)
return -1;
if (!allow_operation(pb)) {
send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
"This plugin is not configured to access operation target data", 0, NULL);
return 0;
}
return (seq_internal_callback_pb(pb, callback_data, res_callback, srch_callback, ref_callback));
}
void
slapi_search_internal_set_pb(Slapi_PBlock *pb, const char *base, int scope, const char *filter, char **attrs, int attrsonly, LDAPControl **controls, const char *uniqueid, Slapi_ComponentId *plugin_identity, int operation_flags)
{
Operation *op;
char **tmp_attrs = NULL;
if (pb == NULL || base == NULL) {
slapi_log_err(SLAPI_LOG_ERR, "slapi_search_internal_set_pb",
"NULL parameter\n");
return;
}
op = internal_operation_new(SLAPI_OPERATION_SEARCH, operation_flags);
slapi_pblock_set(pb, SLAPI_OPERATION, op);
slapi_pblock_set(pb, SLAPI_ORIGINAL_TARGET_DN, (void *)base);
slapi_pblock_set(pb, SLAPI_SEARCH_SCOPE, &scope);
slapi_pblock_set(pb, SLAPI_SEARCH_STRFILTER, (void *)filter);
slapi_pblock_set(pb, SLAPI_CONTROLS_ARG, controls);
/* forbidden attrs could be removed in slapi_pblock_set. */
tmp_attrs = slapi_ch_array_dup(attrs);
slapi_pblock_set(pb, SLAPI_SEARCH_ATTRS, tmp_attrs);
slapi_pblock_set(pb, SLAPI_SEARCH_ATTRSONLY, &attrsonly);
if (uniqueid) {
slapi_pblock_set(pb, SLAPI_TARGET_UNIQUEID, (void *)uniqueid);
}
slapi_pblock_set(pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity);
}
void
slapi_search_internal_set_pb_ext(Slapi_PBlock *pb, Slapi_DN *sdn, int scope, const char *filter, char **attrs, int attrsonly, LDAPControl **controls, const char *uniqueid, Slapi_ComponentId *plugin_identity, int operation_flags)
{
Operation *op;
char **tmp_attrs = NULL;
if (pb == NULL || sdn == NULL) {
slapi_log_err(SLAPI_LOG_ERR, "slapi_search_internal_set_pb",
"NULL parameter\n");
return;
}
op = internal_operation_new(SLAPI_OPERATION_SEARCH, operation_flags);
slapi_pblock_set(pb, SLAPI_OPERATION, op);
slapi_pblock_set(pb, SLAPI_ORIGINAL_TARGET_DN,
(void *)slapi_sdn_get_udn(sdn));
slapi_pblock_set(pb, SLAPI_TARGET_SDN, (void *)sdn);
slapi_pblock_set(pb, SLAPI_SEARCH_SCOPE, &scope);
slapi_pblock_set(pb, SLAPI_SEARCH_STRFILTER, (void *)filter);
slapi_pblock_set(pb, SLAPI_CONTROLS_ARG, controls);
/* forbidden attrs could be removed in slapi_pblock_set. */
tmp_attrs = slapi_ch_array_dup(attrs);
slapi_pblock_set(pb, SLAPI_SEARCH_ATTRS, tmp_attrs);
slapi_pblock_set(pb, SLAPI_SEARCH_ATTRSONLY, &attrsonly);
if (uniqueid) {
slapi_pblock_set(pb, SLAPI_TARGET_UNIQUEID, (void *)uniqueid);
}
slapi_pblock_set(pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity);
}
void
slapi_seq_internal_set_pb(Slapi_PBlock *pb, char *base, int type, char *attrname, char *val, char **attrs, int attrsonly, LDAPControl **controls, Slapi_ComponentId *plugin_identity, int operation_flags)
{
Operation *op;
char **tmp_attrs = NULL;
if (pb == NULL || base == NULL) {
slapi_log_err(SLAPI_LOG_ERR, "slapi_seq_internal_set_pb",
"NULL parameter\n");
return;
}
op = internal_operation_new(SLAPI_OPERATION_SEARCH, operation_flags);
slapi_pblock_set(pb, SLAPI_OPERATION, op);
slapi_pblock_set(pb, SLAPI_ORIGINAL_TARGET_DN, (void *)base);
slapi_pblock_set(pb, SLAPI_SEQ_TYPE, &type);
slapi_pblock_set(pb, SLAPI_SEQ_ATTRNAME, attrname);
slapi_pblock_set(pb, SLAPI_SEQ_VAL, val);
/* forbidden attrs could be removed in slapi_pblock_set. */
tmp_attrs = slapi_ch_array_dup(attrs);
slapi_pblock_set(pb, SLAPI_SEARCH_ATTRS, tmp_attrs);
slapi_pblock_set(pb, SLAPI_SEARCH_ATTRSONLY, &attrsonly);
slapi_pblock_set(pb, SLAPI_CONTROLS_ARG, controls);
slapi_pblock_set(pb, SLAPI_PLUGIN_IDENTITY, plugin_identity);
}
static int
seq_internal_callback_pb(Slapi_PBlock *pb, void *callback_data, plugin_result_callback prc, plugin_search_entry_callback psec, plugin_referral_entry_callback prec)
{
int rc;
LDAPControl **controls;
Operation *op;
struct callback_fn_ptrs callback_handler_data;
Slapi_Backend *be;
char *base;
char *attrname, *val;
Slapi_DN *sdn = NULL;
char **tmp_attrs = NULL;
slapi_pblock_get(pb, SLAPI_ORIGINAL_TARGET_DN, (void *)&base);
slapi_pblock_get(pb, SLAPI_CONTROLS_ARG, &controls);
if (base == NULL) {
sdn = slapi_sdn_new_ndn_byval("");
} else {
sdn = slapi_sdn_new_dn_byref(base);
}
slapi_pblock_set(pb, SLAPI_SEARCH_TARGET_SDN, sdn);
be = slapi_be_select(sdn);
callback_handler_data.p_res_callback = prc;
callback_handler_data.p_srch_entry_callback = psec;
callback_handler_data.p_ref_entry_callback = prec;
callback_handler_data.callback_data = callback_data;
slapi_pblock_get(pb, SLAPI_OPERATION, &op);
op->o_handler_data = (void *)&callback_handler_data;
op->o_result_handler = internal_res_callback;
op->o_search_entry_handler = internal_srch_entry_callback;
op->o_search_referral_handler = internal_ref_entry_callback;
/* set target specification of the operation used to decide which plugins are called for the operation */
operation_set_target_spec(op, sdn);
/* Normalize the attribute type and value */
slapi_pblock_get(pb, SLAPI_SEQ_ATTRNAME, &attrname);
slapi_pblock_get(pb, SLAPI_SEQ_VAL, &val);
attrname = slapi_attr_syntax_normalize(attrname);
val = (NULL == val) ? NULL : slapi_ch_strdup(val);
slapi_pblock_set(pb, SLAPI_BACKEND, be);
slapi_pblock_set(pb, SLAPI_PLUGIN, be->be_database);
slapi_pblock_set(pb, SLAPI_SEQ_ATTRNAME, attrname);
/* coverity false positive:
* var_deref_model: Passing null pointer "val" to "slapi_pblock_set", which dereferences it.
* but val is not dereferenced in SLAPI_SEQ_VAL case so lets ignore this one.
*/
/* coverity[var_deref_model] */
slapi_pblock_set(pb, SLAPI_SEQ_VAL, val);
slapi_pblock_set(pb, SLAPI_REQCONTROLS, controls);
/* set actions taken to process the operation */
set_config_params(pb);
/* set common parameters */
set_common_params(pb);
slapi_td_internal_op_start();
if (be->be_seq != NULL) {
rc = (*be->be_seq)(pb);
} else {
send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL, "Function not implemented", 0, NULL);
rc = 0;
}
slapi_td_internal_op_finish();
slapi_ch_free_string(&attrname);
slapi_ch_free_string(&val);
/* slapi_pblock_get(pb, SLAPI_SEARCH_TARGET, &normbase); */
slapi_pblock_get(pb, SLAPI_SEARCH_TARGET_SDN, &sdn);
slapi_sdn_free(&sdn);
slapi_pblock_set(pb, SLAPI_SEARCH_TARGET_SDN, NULL);
slapi_pblock_get(pb, SLAPI_SEARCH_ATTRS, &tmp_attrs);
slapi_ch_array_free(tmp_attrs);
slapi_pblock_set(pb, SLAPI_SEARCH_ATTRS, NULL);
return rc;
}
int
slapi_search_internal_callback(const char *ibase,
int scope,
const char *ifstr,
char **attrs,
int attrsonly,
void *callback_data,
LDAPControl **controls,
plugin_result_callback res_callback,
plugin_search_entry_callback srch_callback,
plugin_referral_entry_callback ref_callback)
{
Slapi_PBlock *pb = slapi_pblock_new();
int rc = 0;
slapi_search_internal_set_pb(pb, ibase, scope, ifstr, attrs, attrsonly,
controls, NULL, plugin_get_default_component_id(), 0);
rc = search_internal_callback_pb(pb, callback_data, res_callback,
srch_callback, ref_callback);
slapi_pblock_destroy(pb);
return (rc);
}
Slapi_PBlock *
slapi_search_internal(const char *base,
int scope,
const char *filter,
LDAPControl **controls,
char **attrs,
int attrsonly)
{
Slapi_PBlock *pb;
/* initialize pb */
pb = slapi_pblock_new();
if (pb) {
slapi_search_internal_set_pb(pb, base, scope, filter, attrs, attrsonly, controls,
NULL, plugin_get_default_component_id(), 0);
search_internal_pb(pb);
}
return pb;
}
/* This function free searchs result or referral set on internal_ops in pblocks */
void
slapi_free_search_results_internal(Slapi_PBlock *pb)
{
if (pb == NULL) {
return;
}
Slapi_Entry **op_entries = NULL;
slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &op_entries);
if (op_entries != NULL) {
for (size_t i = 0; op_entries[i] != NULL; i++) {
slapi_entry_free(op_entries[i]);
}
slapi_ch_free((void **)&(op_entries));
}
char **op_referrals = NULL;
slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_REFERRALS, &op_referrals);
/* free v3 referrals made from result handler */
if (op_referrals != NULL) {
for (size_t i = 0; op_referrals[i] != NULL; i++) {
slapi_ch_free((void **)&(op_referrals[i]));
}
slapi_ch_free((void **)&(op_referrals));
}
}
/* this functions can be used for dn as well as uniqueid based operations */
/* pblock should contain the following data (can be set via call to slapi_search_internal_set_pb):
For uniqueid based search:
SLAPI_TARGET_DN set to dn that allows to select right backend
SLAPI_TARGET_UNIQUEID set to the uniqueid of the entry we are looking for
For dn based search:
SLAPI_TARGET_DN set to search base
SLAPI_ORIGINAL_TARGET_DN set to original un-normalized search base
SLAPI_SEARCH_SCOPE set to search scope
SLAPI_SEARCH_STRFILTER set to search filter
SLAPI_CONTROLS_ARG set to request controls if present
SLAPI_SEARCH_ATTRS set to the list of attributes to return
SLAPI_SEARCH_ATTRSONLY tells whether attribute values should be returned.
*/
int
slapi_search_internal_pb(Slapi_PBlock *pb)
{
if (pb == NULL)
return -1;
if (!allow_operation(pb)) {
slapi_send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
"This plugin is not configured to access operation target data", 0, NULL);
return 0;
}
return search_internal_pb(pb);
}
/* pblock should contain the same data as for slapi_search_internal_pb */
int
slapi_search_internal_callback_pb(Slapi_PBlock *pb, void *callback_data, plugin_result_callback prc, plugin_search_entry_callback psec, plugin_referral_entry_callback prec)
{
if (pb == NULL)
return -1;
if (!allow_operation(pb)) {
send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
"This plugin is not configured to access operation target data", 0, NULL);
return 0;
}
return (search_internal_callback_pb(pb, callback_data, prc, psec, prec));
}
static int
internal_plugin_search_entry_callback(Slapi_Entry *e, void *callback_data)
{
Entry_Node *this_entry;
/* add this entry to the list of entries we are making */
this_entry = (Entry_Node *)slapi_ch_calloc(1, sizeof(Entry_Node));
if ((this_entry->data = slapi_entry_dup(e)) == NULL) {
slapi_ch_free((void**)&this_entry);
return (0);
}
this_entry->next = ((plugin_search_internal_data *)callback_data)->entry_list_head;
((plugin_search_internal_data *)callback_data)->entry_list_head = this_entry;
((plugin_search_internal_data *)callback_data)->num_entries++;
return (0);
}
static int
internal_plugin_search_referral_callback(char *referral, void *callback_data)
{
Referral_Node *this_referral;
/* add this to the list of referrals we are making */
this_referral = (Referral_Node *)slapi_ch_calloc(1, sizeof(Referral_Node));
this_referral->data = slapi_ch_strdup(referral);
this_referral->next = ((plugin_search_internal_data *)callback_data)->referral_list_head;
((plugin_search_internal_data *)callback_data)->referral_list_head = this_referral;
((plugin_search_internal_data *)callback_data)->num_referrals++;
return (0);
}
static void
internal_plugin_result_callback(int rc, void *callback_data)
{
/* put the result into pb_op_result */
((plugin_search_internal_data *)callback_data)->rc = rc;
}
static int
search_internal_pb(Slapi_PBlock *pb)
{
plugin_search_internal_data psid;
Entry_Node *iterator, *tmp;
Referral_Node *ref_iterator, *ref_tmp;
int i;
int opresult = 0;
Slapi_Entry **pb_search_entries = NULL;
char **pb_search_referrals = NULL;
PR_ASSERT(pb);
/* initialize psid */
psid.rc = -1;
psid.num_entries = 0;
psid.num_referrals = 0;
psid.entry_list_head = NULL;
psid.referral_list_head = NULL;
/* setup additional pb data */
slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
/* coverity[var_deref_model] */
slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, pb_search_entries);
/* coverity[var_deref_model] */
slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_SEARCH_REFERRALS, pb_search_referrals);
/* call internal search callback, define search_entry_callback, and result_callback such
that the results of the search are stuffed into pb */
search_internal_callback_pb(pb, &psid, internal_plugin_result_callback,
internal_plugin_search_entry_callback,
internal_plugin_search_referral_callback);
opresult = psid.rc;
/* stuff search entry pointers from linked list to contiguous array for pblock */
pb_search_entries = (Slapi_Entry **)slapi_ch_calloc((psid.num_entries + 1), sizeof(Slapi_Entry *));
for (i = 0, iterator = psid.entry_list_head; iterator != NULL; iterator = iterator->next, i++) {
pb_search_entries[i] = iterator->data;
}
pb_search_entries[i] = NULL;
/* free the linked list now that data has been put in the array */
iterator = psid.entry_list_head;
while (iterator != NULL) {
/* free the data held in this node */
tmp = iterator;
iterator = iterator->next;
/* free the node */
if (tmp != NULL) {
slapi_ch_free((void **)&tmp);
}
}
psid.entry_list_head = NULL;
/* stuff referrals list into an array if we got any to put into the pblock */
if (psid.num_referrals != 0) {
pb_search_referrals = (char **)slapi_ch_calloc((psid.num_referrals + 1), sizeof(char *));
for (i = 0, ref_iterator = psid.referral_list_head; ref_iterator != NULL; ref_iterator = ref_iterator->next, i++) {
pb_search_referrals[i] = ref_iterator->data;
}
pb_search_referrals[i] = NULL;
/* free the linked list now that data has been put in the array */
ref_iterator = psid.referral_list_head;
while (ref_iterator != NULL) {
ref_tmp = ref_iterator;
ref_iterator = ref_iterator->next;
/* free the node */
if (ref_tmp != NULL) {
slapi_ch_free((void **)&ref_tmp);
}
}
psid.referral_list_head = NULL;
}
/* set the result, the array of entries, and the array of referrals in pb */
slapi_pblock_set(pb, SLAPI_NENTRIES, &psid.num_entries);
slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, pb_search_entries);
slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_SEARCH_REFERRALS, pb_search_referrals);
return 0;
}
static int
search_internal_callback_pb(Slapi_PBlock *pb, void *callback_data, plugin_result_callback prc, plugin_search_entry_callback psec, plugin_referral_entry_callback prec)
{
LDAPControl **controls;
Operation *op;
struct slapi_filter *filter = NULL;
char *fstr = NULL;
struct callback_fn_ptrs callback_handler_data;
int scope;
char *ifstr;
int opresult;
int rc = 0;
char **tmp_attrs = NULL;
PR_ASSERT(pb);
/* retrieve search parameters */
slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
slapi_pblock_get(pb, SLAPI_SEARCH_STRFILTER, &ifstr);
slapi_pblock_get(pb, SLAPI_CONTROLS_ARG, &controls);
/* data validation */
if (ifstr == NULL || (scope != LDAP_SCOPE_BASE && scope != LDAP_SCOPE_ONELEVEL && scope != LDAP_SCOPE_SUBTREE)) {
opresult = LDAP_PARAM_ERROR;
slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
return -1;
}
callback_handler_data.p_res_callback = prc;
callback_handler_data.p_srch_entry_callback = psec;
callback_handler_data.p_ref_entry_callback = prec;
callback_handler_data.callback_data = callback_data;
slapi_pblock_get(pb, SLAPI_OPERATION, &op);
op->o_handler_data = (void *)&callback_handler_data;
op->o_result_handler = internal_res_callback;
op->o_search_entry_handler = internal_srch_entry_callback;
op->o_search_referral_handler = internal_ref_entry_callback;
filter = slapi_str2filter((fstr = slapi_ch_strdup(ifstr)));
if (NULL == filter) {
int result = LDAP_FILTER_ERROR;
send_ldap_result(pb, result, NULL, NULL, 0, NULL);
slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
rc = -1;
goto done;
}
if (scope == LDAP_SCOPE_BASE) {
filter->f_flags |= (SLAPI_FILTER_LDAPSUBENTRY |
SLAPI_FILTER_TOMBSTONE | SLAPI_FILTER_RUV);
}
filter_normalize(filter);
slapi_pblock_set(pb, SLAPI_SEARCH_FILTER, filter);
slapi_pblock_set(pb, SLAPI_REQCONTROLS, controls);
/* set actions taken to process the operation */
set_config_params(pb);
/* set parameters common for all internal operations */
set_common_params(pb);
{
int timelimit = -1;
int sizelimit = -1;
int deref = LDAP_DEREF_ALWAYS;
slapi_pblock_set(pb, SLAPI_SEARCH_DEREF, &deref);
slapi_pblock_set(pb, SLAPI_SEARCH_TIMELIMIT, &timelimit);
slapi_pblock_set(pb, SLAPI_SEARCH_SIZELIMIT, &sizelimit);
}
/* plugins which play with the search may
* change the search params may allocate
* memory so we need to keep track of
* changed base search strings
*/
slapi_td_internal_op_start();
op_shared_search(pb, 1);
slapi_td_internal_op_finish();
slapi_pblock_get(pb, SLAPI_SEARCH_FILTER, &filter);
done:
slapi_ch_free_string(&fstr);
slapi_filter_free(filter, 1 /* recurse */);
slapi_pblock_get(pb, SLAPI_SEARCH_ATTRS, &tmp_attrs);
/* coverity[callee_ptr_arith] */
slapi_ch_array_free(tmp_attrs);
slapi_pblock_set(pb, SLAPI_SEARCH_ATTRS, NULL);
return (rc);
}
/* allow/disallow operation based of the plugin configuration */
PRBool
allow_operation(Slapi_PBlock *pb)
{
struct slapdplugin *plugin = NULL;
Slapi_DN *sdnp = NULL;
Slapi_DN sdn;
PRBool allow;
struct slapi_componentid *cid = NULL;
PR_ASSERT(pb);
/* make sure that users of new API provide plugin identity */
slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &cid);
if (cid == NULL) {
slapi_log_err(SLAPI_LOG_ERR, "allow_operation", "Component identity is NULL\n");
return PR_FALSE;
}
plugin = (struct slapdplugin *)cid->sci_plugin;
if (plugin == NULL) {
slapi_log_err(SLAPI_LOG_ERR, "allow_operation", "Plugin identity is NULL\n");
return PR_FALSE;
}
slapi_sdn_init(&sdn);
slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdnp);
if (NULL == sdnp) {
slapi_sdn_init_ndn_byval(&sdn, "");
sdnp = &sdn;
}
allow = plugin_allow_internal_op(sdnp, plugin);
slapi_sdn_done(&sdn);
return allow;
}
/* set operation configuration based on the plugin configuration */
void
set_config_params(Slapi_PBlock *pb)
{
Slapi_Operation *operation;
struct slapdplugin *plugin = NULL;
char *dn;
struct slapi_componentid *cid = NULL;
slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &cid);
if (cid)
plugin = (struct slapdplugin *)cid->sci_plugin;
/* set actions */
slapi_pblock_get(pb, SLAPI_OPERATION, &operation);
operation_set_flag(operation, plugin_build_operation_action_bitmap(operation->o_flags, plugin));
/* Check if we have a flag to keep the operation secret */
if (operation_is_flag_set(operation, OP_FLAG_ACTION_NOLOG)) {
/* Clear the LOG_AUDIT and LOG_CHANGES flag */
operation_clear_flag(operation, OP_FLAG_ACTION_LOG_AUDIT | OP_FLAG_ACTION_LOG_CHANGES);
}
/*
* if we are not tracking the bind dn, then use the plugin name as the
* modifiersname, otherwise, we have already set the op dn as the bind dn
*/
if (!slapdFrontendConfig->plugin_track || slapi_sdn_isempty(&operation->o_sdn)) {
/* set name to be used for creator's and modifiers attributes */
dn = plugin_get_dn(plugin);
if (dn)
slapi_sdn_init_dn_passin(&operation->o_sdn, dn);
}
}
/* set parameters common for all internal operations */
void
set_common_params(Slapi_PBlock *pb)
{
int isroot = 1;
LDAPControl **controls;
slapi_pblock_get(pb, SLAPI_CONTROLS_ARG, &controls);
if (NULL != controls) {
int managedsait = slapi_control_present(controls,
LDAP_CONTROL_MANAGEDSAIT, NULL, NULL);
int pwpolicy_ctrl = slapi_control_present(controls,
LDAP_X_CONTROL_PWPOLICY_REQUEST, NULL, NULL);
slapi_pblock_set(pb, SLAPI_MANAGEDSAIT, &managedsait);
slapi_pblock_set(pb, SLAPI_PWPOLICY, &pwpolicy_ctrl);
}
slapi_pblock_set(pb, SLAPI_REQUESTOR_ISROOT, &isroot);
}
/*
* Given a DN, find an entry by doing an internal search. An LDAP error
* code is returned. To check if an entry exists without returning a
* copy of the entry, NULL can be passed for ret_entry.
*/
int
slapi_search_internal_get_entry(Slapi_DN *dn, char **attrs, Slapi_Entry **ret_entry, void *component_identity)
{
Slapi_Entry **entries = NULL;
Slapi_PBlock *int_search_pb = NULL;
int rc = 0;
if (ret_entry) {
*ret_entry = NULL;
}
int_search_pb = slapi_pblock_new();
slapi_search_internal_set_pb(int_search_pb, slapi_sdn_get_dn(dn), LDAP_SCOPE_BASE, "(|(objectclass=*)(objectclass=ldapsubentry))",
attrs,
0 /* attrsonly */, NULL /* controls */,
NULL /* uniqueid */,
component_identity, 0 /* actions */);
slapi_search_internal_pb(int_search_pb);
slapi_pblock_get(int_search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
if (LDAP_SUCCESS == rc) {
slapi_pblock_get(int_search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
if (NULL != entries && NULL != entries[0]) {
/* Only need to dup the entry if the caller passed ret_entry in. */
if (ret_entry) {
Slapi_Entry *temp_entry = NULL;
temp_entry = entries[0];
*ret_entry = slapi_entry_dup(temp_entry);
}
} else {
/* No entry there */
rc = LDAP_NO_SUCH_OBJECT;
}
}
slapi_free_search_results_internal(int_search_pb);
slapi_pblock_destroy(int_search_pb);
int_search_pb = NULL;
return rc;
}
int32_t
slapi_search_get_entry(Slapi_PBlock **pb, Slapi_DN *dn, char **attrs, Slapi_Entry **ret_entry, void *component_identity)
{
Slapi_Entry **entries = NULL;
int32_t rc = 0;
void *component = component_identity;
if (ret_entry) {
*ret_entry = NULL;
}
if (component == NULL) {
component = (void *)plugin_get_default_component_id();
}
if (*pb == NULL) {
*pb = slapi_pblock_new();
}
slapi_search_internal_set_pb(*pb, slapi_sdn_get_dn(dn), LDAP_SCOPE_BASE,
"(|(objectclass=*)(objectclass=ldapsubentry))",
attrs, 0, NULL, NULL, component, 0 );
slapi_search_internal_pb(*pb);
slapi_pblock_get(*pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
if (LDAP_SUCCESS == rc) {
slapi_pblock_get(*pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
if (NULL != entries && NULL != entries[0]) {
/* Only need to dup the entry if the caller passed ret_entry in. */
if (ret_entry) {
*ret_entry = entries[0];
}
} else {
rc = LDAP_NO_SUCH_OBJECT;
}
}
return rc;
}
void
slapi_search_get_entry_done(Slapi_PBlock **pb)
{
if (pb && *pb) {
slapi_free_search_results_internal(*pb);
slapi_pblock_destroy(*pb);
*pb = NULL;
}
}
|