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