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_SUCCESS;
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;
77 * controls are set in ldap_back_dobind()
79 * FIXME: in case of values return filter, we might want
80 * to map attrs and maybe rewrite value
82 lc = meta_back_getconn( op, rs, META_OP_ALLOW_MULTIPLE,
83 &op->o_req_ndn, NULL );
85 send_ldap_result( op, rs );
89 if ( !meta_back_dobind( lc, op ) ) {
90 rs->sr_err = LDAP_OTHER;
91 send_ldap_result( op, rs );
96 * Array of message id of each target
98 msgid = ch_calloc( sizeof( int ), li->ntargets );
99 if ( msgid == NULL ) {
100 rs->sr_err = LDAP_OTHER;
101 send_ldap_result( op, rs );
105 /* if not root, get appropriate limits */
106 if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
109 ( void ) get_limits( op, &op->o_ndn, &limit );
112 /* if no time limit requested, rely on remote server limits */
113 /* if requested limit higher than hard limit, abort */
114 if ( !isroot && op->oq_search.rs_tlimit > limit->lms_t_hard ) {
115 /* no hard limit means use soft instead */
116 if ( limit->lms_t_hard == 0
117 && limit->lms_t_soft > -1
118 && op->oq_search.rs_tlimit > limit->lms_t_soft ) {
119 op->oq_search.rs_tlimit = limit->lms_t_soft;
121 /* positive hard limit means abort */
122 } else if ( limit->lms_t_hard > 0 ) {
123 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
124 send_ldap_result( op, rs );
129 /* negative hard limit means no limit */
132 /* if no size limit requested, rely on remote server limits */
133 /* if requested limit higher than hard limit, abort */
134 if ( !isroot && op->oq_search.rs_slimit > limit->lms_s_hard ) {
135 /* no hard limit means use soft instead */
136 if ( limit->lms_s_hard == 0
137 && limit->lms_s_soft > -1
138 && op->oq_search.rs_slimit > limit->lms_s_soft ) {
139 op->oq_search.rs_slimit = limit->lms_s_soft;
141 /* positive hard limit means abort */
142 } else if ( limit->lms_s_hard > 0 ) {
143 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
144 send_ldap_result( op, rs );
149 /* negative hard limit means no limit */
153 dc.conn = op->o_conn;
159 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
160 struct berval realbase = op->o_req_dn;
161 int realscope = op->oq_search.rs_scope;
162 ber_len_t suffixlen = 0;
163 struct berval mbase = { 0, NULL };
164 struct berval mfilter = { 0, NULL };
165 char **mapped_attrs = NULL;
167 if ( lsc->candidate != META_CANDIDATE ) {
172 /* should we check return values? */
173 if ( op->oq_search.rs_deref != -1 ) {
174 ldap_set_option( lsc->ld, LDAP_OPT_DEREF,
175 ( void * )&op->oq_search.rs_deref);
177 if ( op->oq_search.rs_tlimit != -1 ) {
178 ldap_set_option( lsc->ld, LDAP_OPT_TIMELIMIT,
179 ( void * )&op->oq_search.rs_tlimit);
181 if ( op->oq_search.rs_slimit != -1 ) {
182 ldap_set_option( lsc->ld, LDAP_OPT_SIZELIMIT,
183 ( void * )&op->oq_search.rs_slimit);
186 dc.rwmap = &li->targets[ i ]->rwmap;
189 * modifies the base according to the scope, if required
191 suffixlen = li->targets[ i ]->suffix.bv_len;
192 if ( suffixlen > op->o_req_ndn.bv_len ) {
193 switch ( op->oq_search.rs_scope ) {
194 case LDAP_SCOPE_SUBTREE:
196 * make the target suffix the new base
197 * FIXME: this is very forgiving, because
198 * illegal bases may be turned into
199 * the suffix of the target.
201 if ( dnIsSuffix( &li->targets[ i ]->suffix,
203 realbase = li->targets[ i ]->suffix;
208 * this target is no longer candidate
215 case LDAP_SCOPE_ONELEVEL:
216 if ( is_one_level_rdn( li->targets[ i ]->suffix.bv_val,
217 suffixlen - op->o_req_ndn.bv_len - 1 )
218 && dnIsSuffix( &li->targets[ i ]->suffix, &op->o_req_ndn ) ) {
220 * if there is exactly one level,
221 * make the target suffix the new
222 * base, and make scope "base"
224 realbase = li->targets[ i ]->suffix;
225 realscope = LDAP_SCOPE_BASE;
228 } /* else continue with the next case */
230 case LDAP_SCOPE_BASE:
232 * this target is no longer candidate
241 * Rewrite the search base, if required
243 dc.ctx = "searchBase";
244 switch ( ldap_back_dn_massage( &dc, &realbase, &mbase ) ) {
248 case REWRITE_REGEXEC_UNWILLING:
249 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
250 rs->sr_text = "Operation not allowed";
251 send_ldap_result( op, rs );
255 case REWRITE_REGEXEC_ERR:
257 rs->sr_err = LDAP_OTHER;
258 rs->sr_text = "Rewrite error";
259 send_ldap_result( op, rs );
265 * this target is no longer candidate
274 rc = ldap_back_filter_map_rewrite( &dc,
275 op->oq_search.rs_filter,
276 &mfilter, BACKLDAP_MAP );
282 case LDAP_COMPARE_FALSE:
287 * this target is no longer candidate
294 * Maps required attributes
296 rc = ldap_back_map_attrs( &li->targets[ i ]->rwmap.rwm_at,
297 op->oq_search.rs_attrs, BACKLDAP_MAP,
299 if ( rc != LDAP_SUCCESS ) {
301 * this target is no longer candidate
310 msgid[ i ] = ldap_search( lsc->ld, mbase.bv_val, realscope,
311 mfilter.bv_val, mapped_attrs,
312 op->oq_search.rs_attrsonly );
313 if ( mapped_attrs ) {
314 free( mapped_attrs );
317 if ( mfilter.bv_val != op->oq_search.rs_filterstr.bv_val ) {
318 free( mfilter.bv_val );
319 mfilter.bv_val = NULL;
321 if ( mbase.bv_val != realbase.bv_val ) {
322 free( mbase.bv_val );
326 if ( msgid[ i ] == -1 ) {
335 initial_candidates = candidates;
337 /* We pull apart the ber result, stuff it into a slapd entry, and
338 * let send_search_entry stuff it back into ber format. Slow & ugly,
339 * but this is necessary for version matching, and for ACL processing.
344 * In case there are no candidates, no cycle takes place...
346 * FIXME: we might use a queue, to balance the load
347 * among the candidates
349 for ( rc = 0; candidates > 0; ) {
352 /* check for abandon */
355 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); lsc++, i++ ) {
356 if ( msgid[ i ] == -1 ) {
361 ldap_abandon( lsc->ld, msgid[ i ] );
366 if ( op->oq_search.rs_slimit > 0
367 && rs->sr_nentries == op->oq_search.rs_slimit ) {
368 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
369 rs->sr_v2ref = v2refs;
370 send_ldap_result( op, rs );
375 * FIXME: handle time limit as well?
376 * Note that target servers are likely
377 * to handle it, so at some time we'll
378 * get a LDAP_TIMELIMIT_EXCEEDED from
381 rc = ldap_result( lsc->ld, msgid[ i ],
387 } else if ( rc == -1 ) {
388 /* something REALLY bad happened! */
389 ( void )meta_clear_unused_candidates( li,
391 rs->sr_err = LDAP_OTHER;
392 rs->sr_v2ref = v2refs;
393 send_ldap_result( op, rs );
395 /* anything else needs be done? */
398 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
399 e = ldap_first_entry( lsc->ld, res );
400 meta_send_entry( op, rs, lc, i, e );
403 * If scope is BASE, we need to jump out
404 * as soon as one entry is found; if
405 * the target pool is properly crafted,
406 * this should correspond to the sole
407 * entry that has the base DN
409 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE
410 && rs->sr_nentries > 0 ) {
418 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
419 char **references = NULL;
423 * FIXME: should we collect references
424 * and send them alltogether at the end?
427 rc = ldap_parse_reference( lsc->ld, res,
428 &references, &rs->sr_ctrls, 1 );
430 if ( rc != LDAP_SUCCESS ) {
434 if ( references == NULL ) {
438 for ( cnt = 0; references[ cnt ]; cnt++ )
441 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
443 for ( cnt = 0; references[ cnt ]; cnt++ ) {
444 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
445 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
448 /* ignore return value by now */
449 ( void )send_search_reference( op, rs );
453 ldap_value_free( references );
454 ch_free( rs->sr_ref );
458 if ( rs->sr_ctrls ) {
459 ldap_controls_free( rs->sr_ctrls );
464 rs->sr_err = ldap_result2error( lsc->ld,
466 sres = ldap_back_map_result( rs );
470 ldap_get_option( lsc->ld,
471 LDAP_OPT_ERROR_STRING, &err );
472 if ( match.bv_val != NULL ) {
473 free( match.bv_val );
475 ldap_get_option( lsc->ld,
476 LDAP_OPT_MATCHED_DN, &match.bv_val );
479 LDAP_LOG( BACK_META, ERR,
480 "meta_back_search [%d] "
481 "match=\"%s\" err=\"%s\"\n",
482 i, match.bv_val, err );
483 #else /* !NEW_LOGGING */
484 Debug( LDAP_DEBUG_ANY,
485 "=>meta_back_search [%d] "
486 "match=\"%s\" err=\"%s\"\n",
487 i, match.bv_val, err );
488 #endif /* !NEW_LOGGING */
494 * When no candidates are left,
495 * the outer cycle finishes
509 ldap_pvt_thread_yield();
518 * FIXME: need a strategy to handle errors
520 rc = meta_back_op_result( lc, op, rs );
525 * Rewrite the matched portion of the search base, if required
527 * FIXME: only the last one gets caught!
529 if ( candidate_match == initial_candidates
530 && match.bv_val != NULL && *match.bv_val ) {
531 dc.ctx = "matchedDn";
532 dc.rwmap = &li->targets[ last ]->rwmap;
534 if ( ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
535 mmatch.bv_val = NULL;
540 * In case we returned at least one entry, we return LDAP_SUCCESS
541 * otherwise, the latter error code we got
543 * FIXME: we should handle error codes and return the more
544 * important/reasonable
546 if ( is_scope == 0 ) {
547 sres = LDAP_NO_SUCH_OBJECT;
550 if ( sres == LDAP_SUCCESS && v2refs ) {
551 sres = LDAP_REFERRAL;
554 rs->sr_matched = mmatch.bv_val;
555 rs->sr_v2ref = v2refs;
556 send_ldap_result( op, rs );
557 rs->sr_matched = NULL;
562 if ( match.bv_val ) {
563 if ( mmatch.bv_val != match.bv_val ) {
564 free( mmatch.bv_val );
566 free( match.bv_val );
589 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
590 struct berval a, mapped;
592 BerElement ber = *e->lm_ber;
593 Attribute *attr, **attrp;
594 struct berval dummy = { 0, NULL };
595 struct berval *bv, bdn;
599 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
600 return LDAP_DECODING_ERROR;
604 * Rewrite the dn of the result, if needed
606 dc.rwmap = &li->targets[ target ]->rwmap;
607 dc.conn = op->o_conn;
609 dc.ctx = "searchResult";
611 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &ent.e_name );
612 if ( rs->sr_err != LDAP_SUCCESS) {
617 * Note: this may fail if the target host(s) schema differs
618 * from the one known to the meta, and a DN with unknown
619 * attributes is returned.
621 * FIXME: should we log anything, or delegate to dnNormalize?
623 if ( dnNormalize( 0, NULL, NULL, &ent.e_name, &ent.e_nname,
624 &op->o_tmpmemctx ) != LDAP_SUCCESS )
626 return LDAP_INVALID_DN_SYNTAX;
632 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
633 ( void )meta_dncache_update_entry( &li->cache,
634 &ent.e_nname, target );
637 attrp = &ent.e_attrs;
639 dc.ctx = "searchAttrDN";
640 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
643 ldap_back_map( &li->targets[ target ]->rwmap.rwm_at,
644 &a, &mapped, BACKLDAP_REMAP );
645 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0' ) {
648 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
649 if ( attr == NULL ) {
655 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
657 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text )
660 LDAP_LOG( BACK_META, DETAIL1,
661 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
662 #else /* !NEW_LOGGING */
663 Debug( LDAP_DEBUG_ANY,
664 "slap_bv2undef_ad(%s): "
665 "%s\n%s", mapped.bv_val, text, "" );
666 #endif /* !NEW_LOGGING */
672 /* no subschemaSubentry */
673 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
676 * We eat target's subschemaSubentry because
677 * a search for this value is likely not
678 * to resolve to the appropriate backend;
679 * later, the local subschemaSubentry is
682 ( void )ber_scanf( &ber, "x" /* [W] */ );
688 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
689 || attr->a_vals == NULL ) {
690 attr->a_vals = &dummy;
692 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
693 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
695 for ( last = 0; attr->a_vals[ last ].bv_val; ++last );
697 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
698 ldap_back_map( &li->targets[ target ]->rwmap.rwm_oc,
699 bv, &mapped, BACKLDAP_REMAP );
700 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
706 *bv = attr->a_vals[ last ];
707 attr->a_vals[ last ].bv_val = NULL;
710 } else if ( mapped.bv_val != bv->bv_val ) {
712 ber_dupbv( bv, &mapped );
716 * It is necessary to try to rewrite attributes with
717 * dn syntax because they might be used in ACLs as
718 * members of groups; since ACLs are applied to the
719 * rewritten stuff, no dn-based subecj clause could
720 * be used at the ldap backend side (see
721 * http://www.OpenLDAP.org/faq/data/cache/452.html)
722 * The problem can be overcome by moving the dn-based
723 * ACLs to the target directory server, and letting
724 * everything pass thru the ldap backend.
726 } else if ( attr->a_desc->ad_type->sat_syntax ==
727 slap_schema.si_syn_distinguishedName ) {
728 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
731 if ( last && attr->a_desc->ad_type->sat_equality &&
732 attr->a_desc->ad_type->sat_equality->smr_normalize ) {
735 attr->a_nvals = ch_malloc((last + 1)*sizeof(struct berval));
736 for ( i = 0; i<last; i++ ) {
737 attr->a_desc->ad_type->sat_equality->smr_normalize(
738 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
739 attr->a_desc->ad_type->sat_syntax,
740 attr->a_desc->ad_type->sat_equality,
741 &attr->a_vals[i], &attr->a_nvals[i],
744 attr->a_nvals[i].bv_val = NULL;
745 attr->a_nvals[i].bv_len = 0;
747 attr->a_nvals = attr->a_vals;
751 attrp = &attr->a_next;
754 rs->sr_attrs = op->oq_search.rs_attrs;
755 send_search_entry( op, rs );
758 while ( ent.e_attrs ) {
760 ent.e_attrs = attr->a_next;
761 if ( attr->a_vals != &dummy ) {
762 ber_bvarray_free( attr->a_vals );
767 if ( ent.e_dn && ent.e_dn != bdn.bv_val ) {
784 if ( DN_SEPARATOR( rdn[ from ] ) ) {