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 rc = ldap_sasl_bind( msc->msc_ld, binddn.bv_val, LDAP_SASL_SIMPLE, &cred,
233 NULL, NULL, &candidates[ candidate ].sr_msgid );
237 char buf[ SLAP_TEXT_BUFLEN ];
239 snprintf( buf, sizeof( buf ), "meta_search_dobind_init[%d] mc=%p ld=%p rc=%d",
240 candidate, (void *)mc, (void *)mc->mc_conns[ candidate ].msc_ld, rc );
241 Debug( LDAP_DEBUG_ANY, "### %s %s\n",
242 op->o_log_prefix, buf, 0 );
244 #endif /* DEBUG_205 */
248 assert( candidates[ candidate ].sr_msgid >= 0 );
249 META_BINDING_SET( &candidates[ candidate ] );
250 return META_SEARCH_BINDING;
252 case LDAP_SERVER_DOWN:
254 /* This is the worst thing that could happen:
255 * the search will wait until the retry is over. */
256 if ( meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
257 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
258 return META_SEARCH_CANDIDATE;
261 if ( *mcp == NULL ) {
262 retcode = META_SEARCH_ERR;
263 rs->sr_err = LDAP_UNAVAILABLE;
271 rc = slap_map_api2result( rs );
273 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
274 meta_clear_one_candidate( op, mc, candidate );
275 if ( META_BACK_ONERR_STOP( mi ) ) {
276 LDAP_BACK_CONN_TAINTED_SET( mc );
277 meta_back_release_conn_lock( op, mc, 0 );
280 retcode = META_SEARCH_ERR;
283 if ( META_BACK_ONERR_REPORT( mi ) ) {
284 candidates[ candidate ].sr_err = rc;
287 retcode = META_SEARCH_NOT_CANDIDATE;
289 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
290 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
297 static meta_search_candidate_t
298 meta_search_dobind_result(
303 SlapReply *candidates,
306 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
307 metaconn_t *mc = *mcp;
308 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
310 meta_search_candidate_t retcode = META_SEARCH_NOT_CANDIDATE;
313 assert( msc->msc_ld != NULL );
315 /* FIXME: matched? referrals? response controls? */
316 rc = ldap_parse_result( msc->msc_ld, res,
317 &candidates[ candidate ].sr_err,
318 NULL, NULL, NULL, NULL, 0 );
319 if ( rc != LDAP_SUCCESS ) {
320 candidates[ candidate ].sr_err = rc;
323 rc = slap_map_api2result( &candidates[ candidate ] );
326 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
327 LDAP_BACK_CONN_BINDING_CLEAR( msc );
328 if ( rc != LDAP_SUCCESS ) {
329 if ( META_BACK_ONERR_STOP( mi ) ) {
330 LDAP_BACK_CONN_TAINTED_SET( mc );
331 meta_clear_one_candidate( op, mc, candidate );
332 meta_back_release_conn_lock( op, mc, 0 );
334 retcode = META_SEARCH_ERR;
337 } else if ( META_BACK_ONERR_REPORT( mi ) ) {
338 candidates[ candidate ].sr_err = rc;
342 /* FIXME: check if bound as idassert authcDN! */
343 if ( BER_BVISNULL( &msc->msc_bound_ndn )
344 || BER_BVISEMPTY( &msc->msc_bound_ndn ) )
346 LDAP_BACK_CONN_ISANON_SET( msc );
349 LDAP_BACK_CONN_ISBOUND_SET( msc );
351 retcode = META_SEARCH_CANDIDATE;
354 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
355 META_BINDING_CLEAR( &candidates[ candidate ] );
357 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
362 static meta_search_candidate_t
363 meta_back_search_start(
369 SlapReply *candidates )
371 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
372 metatarget_t *mt = mi->mi_targets[ candidate ];
373 metasingleconn_t *msc = &(*mcp)->mc_conns[ candidate ];
374 struct berval realbase = op->o_req_dn;
375 int realscope = op->ors_scope;
376 struct berval mbase = BER_BVNULL;
377 struct berval mfilter = BER_BVNULL;
378 char **mapped_attrs = NULL;
380 meta_search_candidate_t retcode;
381 struct timeval tv, *tvp = NULL;
383 LDAPControl **ctrls = NULL;
385 /* this should not happen; just in case... */
386 if ( msc->msc_ld == NULL ) {
387 Debug( LDAP_DEBUG_ANY,
388 "%s: meta_back_search_start candidate=%d ld=NULL%s.\n",
389 op->o_log_prefix, candidate,
390 META_BACK_ONERR_STOP( mi ) ? "" : " (ignored)" );
391 if ( META_BACK_ONERR_STOP( mi ) ) {
392 return META_SEARCH_ERR;
394 if ( META_BACK_ONERR_REPORT( mi ) ) {
395 candidates[ candidate ].sr_err = LDAP_OTHER;
397 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
398 return META_SEARCH_NOT_CANDIDATE;
401 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_back_search_start[%d]\n", op->o_log_prefix, candidate, 0 );
404 * modifies the base according to the scope, if required
406 if ( mt->mt_nsuffix.bv_len > op->o_req_ndn.bv_len ) {
407 switch ( op->ors_scope ) {
408 case LDAP_SCOPE_SUBTREE:
410 * make the target suffix the new base
411 * FIXME: this is very forgiving, because
412 * "illegal" searchBases may be turned
413 * into the suffix of the target; however,
414 * the requested searchBase already passed
415 * thru the candidate analyzer...
417 if ( dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) ) {
418 realbase = mt->mt_nsuffix;
419 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
420 realscope = LDAP_SCOPE_SUBORDINATE;
425 * this target is no longer candidate
427 retcode = META_SEARCH_NOT_CANDIDATE;
432 case LDAP_SCOPE_SUBORDINATE:
433 case LDAP_SCOPE_ONELEVEL:
435 struct berval rdn = mt->mt_nsuffix;
436 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
437 if ( dnIsOneLevelRDN( &rdn )
438 && dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) )
441 * if there is exactly one level,
442 * make the target suffix the new
443 * base, and make scope "base"
445 realbase = mt->mt_nsuffix;
446 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
447 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
448 realscope = LDAP_SCOPE_SUBORDINATE;
450 realscope = LDAP_SCOPE_SUBTREE;
453 realscope = LDAP_SCOPE_BASE;
456 } /* else continue with the next case */
459 case LDAP_SCOPE_BASE:
461 * this target is no longer candidate
463 retcode = META_SEARCH_NOT_CANDIDATE;
468 /* initiate dobind */
469 retcode = meta_search_dobind_init( op, rs, mcp, candidate, candidates );
471 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%d]=%d\n", op->o_log_prefix, candidate, retcode );
473 if ( retcode != META_SEARCH_CANDIDATE ) {
478 * Rewrite the search base, if required
481 dc->ctx = "searchBase";
482 switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
486 case LDAP_UNWILLING_TO_PERFORM:
487 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
488 rs->sr_text = "Operation not allowed";
489 send_ldap_result( op, rs );
490 retcode = META_SEARCH_ERR;
496 * this target is no longer candidate
498 retcode = META_SEARCH_NOT_CANDIDATE;
505 rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
506 &mfilter, BACKLDAP_MAP );
511 case LDAP_COMPARE_FALSE:
514 * this target is no longer candidate
516 retcode = META_SEARCH_NOT_CANDIDATE;
521 * Maps required attributes
523 rc = ldap_back_map_attrs( &mt->mt_rwmap.rwm_at,
524 op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
525 if ( rc != LDAP_SUCCESS ) {
527 * this target is no longer candidate
529 retcode = META_SEARCH_NOT_CANDIDATE;
533 /* should we check return values? */
534 if ( op->ors_deref != -1 ) {
535 assert( msc->msc_ld != NULL );
536 (void)ldap_set_option( msc->msc_ld, LDAP_OPT_DEREF,
537 ( void * )&op->ors_deref );
540 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
541 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
548 if ( ldap_back_proxy_authz_ctrl( &msc->msc_bound_ndn,
549 mt->mt_version, &mt->mt_idassert, op, rs, &ctrls )
552 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
553 retcode = META_SEARCH_NOT_CANDIDATE;
560 assert( msc->msc_ld != NULL );
561 rc = ldap_search_ext( msc->msc_ld,
562 mbase.bv_val, realscope, mfilter.bv_val,
563 mapped_attrs, op->ors_attrsonly,
564 ctrls, NULL, tvp, op->ors_slimit,
565 &candidates[ candidate ].sr_msgid );
568 retcode = META_SEARCH_CANDIDATE;
571 case LDAP_SERVER_DOWN:
572 if ( nretries && meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
574 /* if the identity changed, there might be need to re-authz */
575 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
579 if ( *mcp == NULL ) {
580 retcode = META_SEARCH_ERR;
586 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
587 retcode = META_SEARCH_NOT_CANDIDATE;
591 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
593 if ( mapped_attrs ) {
594 free( mapped_attrs );
596 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
597 free( mfilter.bv_val );
599 if ( mbase.bv_val != realbase.bv_val ) {
600 free( mbase.bv_val );
604 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_back_search_start[%d]=%d\n", op->o_log_prefix, candidate, retcode );
610 meta_back_search( Operation *op, SlapReply *rs )
612 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
614 struct timeval save_tv = { 0, 0 },
616 time_t stoptime = (time_t)-1;
617 int rc = 0, sres = LDAP_SUCCESS;
618 char *matched = NULL;
619 int last = 0, ncandidates = 0,
620 initial_candidates = 0, candidate_match = 0,
622 ldap_back_send_t sendok = LDAP_BACK_SENDERR;
627 SlapReply *candidates = meta_back_candidates_get( op );
630 * controls are set in ldap_back_dobind()
632 * FIXME: in case of values return filter, we might want
633 * to map attrs and maybe rewrite value
636 mc = meta_back_getconn( op, rs, NULL, sendok );
641 dc.conn = op->o_conn;
647 for ( i = 0; i < mi->mi_ntargets; i++ ) {
648 /* reset sr_msgid; it is used in most loops
649 * to check if that target is still to be considered */
650 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
652 /* a target is marked as candidate by meta_back_getconn();
653 * if for any reason (an error, it's over or so) it is
654 * no longer active, sr_msgid is set to META_MSGID_IGNORE
655 * but it remains candidate, which means it has been active
656 * at some point during the operation. This allows to
657 * use its response code and more to compute the final
659 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
663 candidates[ i ].sr_matched = NULL;
664 candidates[ i ].sr_text = NULL;
665 candidates[ i ].sr_ref = NULL;
666 candidates[ i ].sr_ctrls = NULL;
669 for ( i = 0; i < mi->mi_ntargets; i++ ) {
670 if ( !META_IS_CANDIDATE( &candidates[ i ] )
671 || candidates[ i ].sr_err != LDAP_SUCCESS )
676 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
678 case META_SEARCH_NOT_CANDIDATE:
679 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
682 case META_SEARCH_NEED_BIND:
686 case META_SEARCH_CANDIDATE:
687 case META_SEARCH_BINDING:
688 candidates[ i ].sr_type = REP_INTERMEDIATE;
692 case META_SEARCH_ERR:
693 savepriv = op->o_private;
694 op->o_private = (void *)i;
695 send_ldap_result( op, rs );
696 op->o_private = savepriv;
702 if ( ncandidates > 0 && needbind == ncandidates ) {
704 * give up the second time...
706 * NOTE: this should not occur the second time, since a fresh
707 * connection has ben created; however, targets may also
708 * need bind because the bind timed out or so.
710 if ( sendok & LDAP_BACK_BINDING ) {
711 Debug( LDAP_DEBUG_ANY,
712 "%s meta_back_search: unable to initialize conn\n",
713 op->o_log_prefix, 0, 0 );
714 rs->sr_err = LDAP_UNAVAILABLE;
715 rs->sr_text = "unable to initialize connection to remote targets";
716 send_ldap_result( op, rs );
721 /* FIXME: better create a separate connection? */
722 sendok |= LDAP_BACK_BINDING;
725 Debug( LDAP_DEBUG_ANY, "*** %s drop mc=%p create new connection\n",
726 op->o_log_prefix, (void *)mc, 0 );
727 #endif /* DEBUG_205 */
729 meta_back_release_conn( op, mc );
738 initial_candidates = ncandidates;
740 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
741 char cnd[ SLAP_TEXT_BUFLEN ];
744 for ( c = 0; c < mi->mi_ntargets; c++ ) {
745 if ( META_IS_CANDIDATE( &candidates[ c ] ) ) {
753 Debug( LDAP_DEBUG_TRACE, "%s meta_back_search: ncandidates=%d "
754 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
757 if ( initial_candidates == 0 ) {
758 /* NOTE: here we are not sending any matchedDN;
759 * this is intended, because if the back-meta
760 * is serving this search request, but no valid
761 * candidate could be looked up, it means that
762 * there is a hole in the mapping of the targets
763 * and thus no knowledge of any remote superior
765 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
766 "base=\"%s\" scope=%d: "
767 "no candidate could be selected\n",
768 op->o_log_prefix, op->o_req_dn.bv_val,
771 /* FIXME: we're sending the first error we encounter;
772 * maybe we should pick the worst... */
773 rc = LDAP_NO_SUCH_OBJECT;
774 for ( i = 0; i < mi->mi_ntargets; i++ ) {
775 if ( META_IS_CANDIDATE( &candidates[ i ] )
776 && candidates[ i ].sr_err != LDAP_SUCCESS )
778 rc = candidates[ i ].sr_err;
783 send_ldap_error( op, rs, rc, NULL );
788 /* We pull apart the ber result, stuff it into a slapd entry, and
789 * let send_search_entry stuff it back into ber format. Slow & ugly,
790 * but this is necessary for version matching, and for ACL processing.
793 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
794 stoptime = op->o_time + op->ors_tlimit;
798 * In case there are no candidates, no cycle takes place...
800 * FIXME: we might use a queue, to better balance the load
801 * among the candidates
803 for ( rc = 0; ncandidates > 0; ) {
806 alreadybound = ncandidates;
808 /* check time limit */
809 if ( op->ors_tlimit != SLAP_NO_LIMIT
810 && slap_get_time() > stoptime )
813 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
814 savepriv = op->o_private;
815 op->o_private = (void *)i;
816 send_ldap_result( op, rs );
817 op->o_private = savepriv;
821 for ( i = 0; i < mi->mi_ntargets; i++ ) {
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 meta_search_candidate_t retcode;
834 /* initiate dobind */
835 retcode = meta_search_dobind_init( op, rs, &mc, i, candidates );
837 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%ld]=%d\n",
838 op->o_log_prefix, i, retcode );
841 case META_SEARCH_NEED_BIND:
845 case META_SEARCH_BINDING:
848 case META_SEARCH_ERR:
849 if ( META_BACK_ONERR_STOP( mi ) ) {
850 savepriv = op->o_private;
851 op->o_private = (void *)i;
852 send_ldap_result( op, rs );
853 op->o_private = savepriv;
856 if ( META_BACK_ONERR_REPORT( mi ) ) {
857 candidates[ i ].sr_err = rs->sr_err;
861 case META_SEARCH_NOT_CANDIDATE:
863 * When no candidates are left,
864 * the outer cycle finishes
866 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
867 assert( ncandidates > 0 );
871 case META_SEARCH_CANDIDATE:
872 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
873 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
875 case META_SEARCH_CANDIDATE:
876 assert( candidates[ i ].sr_msgid >= 0 );
879 case META_SEARCH_ERR:
880 if ( META_BACK_ONERR_STOP( mi ) ) {
881 savepriv = op->o_private;
882 op->o_private = (void *)i;
883 send_ldap_result( op, rs );
884 op->o_private = savepriv;
887 if ( META_BACK_ONERR_REPORT( mi ) ) {
888 candidates[ i ].sr_err = rs->sr_err;
892 case META_SEARCH_NOT_CANDIDATE:
893 /* means that meta_back_search_start()
894 * failed but onerr == continue */
895 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
896 assert( ncandidates > 0 );
915 /* check for abandon */
916 if ( op->o_abandon ) {
921 if ( msc->msc_ld == NULL ) {
922 char buf[ SLAP_TEXT_BUFLEN ];
924 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
925 snprintf( buf, sizeof( buf ),
926 "%s meta_back_search[%ld] mc=%p msgid=%d%s%s%s\n",
927 op->o_log_prefix, (long)i, (void *)mc,
928 candidates[ i ].sr_msgid,
929 META_IS_BINDING( &candidates[ i ] ) ? " binding" : "",
930 LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ? " connbinding" : "",
931 META_BACK_CONN_CREATING( &mc->mc_conns[ i ] ) ? " conncreating" : "" );
932 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
934 Debug( LDAP_DEBUG_ANY, "!!! %s\n", buf, 0, 0 );
936 #endif /* DEBUG_205 */
939 * FIXME: handle time limit as well?
940 * Note that target servers are likely
941 * to handle it, so at some time we'll
942 * get a LDAP_TIMELIMIT_EXCEEDED from
946 rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
947 LDAP_MSG_RECEIVED, &tv, &res );
950 /* FIXME: res should not need to be freed */
951 assert( res == NULL );
956 /* something REALLY bad happened! */
957 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
958 candidates[ i ].sr_type = REP_RESULT;
960 if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
961 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
962 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
964 case META_SEARCH_CANDIDATE:
965 /* get back into business... */
968 /* means that failed but onerr == continue */
969 case META_SEARCH_NOT_CANDIDATE:
970 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
973 if ( META_BACK_ONERR_STOP( mi ) ) {
974 savepriv = op->o_private;
975 op->o_private = (void *)i;
976 send_ldap_result( op, rs );
977 op->o_private = savepriv;
980 if ( META_BACK_ONERR_REPORT( mi ) ) {
981 candidates[ i ].sr_err = rs->sr_err;
985 case META_SEARCH_BINDING:
986 case META_SEARCH_NEED_BIND:
990 /* unrecoverable error */
991 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
992 rc = rs->sr_err = LDAP_OTHER;
997 if ( META_BACK_ONERR_STOP( mi ) ) {
998 savepriv = op->o_private;
999 op->o_private = (void *)i;
1000 send_ldap_result( op, rs );
1001 op->o_private = savepriv;
1004 if ( META_BACK_ONERR_REPORT( mi ) ) {
1005 candidates[ i ].sr_err = rs->sr_err;
1010 * When no candidates are left,
1011 * the outer cycle finishes
1013 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1014 assert( ncandidates > 0 );
1016 rs->sr_err = candidates[ i ].sr_err;
1020 /* only touch when activity actually took place... */
1021 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
1022 msc->msc_time = op->o_time;
1027 for ( msg = ldap_first_message( msc->msc_ld, res );
1029 msg = ldap_next_message( msc->msc_ld, msg ) )
1031 rc = ldap_msgtype( msg );
1032 if ( rc == LDAP_RES_SEARCH_ENTRY ) {
1035 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1036 /* don't retry any more... */
1037 candidates[ i ].sr_type = REP_RESULT;
1042 e = ldap_first_entry( msc->msc_ld, msg );
1043 savepriv = op->o_private;
1044 op->o_private = (void *)i;
1045 rs->sr_err = meta_send_entry( op, rs, mc, i, e );
1047 switch ( rs->sr_err ) {
1048 case LDAP_SIZELIMIT_EXCEEDED:
1049 savepriv = op->o_private;
1050 op->o_private = (void *)i;
1051 send_ldap_result( op, rs );
1052 op->o_private = savepriv;
1053 rs->sr_err = LDAP_SUCCESS;
1054 ldap_msgfree( res );
1058 case LDAP_UNAVAILABLE:
1059 rs->sr_err = LDAP_OTHER;
1060 ldap_msgfree( res );
1064 op->o_private = savepriv;
1066 /* don't wait any longer... */
1069 save_tv.tv_usec = 0;
1071 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
1072 char **references = NULL;
1075 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1076 /* don't retry any more... */
1077 candidates[ i ].sr_type = REP_RESULT;
1082 rc = ldap_parse_reference( msc->msc_ld, msg,
1083 &references, &rs->sr_ctrls, 0 );
1085 if ( rc != LDAP_SUCCESS ) {
1089 if ( references == NULL ) {
1093 #ifdef ENABLE_REWRITE
1094 dc.ctx = "referralDN";
1095 #else /* ! ENABLE_REWRITE */
1098 #endif /* ! ENABLE_REWRITE */
1100 /* FIXME: merge all and return at the end */
1102 for ( cnt = 0; references[ cnt ]; cnt++ )
1105 rs->sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
1107 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1108 ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
1110 BER_BVZERO( &rs->sr_ref[ cnt ] );
1112 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref );
1114 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
1115 /* ignore return value by now */
1116 savepriv = op->o_private;
1117 op->o_private = (void *)i;
1118 ( void )send_search_reference( op, rs );
1119 op->o_private = savepriv;
1121 ber_bvarray_free( rs->sr_ref );
1127 ber_memvfree( (void **)references );
1130 if ( rs->sr_ctrls ) {
1131 ldap_controls_free( rs->sr_ctrls );
1132 rs->sr_ctrls = NULL;
1135 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
1136 char buf[ SLAP_TEXT_BUFLEN ];
1137 char **references = NULL;
1139 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1140 /* don't retry any more... */
1141 candidates[ i ].sr_type = REP_RESULT;
1144 /* NOTE: ignores response controls
1145 * (and intermediate response controls
1146 * as well, except for those with search
1147 * references); this may not be correct,
1148 * but if they're not ignored then
1149 * back-meta would need to merge them
1150 * consistently (think of pagedResults...)
1152 /* FIXME: response controls? */
1153 rs->sr_err = ldap_parse_result( msc->msc_ld,
1155 &candidates[ i ].sr_err,
1156 (char **)&candidates[ i ].sr_matched,
1157 NULL /* (char **)&candidates[ i ].sr_text */ ,
1159 NULL /* &candidates[ i ].sr_ctrls (unused) */ ,
1161 if ( rs->sr_err != LDAP_SUCCESS ) {
1162 ldap_get_option( msc->msc_ld,
1163 LDAP_OPT_ERROR_NUMBER,
1165 sres = slap_map_api2result( rs );
1166 candidates[ i ].sr_type = REP_RESULT;
1167 ldap_msgfree( res );
1172 /* massage matchedDN if need be */
1173 if ( candidates[ i ].sr_matched != NULL ) {
1174 struct berval match, mmatch;
1176 ber_str2bv( candidates[ i ].sr_matched,
1178 candidates[ i ].sr_matched = NULL;
1180 dc.ctx = "matchedDN";
1181 dc.target = mi->mi_targets[ i ];
1182 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
1183 if ( mmatch.bv_val == match.bv_val ) {
1184 candidates[ i ].sr_matched
1185 = ch_strdup( mmatch.bv_val );
1188 candidates[ i ].sr_matched = mmatch.bv_val;
1193 ldap_memfree( match.bv_val );
1196 /* add references to array */
1201 for ( cnt = 0; references[ cnt ]; cnt++ )
1204 sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
1206 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1207 ber_str2bv( references[ cnt ], 0, 1, &sr_ref[ cnt ] );
1209 BER_BVZERO( &sr_ref[ cnt ] );
1211 ( void )ldap_back_referral_result_rewrite( &dc, sr_ref );
1214 ber_memvfree( (void **)references );
1216 if ( rs->sr_v2ref == NULL ) {
1217 rs->sr_v2ref = sr_ref;
1220 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
1221 ber_bvarray_add( &rs->sr_v2ref, &sr_ref[ cnt ] );
1223 ber_memfree( sr_ref );
1227 rs->sr_err = candidates[ i ].sr_err;
1228 sres = slap_map_api2result( rs );
1230 if ( LogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
1231 snprintf( buf, sizeof( buf ),
1232 "%s meta_back_search[%ld] "
1233 "match=\"%s\" err=%ld",
1234 op->o_log_prefix, i,
1235 candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
1236 (long) candidates[ i ].sr_err );
1237 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1238 Debug( LDAP_DEBUG_TRACE, "%s.\n", buf, 0, 0 );
1241 Debug( LDAP_DEBUG_ANY, "%s (%s).\n",
1242 buf, ldap_err2string( candidates[ i ].sr_err ), 0 );
1247 case LDAP_NO_SUCH_OBJECT:
1248 /* is_ok is touched any time a valid
1249 * (even intermediate) result is
1250 * returned; as a consequence, if
1251 * a candidate returns noSuchObject
1252 * it is ignored and the candidate
1253 * is simply demoted. */
1255 sres = LDAP_SUCCESS;
1264 case LDAP_SIZELIMIT_EXCEEDED:
1265 /* if a target returned sizelimitExceeded
1266 * and the entry count is equal to the
1267 * proxy's limit, the target would have
1268 * returned more, and the error must be
1269 * propagated to the client; otherwise,
1270 * the target enforced a limit lower
1271 * than what requested by the proxy;
1273 if ( rs->sr_nentries == op->ors_slimit
1274 || META_BACK_ONERR_STOP( mi ) )
1276 savepriv = op->o_private;
1277 op->o_private = (void *)i;
1278 send_ldap_result( op, rs );
1279 op->o_private = savepriv;
1280 ldap_msgfree( res );
1284 if ( META_BACK_ONERR_REPORT( mi ) ) {
1285 candidates[ i ].sr_err = rs->sr_err;
1290 if ( META_BACK_ONERR_STOP( mi ) ) {
1291 savepriv = op->o_private;
1292 op->o_private = (void *)i;
1293 send_ldap_result( op, rs );
1294 op->o_private = savepriv;
1295 ldap_msgfree( res );
1299 if ( META_BACK_ONERR_REPORT( mi ) ) {
1300 candidates[ i ].sr_err = rs->sr_err;
1309 * When no candidates are left,
1310 * the outer cycle finishes
1312 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1315 } else if ( rc == LDAP_RES_BIND ) {
1316 meta_search_candidate_t retcode;
1318 retcode = meta_search_dobind_result( op, rs, &mc, i, candidates, msg );
1319 if ( retcode == META_SEARCH_CANDIDATE ) {
1320 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1321 retcode = meta_back_search_start( op, rs, &dc, &mc, i, candidates );
1324 switch ( retcode ) {
1325 case META_SEARCH_CANDIDATE:
1328 /* means that failed but onerr == continue */
1329 case META_SEARCH_NOT_CANDIDATE:
1330 case META_SEARCH_ERR:
1331 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1334 if ( META_BACK_ONERR_STOP( mi ) ) {
1335 savepriv = op->o_private;
1336 op->o_private = (void *)i;
1337 send_ldap_result( op, rs );
1338 op->o_private = savepriv;
1339 ldap_msgfree( res );
1343 if ( META_BACK_ONERR_REPORT( mi ) ) {
1344 candidates[ i ].sr_err = rs->sr_err;
1355 ldap_msgfree( res );
1361 ldap_msgfree( res );
1365 /* check for abandon */
1366 if ( op->o_abandon || doabandon ) {
1367 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1368 if ( candidates[ i ].sr_msgid >= 0 ) {
1369 if ( META_IS_BINDING( &candidates[ i ] ) ) {
1370 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1371 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ) {
1372 /* if still binding, destroy */
1375 char buf[ SLAP_TEXT_BUFLEN ];
1377 snprintf( buf, sizeof( buf), "%s meta_back_search(abandon) "
1378 "ldap_unbind_ext[%ld] mc=%p ld=%p",
1379 op->o_log_prefix, i, (void *)mc,
1380 (void *)mc->mc_conns[i].msc_ld );
1382 Debug( LDAP_DEBUG_ANY, "### %s\n", buf, 0, 0 );
1383 #endif /* DEBUG_205 */
1385 meta_clear_one_candidate( op, mc, i );
1387 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1388 META_BINDING_CLEAR( &candidates[ i ] );
1391 (void)meta_back_cancel( mc, op, rs,
1392 candidates[ i ].sr_msgid, i,
1393 LDAP_BACK_DONTSEND );
1396 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1397 assert( ncandidates > 0 );
1402 if ( op->o_abandon ) {
1404 /* let send_ldap_result play cleanup handlers (ITS#4645) */
1409 /* if no entry was found during this loop,
1410 * set a minimal timeout */
1411 if ( ncandidates > 0 && gotit == 0 ) {
1412 if ( save_tv.tv_sec == 0 && save_tv.tv_usec == 0 ) {
1413 save_tv.tv_usec = LDAP_BACK_RESULT_UTIMEOUT/initial_candidates;
1415 /* arbitrarily limit to something between 1 and 2 minutes */
1416 } else if ( ( stoptime == -1 && save_tv.tv_sec < 60 )
1417 || save_tv.tv_sec < ( stoptime - slap_get_time() ) / ( 2 * ncandidates ) )
1419 /* double the timeout */
1420 lutil_timermul( &save_tv, 2, &save_tv );
1424 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1425 char buf[ SLAP_TEXT_BUFLEN ];
1427 snprintf( buf, sizeof( buf ), "%s %ld.%06ld %d/%d mc=%p",
1428 op->o_log_prefix, save_tv.tv_sec, save_tv.tv_usec,
1429 ncandidates, initial_candidates, mc );
1430 Debug( LDAP_DEBUG_TRACE, "### %s\n", buf, 0, 0 );
1431 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1432 if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
1436 snprintf( buf, sizeof( buf ), "[%ld] ld=%p%s%s\n",
1438 mc->mc_conns[ i ].msc_ld,
1439 ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND ) ? " needbind" : "",
1440 META_IS_BINDING( &candidates[ i ] ) ? " binding" : "" );
1441 Debug( LDAP_DEBUG_TRACE, "### %s\n", buf, 0, 0 );
1446 if ( alreadybound == 0 ) {
1448 Debug( LDAP_DEBUG_TRACE, "### %s select(%ld.%06ld)\n",
1449 op->o_log_prefix, save_tv.tv_sec, save_tv.tv_usec );
1452 (void)select( 0, NULL, NULL, NULL, &tv );
1455 ldap_pvt_thread_yield();
1462 * FIXME: need a better strategy to handle errors
1465 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE,
1466 -1, stoptime != -1 ? (stoptime - slap_get_time()) : 0,
1467 LDAP_BACK_SENDERR );
1475 * Rewrite the matched portion of the search base, if required
1477 * FIXME: only the last one gets caught!
1479 savepriv = op->o_private;
1480 op->o_private = (void *)(long)mi->mi_ntargets;
1481 if ( candidate_match > 0 ) {
1482 struct berval pmatched = BER_BVNULL;
1484 /* we use the first one */
1485 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1486 if ( META_IS_CANDIDATE( &candidates[ i ] )
1487 && candidates[ i ].sr_matched != NULL )
1489 struct berval bv, pbv;
1492 /* if we got success, and this target
1493 * returned noSuchObject, and its suffix
1494 * is a superior of the searchBase,
1495 * ignore the matchedDN */
1496 if ( sres == LDAP_SUCCESS
1497 && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
1498 && op->o_req_ndn.bv_len > mi->mi_targets[ i ]->mt_nsuffix.bv_len )
1500 free( (char *)candidates[ i ].sr_matched );
1501 candidates[ i ].sr_matched = NULL;
1505 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
1506 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
1508 if ( rc == LDAP_SUCCESS ) {
1510 /* NOTE: if they all are superiors
1511 * of the baseDN, the shorter is also
1512 * superior of the longer... */
1513 if ( pbv.bv_len > pmatched.bv_len ) {
1514 if ( !BER_BVISNULL( &pmatched ) ) {
1515 op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
1518 op->o_private = (void *)i;
1521 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
1525 if ( candidates[ i ].sr_matched != NULL ) {
1526 free( (char *)candidates[ i ].sr_matched );
1527 candidates[ i ].sr_matched = NULL;
1532 if ( !BER_BVISNULL( &pmatched ) ) {
1533 matched = pmatched.bv_val;
1536 } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
1537 matched = op->o_bd->be_suffix[ 0 ].bv_val;
1542 char buf[ SLAP_TEXT_BUFLEN ];
1543 char cnd[ SLAP_TEXT_BUFLEN ];
1546 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1547 if ( META_IS_CANDIDATE( &candidates[ i ] ) ) {
1555 snprintf( buf, sizeof( buf ), "%s meta_back_search: is_scope=%d is_ok=%d cnd=\"%s\"\n",
1556 op->o_log_prefix, initial_candidates, is_ok, cnd );
1558 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1563 * In case we returned at least one entry, we return LDAP_SUCCESS
1564 * otherwise, the latter error code we got
1567 if ( sres == LDAP_SUCCESS ) {
1568 if ( rs->sr_v2ref ) {
1569 sres = LDAP_REFERRAL;
1572 if ( META_BACK_ONERR_REPORT( mi ) ) {
1574 * Report errors, if any
1576 * FIXME: we should handle error codes and return the more
1577 * important/reasonable
1579 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1580 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1584 if ( candidates[ i ].sr_err != LDAP_SUCCESS
1585 && candidates[ i ].sr_err != LDAP_NO_SUCH_OBJECT )
1587 sres = candidates[ i ].sr_err;
1595 rs->sr_matched = matched;
1596 rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
1597 send_ldap_result( op, rs );
1598 op->o_private = savepriv;
1599 rs->sr_matched = NULL;
1603 if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
1604 op->o_tmpfree( matched, op->o_tmpmemctx );
1607 if ( rs->sr_v2ref ) {
1608 ber_bvarray_free( rs->sr_v2ref );
1611 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1612 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1616 if ( mc && META_IS_BINDING( &candidates[ i ] ) ) {
1617 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1618 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ) {
1619 assert( candidates[ i ].sr_msgid >= 0 );
1620 assert( mc->mc_conns[ i ].msc_ld != NULL );
1623 Debug( LDAP_DEBUG_ANY, "### %s meta_back_search(cleanup) "
1624 "ldap_unbind_ext[%ld] ld=%p\n",
1625 op->o_log_prefix, i, (void *)mc->mc_conns[i].msc_ld );
1626 #endif /* DEBUG_205 */
1628 /* if still binding, destroy */
1629 meta_clear_one_candidate( op, mc, i );
1631 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1632 META_BINDING_CLEAR( &candidates[ i ] );
1635 if ( candidates[ i ].sr_matched ) {
1636 free( (char *)candidates[ i ].sr_matched );
1637 candidates[ i ].sr_matched = NULL;
1640 if ( candidates[ i ].sr_text ) {
1641 ldap_memfree( (char *)candidates[ i ].sr_text );
1642 candidates[ i ].sr_text = NULL;
1645 if ( candidates[ i ].sr_ref ) {
1646 ber_bvarray_free( candidates[ i ].sr_ref );
1647 candidates[ i ].sr_ref = NULL;
1650 if ( candidates[ i ].sr_ctrls ) {
1651 ldap_controls_free( candidates[ i ].sr_ctrls );
1652 candidates[ i ].sr_ctrls = NULL;
1655 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1656 meta_back_quarantine( op, &candidates[ i ], i );
1659 /* only in case of timelimit exceeded, if the timelimit exceeded because
1660 * one contacted target never responded, invalidate the connection
1661 * NOTE: should we quarantine the target as well? right now, the connection
1662 * is invalidated; the next time it will be recreated and the target
1663 * will be quarantined if it cannot be contacted */
1664 if ( mi->mi_idle_timeout != 0
1665 && rs->sr_err == LDAP_TIMELIMIT_EXCEEDED
1666 && op->o_time > mc->mc_conns[ i ].msc_time )
1668 /* don't let anyone else use this expired connection */
1669 LDAP_BACK_CONN_TAINTED_SET( mc );
1674 meta_back_release_conn( op, mc );
1688 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1689 struct berval a, mapped;
1691 BerElement ber = *e->lm_ber;
1692 Attribute *attr, **attrp;
1699 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
1700 return LDAP_DECODING_ERROR;
1704 * Rewrite the dn of the result, if needed
1706 dc.target = mi->mi_targets[ target ];
1707 dc.conn = op->o_conn;
1709 dc.ctx = "searchResult";
1711 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
1712 if ( rs->sr_err != LDAP_SUCCESS) {
1717 * Note: this may fail if the target host(s) schema differs
1718 * from the one known to the meta, and a DN with unknown
1719 * attributes is returned.
1721 * FIXME: should we log anything, or delegate to dnNormalize?
1723 rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
1725 if ( dn.bv_val != bdn.bv_val ) {
1730 if ( rc != LDAP_SUCCESS ) {
1731 return LDAP_INVALID_DN_SYNTAX;
1737 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1738 ( void )meta_dncache_update_entry( &mi->mi_cache,
1739 &ent.e_nname, target );
1742 attrp = &ent.e_attrs;
1744 dc.ctx = "searchAttrDN";
1745 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
1747 slap_syntax_validate_func *validate;
1748 slap_syntax_transform_func *pretty;
1750 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_at,
1751 &a, &mapped, BACKLDAP_REMAP );
1752 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
1753 ( void )ber_scanf( &ber, "x" /* [W] */ );
1756 attr = attr_alloc( NULL );
1757 if ( attr == NULL ) {
1760 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
1762 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
1763 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
1765 char buf[ SLAP_TEXT_BUFLEN ];
1767 snprintf( buf, sizeof( buf ),
1768 "%s meta_send_entry(\"%s\"): "
1769 "slap_bv2undef_ad(%s): %s\n",
1770 op->o_log_prefix, ent.e_name.bv_val,
1771 mapped.bv_val, text );
1773 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1779 /* no subschemaSubentry */
1780 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
1781 || attr->a_desc == slap_schema.si_ad_entryDN )
1785 * We eat target's subschemaSubentry because
1786 * a search for this value is likely not
1787 * to resolve to the appropriate backend;
1788 * later, the local subschemaSubentry is
1791 * We also eat entryDN because the frontend
1792 * will reattach it without checking if already
1795 ( void )ber_scanf( &ber, "x" /* [W] */ );
1801 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
1802 || attr->a_vals == NULL )
1804 attr->a_vals = (struct berval *)&slap_dummy_bv;
1807 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
1811 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
1812 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
1814 if ( !validate && !pretty ) {
1819 if ( attr->a_desc == slap_schema.si_ad_objectClass
1820 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
1824 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1825 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_oc,
1826 bv, &mapped, BACKLDAP_REMAP );
1827 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
1833 *bv = attr->a_vals[ last ];
1834 BER_BVZERO( &attr->a_vals[ last ] );
1837 } else if ( mapped.bv_val != bv->bv_val ) {
1839 ber_dupbv( bv, &mapped );
1843 * It is necessary to try to rewrite attributes with
1844 * dn syntax because they might be used in ACLs as
1845 * members of groups; since ACLs are applied to the
1846 * rewritten stuff, no dn-based subecj clause could
1847 * be used at the ldap backend side (see
1848 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1849 * The problem can be overcome by moving the dn-based
1850 * ACLs to the target directory server, and letting
1851 * everything pass thru the ldap backend.
1856 if ( attr->a_desc->ad_type->sat_syntax ==
1857 slap_schema.si_syn_distinguishedName )
1859 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
1861 } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
1862 ldap_back_referral_result_rewrite( &dc, attr->a_vals );
1866 for ( i = 0; i < last; i++ ) {
1871 rc = pretty( attr->a_desc->ad_type->sat_syntax,
1872 &attr->a_vals[i], &pval, NULL );
1875 rc = validate( attr->a_desc->ad_type->sat_syntax,
1880 LBER_FREE( attr->a_vals[i].bv_val );
1881 if ( --last == i ) {
1882 BER_BVZERO( &attr->a_vals[ i ] );
1885 attr->a_vals[i] = attr->a_vals[last];
1886 BER_BVZERO( &attr->a_vals[last] );
1892 LBER_FREE( attr->a_vals[i].bv_val );
1893 attr->a_vals[i] = pval;
1897 if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
1903 if ( last && attr->a_desc->ad_type->sat_equality &&
1904 attr->a_desc->ad_type->sat_equality->smr_normalize )
1908 attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
1909 for ( i = 0; i<last; i++ ) {
1910 attr->a_desc->ad_type->sat_equality->smr_normalize(
1911 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1912 attr->a_desc->ad_type->sat_syntax,
1913 attr->a_desc->ad_type->sat_equality,
1914 &attr->a_vals[i], &attr->a_nvals[i],
1917 BER_BVZERO( &attr->a_nvals[i] );
1920 attr->a_nvals = attr->a_vals;
1924 attrp = &attr->a_next;
1927 rs->sr_entry = &ent;
1928 rs->sr_attrs = op->ors_attrs;
1930 rs->sr_err = LDAP_SUCCESS;
1931 rc = send_search_entry( op, rs );
1933 case LDAP_UNAVAILABLE:
1937 rs->sr_entry = NULL;
1938 rs->sr_attrs = NULL;
1940 if ( !BER_BVISNULL( &ent.e_name ) ) {
1941 free( ent.e_name.bv_val );
1942 BER_BVZERO( &ent.e_name );
1944 if ( !BER_BVISNULL( &ent.e_nname ) ) {
1945 free( ent.e_nname.bv_val );
1946 BER_BVZERO( &ent.e_nname );
1948 entry_clean( &ent );