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