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