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 /* copy the DN idf needed */
258 if ( lsc->bound_dn.bv_val == op->o_conn->c_dn.bv_val ) {
259 ber_dupbv( &lsc->bound_dn, &op->o_conn->c_dn );
262 assert( lsc->bound_dn.bv_val );
265 ber_str2bv( "", 0, 1, &lsc->bound_dn );
268 lsc->bound = META_UNBOUND;
271 * The candidate is activated
273 lsc->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 = -1, i = -1, err = LDAP_SUCCESS;
302 /* Searches for a metaconn in the avl tree */
303 lc_curr.conn = op->o_conn;
304 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
305 lc = (struct metaconn *)avl_find( li->conntree,
306 (caddr_t)&lc_curr, meta_back_conn_cmp );
307 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
309 /* Looks like we didn't get a bind. Open a new session... */
311 lc = metaconn_alloc( li->ntargets );
312 lc->conn = op->o_conn;
317 * looks in cache, if any
319 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
320 cached = i = meta_dncache_get_target( &li->cache, ndn );
323 if ( op_type == META_OP_REQUIRE_SINGLE ) {
326 * tries to get a unique candidate
327 * (takes care of default target
330 i = meta_back_select_unique_candidate( li, ndn );
334 * if any is found, inits the connection
341 rs->sr_err = LDAP_NO_SUCH_OBJECT;
346 LDAP_LOG( BACK_META, INFO,
347 "meta_back_getconn: got target %d for ndn=\"%s\" from cache\n",
349 #else /* !NEW_LOGGING */
350 Debug( LDAP_DEBUG_CACHE,
351 "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n%s",
352 i, ndn->bv_val, "" );
353 #endif /* !NEW_LOGGING */
356 * Clear all other candidates
358 ( void )meta_clear_unused_candidates( li, lc, i, 0 );
361 * The target is activated; if needed, it is
362 * also init'd. In case of error, init_one_conn
363 * sends the appropriate result.
365 err = init_one_conn( op, rs, li->targets[ i ],
367 if ( err != LDAP_SUCCESS ) {
370 * FIXME: in case one target cannot
371 * be init'd, should the other ones
374 ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
386 * require all connections ...
388 } else if (op_type == META_OP_REQUIRE_ALL) {
389 for ( i = 0; i < li->ntargets; i++ ) {
392 * The target is activated; if needed, it is
395 int lerr = init_one_conn( op, rs, li->targets[ i ],
397 if ( lerr != LDAP_SUCCESS ) {
400 * FIXME: in case one target cannot
401 * be init'd, should the other ones
404 ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
411 * if no unique candidate ...
414 for ( i = 0; i < li->ntargets; i++ ) {
416 || meta_back_is_candidate( &li->targets[ i ]->suffix, ndn ) ) {
419 * The target is activated; if needed, it is
422 int lerr = init_one_conn( op, rs,
425 if ( lerr != LDAP_SUCCESS ) {
428 * FIXME: in case one target cannot
429 * be init'd, should the other ones
432 ( void )meta_clear_one_candidate( &lc->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 );
460 LDAP_LOG( BACK_META, INFO,
461 "meta_back_getconn: conn %ld inserted\n", lc->conn->c_connid, 0, 0);
462 #else /* !NEW_LOGGING */
463 Debug( LDAP_DEBUG_TRACE,
464 "=>meta_back_getconn: conn %ld inserted\n%s%s",
465 lc->conn->c_connid, "", "" );
466 #endif /* !NEW_LOGGING */
469 * Err could be -1 in case a duplicate metaconn is inserted
472 rs->sr_err = LDAP_OTHER;
473 rs->sr_text = "Internal server error";
479 LDAP_LOG( BACK_META, INFO,
480 "meta_back_getconn: conn %ld fetched\n", lc->conn->c_connid, 0, 0 );
481 #else /* !NEW_LOGGING */
482 Debug( LDAP_DEBUG_TRACE,
483 "=>meta_back_getconn: conn %ld fetched\n%s%s",
484 lc->conn->c_connid, "", "" );
485 #endif /* !NEW_LOGGING */