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>
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 );
69 * Initialize connection management infrastructure.
71 int connections_init(void)
73 assert( connections == NULL );
75 if( connections != NULL) {
77 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
78 "connections_init: already initialized.\n" ));
80 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
86 /* should check return of every call */
87 ldap_pvt_thread_mutex_init( &connections_mutex );
89 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
91 if( connections == NULL ) {
93 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
94 "connections_init: allocation (%d * %ld) of connection array failed\n",
95 dtblsize, (long) sizeof(Connection) ));
97 Debug( LDAP_DEBUG_ANY,
98 "connections_init: allocation (%d*%ld) of connection array failed\n",
99 dtblsize, (long) sizeof(Connection), 0 );
104 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
105 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
108 * per entry initialization of the Connection array initialization
109 * will be done by connection_init()
116 * Destroy connection management infrastructure.
118 int connections_destroy(void)
122 /* should check return of every call */
124 if( connections == NULL) {
126 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
127 "connections_destroy: nothing to destroy.\n"));
129 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
135 for ( i = 0; i < dtblsize; i++ ) {
136 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
137 ber_sockbuf_free( connections[i].c_sb );
138 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
139 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
140 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
147 ldap_pvt_thread_mutex_destroy( &connections_mutex );
152 * shutdown all connections
154 int connections_shutdown(void)
158 ldap_pvt_thread_mutex_lock( &connections_mutex );
160 for ( i = 0; i < dtblsize; i++ ) {
161 if( connections[i].c_struct_state != SLAP_C_USED ) {
165 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
167 /* connections_mutex and c_mutex are locked */
168 connection_closing( &connections[i] );
169 connection_close( &connections[i] );
171 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
174 ldap_pvt_thread_mutex_unlock( &connections_mutex );
180 * Timeout idle connections.
182 int connections_timeout_idle(time_t now)
188 for( c = connection_first( &connindex );
190 c = connection_next( c, &connindex ) )
192 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
194 connection_closing( c );
195 connection_close( c );
199 connection_done( c );
204 static Connection* connection_get( ber_socket_t s )
206 /* connections_mutex should be locked by caller */
211 LDAP_LOG(( "connection", LDAP_LEVEL_ENTRY,
212 "connection_get: socket %ld\n", (long)s ));
214 Debug( LDAP_DEBUG_ARGS,
215 "connection_get(%ld)\n",
219 assert( connections != NULL );
221 if(s == AC_SOCKET_INVALID) {
228 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
235 for(i=0; i<dtblsize; i++) {
236 ber_sockbuf_ctrl( connections[i].c_sb,
237 LBER_SB_OPT_GET_FD, &sd );
239 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
240 assert( connections[i].c_conn_state == SLAP_C_INVALID );
241 assert( connections[i].c_sb == 0 );
245 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
246 assert( connections[i].c_conn_state == SLAP_C_INVALID );
247 assert( sd == AC_SOCKET_INVALID );
251 /* state can actually change from used -> unused by resched,
252 * so don't assert details here.
266 ldap_pvt_thread_mutex_lock( &c->c_mutex );
268 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
269 if( c->c_struct_state != SLAP_C_USED ) {
270 /* connection must have been closed due to resched */
272 assert( c->c_conn_state == SLAP_C_INVALID );
273 assert( sd == AC_SOCKET_INVALID );
276 LDAP_LOG(( "connection", LDAP_LEVEL_ARGS,
277 "connection_get: connection %d not used\n", s ));
279 Debug( LDAP_DEBUG_TRACE,
280 "connection_get(%d): connection not used\n",
284 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
289 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
290 "connection_get: get for %d got connid %ld\n",s, c->c_connid ));
292 Debug( LDAP_DEBUG_TRACE,
293 "connection_get(%d): got connid=%ld\n",
299 assert( c->c_struct_state == SLAP_C_USED );
300 assert( c->c_conn_state != SLAP_C_INVALID );
301 assert( sd != AC_SOCKET_INVALID );
304 c->c_activitytime = slap_get_time();
306 if( global_idletimeout > 0 ) {
307 c->c_activitytime = slap_get_time();
315 static void connection_return( Connection *c )
317 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
320 long connection_init(
324 const char* peername,
325 const char* sockname,
333 assert( connections != NULL );
335 assert( dnsname != NULL );
336 assert( peername != NULL );
337 assert( sockname != NULL );
340 assert( tls_udp_option != 1 );
343 if( s == AC_SOCKET_INVALID ) {
345 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
346 "connection_init: init of socket %ld invalid.\n", (long)s ));
348 Debug( LDAP_DEBUG_ANY,
349 "connection_init(%ld): invalid.\n",
357 assert( s < dtblsize );
360 ldap_pvt_thread_mutex_lock( &connections_mutex );
371 for( i=0; i < dtblsize; i++) {
374 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
375 assert( connections[i].c_sb == 0 );
380 sd = AC_SOCKET_INVALID;
381 if (connections[i].c_sb != NULL)
382 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
384 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
385 assert( sd == AC_SOCKET_INVALID );
390 assert( connections[i].c_struct_state == SLAP_C_USED );
391 assert( connections[i].c_conn_state != SLAP_C_INVALID );
392 assert( sd != AC_SOCKET_INVALID );
397 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
398 "connection_init: skt %d connection table full (%d/%d)\n",
401 Debug( LDAP_DEBUG_ANY,
402 "connection_init(%d): connection table full (%d/%d)\n",
405 ldap_pvt_thread_mutex_unlock( &connections_mutex );
413 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
414 c->c_authmech = NULL;
419 c->c_listener_url = NULL;
420 c->c_peer_domain = NULL;
421 c->c_peer_name = NULL;
422 c->c_sock_name = NULL;
425 c->c_pending_ops = NULL;
427 c->c_sasl_bind_mech = NULL;
428 c->c_sasl_context = NULL;
429 c->c_sasl_extra = NULL;
431 c->c_sb = ber_sockbuf_alloc( );
434 ber_len_t max = sockbuf_max_incoming;
435 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
438 c->c_currentber = NULL;
440 /* should check status of thread calls */
441 ldap_pvt_thread_mutex_init( &c->c_mutex );
442 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
443 ldap_pvt_thread_cond_init( &c->c_write_cv );
445 c->c_struct_state = SLAP_C_UNUSED;
448 ldap_pvt_thread_mutex_lock( &c->c_mutex );
450 assert( c->c_struct_state == SLAP_C_UNUSED );
451 assert( c->c_authmech == NULL );
452 assert( c->c_dn == NULL );
453 assert( c->c_cdn == NULL );
454 assert( c->c_groups == NULL );
455 assert( c->c_listener_url == NULL );
456 assert( c->c_peer_domain == NULL );
457 assert( c->c_peer_name == NULL );
458 assert( c->c_sock_name == NULL );
459 assert( c->c_ops == NULL );
460 assert( c->c_pending_ops == NULL );
461 assert( c->c_sasl_bind_mech == NULL );
462 assert( c->c_sasl_context == NULL );
463 assert( c->c_sasl_extra == NULL );
464 assert( c->c_currentber == NULL );
466 c->c_listener_url = ch_strdup( url );
467 c->c_peer_domain = ch_strdup( dnsname );
468 c->c_peer_name = ch_strdup( peername );
469 c->c_sock_name = ch_strdup( sockname );
471 c->c_n_ops_received = 0;
472 c->c_n_ops_executing = 0;
473 c->c_n_ops_pending = 0;
474 c->c_n_ops_completed = 0;
480 /* set to zero until bind, implies LDAP_VERSION3 */
484 c->c_activitytime = c->c_starttime = slap_get_time();
486 if( global_idletimeout > 0 ) {
487 c->c_activitytime = c->c_starttime = slap_get_time();
491 #ifdef LDAP_CONNECTIONLESS
493 if (tls_udp_option == 2)
497 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
498 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
500 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
501 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
506 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
507 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
509 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
510 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
512 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
513 LBER_SBIOD_LEVEL_PROVIDER, NULL );
516 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
517 INT_MAX, (void*)"ldap_" );
520 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
521 c /* non-NULL */ ) < 0 )
524 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
525 "connection_init: conn %d set nonblocking failed\n",
528 Debug( LDAP_DEBUG_ANY,
529 "connection_init(%d, %s): set nonblocking failed\n",
530 s, c->c_peer_name, 0 );
534 id = c->c_connid = conn_nextid++;
536 c->c_conn_state = SLAP_C_INACTIVE;
537 c->c_struct_state = SLAP_C_USED;
539 c->c_ssf = c->c_transport_ssf = ssf;
543 if ( tls_udp_option == 1 ) {
545 c->c_needs_tls_accept = 1;
548 c->c_needs_tls_accept = 0;
553 slap_sasl_external( c, ssf, authid );
555 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
556 ldap_pvt_thread_mutex_unlock( &connections_mutex );
558 backend_connection_init(c);
563 void connection2anonymous( Connection *c )
565 assert( connections != NULL );
569 ber_len_t max = sockbuf_max_incoming;
570 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
573 if(c->c_authmech != NULL ) {
575 c->c_authmech = NULL;
578 if(c->c_dn != NULL) {
583 if(c->c_cdn != NULL) {
588 c->c_authc_backend = NULL;
589 c->c_authz_backend = NULL;
592 GroupAssertion *g, *n;
593 for (g = c->c_groups; g; g=n)
604 connection_destroy( Connection *c )
606 /* note: connections_mutex should be locked by caller */
609 assert( connections != NULL );
611 assert( c->c_struct_state != SLAP_C_UNUSED );
612 assert( c->c_conn_state != SLAP_C_INVALID );
613 assert( c->c_ops == NULL );
615 backend_connection_destroy(c);
620 c->c_activitytime = c->c_starttime = 0;
622 connection2anonymous( c );
624 if(c->c_listener_url != NULL) {
625 free(c->c_listener_url);
626 c->c_listener_url = NULL;
629 if(c->c_peer_domain != NULL) {
630 free(c->c_peer_domain);
631 c->c_peer_domain = NULL;
633 if(c->c_peer_name != NULL) {
636 * If peer was a domain socket, unlink. Mind you,
637 * they may be un-named. Should we leave this to
640 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
641 char *path = c->c_peer_name + 5;
646 #endif /* LDAP_PF_LOCAL */
648 free(c->c_peer_name);
649 c->c_peer_name = NULL;
651 if(c->c_sock_name != NULL) {
652 free(c->c_sock_name);
653 c->c_sock_name = NULL;
656 c->c_sasl_bind_in_progress = 0;
657 if(c->c_sasl_bind_mech != NULL) {
658 free(c->c_sasl_bind_mech);
659 c->c_sasl_bind_mech = NULL;
662 slap_sasl_close( c );
664 if ( c->c_currentber != NULL ) {
665 ber_free( c->c_currentber, 1 );
666 c->c_currentber = NULL;
669 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
670 if ( sd != AC_SOCKET_INVALID ) {
671 slapd_remove( sd, 0 );
673 Statslog( LDAP_DEBUG_STATS,
674 "conn=%ld fd=%d closed\n",
675 c->c_connid, sd, 0, 0, 0 );
678 ber_sockbuf_free( c->c_sb );
680 c->c_sb = ber_sockbuf_alloc( );
683 ber_len_t max = sockbuf_max_incoming;
684 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
687 c->c_conn_state = SLAP_C_INVALID;
688 c->c_struct_state = SLAP_C_UNUSED;
691 int connection_state_closing( Connection *c )
693 /* c_mutex must be locked by caller */
697 assert( c->c_struct_state == SLAP_C_USED );
699 state = c->c_conn_state;
701 assert( state != SLAP_C_INVALID );
703 return state == SLAP_C_CLOSING;
706 static void connection_abandon( Connection *c )
708 /* c_mutex must be locked by caller */
712 for( o = c->c_ops; o != NULL; o = o->o_next ) {
713 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
715 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
718 /* remove pending operations */
719 for( o = slap_op_pop( &c->c_pending_ops );
721 o = slap_op_pop( &c->c_pending_ops ) )
727 void connection_closing( Connection *c )
729 assert( connections != NULL );
731 assert( c->c_struct_state == SLAP_C_USED );
732 assert( c->c_conn_state != SLAP_C_INVALID );
734 /* c_mutex must be locked by caller */
736 if( c->c_conn_state != SLAP_C_CLOSING ) {
739 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
741 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
742 "connection_closing: conn %d readying socket %d for close.\n",
745 Debug( LDAP_DEBUG_TRACE,
746 "connection_closing: readying conn=%ld sd=%d for close\n",
747 c->c_connid, sd, 0 );
749 /* update state to closing */
750 c->c_conn_state = SLAP_C_CLOSING;
752 /* don't listen on this port anymore */
753 slapd_clr_read( sd, 1 );
755 /* abandon active operations */
756 connection_abandon( c );
758 /* wake write blocked operations */
759 slapd_clr_write( sd, 1 );
760 ldap_pvt_thread_cond_signal( &c->c_write_cv );
764 static void connection_close( Connection *c )
768 assert( connections != NULL );
770 assert( c->c_struct_state == SLAP_C_USED );
771 assert( c->c_conn_state == SLAP_C_CLOSING );
773 /* note: connections_mutex and c_mutex should be locked by caller */
775 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
776 if( c->c_ops != NULL ) {
778 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
779 "connection_close: conn %d deferring sd %d\n",
782 Debug( LDAP_DEBUG_TRACE,
783 "connection_close: deferring conn=%ld sd=%d\n",
784 c->c_connid, sd, 0 );
790 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
791 "connection_close: conn %d sd %d\n",
794 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
795 c->c_connid, sd, 0 );
797 connection_destroy( c );
800 unsigned long connections_nextid(void)
803 assert( connections != NULL );
805 ldap_pvt_thread_mutex_lock( &connections_mutex );
809 ldap_pvt_thread_mutex_unlock( &connections_mutex );
814 Connection* connection_first( ber_socket_t *index )
816 assert( connections != NULL );
817 assert( index != NULL );
819 ldap_pvt_thread_mutex_lock( &connections_mutex );
823 return connection_next(NULL, index);
826 Connection* connection_next( Connection *c, ber_socket_t *index )
828 assert( connections != NULL );
829 assert( index != NULL );
830 assert( *index <= dtblsize );
833 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
838 for(; *index < dtblsize; (*index)++) {
839 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
840 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
848 if( connections[*index].c_struct_state == SLAP_C_USED ) {
849 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
850 c = &connections[(*index)++];
854 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
855 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
859 ldap_pvt_thread_mutex_lock( &c->c_mutex );
865 void connection_done( Connection *c )
867 assert( connections != NULL );
870 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
873 ldap_pvt_thread_mutex_unlock( &connections_mutex );
877 * connection_activity - handle the request operation op on connection
878 * conn. This routine figures out what kind of operation it is and
879 * calls the appropriate stub to handle it.
883 connection_operation( void *arg_v )
886 struct co_arg *arg = arg_v;
887 ber_tag_t tag = arg->co_op->o_tag;
888 Connection *conn = arg->co_conn;
890 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
892 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
894 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
896 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
897 "connection_operation: conn %d SASL bind in progress (tag=%ld).\n",
898 conn->c_connid, (long)tag ));
900 Debug( LDAP_DEBUG_ANY, "connection_operation: "
901 "error: SASL bind in progress (tag=%ld).\n",
904 send_ldap_result( conn, arg->co_op,
905 rc = LDAP_OPERATIONS_ERROR,
906 NULL, "SASL bind in progress", NULL, NULL );
907 goto operations_error;
912 rc = do_bind( conn, arg->co_op );
915 case LDAP_REQ_UNBIND:
916 rc = do_unbind( conn, arg->co_op );
920 rc = do_add( conn, arg->co_op );
923 case LDAP_REQ_DELETE:
924 rc = do_delete( conn, arg->co_op );
927 case LDAP_REQ_MODRDN:
928 rc = do_modrdn( conn, arg->co_op );
931 case LDAP_REQ_MODIFY:
932 rc = do_modify( conn, arg->co_op );
935 case LDAP_REQ_COMPARE:
936 rc = do_compare( conn, arg->co_op );
939 case LDAP_REQ_SEARCH:
940 rc = do_search( conn, arg->co_op );
943 case LDAP_REQ_ABANDON:
944 rc = do_abandon( conn, arg->co_op );
947 case LDAP_REQ_EXTENDED:
948 rc = do_extended( conn, arg->co_op );
953 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
954 "connection_operation: conn %d unknown LDAP request 0x%lx\n",
955 conn->c_connid, tag ));
957 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
960 arg->co_op->o_tag = LBER_ERROR;
961 send_ldap_disconnect( conn, arg->co_op,
962 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
967 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
970 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
972 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
974 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
976 conn->c_n_ops_executing--;
977 conn->c_n_ops_completed++;
979 slap_op_remove( &conn->c_ops, arg->co_op );
980 slap_op_free( arg->co_op );
983 free( (char *) arg );
988 case LDAP_REQ_UNBIND:
989 /* c_mutex is locked */
990 connection_closing( conn );
994 conn->c_sasl_bind_in_progress =
995 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
997 if( conn->c_conn_state == SLAP_C_BINDING) {
998 conn->c_conn_state = SLAP_C_ACTIVE;
1002 connection_resched( conn );
1004 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1009 int connection_read(ber_socket_t s)
1013 assert( connections != NULL );
1015 ldap_pvt_thread_mutex_lock( &connections_mutex );
1017 /* get (locked) connection */
1018 c = connection_get( s );
1022 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1023 "connection_read: sock %ld no connection\n",
1026 Debug( LDAP_DEBUG_ANY,
1027 "connection_read(%ld): no connection!\n",
1032 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1038 if( c->c_conn_state == SLAP_C_CLOSING ) {
1040 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1041 "connection_read: conn %d connection closing, ignoring input\n",
1044 Debug( LDAP_DEBUG_TRACE,
1045 "connection_read(%d): closing, ignoring input for id=%ld\n",
1046 s, c->c_connid, 0 );
1048 connection_return( c );
1049 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1054 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1055 "connection_read: conn %d checking for input.\n", c->c_connid ));
1057 Debug( LDAP_DEBUG_TRACE,
1058 "connection_read(%d): checking for input on id=%ld\n",
1059 s, c->c_connid, 0 );
1063 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1064 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1070 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1071 "connection_read: conn %d TLS accept error, error %d\n",
1074 Debug( LDAP_DEBUG_TRACE,
1075 "connection_read(%d): TLS accept error "
1076 "error=%d id=%ld, closing\n",
1077 s, rc, c->c_connid );
1079 c->c_needs_tls_accept = 0;
1080 /* connections_mutex and c_mutex are locked */
1081 connection_closing( c );
1084 /* Drain input before close, to allow SSL error codes
1085 * to propagate to client. */
1092 rc = select(s+1, &rfd, NULL, NULL, &tv);
1094 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
1098 connection_close( c );
1100 } else if ( rc == 0 ) {
1104 c->c_needs_tls_accept = 0;
1106 /* we need to let SASL know */
1107 ssl = (void *)ldap_pvt_tls_sb_ctx( c->c_sb );
1109 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1110 if( c->c_tls_ssf > c->c_ssf ) {
1111 c->c_ssf = c->c_tls_ssf;
1114 authid = (char *)ldap_pvt_tls_get_peer( ssl );
1115 slap_sasl_external( c, c->c_tls_ssf, authid );
1117 connection_return( c );
1118 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1123 #ifdef HAVE_CYRUS_SASL
1124 if ( c->c_sasl_layers ) {
1125 c->c_sasl_layers = 0;
1127 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
1129 if( rc != LDAP_SUCCESS ) {
1131 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1132 "connection_read: conn %d SASL install error %d, closing\n",
1135 Debug( LDAP_DEBUG_TRACE,
1136 "connection_read(%d): SASL install error "
1137 "error=%d id=%ld, closing\n",
1138 s, rc, c->c_connid );
1140 /* connections_mutex and c_mutex are locked */
1141 connection_closing( c );
1142 connection_close( c );
1143 connection_return( c );
1144 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1150 #define CONNECTION_INPUT_LOOP 1
1152 #ifdef DATA_READY_LOOP
1153 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
1154 #elif CONNECTION_INPUT_LOOP
1158 /* How do we do this without getting into a busy loop ? */
1159 rc = connection_input( c );
1164 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1165 "connection_read: conn %d input error %d, closing.\n",
1168 Debug( LDAP_DEBUG_TRACE,
1169 "connection_read(%d): input error=%d id=%ld, closing.\n",
1170 s, rc, c->c_connid );
1172 /* connections_mutex and c_mutex are locked */
1173 connection_closing( c );
1174 connection_close( c );
1175 connection_return( c );
1176 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1180 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1181 slapd_set_read( s, 1 );
1184 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1185 slapd_set_write( s, 1 );
1188 connection_return( c );
1189 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1203 #ifdef LDAP_CONNECTIONLESS
1208 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1211 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1212 "connection_input: conn %d ber_alloc failed.\n", conn->c_connid ));
1214 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1221 #ifdef LDAP_CONNECTIONLESS
1224 char peername[sizeof("IP=255.255.255.255:65336")];
1225 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1226 sizeof(struct sockaddr));
1227 if (len != sizeof(struct sockaddr))
1229 sprintf( peername, "IP=%s:%d",
1230 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1231 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1232 Statslog( LDAP_DEBUG_STATS,
1233 "conn=%ld UDP request from %s (%s) accepted.\n",
1234 conn->c_connid, peername,
1235 conn->c_sock_name, 0, 0 );
1238 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1239 if ( tag != LDAP_TAG_MESSAGE ) {
1243 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1246 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1247 "connection_input: conn %d ber_get_next failed, errno %d (%s).\n",
1248 conn->c_connid, err, sock_errstr(err) ));
1250 Debug( LDAP_DEBUG_TRACE,
1251 "ber_get_next on fd %d failed errno=%d (%s)\n",
1252 sd, err, sock_errstr(err) );
1254 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1255 /* log, close and send error */
1256 ber_free( conn->c_currentber, 1 );
1257 conn->c_currentber = NULL;
1264 ber = conn->c_currentber;
1265 conn->c_currentber = NULL;
1267 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1268 /* log, close and send error */
1270 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1271 "connection_input: conn %d ber_get_int returns 0x%lx.\n",
1272 conn->c_connid, tag ));
1274 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1281 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1282 /* log, close and send error */
1284 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1285 "connection_input: conn %d ber_peek_tag returns 0x%lx.\n",
1286 conn->c_connid, tag ));
1288 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1296 #ifdef LDAP_CONNECTIONLESS
1297 if (conn->c_is_udp) {
1298 if (tag == LBER_OCTETSTRING) {
1299 ber_get_stringa( ber, &cdn );
1300 tag = ber_peek_tag(ber, &len);
1302 if (tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH) {
1304 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1305 "connection_input: conn %d invalid req for UDP 0x%lx.\n",
1306 conn->c_connid, tag ));
1308 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1316 if(tag == LDAP_REQ_BIND) {
1317 /* immediately abandon all exiting operations upon BIND */
1318 connection_abandon( conn );
1321 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1323 #ifdef LDAP_CONNECTIONLESS
1324 op->o_peeraddr = peeraddr;
1327 op->o_protocol = LDAP_VERSION2;
1330 if ( conn->c_conn_state == SLAP_C_BINDING
1331 || conn->c_conn_state == SLAP_C_CLOSING )
1334 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1335 "connection_input: conn %d deferring operation\n",
1338 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1340 conn->c_n_ops_pending++;
1341 slap_op_add( &conn->c_pending_ops, op );
1344 conn->c_n_ops_executing++;
1345 connection_op_activate( conn, op );
1349 if ( conn->c_struct_state != SLAP_C_USED ) {
1350 /* connection must have got closed underneath us */
1354 assert( conn->c_struct_state == SLAP_C_USED );
1360 connection_resched( Connection *conn )
1364 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1367 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1369 /* us trylock to avoid possible deadlock */
1370 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1374 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1375 "connection_resched: conn %d reaquiring locks.\n",
1378 Debug( LDAP_DEBUG_TRACE,
1379 "connection_resched: reaquiring locks conn=%ld sd=%d\n",
1380 conn->c_connid, sd, 0 );
1383 * reaquire locks in the right order...
1384 * this may allow another thread to close this connection,
1385 * so recheck state below.
1387 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1388 ldap_pvt_thread_mutex_lock( &connections_mutex );
1389 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1392 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1394 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1395 "connection_resched: conn %d closed by other thread.\n",
1398 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1399 "closed by other thread conn=%ld sd=%d\n",
1400 conn->c_connid, sd, 0 );
1404 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1405 "connection_resched: conn %d attempting closing.\n",
1408 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1409 "attempting closing conn=%ld sd=%d\n",
1410 conn->c_connid, sd, 0 );
1412 connection_close( conn );
1415 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1419 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1420 /* other states need different handling */
1424 for( op = slap_op_pop( &conn->c_pending_ops );
1426 op = slap_op_pop( &conn->c_pending_ops ) )
1428 /* pending operations should not be marked for abandonment */
1429 assert(!op->o_abandon);
1431 conn->c_n_ops_pending--;
1432 conn->c_n_ops_executing++;
1434 connection_op_activate( conn, op );
1436 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1443 static int connection_op_activate( Connection *conn, Operation *op )
1447 ber_tag_t tag = op->o_tag;
1449 if(tag == LDAP_REQ_BIND) {
1450 conn->c_conn_state = SLAP_C_BINDING;
1453 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1454 arg->co_conn = conn;
1457 if (!arg->co_op->o_dn) {
1458 arg->co_op->o_authz = conn->c_authz;
1459 arg->co_op->o_dn = ch_strdup( conn->c_dn != NULL ? conn->c_dn : "" );
1461 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1462 (void) dn_normalize( arg->co_op->o_ndn );
1463 arg->co_op->o_authtype = conn->c_authtype;
1464 arg->co_op->o_authmech = conn->c_authmech != NULL
1465 ? ch_strdup( conn->c_authmech ) : NULL;
1467 if (!arg->co_op->o_protocol) {
1468 arg->co_op->o_protocol = conn->c_protocol
1469 ? conn->c_protocol : LDAP_VERSION3;
1471 arg->co_op->o_connid = conn->c_connid;
1473 slap_op_add( &conn->c_ops, arg->co_op );
1475 status = ldap_pvt_thread_pool_submit( &connection_pool,
1476 connection_operation, (void *) arg );
1478 if ( status != 0 ) {
1480 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1481 "connection_op_activate: conn %d thread pool submit failed.\n",
1484 Debug( LDAP_DEBUG_ANY,
1485 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1487 /* should move op to pending list */
1493 int connection_write(ber_socket_t s)
1496 assert( connections != NULL );
1498 ldap_pvt_thread_mutex_lock( &connections_mutex );
1500 c = connection_get( s );
1502 slapd_clr_write( s, 0);
1506 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1507 "connection_write: sock %ld no connection!\n",(long)s));
1509 Debug( LDAP_DEBUG_ANY,
1510 "connection_write(%ld): no connection!\n",
1514 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1521 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1522 "connection_write conn %d waking output.\n",
1525 Debug( LDAP_DEBUG_TRACE,
1526 "connection_write(%d): waking output for id=%ld\n",
1527 s, c->c_connid, 0 );
1529 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1531 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1532 slapd_set_read( s, 1 );
1533 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1534 slapd_set_write( s, 1 );
1535 connection_return( c );
1536 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1542 * Create client side and server side connection structures, connected to
1543 * one another, for the front end to use for searches on arbitrary back ends.
1546 int connection_internal_open( Connection **conn, LDAP **ldp, const char *id )
1549 ber_socket_t fd[2] = {-1,-1};
1555 rc = lutil_pair( fd );
1560 rc = connection_init( fd[1], "INT", "localhost",
1561 "localhost:0", "localhost:00", 0, 256, id );
1567 slapd_add_internal( fd[1] );
1569 /* A search operation, number 0 */
1570 op = slap_op_alloc( NULL, 0, LDAP_REQ_SEARCH, 0);
1571 op->o_ndn = ch_strdup( id );
1572 op->o_protocol = LDAP_VERSION3;
1574 (*conn) = connection_get( fd[1] );
1575 (*conn)->c_ops = op;
1576 (*conn)->c_conn_state = SLAP_C_ACTIVE;
1579 /* Create the client side of the connection */
1580 rc = ldap_open_internal_connection( ldp, &(fd[0]) );
1581 if( rc != LDAP_SUCCESS ) {
1586 /* The connection_get() will have locked the connection's mutex */
1587 ldap_pvt_thread_mutex_unlock( &((*conn)->c_mutex) );
1589 return LDAP_SUCCESS;
1593 void connection_internal_close( Connection *conn )
1595 Operation *op = conn->c_ops;
1597 slap_op_remove( &conn->c_ops, op );
1599 connection_closing( conn );
1600 connection_close( conn );