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"
106 struct berval *nbase,
112 struct berval *filterstr,
113 AttributeName *attrs,
117 struct metainfo *li = ( struct metainfo * )be->be_private;
119 struct metasingleconn *lsc;
120 struct timeval tv = { 0, 0 };
121 LDAPMessage *res, *e;
122 int count, rc = 0, *msgid, sres = LDAP_NO_SUCH_OBJECT;
123 char *match = NULL, *err = NULL;
124 char *mbase = NULL, *mmatch = NULL;
125 struct berval mfilter;
126 BerVarray v2refs = NULL;
128 int i, last = 0, candidates = 0;
129 struct slap_limits_set *limit = NULL;
133 cache_manager* cm = li->cm;
136 return meta_back_cache_search(be, conn, op, base, nbase,
137 scope, deref, slimit, tlimit,
138 filter, filterstr, attrs, attrsonly);
140 #endif /* LDAP_CACHING */
143 * controls are set in ldap_back_dobind()
145 * FIXME: in case of values return filter, we might want
146 * to map attrs and maybe rewrite value
148 lc = meta_back_getconn( li, conn, op, META_OP_ALLOW_MULTIPLE,
150 if ( !lc || !meta_back_dobind( lc, op ) ) {
151 send_ldap_result( conn, op, LDAP_OTHER,
152 NULL, NULL, NULL, NULL );
157 * Array of message id of each target
159 msgid = ch_calloc( sizeof( int ), li->ntargets );
160 if ( msgid == NULL ) {
161 send_ldap_result( conn, op, LDAP_OTHER,
162 NULL, NULL, NULL, NULL );
166 /* if not root, get appropriate limits */
167 if ( be_isroot( be, &op->o_ndn ) ) {
170 ( void ) get_limits( be, &op->o_ndn, &limit );
173 /* if no time limit requested, rely on remote server limits */
174 /* if requested limit higher than hard limit, abort */
175 if ( !isroot && tlimit > limit->lms_t_hard ) {
176 /* no hard limit means use soft instead */
177 if ( limit->lms_t_hard == 0
178 && limit->lms_t_soft > -1
179 && tlimit > limit->lms_t_soft ) {
180 tlimit = limit->lms_t_soft;
182 /* positive hard limit means abort */
183 } else if ( limit->lms_t_hard > 0 ) {
184 send_ldap_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
185 NULL, NULL, NULL, NULL );
190 /* negative hard limit means no limit */
193 /* if no size limit requested, rely on remote server limits */
194 /* if requested limit higher than hard limit, abort */
195 if ( !isroot && slimit > limit->lms_s_hard ) {
196 /* no hard limit means use soft instead */
197 if ( limit->lms_s_hard == 0
198 && limit->lms_s_soft > -1
199 && slimit > limit->lms_s_soft ) {
200 slimit = limit->lms_s_soft;
202 /* positive hard limit means abort */
203 } else if ( limit->lms_s_hard > 0 ) {
204 send_ldap_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
205 NULL, NULL, NULL, NULL );
210 /* negative hard limit means no limit */
216 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
217 char *realbase = ( char * )base->bv_val;
218 int realscope = scope;
222 if ( lsc->candidate != META_CANDIDATE ) {
227 /* should we check return values? */
229 ldap_set_option( lsc->ld, LDAP_OPT_DEREF,
232 if ( tlimit != -1 ) {
233 ldap_set_option( lsc->ld, LDAP_OPT_TIMELIMIT,
236 if ( slimit != -1 ) {
237 ldap_set_option( lsc->ld, LDAP_OPT_SIZELIMIT,
242 * modifies the base according to the scope, if required
244 suffixlen = li->targets[ i ]->suffix.bv_len;
245 if ( suffixlen > nbase->bv_len ) {
247 case LDAP_SCOPE_SUBTREE:
249 * make the target suffix the new base
250 * FIXME: this is very forgiving, because
251 * illegal bases may be turned into
252 * the suffix of the target.
254 if ( dnIsSuffix( &li->targets[ i ]->suffix,
256 realbase = li->targets[ i ]->suffix.bv_val;
259 * this target is no longer candidate
266 case LDAP_SCOPE_ONELEVEL:
267 if ( is_one_level_rdn( li->targets[ i ]->suffix.bv_val,
268 suffixlen - nbase->bv_len - 1 )
269 && dnIsSuffix( &li->targets[ i ]->suffix, nbase ) ) {
271 * if there is exactly one level,
272 * make the target suffix the new
273 * base, and make scope "base"
275 realbase = li->targets[ i ]->suffix.bv_val;
276 realscope = LDAP_SCOPE_BASE;
278 } /* else continue with the next case */
280 case LDAP_SCOPE_BASE:
282 * this target is no longer candidate
291 * Rewrite the search base, if required
293 switch ( rewrite_session( li->targets[ i ]->rwinfo,
295 realbase, conn, &mbase ) ) {
296 case REWRITE_REGEXEC_OK:
297 if ( mbase == NULL ) {
301 LDAP_LOG( BACK_META, DETAIL1,
302 "[rw] searchBase [%d]: \"%s\" -> \"%s\"\n",
303 i, base->bv_val, mbase );
304 #else /* !NEW_LOGGING */
305 Debug( LDAP_DEBUG_ARGS,
306 "rw> searchBase [%d]: \"%s\" -> \"%s\"\n",
307 i, base->bv_val, mbase );
308 #endif /* !NEW_LOGGING */
311 case REWRITE_REGEXEC_UNWILLING:
312 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
313 NULL, "Operation not allowed",
318 case REWRITE_REGEXEC_ERR:
319 send_ldap_result( conn, op, LDAP_OTHER,
320 NULL, "rewrite error", NULL, NULL );
327 * Rewrite the search filter, if required
329 switch ( rewrite_session( li->targets[ i ]->rwinfo,
331 filterstr->bv_val, conn, &mfilter.bv_val ) ) {
332 case REWRITE_REGEXEC_OK:
333 if ( mfilter.bv_val != NULL && mfilter.bv_val[ 0 ] != '\0') {
334 mfilter.bv_len = strlen( mfilter.bv_val );
336 if ( mfilter.bv_val != NULL ) {
337 free( mfilter.bv_val );
339 mfilter = *filterstr;
342 LDAP_LOG( BACK_META, DETAIL1,
343 "[rw] searchFilter [%d]: \"%s\" -> \"%s\"\n",
344 i, filterstr->bv_val, mfilter.bv_val );
345 #else /* !NEW_LOGGING */
346 Debug( LDAP_DEBUG_ARGS,
347 "rw> searchFilter [%d]: \"%s\" -> \"%s\"\n",
348 i, filterstr->bv_val, mfilter.bv_val );
349 #endif /* !NEW_LOGGING */
352 case REWRITE_REGEXEC_UNWILLING:
353 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
354 NULL, "Operation not allowed",
359 case REWRITE_REGEXEC_ERR:
360 send_ldap_result( conn, op, LDAP_OTHER,
361 NULL, "Rewrite error", NULL, NULL );
367 * Maps attributes in filter
369 mapped_filter = ldap_back_map_filter( &li->targets[ i ]->at_map,
370 &li->targets[ i ]->oc_map, &mfilter,
372 if ( mapped_filter == NULL ) {
373 mapped_filter = ( char * )mfilter.bv_val;
375 if ( mfilter.bv_val != filterstr->bv_val ) {
376 free( mfilter.bv_val );
379 mfilter.bv_val = NULL;
383 rc = ldap_back_filter_map_rewrite_( li->targets[ i ]->rwinfo, conn,
384 &li->targets[ i ]->at_map, &li->targets[ i ]->oc_map,
385 filter, &mfilter, BACKLDAP_MAP );
388 * Maps required attributes
390 mapped_attrs = ldap_back_map_attrs( &li->targets[ i ]->at_map,
391 attrs, BACKLDAP_MAP );
392 if ( mapped_attrs == NULL && attrs) {
393 for ( count=0; attrs[ count ].an_name.bv_val; count++ );
394 mapped_attrs = ch_malloc( ( count + 1 ) * sizeof(char *));
395 for ( count=0; attrs[ count ].an_name.bv_val; count++ ) {
396 mapped_attrs[ count ] = 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, attrsonly );
406 if ( mapped_attrs ) {
407 free( mapped_attrs );
410 if ( mfilter.bv_val != filterstr->bv_val ) {
411 free( mfilter.bv_val );
412 mfilter.bv_val = NULL;
414 if ( mbase != realbase ) {
419 if ( msgid[ i ] == -1 ) {
428 /* We pull apart the ber result, stuff it into a slapd entry, and
429 * let send_search_entry stuff it back into ber format. Slow & ugly,
430 * but this is necessary for version matching, and for ACL processing.
435 * In case there are no candidates, no cycle takes place...
437 * FIXME: we might use a queue, to balance the load
438 * among the candidates
440 for ( count = 0, rc = 0; candidates > 0; ) {
443 /* check for abandon */
446 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); lsc++, i++ ) {
447 if ( msgid[ i ] == -1 ) {
452 ldap_abandon( lsc->ld, msgid[ i ] );
457 if ( slimit > 0 && count == slimit ) {
458 send_search_result( conn, op,
459 LDAP_SIZELIMIT_EXCEEDED,
460 NULL, NULL, v2refs, NULL,
466 * FIXME: handle time limit as well?
467 * Note that target servers are likely
468 * to handle it, so at some time we'll
469 * get a LDAP_TIMELIMIT_EXCEEDED from
472 rc = ldap_result( lsc->ld, msgid[ i ],
478 } else if ( rc == -1 ) {
479 /* something REALLY bad happened! */
480 ( void )meta_clear_unused_candidates( li,
482 send_search_result( conn, op, LDAP_OTHER,
483 NULL, NULL, v2refs, NULL,
486 /* anything else needs be done? */
489 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
490 e = ldap_first_entry( lsc->ld, res );
491 if ( meta_send_entry( be, op, lc, i, e, attrs,
492 attrsonly ) == LDAP_SUCCESS ) {
497 * If scope is BASE, we need to jump out
498 * as soon as one entry is found; if
499 * the target pool is properly crafted,
500 * this should correspond to the sole
501 * entry that has the base DN
503 if ( scope == LDAP_SCOPE_BASE && count > 0 ) {
511 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
512 char **references = NULL;
513 LDAPControl **ctrls = NULL;
518 * FIXME: should we collect references
519 * and send them alltogether at the end?
522 rc = ldap_parse_reference( lsc->ld, res,
523 &references, &ctrls, 1 );
525 if ( rc != LDAP_SUCCESS ) {
529 if ( references == NULL ) {
533 for ( cnt = 0; references[ cnt ]; cnt++ )
536 refs = ch_calloc( cnt + 1, sizeof( struct berval ) );
538 for ( cnt = 0; references[ cnt ]; cnt++ ) {
539 refs[ cnt ].bv_val = references[ cnt ];
540 refs[ cnt ].bv_len = strlen( references[ cnt ] );
543 /* ignore return value by now */
544 ( void )send_search_reference( be, conn, op,
545 NULL, refs, ctrls, &v2refs );
549 ldap_value_free( references );
554 ldap_controls_free( ctrls );
558 sres = ldap_result2error( lsc->ld,
560 sres = ldap_back_map_result( sres );
564 ldap_get_option( lsc->ld,
565 LDAP_OPT_ERROR_STRING, &err );
566 if ( match != NULL ) {
569 ldap_get_option( lsc->ld,
570 LDAP_OPT_MATCHED_DN, &match );
573 LDAP_LOG( BACK_META, ERR,
574 "meta_back_search [%d] "
575 "match=\"%s\" err=\"%s\"\n",
577 #else /* !NEW_LOGGING */
578 Debug( LDAP_DEBUG_ANY,
579 "=>meta_back_search [%d] "
580 "match=\"%s\" err=\"%s\"\n",
582 #endif /* !NEW_LOGGING */
588 * When no candidates are left,
589 * the outer cycle finishes
603 ldap_pvt_thread_yield();
612 * FIXME: need a strategy to handle errors
614 rc = meta_back_op_result( lc, op );
619 * Rewrite the matched portion of the search base, if required
621 * FIXME: only the last one gets caught!
623 if ( match != NULL ) {
624 switch ( rewrite_session( li->targets[ last ]->rwinfo,
625 "matchedDn", match, conn, &mmatch ) ) {
626 case REWRITE_REGEXEC_OK:
627 if ( mmatch == NULL ) {
628 mmatch = ( char * )match;
631 LDAP_LOG( BACK_META, DETAIL1,
632 "[rw] matchedDn: \"%s\" -> \"%s\"\n",
634 #else /* !NEW_LOGGING */
635 Debug( LDAP_DEBUG_ARGS,
636 "rw> matchedDn: \"%s\" -> \"%s\"\n",
638 #endif /* !NEW_LOGGING */
641 case REWRITE_REGEXEC_UNWILLING:
643 case REWRITE_REGEXEC_ERR:
644 /* FIXME: no error, but no matched ... */
651 * In case we returned at least one entry, we return LDAP_SUCCESS
652 * otherwise, the latter error code we got
654 * FIXME: we should handle error codes and return the more
655 * important/reasonable
657 if ( sres == LDAP_SUCCESS && v2refs ) {
658 sres = LDAP_REFERRAL;
660 send_search_result( conn, op, sres, mmatch, err, v2refs, NULL, count );
664 if ( mmatch != match ) {
688 AttributeName *attrs,
692 struct metainfo *li = ( struct metainfo * )be->be_private;
693 struct berval a, mapped;
695 BerElement ber = *e->lm_ber;
696 Attribute *attr, **attrp;
697 struct berval dummy = { 0, NULL };
698 struct berval *bv, bdn;
701 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
702 return LDAP_DECODING_ERROR;
706 * Rewrite the dn of the result, if needed
708 switch ( rewrite_session( li->targets[ target ]->rwinfo,
709 "searchResult", bdn.bv_val, lc->conn, &ent.e_name.bv_val ) ) {
710 case REWRITE_REGEXEC_OK:
711 if ( ent.e_name.bv_val == NULL ) {
716 LDAP_LOG( BACK_META, DETAIL1,
717 "[rw] searchResult[%d]: \"%s\" -> \"%s\"\n",
718 target, bdn.bv_val, ent.e_name.bv_val );
719 #else /* !NEW_LOGGING */
720 Debug( LDAP_DEBUG_ARGS, "rw> searchResult[%d]: \"%s\""
721 " -> \"%s\"\n", target, bdn.bv_val, ent.e_name.bv_val );
722 #endif /* !NEW_LOGGING */
723 ent.e_name.bv_len = strlen( ent.e_name.bv_val );
727 case REWRITE_REGEXEC_UNWILLING:
728 return LDAP_UNWILLING_TO_PERFORM;
730 case REWRITE_REGEXEC_ERR:
735 * Note: this may fail if the target host(s) schema differs
736 * from the one known to the meta, and a DN with unknown
737 * attributes is returned.
739 * FIXME: should we log anything, or delegate to dnNormalize2?
741 if ( dnNormalize2( NULL, &ent.e_name, &ent.e_nname ) != LDAP_SUCCESS ) {
742 return LDAP_INVALID_DN_SYNTAX;
748 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
749 ( void )meta_dncache_update_entry( &li->cache,
757 attrp = &ent.e_attrs;
759 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
760 ldap_back_map( &li->targets[ target ]->at_map,
761 &a, &mapped, BACKLDAP_REMAP );
762 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0' ) {
765 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
766 if ( attr == NULL ) {
772 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
774 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text )
777 LDAP_LOG( BACK_META, DETAIL1,
778 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
779 #else /* !NEW_LOGGING */
780 Debug( LDAP_DEBUG_ANY,
781 "slap_bv2undef_ad(%s): "
782 "%s\n%s", mapped.bv_val, text, "" );
783 #endif /* !NEW_LOGGING */
789 /* no subschemaSubentry */
790 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
795 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
796 || attr->a_vals == NULL ) {
797 attr->a_vals = &dummy;
799 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
800 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
803 for ( last = 0; attr->a_vals[ last ].bv_val; ++last );
805 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
806 ldap_back_map( &li->targets[ target]->oc_map,
807 bv, &mapped, BACKLDAP_REMAP );
808 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
814 *bv = attr->a_vals[ last ];
815 attr->a_vals[ last ].bv_val = NULL;
818 } else if ( mapped.bv_val != bv->bv_val ) {
820 ber_dupbv( bv, &mapped );
824 * It is necessary to try to rewrite attributes with
825 * dn syntax because they might be used in ACLs as
826 * members of groups; since ACLs are applied to the
827 * rewritten stuff, no dn-based subecj clause could
828 * be used at the ldap backend side (see
829 * http://www.OpenLDAP.org/faq/data/cache/452.html)
830 * The problem can be overcome by moving the dn-based
831 * ACLs to the target directory server, and letting
832 * everything pass thru the ldap backend.
834 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
835 SLAPD_DN_SYNTAX ) == 0 ) {
838 for ( last = 0; attr->a_vals[ last ].bv_val; ++last );
840 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
843 switch ( rewrite_session( li->targets[ target ]->rwinfo,
846 lc->conn, &newval )) {
847 case REWRITE_REGEXEC_OK:
849 if ( newval == NULL ) {
853 LDAP_LOG( BACK_META, DETAIL1,
854 "[rw] searchResult on attr=%s: \"%s\" -> \"%s\"\n",
855 attr->a_desc->ad_type->sat_cname.bv_val,
856 bv->bv_val, newval );
857 #else /* !NEW_LOGGING */
858 Debug( LDAP_DEBUG_ARGS,
859 "rw> searchResult on attr=%s:"
860 " \"%s\" -> \"%s\"\n",
861 attr->a_desc->ad_type->sat_cname.bv_val,
862 bv->bv_val, newval );
863 #endif /* !NEW_LOGGING */
866 bv->bv_len = strlen( newval );
870 case REWRITE_REGEXEC_UNWILLING:
871 LBER_FREE(bv->bv_val);
875 *bv = attr->a_vals[last];
876 attr->a_vals[last].bv_val = NULL;
880 case REWRITE_REGEXEC_ERR:
882 * FIXME: better give up,
884 * or leave it untouched?
893 attrp = &attr->a_next;
895 send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
896 while ( ent.e_attrs ) {
898 ent.e_attrs = attr->a_next;
899 if ( attr->a_vals != &dummy ) {
900 ber_bvarray_free( attr->a_vals );
905 if ( ent.e_dn && ent.e_dn != bdn.bv_val ) {
922 if ( DN_SEPARATOR( rdn[ from ] ) ) {