]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/conn.c
fix potential leak introduced by fix to ITS#6574
[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-2010 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  * meta_back_conndn_cmp
39  *
40  * compares two struct metaconn based on the value of the conn pointer
41  * and of the local DN; used by avl stuff
42  */
43 int
44 meta_back_conndn_cmp(
45         const void *c1,
46         const void *c2 )
47 {
48         metaconn_t      *mc1 = ( metaconn_t * )c1;
49         metaconn_t      *mc2 = ( metaconn_t * )c2;
50         int             rc;
51         
52         /* If local DNs don't match, it is definitely not a match */
53         /* For shared sessions, conn is NULL. Only explicitly
54          * bound sessions will have non-NULL conn.
55          */
56         rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
57         if ( rc == 0 ) {
58                 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
59         }
60
61         return rc;
62 }
63
64 /*
65  * meta_back_conndnmc_cmp
66  *
67  * compares two struct metaconn based on the value of the conn pointer,
68  * the local DN and the struct pointer; used by avl stuff
69  */
70 static int
71 meta_back_conndnmc_cmp(
72         const void *c1,
73         const void *c2 )
74 {
75         metaconn_t      *mc1 = ( metaconn_t * )c1;
76         metaconn_t      *mc2 = ( metaconn_t * )c2;
77         int             rc;
78         
79         /* If local DNs don't match, it is definitely not a match */
80         /* For shared sessions, conn is NULL. Only explicitly
81          * bound sessions will have non-NULL conn.
82          */
83         rc = SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
84         if ( rc == 0 ) {
85                 rc = ber_bvcmp( &mc1->mc_local_ndn, &mc2->mc_local_ndn );
86                 if ( rc == 0 ) {
87                         rc = SLAP_PTRCMP( mc1, mc2 );
88                 }
89         }
90
91         return rc;
92 }
93
94 /*
95  * meta_back_conn_cmp
96  *
97  * compares two struct metaconn based on the value of the conn pointer;
98  * used by avl stuff
99  */
100 int
101 meta_back_conn_cmp(
102         const void *c1,
103         const void *c2 )
104 {
105         metaconn_t      *mc1 = ( metaconn_t * )c1;
106         metaconn_t      *mc2 = ( metaconn_t * )c2;
107         
108         /* For shared sessions, conn is NULL. Only explicitly
109          * bound sessions will have non-NULL conn.
110          */
111         return SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
112 }
113
114 /*
115  * meta_back_conndn_dup
116  *
117  * returns -1 in case a duplicate struct metaconn has been inserted;
118  * used by avl stuff
119  */
120 int
121 meta_back_conndn_dup(
122         void *c1,
123         void *c2 )
124 {
125         metaconn_t      *mc1 = ( metaconn_t * )c1;
126         metaconn_t      *mc2 = ( metaconn_t * )c2;
127
128         /* Cannot have more than one shared session with same DN */
129         if ( mc1->mc_conn == mc2->mc_conn &&
130                 dn_match( &mc1->mc_local_ndn, &mc2->mc_local_ndn ) )
131         {
132                 return -1;
133         }
134                 
135         return 0;
136 }
137
138 /*
139  * Debug stuff (got it from libavl)
140  */
141 #if META_BACK_PRINT_CONNTREE > 0
142 static void
143 meta_back_print( metaconn_t *mc, char *avlstr )
144 {
145         int     i;
146
147         fputs( "targets=[", stderr );
148         for ( i = 0; i < mc->mc_info->mi_ntargets; i++ ) {
149                 fputc( mc->mc_conns[ i ].msc_ld ? '*' : 'o', stderr);
150         }
151         fputc( ']', stderr );
152
153         fprintf( stderr, " mc=%p local=\"%s\" conn=%p refcnt=%d%s %s\n",
154                 (void *)mc,
155                 mc->mc_local_ndn.bv_val ? mc->mc_local_ndn.bv_val : "",
156                 (void *)mc->mc_conn,
157                 mc->mc_refcnt,
158                 LDAP_BACK_CONN_TAINTED( mc ) ? " tainted" : "",
159                 avlstr );
160 }
161
162 static void
163 meta_back_ravl_print( Avlnode *root, int depth )
164 {
165         int             i;
166
167         if ( root == 0 ) {
168                 return;
169         }
170         
171         meta_back_ravl_print( root->avl_right, depth + 1 );
172         
173         for ( i = 0; i < depth; i++ ) {
174                 fprintf( stderr, "-" );
175         }
176         fputc( ' ', stderr );
177
178         meta_back_print( (metaconn_t *)root->avl_data,
179                 avl_bf2str( root->avl_bf ) );
180
181         meta_back_ravl_print( root->avl_left, depth + 1 );
182 }
183
184 /* NOTE: duplicate from back-ldap/bind.c */
185 static char* priv2str[] = {
186         "privileged",
187         "privileged/TLS",
188         "anonymous",
189         "anonymous/TLS",
190         "bind",
191         "bind/TLS",
192         NULL
193 };
194
195 void
196 meta_back_print_conntree( metainfo_t *mi, char *msg )
197 {
198         int     c;
199
200         fprintf( stderr, "========> %s\n", msg );
201         
202         for ( c = LDAP_BACK_PCONN_FIRST; c < LDAP_BACK_PCONN_LAST; c++ ) {
203                 int             i = 0;
204                 metaconn_t      *mc;
205
206                 fprintf( stderr, "  %s[%d]\n", priv2str[ c ], mi->mi_conn_priv[ c ].mic_num );
207
208                 LDAP_TAILQ_FOREACH( mc, &mi->mi_conn_priv[ c ].mic_priv, mc_q )
209                 {
210                         fprintf( stderr, "    [%d] ", i );
211                         meta_back_print( mc, "" );
212                         i++;
213                 }
214         }
215         
216         if ( mi->mi_conninfo.lai_tree == NULL ) {
217                 fprintf( stderr, "\t(empty)\n" );
218
219         } else {
220                 meta_back_ravl_print( mi->mi_conninfo.lai_tree, 0 );
221         }
222         
223         fprintf( stderr, "<======== %s\n", msg );
224 }
225 #endif /* META_BACK_PRINT_CONNTREE */
226 /*
227  * End of debug stuff
228  */
229
230 /*
231  * metaconn_alloc
232  * 
233  * Allocates a connection structure, making room for all the referenced targets
234  */
235 static metaconn_t *
236 metaconn_alloc(
237         Operation               *op )
238 {
239         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
240         metaconn_t      *mc;
241         int             ntargets = mi->mi_ntargets;
242
243         assert( ntargets > 0 );
244
245         /* malloc all in one */
246         mc = ( metaconn_t * )ch_calloc( 1, sizeof( metaconn_t )
247                 + sizeof( metasingleconn_t ) * ( ntargets - 1 ) );
248         if ( mc == NULL ) {
249                 return NULL;
250         }
251
252         mc->mc_info = mi;
253
254         mc->mc_authz_target = META_BOUND_NONE;
255         mc->mc_refcnt = 1;
256
257         return mc;
258 }
259
260 /*
261  * meta_back_init_one_conn
262  * 
263  * Initializes one connection
264  */
265 int
266 meta_back_init_one_conn(
267         Operation               *op,
268         SlapReply               *rs,
269         metaconn_t              *mc,
270         int                     candidate,
271         int                     ispriv,
272         ldap_back_send_t        sendok,
273         int                     dolock )
274 {
275         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
276         metatarget_t            *mt = mi->mi_targets[ candidate ];
277         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
278         int                     version;
279         dncookie                dc;
280         int                     isauthz = ( candidate == mc->mc_authz_target );
281         int                     do_return = 0;
282 #ifdef HAVE_TLS
283         int                     is_ldaps = 0;
284 #endif /* HAVE_TLS */
285
286         /* if the server is quarantined, and
287          * - the current interval did not expire yet, or
288          * - no more retries should occur,
289          * don't return the connection */
290         if ( mt->mt_isquarantined ) {
291                 slap_retry_info_t       *ri = &mt->mt_quarantine;
292                 int                     dont_retry = 0;
293
294                 if ( mt->mt_quarantine.ri_interval ) {
295                         ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
296                         dont_retry = ( mt->mt_isquarantined > LDAP_BACK_FQ_NO );
297                         if ( dont_retry ) {
298                                 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
299                                         || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
300                                 if ( !dont_retry ) {
301                                         if ( LogTest( LDAP_DEBUG_ANY ) ) {
302                                                 char    buf[ SLAP_TEXT_BUFLEN ];
303
304                                                 snprintf( buf, sizeof( buf ),
305                                                         "meta_back_init_one_conn[%d]: quarantine "
306                                                         "retry block #%d try #%d",
307                                                         candidate, ri->ri_idx, ri->ri_count );
308                                                 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
309                                                         op->o_log_prefix, buf, 0 );
310                                         }
311
312                                         mt->mt_isquarantined = LDAP_BACK_FQ_RETRYING;
313                                 }
314
315                         }
316                         ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );
317                 }
318
319                 if ( dont_retry ) {
320                         rs->sr_err = LDAP_UNAVAILABLE;
321                         if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
322                                 rs->sr_text = "Target is quarantined";
323                                 send_ldap_result( op, rs );
324                         }
325                         return rs->sr_err;
326                 }
327         }
328
329 retry_lock:;
330         if ( dolock ) {
331                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
332         }
333
334         /*
335          * Already init'ed
336          */
337         if ( LDAP_BACK_CONN_ISBOUND( msc )
338                 || LDAP_BACK_CONN_ISANON( msc ) )
339         {
340                 assert( msc->msc_ld != NULL );
341                 rs->sr_err = LDAP_SUCCESS;
342                 do_return = 1;
343
344         } else if ( META_BACK_CONN_CREATING( msc )
345                 || LDAP_BACK_CONN_BINDING( msc ) )
346         {
347                 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
348                         if ( dolock ) {
349                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
350                         }
351
352                         ldap_pvt_thread_yield();
353                         goto retry_lock;
354                 }
355
356                 /* sounds more appropriate */
357                 rs->sr_err = LDAP_BUSY;
358                 rs->sr_text = "No connections to target are available";
359                 do_return = 1;
360
361         } else if ( META_BACK_CONN_INITED( msc ) ) {
362                 assert( msc->msc_ld != NULL );
363                 rs->sr_err = LDAP_SUCCESS;
364                 do_return = 1;
365
366         } else {
367                 /*
368                  * creating...
369                  */
370                 META_BACK_CONN_CREATING_SET( msc );
371         }
372
373         if ( dolock ) {
374                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
375         }
376
377         if ( do_return ) {
378                 if ( rs->sr_err != LDAP_SUCCESS
379                         && op->o_conn
380                         && ( sendok & LDAP_BACK_SENDERR ) )
381                 {
382                         send_ldap_result( op, rs );
383                 }
384
385                 return rs->sr_err;
386         }
387
388         assert( msc->msc_ld == NULL );
389        
390         /*
391          * Attempts to initialize the connection to the target ds
392          */
393         ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
394         rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
395 #ifdef HAVE_TLS
396         is_ldaps = ldap_is_ldaps_url( mt->mt_uri );
397 #endif /* HAVE_TLS */
398         ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
399         if ( rs->sr_err != LDAP_SUCCESS ) {
400                 goto error_return;
401         }
402
403         /*
404          * Set LDAP version. This will always succeed: If the client
405          * bound with a particular version, then so can we.
406          */
407         if ( mt->mt_version != 0 ) {
408                 version = mt->mt_version;
409
410         } else if ( op->o_conn->c_protocol != 0 ) {
411                 version = op->o_conn->c_protocol;
412
413         } else {
414                 version = LDAP_VERSION3;
415         }
416         ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &version );
417         ldap_set_urllist_proc( msc->msc_ld, mt->mt_urllist_f, mt->mt_urllist_p );
418
419         /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
420         ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS,
421                 META_BACK_TGT_CHASE_REFERRALS( mt ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
422
423 #ifdef HAVE_TLS
424         /* start TLS ("tls [try-]{start|propagate}" statement) */
425         if ( ( META_BACK_TGT_USE_TLS( mt )
426                 || ( op->o_conn->c_is_tls
427                         && META_BACK_TGT_PROPAGATE_TLS( mt ) ) )
428                 && !is_ldaps )
429         {
430 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
431                 /*
432                  * use asynchronous StartTLS; in case, chase referral
433                  * FIXME: OpenLDAP does not return referral on StartTLS yet
434                  */
435                 int             msgid;
436
437                 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
438                 if ( rs->sr_err == LDAP_SUCCESS ) {
439                         LDAPMessage     *res = NULL;
440                         int             rc, nretries = mt->mt_nretries;
441                         struct timeval  tv;
442
443                         LDAP_BACK_TV_SET( &tv );
444
445 retry:;
446                         rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
447                         switch ( rc ) {
448                         case -1:
449                                 rs->sr_err = LDAP_OTHER;
450                                 break;
451
452                         case 0:
453                                 if ( nretries != 0 ) {
454                                         if ( nretries > 0 ) {
455                                                 nretries--;
456                                         }
457                                         LDAP_BACK_TV_SET( &tv );
458                                         goto retry;
459                                 }
460                                 rs->sr_err = LDAP_OTHER;
461                                 break;
462
463                         default:
464                                 /* only touch when activity actually took place... */
465                                 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
466                                         msc->msc_time = op->o_time;
467                                 }
468                                 break;
469                         }
470
471                         if ( rc == LDAP_RES_EXTENDED ) {
472                                 struct berval   *data = NULL;
473
474                                 /* NOTE: right now, data is unused, so don't get it */
475                                 rs->sr_err = ldap_parse_extended_result( msc->msc_ld,
476                                         res, NULL, NULL /* &data */ , 0 );
477                                 if ( rs->sr_err == LDAP_SUCCESS ) {
478                                         int             err;
479
480                                         /* FIXME: matched? referrals? response controls? */
481                                         rs->sr_err = ldap_parse_result( msc->msc_ld,
482                                                 res, &err, NULL, NULL, NULL, NULL, 1 );
483                                         res = NULL;
484
485                                         if ( rs->sr_err == LDAP_SUCCESS ) {
486                                                 rs->sr_err = err;
487                                         }
488                                         rs->sr_err = slap_map_api2result( rs );
489                                         
490                                         /* FIXME: in case a referral 
491                                          * is returned, should we try
492                                          * using it instead of the 
493                                          * configured URI? */
494                                         if ( rs->sr_err == LDAP_SUCCESS ) {
495                                                 ldap_install_tls( msc->msc_ld );
496
497                                         } else if ( rs->sr_err == LDAP_REFERRAL ) {
498                                                 /* FIXME: LDAP_OPERATIONS_ERROR? */
499                                                 rs->sr_err = LDAP_OTHER;
500                                                 rs->sr_text = "Unwilling to chase referral "
501                                                         "returned by Start TLS exop";
502                                         }
503
504                                         if ( data ) {
505                                                 ber_bvfree( data );
506                                         }
507                                 }
508
509                         } else {
510                                 rs->sr_err = LDAP_OTHER;
511                         }
512
513                         if ( res != NULL ) {
514                                 ldap_msgfree( res );
515                         }
516                 }
517 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
518                 /*
519                  * use synchronous StartTLS
520                  */
521                 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
522 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
523
524                 /* if StartTLS is requested, only attempt it if the URL
525                  * is not "ldaps://"; this may occur not only in case
526                  * of misconfiguration, but also when used in the chain 
527                  * overlay, where the "uri" can be parsed out of a referral */
528                 if ( rs->sr_err == LDAP_SERVER_DOWN
529                         || ( rs->sr_err != LDAP_SUCCESS
530                                 && META_BACK_TGT_TLS_CRITICAL( mt ) ) )
531                 {
532
533 #ifdef DEBUG_205
534                         Debug( LDAP_DEBUG_ANY,
535                                 "### %s meta_back_init_one_conn(TLS) "
536                                 "ldap_unbind_ext[%d] ld=%p\n",
537                                 op->o_log_prefix, candidate,
538                                 (void *)msc->msc_ld );
539 #endif /* DEBUG_205 */
540
541                         /* need to trash a failed Start TLS */
542                         meta_clear_one_candidate( op, mc, candidate );
543                         goto error_return;
544                 }
545         }
546 #endif /* HAVE_TLS */
547
548         /*
549          * Set the network timeout if set
550          */
551         if ( mt->mt_network_timeout != 0 ) {
552                 struct timeval  network_timeout;
553
554                 network_timeout.tv_usec = 0;
555                 network_timeout.tv_sec = mt->mt_network_timeout;
556
557                 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
558                                 (void *)&network_timeout );
559         }
560
561         /*
562          * If the connection DN is not null, an attempt to rewrite it is made
563          */
564
565         if ( ispriv ) {
566                 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
567                         ber_bvreplace( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN );
568                         if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
569                                 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
570                                         memset( msc->msc_cred.bv_val, 0,
571                                                 msc->msc_cred.bv_len );
572                                 }
573                                 ber_bvreplace( &msc->msc_cred, &mt->mt_idassert_passwd );
574                         }
575                         LDAP_BACK_CONN_ISIDASSERT_SET( msc );
576
577                 } else {
578                         ber_bvreplace( &msc->msc_bound_ndn, &slap_empty_bv );
579                 }
580
581         } else {
582                 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
583                         memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
584                         ber_memfree_x( msc->msc_cred.bv_val, NULL );
585                         BER_BVZERO( &msc->msc_cred );
586                 }
587                 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
588                         ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
589                         BER_BVZERO( &msc->msc_bound_ndn );
590                 }
591                 if ( !BER_BVISEMPTY( &op->o_ndn )
592                         && SLAP_IS_AUTHZ_BACKEND( op )
593                         && isauthz )
594                 {
595                         dc.target = mt;
596                         dc.conn = op->o_conn;
597                         dc.rs = rs;
598                         dc.ctx = "bindDN";
599                 
600                         /*
601                          * Rewrite the bind dn if needed
602                          */
603                         if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
604                                                 &msc->msc_bound_ndn ) )
605                         {
606
607 #ifdef DEBUG_205
608                                 Debug( LDAP_DEBUG_ANY,
609                                         "### %s meta_back_init_one_conn(rewrite) "
610                                         "ldap_unbind_ext[%d] ld=%p\n",
611                                         op->o_log_prefix, candidate,
612                                         (void *)msc->msc_ld );
613 #endif /* DEBUG_205 */
614
615                                 /* need to trash a connection not fully established */
616                                 meta_clear_one_candidate( op, mc, candidate );
617                                 goto error_return;
618                         }
619                         
620                         /* copy the DN if needed */
621                         if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
622                                 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
623                         }
624
625                         assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
626
627                 } else {
628                         ber_dupbv( &msc->msc_bound_ndn, (struct berval *)&slap_empty_bv );
629                 }
630         }
631
632         assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
633
634 error_return:;
635         if ( dolock ) {
636                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
637         }
638         META_BACK_CONN_CREATING_CLEAR( msc );
639         if ( rs->sr_err == LDAP_SUCCESS ) {
640                 /*
641                  * Sets a cookie for the rewrite session
642                  */
643                 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
644                 META_BACK_CONN_INITED_SET( msc );
645         }
646         if ( dolock ) {
647                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
648         }
649
650         if ( rs->sr_err != LDAP_SUCCESS ) {
651                 rs->sr_err = slap_map_api2result( rs );
652                 if ( sendok & LDAP_BACK_SENDERR ) {
653                         send_ldap_result( op, rs );
654                 }
655         }
656
657         return rs->sr_err;
658 }
659
660 /*
661  * meta_back_retry
662  * 
663  * Retries one connection
664  */
665 int
666 meta_back_retry(
667         Operation               *op,
668         SlapReply               *rs,
669         metaconn_t              **mcp,
670         int                     candidate,
671         ldap_back_send_t        sendok )
672 {
673         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
674         metatarget_t            *mt = mi->mi_targets[ candidate ];
675         metaconn_t              *mc = *mcp;
676         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
677         int                     rc = LDAP_UNAVAILABLE,
678                                 binding,
679                                 quarantine = 1;
680
681         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
682
683         assert( !META_BACK_CONN_CREATING( msc ) );
684         binding = LDAP_BACK_CONN_BINDING( msc );
685         LDAP_BACK_CONN_BINDING_CLEAR( msc );
686
687         assert( mc->mc_refcnt > 0 );
688         if ( mc->mc_refcnt == 1 ) {
689                 struct berval save_cred;
690
691                 if ( LogTest( LDAP_DEBUG_ANY ) ) {
692                         char    buf[ SLAP_TEXT_BUFLEN ];
693
694                         /* this lock is required; however,
695                          * it's invoked only when logging is on */
696                         ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
697                         snprintf( buf, sizeof( buf ),
698                                 "retrying URI=\"%s\" DN=\"%s\"",
699                                 mt->mt_uri,
700                                 BER_BVISNULL( &msc->msc_bound_ndn ) ?
701                                         "" : msc->msc_bound_ndn.bv_val );
702                         ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
703
704                         Debug( LDAP_DEBUG_ANY,
705                                 "%s meta_back_retry[%d]: %s.\n",
706                                 op->o_log_prefix, candidate, buf );
707                 }
708
709                 /* save credentials, if any, for later use;
710                  * meta_clear_one_candidate() would free them */
711                 save_cred = msc->msc_cred;
712                 BER_BVZERO( &msc->msc_cred );
713
714                 meta_clear_one_candidate( op, mc, candidate );
715                 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
716
717                 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
718
719                 /* mc here must be the regular mc, reset and ready for init */
720                 rc = meta_back_init_one_conn( op, rs, mc, candidate,
721                         LDAP_BACK_CONN_ISPRIV( mc ), sendok, 0 );
722
723                 /* restore credentials, if any and if needed;
724                  * meta_back_init_one_conn() restores msc_bound_ndn, if any;
725                  * if no msc_bound_ndn is restored, destroy credentials */
726                 if ( !BER_BVISNULL( &msc->msc_bound_ndn )
727                         && BER_BVISNULL( &msc->msc_cred ) )
728                 {
729                         msc->msc_cred = save_cred;
730
731                 } else if ( !BER_BVISNULL( &save_cred ) ) {
732                         memset( save_cred.bv_val, 0, save_cred.bv_len );
733                         ber_memfree_x( save_cred.bv_val, NULL );
734                 }
735
736                 /* restore the "binding" flag, in case */
737                 if ( binding ) {
738                         LDAP_BACK_CONN_BINDING_SET( msc );
739                 }
740
741                 if ( rc == LDAP_SUCCESS ) {
742                         quarantine = 0;
743                         rc = meta_back_single_dobind( op, rs, mcp, candidate,
744                                 sendok, mt->mt_nretries, 0 );
745
746                         Debug( LDAP_DEBUG_ANY,
747                                 "%s meta_back_retry[%d]: "
748                                 "meta_back_single_dobind=%d\n",
749                                 op->o_log_prefix, candidate, rc );
750                         if ( rc == LDAP_SUCCESS ) {
751                                 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) &&
752                                         !BER_BVISEMPTY( &msc->msc_bound_ndn ) )
753                                 {
754                                         LDAP_BACK_CONN_ISBOUND_SET( msc );
755
756                                 } else {
757                                         LDAP_BACK_CONN_ISANON_SET( msc );
758                                 }
759
760                                 /* when bound, dispose of the "binding" flag */
761                                 if ( binding ) {
762                                         LDAP_BACK_CONN_BINDING_CLEAR( msc );
763                                 }
764                         }
765                 }
766
767                 /* don't send twice */
768                 sendok &= ~LDAP_BACK_SENDERR;
769         }
770
771         if ( rc != LDAP_SUCCESS ) {
772                 SlapReply               *candidates = meta_back_candidates_get( op );
773
774                 candidates[ candidate ].sr_err = rc;
775
776                 if ( *mcp != NULL ) {
777                         if ( mc->mc_refcnt == 1 ) {
778                                 if ( binding ) {
779                                         LDAP_BACK_CONN_BINDING_CLEAR( msc );
780                                 }
781                                 (void)meta_clear_one_candidate( op, mc, candidate );
782                         }
783
784                         LDAP_BACK_CONN_TAINTED_SET( mc );
785                         /* only release if mandatory; otherwise
786                          * let the caller do what's best before
787                          * releasing */
788                         if ( META_BACK_ONERR_STOP( mi ) ) {
789                                 meta_back_release_conn_lock( mi, mc, 0 );
790                                 *mcp = NULL;
791
792                         } else {
793 #if META_BACK_PRINT_CONNTREE > 0
794                                 meta_back_print_conntree( mi, ">>> meta_back_retry" );
795 #endif /* META_BACK_PRINT_CONNTREE */
796
797                                 /* FIXME: could be done better, reworking meta_back_release_conn_lock() */
798                                 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
799                                         if ( mc->mc_q.tqe_prev != NULL ) {
800                                                 assert( LDAP_BACK_CONN_CACHED( mc ) );
801                                                 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
802                                                 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
803                                                         mc, mc_q );
804                                                 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
805                                                 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
806
807                                         } else {
808                                                 assert( !LDAP_BACK_CONN_CACHED( mc ) );
809                                         }
810
811                                 } else {
812                                         /* FIXME: check if in tree, for consistency? */
813                                         (void)avl_delete( &mi->mi_conninfo.lai_tree,
814                                                 ( caddr_t )mc, meta_back_conndnmc_cmp );
815                                 }
816                                 LDAP_BACK_CONN_CACHED_CLEAR( mc );
817
818 #if META_BACK_PRINT_CONNTREE > 0
819                                 meta_back_print_conntree( mi, "<<< meta_back_retry" );
820 #endif /* META_BACK_PRINT_CONNTREE */
821                         }
822                 }
823
824                 if ( sendok & LDAP_BACK_SENDERR ) {
825                         rs->sr_err = rc;
826                         rs->sr_text = "Unable to retry";
827                         send_ldap_result( op, rs );
828                 }
829         }
830
831         if ( quarantine && META_BACK_TGT_QUARANTINE( mt ) ) {
832                 meta_back_quarantine( op, rs, candidate );
833         }
834
835         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
836
837         return rc == LDAP_SUCCESS ? 1 : 0;
838 }
839
840 /*
841  * callback for unique candidate selection
842  */
843 static int
844 meta_back_conn_cb( Operation *op, SlapReply *rs )
845 {
846         assert( op->o_tag == LDAP_REQ_SEARCH );
847
848         switch ( rs->sr_type ) {
849         case REP_SEARCH:
850                 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
851                 break;
852
853         case REP_SEARCHREF:
854         case REP_RESULT:
855                 break;
856
857         default:
858                 return rs->sr_err;
859         }
860
861         return 0;
862 }
863
864
865 static int
866 meta_back_get_candidate(
867         Operation       *op,
868         SlapReply       *rs,
869         struct berval   *ndn )
870 {
871         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
872         long            candidate;
873
874         /*
875          * tries to get a unique candidate
876          * (takes care of default target)
877          */
878         candidate = meta_back_select_unique_candidate( mi, ndn );
879
880         /*
881          * if any is found, inits the connection
882          */
883         if ( candidate == META_TARGET_NONE ) {
884                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
885                 rs->sr_text = "No suitable candidate target found";
886
887         } else if ( candidate == META_TARGET_MULTIPLE ) {
888                 Operation       op2 = *op;
889                 SlapReply       rs2 = { 0 };
890                 slap_callback   cb2 = { 0 };
891                 int             rc;
892
893                 /* try to get a unique match for the request ndn
894                  * among the multiple candidates available */
895                 op2.o_tag = LDAP_REQ_SEARCH;
896                 op2.o_req_dn = *ndn;
897                 op2.o_req_ndn = *ndn;
898                 op2.ors_scope = LDAP_SCOPE_BASE;
899                 op2.ors_deref = LDAP_DEREF_NEVER;
900                 op2.ors_attrs = slap_anlist_no_attrs;
901                 op2.ors_attrsonly = 0;
902                 op2.ors_limit = NULL;
903                 op2.ors_slimit = 1;
904                 op2.ors_tlimit = SLAP_NO_LIMIT;
905
906                 op2.ors_filter = (Filter *)slap_filter_objectClass_pres;
907                 op2.ors_filterstr = *slap_filterstr_objectClass_pres;
908
909                 op2.o_callback = &cb2;
910                 cb2.sc_response = meta_back_conn_cb;
911                 cb2.sc_private = (void *)&candidate;
912
913                 rc = op->o_bd->be_search( &op2, &rs2 );
914
915                 switch ( rs2.sr_err ) {
916                 case LDAP_SUCCESS:
917                 default:
918                         rs->sr_err = rs2.sr_err;
919                         break;
920
921                 case LDAP_SIZELIMIT_EXCEEDED:
922                         /* if multiple candidates can serve the operation,
923                          * and a default target is defined, and it is
924                          * a candidate, try using it (FIXME: YMMV) */
925                         if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
926                                 && meta_back_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ],
927                                                 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
928                         {
929                                 candidate = mi->mi_defaulttarget;
930                                 rs->sr_err = LDAP_SUCCESS;
931                                 rs->sr_text = NULL;
932
933                         } else {
934                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
935                                 rs->sr_text = "Unable to select unique candidate target";
936                         }
937                         break;
938                 }
939
940         } else {
941                 rs->sr_err = LDAP_SUCCESS;
942         }
943
944         return candidate;
945 }
946
947 static void     *meta_back_candidates_dummy;
948
949 static void
950 meta_back_candidates_keyfree(
951         void            *key,
952         void            *data )
953 {
954         metacandidates_t        *mc = (metacandidates_t *)data;
955
956         ber_memfree_x( mc->mc_candidates, NULL );
957         ber_memfree_x( data, NULL );
958 }
959
960 SlapReply *
961 meta_back_candidates_get( Operation *op )
962 {
963         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
964         metacandidates_t        *mc;
965
966         if ( op->o_threadctx ) {
967                 void            *data = NULL;
968
969                 ldap_pvt_thread_pool_getkey( op->o_threadctx,
970                                 &meta_back_candidates_dummy, &data, NULL );
971                 mc = (metacandidates_t *)data;
972
973         } else {
974                 mc = mi->mi_candidates;
975         }
976
977         if ( mc == NULL ) {
978                 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
979                 mc->mc_ntargets = mi->mi_ntargets;
980                 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
981                 if ( op->o_threadctx ) {
982                         void            *data = NULL;
983
984                         data = (void *)mc;
985                         ldap_pvt_thread_pool_setkey( op->o_threadctx,
986                                         &meta_back_candidates_dummy, data,
987                                         meta_back_candidates_keyfree,
988                                         NULL, NULL );
989
990                 } else {
991                         mi->mi_candidates = mc;
992                 }
993
994         } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
995                 /* NOTE: in the future, may want to allow back-config
996                  * to add/remove targets from back-meta... */
997                 mc->mc_candidates = ch_realloc( mc->mc_candidates,
998                                 sizeof( SlapReply ) * mi->mi_ntargets );
999                 memset( &mc->mc_candidates[ mc->mc_ntargets ], 0,
1000                         sizeof( SlapReply ) * ( mi->mi_ntargets - mc->mc_ntargets ) );
1001                 mc->mc_ntargets = mi->mi_ntargets;
1002         }
1003
1004         return mc->mc_candidates;
1005 }
1006
1007 /*
1008  * meta_back_getconn
1009  * 
1010  * Prepares the connection structure
1011  * 
1012  * RATIONALE:
1013  *
1014  * - determine what DN is being requested:
1015  *
1016  *      op      requires candidate      checks
1017  *
1018  *      add     unique                  parent of o_req_ndn
1019  *      bind    unique^*[/all]          o_req_ndn [no check]
1020  *      compare unique^+                o_req_ndn
1021  *      delete  unique                  o_req_ndn
1022  *      modify  unique                  o_req_ndn
1023  *      search  any                     o_req_ndn
1024  *      modrdn  unique[, unique]        o_req_ndn[, orr_nnewSup]
1025  *
1026  * - for ops that require the candidate to be unique, in case of multiple
1027  *   occurrences an internal search with sizeLimit=1 is performed
1028  *   if a unique candidate can actually be determined.  If none is found,
1029  *   the operation aborts; if multiple are found, the default target
1030  *   is used if defined and candidate; otherwise the operation aborts.
1031  *
1032  * *^note: actually, the bind operation is handled much like a search;
1033  *   i.e. the bind is broadcast to all candidate targets.
1034  *
1035  * +^note: actually, the compare operation is handled much like a search;
1036  *   i.e. the compare is broadcast to all candidate targets, while checking
1037  *   that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
1038  *   returned.
1039  */
1040 metaconn_t *
1041 meta_back_getconn(
1042         Operation               *op,
1043         SlapReply               *rs,
1044         int                     *candidate,
1045         ldap_back_send_t        sendok )
1046 {
1047         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
1048         metaconn_t      *mc = NULL,
1049                         mc_curr = {{ 0 }};
1050         int             cached = META_TARGET_NONE,
1051                         i = META_TARGET_NONE,
1052                         err = LDAP_SUCCESS,
1053                         new_conn = 0,
1054                         ncandidates = 0;
1055
1056
1057         meta_op_type    op_type = META_OP_REQUIRE_SINGLE;
1058         enum            {
1059                 META_DNTYPE_ENTRY,
1060                 META_DNTYPE_PARENT,
1061                 META_DNTYPE_NEWPARENT
1062         }               dn_type = META_DNTYPE_ENTRY;
1063         struct berval   ndn = op->o_req_ndn,
1064                         pndn;
1065
1066         SlapReply       *candidates = meta_back_candidates_get( op );
1067
1068         /* Internal searches are privileged and shared. So is root. */
1069         if ( ( !BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ALWAYS( mi ) )
1070                 || ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ANON( mi ) )
1071                 || op->o_do_not_cache || be_isroot( op ) )
1072         {
1073                 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
1074                 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
1075                 LDAP_BACK_PCONN_ROOTDN_SET( &mc_curr, op );
1076
1077         } else if ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_NOANON( mi ) )
1078         {
1079                 LDAP_BACK_CONN_ISANON_SET( &mc_curr );
1080                 BER_BVSTR( &mc_curr.mc_local_ndn, "" );
1081                 LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
1082
1083         } else {
1084                 mc_curr.mc_local_ndn = op->o_ndn;
1085
1086                 /* Explicit binds must not be shared */
1087                 if ( !BER_BVISEMPTY( &op->o_ndn )
1088                         || op->o_tag == LDAP_REQ_BIND
1089                         || SLAP_IS_AUTHZ_BACKEND( op ) )
1090                 {
1091                         mc_curr.mc_conn = op->o_conn;
1092         
1093                 } else {
1094                         LDAP_BACK_CONN_ISANON_SET( &mc_curr );
1095                         LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
1096                 }
1097         }
1098
1099         /* Explicit Bind requests always get their own conn */
1100         if ( sendok & LDAP_BACK_BINDING ) {
1101                 mc_curr.mc_conn = op->o_conn;
1102
1103         } else {
1104                 /* Searches for a metaconn in the avl tree */
1105 retry_lock:;
1106                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1107                 if ( LDAP_BACK_PCONN_ISPRIV( &mc_curr ) ) {
1108                         /* lookup a conn that's not binding */
1109                         LDAP_TAILQ_FOREACH( mc,
1110                                 &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv,
1111                                 mc_q )
1112                         {
1113                                 if ( !LDAP_BACK_CONN_BINDING( mc ) && mc->mc_refcnt == 0 ) {
1114                                         break;
1115                                 }
1116                         }
1117
1118                         if ( mc != NULL ) {
1119                                 /* move to tail of queue */
1120                                 if ( mc != LDAP_TAILQ_LAST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1121                                         metaconn_t, mc_q ) )
1122                                 {
1123                                         LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1124                                                 mc, mc_q );
1125                                         LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1126                                         LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1127                                                 mc, mc_q );
1128                                 }
1129
1130                         } else if ( !LDAP_BACK_USE_TEMPORARIES( mi )
1131                                 && mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_num == mi->mi_conn_priv_max )
1132                         {
1133                                 mc = LDAP_TAILQ_FIRST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv );
1134                         }
1135                         
1136
1137                 } else {
1138                         mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree, 
1139                                 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1140                 }
1141
1142                 if ( mc ) {
1143                         /* catch taint errors */
1144                         assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1145
1146                         /* Don't reuse connections while they're still binding
1147                          * NOTE: only makes sense for binds */
1148                         if ( LDAP_BACK_CONN_BINDING( mc ) ) {
1149                                 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1150                                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1151
1152                                         ldap_pvt_thread_yield();
1153                                         goto retry_lock;
1154                                 }
1155
1156                                 /* release conn, and create a temporary */
1157                                 mc = NULL;
1158
1159                         } else {
1160                                 if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
1161                                         || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
1162                                 {
1163 #if META_BACK_PRINT_CONNTREE > 0
1164                                         meta_back_print_conntree( mi,
1165                                                 ">>> meta_back_getconn(expired)" );
1166 #endif /* META_BACK_PRINT_CONNTREE */
1167
1168                                         /* don't let anyone else use this expired connection */
1169                                         if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1170                                                 if ( mc->mc_q.tqe_prev != NULL ) {
1171                                                         assert( LDAP_BACK_CONN_CACHED( mc ) );
1172                                                         assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
1173                                                         LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1174                                                                 mc, mc_q );
1175                                                         mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1176                                                         LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1177
1178                                                 } else {
1179                                                         assert( !LDAP_BACK_CONN_CACHED( mc ) );
1180                                                 }
1181
1182                                         } else {
1183                                                 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1184                                                         (caddr_t)mc, meta_back_conndnmc_cmp );
1185                                         }
1186
1187 #if META_BACK_PRINT_CONNTREE > 0
1188                                         meta_back_print_conntree( mi,
1189                                                 "<<< meta_back_getconn(expired)" );
1190 #endif /* META_BACK_PRINT_CONNTREE */
1191                                         LDAP_BACK_CONN_TAINTED_SET( mc );
1192                                         LDAP_BACK_CONN_CACHED_CLEAR( mc );
1193
1194                                         if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1195                                                 char buf[STRLENOF("4294967295U") + 1] = { 0 };
1196                                                 mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
1197
1198                                                 Debug( LDAP_DEBUG_TRACE,
1199                                                         "%s meta_back_getconn: mc=%p conn=%s expired (tainted).\n",
1200                                                         op->o_log_prefix, (void *)mc, buf );
1201                                         }
1202                                 }
1203
1204                                 mc->mc_refcnt++;
1205                         }
1206                 }
1207                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1208         }
1209
1210         switch ( op->o_tag ) {
1211         case LDAP_REQ_ADD:
1212                 /* if we go to selection, the entry must not exist,
1213                  * and we must be able to resolve the parent */
1214                 dn_type = META_DNTYPE_PARENT;
1215                 dnParent( &ndn, &pndn );
1216                 break;
1217
1218         case LDAP_REQ_MODRDN:
1219                 /* if nnewSuperior is not NULL, it must resolve
1220                  * to the same candidate as the req_ndn */
1221                 if ( op->orr_nnewSup ) {
1222                         dn_type = META_DNTYPE_NEWPARENT;
1223                 }
1224                 break;
1225
1226         case LDAP_REQ_BIND:
1227                 /* if bound as rootdn, the backend must bind to all targets
1228                  * with the administrative identity
1229                  * (unless pseoudoroot-bind-defer is TRUE) */
1230                 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
1231                         op_type = META_OP_REQUIRE_ALL;
1232                 }
1233                 break;
1234
1235         case LDAP_REQ_COMPARE:
1236         case LDAP_REQ_DELETE:
1237         case LDAP_REQ_MODIFY:
1238                 /* just a unique candidate */
1239                 break;
1240
1241         case LDAP_REQ_SEARCH:
1242                 /* allow multiple candidates for the searchBase */
1243                 op_type = META_OP_ALLOW_MULTIPLE;
1244                 break;
1245
1246         default:
1247                 /* right now, just break (exop?) */
1248                 break;
1249         }
1250
1251         /*
1252          * require all connections ...
1253          */
1254         if ( op_type == META_OP_REQUIRE_ALL ) {
1255
1256                 /* Looks like we didn't get a bind. Open a new session... */
1257                 if ( mc == NULL ) {
1258                         assert( new_conn == 0 );
1259                         mc = metaconn_alloc( op );
1260                         mc->mc_conn = mc_curr.mc_conn;
1261                         ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1262                         new_conn = 1;
1263                         if ( sendok & LDAP_BACK_BINDING ) {
1264                                 LDAP_BACK_CONN_BINDING_SET( mc );
1265                         }
1266                         if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1267                                 LDAP_BACK_CONN_ISPRIV_SET( mc );
1268
1269                         } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1270                                 LDAP_BACK_CONN_ISANON_SET( mc );
1271                         }
1272
1273                 } else if ( 0 ) {
1274                         /* TODO: if any of the connections is binding,
1275                          * release mc and create a new one */
1276                 }
1277
1278                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1279                         /*
1280                          * The target is activated; if needed, it is
1281                          * also init'd
1282                          */
1283                         candidates[ i ].sr_err = meta_back_init_one_conn( op,
1284                                 rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1285                                 LDAP_BACK_DONTSEND, !new_conn );
1286                         if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1287                                 if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) {
1288                                         LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] );
1289                                 }
1290                                 META_CANDIDATE_SET( &candidates[ i ] );
1291                                 ncandidates++;
1292         
1293                         } else {
1294                                 
1295                                 /*
1296                                  * FIXME: in case one target cannot
1297                                  * be init'd, should the other ones
1298                                  * be tried?
1299                                  */
1300                                 META_CANDIDATE_RESET( &candidates[ i ] );
1301                                 err = candidates[ i ].sr_err;
1302                                 continue;
1303                         }
1304                 }
1305
1306                 if ( ncandidates == 0 ) {
1307                         if ( new_conn ) {
1308                                 mc->mc_refcnt = 0;
1309                                 meta_back_conn_free( mc );
1310
1311                         } else {
1312                                 meta_back_release_conn( mi, mc );
1313                         }
1314
1315                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
1316                         rs->sr_text = "Unable to select valid candidates";
1317
1318                         if ( sendok & LDAP_BACK_SENDERR ) {
1319                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1320                                         rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1321                                 }
1322                                 send_ldap_result( op, rs );
1323                                 rs->sr_matched = NULL;
1324                         }
1325
1326                         return NULL;
1327                 }
1328
1329                 goto done;
1330         }
1331         
1332         /*
1333          * looks in cache, if any
1334          */
1335         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1336                 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
1337         }
1338
1339         if ( op_type == META_OP_REQUIRE_SINGLE ) {
1340                 metatarget_t            *mt = NULL;
1341                 metasingleconn_t        *msc = NULL;
1342
1343                 int                     j;
1344
1345                 for ( j = 0; j < mi->mi_ntargets; j++ ) {
1346                         META_CANDIDATE_RESET( &candidates[ j ] );
1347                 }
1348
1349                 /*
1350                  * tries to get a unique candidate
1351                  * (takes care of default target)
1352                  */
1353                 if ( i == META_TARGET_NONE ) {
1354                         i = meta_back_get_candidate( op, rs, &ndn );
1355
1356                         if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1357                                 i = meta_back_get_candidate( op, rs, &pndn );
1358                         }
1359         
1360                         if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1361                                 if ( mc != NULL ) {
1362                                         meta_back_release_conn( mi, mc );
1363                                 }
1364
1365                                 if ( sendok & LDAP_BACK_SENDERR ) {
1366                                         if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1367                                                 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1368                                         }
1369                                         send_ldap_result( op, rs );
1370                                         rs->sr_matched = NULL;
1371                                 }
1372                         
1373                                 return NULL;
1374                         }
1375                 }
1376
1377                 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1378                 {
1379                         if ( mc != NULL ) {
1380                                 meta_back_release_conn( mi, mc );
1381                         }
1382
1383                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1384                         rs->sr_text = "Cross-target rename not supported";
1385                         if ( sendok & LDAP_BACK_SENDERR ) {
1386                                 send_ldap_result( op, rs );
1387                         }
1388
1389                         return NULL;
1390                 }
1391
1392                 Debug( LDAP_DEBUG_TRACE,
1393         "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1394                                 i, op->o_req_ndn.bv_val, 0 );
1395
1396                 if ( mc == NULL ) {
1397                         /* Retries searching for a metaconn in the avl tree
1398                          * the reason is that the connection might have been
1399                          * created by meta_back_get_candidate() */
1400                         if ( !( sendok & LDAP_BACK_BINDING ) ) {
1401 retry_lock2:;
1402                                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1403                                 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree, 
1404                                         (caddr_t)&mc_curr, meta_back_conndn_cmp );
1405                                 if ( mc != NULL ) {
1406                                         /* catch taint errors */
1407                                         assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1408
1409                                         /* Don't reuse connections while they're still binding */
1410                                         if ( META_BACK_CONN_CREATING( &mc->mc_conns[ i ] )
1411                                                 || LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) )
1412                                         {
1413                                                 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1414                                                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1415                                                         ldap_pvt_thread_yield();
1416                                                         goto retry_lock2;
1417                                                 }
1418
1419                                                 mc = NULL;
1420
1421                                         } else {
1422                                                 mc->mc_refcnt++;
1423                                         }
1424                                 }
1425                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1426                         }
1427
1428                         /* Looks like we didn't get a bind. Open a new session... */
1429                         if ( mc == NULL ) {
1430                                 assert( new_conn == 0 );
1431                                 mc = metaconn_alloc( op );
1432                                 mc->mc_conn = mc_curr.mc_conn;
1433                                 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1434                                 new_conn = 1;
1435                                 if ( sendok & LDAP_BACK_BINDING ) {
1436                                         LDAP_BACK_CONN_BINDING_SET( mc );
1437                                 }
1438                                 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1439                                         LDAP_BACK_CONN_ISPRIV_SET( mc );
1440
1441                                 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1442                                         LDAP_BACK_CONN_ISANON_SET( mc );
1443                                 }
1444                         }
1445                 }
1446
1447                 /*
1448                  * Clear all other candidates
1449                  */
1450                 ( void )meta_clear_unused_candidates( op, i );
1451
1452                 mt = mi->mi_targets[ i ];
1453                 msc = &mc->mc_conns[ i ];
1454
1455                 /*
1456                  * The target is activated; if needed, it is
1457                  * also init'd. In case of error, meta_back_init_one_conn
1458                  * sends the appropriate result.
1459                  */
1460                 err = meta_back_init_one_conn( op, rs, mc, i,
1461                         LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok, !new_conn );
1462                 if ( err != LDAP_SUCCESS ) {
1463                         /*
1464                          * FIXME: in case one target cannot
1465                          * be init'd, should the other ones
1466                          * be tried?
1467                          */
1468                         META_CANDIDATE_RESET( &candidates[ i ] );
1469                         if ( new_conn ) {
1470                                 mc->mc_refcnt = 0;
1471                                 meta_back_conn_free( mc );
1472
1473                         } else {
1474                                 meta_back_release_conn( mi, mc );
1475                         }
1476                         return NULL;
1477                 }
1478
1479                 if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) {
1480                         LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] );
1481                 }
1482
1483                 candidates[ i ].sr_err = LDAP_SUCCESS;
1484                 META_CANDIDATE_SET( &candidates[ i ] );
1485                 ncandidates++;
1486
1487                 if ( candidate ) {
1488                         *candidate = i;
1489                 }
1490
1491         /*
1492          * if no unique candidate ...
1493          */
1494         } else {
1495
1496                 /* Looks like we didn't get a bind. Open a new session... */
1497                 if ( mc == NULL ) {
1498                         assert( new_conn == 0 );
1499                         mc = metaconn_alloc( op );
1500                         mc->mc_conn = mc_curr.mc_conn;
1501                         ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1502                         new_conn = 1;
1503                         if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1504                                 LDAP_BACK_CONN_ISPRIV_SET( mc );
1505
1506                         } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1507                                 LDAP_BACK_CONN_ISANON_SET( mc );
1508                         }
1509                 }
1510
1511                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1512                         metatarget_t            *mt = mi->mi_targets[ i ];
1513
1514                         META_CANDIDATE_RESET( &candidates[ i ] );
1515
1516                         if ( i == cached 
1517                                 || meta_back_is_candidate( mt, &op->o_req_ndn,
1518                                         LDAP_SCOPE_SUBTREE ) )
1519                         {
1520
1521                                 /*
1522                                  * The target is activated; if needed, it is
1523                                  * also init'd
1524                                  */
1525                                 int lerr = meta_back_init_one_conn( op, rs, mc, i,
1526                                         LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1527                                         LDAP_BACK_DONTSEND, !new_conn );
1528                                 candidates[ i ].sr_err = lerr;
1529                                 if ( lerr == LDAP_SUCCESS ) {
1530                                         META_CANDIDATE_SET( &candidates[ i ] );
1531                                         ncandidates++;
1532
1533                                         Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1534                                                 op->o_log_prefix, i, 0 );
1535
1536                                 } else if ( lerr == LDAP_UNAVAILABLE && !META_BACK_ONERR_STOP( mi ) ) {
1537                                         META_CANDIDATE_SET( &candidates[ i ] );
1538
1539                                         Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] %s\n",
1540                                                 op->o_log_prefix, i,
1541                                                 mt->mt_isquarantined != LDAP_BACK_FQ_NO ? "quarantined" : "unavailable" );
1542
1543                                 } else {
1544                                 
1545                                         /*
1546                                          * FIXME: in case one target cannot
1547                                          * be init'd, should the other ones
1548                                          * be tried?
1549                                          */
1550                                         if ( new_conn ) {
1551                                                 ( void )meta_clear_one_candidate( op, mc, i );
1552                                         }
1553                                         /* leave the target candidate, but record the error for later use */
1554                                         err = lerr;
1555
1556                                         if ( lerr == LDAP_UNAVAILABLE && mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
1557                                                 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] quarantined err=%d\n",
1558                                                         op->o_log_prefix, i, lerr );
1559
1560                                         } else {
1561                                                 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed err=%d\n",
1562                                                         op->o_log_prefix, i, lerr );
1563                                         }
1564
1565                                         if ( META_BACK_ONERR_STOP( mi ) ) {
1566                                                 if ( sendok & LDAP_BACK_SENDERR ) {
1567                                                         send_ldap_result( op, rs );
1568                                                 }
1569                                                 if ( new_conn ) {
1570                                                         mc->mc_refcnt = 0;
1571                                                         meta_back_conn_free( mc );
1572
1573                                                 } else {
1574                                                         meta_back_release_conn( mi, mc );
1575                                                 }
1576
1577                                                 return NULL;
1578                                         }
1579
1580                                         continue;
1581                                 }
1582
1583                         } else {
1584                                 if ( new_conn ) {
1585                                         ( void )meta_clear_one_candidate( op, mc, i );
1586                                 }
1587                         }
1588                 }
1589
1590                 if ( ncandidates == 0 ) {
1591                         if ( new_conn ) {
1592                                 mc->mc_refcnt = 0;
1593                                 meta_back_conn_free( mc );
1594
1595                         } else {
1596                                 meta_back_release_conn( mi, mc );
1597                         }
1598
1599                         if ( rs->sr_err == LDAP_SUCCESS ) {
1600                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1601                                 rs->sr_text = "Unable to select valid candidates";
1602                         }
1603
1604                         if ( sendok & LDAP_BACK_SENDERR ) {
1605                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1606                                         rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1607                                 }
1608                                 send_ldap_result( op, rs );
1609                                 rs->sr_matched = NULL;
1610                         }
1611
1612                         return NULL;
1613                 }
1614         }
1615
1616 done:;
1617         /* clear out meta_back_init_one_conn non-fatal errors */
1618         rs->sr_err = LDAP_SUCCESS;
1619         rs->sr_text = NULL;
1620
1621         /* touch the timestamp */
1622         if ( mi->mi_idle_timeout != 0 ) {
1623                 mc->mc_time = op->o_time;
1624         }
1625
1626         if ( new_conn ) {
1627                 if ( mi->mi_conn_ttl ) {
1628                         mc->mc_create_time = op->o_time;
1629                 }
1630
1631                 /*
1632                  * Inserts the newly created metaconn in the avl tree
1633                  */
1634                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1635 #if META_BACK_PRINT_CONNTREE > 0
1636                 meta_back_print_conntree( mi, ">>> meta_back_getconn" );
1637 #endif /* META_BACK_PRINT_CONNTREE */
1638
1639                 err = 0;
1640                 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1641                         if ( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num < mi->mi_conn_priv_max ) {
1642                                 LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1643                                 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num++;
1644                                 LDAP_BACK_CONN_CACHED_SET( mc );
1645
1646                         } else {
1647                                 LDAP_BACK_CONN_TAINTED_SET( mc );
1648                         }
1649                         rs->sr_err = 0;
1650
1651                 } else if ( !( sendok & LDAP_BACK_BINDING ) ) {
1652                         err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1653                                 meta_back_conndn_cmp, meta_back_conndn_dup );
1654                         LDAP_BACK_CONN_CACHED_SET( mc );
1655                 }
1656
1657 #if META_BACK_PRINT_CONNTREE > 0
1658                 meta_back_print_conntree( mi, "<<< meta_back_getconn" );
1659 #endif /* META_BACK_PRINT_CONNTREE */
1660                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1661
1662                 if ( !LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1663                         /*
1664                          * Err could be -1 in case a duplicate metaconn is inserted
1665                          */
1666                         switch ( err ) {
1667                         case 0:
1668                                 break;
1669
1670                         case -1:
1671                                 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1672                                 /* duplicate: free and try to get the newly created one */
1673                                 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1674                                         mc->mc_refcnt = 0;      
1675                                         meta_back_conn_free( mc );
1676         
1677                                         new_conn = 0;
1678                                         goto retry_lock;
1679                                 }
1680
1681                                 LDAP_BACK_CONN_TAINTED_SET( mc );
1682                                 break;
1683
1684                         default:
1685                                 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1686                                 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1687                                         char buf[STRLENOF("4294967295U") + 1] = { 0 };
1688                                         mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
1689
1690                                         Debug( LDAP_DEBUG_ANY,
1691                                                 "%s meta_back_getconn: candidates=%d conn=%s insert failed\n",
1692                                                 op->o_log_prefix, ncandidates, buf );
1693                                 }
1694         
1695                                 mc->mc_refcnt = 0;      
1696                                 meta_back_conn_free( mc );
1697
1698                                 rs->sr_err = LDAP_OTHER;
1699                                 rs->sr_text = "Proxy bind collision";
1700                                 if ( sendok & LDAP_BACK_SENDERR ) {
1701                                         send_ldap_result( op, rs );
1702                                 }
1703                                 return NULL;
1704                         }
1705                 }
1706
1707                 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1708                         char buf[STRLENOF("4294967295U") + 1] = { 0 };
1709                         mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
1710
1711                         Debug( LDAP_DEBUG_TRACE,
1712                                 "%s meta_back_getconn: candidates=%d conn=%s inserted\n",
1713                                 op->o_log_prefix, ncandidates, buf );
1714                 }
1715
1716         } else {
1717                 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1718                         char buf[STRLENOF("4294967295U") + 1] = { 0 };
1719                         mi->mi_ldap_extra->connid2str( &mc->mc_base, buf, sizeof(buf) );
1720
1721                         Debug( LDAP_DEBUG_TRACE,
1722                                 "%s meta_back_getconn: candidates=%d conn=%s fetched\n",
1723                                 op->o_log_prefix, ncandidates, buf );
1724                 }
1725         }
1726
1727         return mc;
1728 }
1729
1730 void
1731 meta_back_release_conn_lock(
1732         metainfo_t              *mi,
1733         metaconn_t              *mc,
1734         int                     dolock )
1735 {
1736         assert( mc != NULL );
1737
1738         if ( dolock ) {
1739                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1740         }
1741         assert( mc->mc_refcnt > 0 );
1742         mc->mc_refcnt--;
1743         /* NOTE: the connection is removed if either it is tainted
1744          * or if it is shared and no one else is using it.  This needs
1745          * to occur because for intrinsic reasons cached connections
1746          * that are not privileged would live forever and pollute
1747          * the connection space (and eat up resources).  Maybe this
1748          * should be configurable... */
1749         if ( LDAP_BACK_CONN_TAINTED( mc ) || !LDAP_BACK_CONN_CACHED( mc ) ) {
1750 #if META_BACK_PRINT_CONNTREE > 0
1751                 meta_back_print_conntree( mi, ">>> meta_back_release_conn" );
1752 #endif /* META_BACK_PRINT_CONNTREE */
1753
1754                 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1755                         if ( mc->mc_q.tqe_prev != NULL ) {
1756                                 assert( LDAP_BACK_CONN_CACHED( mc ) );
1757                                 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
1758                                 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1759                                 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1760                                 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1761
1762                         } else {
1763                                 assert( !LDAP_BACK_CONN_CACHED( mc ) );
1764                         }
1765
1766                 } else if ( LDAP_BACK_CONN_CACHED( mc ) ) {
1767                         metaconn_t      *tmpmc;
1768
1769                         tmpmc = avl_delete( &mi->mi_conninfo.lai_tree,
1770                                 ( caddr_t )mc, meta_back_conndnmc_cmp );
1771
1772                         /* Overparanoid, but useful... */
1773                         assert( tmpmc == NULL || tmpmc == mc );
1774                 }
1775
1776                 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1777
1778 #if META_BACK_PRINT_CONNTREE > 0
1779                 meta_back_print_conntree( mi, "<<< meta_back_release_conn" );
1780 #endif /* META_BACK_PRINT_CONNTREE */
1781
1782                 if ( mc->mc_refcnt == 0 ) {
1783                         meta_back_conn_free( mc );
1784                         mc = NULL;
1785                 }
1786         }
1787
1788         if ( mc != NULL && LDAP_BACK_CONN_BINDING( mc ) ) {
1789                 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1790         }
1791
1792         if ( dolock ) {
1793                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1794         }
1795 }
1796
1797 void
1798 meta_back_quarantine(
1799         Operation       *op,
1800         SlapReply       *rs,
1801         int             candidate )
1802 {
1803         metainfo_t              *mi = (metainfo_t *)op->o_bd->be_private;
1804         metatarget_t            *mt = mi->mi_targets[ candidate ];
1805
1806         slap_retry_info_t       *ri = &mt->mt_quarantine;
1807
1808         ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
1809
1810         if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1811                 time_t  new_last = slap_get_time();
1812
1813                 switch ( mt->mt_isquarantined ) {
1814                 case LDAP_BACK_FQ_NO:
1815                         if ( ri->ri_last == new_last ) {
1816                                 goto done;
1817                         }
1818
1819                         Debug( LDAP_DEBUG_ANY,
1820                                 "%s meta_back_quarantine[%d]: enter.\n",
1821                                 op->o_log_prefix, candidate, 0 );
1822
1823                         ri->ri_idx = 0;
1824                         ri->ri_count = 0;
1825                         break;
1826
1827                 case LDAP_BACK_FQ_RETRYING:
1828                         if ( LogTest( LDAP_DEBUG_ANY ) ) {
1829                                 char    buf[ SLAP_TEXT_BUFLEN ];
1830
1831                                 snprintf( buf, sizeof( buf ),
1832                                         "meta_back_quarantine[%d]: block #%d try #%d failed",
1833                                         candidate, ri->ri_idx, ri->ri_count );
1834                                 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1835                                         op->o_log_prefix, buf, 0 );
1836                         }
1837
1838                         ++ri->ri_count;
1839                         if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1840                                 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1841                         {
1842                                 ri->ri_count = 0;
1843                                 ++ri->ri_idx;
1844                         }
1845                         break;
1846
1847                 default:
1848                         break;
1849                 }
1850
1851                 mt->mt_isquarantined = LDAP_BACK_FQ_YES;
1852                 ri->ri_last = new_last;
1853
1854         } else if ( mt->mt_isquarantined == LDAP_BACK_FQ_RETRYING ) {
1855                 Debug( LDAP_DEBUG_ANY,
1856                         "%s meta_back_quarantine[%d]: exit.\n",
1857                         op->o_log_prefix, candidate, 0 );
1858
1859                 if ( mi->mi_quarantine_f ) {
1860                         (void)mi->mi_quarantine_f( mi, candidate,
1861                                 mi->mi_quarantine_p );
1862                 }
1863
1864                 ri->ri_count = 0;
1865                 ri->ri_idx = 0;
1866                 mt->mt_isquarantined = LDAP_BACK_FQ_NO;
1867         }
1868
1869 done:;
1870         ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );
1871 }
1872