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;
126 if ( scope == LDAP_SCOPE_BASE ) {
127 op_type = META_OP_REQUIRE_SINGLE;
129 op_type = META_OP_ALLOW_MULTIPLE;
132 lc = meta_back_getconn( li, conn, op, op_type, nbase, NULL );
133 if ( !lc || !meta_back_dobind( lc, op ) ) {
138 * Array of message id of each target
140 msgid = ch_calloc( sizeof( int ), li->ntargets );
141 if ( msgid == NULL ) {
145 nbaselen = strlen( nbase );
150 for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; ++i, ++lsc ) {
151 char *realbase = ( char * )base;
152 int realscope = scope;
155 if ( lsc[ 0 ]->candidate != META_CANDIDATE ) {
160 ldap_set_option( lsc[ 0 ]->ld, LDAP_OPT_DEREF,
164 ldap_set_option( lsc[ 0 ]->ld, LDAP_OPT_TIMELIMIT,
168 ldap_set_option( lsc[ 0 ]->ld, LDAP_OPT_SIZELIMIT,
173 * modifies the base according to the scope, if required
175 suffixlen = strlen( li->targets[ i ]->suffix );
176 if ( suffixlen > nbaselen ) {
178 case LDAP_SCOPE_SUBTREE:
180 * make the target suffix the new base
182 realbase = li->targets[ i ]->suffix;
185 case LDAP_SCOPE_ONELEVEL:
186 if ( is_one_level_rdn( li->targets[ i ]->suffix,
187 suffixlen-nbaselen-1) ) {
189 * if there is exactly one level,
190 * make the target suffix the new
191 * base, and make scope "base"
193 realbase = li->targets[ i ]->suffix;
194 realscope = LDAP_SCOPE_BASE;
196 } /* else continue with the next case */
198 case LDAP_SCOPE_BASE:
200 * this target is no longer candidate
202 lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
205 rc = meta_back_op_result(lc, op);
213 * Rewrite the search base, if required
215 switch ( rewrite_session( li->targets[ i ]->rwinfo,
217 realbase, conn, &mbase ) ) {
218 case REWRITE_REGEXEC_OK:
219 if ( mbase == NULL ) {
223 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
224 "[rw] searchBase: \"%s\" -> \"%s\"\n",
226 #else /* !NEW_LOGGING */
227 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
229 #endif /* !NEW_LOGGING */
232 case REWRITE_REGEXEC_UNWILLING:
233 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
234 NULL, "Unwilling to perform",
239 case REWRITE_REGEXEC_ERR:
240 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
241 NULL, "Operations error",
248 * Rewrite the search filter, if required
250 switch ( rewrite_session( li->targets[ i ]->rwinfo,
252 filterstr, conn, &mfilter ) ) {
253 case REWRITE_REGEXEC_OK:
254 if ( mfilter == NULL || mfilter[ 0 ] == '\0') {
255 if ( mfilter != NULL ) {
258 mfilter = ( char * )filterstr;
261 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
262 "[rw] searchFilter: \"%s\" -> \"%s\"\n",
263 filterstr, mfilter ));
264 #else /* !NEW_LOGGING */
265 Debug( LDAP_DEBUG_ARGS,
266 "rw> searchFilter: \"%s\" -> \"%s\"\n%s",
267 filterstr, mfilter, "" );
268 #endif /* !NEW_LOGGING */
271 case REWRITE_REGEXEC_UNWILLING:
272 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
273 NULL, "Unwilling to perform",
275 /* continue to the next case */
277 case REWRITE_REGEXEC_ERR:
283 * Maps attributes in filter
285 mapped_filter = ldap_back_map_filter( &li->targets[ i ]->at_map,
286 &li->targets[ i ]->oc_map,
287 ( char * )mfilter, 0 );
288 if ( mapped_filter == NULL ) {
289 mapped_filter = ( char * )mfilter;
293 * Maps required attributes
295 mapped_attrs = ldap_back_map_attrs( &li->targets[ i ]->at_map,
297 if ( mapped_attrs == NULL ) {
298 mapped_attrs = attrs;
304 msgid[ i ] = ldap_search( lsc[ 0 ]->ld, mbase, realscope,
305 mapped_filter, mapped_attrs, attrsonly);
306 if ( msgid[ i ] == -1 ) {
307 lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
311 if ( mapped_attrs != attrs ) {
312 charray_free( mapped_attrs );
315 if ( mapped_filter != mfilter ) {
316 free( mapped_filter );
317 mapped_filter = NULL;
319 if ( mfilter != filterstr ) {
323 if ( mbase != realbase ) {
331 /* We pull apart the ber result, stuff it into a slapd entry, and
332 * let send_search_entry stuff it back into ber format. Slow & ugly,
333 * but this is necessary for version matching, and for ACL processing.
338 * In case there are no candidates, no cycle takes place...
340 for ( count = 0, rc = 0; candidates > 0; ) {
343 /* check for abandon */
344 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
346 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
348 for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; lsc++, i++ ) {
349 if ( lsc[ 0 ]->candidate != META_CANDIDATE ) {
354 ldap_abandon( lsc[ 0 ]->ld, msgid[ i ] );
359 rc = ldap_result( lsc[ 0 ]->ld, msgid[ i ],
364 } else if ( rc == -1 ) {
365 /* something REALLY bad happened! */
366 ( void )meta_clear_unused_candidates( li,
368 send_search_result( conn, op,
369 LDAP_OPERATIONS_ERROR,
370 "", "", NULL, NULL, count );
372 /* anything else needs be done? */
374 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
375 e = ldap_first_entry( lsc[ 0 ]->ld,res );
376 meta_send_entry(be, op, lc, i, e, attrs,
382 sres = ldap_result2error( lsc[ 0 ]->ld,
384 sres = ldap_back_map_result( sres );
388 ldap_get_option( lsc[ 0 ]->ld,
389 LDAP_OPT_ERROR_STRING, &err );
390 if ( match != NULL ) {
393 ldap_get_option( lsc[ 0 ]->ld,
394 LDAP_OPT_MATCHED_DN, &match );
397 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
398 "meta_back_search [%d]"
399 " match=\"%s\" err=\"%s\"\n",
401 #else /* !NEW_LOGGING */
402 Debug( LDAP_DEBUG_ANY,
403 "=>meta_back_search [%d] match=\"%s\" err=\"%s\"\n",
405 #endif /* !NEW_LOGGING */
411 * When no candidates are left,
412 * the outer cycle finishes
414 lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
426 ldap_pvt_thread_yield();
435 * FIXME: need a strategy to handle errors
437 rc = meta_back_op_result( lc, op );
442 * Rewrite the matched portion of the search base, if required
444 * FIXME: only the last one gets caught!
446 if ( match != NULL ) {
447 switch ( rewrite_session( li->targets[ last ]->rwinfo,
448 "matchedDn", match, conn, &mmatch ) ) {
449 case REWRITE_REGEXEC_OK:
450 if ( mmatch == NULL ) {
451 mmatch = ( char * )match;
454 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
455 "[rw] matchedDn: \"%s\" -> \"%s\"\n",
457 #else /* !NEW_LOGGING */
458 Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
459 " \"%s\" -> \"%s\"\n%s",
461 #endif /* !NEW_LOGGING */
464 case REWRITE_REGEXEC_UNWILLING:
465 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
466 NULL, "Unwilling to perform",
468 /* continue to the next case */
470 case REWRITE_REGEXEC_ERR:
476 send_search_result( conn, op, sres,
477 mmatch, err, NULL, NULL, count );
481 if ( mmatch != match ) {
509 struct metainfo *li = ( struct metainfo * )be->be_private;
512 BerElement *ber = NULL;
513 Attribute *attr, **attrp;
514 struct berval *dummy = NULL;
519 struct metasingleconn *lsc = lc->conns[ target ];
521 dn = ldap_get_dn( lsc->ld, e );
527 * Rewrite the dn of the result, if needed
529 switch ( rewrite_session( li->targets[ target ]->rwinfo,
530 "searchResult", dn, lc->conn, &ent.e_dn ) ) {
531 case REWRITE_REGEXEC_OK:
532 if ( ent.e_dn == NULL ) {
536 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
537 "[rw] searchResult[%d]:"
538 " \"%s\" -> \"%s\"\n",
539 target, dn, ent.e_dn ));
540 #else /* !NEW_LOGGING */
541 Debug( LDAP_DEBUG_ARGS, "rw> searchResult[%d]: \"%s\""
542 " -> \"%s\"\n", target, dn, ent.e_dn );
543 #endif /* !NEW_LOGGING */
549 case REWRITE_REGEXEC_ERR:
550 case REWRITE_REGEXEC_UNWILLING:
555 ent.e_ndn = ch_strdup( ent.e_dn );
556 ( void )dn_normalize( ent.e_ndn );
561 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
562 ( void )meta_dncache_update_entry( &li->cache,
563 ch_strdup( ent.e_ndn ),
570 attrp = &ent.e_attrs;
572 for ( a = ldap_first_attribute( lsc->ld, e, &ber );
574 a = ldap_next_attribute( lsc->ld, e, ber ) )
576 mapped = ldap_back_map( &li->targets[ target ]->at_map, a, 1 );
577 if ( mapped == NULL ) {
580 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
581 if ( attr == NULL ) {
586 if ( slap_str2ad( mapped, &attr->a_desc, &text )
591 attr->a_vals = ldap_get_values_len( lsc->ld, e, a );
592 if ( !attr->a_vals ) {
593 attr->a_vals = &dummy;
594 } else if ( strcasecmp( mapped, "objectClass" ) == 0 ) {
596 for ( last = 0; attr->a_vals[ last ]; ++last ) ;
597 for ( i = 0; ( bv = attr->a_vals[ i ] ); i++ ) {
598 mapped = ldap_back_map(
599 &li->targets[ target]->oc_map,
601 if ( mapped == NULL ) {
602 ber_bvfree( attr->a_vals[ i ] );
603 attr->a_vals[ i ] = NULL;
608 attr->a_vals[ last ];
609 attr->a_vals[ last ] = NULL;
611 } else if ( mapped != bv->bv_val ) {
612 ch_free( bv->bv_val );
613 bv->bv_val = ch_strdup( mapped );
614 bv->bv_len = strlen( mapped );
618 * It is necessary to try to rewrite attributes with
619 * dn syntax because they might be used in ACLs as
620 * members of groups; since ACLs are applied to the
621 * rewritten stuff, no dn-based subecj clause could
622 * be used at the ldap backend side (see
623 * http://www.OpenLDAP.org/faq/data/cache/452.html)
624 * The problem can be overcome by moving the dn-based
625 * ACLs to the target directory server, and letting
626 * everything pass thru the ldap backend.
628 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
629 SLAPD_DN_SYNTAX ) == 0 ) {
631 for ( i = 0; ( bv = attr->a_vals[ i ] ); i++ ) {
634 switch ( rewrite_session( li->targets[ target ]->rwinfo,
637 lc->conn, &newval )) {
638 case REWRITE_REGEXEC_OK:
640 if ( newval == NULL ) {
644 LDAP_LOG(( "backend",
646 "[rw] searchResult on"
648 " \"%s\" -> \"%s\"\n",
649 attr->a_desc->ad_type->sat_cname,
650 bv->bv_val, newval ));
651 #else /* !NEW_LOGGING */
652 Debug( LDAP_DEBUG_ARGS,
653 "rw> searchResult on attr=%s:"
654 " \"%s\" -> \"%s\"\n",
655 attr->a_desc->ad_type->sat_cname,
656 bv->bv_val, newval );
657 #endif /* !NEW_LOGGING */
661 bv->bv_len = strlen( newval );
665 case REWRITE_REGEXEC_UNWILLING:
667 case REWRITE_REGEXEC_ERR:
669 * FIXME: better give up,
671 * or leave it untouched?
678 attrp = &attr->a_next;
680 send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
681 while ( ent.e_attrs ) {
683 ent.e_attrs = attr->a_next;
684 ad_free( attr->a_desc, 1 );
685 if ( attr->a_vals != &dummy ) {
686 ber_bvecfree(attr->a_vals);
709 if ( LDAP_DNSEPARATOR( rdn[ len ] ) ) {