2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2009 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 enum sc_struct_state {
57 SLAP_C_UNINITIALIZED = 0, /* MUST BE ZERO (0) */
63 /* connection state (protected by c_mutex ) */
65 SLAP_C_INVALID = 0, /* MUST BE ZERO (0) */
66 SLAP_C_INACTIVE, /* zero threads */
67 SLAP_C_CLOSING, /* closing */
68 SLAP_C_ACTIVE, /* one or more threads */
69 SLAP_C_BINDING, /* binding */
70 SLAP_C_CLIENT /* outbound client conn */
74 connection_state2str( int state )
77 case SLAP_C_INVALID: return "!";
78 case SLAP_C_INACTIVE: return "|";
79 case SLAP_C_CLOSING: return "C";
80 case SLAP_C_ACTIVE: return "";
81 case SLAP_C_BINDING: return "B";
82 case SLAP_C_CLIENT: return "L";
88 static Connection* connection_get( ber_socket_t s );
90 typedef struct conn_readinfo {
92 ldap_pvt_thread_start_t *func;
98 static int connection_input( Connection *c, conn_readinfo *cri );
99 static void connection_close( Connection *c );
101 static int connection_op_activate( Operation *op );
102 static void connection_op_queue( Operation *op );
103 static int connection_resched( Connection *conn );
104 static void connection_abandon( Connection *conn );
105 static void connection_destroy( Connection *c );
107 static ldap_pvt_thread_start_t connection_operation;
110 * Initialize connection management infrastructure.
112 int connections_init(void)
116 assert( connections == NULL );
118 if( connections != NULL) {
119 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
124 /* should check return of every call */
125 ldap_pvt_thread_mutex_init( &connections_mutex );
126 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
128 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
130 if( connections == NULL ) {
131 Debug( LDAP_DEBUG_ANY, "connections_init: "
132 "allocation (%d*%ld) of connection array failed\n",
133 dtblsize, (long) sizeof(Connection), 0 );
137 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
138 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
140 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
143 * per entry initialization of the Connection array initialization
144 * will be done by connection_init()
151 * Destroy connection management infrastructure.
154 int connections_destroy(void)
158 /* should check return of every call */
160 if( connections == NULL) {
161 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
166 for ( i = 0; i < dtblsize; i++ ) {
167 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
168 ber_sockbuf_free( connections[i].c_sb );
169 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
170 ldap_pvt_thread_mutex_destroy( &connections[i].c_write1_mutex );
171 ldap_pvt_thread_mutex_destroy( &connections[i].c_write2_mutex );
172 ldap_pvt_thread_cond_destroy( &connections[i].c_write1_cv );
173 ldap_pvt_thread_cond_destroy( &connections[i].c_write2_cv );
175 if ( slapi_plugins_used ) {
176 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
186 ldap_pvt_thread_mutex_destroy( &connections_mutex );
187 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
192 * shutdown all connections
194 int connections_shutdown(void)
198 for ( i = 0; i < dtblsize; i++ ) {
199 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
200 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
201 if( connections[i].c_struct_state == SLAP_C_USED ) {
203 /* give persistent clients a chance to cleanup */
204 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
205 ldap_pvt_thread_pool_submit( &connection_pool,
206 connections[i].c_clientfunc, connections[i].c_clientarg );
208 /* c_mutex is locked */
209 connection_closing( &connections[i], "slapd shutdown" );
210 connection_close( &connections[i] );
213 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
221 * Timeout idle connections.
223 int connections_timeout_idle(time_t now)
225 int i = 0, writers = 0;
230 old = slapd_get_writetime();
232 for( c = connection_first( &connindex );
234 c = connection_next( c, &connindex ) )
236 /* Don't timeout a slow-running request or a persistent
237 * outbound connection. But if it has a writewaiter, see
238 * if the waiter has been there too long.
240 if(( c->c_n_ops_executing && !c->c_writewaiter)
241 || c->c_conn_state == SLAP_C_CLIENT ) {
245 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
247 connection_closing( c, "idletimeout" );
248 connection_close( c );
252 if ( c->c_writewaiter && global_writetimeout ) {
254 if( difftime( c->c_activitytime+global_writetimeout, now) < 0 ) {
256 connection_closing( c, "writetimeout" );
257 connection_close( c );
262 connection_done( c );
263 if ( old && !writers )
264 slapd_clr_writetime( old );
269 static Connection* connection_get( ber_socket_t s )
273 Debug( LDAP_DEBUG_ARGS,
274 "connection_get(%ld)\n",
277 assert( connections != NULL );
279 if(s == AC_SOCKET_INVALID) return NULL;
281 assert( s < dtblsize );
285 ldap_pvt_thread_mutex_lock( &c->c_mutex );
287 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
289 if( c->c_struct_state != SLAP_C_USED ) {
290 /* connection must have been closed due to resched */
292 assert( c->c_conn_state == SLAP_C_INVALID );
293 assert( c->c_sd == AC_SOCKET_INVALID );
295 Debug( LDAP_DEBUG_CONNS,
296 "connection_get(%d): connection not used\n",
299 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
303 Debug( LDAP_DEBUG_TRACE,
304 "connection_get(%d): got connid=%lu\n",
309 assert( c->c_struct_state == SLAP_C_USED );
310 assert( c->c_conn_state != SLAP_C_INVALID );
311 assert( c->c_sd != AC_SOCKET_INVALID );
313 #ifndef SLAPD_MONITOR
314 if ( global_idletimeout > 0 )
315 #endif /* ! SLAPD_MONITOR */
317 c->c_activitytime = slap_get_time();
324 static void connection_return( Connection *c )
326 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
329 Connection * connection_init(
333 const char* peername,
336 struct berval *authid
337 LDAP_PF_LOCAL_SENDMSG_ARG(struct berval *peerbv))
342 ber_socket_t sfd = SLAP_FD2SOCK(s);
344 assert( connections != NULL );
346 assert( listener != NULL );
347 assert( dnsname != NULL );
348 assert( peername != NULL );
351 assert( !( flags & CONN_IS_TLS ));
354 if( s == AC_SOCKET_INVALID ) {
355 Debug( LDAP_DEBUG_ANY,
356 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
361 assert( s < dtblsize );
363 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
366 assert( c->c_struct_state == SLAP_C_UNUSED );
370 c->c_send_ldap_result = slap_send_ldap_result;
371 c->c_send_search_entry = slap_send_search_entry;
372 c->c_send_search_reference = slap_send_search_reference;
373 c->c_send_ldap_extended = slap_send_ldap_extended;
374 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
376 BER_BVZERO( &c->c_authmech );
377 BER_BVZERO( &c->c_dn );
378 BER_BVZERO( &c->c_ndn );
380 c->c_listener = NULL;
381 BER_BVZERO( &c->c_peer_domain );
382 BER_BVZERO( &c->c_peer_name );
384 LDAP_STAILQ_INIT(&c->c_ops);
385 LDAP_STAILQ_INIT(&c->c_pending_ops);
388 c->c_txn = CONN_TXN_INACTIVE;
389 c->c_txn_backend = NULL;
390 LDAP_STAILQ_INIT(&c->c_txn_ops);
393 BER_BVZERO( &c->c_sasl_bind_mech );
395 c->c_sasl_authctx = NULL;
396 c->c_sasl_sockctx = NULL;
397 c->c_sasl_extra = NULL;
398 c->c_sasl_bindop = NULL;
400 c->c_sb = ber_sockbuf_alloc( );
403 ber_len_t max = sockbuf_max_incoming;
404 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
407 c->c_currentber = NULL;
409 /* should check status of thread calls */
410 ldap_pvt_thread_mutex_init( &c->c_mutex );
411 ldap_pvt_thread_mutex_init( &c->c_write1_mutex );
412 ldap_pvt_thread_mutex_init( &c->c_write2_mutex );
413 ldap_pvt_thread_cond_init( &c->c_write1_cv );
414 ldap_pvt_thread_cond_init( &c->c_write2_cv );
417 if ( slapi_plugins_used ) {
418 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
423 ldap_pvt_thread_mutex_lock( &c->c_mutex );
425 assert( BER_BVISNULL( &c->c_authmech ) );
426 assert( BER_BVISNULL( &c->c_dn ) );
427 assert( BER_BVISNULL( &c->c_ndn ) );
428 assert( c->c_listener == NULL );
429 assert( BER_BVISNULL( &c->c_peer_domain ) );
430 assert( BER_BVISNULL( &c->c_peer_name ) );
431 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
432 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
434 assert( c->c_txn == CONN_TXN_INACTIVE );
435 assert( c->c_txn_backend == NULL );
436 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
438 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
439 assert( c->c_sasl_done == 0 );
440 assert( c->c_sasl_authctx == NULL );
441 assert( c->c_sasl_sockctx == NULL );
442 assert( c->c_sasl_extra == NULL );
443 assert( c->c_sasl_bindop == NULL );
444 assert( c->c_currentber == NULL );
445 assert( c->c_writewaiter == 0);
446 assert( c->c_writers == 0);
448 c->c_listener = listener;
451 if ( flags & CONN_IS_CLIENT ) {
453 ldap_pvt_thread_mutex_lock( &connections_mutex );
454 c->c_conn_state = SLAP_C_CLIENT;
455 c->c_struct_state = SLAP_C_USED;
456 ldap_pvt_thread_mutex_unlock( &connections_mutex );
457 c->c_close_reason = "?"; /* should never be needed */
458 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &sfd );
459 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
464 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
465 ber_str2bv( peername, 0, 1, &c->c_peer_name );
467 c->c_n_ops_received = 0;
468 c->c_n_ops_executing = 0;
469 c->c_n_ops_pending = 0;
470 c->c_n_ops_completed = 0;
476 /* set to zero until bind, implies LDAP_VERSION3 */
479 #ifndef SLAPD_MONITOR
480 if ( global_idletimeout > 0 )
481 #endif /* ! SLAPD_MONITOR */
483 c->c_activitytime = c->c_starttime = slap_get_time();
486 #ifdef LDAP_CONNECTIONLESS
488 if( flags & CONN_IS_UDP ) {
491 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
492 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
494 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
495 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
496 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
497 LBER_SBIOD_LEVEL_PROVIDER, NULL );
499 #endif /* LDAP_CONNECTIONLESS */
501 if ( flags & CONN_IS_IPC ) {
503 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
504 LBER_SBIOD_LEVEL_PROVIDER, (void*)"ipc_" );
506 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_fd,
507 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
508 #ifdef LDAP_PF_LOCAL_SENDMSG
509 if ( !BER_BVISEMPTY( peerbv ))
510 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_UNGET_BUF, peerbv );
513 #endif /* LDAP_PF_LOCAL */
516 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
517 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
519 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
520 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
524 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
525 INT_MAX, (void*)"ldap_" );
528 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
529 c /* non-NULL */ ) < 0 )
531 Debug( LDAP_DEBUG_ANY,
532 "connection_init(%d, %s): set nonblocking failed\n",
533 s, c->c_peer_name.bv_val, 0 );
536 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
537 id = c->c_connid = conn_nextid++;
538 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
540 ldap_pvt_thread_mutex_lock( &connections_mutex );
541 c->c_conn_state = SLAP_C_INACTIVE;
542 c->c_struct_state = SLAP_C_USED;
543 ldap_pvt_thread_mutex_unlock( &connections_mutex );
544 c->c_close_reason = "?"; /* should never be needed */
546 c->c_ssf = c->c_transport_ssf = ssf;
550 if ( flags & CONN_IS_TLS ) {
552 c->c_needs_tls_accept = 1;
555 c->c_needs_tls_accept = 0;
559 slap_sasl_open( c, 0 );
560 slap_sasl_external( c, ssf, authid );
562 slapd_add_internal( s, 1 );
563 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
565 backend_connection_init(c);
570 void connection2anonymous( Connection *c )
572 assert( connections != NULL );
576 ber_len_t max = sockbuf_max_incoming;
577 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
580 if ( !BER_BVISNULL( &c->c_authmech ) ) {
581 ch_free(c->c_authmech.bv_val);
583 BER_BVZERO( &c->c_authmech );
585 if ( !BER_BVISNULL( &c->c_dn ) ) {
586 ch_free(c->c_dn.bv_val);
588 BER_BVZERO( &c->c_dn );
590 if ( !BER_BVISNULL( &c->c_ndn ) ) {
591 ch_free(c->c_ndn.bv_val);
593 BER_BVZERO( &c->c_ndn );
595 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
596 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
598 BER_BVZERO( &c->c_sasl_authz_dn );
600 c->c_authz_backend = NULL;
604 connection_destroy( Connection *c )
606 unsigned long connid;
607 const char *close_reason;
611 assert( connections != NULL );
613 assert( c->c_struct_state != SLAP_C_UNUSED );
614 assert( c->c_conn_state != SLAP_C_INVALID );
615 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
616 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
618 assert( c->c_txn == CONN_TXN_INACTIVE );
619 assert( c->c_txn_backend == NULL );
620 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
622 assert( c->c_writewaiter == 0);
623 assert( c->c_writers == 0);
625 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
626 connid = c->c_connid;
627 close_reason = c->c_close_reason;
629 ldap_pvt_thread_mutex_lock( &connections_mutex );
630 c->c_struct_state = SLAP_C_PENDING;
631 ldap_pvt_thread_mutex_unlock( &connections_mutex );
633 backend_connection_destroy(c);
638 c->c_activitytime = c->c_starttime = 0;
640 connection2anonymous( c );
641 c->c_listener = NULL;
643 if(c->c_peer_domain.bv_val != NULL) {
644 free(c->c_peer_domain.bv_val);
646 BER_BVZERO( &c->c_peer_domain );
647 if(c->c_peer_name.bv_val != NULL) {
648 free(c->c_peer_name.bv_val);
650 BER_BVZERO( &c->c_peer_name );
652 c->c_sasl_bind_in_progress = 0;
653 if(c->c_sasl_bind_mech.bv_val != NULL) {
654 free(c->c_sasl_bind_mech.bv_val);
656 BER_BVZERO( &c->c_sasl_bind_mech );
658 slap_sasl_close( c );
660 if ( c->c_currentber != NULL ) {
661 ber_free( c->c_currentber, 1 );
662 c->c_currentber = NULL;
667 /* call destructors, then constructors; avoids unnecessary allocation */
668 if ( slapi_plugins_used ) {
669 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
674 c->c_sd = AC_SOCKET_INVALID;
675 c->c_conn_state = SLAP_C_INVALID;
676 c->c_struct_state = SLAP_C_UNUSED;
677 c->c_close_reason = "?"; /* should never be needed */
680 c->c_sb = ber_sockbuf_alloc( );
682 ber_len_t max = sockbuf_max_incoming;
683 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
686 /* c must be fully reset by this point; when we call slapd_remove
687 * it may get immediately reused by a new connection.
689 if ( sd != AC_SOCKET_INVALID ) {
690 slapd_remove( sd, sb, 1, 0, 0 );
692 if ( close_reason == NULL ) {
693 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
694 connid, (long) sd, 0, 0, 0 );
696 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
697 connid, (long) sd, close_reason, 0, 0 );
702 int connection_valid( Connection *c )
704 /* c_mutex must be locked by caller */
708 return c->c_struct_state == SLAP_C_USED &&
709 c->c_conn_state >= SLAP_C_ACTIVE &&
710 c->c_conn_state <= SLAP_C_CLIENT;
713 static void connection_abandon( Connection *c )
715 /* c_mutex must be locked by caller */
717 Operation *o, *next, op = {0};
723 op.o_connid = c->c_connid;
724 op.o_tag = LDAP_REQ_ABANDON;
726 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
727 next = LDAP_STAILQ_NEXT( o, o_next );
728 op.orn_msgid = o->o_msgid;
730 op.o_bd = frontendDB;
731 frontendDB->be_abandon( &op, &rs );
735 /* remove operations in pending transaction */
736 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
737 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
738 LDAP_STAILQ_NEXT(o, o_next) = NULL;
739 slap_op_free( o, NULL );
742 /* clear transaction */
743 c->c_txn_backend = NULL;
744 c->c_txn = CONN_TXN_INACTIVE;
747 /* remove pending operations */
748 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
749 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
750 LDAP_STAILQ_NEXT(o, o_next) = NULL;
751 slap_op_free( o, NULL );
755 void connection_closing( Connection *c, const char *why )
757 assert( connections != NULL );
759 assert( c->c_struct_state == SLAP_C_USED );
760 assert( c->c_conn_state != SLAP_C_INVALID );
762 /* c_mutex must be locked by caller */
764 if( c->c_conn_state != SLAP_C_CLOSING ) {
765 Debug( LDAP_DEBUG_CONNS,
766 "connection_closing: readying conn=%lu sd=%d for close\n",
767 c->c_connid, c->c_sd, 0 );
768 /* update state to closing */
769 c->c_conn_state = SLAP_C_CLOSING;
770 c->c_close_reason = why;
772 /* don't listen on this port anymore */
773 slapd_clr_read( c->c_sd, 0 );
775 /* abandon active operations */
776 connection_abandon( c );
778 /* wake write blocked operations */
779 ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
780 if ( c->c_writers > 0 ) {
781 c->c_writers = -c->c_writers;
782 ldap_pvt_thread_cond_broadcast( &c->c_write1_cv );
783 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
784 if ( c->c_writewaiter ) {
785 ldap_pvt_thread_mutex_lock( &c->c_write2_mutex );
786 ldap_pvt_thread_cond_signal( &c->c_write2_cv );
787 slapd_clr_write( c->c_sd, 1 );
788 ldap_pvt_thread_mutex_unlock( &c->c_write2_mutex );
790 ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
791 while ( c->c_writers ) {
792 ldap_pvt_thread_cond_wait( &c->c_write1_cv, &c->c_write1_mutex );
794 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
796 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
797 slapd_clr_write( c->c_sd, 1 );
800 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
801 /* Client closed connection after doing Unbind. */
802 c->c_close_reason = NULL;
807 connection_close( Connection *c )
809 assert( connections != NULL );
811 assert( c->c_struct_state == SLAP_C_USED );
812 assert( c->c_conn_state == SLAP_C_CLOSING );
814 /* NOTE: c_mutex should be locked by caller */
816 if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
817 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
819 Debug( LDAP_DEBUG_CONNS,
820 "connection_close: deferring conn=%lu sd=%d\n",
821 c->c_connid, c->c_sd, 0 );
825 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
826 c->c_connid, c->c_sd, 0 );
828 connection_destroy( c );
831 unsigned long connections_nextid(void)
834 assert( connections != NULL );
836 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
840 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
845 Connection* connection_first( ber_socket_t *index )
847 assert( connections != NULL );
848 assert( index != NULL );
850 ldap_pvt_thread_mutex_lock( &connections_mutex );
851 for( *index = 0; *index < dtblsize; (*index)++) {
852 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
856 ldap_pvt_thread_mutex_unlock( &connections_mutex );
858 return connection_next(NULL, index);
861 Connection* connection_next( Connection *c, ber_socket_t *index )
863 assert( connections != NULL );
864 assert( index != NULL );
865 assert( *index <= dtblsize );
867 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
871 ldap_pvt_thread_mutex_lock( &connections_mutex );
872 for(; *index < dtblsize; (*index)++) {
874 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
875 /* FIXME: accessing c_conn_state without locking c_mutex */
876 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
880 if( connections[*index].c_struct_state == SLAP_C_USED ) {
881 c = &connections[(*index)++];
882 if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
884 ldap_pvt_thread_mutex_unlock( &connections_mutex );
885 ldap_pvt_thread_mutex_lock( &c->c_mutex );
886 ldap_pvt_thread_mutex_lock( &connections_mutex );
887 if ( c->c_struct_state != SLAP_C_USED ) {
888 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
893 assert( c->c_conn_state != SLAP_C_INVALID );
897 c_struct = connections[*index].c_struct_state;
898 if ( c_struct == SLAP_C_PENDING )
900 assert( c_struct == SLAP_C_UNUSED );
901 /* FIXME: accessing c_conn_state without locking c_mutex */
902 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
905 ldap_pvt_thread_mutex_unlock( &connections_mutex );
909 void connection_done( Connection *c )
911 assert( connections != NULL );
913 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
917 * connection_activity - handle the request operation op on connection
918 * conn. This routine figures out what kind of operation it is and
919 * calls the appropriate stub to handle it.
923 /* FIXME: returns 0 in case of failure */
924 #define INCR_OP_INITIATED(index) \
926 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
927 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated_[(index)], 1); \
928 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
930 #define INCR_OP_COMPLETED(index) \
932 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
933 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
934 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed_[(index)], 1); \
935 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
937 #else /* !SLAPD_MONITOR */
938 #define INCR_OP_INITIATED(index) do { } while (0)
939 #define INCR_OP_COMPLETED(index) \
941 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
942 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
943 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
945 #endif /* !SLAPD_MONITOR */
948 * NOTE: keep in sync with enum in slapd.h
950 static BI_op_func *opfun[] = {
964 /* Counters are per-thread, not per-connection.
967 conn_counter_destroy( void *key, void *data )
969 slap_counters_t **prev, *sc;
971 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
972 for ( prev = &slap_counters.sc_next, sc = slap_counters.sc_next; sc;
973 prev = &sc->sc_next, sc = sc->sc_next ) {
978 /* Copy data to main counter */
979 ldap_pvt_mp_add( slap_counters.sc_bytes, sc->sc_bytes );
980 ldap_pvt_mp_add( slap_counters.sc_pdu, sc->sc_pdu );
981 ldap_pvt_mp_add( slap_counters.sc_entries, sc->sc_entries );
982 ldap_pvt_mp_add( slap_counters.sc_refs, sc->sc_refs );
983 ldap_pvt_mp_add( slap_counters.sc_ops_initiated, sc->sc_ops_initiated );
984 ldap_pvt_mp_add( slap_counters.sc_ops_completed, sc->sc_ops_completed );
986 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
987 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_initiated_[ i ] );
988 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_completed_[ i ] );
990 #endif /* SLAPD_MONITOR */
991 slap_counters_destroy( sc );
992 ber_memfree_x( data, NULL );
996 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
1000 conn_counter_init( Operation *op, void *ctx )
1002 slap_counters_t *sc;
1005 if ( ldap_pvt_thread_pool_getkey(
1006 ctx, (void *)conn_counter_init, &vsc, NULL ) || !vsc ) {
1007 vsc = ch_malloc( sizeof( slap_counters_t ));
1009 slap_counters_init( sc );
1010 ldap_pvt_thread_pool_setkey( ctx, (void*)conn_counter_init, vsc,
1011 conn_counter_destroy, NULL, NULL );
1013 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
1014 sc->sc_next = slap_counters.sc_next;
1015 slap_counters.sc_next = sc;
1016 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
1018 op->o_counters = vsc;
1022 connection_operation( void *ctx, void *arg_v )
1024 int rc = LDAP_OTHER, cancel;
1025 Operation *op = arg_v;
1026 SlapReply rs = {REP_RESULT};
1027 ber_tag_t tag = op->o_tag;
1028 slap_op_t opidx = SLAP_OP_LAST;
1029 Connection *conn = op->o_conn;
1030 void *memctx = NULL;
1031 void *memctx_null = NULL;
1034 conn_counter_init( op, ctx );
1035 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1036 /* FIXME: returns 0 in case of failure */
1037 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated, 1);
1038 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1040 op->o_threadctx = ctx;
1041 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1045 case LDAP_REQ_UNBIND:
1047 case LDAP_REQ_DELETE:
1048 case LDAP_REQ_MODDN:
1049 case LDAP_REQ_MODIFY:
1050 case LDAP_REQ_COMPARE:
1051 case LDAP_REQ_SEARCH:
1052 case LDAP_REQ_ABANDON:
1053 case LDAP_REQ_EXTENDED:
1056 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1057 "conn %lu unknown LDAP request 0x%lx\n",
1058 conn->c_connid, tag, 0 );
1059 op->o_tag = LBER_ERROR;
1060 rs.sr_err = LDAP_PROTOCOL_ERROR;
1061 rs.sr_text = "unknown LDAP request";
1062 send_ldap_disconnect( op, &rs );
1063 rc = SLAPD_DISCONNECT;
1064 goto operations_error;
1067 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1068 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1069 "error: SASL bind in progress (tag=%ld).\n",
1071 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1072 "SASL bind in progress" );
1073 rc = LDAP_OPERATIONS_ERROR;
1074 goto operations_error;
1078 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1079 ( tag == LDAP_REQ_ADD ) ||
1080 ( tag == LDAP_REQ_DELETE ) ||
1081 ( tag == LDAP_REQ_MODIFY ) ||
1082 ( tag == LDAP_REQ_MODRDN )))
1084 /* Disable SLAB allocator for all update operations
1085 issued inside of a transaction */
1086 op->o_tmpmemctx = NULL;
1087 op->o_tmpmfuncs = &ch_mfuncs;
1091 /* We can use Thread-Local storage for most mallocs. We can
1092 * also use TL for ber parsing, but not on Add or Modify.
1095 memsiz = ber_len( op->o_ber ) * 64;
1096 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1098 memsiz = SLAP_SLAB_SIZE;
1100 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx, 1 );
1101 op->o_tmpmemctx = memctx;
1102 op->o_tmpmfuncs = &slap_sl_mfuncs;
1103 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1104 /* Note - the ber and its buffer are already allocated from
1105 * regular memory; this only affects subsequent mallocs that
1106 * ber_scanf may invoke.
1108 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1112 opidx = slap_req2op( tag );
1113 assert( opidx != SLAP_OP_LAST );
1114 INCR_OP_INITIATED( opidx );
1115 rc = (*(opfun[opidx]))( op, &rs );
1118 if ( rc == SLAPD_DISCONNECT ) {
1121 } else if ( opidx != SLAP_OP_LAST ) {
1122 /* increment completed operations count
1123 * only if operation was initiated
1124 * and rc != SLAPD_DISCONNECT */
1125 INCR_OP_COMPLETED( opidx );
1128 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1130 if ( opidx == SLAP_OP_BIND && conn->c_conn_state == SLAP_C_BINDING )
1131 conn->c_conn_state = SLAP_C_ACTIVE;
1133 cancel = op->o_cancel;
1134 if ( cancel != SLAP_CANCEL_NONE && cancel != SLAP_CANCEL_DONE ) {
1135 if ( cancel == SLAP_CANCEL_REQ ) {
1136 op->o_cancel = rc == SLAPD_ABANDON
1137 ? SLAP_CANCEL_ACK : LDAP_TOO_LATE;
1141 /* Fake a cond_wait with thread_yield, then
1142 * verify the result properly mutex-protected.
1144 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1146 ldap_pvt_thread_yield();
1147 } while ( (cancel = op->o_cancel) != SLAP_CANCEL_NONE
1148 && cancel != SLAP_CANCEL_DONE );
1149 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1150 } while ( (cancel = op->o_cancel) != SLAP_CANCEL_NONE
1151 && cancel != SLAP_CANCEL_DONE );
1154 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1156 LDAP_STAILQ_REMOVE( &conn->c_ops, op, Operation, o_next);
1157 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1158 conn->c_n_ops_executing--;
1159 conn->c_n_ops_completed++;
1163 case LDAP_REQ_UNBIND:
1164 /* c_mutex is locked */
1165 connection_closing( conn,
1166 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1170 connection_resched( conn );
1171 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1172 slap_op_free( op, ctx );
1176 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1178 Connection *connection_client_setup(
1180 ldap_pvt_thread_start_t *func,
1184 ber_socket_t sfd = SLAP_SOCKNEW( s );
1186 c = connection_init( sfd, (Listener *)&dummy_list, "", "",
1187 CONN_IS_CLIENT, 0, NULL
1188 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
1190 c->c_clientfunc = func;
1191 c->c_clientarg = arg;
1193 slapd_add_internal( sfd, 0 );
1198 void connection_client_enable(
1201 slapd_set_read( c->c_sd, 1 );
1204 void connection_client_stop(
1208 ber_socket_t s = c->c_sd;
1210 /* get (locked) connection */
1211 c = connection_get( s );
1213 assert( c->c_conn_state == SLAP_C_CLIENT );
1215 c->c_listener = NULL;
1216 c->c_conn_state = SLAP_C_INVALID;
1217 c->c_struct_state = SLAP_C_UNUSED;
1218 c->c_sd = AC_SOCKET_INVALID;
1219 c->c_close_reason = "?"; /* should never be needed */
1221 c->c_sb = ber_sockbuf_alloc( );
1223 ber_len_t max = sockbuf_max_incoming;
1224 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1226 slapd_remove( s, sb, 0, 1, 0 );
1228 connection_return( c );
1231 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1233 static void* connection_read_thread( void* ctx, void* argv )
1236 conn_readinfo cri = { NULL, NULL, NULL, NULL, 0 };
1237 ber_socket_t s = (long)argv;
1240 * read incoming LDAP requests. If there is more than one,
1241 * the first one is returned with new_op
1244 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1245 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1246 return (void*)(long)rc;
1249 /* execute a single queued request in the same thread */
1250 if( cri.op && !cri.nullop ) {
1251 rc = (long)connection_operation( ctx, cri.op );
1252 } else if ( cri.func ) {
1253 rc = (long)cri.func( ctx, cri.arg );
1256 return (void*)(long)rc;
1259 int connection_read_activate( ber_socket_t s )
1264 * suspend reading on this file descriptor until a connection processing
1265 * thread reads data on it. Otherwise the listener thread will repeatedly
1266 * submit the same event on it to the pool.
1268 rc = slapd_clr_read( s, 0 );
1272 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1273 connection_read_thread, (void *)(long)s );
1276 Debug( LDAP_DEBUG_ANY,
1277 "connection_read_activate(%d): submit failed (%d)\n",
1285 connection_read( ber_socket_t s, conn_readinfo *cri )
1290 assert( connections != NULL );
1292 /* get (locked) connection */
1293 c = connection_get( s );
1296 Debug( LDAP_DEBUG_ANY,
1297 "connection_read(%ld): no connection!\n",
1305 if( c->c_conn_state == SLAP_C_CLOSING ) {
1306 Debug( LDAP_DEBUG_CONNS,
1307 "connection_read(%d): closing, ignoring input for id=%lu\n",
1308 s, c->c_connid, 0 );
1309 connection_return( c );
1313 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1314 cri->func = c->c_clientfunc;
1315 cri->arg = c->c_clientarg;
1316 /* read should already be cleared */
1317 connection_return( c );
1321 Debug( LDAP_DEBUG_TRACE,
1322 "connection_read(%d): checking for input on id=%lu\n",
1323 s, c->c_connid, 0 );
1326 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1327 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1329 Debug( LDAP_DEBUG_TRACE,
1330 "connection_read(%d): TLS accept failure "
1331 "error=%d id=%lu, closing\n",
1332 s, rc, c->c_connid );
1334 c->c_needs_tls_accept = 0;
1335 /* c_mutex is locked */
1336 connection_closing( c, "TLS negotiation failure" );
1337 connection_close( c );
1338 connection_return( c );
1341 } else if ( rc == 0 ) {
1343 struct berval authid = BER_BVNULL;
1345 c->c_needs_tls_accept = 0;
1347 /* we need to let SASL know */
1348 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1350 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1351 if( c->c_tls_ssf > c->c_ssf ) {
1352 c->c_ssf = c->c_tls_ssf;
1355 rc = dnX509peerNormalize( ssl, &authid );
1356 if ( rc != LDAP_SUCCESS ) {
1357 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1358 "unable to get TLS client DN, error=%d id=%lu\n",
1359 s, rc, c->c_connid );
1361 Statslog( LDAP_DEBUG_STATS,
1362 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1363 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1364 slap_sasl_external( c, c->c_tls_ssf, &authid );
1365 if ( authid.bv_val ) free( authid.bv_val );
1368 /* if success and data is ready, fall thru to data input loop */
1369 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1371 slapd_set_read( s, 1 );
1372 connection_return( c );
1378 #ifdef HAVE_CYRUS_SASL
1379 if ( c->c_sasl_layers ) {
1380 /* If previous layer is not removed yet, give up for now */
1381 if ( !c->c_sasl_sockctx ) {
1382 slapd_set_read( s, 1 );
1383 connection_return( c );
1387 c->c_sasl_layers = 0;
1389 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1390 if( rc != LDAP_SUCCESS ) {
1391 Debug( LDAP_DEBUG_TRACE,
1392 "connection_read(%d): SASL install error "
1393 "error=%d id=%lu, closing\n",
1394 s, rc, c->c_connid );
1396 /* c_mutex is locked */
1397 connection_closing( c, "SASL layer install failure" );
1398 connection_close( c );
1399 connection_return( c );
1405 #define CONNECTION_INPUT_LOOP 1
1406 /* #define DATA_READY_LOOP 1 */
1409 /* How do we do this without getting into a busy loop ? */
1410 rc = connection_input( c, cri );
1412 #ifdef DATA_READY_LOOP
1413 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1414 #elif defined CONNECTION_INPUT_LOOP
1421 Debug( LDAP_DEBUG_CONNS,
1422 "connection_read(%d): input error=%d id=%lu, closing.\n",
1423 s, rc, c->c_connid );
1425 /* c_mutex is locked */
1426 connection_closing( c, conn_lost_str );
1427 connection_close( c );
1428 connection_return( c );
1432 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1433 slapd_set_write( s, 0 );
1436 slapd_set_read( s, 1 );
1437 connection_return( c );
1443 connection_input( Connection *conn , conn_readinfo *cri )
1451 #ifdef LDAP_CONNECTIONLESS
1458 if ( conn->c_currentber == NULL &&
1459 ( conn->c_currentber = ber_alloc()) == NULL )
1461 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1467 #ifdef LDAP_CONNECTIONLESS
1468 if ( conn->c_is_udp ) {
1469 char peername[sizeof("IP=255.255.255.255:65336")];
1471 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1472 if (len != sizeof(struct sockaddr)) return 1;
1474 sprintf( peername, "IP=%s:%d",
1475 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1476 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1477 Statslog( LDAP_DEBUG_STATS,
1478 "conn=%lu UDP request from %s (%s) accepted.\n",
1479 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1483 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1484 if ( tag != LDAP_TAG_MESSAGE ) {
1485 int err = sock_errno();
1487 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1488 /* log, close and send error */
1489 Debug( LDAP_DEBUG_TRACE,
1490 "ber_get_next on fd %d failed errno=%d (%s)\n",
1491 conn->c_sd, err, sock_errstr(err) );
1492 ber_free( conn->c_currentber, 1 );
1493 conn->c_currentber = NULL;
1500 ber = conn->c_currentber;
1501 conn->c_currentber = NULL;
1503 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1504 /* log, close and send error */
1505 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1510 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1511 /* log, close and send error */
1512 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1518 #ifdef LDAP_CONNECTIONLESS
1519 if( conn->c_is_udp ) {
1520 if( tag == LBER_OCTETSTRING ) {
1521 ber_get_stringa( ber, &cdn );
1522 tag = ber_peek_tag(ber, &len);
1524 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1525 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1532 if(tag == LDAP_REQ_BIND) {
1533 /* immediately abandon all existing operations upon BIND */
1534 connection_abandon( conn );
1538 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++, ctx );
1540 Debug( LDAP_DEBUG_TRACE, "op tag 0x%lx, time %ld\n", tag,
1541 (long) op->o_time, 0);
1544 /* clear state if the connection is being reused from inactive */
1545 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1546 memset( &conn->c_pagedresults_state, 0,
1547 sizeof( conn->c_pagedresults_state ) );
1550 op->o_res_ber = NULL;
1552 #ifdef LDAP_CONNECTIONLESS
1553 if (conn->c_is_udp) {
1555 ber_str2bv( cdn, 0, 1, &op->o_dn );
1556 op->o_protocol = LDAP_VERSION2;
1558 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1559 if (op->o_res_ber == NULL) return 1;
1561 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1562 sizeof(struct sockaddr), 0 );
1564 if (rc != sizeof(struct sockaddr)) {
1565 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1569 if (op->o_protocol == LDAP_VERSION2) {
1570 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1572 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1577 #endif /* LDAP_CONNECTIONLESS */
1581 /* Don't process requests when the conn is in the middle of a
1582 * Bind, or if it's closing. Also, don't let any single conn
1583 * use up all the available threads, and don't execute if we're
1584 * currently blocked on output. And don't execute if there are
1585 * already pending ops, let them go first. Abandon operations
1586 * get exceptions to some, but not all, cases.
1590 /* Abandon and Unbind are exempt from these checks */
1591 if (conn->c_conn_state == SLAP_C_CLOSING) {
1594 } else if (conn->c_writewaiter) {
1595 defer = "awaiting write";
1597 } else if (conn->c_n_ops_pending) {
1598 defer = "pending operations";
1602 case LDAP_REQ_ABANDON:
1603 /* Unbind is exempt from these checks */
1604 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1605 defer = "too many executing";
1607 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1612 case LDAP_REQ_UNBIND:
1617 int max = conn->c_dn.bv_len
1618 ? slap_conn_max_pending_auth
1619 : slap_conn_max_pending;
1621 Debug( LDAP_DEBUG_ANY,
1622 "connection_input: conn=%lu deferring operation: %s\n",
1623 conn->c_connid, defer, 0 );
1624 conn->c_n_ops_pending++;
1625 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1626 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1629 conn->c_n_ops_executing++;
1632 * The first op will be processed in the same thread context,
1633 * as long as there is only one op total.
1634 * Subsequent ops will be submitted to the pool by
1635 * calling connection_op_activate()
1637 if ( cri->op == NULL ) {
1638 /* the first incoming request */
1639 connection_op_queue( op );
1642 if ( !cri->nullop ) {
1644 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1645 connection_operation, (void *) cri->op );
1647 connection_op_activate( op );
1652 if ( conn->c_struct_state != SLAP_C_USED ) {
1653 /* connection must have got closed underneath us */
1658 assert( conn->c_struct_state == SLAP_C_USED );
1663 connection_resched( Connection *conn )
1667 if( conn->c_writewaiter )
1670 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1671 Debug( LDAP_DEBUG_CONNS, "connection_resched: "
1672 "attempting closing conn=%lu sd=%d\n",
1673 conn->c_connid, conn->c_sd, 0 );
1674 connection_close( conn );
1678 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1679 /* other states need different handling */
1683 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1684 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1686 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1687 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1689 /* pending operations should not be marked for abandonment */
1690 assert(!op->o_abandon);
1692 conn->c_n_ops_pending--;
1693 conn->c_n_ops_executing++;
1695 connection_op_activate( op );
1697 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1703 connection_init_log_prefix( Operation *op )
1705 if ( op->o_connid == (unsigned long)(-1) ) {
1706 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1707 "conn=-1 op=%lu", op->o_opid );
1710 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1711 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1715 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1717 op->o_conn->c_sasl_bindop = NULL;
1719 ch_free( op->o_callback );
1720 op->o_callback = NULL;
1722 return SLAP_CB_CONTINUE;
1725 static int connection_bind_cb( Operation *op, SlapReply *rs )
1727 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1728 op->o_conn->c_sasl_bind_in_progress =
1729 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1731 /* Moved here from bind.c due to ITS#4158 */
1732 op->o_conn->c_sasl_bindop = NULL;
1733 if ( op->orb_method == LDAP_AUTH_SASL ) {
1734 if( rs->sr_err == LDAP_SUCCESS ) {
1735 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1736 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1737 /* edn is always normalized already */
1738 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1740 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1741 BER_BVZERO( &op->orb_edn );
1742 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1743 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1745 op->o_conn->c_sasl_ssf = op->orb_ssf;
1746 if( op->orb_ssf > op->o_conn->c_ssf ) {
1747 op->o_conn->c_ssf = op->orb_ssf;
1750 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1751 ber_len_t max = sockbuf_max_incoming_auth;
1752 ber_sockbuf_ctrl( op->o_conn->c_sb,
1753 LBER_SB_OPT_SET_MAX_INCOMING, &max );
1756 /* log authorization identity */
1757 Statslog( LDAP_DEBUG_STATS,
1758 "%s BIND dn=\"%s\" mech=%s sasl_ssf=%d ssf=%d\n",
1760 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1761 op->o_conn->c_authmech.bv_val,
1762 op->orb_ssf, op->o_conn->c_ssf );
1764 Debug( LDAP_DEBUG_TRACE,
1765 "do_bind: SASL/%s bind: dn=\"%s\" sasl_ssf=%d\n",
1766 op->o_conn->c_authmech.bv_val,
1767 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1770 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1771 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1772 free( op->o_conn->c_sasl_bind_mech.bv_val );
1773 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1777 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1779 ch_free( op->o_callback );
1780 op->o_callback = NULL;
1782 return SLAP_CB_CONTINUE;
1785 static void connection_op_queue( Operation *op )
1787 ber_tag_t tag = op->o_tag;
1789 if (tag == LDAP_REQ_BIND) {
1790 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1791 sc->sc_response = connection_bind_cb;
1792 sc->sc_cleanup = connection_bind_cleanup_cb;
1793 sc->sc_next = op->o_callback;
1794 op->o_callback = sc;
1795 op->o_conn->c_conn_state = SLAP_C_BINDING;
1798 if (!op->o_dn.bv_len) {
1799 op->o_authz = op->o_conn->c_authz;
1800 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1801 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1802 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1804 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1805 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1809 op->o_authtype = op->o_conn->c_authtype;
1810 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1812 if (!op->o_protocol) {
1813 op->o_protocol = op->o_conn->c_protocol
1814 ? op->o_conn->c_protocol : LDAP_VERSION3;
1817 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1818 op->o_protocol > LDAP_VERSION2)
1820 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1823 op->o_connid = op->o_conn->c_connid;
1824 connection_init_log_prefix( op );
1826 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1829 static int connection_op_activate( Operation *op )
1833 connection_op_queue( op );
1835 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1836 connection_operation, (void *) op );
1839 Debug( LDAP_DEBUG_ANY,
1840 "connection_op_activate: submit failed (%d) for conn=%lu\n",
1841 rc, op->o_connid, 0 );
1842 /* should move op to pending list */
1848 int connection_write(ber_socket_t s)
1853 assert( connections != NULL );
1855 slapd_clr_write( s, 0 );
1857 c = connection_get( s );
1859 Debug( LDAP_DEBUG_ANY,
1860 "connection_write(%ld): no connection!\n",
1867 Debug( LDAP_DEBUG_TRACE,
1868 "connection_write(%d): waking output for id=%lu\n",
1869 s, c->c_connid, 0 );
1870 ldap_pvt_thread_mutex_lock( &c->c_write2_mutex );
1871 ldap_pvt_thread_cond_signal( &c->c_write2_cv );
1872 ldap_pvt_thread_mutex_unlock( &c->c_write2_mutex );
1874 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1875 slapd_set_read( s, 1 );
1877 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1878 slapd_set_write( s, 1 );
1881 /* If there are ops pending because of a writewaiter,
1884 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1885 if ( !c->c_writewaiter ) break;
1886 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
1888 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1889 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1891 /* pending operations should not be marked for abandonment */
1892 assert(!op->o_abandon);
1894 c->c_n_ops_pending--;
1895 c->c_n_ops_executing++;
1897 connection_op_activate( op );
1902 connection_return( c );
1907 typedef struct conn_fake_extblock {
1910 } conn_fake_extblock;
1913 connection_fake_destroy(
1917 Connection conn = {0};
1919 Opheader ohdr = {0};
1921 conn_fake_extblock *eb = data;
1924 op.o_hdr->oh_extensions = eb->eb_op;
1925 conn.c_extensions = eb->eb_conn;
1930 ber_memfree_x( eb, NULL );
1931 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, &op );
1932 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &conn );
1937 connection_fake_init(
1939 OperationBuffer *opbuf,
1942 connection_fake_init2( conn, opbuf, ctx, 1 );
1946 operation_fake_init(
1952 /* set memory context */
1953 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx,
1955 op->o_tmpmfuncs = &slap_sl_mfuncs;
1956 op->o_threadctx = ctx;
1957 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1959 op->o_counters = &slap_counters;
1961 op->o_connid = op->o_conn->c_connid;
1962 connection_init_log_prefix( op );
1967 connection_fake_init2(
1969 OperationBuffer *opbuf,
1973 Operation *op = (Operation *) opbuf;
1975 conn->c_connid = -1;
1976 conn->c_conn_idx = -1;
1977 conn->c_send_ldap_result = slap_send_ldap_result;
1978 conn->c_send_search_entry = slap_send_search_entry;
1979 conn->c_send_search_reference = slap_send_search_reference;
1980 conn->c_send_ldap_extended = slap_send_ldap_extended;
1981 conn->c_send_ldap_intermediate = slap_send_ldap_intermediate;
1982 conn->c_listener = (Listener *)&dummy_list;
1983 conn->c_peer_domain = slap_empty_bv;
1984 conn->c_peer_name = slap_empty_bv;
1986 memset( opbuf, 0, sizeof( *opbuf ));
1987 op->o_hdr = &opbuf->ob_hdr;
1988 op->o_controls = opbuf->ob_controls;
1990 operation_fake_init( conn, op, ctx, newmem );
1993 if ( slapi_plugins_used ) {
1994 conn_fake_extblock *eb;
1997 /* Use thread keys to make sure these eventually get cleaned up */
1998 if ( ldap_pvt_thread_pool_getkey( ctx, (void *)connection_fake_init,
2000 eb = ch_malloc( sizeof( *eb ));
2001 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
2002 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
2003 eb->eb_conn = conn->c_extensions;
2004 eb->eb_op = op->o_hdr->oh_extensions;
2005 ldap_pvt_thread_pool_setkey( ctx, (void *)connection_fake_init,
2006 eb, connection_fake_destroy, NULL, NULL );
2009 conn->c_extensions = eb->eb_conn;
2010 op->o_hdr->oh_extensions = eb->eb_op;
2013 #endif /* LDAP_SLAPI */
2015 slap_op_time( &op->o_time, &op->o_tincr );
2019 connection_assign_nextid( Connection *conn )
2021 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2022 conn->c_connid = conn_nextid++;
2023 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );