1 /* lcup.c - lcup operations */
3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
15 #ifdef LDAP_CLIENT_UPDATE
24 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
26 LDAP_LIST_FOREACH ( ps_list, &bdb->psearch_list, link ) {
27 if ( ps_list->o_connid == conn->c_connid ) {
28 if ( ps_list->o_msgid == id ) {
29 ps_list->o_abandon = 1;
30 LDAP_LIST_REMOVE( ps_list, link );
31 slap_op_free ( ps_list );
36 return LDAP_UNAVAILABLE;
55 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
57 LDAP_LIST_FIRST(&op->psearch_spec) = (struct lcup_search_spec *)
58 calloc ( 1, sizeof ( struct lcup_search_spec ) );
60 LDAP_LIST_FIRST(&op->psearch_spec)->op = op;
62 LDAP_LIST_FIRST(&op->psearch_spec)->base = ber_dupbv(NULL, base);
63 LDAP_LIST_FIRST(&op->psearch_spec)->nbase = ber_dupbv(NULL, nbase);
65 LDAP_LIST_FIRST(&op->psearch_spec)->scope = scope;
66 LDAP_LIST_FIRST(&op->psearch_spec)->deref = deref;
67 LDAP_LIST_FIRST(&op->psearch_spec)->slimit = slimit;
68 LDAP_LIST_FIRST(&op->psearch_spec)->tlimit = tlimit;
70 LDAP_LIST_FIRST(&op->psearch_spec)->filter = filter;
71 LDAP_LIST_FIRST(&op->psearch_spec)->filterstr = ber_dupbv(NULL, fstr);
72 LDAP_LIST_FIRST(&op->psearch_spec)->attrs = attrs;
74 LDAP_LIST_FIRST(&op->psearch_spec)->attrsonly = attrsonly;
76 LDAP_LIST_FIRST(&op->psearch_spec)->entry_count = 0;
78 LDAP_LIST_INSERT_HEAD( &bdb->psearch_list, op, link );
90 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
92 const char *text = NULL;
96 ID candidates[BDB_IDL_UM_SIZE];
98 BerVarray v2refs = NULL;
99 Entry *matched = NULL;
100 struct berval realbase = { 0, NULL };
104 Filter lcupf, csnfnot, csnfeq, csnfand, csnfge;
105 AttributeAssertion aa_ge, aa_eq;
106 struct berval entrycsn_bv = { 0, NULL };
107 struct berval latest_entrycsn_bv = { 0, NULL };
109 struct slap_limits_set *limit = NULL;
116 Connection *ps_conn = ps_op->o_conn;
117 struct berval *base = LDAP_LIST_FIRST(&ps_op->psearch_spec)->base;
118 struct berval *nbase = LDAP_LIST_FIRST(&ps_op->psearch_spec)->nbase;
119 int scope = LDAP_LIST_FIRST(&ps_op->psearch_spec)->scope;
120 int deref = LDAP_LIST_FIRST(&ps_op->psearch_spec)->deref;
121 int slimit = LDAP_LIST_FIRST(&ps_op->psearch_spec)->slimit;
122 int tlimit = LDAP_LIST_FIRST(&ps_op->psearch_spec)->tlimit;
123 Filter *filter = LDAP_LIST_FIRST(&ps_op->psearch_spec)->filter;
124 struct berval *filterstr = LDAP_LIST_FIRST(&ps_op->psearch_spec)->filterstr;
125 int attrsonly = LDAP_LIST_FIRST(&ps_op->psearch_spec)->attrsonly;
126 AttributeName *attrs;
128 if ( psearch_type != LCUP_PSEARCH_BY_DELETE &&
129 psearch_type != LCUP_PSEARCH_BY_SCOPEOUT ) {
130 attrs = LDAP_LIST_FIRST(&ps_op->psearch_spec)->attrs;
136 LDAP_LOG ( OPERATION, ENTRY, "bdb_back_search\n", 0, 0, 0 );
138 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
142 manageDSAit = get_manageDSAit( ps_op );
144 rc = LOCK_ID (bdb->bi_dbenv, &locker );
149 send_ldap_result( ps_conn, ps_op, rc=LDAP_OTHER,
150 NULL, "internal error", NULL, NULL );
154 if ( nbase->bv_len == 0 ) {
155 /* DIT root special case */
156 e = (Entry *) &slap_entry_root;
160 /* get entry with reader lock */
161 if ( deref & LDAP_DEREF_FINDING ) {
162 e = deref_dn_r( be, nbase-, &err, &matched, &text );
168 rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0, locker, &lock );
177 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
179 if (matched != NULL) {
180 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
182 send_ldap_result( ps_conn, ps_op, LDAP_BUSY,
183 NULL, "ldap server busy", NULL, NULL );
184 LOCK_ID_FREE (bdb->bi_dbenv, locker );
186 case DB_LOCK_DEADLOCK:
187 case DB_LOCK_NOTGRANTED:
191 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
193 if (matched != NULL) {
194 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
196 send_ldap_result( ps_conn, ps_op, rc=LDAP_OTHER,
197 NULL, "internal error", NULL, NULL );
198 LOCK_ID_FREE (bdb->bi_dbenv, locker );
203 struct berval matched_dn = { 0, NULL };
204 BerVarray refs = NULL;
206 if ( matched != NULL ) {
208 ber_dupbv( &matched_dn, &matched->e_name );
210 erefs = is_entry_referral( matched )
211 ? get_entry_referrals( be, ps_conn, ps_op, matched )
214 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
218 refs = referral_rewrite( erefs, &matched_dn,
220 ber_bvarray_free( erefs );
224 refs = referral_rewrite( default_referral,
228 send_ldap_result( ps_conn, ps_op, rc=LDAP_REFERRAL ,
229 matched_dn.bv_val, text, refs, NULL );
231 LOCK_ID_FREE (bdb->bi_dbenv, locker );
232 if ( refs ) ber_bvarray_free( refs );
233 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
237 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
238 /* entry is a referral, don't allow add */
239 struct berval matched_dn;
240 BerVarray erefs, refs;
242 ber_dupbv( &matched_dn, &e->e_name );
243 erefs = get_entry_referrals( be, ps_conn, ps_op, e );
246 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
250 refs = referral_rewrite( erefs, &matched_dn,
252 ber_bvarray_free( erefs );
256 LDAP_LOG ( OPERATION, RESULTS,
257 "bdb_search: entry is referral\n", 0, 0, 0 );
259 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
263 send_ldap_result( ps_conn, ps_op, LDAP_REFERRAL,
265 refs ? NULL : "bad referral object",
268 LOCK_ID_FREE (bdb->bi_dbenv, locker );
269 ber_bvarray_free( refs );
270 ber_memfree( matched_dn.bv_val );
274 /* if not root, get appropriate limits */
275 if ( be_isroot( be, &ps_op->o_ndn ) ) {
278 ( void ) get_limits( be, &ps_op->o_ndn, &limit );
281 /* The time/size limits come first because they require very little
282 * effort, so there's no chance the candidates are selected and then
283 * the request is not honored only because of time/size constraints */
285 /* if no time limit requested, use soft limit (unless root!) */
288 tlimit = -1; /* allow root to set no limit */
296 /* if no limit is required, use soft limit */
298 tlimit = limit->lms_t_soft;
300 /* if requested limit higher than hard limit, abort */
301 } else if ( tlimit > limit->lms_t_hard ) {
302 /* no hard limit means use soft instead */
303 if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
304 tlimit = limit->lms_t_soft;
306 /* positive hard limit means abort */
307 } else if ( limit->lms_t_hard > 0 ) {
308 send_search_result( ps_conn, ps_op,
309 LDAP_UNWILLING_TO_PERFORM,
310 NULL, NULL, NULL, NULL, 0 );
315 /* negative hard limit means no limit */
318 /* if no limit is required, use soft limit */
320 slimit = limit->lms_s_soft;
322 /* if requested limit higher than hard limit, abort */
323 } else if ( slimit > limit->lms_s_hard ) {
324 /* no hard limit means use soft instead */
325 if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
326 slimit = limit->lms_s_soft;
328 /* positive hard limit means abort */
329 } else if ( limit->lms_s_hard > 0 ) {
330 send_search_result( ps_conn, ps_op,
331 LDAP_UNWILLING_TO_PERFORM,
332 NULL, NULL, NULL, NULL, 0 );
337 /* negative hard limit means no limit */
341 /* compute it anyway; root does not use it */
342 stoptime = ps_op->o_time + tlimit;
344 /* candidates = { e } */
346 candidates[1] = entry->e_id;
348 /* need normalized dn below */
349 ber_dupbv( &realbase, &e->e_nname );
351 if ( e != &slap_entry_root ) {
352 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
356 if ( candidates[0] == 0 ) {
358 LDAP_LOG ( OPERATION, RESULTS,
359 "bdb_search: no candidates\n", 0, 0, 0 );
361 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
365 send_search_result( ps_conn, ps_op,
367 NULL, NULL, NULL, NULL, 0 );
373 /* if not root and candidates exceed to-be-checked entries, abort */
374 if ( !isroot && limit->lms_s_unchecked != -1 ) {
375 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
376 send_search_result( ps_conn, ps_op,
377 LDAP_ADMINLIMIT_EXCEEDED,
378 NULL, NULL, NULL, NULL, 0 );
384 lcupf.f_choice = LDAP_FILTER_AND;
385 lcupf.f_and = &csnfnot;
388 csnfnot.f_choice = LDAP_FILTER_NOT;
389 csnfnot.f_not = &csnfeq;
390 csnfnot.f_next = &csnfand;
392 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
393 csnfeq.f_ava = &aa_eq;
394 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
395 ber_dupbv( &csnfeq.f_av_value, &ps_op->o_clientupdate_state );
397 csnfand.f_choice = LDAP_FILTER_AND;
398 csnfand.f_and = &csnfge;
399 csnfand.f_next = NULL;
401 csnfge.f_choice = LDAP_FILTER_GE;
402 csnfge.f_ava = &aa_ge;
403 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
404 ber_dupbv( &csnfge.f_av_value, &ps_op->o_clientupdate_state );
405 csnfge.f_next = filter;
407 if ( !BDB_IDL_IS_RANGE( candidates ) ) {
408 cursor = bdb_idl_search( candidates, entry->e_id );
409 if ( candidates[cursor] != entry->e_id ) {
414 if ( entry->e_id < BDB_IDL_RANGE_FIRST(candidates) &&
415 entry->e_id > BDB_IDL_RANGE_LAST(candidates) ) {
423 /* check for abandon */
424 if ( ps_op->o_abandon ) {
429 /* check time limit */
430 if ( tlimit != -1 && slap_get_time() > stoptime ) {
431 send_search_result( ps_conn, ps_op, rc = LDAP_TIMELIMIT_EXCEEDED,
432 NULL, NULL, v2refs, NULL, nentries );
438 #ifdef BDB_SUBENTRIES
439 if ( is_entry_subentry( e ) ) {
440 if( scope != LDAP_SCOPE_BASE ) {
441 if(!get_subentries_visibility( ps_op )) {
442 /* only subentries are visible */
446 } else if ( get_subentries( ps_op ) &&
447 !get_subentries_visibility( ps_op ))
449 /* only subentries are visible */
453 } else if ( get_subentries_visibility( ps_op )) {
454 /* only subentries are visible */
460 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
465 e = deref_entry_r( be, e, &err, &matched, &text );
472 if( e->e_id == id ) {
477 /* need to skip alias which deref into scope */
478 if( scope & LDAP_SCOPE_ONELEVEL ) {
481 dnParent( &e->e_nname, &pdn ):
482 if ( ber_bvcmp( pdn, &realbase ) ) {
486 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
487 /* alias is within scope */
489 LDAP_LOG ( OPERATION, RESULTS,
490 "bdb_search: \"%s\" in subtree\n", e->edn, 0, 0);
492 Debug( LDAP_DEBUG_TRACE,
493 "bdb_search: \"%s\" in subtree\n",
504 * if it's a referral, add it to the list of referrals. only do
505 * this for non-base searches, and don't check the filter
506 * explicitly here since it's only a candidate anyway.
508 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
509 is_entry_referral( e ) )
514 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
515 if ( !be_issuffix( be, &e->e_nname ) ) {
516 dnParent( &e->e_nname, &dn );
517 scopeok = dn_match( &dn, &realbase );
519 scopeok = (realbase.bv_len == 0);
522 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
523 scopeok = dnIsSuffix( &e->e_nname, &realbase );
530 BerVarray erefs = get_entry_referrals(
531 be, ps_conn, ps_op, e );
532 BerVarray refs = referral_rewrite( erefs,
534 scope == LDAP_SCOPE_SUBTREE
538 send_search_reference( be, ps_conn, ps_op,
539 e, refs, NULL, &v2refs );
541 ber_bvarray_free( refs );
545 LDAP_LOG(OPERATION, DETAIL2,
546 "bdb_search: candidate referral %ld scope not okay\n",
549 Debug( LDAP_DEBUG_TRACE,
550 "bdb_search: candidate referral %ld scope not okay\n",
558 if ( psearch_type != LCUP_PSEARCH_BY_SCOPEOUT ) {
559 rc = test_filter( be, ps_conn, ps_op, e, &lcupf );
561 rc = LDAP_COMPARE_TRUE;
564 if ( rc == LDAP_COMPARE_TRUE ) {
568 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
569 if ( be_issuffix( be, &e->e_nname ) ) {
570 scopeok = (realbase.bv_len == 0);
572 dnParent( &e->e_nname, &dn );
573 scopeok = dn_match( &dn, &realbase );
576 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
577 scopeok = dnIsSuffix( &e->e_nname, &realbase );
584 /* check size limit */
585 if ( --slimit == -1 ) {
586 send_search_result( ps_conn, ps_op,
587 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
588 v2refs, NULL, nentries );
595 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
596 if( ps_op->o_noop ) {
601 if ( psearch_type == LCUP_PSEARCH_BY_ADD ||
602 psearch_type == LCUP_PSEARCH_BY_DELETE ||
603 psearch_type == LCUP_PSEARCH_BY_MODIFY ||
604 psearch_type == LCUP_PSEARCH_BY_SCOPEOUT ) {
608 const char *text = NULL;
609 LDAPControl *ctrls[2];
612 BerElement *ber = ber_alloc_t( LBER_USE_DER );
616 LDAP_LOG ( OPERATION, RESULTS,
617 "bdb_search: ber_alloc_t failed\n",
620 Debug( LDAP_DEBUG_TRACE,
621 "bdb_search: ber_alloc_t failed\n",
624 send_ldap_result( ps_conn, ps_op, rc=LDAP_OTHER,
625 NULL, "internal error", NULL, NULL );
629 LDAP_LIST_FIRST(&ps_op->psearch_spec)->entry_count++;
631 ctrls[0] = ch_malloc ( sizeof ( LDAPControl ) );
634 if ( LDAP_LIST_FIRST(&ps_op->psearch_spec)->entry_count % ps_op->o_clientupdate_interval == 0 ) {
636 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
637 AttributeDescription *desc = a->a_desc;
638 if ( desc == slap_schema.si_ad_entryCSN ) {
639 ber_dupbv( &entrycsn_bv, &a->a_vals[0] );
640 if ( latest_entrycsn_bv.bv_val == NULL ) {
641 ber_dupbv( &latest_entrycsn_bv, &entrycsn_bv );
643 res = value_match( &ret, desc,
644 desc->ad_type->sat_ordering,
645 SLAP_MR_ASSERTION_SYNTAX_MATCH,
646 &entrycsn_bv, &latest_entrycsn_bv, &text );
647 if ( res != LDAP_SUCCESS ) {
650 LDAP_LOG ( OPERATION, RESULTS,
651 "bdb_search: value_match failed\n",
654 Debug( LDAP_DEBUG_TRACE,
655 "bdb_search: value_match failed\n",
661 ch_free( latest_entrycsn_bv.bv_val );
662 latest_entrycsn_bv.bv_val = NULL;
663 ber_dupbv( &latest_entrycsn_bv,
670 if ( psearch_type != LCUP_PSEARCH_BY_DELETE || psearch_type != LCUP_PSEARCH_BY_SCOPEOUT ) {
673 SLAP_LCUP_STATE_UPDATE_FALSE,
674 SLAP_LCUP_ENTRY_DELETED_FALSE,
675 LCUP_COOKIE_OID, &entrycsn_bv );
679 SLAP_LCUP_STATE_UPDATE_FALSE,
680 SLAP_LCUP_ENTRY_DELETED_TRUE,
681 LCUP_COOKIE_OID, &entrycsn_bv );
684 ch_free( entrycsn_bv.bv_val );
685 entrycsn_bv.bv_val = NULL;
688 /* Do not send cookie */
689 if ( psearch_type != LCUP_PSEARCH_BY_DELETE || psearch_type != LCUP_PSEARCH_BY_SCOPEOUT ) {
692 SLAP_LCUP_STATE_UPDATE_FALSE,
693 SLAP_LCUP_ENTRY_DELETED_FALSE );
697 SLAP_LCUP_STATE_UPDATE_FALSE,
698 SLAP_LCUP_ENTRY_DELETED_TRUE );
702 ctrls[0]->ldctl_oid = LDAP_CONTROL_ENTRY_UPDATE;
703 ctrls[0]->ldctl_iscritical = ps_op->o_clientupdate;
704 ret = ber_flatten( ber, &bv );
708 LDAP_LOG ( OPERATION, RESULTS,
709 "bdb_search: ber_flatten failed\n",
712 Debug( LDAP_DEBUG_TRACE,
713 "bdb_search: ber_flatten failed\n",
716 send_ldap_result( ps_conn, ps_op, rc=LDAP_OTHER,
717 NULL, "internal error", NULL, NULL );
721 ber_dupbv( &ctrls[0]->ldctl_value, bv );
723 result = send_search_entry( be, ps_conn, ps_op,
724 e, attrs, attrsonly, ctrls);
726 ch_free( ctrls[0]->ldctl_value.bv_val );
731 if ( psearch_type == LCUP_PSEARCH_BY_MODIFY ) {
732 struct psid_entry* psid_e;
733 LDAP_LIST_FOREACH( psid_e, &op->premodify_list, link) {
734 if (psid_e->ps == LDAP_LIST_FIRST(&ps_op->psearch_spec)) {
735 LDAP_LIST_REMOVE(psid_e, link);
742 } else if ( psearch_type == LCUP_PSEARCH_BY_PREMODIFY ) {
743 struct psid_entry* psid_e;
744 psid_e = (struct psid_entry *) calloc (1, sizeof (struct psid_entry));
745 psid_e->ps = LDAP_LIST_FIRST(&ps_op->psearch_spec);
746 LDAP_LIST_INSERT_HEAD( &op->premodify_list, psid_e, link );
755 case 0: /* entry sent ok */
758 case 1: /* entry not sent */
760 case -1: /* connection closed */
767 LDAP_LOG ( OPERATION, RESULTS,
768 "bdb_search: %ld scope not okay\n", (long) id, 0, 0);
770 Debug( LDAP_DEBUG_TRACE,
771 "bdb_search: %ld scope not okay\n",
777 LDAP_LOG ( OPERATION, RESULTS,
778 "bdb_search: %ld does match filter\n", (long) id, 0, 0);
780 Debug( LDAP_DEBUG_TRACE,
781 "bdb_search: %ld does match filter\n",
790 if ( csnfeq.f_ava != NULL && csnfeq.f_av_value.bv_val != NULL ) {
791 ch_free( csnfeq.f_av_value.bv_val );
794 if ( csnfge.f_ava != NULL && csnfge.f_av_value.bv_val != NULL ) {
795 ch_free( csnfge.f_av_value.bv_val );
798 LOCK_ID_FREE (bdb->bi_dbenv, locker );
800 if( v2refs ) ber_bvarray_free( v2refs );
801 if( realbase.bv_val ) ch_free( realbase.bv_val );
806 #endif /* LDAP_CLIENT_UPDATE */