3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
14 #include <ac/string.h>
16 #include <ac/unistd.h>
22 /* protected by connections_mutex */
23 static ldap_pvt_thread_mutex_t connections_mutex;
24 static Connection *connections = NULL;
25 static unsigned long conn_nextid = 0;
27 /* structure state (protected by connections_mutex) */
28 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
29 #define SLAP_C_UNUSED 0x01
30 #define SLAP_C_USED 0x02
32 /* connection state (protected by c_mutex ) */
33 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
34 #define SLAP_C_INACTIVE 0x01 /* zero threads */
35 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
36 #define SLAP_C_BINDING 0x03 /* binding */
37 #define SLAP_C_CLOSING 0x04 /* closing */
40 connection_state2str( int state )
43 case SLAP_C_INVALID: return "!";
44 case SLAP_C_INACTIVE: return "|";
45 case SLAP_C_ACTIVE: return "";
46 case SLAP_C_BINDING: return "B";
47 case SLAP_C_CLOSING: return "C";
53 static Connection* connection_get( ber_socket_t s );
55 static int connection_input( Connection *c );
56 static void connection_close( Connection *c );
58 static int connection_op_activate( Connection *conn, Operation *op );
59 static int connection_resched( Connection *conn );
60 static void connection_abandon( Connection *conn );
61 static void connection_destroy( Connection *c );
63 static ldap_pvt_thread_start_t connection_operation;
71 * Initialize connection management infrastructure.
73 int connections_init(void)
75 assert( connections == NULL );
77 if( connections != NULL) {
79 LDAP_LOG( CONNECTION, INFO,
80 "connections_init: already initialized.\n", 0, 0, 0 );
82 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
88 /* should check return of every call */
89 ldap_pvt_thread_mutex_init( &connections_mutex );
91 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
93 if( connections == NULL ) {
95 LDAP_LOG( CONNECTION, ERR,
96 "connections_init: allocation (%d * %ld) of connection "
97 "array failed\n", dtblsize, (long) sizeof(Connection), 0 );
99 Debug( LDAP_DEBUG_ANY,
100 "connections_init: allocation (%d*%ld) of connection array failed\n",
101 dtblsize, (long) sizeof(Connection), 0 );
106 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
107 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
110 * per entry initialization of the Connection array initialization
111 * will be done by connection_init()
118 * Destroy connection management infrastructure.
120 int connections_destroy(void)
124 /* should check return of every call */
126 if( connections == NULL) {
128 LDAP_LOG( CONNECTION, INFO,
129 "connections_destroy: nothing to destroy.\n", 0, 0, 0 );
131 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
137 for ( i = 0; i < dtblsize; i++ ) {
138 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
139 ber_sockbuf_free( connections[i].c_sb );
140 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
141 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
142 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
149 ldap_pvt_thread_mutex_destroy( &connections_mutex );
154 * shutdown all connections
156 int connections_shutdown(void)
160 ldap_pvt_thread_mutex_lock( &connections_mutex );
162 for ( i = 0; i < dtblsize; i++ ) {
163 if( connections[i].c_struct_state != SLAP_C_USED ) {
167 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
169 /* connections_mutex and c_mutex are locked */
170 connection_closing( &connections[i] );
171 connection_close( &connections[i] );
173 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
176 ldap_pvt_thread_mutex_unlock( &connections_mutex );
182 * Timeout idle connections.
184 int connections_timeout_idle(time_t now)
190 for( c = connection_first( &connindex );
192 c = connection_next( c, &connindex ) )
194 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
196 connection_closing( c );
197 connection_close( c );
201 connection_done( c );
206 static Connection* connection_get( ber_socket_t s )
208 /* connections_mutex should be locked by caller */
213 LDAP_LOG( CONNECTION, ENTRY, "connection_get: socket %ld\n", (long)s, 0, 0 );
215 Debug( LDAP_DEBUG_ARGS,
216 "connection_get(%ld)\n",
220 assert( connections != NULL );
222 if(s == AC_SOCKET_INVALID) {
229 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
236 for(i=0; i<dtblsize; i++) {
237 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
238 assert( connections[i].c_conn_state == SLAP_C_INVALID );
239 assert( connections[i].c_sb == 0 );
243 ber_sockbuf_ctrl( connections[i].c_sb,
244 LBER_SB_OPT_GET_FD, &sd );
246 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
247 assert( connections[i].c_conn_state == SLAP_C_INVALID );
248 assert( sd == AC_SOCKET_INVALID );
252 /* state can actually change from used -> unused by resched,
253 * so don't assert details here.
267 ldap_pvt_thread_mutex_lock( &c->c_mutex );
269 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
270 if( c->c_struct_state != SLAP_C_USED ) {
271 /* connection must have been closed due to resched */
273 assert( c->c_conn_state == SLAP_C_INVALID );
274 assert( sd == AC_SOCKET_INVALID );
277 LDAP_LOG( CONNECTION, ARGS,
278 "connection_get: connection %d not used\n", s, 0, 0 );
280 Debug( LDAP_DEBUG_TRACE,
281 "connection_get(%d): connection not used\n",
285 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
290 LDAP_LOG( CONNECTION, RESULTS,
291 "connection_get: get for %d got connid %lu\n", s, c->c_connid, 0 );
293 Debug( LDAP_DEBUG_TRACE,
294 "connection_get(%d): got connid=%lu\n",
300 assert( c->c_struct_state == SLAP_C_USED );
301 assert( c->c_conn_state != SLAP_C_INVALID );
302 assert( sd != AC_SOCKET_INVALID );
305 c->c_activitytime = slap_get_time();
307 if( global_idletimeout > 0 ) {
308 c->c_activitytime = slap_get_time();
316 static void connection_return( Connection *c )
318 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
321 long connection_init(
325 const char* peername,
326 const char* sockname,
334 assert( connections != NULL );
336 assert( dnsname != NULL );
337 assert( peername != NULL );
338 assert( sockname != NULL );
341 assert( tls_udp_option != 1 );
344 if( s == AC_SOCKET_INVALID ) {
346 LDAP_LOG( CONNECTION, INFO,
347 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
349 Debug( LDAP_DEBUG_ANY,
350 "connection_init(%ld): invalid.\n",
358 assert( s < dtblsize );
361 ldap_pvt_thread_mutex_lock( &connections_mutex );
372 for( i=0; i < dtblsize; i++) {
375 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
376 assert( connections[i].c_sb == 0 );
381 sd = AC_SOCKET_INVALID;
382 if (connections[i].c_sb != NULL)
383 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
385 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
386 assert( sd == AC_SOCKET_INVALID );
391 assert( connections[i].c_struct_state == SLAP_C_USED );
392 assert( connections[i].c_conn_state != SLAP_C_INVALID );
393 assert( sd != AC_SOCKET_INVALID );
398 LDAP_LOG( CONNECTION, INFO,
399 "connection_init: skt %d connection table full (%d/%d)\n",
402 Debug( LDAP_DEBUG_ANY,
403 "connection_init(%d): connection table full (%d/%d)\n",
406 ldap_pvt_thread_mutex_unlock( &connections_mutex );
414 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
415 c->c_authmech.bv_val = NULL;
416 c->c_authmech.bv_len = 0;
417 c->c_dn.bv_val = NULL;
419 c->c_ndn.bv_val = NULL;
423 c->c_listener_url.bv_val = NULL;
424 c->c_listener_url.bv_len = 0;
425 c->c_peer_domain.bv_val = NULL;
426 c->c_peer_domain.bv_len = 0;
427 c->c_peer_name.bv_val = NULL;
428 c->c_peer_name.bv_len = 0;
429 c->c_sock_name.bv_val = NULL;
430 c->c_sock_name.bv_len = 0;
432 LDAP_STAILQ_INIT(&c->c_ops);
433 LDAP_STAILQ_INIT(&c->c_pending_ops);
435 c->c_sasl_bind_mech.bv_val = NULL;
436 c->c_sasl_bind_mech.bv_len = 0;
437 c->c_sasl_context = NULL;
438 c->c_sasl_extra = NULL;
439 c->c_sasl_bindop = NULL;
441 c->c_sb = ber_sockbuf_alloc( );
444 ber_len_t max = sockbuf_max_incoming;
445 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
448 c->c_currentber = NULL;
450 /* should check status of thread calls */
451 ldap_pvt_thread_mutex_init( &c->c_mutex );
452 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
453 ldap_pvt_thread_cond_init( &c->c_write_cv );
455 c->c_struct_state = SLAP_C_UNUSED;
458 ldap_pvt_thread_mutex_lock( &c->c_mutex );
460 assert( c->c_struct_state == SLAP_C_UNUSED );
461 assert( c->c_authmech.bv_val == NULL );
462 assert( c->c_dn.bv_val == NULL );
463 assert( c->c_ndn.bv_val == NULL );
464 assert( c->c_groups == NULL );
465 assert( c->c_listener_url.bv_val == NULL );
466 assert( c->c_peer_domain.bv_val == NULL );
467 assert( c->c_peer_name.bv_val == NULL );
468 assert( c->c_sock_name.bv_val == NULL );
469 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
470 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
471 assert( c->c_sasl_bind_mech.bv_val == NULL );
472 assert( c->c_sasl_context == NULL );
473 assert( c->c_sasl_extra == NULL );
474 assert( c->c_sasl_bindop == NULL );
475 assert( c->c_currentber == NULL );
477 ber_str2bv( url, 0, 1, &c->c_listener_url );
478 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
479 ber_str2bv( peername, 0, 1, &c->c_peer_name );
480 ber_str2bv( sockname, 0, 1, &c->c_sock_name );
482 c->c_n_ops_received = 0;
483 c->c_n_ops_executing = 0;
484 c->c_n_ops_pending = 0;
485 c->c_n_ops_completed = 0;
491 /* set to zero until bind, implies LDAP_VERSION3 */
495 c->c_activitytime = c->c_starttime = slap_get_time();
497 if( global_idletimeout > 0 ) {
498 c->c_activitytime = c->c_starttime = slap_get_time();
502 #ifdef LDAP_CONNECTIONLESS
504 if( tls_udp_option == 2 ) {
507 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
508 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
510 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
511 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
512 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
513 LBER_SBIOD_LEVEL_PROVIDER, NULL );
518 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
519 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
521 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
522 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
526 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
527 INT_MAX, (void*)"ldap_" );
530 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
531 c /* non-NULL */ ) < 0 )
534 LDAP_LOG( CONNECTION, INFO,
535 "connection_init: conn %lu set nonblocking failed\n",
538 Debug( LDAP_DEBUG_ANY,
539 "connection_init(%d, %s): set nonblocking failed\n",
540 s, c->c_peer_name.bv_val, 0 );
544 id = c->c_connid = conn_nextid++;
546 c->c_conn_state = SLAP_C_INACTIVE;
547 c->c_struct_state = SLAP_C_USED;
549 c->c_ssf = c->c_transport_ssf = ssf;
553 if ( tls_udp_option == 1 ) {
555 c->c_needs_tls_accept = 1;
558 c->c_needs_tls_accept = 0;
563 slap_sasl_external( c, ssf, authid );
565 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
566 ldap_pvt_thread_mutex_unlock( &connections_mutex );
568 backend_connection_init(c);
573 void connection2anonymous( Connection *c )
575 assert( connections != NULL );
579 ber_len_t max = sockbuf_max_incoming;
580 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
583 if(c->c_authmech.bv_val != NULL ) {
584 free(c->c_authmech.bv_val);
585 c->c_authmech.bv_val = NULL;
587 c->c_authmech.bv_len = 0;
589 if(c->c_dn.bv_val != NULL) {
590 free(c->c_dn.bv_val);
591 c->c_dn.bv_val = NULL;
594 if(c->c_ndn.bv_val != NULL) {
595 free(c->c_ndn.bv_val);
596 c->c_ndn.bv_val = NULL;
600 c->c_authz_backend = NULL;
603 GroupAssertion *g, *n;
604 for (g = c->c_groups; g; g=n) {
613 connection_destroy( Connection *c )
615 /* note: connections_mutex should be locked by caller */
617 unsigned long connid;
619 assert( connections != NULL );
621 assert( c->c_struct_state != SLAP_C_UNUSED );
622 assert( c->c_conn_state != SLAP_C_INVALID );
623 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
625 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
626 connid = c->c_connid;
628 backend_connection_destroy(c);
633 c->c_activitytime = c->c_starttime = 0;
635 connection2anonymous( c );
637 if(c->c_listener_url.bv_val != NULL) {
638 free(c->c_listener_url.bv_val);
639 c->c_listener_url.bv_val = NULL;
641 c->c_listener_url.bv_len = 0;
643 if(c->c_peer_domain.bv_val != NULL) {
644 free(c->c_peer_domain.bv_val);
645 c->c_peer_domain.bv_val = NULL;
647 c->c_peer_domain.bv_len = 0;
648 if(c->c_peer_name.bv_val != NULL) {
651 * If peer was a domain socket, unlink. Mind you,
652 * they may be un-named. Should we leave this to
655 if (strncmp(c->c_peer_name.bv_val, "PATH=",
656 sizeof("PATH=") - 1) == 0) {
657 char *path = c->c_peer_name.bv_val
658 + sizeof("PATH=") - 1;
659 if (path[0] != '\0') {
663 #endif /* LDAP_PF_LOCAL */
665 free(c->c_peer_name.bv_val);
666 c->c_peer_name.bv_val = NULL;
668 c->c_peer_name.bv_len = 0;
669 if(c->c_sock_name.bv_val != NULL) {
670 free(c->c_sock_name.bv_val);
671 c->c_sock_name.bv_val = NULL;
673 c->c_sock_name.bv_len = 0;
675 c->c_sasl_bind_in_progress = 0;
676 if(c->c_sasl_bind_mech.bv_val != NULL) {
677 free(c->c_sasl_bind_mech.bv_val);
678 c->c_sasl_bind_mech.bv_val = NULL;
680 c->c_sasl_bind_mech.bv_len = 0;
682 slap_sasl_close( c );
684 if ( c->c_currentber != NULL ) {
685 ber_free( c->c_currentber, 1 );
686 c->c_currentber = NULL;
689 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
690 if ( sd != AC_SOCKET_INVALID ) {
691 slapd_remove( sd, 0 );
693 Statslog( LDAP_DEBUG_STATS,
694 "conn=%lu fd=%d closed\n",
695 connid, sd, 0, 0, 0 );
698 ber_sockbuf_free( c->c_sb );
700 c->c_sb = ber_sockbuf_alloc( );
703 ber_len_t max = sockbuf_max_incoming;
704 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
707 c->c_conn_state = SLAP_C_INVALID;
708 c->c_struct_state = SLAP_C_UNUSED;
711 int connection_state_closing( Connection *c )
713 /* c_mutex must be locked by caller */
717 assert( c->c_struct_state == SLAP_C_USED );
719 state = c->c_conn_state;
721 assert( state != SLAP_C_INVALID );
723 return state == SLAP_C_CLOSING;
726 static void connection_abandon( Connection *c )
728 /* c_mutex must be locked by caller */
732 LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
736 /* remove pending operations */
737 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
738 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
739 LDAP_STAILQ_NEXT(o, o_next) = NULL;
744 void connection_closing( Connection *c )
746 assert( connections != NULL );
748 assert( c->c_struct_state == SLAP_C_USED );
749 assert( c->c_conn_state != SLAP_C_INVALID );
751 /* c_mutex must be locked by caller */
753 if( c->c_conn_state != SLAP_C_CLOSING ) {
756 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
758 LDAP_LOG( CONNECTION, DETAIL1,
759 "connection_closing: conn %lu readying socket %d for close.\n",
760 c->c_connid, sd, 0 );
762 Debug( LDAP_DEBUG_TRACE,
763 "connection_closing: readying conn=%lu sd=%d for close\n",
764 c->c_connid, sd, 0 );
766 /* update state to closing */
767 c->c_conn_state = SLAP_C_CLOSING;
769 /* don't listen on this port anymore */
770 slapd_clr_read( sd, 1 );
772 /* abandon active operations */
773 connection_abandon( c );
775 /* wake write blocked operations */
776 slapd_clr_write( sd, 1 );
777 ldap_pvt_thread_cond_signal( &c->c_write_cv );
781 static void connection_close( Connection *c )
785 assert( connections != NULL );
787 assert( c->c_struct_state == SLAP_C_USED );
788 assert( c->c_conn_state == SLAP_C_CLOSING );
790 /* note: connections_mutex and c_mutex should be locked by caller */
792 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
793 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
795 LDAP_LOG( CONNECTION, DETAIL1,
796 "connection_close: conn %lu deferring sd %d\n",
797 c->c_connid, sd, 0 );
799 Debug( LDAP_DEBUG_TRACE,
800 "connection_close: deferring conn=%lu sd=%d\n",
801 c->c_connid, sd, 0 );
807 LDAP_LOG( CONNECTION, RESULTS,
808 "connection_close: conn %lu sd %d\n", c->c_connid, sd, 0 );
810 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
811 c->c_connid, sd, 0 );
813 connection_destroy( c );
816 unsigned long connections_nextid(void)
819 assert( connections != NULL );
821 ldap_pvt_thread_mutex_lock( &connections_mutex );
825 ldap_pvt_thread_mutex_unlock( &connections_mutex );
830 Connection* connection_first( ber_socket_t *index )
832 assert( connections != NULL );
833 assert( index != NULL );
835 ldap_pvt_thread_mutex_lock( &connections_mutex );
839 return connection_next(NULL, index);
842 Connection* connection_next( Connection *c, ber_socket_t *index )
844 assert( connections != NULL );
845 assert( index != NULL );
846 assert( *index <= dtblsize );
849 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
854 for(; *index < dtblsize; (*index)++) {
855 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
856 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
864 if( connections[*index].c_struct_state == SLAP_C_USED ) {
865 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
866 c = &connections[(*index)++];
870 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
871 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
875 ldap_pvt_thread_mutex_lock( &c->c_mutex );
881 void connection_done( Connection *c )
883 assert( connections != NULL );
886 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
889 ldap_pvt_thread_mutex_unlock( &connections_mutex );
893 * connection_activity - handle the request operation op on connection
894 * conn. This routine figures out what kind of operation it is and
895 * calls the appropriate stub to handle it.
899 #define INCR_OP(var,index) \
901 ldap_pvt_thread_mutex_lock( &num_ops_mutex ); \
903 ldap_pvt_thread_mutex_unlock( &num_ops_mutex ); \
905 #else /* !SLAPD_MONITOR */
906 #define INCR_OP(var,index)
907 #endif /* !SLAPD_MONITOR */
910 connection_operation( void *ctx, void *arg_v )
913 struct co_arg *arg = arg_v;
914 ber_tag_t tag = arg->co_op->o_tag;
916 ber_tag_t oldtag = tag;
917 #endif /* SLAPD_MONITOR */
918 Connection *conn = arg->co_conn;
920 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
922 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
924 arg->co_op->o_threadctx = ctx;
926 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
928 LDAP_LOG( CONNECTION, ERR,
929 "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
930 conn->c_connid, (long)tag, 0 );
932 Debug( LDAP_DEBUG_ANY, "connection_operation: "
933 "error: SASL bind in progress (tag=%ld).\n",
936 send_ldap_result( conn, arg->co_op,
937 rc = LDAP_OPERATIONS_ERROR,
938 NULL, "SASL bind in progress", NULL, NULL );
939 goto operations_error;
944 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
945 rc = do_bind( conn, arg->co_op );
948 case LDAP_REQ_UNBIND:
949 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
950 rc = do_unbind( conn, arg->co_op );
954 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
955 rc = do_add( conn, arg->co_op );
958 case LDAP_REQ_DELETE:
959 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
960 rc = do_delete( conn, arg->co_op );
963 case LDAP_REQ_MODRDN:
964 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
965 rc = do_modrdn( conn, arg->co_op );
968 case LDAP_REQ_MODIFY:
969 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
970 rc = do_modify( conn, arg->co_op );
973 case LDAP_REQ_COMPARE:
974 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
975 rc = do_compare( conn, arg->co_op );
978 case LDAP_REQ_SEARCH:
979 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
980 rc = do_search( conn, arg->co_op );
983 case LDAP_REQ_ABANDON:
984 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
985 rc = do_abandon( conn, arg->co_op );
988 case LDAP_REQ_EXTENDED:
989 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
990 rc = do_extended( conn, arg->co_op );
995 LDAP_LOG( CONNECTION, INFO,
996 "connection_operation: conn %lu unknown LDAP request 0x%lx\n",
997 conn->c_connid, tag, 0 );
999 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
1002 arg->co_op->o_tag = LBER_ERROR;
1003 send_ldap_disconnect( conn, arg->co_op,
1004 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
1009 #ifdef SLAPD_MONITOR
1011 #endif /* SLAPD_MONITOR */
1012 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
1015 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
1016 num_ops_completed++;
1017 #ifdef SLAPD_MONITOR
1020 num_ops_completed_[SLAP_OP_BIND]++;
1022 case LDAP_REQ_UNBIND:
1023 num_ops_completed_[SLAP_OP_UNBIND]++;
1026 num_ops_completed_[SLAP_OP_ADD]++;
1028 case LDAP_REQ_DELETE:
1029 num_ops_completed_[SLAP_OP_DELETE]++;
1031 case LDAP_REQ_MODRDN:
1032 num_ops_completed_[SLAP_OP_MODRDN]++;
1034 case LDAP_REQ_MODIFY:
1035 num_ops_completed_[SLAP_OP_MODIFY]++;
1037 case LDAP_REQ_COMPARE:
1038 num_ops_completed_[SLAP_OP_COMPARE]++;
1040 case LDAP_REQ_SEARCH:
1041 num_ops_completed_[SLAP_OP_SEARCH]++;
1043 case LDAP_REQ_ABANDON:
1044 num_ops_completed_[SLAP_OP_ABANDON]++;
1046 case LDAP_REQ_EXTENDED:
1047 num_ops_completed_[SLAP_OP_EXTENDED]++;
1050 #endif /* SLAPD_MONITOR */
1051 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1053 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1055 conn->c_n_ops_executing--;
1056 conn->c_n_ops_completed++;
1058 LDAP_STAILQ_REMOVE( &conn->c_ops, arg->co_op, slap_op, o_next);
1059 LDAP_STAILQ_NEXT(arg->co_op, o_next) = NULL;
1060 slap_op_free( arg->co_op );
1062 arg->co_conn = NULL;
1063 free( (char *) arg );
1068 case LDAP_REQ_UNBIND:
1069 /* c_mutex is locked */
1070 connection_closing( conn );
1074 conn->c_sasl_bind_in_progress =
1075 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1077 if( conn->c_conn_state == SLAP_C_BINDING) {
1078 conn->c_conn_state = SLAP_C_ACTIVE;
1082 connection_resched( conn );
1084 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1089 int connection_read(ber_socket_t s)
1094 assert( connections != NULL );
1096 ldap_pvt_thread_mutex_lock( &connections_mutex );
1098 /* get (locked) connection */
1099 c = connection_get( s );
1103 LDAP_LOG( CONNECTION, INFO,
1104 "connection_read: sock %ld no connection\n", (long)s, 0, 0 );
1106 Debug( LDAP_DEBUG_ANY,
1107 "connection_read(%ld): no connection!\n",
1112 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1118 if( c->c_conn_state == SLAP_C_CLOSING ) {
1120 LDAP_LOG( CONNECTION, INFO,
1121 "connection_read: conn %lu connection closing, ignoring input\n",
1122 c->c_connid, 0, 0 );
1124 Debug( LDAP_DEBUG_TRACE,
1125 "connection_read(%d): closing, ignoring input for id=%lu\n",
1126 s, c->c_connid, 0 );
1128 connection_return( c );
1129 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1134 LDAP_LOG( CONNECTION, DETAIL1,
1135 "connection_read: conn %lu checking for input.\n",
1136 c->c_connid, 0, 0 );
1138 Debug( LDAP_DEBUG_TRACE,
1139 "connection_read(%d): checking for input on id=%lu\n",
1140 s, c->c_connid, 0 );
1144 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1145 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1147 #if 0 /* required by next #if 0 */
1153 LDAP_LOG( CONNECTION, ERR,
1154 "connection_read: conn %lu TLS accept error, error %d\n",
1155 c->c_connid, rc, 0 );
1157 Debug( LDAP_DEBUG_TRACE,
1158 "connection_read(%d): TLS accept error "
1159 "error=%d id=%lu, closing\n",
1160 s, rc, c->c_connid );
1162 c->c_needs_tls_accept = 0;
1163 /* connections_mutex and c_mutex are locked */
1164 connection_closing( c );
1167 /* Drain input before close, to allow SSL error codes
1168 * to propagate to client. */
1174 rc = select(s+1, &rfd, NULL, NULL, &tv);
1176 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1180 connection_close( c );
1182 } else if ( rc == 0 ) {
1184 struct berval authid = { 0, NULL };
1186 c->c_needs_tls_accept = 0;
1188 /* we need to let SASL know */
1189 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1191 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1192 if( c->c_tls_ssf > c->c_ssf ) {
1193 c->c_ssf = c->c_tls_ssf;
1196 rc = dnX509peerNormalize( ssl, &authid );
1197 if ( rc != LDAP_SUCCESS ) {
1199 LDAP_LOG( CONNECTION, INFO,
1200 "connection_read: conn %lu unable to get TLS client DN, "
1201 "error %d\n", c->c_connid, rc, 0 );
1203 Debug( LDAP_DEBUG_TRACE,
1204 "connection_read(%d): unable to get TLS client DN "
1205 "error=%d id=%lu\n",
1206 s, rc, c->c_connid );
1209 slap_sasl_external( c, c->c_tls_ssf, authid.bv_val );
1210 if ( authid.bv_val ) free( authid.bv_val );
1213 /* if success and data is ready, fall thru to data input loop */
1215 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1217 connection_return( c );
1218 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1224 #ifdef HAVE_CYRUS_SASL
1225 if ( c->c_sasl_layers ) {
1226 c->c_sasl_layers = 0;
1228 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
1230 if( rc != LDAP_SUCCESS ) {
1232 LDAP_LOG( CONNECTION, ERR,
1233 "connection_read: conn %lu SASL install error %d, closing\n",
1234 c->c_connid, rc, 0 );
1236 Debug( LDAP_DEBUG_TRACE,
1237 "connection_read(%d): SASL install error "
1238 "error=%d id=%lu, closing\n",
1239 s, rc, c->c_connid );
1241 /* connections_mutex and c_mutex are locked */
1242 connection_closing( c );
1243 connection_close( c );
1244 connection_return( c );
1245 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1251 #define CONNECTION_INPUT_LOOP 1
1253 #ifdef DATA_READY_LOOP
1254 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
1255 #elif CONNECTION_INPUT_LOOP
1259 /* How do we do this without getting into a busy loop ? */
1260 rc = connection_input( c );
1265 LDAP_LOG( CONNECTION, ERR,
1266 "connection_read: conn %lu input error %d, closing.\n",
1267 c->c_connid, rc, 0 );
1269 Debug( LDAP_DEBUG_TRACE,
1270 "connection_read(%d): input error=%d id=%lu, closing.\n",
1271 s, rc, c->c_connid );
1273 /* connections_mutex and c_mutex are locked */
1274 connection_closing( c );
1275 connection_close( c );
1276 connection_return( c );
1277 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1281 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1282 slapd_set_read( s, 1 );
1285 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1286 slapd_set_write( s, 1 );
1289 connection_return( c );
1290 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1304 #ifdef LDAP_CONNECTIONLESS
1309 if ( conn->c_currentber == NULL &&
1310 ( conn->c_currentber = ber_alloc()) == NULL )
1313 LDAP_LOG( CONNECTION, ERR,
1314 "connection_input: conn %lu ber_alloc failed.\n",
1315 conn->c_connid, 0, 0 );
1317 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1324 #ifdef LDAP_CONNECTIONLESS
1325 if ( conn->c_is_udp ) {
1326 char peername[sizeof("IP=255.255.255.255:65336")];
1327 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1328 sizeof(struct sockaddr));
1329 if (len != sizeof(struct sockaddr))
1331 sprintf( peername, "IP=%s:%d",
1332 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1333 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1334 Statslog( LDAP_DEBUG_STATS,
1335 "conn=%lu UDP request from %s (%s) accepted.\n",
1336 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1339 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1340 if ( tag != LDAP_TAG_MESSAGE ) {
1344 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1347 LDAP_LOG( CONNECTION, ERR,
1348 "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1349 conn->c_connid, err, sock_errstr(err) );
1351 Debug( LDAP_DEBUG_TRACE,
1352 "ber_get_next on fd %d failed errno=%d (%s)\n",
1353 sd, err, sock_errstr(err) );
1355 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1356 /* log, close and send error */
1357 ber_free( conn->c_currentber, 1 );
1358 conn->c_currentber = NULL;
1365 ber = conn->c_currentber;
1366 conn->c_currentber = NULL;
1368 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1369 /* log, close and send error */
1371 LDAP_LOG( CONNECTION, ERR,
1372 "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1373 conn->c_connid, tag, 0 );
1375 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1382 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1383 /* log, close and send error */
1385 LDAP_LOG( CONNECTION, ERR,
1386 "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1387 conn->c_connid, tag, 0 );
1389 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1397 #ifdef LDAP_CONNECTIONLESS
1398 if( conn->c_is_udp ) {
1399 if( tag == LBER_OCTETSTRING ) {
1400 ber_get_stringa( ber, &cdn );
1401 tag = ber_peek_tag(ber, &len);
1403 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1405 LDAP_LOG( CONNECTION, ERR,
1406 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1407 conn->c_connid, tag, 0 );
1409 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1417 if(tag == LDAP_REQ_BIND) {
1418 /* immediately abandon all exiting operations upon BIND */
1419 connection_abandon( conn );
1422 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1425 op->vrFilter = NULL;
1426 op->o_pagedresults_state = conn->c_pagedresults_state;
1427 #ifdef LDAP_CONNECTIONLESS
1428 op->o_peeraddr = peeraddr;
1430 ber_str2bv( cdn, 0, 1, &op->o_dn );
1431 op->o_protocol = LDAP_VERSION2;
1435 if ( conn->c_conn_state == SLAP_C_BINDING
1436 || conn->c_conn_state == SLAP_C_CLOSING )
1439 LDAP_LOG( CONNECTION, INFO,
1440 "connection_input: conn %lu deferring operation\n",
1441 conn->c_connid, 0, 0 );
1443 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1445 conn->c_n_ops_pending++;
1446 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1449 conn->c_n_ops_executing++;
1450 connection_op_activate( conn, op );
1454 if ( conn->c_struct_state != SLAP_C_USED ) {
1455 /* connection must have got closed underneath us */
1459 assert( conn->c_struct_state == SLAP_C_USED );
1465 connection_resched( Connection *conn )
1469 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1472 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1474 /* us trylock to avoid possible deadlock */
1475 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1479 LDAP_LOG( CONNECTION, DETAIL1,
1480 "connection_resched: conn %lu reaquiring locks.\n",
1481 conn->c_connid, 0, 0 );
1483 Debug( LDAP_DEBUG_TRACE,
1484 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1485 conn->c_connid, sd, 0 );
1488 * reaquire locks in the right order...
1489 * this may allow another thread to close this connection,
1490 * so recheck state below.
1492 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1493 ldap_pvt_thread_mutex_lock( &connections_mutex );
1494 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1497 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1499 LDAP_LOG( CONNECTION, INFO,
1500 "connection_resched: conn %lu closed by other thread.\n",
1501 conn->c_connid, 0, 0 );
1503 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1504 "closed by other thread conn=%lu sd=%d\n",
1505 conn->c_connid, sd, 0 );
1509 LDAP_LOG( CONNECTION, DETAIL1,
1510 "connection_resched: conn %lu attempting closing.\n",
1511 conn->c_connid, 0, 0 );
1513 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1514 "attempting closing conn=%lu sd=%d\n",
1515 conn->c_connid, sd, 0 );
1517 connection_close( conn );
1520 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1524 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1525 /* other states need different handling */
1529 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1530 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1531 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1532 /* pending operations should not be marked for abandonment */
1533 assert(!op->o_abandon);
1535 conn->c_n_ops_pending--;
1536 conn->c_n_ops_executing++;
1538 connection_op_activate( conn, op );
1540 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1547 static int connection_op_activate( Connection *conn, Operation *op )
1551 ber_tag_t tag = op->o_tag;
1553 if(tag == LDAP_REQ_BIND) {
1554 conn->c_conn_state = SLAP_C_BINDING;
1557 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1558 arg->co_conn = conn;
1561 if (!arg->co_op->o_dn.bv_len) {
1562 arg->co_op->o_authz = conn->c_authz;
1563 arg->co_op->o_dn.bv_val = ch_strdup( conn->c_dn.bv_val ?
1564 conn->c_dn.bv_val : "" );
1565 arg->co_op->o_ndn.bv_val = ch_strdup( conn->c_ndn.bv_val ?
1566 conn->c_ndn.bv_val : "" );
1568 arg->co_op->o_authtype = conn->c_authtype;
1569 ber_dupbv( &arg->co_op->o_authmech, &conn->c_authmech );
1571 if (!arg->co_op->o_protocol) {
1572 arg->co_op->o_protocol = conn->c_protocol
1573 ? conn->c_protocol : LDAP_VERSION3;
1575 arg->co_op->o_connid = conn->c_connid;
1577 LDAP_STAILQ_INSERT_TAIL( &conn->c_ops, arg->co_op, o_next );
1579 status = ldap_pvt_thread_pool_submit( &connection_pool,
1580 connection_operation, (void *) arg );
1582 if ( status != 0 ) {
1584 LDAP_LOG( CONNECTION, ERR,
1585 "connection_op_activate: conn %lu thread pool submit failed.\n",
1586 conn->c_connid, 0, 0 );
1588 Debug( LDAP_DEBUG_ANY,
1589 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1591 /* should move op to pending list */
1597 int connection_write(ber_socket_t s)
1601 assert( connections != NULL );
1603 ldap_pvt_thread_mutex_lock( &connections_mutex );
1605 c = connection_get( s );
1607 slapd_clr_write( s, 0);
1611 LDAP_LOG( CONNECTION, ERR,
1612 "connection_write: sock %ld no connection!\n", (long)s, 0, 0);
1614 Debug( LDAP_DEBUG_ANY,
1615 "connection_write(%ld): no connection!\n",
1619 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1626 LDAP_LOG( CONNECTION, DETAIL1,
1627 "connection_write conn %lu waking output.\n", c->c_connid, 0, 0 );
1629 Debug( LDAP_DEBUG_TRACE,
1630 "connection_write(%d): waking output for id=%lu\n",
1631 s, c->c_connid, 0 );
1633 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1635 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1636 slapd_set_read( s, 1 );
1637 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1638 slapd_set_write( s, 1 );
1639 connection_return( c );
1640 ldap_pvt_thread_mutex_unlock( &connections_mutex );