2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2003 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by the Howard Chu for inclusion
17 * in OpenLDAP Software and subsequently enhanced by Pierangelo
20 /* This is an altered version */
22 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
24 * This work has been developed to fulfill the requirements
25 * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
26 * to the OpenLDAP Foundation in the hope that it may be useful
27 * to the Open Source community, but WITHOUT ANY WARRANTY.
29 * Permission is granted to anyone to use this software for any purpose
30 * on any computer system, and to alter it and redistribute it, subject
31 * to the following restrictions:
33 * 1. The author and SysNet s.n.c. are not responsible for the consequences
34 * of use of this software, no matter how awful, even if they arise from
37 * 2. The origin of this software must not be misrepresented, either by
38 * explicit claim or by omission. Since few users ever read sources,
39 * credits should appear in the documentation.
41 * 3. Altered versions must be plainly marked as such, and must not be
42 * misrepresented as being the original software. Since few users
43 * ever read sources, credits should appear in the documentation.
44 * SysNet s.n.c. cannot be responsible for the consequences of the
47 * 4. This notice may not be removed or altered.
50 * This software is based on the backend back-ldap, implemented
51 * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
52 * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
53 * contributors. The contribution of the original software to the present
54 * implementation is acknowledged in this copyright statement.
56 * A special acknowledgement goes to Howard for the overall architecture
57 * (and for borrowing large pieces of code), and to Mark, who implemented
58 * from scratch the attribute/objectclass mapping.
60 * The original copyright statement follows.
62 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
64 * Permission is granted to anyone to use this software for any purpose
65 * on any computer system, and to alter it and redistribute it, subject
66 * to the following restrictions:
68 * 1. The author is not responsible for the consequences of use of this
69 * software, no matter how awful, even if they arise from flaws in it.
71 * 2. The origin of this software must not be misrepresented, either by
72 * explicit claim or by omission. Since few users ever read sources,
73 * credits should appear in the documentation.
75 * 3. Altered versions must be plainly marked as such, and must not be
76 * misrepresented as being the original software. Since few users
77 * ever read sources, credits should appear in the
80 * 4. This notice may not be removed or altered.
88 #include <ac/socket.h>
89 #include <ac/string.h>
93 #include "../back-ldap/back-ldap.h"
94 #include "back-meta.h"
96 #undef ldap_debug /* silence a warning in ldap-int.h */
98 #include "../../../libraries/libldap/ldap-int.h"
116 meta_back_search( Operation *op, SlapReply *rs )
118 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
120 struct metasingleconn *lsc;
121 struct timeval tv = { 0, 0 };
122 LDAPMessage *res, *e;
123 int rc = 0, *msgid, sres = LDAP_NO_SUCH_OBJECT;
125 struct berval match = { 0, NULL }, mmatch = { 0, NULL };
126 BerVarray v2refs = NULL;
128 int i, last = 0, candidates = 0, initial_candidates = 0,
130 struct slap_limits_set *limit = NULL;
134 cache_manager* cm = li->cm;
137 return meta_back_cache_search(op, rs);
141 * controls are set in ldap_back_dobind()
143 * FIXME: in case of values return filter, we might want
144 * to map attrs and maybe rewrite value
146 lc = meta_back_getconn( op, rs, META_OP_ALLOW_MULTIPLE,
147 &op->o_req_ndn, NULL );
149 send_ldap_result( op, rs );
153 if ( !meta_back_dobind( lc, op ) ) {
154 rs->sr_err = LDAP_OTHER;
155 send_ldap_result( op, rs );
160 * Array of message id of each target
162 msgid = ch_calloc( sizeof( int ), li->ntargets );
163 if ( msgid == NULL ) {
164 rs->sr_err = LDAP_OTHER;
165 send_ldap_result( op, rs );
169 /* if not root, get appropriate limits */
170 if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
173 ( void ) get_limits( op->o_bd, &op->o_ndn, &limit );
176 /* if no time limit requested, rely on remote server limits */
177 /* if requested limit higher than hard limit, abort */
178 if ( !isroot && op->oq_search.rs_tlimit > limit->lms_t_hard ) {
179 /* no hard limit means use soft instead */
180 if ( limit->lms_t_hard == 0
181 && limit->lms_t_soft > -1
182 && op->oq_search.rs_tlimit > limit->lms_t_soft ) {
183 op->oq_search.rs_tlimit = limit->lms_t_soft;
185 /* positive hard limit means abort */
186 } else if ( limit->lms_t_hard > 0 ) {
187 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
188 send_ldap_result( op, rs );
193 /* negative hard limit means no limit */
196 /* if no size limit requested, rely on remote server limits */
197 /* if requested limit higher than hard limit, abort */
198 if ( !isroot && op->oq_search.rs_slimit > limit->lms_s_hard ) {
199 /* no hard limit means use soft instead */
200 if ( limit->lms_s_hard == 0
201 && limit->lms_s_soft > -1
202 && op->oq_search.rs_slimit > limit->lms_s_soft ) {
203 op->oq_search.rs_slimit = limit->lms_s_soft;
205 /* positive hard limit means abort */
206 } else if ( limit->lms_s_hard > 0 ) {
207 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
208 send_ldap_result( op, rs );
213 /* negative hard limit means no limit */
217 dc.conn = op->o_conn;
223 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
224 struct berval realbase = op->o_req_dn;
225 int realscope = op->oq_search.rs_scope;
226 ber_len_t suffixlen = 0;
227 struct berval mbase = { 0, NULL };
228 struct berval mfilter = { 0, NULL };
229 char **mapped_attrs = NULL;
231 if ( lsc->candidate != META_CANDIDATE ) {
236 /* should we check return values? */
237 if ( op->oq_search.rs_deref != -1 ) {
238 ldap_set_option( lsc->ld, LDAP_OPT_DEREF,
239 ( void * )&op->oq_search.rs_deref);
241 if ( op->oq_search.rs_tlimit != -1 ) {
242 ldap_set_option( lsc->ld, LDAP_OPT_TIMELIMIT,
243 ( void * )&op->oq_search.rs_tlimit);
245 if ( op->oq_search.rs_slimit != -1 ) {
246 ldap_set_option( lsc->ld, LDAP_OPT_SIZELIMIT,
247 ( void * )&op->oq_search.rs_slimit);
250 dc.rwmap = &li->targets[ i ]->rwmap;
253 * modifies the base according to the scope, if required
255 suffixlen = li->targets[ i ]->suffix.bv_len;
256 if ( suffixlen > op->o_req_ndn.bv_len ) {
257 switch ( op->oq_search.rs_scope ) {
258 case LDAP_SCOPE_SUBTREE:
260 * make the target suffix the new base
261 * FIXME: this is very forgiving, because
262 * illegal bases may be turned into
263 * the suffix of the target.
265 if ( dnIsSuffix( &li->targets[ i ]->suffix,
267 realbase = li->targets[ i ]->suffix;
270 * this target is no longer candidate
277 case LDAP_SCOPE_ONELEVEL:
278 if ( is_one_level_rdn( li->targets[ i ]->suffix.bv_val,
279 suffixlen - op->o_req_ndn.bv_len - 1 )
280 && dnIsSuffix( &li->targets[ i ]->suffix, &op->o_req_ndn ) ) {
282 * if there is exactly one level,
283 * make the target suffix the new
284 * base, and make scope "base"
286 realbase = li->targets[ i ]->suffix;
287 realscope = LDAP_SCOPE_BASE;
289 } /* else continue with the next case */
291 case LDAP_SCOPE_BASE:
293 * this target is no longer candidate
302 * Rewrite the search base, if required
304 dc.ctx = "searchBase";
305 switch ( ldap_back_dn_massage( &dc, &realbase, &mbase ) ) {
309 case REWRITE_REGEXEC_UNWILLING:
310 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
311 rs->sr_text = "Operation not allowed";
312 send_ldap_result( op, rs );
316 case REWRITE_REGEXEC_ERR:
318 rs->sr_err = LDAP_OTHER;
319 rs->sr_text = "Rewrite error";
320 send_ldap_result( op, rs );
326 * this target is no longer candidate
335 rc = ldap_back_filter_map_rewrite( &dc,
336 op->oq_search.rs_filter,
337 &mfilter, BACKLDAP_MAP );
340 * this target is no longer candidate
347 * Maps required attributes
349 rc = ldap_back_map_attrs( &li->targets[ i ]->rwmap.rwm_at,
350 op->oq_search.rs_attrs, BACKLDAP_MAP,
352 if ( rc != LDAP_SUCCESS ) {
354 * this target is no longer candidate
363 msgid[ i ] = ldap_search( lsc->ld, mbase.bv_val, realscope,
364 mfilter.bv_val, mapped_attrs,
365 op->oq_search.rs_attrsonly );
366 if ( mapped_attrs ) {
367 free( mapped_attrs );
370 if ( mfilter.bv_val != op->oq_search.rs_filterstr.bv_val ) {
371 free( mfilter.bv_val );
372 mfilter.bv_val = NULL;
374 if ( mbase.bv_val != realbase.bv_val ) {
375 free( mbase.bv_val );
379 if ( msgid[ i ] == -1 ) {
388 initial_candidates = candidates;
390 /* We pull apart the ber result, stuff it into a slapd entry, and
391 * let send_search_entry stuff it back into ber format. Slow & ugly,
392 * but this is necessary for version matching, and for ACL processing.
397 * In case there are no candidates, no cycle takes place...
399 * FIXME: we might use a queue, to balance the load
400 * among the candidates
402 for ( rc = 0; candidates > 0; ) {
405 /* check for abandon */
408 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); lsc++, i++ ) {
409 if ( msgid[ i ] == -1 ) {
414 ldap_abandon( lsc->ld, msgid[ i ] );
419 if ( op->oq_search.rs_slimit > 0
420 && rs->sr_nentries == op->oq_search.rs_slimit ) {
421 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
422 rs->sr_v2ref = v2refs;
423 send_ldap_result( op, rs );
428 * FIXME: handle time limit as well?
429 * Note that target servers are likely
430 * to handle it, so at some time we'll
431 * get a LDAP_TIMELIMIT_EXCEEDED from
434 rc = ldap_result( lsc->ld, msgid[ i ],
440 } else if ( rc == -1 ) {
441 /* something REALLY bad happened! */
442 ( void )meta_clear_unused_candidates( li,
444 rs->sr_err = LDAP_OTHER;
445 rs->sr_v2ref = v2refs;
446 send_ldap_result( op, rs );
448 /* anything else needs be done? */
451 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
452 e = ldap_first_entry( lsc->ld, res );
453 meta_send_entry( op, rs, lc, i, e );
456 * If scope is BASE, we need to jump out
457 * as soon as one entry is found; if
458 * the target pool is properly crafted,
459 * this should correspond to the sole
460 * entry that has the base DN
462 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE
463 && rs->sr_nentries > 0 ) {
471 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
472 char **references = NULL;
476 * FIXME: should we collect references
477 * and send them alltogether at the end?
480 rc = ldap_parse_reference( lsc->ld, res,
481 &references, &rs->sr_ctrls, 1 );
483 if ( rc != LDAP_SUCCESS ) {
487 if ( references == NULL ) {
491 for ( cnt = 0; references[ cnt ]; cnt++ )
494 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
496 for ( cnt = 0; references[ cnt ]; cnt++ ) {
497 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
498 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
501 /* ignore return value by now */
502 ( void )send_search_reference( op, rs );
506 ldap_value_free( references );
507 ch_free( rs->sr_ref );
511 if ( rs->sr_ctrls ) {
512 ldap_controls_free( rs->sr_ctrls );
517 rs->sr_err = ldap_result2error( lsc->ld,
519 sres = ldap_back_map_result( rs );
523 ldap_get_option( lsc->ld,
524 LDAP_OPT_ERROR_STRING, &err );
525 if ( match.bv_val != NULL ) {
526 free( match.bv_val );
528 ldap_get_option( lsc->ld,
529 LDAP_OPT_MATCHED_DN, &match.bv_val );
532 LDAP_LOG( BACK_META, ERR,
533 "meta_back_search [%d] "
534 "match=\"%s\" err=\"%s\"\n",
535 i, match.bv_val, err );
536 #else /* !NEW_LOGGING */
537 Debug( LDAP_DEBUG_ANY,
538 "=>meta_back_search [%d] "
539 "match=\"%s\" err=\"%s\"\n",
540 i, match.bv_val, err );
541 #endif /* !NEW_LOGGING */
547 * When no candidates are left,
548 * the outer cycle finishes
562 ldap_pvt_thread_yield();
571 * FIXME: need a strategy to handle errors
573 rc = meta_back_op_result( lc, op, rs );
578 * Rewrite the matched portion of the search base, if required
580 * FIXME: only the last one gets caught!
582 if ( candidate_match == initial_candidates
583 && match.bv_val != NULL && *match.bv_val ) {
584 dc.ctx = "matchedDn";
585 dc.rwmap = &li->targets[ last ]->rwmap;
587 if ( ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
588 mmatch.bv_val = NULL;
593 * In case we returned at least one entry, we return LDAP_SUCCESS
594 * otherwise, the latter error code we got
596 * FIXME: we should handle error codes and return the more
597 * important/reasonable
599 if ( sres == LDAP_SUCCESS && v2refs ) {
600 sres = LDAP_REFERRAL;
603 rs->sr_matched = mmatch.bv_val;
604 rs->sr_v2ref = v2refs;
605 send_ldap_result( op, rs );
606 rs->sr_matched = NULL;
611 if ( match.bv_val ) {
612 if ( mmatch.bv_val != match.bv_val ) {
613 free( mmatch.bv_val );
615 free( match.bv_val );
638 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
639 struct berval a, mapped;
641 BerElement ber = *e->lm_ber;
642 Attribute *attr, **attrp;
643 struct berval dummy = { 0, NULL };
644 struct berval *bv, bdn;
648 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
649 return LDAP_DECODING_ERROR;
653 * Rewrite the dn of the result, if needed
655 dc.rwmap = &li->targets[ target ]->rwmap;
656 dc.conn = op->o_conn;
658 dc.ctx = "searchResult";
660 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &ent.e_name );
661 if ( rs->sr_err != LDAP_SUCCESS) {
666 * Note: this may fail if the target host(s) schema differs
667 * from the one known to the meta, and a DN with unknown
668 * attributes is returned.
670 * FIXME: should we log anything, or delegate to dnNormalize?
672 if ( dnNormalize( 0, NULL, NULL, &ent.e_name, &ent.e_nname,
673 &op->o_tmpmemctx ) != LDAP_SUCCESS )
675 return LDAP_INVALID_DN_SYNTAX;
681 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
682 ( void )meta_dncache_update_entry( &li->cache,
683 &ent.e_nname, target );
689 attrp = &ent.e_attrs;
691 dc.ctx = "searchAttrDN";
692 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
695 ldap_back_map( &li->targets[ target ]->rwmap.rwm_at,
696 &a, &mapped, BACKLDAP_REMAP );
697 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0' ) {
700 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
701 if ( attr == NULL ) {
707 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
709 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text )
712 LDAP_LOG( BACK_META, DETAIL1,
713 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
714 #else /* !NEW_LOGGING */
715 Debug( LDAP_DEBUG_ANY,
716 "slap_bv2undef_ad(%s): "
717 "%s\n%s", mapped.bv_val, text, "" );
718 #endif /* !NEW_LOGGING */
724 /* no subschemaSubentry */
725 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
728 * We eat target's subschemaSubentry because
729 * a search for this value is likely not
730 * to resolve to the appropriate backend;
731 * later, the local subschemaSubentry is
734 ( void )ber_scanf( &ber, "x" /* [W] */ );
740 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
741 || attr->a_vals == NULL ) {
742 attr->a_vals = &dummy;
744 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
745 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
747 for ( last = 0; attr->a_vals[ last ].bv_val; ++last );
749 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
750 ldap_back_map( &li->targets[ target ]->rwmap.rwm_oc,
751 bv, &mapped, BACKLDAP_REMAP );
752 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
758 *bv = attr->a_vals[ last ];
759 attr->a_vals[ last ].bv_val = NULL;
762 } else if ( mapped.bv_val != bv->bv_val ) {
764 ber_dupbv( bv, &mapped );
768 * It is necessary to try to rewrite attributes with
769 * dn syntax because they might be used in ACLs as
770 * members of groups; since ACLs are applied to the
771 * rewritten stuff, no dn-based subecj clause could
772 * be used at the ldap backend side (see
773 * http://www.OpenLDAP.org/faq/data/cache/452.html)
774 * The problem can be overcome by moving the dn-based
775 * ACLs to the target directory server, and letting
776 * everything pass thru the ldap backend.
778 } else if ( attr->a_desc->ad_type->sat_syntax ==
779 slap_schema.si_syn_distinguishedName ) {
780 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
783 if ( last && attr->a_desc->ad_type->sat_equality &&
784 attr->a_desc->ad_type->sat_equality->smr_normalize ) {
787 attr->a_nvals = ch_malloc((last + 1)*sizeof(struct berval));
788 for ( i = 0; i<last; i++ ) {
789 attr->a_desc->ad_type->sat_equality->smr_normalize(
790 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
791 attr->a_desc->ad_type->sat_syntax,
792 attr->a_desc->ad_type->sat_equality,
793 &attr->a_vals[i], &attr->a_nvals[i],
796 attr->a_nvals[i].bv_val = NULL;
797 attr->a_nvals[i].bv_len = 0;
799 attr->a_nvals = attr->a_vals;
803 attrp = &attr->a_next;
806 rs->sr_attrs = op->oq_search.rs_attrs;
807 send_search_entry( op, rs );
810 while ( ent.e_attrs ) {
812 ent.e_attrs = attr->a_next;
813 if ( attr->a_vals != &dummy ) {
814 ber_bvarray_free( attr->a_vals );
819 if ( ent.e_dn && ent.e_dn != bdn.bv_val ) {
836 if ( DN_SEPARATOR( rdn[ from ] ) ) {