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