2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
27 #include <ac/socket.h>
28 #include <ac/string.h>
33 #include "../back-ldap/back-ldap.h"
34 #include "back-meta.h"
37 * Set PRINT_CONNTREE larger than 0 to dump the connection tree (debug only)
39 #define PRINT_CONNTREE 0
44 * compares two struct metaconn based on the value of the conn pointer;
53 struct metaconn *lc1 = ( struct metaconn * )c1;
54 struct metaconn *lc2 = ( struct metaconn * )c2;
56 return SLAP_PTRCMP( lc1->mc_conn, lc2->mc_conn );
62 * returns -1 in case a duplicate struct metaconn has been inserted;
71 struct metaconn *lc1 = ( struct metaconn * )c1;
72 struct metaconn *lc2 = ( struct metaconn * )c2;
74 return( ( lc1->mc_conn == lc2->mc_conn ) ? -1 : 0 );
78 * Debug stuff (got it from libavl)
80 #if PRINT_CONNTREE > 0
82 ravl_print( Avlnode *root, int depth )
90 ravl_print( root->avl_right, depth + 1 );
92 for ( i = 0; i < depth; i++ ) {
96 printf( "c(%d) %d\n", ( ( struct metaconn * )root->avl_data )->mc_conn->c_connid, root->avl_bf );
98 ravl_print( root->avl_left, depth + 1 );
102 myprint( Avlnode *root )
104 printf( "********\n" );
107 printf( "\tNULL\n" );
109 ravl_print( root, 0 );
112 printf( "********\n" );
114 #endif /* PRINT_CONNTREE */
122 * Allocates a connection structure, making room for all the referenced targets
124 static struct metaconn *
125 metaconn_alloc( int ntargets )
129 assert( ntargets > 0 );
131 lc = ch_calloc( sizeof( struct metaconn ), 1 );
137 * make it a null-terminated array ...
139 lc->mc_conns = ch_calloc( sizeof( struct metasingleconn ), ntargets+1 );
140 if ( lc->mc_conns == NULL ) {
144 lc->mc_conns[ ntargets ].msc_candidate = META_LAST_CONN;
146 for ( ; ntargets-- > 0; ) {
147 lc->mc_conns[ ntargets ].msc_ld = NULL;
148 BER_BVZERO( &lc->mc_conns[ ntargets ].msc_bound_ndn );
149 BER_BVZERO( &lc->mc_conns[ ntargets ].msc_cred );
150 lc->mc_conns[ ntargets ].msc_bound = META_UNBOUND;
153 lc->mc_bound_target = META_BOUND_NONE;
172 if ( lc->mc_conns ) {
173 ch_free( lc->mc_conns );
182 * Initializes one connection
188 struct metatarget *lt,
189 struct metasingleconn *lsc,
190 ldap_back_send_t sendok )
192 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
199 if ( lsc->msc_ld != NULL ) {
204 * Attempts to initialize the connection to the target ds
206 rs->sr_err = ldap_initialize( &lsc->msc_ld, lt->mt_uri );
207 if ( rs->sr_err != LDAP_SUCCESS ) {
212 * Set LDAP version. This will always succeed: If the client
213 * bound with a particular version, then so can we.
215 vers = op->o_conn->c_protocol;
216 ldap_set_option( lsc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
218 /* automatically chase referrals ("chase-referrals"/"dont-chase-referrals" statement) */
219 if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
220 ldap_set_option( lsc->msc_ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
223 /* start TLS ("start-tls"/"try-start-tls" statements) */
224 if ( ( LDAP_BACK_USE_TLS( li ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( li ) ) )
225 && !ldap_is_ldaps_url( lt->mt_uri ) )
233 rc = ldap_start_tls( lsc->msc_ld, NULL, NULL, &msgid );
234 if ( rc == LDAP_SUCCESS ) {
235 struct timeval tv = { 0, 0 };
237 rc = ldap_result( lsc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
239 rs->sr_err = LDAP_OTHER;
241 } else if ( rc == 0 ) {
248 rs->sr_err = LDAP_OTHER;
251 if ( rc == LDAP_RES_EXTENDED ) {
252 rc = ldap_parse_result( lsc->msc_ld, res,
253 &rs->sr_err, NULL, NULL, NULL, NULL, 1 );
254 if ( rc != LDAP_SUCCESS ) {
257 /* FIXME: in case a referral
258 * is returned, should we try
259 * using it instead of the
261 } else if ( rs->sr_err == LDAP_REFERRAL ) {
262 rs->sr_err = LDAP_OTHER;
263 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
268 rs->sr_err = LDAP_OTHER;
273 rs->sr_err = ldap_start_tls_s( lsc->msc_ld, NULL, NULL );
276 /* if StartTLS is requested, only attempt it if the URL
277 * is not "ldaps://"; this may occur not only in case
278 * of misconfiguration, but also when used in the chain
279 * overlay, where the "uri" can be parsed out of a referral */
280 if ( rs->sr_err == LDAP_SERVER_DOWN
281 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( li ) ) )
283 ldap_unbind_ext_s( lsc->msc_ld, NULL, NULL );
289 * Set the network timeout if set
291 if (li->network_timeout != 0){
292 struct timeval network_timeout;
294 network_timeout.tv_usec = 0;
295 network_timeout.tv_sec = li->network_timeout;
297 ldap_set_option( lsc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
298 (void *)&network_timeout );
302 * Sets a cookie for the rewrite session
304 ( void )rewrite_session_init( lt->mt_rwmap.rwm_rw, op->o_conn );
307 * If the connection DN is not null, an attempt to rewrite it is made
309 if ( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
310 dc.rwmap = <->mt_rwmap;
311 dc.conn = op->o_conn;
316 * Rewrite the bind dn if needed
318 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
319 &lsc->msc_bound_ndn ) )
324 /* copy the DN idf needed */
325 if ( lsc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
326 ber_dupbv( &lsc->msc_bound_ndn, &op->o_conn->c_dn );
329 assert( !BER_BVISNULL( &lsc->msc_bound_ndn ) );
332 ber_str2bv( "", 0, 1, &lsc->msc_bound_ndn );
335 lsc->msc_bound = META_UNBOUND;
338 if ( rs->sr_err != LDAP_SUCCESS ) {
339 rs->sr_err = slap_map_api2result( rs );
340 if ( sendok & LDAP_BACK_SENDERR ) {
341 send_ldap_result( op, rs );
348 * The candidate is activated
350 lsc->msc_candidate = META_CANDIDATE;
359 * Prepares the connection structure
361 * FIXME: This function needs to receive some info on the type of operation
362 * it is invoked by, so that only the correct pool of candidate targets
363 * is initialized in case no connection was available yet.
365 * At present a flag that says whether the candidate target must be unique
366 * is passed; eventually an operation agent will be used.
375 ldap_back_send_t sendok )
377 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
378 struct metaconn *lc, lc_curr;
379 int cached = META_TARGET_NONE,
380 i = META_TARGET_NONE,
384 /* Searches for a metaconn in the avl tree */
385 lc_curr.mc_conn = op->o_conn;
386 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
387 lc = (struct metaconn *)avl_find( li->conntree,
388 (caddr_t)&lc_curr, meta_back_conn_cmp );
389 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
391 /* Looks like we didn't get a bind. Open a new session... */
393 lc = metaconn_alloc( li->ntargets );
394 lc->mc_conn = op->o_conn;
399 * require all connections ...
401 if ( op_type == META_OP_REQUIRE_ALL ) {
402 for ( i = 0; i < li->ntargets; i++ ) {
405 * The target is activated; if needed, it is
408 int lerr = init_one_conn( op, rs, li->targets[ i ],
409 &lc->mc_conns[ i ], sendok );
410 if ( lerr != LDAP_SUCCESS ) {
413 * FIXME: in case one target cannot
414 * be init'd, should the other ones
417 ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
426 * looks in cache, if any
428 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
429 cached = i = meta_dncache_get_target( &li->cache, ndn );
432 if ( op_type == META_OP_REQUIRE_SINGLE ) {
435 * tries to get a unique candidate
436 * (takes care of default target
438 if ( i == META_TARGET_NONE ) {
439 i = meta_back_select_unique_candidate( li, ndn );
443 * if any is found, inits the connection
445 if ( i == META_TARGET_NONE ) {
450 rs->sr_err = LDAP_NO_SUCH_OBJECT;
454 Debug( LDAP_DEBUG_CACHE,
455 "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n",
459 * Clear all other candidates
461 ( void )meta_clear_unused_candidates( li, lc, i, 0 );
464 * The target is activated; if needed, it is
465 * also init'd. In case of error, init_one_conn
466 * sends the appropriate result.
468 err = init_one_conn( op, rs, li->targets[ i ],
469 &lc->mc_conns[ i ], sendok );
470 if ( err != LDAP_SUCCESS ) {
473 * FIXME: in case one target cannot
474 * be init'd, should the other ones
477 ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
489 * if no unique candidate ...
492 for ( i = 0; i < li->ntargets; i++ ) {
494 || meta_back_is_candidate( &li->targets[ i ]->mt_nsuffix, ndn ) )
498 * The target is activated; if needed, it is
501 int lerr = init_one_conn( op, rs,
503 &lc->mc_conns[ i ], sendok );
504 if ( lerr != LDAP_SUCCESS ) {
507 * FIXME: in case one target cannot
508 * be init'd, should the other ones
511 ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
520 /* clear out init_one_conn non-fatal errors */
521 rs->sr_err = LDAP_SUCCESS;
527 * Inserts the newly created metaconn in the avl tree
529 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
530 err = avl_insert( &li->conntree, ( caddr_t )lc,
531 meta_back_conn_cmp, meta_back_conn_dup );
533 #if PRINT_CONNTREE > 0
534 myprint( li->conntree );
535 #endif /* PRINT_CONNTREE */
537 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
539 Debug( LDAP_DEBUG_TRACE,
540 "=>meta_back_getconn: conn %ld inserted\n",
541 lc->mc_conn->c_connid, 0, 0 );
544 * Err could be -1 in case a duplicate metaconn is inserted
547 rs->sr_err = LDAP_OTHER;
548 rs->sr_text = "Internal server error";
554 Debug( LDAP_DEBUG_TRACE,
555 "=>meta_back_getconn: conn %ld fetched\n",
556 lc->mc_conn->c_connid, 0, 0 );