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 );
263 if ( slapi_plugins_used ) {
264 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
272 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
273 free( connections[i] );
274 connections[i] = NULL;
275 ldap_pvt_thread_mutex_destroy( &connections_mutex[i] );
279 free( connections_mutex );
281 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
290 /* should check return of every call */
292 if( connections == NULL) {
293 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
298 for ( i = 0; i < dtblsize; i++ ) {
299 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
300 ber_sockbuf_free( connections[i].c_sb );
301 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
302 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
303 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
305 if ( slapi_plugins_used ) {
306 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
316 ldap_pvt_thread_mutex_destroy( &connections_mutex );
317 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
323 * shutdown all connections
325 int connections_shutdown(void)
326 #ifdef SLAP_MULTI_CONN_ARRAY
331 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
332 Connection* conn = connections[i];
333 ldap_pvt_thread_mutex_lock( &connections_mutex[i] );
334 for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) {
335 if( conn[j].c_struct_state != SLAP_C_USED ) {
338 /* give persistent clients a chance to cleanup */
339 if( conn[j].c_conn_state == SLAP_C_CLIENT ) {
340 ldap_pvt_thread_pool_submit( &connection_pool,
341 conn[j].c_clientfunc, conn[j].c_clientarg );
345 ldap_pvt_thread_mutex_lock( &conn[j].c_mutex );
346 /* connections_mutex and c_mutex are locked */
347 connection_closing( &conn[j], "connection shutdown" );
348 connection_close( &conn[j] );
349 ldap_pvt_thread_mutex_unlock( &conn[j].c_mutex );
352 ldap_pvt_thread_mutex_unlock( &connections_mutex[i] );
362 ldap_pvt_thread_mutex_lock( &connections_mutex );
364 for ( i = 0; i < dtblsize; i++ ) {
365 if( connections[i].c_struct_state != SLAP_C_USED ) {
368 /* give persistent clients a chance to cleanup */
369 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
370 ldap_pvt_thread_pool_submit( &connection_pool,
371 connections[i].c_clientfunc, connections[i].c_clientarg );
375 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
377 /* connections_mutex and c_mutex are locked */
378 connection_closing( &connections[i], "slapd shutdown" );
379 connection_close( &connections[i] );
381 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
384 ldap_pvt_thread_mutex_unlock( &connections_mutex );
391 * Timeout idle connections.
393 int connections_timeout_idle(time_t now)
399 for( c = connection_first( &connindex );
401 c = connection_next( c, &connindex ) )
403 /* Don't timeout a slow-running request or a persistent
404 * outbound connection */
405 if( c->c_n_ops_executing || c->c_conn_state == SLAP_C_CLIENT ) {
409 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
411 connection_closing( c, "idletimeout" );
412 connection_close( c );
416 connection_done( c );
421 static Connection* connection_get( ber_socket_t s )
423 /* connections_mutex should be locked by caller */
427 Debug( LDAP_DEBUG_ARGS,
428 "connection_get(%ld)\n",
431 assert( connections != NULL );
433 if(s == AC_SOCKET_INVALID) return NULL;
436 assert( MCA_conn_check( s ) );
437 c = MCA_GET_CONNECTION(s);
444 for(i=0; i<dtblsize; i++) {
445 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
446 assert( connections[i].c_conn_state == SLAP_C_INVALID );
447 assert( connections[i].c_sb == 0 );
451 ber_sockbuf_ctrl( connections[i].c_sb,
452 LBER_SB_OPT_GET_FD, &sd );
454 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
455 assert( connections[i].c_conn_state == SLAP_C_INVALID );
456 assert( sd == AC_SOCKET_INVALID );
460 /* state can actually change from used -> unused by resched,
461 * so don't assert details here.
475 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
477 ldap_pvt_thread_mutex_lock( &c->c_mutex );
479 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
480 if( c->c_struct_state != SLAP_C_USED ) {
481 /* connection must have been closed due to resched */
483 assert( c->c_conn_state == SLAP_C_INVALID );
484 assert( sd == AC_SOCKET_INVALID );
486 Debug( LDAP_DEBUG_TRACE,
487 "connection_get(%d): connection not used\n",
490 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
494 Debug( LDAP_DEBUG_TRACE,
495 "connection_get(%d): got connid=%lu\n",
500 assert( c->c_struct_state == SLAP_C_USED );
501 assert( c->c_conn_state != SLAP_C_INVALID );
502 assert( sd != AC_SOCKET_INVALID );
504 #ifndef SLAPD_MONITOR
505 if ( global_idletimeout > 0 )
506 #endif /* ! SLAPD_MONITOR */
508 c->c_activitytime = slap_get_time();
515 static void connection_return( Connection *c )
517 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
520 long connection_init(
524 const char* peername,
527 struct berval *authid )
532 assert( connections != NULL );
534 assert( listener != NULL );
535 assert( dnsname != NULL );
536 assert( peername != NULL );
539 assert( flags != CONN_IS_TLS );
542 if( s == AC_SOCKET_INVALID ) {
543 Debug( LDAP_DEBUG_ANY,
544 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
550 assert( s < dtblsize );
553 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
556 assert( MCA_conn_check( s ) );
557 c = MCA_GET_CONNECTION(s);
563 for( i=0; i < dtblsize; i++) {
566 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
567 assert( connections[i].c_sb == 0 );
572 sd = AC_SOCKET_INVALID;
573 if (connections[i].c_sb != NULL) {
574 ber_sockbuf_ctrl( connections[i].c_sb,
575 LBER_SB_OPT_GET_FD, &sd );
578 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
579 assert( sd == AC_SOCKET_INVALID );
584 if( connections[i].c_conn_state == SLAP_C_CLIENT ) continue;
586 assert( connections[i].c_struct_state == SLAP_C_USED );
587 assert( connections[i].c_conn_state != SLAP_C_INVALID );
588 assert( sd != AC_SOCKET_INVALID );
592 Debug( LDAP_DEBUG_ANY,
593 "connection_init(%d): connection table full "
594 "(%d/%d)\n", s, i, dtblsize);
595 ldap_pvt_thread_mutex_unlock( &connections_mutex );
603 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
604 c->c_send_ldap_result = slap_send_ldap_result;
605 c->c_send_search_entry = slap_send_search_entry;
606 c->c_send_search_reference = slap_send_search_reference;
607 c->c_send_ldap_extended = slap_send_ldap_extended;
608 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
610 BER_BVZERO( &c->c_authmech );
611 BER_BVZERO( &c->c_dn );
612 BER_BVZERO( &c->c_ndn );
614 c->c_listener = NULL;
615 BER_BVZERO( &c->c_peer_domain );
616 BER_BVZERO( &c->c_peer_name );
618 LDAP_STAILQ_INIT(&c->c_ops);
619 LDAP_STAILQ_INIT(&c->c_pending_ops);
622 c->c_txn = CONN_TXN_INACTIVE;
623 c->c_txn_backend = NULL;
624 LDAP_STAILQ_INIT(&c->c_txn_ops);
627 BER_BVZERO( &c->c_sasl_bind_mech );
629 c->c_sasl_authctx = NULL;
630 c->c_sasl_sockctx = NULL;
631 c->c_sasl_extra = NULL;
632 c->c_sasl_bindop = NULL;
634 c->c_sb = ber_sockbuf_alloc( );
637 ber_len_t max = sockbuf_max_incoming;
638 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
641 c->c_currentber = NULL;
643 /* should check status of thread calls */
644 ldap_pvt_thread_mutex_init( &c->c_mutex );
645 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
646 ldap_pvt_thread_cond_init( &c->c_write_cv );
649 if ( slapi_plugins_used ) {
650 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
654 c->c_struct_state = SLAP_C_UNUSED;
657 ldap_pvt_thread_mutex_lock( &c->c_mutex );
659 assert( c->c_struct_state == SLAP_C_UNUSED );
660 assert( BER_BVISNULL( &c->c_authmech ) );
661 assert( BER_BVISNULL( &c->c_dn ) );
662 assert( BER_BVISNULL( &c->c_ndn ) );
663 assert( c->c_listener == NULL );
664 assert( BER_BVISNULL( &c->c_peer_domain ) );
665 assert( BER_BVISNULL( &c->c_peer_name ) );
666 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
667 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
669 assert( c->c_txn == CONN_TXN_INACTIVE );
670 assert( c->c_txn_backend == NULL );
671 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
673 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
674 assert( c->c_sasl_done == 0 );
675 assert( c->c_sasl_authctx == NULL );
676 assert( c->c_sasl_sockctx == NULL );
677 assert( c->c_sasl_extra == NULL );
678 assert( c->c_sasl_bindop == NULL );
679 assert( c->c_currentber == NULL );
680 assert( c->c_writewaiter == 0);
682 c->c_listener = listener;
684 if ( flags == CONN_IS_CLIENT ) {
685 c->c_conn_state = SLAP_C_CLIENT;
686 c->c_struct_state = SLAP_C_USED;
687 c->c_close_reason = "?"; /* should never be needed */
688 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
689 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
690 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
695 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
696 ber_str2bv( peername, 0, 1, &c->c_peer_name );
698 c->c_n_ops_received = 0;
699 c->c_n_ops_executing = 0;
700 c->c_n_ops_pending = 0;
701 c->c_n_ops_completed = 0;
707 /* set to zero until bind, implies LDAP_VERSION3 */
710 #ifndef SLAPD_MONITOR
711 if ( global_idletimeout > 0 )
712 #endif /* ! SLAPD_MONITOR */
714 c->c_activitytime = c->c_starttime = slap_get_time();
717 #ifdef LDAP_CONNECTIONLESS
719 if( flags == CONN_IS_UDP ) {
722 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
723 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
725 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
726 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
727 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
728 LBER_SBIOD_LEVEL_PROVIDER, NULL );
733 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
734 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
736 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
737 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
741 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
742 INT_MAX, (void*)"ldap_" );
745 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
746 c /* non-NULL */ ) < 0 )
748 Debug( LDAP_DEBUG_ANY,
749 "connection_init(%d, %s): set nonblocking failed\n",
750 s, c->c_peer_name.bv_val, 0 );
753 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
754 id = c->c_connid = conn_nextid++;
755 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
757 c->c_conn_state = SLAP_C_INACTIVE;
758 c->c_struct_state = SLAP_C_USED;
759 c->c_close_reason = "?"; /* should never be needed */
761 c->c_ssf = c->c_transport_ssf = ssf;
765 if ( flags == CONN_IS_TLS ) {
767 c->c_needs_tls_accept = 1;
770 c->c_needs_tls_accept = 0;
774 slap_sasl_open( c, 0 );
775 slap_sasl_external( c, ssf, authid );
777 slapd_add_internal( s, 1 );
778 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
779 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
781 backend_connection_init(c);
786 void connection2anonymous( Connection *c )
788 assert( connections != NULL );
792 ber_len_t max = sockbuf_max_incoming;
793 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
796 if ( !BER_BVISNULL( &c->c_authmech ) ) {
797 ch_free(c->c_authmech.bv_val);
799 BER_BVZERO( &c->c_authmech );
801 if ( !BER_BVISNULL( &c->c_dn ) ) {
802 ch_free(c->c_dn.bv_val);
804 BER_BVZERO( &c->c_dn );
806 if ( !BER_BVISNULL( &c->c_ndn ) ) {
807 ch_free(c->c_ndn.bv_val);
809 BER_BVZERO( &c->c_ndn );
811 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
812 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
814 BER_BVZERO( &c->c_sasl_authz_dn );
816 c->c_authz_backend = NULL;
820 connection_destroy( Connection *c )
822 /* note: connections_mutex should be locked by caller */
824 unsigned long connid;
825 const char *close_reason;
827 assert( connections != NULL );
829 assert( c->c_struct_state != SLAP_C_UNUSED );
830 assert( c->c_conn_state != SLAP_C_INVALID );
831 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
832 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
834 assert( c->c_txn == CONN_TXN_INACTIVE );
835 assert( c->c_txn_backend == NULL );
836 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
838 assert( c->c_writewaiter == 0);
840 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
841 connid = c->c_connid;
842 close_reason = c->c_close_reason;
844 backend_connection_destroy(c);
849 c->c_activitytime = c->c_starttime = 0;
851 connection2anonymous( c );
852 c->c_listener = NULL;
854 if(c->c_peer_domain.bv_val != NULL) {
855 free(c->c_peer_domain.bv_val);
857 BER_BVZERO( &c->c_peer_domain );
858 if(c->c_peer_name.bv_val != NULL) {
859 free(c->c_peer_name.bv_val);
861 BER_BVZERO( &c->c_peer_name );
863 c->c_sasl_bind_in_progress = 0;
864 if(c->c_sasl_bind_mech.bv_val != NULL) {
865 free(c->c_sasl_bind_mech.bv_val);
867 BER_BVZERO( &c->c_sasl_bind_mech );
869 slap_sasl_close( c );
871 if ( c->c_currentber != NULL ) {
872 ber_free( c->c_currentber, 1 );
873 c->c_currentber = NULL;
876 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
878 ber_sockbuf_free( c->c_sb );
879 if ( sd != AC_SOCKET_INVALID ) {
880 slapd_remove( sd, 1, 0, 1 );
882 Statslog( LDAP_DEBUG_STATS, (close_reason
883 ? "conn=%lu fd=%ld closed (%s)\n"
884 : "conn=%lu fd=%ld closed\n"),
885 connid, (long) sd, close_reason, 0, 0 );
890 c->c_sb = ber_sockbuf_alloc( );
893 ber_len_t max = sockbuf_max_incoming;
894 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
897 c->c_conn_state = SLAP_C_INVALID;
898 c->c_struct_state = SLAP_C_UNUSED;
899 c->c_close_reason = "?"; /* should never be needed */
902 /* call destructors, then constructors; avoids unnecessary allocation */
903 if ( slapi_plugins_used ) {
904 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
909 int connection_state_closing( Connection *c )
911 /* c_mutex must be locked by caller */
915 assert( c->c_struct_state == SLAP_C_USED );
917 state = c->c_conn_state;
919 assert( state != SLAP_C_INVALID );
921 return state == SLAP_C_CLOSING;
924 static void connection_abandon( Connection *c )
926 /* c_mutex must be locked by caller */
928 Operation *o, *next, op = {0};
934 op.o_connid = c->c_connid;
935 op.o_tag = LDAP_REQ_ABANDON;
937 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
938 next = LDAP_STAILQ_NEXT( o, o_next );
939 op.orn_msgid = o->o_msgid;
941 op.o_bd = frontendDB;
942 frontendDB->be_abandon( &op, &rs );
946 /* remove operations in pending transaction */
947 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
948 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
949 LDAP_STAILQ_NEXT(o, o_next) = NULL;
953 /* clear transaction */
954 c->c_txn_backend = NULL;
955 c->c_txn = CONN_TXN_INACTIVE;
958 /* remove pending operations */
959 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
960 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
961 LDAP_STAILQ_NEXT(o, o_next) = NULL;
966 void connection_closing( Connection *c, const char *why )
968 assert( connections != NULL );
970 assert( c->c_struct_state == SLAP_C_USED );
971 assert( c->c_conn_state != SLAP_C_INVALID );
973 /* c_mutex must be locked by caller */
975 if( c->c_conn_state != SLAP_C_CLOSING ) {
978 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
979 Debug( LDAP_DEBUG_TRACE,
980 "connection_closing: readying conn=%lu sd=%d for close\n",
981 c->c_connid, sd, 0 );
982 /* update state to closing */
983 c->c_conn_state = SLAP_C_CLOSING;
984 c->c_close_reason = why;
986 /* don't listen on this port anymore */
987 slapd_clr_read( sd, 1 );
989 /* abandon active operations */
990 connection_abandon( c );
992 /* wake write blocked operations */
993 slapd_clr_write( sd, 1 );
994 if ( c->c_writewaiter ) {
995 ldap_pvt_thread_cond_signal( &c->c_write_cv );
996 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
997 ldap_pvt_thread_yield();
998 ldap_pvt_thread_mutex_lock( &c->c_mutex );
1001 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
1002 /* Client closed connection after doing Unbind. */
1003 c->c_close_reason = NULL;
1007 static void connection_close( Connection *c )
1011 assert( connections != NULL );
1012 assert( c != NULL );
1013 assert( c->c_struct_state == SLAP_C_USED );
1014 assert( c->c_conn_state == SLAP_C_CLOSING );
1016 /* note: connections_mutex and c_mutex should be locked by caller */
1018 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
1019 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
1020 Debug( LDAP_DEBUG_TRACE,
1021 "connection_close: deferring conn=%lu sd=%d\n",
1022 c->c_connid, sd, 0 );
1026 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
1027 c->c_connid, sd, 0 );
1028 connection_destroy( c );
1031 unsigned long connections_nextid(void)
1034 assert( connections != NULL );
1036 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1040 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
1045 Connection* connection_first( ber_socket_t *index )
1047 #ifdef SLAP_MULTI_CONN_ARRAY
1051 assert( connections != NULL );
1052 assert( index != NULL );
1054 #ifdef SLAP_MULTI_CONN_ARRAY
1055 for ( conn_array_id = 0;
1056 conn_array_id < NUM_CONNECTION_ARRAY;
1059 ldap_pvt_thread_mutex_lock( &connections_mutex[ conn_array_id ] );
1062 ldap_pvt_thread_mutex_lock( &connections_mutex );
1067 return connection_next(NULL, index);
1070 Connection* connection_next( Connection *c, ber_socket_t *index )
1071 #ifdef SLAP_MULTI_CONN_ARRAY
1075 assert( connections != NULL );
1076 assert( index != NULL );
1077 assert( *index >= 0 && *index <= dtblsize );
1079 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1083 for(; *index < dtblsize; (*index)++) {
1084 assert( MCA_conn_check( *index ) );
1085 conn = MCA_GET_CONNECTION(*index);
1086 if( conn->c_struct_state == SLAP_C_UNINITIALIZED ) {
1087 assert( conn->c_conn_state == SLAP_C_INVALID );
1088 #ifndef HAVE_WINSOCK
1095 if( conn->c_struct_state == SLAP_C_USED ) {
1096 assert( conn->c_conn_state != SLAP_C_INVALID );
1102 assert( conn->c_struct_state == SLAP_C_UNUSED );
1103 assert( conn->c_conn_state == SLAP_C_INVALID );
1106 if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
1113 assert( connections != NULL );
1114 assert( index != NULL );
1115 assert( *index <= dtblsize );
1117 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1121 for(; *index < dtblsize; (*index)++) {
1122 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
1123 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1124 #ifndef HAVE_WINSOCK
1131 if( connections[*index].c_struct_state == SLAP_C_USED ) {
1132 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
1133 c = &connections[(*index)++];
1137 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
1138 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1141 if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
1146 void connection_done( Connection *c )
1148 #ifdef SLAP_MULTI_CONN_ARRAY
1152 assert( connections != NULL );
1154 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1156 #ifdef SLAP_MULTI_CONN_ARRAY
1157 for ( conn_array_id = 0;
1158 conn_array_id < NUM_CONNECTION_ARRAY;
1161 ldap_pvt_thread_mutex_unlock( &connections_mutex[ conn_array_id ] );
1164 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1169 * connection_activity - handle the request operation op on connection
1170 * conn. This routine figures out what kind of operation it is and
1171 * calls the appropriate stub to handle it.
1174 #ifdef SLAPD_MONITOR
1175 /* FIXME: returns 0 in case of failure */
1176 #define INCR_OP_INITIATED(index) \
1178 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1179 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
1180 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1182 #define INCR_OP_COMPLETED(index) \
1184 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1185 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1186 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
1187 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1189 #else /* !SLAPD_MONITOR */
1190 #define INCR_OP_INITIATED(index) do { } while (0)
1191 #define INCR_OP_COMPLETED(index) \
1193 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1194 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1195 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1197 #endif /* !SLAPD_MONITOR */
1200 * NOTE: keep in sync with enum in slapd.h
1202 static int (*opfun[])( Operation *op, SlapReply *rs ) = {
1217 connection_operation( void *ctx, void *arg_v )
1219 int rc = LDAP_OTHER;
1220 Operation *op = arg_v;
1221 SlapReply rs = {REP_RESULT};
1222 ber_tag_t tag = op->o_tag;
1224 Connection *conn = op->o_conn;
1225 void *memctx = NULL;
1226 void *memctx_null = NULL;
1229 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
1230 /* FIXME: returns 0 in case of failure */
1231 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
1232 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1234 op->o_threadctx = ctx;
1235 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1239 case LDAP_REQ_UNBIND:
1241 case LDAP_REQ_DELETE:
1242 case LDAP_REQ_MODDN:
1243 case LDAP_REQ_MODIFY:
1244 case LDAP_REQ_COMPARE:
1245 case LDAP_REQ_SEARCH:
1246 case LDAP_REQ_ABANDON:
1247 case LDAP_REQ_EXTENDED:
1250 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1251 "conn %lu unknown LDAP request 0x%lx\n",
1252 conn->c_connid, tag, 0 );
1253 op->o_tag = LBER_ERROR;
1254 rs.sr_err = LDAP_PROTOCOL_ERROR;
1255 rs.sr_text = "unknown LDAP request";
1256 send_ldap_disconnect( op, &rs );
1257 rc = SLAPD_DISCONNECT;
1258 goto operations_error;
1261 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1262 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1263 "error: SASL bind in progress (tag=%ld).\n",
1265 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1266 "SASL bind in progress" );
1267 rc = LDAP_OPERATIONS_ERROR;
1268 goto operations_error;
1272 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1273 ( tag == LDAP_REQ_ADD ) ||
1274 ( tag == LDAP_REQ_DELETE ) ||
1275 ( tag == LDAP_REQ_MODIFY ) ||
1276 ( tag == LDAP_REQ_MODRDN )))
1278 /* Disable SLAB allocator for all update operations
1279 issued inside of a transaction */
1280 op->o_tmpmemctx = NULL;
1281 op->o_tmpmfuncs = &ch_mfuncs;
1285 /* We can use Thread-Local storage for most mallocs. We can
1286 * also use TL for ber parsing, but not on Add or Modify.
1289 memsiz = ber_len( op->o_ber ) * 64;
1290 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1292 memsiz = SLAP_SLAB_SIZE;
1294 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
1295 op->o_tmpmemctx = memctx;
1296 op->o_tmpmfuncs = &slap_sl_mfuncs;
1297 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1298 /* Note - the ber and its buffer are already allocated from
1299 * regular memory; this only affects subsequent mallocs that
1300 * ber_scanf may invoke.
1302 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1308 opidx = SLAP_OP_BIND;
1311 case LDAP_REQ_UNBIND:
1312 opidx = SLAP_OP_UNBIND;
1316 opidx = SLAP_OP_ADD;
1319 case LDAP_REQ_DELETE:
1320 opidx = SLAP_OP_DELETE;
1323 case LDAP_REQ_MODRDN:
1324 opidx = SLAP_OP_MODRDN;
1327 case LDAP_REQ_MODIFY:
1328 opidx = SLAP_OP_MODIFY;
1331 case LDAP_REQ_COMPARE:
1332 opidx = SLAP_OP_COMPARE;
1335 case LDAP_REQ_SEARCH:
1336 opidx = SLAP_OP_SEARCH;
1339 case LDAP_REQ_ABANDON:
1340 opidx = SLAP_OP_ABANDON;
1343 case LDAP_REQ_EXTENDED:
1344 opidx = SLAP_OP_EXTENDED;
1352 assert( opidx > -1 );
1353 INCR_OP_INITIATED( opidx );
1354 rc = (*(opfun[opidx]))( op, &rs );
1357 if ( rc == SLAPD_DISCONNECT ) {
1360 } else if ( opidx > -1 ) {
1361 /* increment completed operations count
1362 * only if operation was initiated
1363 * and rc != SLAPD_DISCONNECT */
1364 INCR_OP_COMPLETED( opidx );
1367 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1368 if ( rc == SLAPD_ABANDON ) {
1369 op->o_cancel = SLAP_CANCEL_ACK;
1371 op->o_cancel = LDAP_TOO_LATE;
1374 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1375 op->o_cancel != SLAP_CANCEL_DONE )
1377 ldap_pvt_thread_yield();
1380 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1382 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1384 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1385 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1387 conn->c_n_ops_executing--;
1388 conn->c_n_ops_completed++;
1392 case LDAP_REQ_UNBIND:
1393 /* c_mutex is locked */
1394 connection_closing( conn,
1395 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1399 connection_resched( conn );
1400 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1404 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1406 int connection_client_setup(
1408 ldap_pvt_thread_start_t *func,
1414 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1415 CONN_IS_CLIENT, 0, NULL );
1416 if ( rc < 0 ) return -1;
1418 c = connection_get( s );
1419 c->c_clientfunc = func;
1420 c->c_clientarg = arg;
1422 slapd_add_internal( s, 0 );
1423 slapd_set_read( s, 1 );
1424 connection_return( c );
1428 void connection_client_enable(
1431 slapd_set_read( s, 1 );
1434 void connection_client_stop(
1439 /* get (locked) connection */
1440 c = connection_get( s );
1442 assert( c->c_conn_state == SLAP_C_CLIENT );
1444 c->c_listener = NULL;
1445 c->c_conn_state = SLAP_C_INVALID;
1446 c->c_struct_state = SLAP_C_UNUSED;
1447 c->c_close_reason = "?"; /* should never be needed */
1449 ber_sockbuf_free( c->c_sb );
1450 slapd_remove( s, 0, 1, 1 );
1451 c->c_sb = ber_sockbuf_alloc( );
1453 ber_len_t max = sockbuf_max_incoming;
1454 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1457 connection_return( c );
1460 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1462 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1464 static void* connection_read_thread( void* ctx, void* argv )
1467 conn_readinfo cri = { NULL, NULL, NULL, 0 };
1468 ber_socket_t s = (long)argv;
1471 * read incoming LDAP requests. If there is more than one,
1472 * the first one is returned with new_op
1474 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1475 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1476 return (void*)(long)rc;
1479 /* execute a single queued request in the same thread */
1480 if( cri.op && !cri.nullop ) {
1481 rc = (long)connection_operation( ctx, cri.op );
1482 } else if ( cri.func ) {
1483 rc = (long)cri.func( ctx, cri.arg );
1486 return (void*)(long)rc;
1489 int connection_read_activate( ber_socket_t s )
1494 * suspend reading on this file descriptor until a connection processing
1495 * thread reads data on it. Otherwise the listener thread will repeatedly
1496 * submit the same event on it to the pool.
1498 rc = slapd_clr_read( s, 0 );
1502 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1503 connection_read_thread, (void *)(long)s );
1506 Debug( LDAP_DEBUG_ANY,
1507 "connection_read_activate(%d): submit failed (%d)\n",
1515 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1517 connection_read( ber_socket_t s, conn_readinfo *cri )
1519 int connection_read(ber_socket_t s)
1525 assert( connections != NULL );
1527 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
1529 /* get (locked) connection */
1530 c = connection_get( s );
1533 Debug( LDAP_DEBUG_ANY,
1534 "connection_read(%ld): no connection!\n",
1537 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1543 if( c->c_conn_state == SLAP_C_CLOSING ) {
1544 Debug( LDAP_DEBUG_TRACE,
1545 "connection_read(%d): closing, ignoring input for id=%lu\n",
1546 s, c->c_connid, 0 );
1548 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1549 slapd_set_read( s, 1 );
1551 connection_return( c );
1552 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1556 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1557 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1558 cri->func = c->c_clientfunc;
1559 cri->arg = c->c_clientarg;
1560 /* read should already be cleared */
1562 slapd_clr_read( s, 0 );
1563 ldap_pvt_thread_pool_submit( &connection_pool,
1564 c->c_clientfunc, c->c_clientarg );
1566 connection_return( c );
1567 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1571 Debug( LDAP_DEBUG_TRACE,
1572 "connection_read(%d): checking for input on id=%lu\n",
1573 s, c->c_connid, 0 );
1576 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1577 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1579 Debug( LDAP_DEBUG_TRACE,
1580 "connection_read(%d): TLS accept failure "
1581 "error=%d id=%lu, closing\n",
1582 s, rc, c->c_connid );
1584 c->c_needs_tls_accept = 0;
1585 /* connections_mutex and c_mutex are locked */
1586 connection_closing( c, "TLS negotiation failure" );
1592 /* Drain input before close, to allow SSL error codes
1593 * to propagate to client. */
1599 rc = select(s+1, &rfd, NULL, NULL, &tv);
1601 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1607 connection_close( c );
1608 connection_return( c );
1609 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1612 } else if ( rc == 0 ) {
1614 struct berval authid = BER_BVNULL;
1616 c->c_needs_tls_accept = 0;
1618 /* we need to let SASL know */
1619 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1621 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1622 if( c->c_tls_ssf > c->c_ssf ) {
1623 c->c_ssf = c->c_tls_ssf;
1626 rc = dnX509peerNormalize( ssl, &authid );
1627 if ( rc != LDAP_SUCCESS ) {
1628 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1629 "unable to get TLS client DN, error=%d id=%lu\n",
1630 s, rc, c->c_connid );
1632 Statslog( LDAP_DEBUG_STATS,
1633 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1634 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1635 slap_sasl_external( c, c->c_tls_ssf, &authid );
1636 if ( authid.bv_val ) free( authid.bv_val );
1639 /* if success and data is ready, fall thru to data input loop */
1641 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1643 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1644 slapd_set_read( s, 1 );
1647 connection_return( c );
1648 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1654 #ifdef HAVE_CYRUS_SASL
1655 if ( c->c_sasl_layers ) {
1656 /* If previous layer is not removed yet, give up for now */
1657 if ( !c->c_sasl_sockctx ) {
1658 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1659 slapd_set_read( s, 1 );
1662 connection_return( c );
1663 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1667 c->c_sasl_layers = 0;
1669 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1670 if( rc != LDAP_SUCCESS ) {
1671 Debug( LDAP_DEBUG_TRACE,
1672 "connection_read(%d): SASL install error "
1673 "error=%d id=%lu, closing\n",
1674 s, rc, c->c_connid );
1676 /* connections_mutex and c_mutex are locked */
1677 connection_closing( c, "SASL layer install failure" );
1678 connection_close( c );
1679 connection_return( c );
1680 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1686 #define CONNECTION_INPUT_LOOP 1
1687 /* #define DATA_READY_LOOP 1 */
1690 /* How do we do this without getting into a busy loop ? */
1691 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1692 rc = connection_input( c, cri );
1694 rc = connection_input( c );
1697 #ifdef DATA_READY_LOOP
1698 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1699 #elif CONNECTION_INPUT_LOOP
1706 Debug( LDAP_DEBUG_TRACE,
1707 "connection_read(%d): input error=%d id=%lu, closing.\n",
1708 s, rc, c->c_connid );
1710 /* connections_mutex and c_mutex are locked */
1711 connection_closing( c, conn_lost_str );
1712 connection_close( c );
1713 connection_return( c );
1714 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1718 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1719 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1720 slapd_set_write( s, 0 );
1723 slapd_set_read( s, 1 );
1725 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1726 slapd_set_read( s, 1 );
1729 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1730 slapd_set_write( s, 1 );
1734 connection_return( c );
1735 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1741 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1742 connection_input( Connection *conn , conn_readinfo *cri )
1744 connection_input( Connection *conn )
1753 #ifdef LDAP_CONNECTIONLESS
1759 if ( conn->c_currentber == NULL &&
1760 ( conn->c_currentber = ber_alloc()) == NULL )
1762 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1768 #ifdef LDAP_CONNECTIONLESS
1769 if ( conn->c_is_udp ) {
1770 char peername[sizeof("IP=255.255.255.255:65336")];
1772 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1773 if (len != sizeof(struct sockaddr)) return 1;
1775 sprintf( peername, "IP=%s:%d",
1776 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1777 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1778 Statslog( LDAP_DEBUG_STATS,
1779 "conn=%lu UDP request from %s (%s) accepted.\n",
1780 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1784 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1785 if ( tag != LDAP_TAG_MESSAGE ) {
1789 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1791 Debug( LDAP_DEBUG_TRACE,
1792 "ber_get_next on fd %d failed errno=%d (%s)\n",
1793 sd, err, sock_errstr(err) );
1794 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1795 /* log, close and send error */
1796 ber_free( conn->c_currentber, 1 );
1797 conn->c_currentber = NULL;
1804 ber = conn->c_currentber;
1805 conn->c_currentber = NULL;
1807 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1808 /* log, close and send error */
1809 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1814 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1815 /* log, close and send error */
1816 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1822 #ifdef LDAP_CONNECTIONLESS
1823 if( conn->c_is_udp ) {
1824 if( tag == LBER_OCTETSTRING ) {
1825 ber_get_stringa( ber, &cdn );
1826 tag = ber_peek_tag(ber, &len);
1828 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1829 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1836 if(tag == LDAP_REQ_BIND) {
1837 /* immediately abandon all existing operations upon BIND */
1838 connection_abandon( conn );
1841 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1844 /* clear state if the connection is being reused from inactive */
1845 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1846 memset( &conn->c_pagedresults_state, 0,
1847 sizeof( conn->c_pagedresults_state ) );
1850 op->o_res_ber = NULL;
1852 #ifdef LDAP_CONNECTIONLESS
1853 if (conn->c_is_udp) {
1855 ber_str2bv( cdn, 0, 1, &op->o_dn );
1856 op->o_protocol = LDAP_VERSION2;
1858 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1859 if (op->o_res_ber == NULL) return 1;
1861 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1862 sizeof(struct sockaddr), 0 );
1864 if (rc != sizeof(struct sockaddr)) {
1865 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1869 if (op->o_protocol == LDAP_VERSION2) {
1870 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1872 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1877 #endif /* LDAP_CONNECTIONLESS */
1881 /* Don't process requests when the conn is in the middle of a
1882 * Bind, or if it's closing. Also, don't let any single conn
1883 * use up all the available threads, and don't execute if we're
1884 * currently blocked on output. And don't execute if there are
1885 * already pending ops, let them go first. Abandon operations
1886 * get exceptions to some, but not all, cases.
1890 /* Abandon and Unbind are exempt from these checks */
1891 if (conn->c_conn_state == SLAP_C_CLOSING) {
1894 } else if (conn->c_writewaiter) {
1895 defer = "awaiting write";
1897 } else if (conn->c_n_ops_pending) {
1898 defer = "pending operations";
1902 case LDAP_REQ_ABANDON:
1903 /* Unbind is exempt from these checks */
1904 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1905 defer = "too many executing";
1907 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1912 case LDAP_REQ_UNBIND:
1917 int max = conn->c_dn.bv_len
1918 ? slap_conn_max_pending_auth
1919 : slap_conn_max_pending;
1921 Debug( LDAP_DEBUG_ANY,
1922 "connection_input: conn=%lu deferring operation: %s\n",
1923 conn->c_connid, defer, 0 );
1924 conn->c_n_ops_pending++;
1925 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1926 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1929 conn->c_n_ops_executing++;
1931 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1933 * The first op will be processed in the same thread context,
1934 * as long as there is only one op total.
1935 * Subsequent ops will be submitted to the pool by
1936 * calling connection_op_activate()
1938 if ( cri->op == NULL ) {
1939 /* the first incoming request */
1940 connection_op_queue( op );
1943 if ( !cri->nullop ) {
1945 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1946 connection_operation, (void *) cri->op );
1948 connection_op_activate( op );
1951 connection_op_activate( op );
1956 if ( conn->c_struct_state != SLAP_C_USED ) {
1957 /* connection must have got closed underneath us */
1962 assert( conn->c_struct_state == SLAP_C_USED );
1967 connection_resched( Connection *conn )
1971 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1974 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1976 /* use trylock to avoid possible deadlock */
1977 rc = ldap_pvt_thread_mutex_trylock( MCA_GET_CONN_MUTEX( sd ) );
1980 Debug( LDAP_DEBUG_TRACE,
1981 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1982 conn->c_connid, sd, 0 );
1984 * reaquire locks in the right order...
1985 * this may allow another thread to close this connection,
1986 * so recheck state below.
1988 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1989 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX ( sd ) );
1990 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1993 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1994 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1995 "closed by other thread conn=%lu sd=%d\n",
1996 conn->c_connid, sd, 0 );
1998 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1999 "attempting closing conn=%lu sd=%d\n",
2000 conn->c_connid, sd, 0 );
2001 connection_close( conn );
2004 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( sd ) );
2008 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
2009 /* other states need different handling */
2013 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
2014 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
2016 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
2017 LDAP_STAILQ_NEXT(op, o_next) = NULL;
2019 /* pending operations should not be marked for abandonment */
2020 assert(!op->o_abandon);
2022 conn->c_n_ops_pending--;
2023 conn->c_n_ops_executing++;
2025 connection_op_activate( op );
2027 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
2033 connection_init_log_prefix( Operation *op )
2035 if ( op->o_connid == (unsigned long)(-1) ) {
2036 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
2037 "conn=-1 op=%lu", op->o_opid );
2040 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
2041 "conn=%lu op=%lu", op->o_connid, op->o_opid );
2045 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
2047 op->o_conn->c_sasl_bindop = NULL;
2049 return SLAP_CB_CONTINUE;
2052 static int connection_bind_cb( Operation *op, SlapReply *rs )
2054 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
2055 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
2056 op->o_conn->c_sasl_bind_in_progress =
2057 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
2059 /* Moved here from bind.c due to ITS#4158 */
2060 op->o_conn->c_sasl_bindop = NULL;
2061 if ( op->orb_method == LDAP_AUTH_SASL ) {
2062 if( rs->sr_err == LDAP_SUCCESS ) {
2063 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
2064 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
2065 /* edn is always normalized already */
2066 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
2068 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
2069 BER_BVZERO( &op->orb_edn );
2070 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
2071 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
2073 op->o_conn->c_sasl_ssf = op->orb_ssf;
2074 if( op->orb_ssf > op->o_conn->c_ssf ) {
2075 op->o_conn->c_ssf = op->orb_ssf;
2078 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
2079 ber_len_t max = sockbuf_max_incoming_auth;
2080 ber_sockbuf_ctrl( op->o_conn->c_sb,
2081 LBER_SB_OPT_SET_MAX_INCOMING, &max );
2084 /* log authorization identity */
2085 Statslog( LDAP_DEBUG_STATS,
2086 "%s BIND dn=\"%s\" mech=%s ssf=%d\n",
2088 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
2089 op->o_conn->c_authmech.bv_val, op->orb_ssf, 0 );
2091 Debug( LDAP_DEBUG_TRACE,
2092 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
2093 op->o_conn->c_authmech.bv_val,
2094 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
2097 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
2098 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
2099 free( op->o_conn->c_sasl_bind_mech.bv_val );
2100 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
2104 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
2106 ch_free( op->o_callback );
2107 op->o_callback = NULL;
2109 return SLAP_CB_CONTINUE;
2112 static void connection_op_queue( Operation *op )
2114 ber_tag_t tag = op->o_tag;
2116 if (tag == LDAP_REQ_BIND) {
2117 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
2118 sc->sc_response = connection_bind_cb;
2119 sc->sc_cleanup = connection_bind_cleanup_cb;
2120 sc->sc_next = op->o_callback;
2121 op->o_callback = sc;
2122 op->o_conn->c_conn_state = SLAP_C_BINDING;
2125 if (!op->o_dn.bv_len) {
2126 op->o_authz = op->o_conn->c_authz;
2127 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
2128 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
2129 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
2131 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
2132 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
2136 op->o_authtype = op->o_conn->c_authtype;
2137 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
2139 if (!op->o_protocol) {
2140 op->o_protocol = op->o_conn->c_protocol
2141 ? op->o_conn->c_protocol : LDAP_VERSION3;
2144 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
2145 op->o_protocol > LDAP_VERSION2)
2147 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
2150 op->o_connid = op->o_conn->c_connid;
2151 connection_init_log_prefix( op );
2153 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
2156 static int connection_op_activate( Operation *op )
2160 connection_op_queue( op );
2162 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2163 connection_operation, (void *) op );
2166 Debug( LDAP_DEBUG_ANY,
2167 "connection_op_activate: submit failed (%d) for conn=%lu\n",
2168 rc, op->o_connid, 0 );
2169 /* should move op to pending list */
2175 int connection_write(ber_socket_t s)
2180 assert( connections != NULL );
2182 slapd_clr_write( s, 0 );
2184 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX( s ) );
2186 c = connection_get( s );
2188 Debug( LDAP_DEBUG_ANY,
2189 "connection_write(%ld): no connection!\n",
2191 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( s ) );
2197 Debug( LDAP_DEBUG_TRACE,
2198 "connection_write(%d): waking output for id=%lu\n",
2199 s, c->c_connid, 0 );
2200 ldap_pvt_thread_cond_signal( &c->c_write_cv );
2202 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
2203 slapd_set_read( s, 1 );
2205 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
2206 slapd_set_write( s, 1 );
2209 /* If there are ops pending because of a writewaiter,
2212 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
2213 if ( !c->c_writewaiter ) break;
2214 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
2216 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
2217 LDAP_STAILQ_NEXT(op, o_next) = NULL;
2219 /* pending operations should not be marked for abandonment */
2220 assert(!op->o_abandon);
2222 c->c_n_ops_pending--;
2223 c->c_n_ops_executing++;
2225 connection_op_activate( op );
2230 connection_return( c );
2231 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
2236 connection_fake_init(
2241 conn->c_connid = -1;
2242 conn->c_send_ldap_result = slap_send_ldap_result;
2243 conn->c_send_search_entry = slap_send_search_entry;
2244 conn->c_send_search_reference = slap_send_search_reference;
2245 conn->c_listener = (Listener *)&dummy_list;
2246 conn->c_peer_domain = slap_empty_bv;
2247 conn->c_peer_name = slap_empty_bv;
2249 memset(op, 0, OPERATION_BUFFER_SIZE);
2250 op->o_hdr = (Opheader *)(op+1);
2251 op->o_controls = (void **)(op->o_hdr+1);
2252 /* set memory context */
2253 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
2254 op->o_tmpmfuncs = &slap_sl_mfuncs;
2255 op->o_threadctx = ctx;
2256 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
2259 op->o_connid = op->o_conn->c_connid;
2260 connection_init_log_prefix( op );
2263 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
2264 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
2265 #endif /* LDAP_SLAPI */
2267 slap_op_time( &op->o_time, &op->o_tincr );
2271 connection_assign_nextid( Connection *conn )
2273 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2274 conn->c_connid = conn_nextid++;
2275 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );