2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2010 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>
42 #ifdef LDAP_CONNECTIONLESS
43 #include "../../libraries/liblber/lber-int.h" /* ber_int_sb_read() */
47 #include "slapi/slapi.h"
50 /* protected by connections_mutex */
51 static ldap_pvt_thread_mutex_t connections_mutex;
52 static Connection *connections = NULL;
54 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
55 static unsigned long conn_nextid = SLAPD_SYNC_SYNCCONN_OFFSET;
57 static const char conn_lost_str[] = "connection lost";
60 connection_state2str( int state )
63 case SLAP_C_INVALID: return "!";
64 case SLAP_C_INACTIVE: return "|";
65 case SLAP_C_CLOSING: return "C";
66 case SLAP_C_ACTIVE: return "";
67 case SLAP_C_BINDING: return "B";
68 case SLAP_C_CLIENT: return "L";
74 static Connection* connection_get( ber_socket_t s );
76 typedef struct conn_readinfo {
78 ldap_pvt_thread_start_t *func;
84 static int connection_input( Connection *c, conn_readinfo *cri );
85 static void connection_close( Connection *c );
87 static int connection_op_activate( Operation *op );
88 static void connection_op_queue( Operation *op );
89 static int connection_resched( Connection *conn );
90 static void connection_abandon( Connection *conn );
91 static void connection_destroy( Connection *c );
93 static ldap_pvt_thread_start_t connection_operation;
96 * Initialize connection management infrastructure.
98 int connections_init(void)
102 assert( connections == NULL );
104 if( connections != NULL) {
105 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
110 /* should check return of every call */
111 ldap_pvt_thread_mutex_init( &connections_mutex );
112 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
114 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
116 if( connections == NULL ) {
117 Debug( LDAP_DEBUG_ANY, "connections_init: "
118 "allocation (%d*%ld) of connection array failed\n",
119 dtblsize, (long) sizeof(Connection), 0 );
123 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
124 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
126 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
129 * per entry initialization of the Connection array initialization
130 * will be done by connection_init()
137 * Destroy connection management infrastructure.
140 int connections_destroy(void)
144 /* should check return of every call */
146 if( connections == NULL) {
147 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
152 for ( i = 0; i < dtblsize; i++ ) {
153 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
154 ber_sockbuf_free( connections[i].c_sb );
155 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
156 ldap_pvt_thread_mutex_destroy( &connections[i].c_write1_mutex );
157 ldap_pvt_thread_mutex_destroy( &connections[i].c_write2_mutex );
158 ldap_pvt_thread_cond_destroy( &connections[i].c_write1_cv );
159 ldap_pvt_thread_cond_destroy( &connections[i].c_write2_cv );
161 if ( slapi_plugins_used ) {
162 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
172 ldap_pvt_thread_mutex_destroy( &connections_mutex );
173 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
178 * shutdown all connections
180 int connections_shutdown(void)
184 for ( i = 0; i < dtblsize; i++ ) {
185 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
186 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
187 if( connections[i].c_struct_state == SLAP_C_USED ) {
189 /* give persistent clients a chance to cleanup */
190 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
191 ldap_pvt_thread_pool_submit( &connection_pool,
192 connections[i].c_clientfunc, connections[i].c_clientarg );
194 /* c_mutex is locked */
195 connection_closing( &connections[i], "slapd shutdown" );
196 connection_close( &connections[i] );
199 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
207 * Timeout idle connections.
209 int connections_timeout_idle(time_t now)
211 int i = 0, writers = 0;
216 old = slapd_get_writetime();
218 for( c = connection_first( &connindex );
220 c = connection_next( c, &connindex ) )
222 /* Don't timeout a slow-running request or a persistent
223 * outbound connection. But if it has a writewaiter, see
224 * if the waiter has been there too long.
226 if(( c->c_n_ops_executing && !c->c_writewaiter)
227 || c->c_conn_state == SLAP_C_CLIENT ) {
231 if( global_idletimeout &&
232 difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
234 connection_closing( c, "idletimeout" );
235 connection_close( c );
239 if ( c->c_writewaiter && global_writetimeout ) {
241 if( difftime( c->c_activitytime+global_writetimeout, now) < 0 ) {
243 connection_closing( c, "writetimeout" );
244 connection_close( c );
249 connection_done( c );
250 if ( old && !writers )
251 slapd_clr_writetime( old );
256 static Connection* connection_get( ber_socket_t s )
260 Debug( LDAP_DEBUG_ARGS,
261 "connection_get(%ld)\n",
264 assert( connections != NULL );
266 if(s == AC_SOCKET_INVALID) return NULL;
268 assert( s < dtblsize );
272 ldap_pvt_thread_mutex_lock( &c->c_mutex );
274 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
276 if( c->c_struct_state != SLAP_C_USED ) {
277 /* connection must have been closed due to resched */
279 assert( c->c_conn_state == SLAP_C_INVALID );
280 assert( c->c_sd == AC_SOCKET_INVALID );
282 Debug( LDAP_DEBUG_CONNS,
283 "connection_get(%d): connection not used\n",
286 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
290 Debug( LDAP_DEBUG_TRACE,
291 "connection_get(%d): got connid=%lu\n",
296 assert( c->c_struct_state == SLAP_C_USED );
297 assert( c->c_conn_state != SLAP_C_INVALID );
298 assert( c->c_sd != AC_SOCKET_INVALID );
300 #ifndef SLAPD_MONITOR
301 if ( global_idletimeout > 0 )
302 #endif /* ! SLAPD_MONITOR */
304 c->c_activitytime = slap_get_time();
311 static void connection_return( Connection *c )
313 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
316 Connection * connection_init(
320 const char* peername,
323 struct berval *authid
324 LDAP_PF_LOCAL_SENDMSG_ARG(struct berval *peerbv))
329 ber_socket_t sfd = SLAP_FD2SOCK(s);
331 assert( connections != NULL );
333 assert( listener != NULL );
334 assert( dnsname != NULL );
335 assert( peername != NULL );
338 assert( !( flags & CONN_IS_TLS ));
341 if( s == AC_SOCKET_INVALID ) {
342 Debug( LDAP_DEBUG_ANY,
343 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
348 assert( s < dtblsize );
350 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
353 assert( c->c_struct_state == SLAP_C_UNUSED );
357 c->c_send_ldap_result = slap_send_ldap_result;
358 c->c_send_search_entry = slap_send_search_entry;
359 c->c_send_search_reference = slap_send_search_reference;
360 c->c_send_ldap_extended = slap_send_ldap_extended;
361 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
363 BER_BVZERO( &c->c_authmech );
364 BER_BVZERO( &c->c_dn );
365 BER_BVZERO( &c->c_ndn );
367 c->c_listener = NULL;
368 BER_BVZERO( &c->c_peer_domain );
369 BER_BVZERO( &c->c_peer_name );
371 LDAP_STAILQ_INIT(&c->c_ops);
372 LDAP_STAILQ_INIT(&c->c_pending_ops);
375 c->c_txn = CONN_TXN_INACTIVE;
376 c->c_txn_backend = NULL;
377 LDAP_STAILQ_INIT(&c->c_txn_ops);
380 BER_BVZERO( &c->c_sasl_bind_mech );
382 c->c_sasl_authctx = NULL;
383 c->c_sasl_sockctx = NULL;
384 c->c_sasl_extra = NULL;
385 c->c_sasl_bindop = NULL;
387 c->c_sb = ber_sockbuf_alloc( );
390 ber_len_t max = sockbuf_max_incoming;
391 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
394 c->c_currentber = NULL;
396 /* should check status of thread calls */
397 ldap_pvt_thread_mutex_init( &c->c_mutex );
398 ldap_pvt_thread_mutex_init( &c->c_write1_mutex );
399 ldap_pvt_thread_mutex_init( &c->c_write2_mutex );
400 ldap_pvt_thread_cond_init( &c->c_write1_cv );
401 ldap_pvt_thread_cond_init( &c->c_write2_cv );
404 if ( slapi_plugins_used ) {
405 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
410 ldap_pvt_thread_mutex_lock( &c->c_mutex );
412 assert( BER_BVISNULL( &c->c_authmech ) );
413 assert( BER_BVISNULL( &c->c_dn ) );
414 assert( BER_BVISNULL( &c->c_ndn ) );
415 assert( c->c_listener == NULL );
416 assert( BER_BVISNULL( &c->c_peer_domain ) );
417 assert( BER_BVISNULL( &c->c_peer_name ) );
418 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
419 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
421 assert( c->c_txn == CONN_TXN_INACTIVE );
422 assert( c->c_txn_backend == NULL );
423 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
425 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
426 assert( c->c_sasl_done == 0 );
427 assert( c->c_sasl_authctx == NULL );
428 assert( c->c_sasl_sockctx == NULL );
429 assert( c->c_sasl_extra == NULL );
430 assert( c->c_sasl_bindop == NULL );
431 assert( c->c_currentber == NULL );
432 assert( c->c_writewaiter == 0);
433 assert( c->c_writers == 0);
435 c->c_listener = listener;
438 if ( flags & CONN_IS_CLIENT ) {
440 ldap_pvt_thread_mutex_lock( &connections_mutex );
441 c->c_conn_state = SLAP_C_CLIENT;
442 c->c_struct_state = SLAP_C_USED;
443 ldap_pvt_thread_mutex_unlock( &connections_mutex );
444 c->c_close_reason = "?"; /* should never be needed */
445 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &sfd );
446 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
451 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
452 ber_str2bv( peername, 0, 1, &c->c_peer_name );
454 c->c_n_ops_received = 0;
455 c->c_n_ops_executing = 0;
456 c->c_n_ops_pending = 0;
457 c->c_n_ops_completed = 0;
463 /* set to zero until bind, implies LDAP_VERSION3 */
466 #ifndef SLAPD_MONITOR
467 if ( global_idletimeout > 0 )
468 #endif /* ! SLAPD_MONITOR */
470 c->c_activitytime = c->c_starttime = slap_get_time();
473 #ifdef LDAP_CONNECTIONLESS
475 if( flags & CONN_IS_UDP ) {
478 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
479 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
481 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
482 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
483 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
484 LBER_SBIOD_LEVEL_PROVIDER, NULL );
486 #endif /* LDAP_CONNECTIONLESS */
488 if ( flags & CONN_IS_IPC ) {
490 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
491 LBER_SBIOD_LEVEL_PROVIDER, (void*)"ipc_" );
493 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_fd,
494 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
495 #ifdef LDAP_PF_LOCAL_SENDMSG
496 if ( !BER_BVISEMPTY( peerbv ))
497 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_UNGET_BUF, peerbv );
500 #endif /* LDAP_PF_LOCAL */
503 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
504 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
506 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
507 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
511 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
512 INT_MAX, (void*)"ldap_" );
515 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
516 c /* non-NULL */ ) < 0 )
518 Debug( LDAP_DEBUG_ANY,
519 "connection_init(%d, %s): set nonblocking failed\n",
520 s, c->c_peer_name.bv_val, 0 );
523 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
524 id = c->c_connid = conn_nextid++;
525 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
527 ldap_pvt_thread_mutex_lock( &connections_mutex );
528 c->c_conn_state = SLAP_C_INACTIVE;
529 c->c_struct_state = SLAP_C_USED;
530 ldap_pvt_thread_mutex_unlock( &connections_mutex );
531 c->c_close_reason = "?"; /* should never be needed */
533 c->c_ssf = c->c_transport_ssf = ssf;
537 if ( flags & CONN_IS_TLS ) {
539 c->c_needs_tls_accept = 1;
542 c->c_needs_tls_accept = 0;
546 slap_sasl_open( c, 0 );
547 slap_sasl_external( c, ssf, authid );
549 slapd_add_internal( s, 1 );
550 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
552 backend_connection_init(c);
557 void connection2anonymous( Connection *c )
559 assert( connections != NULL );
563 ber_len_t max = sockbuf_max_incoming;
564 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
567 if ( !BER_BVISNULL( &c->c_authmech ) ) {
568 ch_free(c->c_authmech.bv_val);
570 BER_BVZERO( &c->c_authmech );
572 if ( !BER_BVISNULL( &c->c_dn ) ) {
573 ch_free(c->c_dn.bv_val);
575 BER_BVZERO( &c->c_dn );
577 if ( !BER_BVISNULL( &c->c_ndn ) ) {
578 ch_free(c->c_ndn.bv_val);
580 BER_BVZERO( &c->c_ndn );
582 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
583 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
585 BER_BVZERO( &c->c_sasl_authz_dn );
587 c->c_authz_backend = NULL;
591 connection_destroy( Connection *c )
593 unsigned long connid;
594 const char *close_reason;
598 assert( connections != NULL );
600 assert( c->c_struct_state != SLAP_C_UNUSED );
601 assert( c->c_conn_state != SLAP_C_INVALID );
602 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
603 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
605 assert( c->c_txn == CONN_TXN_INACTIVE );
606 assert( c->c_txn_backend == NULL );
607 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
609 assert( c->c_writewaiter == 0);
610 assert( c->c_writers == 0);
612 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
613 connid = c->c_connid;
614 close_reason = c->c_close_reason;
616 ldap_pvt_thread_mutex_lock( &connections_mutex );
617 c->c_struct_state = SLAP_C_PENDING;
618 ldap_pvt_thread_mutex_unlock( &connections_mutex );
620 backend_connection_destroy(c);
625 c->c_activitytime = c->c_starttime = 0;
627 connection2anonymous( c );
628 c->c_listener = NULL;
630 if(c->c_peer_domain.bv_val != NULL) {
631 free(c->c_peer_domain.bv_val);
633 BER_BVZERO( &c->c_peer_domain );
634 if(c->c_peer_name.bv_val != NULL) {
635 free(c->c_peer_name.bv_val);
637 BER_BVZERO( &c->c_peer_name );
639 c->c_sasl_bind_in_progress = 0;
640 if(c->c_sasl_bind_mech.bv_val != NULL) {
641 free(c->c_sasl_bind_mech.bv_val);
643 BER_BVZERO( &c->c_sasl_bind_mech );
645 slap_sasl_close( c );
647 if ( c->c_currentber != NULL ) {
648 ber_free( c->c_currentber, 1 );
649 c->c_currentber = NULL;
654 /* call destructors, then constructors; avoids unnecessary allocation */
655 if ( slapi_plugins_used ) {
656 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
661 c->c_sd = AC_SOCKET_INVALID;
662 c->c_conn_state = SLAP_C_INVALID;
663 c->c_struct_state = SLAP_C_UNUSED;
664 c->c_close_reason = "?"; /* should never be needed */
667 c->c_sb = ber_sockbuf_alloc( );
669 ber_len_t max = sockbuf_max_incoming;
670 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
673 /* c must be fully reset by this point; when we call slapd_remove
674 * it may get immediately reused by a new connection.
676 if ( sd != AC_SOCKET_INVALID ) {
677 slapd_remove( sd, sb, 1, 0, 0 );
679 if ( close_reason == NULL ) {
680 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
681 connid, (long) sd, 0, 0, 0 );
683 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
684 connid, (long) sd, close_reason, 0, 0 );
689 int connection_valid( Connection *c )
691 /* c_mutex must be locked by caller */
695 return c->c_struct_state == SLAP_C_USED &&
696 c->c_conn_state >= SLAP_C_ACTIVE &&
697 c->c_conn_state <= SLAP_C_CLIENT;
700 static void connection_abandon( Connection *c )
702 /* c_mutex must be locked by caller */
704 Operation *o, *next, op = {0};
710 op.o_connid = c->c_connid;
711 op.o_tag = LDAP_REQ_ABANDON;
713 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
714 next = LDAP_STAILQ_NEXT( o, o_next );
715 op.orn_msgid = o->o_msgid;
717 op.o_bd = frontendDB;
718 frontendDB->be_abandon( &op, &rs );
722 /* remove operations in pending transaction */
723 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
724 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
725 LDAP_STAILQ_NEXT(o, o_next) = NULL;
726 slap_op_free( o, NULL );
729 /* clear transaction */
730 c->c_txn_backend = NULL;
731 c->c_txn = CONN_TXN_INACTIVE;
734 /* remove pending operations */
735 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
736 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
737 LDAP_STAILQ_NEXT(o, o_next) = NULL;
738 slap_op_free( o, NULL );
743 connection_wake_writers( Connection *c )
745 /* wake write blocked operations */
746 ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
747 if ( c->c_writers > 0 ) {
748 c->c_writers = -c->c_writers;
749 ldap_pvt_thread_cond_broadcast( &c->c_write1_cv );
750 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
751 if ( c->c_writewaiter ) {
752 ldap_pvt_thread_mutex_lock( &c->c_write2_mutex );
753 ldap_pvt_thread_cond_signal( &c->c_write2_cv );
754 slapd_clr_write( c->c_sd, 1 );
755 ldap_pvt_thread_mutex_unlock( &c->c_write2_mutex );
757 ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
758 while ( c->c_writers ) {
759 ldap_pvt_thread_cond_wait( &c->c_write1_cv, &c->c_write1_mutex );
761 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
763 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
764 slapd_clr_write( c->c_sd, 1 );
768 void connection_closing( Connection *c, const char *why )
770 assert( connections != NULL );
773 if ( c->c_struct_state != SLAP_C_USED ) return;
775 assert( c->c_conn_state != SLAP_C_INVALID );
777 /* c_mutex must be locked by caller */
779 if( c->c_conn_state != SLAP_C_CLOSING ) {
780 Debug( LDAP_DEBUG_CONNS,
781 "connection_closing: readying conn=%lu sd=%d for close\n",
782 c->c_connid, c->c_sd, 0 );
783 /* update state to closing */
784 c->c_conn_state = SLAP_C_CLOSING;
785 c->c_close_reason = why;
787 /* don't listen on this port anymore */
788 slapd_clr_read( c->c_sd, 0 );
790 /* abandon active operations */
791 connection_abandon( c );
793 /* wake write blocked operations */
794 connection_wake_writers( c );
796 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
797 /* Client closed connection after doing Unbind. */
798 c->c_close_reason = NULL;
803 connection_close( Connection *c )
805 assert( connections != NULL );
808 if ( c->c_struct_state != SLAP_C_USED ) return;
810 assert( c->c_conn_state == SLAP_C_CLOSING );
812 /* NOTE: c_mutex should be locked by caller */
814 if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
815 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
817 Debug( LDAP_DEBUG_CONNS,
818 "connection_close: deferring conn=%lu sd=%d\n",
819 c->c_connid, c->c_sd, 0 );
823 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
824 c->c_connid, c->c_sd, 0 );
826 connection_destroy( c );
829 unsigned long connections_nextid(void)
832 assert( connections != NULL );
834 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
838 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
843 Connection* connection_first( ber_socket_t *index )
845 assert( connections != NULL );
846 assert( index != NULL );
848 ldap_pvt_thread_mutex_lock( &connections_mutex );
849 for( *index = 0; *index < dtblsize; (*index)++) {
850 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
854 ldap_pvt_thread_mutex_unlock( &connections_mutex );
856 return connection_next(NULL, index);
859 Connection* connection_next( Connection *c, ber_socket_t *index )
861 assert( connections != NULL );
862 assert( index != NULL );
863 assert( *index <= dtblsize );
865 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
869 ldap_pvt_thread_mutex_lock( &connections_mutex );
870 for(; *index < dtblsize; (*index)++) {
872 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
873 /* FIXME: accessing c_conn_state without locking c_mutex */
874 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
878 if( connections[*index].c_struct_state == SLAP_C_USED ) {
879 c = &connections[(*index)++];
880 if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
882 ldap_pvt_thread_mutex_unlock( &connections_mutex );
883 ldap_pvt_thread_mutex_lock( &c->c_mutex );
884 ldap_pvt_thread_mutex_lock( &connections_mutex );
885 if ( c->c_struct_state != SLAP_C_USED ) {
886 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
891 assert( c->c_conn_state != SLAP_C_INVALID );
895 c_struct = connections[*index].c_struct_state;
896 if ( c_struct == SLAP_C_PENDING )
898 assert( c_struct == SLAP_C_UNUSED );
899 /* FIXME: accessing c_conn_state without locking c_mutex */
900 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
903 ldap_pvt_thread_mutex_unlock( &connections_mutex );
907 void connection_done( Connection *c )
909 assert( connections != NULL );
911 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
915 * connection_activity - handle the request operation op on connection
916 * conn. This routine figures out what kind of operation it is and
917 * calls the appropriate stub to handle it.
921 /* FIXME: returns 0 in case of failure */
922 #define INCR_OP_INITIATED(index) \
924 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
925 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated_[(index)], 1); \
926 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
928 #define INCR_OP_COMPLETED(index) \
930 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
931 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
932 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed_[(index)], 1); \
933 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
935 #else /* !SLAPD_MONITOR */
936 #define INCR_OP_INITIATED(index) do { } while (0)
937 #define INCR_OP_COMPLETED(index) \
939 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
940 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
941 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
943 #endif /* !SLAPD_MONITOR */
946 * NOTE: keep in sync with enum in slapd.h
948 static BI_op_func *opfun[] = {
962 /* Counters are per-thread, not per-connection.
965 conn_counter_destroy( void *key, void *data )
967 slap_counters_t **prev, *sc;
969 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
970 for ( prev = &slap_counters.sc_next, sc = slap_counters.sc_next; sc;
971 prev = &sc->sc_next, sc = sc->sc_next ) {
976 /* Copy data to main counter */
977 ldap_pvt_mp_add( slap_counters.sc_bytes, sc->sc_bytes );
978 ldap_pvt_mp_add( slap_counters.sc_pdu, sc->sc_pdu );
979 ldap_pvt_mp_add( slap_counters.sc_entries, sc->sc_entries );
980 ldap_pvt_mp_add( slap_counters.sc_refs, sc->sc_refs );
981 ldap_pvt_mp_add( slap_counters.sc_ops_initiated, sc->sc_ops_initiated );
982 ldap_pvt_mp_add( slap_counters.sc_ops_completed, sc->sc_ops_completed );
984 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
985 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_initiated_[ i ] );
986 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_completed_[ i ] );
988 #endif /* SLAPD_MONITOR */
989 slap_counters_destroy( sc );
990 ber_memfree_x( data, NULL );
994 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
998 conn_counter_init( Operation *op, void *ctx )
1000 slap_counters_t *sc;
1003 if ( ldap_pvt_thread_pool_getkey(
1004 ctx, (void *)conn_counter_init, &vsc, NULL ) || !vsc ) {
1005 vsc = ch_malloc( sizeof( slap_counters_t ));
1007 slap_counters_init( sc );
1008 ldap_pvt_thread_pool_setkey( ctx, (void*)conn_counter_init, vsc,
1009 conn_counter_destroy, NULL, NULL );
1011 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
1012 sc->sc_next = slap_counters.sc_next;
1013 slap_counters.sc_next = sc;
1014 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
1016 op->o_counters = vsc;
1020 connection_operation( void *ctx, void *arg_v )
1022 int rc = LDAP_OTHER, cancel;
1023 Operation *op = arg_v;
1024 SlapReply rs = {REP_RESULT};
1025 ber_tag_t tag = op->o_tag;
1026 slap_op_t opidx = SLAP_OP_LAST;
1027 Connection *conn = op->o_conn;
1028 void *memctx = NULL;
1029 void *memctx_null = NULL;
1032 conn_counter_init( op, ctx );
1033 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1034 /* FIXME: returns 0 in case of failure */
1035 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated, 1);
1036 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1038 op->o_threadctx = ctx;
1039 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1043 case LDAP_REQ_UNBIND:
1045 case LDAP_REQ_DELETE:
1046 case LDAP_REQ_MODDN:
1047 case LDAP_REQ_MODIFY:
1048 case LDAP_REQ_COMPARE:
1049 case LDAP_REQ_SEARCH:
1050 case LDAP_REQ_ABANDON:
1051 case LDAP_REQ_EXTENDED:
1054 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1055 "conn %lu unknown LDAP request 0x%lx\n",
1056 conn->c_connid, tag, 0 );
1057 op->o_tag = LBER_ERROR;
1058 rs.sr_err = LDAP_PROTOCOL_ERROR;
1059 rs.sr_text = "unknown LDAP request";
1060 send_ldap_disconnect( op, &rs );
1061 rc = SLAPD_DISCONNECT;
1062 goto operations_error;
1065 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1066 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1067 "error: SASL bind in progress (tag=%ld).\n",
1069 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1070 "SASL bind in progress" );
1071 rc = LDAP_OPERATIONS_ERROR;
1072 goto operations_error;
1076 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1077 ( tag == LDAP_REQ_ADD ) ||
1078 ( tag == LDAP_REQ_DELETE ) ||
1079 ( tag == LDAP_REQ_MODIFY ) ||
1080 ( tag == LDAP_REQ_MODRDN )))
1082 /* Disable SLAB allocator for all update operations
1083 issued inside of a transaction */
1084 op->o_tmpmemctx = NULL;
1085 op->o_tmpmfuncs = &ch_mfuncs;
1089 /* We can use Thread-Local storage for most mallocs. We can
1090 * also use TL for ber parsing, but not on Add or Modify.
1093 memsiz = ber_len( op->o_ber ) * 64;
1094 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1096 memsiz = SLAP_SLAB_SIZE;
1098 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx, 1 );
1099 op->o_tmpmemctx = memctx;
1100 op->o_tmpmfuncs = &slap_sl_mfuncs;
1101 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1102 /* Note - the ber and its buffer are already allocated from
1103 * regular memory; this only affects subsequent mallocs that
1104 * ber_scanf may invoke.
1106 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1110 opidx = slap_req2op( tag );
1111 assert( opidx != SLAP_OP_LAST );
1112 INCR_OP_INITIATED( opidx );
1113 rc = (*(opfun[opidx]))( op, &rs );
1116 if ( rc == SLAPD_DISCONNECT ) {
1119 } else if ( opidx != SLAP_OP_LAST ) {
1120 /* increment completed operations count
1121 * only if operation was initiated
1122 * and rc != SLAPD_DISCONNECT */
1123 INCR_OP_COMPLETED( opidx );
1126 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1128 if ( opidx == SLAP_OP_BIND && conn->c_conn_state == SLAP_C_BINDING )
1129 conn->c_conn_state = SLAP_C_ACTIVE;
1131 cancel = op->o_cancel;
1132 if ( cancel != SLAP_CANCEL_NONE && cancel != SLAP_CANCEL_DONE ) {
1133 if ( cancel == SLAP_CANCEL_REQ ) {
1134 op->o_cancel = rc == SLAPD_ABANDON
1135 ? SLAP_CANCEL_ACK : LDAP_TOO_LATE;
1139 /* Fake a cond_wait with thread_yield, then
1140 * verify the result properly mutex-protected.
1142 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1144 ldap_pvt_thread_yield();
1145 } while ( (cancel = op->o_cancel) != SLAP_CANCEL_NONE
1146 && cancel != SLAP_CANCEL_DONE );
1147 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1148 } while ( (cancel = op->o_cancel) != SLAP_CANCEL_NONE
1149 && cancel != SLAP_CANCEL_DONE );
1152 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1154 LDAP_STAILQ_REMOVE( &conn->c_ops, op, Operation, o_next);
1155 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1156 conn->c_n_ops_executing--;
1157 conn->c_n_ops_completed++;
1161 case LDAP_REQ_UNBIND:
1162 /* c_mutex is locked */
1163 connection_closing( conn,
1164 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1168 connection_resched( conn );
1169 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1170 slap_op_free( op, ctx );
1174 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1176 Connection *connection_client_setup(
1178 ldap_pvt_thread_start_t *func,
1182 ber_socket_t sfd = SLAP_SOCKNEW( s );
1184 c = connection_init( sfd, (Listener *)&dummy_list, "", "",
1185 CONN_IS_CLIENT, 0, NULL
1186 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
1188 c->c_clientfunc = func;
1189 c->c_clientarg = arg;
1191 slapd_add_internal( sfd, 0 );
1196 void connection_client_enable(
1199 slapd_set_read( c->c_sd, 1 );
1202 void connection_client_stop(
1206 ber_socket_t s = c->c_sd;
1208 /* get (locked) connection */
1209 c = connection_get( s );
1211 assert( c->c_conn_state == SLAP_C_CLIENT );
1213 c->c_listener = NULL;
1214 c->c_conn_state = SLAP_C_INVALID;
1215 c->c_struct_state = SLAP_C_UNUSED;
1216 c->c_sd = AC_SOCKET_INVALID;
1217 c->c_close_reason = "?"; /* should never be needed */
1219 c->c_sb = ber_sockbuf_alloc( );
1221 ber_len_t max = sockbuf_max_incoming;
1222 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1224 slapd_remove( s, sb, 0, 1, 0 );
1226 connection_return( c );
1229 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1231 static void* connection_read_thread( void* ctx, void* argv )
1234 conn_readinfo cri = { NULL, NULL, NULL, NULL, 0 };
1235 ber_socket_t s = (long)argv;
1238 * read incoming LDAP requests. If there is more than one,
1239 * the first one is returned with new_op
1242 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1243 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1244 return (void*)(long)rc;
1247 /* execute a single queued request in the same thread */
1248 if( cri.op && !cri.nullop ) {
1249 rc = (long)connection_operation( ctx, cri.op );
1250 } else if ( cri.func ) {
1251 rc = (long)cri.func( ctx, cri.arg );
1254 return (void*)(long)rc;
1257 int connection_read_activate( ber_socket_t s )
1262 * suspend reading on this file descriptor until a connection processing
1263 * thread reads data on it. Otherwise the listener thread will repeatedly
1264 * submit the same event on it to the pool.
1266 rc = slapd_clr_read( s, 0 );
1270 /* Don't let blocked writers block a pause request */
1271 if ( connections[s].c_writewaiter &&
1272 ldap_pvt_thread_pool_pausing( &connection_pool ))
1273 connection_wake_writers( &connections[s] );
1275 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1276 connection_read_thread, (void *)(long)s );
1279 Debug( LDAP_DEBUG_ANY,
1280 "connection_read_activate(%d): submit failed (%d)\n",
1288 connection_read( ber_socket_t s, conn_readinfo *cri )
1293 assert( connections != NULL );
1295 /* get (locked) connection */
1296 c = connection_get( s );
1299 Debug( LDAP_DEBUG_ANY,
1300 "connection_read(%ld): no connection!\n",
1308 if( c->c_conn_state == SLAP_C_CLOSING ) {
1309 Debug( LDAP_DEBUG_CONNS,
1310 "connection_read(%d): closing, ignoring input for id=%lu\n",
1311 s, c->c_connid, 0 );
1312 connection_return( c );
1316 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1317 cri->func = c->c_clientfunc;
1318 cri->arg = c->c_clientarg;
1319 /* read should already be cleared */
1320 connection_return( c );
1324 Debug( LDAP_DEBUG_TRACE,
1325 "connection_read(%d): checking for input on id=%lu\n",
1326 s, c->c_connid, 0 );
1329 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1330 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1332 Debug( LDAP_DEBUG_TRACE,
1333 "connection_read(%d): TLS accept failure "
1334 "error=%d id=%lu, closing\n",
1335 s, rc, c->c_connid );
1337 c->c_needs_tls_accept = 0;
1338 /* c_mutex is locked */
1339 connection_closing( c, "TLS negotiation failure" );
1340 connection_close( c );
1341 connection_return( c );
1344 } else if ( rc == 0 ) {
1346 struct berval authid = BER_BVNULL;
1348 c->c_needs_tls_accept = 0;
1350 /* we need to let SASL know */
1351 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1353 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1354 if( c->c_tls_ssf > c->c_ssf ) {
1355 c->c_ssf = c->c_tls_ssf;
1358 rc = dnX509peerNormalize( ssl, &authid );
1359 if ( rc != LDAP_SUCCESS ) {
1360 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1361 "unable to get TLS client DN, error=%d id=%lu\n",
1362 s, rc, c->c_connid );
1364 Statslog( LDAP_DEBUG_STATS,
1365 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1366 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1367 slap_sasl_external( c, c->c_tls_ssf, &authid );
1368 if ( authid.bv_val ) free( authid.bv_val );
1369 } else if ( rc == 1 && ber_sockbuf_ctrl( c->c_sb,
1370 LBER_SB_OPT_NEEDS_WRITE, NULL )) { /* need to retry */
1371 slapd_set_write( s, 1 );
1372 connection_return( c );
1376 /* if success and data is ready, fall thru to data input loop */
1377 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1379 slapd_set_read( s, 1 );
1380 connection_return( c );
1386 #ifdef HAVE_CYRUS_SASL
1387 if ( c->c_sasl_layers ) {
1388 /* If previous layer is not removed yet, give up for now */
1389 if ( !c->c_sasl_sockctx ) {
1390 slapd_set_read( s, 1 );
1391 connection_return( c );
1395 c->c_sasl_layers = 0;
1397 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1398 if( rc != LDAP_SUCCESS ) {
1399 Debug( LDAP_DEBUG_TRACE,
1400 "connection_read(%d): SASL install error "
1401 "error=%d id=%lu, closing\n",
1402 s, rc, c->c_connid );
1404 /* c_mutex is locked */
1405 connection_closing( c, "SASL layer install failure" );
1406 connection_close( c );
1407 connection_return( c );
1413 #define CONNECTION_INPUT_LOOP 1
1414 /* #define DATA_READY_LOOP 1 */
1417 /* How do we do this without getting into a busy loop ? */
1418 rc = connection_input( c, cri );
1420 #ifdef DATA_READY_LOOP
1421 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1422 #elif defined CONNECTION_INPUT_LOOP
1429 Debug( LDAP_DEBUG_CONNS,
1430 "connection_read(%d): input error=%d id=%lu, closing.\n",
1431 s, rc, c->c_connid );
1433 /* c_mutex is locked */
1434 connection_closing( c, conn_lost_str );
1435 connection_close( c );
1436 connection_return( c );
1440 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1441 slapd_set_write( s, 0 );
1444 slapd_set_read( s, 1 );
1445 connection_return( c );
1451 connection_input( Connection *conn , conn_readinfo *cri )
1459 #ifdef LDAP_CONNECTIONLESS
1466 if ( conn->c_currentber == NULL &&
1467 ( conn->c_currentber = ber_alloc()) == NULL )
1469 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1475 #ifdef LDAP_CONNECTIONLESS
1476 if ( conn->c_is_udp ) {
1477 char peername[sizeof("IP=255.255.255.255:65336")];
1479 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1480 if (len != sizeof(struct sockaddr)) return 1;
1482 sprintf( peername, "IP=%s:%d",
1483 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1484 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1485 Statslog( LDAP_DEBUG_STATS,
1486 "conn=%lu UDP request from %s (%s) accepted.\n",
1487 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1491 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1492 if ( tag != LDAP_TAG_MESSAGE ) {
1493 int err = sock_errno();
1495 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1496 /* log, close and send error */
1497 Debug( LDAP_DEBUG_TRACE,
1498 "ber_get_next on fd %d failed errno=%d (%s)\n",
1499 conn->c_sd, err, sock_errstr(err) );
1500 ber_free( conn->c_currentber, 1 );
1501 conn->c_currentber = NULL;
1508 ber = conn->c_currentber;
1509 conn->c_currentber = NULL;
1511 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1512 /* log, close and send error */
1513 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1518 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1519 /* log, close and send error */
1520 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1526 #ifdef LDAP_CONNECTIONLESS
1527 if( conn->c_is_udp ) {
1528 if( tag == LBER_OCTETSTRING ) {
1529 ber_get_stringa( ber, &cdn );
1530 tag = ber_peek_tag(ber, &len);
1532 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1533 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1540 if(tag == LDAP_REQ_BIND) {
1541 /* immediately abandon all existing operations upon BIND */
1542 connection_abandon( conn );
1546 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++, ctx );
1548 Debug( LDAP_DEBUG_TRACE, "op tag 0x%lx, time %ld\n", tag,
1549 (long) op->o_time, 0);
1552 /* clear state if the connection is being reused from inactive */
1553 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1554 memset( &conn->c_pagedresults_state, 0,
1555 sizeof( conn->c_pagedresults_state ) );
1558 op->o_res_ber = NULL;
1560 #ifdef LDAP_CONNECTIONLESS
1561 if (conn->c_is_udp) {
1563 ber_str2bv( cdn, 0, 1, &op->o_dn );
1564 op->o_protocol = LDAP_VERSION2;
1566 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1567 if (op->o_res_ber == NULL) return 1;
1569 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1570 sizeof(struct sockaddr), 0 );
1572 if (rc != sizeof(struct sockaddr)) {
1573 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1577 if (op->o_protocol == LDAP_VERSION2) {
1578 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1580 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1585 #endif /* LDAP_CONNECTIONLESS */
1589 /* Don't process requests when the conn is in the middle of a
1590 * Bind, or if it's closing. Also, don't let any single conn
1591 * use up all the available threads, and don't execute if we're
1592 * currently blocked on output. And don't execute if there are
1593 * already pending ops, let them go first. Abandon operations
1594 * get exceptions to some, but not all, cases.
1598 /* Abandon and Unbind are exempt from these checks */
1599 if (conn->c_conn_state == SLAP_C_CLOSING) {
1602 } else if (conn->c_writewaiter) {
1603 defer = "awaiting write";
1605 } else if (conn->c_n_ops_pending) {
1606 defer = "pending operations";
1610 case LDAP_REQ_ABANDON:
1611 /* Unbind is exempt from these checks */
1612 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1613 defer = "too many executing";
1615 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1620 case LDAP_REQ_UNBIND:
1625 int max = conn->c_dn.bv_len
1626 ? slap_conn_max_pending_auth
1627 : slap_conn_max_pending;
1629 Debug( LDAP_DEBUG_ANY,
1630 "connection_input: conn=%lu deferring operation: %s\n",
1631 conn->c_connid, defer, 0 );
1632 conn->c_n_ops_pending++;
1633 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1634 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1637 conn->c_n_ops_executing++;
1640 * The first op will be processed in the same thread context,
1641 * as long as there is only one op total.
1642 * Subsequent ops will be submitted to the pool by
1643 * calling connection_op_activate()
1645 if ( cri->op == NULL ) {
1646 /* the first incoming request */
1647 connection_op_queue( op );
1650 if ( !cri->nullop ) {
1652 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1653 connection_operation, (void *) cri->op );
1655 connection_op_activate( op );
1660 if ( conn->c_struct_state != SLAP_C_USED ) {
1661 /* connection must have got closed underneath us */
1666 assert( conn->c_struct_state == SLAP_C_USED );
1671 connection_resched( Connection *conn )
1675 if( conn->c_writewaiter )
1678 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1679 Debug( LDAP_DEBUG_CONNS, "connection_resched: "
1680 "attempting closing conn=%lu sd=%d\n",
1681 conn->c_connid, conn->c_sd, 0 );
1682 connection_close( conn );
1686 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1687 /* other states need different handling */
1691 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1692 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1694 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1695 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1697 /* pending operations should not be marked for abandonment */
1698 assert(!op->o_abandon);
1700 conn->c_n_ops_pending--;
1701 conn->c_n_ops_executing++;
1703 connection_op_activate( op );
1705 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1711 connection_init_log_prefix( Operation *op )
1713 if ( op->o_connid == (unsigned long)(-1) ) {
1714 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1715 "conn=-1 op=%lu", op->o_opid );
1718 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1719 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1723 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1725 op->o_conn->c_sasl_bindop = NULL;
1727 ch_free( op->o_callback );
1728 op->o_callback = NULL;
1730 return SLAP_CB_CONTINUE;
1733 static int connection_bind_cb( Operation *op, SlapReply *rs )
1735 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1736 op->o_conn->c_sasl_bind_in_progress =
1737 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1739 /* Moved here from bind.c due to ITS#4158 */
1740 op->o_conn->c_sasl_bindop = NULL;
1741 if ( op->orb_method == LDAP_AUTH_SASL ) {
1742 if( rs->sr_err == LDAP_SUCCESS ) {
1743 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1744 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1745 /* edn is always normalized already */
1746 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1748 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1749 BER_BVZERO( &op->orb_edn );
1750 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1751 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1753 op->o_conn->c_sasl_ssf = op->orb_ssf;
1754 if( op->orb_ssf > op->o_conn->c_ssf ) {
1755 op->o_conn->c_ssf = op->orb_ssf;
1758 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1759 ber_len_t max = sockbuf_max_incoming_auth;
1760 ber_sockbuf_ctrl( op->o_conn->c_sb,
1761 LBER_SB_OPT_SET_MAX_INCOMING, &max );
1764 /* log authorization identity */
1765 Statslog( LDAP_DEBUG_STATS,
1766 "%s BIND dn=\"%s\" mech=%s sasl_ssf=%d ssf=%d\n",
1768 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1769 op->o_conn->c_authmech.bv_val,
1770 op->orb_ssf, op->o_conn->c_ssf );
1772 Debug( LDAP_DEBUG_TRACE,
1773 "do_bind: SASL/%s bind: dn=\"%s\" sasl_ssf=%d\n",
1774 op->o_conn->c_authmech.bv_val,
1775 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1778 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1779 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1780 free( op->o_conn->c_sasl_bind_mech.bv_val );
1781 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1785 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1787 ch_free( op->o_callback );
1788 op->o_callback = NULL;
1790 return SLAP_CB_CONTINUE;
1793 static void connection_op_queue( Operation *op )
1795 ber_tag_t tag = op->o_tag;
1797 if (tag == LDAP_REQ_BIND) {
1798 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1799 sc->sc_response = connection_bind_cb;
1800 sc->sc_cleanup = connection_bind_cleanup_cb;
1801 sc->sc_next = op->o_callback;
1802 op->o_callback = sc;
1803 op->o_conn->c_conn_state = SLAP_C_BINDING;
1806 if (!op->o_dn.bv_len) {
1807 op->o_authz = op->o_conn->c_authz;
1808 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1809 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1810 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1812 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1813 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1817 op->o_authtype = op->o_conn->c_authtype;
1818 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1820 if (!op->o_protocol) {
1821 op->o_protocol = op->o_conn->c_protocol
1822 ? op->o_conn->c_protocol : LDAP_VERSION3;
1825 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1826 op->o_protocol > LDAP_VERSION2)
1828 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1831 op->o_connid = op->o_conn->c_connid;
1832 connection_init_log_prefix( op );
1834 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1837 static int connection_op_activate( Operation *op )
1841 connection_op_queue( op );
1843 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1844 connection_operation, (void *) op );
1847 Debug( LDAP_DEBUG_ANY,
1848 "connection_op_activate: submit failed (%d) for conn=%lu\n",
1849 rc, op->o_connid, 0 );
1850 /* should move op to pending list */
1856 int connection_write(ber_socket_t s)
1861 assert( connections != NULL );
1863 slapd_clr_write( s, 0 );
1865 c = connection_get( s );
1867 Debug( LDAP_DEBUG_ANY,
1868 "connection_write(%ld): no connection!\n",
1874 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1875 connection_return( c );
1876 connection_read_activate( s );
1883 Debug( LDAP_DEBUG_TRACE,
1884 "connection_write(%d): waking output for id=%lu\n",
1885 s, c->c_connid, 0 );
1886 ldap_pvt_thread_mutex_lock( &c->c_write2_mutex );
1887 ldap_pvt_thread_cond_signal( &c->c_write2_cv );
1888 ldap_pvt_thread_mutex_unlock( &c->c_write2_mutex );
1890 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1891 slapd_set_read( s, 1 );
1893 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1894 slapd_set_write( s, 1 );
1897 /* If there are ops pending because of a writewaiter,
1900 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1901 if ( !c->c_writewaiter ) break;
1902 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
1904 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1905 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1907 /* pending operations should not be marked for abandonment */
1908 assert(!op->o_abandon);
1910 c->c_n_ops_pending--;
1911 c->c_n_ops_executing++;
1913 connection_op_activate( op );
1918 connection_return( c );
1923 typedef struct conn_fake_extblock {
1926 } conn_fake_extblock;
1929 connection_fake_destroy(
1933 Connection conn = {0};
1935 Opheader ohdr = {0};
1937 conn_fake_extblock *eb = data;
1940 op.o_hdr->oh_extensions = eb->eb_op;
1941 conn.c_extensions = eb->eb_conn;
1946 ber_memfree_x( eb, NULL );
1947 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, &op );
1948 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &conn );
1953 connection_fake_init(
1955 OperationBuffer *opbuf,
1958 connection_fake_init2( conn, opbuf, ctx, 1 );
1962 operation_fake_init(
1968 /* set memory context */
1969 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx,
1971 op->o_tmpmfuncs = &slap_sl_mfuncs;
1972 op->o_threadctx = ctx;
1973 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1975 op->o_counters = &slap_counters;
1977 op->o_connid = op->o_conn->c_connid;
1978 connection_init_log_prefix( op );
1983 connection_fake_init2(
1985 OperationBuffer *opbuf,
1989 Operation *op = (Operation *) opbuf;
1991 conn->c_connid = -1;
1992 conn->c_conn_idx = -1;
1993 conn->c_send_ldap_result = slap_send_ldap_result;
1994 conn->c_send_search_entry = slap_send_search_entry;
1995 conn->c_send_search_reference = slap_send_search_reference;
1996 conn->c_send_ldap_extended = slap_send_ldap_extended;
1997 conn->c_send_ldap_intermediate = slap_send_ldap_intermediate;
1998 conn->c_listener = (Listener *)&dummy_list;
1999 conn->c_peer_domain = slap_empty_bv;
2000 conn->c_peer_name = slap_empty_bv;
2002 memset( opbuf, 0, sizeof( *opbuf ));
2003 op->o_hdr = &opbuf->ob_hdr;
2004 op->o_controls = opbuf->ob_controls;
2006 operation_fake_init( conn, op, ctx, newmem );
2009 if ( slapi_plugins_used ) {
2010 conn_fake_extblock *eb;
2013 /* Use thread keys to make sure these eventually get cleaned up */
2014 if ( ldap_pvt_thread_pool_getkey( ctx, (void *)connection_fake_init,
2016 eb = ch_malloc( sizeof( *eb ));
2017 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
2018 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
2019 eb->eb_conn = conn->c_extensions;
2020 eb->eb_op = op->o_hdr->oh_extensions;
2021 ldap_pvt_thread_pool_setkey( ctx, (void *)connection_fake_init,
2022 eb, connection_fake_destroy, NULL, NULL );
2025 conn->c_extensions = eb->eb_conn;
2026 op->o_hdr->oh_extensions = eb->eb_op;
2029 #endif /* LDAP_SLAPI */
2031 slap_op_time( &op->o_time, &op->o_tincr );
2035 connection_assign_nextid( Connection *conn )
2037 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2038 conn->c_connid = conn_nextid++;
2039 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );