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 };
61 LDAPMessage *res = NULL, *e;
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,
75 * controls are set in ldap_back_dobind()
77 * FIXME: in case of values return filter, we might want
78 * to map attrs and maybe rewrite value
80 lc = meta_back_getconn( op, rs, META_OP_ALLOW_MULTIPLE,
81 &op->o_req_ndn, NULL );
83 send_ldap_result( op, rs );
87 if ( !meta_back_dobind( lc, op ) ) {
88 rs->sr_err = LDAP_OTHER;
89 send_ldap_result( op, rs );
94 * Array of message id of each target
96 msgid = ch_calloc( sizeof( int ), li->ntargets );
97 if ( msgid == NULL ) {
98 rs->sr_err = LDAP_OTHER;
99 send_ldap_result( op, rs );
103 dc.conn = op->o_conn;
109 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
110 struct berval realbase = op->o_req_dn;
111 int realscope = op->ors_scope;
112 ber_len_t suffixlen = 0;
113 struct berval mbase = { 0, NULL };
114 struct berval mfilter = { 0, NULL };
115 char **mapped_attrs = NULL;
117 if ( lsc->candidate != META_CANDIDATE ) {
122 /* should we check return values? */
123 if ( op->ors_deref != -1 ) {
124 ldap_set_option( lsc->ld, LDAP_OPT_DEREF,
125 ( void * )&op->ors_deref);
127 if ( op->ors_tlimit != -1 ) {
128 ldap_set_option( lsc->ld, LDAP_OPT_TIMELIMIT,
129 ( void * )&op->ors_tlimit);
131 if ( op->ors_slimit != -1 ) {
132 ldap_set_option( lsc->ld, LDAP_OPT_SIZELIMIT,
133 ( void * )&op->ors_slimit);
136 dc.rwmap = &li->targets[ i ]->rwmap;
139 * modifies the base according to the scope, if required
141 suffixlen = li->targets[ i ]->suffix.bv_len;
142 if ( suffixlen > op->o_req_ndn.bv_len ) {
143 switch ( op->ors_scope ) {
144 case LDAP_SCOPE_SUBTREE:
146 * make the target suffix the new base
147 * FIXME: this is very forgiving, because
148 * illegal bases may be turned into
149 * the suffix of the target.
151 if ( dnIsSuffix( &li->targets[ i ]->suffix,
153 realbase = li->targets[ i ]->suffix;
158 * this target is no longer candidate
165 case LDAP_SCOPE_ONELEVEL:
166 if ( is_one_level_rdn( li->targets[ i ]->suffix.bv_val,
167 suffixlen - op->o_req_ndn.bv_len - 1 )
168 && dnIsSuffix( &li->targets[ i ]->suffix, &op->o_req_ndn ) ) {
170 * if there is exactly one level,
171 * make the target suffix the new
172 * base, and make scope "base"
174 realbase = li->targets[ i ]->suffix;
175 realscope = LDAP_SCOPE_BASE;
178 } /* else continue with the next case */
180 case LDAP_SCOPE_BASE:
182 * this target is no longer candidate
191 * Rewrite the search base, if required
193 dc.ctx = "searchBase";
194 switch ( ldap_back_dn_massage( &dc, &realbase, &mbase ) ) {
198 case REWRITE_REGEXEC_UNWILLING:
199 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
200 rs->sr_text = "Operation not allowed";
201 send_ldap_result( op, rs );
205 case REWRITE_REGEXEC_ERR:
207 rs->sr_err = LDAP_OTHER;
208 rs->sr_text = "Rewrite error";
209 send_ldap_result( op, rs );
215 * this target is no longer candidate
224 rc = ldap_back_filter_map_rewrite( &dc,
226 &mfilter, BACKLDAP_MAP );
232 case LDAP_COMPARE_FALSE:
237 * this target is no longer candidate
244 * Maps required attributes
246 rc = ldap_back_map_attrs( &li->targets[ i ]->rwmap.rwm_at,
247 op->ors_attrs, BACKLDAP_MAP,
249 if ( rc != LDAP_SUCCESS ) {
251 * this target is no longer candidate
260 msgid[ i ] = ldap_search( lsc->ld, mbase.bv_val, realscope,
261 mfilter.bv_val, mapped_attrs,
263 if ( mapped_attrs ) {
264 free( mapped_attrs );
267 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
268 free( mfilter.bv_val );
269 mfilter.bv_val = NULL;
271 if ( mbase.bv_val != realbase.bv_val ) {
272 free( mbase.bv_val );
276 if ( msgid[ i ] == -1 ) {
285 initial_candidates = candidates;
287 /* We pull apart the ber result, stuff it into a slapd entry, and
288 * let send_search_entry stuff it back into ber format. Slow & ugly,
289 * but this is necessary for version matching, and for ACL processing.
294 * In case there are no candidates, no cycle takes place...
296 * FIXME: we might use a queue, to balance the load
297 * among the candidates
299 for ( rc = 0; candidates > 0; ) {
302 /* check for abandon */
305 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); lsc++, i++ ) {
306 if ( msgid[ i ] == -1 ) {
311 ldap_abandon( lsc->ld, msgid[ i ] );
316 if ( op->ors_slimit > 0
317 && rs->sr_nentries == op->ors_slimit ) {
318 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
319 rs->sr_v2ref = v2refs;
320 send_ldap_result( op, rs );
325 * FIXME: handle time limit as well?
326 * Note that target servers are likely
327 * to handle it, so at some time we'll
328 * get a LDAP_TIMELIMIT_EXCEEDED from
331 rc = ldap_result( lsc->ld, msgid[ i ],
335 /* timeout exceeded */
337 /* FIXME: res should not need to be freed */
338 assert( res == NULL );
342 } else if ( rc == -1 ) {
343 /* something REALLY bad happened! */
344 ( void )meta_clear_unused_candidates( li,
346 rs->sr_err = LDAP_OTHER;
347 rs->sr_v2ref = v2refs;
348 send_ldap_result( op, rs );
350 /* anything else needs be done? */
352 /* FIXME: res should not need to be freed */
353 assert( res == NULL );
357 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
358 e = ldap_first_entry( lsc->ld, res );
359 meta_send_entry( op, rs, lc, i, e );
365 * If scope is BASE, we need to jump out
366 * as soon as one entry is found; if
367 * the target pool is properly crafted,
368 * this should correspond to the sole
369 * entry that has the base DN
371 if ( op->ors_scope == LDAP_SCOPE_BASE
372 && rs->sr_nentries > 0 ) {
380 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
381 char **references = NULL;
385 * FIXME: should we collect references
386 * and send them alltogether at the end?
389 rc = ldap_parse_reference( lsc->ld, res,
390 &references, &rs->sr_ctrls, 1 );
392 if ( rc != LDAP_SUCCESS ) {
396 if ( references == NULL ) {
400 for ( cnt = 0; references[ cnt ]; cnt++ )
403 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
405 for ( cnt = 0; references[ cnt ]; cnt++ ) {
406 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
407 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
410 /* ignore return value by now */
411 ( void )send_search_reference( op, rs );
415 ldap_value_free( references );
416 ch_free( rs->sr_ref );
420 if ( rs->sr_ctrls ) {
421 ldap_controls_free( rs->sr_ctrls );
429 rs->sr_err = ldap_result2error( lsc->ld,
433 sres = ldap_back_map_result( rs );
437 ldap_get_option( lsc->ld,
438 LDAP_OPT_ERROR_STRING, &err );
439 if ( match.bv_val != NULL ) {
440 free( match.bv_val );
442 ldap_get_option( lsc->ld,
443 LDAP_OPT_MATCHED_DN, &match.bv_val );
446 LDAP_LOG( BACK_META, ERR,
447 "meta_back_search [%d] "
448 "match=\"%s\" err=\"%s\"\n",
449 i, match.bv_val, err );
450 #else /* !NEW_LOGGING */
451 Debug( LDAP_DEBUG_ANY,
452 "=>meta_back_search [%d] "
453 "match=\"%s\" err=\"%s\"\n",
454 i, match.bv_val, err );
455 #endif /* !NEW_LOGGING */
461 * When no candidates are left,
462 * the outer cycle finishes
476 ldap_pvt_thread_yield();
485 * FIXME: need a strategy to handle errors
487 rc = meta_back_op_result( lc, op, rs );
492 * Rewrite the matched portion of the search base, if required
494 * FIXME: only the last one gets caught!
496 if ( candidate_match == initial_candidates
497 && match.bv_val != NULL && *match.bv_val ) {
498 dc.ctx = "matchedDN";
499 dc.rwmap = &li->targets[ last ]->rwmap;
501 if ( ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
502 mmatch.bv_val = NULL;
507 * In case we returned at least one entry, we return LDAP_SUCCESS
508 * otherwise, the latter error code we got
510 * FIXME: we should handle error codes and return the more
511 * important/reasonable
513 if ( is_scope == 0 ) {
514 sres = LDAP_NO_SUCH_OBJECT;
517 if ( sres == LDAP_SUCCESS && v2refs ) {
518 sres = LDAP_REFERRAL;
521 rs->sr_matched = mmatch.bv_val;
522 rs->sr_v2ref = v2refs;
523 send_ldap_result( op, rs );
524 rs->sr_matched = NULL;
529 if ( match.bv_val ) {
530 if ( mmatch.bv_val != match.bv_val ) {
531 free( mmatch.bv_val );
533 free( match.bv_val );
556 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
557 struct berval a, mapped;
559 BerElement ber = *e->lm_ber;
560 Attribute *attr, **attrp;
561 struct berval dummy = { 0, NULL };
562 struct berval *bv, bdn;
566 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
567 return LDAP_DECODING_ERROR;
571 * Rewrite the dn of the result, if needed
573 dc.rwmap = &li->targets[ target ]->rwmap;
574 dc.conn = op->o_conn;
576 dc.ctx = "searchResult";
578 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &ent.e_name );
579 if ( rs->sr_err != LDAP_SUCCESS) {
584 * Note: this may fail if the target host(s) schema differs
585 * from the one known to the meta, and a DN with unknown
586 * attributes is returned.
588 * FIXME: should we log anything, or delegate to dnNormalize?
590 if ( dnNormalize( 0, NULL, NULL, &ent.e_name, &ent.e_nname,
591 &op->o_tmpmemctx ) != LDAP_SUCCESS )
593 return LDAP_INVALID_DN_SYNTAX;
599 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
600 ( void )meta_dncache_update_entry( &li->cache,
601 &ent.e_nname, target );
604 attrp = &ent.e_attrs;
606 dc.ctx = "searchAttrDN";
607 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
610 ldap_back_map( &li->targets[ target ]->rwmap.rwm_at,
611 &a, &mapped, BACKLDAP_REMAP );
612 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0' ) {
615 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
616 if ( attr == NULL ) {
622 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
624 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text )
627 LDAP_LOG( BACK_META, DETAIL1,
628 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
629 #else /* !NEW_LOGGING */
630 Debug( LDAP_DEBUG_ANY,
631 "slap_bv2undef_ad(%s): "
632 "%s\n%s", mapped.bv_val, text, "" );
633 #endif /* !NEW_LOGGING */
639 /* no subschemaSubentry */
640 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
643 * We eat target's subschemaSubentry because
644 * a search for this value is likely not
645 * to resolve to the appropriate backend;
646 * later, the local subschemaSubentry is
649 ( void )ber_scanf( &ber, "x" /* [W] */ );
655 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
656 || attr->a_vals == NULL ) {
657 attr->a_vals = &dummy;
659 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
660 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
662 for ( last = 0; attr->a_vals[ last ].bv_val; ++last );
664 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
665 ldap_back_map( &li->targets[ target ]->rwmap.rwm_oc,
666 bv, &mapped, BACKLDAP_REMAP );
667 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
673 *bv = attr->a_vals[ last ];
674 attr->a_vals[ last ].bv_val = NULL;
677 } else if ( mapped.bv_val != bv->bv_val ) {
679 ber_dupbv( bv, &mapped );
683 * It is necessary to try to rewrite attributes with
684 * dn syntax because they might be used in ACLs as
685 * members of groups; since ACLs are applied to the
686 * rewritten stuff, no dn-based subecj clause could
687 * be used at the ldap backend side (see
688 * http://www.OpenLDAP.org/faq/data/cache/452.html)
689 * The problem can be overcome by moving the dn-based
690 * ACLs to the target directory server, and letting
691 * everything pass thru the ldap backend.
693 } else if ( attr->a_desc->ad_type->sat_syntax ==
694 slap_schema.si_syn_distinguishedName ) {
695 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
698 if ( last && attr->a_desc->ad_type->sat_equality &&
699 attr->a_desc->ad_type->sat_equality->smr_normalize ) {
702 attr->a_nvals = ch_malloc((last + 1)*sizeof(struct berval));
703 for ( i = 0; i<last; i++ ) {
704 attr->a_desc->ad_type->sat_equality->smr_normalize(
705 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
706 attr->a_desc->ad_type->sat_syntax,
707 attr->a_desc->ad_type->sat_equality,
708 &attr->a_vals[i], &attr->a_nvals[i],
711 attr->a_nvals[i].bv_val = NULL;
712 attr->a_nvals[i].bv_len = 0;
714 attr->a_nvals = attr->a_vals;
718 attrp = &attr->a_next;
721 rs->sr_attrs = op->ors_attrs;
722 send_search_entry( op, rs );
725 while ( ent.e_attrs ) {
727 ent.e_attrs = attr->a_next;
728 if ( attr->a_vals != &dummy ) {
729 ber_bvarray_free( attr->a_vals );
734 if ( ent.e_dn && ent.e_dn != bdn.bv_val ) {
751 if ( DN_SEPARATOR( rdn[ from ] ) ) {