]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/candidates.c
major namespace cleanup & minor fixes
[openldap] / servers / slapd / back-meta / candidates.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 The OpenLDAP Foundation.
5  * Portions Copyright 2001-2003 Pierangelo Masarati.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
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>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by the Howard Chu for inclusion
19  * in OpenLDAP Software and subsequently enhanced by Pierangelo
20  * Masarati.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include "slap.h"
28 #include "../back-ldap/back-ldap.h"
29 #include "back-meta.h"
30
31 /*
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>
35  *
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
41  *
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.
49  *
50  * A possible extension will include the handling of multiple suffixes
51  */
52
53 /*
54  * returns 1 if suffix is candidate for dn, otherwise 0
55  *
56  * Note: this function should never be called if dn is the <suffix>.
57  */
58 int 
59 meta_back_is_candidate(
60                 struct berval   *nsuffix,
61                 struct berval   *ndn
62 )
63 {
64         if ( dnIsSuffix( nsuffix, ndn ) || dnIsSuffix( ndn, nsuffix ) ) {
65                 /*
66                  * suffix longer than dn
67                  */
68                 return META_CANDIDATE;
69         }
70
71         return META_NOT_CANDIDATE;
72 }
73
74 /*
75  * meta_back_count_candidates
76  *
77  * returns a count of the possible candidate targets
78  * Note: dn MUST be normalized
79  */
80
81 int
82 meta_back_count_candidates(
83                 struct metainfo         *li,
84                 struct berval           *ndn
85 )
86 {
87         int i, cnt = 0;
88
89         /*
90          * I know assertions should not check run-time values;
91          * at present I didn't find a place for such checks
92          * after config.c
93          */
94         assert( li->targets != NULL );
95         assert( li->ntargets != 0 );
96
97         for ( i = 0; i < li->ntargets; ++i ) {
98                 if ( meta_back_is_candidate( &li->targets[ i ]->mt_nsuffix, ndn ) )
99                 {
100                         ++cnt;
101                 }
102         }
103
104         return cnt;
105 }
106
107 /*
108  * meta_back_is_candidate_unique
109  *
110  * checks whether a candidate is unique
111  * Note: dn MUST be normalized
112  */
113 int
114 meta_back_is_candidate_unique(
115                 struct metainfo         *li,
116                 struct berval           *ndn
117 )
118 {
119         return ( meta_back_count_candidates( li, ndn ) == 1 );
120 }
121
122 /*
123  * meta_back_select_unique_candidate
124  *
125  * returns the index of the candidate in case it is unique, otherwise -1
126  * Note: dn MUST be normalized.
127  * Note: if defined, the default candidate is returned in case of no match.
128  */
129 int
130 meta_back_select_unique_candidate(
131                 struct metainfo         *li,
132                 struct berval           *ndn
133 )
134 {
135         int     i;
136         
137         switch ( meta_back_count_candidates( li, ndn ) ) {
138         case 1:
139                 break;
140         case 0:
141         default:
142                 return ( li->defaulttarget == META_DEFAULT_TARGET_NONE
143                                 ? META_TARGET_NONE : li->defaulttarget );
144         }
145
146         for ( i = 0; i < li->ntargets; ++i ) {
147                 if ( meta_back_is_candidate( &li->targets[ i ]->mt_nsuffix, ndn ) )
148                 {
149                         return i;
150                 }
151         }
152
153         return META_TARGET_NONE;
154 }
155
156 /*
157  * meta_clear_unused_candidates
158  *
159  * clears all candidates except candidate
160  */
161 int
162 meta_clear_unused_candidates(
163                 struct metainfo         *li,
164                 struct metaconn         *lc,
165                 int                     candidate,
166                 int                     reallyclean
167 )
168 {
169         int i;
170         
171         for ( i = 0; i < li->ntargets; ++i ) {
172                 if ( i == candidate ) {
173                         continue;
174                 }
175                 meta_clear_one_candidate( &lc->mc_conns[ i ], reallyclean );
176         }
177
178         return 0;
179 }
180
181 /*
182  * meta_clear_one_candidate
183  *
184  * clears the selected candidate
185  */
186 int
187 meta_clear_one_candidate(
188                 struct metasingleconn   *lsc,
189                 int                     reallyclean
190 )
191 {
192         lsc->msc_candidate = META_NOT_CANDIDATE;
193
194         if ( !reallyclean ) {
195                 return 0;
196         }
197
198         if ( lsc->msc_ld ) {
199                 ldap_unbind_ext_s( lsc->msc_ld, NULL, NULL );
200                 lsc->msc_ld = NULL;
201         }
202
203         if ( !BER_BVISNULL( &lsc->msc_bound_ndn ) ) {
204                 ber_memfree( lsc->msc_bound_ndn.bv_val );
205                 BER_BVZERO( &lsc->msc_bound_ndn );
206         }
207
208         if ( !BER_BVISNULL( &lsc->msc_cred ) ) {
209                 ber_memfree( lsc->msc_cred.bv_val );
210                 BER_BVZERO( &lsc->msc_cred );
211         }
212
213         return 0;
214 }
215