2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
33 #include <ac/socket.h>
35 #include <ac/string.h>
37 #include <ac/unistd.h>
43 #include "slapi/slapi.h"
46 #ifdef SLAP_MULTI_CONN_ARRAY
47 /* for Multiple Connection Arrary (MCA) Support */
48 static ldap_pvt_thread_mutex_t* connections_mutex;
49 static Connection **connections = NULL;
51 /* set to the number of processors (round up to a power of 2) */
52 # define NUM_CONNECTION_ARRAY 4
54 /* partition the array in a modulo manner */
55 # define MCA_conn_array_id(fd) ((int)(fd)%NUM_CONNECTION_ARRAY)
56 # define MCA_conn_array_element_id(fd) ((int)(fd)/NUM_CONNECTION_ARRAY)
57 # define MCA_ARRAY_SIZE ((int)(MCA_conn_array_element_id(dtblsize) + (MCA_conn_array_id(dtblsize) ? 1 : 0)))
58 # define MCA_conn_check(fd) (dtblsize > 0 && (fd) >= 0 && (fd) < (MCA_ARRAY_SIZE*NUM_CONNECTION_ARRAY))
59 # define MCA_GET_CONNECTION(fd) (&(connections[MCA_conn_array_id(fd)]) \
60 [MCA_conn_array_element_id(fd)])
61 # define MCA_GET_CONN_MUTEX(fd) (&connections_mutex[MCA_conn_array_id(fd)])
64 /* protected by connections_mutex */
65 static ldap_pvt_thread_mutex_t connections_mutex;
66 static Connection *connections = NULL;
68 # define MCA_conn_check(fd) (dtblsize > 0 && (fd) < dtblsize)
69 # define MCA_GET_CONNECTION(fd) (&connections[s])
70 # define MCA_GET_CONN_MUTEX(fd) (&connections_mutex)
73 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
74 static unsigned long conn_nextid = 0;
76 static const char conn_lost_str[] = "connection lost";
78 /* structure state (protected by connections_mutex) */
79 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
80 #define SLAP_C_UNUSED 0x01
81 #define SLAP_C_USED 0x02
83 /* connection state (protected by c_mutex ) */
84 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
85 #define SLAP_C_INACTIVE 0x01 /* zero threads */
86 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
87 #define SLAP_C_BINDING 0x03 /* binding */
88 #define SLAP_C_CLOSING 0x04 /* closing */
89 #define SLAP_C_CLIENT 0x05 /* outbound client conn */
92 connection_state2str( int state )
95 case SLAP_C_INVALID: return "!";
96 case SLAP_C_INACTIVE: return "|";
97 case SLAP_C_ACTIVE: return "";
98 case SLAP_C_BINDING: return "B";
99 case SLAP_C_CLOSING: return "C";
100 case SLAP_C_CLIENT: return "L";
106 static Connection* connection_get( ber_socket_t s );
108 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
110 typedef struct conn_readinfo {
112 ldap_pvt_thread_start_t *func;
117 static int connection_input( Connection *c, conn_readinfo *cri );
119 static int connection_input( Connection *c );
121 static void connection_close( Connection *c );
123 static int connection_op_activate( Operation *op );
124 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
125 static void connection_op_queue( Operation *op );
127 static int connection_resched( Connection *conn );
128 static void connection_abandon( Connection *conn );
129 static void connection_destroy( Connection *c );
131 static ldap_pvt_thread_start_t connection_operation;
134 * Initialize connection management infrastructure.
136 int connections_init(void)
137 #ifdef SLAP_MULTI_CONN_ARRAY
142 assert( connections == NULL );
144 if( connections != NULL) {
145 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
150 connections_mutex = (ldap_pvt_thread_mutex_t*) ch_calloc(
151 NUM_CONNECTION_ARRAY, sizeof(ldap_pvt_thread_mutex_t) );
152 if( connections_mutex == NULL ) {
153 Debug( LDAP_DEBUG_ANY, "connections_init: "
154 "allocation of connection mutexes failed\n", 0, 0, 0 );
158 connections = (Connection**) ch_calloc(
159 NUM_CONNECTION_ARRAY, sizeof(Connection*));
160 if( connections == NULL ) {
161 Debug( LDAP_DEBUG_ANY, "connections_init: "
162 "allocation of connection[%d] failed\n", 0, 0, 0 );
166 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
167 ldap_pvt_thread_mutex_init( connections_mutex+i );
168 connections[i] = (Connection*) ch_calloc(
169 MCA_ARRAY_SIZE, sizeof(Connection) );
170 if( connections[i] == NULL ) {
171 Debug( LDAP_DEBUG_ANY, "connections_init: "
172 "allocation (%d*%ld) of connection array[%d] failed\n",
173 dtblsize, (long) sizeof(Connection), i );
178 /* should check return of every call */
179 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
181 assert( connections[0]->c_struct_state == SLAP_C_UNINITIALIZED );
182 assert( connections[NUM_CONNECTION_ARRAY-1]->c_struct_state ==
183 SLAP_C_UNINITIALIZED );
185 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
186 conn = connections[i];
187 for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) {
188 conn[j].c_conn_idx = j;
193 * per entry initialization of the Connection array initialization
194 * will be done by connection_init()
203 assert( connections == NULL );
205 if( connections != NULL) {
206 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
211 /* should check return of every call */
212 ldap_pvt_thread_mutex_init( &connections_mutex );
213 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
215 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
217 if( connections == NULL ) {
218 Debug( LDAP_DEBUG_ANY, "connections_init: "
219 "allocation (%d*%ld) of connection array failed\n",
220 dtblsize, (long) sizeof(Connection), 0 );
224 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
225 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
227 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
230 * per entry initialization of the Connection array initialization
231 * will be done by connection_init()
239 * Destroy connection management infrastructure.
242 int connections_destroy(void)
243 #ifdef SLAP_MULTI_CONN_ARRAY
248 if( connections == NULL) {
249 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
254 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
255 Connection* conn = connections[i];
256 for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) {
257 if( conn[j].c_struct_state != SLAP_C_UNINITIALIZED ) {
258 ber_sockbuf_free( conn[j].c_sb );
259 ldap_pvt_thread_mutex_destroy( &conn[j].c_mutex );
260 ldap_pvt_thread_mutex_destroy( &conn[j].c_write_mutex );
261 ldap_pvt_thread_cond_destroy( &conn[j].c_write_cv );
264 if ( slapi_plugins_used ) {
265 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
273 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
274 free( connections[i] );
275 connections[i] = NULL;
276 ldap_pvt_thread_mutex_destroy( &connections_mutex[i] );
280 free( connections_mutex );
282 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
291 /* should check return of every call */
293 if( connections == NULL) {
294 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
299 for ( i = 0; i < dtblsize; i++ ) {
300 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
301 ber_sockbuf_free( connections[i].c_sb );
302 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
303 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
304 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
306 if ( slapi_plugins_used ) {
307 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
317 ldap_pvt_thread_mutex_destroy( &connections_mutex );
318 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
324 * shutdown all connections
326 int connections_shutdown(void)
327 #ifdef SLAP_MULTI_CONN_ARRAY
332 for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
333 Connection* conn = connections[i];
334 ldap_pvt_thread_mutex_lock( &connections_mutex[i] );
335 for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) {
336 if( conn[j].c_struct_state != SLAP_C_USED ) {
339 /* give persistent clients a chance to cleanup */
340 if( conn[j].c_conn_state == SLAP_C_CLIENT ) {
341 ldap_pvt_thread_pool_submit( &connection_pool,
342 conn[j].c_clientfunc, conn[j].c_clientarg );
346 ldap_pvt_thread_mutex_lock( &conn[j].c_mutex );
347 /* connections_mutex and c_mutex are locked */
348 connection_closing( &conn[j], "connection shutdown" );
349 connection_close( &conn[j] );
350 ldap_pvt_thread_mutex_unlock( &conn[j].c_mutex );
353 ldap_pvt_thread_mutex_unlock( &connections_mutex[i] );
363 ldap_pvt_thread_mutex_lock( &connections_mutex );
365 for ( i = 0; i < dtblsize; i++ ) {
366 if( connections[i].c_struct_state != SLAP_C_USED ) {
369 /* give persistent clients a chance to cleanup */
370 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
371 ldap_pvt_thread_pool_submit( &connection_pool,
372 connections[i].c_clientfunc, connections[i].c_clientarg );
376 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
378 /* connections_mutex and c_mutex are locked */
379 connection_closing( &connections[i], "slapd shutdown" );
380 connection_close( &connections[i] );
382 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
385 ldap_pvt_thread_mutex_unlock( &connections_mutex );
392 * Timeout idle connections.
394 int connections_timeout_idle(time_t now)
400 for( c = connection_first( &connindex );
402 c = connection_next( c, &connindex ) )
404 /* Don't timeout a slow-running request or a persistent
405 * outbound connection */
406 if( c->c_n_ops_executing || c->c_conn_state == SLAP_C_CLIENT ) {
410 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
412 connection_closing( c, "idletimeout" );
413 connection_close( c );
417 connection_done( c );
422 static Connection* connection_get( ber_socket_t s )
424 /* connections_mutex should be locked by caller */
428 Debug( LDAP_DEBUG_ARGS,
429 "connection_get(%ld)\n",
432 assert( connections != NULL );
434 if(s == AC_SOCKET_INVALID) return NULL;
437 assert( MCA_conn_check( s ) );
438 c = MCA_GET_CONNECTION(s);
440 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
447 for(i=0; i<dtblsize; i++) {
448 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
449 assert( connections[i].c_conn_state == SLAP_C_INVALID );
450 assert( connections[i].c_sb == 0 );
454 ber_sockbuf_ctrl( connections[i].c_sb,
455 LBER_SB_OPT_GET_FD, &sd );
457 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
458 assert( connections[i].c_conn_state == SLAP_C_INVALID );
459 assert( sd == AC_SOCKET_INVALID );
463 /* state can actually change from used -> unused by resched,
464 * so don't assert details here.
478 ldap_pvt_thread_mutex_lock( &c->c_mutex );
480 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
481 if( c->c_struct_state != SLAP_C_USED ) {
482 /* connection must have been closed due to resched */
484 assert( c->c_conn_state == SLAP_C_INVALID );
485 assert( sd == AC_SOCKET_INVALID );
487 Debug( LDAP_DEBUG_TRACE,
488 "connection_get(%d): connection not used\n",
491 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
495 Debug( LDAP_DEBUG_TRACE,
496 "connection_get(%d): got connid=%lu\n",
501 assert( c->c_struct_state == SLAP_C_USED );
502 assert( c->c_conn_state != SLAP_C_INVALID );
503 assert( sd != AC_SOCKET_INVALID );
505 #ifndef SLAPD_MONITOR
506 if ( global_idletimeout > 0 )
507 #endif /* ! SLAPD_MONITOR */
509 c->c_activitytime = slap_get_time();
516 static void connection_return( Connection *c )
518 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
521 long connection_init(
525 const char* peername,
528 struct berval *authid )
533 assert( connections != NULL );
535 assert( listener != NULL );
536 assert( dnsname != NULL );
537 assert( peername != NULL );
540 assert( flags != CONN_IS_TLS );
543 if( s == AC_SOCKET_INVALID ) {
544 Debug( LDAP_DEBUG_ANY,
545 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
551 assert( s < dtblsize );
554 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
557 assert( MCA_conn_check( s ) );
558 c = MCA_GET_CONNECTION(s);
564 for( i=0; i < dtblsize; i++) {
567 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
568 assert( connections[i].c_sb == 0 );
573 sd = AC_SOCKET_INVALID;
574 if (connections[i].c_sb != NULL) {
575 ber_sockbuf_ctrl( connections[i].c_sb,
576 LBER_SB_OPT_GET_FD, &sd );
579 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
580 assert( sd == AC_SOCKET_INVALID );
585 if( connections[i].c_conn_state == SLAP_C_CLIENT ) continue;
587 assert( connections[i].c_struct_state == SLAP_C_USED );
588 assert( connections[i].c_conn_state != SLAP_C_INVALID );
589 assert( sd != AC_SOCKET_INVALID );
593 Debug( LDAP_DEBUG_ANY,
594 "connection_init(%d): connection table full "
595 "(%d/%d)\n", s, i, dtblsize);
596 ldap_pvt_thread_mutex_unlock( &connections_mutex );
604 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
605 c->c_send_ldap_result = slap_send_ldap_result;
606 c->c_send_search_entry = slap_send_search_entry;
607 c->c_send_search_reference = slap_send_search_reference;
608 c->c_send_ldap_extended = slap_send_ldap_extended;
609 #ifdef LDAP_RES_INTERMEDIATE
610 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
613 BER_BVZERO( &c->c_authmech );
614 BER_BVZERO( &c->c_dn );
615 BER_BVZERO( &c->c_ndn );
617 c->c_listener = NULL;
618 BER_BVZERO( &c->c_peer_domain );
619 BER_BVZERO( &c->c_peer_name );
621 LDAP_STAILQ_INIT(&c->c_ops);
622 LDAP_STAILQ_INIT(&c->c_pending_ops);
624 BER_BVZERO( &c->c_sasl_bind_mech );
626 c->c_sasl_authctx = NULL;
627 c->c_sasl_sockctx = NULL;
628 c->c_sasl_extra = NULL;
629 c->c_sasl_bindop = NULL;
631 c->c_sb = ber_sockbuf_alloc( );
634 ber_len_t max = sockbuf_max_incoming;
635 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
638 c->c_currentber = NULL;
640 /* should check status of thread calls */
641 ldap_pvt_thread_mutex_init( &c->c_mutex );
642 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
643 ldap_pvt_thread_cond_init( &c->c_write_cv );
646 if ( slapi_plugins_used ) {
647 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
651 c->c_struct_state = SLAP_C_UNUSED;
654 ldap_pvt_thread_mutex_lock( &c->c_mutex );
656 assert( c->c_struct_state == SLAP_C_UNUSED );
657 assert( BER_BVISNULL( &c->c_authmech ) );
658 assert( BER_BVISNULL( &c->c_dn ) );
659 assert( BER_BVISNULL( &c->c_ndn ) );
660 assert( c->c_listener == NULL );
661 assert( BER_BVISNULL( &c->c_peer_domain ) );
662 assert( BER_BVISNULL( &c->c_peer_name ) );
663 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
664 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
665 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
666 assert( c->c_sasl_done == 0 );
667 assert( c->c_sasl_authctx == NULL );
668 assert( c->c_sasl_sockctx == NULL );
669 assert( c->c_sasl_extra == NULL );
670 assert( c->c_sasl_bindop == NULL );
671 assert( c->c_currentber == NULL );
672 assert( c->c_writewaiter == 0);
674 c->c_listener = listener;
676 if ( flags == CONN_IS_CLIENT ) {
677 c->c_conn_state = SLAP_C_CLIENT;
678 c->c_struct_state = SLAP_C_USED;
679 c->c_close_reason = "?"; /* should never be needed */
680 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
681 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
682 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
687 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
688 ber_str2bv( peername, 0, 1, &c->c_peer_name );
690 c->c_n_ops_received = 0;
691 c->c_n_ops_executing = 0;
692 c->c_n_ops_pending = 0;
693 c->c_n_ops_completed = 0;
699 /* set to zero until bind, implies LDAP_VERSION3 */
702 #ifndef SLAPD_MONITOR
703 if ( global_idletimeout > 0 )
704 #endif /* ! SLAPD_MONITOR */
706 c->c_activitytime = c->c_starttime = slap_get_time();
709 #ifdef LDAP_CONNECTIONLESS
711 if( flags == CONN_IS_UDP ) {
714 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
715 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
717 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
718 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
719 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
720 LBER_SBIOD_LEVEL_PROVIDER, NULL );
725 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
726 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
728 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
729 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
733 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
734 INT_MAX, (void*)"ldap_" );
737 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
738 c /* non-NULL */ ) < 0 )
740 Debug( LDAP_DEBUG_ANY,
741 "connection_init(%d, %s): set nonblocking failed\n",
742 s, c->c_peer_name.bv_val, 0 );
745 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
746 id = c->c_connid = conn_nextid++;
747 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
749 c->c_conn_state = SLAP_C_INACTIVE;
750 c->c_struct_state = SLAP_C_USED;
751 c->c_close_reason = "?"; /* should never be needed */
753 c->c_ssf = c->c_transport_ssf = ssf;
757 if ( flags == CONN_IS_TLS ) {
759 c->c_needs_tls_accept = 1;
762 c->c_needs_tls_accept = 0;
766 slap_sasl_open( c, 0 );
767 slap_sasl_external( c, ssf, authid );
769 slapd_add_internal( s, 1 );
770 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
771 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
773 backend_connection_init(c);
778 void connection2anonymous( Connection *c )
780 assert( connections != NULL );
784 ber_len_t max = sockbuf_max_incoming;
785 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
788 if ( !BER_BVISNULL( &c->c_authmech ) ) {
789 ch_free(c->c_authmech.bv_val);
791 BER_BVZERO( &c->c_authmech );
793 if ( !BER_BVISNULL( &c->c_dn ) ) {
794 ch_free(c->c_dn.bv_val);
796 BER_BVZERO( &c->c_dn );
798 if ( !BER_BVISNULL( &c->c_ndn ) ) {
799 ch_free(c->c_ndn.bv_val);
801 BER_BVZERO( &c->c_ndn );
803 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
804 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
806 BER_BVZERO( &c->c_sasl_authz_dn );
808 c->c_authz_backend = NULL;
812 connection_destroy( Connection *c )
814 /* note: connections_mutex should be locked by caller */
816 unsigned long connid;
817 const char *close_reason;
819 assert( connections != NULL );
821 assert( c->c_struct_state != SLAP_C_UNUSED );
822 assert( c->c_conn_state != SLAP_C_INVALID );
823 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
824 assert( c->c_writewaiter == 0);
826 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
827 connid = c->c_connid;
828 close_reason = c->c_close_reason;
830 backend_connection_destroy(c);
835 c->c_activitytime = c->c_starttime = 0;
837 connection2anonymous( c );
838 c->c_listener = NULL;
840 if(c->c_peer_domain.bv_val != NULL) {
841 free(c->c_peer_domain.bv_val);
843 BER_BVZERO( &c->c_peer_domain );
844 if(c->c_peer_name.bv_val != NULL) {
845 free(c->c_peer_name.bv_val);
847 BER_BVZERO( &c->c_peer_name );
849 c->c_sasl_bind_in_progress = 0;
850 if(c->c_sasl_bind_mech.bv_val != NULL) {
851 free(c->c_sasl_bind_mech.bv_val);
853 BER_BVZERO( &c->c_sasl_bind_mech );
855 slap_sasl_close( c );
857 if ( c->c_currentber != NULL ) {
858 ber_free( c->c_currentber, 1 );
859 c->c_currentber = NULL;
862 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
864 ber_sockbuf_free( c->c_sb );
865 if ( sd != AC_SOCKET_INVALID ) {
866 slapd_remove( sd, 1, 0, 1 );
868 Statslog( LDAP_DEBUG_STATS, (close_reason
869 ? "conn=%lu fd=%ld closed (%s)\n"
870 : "conn=%lu fd=%ld closed\n"),
871 connid, (long) sd, close_reason, 0, 0 );
876 c->c_sb = ber_sockbuf_alloc( );
879 ber_len_t max = sockbuf_max_incoming;
880 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
883 c->c_conn_state = SLAP_C_INVALID;
884 c->c_struct_state = SLAP_C_UNUSED;
885 c->c_close_reason = "?"; /* should never be needed */
888 /* call destructors, then constructors; avoids unnecessary allocation */
889 if ( slapi_plugins_used ) {
890 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
895 int connection_state_closing( Connection *c )
897 /* c_mutex must be locked by caller */
901 assert( c->c_struct_state == SLAP_C_USED );
903 state = c->c_conn_state;
905 assert( state != SLAP_C_INVALID );
907 return state == SLAP_C_CLOSING;
910 static void connection_abandon( Connection *c )
912 /* c_mutex must be locked by caller */
914 Operation *o, *next, op = {0};
920 op.o_connid = c->c_connid;
921 op.o_tag = LDAP_REQ_ABANDON;
922 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
923 next = LDAP_STAILQ_NEXT( o, o_next );
924 op.orn_msgid = o->o_msgid;
926 op.o_bd = frontendDB;
927 frontendDB->be_abandon( &op, &rs );
930 /* remove pending operations */
931 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
932 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
933 LDAP_STAILQ_NEXT(o, o_next) = NULL;
938 void connection_closing( Connection *c, const char *why )
940 assert( connections != NULL );
942 assert( c->c_struct_state == SLAP_C_USED );
943 assert( c->c_conn_state != SLAP_C_INVALID );
945 /* c_mutex must be locked by caller */
947 if( c->c_conn_state != SLAP_C_CLOSING ) {
950 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
951 Debug( LDAP_DEBUG_TRACE,
952 "connection_closing: readying conn=%lu sd=%d for close\n",
953 c->c_connid, sd, 0 );
954 /* update state to closing */
955 c->c_conn_state = SLAP_C_CLOSING;
956 c->c_close_reason = why;
958 /* don't listen on this port anymore */
959 slapd_clr_read( sd, 1 );
961 /* abandon active operations */
962 connection_abandon( c );
964 /* wake write blocked operations */
965 slapd_clr_write( sd, 1 );
966 if ( c->c_writewaiter ) {
967 ldap_pvt_thread_cond_signal( &c->c_write_cv );
968 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
969 ldap_pvt_thread_yield();
970 ldap_pvt_thread_mutex_lock( &c->c_mutex );
972 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
973 /* Client closed connection after doing Unbind. */
974 c->c_close_reason = NULL;
978 static void connection_close( Connection *c )
982 assert( connections != NULL );
984 assert( c->c_struct_state == SLAP_C_USED );
985 assert( c->c_conn_state == SLAP_C_CLOSING );
987 /* note: connections_mutex and c_mutex should be locked by caller */
989 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
990 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
991 Debug( LDAP_DEBUG_TRACE,
992 "connection_close: deferring conn=%lu sd=%d\n",
993 c->c_connid, sd, 0 );
997 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
998 c->c_connid, sd, 0 );
999 connection_destroy( c );
1002 unsigned long connections_nextid(void)
1005 assert( connections != NULL );
1007 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1011 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
1016 Connection* connection_first( ber_socket_t *index )
1018 #ifdef SLAP_MULTI_CONN_ARRAY
1022 assert( connections != NULL );
1023 assert( index != NULL );
1025 #ifdef SLAP_MULTI_CONN_ARRAY
1026 for ( conn_array_id = 0;
1027 conn_array_id < NUM_CONNECTION_ARRAY;
1030 ldap_pvt_thread_mutex_lock( &connections_mutex[ conn_array_id ] );
1033 ldap_pvt_thread_mutex_lock( &connections_mutex );
1038 return connection_next(NULL, index);
1041 Connection* connection_next( Connection *c, ber_socket_t *index )
1042 #ifdef SLAP_MULTI_CONN_ARRAY
1046 assert( connections != NULL );
1047 assert( index != NULL );
1048 assert( *index >= 0 && *index < MCA_ARRAY_SIZE );
1050 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1054 for(; *index < dtblsize; (*index)++) {
1055 assert( MCA_conn_check( *index ) );
1056 conn = MCA_GET_CONNECTION(*index);
1057 if( conn->c_struct_state == SLAP_C_UNINITIALIZED ) {
1058 assert( conn->c_conn_state == SLAP_C_INVALID );
1059 #ifndef HAVE_WINSOCK
1066 if( conn->c_struct_state == SLAP_C_USED ) {
1067 assert( conn->c_conn_state != SLAP_C_INVALID );
1073 assert( conn->c_struct_state == SLAP_C_UNUSED );
1074 assert( conn->c_conn_state == SLAP_C_INVALID );
1077 if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
1084 assert( connections != NULL );
1085 assert( index != NULL );
1086 assert( *index <= dtblsize );
1088 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1092 for(; *index < dtblsize; (*index)++) {
1093 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
1094 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1095 #ifndef HAVE_WINSOCK
1102 if( connections[*index].c_struct_state == SLAP_C_USED ) {
1103 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
1104 c = &connections[(*index)++];
1108 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
1109 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
1112 if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
1117 void connection_done( Connection *c )
1119 #ifdef SLAP_MULTI_CONN_ARRAY
1123 assert( connections != NULL );
1125 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1127 #ifdef SLAP_MULTI_CONN_ARRAY
1128 for ( conn_array_id = 0;
1129 conn_array_id < NUM_CONNECTION_ARRAY;
1132 ldap_pvt_thread_mutex_unlock( &connections_mutex[ conn_array_id ] );
1135 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1140 * connection_activity - handle the request operation op on connection
1141 * conn. This routine figures out what kind of operation it is and
1142 * calls the appropriate stub to handle it.
1145 #ifdef SLAPD_MONITOR
1146 /* FIXME: returns 0 in case of failure */
1147 #define INCR_OP_INITIATED(index) \
1149 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1150 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
1151 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1153 #define INCR_OP_COMPLETED(index) \
1155 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1156 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1157 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
1158 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1160 #else /* !SLAPD_MONITOR */
1161 #define INCR_OP_INITIATED(index) do { } while (0)
1162 #define INCR_OP_COMPLETED(index) \
1164 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1165 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1166 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1168 #endif /* !SLAPD_MONITOR */
1171 * NOTE: keep in sync with enum in slapd.h
1173 static int (*opfun[])( Operation *op, SlapReply *rs ) = {
1188 connection_operation( void *ctx, void *arg_v )
1190 int rc = LDAP_OTHER;
1191 Operation *op = arg_v;
1192 SlapReply rs = {REP_RESULT};
1193 ber_tag_t tag = op->o_tag;
1195 Connection *conn = op->o_conn;
1196 void *memctx = NULL;
1197 void *memctx_null = NULL;
1200 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
1201 /* FIXME: returns 0 in case of failure */
1202 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
1203 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1205 op->o_threadctx = ctx;
1209 case LDAP_REQ_UNBIND:
1211 case LDAP_REQ_DELETE:
1212 case LDAP_REQ_MODDN:
1213 case LDAP_REQ_MODIFY:
1214 case LDAP_REQ_COMPARE:
1215 case LDAP_REQ_SEARCH:
1216 case LDAP_REQ_ABANDON:
1217 case LDAP_REQ_EXTENDED:
1220 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1221 "conn %lu unknown LDAP request 0x%lx\n",
1222 conn->c_connid, tag, 0 );
1223 op->o_tag = LBER_ERROR;
1224 rs.sr_err = LDAP_PROTOCOL_ERROR;
1225 rs.sr_text = "unknown LDAP request";
1226 send_ldap_disconnect( op, &rs );
1227 rc = SLAPD_DISCONNECT;
1228 goto operations_error;
1231 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1232 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1233 "error: SASL bind in progress (tag=%ld).\n",
1235 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1236 "SASL bind in progress" );
1237 rc = LDAP_OPERATIONS_ERROR;
1238 goto operations_error;
1241 /* We can use Thread-Local storage for most mallocs. We can
1242 * also use TL for ber parsing, but not on Add or Modify.
1245 memsiz = ber_len( op->o_ber ) * 64;
1246 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1248 memsiz = SLAP_SLAB_SIZE;
1250 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
1251 op->o_tmpmemctx = memctx;
1252 op->o_tmpmfuncs = &slap_sl_mfuncs;
1253 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1254 /* Note - the ber and its buffer are already allocated from
1255 * regular memory; this only affects subsequent mallocs that
1256 * ber_scanf may invoke.
1258 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1263 opidx = SLAP_OP_BIND;
1266 case LDAP_REQ_UNBIND:
1267 opidx = SLAP_OP_UNBIND;
1271 opidx = SLAP_OP_ADD;
1274 case LDAP_REQ_DELETE:
1275 opidx = SLAP_OP_DELETE;
1278 case LDAP_REQ_MODRDN:
1279 opidx = SLAP_OP_MODRDN;
1282 case LDAP_REQ_MODIFY:
1283 opidx = SLAP_OP_MODIFY;
1286 case LDAP_REQ_COMPARE:
1287 opidx = SLAP_OP_COMPARE;
1290 case LDAP_REQ_SEARCH:
1291 opidx = SLAP_OP_SEARCH;
1294 case LDAP_REQ_ABANDON:
1295 opidx = SLAP_OP_ABANDON;
1298 case LDAP_REQ_EXTENDED:
1299 opidx = SLAP_OP_EXTENDED;
1307 assert( opidx > -1 );
1308 INCR_OP_INITIATED( opidx );
1309 rc = (*(opfun[opidx]))( op, &rs );
1312 if ( rc == SLAPD_DISCONNECT ) {
1315 } else if ( opidx > -1 ) {
1316 /* increment completed operations count
1317 * only if operation was initiated
1318 * and rc != SLAPD_DISCONNECT */
1319 INCR_OP_COMPLETED( opidx );
1322 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1323 if ( rc == SLAPD_ABANDON ) {
1324 op->o_cancel = SLAP_CANCEL_ACK;
1326 op->o_cancel = LDAP_TOO_LATE;
1329 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1330 op->o_cancel != SLAP_CANCEL_DONE )
1332 ldap_pvt_thread_yield();
1335 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1337 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1339 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1340 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1342 conn->c_n_ops_executing--;
1343 conn->c_n_ops_completed++;
1347 case LDAP_REQ_UNBIND:
1348 /* c_mutex is locked */
1349 connection_closing( conn,
1350 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1354 connection_resched( conn );
1355 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1359 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1361 int connection_client_setup(
1363 ldap_pvt_thread_start_t *func,
1369 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1370 CONN_IS_CLIENT, 0, NULL );
1371 if ( rc < 0 ) return -1;
1373 c = connection_get( s );
1374 c->c_clientfunc = func;
1375 c->c_clientarg = arg;
1377 slapd_add_internal( s, 0 );
1378 slapd_set_read( s, 1 );
1379 connection_return( c );
1383 void connection_client_enable(
1386 slapd_set_read( s, 1 );
1389 void connection_client_stop(
1394 /* get (locked) connection */
1395 c = connection_get( s );
1397 assert( c->c_conn_state == SLAP_C_CLIENT );
1399 c->c_listener = NULL;
1400 c->c_conn_state = SLAP_C_INVALID;
1401 c->c_struct_state = SLAP_C_UNUSED;
1402 c->c_close_reason = "?"; /* should never be needed */
1404 ber_sockbuf_free( c->c_sb );
1405 slapd_remove( s, 0, 1, 1 );
1406 c->c_sb = ber_sockbuf_alloc( );
1408 ber_len_t max = sockbuf_max_incoming;
1409 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1412 connection_return( c );
1415 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1417 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1419 static void* connection_read_thread( void* ctx, void* argv )
1422 conn_readinfo cri = { NULL, NULL, NULL, 0 };
1423 ber_socket_t s = (long)argv;
1426 * read incoming LDAP requests. If there is more than one,
1427 * the first one is returned with new_op
1429 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1430 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1431 return (void*)(long)rc;
1434 /* execute a single queued request in the same thread */
1435 if( cri.op && !cri.nullop ) {
1436 rc = (long)connection_operation( ctx, cri.op );
1437 } else if ( cri.func ) {
1438 rc = (long)cri.func( ctx, cri.arg );
1441 return (void*)(long)rc;
1444 int connection_read_activate( ber_socket_t s )
1449 * suspend reading on this file descriptor until a connection processing
1450 * thread reads data on it. Otherwise the listener thread will repeatedly
1451 * submit the same event on it to the pool.
1453 rc = slapd_clr_read( s, 0 );
1457 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1458 connection_read_thread, (void *)(long)s );
1461 Debug( LDAP_DEBUG_ANY,
1462 "connection_read_activate(%d): submit failed (%d)\n",
1470 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1472 connection_read( ber_socket_t s, conn_readinfo *cri )
1474 int connection_read(ber_socket_t s)
1480 assert( connections != NULL );
1482 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
1484 /* get (locked) connection */
1485 c = connection_get( s );
1488 Debug( LDAP_DEBUG_ANY,
1489 "connection_read(%ld): no connection!\n",
1492 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1498 if( c->c_conn_state == SLAP_C_CLOSING ) {
1499 Debug( LDAP_DEBUG_TRACE,
1500 "connection_read(%d): closing, ignoring input for id=%lu\n",
1501 s, c->c_connid, 0 );
1503 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1504 slapd_set_read( s, 1 );
1506 connection_return( c );
1507 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1511 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1512 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1513 cri->func = c->c_clientfunc;
1514 cri->arg = c->c_clientarg;
1515 /* read should already be cleared */
1517 slapd_clr_read( s, 0 );
1518 ldap_pvt_thread_pool_submit( &connection_pool,
1519 c->c_clientfunc, c->c_clientarg );
1521 connection_return( c );
1522 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1526 Debug( LDAP_DEBUG_TRACE,
1527 "connection_read(%d): checking for input on id=%lu\n",
1528 s, c->c_connid, 0 );
1531 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1532 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1534 Debug( LDAP_DEBUG_TRACE,
1535 "connection_read(%d): TLS accept failure "
1536 "error=%d id=%lu, closing\n",
1537 s, rc, c->c_connid );
1539 c->c_needs_tls_accept = 0;
1540 /* connections_mutex and c_mutex are locked */
1541 connection_closing( c, "TLS negotiation failure" );
1547 /* Drain input before close, to allow SSL error codes
1548 * to propagate to client. */
1554 rc = select(s+1, &rfd, NULL, NULL, &tv);
1556 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1562 connection_close( c );
1563 connection_return( c );
1564 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1567 } else if ( rc == 0 ) {
1569 struct berval authid = BER_BVNULL;
1571 c->c_needs_tls_accept = 0;
1573 /* we need to let SASL know */
1574 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1576 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1577 if( c->c_tls_ssf > c->c_ssf ) {
1578 c->c_ssf = c->c_tls_ssf;
1581 rc = dnX509peerNormalize( ssl, &authid );
1582 if ( rc != LDAP_SUCCESS ) {
1583 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1584 "unable to get TLS client DN, error=%d id=%lu\n",
1585 s, rc, c->c_connid );
1587 Statslog( LDAP_DEBUG_STATS,
1588 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1589 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1590 slap_sasl_external( c, c->c_tls_ssf, &authid );
1591 if ( authid.bv_val ) free( authid.bv_val );
1594 /* if success and data is ready, fall thru to data input loop */
1596 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1598 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1599 slapd_set_read( s, 1 );
1602 connection_return( c );
1603 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1609 #ifdef HAVE_CYRUS_SASL
1610 if ( c->c_sasl_layers ) {
1611 /* If previous layer is not removed yet, give up for now */
1612 if ( !c->c_sasl_sockctx ) {
1613 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1614 slapd_set_read( s, 1 );
1617 connection_return( c );
1618 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1622 c->c_sasl_layers = 0;
1624 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1625 if( rc != LDAP_SUCCESS ) {
1626 Debug( LDAP_DEBUG_TRACE,
1627 "connection_read(%d): SASL install error "
1628 "error=%d id=%lu, closing\n",
1629 s, rc, c->c_connid );
1631 /* connections_mutex and c_mutex are locked */
1632 connection_closing( c, "SASL layer install failure" );
1633 connection_close( c );
1634 connection_return( c );
1635 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1641 #define CONNECTION_INPUT_LOOP 1
1642 /* #define DATA_READY_LOOP 1 */
1645 /* How do we do this without getting into a busy loop ? */
1646 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1647 rc = connection_input( c, cri );
1649 rc = connection_input( c );
1652 #ifdef DATA_READY_LOOP
1653 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1654 #elif CONNECTION_INPUT_LOOP
1661 Debug( LDAP_DEBUG_TRACE,
1662 "connection_read(%d): input error=%d id=%lu, closing.\n",
1663 s, rc, c->c_connid );
1665 /* connections_mutex and c_mutex are locked */
1666 connection_closing( c, conn_lost_str );
1667 connection_close( c );
1668 connection_return( c );
1669 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1673 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1674 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1675 slapd_set_write( s, 0 );
1678 slapd_set_read( s, 1 );
1680 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1681 slapd_set_read( s, 1 );
1684 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1685 slapd_set_write( s, 1 );
1689 connection_return( c );
1690 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
1696 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1697 connection_input( Connection *conn , conn_readinfo *cri )
1699 connection_input( Connection *conn )
1708 #ifdef LDAP_CONNECTIONLESS
1714 if ( conn->c_currentber == NULL &&
1715 ( conn->c_currentber = ber_alloc()) == NULL )
1717 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1723 #ifdef LDAP_CONNECTIONLESS
1724 if ( conn->c_is_udp ) {
1725 char peername[sizeof("IP=255.255.255.255:65336")];
1727 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1728 if (len != sizeof(struct sockaddr)) return 1;
1730 sprintf( peername, "IP=%s:%d",
1731 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1732 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1733 Statslog( LDAP_DEBUG_STATS,
1734 "conn=%lu UDP request from %s (%s) accepted.\n",
1735 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1739 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1740 if ( tag != LDAP_TAG_MESSAGE ) {
1744 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1746 Debug( LDAP_DEBUG_TRACE,
1747 "ber_get_next on fd %d failed errno=%d (%s)\n",
1748 sd, err, sock_errstr(err) );
1749 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1750 /* log, close and send error */
1751 ber_free( conn->c_currentber, 1 );
1752 conn->c_currentber = NULL;
1759 ber = conn->c_currentber;
1760 conn->c_currentber = NULL;
1762 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1763 /* log, close and send error */
1764 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1769 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1770 /* log, close and send error */
1771 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1777 #ifdef LDAP_CONNECTIONLESS
1778 if( conn->c_is_udp ) {
1779 if( tag == LBER_OCTETSTRING ) {
1780 ber_get_stringa( ber, &cdn );
1781 tag = ber_peek_tag(ber, &len);
1783 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1784 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1791 if(tag == LDAP_REQ_BIND) {
1792 /* immediately abandon all existing operations upon BIND */
1793 connection_abandon( conn );
1796 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1799 /* clear state if the connection is being reused from inactive */
1800 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1801 memset( &conn->c_pagedresults_state, 0,
1802 sizeof( conn->c_pagedresults_state ) );
1805 op->o_res_ber = NULL;
1807 #ifdef LDAP_CONNECTIONLESS
1808 if (conn->c_is_udp) {
1810 ber_str2bv( cdn, 0, 1, &op->o_dn );
1811 op->o_protocol = LDAP_VERSION2;
1813 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1814 if (op->o_res_ber == NULL) return 1;
1816 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1817 sizeof(struct sockaddr), 0 );
1819 if (rc != sizeof(struct sockaddr)) {
1820 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1824 if (op->o_protocol == LDAP_VERSION2) {
1825 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1827 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1832 #endif /* LDAP_CONNECTIONLESS */
1836 /* Don't process requests when the conn is in the middle of a
1837 * Bind, or if it's closing. Also, don't let any single conn
1838 * use up all the available threads, and don't execute if we're
1839 * currently blocked on output. And don't execute if there are
1840 * already pending ops, let them go first. Abandon operations
1841 * get exceptions to some, but not all, cases.
1845 /* Abandon and Unbind are exempt from these checks */
1846 if (conn->c_conn_state == SLAP_C_CLOSING) {
1849 } else if (conn->c_writewaiter) {
1850 defer = "awaiting write";
1852 } else if (conn->c_n_ops_pending) {
1853 defer = "pending operations";
1857 case LDAP_REQ_ABANDON:
1858 /* Unbind is exempt from these checks */
1859 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1860 defer = "too many executing";
1862 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1867 case LDAP_REQ_UNBIND:
1872 int max = conn->c_dn.bv_len
1873 ? slap_conn_max_pending_auth
1874 : slap_conn_max_pending;
1876 Debug( LDAP_DEBUG_ANY,
1877 "connection_input: conn=%lu deferring operation: %s\n",
1878 conn->c_connid, defer, 0 );
1879 conn->c_n_ops_pending++;
1880 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1881 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1884 conn->c_n_ops_executing++;
1886 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1888 * The first op will be processed in the same thread context,
1889 * as long as there is only one op total.
1890 * Subsequent ops will be submitted to the pool by
1891 * calling connection_op_activate()
1893 if ( cri->op == NULL ) {
1894 /* the first incoming request */
1895 connection_op_queue( op );
1898 if ( !cri->nullop ) {
1900 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1901 connection_operation, (void *) cri->op );
1903 connection_op_activate( op );
1906 connection_op_activate( op );
1911 if ( conn->c_struct_state != SLAP_C_USED ) {
1912 /* connection must have got closed underneath us */
1917 assert( conn->c_struct_state == SLAP_C_USED );
1922 connection_resched( Connection *conn )
1926 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1929 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1931 /* use trylock to avoid possible deadlock */
1932 rc = ldap_pvt_thread_mutex_trylock( MCA_GET_CONN_MUTEX( sd ) );
1935 Debug( LDAP_DEBUG_TRACE,
1936 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1937 conn->c_connid, sd, 0 );
1939 * reaquire locks in the right order...
1940 * this may allow another thread to close this connection,
1941 * so recheck state below.
1943 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1944 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX ( sd ) );
1945 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1948 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1949 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1950 "closed by other thread conn=%lu sd=%d\n",
1951 conn->c_connid, sd, 0 );
1953 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1954 "attempting closing conn=%lu sd=%d\n",
1955 conn->c_connid, sd, 0 );
1956 connection_close( conn );
1959 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( sd ) );
1963 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1964 /* other states need different handling */
1968 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1969 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1971 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1972 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1974 /* pending operations should not be marked for abandonment */
1975 assert(!op->o_abandon);
1977 conn->c_n_ops_pending--;
1978 conn->c_n_ops_executing++;
1980 connection_op_activate( op );
1982 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1988 connection_init_log_prefix( Operation *op )
1990 if ( op->o_connid == (unsigned long)(-1) ) {
1991 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1992 "conn=-1 op=%lu", op->o_opid );
1995 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1996 "conn=%lu op=%lu", op->o_connid, op->o_opid );
2000 static int connection_bind_cb( Operation *op, SlapReply *rs )
2002 slap_callback *cb = op->o_callback;
2003 op->o_callback = cb->sc_next;
2005 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
2006 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
2007 op->o_conn->c_sasl_bind_in_progress =
2008 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
2010 /* Moved here from bind.c due to ITS#4158 */
2011 op->o_conn->c_sasl_bindop = NULL;
2012 if ( op->orb_method == LDAP_AUTH_SASL ) {
2013 if( rs->sr_err == LDAP_SUCCESS ) {
2014 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
2015 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
2016 /* edn is always normalized already */
2017 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
2019 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
2020 BER_BVZERO( &op->orb_edn );
2021 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
2022 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
2024 op->o_conn->c_sasl_ssf = op->orb_ssf;
2025 if( op->orb_ssf > op->o_conn->c_ssf ) {
2026 op->o_conn->c_ssf = op->orb_ssf;
2029 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
2030 ber_len_t max = sockbuf_max_incoming_auth;
2031 ber_sockbuf_ctrl( op->o_conn->c_sb,
2032 LBER_SB_OPT_SET_MAX_INCOMING, &max );
2035 /* log authorization identity */
2036 Statslog( LDAP_DEBUG_STATS,
2037 "%s BIND dn=\"%s\" mech=%s ssf=%d\n",
2039 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
2040 op->o_conn->c_authmech.bv_val, op->orb_ssf, 0 );
2042 Debug( LDAP_DEBUG_TRACE,
2043 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
2044 op->o_conn->c_authmech.bv_val,
2045 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
2048 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
2049 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
2050 free( op->o_conn->c_sasl_bind_mech.bv_val );
2051 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
2055 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
2058 return SLAP_CB_CONTINUE;
2061 static void connection_op_queue( Operation *op )
2063 ber_tag_t tag = op->o_tag;
2065 if (tag == LDAP_REQ_BIND) {
2066 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
2067 sc->sc_response = connection_bind_cb;
2068 sc->sc_next = op->o_callback;
2069 op->o_callback = sc;
2070 op->o_conn->c_conn_state = SLAP_C_BINDING;
2073 if (!op->o_dn.bv_len) {
2074 op->o_authz = op->o_conn->c_authz;
2075 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
2076 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
2077 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
2079 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
2080 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
2084 op->o_authtype = op->o_conn->c_authtype;
2085 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
2087 if (!op->o_protocol) {
2088 op->o_protocol = op->o_conn->c_protocol
2089 ? op->o_conn->c_protocol : LDAP_VERSION3;
2092 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
2093 op->o_protocol > LDAP_VERSION2)
2095 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
2098 op->o_connid = op->o_conn->c_connid;
2099 connection_init_log_prefix( op );
2101 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
2104 static int connection_op_activate( Operation *op )
2108 connection_op_queue( op );
2110 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2111 connection_operation, (void *) op );
2114 Debug( LDAP_DEBUG_ANY,
2115 "connection_op_activate: submit failed (%d) for conn=%lu\n",
2116 rc, op->o_connid, 0 );
2117 /* should move op to pending list */
2123 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
2124 static int connection_write( ber_socket_t s );
2125 static void *connection_write_thread( void *ctx, void *arg )
2127 return (void *)(long)connection_write((long)arg);
2130 int connection_write_activate( ber_socket_t s )
2135 * suspend reading on this file descriptor until a connection processing
2136 * thread write data on it. Otherwise the listener thread will repeatedly
2137 * submit the same event on it to the pool.
2139 slapd_clr_write( s, 0);
2141 rc = ldap_pvt_thread_pool_submit( &connection_pool,
2142 connection_write_thread, (void *)(long)s );
2145 Debug( LDAP_DEBUG_ANY,
2146 "connection_write_activate(%d): submit failed (%d)\n",
2154 int connection_write(ber_socket_t s)
2159 assert( connections != NULL );
2161 ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX( s ) );
2163 c = connection_get( s );
2165 Debug( LDAP_DEBUG_ANY,
2166 "connection_write(%ld): no connection!\n",
2168 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( s ) );
2172 #ifndef SLAP_LIGHTWEIGHT_DISPATCHER
2173 slapd_clr_write( s, 0);
2177 Debug( LDAP_DEBUG_TRACE,
2178 "connection_write(%d): waking output for id=%lu\n",
2179 s, c->c_connid, 0 );
2180 ldap_pvt_thread_cond_signal( &c->c_write_cv );
2182 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
2183 slapd_set_read( s, 1 );
2185 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
2186 slapd_set_write( s, 1 );
2189 /* If there are ops pending because of a writewaiter,
2192 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
2193 if ( !c->c_writewaiter ) break;
2194 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
2196 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
2197 LDAP_STAILQ_NEXT(op, o_next) = NULL;
2199 /* pending operations should not be marked for abandonment */
2200 assert(!op->o_abandon);
2202 c->c_n_ops_pending--;
2203 c->c_n_ops_executing++;
2205 connection_op_activate( op );
2210 connection_return( c );
2211 ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
2216 connection_fake_init(
2221 conn->c_connid = -1;
2222 conn->c_send_ldap_result = slap_send_ldap_result;
2223 conn->c_send_search_entry = slap_send_search_entry;
2224 conn->c_send_search_reference = slap_send_search_reference;
2225 conn->c_listener = (Listener *)&dummy_list;
2226 conn->c_peer_domain = slap_empty_bv;
2227 conn->c_peer_name = slap_empty_bv;
2229 memset(op, 0, OPERATION_BUFFER_SIZE);
2230 op->o_hdr = (Opheader *)(op+1);
2231 op->o_controls = (void **)(op->o_hdr+1);
2232 /* set memory context */
2233 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
2234 op->o_tmpmfuncs = &slap_sl_mfuncs;
2235 op->o_threadctx = ctx;
2238 op->o_connid = op->o_conn->c_connid;
2239 connection_init_log_prefix( op );
2241 slap_op_time( &op->o_time, &op->o_tincr );
2245 connection_assign_nextid( Connection *conn )
2247 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2248 conn->c_connid = conn_nextid++;
2249 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );