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);
445 for(i=0; i<dtblsize; i++) {
446 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
447 assert( connections[i].c_conn_state == SLAP_C_INVALID );
448 assert( connections[i].c_sb == 0 );
452 ber_sockbuf_ctrl( connections[i].c_sb,
453 LBER_SB_OPT_GET_FD, &sd );
455 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
456 assert( connections[i].c_conn_state == SLAP_C_INVALID );
457 assert( sd == AC_SOCKET_INVALID );
461 /* state can actually change from used -> unused by resched,
462 * so don't assert details here.
476 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
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);
623 c->c_txn = CONN_TXN_INACTIVE;
624 c->c_txn_backend = NULL;
625 LDAP_STAILQ_INIT(&c->c_txn_ops);
628 BER_BVZERO( &c->c_sasl_bind_mech );
630 c->c_sasl_authctx = NULL;
631 c->c_sasl_sockctx = NULL;
632 c->c_sasl_extra = NULL;
633 c->c_sasl_bindop = NULL;
635 c->c_sb = ber_sockbuf_alloc( );
638 ber_len_t max = sockbuf_max_incoming;
639 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
642 c->c_currentber = NULL;
644 /* should check status of thread calls */
645 ldap_pvt_thread_mutex_init( &c->c_mutex );
646 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
647 ldap_pvt_thread_cond_init( &c->c_write_cv );
650 if ( slapi_plugins_used ) {
651 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
655 c->c_struct_state = SLAP_C_UNUSED;
658 ldap_pvt_thread_mutex_lock( &c->c_mutex );
660 assert( c->c_struct_state == SLAP_C_UNUSED );
661 assert( BER_BVISNULL( &c->c_authmech ) );
662 assert( BER_BVISNULL( &c->c_dn ) );
663 assert( BER_BVISNULL( &c->c_ndn ) );
664 assert( c->c_listener == NULL );
665 assert( BER_BVISNULL( &c->c_peer_domain ) );
666 assert( BER_BVISNULL( &c->c_peer_name ) );
667 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
668 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
670 assert( c->c_txn == CONN_TXN_INACTIVE );
671 assert( c->c_txn_backend == NULL );
672 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
674 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
675 assert( c->c_sasl_done == 0 );
676 assert( c->c_sasl_authctx == NULL );
677 assert( c->c_sasl_sockctx == NULL );
678 assert( c->c_sasl_extra == NULL );
679 assert( c->c_sasl_bindop == NULL );
680 assert( c->c_currentber == NULL );
681 assert( c->c_writewaiter == 0);
683 c->c_listener = listener;
685 if ( flags == CONN_IS_CLIENT ) {
686 c->c_conn_state = SLAP_C_CLIENT;
687 c->c_struct_state = SLAP_C_USED;
688 c->c_close_reason = "?"; /* should never be needed */
689 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
690 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
691 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
696 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
697 ber_str2bv( peername, 0, 1, &c->c_peer_name );
699 c->c_n_ops_received = 0;
700 c->c_n_ops_executing = 0;
701 c->c_n_ops_pending = 0;
702 c->c_n_ops_completed = 0;
708 /* set to zero until bind, implies LDAP_VERSION3 */
711 #ifndef SLAPD_MONITOR
712 if ( global_idletimeout > 0 )
713 #endif /* ! SLAPD_MONITOR */
715 c->c_activitytime = c->c_starttime = slap_get_time();
718 #ifdef LDAP_CONNECTIONLESS
720 if( flags == CONN_IS_UDP ) {
723 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
724 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
726 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
727 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
728 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
729 LBER_SBIOD_LEVEL_PROVIDER, NULL );
734 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
735 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
737 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
738 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
742 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
743 INT_MAX, (void*)"ldap_" );
746 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
747 c /* non-NULL */ ) < 0 )
749 Debug( LDAP_DEBUG_ANY,
750 "connection_init(%d, %s): set nonblocking failed\n",
751 s, c->c_peer_name.bv_val, 0 );
754 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
755 id = c->c_connid = conn_nextid++;
756 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
758 c->c_conn_state = SLAP_C_INACTIVE;
759 c->c_struct_state = SLAP_C_USED;
760 c->c_close_reason = "?"; /* should never be needed */
762 c->c_ssf = c->c_transport_ssf = ssf;
766 if ( flags == CONN_IS_TLS ) {
768 c->c_needs_tls_accept = 1;
771 c->c_needs_tls_accept = 0;
775 slap_sasl_open( c, 0 );
776 slap_sasl_external( c, ssf, authid );
778 slapd_add_internal( s, 1 );
779 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
780 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
782 backend_connection_init(c);
787 void connection2anonymous( Connection *c )
789 assert( connections != NULL );
793 ber_len_t max = sockbuf_max_incoming;
794 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
797 if ( !BER_BVISNULL( &c->c_authmech ) ) {
798 ch_free(c->c_authmech.bv_val);
800 BER_BVZERO( &c->c_authmech );
802 if ( !BER_BVISNULL( &c->c_dn ) ) {
803 ch_free(c->c_dn.bv_val);
805 BER_BVZERO( &c->c_dn );
807 if ( !BER_BVISNULL( &c->c_ndn ) ) {
808 ch_free(c->c_ndn.bv_val);
810 BER_BVZERO( &c->c_ndn );
812 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
813 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
815 BER_BVZERO( &c->c_sasl_authz_dn );
817 c->c_authz_backend = NULL;
821 connection_destroy( Connection *c )
823 /* note: connections_mutex should be locked by caller */
825 unsigned long connid;
826 const char *close_reason;
828 assert( connections != NULL );
830 assert( c->c_struct_state != SLAP_C_UNUSED );
831 assert( c->c_conn_state != SLAP_C_INVALID );
832 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
833 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
835 assert( c->c_txn == CONN_TXN_INACTIVE );
836 assert( c->c_txn_backend == NULL );
837 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
839 assert( c->c_writewaiter == 0);
841 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
842 connid = c->c_connid;
843 close_reason = c->c_close_reason;
845 backend_connection_destroy(c);
850 c->c_activitytime = c->c_starttime = 0;
852 connection2anonymous( c );
853 c->c_listener = NULL;
855 if(c->c_peer_domain.bv_val != NULL) {
856 free(c->c_peer_domain.bv_val);
858 BER_BVZERO( &c->c_peer_domain );
859 if(c->c_peer_name.bv_val != NULL) {
860 free(c->c_peer_name.bv_val);
862 BER_BVZERO( &c->c_peer_name );
864 c->c_sasl_bind_in_progress = 0;
865 if(c->c_sasl_bind_mech.bv_val != NULL) {
866 free(c->c_sasl_bind_mech.bv_val);
868 BER_BVZERO( &c->c_sasl_bind_mech );
870 slap_sasl_close( c );
872 if ( c->c_currentber != NULL ) {
873 ber_free( c->c_currentber, 1 );
874 c->c_currentber = NULL;
877 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
879 ber_sockbuf_free( c->c_sb );
880 if ( sd != AC_SOCKET_INVALID ) {
881 slapd_remove( sd, 1, 0, 1 );
883 Statslog( LDAP_DEBUG_STATS, (close_reason
884 ? "conn=%lu fd=%ld closed (%s)\n"
885 : "conn=%lu fd=%ld closed\n"),
886 connid, (long) sd, close_reason, 0, 0 );
891 c->c_sb = ber_sockbuf_alloc( );
894 ber_len_t max = sockbuf_max_incoming;
895 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
898 c->c_conn_state = SLAP_C_INVALID;
899 c->c_struct_state = SLAP_C_UNUSED;
900 c->c_close_reason = "?"; /* should never be needed */
903 /* call destructors, then constructors; avoids unnecessary allocation */
904 if ( slapi_plugins_used ) {
905 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
910 int connection_state_closing( Connection *c )
912 /* c_mutex must be locked by caller */
916 assert( c->c_struct_state == SLAP_C_USED );
918 state = c->c_conn_state;
920 assert( state != SLAP_C_INVALID );
922 return state == SLAP_C_CLOSING;
925 static void connection_abandon( Connection *c )
927 /* c_mutex must be locked by caller */
929 Operation *o, *next, op = {0};
935 op.o_connid = c->c_connid;
936 op.o_tag = LDAP_REQ_ABANDON;
938 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
939 next = LDAP_STAILQ_NEXT( o, o_next );
940 op.orn_msgid = o->o_msgid;
942 op.o_bd = frontendDB;
943 frontendDB->be_abandon( &op, &rs );
947 /* remove operations in pending transaction */
948 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
949 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
950 LDAP_STAILQ_NEXT(o, o_next) = NULL;
954 /* clear transaction */
955 c->c_txn_backend = NULL;
956 c->c_txn = CONN_TXN_INACTIVE;
959 /* remove pending operations */
960 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
961 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
962 LDAP_STAILQ_NEXT(o, o_next) = NULL;
967 void connection_closing( Connection *c, const char *why )
969 assert( connections != NULL );
971 assert( c->c_struct_state == SLAP_C_USED );
972 assert( c->c_conn_state != SLAP_C_INVALID );
974 /* c_mutex must be locked by caller */
976 if( c->c_conn_state != SLAP_C_CLOSING ) {
979 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
980 Debug( LDAP_DEBUG_TRACE,
981 "connection_closing: readying conn=%lu sd=%d for close\n",
982 c->c_connid, sd, 0 );
983 /* update state to closing */
984 c->c_conn_state = SLAP_C_CLOSING;
985 c->c_close_reason = why;
987 /* don't listen on this port anymore */
988 slapd_clr_read( sd, 1 );
990 /* abandon active operations */
991 connection_abandon( c );
993 /* wake write blocked operations */
994 slapd_clr_write( sd, 1 );
995 if ( c->c_writewaiter ) {
996 ldap_pvt_thread_cond_signal( &c->c_write_cv );
997 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
998 ldap_pvt_thread_yield();
999 ldap_pvt_thread_mutex_lock( &c->c_mutex );
1002 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
1003 /* Client closed connection after doing Unbind. */
1004 c->c_close_reason = NULL;
1008 static void connection_close( Connection *c )
1012 assert( connections != NULL );
1013 assert( c != NULL );
1014 assert( c->c_struct_state == SLAP_C_USED );
1015 assert( c->c_conn_state == SLAP_C_CLOSING );
1017 /* note: connections_mutex and c_mutex should be locked by caller */
1019 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
1020 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
1021 Debug( LDAP_DEBUG_TRACE,
1022 "connection_close: deferring conn=%lu sd=%d\n",
1023 c->c_connid, sd, 0 );
1027 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
1028 c->c_connid, sd, 0 );
1029 connection_destroy( c );
1032 unsigned long connections_nextid(void)
1035 assert( connections != NULL );
1037 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1041 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
1046 Connection* connection_first( ber_socket_t *index )
1048 #ifdef SLAP_MULTI_CONN_ARRAY
1052 assert( connections != NULL );
1053 assert( index != NULL );
1055 #ifdef SLAP_MULTI_CONN_ARRAY
1056 for ( conn_array_id = 0;
1057 conn_array_id < NUM_CONNECTION_ARRAY;
1060 ldap_pvt_thread_mutex_lock( &connections_mutex[ conn_array_id ] );
1063 ldap_pvt_thread_mutex_lock( &connections_mutex );
1068 return connection_next(NULL, index);
1071 Connection* connection_next( Connection *c, ber_socket_t *index )
1072 #ifdef SLAP_MULTI_CONN_ARRAY
1076 assert( connections != NULL );
1077 assert( index != NULL );
1078 assert( *index >= 0 && *index <= dtblsize );
1080 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1084 for(; *index < dtblsize; (*index)++) {
1085 assert( MCA_conn_check( *index ) );
1086 conn = MCA_GET_CONNECTION(*index);
1087 if( conn->c_struct_state == SLAP_C_UNINITIALIZED ) {
1088 assert( conn->c_conn_state == SLAP_C_INVALID );
1089 #ifndef HAVE_WINSOCK
1096 if( conn->c_struct_state == SLAP_C_USED ) {
1097 assert( conn->c_conn_state != SLAP_C_INVALID );
1103 assert( conn->c_struct_state == SLAP_C_UNUSED );
1104 assert( conn->c_conn_state == SLAP_C_INVALID );
1107 if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
1114 assert( connections != NULL );
1115 assert( index != NULL );
1116 assert( *index <= dtblsize );
1118 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1122 for(; *index < dtblsize; (*index)++) {
1123 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
1124 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1125 #ifndef HAVE_WINSOCK
1132 if( connections[*index].c_struct_state == SLAP_C_USED ) {
1133 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
1134 c = &connections[(*index)++];
1138 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
1139 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1142 if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
1147 void connection_done( Connection *c )
1149 #ifdef SLAP_MULTI_CONN_ARRAY
1153 assert( connections != NULL );
1155 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1157 #ifdef SLAP_MULTI_CONN_ARRAY
1158 for ( conn_array_id = 0;
1159 conn_array_id < NUM_CONNECTION_ARRAY;
1162 ldap_pvt_thread_mutex_unlock( &connections_mutex[ conn_array_id ] );
1165 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1170 * connection_activity - handle the request operation op on connection
1171 * conn. This routine figures out what kind of operation it is and
1172 * calls the appropriate stub to handle it.
1175 #ifdef SLAPD_MONITOR
1176 /* FIXME: returns 0 in case of failure */
1177 #define INCR_OP_INITIATED(index) \
1179 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1180 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
1181 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1183 #define INCR_OP_COMPLETED(index) \
1185 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1186 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1187 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
1188 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1190 #else /* !SLAPD_MONITOR */
1191 #define INCR_OP_INITIATED(index) do { } while (0)
1192 #define INCR_OP_COMPLETED(index) \
1194 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1195 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1196 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1198 #endif /* !SLAPD_MONITOR */
1201 * NOTE: keep in sync with enum in slapd.h
1203 static int (*opfun[])( Operation *op, SlapReply *rs ) = {
1218 connection_operation( void *ctx, void *arg_v )
1220 int rc = LDAP_OTHER;
1221 Operation *op = arg_v;
1222 SlapReply rs = {REP_RESULT};
1223 ber_tag_t tag = op->o_tag;
1225 Connection *conn = op->o_conn;
1226 void *memctx = NULL;
1227 void *memctx_null = NULL;
1230 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
1231 /* FIXME: returns 0 in case of failure */
1232 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
1233 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1235 op->o_threadctx = ctx;
1236 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1240 case LDAP_REQ_UNBIND:
1242 case LDAP_REQ_DELETE:
1243 case LDAP_REQ_MODDN:
1244 case LDAP_REQ_MODIFY:
1245 case LDAP_REQ_COMPARE:
1246 case LDAP_REQ_SEARCH:
1247 case LDAP_REQ_ABANDON:
1248 case LDAP_REQ_EXTENDED:
1251 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1252 "conn %lu unknown LDAP request 0x%lx\n",
1253 conn->c_connid, tag, 0 );
1254 op->o_tag = LBER_ERROR;
1255 rs.sr_err = LDAP_PROTOCOL_ERROR;
1256 rs.sr_text = "unknown LDAP request";
1257 send_ldap_disconnect( op, &rs );
1258 rc = SLAPD_DISCONNECT;
1259 goto operations_error;
1262 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1263 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1264 "error: SASL bind in progress (tag=%ld).\n",
1266 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1267 "SASL bind in progress" );
1268 rc = LDAP_OPERATIONS_ERROR;
1269 goto operations_error;
1273 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1274 ( tag == LDAP_REQ_ADD ) ||
1275 ( tag == LDAP_REQ_DELETE ) ||
1276 ( tag == LDAP_REQ_MODIFY ) ||
1277 ( tag == LDAP_REQ_MODRDN )))
1279 /* Disable SLAB allocator for all update operations
1280 issued inside of a transaction */
1281 op->o_tmpmemctx = NULL;
1282 op->o_tmpmfuncs = &ch_mfuncs;
1286 /* We can use Thread-Local storage for most mallocs. We can
1287 * also use TL for ber parsing, but not on Add or Modify.
1290 memsiz = ber_len( op->o_ber ) * 64;
1291 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1293 memsiz = SLAP_SLAB_SIZE;
1295 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
1296 op->o_tmpmemctx = memctx;
1297 op->o_tmpmfuncs = &slap_sl_mfuncs;
1298 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1299 /* Note - the ber and its buffer are already allocated from
1300 * regular memory; this only affects subsequent mallocs that
1301 * ber_scanf may invoke.
1303 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1309 opidx = SLAP_OP_BIND;
1312 case LDAP_REQ_UNBIND:
1313 opidx = SLAP_OP_UNBIND;
1317 opidx = SLAP_OP_ADD;
1320 case LDAP_REQ_DELETE:
1321 opidx = SLAP_OP_DELETE;
1324 case LDAP_REQ_MODRDN:
1325 opidx = SLAP_OP_MODRDN;
1328 case LDAP_REQ_MODIFY:
1329 opidx = SLAP_OP_MODIFY;
1332 case LDAP_REQ_COMPARE:
1333 opidx = SLAP_OP_COMPARE;
1336 case LDAP_REQ_SEARCH:
1337 opidx = SLAP_OP_SEARCH;
1340 case LDAP_REQ_ABANDON:
1341 opidx = SLAP_OP_ABANDON;
1344 case LDAP_REQ_EXTENDED:
1345 opidx = SLAP_OP_EXTENDED;
1353 assert( opidx > -1 );
1354 INCR_OP_INITIATED( opidx );
1355 rc = (*(opfun[opidx]))( op, &rs );
1358 if ( rc == SLAPD_DISCONNECT ) {
1361 } else if ( opidx > -1 ) {
1362 /* increment completed operations count
1363 * only if operation was initiated
1364 * and rc != SLAPD_DISCONNECT */
1365 INCR_OP_COMPLETED( opidx );
1368 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1369 if ( rc == SLAPD_ABANDON ) {
1370 op->o_cancel = SLAP_CANCEL_ACK;
1372 op->o_cancel = LDAP_TOO_LATE;
1375 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1376 op->o_cancel != SLAP_CANCEL_DONE )
1378 ldap_pvt_thread_yield();
1381 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1383 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1385 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1386 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1388 conn->c_n_ops_executing--;
1389 conn->c_n_ops_completed++;
1393 case LDAP_REQ_UNBIND:
1394 /* c_mutex is locked */
1395 connection_closing( conn,
1396 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1400 connection_resched( conn );
1401 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1405 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1407 int connection_client_setup(
1409 ldap_pvt_thread_start_t *func,
1415 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1416 CONN_IS_CLIENT, 0, NULL );
1417 if ( rc < 0 ) return -1;
1419 c = connection_get( s );
1420 c->c_clientfunc = func;
1421 c->c_clientarg = arg;
1423 slapd_add_internal( s, 0 );
1424 slapd_set_read( s, 1 );
1425 connection_return( c );
1429 void connection_client_enable(
1432 slapd_set_read( s, 1 );
1435 void connection_client_stop(
1440 /* get (locked) connection */
1441 c = connection_get( s );
1443 assert( c->c_conn_state == SLAP_C_CLIENT );
1445 c->c_listener = NULL;
1446 c->c_conn_state = SLAP_C_INVALID;
1447 c->c_struct_state = SLAP_C_UNUSED;
1448 c->c_close_reason = "?"; /* should never be needed */
1450 ber_sockbuf_free( c->c_sb );
1451 slapd_remove( s, 0, 1, 1 );
1452 c->c_sb = ber_sockbuf_alloc( );
1454 ber_len_t max = sockbuf_max_incoming;
1455 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1458 connection_return( c );
1461 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1463 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1465 static void* connection_read_thread( void* ctx, void* argv )
1468 conn_readinfo cri = { NULL, NULL, NULL, 0 };
1469 ber_socket_t s = (long)argv;
1472 * read incoming LDAP requests. If there is more than one,
1473 * the first one is returned with new_op
1475 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1476 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1477 return (void*)(long)rc;
1480 /* execute a single queued request in the same thread */
1481 if( cri.op && !cri.nullop ) {
1482 rc = (long)connection_operation( ctx, cri.op );
1483 } else if ( cri.func ) {
1484 rc = (long)cri.func( ctx, cri.arg );
1487 return (void*)(long)rc;
1490 int connection_read_activate( ber_socket_t s )
1495 * suspend reading on this file descriptor until a connection processing
1496 * thread reads data on it. Otherwise the listener thread will repeatedly
1497 * submit the same event on it to the pool.
1499 rc = slapd_clr_read( s, 0 );
1503 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1504 connection_read_thread, (void *)(long)s );
1507 Debug( LDAP_DEBUG_ANY,
1508 "connection_read_activate(%d): submit failed (%d)\n",
1516 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1518 connection_read( ber_socket_t s, conn_readinfo *cri )
1520 int connection_read(ber_socket_t s)
1526 assert( connections != NULL );
1528 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
1530 /* get (locked) connection */
1531 c = connection_get( s );
1534 Debug( LDAP_DEBUG_ANY,
1535 "connection_read(%ld): no connection!\n",
1538 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1544 if( c->c_conn_state == SLAP_C_CLOSING ) {
1545 Debug( LDAP_DEBUG_TRACE,
1546 "connection_read(%d): closing, ignoring input for id=%lu\n",
1547 s, c->c_connid, 0 );
1549 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1550 slapd_set_read( s, 1 );
1552 connection_return( c );
1553 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1557 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1558 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1559 cri->func = c->c_clientfunc;
1560 cri->arg = c->c_clientarg;
1561 /* read should already be cleared */
1563 slapd_clr_read( s, 0 );
1564 ldap_pvt_thread_pool_submit( &connection_pool,
1565 c->c_clientfunc, c->c_clientarg );
1567 connection_return( c );
1568 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1572 Debug( LDAP_DEBUG_TRACE,
1573 "connection_read(%d): checking for input on id=%lu\n",
1574 s, c->c_connid, 0 );
1577 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1578 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1580 Debug( LDAP_DEBUG_TRACE,
1581 "connection_read(%d): TLS accept failure "
1582 "error=%d id=%lu, closing\n",
1583 s, rc, c->c_connid );
1585 c->c_needs_tls_accept = 0;
1586 /* connections_mutex and c_mutex are locked */
1587 connection_closing( c, "TLS negotiation failure" );
1593 /* Drain input before close, to allow SSL error codes
1594 * to propagate to client. */
1600 rc = select(s+1, &rfd, NULL, NULL, &tv);
1602 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1608 connection_close( c );
1609 connection_return( c );
1610 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1613 } else if ( rc == 0 ) {
1615 struct berval authid = BER_BVNULL;
1617 c->c_needs_tls_accept = 0;
1619 /* we need to let SASL know */
1620 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1622 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1623 if( c->c_tls_ssf > c->c_ssf ) {
1624 c->c_ssf = c->c_tls_ssf;
1627 rc = dnX509peerNormalize( ssl, &authid );
1628 if ( rc != LDAP_SUCCESS ) {
1629 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1630 "unable to get TLS client DN, error=%d id=%lu\n",
1631 s, rc, c->c_connid );
1633 Statslog( LDAP_DEBUG_STATS,
1634 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1635 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1636 slap_sasl_external( c, c->c_tls_ssf, &authid );
1637 if ( authid.bv_val ) free( authid.bv_val );
1640 /* if success and data is ready, fall thru to data input loop */
1642 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1644 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1645 slapd_set_read( s, 1 );
1648 connection_return( c );
1649 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1655 #ifdef HAVE_CYRUS_SASL
1656 if ( c->c_sasl_layers ) {
1657 /* If previous layer is not removed yet, give up for now */
1658 if ( !c->c_sasl_sockctx ) {
1659 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1660 slapd_set_read( s, 1 );
1663 connection_return( c );
1664 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1668 c->c_sasl_layers = 0;
1670 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1671 if( rc != LDAP_SUCCESS ) {
1672 Debug( LDAP_DEBUG_TRACE,
1673 "connection_read(%d): SASL install error "
1674 "error=%d id=%lu, closing\n",
1675 s, rc, c->c_connid );
1677 /* connections_mutex and c_mutex are locked */
1678 connection_closing( c, "SASL layer install failure" );
1679 connection_close( c );
1680 connection_return( c );
1681 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1687 #define CONNECTION_INPUT_LOOP 1
1688 /* #define DATA_READY_LOOP 1 */
1691 /* How do we do this without getting into a busy loop ? */
1692 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1693 rc = connection_input( c, cri );
1695 rc = connection_input( c );
1698 #ifdef DATA_READY_LOOP
1699 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1700 #elif CONNECTION_INPUT_LOOP
1707 Debug( LDAP_DEBUG_TRACE,
1708 "connection_read(%d): input error=%d id=%lu, closing.\n",
1709 s, rc, c->c_connid );
1711 /* connections_mutex and c_mutex are locked */
1712 connection_closing( c, conn_lost_str );
1713 connection_close( c );
1714 connection_return( c );
1715 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1719 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1720 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1721 slapd_set_write( s, 0 );
1724 slapd_set_read( s, 1 );
1726 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1727 slapd_set_read( s, 1 );
1730 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1731 slapd_set_write( s, 1 );
1735 connection_return( c );
1736 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1742 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1743 connection_input( Connection *conn , conn_readinfo *cri )
1745 connection_input( Connection *conn )
1754 #ifdef LDAP_CONNECTIONLESS
1760 if ( conn->c_currentber == NULL &&
1761 ( conn->c_currentber = ber_alloc()) == NULL )
1763 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1769 #ifdef LDAP_CONNECTIONLESS
1770 if ( conn->c_is_udp ) {
1771 char peername[sizeof("IP=255.255.255.255:65336")];
1773 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1774 if (len != sizeof(struct sockaddr)) return 1;
1776 sprintf( peername, "IP=%s:%d",
1777 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1778 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1779 Statslog( LDAP_DEBUG_STATS,
1780 "conn=%lu UDP request from %s (%s) accepted.\n",
1781 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1785 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1786 if ( tag != LDAP_TAG_MESSAGE ) {
1790 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1792 Debug( LDAP_DEBUG_TRACE,
1793 "ber_get_next on fd %d failed errno=%d (%s)\n",
1794 sd, err, sock_errstr(err) );
1795 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1796 /* log, close and send error */
1797 ber_free( conn->c_currentber, 1 );
1798 conn->c_currentber = NULL;
1805 ber = conn->c_currentber;
1806 conn->c_currentber = NULL;
1808 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1809 /* log, close and send error */
1810 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1815 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1816 /* log, close and send error */
1817 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1823 #ifdef LDAP_CONNECTIONLESS
1824 if( conn->c_is_udp ) {
1825 if( tag == LBER_OCTETSTRING ) {
1826 ber_get_stringa( ber, &cdn );
1827 tag = ber_peek_tag(ber, &len);
1829 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1830 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1837 if(tag == LDAP_REQ_BIND) {
1838 /* immediately abandon all existing operations upon BIND */
1839 connection_abandon( conn );
1842 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1845 /* clear state if the connection is being reused from inactive */
1846 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1847 memset( &conn->c_pagedresults_state, 0,
1848 sizeof( conn->c_pagedresults_state ) );
1851 op->o_res_ber = NULL;
1853 #ifdef LDAP_CONNECTIONLESS
1854 if (conn->c_is_udp) {
1856 ber_str2bv( cdn, 0, 1, &op->o_dn );
1857 op->o_protocol = LDAP_VERSION2;
1859 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1860 if (op->o_res_ber == NULL) return 1;
1862 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1863 sizeof(struct sockaddr), 0 );
1865 if (rc != sizeof(struct sockaddr)) {
1866 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1870 if (op->o_protocol == LDAP_VERSION2) {
1871 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1873 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1878 #endif /* LDAP_CONNECTIONLESS */
1882 /* Don't process requests when the conn is in the middle of a
1883 * Bind, or if it's closing. Also, don't let any single conn
1884 * use up all the available threads, and don't execute if we're
1885 * currently blocked on output. And don't execute if there are
1886 * already pending ops, let them go first. Abandon operations
1887 * get exceptions to some, but not all, cases.
1891 /* Abandon and Unbind are exempt from these checks */
1892 if (conn->c_conn_state == SLAP_C_CLOSING) {
1895 } else if (conn->c_writewaiter) {
1896 defer = "awaiting write";
1898 } else if (conn->c_n_ops_pending) {
1899 defer = "pending operations";
1903 case LDAP_REQ_ABANDON:
1904 /* Unbind is exempt from these checks */
1905 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1906 defer = "too many executing";
1908 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1913 case LDAP_REQ_UNBIND:
1918 int max = conn->c_dn.bv_len
1919 ? slap_conn_max_pending_auth
1920 : slap_conn_max_pending;
1922 Debug( LDAP_DEBUG_ANY,
1923 "connection_input: conn=%lu deferring operation: %s\n",
1924 conn->c_connid, defer, 0 );
1925 conn->c_n_ops_pending++;
1926 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1927 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1930 conn->c_n_ops_executing++;
1932 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1934 * The first op will be processed in the same thread context,
1935 * as long as there is only one op total.
1936 * Subsequent ops will be submitted to the pool by
1937 * calling connection_op_activate()
1939 if ( cri->op == NULL ) {
1940 /* the first incoming request */
1941 connection_op_queue( op );
1944 if ( !cri->nullop ) {
1946 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1947 connection_operation, (void *) cri->op );
1949 connection_op_activate( op );
1952 connection_op_activate( op );
1957 if ( conn->c_struct_state != SLAP_C_USED ) {
1958 /* connection must have got closed underneath us */
1963 assert( conn->c_struct_state == SLAP_C_USED );
1968 connection_resched( Connection *conn )
1972 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1975 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1977 /* use trylock to avoid possible deadlock */
1978 rc = ldap_pvt_thread_mutex_trylock( MCA_GET_CONN_MUTEX( sd ) );
1981 Debug( LDAP_DEBUG_TRACE,
1982 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1983 conn->c_connid, sd, 0 );
1985 * reaquire locks in the right order...
1986 * this may allow another thread to close this connection,
1987 * so recheck state below.
1989 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1990 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX ( sd ) );
1991 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1994 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1995 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1996 "closed by other thread conn=%lu sd=%d\n",
1997 conn->c_connid, sd, 0 );
1999 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
2000 "attempting closing conn=%lu sd=%d\n",
2001 conn->c_connid, sd, 0 );
2002 connection_close( conn );
2005 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( sd ) );
2009 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
2010 /* other states need different handling */
2014 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
2015 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
2017 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
2018 LDAP_STAILQ_NEXT(op, o_next) = NULL;
2020 /* pending operations should not be marked for abandonment */
2021 assert(!op->o_abandon);
2023 conn->c_n_ops_pending--;
2024 conn->c_n_ops_executing++;
2026 connection_op_activate( op );
2028 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
2034 connection_init_log_prefix( Operation *op )
2036 if ( op->o_connid == (unsigned long)(-1) ) {
2037 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
2038 "conn=-1 op=%lu", op->o_opid );
2041 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
2042 "conn=%lu op=%lu", op->o_connid, op->o_opid );
2046 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
2048 op->o_conn->c_sasl_bindop = NULL;
2050 return SLAP_CB_CONTINUE;
2053 static int connection_bind_cb( Operation *op, SlapReply *rs )
2055 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
2056 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
2057 op->o_conn->c_sasl_bind_in_progress =
2058 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
2060 /* Moved here from bind.c due to ITS#4158 */
2061 op->o_conn->c_sasl_bindop = NULL;
2062 if ( op->orb_method == LDAP_AUTH_SASL ) {
2063 if( rs->sr_err == LDAP_SUCCESS ) {
2064 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
2065 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
2066 /* edn is always normalized already */
2067 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
2069 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
2070 BER_BVZERO( &op->orb_edn );
2071 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
2072 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
2074 op->o_conn->c_sasl_ssf = op->orb_ssf;
2075 if( op->orb_ssf > op->o_conn->c_ssf ) {
2076 op->o_conn->c_ssf = op->orb_ssf;
2079 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
2080 ber_len_t max = sockbuf_max_incoming_auth;
2081 ber_sockbuf_ctrl( op->o_conn->c_sb,
2082 LBER_SB_OPT_SET_MAX_INCOMING, &max );
2085 /* log authorization identity */
2086 Statslog( LDAP_DEBUG_STATS,
2087 "%s BIND dn=\"%s\" mech=%s ssf=%d\n",
2089 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
2090 op->o_conn->c_authmech.bv_val, op->orb_ssf, 0 );
2092 Debug( LDAP_DEBUG_TRACE,
2093 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
2094 op->o_conn->c_authmech.bv_val,
2095 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
2098 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
2099 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
2100 free( op->o_conn->c_sasl_bind_mech.bv_val );
2101 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
2105 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
2107 ch_free( op->o_callback );
2108 op->o_callback = NULL;
2110 return SLAP_CB_CONTINUE;
2113 static void connection_op_queue( Operation *op )
2115 ber_tag_t tag = op->o_tag;
2117 if (tag == LDAP_REQ_BIND) {
2118 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
2119 sc->sc_response = connection_bind_cb;
2120 sc->sc_cleanup = connection_bind_cleanup_cb;
2121 sc->sc_next = op->o_callback;
2122 op->o_callback = sc;
2123 op->o_conn->c_conn_state = SLAP_C_BINDING;
2126 if (!op->o_dn.bv_len) {
2127 op->o_authz = op->o_conn->c_authz;
2128 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
2129 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
2130 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
2132 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
2133 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
2137 op->o_authtype = op->o_conn->c_authtype;
2138 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
2140 if (!op->o_protocol) {
2141 op->o_protocol = op->o_conn->c_protocol
2142 ? op->o_conn->c_protocol : LDAP_VERSION3;
2145 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
2146 op->o_protocol > LDAP_VERSION2)
2148 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
2151 op->o_connid = op->o_conn->c_connid;
2152 connection_init_log_prefix( op );
2154 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
2157 static int connection_op_activate( Operation *op )
2161 connection_op_queue( op );
2163 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2164 connection_operation, (void *) op );
2167 Debug( LDAP_DEBUG_ANY,
2168 "connection_op_activate: submit failed (%d) for conn=%lu\n",
2169 rc, op->o_connid, 0 );
2170 /* should move op to pending list */
2176 int connection_write(ber_socket_t s)
2181 assert( connections != NULL );
2183 slapd_clr_write( s, 0 );
2185 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX( s ) );
2187 c = connection_get( s );
2189 Debug( LDAP_DEBUG_ANY,
2190 "connection_write(%ld): no connection!\n",
2192 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( s ) );
2198 Debug( LDAP_DEBUG_TRACE,
2199 "connection_write(%d): waking output for id=%lu\n",
2200 s, c->c_connid, 0 );
2201 ldap_pvt_thread_cond_signal( &c->c_write_cv );
2203 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
2204 slapd_set_read( s, 1 );
2206 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
2207 slapd_set_write( s, 1 );
2210 /* If there are ops pending because of a writewaiter,
2213 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
2214 if ( !c->c_writewaiter ) break;
2215 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
2217 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
2218 LDAP_STAILQ_NEXT(op, o_next) = NULL;
2220 /* pending operations should not be marked for abandonment */
2221 assert(!op->o_abandon);
2223 c->c_n_ops_pending--;
2224 c->c_n_ops_executing++;
2226 connection_op_activate( op );
2231 connection_return( c );
2232 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
2237 connection_fake_init(
2242 conn->c_connid = -1;
2243 conn->c_send_ldap_result = slap_send_ldap_result;
2244 conn->c_send_search_entry = slap_send_search_entry;
2245 conn->c_send_search_reference = slap_send_search_reference;
2246 conn->c_listener = (Listener *)&dummy_list;
2247 conn->c_peer_domain = slap_empty_bv;
2248 conn->c_peer_name = slap_empty_bv;
2250 memset(op, 0, OPERATION_BUFFER_SIZE);
2251 op->o_hdr = (Opheader *)(op+1);
2252 op->o_controls = (void **)(op->o_hdr+1);
2253 /* set memory context */
2254 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
2255 op->o_tmpmfuncs = &slap_sl_mfuncs;
2256 op->o_threadctx = ctx;
2257 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
2260 op->o_connid = op->o_conn->c_connid;
2261 connection_init_log_prefix( op );
2264 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
2265 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
2266 #endif /* LDAP_SLAPI */
2268 slap_op_time( &op->o_time, &op->o_tincr );
2272 connection_assign_nextid( Connection *conn )
2274 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2275 conn->c_connid = conn_nextid++;
2276 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );