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