2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
33 #include <ac/socket.h>
35 #include <ac/string.h>
37 #include <ac/unistd.h>
43 #include "slapi/slapi.h"
46 #ifdef SLAP_MULTI_CONN_ARRAY
47 /* for Multiple Connection Arrary (MCA) Support */
48 static ldap_pvt_thread_mutex_t* connections_mutex;
49 static Connection **connections = NULL;
51 /* set to the number of processors (round up to a power of 2) */
52 # define NUM_CONNECTION_ARRAY 4
54 /* partition the array in a modulo manner */
55 # define MCA_conn_array_id(fd) ((int)(fd)%NUM_CONNECTION_ARRAY)
56 # define MCA_conn_array_element_id(fd) ((int)(fd)/NUM_CONNECTION_ARRAY)
57 # define MCA_ARRAY_SIZE ((int)(MCA_conn_array_element_id(dtblsize) + (MCA_conn_array_id(dtblsize) ? 1 : 0)))
58 # define MCA_conn_check(fd) (dtblsize > 0 && (fd) >= 0 && (fd) < (MCA_ARRAY_SIZE*NUM_CONNECTION_ARRAY))
59 # define MCA_GET_CONNECTION(fd) (&(connections[MCA_conn_array_id(fd)]) \
60 [MCA_conn_array_element_id(fd)])
61 # define MCA_GET_CONN_MUTEX(fd) (&connections_mutex[MCA_conn_array_id(fd)])
64 /* protected by connections_mutex */
65 static ldap_pvt_thread_mutex_t connections_mutex;
66 static Connection *connections = NULL;
68 # define MCA_conn_check(fd) (dtblsize > 0 && (fd) < dtblsize)
69 # define MCA_GET_CONNECTION(fd) (&connections[s])
70 # define MCA_GET_CONN_MUTEX(fd) (&connections_mutex)
73 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
74 static unsigned long conn_nextid = 0;
76 static const char conn_lost_str[] = "connection lost";
78 /* structure state (protected by connections_mutex) */
79 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
80 #define SLAP_C_UNUSED 0x01
81 #define SLAP_C_USED 0x02
83 /* connection state (protected by c_mutex ) */
84 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
85 #define SLAP_C_INACTIVE 0x01 /* zero threads */
86 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
87 #define SLAP_C_BINDING 0x03 /* binding */
88 #define SLAP_C_CLOSING 0x04 /* closing */
89 #define SLAP_C_CLIENT 0x05 /* outbound client conn */
92 connection_state2str( int state )
95 case SLAP_C_INVALID: return "!";
96 case SLAP_C_INACTIVE: return "|";
97 case SLAP_C_ACTIVE: return "";
98 case SLAP_C_BINDING: return "B";
99 case SLAP_C_CLOSING: return "C";
100 case SLAP_C_CLIENT: return "L";
106 static Connection* connection_get( ber_socket_t s );
108 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
110 typedef struct conn_readinfo {
112 ldap_pvt_thread_start_t *func;
117 static int connection_input( Connection *c, conn_readinfo *cri );
119 static int connection_input( Connection *c );
121 static void connection_close( Connection *c );
123 static int connection_op_activate( Operation *op );
124 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
125 static void connection_op_queue( Operation *op );
127 static int connection_resched( Connection *conn );
128 static void connection_abandon( Connection *conn );
129 static void connection_destroy( Connection *c );
131 static ldap_pvt_thread_start_t connection_operation;
134 * Initialize connection management infrastructure.
136 int connections_init(void)
137 #ifdef SLAP_MULTI_CONN_ARRAY
142 assert( connections == NULL );
144 if( connections != NULL) {
145 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
150 connections_mutex = (ldap_pvt_thread_mutex_t*) ch_calloc(
151 NUM_CONNECTION_ARRAY, sizeof(ldap_pvt_thread_mutex_t) );
152 if( connections_mutex == NULL ) {
153 Debug( LDAP_DEBUG_ANY, "connections_init: "
154 "allocation of connection mutexes failed\n", 0, 0, 0 );
158 connections = (Connection**) ch_calloc(
159 NUM_CONNECTION_ARRAY, sizeof(Connection*));
160 if( connections == NULL ) {
161 Debug( LDAP_DEBUG_ANY, "connections_init: "
162 "allocation of connection[%d] failed\n", 0, 0, 0 );
166 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
167 ldap_pvt_thread_mutex_init( connections_mutex+i );
168 connections[i] = (Connection*) ch_calloc(
169 MCA_ARRAY_SIZE, sizeof(Connection) );
170 if( connections[i] == NULL ) {
171 Debug( LDAP_DEBUG_ANY, "connections_init: "
172 "allocation (%d*%ld) of connection array[%d] failed\n",
173 dtblsize, (long) sizeof(Connection), i );
178 /* should check return of every call */
179 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
181 assert( connections[0]->c_struct_state == SLAP_C_UNINITIALIZED );
182 assert( connections[NUM_CONNECTION_ARRAY-1]->c_struct_state ==
183 SLAP_C_UNINITIALIZED );
185 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
186 conn = connections[i];
187 for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) {
188 conn[j].c_conn_idx = j;
193 * per entry initialization of the Connection array initialization
194 * will be done by connection_init()
203 assert( connections == NULL );
205 if( connections != NULL) {
206 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
211 /* should check return of every call */
212 ldap_pvt_thread_mutex_init( &connections_mutex );
213 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
215 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
217 if( connections == NULL ) {
218 Debug( LDAP_DEBUG_ANY, "connections_init: "
219 "allocation (%d*%ld) of connection array failed\n",
220 dtblsize, (long) sizeof(Connection), 0 );
224 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
225 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
227 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
230 * per entry initialization of the Connection array initialization
231 * will be done by connection_init()
239 * Destroy connection management infrastructure.
242 int connections_destroy(void)
243 #ifdef SLAP_MULTI_CONN_ARRAY
248 if( connections == NULL) {
249 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
254 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
255 Connection* conn = connections[i];
256 for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) {
257 if( conn[j].c_struct_state != SLAP_C_UNINITIALIZED ) {
258 ber_sockbuf_free( conn[j].c_sb );
259 ldap_pvt_thread_mutex_destroy( &conn[j].c_mutex );
260 ldap_pvt_thread_mutex_destroy( &conn[j].c_write_mutex );
261 ldap_pvt_thread_cond_destroy( &conn[j].c_write_cv );
264 if ( slapi_plugins_used ) {
265 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
273 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
274 free( connections[i] );
275 connections[i] = NULL;
276 ldap_pvt_thread_mutex_destroy( &connections_mutex[i] );
280 free( connections_mutex );
282 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
291 /* should check return of every call */
293 if( connections == NULL) {
294 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
299 for ( i = 0; i < dtblsize; i++ ) {
300 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
301 ber_sockbuf_free( connections[i].c_sb );
302 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
303 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
304 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
306 if ( slapi_plugins_used ) {
307 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
317 ldap_pvt_thread_mutex_destroy( &connections_mutex );
318 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
324 * shutdown all connections
326 int connections_shutdown(void)
327 #ifdef SLAP_MULTI_CONN_ARRAY
332 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
333 Connection* conn = connections[i];
334 ldap_pvt_thread_mutex_lock( &connections_mutex[i] );
335 for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) {
336 if( conn[j].c_struct_state != SLAP_C_USED ) {
339 /* give persistent clients a chance to cleanup */
340 if( conn[j].c_conn_state == SLAP_C_CLIENT ) {
341 ldap_pvt_thread_pool_submit( &connection_pool,
342 conn[j].c_clientfunc, conn[j].c_clientarg );
346 ldap_pvt_thread_mutex_lock( &conn[j].c_mutex );
347 /* connections_mutex and c_mutex are locked */
348 connection_closing( &conn[j], "connection shutdown" );
349 connection_close( &conn[j] );
350 ldap_pvt_thread_mutex_unlock( &conn[j].c_mutex );
353 ldap_pvt_thread_mutex_unlock( &connections_mutex[i] );
363 ldap_pvt_thread_mutex_lock( &connections_mutex );
365 for ( i = 0; i < dtblsize; i++ ) {
366 if( connections[i].c_struct_state != SLAP_C_USED ) {
369 /* give persistent clients a chance to cleanup */
370 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
371 ldap_pvt_thread_pool_submit( &connection_pool,
372 connections[i].c_clientfunc, connections[i].c_clientarg );
376 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
378 /* connections_mutex and c_mutex are locked */
379 connection_closing( &connections[i], "slapd shutdown" );
380 connection_close( &connections[i] );
382 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
385 ldap_pvt_thread_mutex_unlock( &connections_mutex );
392 * Timeout idle connections.
394 int connections_timeout_idle(time_t now)
400 for( c = connection_first( &connindex );
402 c = connection_next( c, &connindex ) )
404 /* Don't timeout a slow-running request or a persistent
405 * outbound connection */
406 if( c->c_n_ops_executing || c->c_conn_state == SLAP_C_CLIENT ) {
410 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
412 connection_closing( c, "idletimeout" );
413 connection_close( c );
417 connection_done( c );
422 static Connection* connection_get( ber_socket_t s )
424 /* connections_mutex should be locked by caller */
428 Debug( LDAP_DEBUG_ARGS,
429 "connection_get(%ld)\n",
432 assert( connections != NULL );
434 if(s == AC_SOCKET_INVALID) return NULL;
437 assert( MCA_conn_check( s ) );
438 c = MCA_GET_CONNECTION(s);
440 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
447 for(i=0; i<dtblsize; i++) {
448 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
449 assert( connections[i].c_conn_state == SLAP_C_INVALID );
450 assert( connections[i].c_sb == 0 );
454 ber_sockbuf_ctrl( connections[i].c_sb,
455 LBER_SB_OPT_GET_FD, &sd );
457 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
458 assert( connections[i].c_conn_state == SLAP_C_INVALID );
459 assert( sd == AC_SOCKET_INVALID );
463 /* state can actually change from used -> unused by resched,
464 * so don't assert details here.
478 ldap_pvt_thread_mutex_lock( &c->c_mutex );
480 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
481 if( c->c_struct_state != SLAP_C_USED ) {
482 /* connection must have been closed due to resched */
484 assert( c->c_conn_state == SLAP_C_INVALID );
485 assert( sd == AC_SOCKET_INVALID );
487 Debug( LDAP_DEBUG_TRACE,
488 "connection_get(%d): connection not used\n",
491 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
495 Debug( LDAP_DEBUG_TRACE,
496 "connection_get(%d): got connid=%lu\n",
501 assert( c->c_struct_state == SLAP_C_USED );
502 assert( c->c_conn_state != SLAP_C_INVALID );
503 assert( sd != AC_SOCKET_INVALID );
505 #ifndef SLAPD_MONITOR
506 if ( global_idletimeout > 0 )
507 #endif /* ! SLAPD_MONITOR */
509 c->c_activitytime = slap_get_time();
516 static void connection_return( Connection *c )
518 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
521 long connection_init(
525 const char* peername,
528 struct berval *authid )
533 assert( connections != NULL );
535 assert( listener != NULL );
536 assert( dnsname != NULL );
537 assert( peername != NULL );
540 assert( flags != CONN_IS_TLS );
543 if( s == AC_SOCKET_INVALID ) {
544 Debug( LDAP_DEBUG_ANY,
545 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
551 assert( s < dtblsize );
554 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
557 assert( MCA_conn_check( s ) );
558 c = MCA_GET_CONNECTION(s);
564 for( i=0; i < dtblsize; i++) {
567 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
568 assert( connections[i].c_sb == 0 );
573 sd = AC_SOCKET_INVALID;
574 if (connections[i].c_sb != NULL) {
575 ber_sockbuf_ctrl( connections[i].c_sb,
576 LBER_SB_OPT_GET_FD, &sd );
579 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
580 assert( sd == AC_SOCKET_INVALID );
585 if( connections[i].c_conn_state == SLAP_C_CLIENT ) continue;
587 assert( connections[i].c_struct_state == SLAP_C_USED );
588 assert( connections[i].c_conn_state != SLAP_C_INVALID );
589 assert( sd != AC_SOCKET_INVALID );
593 Debug( LDAP_DEBUG_ANY,
594 "connection_init(%d): connection table full "
595 "(%d/%d)\n", s, i, dtblsize);
596 ldap_pvt_thread_mutex_unlock( &connections_mutex );
604 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
605 c->c_send_ldap_result = slap_send_ldap_result;
606 c->c_send_search_entry = slap_send_search_entry;
607 c->c_send_search_reference = slap_send_search_reference;
608 c->c_send_ldap_extended = slap_send_ldap_extended;
609 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
611 BER_BVZERO( &c->c_authmech );
612 BER_BVZERO( &c->c_dn );
613 BER_BVZERO( &c->c_ndn );
615 c->c_listener = NULL;
616 BER_BVZERO( &c->c_peer_domain );
617 BER_BVZERO( &c->c_peer_name );
619 LDAP_STAILQ_INIT(&c->c_ops);
620 LDAP_STAILQ_INIT(&c->c_pending_ops);
622 BER_BVZERO( &c->c_sasl_bind_mech );
624 c->c_sasl_authctx = NULL;
625 c->c_sasl_sockctx = NULL;
626 c->c_sasl_extra = NULL;
627 c->c_sasl_bindop = NULL;
629 c->c_sb = ber_sockbuf_alloc( );
632 ber_len_t max = sockbuf_max_incoming;
633 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
636 c->c_currentber = NULL;
638 /* should check status of thread calls */
639 ldap_pvt_thread_mutex_init( &c->c_mutex );
640 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
641 ldap_pvt_thread_cond_init( &c->c_write_cv );
644 if ( slapi_plugins_used ) {
645 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
649 c->c_struct_state = SLAP_C_UNUSED;
652 ldap_pvt_thread_mutex_lock( &c->c_mutex );
654 assert( c->c_struct_state == SLAP_C_UNUSED );
655 assert( BER_BVISNULL( &c->c_authmech ) );
656 assert( BER_BVISNULL( &c->c_dn ) );
657 assert( BER_BVISNULL( &c->c_ndn ) );
658 assert( c->c_listener == NULL );
659 assert( BER_BVISNULL( &c->c_peer_domain ) );
660 assert( BER_BVISNULL( &c->c_peer_name ) );
661 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
662 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
663 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
664 assert( c->c_sasl_done == 0 );
665 assert( c->c_sasl_authctx == NULL );
666 assert( c->c_sasl_sockctx == NULL );
667 assert( c->c_sasl_extra == NULL );
668 assert( c->c_sasl_bindop == NULL );
669 assert( c->c_currentber == NULL );
670 assert( c->c_writewaiter == 0);
672 c->c_listener = listener;
674 if ( flags == CONN_IS_CLIENT ) {
675 c->c_conn_state = SLAP_C_CLIENT;
676 c->c_struct_state = SLAP_C_USED;
677 c->c_close_reason = "?"; /* should never be needed */
678 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
679 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
680 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
685 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
686 ber_str2bv( peername, 0, 1, &c->c_peer_name );
688 c->c_n_ops_received = 0;
689 c->c_n_ops_executing = 0;
690 c->c_n_ops_pending = 0;
691 c->c_n_ops_completed = 0;
697 /* set to zero until bind, implies LDAP_VERSION3 */
700 #ifndef SLAPD_MONITOR
701 if ( global_idletimeout > 0 )
702 #endif /* ! SLAPD_MONITOR */
704 c->c_activitytime = c->c_starttime = slap_get_time();
707 #ifdef LDAP_CONNECTIONLESS
709 if( flags == CONN_IS_UDP ) {
712 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
713 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
715 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
716 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
717 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
718 LBER_SBIOD_LEVEL_PROVIDER, NULL );
723 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
724 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
726 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
727 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
731 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
732 INT_MAX, (void*)"ldap_" );
735 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
736 c /* non-NULL */ ) < 0 )
738 Debug( LDAP_DEBUG_ANY,
739 "connection_init(%d, %s): set nonblocking failed\n",
740 s, c->c_peer_name.bv_val, 0 );
743 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
744 id = c->c_connid = conn_nextid++;
745 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
747 c->c_conn_state = SLAP_C_INACTIVE;
748 c->c_struct_state = SLAP_C_USED;
749 c->c_close_reason = "?"; /* should never be needed */
751 c->c_ssf = c->c_transport_ssf = ssf;
755 if ( flags == CONN_IS_TLS ) {
757 c->c_needs_tls_accept = 1;
760 c->c_needs_tls_accept = 0;
764 slap_sasl_open( c, 0 );
765 slap_sasl_external( c, ssf, authid );
767 slapd_add_internal( s, 1 );
768 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
769 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
771 backend_connection_init(c);
776 void connection2anonymous( Connection *c )
778 assert( connections != NULL );
782 ber_len_t max = sockbuf_max_incoming;
783 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
786 if ( !BER_BVISNULL( &c->c_authmech ) ) {
787 ch_free(c->c_authmech.bv_val);
789 BER_BVZERO( &c->c_authmech );
791 if ( !BER_BVISNULL( &c->c_dn ) ) {
792 ch_free(c->c_dn.bv_val);
794 BER_BVZERO( &c->c_dn );
796 if ( !BER_BVISNULL( &c->c_ndn ) ) {
797 ch_free(c->c_ndn.bv_val);
799 BER_BVZERO( &c->c_ndn );
801 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
802 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
804 BER_BVZERO( &c->c_sasl_authz_dn );
806 c->c_authz_backend = NULL;
810 connection_destroy( Connection *c )
812 /* note: connections_mutex should be locked by caller */
814 unsigned long connid;
815 const char *close_reason;
817 assert( connections != NULL );
819 assert( c->c_struct_state != SLAP_C_UNUSED );
820 assert( c->c_conn_state != SLAP_C_INVALID );
821 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
822 assert( c->c_writewaiter == 0);
824 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
825 connid = c->c_connid;
826 close_reason = c->c_close_reason;
828 backend_connection_destroy(c);
833 c->c_activitytime = c->c_starttime = 0;
835 connection2anonymous( c );
836 c->c_listener = NULL;
838 if(c->c_peer_domain.bv_val != NULL) {
839 free(c->c_peer_domain.bv_val);
841 BER_BVZERO( &c->c_peer_domain );
842 if(c->c_peer_name.bv_val != NULL) {
843 free(c->c_peer_name.bv_val);
845 BER_BVZERO( &c->c_peer_name );
847 c->c_sasl_bind_in_progress = 0;
848 if(c->c_sasl_bind_mech.bv_val != NULL) {
849 free(c->c_sasl_bind_mech.bv_val);
851 BER_BVZERO( &c->c_sasl_bind_mech );
853 slap_sasl_close( c );
855 if ( c->c_currentber != NULL ) {
856 ber_free( c->c_currentber, 1 );
857 c->c_currentber = NULL;
860 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
862 ber_sockbuf_free( c->c_sb );
863 if ( sd != AC_SOCKET_INVALID ) {
864 slapd_remove( sd, 1, 0, 1 );
866 Statslog( LDAP_DEBUG_STATS, (close_reason
867 ? "conn=%lu fd=%ld closed (%s)\n"
868 : "conn=%lu fd=%ld closed\n"),
869 connid, (long) sd, close_reason, 0, 0 );
874 c->c_sb = ber_sockbuf_alloc( );
877 ber_len_t max = sockbuf_max_incoming;
878 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
881 c->c_conn_state = SLAP_C_INVALID;
882 c->c_struct_state = SLAP_C_UNUSED;
883 c->c_close_reason = "?"; /* should never be needed */
886 /* call destructors, then constructors; avoids unnecessary allocation */
887 if ( slapi_plugins_used ) {
888 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
893 int connection_state_closing( Connection *c )
895 /* c_mutex must be locked by caller */
899 assert( c->c_struct_state == SLAP_C_USED );
901 state = c->c_conn_state;
903 assert( state != SLAP_C_INVALID );
905 return state == SLAP_C_CLOSING;
908 static void connection_abandon( Connection *c )
910 /* c_mutex must be locked by caller */
912 Operation *o, *next, op = {0};
918 op.o_connid = c->c_connid;
919 op.o_tag = LDAP_REQ_ABANDON;
920 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
921 next = LDAP_STAILQ_NEXT( o, o_next );
922 op.orn_msgid = o->o_msgid;
924 op.o_bd = frontendDB;
925 frontendDB->be_abandon( &op, &rs );
928 /* remove pending operations */
929 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
930 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
931 LDAP_STAILQ_NEXT(o, o_next) = NULL;
936 void connection_closing( Connection *c, const char *why )
938 assert( connections != NULL );
940 assert( c->c_struct_state == SLAP_C_USED );
941 assert( c->c_conn_state != SLAP_C_INVALID );
943 /* c_mutex must be locked by caller */
945 if( c->c_conn_state != SLAP_C_CLOSING ) {
948 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
949 Debug( LDAP_DEBUG_TRACE,
950 "connection_closing: readying conn=%lu sd=%d for close\n",
951 c->c_connid, sd, 0 );
952 /* update state to closing */
953 c->c_conn_state = SLAP_C_CLOSING;
954 c->c_close_reason = why;
956 /* don't listen on this port anymore */
957 slapd_clr_read( sd, 1 );
959 /* abandon active operations */
960 connection_abandon( c );
962 /* wake write blocked operations */
963 slapd_clr_write( sd, 1 );
964 if ( c->c_writewaiter ) {
965 ldap_pvt_thread_cond_signal( &c->c_write_cv );
966 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
967 ldap_pvt_thread_yield();
968 ldap_pvt_thread_mutex_lock( &c->c_mutex );
970 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
971 /* Client closed connection after doing Unbind. */
972 c->c_close_reason = NULL;
976 static void connection_close( Connection *c )
980 assert( connections != NULL );
982 assert( c->c_struct_state == SLAP_C_USED );
983 assert( c->c_conn_state == SLAP_C_CLOSING );
985 /* note: connections_mutex and c_mutex should be locked by caller */
987 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
988 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
989 Debug( LDAP_DEBUG_TRACE,
990 "connection_close: deferring conn=%lu sd=%d\n",
991 c->c_connid, sd, 0 );
995 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
996 c->c_connid, sd, 0 );
997 connection_destroy( c );
1000 unsigned long connections_nextid(void)
1003 assert( connections != NULL );
1005 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1009 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
1014 Connection* connection_first( ber_socket_t *index )
1016 #ifdef SLAP_MULTI_CONN_ARRAY
1020 assert( connections != NULL );
1021 assert( index != NULL );
1023 #ifdef SLAP_MULTI_CONN_ARRAY
1024 for ( conn_array_id = 0;
1025 conn_array_id < NUM_CONNECTION_ARRAY;
1028 ldap_pvt_thread_mutex_lock( &connections_mutex[ conn_array_id ] );
1031 ldap_pvt_thread_mutex_lock( &connections_mutex );
1036 return connection_next(NULL, index);
1039 Connection* connection_next( Connection *c, ber_socket_t *index )
1040 #ifdef SLAP_MULTI_CONN_ARRAY
1044 assert( connections != NULL );
1045 assert( index != NULL );
1046 assert( *index >= 0 && *index < MCA_ARRAY_SIZE );
1048 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1052 for(; *index < dtblsize; (*index)++) {
1053 assert( MCA_conn_check( *index ) );
1054 conn = MCA_GET_CONNECTION(*index);
1055 if( conn->c_struct_state == SLAP_C_UNINITIALIZED ) {
1056 assert( conn->c_conn_state == SLAP_C_INVALID );
1057 #ifndef HAVE_WINSOCK
1064 if( conn->c_struct_state == SLAP_C_USED ) {
1065 assert( conn->c_conn_state != SLAP_C_INVALID );
1071 assert( conn->c_struct_state == SLAP_C_UNUSED );
1072 assert( conn->c_conn_state == SLAP_C_INVALID );
1075 if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
1082 assert( connections != NULL );
1083 assert( index != NULL );
1084 assert( *index <= dtblsize );
1086 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1090 for(; *index < dtblsize; (*index)++) {
1091 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
1092 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1093 #ifndef HAVE_WINSOCK
1100 if( connections[*index].c_struct_state == SLAP_C_USED ) {
1101 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
1102 c = &connections[(*index)++];
1106 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
1107 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1110 if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
1115 void connection_done( Connection *c )
1117 #ifdef SLAP_MULTI_CONN_ARRAY
1121 assert( connections != NULL );
1123 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1125 #ifdef SLAP_MULTI_CONN_ARRAY
1126 for ( conn_array_id = 0;
1127 conn_array_id < NUM_CONNECTION_ARRAY;
1130 ldap_pvt_thread_mutex_unlock( &connections_mutex[ conn_array_id ] );
1133 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1138 * connection_activity - handle the request operation op on connection
1139 * conn. This routine figures out what kind of operation it is and
1140 * calls the appropriate stub to handle it.
1143 #ifdef SLAPD_MONITOR
1144 /* FIXME: returns 0 in case of failure */
1145 #define INCR_OP_INITIATED(index) \
1147 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1148 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
1149 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1151 #define INCR_OP_COMPLETED(index) \
1153 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1154 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1155 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
1156 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1158 #else /* !SLAPD_MONITOR */
1159 #define INCR_OP_INITIATED(index) do { } while (0)
1160 #define INCR_OP_COMPLETED(index) \
1162 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1163 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1164 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1166 #endif /* !SLAPD_MONITOR */
1169 * NOTE: keep in sync with enum in slapd.h
1171 static int (*opfun[])( Operation *op, SlapReply *rs ) = {
1186 connection_operation( void *ctx, void *arg_v )
1188 int rc = LDAP_OTHER;
1189 Operation *op = arg_v;
1190 SlapReply rs = {REP_RESULT};
1191 ber_tag_t tag = op->o_tag;
1193 Connection *conn = op->o_conn;
1194 void *memctx = NULL;
1195 void *memctx_null = NULL;
1198 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
1199 /* FIXME: returns 0 in case of failure */
1200 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
1201 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1203 op->o_threadctx = ctx;
1207 case LDAP_REQ_UNBIND:
1209 case LDAP_REQ_DELETE:
1210 case LDAP_REQ_MODDN:
1211 case LDAP_REQ_MODIFY:
1212 case LDAP_REQ_COMPARE:
1213 case LDAP_REQ_SEARCH:
1214 case LDAP_REQ_ABANDON:
1215 case LDAP_REQ_EXTENDED:
1218 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1219 "conn %lu unknown LDAP request 0x%lx\n",
1220 conn->c_connid, tag, 0 );
1221 op->o_tag = LBER_ERROR;
1222 rs.sr_err = LDAP_PROTOCOL_ERROR;
1223 rs.sr_text = "unknown LDAP request";
1224 send_ldap_disconnect( op, &rs );
1225 rc = SLAPD_DISCONNECT;
1226 goto operations_error;
1229 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1230 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1231 "error: SASL bind in progress (tag=%ld).\n",
1233 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1234 "SASL bind in progress" );
1235 rc = LDAP_OPERATIONS_ERROR;
1236 goto operations_error;
1239 /* We can use Thread-Local storage for most mallocs. We can
1240 * also use TL for ber parsing, but not on Add or Modify.
1243 memsiz = ber_len( op->o_ber ) * 64;
1244 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1246 memsiz = SLAP_SLAB_SIZE;
1248 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
1249 op->o_tmpmemctx = memctx;
1250 op->o_tmpmfuncs = &slap_sl_mfuncs;
1251 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1252 /* Note - the ber and its buffer are already allocated from
1253 * regular memory; this only affects subsequent mallocs that
1254 * ber_scanf may invoke.
1256 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1261 opidx = SLAP_OP_BIND;
1264 case LDAP_REQ_UNBIND:
1265 opidx = SLAP_OP_UNBIND;
1269 opidx = SLAP_OP_ADD;
1272 case LDAP_REQ_DELETE:
1273 opidx = SLAP_OP_DELETE;
1276 case LDAP_REQ_MODRDN:
1277 opidx = SLAP_OP_MODRDN;
1280 case LDAP_REQ_MODIFY:
1281 opidx = SLAP_OP_MODIFY;
1284 case LDAP_REQ_COMPARE:
1285 opidx = SLAP_OP_COMPARE;
1288 case LDAP_REQ_SEARCH:
1289 opidx = SLAP_OP_SEARCH;
1292 case LDAP_REQ_ABANDON:
1293 opidx = SLAP_OP_ABANDON;
1296 case LDAP_REQ_EXTENDED:
1297 opidx = SLAP_OP_EXTENDED;
1305 assert( opidx > -1 );
1306 INCR_OP_INITIATED( opidx );
1307 rc = (*(opfun[opidx]))( op, &rs );
1310 if ( rc == SLAPD_DISCONNECT ) {
1313 } else if ( opidx > -1 ) {
1314 /* increment completed operations count
1315 * only if operation was initiated
1316 * and rc != SLAPD_DISCONNECT */
1317 INCR_OP_COMPLETED( opidx );
1320 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1321 if ( rc == SLAPD_ABANDON ) {
1322 op->o_cancel = SLAP_CANCEL_ACK;
1324 op->o_cancel = LDAP_TOO_LATE;
1327 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1328 op->o_cancel != SLAP_CANCEL_DONE )
1330 ldap_pvt_thread_yield();
1333 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1335 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1337 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1338 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1340 conn->c_n_ops_executing--;
1341 conn->c_n_ops_completed++;
1345 case LDAP_REQ_UNBIND:
1346 /* c_mutex is locked */
1347 connection_closing( conn,
1348 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1352 connection_resched( conn );
1353 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1357 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1359 int connection_client_setup(
1361 ldap_pvt_thread_start_t *func,
1367 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1368 CONN_IS_CLIENT, 0, NULL );
1369 if ( rc < 0 ) return -1;
1371 c = connection_get( s );
1372 c->c_clientfunc = func;
1373 c->c_clientarg = arg;
1375 slapd_add_internal( s, 0 );
1376 slapd_set_read( s, 1 );
1377 connection_return( c );
1381 void connection_client_enable(
1384 slapd_set_read( s, 1 );
1387 void connection_client_stop(
1392 /* get (locked) connection */
1393 c = connection_get( s );
1395 assert( c->c_conn_state == SLAP_C_CLIENT );
1397 c->c_listener = NULL;
1398 c->c_conn_state = SLAP_C_INVALID;
1399 c->c_struct_state = SLAP_C_UNUSED;
1400 c->c_close_reason = "?"; /* should never be needed */
1402 ber_sockbuf_free( c->c_sb );
1403 slapd_remove( s, 0, 1, 1 );
1404 c->c_sb = ber_sockbuf_alloc( );
1406 ber_len_t max = sockbuf_max_incoming;
1407 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1410 connection_return( c );
1413 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1415 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1417 static void* connection_read_thread( void* ctx, void* argv )
1420 conn_readinfo cri = { NULL, NULL, NULL, 0 };
1421 ber_socket_t s = (long)argv;
1424 * read incoming LDAP requests. If there is more than one,
1425 * the first one is returned with new_op
1427 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1428 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1429 return (void*)(long)rc;
1432 /* execute a single queued request in the same thread */
1433 if( cri.op && !cri.nullop ) {
1434 rc = (long)connection_operation( ctx, cri.op );
1435 } else if ( cri.func ) {
1436 rc = (long)cri.func( ctx, cri.arg );
1439 return (void*)(long)rc;
1442 int connection_read_activate( ber_socket_t s )
1447 * suspend reading on this file descriptor until a connection processing
1448 * thread reads data on it. Otherwise the listener thread will repeatedly
1449 * submit the same event on it to the pool.
1451 rc = slapd_clr_read( s, 0 );
1455 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1456 connection_read_thread, (void *)(long)s );
1459 Debug( LDAP_DEBUG_ANY,
1460 "connection_read_activate(%d): submit failed (%d)\n",
1468 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1470 connection_read( ber_socket_t s, conn_readinfo *cri )
1472 int connection_read(ber_socket_t s)
1478 assert( connections != NULL );
1480 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
1482 /* get (locked) connection */
1483 c = connection_get( s );
1486 Debug( LDAP_DEBUG_ANY,
1487 "connection_read(%ld): no connection!\n",
1490 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1496 if( c->c_conn_state == SLAP_C_CLOSING ) {
1497 Debug( LDAP_DEBUG_TRACE,
1498 "connection_read(%d): closing, ignoring input for id=%lu\n",
1499 s, c->c_connid, 0 );
1501 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1502 slapd_set_read( s, 1 );
1504 connection_return( c );
1505 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1509 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1510 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1511 cri->func = c->c_clientfunc;
1512 cri->arg = c->c_clientarg;
1513 /* read should already be cleared */
1515 slapd_clr_read( s, 0 );
1516 ldap_pvt_thread_pool_submit( &connection_pool,
1517 c->c_clientfunc, c->c_clientarg );
1519 connection_return( c );
1520 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1524 Debug( LDAP_DEBUG_TRACE,
1525 "connection_read(%d): checking for input on id=%lu\n",
1526 s, c->c_connid, 0 );
1529 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1530 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1532 Debug( LDAP_DEBUG_TRACE,
1533 "connection_read(%d): TLS accept failure "
1534 "error=%d id=%lu, closing\n",
1535 s, rc, c->c_connid );
1537 c->c_needs_tls_accept = 0;
1538 /* connections_mutex and c_mutex are locked */
1539 connection_closing( c, "TLS negotiation failure" );
1545 /* Drain input before close, to allow SSL error codes
1546 * to propagate to client. */
1552 rc = select(s+1, &rfd, NULL, NULL, &tv);
1554 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1560 connection_close( c );
1561 connection_return( c );
1562 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1565 } else if ( rc == 0 ) {
1567 struct berval authid = BER_BVNULL;
1569 c->c_needs_tls_accept = 0;
1571 /* we need to let SASL know */
1572 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1574 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1575 if( c->c_tls_ssf > c->c_ssf ) {
1576 c->c_ssf = c->c_tls_ssf;
1579 rc = dnX509peerNormalize( ssl, &authid );
1580 if ( rc != LDAP_SUCCESS ) {
1581 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1582 "unable to get TLS client DN, error=%d id=%lu\n",
1583 s, rc, c->c_connid );
1585 Statslog( LDAP_DEBUG_STATS,
1586 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1587 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1588 slap_sasl_external( c, c->c_tls_ssf, &authid );
1589 if ( authid.bv_val ) free( authid.bv_val );
1592 /* if success and data is ready, fall thru to data input loop */
1594 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1596 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1597 slapd_set_read( s, 1 );
1600 connection_return( c );
1601 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1607 #ifdef HAVE_CYRUS_SASL
1608 if ( c->c_sasl_layers ) {
1609 /* If previous layer is not removed yet, give up for now */
1610 if ( !c->c_sasl_sockctx ) {
1611 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1612 slapd_set_read( s, 1 );
1615 connection_return( c );
1616 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1620 c->c_sasl_layers = 0;
1622 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1623 if( rc != LDAP_SUCCESS ) {
1624 Debug( LDAP_DEBUG_TRACE,
1625 "connection_read(%d): SASL install error "
1626 "error=%d id=%lu, closing\n",
1627 s, rc, c->c_connid );
1629 /* connections_mutex and c_mutex are locked */
1630 connection_closing( c, "SASL layer install failure" );
1631 connection_close( c );
1632 connection_return( c );
1633 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1639 #define CONNECTION_INPUT_LOOP 1
1640 /* #define DATA_READY_LOOP 1 */
1643 /* How do we do this without getting into a busy loop ? */
1644 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1645 rc = connection_input( c, cri );
1647 rc = connection_input( c );
1650 #ifdef DATA_READY_LOOP
1651 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1652 #elif CONNECTION_INPUT_LOOP
1659 Debug( LDAP_DEBUG_TRACE,
1660 "connection_read(%d): input error=%d id=%lu, closing.\n",
1661 s, rc, c->c_connid );
1663 /* connections_mutex and c_mutex are locked */
1664 connection_closing( c, conn_lost_str );
1665 connection_close( c );
1666 connection_return( c );
1667 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1671 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1672 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1673 slapd_set_write( s, 0 );
1676 slapd_set_read( s, 1 );
1678 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1679 slapd_set_read( s, 1 );
1682 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1683 slapd_set_write( s, 1 );
1687 connection_return( c );
1688 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1694 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1695 connection_input( Connection *conn , conn_readinfo *cri )
1697 connection_input( Connection *conn )
1706 #ifdef LDAP_CONNECTIONLESS
1712 if ( conn->c_currentber == NULL &&
1713 ( conn->c_currentber = ber_alloc()) == NULL )
1715 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1721 #ifdef LDAP_CONNECTIONLESS
1722 if ( conn->c_is_udp ) {
1723 char peername[sizeof("IP=255.255.255.255:65336")];
1725 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1726 if (len != sizeof(struct sockaddr)) return 1;
1728 sprintf( peername, "IP=%s:%d",
1729 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1730 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1731 Statslog( LDAP_DEBUG_STATS,
1732 "conn=%lu UDP request from %s (%s) accepted.\n",
1733 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1737 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1738 if ( tag != LDAP_TAG_MESSAGE ) {
1742 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1744 Debug( LDAP_DEBUG_TRACE,
1745 "ber_get_next on fd %d failed errno=%d (%s)\n",
1746 sd, err, sock_errstr(err) );
1747 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1748 /* log, close and send error */
1749 ber_free( conn->c_currentber, 1 );
1750 conn->c_currentber = NULL;
1757 ber = conn->c_currentber;
1758 conn->c_currentber = NULL;
1760 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1761 /* log, close and send error */
1762 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1767 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1768 /* log, close and send error */
1769 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1775 #ifdef LDAP_CONNECTIONLESS
1776 if( conn->c_is_udp ) {
1777 if( tag == LBER_OCTETSTRING ) {
1778 ber_get_stringa( ber, &cdn );
1779 tag = ber_peek_tag(ber, &len);
1781 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1782 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1789 if(tag == LDAP_REQ_BIND) {
1790 /* immediately abandon all existing operations upon BIND */
1791 connection_abandon( conn );
1794 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1797 /* clear state if the connection is being reused from inactive */
1798 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1799 memset( &conn->c_pagedresults_state, 0,
1800 sizeof( conn->c_pagedresults_state ) );
1803 op->o_res_ber = NULL;
1805 #ifdef LDAP_CONNECTIONLESS
1806 if (conn->c_is_udp) {
1808 ber_str2bv( cdn, 0, 1, &op->o_dn );
1809 op->o_protocol = LDAP_VERSION2;
1811 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1812 if (op->o_res_ber == NULL) return 1;
1814 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1815 sizeof(struct sockaddr), 0 );
1817 if (rc != sizeof(struct sockaddr)) {
1818 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1822 if (op->o_protocol == LDAP_VERSION2) {
1823 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1825 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1830 #endif /* LDAP_CONNECTIONLESS */
1834 /* Don't process requests when the conn is in the middle of a
1835 * Bind, or if it's closing. Also, don't let any single conn
1836 * use up all the available threads, and don't execute if we're
1837 * currently blocked on output. And don't execute if there are
1838 * already pending ops, let them go first. Abandon operations
1839 * get exceptions to some, but not all, cases.
1843 /* Abandon and Unbind are exempt from these checks */
1844 if (conn->c_conn_state == SLAP_C_CLOSING) {
1847 } else if (conn->c_writewaiter) {
1848 defer = "awaiting write";
1850 } else if (conn->c_n_ops_pending) {
1851 defer = "pending operations";
1855 case LDAP_REQ_ABANDON:
1856 /* Unbind is exempt from these checks */
1857 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1858 defer = "too many executing";
1860 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1865 case LDAP_REQ_UNBIND:
1870 int max = conn->c_dn.bv_len
1871 ? slap_conn_max_pending_auth
1872 : slap_conn_max_pending;
1874 Debug( LDAP_DEBUG_ANY,
1875 "connection_input: conn=%lu deferring operation: %s\n",
1876 conn->c_connid, defer, 0 );
1877 conn->c_n_ops_pending++;
1878 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1879 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1882 conn->c_n_ops_executing++;
1884 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1886 * The first op will be processed in the same thread context,
1887 * as long as there is only one op total.
1888 * Subsequent ops will be submitted to the pool by
1889 * calling connection_op_activate()
1891 if ( cri->op == NULL ) {
1892 /* the first incoming request */
1893 connection_op_queue( op );
1896 if ( !cri->nullop ) {
1898 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1899 connection_operation, (void *) cri->op );
1901 connection_op_activate( op );
1904 connection_op_activate( op );
1909 if ( conn->c_struct_state != SLAP_C_USED ) {
1910 /* connection must have got closed underneath us */
1915 assert( conn->c_struct_state == SLAP_C_USED );
1920 connection_resched( Connection *conn )
1924 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1927 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1929 /* use trylock to avoid possible deadlock */
1930 rc = ldap_pvt_thread_mutex_trylock( MCA_GET_CONN_MUTEX( sd ) );
1933 Debug( LDAP_DEBUG_TRACE,
1934 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1935 conn->c_connid, sd, 0 );
1937 * reaquire locks in the right order...
1938 * this may allow another thread to close this connection,
1939 * so recheck state below.
1941 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1942 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX ( sd ) );
1943 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1946 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1947 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1948 "closed by other thread conn=%lu sd=%d\n",
1949 conn->c_connid, sd, 0 );
1951 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1952 "attempting closing conn=%lu sd=%d\n",
1953 conn->c_connid, sd, 0 );
1954 connection_close( conn );
1957 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( sd ) );
1961 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1962 /* other states need different handling */
1966 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1967 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1969 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1970 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1972 /* pending operations should not be marked for abandonment */
1973 assert(!op->o_abandon);
1975 conn->c_n_ops_pending--;
1976 conn->c_n_ops_executing++;
1978 connection_op_activate( op );
1980 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1986 connection_init_log_prefix( Operation *op )
1988 if ( op->o_connid == (unsigned long)(-1) ) {
1989 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1990 "conn=-1 op=%lu", op->o_opid );
1993 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1994 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1998 static int connection_bind_cb( Operation *op, SlapReply *rs )
2000 slap_callback *cb = op->o_callback;
2002 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
2003 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
2004 op->o_conn->c_sasl_bind_in_progress =
2005 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
2007 /* Moved here from bind.c due to ITS#4158 */
2008 op->o_conn->c_sasl_bindop = NULL;
2009 if ( op->orb_method == LDAP_AUTH_SASL ) {
2010 if( rs->sr_err == LDAP_SUCCESS ) {
2011 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
2012 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
2013 /* edn is always normalized already */
2014 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
2016 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
2017 BER_BVZERO( &op->orb_edn );
2018 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
2019 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
2021 op->o_conn->c_sasl_ssf = op->orb_ssf;
2022 if( op->orb_ssf > op->o_conn->c_ssf ) {
2023 op->o_conn->c_ssf = op->orb_ssf;
2026 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
2027 ber_len_t max = sockbuf_max_incoming_auth;
2028 ber_sockbuf_ctrl( op->o_conn->c_sb,
2029 LBER_SB_OPT_SET_MAX_INCOMING, &max );
2032 /* log authorization identity */
2033 Statslog( LDAP_DEBUG_STATS,
2034 "%s BIND dn=\"%s\" mech=%s ssf=%d\n",
2036 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
2037 op->o_conn->c_authmech.bv_val, op->orb_ssf, 0 );
2039 Debug( LDAP_DEBUG_TRACE,
2040 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
2041 op->o_conn->c_authmech.bv_val,
2042 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
2045 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
2046 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
2047 free( op->o_conn->c_sasl_bind_mech.bv_val );
2048 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
2052 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
2054 ch_free( op->o_callback );
2055 op->o_callback = NULL;
2057 return SLAP_CB_CONTINUE;
2060 static void connection_op_queue( Operation *op )
2062 ber_tag_t tag = op->o_tag;
2064 if (tag == LDAP_REQ_BIND) {
2065 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
2066 sc->sc_response = connection_bind_cb;
2067 sc->sc_next = op->o_callback;
2068 op->o_callback = sc;
2069 op->o_conn->c_conn_state = SLAP_C_BINDING;
2072 if (!op->o_dn.bv_len) {
2073 op->o_authz = op->o_conn->c_authz;
2074 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
2075 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
2076 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
2078 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
2079 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
2083 op->o_authtype = op->o_conn->c_authtype;
2084 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
2086 if (!op->o_protocol) {
2087 op->o_protocol = op->o_conn->c_protocol
2088 ? op->o_conn->c_protocol : LDAP_VERSION3;
2091 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
2092 op->o_protocol > LDAP_VERSION2)
2094 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
2097 op->o_connid = op->o_conn->c_connid;
2098 connection_init_log_prefix( op );
2100 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
2103 static int connection_op_activate( Operation *op )
2107 connection_op_queue( op );
2109 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2110 connection_operation, (void *) op );
2113 Debug( LDAP_DEBUG_ANY,
2114 "connection_op_activate: submit failed (%d) for conn=%lu\n",
2115 rc, op->o_connid, 0 );
2116 /* should move op to pending list */
2122 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
2123 static int connection_write( ber_socket_t s );
2124 static void *connection_write_thread( void *ctx, void *arg )
2126 return (void *)(long)connection_write((long)arg);
2129 int connection_write_activate( ber_socket_t s )
2134 * suspend reading on this file descriptor until a connection processing
2135 * thread write data on it. Otherwise the listener thread will repeatedly
2136 * submit the same event on it to the pool.
2138 slapd_clr_write( s, 0);
2140 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2141 connection_write_thread, (void *)(long)s );
2144 Debug( LDAP_DEBUG_ANY,
2145 "connection_write_activate(%d): submit failed (%d)\n",
2153 int connection_write(ber_socket_t s)
2158 assert( connections != NULL );
2160 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX( s ) );
2162 c = connection_get( s );
2164 Debug( LDAP_DEBUG_ANY,
2165 "connection_write(%ld): no connection!\n",
2167 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( s ) );
2171 #ifndef SLAP_LIGHTWEIGHT_DISPATCHER
2172 slapd_clr_write( s, 0);
2176 Debug( LDAP_DEBUG_TRACE,
2177 "connection_write(%d): waking output for id=%lu\n",
2178 s, c->c_connid, 0 );
2179 ldap_pvt_thread_cond_signal( &c->c_write_cv );
2181 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
2182 slapd_set_read( s, 1 );
2184 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
2185 slapd_set_write( s, 1 );
2188 /* If there are ops pending because of a writewaiter,
2191 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
2192 if ( !c->c_writewaiter ) break;
2193 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
2195 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
2196 LDAP_STAILQ_NEXT(op, o_next) = NULL;
2198 /* pending operations should not be marked for abandonment */
2199 assert(!op->o_abandon);
2201 c->c_n_ops_pending--;
2202 c->c_n_ops_executing++;
2204 connection_op_activate( op );
2209 connection_return( c );
2210 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
2215 connection_fake_init(
2220 conn->c_connid = -1;
2221 conn->c_send_ldap_result = slap_send_ldap_result;
2222 conn->c_send_search_entry = slap_send_search_entry;
2223 conn->c_send_search_reference = slap_send_search_reference;
2224 conn->c_listener = (Listener *)&dummy_list;
2225 conn->c_peer_domain = slap_empty_bv;
2226 conn->c_peer_name = slap_empty_bv;
2228 memset(op, 0, OPERATION_BUFFER_SIZE);
2229 op->o_hdr = (Opheader *)(op+1);
2230 op->o_controls = (void **)(op->o_hdr+1);
2231 /* set memory context */
2232 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
2233 op->o_tmpmfuncs = &slap_sl_mfuncs;
2234 op->o_threadctx = ctx;
2237 op->o_connid = op->o_conn->c_connid;
2238 connection_init_log_prefix( op );
2241 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
2242 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
2243 #endif /* LDAP_SLAPI */
2245 slap_op_time( &op->o_time, &op->o_tincr );
2249 connection_assign_nextid( Connection *conn )
2251 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2252 conn->c_connid = conn_nextid++;
2253 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );