]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/conn.c
59cd94962a213af5f38187e592737e69d3a116f6
[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_s( msc->msc_ld, NULL, NULL );
406                         goto error_return;
407                 }
408         }
409 #endif /* HAVE_TLS */
410
411         /*
412          * Set the network timeout if set
413          */
414         if ( mt->mt_network_timeout != 0 ) {
415                 struct timeval  network_timeout;
416
417                 network_timeout.tv_usec = 0;
418                 network_timeout.tv_sec = mt->mt_network_timeout;
419
420                 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
421                                 (void *)&network_timeout );
422         }
423
424         /*
425          * If the connection DN is not null, an attempt to rewrite it is made
426          */
427
428         if ( ispriv ) {
429                 if ( !BER_BVISNULL( &mt->mt_pseudorootdn ) ) {
430                         ber_dupbv( &msc->msc_bound_ndn, &mt->mt_pseudorootdn );
431                         if ( !BER_BVISNULL( &mt->mt_pseudorootpw ) ) {
432                                 ber_dupbv( &msc->msc_cred, &mt->mt_pseudorootpw );
433                         }
434
435                 } else {
436                         ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
437                 }
438
439                 LDAP_BACK_CONN_ISPRIV_SET( msc );
440
441         } else {
442                 BER_BVZERO( &msc->msc_cred );
443                 BER_BVZERO( &msc->msc_bound_ndn );
444                 if ( !BER_BVISEMPTY( &op->o_ndn )
445                         && SLAP_IS_AUTHZ_BACKEND( op )
446                         && isauthz )
447                 {
448                         dc.target = mt;
449                         dc.conn = op->o_conn;
450                         dc.rs = rs;
451                         dc.ctx = "bindDN";
452                 
453                         /*
454                          * Rewrite the bind dn if needed
455                          */
456                         if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
457                                                 &msc->msc_bound_ndn ) )
458                         {
459                                 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
460                                 goto error_return;
461                         }
462                         
463                         /* copy the DN idf needed */
464                         if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
465                                 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
466                         }
467
468                 } else {
469                         ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
470                 }
471         }
472
473         assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
474
475 error_return:;
476         if ( rs->sr_err == LDAP_SUCCESS ) {
477                 /*
478                  * Sets a cookie for the rewrite session
479                  */
480                 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
481
482         } else {
483                 rs->sr_err = slap_map_api2result( rs );
484                 if ( sendok & LDAP_BACK_SENDERR ) {
485                         send_ldap_result( op, rs );
486                         rs->sr_text = NULL;
487                 }
488         }
489
490         return rs->sr_err;
491 }
492
493 /*
494  * meta_back_retry
495  * 
496  * Retries one connection
497  */
498 int
499 meta_back_retry(
500         Operation               *op,
501         SlapReply               *rs,
502         metaconn_t              **mcp,
503         int                     candidate,
504         ldap_back_send_t        sendok )
505 {
506         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
507         metatarget_t            *mt = mi->mi_targets[ candidate ];
508         metaconn_t              *mc = *mcp;
509         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
510         int                     rc = LDAP_UNAVAILABLE,
511                                 binding = LDAP_BACK_CONN_BINDING( msc );
512
513         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
514
515         assert( mc->mc_refcnt > 0 );
516         if ( mc->mc_refcnt == 1 ) {
517                 if ( LogTest( LDAP_DEBUG_ANY ) ) {
518                         char    buf[ SLAP_TEXT_BUFLEN ];
519
520                         ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
521                         snprintf( buf, sizeof( buf ),
522                                 "retrying URI=\"%s\" DN=\"%s\"",
523                                 mt->mt_uri,
524                                 BER_BVISNULL( &msc->msc_bound_ndn ) ?
525                                         "" : msc->msc_bound_ndn.bv_val );
526                         ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
527
528                         Debug( LDAP_DEBUG_ANY,
529                                 "%s meta_back_retry[%d]: %s.\n",
530                                 op->o_log_prefix, candidate, buf );
531                 }
532
533                 meta_clear_one_candidate( msc );
534                 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
535
536                 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
537
538                 /* mc here must be the regular mc, reset and ready for init */
539                 rc = meta_back_init_one_conn( op, rs, mt, mc, candidate,
540                         LDAP_BACK_CONN_ISPRIV( mc ), sendok );
541                 if ( binding ) {
542                         LDAP_BACK_CONN_BINDING_SET( msc );
543                 }
544
545                 if ( rc == LDAP_SUCCESS ) {
546                         rc = meta_back_single_dobind( op, rs, mcp, candidate,
547                                 sendok, mt->mt_nretries, 0 );
548
549                         Debug( LDAP_DEBUG_ANY,
550                                 "%s meta_back_retry[%d]: "
551                                 "meta_back_single_dobind=%d\n",
552                                 op->o_log_prefix, candidate, rc );
553                         if ( rc == LDAP_SUCCESS ) {
554                                 if ( be_isroot( op )  ) {
555                                         LDAP_BACK_CONN_ISBOUND_SET( msc );
556                                 } else {
557                                         LDAP_BACK_CONN_ISANON_SET( msc );
558                                 }
559                         }
560                 }
561         }
562
563         if ( rc != LDAP_SUCCESS ) {
564                 SlapReply               *candidates = meta_back_candidates_get( op );
565
566                 candidates[ candidate ].sr_err = rc;
567
568                 if ( *mcp != NULL ) {
569                         if ( binding ) {
570                                 LDAP_BACK_CONN_BINDING_CLEAR( msc );
571                         }
572                         LDAP_BACK_CONN_TAINTED_SET( mc );
573                         /* only release if mandatory; otherwise
574                          * let the caller do what's best before
575                          * releasing */
576                         if ( META_BACK_ONERR_STOP( mi ) ) {
577                                 meta_back_release_conn_lock( op, mc, 0 );
578                                 *mcp = NULL;
579                         }
580                 }
581
582                 if ( sendok ) {
583                         rs->sr_err = rc;
584                         rs->sr_text = NULL;
585                         send_ldap_result( op, rs );
586                 }
587         }
588
589         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
590
591         return rc == LDAP_SUCCESS ? 1 : 0;
592 }
593
594 /*
595  * callback for unique candidate selection
596  */
597 static int
598 meta_back_conn_cb( Operation *op, SlapReply *rs )
599 {
600         assert( op->o_tag == LDAP_REQ_SEARCH );
601
602         switch ( rs->sr_type ) {
603         case REP_SEARCH:
604                 ((long *)op->o_callback->sc_private)[0] = (long)op->o_private;
605                 break;
606
607         case REP_SEARCHREF:
608         case REP_RESULT:
609                 break;
610
611         default:
612                 return rs->sr_err;
613         }
614
615         return 0;
616 }
617
618
619 static int
620 meta_back_get_candidate(
621         Operation       *op,
622         SlapReply       *rs,
623         struct berval   *ndn )
624 {
625         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
626         long            candidate;
627
628         /*
629          * tries to get a unique candidate
630          * (takes care of default target)
631          */
632         candidate = meta_back_select_unique_candidate( mi, ndn );
633
634         /*
635          * if any is found, inits the connection
636          */
637         if ( candidate == META_TARGET_NONE ) {
638                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
639                 rs->sr_text = "no suitable candidate target found";
640
641         } else if ( candidate == META_TARGET_MULTIPLE ) {
642                 Filter          f = { 0 };
643                 Operation       op2 = *op;
644                 SlapReply       rs2 = { 0 };
645                 slap_callback   cb2 = { 0 };
646                 int             rc;
647
648                 /* try to get a unique match for the request ndn
649                  * among the multiple candidates available */
650                 op2.o_tag = LDAP_REQ_SEARCH;
651                 op2.o_req_dn = *ndn;
652                 op2.o_req_ndn = *ndn;
653                 op2.ors_scope = LDAP_SCOPE_BASE;
654                 op2.ors_deref = LDAP_DEREF_NEVER;
655                 op2.ors_attrs = slap_anlist_no_attrs;
656                 op2.ors_attrsonly = 0;
657                 op2.ors_limit = NULL;
658                 op2.ors_slimit = 1;
659                 op2.ors_tlimit = SLAP_NO_LIMIT;
660
661                 f.f_choice = LDAP_FILTER_PRESENT;
662                 f.f_desc = slap_schema.si_ad_objectClass;
663                 op2.ors_filter = &f;
664                 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
665
666                 op2.o_callback = &cb2;
667                 cb2.sc_response = meta_back_conn_cb;
668                 cb2.sc_private = (void *)&candidate;
669
670                 rc = op->o_bd->be_search( &op2, &rs2 );
671
672                 switch ( rs2.sr_err ) {
673                 case LDAP_SUCCESS:
674                 default:
675                         rs->sr_err = rs2.sr_err;
676                         break;
677
678                 case LDAP_SIZELIMIT_EXCEEDED:
679                         /* if multiple candidates can serve the operation,
680                          * and a default target is defined, and it is
681                          * a candidate, try using it (FIXME: YMMV) */
682                         if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
683                                 && meta_back_is_candidate( &mi->mi_targets[ mi->mi_defaulttarget ]->mt_nsuffix,
684                                                 mi->mi_targets[ mi->mi_defaulttarget ]->mt_scope,
685                                                 mi->mi_targets[ mi->mi_defaulttarget ]->mt_subtree_exclude,
686                                                 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
687                         {
688                                 candidate = mi->mi_defaulttarget;
689                                 rs->sr_err = LDAP_SUCCESS;
690                                 rs->sr_text = NULL;
691
692                         } else {
693                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
694                                 rs->sr_text = "cannot select unique candidate target";
695                         }
696                         break;
697                 }
698
699         } else {
700                 rs->sr_err = LDAP_SUCCESS;
701         }
702
703         return candidate;
704 }
705
706 static void     *meta_back_candidates_dummy;
707
708 static void
709 meta_back_candidates_keyfree(
710         void            *key,
711         void            *data )
712 {
713         metacandidates_t        *mc = (metacandidates_t *)data;
714
715         ber_memfree_x( mc->mc_candidates, NULL );
716         ber_memfree_x( data, NULL );
717 }
718
719 SlapReply *
720 meta_back_candidates_get( Operation *op )
721 {
722         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
723         metacandidates_t        *mc;
724
725         if ( op->o_threadctx ) {
726                 void            *data = NULL;
727
728                 ldap_pvt_thread_pool_getkey( op->o_threadctx,
729                                 &meta_back_candidates_dummy, &data, NULL );
730                 mc = (metacandidates_t *)data;
731
732         } else {
733                 mc = mi->mi_candidates;
734         }
735
736         if ( mc == NULL ) {
737                 mc = ch_calloc( sizeof( metacandidates_t ), 1 );
738                 mc->mc_ntargets = mi->mi_ntargets;
739                 mc->mc_candidates = ch_calloc( sizeof( SlapReply ), mc->mc_ntargets );
740                 if ( op->o_threadctx ) {
741                         void            *data = NULL;
742
743                         data = (void *)mc;
744                         ldap_pvt_thread_pool_setkey( op->o_threadctx,
745                                         &meta_back_candidates_dummy, data,
746                                         meta_back_candidates_keyfree );
747
748                 } else {
749                         mi->mi_candidates = mc;
750                 }
751
752         } else if ( mc->mc_ntargets < mi->mi_ntargets ) {
753                 /* NOTE: in the future, may want to allow back-config
754                  * to add/remove targets from back-meta... */
755                 mc->mc_ntargets = mi->mi_ntargets;
756                 mc->mc_candidates = ch_realloc( mc->mc_candidates,
757                                 sizeof( SlapReply ) * mc->mc_ntargets );
758         }
759
760         return mc->mc_candidates;
761 }
762
763 /*
764  * meta_back_getconn
765  * 
766  * Prepares the connection structure
767  * 
768  * RATIONALE:
769  *
770  * - determine what DN is being requested:
771  *
772  *      op      requires candidate      checks
773  *
774  *      add     unique                  parent of o_req_ndn
775  *      bind    unique^*[/all]          o_req_ndn [no check]
776  *      compare unique^+                o_req_ndn
777  *      delete  unique                  o_req_ndn
778  *      modify  unique                  o_req_ndn
779  *      search  any                     o_req_ndn
780  *      modrdn  unique[, unique]        o_req_ndn[, orr_nnewSup]
781  *
782  * - for ops that require the candidate to be unique, in case of multiple
783  *   occurrences an internal search with sizeLimit=1 is performed
784  *   if a unique candidate can actually be determined.  If none is found,
785  *   the operation aborts; if multiple are found, the default target
786  *   is used if defined and candidate; otherwise the operation aborts.
787  *
788  * *^note: actually, the bind operation is handled much like a search;
789  *   i.e. the bind is broadcast to all candidate targets.
790  *
791  * +^note: actually, the compare operation is handled much like a search;
792  *   i.e. the compare is broadcast to all candidate targets, while checking
793  *   that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
794  *   returned.
795  */
796 metaconn_t *
797 meta_back_getconn(
798         Operation               *op,
799         SlapReply               *rs,
800         int                     *candidate,
801         ldap_back_send_t        sendok )
802 {
803         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
804         metaconn_t      *mc = NULL,
805                         mc_curr = { 0 };
806         int             cached = META_TARGET_NONE,
807                         i = META_TARGET_NONE,
808                         err = LDAP_SUCCESS,
809                         new_conn = 0,
810                         ncandidates = 0;
811
812
813         meta_op_type    op_type = META_OP_REQUIRE_SINGLE;
814         enum            {
815                 META_DNTYPE_ENTRY,
816                 META_DNTYPE_PARENT,
817                 META_DNTYPE_NEWPARENT
818                         } dn_type = META_DNTYPE_ENTRY;
819         struct berval   ndn = op->o_req_ndn,
820                         pndn;
821
822         SlapReply       *candidates = meta_back_candidates_get( op );
823
824         /* Internal searches are privileged and shared. So is root. */
825         /* FIXME: there seem to be concurrency issues */
826         if ( op->o_do_not_cache || be_isroot( op ) ) {
827                 mc_curr.mc_local_ndn = op->o_bd->be_rootndn;
828                 LDAP_BACK_CONN_ISPRIV_SET( &mc_curr );
829                 mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
830
831         } else {
832                 mc_curr.mc_local_ndn = op->o_ndn;
833
834                 /* Explicit binds must not be shared */
835                 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
836                         mc_curr.mc_conn = op->o_conn;
837         
838                 } else {
839                         mc_curr.mc_conn = LDAP_BACK_PCONN_SET( op );
840                 }
841         }
842
843         /* Explicit Bind requests always get their own conn */
844         if ( !( sendok & LDAP_BACK_BINDING ) ) {
845                 /* Searches for a metaconn in the avl tree */
846 retry_lock:
847                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
848                 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree, 
849                         (caddr_t)&mc_curr, meta_back_conndn_cmp );
850                 if ( mc ) {
851                         if ( ( mi->mi_conn_ttl != 0 && op->o_time > mc->mc_create_time + mi->mi_conn_ttl )
852                                 || ( mi->mi_idle_timeout != 0 && op->o_time > mc->mc_time + mi->mi_idle_timeout ) )
853                         {
854                                 /* don't let anyone else use this expired connection */
855                                 (void)avl_delete( &mi->mi_conninfo.lai_tree,
856                                         (caddr_t)mc, meta_back_conndnmc_cmp );
857                                 LDAP_BACK_CONN_TAINTED_SET( mc );
858
859                                 Debug( LDAP_DEBUG_TRACE, "%s meta_back_getconn: mc=%p conn=%ld expired.\n",
860                                         op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc->mc_conn ) );
861                         }
862
863                         /* Don't reuse connections while they're still binding */
864                         if ( LDAP_BACK_CONN_BINDING( mc ) ) {
865                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
866                                 ldap_pvt_thread_yield();
867                                 goto retry_lock;
868                         }
869
870                         mc->mc_refcnt++;
871                 }
872                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
873         }
874
875         switch ( op->o_tag ) {
876         case LDAP_REQ_ADD:
877                 /* if we go to selection, the entry must not exist,
878                  * and we must be able to resolve the parent */
879                 dn_type = META_DNTYPE_PARENT;
880                 dnParent( &ndn, &pndn );
881                 break;
882
883         case LDAP_REQ_MODRDN:
884                 /* if nnewSuperior is not NULL, it must resolve
885                  * to the same candidate as the req_ndn */
886                 if ( op->orr_nnewSup ) {
887                         dn_type = META_DNTYPE_NEWPARENT;
888                 }
889                 break;
890
891         case LDAP_REQ_BIND:
892                 /* if bound as rootdn, the backend must bind to all targets
893                  * with the administrative identity */
894                 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
895                         op_type = META_OP_REQUIRE_ALL;
896                 }
897                 break;
898
899         case LDAP_REQ_DELETE:
900         case LDAP_REQ_MODIFY:
901                 /* just a unique candidate */
902                 break;
903
904         case LDAP_REQ_COMPARE:
905         case LDAP_REQ_SEARCH:
906                 /* allow multiple candidates for the searchBase */
907                 op_type = META_OP_ALLOW_MULTIPLE;
908                 break;
909
910         default:
911                 /* right now, just break (exop?) */
912                 break;
913         }
914
915         /*
916          * require all connections ...
917          */
918         if ( op_type == META_OP_REQUIRE_ALL ) {
919
920                 /* Looks like we didn't get a bind. Open a new session... */
921                 if ( mc == NULL ) {
922                         mc = metaconn_alloc( op );
923                         mc->mc_conn = mc_curr.mc_conn;
924                         ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
925                         new_conn = 1;
926                         if ( sendok & LDAP_BACK_BINDING ) {
927                                 LDAP_BACK_CONN_BINDING_SET( mc );
928                         }
929                 }
930
931                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
932                         metatarget_t            *mt = mi->mi_targets[ i ];
933
934                         /*
935                          * The target is activated; if needed, it is
936                          * also init'd
937                          */
938                         candidates[ i ].sr_err = meta_back_init_one_conn( op,
939                                 rs, mt, mc, i,
940                                 LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
941                         if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
942                                 candidates[ i ].sr_tag = META_CANDIDATE;
943                                 ncandidates++;
944         
945                         } else {
946                                 
947                                 /*
948                                  * FIXME: in case one target cannot
949                                  * be init'd, should the other ones
950                                  * be tried?
951                                  */
952                                 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
953                                 err = candidates[ i ].sr_err;
954                                 continue;
955                         }
956                 }
957
958                 if ( ncandidates == 0 ) {
959                         if ( new_conn ) {
960                                 meta_back_freeconn( op, mc );
961
962                         } else {
963                                 meta_back_release_conn( op, mc );
964                         }
965
966                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
967                         rs->sr_text = "Unable to select valid candidates";
968
969                         if ( sendok & LDAP_BACK_SENDERR ) {
970                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
971                                         rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
972                                 }
973                                 send_ldap_result( op, rs );
974                                 rs->sr_text = NULL;
975                                 rs->sr_matched = NULL;
976                         }
977
978                         return NULL;
979                 }
980
981                 goto done;
982         }
983         
984         /*
985          * looks in cache, if any
986          */
987         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
988                 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
989         }
990
991         if ( op_type == META_OP_REQUIRE_SINGLE ) {
992                 metatarget_t            *mt = NULL;
993                 metasingleconn_t        *msc = NULL;
994
995                 int                     j;
996
997                 for ( j = 0; j < mi->mi_ntargets; j++ ) {
998                         candidates[ j ].sr_tag = META_NOT_CANDIDATE;
999                 }
1000
1001                 /*
1002                  * tries to get a unique candidate
1003                  * (takes care of default target)
1004                  */
1005                 if ( i == META_TARGET_NONE ) {
1006                         i = meta_back_get_candidate( op, rs, &ndn );
1007
1008                         if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && dn_type == META_DNTYPE_PARENT ) {
1009                                 i = meta_back_get_candidate( op, rs, &pndn );
1010                         }
1011         
1012                         if ( i < 0 || rs->sr_err != LDAP_SUCCESS ) {
1013                                 if ( mc != NULL ) {
1014                                         meta_back_release_conn( op, mc );
1015                                 }
1016
1017                                 if ( sendok & LDAP_BACK_SENDERR ) {
1018                                         if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1019                                                 rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1020                                         }
1021                                         send_ldap_result( op, rs );
1022                                         rs->sr_text = NULL;
1023                                         rs->sr_matched = NULL;
1024                                 }
1025                         
1026                                 return NULL;
1027                         }
1028                 }
1029
1030                 if ( dn_type == META_DNTYPE_NEWPARENT && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
1031                 {
1032                         if ( mc != NULL ) {
1033                                 meta_back_release_conn( op, mc );
1034                         }
1035
1036                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1037                         rs->sr_text = "cross-target rename not supported";
1038                         if ( sendok & LDAP_BACK_SENDERR ) {
1039                                 send_ldap_result( op, rs );
1040                                 rs->sr_text = NULL;
1041                         }
1042
1043                         return NULL;
1044                 }
1045
1046                 Debug( LDAP_DEBUG_TRACE,
1047         "==>meta_back_getconn: got target=%d for ndn=\"%s\" from cache\n",
1048                                 i, op->o_req_ndn.bv_val, 0 );
1049
1050                 if ( mc == NULL ) {
1051                         /* Retries searching for a metaconn in the avl tree
1052                          * the reason is that the connection might have been
1053                          * created by meta_back_get_candidate() */
1054                         if ( !( sendok & LDAP_BACK_BINDING ) ) {
1055 retry_lock2:;
1056                                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1057                                 mc = (metaconn_t *)avl_find( mi->mi_conninfo.lai_tree, 
1058                                         (caddr_t)&mc_curr, meta_back_conndn_cmp );
1059                                 if ( mc != NULL ) {
1060                                         /* Don't reuse connections while they're still binding */
1061                                         if ( LDAP_BACK_CONN_BINDING( mc ) ) {
1062                                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1063                                                 ldap_pvt_thread_yield();
1064                                                 goto retry_lock2;
1065                                         }
1066
1067                                         mc->mc_refcnt++;
1068                                 }
1069                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1070                         }
1071
1072                         /* Looks like we didn't get a bind. Open a new session... */
1073                         if ( mc == NULL ) {
1074                                 mc = metaconn_alloc( op );
1075                                 mc->mc_conn = mc_curr.mc_conn;
1076                                 ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1077                                 new_conn = 1;
1078                                 if ( sendok & LDAP_BACK_BINDING ) {
1079                                         LDAP_BACK_CONN_BINDING_SET( mc );
1080                                 }
1081                         }
1082                 }
1083
1084                 /*
1085                  * Clear all other candidates
1086                  */
1087                 ( void )meta_clear_unused_candidates( op, i );
1088
1089                 mt = mi->mi_targets[ i ];
1090                 msc = &mc->mc_conns[ i ];
1091
1092                 /*
1093                  * The target is activated; if needed, it is
1094                  * also init'd. In case of error, meta_back_init_one_conn
1095                  * sends the appropriate result.
1096                  */
1097                 err = meta_back_init_one_conn( op, rs, mt, mc, i,
1098                         LDAP_BACK_CONN_ISPRIV( &mc_curr ), sendok );
1099                 if ( err != LDAP_SUCCESS ) {
1100                         /*
1101                          * FIXME: in case one target cannot
1102                          * be init'd, should the other ones
1103                          * be tried?
1104                          */
1105                         candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1106                         if ( new_conn ) {
1107                                 (void)meta_clear_one_candidate( msc );
1108                                 meta_back_freeconn( op, mc );
1109
1110                         } else {
1111                                 meta_back_release_conn( op, mc );
1112                         }
1113                         return NULL;
1114                 }
1115
1116                 candidates[ i ].sr_err = LDAP_SUCCESS;
1117                 candidates[ i ].sr_tag = META_CANDIDATE;
1118                 ncandidates++;
1119
1120                 if ( candidate ) {
1121                         *candidate = i;
1122                 }
1123
1124         /*
1125          * if no unique candidate ...
1126          */
1127         } else {
1128
1129                 /* Looks like we didn't get a bind. Open a new session... */
1130                 if ( mc == NULL ) {
1131                         mc = metaconn_alloc( op );
1132                         mc->mc_conn = mc_curr.mc_conn;
1133                         ber_dupbv( &mc->mc_local_ndn, &mc_curr.mc_local_ndn );
1134                         new_conn = 1;
1135                         if ( sendok & LDAP_BACK_BINDING ) {
1136                                 LDAP_BACK_CONN_BINDING_SET( mc );
1137                         }
1138                 }
1139
1140                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1141                         metatarget_t            *mt = mi->mi_targets[ i ];
1142                         metasingleconn_t        *msc = &mc->mc_conns[ i ];
1143
1144                         if ( i == cached 
1145                                 || meta_back_is_candidate( &mt->mt_nsuffix,
1146                                                 mt->mt_scope,
1147                                                 mt->mt_subtree_exclude,
1148                                                 &op->o_req_ndn,
1149                                                 LDAP_SCOPE_SUBTREE ) )
1150                         {
1151
1152                                 /*
1153                                  * The target is activated; if needed, it is
1154                                  * also init'd
1155                                  */
1156                                 int lerr = meta_back_init_one_conn( op, rs,
1157                                                 mt, mc, i,
1158                                                 LDAP_BACK_CONN_ISPRIV( &mc_curr ),
1159                                                 sendok );
1160                                 if ( lerr == LDAP_SUCCESS ) {
1161                                         candidates[ i ].sr_tag = META_CANDIDATE;
1162                                         candidates[ i ].sr_err = LDAP_SUCCESS;
1163                                         ncandidates++;
1164
1165                                         Debug( LDAP_DEBUG_TRACE, "%s: meta_back_getconn[%d]\n",
1166                                                 op->o_log_prefix, i, 0 );
1167
1168                                 } else {
1169                                 
1170                                         /*
1171                                          * FIXME: in case one target cannot
1172                                          * be init'd, should the other ones
1173                                          * be tried?
1174                                          */
1175                                         if ( new_conn ) {
1176                                                 ( void )meta_clear_one_candidate( msc );
1177                                         }
1178                                         /* leave the target candidate, but record the error for later use */
1179                                         candidates[ i ].sr_err = lerr;
1180                                         err = lerr;
1181
1182                                         Debug( LDAP_DEBUG_ANY, "%s: meta_back_getconn[%d] failed: %d\n",
1183                                                 op->o_log_prefix, i, lerr );
1184
1185                                         continue;
1186                                 }
1187
1188                         } else {
1189                                 if ( new_conn ) {
1190                                         ( void )meta_clear_one_candidate( msc );
1191                                 }
1192                                 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
1193                         }
1194                 }
1195
1196                 if ( ncandidates == 0 ) {
1197                         if ( new_conn ) {
1198                                 meta_back_freeconn( op, mc );
1199
1200                         } else {
1201                                 meta_back_release_conn( op, mc );
1202                         }
1203
1204                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
1205                         rs->sr_text = "Unable to select valid candidates";
1206
1207                         if ( sendok & LDAP_BACK_SENDERR ) {
1208                                 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
1209                                         rs->sr_matched = op->o_bd->be_suffix[ 0 ].bv_val;
1210                                 }
1211                                 send_ldap_result( op, rs );
1212                                 rs->sr_text = NULL;
1213                                 rs->sr_matched = NULL;
1214                         }
1215
1216                         return NULL;
1217                 }
1218         }
1219
1220 done:;
1221         /* clear out meta_back_init_one_conn non-fatal errors */
1222         rs->sr_err = LDAP_SUCCESS;
1223         rs->sr_text = NULL;
1224
1225         /* touch the timestamp */
1226         if ( mi->mi_idle_timeout != 0 ) {
1227                 mc->mc_time = op->o_time;
1228         }
1229
1230         if ( new_conn ) {
1231                 if ( mi->mi_conn_ttl ) {
1232                         mc->mc_create_time = op->o_time;
1233                 }
1234
1235                 /*
1236                  * Inserts the newly created metaconn in the avl tree
1237                  */
1238                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1239                 err = avl_insert( &mi->mi_conninfo.lai_tree, ( caddr_t )mc,
1240                                 meta_back_conndn_cmp, meta_back_conndn_dup );
1241
1242 #if PRINT_CONNTREE > 0
1243                 myprint( mi->mi_conninfo.lai_tree );
1244 #endif /* PRINT_CONNTREE */
1245                 
1246                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1247
1248                 /*
1249                  * Err could be -1 in case a duplicate metaconn is inserted
1250                  */
1251                 switch ( err ) {
1252                 case 0:
1253                         break;
1254
1255                 case -1:
1256                         if ( !( sendok & LDAP_BACK_BINDING ) ) {
1257                                 /* duplicate: free and try to get the newly created one */
1258                                 goto retry_lock;
1259                         }
1260                         LDAP_BACK_CONN_TAINTED_SET( mc );
1261                         break;
1262
1263                 default:
1264                         Debug( LDAP_DEBUG_ANY,
1265                                 "%s meta_back_getconn: candidates=%d conn=%ld insert failed\n",
1266                                 op->o_log_prefix, ncandidates,
1267                                 LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1268                 
1269                         meta_back_freeconn( op, mc );
1270
1271                         rs->sr_err = LDAP_OTHER;
1272                         rs->sr_text = "proxy bind collision";
1273                         if ( sendok & LDAP_BACK_SENDERR ) {
1274                                 send_ldap_result( op, rs );
1275                                 rs->sr_text = NULL;
1276                         }
1277                         return NULL;
1278                 }
1279
1280                 Debug( LDAP_DEBUG_TRACE,
1281                         "%s meta_back_getconn: candidates=%d conn=%ld inserted\n",
1282                         op->o_log_prefix, ncandidates,
1283                         LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1284
1285         } else {
1286                 Debug( LDAP_DEBUG_TRACE,
1287                         "%s meta_back_getconn: candidates=%d conn=%ld fetched\n",
1288                         op->o_log_prefix, ncandidates,
1289                         LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1290         }
1291         
1292         return mc;
1293 }
1294
1295 void
1296 meta_back_release_conn_lock(
1297         Operation               *op,
1298         metaconn_t              *mc,
1299         int                     dolock )
1300 {
1301         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
1302
1303         assert( mc != NULL );
1304
1305         if ( dolock ) {
1306                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1307         }
1308         assert( mc->mc_refcnt > 0 );
1309         mc->mc_refcnt--;
1310         LDAP_BACK_CONN_BINDING_CLEAR( mc );
1311         if ( LDAP_BACK_CONN_TAINTED( mc ) ) {
1312                 Debug( LDAP_DEBUG_TRACE, "%s meta_back_release_conn: mc=%p conn=%ld expired.\n",
1313                         op->o_log_prefix, (void *)mc, LDAP_BACK_PCONN_ID( mc->mc_conn ) );
1314                 (void)avl_delete( &mi->mi_conninfo.lai_tree,
1315                         ( caddr_t )mc, meta_back_conndnmc_cmp );
1316                 if ( mc->mc_refcnt == 0 ) {
1317                         meta_clear_candidates( op, mc );
1318                         meta_back_conn_free( mc );
1319                 }
1320         }
1321         if ( dolock ) {
1322                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1323         }
1324 }