2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2006 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>
32 #include "../back-ldap/back-ldap.h"
33 #include "back-meta.h"
34 #undef ldap_debug /* silence a warning in ldap-int.h */
36 #include "../../../libraries/libldap/ldap-int.h"
38 /* IGNORE means that target does not (no longer) participate
40 * NOTREADY means the search on that target has not been initialized yet
42 #define META_MSGID_IGNORE (-1)
43 #define META_MSGID_NEED_BIND (-2)
53 typedef enum meta_search_candidate_t {
55 META_SEARCH_NOT_CANDIDATE,
56 META_SEARCH_CANDIDATE,
59 } meta_search_candidate_t;
62 * meta_search_dobind_init()
64 * initiates bind for a candidate target of a search.
66 static meta_search_candidate_t
67 meta_search_dobind_init(
72 SlapReply *candidates )
74 metaconn_t *mc = *mcp;
75 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
76 metatarget_t *mt = mi->mi_targets[ candidate ];
77 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
79 struct berval binddn = msc->msc_bound_ndn,
85 meta_search_candidate_t retcode;
87 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_search_dobind_init[%d]\n", op->o_log_prefix, candidate, 0 );
90 * all the targets are already bound as pseudoroot
92 if ( mc->mc_authz_target == META_BOUND_ALL ) {
93 return META_SEARCH_CANDIDATE;
96 retcode = META_SEARCH_BINDING;
97 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
98 if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
99 /* already bound (or anonymous) */
100 retcode = META_SEARCH_CANDIDATE;
102 } else if ( LDAP_BACK_CONN_BINDING( msc ) ) {
103 /* another thread is binding the target for this conn; wait */
104 candidates[ candidate ].sr_msgid = META_MSGID_NEED_BIND;
105 retcode = META_SEARCH_NEED_BIND;
108 /* we'll need to bind the target for this conn */
109 LDAP_BACK_CONN_BINDING_SET( msc );
111 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
112 if ( retcode != META_SEARCH_BINDING ) {
116 /* NOTE: this obsoletes pseudorootdn */
117 if ( op->o_conn != NULL &&
118 !op->o_do_not_cache &&
119 ( BER_BVISNULL( &msc->msc_bound_ndn ) ||
120 BER_BVISEMPTY( &msc->msc_bound_ndn ) ||
121 ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
123 rc = meta_back_proxy_authz_cred( mc, candidate, op, rs, LDAP_BACK_DONTSEND, &binddn, &cred, &method );
124 if ( rc != LDAP_SUCCESS ) {
128 /* NOTE: we copy things here, even if bind didn't succeed yet,
129 * because the connection is not shared until bind is over */
130 if ( !BER_BVISNULL( &binddn ) ) {
131 ber_bvreplace( &msc->msc_bound_ndn, &binddn );
132 if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &cred ) ) {
133 ber_dupbv( &msc->msc_cred, &cred );
137 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
138 /* apparently, idassert was configured with SASL bind,
139 * so bind occurred inside meta_back_proxy_authz_cred() */
140 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
141 LDAP_BACK_CONN_BINDING_CLEAR( msc );
142 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
143 return META_SEARCH_CANDIDATE;
149 case LDAP_AUTH_SIMPLE:
150 /* do a simple bind with binddn, cred */
159 assert( msc->msc_ld != NULL );
161 rc = ldap_sasl_bind( msc->msc_ld, binddn.bv_val, LDAP_SASL_SIMPLE, &cred,
162 NULL, NULL, &candidates[ candidate ].sr_msgid );
165 META_BINDING_SET( &candidates[ candidate ] );
166 return META_SEARCH_BINDING;
169 case LDAP_SERVER_DOWN:
170 /* This is the worst thing that could happen:
171 * the search will wait until the retry is over. */
172 if ( meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
173 return META_SEARCH_CANDIDATE;
176 if ( *mcp == NULL ) {
177 retcode = META_SEARCH_ERR;
178 rs->sr_err = LDAP_UNAVAILABLE;
185 rc = slap_map_api2result( rs );
187 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
188 LDAP_BACK_CONN_BINDING_CLEAR( msc );
189 if ( META_BACK_ONERR_STOP( mi ) ) {
190 LDAP_BACK_CONN_TAINTED_SET( mc );
191 meta_back_release_conn( op, mc );
194 retcode = META_SEARCH_ERR;
197 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
198 retcode = META_SEARCH_NOT_CANDIDATE;
200 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
207 static meta_search_candidate_t
208 meta_search_dobind_result(
213 SlapReply *candidates,
216 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
217 metaconn_t *mc = *mcp;
218 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
220 meta_search_candidate_t retcode = META_SEARCH_NOT_CANDIDATE;
223 assert( msc->msc_ld != NULL );
225 /* FIXME: matched? referrals? response controls? */
226 rc = ldap_parse_result( msc->msc_ld, res,
227 &candidates[ candidate ].sr_err,
228 NULL, NULL, NULL, NULL, 1 );
229 if ( rc != LDAP_SUCCESS ) {
230 candidates[ candidate ].sr_err = rc;
232 rc = slap_map_api2result( &candidates[ candidate ] );
234 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
235 LDAP_BACK_CONN_BINDING_CLEAR( msc );
236 if ( rc != LDAP_SUCCESS ) {
237 if ( META_BACK_ONERR_STOP( mi ) ) {
238 LDAP_BACK_CONN_TAINTED_SET( mc );
239 meta_back_release_conn( op, mc );
241 retcode = META_SEARCH_ERR;
245 /* FIXME: check if bound as idassert authcDN! */
246 if ( be_isroot( op ) ) {
247 LDAP_BACK_CONN_ISBOUND_SET( msc );
249 LDAP_BACK_CONN_ISANON_SET( msc );
251 retcode = META_SEARCH_CANDIDATE;
253 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
255 META_BINDING_CLEAR( &candidates[ candidate ] );
260 static meta_search_candidate_t
261 meta_back_search_start(
267 SlapReply *candidates )
269 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
270 metatarget_t *mt = mi->mi_targets[ candidate ];
271 metasingleconn_t *msc = &(*mcp)->mc_conns[ candidate ];
272 struct berval realbase = op->o_req_dn;
273 int realscope = op->ors_scope;
274 struct berval mbase = BER_BVNULL;
275 struct berval mfilter = BER_BVNULL;
276 char **mapped_attrs = NULL;
278 meta_search_candidate_t retcode;
279 struct timeval tv, *tvp = NULL;
281 LDAPControl **ctrls = NULL;
283 /* this should not happen; just in case... */
284 if ( msc->msc_ld == NULL ) {
285 Debug( LDAP_DEBUG_ANY,
286 "%s: meta_back_search_start candidate=%d ld=NULL%s.\n",
287 op->o_log_prefix, candidate,
288 META_BACK_ONERR_STOP( mi ) ? "" : " (ignored)" );
289 if ( META_BACK_ONERR_STOP( mi ) ) {
290 return META_SEARCH_ERR;
292 return META_SEARCH_NOT_CANDIDATE;
295 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_back_search_start[%d]\n", op->o_log_prefix, candidate, 0 );
298 * modifies the base according to the scope, if required
300 if ( mt->mt_nsuffix.bv_len > op->o_req_ndn.bv_len ) {
301 switch ( op->ors_scope ) {
302 case LDAP_SCOPE_SUBTREE:
304 * make the target suffix the new base
305 * FIXME: this is very forgiving, because
306 * "illegal" searchBases may be turned
307 * into the suffix of the target; however,
308 * the requested searchBase already passed
309 * thru the candidate analyzer...
311 if ( dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) ) {
312 realbase = mt->mt_nsuffix;
313 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
314 realscope = LDAP_SCOPE_SUBORDINATE;
319 * this target is no longer candidate
321 retcode = META_SEARCH_NOT_CANDIDATE;
326 case LDAP_SCOPE_SUBORDINATE:
327 case LDAP_SCOPE_ONELEVEL:
329 struct berval rdn = mt->mt_nsuffix;
330 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
331 if ( dnIsOneLevelRDN( &rdn )
332 && dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) )
335 * if there is exactly one level,
336 * make the target suffix the new
337 * base, and make scope "base"
339 realbase = mt->mt_nsuffix;
340 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
341 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
342 realscope = LDAP_SCOPE_SUBORDINATE;
344 realscope = LDAP_SCOPE_SUBTREE;
347 realscope = LDAP_SCOPE_BASE;
350 } /* else continue with the next case */
353 case LDAP_SCOPE_BASE:
355 * this target is no longer candidate
357 retcode = META_SEARCH_NOT_CANDIDATE;
362 /* initiate dobind */
363 retcode = meta_search_dobind_init( op, rs, mcp, candidate, candidates );
365 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%d]=%d\n", op->o_log_prefix, candidate, retcode );
367 if ( retcode != META_SEARCH_CANDIDATE ) {
372 * Rewrite the search base, if required
375 dc->ctx = "searchBase";
376 switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
380 case REWRITE_REGEXEC_UNWILLING:
381 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
382 rs->sr_text = "Operation not allowed";
383 send_ldap_result( op, rs );
384 retcode = META_SEARCH_ERR;
387 case REWRITE_REGEXEC_ERR:
390 * this target is no longer candidate
392 retcode = META_SEARCH_NOT_CANDIDATE;
399 rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
400 &mfilter, BACKLDAP_MAP );
405 case LDAP_COMPARE_FALSE:
408 * this target is no longer candidate
410 retcode = META_SEARCH_NOT_CANDIDATE;
415 * Maps required attributes
417 rc = ldap_back_map_attrs( &mt->mt_rwmap.rwm_at,
418 op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
419 if ( rc != LDAP_SUCCESS ) {
421 * this target is no longer candidate
423 retcode = META_SEARCH_NOT_CANDIDATE;
427 /* should we check return values? */
428 if ( op->ors_deref != -1 ) {
429 assert( msc->msc_ld != NULL );
430 (void)ldap_set_option( msc->msc_ld, LDAP_OPT_DEREF,
431 ( void * )&op->ors_deref );
434 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
435 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
441 if ( ldap_back_proxy_authz_ctrl( &msc->msc_bound_ndn,
442 mt->mt_version, &mt->mt_idassert, op, rs, &ctrls )
445 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
446 retcode = META_SEARCH_NOT_CANDIDATE;
454 assert( msc->msc_ld != NULL );
455 rc = ldap_search_ext( msc->msc_ld,
456 mbase.bv_val, realscope, mfilter.bv_val,
457 mapped_attrs, op->ors_attrsonly,
458 ctrls, NULL, tvp, op->ors_slimit,
459 &candidates[ candidate ].sr_msgid );
462 retcode = META_SEARCH_CANDIDATE;
465 case LDAP_SERVER_DOWN:
466 if ( nretries && meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
471 if ( *mcp == NULL ) {
472 retcode = META_SEARCH_ERR;
478 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
479 retcode = META_SEARCH_NOT_CANDIDATE;
483 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
485 if ( mapped_attrs ) {
486 free( mapped_attrs );
488 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
489 free( mfilter.bv_val );
491 if ( mbase.bv_val != realbase.bv_val ) {
492 free( mbase.bv_val );
496 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_back_search_start[%d]=%d\n", op->o_log_prefix, candidate, retcode );
502 meta_back_search( Operation *op, SlapReply *rs )
504 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
506 struct timeval tv = { 0, 0 };
507 time_t stoptime = (time_t)-1;
508 LDAPMessage *res = NULL, *e;
509 int rc = 0, sres = LDAP_SUCCESS;
510 char *matched = NULL;
511 int last = 0, ncandidates = 0,
512 initial_candidates = 0, candidate_match = 0;
517 SlapReply *candidates = meta_back_candidates_get( op );
520 * controls are set in ldap_back_dobind()
522 * FIXME: in case of values return filter, we might want
523 * to map attrs and maybe rewrite value
525 mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_SENDERR );
530 dc.conn = op->o_conn;
536 for ( i = 0; i < mi->mi_ntargets; i++ ) {
537 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
539 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
543 candidates[ i ].sr_matched = NULL;
544 candidates[ i ].sr_text = NULL;
545 candidates[ i ].sr_ref = NULL;
546 candidates[ i ].sr_ctrls = NULL;
549 for ( i = 0; i < mi->mi_ntargets; i++ ) {
550 if ( !META_IS_CANDIDATE( &candidates[ i ] )
551 || candidates[ i ].sr_err != LDAP_SUCCESS )
556 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
558 case META_SEARCH_NOT_CANDIDATE:
561 case META_SEARCH_CANDIDATE:
562 case META_SEARCH_BINDING:
563 case META_SEARCH_NEED_BIND:
564 candidates[ i ].sr_type = REP_INTERMEDIATE;
568 case META_SEARCH_ERR:
569 savepriv = op->o_private;
570 op->o_private = (void *)i;
571 send_ldap_result( op, rs );
572 op->o_private = savepriv;
578 initial_candidates = ncandidates;
580 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
584 for ( c = 0; c < mi->mi_ntargets; c++ ) {
585 if ( META_IS_CANDIDATE( &candidates[ c ] ) ) {
593 Debug( LDAP_DEBUG_TRACE, "%s meta_back_search: ncandidates=%d "
594 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
597 if ( initial_candidates == 0 ) {
598 /* NOTE: here we are not sending any matchedDN;
599 * this is intended, because if the back-meta
600 * is serving this search request, but no valid
601 * candidate could be looked up, it means that
602 * there is a hole in the mapping of the targets
603 * and thus no knowledge of any remote superior
605 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
606 "base=\"%s\" scope=%d: "
607 "no candidate could be selected\n",
608 op->o_log_prefix, op->o_req_dn.bv_val,
611 /* FIXME: we're sending the first error we encounter;
612 * maybe we should pick the worst... */
613 rc = LDAP_NO_SUCH_OBJECT;
614 for ( i = 0; i < mi->mi_ntargets; i++ ) {
615 if ( META_IS_CANDIDATE( &candidates[ i ] )
616 && candidates[ i ].sr_err != LDAP_SUCCESS )
618 rc = candidates[ i ].sr_err;
623 send_ldap_error( op, rs, rc, NULL );
628 /* We pull apart the ber result, stuff it into a slapd entry, and
629 * let send_search_entry stuff it back into ber format. Slow & ugly,
630 * but this is necessary for version matching, and for ACL processing.
633 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
634 stoptime = op->o_time + op->ors_tlimit;
638 * In case there are no candidates, no cycle takes place...
640 * FIXME: we might use a queue, to better balance the load
641 * among the candidates
643 for ( rc = 0; ncandidates > 0; ) {
644 int gotit = 0, doabandon = 0;
646 /* check time limit */
647 if ( op->ors_tlimit != SLAP_NO_LIMIT
648 && slap_get_time() > stoptime )
651 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
652 savepriv = op->o_private;
653 op->o_private = (void *)i;
654 send_ldap_result( op, rs );
655 op->o_private = savepriv;
659 for ( i = 0; i < mi->mi_ntargets; i++ ) {
660 metasingleconn_t *msc = &mc->mc_conns[ i ];
662 if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
666 if ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND ) {
667 meta_search_candidate_t retcode;
669 /* initiate dobind */
670 retcode = meta_search_dobind_init( op, rs, &mc, i, candidates );
672 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%ld]=%d\n",
673 op->o_log_prefix, i, retcode );
676 case META_SEARCH_BINDING:
677 case META_SEARCH_NEED_BIND:
680 case META_SEARCH_NOT_CANDIDATE:
682 * When no candidates are left,
683 * the outer cycle finishes
685 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
689 case META_SEARCH_ERR:
690 if ( META_BACK_ONERR_STOP( mi ) ) {
691 savepriv = op->o_private;
692 op->o_private = (void *)i;
693 send_ldap_result( op, rs );
694 op->o_private = savepriv;
699 case META_SEARCH_CANDIDATE:
700 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
702 case META_SEARCH_CANDIDATE:
705 /* means that failed but onerr == continue */
706 case META_SEARCH_NOT_CANDIDATE:
707 case META_SEARCH_ERR:
708 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
711 if ( META_BACK_ONERR_STOP( mi ) ) {
712 savepriv = op->o_private;
713 op->o_private = (void *)i;
714 send_ldap_result( op, rs );
715 op->o_private = savepriv;
735 /* check for abandon */
736 if ( op->o_abandon ) {
741 * FIXME: handle time limit as well?
742 * Note that target servers are likely
743 * to handle it, so at some time we'll
744 * get a LDAP_TIMELIMIT_EXCEEDED from
748 rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
749 LDAP_MSG_ONE, &tv, &res );
753 /* FIXME: res should not need to be freed */
754 assert( res == NULL );
760 /* something REALLY bad happened! */
761 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
762 candidates[ i ].sr_type = REP_RESULT;
764 if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
765 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
767 case META_SEARCH_CANDIDATE:
770 /* means that failed but onerr == continue */
771 case META_SEARCH_NOT_CANDIDATE:
772 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
775 if ( META_BACK_ONERR_STOP( mi ) ) {
776 savepriv = op->o_private;
777 op->o_private = (void *)i;
778 send_ldap_result( op, rs );
779 op->o_private = savepriv;
784 case META_SEARCH_BINDING:
785 case META_SEARCH_NEED_BIND:
789 rc = rs->sr_err = LDAP_OTHER;
794 if ( META_BACK_ONERR_STOP( mi ) ) {
795 savepriv = op->o_private;
796 op->o_private = (void *)i;
797 send_ldap_result( op, rs );
798 op->o_private = savepriv;
804 * When no candidates are left,
805 * the outer cycle finishes
807 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
809 rs->sr_err = candidates[ i ].sr_err;
813 /* only touch when activity actually took place... */
814 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
815 msc->msc_time = op->o_time;
820 if ( rc == LDAP_RES_SEARCH_ENTRY ) {
821 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
822 /* don't retry any more... */
823 candidates[ i ].sr_type = REP_RESULT;
828 e = ldap_first_entry( msc->msc_ld, res );
829 savepriv = op->o_private;
830 op->o_private = (void *)i;
831 rs->sr_err = meta_send_entry( op, rs, mc, i, e );
835 switch ( rs->sr_err ) {
836 case LDAP_SIZELIMIT_EXCEEDED:
837 savepriv = op->o_private;
838 op->o_private = (void *)i;
839 send_ldap_result( op, rs );
840 op->o_private = savepriv;
841 rs->sr_err = LDAP_SUCCESS;
844 case LDAP_UNAVAILABLE:
845 rs->sr_err = LDAP_OTHER;
848 op->o_private = savepriv;
850 /* don't wait any longer... */
857 * If scope is BASE, we need to jump out
858 * as soon as one entry is found; if
859 * the target pool is properly crafted,
860 * this should correspond to the sole
861 * entry that has the base DN
863 /* FIXME: this defeats the purpose of
864 * doing a search with scope == base and
865 * sizelimit = 1 to determine if a
866 * candidate is actually unique */
867 if ( op->ors_scope == LDAP_SCOPE_BASE
868 && rs->sr_nentries > 0 )
877 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
878 char **references = NULL;
881 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
882 /* don't retry any more... */
883 candidates[ i ].sr_type = REP_RESULT;
888 rc = ldap_parse_reference( msc->msc_ld, res,
889 &references, &rs->sr_ctrls, 1 );
892 if ( rc != LDAP_SUCCESS ) {
896 if ( references == NULL ) {
900 #ifdef ENABLE_REWRITE
901 dc.ctx = "referralDN";
902 #else /* ! ENABLE_REWRITE */
905 #endif /* ! ENABLE_REWRITE */
907 /* FIXME: merge all and return at the end */
909 for ( cnt = 0; references[ cnt ]; cnt++ )
912 rs->sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
914 for ( cnt = 0; references[ cnt ]; cnt++ ) {
915 ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
917 BER_BVZERO( &rs->sr_ref[ cnt ] );
919 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref );
921 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
922 /* ignore return value by now */
923 savepriv = op->o_private;
924 op->o_private = (void *)i;
925 ( void )send_search_reference( op, rs );
926 op->o_private = savepriv;
928 ber_bvarray_free( rs->sr_ref );
934 ber_memvfree( (void **)references );
937 if ( rs->sr_ctrls ) {
938 ldap_controls_free( rs->sr_ctrls );
942 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
943 char buf[ SLAP_TEXT_BUFLEN ];
944 char **references = NULL;
946 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
947 /* don't retry any more... */
948 candidates[ i ].sr_type = REP_RESULT;
951 /* NOTE: ignores response controls
952 * (and intermediate response controls
953 * as well, except for those with search
954 * references); this may not be correct,
955 * but if they're not ignored then
956 * back-meta would need to merge them
957 * consistently (think of pagedResults...)
959 /* FIXME: response controls? */
960 rs->sr_err = ldap_parse_result( msc->msc_ld,
962 &candidates[ i ].sr_err,
963 (char **)&candidates[ i ].sr_matched,
964 NULL /* (char **)&candidates[ i ].sr_text */ ,
966 NULL /* &candidates[ i ].sr_ctrls (unused) */ ,
969 if ( rs->sr_err != LDAP_SUCCESS ) {
970 ldap_get_option( msc->msc_ld,
971 LDAP_OPT_ERROR_NUMBER,
973 sres = slap_map_api2result( rs );
974 candidates[ i ].sr_type = REP_RESULT;
978 /* massage matchedDN if need be */
979 if ( candidates[ i ].sr_matched != NULL ) {
980 struct berval match, mmatch;
982 ber_str2bv( candidates[ i ].sr_matched,
984 candidates[ i ].sr_matched = NULL;
986 dc.ctx = "matchedDN";
987 dc.target = mi->mi_targets[ i ];
988 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
989 if ( mmatch.bv_val == match.bv_val ) {
990 candidates[ i ].sr_matched = ch_strdup( mmatch.bv_val );
993 candidates[ i ].sr_matched = mmatch.bv_val;
998 ldap_memfree( match.bv_val );
1001 /* add references to array */
1006 for ( cnt = 0; references[ cnt ]; cnt++ )
1009 sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
1011 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1012 ber_str2bv( references[ cnt ], 0, 1, &sr_ref[ cnt ] );
1014 BER_BVZERO( &sr_ref[ cnt ] );
1016 ( void )ldap_back_referral_result_rewrite( &dc, sr_ref );
1019 ber_memvfree( (void **)references );
1021 if ( rs->sr_v2ref == NULL ) {
1022 rs->sr_v2ref = sr_ref;
1025 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
1026 ber_bvarray_add( &rs->sr_v2ref, &sr_ref[ cnt ] );
1028 ber_memfree( sr_ref );
1032 rs->sr_err = candidates[ i ].sr_err;
1033 sres = slap_map_api2result( rs );
1035 if ( LogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
1036 snprintf( buf, sizeof( buf ),
1037 "%s meta_back_search[%ld] "
1038 "match=\"%s\" err=%ld",
1039 op->o_log_prefix, i,
1040 candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
1041 (long) candidates[ i ].sr_err );
1042 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1043 Debug( LDAP_DEBUG_TRACE, "%s.\n", buf, 0, 0 );
1046 Debug( LDAP_DEBUG_ANY, "%s (%s).\n",
1047 buf, ldap_err2string( candidates[ i ].sr_err ), 0 );
1052 case LDAP_NO_SUCH_OBJECT:
1053 /* is_ok is touched any time a valid
1054 * (even intermediate) result is
1055 * returned; as a consequence, if
1056 * a candidate returns noSuchObject
1057 * it is ignored and the candidate
1058 * is simply demoted. */
1060 sres = LDAP_SUCCESS;
1069 case LDAP_SIZELIMIT_EXCEEDED:
1070 /* if a target returned sizelimitExceeded
1071 * and the entry count is equal to the
1072 * proxy's limit, the target would have
1073 * returned more, and the error must be
1074 * propagated to the client; otherwise,
1075 * the target enforced a limit lower
1076 * than what requested by the proxy;
1078 if ( rs->sr_nentries == op->ors_slimit
1079 || META_BACK_ONERR_STOP( mi ) )
1081 savepriv = op->o_private;
1082 op->o_private = (void *)i;
1083 send_ldap_result( op, rs );
1084 op->o_private = savepriv;
1090 if ( META_BACK_ONERR_STOP( mi ) ) {
1091 savepriv = op->o_private;
1092 op->o_private = (void *)i;
1093 send_ldap_result( op, rs );
1094 op->o_private = savepriv;
1104 * When no candidates are left,
1105 * the outer cycle finishes
1107 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1110 } else if ( rc == LDAP_RES_BIND ) {
1111 meta_search_candidate_t retcode;
1113 retcode = meta_search_dobind_result( op, rs, &mc, i, candidates, res );
1114 if ( retcode == META_SEARCH_CANDIDATE ) {
1115 retcode = meta_back_search_start( op, rs, &dc, &mc, i, candidates );
1118 switch ( retcode ) {
1119 case META_SEARCH_CANDIDATE:
1122 /* means that failed but onerr == continue */
1123 case META_SEARCH_NOT_CANDIDATE:
1124 case META_SEARCH_ERR:
1125 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1128 if ( META_BACK_ONERR_STOP( mi ) ) {
1129 savepriv = op->o_private;
1130 op->o_private = (void *)i;
1131 send_ldap_result( op, rs );
1132 op->o_private = savepriv;
1148 /* check for abandon */
1149 if ( op->o_abandon || doabandon ) {
1150 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1151 if ( candidates[ i ].sr_msgid >= 0 ) {
1152 if ( META_IS_BINDING( &candidates[ i ] ) ) {
1153 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1154 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ) {
1155 /* if still binding, destroy */
1156 ldap_unbind_ext( mc->mc_conns[ i ].msc_ld, NULL, NULL );
1157 mc->mc_conns[ i ].msc_ld = NULL;
1158 LDAP_BACK_CONN_BINDING_CLEAR( &mc->mc_conns[ i ] );
1160 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1161 META_BINDING_CLEAR( &candidates[ i ] );
1164 (void)meta_back_cancel( mc, op, rs,
1165 candidates[ i ].sr_msgid, i,
1166 LDAP_BACK_DONTSEND );
1169 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1173 if ( op->o_abandon ) {
1175 /* let send_ldap_result play cleanup handlers (ITS#4645) */
1180 /* if no entry was found during this loop,
1181 * set a minimal timeout */
1183 /* make the entire wait last
1184 * LDAP_BACK_RESULT_UTIMEOUT at worst */
1186 tv.tv_usec = LDAP_BACK_RESULT_UTIMEOUT/initial_candidates;
1187 ldap_pvt_thread_yield();
1193 * FIXME: need a better strategy to handle errors
1196 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE,
1197 -1, stoptime != -1 ? (stoptime - slap_get_time()) : 0,
1198 LDAP_BACK_SENDERR );
1206 * Rewrite the matched portion of the search base, if required
1208 * FIXME: only the last one gets caught!
1210 savepriv = op->o_private;
1211 op->o_private = (void *)(long)mi->mi_ntargets;
1212 if ( candidate_match > 0 ) {
1213 struct berval pmatched = BER_BVNULL;
1215 /* we use the first one */
1216 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1217 if ( META_IS_CANDIDATE( &candidates[ i ] )
1218 && candidates[ i ].sr_matched != NULL )
1220 struct berval bv, pbv;
1223 /* if we got success, and this target
1224 * returned noSuchObject, and its suffix
1225 * is a superior of the searchBase,
1226 * ignore the matchedDN */
1227 if ( sres == LDAP_SUCCESS
1228 && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
1229 && op->o_req_ndn.bv_len > mi->mi_targets[ i ]->mt_nsuffix.bv_len )
1231 free( (char *)candidates[ i ].sr_matched );
1232 candidates[ i ].sr_matched = NULL;
1236 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
1237 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
1239 if ( rc == LDAP_SUCCESS ) {
1241 /* NOTE: if they all are superiors
1242 * of the baseDN, the shorter is also
1243 * superior of the longer... */
1244 if ( pbv.bv_len > pmatched.bv_len ) {
1245 if ( !BER_BVISNULL( &pmatched ) ) {
1246 op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
1249 op->o_private = (void *)i;
1252 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
1256 if ( candidates[ i ].sr_matched != NULL ) {
1257 free( (char *)candidates[ i ].sr_matched );
1258 candidates[ i ].sr_matched = NULL;
1263 if ( !BER_BVISNULL( &pmatched ) ) {
1264 matched = pmatched.bv_val;
1267 } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
1268 matched = op->o_bd->be_suffix[ 0 ].bv_val;
1277 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1278 if ( META_IS_CANDIDATE( &candidates[ i ] ) ) {
1286 snprintf( buf, sizeof( buf ), "%s meta_back_search: is_scope=%d is_ok=%d cnd=\"%s\"\n",
1287 op->o_log_prefix, initial_candidates, is_ok, cnd );
1289 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1294 * In case we returned at least one entry, we return LDAP_SUCCESS
1295 * otherwise, the latter error code we got
1297 * FIXME: we should handle error codes and return the more
1298 * important/reasonable
1301 if ( sres == LDAP_SUCCESS && rs->sr_v2ref ) {
1302 sres = LDAP_REFERRAL;
1305 rs->sr_matched = matched;
1306 rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
1307 send_ldap_result( op, rs );
1308 op->o_private = savepriv;
1309 rs->sr_matched = NULL;
1313 if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
1314 op->o_tmpfree( matched, op->o_tmpmemctx );
1317 if ( rs->sr_v2ref ) {
1318 ber_bvarray_free( rs->sr_v2ref );
1321 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1322 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1326 if ( mc && META_IS_BINDING( &candidates[ i ] ) ) {
1327 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1328 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ) {
1329 LDAP_BACK_CONN_BINDING_CLEAR( &mc->mc_conns[ i ] );
1331 assert( candidates[ i ].sr_msgid >= 0 );
1332 assert( mc->mc_conns[ i ].msc_ld != NULL );
1334 /* if still binding, destroy */
1335 ldap_unbind_ext( mc->mc_conns[ i ].msc_ld, NULL, NULL );
1336 mc->mc_conns[ i ].msc_ld = NULL;
1338 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1339 META_BINDING_CLEAR( &candidates[ i ] );
1342 if ( candidates[ i ].sr_matched ) {
1343 free( (char *)candidates[ i ].sr_matched );
1344 candidates[ i ].sr_matched = NULL;
1347 if ( candidates[ i ].sr_text ) {
1348 ldap_memfree( (char *)candidates[ i ].sr_text );
1349 candidates[ i ].sr_text = NULL;
1352 if ( candidates[ i ].sr_ref ) {
1353 ber_bvarray_free( candidates[ i ].sr_ref );
1354 candidates[ i ].sr_ref = NULL;
1357 if ( candidates[ i ].sr_ctrls ) {
1358 ldap_controls_free( candidates[ i ].sr_ctrls );
1359 candidates[ i ].sr_ctrls = NULL;
1362 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1363 meta_back_quarantine( op, &candidates[ i ], i );
1366 /* only in case of timelimit exceeded, if the timelimit exceeded because
1367 * one contacted target never responded, invalidate the connection
1368 * NOTE: should we quarantine the target as well? right now, the connection
1369 * is invalidated; the next time it will be recreated and the target
1370 * will be quarantined if it cannot be contacted */
1371 if ( mi->mi_idle_timeout != 0
1372 && rs->sr_err == LDAP_TIMELIMIT_EXCEEDED
1373 && op->o_time > mc->mc_conns[ i ].msc_time )
1375 /* don't let anyone else use this expired connection */
1376 LDAP_BACK_CONN_TAINTED_SET( mc );
1381 meta_back_release_conn( op, mc );
1395 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1396 struct berval a, mapped;
1398 BerElement ber = *e->lm_ber;
1399 Attribute *attr, **attrp;
1406 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
1407 return LDAP_DECODING_ERROR;
1411 * Rewrite the dn of the result, if needed
1413 dc.target = mi->mi_targets[ target ];
1414 dc.conn = op->o_conn;
1416 dc.ctx = "searchResult";
1418 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
1419 if ( rs->sr_err != LDAP_SUCCESS) {
1424 * Note: this may fail if the target host(s) schema differs
1425 * from the one known to the meta, and a DN with unknown
1426 * attributes is returned.
1428 * FIXME: should we log anything, or delegate to dnNormalize?
1430 rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
1432 if ( dn.bv_val != bdn.bv_val ) {
1437 if ( rc != LDAP_SUCCESS ) {
1438 return LDAP_INVALID_DN_SYNTAX;
1444 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1445 ( void )meta_dncache_update_entry( &mi->mi_cache,
1446 &ent.e_nname, target );
1449 attrp = &ent.e_attrs;
1451 dc.ctx = "searchAttrDN";
1452 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
1454 slap_syntax_validate_func *validate;
1455 slap_syntax_transform_func *pretty;
1457 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_at,
1458 &a, &mapped, BACKLDAP_REMAP );
1459 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
1460 ( void )ber_scanf( &ber, "x" /* [W] */ );
1463 attr = attr_alloc( NULL );
1464 if ( attr == NULL ) {
1467 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
1469 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
1470 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
1472 char buf[ SLAP_TEXT_BUFLEN ];
1474 snprintf( buf, sizeof( buf ),
1475 "%s meta_send_entry(\"%s\"): "
1476 "slap_bv2undef_ad(%s): %s\n",
1477 op->o_log_prefix, ent.e_name.bv_val,
1478 mapped.bv_val, text );
1480 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1486 /* no subschemaSubentry */
1487 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
1488 || attr->a_desc == slap_schema.si_ad_entryDN )
1492 * We eat target's subschemaSubentry because
1493 * a search for this value is likely not
1494 * to resolve to the appropriate backend;
1495 * later, the local subschemaSubentry is
1498 * We also eat entryDN because the frontend
1499 * will reattach it without checking if already
1502 ( void )ber_scanf( &ber, "x" /* [W] */ );
1508 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
1509 || attr->a_vals == NULL )
1511 attr->a_vals = (struct berval *)&slap_dummy_bv;
1514 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
1518 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
1519 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
1521 if ( !validate && !pretty ) {
1526 if ( attr->a_desc == slap_schema.si_ad_objectClass
1527 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
1531 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1532 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_oc,
1533 bv, &mapped, BACKLDAP_REMAP );
1534 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
1540 *bv = attr->a_vals[ last ];
1541 BER_BVZERO( &attr->a_vals[ last ] );
1544 } else if ( mapped.bv_val != bv->bv_val ) {
1546 ber_dupbv( bv, &mapped );
1550 * It is necessary to try to rewrite attributes with
1551 * dn syntax because they might be used in ACLs as
1552 * members of groups; since ACLs are applied to the
1553 * rewritten stuff, no dn-based subecj clause could
1554 * be used at the ldap backend side (see
1555 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1556 * The problem can be overcome by moving the dn-based
1557 * ACLs to the target directory server, and letting
1558 * everything pass thru the ldap backend.
1563 if ( attr->a_desc->ad_type->sat_syntax ==
1564 slap_schema.si_syn_distinguishedName )
1566 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
1568 } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
1569 ldap_back_referral_result_rewrite( &dc, attr->a_vals );
1573 for ( i = 0; i < last; i++ ) {
1578 rc = pretty( attr->a_desc->ad_type->sat_syntax,
1579 &attr->a_vals[i], &pval, NULL );
1582 rc = validate( attr->a_desc->ad_type->sat_syntax,
1587 LBER_FREE( attr->a_vals[i].bv_val );
1588 if ( --last == i ) {
1589 BER_BVZERO( &attr->a_vals[ i ] );
1592 attr->a_vals[i] = attr->a_vals[last];
1593 BER_BVZERO( &attr->a_vals[last] );
1599 LBER_FREE( attr->a_vals[i].bv_val );
1600 attr->a_vals[i] = pval;
1604 if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
1610 if ( last && attr->a_desc->ad_type->sat_equality &&
1611 attr->a_desc->ad_type->sat_equality->smr_normalize )
1615 attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
1616 for ( i = 0; i<last; i++ ) {
1617 attr->a_desc->ad_type->sat_equality->smr_normalize(
1618 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1619 attr->a_desc->ad_type->sat_syntax,
1620 attr->a_desc->ad_type->sat_equality,
1621 &attr->a_vals[i], &attr->a_nvals[i],
1624 BER_BVZERO( &attr->a_nvals[i] );
1627 attr->a_nvals = attr->a_vals;
1631 attrp = &attr->a_next;
1634 rs->sr_entry = &ent;
1635 rs->sr_attrs = op->ors_attrs;
1637 rs->sr_err = LDAP_SUCCESS;
1638 rc = send_search_entry( op, rs );
1640 case LDAP_UNAVAILABLE:
1644 rs->sr_entry = NULL;
1645 rs->sr_attrs = NULL;
1647 if ( !BER_BVISNULL( &ent.e_name ) ) {
1648 free( ent.e_name.bv_val );
1649 BER_BVZERO( &ent.e_name );
1651 if ( !BER_BVISNULL( &ent.e_nname ) ) {
1652 free( ent.e_nname.bv_val );
1653 BER_BVZERO( &ent.e_nname );
1655 entry_clean( &ent );