]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/conn.c
invalidate idle connection if a candidate target does not respond for the duration...
[openldap] / servers / slapd / back-meta / conn.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2006 The OpenLDAP Foundation.
5  * Portions Copyright 2001-2003 Pierangelo Masarati.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
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>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by the Howard Chu for inclusion
19  * in OpenLDAP Software and subsequently enhanced by Pierangelo
20  * Masarati.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/errno.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30
31
32 #define AVL_INTERNAL
33 #include "slap.h"
34 #include "../back-ldap/back-ldap.h"
35 #include "back-meta.h"
36
37 /*
38  * Set PRINT_CONNTREE larger than 0 to dump the connection tree (debug only)
39  */
40 #ifndef PRINT_CONNTREE
41 #define PRINT_CONNTREE 0
42 #endif /* !PRINT_CONNTREE */
43
44 /*
45  * meta_back_conndn_cmp
46  *
47  * compares two struct metaconn based on the value of the conn pointer
48  * and of the local DN; used by avl stuff
49  */
50 int
51 meta_back_conndn_cmp(
52         const void *c1,
53         const void *c2 )
54 {
55         metaconn_t      *mc1 = ( metaconn_t * )c1;
56         metaconn_t      *mc2 = ( metaconn_t * )c2;
57         int             rc;
58         
59         /* If local DNs don't match, it is definitely not a match */
60         /* For shared sessions, conn is NULL. Only explicitly
61          * bound sessions will have non-NULL conn.
62          */
63         rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
64         if ( rc == 0 ) {
65                 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
66         }
67
68         return rc;
69 }
70
71 /*
72  * meta_back_conndnmc_cmp
73  *
74  * compares two struct metaconn based on the value of the conn pointer,
75  * the local DN and the struct pointer; used by avl stuff
76  */
77 static int
78 meta_back_conndnmc_cmp(
79         const void *c1,
80         const void *c2 )
81 {
82         metaconn_t      *mc1 = ( metaconn_t * )c1;
83         metaconn_t      *mc2 = ( metaconn_t * )c2;
84         int             rc;
85         
86         /* If local DNs don't match, it is definitely not a match */
87         /* For shared sessions, conn is NULL. Only explicitly
88          * bound sessions will have non-NULL conn.
89          */
90         rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
91         if ( rc == 0 ) {
92                 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
93                 if ( rc == 0 ) {
94                         rc = SLAP_PTRCMP( mc1, mc2 );
95                 }
96         }
97
98         return rc;
99 }
100
101 /*
102  * meta_back_conn_cmp
103  *
104  * compares two struct metaconn based on the value of the conn pointer;
105  * used by avl stuff
106  */
107 int
108 meta_back_conn_cmp(
109         const void *c1,
110         const void *c2 )
111 {
112         metaconn_t      *mc1 = ( metaconn_t * )c1;
113         metaconn_t      *mc2 = ( metaconn_t * )c2;
114         
115         /* For shared sessions, conn is NULL. Only explicitly
116          * bound sessions will have non-NULL conn.
117          */
118         return SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
119 }
120
121 /*
122  * meta_back_conndn_dup
123  *
124  * returns -1 in case a duplicate struct metaconn has been inserted;
125  * used by avl stuff
126  */
127 int
128 meta_back_conndn_dup(
129         void *c1,
130         void *c2 )
131 {
132         metaconn_t      *mc1 = ( metaconn_t * )c1;
133         metaconn_t      *mc2 = ( metaconn_t * )c2;
134
135         /* Cannot have more than one shared session with same DN */
136         if ( mc1->mc_conn == mc2->mc_conn &&
137                 dn_match( &mc1->mc_local_ndn, &mc2->mc_local_ndn ) )
138         {
139                 return -1;
140         }
141                 
142         return 0;
143 }
144
145 /*
146  * Debug stuff (got it from libavl)
147  */
148 #if PRINT_CONNTREE > 0
149 static void
150 ravl_print( Avlnode *root, int depth )
151 {
152         int             i;
153         metaconn_t      *mc;
154         
155         if ( root == 0 ) {
156                 return;
157         }
158         
159         ravl_print( root->avl_right, depth + 1 );
160         
161         for ( i = 0; i < depth; i++ ) {
162                 fprintf( stderr, "-" );
163         }
164
165         mc = (metaconn_t *)root->avl_data;
166         fprintf( stderr, "mc=%p local=\"%s\" conn=%p %s refcnt=%d\n",
167                 (void *)mc,
168                 mc->mc_local_ndn.bv_val ? mc->mc_local_ndn.bv_val : "",
169                 (void *)mc->mc_conn,
170                 avl_bf2str( root->avl_bf ), mc->mc_refcnt );
171         
172         ravl_print( root->avl_left, depth + 1 );
173 }
174
175 static void
176 myprint( Avlnode *root, char *msg )
177 {
178         fprintf( stderr, "========> %s\n", msg );
179         
180         if ( root == 0 ) {
181                 fprintf( stderr, "\tNULL\n" );
182
183         } else {
184                 ravl_print( root, 0 );
185         }
186         
187         fprintf( stderr, "<======== %s\n", msg );
188 }
189 #endif /* PRINT_CONNTREE */
190 /*
191  * End of debug stuff
192  */
193
194 /*
195  * metaconn_alloc
196  * 
197  * Allocates a connection structure, making room for all the referenced targets
198  */
199 static metaconn_t *
200 metaconn_alloc(
201         Operation               *op )
202 {
203         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
204         metaconn_t      *mc;
205         int             i, ntargets = mi->mi_ntargets;
206
207         assert( ntargets > 0 );
208
209         /* malloc all in one */
210         mc = ( metaconn_t * )ch_calloc( 1, sizeof( metaconn_t )
211                         + sizeof( metasingleconn_t ) * ntargets );
212         if ( mc == NULL ) {
213                 return NULL;
214         }
215
216         for ( i = 0; i < ntargets; i++ ) {
217                 mc->mc_conns[ i ].msc_info = mi;
218         }
219
220         mc->mc_authz_target = META_BOUND_NONE;
221         mc->mc_refcnt = 1;
222
223         return mc;
224 }
225
226 /*
227  * meta_back_init_one_conn
228  * 
229  * Initializes one connection
230  */
231 int
232 meta_back_init_one_conn(
233         Operation               *op,
234         SlapReply               *rs,
235         metaconn_t              *mc,
236         int                     candidate,
237         int                     ispriv,
238         ldap_back_send_t        sendok )
239 {
240         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
241         metatarget_t            *mt = mi->mi_targets[ candidate ];
242         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
243         int                     version;
244         dncookie                dc;
245         int                     isauthz = ( candidate == mc->mc_authz_target );
246 #ifdef HAVE_TLS
247         int                     is_ldaps = 0;
248 #endif /* HAVE_TLS */
249
250         /* if the server is quarantined, and
251          * - the current interval did not expire yet, or
252          * - no more retries should occur,
253          * don't return the connection */
254         if ( mt->mt_isquarantined ) {
255                 slap_retry_info_t       *ri = &mt->mt_quarantine;
256                 int                     dont_retry = 1;
257
258                 if ( mt->mt_isquarantined == LDAP_BACK_FQ_YES ) {
259                         dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
260                                 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
261                         if ( !dont_retry ) {
262                                 if ( LogTest( LDAP_DEBUG_ANY ) ) {
263                                         char    buf[ SLAP_TEXT_BUFLEN ];
264
265                                         snprintf( buf, sizeof( buf ),
266                                                 "meta_back_init_one_conn[%d]: quarantine "
267                                                 "retry block #%d try #%d",
268                                                 candidate, ri->ri_idx, ri->ri_count );
269                                         Debug( LDAP_DEBUG_ANY, "%s %s.\n",
270                                                 op->o_log_prefix, buf, 0 );
271                                 }
272
273                                 mt->mt_isquarantined = LDAP_BACK_FQ_RETRYING;
274                         }
275                 }
276
277                 if ( dont_retry ) {
278                         rs->sr_err = LDAP_UNAVAILABLE;
279                         if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
280                                 send_ldap_result( op, rs );
281                         }
282                         return rs->sr_err;
283                 }
284         }
285
286         /*
287          * Already init'ed
288          */
289         if ( msc->msc_ld != NULL ) {
290                 return rs->sr_err = LDAP_SUCCESS;
291         }
292
293         msc->msc_mscflags = 0;
294        
295         /*
296          * Attempts to initialize the connection to the target ds
297          */
298         ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
299         rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
300 #ifdef HAVE_TLS
301         is_ldaps = ldap_is_ldaps_url( mt->mt_uri );
302 #endif /* HAVE_TLS */
303         ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
304         if ( rs->sr_err != LDAP_SUCCESS ) {
305                 goto error_return;
306         }
307
308         /*
309          * Set LDAP version. This will always succeed: If the client
310          * bound with a particular version, then so can we.
311          */
312         if ( mt->mt_version != 0 ) {
313                 version = mt->mt_version;
314
315         } else if ( op->o_conn->c_protocol != 0 ) {
316                 version = op->o_conn->c_protocol;
317
318         } else {
319                 version = LDAP_VERSION3;
320         }
321         ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &version );
322         ldap_set_urllist_proc( msc->msc_ld, mt->mt_urllist_f, mt->mt_urllist_p );
323
324         /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
325         ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
326                 LDAP_BACK_CHASE_REFERRALS( mi ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
327
328 #ifdef HAVE_TLS
329         /* start TLS ("tls [try-]{start|propagate}" statement) */
330         if ( ( LDAP_BACK_USE_TLS( mi ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
331                         && !is_ldaps )
332         {
333 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
334                 /*
335                  * use asynchronous StartTLS; in case, chase referral
336                  * FIXME: OpenLDAP does not return referral on StartTLS yet
337                  */
338                 int             msgid;
339
340                 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
341                 if ( rs->sr_err == LDAP_SUCCESS ) {
342                         LDAPMessage     *res = NULL;
343                         int             rc, nretries = mt->mt_nretries;
344                         struct timeval  tv;
345
346                         LDAP_BACK_TV_SET( &tv );
347
348 retry:;
349                         rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
350                         switch ( rc ) {
351                         case -1:
352                                 rs->sr_err = LDAP_OTHER;
353                                 break;
354
355                         case 0:
356                                 if ( nretries != 0 ) {
357                                         if ( nretries > 0 ) {
358                                                 nretries--;
359                                         }
360                                         LDAP_BACK_TV_SET( &tv );
361                                         goto retry;
362                                 }
363                                 rs->sr_err = LDAP_OTHER;
364                                 break;
365
366                         default:
367                                 /* only touch when activity actually took place... */
368                                 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
369                                         msc->msc_time = op->o_time;
370                                 }
371                                 break;
372                         }
373
374                         if ( rc == LDAP_RES_EXTENDED ) {
375                                 struct berval   *data = NULL;
376
377                                 /* NOTE: right now, data is unused, so don't get it */
378                                 rs->sr_err = ldap_parse_extended_result( msc->msc_ld, res,
379                                                 NULL, NULL /* &data */ , 0 );
380                                 if ( rs->sr_err == LDAP_SUCCESS ) {
381                                         int             err;
382
383                                         /* FIXME: matched? referrals? response controls? */
384                                         rs->sr_err = ldap_parse_result( msc->msc_ld, res,
385                                                 &err, NULL, NULL, NULL, NULL, 1 );
386                                         res = NULL;
387
388                                         if ( rs->sr_err == LDAP_SUCCESS ) {
389                                                 rs->sr_err = err;
390                                         }
391                                         
392                                         /* FIXME: in case a referral 
393                                          * is returned, should we try
394                                          * using it instead of the 
395                                          * configured URI? */
396                                         if ( rs->sr_err == LDAP_SUCCESS ) {
397                                                 ldap_install_tls( msc->msc_ld );
398
399                                         } else if ( rs->sr_err == LDAP_REFERRAL ) {
400                                                 /* FIXME: LDAP_OPERATIONS_ERROR? */
401                                                 rs->sr_err = LDAP_OTHER;
402                                                 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
403                                         }
404
405                                         if ( data ) {
406                                                 if ( data->bv_val ) {
407                                                         ber_memfree( data->bv_val );
408                                                 }
409                                                 ber_memfree( data );
410                                         }
411                                 }
412
413                         } else {
414                                 rs->sr_err = LDAP_OTHER;
415                         }
416
417                         if ( res != NULL ) {
418                                 ldap_msgfree( res );
419                         }
420                 }
421 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
422                 /*
423                  * use synchronous StartTLS
424                  */
425                 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
426 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
427
428                 /* if StartTLS is requested, only attempt it if the URL
429                  * is not "ldaps://"; this may occur not only in case
430                  * of misconfiguration, but also when used in the chain 
431                  * overlay, where the "uri" can be parsed out of a referral */
432                 if ( rs->sr_err == LDAP_SERVER_DOWN
433                                 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( mi ) ) )
434                 {
435                         ldap_unbind_ext( msc->msc_ld, NULL, NULL );
436                         msc->msc_ld = NULL;
437                         goto error_return;
438                 }
439         }
440 #endif /* HAVE_TLS */
441
442         /*
443          * Set the network timeout if set
444          */
445         if ( mt->mt_network_timeout != 0 ) {
446                 struct timeval  network_timeout;
447
448                 network_timeout.tv_usec = 0;
449                 network_timeout.tv_sec = mt->mt_network_timeout;
450
451                 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
452                                 (void *)&network_timeout );
453         }
454
455         /*
456          * If the connection DN is not null, an attempt to rewrite it is made
457          */
458
459         if ( ispriv ) {
460                 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
461                         ber_bvreplace( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN );
462                         if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
463                                 ber_bvreplace( &msc->msc_cred, &mt->mt_idassert_passwd );
464                         }
465
466                 } else {
467                         ber_bvreplace( &msc->msc_bound_ndn, &slap_empty_bv );
468                 }
469
470                 LDAP_BACK_CONN_ISPRIV_SET( msc );
471
472         } else {
473                 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
474                         memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
475                         ber_memfree_x( msc->msc_cred.bv_val, NULL );
476                         BER_BVZERO( &msc->msc_cred );
477                 }
478                 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
479                         ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
480                         BER_BVZERO( &msc->msc_bound_ndn );
481                 }
482                 if ( !BER_BVISEMPTY( &op->o_ndn )
483                         && SLAP_IS_AUTHZ_BACKEND( op )
484                         && isauthz )
485                 {
486                         dc.target = mt;
487                         dc.conn = op->o_conn;
488                         dc.rs = rs;
489                         dc.ctx = "bindDN";
490                 
491                         /*
492                          * Rewrite the bind dn if needed
493                          */
494                         if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
495                                                 &msc->msc_bound_ndn ) )
496                         {
497                                 ldap_unbind_ext( msc->msc_ld, NULL, NULL );
498                                 msc->msc_ld = NULL;
499                                 goto error_return;
500                         }
501                         
502                         /* copy the DN if needed */
503                         if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
504                                 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
505                         }
506
507                 } else {
508                         ber_dupbv( &msc->msc_bound_ndn, (struct berval *)&slap_empty_bv );
509                 }
510         }
511
512         assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
513
514 error_return:;
515         if ( rs->sr_err == LDAP_SUCCESS ) {
516                 /*
517                  * Sets a cookie for the rewrite session
518                  */
519                 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
520
521         } else {
522                 rs->sr_err = slap_map_api2result( rs );
523                 if ( sendok & LDAP_BACK_SENDERR ) {
524                         send_ldap_result( op, rs );
525                         rs->sr_text = NULL;
526                 }
527         }
528
529         return rs->sr_err;
530 }
531
532 /*
533  * meta_back_retry
534  * 
535  * Retries one connection
536  */
537 int
538 meta_back_retry(
539         Operation               *op,
540         SlapReply               *rs,
541         metaconn_t              **mcp,
542         int                     candidate,
543         ldap_back_send_t        sendok )
544 {
545         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
546         metatarget_t            *mt = mi->mi_targets[ candidate ];
547         metaconn_t              *mc = *mcp;
548         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
549         int                     rc = LDAP_UNAVAILABLE,
550                                 binding = LDAP_BACK_CONN_BINDING( msc );
551
552         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
553
554         assert( mc->mc_refcnt > 0 );
555         if ( mc->mc_refcnt == 1 ) {
556                 if ( LogTest( LDAP_DEBUG_ANY ) ) {
557                         char    buf[ SLAP_TEXT_BUFLEN ];
558
559                         ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
560                         snprintf( buf, sizeof( buf ),
561                                 "retrying URI=\"%s\" DN=\"%s\"",
562                                 mt->mt_uri,
563                                 BER_BVISNULL( &msc->msc_bound_ndn ) ?
564                                         "" : msc->msc_bound_ndn.bv_val );
565                         ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
566
567                         Debug( LDAP_DEBUG_ANY,
568                                 "%s meta_back_retry[%d]: %s.\n",
569                                 op->o_log_prefix, candidate, buf );
570                 }
571
572                 meta_clear_one_candidate( msc );
573                 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
574
575                 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
576
577                 /* mc here must be the regular mc, reset and ready for init */
578                 rc = meta_back_init_one_conn( op, rs, mc, candidate,
579                         LDAP_BACK_CONN_ISPRIV( mc ), sendok );
580                 if ( binding ) {
581                         LDAP_BACK_CONN_BINDING_SET( msc );
582                 }
583
584                 if ( rc == LDAP_SUCCESS ) {
585                         rc = meta_back_single_dobind( op, rs, mcp, candidate,
586                                 sendok, mt->mt_nretries, 0 );
587
588                         Debug( LDAP_DEBUG_ANY,
589                                 "%s meta_back_retry[%d]: "
590                                 "meta_back_single_dobind=%d\n",
591                                 op->o_log_prefix, candidate, rc );
592                         if ( rc == LDAP_SUCCESS ) {
593                                 if ( be_isroot( op )  ) {
594                                         LDAP_BACK_CONN_ISBOUND_SET( msc );
595                                 } else {
596                                         LDAP_BACK_CONN_ISANON_SET( msc );
597                                 }
598                         }
599                 }
600         }
601
602         if ( rc != LDAP_SUCCESS ) {
603                 SlapReply               *candidates = meta_back_candidates_get( op );
604
605                 candidates[ candidate ].sr_err = rc;
606
607                 if ( *mcp != NULL ) {
608                         if ( binding ) {
609                                 LDAP_BACK_CONN_BINDING_CLEAR( msc );
610                         }
611                         LDAP_BACK_CONN_TAINTED_SET( mc );
612                         /* only release if mandatory; otherwise
613                          * let the caller do what's best before
614                          * releasing */
615                         if ( META_BACK_ONERR_STOP( mi ) ) {
616                                 meta_back_release_conn_lock( op, mc, 0 );
617                                 *mcp = NULL;
618                         }
619                 }
620
621                 if ( sendok ) {
622                         rs->sr_err = rc;
623                         rs->sr_text = NULL;
624                         send_ldap_result( op, rs );
625                 }
626         }
627
628         if ( META_BACK_TGT_QUARANTINE( mt ) ) {
629                 meta_back_quarantine( op, rs, candidate );
630         }
631
632         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
633
634         return rc == LDAP_SUCCESS ? 1 : 0;
635 }
636
637 /*
638  * callback for unique candidate selection
639  */
640 static int
641 meta_back_conn_cb( Operation *op, SlapReply *rs )
642 {
643         assert( op->o_tag == LDAP_REQ_SEARCH );
644
645         switch ( rs->sr_type ) {
646         case REP_SEARCH:
647                 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
648                 break;
649
650         case REP_SEARCHREF:
651         case REP_RESULT:
652                 break;
653
654         default:
655                 return rs->sr_err;
656         }
657
658         return 0;
659 }
660
661
662 static int
663 meta_back_get_candidate(
664         Operation       *op,
665         SlapReply       *rs,
666         struct berval   *ndn )
667 {
668         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
669         long            candidate;
670
671         /*
672          * tries to get a unique candidate
673          * (takes care of default target)
674          */
675         candidate = meta_back_select_unique_candidate( mi, ndn );
676
677         /*
678          * if any is found, inits the connection
679          */
680         if ( candidate == META_TARGET_NONE ) {
681                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
682                 rs->sr_text = "no suitable candidate target found";
683
684         } else if ( candidate == META_TARGET_MULTIPLE ) {
685                 Filter          f = { 0 };
686                 Operation       op2 = *op;
687                 SlapReply       rs2 = { 0 };
688                 slap_callback   cb2 = { 0 };
689                 int             rc;
690
691                 /* try to get a unique match for the request ndn
692                  * among the multiple candidates available */
693                 op2.o_tag = LDAP_REQ_SEARCH;
694                 op2.o_req_dn = *ndn;
695                 op2.o_req_ndn = *ndn;
696                 op2.ors_scope = LDAP_SCOPE_BASE;
697                 op2.ors_deref = LDAP_DEREF_NEVER;
698                 op2.ors_attrs = slap_anlist_no_attrs;
699                 op2.ors_attrsonly = 0;
700                 op2.ors_limit = NULL;
701                 op2.ors_slimit = 1;
702                 op2.ors_tlimit = SLAP_NO_LIMIT;
703
704                 f.f_choice = LDAP_FILTER_PRESENT;
705                 f.f_desc = slap_schema.si_ad_objectClass;
706                 op2.ors_filter = &f;
707                 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
708
709                 op2.o_callback = &cb2;
710                 cb2.sc_response = meta_back_conn_cb;
711                 cb2.sc_private = (void *)&candidate;
712
713                 rc = op->o_bd->be_search( &op2, &rs2 );
714
715                 switch ( rs2.sr_err ) {
716                 case LDAP_SUCCESS:
717                 default:
718                         rs->sr_err = rs2.sr_err;
719                         break;
720
721                 case LDAP_SIZELIMIT_EXCEEDED:
722                         /* if multiple candidates can serve the operation,
723                          * and a default target is defined, and it is
724                          * a candidate, try using it (FIXME: YMMV) */
725                         if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
726                                 && meta_back_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ],
727                                                 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
728                         {
729                                 candidate = mi->mi_defaulttarget;
730                                 rs->sr_err = LDAP_SUCCESS;
731                                 rs->sr_text = NULL;
732
733                         } else {
734                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
735                                 rs->sr_text = "cannot select unique candidate target";
736                         }
737                         break;
738                 }
739
740         } else {
741                 rs->sr_err = LDAP_SUCCESS;
742         }
743
744         return candidate;
745 }
746
747 static void     *meta_back_candidates_dummy;
748
749 static void
750 meta_back_candidates_keyfree(
751         void            *key,
752         void            *data )
753 {
754         metacandidates_t        *mc = (metacandidates_t *)data;
755
756         ber_memfree_x( mc->mc_candidates, NULL );
757         ber_memfree_x( data, NULL );
758 }
759
760 SlapReply *
761 meta_back_candidates_get( Operation *op )
762 {
763         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
764         metacandidates_t        *mc;
765
766         if ( op->o_threadctx ) {
767                 void            *data = NULL;
768
769                 ldap_pvt_thread_pool_getkey( op->o_threadctx,
770                                 &meta_back_candidates_dummy, &data, NULL );
771                 mc = (metacandidates_t *)data;
772
773         } else {
774                 mc = mi->mi_candidates;
775         }
776
777         if ( mc == NULL ) {
778                 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
779                 mc->mc_ntargets = mi->mi_ntargets;
780                 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
781                 if ( op->o_threadctx ) {
782                         void            *data = NULL;
783
784                         data = (void *)mc;
785                         ldap_pvt_thread_pool_setkey( op->o_threadctx,
786                                         &meta_back_candidates_dummy, data,
787                                         meta_back_candidates_keyfree );
788
789                 } else {
790                         mi->mi_candidates = mc;
791                 }
792
793         } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
794                 /* NOTE: in the future, may want to allow back-config
795                  * to add/remove targets from back-meta... */
796                 mc->mc_candidates = ch_realloc( mc->mc_candidates,
797                                 sizeof( SlapReply ) * mi->mi_ntargets );
798                 memset( &mc->mc_candidates[ mc->mc_ntargets ], 0,
799                         sizeof( SlapReply ) * ( mi->mi_ntargets - mc->mc_ntargets ) );
800                 mc->mc_ntargets = mi->mi_ntargets;
801         }
802
803         return mc->mc_candidates;
804 }
805
806 /*
807  * meta_back_getconn
808  * 
809  * Prepares the connection structure
810  * 
811  * RATIONALE:
812  *
813  * - determine what DN is being requested:
814  *
815  *      op      requires candidate      checks
816  *
817  *      add     unique                  parent of o_req_ndn
818  *      bind    unique^*[/all]          o_req_ndn [no check]
819  *      compare unique^+                o_req_ndn
820  *      delete  unique                  o_req_ndn
821  *      modify  unique                  o_req_ndn
822  *      search  any                     o_req_ndn
823  *      modrdn  unique[, unique]        o_req_ndn[, orr_nnewSup]
824  *
825  * - for ops that require the candidate to be unique, in case of multiple
826  *   occurrences an internal search with sizeLimit=1 is performed
827  *   if a unique candidate can actually be determined.  If none is found,
828  *   the operation aborts; if multiple are found, the default target
829  *   is used if defined and candidate; otherwise the operation aborts.
830  *
831  * *^note: actually, the bind operation is handled much like a search;
832  *   i.e. the bind is broadcast to all candidate targets.
833  *
834  * +^note: actually, the compare operation is handled much like a search;
835  *   i.e. the compare is broadcast to all candidate targets, while checking
836  *   that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
837  *   returned.
838  */
839 metaconn_t *
840 meta_back_getconn(
841         Operation               *op,
842         SlapReply               *rs,
843         int                     *candidate,
844         ldap_back_send_t        sendok )
845 {
846         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
847         metaconn_t      *mc = NULL,
848                         mc_curr = { 0 };
849         int             cached = META_TARGET_NONE,
850                         i = META_TARGET_NONE,
851                         err = LDAP_SUCCESS,
852                         new_conn = 0,
853                         ncandidates = 0;
854
855
856         meta_op_type    op_type = META_OP_REQUIRE_SINGLE;
857         enum            {
858                 META_DNTYPE_ENTRY,
859                 META_DNTYPE_PARENT,
860                 META_DNTYPE_NEWPARENT
861                         } dn_type = META_DNTYPE_ENTRY;
862         struct berval   ndn = op->o_req_ndn,
863                         pndn;
864
865         SlapReply       *candidates = meta_back_candidates_get( op );
866
867         /* Internal searches are privileged and shared. So is root. */
868         /* FIXME: there seem to be concurrency issues */
869         if ( op->o_do_not_cache || be_isroot( op ) ) {
870                 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
871                 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
872                 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
873
874         } else {
875                 mc_curr.mc_local_ndn = op->o_ndn;
876
877                 /* Explicit binds must not be shared */
878                 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
879                         mc_curr.mc_conn = op->o_conn;
880         
881                 } else {
882                         mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
883                 }
884         }
885
886         /* Explicit Bind requests always get their own conn */
887         if ( !( sendok & LDAP_BACK_BINDING ) ) {
888                 /* Searches for a metaconn in the avl tree */
889 retry_lock:;
890                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
891                 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree, 
892                         (caddr_t)&mc_curr, meta_back_conndn_cmp );
893                 if ( mc ) {
894                         if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
895                                 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
896                         {
897                                 /* don't let anyone else use this expired connection */
898                                 (void)avl_delete( &mi->mi_conninfo.lai_tree,
899                                         (caddr_t)mc, meta_back_conndnmc_cmp );
900                                 LDAP_BACK_CONN_TAINTED_SET( mc );
901
902                                 Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: mc=%p conn=%ld expired.\n",
903                                         op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc->mc_conn ) );
904                         }
905
906                         /* Don't reuse connections while they're still binding */
907                         if ( LDAP_BACK_CONN_BINDING( mc ) ) {
908                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
909                                 ldap_pvt_thread_yield();
910                                 goto retry_lock;
911                         }
912
913                         mc->mc_refcnt++;
914                 }
915                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
916         }
917
918         switch ( op->o_tag ) {
919         case LDAP_REQ_ADD:
920                 /* if we go to selection, the entry must not exist,
921                  * and we must be able to resolve the parent */
922                 dn_type = META_DNTYPE_PARENT;
923                 dnParent( &ndn, &pndn );
924                 break;
925
926         case LDAP_REQ_MODRDN:
927                 /* if nnewSuperior is not NULL, it must resolve
928                  * to the same candidate as the req_ndn */
929                 if ( op->orr_nnewSup ) {
930                         dn_type = META_DNTYPE_NEWPARENT;
931                 }
932                 break;
933
934         case LDAP_REQ_BIND:
935                 /* if bound as rootdn, the backend must bind to all targets
936                  * with the administrative identity */
937                 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
938                         op_type = META_OP_REQUIRE_ALL;
939                 }
940                 break;
941
942         case LDAP_REQ_DELETE:
943         case LDAP_REQ_MODIFY:
944                 /* just a unique candidate */
945                 break;
946
947         case LDAP_REQ_COMPARE:
948         case LDAP_REQ_SEARCH:
949                 /* allow multiple candidates for the searchBase */
950                 op_type = META_OP_ALLOW_MULTIPLE;
951                 break;
952
953         default:
954                 /* right now, just break (exop?) */
955                 break;
956         }
957
958         /*
959          * require all connections ...
960          */
961         if ( op_type == META_OP_REQUIRE_ALL ) {
962
963                 /* Looks like we didn't get a bind. Open a new session... */
964                 if ( mc == NULL ) {
965                         assert( new_conn == 0 );
966                         mc = metaconn_alloc( op );
967                         mc->mc_conn = mc_curr.mc_conn;
968                         ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
969                         new_conn = 1;
970                         if ( sendok & LDAP_BACK_BINDING ) {
971                                 LDAP_BACK_CONN_BINDING_SET( mc );
972                         }
973                 }
974
975                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
976                         /*
977                          * The target is activated; if needed, it is
978                          * also init'd
979                          */
980                         candidates[ i ].sr_err = meta_back_init_one_conn( op,
981                                 rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
982                                 sendok );
983                         if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
984                                 META_CANDIDATE_SET( &candidates[ i ] );
985                                 ncandidates++;
986         
987                         } else {
988                                 
989                                 /*
990                                  * FIXME: in case one target cannot
991                                  * be init'd, should the other ones
992                                  * be tried?
993                                  */
994                                 META_CANDIDATE_RESET( &candidates[ i ] );
995                                 err = candidates[ i ].sr_err;
996                                 continue;
997                         }
998                 }
999
1000                 if ( ncandidates == 0 ) {
1001                         if ( new_conn ) {
1002                                 mc->mc_refcnt = 0;
1003                                 meta_back_conn_free( mc );
1004
1005                         } else {
1006                                 meta_back_release_conn( op, mc );
1007                         }
1008
1009                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
1010                         rs->sr_text = "Unable to select valid candidates";
1011
1012                         if ( sendok & LDAP_BACK_SENDERR ) {
1013                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1014                                         rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1015                                 }
1016                                 send_ldap_result( op, rs );
1017                                 rs->sr_text = NULL;
1018                                 rs->sr_matched = NULL;
1019                         }
1020
1021                         return NULL;
1022                 }
1023
1024                 goto done;
1025         }
1026         
1027         /*
1028          * looks in cache, if any
1029          */
1030         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1031                 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
1032         }
1033
1034         if ( op_type == META_OP_REQUIRE_SINGLE ) {
1035                 metatarget_t            *mt = NULL;
1036                 metasingleconn_t        *msc = NULL;
1037
1038                 int                     j;
1039
1040                 for ( j = 0; j < mi->mi_ntargets; j++ ) {
1041                         META_CANDIDATE_RESET( &candidates[ j ] );
1042                 }
1043
1044                 /*
1045                  * tries to get a unique candidate
1046                  * (takes care of default target)
1047                  */
1048                 if ( i == META_TARGET_NONE ) {
1049                         i = meta_back_get_candidate( op, rs, &ndn );
1050
1051                         if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1052                                 i = meta_back_get_candidate( op, rs, &pndn );
1053                         }
1054         
1055                         if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1056                                 if ( mc != NULL ) {
1057                                         meta_back_release_conn( op, mc );
1058                                 }
1059
1060                                 if ( sendok & LDAP_BACK_SENDERR ) {
1061                                         if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1062                                                 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1063                                         }
1064                                         send_ldap_result( op, rs );
1065                                         rs->sr_text = NULL;
1066                                         rs->sr_matched = NULL;
1067                                 }
1068                         
1069                                 return NULL;
1070                         }
1071                 }
1072
1073                 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1074                 {
1075                         if ( mc != NULL ) {
1076                                 meta_back_release_conn( op, mc );
1077                         }
1078
1079                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1080                         rs->sr_text = "cross-target rename not supported";
1081                         if ( sendok & LDAP_BACK_SENDERR ) {
1082                                 send_ldap_result( op, rs );
1083                                 rs->sr_text = NULL;
1084                         }
1085
1086                         return NULL;
1087                 }
1088
1089                 Debug( LDAP_DEBUG_TRACE,
1090         "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1091                                 i, op->o_req_ndn.bv_val, 0 );
1092
1093                 if ( mc == NULL ) {
1094                         /* Retries searching for a metaconn in the avl tree
1095                          * the reason is that the connection might have been
1096                          * created by meta_back_get_candidate() */
1097                         if ( !( sendok & LDAP_BACK_BINDING ) ) {
1098 retry_lock2:;
1099                                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1100                                 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree, 
1101                                         (caddr_t)&mc_curr, meta_back_conndn_cmp );
1102                                 if ( mc != NULL ) {
1103                                         /* Don't reuse connections while they're still binding */
1104                                         if ( LDAP_BACK_CONN_BINDING( mc ) ) {
1105                                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1106                                                 ldap_pvt_thread_yield();
1107                                                 goto retry_lock2;
1108                                         }
1109
1110                                         mc->mc_refcnt++;
1111                                 }
1112                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1113                         }
1114
1115                         /* Looks like we didn't get a bind. Open a new session... */
1116                         if ( mc == NULL ) {
1117                                 assert( new_conn == 0 );
1118                                 mc = metaconn_alloc( op );
1119                                 mc->mc_conn = mc_curr.mc_conn;
1120                                 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1121                                 new_conn = 1;
1122                                 if ( sendok & LDAP_BACK_BINDING ) {
1123                                         LDAP_BACK_CONN_BINDING_SET( mc );
1124                                 }
1125                         }
1126                 }
1127
1128                 /*
1129                  * Clear all other candidates
1130                  */
1131                 ( void )meta_clear_unused_candidates( op, i );
1132
1133                 mt = mi->mi_targets[ i ];
1134                 msc = &mc->mc_conns[ i ];
1135
1136                 /*
1137                  * The target is activated; if needed, it is
1138                  * also init'd. In case of error, meta_back_init_one_conn
1139                  * sends the appropriate result.
1140                  */
1141                 err = meta_back_init_one_conn( op, rs, mc, i,
1142                         LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
1143                 if ( err != LDAP_SUCCESS ) {
1144                         /*
1145                          * FIXME: in case one target cannot
1146                          * be init'd, should the other ones
1147                          * be tried?
1148                          */
1149                         META_CANDIDATE_RESET( &candidates[ i ] );
1150                         if ( new_conn ) {
1151                                 mc->mc_refcnt = 0;
1152                                 meta_back_conn_free( mc );
1153
1154                         } else {
1155                                 meta_back_release_conn( op, mc );
1156                         }
1157                         return NULL;
1158                 }
1159
1160                 candidates[ i ].sr_err = LDAP_SUCCESS;
1161                 META_CANDIDATE_SET( &candidates[ i ] );
1162                 ncandidates++;
1163
1164                 if ( candidate ) {
1165                         *candidate = i;
1166                 }
1167
1168         /*
1169          * if no unique candidate ...
1170          */
1171         } else {
1172
1173                 /* Looks like we didn't get a bind. Open a new session... */
1174                 if ( mc == NULL ) {
1175                         assert( new_conn == 0 );
1176                         mc = metaconn_alloc( op );
1177                         mc->mc_conn = mc_curr.mc_conn;
1178                         ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1179                         new_conn = 1;
1180                         if ( sendok & LDAP_BACK_BINDING ) {
1181                                 LDAP_BACK_CONN_BINDING_SET( mc );
1182                         }
1183                 }
1184
1185                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1186                         metatarget_t            *mt = mi->mi_targets[ i ];
1187                         metasingleconn_t        *msc = &mc->mc_conns[ i ];
1188
1189                         if ( i == cached 
1190                                 || meta_back_is_candidate( mt, &op->o_req_ndn,
1191                                         LDAP_SCOPE_SUBTREE ) )
1192                         {
1193
1194                                 /*
1195                                  * The target is activated; if needed, it is
1196                                  * also init'd
1197                                  */
1198                                 int lerr = meta_back_init_one_conn( op, rs, mc, i,
1199                                         LDAP_BACK_CONN_ISPRIV( &mc_curr ), LDAP_BACK_DONTSEND );
1200                                 if ( lerr == LDAP_SUCCESS ) {
1201                                         META_CANDIDATE_SET( &candidates[ i ] );
1202                                         candidates[ i ].sr_err = LDAP_SUCCESS;
1203                                         ncandidates++;
1204
1205                                         Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1206                                                 op->o_log_prefix, i, 0 );
1207
1208                                 } else if ( lerr == LDAP_UNAVAILABLE && !META_BACK_ONERR_STOP( mi ) ) {
1209                                         META_CANDIDATE_SET( &candidates[ i ] );
1210                                         candidates[ i ].sr_err = LDAP_UNAVAILABLE;
1211
1212                                         Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] %s\n",
1213                                                 op->o_log_prefix, i,
1214                                                 mt->mt_isquarantined != LDAP_BACK_FQ_NO ? "quarantined" : "unavailable" );
1215
1216                                 } else {
1217                                 
1218                                         /*
1219                                          * FIXME: in case one target cannot
1220                                          * be init'd, should the other ones
1221                                          * be tried?
1222                                          */
1223                                         if ( new_conn ) {
1224                                                 ( void )meta_clear_one_candidate( msc );
1225                                         }
1226                                         /* leave the target candidate, but record the error for later use */
1227                                         candidates[ i ].sr_err = lerr;
1228                                         err = lerr;
1229
1230                                         if ( lerr == LDAP_UNAVAILABLE && mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
1231                                                 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] quarantined: %d\n",
1232                                                         op->o_log_prefix, i, lerr );
1233
1234                                         } else {
1235                                                 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed: %d\n",
1236                                                         op->o_log_prefix, i, lerr );
1237                                         }
1238
1239                                         if ( META_BACK_ONERR_STOP( mi ) ) {
1240                                                 if ( sendok & LDAP_BACK_SENDERR ) {
1241                                                         send_ldap_result( op, rs );
1242                                                         rs->sr_text = NULL;
1243                                                 }
1244                                                 if ( new_conn ) {
1245                                                         mc->mc_refcnt = 0;
1246                                                         meta_back_conn_free( mc );
1247
1248                                                 } else {
1249                                                         meta_back_release_conn( op, mc );
1250                                                 }
1251
1252                                                 return NULL;
1253                                         }
1254
1255                                         rs->sr_text = NULL;
1256                                         continue;
1257                                 }
1258
1259                         } else {
1260                                 if ( new_conn ) {
1261                                         ( void )meta_clear_one_candidate( msc );
1262                                 }
1263                                 META_CANDIDATE_RESET( &candidates[ i ] );
1264                         }
1265                 }
1266
1267                 if ( ncandidates == 0 ) {
1268                         if ( new_conn ) {
1269                                 mc->mc_refcnt = 0;
1270                                 meta_back_conn_free( mc );
1271
1272                         } else {
1273                                 meta_back_release_conn( op, mc );
1274                         }
1275
1276                         if ( rs->sr_err == LDAP_SUCCESS ) {
1277                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1278                                 rs->sr_text = "Unable to select valid candidates";
1279                         }
1280
1281                         if ( sendok & LDAP_BACK_SENDERR ) {
1282                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1283                                         rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1284                                 }
1285                                 send_ldap_result( op, rs );
1286                                 rs->sr_text = NULL;
1287                                 rs->sr_matched = NULL;
1288                         }
1289
1290                         return NULL;
1291                 }
1292         }
1293
1294 done:;
1295         /* clear out meta_back_init_one_conn non-fatal errors */
1296         rs->sr_err = LDAP_SUCCESS;
1297         rs->sr_text = NULL;
1298
1299         /* touch the timestamp */
1300         if ( mi->mi_idle_timeout != 0 ) {
1301                 mc->mc_time = op->o_time;
1302         }
1303
1304         if ( new_conn ) {
1305                 if ( mi->mi_conn_ttl ) {
1306                         mc->mc_create_time = op->o_time;
1307                 }
1308
1309                 /*
1310                  * Inserts the newly created metaconn in the avl tree
1311                  */
1312                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1313                 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1314                                 meta_back_conndn_cmp, meta_back_conndn_dup );
1315
1316 #if PRINT_CONNTREE > 0
1317                 myprint( mi->mi_conninfo.lai_tree, "meta_back_getconn" );
1318 #endif /* PRINT_CONNTREE */
1319                 
1320                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1321
1322                 /*
1323                  * Err could be -1 in case a duplicate metaconn is inserted
1324                  */
1325                 switch ( err ) {
1326                 case 0:
1327                         break;
1328
1329                 case -1:
1330                         /* duplicate: free and try to get the newly created one */
1331                         if ( !( sendok & LDAP_BACK_BINDING ) ) {
1332                                 new_conn = 0;
1333                                 goto retry_lock;
1334                         }
1335                         LDAP_BACK_CONN_TAINTED_SET( mc );
1336                         break;
1337
1338                 default:
1339                         Debug( LDAP_DEBUG_ANY,
1340                                 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1341                                 op->o_log_prefix, ncandidates,
1342                                 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1343         
1344                         mc->mc_refcnt = 0;      
1345                         meta_back_conn_free( mc );
1346
1347                         rs->sr_err = LDAP_OTHER;
1348                         rs->sr_text = "proxy bind collision";
1349                         if ( sendok & LDAP_BACK_SENDERR ) {
1350                                 send_ldap_result( op, rs );
1351                                 rs->sr_text = NULL;
1352                         }
1353                         return NULL;
1354                 }
1355
1356                 Debug( LDAP_DEBUG_TRACE,
1357                         "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1358                         op->o_log_prefix, ncandidates,
1359                         LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1360
1361         } else {
1362                 Debug( LDAP_DEBUG_TRACE,
1363                         "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1364                         op->o_log_prefix, ncandidates,
1365                         LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1366         }
1367         
1368         return mc;
1369 }
1370
1371 void
1372 meta_back_release_conn_lock(
1373         Operation               *op,
1374         metaconn_t              *mc,
1375         int                     dolock )
1376 {
1377         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
1378
1379         assert( mc != NULL );
1380
1381         if ( dolock ) {
1382                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1383         }
1384         assert( mc->mc_refcnt > 0 );
1385         mc->mc_refcnt--;
1386         LDAP_BACK_CONN_BINDING_CLEAR( mc );
1387         if ( LDAP_BACK_CONN_TAINTED( mc ) ) {
1388                 Debug( LDAP_DEBUG_TRACE, "%s meta_back_release_conn: mc=%p conn=%ld tainted.\n",
1389                         op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1390                 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1391                         ( caddr_t )mc, meta_back_conndnmc_cmp );
1392                 if ( mc->mc_refcnt == 0 ) {
1393                         meta_back_conn_free( mc );
1394                 }
1395         }
1396         if ( dolock ) {
1397                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1398         }
1399 }
1400
1401 void
1402 meta_back_quarantine(
1403         Operation       *op,
1404         SlapReply       *rs,
1405         int             candidate )
1406 {
1407         metainfo_t              *mi = (metainfo_t *)op->o_bd->be_private;
1408         metatarget_t            *mt = mi->mi_targets[ candidate ];
1409
1410         slap_retry_info_t       *ri = &mt->mt_quarantine;
1411
1412         ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
1413
1414         if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1415                 time_t  new_last = slap_get_time();
1416
1417                 switch ( mt->mt_isquarantined ) {
1418                 case LDAP_BACK_FQ_NO:
1419                         if ( ri->ri_last == new_last ) {
1420                                 goto done;
1421                         }
1422
1423                         Debug( LDAP_DEBUG_ANY,
1424                                 "%s meta_back_quarantine[%d]: enter.\n",
1425                                 op->o_log_prefix, candidate, 0 );
1426
1427                         ri->ri_idx = 0;
1428                         ri->ri_count = 0;
1429                         break;
1430
1431                 case LDAP_BACK_FQ_RETRYING:
1432                         if ( LogTest( LDAP_DEBUG_ANY ) ) {
1433                                 char    buf[ SLAP_TEXT_BUFLEN ];
1434
1435                                 snprintf( buf, sizeof( buf ),
1436                                         "meta_back_quarantine[%d]: block #%d try #%d failed",
1437                                         candidate, ri->ri_idx, ri->ri_count );
1438                                 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1439                                         op->o_log_prefix, buf, 0 );
1440                         }
1441
1442                         ++ri->ri_count;
1443                         if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1444                                 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1445                         {
1446                                 ri->ri_count = 0;
1447                                 ++ri->ri_idx;
1448                         }
1449                         break;
1450
1451                 default:
1452                         break;
1453                 }
1454
1455                 mt->mt_isquarantined = LDAP_BACK_FQ_YES;
1456                 ri->ri_last = new_last;
1457
1458         } else if ( mt->mt_isquarantined == LDAP_BACK_FQ_RETRYING ) {
1459                 Debug( LDAP_DEBUG_ANY,
1460                         "%s meta_back_quarantine[%d]: exit.\n",
1461                         op->o_log_prefix, candidate, 0 );
1462
1463                 if ( mi->mi_quarantine_f ) {
1464                         (void)mi->mi_quarantine_f( mi, candidate,
1465                                 mi->mi_quarantine_p );
1466                 }
1467
1468                 ri->ri_count = 0;
1469                 ri->ri_idx = 0;
1470                 mt->mt_isquarantined = LDAP_BACK_FQ_NO;
1471         }
1472
1473 done:;
1474         ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );
1475 }
1476