]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/candidates.c
Changes suggested by Ando.
[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-2006 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 #include "ac/string.h"
27
28 #include "slap.h"
29 #include "../back-ldap/back-ldap.h"
30 #include "back-meta.h"
31
32 /*
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>
36  *
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
42  *
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.
50  *
51  * A possible extension will include the handling of multiple suffixes
52  */
53
54
55 /*
56  * returns 1 if suffix is candidate for dn, otherwise 0
57  *
58  * Note: this function should never be called if dn is the <suffix>.
59  */
60 int 
61 meta_back_is_candidate(
62         struct berval   *nsuffix,
63         int             suffixscope,
64         BerVarray       subtree_exclude,
65         struct berval   *ndn,
66         int             scope )
67 {
68         if ( dnIsSuffix( ndn, nsuffix ) ) {
69                 if ( subtree_exclude ) {
70                         int     i;
71
72                         for ( i = 0; !BER_BVISNULL( &subtree_exclude[ i ] ); i++ ) {
73                                 if ( dnIsSuffix( ndn, &subtree_exclude[ i ] ) ) {
74                                         return META_NOT_CANDIDATE;
75                                 }
76                         }
77                 }
78
79                 switch ( suffixscope ) {
80                 case LDAP_SCOPE_SUBTREE:
81                 default:
82                         return META_CANDIDATE;
83
84                 case LDAP_SCOPE_SUBORDINATE:
85                         if ( ndn->bv_len > nsuffix->bv_len ) {
86                                 return META_CANDIDATE;
87                         }
88                         break;
89
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;
94
95                                 rdn.bv_len -= nsuffix->bv_len
96                                         + STRLENOF( "," );
97                                 if ( dnIsOneLevelRDN( &rdn ) ) {
98                                         return META_CANDIDATE;
99                                 }
100                         }
101                         break;
102
103                 /* nearly useless; not allowed by config */
104                 case LDAP_SCOPE_BASE:
105                         if ( ndn->bv_len == nsuffix->bv_len ) {
106                                 return META_CANDIDATE;
107                         }
108                         break;
109                 }
110
111                 return META_NOT_CANDIDATE;
112         }
113
114         if ( scope == LDAP_SCOPE_SUBTREE && dnIsSuffix( nsuffix, ndn ) ) {
115                 /*
116                  * suffix longer than dn, but common part matches
117                  */
118                 return META_CANDIDATE;
119         }
120
121         return META_NOT_CANDIDATE;
122 }
123
124 /*
125  * meta_back_select_unique_candidate
126  *
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.
131  */
132 int
133 meta_back_select_unique_candidate(
134         metainfo_t      *mi,
135         struct berval   *ndn )
136 {
137         int     i, candidate = META_TARGET_NONE;
138
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 ) )
144                 {
145                         if ( candidate == META_TARGET_NONE ) {
146                                 candidate = i;
147
148                         } else {
149                                 return META_TARGET_MULTIPLE;
150                         }
151                 }
152         }
153
154         return candidate;
155 }
156
157 /*
158  * meta_clear_unused_candidates
159  *
160  * clears all candidates except candidate
161  */
162 int
163 meta_clear_unused_candidates(
164         Operation       *op,
165         int             candidate )
166 {
167         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
168         int             i;
169         SlapReply       *candidates = meta_back_candidates_get( op );
170         
171         for ( i = 0; i < mi->mi_ntargets; ++i ) {
172                 if ( i == candidate ) {
173                         continue;
174                 }
175                 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
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         metasingleconn_t        *msc )
189 {
190         if ( msc->msc_ld ) {
191                 ldap_unbind_ext( msc->msc_ld, NULL, NULL );
192                 msc->msc_ld = NULL;
193         }
194
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 );
198         }
199
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 );
204         }
205
206         return 0;
207 }
208
209 /*
210  * meta_clear_candidates
211  *
212  * clears all candidates
213  */
214 int
215 meta_clear_candidates( Operation *op, metaconn_t *mc )
216 {
217         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
218         int             c;
219
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 ] );
223                 }
224         }
225
226         return 0;
227 }