2 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
7 * This work has been developed to fulfill the requirements
8 * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
9 * to the OpenLDAP Foundation in the hope that it may be useful
10 * to the Open Source community, but WITHOUT ANY WARRANTY.
12 * Permission is granted to anyone to use this software for any purpose
13 * on any computer system, and to alter it and redistribute it, subject
14 * to the following restrictions:
16 * 1. The author and SysNet s.n.c. are not responsible for the consequences
17 * of use of this software, no matter how awful, even if they arise from
20 * 2. The origin of this software must not be misrepresented, either by
21 * explicit claim or by omission. Since few users ever read sources,
22 * credits should appear in the documentation.
24 * 3. Altered versions must be plainly marked as such, and must not be
25 * misrepresented as being the original software. Since few users
26 * ever read sources, credits should appear in the documentation.
27 * SysNet s.n.c. cannot be responsible for the consequences of the
30 * 4. This notice may not be removed or altered.
33 * This software is based on the backend back-ldap, implemented
34 * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
35 * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
36 * contributors. The contribution of the original software to the present
37 * implementation is acknowledged in this copyright statement.
39 * A special acknowledgement goes to Howard for the overall architecture
40 * (and for borrowing large pieces of code), and to Mark, who implemented
41 * from scratch the attribute/objectclass mapping.
43 * The original copyright statement follows.
45 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
47 * Permission is granted to anyone to use this software for any purpose
48 * on any computer system, and to alter it and redistribute it, subject
49 * to the following restrictions:
51 * 1. The author is not responsible for the consequences of use of this
52 * software, no matter how awful, even if they arise from flaws in it.
54 * 2. The origin of this software must not be misrepresented, either by
55 * explicit claim or by omission. Since few users ever read sources,
56 * credits should appear in the documentation.
58 * 3. Altered versions must be plainly marked as such, and must not be
59 * misrepresented as being the original software. Since few users
60 * ever read sources, credits should appear in the
63 * 4. This notice may not be removed or altered.
71 #include <ac/socket.h>
72 #include <ac/string.h>
76 #include "../back-ldap/back-ldap.h"
77 #include "back-meta.h"
79 #undef ldap_debug /* silence a warning in ldap-int.h */
81 #include "../../../libraries/libldap/ldap-int.h"
99 meta_back_search( Operation *op, SlapReply *rs )
101 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
103 struct metasingleconn *lsc;
104 struct timeval tv = { 0, 0 };
105 LDAPMessage *res, *e;
106 int rc = 0, *msgid, sres = LDAP_NO_SUCH_OBJECT;
108 struct berval match = { 0, NULL }, mmatch = { 0, NULL };
109 BerVarray v2refs = NULL;
111 int i, last = 0, candidates = 0, initial_candidates = 0,
113 struct slap_limits_set *limit = NULL;
118 cache_manager* cm = li->cm;
121 return meta_back_cache_search(op->o_bd, op->o_conn, op,
122 &op->o_req_dn, &op->o_req_ndn,
123 op->oq_search.rs_scope,
124 op->oq_search.rs_deref,
125 op->oq_search.rs_slimit,
126 op->oq_search.rs_tlimit,
127 op->oq_search.rs_filter,
128 &op->oq_search.rs_filterstr,
129 op->oq_search.rs_attrs,
130 op->oq_search.rs_attrsonly);
132 #endif /* LDAP_CACHING */
135 * controls are set in ldap_back_dobind()
137 * FIXME: in case of values return filter, we might want
138 * to map attrs and maybe rewrite value
140 lc = meta_back_getconn( op, rs, META_OP_ALLOW_MULTIPLE,
141 &op->o_req_ndn, NULL );
143 send_ldap_result( op, rs );
147 if ( !meta_back_dobind( lc, op ) ) {
148 rs->sr_err = LDAP_OTHER;
149 send_ldap_result( op, rs );
154 * Array of message id of each target
156 msgid = ch_calloc( sizeof( int ), li->ntargets );
157 if ( msgid == NULL ) {
158 rs->sr_err = LDAP_OTHER;
159 send_ldap_result( op, rs );
163 /* if not root, get appropriate limits */
164 if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
167 ( void ) get_limits( op->o_bd, &op->o_ndn, &limit );
170 /* if no time limit requested, rely on remote server limits */
171 /* if requested limit higher than hard limit, abort */
172 if ( !isroot && op->oq_search.rs_tlimit > limit->lms_t_hard ) {
173 /* no hard limit means use soft instead */
174 if ( limit->lms_t_hard == 0
175 && limit->lms_t_soft > -1
176 && op->oq_search.rs_tlimit > limit->lms_t_soft ) {
177 op->oq_search.rs_tlimit = limit->lms_t_soft;
179 /* positive hard limit means abort */
180 } else if ( limit->lms_t_hard > 0 ) {
181 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
182 send_ldap_result( op, rs );
187 /* negative hard limit means no limit */
190 /* if no size limit requested, rely on remote server limits */
191 /* if requested limit higher than hard limit, abort */
192 if ( !isroot && op->oq_search.rs_slimit > limit->lms_s_hard ) {
193 /* no hard limit means use soft instead */
194 if ( limit->lms_s_hard == 0
195 && limit->lms_s_soft > -1
196 && op->oq_search.rs_slimit > limit->lms_s_soft ) {
197 op->oq_search.rs_slimit = limit->lms_s_soft;
199 /* positive hard limit means abort */
200 } else if ( limit->lms_s_hard > 0 ) {
201 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
202 send_ldap_result( op, rs );
207 /* negative hard limit means no limit */
211 dc.conn = op->o_conn;
217 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
218 struct berval realbase = op->o_req_dn;
219 int realscope = op->oq_search.rs_scope;
220 ber_len_t suffixlen = 0;
221 struct berval mbase = { 0, NULL };
222 struct berval mfilter = { 0, NULL };
223 char **mapped_attrs = NULL;
225 if ( lsc->candidate != META_CANDIDATE ) {
230 /* should we check return values? */
231 if ( op->oq_search.rs_deref != -1 ) {
232 ldap_set_option( lsc->ld, LDAP_OPT_DEREF,
233 ( void * )&op->oq_search.rs_deref);
235 if ( op->oq_search.rs_tlimit != -1 ) {
236 ldap_set_option( lsc->ld, LDAP_OPT_TIMELIMIT,
237 ( void * )&op->oq_search.rs_tlimit);
239 if ( op->oq_search.rs_slimit != -1 ) {
240 ldap_set_option( lsc->ld, LDAP_OPT_SIZELIMIT,
241 ( void * )&op->oq_search.rs_slimit);
244 dc.rwmap = &li->targets[ i ]->rwmap;
247 * modifies the base according to the scope, if required
249 suffixlen = li->targets[ i ]->suffix.bv_len;
250 if ( suffixlen > op->o_req_ndn.bv_len ) {
251 switch ( op->oq_search.rs_scope ) {
252 case LDAP_SCOPE_SUBTREE:
254 * make the target suffix the new base
255 * FIXME: this is very forgiving, because
256 * illegal bases may be turned into
257 * the suffix of the target.
259 if ( dnIsSuffix( &li->targets[ i ]->suffix,
261 realbase = li->targets[ i ]->suffix;
264 * this target is no longer candidate
271 case LDAP_SCOPE_ONELEVEL:
272 if ( is_one_level_rdn( li->targets[ i ]->suffix.bv_val,
273 suffixlen - op->o_req_ndn.bv_len - 1 )
274 && dnIsSuffix( &li->targets[ i ]->suffix, &op->o_req_ndn ) ) {
276 * if there is exactly one level,
277 * make the target suffix the new
278 * base, and make scope "base"
280 realbase = li->targets[ i ]->suffix;
281 realscope = LDAP_SCOPE_BASE;
283 } /* else continue with the next case */
285 case LDAP_SCOPE_BASE:
287 * this target is no longer candidate
296 * Rewrite the search base, if required
298 dc.ctx = "searchBase";
299 switch ( ldap_back_dn_massage( &dc, &realbase, &mbase ) ) {
303 case REWRITE_REGEXEC_UNWILLING:
304 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
305 rs->sr_text = "Operation not allowed";
306 send_ldap_result( op, rs );
310 case REWRITE_REGEXEC_ERR:
312 rs->sr_err = LDAP_OTHER;
313 rs->sr_text = "Rewrite error";
314 send_ldap_result( op, rs );
320 * this target is no longer candidate
329 rc = ldap_back_filter_map_rewrite( &dc,
330 op->oq_search.rs_filter,
331 &mfilter, BACKLDAP_MAP );
334 * this target is no longer candidate
341 * Maps required attributes
343 rc = ldap_back_map_attrs( &li->targets[ i ]->rwmap.rwm_at,
344 op->oq_search.rs_attrs, BACKLDAP_MAP,
346 if ( rc != LDAP_SUCCESS ) {
348 * this target is no longer candidate
357 msgid[ i ] = ldap_search( lsc->ld, mbase.bv_val, realscope,
358 mfilter.bv_val, mapped_attrs,
359 op->oq_search.rs_attrsonly );
360 if ( mapped_attrs ) {
361 free( mapped_attrs );
364 if ( mfilter.bv_val != op->oq_search.rs_filterstr.bv_val ) {
365 free( mfilter.bv_val );
366 mfilter.bv_val = NULL;
368 if ( mbase.bv_val != realbase.bv_val ) {
369 free( mbase.bv_val );
373 if ( msgid[ i ] == -1 ) {
382 initial_candidates = candidates;
384 /* We pull apart the ber result, stuff it into a slapd entry, and
385 * let send_search_entry stuff it back into ber format. Slow & ugly,
386 * but this is necessary for version matching, and for ACL processing.
391 * In case there are no candidates, no cycle takes place...
393 * FIXME: we might use a queue, to balance the load
394 * among the candidates
396 for ( rc = 0; candidates > 0; ) {
399 /* check for abandon */
402 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); lsc++, i++ ) {
403 if ( msgid[ i ] == -1 ) {
408 ldap_abandon( lsc->ld, msgid[ i ] );
413 if ( op->oq_search.rs_slimit > 0
414 && rs->sr_nentries == op->oq_search.rs_slimit ) {
415 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
416 rs->sr_v2ref = v2refs;
417 send_ldap_result( op, rs );
422 * FIXME: handle time limit as well?
423 * Note that target servers are likely
424 * to handle it, so at some time we'll
425 * get a LDAP_TIMELIMIT_EXCEEDED from
428 rc = ldap_result( lsc->ld, msgid[ i ],
434 } else if ( rc == -1 ) {
435 /* something REALLY bad happened! */
436 ( void )meta_clear_unused_candidates( li,
438 rs->sr_err = LDAP_OTHER;
439 rs->sr_v2ref = v2refs;
440 send_ldap_result( op, rs );
442 /* anything else needs be done? */
445 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
446 e = ldap_first_entry( lsc->ld, res );
447 meta_send_entry( op, rs, lc, i, e );
450 * If scope is BASE, we need to jump out
451 * as soon as one entry is found; if
452 * the target pool is properly crafted,
453 * this should correspond to the sole
454 * entry that has the base DN
456 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE
457 && rs->sr_nentries > 0 ) {
465 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
466 char **references = NULL;
470 * FIXME: should we collect references
471 * and send them alltogether at the end?
474 rc = ldap_parse_reference( lsc->ld, res,
475 &references, &rs->sr_ctrls, 1 );
477 if ( rc != LDAP_SUCCESS ) {
481 if ( references == NULL ) {
485 for ( cnt = 0; references[ cnt ]; cnt++ )
488 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
490 for ( cnt = 0; references[ cnt ]; cnt++ ) {
491 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
492 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
495 /* ignore return value by now */
496 ( void )send_search_reference( op, rs );
500 ldap_value_free( references );
501 ch_free( rs->sr_ref );
505 if ( rs->sr_ctrls ) {
506 ldap_controls_free( rs->sr_ctrls );
511 rs->sr_err = ldap_result2error( lsc->ld,
513 sres = ldap_back_map_result( rs );
517 ldap_get_option( lsc->ld,
518 LDAP_OPT_ERROR_STRING, &err );
519 if ( match.bv_val != NULL ) {
520 free( match.bv_val );
522 ldap_get_option( lsc->ld,
523 LDAP_OPT_MATCHED_DN, &match.bv_val );
526 LDAP_LOG( BACK_META, ERR,
527 "meta_back_search [%d] "
528 "match=\"%s\" err=\"%s\"\n",
529 i, match.bv_val, err );
530 #else /* !NEW_LOGGING */
531 Debug( LDAP_DEBUG_ANY,
532 "=>meta_back_search [%d] "
533 "match=\"%s\" err=\"%s\"\n",
534 i, match.bv_val, err );
535 #endif /* !NEW_LOGGING */
541 * When no candidates are left,
542 * the outer cycle finishes
556 ldap_pvt_thread_yield();
565 * FIXME: need a strategy to handle errors
567 rc = meta_back_op_result( lc, op, rs );
572 * Rewrite the matched portion of the search base, if required
574 * FIXME: only the last one gets caught!
576 if ( candidate_match == initial_candidates
577 && match.bv_val != NULL && *match.bv_val ) {
578 dc.ctx = "matchedDn";
579 dc.rwmap = &li->targets[ last ]->rwmap;
581 if ( ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
582 mmatch.bv_val = NULL;
587 * In case we returned at least one entry, we return LDAP_SUCCESS
588 * otherwise, the latter error code we got
590 * FIXME: we should handle error codes and return the more
591 * important/reasonable
593 if ( sres == LDAP_SUCCESS && v2refs ) {
594 sres = LDAP_REFERRAL;
597 rs->sr_matched = mmatch.bv_val;
598 rs->sr_v2ref = v2refs;
599 send_ldap_result( op, rs );
600 rs->sr_matched = NULL;
605 if ( match.bv_val ) {
606 if ( mmatch.bv_val != match.bv_val ) {
607 free( mmatch.bv_val );
609 free( match.bv_val );
632 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
633 struct berval a, mapped;
635 BerElement ber = *e->lm_ber;
636 Attribute *attr, **attrp;
637 struct berval dummy = { 0, NULL };
638 struct berval *bv, bdn;
642 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
643 return LDAP_DECODING_ERROR;
647 * Rewrite the dn of the result, if needed
649 dc.rwmap = &li->targets[ target ]->rwmap;
650 dc.conn = op->o_conn;
652 dc.ctx = "searchResult";
654 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &ent.e_name );
655 if ( rs->sr_err != LDAP_SUCCESS) {
660 * Note: this may fail if the target host(s) schema differs
661 * from the one known to the meta, and a DN with unknown
662 * attributes is returned.
664 * FIXME: should we log anything, or delegate to dnNormalize2?
666 if ( dnNormalize2( NULL, &ent.e_name, &ent.e_nname ) != LDAP_SUCCESS ) {
667 return LDAP_INVALID_DN_SYNTAX;
673 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
674 ( void )meta_dncache_update_entry( &li->cache,
675 &ent.e_nname, target );
681 attrp = &ent.e_attrs;
683 dc.ctx = "searchAttrDN";
684 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
685 ldap_back_map( &li->targets[ target ]->rwmap.rwm_at,
686 &a, &mapped, BACKLDAP_REMAP );
687 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0' ) {
690 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
691 if ( attr == NULL ) {
697 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
699 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text )
702 LDAP_LOG( BACK_META, DETAIL1,
703 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
704 #else /* !NEW_LOGGING */
705 Debug( LDAP_DEBUG_ANY,
706 "slap_bv2undef_ad(%s): "
707 "%s\n%s", mapped.bv_val, text, "" );
708 #endif /* !NEW_LOGGING */
714 /* no subschemaSubentry */
715 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
718 * We eat target's subschemaSubentry because
719 * a search for this value is likely not
720 * to resolve to the appropriate backend;
721 * later, the local subschemaSubentry is
724 ( void )ber_scanf( &ber, "x" /* [W] */ );
730 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
731 || attr->a_vals == NULL ) {
732 attr->a_vals = &dummy;
734 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
735 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
738 for ( last = 0; attr->a_vals[ last ].bv_val; ++last );
740 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
741 ldap_back_map( &li->targets[ target ]->rwmap.rwm_oc,
742 bv, &mapped, BACKLDAP_REMAP );
743 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
749 *bv = attr->a_vals[ last ];
750 attr->a_vals[ last ].bv_val = NULL;
753 } else if ( mapped.bv_val != bv->bv_val ) {
755 ber_dupbv( bv, &mapped );
759 * It is necessary to try to rewrite attributes with
760 * dn syntax because they might be used in ACLs as
761 * members of groups; since ACLs are applied to the
762 * rewritten stuff, no dn-based subecj clause could
763 * be used at the ldap backend side (see
764 * http://www.OpenLDAP.org/faq/data/cache/452.html)
765 * The problem can be overcome by moving the dn-based
766 * ACLs to the target directory server, and letting
767 * everything pass thru the ldap backend.
769 } else if ( attr->a_desc->ad_type->sat_syntax ==
770 slap_schema.si_syn_distinguishedName ) {
771 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
775 attrp = &attr->a_next;
778 rs->sr_attrs = op->oq_search.rs_attrs;
779 send_search_entry( op, rs );
782 while ( ent.e_attrs ) {
784 ent.e_attrs = attr->a_next;
785 if ( attr->a_vals != &dummy ) {
786 ber_bvarray_free( attr->a_vals );
791 if ( ent.e_dn && ent.e_dn != bdn.bv_val ) {
808 if ( DN_SEPARATOR( rdn[ from ] ) ) {