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 ];
80 struct berval cred = BER_BVC( "" );
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 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
97 if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
98 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
99 return META_SEARCH_CANDIDATE;
102 if ( LDAP_BACK_CONN_BINDING( msc ) ) {
103 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
104 candidates[ candidate ].sr_msgid = META_MSGID_NEED_BIND;
105 return META_SEARCH_NEED_BIND;
108 LDAP_BACK_CONN_BINDING_SET( msc );
109 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
111 if ( be_isroot( op ) && !BER_BVISNULL( &mt->mt_pseudorootdn ) ) {
112 binddn = mt->mt_pseudorootdn.bv_val;
113 cred = mt->mt_pseudorootpw;
117 * Otherwise an anonymous bind is performed
118 * (note: if the target was already bound, the anonymous
119 * bind clears the previous bind).
121 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
122 ber_memfree( msc->msc_bound_ndn.bv_val );
123 BER_BVZERO( &msc->msc_bound_ndn );
126 if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
127 /* destroy sensitive data */
128 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
129 ber_memfree( msc->msc_cred.bv_val );
130 BER_BVZERO( &msc->msc_cred );
134 assert( msc->msc_ld != NULL );
136 rc = ldap_sasl_bind( msc->msc_ld, binddn, LDAP_SASL_SIMPLE, &cred,
137 NULL, NULL, &candidates[ candidate ].sr_msgid );
140 return META_SEARCH_BINDING;
142 case LDAP_SERVER_DOWN:
143 if ( nretries && meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
148 if ( *mcp == NULL ) {
149 retcode = META_SEARCH_ERR;
150 rs->sr_err = LDAP_UNAVAILABLE;
157 rc = slap_map_api2result( rs );
159 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
160 LDAP_BACK_CONN_BINDING_CLEAR( msc );
161 if ( META_BACK_ONERR_STOP( mi ) ) {
162 LDAP_BACK_CONN_TAINTED_SET( mc );
163 meta_back_release_conn( op, mc );
166 retcode = META_SEARCH_ERR;
169 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
170 retcode = META_SEARCH_NOT_CANDIDATE;
172 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
179 static meta_search_candidate_t
180 meta_search_dobind_result(
185 SlapReply *candidates,
188 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
189 metaconn_t *mc = *mcp;
190 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
192 meta_search_candidate_t retcode = META_SEARCH_NOT_CANDIDATE;
195 assert( msc->msc_ld != NULL );
197 rc = ldap_parse_result( msc->msc_ld, res,
198 &candidates[ candidate ].sr_err,
199 NULL, NULL, NULL, NULL, 1 );
200 if ( rc == LDAP_SUCCESS ) {
201 rc = slap_map_api2result( &candidates[ candidate ] );
204 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
205 LDAP_BACK_CONN_BINDING_CLEAR( msc );
206 if ( rc != LDAP_SUCCESS ) {
207 if ( META_BACK_ONERR_STOP( mi ) ) {
208 LDAP_BACK_CONN_TAINTED_SET( mc );
209 meta_back_release_conn( op, mc );
211 retcode = META_SEARCH_ERR;
215 if ( be_isroot( op ) ) {
216 LDAP_BACK_CONN_ISBOUND_SET( msc );
218 LDAP_BACK_CONN_ISANON_SET( msc );
220 retcode = META_SEARCH_CANDIDATE;
222 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
227 static meta_search_candidate_t
228 meta_back_search_start(
234 SlapReply *candidates )
236 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
237 metatarget_t *mt = mi->mi_targets[ candidate ];
238 metasingleconn_t *msc = &(*mcp)->mc_conns[ candidate ];
239 struct berval realbase = op->o_req_dn;
240 int realscope = op->ors_scope;
241 struct berval mbase = BER_BVNULL;
242 struct berval mfilter = BER_BVNULL;
243 char **mapped_attrs = NULL;
245 meta_search_candidate_t retcode;
246 struct timeval tv, *tvp = NULL;
249 /* this should not happen; just in case... */
250 if ( msc->msc_ld == NULL ) {
251 Debug( LDAP_DEBUG_ANY,
252 "%s: meta_back_search_start candidate=%d ld=NULL%s.\n",
253 op->o_log_prefix, candidate,
254 META_BACK_ONERR_STOP( mi ) ? "" : " (ignored)" );
255 if ( META_BACK_ONERR_STOP( mi ) ) {
256 return META_SEARCH_ERR;
258 return META_SEARCH_NOT_CANDIDATE;
261 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_back_search_start[%d]\n", op->o_log_prefix, candidate, 0 );
264 * modifies the base according to the scope, if required
266 if ( mt->mt_nsuffix.bv_len > op->o_req_ndn.bv_len ) {
267 switch ( op->ors_scope ) {
268 case LDAP_SCOPE_SUBTREE:
270 * make the target suffix the new base
271 * FIXME: this is very forgiving, because
272 * "illegal" searchBases may be turned
273 * into the suffix of the target; however,
274 * the requested searchBase already passed
275 * thru the candidate analyzer...
277 if ( dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) ) {
278 realbase = mt->mt_nsuffix;
279 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
280 realscope = LDAP_SCOPE_SUBORDINATE;
285 * this target is no longer candidate
287 return META_SEARCH_NOT_CANDIDATE;
291 case LDAP_SCOPE_SUBORDINATE:
292 case LDAP_SCOPE_ONELEVEL:
294 struct berval rdn = mt->mt_nsuffix;
295 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
296 if ( dnIsOneLevelRDN( &rdn )
297 && dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) )
300 * if there is exactly one level,
301 * make the target suffix the new
302 * base, and make scope "base"
304 realbase = mt->mt_nsuffix;
305 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
306 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
307 realscope = LDAP_SCOPE_SUBORDINATE;
309 realscope = LDAP_SCOPE_SUBTREE;
312 realscope = LDAP_SCOPE_BASE;
315 } /* else continue with the next case */
318 case LDAP_SCOPE_BASE:
320 * this target is no longer candidate
322 return META_SEARCH_NOT_CANDIDATE;
326 /* initiate dobind */
327 retcode = meta_search_dobind_init( op, rs, mcp, candidate, candidates );
329 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%d]=%d\n", op->o_log_prefix, candidate, retcode );
331 if ( retcode != META_SEARCH_CANDIDATE ) {
336 * Rewrite the search base, if required
339 dc->ctx = "searchBase";
340 switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
344 case REWRITE_REGEXEC_UNWILLING:
345 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
346 rs->sr_text = "Operation not allowed";
347 send_ldap_result( op, rs );
348 return META_SEARCH_ERR;
350 case REWRITE_REGEXEC_ERR:
353 * this target is no longer candidate
355 return META_SEARCH_NOT_CANDIDATE;
361 rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
362 &mfilter, BACKLDAP_MAP );
367 case LDAP_COMPARE_FALSE:
370 * this target is no longer candidate
372 retcode = META_SEARCH_NOT_CANDIDATE;
377 * Maps required attributes
379 rc = ldap_back_map_attrs( &mt->mt_rwmap.rwm_at,
380 op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
381 if ( rc != LDAP_SUCCESS ) {
383 * this target is no longer candidate
385 retcode = META_SEARCH_NOT_CANDIDATE;
389 /* should we check return values? */
390 if ( op->ors_deref != -1 ) {
391 assert( msc->msc_ld != NULL );
392 (void)ldap_set_option( msc->msc_ld, LDAP_OPT_DEREF,
393 ( void * )&op->ors_deref );
396 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
397 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
406 assert( msc->msc_ld != NULL );
407 rc = ldap_search_ext( msc->msc_ld,
408 mbase.bv_val, realscope, mfilter.bv_val,
409 mapped_attrs, op->ors_attrsonly,
410 op->o_ctrls, NULL, tvp, op->ors_slimit,
411 &candidates[ candidate ].sr_msgid );
414 retcode = META_SEARCH_CANDIDATE;
417 case LDAP_SERVER_DOWN:
418 if ( nretries && meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
423 if ( *mcp == NULL ) {
424 retcode = META_SEARCH_ERR;
430 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
431 retcode = META_SEARCH_NOT_CANDIDATE;
435 if ( mapped_attrs ) {
436 free( mapped_attrs );
438 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
439 free( mfilter.bv_val );
441 if ( mbase.bv_val != realbase.bv_val ) {
442 free( mbase.bv_val );
449 meta_back_search( Operation *op, SlapReply *rs )
451 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
453 struct timeval tv = { 0, 0 };
454 time_t stoptime = (time_t)-1;
455 LDAPMessage *res = NULL, *e;
456 int rc = 0, sres = LDAP_SUCCESS;
457 char *matched = NULL;
458 int last = 0, ncandidates = 0,
459 initial_candidates = 0, candidate_match = 0;
464 SlapReply *candidates = meta_back_candidates_get( op );
467 * controls are set in ldap_back_dobind()
469 * FIXME: in case of values return filter, we might want
470 * to map attrs and maybe rewrite value
472 mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_SENDERR );
477 dc.conn = op->o_conn;
483 for ( i = 0; i < mi->mi_ntargets; i++ ) {
484 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
486 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
490 candidates[ i ].sr_matched = NULL;
491 candidates[ i ].sr_text = NULL;
492 candidates[ i ].sr_ref = NULL;
493 candidates[ i ].sr_ctrls = NULL;
496 for ( i = 0; i < mi->mi_ntargets; i++ ) {
497 if ( candidates[ i ].sr_tag != META_CANDIDATE
498 || candidates[ i ].sr_err != LDAP_SUCCESS )
503 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
505 case META_SEARCH_NOT_CANDIDATE:
508 case META_SEARCH_CANDIDATE:
509 case META_SEARCH_BINDING:
510 case META_SEARCH_NEED_BIND:
511 candidates[ i ].sr_type = REP_INTERMEDIATE;
515 case META_SEARCH_ERR:
516 savepriv = op->o_private;
517 op->o_private = (void *)i;
518 send_ldap_result( op, rs );
519 op->o_private = savepriv;
525 initial_candidates = ncandidates;
532 for ( i = 0; i < mi->mi_ntargets; i++ ) {
533 if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
541 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: ncandidates=%d "
542 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
546 if ( initial_candidates == 0 ) {
547 /* NOTE: here we are not sending any matchedDN;
548 * this is intended, because if the back-meta
549 * is serving this search request, but no valid
550 * candidate could be looked up, it means that
551 * there is a hole in the mapping of the targets
552 * and thus no knowledge of any remote superior
554 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
555 "base=\"%s\" scope=%d: "
556 "no candidate could be selected\n",
557 op->o_log_prefix, op->o_req_dn.bv_val,
560 /* FIXME: we're sending the first error we encounter;
561 * maybe we should pick the worst... */
562 rc = LDAP_NO_SUCH_OBJECT;
563 for ( i = 0; i < mi->mi_ntargets; i++ ) {
564 if ( candidates[ i ].sr_tag == META_CANDIDATE
565 && candidates[ i ].sr_err != LDAP_SUCCESS )
567 rc = candidates[ i ].sr_err;
572 send_ldap_error( op, rs, rc, NULL );
577 /* We pull apart the ber result, stuff it into a slapd entry, and
578 * let send_search_entry stuff it back into ber format. Slow & ugly,
579 * but this is necessary for version matching, and for ACL processing.
582 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
583 stoptime = op->o_time + op->ors_tlimit;
587 * In case there are no candidates, no cycle takes place...
589 * FIXME: we might use a queue, to better balance the load
590 * among the candidates
592 for ( rc = 0; ncandidates > 0; ) {
593 int gotit = 0, doabandon = 0;
595 /* check time limit */
596 if ( op->ors_tlimit != SLAP_NO_LIMIT
597 && slap_get_time() > stoptime )
600 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
601 savepriv = op->o_private;
602 op->o_private = (void *)i;
603 send_ldap_result( op, rs );
604 op->o_private = savepriv;
608 for ( i = 0; i < mi->mi_ntargets; i++ ) {
609 metasingleconn_t *msc = &mc->mc_conns[ i ];
611 if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
615 if ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND ) {
616 /* initiate dobind */
617 switch ( meta_search_dobind_init( op, rs, &mc, i, candidates ) )
619 case META_SEARCH_BINDING:
620 case META_SEARCH_NEED_BIND:
623 case META_SEARCH_NOT_CANDIDATE:
625 * When no candidates are left,
626 * the outer cycle finishes
628 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
632 case META_SEARCH_ERR:
633 if ( META_BACK_ONERR_STOP( mi ) ) {
634 savepriv = op->o_private;
635 op->o_private = (void *)i;
636 send_ldap_result( op, rs );
637 op->o_private = savepriv;
642 case META_SEARCH_CANDIDATE:
643 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
645 case META_SEARCH_CANDIDATE:
648 /* means that failed but onerr == continue */
649 case META_SEARCH_NOT_CANDIDATE:
650 case META_SEARCH_ERR:
651 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
654 if ( META_BACK_ONERR_STOP( mi ) ) {
655 savepriv = op->o_private;
656 op->o_private = (void *)i;
657 send_ldap_result( op, rs );
658 op->o_private = savepriv;
677 /* check for abandon */
678 if ( op->o_abandon ) {
683 * FIXME: handle time limit as well?
684 * Note that target servers are likely
685 * to handle it, so at some time we'll
686 * get a LDAP_TIMELIMIT_EXCEEDED from
690 rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
691 LDAP_MSG_ONE, &tv, &res );
694 /* FIXME: res should not need to be freed */
695 assert( res == NULL );
699 } else if ( rc == -1 ) {
701 /* something REALLY bad happened! */
702 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
703 candidates[ i ].sr_type = REP_RESULT;
705 if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
706 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
708 case META_SEARCH_CANDIDATE:
711 /* means that failed but onerr == continue */
712 case META_SEARCH_NOT_CANDIDATE:
713 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
716 if ( META_BACK_ONERR_STOP( mi ) ) {
717 savepriv = op->o_private;
718 op->o_private = (void *)i;
719 send_ldap_result( op, rs );
720 op->o_private = savepriv;
725 case META_SEARCH_BINDING:
726 case META_SEARCH_NEED_BIND:
730 rc = rs->sr_err = LDAP_OTHER;
735 if ( META_BACK_ONERR_STOP( mi ) ) {
736 savepriv = op->o_private;
737 op->o_private = (void *)i;
738 send_ldap_result( op, rs );
739 op->o_private = savepriv;
745 * When no candidates are left,
746 * the outer cycle finishes
748 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
750 rs->sr_err = candidates[ i ].sr_err;
752 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
753 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
754 /* don't retry any more... */
755 candidates[ i ].sr_type = REP_RESULT;
760 e = ldap_first_entry( msc->msc_ld, res );
761 savepriv = op->o_private;
762 op->o_private = (void *)i;
763 rs->sr_err = meta_send_entry( op, rs, mc, i, e );
767 switch ( rs->sr_err ) {
768 case LDAP_SIZELIMIT_EXCEEDED:
769 savepriv = op->o_private;
770 op->o_private = (void *)i;
771 send_ldap_result( op, rs );
772 op->o_private = savepriv;
773 rs->sr_err = LDAP_SUCCESS;
776 case LDAP_UNAVAILABLE:
777 rs->sr_err = LDAP_OTHER;
780 op->o_private = savepriv;
782 /* don't wait any longer... */
789 * If scope is BASE, we need to jump out
790 * as soon as one entry is found; if
791 * the target pool is properly crafted,
792 * this should correspond to the sole
793 * entry that has the base DN
795 /* FIXME: this defeats the purpose of
796 * doing a search with scope == base and
797 * sizelimit = 1 to determine if a
798 * candidate is actually unique */
799 if ( op->ors_scope == LDAP_SCOPE_BASE
800 && rs->sr_nentries > 0 )
809 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
810 char **references = NULL;
813 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
814 /* don't retry any more... */
815 candidates[ i ].sr_type = REP_RESULT;
820 rc = ldap_parse_reference( msc->msc_ld, res,
821 &references, &rs->sr_ctrls, 1 );
824 if ( rc != LDAP_SUCCESS ) {
828 if ( references == NULL ) {
832 #ifdef ENABLE_REWRITE
833 dc.ctx = "referralDN";
834 #else /* ! ENABLE_REWRITE */
837 #endif /* ! ENABLE_REWRITE */
839 /* FIXME: merge all and return at the end */
841 for ( cnt = 0; references[ cnt ]; cnt++ )
844 rs->sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
846 for ( cnt = 0; references[ cnt ]; cnt++ ) {
847 ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
849 BER_BVZERO( &rs->sr_ref[ cnt ] );
851 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref );
853 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
854 /* ignore return value by now */
855 savepriv = op->o_private;
856 op->o_private = (void *)i;
857 ( void )send_search_reference( op, rs );
858 op->o_private = savepriv;
860 ber_bvarray_free( rs->sr_ref );
866 ber_memvfree( (void **)references );
869 if ( rs->sr_ctrls ) {
870 ldap_controls_free( rs->sr_ctrls );
874 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
875 char buf[ SLAP_TEXT_BUFLEN ];
876 char **references = NULL;
878 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
879 /* don't retry any more... */
880 candidates[ i ].sr_type = REP_RESULT;
883 /* NOTE: ignores response controls
884 * (and intermediate response controls
885 * as well, except for those with search
886 * references); this may not be correct,
887 * but if they're not ignored then
888 * back-meta would need to merge them
889 * consistently (think of pagedResults...)
891 rs->sr_err = ldap_parse_result( msc->msc_ld,
893 &candidates[ i ].sr_err,
894 (char **)&candidates[ i ].sr_matched,
895 NULL /* (char **)&candidates[ i ].sr_text */ ,
897 NULL /* &candidates[ i ].sr_ctrls (unused) */ ,
900 if ( rs->sr_err != LDAP_SUCCESS ) {
901 ldap_get_option( msc->msc_ld,
902 LDAP_OPT_ERROR_NUMBER,
904 sres = slap_map_api2result( rs );
905 candidates[ i ].sr_type = REP_RESULT;
909 /* massage matchedDN if need be */
910 if ( candidates[ i ].sr_matched != NULL ) {
911 struct berval match, mmatch;
913 ber_str2bv( candidates[ i ].sr_matched,
915 candidates[ i ].sr_matched = NULL;
917 dc.ctx = "matchedDN";
918 dc.target = mi->mi_targets[ i ];
919 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
920 if ( mmatch.bv_val == match.bv_val ) {
921 candidates[ i ].sr_matched = ch_strdup( mmatch.bv_val );
924 candidates[ i ].sr_matched = mmatch.bv_val;
929 ldap_memfree( match.bv_val );
932 /* add references to array */
937 for ( cnt = 0; references[ cnt ]; cnt++ )
940 sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
942 for ( cnt = 0; references[ cnt ]; cnt++ ) {
943 ber_str2bv( references[ cnt ], 0, 1, &sr_ref[ cnt ] );
945 BER_BVZERO( &sr_ref[ cnt ] );
947 ( void )ldap_back_referral_result_rewrite( &dc, sr_ref );
950 ber_memvfree( (void **)references );
952 if ( rs->sr_v2ref == NULL ) {
953 rs->sr_v2ref = sr_ref;
956 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
957 ber_bvarray_add( &rs->sr_v2ref, &sr_ref[ cnt ] );
959 ber_memfree( sr_ref );
963 rs->sr_err = candidates[ i ].sr_err;
964 sres = slap_map_api2result( rs );
966 if ( LogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
967 snprintf( buf, sizeof( buf ),
968 "%s meta_back_search[%ld] "
969 "match=\"%s\" err=%ld",
971 candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
972 (long) candidates[ i ].sr_err );
973 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
974 Debug( LDAP_DEBUG_TRACE, "%s.\n", buf, 0, 0 );
977 Debug( LDAP_DEBUG_ANY, "%s (%s).\n",
978 buf, ldap_err2string( candidates[ i ].sr_err ), 0 );
983 case LDAP_NO_SUCH_OBJECT:
984 /* is_ok is touched any time a valid
985 * (even intermediate) result is
986 * returned; as a consequence, if
987 * a candidate returns noSuchObject
988 * it is ignored and the candidate
989 * is simply demoted. */
1000 case LDAP_SIZELIMIT_EXCEEDED:
1001 /* if a target returned sizelimitExceeded
1002 * and the entry count is equal to the
1003 * proxy's limit, the target would have
1004 * returned more, and the error must be
1005 * propagated to the client; otherwise,
1006 * the target enforced a limit lower
1007 * than what requested by the proxy;
1009 if ( rs->sr_nentries == op->ors_slimit
1010 || META_BACK_ONERR_STOP( mi ) )
1012 savepriv = op->o_private;
1013 op->o_private = (void *)i;
1014 send_ldap_result( op, rs );
1015 op->o_private = savepriv;
1021 if ( META_BACK_ONERR_STOP( mi ) ) {
1022 savepriv = op->o_private;
1023 op->o_private = (void *)i;
1024 send_ldap_result( op, rs );
1025 op->o_private = savepriv;
1035 * When no candidates are left,
1036 * the outer cycle finishes
1038 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1041 } else if ( rc == LDAP_RES_BIND ) {
1042 meta_search_candidate_t retcode;
1044 retcode = meta_search_dobind_result( op, rs, &mc, i, candidates, res );
1045 if ( retcode == META_SEARCH_CANDIDATE ) {
1046 retcode = meta_back_search_start( op, rs, &dc, &mc, i, candidates );
1049 switch ( retcode ) {
1050 case META_SEARCH_CANDIDATE:
1053 /* means that failed but onerr == continue */
1054 case META_SEARCH_NOT_CANDIDATE:
1055 case META_SEARCH_ERR:
1056 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
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;
1079 /* check for abandon */
1080 if ( op->o_abandon || doabandon ) {
1081 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1082 if ( candidates[ i ].sr_msgid != META_MSGID_IGNORE )
1084 (void)meta_back_cancel( mc, op, rs,
1085 candidates[ i ].sr_msgid, i,
1086 LDAP_BACK_DONTSEND );
1087 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1091 if ( op->o_abandon ) {
1097 /* if no entry was found during this loop,
1098 * set a minimal timeout */
1100 /* make the entire wait last
1101 * LDAP_BACK_RESULT_UTIMEOUT at worst */
1103 tv.tv_usec = LDAP_BACK_RESULT_UTIMEOUT/initial_candidates;
1104 ldap_pvt_thread_yield();
1110 * FIXME: need a better strategy to handle errors
1113 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE );
1121 * Rewrite the matched portion of the search base, if required
1123 * FIXME: only the last one gets caught!
1125 savepriv = op->o_private;
1126 op->o_private = (void *)(long)mi->mi_ntargets;
1127 if ( candidate_match > 0 ) {
1128 struct berval pmatched = BER_BVNULL;
1130 /* we use the first one */
1131 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1132 if ( candidates[ i ].sr_tag == META_CANDIDATE
1133 && candidates[ i ].sr_matched != NULL )
1135 struct berval bv, pbv;
1138 /* if we got success, and this target
1139 * returned noSuchObject, and its suffix
1140 * is a superior of the searchBase,
1141 * ignore the matchedDN */
1142 if ( sres == LDAP_SUCCESS
1143 && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
1144 && op->o_req_ndn.bv_len > mi->mi_targets[ i ]->mt_nsuffix.bv_len )
1146 free( (char *)candidates[ i ].sr_matched );
1147 candidates[ i ].sr_matched = NULL;
1151 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
1152 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
1154 if ( rc == LDAP_SUCCESS ) {
1156 /* NOTE: if they all are superiors
1157 * of the baseDN, the shorter is also
1158 * superior of the longer... */
1159 if ( pbv.bv_len > pmatched.bv_len ) {
1160 if ( !BER_BVISNULL( &pmatched ) ) {
1161 op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
1164 op->o_private = (void *)i;
1167 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
1171 if ( candidates[ i ].sr_matched != NULL ) {
1172 free( (char *)candidates[ i ].sr_matched );
1173 candidates[ i ].sr_matched = NULL;
1178 if ( !BER_BVISNULL( &pmatched ) ) {
1179 matched = pmatched.bv_val;
1182 } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
1183 matched = op->o_bd->be_suffix[ 0 ].bv_val;
1192 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1193 if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
1201 snprintf( buf, sizeof( buf ), "%s meta_back_search: is_scope=%d is_ok=%d cnd=\"%s\"\n",
1202 op->o_log_prefix, initial_candidates, is_ok, cnd );
1204 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1209 * In case we returned at least one entry, we return LDAP_SUCCESS
1210 * otherwise, the latter error code we got
1212 * FIXME: we should handle error codes and return the more
1213 * important/reasonable
1216 if ( sres == LDAP_SUCCESS && rs->sr_v2ref ) {
1217 sres = LDAP_REFERRAL;
1220 rs->sr_matched = matched;
1221 rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
1222 send_ldap_result( op, rs );
1223 op->o_private = savepriv;
1224 rs->sr_matched = NULL;
1228 if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
1229 op->o_tmpfree( matched, op->o_tmpmemctx );
1232 if ( rs->sr_v2ref ) {
1233 ber_bvarray_free( rs->sr_v2ref );
1236 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1237 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
1241 if ( mc && candidates[ i ].sr_msgid >= 0 ) {
1242 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1243 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ) {
1244 LDAP_BACK_CONN_BINDING_CLEAR( &mc->mc_conns[ i ] );
1246 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1249 if ( candidates[ i ].sr_matched ) {
1250 free( (char *)candidates[ i ].sr_matched );
1251 candidates[ i ].sr_matched = NULL;
1254 if ( candidates[ i ].sr_text ) {
1255 ldap_memfree( (char *)candidates[ i ].sr_text );
1256 candidates[ i ].sr_text = NULL;
1259 if ( candidates[ i ].sr_ref ) {
1260 ber_bvarray_free( candidates[ i ].sr_ref );
1261 candidates[ i ].sr_ref = NULL;
1264 if ( candidates[ i ].sr_ctrls ) {
1265 ldap_controls_free( candidates[ i ].sr_ctrls );
1266 candidates[ i ].sr_ctrls = NULL;
1269 if ( META_BACK_QUARANTINE( mi ) ) {
1270 meta_back_quarantine( op, &candidates[ i ], i, 1 );
1275 meta_back_release_conn( op, mc );
1289 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
1290 struct berval a, mapped;
1292 BerElement ber = *e->lm_ber;
1293 Attribute *attr, **attrp;
1300 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
1301 return LDAP_DECODING_ERROR;
1305 * Rewrite the dn of the result, if needed
1307 dc.target = mi->mi_targets[ target ];
1308 dc.conn = op->o_conn;
1310 dc.ctx = "searchResult";
1312 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
1313 if ( rs->sr_err != LDAP_SUCCESS) {
1318 * Note: this may fail if the target host(s) schema differs
1319 * from the one known to the meta, and a DN with unknown
1320 * attributes is returned.
1322 * FIXME: should we log anything, or delegate to dnNormalize?
1324 rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
1326 if ( dn.bv_val != bdn.bv_val ) {
1331 if ( rc != LDAP_SUCCESS ) {
1332 return LDAP_INVALID_DN_SYNTAX;
1338 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1339 ( void )meta_dncache_update_entry( &mi->mi_cache,
1340 &ent.e_nname, target );
1343 attrp = &ent.e_attrs;
1345 dc.ctx = "searchAttrDN";
1346 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
1348 slap_syntax_validate_func *validate;
1349 slap_syntax_transform_func *pretty;
1351 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_at,
1352 &a, &mapped, BACKLDAP_REMAP );
1353 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
1354 ( void )ber_scanf( &ber, "x" /* [W] */ );
1357 attr = ( Attribute * )ch_calloc( 1, sizeof( Attribute ) );
1358 if ( attr == NULL ) {
1361 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
1363 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
1364 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
1366 char buf[ SLAP_TEXT_BUFLEN ];
1368 snprintf( buf, sizeof( buf ),
1369 "%s meta_send_entry(\"%s\"): "
1370 "slap_bv2undef_ad(%s): %s\n",
1371 op->o_log_prefix, ent.e_name.bv_val,
1372 mapped.bv_val, text );
1374 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1380 /* no subschemaSubentry */
1381 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
1382 || attr->a_desc == slap_schema.si_ad_entryDN )
1386 * We eat target's subschemaSubentry because
1387 * a search for this value is likely not
1388 * to resolve to the appropriate backend;
1389 * later, the local subschemaSubentry is
1392 * We also eat entryDN because the frontend
1393 * will reattach it without checking if already
1396 ( void )ber_scanf( &ber, "x" /* [W] */ );
1402 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
1403 || attr->a_vals == NULL )
1405 attr->a_vals = (struct berval *)&slap_dummy_bv;
1408 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
1412 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
1413 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
1415 if ( !validate && !pretty ) {
1420 if ( attr->a_desc == slap_schema.si_ad_objectClass
1421 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
1425 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1426 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_oc,
1427 bv, &mapped, BACKLDAP_REMAP );
1428 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
1434 *bv = attr->a_vals[ last ];
1435 BER_BVZERO( &attr->a_vals[ last ] );
1438 } else if ( mapped.bv_val != bv->bv_val ) {
1440 ber_dupbv( bv, &mapped );
1444 * It is necessary to try to rewrite attributes with
1445 * dn syntax because they might be used in ACLs as
1446 * members of groups; since ACLs are applied to the
1447 * rewritten stuff, no dn-based subecj clause could
1448 * be used at the ldap backend side (see
1449 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1450 * The problem can be overcome by moving the dn-based
1451 * ACLs to the target directory server, and letting
1452 * everything pass thru the ldap backend.
1457 if ( attr->a_desc->ad_type->sat_syntax ==
1458 slap_schema.si_syn_distinguishedName )
1460 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
1462 } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
1463 ldap_back_referral_result_rewrite( &dc, attr->a_vals );
1467 for ( i = 0; i < last; i++ ) {
1472 rc = pretty( attr->a_desc->ad_type->sat_syntax,
1473 &attr->a_vals[i], &pval, NULL );
1476 rc = validate( attr->a_desc->ad_type->sat_syntax,
1481 LBER_FREE( attr->a_vals[i].bv_val );
1482 if ( --last == i ) {
1483 BER_BVZERO( &attr->a_vals[ i ] );
1486 attr->a_vals[i] = attr->a_vals[last];
1487 BER_BVZERO( &attr->a_vals[last] );
1493 LBER_FREE( attr->a_vals[i].bv_val );
1494 attr->a_vals[i] = pval;
1498 if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
1504 if ( last && attr->a_desc->ad_type->sat_equality &&
1505 attr->a_desc->ad_type->sat_equality->smr_normalize )
1509 attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
1510 for ( i = 0; i<last; i++ ) {
1511 attr->a_desc->ad_type->sat_equality->smr_normalize(
1512 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1513 attr->a_desc->ad_type->sat_syntax,
1514 attr->a_desc->ad_type->sat_equality,
1515 &attr->a_vals[i], &attr->a_nvals[i],
1518 BER_BVZERO( &attr->a_nvals[i] );
1521 attr->a_nvals = attr->a_vals;
1525 attrp = &attr->a_next;
1528 rs->sr_entry = &ent;
1529 rs->sr_attrs = op->ors_attrs;
1531 rs->sr_err = LDAP_SUCCESS;
1532 rc = send_search_entry( op, rs );
1534 case LDAP_UNAVAILABLE:
1538 rs->sr_entry = NULL;
1539 rs->sr_attrs = NULL;
1541 if ( !BER_BVISNULL( &ent.e_name ) ) {
1542 free( ent.e_name.bv_val );
1543 BER_BVZERO( &ent.e_name );
1545 if ( !BER_BVISNULL( &ent.e_nname ) ) {
1546 free( ent.e_nname.bv_val );
1547 BER_BVZERO( &ent.e_nname );
1549 entry_clean( &ent );