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