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