1 /* candidates.c - candidate targets selection and processing for
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 * Copyright 2016-2018 The OpenLDAP Foundation.
7 * Portions Copyright 2016 Symas Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
20 + * This work was developed by Symas Corporation
21 + * based on back-meta module for inclusion in OpenLDAP Software.
22 + * This work was sponsored by Ericsson. */
27 #include "ac/string.h"
30 #include "../back-ldap/back-ldap.h"
31 #include "back-asyncmeta.h"
34 * The meta-directory has one suffix, called <suffix>.
35 * It handles a pool of target servers, each with a branch suffix
36 * of the form <branch X>,<suffix>, where <branch X> may be empty.
38 * When the meta-directory receives a request with a request DN that belongs
39 * to a branch, the corresponding target is invoked. When the request DN
40 * does not belong to a specific branch, all the targets that
41 * are compatible with the request DN are selected as candidates, and
42 * the request is spawned to all the candidate targets
44 * A request is characterized by a request DN. The following cases are
46 * - the request DN is the suffix: <dn> == <suffix>,
47 * all the targets are candidates (search ...)
48 * - the request DN is a branch suffix: <dn> == <branch X>,<suffix>, or
49 * - the request DN is a subtree of a branch suffix:
50 * <dn> == <rdn>,<branch X>,<suffix>,
51 * the target is the only candidate.
53 * A possible extension will include the handling of multiple suffixes
56 static a_metasubtree_t *
57 asyncmeta_subtree_match( a_metatarget_t *mt, struct berval *ndn, int scope )
59 a_metasubtree_t *ms = mt->mt_subtree;
61 for ( ms = mt->mt_subtree; ms; ms = ms->ms_next ) {
62 switch ( ms->ms_type ) {
64 if ( dnIsSuffix( ndn, &ms->ms_dn ) ) {
69 case META_ST_SUBORDINATE:
70 if ( dnIsSuffix( ndn, &ms->ms_dn ) &&
71 ( ndn->bv_len > ms->ms_dn.bv_len || scope != LDAP_SCOPE_BASE ) )
78 /* NOTE: cannot handle scope */
79 if ( regexec( &ms->ms_regex, ndn->bv_val, 0, NULL, 0 ) == 0 ) {
90 * returns 1 if suffix is candidate for dn, otherwise 0
92 * Note: this function should never be called if dn is the <suffix>.
95 asyncmeta_is_candidate(
101 int d = ndn->bv_len - mt->mt_nsuffix.bv_len;
104 if ( !dnIsSuffix( ndn, &mt->mt_nsuffix ) ) {
105 return META_NOT_CANDIDATE;
109 * | match | exclude |
110 * +---------+---------+-------------------+
111 * | T | T | not candidate |
112 * | F | T | continue checking |
113 * +---------+---------+-------------------+
114 * | T | F | candidate |
115 * | F | F | not candidate |
116 * +---------+---------+-------------------+
119 if ( mt->mt_subtree ) {
120 int match = ( asyncmeta_subtree_match( mt, ndn, scope ) != NULL );
122 if ( !mt->mt_subtree_exclude ) {
123 return match ? META_CANDIDATE : META_NOT_CANDIDATE;
126 if ( match /* && mt->mt_subtree_exclude */ ) {
127 return META_NOT_CANDIDATE;
131 switch ( mt->mt_scope ) {
132 case LDAP_SCOPE_SUBTREE:
134 return META_CANDIDATE;
136 case LDAP_SCOPE_SUBORDINATE:
138 return META_CANDIDATE;
142 /* nearly useless; not allowed by config */
143 case LDAP_SCOPE_ONELEVEL:
145 rdn.bv_val = ndn->bv_val;
146 rdn.bv_len = (ber_len_t)d - STRLENOF( "," );
147 if ( dnIsOneLevelRDN( &rdn ) ) {
148 return META_CANDIDATE;
153 /* nearly useless; not allowed by config */
154 case LDAP_SCOPE_BASE:
156 return META_CANDIDATE;
161 } else /* if ( d < 0 ) */ {
162 if ( !dnIsSuffix( &mt->mt_nsuffix, ndn ) ) {
163 return META_NOT_CANDIDATE;
167 case LDAP_SCOPE_SUBTREE:
168 case LDAP_SCOPE_SUBORDINATE:
170 * suffix longer than dn, but common part matches
172 return META_CANDIDATE;
174 case LDAP_SCOPE_ONELEVEL:
175 rdn.bv_val = mt->mt_nsuffix.bv_val;
176 rdn.bv_len = (ber_len_t)(-d) - STRLENOF( "," );
177 if ( dnIsOneLevelRDN( &rdn ) ) {
178 return META_CANDIDATE;
184 return META_NOT_CANDIDATE;
188 * meta_back_select_unique_candidate
190 * returns the index of the candidate in case it is unique, otherwise
191 * META_TARGET_NONE if none matches, or
192 * META_TARGET_MULTIPLE if more than one matches
193 * Note: ndn MUST be normalized.
196 asyncmeta_select_unique_candidate(
200 int i, candidate = META_TARGET_NONE;
202 for ( i = 0; i < mi->mi_ntargets; i++ ) {
203 a_metatarget_t *mt = mi->mi_targets[ i ];
205 if ( asyncmeta_is_candidate( mt, ndn, LDAP_SCOPE_BASE ) ) {
206 if ( candidate == META_TARGET_NONE ) {
210 return META_TARGET_MULTIPLE;
219 * asyncmeta_clear_unused_candidates
221 * clears all candidates except candidate
224 asyncmeta_clear_unused_candidates(
228 SlapReply *candidates)
230 a_metainfo_t *mi = mc->mc_info;
233 for ( i = 0; i < mi->mi_ntargets; ++i ) {
234 if ( i == candidate ) {
237 META_CANDIDATE_RESET( &candidates[ i ] );
244 asyncmeta_clear_one_msc(
249 a_metasingleconn_t *msc;
253 msc = &mc->mc_conns[candidate];
255 connection_client_stop( msc->conn );
258 if ( msc->msc_ld != NULL ) {
263 snprintf( buf, sizeof( buf ), "asyncmeta_clear_one_msc ldap_unbind_ext[%d] ld=%p",
264 candidate, (void *)msc->msc_ld );
265 Debug( LDAP_DEBUG_ANY, "### %s %s\n",
266 op ? op->o_log_prefix : "", buf, 0 );
267 #endif /* DEBUG_205 */
269 ldap_unbind_ext( msc->msc_ld, NULL, NULL );
271 ldap_ld_free( msc->msc_ldr, 0, NULL, NULL );
275 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
276 ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
277 BER_BVZERO( &msc->msc_bound_ndn );
280 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
281 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
282 ber_memfree_x( msc->msc_cred.bv_val, NULL );
283 BER_BVZERO( &msc->msc_cred );
286 msc->msc_mscflags = 0;
287 msc->msc_timeout_ops = 0;
288 msc->msc_pending_ops = 0;