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