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"
34 #undef ldap_debug /* silence a warning in ldap-int.h */
36 #include "../../../libraries/libldap/ldap-int.h"
48 meta_back_search( Operation *op, SlapReply *rs )
50 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
52 struct metasingleconn *lsc;
53 struct timeval tv = { 0, 0 };
54 LDAPMessage *res = NULL, *e;
55 int rc = 0, *msgid, sres = LDAP_SUCCESS;
57 struct berval match = BER_BVNULL, mmatch = BER_BVNULL;
58 BerVarray v2refs = NULL;
60 int i, last = 0, candidates = 0, initial_candidates = 0,
68 * controls are set in ldap_back_dobind()
70 * FIXME: in case of values return filter, we might want
71 * to map attrs and maybe rewrite value
73 lc = meta_back_getconn( op, rs, META_OP_ALLOW_MULTIPLE,
74 &op->o_req_ndn, NULL );
76 send_ldap_result( op, rs );
80 if ( !meta_back_dobind( lc, op ) ) {
81 rs->sr_err = LDAP_OTHER;
82 send_ldap_result( op, rs );
87 * Array of message id of each target
89 msgid = ch_calloc( sizeof( int ), li->ntargets );
90 if ( msgid == NULL ) {
91 rs->sr_err = LDAP_OTHER;
92 send_ldap_result( op, rs );
102 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
103 struct berval realbase = op->o_req_dn;
104 int realscope = op->ors_scope;
105 ber_len_t suffixlen = 0;
106 struct berval mbase = BER_BVNULL;
107 struct berval mfilter = BER_BVNULL;
108 char **mapped_attrs = NULL;
110 if ( lsc->candidate != META_CANDIDATE ) {
115 /* should we check return values? */
116 if ( op->ors_deref != -1 ) {
117 ldap_set_option( lsc->ld, LDAP_OPT_DEREF,
118 ( void * )&op->ors_deref);
120 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
121 ldap_set_option( lsc->ld, LDAP_OPT_TIMELIMIT,
122 ( void * )&op->ors_tlimit);
124 if ( op->ors_slimit != SLAP_NO_LIMIT ) {
125 ldap_set_option( lsc->ld, LDAP_OPT_SIZELIMIT,
126 ( void * )&op->ors_slimit);
129 dc.rwmap = &li->targets[ i ]->rwmap;
132 * modifies the base according to the scope, if required
134 suffixlen = li->targets[ i ]->suffix.bv_len;
135 if ( suffixlen > op->o_req_ndn.bv_len ) {
136 switch ( op->ors_scope ) {
137 case LDAP_SCOPE_SUBTREE:
139 * make the target suffix the new base
140 * FIXME: this is very forgiving, because
141 * illegal bases may be turned into
142 * the suffix of the target.
144 if ( dnIsSuffix( &li->targets[ i ]->suffix,
146 realbase = li->targets[ i ]->suffix;
151 * this target is no longer candidate
158 case LDAP_SCOPE_ONELEVEL:
160 struct berval rdn = li->targets[ i ]->suffix;
161 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
162 if ( dnIsOneLevelRDN( &rdn )
163 && dnIsSuffix( &li->targets[ i ]->suffix, &op->o_req_ndn ) )
166 * if there is exactly one level,
167 * make the target suffix the new
168 * base, and make scope "base"
170 realbase = li->targets[ i ]->suffix;
171 realscope = LDAP_SCOPE_BASE;
174 } /* else continue with the next case */
177 case LDAP_SCOPE_BASE:
179 * this target is no longer candidate
188 * Rewrite the search base, if required
190 dc.ctx = "searchBase";
191 switch ( ldap_back_dn_massage( &dc, &realbase, &mbase ) ) {
195 case REWRITE_REGEXEC_UNWILLING:
196 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
197 rs->sr_text = "Operation not allowed";
198 send_ldap_result( op, rs );
202 case REWRITE_REGEXEC_ERR:
204 rs->sr_err = LDAP_OTHER;
205 rs->sr_text = "Rewrite error";
206 send_ldap_result( op, rs );
212 * this target is no longer candidate
221 rc = ldap_back_filter_map_rewrite( &dc,
223 &mfilter, BACKLDAP_MAP );
229 case LDAP_COMPARE_FALSE:
234 * this target is no longer candidate
241 * Maps required attributes
243 rc = ldap_back_map_attrs( &li->targets[ i ]->rwmap.rwm_at,
244 op->ors_attrs, BACKLDAP_MAP,
246 if ( rc != LDAP_SUCCESS ) {
248 * this target is no longer candidate
257 msgid[ i ] = ldap_search( lsc->ld, mbase.bv_val, realscope,
258 mfilter.bv_val, mapped_attrs,
260 if ( mapped_attrs ) {
261 free( mapped_attrs );
264 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
265 free( mfilter.bv_val );
266 mfilter.bv_val = NULL;
268 if ( mbase.bv_val != realbase.bv_val ) {
269 free( mbase.bv_val );
273 if ( msgid[ i ] == -1 ) {
282 initial_candidates = candidates;
284 /* We pull apart the ber result, stuff it into a slapd entry, and
285 * let send_search_entry stuff it back into ber format. Slow & ugly,
286 * but this is necessary for version matching, and for ACL processing.
291 * In case there are no candidates, no cycle takes place...
293 * FIXME: we might use a queue, to balance the load
294 * among the candidates
296 for ( rc = 0; candidates > 0; ) {
299 /* check for abandon */
302 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); lsc++, i++ ) {
303 if ( msgid[ i ] == -1 ) {
308 ldap_abandon( lsc->ld, msgid[ i ] );
313 if ( op->ors_slimit > 0
314 && rs->sr_nentries == op->ors_slimit ) {
315 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
316 rs->sr_v2ref = v2refs;
317 send_ldap_result( op, rs );
322 * FIXME: handle time limit as well?
323 * Note that target servers are likely
324 * to handle it, so at some time we'll
325 * get a LDAP_TIMELIMIT_EXCEEDED from
328 rc = ldap_result( lsc->ld, msgid[ i ],
332 /* timeout exceeded */
334 /* FIXME: res should not need to be freed */
335 assert( res == NULL );
339 } else if ( rc == -1 ) {
340 /* something REALLY bad happened! */
341 ( void )meta_clear_unused_candidates( li,
343 rs->sr_err = LDAP_OTHER;
344 rs->sr_v2ref = v2refs;
345 send_ldap_result( op, rs );
347 /* anything else needs be done? */
349 /* FIXME: res should not need to be freed */
350 assert( res == NULL );
354 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
355 e = ldap_first_entry( lsc->ld, res );
356 meta_send_entry( op, rs, lc, i, e );
362 * If scope is BASE, we need to jump out
363 * as soon as one entry is found; if
364 * the target pool is properly crafted,
365 * this should correspond to the sole
366 * entry that has the base DN
368 if ( op->ors_scope == LDAP_SCOPE_BASE
369 && rs->sr_nentries > 0 ) {
377 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
378 char **references = NULL;
382 * FIXME: should we collect references
383 * and send them alltogether at the end?
386 rc = ldap_parse_reference( lsc->ld, res,
387 &references, &rs->sr_ctrls, 1 );
390 if ( rc != LDAP_SUCCESS ) {
394 if ( references == NULL ) {
398 for ( cnt = 0; references[ cnt ]; cnt++ )
401 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
403 for ( cnt = 0; references[ cnt ]; cnt++ ) {
404 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
405 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
408 /* ignore return value by now */
409 ( void )send_search_reference( op, rs );
413 ldap_value_free( references );
414 ch_free( rs->sr_ref );
418 if ( rs->sr_ctrls ) {
419 ldap_controls_free( rs->sr_ctrls );
424 rs->sr_err = ldap_result2error( lsc->ld,
428 sres = slap_map_api2result( rs );
432 ldap_get_option( lsc->ld,
433 LDAP_OPT_ERROR_STRING, &err );
434 if ( match.bv_val != NULL ) {
435 free( match.bv_val );
437 ldap_get_option( lsc->ld,
438 LDAP_OPT_MATCHED_DN, &match.bv_val );
440 Debug( LDAP_DEBUG_ANY,
441 "=>meta_back_search [%d] "
442 "match=\"%s\" err=\"%s\"\n",
443 i, match.bv_val, err );
449 * When no candidates are left,
450 * the outer cycle finishes
464 ldap_pvt_thread_yield();
473 * FIXME: need a strategy to handle errors
475 rc = meta_back_op_result( lc, op, rs );
480 * Rewrite the matched portion of the search base, if required
482 * FIXME: only the last one gets caught!
484 if ( candidate_match == initial_candidates
485 && match.bv_val != NULL && *match.bv_val ) {
486 dc.ctx = "matchedDN";
487 dc.rwmap = &li->targets[ last ]->rwmap;
489 if ( ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
490 mmatch.bv_val = NULL;
495 * In case we returned at least one entry, we return LDAP_SUCCESS
496 * otherwise, the latter error code we got
498 * FIXME: we should handle error codes and return the more
499 * important/reasonable
501 if ( is_scope == 0 ) {
502 sres = LDAP_NO_SUCH_OBJECT;
505 if ( sres == LDAP_SUCCESS && v2refs ) {
506 sres = LDAP_REFERRAL;
509 rs->sr_matched = mmatch.bv_val;
510 rs->sr_v2ref = v2refs;
511 send_ldap_result( op, rs );
512 rs->sr_matched = NULL;
517 if ( match.bv_val ) {
518 if ( mmatch.bv_val != match.bv_val ) {
519 free( mmatch.bv_val );
521 free( match.bv_val );
544 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
545 struct berval a, mapped;
547 BerElement ber = *e->lm_ber;
548 Attribute *attr, **attrp;
549 struct berval *bv, bdn;
553 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
554 return LDAP_DECODING_ERROR;
558 * Rewrite the dn of the result, if needed
560 dc.rwmap = &li->targets[ target ]->rwmap;
561 dc.conn = op->o_conn;
563 dc.ctx = "searchResult";
565 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &ent.e_name );
566 if ( rs->sr_err != LDAP_SUCCESS) {
571 * Note: this may fail if the target host(s) schema differs
572 * from the one known to the meta, and a DN with unknown
573 * attributes is returned.
575 * FIXME: should we log anything, or delegate to dnNormalize?
577 if ( dnNormalize( 0, NULL, NULL, &ent.e_name, &ent.e_nname,
578 &op->o_tmpmemctx ) != LDAP_SUCCESS )
580 return LDAP_INVALID_DN_SYNTAX;
586 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
587 ( void )meta_dncache_update_entry( &li->cache,
588 &ent.e_nname, target );
591 attrp = &ent.e_attrs;
593 dc.ctx = "searchAttrDN";
594 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
597 ldap_back_map( &li->targets[ target ]->rwmap.rwm_at,
598 &a, &mapped, BACKLDAP_REMAP );
599 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0' ) {
602 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
603 if ( attr == NULL ) {
609 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
611 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text )
613 Debug( LDAP_DEBUG_ANY,
614 "slap_bv2undef_ad(%s): "
615 "%s\n%s", mapped.bv_val, text, "" );
621 /* no subschemaSubentry */
622 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
625 * We eat target's subschemaSubentry because
626 * a search for this value is likely not
627 * to resolve to the appropriate backend;
628 * later, the local subschemaSubentry is
631 ( void )ber_scanf( &ber, "x" /* [W] */ );
637 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
638 || attr->a_vals == NULL ) {
639 attr->a_vals = &slap_dummy_bv;
641 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
642 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
644 for ( last = 0; attr->a_vals[ last ].bv_val; ++last );
646 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
647 ldap_back_map( &li->targets[ target ]->rwmap.rwm_oc,
648 bv, &mapped, BACKLDAP_REMAP );
649 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
655 *bv = attr->a_vals[ last ];
656 attr->a_vals[ last ].bv_val = NULL;
659 } else if ( mapped.bv_val != bv->bv_val ) {
661 ber_dupbv( bv, &mapped );
665 * It is necessary to try to rewrite attributes with
666 * dn syntax because they might be used in ACLs as
667 * members of groups; since ACLs are applied to the
668 * rewritten stuff, no dn-based subecj clause could
669 * be used at the ldap backend side (see
670 * http://www.OpenLDAP.org/faq/data/cache/452.html)
671 * The problem can be overcome by moving the dn-based
672 * ACLs to the target directory server, and letting
673 * everything pass thru the ldap backend.
675 } else if ( attr->a_desc->ad_type->sat_syntax ==
676 slap_schema.si_syn_distinguishedName ) {
677 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
680 if ( last && attr->a_desc->ad_type->sat_equality &&
681 attr->a_desc->ad_type->sat_equality->smr_normalize ) {
684 attr->a_nvals = ch_malloc((last + 1)*sizeof(struct berval));
685 for ( i = 0; i<last; i++ ) {
686 attr->a_desc->ad_type->sat_equality->smr_normalize(
687 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
688 attr->a_desc->ad_type->sat_syntax,
689 attr->a_desc->ad_type->sat_equality,
690 &attr->a_vals[i], &attr->a_nvals[i],
693 attr->a_nvals[i].bv_val = NULL;
694 attr->a_nvals[i].bv_len = 0;
696 attr->a_nvals = attr->a_vals;
700 attrp = &attr->a_next;
703 rs->sr_attrs = op->ors_attrs;
705 send_search_entry( op, rs );
708 while ( ent.e_attrs ) {
710 ent.e_attrs = attr->a_next;
711 if ( attr->a_vals != &slap_dummy_bv ) {
712 ber_bvarray_free( attr->a_vals );
717 if ( ent.e_dn && ent.e_dn != bdn.bv_val ) {