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