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 count, rc = 0, *msgid, sres = LDAP_NO_SUCH_OBJECT;
107 char *match = NULL, *err = NULL;
108 char *mbase = NULL, *mmatch = NULL;
109 struct berval mfilter;
110 BerVarray v2refs = NULL;
112 int i, last = 0, candidates = 0;
113 struct slap_limits_set *limit = NULL;
117 cache_manager* cm = li->cm;
120 return meta_back_cache_search(op->o_bd, op->o_conn, op,
121 &op->o_req_dn, &op->o_req_ndn,
122 op->oq_search.rs_scope,
123 op->oq_search.rs_deref,
124 op->oq_search.rs_slimit,
125 op->oq_search.rs_tlimit,
126 op->oq_search.rs_filter,
127 &op->oq_search.rs_filterstr,
128 op->oq_search.rs_attrs,
129 op->oq_search.rs_attrsonly);
131 #endif /* LDAP_CACHING */
134 * controls are set in ldap_back_dobind()
136 * FIXME: in case of values return filter, we might want
137 * to map attrs and maybe rewrite value
139 lc = meta_back_getconn( op, rs, META_OP_ALLOW_MULTIPLE,
140 &op->o_req_ndn, NULL );
142 send_ldap_result( op, rs );
146 if ( !meta_back_dobind( lc, op ) ) {
147 rs->sr_err = LDAP_OTHER;
148 send_ldap_result( op, rs );
153 * Array of message id of each target
155 msgid = ch_calloc( sizeof( int ), li->ntargets );
156 if ( msgid == NULL ) {
157 rs->sr_err = LDAP_OTHER;
158 send_ldap_result( op, rs );
162 /* if not root, get appropriate limits */
163 if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
166 ( void ) get_limits( op->o_bd, &op->o_ndn, &limit );
169 /* if no time limit requested, rely on remote server limits */
170 /* if requested limit higher than hard limit, abort */
171 if ( !isroot && op->oq_search.rs_tlimit > limit->lms_t_hard ) {
172 /* no hard limit means use soft instead */
173 if ( limit->lms_t_hard == 0
174 && limit->lms_t_soft > -1
175 && op->oq_search.rs_tlimit > limit->lms_t_soft ) {
176 op->oq_search.rs_tlimit = limit->lms_t_soft;
178 /* positive hard limit means abort */
179 } else if ( limit->lms_t_hard > 0 ) {
180 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
181 send_ldap_result( op, rs );
186 /* negative hard limit means no limit */
189 /* if no size limit requested, rely on remote server limits */
190 /* if requested limit higher than hard limit, abort */
191 if ( !isroot && op->oq_search.rs_slimit > limit->lms_s_hard ) {
192 /* no hard limit means use soft instead */
193 if ( limit->lms_s_hard == 0
194 && limit->lms_s_soft > -1
195 && op->oq_search.rs_slimit > limit->lms_s_soft ) {
196 op->oq_search.rs_slimit = limit->lms_s_soft;
198 /* positive hard limit means abort */
199 } else if ( limit->lms_s_hard > 0 ) {
200 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
201 send_ldap_result( op, rs );
206 /* negative hard limit means no limit */
212 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
213 char *realbase = ( char * )op->o_req_dn.bv_val;
214 int realscope = op->oq_search.rs_scope;
218 if ( lsc->candidate != META_CANDIDATE ) {
223 /* should we check return values? */
224 if ( op->oq_search.rs_deref != -1 ) {
225 ldap_set_option( lsc->ld, LDAP_OPT_DEREF,
226 ( void * )&op->oq_search.rs_deref);
228 if ( op->oq_search.rs_tlimit != -1 ) {
229 ldap_set_option( lsc->ld, LDAP_OPT_TIMELIMIT,
230 ( void * )&op->oq_search.rs_tlimit);
232 if ( op->oq_search.rs_slimit != -1 ) {
233 ldap_set_option( lsc->ld, LDAP_OPT_SIZELIMIT,
234 ( void * )&op->oq_search.rs_slimit);
238 * modifies the base according to the scope, if required
240 suffixlen = li->targets[ i ]->suffix.bv_len;
241 if ( suffixlen > op->o_req_ndn.bv_len ) {
242 switch ( op->oq_search.rs_scope ) {
243 case LDAP_SCOPE_SUBTREE:
245 * make the target suffix the new base
246 * FIXME: this is very forgiving, because
247 * illegal bases may be turned into
248 * the suffix of the target.
250 if ( dnIsSuffix( &li->targets[ i ]->suffix,
252 realbase = li->targets[ i ]->suffix.bv_val;
255 * this target is no longer candidate
262 case LDAP_SCOPE_ONELEVEL:
263 if ( is_one_level_rdn( li->targets[ i ]->suffix.bv_val,
264 suffixlen - op->o_req_ndn.bv_len - 1 )
265 && dnIsSuffix( &li->targets[ i ]->suffix, &op->o_req_ndn ) ) {
267 * if there is exactly one level,
268 * make the target suffix the new
269 * base, and make scope "base"
271 realbase = li->targets[ i ]->suffix.bv_val;
272 realscope = LDAP_SCOPE_BASE;
274 } /* else continue with the next case */
276 case LDAP_SCOPE_BASE:
278 * this target is no longer candidate
287 * Rewrite the search base, if required
289 switch ( rewrite_session( li->targets[ i ]->rwinfo,
291 realbase, op->o_conn, &mbase ) ) {
292 case REWRITE_REGEXEC_OK:
293 if ( mbase == NULL ) {
297 LDAP_LOG( BACK_META, DETAIL1,
298 "[rw] searchBase [%d]: \"%s\" -> \"%s\"\n",
299 i, op->o_req_dn.bv_val, mbase );
300 #else /* !NEW_LOGGING */
301 Debug( LDAP_DEBUG_ARGS,
302 "rw> searchBase [%d]: \"%s\" -> \"%s\"\n",
303 i, op->o_req_dn.bv_val, mbase );
304 #endif /* !NEW_LOGGING */
307 case REWRITE_REGEXEC_UNWILLING:
308 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
309 rs->sr_text = "Operation not allowed";
310 send_ldap_result( op, rs );
314 case REWRITE_REGEXEC_ERR:
315 rs->sr_err = LDAP_OTHER;
316 rs->sr_text = "rewrite error";
317 send_ldap_result( op, rs );
324 * Rewrite the search filter, if required
326 switch ( rewrite_session( li->targets[ i ]->rwinfo,
328 filterstr->bv_val, conn, &mfilter.bv_val ) ) {
329 case REWRITE_REGEXEC_OK:
330 if ( mfilter.bv_val != NULL && mfilter.bv_val[ 0 ] != '\0') {
331 mfilter.bv_len = strlen( mfilter.bv_val );
333 if ( mfilter.bv_val != NULL ) {
334 free( mfilter.bv_val );
336 mfilter = *filterstr;
339 LDAP_LOG( BACK_META, DETAIL1,
340 "[rw] searchFilter [%d]: \"%s\" -> \"%s\"\n",
341 i, filterstr->bv_val, mfilter.bv_val );
342 #else /* !NEW_LOGGING */
343 Debug( LDAP_DEBUG_ARGS,
344 "rw> searchFilter [%d]: \"%s\" -> \"%s\"\n",
345 i, filterstr->bv_val, mfilter.bv_val );
346 #endif /* !NEW_LOGGING */
349 case REWRITE_REGEXEC_UNWILLING:
350 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
351 NULL, "Operation not allowed",
356 case REWRITE_REGEXEC_ERR:
357 send_ldap_result( conn, op, LDAP_OTHER,
358 NULL, "Rewrite error", NULL, NULL );
364 * Maps attributes in filter
366 mapped_filter = ldap_back_map_filter( &li->targets[ i ]->at_map,
367 &li->targets[ i ]->oc_map, &mfilter,
369 if ( mapped_filter == NULL ) {
370 mapped_filter = ( char * )mfilter.bv_val;
372 if ( mfilter.bv_val != filterstr->bv_val ) {
373 free( mfilter.bv_val );
376 mfilter.bv_val = NULL;
380 rc = ldap_back_filter_map_rewrite_( li->targets[ i ]->rwinfo,
382 &li->targets[ i ]->at_map,
383 &li->targets[ i ]->oc_map,
384 op->oq_search.rs_filter,
385 &mfilter, BACKLDAP_MAP );
388 * Maps required attributes
390 mapped_attrs = ldap_back_map_attrs( &li->targets[ i ]->at_map,
391 op->oq_search.rs_attrs, BACKLDAP_MAP );
392 if ( mapped_attrs == NULL && op->oq_search.rs_attrs) {
393 for ( count = 0; op->oq_search.rs_attrs[ count ].an_name.bv_val; count++ );
394 mapped_attrs = ch_malloc( ( count + 1 ) * sizeof(char *));
395 for ( count = 0; op->oq_search.rs_attrs[ count ].an_name.bv_val; count++ ) {
396 mapped_attrs[ count ] = op->oq_search.rs_attrs[ count ].an_name.bv_val;
398 mapped_attrs[ count ] = NULL;
404 msgid[ i ] = ldap_search( lsc->ld, mbase, realscope,
405 mfilter.bv_val, mapped_attrs,
406 op->oq_search.rs_attrsonly );
407 if ( mapped_attrs ) {
408 free( mapped_attrs );
411 if ( mfilter.bv_val != op->oq_search.rs_filterstr.bv_val ) {
412 free( mfilter.bv_val );
413 mfilter.bv_val = NULL;
415 if ( mbase != realbase ) {
420 if ( msgid[ i ] == -1 ) {
429 /* We pull apart the ber result, stuff it into a slapd entry, and
430 * let send_search_entry stuff it back into ber format. Slow & ugly,
431 * but this is necessary for version matching, and for ACL processing.
436 * In case there are no candidates, no cycle takes place...
438 * FIXME: we might use a queue, to balance the load
439 * among the candidates
441 for ( rc = 0; candidates > 0; ) {
444 /* check for abandon */
447 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); lsc++, i++ ) {
448 if ( msgid[ i ] == -1 ) {
453 ldap_abandon( lsc->ld, msgid[ i ] );
458 if ( op->oq_search.rs_slimit > 0
459 && rs->sr_nentries == op->oq_search.rs_slimit ) {
460 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
461 rs->sr_v2ref = v2refs;
462 send_ldap_result( op, rs );
467 * FIXME: handle time limit as well?
468 * Note that target servers are likely
469 * to handle it, so at some time we'll
470 * get a LDAP_TIMELIMIT_EXCEEDED from
473 rc = ldap_result( lsc->ld, msgid[ i ],
479 } else if ( rc == -1 ) {
480 /* something REALLY bad happened! */
481 ( void )meta_clear_unused_candidates( li,
483 rs->sr_err = LDAP_OTHER;
484 rs->sr_v2ref = v2refs;
485 send_ldap_result( op, rs );
487 /* anything else needs be done? */
490 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
491 e = ldap_first_entry( lsc->ld, res );
492 meta_send_entry( op, rs, lc, i, e );
495 * If scope is BASE, we need to jump out
496 * as soon as one entry is found; if
497 * the target pool is properly crafted,
498 * this should correspond to the sole
499 * entry that has the base DN
501 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE
502 && rs->sr_nentries > 0 ) {
510 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
511 char **references = NULL;
515 * FIXME: should we collect references
516 * and send them alltogether at the end?
519 rc = ldap_parse_reference( lsc->ld, res,
520 &references, &rs->sr_ctrls, 1 );
522 if ( rc != LDAP_SUCCESS ) {
526 if ( references == NULL ) {
530 for ( cnt = 0; references[ cnt ]; cnt++ )
533 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
535 for ( cnt = 0; references[ cnt ]; cnt++ ) {
536 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
537 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
540 /* ignore return value by now */
541 ( void )send_search_reference( op, rs );
545 ldap_value_free( references );
546 ch_free( rs->sr_ref );
550 if ( rs->sr_ctrls ) {
551 ldap_controls_free( rs->sr_ctrls );
556 sres = ldap_result2error( lsc->ld,
558 sres = ldap_back_map_result( sres );
562 ldap_get_option( lsc->ld,
563 LDAP_OPT_ERROR_STRING, &err );
564 if ( match != NULL ) {
567 ldap_get_option( lsc->ld,
568 LDAP_OPT_MATCHED_DN, &match );
571 LDAP_LOG( BACK_META, ERR,
572 "meta_back_search [%d] "
573 "match=\"%s\" err=\"%s\"\n",
575 #else /* !NEW_LOGGING */
576 Debug( LDAP_DEBUG_ANY,
577 "=>meta_back_search [%d] "
578 "match=\"%s\" err=\"%s\"\n",
580 #endif /* !NEW_LOGGING */
586 * When no candidates are left,
587 * the outer cycle finishes
601 ldap_pvt_thread_yield();
610 * FIXME: need a strategy to handle errors
612 rc = meta_back_op_result( lc, op, rs );
617 * Rewrite the matched portion of the search base, if required
619 * FIXME: only the last one gets caught!
621 if ( match != NULL ) {
622 switch ( rewrite_session( li->targets[ last ]->rwinfo,
623 "matchedDn", match, op->o_conn,
625 case REWRITE_REGEXEC_OK:
626 if ( mmatch == NULL ) {
627 mmatch = ( char * )match;
630 LDAP_LOG( BACK_META, DETAIL1,
631 "[rw] matchedDn: \"%s\" -> \"%s\"\n",
633 #else /* !NEW_LOGGING */
634 Debug( LDAP_DEBUG_ARGS,
635 "rw> matchedDn: \"%s\" -> \"%s\"\n",
637 #endif /* !NEW_LOGGING */
640 case REWRITE_REGEXEC_UNWILLING:
642 case REWRITE_REGEXEC_ERR:
643 /* FIXME: no error, but no matched ... */
650 * In case we returned at least one entry, we return LDAP_SUCCESS
651 * otherwise, the latter error code we got
653 * FIXME: we should handle error codes and return the more
654 * important/reasonable
656 if ( sres == LDAP_SUCCESS && v2refs ) {
657 sres = LDAP_REFERRAL;
660 rs->sr_matched = mmatch;
661 rs->sr_v2ref = v2refs;
662 send_ldap_result( op, rs );
663 rs->sr_matched = NULL;
669 if ( mmatch != match ) {
695 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
696 struct berval a, mapped;
698 BerElement ber = *e->lm_ber;
699 Attribute *attr, **attrp;
700 struct berval dummy = { 0, NULL };
701 struct berval *bv, bdn;
704 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
705 return LDAP_DECODING_ERROR;
709 * Rewrite the dn of the result, if needed
711 switch ( rewrite_session( li->targets[ target ]->rwinfo,
712 "searchResult", bdn.bv_val, lc->conn,
713 &ent.e_name.bv_val ) ) {
714 case REWRITE_REGEXEC_OK:
715 if ( ent.e_name.bv_val == NULL ) {
720 LDAP_LOG( BACK_META, DETAIL1,
721 "[rw] searchResult[%d]: \"%s\" -> \"%s\"\n",
722 target, bdn.bv_val, ent.e_name.bv_val );
723 #else /* !NEW_LOGGING */
724 Debug( LDAP_DEBUG_ARGS, "rw> searchResult[%d]: \"%s\""
725 " -> \"%s\"\n", target, bdn.bv_val,
727 #endif /* !NEW_LOGGING */
728 ent.e_name.bv_len = strlen( ent.e_name.bv_val );
732 case REWRITE_REGEXEC_UNWILLING:
733 return LDAP_UNWILLING_TO_PERFORM;
735 case REWRITE_REGEXEC_ERR:
740 * Note: this may fail if the target host(s) schema differs
741 * from the one known to the meta, and a DN with unknown
742 * attributes is returned.
744 * FIXME: should we log anything, or delegate to dnNormalize2?
746 if ( dnNormalize2( NULL, &ent.e_name, &ent.e_nname ) != LDAP_SUCCESS ) {
747 return LDAP_INVALID_DN_SYNTAX;
753 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
754 ( void )meta_dncache_update_entry( &li->cache,
762 attrp = &ent.e_attrs;
764 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
765 ldap_back_map( &li->targets[ target ]->at_map,
766 &a, &mapped, BACKLDAP_REMAP );
767 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0' ) {
770 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
771 if ( attr == NULL ) {
777 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
779 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text )
782 LDAP_LOG( BACK_META, DETAIL1,
783 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
784 #else /* !NEW_LOGGING */
785 Debug( LDAP_DEBUG_ANY,
786 "slap_bv2undef_ad(%s): "
787 "%s\n%s", mapped.bv_val, text, "" );
788 #endif /* !NEW_LOGGING */
794 /* no subschemaSubentry */
795 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
800 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
801 || attr->a_vals == NULL ) {
802 attr->a_vals = &dummy;
804 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
805 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
808 for ( last = 0; attr->a_vals[ last ].bv_val; ++last );
810 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
811 ldap_back_map( &li->targets[ target]->oc_map,
812 bv, &mapped, BACKLDAP_REMAP );
813 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
819 *bv = attr->a_vals[ last ];
820 attr->a_vals[ last ].bv_val = NULL;
823 } else if ( mapped.bv_val != bv->bv_val ) {
825 ber_dupbv( bv, &mapped );
829 * It is necessary to try to rewrite attributes with
830 * dn syntax because they might be used in ACLs as
831 * members of groups; since ACLs are applied to the
832 * rewritten stuff, no dn-based subecj clause could
833 * be used at the ldap backend side (see
834 * http://www.OpenLDAP.org/faq/data/cache/452.html)
835 * The problem can be overcome by moving the dn-based
836 * ACLs to the target directory server, and letting
837 * everything pass thru the ldap backend.
839 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
840 SLAPD_DN_SYNTAX ) == 0 ) {
843 for ( last = 0; attr->a_vals[ last ].bv_val; ++last );
845 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
848 switch ( rewrite_session( li->targets[ target ]->rwinfo,
851 lc->conn, &newval )) {
852 case REWRITE_REGEXEC_OK:
854 if ( newval == NULL ) {
858 LDAP_LOG( BACK_META, DETAIL1,
859 "[rw] searchResult on attr=%s: \"%s\" -> \"%s\"\n",
860 attr->a_desc->ad_type->sat_cname.bv_val,
861 bv->bv_val, newval );
862 #else /* !NEW_LOGGING */
863 Debug( LDAP_DEBUG_ARGS,
864 "rw> searchResult on attr=%s:"
865 " \"%s\" -> \"%s\"\n",
866 attr->a_desc->ad_type->sat_cname.bv_val,
867 bv->bv_val, newval );
868 #endif /* !NEW_LOGGING */
871 bv->bv_len = strlen( newval );
875 case REWRITE_REGEXEC_UNWILLING:
876 LBER_FREE(bv->bv_val);
880 *bv = attr->a_vals[last];
881 attr->a_vals[last].bv_val = NULL;
885 case REWRITE_REGEXEC_ERR:
887 * FIXME: better give up,
889 * or leave it untouched?
898 attrp = &attr->a_next;
901 rs->sr_attrs = op->oq_search.rs_attrs;
902 send_search_entry( op, rs );
905 while ( ent.e_attrs ) {
907 ent.e_attrs = attr->a_next;
908 if ( attr->a_vals != &dummy ) {
909 ber_bvarray_free( attr->a_vals );
914 if ( ent.e_dn && ent.e_dn != bdn.bv_val ) {
931 if ( DN_SEPARATOR( rdn[ from ] ) ) {