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;
117 cache_manager* cm = li->cm;
120 return meta_back_cache_search(op, rs);
124 * controls are set in ldap_back_dobind()
126 * FIXME: in case of values return filter, we might want
127 * to map attrs and maybe rewrite value
129 lc = meta_back_getconn( op, rs, META_OP_ALLOW_MULTIPLE,
130 &op->o_req_ndn, NULL );
132 send_ldap_result( op, rs );
136 if ( !meta_back_dobind( lc, op ) ) {
137 rs->sr_err = LDAP_OTHER;
138 send_ldap_result( op, rs );
143 * Array of message id of each target
145 msgid = ch_calloc( sizeof( int ), li->ntargets );
146 if ( msgid == NULL ) {
147 rs->sr_err = LDAP_OTHER;
148 send_ldap_result( op, rs );
152 /* if not root, get appropriate limits */
153 if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
156 ( void ) get_limits( op->o_bd, &op->o_ndn, &limit );
159 /* if no time limit requested, rely on remote server limits */
160 /* if requested limit higher than hard limit, abort */
161 if ( !isroot && op->oq_search.rs_tlimit > limit->lms_t_hard ) {
162 /* no hard limit means use soft instead */
163 if ( limit->lms_t_hard == 0
164 && limit->lms_t_soft > -1
165 && op->oq_search.rs_tlimit > limit->lms_t_soft ) {
166 op->oq_search.rs_tlimit = limit->lms_t_soft;
168 /* positive hard limit means abort */
169 } else if ( limit->lms_t_hard > 0 ) {
170 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
171 send_ldap_result( op, rs );
176 /* negative hard limit means no limit */
179 /* if no size limit requested, rely on remote server limits */
180 /* if requested limit higher than hard limit, abort */
181 if ( !isroot && op->oq_search.rs_slimit > limit->lms_s_hard ) {
182 /* no hard limit means use soft instead */
183 if ( limit->lms_s_hard == 0
184 && limit->lms_s_soft > -1
185 && op->oq_search.rs_slimit > limit->lms_s_soft ) {
186 op->oq_search.rs_slimit = limit->lms_s_soft;
188 /* positive hard limit means abort */
189 } else if ( limit->lms_s_hard > 0 ) {
190 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
191 send_ldap_result( op, rs );
196 /* negative hard limit means no limit */
200 dc.conn = op->o_conn;
206 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
207 struct berval realbase = op->o_req_dn;
208 int realscope = op->oq_search.rs_scope;
209 ber_len_t suffixlen = 0;
210 struct berval mbase = { 0, NULL };
211 struct berval mfilter = { 0, NULL };
212 char **mapped_attrs = NULL;
214 if ( lsc->candidate != META_CANDIDATE ) {
219 /* should we check return values? */
220 if ( op->oq_search.rs_deref != -1 ) {
221 ldap_set_option( lsc->ld, LDAP_OPT_DEREF,
222 ( void * )&op->oq_search.rs_deref);
224 if ( op->oq_search.rs_tlimit != -1 ) {
225 ldap_set_option( lsc->ld, LDAP_OPT_TIMELIMIT,
226 ( void * )&op->oq_search.rs_tlimit);
228 if ( op->oq_search.rs_slimit != -1 ) {
229 ldap_set_option( lsc->ld, LDAP_OPT_SIZELIMIT,
230 ( void * )&op->oq_search.rs_slimit);
233 dc.rwmap = &li->targets[ i ]->rwmap;
236 * modifies the base according to the scope, if required
238 suffixlen = li->targets[ i ]->suffix.bv_len;
239 if ( suffixlen > op->o_req_ndn.bv_len ) {
240 switch ( op->oq_search.rs_scope ) {
241 case LDAP_SCOPE_SUBTREE:
243 * make the target suffix the new base
244 * FIXME: this is very forgiving, because
245 * illegal bases may be turned into
246 * the suffix of the target.
248 if ( dnIsSuffix( &li->targets[ i ]->suffix,
250 realbase = li->targets[ i ]->suffix;
253 * this target is no longer candidate
260 case LDAP_SCOPE_ONELEVEL:
261 if ( is_one_level_rdn( li->targets[ i ]->suffix.bv_val,
262 suffixlen - op->o_req_ndn.bv_len - 1 )
263 && dnIsSuffix( &li->targets[ i ]->suffix, &op->o_req_ndn ) ) {
265 * if there is exactly one level,
266 * make the target suffix the new
267 * base, and make scope "base"
269 realbase = li->targets[ i ]->suffix;
270 realscope = LDAP_SCOPE_BASE;
272 } /* else continue with the next case */
274 case LDAP_SCOPE_BASE:
276 * this target is no longer candidate
285 * Rewrite the search base, if required
287 dc.ctx = "searchBase";
288 switch ( ldap_back_dn_massage( &dc, &realbase, &mbase ) ) {
292 case REWRITE_REGEXEC_UNWILLING:
293 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
294 rs->sr_text = "Operation not allowed";
295 send_ldap_result( op, rs );
299 case REWRITE_REGEXEC_ERR:
301 rs->sr_err = LDAP_OTHER;
302 rs->sr_text = "Rewrite error";
303 send_ldap_result( op, rs );
309 * this target is no longer candidate
318 rc = ldap_back_filter_map_rewrite( &dc,
319 op->oq_search.rs_filter,
320 &mfilter, BACKLDAP_MAP );
323 * this target is no longer candidate
330 * Maps required attributes
332 rc = ldap_back_map_attrs( &li->targets[ i ]->rwmap.rwm_at,
333 op->oq_search.rs_attrs, BACKLDAP_MAP,
335 if ( rc != LDAP_SUCCESS ) {
337 * this target is no longer candidate
346 msgid[ i ] = ldap_search( lsc->ld, mbase.bv_val, realscope,
347 mfilter.bv_val, mapped_attrs,
348 op->oq_search.rs_attrsonly );
349 if ( mapped_attrs ) {
350 free( mapped_attrs );
353 if ( mfilter.bv_val != op->oq_search.rs_filterstr.bv_val ) {
354 free( mfilter.bv_val );
355 mfilter.bv_val = NULL;
357 if ( mbase.bv_val != realbase.bv_val ) {
358 free( mbase.bv_val );
362 if ( msgid[ i ] == -1 ) {
371 initial_candidates = candidates;
373 /* We pull apart the ber result, stuff it into a slapd entry, and
374 * let send_search_entry stuff it back into ber format. Slow & ugly,
375 * but this is necessary for version matching, and for ACL processing.
380 * In case there are no candidates, no cycle takes place...
382 * FIXME: we might use a queue, to balance the load
383 * among the candidates
385 for ( rc = 0; candidates > 0; ) {
388 /* check for abandon */
391 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); lsc++, i++ ) {
392 if ( msgid[ i ] == -1 ) {
397 ldap_abandon( lsc->ld, msgid[ i ] );
402 if ( op->oq_search.rs_slimit > 0
403 && rs->sr_nentries == op->oq_search.rs_slimit ) {
404 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
405 rs->sr_v2ref = v2refs;
406 send_ldap_result( op, rs );
411 * FIXME: handle time limit as well?
412 * Note that target servers are likely
413 * to handle it, so at some time we'll
414 * get a LDAP_TIMELIMIT_EXCEEDED from
417 rc = ldap_result( lsc->ld, msgid[ i ],
423 } else if ( rc == -1 ) {
424 /* something REALLY bad happened! */
425 ( void )meta_clear_unused_candidates( li,
427 rs->sr_err = LDAP_OTHER;
428 rs->sr_v2ref = v2refs;
429 send_ldap_result( op, rs );
431 /* anything else needs be done? */
434 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
435 e = ldap_first_entry( lsc->ld, res );
436 meta_send_entry( op, rs, lc, i, e );
439 * If scope is BASE, we need to jump out
440 * as soon as one entry is found; if
441 * the target pool is properly crafted,
442 * this should correspond to the sole
443 * entry that has the base DN
445 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE
446 && rs->sr_nentries > 0 ) {
454 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
455 char **references = NULL;
459 * FIXME: should we collect references
460 * and send them alltogether at the end?
463 rc = ldap_parse_reference( lsc->ld, res,
464 &references, &rs->sr_ctrls, 1 );
466 if ( rc != LDAP_SUCCESS ) {
470 if ( references == NULL ) {
474 for ( cnt = 0; references[ cnt ]; cnt++ )
477 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
479 for ( cnt = 0; references[ cnt ]; cnt++ ) {
480 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
481 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
484 /* ignore return value by now */
485 ( void )send_search_reference( op, rs );
489 ldap_value_free( references );
490 ch_free( rs->sr_ref );
494 if ( rs->sr_ctrls ) {
495 ldap_controls_free( rs->sr_ctrls );
500 rs->sr_err = ldap_result2error( lsc->ld,
502 sres = ldap_back_map_result( rs );
506 ldap_get_option( lsc->ld,
507 LDAP_OPT_ERROR_STRING, &err );
508 if ( match.bv_val != NULL ) {
509 free( match.bv_val );
511 ldap_get_option( lsc->ld,
512 LDAP_OPT_MATCHED_DN, &match.bv_val );
515 LDAP_LOG( BACK_META, ERR,
516 "meta_back_search [%d] "
517 "match=\"%s\" err=\"%s\"\n",
518 i, match.bv_val, err );
519 #else /* !NEW_LOGGING */
520 Debug( LDAP_DEBUG_ANY,
521 "=>meta_back_search [%d] "
522 "match=\"%s\" err=\"%s\"\n",
523 i, match.bv_val, err );
524 #endif /* !NEW_LOGGING */
530 * When no candidates are left,
531 * the outer cycle finishes
545 ldap_pvt_thread_yield();
554 * FIXME: need a strategy to handle errors
556 rc = meta_back_op_result( lc, op, rs );
561 * Rewrite the matched portion of the search base, if required
563 * FIXME: only the last one gets caught!
565 if ( candidate_match == initial_candidates
566 && match.bv_val != NULL && *match.bv_val ) {
567 dc.ctx = "matchedDn";
568 dc.rwmap = &li->targets[ last ]->rwmap;
570 if ( ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
571 mmatch.bv_val = NULL;
576 * In case we returned at least one entry, we return LDAP_SUCCESS
577 * otherwise, the latter error code we got
579 * FIXME: we should handle error codes and return the more
580 * important/reasonable
582 if ( sres == LDAP_SUCCESS && v2refs ) {
583 sres = LDAP_REFERRAL;
586 rs->sr_matched = mmatch.bv_val;
587 rs->sr_v2ref = v2refs;
588 send_ldap_result( op, rs );
589 rs->sr_matched = NULL;
594 if ( match.bv_val ) {
595 if ( mmatch.bv_val != match.bv_val ) {
596 free( mmatch.bv_val );
598 free( match.bv_val );
621 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
622 struct berval a, mapped;
624 BerElement ber = *e->lm_ber;
625 Attribute *attr, **attrp;
626 struct berval dummy = { 0, NULL };
627 struct berval *bv, bdn;
631 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
632 return LDAP_DECODING_ERROR;
636 * Rewrite the dn of the result, if needed
638 dc.rwmap = &li->targets[ target ]->rwmap;
639 dc.conn = op->o_conn;
641 dc.ctx = "searchResult";
643 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &ent.e_name );
644 if ( rs->sr_err != LDAP_SUCCESS) {
649 * Note: this may fail if the target host(s) schema differs
650 * from the one known to the meta, and a DN with unknown
651 * attributes is returned.
653 * FIXME: should we log anything, or delegate to dnNormalize?
655 if ( dnNormalize( 0, NULL, NULL, &ent.e_name, &ent.e_nname,
656 &op->o_tmpmemctx ) != LDAP_SUCCESS )
658 return LDAP_INVALID_DN_SYNTAX;
664 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
665 ( void )meta_dncache_update_entry( &li->cache,
666 &ent.e_nname, target );
672 attrp = &ent.e_attrs;
674 dc.ctx = "searchAttrDN";
675 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
676 ldap_back_map( &li->targets[ target ]->rwmap.rwm_at,
677 &a, &mapped, BACKLDAP_REMAP );
678 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0' ) {
681 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
682 if ( attr == NULL ) {
688 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
690 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text )
693 LDAP_LOG( BACK_META, DETAIL1,
694 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
695 #else /* !NEW_LOGGING */
696 Debug( LDAP_DEBUG_ANY,
697 "slap_bv2undef_ad(%s): "
698 "%s\n%s", mapped.bv_val, text, "" );
699 #endif /* !NEW_LOGGING */
705 /* no subschemaSubentry */
706 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
709 * We eat target's subschemaSubentry because
710 * a search for this value is likely not
711 * to resolve to the appropriate backend;
712 * later, the local subschemaSubentry is
715 ( void )ber_scanf( &ber, "x" /* [W] */ );
721 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
722 || attr->a_vals == NULL ) {
723 attr->a_vals = &dummy;
725 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
726 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
729 for ( last = 0; attr->a_vals[ last ].bv_val; ++last );
731 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
732 ldap_back_map( &li->targets[ target ]->rwmap.rwm_oc,
733 bv, &mapped, BACKLDAP_REMAP );
734 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
740 *bv = attr->a_vals[ last ];
741 attr->a_vals[ last ].bv_val = NULL;
744 } else if ( mapped.bv_val != bv->bv_val ) {
746 ber_dupbv( bv, &mapped );
750 * It is necessary to try to rewrite attributes with
751 * dn syntax because they might be used in ACLs as
752 * members of groups; since ACLs are applied to the
753 * rewritten stuff, no dn-based subecj clause could
754 * be used at the ldap backend side (see
755 * http://www.OpenLDAP.org/faq/data/cache/452.html)
756 * The problem can be overcome by moving the dn-based
757 * ACLs to the target directory server, and letting
758 * everything pass thru the ldap backend.
760 } else if ( attr->a_desc->ad_type->sat_syntax ==
761 slap_schema.si_syn_distinguishedName ) {
762 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
766 attrp = &attr->a_next;
769 rs->sr_attrs = op->oq_search.rs_attrs;
770 send_search_entry( op, rs );
773 while ( ent.e_attrs ) {
775 ent.e_attrs = attr->a_next;
776 if ( attr->a_vals != &dummy ) {
777 ber_bvarray_free( attr->a_vals );
782 if ( ent.e_dn && ent.e_dn != bdn.bv_val ) {
799 if ( DN_SEPARATOR( rdn[ from ] ) ) {