Home | History | Annotate | Line # | Download | only in back-meta
candidates.c revision 1.1
      1  1.1  lukem /* $OpenLDAP: pkg/ldap/servers/slapd/back-meta/candidates.c,v 1.28.2.5 2008/02/11 23:26:46 kurt Exp $ */
      2  1.1  lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      3  1.1  lukem  *
      4  1.1  lukem  * Copyright 1999-2008 The OpenLDAP Foundation.
      5  1.1  lukem  * Portions Copyright 2001-2003 Pierangelo Masarati.
      6  1.1  lukem  * Portions Copyright 1999-2003 Howard Chu.
      7  1.1  lukem  * All rights reserved.
      8  1.1  lukem  *
      9  1.1  lukem  * Redistribution and use in source and binary forms, with or without
     10  1.1  lukem  * modification, are permitted only as authorized by the OpenLDAP
     11  1.1  lukem  * Public License.
     12  1.1  lukem  *
     13  1.1  lukem  * A copy of this license is available in the file LICENSE in the
     14  1.1  lukem  * top-level directory of the distribution or, alternatively, at
     15  1.1  lukem  * <http://www.OpenLDAP.org/license.html>.
     16  1.1  lukem  */
     17  1.1  lukem /* ACKNOWLEDGEMENTS:
     18  1.1  lukem  * This work was initially developed by the Howard Chu for inclusion
     19  1.1  lukem  * in OpenLDAP Software and subsequently enhanced by Pierangelo
     20  1.1  lukem  * Masarati.
     21  1.1  lukem  */
     22  1.1  lukem 
     23  1.1  lukem #include "portable.h"
     24  1.1  lukem 
     25  1.1  lukem #include <stdio.h>
     26  1.1  lukem #include "ac/string.h"
     27  1.1  lukem 
     28  1.1  lukem #include "slap.h"
     29  1.1  lukem #include "../back-ldap/back-ldap.h"
     30  1.1  lukem #include "back-meta.h"
     31  1.1  lukem 
     32  1.1  lukem /*
     33  1.1  lukem  * The meta-directory has one suffix, called <suffix>.
     34  1.1  lukem  * It handles a pool of target servers, each with a branch suffix
     35  1.1  lukem  * of the form <branch X>,<suffix>
     36  1.1  lukem  *
     37  1.1  lukem  * When the meta-directory receives a request with a dn that belongs
     38  1.1  lukem  * to a branch, the corresponding target is invoked. When the dn
     39  1.1  lukem  * does not belong to a specific branch, all the targets that
     40  1.1  lukem  * are compatible with the dn are selected as candidates, and
     41  1.1  lukem  * the request is spawned to all the candidate targets
     42  1.1  lukem  *
     43  1.1  lukem  * A request is characterized by a dn. The following cases are handled:
     44  1.1  lukem  * 	- the dn is the suffix: <dn> == <suffix>,
     45  1.1  lukem  * 		all the targets are candidates (search ...)
     46  1.1  lukem  * 	- the dn is a branch suffix: <dn> == <branch X>,<suffix>, or
     47  1.1  lukem  * 	- the dn is a subtree of a branch suffix:
     48  1.1  lukem  * 		<dn> == <rdn>,<branch X>,<suffix>,
     49  1.1  lukem  * 		the target is the only candidate.
     50  1.1  lukem  *
     51  1.1  lukem  * A possible extension will include the handling of multiple suffixes
     52  1.1  lukem  */
     53  1.1  lukem 
     54  1.1  lukem 
     55  1.1  lukem /*
     56  1.1  lukem  * returns 1 if suffix is candidate for dn, otherwise 0
     57  1.1  lukem  *
     58  1.1  lukem  * Note: this function should never be called if dn is the <suffix>.
     59  1.1  lukem  */
     60  1.1  lukem int
     61  1.1  lukem meta_back_is_candidate(
     62  1.1  lukem 	metatarget_t	*mt,
     63  1.1  lukem 	struct berval	*ndn,
     64  1.1  lukem 	int		scope )
     65  1.1  lukem {
     66  1.1  lukem 	if ( dnIsSuffix( ndn, &mt->mt_nsuffix ) ) {
     67  1.1  lukem 		if ( mt->mt_subtree_exclude ) {
     68  1.1  lukem 			int	i;
     69  1.1  lukem 
     70  1.1  lukem 			for ( i = 0; !BER_BVISNULL( &mt->mt_subtree_exclude[ i ] ); i++ ) {
     71  1.1  lukem 				if ( dnIsSuffix( ndn, &mt->mt_subtree_exclude[ i ] ) ) {
     72  1.1  lukem 					return META_NOT_CANDIDATE;
     73  1.1  lukem 				}
     74  1.1  lukem 			}
     75  1.1  lukem 		}
     76  1.1  lukem 
     77  1.1  lukem 		switch ( mt->mt_scope ) {
     78  1.1  lukem 		case LDAP_SCOPE_SUBTREE:
     79  1.1  lukem 		default:
     80  1.1  lukem 			return META_CANDIDATE;
     81  1.1  lukem 
     82  1.1  lukem 		case LDAP_SCOPE_SUBORDINATE:
     83  1.1  lukem 			if ( ndn->bv_len > mt->mt_nsuffix.bv_len ) {
     84  1.1  lukem 				return META_CANDIDATE;
     85  1.1  lukem 			}
     86  1.1  lukem 			break;
     87  1.1  lukem 
     88  1.1  lukem 		/* nearly useless; not allowed by config */
     89  1.1  lukem 		case LDAP_SCOPE_ONELEVEL:
     90  1.1  lukem 			if ( ndn->bv_len > mt->mt_nsuffix.bv_len ) {
     91  1.1  lukem 				struct berval	rdn = *ndn;
     92  1.1  lukem 
     93  1.1  lukem 				rdn.bv_len -= mt->mt_nsuffix.bv_len
     94  1.1  lukem 					+ STRLENOF( "," );
     95  1.1  lukem 				if ( dnIsOneLevelRDN( &rdn ) ) {
     96  1.1  lukem 					return META_CANDIDATE;
     97  1.1  lukem 				}
     98  1.1  lukem 			}
     99  1.1  lukem 			break;
    100  1.1  lukem 
    101  1.1  lukem 		/* nearly useless; not allowed by config */
    102  1.1  lukem 		case LDAP_SCOPE_BASE:
    103  1.1  lukem 			if ( ndn->bv_len == mt->mt_nsuffix.bv_len ) {
    104  1.1  lukem 				return META_CANDIDATE;
    105  1.1  lukem 			}
    106  1.1  lukem 			break;
    107  1.1  lukem 		}
    108  1.1  lukem 
    109  1.1  lukem 		return META_NOT_CANDIDATE;
    110  1.1  lukem 	}
    111  1.1  lukem 
    112  1.1  lukem 	if ( scope == LDAP_SCOPE_SUBTREE && dnIsSuffix( &mt->mt_nsuffix, ndn ) ) {
    113  1.1  lukem 		/*
    114  1.1  lukem 		 * suffix longer than dn, but common part matches
    115  1.1  lukem 		 */
    116  1.1  lukem 		return META_CANDIDATE;
    117  1.1  lukem 	}
    118  1.1  lukem 
    119  1.1  lukem 	return META_NOT_CANDIDATE;
    120  1.1  lukem }
    121  1.1  lukem 
    122  1.1  lukem /*
    123  1.1  lukem  * meta_back_select_unique_candidate
    124  1.1  lukem  *
    125  1.1  lukem  * returns the index of the candidate in case it is unique, otherwise
    126  1.1  lukem  * META_TARGET_NONE if none matches, or
    127  1.1  lukem  * META_TARGET_MULTIPLE if more than one matches
    128  1.1  lukem  * Note: ndn MUST be normalized.
    129  1.1  lukem  */
    130  1.1  lukem int
    131  1.1  lukem meta_back_select_unique_candidate(
    132  1.1  lukem 	metainfo_t	*mi,
    133  1.1  lukem 	struct berval	*ndn )
    134  1.1  lukem {
    135  1.1  lukem 	int	i, candidate = META_TARGET_NONE;
    136  1.1  lukem 
    137  1.1  lukem 	for ( i = 0; i < mi->mi_ntargets; i++ ) {
    138  1.1  lukem 		metatarget_t	*mt = mi->mi_targets[ i ];
    139  1.1  lukem 
    140  1.1  lukem 		if ( meta_back_is_candidate( mt, ndn, LDAP_SCOPE_BASE ) ) {
    141  1.1  lukem 			if ( candidate == META_TARGET_NONE ) {
    142  1.1  lukem 				candidate = i;
    143  1.1  lukem 
    144  1.1  lukem 			} else {
    145  1.1  lukem 				return META_TARGET_MULTIPLE;
    146  1.1  lukem 			}
    147  1.1  lukem 		}
    148  1.1  lukem 	}
    149  1.1  lukem 
    150  1.1  lukem 	return candidate;
    151  1.1  lukem }
    152  1.1  lukem 
    153  1.1  lukem /*
    154  1.1  lukem  * meta_clear_unused_candidates
    155  1.1  lukem  *
    156  1.1  lukem  * clears all candidates except candidate
    157  1.1  lukem  */
    158  1.1  lukem int
    159  1.1  lukem meta_clear_unused_candidates(
    160  1.1  lukem 	Operation	*op,
    161  1.1  lukem 	int		candidate )
    162  1.1  lukem {
    163  1.1  lukem 	metainfo_t	*mi = ( metainfo_t * )op->o_bd->be_private;
    164  1.1  lukem 	int		i;
    165  1.1  lukem 	SlapReply	*candidates = meta_back_candidates_get( op );
    166  1.1  lukem 
    167  1.1  lukem 	for ( i = 0; i < mi->mi_ntargets; ++i ) {
    168  1.1  lukem 		if ( i == candidate ) {
    169  1.1  lukem 			continue;
    170  1.1  lukem 		}
    171  1.1  lukem 		META_CANDIDATE_RESET( &candidates[ i ] );
    172  1.1  lukem 	}
    173  1.1  lukem 
    174  1.1  lukem 	return 0;
    175  1.1  lukem }
    176  1.1  lukem 
    177  1.1  lukem /*
    178  1.1  lukem  * meta_clear_one_candidate
    179  1.1  lukem  *
    180  1.1  lukem  * clears the selected candidate
    181  1.1  lukem  */
    182  1.1  lukem int
    183  1.1  lukem meta_clear_one_candidate(
    184  1.1  lukem 	Operation	*op,
    185  1.1  lukem 	metaconn_t	*mc,
    186  1.1  lukem 	int		candidate )
    187  1.1  lukem {
    188  1.1  lukem 	metasingleconn_t	*msc = &mc->mc_conns[ candidate ];
    189  1.1  lukem 
    190  1.1  lukem 	if ( msc->msc_ld != NULL ) {
    191  1.1  lukem 
    192  1.1  lukem #ifdef DEBUG_205
    193  1.1  lukem 		char	buf[ BUFSIZ ];
    194  1.1  lukem 
    195  1.1  lukem 		snprintf( buf, sizeof( buf ), "meta_clear_one_candidate ldap_unbind_ext[%d] mc=%p ld=%p",
    196  1.1  lukem 			candidate, (void *)mc, (void *)msc->msc_ld );
    197  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "### %s %s\n",
    198  1.1  lukem 			op ? op->o_log_prefix : "", buf, 0 );
    199  1.1  lukem #endif /* DEBUG_205 */
    200  1.1  lukem 
    201  1.1  lukem 		ldap_unbind_ext( msc->msc_ld, NULL, NULL );
    202  1.1  lukem 		msc->msc_ld = NULL;
    203  1.1  lukem 	}
    204  1.1  lukem 
    205  1.1  lukem 	if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
    206  1.1  lukem 		ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
    207  1.1  lukem 		BER_BVZERO( &msc->msc_bound_ndn );
    208  1.1  lukem 	}
    209  1.1  lukem 
    210  1.1  lukem 	if ( !BER_BVISNULL( &msc->msc_cred ) ) {
    211  1.1  lukem 		memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
    212  1.1  lukem 		ber_memfree_x( msc->msc_cred.bv_val, NULL );
    213  1.1  lukem 		BER_BVZERO( &msc->msc_cred );
    214  1.1  lukem 	}
    215  1.1  lukem 
    216  1.1  lukem 	msc->msc_mscflags = 0;
    217  1.1  lukem 
    218  1.1  lukem 	return 0;
    219  1.1  lukem }
    220  1.1  lukem 
    221