2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 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->conn, lc2->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->conn == lc2->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 )->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->conns = ch_calloc( sizeof( struct metasingleconn ), ntargets+1 );
140 if ( lc->conns == NULL ) {
144 lc->conns[ ntargets ].candidate = META_LAST_CONN;
146 for ( ; ntargets-- > 0; ) {
147 lc->conns[ ntargets ].ld = NULL;
148 lc->conns[ ntargets ].bound_dn.bv_val = NULL;
149 lc->conns[ ntargets ].bound_dn.bv_len = 0;
150 lc->conns[ ntargets ].cred.bv_val = NULL;
151 lc->conns[ ntargets ].cred.bv_len = 0;
152 lc->conns[ ntargets ].bound = META_UNBOUND;
155 lc->bound_target = META_BOUND_NONE;
175 ch_free( lc->conns );
184 * Initializes one connection
190 struct metatarget *lt,
191 struct metasingleconn *lsc
194 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
201 if ( lsc->ld != NULL ) {
206 * Attempts to initialize the connection to the target ds
208 rs->sr_err = ldap_initialize( &lsc->ld, lt->uri );
209 if ( rs->sr_err != LDAP_SUCCESS ) {
210 return ldap_back_map_result( rs );
214 * Set LDAP version. This will always succeed: If the client
215 * bound with a particular version, then so can we.
217 vers = op->o_conn->c_protocol;
218 ldap_set_option( lsc->ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
219 /* FIXME: configurable? */
220 ldap_set_option(lsc->ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
223 * Set the network timeout if set
225 if (li->network_timeout != 0){
226 struct timeval network_timeout;
228 network_timeout.tv_usec = 0;
229 network_timeout.tv_sec = li->network_timeout;
231 ldap_set_option( lsc->ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &network_timeout);
235 * Sets a cookie for the rewrite session
237 ( void )rewrite_session_init( lt->rwmap.rwm_rw, op->o_conn );
240 * If the connection dn is not null, an attempt to rewrite it is made
242 if ( op->o_conn->c_dn.bv_len != 0 ) {
243 dc.rwmap = <->rwmap;
244 dc.conn = op->o_conn;
249 * Rewrite the bind dn if needed
251 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
253 send_ldap_result( op, rs );
257 assert( lsc->bound_dn.bv_val );
260 ber_str2bv( "", 0, 1, &lsc->bound_dn );
263 lsc->bound = META_UNBOUND;
266 * The candidate is activated
268 lsc->candidate = META_CANDIDATE;
275 * Prepares the connection structure
277 * FIXME: This function needs to receive some info on the type of operation
278 * it is invoked by, so that only the correct pool of candidate targets
279 * is initialized in case no connection was available yet.
281 * At present a flag that says whether the candidate target must be unique
282 * is passed; eventually an operation agent will be used.
292 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
293 struct metaconn *lc, lc_curr;
294 int cached = -1, i = -1, err = LDAP_SUCCESS;
297 /* Searches for a metaconn in the avl tree */
298 lc_curr.conn = op->o_conn;
299 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
300 lc = (struct metaconn *)avl_find( li->conntree,
301 (caddr_t)&lc_curr, meta_back_conn_cmp );
302 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
304 /* Looks like we didn't get a bind. Open a new session... */
306 lc = metaconn_alloc( li->ntargets );
307 lc->conn = op->o_conn;
312 * looks in cache, if any
314 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
315 cached = i = meta_dncache_get_target( &li->cache, ndn );
318 if ( op_type == META_OP_REQUIRE_SINGLE ) {
321 * tries to get a unique candidate
322 * (takes care of default target
325 i = meta_back_select_unique_candidate( li, ndn );
329 * if any is found, inits the connection
336 rs->sr_err = LDAP_NO_SUCH_OBJECT;
341 LDAP_LOG( BACK_META, INFO,
342 "meta_back_getconn: got target %d for ndn=\"%s\" from cache\n",
344 #else /* !NEW_LOGGING */
345 Debug( LDAP_DEBUG_CACHE,
346 "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n%s",
347 i, ndn->bv_val, "" );
348 #endif /* !NEW_LOGGING */
351 * Clear all other candidates
353 ( void )meta_clear_unused_candidates( li, lc, i, 0 );
356 * The target is activated; if needed, it is
357 * also init'd. In case of error, init_one_conn
358 * sends the appropriate result.
360 err = init_one_conn( op, rs, li->targets[ i ],
362 if ( err != LDAP_SUCCESS ) {
365 * FIXME: in case one target cannot
366 * be init'd, should the other ones
369 ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
381 * require all connections ...
383 } else if (op_type == META_OP_REQUIRE_ALL) {
384 for ( i = 0; i < li->ntargets; i++ ) {
387 * The target is activated; if needed, it is
390 int lerr = init_one_conn( op, rs, li->targets[ i ],
392 if ( lerr != LDAP_SUCCESS ) {
395 * FIXME: in case one target cannot
396 * be init'd, should the other ones
399 ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
406 * if no unique candidate ...
409 for ( i = 0; i < li->ntargets; i++ ) {
411 || meta_back_is_candidate( &li->targets[ i ]->suffix, ndn ) ) {
414 * The target is activated; if needed, it is
417 int lerr = init_one_conn( op, rs,
420 if ( lerr != LDAP_SUCCESS ) {
423 * FIXME: in case one target cannot
424 * be init'd, should the other ones
427 ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
435 /* clear out init_one_conn non-fatal errors */
436 rs->sr_err = LDAP_SUCCESS;
442 * Inserts the newly created metaconn in the avl tree
444 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
445 err = avl_insert( &li->conntree, ( caddr_t )lc,
446 meta_back_conn_cmp, meta_back_conn_dup );
448 #if PRINT_CONNTREE > 0
449 myprint( li->conntree );
450 #endif /* PRINT_CONNTREE */
452 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
455 LDAP_LOG( BACK_META, INFO,
456 "meta_back_getconn: conn %ld inserted\n", lc->conn->c_connid, 0, 0);
457 #else /* !NEW_LOGGING */
458 Debug( LDAP_DEBUG_TRACE,
459 "=>meta_back_getconn: conn %ld inserted\n%s%s",
460 lc->conn->c_connid, "", "" );
461 #endif /* !NEW_LOGGING */
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 LDAP_LOG( BACK_META, INFO,
475 "meta_back_getconn: conn %ld fetched\n", lc->conn->c_connid, 0, 0 );
476 #else /* !NEW_LOGGING */
477 Debug( LDAP_DEBUG_TRACE,
478 "=>meta_back_getconn: conn %ld fetched\n%s%s",
479 lc->conn->c_connid, "", "" );
480 #endif /* !NEW_LOGGING */