]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
c8ad754d6193d25e4c4f1ee895b71f478479dd17
[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         unsigned ssf,
293         char *authid )
294 {
295         unsigned long id;
296         Connection *c;
297
298         assert( connections != NULL );
299
300         assert( dnsname != NULL );
301         assert( peername != NULL );
302         assert( sockname != NULL );
303
304 #ifndef HAVE_TLS
305         assert( !use_tls );
306 #endif
307
308         if( s == AC_SOCKET_INVALID ) {
309         Debug( LDAP_DEBUG_ANY,
310                         "connection_init(%ld): invalid.\n",
311                         (long) s, 0, 0 );
312                 return -1;
313         }
314
315         assert( s >= 0 );
316 #ifndef HAVE_WINSOCK
317         assert( s < dtblsize );
318 #endif
319
320         ldap_pvt_thread_mutex_lock( &connections_mutex );
321
322 #ifndef HAVE_WINSOCK
323         c = &connections[s];
324
325 #else
326         {
327                 unsigned int i;
328
329                 c = NULL;
330
331         for( i=0; i < dtblsize; i++) {
332                 ber_socket_t    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                         sd = AC_SOCKET_INVALID;
341                         if (connections[i].c_sb != NULL)
342                         ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
343             
344             if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
345                 assert( sd == AC_SOCKET_INVALID );
346                 c = &connections[i];
347                 break;
348             }
349
350             assert( connections[i].c_struct_state == SLAP_C_USED );
351             assert( connections[i].c_conn_state != SLAP_C_INVALID );
352             assert( sd != AC_SOCKET_INVALID );
353         }
354
355         if( c == NULL ) {
356                 Debug( LDAP_DEBUG_ANY,
357                                 "connection_init(%d): connection table full (%d/%d)\n",
358                                 s, i, dtblsize);
359             ldap_pvt_thread_mutex_unlock( &connections_mutex );
360             return -1;
361         }
362     }
363 #endif
364
365     assert( c != NULL );
366
367     if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
368                 c->c_authmech = NULL;
369         c->c_dn = NULL;
370         c->c_cdn = NULL;
371
372                 c->c_listener_url = NULL;
373                 c->c_peer_domain = NULL;
374         c->c_peer_name = NULL;
375         c->c_sock_name = NULL;
376
377         c->c_ops = NULL;
378         c->c_pending_ops = NULL;
379
380                 c->c_sasl_bind_mech = NULL;
381                 c->c_sasl_context = NULL;
382                 c->c_sasl_extra = NULL;
383
384         c->c_sb = ber_sockbuf_alloc( );
385                 c->c_currentber = NULL;
386
387         /* should check status of thread calls */
388         ldap_pvt_thread_mutex_init( &c->c_mutex );
389         ldap_pvt_thread_mutex_init( &c->c_write_mutex );
390         ldap_pvt_thread_cond_init( &c->c_write_cv );
391
392         c->c_struct_state = SLAP_C_UNUSED;
393     }
394
395     ldap_pvt_thread_mutex_lock( &c->c_mutex );
396
397     assert( c->c_struct_state == SLAP_C_UNUSED );
398         assert( c->c_authmech == NULL );
399     assert(     c->c_dn == NULL );
400     assert(     c->c_cdn == NULL );
401     assert( c->c_listener_url == NULL );
402     assert( c->c_peer_domain == NULL );
403     assert( c->c_peer_name == NULL );
404     assert( c->c_sock_name == NULL );
405     assert( c->c_ops == NULL );
406     assert( c->c_pending_ops == NULL );
407         assert( c->c_sasl_bind_mech == NULL );
408         assert( c->c_sasl_context == NULL );
409         assert( c->c_sasl_extra == NULL );
410         assert( c->c_currentber == NULL );
411
412         c->c_listener_url = ch_strdup( url  );
413         c->c_peer_domain = ch_strdup( dnsname  );
414     c->c_peer_name = ch_strdup( peername  );
415     c->c_sock_name = ch_strdup( sockname );
416
417     c->c_n_ops_received = 0;
418     c->c_n_ops_executing = 0;
419     c->c_n_ops_pending = 0;
420     c->c_n_ops_completed = 0;
421
422         c->c_n_get = 0;
423         c->c_n_read = 0;
424         c->c_n_write = 0;
425
426         /* assume LDAPv3 until bind */
427         c->c_protocol = LDAP_VERSION3;
428
429     c->c_activitytime = c->c_starttime = slap_get_time();
430
431     ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp, LBER_SBIOD_LEVEL_PROVIDER,
432         (void *)&s );
433     ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
434         LBER_SBIOD_LEVEL_PROVIDER, NULL );
435 #ifdef LDAP_DEBUG
436     ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug, INT_MAX, NULL );
437 #endif
438     if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK, c /* non-NULL */ ) < 0 ) {
439         Debug( LDAP_DEBUG_ANY,
440             "connection_init(%d, %s): set nonblocking failed\n",
441             s, c->c_peer_name, 0 );
442     }
443
444
445     id = c->c_connid = conn_nextid++;
446
447     c->c_conn_state = SLAP_C_INACTIVE;
448     c->c_struct_state = SLAP_C_USED;
449
450 #ifdef HAVE_TLS
451     if ( use_tls ) {
452             c->c_is_tls = 1;
453             c->c_needs_tls_accept = 1;
454     } else {
455             c->c_is_tls = 0;
456             c->c_needs_tls_accept = 0;
457     }
458 #endif
459         slap_sasl_open( c );
460         slap_sasl_external( c, ssf, authid );
461
462     ldap_pvt_thread_mutex_unlock( &c->c_mutex );
463     ldap_pvt_thread_mutex_unlock( &connections_mutex );
464
465     backend_connection_init(c);
466
467     return id;
468 }
469
470 static void
471 connection_destroy( Connection *c )
472 {
473         /* note: connections_mutex should be locked by caller */
474     ber_socket_t        sd;
475
476     assert( connections != NULL );
477     assert( c != NULL );
478     assert( c->c_struct_state != SLAP_C_UNUSED );
479     assert( c->c_conn_state != SLAP_C_INVALID );
480     assert( c->c_ops == NULL );
481
482     backend_connection_destroy(c);
483
484     c->c_protocol = 0;
485     c->c_connid = -1;
486
487     c->c_activitytime = c->c_starttime = 0;
488
489         if(c->c_authmech != NULL ) {
490                 free(c->c_authmech);
491                 c->c_authmech = NULL;
492         }
493     if(c->c_dn != NULL) {
494         free(c->c_dn);
495         c->c_dn = NULL;
496     }
497         if(c->c_cdn != NULL) {
498                 free(c->c_cdn);
499                 c->c_cdn = NULL;
500         }
501         if(c->c_listener_url != NULL) {
502                 free(c->c_listener_url);
503                 c->c_listener_url = NULL;
504         }
505         if(c->c_peer_domain != NULL) {
506                 free(c->c_peer_domain);
507                 c->c_peer_domain = NULL;
508         }
509         if(c->c_peer_name != NULL) {
510 #ifdef LDAP_PF_lOCAL
511                 /*
512                  * If peer was a domain socket, unlink. Mind you,
513                  * they may be un-named. Should we leave this to
514                  * the client?
515                  */
516                 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
517                         char *path = c->c_peer_name + 5;
518                         if (path != '\0') {
519                                 (void)unlink(path);
520                         }
521                 }
522 #endif /* LDAP_PF_LOCAL */
523
524                 free(c->c_peer_name);
525                 c->c_peer_name = NULL;
526         }
527         if(c->c_sock_name != NULL) {
528                 free(c->c_sock_name);
529                 c->c_sock_name = NULL;
530         }
531
532         c->c_sasl_bind_in_progress = 0;
533         if(c->c_sasl_bind_mech != NULL) {
534                 free(c->c_sasl_bind_mech);
535                 c->c_sasl_bind_mech = NULL;
536         }
537
538         slap_sasl_close( c );
539
540         if ( c->c_currentber != NULL ) {
541                 ber_free( c->c_currentber, 1 );
542                 c->c_currentber = NULL;
543         }
544
545         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
546         if ( sd != AC_SOCKET_INVALID ) {
547                 slapd_remove( sd, 0 );
548
549                 Statslog( LDAP_DEBUG_STATS,
550                     "conn=%ld fd=%d closed\n",
551                         c->c_connid, sd, 0, 0, 0 );
552         }
553
554         ber_sockbuf_free( c->c_sb );
555         c->c_sb = ber_sockbuf_alloc( );
556
557     c->c_conn_state = SLAP_C_INVALID;
558     c->c_struct_state = SLAP_C_UNUSED;
559 }
560
561 int connection_state_closing( Connection *c )
562 {
563         /* c_mutex must be locked by caller */
564
565         int state;
566         assert( c != NULL );
567         assert( c->c_struct_state == SLAP_C_USED );
568
569         state = c->c_conn_state;
570
571         assert( state != SLAP_C_INVALID );
572
573         return state == SLAP_C_CLOSING;
574 }
575
576 static void connection_abandon( Connection *c )
577 {
578         /* c_mutex must be locked by caller */
579
580         Operation *o;
581
582         for( o = c->c_ops; o != NULL; o = o->o_next ) {
583                 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
584                 o->o_abandon = 1;
585                 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
586         }
587
588         /* remove pending operations */
589         for( o = slap_op_pop( &c->c_pending_ops );
590                 o != NULL;
591                 o = slap_op_pop( &c->c_pending_ops ) )
592         {
593                 slap_op_free( o );
594         }
595 }
596
597 void connection_closing( Connection *c )
598 {
599         assert( connections != NULL );
600         assert( c != NULL );
601         assert( c->c_struct_state == SLAP_C_USED );
602         assert( c->c_conn_state != SLAP_C_INVALID );
603
604         /* c_mutex must be locked by caller */
605
606         if( c->c_conn_state != SLAP_C_CLOSING ) {
607                 ber_socket_t    sd;
608
609                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
610                 Debug( LDAP_DEBUG_TRACE,
611                         "connection_closing: readying conn=%ld sd=%d for close\n",
612                         c->c_connid, sd, 0 );
613
614                 /* update state to closing */
615                 c->c_conn_state = SLAP_C_CLOSING;
616
617                 /* don't listen on this port anymore */
618                 slapd_clr_read( sd, 1 );
619
620                 /* abandon active operations */
621                 connection_abandon( c );
622
623                 /* wake write blocked operations */
624                 slapd_clr_write( sd, 1 );
625                 ldap_pvt_thread_cond_signal( &c->c_write_cv );
626         }
627 }
628
629 static void connection_close( Connection *c )
630 {
631         ber_socket_t    sd;
632
633         assert( connections != NULL );
634         assert( c != NULL );
635         assert( c->c_struct_state == SLAP_C_USED );
636         assert( c->c_conn_state == SLAP_C_CLOSING );
637
638         /* note: connections_mutex and c_mutex should be locked by caller */
639
640         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
641         if( c->c_ops != NULL ) {
642                 Debug( LDAP_DEBUG_TRACE,
643                         "connection_close: deferring conn=%ld sd=%d\n",
644                         c->c_connid, sd, 0 );
645
646                 return;
647         }
648
649         Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
650                 c->c_connid, sd, 0 );
651
652         connection_destroy( c );
653 }
654
655 unsigned long connections_nextid(void)
656 {
657         unsigned long id;
658         assert( connections != NULL );
659
660         ldap_pvt_thread_mutex_lock( &connections_mutex );
661
662         id = conn_nextid;
663
664         ldap_pvt_thread_mutex_unlock( &connections_mutex );
665
666         return id;
667 }
668
669 Connection* connection_first( ber_socket_t *index )
670 {
671         assert( connections != NULL );
672         assert( index != NULL );
673
674         ldap_pvt_thread_mutex_lock( &connections_mutex );
675
676         *index = 0;
677
678         return connection_next(NULL, index);
679 }
680
681 Connection* connection_next( Connection *c, ber_socket_t *index )
682 {
683         assert( connections != NULL );
684         assert( index != NULL );
685         assert( *index <= dtblsize );
686
687         if( c != NULL ) {
688                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
689         }
690
691         c = NULL;
692
693         for(; *index < dtblsize; (*index)++) {
694                 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
695                         assert( connections[*index].c_conn_state == SLAP_C_INVALID );
696 #ifndef HAVE_WINSOCK
697                         continue;
698 #else
699                         break;
700 #endif
701                 }
702
703                 if( connections[*index].c_struct_state == SLAP_C_USED ) {
704                         assert( connections[*index].c_conn_state != SLAP_C_INVALID );
705                         c = &connections[(*index)++];
706                         break;
707                 }
708
709                 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
710                 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
711         }
712
713         if( c != NULL ) {
714                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
715         }
716
717         return c;
718 }
719
720 void connection_done( Connection *c )
721 {
722         assert( connections != NULL );
723
724         if( c != NULL ) {
725                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
726         }
727
728         ldap_pvt_thread_mutex_unlock( &connections_mutex );
729 }
730
731 /*
732  * connection_activity - handle the request operation op on connection
733  * conn.  This routine figures out what kind of operation it is and
734  * calls the appropriate stub to handle it.
735  */
736
737 static void *
738 connection_operation( void *arg_v )
739 {
740         int rc;
741         struct co_arg   *arg = arg_v;
742         ber_tag_t tag = arg->co_op->o_tag;
743         Connection *conn = arg->co_conn;
744
745         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
746         num_ops_initiated++;
747         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
748
749         if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
750                 Debug( LDAP_DEBUG_ANY, "connection_operation: "
751                         "error: SASL bind in progress (tag=%ld).\n",
752                         (long) tag, 0, 0 );
753                 send_ldap_result( conn, arg->co_op,
754                         rc = LDAP_OPERATIONS_ERROR,
755                         NULL, "SASL bind in progress", NULL, NULL );
756                 goto operations_error;
757         }
758
759         switch ( tag ) {
760         case LDAP_REQ_BIND:
761                 rc = do_bind( conn, arg->co_op );
762                 break;
763
764         case LDAP_REQ_UNBIND:
765                 rc = do_unbind( conn, arg->co_op );
766                 break;
767
768         case LDAP_REQ_ADD:
769                 rc = do_add( conn, arg->co_op );
770                 break;
771
772         case LDAP_REQ_DELETE:
773                 rc = do_delete( conn, arg->co_op );
774                 break;
775
776         case LDAP_REQ_MODRDN:
777                 rc = do_modrdn( conn, arg->co_op );
778                 break;
779
780         case LDAP_REQ_MODIFY:
781                 rc = do_modify( conn, arg->co_op );
782                 break;
783
784         case LDAP_REQ_COMPARE:
785                 rc = do_compare( conn, arg->co_op );
786                 break;
787
788         case LDAP_REQ_SEARCH:
789                 rc = do_search( conn, arg->co_op );
790                 break;
791
792         case LDAP_REQ_ABANDON:
793                 rc = do_abandon( conn, arg->co_op );
794                 break;
795
796         case LDAP_REQ_EXTENDED:
797                 rc = do_extended( conn, arg->co_op );
798                 break;
799
800         default:
801                 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
802                     tag, 0, 0 );
803                 arg->co_op->o_tag = LBER_ERROR;
804                 send_ldap_disconnect( conn, arg->co_op,
805                         LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
806                 rc = -1;
807                 break;
808         }
809
810         if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
811
812 operations_error:
813         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
814         num_ops_completed++;
815         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
816
817         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
818
819         conn->c_n_ops_executing--;
820         conn->c_n_ops_completed++;
821
822         slap_op_remove( &conn->c_ops, arg->co_op );
823         slap_op_free( arg->co_op );
824         arg->co_op = NULL;
825         arg->co_conn = NULL;
826         free( (char *) arg );
827         arg = NULL;
828
829         switch( tag ) {
830         case LBER_ERROR:
831         case LDAP_REQ_UNBIND:
832                 /* c_mutex is locked */
833                 connection_closing( conn );
834                 break;
835
836         case LDAP_REQ_BIND:
837                 conn->c_sasl_bind_in_progress =
838                         rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
839
840                 if( conn->c_conn_state == SLAP_C_BINDING) {
841                         conn->c_conn_state = SLAP_C_ACTIVE;
842                 }
843         }
844
845         connection_resched( conn );
846
847         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
848
849         return NULL;
850 }
851
852 int connection_read(ber_socket_t s)
853 {
854         int rc = 0;
855         Connection *c;
856         assert( connections != NULL );
857
858         ldap_pvt_thread_mutex_lock( &connections_mutex );
859
860         /* get (locked) connection */
861         c = connection_get( s );
862
863         if( c == NULL ) {
864                 Debug( LDAP_DEBUG_ANY,
865                         "connection_read(%ld): no connection!\n",
866                         (long) s, 0, 0 );
867
868                 slapd_remove(s, 0);
869
870                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
871                 return -1;
872         }
873
874         c->c_n_read++;
875
876         if( c->c_conn_state == SLAP_C_CLOSING ) {
877                 Debug( LDAP_DEBUG_TRACE,
878                         "connection_read(%d): closing, ignoring input for id=%ld\n",
879                         s, c->c_connid, 0 );
880
881                 connection_return( c );
882                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
883                 return 0;
884         }
885
886         Debug( LDAP_DEBUG_TRACE,
887                 "connection_read(%d): checking for input on id=%ld\n",
888                 s, c->c_connid, 0 );
889
890 #ifdef HAVE_TLS
891         if ( c->c_is_tls && c->c_needs_tls_accept ) {
892                 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
893                 if ( rc < 0 ) {
894                         struct timeval tv;
895                         fd_set rfd;
896
897                         Debug( LDAP_DEBUG_TRACE,
898                                 "connection_read(%d): TLS accept error "
899                                 "error=%d id=%ld, closing\n",
900                                 s, rc, c->c_connid );
901
902                         c->c_needs_tls_accept = 0;
903                         /* connections_mutex and c_mutex are locked */
904                         connection_closing( c );
905
906                         /* Drain input before close, to allow SSL error codes
907                          * to propagate to client. */
908                         FD_ZERO(&rfd);
909                         FD_SET(s, &rfd);
910                         for (rc=1; rc>0;)
911                         {
912                             tv.tv_sec = 1;
913                             tv.tv_usec = 0;
914                             rc = select(s+1, &rfd, NULL, NULL, &tv);
915                             if (rc == 1)
916                                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
917                                     NULL);
918                         }
919                         connection_close( c );
920
921                 } else if ( rc == 0 ) {
922                         void *ssl;
923                         unsigned ssf;
924                         char *authid;
925
926                         c->c_needs_tls_accept = 0;
927
928                         /* we need to let SASL know */
929                         ssl = (void *)ldap_pvt_tls_sb_handle( c->c_sb );
930                         ssf = (unsigned)ldap_pvt_tls_get_strength( ssl );
931                         authid = (char *)ldap_pvt_tls_get_peer( ssl );
932                         slap_sasl_external( c, ssf, authid );
933                 }
934                 connection_return( c );
935                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
936                 return 0;
937         }
938 #endif
939
940 #ifdef HAVE_CYRUS_SASL
941         if ( c->c_sasl_layers ) {
942                 c->c_sasl_layers = 0;
943
944                 rc = ldap_pvt_sasl_install( c->c_sb,  c->c_sasl_context );
945
946                 if( rc != LDAP_SUCCESS ) {
947                         Debug( LDAP_DEBUG_TRACE,
948                                 "connection_read(%d): SASL install error "
949                                 "error=%d id=%ld, closing\n",
950                                 s, rc, c->c_connid );
951
952                         /* connections_mutex and c_mutex are locked */
953                         connection_closing( c );
954                         connection_close( c );
955                         connection_return( c );
956                         ldap_pvt_thread_mutex_unlock( &connections_mutex );
957                         return 0;
958                 }
959         }
960 #endif
961
962 #define CONNECTION_INPUT_LOOP 1
963
964 #ifdef DATA_READY_LOOP
965         while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
966 #elif CONNECTION_INPUT_LOOP
967         while(!rc)
968 #endif
969         {
970                 /* How do we do this without getting into a busy loop ? */
971                 rc = connection_input( c );
972         }
973
974         if( rc < 0 ) {
975                 Debug( LDAP_DEBUG_TRACE,
976                         "connection_read(%d): input error=%d id=%ld, closing.\n",
977                         s, rc, c->c_connid );
978
979                 /* connections_mutex and c_mutex are locked */
980                 connection_closing( c );
981                 connection_close( c );
982         }
983
984         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
985                 slapd_set_read( s, 1 );
986         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
987                 slapd_set_write( s, 1 );
988         connection_return( c );
989         ldap_pvt_thread_mutex_unlock( &connections_mutex );
990         return 0;
991 }
992
993 static int
994 connection_input(
995     Connection *conn
996 )
997 {
998         Operation *op;
999         ber_tag_t       tag;
1000         ber_len_t       len;
1001         ber_int_t       msgid;
1002         BerElement      *ber;
1003
1004         if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1005             == NULL ) {
1006                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1007                 return -1;
1008         }
1009
1010         errno = 0;
1011
1012         tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1013         if ( tag != LDAP_TAG_MESSAGE ) {
1014                 int err = errno;
1015                 ber_socket_t    sd;
1016
1017                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1018
1019                 Debug( LDAP_DEBUG_TRACE,
1020                         "ber_get_next on fd %d failed errno=%d (%s)\n",
1021                         sd, err, sock_errstr(err) );
1022
1023                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1024                         /* log, close and send error */
1025                         ber_free( conn->c_currentber, 1 );
1026                         conn->c_currentber = NULL;
1027
1028                         return -2;
1029                 }
1030                 return 1;
1031         }
1032
1033         ber = conn->c_currentber;
1034         conn->c_currentber = NULL;
1035
1036         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1037                 /* log, close and send error */
1038                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1039                     0 );
1040                 ber_free( ber, 1 );
1041                 return -1;
1042         }
1043
1044         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1045                 /* log, close and send error */
1046                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1047                     0 );
1048                 ber_free( ber, 1 );
1049
1050                 return -1;
1051         }
1052
1053         if(tag == LDAP_REQ_BIND) {
1054                 /* immediately abandon all exiting operations upon BIND */
1055                 connection_abandon( conn );
1056         }
1057
1058         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1059
1060         if ( conn->c_conn_state == SLAP_C_BINDING
1061                 || conn->c_conn_state == SLAP_C_CLOSING )
1062         {
1063                 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1064                 conn->c_n_ops_pending++;
1065                 slap_op_add( &conn->c_pending_ops, op );
1066
1067         } else {
1068                 conn->c_n_ops_executing++;
1069                 connection_op_activate( conn, op );
1070         }
1071
1072 #ifdef NO_THREADS
1073         if ( conn->c_struct_state != SLAP_C_USED ) {
1074                 /* connection must have got closed underneath us */
1075                 return 1;
1076         }
1077 #endif
1078         assert( conn->c_struct_state == SLAP_C_USED );
1079
1080         return 0;
1081 }
1082
1083 static int
1084 connection_resched( Connection *conn )
1085 {
1086         Operation *op;
1087
1088         if( conn->c_conn_state == SLAP_C_CLOSING ) {
1089                 ber_socket_t    sd;
1090
1091                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1092                 Debug( LDAP_DEBUG_TRACE,
1093                         "connection_resched: attempting closing conn=%ld sd=%d\n",
1094                         conn->c_connid, sd, 0 );
1095
1096                 ldap_pvt_thread_mutex_lock( &connections_mutex );
1097                 connection_close( conn );
1098                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1099                 return 0;
1100         }
1101
1102         if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1103                 /* other states need different handling */
1104                 return 0;
1105         }
1106
1107         for( op = slap_op_pop( &conn->c_pending_ops );
1108                 op != NULL;
1109                 op = slap_op_pop( &conn->c_pending_ops ) )
1110         {
1111                 /* pending operations should not be marked for abandonment */
1112                 assert(!op->o_abandon);
1113
1114                 conn->c_n_ops_pending--;
1115                 conn->c_n_ops_executing++;
1116
1117                 connection_op_activate( conn, op );
1118
1119                 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1120                         break;
1121                 }
1122         }
1123         return 0;
1124 }
1125
1126 static int connection_op_activate( Connection *conn, Operation *op )
1127 {
1128         struct co_arg *arg;
1129         int status;
1130         ber_tag_t tag = op->o_tag;
1131
1132         if(tag == LDAP_REQ_BIND) {
1133                 conn->c_conn_state = SLAP_C_BINDING;
1134         }
1135
1136         arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1137         arg->co_conn = conn;
1138         arg->co_op = op;
1139
1140         arg->co_op->o_dn = ch_strdup( conn->c_dn != NULL ? conn->c_dn : "" );
1141         arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1142         (void) dn_normalize( arg->co_op->o_ndn );
1143
1144         arg->co_op->o_protocol = conn->c_protocol;
1145         arg->co_op->o_connid = conn->c_connid;
1146
1147         arg->co_op->o_authtype = conn->c_authtype;
1148         arg->co_op->o_authmech = conn->c_authmech != NULL
1149                 ?  ch_strdup( conn->c_authmech ) : NULL;
1150         
1151         slap_op_add( &conn->c_ops, arg->co_op );
1152
1153         status = ldap_pvt_thread_pool_submit( &connection_pool,
1154                 connection_operation, (void *) arg );
1155
1156         if ( status != 0 ) {
1157                 Debug( LDAP_DEBUG_ANY,
1158                 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1159                 /* should move op to pending list */
1160         }
1161
1162         return status;
1163 }
1164
1165 int connection_write(ber_socket_t s)
1166 {
1167         Connection *c;
1168         assert( connections != NULL );
1169
1170         ldap_pvt_thread_mutex_lock( &connections_mutex );
1171
1172         c = connection_get( s );
1173
1174         slapd_clr_write( s, 0);
1175
1176         if( c == NULL ) {
1177                 Debug( LDAP_DEBUG_ANY,
1178                         "connection_write(%ld): no connection!\n",
1179                         (long) s, 0, 0 );
1180                 slapd_remove(s, 0);
1181                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1182                 return -1;
1183         }
1184
1185         c->c_n_write++;
1186
1187         Debug( LDAP_DEBUG_TRACE,
1188                 "connection_write(%d): waking output for id=%ld\n",
1189                 s, c->c_connid, 0 );
1190
1191         ldap_pvt_thread_cond_signal( &c->c_write_cv );
1192
1193         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1194                 slapd_set_read( s, 1 );
1195         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1196                 slapd_set_write( s, 1 );
1197         connection_return( c );
1198         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1199         return 0;
1200 }