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