2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 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 #ifdef LDAP_SCOPE_SUBORDINATE
106 if ( mi->mi_targets[ candidate ].mt_scope == LDAP_SCOPE_SUBORDINATE ) {
107 realscope = LDAP_SCOPE_SUBORDINATE;
109 #endif /* LDAP_SCOPE_SUBORDINATE */
113 * this target is no longer candidate
115 return META_SEARCH_NOT_CANDIDATE;
119 #ifdef LDAP_SCOPE_SUBORDINATE
120 case LDAP_SCOPE_SUBORDINATE:
121 #endif /* LDAP_SCOPE_SUBORDINATE */
122 case LDAP_SCOPE_ONELEVEL:
124 struct berval rdn = mi->mi_targets[ candidate ].mt_nsuffix;
125 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
126 if ( dnIsOneLevelRDN( &rdn )
127 && dnIsSuffix( &mi->mi_targets[ candidate ].mt_nsuffix, &op->o_req_ndn ) )
130 * if there is exactly one level,
131 * make the target suffix the new
132 * base, and make scope "base"
134 realbase = mi->mi_targets[ candidate ].mt_nsuffix;
135 #ifdef LDAP_SCOPE_SUBORDINATE
136 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
137 if ( mi->mi_targets[ candidate ].mt_scope == LDAP_SCOPE_SUBORDINATE ) {
138 realscope = LDAP_SCOPE_SUBORDINATE;
140 realscope = LDAP_SCOPE_SUBTREE;
143 #endif /* LDAP_SCOPE_SUBORDINATE */
145 realscope = LDAP_SCOPE_BASE;
148 } /* else continue with the next case */
151 case LDAP_SCOPE_BASE:
153 * this target is no longer candidate
155 return META_SEARCH_NOT_CANDIDATE;
160 * Rewrite the search base, if required
162 dc->ctx = "searchBase";
163 switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
167 case REWRITE_REGEXEC_UNWILLING:
168 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
169 rs->sr_text = "Operation not allowed";
170 send_ldap_result( op, rs );
171 return META_SEARCH_ERR;
173 case REWRITE_REGEXEC_ERR:
176 * this target is no longer candidate
178 return META_SEARCH_NOT_CANDIDATE;
184 rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
185 &mfilter, BACKLDAP_MAP );
190 case LDAP_COMPARE_FALSE:
193 * this target is no longer candidate
195 retcode = META_SEARCH_NOT_CANDIDATE;
200 * Maps required attributes
202 rc = ldap_back_map_attrs( &mi->mi_targets[ candidate ].mt_rwmap.rwm_at,
203 op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
204 if ( rc != LDAP_SUCCESS ) {
206 * this target is no longer candidate
208 retcode = META_SEARCH_NOT_CANDIDATE;
215 rc = ldap_search_ext( msc->msc_ld,
216 mbase.bv_val, realscope, mfilter.bv_val,
217 mapped_attrs, op->ors_attrsonly,
218 op->o_ctrls, NULL, tvp, op->ors_slimit,
219 &candidates[ candidate ].sr_msgid );
220 if ( rc == LDAP_SUCCESS ) {
221 retcode = META_SEARCH_CANDIDATE;
224 candidates[ candidate ].sr_msgid = -1;
225 retcode = META_SEARCH_NOT_CANDIDATE;
229 if ( mapped_attrs ) {
230 free( mapped_attrs );
232 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
233 free( mfilter.bv_val );
235 if ( mbase.bv_val != realbase.bv_val ) {
236 free( mbase.bv_val );
243 meta_back_search( Operation *op, SlapReply *rs )
245 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
247 struct timeval tv = { 0, 0 };
248 time_t stoptime = (time_t)-1;
249 LDAPMessage *res = NULL, *e;
250 int rc = 0, sres = LDAP_SUCCESS;
251 char *matched = NULL;
252 int last = 0, ncandidates = 0,
253 initial_candidates = 0, candidate_match = 0;
258 SlapReply *candidates = meta_back_candidates_get( op );
261 * controls are set in ldap_back_dobind()
263 * FIXME: in case of values return filter, we might want
264 * to map attrs and maybe rewrite value
266 mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_SENDERR );
267 if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
271 dc.conn = op->o_conn;
277 for ( i = 0; i < mi->mi_ntargets; i++ ) {
278 metasingleconn_t *msc = &mc->mc_conns[ i ];
280 candidates[ i ].sr_msgid = -1;
281 candidates[ i ].sr_matched = NULL;
282 candidates[ i ].sr_text = NULL;
283 candidates[ i ].sr_ref = NULL;
284 candidates[ i ].sr_ctrls = NULL;
286 if ( candidates[ i ].sr_tag != META_CANDIDATE
287 || candidates[ i ].sr_err != LDAP_SUCCESS )
292 switch ( meta_back_search_start( op, rs, &dc, msc, i, candidates ) )
294 case META_SEARCH_NOT_CANDIDATE:
297 case META_SEARCH_CANDIDATE:
298 candidates[ i ].sr_type = REP_INTERMEDIATE;
302 case META_SEARCH_ERR:
308 initial_candidates = ncandidates;
315 for ( i = 0; i < mi->mi_ntargets; i++ ) {
316 if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
324 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: ncandidates=%d "
325 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
329 if ( initial_candidates == 0 ) {
330 /* NOTE: here we are not sending any matchedDN;
331 * this is intended, because if the back-meta
332 * is serving this search request, but no valid
333 * candidate could be looked up, it means that
334 * there is a hole in the mapping of the targets
335 * and thus no knowledge of any remote superior
337 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
338 "base=\"%s\" scope=%d: "
339 "no candidate could be selected\n",
340 op->o_log_prefix, op->o_req_dn.bv_val,
343 /* FIXME: we're sending the first error we encounter;
344 * maybe we should pick the worst... */
345 rc = LDAP_NO_SUCH_OBJECT;
346 for ( i = 0; i < mi->mi_ntargets; i++ ) {
347 if ( candidates[ i ].sr_tag == META_CANDIDATE
348 && candidates[ i ].sr_err != LDAP_SUCCESS )
350 rc = candidates[ i ].sr_err;
355 send_ldap_error( op, rs, rc, NULL );
360 /* We pull apart the ber result, stuff it into a slapd entry, and
361 * let send_search_entry stuff it back into ber format. Slow & ugly,
362 * but this is necessary for version matching, and for ACL processing.
365 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
366 stoptime = op->o_time + op->ors_tlimit;
370 * In case there are no candidates, no cycle takes place...
372 * FIXME: we might use a queue, to better balance the load
373 * among the candidates
375 for ( rc = 0; ncandidates > 0; ) {
376 int gotit = 0, doabandon = 0;
378 for ( i = 0; i < mi->mi_ntargets; i++ ) {
379 metasingleconn_t *msc = &mc->mc_conns[ i ];
381 if ( candidates[ i ].sr_msgid == -1 ) {
385 /* check for abandon */
386 if ( op->o_abandon ) {
391 * FIXME: handle time limit as well?
392 * Note that target servers are likely
393 * to handle it, so at some time we'll
394 * get a LDAP_TIMELIMIT_EXCEEDED from
398 rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
402 /* FIXME: res should not need to be freed */
403 assert( res == NULL );
405 /* check time limit */
406 if ( op->ors_tlimit != SLAP_NO_LIMIT
407 && slap_get_time() > stoptime )
410 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
411 savepriv = op->o_private;
412 op->o_private = (void *)i;
413 send_ldap_result( op, rs );
414 op->o_private = savepriv;
420 } else if ( rc == -1 ) {
422 /* something REALLY bad happened! */
423 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
424 candidates[ i ].sr_type = REP_RESULT;
426 if ( meta_back_retry( op, rs, mc, i, LDAP_BACK_DONTSEND ) ) {
427 switch ( meta_back_search_start( op, rs, &dc, msc, i, candidates ) )
429 case META_SEARCH_CANDIDATE:
433 rc = rs->sr_err = LDAP_OTHER;
440 * When no candidates are left,
441 * the outer cycle finishes
443 candidates[ i ].sr_msgid = -1;
445 rs->sr_err = candidates[ i ].sr_err = LDAP_OTHER;
446 rs->sr_text = "remote server unavailable";
448 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
449 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
450 /* don't retry any more... */
451 candidates[ i ].sr_type = REP_RESULT;
454 if ( --op->ors_slimit == -1 ) {
458 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
459 savepriv = op->o_private;
460 op->o_private = (void *)i;
461 send_ldap_result( op, rs );
462 op->o_private = savepriv;
468 e = ldap_first_entry( msc->msc_ld, res );
469 savepriv = op->o_private;
470 op->o_private = (void *)i;
471 meta_send_entry( op, rs, mc, i, e );
472 op->o_private = savepriv;
481 * If scope is BASE, we need to jump out
482 * as soon as one entry is found; if
483 * the target pool is properly crafted,
484 * this should correspond to the sole
485 * entry that has the base DN
487 /* FIXME: this defeats the purpose of
488 * doing a search with scope == base and
489 * sizelimit = 1 to determine if a
490 * candidate is actually unique */
491 if ( op->ors_scope == LDAP_SCOPE_BASE
492 && rs->sr_nentries > 0 )
501 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
502 char **references = NULL;
505 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
506 /* don't retry any more... */
507 candidates[ i ].sr_type = REP_RESULT;
512 rc = ldap_parse_reference( msc->msc_ld, res,
513 &references, &rs->sr_ctrls, 1 );
516 if ( rc != LDAP_SUCCESS ) {
520 if ( references == NULL ) {
524 #ifdef ENABLE_REWRITE
525 dc.ctx = "referralDN";
526 #else /* ! ENABLE_REWRITE */
529 #endif /* ! ENABLE_REWRITE */
531 /* FIXME: merge all and return at the end */
533 for ( cnt = 0; references[ cnt ]; cnt++ )
536 rs->sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
538 for ( cnt = 0; references[ cnt ]; cnt++ ) {
539 ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
541 BER_BVZERO( &rs->sr_ref[ cnt ] );
543 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref );
545 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
546 /* ignore return value by now */
547 savepriv = op->o_private;
548 op->o_private = (void *)i;
549 ( void )send_search_reference( op, rs );
550 op->o_private = savepriv;
552 ber_bvarray_free( rs->sr_ref );
558 ber_memvfree( (void **)references );
561 if ( rs->sr_ctrls ) {
562 ldap_controls_free( rs->sr_ctrls );
566 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
567 char buf[ SLAP_TEXT_BUFLEN ];
568 char **references = NULL;
570 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
571 /* don't retry any more... */
572 candidates[ i ].sr_type = REP_RESULT;
575 if ( ldap_parse_result( msc->msc_ld,
577 &candidates[ i ].sr_err,
578 (char **)&candidates[ i ].sr_matched,
579 NULL /* (char **)&candidates[ i ].sr_text */ ,
581 &candidates[ i ].sr_ctrls, 1 ) != LDAP_SUCCESS )
584 ldap_get_option( msc->msc_ld,
585 LDAP_OPT_ERROR_NUMBER,
587 sres = slap_map_api2result( rs );
588 candidates[ i ].sr_type = REP_RESULT;
592 rs->sr_err = candidates[ i ].sr_err;
593 sres = slap_map_api2result( rs );
596 /* massage matchedDN if need be */
597 if ( candidates[ i ].sr_matched != NULL ) {
598 #ifndef LDAP_NULL_IS_NULL
599 if ( candidates[ i ].sr_matched[ 0 ] == '\0' ) {
600 ldap_memfree( (char *)candidates[ i ].sr_matched );
601 candidates[ i ].sr_matched = NULL;
604 #endif /* LDAP_NULL_IS_NULL */
606 struct berval match, mmatch;
608 ber_str2bv( candidates[ i ].sr_matched,
611 dc.ctx = "matchedDN";
612 dc.target = &mi->mi_targets[ i ];
614 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
615 if ( mmatch.bv_val == match.bv_val ) {
616 candidates[ i ].sr_matched = ch_strdup( mmatch.bv_val );
619 candidates[ i ].sr_matched = mmatch.bv_val;
624 ldap_memfree( match.bv_val );
628 #ifndef LDAP_NULL_IS_NULL
629 /* just get rid of the error message, if any */
630 if ( candidates[ i ].sr_text && candidates[ i ].sr_text[ 0 ] == '\0' )
632 ldap_memfree( (char *)candidates[ i ].sr_text );
633 candidates[ i ].sr_text = NULL;
635 #endif /* LDAP_NULL_IS_NULL */
637 /* add references to array */
642 for ( cnt = 0; references[ cnt ]; cnt++ )
645 sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
647 for ( cnt = 0; references[ cnt ]; cnt++ ) {
648 ber_str2bv( references[ cnt ], 0, 1, &sr_ref[ cnt ] );
650 BER_BVZERO( &sr_ref[ cnt ] );
652 ( void )ldap_back_referral_result_rewrite( &dc, sr_ref );
655 ber_memvfree( (void **)references );
657 if ( rs->sr_v2ref == NULL ) {
658 rs->sr_v2ref = sr_ref;
661 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
662 ber_bvarray_add( &rs->sr_v2ref, &sr_ref[ cnt ] );
664 ber_memfree( sr_ref );
668 rs->sr_err = candidates[ i ].sr_err;
669 sres = slap_map_api2result( rs );
671 snprintf( buf, sizeof( buf ),
672 "%s meta_back_search[%ld] "
673 "match=\"%s\" err=%ld\n",
675 candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
676 (long) candidates[ i ].sr_err );
677 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
680 case LDAP_NO_SUCH_OBJECT:
681 /* is_ok is touched any time a valid
682 * (even intermediate) result is
683 * returned; as a consequence, if
684 * a candidate returns noSuchObject
685 * it is ignored and the candidate
686 * is simply demoted. */
698 if ( META_BACK_ONERR_STOP( mi ) ) {
699 savepriv = op->o_private;
700 op->o_private = (void *)i;
701 send_ldap_result( op, rs );
702 op->o_private = savepriv;
712 * When no candidates are left,
713 * the outer cycle finishes
715 candidates[ i ].sr_msgid = -1;
724 /* check for abandon */
725 if ( op->o_abandon || doabandon ) {
726 for ( i = 0; i < mi->mi_ntargets; i++ ) {
727 metasingleconn_t *msc = &mc->mc_conns[ i ];
729 if ( candidates[ i ].sr_msgid != -1 ) {
730 ldap_abandon_ext( msc->msc_ld,
731 candidates[ i ].sr_msgid,
733 candidates[ i ].sr_msgid = -1;
737 if ( op->o_abandon ) {
744 LDAP_BACK_TV_SET( &tv );
745 ldap_pvt_thread_yield();
755 * FIXME: need a better strategy to handle errors
757 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE );
762 * Rewrite the matched portion of the search base, if required
764 * FIXME: only the last one gets caught!
766 savepriv = op->o_private;
767 op->o_private = (void *)(long)mi->mi_ntargets;
768 if ( candidate_match > 0 ) {
769 struct berval pmatched = BER_BVNULL;
771 /* we use the first one */
772 for ( i = 0; i < mi->mi_ntargets; i++ ) {
773 if ( candidates[ i ].sr_tag == META_CANDIDATE
774 && candidates[ i ].sr_matched )
776 struct berval bv, pbv;
779 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
780 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
782 if ( rc == LDAP_SUCCESS ) {
784 /* NOTE: if they all are superiors
785 * of the baseDN, the shorter is also
786 * superior of the longer... */
787 if ( pbv.bv_len > pmatched.bv_len ) {
788 if ( !BER_BVISNULL( &pmatched ) ) {
789 op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
792 op->o_private = (void *)i;
795 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
799 if ( candidates[ i ].sr_matched != NULL ) {
800 free( (char *)candidates[ i ].sr_matched );
801 candidates[ i ].sr_matched = NULL;
806 if ( !BER_BVISNULL( &pmatched ) ) {
807 matched = pmatched.bv_val;
810 } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
811 matched = op->o_bd->be_suffix[ 0 ].bv_val;
820 for ( i = 0; i < mi->mi_ntargets; i++ ) {
821 if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
829 snprintf( buf, sizeof( buf ), "%s meta_back_search: is_scope=%d is_ok=%d cnd=\"%s\"\n",
830 op->o_log_prefix, initial_candidates, is_ok, cnd );
832 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
837 * In case we returned at least one entry, we return LDAP_SUCCESS
838 * otherwise, the latter error code we got
840 * FIXME: we should handle error codes and return the more
841 * important/reasonable
844 if ( sres == LDAP_SUCCESS && rs->sr_v2ref ) {
845 sres = LDAP_REFERRAL;
848 rs->sr_matched = matched;
849 rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
850 send_ldap_result( op, rs );
851 op->o_private = savepriv;
852 rs->sr_matched = NULL;
856 if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
857 op->o_tmpfree( matched, op->o_tmpmemctx );
860 if ( rs->sr_v2ref ) {
861 ber_bvarray_free( rs->sr_v2ref );
864 for ( i = 0; i < mi->mi_ntargets; i++ ) {
865 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
869 if ( candidates[ i ].sr_matched ) {
870 free( (char *)candidates[ i ].sr_matched );
871 candidates[ i ].sr_matched = NULL;
874 if ( candidates[ i ].sr_text ) {
875 ldap_memfree( (char *)candidates[ i ].sr_text );
876 candidates[ i ].sr_text = NULL;
879 if ( candidates[ i ].sr_ref ) {
880 ber_bvarray_free( candidates[ i ].sr_ref );
881 candidates[ i ].sr_ref = NULL;
884 if ( candidates[ i ].sr_ctrls ) {
885 ldap_controls_free( candidates[ i ].sr_ctrls );
886 candidates[ i ].sr_ctrls = NULL;
890 meta_back_release_conn( op, mc );
903 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
904 struct berval a, mapped;
906 BerElement ber = *e->lm_ber;
907 Attribute *attr, **attrp;
914 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
915 return LDAP_DECODING_ERROR;
919 * Rewrite the dn of the result, if needed
921 dc.target = &mi->mi_targets[ target ];
922 dc.conn = op->o_conn;
924 dc.ctx = "searchResult";
926 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
927 if ( rs->sr_err != LDAP_SUCCESS) {
932 * Note: this may fail if the target host(s) schema differs
933 * from the one known to the meta, and a DN with unknown
934 * attributes is returned.
936 * FIXME: should we log anything, or delegate to dnNormalize?
938 rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
940 if ( dn.bv_val != bdn.bv_val ) {
945 if ( rc != LDAP_SUCCESS ) {
946 return LDAP_INVALID_DN_SYNTAX;
952 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
953 ( void )meta_dncache_update_entry( &mi->mi_cache,
954 &ent.e_nname, target );
957 attrp = &ent.e_attrs;
959 dc.ctx = "searchAttrDN";
960 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
962 slap_syntax_validate_func *validate;
963 slap_syntax_transform_func *pretty;
965 ldap_back_map( &mi->mi_targets[ target ].mt_rwmap.rwm_at,
966 &a, &mapped, BACKLDAP_REMAP );
967 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
968 ( void )ber_scanf( &ber, "x" /* [W] */ );
971 attr = ( Attribute * )ch_calloc( 1, sizeof( Attribute ) );
972 if ( attr == NULL ) {
975 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
977 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
978 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
980 char buf[ SLAP_TEXT_BUFLEN ];
982 snprintf( buf, sizeof( buf ),
983 "%s meta_send_entry(\"%s\"): "
984 "slap_bv2undef_ad(%s): %s\n",
985 op->o_log_prefix, ent.e_name.bv_val,
986 mapped.bv_val, text );
988 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
994 /* no subschemaSubentry */
995 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
996 || attr->a_desc == slap_schema.si_ad_entryDN )
1000 * We eat target's subschemaSubentry because
1001 * a search for this value is likely not
1002 * to resolve to the appropriate backend;
1003 * later, the local subschemaSubentry is
1006 * We also eat entryDN because the frontend
1007 * will reattach it without checking if already
1010 ( void )ber_scanf( &ber, "x" /* [W] */ );
1016 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
1017 || attr->a_vals == NULL )
1019 attr->a_vals = (struct berval *)&slap_dummy_bv;
1022 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
1026 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
1027 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
1029 if ( !validate && !pretty ) {
1034 if ( attr->a_desc == slap_schema.si_ad_objectClass
1035 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
1039 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1040 ldap_back_map( &mi->mi_targets[ target ].mt_rwmap.rwm_oc,
1041 bv, &mapped, BACKLDAP_REMAP );
1042 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
1048 *bv = attr->a_vals[ last ];
1049 BER_BVZERO( &attr->a_vals[ last ] );
1052 } else if ( mapped.bv_val != bv->bv_val ) {
1054 ber_dupbv( bv, &mapped );
1058 * It is necessary to try to rewrite attributes with
1059 * dn syntax because they might be used in ACLs as
1060 * members of groups; since ACLs are applied to the
1061 * rewritten stuff, no dn-based subecj clause could
1062 * be used at the ldap backend side (see
1063 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1064 * The problem can be overcome by moving the dn-based
1065 * ACLs to the target directory server, and letting
1066 * everything pass thru the ldap backend.
1071 if ( attr->a_desc->ad_type->sat_syntax ==
1072 slap_schema.si_syn_distinguishedName )
1074 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
1076 } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
1077 ldap_back_referral_result_rewrite( &dc, attr->a_vals );
1081 for ( i = 0; i < last; i++ ) {
1086 rc = pretty( attr->a_desc->ad_type->sat_syntax,
1087 &attr->a_vals[i], &pval, NULL );
1090 rc = validate( attr->a_desc->ad_type->sat_syntax,
1095 LBER_FREE( attr->a_vals[i].bv_val );
1096 if ( --last == i ) {
1097 BER_BVZERO( &attr->a_vals[ i ] );
1100 attr->a_vals[i] = attr->a_vals[last];
1101 BER_BVZERO( &attr->a_vals[last] );
1107 LBER_FREE( attr->a_vals[i].bv_val );
1108 attr->a_vals[i] = pval;
1112 if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
1118 if ( last && attr->a_desc->ad_type->sat_equality &&
1119 attr->a_desc->ad_type->sat_equality->smr_normalize )
1123 attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
1124 for ( i = 0; i<last; i++ ) {
1125 attr->a_desc->ad_type->sat_equality->smr_normalize(
1126 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1127 attr->a_desc->ad_type->sat_syntax,
1128 attr->a_desc->ad_type->sat_equality,
1129 &attr->a_vals[i], &attr->a_nvals[i],
1132 BER_BVZERO( &attr->a_nvals[i] );
1135 attr->a_nvals = attr->a_vals;
1139 attrp = &attr->a_next;
1142 rs->sr_entry = &ent;
1143 rs->sr_attrs = op->ors_attrs;
1145 send_search_entry( op, rs );
1146 rs->sr_entry = NULL;
1147 rs->sr_attrs = NULL;
1149 if ( !BER_BVISNULL( &ent.e_name ) ) {
1150 free( ent.e_name.bv_val );
1151 BER_BVZERO( &ent.e_name );
1153 if ( !BER_BVISNULL( &ent.e_nname ) ) {
1154 free( ent.e_nname.bv_val );
1155 BER_BVZERO( &ent.e_nname );
1157 entry_clean( &ent );
1159 return LDAP_SUCCESS;