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 {
56 META_SEARCH_NOT_CANDIDATE,
57 META_SEARCH_CANDIDATE,
60 } meta_search_candidate_t;
63 * meta_search_dobind_init()
65 * initiates bind for a candidate target of a search.
67 static meta_search_candidate_t
68 meta_search_dobind_init(
73 SlapReply *candidates )
75 metaconn_t *mc = *mcp;
76 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
77 metatarget_t *mt = mi->mi_targets[ candidate ];
78 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
80 struct berval binddn = msc->msc_bound_ndn,
86 meta_search_candidate_t retcode;
88 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_search_dobind_init[%d]\n",
89 op->o_log_prefix, candidate, 0 );
92 * all the targets are already bound as pseudoroot
94 if ( mc->mc_authz_target == META_BOUND_ALL ) {
95 return META_SEARCH_CANDIDATE;
98 retcode = META_SEARCH_BINDING;
99 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
100 if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
101 /* already bound (or anonymous) */
104 char buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
107 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
111 snprintf( buf, sizeof( buf ), " mc=%p ld=%p%s DN=\"%s\"",
112 (void *)mc, (void *)msc->msc_ld,
113 bound ? " bound" : " anonymous",
114 bound == 0 ? "" : msc->msc_bound_ndn.bv_val );
115 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
116 op->o_log_prefix, candidate, buf );
117 #endif /* DEBUG_205 */
119 retcode = META_SEARCH_CANDIDATE;
121 } else if ( META_BACK_CONN_CREATING( msc ) || LDAP_BACK_CONN_BINDING( msc ) ) {
122 /* another thread is binding the target for this conn; wait */
125 char buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
127 snprintf( buf, sizeof( buf ), " mc=%p ld=%p needbind",
128 (void *)mc, (void *)msc->msc_ld );
129 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
130 op->o_log_prefix, candidate, buf );
131 #endif /* DEBUG_205 */
133 candidates[ candidate ].sr_msgid = META_MSGID_NEED_BIND;
134 retcode = META_SEARCH_NEED_BIND;
137 /* we'll need to bind the target for this conn */
140 char buf[ SLAP_TEXT_BUFLEN ];
142 snprintf( buf, sizeof( buf ), " mc=%p ld=%p binding",
143 (void *)mc, (void *)msc->msc_ld );
144 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
145 op->o_log_prefix, candidate, buf );
146 #endif /* DEBUG_205 */
148 if ( msc->msc_ld == NULL ) {
149 /* for some reason (e.g. because formerly in "binding"
150 * state, with eventual connection expiration or invalidation)
151 * it was not initialized as expected */
153 Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p ld=NULL\n",
154 op->o_log_prefix, candidate, (void *)mc );
156 rc = meta_back_init_one_conn( op, rs, *mcp, candidate,
157 LDAP_BACK_CONN_ISPRIV( *mcp ), LDAP_BACK_DONTSEND, 0 );
160 assert( msc->msc_ld != NULL );
163 case LDAP_SERVER_DOWN:
164 case LDAP_UNAVAILABLE:
165 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
169 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
174 LDAP_BACK_CONN_BINDING_SET( msc );
177 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
179 if ( retcode != META_SEARCH_BINDING ) {
183 /* NOTE: this obsoletes pseudorootdn */
184 if ( op->o_conn != NULL &&
185 !op->o_do_not_cache &&
186 ( BER_BVISNULL( &msc->msc_bound_ndn ) ||
187 BER_BVISEMPTY( &msc->msc_bound_ndn ) ||
188 ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
190 rc = meta_back_proxy_authz_cred( mc, candidate, op, rs, LDAP_BACK_DONTSEND, &binddn, &cred, &method );
191 if ( rc != LDAP_SUCCESS ) {
195 /* NOTE: we copy things here, even if bind didn't succeed yet,
196 * because the connection is not shared until bind is over */
197 if ( !BER_BVISNULL( &binddn ) ) {
198 ber_bvreplace( &msc->msc_bound_ndn, &binddn );
199 if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &cred ) ) {
200 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
201 memset( msc->msc_cred.bv_val, 0,
202 msc->msc_cred.bv_len );
204 ber_bvreplace( &msc->msc_cred, &cred );
208 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
209 /* apparently, idassert was configured with SASL bind,
210 * so bind occurred inside meta_back_proxy_authz_cred() */
211 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
212 LDAP_BACK_CONN_BINDING_CLEAR( msc );
213 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
214 return META_SEARCH_CANDIDATE;
220 case LDAP_AUTH_SIMPLE:
221 /* do a simple bind with binddn, cred */
230 assert( msc->msc_ld != NULL );
232 /* connect must be async */
234 ldap_set_option( msc->msc_ld, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_ON );
236 rc = ldap_sasl_bind( msc->msc_ld, binddn.bv_val, LDAP_SASL_SIMPLE, &cred,
237 NULL, NULL, &candidates[ candidate ].sr_msgid );
241 char buf[ SLAP_TEXT_BUFLEN ];
243 snprintf( buf, sizeof( buf ), "meta_search_dobind_init[%d] mc=%p ld=%p rc=%d",
244 candidate, (void *)mc, (void *)mc->mc_conns[ candidate ].msc_ld, rc );
245 Debug( LDAP_DEBUG_ANY, "### %s %s\n",
246 op->o_log_prefix, buf, 0 );
248 #endif /* DEBUG_205 */
252 assert( candidates[ candidate ].sr_msgid >= 0 );
253 META_BINDING_SET( &candidates[ candidate ] );
254 return META_SEARCH_BINDING;
256 case LDAP_X_CONNECTING:
257 /* must retry, same conn */
258 candidates[ candidate ].sr_msgid = META_MSGID_NEED_BIND;
259 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
260 LDAP_BACK_CONN_BINDING_CLEAR( msc );
261 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
262 return META_SEARCH_NEED_BIND;
264 case LDAP_SERVER_DOWN:
266 /* This is the worst thing that could happen:
267 * the search will wait until the retry is over. */
268 if ( !META_IS_RETRYING( &candidates[ candidate ] ) ) {
269 META_RETRYING_SET( &candidates[ candidate ] );
271 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
273 assert( mc->mc_refcnt > 0 );
274 if ( LogTest( LDAP_DEBUG_ANY ) ) {
275 char buf[ SLAP_TEXT_BUFLEN ];
277 /* this lock is required; however,
278 * it's invoked only when logging is on */
279 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
280 snprintf( buf, sizeof( buf ),
281 "retrying URI=\"%s\" DN=\"%s\"",
283 BER_BVISNULL( &msc->msc_bound_ndn ) ?
284 "" : msc->msc_bound_ndn.bv_val );
285 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
287 Debug( LDAP_DEBUG_ANY,
288 "%s meta_search_dobind_init[%d]: %s.\n",
289 op->o_log_prefix, candidate, buf );
292 meta_clear_one_candidate( op, mc, candidate );
293 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
295 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
297 /* mc here must be the regular mc, reset and ready for init */
298 rc = meta_back_init_one_conn( op, rs, mc, candidate,
299 LDAP_BACK_CONN_ISPRIV( mc ), LDAP_BACK_DONTSEND, 0 );
301 LDAP_BACK_CONN_BINDING_SET( msc );
302 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
307 if ( *mcp == NULL ) {
308 retcode = META_SEARCH_ERR;
309 rs->sr_err = LDAP_UNAVAILABLE;
317 rc = slap_map_api2result( rs );
319 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
320 meta_clear_one_candidate( op, mc, candidate );
321 candidates[ candidate ].sr_err = rc;
322 if ( META_BACK_ONERR_STOP( mi ) ) {
323 LDAP_BACK_CONN_TAINTED_SET( mc );
324 meta_back_release_conn_lock( op, mc, 0 );
328 retcode = META_SEARCH_ERR;
331 retcode = META_SEARCH_NOT_CANDIDATE;
333 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
334 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
341 static meta_search_candidate_t
342 meta_search_dobind_result(
347 SlapReply *candidates,
350 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
351 metaconn_t *mc = *mcp;
352 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
354 meta_search_candidate_t retcode = META_SEARCH_NOT_CANDIDATE;
357 assert( msc->msc_ld != NULL );
359 /* FIXME: matched? referrals? response controls? */
360 rc = ldap_parse_result( msc->msc_ld, res,
361 &candidates[ candidate ].sr_err,
362 NULL, NULL, NULL, NULL, 0 );
363 if ( rc != LDAP_SUCCESS ) {
364 candidates[ candidate ].sr_err = rc;
367 rc = slap_map_api2result( &candidates[ candidate ] );
370 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
371 LDAP_BACK_CONN_BINDING_CLEAR( msc );
372 if ( rc != LDAP_SUCCESS ) {
373 meta_clear_one_candidate( op, mc, candidate );
374 candidates[ candidate ].sr_err = rc;
375 if ( META_BACK_ONERR_STOP( mi ) ) {
376 LDAP_BACK_CONN_TAINTED_SET( mc );
377 meta_back_release_conn_lock( op, mc, 0 );
379 retcode = META_SEARCH_ERR;
384 /* FIXME: check if bound as idassert authcDN! */
385 if ( BER_BVISNULL( &msc->msc_bound_ndn )
386 || BER_BVISEMPTY( &msc->msc_bound_ndn ) )
388 LDAP_BACK_CONN_ISANON_SET( msc );
391 LDAP_BACK_CONN_ISBOUND_SET( msc );
393 retcode = META_SEARCH_CANDIDATE;
395 /* connect must be async */
396 ldap_set_option( msc->msc_ld, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_OFF );
399 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
400 META_BINDING_CLEAR( &candidates[ candidate ] );
402 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
407 static meta_search_candidate_t
408 meta_back_search_start(
414 SlapReply *candidates )
416 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
417 metatarget_t *mt = mi->mi_targets[ candidate ];
418 metasingleconn_t *msc = &(*mcp)->mc_conns[ candidate ];
419 struct berval realbase = op->o_req_dn;
420 int realscope = op->ors_scope;
421 struct berval mbase = BER_BVNULL;
422 struct berval mfilter = BER_BVNULL;
423 char **mapped_attrs = NULL;
425 meta_search_candidate_t retcode;
426 struct timeval tv, *tvp = NULL;
428 LDAPControl **ctrls = NULL;
430 /* this should not happen; just in case... */
431 if ( msc->msc_ld == NULL ) {
432 Debug( LDAP_DEBUG_ANY,
433 "%s: meta_back_search_start candidate=%d ld=NULL%s.\n",
434 op->o_log_prefix, candidate,
435 META_BACK_ONERR_STOP( mi ) ? "" : " (ignored)" );
436 candidates[ candidate ].sr_err = LDAP_OTHER;
437 if ( META_BACK_ONERR_STOP( mi ) ) {
438 return META_SEARCH_ERR;
440 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
441 return META_SEARCH_NOT_CANDIDATE;
444 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_back_search_start[%d]\n", op->o_log_prefix, candidate, 0 );
447 * modifies the base according to the scope, if required
449 if ( mt->mt_nsuffix.bv_len > op->o_req_ndn.bv_len ) {
450 switch ( op->ors_scope ) {
451 case LDAP_SCOPE_SUBTREE:
453 * make the target suffix the new base
454 * FIXME: this is very forgiving, because
455 * "illegal" searchBases may be turned
456 * into the suffix of the target; however,
457 * the requested searchBase already passed
458 * thru the candidate analyzer...
460 if ( dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) ) {
461 realbase = mt->mt_nsuffix;
462 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
463 realscope = LDAP_SCOPE_SUBORDINATE;
468 * this target is no longer candidate
470 retcode = META_SEARCH_NOT_CANDIDATE;
475 case LDAP_SCOPE_SUBORDINATE:
476 case LDAP_SCOPE_ONELEVEL:
478 struct berval rdn = mt->mt_nsuffix;
479 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
480 if ( dnIsOneLevelRDN( &rdn )
481 && dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) )
484 * if there is exactly one level,
485 * make the target suffix the new
486 * base, and make scope "base"
488 realbase = mt->mt_nsuffix;
489 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
490 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
491 realscope = LDAP_SCOPE_SUBORDINATE;
493 realscope = LDAP_SCOPE_SUBTREE;
496 realscope = LDAP_SCOPE_BASE;
499 } /* else continue with the next case */
502 case LDAP_SCOPE_BASE:
504 * this target is no longer candidate
506 retcode = META_SEARCH_NOT_CANDIDATE;
511 /* initiate dobind */
512 retcode = meta_search_dobind_init( op, rs, mcp, candidate, candidates );
514 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%d]=%d\n", op->o_log_prefix, candidate, retcode );
516 if ( retcode != META_SEARCH_CANDIDATE ) {
521 * Rewrite the search base, if required
524 dc->ctx = "searchBase";
525 switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
529 case LDAP_UNWILLING_TO_PERFORM:
530 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
531 rs->sr_text = "Operation not allowed";
532 send_ldap_result( op, rs );
533 retcode = META_SEARCH_ERR;
539 * this target is no longer candidate
541 retcode = META_SEARCH_NOT_CANDIDATE;
548 rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
549 &mfilter, BACKLDAP_MAP );
554 case LDAP_COMPARE_FALSE:
557 * this target is no longer candidate
559 retcode = META_SEARCH_NOT_CANDIDATE;
564 * Maps required attributes
566 rc = ldap_back_map_attrs( &mt->mt_rwmap.rwm_at,
567 op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
568 if ( rc != LDAP_SUCCESS ) {
570 * this target is no longer candidate
572 retcode = META_SEARCH_NOT_CANDIDATE;
576 /* should we check return values? */
577 if ( op->ors_deref != -1 ) {
578 assert( msc->msc_ld != NULL );
579 (void)ldap_set_option( msc->msc_ld, LDAP_OPT_DEREF,
580 ( void * )&op->ors_deref );
583 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
584 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
591 if ( ldap_back_proxy_authz_ctrl( &msc->msc_bound_ndn,
592 mt->mt_version, &mt->mt_idassert, op, rs, &ctrls )
595 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
596 retcode = META_SEARCH_NOT_CANDIDATE;
603 assert( msc->msc_ld != NULL );
604 rc = ldap_search_ext( msc->msc_ld,
605 mbase.bv_val, realscope, mfilter.bv_val,
606 mapped_attrs, op->ors_attrsonly,
607 ctrls, NULL, tvp, op->ors_slimit,
608 &candidates[ candidate ].sr_msgid );
611 retcode = META_SEARCH_CANDIDATE;
614 case LDAP_SERVER_DOWN:
615 if ( nretries && meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
617 /* if the identity changed, there might be need to re-authz */
618 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
622 if ( *mcp == NULL ) {
623 retcode = META_SEARCH_ERR;
629 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
630 retcode = META_SEARCH_NOT_CANDIDATE;
634 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
636 if ( mapped_attrs ) {
637 free( mapped_attrs );
639 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
640 free( mfilter.bv_val );
642 if ( mbase.bv_val != realbase.bv_val ) {
643 free( mbase.bv_val );
647 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_back_search_start[%d]=%d\n", op->o_log_prefix, candidate, retcode );
653 meta_back_search( Operation *op, SlapReply *rs )
655 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
657 struct timeval save_tv = { 0, 0 },
659 time_t stoptime = (time_t)-1;
660 int rc = 0, sres = LDAP_SUCCESS;
661 char *matched = NULL;
662 int last = 0, ncandidates = 0,
663 initial_candidates = 0, candidate_match = 0,
665 ldap_back_send_t sendok = LDAP_BACK_SENDERR;
670 SlapReply *candidates = meta_back_candidates_get( op );
673 * controls are set in ldap_back_dobind()
675 * FIXME: in case of values return filter, we might want
676 * to map attrs and maybe rewrite value
679 mc = meta_back_getconn( op, rs, NULL, sendok );
684 dc.conn = op->o_conn;
690 for ( i = 0; i < mi->mi_ntargets; i++ ) {
691 /* reset sr_msgid; it is used in most loops
692 * to check if that target is still to be considered */
693 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
695 /* a target is marked as candidate by meta_back_getconn();
696 * if for any reason (an error, it's over or so) it is
697 * no longer active, sr_msgid is set to META_MSGID_IGNORE
698 * but it remains candidate, which means it has been active
699 * at some point during the operation. This allows to
700 * use its response code and more to compute the final
702 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
706 candidates[ i ].sr_matched = NULL;
707 candidates[ i ].sr_text = NULL;
708 candidates[ i ].sr_ref = NULL;
709 candidates[ i ].sr_ctrls = NULL;
712 for ( i = 0; i < mi->mi_ntargets; i++ ) {
713 if ( !META_IS_CANDIDATE( &candidates[ i ] )
714 || candidates[ i ].sr_err != LDAP_SUCCESS )
719 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
721 case META_SEARCH_NOT_CANDIDATE:
722 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
725 case META_SEARCH_NEED_BIND:
729 case META_SEARCH_CANDIDATE:
730 case META_SEARCH_BINDING:
731 candidates[ i ].sr_type = REP_INTERMEDIATE;
735 case META_SEARCH_ERR:
736 savepriv = op->o_private;
737 op->o_private = (void *)i;
738 send_ldap_result( op, rs );
739 op->o_private = savepriv;
745 if ( ncandidates > 0 && needbind == ncandidates ) {
747 * give up the second time...
749 * NOTE: this should not occur the second time, since a fresh
750 * connection has ben created; however, targets may also
751 * need bind because the bind timed out or so.
753 if ( sendok & LDAP_BACK_BINDING ) {
754 Debug( LDAP_DEBUG_ANY,
755 "%s meta_back_search: unable to initialize conn\n",
756 op->o_log_prefix, 0, 0 );
757 rs->sr_err = LDAP_UNAVAILABLE;
758 rs->sr_text = "unable to initialize connection to remote targets";
759 send_ldap_result( op, rs );
764 /* FIXME: better create a separate connection? */
765 sendok |= LDAP_BACK_BINDING;
768 Debug( LDAP_DEBUG_ANY, "*** %s drop mc=%p create new connection\n",
769 op->o_log_prefix, (void *)mc, 0 );
770 #endif /* DEBUG_205 */
772 meta_back_release_conn( op, mc );
781 initial_candidates = ncandidates;
783 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
784 char cnd[ SLAP_TEXT_BUFLEN ];
787 for ( c = 0; c < mi->mi_ntargets; c++ ) {
788 if ( META_IS_CANDIDATE( &candidates[ c ] ) ) {
796 Debug( LDAP_DEBUG_TRACE, "%s meta_back_search: ncandidates=%d "
797 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
800 if ( initial_candidates == 0 ) {
801 /* NOTE: here we are not sending any matchedDN;
802 * this is intended, because if the back-meta
803 * is serving this search request, but no valid
804 * candidate could be looked up, it means that
805 * there is a hole in the mapping of the targets
806 * and thus no knowledge of any remote superior
808 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
809 "base=\"%s\" scope=%d: "
810 "no candidate could be selected\n",
811 op->o_log_prefix, op->o_req_dn.bv_val,
814 /* FIXME: we're sending the first error we encounter;
815 * maybe we should pick the worst... */
816 rc = LDAP_NO_SUCH_OBJECT;
817 for ( i = 0; i < mi->mi_ntargets; i++ ) {
818 if ( META_IS_CANDIDATE( &candidates[ i ] )
819 && candidates[ i ].sr_err != LDAP_SUCCESS )
821 rc = candidates[ i ].sr_err;
826 send_ldap_error( op, rs, rc, NULL );
831 /* We pull apart the ber result, stuff it into a slapd entry, and
832 * let send_search_entry stuff it back into ber format. Slow & ugly,
833 * but this is necessary for version matching, and for ACL processing.
836 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
837 stoptime = op->o_time + op->ors_tlimit;
841 * In case there are no candidates, no cycle takes place...
843 * FIXME: we might use a queue, to better balance the load
844 * among the candidates
846 for ( rc = 0; ncandidates > 0; ) {
849 alreadybound = ncandidates;
851 /* check time limit */
852 if ( op->ors_tlimit != SLAP_NO_LIMIT
853 && slap_get_time() > stoptime )
856 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
857 savepriv = op->o_private;
858 op->o_private = (void *)i;
859 send_ldap_result( op, rs );
860 op->o_private = savepriv;
864 for ( i = 0; i < mi->mi_ntargets; i++ ) {
865 metasingleconn_t *msc = &mc->mc_conns[ i ];
866 LDAPMessage *res = NULL, *msg;
868 /* if msgid is invalid, don't ldap_result() */
869 if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
873 /* if target still needs bind, retry */
874 if ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND ) {
875 meta_search_candidate_t retcode;
877 /* initiate dobind */
878 retcode = meta_search_dobind_init( op, rs, &mc, i, candidates );
880 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%ld]=%d\n",
881 op->o_log_prefix, i, retcode );
884 case META_SEARCH_NEED_BIND:
888 case META_SEARCH_BINDING:
891 case META_SEARCH_ERR:
892 candidates[ i ].sr_err = rs->sr_err;
893 if ( META_BACK_ONERR_STOP( mi ) ) {
894 savepriv = op->o_private;
895 op->o_private = (void *)i;
896 send_ldap_result( op, rs );
897 op->o_private = savepriv;
902 case META_SEARCH_NOT_CANDIDATE:
904 * When no candidates are left,
905 * the outer cycle finishes
907 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
908 assert( ncandidates > 0 );
912 case META_SEARCH_CANDIDATE:
913 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
914 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
916 case META_SEARCH_CANDIDATE:
917 assert( candidates[ i ].sr_msgid >= 0 );
920 case META_SEARCH_ERR:
921 candidates[ i ].sr_err = rs->sr_err;
922 if ( META_BACK_ONERR_STOP( mi ) ) {
923 savepriv = op->o_private;
924 op->o_private = (void *)i;
925 send_ldap_result( op, rs );
926 op->o_private = savepriv;
931 case META_SEARCH_NOT_CANDIDATE:
932 /* means that meta_back_search_start()
933 * failed but onerr == continue */
934 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
935 assert( ncandidates > 0 );
954 /* check for abandon */
955 if ( op->o_abandon ) {
960 if ( msc->msc_ld == NULL ) {
961 char buf[ SLAP_TEXT_BUFLEN ];
963 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
964 snprintf( buf, sizeof( buf ),
965 "%s meta_back_search[%ld] mc=%p msgid=%d%s%s%s\n",
966 op->o_log_prefix, (long)i, (void *)mc,
967 candidates[ i ].sr_msgid,
968 META_IS_BINDING( &candidates[ i ] ) ? " binding" : "",
969 LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ? " connbinding" : "",
970 META_BACK_CONN_CREATING( &mc->mc_conns[ i ] ) ? " conncreating" : "" );
971 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
973 Debug( LDAP_DEBUG_ANY, "!!! %s\n", buf, 0, 0 );
975 #endif /* DEBUG_205 */
978 * FIXME: handle time limit as well?
979 * Note that target servers are likely
980 * to handle it, so at some time we'll
981 * get a LDAP_TIMELIMIT_EXCEEDED from
985 rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
986 LDAP_MSG_RECEIVED, &tv, &res );
989 /* FIXME: res should not need to be freed */
990 assert( res == NULL );
995 /* something REALLY bad happened! */
996 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
997 candidates[ i ].sr_type = REP_RESULT;
999 if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
1000 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1001 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
1003 case META_SEARCH_CANDIDATE:
1004 /* get back into business... */
1007 /* means that failed but onerr == continue */
1008 case META_SEARCH_NOT_CANDIDATE:
1009 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1012 candidates[ i ].sr_err = rs->sr_err;
1013 if ( META_BACK_ONERR_STOP( mi ) ) {
1014 savepriv = op->o_private;
1015 op->o_private = (void *)i;
1016 send_ldap_result( op, rs );
1017 op->o_private = savepriv;
1022 case META_SEARCH_BINDING:
1023 case META_SEARCH_NEED_BIND:
1027 /* unrecoverable error */
1028 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1029 rc = rs->sr_err = LDAP_OTHER;
1034 candidates[ i ].sr_err = rs->sr_err;
1035 if ( META_BACK_ONERR_STOP( mi ) ) {
1036 savepriv = op->o_private;
1037 op->o_private = (void *)i;
1038 send_ldap_result( op, rs );
1039 op->o_private = savepriv;
1045 * When no candidates are left,
1046 * the outer cycle finishes
1048 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1049 assert( ncandidates > 0 );
1051 rs->sr_err = candidates[ i ].sr_err;
1055 /* only touch when activity actually took place... */
1056 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
1057 msc->msc_time = op->o_time;
1062 for ( msg = ldap_first_message( msc->msc_ld, res );
1064 msg = ldap_next_message( msc->msc_ld, msg ) )
1066 rc = ldap_msgtype( msg );
1067 if ( rc == LDAP_RES_SEARCH_ENTRY ) {
1070 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1071 /* don't retry any more... */
1072 candidates[ i ].sr_type = REP_RESULT;
1077 e = ldap_first_entry( msc->msc_ld, msg );
1078 savepriv = op->o_private;
1079 op->o_private = (void *)i;
1080 rs->sr_err = meta_send_entry( op, rs, mc, i, e );
1082 switch ( rs->sr_err ) {
1083 case LDAP_SIZELIMIT_EXCEEDED:
1084 savepriv = op->o_private;
1085 op->o_private = (void *)i;
1086 send_ldap_result( op, rs );
1087 op->o_private = savepriv;
1088 rs->sr_err = LDAP_SUCCESS;
1089 ldap_msgfree( res );
1093 case LDAP_UNAVAILABLE:
1094 rs->sr_err = LDAP_OTHER;
1095 ldap_msgfree( res );
1099 op->o_private = savepriv;
1101 /* don't wait any longer... */
1104 save_tv.tv_usec = 0;
1106 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
1107 char **references = NULL;
1110 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1111 /* don't retry any more... */
1112 candidates[ i ].sr_type = REP_RESULT;
1117 rc = ldap_parse_reference( msc->msc_ld, msg,
1118 &references, &rs->sr_ctrls, 0 );
1120 if ( rc != LDAP_SUCCESS ) {
1124 if ( references == NULL ) {
1128 #ifdef ENABLE_REWRITE
1129 dc.ctx = "referralDN";
1130 #else /* ! ENABLE_REWRITE */
1133 #endif /* ! ENABLE_REWRITE */
1135 /* FIXME: merge all and return at the end */
1137 for ( cnt = 0; references[ cnt ]; cnt++ )
1140 rs->sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
1142 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1143 ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
1145 BER_BVZERO( &rs->sr_ref[ cnt ] );
1147 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref );
1149 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
1150 /* ignore return value by now */
1151 savepriv = op->o_private;
1152 op->o_private = (void *)i;
1153 ( void )send_search_reference( op, rs );
1154 op->o_private = savepriv;
1156 ber_bvarray_free( rs->sr_ref );
1162 ber_memvfree( (void **)references );
1165 if ( rs->sr_ctrls ) {
1166 ldap_controls_free( rs->sr_ctrls );
1167 rs->sr_ctrls = NULL;
1170 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
1171 char buf[ SLAP_TEXT_BUFLEN ];
1172 char **references = NULL;
1174 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1175 /* don't retry any more... */
1176 candidates[ i ].sr_type = REP_RESULT;
1179 /* NOTE: ignores response controls
1180 * (and intermediate response controls
1181 * as well, except for those with search
1182 * references); this may not be correct,
1183 * but if they're not ignored then
1184 * back-meta would need to merge them
1185 * consistently (think of pagedResults...)
1187 /* FIXME: response controls? */
1188 rs->sr_err = ldap_parse_result( msc->msc_ld,
1190 &candidates[ i ].sr_err,
1191 (char **)&candidates[ i ].sr_matched,
1192 NULL /* (char **)&candidates[ i ].sr_text */ ,
1194 NULL /* &candidates[ i ].sr_ctrls (unused) */ ,
1196 if ( rs->sr_err != LDAP_SUCCESS ) {
1197 ldap_get_option( msc->msc_ld,
1198 LDAP_OPT_ERROR_NUMBER,
1200 sres = slap_map_api2result( rs );
1201 candidates[ i ].sr_type = REP_RESULT;
1202 ldap_msgfree( res );
1207 /* massage matchedDN if need be */
1208 if ( candidates[ i ].sr_matched != NULL ) {
1209 struct berval match, mmatch;
1211 ber_str2bv( candidates[ i ].sr_matched,
1213 candidates[ i ].sr_matched = NULL;
1215 dc.ctx = "matchedDN";
1216 dc.target = mi->mi_targets[ i ];
1217 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
1218 if ( mmatch.bv_val == match.bv_val ) {
1219 candidates[ i ].sr_matched
1220 = ch_strdup( mmatch.bv_val );
1223 candidates[ i ].sr_matched = mmatch.bv_val;
1228 ldap_memfree( match.bv_val );
1231 /* add references to array */
1236 for ( cnt = 0; references[ cnt ]; cnt++ )
1239 sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
1241 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1242 ber_str2bv( references[ cnt ], 0, 1, &sr_ref[ cnt ] );
1244 BER_BVZERO( &sr_ref[ cnt ] );
1246 ( void )ldap_back_referral_result_rewrite( &dc, sr_ref );
1249 ber_memvfree( (void **)references );
1251 if ( rs->sr_v2ref == NULL ) {
1252 rs->sr_v2ref = sr_ref;
1255 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
1256 ber_bvarray_add( &rs->sr_v2ref, &sr_ref[ cnt ] );
1258 ber_memfree( sr_ref );
1262 rs->sr_err = candidates[ i ].sr_err;
1263 sres = slap_map_api2result( rs );
1265 if ( LogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
1266 snprintf( buf, sizeof( buf ),
1267 "%s meta_back_search[%ld] "
1268 "match=\"%s\" err=%ld",
1269 op->o_log_prefix, i,
1270 candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
1271 (long) candidates[ i ].sr_err );
1272 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1273 Debug( LDAP_DEBUG_TRACE, "%s.\n", buf, 0, 0 );
1276 Debug( LDAP_DEBUG_ANY, "%s (%s).\n",
1277 buf, ldap_err2string( candidates[ i ].sr_err ), 0 );
1282 case LDAP_NO_SUCH_OBJECT:
1283 /* is_ok is touched any time a valid
1284 * (even intermediate) result is
1285 * returned; as a consequence, if
1286 * a candidate returns noSuchObject
1287 * it is ignored and the candidate
1288 * is simply demoted. */
1290 sres = LDAP_SUCCESS;
1299 case LDAP_SIZELIMIT_EXCEEDED:
1300 /* if a target returned sizelimitExceeded
1301 * and the entry count is equal to the
1302 * proxy's limit, the target would have
1303 * returned more, and the error must be
1304 * propagated to the client; otherwise,
1305 * the target enforced a limit lower
1306 * than what requested by the proxy;
1308 candidates[ i ].sr_err = rs->sr_err;
1309 if ( rs->sr_nentries == op->ors_slimit
1310 || META_BACK_ONERR_STOP( mi ) )
1312 savepriv = op->o_private;
1313 op->o_private = (void *)i;
1314 send_ldap_result( op, rs );
1315 op->o_private = savepriv;
1316 ldap_msgfree( res );
1323 candidates[ i ].sr_err = rs->sr_err;
1324 if ( META_BACK_ONERR_STOP( mi ) ) {
1325 savepriv = op->o_private;
1326 op->o_private = (void *)i;
1327 send_ldap_result( op, rs );
1328 op->o_private = savepriv;
1329 ldap_msgfree( res );
1340 * When no candidates are left,
1341 * the outer cycle finishes
1343 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1346 } else if ( rc == LDAP_RES_BIND ) {
1347 meta_search_candidate_t retcode;
1349 retcode = meta_search_dobind_result( op, rs, &mc, i, candidates, msg );
1350 if ( retcode == META_SEARCH_CANDIDATE ) {
1351 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1352 retcode = meta_back_search_start( op, rs, &dc, &mc, i, candidates );
1355 switch ( retcode ) {
1356 case META_SEARCH_CANDIDATE:
1359 /* means that failed but onerr == continue */
1360 case META_SEARCH_NOT_CANDIDATE:
1361 case META_SEARCH_ERR:
1362 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1365 candidates[ i ].sr_err = rs->sr_err;
1366 if ( META_BACK_ONERR_STOP( mi ) ) {
1367 savepriv = op->o_private;
1368 op->o_private = (void *)i;
1369 send_ldap_result( op, rs );
1370 op->o_private = savepriv;
1371 ldap_msgfree( res );
1384 ldap_msgfree( res );
1390 ldap_msgfree( res );
1394 /* check for abandon */
1395 if ( op->o_abandon || doabandon ) {
1396 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1397 if ( candidates[ i ].sr_msgid >= 0 ) {
1398 if ( META_IS_BINDING( &candidates[ i ] ) ) {
1399 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1400 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ) {
1401 /* if still binding, destroy */
1404 char buf[ SLAP_TEXT_BUFLEN ];
1406 snprintf( buf, sizeof( buf), "%s meta_back_search(abandon) "
1407 "ldap_unbind_ext[%ld] mc=%p ld=%p",
1408 op->o_log_prefix, i, (void *)mc,
1409 (void *)mc->mc_conns[i].msc_ld );
1411 Debug( LDAP_DEBUG_ANY, "### %s\n", buf, 0, 0 );
1412 #endif /* DEBUG_205 */
1414 meta_clear_one_candidate( op, mc, i );
1416 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1417 META_BINDING_CLEAR( &candidates[ i ] );
1420 (void)meta_back_cancel( mc, op, rs,
1421 candidates[ i ].sr_msgid, i,
1422 LDAP_BACK_DONTSEND );
1425 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1426 assert( ncandidates > 0 );
1431 if ( op->o_abandon ) {
1433 /* let send_ldap_result play cleanup handlers (ITS#4645) */
1438 /* if no entry was found during this loop,
1439 * set a minimal timeout */
1440 if ( ncandidates > 0 && gotit == 0 ) {
1441 if ( save_tv.tv_sec == 0 && save_tv.tv_usec == 0 ) {
1442 save_tv.tv_usec = LDAP_BACK_RESULT_UTIMEOUT/initial_candidates;
1444 /* arbitrarily limit to something between 1 and 2 minutes */
1445 } else if ( ( stoptime == -1 && save_tv.tv_sec < 60 )
1446 || save_tv.tv_sec < ( stoptime - slap_get_time() ) / ( 2 * ncandidates ) )
1448 /* double the timeout */
1449 lutil_timermul( &save_tv, 2, &save_tv );
1453 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1454 char buf[ SLAP_TEXT_BUFLEN ];
1456 snprintf( buf, sizeof( buf ), "%s %ld.%06ld %d/%d mc=%p",
1457 op->o_log_prefix, save_tv.tv_sec, save_tv.tv_usec,
1458 ncandidates, initial_candidates, mc );
1459 Debug( LDAP_DEBUG_TRACE, "### %s\n", buf, 0, 0 );
1460 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1461 if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
1465 snprintf( buf, sizeof( buf ), "[%ld] ld=%p%s%s\n",
1467 mc->mc_conns[ i ].msc_ld,
1468 ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND ) ? " needbind" : "",
1469 META_IS_BINDING( &candidates[ i ] ) ? " binding" : "" );
1470 Debug( LDAP_DEBUG_TRACE, "### %s\n", buf, 0, 0 );
1475 if ( alreadybound == 0 ) {
1477 Debug( LDAP_DEBUG_TRACE, "### %s select(%ld.%06ld)\n",
1478 op->o_log_prefix, save_tv.tv_sec, save_tv.tv_usec );
1481 (void)select( 0, NULL, NULL, NULL, &tv );
1484 ldap_pvt_thread_yield();
1491 * FIXME: need a better strategy to handle errors
1494 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE,
1495 -1, stoptime != -1 ? (stoptime - slap_get_time()) : 0,
1496 LDAP_BACK_SENDERR );
1504 * Rewrite the matched portion of the search base, if required
1506 * FIXME: only the last one gets caught!
1508 savepriv = op->o_private;
1509 op->o_private = (void *)(long)mi->mi_ntargets;
1510 if ( candidate_match > 0 ) {
1511 struct berval pmatched = BER_BVNULL;
1513 /* we use the first one */
1514 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1515 if ( META_IS_CANDIDATE( &candidates[ i ] )
1516 && candidates[ i ].sr_matched != NULL )
1518 struct berval bv, pbv;
1521 /* if we got success, and this target
1522 * returned noSuchObject, and its suffix
1523 * is a superior of the searchBase,
1524 * ignore the matchedDN */
1525 if ( sres == LDAP_SUCCESS
1526 && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
1527 && op->o_req_ndn.bv_len > mi->mi_targets[ i ]->mt_nsuffix.bv_len )
1529 free( (char *)candidates[ i ].sr_matched );
1530 candidates[ i ].sr_matched = NULL;
1534 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
1535 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
1537 if ( rc == LDAP_SUCCESS ) {
1539 /* NOTE: if they all are superiors
1540 * of the baseDN, the shorter is also
1541 * superior of the longer... */
1542 if ( pbv.bv_len > pmatched.bv_len ) {
1543 if ( !BER_BVISNULL( &pmatched ) ) {
1544 op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
1547 op->o_private = (void *)i;
1550 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
1554 if ( candidates[ i ].sr_matched != NULL ) {
1555 free( (char *)candidates[ i ].sr_matched );
1556 candidates[ i ].sr_matched = NULL;
1561 if ( !BER_BVISNULL( &pmatched ) ) {
1562 matched = pmatched.bv_val;
1565 } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
1566 matched = op->o_bd->be_suffix[ 0 ].bv_val;
1571 char buf[ SLAP_TEXT_BUFLEN ];
1572 char cnd[ SLAP_TEXT_BUFLEN ];
1575 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1576 if ( META_IS_CANDIDATE( &candidates[ i ] ) ) {
1584 snprintf( buf, sizeof( buf ), "%s meta_back_search: is_scope=%d is_ok=%d cnd=\"%s\"\n",
1585 op->o_log_prefix, initial_candidates, is_ok, cnd );
1587 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1592 * In case we returned at least one entry, we return LDAP_SUCCESS
1593 * otherwise, the latter error code we got
1596 if ( sres == LDAP_SUCCESS ) {
1597 if ( rs->sr_v2ref ) {
1598 sres = LDAP_REFERRAL;
1601 if ( META_BACK_ONERR_REPORT( mi ) ) {
1603 * Report errors, if any
1605 * FIXME: we should handle error codes and return the more
1606 * important/reasonable
1608 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1609 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1613 if ( candidates[ i ].sr_err != LDAP_SUCCESS
1614 && candidates[ i ].sr_err != LDAP_NO_SUCH_OBJECT )
1616 sres = candidates[ i ].sr_err;
1624 rs->sr_matched = matched;
1625 rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
1626 send_ldap_result( op, rs );
1627 op->o_private = savepriv;
1628 rs->sr_matched = NULL;
1632 if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
1633 op->o_tmpfree( matched, op->o_tmpmemctx );
1636 if ( rs->sr_v2ref ) {
1637 ber_bvarray_free( rs->sr_v2ref );
1640 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1641 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1645 if ( mc && META_IS_BINDING( &candidates[ i ] ) ) {
1646 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1647 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ) {
1648 assert( candidates[ i ].sr_msgid >= 0 );
1649 assert( mc->mc_conns[ i ].msc_ld != NULL );
1652 Debug( LDAP_DEBUG_ANY, "### %s meta_back_search(cleanup) "
1653 "ldap_unbind_ext[%ld] ld=%p\n",
1654 op->o_log_prefix, i, (void *)mc->mc_conns[i].msc_ld );
1655 #endif /* DEBUG_205 */
1657 /* if still binding, destroy */
1658 meta_clear_one_candidate( op, mc, i );
1660 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1661 META_BINDING_CLEAR( &candidates[ i ] );
1664 if ( candidates[ i ].sr_matched ) {
1665 free( (char *)candidates[ i ].sr_matched );
1666 candidates[ i ].sr_matched = NULL;
1669 if ( candidates[ i ].sr_text ) {
1670 ldap_memfree( (char *)candidates[ i ].sr_text );
1671 candidates[ i ].sr_text = NULL;
1674 if ( candidates[ i ].sr_ref ) {
1675 ber_bvarray_free( candidates[ i ].sr_ref );
1676 candidates[ i ].sr_ref = NULL;
1679 if ( candidates[ i ].sr_ctrls ) {
1680 ldap_controls_free( candidates[ i ].sr_ctrls );
1681 candidates[ i ].sr_ctrls = NULL;
1684 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1685 meta_back_quarantine( op, &candidates[ i ], i );
1688 /* only in case of timelimit exceeded, if the timelimit exceeded because
1689 * one contacted target never responded, invalidate the connection
1690 * NOTE: should we quarantine the target as well? right now, the connection
1691 * is invalidated; the next time it will be recreated and the target
1692 * will be quarantined if it cannot be contacted */
1693 if ( mi->mi_idle_timeout != 0
1694 && rs->sr_err == LDAP_TIMELIMIT_EXCEEDED
1695 && op->o_time > mc->mc_conns[ i ].msc_time )
1697 /* don't let anyone else use this expired connection */
1698 LDAP_BACK_CONN_TAINTED_SET( mc );
1703 meta_back_release_conn( op, mc );
1717 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1718 struct berval a, mapped;
1720 BerElement ber = *e->lm_ber;
1721 Attribute *attr, **attrp;
1728 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
1729 return LDAP_DECODING_ERROR;
1733 * Rewrite the dn of the result, if needed
1735 dc.target = mi->mi_targets[ target ];
1736 dc.conn = op->o_conn;
1738 dc.ctx = "searchResult";
1740 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
1741 if ( rs->sr_err != LDAP_SUCCESS) {
1746 * Note: this may fail if the target host(s) schema differs
1747 * from the one known to the meta, and a DN with unknown
1748 * attributes is returned.
1750 * FIXME: should we log anything, or delegate to dnNormalize?
1752 rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
1754 if ( dn.bv_val != bdn.bv_val ) {
1759 if ( rc != LDAP_SUCCESS ) {
1760 return LDAP_INVALID_DN_SYNTAX;
1766 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1767 ( void )meta_dncache_update_entry( &mi->mi_cache,
1768 &ent.e_nname, target );
1771 attrp = &ent.e_attrs;
1773 dc.ctx = "searchAttrDN";
1774 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
1776 slap_syntax_validate_func *validate;
1777 slap_syntax_transform_func *pretty;
1779 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_at,
1780 &a, &mapped, BACKLDAP_REMAP );
1781 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
1782 ( void )ber_scanf( &ber, "x" /* [W] */ );
1785 attr = attr_alloc( NULL );
1786 if ( attr == NULL ) {
1789 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
1791 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
1792 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
1794 char buf[ SLAP_TEXT_BUFLEN ];
1796 snprintf( buf, sizeof( buf ),
1797 "%s meta_send_entry(\"%s\"): "
1798 "slap_bv2undef_ad(%s): %s\n",
1799 op->o_log_prefix, ent.e_name.bv_val,
1800 mapped.bv_val, text );
1802 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1808 /* no subschemaSubentry */
1809 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
1810 || attr->a_desc == slap_schema.si_ad_entryDN )
1814 * We eat target's subschemaSubentry because
1815 * a search for this value is likely not
1816 * to resolve to the appropriate backend;
1817 * later, the local subschemaSubentry is
1820 * We also eat entryDN because the frontend
1821 * will reattach it without checking if already
1824 ( void )ber_scanf( &ber, "x" /* [W] */ );
1830 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
1831 || attr->a_vals == NULL )
1833 attr->a_vals = (struct berval *)&slap_dummy_bv;
1836 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
1840 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
1841 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
1843 if ( !validate && !pretty ) {
1848 if ( attr->a_desc == slap_schema.si_ad_objectClass
1849 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
1853 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1854 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_oc,
1855 bv, &mapped, BACKLDAP_REMAP );
1856 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
1862 *bv = attr->a_vals[ last ];
1863 BER_BVZERO( &attr->a_vals[ last ] );
1866 } else if ( mapped.bv_val != bv->bv_val ) {
1868 ber_dupbv( bv, &mapped );
1872 * It is necessary to try to rewrite attributes with
1873 * dn syntax because they might be used in ACLs as
1874 * members of groups; since ACLs are applied to the
1875 * rewritten stuff, no dn-based subecj clause could
1876 * be used at the ldap backend side (see
1877 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1878 * The problem can be overcome by moving the dn-based
1879 * ACLs to the target directory server, and letting
1880 * everything pass thru the ldap backend.
1885 if ( attr->a_desc->ad_type->sat_syntax ==
1886 slap_schema.si_syn_distinguishedName )
1888 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
1890 } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
1891 ldap_back_referral_result_rewrite( &dc, attr->a_vals );
1895 for ( i = 0; i < last; i++ ) {
1900 rc = pretty( attr->a_desc->ad_type->sat_syntax,
1901 &attr->a_vals[i], &pval, NULL );
1904 rc = validate( attr->a_desc->ad_type->sat_syntax,
1909 LBER_FREE( attr->a_vals[i].bv_val );
1910 if ( --last == i ) {
1911 BER_BVZERO( &attr->a_vals[ i ] );
1914 attr->a_vals[i] = attr->a_vals[last];
1915 BER_BVZERO( &attr->a_vals[last] );
1921 LBER_FREE( attr->a_vals[i].bv_val );
1922 attr->a_vals[i] = pval;
1926 if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
1932 if ( last && attr->a_desc->ad_type->sat_equality &&
1933 attr->a_desc->ad_type->sat_equality->smr_normalize )
1937 attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
1938 for ( i = 0; i<last; i++ ) {
1939 attr->a_desc->ad_type->sat_equality->smr_normalize(
1940 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1941 attr->a_desc->ad_type->sat_syntax,
1942 attr->a_desc->ad_type->sat_equality,
1943 &attr->a_vals[i], &attr->a_nvals[i],
1946 BER_BVZERO( &attr->a_nvals[i] );
1949 attr->a_nvals = attr->a_vals;
1953 attrp = &attr->a_next;
1956 rs->sr_entry = &ent;
1957 rs->sr_attrs = op->ors_attrs;
1959 rs->sr_err = LDAP_SUCCESS;
1960 rc = send_search_entry( op, rs );
1962 case LDAP_UNAVAILABLE:
1966 rs->sr_entry = NULL;
1967 rs->sr_attrs = NULL;
1969 if ( !BER_BVISNULL( &ent.e_name ) ) {
1970 free( ent.e_name.bv_val );
1971 BER_BVZERO( &ent.e_name );
1973 if ( !BER_BVISNULL( &ent.e_nname ) ) {
1974 free( ent.e_nname.bv_val );
1975 BER_BVZERO( &ent.e_nname );
1977 entry_clean( &ent );