]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/candidates.c
1254a26184ea52ec93a5dea7de2c2524309dc2d7
[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 #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         struct berval   *ndn,
65         int             scope )
66 {
67         if ( dnIsSuffix( ndn, nsuffix ) ) {
68                 switch ( suffixscope ) {
69                 case LDAP_SCOPE_SUBTREE:
70                 default:
71                         return META_CANDIDATE;
72
73 #ifdef LDAP_SCOPE_SUBORDINATE
74                 case LDAP_SCOPE_SUBORDINATE:
75                         if ( ndn->bv_len > nsuffix->bv_len ) {
76                                 return META_CANDIDATE;
77                         }
78                         break;
79 #endif /* LDAP_SCOPE_SUBORDINATE */
80
81                 /* nearly useless; not allowed by config */
82                 case LDAP_SCOPE_ONELEVEL:
83                         if ( ndn->bv_len > nsuffix->bv_len ) {
84                                 struct berval   rdn = *ndn;
85
86                                 rdn.bv_len -= nsuffix->bv_len
87                                         + STRLENOF( "," );
88                                 if ( dnIsOneLevelRDN( &rdn ) ) {
89                                         return META_CANDIDATE;
90                                 }
91                         }
92                         break;
93
94                 /* nearly useless; not allowed by config */
95                 case LDAP_SCOPE_BASE:
96                         if ( ndn->bv_len == nsuffix->bv_len ) {
97                                 return META_CANDIDATE;
98                         }
99                         break;
100                 }
101
102                 return META_NOT_CANDIDATE;
103         }
104
105         if ( scope == LDAP_SCOPE_SUBTREE && dnIsSuffix( nsuffix, ndn ) ) {
106                 /*
107                  * suffix longer than dn, but common part matches
108                  */
109                 return META_CANDIDATE;
110         }
111
112         return META_NOT_CANDIDATE;
113 }
114
115 /*
116  * meta_back_select_unique_candidate
117  *
118  * returns the index of the candidate in case it is unique, otherwise
119  * META_TARGET_NONE if none matches, or
120  * META_TARGET_MULTIPLE if more than one matches
121  * Note: ndn MUST be normalized.
122  */
123 int
124 meta_back_select_unique_candidate(
125         metainfo_t      *mi,
126         struct berval   *ndn )
127 {
128         int     i, candidate = META_TARGET_NONE;
129
130         for ( i = 0; i < mi->mi_ntargets; ++i ) {
131                 if ( meta_back_is_candidate( &mi->mi_targets[ i ].mt_nsuffix,
132                                 mi->mi_targets[ i ].mt_scope,
133                                 ndn, LDAP_SCOPE_BASE ) )
134                 {
135                         if ( candidate == META_TARGET_NONE ) {
136                                 candidate = i;
137
138                         } else {
139                                 return META_TARGET_MULTIPLE;
140                         }
141                 }
142         }
143
144         return candidate;
145 }
146
147 /*
148  * meta_clear_unused_candidates
149  *
150  * clears all candidates except candidate
151  */
152 int
153 meta_clear_unused_candidates(
154         Operation       *op,
155         int             candidate )
156 {
157         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
158         int             i;
159         SlapReply       *candidates = meta_back_candidates_get( op );
160         
161         for ( i = 0; i < mi->mi_ntargets; ++i ) {
162                 if ( i == candidate ) {
163                         continue;
164                 }
165                 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
166         }
167
168         return 0;
169 }
170
171 /*
172  * meta_clear_one_candidate
173  *
174  * clears the selected candidate
175  */
176 int
177 meta_clear_one_candidate(
178         metasingleconn_t        *msc )
179 {
180         if ( msc->msc_ld ) {
181                 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
182                 msc->msc_ld = NULL;
183         }
184
185         if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
186                 ber_memfree( msc->msc_bound_ndn.bv_val );
187                 BER_BVZERO( &msc->msc_bound_ndn );
188         }
189
190         if ( !BER_BVISNULL( &msc->msc_cred ) ) {
191                 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
192                 ber_memfree( msc->msc_cred.bv_val );
193                 BER_BVZERO( &msc->msc_cred );
194         }
195
196         return 0;
197 }
198