]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/candidates.c
Silence "unused <something>" warnings
[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 /*
55  * returns 1 if suffix is candidate for dn, otherwise 0
56  *
57  * Note: this function should never be called if dn is the <suffix>.
58  */
59 int 
60 meta_back_is_candidate(
61         struct berval   *nsuffix,
62         struct berval   *ndn,
63         int             scope )
64 {
65         if ( dnIsSuffix( ndn, nsuffix ) ) {
66                 return META_CANDIDATE;
67         }
68
69         if ( scope == LDAP_SCOPE_SUBTREE && dnIsSuffix( nsuffix, ndn ) ) {
70                 /*
71                  * suffix longer than dn, but common part matches
72                  */
73                 return META_CANDIDATE;
74         }
75
76         return META_NOT_CANDIDATE;
77 }
78
79 #if 0
80 /*
81  * meta_back_is_candidate_unique
82  *
83  * checks whether a candidate is unique
84  * Note: dn MUST be normalized
85  */
86 static int
87 meta_back_is_candidate_unique(
88         metainfo_t      *mi,
89         struct berval   *ndn )
90 {
91         switch ( meta_back_select_unique_candidate( mi, ndn ) ) {
92         case META_TARGET_MULTIPLE:
93         case META_TARGET_NONE:
94                 return 0;
95         }
96
97         return 1;
98 }
99 #endif /* 0 */
100
101 /*
102  * meta_back_select_unique_candidate
103  *
104  * returns the index of the candidate in case it is unique, otherwise
105  * META_TARGET_NONE if none matches, or
106  * META_TARGET_MULTIPLE if more than one matches
107  * Note: ndn MUST be normalized.
108  */
109 int
110 meta_back_select_unique_candidate(
111         metainfo_t      *mi,
112         struct berval   *ndn )
113 {
114         int     i, candidate = META_TARGET_NONE;
115
116         for ( i = 0; i < mi->mi_ntargets; ++i ) {
117                 if ( meta_back_is_candidate( &mi->mi_targets[ i ].mt_nsuffix, ndn, LDAP_SCOPE_BASE ) )
118                 {
119                         if ( candidate == META_TARGET_NONE ) {
120                                 candidate = i;
121
122                         } else {
123                                 return META_TARGET_MULTIPLE;
124                         }
125                 }
126         }
127
128         return candidate;
129 }
130
131 /*
132  * meta_clear_unused_candidates
133  *
134  * clears all candidates except candidate
135  */
136 int
137 meta_clear_unused_candidates(
138         Operation       *op,
139         int             candidate )
140 {
141         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
142         int             i;
143         SlapReply       *candidates = meta_back_candidates_get( op );
144         
145         for ( i = 0; i < mi->mi_ntargets; ++i ) {
146                 if ( i == candidate ) {
147                         continue;
148                 }
149                 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
150         }
151
152         return 0;
153 }
154
155 /*
156  * meta_clear_one_candidate
157  *
158  * clears the selected candidate
159  */
160 int
161 meta_clear_one_candidate(
162         metasingleconn_t        *msc )
163 {
164         if ( msc->msc_ld ) {
165                 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
166                 msc->msc_ld = NULL;
167         }
168
169         if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
170                 ber_memfree( msc->msc_bound_ndn.bv_val );
171                 BER_BVZERO( &msc->msc_bound_ndn );
172         }
173
174         if ( !BER_BVISNULL( &msc->msc_cred ) ) {
175                 ber_memfree( msc->msc_cred.bv_val );
176                 BER_BVZERO( &msc->msc_cred );
177         }
178
179         return 0;
180 }
181