]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
Import ITS#4158 fixes from HEAD
[openldap] / servers / slapd / connection.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29 #include <limits.h>
30
31 #include <ac/socket.h>
32 #include <ac/errno.h>
33 #include <ac/string.h>
34 #include <ac/time.h>
35 #include <ac/unistd.h>
36
37 #include "ldap_pvt.h"
38 #include "lutil.h"
39 #include "slap.h"
40
41 #ifdef LDAP_SLAPI
42 #include "slapi/slapi.h"
43 #endif
44
45 /* protected by connections_mutex */
46 static ldap_pvt_thread_mutex_t connections_mutex;
47 static Connection *connections = NULL;
48
49 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
50 static unsigned long conn_nextid = 0;
51
52 /* structure state (protected by connections_mutex) */
53 #define SLAP_C_UNINITIALIZED    0x00    /* MUST BE ZERO (0) */
54 #define SLAP_C_UNUSED                   0x01
55 #define SLAP_C_USED                             0x02
56
57 /* connection state (protected by c_mutex ) */
58 #define SLAP_C_INVALID                  0x00    /* MUST BE ZERO (0) */
59 #define SLAP_C_INACTIVE                 0x01    /* zero threads */
60 #define SLAP_C_ACTIVE                   0x02    /* one or more threads */
61 #define SLAP_C_BINDING                  0x03    /* binding */
62 #define SLAP_C_CLOSING                  0x04    /* closing */
63 #define SLAP_C_CLIENT                   0x05    /* outbound client conn */
64
65 const char *
66 connection_state2str( int state )
67 {
68         switch( state ) {
69         case SLAP_C_INVALID:    return "!";
70         case SLAP_C_INACTIVE:   return "|";
71         case SLAP_C_ACTIVE:             return "";
72         case SLAP_C_BINDING:    return "B";
73         case SLAP_C_CLOSING:    return "C";
74         case SLAP_C_CLIENT:             return "L";
75         }
76
77         return "?";
78 }
79
80 static Connection* connection_get( ber_socket_t s );
81
82 static int connection_input( Connection *c );
83 static void connection_close( Connection *c );
84
85 static int connection_op_activate( Operation *op );
86 static int connection_resched( Connection *conn );
87 static void connection_abandon( Connection *conn );
88 static void connection_destroy( Connection *c );
89
90 static ldap_pvt_thread_start_t connection_operation;
91
92 /*
93  * Initialize connection management infrastructure.
94  */
95 int connections_init(void)
96 {
97         int i;
98
99         assert( connections == NULL );
100
101         if( connections != NULL) {
102 #ifdef NEW_LOGGING
103                 LDAP_LOG( CONNECTION, INFO,
104                         "connections_init: already initialized.\n", 0, 0, 0 );
105 #else
106                 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
107                         0, 0, 0 );
108 #endif
109                 return -1;
110         }
111
112         /* should check return of every call */
113         ldap_pvt_thread_mutex_init( &connections_mutex );
114         ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
115
116         connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
117
118         if( connections == NULL ) {
119 #ifdef NEW_LOGGING
120                 LDAP_LOG( CONNECTION, ERR,
121                         "connections_init: allocation (%d * %ld) of connection "
122                         "array failed\n", dtblsize, (long) sizeof(Connection), 0 );
123 #else
124                 Debug( LDAP_DEBUG_ANY,
125                         "connections_init: allocation (%d*%ld) of connection array failed\n",
126                         dtblsize, (long) sizeof(Connection), 0 );
127 #endif
128                 return -1;
129         }
130
131         assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
132         assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
133
134         for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
135
136         /*
137          * per entry initialization of the Connection array initialization
138          * will be done by connection_init()
139          */ 
140
141         return 0;
142 }
143
144 /*
145  * Destroy connection management infrastructure.
146  */
147 int connections_destroy(void)
148 {
149         ber_socket_t i;
150
151         /* should check return of every call */
152
153         if( connections == NULL) {
154 #ifdef NEW_LOGGING
155                 LDAP_LOG( CONNECTION, INFO,
156                         "connections_destroy: nothing to destroy.\n", 0, 0, 0 );
157 #else
158                 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
159                         0, 0, 0 );
160 #endif
161                 return -1;
162         }
163
164         for ( i = 0; i < dtblsize; i++ ) {
165                 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
166                         ber_sockbuf_free( connections[i].c_sb );
167                         ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
168                         ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
169                         ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
170 #ifdef LDAP_SLAPI
171                         if ( slapi_plugins_used ) {
172                                 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &connections[i] );
173                         }
174 #endif
175                 }
176         }
177
178         free( connections );
179         connections = NULL;
180
181         ldap_pvt_thread_mutex_destroy( &connections_mutex );
182         ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
183         return 0;
184 }
185
186 /*
187  * shutdown all connections
188  */
189 int connections_shutdown(void)
190 {
191         ber_socket_t i;
192
193         ldap_pvt_thread_mutex_lock( &connections_mutex );
194
195         for ( i = 0; i < dtblsize; i++ ) {
196                 if( connections[i].c_struct_state != SLAP_C_USED ) {
197                         continue;
198                 }
199                 /* give persistent clients a chance to cleanup */
200                 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
201                         ldap_pvt_thread_pool_submit( &connection_pool,
202                         connections[i].c_clientfunc, connections[i].c_clientarg );
203                         continue;
204                 }
205
206                 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
207
208                 /* connections_mutex and c_mutex are locked */
209                 connection_closing( &connections[i] );
210                 connection_close( &connections[i] );
211
212                 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
213         }
214
215         ldap_pvt_thread_mutex_unlock( &connections_mutex );
216
217         return 0;
218 }
219
220 /*
221  * Timeout idle connections.
222  */
223 int connections_timeout_idle(time_t now)
224 {
225         int i = 0;
226         int connindex;
227         Connection* c;
228
229         for( c = connection_first( &connindex );
230                 c != NULL;
231                 c = connection_next( c, &connindex ) )
232         {
233                 /* Don't timeout a slow-running request or a persistent
234                  * outbound connection */
235                 if( c->c_n_ops_executing ||
236                         c->c_conn_state == SLAP_C_CLIENT ) continue;
237
238                 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
239                         /* close it */
240                         connection_closing( c );
241                         connection_close( c );
242                         i++;
243                 }
244         }
245         connection_done( c );
246
247         return i;
248 }
249
250 static Connection* connection_get( ber_socket_t s )
251 {
252         /* connections_mutex should be locked by caller */
253
254         Connection *c;
255
256 #ifdef NEW_LOGGING
257         LDAP_LOG( CONNECTION, ENTRY, "connection_get: socket %ld\n", (long)s, 0, 0 );
258 #else
259         Debug( LDAP_DEBUG_ARGS,
260                 "connection_get(%ld)\n",
261                 (long) s, 0, 0 );
262 #endif
263
264         assert( connections != NULL );
265
266         if(s == AC_SOCKET_INVALID) {
267                 return NULL;
268         }
269
270 #ifndef HAVE_WINSOCK
271         c = &connections[s];
272
273         assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
274
275 #else
276         c = NULL;
277         {
278                 ber_socket_t i, sd;
279
280                 for(i=0; i<dtblsize; i++) {
281                         if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
282                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
283                                 assert( connections[i].c_sb == 0 );
284                                 break;
285                         }
286
287                         ber_sockbuf_ctrl( connections[i].c_sb,
288                                 LBER_SB_OPT_GET_FD, &sd );
289
290                         if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
291                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
292                                 assert( sd == AC_SOCKET_INVALID );
293                                 continue;
294                         }
295
296                         /* state can actually change from used -> unused by resched,
297                          * so don't assert details here.
298                          */
299
300                         if( sd == s ) {
301                                 c = &connections[i];
302                                 break;
303                         }
304                 }
305         }
306 #endif
307
308         if( c != NULL ) {
309                 ber_socket_t    sd;
310
311                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
312
313                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
314                 if( c->c_struct_state != SLAP_C_USED ) {
315                         /* connection must have been closed due to resched */
316
317                         assert( c->c_conn_state == SLAP_C_INVALID );
318                         assert( sd == AC_SOCKET_INVALID );
319
320 #ifdef NEW_LOGGING
321                         LDAP_LOG( CONNECTION, ARGS, 
322                                 "connection_get: connection %d not used\n", s, 0, 0 );
323 #else
324                         Debug( LDAP_DEBUG_TRACE,
325                                 "connection_get(%d): connection not used\n",
326                                 s, 0, 0 );
327 #endif
328
329                         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
330                         return NULL;
331                 }
332
333 #ifdef NEW_LOGGING
334                 LDAP_LOG( CONNECTION, RESULTS, 
335                         "connection_get: get for %d got connid %lu\n", s, c->c_connid, 0 );
336 #else
337                 Debug( LDAP_DEBUG_TRACE,
338                         "connection_get(%d): got connid=%lu\n",
339                         s, c->c_connid, 0 );
340 #endif
341
342                 c->c_n_get++;
343
344                 assert( c->c_struct_state == SLAP_C_USED );
345                 assert( c->c_conn_state != SLAP_C_INVALID );
346                 assert( sd != AC_SOCKET_INVALID );
347
348 #ifdef SLAPD_MONITOR
349                 c->c_activitytime = slap_get_time();
350 #else
351                 if( global_idletimeout > 0 ) {
352                         c->c_activitytime = slap_get_time();
353                 }
354 #endif
355         }
356
357         return c;
358 }
359
360 static void connection_return( Connection *c )
361 {
362         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
363 }
364
365 long connection_init(
366         ber_socket_t s,
367         Listener *listener,
368         const char* dnsname,
369         const char* peername,
370         int flags,
371         slap_ssf_t ssf,
372         struct berval *authid )
373 {
374         unsigned long id;
375         Connection *c;
376
377         assert( connections != NULL );
378
379         assert( listener != NULL );
380         assert( dnsname != NULL );
381         assert( peername != NULL );
382
383 #ifndef HAVE_TLS
384         assert( flags != CONN_IS_TLS );
385 #endif
386
387         if( s == AC_SOCKET_INVALID ) {
388 #ifdef NEW_LOGGING
389                 LDAP_LOG( CONNECTION, INFO, 
390                         "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
391 #else
392                 Debug( LDAP_DEBUG_ANY,
393                         "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
394 #endif
395                 return -1;
396         }
397
398         assert( s >= 0 );
399 #ifndef HAVE_WINSOCK
400         assert( s < dtblsize );
401 #endif
402
403         ldap_pvt_thread_mutex_lock( &connections_mutex );
404
405 #ifndef HAVE_WINSOCK
406         c = &connections[s];
407
408 #else
409         {
410                 ber_socket_t i;
411                 c = NULL;
412
413                 for( i=0; i < dtblsize; i++) {
414                         ber_socket_t    sd;
415
416                         if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
417                                 assert( connections[i].c_sb == 0 );
418                                 c = &connections[i];
419                                 break;
420                         }
421
422                         sd = AC_SOCKET_INVALID;
423                         if (connections[i].c_sb != NULL) {
424                                 ber_sockbuf_ctrl( connections[i].c_sb,
425                                         LBER_SB_OPT_GET_FD, &sd );
426                         }
427
428                         if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
429                                 assert( sd == AC_SOCKET_INVALID );
430                                 c = &connections[i];
431                                 break;
432                         }
433
434                         if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
435                                 continue;
436                         }
437
438                         assert( connections[i].c_struct_state == SLAP_C_USED );
439                         assert( connections[i].c_conn_state != SLAP_C_INVALID );
440                         assert( sd != AC_SOCKET_INVALID );
441                 }
442
443                 if( c == NULL ) {
444 #ifdef NEW_LOGGING
445                         LDAP_LOG( CONNECTION, INFO, 
446                                 "connection_init(%d): connection table full "
447                                 "(%d/%d)\n", s, i, dtblsize );
448 #else
449                         Debug( LDAP_DEBUG_ANY,
450                                 "connection_init(%d): connection table full "
451                                 "(%d/%d)\n", s, i, dtblsize);
452 #endif
453                         ldap_pvt_thread_mutex_unlock( &connections_mutex );
454                         return -1;
455                 }
456         }
457 #endif
458
459         assert( c != NULL );
460
461         if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
462                 c->c_send_ldap_result = slap_send_ldap_result;
463                 c->c_send_search_entry = slap_send_search_entry;
464                 c->c_send_search_reference = slap_send_search_reference;
465                 c->c_send_ldap_extended = slap_send_ldap_extended;
466 #ifdef LDAP_RES_INTERMEDIATE
467                 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
468 #endif
469
470                 c->c_authmech.bv_val = NULL;
471                 c->c_authmech.bv_len = 0;
472                 c->c_dn.bv_val = NULL;
473                 c->c_dn.bv_len = 0;
474                 c->c_ndn.bv_val = NULL;
475                 c->c_ndn.bv_len = 0;
476
477                 c->c_listener = NULL;
478                 c->c_peer_domain.bv_val = NULL;
479                 c->c_peer_domain.bv_len = 0;
480                 c->c_peer_name.bv_val = NULL;
481                 c->c_peer_name.bv_len = 0;
482
483                 LDAP_STAILQ_INIT(&c->c_ops);
484                 LDAP_STAILQ_INIT(&c->c_pending_ops);
485
486                 c->c_sasl_bind_mech.bv_val = NULL;
487                 c->c_sasl_bind_mech.bv_len = 0;
488                 c->c_sasl_done = 0;
489                 c->c_sasl_authctx = NULL;
490                 c->c_sasl_sockctx = NULL;
491                 c->c_sasl_extra = NULL;
492                 c->c_sasl_bindop = NULL;
493
494                 c->c_sb = ber_sockbuf_alloc( );
495
496                 {
497                         ber_len_t max = sockbuf_max_incoming;
498                         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
499                 }
500
501                 c->c_currentber = NULL;
502
503                 /* should check status of thread calls */
504                 ldap_pvt_thread_mutex_init( &c->c_mutex );
505                 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
506                 ldap_pvt_thread_cond_init( &c->c_write_cv );
507
508 #ifdef LDAP_SLAPI
509                 if ( slapi_plugins_used ) {
510                         slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
511                 }
512 #endif
513
514                 c->c_struct_state = SLAP_C_UNUSED;
515         }
516
517         ldap_pvt_thread_mutex_lock( &c->c_mutex );
518
519         assert( c->c_struct_state == SLAP_C_UNUSED );
520         assert( c->c_authmech.bv_val == NULL );
521         assert( c->c_dn.bv_val == NULL );
522         assert( c->c_ndn.bv_val == NULL );
523         assert( c->c_listener == NULL );
524         assert( c->c_peer_domain.bv_val == NULL );
525         assert( c->c_peer_name.bv_val == NULL );
526         assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
527         assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
528         assert( c->c_sasl_bind_mech.bv_val == NULL );
529         assert( c->c_sasl_done == 0 );
530         assert( c->c_sasl_authctx == NULL );
531         assert( c->c_sasl_sockctx == NULL );
532         assert( c->c_sasl_extra == NULL );
533         assert( c->c_sasl_bindop == NULL );
534         assert( c->c_currentber == NULL );
535         assert( c->c_writewaiter == 0);
536
537         c->c_listener = listener;
538
539         if ( flags == CONN_IS_CLIENT ) {
540                 c->c_conn_state = SLAP_C_CLIENT;
541                 c->c_struct_state = SLAP_C_USED;
542                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
543                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
544                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
545
546                 return 0;
547         }
548
549         ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
550         ber_str2bv( peername, 0, 1, &c->c_peer_name );
551
552         c->c_n_ops_received = 0;
553         c->c_n_ops_executing = 0;
554         c->c_n_ops_pending = 0;
555         c->c_n_ops_completed = 0;
556
557         c->c_n_get = 0;
558         c->c_n_read = 0;
559         c->c_n_write = 0;
560
561         /* set to zero until bind, implies LDAP_VERSION3 */
562         c->c_protocol = 0;
563
564 #ifdef SLAPD_MONITOR
565         c->c_activitytime = c->c_starttime = slap_get_time();
566 #else
567         if( global_idletimeout > 0 ) {
568                 c->c_activitytime = c->c_starttime = slap_get_time();
569         }
570 #endif
571
572 #ifdef LDAP_CONNECTIONLESS
573         c->c_is_udp = 0;
574         if( flags == CONN_IS_UDP ) {
575                 c->c_is_udp = 1;
576 #ifdef LDAP_DEBUG
577                 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
578                         LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
579 #endif
580                 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
581                         LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
582                 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
583                         LBER_SBIOD_LEVEL_PROVIDER, NULL );
584         } else
585 #endif
586         {
587 #ifdef LDAP_DEBUG
588                 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
589                         LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
590 #endif
591                 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
592                         LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
593         }
594
595 #ifdef LDAP_DEBUG
596         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
597                 INT_MAX, (void*)"ldap_" );
598 #endif
599
600         if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
601                 c /* non-NULL */ ) < 0 )
602         {
603 #ifdef NEW_LOGGING
604                 LDAP_LOG( CONNECTION, INFO, 
605                         "connection_init: conn %lu set nonblocking failed\n",
606                         c->c_connid, 0, 0 );
607 #else
608                 Debug( LDAP_DEBUG_ANY,
609                         "connection_init(%d, %s): set nonblocking failed\n",
610                         s, c->c_peer_name.bv_val, 0 );
611 #endif
612         }
613
614         ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
615         id = c->c_connid = conn_nextid++;
616         ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
617
618         c->c_conn_state = SLAP_C_INACTIVE;
619         c->c_struct_state = SLAP_C_USED;
620
621         c->c_ssf = c->c_transport_ssf = ssf;
622         c->c_tls_ssf = 0;
623
624 #ifdef HAVE_TLS
625         if ( flags == CONN_IS_TLS ) {
626                 c->c_is_tls = 1;
627                 c->c_needs_tls_accept = 1;
628         } else {
629                 c->c_is_tls = 0;
630                 c->c_needs_tls_accept = 0;
631         }
632 #endif
633
634         slap_sasl_open( c, 0 );
635         slap_sasl_external( c, ssf, authid );
636
637         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
638         ldap_pvt_thread_mutex_unlock( &connections_mutex );
639
640         backend_connection_init(c);
641
642         return id;
643 }
644
645 void connection2anonymous( Connection *c )
646 {
647         assert( connections != NULL );
648         assert( c != NULL );
649
650         {
651                 ber_len_t max = sockbuf_max_incoming;
652                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
653         }
654
655         if(c->c_authmech.bv_val != NULL ) {
656                 free(c->c_authmech.bv_val);
657                 c->c_authmech.bv_val = NULL;
658         }
659         c->c_authmech.bv_len = 0;
660
661         if(c->c_dn.bv_val != NULL) {
662                 free(c->c_dn.bv_val);
663                 c->c_dn.bv_val = NULL;
664         }
665         c->c_dn.bv_len = 0;
666         if(c->c_ndn.bv_val != NULL) {
667                 free(c->c_ndn.bv_val);
668                 c->c_ndn.bv_val = NULL;
669         }
670         c->c_ndn.bv_len = 0;
671
672         c->c_authz_backend = NULL;
673 }
674
675 static void
676 connection_destroy( Connection *c )
677 {
678         /* note: connections_mutex should be locked by caller */
679         ber_socket_t    sd;
680         unsigned long   connid;
681
682         assert( connections != NULL );
683         assert( c != NULL );
684         assert( c->c_struct_state != SLAP_C_UNUSED );
685         assert( c->c_conn_state != SLAP_C_INVALID );
686         assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
687         assert( c->c_writewaiter == 0);
688
689         /* only for stats (print -1 as "%lu" may give unexpected results ;) */
690         connid = c->c_connid;
691
692         backend_connection_destroy(c);
693
694         c->c_protocol = 0;
695         c->c_connid = -1;
696
697         c->c_activitytime = c->c_starttime = 0;
698
699         connection2anonymous( c );
700         c->c_listener = NULL;
701
702         if(c->c_peer_domain.bv_val != NULL) {
703                 free(c->c_peer_domain.bv_val);
704                 c->c_peer_domain.bv_val = NULL;
705         }
706         c->c_peer_domain.bv_len = 0;
707         if(c->c_peer_name.bv_val != NULL) {
708                 free(c->c_peer_name.bv_val);
709                 c->c_peer_name.bv_val = NULL;
710         }
711         c->c_peer_name.bv_len = 0;
712
713         c->c_sasl_bind_in_progress = 0;
714         if(c->c_sasl_bind_mech.bv_val != NULL) {
715                 free(c->c_sasl_bind_mech.bv_val);
716                 c->c_sasl_bind_mech.bv_val = NULL;
717         }
718         c->c_sasl_bind_mech.bv_len = 0;
719
720         slap_sasl_close( c );
721
722         if ( c->c_currentber != NULL ) {
723                 ber_free( c->c_currentber, 1 );
724                 c->c_currentber = NULL;
725         }
726
727         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
728         if ( sd != AC_SOCKET_INVALID ) {
729                 slapd_remove( sd, 1, 0 );
730
731                 Statslog( LDAP_DEBUG_STATS,
732                         "conn=%lu fd=%ld closed\n",
733                         connid, (long) sd, 0, 0, 0 );
734         }
735
736         ber_sockbuf_free( c->c_sb );
737
738         c->c_sb = ber_sockbuf_alloc( );
739
740         {
741                 ber_len_t max = sockbuf_max_incoming;
742                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
743         }
744
745         c->c_conn_state = SLAP_C_INVALID;
746         c->c_struct_state = SLAP_C_UNUSED;
747
748 #ifdef LDAP_SLAPI
749         /* call destructors, then constructors; avoids unnecessary allocation */
750         if ( slapi_plugins_used ) {
751                 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
752         }
753 #endif
754 }
755
756 int connection_state_closing( Connection *c )
757 {
758         /* c_mutex must be locked by caller */
759
760         int state;
761         assert( c != NULL );
762         assert( c->c_struct_state == SLAP_C_USED );
763
764         state = c->c_conn_state;
765
766         assert( state != SLAP_C_INVALID );
767
768         return state == SLAP_C_CLOSING;
769 }
770
771 static void connection_abandon( Connection *c )
772 {
773         /* c_mutex must be locked by caller */
774
775         Operation *o;
776
777         LDAP_STAILQ_FOREACH( o, &c->c_ops, o_next ) {
778                 o->o_abandon = 1;
779         }
780
781         /* remove pending operations */
782         while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
783                 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
784                 LDAP_STAILQ_NEXT(o, o_next) = NULL;
785                 slap_op_free( o );
786         }
787 }
788
789 void connection_closing( Connection *c )
790 {
791         assert( connections != NULL );
792         assert( c != NULL );
793         assert( c->c_struct_state == SLAP_C_USED );
794         assert( c->c_conn_state != SLAP_C_INVALID );
795
796         /* c_mutex must be locked by caller */
797
798         if( c->c_conn_state != SLAP_C_CLOSING ) {
799                 ber_socket_t    sd;
800
801                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
802 #ifdef NEW_LOGGING
803                 LDAP_LOG( CONNECTION, DETAIL1, 
804                         "connection_closing: conn %lu readying socket %d for close.\n",
805                         c->c_connid, sd, 0 );
806 #else
807                 Debug( LDAP_DEBUG_TRACE,
808                         "connection_closing: readying conn=%lu sd=%d for close\n",
809                         c->c_connid, sd, 0 );
810 #endif
811                 /* update state to closing */
812                 c->c_conn_state = SLAP_C_CLOSING;
813
814                 /* don't listen on this port anymore */
815                 slapd_clr_read( sd, 1 );
816
817                 /* abandon active operations */
818                 connection_abandon( c );
819
820                 /* wake write blocked operations */
821                 slapd_clr_write( sd, 1 );
822                 ldap_pvt_thread_cond_signal( &c->c_write_cv );
823         }
824 }
825
826 static void connection_close( Connection *c )
827 {
828         ber_socket_t    sd;
829
830         assert( connections != NULL );
831         assert( c != NULL );
832         assert( c->c_struct_state == SLAP_C_USED );
833         assert( c->c_conn_state == SLAP_C_CLOSING );
834
835         /* note: connections_mutex and c_mutex should be locked by caller */
836
837         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
838         if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
839 #ifdef NEW_LOGGING
840                 LDAP_LOG( CONNECTION, DETAIL1, 
841                         "connection_close: conn %lu deferring sd %d\n",
842                                 c->c_connid, sd, 0 );
843 #else
844                 Debug( LDAP_DEBUG_TRACE,
845                         "connection_close: deferring conn=%lu sd=%d\n",
846                         c->c_connid, sd, 0 );
847 #endif
848                 return;
849         }
850
851 #ifdef NEW_LOGGING
852         LDAP_LOG( CONNECTION, RESULTS, 
853                 "connection_close: conn %lu sd %d\n", c->c_connid, sd, 0 );
854 #else
855         Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
856                 c->c_connid, sd, 0 );
857 #endif
858         connection_destroy( c );
859 }
860
861 unsigned long connections_nextid(void)
862 {
863         unsigned long id;
864         assert( connections != NULL );
865
866         ldap_pvt_thread_mutex_lock( &connections_mutex );
867
868         id = conn_nextid;
869
870         ldap_pvt_thread_mutex_unlock( &connections_mutex );
871
872         return id;
873 }
874
875 Connection* connection_first( ber_socket_t *index )
876 {
877         assert( connections != NULL );
878         assert( index != NULL );
879
880         ldap_pvt_thread_mutex_lock( &connections_mutex );
881
882         *index = 0;
883
884         return connection_next(NULL, index);
885 }
886
887 Connection* connection_next( Connection *c, ber_socket_t *index )
888 {
889         assert( connections != NULL );
890         assert( index != NULL );
891         assert( *index <= dtblsize );
892
893         if( c != NULL ) {
894                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
895         }
896
897         c = NULL;
898
899         for(; *index < dtblsize; (*index)++) {
900                 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
901                         assert( connections[*index].c_conn_state == SLAP_C_INVALID );
902 #ifndef HAVE_WINSOCK
903                         continue;
904 #else
905                         break;
906 #endif
907                 }
908
909                 if( connections[*index].c_struct_state == SLAP_C_USED ) {
910                         assert( connections[*index].c_conn_state != SLAP_C_INVALID );
911                         c = &connections[(*index)++];
912                         break;
913                 }
914
915                 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
916                 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
917         }
918
919         if( c != NULL ) {
920                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
921         }
922
923         return c;
924 }
925
926 void connection_done( Connection *c )
927 {
928         assert( connections != NULL );
929
930         if( c != NULL ) {
931                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
932         }
933
934         ldap_pvt_thread_mutex_unlock( &connections_mutex );
935 }
936
937 /*
938  * connection_activity - handle the request operation op on connection
939  * conn.  This routine figures out what kind of operation it is and
940  * calls the appropriate stub to handle it.
941  */
942
943 #ifdef SLAPD_MONITOR
944 #define INCR_OP(var,index) \
945         do { \
946                 ldap_pvt_thread_mutex_lock( &num_ops_mutex ); \
947                 (var)[(index)]++; \
948                 ldap_pvt_thread_mutex_unlock( &num_ops_mutex ); \
949         } while (0)
950 #else /* !SLAPD_MONITOR */
951 #define INCR_OP(var,index) 
952 #endif /* !SLAPD_MONITOR */
953
954 static void *
955 connection_operation( void *ctx, void *arg_v )
956 {
957         int rc = LDAP_OTHER;
958         Operation *op = arg_v;
959         SlapReply rs = {REP_RESULT};
960         ber_tag_t tag = op->o_tag;
961 #ifdef SLAPD_MONITOR
962         ber_tag_t oldtag = tag;
963 #endif /* SLAPD_MONITOR */
964         Connection *conn = op->o_conn;
965         void *memctx = NULL;
966         void *memctx_null = NULL;
967         ber_len_t memsiz;
968
969         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
970         num_ops_initiated++;
971         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
972
973         op->o_threadctx = ctx;
974
975         switch ( tag ) {
976         case LDAP_REQ_BIND:
977         case LDAP_REQ_UNBIND:
978         case LDAP_REQ_ADD:
979         case LDAP_REQ_DELETE:
980         case LDAP_REQ_MODRDN:
981         case LDAP_REQ_MODIFY:
982         case LDAP_REQ_COMPARE:
983         case LDAP_REQ_SEARCH:
984         case LDAP_REQ_ABANDON:
985         case LDAP_REQ_EXTENDED:
986                 break;
987         default:
988 #ifdef NEW_LOGGING
989                 LDAP_LOG( CONNECTION, INFO, "connection_operation: "
990                         "conn %lu unknown LDAP request 0x%lx\n",
991                         conn->c_connid, tag, 0 );
992 #else
993                 Debug( LDAP_DEBUG_ANY, "connection_operation: "
994                         "conn %lu unknown LDAP request 0x%lx\n",
995                         conn->c_connid, tag, 0 );
996 #endif
997                 op->o_tag = LBER_ERROR;
998                 rs.sr_err = LDAP_PROTOCOL_ERROR;
999                 rs.sr_text = "unknown LDAP request";
1000                 send_ldap_disconnect( op, &rs );
1001                 rc = SLAPD_DISCONNECT;
1002                 goto operations_error;
1003         }
1004
1005         if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1006 #ifdef NEW_LOGGING
1007                 LDAP_LOG( CONNECTION, ERR, 
1008                         "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
1009                         conn->c_connid, (long)tag, 0 );
1010 #else
1011                 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1012                         "error: SASL bind in progress (tag=%ld).\n",
1013                         (long) tag, 0, 0 );
1014 #endif
1015                 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1016                         "SASL bind in progress" );
1017                 rc = LDAP_OPERATIONS_ERROR;
1018                 goto operations_error;
1019         }
1020
1021         /* We can use Thread-Local storage for most mallocs. We can
1022          * also use TL for ber parsing, but not on Add or Modify.
1023          */
1024 #if 0
1025         memsiz = ber_len( op->o_ber ) * 64;
1026         if ( SLMALLOC_SLAB_SIZE > memsiz ) memsiz = SLMALLOC_SLAB_SIZE;
1027 #endif
1028         memsiz = SLMALLOC_SLAB_SIZE;
1029
1030         memctx = sl_mem_create( memsiz, ctx );
1031         op->o_tmpmemctx = memctx;
1032         op->o_tmpmfuncs = &sl_mfuncs;
1033         if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1034                 /* Note - the ber and its buffer are already allocated from
1035                  * regular memory; this only affects subsequent mallocs that
1036                  * ber_scanf may invoke.
1037                  */
1038                 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1039         }
1040
1041         switch ( tag ) {
1042         case LDAP_REQ_BIND:
1043                 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
1044                 rc = do_bind( op, &rs );
1045                 break;
1046
1047         case LDAP_REQ_UNBIND:
1048                 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
1049                 rc = do_unbind( op, &rs );
1050                 break;
1051
1052         case LDAP_REQ_ADD:
1053                 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
1054                 rc = do_add( op, &rs );
1055                 break;
1056
1057         case LDAP_REQ_DELETE:
1058                 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
1059                 rc = do_delete( op, &rs );
1060                 break;
1061
1062         case LDAP_REQ_MODRDN:
1063                 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
1064                 rc = do_modrdn( op, &rs );
1065                 break;
1066
1067         case LDAP_REQ_MODIFY:
1068                 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
1069                 rc = do_modify( op, &rs );
1070                 break;
1071
1072         case LDAP_REQ_COMPARE:
1073                 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
1074                 rc = do_compare( op, &rs );
1075                 break;
1076
1077         case LDAP_REQ_SEARCH:
1078                 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
1079                 rc = do_search( op, &rs );
1080                 break;
1081
1082         case LDAP_REQ_ABANDON:
1083                 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
1084                 rc = do_abandon( op, &rs );
1085                 break;
1086
1087         case LDAP_REQ_EXTENDED:
1088                 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
1089                 rc = do_extended( op, &rs );
1090                 break;
1091
1092         default:
1093                 /* not reachable */
1094                 assert( 0 );
1095         }
1096
1097 operations_error:
1098         if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
1099
1100         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
1101
1102         num_ops_completed++;
1103 #ifdef SLAPD_MONITOR
1104         switch (oldtag) {
1105         case LDAP_REQ_BIND:
1106                 num_ops_completed_[SLAP_OP_BIND]++;
1107                 break;
1108         case LDAP_REQ_UNBIND:
1109                 num_ops_completed_[SLAP_OP_UNBIND]++;
1110                 break;
1111         case LDAP_REQ_ADD:
1112                 num_ops_completed_[SLAP_OP_ADD]++;
1113                 break;
1114         case LDAP_REQ_DELETE:
1115                 num_ops_completed_[SLAP_OP_DELETE]++;
1116                 break;
1117         case LDAP_REQ_MODRDN:
1118                 num_ops_completed_[SLAP_OP_MODRDN]++;
1119                 break;
1120         case LDAP_REQ_MODIFY:
1121                 num_ops_completed_[SLAP_OP_MODIFY]++;
1122                 break;
1123         case LDAP_REQ_COMPARE:
1124                 num_ops_completed_[SLAP_OP_COMPARE]++;
1125                 break;
1126         case LDAP_REQ_SEARCH:
1127                 num_ops_completed_[SLAP_OP_SEARCH]++;
1128                 break;
1129         case LDAP_REQ_ABANDON:
1130                 num_ops_completed_[SLAP_OP_ABANDON]++;
1131                 break;
1132         case LDAP_REQ_EXTENDED:
1133                 num_ops_completed_[SLAP_OP_EXTENDED]++;
1134                 break;
1135         default:
1136                 /* this is reachable */
1137                 break;
1138         }
1139 #endif /* SLAPD_MONITOR */
1140         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1141
1142         if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1143                 op->o_cancel = LDAP_TOO_LATE;
1144         }
1145         while ( op->o_cancel != SLAP_CANCEL_NONE &&
1146                 op->o_cancel != SLAP_CANCEL_DONE )
1147         {
1148                 ldap_pvt_thread_yield();
1149         }
1150
1151         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1152
1153         ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1154
1155         if ( op->o_cancel != SLAP_CANCEL_ACK &&
1156                 ( op->o_sync_mode & SLAP_SYNC_PERSIST ) )
1157         {
1158                 sl_mem_detach( ctx, memctx );
1159
1160         } else if ( op->o_sync_slog_size != -1 ) {
1161                 sl_mem_detach( ctx, memctx );
1162                 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1163                 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1164                 conn->c_n_ops_executing--;
1165                 conn->c_n_ops_completed++;
1166
1167         } else {
1168                 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1169                 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1170                 slap_op_free( op );
1171                 conn->c_n_ops_executing--;
1172                 conn->c_n_ops_completed++;
1173         }
1174
1175         switch( tag ) {
1176         case LBER_ERROR:
1177         case LDAP_REQ_UNBIND:
1178                 /* c_mutex is locked */
1179                 connection_closing( conn );
1180                 break;
1181         }
1182
1183         connection_resched( conn );
1184         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1185         return NULL;
1186 }
1187
1188 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1189
1190 int connection_client_setup(
1191         ber_socket_t s,
1192         ldap_pvt_thread_start_t *func,
1193         void *arg )
1194 {
1195         int rc;
1196         Connection *c;
1197
1198         rc = connection_init( s, (Listener *)&dummy_list, "", "",
1199                 CONN_IS_CLIENT, 0, NULL );
1200         if ( rc < 0 ) return -1;
1201
1202         c = connection_get( s );
1203         c->c_clientfunc = func;
1204         c->c_clientarg = arg;
1205         connection_return( c );
1206         slapd_add_internal( s, 0 );
1207         slapd_set_read( s, 1 );
1208         return 0;
1209 }
1210
1211 void connection_client_enable(
1212         ber_socket_t s )
1213 {
1214         slapd_set_read( s, 1 );
1215 }
1216
1217 void connection_client_stop(
1218         ber_socket_t s )
1219 {
1220         Connection *c;
1221
1222         /* get (locked) connection */
1223         c = connection_get( s );
1224         
1225         assert( c->c_conn_state == SLAP_C_CLIENT );
1226
1227         c->c_listener = NULL;
1228         c->c_conn_state = SLAP_C_INVALID;
1229         c->c_struct_state = SLAP_C_UNUSED;
1230         connection_return( c );
1231         slapd_remove( s, 0, 1 );
1232 }
1233
1234 int connection_read(ber_socket_t s)
1235 {
1236         int rc = 0;
1237         Connection *c;
1238
1239         assert( connections != NULL );
1240
1241         ldap_pvt_thread_mutex_lock( &connections_mutex );
1242
1243         /* get (locked) connection */
1244         c = connection_get( s );
1245
1246         if( c == NULL ) {
1247 #ifdef NEW_LOGGING
1248                 LDAP_LOG( CONNECTION, INFO, 
1249                         "connection_read: sock %ld no connection\n", (long)s, 0, 0 );
1250 #else
1251                 Debug( LDAP_DEBUG_ANY,
1252                         "connection_read(%ld): no connection!\n",
1253                         (long) s, 0, 0 );
1254 #endif
1255                 slapd_remove(s, 1, 0);
1256
1257                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1258                 return -1;
1259         }
1260
1261         c->c_n_read++;
1262
1263         if( c->c_conn_state == SLAP_C_CLOSING ) {
1264 #ifdef NEW_LOGGING
1265                 LDAP_LOG( CONNECTION, INFO, 
1266                         "connection_read: conn %lu connection closing, ignoring input\n",
1267                         c->c_connid, 0, 0 );
1268 #else
1269                 Debug( LDAP_DEBUG_TRACE,
1270                         "connection_read(%d): closing, ignoring input for id=%lu\n",
1271                         s, c->c_connid, 0 );
1272 #endif
1273                 connection_return( c );
1274                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1275                 return 0;
1276         }
1277
1278         if ( c->c_conn_state == SLAP_C_CLIENT ) {
1279                 slapd_clr_read( s, 0 );
1280                 ldap_pvt_thread_pool_submit( &connection_pool,
1281                         c->c_clientfunc, c->c_clientarg );
1282                 connection_return( c );
1283                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1284                 return 0;
1285         }
1286
1287 #ifdef NEW_LOGGING
1288         LDAP_LOG( CONNECTION, DETAIL1, 
1289                 "connection_read: conn %lu checking for input.\n", 
1290                         c->c_connid, 0, 0 );
1291 #else
1292         Debug( LDAP_DEBUG_TRACE,
1293                 "connection_read(%d): checking for input on id=%lu\n",
1294                 s, c->c_connid, 0 );
1295 #endif
1296
1297 #ifdef HAVE_TLS
1298         if ( c->c_is_tls && c->c_needs_tls_accept ) {
1299                 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1300                 if ( rc < 0 ) {
1301 #if 0 /* required by next #if 0 */
1302                         struct timeval tv;
1303                         fd_set rfd;
1304 #endif
1305
1306 #ifdef NEW_LOGGING
1307                         LDAP_LOG( CONNECTION, ERR, 
1308                                 "connection_read: conn %lu TLS accept error, error %d\n",
1309                                 c->c_connid, rc, 0 );
1310 #else
1311                         Debug( LDAP_DEBUG_TRACE,
1312                                 "connection_read(%d): TLS accept error "
1313                                 "error=%d id=%lu, closing\n",
1314                                 s, rc, c->c_connid );
1315 #endif
1316                         c->c_needs_tls_accept = 0;
1317                         /* connections_mutex and c_mutex are locked */
1318                         connection_closing( c );
1319
1320 #if 0
1321                         /* Drain input before close, to allow SSL error codes
1322                          * to propagate to client. */
1323                         FD_ZERO(&rfd);
1324                         FD_SET(s, &rfd);
1325                         for (rc=1; rc>0;) {
1326                                 tv.tv_sec = 1;
1327                                 tv.tv_usec = 0;
1328                                 rc = select(s+1, &rfd, NULL, NULL, &tv);
1329                                 if (rc == 1) {
1330                                         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1331                                 }
1332                         }
1333 #endif
1334                         connection_close( c );
1335
1336                 } else if ( rc == 0 ) {
1337                         void *ssl;
1338                         struct berval authid = BER_BVNULL;
1339
1340                         c->c_needs_tls_accept = 0;
1341
1342                         /* we need to let SASL know */
1343                         ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1344
1345                         c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1346                         if( c->c_tls_ssf > c->c_ssf ) {
1347                                 c->c_ssf = c->c_tls_ssf;
1348                         }
1349
1350                         rc = dnX509peerNormalize( ssl, &authid );
1351                         if ( rc != LDAP_SUCCESS ) {
1352 #ifdef NEW_LOGGING
1353                                 LDAP_LOG( CONNECTION, INFO, "connection_read: "
1354                                         "conn %lu unable to get TLS client DN, error %d\n",
1355                                         c->c_connid, rc, 0 );
1356 #else
1357                                 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1358                                         "unable to get TLS client DN, error=%d id=%lu\n",
1359                                         s, rc, c->c_connid );
1360 #endif
1361                         }
1362                         slap_sasl_external( c, c->c_tls_ssf, &authid );
1363                         if ( authid.bv_val ) free( authid.bv_val );
1364                 }
1365
1366                 /* if success and data is ready, fall thru to data input loop */
1367                 if( rc != 0 ||
1368                         !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1369                 {
1370                         connection_return( c );
1371                         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1372                         return 0;
1373                 }
1374         }
1375 #endif
1376
1377 #ifdef HAVE_CYRUS_SASL
1378         if ( c->c_sasl_layers ) {
1379                 /* If previous layer is not removed yet, give up for now */
1380                 if ( !c->c_sasl_sockctx ) {
1381                         connection_return( c );
1382                         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1383                         return 0;
1384                 }
1385
1386                 c->c_sasl_layers = 0;
1387
1388                 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1389
1390                 if( rc != LDAP_SUCCESS ) {
1391 #ifdef NEW_LOGGING
1392                         LDAP_LOG( CONNECTION, ERR, 
1393                                 "connection_read: conn %lu SASL install error %d, closing\n",
1394                                 c->c_connid, rc, 0 );
1395 #else
1396                         Debug( LDAP_DEBUG_TRACE,
1397                                 "connection_read(%d): SASL install error "
1398                                 "error=%d id=%lu, closing\n",
1399                                 s, rc, c->c_connid );
1400 #endif
1401                         /* connections_mutex and c_mutex are locked */
1402                         connection_closing( c );
1403                         connection_close( c );
1404                         connection_return( c );
1405                         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1406                         return 0;
1407                 }
1408         }
1409 #endif
1410
1411 #define CONNECTION_INPUT_LOOP 1
1412 /* #define      DATA_READY_LOOP 1 */
1413
1414         do {
1415                 /* How do we do this without getting into a busy loop ? */
1416                 rc = connection_input( c );
1417         }
1418 #ifdef DATA_READY_LOOP
1419         while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1420 #elif CONNECTION_INPUT_LOOP
1421         while(!rc);
1422 #else
1423         while(0);
1424 #endif
1425
1426         if( rc < 0 ) {
1427 #ifdef NEW_LOGGING
1428                 LDAP_LOG( CONNECTION, ERR, 
1429                         "connection_read: conn %lu input error %d, closing.\n",
1430                         c->c_connid, rc, 0 );
1431 #else
1432                 Debug( LDAP_DEBUG_TRACE,
1433                         "connection_read(%d): input error=%d id=%lu, closing.\n",
1434                         s, rc, c->c_connid );
1435 #endif
1436                 /* connections_mutex and c_mutex are locked */
1437                 connection_closing( c );
1438                 connection_close( c );
1439                 connection_return( c );
1440                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1441                 return 0;
1442         }
1443
1444         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1445                 slapd_set_read( s, 1 );
1446         }
1447
1448         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1449                 slapd_set_write( s, 1 );
1450         }
1451
1452         connection_return( c );
1453         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1454         return 0;
1455 }
1456
1457 static int
1458 connection_input(
1459         Connection *conn )
1460 {
1461         Operation *op;
1462         ber_tag_t       tag;
1463         ber_len_t       len;
1464         ber_int_t       msgid;
1465         BerElement      *ber;
1466         int             rc;
1467 #ifdef LDAP_CONNECTIONLESS
1468         Sockaddr        peeraddr;
1469         char            *cdn = NULL;
1470 #endif
1471         char *defer = NULL;
1472
1473         if ( conn->c_currentber == NULL &&
1474                 ( conn->c_currentber = ber_alloc()) == NULL )
1475         {
1476 #ifdef NEW_LOGGING
1477                 LDAP_LOG( CONNECTION, ERR, 
1478                         "connection_input: conn %lu ber_alloc failed.\n", 
1479                         conn->c_connid, 0, 0 );
1480 #else
1481                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1482 #endif
1483                 return -1;
1484         }
1485
1486         errno = 0;
1487
1488 #ifdef LDAP_CONNECTIONLESS
1489         if ( conn->c_is_udp ) {
1490                 char    peername[sizeof("IP=255.255.255.255:65336")];
1491                 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1492                         sizeof(struct sockaddr));
1493                 if (len != sizeof(struct sockaddr))
1494                         return 1;
1495                 sprintf( peername, "IP=%s:%d",
1496                         inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1497                         (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1498                 Statslog( LDAP_DEBUG_STATS,
1499                         "conn=%lu UDP request from %s (%s) accepted.\n",
1500                         conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1501         }
1502 #endif
1503         tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1504         if ( tag != LDAP_TAG_MESSAGE ) {
1505                 int err = errno;
1506                 ber_socket_t    sd;
1507
1508                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1509
1510 #ifdef NEW_LOGGING
1511                 LDAP_LOG( CONNECTION, ERR, 
1512                         "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1513                         conn->c_connid, err, sock_errstr(err) );
1514 #else
1515                 Debug( LDAP_DEBUG_TRACE,
1516                         "ber_get_next on fd %d failed errno=%d (%s)\n",
1517                         sd, err, sock_errstr(err) );
1518 #endif
1519                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1520                         /* log, close and send error */
1521                         ber_free( conn->c_currentber, 1 );
1522                         conn->c_currentber = NULL;
1523
1524                         return -2;
1525                 }
1526                 return 1;
1527         }
1528
1529         ber = conn->c_currentber;
1530         conn->c_currentber = NULL;
1531
1532         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1533                 /* log, close and send error */
1534 #ifdef NEW_LOGGING
1535                 LDAP_LOG( CONNECTION, ERR, 
1536                         "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1537                         conn->c_connid, tag, 0 );
1538 #else
1539                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n",
1540                         tag, 0, 0 );
1541 #endif
1542                 ber_free( ber, 1 );
1543                 return -1;
1544         }
1545
1546         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1547                 /* log, close and send error */
1548 #ifdef NEW_LOGGING
1549                 LDAP_LOG( CONNECTION, ERR, 
1550                         "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1551                         conn->c_connid, tag, 0 );
1552 #else
1553                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n",
1554                         tag, 0, 0 );
1555 #endif
1556                 ber_free( ber, 1 );
1557
1558                 return -1;
1559         }
1560
1561 #ifdef LDAP_CONNECTIONLESS
1562         if( conn->c_is_udp ) {
1563                 if( tag == LBER_OCTETSTRING ) {
1564                         ber_get_stringa( ber, &cdn );
1565                         tag = ber_peek_tag(ber, &len);
1566                 }
1567                 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1568 #ifdef NEW_LOGGING
1569                         LDAP_LOG( CONNECTION, ERR, 
1570                                 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1571                                 conn->c_connid, tag, 0 );
1572 #else
1573                         Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1574 #endif
1575                         ber_free( ber, 1 );
1576                         return 0;
1577                 }
1578         }
1579 #endif
1580         if(tag == LDAP_REQ_BIND) {
1581                 /* immediately abandon all exiting operations upon BIND */
1582                 connection_abandon( conn );
1583         }
1584
1585         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1586
1587         op->o_conn = conn;
1588         op->o_assertion = NULL;
1589         op->o_preread_attrs = NULL;
1590         op->o_postread_attrs = NULL;
1591         op->o_vrFilter = NULL;
1592         /* clear state if the connection is being reused from inactive */
1593         if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1594                 memset( &conn->c_pagedresults_state, 0, sizeof( conn->c_pagedresults_state ) );
1595         }
1596         op->o_pagedresults_state = conn->c_pagedresults_state;
1597
1598         op->o_res_ber = NULL;
1599
1600 #ifdef LDAP_CONNECTIONLESS
1601         if (conn->c_is_udp) {
1602                 if ( cdn ) {
1603                         ber_str2bv( cdn, 0, 1, &op->o_dn );
1604                         op->o_protocol = LDAP_VERSION2;
1605                 }
1606                 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1607                 if (op->o_res_ber == NULL) return 1;
1608
1609                 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1610                         sizeof(struct sockaddr), 0 );
1611
1612                 if (rc != sizeof(struct sockaddr)) {
1613 #ifdef NEW_LOGGING
1614                         LDAP_LOG( CONNECTION, INFO, 
1615                                 "connection_input: conn %lu ber_write failed\n",
1616                                 conn->c_connid, 0, 0 );
1617 #else
1618                         Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1619 #endif
1620                         return 1;
1621                 }
1622
1623                 if (op->o_protocol == LDAP_VERSION2) {
1624                         rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1625                         if (rc == -1) {
1626 #ifdef NEW_LOGGING
1627                                 LDAP_LOG( CONNECTION, INFO, 
1628                                         "connection_input: conn %lu put outer sequence failed\n",
1629                                         conn->c_connid, 0, 0 );
1630 #else
1631                                 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1632 #endif
1633                                 return rc;
1634                         }
1635                 }
1636         }
1637 #endif /* LDAP_CONNECTIONLESS */
1638
1639         rc = 0;
1640
1641         /* Don't process requests when the conn is in the middle of a
1642          * Bind, or if it's closing. Also, don't let any single conn
1643          * use up all the available threads, and don't execute if we're
1644          * currently blocked on output. And don't execute if there are
1645          * already pending ops, let them go first.  Abandon operations
1646          * get exceptions to some, but not all, cases.
1647          */
1648         if (tag != LDAP_REQ_ABANDON && conn->c_conn_state == SLAP_C_CLOSING) {
1649                 defer = "closing";
1650         } else if (tag != LDAP_REQ_ABANDON && conn->c_writewaiter) {
1651                 defer = "awaiting write";
1652         } else if (conn->c_n_ops_executing >= connection_pool_max/2) {
1653                 defer = "too many executing";
1654         } else if (conn->c_conn_state == SLAP_C_BINDING) {
1655                 defer = "binding";
1656         } else if (tag != LDAP_REQ_ABANDON && conn->c_n_ops_pending) {
1657                 defer = "pending operations";
1658         }
1659
1660         if( defer ) {
1661                 int max = conn->c_dn.bv_len
1662                         ? slap_conn_max_pending_auth
1663                         : slap_conn_max_pending;
1664
1665 #ifdef NEW_LOGGING
1666                 LDAP_LOG( CONNECTION, INFO, 
1667                         "connection_input: conn %lu deferring operation: %s\n",
1668                         conn->c_connid, defer, 0 );
1669 #else
1670                 Debug( LDAP_DEBUG_ANY,
1671                         "connection_input: conn=%lu deferring operation: %s\n",
1672                         conn->c_connid, defer, 0 );
1673 #endif
1674                 conn->c_n_ops_pending++;
1675                 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1676                 if ( conn->c_n_ops_pending > max ) {
1677                         rc = -1;
1678                 } else {
1679                         rc = 1;
1680                 }
1681         } else {
1682                 conn->c_n_ops_executing++;
1683                 connection_op_activate( op );
1684         }
1685
1686 #ifdef NO_THREADS
1687         if ( conn->c_struct_state != SLAP_C_USED ) {
1688                 /* connection must have got closed underneath us */
1689                 return 1;
1690         }
1691 #endif
1692         assert( conn->c_struct_state == SLAP_C_USED );
1693
1694         return rc;
1695 }
1696
1697 static int
1698 connection_resched( Connection *conn )
1699 {
1700         Operation *op;
1701
1702         if( conn->c_conn_state == SLAP_C_CLOSING ) {
1703                 int rc;
1704                 ber_socket_t    sd;
1705                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1706
1707                 /* us trylock to avoid possible deadlock */
1708                 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1709
1710                 if( rc ) {
1711 #ifdef NEW_LOGGING
1712                         LDAP_LOG( CONNECTION, DETAIL1, 
1713                                 "connection_resched: conn %lu reaquiring locks.\n",
1714                                 conn->c_connid, 0, 0 );
1715 #else
1716                         Debug( LDAP_DEBUG_TRACE,
1717                                 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1718                                 conn->c_connid, sd, 0 );
1719 #endif
1720                         /*
1721                          * reaquire locks in the right order...
1722                          * this may allow another thread to close this connection,
1723                          * so recheck state below.
1724                          */
1725                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1726                         ldap_pvt_thread_mutex_lock( &connections_mutex );
1727                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1728                 }
1729
1730                 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1731 #ifdef NEW_LOGGING
1732                         LDAP_LOG( CONNECTION, INFO, 
1733                                 "connection_resched: conn %lu closed by other thread.\n",
1734                                 conn->c_connid, 0, 0 );
1735 #else
1736                         Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1737                                 "closed by other thread conn=%lu sd=%d\n",
1738                                 conn->c_connid, sd, 0 );
1739 #endif
1740                 } else {
1741 #ifdef NEW_LOGGING
1742                         LDAP_LOG( CONNECTION, DETAIL1, 
1743                                 "connection_resched: conn %lu attempting closing.\n",
1744                                 conn->c_connid, 0, 0 );
1745 #else
1746                         Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1747                                 "attempting closing conn=%lu sd=%d\n",
1748                                 conn->c_connid, sd, 0 );
1749 #endif
1750                         connection_close( conn );
1751                 }
1752
1753                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1754                 return 0;
1755         }
1756
1757         if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1758                 /* other states need different handling */
1759                 return 0;
1760         }
1761
1762         while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1763                 if ( conn->c_n_ops_executing > connection_pool_max/2 ) {
1764                         break;
1765                 }
1766                 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1767                 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1768                 /* pending operations should not be marked for abandonment */
1769                 assert(!op->o_abandon);
1770
1771                 conn->c_n_ops_pending--;
1772                 conn->c_n_ops_executing++;
1773
1774                 connection_op_activate( op );
1775
1776                 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1777                         break;
1778                 }
1779         }
1780         return 0;
1781 }
1782
1783 static int connection_bind_cb( Operation *op, SlapReply *rs )
1784 {
1785         slap_callback *cb = op->o_callback;
1786         op->o_callback = cb->sc_next;
1787
1788         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1789         op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1790     op->o_conn->c_sasl_bind_in_progress =
1791         ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1792
1793     /* Moved here from bind.c due to ITS#4158 */
1794     op->o_conn->c_sasl_bindop = NULL;
1795         if ( op->orb_method == LDAP_AUTH_SASL ) {
1796                 if( rs->sr_err == LDAP_SUCCESS ) {
1797                         ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1798                         if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1799                                 /* edn is always normalized already */
1800                                 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1801                         }
1802                         op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1803                         BER_BVZERO( &op->orb_edn );
1804                         op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1805                         BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1806
1807                         op->o_conn->c_sasl_ssf = op->orb_ssf;
1808                         if( op->orb_ssf > op->o_conn->c_ssf ) {
1809                                 op->o_conn->c_ssf = op->orb_ssf;
1810                         }
1811
1812                         if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1813                                 ber_len_t max = sockbuf_max_incoming_auth;
1814                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
1815                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
1816                         }
1817
1818                         /* log authorization identity */
1819                         Statslog( LDAP_DEBUG_STATS,
1820                                 "conn=%lu op=%lu BIND dn=\"%s\" mech=%s ssf=%d\n",
1821                                 op->o_connid, op->o_opid,
1822                                 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
1823                                 op->o_conn->c_authmech.bv_val, op->orb_ssf );
1824
1825                         Debug( LDAP_DEBUG_TRACE,
1826                                 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
1827                                 op->o_conn->c_authmech.bv_val,
1828                                 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
1829                                 op->orb_ssf );
1830
1831                 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1832                         if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1833                                 free( op->o_conn->c_sasl_bind_mech.bv_val );
1834                                 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1835                         }
1836                 }
1837         }
1838         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1839
1840         ch_free( cb );
1841         op->o_callback = NULL;
1842
1843         return SLAP_CB_CONTINUE;
1844 }
1845
1846 static int connection_op_activate( Operation *op )
1847 {
1848         int status;
1849         ber_tag_t tag = op->o_tag;
1850
1851         if(tag == LDAP_REQ_BIND) {
1852                 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1853                 sc->sc_response = connection_bind_cb;
1854                 sc->sc_next = op->o_callback;
1855                 op->o_callback = sc;
1856                 op->o_conn->c_conn_state = SLAP_C_BINDING;
1857         }
1858
1859         if (!op->o_dn.bv_len) {
1860                 op->o_authz = op->o_conn->c_authz;
1861                 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1862                 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1863         }
1864         op->o_authtype = op->o_conn->c_authtype;
1865         ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1866         
1867         if (!op->o_protocol) {
1868                 op->o_protocol = op->o_conn->c_protocol
1869                         ? op->o_conn->c_protocol : LDAP_VERSION3;
1870         }
1871         if (op->o_conn->c_conn_state == SLAP_C_INACTIVE
1872                 && op->o_protocol > LDAP_VERSION2)
1873         {
1874                 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1875         }
1876
1877         op->o_connid = op->o_conn->c_connid;
1878
1879         LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1880
1881         status = ldap_pvt_thread_pool_submit( &connection_pool,
1882                 connection_operation, (void *) op );
1883
1884         if ( status != 0 ) {
1885 #ifdef NEW_LOGGING
1886                 LDAP_LOG( CONNECTION, ERR, 
1887                         "connection_op_activate: conn %lu        thread pool submit failed.\n",
1888                         op->o_connid, 0, 0 );
1889 #else
1890                 Debug( LDAP_DEBUG_ANY,
1891                         "ldap_pvt_thread_pool_submit: failed (%d) for conn=%lu\n",
1892                         status, op->o_connid, 0 );
1893 #endif
1894                 /* should move op to pending list */
1895         }
1896
1897         return status;
1898 }
1899
1900 int connection_write(ber_socket_t s)
1901 {
1902         Connection *c;
1903
1904         assert( connections != NULL );
1905
1906         ldap_pvt_thread_mutex_lock( &connections_mutex );
1907
1908         c = connection_get( s );
1909
1910         slapd_clr_write( s, 0);
1911
1912         if( c == NULL ) {
1913 #ifdef NEW_LOGGING
1914                 LDAP_LOG( CONNECTION, ERR, 
1915                         "connection_write: sock %ld no connection!\n", (long)s, 0, 0);
1916 #else
1917                 Debug( LDAP_DEBUG_ANY,
1918                         "connection_write(%ld): no connection!\n",
1919                         (long)s, 0, 0 );
1920 #endif
1921                 slapd_remove(s, 1, 0);
1922                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1923                 return -1;
1924         }
1925
1926         c->c_n_write++;
1927
1928 #ifdef NEW_LOGGING
1929         LDAP_LOG( CONNECTION, DETAIL1, 
1930                 "connection_write conn %lu waking output.\n", c->c_connid, 0, 0 );
1931 #else
1932         Debug( LDAP_DEBUG_TRACE,
1933                 "connection_write(%d): waking output for id=%lu\n",
1934                 s, c->c_connid, 0 );
1935 #endif
1936         ldap_pvt_thread_cond_signal( &c->c_write_cv );
1937
1938         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1939                 slapd_set_read( s, 1 );
1940         }
1941         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1942                 slapd_set_write( s, 1 );
1943         }
1944         connection_return( c );
1945         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1946         return 0;
1947 }
1948
1949 void
1950 connection_fake_init(
1951         Connection *conn,
1952         Operation *op,
1953         void *ctx )
1954 {
1955         conn->c_connid = -1;
1956         conn->c_send_ldap_result = slap_send_ldap_result;
1957         conn->c_send_search_entry = slap_send_search_entry;
1958         conn->c_send_search_reference = slap_send_search_reference;
1959         conn->c_listener = (Listener *)&dummy_list;
1960         conn->c_peer_domain = slap_empty_bv;
1961         conn->c_peer_name = slap_empty_bv;
1962
1963         /* set memory context */
1964         op->o_tmpmemctx = sl_mem_create( SLMALLOC_SLAB_SIZE, ctx );
1965         op->o_tmpmfuncs = &sl_mfuncs;
1966         op->o_threadctx = ctx;
1967
1968         op->o_conn = conn;
1969         op->o_connid = op->o_conn->c_connid;
1970
1971         op->o_time = slap_get_time();
1972 }
1973
1974 void
1975 connection_assign_nextid( Connection *conn )
1976 {
1977         ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1978         conn->c_connid = conn_nextid++;
1979         ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
1980 }
1981