2 * Copyright 1998-2001 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
7 * This work has been developed to fulfill the requirements
8 * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
9 * to the OpenLDAP Foundation in the hope that it may be useful
10 * to the Open Source community, but WITHOUT ANY WARRANTY.
12 * Permission is granted to anyone to use this software for any purpose
13 * on any computer system, and to alter it and redistribute it, subject
14 * to the following restrictions:
16 * 1. The author and SysNet s.n.c. are not responsible for the consequences
17 * of use of this software, no matter how awful, even if they arise from
20 * 2. The origin of this software must not be misrepresented, either by
21 * explicit claim or by omission. Since few users ever read sources,
22 * credits should appear in the documentation.
24 * 3. Altered versions must be plainly marked as such, and must not be
25 * misrepresented as being the original software. Since few users
26 * ever read sources, credits should appear in the documentation.
27 * SysNet s.n.c. cannot be responsible for the consequences of the
30 * 4. This notice may not be removed or altered.
33 * This software is based on the backend back-ldap, implemented
34 * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
35 * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
36 * contributors. The contribution of the original software to the present
37 * implementation is acknowledged in this copyright statement.
39 * A special acknowledgement goes to Howard for the overall architecture
40 * (and for borrowing large pieces of code), and to Mark, who implemented
41 * from scratch the attribute/objectclass mapping.
43 * The original copyright statement follows.
45 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
47 * Permission is granted to anyone to use this software for any purpose
48 * on any computer system, and to alter it and redistribute it, subject
49 * to the following restrictions:
51 * 1. The author is not responsible for the consequences of use of this
52 * software, no matter how awful, even if they arise from flaws in it.
54 * 2. The origin of this software must not be misrepresented, either by
55 * explicit claim or by omission. Since few users ever read sources,
56 * credits should appear in the documentation.
58 * 3. Altered versions must be plainly marked as such, and must not be
59 * misrepresented as being the original software. Since few users
60 * ever read sources, credits should appear in the
63 * 4. This notice may not be removed or altered.
71 #include <ac/socket.h>
72 #include <ac/string.h>
77 #include "../back-ldap/back-ldap.h"
78 #include "back-meta.h"
81 * Set PRINT_CONNTREE larger than 0 to dump the connection tree (debug only)
83 #define PRINT_CONNTREE 0
88 * compares two struct metaconn based on the value of the conn pointer;
97 struct metaconn *lc1 = ( struct metaconn * )c1;
98 struct metaconn *lc2 = ( struct metaconn * )c2;
100 return ( ( lc1->conn < lc2->conn ) ? -1 :
101 ( ( lc1->conn > lc2-> conn ) ? 1 : 0 ) );
107 * returns -1 in case a duplicate struct metaconn has been inserted;
116 struct metaconn *lc1 = ( struct metaconn * )c1;
117 struct metaconn *lc2 = ( struct metaconn * )c2;
119 return( ( lc1->conn == lc2->conn ) ? -1 : 0 );
123 * Debug stuff (got it from libavl)
125 #if PRINT_CONNTREE > 0
127 ravl_print( Avlnode *root, int depth )
135 ravl_print( root->avl_right, depth+1 );
137 for ( i = 0; i < depth; i++ ) {
141 printf( "c(%d) %d\n", ( ( struct metaconn * )root->avl_data )->conn->c_connid, root->avl_bf );
143 ravl_print( root->avl_left, depth+1 );
147 myprint( Avlnode *root )
149 printf( "********\n" );
152 printf( "\tNULL\n" );
154 ravl_print( root, 0 );
157 printf( "********\n" );
159 #endif /* PRINT_CONNTREE */
167 * Allocates a connection structure, making room for all the referenced targets
169 static struct metaconn *
170 metaconn_alloc( int ntargets )
175 assert( ntargets > 0 );
177 lc = ch_calloc( sizeof( struct metaconn ), 1 );
183 * make it a null-terminated array ...
185 lc->conns = ch_calloc( sizeof( struct metasingleconn * ), ntargets+1 );
186 if ( lc->conns == NULL ) {
191 for ( i = 0; i < ntargets; i++ ) {
193 ch_calloc( sizeof( struct metasingleconn ), 1 );
194 if ( lc->conns[ i ] == NULL ) {
195 charray_free( ( char ** )lc->conns );
202 lc->bound_target = META_BOUND_NONE;
224 for ( i = 0; lc->conns[ i ] != NULL; ++i ) {
225 free( lc->conns[ i ] );
227 charray_free( ( char ** )lc->conns );
236 * Initializes one connection
242 struct metatarget *lt,
244 struct metasingleconn *lsc
252 if ( lsc->ld != NULL ) {
257 * Attempts to initialize the connection to the target ds
259 err = ldap_initialize( &lsc->ld, lt->uri );
260 if ( err != LDAP_SUCCESS ) {
261 return ldap_back_map_result( err );
265 * Set LDAP version. This will always succeed: If the client
266 * bound with a particular version, then so can we.
268 ldap_set_option( lsc->ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
271 * Sets a cookie for the rewrite session
273 ( void )rewrite_session_init( lt->rwinfo, conn );
276 * If the connection dn is not null, an attempt to rewrite it is made
278 if ( conn->c_cdn != 0 ) {
282 * Rewrite the bind dn if needed
284 lsc->bound_dn = NULL;
285 switch ( rewrite_session( lt->rwinfo, "bindDn",
286 conn->c_cdn, conn, &mdn ) ) {
287 case REWRITE_REGEXEC_OK:
289 lsc->bound_dn = ber_bvstrdup( conn->c_cdn );
292 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
293 "[rw] bindDn: \"%s\" -> \"%s\"\n",
294 conn->c_cdn, lsc->bound_dn->bv_val ));
295 #else /* !NEW_LOGGING */
296 Debug( LDAP_DEBUG_ARGS,
297 "rw> bindDn: \"%s\" -> \"%s\"\n",
298 conn->c_cdn, lsc->bound_dn->bv_val, 0 );
299 #endif /* !NEW_LOGGING */
302 case REWRITE_REGEXEC_UNWILLING:
303 send_ldap_result( conn, op,
304 LDAP_UNWILLING_TO_PERFORM,
305 NULL, "Unwilling to perform",
307 return LDAP_UNWILLING_TO_PERFORM;
309 case REWRITE_REGEXEC_ERR:
310 send_ldap_result( conn, op,
311 LDAP_OPERATIONS_ERROR,
312 NULL, "Operations error",
314 return LDAP_OPERATIONS_ERROR;
318 lsc->bound_dn = ber_bvstr( mdn );
320 lsc->bound_dn = ber_bvstrdup( "" );
324 lsc->bound_dn = ber_bvstrdup( "" );
327 lsc->bound = META_UNBOUND;
330 * The candidate is activated
332 lsc->candidate = META_CANDIDATE;
339 * Prepares the connection structure
341 * FIXME: This function needs to receive some info on the type of operation
342 * it is invoked by, so that only the correct pool of candidate targets
343 * is initialized in case no connection was available yet.
345 * At present a flag that says whether the candidate target must be unique
346 * is passed; eventually an operation agent will be used.
357 struct metaconn *lc, lc_curr;
358 int vers, cached = -1, i = -1, err = LDAP_SUCCESS;
361 /* Searches for a metaconn in the avl tree */
363 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
364 lc = (struct metaconn *)avl_find( li->conntree,
365 (caddr_t)&lc_curr, meta_back_conn_cmp );
366 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
368 /* Looks like we didn't get a bind. Open a new session... */
370 lc = metaconn_alloc( li->ntargets );
375 vers = conn->c_protocol;
378 * looks in cache, if any
380 if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
381 cached = i = meta_dncache_get_target( &li->cache, ndn );
384 if ( op_type == META_OP_REQUIRE_SINGLE ) {
387 * tries to get a unique candidate
388 * (takes care of default target
391 i = meta_back_select_unique_candidate( li, ndn );
395 * if any is found, inits the connection
402 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
403 NULL, "", NULL, NULL );
409 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
410 "meta_back_getconn: got target %d"
411 " for ndn=\"%s\" from cache\n",
413 #else /* !NEW_LOGGING */
414 Debug( LDAP_DEBUG_CACHE,
415 "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n%s",
416 i, ndn->bv_val, "" );
417 #endif /* !NEW_LOGGING */
420 * Clear all other candidates
422 ( void )meta_clear_unused_candidates( li, lc, i, 0 );
425 * The target is activated; if needed, it is
426 * also init'd. In case of error, init_one_conn
427 * sends the appropriate result.
429 err = init_one_conn( conn, op, li->targets[ i ],
430 vers, lc->conns[ i ] );
431 if ( err != LDAP_SUCCESS ) {
434 * FIXME: in case one target cannot
435 * be init'd, should the other ones
438 ( void )meta_clear_one_candidate( lc->conns[ i ], 1 );
450 * require all connections ...
452 } else if (op_type == META_OP_REQUIRE_ALL) {
453 for ( i = 0; i < li->ntargets; i++ ) {
456 * The target is activated; if needed, it is
459 int lerr = init_one_conn( conn, op, li->targets[ i ],
460 vers, lc->conns[ i ] );
461 if ( lerr != LDAP_SUCCESS ) {
464 * FIXME: in case one target cannot
465 * be init'd, should the other ones
468 ( void )meta_clear_one_candidate( lc->conns[ i ], 1 );
475 * if no unique candidate ...
478 for ( i = 0; i < li->ntargets; i++ ) {
480 || meta_back_is_candidate( &li->targets[ i ]->suffix, ndn ) ) {
483 * The target is activated; if needed, it is
486 int lerr = init_one_conn( conn, op,
488 vers, lc->conns[ i ] );
489 if ( lerr != LDAP_SUCCESS ) {
492 * FIXME: in case one target cannot
493 * be init'd, should the other ones
496 ( void )meta_clear_one_candidate( lc->conns[ i ], 1 );
507 * Inserts the newly created metaconn in the avl tree
509 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
510 err = avl_insert( &li->conntree, ( caddr_t )lc,
511 meta_back_conn_cmp, meta_back_conn_dup );
513 #if PRINT_CONNTREE > 0
514 myprint( li->conntree );
515 #endif /* PRINT_CONNTREE */
517 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
520 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
521 "meta_back_getconn: conn %ld inserted\n",
522 lc->conn->c_connid ));
523 #else /* !NEW_LOGGING */
524 Debug( LDAP_DEBUG_TRACE,
525 "=>meta_back_getconn: conn %ld inserted\n%s%s",
526 lc->conn->c_connid, "", "" );
527 #endif /* !NEW_LOGGING */
530 * Err could be -1 in case a duplicate metaconn is inserted
533 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
534 NULL, "Internal server error", NULL, NULL );
540 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
541 "meta_back_getconn: conn %ld fetched\n",
542 lc->conn->c_connid ));
543 #else /* !NEW_LOGGING */
544 Debug( LDAP_DEBUG_TRACE,
545 "=>meta_back_getconn: conn %ld fetched\n%s%s",
546 lc->conn->c_connid, "", "" );
547 #endif /* !NEW_LOGGING */