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)
45 #define META_MSGID_CONNECTING (-3)
55 typedef enum meta_search_candidate_t {
56 META_SEARCH_UNDEFINED = -2,
58 META_SEARCH_NOT_CANDIDATE,
59 META_SEARCH_CANDIDATE,
61 META_SEARCH_NEED_BIND,
62 META_SEARCH_CONNECTING
63 } meta_search_candidate_t;
66 * meta_search_dobind_init()
68 * initiates bind for a candidate target of a search.
70 static meta_search_candidate_t
71 meta_search_dobind_init(
76 SlapReply *candidates )
78 metaconn_t *mc = *mcp;
79 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
80 metatarget_t *mt = mi->mi_targets[ candidate ];
81 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
83 struct berval binddn = msc->msc_bound_ndn,
89 meta_search_candidate_t retcode;
91 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_search_dobind_init[%d]\n",
92 op->o_log_prefix, candidate, 0 );
95 * all the targets are already bound as pseudoroot
97 if ( mc->mc_authz_target == META_BOUND_ALL ) {
98 return META_SEARCH_CANDIDATE;
101 retcode = META_SEARCH_BINDING;
102 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
103 if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
104 /* already bound (or anonymous) */
107 char buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
110 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
114 snprintf( buf, sizeof( buf ), " mc=%p ld=%p%s DN=\"%s\"",
115 (void *)mc, (void *)msc->msc_ld,
116 bound ? " bound" : " anonymous",
117 bound == 0 ? "" : msc->msc_bound_ndn.bv_val );
118 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
119 op->o_log_prefix, candidate, buf );
120 #endif /* DEBUG_205 */
122 retcode = META_SEARCH_CANDIDATE;
124 } else if ( META_BACK_CONN_CREATING( msc ) || LDAP_BACK_CONN_BINDING( msc ) ) {
125 /* another thread is binding the target for this conn; wait */
128 char buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
130 snprintf( buf, sizeof( buf ), " mc=%p ld=%p needbind",
131 (void *)mc, (void *)msc->msc_ld );
132 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
133 op->o_log_prefix, candidate, buf );
134 #endif /* DEBUG_205 */
136 candidates[ candidate ].sr_msgid = META_MSGID_NEED_BIND;
137 retcode = META_SEARCH_NEED_BIND;
140 /* we'll need to bind the target for this conn */
143 char buf[ SLAP_TEXT_BUFLEN ];
145 snprintf( buf, sizeof( buf ), " mc=%p ld=%p binding",
146 (void *)mc, (void *)msc->msc_ld );
147 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
148 op->o_log_prefix, candidate, buf );
149 #endif /* DEBUG_205 */
151 if ( msc->msc_ld == NULL ) {
152 /* for some reason (e.g. because formerly in "binding"
153 * state, with eventual connection expiration or invalidation)
154 * it was not initialized as expected */
156 Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p ld=NULL\n",
157 op->o_log_prefix, candidate, (void *)mc );
159 rc = meta_back_init_one_conn( op, rs, *mcp, candidate,
160 LDAP_BACK_CONN_ISPRIV( *mcp ), LDAP_BACK_DONTSEND, 0 );
163 assert( msc->msc_ld != NULL );
166 case LDAP_SERVER_DOWN:
167 case LDAP_UNAVAILABLE:
168 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
172 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
177 LDAP_BACK_CONN_BINDING_SET( msc );
180 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
182 if ( retcode != META_SEARCH_BINDING ) {
186 /* NOTE: this obsoletes pseudorootdn */
187 if ( op->o_conn != NULL &&
188 !op->o_do_not_cache &&
189 ( BER_BVISNULL( &msc->msc_bound_ndn ) ||
190 BER_BVISEMPTY( &msc->msc_bound_ndn ) ||
191 ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
193 rc = meta_back_proxy_authz_cred( mc, candidate, op, rs, LDAP_BACK_DONTSEND, &binddn, &cred, &method );
194 if ( rc != LDAP_SUCCESS ) {
198 /* NOTE: we copy things here, even if bind didn't succeed yet,
199 * because the connection is not shared until bind is over */
200 if ( !BER_BVISNULL( &binddn ) ) {
201 ber_bvreplace( &msc->msc_bound_ndn, &binddn );
202 if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &cred ) ) {
203 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
204 memset( msc->msc_cred.bv_val, 0,
205 msc->msc_cred.bv_len );
207 ber_bvreplace( &msc->msc_cred, &cred );
211 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
212 /* apparently, idassert was configured with SASL bind,
213 * so bind occurred inside meta_back_proxy_authz_cred() */
214 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
215 LDAP_BACK_CONN_BINDING_CLEAR( msc );
216 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
217 return META_SEARCH_CANDIDATE;
223 case LDAP_AUTH_SIMPLE:
224 /* do a simple bind with binddn, cred */
233 assert( msc->msc_ld != NULL );
235 /* connect must be async */
237 ldap_set_option( msc->msc_ld, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_ON );
239 rc = ldap_sasl_bind( msc->msc_ld, binddn.bv_val, LDAP_SASL_SIMPLE, &cred,
240 NULL, NULL, &candidates[ candidate ].sr_msgid );
244 char buf[ SLAP_TEXT_BUFLEN ];
246 snprintf( buf, sizeof( buf ), "meta_search_dobind_init[%d] mc=%p ld=%p rc=%d",
247 candidate, (void *)mc, (void *)mc->mc_conns[ candidate ].msc_ld, rc );
248 Debug( LDAP_DEBUG_ANY, "### %s %s\n",
249 op->o_log_prefix, buf, 0 );
251 #endif /* DEBUG_205 */
255 assert( candidates[ candidate ].sr_msgid >= 0 );
256 META_BINDING_SET( &candidates[ candidate ] );
257 return META_SEARCH_BINDING;
259 case LDAP_X_CONNECTING:
260 /* must retry, same conn */
261 candidates[ candidate ].sr_msgid = META_MSGID_CONNECTING;
262 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
263 LDAP_BACK_CONN_BINDING_CLEAR( msc );
264 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
265 return META_SEARCH_CONNECTING;
267 case LDAP_SERVER_DOWN:
269 /* This is the worst thing that could happen:
270 * the search will wait until the retry is over. */
271 if ( !META_IS_RETRYING( &candidates[ candidate ] ) ) {
272 META_RETRYING_SET( &candidates[ candidate ] );
274 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
276 assert( mc->mc_refcnt > 0 );
277 if ( LogTest( LDAP_DEBUG_ANY ) ) {
278 char buf[ SLAP_TEXT_BUFLEN ];
280 /* this lock is required; however,
281 * it's invoked only when logging is on */
282 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
283 snprintf( buf, sizeof( buf ),
284 "retrying URI=\"%s\" DN=\"%s\"",
286 BER_BVISNULL( &msc->msc_bound_ndn ) ?
287 "" : msc->msc_bound_ndn.bv_val );
288 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
290 Debug( LDAP_DEBUG_ANY,
291 "%s meta_search_dobind_init[%d]: %s.\n",
292 op->o_log_prefix, candidate, buf );
295 meta_clear_one_candidate( op, mc, candidate );
296 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
298 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
300 /* mc here must be the regular mc, reset and ready for init */
301 rc = meta_back_init_one_conn( op, rs, mc, candidate,
302 LDAP_BACK_CONN_ISPRIV( mc ), LDAP_BACK_DONTSEND, 0 );
304 if ( rc == LDAP_SUCCESS ) {
305 LDAP_BACK_CONN_BINDING_SET( msc );
308 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
310 if ( rc == LDAP_SUCCESS ) {
311 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
316 if ( *mcp == NULL ) {
317 retcode = META_SEARCH_ERR;
318 rs->sr_err = LDAP_UNAVAILABLE;
326 rc = slap_map_api2result( rs );
328 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
329 meta_clear_one_candidate( op, mc, candidate );
330 candidates[ candidate ].sr_err = rc;
331 if ( META_BACK_ONERR_STOP( mi ) ) {
332 LDAP_BACK_CONN_TAINTED_SET( mc );
333 meta_back_release_conn_lock( mi, mc, 0 );
337 retcode = META_SEARCH_ERR;
340 retcode = META_SEARCH_NOT_CANDIDATE;
342 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
343 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
350 static meta_search_candidate_t
351 meta_search_dobind_result(
356 SlapReply *candidates,
359 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
360 metaconn_t *mc = *mcp;
361 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
363 meta_search_candidate_t retcode = META_SEARCH_NOT_CANDIDATE;
366 assert( msc->msc_ld != NULL );
368 /* FIXME: matched? referrals? response controls? */
369 rc = ldap_parse_result( msc->msc_ld, res,
370 &candidates[ candidate ].sr_err,
371 NULL, NULL, NULL, NULL, 0 );
372 if ( rc != LDAP_SUCCESS ) {
373 candidates[ candidate ].sr_err = rc;
376 rc = slap_map_api2result( &candidates[ candidate ] );
379 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
380 LDAP_BACK_CONN_BINDING_CLEAR( msc );
381 if ( rc != LDAP_SUCCESS ) {
382 meta_clear_one_candidate( op, mc, candidate );
383 candidates[ candidate ].sr_err = rc;
384 if ( META_BACK_ONERR_STOP( mi ) ) {
385 LDAP_BACK_CONN_TAINTED_SET( mc );
386 meta_back_release_conn_lock( mi, mc, 0 );
388 retcode = META_SEARCH_ERR;
393 /* FIXME: check if bound as idassert authcDN! */
394 if ( BER_BVISNULL( &msc->msc_bound_ndn )
395 || BER_BVISEMPTY( &msc->msc_bound_ndn ) )
397 LDAP_BACK_CONN_ISANON_SET( msc );
400 LDAP_BACK_CONN_ISBOUND_SET( msc );
402 retcode = META_SEARCH_CANDIDATE;
404 /* connect must be async */
405 ldap_set_option( msc->msc_ld, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_OFF );
408 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
409 META_BINDING_CLEAR( &candidates[ candidate ] );
411 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
416 static meta_search_candidate_t
417 meta_back_search_start(
423 SlapReply *candidates )
425 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
426 metatarget_t *mt = mi->mi_targets[ candidate ];
427 metasingleconn_t *msc = &(*mcp)->mc_conns[ candidate ];
428 struct berval realbase = op->o_req_dn;
429 int realscope = op->ors_scope;
430 struct berval mbase = BER_BVNULL;
431 struct berval mfilter = BER_BVNULL;
432 char **mapped_attrs = NULL;
434 meta_search_candidate_t retcode;
435 struct timeval tv, *tvp = NULL;
437 LDAPControl **ctrls = NULL;
439 /* this should not happen; just in case... */
440 if ( msc->msc_ld == NULL ) {
441 Debug( LDAP_DEBUG_ANY,
442 "%s: meta_back_search_start candidate=%d ld=NULL%s.\n",
443 op->o_log_prefix, candidate,
444 META_BACK_ONERR_STOP( mi ) ? "" : " (ignored)" );
445 candidates[ candidate ].sr_err = LDAP_OTHER;
446 if ( META_BACK_ONERR_STOP( mi ) ) {
447 return META_SEARCH_ERR;
449 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
450 return META_SEARCH_NOT_CANDIDATE;
453 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_back_search_start[%d]\n", op->o_log_prefix, candidate, 0 );
456 * modifies the base according to the scope, if required
458 if ( mt->mt_nsuffix.bv_len > op->o_req_ndn.bv_len ) {
459 switch ( op->ors_scope ) {
460 case LDAP_SCOPE_SUBTREE:
462 * make the target suffix the new base
463 * FIXME: this is very forgiving, because
464 * "illegal" searchBases may be turned
465 * into the suffix of the target; however,
466 * the requested searchBase already passed
467 * thru the candidate analyzer...
469 if ( dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) ) {
470 realbase = mt->mt_nsuffix;
471 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
472 realscope = LDAP_SCOPE_SUBORDINATE;
477 * this target is no longer candidate
479 retcode = META_SEARCH_NOT_CANDIDATE;
484 case LDAP_SCOPE_SUBORDINATE:
485 case LDAP_SCOPE_ONELEVEL:
487 struct berval rdn = mt->mt_nsuffix;
488 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
489 if ( dnIsOneLevelRDN( &rdn )
490 && dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) )
493 * if there is exactly one level,
494 * make the target suffix the new
495 * base, and make scope "base"
497 realbase = mt->mt_nsuffix;
498 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
499 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
500 realscope = LDAP_SCOPE_SUBORDINATE;
502 realscope = LDAP_SCOPE_SUBTREE;
505 realscope = LDAP_SCOPE_BASE;
508 } /* else continue with the next case */
511 case LDAP_SCOPE_BASE:
513 * this target is no longer candidate
515 retcode = META_SEARCH_NOT_CANDIDATE;
520 /* initiate dobind */
521 retcode = meta_search_dobind_init( op, rs, mcp, candidate, candidates );
523 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%d]=%d\n", op->o_log_prefix, candidate, retcode );
525 if ( retcode != META_SEARCH_CANDIDATE ) {
530 * Rewrite the search base, if required
533 dc->ctx = "searchBase";
534 switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
538 case LDAP_UNWILLING_TO_PERFORM:
539 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
540 rs->sr_text = "Operation not allowed";
541 send_ldap_result( op, rs );
542 retcode = META_SEARCH_ERR;
548 * this target is no longer candidate
550 retcode = META_SEARCH_NOT_CANDIDATE;
557 rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
558 &mfilter, BACKLDAP_MAP );
563 case LDAP_COMPARE_FALSE:
566 * this target is no longer candidate
568 retcode = META_SEARCH_NOT_CANDIDATE;
573 * Maps required attributes
575 rc = ldap_back_map_attrs( &mt->mt_rwmap.rwm_at,
576 op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
577 if ( rc != LDAP_SUCCESS ) {
579 * this target is no longer candidate
581 retcode = META_SEARCH_NOT_CANDIDATE;
585 /* should we check return values? */
586 if ( op->ors_deref != -1 ) {
587 assert( msc->msc_ld != NULL );
588 (void)ldap_set_option( msc->msc_ld, LDAP_OPT_DEREF,
589 ( void * )&op->ors_deref );
592 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
593 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
600 if ( ldap_back_proxy_authz_ctrl( &msc->msc_bound_ndn,
601 mt->mt_version, &mt->mt_idassert, op, rs, &ctrls )
604 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
605 retcode = META_SEARCH_NOT_CANDIDATE;
612 assert( msc->msc_ld != NULL );
613 rc = ldap_search_ext( msc->msc_ld,
614 mbase.bv_val, realscope, mfilter.bv_val,
615 mapped_attrs, op->ors_attrsonly,
616 ctrls, NULL, tvp, op->ors_slimit,
617 &candidates[ candidate ].sr_msgid );
620 retcode = META_SEARCH_CANDIDATE;
623 case LDAP_SERVER_DOWN:
624 if ( nretries && meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
626 /* if the identity changed, there might be need to re-authz */
627 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
631 if ( *mcp == NULL ) {
632 retcode = META_SEARCH_ERR;
638 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
639 retcode = META_SEARCH_NOT_CANDIDATE;
643 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
645 if ( mapped_attrs ) {
646 free( mapped_attrs );
648 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
649 free( mfilter.bv_val );
651 if ( mbase.bv_val != realbase.bv_val ) {
652 free( mbase.bv_val );
656 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_back_search_start[%d]=%d\n", op->o_log_prefix, candidate, retcode );
662 meta_back_search( Operation *op, SlapReply *rs )
664 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
666 struct timeval save_tv = { 0, 0 },
668 time_t stoptime = (time_t)(-1),
669 lastres_time = slap_get_time(),
671 int rc = 0, sres = LDAP_SUCCESS;
672 char *matched = NULL;
673 int last = 0, ncandidates = 0,
674 initial_candidates = 0, candidate_match = 0,
676 ldap_back_send_t sendok = LDAP_BACK_SENDERR;
681 SlapReply *candidates = meta_back_candidates_get( op );
684 * controls are set in ldap_back_dobind()
686 * FIXME: in case of values return filter, we might want
687 * to map attrs and maybe rewrite value
690 mc = meta_back_getconn( op, rs, NULL, sendok );
695 dc.conn = op->o_conn;
701 for ( i = 0; i < mi->mi_ntargets; i++ ) {
702 /* reset sr_msgid; it is used in most loops
703 * to check if that target is still to be considered */
704 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
706 /* a target is marked as candidate by meta_back_getconn();
707 * if for any reason (an error, it's over or so) it is
708 * no longer active, sr_msgid is set to META_MSGID_IGNORE
709 * but it remains candidate, which means it has been active
710 * at some point during the operation. This allows to
711 * use its response code and more to compute the final
713 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
717 candidates[ i ].sr_matched = NULL;
718 candidates[ i ].sr_text = NULL;
719 candidates[ i ].sr_ref = NULL;
720 candidates[ i ].sr_ctrls = NULL;
722 /* get largest timeout among candidates */
723 if ( mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ]
724 && mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ] > timeout )
726 timeout = mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ];
730 for ( i = 0; i < mi->mi_ntargets; i++ ) {
731 if ( !META_IS_CANDIDATE( &candidates[ i ] )
732 || candidates[ i ].sr_err != LDAP_SUCCESS )
737 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
739 case META_SEARCH_NOT_CANDIDATE:
740 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
743 case META_SEARCH_NEED_BIND:
747 case META_SEARCH_CONNECTING:
748 case META_SEARCH_CANDIDATE:
749 case META_SEARCH_BINDING:
750 candidates[ i ].sr_type = REP_INTERMEDIATE;
754 case META_SEARCH_ERR:
755 savepriv = op->o_private;
756 op->o_private = (void *)i;
757 send_ldap_result( op, rs );
758 op->o_private = savepriv;
768 if ( ncandidates > 0 && needbind == ncandidates ) {
770 * give up the second time...
772 * NOTE: this should not occur the second time, since a fresh
773 * connection has ben created; however, targets may also
774 * need bind because the bind timed out or so.
776 if ( sendok & LDAP_BACK_BINDING ) {
777 Debug( LDAP_DEBUG_ANY,
778 "%s meta_back_search: unable to initialize conn\n",
779 op->o_log_prefix, 0, 0 );
780 rs->sr_err = LDAP_UNAVAILABLE;
781 rs->sr_text = "unable to initialize connection to remote targets";
782 send_ldap_result( op, rs );
787 /* FIXME: better create a separate connection? */
788 sendok |= LDAP_BACK_BINDING;
791 Debug( LDAP_DEBUG_ANY, "*** %s drop mc=%p create new connection\n",
792 op->o_log_prefix, (void *)mc, 0 );
793 #endif /* DEBUG_205 */
795 meta_back_release_conn( mi, mc );
804 initial_candidates = ncandidates;
806 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
807 char cnd[ SLAP_TEXT_BUFLEN ];
810 for ( c = 0; c < mi->mi_ntargets; c++ ) {
811 if ( META_IS_CANDIDATE( &candidates[ c ] ) ) {
819 Debug( LDAP_DEBUG_TRACE, "%s meta_back_search: ncandidates=%d "
820 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
823 if ( initial_candidates == 0 ) {
824 /* NOTE: here we are not sending any matchedDN;
825 * this is intended, because if the back-meta
826 * is serving this search request, but no valid
827 * candidate could be looked up, it means that
828 * there is a hole in the mapping of the targets
829 * and thus no knowledge of any remote superior
831 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
832 "base=\"%s\" scope=%d: "
833 "no candidate could be selected\n",
834 op->o_log_prefix, op->o_req_dn.bv_val,
837 /* FIXME: we're sending the first error we encounter;
838 * maybe we should pick the worst... */
839 rc = LDAP_NO_SUCH_OBJECT;
840 for ( i = 0; i < mi->mi_ntargets; i++ ) {
841 if ( META_IS_CANDIDATE( &candidates[ i ] )
842 && candidates[ i ].sr_err != LDAP_SUCCESS )
844 rc = candidates[ i ].sr_err;
849 send_ldap_error( op, rs, rc, NULL );
854 /* We pull apart the ber result, stuff it into a slapd entry, and
855 * let send_search_entry stuff it back into ber format. Slow & ugly,
856 * but this is necessary for version matching, and for ACL processing.
859 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
860 stoptime = op->o_time + op->ors_tlimit;
864 * In case there are no candidates, no cycle takes place...
866 * FIXME: we might use a queue, to better balance the load
867 * among the candidates
869 for ( rc = 0; ncandidates > 0; ) {
872 alreadybound = ncandidates;
873 time_t curr_time = 0;
876 if ( timeout && lastres_time > 0
877 && ( slap_get_time() - lastres_time ) > timeout )
880 rs->sr_text = "Operation timed out";
881 rc = rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
882 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
883 savepriv = op->o_private;
884 op->o_private = (void *)i;
885 send_ldap_result( op, rs );
886 op->o_private = savepriv;
890 /* check time limit */
891 if ( op->ors_tlimit != SLAP_NO_LIMIT
892 && slap_get_time() > stoptime )
895 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
896 savepriv = op->o_private;
897 op->o_private = (void *)i;
898 send_ldap_result( op, rs );
899 op->o_private = savepriv;
903 for ( i = 0; i < mi->mi_ntargets; i++ ) {
904 meta_search_candidate_t retcode = META_SEARCH_UNDEFINED;
905 metasingleconn_t *msc = &mc->mc_conns[ i ];
906 LDAPMessage *res = NULL, *msg;
908 /* if msgid is invalid, don't ldap_result() */
909 if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
913 /* if target still needs bind, retry */
914 if ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND
915 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
917 /* initiate dobind */
918 retcode = meta_search_dobind_init( op, rs, &mc, i, candidates );
920 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%ld]=%d\n",
921 op->o_log_prefix, i, retcode );
924 case META_SEARCH_NEED_BIND:
928 case META_SEARCH_CONNECTING:
929 case META_SEARCH_BINDING:
932 case META_SEARCH_ERR:
933 candidates[ i ].sr_err = rs->sr_err;
934 if ( META_BACK_ONERR_STOP( mi ) ) {
935 savepriv = op->o_private;
936 op->o_private = (void *)i;
937 send_ldap_result( op, rs );
938 op->o_private = savepriv;
943 case META_SEARCH_NOT_CANDIDATE:
945 * When no candidates are left,
946 * the outer cycle finishes
948 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
949 assert( ncandidates > 0 );
953 case META_SEARCH_CANDIDATE:
954 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
955 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
957 case META_SEARCH_CANDIDATE:
958 assert( candidates[ i ].sr_msgid >= 0 );
961 case META_SEARCH_ERR:
962 candidates[ i ].sr_err = rs->sr_err;
963 if ( META_BACK_ONERR_STOP( mi ) ) {
964 savepriv = op->o_private;
965 op->o_private = (void *)i;
966 send_ldap_result( op, rs );
967 op->o_private = savepriv;
972 case META_SEARCH_NOT_CANDIDATE:
973 /* means that meta_back_search_start()
974 * failed but onerr == continue */
975 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
976 assert( ncandidates > 0 );
995 /* check for abandon */
996 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
1001 if ( msc->msc_ld == NULL ) {
1002 char buf[ SLAP_TEXT_BUFLEN ];
1004 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1005 snprintf( buf, sizeof( buf ),
1006 "%s meta_back_search[%ld] mc=%p msgid=%d%s%s%s\n",
1007 op->o_log_prefix, (long)i, (void *)mc,
1008 candidates[ i ].sr_msgid,
1009 META_IS_BINDING( &candidates[ i ] ) ? " binding" : "",
1010 LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ? " connbinding" : "",
1011 META_BACK_CONN_CREATING( &mc->mc_conns[ i ] ) ? " conncreating" : "" );
1012 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1014 Debug( LDAP_DEBUG_ANY, "!!! %s\n", buf, 0, 0 );
1016 #endif /* DEBUG_205 */
1019 * FIXME: handle time limit as well?
1020 * Note that target servers are likely
1021 * to handle it, so at some time we'll
1022 * get a LDAP_TIMELIMIT_EXCEEDED from
1026 rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
1027 LDAP_MSG_RECEIVED, &tv, &res );
1030 /* FIXME: res should not need to be freed */
1031 assert( res == NULL );
1036 /* something REALLY bad happened! */
1037 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1038 candidates[ i ].sr_type = REP_RESULT;
1040 if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
1041 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1042 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
1044 /* means that failed but onerr == continue */
1045 case META_SEARCH_NOT_CANDIDATE:
1046 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1048 assert( ncandidates > 0 );
1051 candidates[ i ].sr_err = rs->sr_err;
1052 if ( META_BACK_ONERR_STOP( mi ) ) {
1053 savepriv = op->o_private;
1054 op->o_private = (void *)i;
1055 send_ldap_result( op, rs );
1056 op->o_private = savepriv;
1061 case META_SEARCH_CANDIDATE:
1062 /* get back into business... */
1065 case META_SEARCH_BINDING:
1066 case META_SEARCH_CONNECTING:
1067 case META_SEARCH_NEED_BIND:
1068 case META_SEARCH_UNDEFINED:
1072 /* unrecoverable error */
1073 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1074 rc = rs->sr_err = LDAP_OTHER;
1079 candidates[ i ].sr_err = rs->sr_err;
1080 if ( META_BACK_ONERR_STOP( mi ) ) {
1081 savepriv = op->o_private;
1082 op->o_private = (void *)i;
1083 send_ldap_result( op, rs );
1084 op->o_private = savepriv;
1090 * When no candidates are left,
1091 * the outer cycle finishes
1093 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1094 assert( ncandidates > 0 );
1096 rs->sr_err = candidates[ i ].sr_err;
1100 lastres_time = slap_get_time();
1102 /* only touch when activity actually took place... */
1103 if ( mi->mi_idle_timeout != 0 && msc->msc_time < lastres_time ) {
1104 msc->msc_time = lastres_time;
1109 for ( msg = ldap_first_message( msc->msc_ld, res );
1111 msg = ldap_next_message( msc->msc_ld, msg ) )
1113 rc = ldap_msgtype( msg );
1114 if ( rc == LDAP_RES_SEARCH_ENTRY ) {
1117 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1118 /* don't retry any more... */
1119 candidates[ i ].sr_type = REP_RESULT;
1124 e = ldap_first_entry( msc->msc_ld, msg );
1125 savepriv = op->o_private;
1126 op->o_private = (void *)i;
1127 rs->sr_err = meta_send_entry( op, rs, mc, i, e );
1129 switch ( rs->sr_err ) {
1130 case LDAP_SIZELIMIT_EXCEEDED:
1131 savepriv = op->o_private;
1132 op->o_private = (void *)i;
1133 send_ldap_result( op, rs );
1134 op->o_private = savepriv;
1135 rs->sr_err = LDAP_SUCCESS;
1136 ldap_msgfree( res );
1140 case LDAP_UNAVAILABLE:
1141 rs->sr_err = LDAP_OTHER;
1142 ldap_msgfree( res );
1146 op->o_private = savepriv;
1148 /* don't wait any longer... */
1151 save_tv.tv_usec = 0;
1153 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
1154 char **references = NULL;
1157 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1158 /* don't retry any more... */
1159 candidates[ i ].sr_type = REP_RESULT;
1164 rc = ldap_parse_reference( msc->msc_ld, msg,
1165 &references, &rs->sr_ctrls, 0 );
1167 if ( rc != LDAP_SUCCESS ) {
1171 if ( references == NULL ) {
1175 #ifdef ENABLE_REWRITE
1176 dc.ctx = "referralDN";
1177 #else /* ! ENABLE_REWRITE */
1180 #endif /* ! ENABLE_REWRITE */
1182 /* FIXME: merge all and return at the end */
1184 for ( cnt = 0; references[ cnt ]; cnt++ )
1187 rs->sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
1189 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1190 ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
1192 BER_BVZERO( &rs->sr_ref[ cnt ] );
1194 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref );
1196 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
1197 /* ignore return value by now */
1198 savepriv = op->o_private;
1199 op->o_private = (void *)i;
1200 ( void )send_search_reference( op, rs );
1201 op->o_private = savepriv;
1203 ber_bvarray_free( rs->sr_ref );
1209 ber_memvfree( (void **)references );
1212 if ( rs->sr_ctrls ) {
1213 ldap_controls_free( rs->sr_ctrls );
1214 rs->sr_ctrls = NULL;
1217 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
1218 char buf[ SLAP_TEXT_BUFLEN ];
1219 char **references = NULL;
1221 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1222 /* don't retry any more... */
1223 candidates[ i ].sr_type = REP_RESULT;
1226 /* NOTE: ignores response controls
1227 * (and intermediate response controls
1228 * as well, except for those with search
1229 * references); this may not be correct,
1230 * but if they're not ignored then
1231 * back-meta would need to merge them
1232 * consistently (think of pagedResults...)
1234 /* FIXME: response controls? */
1235 rs->sr_err = ldap_parse_result( msc->msc_ld,
1237 &candidates[ i ].sr_err,
1238 (char **)&candidates[ i ].sr_matched,
1239 NULL /* (char **)&candidates[ i ].sr_text */ ,
1241 NULL /* &candidates[ i ].sr_ctrls (unused) */ ,
1243 if ( rs->sr_err != LDAP_SUCCESS ) {
1244 ldap_get_option( msc->msc_ld,
1245 LDAP_OPT_ERROR_NUMBER,
1247 sres = slap_map_api2result( rs );
1248 candidates[ i ].sr_type = REP_RESULT;
1249 ldap_msgfree( res );
1254 /* massage matchedDN if need be */
1255 if ( candidates[ i ].sr_matched != NULL ) {
1256 struct berval match, mmatch;
1258 ber_str2bv( candidates[ i ].sr_matched,
1260 candidates[ i ].sr_matched = NULL;
1262 dc.ctx = "matchedDN";
1263 dc.target = mi->mi_targets[ i ];
1264 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
1265 if ( mmatch.bv_val == match.bv_val ) {
1266 candidates[ i ].sr_matched
1267 = ch_strdup( mmatch.bv_val );
1270 candidates[ i ].sr_matched = mmatch.bv_val;
1275 ldap_memfree( match.bv_val );
1278 /* add references to array */
1283 for ( cnt = 0; references[ cnt ]; cnt++ )
1286 sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
1288 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1289 ber_str2bv( references[ cnt ], 0, 1, &sr_ref[ cnt ] );
1291 BER_BVZERO( &sr_ref[ cnt ] );
1293 ( void )ldap_back_referral_result_rewrite( &dc, sr_ref );
1296 ber_memvfree( (void **)references );
1298 if ( rs->sr_v2ref == NULL ) {
1299 rs->sr_v2ref = sr_ref;
1302 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
1303 ber_bvarray_add( &rs->sr_v2ref, &sr_ref[ cnt ] );
1305 ber_memfree( sr_ref );
1309 rs->sr_err = candidates[ i ].sr_err;
1310 sres = slap_map_api2result( rs );
1312 if ( LogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
1313 snprintf( buf, sizeof( buf ),
1314 "%s meta_back_search[%ld] "
1315 "match=\"%s\" err=%ld",
1316 op->o_log_prefix, i,
1317 candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
1318 (long) candidates[ i ].sr_err );
1319 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1320 Debug( LDAP_DEBUG_TRACE, "%s.\n", buf, 0, 0 );
1323 Debug( LDAP_DEBUG_ANY, "%s (%s).\n",
1324 buf, ldap_err2string( candidates[ i ].sr_err ), 0 );
1329 case LDAP_NO_SUCH_OBJECT:
1330 /* is_ok is touched any time a valid
1331 * (even intermediate) result is
1332 * returned; as a consequence, if
1333 * a candidate returns noSuchObject
1334 * it is ignored and the candidate
1335 * is simply demoted. */
1337 sres = LDAP_SUCCESS;
1346 case LDAP_SIZELIMIT_EXCEEDED:
1347 /* if a target returned sizelimitExceeded
1348 * and the entry count is equal to the
1349 * proxy's limit, the target would have
1350 * returned more, and the error must be
1351 * propagated to the client; otherwise,
1352 * the target enforced a limit lower
1353 * than what requested by the proxy;
1355 candidates[ i ].sr_err = rs->sr_err;
1356 if ( rs->sr_nentries == op->ors_slimit
1357 || META_BACK_ONERR_STOP( mi ) )
1359 savepriv = op->o_private;
1360 op->o_private = (void *)i;
1361 send_ldap_result( op, rs );
1362 op->o_private = savepriv;
1363 ldap_msgfree( res );
1370 candidates[ i ].sr_err = rs->sr_err;
1371 if ( META_BACK_ONERR_STOP( mi ) ) {
1372 savepriv = op->o_private;
1373 op->o_private = (void *)i;
1374 send_ldap_result( op, rs );
1375 op->o_private = savepriv;
1376 ldap_msgfree( res );
1387 * When no candidates are left,
1388 * the outer cycle finishes
1390 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1391 assert( ncandidates > 0 );
1394 } else if ( rc == LDAP_RES_BIND ) {
1395 meta_search_candidate_t retcode;
1397 retcode = meta_search_dobind_result( op, rs, &mc, i, candidates, msg );
1398 if ( retcode == META_SEARCH_CANDIDATE ) {
1399 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1400 retcode = meta_back_search_start( op, rs, &dc, &mc, i, candidates );
1403 switch ( retcode ) {
1404 case META_SEARCH_CANDIDATE:
1407 /* means that failed but onerr == continue */
1408 case META_SEARCH_NOT_CANDIDATE:
1409 case META_SEARCH_ERR:
1410 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1411 assert( ncandidates > 0 );
1414 candidates[ i ].sr_err = rs->sr_err;
1415 if ( META_BACK_ONERR_STOP( mi ) ) {
1416 savepriv = op->o_private;
1417 op->o_private = (void *)i;
1418 send_ldap_result( op, rs );
1419 op->o_private = savepriv;
1420 ldap_msgfree( res );
1433 ldap_msgfree( res );
1440 ldap_msgfree( res );
1444 /* check for abandon */
1445 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
1446 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1447 if ( candidates[ i ].sr_msgid >= 0 ) {
1448 if ( META_IS_BINDING( &candidates[ i ] ) ) {
1449 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1450 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ) {
1451 /* if still binding, destroy */
1454 char buf[ SLAP_TEXT_BUFLEN ];
1456 snprintf( buf, sizeof( buf), "%s meta_back_search(abandon) "
1457 "ldap_unbind_ext[%ld] mc=%p ld=%p",
1458 op->o_log_prefix, i, (void *)mc,
1459 (void *)mc->mc_conns[i].msc_ld );
1461 Debug( LDAP_DEBUG_ANY, "### %s\n", buf, 0, 0 );
1462 #endif /* DEBUG_205 */
1464 meta_clear_one_candidate( op, mc, i );
1466 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1467 META_BINDING_CLEAR( &candidates[ i ] );
1470 (void)meta_back_cancel( mc, op, rs,
1471 candidates[ i ].sr_msgid, i,
1472 LDAP_BACK_DONTSEND );
1475 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1476 assert( ncandidates > 0 );
1481 if ( op->o_abandon ) {
1485 /* let send_ldap_result play cleanup handlers (ITS#4645) */
1489 /* if no entry was found during this loop,
1490 * set a minimal timeout */
1491 if ( ncandidates > 0 && gotit == 0 ) {
1492 if ( save_tv.tv_sec == 0 && save_tv.tv_usec == 0 ) {
1493 save_tv.tv_usec = LDAP_BACK_RESULT_UTIMEOUT/initial_candidates;
1495 /* arbitrarily limit to something between 1 and 2 minutes */
1496 } else if ( ( stoptime == -1 && save_tv.tv_sec < 60 )
1497 || save_tv.tv_sec < ( stoptime - slap_get_time() ) / ( 2 * ncandidates ) )
1499 /* double the timeout */
1500 lutil_timermul( &save_tv, 2, &save_tv );
1503 if ( alreadybound == 0 ) {
1505 (void)select( 0, NULL, NULL, NULL, &tv );
1508 ldap_pvt_thread_yield();
1515 * FIXME: need a better strategy to handle errors
1518 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE,
1519 -1, stoptime != -1 ? (stoptime - slap_get_time()) : 0,
1520 LDAP_BACK_SENDERR );
1528 * Rewrite the matched portion of the search base, if required
1530 * FIXME: only the last one gets caught!
1532 savepriv = op->o_private;
1533 op->o_private = (void *)(long)mi->mi_ntargets;
1534 if ( candidate_match > 0 ) {
1535 struct berval pmatched = BER_BVNULL;
1537 /* we use the first one */
1538 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1539 if ( META_IS_CANDIDATE( &candidates[ i ] )
1540 && candidates[ i ].sr_matched != NULL )
1542 struct berval bv, pbv;
1545 /* if we got success, and this target
1546 * returned noSuchObject, and its suffix
1547 * is a superior of the searchBase,
1548 * ignore the matchedDN */
1549 if ( sres == LDAP_SUCCESS
1550 && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
1551 && op->o_req_ndn.bv_len > mi->mi_targets[ i ]->mt_nsuffix.bv_len )
1553 free( (char *)candidates[ i ].sr_matched );
1554 candidates[ i ].sr_matched = NULL;
1558 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
1559 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
1561 if ( rc == LDAP_SUCCESS ) {
1563 /* NOTE: if they all are superiors
1564 * of the baseDN, the shorter is also
1565 * superior of the longer... */
1566 if ( pbv.bv_len > pmatched.bv_len ) {
1567 if ( !BER_BVISNULL( &pmatched ) ) {
1568 op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
1571 op->o_private = (void *)i;
1574 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
1578 if ( candidates[ i ].sr_matched != NULL ) {
1579 free( (char *)candidates[ i ].sr_matched );
1580 candidates[ i ].sr_matched = NULL;
1585 if ( !BER_BVISNULL( &pmatched ) ) {
1586 matched = pmatched.bv_val;
1589 } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
1590 matched = op->o_bd->be_suffix[ 0 ].bv_val;
1594 * In case we returned at least one entry, we return LDAP_SUCCESS
1595 * otherwise, the latter error code we got
1598 if ( sres == LDAP_SUCCESS ) {
1599 if ( rs->sr_v2ref ) {
1600 sres = LDAP_REFERRAL;
1603 if ( META_BACK_ONERR_REPORT( mi ) ) {
1605 * Report errors, if any
1607 * FIXME: we should handle error codes and return the more
1608 * important/reasonable
1610 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1611 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1615 if ( candidates[ i ].sr_err != LDAP_SUCCESS
1616 && candidates[ i ].sr_err != LDAP_NO_SUCH_OBJECT )
1618 sres = candidates[ i ].sr_err;
1626 rs->sr_matched = matched;
1627 rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
1628 send_ldap_result( op, rs );
1629 op->o_private = savepriv;
1630 rs->sr_matched = NULL;
1634 if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
1635 op->o_tmpfree( matched, op->o_tmpmemctx );
1638 if ( rs->sr_v2ref ) {
1639 ber_bvarray_free( rs->sr_v2ref );
1642 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1643 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1647 if ( mc && META_IS_BINDING( &candidates[ i ] ) ) {
1648 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1649 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ) {
1650 assert( candidates[ i ].sr_msgid >= 0 );
1651 assert( mc->mc_conns[ i ].msc_ld != NULL );
1654 Debug( LDAP_DEBUG_ANY, "### %s meta_back_search(cleanup) "
1655 "ldap_unbind_ext[%ld] ld=%p\n",
1656 op->o_log_prefix, i, (void *)mc->mc_conns[i].msc_ld );
1657 #endif /* DEBUG_205 */
1659 /* if still binding, destroy */
1660 meta_clear_one_candidate( op, mc, i );
1662 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1663 META_BINDING_CLEAR( &candidates[ i ] );
1666 if ( candidates[ i ].sr_matched ) {
1667 free( (char *)candidates[ i ].sr_matched );
1668 candidates[ i ].sr_matched = NULL;
1671 if ( candidates[ i ].sr_text ) {
1672 ldap_memfree( (char *)candidates[ i ].sr_text );
1673 candidates[ i ].sr_text = NULL;
1676 if ( candidates[ i ].sr_ref ) {
1677 ber_bvarray_free( candidates[ i ].sr_ref );
1678 candidates[ i ].sr_ref = NULL;
1681 if ( candidates[ i ].sr_ctrls ) {
1682 ldap_controls_free( candidates[ i ].sr_ctrls );
1683 candidates[ i ].sr_ctrls = NULL;
1686 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1687 meta_back_quarantine( op, &candidates[ i ], i );
1690 /* only in case of timelimit exceeded, if the timelimit exceeded because
1691 * one contacted target never responded, invalidate the connection
1692 * NOTE: should we quarantine the target as well? right now, the connection
1693 * is invalidated; the next time it will be recreated and the target
1694 * will be quarantined if it cannot be contacted */
1695 if ( mi->mi_idle_timeout != 0
1696 && rs->sr_err == LDAP_TIMELIMIT_EXCEEDED
1697 && op->o_time > mc->mc_conns[ i ].msc_time )
1699 /* don't let anyone else use this expired connection */
1700 LDAP_BACK_CONN_TAINTED_SET( mc );
1705 meta_back_release_conn( mi, mc );
1719 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1720 struct berval a, mapped;
1722 BerElement ber = *e->lm_ber;
1723 Attribute *attr, **attrp;
1730 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
1731 return LDAP_DECODING_ERROR;
1735 * Rewrite the dn of the result, if needed
1737 dc.target = mi->mi_targets[ target ];
1738 dc.conn = op->o_conn;
1740 dc.ctx = "searchResult";
1742 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
1743 if ( rs->sr_err != LDAP_SUCCESS) {
1748 * Note: this may fail if the target host(s) schema differs
1749 * from the one known to the meta, and a DN with unknown
1750 * attributes is returned.
1752 * FIXME: should we log anything, or delegate to dnNormalize?
1754 rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
1756 if ( dn.bv_val != bdn.bv_val ) {
1761 if ( rc != LDAP_SUCCESS ) {
1762 return LDAP_INVALID_DN_SYNTAX;
1768 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1769 ( void )meta_dncache_update_entry( &mi->mi_cache,
1770 &ent.e_nname, target );
1773 attrp = &ent.e_attrs;
1775 dc.ctx = "searchAttrDN";
1776 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
1778 slap_syntax_validate_func *validate;
1779 slap_syntax_transform_func *pretty;
1781 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_at,
1782 &a, &mapped, BACKLDAP_REMAP );
1783 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
1784 ( void )ber_scanf( &ber, "x" /* [W] */ );
1787 attr = attr_alloc( NULL );
1788 if ( attr == NULL ) {
1791 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
1793 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
1794 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
1796 char buf[ SLAP_TEXT_BUFLEN ];
1798 snprintf( buf, sizeof( buf ),
1799 "%s meta_send_entry(\"%s\"): "
1800 "slap_bv2undef_ad(%s): %s\n",
1801 op->o_log_prefix, ent.e_name.bv_val,
1802 mapped.bv_val, text );
1804 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1810 /* no subschemaSubentry */
1811 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
1812 || attr->a_desc == slap_schema.si_ad_entryDN )
1816 * We eat target's subschemaSubentry because
1817 * a search for this value is likely not
1818 * to resolve to the appropriate backend;
1819 * later, the local subschemaSubentry is
1822 * We also eat entryDN because the frontend
1823 * will reattach it without checking if already
1826 ( void )ber_scanf( &ber, "x" /* [W] */ );
1832 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
1833 || attr->a_vals == NULL )
1835 attr->a_vals = (struct berval *)&slap_dummy_bv;
1838 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
1842 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
1843 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
1845 if ( !validate && !pretty ) {
1850 if ( attr->a_desc == slap_schema.si_ad_objectClass
1851 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
1855 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1856 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_oc,
1857 bv, &mapped, BACKLDAP_REMAP );
1858 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
1864 *bv = attr->a_vals[ last ];
1865 BER_BVZERO( &attr->a_vals[ last ] );
1868 } else if ( mapped.bv_val != bv->bv_val ) {
1870 ber_dupbv( bv, &mapped );
1874 * It is necessary to try to rewrite attributes with
1875 * dn syntax because they might be used in ACLs as
1876 * members of groups; since ACLs are applied to the
1877 * rewritten stuff, no dn-based subecj clause could
1878 * be used at the ldap backend side (see
1879 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1880 * The problem can be overcome by moving the dn-based
1881 * ACLs to the target directory server, and letting
1882 * everything pass thru the ldap backend.
1887 if ( attr->a_desc->ad_type->sat_syntax ==
1888 slap_schema.si_syn_distinguishedName )
1890 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
1892 } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
1893 ldap_back_referral_result_rewrite( &dc, attr->a_vals );
1897 for ( i = 0; i < last; i++ ) {
1902 rc = pretty( attr->a_desc->ad_type->sat_syntax,
1903 &attr->a_vals[i], &pval, NULL );
1906 rc = validate( attr->a_desc->ad_type->sat_syntax,
1911 LBER_FREE( attr->a_vals[i].bv_val );
1912 if ( --last == i ) {
1913 BER_BVZERO( &attr->a_vals[ i ] );
1916 attr->a_vals[i] = attr->a_vals[last];
1917 BER_BVZERO( &attr->a_vals[last] );
1923 LBER_FREE( attr->a_vals[i].bv_val );
1924 attr->a_vals[i] = pval;
1928 if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
1934 if ( last && attr->a_desc->ad_type->sat_equality &&
1935 attr->a_desc->ad_type->sat_equality->smr_normalize )
1939 attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
1940 for ( i = 0; i<last; i++ ) {
1941 attr->a_desc->ad_type->sat_equality->smr_normalize(
1942 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1943 attr->a_desc->ad_type->sat_syntax,
1944 attr->a_desc->ad_type->sat_equality,
1945 &attr->a_vals[i], &attr->a_nvals[i],
1948 BER_BVZERO( &attr->a_nvals[i] );
1951 attr->a_nvals = attr->a_vals;
1955 attrp = &attr->a_next;
1958 rs->sr_entry = &ent;
1959 rs->sr_attrs = op->ors_attrs;
1961 rs->sr_err = LDAP_SUCCESS;
1962 rc = send_search_entry( op, rs );
1964 case LDAP_UNAVAILABLE:
1968 rs->sr_entry = NULL;
1969 rs->sr_attrs = NULL;
1971 if ( !BER_BVISNULL( &ent.e_name ) ) {
1972 free( ent.e_name.bv_val );
1973 BER_BVZERO( &ent.e_name );
1975 if ( !BER_BVISNULL( &ent.e_nname ) ) {
1976 free( ent.e_nname.bv_val );
1977 BER_BVZERO( &ent.e_nname );
1979 entry_clean( &ent );