-/*
- * Copyright 1998-2001 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
- *
- * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
- *
- * This work has been developed to fulfill the requirements
- * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
- * to the OpenLDAP Foundation in the hope that it may be useful
- * to the Open Source community, but WITHOUT ANY WARRANTY.
- *
- * Permission is granted to anyone to use this software for any purpose
- * on any computer system, and to alter it and redistribute it, subject
- * to the following restrictions:
- *
- * 1. The author and SysNet s.n.c. are not responsible for the consequences
- * of use of this software, no matter how awful, even if they arise from
- * flaws in it.
- *
- * 2. The origin of this software must not be misrepresented, either by
- * explicit claim or by omission. Since few users ever read sources,
- * credits should appear in the documentation.
- *
- * 3. Altered versions must be plainly marked as such, and must not be
- * misrepresented as being the original software. Since few users
- * ever read sources, credits should appear in the documentation.
- * SysNet s.n.c. cannot be responsible for the consequences of the
- * alterations.
- *
- * 4. This notice may not be removed or altered.
- *
- *
- * This software is based on the backend back-ldap, implemented
- * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
- * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
- * contributors. The contribution of the original software to the present
- * implementation is acknowledged in this copyright statement.
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
- * A special acknowledgement goes to Howard for the overall architecture
- * (and for borrowing large pieces of code), and to Mark, who implemented
- * from scratch the attribute/objectclass mapping.
+ * Copyright 1999-2005 The OpenLDAP Foundation.
+ * Portions Copyright 2001-2003 Pierangelo Masarati.
+ * Portions Copyright 1999-2003 Howard Chu.
+ * All rights reserved.
*
- * The original copyright statement follows.
- *
- * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
- *
- * Permission is granted to anyone to use this software for any purpose
- * on any computer system, and to alter it and redistribute it, subject
- * to the following restrictions:
- *
- * 1. The author is not responsible for the consequences of use of this
- * software, no matter how awful, even if they arise from flaws in it.
- *
- * 2. The origin of this software must not be misrepresented, either by
- * explicit claim or by omission. Since few users ever read sources,
- * credits should appear in the documentation.
- *
- * 3. Altered versions must be plainly marked as such, and must not be
- * misrepresented as being the original software. Since few users
- * ever read sources, credits should appear in the
- * documentation.
- *
- * 4. This notice may not be removed or altered.
+ * 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.
*/
#include "portable.h"
struct metaconn *lc1 = ( struct metaconn * )c1;
struct metaconn *lc2 = ( struct metaconn * )c2;
- return ( ( lc1->conn < lc2->conn ) ? -1 :
- ( ( lc1->conn > lc2-> conn ) ? 1 : 0 ) );
+ return SLAP_PTRCMP( lc1->mc_conn, lc2->mc_conn );
}
/*
struct metaconn *lc1 = ( struct metaconn * )c1;
struct metaconn *lc2 = ( struct metaconn * )c2;
- return( ( lc1->conn == lc2->conn ) ? -1 : 0 );
+ return( ( lc1->mc_conn == lc2->mc_conn ) ? -1 : 0 );
}
/*
return;
}
- ravl_print( root->avl_right, depth+1 );
+ ravl_print( root->avl_right, depth + 1 );
for ( i = 0; i < depth; i++ ) {
printf( " " );
}
- printf( "c(%d) %d\n", ( ( struct metaconn * )root->avl_data )->conn->c_connid, root->avl_bf );
+ printf( "c(%d) %d\n", ( ( struct metaconn * )root->avl_data )->mc_conn->c_connid, root->avl_bf );
- ravl_print( root->avl_left, depth+1 );
+ ravl_print( root->avl_left, depth + 1 );
}
static void
metaconn_alloc( int ntargets )
{
struct metaconn *lc;
- int i;
assert( ntargets > 0 );
/*
* make it a null-terminated array ...
*/
- lc->conns = ch_calloc( sizeof( struct metasingleconn * ), ntargets+1 );
- if ( lc->conns == NULL ) {
+ lc->mc_conns = ch_calloc( sizeof( struct metasingleconn ), ntargets+1 );
+ if ( lc->mc_conns == NULL ) {
free( lc );
return NULL;
}
+ lc->mc_conns[ ntargets ].msc_candidate = META_LAST_CONN;
- for ( i = 0; i < ntargets; i++ ) {
- lc->conns[ i ] =
- ch_calloc( sizeof( struct metasingleconn ), 1 );
- if ( lc->conns[ i ] == NULL ) {
- charray_free( ( char ** )lc->conns );
- free( lc->conns );
- free( lc );
- return NULL;
- }
+ for ( ; ntargets-- > 0; ) {
+ lc->mc_conns[ ntargets ].msc_ld = NULL;
+ BER_BVZERO( &lc->mc_conns[ ntargets ].msc_bound_ndn );
+ BER_BVZERO( &lc->mc_conns[ ntargets ].msc_cred );
+ lc->mc_conns[ ntargets ].msc_bound = META_UNBOUND;
}
- lc->bound_target = -1;
+ lc->mc_bound_target = META_BOUND_NONE;
return lc;
}
return;
}
- if ( lc->conns ) {
- int i;
-
- for ( i = 0; lc->conns[ i ] != NULL; ++i ) {
- free( lc->conns[ i ] );
- }
- charray_free( ( char ** )lc->conns );
+ if ( lc->mc_conns ) {
+ ch_free( lc->mc_conns );
}
free( lc );
*/
static int
init_one_conn(
- Connection *conn,
- Operation *op,
- struct metatarget *lt,
- int vers,
- struct metasingleconn *lsc
- )
+ Operation *op,
+ SlapReply *rs,
+ struct metatarget *lt,
+ struct metasingleconn *lsc,
+ ldap_back_send_t sendok )
{
- int err;
+ struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
+ int vers;
+ dncookie dc;
/*
* Already init'ed
*/
- if ( lsc->ld != NULL ) {
+ if ( lsc->msc_ld != NULL ) {
return LDAP_SUCCESS;
}
/*
* Attempts to initialize the connection to the target ds
*/
- err = ldap_initialize( &lsc->ld, lt->uri );
+ rs->sr_err = ldap_initialize( &lsc->msc_ld, lt->mt_uri );
+ if ( rs->sr_err != LDAP_SUCCESS ) {
+ goto error_return;
+ }
/*
- * In case of failure, the error is mapped back from client
- * to server error code
+ * Set LDAP version. This will always succeed: If the client
+ * bound with a particular version, then so can we.
*/
- if ( err != LDAP_SUCCESS ) {
- return ldap_back_map_result( err );
+ vers = op->o_conn->c_protocol;
+ ldap_set_option( lsc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
+
+ /* automatically chase referrals ("chase-referrals"/"dont-chase-referrals" statement) */
+ if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
+ ldap_set_option( lsc->msc_ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
}
-
+
+#ifdef HAVE_TLS
+ /* start TLS ("start-tls"/"try-start-tls" statements) */
+ if ( ( LDAP_BACK_USE_TLS( li ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( li ) ) )
+ && !ldap_is_ldaps_url( lt->mt_uri ) )
+ {
+#if 1
+ /*
+ * use asynchronous StartTLS
+ * in case, chase referral (not implemented yet)
+ */
+ int msgid;
+
+ rs->sr_err = ldap_start_tls( lsc->msc_ld, NULL, NULL, &msgid );
+ if ( rs->sr_err == LDAP_SUCCESS ) {
+ LDAPMessage *res = NULL;
+ int rc, retries = 1;
+ struct timeval tv = { 0, 0 };
+
+retry:;
+ rc = ldap_result( lsc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
+ if ( rc < 0 ) {
+ rs->sr_err = LDAP_OTHER;
+
+ } else if ( rc == 0 ) {
+ if ( retries ) {
+ retries--;
+ tv.tv_sec = 0;
+ tv.tv_usec = 100000;
+ goto retry;
+ }
+ rs->sr_err = LDAP_OTHER;
+
+ } else if ( rc == LDAP_RES_EXTENDED ) {
+ struct berval *data = NULL;
+
+ rs->sr_err = ldap_parse_extended_result( lsc->msc_ld, res,
+ NULL, &data, 0 );
+ if ( rs->sr_err == LDAP_SUCCESS ) {
+ rs->sr_err = ldap_result2error( lsc->msc_ld, res, 1 );
+ res = NULL;
+
+ /* FIXME: in case a referral
+ * is returned, should we try
+ * using it instead of the
+ * configured URI? */
+ if ( rs->sr_err == LDAP_SUCCESS ) {
+ ldap_install_tls( lsc->msc_ld );
+
+ } else if ( rs->sr_err == LDAP_REFERRAL ) {
+ rs->sr_err = LDAP_OTHER;
+ rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
+ }
+
+ if ( data ) {
+ if ( data->bv_val ) {
+ ber_memfree( data->bv_val );
+ }
+ ber_memfree( data );
+ }
+ }
+
+ } else {
+ rs->sr_err = LDAP_OTHER;
+ }
+
+ if ( res != NULL ) {
+ ldap_msgfree( res );
+ }
+ }
+#else
+ /*
+ * use synchronous StartTLS
+ */
+ rs->sr_err = ldap_start_tls_s( lsc->msc_ld, NULL, NULL );
+#endif
+
+ /* if StartTLS is requested, only attempt it if the URL
+ * is not "ldaps://"; this may occur not only in case
+ * of misconfiguration, but also when used in the chain
+ * overlay, where the "uri" can be parsed out of a referral */
+ if ( rs->sr_err == LDAP_SERVER_DOWN
+ || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( li ) ) )
+ {
+ ldap_unbind_ext_s( lsc->msc_ld, NULL, NULL );
+ goto error_return;
+ }
+ }
+#endif /* HAVE_TLS */
+
/*
- * Set LDAP version. This will always succeed: If the client
- * bound with a particular version, then so can we.
+ * Set the network timeout if set
*/
- ldap_set_option( lsc->ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
+ if (li->network_timeout != 0){
+ struct timeval network_timeout;
+
+ network_timeout.tv_usec = 0;
+ network_timeout.tv_sec = li->network_timeout;
+
+ ldap_set_option( lsc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
+ (void *)&network_timeout );
+ }
/*
* Sets a cookie for the rewrite session
*/
- ( void )rewrite_session_init( lt->rwinfo, conn );
+ ( void )rewrite_session_init( lt->mt_rwmap.rwm_rw, op->o_conn );
/*
- * If the connection dn is not null, an attempt to rewrite it is made
+ * If the connection DN is not null, an attempt to rewrite it is made
*/
- if ( conn->c_cdn != NULL && conn->c_cdn[ 0 ] != '\0' ) {
+ if ( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
+ dc.rwmap = <->mt_rwmap;
+ dc.conn = op->o_conn;
+ dc.rs = rs;
+ dc.ctx = "bindDN";
+
/*
* Rewrite the bind dn if needed
*/
- lsc->bound_dn = NULL;
- switch ( rewrite_session( lt->rwinfo, "bindDn",
- conn->c_cdn, conn,
- &lsc->bound_dn ) ) {
- case REWRITE_REGEXEC_OK:
- if ( lsc->bound_dn == NULL ) {
- lsc->bound_dn = ch_strdup( conn->c_cdn );
- }
-#ifdef NEW_LOGGING
- LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
- "[rw] bindDn: \"%s\" -> \"%s\"\n",
- conn->c_cdn, lsc->bound_dn ));
-#else /* !NEW_LOGGING */
- Debug( LDAP_DEBUG_ARGS,
- "rw> bindDn: \"%s\" -> \"%s\"\n%s",
- conn->c_cdn, lsc->bound_dn, "" );
-#endif /* !NEW_LOGGING */
- break;
-
- case REWRITE_REGEXEC_UNWILLING:
- send_ldap_result( conn, op,
- LDAP_UNWILLING_TO_PERFORM,
- NULL, "Unwilling to perform",
- NULL, NULL );
- /* continues to the next case */
-
- case REWRITE_REGEXEC_ERR:
- return LDAP_OPERATIONS_ERROR;
+ if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
+ &lsc->msc_bound_ndn ) )
+ {
+ goto error_return;
+ }
+
+ /* copy the DN idf needed */
+ if ( lsc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
+ ber_dupbv( &lsc->msc_bound_ndn, &op->o_conn->c_dn );
}
+
+ assert( !BER_BVISNULL( &lsc->msc_bound_ndn ) );
+
} else {
- lsc->bound_dn = NULL;
+ ber_str2bv( "", 0, 1, &lsc->msc_bound_ndn );
}
- lsc->bound = META_UNBOUND;
+ lsc->msc_bound = META_UNBOUND;
- /*
- * The candidate is activated
- */
- lsc->candidate = META_CANDIDATE;
- return LDAP_SUCCESS;
+error_return:;
+ if ( rs->sr_err != LDAP_SUCCESS ) {
+ rs->sr_err = slap_map_api2result( rs );
+ if ( sendok & LDAP_BACK_SENDERR ) {
+ send_ldap_result( op, rs );
+ rs->sr_text = NULL;
+ }
+
+ } else {
+
+ /*
+ * The candidate is activated
+ */
+ lsc->msc_candidate = META_CANDIDATE;
+ }
+
+ return rs->sr_err;
}
/*
*/
struct metaconn *
meta_back_getconn(
- struct metainfo *li,
- Connection *conn,
- Operation *op,
- int op_type,
- const char *ndn,
- int *candidate
- )
+ Operation *op,
+ SlapReply *rs,
+ int op_type,
+ struct berval *ndn,
+ int *candidate,
+ ldap_back_send_t sendok )
{
- struct metaconn *lc, lc_curr;
- int vers, cached = -1, i = -1, err = LDAP_SUCCESS;
- int new_conn = 0;
+ struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
+ struct metaconn *lc, lc_curr;
+ int cached = META_TARGET_NONE,
+ i = META_TARGET_NONE,
+ err = LDAP_SUCCESS,
+ new_conn = 0;
/* Searches for a metaconn in the avl tree */
- lc_curr.conn = conn;
+ lc_curr.mc_conn = op->o_conn;
ldap_pvt_thread_mutex_lock( &li->conn_mutex );
lc = (struct metaconn *)avl_find( li->conntree,
(caddr_t)&lc_curr, meta_back_conn_cmp );
/* Looks like we didn't get a bind. Open a new session... */
if ( !lc ) {
lc = metaconn_alloc( li->ntargets );
- lc->conn = conn;
+ lc->mc_conn = op->o_conn;
new_conn = 1;
}
- vers = conn->c_protocol;
+ /*
+ * require all connections ...
+ */
+ if ( op_type == META_OP_REQUIRE_ALL ) {
+ for ( i = 0; i < li->ntargets; i++ ) {
+ /*
+ * The target is activated; if needed, it is
+ * also init'd
+ */
+ int lerr = init_one_conn( op, rs, li->targets[ i ],
+ &lc->mc_conns[ i ], sendok );
+ if ( lerr != LDAP_SUCCESS ) {
+
+ /*
+ * FIXME: in case one target cannot
+ * be init'd, should the other ones
+ * be tried?
+ */
+ ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
+ err = lerr;
+ continue;
+ }
+ }
+ goto done;
+ }
+
/*
* looks in cache, if any
*/
* tries to get a unique candidate
* (takes care of default target
*/
- if ( i < 0 ) {
+ if ( i == META_TARGET_NONE ) {
i = meta_back_select_unique_candidate( li, ndn );
}
/*
* if any is found, inits the connection
*/
- if ( i < 0 ) {
+ if ( i == META_TARGET_NONE ) {
if ( new_conn ) {
metaconn_free( lc );
}
- send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
- NULL, "", NULL, NULL );
-
+ rs->sr_err = LDAP_NO_SUCH_OBJECT;
return NULL;
}
-#ifdef NEW_LOGGING
- LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
- "meta_back_getconn: got target %d"
- " for ndn=\"%s\" from cache\n", i, ndn ));
-#else /* !NEW_LOGGING */
Debug( LDAP_DEBUG_CACHE,
- "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n%s",
- i, ndn, "" );
-#endif /* !NEW_LOGGING */
+ "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n",
+ i, ndn->bv_val, 0 );
/*
* Clear all other candidates
/*
* The target is activated; if needed, it is
- * also init'd
+ * also init'd. In case of error, init_one_conn
+ * sends the appropriate result.
*/
- err = init_one_conn( conn, op, li->targets[ i ],
- vers, lc->conns[ i ] );
+ err = init_one_conn( op, rs, li->targets[ i ],
+ &lc->mc_conns[ i ], sendok );
if ( err != LDAP_SUCCESS ) {
/*
* be init'd, should the other ones
* be tried?
*/
- ( void )meta_clear_one_candidate( lc->conns[ i ], 1 );
+ ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
if ( new_conn ) {
metaconn_free( lc );
}
-
- send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, "internal server error", NULL, NULL );
-
return NULL;
}
* if no unique candidate ...
*/
} else {
- int ndnlen = strlen( ndn );
for ( i = 0; i < li->ntargets; i++ ) {
if ( i == cached
- || meta_back_is_candidate( li->targets[ i ]->suffix,
- ndn, ndnlen ) ) {
+ || meta_back_is_candidate( &li->targets[ i ]->mt_nsuffix, ndn ) )
+ {
/*
* The target is activated; if needed, it is
* also init'd
*/
- int lerr = init_one_conn( conn, op,
+ int lerr = init_one_conn( op, rs,
li->targets[ i ],
- vers, lc->conns[ i ] );
+ &lc->mc_conns[ i ], sendok );
if ( lerr != LDAP_SUCCESS ) {
/*
* be init'd, should the other ones
* be tried?
*/
- ( void )meta_clear_one_candidate( lc->conns[ i ], 1 );
+ ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
err = lerr;
continue;
}
}
}
+done:;
+ /* clear out init_one_conn non-fatal errors */
+ rs->sr_err = LDAP_SUCCESS;
+ rs->sr_text = NULL;
+
if ( new_conn ) {
/*
ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
-#ifdef NEW_LOGGING
- LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
- "meta_back_getconn: conn %ld inserted\n",
- lc->conn->c_connid ));
-#else /* !NEW_LOGGING */
Debug( LDAP_DEBUG_TRACE,
- "=>meta_back_getconn: conn %ld inserted\n%s%s",
- lc->conn->c_connid, "", "" );
-#endif /* !NEW_LOGGING */
+ "=>meta_back_getconn: conn %ld inserted\n",
+ lc->mc_conn->c_connid, 0, 0 );
/*
* Err could be -1 in case a duplicate metaconn is inserted
*/
if ( err != 0 ) {
- send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
- NULL, "internal server error", NULL, NULL );
+ rs->sr_err = LDAP_OTHER;
+ rs->sr_text = "Internal server error";
metaconn_free( lc );
return NULL;
}
+
} else {
-#ifdef NEW_LOGGING
- LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
- "meta_back_getconn: conn %ld fetched\n",
- lc->conn->c_connid ));
-#else /* !NEW_LOGGING */
Debug( LDAP_DEBUG_TRACE,
- "=>meta_back_getconn: conn %ld fetched\n%s%s",
- lc->conn->c_connid, "", "" );
-#endif /* !NEW_LOGGING */
+ "=>meta_back_getconn: conn %ld fetched\n",
+ lc->mc_conn->c_connid, 0, 0 );
}
return lc;