2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
28 #include "../back-ldap/back-ldap.h"
29 #include "back-meta.h"
32 * The meta-directory has one suffix, called <suffix>.
33 * It handles a pool of target servers, each with a branch suffix
34 * of the form <branch X>,<suffix>
36 * When the meta-directory receives a request with a dn that belongs
37 * to a branch, the corresponding target is invoked. When the dn
38 * does not belong to a specific branch, all the targets that
39 * are compatible with the dn are selected as candidates, and
40 * the request is spawned to all the candidate targets
42 * A request is characterized by a dn. The following cases are handled:
43 * - the dn is the suffix: <dn> == <suffix>,
44 * all the targets are candidates (search ...)
45 * - the dn is a branch suffix: <dn> == <branch X>,<suffix>, or
46 * - the dn is a subtree of a branch suffix:
47 * <dn> == <rdn>,<branch X>,<suffix>,
48 * the target is the only candidate.
50 * A possible extension will include the handling of multiple suffixes
54 * returns 1 if suffix is candidate for dn, otherwise 0
56 * Note: this function should never be called if dn is the <suffix>.
59 meta_back_is_candidate(
60 struct berval *nsuffix,
64 if ( dnIsSuffix( nsuffix, ndn ) || dnIsSuffix( ndn, nsuffix ) ) {
66 * suffix longer than dn
68 return META_CANDIDATE;
71 return META_NOT_CANDIDATE;
75 * meta_back_count_candidates
77 * returns a count of the possible candidate targets
78 * Note: dn MUST be normalized
82 meta_back_count_candidates(
90 * I know assertions should not check run-time values;
91 * at present I didn't find a place for such checks
94 assert( li->targets != NULL );
95 assert( li->ntargets != 0 );
97 for ( i = 0; i < li->ntargets; ++i ) {
98 if ( meta_back_is_candidate( &li->targets[ i ]->suffix, ndn ) ) {
107 * meta_back_is_candidate_unique
109 * checks whether a candidate is unique
110 * Note: dn MUST be normalized
113 meta_back_is_candidate_unique(
118 return ( meta_back_count_candidates( li, ndn ) == 1 );
122 * meta_back_select_unique_candidate
124 * returns the index of the candidate in case it is unique, otherwise -1
125 * Note: dn MUST be normalized.
126 * Note: if defined, the default candidate is returned in case of no match.
129 meta_back_select_unique_candidate(
136 switch ( meta_back_count_candidates( li, ndn ) ) {
141 return ( li->defaulttarget == META_DEFAULT_TARGET_NONE
142 ? -1 : li->defaulttarget );
145 for ( i = 0; i < li->ntargets; ++i ) {
146 if ( meta_back_is_candidate( &li->targets[ i ]->suffix, ndn ) ) {
155 * meta_clear_unused_candidates
157 * clears all candidates except candidate
160 meta_clear_unused_candidates(
169 for ( i = 0; i < li->ntargets; ++i ) {
170 if ( i == candidate ) {
173 meta_clear_one_candidate( &lc->conns[ i ], reallyclean );
180 * meta_clear_one_candidate
182 * clears the selected candidate
185 meta_clear_one_candidate(
186 struct metasingleconn *lsc,
190 lsc->candidate = META_NOT_CANDIDATE;
192 if ( !reallyclean ) {
197 ldap_unbind( lsc->ld );
201 if ( lsc->bound_dn.bv_val != NULL ) {
202 ber_memfree( lsc->bound_dn.bv_val );
203 lsc->bound_dn.bv_val = NULL;
204 lsc->bound_dn.bv_len = 0;
207 if ( lsc->cred.bv_val != NULL ) {
208 ber_memfree( lsc->cred.bv_val );
209 lsc->cred.bv_val = NULL;
210 lsc->cred.bv_len = 0;