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