3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
17 #include <ac/unistd.h>
23 /* I guess this should be declared in ldap_pvt.h */
24 extern void * ldap_pvt_tls_sb_ctx( Sockbuf *sb );
26 /* protected by connections_mutex */
27 static ldap_pvt_thread_mutex_t connections_mutex;
28 static Connection *connections = NULL;
29 static unsigned long conn_nextid = 0;
31 /* structure state (protected by connections_mutex) */
32 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
33 #define SLAP_C_UNUSED 0x01
34 #define SLAP_C_USED 0x02
36 /* connection state (protected by c_mutex ) */
37 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
38 #define SLAP_C_INACTIVE 0x01 /* zero threads */
39 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
40 #define SLAP_C_BINDING 0x03 /* binding */
41 #define SLAP_C_CLOSING 0x04 /* closing */
44 connection_state2str( int state )
47 case SLAP_C_INVALID: return "!";
48 case SLAP_C_INACTIVE: return "|";
49 case SLAP_C_ACTIVE: return "";
50 case SLAP_C_BINDING: return "B";
51 case SLAP_C_CLOSING: return "C";
57 static Connection* connection_get( ber_socket_t s );
59 static int connection_input( Connection *c );
60 static void connection_close( Connection *c );
62 static int connection_op_activate( Connection *conn, Operation *op );
63 static int connection_resched( Connection *conn );
64 static void connection_abandon( Connection *conn );
65 static void connection_destroy( Connection *c );
73 * Initialize connection management infrastructure.
75 int connections_init(void)
77 assert( connections == NULL );
79 if( connections != NULL) {
81 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
82 "connections_init: already initialized.\n" ));
84 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
90 /* should check return of every call */
91 ldap_pvt_thread_mutex_init( &connections_mutex );
93 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
95 if( connections == NULL ) {
97 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
98 "connections_init: allocation (%d * %ld) of connection array failed\n",
99 dtblsize, (long) sizeof(Connection) ));
101 Debug( LDAP_DEBUG_ANY,
102 "connections_init: allocation (%d*%ld) of connection array failed\n",
103 dtblsize, (long) sizeof(Connection), 0 );
108 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
109 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
112 * per entry initialization of the Connection array initialization
113 * will be done by connection_init()
120 * Destroy connection management infrastructure.
122 int connections_destroy(void)
126 /* should check return of every call */
128 if( connections == NULL) {
130 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
131 "connections_destroy: nothing to destroy.\n"));
133 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
139 for ( i = 0; i < dtblsize; i++ ) {
140 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
141 ber_sockbuf_free( connections[i].c_sb );
142 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
143 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
144 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
151 ldap_pvt_thread_mutex_destroy( &connections_mutex );
156 * shutdown all connections
158 int connections_shutdown(void)
162 ldap_pvt_thread_mutex_lock( &connections_mutex );
164 for ( i = 0; i < dtblsize; i++ ) {
165 if( connections[i].c_struct_state != SLAP_C_USED ) {
169 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
171 /* connections_mutex and c_mutex are locked */
172 connection_closing( &connections[i] );
173 connection_close( &connections[i] );
175 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
178 ldap_pvt_thread_mutex_unlock( &connections_mutex );
184 * Timeout idle connections.
186 int connections_timeout_idle(time_t now)
192 for( c = connection_first( &connindex );
194 c = connection_next( c, &connindex ) )
196 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
198 connection_closing( c );
199 connection_close( c );
203 connection_done( c );
208 static Connection* connection_get( ber_socket_t s )
210 /* connections_mutex should be locked by caller */
215 LDAP_LOG(( "connection", LDAP_LEVEL_ENTRY,
216 "connection_get: socket %ld\n", (long)s ));
218 Debug( LDAP_DEBUG_ARGS,
219 "connection_get(%ld)\n",
223 assert( connections != NULL );
225 if(s == AC_SOCKET_INVALID) {
232 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
239 for(i=0; i<dtblsize; i++) {
240 ber_sockbuf_ctrl( connections[i].c_sb,
241 LBER_SB_OPT_GET_FD, &sd );
243 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
244 assert( connections[i].c_conn_state == SLAP_C_INVALID );
245 assert( connections[i].c_sb == 0 );
249 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
250 assert( connections[i].c_conn_state == SLAP_C_INVALID );
251 assert( sd == AC_SOCKET_INVALID );
255 /* state can actually change from used -> unused by resched,
256 * so don't assert details here.
270 ldap_pvt_thread_mutex_lock( &c->c_mutex );
272 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
273 if( c->c_struct_state != SLAP_C_USED ) {
274 /* connection must have been closed due to resched */
276 assert( c->c_conn_state == SLAP_C_INVALID );
277 assert( sd == AC_SOCKET_INVALID );
280 LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
281 "connection_get: connection %d not used\n", s ));
283 Debug( LDAP_DEBUG_TRACE,
284 "connection_get(%d): connection not used\n",
288 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
293 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
294 "connection_get: get for %d got connid %ld\n",s, c->c_connid ));
296 Debug( LDAP_DEBUG_TRACE,
297 "connection_get(%d): got connid=%ld\n",
303 assert( c->c_struct_state == SLAP_C_USED );
304 assert( c->c_conn_state != SLAP_C_INVALID );
305 assert( sd != AC_SOCKET_INVALID );
308 c->c_activitytime = slap_get_time();
310 if( global_idletimeout > 0 ) {
311 c->c_activitytime = slap_get_time();
319 static void connection_return( Connection *c )
321 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
324 long connection_init(
328 const char* peername,
329 const char* sockname,
337 assert( connections != NULL );
339 assert( dnsname != NULL );
340 assert( peername != NULL );
341 assert( sockname != NULL );
344 assert( tls_udp_option != 1 );
347 if( s == AC_SOCKET_INVALID ) {
349 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
350 "connection_init: init of socket %ld invalid.\n", (long)s ));
352 Debug( LDAP_DEBUG_ANY,
353 "connection_init(%ld): invalid.\n",
361 assert( s < dtblsize );
364 ldap_pvt_thread_mutex_lock( &connections_mutex );
375 for( i=0; i < dtblsize; i++) {
378 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
379 assert( connections[i].c_sb == 0 );
384 sd = AC_SOCKET_INVALID;
385 if (connections[i].c_sb != NULL)
386 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
388 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
389 assert( sd == AC_SOCKET_INVALID );
394 assert( connections[i].c_struct_state == SLAP_C_USED );
395 assert( connections[i].c_conn_state != SLAP_C_INVALID );
396 assert( sd != AC_SOCKET_INVALID );
401 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
402 "connection_init: skt %d connection table full (%d/%d)\n",
405 Debug( LDAP_DEBUG_ANY,
406 "connection_init(%d): connection table full (%d/%d)\n",
409 ldap_pvt_thread_mutex_unlock( &connections_mutex );
417 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
418 c->c_authmech = NULL;
423 c->c_listener_url = NULL;
424 c->c_peer_domain = NULL;
425 c->c_peer_name = NULL;
426 c->c_sock_name = NULL;
429 c->c_pending_ops = NULL;
431 c->c_sasl_bind_mech = NULL;
432 c->c_sasl_context = NULL;
433 c->c_sasl_extra = NULL;
435 c->c_sb = ber_sockbuf_alloc( );
438 ber_len_t max = sockbuf_max_incoming;
439 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
442 c->c_currentber = NULL;
444 /* should check status of thread calls */
445 ldap_pvt_thread_mutex_init( &c->c_mutex );
446 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
447 ldap_pvt_thread_cond_init( &c->c_write_cv );
449 c->c_struct_state = SLAP_C_UNUSED;
452 ldap_pvt_thread_mutex_lock( &c->c_mutex );
454 assert( c->c_struct_state == SLAP_C_UNUSED );
455 assert( c->c_authmech == NULL );
456 assert( c->c_dn == NULL );
457 assert( c->c_cdn == NULL );
458 assert( c->c_groups == NULL );
459 assert( c->c_listener_url == NULL );
460 assert( c->c_peer_domain == NULL );
461 assert( c->c_peer_name == NULL );
462 assert( c->c_sock_name == NULL );
463 assert( c->c_ops == NULL );
464 assert( c->c_pending_ops == NULL );
465 assert( c->c_sasl_bind_mech == NULL );
466 assert( c->c_sasl_context == NULL );
467 assert( c->c_sasl_extra == NULL );
468 assert( c->c_currentber == NULL );
470 c->c_listener_url = ch_strdup( url );
471 c->c_peer_domain = ch_strdup( dnsname );
472 c->c_peer_name = ch_strdup( peername );
473 c->c_sock_name = ch_strdup( sockname );
475 c->c_n_ops_received = 0;
476 c->c_n_ops_executing = 0;
477 c->c_n_ops_pending = 0;
478 c->c_n_ops_completed = 0;
484 /* set to zero until bind, implies LDAP_VERSION3 */
488 c->c_activitytime = c->c_starttime = slap_get_time();
490 if( global_idletimeout > 0 ) {
491 c->c_activitytime = c->c_starttime = slap_get_time();
495 #ifdef LDAP_CONNECTIONLESS
497 if (tls_udp_option == 2)
501 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
502 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
504 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
505 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
510 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
511 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
513 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
514 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
516 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
517 LBER_SBIOD_LEVEL_PROVIDER, NULL );
520 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
521 INT_MAX, (void*)"ldap_" );
524 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
525 c /* non-NULL */ ) < 0 )
528 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
529 "connection_init: conn %d set nonblocking failed\n",
532 Debug( LDAP_DEBUG_ANY,
533 "connection_init(%d, %s): set nonblocking failed\n",
534 s, c->c_peer_name, 0 );
538 id = c->c_connid = conn_nextid++;
540 c->c_conn_state = SLAP_C_INACTIVE;
541 c->c_struct_state = SLAP_C_USED;
543 c->c_ssf = c->c_transport_ssf = ssf;
547 if ( tls_udp_option == 1 ) {
549 c->c_needs_tls_accept = 1;
552 c->c_needs_tls_accept = 0;
557 slap_sasl_external( c, ssf, authid );
559 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
560 ldap_pvt_thread_mutex_unlock( &connections_mutex );
562 backend_connection_init(c);
567 void connection2anonymous( Connection *c )
569 assert( connections != NULL );
573 ber_len_t max = sockbuf_max_incoming;
574 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
577 if(c->c_authmech != NULL ) {
579 c->c_authmech = NULL;
582 if(c->c_dn != NULL) {
587 if(c->c_cdn != NULL) {
592 c->c_authc_backend = NULL;
593 c->c_authz_backend = NULL;
596 GroupAssertion *g, *n;
597 for (g = c->c_groups; g; g=n)
608 connection_destroy( Connection *c )
610 /* note: connections_mutex should be locked by caller */
613 assert( connections != NULL );
615 assert( c->c_struct_state != SLAP_C_UNUSED );
616 assert( c->c_conn_state != SLAP_C_INVALID );
617 assert( c->c_ops == NULL );
619 backend_connection_destroy(c);
624 c->c_activitytime = c->c_starttime = 0;
626 connection2anonymous( c );
628 if(c->c_listener_url != NULL) {
629 free(c->c_listener_url);
630 c->c_listener_url = NULL;
633 if(c->c_peer_domain != NULL) {
634 free(c->c_peer_domain);
635 c->c_peer_domain = NULL;
637 if(c->c_peer_name != NULL) {
640 * If peer was a domain socket, unlink. Mind you,
641 * they may be un-named. Should we leave this to
644 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
645 char *path = c->c_peer_name + 5;
650 #endif /* LDAP_PF_LOCAL */
652 free(c->c_peer_name);
653 c->c_peer_name = NULL;
655 if(c->c_sock_name != NULL) {
656 free(c->c_sock_name);
657 c->c_sock_name = NULL;
660 c->c_sasl_bind_in_progress = 0;
661 if(c->c_sasl_bind_mech != NULL) {
662 free(c->c_sasl_bind_mech);
663 c->c_sasl_bind_mech = NULL;
666 slap_sasl_close( c );
668 if ( c->c_currentber != NULL ) {
669 ber_free( c->c_currentber, 1 );
670 c->c_currentber = NULL;
673 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
674 if ( sd != AC_SOCKET_INVALID ) {
675 slapd_remove( sd, 0 );
677 Statslog( LDAP_DEBUG_STATS,
678 "conn=%ld fd=%d closed\n",
679 c->c_connid, sd, 0, 0, 0 );
682 ber_sockbuf_free( c->c_sb );
684 c->c_sb = ber_sockbuf_alloc( );
687 ber_len_t max = sockbuf_max_incoming;
688 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
691 c->c_conn_state = SLAP_C_INVALID;
692 c->c_struct_state = SLAP_C_UNUSED;
695 int connection_state_closing( Connection *c )
697 /* c_mutex must be locked by caller */
701 assert( c->c_struct_state == SLAP_C_USED );
703 state = c->c_conn_state;
705 assert( state != SLAP_C_INVALID );
707 return state == SLAP_C_CLOSING;
710 static void connection_abandon( Connection *c )
712 /* c_mutex must be locked by caller */
716 for( o = c->c_ops; o != NULL; o = o->o_next ) {
717 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
719 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
722 /* remove pending operations */
723 for( o = slap_op_pop( &c->c_pending_ops );
725 o = slap_op_pop( &c->c_pending_ops ) )
731 void connection_closing( Connection *c )
733 assert( connections != NULL );
735 assert( c->c_struct_state == SLAP_C_USED );
736 assert( c->c_conn_state != SLAP_C_INVALID );
738 /* c_mutex must be locked by caller */
740 if( c->c_conn_state != SLAP_C_CLOSING ) {
743 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
745 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
746 "connection_closing: conn %d readying socket %d for close.\n",
749 Debug( LDAP_DEBUG_TRACE,
750 "connection_closing: readying conn=%ld sd=%d for close\n",
751 c->c_connid, sd, 0 );
753 /* update state to closing */
754 c->c_conn_state = SLAP_C_CLOSING;
756 /* don't listen on this port anymore */
757 slapd_clr_read( sd, 1 );
759 /* abandon active operations */
760 connection_abandon( c );
762 /* wake write blocked operations */
763 slapd_clr_write( sd, 1 );
764 ldap_pvt_thread_cond_signal( &c->c_write_cv );
768 static void connection_close( Connection *c )
772 assert( connections != NULL );
774 assert( c->c_struct_state == SLAP_C_USED );
775 assert( c->c_conn_state == SLAP_C_CLOSING );
777 /* note: connections_mutex and c_mutex should be locked by caller */
779 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
780 if( c->c_ops != NULL ) {
782 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
783 "connection_close: conn %d deferring sd %d\n",
786 Debug( LDAP_DEBUG_TRACE,
787 "connection_close: deferring conn=%ld sd=%d\n",
788 c->c_connid, sd, 0 );
794 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
795 "connection_close: conn %d sd %d\n",
798 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
799 c->c_connid, sd, 0 );
801 connection_destroy( c );
804 unsigned long connections_nextid(void)
807 assert( connections != NULL );
809 ldap_pvt_thread_mutex_lock( &connections_mutex );
813 ldap_pvt_thread_mutex_unlock( &connections_mutex );
818 Connection* connection_first( ber_socket_t *index )
820 assert( connections != NULL );
821 assert( index != NULL );
823 ldap_pvt_thread_mutex_lock( &connections_mutex );
827 return connection_next(NULL, index);
830 Connection* connection_next( Connection *c, ber_socket_t *index )
832 assert( connections != NULL );
833 assert( index != NULL );
834 assert( *index <= dtblsize );
837 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
842 for(; *index < dtblsize; (*index)++) {
843 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
844 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
852 if( connections[*index].c_struct_state == SLAP_C_USED ) {
853 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
854 c = &connections[(*index)++];
858 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
859 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
863 ldap_pvt_thread_mutex_lock( &c->c_mutex );
869 void connection_done( Connection *c )
871 assert( connections != NULL );
874 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
877 ldap_pvt_thread_mutex_unlock( &connections_mutex );
881 * connection_activity - handle the request operation op on connection
882 * conn. This routine figures out what kind of operation it is and
883 * calls the appropriate stub to handle it.
887 connection_operation( void *arg_v )
890 struct co_arg *arg = arg_v;
891 ber_tag_t tag = arg->co_op->o_tag;
892 Connection *conn = arg->co_conn;
894 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
896 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
898 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
900 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
901 "connection_operation: conn %d SASL bind in progress (tag=%ld).\n",
902 conn->c_connid, (long)tag ));
904 Debug( LDAP_DEBUG_ANY, "connection_operation: "
905 "error: SASL bind in progress (tag=%ld).\n",
908 send_ldap_result( conn, arg->co_op,
909 rc = LDAP_OPERATIONS_ERROR,
910 NULL, "SASL bind in progress", NULL, NULL );
911 goto operations_error;
916 rc = do_bind( conn, arg->co_op );
919 case LDAP_REQ_UNBIND:
920 rc = do_unbind( conn, arg->co_op );
924 rc = do_add( conn, arg->co_op );
927 case LDAP_REQ_DELETE:
928 rc = do_delete( conn, arg->co_op );
931 case LDAP_REQ_MODRDN:
932 rc = do_modrdn( conn, arg->co_op );
935 case LDAP_REQ_MODIFY:
936 rc = do_modify( conn, arg->co_op );
939 case LDAP_REQ_COMPARE:
940 rc = do_compare( conn, arg->co_op );
943 case LDAP_REQ_SEARCH:
944 rc = do_search( conn, arg->co_op );
947 case LDAP_REQ_ABANDON:
948 rc = do_abandon( conn, arg->co_op );
951 case LDAP_REQ_EXTENDED:
952 rc = do_extended( conn, arg->co_op );
957 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
958 "connection_operation: conn %d unknown LDAP request 0x%lx\n",
959 conn->c_connid, tag ));
961 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
964 arg->co_op->o_tag = LBER_ERROR;
965 send_ldap_disconnect( conn, arg->co_op,
966 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
971 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
974 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
976 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
978 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
980 conn->c_n_ops_executing--;
981 conn->c_n_ops_completed++;
983 slap_op_remove( &conn->c_ops, arg->co_op );
984 slap_op_free( arg->co_op );
987 free( (char *) arg );
992 case LDAP_REQ_UNBIND:
993 /* c_mutex is locked */
994 connection_closing( conn );
998 conn->c_sasl_bind_in_progress =
999 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1001 if( conn->c_conn_state == SLAP_C_BINDING) {
1002 conn->c_conn_state = SLAP_C_ACTIVE;
1006 connection_resched( conn );
1008 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1013 int connection_read(ber_socket_t s)
1017 assert( connections != NULL );
1019 ldap_pvt_thread_mutex_lock( &connections_mutex );
1021 /* get (locked) connection */
1022 c = connection_get( s );
1026 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1027 "connection_read: sock %ld no connection\n",
1030 Debug( LDAP_DEBUG_ANY,
1031 "connection_read(%ld): no connection!\n",
1036 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1042 if( c->c_conn_state == SLAP_C_CLOSING ) {
1044 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1045 "connection_read: conn %d connection closing, ignoring input\n",
1048 Debug( LDAP_DEBUG_TRACE,
1049 "connection_read(%d): closing, ignoring input for id=%ld\n",
1050 s, c->c_connid, 0 );
1052 connection_return( c );
1053 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1058 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1059 "connection_read: conn %d checking for input.\n", c->c_connid ));
1061 Debug( LDAP_DEBUG_TRACE,
1062 "connection_read(%d): checking for input on id=%ld\n",
1063 s, c->c_connid, 0 );
1067 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1068 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1070 #if 0 /* required by next #if 0 */
1076 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1077 "connection_read: conn %d TLS accept error, error %d\n",
1080 Debug( LDAP_DEBUG_TRACE,
1081 "connection_read(%d): TLS accept error "
1082 "error=%d id=%ld, closing\n",
1083 s, rc, c->c_connid );
1085 c->c_needs_tls_accept = 0;
1086 /* connections_mutex and c_mutex are locked */
1087 connection_closing( c );
1090 /* Drain input before close, to allow SSL error codes
1091 * to propagate to client. */
1098 rc = select(s+1, &rfd, NULL, NULL, &tv);
1100 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
1104 connection_close( c );
1106 } else if ( rc == 0 ) {
1110 c->c_needs_tls_accept = 0;
1112 /* we need to let SASL know */
1113 ssl = (void *)ldap_pvt_tls_sb_ctx( c->c_sb );
1115 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1116 if( c->c_tls_ssf > c->c_ssf ) {
1117 c->c_ssf = c->c_tls_ssf;
1120 authid = (char *)ldap_pvt_tls_get_peer( ssl );
1121 slap_sasl_external( c, c->c_tls_ssf, authid );
1123 connection_return( c );
1124 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1129 #ifdef HAVE_CYRUS_SASL
1130 if ( c->c_sasl_layers ) {
1131 c->c_sasl_layers = 0;
1133 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
1135 if( rc != LDAP_SUCCESS ) {
1137 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1138 "connection_read: conn %d SASL install error %d, closing\n",
1141 Debug( LDAP_DEBUG_TRACE,
1142 "connection_read(%d): SASL install error "
1143 "error=%d id=%ld, closing\n",
1144 s, rc, c->c_connid );
1146 /* connections_mutex and c_mutex are locked */
1147 connection_closing( c );
1148 connection_close( c );
1149 connection_return( c );
1150 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1156 #define CONNECTION_INPUT_LOOP 1
1158 #ifdef DATA_READY_LOOP
1159 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
1160 #elif CONNECTION_INPUT_LOOP
1164 /* How do we do this without getting into a busy loop ? */
1165 rc = connection_input( c );
1170 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1171 "connection_read: conn %d input error %d, closing.\n",
1174 Debug( LDAP_DEBUG_TRACE,
1175 "connection_read(%d): input error=%d id=%ld, closing.\n",
1176 s, rc, c->c_connid );
1178 /* connections_mutex and c_mutex are locked */
1179 connection_closing( c );
1180 connection_close( c );
1181 connection_return( c );
1182 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1186 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1187 slapd_set_read( s, 1 );
1190 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1191 slapd_set_write( s, 1 );
1194 connection_return( c );
1195 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1209 #ifdef LDAP_CONNECTIONLESS
1214 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1217 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1218 "connection_input: conn %d ber_alloc failed.\n", conn->c_connid ));
1220 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1227 #ifdef LDAP_CONNECTIONLESS
1230 char peername[sizeof("IP=255.255.255.255:65336")];
1231 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1232 sizeof(struct sockaddr));
1233 if (len != sizeof(struct sockaddr))
1235 sprintf( peername, "IP=%s:%d",
1236 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1237 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1238 Statslog( LDAP_DEBUG_STATS,
1239 "conn=%ld UDP request from %s (%s) accepted.\n",
1240 conn->c_connid, peername,
1241 conn->c_sock_name, 0, 0 );
1244 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1245 if ( tag != LDAP_TAG_MESSAGE ) {
1249 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1252 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1253 "connection_input: conn %d ber_get_next failed, errno %d (%s).\n",
1254 conn->c_connid, err, sock_errstr(err) ));
1256 Debug( LDAP_DEBUG_TRACE,
1257 "ber_get_next on fd %d failed errno=%d (%s)\n",
1258 sd, err, sock_errstr(err) );
1260 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1261 /* log, close and send error */
1262 ber_free( conn->c_currentber, 1 );
1263 conn->c_currentber = NULL;
1270 ber = conn->c_currentber;
1271 conn->c_currentber = NULL;
1273 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1274 /* log, close and send error */
1276 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1277 "connection_input: conn %d ber_get_int returns 0x%lx.\n",
1278 conn->c_connid, tag ));
1280 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1287 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1288 /* log, close and send error */
1290 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1291 "connection_input: conn %d ber_peek_tag returns 0x%lx.\n",
1292 conn->c_connid, tag ));
1294 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1302 #ifdef LDAP_CONNECTIONLESS
1303 if (conn->c_is_udp) {
1304 if (tag == LBER_OCTETSTRING) {
1305 ber_get_stringa( ber, &cdn );
1306 tag = ber_peek_tag(ber, &len);
1308 if (tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH) {
1310 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1311 "connection_input: conn %d invalid req for UDP 0x%lx.\n",
1312 conn->c_connid, tag ));
1314 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1322 if(tag == LDAP_REQ_BIND) {
1323 /* immediately abandon all exiting operations upon BIND */
1324 connection_abandon( conn );
1327 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1329 #ifdef LDAP_CONNECTIONLESS
1330 op->o_peeraddr = peeraddr;
1333 op->o_protocol = LDAP_VERSION2;
1336 if ( conn->c_conn_state == SLAP_C_BINDING
1337 || conn->c_conn_state == SLAP_C_CLOSING )
1340 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1341 "connection_input: conn %d deferring operation\n",
1344 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1346 conn->c_n_ops_pending++;
1347 slap_op_add( &conn->c_pending_ops, op );
1350 conn->c_n_ops_executing++;
1351 connection_op_activate( conn, op );
1355 if ( conn->c_struct_state != SLAP_C_USED ) {
1356 /* connection must have got closed underneath us */
1360 assert( conn->c_struct_state == SLAP_C_USED );
1366 connection_resched( Connection *conn )
1370 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1373 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1375 /* us trylock to avoid possible deadlock */
1376 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1380 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1381 "connection_resched: conn %d reaquiring locks.\n",
1384 Debug( LDAP_DEBUG_TRACE,
1385 "connection_resched: reaquiring locks conn=%ld sd=%d\n",
1386 conn->c_connid, sd, 0 );
1389 * reaquire locks in the right order...
1390 * this may allow another thread to close this connection,
1391 * so recheck state below.
1393 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1394 ldap_pvt_thread_mutex_lock( &connections_mutex );
1395 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1398 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1400 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1401 "connection_resched: conn %d closed by other thread.\n",
1404 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1405 "closed by other thread conn=%ld sd=%d\n",
1406 conn->c_connid, sd, 0 );
1410 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1411 "connection_resched: conn %d attempting closing.\n",
1414 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1415 "attempting closing conn=%ld sd=%d\n",
1416 conn->c_connid, sd, 0 );
1418 connection_close( conn );
1421 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1425 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1426 /* other states need different handling */
1430 for( op = slap_op_pop( &conn->c_pending_ops );
1432 op = slap_op_pop( &conn->c_pending_ops ) )
1434 /* pending operations should not be marked for abandonment */
1435 assert(!op->o_abandon);
1437 conn->c_n_ops_pending--;
1438 conn->c_n_ops_executing++;
1440 connection_op_activate( conn, op );
1442 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1449 static int connection_op_activate( Connection *conn, Operation *op )
1453 ber_tag_t tag = op->o_tag;
1455 if(tag == LDAP_REQ_BIND) {
1456 conn->c_conn_state = SLAP_C_BINDING;
1459 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1460 arg->co_conn = conn;
1463 if (!arg->co_op->o_dn) {
1464 arg->co_op->o_authz = conn->c_authz;
1465 arg->co_op->o_dn = ch_strdup( conn->c_dn != NULL ? conn->c_dn : "" );
1467 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1468 (void) dn_normalize( arg->co_op->o_ndn );
1469 arg->co_op->o_authtype = conn->c_authtype;
1470 arg->co_op->o_authmech = conn->c_authmech != NULL
1471 ? ch_strdup( conn->c_authmech ) : NULL;
1473 if (!arg->co_op->o_protocol) {
1474 arg->co_op->o_protocol = conn->c_protocol
1475 ? conn->c_protocol : LDAP_VERSION3;
1477 arg->co_op->o_connid = conn->c_connid;
1479 slap_op_add( &conn->c_ops, arg->co_op );
1481 status = ldap_pvt_thread_pool_submit( &connection_pool,
1482 connection_operation, (void *) arg );
1484 if ( status != 0 ) {
1486 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1487 "connection_op_activate: conn %d thread pool submit failed.\n",
1490 Debug( LDAP_DEBUG_ANY,
1491 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1493 /* should move op to pending list */
1499 int connection_write(ber_socket_t s)
1502 assert( connections != NULL );
1504 ldap_pvt_thread_mutex_lock( &connections_mutex );
1506 c = connection_get( s );
1508 slapd_clr_write( s, 0);
1512 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1513 "connection_write: sock %ld no connection!\n",(long)s));
1515 Debug( LDAP_DEBUG_ANY,
1516 "connection_write(%ld): no connection!\n",
1520 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1527 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1528 "connection_write conn %d waking output.\n",
1531 Debug( LDAP_DEBUG_TRACE,
1532 "connection_write(%d): waking output for id=%ld\n",
1533 s, c->c_connid, 0 );
1535 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1537 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1538 slapd_set_read( s, 1 );
1539 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1540 slapd_set_write( s, 1 );
1541 connection_return( c );
1542 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1548 * Create client side and server side connection structures, connected to
1549 * one another, for the front end to use for searches on arbitrary back ends.
1552 int connection_internal_open( Connection **conn, LDAP **ldp, const char *id )
1555 ber_socket_t fd[2] = {-1,-1};
1561 rc = lutil_pair( fd );
1566 rc = connection_init( fd[1], "INT", "localhost",
1567 "localhost:0", "localhost:00", 0, 256, id );
1573 slapd_add_internal( fd[1] );
1575 /* A search operation, number 0 */
1576 op = slap_op_alloc( NULL, 0, LDAP_REQ_SEARCH, 0);
1577 op->o_ndn = ch_strdup( id );
1578 op->o_protocol = LDAP_VERSION3;
1580 (*conn) = connection_get( fd[1] );
1581 (*conn)->c_ops = op;
1582 (*conn)->c_conn_state = SLAP_C_ACTIVE;
1585 /* Create the client side of the connection */
1586 rc = ldap_open_internal_connection( ldp, &(fd[0]) );
1587 if( rc != LDAP_SUCCESS ) {
1592 /* The connection_get() will have locked the connection's mutex */
1593 ldap_pvt_thread_mutex_unlock( &((*conn)->c_mutex) );
1595 return LDAP_SUCCESS;
1599 void connection_internal_close( Connection *conn )
1601 Operation *op = conn->c_ops;
1603 slap_op_remove( &conn->c_ops, op );
1605 connection_closing( conn );
1606 connection_close( conn );