#include "portable.h"
#include <stdio.h>
#include "ac/string.h"
#include "slap.h"
#include "../back-ldap/back-ldap.h"
#include "back-meta.h"
int
meta_back_is_candidate(
struct berval *nsuffix,
int suffixscope,
BerVarray subtree_exclude,
struct berval *ndn,
int scope )
{
if ( dnIsSuffix( ndn, nsuffix ) ) {
if ( subtree_exclude ) {
int i;
for ( i = 0; !BER_BVISNULL( &subtree_exclude[ i ] ); i++ ) {
if ( dnIsSuffix( ndn, &subtree_exclude[ i ] ) ) {
return META_NOT_CANDIDATE;
}
}
}
switch ( suffixscope ) {
case LDAP_SCOPE_SUBTREE:
default:
return META_CANDIDATE;
case LDAP_SCOPE_SUBORDINATE:
if ( ndn->bv_len > nsuffix->bv_len ) {
return META_CANDIDATE;
}
break;
case LDAP_SCOPE_ONELEVEL:
if ( ndn->bv_len > nsuffix->bv_len ) {
struct berval rdn = *ndn;
rdn.bv_len -= nsuffix->bv_len
+ STRLENOF( "," );
if ( dnIsOneLevelRDN( &rdn ) ) {
return META_CANDIDATE;
}
}
break;
case LDAP_SCOPE_BASE:
if ( ndn->bv_len == nsuffix->bv_len ) {
return META_CANDIDATE;
}
break;
}
return META_NOT_CANDIDATE;
}
if ( scope == LDAP_SCOPE_SUBTREE && dnIsSuffix( nsuffix, ndn ) ) {
return META_CANDIDATE;
}
return META_NOT_CANDIDATE;
}
int
meta_back_select_unique_candidate(
metainfo_t *mi,
struct berval *ndn )
{
int i, candidate = META_TARGET_NONE;
for ( i = 0; i < mi->mi_ntargets; ++i ) {
if ( meta_back_is_candidate( &mi->mi_targets[ i ].mt_nsuffix,
mi->mi_targets[ i ].mt_scope,
mi->mi_targets[ i ].mt_subtree_exclude,
ndn, LDAP_SCOPE_BASE ) )
{
if ( candidate == META_TARGET_NONE ) {
candidate = i;
} else {
return META_TARGET_MULTIPLE;
}
}
}
return candidate;
}
int
meta_clear_unused_candidates(
Operation *op,
int candidate )
{
metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
int i;
SlapReply *candidates = meta_back_candidates_get( op );
for ( i = 0; i < mi->mi_ntargets; ++i ) {
if ( i == candidate ) {
continue;
}
candidates[ i ].sr_tag = META_NOT_CANDIDATE;
}
return 0;
}
int
meta_clear_one_candidate(
metasingleconn_t *msc )
{
if ( msc->msc_ld ) {
ldap_unbind_ext( msc->msc_ld, NULL, NULL );
msc->msc_ld = NULL;
}
if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
BER_BVZERO( &msc->msc_bound_ndn );
}
if ( !BER_BVISNULL( &msc->msc_cred ) ) {
memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
ber_memfree_x( msc->msc_cred.bv_val, NULL );
BER_BVZERO( &msc->msc_cred );
}
return 0;
}
int
meta_clear_candidates( Operation *op, metaconn_t *mc )
{
metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
int c;
for ( c = 0; c < mi->mi_ntargets; c++ ) {
if ( mc->mc_conns[ c ].msc_ld != NULL ) {
meta_clear_one_candidate( &mc->mc_conns[ c ] );
}
}
return 0;
}