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