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