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