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 );
224 /* start TLS ("start-tls"/"try-start-tls" statements) */
225 if ( ( LDAP_BACK_USE_TLS( li ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( li ) ) )
226 && !ldap_is_ldaps_url( lt->mt_uri ) )
230 * use asynchronous StartTLS
231 * in case, chase referral (not implemented yet)
235 rs->sr_err = ldap_start_tls( lsc->msc_ld, NULL, NULL, &msgid );
236 if ( rs->sr_err == LDAP_SUCCESS ) {
237 LDAPMessage *res = NULL;
239 struct timeval tv = { 0, 0 };
242 rc = ldap_result( lsc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
244 rs->sr_err = LDAP_OTHER;
246 } else if ( rc == 0 ) {
253 rs->sr_err = LDAP_OTHER;
255 } else if ( rc == LDAP_RES_EXTENDED ) {
256 struct berval *data = NULL;
258 rs->sr_err = ldap_parse_extended_result( lsc->msc_ld, res,
260 if ( rs->sr_err == LDAP_SUCCESS ) {
261 rs->sr_err = ldap_result2error( lsc->msc_ld, res, 1 );
264 /* FIXME: in case a referral
265 * is returned, should we try
266 * using it instead of the
268 if ( rs->sr_err == LDAP_SUCCESS ) {
269 ldap_install_tls( lsc->msc_ld );
271 } else if ( rs->sr_err == LDAP_REFERRAL ) {
272 rs->sr_err = LDAP_OTHER;
273 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
277 if ( data->bv_val ) {
278 ber_memfree( data->bv_val );
285 rs->sr_err = LDAP_OTHER;
294 * use synchronous StartTLS
296 rs->sr_err = ldap_start_tls_s( lsc->msc_ld, NULL, NULL );
299 /* if StartTLS is requested, only attempt it if the URL
300 * is not "ldaps://"; this may occur not only in case
301 * of misconfiguration, but also when used in the chain
302 * overlay, where the "uri" can be parsed out of a referral */
303 if ( rs->sr_err == LDAP_SERVER_DOWN
304 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( li ) ) )
306 ldap_unbind_ext_s( lsc->msc_ld, NULL, NULL );
310 #endif /* HAVE_TLS */
313 * Set the network timeout if set
315 if (li->network_timeout != 0){
316 struct timeval network_timeout;
318 network_timeout.tv_usec = 0;
319 network_timeout.tv_sec = li->network_timeout;
321 ldap_set_option( lsc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
322 (void *)&network_timeout );
326 * Sets a cookie for the rewrite session
328 ( void )rewrite_session_init( lt->mt_rwmap.rwm_rw, op->o_conn );
331 * If the connection DN is not null, an attempt to rewrite it is made
333 if ( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
334 dc.rwmap = <->mt_rwmap;
335 dc.conn = op->o_conn;
340 * Rewrite the bind dn if needed
342 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
343 &lsc->msc_bound_ndn ) )
348 /* copy the DN idf needed */
349 if ( lsc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
350 ber_dupbv( &lsc->msc_bound_ndn, &op->o_conn->c_dn );
353 assert( !BER_BVISNULL( &lsc->msc_bound_ndn ) );
356 ber_str2bv( "", 0, 1, &lsc->msc_bound_ndn );
359 lsc->msc_bound = META_UNBOUND;
362 if ( rs->sr_err != LDAP_SUCCESS ) {
363 rs->sr_err = slap_map_api2result( rs );
364 if ( sendok & LDAP_BACK_SENDERR ) {
365 send_ldap_result( op, rs );
372 * The candidate is activated
374 lsc->msc_candidate = META_CANDIDATE;
383 * Prepares the connection structure
385 * FIXME: This function needs to receive some info on the type of operation
386 * it is invoked by, so that only the correct pool of candidate targets
387 * is initialized in case no connection was available yet.
389 * At present a flag that says whether the candidate target must be unique
390 * is passed; eventually an operation agent will be used.
399 ldap_back_send_t sendok )
401 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
402 struct metaconn *lc, lc_curr;
403 int cached = META_TARGET_NONE,
404 i = META_TARGET_NONE,
408 /* Searches for a metaconn in the avl tree */
409 lc_curr.mc_conn = op->o_conn;
410 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
411 lc = (struct metaconn *)avl_find( li->conntree,
412 (caddr_t)&lc_curr, meta_back_conn_cmp );
413 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
415 /* Looks like we didn't get a bind. Open a new session... */
417 lc = metaconn_alloc( li->ntargets );
418 lc->mc_conn = op->o_conn;
423 * require all connections ...
425 if ( op_type == META_OP_REQUIRE_ALL ) {
426 for ( i = 0; i < li->ntargets; i++ ) {
429 * The target is activated; if needed, it is
432 int lerr = init_one_conn( op, rs, li->targets[ i ],
433 &lc->mc_conns[ i ], sendok );
434 if ( lerr != LDAP_SUCCESS ) {
437 * FIXME: in case one target cannot
438 * be init'd, should the other ones
441 ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
450 * looks in cache, if any
452 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
453 cached = i = meta_dncache_get_target( &li->cache, ndn );
456 if ( op_type == META_OP_REQUIRE_SINGLE ) {
459 * tries to get a unique candidate
460 * (takes care of default target
462 if ( i == META_TARGET_NONE ) {
463 i = meta_back_select_unique_candidate( li, ndn );
467 * if any is found, inits the connection
469 if ( i == META_TARGET_NONE ) {
474 rs->sr_err = LDAP_NO_SUCH_OBJECT;
478 Debug( LDAP_DEBUG_CACHE,
479 "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n",
483 * Clear all other candidates
485 ( void )meta_clear_unused_candidates( li, lc, i, 0 );
488 * The target is activated; if needed, it is
489 * also init'd. In case of error, init_one_conn
490 * sends the appropriate result.
492 err = init_one_conn( op, rs, li->targets[ i ],
493 &lc->mc_conns[ i ], sendok );
494 if ( err != LDAP_SUCCESS ) {
497 * FIXME: in case one target cannot
498 * be init'd, should the other ones
501 ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
513 * if no unique candidate ...
516 for ( i = 0; i < li->ntargets; i++ ) {
518 || meta_back_is_candidate( &li->targets[ i ]->mt_nsuffix, ndn ) )
522 * The target is activated; if needed, it is
525 int lerr = init_one_conn( op, rs,
527 &lc->mc_conns[ i ], sendok );
528 if ( lerr != LDAP_SUCCESS ) {
531 * FIXME: in case one target cannot
532 * be init'd, should the other ones
535 ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
544 /* clear out init_one_conn non-fatal errors */
545 rs->sr_err = LDAP_SUCCESS;
551 * Inserts the newly created metaconn in the avl tree
553 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
554 err = avl_insert( &li->conntree, ( caddr_t )lc,
555 meta_back_conn_cmp, meta_back_conn_dup );
557 #if PRINT_CONNTREE > 0
558 myprint( li->conntree );
559 #endif /* PRINT_CONNTREE */
561 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
563 Debug( LDAP_DEBUG_TRACE,
564 "=>meta_back_getconn: conn %ld inserted\n",
565 lc->mc_conn->c_connid, 0, 0 );
568 * Err could be -1 in case a duplicate metaconn is inserted
571 rs->sr_err = LDAP_OTHER;
572 rs->sr_text = "Internal server error";
578 Debug( LDAP_DEBUG_TRACE,
579 "=>meta_back_getconn: conn %ld fetched\n",
580 lc->mc_conn->c_connid, 0, 0 );