]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/candidates.c
Additional fix for ITS#4522. The "dn=" ist not optional.
[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                 metatarget_t    *mt = mi->mi_targets[ i ];
141
142                 if ( meta_back_is_candidate( &mt->mt_nsuffix,
143                                 mt->mt_scope,
144                                 mt->mt_subtree_exclude,
145                                 ndn, LDAP_SCOPE_BASE ) )
146                 {
147                         if ( candidate == META_TARGET_NONE ) {
148                                 candidate = i;
149
150                         } else {
151                                 return META_TARGET_MULTIPLE;
152                         }
153                 }
154         }
155
156         return candidate;
157 }
158
159 /*
160  * meta_clear_unused_candidates
161  *
162  * clears all candidates except candidate
163  */
164 int
165 meta_clear_unused_candidates(
166         Operation       *op,
167         int             candidate )
168 {
169         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
170         int             i;
171         SlapReply       *candidates = meta_back_candidates_get( op );
172         
173         for ( i = 0; i < mi->mi_ntargets; ++i ) {
174                 if ( i == candidate ) {
175                         continue;
176                 }
177                 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
178         }
179
180         return 0;
181 }
182
183 /*
184  * meta_clear_one_candidate
185  *
186  * clears the selected candidate
187  */
188 int
189 meta_clear_one_candidate(
190         metasingleconn_t        *msc )
191 {
192         if ( msc->msc_ld ) {
193                 ldap_unbind_ext( msc->msc_ld, NULL, NULL );
194                 msc->msc_ld = NULL;
195         }
196
197         if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
198                 ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
199                 BER_BVZERO( &msc->msc_bound_ndn );
200         }
201
202         if ( !BER_BVISNULL( &msc->msc_cred ) ) {
203                 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
204                 ber_memfree_x( msc->msc_cred.bv_val, NULL );
205                 BER_BVZERO( &msc->msc_cred );
206         }
207
208         return 0;
209 }
210
211 /*
212  * meta_clear_candidates
213  *
214  * clears all candidates
215  */
216 int
217 meta_clear_candidates( Operation *op, metaconn_t *mc )
218 {
219         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
220         int             c;
221
222         for ( c = 0; c < mi->mi_ntargets; c++ ) {
223                 if ( mc->mc_conns[ c ].msc_ld != NULL ) {
224                         meta_clear_one_candidate( &mc->mc_conns[ c ] );
225                 }
226         }
227
228         return 0;
229 }