2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2007 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
27 #include <ac/socket.h>
28 #include <ac/string.h>
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"
46 typedef enum meta_search_candidate_t {
48 META_SEARCH_NOT_CANDIDATE,
50 } meta_search_candidate_t;
52 static meta_search_candidate_t
53 meta_back_search_start(
57 metasingleconn_t *msc,
62 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
63 struct berval realbase = op->o_req_dn;
64 int realscope = op->ors_scope;
65 ber_len_t suffixlen = 0;
66 struct berval mbase = BER_BVNULL;
67 struct berval mfilter = BER_BVNULL;
68 char **mapped_attrs = NULL;
70 meta_search_candidate_t retcode;
71 struct timeval tv, *tvp = NULL;
73 /* should we check return values? */
74 if ( op->ors_deref != -1 ) {
75 ldap_set_option( msc->msc_ld, LDAP_OPT_DEREF,
76 ( void * )&op->ors_deref );
79 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
80 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
84 dc->target = &mi->mi_targets[ candidate ];
87 * modifies the base according to the scope, if required
89 suffixlen = mi->mi_targets[ candidate ].mt_nsuffix.bv_len;
90 if ( suffixlen > op->o_req_ndn.bv_len ) {
91 switch ( op->ors_scope ) {
92 case LDAP_SCOPE_SUBTREE:
94 * make the target suffix the new base
95 * FIXME: this is very forgiving, because
96 * "illegal" searchBases may be turned
97 * into the suffix of the target; however,
98 * the requested searchBase already passed
99 * thru the candidate analyzer...
101 if ( dnIsSuffix( &mi->mi_targets[ candidate ].mt_nsuffix,
104 realbase = mi->mi_targets[ candidate ].mt_nsuffix;
105 if ( mi->mi_targets[ candidate ].mt_scope == LDAP_SCOPE_SUBORDINATE ) {
106 realscope = LDAP_SCOPE_SUBORDINATE;
111 * this target is no longer candidate
113 return META_SEARCH_NOT_CANDIDATE;
117 case LDAP_SCOPE_SUBORDINATE:
118 case LDAP_SCOPE_ONELEVEL:
120 struct berval rdn = mi->mi_targets[ candidate ].mt_nsuffix;
121 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
122 if ( dnIsOneLevelRDN( &rdn )
123 && dnIsSuffix( &mi->mi_targets[ candidate ].mt_nsuffix, &op->o_req_ndn ) )
126 * if there is exactly one level,
127 * make the target suffix the new
128 * base, and make scope "base"
130 realbase = mi->mi_targets[ candidate ].mt_nsuffix;
131 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
132 if ( mi->mi_targets[ candidate ].mt_scope == LDAP_SCOPE_SUBORDINATE ) {
133 realscope = LDAP_SCOPE_SUBORDINATE;
135 realscope = LDAP_SCOPE_SUBTREE;
138 realscope = LDAP_SCOPE_BASE;
141 } /* else continue with the next case */
144 case LDAP_SCOPE_BASE:
146 * this target is no longer candidate
148 return META_SEARCH_NOT_CANDIDATE;
153 * Rewrite the search base, if required
155 dc->ctx = "searchBase";
156 switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
160 case LDAP_UNWILLING_TO_PERFORM:
161 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
162 rs->sr_text = "Operation not allowed";
163 send_ldap_result( op, rs );
164 return META_SEARCH_ERR;
169 * this target is no longer candidate
171 return META_SEARCH_NOT_CANDIDATE;
177 rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
178 &mfilter, BACKLDAP_MAP );
183 case LDAP_COMPARE_FALSE:
186 * this target is no longer candidate
188 retcode = META_SEARCH_NOT_CANDIDATE;
193 * Maps required attributes
195 rc = ldap_back_map_attrs( &mi->mi_targets[ candidate ].mt_rwmap.rwm_at,
196 op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
197 if ( rc != LDAP_SUCCESS ) {
199 * this target is no longer candidate
201 retcode = META_SEARCH_NOT_CANDIDATE;
208 rc = ldap_search_ext( msc->msc_ld,
209 mbase.bv_val, realscope, mfilter.bv_val,
210 mapped_attrs, op->ors_attrsonly,
211 op->o_ctrls, NULL, tvp, op->ors_slimit,
212 &candidates[ candidate ].sr_msgid );
213 if ( rc == LDAP_SUCCESS ) {
214 retcode = META_SEARCH_CANDIDATE;
217 candidates[ candidate ].sr_msgid = -1;
218 retcode = META_SEARCH_NOT_CANDIDATE;
222 if ( mapped_attrs ) {
223 free( mapped_attrs );
225 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
226 free( mfilter.bv_val );
228 if ( mbase.bv_val != realbase.bv_val ) {
229 free( mbase.bv_val );
236 meta_back_search( Operation *op, SlapReply *rs )
238 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
240 struct timeval tv = { 0, 0 };
241 time_t stoptime = (time_t)-1;
242 LDAPMessage *res = NULL, *e;
243 int rc = 0, sres = LDAP_SUCCESS;
244 char *matched = NULL;
245 int last = 0, ncandidates = 0,
246 initial_candidates = 0, candidate_match = 0;
251 SlapReply *candidates = meta_back_candidates_get( op );
254 * controls are set in ldap_back_dobind()
256 * FIXME: in case of values return filter, we might want
257 * to map attrs and maybe rewrite value
259 mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_SENDERR );
260 if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
264 dc.conn = op->o_conn;
270 for ( i = 0; i < mi->mi_ntargets; i++ ) {
271 metasingleconn_t *msc = &mc->mc_conns[ i ];
273 candidates[ i ].sr_msgid = -1;
274 candidates[ i ].sr_matched = NULL;
275 candidates[ i ].sr_text = NULL;
276 candidates[ i ].sr_ref = NULL;
277 candidates[ i ].sr_ctrls = NULL;
279 if ( candidates[ i ].sr_tag != META_CANDIDATE
280 || candidates[ i ].sr_err != LDAP_SUCCESS )
285 switch ( meta_back_search_start( op, rs, &dc, msc, i, candidates ) )
287 case META_SEARCH_NOT_CANDIDATE:
290 case META_SEARCH_CANDIDATE:
291 candidates[ i ].sr_type = REP_INTERMEDIATE;
295 case META_SEARCH_ERR:
301 initial_candidates = ncandidates;
308 for ( i = 0; i < mi->mi_ntargets; i++ ) {
309 if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
317 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: ncandidates=%d "
318 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
322 if ( initial_candidates == 0 ) {
323 /* NOTE: here we are not sending any matchedDN;
324 * this is intended, because if the back-meta
325 * is serving this search request, but no valid
326 * candidate could be looked up, it means that
327 * there is a hole in the mapping of the targets
328 * and thus no knowledge of any remote superior
330 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
331 "base=\"%s\" scope=%d: "
332 "no candidate could be selected\n",
333 op->o_log_prefix, op->o_req_dn.bv_val,
336 /* FIXME: we're sending the first error we encounter;
337 * maybe we should pick the worst... */
338 rc = LDAP_NO_SUCH_OBJECT;
339 for ( i = 0; i < mi->mi_ntargets; i++ ) {
340 if ( candidates[ i ].sr_tag == META_CANDIDATE
341 && candidates[ i ].sr_err != LDAP_SUCCESS )
343 rc = candidates[ i ].sr_err;
348 send_ldap_error( op, rs, rc, NULL );
353 /* We pull apart the ber result, stuff it into a slapd entry, and
354 * let send_search_entry stuff it back into ber format. Slow & ugly,
355 * but this is necessary for version matching, and for ACL processing.
358 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
359 stoptime = op->o_time + op->ors_tlimit;
363 * In case there are no candidates, no cycle takes place...
365 * FIXME: we might use a queue, to better balance the load
366 * among the candidates
368 for ( rc = 0; ncandidates > 0; ) {
369 int gotit = 0, doabandon = 0;
371 for ( i = 0; i < mi->mi_ntargets; i++ ) {
372 metasingleconn_t *msc = &mc->mc_conns[ i ];
374 if ( candidates[ i ].sr_msgid == -1 ) {
378 /* check for abandon */
379 if ( op->o_abandon ) {
384 * FIXME: handle time limit as well?
385 * Note that target servers are likely
386 * to handle it, so at some time we'll
387 * get a LDAP_TIMELIMIT_EXCEEDED from
391 rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
392 LDAP_MSG_ONE, &tv, &res );
395 /* FIXME: res should not need to be freed */
396 assert( res == NULL );
398 /* check time limit */
399 if ( op->ors_tlimit != SLAP_NO_LIMIT
400 && slap_get_time() > stoptime )
403 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
404 savepriv = op->o_private;
405 op->o_private = (void *)i;
406 send_ldap_result( op, rs );
407 op->o_private = savepriv;
413 } else if ( rc == -1 ) {
415 /* something REALLY bad happened! */
416 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
417 candidates[ i ].sr_type = REP_RESULT;
419 if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
420 switch ( meta_back_search_start( op, rs, &dc, msc, i, candidates ) )
422 case META_SEARCH_CANDIDATE:
426 rc = rs->sr_err = LDAP_OTHER;
431 savepriv = op->o_private;
432 op->o_private = (void *)i;
433 send_ldap_result( op, rs );
434 op->o_private = savepriv;
439 * When no candidates are left,
440 * the outer cycle finishes
442 candidates[ i ].sr_msgid = -1;
444 rs->sr_err = candidates[ i ].sr_err = LDAP_OTHER;
445 rs->sr_text = "remote server unavailable";
447 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
448 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
449 /* don't retry any more... */
450 candidates[ i ].sr_type = REP_RESULT;
455 e = ldap_first_entry( msc->msc_ld, res );
456 savepriv = op->o_private;
457 op->o_private = (void *)i;
458 rs->sr_err = meta_send_entry( op, rs, mc, i, e );
462 switch ( rs->sr_err ) {
463 case LDAP_SIZELIMIT_EXCEEDED:
464 savepriv = op->o_private;
465 op->o_private = (void *)i;
466 send_ldap_result( op, rs );
467 op->o_private = savepriv;
468 rs->sr_err = LDAP_SUCCESS;
471 case LDAP_UNAVAILABLE:
472 rs->sr_err = LDAP_OTHER;
475 op->o_private = savepriv;
477 /* don't wait any longer... */
484 * If scope is BASE, we need to jump out
485 * as soon as one entry is found; if
486 * the target pool is properly crafted,
487 * this should correspond to the sole
488 * entry that has the base DN
490 /* FIXME: this defeats the purpose of
491 * doing a search with scope == base and
492 * sizelimit = 1 to determine if a
493 * candidate is actually unique */
494 if ( op->ors_scope == LDAP_SCOPE_BASE
495 && rs->sr_nentries > 0 )
504 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
505 char **references = NULL;
508 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
509 /* don't retry any more... */
510 candidates[ i ].sr_type = REP_RESULT;
515 rc = ldap_parse_reference( msc->msc_ld, res,
516 &references, &rs->sr_ctrls, 1 );
519 if ( rc != LDAP_SUCCESS ) {
523 if ( references == NULL ) {
527 #ifdef ENABLE_REWRITE
528 dc.ctx = "referralDN";
529 #else /* ! ENABLE_REWRITE */
532 #endif /* ! ENABLE_REWRITE */
534 /* FIXME: merge all and return at the end */
536 for ( cnt = 0; references[ cnt ]; cnt++ )
539 rs->sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
541 for ( cnt = 0; references[ cnt ]; cnt++ ) {
542 ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
544 BER_BVZERO( &rs->sr_ref[ cnt ] );
546 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref );
548 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
549 /* ignore return value by now */
550 savepriv = op->o_private;
551 op->o_private = (void *)i;
552 ( void )send_search_reference( op, rs );
553 op->o_private = savepriv;
555 ber_bvarray_free( rs->sr_ref );
561 ber_memvfree( (void **)references );
564 if ( rs->sr_ctrls ) {
565 ldap_controls_free( rs->sr_ctrls );
569 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
570 char buf[ SLAP_TEXT_BUFLEN ];
571 char **references = NULL;
573 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
574 /* don't retry any more... */
575 candidates[ i ].sr_type = REP_RESULT;
578 /* NOTE: ignores response controls
579 * (and intermediate response controls
580 * as well, except for those with search
581 * references); this may not be correct,
582 * but if they're not ignored then
583 * back-meta would need to merge them
584 * consistently (think of pagedResults...)
586 rs->sr_err = ldap_parse_result( msc->msc_ld,
588 &candidates[ i ].sr_err,
589 (char **)&candidates[ i ].sr_matched,
590 NULL /* (char **)&candidates[ i ].sr_text */ ,
592 NULL /* &candidates[ i ].sr_ctrls (unused) */ ,
595 if ( rs->sr_err != LDAP_SUCCESS ) {
596 ldap_get_option( msc->msc_ld,
597 LDAP_OPT_ERROR_NUMBER,
599 sres = slap_map_api2result( rs );
600 candidates[ i ].sr_type = REP_RESULT;
604 /* massage matchedDN if need be */
605 if ( candidates[ i ].sr_matched != NULL ) {
606 #ifndef LDAP_NULL_IS_NULL
607 if ( candidates[ i ].sr_matched[ 0 ] == '\0' ) {
608 ldap_memfree( (char *)candidates[ i ].sr_matched );
609 candidates[ i ].sr_matched = NULL;
612 #endif /* LDAP_NULL_IS_NULL */
614 struct berval match, mmatch;
616 ber_str2bv( candidates[ i ].sr_matched,
618 candidates[ i ].sr_matched = NULL;
620 dc.ctx = "matchedDN";
621 dc.target = &mi->mi_targets[ i ];
622 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
623 if ( mmatch.bv_val == match.bv_val ) {
624 candidates[ i ].sr_matched = ch_strdup( mmatch.bv_val );
627 candidates[ i ].sr_matched = mmatch.bv_val;
632 ldap_memfree( match.bv_val );
636 #ifndef LDAP_NULL_IS_NULL
637 /* just get rid of the error message, if any */
638 if ( candidates[ i ].sr_text && candidates[ i ].sr_text[ 0 ] == '\0' )
640 ldap_memfree( (char *)candidates[ i ].sr_text );
641 candidates[ i ].sr_text = NULL;
643 #endif /* LDAP_NULL_IS_NULL */
645 /* add references to array */
650 for ( cnt = 0; references[ cnt ]; cnt++ )
653 sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
655 for ( cnt = 0; references[ cnt ]; cnt++ ) {
656 ber_str2bv( references[ cnt ], 0, 1, &sr_ref[ cnt ] );
658 BER_BVZERO( &sr_ref[ cnt ] );
660 ( void )ldap_back_referral_result_rewrite( &dc, sr_ref );
663 ber_memvfree( (void **)references );
665 if ( rs->sr_v2ref == NULL ) {
666 rs->sr_v2ref = sr_ref;
669 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
670 ber_bvarray_add( &rs->sr_v2ref, &sr_ref[ cnt ] );
672 ber_memfree( sr_ref );
676 rs->sr_err = candidates[ i ].sr_err;
677 sres = slap_map_api2result( rs );
679 if ( StatslogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
680 snprintf( buf, sizeof( buf ),
681 "%s meta_back_search[%ld] "
682 "match=\"%s\" err=%ld",
684 candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
685 (long) candidates[ i ].sr_err );
686 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
687 Debug( LDAP_DEBUG_TRACE, "%s.\n", buf, 0, 0 );
690 Debug( LDAP_DEBUG_ANY, "%s (%s).\n",
691 buf, ldap_err2string( candidates[ i ].sr_err ), 0 );
696 case LDAP_NO_SUCH_OBJECT:
697 /* is_ok is touched any time a valid
698 * (even intermediate) result is
699 * returned; as a consequence, if
700 * a candidate returns noSuchObject
701 * it is ignored and the candidate
702 * is simply demoted. */
713 case LDAP_SIZELIMIT_EXCEEDED:
714 /* if a target returned sizelimitExceeded
715 * and the entry count is equal to the
716 * proxy's limit, the target would have
717 * returned more, and the error must be
718 * propagated to the client; otherwise,
719 * the target enforced a limit lower
720 * than what requested by the proxy;
722 if ( rs->sr_nentries == op->ors_slimit
723 || META_BACK_ONERR_STOP( mi ) )
725 savepriv = op->o_private;
726 op->o_private = (void *)i;
727 send_ldap_result( op, rs );
728 op->o_private = savepriv;
734 if ( META_BACK_ONERR_STOP( mi ) ) {
735 savepriv = op->o_private;
736 op->o_private = (void *)i;
737 send_ldap_result( op, rs );
738 op->o_private = savepriv;
748 * When no candidates are left,
749 * the outer cycle finishes
751 candidates[ i ].sr_msgid = -1;
760 /* check for abandon */
761 if ( op->o_abandon || doabandon ) {
762 for ( i = 0; i < mi->mi_ntargets; i++ ) {
763 metasingleconn_t *msc = &mc->mc_conns[ i ];
765 if ( candidates[ i ].sr_msgid != -1 ) {
766 ldap_abandon_ext( msc->msc_ld,
767 candidates[ i ].sr_msgid,
769 candidates[ i ].sr_msgid = -1;
773 if ( op->o_abandon ) {
779 /* if no entry was found during this loop,
780 * set a minimal timeout */
782 LDAP_BACK_TV_SET( &tv );
783 ldap_pvt_thread_yield();
789 * FIXME: need a better strategy to handle errors
791 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE );
796 * Rewrite the matched portion of the search base, if required
798 * FIXME: only the last one gets caught!
800 savepriv = op->o_private;
801 op->o_private = (void *)(long)mi->mi_ntargets;
802 if ( candidate_match > 0 ) {
803 struct berval pmatched = BER_BVNULL;
805 /* we use the first one */
806 for ( i = 0; i < mi->mi_ntargets; i++ ) {
807 if ( candidates[ i ].sr_tag == META_CANDIDATE
808 && candidates[ i ].sr_matched != NULL )
810 struct berval bv, pbv;
813 /* if we got success, and this target
814 * returned noSuchObject, and its suffix
815 * is a superior of the searchBase,
816 * ignore the matchedDN */
817 if ( sres == LDAP_SUCCESS
818 && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
819 && op->o_req_ndn.bv_len > mi->mi_targets[ i ].mt_nsuffix.bv_len )
821 free( (char *)candidates[ i ].sr_matched );
822 candidates[ i ].sr_matched = NULL;
826 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
827 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
829 if ( rc == LDAP_SUCCESS ) {
831 /* NOTE: if they all are superiors
832 * of the baseDN, the shorter is also
833 * superior of the longer... */
834 if ( pbv.bv_len > pmatched.bv_len ) {
835 if ( !BER_BVISNULL( &pmatched ) ) {
836 op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
839 op->o_private = (void *)i;
842 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
846 if ( candidates[ i ].sr_matched != NULL ) {
847 free( (char *)candidates[ i ].sr_matched );
848 candidates[ i ].sr_matched = NULL;
853 if ( !BER_BVISNULL( &pmatched ) ) {
854 matched = pmatched.bv_val;
857 } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
858 matched = op->o_bd->be_suffix[ 0 ].bv_val;
867 for ( i = 0; i < mi->mi_ntargets; i++ ) {
868 if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
876 snprintf( buf, sizeof( buf ), "%s meta_back_search: is_scope=%d is_ok=%d cnd=\"%s\"\n",
877 op->o_log_prefix, initial_candidates, is_ok, cnd );
879 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
884 * In case we returned at least one entry, we return LDAP_SUCCESS
885 * otherwise, the latter error code we got
887 * FIXME: we should handle error codes and return the more
888 * important/reasonable
891 if ( sres == LDAP_SUCCESS && rs->sr_v2ref ) {
892 sres = LDAP_REFERRAL;
895 rs->sr_matched = matched;
896 rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
897 send_ldap_result( op, rs );
898 op->o_private = savepriv;
899 rs->sr_matched = NULL;
903 if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
904 op->o_tmpfree( matched, op->o_tmpmemctx );
907 if ( rs->sr_v2ref ) {
908 ber_bvarray_free( rs->sr_v2ref );
911 for ( i = 0; i < mi->mi_ntargets; i++ ) {
912 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
916 if ( candidates[ i ].sr_matched ) {
917 free( (char *)candidates[ i ].sr_matched );
918 candidates[ i ].sr_matched = NULL;
921 if ( candidates[ i ].sr_text ) {
922 ldap_memfree( (char *)candidates[ i ].sr_text );
923 candidates[ i ].sr_text = NULL;
926 if ( candidates[ i ].sr_ref ) {
927 ber_bvarray_free( candidates[ i ].sr_ref );
928 candidates[ i ].sr_ref = NULL;
931 if ( candidates[ i ].sr_ctrls ) {
932 ldap_controls_free( candidates[ i ].sr_ctrls );
933 candidates[ i ].sr_ctrls = NULL;
938 meta_back_release_conn( op, mc );
952 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
953 struct berval a, mapped;
955 BerElement ber = *e->lm_ber;
956 Attribute *attr, **attrp;
963 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
964 return LDAP_DECODING_ERROR;
968 * Rewrite the dn of the result, if needed
970 dc.target = &mi->mi_targets[ target ];
971 dc.conn = op->o_conn;
973 dc.ctx = "searchResult";
975 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
976 if ( rs->sr_err != LDAP_SUCCESS) {
981 * Note: this may fail if the target host(s) schema differs
982 * from the one known to the meta, and a DN with unknown
983 * attributes is returned.
985 * FIXME: should we log anything, or delegate to dnNormalize?
987 rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
989 if ( dn.bv_val != bdn.bv_val ) {
994 if ( rc != LDAP_SUCCESS ) {
995 return LDAP_INVALID_DN_SYNTAX;
1001 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1002 ( void )meta_dncache_update_entry( &mi->mi_cache,
1003 &ent.e_nname, target );
1006 attrp = &ent.e_attrs;
1008 dc.ctx = "searchAttrDN";
1009 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
1011 slap_syntax_validate_func *validate;
1012 slap_syntax_transform_func *pretty;
1014 ldap_back_map( &mi->mi_targets[ target ].mt_rwmap.rwm_at,
1015 &a, &mapped, BACKLDAP_REMAP );
1016 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
1017 ( void )ber_scanf( &ber, "x" /* [W] */ );
1020 attr = ( Attribute * )ch_calloc( 1, sizeof( Attribute ) );
1021 if ( attr == NULL ) {
1024 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
1026 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
1027 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
1029 char buf[ SLAP_TEXT_BUFLEN ];
1031 snprintf( buf, sizeof( buf ),
1032 "%s meta_send_entry(\"%s\"): "
1033 "slap_bv2undef_ad(%s): %s\n",
1034 op->o_log_prefix, ent.e_name.bv_val,
1035 mapped.bv_val, text );
1037 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1043 /* no subschemaSubentry */
1044 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
1045 || attr->a_desc == slap_schema.si_ad_entryDN )
1049 * We eat target's subschemaSubentry because
1050 * a search for this value is likely not
1051 * to resolve to the appropriate backend;
1052 * later, the local subschemaSubentry is
1055 * We also eat entryDN because the frontend
1056 * will reattach it without checking if already
1059 ( void )ber_scanf( &ber, "x" /* [W] */ );
1065 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
1066 || attr->a_vals == NULL )
1068 attr->a_vals = (struct berval *)&slap_dummy_bv;
1071 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
1075 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
1076 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
1078 if ( !validate && !pretty ) {
1083 if ( attr->a_desc == slap_schema.si_ad_objectClass
1084 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
1088 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1089 ldap_back_map( &mi->mi_targets[ target ].mt_rwmap.rwm_oc,
1090 bv, &mapped, BACKLDAP_REMAP );
1091 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
1097 *bv = attr->a_vals[ last ];
1098 BER_BVZERO( &attr->a_vals[ last ] );
1101 } else if ( mapped.bv_val != bv->bv_val ) {
1103 ber_dupbv( bv, &mapped );
1107 * It is necessary to try to rewrite attributes with
1108 * dn syntax because they might be used in ACLs as
1109 * members of groups; since ACLs are applied to the
1110 * rewritten stuff, no dn-based subecj clause could
1111 * be used at the ldap backend side (see
1112 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1113 * The problem can be overcome by moving the dn-based
1114 * ACLs to the target directory server, and letting
1115 * everything pass thru the ldap backend.
1120 if ( attr->a_desc->ad_type->sat_syntax ==
1121 slap_schema.si_syn_distinguishedName )
1123 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
1125 } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
1126 ldap_back_referral_result_rewrite( &dc, attr->a_vals );
1130 for ( i = 0; i < last; i++ ) {
1135 rc = pretty( attr->a_desc->ad_type->sat_syntax,
1136 &attr->a_vals[i], &pval, NULL );
1139 rc = validate( attr->a_desc->ad_type->sat_syntax,
1144 LBER_FREE( attr->a_vals[i].bv_val );
1145 if ( --last == i ) {
1146 BER_BVZERO( &attr->a_vals[ i ] );
1149 attr->a_vals[i] = attr->a_vals[last];
1150 BER_BVZERO( &attr->a_vals[last] );
1156 LBER_FREE( attr->a_vals[i].bv_val );
1157 attr->a_vals[i] = pval;
1161 if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
1167 if ( last && attr->a_desc->ad_type->sat_equality &&
1168 attr->a_desc->ad_type->sat_equality->smr_normalize )
1172 attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
1173 for ( i = 0; i<last; i++ ) {
1174 attr->a_desc->ad_type->sat_equality->smr_normalize(
1175 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1176 attr->a_desc->ad_type->sat_syntax,
1177 attr->a_desc->ad_type->sat_equality,
1178 &attr->a_vals[i], &attr->a_nvals[i],
1181 BER_BVZERO( &attr->a_nvals[i] );
1184 attr->a_nvals = attr->a_vals;
1188 attrp = &attr->a_next;
1191 rs->sr_entry = &ent;
1192 rs->sr_attrs = op->ors_attrs;
1194 rs->sr_err = LDAP_SUCCESS;
1195 rc = send_search_entry( op, rs );
1197 case LDAP_UNAVAILABLE:
1201 rs->sr_entry = NULL;
1202 rs->sr_attrs = NULL;
1204 if ( !BER_BVISNULL( &ent.e_name ) ) {
1205 free( ent.e_name.bv_val );
1206 BER_BVZERO( &ent.e_name );
1208 if ( !BER_BVISNULL( &ent.e_nname ) ) {
1209 free( ent.e_nname.bv_val );
1210 BER_BVZERO( &ent.e_nname );
1212 entry_clean( &ent );