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;
65 /* should we check return values? */
66 if ( op->ors_deref != -1 ) {
67 ldap_set_option( msc->msc_ld, LDAP_OPT_DEREF,
68 ( void * )&op->ors_deref);
70 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
71 ldap_set_option( msc->msc_ld, LDAP_OPT_TIMELIMIT,
72 ( void * )&op->ors_tlimit);
74 if ( op->ors_slimit != SLAP_NO_LIMIT ) {
75 ldap_set_option( msc->msc_ld, LDAP_OPT_SIZELIMIT,
76 ( void * )&op->ors_slimit);
79 dc->target = &mi->mi_targets[ candidate ];
82 * modifies the base according to the scope, if required
84 suffixlen = mi->mi_targets[ candidate ].mt_nsuffix.bv_len;
85 if ( suffixlen > op->o_req_ndn.bv_len ) {
86 switch ( op->ors_scope ) {
87 case LDAP_SCOPE_SUBTREE:
89 * make the target suffix the new base
90 * FIXME: this is very forgiving, because
91 * "illegal" searchBases may be turned
92 * into the suffix of the target; however,
93 * the requested searchBase already passed
94 * thru the candidate analyzer...
96 if ( dnIsSuffix( &mi->mi_targets[ candidate ].mt_nsuffix,
99 realbase = mi->mi_targets[ candidate ].mt_nsuffix;
103 * this target is no longer candidate
109 case LDAP_SCOPE_ONELEVEL:
111 struct berval rdn = mi->mi_targets[ candidate ].mt_nsuffix;
112 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
113 if ( dnIsOneLevelRDN( &rdn )
114 && dnIsSuffix( &mi->mi_targets[ candidate ].mt_nsuffix, &op->o_req_ndn ) )
117 * if there is exactly one level,
118 * make the target suffix the new
119 * base, and make scope "base"
121 realbase = mi->mi_targets[ candidate ].mt_nsuffix;
122 realscope = LDAP_SCOPE_BASE;
124 } /* else continue with the next case */
127 case LDAP_SCOPE_BASE:
129 * this target is no longer candidate
136 * Rewrite the search base, if required
138 dc->ctx = "searchBase";
139 switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
143 case REWRITE_REGEXEC_UNWILLING:
144 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
145 rs->sr_text = "Operation not allowed";
146 send_ldap_result( op, rs );
149 case REWRITE_REGEXEC_ERR:
152 * this target is no longer candidate
160 rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
161 &mfilter, BACKLDAP_MAP );
166 case LDAP_COMPARE_FALSE:
169 * this target is no longer candidate
176 * Maps required attributes
178 rc = ldap_back_map_attrs( &mi->mi_targets[ candidate ].mt_rwmap.rwm_at,
179 op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
180 if ( rc != LDAP_SUCCESS ) {
182 * this target is no longer candidate
191 rc = ldap_search_ext( msc->msc_ld,
192 mbase.bv_val, realscope, mfilter.bv_val,
193 mapped_attrs, op->ors_attrsonly,
194 op->o_ctrls, NULL, NULL, op->ors_slimit,
195 &candidates[ candidate ].sr_msgid );
196 if ( rc == LDAP_SUCCESS ) {
200 candidates[ candidate ].sr_msgid = -1;
205 if ( mapped_attrs ) {
206 free( mapped_attrs );
208 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
209 free( mfilter.bv_val );
211 if ( mbase.bv_val != realbase.bv_val ) {
212 free( mbase.bv_val );
219 meta_back_search( Operation *op, SlapReply *rs )
221 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
223 metasingleconn_t *msc;
224 struct timeval tv = { 0, 0 };
225 LDAPMessage *res = NULL, *e;
226 int rc = 0, sres = LDAP_SUCCESS;
227 char *matched = NULL;
228 int i, last = 0, ncandidates = 0,
229 initial_candidates = 0, candidate_match = 0;
233 SlapReply *candidates = meta_back_candidates_get( op );
236 * controls are set in ldap_back_dobind()
238 * FIXME: in case of values return filter, we might want
239 * to map attrs and maybe rewrite value
241 mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_SENDERR );
242 if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
246 dc.conn = op->o_conn;
252 for ( i = 0, msc = &mc->mc_conns[ 0 ]; !META_LAST( msc ); ++i, ++msc ) {
253 candidates[ i ].sr_msgid = -1;
255 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
258 candidates[ i ].sr_err = LDAP_SUCCESS;
259 candidates[ i ].sr_matched = NULL;
260 candidates[ i ].sr_text = NULL;
261 candidates[ i ].sr_ref = NULL;
262 candidates[ i ].sr_ctrls = NULL;
264 switch ( meta_back_search_start( op, rs, &dc, msc, i, candidates ) )
279 initial_candidates = ncandidates;
286 for ( i = 0; i < mi->mi_ntargets; i++ ) {
287 if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
295 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: ncandidates=%d "
296 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
300 if ( initial_candidates == 0 ) {
301 send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT, NULL );
302 /* FIXME: find a way to look up the best match */
304 rc = LDAP_NO_SUCH_OBJECT;
308 /* We pull apart the ber result, stuff it into a slapd entry, and
309 * let send_search_entry stuff it back into ber format. Slow & ugly,
310 * but this is necessary for version matching, and for ACL processing.
314 * In case there are no candidates, no cycle takes place...
316 * FIXME: we might use a queue, to better balance the load
317 * among the candidates
319 for ( rc = 0; ncandidates > 0; ) {
320 int gotit = 0, doabandon = 0;
322 for ( i = 0, msc = &mc->mc_conns[ 0 ]; !META_LAST( msc ); msc++, i++ ) {
323 if ( candidates[ i ].sr_msgid == -1 ) {
327 /* check for abandon */
328 if ( op->o_abandon ) {
332 if ( op->ors_slimit > 0 && rs->sr_nentries == op->ors_slimit )
334 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
335 savepriv = op->o_private;
336 op->o_private = (void *)i;
337 send_ldap_result( op, rs );
338 op->o_private = savepriv;
343 * FIXME: handle time limit as well?
344 * Note that target servers are likely
345 * to handle it, so at some time we'll
346 * get a LDAP_TIMELIMIT_EXCEEDED from
349 rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
353 /* timeout exceeded */
355 /* FIXME: res should not need to be freed */
356 assert( res == NULL );
360 } else if ( rc == -1 ) {
362 /* something REALLY bad happened! */
363 ( void )meta_clear_unused_candidates( op, -1 );
364 rs->sr_err = LDAP_OTHER;
365 savepriv = op->o_private;
366 op->o_private = (void *)i;
367 send_ldap_result( op, rs );
368 op->o_private = savepriv;
370 /* anything else needs be done? */
372 /* FIXME: res should not need to be freed */
373 assert( res == NULL );
377 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
380 e = ldap_first_entry( msc->msc_ld, res );
381 savepriv = op->o_private;
382 op->o_private = (void *)i;
383 meta_send_entry( op, rs, mc, i, e );
384 op->o_private = savepriv;
393 * If scope is BASE, we need to jump out
394 * as soon as one entry is found; if
395 * the target pool is properly crafted,
396 * this should correspond to the sole
397 * entry that has the base DN
399 /* FIXME: this defeats the purpose of
400 * doing a search with scope == base and
401 * sizelimit = 1 to determine if a
402 * candidate is actually unique */
403 if ( op->ors_scope == LDAP_SCOPE_BASE
404 && rs->sr_nentries > 0 )
413 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
414 char **references = NULL;
419 rc = ldap_parse_reference( msc->msc_ld, res,
420 &references, &rs->sr_ctrls, 1 );
423 if ( rc != LDAP_SUCCESS ) {
427 if ( references == NULL ) {
431 #ifdef ENABLE_REWRITE
432 dc.ctx = "referralDN";
433 #else /* ! ENABLE_REWRITE */
436 #endif /* ! ENABLE_REWRITE */
438 /* FIXME: merge all and return at the end */
440 for ( cnt = 0; references[ cnt ]; cnt++ )
443 rs->sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
445 for ( cnt = 0; references[ cnt ]; cnt++ ) {
446 ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
448 BER_BVZERO( &rs->sr_ref[ cnt ] );
450 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref );
452 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
453 /* ignore return value by now */
454 savepriv = op->o_private;
455 op->o_private = (void *)i;
456 ( void )send_search_reference( op, rs );
457 op->o_private = savepriv;
459 ber_bvarray_free( rs->sr_ref );
465 ldap_value_free( references );
468 if ( rs->sr_ctrls ) {
469 ldap_controls_free( rs->sr_ctrls );
473 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
474 char buf[ SLAP_TEXT_BUFLEN ];
475 char **references = NULL;
477 if ( ldap_parse_result( msc->msc_ld,
479 &candidates[ i ].sr_err,
480 (char **)&candidates[ i ].sr_matched,
481 NULL /* (char **)&candidates[ i ].sr_text */ ,
483 &candidates[ i ].sr_ctrls, 1 ) )
486 ldap_get_option( msc->msc_ld,
487 LDAP_OPT_ERROR_NUMBER,
489 sres = slap_map_api2result( rs );
492 rs->sr_err = candidates[ i ].sr_err;
493 sres = slap_map_api2result( rs );
496 /* massage matchedDN if need be */
497 if ( candidates[ i ].sr_matched != NULL ) {
498 if ( candidates[ i ].sr_matched[ 0 ] == '\0' ) {
499 ldap_memfree( (char *)candidates[ i ].sr_matched );
500 candidates[ i ].sr_matched = NULL;
503 struct berval match, mmatch;
505 ber_str2bv( candidates[ i ].sr_matched,
508 dc.ctx = "matchedDN";
509 dc.target = &mi->mi_targets[ i ];
511 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
512 if ( mmatch.bv_val == match.bv_val ) {
513 candidates[ i ].sr_matched = ch_strdup( mmatch.bv_val );
516 candidates[ i ].sr_matched = mmatch.bv_val;
521 ldap_memfree( match.bv_val );
525 /* just get rid of the error message, if any */
526 if ( candidates[ i ].sr_text && candidates[ i ].sr_text[ 0 ] == '\0' )
528 ldap_memfree( (char *)candidates[ i ].sr_text );
529 candidates[ i ].sr_text = NULL;
532 /* add references to array */
537 for ( cnt = 0; references[ cnt ]; cnt++ )
540 sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
542 for ( cnt = 0; references[ cnt ]; cnt++ ) {
543 ber_str2bv( references[ cnt ], 0, 1, &sr_ref[ cnt ] );
545 BER_BVZERO( &sr_ref[ cnt ] );
547 ( void )ldap_back_referral_result_rewrite( &dc, sr_ref );
550 ldap_value_free( references );
552 if ( rs->sr_v2ref == NULL ) {
553 rs->sr_v2ref = sr_ref;
556 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
557 ber_bvarray_add( &rs->sr_v2ref, &sr_ref[ cnt ] );
559 ber_memfree( sr_ref );
563 rs->sr_err = candidates[ i ].sr_err;
564 sres = slap_map_api2result( rs );
566 case LDAP_NO_SUCH_OBJECT:
567 /* is_ok is touched any time a valid
568 * (even intermediate) result is
569 * returned; as a consequence, if
570 * a candidate returns noSuchObject
571 * it is ignored and the candidate
572 * is simply demoted. */
584 snprintf( buf, sizeof( buf ),
585 "%s meta_back_search[%d] "
586 "match=\"%s\" err=%d\n",
588 candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
589 candidates[ i ].sr_err );
590 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
596 * When no candidates are left,
597 * the outer cycle finishes
599 candidates[ i ].sr_msgid = -1;
608 /* check for abandon */
609 if ( op->o_abandon || doabandon ) {
610 for ( i = 0, msc = mc->mc_conns; !META_LAST( msc ); msc++, i++ ) {
611 if ( candidates[ i ].sr_msgid != -1 ) {
612 ldap_abandon_ext( msc->msc_ld,
613 candidates[ i ].sr_msgid,
615 candidates[ i ].sr_msgid = -1;
619 if ( op->o_abandon ) {
627 tv.tv_usec = 100000; /* 0.1 s */
628 ldap_pvt_thread_yield();
638 * FIXME: need a better strategy to handle errors
640 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE );
645 * Rewrite the matched portion of the search base, if required
647 * FIXME: only the last one gets caught!
649 if ( candidate_match > 0 && rs->sr_nentries > 0 ) {
650 /* we use the first one */
651 for ( i = 0; i < mi->mi_ntargets; i++ ) {
652 if ( candidates[ i ].sr_tag == META_CANDIDATE
653 && candidates[ i ].sr_matched )
655 matched = (char *)candidates[ i ].sr_matched;
656 candidates[ i ].sr_matched = NULL;
668 for ( i = 0; i < mi->mi_ntargets; i++ ) {
669 if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
677 snprintf( buf, sizeof( buf ), "%s meta_back_search: is_scope=%d is_ok=%d cnd=\"%s\"\n",
678 op->o_log_prefix, initial_candidates, is_ok, cnd );
680 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
685 * In case we returned at least one entry, we return LDAP_SUCCESS
686 * otherwise, the latter error code we got
688 * FIXME: we should handle error codes and return the more
689 * important/reasonable
692 if ( sres == LDAP_SUCCESS && rs->sr_v2ref ) {
693 sres = LDAP_REFERRAL;
696 rs->sr_matched = matched;
697 rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
698 savepriv = op->o_private;
699 op->o_private = (void *)mi->mi_ntargets;
700 send_ldap_result( op, rs );
701 op->o_private = savepriv;
702 rs->sr_matched = NULL;
710 if ( rs->sr_v2ref ) {
711 ber_bvarray_free( rs->sr_v2ref );
714 for ( i = 0; i < mi->mi_ntargets; i++ ) {
715 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
719 if ( candidates[ i ].sr_matched ) {
720 free( (char *)candidates[ i ].sr_matched );
721 candidates[ i ].sr_matched = NULL;
724 if ( candidates[ i ].sr_text ) {
725 ldap_memfree( (char *)candidates[ i ].sr_text );
726 candidates[ i ].sr_text = NULL;
729 if ( candidates[ i ].sr_ref ) {
730 ber_bvarray_free( candidates[ i ].sr_ref );
731 candidates[ i ].sr_ref = NULL;
734 if ( candidates[ i ].sr_ctrls ) {
735 ldap_controls_free( candidates[ i ].sr_ctrls );
736 candidates[ i ].sr_ctrls = NULL;
751 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
752 struct berval a, mapped;
754 BerElement ber = *e->lm_ber;
755 Attribute *attr, **attrp;
756 struct berval *bv, bdn;
760 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
761 return LDAP_DECODING_ERROR;
765 * Rewrite the dn of the result, if needed
767 dc.target = &mi->mi_targets[ target ];
768 dc.conn = op->o_conn;
770 dc.ctx = "searchResult";
772 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &ent.e_name );
773 if ( rs->sr_err != LDAP_SUCCESS) {
778 * Note: this may fail if the target host(s) schema differs
779 * from the one known to the meta, and a DN with unknown
780 * attributes is returned.
782 * FIXME: should we log anything, or delegate to dnNormalize?
784 if ( dnNormalize( 0, NULL, NULL, &ent.e_name, &ent.e_nname,
785 op->o_tmpmemctx ) != LDAP_SUCCESS )
787 return LDAP_INVALID_DN_SYNTAX;
793 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
794 ( void )meta_dncache_update_entry( &mi->mi_cache,
795 &ent.e_nname, target );
798 attrp = &ent.e_attrs;
800 dc.ctx = "searchAttrDN";
801 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
804 ldap_back_map( &mi->mi_targets[ target ].mt_rwmap.rwm_at,
805 &a, &mapped, BACKLDAP_REMAP );
806 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
809 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
810 if ( attr == NULL ) {
816 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
818 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text )
821 char buf[ SLAP_TEXT_BUFLEN ];
823 snprintf( buf, sizeof( buf ),
824 "%s meta_send_entry(\"%s\"): "
825 "slap_bv2undef_ad(%s): %s\n",
826 op->o_log_prefix, ent.e_name.bv_val,
827 mapped.bv_val, text );
829 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
835 /* no subschemaSubentry */
836 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
839 * We eat target's subschemaSubentry because
840 * a search for this value is likely not
841 * to resolve to the appropriate backend;
842 * later, the local subschemaSubentry is
845 ( void )ber_scanf( &ber, "x" /* [W] */ );
851 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
852 || attr->a_vals == NULL )
854 attr->a_vals = (struct berval *)&slap_dummy_bv;
856 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
857 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
859 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last );
861 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
862 ldap_back_map( &mi->mi_targets[ target ].mt_rwmap.rwm_oc,
863 bv, &mapped, BACKLDAP_REMAP );
864 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
870 *bv = attr->a_vals[ last ];
871 BER_BVZERO( &attr->a_vals[ last ] );
874 } else if ( mapped.bv_val != bv->bv_val ) {
876 ber_dupbv( bv, &mapped );
880 * It is necessary to try to rewrite attributes with
881 * dn syntax because they might be used in ACLs as
882 * members of groups; since ACLs are applied to the
883 * rewritten stuff, no dn-based subecj clause could
884 * be used at the ldap backend side (see
885 * http://www.OpenLDAP.org/faq/data/cache/452.html)
886 * The problem can be overcome by moving the dn-based
887 * ACLs to the target directory server, and letting
888 * everything pass thru the ldap backend.
890 } else if ( attr->a_desc->ad_type->sat_syntax ==
891 slap_schema.si_syn_distinguishedName )
893 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
895 } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
896 ldap_back_referral_result_rewrite( &dc, attr->a_vals );
899 if ( last && attr->a_desc->ad_type->sat_equality &&
900 attr->a_desc->ad_type->sat_equality->smr_normalize ) {
903 attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
904 for ( i = 0; i<last; i++ ) {
905 attr->a_desc->ad_type->sat_equality->smr_normalize(
906 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
907 attr->a_desc->ad_type->sat_syntax,
908 attr->a_desc->ad_type->sat_equality,
909 &attr->a_vals[i], &attr->a_nvals[i],
912 BER_BVZERO( &attr->a_nvals[i] );
915 attr->a_nvals = attr->a_vals;
919 attrp = &attr->a_next;
922 rs->sr_attrs = op->ors_attrs;
924 send_search_entry( op, rs );
927 while ( ent.e_attrs ) {
929 ent.e_attrs = attr->a_next;
930 if ( attr->a_vals != &slap_dummy_bv ) {
931 if ( attr->a_nvals != attr->a_vals ) {
932 ber_bvarray_free( attr->a_nvals );
934 ber_bvarray_free( attr->a_vals );
939 if ( ent.e_dn && ent.e_dn != bdn.bv_val ) {