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;
107 char *match = NULL, *err = NULL;
109 BerVarray v2refs = NULL;
111 int i, last = 0, candidates = 0;
112 struct slap_limits_set *limit = NULL;
116 cache_manager* cm = li->cm;
119 return meta_back_cache_search(op->o_bd, op->o_conn, op,
120 &op->o_req_dn, &op->o_req_ndn,
121 op->oq_search.rs_scope,
122 op->oq_search.rs_deref,
123 op->oq_search.rs_slimit,
124 op->oq_search.rs_tlimit,
125 op->oq_search.rs_filter,
126 &op->oq_search.rs_filterstr,
127 op->oq_search.rs_attrs,
128 op->oq_search.rs_attrsonly);
130 #endif /* LDAP_CACHING */
133 * controls are set in ldap_back_dobind()
135 * FIXME: in case of values return filter, we might want
136 * to map attrs and maybe rewrite value
138 lc = meta_back_getconn( op, rs, META_OP_ALLOW_MULTIPLE,
139 &op->o_req_ndn, NULL );
141 send_ldap_result( op, rs );
145 if ( !meta_back_dobind( lc, op ) ) {
146 rs->sr_err = LDAP_OTHER;
147 send_ldap_result( op, rs );
152 * Array of message id of each target
154 msgid = ch_calloc( sizeof( int ), li->ntargets );
155 if ( msgid == NULL ) {
156 rs->sr_err = LDAP_OTHER;
157 send_ldap_result( op, rs );
161 /* if not root, get appropriate limits */
162 if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
165 ( void ) get_limits( op->o_bd, &op->o_ndn, &limit );
168 /* if no time limit requested, rely on remote server limits */
169 /* if requested limit higher than hard limit, abort */
170 if ( !isroot && op->oq_search.rs_tlimit > limit->lms_t_hard ) {
171 /* no hard limit means use soft instead */
172 if ( limit->lms_t_hard == 0
173 && limit->lms_t_soft > -1
174 && op->oq_search.rs_tlimit > limit->lms_t_soft ) {
175 op->oq_search.rs_tlimit = limit->lms_t_soft;
177 /* positive hard limit means abort */
178 } else if ( limit->lms_t_hard > 0 ) {
179 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
180 send_ldap_result( op, rs );
185 /* negative hard limit means no limit */
188 /* if no size limit requested, rely on remote server limits */
189 /* if requested limit higher than hard limit, abort */
190 if ( !isroot && op->oq_search.rs_slimit > limit->lms_s_hard ) {
191 /* no hard limit means use soft instead */
192 if ( limit->lms_s_hard == 0
193 && limit->lms_s_soft > -1
194 && op->oq_search.rs_slimit > limit->lms_s_soft ) {
195 op->oq_search.rs_slimit = limit->lms_s_soft;
197 /* positive hard limit means abort */
198 } else if ( limit->lms_s_hard > 0 ) {
199 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
200 send_ldap_result( op, rs );
205 /* negative hard limit means no limit */
211 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
212 char *realbase = ( char * )op->o_req_dn.bv_val;
213 int realscope = op->oq_search.rs_scope;
214 ber_len_t suffixlen = 0;
216 struct berval mfilter = { 0L, NULL };
217 char **mapped_attrs = NULL;
219 if ( lsc->candidate != META_CANDIDATE ) {
224 /* should we check return values? */
225 if ( op->oq_search.rs_deref != -1 ) {
226 ldap_set_option( lsc->ld, LDAP_OPT_DEREF,
227 ( void * )&op->oq_search.rs_deref);
229 if ( op->oq_search.rs_tlimit != -1 ) {
230 ldap_set_option( lsc->ld, LDAP_OPT_TIMELIMIT,
231 ( void * )&op->oq_search.rs_tlimit);
233 if ( op->oq_search.rs_slimit != -1 ) {
234 ldap_set_option( lsc->ld, LDAP_OPT_SIZELIMIT,
235 ( void * )&op->oq_search.rs_slimit);
239 * modifies the base according to the scope, if required
241 suffixlen = li->targets[ i ]->suffix.bv_len;
242 if ( suffixlen > op->o_req_ndn.bv_len ) {
243 switch ( op->oq_search.rs_scope ) {
244 case LDAP_SCOPE_SUBTREE:
246 * make the target suffix the new base
247 * FIXME: this is very forgiving, because
248 * illegal bases may be turned into
249 * the suffix of the target.
251 if ( dnIsSuffix( &li->targets[ i ]->suffix,
253 realbase = li->targets[ i ]->suffix.bv_val;
256 * this target is no longer candidate
263 case LDAP_SCOPE_ONELEVEL:
264 if ( is_one_level_rdn( li->targets[ i ]->suffix.bv_val,
265 suffixlen - op->o_req_ndn.bv_len - 1 )
266 && dnIsSuffix( &li->targets[ i ]->suffix, &op->o_req_ndn ) ) {
268 * if there is exactly one level,
269 * make the target suffix the new
270 * base, and make scope "base"
272 realbase = li->targets[ i ]->suffix.bv_val;
273 realscope = LDAP_SCOPE_BASE;
275 } /* else continue with the next case */
277 case LDAP_SCOPE_BASE:
279 * this target is no longer candidate
288 * Rewrite the search base, if required
290 switch ( rewrite_session( li->targets[ i ]->rwinfo,
292 realbase, op->o_conn, &mbase ) ) {
293 case REWRITE_REGEXEC_OK:
294 if ( mbase == NULL ) {
298 LDAP_LOG( BACK_META, DETAIL1,
299 "[rw] searchBase [%d]: \"%s\" -> \"%s\"\n",
300 i, op->o_req_dn.bv_val, mbase );
301 #else /* !NEW_LOGGING */
302 Debug( LDAP_DEBUG_ARGS,
303 "rw> searchBase [%d]: \"%s\" -> \"%s\"\n",
304 i, op->o_req_dn.bv_val, mbase );
305 #endif /* !NEW_LOGGING */
308 case REWRITE_REGEXEC_UNWILLING:
309 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
310 rs->sr_text = "Operation not allowed";
311 send_ldap_result( op, rs );
315 case REWRITE_REGEXEC_ERR:
317 rs->sr_err = LDAP_OTHER;
318 rs->sr_text = "Rewrite error";
319 send_ldap_result( op, rs );
325 * this target is no longer candidate
334 rc = ldap_back_filter_map_rewrite_( li->targets[ i ]->rwinfo,
336 &li->targets[ i ]->at_map,
337 &li->targets[ i ]->oc_map,
338 op->oq_search.rs_filter,
339 &mfilter, BACKLDAP_MAP );
342 * this target is no longer candidate
349 * Maps required attributes
351 rc = ldap_back_map_attrs( &li->targets[ i ]->at_map,
352 op->oq_search.rs_attrs, BACKLDAP_MAP,
354 if ( rc != LDAP_SUCCESS ) {
356 * this target is no longer candidate
363 if ( mapped_attrs == NULL && op->oq_search.rs_attrs) {
365 for ( count = 0; op->oq_search.rs_attrs[ count ].an_name.bv_val; count++ );
366 mapped_attrs = ch_malloc( ( count + 1 ) * sizeof(char *));
367 for ( count = 0; op->oq_search.rs_attrs[ count ].an_name.bv_val; count++ ) {
368 mapped_attrs[ count ] = op->oq_search.rs_attrs[ count ].an_name.bv_val;
370 mapped_attrs[ count ] = NULL;
377 msgid[ i ] = ldap_search( lsc->ld, mbase, realscope,
378 mfilter.bv_val, mapped_attrs,
379 op->oq_search.rs_attrsonly );
380 if ( mapped_attrs ) {
381 free( mapped_attrs );
384 if ( mfilter.bv_val != op->oq_search.rs_filterstr.bv_val ) {
385 free( mfilter.bv_val );
386 mfilter.bv_val = NULL;
388 if ( mbase != realbase ) {
393 if ( msgid[ i ] == -1 ) {
402 /* We pull apart the ber result, stuff it into a slapd entry, and
403 * let send_search_entry stuff it back into ber format. Slow & ugly,
404 * but this is necessary for version matching, and for ACL processing.
409 * In case there are no candidates, no cycle takes place...
411 * FIXME: we might use a queue, to balance the load
412 * among the candidates
414 for ( rc = 0; candidates > 0; ) {
417 /* check for abandon */
420 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); lsc++, i++ ) {
421 if ( msgid[ i ] == -1 ) {
426 ldap_abandon( lsc->ld, msgid[ i ] );
431 if ( op->oq_search.rs_slimit > 0
432 && rs->sr_nentries == op->oq_search.rs_slimit ) {
433 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
434 rs->sr_v2ref = v2refs;
435 send_ldap_result( op, rs );
440 * FIXME: handle time limit as well?
441 * Note that target servers are likely
442 * to handle it, so at some time we'll
443 * get a LDAP_TIMELIMIT_EXCEEDED from
446 rc = ldap_result( lsc->ld, msgid[ i ],
452 } else if ( rc == -1 ) {
453 /* something REALLY bad happened! */
454 ( void )meta_clear_unused_candidates( li,
456 rs->sr_err = LDAP_OTHER;
457 rs->sr_v2ref = v2refs;
458 send_ldap_result( op, rs );
460 /* anything else needs be done? */
463 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
464 e = ldap_first_entry( lsc->ld, res );
465 meta_send_entry( op, rs, lc, i, e );
468 * If scope is BASE, we need to jump out
469 * as soon as one entry is found; if
470 * the target pool is properly crafted,
471 * this should correspond to the sole
472 * entry that has the base DN
474 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE
475 && rs->sr_nentries > 0 ) {
483 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
484 char **references = NULL;
488 * FIXME: should we collect references
489 * and send them alltogether at the end?
492 rc = ldap_parse_reference( lsc->ld, res,
493 &references, &rs->sr_ctrls, 1 );
495 if ( rc != LDAP_SUCCESS ) {
499 if ( references == NULL ) {
503 for ( cnt = 0; references[ cnt ]; cnt++ )
506 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
508 for ( cnt = 0; references[ cnt ]; cnt++ ) {
509 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
510 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
513 /* ignore return value by now */
514 ( void )send_search_reference( op, rs );
518 ldap_value_free( references );
519 ch_free( rs->sr_ref );
523 if ( rs->sr_ctrls ) {
524 ldap_controls_free( rs->sr_ctrls );
529 rs->sr_err = ldap_result2error( lsc->ld,
531 sres = ldap_back_map_result( rs );
535 ldap_get_option( lsc->ld,
536 LDAP_OPT_ERROR_STRING, &err );
537 if ( match != NULL ) {
540 ldap_get_option( lsc->ld,
541 LDAP_OPT_MATCHED_DN, &match );
544 LDAP_LOG( BACK_META, ERR,
545 "meta_back_search [%d] "
546 "match=\"%s\" err=\"%s\"\n",
548 #else /* !NEW_LOGGING */
549 Debug( LDAP_DEBUG_ANY,
550 "=>meta_back_search [%d] "
551 "match=\"%s\" err=\"%s\"\n",
553 #endif /* !NEW_LOGGING */
559 * When no candidates are left,
560 * the outer cycle finishes
574 ldap_pvt_thread_yield();
583 * FIXME: need a strategy to handle errors
585 rc = meta_back_op_result( lc, op, rs );
590 * Rewrite the matched portion of the search base, if required
592 * FIXME: only the last one gets caught!
594 if ( match != NULL ) {
595 switch ( rewrite_session( li->targets[ last ]->rwinfo,
596 "matchedDn", match, op->o_conn,
598 case REWRITE_REGEXEC_OK:
599 if ( mmatch == NULL ) {
600 mmatch = ( char * )match;
603 LDAP_LOG( BACK_META, DETAIL1,
604 "[rw] matchedDn: \"%s\" -> \"%s\"\n",
606 #else /* !NEW_LOGGING */
607 Debug( LDAP_DEBUG_ARGS,
608 "rw> matchedDn: \"%s\" -> \"%s\"\n",
610 #endif /* !NEW_LOGGING */
613 case REWRITE_REGEXEC_UNWILLING:
615 case REWRITE_REGEXEC_ERR:
616 /* FIXME: no error, but no matched ... */
623 * In case we returned at least one entry, we return LDAP_SUCCESS
624 * otherwise, the latter error code we got
626 * FIXME: we should handle error codes and return the more
627 * important/reasonable
629 if ( sres == LDAP_SUCCESS && v2refs ) {
630 sres = LDAP_REFERRAL;
633 rs->sr_matched = mmatch;
634 rs->sr_v2ref = v2refs;
635 send_ldap_result( op, rs );
636 rs->sr_matched = NULL;
642 if ( mmatch != match ) {
668 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
669 struct berval a, mapped;
671 BerElement ber = *e->lm_ber;
672 Attribute *attr, **attrp;
673 struct berval dummy = { 0, NULL };
674 struct berval *bv, bdn;
677 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
678 return LDAP_DECODING_ERROR;
682 * Rewrite the dn of the result, if needed
684 switch ( rewrite_session( li->targets[ target ]->rwinfo,
685 "searchResult", bdn.bv_val, lc->conn,
686 &ent.e_name.bv_val ) ) {
687 case REWRITE_REGEXEC_OK:
688 if ( ent.e_name.bv_val == NULL ) {
693 LDAP_LOG( BACK_META, DETAIL1,
694 "[rw] searchResult[%d]: \"%s\" -> \"%s\"\n",
695 target, bdn.bv_val, ent.e_name.bv_val );
696 #else /* !NEW_LOGGING */
697 Debug( LDAP_DEBUG_ARGS, "rw> searchResult[%d]: \"%s\""
698 " -> \"%s\"\n", target, bdn.bv_val,
700 #endif /* !NEW_LOGGING */
701 ent.e_name.bv_len = strlen( ent.e_name.bv_val );
705 case REWRITE_REGEXEC_UNWILLING:
706 return LDAP_UNWILLING_TO_PERFORM;
708 case REWRITE_REGEXEC_ERR:
713 * Note: this may fail if the target host(s) schema differs
714 * from the one known to the meta, and a DN with unknown
715 * attributes is returned.
717 * FIXME: should we log anything, or delegate to dnNormalize2?
719 if ( dnNormalize2( NULL, &ent.e_name, &ent.e_nname ) != LDAP_SUCCESS ) {
720 return LDAP_INVALID_DN_SYNTAX;
726 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
727 ( void )meta_dncache_update_entry( &li->cache,
735 attrp = &ent.e_attrs;
737 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
738 ldap_back_map( &li->targets[ target ]->at_map,
739 &a, &mapped, BACKLDAP_REMAP );
740 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0' ) {
743 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
744 if ( attr == NULL ) {
750 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
752 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text )
755 LDAP_LOG( BACK_META, DETAIL1,
756 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
757 #else /* !NEW_LOGGING */
758 Debug( LDAP_DEBUG_ANY,
759 "slap_bv2undef_ad(%s): "
760 "%s\n%s", mapped.bv_val, text, "" );
761 #endif /* !NEW_LOGGING */
767 /* no subschemaSubentry */
768 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
773 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
774 || attr->a_vals == NULL ) {
775 attr->a_vals = &dummy;
777 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
778 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
781 for ( last = 0; attr->a_vals[ last ].bv_val; ++last );
783 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
784 ldap_back_map( &li->targets[ target]->oc_map,
785 bv, &mapped, BACKLDAP_REMAP );
786 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
792 *bv = attr->a_vals[ last ];
793 attr->a_vals[ last ].bv_val = NULL;
796 } else if ( mapped.bv_val != bv->bv_val ) {
798 ber_dupbv( bv, &mapped );
802 * It is necessary to try to rewrite attributes with
803 * dn syntax because they might be used in ACLs as
804 * members of groups; since ACLs are applied to the
805 * rewritten stuff, no dn-based subecj clause could
806 * be used at the ldap backend side (see
807 * http://www.OpenLDAP.org/faq/data/cache/452.html)
808 * The problem can be overcome by moving the dn-based
809 * ACLs to the target directory server, and letting
810 * everything pass thru the ldap backend.
812 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
813 SLAPD_DN_SYNTAX ) == 0 ) {
816 for ( last = 0; attr->a_vals[ last ].bv_val; ++last );
818 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
821 switch ( rewrite_session( li->targets[ target ]->rwinfo,
824 lc->conn, &newval )) {
825 case REWRITE_REGEXEC_OK:
827 if ( newval == NULL ) {
831 LDAP_LOG( BACK_META, DETAIL1,
832 "[rw] searchResult on attr=%s: \"%s\" -> \"%s\"\n",
833 attr->a_desc->ad_type->sat_cname.bv_val,
834 bv->bv_val, newval );
835 #else /* !NEW_LOGGING */
836 Debug( LDAP_DEBUG_ARGS,
837 "rw> searchResult on attr=%s:"
838 " \"%s\" -> \"%s\"\n",
839 attr->a_desc->ad_type->sat_cname.bv_val,
840 bv->bv_val, newval );
841 #endif /* !NEW_LOGGING */
844 bv->bv_len = strlen( newval );
848 case REWRITE_REGEXEC_UNWILLING:
849 LBER_FREE(bv->bv_val);
853 *bv = attr->a_vals[last];
854 attr->a_vals[last].bv_val = NULL;
858 case REWRITE_REGEXEC_ERR:
860 * FIXME: better give up,
862 * or leave it untouched?
871 attrp = &attr->a_next;
874 rs->sr_attrs = op->oq_search.rs_attrs;
875 send_search_entry( op, rs );
878 while ( ent.e_attrs ) {
880 ent.e_attrs = attr->a_next;
881 if ( attr->a_vals != &dummy ) {
882 ber_bvarray_free( attr->a_vals );
887 if ( ent.e_dn && ent.e_dn != bdn.bv_val ) {
904 if ( DN_SEPARATOR( rdn[ from ] ) ) {