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