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"
47 meta_back_search_start(
51 metasingleconn_t *msc,
56 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
57 struct berval realbase = op->o_req_dn;
58 int realscope = op->ors_scope;
59 ber_len_t suffixlen = 0;
60 struct berval mbase = BER_BVNULL;
61 struct berval mfilter = BER_BVNULL;
62 char **mapped_attrs = NULL;
64 struct timeval tv, *tvp = NULL;
66 /* should we check return values? */
67 if ( op->ors_deref != -1 ) {
68 ldap_set_option( msc->msc_ld, LDAP_OPT_DEREF,
69 ( void * )&op->ors_deref );
72 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
73 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
77 dc->target = &mi->mi_targets[ candidate ];
80 * modifies the base according to the scope, if required
82 suffixlen = mi->mi_targets[ candidate ].mt_nsuffix.bv_len;
83 if ( suffixlen > op->o_req_ndn.bv_len ) {
84 switch ( op->ors_scope ) {
85 case LDAP_SCOPE_SUBTREE:
87 * make the target suffix the new base
88 * FIXME: this is very forgiving, because
89 * "illegal" searchBases may be turned
90 * into the suffix of the target; however,
91 * the requested searchBase already passed
92 * thru the candidate analyzer...
94 if ( dnIsSuffix( &mi->mi_targets[ candidate ].mt_nsuffix,
97 realbase = mi->mi_targets[ candidate ].mt_nsuffix;
98 #ifdef LDAP_SCOPE_SUBORDINATE
99 if ( mi->mi_targets[ candidate ].mt_scope == LDAP_SCOPE_SUBORDINATE ) {
100 realscope = LDAP_SCOPE_SUBORDINATE;
102 #endif /* LDAP_SCOPE_SUBORDINATE */
106 * this target is no longer candidate
112 #ifdef LDAP_SCOPE_SUBORDINATE
113 case LDAP_SCOPE_SUBORDINATE:
114 #endif /* LDAP_SCOPE_SUBORDINATE */
115 case LDAP_SCOPE_ONELEVEL:
117 struct berval rdn = mi->mi_targets[ candidate ].mt_nsuffix;
118 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
119 if ( dnIsOneLevelRDN( &rdn )
120 && dnIsSuffix( &mi->mi_targets[ candidate ].mt_nsuffix, &op->o_req_ndn ) )
123 * if there is exactly one level,
124 * make the target suffix the new
125 * base, and make scope "base"
127 realbase = mi->mi_targets[ candidate ].mt_nsuffix;
128 #ifdef LDAP_SCOPE_SUBORDINATE
129 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
130 if ( mi->mi_targets[ candidate ].mt_scope == LDAP_SCOPE_SUBORDINATE ) {
131 realscope = LDAP_SCOPE_SUBORDINATE;
133 realscope = LDAP_SCOPE_SUBTREE;
136 #endif /* LDAP_SCOPE_SUBORDINATE */
138 realscope = LDAP_SCOPE_BASE;
141 } /* else continue with the next case */
144 case LDAP_SCOPE_BASE:
146 * this target is no longer candidate
153 * Rewrite the search base, if required
155 dc->ctx = "searchBase";
156 switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
160 case REWRITE_REGEXEC_UNWILLING:
161 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
162 rs->sr_text = "Operation not allowed";
163 send_ldap_result( op, rs );
166 case REWRITE_REGEXEC_ERR:
169 * this target is no longer 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
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
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 ) {
217 candidates[ candidate ].sr_msgid = -1;
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 };
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 ) )
300 initial_candidates = ncandidates;
307 for ( i = 0; i < mi->mi_ntargets; i++ ) {
308 if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
316 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: ncandidates=%d "
317 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
321 if ( initial_candidates == 0 ) {
322 /* NOTE: here we are not sending any matchedDN;
323 * this is intended, because if the back-meta
324 * is serving this search request, but no valid
325 * candidate could be looked up, it means that
326 * there is a hole in the mapping of the targets
327 * and thus no knowledge of any remote superior
329 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
330 "base=\"%s\" scope=%d: "
331 "no candidate could be selected\n",
332 op->o_log_prefix, op->o_req_dn.bv_val,
335 /* FIXME: we're sending the first error we encounter;
336 * maybe we should pick the worst... */
337 rc = LDAP_NO_SUCH_OBJECT;
338 for ( i = 0; i < mi->mi_ntargets; i++ ) {
339 if ( candidates[ i ].sr_tag == META_CANDIDATE
340 && candidates[ i ].sr_err != LDAP_SUCCESS )
342 rc = candidates[ i ].sr_err;
347 send_ldap_error( op, rs, rc, NULL );
352 /* We pull apart the ber result, stuff it into a slapd entry, and
353 * let send_search_entry stuff it back into ber format. Slow & ugly,
354 * but this is necessary for version matching, and for ACL processing.
357 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
358 stoptime = op->o_time + op->ors_tlimit;
362 * In case there are no candidates, no cycle takes place...
364 * FIXME: we might use a queue, to better balance the load
365 * among the candidates
367 for ( rc = 0; ncandidates > 0; ) {
368 int gotit = 0, doabandon = 0;
370 for ( i = 0; i < mi->mi_ntargets; i++ ) {
371 metasingleconn_t *msc = &mc->mc_conns[ i ];
373 if ( candidates[ i ].sr_msgid == -1 ) {
377 /* check for abandon */
378 if ( op->o_abandon ) {
383 * FIXME: handle time limit as well?
384 * Note that target servers are likely
385 * to handle it, so at some time we'll
386 * get a LDAP_TIMELIMIT_EXCEEDED from
389 rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
393 /* FIXME: res should not need to be freed */
394 assert( res == NULL );
396 /* check time limit */
397 if ( op->ors_tlimit != SLAP_NO_LIMIT
398 && slap_get_time() > stoptime )
401 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
402 savepriv = op->o_private;
403 op->o_private = (void *)i;
404 send_ldap_result( op, rs );
405 op->o_private = savepriv;
411 } else if ( rc == -1 ) {
413 /* something REALLY bad happened! */
414 ( void )meta_clear_unused_candidates( op, -1 );
415 rs->sr_err = LDAP_OTHER;
416 savepriv = op->o_private;
417 op->o_private = (void *)i;
418 send_ldap_result( op, rs );
419 op->o_private = savepriv;
421 /* anything else needs be done? */
423 /* FIXME: res should not need to be freed */
424 assert( res == NULL );
428 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
429 if ( --op->ors_slimit == -1 ) {
433 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
434 savepriv = op->o_private;
435 op->o_private = (void *)i;
436 send_ldap_result( op, rs );
437 op->o_private = savepriv;
443 e = ldap_first_entry( msc->msc_ld, res );
444 savepriv = op->o_private;
445 op->o_private = (void *)i;
446 meta_send_entry( op, rs, mc, i, e );
447 op->o_private = savepriv;
456 * If scope is BASE, we need to jump out
457 * as soon as one entry is found; if
458 * the target pool is properly crafted,
459 * this should correspond to the sole
460 * entry that has the base DN
462 /* FIXME: this defeats the purpose of
463 * doing a search with scope == base and
464 * sizelimit = 1 to determine if a
465 * candidate is actually unique */
466 if ( op->ors_scope == LDAP_SCOPE_BASE
467 && rs->sr_nentries > 0 )
476 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
477 char **references = NULL;
482 rc = ldap_parse_reference( msc->msc_ld, res,
483 &references, &rs->sr_ctrls, 1 );
486 if ( rc != LDAP_SUCCESS ) {
490 if ( references == NULL ) {
494 #ifdef ENABLE_REWRITE
495 dc.ctx = "referralDN";
496 #else /* ! ENABLE_REWRITE */
499 #endif /* ! ENABLE_REWRITE */
501 /* FIXME: merge all and return at the end */
503 for ( cnt = 0; references[ cnt ]; cnt++ )
506 rs->sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
508 for ( cnt = 0; references[ cnt ]; cnt++ ) {
509 ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
511 BER_BVZERO( &rs->sr_ref[ cnt ] );
513 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref );
515 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
516 /* ignore return value by now */
517 savepriv = op->o_private;
518 op->o_private = (void *)i;
519 ( void )send_search_reference( op, rs );
520 op->o_private = savepriv;
522 ber_bvarray_free( rs->sr_ref );
528 ldap_value_free( references );
531 if ( rs->sr_ctrls ) {
532 ldap_controls_free( rs->sr_ctrls );
536 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
537 char buf[ SLAP_TEXT_BUFLEN ];
538 char **references = NULL;
540 if ( ldap_parse_result( msc->msc_ld,
542 &candidates[ i ].sr_err,
543 (char **)&candidates[ i ].sr_matched,
544 NULL /* (char **)&candidates[ i ].sr_text */ ,
546 &candidates[ i ].sr_ctrls, 1 ) != LDAP_SUCCESS )
549 ldap_get_option( msc->msc_ld,
550 LDAP_OPT_ERROR_NUMBER,
552 sres = slap_map_api2result( rs );
556 rs->sr_err = candidates[ i ].sr_err;
557 sres = slap_map_api2result( rs );
560 /* massage matchedDN if need be */
561 if ( candidates[ i ].sr_matched != NULL ) {
562 if ( candidates[ i ].sr_matched[ 0 ] == '\0' ) {
563 ldap_memfree( (char *)candidates[ i ].sr_matched );
564 candidates[ i ].sr_matched = NULL;
567 struct berval match, mmatch;
569 ber_str2bv( candidates[ i ].sr_matched,
572 dc.ctx = "matchedDN";
573 dc.target = &mi->mi_targets[ i ];
575 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
576 if ( mmatch.bv_val == match.bv_val ) {
577 candidates[ i ].sr_matched = ch_strdup( mmatch.bv_val );
580 candidates[ i ].sr_matched = mmatch.bv_val;
585 ldap_memfree( match.bv_val );
589 /* just get rid of the error message, if any */
590 if ( candidates[ i ].sr_text && candidates[ i ].sr_text[ 0 ] == '\0' )
592 ldap_memfree( (char *)candidates[ i ].sr_text );
593 candidates[ i ].sr_text = NULL;
596 /* add references to array */
601 for ( cnt = 0; references[ cnt ]; cnt++ )
604 sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
606 for ( cnt = 0; references[ cnt ]; cnt++ ) {
607 ber_str2bv( references[ cnt ], 0, 1, &sr_ref[ cnt ] );
609 BER_BVZERO( &sr_ref[ cnt ] );
611 ( void )ldap_back_referral_result_rewrite( &dc, sr_ref );
614 ldap_value_free( references );
616 if ( rs->sr_v2ref == NULL ) {
617 rs->sr_v2ref = sr_ref;
620 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
621 ber_bvarray_add( &rs->sr_v2ref, &sr_ref[ cnt ] );
623 ber_memfree( sr_ref );
627 rs->sr_err = candidates[ i ].sr_err;
628 sres = slap_map_api2result( rs );
630 snprintf( buf, sizeof( buf ),
631 "%s meta_back_search[%ld] "
632 "match=\"%s\" err=%ld\n",
634 candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
635 (long) candidates[ i ].sr_err );
636 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
639 case LDAP_NO_SUCH_OBJECT:
640 /* is_ok is touched any time a valid
641 * (even intermediate) result is
642 * returned; as a consequence, if
643 * a candidate returns noSuchObject
644 * it is ignored and the candidate
645 * is simply demoted. */
657 if ( META_BACK_ONERR_STOP( mi ) ) {
658 savepriv = op->o_private;
659 op->o_private = (void *)i;
660 send_ldap_result( op, rs );
661 op->o_private = savepriv;
671 * When no candidates are left,
672 * the outer cycle finishes
674 candidates[ i ].sr_msgid = -1;
683 /* check for abandon */
684 if ( op->o_abandon || doabandon ) {
685 for ( i = 0; i < mi->mi_ntargets; i++ ) {
686 metasingleconn_t *msc = &mc->mc_conns[ i ];
688 if ( candidates[ i ].sr_msgid != -1 ) {
689 ldap_abandon_ext( msc->msc_ld,
690 candidates[ i ].sr_msgid,
692 candidates[ i ].sr_msgid = -1;
696 if ( op->o_abandon ) {
703 LDAP_BACK_TV_SET( &tv );
704 ldap_pvt_thread_yield();
714 * FIXME: need a better strategy to handle errors
716 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE );
721 * Rewrite the matched portion of the search base, if required
723 * FIXME: only the last one gets caught!
725 savepriv = op->o_private;
726 op->o_private = (void *)(long)mi->mi_ntargets;
727 if ( candidate_match > 0 ) {
728 struct berval pmatched = BER_BVNULL;
730 /* we use the first one */
731 for ( i = 0; i < mi->mi_ntargets; i++ ) {
732 if ( candidates[ i ].sr_tag == META_CANDIDATE
733 && candidates[ i ].sr_matched )
735 struct berval bv, pbv;
738 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
739 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
741 if ( rc == LDAP_SUCCESS ) {
743 /* NOTE: if they all are superiors
744 * of the baseDN, the shorter is also
745 * superior of the longer... */
746 if ( pbv.bv_len > pmatched.bv_len ) {
747 if ( !BER_BVISNULL( &pmatched ) ) {
748 op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
751 op->o_private = (void *)i;
754 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
758 if ( candidates[ i ].sr_matched != NULL ) {
759 free( (char *)candidates[ i ].sr_matched );
760 candidates[ i ].sr_matched = NULL;
765 if ( !BER_BVISNULL( &pmatched ) ) {
766 matched = pmatched.bv_val;
769 } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
770 matched = op->o_bd->be_suffix[ 0 ].bv_val;
779 for ( i = 0; i < mi->mi_ntargets; i++ ) {
780 if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
788 snprintf( buf, sizeof( buf ), "%s meta_back_search: is_scope=%d is_ok=%d cnd=\"%s\"\n",
789 op->o_log_prefix, initial_candidates, is_ok, cnd );
791 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
796 * In case we returned at least one entry, we return LDAP_SUCCESS
797 * otherwise, the latter error code we got
799 * FIXME: we should handle error codes and return the more
800 * important/reasonable
803 if ( sres == LDAP_SUCCESS && rs->sr_v2ref ) {
804 sres = LDAP_REFERRAL;
807 rs->sr_matched = matched;
808 rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
809 send_ldap_result( op, rs );
810 op->o_private = savepriv;
811 rs->sr_matched = NULL;
815 if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
816 op->o_tmpfree( matched, op->o_tmpmemctx );
819 if ( rs->sr_v2ref ) {
820 ber_bvarray_free( rs->sr_v2ref );
823 for ( i = 0; i < mi->mi_ntargets; i++ ) {
824 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
828 if ( candidates[ i ].sr_matched ) {
829 free( (char *)candidates[ i ].sr_matched );
830 candidates[ i ].sr_matched = NULL;
833 if ( candidates[ i ].sr_text ) {
834 ldap_memfree( (char *)candidates[ i ].sr_text );
835 candidates[ i ].sr_text = NULL;
838 if ( candidates[ i ].sr_ref ) {
839 ber_bvarray_free( candidates[ i ].sr_ref );
840 candidates[ i ].sr_ref = NULL;
843 if ( candidates[ i ].sr_ctrls ) {
844 ldap_controls_free( candidates[ i ].sr_ctrls );
845 candidates[ i ].sr_ctrls = NULL;
849 meta_back_release_conn( op, mc );
862 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
863 struct berval a, mapped;
865 BerElement ber = *e->lm_ber;
866 Attribute *attr, **attrp;
873 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
874 return LDAP_DECODING_ERROR;
878 * Rewrite the dn of the result, if needed
880 dc.target = &mi->mi_targets[ target ];
881 dc.conn = op->o_conn;
883 dc.ctx = "searchResult";
885 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
886 if ( rs->sr_err != LDAP_SUCCESS) {
891 * Note: this may fail if the target host(s) schema differs
892 * from the one known to the meta, and a DN with unknown
893 * attributes is returned.
895 * FIXME: should we log anything, or delegate to dnNormalize?
897 rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
899 if ( dn.bv_val != bdn.bv_val ) {
904 if ( rc != LDAP_SUCCESS ) {
905 return LDAP_INVALID_DN_SYNTAX;
911 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
912 ( void )meta_dncache_update_entry( &mi->mi_cache,
913 &ent.e_nname, target );
916 attrp = &ent.e_attrs;
918 dc.ctx = "searchAttrDN";
919 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
921 slap_syntax_validate_func *validate;
922 slap_syntax_transform_func *pretty;
924 ldap_back_map( &mi->mi_targets[ target ].mt_rwmap.rwm_at,
925 &a, &mapped, BACKLDAP_REMAP );
926 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
927 ( void )ber_scanf( &ber, "x" /* [W] */ );
930 attr = ( Attribute * )ch_calloc( 1, sizeof( Attribute ) );
931 if ( attr == NULL ) {
934 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
936 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
937 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
939 char buf[ SLAP_TEXT_BUFLEN ];
941 snprintf( buf, sizeof( buf ),
942 "%s meta_send_entry(\"%s\"): "
943 "slap_bv2undef_ad(%s): %s\n",
944 op->o_log_prefix, ent.e_name.bv_val,
945 mapped.bv_val, text );
947 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
953 /* no subschemaSubentry */
954 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
955 || attr->a_desc == slap_schema.si_ad_entryDN )
959 * We eat target's subschemaSubentry because
960 * a search for this value is likely not
961 * to resolve to the appropriate backend;
962 * later, the local subschemaSubentry is
965 * We also eat entryDN because the frontend
966 * will reattach it without checking if already
969 ( void )ber_scanf( &ber, "x" /* [W] */ );
975 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
976 || attr->a_vals == NULL )
978 attr->a_vals = (struct berval *)&slap_dummy_bv;
981 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
985 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
986 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
988 if ( !validate && !pretty ) {
993 if ( attr->a_desc == slap_schema.si_ad_objectClass
994 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
998 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
999 ldap_back_map( &mi->mi_targets[ target ].mt_rwmap.rwm_oc,
1000 bv, &mapped, BACKLDAP_REMAP );
1001 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
1007 *bv = attr->a_vals[ last ];
1008 BER_BVZERO( &attr->a_vals[ last ] );
1011 } else if ( mapped.bv_val != bv->bv_val ) {
1013 ber_dupbv( bv, &mapped );
1017 * It is necessary to try to rewrite attributes with
1018 * dn syntax because they might be used in ACLs as
1019 * members of groups; since ACLs are applied to the
1020 * rewritten stuff, no dn-based subecj clause could
1021 * be used at the ldap backend side (see
1022 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1023 * The problem can be overcome by moving the dn-based
1024 * ACLs to the target directory server, and letting
1025 * everything pass thru the ldap backend.
1030 if ( attr->a_desc->ad_type->sat_syntax ==
1031 slap_schema.si_syn_distinguishedName )
1033 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
1035 } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
1036 ldap_back_referral_result_rewrite( &dc, attr->a_vals );
1040 for ( i = 0; i < last; i++ ) {
1045 rc = pretty( attr->a_desc->ad_type->sat_syntax,
1046 &attr->a_vals[i], &pval, NULL );
1049 rc = validate( attr->a_desc->ad_type->sat_syntax,
1054 LBER_FREE( attr->a_vals[i].bv_val );
1055 if ( --last == i ) {
1056 BER_BVZERO( &attr->a_vals[ i ] );
1059 attr->a_vals[i] = attr->a_vals[last];
1060 BER_BVZERO( &attr->a_vals[last] );
1066 LBER_FREE( attr->a_vals[i].bv_val );
1067 attr->a_vals[i] = pval;
1071 if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
1077 if ( last && attr->a_desc->ad_type->sat_equality &&
1078 attr->a_desc->ad_type->sat_equality->smr_normalize )
1082 attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
1083 for ( i = 0; i<last; i++ ) {
1084 attr->a_desc->ad_type->sat_equality->smr_normalize(
1085 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1086 attr->a_desc->ad_type->sat_syntax,
1087 attr->a_desc->ad_type->sat_equality,
1088 &attr->a_vals[i], &attr->a_nvals[i],
1091 BER_BVZERO( &attr->a_nvals[i] );
1094 attr->a_nvals = attr->a_vals;
1098 attrp = &attr->a_next;
1101 rs->sr_entry = &ent;
1102 rs->sr_attrs = op->ors_attrs;
1104 send_search_entry( op, rs );
1105 rs->sr_entry = NULL;
1106 rs->sr_attrs = NULL;
1108 if ( !BER_BVISNULL( &ent.e_name ) ) {
1109 free( ent.e_name.bv_val );
1110 BER_BVZERO( &ent.e_name );
1112 if ( !BER_BVISNULL( &ent.e_nname ) ) {
1113 free( ent.e_nname.bv_val );
1114 BER_BVZERO( &ent.e_nname );
1116 entry_clean( &ent );
1118 return LDAP_SUCCESS;