2003-11-27 14:35:14 +08:00
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2007-01-03 04:00:42 +08:00
|
|
|
* Copyright 1999-2007 The OpenLDAP Foundation.
|
2003-12-09 01:41:40 +08:00
|
|
|
* Portions Copyright 2001-2003 Pierangelo Masarati.
|
|
|
|
* Portions Copyright 1999-2003 Howard Chu.
|
2003-11-27 14:35:14 +08:00
|
|
|
* All rights reserved.
|
2001-05-12 08:51:28 +08:00
|
|
|
*
|
2003-11-27 14:35:14 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
|
|
|
/* ACKNOWLEDGEMENTS:
|
|
|
|
* This work was initially developed by the Howard Chu for inclusion
|
|
|
|
* in OpenLDAP Software and subsequently enhanced by Pierangelo
|
|
|
|
* Masarati.
|
|
|
|
*/
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2005-08-17 16:34:49 +08:00
|
|
|
#include "ac/string.h"
|
2001-05-12 08:51:28 +08:00
|
|
|
|
|
|
|
#include "slap.h"
|
|
|
|
#include "../back-ldap/back-ldap.h"
|
|
|
|
#include "back-meta.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The meta-directory has one suffix, called <suffix>.
|
|
|
|
* It handles a pool of target servers, each with a branch suffix
|
|
|
|
* of the form <branch X>,<suffix>
|
|
|
|
*
|
|
|
|
* When the meta-directory receives a request with a dn that belongs
|
|
|
|
* to a branch, the corresponding target is invoked. When the dn
|
|
|
|
* does not belong to a specific branch, all the targets that
|
|
|
|
* are compatible with the dn are selected as candidates, and
|
|
|
|
* the request is spawned to all the candidate targets
|
|
|
|
*
|
|
|
|
* A request is characterized by a dn. The following cases are handled:
|
|
|
|
* - the dn is the suffix: <dn> == <suffix>,
|
|
|
|
* all the targets are candidates (search ...)
|
|
|
|
* - the dn is a branch suffix: <dn> == <branch X>,<suffix>, or
|
|
|
|
* - the dn is a subtree of a branch suffix:
|
|
|
|
* <dn> == <rdn>,<branch X>,<suffix>,
|
|
|
|
* the target is the only candidate.
|
|
|
|
*
|
|
|
|
* A possible extension will include the handling of multiple suffixes
|
|
|
|
*/
|
|
|
|
|
2005-04-16 10:25:41 +08:00
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
/*
|
|
|
|
* returns 1 if suffix is candidate for dn, otherwise 0
|
|
|
|
*
|
|
|
|
* Note: this function should never be called if dn is the <suffix>.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
meta_back_is_candidate(
|
2006-05-31 22:11:07 +08:00
|
|
|
metatarget_t *mt,
|
2005-04-23 05:43:52 +08:00
|
|
|
struct berval *ndn,
|
|
|
|
int scope )
|
2001-05-12 08:51:28 +08:00
|
|
|
{
|
2006-05-31 22:11:07 +08:00
|
|
|
if ( dnIsSuffix( ndn, &mt->mt_nsuffix ) ) {
|
|
|
|
if ( mt->mt_subtree_exclude ) {
|
2006-02-16 09:26:39 +08:00
|
|
|
int i;
|
|
|
|
|
2006-05-31 22:11:07 +08:00
|
|
|
for ( i = 0; !BER_BVISNULL( &mt->mt_subtree_exclude[ i ] ); i++ ) {
|
|
|
|
if ( dnIsSuffix( ndn, &mt->mt_subtree_exclude[ i ] ) ) {
|
2006-02-16 09:26:39 +08:00
|
|
|
return META_NOT_CANDIDATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-31 22:11:07 +08:00
|
|
|
switch ( mt->mt_scope ) {
|
2005-08-17 16:34:49 +08:00
|
|
|
case LDAP_SCOPE_SUBTREE:
|
|
|
|
default:
|
|
|
|
return META_CANDIDATE;
|
|
|
|
|
|
|
|
case LDAP_SCOPE_SUBORDINATE:
|
2006-05-31 22:11:07 +08:00
|
|
|
if ( ndn->bv_len > mt->mt_nsuffix.bv_len ) {
|
2005-08-17 16:34:49 +08:00
|
|
|
return META_CANDIDATE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* nearly useless; not allowed by config */
|
|
|
|
case LDAP_SCOPE_ONELEVEL:
|
2006-05-31 22:11:07 +08:00
|
|
|
if ( ndn->bv_len > mt->mt_nsuffix.bv_len ) {
|
2005-08-17 16:34:49 +08:00
|
|
|
struct berval rdn = *ndn;
|
|
|
|
|
2006-05-31 22:11:07 +08:00
|
|
|
rdn.bv_len -= mt->mt_nsuffix.bv_len
|
2005-08-17 16:34:49 +08:00
|
|
|
+ STRLENOF( "," );
|
|
|
|
if ( dnIsOneLevelRDN( &rdn ) ) {
|
|
|
|
return META_CANDIDATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* nearly useless; not allowed by config */
|
|
|
|
case LDAP_SCOPE_BASE:
|
2006-05-31 22:11:07 +08:00
|
|
|
if ( ndn->bv_len == mt->mt_nsuffix.bv_len ) {
|
2005-08-17 16:34:49 +08:00
|
|
|
return META_CANDIDATE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return META_NOT_CANDIDATE;
|
2005-04-21 11:17:31 +08:00
|
|
|
}
|
|
|
|
|
2006-05-31 22:11:07 +08:00
|
|
|
if ( scope == LDAP_SCOPE_SUBTREE && dnIsSuffix( &mt->mt_nsuffix, ndn ) ) {
|
2001-05-12 08:51:28 +08:00
|
|
|
/*
|
2005-04-21 11:17:31 +08:00
|
|
|
* suffix longer than dn, but common part matches
|
2001-05-12 08:51:28 +08:00
|
|
|
*/
|
|
|
|
return META_CANDIDATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return META_NOT_CANDIDATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* meta_back_select_unique_candidate
|
|
|
|
*
|
2005-06-29 07:22:48 +08:00
|
|
|
* returns the index of the candidate in case it is unique, otherwise
|
|
|
|
* META_TARGET_NONE if none matches, or
|
|
|
|
* META_TARGET_MULTIPLE if more than one matches
|
|
|
|
* Note: ndn MUST be normalized.
|
2001-05-12 08:51:28 +08:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
meta_back_select_unique_candidate(
|
2005-04-23 05:43:52 +08:00
|
|
|
metainfo_t *mi,
|
|
|
|
struct berval *ndn )
|
2001-05-12 08:51:28 +08:00
|
|
|
{
|
2005-04-16 10:25:41 +08:00
|
|
|
int i, candidate = META_TARGET_NONE;
|
2001-05-12 08:51:28 +08:00
|
|
|
|
2006-05-07 00:15:25 +08:00
|
|
|
for ( i = 0; i < mi->mi_ntargets; i++ ) {
|
|
|
|
metatarget_t *mt = mi->mi_targets[ i ];
|
|
|
|
|
2006-05-31 22:11:07 +08:00
|
|
|
if ( meta_back_is_candidate( mt, ndn, LDAP_SCOPE_BASE ) ) {
|
2005-04-16 10:25:41 +08:00
|
|
|
if ( candidate == META_TARGET_NONE ) {
|
|
|
|
candidate = i;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return META_TARGET_MULTIPLE;
|
|
|
|
}
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-16 10:25:41 +08:00
|
|
|
return candidate;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* meta_clear_unused_candidates
|
|
|
|
*
|
|
|
|
* clears all candidates except candidate
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
meta_clear_unused_candidates(
|
2005-04-23 05:43:52 +08:00
|
|
|
Operation *op,
|
|
|
|
int candidate )
|
2001-05-12 08:51:28 +08:00
|
|
|
{
|
2005-04-23 05:43:52 +08:00
|
|
|
metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
|
2005-04-16 10:25:41 +08:00
|
|
|
int i;
|
2005-04-22 06:22:43 +08:00
|
|
|
SlapReply *candidates = meta_back_candidates_get( op );
|
2001-05-12 08:51:28 +08:00
|
|
|
|
2005-04-23 05:43:52 +08:00
|
|
|
for ( i = 0; i < mi->mi_ntargets; ++i ) {
|
2001-05-12 08:51:28 +08:00
|
|
|
if ( i == candidate ) {
|
|
|
|
continue;
|
|
|
|
}
|
2006-06-03 01:30:15 +08:00
|
|
|
META_CANDIDATE_RESET( &candidates[ i ] );
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* meta_clear_one_candidate
|
|
|
|
*
|
|
|
|
* clears the selected candidate
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
meta_clear_one_candidate(
|
2006-10-29 00:20:59 +08:00
|
|
|
Operation *op,
|
|
|
|
metaconn_t *mc,
|
|
|
|
int candidate )
|
2001-05-12 08:51:28 +08:00
|
|
|
{
|
2006-10-29 00:20:59 +08:00
|
|
|
metasingleconn_t *msc = &mc->mc_conns[ candidate ];
|
|
|
|
|
2006-12-18 07:52:23 +08:00
|
|
|
if ( msc->msc_ld != NULL ) {
|
2006-10-29 00:20:59 +08:00
|
|
|
|
2006-11-05 20:05:47 +08:00
|
|
|
#ifdef DEBUG_205
|
|
|
|
char buf[ BUFSIZ ];
|
|
|
|
|
|
|
|
snprintf( buf, sizeof( buf ), "meta_clear_one_candidate ldap_unbind_ext[%d] mc=%p ld=%p",
|
|
|
|
candidate, (void *)mc, (void *)msc->msc_ld );
|
|
|
|
Debug( LDAP_DEBUG_ANY, "### %s %s\n",
|
|
|
|
op ? op->o_log_prefix : "", buf, 0 );
|
|
|
|
#endif /* DEBUG_205 */
|
2006-10-29 00:20:59 +08:00
|
|
|
|
2005-11-02 00:36:17 +08:00
|
|
|
ldap_unbind_ext( msc->msc_ld, NULL, NULL );
|
2005-04-23 05:43:52 +08:00
|
|
|
msc->msc_ld = NULL;
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
2005-04-23 05:43:52 +08:00
|
|
|
if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
|
2005-11-02 00:36:17 +08:00
|
|
|
ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
|
2005-04-23 05:43:52 +08:00
|
|
|
BER_BVZERO( &msc->msc_bound_ndn );
|
2001-05-12 08:51:28 +08:00
|
|
|
}
|
|
|
|
|
2005-04-23 05:43:52 +08:00
|
|
|
if ( !BER_BVISNULL( &msc->msc_cred ) ) {
|
2005-09-25 02:39:26 +08:00
|
|
|
memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
|
2005-11-02 00:36:17 +08:00
|
|
|
ber_memfree_x( msc->msc_cred.bv_val, NULL );
|
2005-04-23 05:43:52 +08:00
|
|
|
BER_BVZERO( &msc->msc_cred );
|
2004-03-06 23:01:16 +08:00
|
|
|
}
|
|
|
|
|
2006-12-18 07:52:23 +08:00
|
|
|
msc->msc_mscflags = 0;
|
|
|
|
|
2001-05-12 08:51:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|