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)
52 typedef enum meta_search_candidate_t {
54 META_SEARCH_NOT_CANDIDATE,
56 } meta_search_candidate_t;
58 static meta_search_candidate_t
59 meta_back_search_start(
65 SlapReply *candidates )
67 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
68 metatarget_t *mt = &mi->mi_targets[ candidate ];
69 metaconn_t *mc = *mcp;
70 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
71 struct berval realbase = op->o_req_dn;
72 int realscope = op->ors_scope;
73 ber_len_t suffixlen = 0;
74 struct berval mbase = BER_BVNULL;
75 struct berval mfilter = BER_BVNULL;
76 char **mapped_attrs = NULL;
78 meta_search_candidate_t retcode;
79 struct timeval tv, *tvp = NULL;
82 /* should we check return values? */
83 if ( op->ors_deref != -1 ) {
84 ldap_set_option( msc->msc_ld, LDAP_OPT_DEREF,
85 ( void * )&op->ors_deref );
88 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
89 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
97 * modifies the base according to the scope, if required
99 suffixlen = mt->mt_nsuffix.bv_len;
100 if ( suffixlen > op->o_req_ndn.bv_len ) {
101 switch ( op->ors_scope ) {
102 case LDAP_SCOPE_SUBTREE:
104 * make the target suffix the new base
105 * FIXME: this is very forgiving, because
106 * "illegal" searchBases may be turned
107 * into the suffix of the target; however,
108 * the requested searchBase already passed
109 * thru the candidate analyzer...
111 if ( dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) ) {
112 realbase = mt->mt_nsuffix;
113 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
114 realscope = LDAP_SCOPE_SUBORDINATE;
119 * this target is no longer candidate
121 return META_SEARCH_NOT_CANDIDATE;
125 case LDAP_SCOPE_SUBORDINATE:
126 case LDAP_SCOPE_ONELEVEL:
128 struct berval rdn = mt->mt_nsuffix;
129 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
130 if ( dnIsOneLevelRDN( &rdn )
131 && dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) )
134 * if there is exactly one level,
135 * make the target suffix the new
136 * base, and make scope "base"
138 realbase = mt->mt_nsuffix;
139 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
140 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
141 realscope = LDAP_SCOPE_SUBORDINATE;
143 realscope = LDAP_SCOPE_SUBTREE;
146 realscope = LDAP_SCOPE_BASE;
149 } /* else continue with the next case */
152 case LDAP_SCOPE_BASE:
154 * this target is no longer candidate
156 return META_SEARCH_NOT_CANDIDATE;
161 * Rewrite the search base, if required
163 dc->ctx = "searchBase";
164 switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
168 case REWRITE_REGEXEC_UNWILLING:
169 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
170 rs->sr_text = "Operation not allowed";
171 send_ldap_result( op, rs );
172 return META_SEARCH_ERR;
174 case REWRITE_REGEXEC_ERR:
177 * this target is no longer candidate
179 return META_SEARCH_NOT_CANDIDATE;
185 rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
186 &mfilter, BACKLDAP_MAP );
191 case LDAP_COMPARE_FALSE:
194 * this target is no longer candidate
196 retcode = META_SEARCH_NOT_CANDIDATE;
201 * Maps required attributes
203 rc = ldap_back_map_attrs( &mt->mt_rwmap.rwm_at,
204 op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
205 if ( rc != LDAP_SUCCESS ) {
207 * this target is no longer candidate
209 retcode = META_SEARCH_NOT_CANDIDATE;
217 rc = ldap_search_ext( msc->msc_ld,
218 mbase.bv_val, realscope, mfilter.bv_val,
219 mapped_attrs, op->ors_attrsonly,
220 op->o_ctrls, NULL, tvp, op->ors_slimit,
221 &candidates[ candidate ].sr_msgid );
224 retcode = META_SEARCH_CANDIDATE;
227 case LDAP_SERVER_DOWN:
228 if ( nretries && meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
233 if ( *mcp == NULL ) {
234 retcode = META_SEARCH_ERR;
240 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
241 retcode = META_SEARCH_NOT_CANDIDATE;
245 if ( mapped_attrs ) {
246 free( mapped_attrs );
248 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
249 free( mfilter.bv_val );
251 if ( mbase.bv_val != realbase.bv_val ) {
252 free( mbase.bv_val );
259 meta_back_search( Operation *op, SlapReply *rs )
261 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
263 struct timeval tv = { 0, 0 };
264 time_t stoptime = (time_t)-1;
265 LDAPMessage *res = NULL, *e;
266 int rc = 0, sres = LDAP_SUCCESS;
267 char *matched = NULL;
268 int last = 0, ncandidates = 0,
269 initial_candidates = 0, candidate_match = 0;
274 SlapReply *candidates = meta_back_candidates_get( op );
277 * controls are set in ldap_back_dobind()
279 * FIXME: in case of values return filter, we might want
280 * to map attrs and maybe rewrite value
282 mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_SENDERR );
283 if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
287 dc.conn = op->o_conn;
293 for ( i = 0; i < mi->mi_ntargets; i++ ) {
294 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
295 candidates[ i ].sr_matched = NULL;
296 candidates[ i ].sr_text = NULL;
297 candidates[ i ].sr_ref = NULL;
298 candidates[ i ].sr_ctrls = NULL;
300 if ( candidates[ i ].sr_tag != META_CANDIDATE
301 || candidates[ i ].sr_err != LDAP_SUCCESS )
306 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
308 case META_SEARCH_NOT_CANDIDATE:
311 case META_SEARCH_CANDIDATE:
312 candidates[ i ].sr_type = REP_INTERMEDIATE;
316 case META_SEARCH_ERR:
322 initial_candidates = ncandidates;
329 for ( i = 0; i < mi->mi_ntargets; i++ ) {
330 if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
338 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: ncandidates=%d "
339 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
343 if ( initial_candidates == 0 ) {
344 /* NOTE: here we are not sending any matchedDN;
345 * this is intended, because if the back-meta
346 * is serving this search request, but no valid
347 * candidate could be looked up, it means that
348 * there is a hole in the mapping of the targets
349 * and thus no knowledge of any remote superior
351 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
352 "base=\"%s\" scope=%d: "
353 "no candidate could be selected\n",
354 op->o_log_prefix, op->o_req_dn.bv_val,
357 /* FIXME: we're sending the first error we encounter;
358 * maybe we should pick the worst... */
359 rc = LDAP_NO_SUCH_OBJECT;
360 for ( i = 0; i < mi->mi_ntargets; i++ ) {
361 if ( candidates[ i ].sr_tag == META_CANDIDATE
362 && candidates[ i ].sr_err != LDAP_SUCCESS )
364 rc = candidates[ i ].sr_err;
369 send_ldap_error( op, rs, rc, NULL );
374 /* We pull apart the ber result, stuff it into a slapd entry, and
375 * let send_search_entry stuff it back into ber format. Slow & ugly,
376 * but this is necessary for version matching, and for ACL processing.
379 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
380 stoptime = op->o_time + op->ors_tlimit;
384 * In case there are no candidates, no cycle takes place...
386 * FIXME: we might use a queue, to better balance the load
387 * among the candidates
389 for ( rc = 0; ncandidates > 0; ) {
390 int gotit = 0, doabandon = 0;
392 for ( i = 0; i < mi->mi_ntargets; i++ ) {
393 metasingleconn_t *msc = &mc->mc_conns[ i ];
395 if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
399 /* check for abandon */
400 if ( op->o_abandon ) {
405 * FIXME: handle time limit as well?
406 * Note that target servers are likely
407 * to handle it, so at some time we'll
408 * get a LDAP_TIMELIMIT_EXCEEDED from
412 rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
413 LDAP_MSG_ONE, &tv, &res );
416 /* FIXME: res should not need to be freed */
417 assert( res == NULL );
419 /* check time limit */
420 if ( op->ors_tlimit != SLAP_NO_LIMIT
421 && slap_get_time() > stoptime )
424 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
425 savepriv = op->o_private;
426 op->o_private = (void *)i;
427 send_ldap_result( op, rs );
428 op->o_private = savepriv;
434 } else if ( rc == -1 ) {
436 /* something REALLY bad happened! */
437 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
438 candidates[ i ].sr_type = REP_RESULT;
440 if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
441 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
443 case META_SEARCH_CANDIDATE:
446 case META_SEARCH_NOT_CANDIDATE:
450 rc = rs->sr_err = LDAP_OTHER;
455 if ( META_BACK_ONERR_STOP( mi ) ) {
456 savepriv = op->o_private;
457 op->o_private = (void *)i;
458 send_ldap_result( op, rs );
459 op->o_private = savepriv;
465 * When no candidates are left,
466 * the outer cycle finishes
468 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
470 rs->sr_err = candidates[ i ].sr_err = LDAP_OTHER;
471 rs->sr_text = "remote server unavailable";
473 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
474 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
475 /* don't retry any more... */
476 candidates[ i ].sr_type = REP_RESULT;
481 e = ldap_first_entry( msc->msc_ld, res );
482 savepriv = op->o_private;
483 op->o_private = (void *)i;
484 rs->sr_err = meta_send_entry( op, rs, mc, i, e );
488 switch ( rs->sr_err ) {
489 case LDAP_SIZELIMIT_EXCEEDED:
490 savepriv = op->o_private;
491 op->o_private = (void *)i;
492 send_ldap_result( op, rs );
493 op->o_private = savepriv;
494 rs->sr_err = LDAP_SUCCESS;
497 case LDAP_UNAVAILABLE:
498 rs->sr_err = LDAP_OTHER;
501 op->o_private = savepriv;
503 /* don't wait any longer... */
510 * If scope is BASE, we need to jump out
511 * as soon as one entry is found; if
512 * the target pool is properly crafted,
513 * this should correspond to the sole
514 * entry that has the base DN
516 /* FIXME: this defeats the purpose of
517 * doing a search with scope == base and
518 * sizelimit = 1 to determine if a
519 * candidate is actually unique */
520 if ( op->ors_scope == LDAP_SCOPE_BASE
521 && rs->sr_nentries > 0 )
530 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
531 char **references = NULL;
534 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
535 /* don't retry any more... */
536 candidates[ i ].sr_type = REP_RESULT;
541 rc = ldap_parse_reference( msc->msc_ld, res,
542 &references, &rs->sr_ctrls, 1 );
545 if ( rc != LDAP_SUCCESS ) {
549 if ( references == NULL ) {
553 #ifdef ENABLE_REWRITE
554 dc.ctx = "referralDN";
555 #else /* ! ENABLE_REWRITE */
558 #endif /* ! ENABLE_REWRITE */
560 /* FIXME: merge all and return at the end */
562 for ( cnt = 0; references[ cnt ]; cnt++ )
565 rs->sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
567 for ( cnt = 0; references[ cnt ]; cnt++ ) {
568 ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
570 BER_BVZERO( &rs->sr_ref[ cnt ] );
572 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref );
574 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
575 /* ignore return value by now */
576 savepriv = op->o_private;
577 op->o_private = (void *)i;
578 ( void )send_search_reference( op, rs );
579 op->o_private = savepriv;
581 ber_bvarray_free( rs->sr_ref );
587 ber_memvfree( (void **)references );
590 if ( rs->sr_ctrls ) {
591 ldap_controls_free( rs->sr_ctrls );
595 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
596 char buf[ SLAP_TEXT_BUFLEN ];
597 char **references = NULL;
599 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
600 /* don't retry any more... */
601 candidates[ i ].sr_type = REP_RESULT;
604 /* NOTE: ignores response controls
605 * (and intermediate response controls
606 * as well, except for those with search
607 * references); this may not be correct,
608 * but if they're not ignored then
609 * back-meta would need to merge them
610 * consistently (think of pagedResults...)
612 rs->sr_err = ldap_parse_result( msc->msc_ld,
614 &candidates[ i ].sr_err,
615 (char **)&candidates[ i ].sr_matched,
616 NULL /* (char **)&candidates[ i ].sr_text */ ,
618 NULL /* &candidates[ i ].sr_ctrls (unused) */ ,
621 if ( rs->sr_err != LDAP_SUCCESS ) {
622 ldap_get_option( msc->msc_ld,
623 LDAP_OPT_ERROR_NUMBER,
625 sres = slap_map_api2result( rs );
626 candidates[ i ].sr_type = REP_RESULT;
630 /* massage matchedDN if need be */
631 if ( candidates[ i ].sr_matched != NULL ) {
632 struct berval match, mmatch;
634 ber_str2bv( candidates[ i ].sr_matched,
636 candidates[ i ].sr_matched = NULL;
638 dc.ctx = "matchedDN";
639 dc.target = &mi->mi_targets[ i ];
640 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
641 if ( mmatch.bv_val == match.bv_val ) {
642 candidates[ i ].sr_matched = ch_strdup( mmatch.bv_val );
645 candidates[ i ].sr_matched = mmatch.bv_val;
650 ldap_memfree( match.bv_val );
653 /* add references to array */
658 for ( cnt = 0; references[ cnt ]; cnt++ )
661 sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
663 for ( cnt = 0; references[ cnt ]; cnt++ ) {
664 ber_str2bv( references[ cnt ], 0, 1, &sr_ref[ cnt ] );
666 BER_BVZERO( &sr_ref[ cnt ] );
668 ( void )ldap_back_referral_result_rewrite( &dc, sr_ref );
671 ber_memvfree( (void **)references );
673 if ( rs->sr_v2ref == NULL ) {
674 rs->sr_v2ref = sr_ref;
677 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
678 ber_bvarray_add( &rs->sr_v2ref, &sr_ref[ cnt ] );
680 ber_memfree( sr_ref );
684 rs->sr_err = candidates[ i ].sr_err;
685 sres = slap_map_api2result( rs );
687 if ( LogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
688 snprintf( buf, sizeof( buf ),
689 "%s meta_back_search[%ld] "
690 "match=\"%s\" err=%ld",
692 candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
693 (long) candidates[ i ].sr_err );
694 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
695 Debug( LDAP_DEBUG_TRACE, "%s.\n", buf, 0, 0 );
698 Debug( LDAP_DEBUG_ANY, "%s (%s).\n",
699 buf, ldap_err2string( candidates[ i ].sr_err ), 0 );
704 case LDAP_NO_SUCH_OBJECT:
705 /* is_ok is touched any time a valid
706 * (even intermediate) result is
707 * returned; as a consequence, if
708 * a candidate returns noSuchObject
709 * it is ignored and the candidate
710 * is simply demoted. */
721 case LDAP_SIZELIMIT_EXCEEDED:
722 /* if a target returned sizelimitExceeded
723 * and the entry count is equal to the
724 * proxy's limit, the target would have
725 * returned more, and the error must be
726 * propagated to the client; otherwise,
727 * the target enforced a limit lower
728 * than what requested by the proxy;
730 if ( rs->sr_nentries == op->ors_slimit
731 || META_BACK_ONERR_STOP( mi ) )
733 savepriv = op->o_private;
734 op->o_private = (void *)i;
735 send_ldap_result( op, rs );
736 op->o_private = savepriv;
742 if ( META_BACK_ONERR_STOP( mi ) ) {
743 savepriv = op->o_private;
744 op->o_private = (void *)i;
745 send_ldap_result( op, rs );
746 op->o_private = savepriv;
756 * When no candidates are left,
757 * the outer cycle finishes
759 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
768 /* check for abandon */
769 if ( op->o_abandon || doabandon ) {
770 for ( i = 0; i < mi->mi_ntargets; i++ ) {
771 metasingleconn_t *msc = &mc->mc_conns[ i ];
773 if ( candidates[ i ].sr_msgid != META_MSGID_IGNORE )
775 ldap_abandon_ext( msc->msc_ld,
776 candidates[ i ].sr_msgid,
778 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
782 if ( op->o_abandon ) {
788 /* if no entry was found during this loop,
789 * set a minimal timeout */
791 /* make the entire wait last
792 * LDAP_BACK_RESULT_UTIMEOUT at worst */
794 tv.tv_usec = LDAP_BACK_RESULT_UTIMEOUT/initial_candidates;
795 ldap_pvt_thread_yield();
801 * FIXME: need a better strategy to handle errors
803 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE );
808 * Rewrite the matched portion of the search base, if required
810 * FIXME: only the last one gets caught!
812 savepriv = op->o_private;
813 op->o_private = (void *)(long)mi->mi_ntargets;
814 if ( candidate_match > 0 ) {
815 struct berval pmatched = BER_BVNULL;
817 /* we use the first one */
818 for ( i = 0; i < mi->mi_ntargets; i++ ) {
819 if ( candidates[ i ].sr_tag == META_CANDIDATE
820 && candidates[ i ].sr_matched != NULL )
822 struct berval bv, pbv;
825 /* if we got success, and this target
826 * returned noSuchObject, and its suffix
827 * is a superior of the searchBase,
828 * ignore the matchedDN */
829 if ( sres == LDAP_SUCCESS
830 && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
831 && op->o_req_ndn.bv_len > mi->mi_targets[ i ].mt_nsuffix.bv_len )
833 free( (char *)candidates[ i ].sr_matched );
834 candidates[ i ].sr_matched = NULL;
838 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
839 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
841 if ( rc == LDAP_SUCCESS ) {
843 /* NOTE: if they all are superiors
844 * of the baseDN, the shorter is also
845 * superior of the longer... */
846 if ( pbv.bv_len > pmatched.bv_len ) {
847 if ( !BER_BVISNULL( &pmatched ) ) {
848 op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
851 op->o_private = (void *)i;
854 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
858 if ( candidates[ i ].sr_matched != NULL ) {
859 free( (char *)candidates[ i ].sr_matched );
860 candidates[ i ].sr_matched = NULL;
865 if ( !BER_BVISNULL( &pmatched ) ) {
866 matched = pmatched.bv_val;
869 } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
870 matched = op->o_bd->be_suffix[ 0 ].bv_val;
879 for ( i = 0; i < mi->mi_ntargets; i++ ) {
880 if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
888 snprintf( buf, sizeof( buf ), "%s meta_back_search: is_scope=%d is_ok=%d cnd=\"%s\"\n",
889 op->o_log_prefix, initial_candidates, is_ok, cnd );
891 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
896 * In case we returned at least one entry, we return LDAP_SUCCESS
897 * otherwise, the latter error code we got
899 * FIXME: we should handle error codes and return the more
900 * important/reasonable
903 if ( sres == LDAP_SUCCESS && rs->sr_v2ref ) {
904 sres = LDAP_REFERRAL;
907 rs->sr_matched = matched;
908 rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
909 send_ldap_result( op, rs );
910 op->o_private = savepriv;
911 rs->sr_matched = NULL;
915 if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
916 op->o_tmpfree( matched, op->o_tmpmemctx );
919 if ( rs->sr_v2ref ) {
920 ber_bvarray_free( rs->sr_v2ref );
923 for ( i = 0; i < mi->mi_ntargets; i++ ) {
924 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
928 if ( candidates[ i ].sr_matched ) {
929 free( (char *)candidates[ i ].sr_matched );
930 candidates[ i ].sr_matched = NULL;
933 if ( candidates[ i ].sr_text ) {
934 ldap_memfree( (char *)candidates[ i ].sr_text );
935 candidates[ i ].sr_text = NULL;
938 if ( candidates[ i ].sr_ref ) {
939 ber_bvarray_free( candidates[ i ].sr_ref );
940 candidates[ i ].sr_ref = NULL;
943 if ( candidates[ i ].sr_ctrls ) {
944 ldap_controls_free( candidates[ i ].sr_ctrls );
945 candidates[ i ].sr_ctrls = NULL;
950 meta_back_release_conn( op, mc );
964 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
965 struct berval a, mapped;
967 BerElement ber = *e->lm_ber;
968 Attribute *attr, **attrp;
975 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
976 return LDAP_DECODING_ERROR;
980 * Rewrite the dn of the result, if needed
982 dc.target = &mi->mi_targets[ target ];
983 dc.conn = op->o_conn;
985 dc.ctx = "searchResult";
987 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
988 if ( rs->sr_err != LDAP_SUCCESS) {
993 * Note: this may fail if the target host(s) schema differs
994 * from the one known to the meta, and a DN with unknown
995 * attributes is returned.
997 * FIXME: should we log anything, or delegate to dnNormalize?
999 rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
1001 if ( dn.bv_val != bdn.bv_val ) {
1006 if ( rc != LDAP_SUCCESS ) {
1007 return LDAP_INVALID_DN_SYNTAX;
1013 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1014 ( void )meta_dncache_update_entry( &mi->mi_cache,
1015 &ent.e_nname, target );
1018 attrp = &ent.e_attrs;
1020 dc.ctx = "searchAttrDN";
1021 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
1023 slap_syntax_validate_func *validate;
1024 slap_syntax_transform_func *pretty;
1026 ldap_back_map( &mi->mi_targets[ target ].mt_rwmap.rwm_at,
1027 &a, &mapped, BACKLDAP_REMAP );
1028 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
1029 ( void )ber_scanf( &ber, "x" /* [W] */ );
1032 attr = ( Attribute * )ch_calloc( 1, sizeof( Attribute ) );
1033 if ( attr == NULL ) {
1036 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
1038 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
1039 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
1041 char buf[ SLAP_TEXT_BUFLEN ];
1043 snprintf( buf, sizeof( buf ),
1044 "%s meta_send_entry(\"%s\"): "
1045 "slap_bv2undef_ad(%s): %s\n",
1046 op->o_log_prefix, ent.e_name.bv_val,
1047 mapped.bv_val, text );
1049 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1055 /* no subschemaSubentry */
1056 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
1057 || attr->a_desc == slap_schema.si_ad_entryDN )
1061 * We eat target's subschemaSubentry because
1062 * a search for this value is likely not
1063 * to resolve to the appropriate backend;
1064 * later, the local subschemaSubentry is
1067 * We also eat entryDN because the frontend
1068 * will reattach it without checking if already
1071 ( void )ber_scanf( &ber, "x" /* [W] */ );
1077 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
1078 || attr->a_vals == NULL )
1080 attr->a_vals = (struct berval *)&slap_dummy_bv;
1083 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
1087 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
1088 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
1090 if ( !validate && !pretty ) {
1095 if ( attr->a_desc == slap_schema.si_ad_objectClass
1096 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
1100 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1101 ldap_back_map( &mi->mi_targets[ target ].mt_rwmap.rwm_oc,
1102 bv, &mapped, BACKLDAP_REMAP );
1103 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
1109 *bv = attr->a_vals[ last ];
1110 BER_BVZERO( &attr->a_vals[ last ] );
1113 } else if ( mapped.bv_val != bv->bv_val ) {
1115 ber_dupbv( bv, &mapped );
1119 * It is necessary to try to rewrite attributes with
1120 * dn syntax because they might be used in ACLs as
1121 * members of groups; since ACLs are applied to the
1122 * rewritten stuff, no dn-based subecj clause could
1123 * be used at the ldap backend side (see
1124 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1125 * The problem can be overcome by moving the dn-based
1126 * ACLs to the target directory server, and letting
1127 * everything pass thru the ldap backend.
1132 if ( attr->a_desc->ad_type->sat_syntax ==
1133 slap_schema.si_syn_distinguishedName )
1135 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
1137 } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
1138 ldap_back_referral_result_rewrite( &dc, attr->a_vals );
1142 for ( i = 0; i < last; i++ ) {
1147 rc = pretty( attr->a_desc->ad_type->sat_syntax,
1148 &attr->a_vals[i], &pval, NULL );
1151 rc = validate( attr->a_desc->ad_type->sat_syntax,
1156 LBER_FREE( attr->a_vals[i].bv_val );
1157 if ( --last == i ) {
1158 BER_BVZERO( &attr->a_vals[ i ] );
1161 attr->a_vals[i] = attr->a_vals[last];
1162 BER_BVZERO( &attr->a_vals[last] );
1168 LBER_FREE( attr->a_vals[i].bv_val );
1169 attr->a_vals[i] = pval;
1173 if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
1179 if ( last && attr->a_desc->ad_type->sat_equality &&
1180 attr->a_desc->ad_type->sat_equality->smr_normalize )
1184 attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
1185 for ( i = 0; i<last; i++ ) {
1186 attr->a_desc->ad_type->sat_equality->smr_normalize(
1187 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1188 attr->a_desc->ad_type->sat_syntax,
1189 attr->a_desc->ad_type->sat_equality,
1190 &attr->a_vals[i], &attr->a_nvals[i],
1193 BER_BVZERO( &attr->a_nvals[i] );
1196 attr->a_nvals = attr->a_vals;
1200 attrp = &attr->a_next;
1203 rs->sr_entry = &ent;
1204 rs->sr_attrs = op->ors_attrs;
1206 rs->sr_err = LDAP_SUCCESS;
1207 rc = send_search_entry( op, rs );
1209 case LDAP_UNAVAILABLE:
1213 rs->sr_entry = NULL;
1214 rs->sr_attrs = NULL;
1216 if ( !BER_BVISNULL( &ent.e_name ) ) {
1217 free( ent.e_name.bv_val );
1218 BER_BVZERO( &ent.e_name );
1220 if ( !BER_BVISNULL( &ent.e_nname ) ) {
1221 free( ent.e_nname.bv_val );
1222 BER_BVZERO( &ent.e_nname );
1224 entry_clean( &ent );