2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2006 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
26 #include "ac/string.h"
29 #include "../back-ldap/back-ldap.h"
30 #include "back-meta.h"
33 * The meta-directory has one suffix, called <suffix>.
34 * It handles a pool of target servers, each with a branch suffix
35 * of the form <branch X>,<suffix>
37 * When the meta-directory receives a request with a dn that belongs
38 * to a branch, the corresponding target is invoked. When the dn
39 * does not belong to a specific branch, all the targets that
40 * are compatible with the dn are selected as candidates, and
41 * the request is spawned to all the candidate targets
43 * A request is characterized by a dn. The following cases are handled:
44 * - the dn is the suffix: <dn> == <suffix>,
45 * all the targets are candidates (search ...)
46 * - the dn is a branch suffix: <dn> == <branch X>,<suffix>, or
47 * - the dn is a subtree of a branch suffix:
48 * <dn> == <rdn>,<branch X>,<suffix>,
49 * the target is the only candidate.
51 * A possible extension will include the handling of multiple suffixes
56 * returns 1 if suffix is candidate for dn, otherwise 0
58 * Note: this function should never be called if dn is the <suffix>.
61 meta_back_is_candidate(
62 struct berval *nsuffix,
64 BerVarray subtree_exclude,
68 if ( dnIsSuffix( ndn, nsuffix ) ) {
69 if ( subtree_exclude ) {
72 for ( i = 0; !BER_BVISNULL( &subtree_exclude[ i ] ); i++ ) {
73 if ( dnIsSuffix( ndn, &subtree_exclude[ i ] ) ) {
74 return META_NOT_CANDIDATE;
79 switch ( suffixscope ) {
80 case LDAP_SCOPE_SUBTREE:
82 return META_CANDIDATE;
84 case LDAP_SCOPE_SUBORDINATE:
85 if ( ndn->bv_len > nsuffix->bv_len ) {
86 return META_CANDIDATE;
90 /* nearly useless; not allowed by config */
91 case LDAP_SCOPE_ONELEVEL:
92 if ( ndn->bv_len > nsuffix->bv_len ) {
93 struct berval rdn = *ndn;
95 rdn.bv_len -= nsuffix->bv_len
97 if ( dnIsOneLevelRDN( &rdn ) ) {
98 return META_CANDIDATE;
103 /* nearly useless; not allowed by config */
104 case LDAP_SCOPE_BASE:
105 if ( ndn->bv_len == nsuffix->bv_len ) {
106 return META_CANDIDATE;
111 return META_NOT_CANDIDATE;
114 if ( scope == LDAP_SCOPE_SUBTREE && dnIsSuffix( nsuffix, ndn ) ) {
116 * suffix longer than dn, but common part matches
118 return META_CANDIDATE;
121 return META_NOT_CANDIDATE;
125 * meta_back_select_unique_candidate
127 * returns the index of the candidate in case it is unique, otherwise
128 * META_TARGET_NONE if none matches, or
129 * META_TARGET_MULTIPLE if more than one matches
130 * Note: ndn MUST be normalized.
133 meta_back_select_unique_candidate(
137 int i, candidate = META_TARGET_NONE;
139 for ( i = 0; i < mi->mi_ntargets; ++i ) {
140 if ( meta_back_is_candidate( &mi->mi_targets[ i ].mt_nsuffix,
141 mi->mi_targets[ i ].mt_scope,
142 mi->mi_targets[ i ].mt_subtree_exclude,
143 ndn, LDAP_SCOPE_BASE ) )
145 if ( candidate == META_TARGET_NONE ) {
149 return META_TARGET_MULTIPLE;
158 * meta_clear_unused_candidates
160 * clears all candidates except candidate
163 meta_clear_unused_candidates(
167 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
169 SlapReply *candidates = meta_back_candidates_get( op );
171 for ( i = 0; i < mi->mi_ntargets; ++i ) {
172 if ( i == candidate ) {
175 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
182 * meta_clear_one_candidate
184 * clears the selected candidate
187 meta_clear_one_candidate(
188 metasingleconn_t *msc )
191 ldap_unbind_ext( msc->msc_ld, NULL, NULL );
195 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
196 ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
197 BER_BVZERO( &msc->msc_bound_ndn );
200 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
201 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
202 ber_memfree_x( msc->msc_cred.bv_val, NULL );
203 BER_BVZERO( &msc->msc_cred );
210 * meta_clear_candidates
212 * clears all candidates
215 meta_clear_candidates( Operation *op, metaconn_t *mc )
217 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
220 for ( c = 0; c < mi->mi_ntargets; c++ ) {
221 if ( mc->mc_conns[ c ].msc_ld != NULL ) {
222 meta_clear_one_candidate( &mc->mc_conns[ c ] );