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