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 /* protected by connections_mutex */
24 static ldap_pvt_thread_mutex_t connections_mutex;
25 static Connection *connections = NULL;
26 static unsigned long conn_nextid = 0;
28 /* structure state (protected by connections_mutex) */
29 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
30 #define SLAP_C_UNUSED 0x01
31 #define SLAP_C_USED 0x02
33 /* connection state (protected by c_mutex ) */
34 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
35 #define SLAP_C_INACTIVE 0x01 /* zero threads */
36 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
37 #define SLAP_C_BINDING 0x03 /* binding */
38 #define SLAP_C_CLOSING 0x04 /* closing */
41 connection_state2str( int state )
44 case SLAP_C_INVALID: return "!";
45 case SLAP_C_INACTIVE: return "|";
46 case SLAP_C_ACTIVE: return "";
47 case SLAP_C_BINDING: return "B";
48 case SLAP_C_CLOSING: return "C";
54 static Connection* connection_get( ber_socket_t s );
56 static int connection_input( Connection *c );
57 static void connection_close( Connection *c );
59 static int connection_op_activate( Connection *conn, Operation *op );
60 static int connection_resched( Connection *conn );
61 static void connection_abandon( Connection *conn );
62 static void connection_destroy( Connection *c );
70 * Initialize connection management infrastructure.
72 int connections_init(void)
74 assert( connections == NULL );
76 if( connections != NULL) {
78 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
79 "connections_init: already initialized.\n" ));
81 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
87 /* should check return of every call */
88 ldap_pvt_thread_mutex_init( &connections_mutex );
90 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
92 if( connections == NULL ) {
94 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
95 "connections_init: allocation (%d * %ld) of connection array failed\n",
96 dtblsize, (long) sizeof(Connection) ));
98 Debug( LDAP_DEBUG_ANY,
99 "connections_init: allocation (%d*%ld) of connection array failed\n",
100 dtblsize, (long) sizeof(Connection), 0 );
105 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
106 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
109 * per entry initialization of the Connection array initialization
110 * will be done by connection_init()
117 * Destroy connection management infrastructure.
119 int connections_destroy(void)
123 /* should check return of every call */
125 if( connections == NULL) {
127 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
128 "connections_destroy: nothing to destroy.\n"));
130 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
136 for ( i = 0; i < dtblsize; i++ ) {
137 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
138 ber_sockbuf_free( connections[i].c_sb );
139 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
140 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
141 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
148 ldap_pvt_thread_mutex_destroy( &connections_mutex );
153 * shutdown all connections
155 int connections_shutdown(void)
159 ldap_pvt_thread_mutex_lock( &connections_mutex );
161 for ( i = 0; i < dtblsize; i++ ) {
162 if( connections[i].c_struct_state != SLAP_C_USED ) {
166 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
168 /* connections_mutex and c_mutex are locked */
169 connection_closing( &connections[i] );
170 connection_close( &connections[i] );
172 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
175 ldap_pvt_thread_mutex_unlock( &connections_mutex );
181 * Timeout idle connections.
183 int connections_timeout_idle(time_t now)
189 for( c = connection_first( &connindex );
191 c = connection_next( c, &connindex ) )
193 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
195 connection_closing( c );
196 connection_close( c );
200 connection_done( c );
205 static Connection* connection_get( ber_socket_t s )
207 /* connections_mutex should be locked by caller */
212 LDAP_LOG(( "connection", LDAP_LEVEL_ENTRY,
213 "connection_get: socket %ld\n", (long)s ));
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 ber_sockbuf_ctrl( connections[i].c_sb,
238 LBER_SB_OPT_GET_FD, &sd );
240 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
241 assert( connections[i].c_conn_state == SLAP_C_INVALID );
242 assert( connections[i].c_sb == 0 );
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", LDAP_LEVEL_ARGS,
278 "connection_get: connection %d not used\n", s ));
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", LDAP_LEVEL_RESULTS,
291 "connection_get: get for %d got connid %ld\n",s, c->c_connid ));
293 Debug( LDAP_DEBUG_TRACE,
294 "connection_get(%d): got connid=%ld\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", LDAP_LEVEL_INFO,
347 "connection_init: init of socket %ld invalid.\n", (long)s ));
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", LDAP_LEVEL_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 = NULL;
420 c->c_listener_url = NULL;
421 c->c_peer_domain = NULL;
422 c->c_peer_name = NULL;
423 c->c_sock_name = NULL;
426 c->c_pending_ops = NULL;
428 c->c_sasl_bind_mech = NULL;
429 c->c_sasl_context = NULL;
430 c->c_sasl_extra = NULL;
432 c->c_sb = ber_sockbuf_alloc( );
435 ber_len_t max = sockbuf_max_incoming;
436 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
439 c->c_currentber = NULL;
441 /* should check status of thread calls */
442 ldap_pvt_thread_mutex_init( &c->c_mutex );
443 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
444 ldap_pvt_thread_cond_init( &c->c_write_cv );
446 c->c_struct_state = SLAP_C_UNUSED;
449 ldap_pvt_thread_mutex_lock( &c->c_mutex );
451 assert( c->c_struct_state == SLAP_C_UNUSED );
452 assert( c->c_authmech == NULL );
453 assert( c->c_dn == NULL );
454 assert( c->c_cdn == NULL );
455 assert( c->c_groups == NULL );
456 assert( c->c_listener_url == NULL );
457 assert( c->c_peer_domain == NULL );
458 assert( c->c_peer_name == NULL );
459 assert( c->c_sock_name == NULL );
460 assert( c->c_ops == NULL );
461 assert( c->c_pending_ops == NULL );
462 assert( c->c_sasl_bind_mech == NULL );
463 assert( c->c_sasl_context == NULL );
464 assert( c->c_sasl_extra == NULL );
465 assert( c->c_currentber == NULL );
467 c->c_listener_url = ch_strdup( url );
468 c->c_peer_domain = ch_strdup( dnsname );
469 c->c_peer_name = ch_strdup( peername );
470 c->c_sock_name = ch_strdup( sockname );
472 c->c_n_ops_received = 0;
473 c->c_n_ops_executing = 0;
474 c->c_n_ops_pending = 0;
475 c->c_n_ops_completed = 0;
481 /* set to zero until bind, implies LDAP_VERSION3 */
485 c->c_activitytime = c->c_starttime = slap_get_time();
487 if( global_idletimeout > 0 ) {
488 c->c_activitytime = c->c_starttime = slap_get_time();
492 #ifdef LDAP_CONNECTIONLESS
494 if (tls_udp_option == 2)
498 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
499 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
501 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
502 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
507 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
508 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
510 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
511 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
513 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
514 LBER_SBIOD_LEVEL_PROVIDER, NULL );
517 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
518 INT_MAX, (void*)"ldap_" );
521 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
522 c /* non-NULL */ ) < 0 )
525 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
526 "connection_init: conn %d set nonblocking failed\n",
529 Debug( LDAP_DEBUG_ANY,
530 "connection_init(%d, %s): set nonblocking failed\n",
531 s, c->c_peer_name, 0 );
535 id = c->c_connid = conn_nextid++;
537 c->c_conn_state = SLAP_C_INACTIVE;
538 c->c_struct_state = SLAP_C_USED;
540 c->c_ssf = c->c_transport_ssf = ssf;
544 if ( tls_udp_option == 1 ) {
546 c->c_needs_tls_accept = 1;
549 c->c_needs_tls_accept = 0;
554 slap_sasl_external( c, ssf, authid );
556 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
557 ldap_pvt_thread_mutex_unlock( &connections_mutex );
559 backend_connection_init(c);
564 void connection2anonymous( Connection *c )
566 assert( connections != NULL );
570 ber_len_t max = sockbuf_max_incoming;
571 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
574 if(c->c_authmech != NULL ) {
576 c->c_authmech = NULL;
579 if(c->c_dn != NULL) {
584 if(c->c_cdn != NULL) {
589 c->c_authc_backend = NULL;
590 c->c_authz_backend = NULL;
593 GroupAssertion *g, *n;
594 for (g = c->c_groups; g; g=n)
605 connection_destroy( Connection *c )
607 /* note: connections_mutex should be locked by caller */
610 assert( connections != NULL );
612 assert( c->c_struct_state != SLAP_C_UNUSED );
613 assert( c->c_conn_state != SLAP_C_INVALID );
614 assert( c->c_ops == NULL );
616 backend_connection_destroy(c);
621 c->c_activitytime = c->c_starttime = 0;
623 connection2anonymous( c );
625 if(c->c_listener_url != NULL) {
626 free(c->c_listener_url);
627 c->c_listener_url = NULL;
630 if(c->c_peer_domain != NULL) {
631 free(c->c_peer_domain);
632 c->c_peer_domain = NULL;
634 if(c->c_peer_name != NULL) {
637 * If peer was a domain socket, unlink. Mind you,
638 * they may be un-named. Should we leave this to
641 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
642 char *path = c->c_peer_name + 5;
647 #endif /* LDAP_PF_LOCAL */
649 free(c->c_peer_name);
650 c->c_peer_name = NULL;
652 if(c->c_sock_name != NULL) {
653 free(c->c_sock_name);
654 c->c_sock_name = NULL;
657 c->c_sasl_bind_in_progress = 0;
658 if(c->c_sasl_bind_mech != NULL) {
659 free(c->c_sasl_bind_mech);
660 c->c_sasl_bind_mech = NULL;
663 slap_sasl_close( c );
665 if ( c->c_currentber != NULL ) {
666 ber_free( c->c_currentber, 1 );
667 c->c_currentber = NULL;
670 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
671 if ( sd != AC_SOCKET_INVALID ) {
672 slapd_remove( sd, 0 );
674 Statslog( LDAP_DEBUG_STATS,
675 "conn=%ld fd=%d closed\n",
676 c->c_connid, sd, 0, 0, 0 );
679 ber_sockbuf_free( c->c_sb );
681 c->c_sb = ber_sockbuf_alloc( );
684 ber_len_t max = sockbuf_max_incoming;
685 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
688 c->c_conn_state = SLAP_C_INVALID;
689 c->c_struct_state = SLAP_C_UNUSED;
692 int connection_state_closing( Connection *c )
694 /* c_mutex must be locked by caller */
698 assert( c->c_struct_state == SLAP_C_USED );
700 state = c->c_conn_state;
702 assert( state != SLAP_C_INVALID );
704 return state == SLAP_C_CLOSING;
707 static void connection_abandon( Connection *c )
709 /* c_mutex must be locked by caller */
713 for( o = c->c_ops; o != NULL; o = o->o_next ) {
714 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
716 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
719 /* remove pending operations */
720 for( o = slap_op_pop( &c->c_pending_ops );
722 o = slap_op_pop( &c->c_pending_ops ) )
728 void connection_closing( Connection *c )
730 assert( connections != NULL );
732 assert( c->c_struct_state == SLAP_C_USED );
733 assert( c->c_conn_state != SLAP_C_INVALID );
735 /* c_mutex must be locked by caller */
737 if( c->c_conn_state != SLAP_C_CLOSING ) {
740 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
742 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
743 "connection_closing: conn %d readying socket %d for close.\n",
746 Debug( LDAP_DEBUG_TRACE,
747 "connection_closing: readying conn=%ld sd=%d for close\n",
748 c->c_connid, sd, 0 );
750 /* update state to closing */
751 c->c_conn_state = SLAP_C_CLOSING;
753 /* don't listen on this port anymore */
754 slapd_clr_read( sd, 1 );
756 /* abandon active operations */
757 connection_abandon( c );
759 /* wake write blocked operations */
760 slapd_clr_write( sd, 1 );
761 ldap_pvt_thread_cond_signal( &c->c_write_cv );
765 static void connection_close( Connection *c )
769 assert( connections != NULL );
771 assert( c->c_struct_state == SLAP_C_USED );
772 assert( c->c_conn_state == SLAP_C_CLOSING );
774 /* note: connections_mutex and c_mutex should be locked by caller */
776 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
777 if( c->c_ops != NULL ) {
779 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
780 "connection_close: conn %d deferring sd %d\n",
783 Debug( LDAP_DEBUG_TRACE,
784 "connection_close: deferring conn=%ld sd=%d\n",
785 c->c_connid, sd, 0 );
791 LDAP_LOG(( "connection", LDAP_LEVEL_RESULTS,
792 "connection_close: conn %d sd %d\n",
795 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
796 c->c_connid, sd, 0 );
798 connection_destroy( c );
801 unsigned long connections_nextid(void)
804 assert( connections != NULL );
806 ldap_pvt_thread_mutex_lock( &connections_mutex );
810 ldap_pvt_thread_mutex_unlock( &connections_mutex );
815 Connection* connection_first( ber_socket_t *index )
817 assert( connections != NULL );
818 assert( index != NULL );
820 ldap_pvt_thread_mutex_lock( &connections_mutex );
824 return connection_next(NULL, index);
827 Connection* connection_next( Connection *c, ber_socket_t *index )
829 assert( connections != NULL );
830 assert( index != NULL );
831 assert( *index <= dtblsize );
834 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
839 for(; *index < dtblsize; (*index)++) {
840 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
841 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
849 if( connections[*index].c_struct_state == SLAP_C_USED ) {
850 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
851 c = &connections[(*index)++];
855 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
856 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
860 ldap_pvt_thread_mutex_lock( &c->c_mutex );
866 void connection_done( Connection *c )
868 assert( connections != NULL );
871 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
874 ldap_pvt_thread_mutex_unlock( &connections_mutex );
878 * connection_activity - handle the request operation op on connection
879 * conn. This routine figures out what kind of operation it is and
880 * calls the appropriate stub to handle it.
884 connection_operation( void *arg_v )
887 struct co_arg *arg = arg_v;
888 ber_tag_t tag = arg->co_op->o_tag;
889 Connection *conn = arg->co_conn;
891 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
893 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
895 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
897 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
898 "connection_operation: conn %d SASL bind in progress (tag=%ld).\n",
899 conn->c_connid, (long)tag ));
901 Debug( LDAP_DEBUG_ANY, "connection_operation: "
902 "error: SASL bind in progress (tag=%ld).\n",
905 send_ldap_result( conn, arg->co_op,
906 rc = LDAP_OPERATIONS_ERROR,
907 NULL, "SASL bind in progress", NULL, NULL );
908 goto operations_error;
913 rc = do_bind( conn, arg->co_op );
916 case LDAP_REQ_UNBIND:
917 rc = do_unbind( conn, arg->co_op );
921 rc = do_add( conn, arg->co_op );
924 case LDAP_REQ_DELETE:
925 rc = do_delete( conn, arg->co_op );
928 case LDAP_REQ_MODRDN:
929 rc = do_modrdn( conn, arg->co_op );
932 case LDAP_REQ_MODIFY:
933 rc = do_modify( conn, arg->co_op );
936 case LDAP_REQ_COMPARE:
937 rc = do_compare( conn, arg->co_op );
940 case LDAP_REQ_SEARCH:
941 rc = do_search( conn, arg->co_op );
944 case LDAP_REQ_ABANDON:
945 rc = do_abandon( conn, arg->co_op );
948 case LDAP_REQ_EXTENDED:
949 rc = do_extended( conn, arg->co_op );
954 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
955 "connection_operation: conn %d unknown LDAP request 0x%lx\n",
956 conn->c_connid, tag ));
958 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
961 arg->co_op->o_tag = LBER_ERROR;
962 send_ldap_disconnect( conn, arg->co_op,
963 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
968 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
971 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
973 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
975 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
977 conn->c_n_ops_executing--;
978 conn->c_n_ops_completed++;
980 slap_op_remove( &conn->c_ops, arg->co_op );
981 slap_op_free( arg->co_op );
984 free( (char *) arg );
989 case LDAP_REQ_UNBIND:
990 /* c_mutex is locked */
991 connection_closing( conn );
995 conn->c_sasl_bind_in_progress =
996 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
998 if( conn->c_conn_state == SLAP_C_BINDING) {
999 conn->c_conn_state = SLAP_C_ACTIVE;
1003 connection_resched( conn );
1005 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1010 int connection_read(ber_socket_t s)
1014 assert( connections != NULL );
1016 ldap_pvt_thread_mutex_lock( &connections_mutex );
1018 /* get (locked) connection */
1019 c = connection_get( s );
1023 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1024 "connection_read: sock %ld no connection\n",
1027 Debug( LDAP_DEBUG_ANY,
1028 "connection_read(%ld): no connection!\n",
1033 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1039 if( c->c_conn_state == SLAP_C_CLOSING ) {
1041 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1042 "connection_read: conn %d connection closing, ignoring input\n",
1045 Debug( LDAP_DEBUG_TRACE,
1046 "connection_read(%d): closing, ignoring input for id=%ld\n",
1047 s, c->c_connid, 0 );
1049 connection_return( c );
1050 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1055 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1056 "connection_read: conn %d checking for input.\n", c->c_connid ));
1058 Debug( LDAP_DEBUG_TRACE,
1059 "connection_read(%d): checking for input on id=%ld\n",
1060 s, c->c_connid, 0 );
1064 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1065 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1067 #if 0 /* required by next #if 0 */
1073 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1074 "connection_read: conn %d TLS accept error, error %d\n",
1077 Debug( LDAP_DEBUG_TRACE,
1078 "connection_read(%d): TLS accept error "
1079 "error=%d id=%ld, closing\n",
1080 s, rc, c->c_connid );
1082 c->c_needs_tls_accept = 0;
1083 /* connections_mutex and c_mutex are locked */
1084 connection_closing( c );
1087 /* Drain input before close, to allow SSL error codes
1088 * to propagate to client. */
1095 rc = select(s+1, &rfd, NULL, NULL, &tv);
1097 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
1101 connection_close( c );
1103 } else if ( rc == 0 ) {
1107 c->c_needs_tls_accept = 0;
1109 /* we need to let SASL know */
1110 ssl = (void *)ldap_pvt_tls_sb_ctx( c->c_sb );
1112 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1113 if( c->c_tls_ssf > c->c_ssf ) {
1114 c->c_ssf = c->c_tls_ssf;
1117 authid = (char *)ldap_pvt_tls_get_peer( ssl );
1118 slap_sasl_external( c, c->c_tls_ssf, authid );
1120 connection_return( c );
1121 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1126 #ifdef HAVE_CYRUS_SASL
1127 if ( c->c_sasl_layers ) {
1128 c->c_sasl_layers = 0;
1130 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
1132 if( rc != LDAP_SUCCESS ) {
1134 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1135 "connection_read: conn %d SASL install error %d, closing\n",
1138 Debug( LDAP_DEBUG_TRACE,
1139 "connection_read(%d): SASL install error "
1140 "error=%d id=%ld, closing\n",
1141 s, rc, c->c_connid );
1143 /* connections_mutex and c_mutex are locked */
1144 connection_closing( c );
1145 connection_close( c );
1146 connection_return( c );
1147 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1153 #define CONNECTION_INPUT_LOOP 1
1155 #ifdef DATA_READY_LOOP
1156 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
1157 #elif CONNECTION_INPUT_LOOP
1161 /* How do we do this without getting into a busy loop ? */
1162 rc = connection_input( c );
1167 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1168 "connection_read: conn %d input error %d, closing.\n",
1171 Debug( LDAP_DEBUG_TRACE,
1172 "connection_read(%d): input error=%d id=%ld, closing.\n",
1173 s, rc, c->c_connid );
1175 /* connections_mutex and c_mutex are locked */
1176 connection_closing( c );
1177 connection_close( c );
1178 connection_return( c );
1179 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1183 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1184 slapd_set_read( s, 1 );
1187 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1188 slapd_set_write( s, 1 );
1191 connection_return( c );
1192 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1206 #ifdef LDAP_CONNECTIONLESS
1211 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1214 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1215 "connection_input: conn %d ber_alloc failed.\n", conn->c_connid ));
1217 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1224 #ifdef LDAP_CONNECTIONLESS
1227 char peername[sizeof("IP=255.255.255.255:65336")];
1228 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1229 sizeof(struct sockaddr));
1230 if (len != sizeof(struct sockaddr))
1232 sprintf( peername, "IP=%s:%d",
1233 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1234 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1235 Statslog( LDAP_DEBUG_STATS,
1236 "conn=%ld UDP request from %s (%s) accepted.\n",
1237 conn->c_connid, peername,
1238 conn->c_sock_name, 0, 0 );
1241 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1242 if ( tag != LDAP_TAG_MESSAGE ) {
1246 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1249 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1250 "connection_input: conn %d ber_get_next failed, errno %d (%s).\n",
1251 conn->c_connid, err, sock_errstr(err) ));
1253 Debug( LDAP_DEBUG_TRACE,
1254 "ber_get_next on fd %d failed errno=%d (%s)\n",
1255 sd, err, sock_errstr(err) );
1257 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1258 /* log, close and send error */
1259 ber_free( conn->c_currentber, 1 );
1260 conn->c_currentber = NULL;
1267 ber = conn->c_currentber;
1268 conn->c_currentber = NULL;
1270 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1271 /* log, close and send error */
1273 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1274 "connection_input: conn %d ber_get_int returns 0x%lx.\n",
1275 conn->c_connid, tag ));
1277 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1284 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1285 /* log, close and send error */
1287 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1288 "connection_input: conn %d ber_peek_tag returns 0x%lx.\n",
1289 conn->c_connid, tag ));
1291 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1299 #ifdef LDAP_CONNECTIONLESS
1300 if (conn->c_is_udp) {
1301 if (tag == LBER_OCTETSTRING) {
1302 ber_get_stringa( ber, &cdn );
1303 tag = ber_peek_tag(ber, &len);
1305 if (tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH) {
1307 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1308 "connection_input: conn %d invalid req for UDP 0x%lx.\n",
1309 conn->c_connid, tag ));
1311 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1319 if(tag == LDAP_REQ_BIND) {
1320 /* immediately abandon all exiting operations upon BIND */
1321 connection_abandon( conn );
1324 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1326 #ifdef LDAP_CONNECTIONLESS
1327 op->o_peeraddr = peeraddr;
1330 op->o_protocol = LDAP_VERSION2;
1333 if ( conn->c_conn_state == SLAP_C_BINDING
1334 || conn->c_conn_state == SLAP_C_CLOSING )
1337 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1338 "connection_input: conn %d deferring operation\n",
1341 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1343 conn->c_n_ops_pending++;
1344 slap_op_add( &conn->c_pending_ops, op );
1347 conn->c_n_ops_executing++;
1348 connection_op_activate( conn, op );
1352 if ( conn->c_struct_state != SLAP_C_USED ) {
1353 /* connection must have got closed underneath us */
1357 assert( conn->c_struct_state == SLAP_C_USED );
1363 connection_resched( Connection *conn )
1367 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1370 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1372 /* us trylock to avoid possible deadlock */
1373 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1377 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1378 "connection_resched: conn %d reaquiring locks.\n",
1381 Debug( LDAP_DEBUG_TRACE,
1382 "connection_resched: reaquiring locks conn=%ld sd=%d\n",
1383 conn->c_connid, sd, 0 );
1386 * reaquire locks in the right order...
1387 * this may allow another thread to close this connection,
1388 * so recheck state below.
1390 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1391 ldap_pvt_thread_mutex_lock( &connections_mutex );
1392 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1395 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1397 LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
1398 "connection_resched: conn %d closed by other thread.\n",
1401 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1402 "closed by other thread conn=%ld sd=%d\n",
1403 conn->c_connid, sd, 0 );
1407 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1408 "connection_resched: conn %d attempting closing.\n",
1411 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1412 "attempting closing conn=%ld sd=%d\n",
1413 conn->c_connid, sd, 0 );
1415 connection_close( conn );
1418 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1422 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1423 /* other states need different handling */
1427 for( op = slap_op_pop( &conn->c_pending_ops );
1429 op = slap_op_pop( &conn->c_pending_ops ) )
1431 /* pending operations should not be marked for abandonment */
1432 assert(!op->o_abandon);
1434 conn->c_n_ops_pending--;
1435 conn->c_n_ops_executing++;
1437 connection_op_activate( conn, op );
1439 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1446 static int connection_op_activate( Connection *conn, Operation *op )
1450 ber_tag_t tag = op->o_tag;
1452 if(tag == LDAP_REQ_BIND) {
1453 conn->c_conn_state = SLAP_C_BINDING;
1456 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1457 arg->co_conn = conn;
1460 if (!arg->co_op->o_dn) {
1461 arg->co_op->o_authz = conn->c_authz;
1462 arg->co_op->o_dn = ch_strdup( conn->c_dn != NULL ? conn->c_dn : "" );
1464 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1465 (void) dn_normalize( arg->co_op->o_ndn );
1466 arg->co_op->o_authtype = conn->c_authtype;
1467 arg->co_op->o_authmech = conn->c_authmech != NULL
1468 ? ch_strdup( conn->c_authmech ) : NULL;
1470 if (!arg->co_op->o_protocol) {
1471 arg->co_op->o_protocol = conn->c_protocol
1472 ? conn->c_protocol : LDAP_VERSION3;
1474 arg->co_op->o_connid = conn->c_connid;
1476 slap_op_add( &conn->c_ops, arg->co_op );
1478 status = ldap_pvt_thread_pool_submit( &connection_pool,
1479 connection_operation, (void *) arg );
1481 if ( status != 0 ) {
1483 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1484 "connection_op_activate: conn %d thread pool submit failed.\n",
1487 Debug( LDAP_DEBUG_ANY,
1488 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1490 /* should move op to pending list */
1496 int connection_write(ber_socket_t s)
1499 assert( connections != NULL );
1501 ldap_pvt_thread_mutex_lock( &connections_mutex );
1503 c = connection_get( s );
1505 slapd_clr_write( s, 0);
1509 LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
1510 "connection_write: sock %ld no connection!\n",(long)s));
1512 Debug( LDAP_DEBUG_ANY,
1513 "connection_write(%ld): no connection!\n",
1517 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1524 LDAP_LOG(( "connection", LDAP_LEVEL_DETAIL1,
1525 "connection_write conn %d waking output.\n",
1528 Debug( LDAP_DEBUG_TRACE,
1529 "connection_write(%d): waking output for id=%ld\n",
1530 s, c->c_connid, 0 );
1532 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1534 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1535 slapd_set_read( s, 1 );
1536 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1537 slapd_set_write( s, 1 );
1538 connection_return( c );
1539 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1545 * Create client side and server side connection structures, connected to
1546 * one another, for the front end to use for searches on arbitrary back ends.
1549 int connection_internal_open( Connection **conn, LDAP **ldp, const char *id )
1552 ber_socket_t fd[2] = {-1,-1};
1558 rc = lutil_pair( fd );
1563 rc = connection_init( fd[1], "INT", "localhost",
1564 "localhost:0", "localhost:00", 0, 256, id );
1570 slapd_add_internal( fd[1] );
1572 /* A search operation, number 0 */
1573 op = slap_op_alloc( NULL, 0, LDAP_REQ_SEARCH, 0);
1574 op->o_ndn = ch_strdup( id );
1575 op->o_protocol = LDAP_VERSION3;
1577 (*conn) = connection_get( fd[1] );
1578 (*conn)->c_ops = op;
1579 (*conn)->c_conn_state = SLAP_C_ACTIVE;
1582 /* Create the client side of the connection */
1583 rc = ldap_open_internal_connection( ldp, &(fd[0]) );
1584 if( rc != LDAP_SUCCESS ) {
1589 /* The connection_get() will have locked the connection's mutex */
1590 ldap_pvt_thread_mutex_unlock( &((*conn)->c_mutex) );
1592 return LDAP_SUCCESS;
1596 void connection_internal_close( Connection *conn )
1598 Operation *op = conn->c_ops;
1600 slap_op_remove( &conn->c_ops, op );
1602 connection_closing( conn );
1603 connection_close( conn );