2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2007 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
27 #include <ac/socket.h>
28 #include <ac/string.h>
33 #include "../back-ldap/back-ldap.h"
34 #include "back-meta.h"
35 #undef ldap_debug /* silence a warning in ldap-int.h */
37 #include "../../../libraries/libldap/ldap-int.h"
39 /* IGNORE means that target does not (no longer) participate
41 * NOTREADY means the search on that target has not been initialized yet
43 #define META_MSGID_IGNORE (-1)
44 #define META_MSGID_NEED_BIND (-2)
54 typedef enum meta_search_candidate_t {
55 META_SEARCH_UNDEFINED = -2,
57 META_SEARCH_NOT_CANDIDATE,
58 META_SEARCH_CANDIDATE,
61 } meta_search_candidate_t;
64 * meta_search_dobind_init()
66 * initiates bind for a candidate target of a search.
68 static meta_search_candidate_t
69 meta_search_dobind_init(
74 SlapReply *candidates )
76 metaconn_t *mc = *mcp;
77 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
78 metatarget_t *mt = mi->mi_targets[ candidate ];
79 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
81 struct berval binddn = msc->msc_bound_ndn,
87 meta_search_candidate_t retcode;
89 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_search_dobind_init[%d]\n",
90 op->o_log_prefix, candidate, 0 );
93 * all the targets are already bound as pseudoroot
95 if ( mc->mc_authz_target == META_BOUND_ALL ) {
96 return META_SEARCH_CANDIDATE;
99 retcode = META_SEARCH_BINDING;
100 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
101 if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
102 /* already bound (or anonymous) */
105 char buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
108 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
112 snprintf( buf, sizeof( buf ), " mc=%p ld=%p%s DN=\"%s\"",
113 (void *)mc, (void *)msc->msc_ld,
114 bound ? " bound" : " anonymous",
115 bound == 0 ? "" : msc->msc_bound_ndn.bv_val );
116 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
117 op->o_log_prefix, candidate, buf );
118 #endif /* DEBUG_205 */
120 retcode = META_SEARCH_CANDIDATE;
122 } else if ( META_BACK_CONN_CREATING( msc ) || LDAP_BACK_CONN_BINDING( msc ) ) {
123 /* another thread is binding the target for this conn; wait */
126 char buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
128 snprintf( buf, sizeof( buf ), " mc=%p ld=%p needbind",
129 (void *)mc, (void *)msc->msc_ld );
130 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
131 op->o_log_prefix, candidate, buf );
132 #endif /* DEBUG_205 */
134 candidates[ candidate ].sr_msgid = META_MSGID_NEED_BIND;
135 retcode = META_SEARCH_NEED_BIND;
138 /* we'll need to bind the target for this conn */
141 char buf[ SLAP_TEXT_BUFLEN ];
143 snprintf( buf, sizeof( buf ), " mc=%p ld=%p binding",
144 (void *)mc, (void *)msc->msc_ld );
145 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
146 op->o_log_prefix, candidate, buf );
147 #endif /* DEBUG_205 */
149 if ( msc->msc_ld == NULL ) {
150 /* for some reason (e.g. because formerly in "binding"
151 * state, with eventual connection expiration or invalidation)
152 * it was not initialized as expected */
154 Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p ld=NULL\n",
155 op->o_log_prefix, candidate, (void *)mc );
157 rc = meta_back_init_one_conn( op, rs, *mcp, candidate,
158 LDAP_BACK_CONN_ISPRIV( *mcp ), LDAP_BACK_DONTSEND, 0 );
161 assert( msc->msc_ld != NULL );
164 case LDAP_SERVER_DOWN:
165 case LDAP_UNAVAILABLE:
166 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
170 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
175 LDAP_BACK_CONN_BINDING_SET( msc );
178 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
180 if ( retcode != META_SEARCH_BINDING ) {
184 /* NOTE: this obsoletes pseudorootdn */
185 if ( op->o_conn != NULL &&
186 !op->o_do_not_cache &&
187 ( BER_BVISNULL( &msc->msc_bound_ndn ) ||
188 BER_BVISEMPTY( &msc->msc_bound_ndn ) ||
189 ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
191 rc = meta_back_proxy_authz_cred( mc, candidate, op, rs, LDAP_BACK_DONTSEND, &binddn, &cred, &method );
192 if ( rc != LDAP_SUCCESS ) {
196 /* NOTE: we copy things here, even if bind didn't succeed yet,
197 * because the connection is not shared until bind is over */
198 if ( !BER_BVISNULL( &binddn ) ) {
199 ber_bvreplace( &msc->msc_bound_ndn, &binddn );
200 if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &cred ) ) {
201 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
202 memset( msc->msc_cred.bv_val, 0,
203 msc->msc_cred.bv_len );
205 ber_bvreplace( &msc->msc_cred, &cred );
209 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
210 /* apparently, idassert was configured with SASL bind,
211 * so bind occurred inside meta_back_proxy_authz_cred() */
212 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
213 LDAP_BACK_CONN_BINDING_CLEAR( msc );
214 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
215 return META_SEARCH_CANDIDATE;
221 case LDAP_AUTH_SIMPLE:
222 /* do a simple bind with binddn, cred */
231 assert( msc->msc_ld != NULL );
234 rc = ldap_sasl_bind( msc->msc_ld, binddn.bv_val, LDAP_SASL_SIMPLE, &cred,
235 NULL, NULL, &candidates[ candidate ].sr_msgid );
239 char buf[ SLAP_TEXT_BUFLEN ];
241 snprintf( buf, sizeof( buf ), "meta_search_dobind_init[%d] mc=%p ld=%p rc=%d",
242 candidate, (void *)mc, (void *)mc->mc_conns[ candidate ].msc_ld, rc );
243 Debug( LDAP_DEBUG_ANY, "### %s %s\n",
244 op->o_log_prefix, buf, 0 );
246 #endif /* DEBUG_205 */
250 assert( candidates[ candidate ].sr_msgid >= 0 );
251 META_BINDING_SET( &candidates[ candidate ] );
252 return META_SEARCH_BINDING;
254 case LDAP_SERVER_DOWN:
256 /* This is the worst thing that could happen:
257 * the search will wait until the retry is over. */
258 if ( !META_IS_RETRYING( &candidates[ candidate ] ) ) {
259 META_RETRYING_SET( &candidates[ candidate ] );
261 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
263 assert( mc->mc_refcnt > 0 );
264 if ( StatslogTest( LDAP_DEBUG_ANY ) ) {
265 char buf[ SLAP_TEXT_BUFLEN ];
267 /* this lock is required; however,
268 * it's invoked only when logging is on */
269 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
270 snprintf( buf, sizeof( buf ),
271 "retrying URI=\"%s\" DN=\"%s\"",
273 BER_BVISNULL( &msc->msc_bound_ndn ) ?
274 "" : msc->msc_bound_ndn.bv_val );
275 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
277 Debug( LDAP_DEBUG_ANY,
278 "%s meta_search_dobind_init[%d]: %s.\n",
279 op->o_log_prefix, candidate, buf );
282 meta_clear_one_candidate( op, mc, candidate );
283 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
285 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
287 /* mc here must be the regular mc, reset and ready for init */
288 rc = meta_back_init_one_conn( op, rs, mc, candidate,
289 LDAP_BACK_CONN_ISPRIV( mc ), LDAP_BACK_DONTSEND, 0 );
291 if ( rc == LDAP_SUCCESS ) {
292 LDAP_BACK_CONN_BINDING_SET( msc );
295 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
297 if ( rc == LDAP_SUCCESS ) {
298 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
303 if ( *mcp == NULL ) {
304 retcode = META_SEARCH_ERR;
305 rs->sr_err = LDAP_UNAVAILABLE;
313 rc = slap_map_api2result( rs );
315 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
316 meta_clear_one_candidate( op, mc, candidate );
317 candidates[ candidate ].sr_err = rc;
318 if ( META_BACK_ONERR_STOP( mi ) ) {
319 LDAP_BACK_CONN_TAINTED_SET( mc );
320 meta_back_release_conn_lock( mi, mc, 0 );
324 retcode = META_SEARCH_ERR;
327 retcode = META_SEARCH_NOT_CANDIDATE;
329 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
330 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
337 static meta_search_candidate_t
338 meta_search_dobind_result(
343 SlapReply *candidates,
346 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
347 metaconn_t *mc = *mcp;
348 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
350 meta_search_candidate_t retcode = META_SEARCH_NOT_CANDIDATE;
353 assert( msc->msc_ld != NULL );
355 /* FIXME: matched? referrals? response controls? */
356 rc = ldap_parse_result( msc->msc_ld, res,
357 &candidates[ candidate ].sr_err,
358 NULL, NULL, NULL, NULL, 0 );
359 if ( rc != LDAP_SUCCESS ) {
360 candidates[ candidate ].sr_err = rc;
363 rc = slap_map_api2result( &candidates[ candidate ] );
366 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
367 LDAP_BACK_CONN_BINDING_CLEAR( msc );
368 if ( rc != LDAP_SUCCESS ) {
369 meta_clear_one_candidate( op, mc, candidate );
370 candidates[ candidate ].sr_err = rc;
371 if ( META_BACK_ONERR_STOP( mi ) ) {
372 LDAP_BACK_CONN_TAINTED_SET( mc );
373 meta_back_release_conn_lock( mi, mc, 0 );
375 retcode = META_SEARCH_ERR;
380 /* FIXME: check if bound as idassert authcDN! */
381 if ( BER_BVISNULL( &msc->msc_bound_ndn )
382 || BER_BVISEMPTY( &msc->msc_bound_ndn ) )
384 LDAP_BACK_CONN_ISANON_SET( msc );
387 LDAP_BACK_CONN_ISBOUND_SET( msc );
389 retcode = META_SEARCH_CANDIDATE;
392 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
393 META_BINDING_CLEAR( &candidates[ candidate ] );
395 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
400 static meta_search_candidate_t
401 meta_back_search_start(
407 SlapReply *candidates )
409 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
410 metatarget_t *mt = mi->mi_targets[ candidate ];
411 metasingleconn_t *msc = &(*mcp)->mc_conns[ candidate ];
412 struct berval realbase = op->o_req_dn;
413 int realscope = op->ors_scope;
414 struct berval mbase = BER_BVNULL;
415 struct berval mfilter = BER_BVNULL;
416 char **mapped_attrs = NULL;
418 meta_search_candidate_t retcode;
419 struct timeval tv, *tvp = NULL;
421 LDAPControl **ctrls = NULL;
423 /* this should not happen; just in case... */
424 if ( msc->msc_ld == NULL ) {
425 Debug( LDAP_DEBUG_ANY,
426 "%s: meta_back_search_start candidate=%d ld=NULL%s.\n",
427 op->o_log_prefix, candidate,
428 META_BACK_ONERR_STOP( mi ) ? "" : " (ignored)" );
429 candidates[ candidate ].sr_err = LDAP_OTHER;
430 if ( META_BACK_ONERR_STOP( mi ) ) {
431 return META_SEARCH_ERR;
433 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
434 return META_SEARCH_NOT_CANDIDATE;
437 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_back_search_start[%d]\n", op->o_log_prefix, candidate, 0 );
440 * modifies the base according to the scope, if required
442 if ( mt->mt_nsuffix.bv_len > op->o_req_ndn.bv_len ) {
443 switch ( op->ors_scope ) {
444 case LDAP_SCOPE_SUBTREE:
446 * make the target suffix the new base
447 * FIXME: this is very forgiving, because
448 * "illegal" searchBases may be turned
449 * into the suffix of the target; however,
450 * the requested searchBase already passed
451 * thru the candidate analyzer...
453 if ( dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) ) {
454 realbase = mt->mt_nsuffix;
455 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
456 realscope = LDAP_SCOPE_SUBORDINATE;
461 * this target is no longer candidate
463 retcode = META_SEARCH_NOT_CANDIDATE;
468 case LDAP_SCOPE_SUBORDINATE:
469 case LDAP_SCOPE_ONELEVEL:
471 struct berval rdn = mt->mt_nsuffix;
472 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
473 if ( dnIsOneLevelRDN( &rdn )
474 && dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) )
477 * if there is exactly one level,
478 * make the target suffix the new
479 * base, and make scope "base"
481 realbase = mt->mt_nsuffix;
482 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
483 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
484 realscope = LDAP_SCOPE_SUBORDINATE;
486 realscope = LDAP_SCOPE_SUBTREE;
489 realscope = LDAP_SCOPE_BASE;
492 } /* else continue with the next case */
495 case LDAP_SCOPE_BASE:
497 * this target is no longer candidate
499 retcode = META_SEARCH_NOT_CANDIDATE;
504 /* initiate dobind */
505 retcode = meta_search_dobind_init( op, rs, mcp, candidate, candidates );
507 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%d]=%d\n", op->o_log_prefix, candidate, retcode );
509 if ( retcode != META_SEARCH_CANDIDATE ) {
514 * Rewrite the search base, if required
517 dc->ctx = "searchBase";
518 switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
522 case LDAP_UNWILLING_TO_PERFORM:
523 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
524 rs->sr_text = "Operation not allowed";
525 send_ldap_result( op, rs );
526 retcode = META_SEARCH_ERR;
532 * this target is no longer candidate
534 retcode = META_SEARCH_NOT_CANDIDATE;
541 rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
542 &mfilter, BACKLDAP_MAP );
547 case LDAP_COMPARE_FALSE:
550 * this target is no longer candidate
552 retcode = META_SEARCH_NOT_CANDIDATE;
557 * Maps required attributes
559 rc = ldap_back_map_attrs( &mt->mt_rwmap.rwm_at,
560 op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
561 if ( rc != LDAP_SUCCESS ) {
563 * this target is no longer candidate
565 retcode = META_SEARCH_NOT_CANDIDATE;
569 /* should we check return values? */
570 if ( op->ors_deref != -1 ) {
571 assert( msc->msc_ld != NULL );
572 (void)ldap_set_option( msc->msc_ld, LDAP_OPT_DEREF,
573 ( void * )&op->ors_deref );
576 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
577 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
584 if ( ldap_back_proxy_authz_ctrl( &msc->msc_bound_ndn,
585 mt->mt_version, &mt->mt_idassert, op, rs, &ctrls )
588 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
589 retcode = META_SEARCH_NOT_CANDIDATE;
596 assert( msc->msc_ld != NULL );
597 rc = ldap_search_ext( msc->msc_ld,
598 mbase.bv_val, realscope, mfilter.bv_val,
599 mapped_attrs, op->ors_attrsonly,
600 ctrls, NULL, tvp, op->ors_slimit,
601 &candidates[ candidate ].sr_msgid );
604 retcode = META_SEARCH_CANDIDATE;
607 case LDAP_SERVER_DOWN:
608 if ( nretries && meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
610 /* if the identity changed, there might be need to re-authz */
611 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
615 if ( *mcp == NULL ) {
616 retcode = META_SEARCH_ERR;
622 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
623 retcode = META_SEARCH_NOT_CANDIDATE;
627 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
629 if ( mapped_attrs ) {
630 free( mapped_attrs );
632 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
633 free( mfilter.bv_val );
635 if ( mbase.bv_val != realbase.bv_val ) {
636 free( mbase.bv_val );
640 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_back_search_start[%d]=%d\n", op->o_log_prefix, candidate, retcode );
646 meta_back_search( Operation *op, SlapReply *rs )
648 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
650 struct timeval save_tv = { 0, 0 },
652 time_t stoptime = (time_t)(-1),
653 lastres_time = slap_get_time(),
655 int rc = 0, sres = LDAP_SUCCESS;
656 char *matched = NULL;
657 int last = 0, ncandidates = 0,
658 initial_candidates = 0, candidate_match = 0,
660 ldap_back_send_t sendok = LDAP_BACK_SENDERR;
665 SlapReply *candidates = meta_back_candidates_get( op );
668 * controls are set in ldap_back_dobind()
670 * FIXME: in case of values return filter, we might want
671 * to map attrs and maybe rewrite value
674 mc = meta_back_getconn( op, rs, NULL, sendok );
679 dc.conn = op->o_conn;
685 for ( i = 0; i < mi->mi_ntargets; i++ ) {
686 /* reset sr_msgid; it is used in most loops
687 * to check if that target is still to be considered */
688 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
690 /* a target is marked as candidate by meta_back_getconn();
691 * if for any reason (an error, it's over or so) it is
692 * no longer active, sr_msgid is set to META_MSGID_IGNORE
693 * but it remains candidate, which means it has been active
694 * at some point during the operation. This allows to
695 * use its response code and more to compute the final
697 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
701 candidates[ i ].sr_matched = NULL;
702 candidates[ i ].sr_text = NULL;
703 candidates[ i ].sr_ref = NULL;
704 candidates[ i ].sr_ctrls = NULL;
706 /* get largest timeout among candidates */
707 if ( mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ]
708 && mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ] > timeout )
710 timeout = mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ];
714 for ( i = 0; i < mi->mi_ntargets; i++ ) {
715 if ( !META_IS_CANDIDATE( &candidates[ i ] )
716 || candidates[ i ].sr_err != LDAP_SUCCESS )
721 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
723 case META_SEARCH_NOT_CANDIDATE:
724 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
727 case META_SEARCH_NEED_BIND:
731 case META_SEARCH_CANDIDATE:
732 case META_SEARCH_BINDING:
733 candidates[ i ].sr_type = REP_INTERMEDIATE;
737 case META_SEARCH_ERR:
738 savepriv = op->o_private;
739 op->o_private = (void *)i;
740 send_ldap_result( op, rs );
741 op->o_private = savepriv;
751 if ( ncandidates > 0 && needbind == ncandidates ) {
753 * give up the second time...
755 * NOTE: this should not occur the second time, since a fresh
756 * connection has ben created; however, targets may also
757 * need bind because the bind timed out or so.
759 if ( sendok & LDAP_BACK_BINDING ) {
760 Debug( LDAP_DEBUG_ANY,
761 "%s meta_back_search: unable to initialize conn\n",
762 op->o_log_prefix, 0, 0 );
763 rs->sr_err = LDAP_UNAVAILABLE;
764 rs->sr_text = "unable to initialize connection to remote targets";
765 send_ldap_result( op, rs );
770 /* FIXME: better create a separate connection? */
771 sendok |= LDAP_BACK_BINDING;
774 Debug( LDAP_DEBUG_ANY, "*** %s drop mc=%p create new connection\n",
775 op->o_log_prefix, (void *)mc, 0 );
776 #endif /* DEBUG_205 */
778 meta_back_release_conn( mi, mc );
787 initial_candidates = ncandidates;
789 if ( StatslogTest( LDAP_DEBUG_TRACE ) ) {
790 char cnd[ SLAP_TEXT_BUFLEN ];
793 for ( c = 0; c < mi->mi_ntargets; c++ ) {
794 if ( META_IS_CANDIDATE( &candidates[ c ] ) ) {
802 Debug( LDAP_DEBUG_TRACE, "%s meta_back_search: ncandidates=%d "
803 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
806 if ( initial_candidates == 0 ) {
807 /* NOTE: here we are not sending any matchedDN;
808 * this is intended, because if the back-meta
809 * is serving this search request, but no valid
810 * candidate could be looked up, it means that
811 * there is a hole in the mapping of the targets
812 * and thus no knowledge of any remote superior
814 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
815 "base=\"%s\" scope=%d: "
816 "no candidate could be selected\n",
817 op->o_log_prefix, op->o_req_dn.bv_val,
820 /* FIXME: we're sending the first error we encounter;
821 * maybe we should pick the worst... */
822 rc = LDAP_NO_SUCH_OBJECT;
823 for ( i = 0; i < mi->mi_ntargets; i++ ) {
824 if ( META_IS_CANDIDATE( &candidates[ i ] )
825 && candidates[ i ].sr_err != LDAP_SUCCESS )
827 rc = candidates[ i ].sr_err;
832 send_ldap_error( op, rs, rc, NULL );
837 /* We pull apart the ber result, stuff it into a slapd entry, and
838 * let send_search_entry stuff it back into ber format. Slow & ugly,
839 * but this is necessary for version matching, and for ACL processing.
842 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
843 stoptime = op->o_time + op->ors_tlimit;
847 * In case there are no candidates, no cycle takes place...
849 * FIXME: we might use a queue, to better balance the load
850 * among the candidates
852 for ( rc = 0; ncandidates > 0; ) {
855 alreadybound = ncandidates;
856 time_t curr_time = 0;
859 if ( timeout && lastres_time > 0
860 && ( slap_get_time() - lastres_time ) > timeout )
863 rs->sr_text = "Operation timed out";
864 rc = rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
865 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
866 savepriv = op->o_private;
867 op->o_private = (void *)i;
868 send_ldap_result( op, rs );
869 op->o_private = savepriv;
873 /* check time limit */
874 if ( op->ors_tlimit != SLAP_NO_LIMIT
875 && slap_get_time() > stoptime )
878 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
879 savepriv = op->o_private;
880 op->o_private = (void *)i;
881 send_ldap_result( op, rs );
882 op->o_private = savepriv;
886 for ( i = 0; i < mi->mi_ntargets; i++ ) {
887 meta_search_candidate_t retcode = META_SEARCH_UNDEFINED;
888 metasingleconn_t *msc = &mc->mc_conns[ i ];
889 LDAPMessage *res = NULL, *msg;
891 /* if msgid is invalid, don't ldap_result() */
892 if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
896 /* if target still needs bind, retry */
897 if ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND ) {
898 /* initiate dobind */
899 retcode = meta_search_dobind_init( op, rs, &mc, i, candidates );
901 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%ld]=%d\n",
902 op->o_log_prefix, i, retcode );
905 case META_SEARCH_NEED_BIND:
909 case META_SEARCH_BINDING:
912 case META_SEARCH_ERR:
913 candidates[ i ].sr_err = rs->sr_err;
914 if ( META_BACK_ONERR_STOP( mi ) ) {
915 savepriv = op->o_private;
916 op->o_private = (void *)i;
917 send_ldap_result( op, rs );
918 op->o_private = savepriv;
923 case META_SEARCH_NOT_CANDIDATE:
925 * When no candidates are left,
926 * the outer cycle finishes
928 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
929 assert( ncandidates > 0 );
933 case META_SEARCH_CANDIDATE:
934 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
935 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
937 case META_SEARCH_CANDIDATE:
938 assert( candidates[ i ].sr_msgid >= 0 );
941 case META_SEARCH_ERR:
942 candidates[ i ].sr_err = rs->sr_err;
943 if ( META_BACK_ONERR_STOP( mi ) ) {
944 savepriv = op->o_private;
945 op->o_private = (void *)i;
946 send_ldap_result( op, rs );
947 op->o_private = savepriv;
952 case META_SEARCH_NOT_CANDIDATE:
953 /* means that meta_back_search_start()
954 * failed but onerr == continue */
955 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
956 assert( ncandidates > 0 );
975 /* check for abandon */
976 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
981 if ( msc->msc_ld == NULL ) {
982 char buf[ SLAP_TEXT_BUFLEN ];
984 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
985 snprintf( buf, sizeof( buf ),
986 "%s meta_back_search[%ld] mc=%p msgid=%d%s%s%s\n",
987 op->o_log_prefix, (long)i, (void *)mc,
988 candidates[ i ].sr_msgid,
989 META_IS_BINDING( &candidates[ i ] ) ? " binding" : "",
990 LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ? " connbinding" : "",
991 META_BACK_CONN_CREATING( &mc->mc_conns[ i ] ) ? " conncreating" : "" );
992 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
994 Debug( LDAP_DEBUG_ANY, "!!! %s\n", buf, 0, 0 );
996 #endif /* DEBUG_205 */
999 * FIXME: handle time limit as well?
1000 * Note that target servers are likely
1001 * to handle it, so at some time we'll
1002 * get a LDAP_TIMELIMIT_EXCEEDED from
1006 rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
1007 LDAP_MSG_RECEIVED, &tv, &res );
1010 /* FIXME: res should not need to be freed */
1011 assert( res == NULL );
1016 /* something REALLY bad happened! */
1017 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1018 candidates[ i ].sr_type = REP_RESULT;
1020 if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
1021 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1022 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
1024 /* means that failed but onerr == continue */
1025 case META_SEARCH_NOT_CANDIDATE:
1026 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1028 assert( ncandidates > 0 );
1031 candidates[ i ].sr_err = rs->sr_err;
1032 if ( META_BACK_ONERR_STOP( mi ) ) {
1033 savepriv = op->o_private;
1034 op->o_private = (void *)i;
1035 send_ldap_result( op, rs );
1036 op->o_private = savepriv;
1041 case META_SEARCH_CANDIDATE:
1042 /* get back into business... */
1045 case META_SEARCH_BINDING:
1046 case META_SEARCH_NEED_BIND:
1047 case META_SEARCH_UNDEFINED:
1051 /* unrecoverable error */
1052 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1053 rc = rs->sr_err = LDAP_OTHER;
1058 candidates[ i ].sr_err = rs->sr_err;
1059 if ( META_BACK_ONERR_STOP( mi ) ) {
1060 savepriv = op->o_private;
1061 op->o_private = (void *)i;
1062 send_ldap_result( op, rs );
1063 op->o_private = savepriv;
1069 * When no candidates are left,
1070 * the outer cycle finishes
1072 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1073 assert( ncandidates > 0 );
1075 rs->sr_err = candidates[ i ].sr_err;
1079 lastres_time = slap_get_time();
1081 /* only touch when activity actually took place... */
1082 if ( mi->mi_idle_timeout != 0 && msc->msc_time < lastres_time ) {
1083 msc->msc_time = lastres_time;
1088 for ( msg = ldap_first_message( msc->msc_ld, res );
1090 msg = ldap_next_message( msc->msc_ld, msg ) )
1092 rc = ldap_msgtype( msg );
1093 if ( rc == LDAP_RES_SEARCH_ENTRY ) {
1096 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1097 /* don't retry any more... */
1098 candidates[ i ].sr_type = REP_RESULT;
1103 e = ldap_first_entry( msc->msc_ld, msg );
1104 savepriv = op->o_private;
1105 op->o_private = (void *)i;
1106 rs->sr_err = meta_send_entry( op, rs, mc, i, e );
1108 switch ( rs->sr_err ) {
1109 case LDAP_SIZELIMIT_EXCEEDED:
1110 savepriv = op->o_private;
1111 op->o_private = (void *)i;
1112 send_ldap_result( op, rs );
1113 op->o_private = savepriv;
1114 rs->sr_err = LDAP_SUCCESS;
1115 ldap_msgfree( res );
1119 case LDAP_UNAVAILABLE:
1120 rs->sr_err = LDAP_OTHER;
1121 ldap_msgfree( res );
1125 op->o_private = savepriv;
1127 /* don't wait any longer... */
1130 save_tv.tv_usec = 0;
1132 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
1133 char **references = NULL;
1136 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1137 /* don't retry any more... */
1138 candidates[ i ].sr_type = REP_RESULT;
1143 rc = ldap_parse_reference( msc->msc_ld, msg,
1144 &references, &rs->sr_ctrls, 0 );
1146 if ( rc != LDAP_SUCCESS ) {
1150 if ( references == NULL ) {
1154 #ifdef ENABLE_REWRITE
1155 dc.ctx = "referralDN";
1156 #else /* ! ENABLE_REWRITE */
1159 #endif /* ! ENABLE_REWRITE */
1161 /* FIXME: merge all and return at the end */
1163 for ( cnt = 0; references[ cnt ]; cnt++ )
1166 rs->sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
1168 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1169 ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
1171 BER_BVZERO( &rs->sr_ref[ cnt ] );
1173 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref );
1175 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
1176 /* ignore return value by now */
1177 savepriv = op->o_private;
1178 op->o_private = (void *)i;
1179 ( void )send_search_reference( op, rs );
1180 op->o_private = savepriv;
1182 ber_bvarray_free( rs->sr_ref );
1188 ber_memvfree( (void **)references );
1191 if ( rs->sr_ctrls ) {
1192 ldap_controls_free( rs->sr_ctrls );
1193 rs->sr_ctrls = NULL;
1196 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
1197 char buf[ SLAP_TEXT_BUFLEN ];
1198 char **references = NULL;
1200 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1201 /* don't retry any more... */
1202 candidates[ i ].sr_type = REP_RESULT;
1205 /* NOTE: ignores response controls
1206 * (and intermediate response controls
1207 * as well, except for those with search
1208 * references); this may not be correct,
1209 * but if they're not ignored then
1210 * back-meta would need to merge them
1211 * consistently (think of pagedResults...)
1213 /* FIXME: response controls? */
1214 rs->sr_err = ldap_parse_result( msc->msc_ld,
1216 &candidates[ i ].sr_err,
1217 (char **)&candidates[ i ].sr_matched,
1218 NULL /* (char **)&candidates[ i ].sr_text */ ,
1220 NULL /* &candidates[ i ].sr_ctrls (unused) */ ,
1222 if ( rs->sr_err != LDAP_SUCCESS ) {
1223 ldap_get_option( msc->msc_ld,
1224 LDAP_OPT_ERROR_NUMBER,
1226 sres = slap_map_api2result( rs );
1227 candidates[ i ].sr_type = REP_RESULT;
1228 ldap_msgfree( res );
1233 /* massage matchedDN if need be */
1234 if ( candidates[ i ].sr_matched != NULL ) {
1235 struct berval match, mmatch;
1237 ber_str2bv( candidates[ i ].sr_matched,
1239 candidates[ i ].sr_matched = NULL;
1241 dc.ctx = "matchedDN";
1242 dc.target = mi->mi_targets[ i ];
1243 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
1244 if ( mmatch.bv_val == match.bv_val ) {
1245 candidates[ i ].sr_matched
1246 = ch_strdup( mmatch.bv_val );
1249 candidates[ i ].sr_matched = mmatch.bv_val;
1254 ldap_memfree( match.bv_val );
1257 /* add references to array */
1262 for ( cnt = 0; references[ cnt ]; cnt++ )
1265 sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
1267 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1268 ber_str2bv( references[ cnt ], 0, 1, &sr_ref[ cnt ] );
1270 BER_BVZERO( &sr_ref[ cnt ] );
1272 ( void )ldap_back_referral_result_rewrite( &dc, sr_ref );
1275 ber_memvfree( (void **)references );
1277 if ( rs->sr_v2ref == NULL ) {
1278 rs->sr_v2ref = sr_ref;
1281 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
1282 ber_bvarray_add( &rs->sr_v2ref, &sr_ref[ cnt ] );
1284 ber_memfree( sr_ref );
1288 rs->sr_err = candidates[ i ].sr_err;
1289 sres = slap_map_api2result( rs );
1291 if ( StatslogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
1292 snprintf( buf, sizeof( buf ),
1293 "%s meta_back_search[%ld] "
1294 "match=\"%s\" err=%ld",
1295 op->o_log_prefix, i,
1296 candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
1297 (long) candidates[ i ].sr_err );
1298 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1299 Debug( LDAP_DEBUG_TRACE, "%s.\n", buf, 0, 0 );
1302 Debug( LDAP_DEBUG_ANY, "%s (%s).\n",
1303 buf, ldap_err2string( candidates[ i ].sr_err ), 0 );
1308 case LDAP_NO_SUCH_OBJECT:
1309 /* is_ok is touched any time a valid
1310 * (even intermediate) result is
1311 * returned; as a consequence, if
1312 * a candidate returns noSuchObject
1313 * it is ignored and the candidate
1314 * is simply demoted. */
1316 sres = LDAP_SUCCESS;
1325 case LDAP_SIZELIMIT_EXCEEDED:
1326 /* if a target returned sizelimitExceeded
1327 * and the entry count is equal to the
1328 * proxy's limit, the target would have
1329 * returned more, and the error must be
1330 * propagated to the client; otherwise,
1331 * the target enforced a limit lower
1332 * than what requested by the proxy;
1334 candidates[ i ].sr_err = rs->sr_err;
1335 if ( rs->sr_nentries == op->ors_slimit
1336 || META_BACK_ONERR_STOP( mi ) )
1338 savepriv = op->o_private;
1339 op->o_private = (void *)i;
1340 send_ldap_result( op, rs );
1341 op->o_private = savepriv;
1342 ldap_msgfree( res );
1349 candidates[ i ].sr_err = rs->sr_err;
1350 if ( META_BACK_ONERR_STOP( mi ) ) {
1351 savepriv = op->o_private;
1352 op->o_private = (void *)i;
1353 send_ldap_result( op, rs );
1354 op->o_private = savepriv;
1355 ldap_msgfree( res );
1366 * When no candidates are left,
1367 * the outer cycle finishes
1369 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1370 assert( ncandidates > 0 );
1373 } else if ( rc == LDAP_RES_BIND ) {
1374 meta_search_candidate_t retcode;
1376 retcode = meta_search_dobind_result( op, rs, &mc, i, candidates, msg );
1377 if ( retcode == META_SEARCH_CANDIDATE ) {
1378 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1379 retcode = meta_back_search_start( op, rs, &dc, &mc, i, candidates );
1382 switch ( retcode ) {
1383 case META_SEARCH_CANDIDATE:
1386 /* means that failed but onerr == continue */
1387 case META_SEARCH_NOT_CANDIDATE:
1388 case META_SEARCH_ERR:
1389 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1390 assert( ncandidates > 0 );
1393 candidates[ i ].sr_err = rs->sr_err;
1394 if ( META_BACK_ONERR_STOP( mi ) ) {
1395 savepriv = op->o_private;
1396 op->o_private = (void *)i;
1397 send_ldap_result( op, rs );
1398 op->o_private = savepriv;
1399 ldap_msgfree( res );
1412 ldap_msgfree( res );
1419 ldap_msgfree( res );
1423 /* check for abandon */
1424 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
1425 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1426 if ( candidates[ i ].sr_msgid >= 0 ) {
1427 if ( META_IS_BINDING( &candidates[ i ] ) ) {
1428 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1429 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ) {
1430 /* if still binding, destroy */
1433 char buf[ SLAP_TEXT_BUFLEN ];
1435 snprintf( buf, sizeof( buf), "%s meta_back_search(abandon) "
1436 "ldap_unbind_ext[%ld] mc=%p ld=%p",
1437 op->o_log_prefix, i, (void *)mc,
1438 (void *)mc->mc_conns[i].msc_ld );
1440 Debug( LDAP_DEBUG_ANY, "### %s\n", buf, 0, 0 );
1441 #endif /* DEBUG_205 */
1443 meta_clear_one_candidate( op, mc, i );
1445 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1446 META_BINDING_CLEAR( &candidates[ i ] );
1449 (void)meta_back_cancel( mc, op, rs,
1450 candidates[ i ].sr_msgid, i,
1451 LDAP_BACK_DONTSEND );
1454 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1455 assert( ncandidates > 0 );
1460 if ( op->o_abandon ) {
1464 /* let send_ldap_result play cleanup handlers (ITS#4645) */
1468 /* if no entry was found during this loop,
1469 * set a minimal timeout */
1470 if ( ncandidates > 0 && gotit == 0 ) {
1471 if ( save_tv.tv_sec == 0 && save_tv.tv_usec == 0 ) {
1472 save_tv.tv_usec = LDAP_BACK_RESULT_UTIMEOUT/initial_candidates;
1474 /* arbitrarily limit to something between 1 and 2 minutes */
1475 } else if ( ( stoptime == -1 && save_tv.tv_sec < 60 )
1476 || save_tv.tv_sec < ( stoptime - slap_get_time() ) / ( 2 * ncandidates ) )
1478 /* double the timeout */
1479 lutil_timermul( &save_tv, 2, &save_tv );
1482 if ( alreadybound == 0 ) {
1484 (void)select( 0, NULL, NULL, NULL, &tv );
1487 ldap_pvt_thread_yield();
1494 * FIXME: need a better strategy to handle errors
1497 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE,
1498 -1, stoptime != -1 ? (stoptime - slap_get_time()) : 0,
1499 LDAP_BACK_SENDERR );
1507 * Rewrite the matched portion of the search base, if required
1509 * FIXME: only the last one gets caught!
1511 savepriv = op->o_private;
1512 op->o_private = (void *)(long)mi->mi_ntargets;
1513 if ( candidate_match > 0 ) {
1514 struct berval pmatched = BER_BVNULL;
1516 /* we use the first one */
1517 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1518 if ( META_IS_CANDIDATE( &candidates[ i ] )
1519 && candidates[ i ].sr_matched != NULL )
1521 struct berval bv, pbv;
1524 /* if we got success, and this target
1525 * returned noSuchObject, and its suffix
1526 * is a superior of the searchBase,
1527 * ignore the matchedDN */
1528 if ( sres == LDAP_SUCCESS
1529 && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
1530 && op->o_req_ndn.bv_len > mi->mi_targets[ i ]->mt_nsuffix.bv_len )
1532 free( (char *)candidates[ i ].sr_matched );
1533 candidates[ i ].sr_matched = NULL;
1537 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
1538 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
1540 if ( rc == LDAP_SUCCESS ) {
1542 /* NOTE: if they all are superiors
1543 * of the baseDN, the shorter is also
1544 * superior of the longer... */
1545 if ( pbv.bv_len > pmatched.bv_len ) {
1546 if ( !BER_BVISNULL( &pmatched ) ) {
1547 op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
1550 op->o_private = (void *)i;
1553 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
1557 if ( candidates[ i ].sr_matched != NULL ) {
1558 free( (char *)candidates[ i ].sr_matched );
1559 candidates[ i ].sr_matched = NULL;
1564 if ( !BER_BVISNULL( &pmatched ) ) {
1565 matched = pmatched.bv_val;
1568 } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
1569 matched = op->o_bd->be_suffix[ 0 ].bv_val;
1573 * In case we returned at least one entry, we return LDAP_SUCCESS
1574 * otherwise, the latter error code we got
1577 if ( sres == LDAP_SUCCESS ) {
1578 if ( rs->sr_v2ref ) {
1579 sres = LDAP_REFERRAL;
1582 if ( META_BACK_ONERR_REPORT( mi ) ) {
1584 * Report errors, if any
1586 * FIXME: we should handle error codes and return the more
1587 * important/reasonable
1589 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1590 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1594 if ( candidates[ i ].sr_err != LDAP_SUCCESS
1595 && candidates[ i ].sr_err != LDAP_NO_SUCH_OBJECT )
1597 sres = candidates[ i ].sr_err;
1605 rs->sr_matched = matched;
1606 rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
1607 send_ldap_result( op, rs );
1608 op->o_private = savepriv;
1609 rs->sr_matched = NULL;
1613 if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
1614 op->o_tmpfree( matched, op->o_tmpmemctx );
1617 if ( rs->sr_v2ref ) {
1618 ber_bvarray_free( rs->sr_v2ref );
1621 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1622 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1626 if ( mc && META_IS_BINDING( &candidates[ i ] ) ) {
1627 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1628 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ) {
1629 assert( candidates[ i ].sr_msgid >= 0 );
1630 assert( mc->mc_conns[ i ].msc_ld != NULL );
1633 Debug( LDAP_DEBUG_ANY, "### %s meta_back_search(cleanup) "
1634 "ldap_unbind_ext[%ld] ld=%p\n",
1635 op->o_log_prefix, i, (void *)mc->mc_conns[i].msc_ld );
1636 #endif /* DEBUG_205 */
1638 /* if still binding, destroy */
1639 meta_clear_one_candidate( op, mc, i );
1641 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1642 META_BINDING_CLEAR( &candidates[ i ] );
1645 if ( candidates[ i ].sr_matched ) {
1646 free( (char *)candidates[ i ].sr_matched );
1647 candidates[ i ].sr_matched = NULL;
1650 if ( candidates[ i ].sr_text ) {
1651 ldap_memfree( (char *)candidates[ i ].sr_text );
1652 candidates[ i ].sr_text = NULL;
1655 if ( candidates[ i ].sr_ref ) {
1656 ber_bvarray_free( candidates[ i ].sr_ref );
1657 candidates[ i ].sr_ref = NULL;
1660 if ( candidates[ i ].sr_ctrls ) {
1661 ldap_controls_free( candidates[ i ].sr_ctrls );
1662 candidates[ i ].sr_ctrls = NULL;
1665 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1666 meta_back_quarantine( op, &candidates[ i ], i );
1669 /* only in case of timelimit exceeded, if the timelimit exceeded because
1670 * one contacted target never responded, invalidate the connection
1671 * NOTE: should we quarantine the target as well? right now, the connection
1672 * is invalidated; the next time it will be recreated and the target
1673 * will be quarantined if it cannot be contacted */
1674 if ( mi->mi_idle_timeout != 0
1675 && rs->sr_err == LDAP_TIMELIMIT_EXCEEDED
1676 && op->o_time > mc->mc_conns[ i ].msc_time )
1678 /* don't let anyone else use this expired connection */
1679 LDAP_BACK_CONN_TAINTED_SET( mc );
1684 meta_back_release_conn( mi, mc );
1698 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1699 struct berval a, mapped;
1701 BerElement ber = *e->lm_ber;
1702 Attribute *attr, **attrp;
1709 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
1710 return LDAP_DECODING_ERROR;
1714 * Rewrite the dn of the result, if needed
1716 dc.target = mi->mi_targets[ target ];
1717 dc.conn = op->o_conn;
1719 dc.ctx = "searchResult";
1721 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
1722 if ( rs->sr_err != LDAP_SUCCESS) {
1727 * Note: this may fail if the target host(s) schema differs
1728 * from the one known to the meta, and a DN with unknown
1729 * attributes is returned.
1731 * FIXME: should we log anything, or delegate to dnNormalize?
1733 rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
1735 if ( dn.bv_val != bdn.bv_val ) {
1740 if ( rc != LDAP_SUCCESS ) {
1741 return LDAP_INVALID_DN_SYNTAX;
1747 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1748 ( void )meta_dncache_update_entry( &mi->mi_cache,
1749 &ent.e_nname, target );
1752 attrp = &ent.e_attrs;
1754 dc.ctx = "searchAttrDN";
1755 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
1757 slap_syntax_validate_func *validate;
1758 slap_syntax_transform_func *pretty;
1760 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_at,
1761 &a, &mapped, BACKLDAP_REMAP );
1762 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
1763 ( void )ber_scanf( &ber, "x" /* [W] */ );
1766 attr = ( Attribute * )ch_calloc( 1, sizeof( Attribute ) );
1767 if ( attr == NULL ) {
1770 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
1772 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
1773 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
1775 char buf[ SLAP_TEXT_BUFLEN ];
1777 snprintf( buf, sizeof( buf ),
1778 "%s meta_send_entry(\"%s\"): "
1779 "slap_bv2undef_ad(%s): %s\n",
1780 op->o_log_prefix, ent.e_name.bv_val,
1781 mapped.bv_val, text );
1783 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1789 /* no subschemaSubentry */
1790 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
1791 || attr->a_desc == slap_schema.si_ad_entryDN )
1795 * We eat target's subschemaSubentry because
1796 * a search for this value is likely not
1797 * to resolve to the appropriate backend;
1798 * later, the local subschemaSubentry is
1801 * We also eat entryDN because the frontend
1802 * will reattach it without checking if already
1805 ( void )ber_scanf( &ber, "x" /* [W] */ );
1811 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
1812 || attr->a_vals == NULL )
1814 attr->a_vals = (struct berval *)&slap_dummy_bv;
1817 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
1821 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
1822 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
1824 if ( !validate && !pretty ) {
1829 if ( attr->a_desc == slap_schema.si_ad_objectClass
1830 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
1834 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1835 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_oc,
1836 bv, &mapped, BACKLDAP_REMAP );
1837 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
1843 *bv = attr->a_vals[ last ];
1844 BER_BVZERO( &attr->a_vals[ last ] );
1847 } else if ( mapped.bv_val != bv->bv_val ) {
1849 ber_dupbv( bv, &mapped );
1853 * It is necessary to try to rewrite attributes with
1854 * dn syntax because they might be used in ACLs as
1855 * members of groups; since ACLs are applied to the
1856 * rewritten stuff, no dn-based subecj clause could
1857 * be used at the ldap backend side (see
1858 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1859 * The problem can be overcome by moving the dn-based
1860 * ACLs to the target directory server, and letting
1861 * everything pass thru the ldap backend.
1866 if ( attr->a_desc->ad_type->sat_syntax ==
1867 slap_schema.si_syn_distinguishedName )
1869 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
1871 } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
1872 ldap_back_referral_result_rewrite( &dc, attr->a_vals );
1876 for ( i = 0; i < last; i++ ) {
1881 rc = pretty( attr->a_desc->ad_type->sat_syntax,
1882 &attr->a_vals[i], &pval, NULL );
1885 rc = validate( attr->a_desc->ad_type->sat_syntax,
1890 LBER_FREE( attr->a_vals[i].bv_val );
1891 if ( --last == i ) {
1892 BER_BVZERO( &attr->a_vals[ i ] );
1895 attr->a_vals[i] = attr->a_vals[last];
1896 BER_BVZERO( &attr->a_vals[last] );
1902 LBER_FREE( attr->a_vals[i].bv_val );
1903 attr->a_vals[i] = pval;
1907 if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
1913 if ( last && attr->a_desc->ad_type->sat_equality &&
1914 attr->a_desc->ad_type->sat_equality->smr_normalize )
1918 attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
1919 for ( i = 0; i<last; i++ ) {
1920 attr->a_desc->ad_type->sat_equality->smr_normalize(
1921 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1922 attr->a_desc->ad_type->sat_syntax,
1923 attr->a_desc->ad_type->sat_equality,
1924 &attr->a_vals[i], &attr->a_nvals[i],
1927 BER_BVZERO( &attr->a_nvals[i] );
1930 attr->a_nvals = attr->a_vals;
1934 attrp = &attr->a_next;
1937 rs->sr_entry = &ent;
1938 rs->sr_attrs = op->ors_attrs;
1940 rs->sr_err = LDAP_SUCCESS;
1941 rc = send_search_entry( op, rs );
1943 case LDAP_UNAVAILABLE:
1947 rs->sr_entry = NULL;
1948 rs->sr_attrs = NULL;
1950 if ( !BER_BVISNULL( &ent.e_name ) ) {
1951 free( ent.e_name.bv_val );
1952 BER_BVZERO( &ent.e_name );
1954 if ( !BER_BVISNULL( &ent.e_nname ) ) {
1955 free( ent.e_nname.bv_val );
1956 BER_BVZERO( &ent.e_nname );
1958 entry_clean( &ent );