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