]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/conn.c
better logging of privileged connections (more to come; might be unstable for a bit)
[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-2009 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                                         
489                                         /* FIXME: in case a referral 
490                                          * is returned, should we try
491                                          * using it instead of the 
492                                          * configured URI? */
493                                         if ( rs->sr_err == LDAP_SUCCESS ) {
494                                                 ldap_install_tls( msc->msc_ld );
495
496                                         } else if ( rs->sr_err == LDAP_REFERRAL ) {
497                                                 /* FIXME: LDAP_OPERATIONS_ERROR? */
498                                                 rs->sr_err = LDAP_OTHER;
499                                                 rs->sr_text = "Unwilling to chase referral "
500                                                         "returned by Start TLS exop";
501                                         }
502
503                                         if ( data ) {
504                                                 ber_bvfree( data );
505                                         }
506                                 }
507
508                         } else {
509                                 rs->sr_err = LDAP_OTHER;
510                         }
511
512                         if ( res != NULL ) {
513                                 ldap_msgfree( res );
514                         }
515                 }
516 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
517                 /*
518                  * use synchronous StartTLS
519                  */
520                 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
521 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
522
523                 /* if StartTLS is requested, only attempt it if the URL
524                  * is not "ldaps://"; this may occur not only in case
525                  * of misconfiguration, but also when used in the chain 
526                  * overlay, where the "uri" can be parsed out of a referral */
527                 if ( rs->sr_err == LDAP_SERVER_DOWN
528                         || ( rs->sr_err != LDAP_SUCCESS
529                                 && META_BACK_TGT_TLS_CRITICAL( mt ) ) )
530                 {
531
532 #ifdef DEBUG_205
533                         Debug( LDAP_DEBUG_ANY,
534                                 "### %s meta_back_init_one_conn(TLS) "
535                                 "ldap_unbind_ext[%d] ld=%p\n",
536                                 op->o_log_prefix, candidate,
537                                 (void *)msc->msc_ld );
538 #endif /* DEBUG_205 */
539
540                         /* need to trash a failed Start TLS */
541                         meta_clear_one_candidate( op, mc, candidate );
542                         goto error_return;
543                 }
544         }
545 #endif /* HAVE_TLS */
546
547         /*
548          * Set the network timeout if set
549          */
550         if ( mt->mt_network_timeout != 0 ) {
551                 struct timeval  network_timeout;
552
553                 network_timeout.tv_usec = 0;
554                 network_timeout.tv_sec = mt->mt_network_timeout;
555
556                 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
557                                 (void *)&network_timeout );
558         }
559
560         /*
561          * If the connection DN is not null, an attempt to rewrite it is made
562          */
563
564         if ( ispriv ) {
565                 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
566                         ber_bvreplace( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN );
567                         if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
568                                 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
569                                         memset( msc->msc_cred.bv_val, 0,
570                                                 msc->msc_cred.bv_len );
571                                 }
572                                 ber_bvreplace( &msc->msc_cred, &mt->mt_idassert_passwd );
573                         }
574
575                 } else {
576                         ber_bvreplace( &msc->msc_bound_ndn, &slap_empty_bv );
577                 }
578
579         } else {
580                 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
581                         memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
582                         ber_memfree_x( msc->msc_cred.bv_val, NULL );
583                         BER_BVZERO( &msc->msc_cred );
584                 }
585                 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
586                         ber_memfree_x( msc->msc_bound_ndn.bv_val, NULL );
587                         BER_BVZERO( &msc->msc_bound_ndn );
588                 }
589                 if ( !BER_BVISEMPTY( &op->o_ndn )
590                         && SLAP_IS_AUTHZ_BACKEND( op )
591                         && isauthz )
592                 {
593                         dc.target = mt;
594                         dc.conn = op->o_conn;
595                         dc.rs = rs;
596                         dc.ctx = "bindDN";
597                 
598                         /*
599                          * Rewrite the bind dn if needed
600                          */
601                         if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
602                                                 &msc->msc_bound_ndn ) )
603                         {
604
605 #ifdef DEBUG_205
606                                 Debug( LDAP_DEBUG_ANY,
607                                         "### %s meta_back_init_one_conn(rewrite) "
608                                         "ldap_unbind_ext[%d] ld=%p\n",
609                                         op->o_log_prefix, candidate,
610                                         (void *)msc->msc_ld );
611 #endif /* DEBUG_205 */
612
613                                 /* need to trash a connection not fully established */
614                                 meta_clear_one_candidate( op, mc, candidate );
615                                 goto error_return;
616                         }
617                         
618                         /* copy the DN if needed */
619                         if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
620                                 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
621                         }
622
623                         assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
624
625                 } else {
626                         ber_dupbv( &msc->msc_bound_ndn, (struct berval *)&slap_empty_bv );
627                 }
628         }
629
630         assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
631
632 error_return:;
633         if ( dolock ) {
634                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
635         }
636         META_BACK_CONN_CREATING_CLEAR( msc );
637         if ( rs->sr_err == LDAP_SUCCESS ) {
638                 /*
639                  * Sets a cookie for the rewrite session
640                  */
641                 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
642                 META_BACK_CONN_INITED_SET( msc );
643         }
644         if ( dolock ) {
645                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
646         }
647
648         if ( rs->sr_err != LDAP_SUCCESS ) {
649                 rs->sr_err = slap_map_api2result( rs );
650                 if ( sendok & LDAP_BACK_SENDERR ) {
651                         send_ldap_result( op, rs );
652                 }
653         }
654
655         return rs->sr_err;
656 }
657
658 /*
659  * meta_back_retry
660  * 
661  * Retries one connection
662  */
663 int
664 meta_back_retry(
665         Operation               *op,
666         SlapReply               *rs,
667         metaconn_t              **mcp,
668         int                     candidate,
669         ldap_back_send_t        sendok )
670 {
671         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
672         metatarget_t            *mt = mi->mi_targets[ candidate ];
673         metaconn_t              *mc = *mcp;
674         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
675         int                     rc = LDAP_UNAVAILABLE,
676                                 binding,
677                                 quarantine = 1;
678
679         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
680
681         assert( !META_BACK_CONN_CREATING( msc ) );
682         binding = LDAP_BACK_CONN_BINDING( msc );
683         LDAP_BACK_CONN_BINDING_CLEAR( msc );
684
685         assert( mc->mc_refcnt > 0 );
686         if ( mc->mc_refcnt == 1 ) {
687                 if ( LogTest( LDAP_DEBUG_ANY ) ) {
688                         char    buf[ SLAP_TEXT_BUFLEN ];
689
690                         /* this lock is required; however,
691                          * it's invoked only when logging is on */
692                         ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
693                         snprintf( buf, sizeof( buf ),
694                                 "retrying URI=\"%s\" DN=\"%s\"",
695                                 mt->mt_uri,
696                                 BER_BVISNULL( &msc->msc_bound_ndn ) ?
697                                         "" : msc->msc_bound_ndn.bv_val );
698                         ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
699
700                         Debug( LDAP_DEBUG_ANY,
701                                 "%s meta_back_retry[%d]: %s.\n",
702                                 op->o_log_prefix, candidate, buf );
703                 }
704
705                 meta_clear_one_candidate( op, mc, candidate );
706                 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
707
708                 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
709
710                 /* mc here must be the regular mc, reset and ready for init */
711                 rc = meta_back_init_one_conn( op, rs, mc, candidate,
712                         LDAP_BACK_CONN_ISPRIV( mc ), sendok, 0 );
713
714                 /* restore the "binding" flag, in case */
715                 if ( binding ) {
716                         LDAP_BACK_CONN_BINDING_SET( msc );
717                 }
718
719                 if ( rc == LDAP_SUCCESS ) {
720                         quarantine = 0;
721                         rc = meta_back_single_dobind( op, rs, mcp, candidate,
722                                 sendok, mt->mt_nretries, 0 );
723
724                         Debug( LDAP_DEBUG_ANY,
725                                 "%s meta_back_retry[%d]: "
726                                 "meta_back_single_dobind=%d\n",
727                                 op->o_log_prefix, candidate, rc );
728                         if ( rc == LDAP_SUCCESS ) {
729                                 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) &&
730                                         !BER_BVISEMPTY( &msc->msc_bound_ndn ) )
731                                 {
732                                         LDAP_BACK_CONN_ISBOUND_SET( msc );
733
734                                 } else {
735                                         LDAP_BACK_CONN_ISANON_SET( msc );
736                                 }
737
738                                 /* when bound, dispose of the "binding" flag */
739                                 if ( binding ) {
740                                         LDAP_BACK_CONN_BINDING_CLEAR( msc );
741                                 }
742                         }
743                 }
744
745                 /* don't send twice */
746                 sendok &= ~LDAP_BACK_SENDERR;
747         }
748
749         if ( rc != LDAP_SUCCESS ) {
750                 SlapReply               *candidates = meta_back_candidates_get( op );
751
752                 candidates[ candidate ].sr_err = rc;
753
754                 if ( *mcp != NULL ) {
755                         if ( mc->mc_refcnt == 1 ) {
756                                 if ( binding ) {
757                                         LDAP_BACK_CONN_BINDING_CLEAR( msc );
758                                 }
759                                 (void)meta_clear_one_candidate( op, mc, candidate );
760                         }
761
762                         LDAP_BACK_CONN_TAINTED_SET( mc );
763                         /* only release if mandatory; otherwise
764                          * let the caller do what's best before
765                          * releasing */
766                         if ( META_BACK_ONERR_STOP( mi ) ) {
767                                 meta_back_release_conn_lock( mi, mc, 0 );
768                                 *mcp = NULL;
769
770                         } else {
771 #if META_BACK_PRINT_CONNTREE > 0
772                                 meta_back_print_conntree( mi, ">>> meta_back_retry" );
773 #endif /* META_BACK_PRINT_CONNTREE */
774
775                                 /* FIXME: could be done better, reworking meta_back_release_conn_lock() */
776                                 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
777                                         if ( mc->mc_q.tqe_prev != NULL ) {
778                                                 assert( LDAP_BACK_CONN_CACHED( mc ) );
779                                                 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
780                                                 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
781                                                         mc, mc_q );
782                                                 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
783                                                 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
784
785                                         } else {
786                                                 assert( !LDAP_BACK_CONN_CACHED( mc ) );
787                                         }
788
789                                 } else {
790                                         /* FIXME: check if in tree, for consistency? */
791                                         (void)avl_delete( &mi->mi_conninfo.lai_tree,
792                                                 ( caddr_t )mc, meta_back_conndnmc_cmp );
793                                 }
794                                 LDAP_BACK_CONN_CACHED_CLEAR( mc );
795
796 #if META_BACK_PRINT_CONNTREE > 0
797                                 meta_back_print_conntree( mi, "<<< meta_back_retry" );
798 #endif /* META_BACK_PRINT_CONNTREE */
799                         }
800                 }
801
802                 if ( sendok & LDAP_BACK_SENDERR ) {
803                         rs->sr_err = rc;
804                         rs->sr_text = "Unable to retry";
805                         send_ldap_result( op, rs );
806                 }
807         }
808
809         if ( quarantine && META_BACK_TGT_QUARANTINE( mt ) ) {
810                 meta_back_quarantine( op, rs, candidate );
811         }
812
813         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
814
815         return rc == LDAP_SUCCESS ? 1 : 0;
816 }
817
818 /*
819  * callback for unique candidate selection
820  */
821 static int
822 meta_back_conn_cb( Operation *op, SlapReply *rs )
823 {
824         assert( op->o_tag == LDAP_REQ_SEARCH );
825
826         switch ( rs->sr_type ) {
827         case REP_SEARCH:
828                 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
829                 break;
830
831         case REP_SEARCHREF:
832         case REP_RESULT:
833                 break;
834
835         default:
836                 return rs->sr_err;
837         }
838
839         return 0;
840 }
841
842
843 static int
844 meta_back_get_candidate(
845         Operation       *op,
846         SlapReply       *rs,
847         struct berval   *ndn )
848 {
849         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
850         long            candidate;
851
852         /*
853          * tries to get a unique candidate
854          * (takes care of default target)
855          */
856         candidate = meta_back_select_unique_candidate( mi, ndn );
857
858         /*
859          * if any is found, inits the connection
860          */
861         if ( candidate == META_TARGET_NONE ) {
862                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
863                 rs->sr_text = "No suitable candidate target found";
864
865         } else if ( candidate == META_TARGET_MULTIPLE ) {
866                 Operation       op2 = *op;
867                 SlapReply       rs2 = { 0 };
868                 slap_callback   cb2 = { 0 };
869                 int             rc;
870
871                 /* try to get a unique match for the request ndn
872                  * among the multiple candidates available */
873                 op2.o_tag = LDAP_REQ_SEARCH;
874                 op2.o_req_dn = *ndn;
875                 op2.o_req_ndn = *ndn;
876                 op2.ors_scope = LDAP_SCOPE_BASE;
877                 op2.ors_deref = LDAP_DEREF_NEVER;
878                 op2.ors_attrs = slap_anlist_no_attrs;
879                 op2.ors_attrsonly = 0;
880                 op2.ors_limit = NULL;
881                 op2.ors_slimit = 1;
882                 op2.ors_tlimit = SLAP_NO_LIMIT;
883
884                 op2.ors_filter = (Filter *)slap_filter_objectClass_pres;
885                 op2.ors_filterstr = *slap_filterstr_objectClass_pres;
886
887                 op2.o_callback = &cb2;
888                 cb2.sc_response = meta_back_conn_cb;
889                 cb2.sc_private = (void *)&candidate;
890
891                 rc = op->o_bd->be_search( &op2, &rs2 );
892
893                 switch ( rs2.sr_err ) {
894                 case LDAP_SUCCESS:
895                 default:
896                         rs->sr_err = rs2.sr_err;
897                         break;
898
899                 case LDAP_SIZELIMIT_EXCEEDED:
900                         /* if multiple candidates can serve the operation,
901                          * and a default target is defined, and it is
902                          * a candidate, try using it (FIXME: YMMV) */
903                         if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
904                                 && meta_back_is_candidate( mi->mi_targets[ mi->mi_defaulttarget ],
905                                                 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
906                         {
907                                 candidate = mi->mi_defaulttarget;
908                                 rs->sr_err = LDAP_SUCCESS;
909                                 rs->sr_text = NULL;
910
911                         } else {
912                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
913                                 rs->sr_text = "Unable to select unique candidate target";
914                         }
915                         break;
916                 }
917
918         } else {
919                 rs->sr_err = LDAP_SUCCESS;
920         }
921
922         return candidate;
923 }
924
925 static void     *meta_back_candidates_dummy;
926
927 static void
928 meta_back_candidates_keyfree(
929         void            *key,
930         void            *data )
931 {
932         metacandidates_t        *mc = (metacandidates_t *)data;
933
934         ber_memfree_x( mc->mc_candidates, NULL );
935         ber_memfree_x( data, NULL );
936 }
937
938 SlapReply *
939 meta_back_candidates_get( Operation *op )
940 {
941         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
942         metacandidates_t        *mc;
943
944         if ( op->o_threadctx ) {
945                 void            *data = NULL;
946
947                 ldap_pvt_thread_pool_getkey( op->o_threadctx,
948                                 &meta_back_candidates_dummy, &data, NULL );
949                 mc = (metacandidates_t *)data;
950
951         } else {
952                 mc = mi->mi_candidates;
953         }
954
955         if ( mc == NULL ) {
956                 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
957                 mc->mc_ntargets = mi->mi_ntargets;
958                 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
959                 if ( op->o_threadctx ) {
960                         void            *data = NULL;
961
962                         data = (void *)mc;
963                         ldap_pvt_thread_pool_setkey( op->o_threadctx,
964                                         &meta_back_candidates_dummy, data,
965                                         meta_back_candidates_keyfree,
966                                         NULL, NULL );
967
968                 } else {
969                         mi->mi_candidates = mc;
970                 }
971
972         } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
973                 /* NOTE: in the future, may want to allow back-config
974                  * to add/remove targets from back-meta... */
975                 mc->mc_candidates = ch_realloc( mc->mc_candidates,
976                                 sizeof( SlapReply ) * mi->mi_ntargets );
977                 memset( &mc->mc_candidates[ mc->mc_ntargets ], 0,
978                         sizeof( SlapReply ) * ( mi->mi_ntargets - mc->mc_ntargets ) );
979                 mc->mc_ntargets = mi->mi_ntargets;
980         }
981
982         return mc->mc_candidates;
983 }
984
985 /*
986  * meta_back_getconn
987  * 
988  * Prepares the connection structure
989  * 
990  * RATIONALE:
991  *
992  * - determine what DN is being requested:
993  *
994  *      op      requires candidate      checks
995  *
996  *      add     unique                  parent of o_req_ndn
997  *      bind    unique^*[/all]          o_req_ndn [no check]
998  *      compare unique^+                o_req_ndn
999  *      delete  unique                  o_req_ndn
1000  *      modify  unique                  o_req_ndn
1001  *      search  any                     o_req_ndn
1002  *      modrdn  unique[, unique]        o_req_ndn[, orr_nnewSup]
1003  *
1004  * - for ops that require the candidate to be unique, in case of multiple
1005  *   occurrences an internal search with sizeLimit=1 is performed
1006  *   if a unique candidate can actually be determined.  If none is found,
1007  *   the operation aborts; if multiple are found, the default target
1008  *   is used if defined and candidate; otherwise the operation aborts.
1009  *
1010  * *^note: actually, the bind operation is handled much like a search;
1011  *   i.e. the bind is broadcast to all candidate targets.
1012  *
1013  * +^note: actually, the compare operation is handled much like a search;
1014  *   i.e. the compare is broadcast to all candidate targets, while checking
1015  *   that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
1016  *   returned.
1017  */
1018 metaconn_t *
1019 meta_back_getconn(
1020         Operation               *op,
1021         SlapReply               *rs,
1022         int                     *candidate,
1023         ldap_back_send_t        sendok )
1024 {
1025         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
1026         metaconn_t      *mc = NULL,
1027                         mc_curr = { 0 };
1028         int             cached = META_TARGET_NONE,
1029                         i = META_TARGET_NONE,
1030                         err = LDAP_SUCCESS,
1031                         new_conn = 0,
1032                         ncandidates = 0;
1033
1034
1035         meta_op_type    op_type = META_OP_REQUIRE_SINGLE;
1036         enum            {
1037                 META_DNTYPE_ENTRY,
1038                 META_DNTYPE_PARENT,
1039                 META_DNTYPE_NEWPARENT
1040         }               dn_type = META_DNTYPE_ENTRY;
1041         struct berval   ndn = op->o_req_ndn,
1042                         pndn;
1043
1044         SlapReply       *candidates = meta_back_candidates_get( op );
1045
1046         /* Internal searches are privileged and shared. So is root. */
1047         if ( ( !BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ALWAYS( mi ) )
1048                 || ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_ANON( mi ) )
1049                 || op->o_do_not_cache || be_isroot( op ) )
1050         {
1051                 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
1052                 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
1053                 LDAP_BACK_PCONN_ROOTDN_SET( &mc_curr, op );
1054
1055         } else if ( BER_BVISEMPTY( &op->o_ndn ) && META_BACK_PROXYAUTHZ_NOANON( mi ) )
1056         {
1057                 LDAP_BACK_CONN_ISANON_SET( &mc_curr );
1058                 BER_BVSTR( &mc_curr.mc_local_ndn, "" );
1059                 LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
1060
1061         } else {
1062                 mc_curr.mc_local_ndn = op->o_ndn;
1063
1064                 /* Explicit binds must not be shared */
1065                 if ( !BER_BVISEMPTY( &op->o_ndn )
1066                         || op->o_tag == LDAP_REQ_BIND
1067                         || SLAP_IS_AUTHZ_BACKEND( op ) )
1068                 {
1069                         mc_curr.mc_conn = op->o_conn;
1070         
1071                 } else {
1072                         LDAP_BACK_CONN_ISANON_SET( &mc_curr );
1073                         LDAP_BACK_PCONN_ANON_SET( &mc_curr, op );
1074                 }
1075         }
1076
1077         /* Explicit Bind requests always get their own conn */
1078         if ( sendok & LDAP_BACK_BINDING ) {
1079                 mc_curr.mc_conn = op->o_conn;
1080
1081         } else {
1082                 /* Searches for a metaconn in the avl tree */
1083 retry_lock:;
1084                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1085                 if ( LDAP_BACK_PCONN_ISPRIV( &mc_curr ) ) {
1086                         /* lookup a conn that's not binding */
1087                         LDAP_TAILQ_FOREACH( mc,
1088                                 &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv,
1089                                 mc_q )
1090                         {
1091                                 if ( !LDAP_BACK_CONN_BINDING( mc ) && mc->mc_refcnt == 0 ) {
1092                                         break;
1093                                 }
1094                         }
1095
1096                         if ( mc != NULL ) {
1097                                 if ( mc != LDAP_TAILQ_LAST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1098                                         metaconn_t, mc_q ) )
1099                                 {
1100                                         LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1101                                                 mc, mc_q );
1102                                         LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1103                                         LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1104                                                 mc, mc_q );
1105                                 }
1106
1107                         } else if ( !LDAP_BACK_USE_TEMPORARIES( mi )
1108                                 && mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_num == mi->mi_conn_priv_max )
1109                         {
1110                                 mc = LDAP_TAILQ_FIRST( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( &mc_curr ) ].mic_priv );
1111                         }
1112                         
1113
1114                 } else {
1115                         mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree, 
1116                                 (caddr_t)&mc_curr, meta_back_conndn_cmp );
1117                 }
1118
1119                 if ( mc ) {
1120                         /* catch taint errors */
1121                         assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1122
1123                         /* Don't reuse connections while they're still binding
1124                          * NOTE: only makes sense for binds */
1125                         if ( LDAP_BACK_CONN_BINDING( mc ) ) {
1126                                 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1127                                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1128
1129                                         ldap_pvt_thread_yield();
1130                                         goto retry_lock;
1131                                 }
1132
1133                                 /* release conn, and create a temporary */
1134                                 mc = NULL;
1135
1136                         } else {
1137                                 if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
1138                                         || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
1139                                 {
1140 #if META_BACK_PRINT_CONNTREE > 0
1141                                         meta_back_print_conntree( mi,
1142                                                 ">>> meta_back_getconn(expired)" );
1143 #endif /* META_BACK_PRINT_CONNTREE */
1144
1145                                         /* don't let anyone else use this expired connection */
1146                                         if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1147                                                 if ( mc->mc_q.tqe_prev != NULL ) {
1148                                                         assert( LDAP_BACK_CONN_CACHED( mc ) );
1149                                                         assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
1150                                                         LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv,
1151                                                                 mc, mc_q );
1152                                                         mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1153                                                         LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1154
1155                                                 } else {
1156                                                         assert( !LDAP_BACK_CONN_CACHED( mc ) );
1157                                                 }
1158
1159                                         } else {
1160                                                 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1161                                                         (caddr_t)mc, meta_back_conndnmc_cmp );
1162                                         }
1163
1164 #if META_BACK_PRINT_CONNTREE > 0
1165                                         meta_back_print_conntree( mi,
1166                                                 "<<< meta_back_getconn(expired)" );
1167 #endif /* META_BACK_PRINT_CONNTREE */
1168                                         LDAP_BACK_CONN_TAINTED_SET( mc );
1169                                         LDAP_BACK_CONN_CACHED_CLEAR( mc );
1170
1171                                         if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1172                                                 char buf[STRLENOF("4294967295U") + 1] = { 0 };
1173                                                 ldap_back_conn2str( (ldapconn_t *)mc, buf, sizeof(buf) );
1174
1175                                                 Debug( LDAP_DEBUG_TRACE,
1176                                                         "%s meta_back_getconn: mc=%p conn=%s expired (tainted).\n",
1177                                                         op->o_log_prefix, (void *)mc, buf );
1178                                         }
1179                                 }
1180
1181                                 mc->mc_refcnt++;
1182                         }
1183                 }
1184                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1185         }
1186
1187         switch ( op->o_tag ) {
1188         case LDAP_REQ_ADD:
1189                 /* if we go to selection, the entry must not exist,
1190                  * and we must be able to resolve the parent */
1191                 dn_type = META_DNTYPE_PARENT;
1192                 dnParent( &ndn, &pndn );
1193                 break;
1194
1195         case LDAP_REQ_MODRDN:
1196                 /* if nnewSuperior is not NULL, it must resolve
1197                  * to the same candidate as the req_ndn */
1198                 if ( op->orr_nnewSup ) {
1199                         dn_type = META_DNTYPE_NEWPARENT;
1200                 }
1201                 break;
1202
1203         case LDAP_REQ_BIND:
1204                 /* if bound as rootdn, the backend must bind to all targets
1205                  * with the administrative identity
1206                  * (unless pseoudoroot-bind-defer is TRUE) */
1207                 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
1208                         op_type = META_OP_REQUIRE_ALL;
1209                 }
1210                 break;
1211
1212         case LDAP_REQ_COMPARE:
1213         case LDAP_REQ_DELETE:
1214         case LDAP_REQ_MODIFY:
1215                 /* just a unique candidate */
1216                 break;
1217
1218         case LDAP_REQ_SEARCH:
1219                 /* allow multiple candidates for the searchBase */
1220                 op_type = META_OP_ALLOW_MULTIPLE;
1221                 break;
1222
1223         default:
1224                 /* right now, just break (exop?) */
1225                 break;
1226         }
1227
1228         /*
1229          * require all connections ...
1230          */
1231         if ( op_type == META_OP_REQUIRE_ALL ) {
1232
1233                 /* Looks like we didn't get a bind. Open a new session... */
1234                 if ( mc == NULL ) {
1235                         assert( new_conn == 0 );
1236                         mc = metaconn_alloc( op );
1237                         mc->mc_conn = mc_curr.mc_conn;
1238                         ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1239                         new_conn = 1;
1240                         if ( sendok & LDAP_BACK_BINDING ) {
1241                                 LDAP_BACK_CONN_BINDING_SET( mc );
1242                         }
1243                         if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1244                                 LDAP_BACK_CONN_ISPRIV_SET( mc );
1245
1246                         } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1247                                 LDAP_BACK_CONN_ISANON_SET( mc );
1248                         }
1249
1250                 } else if ( 0 ) {
1251                         /* TODO: if any of the connections is binding,
1252                          * release mc and create a new one */
1253                 }
1254
1255                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1256                         /*
1257                          * The target is activated; if needed, it is
1258                          * also init'd
1259                          */
1260                         candidates[ i ].sr_err = meta_back_init_one_conn( op,
1261                                 rs, mc, i, LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1262                                 LDAP_BACK_DONTSEND, !new_conn );
1263                         if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1264                                 if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) {
1265                                         LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] );
1266                                 }
1267                                 META_CANDIDATE_SET( &candidates[ i ] );
1268                                 ncandidates++;
1269         
1270                         } else {
1271                                 
1272                                 /*
1273                                  * FIXME: in case one target cannot
1274                                  * be init'd, should the other ones
1275                                  * be tried?
1276                                  */
1277                                 META_CANDIDATE_RESET( &candidates[ i ] );
1278                                 err = candidates[ i ].sr_err;
1279                                 continue;
1280                         }
1281                 }
1282
1283                 if ( ncandidates == 0 ) {
1284                         if ( new_conn ) {
1285                                 mc->mc_refcnt = 0;
1286                                 meta_back_conn_free( mc );
1287
1288                         } else {
1289                                 meta_back_release_conn( mi, mc );
1290                         }
1291
1292                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
1293                         rs->sr_text = "Unable to select valid candidates";
1294
1295                         if ( sendok & LDAP_BACK_SENDERR ) {
1296                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1297                                         rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1298                                 }
1299                                 send_ldap_result( op, rs );
1300                                 rs->sr_matched = NULL;
1301                         }
1302
1303                         return NULL;
1304                 }
1305
1306                 goto done;
1307         }
1308         
1309         /*
1310          * looks in cache, if any
1311          */
1312         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1313                 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
1314         }
1315
1316         if ( op_type == META_OP_REQUIRE_SINGLE ) {
1317                 metatarget_t            *mt = NULL;
1318                 metasingleconn_t        *msc = NULL;
1319
1320                 int                     j;
1321
1322                 for ( j = 0; j < mi->mi_ntargets; j++ ) {
1323                         META_CANDIDATE_RESET( &candidates[ j ] );
1324                 }
1325
1326                 /*
1327                  * tries to get a unique candidate
1328                  * (takes care of default target)
1329                  */
1330                 if ( i == META_TARGET_NONE ) {
1331                         i = meta_back_get_candidate( op, rs, &ndn );
1332
1333                         if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1334                                 i = meta_back_get_candidate( op, rs, &pndn );
1335                         }
1336         
1337                         if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1338                                 if ( mc != NULL ) {
1339                                         meta_back_release_conn( mi, mc );
1340                                 }
1341
1342                                 if ( sendok & LDAP_BACK_SENDERR ) {
1343                                         if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1344                                                 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1345                                         }
1346                                         send_ldap_result( op, rs );
1347                                         rs->sr_matched = NULL;
1348                                 }
1349                         
1350                                 return NULL;
1351                         }
1352                 }
1353
1354                 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1355                 {
1356                         if ( mc != NULL ) {
1357                                 meta_back_release_conn( mi, mc );
1358                         }
1359
1360                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1361                         rs->sr_text = "Cross-target rename not supported";
1362                         if ( sendok & LDAP_BACK_SENDERR ) {
1363                                 send_ldap_result( op, rs );
1364                         }
1365
1366                         return NULL;
1367                 }
1368
1369                 Debug( LDAP_DEBUG_TRACE,
1370         "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1371                                 i, op->o_req_ndn.bv_val, 0 );
1372
1373                 if ( mc == NULL ) {
1374                         /* Retries searching for a metaconn in the avl tree
1375                          * the reason is that the connection might have been
1376                          * created by meta_back_get_candidate() */
1377                         if ( !( sendok & LDAP_BACK_BINDING ) ) {
1378 retry_lock2:;
1379                                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1380                                 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree, 
1381                                         (caddr_t)&mc_curr, meta_back_conndn_cmp );
1382                                 if ( mc != NULL ) {
1383                                         /* catch taint errors */
1384                                         assert( !LDAP_BACK_CONN_TAINTED( mc ) );
1385
1386                                         /* Don't reuse connections while they're still binding */
1387                                         if ( META_BACK_CONN_CREATING( &mc->mc_conns[ i ] )
1388                                                 || LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) )
1389                                         {
1390                                                 if ( !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1391                                                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1392                                                         ldap_pvt_thread_yield();
1393                                                         goto retry_lock2;
1394                                                 }
1395
1396                                                 mc = NULL;
1397
1398                                         } else {
1399                                                 mc->mc_refcnt++;
1400                                         }
1401                                 }
1402                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1403                         }
1404
1405                         /* Looks like we didn't get a bind. Open a new session... */
1406                         if ( mc == NULL ) {
1407                                 assert( new_conn == 0 );
1408                                 mc = metaconn_alloc( op );
1409                                 mc->mc_conn = mc_curr.mc_conn;
1410                                 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1411                                 new_conn = 1;
1412                                 if ( sendok & LDAP_BACK_BINDING ) {
1413                                         LDAP_BACK_CONN_BINDING_SET( mc );
1414                                 }
1415                                 if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1416                                         LDAP_BACK_CONN_ISPRIV_SET( mc );
1417
1418                                 } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1419                                         LDAP_BACK_CONN_ISANON_SET( mc );
1420                                 }
1421                         }
1422                 }
1423
1424                 /*
1425                  * Clear all other candidates
1426                  */
1427                 ( void )meta_clear_unused_candidates( op, i );
1428
1429                 mt = mi->mi_targets[ i ];
1430                 msc = &mc->mc_conns[ i ];
1431
1432                 /*
1433                  * The target is activated; if needed, it is
1434                  * also init'd. In case of error, meta_back_init_one_conn
1435                  * sends the appropriate result.
1436                  */
1437                 err = meta_back_init_one_conn( op, rs, mc, i,
1438                         LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok, !new_conn );
1439                 if ( err != LDAP_SUCCESS ) {
1440                         /*
1441                          * FIXME: in case one target cannot
1442                          * be init'd, should the other ones
1443                          * be tried?
1444                          */
1445                         META_CANDIDATE_RESET( &candidates[ i ] );
1446                         if ( new_conn ) {
1447                                 mc->mc_refcnt = 0;
1448                                 meta_back_conn_free( mc );
1449
1450                         } else {
1451                                 meta_back_release_conn( mi, mc );
1452                         }
1453                         return NULL;
1454                 }
1455
1456                 if ( new_conn && ( sendok & LDAP_BACK_BINDING ) ) {
1457                         LDAP_BACK_CONN_BINDING_SET( &mc->mc_conns[ i ] );
1458                 }
1459
1460                 candidates[ i ].sr_err = LDAP_SUCCESS;
1461                 META_CANDIDATE_SET( &candidates[ i ] );
1462                 ncandidates++;
1463
1464                 if ( candidate ) {
1465                         *candidate = i;
1466                 }
1467
1468         /*
1469          * if no unique candidate ...
1470          */
1471         } else {
1472
1473                 /* Looks like we didn't get a bind. Open a new session... */
1474                 if ( mc == NULL ) {
1475                         assert( new_conn == 0 );
1476                         mc = metaconn_alloc( op );
1477                         mc->mc_conn = mc_curr.mc_conn;
1478                         ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1479                         new_conn = 1;
1480                         if ( LDAP_BACK_CONN_ISPRIV( &mc_curr ) ) {
1481                                 LDAP_BACK_CONN_ISPRIV_SET( mc );
1482
1483                         } else if ( LDAP_BACK_CONN_ISANON( &mc_curr ) ) {
1484                                 LDAP_BACK_CONN_ISANON_SET( mc );
1485                         }
1486                 }
1487
1488                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1489                         metatarget_t            *mt = mi->mi_targets[ i ];
1490
1491                         META_CANDIDATE_RESET( &candidates[ i ] );
1492
1493                         if ( i == cached 
1494                                 || meta_back_is_candidate( mt, &op->o_req_ndn,
1495                                         LDAP_SCOPE_SUBTREE ) )
1496                         {
1497
1498                                 /*
1499                                  * The target is activated; if needed, it is
1500                                  * also init'd
1501                                  */
1502                                 int lerr = meta_back_init_one_conn( op, rs, mc, i,
1503                                         LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1504                                         LDAP_BACK_DONTSEND, !new_conn );
1505                                 candidates[ i ].sr_err = lerr;
1506                                 if ( lerr == LDAP_SUCCESS ) {
1507                                         META_CANDIDATE_SET( &candidates[ i ] );
1508                                         ncandidates++;
1509
1510                                         Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1511                                                 op->o_log_prefix, i, 0 );
1512
1513                                 } else if ( lerr == LDAP_UNAVAILABLE && !META_BACK_ONERR_STOP( mi ) ) {
1514                                         META_CANDIDATE_SET( &candidates[ i ] );
1515
1516                                         Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] %s\n",
1517                                                 op->o_log_prefix, i,
1518                                                 mt->mt_isquarantined != LDAP_BACK_FQ_NO ? "quarantined" : "unavailable" );
1519
1520                                 } else {
1521                                 
1522                                         /*
1523                                          * FIXME: in case one target cannot
1524                                          * be init'd, should the other ones
1525                                          * be tried?
1526                                          */
1527                                         if ( new_conn ) {
1528                                                 ( void )meta_clear_one_candidate( op, mc, i );
1529                                         }
1530                                         /* leave the target candidate, but record the error for later use */
1531                                         err = lerr;
1532
1533                                         if ( lerr == LDAP_UNAVAILABLE && mt->mt_isquarantined != LDAP_BACK_FQ_NO ) {
1534                                                 Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d] quarantined err=%d\n",
1535                                                         op->o_log_prefix, i, lerr );
1536
1537                                         } else {
1538                                                 Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed err=%d\n",
1539                                                         op->o_log_prefix, i, lerr );
1540                                         }
1541
1542                                         if ( META_BACK_ONERR_STOP( mi ) ) {
1543                                                 if ( sendok & LDAP_BACK_SENDERR ) {
1544                                                         send_ldap_result( op, rs );
1545                                                 }
1546                                                 if ( new_conn ) {
1547                                                         mc->mc_refcnt = 0;
1548                                                         meta_back_conn_free( mc );
1549
1550                                                 } else {
1551                                                         meta_back_release_conn( mi, mc );
1552                                                 }
1553
1554                                                 return NULL;
1555                                         }
1556
1557                                         continue;
1558                                 }
1559
1560                         } else {
1561                                 if ( new_conn ) {
1562                                         ( void )meta_clear_one_candidate( op, mc, i );
1563                                 }
1564                         }
1565                 }
1566
1567                 if ( ncandidates == 0 ) {
1568                         if ( new_conn ) {
1569                                 mc->mc_refcnt = 0;
1570                                 meta_back_conn_free( mc );
1571
1572                         } else {
1573                                 meta_back_release_conn( mi, mc );
1574                         }
1575
1576                         if ( rs->sr_err == LDAP_SUCCESS ) {
1577                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1578                                 rs->sr_text = "Unable to select valid candidates";
1579                         }
1580
1581                         if ( sendok & LDAP_BACK_SENDERR ) {
1582                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1583                                         rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1584                                 }
1585                                 send_ldap_result( op, rs );
1586                                 rs->sr_matched = NULL;
1587                         }
1588
1589                         return NULL;
1590                 }
1591         }
1592
1593 done:;
1594         /* clear out meta_back_init_one_conn non-fatal errors */
1595         rs->sr_err = LDAP_SUCCESS;
1596         rs->sr_text = NULL;
1597
1598         /* touch the timestamp */
1599         if ( mi->mi_idle_timeout != 0 ) {
1600                 mc->mc_time = op->o_time;
1601         }
1602
1603         if ( new_conn ) {
1604                 if ( mi->mi_conn_ttl ) {
1605                         mc->mc_create_time = op->o_time;
1606                 }
1607
1608                 /*
1609                  * Inserts the newly created metaconn in the avl tree
1610                  */
1611                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1612 #if META_BACK_PRINT_CONNTREE > 0
1613                 meta_back_print_conntree( mi, ">>> meta_back_getconn" );
1614 #endif /* META_BACK_PRINT_CONNTREE */
1615
1616                 err = 0;
1617                 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1618                         if ( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num < mi->mi_conn_priv_max ) {
1619                                 LDAP_TAILQ_INSERT_TAIL( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1620                                 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num++;
1621                                 LDAP_BACK_CONN_CACHED_SET( mc );
1622
1623                         } else {
1624                                 LDAP_BACK_CONN_TAINTED_SET( mc );
1625                         }
1626                         rs->sr_err = 0;
1627
1628                 } else if ( !( sendok & LDAP_BACK_BINDING ) ) {
1629                         err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1630                                 meta_back_conndn_cmp, meta_back_conndn_dup );
1631                         LDAP_BACK_CONN_CACHED_SET( mc );
1632                 }
1633
1634 #if META_BACK_PRINT_CONNTREE > 0
1635                 meta_back_print_conntree( mi, "<<< meta_back_getconn" );
1636 #endif /* META_BACK_PRINT_CONNTREE */
1637                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1638
1639                 if ( !LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1640                         /*
1641                          * Err could be -1 in case a duplicate metaconn is inserted
1642                          */
1643                         switch ( err ) {
1644                         case 0:
1645                                 break;
1646
1647                         case -1:
1648                                 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1649                                 /* duplicate: free and try to get the newly created one */
1650                                 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( mi ) ) {
1651                                         mc->mc_refcnt = 0;      
1652                                         meta_back_conn_free( mc );
1653         
1654                                         new_conn = 0;
1655                                         goto retry_lock;
1656                                 }
1657
1658                                 LDAP_BACK_CONN_TAINTED_SET( mc );
1659                                 break;
1660
1661                         default:
1662                                 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1663                                 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1664                                         char buf[STRLENOF("4294967295U") + 1] = { 0 };
1665                                         ldap_back_conn2str( (ldapconn_t *)mc, buf, sizeof(buf) );
1666
1667                                         Debug( LDAP_DEBUG_ANY,
1668                                                 "%s meta_back_getconn: candidates=%d conn=%s insert failed\n",
1669                                                 op->o_log_prefix, ncandidates, buf );
1670                                 }
1671         
1672                                 mc->mc_refcnt = 0;      
1673                                 meta_back_conn_free( mc );
1674
1675                                 rs->sr_err = LDAP_OTHER;
1676                                 rs->sr_text = "Proxy bind collision";
1677                                 if ( sendok & LDAP_BACK_SENDERR ) {
1678                                         send_ldap_result( op, rs );
1679                                 }
1680                                 return NULL;
1681                         }
1682                 }
1683
1684                 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1685                         char buf[STRLENOF("4294967295U") + 1] = { 0 };
1686                         ldap_back_conn2str( (ldapconn_t *)mc, buf, sizeof(buf) );
1687
1688                         Debug( LDAP_DEBUG_TRACE,
1689                                 "%s meta_back_getconn: candidates=%d conn=%s inserted\n",
1690                                 op->o_log_prefix, ncandidates, buf );
1691                 }
1692
1693         } else {
1694                 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1695                         char buf[STRLENOF("4294967295U") + 1] = { 0 };
1696                         ldap_back_conn2str( (ldapconn_t *)mc, buf, sizeof(buf) );
1697
1698                         Debug( LDAP_DEBUG_TRACE,
1699                                 "%s meta_back_getconn: candidates=%d conn=%s fetched\n",
1700                                 op->o_log_prefix, ncandidates, buf );
1701                 }
1702         }
1703
1704         return mc;
1705 }
1706
1707 void
1708 meta_back_release_conn_lock(
1709         metainfo_t              *mi,
1710         metaconn_t              *mc,
1711         int                     dolock )
1712 {
1713         assert( mc != NULL );
1714
1715         if ( dolock ) {
1716                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1717         }
1718         assert( mc->mc_refcnt > 0 );
1719         mc->mc_refcnt--;
1720         /* NOTE: the connection is removed if either it is tainted
1721          * or if it is shared and no one else is using it.  This needs
1722          * to occur because for intrinsic reasons cached connections
1723          * that are not privileged would live forever and pollute
1724          * the connection space (and eat up resources).  Maybe this
1725          * should be configurable... */
1726         if ( LDAP_BACK_CONN_TAINTED( mc ) || !LDAP_BACK_CONN_CACHED( mc ) ) {
1727 #if META_BACK_PRINT_CONNTREE > 0
1728                 meta_back_print_conntree( mi, ">>> meta_back_release_conn" );
1729 #endif /* META_BACK_PRINT_CONNTREE */
1730
1731                 if ( LDAP_BACK_PCONN_ISPRIV( mc ) ) {
1732                         if ( mc->mc_q.tqe_prev != NULL ) {
1733                                 assert( LDAP_BACK_CONN_CACHED( mc ) );
1734                                 assert( mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num > 0 );
1735                                 LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_priv, mc, mc_q );
1736                                 mi->mi_conn_priv[ LDAP_BACK_CONN2PRIV( mc ) ].mic_num--;
1737                                 LDAP_TAILQ_ENTRY_INIT( mc, mc_q );
1738
1739                         } else {
1740                                 assert( !LDAP_BACK_CONN_CACHED( mc ) );
1741                         }
1742
1743                 } else if ( LDAP_BACK_CONN_CACHED( mc ) ) {
1744                         metaconn_t      *tmpmc;
1745
1746                         tmpmc = avl_delete( &mi->mi_conninfo.lai_tree,
1747                                 ( caddr_t )mc, meta_back_conndnmc_cmp );
1748
1749                         /* Overparanoid, but useful... */
1750                         assert( tmpmc == NULL || tmpmc == mc );
1751                 }
1752
1753                 LDAP_BACK_CONN_CACHED_CLEAR( mc );
1754
1755 #if META_BACK_PRINT_CONNTREE > 0
1756                 meta_back_print_conntree( mi, "<<< meta_back_release_conn" );
1757 #endif /* META_BACK_PRINT_CONNTREE */
1758
1759                 if ( mc->mc_refcnt == 0 ) {
1760                         meta_back_conn_free( mc );
1761                         mc = NULL;
1762                 }
1763         }
1764
1765         if ( mc != NULL && LDAP_BACK_CONN_BINDING( mc ) ) {
1766                 LDAP_BACK_CONN_BINDING_CLEAR( mc );
1767         }
1768
1769         if ( dolock ) {
1770                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1771         }
1772 }
1773
1774 void
1775 meta_back_quarantine(
1776         Operation       *op,
1777         SlapReply       *rs,
1778         int             candidate )
1779 {
1780         metainfo_t              *mi = (metainfo_t *)op->o_bd->be_private;
1781         metatarget_t            *mt = mi->mi_targets[ candidate ];
1782
1783         slap_retry_info_t       *ri = &mt->mt_quarantine;
1784
1785         ldap_pvt_thread_mutex_lock( &mt->mt_quarantine_mutex );
1786
1787         if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1788                 time_t  new_last = slap_get_time();
1789
1790                 switch ( mt->mt_isquarantined ) {
1791                 case LDAP_BACK_FQ_NO:
1792                         if ( ri->ri_last == new_last ) {
1793                                 goto done;
1794                         }
1795
1796                         Debug( LDAP_DEBUG_ANY,
1797                                 "%s meta_back_quarantine[%d]: enter.\n",
1798                                 op->o_log_prefix, candidate, 0 );
1799
1800                         ri->ri_idx = 0;
1801                         ri->ri_count = 0;
1802                         break;
1803
1804                 case LDAP_BACK_FQ_RETRYING:
1805                         if ( LogTest( LDAP_DEBUG_ANY ) ) {
1806                                 char    buf[ SLAP_TEXT_BUFLEN ];
1807
1808                                 snprintf( buf, sizeof( buf ),
1809                                         "meta_back_quarantine[%d]: block #%d try #%d failed",
1810                                         candidate, ri->ri_idx, ri->ri_count );
1811                                 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1812                                         op->o_log_prefix, buf, 0 );
1813                         }
1814
1815                         ++ri->ri_count;
1816                         if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1817                                 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1818                         {
1819                                 ri->ri_count = 0;
1820                                 ++ri->ri_idx;
1821                         }
1822                         break;
1823
1824                 default:
1825                         break;
1826                 }
1827
1828                 mt->mt_isquarantined = LDAP_BACK_FQ_YES;
1829                 ri->ri_last = new_last;
1830
1831         } else if ( mt->mt_isquarantined == LDAP_BACK_FQ_RETRYING ) {
1832                 Debug( LDAP_DEBUG_ANY,
1833                         "%s meta_back_quarantine[%d]: exit.\n",
1834                         op->o_log_prefix, candidate, 0 );
1835
1836                 if ( mi->mi_quarantine_f ) {
1837                         (void)mi->mi_quarantine_f( mi, candidate,
1838                                 mi->mi_quarantine_p );
1839                 }
1840
1841                 ri->ri_count = 0;
1842                 ri->ri_idx = 0;
1843                 mt->mt_isquarantined = LDAP_BACK_FQ_NO;
1844         }
1845
1846 done:;
1847         ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );
1848 }
1849