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_reference = slap_send_search_reference;
412 c->c_send_ldap_extended = slap_send_ldap_extended;
413 #ifdef LDAP_RES_INTERMEDIATE_RESP
414 c->c_send_ldap_intermediate_resp = slap_send_ldap_intermediate_resp;
417 c->c_authmech.bv_val = NULL;
418 c->c_authmech.bv_len = 0;
419 c->c_dn.bv_val = NULL;
421 c->c_ndn.bv_val = NULL;
425 c->c_listener = NULL;
426 c->c_peer_domain.bv_val = NULL;
427 c->c_peer_domain.bv_len = 0;
428 c->c_peer_name.bv_val = NULL;
429 c->c_peer_name.bv_len = 0;
431 LDAP_STAILQ_INIT(&c->c_ops);
432 LDAP_STAILQ_INIT(&c->c_pending_ops);
434 c->c_sasl_bind_mech.bv_val = NULL;
435 c->c_sasl_bind_mech.bv_len = 0;
436 c->c_sasl_context = NULL;
437 c->c_sasl_extra = NULL;
438 c->c_sasl_bindop = NULL;
440 c->c_sb = ber_sockbuf_alloc( );
443 ber_len_t max = sockbuf_max_incoming;
444 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
447 c->c_currentber = NULL;
449 /* should check status of thread calls */
450 ldap_pvt_thread_mutex_init( &c->c_mutex );
451 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
452 ldap_pvt_thread_cond_init( &c->c_write_cv );
454 c->c_struct_state = SLAP_C_UNUSED;
457 ldap_pvt_thread_mutex_lock( &c->c_mutex );
459 assert( c->c_struct_state == SLAP_C_UNUSED );
460 assert( c->c_authmech.bv_val == NULL );
461 assert( c->c_dn.bv_val == NULL );
462 assert( c->c_ndn.bv_val == NULL );
463 assert( c->c_groups == NULL );
464 assert( c->c_listener == NULL );
465 assert( c->c_peer_domain.bv_val == NULL );
466 assert( c->c_peer_name.bv_val == NULL );
467 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
468 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
469 assert( c->c_sasl_bind_mech.bv_val == NULL );
470 assert( c->c_sasl_context == NULL );
471 assert( c->c_sasl_extra == NULL );
472 assert( c->c_sasl_bindop == NULL );
473 assert( c->c_currentber == NULL );
475 c->c_listener = listener;
476 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
477 ber_str2bv( peername, 0, 1, &c->c_peer_name );
479 c->c_n_ops_received = 0;
480 c->c_n_ops_executing = 0;
481 c->c_n_ops_pending = 0;
482 c->c_n_ops_completed = 0;
488 /* set to zero until bind, implies LDAP_VERSION3 */
492 c->c_activitytime = c->c_starttime = slap_get_time();
494 if( global_idletimeout > 0 ) {
495 c->c_activitytime = c->c_starttime = slap_get_time();
499 #ifdef LDAP_CONNECTIONLESS
501 if( tls_udp_option == 2 ) {
504 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
505 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
507 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
508 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
509 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
510 LBER_SBIOD_LEVEL_PROVIDER, NULL );
515 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
516 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
518 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
519 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
523 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
524 INT_MAX, (void*)"ldap_" );
527 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
528 c /* non-NULL */ ) < 0 )
531 LDAP_LOG( CONNECTION, INFO,
532 "connection_init: conn %lu set nonblocking failed\n",
535 Debug( LDAP_DEBUG_ANY,
536 "connection_init(%d, %s): set nonblocking failed\n",
537 s, c->c_peer_name.bv_val, 0 );
541 id = c->c_connid = conn_nextid++;
543 c->c_conn_state = SLAP_C_INACTIVE;
544 c->c_struct_state = SLAP_C_USED;
546 c->c_ssf = c->c_transport_ssf = ssf;
550 if ( tls_udp_option == 1 ) {
552 c->c_needs_tls_accept = 1;
555 c->c_needs_tls_accept = 0;
560 slap_sasl_external( c, ssf, authid );
562 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
563 ldap_pvt_thread_mutex_unlock( &connections_mutex );
565 backend_connection_init(c);
570 void connection2anonymous( Connection *c )
572 assert( connections != NULL );
576 ber_len_t max = sockbuf_max_incoming;
577 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
580 if(c->c_authmech.bv_val != NULL ) {
581 free(c->c_authmech.bv_val);
582 c->c_authmech.bv_val = NULL;
584 c->c_authmech.bv_len = 0;
586 if(c->c_dn.bv_val != NULL) {
587 free(c->c_dn.bv_val);
588 c->c_dn.bv_val = NULL;
591 if(c->c_ndn.bv_val != NULL) {
592 free(c->c_ndn.bv_val);
593 c->c_ndn.bv_val = NULL;
597 c->c_authz_backend = NULL;
600 GroupAssertion *g, *n;
601 for (g = c->c_groups; g; g=n) {
610 connection_destroy( Connection *c )
612 /* note: connections_mutex should be locked by caller */
614 unsigned long connid;
616 assert( connections != NULL );
618 assert( c->c_struct_state != SLAP_C_UNUSED );
619 assert( c->c_conn_state != SLAP_C_INVALID );
620 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
622 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
623 connid = c->c_connid;
625 backend_connection_destroy(c);
630 c->c_activitytime = c->c_starttime = 0;
632 connection2anonymous( c );
633 c->c_listener = NULL;
635 if(c->c_peer_domain.bv_val != NULL) {
636 free(c->c_peer_domain.bv_val);
637 c->c_peer_domain.bv_val = NULL;
639 c->c_peer_domain.bv_len = 0;
640 if(c->c_peer_name.bv_val != NULL) {
641 free(c->c_peer_name.bv_val);
642 c->c_peer_name.bv_val = NULL;
644 c->c_peer_name.bv_len = 0;
646 c->c_sasl_bind_in_progress = 0;
647 if(c->c_sasl_bind_mech.bv_val != NULL) {
648 free(c->c_sasl_bind_mech.bv_val);
649 c->c_sasl_bind_mech.bv_val = NULL;
651 c->c_sasl_bind_mech.bv_len = 0;
653 slap_sasl_close( c );
655 if ( c->c_currentber != NULL ) {
656 ber_free( c->c_currentber, 1 );
657 c->c_currentber = NULL;
660 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
661 if ( sd != AC_SOCKET_INVALID ) {
662 slapd_remove( sd, 0 );
664 Statslog( LDAP_DEBUG_STATS,
665 "conn=%lu fd=%d closed\n",
666 connid, sd, 0, 0, 0 );
669 ber_sockbuf_free( c->c_sb );
671 c->c_sb = ber_sockbuf_alloc( );
674 ber_len_t max = sockbuf_max_incoming;
675 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
678 c->c_conn_state = SLAP_C_INVALID;
679 c->c_struct_state = SLAP_C_UNUSED;
682 int connection_state_closing( Connection *c )
684 /* c_mutex must be locked by caller */
688 assert( c->c_struct_state == SLAP_C_USED );
690 state = c->c_conn_state;
692 assert( state != SLAP_C_INVALID );
694 return state == SLAP_C_CLOSING;
697 static void connection_abandon( Connection *c )
699 /* c_mutex must be locked by caller */
703 LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
707 /* remove pending operations */
708 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
709 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
710 LDAP_STAILQ_NEXT(o, o_next) = NULL;
715 void connection_closing( Connection *c )
717 assert( connections != NULL );
719 assert( c->c_struct_state == SLAP_C_USED );
720 assert( c->c_conn_state != SLAP_C_INVALID );
722 /* c_mutex must be locked by caller */
724 if( c->c_conn_state != SLAP_C_CLOSING ) {
727 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
729 LDAP_LOG( CONNECTION, DETAIL1,
730 "connection_closing: conn %lu readying socket %d for close.\n",
731 c->c_connid, sd, 0 );
733 Debug( LDAP_DEBUG_TRACE,
734 "connection_closing: readying conn=%lu sd=%d for close\n",
735 c->c_connid, sd, 0 );
737 /* update state to closing */
738 c->c_conn_state = SLAP_C_CLOSING;
740 /* don't listen on this port anymore */
741 slapd_clr_read( sd, 1 );
743 /* abandon active operations */
744 connection_abandon( c );
746 /* wake write blocked operations */
747 slapd_clr_write( sd, 1 );
748 ldap_pvt_thread_cond_signal( &c->c_write_cv );
752 static void connection_close( Connection *c )
756 assert( connections != NULL );
758 assert( c->c_struct_state == SLAP_C_USED );
759 assert( c->c_conn_state == SLAP_C_CLOSING );
761 /* note: connections_mutex and c_mutex should be locked by caller */
763 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
764 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
766 LDAP_LOG( CONNECTION, DETAIL1,
767 "connection_close: conn %lu deferring sd %d\n",
768 c->c_connid, sd, 0 );
770 Debug( LDAP_DEBUG_TRACE,
771 "connection_close: deferring conn=%lu sd=%d\n",
772 c->c_connid, sd, 0 );
778 LDAP_LOG( CONNECTION, RESULTS,
779 "connection_close: conn %lu sd %d\n", c->c_connid, sd, 0 );
781 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
782 c->c_connid, sd, 0 );
784 connection_destroy( c );
787 unsigned long connections_nextid(void)
790 assert( connections != NULL );
792 ldap_pvt_thread_mutex_lock( &connections_mutex );
796 ldap_pvt_thread_mutex_unlock( &connections_mutex );
801 Connection* connection_first( ber_socket_t *index )
803 assert( connections != NULL );
804 assert( index != NULL );
806 ldap_pvt_thread_mutex_lock( &connections_mutex );
810 return connection_next(NULL, index);
813 Connection* connection_next( Connection *c, ber_socket_t *index )
815 assert( connections != NULL );
816 assert( index != NULL );
817 assert( *index <= dtblsize );
820 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
825 for(; *index < dtblsize; (*index)++) {
826 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
827 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
835 if( connections[*index].c_struct_state == SLAP_C_USED ) {
836 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
837 c = &connections[(*index)++];
841 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
842 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
846 ldap_pvt_thread_mutex_lock( &c->c_mutex );
852 void connection_done( Connection *c )
854 assert( connections != NULL );
857 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
860 ldap_pvt_thread_mutex_unlock( &connections_mutex );
864 * connection_activity - handle the request operation op on connection
865 * conn. This routine figures out what kind of operation it is and
866 * calls the appropriate stub to handle it.
870 #define INCR_OP(var,index) \
872 ldap_pvt_thread_mutex_lock( &num_ops_mutex ); \
874 ldap_pvt_thread_mutex_unlock( &num_ops_mutex ); \
876 #else /* !SLAPD_MONITOR */
877 #define INCR_OP(var,index)
878 #endif /* !SLAPD_MONITOR */
881 connection_operation( void *ctx, void *arg_v )
883 int rc = SLAPD_DISCONNECT;
884 Operation *op = arg_v;
885 SlapReply rs = {REP_RESULT};
886 ber_tag_t tag = op->o_tag;
888 ber_tag_t oldtag = tag;
889 #endif /* SLAPD_MONITOR */
890 Connection *conn = op->o_conn;
894 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
896 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
898 op->o_threadctx = ctx;
900 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
902 LDAP_LOG( CONNECTION, ERR,
903 "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
904 conn->c_connid, (long)tag, 0 );
906 Debug( LDAP_DEBUG_ANY, "connection_operation: "
907 "error: SASL bind in progress (tag=%ld).\n",
910 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
911 "SASL bind in progress" );
912 goto operations_error;
915 /* We can use Thread-Local storage for most mallocs. We can
916 * also use TL for ber parsing, but not on Add or Modify.
918 #define SLAB_SIZE 1048576
920 memsiz = ber_len( op->o_ber ) * 64;
921 if ( SLAB_SIZE > memsiz ) memsiz = SLAB_SIZE;
925 memctx = sl_mem_create( memsiz, ctx );
926 op->o_tmpmemctx = memctx;
927 op->o_tmpmfuncs = &sl_mfuncs;
928 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
929 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, memctx );
934 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
935 rc = do_bind( op, &rs );
938 case LDAP_REQ_UNBIND:
939 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
940 rc = do_unbind( op, &rs );
944 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
945 rc = do_add( op, &rs );
948 case LDAP_REQ_DELETE:
949 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
950 rc = do_delete( op, &rs );
953 case LDAP_REQ_MODRDN:
954 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
955 rc = do_modrdn( op, &rs );
958 case LDAP_REQ_MODIFY:
959 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
960 rc = do_modify( op, &rs );
963 case LDAP_REQ_COMPARE:
964 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
965 rc = do_compare( op, &rs );
968 case LDAP_REQ_SEARCH:
969 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
970 rc = do_search( op, &rs );
973 case LDAP_REQ_ABANDON:
974 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
975 rc = do_abandon( op, &rs );
978 case LDAP_REQ_EXTENDED:
979 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
980 rc = do_extended( op, &rs );
985 LDAP_LOG( CONNECTION, INFO,
986 "connection_operation: conn %lu unknown LDAP request 0x%lx\n",
987 conn->c_connid, tag, 0 );
989 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
992 op->o_tag = LBER_ERROR;
993 rs.sr_err = LDAP_PROTOCOL_ERROR;
994 rs.sr_text = "unknown LDAP request";
995 send_ldap_disconnect( op, &rs );
1000 #ifdef SLAPD_MONITOR
1002 #endif /* SLAPD_MONITOR */
1003 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
1006 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
1007 num_ops_completed++;
1008 #ifdef SLAPD_MONITOR
1011 num_ops_completed_[SLAP_OP_BIND]++;
1013 case LDAP_REQ_UNBIND:
1014 num_ops_completed_[SLAP_OP_UNBIND]++;
1017 num_ops_completed_[SLAP_OP_ADD]++;
1019 case LDAP_REQ_DELETE:
1020 num_ops_completed_[SLAP_OP_DELETE]++;
1022 case LDAP_REQ_MODRDN:
1023 num_ops_completed_[SLAP_OP_MODRDN]++;
1025 case LDAP_REQ_MODIFY:
1026 num_ops_completed_[SLAP_OP_MODIFY]++;
1028 case LDAP_REQ_COMPARE:
1029 num_ops_completed_[SLAP_OP_COMPARE]++;
1031 case LDAP_REQ_SEARCH:
1032 num_ops_completed_[SLAP_OP_SEARCH]++;
1034 case LDAP_REQ_ABANDON:
1035 num_ops_completed_[SLAP_OP_ABANDON]++;
1037 case LDAP_REQ_EXTENDED:
1038 num_ops_completed_[SLAP_OP_EXTENDED]++;
1041 #endif /* SLAPD_MONITOR */
1042 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1044 #ifdef LDAP_EXOP_X_CANCEL
1045 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1046 op->o_cancel = LDAP_TOO_LATE;
1049 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1050 op->o_cancel != SLAP_CANCEL_DONE )
1052 ldap_pvt_thread_yield();
1056 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1058 conn->c_n_ops_executing--;
1059 conn->c_n_ops_completed++;
1061 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1062 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1064 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
1065 if ( op->o_cancel == SLAP_CANCEL_ACK )
1068 #ifdef LDAP_CLIENT_UPDATE
1069 if ( ( op->o_clientupdate_type & SLAP_LCUP_PERSIST ) )
1073 if ( ( op->o_sync_mode & SLAP_SYNC_PERSIST ) )
1079 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, NULL );
1086 case LDAP_REQ_UNBIND:
1087 /* c_mutex is locked */
1088 connection_closing( conn );
1092 conn->c_sasl_bind_in_progress =
1093 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1095 if( conn->c_conn_state == SLAP_C_BINDING) {
1096 conn->c_conn_state = SLAP_C_ACTIVE;
1100 connection_resched( conn );
1102 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1107 int connection_read(ber_socket_t s)
1112 assert( connections != NULL );
1114 ldap_pvt_thread_mutex_lock( &connections_mutex );
1116 /* get (locked) connection */
1117 c = connection_get( s );
1121 LDAP_LOG( CONNECTION, INFO,
1122 "connection_read: sock %ld no connection\n", (long)s, 0, 0 );
1124 Debug( LDAP_DEBUG_ANY,
1125 "connection_read(%ld): no connection!\n",
1130 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1136 if( c->c_conn_state == SLAP_C_CLOSING ) {
1138 LDAP_LOG( CONNECTION, INFO,
1139 "connection_read: conn %lu connection closing, ignoring input\n",
1140 c->c_connid, 0, 0 );
1142 Debug( LDAP_DEBUG_TRACE,
1143 "connection_read(%d): closing, ignoring input for id=%lu\n",
1144 s, c->c_connid, 0 );
1146 connection_return( c );
1147 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1152 LDAP_LOG( CONNECTION, DETAIL1,
1153 "connection_read: conn %lu checking for input.\n",
1154 c->c_connid, 0, 0 );
1156 Debug( LDAP_DEBUG_TRACE,
1157 "connection_read(%d): checking for input on id=%lu\n",
1158 s, c->c_connid, 0 );
1162 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1163 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1165 #if 0 /* required by next #if 0 */
1171 LDAP_LOG( CONNECTION, ERR,
1172 "connection_read: conn %lu TLS accept error, error %d\n",
1173 c->c_connid, rc, 0 );
1175 Debug( LDAP_DEBUG_TRACE,
1176 "connection_read(%d): TLS accept error "
1177 "error=%d id=%lu, closing\n",
1178 s, rc, c->c_connid );
1180 c->c_needs_tls_accept = 0;
1181 /* connections_mutex and c_mutex are locked */
1182 connection_closing( c );
1185 /* Drain input before close, to allow SSL error codes
1186 * to propagate to client. */
1192 rc = select(s+1, &rfd, NULL, NULL, &tv);
1194 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1198 connection_close( c );
1200 } else if ( rc == 0 ) {
1202 struct berval authid = { 0, NULL };
1204 c->c_needs_tls_accept = 0;
1206 /* we need to let SASL know */
1207 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1209 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1210 if( c->c_tls_ssf > c->c_ssf ) {
1211 c->c_ssf = c->c_tls_ssf;
1214 rc = dnX509peerNormalize( ssl, &authid );
1215 if ( rc != LDAP_SUCCESS ) {
1217 LDAP_LOG( CONNECTION, INFO,
1218 "connection_read: conn %lu unable to get TLS client DN, "
1219 "error %d\n", c->c_connid, rc, 0 );
1221 Debug( LDAP_DEBUG_TRACE,
1222 "connection_read(%d): unable to get TLS client DN "
1223 "error=%d id=%lu\n",
1224 s, rc, c->c_connid );
1227 slap_sasl_external( c, c->c_tls_ssf, authid.bv_val );
1228 if ( authid.bv_val ) free( authid.bv_val );
1231 /* if success and data is ready, fall thru to data input loop */
1233 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1235 connection_return( c );
1236 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1242 #ifdef HAVE_CYRUS_SASL
1243 if ( c->c_sasl_layers ) {
1244 c->c_sasl_layers = 0;
1246 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
1248 if( rc != LDAP_SUCCESS ) {
1250 LDAP_LOG( CONNECTION, ERR,
1251 "connection_read: conn %lu SASL install error %d, closing\n",
1252 c->c_connid, rc, 0 );
1254 Debug( LDAP_DEBUG_TRACE,
1255 "connection_read(%d): SASL install error "
1256 "error=%d id=%lu, closing\n",
1257 s, rc, c->c_connid );
1259 /* connections_mutex and c_mutex are locked */
1260 connection_closing( c );
1261 connection_close( c );
1262 connection_return( c );
1263 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1269 #define CONNECTION_INPUT_LOOP 1
1270 /* #define DATA_READY_LOOP 1 */
1274 /* How do we do this without getting into a busy loop ? */
1275 rc = connection_input( c );
1277 #ifdef DATA_READY_LOOP
1278 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) );
1279 #elif CONNECTION_INPUT_LOOP
1287 LDAP_LOG( CONNECTION, ERR,
1288 "connection_read: conn %lu input error %d, closing.\n",
1289 c->c_connid, rc, 0 );
1291 Debug( LDAP_DEBUG_TRACE,
1292 "connection_read(%d): input error=%d id=%lu, closing.\n",
1293 s, rc, c->c_connid );
1295 /* connections_mutex and c_mutex are locked */
1296 connection_closing( c );
1297 connection_close( c );
1298 connection_return( c );
1299 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1303 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1304 slapd_set_read( s, 1 );
1307 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1308 slapd_set_write( s, 1 );
1311 connection_return( c );
1312 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1327 #ifdef LDAP_CONNECTIONLESS
1332 if ( conn->c_currentber == NULL &&
1333 ( conn->c_currentber = ber_alloc()) == NULL )
1336 LDAP_LOG( CONNECTION, ERR,
1337 "connection_input: conn %lu ber_alloc failed.\n",
1338 conn->c_connid, 0, 0 );
1340 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1347 #ifdef LDAP_CONNECTIONLESS
1348 if ( conn->c_is_udp ) {
1349 char peername[sizeof("IP=255.255.255.255:65336")];
1350 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1351 sizeof(struct sockaddr));
1352 if (len != sizeof(struct sockaddr))
1354 sprintf( peername, "IP=%s:%d",
1355 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1356 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1357 Statslog( LDAP_DEBUG_STATS,
1358 "conn=%lu UDP request from %s (%s) accepted.\n",
1359 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1362 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1363 if ( tag != LDAP_TAG_MESSAGE ) {
1367 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1370 LDAP_LOG( CONNECTION, ERR,
1371 "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1372 conn->c_connid, err, sock_errstr(err) );
1374 Debug( LDAP_DEBUG_TRACE,
1375 "ber_get_next on fd %d failed errno=%d (%s)\n",
1376 sd, err, sock_errstr(err) );
1378 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1379 /* log, close and send error */
1380 ber_free( conn->c_currentber, 1 );
1381 conn->c_currentber = NULL;
1388 ber = conn->c_currentber;
1389 conn->c_currentber = NULL;
1391 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1392 /* log, close and send error */
1394 LDAP_LOG( CONNECTION, ERR,
1395 "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1396 conn->c_connid, tag, 0 );
1398 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1405 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1406 /* log, close and send error */
1408 LDAP_LOG( CONNECTION, ERR,
1409 "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1410 conn->c_connid, tag, 0 );
1412 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1420 #ifdef LDAP_CONNECTIONLESS
1421 if( conn->c_is_udp ) {
1422 if( tag == LBER_OCTETSTRING ) {
1423 ber_get_stringa( ber, &cdn );
1424 tag = ber_peek_tag(ber, &len);
1426 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1428 LDAP_LOG( CONNECTION, ERR,
1429 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1430 conn->c_connid, tag, 0 );
1432 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1440 if(tag == LDAP_REQ_BIND) {
1441 /* immediately abandon all exiting operations upon BIND */
1442 connection_abandon( conn );
1445 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1448 op->vrFilter = NULL;
1449 #ifdef LDAP_CONTROL_PAGEDRESULTS
1450 op->o_pagedresults_state = conn->c_pagedresults_state;
1452 #ifdef LDAP_CONNECTIONLESS
1453 if (conn->c_is_udp) {
1456 ber_str2bv( cdn, 0, 1, &op->o_dn );
1457 op->o_protocol = LDAP_VERSION2;
1459 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1460 if (op->o_res_ber == NULL)
1463 rc = ber_write(op->o_res_ber, (char *)&peeraddr, sizeof(struct sockaddr), 0);
1464 if (rc != sizeof(struct sockaddr)) {
1466 LDAP_LOG( CONNECTION, INFO,
1467 "connection_input: conn %lu ber_write failed\n",
1468 conn->c_connid, 0, 0 );
1470 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1475 if (op->o_protocol == LDAP_VERSION2) {
1476 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1479 LDAP_LOG( CONNECTION, INFO,
1480 "connection_input: conn %lu put outer sequence failed\n",
1481 conn->c_connid, 0, 0 );
1483 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1489 #endif /* LDAP_CONNECTIONLESS */
1493 /* Don't process requests when the conn is in the middle of a
1494 * Bind, or if it's closing. Also, don't let any single conn
1495 * use up all the available threads, and don't execute if we're
1496 * currently blocked on output. And don't execute if there are
1497 * already pending ops, let them go first.
1499 if ( conn->c_conn_state == SLAP_C_BINDING
1500 || conn->c_conn_state == SLAP_C_CLOSING
1501 || conn->c_n_ops_executing >= connection_pool_max/2
1502 || conn->c_n_ops_pending
1503 || conn->c_writewaiter)
1505 int max = conn->c_dn.bv_len ? slap_conn_max_pending_auth
1506 : slap_conn_max_pending;
1508 LDAP_LOG( CONNECTION, INFO,
1509 "connection_input: conn %lu deferring operation\n",
1510 conn->c_connid, 0, 0 );
1512 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1514 conn->c_n_ops_pending++;
1515 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1516 if ( conn->c_n_ops_pending > max ) {
1522 conn->c_n_ops_executing++;
1523 connection_op_activate( op );
1527 if ( conn->c_struct_state != SLAP_C_USED ) {
1528 /* connection must have got closed underneath us */
1532 assert( conn->c_struct_state == SLAP_C_USED );
1538 connection_resched( Connection *conn )
1542 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1545 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1547 /* us trylock to avoid possible deadlock */
1548 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1552 LDAP_LOG( CONNECTION, DETAIL1,
1553 "connection_resched: conn %lu reaquiring locks.\n",
1554 conn->c_connid, 0, 0 );
1556 Debug( LDAP_DEBUG_TRACE,
1557 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1558 conn->c_connid, sd, 0 );
1561 * reaquire locks in the right order...
1562 * this may allow another thread to close this connection,
1563 * so recheck state below.
1565 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1566 ldap_pvt_thread_mutex_lock( &connections_mutex );
1567 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1570 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1572 LDAP_LOG( CONNECTION, INFO,
1573 "connection_resched: conn %lu closed by other thread.\n",
1574 conn->c_connid, 0, 0 );
1576 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1577 "closed by other thread conn=%lu sd=%d\n",
1578 conn->c_connid, sd, 0 );
1582 LDAP_LOG( CONNECTION, DETAIL1,
1583 "connection_resched: conn %lu attempting closing.\n",
1584 conn->c_connid, 0, 0 );
1586 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1587 "attempting closing conn=%lu sd=%d\n",
1588 conn->c_connid, sd, 0 );
1590 connection_close( conn );
1593 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1597 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1598 /* other states need different handling */
1602 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1603 if ( conn->c_n_ops_executing > connection_pool_max/2 ) {
1606 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1607 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1608 /* pending operations should not be marked for abandonment */
1609 assert(!op->o_abandon);
1611 conn->c_n_ops_pending--;
1612 conn->c_n_ops_executing++;
1614 connection_op_activate( op );
1616 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1623 static int connection_op_activate( Operation *op )
1626 ber_tag_t tag = op->o_tag;
1628 if(tag == LDAP_REQ_BIND) {
1629 op->o_conn->c_conn_state = SLAP_C_BINDING;
1632 if (!op->o_dn.bv_len) {
1633 op->o_authz = op->o_conn->c_authz;
1634 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1635 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1637 op->o_authtype = op->o_conn->c_authtype;
1638 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1640 if (!op->o_protocol) {
1641 op->o_protocol = op->o_conn->c_protocol
1642 ? op->o_conn->c_protocol : LDAP_VERSION3;
1644 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE
1645 && op->o_protocol > LDAP_VERSION2) {
1646 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1649 op->o_connid = op->o_conn->c_connid;
1651 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1653 status = ldap_pvt_thread_pool_submit( &connection_pool,
1654 connection_operation, (void *) op );
1656 if ( status != 0 ) {
1658 LDAP_LOG( CONNECTION, ERR,
1659 "connection_op_activate: conn %lu thread pool submit failed.\n",
1660 op->o_connid, 0, 0 );
1662 Debug( LDAP_DEBUG_ANY,
1663 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1665 /* should move op to pending list */
1671 int connection_write(ber_socket_t s)
1675 assert( connections != NULL );
1677 ldap_pvt_thread_mutex_lock( &connections_mutex );
1679 c = connection_get( s );
1681 slapd_clr_write( s, 0);
1685 LDAP_LOG( CONNECTION, ERR,
1686 "connection_write: sock %ld no connection!\n", (long)s, 0, 0);
1688 Debug( LDAP_DEBUG_ANY,
1689 "connection_write(%ld): no connection!\n",
1693 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1700 LDAP_LOG( CONNECTION, DETAIL1,
1701 "connection_write conn %lu waking output.\n", c->c_connid, 0, 0 );
1703 Debug( LDAP_DEBUG_TRACE,
1704 "connection_write(%d): waking output for id=%lu\n",
1705 s, c->c_connid, 0 );
1707 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1709 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1710 slapd_set_read( s, 1 );
1711 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1712 slapd_set_write( s, 1 );
1713 connection_return( c );
1714 ldap_pvt_thread_mutex_unlock( &connections_mutex );