2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2003 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by the Howard Chu for inclusion
17 * in OpenLDAP Software and subsequently enhanced by Pierangelo
20 /* This is an altered version */
22 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
24 * This work has been developed to fulfill the requirements
25 * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
26 * to the OpenLDAP Foundation in the hope that it may be useful
27 * to the Open Source community, but WITHOUT ANY WARRANTY.
29 * Permission is granted to anyone to use this software for any purpose
30 * on any computer system, and to alter it and redistribute it, subject
31 * to the following restrictions:
33 * 1. The author and SysNet s.n.c. are not responsible for the consequences
34 * of use of this software, no matter how awful, even if they arise from
37 * 2. The origin of this software must not be misrepresented, either by
38 * explicit claim or by omission. Since few users ever read sources,
39 * credits should appear in the documentation.
41 * 3. Altered versions must be plainly marked as such, and must not be
42 * misrepresented as being the original software. Since few users
43 * ever read sources, credits should appear in the documentation.
44 * SysNet s.n.c. cannot be responsible for the consequences of the
47 * 4. This notice may not be removed or altered.
50 * This software is based on the backend back-ldap, implemented
51 * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
52 * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
53 * contributors. The contribution of the original software to the present
54 * implementation is acknowledged in this copyright statement.
56 * A special acknowledgement goes to Howard for the overall architecture
57 * (and for borrowing large pieces of code), and to Mark, who implemented
58 * from scratch the attribute/objectclass mapping.
60 * The original copyright statement follows.
62 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
64 * Permission is granted to anyone to use this software for any purpose
65 * on any computer system, and to alter it and redistribute it, subject
66 * to the following restrictions:
68 * 1. The author is not responsible for the consequences of use of this
69 * software, no matter how awful, even if they arise from flaws in it.
71 * 2. The origin of this software must not be misrepresented, either by
72 * explicit claim or by omission. Since few users ever read sources,
73 * credits should appear in the documentation.
75 * 3. Altered versions must be plainly marked as such, and must not be
76 * misrepresented as being the original software. Since few users
77 * ever read sources, credits should appear in the
80 * 4. This notice may not be removed or altered.
88 #include <ac/socket.h>
89 #include <ac/string.h>
94 #include "../back-ldap/back-ldap.h"
95 #include "back-meta.h"
98 * Set PRINT_CONNTREE larger than 0 to dump the connection tree (debug only)
100 #define PRINT_CONNTREE 0
105 * compares two struct metaconn based on the value of the conn pointer;
114 struct metaconn *lc1 = ( struct metaconn * )c1;
115 struct metaconn *lc2 = ( struct metaconn * )c2;
117 return SLAP_PTRCMP( lc1->conn, lc2->conn );
123 * returns -1 in case a duplicate struct metaconn has been inserted;
132 struct metaconn *lc1 = ( struct metaconn * )c1;
133 struct metaconn *lc2 = ( struct metaconn * )c2;
135 return( ( lc1->conn == lc2->conn ) ? -1 : 0 );
139 * Debug stuff (got it from libavl)
141 #if PRINT_CONNTREE > 0
143 ravl_print( Avlnode *root, int depth )
151 ravl_print( root->avl_right, depth+1 );
153 for ( i = 0; i < depth; i++ ) {
157 printf( "c(%d) %d\n", ( ( struct metaconn * )root->avl_data )->conn->c_connid, root->avl_bf );
159 ravl_print( root->avl_left, depth+1 );
163 myprint( Avlnode *root )
165 printf( "********\n" );
168 printf( "\tNULL\n" );
170 ravl_print( root, 0 );
173 printf( "********\n" );
175 #endif /* PRINT_CONNTREE */
183 * Allocates a connection structure, making room for all the referenced targets
185 static struct metaconn *
186 metaconn_alloc( int ntargets )
190 assert( ntargets > 0 );
192 lc = ch_calloc( sizeof( struct metaconn ), 1 );
198 * make it a null-terminated array ...
200 lc->conns = ch_calloc( sizeof( struct metasingleconn ), ntargets+1 );
201 if ( lc->conns == NULL ) {
205 lc->conns[ ntargets ].candidate = META_LAST_CONN;
207 for ( ; ntargets-- > 0; ) {
208 lc->conns[ ntargets ].ld = NULL;
209 lc->conns[ ntargets ].bound_dn.bv_val = NULL;
210 lc->conns[ ntargets ].bound_dn.bv_len = 0;
211 lc->conns[ ntargets ].cred.bv_val = NULL;
212 lc->conns[ ntargets ].cred.bv_len = 0;
213 lc->conns[ ntargets ].bound = META_UNBOUND;
216 lc->bound_target = META_BOUND_NONE;
236 ch_free( lc->conns );
245 * Initializes one connection
251 struct metatarget *lt,
252 struct metasingleconn *lsc
255 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
262 if ( lsc->ld != NULL ) {
267 * Attempts to initialize the connection to the target ds
269 rs->sr_err = ldap_initialize( &lsc->ld, lt->uri );
270 if ( rs->sr_err != LDAP_SUCCESS ) {
271 return ldap_back_map_result( rs );
275 * Set LDAP version. This will always succeed: If the client
276 * bound with a particular version, then so can we.
278 vers = op->o_conn->c_protocol;
279 ldap_set_option( lsc->ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
280 /* FIXME: configurable? */
281 ldap_set_option(lsc->ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
284 * Set the network timeout if set
286 if (li->network_timeout != 0){
287 struct timeval network_timeout;
289 network_timeout.tv_usec = 0;
290 network_timeout.tv_sec = li->network_timeout;
292 ldap_set_option( lsc->ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &network_timeout);
296 * Sets a cookie for the rewrite session
298 ( void )rewrite_session_init( lt->rwmap.rwm_rw, op->o_conn );
301 * If the connection dn is not null, an attempt to rewrite it is made
303 if ( op->o_conn->c_dn.bv_len != 0 ) {
304 dc.rwmap = <->rwmap;
305 dc.conn = op->o_conn;
310 * Rewrite the bind dn if needed
312 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
314 send_ldap_result( op, rs );
318 assert( lsc->bound_dn.bv_val );
321 ber_str2bv( "", 0, 1, &lsc->bound_dn );
324 lsc->bound = META_UNBOUND;
327 * The candidate is activated
329 lsc->candidate = META_CANDIDATE;
336 * Prepares the connection structure
338 * FIXME: This function needs to receive some info on the type of operation
339 * it is invoked by, so that only the correct pool of candidate targets
340 * is initialized in case no connection was available yet.
342 * At present a flag that says whether the candidate target must be unique
343 * is passed; eventually an operation agent will be used.
353 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
354 struct metaconn *lc, lc_curr;
355 int cached = -1, i = -1, err = LDAP_SUCCESS;
358 /* Searches for a metaconn in the avl tree */
359 lc_curr.conn = op->o_conn;
360 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
361 lc = (struct metaconn *)avl_find( li->conntree,
362 (caddr_t)&lc_curr, meta_back_conn_cmp );
363 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
365 /* Looks like we didn't get a bind. Open a new session... */
367 lc = metaconn_alloc( li->ntargets );
368 lc->conn = op->o_conn;
373 * looks in cache, if any
375 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
376 cached = i = meta_dncache_get_target( &li->cache, ndn );
379 if ( op_type == META_OP_REQUIRE_SINGLE ) {
382 * tries to get a unique candidate
383 * (takes care of default target
386 i = meta_back_select_unique_candidate( li, ndn );
390 * if any is found, inits the connection
397 rs->sr_err = LDAP_NO_SUCH_OBJECT;
402 LDAP_LOG( BACK_META, INFO,
403 "meta_back_getconn: got target %d for ndn=\"%s\" from cache\n",
405 #else /* !NEW_LOGGING */
406 Debug( LDAP_DEBUG_CACHE,
407 "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n%s",
408 i, ndn->bv_val, "" );
409 #endif /* !NEW_LOGGING */
412 * Clear all other candidates
414 ( void )meta_clear_unused_candidates( li, lc, i, 0 );
417 * The target is activated; if needed, it is
418 * also init'd. In case of error, init_one_conn
419 * sends the appropriate result.
421 err = init_one_conn( op, rs, li->targets[ i ],
423 if ( err != LDAP_SUCCESS ) {
426 * FIXME: in case one target cannot
427 * be init'd, should the other ones
430 ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
442 * require all connections ...
444 } else if (op_type == META_OP_REQUIRE_ALL) {
445 for ( i = 0; i < li->ntargets; i++ ) {
448 * The target is activated; if needed, it is
451 int lerr = init_one_conn( op, rs, li->targets[ i ],
453 if ( lerr != LDAP_SUCCESS ) {
456 * FIXME: in case one target cannot
457 * be init'd, should the other ones
460 ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
467 * if no unique candidate ...
470 for ( i = 0; i < li->ntargets; i++ ) {
472 || meta_back_is_candidate( &li->targets[ i ]->suffix, ndn ) ) {
475 * The target is activated; if needed, it is
478 int lerr = init_one_conn( op, rs,
481 if ( lerr != LDAP_SUCCESS ) {
484 * FIXME: in case one target cannot
485 * be init'd, should the other ones
488 ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
496 /* clear out init_one_conn non-fatal errors */
497 rs->sr_err = LDAP_SUCCESS;
503 * Inserts the newly created metaconn in the avl tree
505 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
506 err = avl_insert( &li->conntree, ( caddr_t )lc,
507 meta_back_conn_cmp, meta_back_conn_dup );
509 #if PRINT_CONNTREE > 0
510 myprint( li->conntree );
511 #endif /* PRINT_CONNTREE */
513 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
516 LDAP_LOG( BACK_META, INFO,
517 "meta_back_getconn: conn %ld inserted\n", lc->conn->c_connid, 0, 0);
518 #else /* !NEW_LOGGING */
519 Debug( LDAP_DEBUG_TRACE,
520 "=>meta_back_getconn: conn %ld inserted\n%s%s",
521 lc->conn->c_connid, "", "" );
522 #endif /* !NEW_LOGGING */
525 * Err could be -1 in case a duplicate metaconn is inserted
528 rs->sr_err = LDAP_OTHER;
529 rs->sr_text = "Internal server error";
535 LDAP_LOG( BACK_META, INFO,
536 "meta_back_getconn: conn %ld fetched\n", lc->conn->c_connid, 0, 0 );
537 #else /* !NEW_LOGGING */
538 Debug( LDAP_DEBUG_TRACE,
539 "=>meta_back_getconn: conn %ld fetched\n%s%s",
540 lc->conn->c_connid, "", "" );
541 #endif /* !NEW_LOGGING */