2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2007 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
27 #include <ac/socket.h>
28 #include <ac/string.h>
33 #include "../back-ldap/back-ldap.h"
34 #include "back-meta.h"
35 #undef ldap_debug /* silence a warning in ldap-int.h */
37 #include "../../../libraries/libldap/ldap-int.h"
39 /* IGNORE means that target does not (no longer) participate
41 * NOTREADY means the search on that target has not been initialized yet
43 #define META_MSGID_IGNORE (-1)
44 #define META_MSGID_NEED_BIND (-2)
54 typedef enum meta_search_candidate_t {
55 META_SEARCH_UNDEFINED = -2,
57 META_SEARCH_NOT_CANDIDATE,
58 META_SEARCH_CANDIDATE,
61 } meta_search_candidate_t;
64 * meta_search_dobind_init()
66 * initiates bind for a candidate target of a search.
68 static meta_search_candidate_t
69 meta_search_dobind_init(
74 SlapReply *candidates )
76 metaconn_t *mc = *mcp;
77 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
78 metatarget_t *mt = mi->mi_targets[ candidate ];
79 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
81 struct berval binddn = msc->msc_bound_ndn,
87 meta_search_candidate_t retcode;
89 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_search_dobind_init[%d]\n",
90 op->o_log_prefix, candidate, 0 );
93 * all the targets are already bound as pseudoroot
95 if ( mc->mc_authz_target == META_BOUND_ALL ) {
96 return META_SEARCH_CANDIDATE;
99 retcode = META_SEARCH_BINDING;
100 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
101 if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
102 /* already bound (or anonymous) */
105 char buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
108 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
112 snprintf( buf, sizeof( buf ), " mc=%p ld=%p%s DN=\"%s\"",
113 (void *)mc, (void *)msc->msc_ld,
114 bound ? " bound" : " anonymous",
115 bound == 0 ? "" : msc->msc_bound_ndn.bv_val );
116 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
117 op->o_log_prefix, candidate, buf );
118 #endif /* DEBUG_205 */
120 retcode = META_SEARCH_CANDIDATE;
122 } else if ( META_BACK_CONN_CREATING( msc ) || LDAP_BACK_CONN_BINDING( msc ) ) {
123 /* another thread is binding the target for this conn; wait */
126 char buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
128 snprintf( buf, sizeof( buf ), " mc=%p ld=%p needbind",
129 (void *)mc, (void *)msc->msc_ld );
130 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
131 op->o_log_prefix, candidate, buf );
132 #endif /* DEBUG_205 */
134 candidates[ candidate ].sr_msgid = META_MSGID_NEED_BIND;
135 retcode = META_SEARCH_NEED_BIND;
138 /* we'll need to bind the target for this conn */
141 char buf[ SLAP_TEXT_BUFLEN ];
143 snprintf( buf, sizeof( buf ), " mc=%p ld=%p binding",
144 (void *)mc, (void *)msc->msc_ld );
145 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
146 op->o_log_prefix, candidate, buf );
147 #endif /* DEBUG_205 */
149 if ( msc->msc_ld == NULL ) {
150 /* for some reason (e.g. because formerly in "binding"
151 * state, with eventual connection expiration or invalidation)
152 * it was not initialized as expected */
154 Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p ld=NULL\n",
155 op->o_log_prefix, candidate, (void *)mc );
157 rc = meta_back_init_one_conn( op, rs, *mcp, candidate,
158 LDAP_BACK_CONN_ISPRIV( *mcp ), LDAP_BACK_DONTSEND, 0 );
161 assert( msc->msc_ld != NULL );
164 case LDAP_SERVER_DOWN:
165 case LDAP_UNAVAILABLE:
166 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
170 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
175 LDAP_BACK_CONN_BINDING_SET( msc );
178 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
180 if ( retcode != META_SEARCH_BINDING ) {
184 /* NOTE: this obsoletes pseudorootdn */
185 if ( op->o_conn != NULL &&
186 !op->o_do_not_cache &&
187 ( BER_BVISNULL( &msc->msc_bound_ndn ) ||
188 BER_BVISEMPTY( &msc->msc_bound_ndn ) ||
189 ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
191 rc = meta_back_proxy_authz_cred( mc, candidate, op, rs, LDAP_BACK_DONTSEND, &binddn, &cred, &method );
192 if ( rc != LDAP_SUCCESS ) {
196 /* NOTE: we copy things here, even if bind didn't succeed yet,
197 * because the connection is not shared until bind is over */
198 if ( !BER_BVISNULL( &binddn ) ) {
199 ber_bvreplace( &msc->msc_bound_ndn, &binddn );
200 if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &cred ) ) {
201 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
202 memset( msc->msc_cred.bv_val, 0,
203 msc->msc_cred.bv_len );
205 ber_bvreplace( &msc->msc_cred, &cred );
209 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
210 /* apparently, idassert was configured with SASL bind,
211 * so bind occurred inside meta_back_proxy_authz_cred() */
212 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
213 LDAP_BACK_CONN_BINDING_CLEAR( msc );
214 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
215 return META_SEARCH_CANDIDATE;
221 case LDAP_AUTH_SIMPLE:
222 /* do a simple bind with binddn, cred */
231 assert( msc->msc_ld != NULL );
233 rc = ldap_sasl_bind( msc->msc_ld, binddn.bv_val, LDAP_SASL_SIMPLE, &cred,
234 NULL, NULL, &candidates[ candidate ].sr_msgid );
238 char buf[ SLAP_TEXT_BUFLEN ];
240 snprintf( buf, sizeof( buf ), "meta_search_dobind_init[%d] mc=%p ld=%p rc=%d",
241 candidate, (void *)mc, (void *)mc->mc_conns[ candidate ].msc_ld, rc );
242 Debug( LDAP_DEBUG_ANY, "### %s %s\n",
243 op->o_log_prefix, buf, 0 );
245 #endif /* DEBUG_205 */
249 assert( candidates[ candidate ].sr_msgid >= 0 );
250 META_BINDING_SET( &candidates[ candidate ] );
251 return META_SEARCH_BINDING;
253 case LDAP_SERVER_DOWN:
255 /* This is the worst thing that could happen:
256 * the search will wait until the retry is over. */
257 if ( meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
258 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
259 return META_SEARCH_CANDIDATE;
262 if ( *mcp == NULL ) {
263 retcode = META_SEARCH_ERR;
264 rs->sr_err = LDAP_UNAVAILABLE;
272 rc = slap_map_api2result( rs );
274 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
275 meta_clear_one_candidate( op, mc, candidate );
276 candidates[ candidate ].sr_err = rc;
277 if ( META_BACK_ONERR_STOP( mi ) ) {
278 LDAP_BACK_CONN_TAINTED_SET( mc );
279 meta_back_release_conn_lock( op, mc, 0 );
283 retcode = META_SEARCH_ERR;
286 retcode = META_SEARCH_NOT_CANDIDATE;
288 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
289 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
296 static meta_search_candidate_t
297 meta_search_dobind_result(
302 SlapReply *candidates,
305 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
306 metaconn_t *mc = *mcp;
307 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
309 meta_search_candidate_t retcode = META_SEARCH_NOT_CANDIDATE;
312 assert( msc->msc_ld != NULL );
314 /* FIXME: matched? referrals? response controls? */
315 rc = ldap_parse_result( msc->msc_ld, res,
316 &candidates[ candidate ].sr_err,
317 NULL, NULL, NULL, NULL, 0 );
318 if ( rc != LDAP_SUCCESS ) {
319 candidates[ candidate ].sr_err = rc;
322 rc = slap_map_api2result( &candidates[ candidate ] );
325 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
326 LDAP_BACK_CONN_BINDING_CLEAR( msc );
327 if ( rc != LDAP_SUCCESS ) {
328 meta_clear_one_candidate( op, mc, candidate );
329 candidates[ candidate ].sr_err = rc;
330 if ( META_BACK_ONERR_STOP( mi ) ) {
331 LDAP_BACK_CONN_TAINTED_SET( mc );
332 meta_back_release_conn_lock( op, mc, 0 );
334 retcode = META_SEARCH_ERR;
339 /* FIXME: check if bound as idassert authcDN! */
340 if ( BER_BVISNULL( &msc->msc_bound_ndn )
341 || BER_BVISEMPTY( &msc->msc_bound_ndn ) )
343 LDAP_BACK_CONN_ISANON_SET( msc );
346 LDAP_BACK_CONN_ISBOUND_SET( msc );
348 retcode = META_SEARCH_CANDIDATE;
351 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
352 META_BINDING_CLEAR( &candidates[ candidate ] );
354 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
359 static meta_search_candidate_t
360 meta_back_search_start(
366 SlapReply *candidates )
368 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
369 metatarget_t *mt = mi->mi_targets[ candidate ];
370 metasingleconn_t *msc = &(*mcp)->mc_conns[ candidate ];
371 struct berval realbase = op->o_req_dn;
372 int realscope = op->ors_scope;
373 struct berval mbase = BER_BVNULL;
374 struct berval mfilter = BER_BVNULL;
375 char **mapped_attrs = NULL;
377 meta_search_candidate_t retcode;
378 struct timeval tv, *tvp = NULL;
380 LDAPControl **ctrls = NULL;
382 /* this should not happen; just in case... */
383 if ( msc->msc_ld == NULL ) {
384 Debug( LDAP_DEBUG_ANY,
385 "%s: meta_back_search_start candidate=%d ld=NULL%s.\n",
386 op->o_log_prefix, candidate,
387 META_BACK_ONERR_STOP( mi ) ? "" : " (ignored)" );
388 candidates[ candidate ].sr_err = LDAP_OTHER;
389 if ( META_BACK_ONERR_STOP( mi ) ) {
390 return META_SEARCH_ERR;
392 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
393 return META_SEARCH_NOT_CANDIDATE;
396 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_back_search_start[%d]\n", op->o_log_prefix, candidate, 0 );
399 * modifies the base according to the scope, if required
401 if ( mt->mt_nsuffix.bv_len > op->o_req_ndn.bv_len ) {
402 switch ( op->ors_scope ) {
403 case LDAP_SCOPE_SUBTREE:
405 * make the target suffix the new base
406 * FIXME: this is very forgiving, because
407 * "illegal" searchBases may be turned
408 * into the suffix of the target; however,
409 * the requested searchBase already passed
410 * thru the candidate analyzer...
412 if ( dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) ) {
413 realbase = mt->mt_nsuffix;
414 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
415 realscope = LDAP_SCOPE_SUBORDINATE;
420 * this target is no longer candidate
422 retcode = META_SEARCH_NOT_CANDIDATE;
427 case LDAP_SCOPE_SUBORDINATE:
428 case LDAP_SCOPE_ONELEVEL:
430 struct berval rdn = mt->mt_nsuffix;
431 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
432 if ( dnIsOneLevelRDN( &rdn )
433 && dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) )
436 * if there is exactly one level,
437 * make the target suffix the new
438 * base, and make scope "base"
440 realbase = mt->mt_nsuffix;
441 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
442 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
443 realscope = LDAP_SCOPE_SUBORDINATE;
445 realscope = LDAP_SCOPE_SUBTREE;
448 realscope = LDAP_SCOPE_BASE;
451 } /* else continue with the next case */
454 case LDAP_SCOPE_BASE:
456 * this target is no longer candidate
458 retcode = META_SEARCH_NOT_CANDIDATE;
463 /* initiate dobind */
464 retcode = meta_search_dobind_init( op, rs, mcp, candidate, candidates );
466 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%d]=%d\n", op->o_log_prefix, candidate, retcode );
468 if ( retcode != META_SEARCH_CANDIDATE ) {
473 * Rewrite the search base, if required
476 dc->ctx = "searchBase";
477 switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
481 case LDAP_UNWILLING_TO_PERFORM:
482 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
483 rs->sr_text = "Operation not allowed";
484 send_ldap_result( op, rs );
485 retcode = META_SEARCH_ERR;
491 * this target is no longer candidate
493 retcode = META_SEARCH_NOT_CANDIDATE;
500 rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
501 &mfilter, BACKLDAP_MAP );
506 case LDAP_COMPARE_FALSE:
509 * this target is no longer candidate
511 retcode = META_SEARCH_NOT_CANDIDATE;
516 * Maps required attributes
518 rc = ldap_back_map_attrs( &mt->mt_rwmap.rwm_at,
519 op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
520 if ( rc != LDAP_SUCCESS ) {
522 * this target is no longer candidate
524 retcode = META_SEARCH_NOT_CANDIDATE;
528 /* should we check return values? */
529 if ( op->ors_deref != -1 ) {
530 assert( msc->msc_ld != NULL );
531 (void)ldap_set_option( msc->msc_ld, LDAP_OPT_DEREF,
532 ( void * )&op->ors_deref );
535 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
536 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
543 if ( ldap_back_proxy_authz_ctrl( &msc->msc_bound_ndn,
544 mt->mt_version, &mt->mt_idassert, op, rs, &ctrls )
547 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
548 retcode = META_SEARCH_NOT_CANDIDATE;
555 assert( msc->msc_ld != NULL );
556 rc = ldap_search_ext( msc->msc_ld,
557 mbase.bv_val, realscope, mfilter.bv_val,
558 mapped_attrs, op->ors_attrsonly,
559 ctrls, NULL, tvp, op->ors_slimit,
560 &candidates[ candidate ].sr_msgid );
563 retcode = META_SEARCH_CANDIDATE;
566 case LDAP_SERVER_DOWN:
567 if ( nretries && meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
569 /* if the identity changed, there might be need to re-authz */
570 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
574 if ( *mcp == NULL ) {
575 retcode = META_SEARCH_ERR;
581 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
582 retcode = META_SEARCH_NOT_CANDIDATE;
586 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
588 if ( mapped_attrs ) {
589 free( mapped_attrs );
591 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
592 free( mfilter.bv_val );
594 if ( mbase.bv_val != realbase.bv_val ) {
595 free( mbase.bv_val );
599 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_back_search_start[%d]=%d\n", op->o_log_prefix, candidate, retcode );
605 meta_back_search( Operation *op, SlapReply *rs )
607 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
609 struct timeval save_tv = { 0, 0 },
611 time_t stoptime = (time_t)-1;
612 int rc = 0, sres = LDAP_SUCCESS;
613 char *matched = NULL;
614 int last = 0, ncandidates = 0,
615 initial_candidates = 0, candidate_match = 0,
617 ldap_back_send_t sendok = LDAP_BACK_SENDERR;
622 SlapReply *candidates = meta_back_candidates_get( op );
625 * controls are set in ldap_back_dobind()
627 * FIXME: in case of values return filter, we might want
628 * to map attrs and maybe rewrite value
631 mc = meta_back_getconn( op, rs, NULL, sendok );
636 dc.conn = op->o_conn;
642 for ( i = 0; i < mi->mi_ntargets; i++ ) {
643 /* reset sr_msgid; it is used in most loops
644 * to check if that target is still to be considered */
645 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
647 /* a target is marked as candidate by meta_back_getconn();
648 * if for any reason (an error, it's over or so) it is
649 * no longer active, sr_msgid is set to META_MSGID_IGNORE
650 * but it remains candidate, which means it has been active
651 * at some point during the operation. This allows to
652 * use its response code and more to compute the final
654 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
658 candidates[ i ].sr_matched = NULL;
659 candidates[ i ].sr_text = NULL;
660 candidates[ i ].sr_ref = NULL;
661 candidates[ i ].sr_ctrls = NULL;
664 for ( i = 0; i < mi->mi_ntargets; i++ ) {
665 if ( !META_IS_CANDIDATE( &candidates[ i ] )
666 || candidates[ i ].sr_err != LDAP_SUCCESS )
671 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
673 case META_SEARCH_NOT_CANDIDATE:
674 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
677 case META_SEARCH_NEED_BIND:
681 case META_SEARCH_CANDIDATE:
682 case META_SEARCH_BINDING:
683 candidates[ i ].sr_type = REP_INTERMEDIATE;
687 case META_SEARCH_ERR:
688 savepriv = op->o_private;
689 op->o_private = (void *)i;
690 send_ldap_result( op, rs );
691 op->o_private = savepriv;
701 if ( ncandidates > 0 && needbind == ncandidates ) {
703 * give up the second time...
705 * NOTE: this should not occur the second time, since a fresh
706 * connection has ben created; however, targets may also
707 * need bind because the bind timed out or so.
709 if ( sendok & LDAP_BACK_BINDING ) {
710 Debug( LDAP_DEBUG_ANY,
711 "%s meta_back_search: unable to initialize conn\n",
712 op->o_log_prefix, 0, 0 );
713 rs->sr_err = LDAP_UNAVAILABLE;
714 rs->sr_text = "unable to initialize connection to remote targets";
715 send_ldap_result( op, rs );
720 /* FIXME: better create a separate connection? */
721 sendok |= LDAP_BACK_BINDING;
724 Debug( LDAP_DEBUG_ANY, "*** %s drop mc=%p create new connection\n",
725 op->o_log_prefix, (void *)mc, 0 );
726 #endif /* DEBUG_205 */
728 meta_back_release_conn( op, mc );
737 initial_candidates = ncandidates;
739 if ( StatslogTest( LDAP_DEBUG_TRACE ) ) {
740 char cnd[ SLAP_TEXT_BUFLEN ];
743 for ( c = 0; c < mi->mi_ntargets; c++ ) {
744 if ( META_IS_CANDIDATE( &candidates[ c ] ) ) {
752 Debug( LDAP_DEBUG_TRACE, "%s meta_back_search: ncandidates=%d "
753 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
756 if ( initial_candidates == 0 ) {
757 /* NOTE: here we are not sending any matchedDN;
758 * this is intended, because if the back-meta
759 * is serving this search request, but no valid
760 * candidate could be looked up, it means that
761 * there is a hole in the mapping of the targets
762 * and thus no knowledge of any remote superior
764 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
765 "base=\"%s\" scope=%d: "
766 "no candidate could be selected\n",
767 op->o_log_prefix, op->o_req_dn.bv_val,
770 /* FIXME: we're sending the first error we encounter;
771 * maybe we should pick the worst... */
772 rc = LDAP_NO_SUCH_OBJECT;
773 for ( i = 0; i < mi->mi_ntargets; i++ ) {
774 if ( META_IS_CANDIDATE( &candidates[ i ] )
775 && candidates[ i ].sr_err != LDAP_SUCCESS )
777 rc = candidates[ i ].sr_err;
782 send_ldap_error( op, rs, rc, NULL );
787 /* We pull apart the ber result, stuff it into a slapd entry, and
788 * let send_search_entry stuff it back into ber format. Slow & ugly,
789 * but this is necessary for version matching, and for ACL processing.
792 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
793 stoptime = op->o_time + op->ors_tlimit;
797 * In case there are no candidates, no cycle takes place...
799 * FIXME: we might use a queue, to better balance the load
800 * among the candidates
802 for ( rc = 0; ncandidates > 0; ) {
805 alreadybound = ncandidates;
807 /* check time limit */
808 if ( op->ors_tlimit != SLAP_NO_LIMIT
809 && slap_get_time() > stoptime )
812 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
813 savepriv = op->o_private;
814 op->o_private = (void *)i;
815 send_ldap_result( op, rs );
816 op->o_private = savepriv;
820 for ( i = 0; i < mi->mi_ntargets; i++ ) {
821 meta_search_candidate_t retcode = META_SEARCH_UNDEFINED;
822 metasingleconn_t *msc = &mc->mc_conns[ i ];
823 LDAPMessage *res = NULL, *msg;
825 /* if msgid is invalid, don't ldap_result() */
826 if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
830 /* if target still needs bind, retry */
831 if ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND ) {
832 /* initiate dobind */
833 retcode = meta_search_dobind_init( op, rs, &mc, i, candidates );
835 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%ld]=%d\n",
836 op->o_log_prefix, i, retcode );
839 case META_SEARCH_NEED_BIND:
843 case META_SEARCH_BINDING:
846 case META_SEARCH_ERR:
847 candidates[ i ].sr_err = rs->sr_err;
848 if ( META_BACK_ONERR_STOP( mi ) ) {
849 savepriv = op->o_private;
850 op->o_private = (void *)i;
851 send_ldap_result( op, rs );
852 op->o_private = savepriv;
857 case META_SEARCH_NOT_CANDIDATE:
859 * When no candidates are left,
860 * the outer cycle finishes
862 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
863 assert( ncandidates > 0 );
867 case META_SEARCH_CANDIDATE:
868 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
869 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
871 case META_SEARCH_CANDIDATE:
872 assert( candidates[ i ].sr_msgid >= 0 );
875 case META_SEARCH_ERR:
876 candidates[ i ].sr_err = rs->sr_err;
877 if ( META_BACK_ONERR_STOP( mi ) ) {
878 savepriv = op->o_private;
879 op->o_private = (void *)i;
880 send_ldap_result( op, rs );
881 op->o_private = savepriv;
886 case META_SEARCH_NOT_CANDIDATE:
887 /* means that meta_back_search_start()
888 * failed but onerr == continue */
889 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
890 assert( ncandidates > 0 );
909 /* check for abandon */
910 if ( op->o_abandon ) {
915 if ( msc->msc_ld == NULL ) {
916 char buf[ SLAP_TEXT_BUFLEN ];
918 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
919 snprintf( buf, sizeof( buf ),
920 "%s meta_back_search[%ld] mc=%p msgid=%d%s%s%s\n",
921 op->o_log_prefix, (long)i, (void *)mc,
922 candidates[ i ].sr_msgid,
923 META_IS_BINDING( &candidates[ i ] ) ? " binding" : "",
924 LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ? " connbinding" : "",
925 META_BACK_CONN_CREATING( &mc->mc_conns[ i ] ) ? " conncreating" : "" );
926 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
928 Debug( LDAP_DEBUG_ANY, "!!! %s\n", buf, 0, 0 );
930 #endif /* DEBUG_205 */
933 * FIXME: handle time limit as well?
934 * Note that target servers are likely
935 * to handle it, so at some time we'll
936 * get a LDAP_TIMELIMIT_EXCEEDED from
940 rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
941 LDAP_MSG_RECEIVED, &tv, &res );
944 /* FIXME: res should not need to be freed */
945 assert( res == NULL );
950 /* something REALLY bad happened! */
951 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
952 candidates[ i ].sr_type = REP_RESULT;
954 if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
955 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
956 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
958 /* means that failed but onerr == continue */
959 case META_SEARCH_NOT_CANDIDATE:
960 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
962 assert( ncandidates > 0 );
965 candidates[ i ].sr_err = rs->sr_err;
966 if ( META_BACK_ONERR_STOP( mi ) ) {
967 savepriv = op->o_private;
968 op->o_private = (void *)i;
969 send_ldap_result( op, rs );
970 op->o_private = savepriv;
975 case META_SEARCH_CANDIDATE:
976 /* get back into business... */
979 case META_SEARCH_BINDING:
980 case META_SEARCH_NEED_BIND:
981 case META_SEARCH_UNDEFINED:
985 /* unrecoverable error */
986 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
987 rc = rs->sr_err = LDAP_OTHER;
992 candidates[ i ].sr_err = rs->sr_err;
993 if ( META_BACK_ONERR_STOP( mi ) ) {
994 savepriv = op->o_private;
995 op->o_private = (void *)i;
996 send_ldap_result( op, rs );
997 op->o_private = savepriv;
1003 * When no candidates are left,
1004 * the outer cycle finishes
1006 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1007 assert( ncandidates > 0 );
1009 rs->sr_err = candidates[ i ].sr_err;
1013 /* only touch when activity actually took place... */
1014 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
1015 msc->msc_time = op->o_time;
1020 for ( msg = ldap_first_message( msc->msc_ld, res );
1022 msg = ldap_next_message( msc->msc_ld, msg ) )
1024 rc = ldap_msgtype( msg );
1025 if ( rc == LDAP_RES_SEARCH_ENTRY ) {
1028 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1029 /* don't retry any more... */
1030 candidates[ i ].sr_type = REP_RESULT;
1035 e = ldap_first_entry( msc->msc_ld, msg );
1036 savepriv = op->o_private;
1037 op->o_private = (void *)i;
1038 rs->sr_err = meta_send_entry( op, rs, mc, i, e );
1040 switch ( rs->sr_err ) {
1041 case LDAP_SIZELIMIT_EXCEEDED:
1042 savepriv = op->o_private;
1043 op->o_private = (void *)i;
1044 send_ldap_result( op, rs );
1045 op->o_private = savepriv;
1046 rs->sr_err = LDAP_SUCCESS;
1047 ldap_msgfree( res );
1051 case LDAP_UNAVAILABLE:
1052 rs->sr_err = LDAP_OTHER;
1053 ldap_msgfree( res );
1057 op->o_private = savepriv;
1059 /* don't wait any longer... */
1062 save_tv.tv_usec = 0;
1064 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
1065 char **references = NULL;
1068 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1069 /* don't retry any more... */
1070 candidates[ i ].sr_type = REP_RESULT;
1075 rc = ldap_parse_reference( msc->msc_ld, msg,
1076 &references, &rs->sr_ctrls, 0 );
1078 if ( rc != LDAP_SUCCESS ) {
1082 if ( references == NULL ) {
1086 #ifdef ENABLE_REWRITE
1087 dc.ctx = "referralDN";
1088 #else /* ! ENABLE_REWRITE */
1091 #endif /* ! ENABLE_REWRITE */
1093 /* FIXME: merge all and return at the end */
1095 for ( cnt = 0; references[ cnt ]; cnt++ )
1098 rs->sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
1100 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1101 ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
1103 BER_BVZERO( &rs->sr_ref[ cnt ] );
1105 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref );
1107 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
1108 /* ignore return value by now */
1109 savepriv = op->o_private;
1110 op->o_private = (void *)i;
1111 ( void )send_search_reference( op, rs );
1112 op->o_private = savepriv;
1114 ber_bvarray_free( rs->sr_ref );
1120 ber_memvfree( (void **)references );
1123 if ( rs->sr_ctrls ) {
1124 ldap_controls_free( rs->sr_ctrls );
1125 rs->sr_ctrls = NULL;
1128 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
1129 char buf[ SLAP_TEXT_BUFLEN ];
1130 char **references = NULL;
1132 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1133 /* don't retry any more... */
1134 candidates[ i ].sr_type = REP_RESULT;
1137 /* NOTE: ignores response controls
1138 * (and intermediate response controls
1139 * as well, except for those with search
1140 * references); this may not be correct,
1141 * but if they're not ignored then
1142 * back-meta would need to merge them
1143 * consistently (think of pagedResults...)
1145 /* FIXME: response controls? */
1146 rs->sr_err = ldap_parse_result( msc->msc_ld,
1148 &candidates[ i ].sr_err,
1149 (char **)&candidates[ i ].sr_matched,
1150 NULL /* (char **)&candidates[ i ].sr_text */ ,
1152 NULL /* &candidates[ i ].sr_ctrls (unused) */ ,
1154 if ( rs->sr_err != LDAP_SUCCESS ) {
1155 ldap_get_option( msc->msc_ld,
1156 LDAP_OPT_ERROR_NUMBER,
1158 sres = slap_map_api2result( rs );
1159 candidates[ i ].sr_type = REP_RESULT;
1160 ldap_msgfree( res );
1165 /* massage matchedDN if need be */
1166 if ( candidates[ i ].sr_matched != NULL ) {
1167 struct berval match, mmatch;
1169 ber_str2bv( candidates[ i ].sr_matched,
1171 candidates[ i ].sr_matched = NULL;
1173 dc.ctx = "matchedDN";
1174 dc.target = mi->mi_targets[ i ];
1175 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
1176 if ( mmatch.bv_val == match.bv_val ) {
1177 candidates[ i ].sr_matched
1178 = ch_strdup( mmatch.bv_val );
1181 candidates[ i ].sr_matched = mmatch.bv_val;
1186 ldap_memfree( match.bv_val );
1189 /* add references to array */
1194 for ( cnt = 0; references[ cnt ]; cnt++ )
1197 sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
1199 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1200 ber_str2bv( references[ cnt ], 0, 1, &sr_ref[ cnt ] );
1202 BER_BVZERO( &sr_ref[ cnt ] );
1204 ( void )ldap_back_referral_result_rewrite( &dc, sr_ref );
1207 ber_memvfree( (void **)references );
1209 if ( rs->sr_v2ref == NULL ) {
1210 rs->sr_v2ref = sr_ref;
1213 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
1214 ber_bvarray_add( &rs->sr_v2ref, &sr_ref[ cnt ] );
1216 ber_memfree( sr_ref );
1220 rs->sr_err = candidates[ i ].sr_err;
1221 sres = slap_map_api2result( rs );
1223 if ( StatslogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
1224 snprintf( buf, sizeof( buf ),
1225 "%s meta_back_search[%ld] "
1226 "match=\"%s\" err=%ld",
1227 op->o_log_prefix, i,
1228 candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
1229 (long) candidates[ i ].sr_err );
1230 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1231 Debug( LDAP_DEBUG_TRACE, "%s.\n", buf, 0, 0 );
1234 Debug( LDAP_DEBUG_ANY, "%s (%s).\n",
1235 buf, ldap_err2string( candidates[ i ].sr_err ), 0 );
1240 case LDAP_NO_SUCH_OBJECT:
1241 /* is_ok is touched any time a valid
1242 * (even intermediate) result is
1243 * returned; as a consequence, if
1244 * a candidate returns noSuchObject
1245 * it is ignored and the candidate
1246 * is simply demoted. */
1248 sres = LDAP_SUCCESS;
1257 case LDAP_SIZELIMIT_EXCEEDED:
1258 /* if a target returned sizelimitExceeded
1259 * and the entry count is equal to the
1260 * proxy's limit, the target would have
1261 * returned more, and the error must be
1262 * propagated to the client; otherwise,
1263 * the target enforced a limit lower
1264 * than what requested by the proxy;
1266 candidates[ i ].sr_err = rs->sr_err;
1267 if ( rs->sr_nentries == op->ors_slimit
1268 || META_BACK_ONERR_STOP( mi ) )
1270 savepriv = op->o_private;
1271 op->o_private = (void *)i;
1272 send_ldap_result( op, rs );
1273 op->o_private = savepriv;
1274 ldap_msgfree( res );
1281 candidates[ i ].sr_err = rs->sr_err;
1282 if ( META_BACK_ONERR_STOP( mi ) ) {
1283 savepriv = op->o_private;
1284 op->o_private = (void *)i;
1285 send_ldap_result( op, rs );
1286 op->o_private = savepriv;
1287 ldap_msgfree( res );
1298 * When no candidates are left,
1299 * the outer cycle finishes
1301 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1302 assert( ncandidates > 0 );
1305 } else if ( rc == LDAP_RES_BIND ) {
1306 meta_search_candidate_t retcode;
1308 retcode = meta_search_dobind_result( op, rs, &mc, i, candidates, msg );
1309 if ( retcode == META_SEARCH_CANDIDATE ) {
1310 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1311 retcode = meta_back_search_start( op, rs, &dc, &mc, i, candidates );
1314 switch ( retcode ) {
1315 case META_SEARCH_CANDIDATE:
1318 /* means that failed but onerr == continue */
1319 case META_SEARCH_NOT_CANDIDATE:
1320 case META_SEARCH_ERR:
1321 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1322 assert( ncandidates > 0 );
1325 candidates[ i ].sr_err = rs->sr_err;
1326 if ( META_BACK_ONERR_STOP( mi ) ) {
1327 savepriv = op->o_private;
1328 op->o_private = (void *)i;
1329 send_ldap_result( op, rs );
1330 op->o_private = savepriv;
1331 ldap_msgfree( res );
1344 ldap_msgfree( res );
1351 ldap_msgfree( res );
1355 /* check for abandon */
1356 if ( op->o_abandon || doabandon ) {
1357 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1358 if ( candidates[ i ].sr_msgid >= 0 ) {
1359 if ( META_IS_BINDING( &candidates[ i ] ) ) {
1360 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1361 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ) {
1362 /* if still binding, destroy */
1365 char buf[ SLAP_TEXT_BUFLEN ];
1367 snprintf( buf, sizeof( buf), "%s meta_back_search(abandon) "
1368 "ldap_unbind_ext[%ld] mc=%p ld=%p",
1369 op->o_log_prefix, i, (void *)mc,
1370 (void *)mc->mc_conns[i].msc_ld );
1372 Debug( LDAP_DEBUG_ANY, "### %s\n", buf, 0, 0 );
1373 #endif /* DEBUG_205 */
1375 meta_clear_one_candidate( op, mc, i );
1377 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1378 META_BINDING_CLEAR( &candidates[ i ] );
1381 (void)meta_back_cancel( mc, op, rs,
1382 candidates[ i ].sr_msgid, i,
1383 LDAP_BACK_DONTSEND );
1386 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1387 assert( ncandidates > 0 );
1392 if ( op->o_abandon ) {
1394 /* let send_ldap_result play cleanup handlers (ITS#4645) */
1399 /* if no entry was found during this loop,
1400 * set a minimal timeout */
1401 if ( ncandidates > 0 && gotit == 0 ) {
1402 if ( save_tv.tv_sec == 0 && save_tv.tv_usec == 0 ) {
1403 save_tv.tv_usec = LDAP_BACK_RESULT_UTIMEOUT/initial_candidates;
1405 /* arbitrarily limit to something between 1 and 2 minutes */
1406 } else if ( ( stoptime == -1 && save_tv.tv_sec < 60 )
1407 || save_tv.tv_sec < ( stoptime - slap_get_time() ) / ( 2 * ncandidates ) )
1409 /* double the timeout */
1410 lutil_timermul( &save_tv, 2, &save_tv );
1414 if ( StatslogTest( LDAP_DEBUG_TRACE ) ) {
1415 char buf[ SLAP_TEXT_BUFLEN ];
1417 snprintf( buf, sizeof( buf ), "%s %ld.%06ld %d/%d mc=%p",
1418 op->o_log_prefix, save_tv.tv_sec, save_tv.tv_usec,
1419 ncandidates, initial_candidates, mc );
1420 Debug( LDAP_DEBUG_TRACE, "### %s\n", buf, 0, 0 );
1421 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1422 if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
1426 snprintf( buf, sizeof( buf ), "[%ld] ld=%p%s%s\n",
1428 mc->mc_conns[ i ].msc_ld,
1429 ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND ) ? " needbind" : "",
1430 META_IS_BINDING( &candidates[ i ] ) ? " binding" : "" );
1431 Debug( LDAP_DEBUG_TRACE, "### %s\n", buf, 0, 0 );
1436 if ( alreadybound == 0 ) {
1438 Debug( LDAP_DEBUG_TRACE, "### %s select(%ld.%06ld)\n",
1439 op->o_log_prefix, save_tv.tv_sec, save_tv.tv_usec );
1442 (void)select( 0, NULL, NULL, NULL, &tv );
1445 ldap_pvt_thread_yield();
1452 * FIXME: need a better strategy to handle errors
1455 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE,
1456 -1, stoptime != -1 ? (stoptime - slap_get_time()) : 0,
1457 LDAP_BACK_SENDERR );
1465 * Rewrite the matched portion of the search base, if required
1467 * FIXME: only the last one gets caught!
1469 savepriv = op->o_private;
1470 op->o_private = (void *)(long)mi->mi_ntargets;
1471 if ( candidate_match > 0 ) {
1472 struct berval pmatched = BER_BVNULL;
1474 /* we use the first one */
1475 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1476 if ( META_IS_CANDIDATE( &candidates[ i ] )
1477 && candidates[ i ].sr_matched != NULL )
1479 struct berval bv, pbv;
1482 /* if we got success, and this target
1483 * returned noSuchObject, and its suffix
1484 * is a superior of the searchBase,
1485 * ignore the matchedDN */
1486 if ( sres == LDAP_SUCCESS
1487 && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
1488 && op->o_req_ndn.bv_len > mi->mi_targets[ i ]->mt_nsuffix.bv_len )
1490 free( (char *)candidates[ i ].sr_matched );
1491 candidates[ i ].sr_matched = NULL;
1495 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
1496 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
1498 if ( rc == LDAP_SUCCESS ) {
1500 /* NOTE: if they all are superiors
1501 * of the baseDN, the shorter is also
1502 * superior of the longer... */
1503 if ( pbv.bv_len > pmatched.bv_len ) {
1504 if ( !BER_BVISNULL( &pmatched ) ) {
1505 op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
1508 op->o_private = (void *)i;
1511 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
1515 if ( candidates[ i ].sr_matched != NULL ) {
1516 free( (char *)candidates[ i ].sr_matched );
1517 candidates[ i ].sr_matched = NULL;
1522 if ( !BER_BVISNULL( &pmatched ) ) {
1523 matched = pmatched.bv_val;
1526 } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
1527 matched = op->o_bd->be_suffix[ 0 ].bv_val;
1532 char buf[ SLAP_TEXT_BUFLEN ];
1533 char cnd[ SLAP_TEXT_BUFLEN ];
1536 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1537 if ( META_IS_CANDIDATE( &candidates[ i ] ) ) {
1545 snprintf( buf, sizeof( buf ), "%s meta_back_search: is_scope=%d is_ok=%d cnd=\"%s\"\n",
1546 op->o_log_prefix, initial_candidates, is_ok, cnd );
1548 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1553 * In case we returned at least one entry, we return LDAP_SUCCESS
1554 * otherwise, the latter error code we got
1557 if ( sres == LDAP_SUCCESS ) {
1558 if ( rs->sr_v2ref ) {
1559 sres = LDAP_REFERRAL;
1562 if ( META_BACK_ONERR_REPORT( mi ) ) {
1564 * Report errors, if any
1566 * FIXME: we should handle error codes and return the more
1567 * important/reasonable
1569 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1570 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1574 if ( candidates[ i ].sr_err != LDAP_SUCCESS
1575 && candidates[ i ].sr_err != LDAP_NO_SUCH_OBJECT )
1577 sres = candidates[ i ].sr_err;
1585 rs->sr_matched = matched;
1586 rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
1587 send_ldap_result( op, rs );
1588 op->o_private = savepriv;
1589 rs->sr_matched = NULL;
1593 if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
1594 op->o_tmpfree( matched, op->o_tmpmemctx );
1597 if ( rs->sr_v2ref ) {
1598 ber_bvarray_free( rs->sr_v2ref );
1601 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1602 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1606 if ( mc && META_IS_BINDING( &candidates[ i ] ) ) {
1607 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1608 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ) {
1609 assert( candidates[ i ].sr_msgid >= 0 );
1610 assert( mc->mc_conns[ i ].msc_ld != NULL );
1613 Debug( LDAP_DEBUG_ANY, "### %s meta_back_search(cleanup) "
1614 "ldap_unbind_ext[%ld] ld=%p\n",
1615 op->o_log_prefix, i, (void *)mc->mc_conns[i].msc_ld );
1616 #endif /* DEBUG_205 */
1618 /* if still binding, destroy */
1619 meta_clear_one_candidate( op, mc, i );
1621 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1622 META_BINDING_CLEAR( &candidates[ i ] );
1625 if ( candidates[ i ].sr_matched ) {
1626 free( (char *)candidates[ i ].sr_matched );
1627 candidates[ i ].sr_matched = NULL;
1630 if ( candidates[ i ].sr_text ) {
1631 ldap_memfree( (char *)candidates[ i ].sr_text );
1632 candidates[ i ].sr_text = NULL;
1635 if ( candidates[ i ].sr_ref ) {
1636 ber_bvarray_free( candidates[ i ].sr_ref );
1637 candidates[ i ].sr_ref = NULL;
1640 if ( candidates[ i ].sr_ctrls ) {
1641 ldap_controls_free( candidates[ i ].sr_ctrls );
1642 candidates[ i ].sr_ctrls = NULL;
1645 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1646 meta_back_quarantine( op, &candidates[ i ], i );
1649 /* only in case of timelimit exceeded, if the timelimit exceeded because
1650 * one contacted target never responded, invalidate the connection
1651 * NOTE: should we quarantine the target as well? right now, the connection
1652 * is invalidated; the next time it will be recreated and the target
1653 * will be quarantined if it cannot be contacted */
1654 if ( mi->mi_idle_timeout != 0
1655 && rs->sr_err == LDAP_TIMELIMIT_EXCEEDED
1656 && op->o_time > mc->mc_conns[ i ].msc_time )
1658 /* don't let anyone else use this expired connection */
1659 LDAP_BACK_CONN_TAINTED_SET( mc );
1664 meta_back_release_conn( op, mc );
1678 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1679 struct berval a, mapped;
1681 BerElement ber = *e->lm_ber;
1682 Attribute *attr, **attrp;
1689 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
1690 return LDAP_DECODING_ERROR;
1694 * Rewrite the dn of the result, if needed
1696 dc.target = mi->mi_targets[ target ];
1697 dc.conn = op->o_conn;
1699 dc.ctx = "searchResult";
1701 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
1702 if ( rs->sr_err != LDAP_SUCCESS) {
1707 * Note: this may fail if the target host(s) schema differs
1708 * from the one known to the meta, and a DN with unknown
1709 * attributes is returned.
1711 * FIXME: should we log anything, or delegate to dnNormalize?
1713 rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
1715 if ( dn.bv_val != bdn.bv_val ) {
1720 if ( rc != LDAP_SUCCESS ) {
1721 return LDAP_INVALID_DN_SYNTAX;
1727 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1728 ( void )meta_dncache_update_entry( &mi->mi_cache,
1729 &ent.e_nname, target );
1732 attrp = &ent.e_attrs;
1734 dc.ctx = "searchAttrDN";
1735 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
1737 slap_syntax_validate_func *validate;
1738 slap_syntax_transform_func *pretty;
1740 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_at,
1741 &a, &mapped, BACKLDAP_REMAP );
1742 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
1743 ( void )ber_scanf( &ber, "x" /* [W] */ );
1746 attr = ( Attribute * )ch_calloc( 1, sizeof( Attribute ) );
1747 if ( attr == NULL ) {
1750 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
1752 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
1753 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
1755 char buf[ SLAP_TEXT_BUFLEN ];
1757 snprintf( buf, sizeof( buf ),
1758 "%s meta_send_entry(\"%s\"): "
1759 "slap_bv2undef_ad(%s): %s\n",
1760 op->o_log_prefix, ent.e_name.bv_val,
1761 mapped.bv_val, text );
1763 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1769 /* no subschemaSubentry */
1770 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
1771 || attr->a_desc == slap_schema.si_ad_entryDN )
1775 * We eat target's subschemaSubentry because
1776 * a search for this value is likely not
1777 * to resolve to the appropriate backend;
1778 * later, the local subschemaSubentry is
1781 * We also eat entryDN because the frontend
1782 * will reattach it without checking if already
1785 ( void )ber_scanf( &ber, "x" /* [W] */ );
1791 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
1792 || attr->a_vals == NULL )
1794 attr->a_vals = (struct berval *)&slap_dummy_bv;
1797 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
1801 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
1802 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
1804 if ( !validate && !pretty ) {
1809 if ( attr->a_desc == slap_schema.si_ad_objectClass
1810 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
1814 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1815 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_oc,
1816 bv, &mapped, BACKLDAP_REMAP );
1817 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
1823 *bv = attr->a_vals[ last ];
1824 BER_BVZERO( &attr->a_vals[ last ] );
1827 } else if ( mapped.bv_val != bv->bv_val ) {
1829 ber_dupbv( bv, &mapped );
1833 * It is necessary to try to rewrite attributes with
1834 * dn syntax because they might be used in ACLs as
1835 * members of groups; since ACLs are applied to the
1836 * rewritten stuff, no dn-based subecj clause could
1837 * be used at the ldap backend side (see
1838 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1839 * The problem can be overcome by moving the dn-based
1840 * ACLs to the target directory server, and letting
1841 * everything pass thru the ldap backend.
1846 if ( attr->a_desc->ad_type->sat_syntax ==
1847 slap_schema.si_syn_distinguishedName )
1849 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
1851 } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
1852 ldap_back_referral_result_rewrite( &dc, attr->a_vals );
1856 for ( i = 0; i < last; i++ ) {
1861 rc = pretty( attr->a_desc->ad_type->sat_syntax,
1862 &attr->a_vals[i], &pval, NULL );
1865 rc = validate( attr->a_desc->ad_type->sat_syntax,
1870 LBER_FREE( attr->a_vals[i].bv_val );
1871 if ( --last == i ) {
1872 BER_BVZERO( &attr->a_vals[ i ] );
1875 attr->a_vals[i] = attr->a_vals[last];
1876 BER_BVZERO( &attr->a_vals[last] );
1882 LBER_FREE( attr->a_vals[i].bv_val );
1883 attr->a_vals[i] = pval;
1887 if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
1893 if ( last && attr->a_desc->ad_type->sat_equality &&
1894 attr->a_desc->ad_type->sat_equality->smr_normalize )
1898 attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
1899 for ( i = 0; i<last; i++ ) {
1900 attr->a_desc->ad_type->sat_equality->smr_normalize(
1901 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1902 attr->a_desc->ad_type->sat_syntax,
1903 attr->a_desc->ad_type->sat_equality,
1904 &attr->a_vals[i], &attr->a_nvals[i],
1907 BER_BVZERO( &attr->a_nvals[i] );
1910 attr->a_nvals = attr->a_vals;
1914 attrp = &attr->a_next;
1917 rs->sr_entry = &ent;
1918 rs->sr_attrs = op->ors_attrs;
1920 rs->sr_err = LDAP_SUCCESS;
1921 rc = send_search_entry( op, rs );
1923 case LDAP_UNAVAILABLE:
1927 rs->sr_entry = NULL;
1928 rs->sr_attrs = NULL;
1930 if ( !BER_BVISNULL( &ent.e_name ) ) {
1931 free( ent.e_name.bv_val );
1932 BER_BVZERO( &ent.e_name );
1934 if ( !BER_BVISNULL( &ent.e_nname ) ) {
1935 free( ent.e_nname.bv_val );
1936 BER_BVZERO( &ent.e_nname );
1938 entry_clean( &ent );