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