2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2010 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 ( META_BACK_TGT_SAVECRED( mt ) && !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 only the first time... */
236 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;
319 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
327 rc = slap_map_api2result( rs );
329 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
330 meta_clear_one_candidate( op, mc, candidate );
331 candidates[ candidate ].sr_err = rc;
332 if ( META_BACK_ONERR_STOP( mi ) ) {
333 LDAP_BACK_CONN_TAINTED_SET( mc );
334 meta_back_release_conn_lock( mi, mc, 0 );
338 retcode = META_SEARCH_ERR;
341 retcode = META_SEARCH_NOT_CANDIDATE;
343 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
344 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
351 static meta_search_candidate_t
352 meta_search_dobind_result(
357 SlapReply *candidates,
360 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
361 metaconn_t *mc = *mcp;
362 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
364 meta_search_candidate_t retcode = META_SEARCH_NOT_CANDIDATE;
367 assert( msc->msc_ld != NULL );
369 /* FIXME: matched? referrals? response controls? */
370 rc = ldap_parse_result( msc->msc_ld, res,
371 &candidates[ candidate ].sr_err,
372 NULL, NULL, NULL, NULL, 0 );
373 if ( rc != LDAP_SUCCESS ) {
374 candidates[ candidate ].sr_err = rc;
377 rc = slap_map_api2result( &candidates[ candidate ] );
380 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
381 LDAP_BACK_CONN_BINDING_CLEAR( msc );
382 if ( rc != LDAP_SUCCESS ) {
383 meta_clear_one_candidate( op, mc, candidate );
384 candidates[ candidate ].sr_err = rc;
385 if ( META_BACK_ONERR_STOP( mi ) ) {
386 LDAP_BACK_CONN_TAINTED_SET( mc );
387 meta_back_release_conn_lock( mi, mc, 0 );
389 retcode = META_SEARCH_ERR;
394 /* FIXME: check if bound as idassert authcDN! */
395 if ( BER_BVISNULL( &msc->msc_bound_ndn )
396 || BER_BVISEMPTY( &msc->msc_bound_ndn ) )
398 LDAP_BACK_CONN_ISANON_SET( msc );
401 LDAP_BACK_CONN_ISBOUND_SET( msc );
403 retcode = META_SEARCH_CANDIDATE;
405 /* connect must be async */
406 ldap_set_option( msc->msc_ld, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_OFF );
409 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
410 META_BINDING_CLEAR( &candidates[ candidate ] );
412 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
417 static meta_search_candidate_t
418 meta_back_search_start(
424 SlapReply *candidates )
426 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
427 metatarget_t *mt = mi->mi_targets[ candidate ];
428 metasingleconn_t *msc = &(*mcp)->mc_conns[ candidate ];
429 struct berval realbase = op->o_req_dn;
430 int realscope = op->ors_scope;
431 struct berval mbase = BER_BVNULL;
432 struct berval mfilter = BER_BVNULL;
433 char **mapped_attrs = NULL;
435 meta_search_candidate_t retcode;
436 struct timeval tv, *tvp = NULL;
438 LDAPControl **ctrls = NULL;
440 /* this should not happen; just in case... */
441 if ( msc->msc_ld == NULL ) {
442 Debug( LDAP_DEBUG_ANY,
443 "%s: meta_back_search_start candidate=%d ld=NULL%s.\n",
444 op->o_log_prefix, candidate,
445 META_BACK_ONERR_STOP( mi ) ? "" : " (ignored)" );
446 candidates[ candidate ].sr_err = LDAP_OTHER;
447 if ( META_BACK_ONERR_STOP( mi ) ) {
448 return META_SEARCH_ERR;
450 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
451 return META_SEARCH_NOT_CANDIDATE;
454 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_back_search_start[%d]\n", op->o_log_prefix, candidate, 0 );
457 * modifies the base according to the scope, if required
459 if ( mt->mt_nsuffix.bv_len > op->o_req_ndn.bv_len ) {
460 switch ( op->ors_scope ) {
461 case LDAP_SCOPE_SUBTREE:
463 * make the target suffix the new base
464 * FIXME: this is very forgiving, because
465 * "illegal" searchBases may be turned
466 * into the suffix of the target; however,
467 * the requested searchBase already passed
468 * thru the candidate analyzer...
470 if ( dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) ) {
471 realbase = mt->mt_nsuffix;
472 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
473 realscope = LDAP_SCOPE_SUBORDINATE;
478 * this target is no longer candidate
480 retcode = META_SEARCH_NOT_CANDIDATE;
485 case LDAP_SCOPE_SUBORDINATE:
486 case LDAP_SCOPE_ONELEVEL:
488 struct berval rdn = mt->mt_nsuffix;
489 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
490 if ( dnIsOneLevelRDN( &rdn )
491 && dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) )
494 * if there is exactly one level,
495 * make the target suffix the new
496 * base, and make scope "base"
498 realbase = mt->mt_nsuffix;
499 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
500 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
501 realscope = LDAP_SCOPE_SUBORDINATE;
503 realscope = LDAP_SCOPE_SUBTREE;
506 realscope = LDAP_SCOPE_BASE;
509 } /* else continue with the next case */
512 case LDAP_SCOPE_BASE:
514 * this target is no longer candidate
516 retcode = META_SEARCH_NOT_CANDIDATE;
521 /* initiate dobind */
522 retcode = meta_search_dobind_init( op, rs, mcp, candidate, candidates );
524 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%d]=%d\n", op->o_log_prefix, candidate, retcode );
526 if ( retcode != META_SEARCH_CANDIDATE ) {
531 * Rewrite the search base, if required
534 dc->ctx = "searchBase";
535 switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
539 case LDAP_UNWILLING_TO_PERFORM:
540 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
541 rs->sr_text = "Operation not allowed";
542 send_ldap_result( op, rs );
543 retcode = META_SEARCH_ERR;
549 * this target is no longer candidate
551 retcode = META_SEARCH_NOT_CANDIDATE;
558 rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
559 &mfilter, BACKLDAP_MAP, op->o_tmpmemctx );
564 case LDAP_COMPARE_FALSE:
567 * this target is no longer candidate
569 retcode = META_SEARCH_NOT_CANDIDATE;
574 * Maps required attributes
576 rc = ldap_back_map_attrs( op, &mt->mt_rwmap.rwm_at,
577 op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
578 if ( rc != LDAP_SUCCESS ) {
580 * this target is no longer candidate
582 retcode = META_SEARCH_NOT_CANDIDATE;
586 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
587 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
594 if ( meta_back_controls_add( op, rs, *mcp, candidate, &ctrls )
597 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
598 retcode = META_SEARCH_NOT_CANDIDATE;
605 assert( msc->msc_ld != NULL );
606 rc = ldap_pvt_search( msc->msc_ld,
607 mbase.bv_val, realscope, mfilter.bv_val,
608 mapped_attrs, op->ors_attrsonly,
609 ctrls, NULL, tvp, op->ors_slimit, op->ors_deref,
610 &candidates[ candidate ].sr_msgid );
613 retcode = META_SEARCH_CANDIDATE;
616 case LDAP_SERVER_DOWN:
617 if ( nretries && meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
619 /* if the identity changed, there might be need to re-authz */
620 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
624 if ( *mcp == NULL ) {
625 retcode = META_SEARCH_ERR;
626 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
632 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
633 retcode = META_SEARCH_NOT_CANDIDATE;
637 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
639 if ( mapped_attrs ) {
640 ber_memfree_x( mapped_attrs, op->o_tmpmemctx );
642 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
643 ber_memfree_x( mfilter.bv_val, op->o_tmpmemctx );
645 if ( mbase.bv_val != realbase.bv_val ) {
646 free( mbase.bv_val );
650 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_back_search_start[%d]=%d\n", op->o_log_prefix, candidate, retcode );
656 meta_back_search( Operation *op, SlapReply *rs )
658 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
660 struct timeval save_tv = { 0, 0 },
662 time_t stoptime = (time_t)(-1),
663 lastres_time = slap_get_time(),
665 int rc = 0, sres = LDAP_SUCCESS;
666 char *matched = NULL;
667 int last = 0, ncandidates = 0,
668 initial_candidates = 0, candidate_match = 0,
670 ldap_back_send_t sendok = LDAP_BACK_SENDERR;
675 SlapReply *candidates = NULL;
678 * controls are set in ldap_back_dobind()
680 * FIXME: in case of values return filter, we might want
681 * to map attrs and maybe rewrite value
684 mc = meta_back_getconn( op, rs, NULL, sendok );
689 dc.conn = op->o_conn;
692 if ( candidates == NULL ) candidates = meta_back_candidates_get( op );
696 for ( i = 0; i < mi->mi_ntargets; i++ ) {
697 /* reset sr_msgid; it is used in most loops
698 * to check if that target is still to be considered */
699 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
701 /* a target is marked as candidate by meta_back_getconn();
702 * if for any reason (an error, it's over or so) it is
703 * no longer active, sr_msgid is set to META_MSGID_IGNORE
704 * but it remains candidate, which means it has been active
705 * at some point during the operation. This allows to
706 * use its response code and more to compute the final
708 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
712 candidates[ i ].sr_matched = NULL;
713 candidates[ i ].sr_text = NULL;
714 candidates[ i ].sr_ref = NULL;
715 candidates[ i ].sr_ctrls = NULL;
717 /* get largest timeout among candidates */
718 if ( mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ]
719 && mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ] > timeout )
721 timeout = mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ];
725 for ( i = 0; i < mi->mi_ntargets; i++ ) {
726 if ( !META_IS_CANDIDATE( &candidates[ i ] )
727 || candidates[ i ].sr_err != LDAP_SUCCESS )
732 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
734 case META_SEARCH_NOT_CANDIDATE:
735 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
738 case META_SEARCH_NEED_BIND:
742 case META_SEARCH_CONNECTING:
743 case META_SEARCH_CANDIDATE:
744 case META_SEARCH_BINDING:
745 candidates[ i ].sr_type = REP_INTERMEDIATE;
749 case META_SEARCH_ERR:
750 savepriv = op->o_private;
751 op->o_private = (void *)i;
752 send_ldap_result( op, rs );
753 op->o_private = savepriv;
763 if ( ncandidates > 0 && needbind == ncandidates ) {
765 * give up the second time...
767 * NOTE: this should not occur the second time, since a fresh
768 * connection has ben created; however, targets may also
769 * need bind because the bind timed out or so.
771 if ( sendok & LDAP_BACK_BINDING ) {
772 Debug( LDAP_DEBUG_ANY,
773 "%s meta_back_search: unable to initialize conn\n",
774 op->o_log_prefix, 0, 0 );
775 rs->sr_err = LDAP_UNAVAILABLE;
776 rs->sr_text = "unable to initialize connection to remote targets";
777 send_ldap_result( op, rs );
782 /* FIXME: better create a separate connection? */
783 sendok |= LDAP_BACK_BINDING;
786 Debug( LDAP_DEBUG_ANY, "*** %s drop mc=%p create new connection\n",
787 op->o_log_prefix, (void *)mc, 0 );
788 #endif /* DEBUG_205 */
790 meta_back_release_conn( mi, mc );
799 initial_candidates = ncandidates;
801 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
802 char cnd[ SLAP_TEXT_BUFLEN ];
805 for ( c = 0; c < mi->mi_ntargets; c++ ) {
806 if ( META_IS_CANDIDATE( &candidates[ c ] ) ) {
814 Debug( LDAP_DEBUG_TRACE, "%s meta_back_search: ncandidates=%d "
815 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
818 if ( initial_candidates == 0 ) {
819 /* NOTE: here we are not sending any matchedDN;
820 * this is intended, because if the back-meta
821 * is serving this search request, but no valid
822 * candidate could be looked up, it means that
823 * there is a hole in the mapping of the targets
824 * and thus no knowledge of any remote superior
826 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
827 "base=\"%s\" scope=%d: "
828 "no candidate could be selected\n",
829 op->o_log_prefix, op->o_req_dn.bv_val,
832 /* FIXME: we're sending the first error we encounter;
833 * maybe we should pick the worst... */
834 rc = LDAP_NO_SUCH_OBJECT;
835 for ( i = 0; i < mi->mi_ntargets; i++ ) {
836 if ( META_IS_CANDIDATE( &candidates[ i ] )
837 && candidates[ i ].sr_err != LDAP_SUCCESS )
839 rc = candidates[ i ].sr_err;
844 send_ldap_error( op, rs, rc, NULL );
849 /* We pull apart the ber result, stuff it into a slapd entry, and
850 * let send_search_entry stuff it back into ber format. Slow & ugly,
851 * but this is necessary for version matching, and for ACL processing.
854 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
855 stoptime = op->o_time + op->ors_tlimit;
859 * In case there are no candidates, no cycle takes place...
861 * FIXME: we might use a queue, to better balance the load
862 * among the candidates
864 for ( rc = 0; ncandidates > 0; ) {
867 alreadybound = ncandidates;
870 if ( timeout && lastres_time > 0
871 && ( slap_get_time() - lastres_time ) > timeout )
874 rs->sr_text = "Operation timed out";
875 rc = rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
876 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
877 savepriv = op->o_private;
878 op->o_private = (void *)i;
879 send_ldap_result( op, rs );
880 op->o_private = savepriv;
884 /* check time limit */
885 if ( op->ors_tlimit != SLAP_NO_LIMIT
886 && slap_get_time() > stoptime )
889 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
890 savepriv = op->o_private;
891 op->o_private = (void *)i;
892 send_ldap_result( op, rs );
893 op->o_private = savepriv;
897 for ( i = 0; i < mi->mi_ntargets; i++ ) {
898 meta_search_candidate_t retcode = META_SEARCH_UNDEFINED;
899 metasingleconn_t *msc = &mc->mc_conns[ i ];
900 LDAPMessage *res = NULL, *msg;
902 /* if msgid is invalid, don't ldap_result() */
903 if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
907 /* if target still needs bind, retry */
908 if ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND
909 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
911 /* initiate dobind */
912 retcode = meta_search_dobind_init( op, rs, &mc, i, candidates );
914 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%ld]=%d\n",
915 op->o_log_prefix, i, retcode );
918 case META_SEARCH_NEED_BIND:
922 case META_SEARCH_CONNECTING:
923 case META_SEARCH_BINDING:
926 case META_SEARCH_ERR:
927 candidates[ i ].sr_err = rs->sr_err;
928 if ( META_BACK_ONERR_STOP( mi ) ) {
929 savepriv = op->o_private;
930 op->o_private = (void *)i;
931 send_ldap_result( op, rs );
932 op->o_private = savepriv;
937 case META_SEARCH_NOT_CANDIDATE:
939 * When no candidates are left,
940 * the outer cycle finishes
942 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
943 assert( ncandidates > 0 );
947 case META_SEARCH_CANDIDATE:
948 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
949 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
951 case META_SEARCH_CANDIDATE:
952 assert( candidates[ i ].sr_msgid >= 0 );
955 case META_SEARCH_ERR:
956 candidates[ i ].sr_err = rs->sr_err;
957 if ( META_BACK_ONERR_STOP( mi ) ) {
958 savepriv = op->o_private;
959 op->o_private = (void *)i;
960 send_ldap_result( op, rs );
961 op->o_private = savepriv;
966 case META_SEARCH_NOT_CANDIDATE:
967 /* means that meta_back_search_start()
968 * failed but onerr == continue */
969 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
970 assert( ncandidates > 0 );
989 /* check for abandon */
990 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
995 if ( msc->msc_ld == NULL ) {
996 char buf[ SLAP_TEXT_BUFLEN ];
998 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
999 snprintf( buf, sizeof( buf ),
1000 "%s meta_back_search[%ld] mc=%p msgid=%d%s%s%s\n",
1001 op->o_log_prefix, (long)i, (void *)mc,
1002 candidates[ i ].sr_msgid,
1003 META_IS_BINDING( &candidates[ i ] ) ? " binding" : "",
1004 LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ? " connbinding" : "",
1005 META_BACK_CONN_CREATING( &mc->mc_conns[ i ] ) ? " conncreating" : "" );
1006 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1008 Debug( LDAP_DEBUG_ANY, "!!! %s\n", buf, 0, 0 );
1010 #endif /* DEBUG_205 */
1013 * FIXME: handle time limit as well?
1014 * Note that target servers are likely
1015 * to handle it, so at some time we'll
1016 * get a LDAP_TIMELIMIT_EXCEEDED from
1020 rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
1021 LDAP_MSG_RECEIVED, &tv, &res );
1024 /* FIXME: res should not need to be freed */
1025 assert( res == NULL );
1030 /* something REALLY bad happened! */
1031 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1032 candidates[ i ].sr_type = REP_RESULT;
1034 if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
1035 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1036 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
1038 /* means that failed but onerr == continue */
1039 case META_SEARCH_NOT_CANDIDATE:
1040 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1042 assert( ncandidates > 0 );
1045 candidates[ i ].sr_err = rs->sr_err;
1046 if ( META_BACK_ONERR_STOP( mi ) ) {
1047 savepriv = op->o_private;
1048 op->o_private = (void *)i;
1049 send_ldap_result( op, rs );
1050 op->o_private = savepriv;
1055 case META_SEARCH_CANDIDATE:
1056 /* get back into business... */
1059 case META_SEARCH_BINDING:
1060 case META_SEARCH_CONNECTING:
1061 case META_SEARCH_NEED_BIND:
1062 case META_SEARCH_UNDEFINED:
1066 /* unrecoverable error */
1067 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1068 rc = rs->sr_err = LDAP_OTHER;
1073 candidates[ i ].sr_err = rs->sr_err;
1074 if ( META_BACK_ONERR_STOP( mi ) ) {
1075 savepriv = op->o_private;
1076 op->o_private = (void *)i;
1077 send_ldap_result( op, rs );
1078 op->o_private = savepriv;
1084 * When no candidates are left,
1085 * the outer cycle finishes
1087 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1088 assert( ncandidates > 0 );
1090 rs->sr_err = candidates[ i ].sr_err;
1094 lastres_time = slap_get_time();
1096 /* only touch when activity actually took place... */
1097 if ( mi->mi_idle_timeout != 0 && msc->msc_time < lastres_time ) {
1098 msc->msc_time = lastres_time;
1103 for ( msg = ldap_first_message( msc->msc_ld, res );
1105 msg = ldap_next_message( msc->msc_ld, msg ) )
1107 rc = ldap_msgtype( msg );
1108 if ( rc == LDAP_RES_SEARCH_ENTRY ) {
1111 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1112 /* don't retry any more... */
1113 candidates[ i ].sr_type = REP_RESULT;
1118 e = ldap_first_entry( msc->msc_ld, msg );
1119 savepriv = op->o_private;
1120 op->o_private = (void *)i;
1121 rs->sr_err = meta_send_entry( op, rs, mc, i, e );
1123 switch ( rs->sr_err ) {
1124 case LDAP_SIZELIMIT_EXCEEDED:
1125 savepriv = op->o_private;
1126 op->o_private = (void *)i;
1127 send_ldap_result( op, rs );
1128 op->o_private = savepriv;
1129 rs->sr_err = LDAP_SUCCESS;
1130 ldap_msgfree( res );
1134 case LDAP_UNAVAILABLE:
1135 rs->sr_err = LDAP_OTHER;
1136 ldap_msgfree( res );
1140 op->o_private = savepriv;
1142 /* don't wait any longer... */
1145 save_tv.tv_usec = 0;
1147 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
1148 char **references = NULL;
1151 if ( META_BACK_TGT_NOREFS( mi->mi_targets[ i ] ) ) {
1155 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1156 /* don't retry any more... */
1157 candidates[ i ].sr_type = REP_RESULT;
1162 rc = ldap_parse_reference( msc->msc_ld, msg,
1163 &references, &rs->sr_ctrls, 0 );
1165 if ( rc != LDAP_SUCCESS ) {
1169 if ( references == NULL ) {
1173 #ifdef ENABLE_REWRITE
1174 dc.ctx = "referralDN";
1175 #else /* ! ENABLE_REWRITE */
1178 #endif /* ! ENABLE_REWRITE */
1180 /* FIXME: merge all and return at the end */
1182 for ( cnt = 0; references[ cnt ]; cnt++ )
1185 rs->sr_ref = ber_memalloc_x( sizeof( struct berval ) * ( cnt + 1 ),
1188 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1189 ber_str2bv_x( 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,
1197 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
1198 /* ignore return value by now */
1199 savepriv = op->o_private;
1200 op->o_private = (void *)i;
1201 ( void )send_search_reference( op, rs );
1202 op->o_private = savepriv;
1204 ber_bvarray_free_x( rs->sr_ref, op->o_tmpmemctx );
1210 ber_memvfree( (void **)references );
1213 if ( rs->sr_ctrls ) {
1214 ldap_controls_free( rs->sr_ctrls );
1215 rs->sr_ctrls = NULL;
1218 } else if ( rc == LDAP_RES_INTERMEDIATE ) {
1219 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1220 /* don't retry any more... */
1221 candidates[ i ].sr_type = REP_RESULT;
1224 /* FIXME: response controls
1225 * are passed without checks */
1226 rs->sr_err = ldap_parse_intermediate( msc->msc_ld,
1228 (char **)&rs->sr_rspoid,
1232 if ( rs->sr_err != LDAP_SUCCESS ) {
1233 candidates[ i ].sr_type = REP_RESULT;
1234 ldap_msgfree( res );
1239 slap_send_ldap_intermediate( op, rs );
1241 if ( rs->sr_rspoid != NULL ) {
1242 ber_memfree( (char *)rs->sr_rspoid );
1243 rs->sr_rspoid = NULL;
1246 if ( rs->sr_rspdata != NULL ) {
1247 ber_bvfree( rs->sr_rspdata );
1248 rs->sr_rspdata = NULL;
1251 if ( rs->sr_ctrls != NULL ) {
1252 ldap_controls_free( rs->sr_ctrls );
1253 rs->sr_ctrls = NULL;
1256 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
1257 char buf[ SLAP_TEXT_BUFLEN ];
1258 char **references = NULL;
1260 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1261 /* don't retry any more... */
1262 candidates[ i ].sr_type = REP_RESULT;
1265 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1267 /* NOTE: ignores response controls
1268 * (and intermediate response controls
1269 * as well, except for those with search
1270 * references); this may not be correct,
1271 * but if they're not ignored then
1272 * back-meta would need to merge them
1273 * consistently (think of pagedResults...)
1275 /* FIXME: response controls? */
1276 rs->sr_err = ldap_parse_result( msc->msc_ld,
1278 &candidates[ i ].sr_err,
1279 (char **)&candidates[ i ].sr_matched,
1280 NULL /* (char **)&candidates[ i ].sr_text */ ,
1282 NULL /* &candidates[ i ].sr_ctrls (unused) */ ,
1284 if ( rs->sr_err != LDAP_SUCCESS ) {
1285 sres = slap_map_api2result( &candidates[ i ] );
1286 candidates[ i ].sr_type = REP_RESULT;
1287 ldap_msgfree( res );
1292 rs->sr_err = candidates[ i ].sr_err;
1294 /* massage matchedDN if need be */
1295 if ( candidates[ i ].sr_matched != NULL ) {
1296 struct berval match, mmatch;
1298 ber_str2bv( candidates[ i ].sr_matched,
1300 candidates[ i ].sr_matched = NULL;
1302 dc.ctx = "matchedDN";
1303 dc.target = mi->mi_targets[ i ];
1304 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
1305 if ( mmatch.bv_val == match.bv_val ) {
1306 candidates[ i ].sr_matched
1307 = ch_strdup( mmatch.bv_val );
1310 candidates[ i ].sr_matched = mmatch.bv_val;
1315 ldap_memfree( match.bv_val );
1318 /* add references to array */
1319 /* RFC 4511: referrals can only appear
1320 * if result code is LDAP_REFERRAL */
1321 if ( references != NULL
1322 && references[ 0 ] != NULL
1323 && references[ 0 ][ 0 ] != '\0' )
1325 if ( rs->sr_err != LDAP_REFERRAL ) {
1326 Debug( LDAP_DEBUG_ANY,
1327 "%s meta_back_search[%ld]: "
1328 "got referrals with err=%d\n",
1336 for ( cnt = 0; references[ cnt ]; cnt++ )
1339 sr_ref = ber_memalloc_x( sizeof( struct berval ) * ( cnt + 1 ),
1342 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1343 ber_str2bv_x( references[ cnt ], 0, 1, &sr_ref[ cnt ],
1346 BER_BVZERO( &sr_ref[ cnt ] );
1348 ( void )ldap_back_referral_result_rewrite( &dc, sr_ref,
1351 if ( rs->sr_v2ref == NULL ) {
1352 rs->sr_v2ref = sr_ref;
1355 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
1356 ber_bvarray_add_x( &rs->sr_v2ref, &sr_ref[ cnt ],
1359 ber_memfree_x( sr_ref, op->o_tmpmemctx );
1363 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1364 Debug( LDAP_DEBUG_ANY,
1365 "%s meta_back_search[%ld]: "
1366 "got err=%d with null "
1367 "or empty referrals\n",
1371 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1375 ber_memvfree( (void **)references );
1377 sres = slap_map_api2result( rs );
1379 if ( LogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
1380 snprintf( buf, sizeof( buf ),
1381 "%s meta_back_search[%ld] "
1382 "match=\"%s\" err=%ld",
1383 op->o_log_prefix, i,
1384 candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
1385 (long) candidates[ i ].sr_err );
1386 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1387 Debug( LDAP_DEBUG_TRACE, "%s.\n", buf, 0, 0 );
1390 Debug( LDAP_DEBUG_ANY, "%s (%s).\n",
1391 buf, ldap_err2string( candidates[ i ].sr_err ), 0 );
1396 case LDAP_NO_SUCH_OBJECT:
1397 /* is_ok is touched any time a valid
1398 * (even intermediate) result is
1399 * returned; as a consequence, if
1400 * a candidate returns noSuchObject
1401 * it is ignored and the candidate
1402 * is simply demoted. */
1404 sres = LDAP_SUCCESS;
1413 case LDAP_SIZELIMIT_EXCEEDED:
1414 /* if a target returned sizelimitExceeded
1415 * and the entry count is equal to the
1416 * proxy's limit, the target would have
1417 * returned more, and the error must be
1418 * propagated to the client; otherwise,
1419 * the target enforced a limit lower
1420 * than what requested by the proxy;
1422 candidates[ i ].sr_err = rs->sr_err;
1423 if ( rs->sr_nentries == op->ors_slimit
1424 || META_BACK_ONERR_STOP( mi ) )
1426 savepriv = op->o_private;
1427 op->o_private = (void *)i;
1428 send_ldap_result( op, rs );
1429 op->o_private = savepriv;
1430 ldap_msgfree( res );
1437 candidates[ i ].sr_err = rs->sr_err;
1438 if ( META_BACK_ONERR_STOP( mi ) ) {
1439 savepriv = op->o_private;
1440 op->o_private = (void *)i;
1441 send_ldap_result( op, rs );
1442 op->o_private = savepriv;
1443 ldap_msgfree( res );
1454 * When no candidates are left,
1455 * the outer cycle finishes
1457 assert( ncandidates > 0 );
1460 } else if ( rc == LDAP_RES_BIND ) {
1461 meta_search_candidate_t retcode;
1463 retcode = meta_search_dobind_result( op, rs, &mc, i, candidates, msg );
1464 if ( retcode == META_SEARCH_CANDIDATE ) {
1465 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1466 retcode = meta_back_search_start( op, rs, &dc, &mc, i, candidates );
1469 switch ( retcode ) {
1470 case META_SEARCH_CANDIDATE:
1473 /* means that failed but onerr == continue */
1474 case META_SEARCH_NOT_CANDIDATE:
1475 case META_SEARCH_ERR:
1476 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1477 assert( ncandidates > 0 );
1480 candidates[ i ].sr_err = rs->sr_err;
1481 if ( META_BACK_ONERR_STOP( mi ) ) {
1482 savepriv = op->o_private;
1483 op->o_private = (void *)i;
1484 send_ldap_result( op, rs );
1485 op->o_private = savepriv;
1486 ldap_msgfree( res );
1498 Debug( LDAP_DEBUG_ANY,
1499 "%s meta_back_search[%ld]: "
1500 "unrecognized response message tag=%d\n",
1504 ldap_msgfree( res );
1511 ldap_msgfree( res );
1515 /* check for abandon */
1516 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
1517 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1518 if ( candidates[ i ].sr_msgid >= 0
1519 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1521 if ( META_IS_BINDING( &candidates[ i ] )
1522 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1524 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1525 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] )
1526 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1528 /* if still binding, destroy */
1531 char buf[ SLAP_TEXT_BUFLEN ];
1533 snprintf( buf, sizeof( buf), "%s meta_back_search(abandon) "
1534 "ldap_unbind_ext[%ld] mc=%p ld=%p",
1535 op->o_log_prefix, i, (void *)mc,
1536 (void *)mc->mc_conns[i].msc_ld );
1538 Debug( LDAP_DEBUG_ANY, "### %s\n", buf, 0, 0 );
1539 #endif /* DEBUG_205 */
1541 meta_clear_one_candidate( op, mc, i );
1543 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1544 META_BINDING_CLEAR( &candidates[ i ] );
1547 (void)meta_back_cancel( mc, op, rs,
1548 candidates[ i ].sr_msgid, i,
1549 LDAP_BACK_DONTSEND );
1552 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1553 assert( ncandidates > 0 );
1558 if ( op->o_abandon ) {
1562 /* let send_ldap_result play cleanup handlers (ITS#4645) */
1566 /* if no entry was found during this loop,
1567 * set a minimal timeout */
1568 if ( ncandidates > 0 && gotit == 0 ) {
1569 if ( save_tv.tv_sec == 0 && save_tv.tv_usec == 0 ) {
1570 save_tv.tv_usec = LDAP_BACK_RESULT_UTIMEOUT/initial_candidates;
1572 /* arbitrarily limit to something between 1 and 2 minutes */
1573 } else if ( ( stoptime == -1 && save_tv.tv_sec < 60 )
1574 || save_tv.tv_sec < ( stoptime - slap_get_time() ) / ( 2 * ncandidates ) )
1576 /* double the timeout */
1577 lutil_timermul( &save_tv, 2, &save_tv );
1580 if ( alreadybound == 0 ) {
1582 (void)select( 0, NULL, NULL, NULL, &tv );
1585 ldap_pvt_thread_yield();
1592 * FIXME: need a better strategy to handle errors
1595 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE,
1596 -1, stoptime != -1 ? (stoptime - slap_get_time()) : 0,
1597 LDAP_BACK_SENDERR );
1605 * Rewrite the matched portion of the search base, if required
1607 * FIXME: only the last one gets caught!
1609 savepriv = op->o_private;
1610 op->o_private = (void *)(long)mi->mi_ntargets;
1611 if ( candidate_match > 0 ) {
1612 struct berval pmatched = BER_BVNULL;
1614 /* we use the first one */
1615 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1616 if ( META_IS_CANDIDATE( &candidates[ i ] )
1617 && candidates[ i ].sr_matched != NULL )
1619 struct berval bv, pbv;
1622 /* if we got success, and this target
1623 * returned noSuchObject, and its suffix
1624 * is a superior of the searchBase,
1625 * ignore the matchedDN */
1626 if ( sres == LDAP_SUCCESS
1627 && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
1628 && op->o_req_ndn.bv_len > mi->mi_targets[ i ]->mt_nsuffix.bv_len )
1630 free( (char *)candidates[ i ].sr_matched );
1631 candidates[ i ].sr_matched = NULL;
1635 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
1636 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
1638 if ( rc == LDAP_SUCCESS ) {
1640 /* NOTE: if they all are superiors
1641 * of the baseDN, the shorter is also
1642 * superior of the longer... */
1643 if ( pbv.bv_len > pmatched.bv_len ) {
1644 if ( !BER_BVISNULL( &pmatched ) ) {
1645 op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
1648 op->o_private = (void *)i;
1651 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
1655 if ( candidates[ i ].sr_matched != NULL ) {
1656 free( (char *)candidates[ i ].sr_matched );
1657 candidates[ i ].sr_matched = NULL;
1662 if ( !BER_BVISNULL( &pmatched ) ) {
1663 matched = pmatched.bv_val;
1666 } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
1667 matched = op->o_bd->be_suffix[ 0 ].bv_val;
1671 * In case we returned at least one entry, we return LDAP_SUCCESS
1672 * otherwise, the latter error code we got
1675 if ( sres == LDAP_SUCCESS ) {
1676 if ( rs->sr_v2ref ) {
1677 sres = LDAP_REFERRAL;
1680 if ( META_BACK_ONERR_REPORT( mi ) ) {
1682 * Report errors, if any
1684 * FIXME: we should handle error codes and return the more
1685 * important/reasonable
1687 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1688 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1692 if ( candidates[ i ].sr_err != LDAP_SUCCESS
1693 && candidates[ i ].sr_err != LDAP_NO_SUCH_OBJECT )
1695 sres = candidates[ i ].sr_err;
1703 rs->sr_matched = matched;
1704 rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
1705 send_ldap_result( op, rs );
1706 op->o_private = savepriv;
1707 rs->sr_matched = NULL;
1711 if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
1712 op->o_tmpfree( matched, op->o_tmpmemctx );
1715 if ( rs->sr_v2ref ) {
1716 ber_bvarray_free_x( rs->sr_v2ref, op->o_tmpmemctx );
1719 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1720 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1725 if ( META_IS_BINDING( &candidates[ i ] )
1726 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1728 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1729 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] )
1730 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1732 assert( candidates[ i ].sr_msgid >= 0
1733 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING );
1734 assert( mc->mc_conns[ i ].msc_ld != NULL );
1737 Debug( LDAP_DEBUG_ANY, "### %s meta_back_search(cleanup) "
1738 "ldap_unbind_ext[%ld] ld=%p\n",
1739 op->o_log_prefix, i, (void *)mc->mc_conns[i].msc_ld );
1740 #endif /* DEBUG_205 */
1742 /* if still binding, destroy */
1743 meta_clear_one_candidate( op, mc, i );
1745 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1746 META_BINDING_CLEAR( &candidates[ i ] );
1748 } else if ( candidates[ i ].sr_msgid >= 0 ) {
1749 (void)meta_back_cancel( mc, op, rs,
1750 candidates[ i ].sr_msgid, i,
1751 LDAP_BACK_DONTSEND );
1755 if ( candidates[ i ].sr_matched ) {
1756 free( (char *)candidates[ i ].sr_matched );
1757 candidates[ i ].sr_matched = NULL;
1760 if ( candidates[ i ].sr_text ) {
1761 ldap_memfree( (char *)candidates[ i ].sr_text );
1762 candidates[ i ].sr_text = NULL;
1765 if ( candidates[ i ].sr_ref ) {
1766 ber_bvarray_free( candidates[ i ].sr_ref );
1767 candidates[ i ].sr_ref = NULL;
1770 if ( candidates[ i ].sr_ctrls ) {
1771 ldap_controls_free( candidates[ i ].sr_ctrls );
1772 candidates[ i ].sr_ctrls = NULL;
1775 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1776 meta_back_quarantine( op, &candidates[ i ], i );
1779 /* only in case of timelimit exceeded, if the timelimit exceeded because
1780 * one contacted target never responded, invalidate the connection
1781 * NOTE: should we quarantine the target as well? right now, the connection
1782 * is invalidated; the next time it will be recreated and the target
1783 * will be quarantined if it cannot be contacted */
1784 if ( mi->mi_idle_timeout != 0
1785 && rs->sr_err == LDAP_TIMELIMIT_EXCEEDED
1786 && op->o_time > mc->mc_conns[ i ].msc_time )
1788 /* don't let anyone else use this expired connection */
1789 LDAP_BACK_CONN_TAINTED_SET( mc );
1794 meta_back_release_conn( mi, mc );
1808 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1809 struct berval a, mapped;
1810 int check_duplicate_attrs = 0;
1811 int check_sorted_attrs = 0;
1813 BerElement ber = *e->lm_ber;
1814 Attribute *attr, **attrp;
1823 if ( ber_scanf( &ber, "l{", &len ) == LBER_ERROR ) {
1824 return LDAP_DECODING_ERROR;
1827 if ( ber_set_option( &ber, LBER_OPT_REMAINING_BYTES, &len ) != LBER_OPT_SUCCESS ) {
1831 if ( ber_scanf( &ber, "m{", &bdn ) == LBER_ERROR ) {
1832 return LDAP_DECODING_ERROR;
1836 * Rewrite the dn of the result, if needed
1838 dc.target = mi->mi_targets[ target ];
1839 dc.conn = op->o_conn;
1841 dc.ctx = "searchResult";
1843 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
1844 if ( rs->sr_err != LDAP_SUCCESS) {
1849 * Note: this may fail if the target host(s) schema differs
1850 * from the one known to the meta, and a DN with unknown
1851 * attributes is returned.
1853 * FIXME: should we log anything, or delegate to dnNormalize?
1855 rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
1857 if ( dn.bv_val != bdn.bv_val ) {
1862 if ( rc != LDAP_SUCCESS ) {
1863 Debug( LDAP_DEBUG_ANY,
1864 "%s meta_send_entry(\"%s\"): "
1865 "invalid DN syntax\n",
1866 op->o_log_prefix, ent.e_name.bv_val, 0 );
1867 rc = LDAP_INVALID_DN_SYNTAX;
1874 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1875 ( void )meta_dncache_update_entry( &mi->mi_cache,
1876 &ent.e_nname, target );
1879 attrp = &ent.e_attrs;
1881 dc.ctx = "searchAttrDN";
1882 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
1884 slap_syntax_validate_func *validate;
1885 slap_syntax_transform_func *pretty;
1887 if ( ber_pvt_ber_remaining( &ber ) < 0 ) {
1888 Debug( LDAP_DEBUG_ANY,
1889 "%s meta_send_entry(\"%s\"): "
1890 "unable to parse attr \"%s\".\n",
1891 op->o_log_prefix, ent.e_name.bv_val, a.bv_val );
1897 if ( ber_pvt_ber_remaining( &ber ) == 0 ) {
1901 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_at,
1902 &a, &mapped, BACKLDAP_REMAP );
1903 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
1904 ( void )ber_scanf( &ber, "x" /* [W] */ );
1907 if ( mapped.bv_val != a.bv_val ) {
1908 /* will need to check for duplicate attrs */
1909 check_duplicate_attrs++;
1911 attr = attr_alloc( NULL );
1912 if ( attr == NULL ) {
1916 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
1918 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
1919 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
1921 char buf[ SLAP_TEXT_BUFLEN ];
1923 snprintf( buf, sizeof( buf ),
1924 "%s meta_send_entry(\"%s\"): "
1925 "slap_bv2undef_ad(%s): %s\n",
1926 op->o_log_prefix, ent.e_name.bv_val,
1927 mapped.bv_val, text );
1929 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1930 ( void )ber_scanf( &ber, "x" /* [W] */ );
1936 if ( attr->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL )
1937 check_sorted_attrs = 1;
1939 /* no subschemaSubentry */
1940 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
1941 || attr->a_desc == slap_schema.si_ad_entryDN )
1945 * We eat target's subschemaSubentry because
1946 * a search for this value is likely not
1947 * to resolve to the appropriate backend;
1948 * later, the local subschemaSubentry is
1951 * We also eat entryDN because the frontend
1952 * will reattach it without checking if already
1955 ( void )ber_scanf( &ber, "x" /* [W] */ );
1960 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
1961 || attr->a_vals == NULL )
1963 attr->a_vals = (struct berval *)&slap_dummy_bv;
1966 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
1969 attr->a_numvals = last;
1971 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
1972 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
1974 if ( !validate && !pretty ) {
1979 if ( attr->a_desc == slap_schema.si_ad_objectClass
1980 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
1984 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1987 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_oc,
1988 bv, &mapped, BACKLDAP_REMAP );
1989 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
1996 *bv = attr->a_vals[ last ];
1997 BER_BVZERO( &attr->a_vals[ last ] );
2000 } else if ( mapped.bv_val != bv->bv_val ) {
2003 for ( i = 0; !BER_BVISNULL( &attr->a_vals[ i ] ); i++ ) {
2004 if ( &attr->a_vals[ i ] == bv ) {
2008 if ( ber_bvstrcasecmp( &mapped, &attr->a_vals[ i ] ) == 0 ) {
2013 if ( !BER_BVISNULL( &attr->a_vals[ i ] ) ) {
2017 ber_bvreplace( bv, &mapped );
2019 } else if ( ( oc = oc_bvfind_undef( bv ) ) == NULL ) {
2023 ber_bvreplace( bv, &oc->soc_cname );
2027 * It is necessary to try to rewrite attributes with
2028 * dn syntax because they might be used in ACLs as
2029 * members of groups; since ACLs are applied to the
2030 * rewritten stuff, no dn-based subecj clause could
2031 * be used at the ldap backend side (see
2032 * http://www.OpenLDAP.org/faq/data/cache/452.html)
2033 * The problem can be overcome by moving the dn-based
2034 * ACLs to the target directory server, and letting
2035 * everything pass thru the ldap backend.
2040 if ( attr->a_desc->ad_type->sat_syntax ==
2041 slap_schema.si_syn_distinguishedName )
2043 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
2045 } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
2046 ldap_back_referral_result_rewrite( &dc, attr->a_vals, NULL );
2050 for ( i = 0; i < last; i++ ) {
2055 rc = ordered_value_pretty( attr->a_desc,
2056 &attr->a_vals[i], &pval, NULL );
2059 rc = ordered_value_validate( attr->a_desc,
2060 &attr->a_vals[i], 0 );
2064 LBER_FREE( attr->a_vals[i].bv_val );
2065 if ( --last == i ) {
2066 BER_BVZERO( &attr->a_vals[ i ] );
2069 attr->a_vals[i] = attr->a_vals[last];
2070 BER_BVZERO( &attr->a_vals[last] );
2076 LBER_FREE( attr->a_vals[i].bv_val );
2077 attr->a_vals[i] = pval;
2081 if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
2087 if ( last && attr->a_desc->ad_type->sat_equality &&
2088 attr->a_desc->ad_type->sat_equality->smr_normalize )
2092 attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
2093 for ( i = 0; i<last; i++ ) {
2094 /* if normalizer fails, drop this value */
2095 if ( ordered_value_normalize(
2096 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2098 attr->a_desc->ad_type->sat_equality,
2099 &attr->a_vals[i], &attr->a_nvals[i],
2101 LBER_FREE( attr->a_vals[i].bv_val );
2102 if ( --last == i ) {
2103 BER_BVZERO( &attr->a_vals[ i ] );
2106 attr->a_vals[i] = attr->a_vals[last];
2107 BER_BVZERO( &attr->a_vals[last] );
2111 BER_BVZERO( &attr->a_nvals[i] );
2118 attr->a_nvals = attr->a_vals;
2121 attr->a_numvals = last;
2123 attrp = &attr->a_next;
2127 /* only check if some mapping occurred */
2128 if ( check_duplicate_attrs ) {
2131 for ( ap = &ent.e_attrs; *ap != NULL; ap = &(*ap)->a_next ) {
2134 for ( tap = &(*ap)->a_next; *tap != NULL; ) {
2135 if ( (*tap)->a_desc == (*ap)->a_desc ) {
2137 Modification mod = { 0 };
2138 const char *text = NULL;
2139 char textbuf[ SLAP_TEXT_BUFLEN ];
2140 Attribute *next = (*tap)->a_next;
2142 BER_BVSTR( &e.e_name, "" );
2143 BER_BVSTR( &e.e_nname, "" );
2145 mod.sm_op = LDAP_MOD_ADD;
2146 mod.sm_desc = (*ap)->a_desc;
2147 mod.sm_type = mod.sm_desc->ad_cname;
2148 mod.sm_numvals = (*ap)->a_numvals;
2149 mod.sm_values = (*tap)->a_vals;
2150 if ( (*tap)->a_nvals != (*tap)->a_vals ) {
2151 mod.sm_nvalues = (*tap)->a_nvals;
2154 (void)modify_add_values( &e, &mod,
2156 &text, textbuf, sizeof( textbuf ) );
2158 /* should not insert new attrs! */
2159 assert( e.e_attrs == *ap );
2165 tap = &(*tap)->a_next;
2171 /* Check for sorted attributes */
2172 if ( check_sorted_attrs ) {
2173 for ( attr = ent.e_attrs; attr; attr = attr->a_next ) {
2174 if ( attr->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
2175 while ( attr->a_numvals > 1 ) {
2177 int rc = slap_sort_vals( (Modifications *)attr, &text, &i, op->o_tmpmemctx );
2178 if ( rc != LDAP_TYPE_OR_VALUE_EXISTS )
2181 /* Strip duplicate values */
2182 if ( attr->a_nvals != attr->a_vals )
2183 LBER_FREE( attr->a_nvals[i].bv_val );
2184 LBER_FREE( attr->a_vals[i].bv_val );
2186 if ( i < attr->a_numvals ) {
2187 attr->a_vals[i] = attr->a_vals[attr->a_numvals];
2188 if ( attr->a_nvals != attr->a_vals )
2189 attr->a_nvals[i] = attr->a_nvals[attr->a_numvals];
2191 BER_BVZERO(&attr->a_vals[attr->a_numvals]);
2192 if ( attr->a_nvals != attr->a_vals )
2193 BER_BVZERO(&attr->a_nvals[attr->a_numvals]);
2195 attr->a_flags |= SLAP_ATTR_SORTED_VALS;
2200 ldap_get_entry_controls( mc->mc_conns[target].msc_ld,
2202 rs->sr_entry = &ent;
2203 rs->sr_attrs = op->ors_attrs;
2204 rs->sr_operational_attrs = NULL;
2205 rs->sr_flags = mi->mi_targets[ target ]->mt_rep_flags;
2206 rs->sr_err = LDAP_SUCCESS;
2207 rc = send_search_entry( op, rs );
2209 case LDAP_UNAVAILABLE:
2215 rs->sr_entry = NULL;
2216 rs->sr_attrs = NULL;
2217 if ( rs->sr_ctrls != NULL ) {
2218 ldap_controls_free( rs->sr_ctrls );
2219 rs->sr_ctrls = NULL;
2221 if ( !BER_BVISNULL( &ent.e_name ) ) {
2222 free( ent.e_name.bv_val );
2223 BER_BVZERO( &ent.e_name );
2225 if ( !BER_BVISNULL( &ent.e_nname ) ) {
2226 free( ent.e_nname.bv_val );
2227 BER_BVZERO( &ent.e_nname );
2229 entry_clean( &ent );