2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 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 /* protected by connections_mutex */
47 static ldap_pvt_thread_mutex_t connections_mutex;
48 static Connection *connections = NULL;
50 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
51 static unsigned long conn_nextid = 0;
53 static const char conn_lost_str[] = "connection lost";
55 /* structure state (protected by connections_mutex) */
56 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
57 #define SLAP_C_UNUSED 0x01
58 #define SLAP_C_USED 0x02
59 #define SLAP_C_PENDING 0x03
61 /* connection state (protected by c_mutex ) */
62 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
63 #define SLAP_C_INACTIVE 0x01 /* zero threads */
64 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
65 #define SLAP_C_BINDING 0x03 /* binding */
66 #define SLAP_C_CLOSING 0x04 /* closing */
67 #define SLAP_C_CLIENT 0x05 /* outbound client conn */
70 connection_state2str( int state )
73 case SLAP_C_INVALID: return "!";
74 case SLAP_C_INACTIVE: return "|";
75 case SLAP_C_ACTIVE: return "";
76 case SLAP_C_BINDING: return "B";
77 case SLAP_C_CLOSING: return "C";
78 case SLAP_C_CLIENT: return "L";
84 static Connection* connection_get( ber_socket_t s );
86 typedef struct conn_readinfo {
88 ldap_pvt_thread_start_t *func;
94 static int connection_input( Connection *c, conn_readinfo *cri );
95 static void connection_close( Connection *c );
97 static int connection_op_activate( Operation *op );
98 static void connection_op_queue( Operation *op );
99 static int connection_resched( Connection *conn );
100 static void connection_abandon( Connection *conn );
101 static void connection_destroy( Connection *c );
103 static ldap_pvt_thread_start_t connection_operation;
106 * Initialize connection management infrastructure.
108 int connections_init(void)
112 assert( connections == NULL );
114 if( connections != NULL) {
115 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
120 /* should check return of every call */
121 ldap_pvt_thread_mutex_init( &connections_mutex );
122 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
124 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
126 if( connections == NULL ) {
127 Debug( LDAP_DEBUG_ANY, "connections_init: "
128 "allocation (%d*%ld) of connection array failed\n",
129 dtblsize, (long) sizeof(Connection), 0 );
133 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
134 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
136 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
139 * per entry initialization of the Connection array initialization
140 * will be done by connection_init()
147 * Destroy connection management infrastructure.
150 int connections_destroy(void)
154 /* should check return of every call */
156 if( connections == NULL) {
157 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
162 for ( i = 0; i < dtblsize; i++ ) {
163 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
164 ber_sockbuf_free( connections[i].c_sb );
165 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
166 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
167 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
169 if ( slapi_plugins_used ) {
170 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
180 ldap_pvt_thread_mutex_destroy( &connections_mutex );
181 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
186 * shutdown all connections
188 int connections_shutdown(void)
192 for ( i = 0; i < dtblsize; i++ ) {
193 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
194 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
195 if( connections[i].c_struct_state == SLAP_C_USED ) {
197 /* give persistent clients a chance to cleanup */
198 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
199 ldap_pvt_thread_pool_submit( &connection_pool,
200 connections[i].c_clientfunc, connections[i].c_clientarg );
202 /* c_mutex is locked */
203 connection_closing( &connections[i], "slapd shutdown" );
204 connection_close( &connections[i] );
207 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
215 * Timeout idle connections.
217 int connections_timeout_idle(time_t now)
223 for( c = connection_first( &connindex );
225 c = connection_next( c, &connindex ) )
227 /* Don't timeout a slow-running request or a persistent
228 * outbound connection */
229 if( c->c_n_ops_executing || c->c_conn_state == SLAP_C_CLIENT ) {
233 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
235 connection_closing( c, "idletimeout" );
236 connection_close( c );
240 connection_done( c );
245 static Connection* connection_get( ber_socket_t s )
249 Debug( LDAP_DEBUG_ARGS,
250 "connection_get(%ld)\n",
253 assert( connections != NULL );
255 if(s == AC_SOCKET_INVALID) return NULL;
258 assert( s < dtblsize );
264 ldap_pvt_thread_mutex_lock( &connections_mutex );
265 for(i=0; i<dtblsize; i++) {
266 if( connections[i].c_struct_state == SLAP_C_PENDING )
269 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
270 assert( connections[i].c_conn_state == SLAP_C_INVALID );
271 assert( connections[i].c_sb == 0 );
275 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
276 assert( connections[i].c_conn_state == SLAP_C_INVALID );
277 assert( connections[i].c_sd == AC_SOCKET_INVALID );
281 /* state can actually change from used -> unused by resched,
282 * so don't assert details here.
285 if( connections[i].c_sd == s ) {
290 ldap_pvt_thread_mutex_unlock( &connections_mutex );
295 ldap_pvt_thread_mutex_lock( &c->c_mutex );
297 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
300 /* Avoid race condition after releasing
303 if ( c->c_sd != s ) {
304 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
308 if( c->c_struct_state != SLAP_C_USED ) {
309 /* connection must have been closed due to resched */
311 assert( c->c_conn_state == SLAP_C_INVALID );
312 assert( c->c_sd == AC_SOCKET_INVALID );
314 Debug( LDAP_DEBUG_TRACE,
315 "connection_get(%d): connection not used\n",
318 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
322 Debug( LDAP_DEBUG_TRACE,
323 "connection_get(%d): got connid=%lu\n",
328 assert( c->c_struct_state == SLAP_C_USED );
329 assert( c->c_conn_state != SLAP_C_INVALID );
330 assert( c->c_sd != AC_SOCKET_INVALID );
332 #ifndef SLAPD_MONITOR
333 if ( global_idletimeout > 0 )
334 #endif /* ! SLAPD_MONITOR */
336 c->c_activitytime = slap_get_time();
343 static void connection_return( Connection *c )
345 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
348 Connection * connection_init(
352 const char* peername,
355 struct berval *authid
356 LDAP_PF_LOCAL_SENDMSG_ARG(struct berval *peerbv))
362 assert( connections != NULL );
364 assert( listener != NULL );
365 assert( dnsname != NULL );
366 assert( peername != NULL );
369 assert( !( flags & CONN_IS_TLS ));
372 if( s == AC_SOCKET_INVALID ) {
373 Debug( LDAP_DEBUG_ANY,
374 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
380 assert( s < dtblsize );
382 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
385 assert( c->c_struct_state == SLAP_C_UNUSED );
392 ldap_pvt_thread_mutex_lock( &connections_mutex );
393 for( i=0; i < dtblsize; i++) {
394 if ( connections[i].c_struct_state == SLAP_C_PENDING )
397 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
398 assert( connections[i].c_sb == 0 );
400 c->c_struct_state = SLAP_C_PENDING;
405 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
407 c->c_struct_state = SLAP_C_PENDING;
411 if( connections[i].c_conn_state == SLAP_C_CLIENT ) continue;
413 assert( connections[i].c_struct_state == SLAP_C_USED );
414 assert( connections[i].c_conn_state != SLAP_C_INVALID );
416 ldap_pvt_thread_mutex_unlock( &connections_mutex );
419 Debug( LDAP_DEBUG_ANY,
420 "connection_init(%d): connection table full "
421 "(%d/%d)\n", s, i, dtblsize);
428 c->c_send_ldap_result = slap_send_ldap_result;
429 c->c_send_search_entry = slap_send_search_entry;
430 c->c_send_search_reference = slap_send_search_reference;
431 c->c_send_ldap_extended = slap_send_ldap_extended;
432 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
434 BER_BVZERO( &c->c_authmech );
435 BER_BVZERO( &c->c_dn );
436 BER_BVZERO( &c->c_ndn );
438 c->c_listener = NULL;
439 BER_BVZERO( &c->c_peer_domain );
440 BER_BVZERO( &c->c_peer_name );
442 LDAP_STAILQ_INIT(&c->c_ops);
443 LDAP_STAILQ_INIT(&c->c_pending_ops);
446 c->c_txn = CONN_TXN_INACTIVE;
447 c->c_txn_backend = NULL;
448 LDAP_STAILQ_INIT(&c->c_txn_ops);
451 BER_BVZERO( &c->c_sasl_bind_mech );
453 c->c_sasl_authctx = NULL;
454 c->c_sasl_sockctx = NULL;
455 c->c_sasl_extra = NULL;
456 c->c_sasl_bindop = NULL;
458 c->c_sb = ber_sockbuf_alloc( );
461 ber_len_t max = sockbuf_max_incoming;
462 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
465 c->c_currentber = NULL;
467 /* should check status of thread calls */
468 ldap_pvt_thread_mutex_init( &c->c_mutex );
469 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
470 ldap_pvt_thread_cond_init( &c->c_write_cv );
473 if ( slapi_plugins_used ) {
474 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
479 ldap_pvt_thread_mutex_lock( &c->c_mutex );
481 assert( BER_BVISNULL( &c->c_authmech ) );
482 assert( BER_BVISNULL( &c->c_dn ) );
483 assert( BER_BVISNULL( &c->c_ndn ) );
484 assert( c->c_listener == NULL );
485 assert( BER_BVISNULL( &c->c_peer_domain ) );
486 assert( BER_BVISNULL( &c->c_peer_name ) );
487 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
488 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
490 assert( c->c_txn == CONN_TXN_INACTIVE );
491 assert( c->c_txn_backend == NULL );
492 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
494 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
495 assert( c->c_sasl_done == 0 );
496 assert( c->c_sasl_authctx == NULL );
497 assert( c->c_sasl_sockctx == NULL );
498 assert( c->c_sasl_extra == NULL );
499 assert( c->c_sasl_bindop == NULL );
500 assert( c->c_currentber == NULL );
501 assert( c->c_writewaiter == 0);
503 c->c_listener = listener;
506 if ( flags & CONN_IS_CLIENT ) {
508 c->c_conn_state = SLAP_C_CLIENT;
509 c->c_struct_state = SLAP_C_USED;
510 c->c_close_reason = "?"; /* should never be needed */
511 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
512 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
517 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
518 ber_str2bv( peername, 0, 1, &c->c_peer_name );
520 c->c_n_ops_received = 0;
521 c->c_n_ops_executing = 0;
522 c->c_n_ops_pending = 0;
523 c->c_n_ops_completed = 0;
529 /* set to zero until bind, implies LDAP_VERSION3 */
532 #ifndef SLAPD_MONITOR
533 if ( global_idletimeout > 0 )
534 #endif /* ! SLAPD_MONITOR */
536 c->c_activitytime = c->c_starttime = slap_get_time();
539 #ifdef LDAP_CONNECTIONLESS
541 if( flags & CONN_IS_UDP ) {
544 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
545 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
547 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
548 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
549 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
550 LBER_SBIOD_LEVEL_PROVIDER, NULL );
552 #endif /* LDAP_CONNECTIONLESS */
554 if ( flags & CONN_IS_IPC ) {
556 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
557 LBER_SBIOD_LEVEL_PROVIDER, (void*)"ipc_" );
559 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_fd,
560 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
561 #ifdef LDAP_PF_LOCAL_SENDMSG
562 if ( !BER_BVISEMPTY( peerbv ))
563 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_UNGET_BUF, peerbv );
566 #endif /* LDAP_PF_LOCAL */
569 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
570 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
572 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
573 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
577 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
578 INT_MAX, (void*)"ldap_" );
581 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
582 c /* non-NULL */ ) < 0 )
584 Debug( LDAP_DEBUG_ANY,
585 "connection_init(%d, %s): set nonblocking failed\n",
586 s, c->c_peer_name.bv_val, 0 );
589 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
590 id = c->c_connid = conn_nextid++;
591 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
593 c->c_conn_state = SLAP_C_INACTIVE;
594 c->c_struct_state = SLAP_C_USED;
595 c->c_close_reason = "?"; /* should never be needed */
597 c->c_ssf = c->c_transport_ssf = ssf;
601 if ( flags & CONN_IS_TLS ) {
603 c->c_needs_tls_accept = 1;
606 c->c_needs_tls_accept = 0;
610 slap_sasl_open( c, 0 );
611 slap_sasl_external( c, ssf, authid );
613 slapd_add_internal( s, 1 );
614 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
616 backend_connection_init(c);
621 void connection2anonymous( Connection *c )
623 assert( connections != NULL );
627 ber_len_t max = sockbuf_max_incoming;
628 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
631 if ( !BER_BVISNULL( &c->c_authmech ) ) {
632 ch_free(c->c_authmech.bv_val);
634 BER_BVZERO( &c->c_authmech );
636 if ( !BER_BVISNULL( &c->c_dn ) ) {
637 ch_free(c->c_dn.bv_val);
639 BER_BVZERO( &c->c_dn );
641 if ( !BER_BVISNULL( &c->c_ndn ) ) {
642 ch_free(c->c_ndn.bv_val);
644 BER_BVZERO( &c->c_ndn );
646 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
647 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
649 BER_BVZERO( &c->c_sasl_authz_dn );
651 c->c_authz_backend = NULL;
655 connection_destroy( Connection *c )
657 unsigned long connid;
658 const char *close_reason;
662 assert( connections != NULL );
664 assert( c->c_struct_state != SLAP_C_UNUSED );
665 assert( c->c_conn_state != SLAP_C_INVALID );
666 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
667 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
669 assert( c->c_txn == CONN_TXN_INACTIVE );
670 assert( c->c_txn_backend == NULL );
671 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
673 assert( c->c_writewaiter == 0);
675 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
676 connid = c->c_connid;
677 close_reason = c->c_close_reason;
679 ldap_pvt_thread_mutex_lock( &connections_mutex );
680 c->c_struct_state = SLAP_C_PENDING;
681 ldap_pvt_thread_mutex_unlock( &connections_mutex );
683 backend_connection_destroy(c);
688 c->c_activitytime = c->c_starttime = 0;
690 connection2anonymous( c );
691 c->c_listener = NULL;
693 if(c->c_peer_domain.bv_val != NULL) {
694 free(c->c_peer_domain.bv_val);
696 BER_BVZERO( &c->c_peer_domain );
697 if(c->c_peer_name.bv_val != NULL) {
698 free(c->c_peer_name.bv_val);
700 BER_BVZERO( &c->c_peer_name );
702 c->c_sasl_bind_in_progress = 0;
703 if(c->c_sasl_bind_mech.bv_val != NULL) {
704 free(c->c_sasl_bind_mech.bv_val);
706 BER_BVZERO( &c->c_sasl_bind_mech );
708 slap_sasl_close( c );
710 if ( c->c_currentber != NULL ) {
711 ber_free( c->c_currentber, 1 );
712 c->c_currentber = NULL;
717 /* call destructors, then constructors; avoids unnecessary allocation */
718 if ( slapi_plugins_used ) {
719 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
724 c->c_sd = AC_SOCKET_INVALID;
725 c->c_conn_state = SLAP_C_INVALID;
726 c->c_struct_state = SLAP_C_UNUSED;
727 c->c_close_reason = "?"; /* should never be needed */
730 c->c_sb = ber_sockbuf_alloc( );
732 ber_len_t max = sockbuf_max_incoming;
733 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
736 /* c must be fully reset by this point; when we call slapd_remove
737 * it may get immediately reused by a new connection.
739 if ( sd != AC_SOCKET_INVALID ) {
740 slapd_remove( sd, sb, 1, 0, 0 );
742 if ( close_reason == NULL ) {
743 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
744 connid, (long) sd, 0, 0, 0 );
746 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
747 connid, (long) sd, close_reason, 0, 0 );
752 int connection_state_closing( Connection *c )
754 /* c_mutex must be locked by caller */
758 assert( c->c_struct_state == SLAP_C_USED );
760 state = c->c_conn_state;
762 assert( state != SLAP_C_INVALID );
764 return state == SLAP_C_CLOSING;
767 static void connection_abandon( Connection *c )
769 /* c_mutex must be locked by caller */
771 Operation *o, *next, op = {0};
777 op.o_connid = c->c_connid;
778 op.o_tag = LDAP_REQ_ABANDON;
780 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
781 next = LDAP_STAILQ_NEXT( o, o_next );
782 op.orn_msgid = o->o_msgid;
784 op.o_bd = frontendDB;
785 frontendDB->be_abandon( &op, &rs );
789 /* remove operations in pending transaction */
790 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
791 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
792 LDAP_STAILQ_NEXT(o, o_next) = NULL;
793 slap_op_free( o, NULL );
796 /* clear transaction */
797 c->c_txn_backend = NULL;
798 c->c_txn = CONN_TXN_INACTIVE;
801 /* remove pending operations */
802 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
803 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
804 LDAP_STAILQ_NEXT(o, o_next) = NULL;
805 slap_op_free( o, NULL );
809 void connection_closing( Connection *c, const char *why )
811 assert( connections != NULL );
813 assert( c->c_struct_state == SLAP_C_USED );
814 assert( c->c_conn_state != SLAP_C_INVALID );
816 /* c_mutex must be locked by caller */
818 if( c->c_conn_state != SLAP_C_CLOSING ) {
819 Debug( LDAP_DEBUG_TRACE,
820 "connection_closing: readying conn=%lu sd=%d for close\n",
821 c->c_connid, c->c_sd, 0 );
822 /* update state to closing */
823 c->c_conn_state = SLAP_C_CLOSING;
824 c->c_close_reason = why;
826 /* don't listen on this port anymore */
827 slapd_clr_read( c->c_sd, 0 );
829 /* abandon active operations */
830 connection_abandon( c );
832 /* wake write blocked operations */
833 if ( c->c_writewaiter ) {
834 ldap_pvt_thread_cond_signal( &c->c_write_cv );
835 /* ITS#4667 this may allow another thread to drop into
836 * connection_resched / connection_close before we
837 * finish, but that's OK.
839 slapd_clr_write( c->c_sd, 1 );
840 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
841 ldap_pvt_thread_mutex_lock( &c->c_write_mutex );
842 ldap_pvt_thread_mutex_lock( &c->c_mutex );
843 ldap_pvt_thread_mutex_unlock( &c->c_write_mutex );
845 slapd_clr_write( c->c_sd, 1 );
848 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
849 /* Client closed connection after doing Unbind. */
850 c->c_close_reason = NULL;
855 connection_close( Connection *c )
857 assert( connections != NULL );
860 /* ITS#4667 we may have gotten here twice */
861 if ( c->c_conn_state == SLAP_C_INVALID )
864 assert( c->c_struct_state == SLAP_C_USED );
865 assert( c->c_conn_state == SLAP_C_CLOSING );
867 /* NOTE: c_mutex should be locked by caller */
869 if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
870 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
872 Debug( LDAP_DEBUG_TRACE,
873 "connection_close: deferring conn=%lu sd=%d\n",
874 c->c_connid, c->c_sd, 0 );
878 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
879 c->c_connid, c->c_sd, 0 );
881 connection_destroy( c );
884 unsigned long connections_nextid(void)
887 assert( connections != NULL );
889 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
893 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
898 Connection* connection_first( ber_socket_t *index )
900 assert( connections != NULL );
901 assert( index != NULL );
903 ldap_pvt_thread_mutex_lock( &connections_mutex );
904 for( *index = 0; *index < dtblsize; (*index)++) {
905 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
909 ldap_pvt_thread_mutex_unlock( &connections_mutex );
911 return connection_next(NULL, index);
914 Connection* connection_next( Connection *c, ber_socket_t *index )
916 assert( connections != NULL );
917 assert( index != NULL );
918 assert( *index <= dtblsize );
920 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
924 ldap_pvt_thread_mutex_lock( &connections_mutex );
925 for(; *index < dtblsize; (*index)++) {
927 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
928 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
936 if( connections[*index].c_struct_state == SLAP_C_USED ) {
937 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
938 c = &connections[(*index)++];
939 if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
941 ldap_pvt_thread_mutex_unlock( &connections_mutex );
942 ldap_pvt_thread_mutex_lock( &c->c_mutex );
943 ldap_pvt_thread_mutex_lock( &connections_mutex );
944 if ( c->c_struct_state != SLAP_C_USED ) {
945 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
953 c_struct = connections[*index].c_struct_state;
954 if ( c_struct == SLAP_C_PENDING )
956 assert( c_struct == SLAP_C_UNUSED );
957 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
960 ldap_pvt_thread_mutex_unlock( &connections_mutex );
964 void connection_done( Connection *c )
966 assert( connections != NULL );
968 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
972 * connection_activity - handle the request operation op on connection
973 * conn. This routine figures out what kind of operation it is and
974 * calls the appropriate stub to handle it.
978 /* FIXME: returns 0 in case of failure */
979 #define INCR_OP_INITIATED(index) \
981 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
982 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated_[(index)], 1); \
983 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
985 #define INCR_OP_COMPLETED(index) \
987 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
988 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
989 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed_[(index)], 1); \
990 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
992 #else /* !SLAPD_MONITOR */
993 #define INCR_OP_INITIATED(index) do { } while (0)
994 #define INCR_OP_COMPLETED(index) \
996 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
997 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
998 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
1000 #endif /* !SLAPD_MONITOR */
1003 * NOTE: keep in sync with enum in slapd.h
1005 static BI_op_func *opfun[] = {
1019 /* Counters are per-thread, not per-connection.
1022 conn_counter_destroy( void *key, void *data )
1024 slap_counters_t **prev, *sc;
1026 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
1027 for ( prev = &slap_counters.sc_next, sc = slap_counters.sc_next; sc;
1028 prev = &sc->sc_next, sc = sc->sc_next ) {
1032 *prev = sc->sc_next;
1033 /* Copy data to main counter */
1034 ldap_pvt_mp_add( slap_counters.sc_bytes, sc->sc_bytes );
1035 ldap_pvt_mp_add( slap_counters.sc_pdu, sc->sc_pdu );
1036 ldap_pvt_mp_add( slap_counters.sc_entries, sc->sc_entries );
1037 ldap_pvt_mp_add( slap_counters.sc_refs, sc->sc_refs );
1038 ldap_pvt_mp_add( slap_counters.sc_ops_initiated, sc->sc_ops_initiated );
1039 ldap_pvt_mp_add( slap_counters.sc_ops_completed, sc->sc_ops_completed );
1040 #ifdef SLAPD_MONITOR
1041 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
1042 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_initiated_[ i ] );
1043 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_completed_[ i ] );
1045 #endif /* SLAPD_MONITOR */
1046 slap_counters_destroy( sc );
1047 ber_memfree_x( data, NULL );
1051 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
1055 conn_counter_init( Operation *op, void *ctx )
1057 slap_counters_t *sc;
1060 if ( ldap_pvt_thread_pool_getkey( ctx, conn_counter_init, &vsc, NULL ) || !vsc ) {
1061 vsc = ch_malloc( sizeof( slap_counters_t ));
1063 slap_counters_init( sc );
1064 ldap_pvt_thread_pool_setkey( ctx, conn_counter_init, vsc,
1065 conn_counter_destroy );
1067 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
1068 sc->sc_next = slap_counters.sc_next;
1069 slap_counters.sc_next = sc;
1070 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
1072 op->o_counters = vsc;
1076 connection_operation( void *ctx, void *arg_v )
1078 int rc = LDAP_OTHER;
1079 Operation *op = arg_v;
1080 SlapReply rs = {REP_RESULT};
1081 ber_tag_t tag = op->o_tag;
1082 slap_op_t opidx = SLAP_OP_LAST;
1083 Connection *conn = op->o_conn;
1084 void *memctx = NULL;
1085 void *memctx_null = NULL;
1088 conn_counter_init( op, ctx );
1089 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1090 /* FIXME: returns 0 in case of failure */
1091 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated, 1);
1092 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1094 op->o_threadctx = ctx;
1095 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1099 case LDAP_REQ_UNBIND:
1101 case LDAP_REQ_DELETE:
1102 case LDAP_REQ_MODDN:
1103 case LDAP_REQ_MODIFY:
1104 case LDAP_REQ_COMPARE:
1105 case LDAP_REQ_SEARCH:
1106 case LDAP_REQ_ABANDON:
1107 case LDAP_REQ_EXTENDED:
1110 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1111 "conn %lu unknown LDAP request 0x%lx\n",
1112 conn->c_connid, tag, 0 );
1113 op->o_tag = LBER_ERROR;
1114 rs.sr_err = LDAP_PROTOCOL_ERROR;
1115 rs.sr_text = "unknown LDAP request";
1116 send_ldap_disconnect( op, &rs );
1117 rc = SLAPD_DISCONNECT;
1118 goto operations_error;
1121 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1122 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1123 "error: SASL bind in progress (tag=%ld).\n",
1125 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1126 "SASL bind in progress" );
1127 rc = LDAP_OPERATIONS_ERROR;
1128 goto operations_error;
1132 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1133 ( tag == LDAP_REQ_ADD ) ||
1134 ( tag == LDAP_REQ_DELETE ) ||
1135 ( tag == LDAP_REQ_MODIFY ) ||
1136 ( tag == LDAP_REQ_MODRDN )))
1138 /* Disable SLAB allocator for all update operations
1139 issued inside of a transaction */
1140 op->o_tmpmemctx = NULL;
1141 op->o_tmpmfuncs = &ch_mfuncs;
1145 /* We can use Thread-Local storage for most mallocs. We can
1146 * also use TL for ber parsing, but not on Add or Modify.
1149 memsiz = ber_len( op->o_ber ) * 64;
1150 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1152 memsiz = SLAP_SLAB_SIZE;
1154 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx, 1 );
1155 op->o_tmpmemctx = memctx;
1156 op->o_tmpmfuncs = &slap_sl_mfuncs;
1157 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1158 /* Note - the ber and its buffer are already allocated from
1159 * regular memory; this only affects subsequent mallocs that
1160 * ber_scanf may invoke.
1162 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1166 opidx = slap_req2op( tag );
1167 assert( opidx != SLAP_OP_LAST );
1168 INCR_OP_INITIATED( opidx );
1169 rc = (*(opfun[opidx]))( op, &rs );
1172 if ( rc == SLAPD_DISCONNECT ) {
1175 } else if ( opidx != SLAP_OP_LAST ) {
1176 /* increment completed operations count
1177 * only if operation was initiated
1178 * and rc != SLAPD_DISCONNECT */
1179 INCR_OP_COMPLETED( opidx );
1182 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1183 if ( rc == SLAPD_ABANDON ) {
1184 op->o_cancel = SLAP_CANCEL_ACK;
1186 op->o_cancel = LDAP_TOO_LATE;
1190 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1191 op->o_cancel != SLAP_CANCEL_DONE )
1193 ldap_pvt_thread_yield();
1196 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1198 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1200 LDAP_STAILQ_REMOVE( &conn->c_ops, op, Operation, o_next);
1201 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1202 conn->c_n_ops_executing--;
1203 conn->c_n_ops_completed++;
1207 case LDAP_REQ_UNBIND:
1208 /* c_mutex is locked */
1209 connection_closing( conn,
1210 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1214 connection_resched( conn );
1215 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1216 slap_op_free( op, ctx );
1220 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1222 int connection_client_setup(
1224 ldap_pvt_thread_start_t *func,
1229 c = connection_init( s, (Listener *)&dummy_list, "", "",
1230 CONN_IS_CLIENT, 0, NULL
1231 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
1232 if ( !c ) return -1;
1234 c->c_clientfunc = func;
1235 c->c_clientarg = arg;
1237 slapd_add_internal( s, 0 );
1238 slapd_set_read( s, 1 );
1242 void connection_client_enable(
1245 slapd_set_read( s, 1 );
1248 void connection_client_stop(
1254 /* get (locked) connection */
1255 c = connection_get( s );
1257 assert( c->c_conn_state == SLAP_C_CLIENT );
1259 c->c_listener = NULL;
1260 c->c_conn_state = SLAP_C_INVALID;
1261 c->c_struct_state = SLAP_C_UNUSED;
1262 c->c_sd = AC_SOCKET_INVALID;
1263 c->c_close_reason = "?"; /* should never be needed */
1265 c->c_sb = ber_sockbuf_alloc( );
1267 ber_len_t max = sockbuf_max_incoming;
1268 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1270 slapd_remove( s, sb, 0, 1, 0 );
1272 connection_return( c );
1275 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1277 static void* connection_read_thread( void* ctx, void* argv )
1280 conn_readinfo cri = { NULL, NULL, NULL, 0 };
1281 ber_socket_t s = (long)argv;
1284 * read incoming LDAP requests. If there is more than one,
1285 * the first one is returned with new_op
1288 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1289 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1290 return (void*)(long)rc;
1293 /* execute a single queued request in the same thread */
1294 if( cri.op && !cri.nullop ) {
1295 rc = (long)connection_operation( ctx, cri.op );
1296 } else if ( cri.func ) {
1297 rc = (long)cri.func( ctx, cri.arg );
1300 return (void*)(long)rc;
1303 int connection_read_activate( ber_socket_t s )
1308 * suspend reading on this file descriptor until a connection processing
1309 * thread reads data on it. Otherwise the listener thread will repeatedly
1310 * submit the same event on it to the pool.
1312 rc = slapd_clr_read( s, 0 );
1316 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1317 connection_read_thread, (void *)(long)s );
1320 Debug( LDAP_DEBUG_ANY,
1321 "connection_read_activate(%d): submit failed (%d)\n",
1329 connection_read( ber_socket_t s, conn_readinfo *cri )
1334 assert( connections != NULL );
1336 /* get (locked) connection */
1337 c = connection_get( s );
1340 Debug( LDAP_DEBUG_ANY,
1341 "connection_read(%ld): no connection!\n",
1349 if( c->c_conn_state == SLAP_C_CLOSING ) {
1350 Debug( LDAP_DEBUG_TRACE,
1351 "connection_read(%d): closing, ignoring input for id=%lu\n",
1352 s, c->c_connid, 0 );
1353 connection_return( c );
1357 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1358 cri->func = c->c_clientfunc;
1359 cri->arg = c->c_clientarg;
1360 /* read should already be cleared */
1361 connection_return( c );
1365 Debug( LDAP_DEBUG_TRACE,
1366 "connection_read(%d): checking for input on id=%lu\n",
1367 s, c->c_connid, 0 );
1370 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1371 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1373 Debug( LDAP_DEBUG_TRACE,
1374 "connection_read(%d): TLS accept failure "
1375 "error=%d id=%lu, closing\n",
1376 s, rc, c->c_connid );
1378 c->c_needs_tls_accept = 0;
1379 /* c_mutex is locked */
1380 connection_closing( c, "TLS negotiation failure" );
1381 connection_close( c );
1382 connection_return( c );
1385 } else if ( rc == 0 ) {
1387 struct berval authid = BER_BVNULL;
1389 c->c_needs_tls_accept = 0;
1391 /* we need to let SASL know */
1392 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1394 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1395 if( c->c_tls_ssf > c->c_ssf ) {
1396 c->c_ssf = c->c_tls_ssf;
1399 rc = dnX509peerNormalize( ssl, &authid );
1400 if ( rc != LDAP_SUCCESS ) {
1401 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1402 "unable to get TLS client DN, error=%d id=%lu\n",
1403 s, rc, c->c_connid );
1405 Statslog( LDAP_DEBUG_STATS,
1406 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1407 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1408 slap_sasl_external( c, c->c_tls_ssf, &authid );
1409 if ( authid.bv_val ) free( authid.bv_val );
1412 /* if success and data is ready, fall thru to data input loop */
1413 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1415 slapd_set_read( s, 1 );
1416 connection_return( c );
1422 #ifdef HAVE_CYRUS_SASL
1423 if ( c->c_sasl_layers ) {
1424 /* If previous layer is not removed yet, give up for now */
1425 if ( !c->c_sasl_sockctx ) {
1426 slapd_set_read( s, 1 );
1427 connection_return( c );
1431 c->c_sasl_layers = 0;
1433 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1434 if( rc != LDAP_SUCCESS ) {
1435 Debug( LDAP_DEBUG_TRACE,
1436 "connection_read(%d): SASL install error "
1437 "error=%d id=%lu, closing\n",
1438 s, rc, c->c_connid );
1440 /* c_mutex is locked */
1441 connection_closing( c, "SASL layer install failure" );
1442 connection_close( c );
1443 connection_return( c );
1449 #define CONNECTION_INPUT_LOOP 1
1450 /* #define DATA_READY_LOOP 1 */
1453 /* How do we do this without getting into a busy loop ? */
1454 rc = connection_input( c, cri );
1456 #ifdef DATA_READY_LOOP
1457 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1458 #elif defined CONNECTION_INPUT_LOOP
1465 Debug( LDAP_DEBUG_CONNS,
1466 "connection_read(%d): input error=%d id=%lu, closing.\n",
1467 s, rc, c->c_connid );
1469 /* c_mutex is locked */
1470 connection_closing( c, conn_lost_str );
1471 connection_close( c );
1472 connection_return( c );
1476 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1477 slapd_set_write( s, 0 );
1480 slapd_set_read( s, 1 );
1481 connection_return( c );
1487 connection_input( Connection *conn , conn_readinfo *cri )
1495 #ifdef LDAP_CONNECTIONLESS
1502 if ( conn->c_currentber == NULL &&
1503 ( conn->c_currentber = ber_alloc()) == NULL )
1505 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1511 #ifdef LDAP_CONNECTIONLESS
1512 if ( conn->c_is_udp ) {
1513 char peername[sizeof("IP=255.255.255.255:65336")];
1515 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1516 if (len != sizeof(struct sockaddr)) return 1;
1518 sprintf( peername, "IP=%s:%d",
1519 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1520 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1521 Statslog( LDAP_DEBUG_STATS,
1522 "conn=%lu UDP request from %s (%s) accepted.\n",
1523 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1527 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1528 if ( tag != LDAP_TAG_MESSAGE ) {
1529 int err = sock_errno();
1531 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1532 /* log, close and send error */
1533 Debug( LDAP_DEBUG_TRACE,
1534 "ber_get_next on fd %d failed errno=%d (%s)\n",
1535 conn->c_sd, err, sock_errstr(err) );
1536 ber_free( conn->c_currentber, 1 );
1537 conn->c_currentber = NULL;
1544 ber = conn->c_currentber;
1545 conn->c_currentber = NULL;
1547 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1548 /* log, close and send error */
1549 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1554 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1555 /* log, close and send error */
1556 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1562 #ifdef LDAP_CONNECTIONLESS
1563 if( conn->c_is_udp ) {
1564 if( tag == LBER_OCTETSTRING ) {
1565 ber_get_stringa( ber, &cdn );
1566 tag = ber_peek_tag(ber, &len);
1568 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1569 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1576 if(tag == LDAP_REQ_BIND) {
1577 /* immediately abandon all existing operations upon BIND */
1578 connection_abandon( conn );
1582 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++, ctx );
1585 /* clear state if the connection is being reused from inactive */
1586 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1587 memset( &conn->c_pagedresults_state, 0,
1588 sizeof( conn->c_pagedresults_state ) );
1591 op->o_res_ber = NULL;
1593 #ifdef LDAP_CONNECTIONLESS
1594 if (conn->c_is_udp) {
1596 ber_str2bv( cdn, 0, 1, &op->o_dn );
1597 op->o_protocol = LDAP_VERSION2;
1599 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1600 if (op->o_res_ber == NULL) return 1;
1602 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1603 sizeof(struct sockaddr), 0 );
1605 if (rc != sizeof(struct sockaddr)) {
1606 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1610 if (op->o_protocol == LDAP_VERSION2) {
1611 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1613 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1618 #endif /* LDAP_CONNECTIONLESS */
1622 /* Don't process requests when the conn is in the middle of a
1623 * Bind, or if it's closing. Also, don't let any single conn
1624 * use up all the available threads, and don't execute if we're
1625 * currently blocked on output. And don't execute if there are
1626 * already pending ops, let them go first. Abandon operations
1627 * get exceptions to some, but not all, cases.
1631 /* Abandon and Unbind are exempt from these checks */
1632 if (conn->c_conn_state == SLAP_C_CLOSING) {
1635 } else if (conn->c_writewaiter) {
1636 defer = "awaiting write";
1638 } else if (conn->c_n_ops_pending) {
1639 defer = "pending operations";
1643 case LDAP_REQ_ABANDON:
1644 /* Unbind is exempt from these checks */
1645 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1646 defer = "too many executing";
1648 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1653 case LDAP_REQ_UNBIND:
1658 int max = conn->c_dn.bv_len
1659 ? slap_conn_max_pending_auth
1660 : slap_conn_max_pending;
1662 Debug( LDAP_DEBUG_ANY,
1663 "connection_input: conn=%lu deferring operation: %s\n",
1664 conn->c_connid, defer, 0 );
1665 conn->c_n_ops_pending++;
1666 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1667 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1670 conn->c_n_ops_executing++;
1673 * The first op will be processed in the same thread context,
1674 * as long as there is only one op total.
1675 * Subsequent ops will be submitted to the pool by
1676 * calling connection_op_activate()
1678 if ( cri->op == NULL ) {
1679 /* the first incoming request */
1680 connection_op_queue( op );
1683 if ( !cri->nullop ) {
1685 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1686 connection_operation, (void *) cri->op );
1688 connection_op_activate( op );
1693 if ( conn->c_struct_state != SLAP_C_USED ) {
1694 /* connection must have got closed underneath us */
1699 assert( conn->c_struct_state == SLAP_C_USED );
1704 connection_resched( Connection *conn )
1708 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1709 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1710 "attempting closing conn=%lu sd=%d\n",
1711 conn->c_connid, conn->c_sd, 0 );
1712 connection_close( conn );
1716 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1717 /* other states need different handling */
1721 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1722 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1724 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1725 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1727 /* pending operations should not be marked for abandonment */
1728 assert(!op->o_abandon);
1730 conn->c_n_ops_pending--;
1731 conn->c_n_ops_executing++;
1733 connection_op_activate( op );
1735 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1741 connection_init_log_prefix( Operation *op )
1743 if ( op->o_connid == (unsigned long)(-1) ) {
1744 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1745 "conn=-1 op=%lu", op->o_opid );
1748 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1749 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1753 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1755 op->o_conn->c_sasl_bindop = NULL;
1757 ch_free( op->o_callback );
1758 op->o_callback = NULL;
1760 return SLAP_CB_CONTINUE;
1763 static int connection_bind_cb( Operation *op, SlapReply *rs )
1765 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1766 if ( op->o_conn->c_conn_state == SLAP_C_BINDING )
1767 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1768 op->o_conn->c_sasl_bind_in_progress =
1769 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1771 /* Moved here from bind.c due to ITS#4158 */
1772 op->o_conn->c_sasl_bindop = NULL;
1773 if ( op->orb_method == LDAP_AUTH_SASL ) {
1774 if( rs->sr_err == LDAP_SUCCESS ) {
1775 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1776 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1777 /* edn is always normalized already */
1778 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1780 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1781 BER_BVZERO( &op->orb_edn );
1782 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1783 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1785 op->o_conn->c_sasl_ssf = op->orb_ssf;
1786 if( op->orb_ssf > op->o_conn->c_ssf ) {
1787 op->o_conn->c_ssf = op->orb_ssf;
1790 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1791 ber_len_t max = sockbuf_max_incoming_auth;
1792 ber_sockbuf_ctrl( op->o_conn->c_sb,
1793 LBER_SB_OPT_SET_MAX_INCOMING, &max );
1796 /* log authorization identity */
1797 Statslog( LDAP_DEBUG_STATS,
1798 "%s BIND dn=\"%s\" mech=%s sasl_ssf=%d ssf=%d\n",
1800 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1801 op->o_conn->c_authmech.bv_val,
1802 op->orb_ssf, op->o_conn->c_ssf );
1804 Debug( LDAP_DEBUG_TRACE,
1805 "do_bind: SASL/%s bind: dn=\"%s\" sasl_ssf=%d\n",
1806 op->o_conn->c_authmech.bv_val,
1807 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1810 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1811 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1812 free( op->o_conn->c_sasl_bind_mech.bv_val );
1813 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1817 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1819 ch_free( op->o_callback );
1820 op->o_callback = NULL;
1822 return SLAP_CB_CONTINUE;
1825 static void connection_op_queue( Operation *op )
1827 ber_tag_t tag = op->o_tag;
1829 if (tag == LDAP_REQ_BIND) {
1830 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1831 sc->sc_response = connection_bind_cb;
1832 sc->sc_cleanup = connection_bind_cleanup_cb;
1833 sc->sc_next = op->o_callback;
1834 op->o_callback = sc;
1835 op->o_conn->c_conn_state = SLAP_C_BINDING;
1838 if (!op->o_dn.bv_len) {
1839 op->o_authz = op->o_conn->c_authz;
1840 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1841 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1842 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1844 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1845 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1849 op->o_authtype = op->o_conn->c_authtype;
1850 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1852 if (!op->o_protocol) {
1853 op->o_protocol = op->o_conn->c_protocol
1854 ? op->o_conn->c_protocol : LDAP_VERSION3;
1857 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1858 op->o_protocol > LDAP_VERSION2)
1860 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1863 op->o_connid = op->o_conn->c_connid;
1864 connection_init_log_prefix( op );
1866 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1869 static int connection_op_activate( Operation *op )
1873 connection_op_queue( op );
1875 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1876 connection_operation, (void *) op );
1879 Debug( LDAP_DEBUG_ANY,
1880 "connection_op_activate: submit failed (%d) for conn=%lu\n",
1881 rc, op->o_connid, 0 );
1882 /* should move op to pending list */
1888 int connection_write(ber_socket_t s)
1893 assert( connections != NULL );
1895 slapd_clr_write( s, 0 );
1897 c = connection_get( s );
1899 Debug( LDAP_DEBUG_ANY,
1900 "connection_write(%ld): no connection!\n",
1907 Debug( LDAP_DEBUG_TRACE,
1908 "connection_write(%d): waking output for id=%lu\n",
1909 s, c->c_connid, 0 );
1910 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1912 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1913 slapd_set_read( s, 1 );
1915 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1916 slapd_set_write( s, 1 );
1919 /* If there are ops pending because of a writewaiter,
1922 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1923 if ( !c->c_writewaiter ) break;
1924 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
1926 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1927 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1929 /* pending operations should not be marked for abandonment */
1930 assert(!op->o_abandon);
1932 c->c_n_ops_pending--;
1933 c->c_n_ops_executing++;
1935 connection_op_activate( op );
1940 connection_return( c );
1945 typedef struct conn_fake_extblock {
1948 } conn_fake_extblock;
1951 connection_fake_destroy(
1955 Connection conn = {0};
1957 Opheader ohdr = {0};
1959 conn_fake_extblock *eb = data;
1962 op.o_hdr->oh_extensions = eb->eb_op;
1963 conn.c_extensions = eb->eb_conn;
1968 ber_memfree_x( eb, NULL );
1969 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, &op );
1970 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &conn );
1975 connection_fake_init(
1977 OperationBuffer *opbuf,
1980 connection_fake_init2( conn, opbuf, ctx, 1 );
1984 connection_fake_init2(
1986 OperationBuffer *opbuf,
1990 Operation *op = (Operation *) opbuf;
1992 conn->c_connid = -1;
1993 conn->c_conn_idx = -1;
1994 conn->c_send_ldap_result = slap_send_ldap_result;
1995 conn->c_send_search_entry = slap_send_search_entry;
1996 conn->c_send_search_reference = slap_send_search_reference;
1997 conn->c_listener = (Listener *)&dummy_list;
1998 conn->c_peer_domain = slap_empty_bv;
1999 conn->c_peer_name = slap_empty_bv;
2001 memset( opbuf, 0, sizeof( *opbuf ));
2002 op->o_hdr = &opbuf->ob_hdr;
2003 op->o_controls = opbuf->ob_controls;
2005 /* set memory context */
2006 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx,
2008 op->o_tmpmfuncs = &slap_sl_mfuncs;
2009 op->o_threadctx = ctx;
2010 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
2012 op->o_counters = &slap_counters;
2014 op->o_connid = op->o_conn->c_connid;
2015 connection_init_log_prefix( op );
2018 if ( slapi_plugins_used ) {
2019 conn_fake_extblock *eb;
2022 /* Use thread keys to make sure these eventually get cleaned up */
2023 if ( ldap_pvt_thread_pool_getkey( ctx, connection_fake_init, &ebx,
2025 eb = ch_malloc( sizeof( *eb ));
2026 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
2027 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
2028 eb->eb_conn = conn->c_extensions;
2029 eb->eb_op = op->o_hdr->oh_extensions;
2030 ldap_pvt_thread_pool_setkey( ctx, connection_fake_init, eb,
2031 connection_fake_destroy );
2034 conn->c_extensions = eb->eb_conn;
2035 op->o_hdr->oh_extensions = eb->eb_op;
2038 #endif /* LDAP_SLAPI */
2040 slap_op_time( &op->o_time, &op->o_tincr );
2044 connection_assign_nextid( Connection *conn )
2046 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2047 conn->c_connid = conn_nextid++;
2048 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );