]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
needs ldap_pvt.h
[openldap] / servers / slapd / connection.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10 #include <limits.h>
11
12 #include <ac/socket.h>
13 #include <ac/errno.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/time.h>
17
18 #include "ldap_pvt.h"
19
20 #include "slap.h"
21
22 /* protected by connections_mutex */
23 static ldap_pvt_thread_mutex_t connections_mutex;
24 static Connection *connections = NULL;
25 static unsigned long conn_nextid = 0;
26
27 /* structure state (protected by connections_mutex) */
28 #define SLAP_C_UNINITIALIZED    0x00    /* MUST BE ZERO (0) */
29 #define SLAP_C_UNUSED                   0x01
30 #define SLAP_C_USED                             0x02
31
32 /* connection state (protected by c_mutex ) */
33 #define SLAP_C_INVALID                  0x00    /* MUST BE ZERO (0) */
34 #define SLAP_C_INACTIVE                 0x01    /* zero threads */
35 #define SLAP_C_ACTIVE                   0x02    /* one or more threads */
36 #define SLAP_C_BINDING                  0x03    /* binding */
37 #define SLAP_C_CLOSING                  0x04    /* closing */
38
39 const char *
40 connection_state2str( int state )
41 {
42         switch( state ) {
43         case SLAP_C_INVALID:    return "!";             
44         case SLAP_C_INACTIVE:   return "|";             
45         case SLAP_C_ACTIVE:             return "";                      
46         case SLAP_C_BINDING:    return "B";
47         case SLAP_C_CLOSING:    return "C";                     
48         }
49
50         return "?";
51 }
52
53 static Connection* connection_get( ber_socket_t s );
54
55 static int connection_input( Connection *c );
56 static void connection_close( Connection *c );
57
58 static int connection_op_activate( Connection *conn, Operation *op );
59 static int connection_resched( Connection *conn );
60 static void connection_abandon( Connection *conn );
61 static void connection_destroy( Connection *c );
62
63 struct co_arg {
64         Connection      *co_conn;
65         Operation       *co_op;
66 };
67
68 /*
69  * Initialize connection management infrastructure.
70  */
71 int connections_init(void)
72 {
73         assert( connections == NULL );
74
75         if( connections != NULL) {
76                 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
77                         0, 0, 0 );
78                 return -1;
79         }
80
81         /* should check return of every call */
82         ldap_pvt_thread_mutex_init( &connections_mutex );
83
84         connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
85
86         if( connections == NULL ) {
87                 Debug( LDAP_DEBUG_ANY,
88                         "connections_init: allocation (%d*%ld) of connection array failed\n",
89                         dtblsize, (long) sizeof(Connection), 0 );
90                 return -1;
91         }
92
93     assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
94     assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
95
96         /*
97          * per entry initialization of the Connection array initialization
98          * will be done by connection_init()
99          */ 
100
101         return 0;
102 }
103
104 /*
105  * Destroy connection management infrastructure.
106  */
107 int connections_destroy(void)
108 {
109         ber_socket_t i;
110
111         /* should check return of every call */
112
113         if( connections == NULL) {
114                 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
115                         0, 0, 0 );
116                 return -1;
117         }
118
119         for ( i = 0; i < dtblsize; i++ ) {
120                 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
121                         ber_sockbuf_free( connections[i].c_sb );
122                         ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
123                         ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
124                         ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
125                 }
126         }
127
128         free( connections );
129         connections = NULL;
130
131         ldap_pvt_thread_mutex_destroy( &connections_mutex );
132         return 0;
133 }
134
135 /*
136  * shutdown all connections
137  */
138 int connections_shutdown(void)
139 {
140         ber_socket_t i;
141
142         ldap_pvt_thread_mutex_lock( &connections_mutex );
143
144         for ( i = 0; i < dtblsize; i++ ) {
145                 if( connections[i].c_struct_state != SLAP_C_USED ) {
146                         continue;
147                 }
148
149                 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
150
151                 /* connections_mutex and c_mutex are locked */
152                 connection_closing( &connections[i] );
153                 connection_close( &connections[i] );
154
155                 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
156         }
157
158         ldap_pvt_thread_mutex_unlock( &connections_mutex );
159
160         return 0;
161 }
162
163 /*
164  * Timeout idle connections.
165  */
166 int connections_timeout_idle(time_t now)
167 {
168         int i = 0;
169         int connindex;
170         Connection* c;
171
172         ldap_pvt_thread_mutex_lock( &connections_mutex );
173
174         for( c = connection_first( &connindex );
175                 c != NULL;
176                 c = connection_next( c, &connindex ) )
177         {
178                 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
179                         /* close it */
180                         connection_closing( c );
181                         connection_close( c );
182                         i++;
183                 }
184         }
185         connection_done( c );
186
187         ldap_pvt_thread_mutex_unlock( &connections_mutex );
188
189         return i;
190 }
191
192 static Connection* connection_get( ber_socket_t s )
193 {
194         /* connections_mutex should be locked by caller */
195
196         Connection *c;
197
198         Debug( LDAP_DEBUG_ARGS,
199                 "connection_get(%ld)\n",
200                 (long) s, 0, 0 );
201
202         assert( connections != NULL );
203
204         if(s == AC_SOCKET_INVALID) {
205                 return NULL;
206         }
207
208 #ifndef HAVE_WINSOCK
209         c = &connections[s];
210
211         assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
212
213 #else
214         c = NULL;
215         {
216                 ber_socket_t i, sd;
217
218                 for(i=0; i<dtblsize; i++) {
219                         ber_sockbuf_ctrl( connections[i].c_sb,
220                                 LBER_SB_OPT_GET_FD, &sd );
221
222                         if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
223                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
224                                 assert( connections[i].c_sb == 0 );
225                                 break;
226                         }
227
228                         if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
229                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
230                                 assert( sd == AC_SOCKET_INVALID );
231                                 continue;
232                         }
233
234                         /* state can actually change from used -> unused by resched,
235                          * so don't assert details here.
236                          */
237
238                         if( sd == s ) {
239                                 c = &connections[i];
240                                 break;
241                         }
242                 }
243         }
244 #endif
245
246         if( c != NULL ) {
247                 ber_socket_t    sd;
248
249                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
250
251                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
252                 if( c->c_struct_state != SLAP_C_USED ) {
253                         /* connection must have been closed due to resched */
254
255                         assert( c->c_conn_state == SLAP_C_INVALID );
256                         assert( sd == AC_SOCKET_INVALID );
257
258                         Debug( LDAP_DEBUG_TRACE,
259                                 "connection_get(%d): connection not used\n",
260                                 s, 0, 0 );
261
262                         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
263                         return NULL;
264                 }
265
266                 Debug( LDAP_DEBUG_TRACE,
267                         "connection_get(%d): got connid=%ld\n",
268                         s, c->c_connid, 0 );
269
270                 c->c_n_get++;
271
272                 assert( c->c_struct_state == SLAP_C_USED );
273                 assert( c->c_conn_state != SLAP_C_INVALID );
274                 assert( sd != AC_SOCKET_INVALID );
275
276         c->c_activitytime = slap_get_time();
277         }
278
279         return c;
280 }
281
282 static void connection_return( Connection *c )
283 {
284         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
285 }
286
287 long connection_init(
288         ber_socket_t s,
289         const char* url,
290         const char* dnsname,
291         const char* peername,
292         const char* sockname,
293         int use_tls,
294         unsigned ssf,
295         char *authid )
296 {
297         unsigned long id;
298         Connection *c;
299
300         assert( connections != NULL );
301
302         assert( dnsname != NULL );
303         assert( peername != NULL );
304         assert( sockname != NULL );
305
306 #ifndef HAVE_TLS
307         assert( !use_tls );
308 #endif
309
310         if( s == AC_SOCKET_INVALID ) {
311         Debug( LDAP_DEBUG_ANY,
312                         "connection_init(%ld): invalid.\n",
313                         (long) s, 0, 0 );
314                 return -1;
315         }
316
317         assert( s >= 0 );
318 #ifndef HAVE_WINSOCK
319         assert( s < dtblsize );
320 #endif
321
322         ldap_pvt_thread_mutex_lock( &connections_mutex );
323
324 #ifndef HAVE_WINSOCK
325         c = &connections[s];
326
327 #else
328         {
329                 unsigned int i;
330
331                 c = NULL;
332
333         for( i=0; i < dtblsize; i++) {
334                 ber_socket_t    sd;
335
336             if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
337                 assert( connections[i].c_sb == 0 );
338                 c = &connections[i];
339                 break;
340             }
341
342                         sd = AC_SOCKET_INVALID;
343                         if (connections[i].c_sb != NULL)
344                         ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
345             
346             if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
347                 assert( sd == AC_SOCKET_INVALID );
348                 c = &connections[i];
349                 break;
350             }
351
352             assert( connections[i].c_struct_state == SLAP_C_USED );
353             assert( connections[i].c_conn_state != SLAP_C_INVALID );
354             assert( sd != AC_SOCKET_INVALID );
355         }
356
357         if( c == NULL ) {
358                 Debug( LDAP_DEBUG_ANY,
359                                 "connection_init(%d): connection table full (%d/%d)\n",
360                                 s, i, dtblsize);
361             ldap_pvt_thread_mutex_unlock( &connections_mutex );
362             return -1;
363         }
364     }
365 #endif
366
367     assert( c != NULL );
368
369     if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
370                 c->c_authmech = NULL;
371         c->c_dn = NULL;
372         c->c_cdn = NULL;
373
374                 c->c_listener_url = NULL;
375                 c->c_peer_domain = NULL;
376         c->c_peer_name = NULL;
377         c->c_sock_name = NULL;
378
379         c->c_ops = NULL;
380         c->c_pending_ops = NULL;
381
382                 c->c_sasl_bind_mech = NULL;
383                 c->c_sasl_context = NULL;
384                 c->c_sasl_extra = NULL;
385
386         c->c_sb = ber_sockbuf_alloc( );
387                 c->c_currentber = NULL;
388
389         /* should check status of thread calls */
390         ldap_pvt_thread_mutex_init( &c->c_mutex );
391         ldap_pvt_thread_mutex_init( &c->c_write_mutex );
392         ldap_pvt_thread_cond_init( &c->c_write_cv );
393
394         c->c_struct_state = SLAP_C_UNUSED;
395     }
396
397     ldap_pvt_thread_mutex_lock( &c->c_mutex );
398
399     assert( c->c_struct_state == SLAP_C_UNUSED );
400         assert( c->c_authmech == NULL );
401     assert(     c->c_dn == NULL );
402     assert(     c->c_cdn == NULL );
403     assert( c->c_listener_url == NULL );
404     assert( c->c_peer_domain == NULL );
405     assert( c->c_peer_name == NULL );
406     assert( c->c_sock_name == NULL );
407     assert( c->c_ops == NULL );
408     assert( c->c_pending_ops == NULL );
409         assert( c->c_sasl_bind_mech == NULL );
410         assert( c->c_sasl_context == NULL );
411         assert( c->c_sasl_extra == NULL );
412         assert( c->c_currentber == NULL );
413
414         c->c_listener_url = ch_strdup( url  );
415         c->c_peer_domain = ch_strdup( dnsname  );
416     c->c_peer_name = ch_strdup( peername  );
417     c->c_sock_name = ch_strdup( sockname );
418
419     c->c_n_ops_received = 0;
420     c->c_n_ops_executing = 0;
421     c->c_n_ops_pending = 0;
422     c->c_n_ops_completed = 0;
423
424         c->c_n_get = 0;
425         c->c_n_read = 0;
426         c->c_n_write = 0;
427
428         /* assume LDAPv3 until bind */
429         c->c_protocol = LDAP_VERSION3;
430
431     c->c_activitytime = c->c_starttime = slap_get_time();
432
433     ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp, LBER_SBIOD_LEVEL_PROVIDER,
434         (void *)&s );
435     ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
436         LBER_SBIOD_LEVEL_PROVIDER, NULL );
437 #ifdef LDAP_DEBUG
438     ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug, INT_MAX, NULL );
439 #endif
440     if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK, c /* non-NULL */ ) < 0 ) {
441         Debug( LDAP_DEBUG_ANY,
442             "connection_init(%d, %s): set nonblocking failed\n",
443             s, c->c_peer_name, 0 );
444     }
445
446
447     id = c->c_connid = conn_nextid++;
448
449     c->c_conn_state = SLAP_C_INACTIVE;
450     c->c_struct_state = SLAP_C_USED;
451
452 #ifdef HAVE_TLS
453     if ( use_tls ) {
454             c->c_is_tls = 1;
455             c->c_needs_tls_accept = 1;
456     } else {
457             c->c_is_tls = 0;
458             c->c_needs_tls_accept = 0;
459     }
460 #endif
461         slap_sasl_open( c );
462         slap_sasl_external( c, ssf, authid );
463
464     ldap_pvt_thread_mutex_unlock( &c->c_mutex );
465     ldap_pvt_thread_mutex_unlock( &connections_mutex );
466
467     backend_connection_init(c);
468
469     return id;
470 }
471
472 static void
473 connection_destroy( Connection *c )
474 {
475         /* note: connections_mutex should be locked by caller */
476     ber_socket_t        sd;
477
478     assert( connections != NULL );
479     assert( c != NULL );
480     assert( c->c_struct_state != SLAP_C_UNUSED );
481     assert( c->c_conn_state != SLAP_C_INVALID );
482     assert( c->c_ops == NULL );
483
484     backend_connection_destroy(c);
485
486     c->c_protocol = 0;
487     c->c_connid = -1;
488
489     c->c_activitytime = c->c_starttime = 0;
490
491         if(c->c_authmech != NULL ) {
492                 free(c->c_authmech);
493                 c->c_authmech = NULL;
494         }
495     if(c->c_dn != NULL) {
496         free(c->c_dn);
497         c->c_dn = NULL;
498     }
499         if(c->c_cdn != NULL) {
500                 free(c->c_cdn);
501                 c->c_cdn = NULL;
502         }
503         if(c->c_listener_url != NULL) {
504                 free(c->c_listener_url);
505                 c->c_listener_url = NULL;
506         }
507         if(c->c_peer_domain != NULL) {
508                 free(c->c_peer_domain);
509                 c->c_peer_domain = NULL;
510         }
511         if(c->c_peer_name != NULL) {
512 #ifdef LDAP_PF_lOCAL
513                 /*
514                  * If peer was a domain socket, unlink. Mind you,
515                  * they may be un-named. Should we leave this to
516                  * the client?
517                  */
518                 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
519                         char *path = c->c_peer_name + 5;
520                         if (path != '\0') {
521                                 (void)unlink(path);
522                         }
523                 }
524 #endif /* LDAP_PF_LOCAL */
525
526                 free(c->c_peer_name);
527                 c->c_peer_name = NULL;
528         }
529         if(c->c_sock_name != NULL) {
530                 free(c->c_sock_name);
531                 c->c_sock_name = NULL;
532         }
533
534         c->c_sasl_bind_in_progress = 0;
535         if(c->c_sasl_bind_mech != NULL) {
536                 free(c->c_sasl_bind_mech);
537                 c->c_sasl_bind_mech = NULL;
538         }
539
540         slap_sasl_close( c );
541
542         if ( c->c_currentber != NULL ) {
543                 ber_free( c->c_currentber, 1 );
544                 c->c_currentber = NULL;
545         }
546
547         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
548         if ( sd != AC_SOCKET_INVALID ) {
549                 slapd_remove( sd, 0 );
550
551                 Statslog( LDAP_DEBUG_STATS,
552                     "conn=%ld fd=%d closed\n",
553                         c->c_connid, sd, 0, 0, 0 );
554         }
555
556         ber_sockbuf_free( c->c_sb );
557         c->c_sb = ber_sockbuf_alloc( );
558
559     c->c_conn_state = SLAP_C_INVALID;
560     c->c_struct_state = SLAP_C_UNUSED;
561 }
562
563 int connection_state_closing( Connection *c )
564 {
565         /* c_mutex must be locked by caller */
566
567         int state;
568         assert( c != NULL );
569         assert( c->c_struct_state == SLAP_C_USED );
570
571         state = c->c_conn_state;
572
573         assert( state != SLAP_C_INVALID );
574
575         return state == SLAP_C_CLOSING;
576 }
577
578 static void connection_abandon( Connection *c )
579 {
580         /* c_mutex must be locked by caller */
581
582         Operation *o;
583
584         for( o = c->c_ops; o != NULL; o = o->o_next ) {
585                 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
586                 o->o_abandon = 1;
587                 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
588         }
589
590         /* remove pending operations */
591         for( o = slap_op_pop( &c->c_pending_ops );
592                 o != NULL;
593                 o = slap_op_pop( &c->c_pending_ops ) )
594         {
595                 slap_op_free( o );
596         }
597 }
598
599 void connection_closing( Connection *c )
600 {
601         assert( connections != NULL );
602         assert( c != NULL );
603         assert( c->c_struct_state == SLAP_C_USED );
604         assert( c->c_conn_state != SLAP_C_INVALID );
605
606         /* c_mutex must be locked by caller */
607
608         if( c->c_conn_state != SLAP_C_CLOSING ) {
609                 ber_socket_t    sd;
610
611                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
612                 Debug( LDAP_DEBUG_TRACE,
613                         "connection_closing: readying conn=%ld sd=%d for close\n",
614                         c->c_connid, sd, 0 );
615
616                 /* update state to closing */
617                 c->c_conn_state = SLAP_C_CLOSING;
618
619                 /* don't listen on this port anymore */
620                 slapd_clr_read( sd, 1 );
621
622                 /* abandon active operations */
623                 connection_abandon( c );
624
625                 /* wake write blocked operations */
626                 slapd_clr_write( sd, 1 );
627                 ldap_pvt_thread_cond_signal( &c->c_write_cv );
628         }
629 }
630
631 static void connection_close( Connection *c )
632 {
633         ber_socket_t    sd;
634
635         assert( connections != NULL );
636         assert( c != NULL );
637         assert( c->c_struct_state == SLAP_C_USED );
638         assert( c->c_conn_state == SLAP_C_CLOSING );
639
640         /* note: connections_mutex and c_mutex should be locked by caller */
641
642         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
643         if( c->c_ops != NULL ) {
644                 Debug( LDAP_DEBUG_TRACE,
645                         "connection_close: deferring conn=%ld sd=%d\n",
646                         c->c_connid, sd, 0 );
647
648                 return;
649         }
650
651         Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
652                 c->c_connid, sd, 0 );
653
654         connection_destroy( c );
655 }
656
657 unsigned long connections_nextid(void)
658 {
659         unsigned long id;
660         assert( connections != NULL );
661
662         ldap_pvt_thread_mutex_lock( &connections_mutex );
663
664         id = conn_nextid;
665
666         ldap_pvt_thread_mutex_unlock( &connections_mutex );
667
668         return id;
669 }
670
671 Connection* connection_first( ber_socket_t *index )
672 {
673         assert( connections != NULL );
674         assert( index != NULL );
675
676         ldap_pvt_thread_mutex_lock( &connections_mutex );
677
678         *index = 0;
679
680         return connection_next(NULL, index);
681 }
682
683 Connection* connection_next( Connection *c, ber_socket_t *index )
684 {
685         assert( connections != NULL );
686         assert( index != NULL );
687         assert( *index <= dtblsize );
688
689         if( c != NULL ) {
690                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
691         }
692
693         c = NULL;
694
695         for(; *index < dtblsize; (*index)++) {
696                 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
697                         assert( connections[*index].c_conn_state == SLAP_C_INVALID );
698 #ifndef HAVE_WINSOCK
699                         continue;
700 #else
701                         break;
702 #endif
703                 }
704
705                 if( connections[*index].c_struct_state == SLAP_C_USED ) {
706                         assert( connections[*index].c_conn_state != SLAP_C_INVALID );
707                         c = &connections[(*index)++];
708                         break;
709                 }
710
711                 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
712                 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
713         }
714
715         if( c != NULL ) {
716                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
717         }
718
719         return c;
720 }
721
722 void connection_done( Connection *c )
723 {
724         assert( connections != NULL );
725
726         if( c != NULL ) {
727                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
728         }
729
730         ldap_pvt_thread_mutex_unlock( &connections_mutex );
731 }
732
733 /*
734  * connection_activity - handle the request operation op on connection
735  * conn.  This routine figures out what kind of operation it is and
736  * calls the appropriate stub to handle it.
737  */
738
739 static void *
740 connection_operation( void *arg_v )
741 {
742         int rc;
743         struct co_arg   *arg = arg_v;
744         ber_tag_t tag = arg->co_op->o_tag;
745         Connection *conn = arg->co_conn;
746
747         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
748         num_ops_initiated++;
749         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
750
751         if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
752                 Debug( LDAP_DEBUG_ANY, "connection_operation: "
753                         "error: SASL bind in progress (tag=%ld).\n",
754                         (long) tag, 0, 0 );
755                 send_ldap_result( conn, arg->co_op,
756                         rc = LDAP_OPERATIONS_ERROR,
757                         NULL, "SASL bind in progress", NULL, NULL );
758                 goto operations_error;
759         }
760
761         switch ( tag ) {
762         case LDAP_REQ_BIND:
763                 rc = do_bind( conn, arg->co_op );
764                 break;
765
766         case LDAP_REQ_UNBIND:
767                 rc = do_unbind( conn, arg->co_op );
768                 break;
769
770         case LDAP_REQ_ADD:
771                 rc = do_add( conn, arg->co_op );
772                 break;
773
774         case LDAP_REQ_DELETE:
775                 rc = do_delete( conn, arg->co_op );
776                 break;
777
778         case LDAP_REQ_MODRDN:
779                 rc = do_modrdn( conn, arg->co_op );
780                 break;
781
782         case LDAP_REQ_MODIFY:
783                 rc = do_modify( conn, arg->co_op );
784                 break;
785
786         case LDAP_REQ_COMPARE:
787                 rc = do_compare( conn, arg->co_op );
788                 break;
789
790         case LDAP_REQ_SEARCH:
791                 rc = do_search( conn, arg->co_op );
792                 break;
793
794         case LDAP_REQ_ABANDON:
795                 rc = do_abandon( conn, arg->co_op );
796                 break;
797
798         case LDAP_REQ_EXTENDED:
799                 rc = do_extended( conn, arg->co_op );
800                 break;
801
802         default:
803                 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
804                     tag, 0, 0 );
805                 arg->co_op->o_tag = LBER_ERROR;
806                 send_ldap_disconnect( conn, arg->co_op,
807                         LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
808                 rc = -1;
809                 break;
810         }
811
812         if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
813
814 operations_error:
815         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
816         num_ops_completed++;
817         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
818
819         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
820
821         conn->c_n_ops_executing--;
822         conn->c_n_ops_completed++;
823
824         slap_op_remove( &conn->c_ops, arg->co_op );
825         slap_op_free( arg->co_op );
826         arg->co_op = NULL;
827         arg->co_conn = NULL;
828         free( (char *) arg );
829         arg = NULL;
830
831         switch( tag ) {
832         case LBER_ERROR:
833         case LDAP_REQ_UNBIND:
834                 /* c_mutex is locked */
835                 connection_closing( conn );
836                 break;
837
838         case LDAP_REQ_BIND:
839                 conn->c_sasl_bind_in_progress =
840                         rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
841
842                 if( conn->c_conn_state == SLAP_C_BINDING) {
843                         conn->c_conn_state = SLAP_C_ACTIVE;
844                 }
845         }
846
847         connection_resched( conn );
848
849         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
850
851         return NULL;
852 }
853
854 int connection_read(ber_socket_t s)
855 {
856         int rc = 0;
857         Connection *c;
858         assert( connections != NULL );
859
860         ldap_pvt_thread_mutex_lock( &connections_mutex );
861
862         /* get (locked) connection */
863         c = connection_get( s );
864
865         if( c == NULL ) {
866                 Debug( LDAP_DEBUG_ANY,
867                         "connection_read(%ld): no connection!\n",
868                         (long) s, 0, 0 );
869
870                 slapd_remove(s, 0);
871
872                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
873                 return -1;
874         }
875
876         c->c_n_read++;
877
878         if( c->c_conn_state == SLAP_C_CLOSING ) {
879                 Debug( LDAP_DEBUG_TRACE,
880                         "connection_read(%d): closing, ignoring input for id=%ld\n",
881                         s, c->c_connid, 0 );
882
883                 connection_return( c );
884                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
885                 return 0;
886         }
887
888         Debug( LDAP_DEBUG_TRACE,
889                 "connection_read(%d): checking for input on id=%ld\n",
890                 s, c->c_connid, 0 );
891
892 #ifdef HAVE_TLS
893         if ( c->c_is_tls && c->c_needs_tls_accept ) {
894                 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
895                 if ( rc < 0 ) {
896                         struct timeval tv;
897                         fd_set rfd;
898
899                         Debug( LDAP_DEBUG_TRACE,
900                                 "connection_read(%d): TLS accept error "
901                                 "error=%d id=%ld, closing\n",
902                                 s, rc, c->c_connid );
903
904                         c->c_needs_tls_accept = 0;
905                         /* connections_mutex and c_mutex are locked */
906                         connection_closing( c );
907
908                         /* Drain input before close, to allow SSL error codes
909                          * to propagate to client. */
910                         FD_ZERO(&rfd);
911                         FD_SET(s, &rfd);
912                         for (rc=1; rc>0;)
913                         {
914                             tv.tv_sec = 1;
915                             tv.tv_usec = 0;
916                             rc = select(s+1, &rfd, NULL, NULL, &tv);
917                             if (rc == 1)
918                                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
919                                     NULL);
920                         }
921                         connection_close( c );
922
923                 } else if ( rc == 0 ) {
924                         void *ssl;
925                         unsigned ssf;
926                         char *authid;
927
928                         c->c_needs_tls_accept = 0;
929
930                         /* we need to let SASL know */
931                         ssl = (void *)ldap_pvt_tls_sb_handle( c->c_sb );
932                         ssf = (unsigned)ldap_pvt_tls_get_strength( ssl );
933                         authid = (char *)ldap_pvt_tls_get_peer( ssl );
934                         slap_sasl_external( c, ssf, authid );
935                 }
936                 connection_return( c );
937                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
938                 return 0;
939         }
940 #endif
941
942 #ifdef HAVE_CYRUS_SASL
943         if ( c->c_sasl_layers ) {
944                 c->c_sasl_layers = 0;
945
946                 rc = ldap_pvt_sasl_install( c->c_sb,  c->c_sasl_context );
947
948                 if( rc != LDAP_SUCCESS ) {
949                         Debug( LDAP_DEBUG_TRACE,
950                                 "connection_read(%d): SASL install error "
951                                 "error=%d id=%ld, closing\n",
952                                 s, rc, c->c_connid );
953
954                         /* connections_mutex and c_mutex are locked */
955                         connection_closing( c );
956                         connection_close( c );
957                         connection_return( c );
958                         ldap_pvt_thread_mutex_unlock( &connections_mutex );
959                         return 0;
960                 }
961         }
962 #endif
963
964 #define CONNECTION_INPUT_LOOP 1
965
966 #ifdef DATA_READY_LOOP
967         while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
968 #elif CONNECTION_INPUT_LOOP
969         while(!rc)
970 #endif
971         {
972                 /* How do we do this without getting into a busy loop ? */
973                 rc = connection_input( c );
974         }
975
976         if( rc < 0 ) {
977                 Debug( LDAP_DEBUG_TRACE,
978                         "connection_read(%d): input error=%d id=%ld, closing.\n",
979                         s, rc, c->c_connid );
980
981                 /* connections_mutex and c_mutex are locked */
982                 connection_closing( c );
983                 connection_close( c );
984         }
985
986         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
987                 slapd_set_read( s, 1 );
988         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
989                 slapd_set_write( s, 1 );
990         connection_return( c );
991         ldap_pvt_thread_mutex_unlock( &connections_mutex );
992         return 0;
993 }
994
995 static int
996 connection_input(
997     Connection *conn
998 )
999 {
1000         Operation *op;
1001         ber_tag_t       tag;
1002         ber_len_t       len;
1003         ber_int_t       msgid;
1004         BerElement      *ber;
1005
1006         if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1007             == NULL ) {
1008                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1009                 return -1;
1010         }
1011
1012         errno = 0;
1013
1014         tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1015         if ( tag != LDAP_TAG_MESSAGE ) {
1016                 int err = errno;
1017                 ber_socket_t    sd;
1018
1019                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1020
1021                 Debug( LDAP_DEBUG_TRACE,
1022                         "ber_get_next on fd %d failed errno=%d (%s)\n",
1023                         sd, err, sock_errstr(err) );
1024
1025                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1026                         /* log, close and send error */
1027                         ber_free( conn->c_currentber, 1 );
1028                         conn->c_currentber = NULL;
1029
1030                         return -2;
1031                 }
1032                 return 1;
1033         }
1034
1035         ber = conn->c_currentber;
1036         conn->c_currentber = NULL;
1037
1038         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1039                 /* log, close and send error */
1040                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1041                     0 );
1042                 ber_free( ber, 1 );
1043                 return -1;
1044         }
1045
1046         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1047                 /* log, close and send error */
1048                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1049                     0 );
1050                 ber_free( ber, 1 );
1051
1052                 return -1;
1053         }
1054
1055         if(tag == LDAP_REQ_BIND) {
1056                 /* immediately abandon all exiting operations upon BIND */
1057                 connection_abandon( conn );
1058         }
1059
1060         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1061
1062         if ( conn->c_conn_state == SLAP_C_BINDING
1063                 || conn->c_conn_state == SLAP_C_CLOSING )
1064         {
1065                 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1066                 conn->c_n_ops_pending++;
1067                 slap_op_add( &conn->c_pending_ops, op );
1068
1069         } else {
1070                 conn->c_n_ops_executing++;
1071                 connection_op_activate( conn, op );
1072         }
1073
1074 #ifdef NO_THREADS
1075         if ( conn->c_struct_state != SLAP_C_USED ) {
1076                 /* connection must have got closed underneath us */
1077                 return 1;
1078         }
1079 #endif
1080         assert( conn->c_struct_state == SLAP_C_USED );
1081
1082         return 0;
1083 }
1084
1085 static int
1086 connection_resched( Connection *conn )
1087 {
1088         Operation *op;
1089
1090         if( conn->c_conn_state == SLAP_C_CLOSING ) {
1091                 ber_socket_t    sd;
1092
1093                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1094                 Debug( LDAP_DEBUG_TRACE,
1095                         "connection_resched: attempting closing conn=%ld sd=%d\n",
1096                         conn->c_connid, sd, 0 );
1097
1098                 ldap_pvt_thread_mutex_lock( &connections_mutex );
1099                 connection_close( conn );
1100                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1101                 return 0;
1102         }
1103
1104         if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1105                 /* other states need different handling */
1106                 return 0;
1107         }
1108
1109         for( op = slap_op_pop( &conn->c_pending_ops );
1110                 op != NULL;
1111                 op = slap_op_pop( &conn->c_pending_ops ) )
1112         {
1113                 /* pending operations should not be marked for abandonment */
1114                 assert(!op->o_abandon);
1115
1116                 conn->c_n_ops_pending--;
1117                 conn->c_n_ops_executing++;
1118
1119                 connection_op_activate( conn, op );
1120
1121                 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1122                         break;
1123                 }
1124         }
1125         return 0;
1126 }
1127
1128 static int connection_op_activate( Connection *conn, Operation *op )
1129 {
1130         struct co_arg *arg;
1131         int status;
1132         ber_tag_t tag = op->o_tag;
1133
1134         if(tag == LDAP_REQ_BIND) {
1135                 conn->c_conn_state = SLAP_C_BINDING;
1136         }
1137
1138         arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1139         arg->co_conn = conn;
1140         arg->co_op = op;
1141
1142         arg->co_op->o_dn = ch_strdup( conn->c_dn != NULL ? conn->c_dn : "" );
1143         arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1144         (void) dn_normalize( arg->co_op->o_ndn );
1145
1146         arg->co_op->o_protocol = conn->c_protocol;
1147         arg->co_op->o_connid = conn->c_connid;
1148
1149         arg->co_op->o_authtype = conn->c_authtype;
1150         arg->co_op->o_authmech = conn->c_authmech != NULL
1151                 ?  ch_strdup( conn->c_authmech ) : NULL;
1152         
1153         slap_op_add( &conn->c_ops, arg->co_op );
1154
1155         status = ldap_pvt_thread_pool_submit( &connection_pool,
1156                 connection_operation, (void *) arg );
1157
1158         if ( status != 0 ) {
1159                 Debug( LDAP_DEBUG_ANY,
1160                 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1161                 /* should move op to pending list */
1162         }
1163
1164         return status;
1165 }
1166
1167 int connection_write(ber_socket_t s)
1168 {
1169         Connection *c;
1170         assert( connections != NULL );
1171
1172         ldap_pvt_thread_mutex_lock( &connections_mutex );
1173
1174         c = connection_get( s );
1175
1176         slapd_clr_write( s, 0);
1177
1178         if( c == NULL ) {
1179                 Debug( LDAP_DEBUG_ANY,
1180                         "connection_write(%ld): no connection!\n",
1181                         (long) s, 0, 0 );
1182                 slapd_remove(s, 0);
1183                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1184                 return -1;
1185         }
1186
1187         c->c_n_write++;
1188
1189         Debug( LDAP_DEBUG_TRACE,
1190                 "connection_write(%d): waking output for id=%ld\n",
1191                 s, c->c_connid, 0 );
1192
1193         ldap_pvt_thread_cond_signal( &c->c_write_cv );
1194
1195         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1196                 slapd_set_read( s, 1 );
1197         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1198                 slapd_set_write( s, 1 );
1199         connection_return( c );
1200         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1201         return 0;
1202 }