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