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;
661 assert( connections != NULL );
663 assert( c->c_struct_state != SLAP_C_UNUSED );
664 assert( c->c_conn_state != SLAP_C_INVALID );
665 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
666 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
668 assert( c->c_txn == CONN_TXN_INACTIVE );
669 assert( c->c_txn_backend == NULL );
670 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
672 assert( c->c_writewaiter == 0);
674 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
675 connid = c->c_connid;
676 close_reason = c->c_close_reason;
678 ldap_pvt_thread_mutex_lock( &connections_mutex );
679 c->c_struct_state = SLAP_C_PENDING;
680 ldap_pvt_thread_mutex_unlock( &connections_mutex );
682 backend_connection_destroy(c);
687 c->c_activitytime = c->c_starttime = 0;
689 connection2anonymous( c );
690 c->c_listener = NULL;
692 if(c->c_peer_domain.bv_val != NULL) {
693 free(c->c_peer_domain.bv_val);
695 BER_BVZERO( &c->c_peer_domain );
696 if(c->c_peer_name.bv_val != NULL) {
697 free(c->c_peer_name.bv_val);
699 BER_BVZERO( &c->c_peer_name );
701 c->c_sasl_bind_in_progress = 0;
702 if(c->c_sasl_bind_mech.bv_val != NULL) {
703 free(c->c_sasl_bind_mech.bv_val);
705 BER_BVZERO( &c->c_sasl_bind_mech );
707 slap_sasl_close( c );
709 if ( c->c_currentber != NULL ) {
710 ber_free( c->c_currentber, 1 );
711 c->c_currentber = NULL;
716 /* call destructors, then constructors; avoids unnecessary allocation */
717 if ( slapi_plugins_used ) {
718 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
722 c->c_conn_state = SLAP_C_INVALID;
723 c->c_struct_state = SLAP_C_UNUSED;
724 c->c_close_reason = "?"; /* should never be needed */
727 c->c_sb = ber_sockbuf_alloc( );
729 ber_len_t max = sockbuf_max_incoming;
730 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
733 /* c must be fully reset by this point; when we call slapd_remove
734 * it may get immediately reused by a new connection.
736 if ( c->c_sd != AC_SOCKET_INVALID ) {
737 slapd_remove( c->c_sd, sb, 1, 0, 0 );
739 if ( close_reason == NULL ) {
740 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
741 connid, (long) c->c_sd, 0, 0, 0 );
743 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
744 connid, (long) c->c_sd, close_reason, 0, 0 );
746 c->c_sd = AC_SOCKET_INVALID;
750 int connection_state_closing( Connection *c )
752 /* c_mutex must be locked by caller */
756 assert( c->c_struct_state == SLAP_C_USED );
758 state = c->c_conn_state;
760 assert( state != SLAP_C_INVALID );
762 return state == SLAP_C_CLOSING;
765 static void connection_abandon( Connection *c )
767 /* c_mutex must be locked by caller */
769 Operation *o, *next, op = {0};
775 op.o_connid = c->c_connid;
776 op.o_tag = LDAP_REQ_ABANDON;
778 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
779 next = LDAP_STAILQ_NEXT( o, o_next );
780 op.orn_msgid = o->o_msgid;
782 op.o_bd = frontendDB;
783 frontendDB->be_abandon( &op, &rs );
787 /* remove operations in pending transaction */
788 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
789 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
790 LDAP_STAILQ_NEXT(o, o_next) = NULL;
791 slap_op_free( o, NULL );
794 /* clear transaction */
795 c->c_txn_backend = NULL;
796 c->c_txn = CONN_TXN_INACTIVE;
799 /* remove pending operations */
800 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
801 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
802 LDAP_STAILQ_NEXT(o, o_next) = NULL;
803 slap_op_free( o, NULL );
807 void connection_closing( Connection *c, const char *why )
809 assert( connections != NULL );
811 assert( c->c_struct_state == SLAP_C_USED );
812 assert( c->c_conn_state != SLAP_C_INVALID );
814 /* c_mutex must be locked by caller */
816 if( c->c_conn_state != SLAP_C_CLOSING ) {
817 Debug( LDAP_DEBUG_TRACE,
818 "connection_closing: readying conn=%lu sd=%d for close\n",
819 c->c_connid, c->c_sd, 0 );
820 /* update state to closing */
821 c->c_conn_state = SLAP_C_CLOSING;
822 c->c_close_reason = why;
824 /* don't listen on this port anymore */
825 slapd_clr_read( c->c_sd, 0 );
827 /* abandon active operations */
828 connection_abandon( c );
830 /* wake write blocked operations */
831 if ( c->c_writewaiter ) {
832 ldap_pvt_thread_cond_signal( &c->c_write_cv );
833 /* ITS#4667 this may allow another thread to drop into
834 * connection_resched / connection_close before we
835 * finish, but that's OK.
837 slapd_clr_write( c->c_sd, 1 );
838 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
839 ldap_pvt_thread_mutex_lock( &c->c_write_mutex );
840 ldap_pvt_thread_mutex_lock( &c->c_mutex );
841 ldap_pvt_thread_mutex_unlock( &c->c_write_mutex );
843 slapd_clr_write( c->c_sd, 1 );
846 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
847 /* Client closed connection after doing Unbind. */
848 c->c_close_reason = NULL;
853 connection_close( Connection *c )
855 assert( connections != NULL );
858 /* ITS#4667 we may have gotten here twice */
859 if ( c->c_conn_state == SLAP_C_INVALID )
862 assert( c->c_struct_state == SLAP_C_USED );
863 assert( c->c_conn_state == SLAP_C_CLOSING );
865 /* NOTE: c_mutex should be locked by caller */
867 if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
868 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
870 Debug( LDAP_DEBUG_TRACE,
871 "connection_close: deferring conn=%lu sd=%d\n",
872 c->c_connid, c->c_sd, 0 );
876 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
877 c->c_connid, c->c_sd, 0 );
879 connection_destroy( c );
882 unsigned long connections_nextid(void)
885 assert( connections != NULL );
887 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
891 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
896 Connection* connection_first( ber_socket_t *index )
898 assert( connections != NULL );
899 assert( index != NULL );
901 ldap_pvt_thread_mutex_lock( &connections_mutex );
902 for( *index = 0; *index < dtblsize; (*index)++) {
903 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
907 ldap_pvt_thread_mutex_unlock( &connections_mutex );
909 return connection_next(NULL, index);
912 Connection* connection_next( Connection *c, ber_socket_t *index )
914 assert( connections != NULL );
915 assert( index != NULL );
916 assert( *index <= dtblsize );
918 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
922 ldap_pvt_thread_mutex_lock( &connections_mutex );
923 for(; *index < dtblsize; (*index)++) {
925 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
926 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
934 if( connections[*index].c_struct_state == SLAP_C_USED ) {
935 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
936 c = &connections[(*index)++];
937 if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
939 ldap_pvt_thread_mutex_unlock( &connections_mutex );
940 ldap_pvt_thread_mutex_lock( &c->c_mutex );
941 ldap_pvt_thread_mutex_lock( &connections_mutex );
942 if ( c->c_struct_state != SLAP_C_USED ) {
943 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
951 c_struct = connections[*index].c_struct_state;
952 if ( c_struct == SLAP_C_PENDING )
954 assert( c_struct == SLAP_C_UNUSED );
955 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
958 ldap_pvt_thread_mutex_unlock( &connections_mutex );
962 void connection_done( Connection *c )
964 assert( connections != NULL );
966 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
970 * connection_activity - handle the request operation op on connection
971 * conn. This routine figures out what kind of operation it is and
972 * calls the appropriate stub to handle it.
976 /* FIXME: returns 0 in case of failure */
977 #define INCR_OP_INITIATED(index) \
979 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
980 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated_[(index)], 1); \
981 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
983 #define INCR_OP_COMPLETED(index) \
985 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
986 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
987 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed_[(index)], 1); \
988 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
990 #else /* !SLAPD_MONITOR */
991 #define INCR_OP_INITIATED(index) do { } while (0)
992 #define INCR_OP_COMPLETED(index) \
994 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
995 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
996 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
998 #endif /* !SLAPD_MONITOR */
1001 * NOTE: keep in sync with enum in slapd.h
1003 static BI_op_func *opfun[] = {
1017 /* Counters are per-thread, not per-connection.
1020 conn_counter_destroy( void *key, void *data )
1022 slap_counters_t **prev, *sc;
1024 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
1025 for ( prev = &slap_counters.sc_next, sc = slap_counters.sc_next; sc;
1026 prev = &sc->sc_next, sc = sc->sc_next ) {
1030 *prev = sc->sc_next;
1031 /* Copy data to main counter */
1032 ldap_pvt_mp_add( slap_counters.sc_bytes, sc->sc_bytes );
1033 ldap_pvt_mp_add( slap_counters.sc_pdu, sc->sc_pdu );
1034 ldap_pvt_mp_add( slap_counters.sc_entries, sc->sc_entries );
1035 ldap_pvt_mp_add( slap_counters.sc_refs, sc->sc_refs );
1036 ldap_pvt_mp_add( slap_counters.sc_ops_initiated, sc->sc_ops_initiated );
1037 ldap_pvt_mp_add( slap_counters.sc_ops_completed, sc->sc_ops_completed );
1038 #ifdef SLAPD_MONITOR
1039 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
1040 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_initiated_[ i ] );
1041 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_completed_[ i ] );
1043 #endif /* SLAPD_MONITOR */
1044 slap_counters_destroy( sc );
1045 ber_memfree_x( data, NULL );
1049 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
1053 conn_counter_init( Operation *op, void *ctx )
1055 slap_counters_t *sc;
1058 if ( ldap_pvt_thread_pool_getkey( ctx, conn_counter_init, &vsc, NULL ) || !vsc ) {
1059 vsc = ch_malloc( sizeof( slap_counters_t ));
1061 slap_counters_init( sc );
1062 ldap_pvt_thread_pool_setkey( ctx, conn_counter_init, vsc,
1063 conn_counter_destroy );
1065 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
1066 sc->sc_next = slap_counters.sc_next;
1067 slap_counters.sc_next = sc;
1068 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
1070 op->o_counters = vsc;
1074 connection_operation( void *ctx, void *arg_v )
1076 int rc = LDAP_OTHER;
1077 Operation *op = arg_v;
1078 SlapReply rs = {REP_RESULT};
1079 ber_tag_t tag = op->o_tag;
1080 slap_op_t opidx = SLAP_OP_LAST;
1081 Connection *conn = op->o_conn;
1082 void *memctx = NULL;
1083 void *memctx_null = NULL;
1086 conn_counter_init( op, ctx );
1087 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1088 /* FIXME: returns 0 in case of failure */
1089 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated, 1);
1090 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1092 op->o_threadctx = ctx;
1093 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1097 case LDAP_REQ_UNBIND:
1099 case LDAP_REQ_DELETE:
1100 case LDAP_REQ_MODDN:
1101 case LDAP_REQ_MODIFY:
1102 case LDAP_REQ_COMPARE:
1103 case LDAP_REQ_SEARCH:
1104 case LDAP_REQ_ABANDON:
1105 case LDAP_REQ_EXTENDED:
1108 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1109 "conn %lu unknown LDAP request 0x%lx\n",
1110 conn->c_connid, tag, 0 );
1111 op->o_tag = LBER_ERROR;
1112 rs.sr_err = LDAP_PROTOCOL_ERROR;
1113 rs.sr_text = "unknown LDAP request";
1114 send_ldap_disconnect( op, &rs );
1115 rc = SLAPD_DISCONNECT;
1116 goto operations_error;
1119 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1120 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1121 "error: SASL bind in progress (tag=%ld).\n",
1123 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1124 "SASL bind in progress" );
1125 rc = LDAP_OPERATIONS_ERROR;
1126 goto operations_error;
1130 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1131 ( tag == LDAP_REQ_ADD ) ||
1132 ( tag == LDAP_REQ_DELETE ) ||
1133 ( tag == LDAP_REQ_MODIFY ) ||
1134 ( tag == LDAP_REQ_MODRDN )))
1136 /* Disable SLAB allocator for all update operations
1137 issued inside of a transaction */
1138 op->o_tmpmemctx = NULL;
1139 op->o_tmpmfuncs = &ch_mfuncs;
1143 /* We can use Thread-Local storage for most mallocs. We can
1144 * also use TL for ber parsing, but not on Add or Modify.
1147 memsiz = ber_len( op->o_ber ) * 64;
1148 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1150 memsiz = SLAP_SLAB_SIZE;
1152 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx, 1 );
1153 op->o_tmpmemctx = memctx;
1154 op->o_tmpmfuncs = &slap_sl_mfuncs;
1155 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1156 /* Note - the ber and its buffer are already allocated from
1157 * regular memory; this only affects subsequent mallocs that
1158 * ber_scanf may invoke.
1160 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1164 opidx = slap_req2op( tag );
1165 assert( opidx != SLAP_OP_LAST );
1166 INCR_OP_INITIATED( opidx );
1167 rc = (*(opfun[opidx]))( op, &rs );
1170 if ( rc == SLAPD_DISCONNECT ) {
1173 } else if ( opidx != SLAP_OP_LAST ) {
1174 /* increment completed operations count
1175 * only if operation was initiated
1176 * and rc != SLAPD_DISCONNECT */
1177 INCR_OP_COMPLETED( opidx );
1180 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1181 if ( rc == SLAPD_ABANDON ) {
1182 op->o_cancel = SLAP_CANCEL_ACK;
1184 op->o_cancel = LDAP_TOO_LATE;
1188 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1189 op->o_cancel != SLAP_CANCEL_DONE )
1191 ldap_pvt_thread_yield();
1194 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1196 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1198 LDAP_STAILQ_REMOVE( &conn->c_ops, op, Operation, o_next);
1199 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1200 conn->c_n_ops_executing--;
1201 conn->c_n_ops_completed++;
1205 case LDAP_REQ_UNBIND:
1206 /* c_mutex is locked */
1207 connection_closing( conn,
1208 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1212 connection_resched( conn );
1213 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1214 slap_op_free( op, ctx );
1218 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1220 int connection_client_setup(
1222 ldap_pvt_thread_start_t *func,
1227 c = connection_init( s, (Listener *)&dummy_list, "", "",
1228 CONN_IS_CLIENT, 0, NULL
1229 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
1230 if ( !c ) return -1;
1232 c->c_clientfunc = func;
1233 c->c_clientarg = arg;
1235 slapd_add_internal( s, 0 );
1236 slapd_set_read( s, 1 );
1240 void connection_client_enable(
1243 slapd_set_read( s, 1 );
1246 void connection_client_stop(
1252 /* get (locked) connection */
1253 c = connection_get( s );
1255 assert( c->c_conn_state == SLAP_C_CLIENT );
1257 c->c_listener = NULL;
1258 c->c_conn_state = SLAP_C_INVALID;
1259 c->c_struct_state = SLAP_C_UNUSED;
1260 c->c_close_reason = "?"; /* should never be needed */
1262 c->c_sb = ber_sockbuf_alloc( );
1264 ber_len_t max = sockbuf_max_incoming;
1265 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1267 slapd_remove( s, sb, 0, 1, 0 );
1269 connection_return( c );
1272 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1274 static void* connection_read_thread( void* ctx, void* argv )
1277 conn_readinfo cri = { NULL, NULL, NULL, 0 };
1278 ber_socket_t s = (long)argv;
1281 * read incoming LDAP requests. If there is more than one,
1282 * the first one is returned with new_op
1285 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1286 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1287 return (void*)(long)rc;
1290 /* execute a single queued request in the same thread */
1291 if( cri.op && !cri.nullop ) {
1292 rc = (long)connection_operation( ctx, cri.op );
1293 } else if ( cri.func ) {
1294 rc = (long)cri.func( ctx, cri.arg );
1297 return (void*)(long)rc;
1300 int connection_read_activate( ber_socket_t s )
1305 * suspend reading on this file descriptor until a connection processing
1306 * thread reads data on it. Otherwise the listener thread will repeatedly
1307 * submit the same event on it to the pool.
1309 rc = slapd_clr_read( s, 0 );
1313 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1314 connection_read_thread, (void *)(long)s );
1317 Debug( LDAP_DEBUG_ANY,
1318 "connection_read_activate(%d): submit failed (%d)\n",
1326 connection_read( ber_socket_t s, conn_readinfo *cri )
1331 assert( connections != NULL );
1333 /* get (locked) connection */
1334 c = connection_get( s );
1337 Debug( LDAP_DEBUG_ANY,
1338 "connection_read(%ld): no connection!\n",
1346 if( c->c_conn_state == SLAP_C_CLOSING ) {
1347 Debug( LDAP_DEBUG_TRACE,
1348 "connection_read(%d): closing, ignoring input for id=%lu\n",
1349 s, c->c_connid, 0 );
1350 connection_return( c );
1354 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1355 cri->func = c->c_clientfunc;
1356 cri->arg = c->c_clientarg;
1357 /* read should already be cleared */
1358 connection_return( c );
1362 Debug( LDAP_DEBUG_TRACE,
1363 "connection_read(%d): checking for input on id=%lu\n",
1364 s, c->c_connid, 0 );
1367 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1368 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1370 Debug( LDAP_DEBUG_TRACE,
1371 "connection_read(%d): TLS accept failure "
1372 "error=%d id=%lu, closing\n",
1373 s, rc, c->c_connid );
1375 c->c_needs_tls_accept = 0;
1376 /* c_mutex is locked */
1377 connection_closing( c, "TLS negotiation failure" );
1378 connection_close( c );
1379 connection_return( c );
1382 } else if ( rc == 0 ) {
1384 struct berval authid = BER_BVNULL;
1386 c->c_needs_tls_accept = 0;
1388 /* we need to let SASL know */
1389 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1391 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1392 if( c->c_tls_ssf > c->c_ssf ) {
1393 c->c_ssf = c->c_tls_ssf;
1396 rc = dnX509peerNormalize( ssl, &authid );
1397 if ( rc != LDAP_SUCCESS ) {
1398 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1399 "unable to get TLS client DN, error=%d id=%lu\n",
1400 s, rc, c->c_connid );
1402 Statslog( LDAP_DEBUG_STATS,
1403 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1404 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1405 slap_sasl_external( c, c->c_tls_ssf, &authid );
1406 if ( authid.bv_val ) free( authid.bv_val );
1409 /* if success and data is ready, fall thru to data input loop */
1410 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1412 slapd_set_read( s, 1 );
1413 connection_return( c );
1419 #ifdef HAVE_CYRUS_SASL
1420 if ( c->c_sasl_layers ) {
1421 /* If previous layer is not removed yet, give up for now */
1422 if ( !c->c_sasl_sockctx ) {
1423 slapd_set_read( s, 1 );
1424 connection_return( c );
1428 c->c_sasl_layers = 0;
1430 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1431 if( rc != LDAP_SUCCESS ) {
1432 Debug( LDAP_DEBUG_TRACE,
1433 "connection_read(%d): SASL install error "
1434 "error=%d id=%lu, closing\n",
1435 s, rc, c->c_connid );
1437 /* c_mutex is locked */
1438 connection_closing( c, "SASL layer install failure" );
1439 connection_close( c );
1440 connection_return( c );
1446 #define CONNECTION_INPUT_LOOP 1
1447 /* #define DATA_READY_LOOP 1 */
1450 /* How do we do this without getting into a busy loop ? */
1451 rc = connection_input( c, cri );
1453 #ifdef DATA_READY_LOOP
1454 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1455 #elif defined CONNECTION_INPUT_LOOP
1462 Debug( LDAP_DEBUG_CONNS,
1463 "connection_read(%d): input error=%d id=%lu, closing.\n",
1464 s, rc, c->c_connid );
1466 /* c_mutex is locked */
1467 connection_closing( c, conn_lost_str );
1468 connection_close( c );
1469 connection_return( c );
1473 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1474 slapd_set_write( s, 0 );
1477 slapd_set_read( s, 1 );
1478 connection_return( c );
1484 connection_input( Connection *conn , conn_readinfo *cri )
1492 #ifdef LDAP_CONNECTIONLESS
1499 if ( conn->c_currentber == NULL &&
1500 ( conn->c_currentber = ber_alloc()) == NULL )
1502 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1508 #ifdef LDAP_CONNECTIONLESS
1509 if ( conn->c_is_udp ) {
1510 char peername[sizeof("IP=255.255.255.255:65336")];
1512 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1513 if (len != sizeof(struct sockaddr)) return 1;
1515 sprintf( peername, "IP=%s:%d",
1516 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1517 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1518 Statslog( LDAP_DEBUG_STATS,
1519 "conn=%lu UDP request from %s (%s) accepted.\n",
1520 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1524 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1525 if ( tag != LDAP_TAG_MESSAGE ) {
1526 int err = sock_errno();
1528 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1529 /* log, close and send error */
1530 Debug( LDAP_DEBUG_TRACE,
1531 "ber_get_next on fd %d failed errno=%d (%s)\n",
1532 conn->c_sd, err, sock_errstr(err) );
1533 ber_free( conn->c_currentber, 1 );
1534 conn->c_currentber = NULL;
1541 ber = conn->c_currentber;
1542 conn->c_currentber = NULL;
1544 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1545 /* log, close and send error */
1546 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1551 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1552 /* log, close and send error */
1553 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1559 #ifdef LDAP_CONNECTIONLESS
1560 if( conn->c_is_udp ) {
1561 if( tag == LBER_OCTETSTRING ) {
1562 ber_get_stringa( ber, &cdn );
1563 tag = ber_peek_tag(ber, &len);
1565 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1566 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1573 if(tag == LDAP_REQ_BIND) {
1574 /* immediately abandon all existing operations upon BIND */
1575 connection_abandon( conn );
1579 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++, ctx );
1582 /* clear state if the connection is being reused from inactive */
1583 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1584 memset( &conn->c_pagedresults_state, 0,
1585 sizeof( conn->c_pagedresults_state ) );
1588 op->o_res_ber = NULL;
1590 #ifdef LDAP_CONNECTIONLESS
1591 if (conn->c_is_udp) {
1593 ber_str2bv( cdn, 0, 1, &op->o_dn );
1594 op->o_protocol = LDAP_VERSION2;
1596 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1597 if (op->o_res_ber == NULL) return 1;
1599 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1600 sizeof(struct sockaddr), 0 );
1602 if (rc != sizeof(struct sockaddr)) {
1603 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1607 if (op->o_protocol == LDAP_VERSION2) {
1608 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1610 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1615 #endif /* LDAP_CONNECTIONLESS */
1619 /* Don't process requests when the conn is in the middle of a
1620 * Bind, or if it's closing. Also, don't let any single conn
1621 * use up all the available threads, and don't execute if we're
1622 * currently blocked on output. And don't execute if there are
1623 * already pending ops, let them go first. Abandon operations
1624 * get exceptions to some, but not all, cases.
1628 /* Abandon and Unbind are exempt from these checks */
1629 if (conn->c_conn_state == SLAP_C_CLOSING) {
1632 } else if (conn->c_writewaiter) {
1633 defer = "awaiting write";
1635 } else if (conn->c_n_ops_pending) {
1636 defer = "pending operations";
1640 case LDAP_REQ_ABANDON:
1641 /* Unbind is exempt from these checks */
1642 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1643 defer = "too many executing";
1645 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1650 case LDAP_REQ_UNBIND:
1655 int max = conn->c_dn.bv_len
1656 ? slap_conn_max_pending_auth
1657 : slap_conn_max_pending;
1659 Debug( LDAP_DEBUG_ANY,
1660 "connection_input: conn=%lu deferring operation: %s\n",
1661 conn->c_connid, defer, 0 );
1662 conn->c_n_ops_pending++;
1663 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1664 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1667 conn->c_n_ops_executing++;
1670 * The first op will be processed in the same thread context,
1671 * as long as there is only one op total.
1672 * Subsequent ops will be submitted to the pool by
1673 * calling connection_op_activate()
1675 if ( cri->op == NULL ) {
1676 /* the first incoming request */
1677 connection_op_queue( op );
1680 if ( !cri->nullop ) {
1682 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1683 connection_operation, (void *) cri->op );
1685 connection_op_activate( op );
1690 if ( conn->c_struct_state != SLAP_C_USED ) {
1691 /* connection must have got closed underneath us */
1696 assert( conn->c_struct_state == SLAP_C_USED );
1701 connection_resched( Connection *conn )
1705 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1706 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1707 "attempting closing conn=%lu sd=%d\n",
1708 conn->c_connid, conn->c_sd, 0 );
1709 connection_close( conn );
1713 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1714 /* other states need different handling */
1718 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1719 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1721 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1722 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1724 /* pending operations should not be marked for abandonment */
1725 assert(!op->o_abandon);
1727 conn->c_n_ops_pending--;
1728 conn->c_n_ops_executing++;
1730 connection_op_activate( op );
1732 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1738 connection_init_log_prefix( Operation *op )
1740 if ( op->o_connid == (unsigned long)(-1) ) {
1741 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1742 "conn=-1 op=%lu", op->o_opid );
1745 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1746 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1750 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1752 op->o_conn->c_sasl_bindop = NULL;
1754 ch_free( op->o_callback );
1755 op->o_callback = NULL;
1757 return SLAP_CB_CONTINUE;
1760 static int connection_bind_cb( Operation *op, SlapReply *rs )
1762 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1763 if ( op->o_conn->c_conn_state == SLAP_C_BINDING )
1764 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1765 op->o_conn->c_sasl_bind_in_progress =
1766 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1768 /* Moved here from bind.c due to ITS#4158 */
1769 op->o_conn->c_sasl_bindop = NULL;
1770 if ( op->orb_method == LDAP_AUTH_SASL ) {
1771 if( rs->sr_err == LDAP_SUCCESS ) {
1772 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1773 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1774 /* edn is always normalized already */
1775 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1777 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1778 BER_BVZERO( &op->orb_edn );
1779 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1780 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1782 op->o_conn->c_sasl_ssf = op->orb_ssf;
1783 if( op->orb_ssf > op->o_conn->c_ssf ) {
1784 op->o_conn->c_ssf = op->orb_ssf;
1787 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1788 ber_len_t max = sockbuf_max_incoming_auth;
1789 ber_sockbuf_ctrl( op->o_conn->c_sb,
1790 LBER_SB_OPT_SET_MAX_INCOMING, &max );
1793 /* log authorization identity */
1794 Statslog( LDAP_DEBUG_STATS,
1795 "%s BIND dn=\"%s\" mech=%s sasl_ssf=%d ssf=%d\n",
1797 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1798 op->o_conn->c_authmech.bv_val,
1799 op->orb_ssf, op->o_conn->c_ssf );
1801 Debug( LDAP_DEBUG_TRACE,
1802 "do_bind: SASL/%s bind: dn=\"%s\" sasl_ssf=%d\n",
1803 op->o_conn->c_authmech.bv_val,
1804 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1807 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1808 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1809 free( op->o_conn->c_sasl_bind_mech.bv_val );
1810 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1814 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1816 ch_free( op->o_callback );
1817 op->o_callback = NULL;
1819 return SLAP_CB_CONTINUE;
1822 static void connection_op_queue( Operation *op )
1824 ber_tag_t tag = op->o_tag;
1826 if (tag == LDAP_REQ_BIND) {
1827 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1828 sc->sc_response = connection_bind_cb;
1829 sc->sc_cleanup = connection_bind_cleanup_cb;
1830 sc->sc_next = op->o_callback;
1831 op->o_callback = sc;
1832 op->o_conn->c_conn_state = SLAP_C_BINDING;
1835 if (!op->o_dn.bv_len) {
1836 op->o_authz = op->o_conn->c_authz;
1837 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1838 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1839 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1841 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1842 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1846 op->o_authtype = op->o_conn->c_authtype;
1847 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1849 if (!op->o_protocol) {
1850 op->o_protocol = op->o_conn->c_protocol
1851 ? op->o_conn->c_protocol : LDAP_VERSION3;
1854 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1855 op->o_protocol > LDAP_VERSION2)
1857 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1860 op->o_connid = op->o_conn->c_connid;
1861 connection_init_log_prefix( op );
1863 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1866 static int connection_op_activate( Operation *op )
1870 connection_op_queue( op );
1872 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1873 connection_operation, (void *) op );
1876 Debug( LDAP_DEBUG_ANY,
1877 "connection_op_activate: submit failed (%d) for conn=%lu\n",
1878 rc, op->o_connid, 0 );
1879 /* should move op to pending list */
1885 int connection_write(ber_socket_t s)
1890 assert( connections != NULL );
1892 slapd_clr_write( s, 0 );
1894 c = connection_get( s );
1896 Debug( LDAP_DEBUG_ANY,
1897 "connection_write(%ld): no connection!\n",
1904 Debug( LDAP_DEBUG_TRACE,
1905 "connection_write(%d): waking output for id=%lu\n",
1906 s, c->c_connid, 0 );
1907 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1909 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1910 slapd_set_read( s, 1 );
1912 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1913 slapd_set_write( s, 1 );
1916 /* If there are ops pending because of a writewaiter,
1919 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1920 if ( !c->c_writewaiter ) break;
1921 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
1923 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1924 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1926 /* pending operations should not be marked for abandonment */
1927 assert(!op->o_abandon);
1929 c->c_n_ops_pending--;
1930 c->c_n_ops_executing++;
1932 connection_op_activate( op );
1937 connection_return( c );
1942 typedef struct conn_fake_extblock {
1945 } conn_fake_extblock;
1948 connection_fake_destroy(
1952 Connection conn = {0};
1954 Opheader ohdr = {0};
1956 conn_fake_extblock *eb = data;
1959 op.o_hdr->oh_extensions = eb->eb_op;
1960 conn.c_extensions = eb->eb_conn;
1965 ber_memfree_x( eb, NULL );
1966 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, &op );
1967 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &conn );
1972 connection_fake_init(
1974 OperationBuffer *opbuf,
1977 connection_fake_init2( conn, opbuf, ctx, 1 );
1981 connection_fake_init2(
1983 OperationBuffer *opbuf,
1987 Operation *op = (Operation *) opbuf;
1989 conn->c_connid = -1;
1990 conn->c_conn_idx = -1;
1991 conn->c_send_ldap_result = slap_send_ldap_result;
1992 conn->c_send_search_entry = slap_send_search_entry;
1993 conn->c_send_search_reference = slap_send_search_reference;
1994 conn->c_listener = (Listener *)&dummy_list;
1995 conn->c_peer_domain = slap_empty_bv;
1996 conn->c_peer_name = slap_empty_bv;
1998 memset( opbuf, 0, sizeof( *opbuf ));
1999 op->o_hdr = &opbuf->ob_hdr;
2000 op->o_controls = opbuf->ob_controls;
2002 /* set memory context */
2003 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx,
2005 op->o_tmpmfuncs = &slap_sl_mfuncs;
2006 op->o_threadctx = ctx;
2007 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
2009 op->o_counters = &slap_counters;
2011 op->o_connid = op->o_conn->c_connid;
2012 connection_init_log_prefix( op );
2015 if ( slapi_plugins_used ) {
2016 conn_fake_extblock *eb;
2019 /* Use thread keys to make sure these eventually get cleaned up */
2020 if ( ldap_pvt_thread_pool_getkey( ctx, connection_fake_init, &ebx,
2022 eb = ch_malloc( sizeof( *eb ));
2023 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
2024 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
2025 eb->eb_conn = conn->c_extensions;
2026 eb->eb_op = op->o_hdr->oh_extensions;
2027 ldap_pvt_thread_pool_setkey( ctx, connection_fake_init, eb,
2028 connection_fake_destroy );
2031 conn->c_extensions = eb->eb_conn;
2032 op->o_hdr->oh_extensions = eb->eb_op;
2035 #endif /* LDAP_SLAPI */
2037 slap_op_time( &op->o_time, &op->o_tincr );
2041 connection_assign_nextid( Connection *conn )
2043 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2044 conn->c_connid = conn_nextid++;
2045 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );