2 * Copyright 1998-2001 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"
109 const char *filterstr,
114 struct metainfo *li = ( struct metainfo * )be->be_private;
116 struct metasingleconn **lsc;
118 LDAPMessage *res, *e;
119 int count, rc = 0, *msgid, sres = LDAP_NO_SUCH_OBJECT;
120 char *match = NULL, *err = NULL;
121 char *mbase = NULL, *mfilter = NULL, *mmatch = NULL,
122 *mapped_filter = NULL, **mapped_attrs = NULL;
124 int i, last = 0, candidates = 0, nbaselen, op_type;
125 struct slap_limits_set *limit = NULL;
128 if ( scope == LDAP_SCOPE_BASE ) {
129 op_type = META_OP_REQUIRE_SINGLE;
131 op_type = META_OP_ALLOW_MULTIPLE;
134 lc = meta_back_getconn( li, conn, op, op_type, nbase, NULL );
135 if ( !lc || !meta_back_dobind( lc, op ) ) {
140 * Array of message id of each target
142 msgid = ch_calloc( sizeof( int ), li->ntargets );
143 if ( msgid == NULL ) {
147 nbaselen = strlen( nbase );
149 /* if not root, get appropriate limits */
150 if ( be_isroot( be, op->o_ndn ) ) {
153 ( void ) get_limits( be, op->o_ndn, &limit );
156 /* if no time limit requested, rely on remote server limits */
157 /* if requested limit higher than hard limit, abort */
158 if ( !isroot && tlimit > limit->lms_t_hard ) {
159 /* no hard limit means use soft instead */
160 if ( limit->lms_t_hard == 0 ) {
161 tlimit = limit->lms_t_soft;
163 /* positive hard limit means abort */
164 } else if ( limit->lms_t_hard > 0 ) {
165 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
166 NULL, NULL, NULL, NULL, 0 );
171 /* negative hard limit means no limit */
174 /* if no size limit requested, rely on remote server limits */
175 /* if requested limit higher than hard limit, abort */
176 if ( !isroot && slimit > limit->lms_s_hard ) {
177 /* no hard limit means use soft instead */
178 if ( limit->lms_s_hard == 0 ) {
179 slimit = limit->lms_s_soft;
181 /* positive hard limit means abort */
182 } else if ( limit->lms_s_hard > 0 ) {
183 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
184 NULL, NULL, NULL, NULL, 0 );
189 /* negative hard limit means no limit */
195 for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; ++i, ++lsc ) {
196 char *realbase = ( char * )base;
197 int realscope = scope;
200 if ( lsc[ 0 ]->candidate != META_CANDIDATE ) {
205 ldap_set_option( lsc[ 0 ]->ld, LDAP_OPT_DEREF,
208 if ( tlimit != -1 ) {
209 ldap_set_option( lsc[ 0 ]->ld, LDAP_OPT_TIMELIMIT,
212 if ( slimit != -1 ) {
213 ldap_set_option( lsc[ 0 ]->ld, LDAP_OPT_SIZELIMIT,
218 * modifies the base according to the scope, if required
220 suffixlen = strlen( li->targets[ i ]->suffix );
221 if ( suffixlen > nbaselen ) {
223 case LDAP_SCOPE_SUBTREE:
225 * make the target suffix the new base
227 realbase = li->targets[ i ]->suffix;
230 case LDAP_SCOPE_ONELEVEL:
231 if ( is_one_level_rdn( li->targets[ i ]->suffix,
232 suffixlen-nbaselen-1) ) {
234 * if there is exactly one level,
235 * make the target suffix the new
236 * base, and make scope "base"
238 realbase = li->targets[ i ]->suffix;
239 realscope = LDAP_SCOPE_BASE;
241 } /* else continue with the next case */
243 case LDAP_SCOPE_BASE:
245 * this target is no longer candidate
247 lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
250 rc = meta_back_op_result(lc, op);
258 * Rewrite the search base, if required
260 switch ( rewrite_session( li->targets[ i ]->rwinfo,
262 realbase, conn, &mbase ) ) {
263 case REWRITE_REGEXEC_OK:
264 if ( mbase == NULL ) {
268 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
269 "[rw] searchBase: \"%s\" -> \"%s\"\n",
271 #else /* !NEW_LOGGING */
272 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
274 #endif /* !NEW_LOGGING */
277 case REWRITE_REGEXEC_UNWILLING:
278 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
279 NULL, "Unwilling to perform",
284 case REWRITE_REGEXEC_ERR:
285 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
286 NULL, "Operations error",
293 * Rewrite the search filter, if required
295 switch ( rewrite_session( li->targets[ i ]->rwinfo,
297 filterstr, conn, &mfilter ) ) {
298 case REWRITE_REGEXEC_OK:
299 if ( mfilter == NULL || mfilter[ 0 ] == '\0') {
300 if ( mfilter != NULL ) {
303 mfilter = ( char * )filterstr;
306 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
307 "[rw] searchFilter: \"%s\" -> \"%s\"\n",
308 filterstr, mfilter ));
309 #else /* !NEW_LOGGING */
310 Debug( LDAP_DEBUG_ARGS,
311 "rw> searchFilter: \"%s\" -> \"%s\"\n%s",
312 filterstr, mfilter, "" );
313 #endif /* !NEW_LOGGING */
316 case REWRITE_REGEXEC_UNWILLING:
317 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
318 NULL, "Unwilling to perform",
320 /* continue to the next case */
322 case REWRITE_REGEXEC_ERR:
328 * Maps attributes in filter
330 mapped_filter = ldap_back_map_filter( &li->targets[ i ]->at_map,
331 &li->targets[ i ]->oc_map,
332 ( char * )mfilter, 0 );
333 if ( mapped_filter == NULL ) {
334 mapped_filter = ( char * )mfilter;
338 * Maps required attributes
340 mapped_attrs = ldap_back_map_attrs( &li->targets[ i ]->at_map,
342 if ( mapped_attrs == NULL ) {
343 mapped_attrs = attrs;
349 msgid[ i ] = ldap_search( lsc[ 0 ]->ld, mbase, realscope,
350 mapped_filter, mapped_attrs, attrsonly);
351 if ( msgid[ i ] == -1 ) {
352 lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
356 if ( mapped_attrs != attrs ) {
357 charray_free( mapped_attrs );
360 if ( mapped_filter != mfilter ) {
361 free( mapped_filter );
362 mapped_filter = NULL;
364 if ( mfilter != filterstr ) {
368 if ( mbase != realbase ) {
376 /* We pull apart the ber result, stuff it into a slapd entry, and
377 * let send_search_entry stuff it back into ber format. Slow & ugly,
378 * but this is necessary for version matching, and for ACL processing.
383 * In case there are no candidates, no cycle takes place...
385 for ( count = 0, rc = 0; candidates > 0; ) {
388 /* check for abandon */
389 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
391 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
393 for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; lsc++, i++ ) {
394 if ( lsc[ 0 ]->candidate != META_CANDIDATE ) {
399 ldap_abandon( lsc[ 0 ]->ld, msgid[ i ] );
404 if ( slimit > 0 && count == slimit ) {
405 send_search_result( conn, op,
406 LDAP_SIZELIMIT_EXCEEDED,
407 NULL, NULL, NULL, NULL, count );
411 rc = ldap_result( lsc[ 0 ]->ld, msgid[ i ],
416 } else if ( rc == -1 ) {
417 /* something REALLY bad happened! */
418 ( void )meta_clear_unused_candidates( li,
420 send_search_result( conn, op,
421 LDAP_OPERATIONS_ERROR,
422 "", "", NULL, NULL, count );
424 /* anything else needs be done? */
426 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
427 e = ldap_first_entry( lsc[ 0 ]->ld,res );
428 meta_send_entry(be, op, lc, i, e, attrs,
434 sres = ldap_result2error( lsc[ 0 ]->ld,
436 sres = ldap_back_map_result( sres );
440 ldap_get_option( lsc[ 0 ]->ld,
441 LDAP_OPT_ERROR_STRING, &err );
442 if ( match != NULL ) {
445 ldap_get_option( lsc[ 0 ]->ld,
446 LDAP_OPT_MATCHED_DN, &match );
449 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
450 "meta_back_search [%d]"
451 " match=\"%s\" err=\"%s\"\n",
453 #else /* !NEW_LOGGING */
454 Debug( LDAP_DEBUG_ANY,
455 "=>meta_back_search [%d] match=\"%s\" err=\"%s\"\n",
457 #endif /* !NEW_LOGGING */
463 * When no candidates are left,
464 * the outer cycle finishes
466 lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
478 ldap_pvt_thread_yield();
487 * FIXME: need a strategy to handle errors
489 rc = meta_back_op_result( lc, op );
494 * Rewrite the matched portion of the search base, if required
496 * FIXME: only the last one gets caught!
498 if ( match != NULL ) {
499 switch ( rewrite_session( li->targets[ last ]->rwinfo,
500 "matchedDn", match, conn, &mmatch ) ) {
501 case REWRITE_REGEXEC_OK:
502 if ( mmatch == NULL ) {
503 mmatch = ( char * )match;
506 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
507 "[rw] matchedDn: \"%s\" -> \"%s\"\n",
509 #else /* !NEW_LOGGING */
510 Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
511 " \"%s\" -> \"%s\"\n%s",
513 #endif /* !NEW_LOGGING */
516 case REWRITE_REGEXEC_UNWILLING:
517 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
518 NULL, "Unwilling to perform",
520 /* continue to the next case */
522 case REWRITE_REGEXEC_ERR:
528 send_search_result( conn, op, sres,
529 mmatch, err, NULL, NULL, count );
533 if ( mmatch != match ) {
561 struct metainfo *li = ( struct metainfo * )be->be_private;
564 BerElement *ber = NULL;
565 Attribute *attr, **attrp;
566 struct berval *dummy = NULL;
571 struct metasingleconn *lsc = lc->conns[ target ];
573 dn = ldap_get_dn( lsc->ld, e );
579 * Rewrite the dn of the result, if needed
581 switch ( rewrite_session( li->targets[ target ]->rwinfo,
582 "searchResult", dn, lc->conn, &ent.e_dn ) ) {
583 case REWRITE_REGEXEC_OK:
584 if ( ent.e_dn == NULL ) {
588 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
589 "[rw] searchResult[%d]:"
590 " \"%s\" -> \"%s\"\n",
591 target, dn, ent.e_dn ));
592 #else /* !NEW_LOGGING */
593 Debug( LDAP_DEBUG_ARGS, "rw> searchResult[%d]: \"%s\""
594 " -> \"%s\"\n", target, dn, ent.e_dn );
595 #endif /* !NEW_LOGGING */
601 case REWRITE_REGEXEC_ERR:
602 case REWRITE_REGEXEC_UNWILLING:
607 ent.e_ndn = ch_strdup( ent.e_dn );
608 ( void )dn_normalize( ent.e_ndn );
613 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
614 ( void )meta_dncache_update_entry( &li->cache,
615 ch_strdup( ent.e_ndn ),
622 attrp = &ent.e_attrs;
624 for ( a = ldap_first_attribute( lsc->ld, e, &ber );
626 a = ldap_next_attribute( lsc->ld, e, ber ) )
628 mapped = ldap_back_map( &li->targets[ target ]->at_map, a, 1 );
629 if ( mapped == NULL ) {
632 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
633 if ( attr == NULL ) {
638 if ( slap_str2ad( mapped, &attr->a_desc, &text )
643 attr->a_vals = ldap_get_values_len( lsc->ld, e, a );
644 if ( !attr->a_vals ) {
645 attr->a_vals = &dummy;
646 } else if ( strcasecmp( mapped, "objectClass" ) == 0 ) {
648 for ( last = 0; attr->a_vals[ last ]; ++last ) ;
649 for ( i = 0; ( bv = attr->a_vals[ i ] ); i++ ) {
650 mapped = ldap_back_map(
651 &li->targets[ target]->oc_map,
653 if ( mapped == NULL ) {
654 ber_bvfree( attr->a_vals[ i ] );
655 attr->a_vals[ i ] = NULL;
660 attr->a_vals[ last ];
661 attr->a_vals[ last ] = NULL;
663 } else if ( mapped != bv->bv_val ) {
664 ch_free( bv->bv_val );
665 bv->bv_val = ch_strdup( mapped );
666 bv->bv_len = strlen( mapped );
670 * It is necessary to try to rewrite attributes with
671 * dn syntax because they might be used in ACLs as
672 * members of groups; since ACLs are applied to the
673 * rewritten stuff, no dn-based subecj clause could
674 * be used at the ldap backend side (see
675 * http://www.OpenLDAP.org/faq/data/cache/452.html)
676 * The problem can be overcome by moving the dn-based
677 * ACLs to the target directory server, and letting
678 * everything pass thru the ldap backend.
680 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
681 SLAPD_DN_SYNTAX ) == 0 ) {
683 for ( i = 0; ( bv = attr->a_vals[ i ] ); i++ ) {
686 switch ( rewrite_session( li->targets[ target ]->rwinfo,
689 lc->conn, &newval )) {
690 case REWRITE_REGEXEC_OK:
692 if ( newval == NULL ) {
696 LDAP_LOG(( "backend",
698 "[rw] searchResult on"
700 " \"%s\" -> \"%s\"\n",
701 attr->a_desc->ad_type->sat_cname,
702 bv->bv_val, newval ));
703 #else /* !NEW_LOGGING */
704 Debug( LDAP_DEBUG_ARGS,
705 "rw> searchResult on attr=%s:"
706 " \"%s\" -> \"%s\"\n",
707 attr->a_desc->ad_type->sat_cname,
708 bv->bv_val, newval );
709 #endif /* !NEW_LOGGING */
713 bv->bv_len = strlen( newval );
717 case REWRITE_REGEXEC_UNWILLING:
719 case REWRITE_REGEXEC_ERR:
721 * FIXME: better give up,
723 * or leave it untouched?
730 attrp = &attr->a_next;
732 send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
733 while ( ent.e_attrs ) {
735 ent.e_attrs = attr->a_next;
736 ad_free( attr->a_desc, 1 );
737 if ( attr->a_vals != &dummy ) {
738 ber_bvecfree(attr->a_vals);
761 if ( LDAP_DNSEPARATOR( rdn[ len ] ) ) {