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
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 ) {
208 return slap_map_api2result( rs );
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 );
217 /* FIXME: configurable? */
218 ldap_set_option( lsc->msc_ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
221 * Set the network timeout if set
223 if (li->network_timeout != 0){
224 struct timeval network_timeout;
226 network_timeout.tv_usec = 0;
227 network_timeout.tv_sec = li->network_timeout;
229 ldap_set_option( lsc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
230 (void *)&network_timeout );
234 * Sets a cookie for the rewrite session
236 ( void )rewrite_session_init( lt->mt_rwmap.rwm_rw, op->o_conn );
239 * If the connection DN is not null, an attempt to rewrite it is made
241 if ( op->o_conn->c_dn.bv_len != 0 ) {
242 dc.rwmap = <->mt_rwmap;
243 dc.conn = op->o_conn;
248 * Rewrite the bind dn if needed
250 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
251 &lsc->msc_bound_ndn ) )
253 send_ldap_result( op, rs );
257 /* copy the DN idf needed */
258 if ( lsc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
259 ber_dupbv( &lsc->msc_bound_ndn, &op->o_conn->c_dn );
262 assert( lsc->msc_bound_ndn.bv_val );
265 ber_str2bv( "", 0, 1, &lsc->msc_bound_ndn );
268 lsc->msc_bound = META_UNBOUND;
271 * The candidate is activated
273 lsc->msc_candidate = META_CANDIDATE;
280 * Prepares the connection structure
282 * FIXME: This function needs to receive some info on the type of operation
283 * it is invoked by, so that only the correct pool of candidate targets
284 * is initialized in case no connection was available yet.
286 * At present a flag that says whether the candidate target must be unique
287 * is passed; eventually an operation agent will be used.
297 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
298 struct metaconn *lc, lc_curr;
299 int cached = META_TARGET_NONE,
300 i = META_TARGET_NONE,
304 /* Searches for a metaconn in the avl tree */
305 lc_curr.mc_conn = op->o_conn;
306 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
307 lc = (struct metaconn *)avl_find( li->conntree,
308 (caddr_t)&lc_curr, meta_back_conn_cmp );
309 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
311 /* Looks like we didn't get a bind. Open a new session... */
313 lc = metaconn_alloc( li->ntargets );
314 lc->mc_conn = op->o_conn;
319 * require all connections ...
321 if ( op_type == META_OP_REQUIRE_ALL ) {
322 for ( i = 0; i < li->ntargets; i++ ) {
325 * The target is activated; if needed, it is
328 int lerr = init_one_conn( op, rs, li->targets[ i ],
329 &lc->mc_conns[ i ] );
330 if ( lerr != LDAP_SUCCESS ) {
333 * FIXME: in case one target cannot
334 * be init'd, should the other ones
337 ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
346 * looks in cache, if any
348 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
349 cached = i = meta_dncache_get_target( &li->cache, ndn );
352 if ( op_type == META_OP_REQUIRE_SINGLE ) {
355 * tries to get a unique candidate
356 * (takes care of default target
358 if ( i == META_TARGET_NONE ) {
359 i = meta_back_select_unique_candidate( li, ndn );
363 * if any is found, inits the connection
365 if ( i == META_TARGET_NONE ) {
370 rs->sr_err = LDAP_NO_SUCH_OBJECT;
374 Debug( LDAP_DEBUG_CACHE,
375 "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n",
379 * Clear all other candidates
381 ( void )meta_clear_unused_candidates( li, lc, i, 0 );
384 * The target is activated; if needed, it is
385 * also init'd. In case of error, init_one_conn
386 * sends the appropriate result.
388 err = init_one_conn( op, rs, li->targets[ i ],
389 &lc->mc_conns[ i ] );
390 if ( err != LDAP_SUCCESS ) {
393 * FIXME: in case one target cannot
394 * be init'd, should the other ones
397 ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
409 * if no unique candidate ...
412 for ( i = 0; i < li->ntargets; i++ ) {
414 || meta_back_is_candidate( &li->targets[ i ]->mt_nsuffix, ndn ) )
418 * The target is activated; if needed, it is
421 int lerr = init_one_conn( op, rs,
423 &lc->mc_conns[ i ] );
424 if ( lerr != LDAP_SUCCESS ) {
427 * FIXME: in case one target cannot
428 * be init'd, should the other ones
431 ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
440 /* clear out init_one_conn non-fatal errors */
441 rs->sr_err = LDAP_SUCCESS;
447 * Inserts the newly created metaconn in the avl tree
449 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
450 err = avl_insert( &li->conntree, ( caddr_t )lc,
451 meta_back_conn_cmp, meta_back_conn_dup );
453 #if PRINT_CONNTREE > 0
454 myprint( li->conntree );
455 #endif /* PRINT_CONNTREE */
457 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
459 Debug( LDAP_DEBUG_TRACE,
460 "=>meta_back_getconn: conn %ld inserted\n",
461 lc->mc_conn->c_connid, 0, 0 );
464 * Err could be -1 in case a duplicate metaconn is inserted
467 rs->sr_err = LDAP_OTHER;
468 rs->sr_text = "Internal server error";
474 Debug( LDAP_DEBUG_TRACE,
475 "=>meta_back_getconn: conn %ld fetched\n",
476 lc->mc_conn->c_connid, 0, 0 );