2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 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"
35 #undef ldap_debug /* silence a warning in ldap-int.h */
37 #include "../../../libraries/libldap/ldap-int.h"
55 meta_back_search( Operation *op, SlapReply *rs )
57 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
59 struct metasingleconn *lsc;
60 struct timeval tv = { 0, 0 };
62 int rc = 0, *msgid, sres = LDAP_NO_SUCH_OBJECT;
64 struct berval match = { 0, NULL }, mmatch = { 0, NULL };
65 BerVarray v2refs = NULL;
67 int i, last = 0, candidates = 0, initial_candidates = 0,
69 struct slap_limits_set *limit = NULL;
74 * controls are set in ldap_back_dobind()
76 * FIXME: in case of values return filter, we might want
77 * to map attrs and maybe rewrite value
79 lc = meta_back_getconn( op, rs, META_OP_ALLOW_MULTIPLE,
80 &op->o_req_ndn, NULL );
82 send_ldap_result( op, rs );
86 if ( !meta_back_dobind( lc, op ) ) {
87 rs->sr_err = LDAP_OTHER;
88 send_ldap_result( op, rs );
93 * Array of message id of each target
95 msgid = ch_calloc( sizeof( int ), li->ntargets );
96 if ( msgid == NULL ) {
97 rs->sr_err = LDAP_OTHER;
98 send_ldap_result( op, rs );
102 /* if not root, get appropriate limits */
103 if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
106 ( void ) get_limits( op->o_bd, &op->o_ndn, &limit );
109 /* if no time limit requested, rely on remote server limits */
110 /* if requested limit higher than hard limit, abort */
111 if ( !isroot && op->oq_search.rs_tlimit > limit->lms_t_hard ) {
112 /* no hard limit means use soft instead */
113 if ( limit->lms_t_hard == 0
114 && limit->lms_t_soft > -1
115 && op->oq_search.rs_tlimit > limit->lms_t_soft ) {
116 op->oq_search.rs_tlimit = limit->lms_t_soft;
118 /* positive hard limit means abort */
119 } else if ( limit->lms_t_hard > 0 ) {
120 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
121 send_ldap_result( op, rs );
126 /* negative hard limit means no limit */
129 /* if no size limit requested, rely on remote server limits */
130 /* if requested limit higher than hard limit, abort */
131 if ( !isroot && op->oq_search.rs_slimit > limit->lms_s_hard ) {
132 /* no hard limit means use soft instead */
133 if ( limit->lms_s_hard == 0
134 && limit->lms_s_soft > -1
135 && op->oq_search.rs_slimit > limit->lms_s_soft ) {
136 op->oq_search.rs_slimit = limit->lms_s_soft;
138 /* positive hard limit means abort */
139 } else if ( limit->lms_s_hard > 0 ) {
140 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
141 send_ldap_result( op, rs );
146 /* negative hard limit means no limit */
150 dc.conn = op->o_conn;
156 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
157 struct berval realbase = op->o_req_dn;
158 int realscope = op->oq_search.rs_scope;
159 ber_len_t suffixlen = 0;
160 struct berval mbase = { 0, NULL };
161 struct berval mfilter = { 0, NULL };
162 char **mapped_attrs = NULL;
164 if ( lsc->candidate != META_CANDIDATE ) {
169 /* should we check return values? */
170 if ( op->oq_search.rs_deref != -1 ) {
171 ldap_set_option( lsc->ld, LDAP_OPT_DEREF,
172 ( void * )&op->oq_search.rs_deref);
174 if ( op->oq_search.rs_tlimit != -1 ) {
175 ldap_set_option( lsc->ld, LDAP_OPT_TIMELIMIT,
176 ( void * )&op->oq_search.rs_tlimit);
178 if ( op->oq_search.rs_slimit != -1 ) {
179 ldap_set_option( lsc->ld, LDAP_OPT_SIZELIMIT,
180 ( void * )&op->oq_search.rs_slimit);
183 dc.rwmap = &li->targets[ i ]->rwmap;
186 * modifies the base according to the scope, if required
188 suffixlen = li->targets[ i ]->suffix.bv_len;
189 if ( suffixlen > op->o_req_ndn.bv_len ) {
190 switch ( op->oq_search.rs_scope ) {
191 case LDAP_SCOPE_SUBTREE:
193 * make the target suffix the new base
194 * FIXME: this is very forgiving, because
195 * illegal bases may be turned into
196 * the suffix of the target.
198 if ( dnIsSuffix( &li->targets[ i ]->suffix,
200 realbase = li->targets[ i ]->suffix;
203 * this target is no longer candidate
210 case LDAP_SCOPE_ONELEVEL:
211 if ( is_one_level_rdn( li->targets[ i ]->suffix.bv_val,
212 suffixlen - op->o_req_ndn.bv_len - 1 )
213 && dnIsSuffix( &li->targets[ i ]->suffix, &op->o_req_ndn ) ) {
215 * if there is exactly one level,
216 * make the target suffix the new
217 * base, and make scope "base"
219 realbase = li->targets[ i ]->suffix;
220 realscope = LDAP_SCOPE_BASE;
222 } /* else continue with the next case */
224 case LDAP_SCOPE_BASE:
226 * this target is no longer candidate
235 * Rewrite the search base, if required
237 dc.ctx = "searchBase";
238 switch ( ldap_back_dn_massage( &dc, &realbase, &mbase ) ) {
242 case REWRITE_REGEXEC_UNWILLING:
243 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
244 rs->sr_text = "Operation not allowed";
245 send_ldap_result( op, rs );
249 case REWRITE_REGEXEC_ERR:
251 rs->sr_err = LDAP_OTHER;
252 rs->sr_text = "Rewrite error";
253 send_ldap_result( op, rs );
259 * this target is no longer candidate
268 rc = ldap_back_filter_map_rewrite( &dc,
269 op->oq_search.rs_filter,
270 &mfilter, BACKLDAP_MAP );
273 * this target is no longer candidate
280 * Maps required attributes
282 rc = ldap_back_map_attrs( &li->targets[ i ]->rwmap.rwm_at,
283 op->oq_search.rs_attrs, BACKLDAP_MAP,
285 if ( rc != LDAP_SUCCESS ) {
287 * this target is no longer candidate
296 msgid[ i ] = ldap_search( lsc->ld, mbase.bv_val, realscope,
297 mfilter.bv_val, mapped_attrs,
298 op->oq_search.rs_attrsonly );
299 if ( mapped_attrs ) {
300 free( mapped_attrs );
303 if ( mfilter.bv_val != op->oq_search.rs_filterstr.bv_val ) {
304 free( mfilter.bv_val );
305 mfilter.bv_val = NULL;
307 if ( mbase.bv_val != realbase.bv_val ) {
308 free( mbase.bv_val );
312 if ( msgid[ i ] == -1 ) {
321 initial_candidates = candidates;
323 /* We pull apart the ber result, stuff it into a slapd entry, and
324 * let send_search_entry stuff it back into ber format. Slow & ugly,
325 * but this is necessary for version matching, and for ACL processing.
330 * In case there are no candidates, no cycle takes place...
332 * FIXME: we might use a queue, to balance the load
333 * among the candidates
335 for ( rc = 0; candidates > 0; ) {
338 /* check for abandon */
341 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); lsc++, i++ ) {
342 if ( msgid[ i ] == -1 ) {
347 ldap_abandon( lsc->ld, msgid[ i ] );
352 if ( op->oq_search.rs_slimit > 0
353 && rs->sr_nentries == op->oq_search.rs_slimit ) {
354 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
355 rs->sr_v2ref = v2refs;
356 send_ldap_result( op, rs );
361 * FIXME: handle time limit as well?
362 * Note that target servers are likely
363 * to handle it, so at some time we'll
364 * get a LDAP_TIMELIMIT_EXCEEDED from
367 rc = ldap_result( lsc->ld, msgid[ i ],
373 } else if ( rc == -1 ) {
374 /* something REALLY bad happened! */
375 ( void )meta_clear_unused_candidates( li,
377 rs->sr_err = LDAP_OTHER;
378 rs->sr_v2ref = v2refs;
379 send_ldap_result( op, rs );
381 /* anything else needs be done? */
384 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
385 e = ldap_first_entry( lsc->ld, res );
386 meta_send_entry( op, rs, lc, i, e );
389 * If scope is BASE, we need to jump out
390 * as soon as one entry is found; if
391 * the target pool is properly crafted,
392 * this should correspond to the sole
393 * entry that has the base DN
395 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE
396 && rs->sr_nentries > 0 ) {
404 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
405 char **references = NULL;
409 * FIXME: should we collect references
410 * and send them alltogether at the end?
413 rc = ldap_parse_reference( lsc->ld, res,
414 &references, &rs->sr_ctrls, 1 );
416 if ( rc != LDAP_SUCCESS ) {
420 if ( references == NULL ) {
424 for ( cnt = 0; references[ cnt ]; cnt++ )
427 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
429 for ( cnt = 0; references[ cnt ]; cnt++ ) {
430 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
431 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
434 /* ignore return value by now */
435 ( void )send_search_reference( op, rs );
439 ldap_value_free( references );
440 ch_free( rs->sr_ref );
444 if ( rs->sr_ctrls ) {
445 ldap_controls_free( rs->sr_ctrls );
450 rs->sr_err = ldap_result2error( lsc->ld,
452 sres = ldap_back_map_result( rs );
456 ldap_get_option( lsc->ld,
457 LDAP_OPT_ERROR_STRING, &err );
458 if ( match.bv_val != NULL ) {
459 free( match.bv_val );
461 ldap_get_option( lsc->ld,
462 LDAP_OPT_MATCHED_DN, &match.bv_val );
465 LDAP_LOG( BACK_META, ERR,
466 "meta_back_search [%d] "
467 "match=\"%s\" err=\"%s\"\n",
468 i, match.bv_val, err );
469 #else /* !NEW_LOGGING */
470 Debug( LDAP_DEBUG_ANY,
471 "=>meta_back_search [%d] "
472 "match=\"%s\" err=\"%s\"\n",
473 i, match.bv_val, err );
474 #endif /* !NEW_LOGGING */
480 * When no candidates are left,
481 * the outer cycle finishes
495 ldap_pvt_thread_yield();
504 * FIXME: need a strategy to handle errors
506 rc = meta_back_op_result( lc, op, rs );
511 * Rewrite the matched portion of the search base, if required
513 * FIXME: only the last one gets caught!
515 if ( candidate_match == initial_candidates
516 && match.bv_val != NULL && *match.bv_val ) {
517 dc.ctx = "matchedDn";
518 dc.rwmap = &li->targets[ last ]->rwmap;
520 if ( ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
521 mmatch.bv_val = NULL;
526 * In case we returned at least one entry, we return LDAP_SUCCESS
527 * otherwise, the latter error code we got
529 * FIXME: we should handle error codes and return the more
530 * important/reasonable
532 if ( sres == LDAP_SUCCESS && v2refs ) {
533 sres = LDAP_REFERRAL;
536 rs->sr_matched = mmatch.bv_val;
537 rs->sr_v2ref = v2refs;
538 send_ldap_result( op, rs );
539 rs->sr_matched = NULL;
544 if ( match.bv_val ) {
545 if ( mmatch.bv_val != match.bv_val ) {
546 free( mmatch.bv_val );
548 free( match.bv_val );
571 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
572 struct berval a, mapped;
574 BerElement ber = *e->lm_ber;
575 Attribute *attr, **attrp;
576 struct berval dummy = { 0, NULL };
577 struct berval *bv, bdn;
581 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
582 return LDAP_DECODING_ERROR;
586 * Rewrite the dn of the result, if needed
588 dc.rwmap = &li->targets[ target ]->rwmap;
589 dc.conn = op->o_conn;
591 dc.ctx = "searchResult";
593 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &ent.e_name );
594 if ( rs->sr_err != LDAP_SUCCESS) {
599 * Note: this may fail if the target host(s) schema differs
600 * from the one known to the meta, and a DN with unknown
601 * attributes is returned.
603 * FIXME: should we log anything, or delegate to dnNormalize?
605 if ( dnNormalize( 0, NULL, NULL, &ent.e_name, &ent.e_nname,
606 &op->o_tmpmemctx ) != LDAP_SUCCESS )
608 return LDAP_INVALID_DN_SYNTAX;
614 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
615 ( void )meta_dncache_update_entry( &li->cache,
616 &ent.e_nname, target );
619 attrp = &ent.e_attrs;
621 dc.ctx = "searchAttrDN";
622 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
625 ldap_back_map( &li->targets[ target ]->rwmap.rwm_at,
626 &a, &mapped, BACKLDAP_REMAP );
627 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0' ) {
630 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
631 if ( attr == NULL ) {
637 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
639 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text )
642 LDAP_LOG( BACK_META, DETAIL1,
643 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
644 #else /* !NEW_LOGGING */
645 Debug( LDAP_DEBUG_ANY,
646 "slap_bv2undef_ad(%s): "
647 "%s\n%s", mapped.bv_val, text, "" );
648 #endif /* !NEW_LOGGING */
654 /* no subschemaSubentry */
655 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
658 * We eat target's subschemaSubentry because
659 * a search for this value is likely not
660 * to resolve to the appropriate backend;
661 * later, the local subschemaSubentry is
664 ( void )ber_scanf( &ber, "x" /* [W] */ );
670 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
671 || attr->a_vals == NULL ) {
672 attr->a_vals = &dummy;
674 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
675 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
677 for ( last = 0; attr->a_vals[ last ].bv_val; ++last );
679 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
680 ldap_back_map( &li->targets[ target ]->rwmap.rwm_oc,
681 bv, &mapped, BACKLDAP_REMAP );
682 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
688 *bv = attr->a_vals[ last ];
689 attr->a_vals[ last ].bv_val = NULL;
692 } else if ( mapped.bv_val != bv->bv_val ) {
694 ber_dupbv( bv, &mapped );
698 * It is necessary to try to rewrite attributes with
699 * dn syntax because they might be used in ACLs as
700 * members of groups; since ACLs are applied to the
701 * rewritten stuff, no dn-based subecj clause could
702 * be used at the ldap backend side (see
703 * http://www.OpenLDAP.org/faq/data/cache/452.html)
704 * The problem can be overcome by moving the dn-based
705 * ACLs to the target directory server, and letting
706 * everything pass thru the ldap backend.
708 } else if ( attr->a_desc->ad_type->sat_syntax ==
709 slap_schema.si_syn_distinguishedName ) {
710 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
713 if ( last && attr->a_desc->ad_type->sat_equality &&
714 attr->a_desc->ad_type->sat_equality->smr_normalize ) {
717 attr->a_nvals = ch_malloc((last + 1)*sizeof(struct berval));
718 for ( i = 0; i<last; i++ ) {
719 attr->a_desc->ad_type->sat_equality->smr_normalize(
720 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
721 attr->a_desc->ad_type->sat_syntax,
722 attr->a_desc->ad_type->sat_equality,
723 &attr->a_vals[i], &attr->a_nvals[i],
726 attr->a_nvals[i].bv_val = NULL;
727 attr->a_nvals[i].bv_len = 0;
729 attr->a_nvals = attr->a_vals;
733 attrp = &attr->a_next;
736 rs->sr_attrs = op->oq_search.rs_attrs;
737 send_search_entry( op, rs );
740 while ( ent.e_attrs ) {
742 ent.e_attrs = attr->a_next;
743 if ( attr->a_vals != &dummy ) {
744 ber_bvarray_free( attr->a_vals );
749 if ( ent.e_dn && ent.e_dn != bdn.bv_val ) {
766 if ( DN_SEPARATOR( rdn[ from ] ) ) {