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