3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
14 #include <ac/string.h>
16 #include <ac/unistd.h>
22 /* protected by connections_mutex */
23 static ldap_pvt_thread_mutex_t connections_mutex;
24 static Connection *connections = NULL;
25 static unsigned long conn_nextid = 0;
27 /* structure state (protected by connections_mutex) */
28 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
29 #define SLAP_C_UNUSED 0x01
30 #define SLAP_C_USED 0x02
32 /* connection state (protected by c_mutex ) */
33 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
34 #define SLAP_C_INACTIVE 0x01 /* zero threads */
35 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
36 #define SLAP_C_BINDING 0x03 /* binding */
37 #define SLAP_C_CLOSING 0x04 /* closing */
40 connection_state2str( int state )
43 case SLAP_C_INVALID: return "!";
44 case SLAP_C_INACTIVE: return "|";
45 case SLAP_C_ACTIVE: return "";
46 case SLAP_C_BINDING: return "B";
47 case SLAP_C_CLOSING: return "C";
53 static Connection* connection_get( ber_socket_t s );
55 static int connection_input( Connection *c );
56 static void connection_close( Connection *c );
58 static int connection_op_activate( Operation *op );
59 static int connection_resched( Connection *conn );
60 static void connection_abandon( Connection *conn );
61 static void connection_destroy( Connection *c );
63 static ldap_pvt_thread_start_t connection_operation;
66 * Initialize connection management infrastructure.
68 int connections_init(void)
70 assert( connections == NULL );
72 if( connections != NULL) {
74 LDAP_LOG( CONNECTION, INFO,
75 "connections_init: already initialized.\n", 0, 0, 0 );
77 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
83 /* should check return of every call */
84 ldap_pvt_thread_mutex_init( &connections_mutex );
86 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
88 if( connections == NULL ) {
90 LDAP_LOG( CONNECTION, ERR,
91 "connections_init: allocation (%d * %ld) of connection "
92 "array failed\n", dtblsize, (long) sizeof(Connection), 0 );
94 Debug( LDAP_DEBUG_ANY,
95 "connections_init: allocation (%d*%ld) of connection array failed\n",
96 dtblsize, (long) sizeof(Connection), 0 );
101 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
102 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
105 * per entry initialization of the Connection array initialization
106 * will be done by connection_init()
113 * Destroy connection management infrastructure.
115 int connections_destroy(void)
119 /* should check return of every call */
121 if( connections == NULL) {
123 LDAP_LOG( CONNECTION, INFO,
124 "connections_destroy: nothing to destroy.\n", 0, 0, 0 );
126 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
132 for ( i = 0; i < dtblsize; i++ ) {
133 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
134 ber_sockbuf_free( connections[i].c_sb );
135 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
136 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
137 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
144 ldap_pvt_thread_mutex_destroy( &connections_mutex );
149 * shutdown all connections
151 int connections_shutdown(void)
155 ldap_pvt_thread_mutex_lock( &connections_mutex );
157 for ( i = 0; i < dtblsize; i++ ) {
158 if( connections[i].c_struct_state != SLAP_C_USED ) {
162 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
164 /* connections_mutex and c_mutex are locked */
165 connection_closing( &connections[i] );
166 connection_close( &connections[i] );
168 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
171 ldap_pvt_thread_mutex_unlock( &connections_mutex );
177 * Timeout idle connections.
179 int connections_timeout_idle(time_t now)
185 for( c = connection_first( &connindex );
187 c = connection_next( c, &connindex ) )
189 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
191 connection_closing( c );
192 connection_close( c );
196 connection_done( c );
201 static Connection* connection_get( ber_socket_t s )
203 /* connections_mutex should be locked by caller */
208 LDAP_LOG( CONNECTION, ENTRY, "connection_get: socket %ld\n", (long)s, 0, 0 );
210 Debug( LDAP_DEBUG_ARGS,
211 "connection_get(%ld)\n",
215 assert( connections != NULL );
217 if(s == AC_SOCKET_INVALID) {
224 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
231 for(i=0; i<dtblsize; i++) {
232 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
233 assert( connections[i].c_conn_state == SLAP_C_INVALID );
234 assert( connections[i].c_sb == 0 );
238 ber_sockbuf_ctrl( connections[i].c_sb,
239 LBER_SB_OPT_GET_FD, &sd );
241 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
242 assert( connections[i].c_conn_state == SLAP_C_INVALID );
243 assert( sd == AC_SOCKET_INVALID );
247 /* state can actually change from used -> unused by resched,
248 * so don't assert details here.
262 ldap_pvt_thread_mutex_lock( &c->c_mutex );
264 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
265 if( c->c_struct_state != SLAP_C_USED ) {
266 /* connection must have been closed due to resched */
268 assert( c->c_conn_state == SLAP_C_INVALID );
269 assert( sd == AC_SOCKET_INVALID );
272 LDAP_LOG( CONNECTION, ARGS,
273 "connection_get: connection %d not used\n", s, 0, 0 );
275 Debug( LDAP_DEBUG_TRACE,
276 "connection_get(%d): connection not used\n",
280 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
285 LDAP_LOG( CONNECTION, RESULTS,
286 "connection_get: get for %d got connid %lu\n", s, c->c_connid, 0 );
288 Debug( LDAP_DEBUG_TRACE,
289 "connection_get(%d): got connid=%lu\n",
295 assert( c->c_struct_state == SLAP_C_USED );
296 assert( c->c_conn_state != SLAP_C_INVALID );
297 assert( sd != AC_SOCKET_INVALID );
300 c->c_activitytime = slap_get_time();
302 if( global_idletimeout > 0 ) {
303 c->c_activitytime = slap_get_time();
311 static void connection_return( Connection *c )
313 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
316 long connection_init(
320 const char* peername,
328 assert( connections != NULL );
330 assert( listener != NULL );
331 assert( dnsname != NULL );
332 assert( peername != NULL );
335 assert( tls_udp_option != 1 );
338 if( s == AC_SOCKET_INVALID ) {
340 LDAP_LOG( CONNECTION, INFO,
341 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
343 Debug( LDAP_DEBUG_ANY,
344 "connection_init(%ld): invalid.\n",
352 assert( s < dtblsize );
355 ldap_pvt_thread_mutex_lock( &connections_mutex );
366 for( i=0; i < dtblsize; i++) {
369 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
370 assert( connections[i].c_sb == 0 );
375 sd = AC_SOCKET_INVALID;
376 if (connections[i].c_sb != NULL)
377 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
379 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
380 assert( sd == AC_SOCKET_INVALID );
385 assert( connections[i].c_struct_state == SLAP_C_USED );
386 assert( connections[i].c_conn_state != SLAP_C_INVALID );
387 assert( sd != AC_SOCKET_INVALID );
392 LDAP_LOG( CONNECTION, INFO,
393 "connection_init: skt %d connection table full "
394 "(%d/%d)\n", s, i, dtblsize );
396 Debug( LDAP_DEBUG_ANY,
397 "connection_init(%d): connection table full "
398 "(%d/%d)\n", s, i, dtblsize);
400 ldap_pvt_thread_mutex_unlock( &connections_mutex );
408 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
409 c->c_send_ldap_result = slap_send_ldap_result;
410 c->c_send_search_entry = slap_send_search_entry;
411 c->c_send_search_result = slap_send_search_result;
412 c->c_send_search_reference = slap_send_search_reference;
413 c->c_send_ldap_extended = slap_send_ldap_extended;
414 #ifdef LDAP_RES_INTERMEDIATE_RESP
415 c->c_send_ldap_intermediate_resp = slap_send_ldap_intermediate_resp;
418 c->c_authmech.bv_val = NULL;
419 c->c_authmech.bv_len = 0;
420 c->c_dn.bv_val = NULL;
422 c->c_ndn.bv_val = NULL;
426 c->c_listener = NULL;
427 c->c_peer_domain.bv_val = NULL;
428 c->c_peer_domain.bv_len = 0;
429 c->c_peer_name.bv_val = NULL;
430 c->c_peer_name.bv_len = 0;
432 LDAP_STAILQ_INIT(&c->c_ops);
433 LDAP_STAILQ_INIT(&c->c_pending_ops);
435 c->c_sasl_bind_mech.bv_val = NULL;
436 c->c_sasl_bind_mech.bv_len = 0;
437 c->c_sasl_context = NULL;
438 c->c_sasl_extra = NULL;
439 c->c_sasl_bindop = NULL;
441 c->c_sb = ber_sockbuf_alloc( );
444 ber_len_t max = sockbuf_max_incoming;
445 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
448 c->c_currentber = NULL;
450 /* should check status of thread calls */
451 ldap_pvt_thread_mutex_init( &c->c_mutex );
452 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
453 ldap_pvt_thread_cond_init( &c->c_write_cv );
455 c->c_struct_state = SLAP_C_UNUSED;
458 ldap_pvt_thread_mutex_lock( &c->c_mutex );
460 assert( c->c_struct_state == SLAP_C_UNUSED );
461 assert( c->c_authmech.bv_val == NULL );
462 assert( c->c_dn.bv_val == NULL );
463 assert( c->c_ndn.bv_val == NULL );
464 assert( c->c_groups == NULL );
465 assert( c->c_listener == NULL );
466 assert( c->c_peer_domain.bv_val == NULL );
467 assert( c->c_peer_name.bv_val == NULL );
468 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
469 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
470 assert( c->c_sasl_bind_mech.bv_val == NULL );
471 assert( c->c_sasl_context == NULL );
472 assert( c->c_sasl_extra == NULL );
473 assert( c->c_sasl_bindop == NULL );
474 assert( c->c_currentber == NULL );
476 c->c_listener = listener;
477 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
478 ber_str2bv( peername, 0, 1, &c->c_peer_name );
480 c->c_n_ops_received = 0;
481 c->c_n_ops_executing = 0;
482 c->c_n_ops_pending = 0;
483 c->c_n_ops_completed = 0;
489 /* set to zero until bind, implies LDAP_VERSION3 */
493 c->c_activitytime = c->c_starttime = slap_get_time();
495 if( global_idletimeout > 0 ) {
496 c->c_activitytime = c->c_starttime = slap_get_time();
500 #ifdef LDAP_CONNECTIONLESS
502 if( tls_udp_option == 2 ) {
505 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
506 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
508 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
509 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
510 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
511 LBER_SBIOD_LEVEL_PROVIDER, NULL );
516 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
517 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
519 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
520 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
524 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
525 INT_MAX, (void*)"ldap_" );
528 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
529 c /* non-NULL */ ) < 0 )
532 LDAP_LOG( CONNECTION, INFO,
533 "connection_init: conn %lu set nonblocking failed\n",
536 Debug( LDAP_DEBUG_ANY,
537 "connection_init(%d, %s): set nonblocking failed\n",
538 s, c->c_peer_name.bv_val, 0 );
542 id = c->c_connid = conn_nextid++;
544 c->c_conn_state = SLAP_C_INACTIVE;
545 c->c_struct_state = SLAP_C_USED;
547 c->c_ssf = c->c_transport_ssf = ssf;
551 if ( tls_udp_option == 1 ) {
553 c->c_needs_tls_accept = 1;
556 c->c_needs_tls_accept = 0;
561 slap_sasl_external( c, ssf, authid );
563 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
564 ldap_pvt_thread_mutex_unlock( &connections_mutex );
566 backend_connection_init(c);
571 void connection2anonymous( Connection *c )
573 assert( connections != NULL );
577 ber_len_t max = sockbuf_max_incoming;
578 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
581 if(c->c_authmech.bv_val != NULL ) {
582 free(c->c_authmech.bv_val);
583 c->c_authmech.bv_val = NULL;
585 c->c_authmech.bv_len = 0;
587 if(c->c_dn.bv_val != NULL) {
588 free(c->c_dn.bv_val);
589 c->c_dn.bv_val = NULL;
592 if(c->c_ndn.bv_val != NULL) {
593 free(c->c_ndn.bv_val);
594 c->c_ndn.bv_val = NULL;
598 c->c_authz_backend = NULL;
601 GroupAssertion *g, *n;
602 for (g = c->c_groups; g; g=n) {
611 connection_destroy( Connection *c )
613 /* note: connections_mutex should be locked by caller */
615 unsigned long connid;
617 assert( connections != NULL );
619 assert( c->c_struct_state != SLAP_C_UNUSED );
620 assert( c->c_conn_state != SLAP_C_INVALID );
621 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
623 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
624 connid = c->c_connid;
626 backend_connection_destroy(c);
631 c->c_activitytime = c->c_starttime = 0;
633 connection2anonymous( c );
634 c->c_listener = NULL;
636 if(c->c_peer_domain.bv_val != NULL) {
637 free(c->c_peer_domain.bv_val);
638 c->c_peer_domain.bv_val = NULL;
640 c->c_peer_domain.bv_len = 0;
641 if(c->c_peer_name.bv_val != NULL) {
642 free(c->c_peer_name.bv_val);
643 c->c_peer_name.bv_val = NULL;
645 c->c_peer_name.bv_len = 0;
647 c->c_sasl_bind_in_progress = 0;
648 if(c->c_sasl_bind_mech.bv_val != NULL) {
649 free(c->c_sasl_bind_mech.bv_val);
650 c->c_sasl_bind_mech.bv_val = NULL;
652 c->c_sasl_bind_mech.bv_len = 0;
654 slap_sasl_close( c );
656 if ( c->c_currentber != NULL ) {
657 ber_free( c->c_currentber, 1 );
658 c->c_currentber = NULL;
661 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
662 if ( sd != AC_SOCKET_INVALID ) {
663 slapd_remove( sd, 0 );
665 Statslog( LDAP_DEBUG_STATS,
666 "conn=%lu fd=%d closed\n",
667 connid, sd, 0, 0, 0 );
670 ber_sockbuf_free( c->c_sb );
672 c->c_sb = ber_sockbuf_alloc( );
675 ber_len_t max = sockbuf_max_incoming;
676 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
679 c->c_conn_state = SLAP_C_INVALID;
680 c->c_struct_state = SLAP_C_UNUSED;
683 int connection_state_closing( Connection *c )
685 /* c_mutex must be locked by caller */
689 assert( c->c_struct_state == SLAP_C_USED );
691 state = c->c_conn_state;
693 assert( state != SLAP_C_INVALID );
695 return state == SLAP_C_CLOSING;
698 static void connection_abandon( Connection *c )
700 /* c_mutex must be locked by caller */
704 LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
708 /* remove pending operations */
709 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
710 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
711 LDAP_STAILQ_NEXT(o, o_next) = NULL;
716 void connection_closing( Connection *c )
718 assert( connections != NULL );
720 assert( c->c_struct_state == SLAP_C_USED );
721 assert( c->c_conn_state != SLAP_C_INVALID );
723 /* c_mutex must be locked by caller */
725 if( c->c_conn_state != SLAP_C_CLOSING ) {
728 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
730 LDAP_LOG( CONNECTION, DETAIL1,
731 "connection_closing: conn %lu readying socket %d for close.\n",
732 c->c_connid, sd, 0 );
734 Debug( LDAP_DEBUG_TRACE,
735 "connection_closing: readying conn=%lu sd=%d for close\n",
736 c->c_connid, sd, 0 );
738 /* update state to closing */
739 c->c_conn_state = SLAP_C_CLOSING;
741 /* don't listen on this port anymore */
742 slapd_clr_read( sd, 1 );
744 /* abandon active operations */
745 connection_abandon( c );
747 /* wake write blocked operations */
748 slapd_clr_write( sd, 1 );
749 ldap_pvt_thread_cond_signal( &c->c_write_cv );
753 static void connection_close( Connection *c )
757 assert( connections != NULL );
759 assert( c->c_struct_state == SLAP_C_USED );
760 assert( c->c_conn_state == SLAP_C_CLOSING );
762 /* note: connections_mutex and c_mutex should be locked by caller */
764 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
765 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
767 LDAP_LOG( CONNECTION, DETAIL1,
768 "connection_close: conn %lu deferring sd %d\n",
769 c->c_connid, sd, 0 );
771 Debug( LDAP_DEBUG_TRACE,
772 "connection_close: deferring conn=%lu sd=%d\n",
773 c->c_connid, sd, 0 );
779 LDAP_LOG( CONNECTION, RESULTS,
780 "connection_close: conn %lu sd %d\n", c->c_connid, sd, 0 );
782 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
783 c->c_connid, sd, 0 );
785 connection_destroy( c );
788 unsigned long connections_nextid(void)
791 assert( connections != NULL );
793 ldap_pvt_thread_mutex_lock( &connections_mutex );
797 ldap_pvt_thread_mutex_unlock( &connections_mutex );
802 Connection* connection_first( ber_socket_t *index )
804 assert( connections != NULL );
805 assert( index != NULL );
807 ldap_pvt_thread_mutex_lock( &connections_mutex );
811 return connection_next(NULL, index);
814 Connection* connection_next( Connection *c, ber_socket_t *index )
816 assert( connections != NULL );
817 assert( index != NULL );
818 assert( *index <= dtblsize );
821 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
826 for(; *index < dtblsize; (*index)++) {
827 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
828 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
836 if( connections[*index].c_struct_state == SLAP_C_USED ) {
837 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
838 c = &connections[(*index)++];
842 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
843 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
847 ldap_pvt_thread_mutex_lock( &c->c_mutex );
853 void connection_done( Connection *c )
855 assert( connections != NULL );
858 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
861 ldap_pvt_thread_mutex_unlock( &connections_mutex );
865 * connection_activity - handle the request operation op on connection
866 * conn. This routine figures out what kind of operation it is and
867 * calls the appropriate stub to handle it.
871 #define INCR_OP(var,index) \
873 ldap_pvt_thread_mutex_lock( &num_ops_mutex ); \
875 ldap_pvt_thread_mutex_unlock( &num_ops_mutex ); \
877 #else /* !SLAPD_MONITOR */
878 #define INCR_OP(var,index)
879 #endif /* !SLAPD_MONITOR */
882 connection_operation( void *ctx, void *arg_v )
885 Operation *op = arg_v;
886 ber_tag_t tag = op->o_tag;
888 ber_tag_t oldtag = tag;
889 #endif /* SLAPD_MONITOR */
890 Connection *conn = op->o_conn;
892 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
894 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
896 op->o_threadctx = ctx;
898 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
900 LDAP_LOG( CONNECTION, ERR,
901 "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
902 conn->c_connid, (long)tag, 0 );
904 Debug( LDAP_DEBUG_ANY, "connection_operation: "
905 "error: SASL bind in progress (tag=%ld).\n",
908 send_ldap_result( conn, op,
909 rc = LDAP_OPERATIONS_ERROR,
910 NULL, "SASL bind in progress", NULL, NULL );
911 goto operations_error;
916 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
917 rc = do_bind( conn, op );
920 case LDAP_REQ_UNBIND:
921 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
922 rc = do_unbind( conn, op );
926 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
927 rc = do_add( conn, op );
930 case LDAP_REQ_DELETE:
931 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
932 rc = do_delete( conn, op );
935 case LDAP_REQ_MODRDN:
936 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
937 rc = do_modrdn( conn, op );
940 case LDAP_REQ_MODIFY:
941 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
942 rc = do_modify( conn, op );
945 case LDAP_REQ_COMPARE:
946 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
947 rc = do_compare( conn, op );
950 case LDAP_REQ_SEARCH:
951 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
952 rc = do_search( conn, op );
955 case LDAP_REQ_ABANDON:
956 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
957 rc = do_abandon( conn, op );
960 case LDAP_REQ_EXTENDED:
961 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
962 rc = do_extended( conn, op );
967 LDAP_LOG( CONNECTION, INFO,
968 "connection_operation: conn %lu unknown LDAP request 0x%lx\n",
969 conn->c_connid, tag, 0 );
971 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
974 op->o_tag = LBER_ERROR;
975 send_ldap_disconnect( conn, op,
976 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
983 #endif /* SLAPD_MONITOR */
984 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
987 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
992 num_ops_completed_[SLAP_OP_BIND]++;
994 case LDAP_REQ_UNBIND:
995 num_ops_completed_[SLAP_OP_UNBIND]++;
998 num_ops_completed_[SLAP_OP_ADD]++;
1000 case LDAP_REQ_DELETE:
1001 num_ops_completed_[SLAP_OP_DELETE]++;
1003 case LDAP_REQ_MODRDN:
1004 num_ops_completed_[SLAP_OP_MODRDN]++;
1006 case LDAP_REQ_MODIFY:
1007 num_ops_completed_[SLAP_OP_MODIFY]++;
1009 case LDAP_REQ_COMPARE:
1010 num_ops_completed_[SLAP_OP_COMPARE]++;
1012 case LDAP_REQ_SEARCH:
1013 num_ops_completed_[SLAP_OP_SEARCH]++;
1015 case LDAP_REQ_ABANDON:
1016 num_ops_completed_[SLAP_OP_ABANDON]++;
1018 case LDAP_REQ_EXTENDED:
1019 num_ops_completed_[SLAP_OP_EXTENDED]++;
1022 #endif /* SLAPD_MONITOR */
1023 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1025 #ifdef LDAP_EXOP_X_CANCEL
1026 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1027 op->o_cancel = LDAP_TOO_LATE;
1030 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1031 op->o_cancel != SLAP_CANCEL_DONE )
1033 ldap_pvt_thread_yield();
1037 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1039 conn->c_n_ops_executing--;
1040 conn->c_n_ops_completed++;
1042 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1043 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1045 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
1046 if ( op->o_cancel == SLAP_CANCEL_ACK )
1049 #ifdef LDAP_CLIENT_UPDATE
1050 if ( ( op->o_clientupdate_type & SLAP_LCUP_PERSIST ) )
1054 if ( ( op->o_sync_mode & SLAP_SYNC_PERSIST ) )
1066 case LDAP_REQ_UNBIND:
1067 /* c_mutex is locked */
1068 connection_closing( conn );
1072 conn->c_sasl_bind_in_progress =
1073 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1075 if( conn->c_conn_state == SLAP_C_BINDING) {
1076 conn->c_conn_state = SLAP_C_ACTIVE;
1080 connection_resched( conn );
1082 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1087 int connection_read(ber_socket_t s)
1092 assert( connections != NULL );
1094 ldap_pvt_thread_mutex_lock( &connections_mutex );
1096 /* get (locked) connection */
1097 c = connection_get( s );
1101 LDAP_LOG( CONNECTION, INFO,
1102 "connection_read: sock %ld no connection\n", (long)s, 0, 0 );
1104 Debug( LDAP_DEBUG_ANY,
1105 "connection_read(%ld): no connection!\n",
1110 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1116 if( c->c_conn_state == SLAP_C_CLOSING ) {
1118 LDAP_LOG( CONNECTION, INFO,
1119 "connection_read: conn %lu connection closing, ignoring input\n",
1120 c->c_connid, 0, 0 );
1122 Debug( LDAP_DEBUG_TRACE,
1123 "connection_read(%d): closing, ignoring input for id=%lu\n",
1124 s, c->c_connid, 0 );
1126 connection_return( c );
1127 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1132 LDAP_LOG( CONNECTION, DETAIL1,
1133 "connection_read: conn %lu checking for input.\n",
1134 c->c_connid, 0, 0 );
1136 Debug( LDAP_DEBUG_TRACE,
1137 "connection_read(%d): checking for input on id=%lu\n",
1138 s, c->c_connid, 0 );
1142 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1143 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1145 #if 0 /* required by next #if 0 */
1151 LDAP_LOG( CONNECTION, ERR,
1152 "connection_read: conn %lu TLS accept error, error %d\n",
1153 c->c_connid, rc, 0 );
1155 Debug( LDAP_DEBUG_TRACE,
1156 "connection_read(%d): TLS accept error "
1157 "error=%d id=%lu, closing\n",
1158 s, rc, c->c_connid );
1160 c->c_needs_tls_accept = 0;
1161 /* connections_mutex and c_mutex are locked */
1162 connection_closing( c );
1165 /* Drain input before close, to allow SSL error codes
1166 * to propagate to client. */
1172 rc = select(s+1, &rfd, NULL, NULL, &tv);
1174 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1178 connection_close( c );
1180 } else if ( rc == 0 ) {
1182 struct berval authid = { 0, NULL };
1184 c->c_needs_tls_accept = 0;
1186 /* we need to let SASL know */
1187 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1189 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1190 if( c->c_tls_ssf > c->c_ssf ) {
1191 c->c_ssf = c->c_tls_ssf;
1194 rc = dnX509peerNormalize( ssl, &authid );
1195 if ( rc != LDAP_SUCCESS ) {
1197 LDAP_LOG( CONNECTION, INFO,
1198 "connection_read: conn %lu unable to get TLS client DN, "
1199 "error %d\n", c->c_connid, rc, 0 );
1201 Debug( LDAP_DEBUG_TRACE,
1202 "connection_read(%d): unable to get TLS client DN "
1203 "error=%d id=%lu\n",
1204 s, rc, c->c_connid );
1207 slap_sasl_external( c, c->c_tls_ssf, authid.bv_val );
1208 if ( authid.bv_val ) free( authid.bv_val );
1211 /* if success and data is ready, fall thru to data input loop */
1213 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1215 connection_return( c );
1216 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1222 #ifdef HAVE_CYRUS_SASL
1223 if ( c->c_sasl_layers ) {
1224 c->c_sasl_layers = 0;
1226 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
1228 if( rc != LDAP_SUCCESS ) {
1230 LDAP_LOG( CONNECTION, ERR,
1231 "connection_read: conn %lu SASL install error %d, closing\n",
1232 c->c_connid, rc, 0 );
1234 Debug( LDAP_DEBUG_TRACE,
1235 "connection_read(%d): SASL install error "
1236 "error=%d id=%lu, closing\n",
1237 s, rc, c->c_connid );
1239 /* connections_mutex and c_mutex are locked */
1240 connection_closing( c );
1241 connection_close( c );
1242 connection_return( c );
1243 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1249 #define CONNECTION_INPUT_LOOP 1
1250 /* #define DATA_READY_LOOP 1 */
1254 /* How do we do this without getting into a busy loop ? */
1255 rc = connection_input( c );
1257 #ifdef DATA_READY_LOOP
1258 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) );
1259 #elif CONNECTION_INPUT_LOOP
1267 LDAP_LOG( CONNECTION, ERR,
1268 "connection_read: conn %lu input error %d, closing.\n",
1269 c->c_connid, rc, 0 );
1271 Debug( LDAP_DEBUG_TRACE,
1272 "connection_read(%d): input error=%d id=%lu, closing.\n",
1273 s, rc, c->c_connid );
1275 /* connections_mutex and c_mutex are locked */
1276 connection_closing( c );
1277 connection_close( c );
1278 connection_return( c );
1279 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1283 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1284 slapd_set_read( s, 1 );
1287 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1288 slapd_set_write( s, 1 );
1291 connection_return( c );
1292 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1307 #ifdef LDAP_CONNECTIONLESS
1312 if ( conn->c_currentber == NULL &&
1313 ( conn->c_currentber = ber_alloc()) == NULL )
1316 LDAP_LOG( CONNECTION, ERR,
1317 "connection_input: conn %lu ber_alloc failed.\n",
1318 conn->c_connid, 0, 0 );
1320 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1327 #ifdef LDAP_CONNECTIONLESS
1328 if ( conn->c_is_udp ) {
1329 char peername[sizeof("IP=255.255.255.255:65336")];
1330 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1331 sizeof(struct sockaddr));
1332 if (len != sizeof(struct sockaddr))
1334 sprintf( peername, "IP=%s:%d",
1335 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1336 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1337 Statslog( LDAP_DEBUG_STATS,
1338 "conn=%lu UDP request from %s (%s) accepted.\n",
1339 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1342 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1343 if ( tag != LDAP_TAG_MESSAGE ) {
1347 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1350 LDAP_LOG( CONNECTION, ERR,
1351 "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1352 conn->c_connid, err, sock_errstr(err) );
1354 Debug( LDAP_DEBUG_TRACE,
1355 "ber_get_next on fd %d failed errno=%d (%s)\n",
1356 sd, err, sock_errstr(err) );
1358 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1359 /* log, close and send error */
1360 ber_free( conn->c_currentber, 1 );
1361 conn->c_currentber = NULL;
1368 ber = conn->c_currentber;
1369 conn->c_currentber = NULL;
1371 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1372 /* log, close and send error */
1374 LDAP_LOG( CONNECTION, ERR,
1375 "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1376 conn->c_connid, tag, 0 );
1378 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1385 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1386 /* log, close and send error */
1388 LDAP_LOG( CONNECTION, ERR,
1389 "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1390 conn->c_connid, tag, 0 );
1392 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1400 #ifdef LDAP_CONNECTIONLESS
1401 if( conn->c_is_udp ) {
1402 if( tag == LBER_OCTETSTRING ) {
1403 ber_get_stringa( ber, &cdn );
1404 tag = ber_peek_tag(ber, &len);
1406 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1408 LDAP_LOG( CONNECTION, ERR,
1409 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1410 conn->c_connid, tag, 0 );
1412 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1420 if(tag == LDAP_REQ_BIND) {
1421 /* immediately abandon all exiting operations upon BIND */
1422 connection_abandon( conn );
1425 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1428 op->vrFilter = NULL;
1429 #ifdef LDAP_CONTROL_PAGEDRESULTS
1430 op->o_pagedresults_state = conn->c_pagedresults_state;
1432 #ifdef LDAP_CONNECTIONLESS
1433 if (conn->c_is_udp) {
1436 ber_str2bv( cdn, 0, 1, &op->o_dn );
1437 op->o_protocol = LDAP_VERSION2;
1439 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1440 if (op->o_res_ber == NULL)
1443 rc = ber_write(op->o_res_ber, (char *)&peeraddr, sizeof(struct sockaddr), 0);
1444 if (rc != sizeof(struct sockaddr)) {
1446 LDAP_LOG( CONNECTION, INFO,
1447 "connection_input: conn %lu ber_write failed\n",
1448 conn->c_connid, 0, 0 );
1450 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1455 if (op->o_protocol == LDAP_VERSION2) {
1456 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1459 LDAP_LOG( CONNECTION, INFO,
1460 "connection_input: conn %lu put outer sequence failed\n",
1461 conn->c_connid, 0, 0 );
1463 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1469 #endif /* LDAP_CONNECTIONLESS */
1473 /* Don't process requests when the conn is in the middle of a
1474 * Bind, or if it's closing. Also, don't let any single conn
1475 * use up all the available threads, and don't execute if we're
1476 * currently blocked on output. And don't execute if there are
1477 * already pending ops, let them go first.
1479 if ( conn->c_conn_state == SLAP_C_BINDING
1480 || conn->c_conn_state == SLAP_C_CLOSING
1481 || conn->c_n_ops_executing >= connection_pool_max/2
1482 || conn->c_n_ops_pending
1483 || conn->c_writewaiter)
1485 int max = conn->c_dn.bv_len ? slap_conn_max_pending_auth
1486 : slap_conn_max_pending;
1488 LDAP_LOG( CONNECTION, INFO,
1489 "connection_input: conn %lu deferring operation\n",
1490 conn->c_connid, 0, 0 );
1492 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1494 conn->c_n_ops_pending++;
1495 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1496 if ( conn->c_n_ops_pending > max ) {
1502 conn->c_n_ops_executing++;
1503 connection_op_activate( op );
1507 if ( conn->c_struct_state != SLAP_C_USED ) {
1508 /* connection must have got closed underneath us */
1512 assert( conn->c_struct_state == SLAP_C_USED );
1518 connection_resched( Connection *conn )
1522 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1525 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1527 /* us trylock to avoid possible deadlock */
1528 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1532 LDAP_LOG( CONNECTION, DETAIL1,
1533 "connection_resched: conn %lu reaquiring locks.\n",
1534 conn->c_connid, 0, 0 );
1536 Debug( LDAP_DEBUG_TRACE,
1537 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1538 conn->c_connid, sd, 0 );
1541 * reaquire locks in the right order...
1542 * this may allow another thread to close this connection,
1543 * so recheck state below.
1545 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1546 ldap_pvt_thread_mutex_lock( &connections_mutex );
1547 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1550 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1552 LDAP_LOG( CONNECTION, INFO,
1553 "connection_resched: conn %lu closed by other thread.\n",
1554 conn->c_connid, 0, 0 );
1556 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1557 "closed by other thread conn=%lu sd=%d\n",
1558 conn->c_connid, sd, 0 );
1562 LDAP_LOG( CONNECTION, DETAIL1,
1563 "connection_resched: conn %lu attempting closing.\n",
1564 conn->c_connid, 0, 0 );
1566 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1567 "attempting closing conn=%lu sd=%d\n",
1568 conn->c_connid, sd, 0 );
1570 connection_close( conn );
1573 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1577 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1578 /* other states need different handling */
1582 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1583 if ( conn->c_n_ops_executing > connection_pool_max/2 ) {
1586 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1587 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1588 /* pending operations should not be marked for abandonment */
1589 assert(!op->o_abandon);
1591 conn->c_n_ops_pending--;
1592 conn->c_n_ops_executing++;
1594 connection_op_activate( op );
1596 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1603 static int connection_op_activate( Operation *op )
1606 ber_tag_t tag = op->o_tag;
1608 if(tag == LDAP_REQ_BIND) {
1609 op->o_conn->c_conn_state = SLAP_C_BINDING;
1612 if (!op->o_dn.bv_len) {
1613 op->o_authz = op->o_conn->c_authz;
1614 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1615 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1617 op->o_authtype = op->o_conn->c_authtype;
1618 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1620 if (!op->o_protocol) {
1621 op->o_protocol = op->o_conn->c_protocol
1622 ? op->o_conn->c_protocol : LDAP_VERSION3;
1624 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE
1625 && op->o_protocol > LDAP_VERSION2) {
1626 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1629 op->o_connid = op->o_conn->c_connid;
1631 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1633 status = ldap_pvt_thread_pool_submit( &connection_pool,
1634 connection_operation, (void *) op );
1636 if ( status != 0 ) {
1638 LDAP_LOG( CONNECTION, ERR,
1639 "connection_op_activate: conn %lu thread pool submit failed.\n",
1640 op->o_connid, 0, 0 );
1642 Debug( LDAP_DEBUG_ANY,
1643 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1645 /* should move op to pending list */
1651 int connection_write(ber_socket_t s)
1655 assert( connections != NULL );
1657 ldap_pvt_thread_mutex_lock( &connections_mutex );
1659 c = connection_get( s );
1661 slapd_clr_write( s, 0);
1665 LDAP_LOG( CONNECTION, ERR,
1666 "connection_write: sock %ld no connection!\n", (long)s, 0, 0);
1668 Debug( LDAP_DEBUG_ANY,
1669 "connection_write(%ld): no connection!\n",
1673 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1680 LDAP_LOG( CONNECTION, DETAIL1,
1681 "connection_write conn %lu waking output.\n", c->c_connid, 0, 0 );
1683 Debug( LDAP_DEBUG_TRACE,
1684 "connection_write(%d): waking output for id=%lu\n",
1685 s, c->c_connid, 0 );
1687 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1689 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1690 slapd_set_read( s, 1 );
1691 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1692 slapd_set_write( s, 1 );
1693 connection_return( c );
1694 ldap_pvt_thread_mutex_unlock( &connections_mutex );