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 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
57 #define SLAP_C_UNUSED 0x01
58 #define SLAP_C_USED 0x02
59 #define SLAP_C_PENDING 0x03
61 /* connection state (protected by c_mutex ) */
62 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
63 #define SLAP_C_INACTIVE 0x01 /* zero threads */
64 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
65 #define SLAP_C_BINDING 0x03 /* binding */
66 #define SLAP_C_CLOSING 0x04 /* closing */
67 #define SLAP_C_CLIENT 0x05 /* outbound client conn */
70 connection_state2str( int state )
73 case SLAP_C_INVALID: return "!";
74 case SLAP_C_INACTIVE: return "|";
75 case SLAP_C_ACTIVE: return "";
76 case SLAP_C_BINDING: return "B";
77 case SLAP_C_CLOSING: return "C";
78 case SLAP_C_CLIENT: return "L";
84 static Connection* connection_get( ber_socket_t s );
86 typedef struct conn_readinfo {
88 ldap_pvt_thread_start_t *func;
94 static int connection_input( Connection *c, conn_readinfo *cri );
95 static void connection_close( Connection *c );
97 static int connection_op_activate( Operation *op );
98 static void connection_op_queue( Operation *op );
99 static int connection_resched( Connection *conn );
100 static void connection_abandon( Connection *conn );
101 static void connection_destroy( Connection *c );
103 static ldap_pvt_thread_start_t connection_operation;
106 * Initialize connection management infrastructure.
108 int connections_init(void)
112 assert( connections == NULL );
114 if( connections != NULL) {
115 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
120 /* should check return of every call */
121 ldap_pvt_thread_mutex_init( &connections_mutex );
122 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
124 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
126 if( connections == NULL ) {
127 Debug( LDAP_DEBUG_ANY, "connections_init: "
128 "allocation (%d*%ld) of connection array failed\n",
129 dtblsize, (long) sizeof(Connection), 0 );
133 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
134 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
136 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
139 * per entry initialization of the Connection array initialization
140 * will be done by connection_init()
147 * Destroy connection management infrastructure.
150 int connections_destroy(void)
154 /* should check return of every call */
156 if( connections == NULL) {
157 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
162 for ( i = 0; i < dtblsize; i++ ) {
163 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
164 ber_sockbuf_free( connections[i].c_sb );
165 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
166 ldap_pvt_thread_mutex_destroy( &connections[i].c_write1_mutex );
167 ldap_pvt_thread_mutex_destroy( &connections[i].c_write2_mutex );
168 ldap_pvt_thread_cond_destroy( &connections[i].c_write1_cv );
169 ldap_pvt_thread_cond_destroy( &connections[i].c_write2_cv );
171 if ( slapi_plugins_used ) {
172 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
182 ldap_pvt_thread_mutex_destroy( &connections_mutex );
183 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
188 * shutdown all connections
190 int connections_shutdown(void)
194 for ( i = 0; i < dtblsize; i++ ) {
195 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
196 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
197 if( connections[i].c_struct_state == SLAP_C_USED ) {
199 /* give persistent clients a chance to cleanup */
200 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
201 ldap_pvt_thread_pool_submit( &connection_pool,
202 connections[i].c_clientfunc, connections[i].c_clientarg );
204 /* c_mutex is locked */
205 connection_closing( &connections[i], "slapd shutdown" );
206 connection_close( &connections[i] );
209 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
217 * Timeout idle connections.
219 int connections_timeout_idle(time_t now)
225 for( c = connection_first( &connindex );
227 c = connection_next( c, &connindex ) )
229 /* Don't timeout a slow-running request or a persistent
230 * outbound connection */
231 if( c->c_n_ops_executing || c->c_conn_state == SLAP_C_CLIENT ) {
235 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
237 connection_closing( c, "idletimeout" );
238 connection_close( c );
242 connection_done( c );
247 static Connection* connection_get( ber_socket_t s )
251 Debug( LDAP_DEBUG_ARGS,
252 "connection_get(%ld)\n",
255 assert( connections != NULL );
257 if(s == AC_SOCKET_INVALID) return NULL;
259 assert( s < dtblsize );
263 ldap_pvt_thread_mutex_lock( &c->c_mutex );
265 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
267 if( c->c_struct_state != SLAP_C_USED ) {
268 /* connection must have been closed due to resched */
270 assert( c->c_conn_state == SLAP_C_INVALID );
271 assert( c->c_sd == AC_SOCKET_INVALID );
273 Debug( LDAP_DEBUG_CONNS,
274 "connection_get(%d): connection not used\n",
277 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
281 Debug( LDAP_DEBUG_TRACE,
282 "connection_get(%d): got connid=%lu\n",
287 assert( c->c_struct_state == SLAP_C_USED );
288 assert( c->c_conn_state != SLAP_C_INVALID );
289 assert( c->c_sd != AC_SOCKET_INVALID );
291 #ifndef SLAPD_MONITOR
292 if ( global_idletimeout > 0 )
293 #endif /* ! SLAPD_MONITOR */
295 c->c_activitytime = slap_get_time();
302 static void connection_return( Connection *c )
304 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
307 Connection * connection_init(
311 const char* peername,
314 struct berval *authid
315 LDAP_PF_LOCAL_SENDMSG_ARG(struct berval *peerbv))
320 ber_socket_t sfd = SLAP_FD2SOCK(s);
322 assert( connections != NULL );
324 assert( listener != NULL );
325 assert( dnsname != NULL );
326 assert( peername != NULL );
329 assert( !( flags & CONN_IS_TLS ));
332 if( s == AC_SOCKET_INVALID ) {
333 Debug( LDAP_DEBUG_ANY,
334 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
339 assert( s < dtblsize );
341 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
344 assert( c->c_struct_state == SLAP_C_UNUSED );
348 c->c_send_ldap_result = slap_send_ldap_result;
349 c->c_send_search_entry = slap_send_search_entry;
350 c->c_send_search_reference = slap_send_search_reference;
351 c->c_send_ldap_extended = slap_send_ldap_extended;
352 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
354 BER_BVZERO( &c->c_authmech );
355 BER_BVZERO( &c->c_dn );
356 BER_BVZERO( &c->c_ndn );
358 c->c_listener = NULL;
359 BER_BVZERO( &c->c_peer_domain );
360 BER_BVZERO( &c->c_peer_name );
362 LDAP_STAILQ_INIT(&c->c_ops);
363 LDAP_STAILQ_INIT(&c->c_pending_ops);
366 c->c_txn = CONN_TXN_INACTIVE;
367 c->c_txn_backend = NULL;
368 LDAP_STAILQ_INIT(&c->c_txn_ops);
371 BER_BVZERO( &c->c_sasl_bind_mech );
373 c->c_sasl_authctx = NULL;
374 c->c_sasl_sockctx = NULL;
375 c->c_sasl_extra = NULL;
376 c->c_sasl_bindop = NULL;
378 c->c_sb = ber_sockbuf_alloc( );
381 ber_len_t max = sockbuf_max_incoming;
382 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
385 c->c_currentber = NULL;
387 /* should check status of thread calls */
388 ldap_pvt_thread_mutex_init( &c->c_mutex );
389 ldap_pvt_thread_mutex_init( &c->c_write1_mutex );
390 ldap_pvt_thread_mutex_init( &c->c_write2_mutex );
391 ldap_pvt_thread_cond_init( &c->c_write1_cv );
392 ldap_pvt_thread_cond_init( &c->c_write2_cv );
395 if ( slapi_plugins_used ) {
396 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
401 ldap_pvt_thread_mutex_lock( &c->c_mutex );
403 assert( BER_BVISNULL( &c->c_authmech ) );
404 assert( BER_BVISNULL( &c->c_dn ) );
405 assert( BER_BVISNULL( &c->c_ndn ) );
406 assert( c->c_listener == NULL );
407 assert( BER_BVISNULL( &c->c_peer_domain ) );
408 assert( BER_BVISNULL( &c->c_peer_name ) );
409 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
410 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
412 assert( c->c_txn == CONN_TXN_INACTIVE );
413 assert( c->c_txn_backend == NULL );
414 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
416 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
417 assert( c->c_sasl_done == 0 );
418 assert( c->c_sasl_authctx == NULL );
419 assert( c->c_sasl_sockctx == NULL );
420 assert( c->c_sasl_extra == NULL );
421 assert( c->c_sasl_bindop == NULL );
422 assert( c->c_currentber == NULL );
423 assert( c->c_writewaiter == 0);
424 assert( c->c_writers == 0);
426 c->c_listener = listener;
429 if ( flags & CONN_IS_CLIENT ) {
431 ldap_pvt_thread_mutex_lock( &connections_mutex );
432 c->c_conn_state = SLAP_C_CLIENT;
433 c->c_struct_state = SLAP_C_USED;
434 ldap_pvt_thread_mutex_unlock( &connections_mutex );
435 c->c_close_reason = "?"; /* should never be needed */
436 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &sfd );
437 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
442 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
443 ber_str2bv( peername, 0, 1, &c->c_peer_name );
445 c->c_n_ops_received = 0;
446 c->c_n_ops_executing = 0;
447 c->c_n_ops_pending = 0;
448 c->c_n_ops_completed = 0;
454 /* set to zero until bind, implies LDAP_VERSION3 */
457 #ifndef SLAPD_MONITOR
458 if ( global_idletimeout > 0 )
459 #endif /* ! SLAPD_MONITOR */
461 c->c_activitytime = c->c_starttime = slap_get_time();
464 #ifdef LDAP_CONNECTIONLESS
466 if( flags & CONN_IS_UDP ) {
469 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
470 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
472 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
473 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
474 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
475 LBER_SBIOD_LEVEL_PROVIDER, NULL );
477 #endif /* LDAP_CONNECTIONLESS */
479 if ( flags & CONN_IS_IPC ) {
481 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
482 LBER_SBIOD_LEVEL_PROVIDER, (void*)"ipc_" );
484 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_fd,
485 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
486 #ifdef LDAP_PF_LOCAL_SENDMSG
487 if ( !BER_BVISEMPTY( peerbv ))
488 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_UNGET_BUF, peerbv );
491 #endif /* LDAP_PF_LOCAL */
494 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
495 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
497 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
498 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
502 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
503 INT_MAX, (void*)"ldap_" );
506 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
507 c /* non-NULL */ ) < 0 )
509 Debug( LDAP_DEBUG_ANY,
510 "connection_init(%d, %s): set nonblocking failed\n",
511 s, c->c_peer_name.bv_val, 0 );
514 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
515 id = c->c_connid = conn_nextid++;
516 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
518 ldap_pvt_thread_mutex_lock( &connections_mutex );
519 c->c_conn_state = SLAP_C_INACTIVE;
520 c->c_struct_state = SLAP_C_USED;
521 ldap_pvt_thread_mutex_unlock( &connections_mutex );
522 c->c_close_reason = "?"; /* should never be needed */
524 c->c_ssf = c->c_transport_ssf = ssf;
528 if ( flags & CONN_IS_TLS ) {
530 c->c_needs_tls_accept = 1;
533 c->c_needs_tls_accept = 0;
537 slap_sasl_open( c, 0 );
538 slap_sasl_external( c, ssf, authid );
540 slapd_add_internal( s, 1 );
541 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
543 backend_connection_init(c);
548 void connection2anonymous( Connection *c )
550 assert( connections != NULL );
554 ber_len_t max = sockbuf_max_incoming;
555 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
558 if ( !BER_BVISNULL( &c->c_authmech ) ) {
559 ch_free(c->c_authmech.bv_val);
561 BER_BVZERO( &c->c_authmech );
563 if ( !BER_BVISNULL( &c->c_dn ) ) {
564 ch_free(c->c_dn.bv_val);
566 BER_BVZERO( &c->c_dn );
568 if ( !BER_BVISNULL( &c->c_ndn ) ) {
569 ch_free(c->c_ndn.bv_val);
571 BER_BVZERO( &c->c_ndn );
573 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
574 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
576 BER_BVZERO( &c->c_sasl_authz_dn );
578 c->c_authz_backend = NULL;
582 connection_destroy( Connection *c )
584 unsigned long connid;
585 const char *close_reason;
589 assert( connections != NULL );
591 assert( c->c_struct_state != SLAP_C_UNUSED );
592 assert( c->c_conn_state != SLAP_C_INVALID );
593 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
594 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
596 assert( c->c_txn == CONN_TXN_INACTIVE );
597 assert( c->c_txn_backend == NULL );
598 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
600 assert( c->c_writewaiter == 0);
601 assert( c->c_writers == 0);
603 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
604 connid = c->c_connid;
605 close_reason = c->c_close_reason;
607 ldap_pvt_thread_mutex_lock( &connections_mutex );
608 c->c_struct_state = SLAP_C_PENDING;
609 ldap_pvt_thread_mutex_unlock( &connections_mutex );
611 backend_connection_destroy(c);
616 c->c_activitytime = c->c_starttime = 0;
618 connection2anonymous( c );
619 c->c_listener = NULL;
621 if(c->c_peer_domain.bv_val != NULL) {
622 free(c->c_peer_domain.bv_val);
624 BER_BVZERO( &c->c_peer_domain );
625 if(c->c_peer_name.bv_val != NULL) {
626 free(c->c_peer_name.bv_val);
628 BER_BVZERO( &c->c_peer_name );
630 c->c_sasl_bind_in_progress = 0;
631 if(c->c_sasl_bind_mech.bv_val != NULL) {
632 free(c->c_sasl_bind_mech.bv_val);
634 BER_BVZERO( &c->c_sasl_bind_mech );
636 slap_sasl_close( c );
638 if ( c->c_currentber != NULL ) {
639 ber_free( c->c_currentber, 1 );
640 c->c_currentber = NULL;
645 /* call destructors, then constructors; avoids unnecessary allocation */
646 if ( slapi_plugins_used ) {
647 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
652 c->c_sd = AC_SOCKET_INVALID;
653 c->c_conn_state = SLAP_C_INVALID;
654 c->c_struct_state = SLAP_C_UNUSED;
655 c->c_close_reason = "?"; /* should never be needed */
658 c->c_sb = ber_sockbuf_alloc( );
660 ber_len_t max = sockbuf_max_incoming;
661 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
664 /* c must be fully reset by this point; when we call slapd_remove
665 * it may get immediately reused by a new connection.
667 if ( sd != AC_SOCKET_INVALID ) {
668 slapd_remove( sd, sb, 1, 0, 0 );
670 if ( close_reason == NULL ) {
671 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
672 connid, (long) sd, 0, 0, 0 );
674 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
675 connid, (long) sd, close_reason, 0, 0 );
680 int connection_state_closing( Connection *c )
682 /* c_mutex must be locked by caller */
686 assert( c->c_struct_state == SLAP_C_USED );
688 state = c->c_conn_state;
690 assert( state != SLAP_C_INVALID );
692 return state == SLAP_C_CLOSING;
695 static void connection_abandon( Connection *c )
697 /* c_mutex must be locked by caller */
699 Operation *o, *next, op = {0};
705 op.o_connid = c->c_connid;
706 op.o_tag = LDAP_REQ_ABANDON;
708 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
709 next = LDAP_STAILQ_NEXT( o, o_next );
710 op.orn_msgid = o->o_msgid;
712 op.o_bd = frontendDB;
713 frontendDB->be_abandon( &op, &rs );
717 /* remove operations in pending transaction */
718 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
719 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
720 LDAP_STAILQ_NEXT(o, o_next) = NULL;
721 slap_op_free( o, NULL );
724 /* clear transaction */
725 c->c_txn_backend = NULL;
726 c->c_txn = CONN_TXN_INACTIVE;
729 /* remove pending operations */
730 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
731 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
732 LDAP_STAILQ_NEXT(o, o_next) = NULL;
733 slap_op_free( o, NULL );
737 void connection_closing( Connection *c, const char *why )
739 assert( connections != NULL );
741 assert( c->c_struct_state == SLAP_C_USED );
742 assert( c->c_conn_state != SLAP_C_INVALID );
744 /* c_mutex must be locked by caller */
746 if( c->c_conn_state != SLAP_C_CLOSING ) {
747 Debug( LDAP_DEBUG_CONNS,
748 "connection_closing: readying conn=%lu sd=%d for close\n",
749 c->c_connid, c->c_sd, 0 );
750 /* update state to closing */
751 c->c_conn_state = SLAP_C_CLOSING;
752 c->c_close_reason = why;
754 /* don't listen on this port anymore */
755 slapd_clr_read( c->c_sd, 0 );
757 /* abandon active operations */
758 connection_abandon( c );
760 /* wake write blocked operations */
761 ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
762 if ( c->c_writers > 0 ) {
763 c->c_writers = -c->c_writers;
764 ldap_pvt_thread_cond_broadcast( &c->c_write1_cv );
765 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
766 if ( c->c_writewaiter ) {
767 ldap_pvt_thread_mutex_lock( &c->c_write2_mutex );
768 ldap_pvt_thread_cond_signal( &c->c_write2_cv );
769 slapd_clr_write( c->c_sd, 1 );
770 ldap_pvt_thread_mutex_unlock( &c->c_write2_mutex );
772 ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
773 while ( c->c_writers ) {
774 ldap_pvt_thread_cond_wait( &c->c_write1_cv, &c->c_write1_mutex );
776 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
778 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
779 slapd_clr_write( c->c_sd, 1 );
782 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
783 /* Client closed connection after doing Unbind. */
784 c->c_close_reason = NULL;
789 connection_close( Connection *c )
791 assert( connections != NULL );
793 assert( c->c_struct_state == SLAP_C_USED );
794 assert( c->c_conn_state == SLAP_C_CLOSING );
796 /* NOTE: c_mutex should be locked by caller */
798 if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
799 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
801 Debug( LDAP_DEBUG_CONNS,
802 "connection_close: deferring conn=%lu sd=%d\n",
803 c->c_connid, c->c_sd, 0 );
807 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
808 c->c_connid, c->c_sd, 0 );
810 connection_destroy( c );
813 unsigned long connections_nextid(void)
816 assert( connections != NULL );
818 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
822 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
827 Connection* connection_first( ber_socket_t *index )
829 assert( connections != NULL );
830 assert( index != NULL );
832 ldap_pvt_thread_mutex_lock( &connections_mutex );
833 for( *index = 0; *index < dtblsize; (*index)++) {
834 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
838 ldap_pvt_thread_mutex_unlock( &connections_mutex );
840 return connection_next(NULL, index);
843 Connection* connection_next( Connection *c, ber_socket_t *index )
845 assert( connections != NULL );
846 assert( index != NULL );
847 assert( *index <= dtblsize );
849 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
853 ldap_pvt_thread_mutex_lock( &connections_mutex );
854 for(; *index < dtblsize; (*index)++) {
856 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
857 /* FIXME: accessing c_conn_state without locking c_mutex */
858 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
862 if( connections[*index].c_struct_state == SLAP_C_USED ) {
863 c = &connections[(*index)++];
864 if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
866 ldap_pvt_thread_mutex_unlock( &connections_mutex );
867 ldap_pvt_thread_mutex_lock( &c->c_mutex );
868 ldap_pvt_thread_mutex_lock( &connections_mutex );
869 if ( c->c_struct_state != SLAP_C_USED ) {
870 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
875 assert( c->c_conn_state != SLAP_C_INVALID );
879 c_struct = connections[*index].c_struct_state;
880 if ( c_struct == SLAP_C_PENDING )
882 assert( c_struct == SLAP_C_UNUSED );
883 /* FIXME: accessing c_conn_state without locking c_mutex */
884 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
887 ldap_pvt_thread_mutex_unlock( &connections_mutex );
891 void connection_done( Connection *c )
893 assert( connections != NULL );
895 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
899 * connection_activity - handle the request operation op on connection
900 * conn. This routine figures out what kind of operation it is and
901 * calls the appropriate stub to handle it.
905 /* FIXME: returns 0 in case of failure */
906 #define INCR_OP_INITIATED(index) \
908 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
909 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated_[(index)], 1); \
910 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
912 #define INCR_OP_COMPLETED(index) \
914 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
915 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
916 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed_[(index)], 1); \
917 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
919 #else /* !SLAPD_MONITOR */
920 #define INCR_OP_INITIATED(index) do { } while (0)
921 #define INCR_OP_COMPLETED(index) \
923 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
924 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
925 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
927 #endif /* !SLAPD_MONITOR */
930 * NOTE: keep in sync with enum in slapd.h
932 static BI_op_func *opfun[] = {
946 /* Counters are per-thread, not per-connection.
949 conn_counter_destroy( void *key, void *data )
951 slap_counters_t **prev, *sc;
953 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
954 for ( prev = &slap_counters.sc_next, sc = slap_counters.sc_next; sc;
955 prev = &sc->sc_next, sc = sc->sc_next ) {
960 /* Copy data to main counter */
961 ldap_pvt_mp_add( slap_counters.sc_bytes, sc->sc_bytes );
962 ldap_pvt_mp_add( slap_counters.sc_pdu, sc->sc_pdu );
963 ldap_pvt_mp_add( slap_counters.sc_entries, sc->sc_entries );
964 ldap_pvt_mp_add( slap_counters.sc_refs, sc->sc_refs );
965 ldap_pvt_mp_add( slap_counters.sc_ops_initiated, sc->sc_ops_initiated );
966 ldap_pvt_mp_add( slap_counters.sc_ops_completed, sc->sc_ops_completed );
968 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
969 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_initiated_[ i ] );
970 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_completed_[ i ] );
972 #endif /* SLAPD_MONITOR */
973 slap_counters_destroy( sc );
974 ber_memfree_x( data, NULL );
978 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
982 conn_counter_init( Operation *op, void *ctx )
987 if ( ldap_pvt_thread_pool_getkey(
988 ctx, (void *)conn_counter_init, &vsc, NULL ) || !vsc ) {
989 vsc = ch_malloc( sizeof( slap_counters_t ));
991 slap_counters_init( sc );
992 ldap_pvt_thread_pool_setkey( ctx, (void*)conn_counter_init, vsc,
993 conn_counter_destroy, NULL, NULL );
995 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
996 sc->sc_next = slap_counters.sc_next;
997 slap_counters.sc_next = sc;
998 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
1000 op->o_counters = vsc;
1004 connection_operation( void *ctx, void *arg_v )
1006 int rc = LDAP_OTHER;
1007 Operation *op = arg_v;
1008 SlapReply rs = {REP_RESULT};
1009 ber_tag_t tag = op->o_tag;
1010 slap_op_t opidx = SLAP_OP_LAST;
1011 Connection *conn = op->o_conn;
1012 void *memctx = NULL;
1013 void *memctx_null = NULL;
1016 conn_counter_init( op, ctx );
1017 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1018 /* FIXME: returns 0 in case of failure */
1019 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated, 1);
1020 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1022 op->o_threadctx = ctx;
1023 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1027 case LDAP_REQ_UNBIND:
1029 case LDAP_REQ_DELETE:
1030 case LDAP_REQ_MODDN:
1031 case LDAP_REQ_MODIFY:
1032 case LDAP_REQ_COMPARE:
1033 case LDAP_REQ_SEARCH:
1034 case LDAP_REQ_ABANDON:
1035 case LDAP_REQ_EXTENDED:
1038 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1039 "conn %lu unknown LDAP request 0x%lx\n",
1040 conn->c_connid, tag, 0 );
1041 op->o_tag = LBER_ERROR;
1042 rs.sr_err = LDAP_PROTOCOL_ERROR;
1043 rs.sr_text = "unknown LDAP request";
1044 send_ldap_disconnect( op, &rs );
1045 rc = SLAPD_DISCONNECT;
1046 goto operations_error;
1049 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1050 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1051 "error: SASL bind in progress (tag=%ld).\n",
1053 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1054 "SASL bind in progress" );
1055 rc = LDAP_OPERATIONS_ERROR;
1056 goto operations_error;
1060 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1061 ( tag == LDAP_REQ_ADD ) ||
1062 ( tag == LDAP_REQ_DELETE ) ||
1063 ( tag == LDAP_REQ_MODIFY ) ||
1064 ( tag == LDAP_REQ_MODRDN )))
1066 /* Disable SLAB allocator for all update operations
1067 issued inside of a transaction */
1068 op->o_tmpmemctx = NULL;
1069 op->o_tmpmfuncs = &ch_mfuncs;
1073 /* We can use Thread-Local storage for most mallocs. We can
1074 * also use TL for ber parsing, but not on Add or Modify.
1077 memsiz = ber_len( op->o_ber ) * 64;
1078 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1080 memsiz = SLAP_SLAB_SIZE;
1082 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx, 1 );
1083 op->o_tmpmemctx = memctx;
1084 op->o_tmpmfuncs = &slap_sl_mfuncs;
1085 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1086 /* Note - the ber and its buffer are already allocated from
1087 * regular memory; this only affects subsequent mallocs that
1088 * ber_scanf may invoke.
1090 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1094 opidx = slap_req2op( tag );
1095 assert( opidx != SLAP_OP_LAST );
1096 INCR_OP_INITIATED( opidx );
1097 rc = (*(opfun[opidx]))( op, &rs );
1100 if ( rc == SLAPD_DISCONNECT ) {
1103 } else if ( opidx != SLAP_OP_LAST ) {
1104 /* increment completed operations count
1105 * only if operation was initiated
1106 * and rc != SLAPD_DISCONNECT */
1107 INCR_OP_COMPLETED( opidx );
1110 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1111 if ( rc == SLAPD_ABANDON ) {
1112 op->o_cancel = SLAP_CANCEL_ACK;
1114 op->o_cancel = LDAP_TOO_LATE;
1118 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1119 op->o_cancel != SLAP_CANCEL_DONE )
1121 ldap_pvt_thread_yield();
1124 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1126 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1128 LDAP_STAILQ_REMOVE( &conn->c_ops, op, Operation, o_next);
1129 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1130 conn->c_n_ops_executing--;
1131 conn->c_n_ops_completed++;
1135 case LDAP_REQ_UNBIND:
1136 /* c_mutex is locked */
1137 connection_closing( conn,
1138 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1142 connection_resched( conn );
1143 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1144 slap_op_free( op, ctx );
1148 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1150 Connection *connection_client_setup(
1152 ldap_pvt_thread_start_t *func,
1156 ber_socket_t sfd = SLAP_SOCKNEW( s );
1158 c = connection_init( sfd, (Listener *)&dummy_list, "", "",
1159 CONN_IS_CLIENT, 0, NULL
1160 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
1162 c->c_clientfunc = func;
1163 c->c_clientarg = arg;
1165 slapd_add_internal( sfd, 0 );
1170 void connection_client_enable(
1173 slapd_set_read( c->c_sd, 1 );
1176 void connection_client_stop(
1180 ber_socket_t s = c->c_sd;
1182 /* get (locked) connection */
1183 c = connection_get( s );
1185 assert( c->c_conn_state == SLAP_C_CLIENT );
1187 c->c_listener = NULL;
1188 c->c_conn_state = SLAP_C_INVALID;
1189 c->c_struct_state = SLAP_C_UNUSED;
1190 c->c_sd = AC_SOCKET_INVALID;
1191 c->c_close_reason = "?"; /* should never be needed */
1193 c->c_sb = ber_sockbuf_alloc( );
1195 ber_len_t max = sockbuf_max_incoming;
1196 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1198 slapd_remove( s, sb, 0, 1, 0 );
1200 connection_return( c );
1203 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1205 static void* connection_read_thread( void* ctx, void* argv )
1208 conn_readinfo cri = { NULL, NULL, NULL, NULL, 0 };
1209 ber_socket_t s = (long)argv;
1212 * read incoming LDAP requests. If there is more than one,
1213 * the first one is returned with new_op
1216 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1217 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1218 return (void*)(long)rc;
1221 /* execute a single queued request in the same thread */
1222 if( cri.op && !cri.nullop ) {
1223 rc = (long)connection_operation( ctx, cri.op );
1224 } else if ( cri.func ) {
1225 rc = (long)cri.func( ctx, cri.arg );
1228 return (void*)(long)rc;
1231 int connection_read_activate( ber_socket_t s )
1236 * suspend reading on this file descriptor until a connection processing
1237 * thread reads data on it. Otherwise the listener thread will repeatedly
1238 * submit the same event on it to the pool.
1240 rc = slapd_clr_read( s, 0 );
1244 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1245 connection_read_thread, (void *)(long)s );
1248 Debug( LDAP_DEBUG_ANY,
1249 "connection_read_activate(%d): submit failed (%d)\n",
1257 connection_hangup( ber_socket_t s )
1261 c = connection_get( s );
1263 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1264 connection_return( c );
1265 connection_read_activate( s );
1267 connection_closing( c, "connection lost" );
1268 connection_close( c );
1269 connection_return( c );
1275 connection_read( ber_socket_t s, conn_readinfo *cri )
1280 assert( connections != NULL );
1282 /* get (locked) connection */
1283 c = connection_get( s );
1286 Debug( LDAP_DEBUG_ANY,
1287 "connection_read(%ld): no connection!\n",
1295 if( c->c_conn_state == SLAP_C_CLOSING ) {
1296 Debug( LDAP_DEBUG_CONNS,
1297 "connection_read(%d): closing, ignoring input for id=%lu\n",
1298 s, c->c_connid, 0 );
1299 connection_return( c );
1303 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1304 cri->func = c->c_clientfunc;
1305 cri->arg = c->c_clientarg;
1306 /* read should already be cleared */
1307 connection_return( c );
1311 Debug( LDAP_DEBUG_TRACE,
1312 "connection_read(%d): checking for input on id=%lu\n",
1313 s, c->c_connid, 0 );
1316 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1317 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1319 Debug( LDAP_DEBUG_TRACE,
1320 "connection_read(%d): TLS accept failure "
1321 "error=%d id=%lu, closing\n",
1322 s, rc, c->c_connid );
1324 c->c_needs_tls_accept = 0;
1325 /* c_mutex is locked */
1326 connection_closing( c, "TLS negotiation failure" );
1327 connection_close( c );
1328 connection_return( c );
1331 } else if ( rc == 0 ) {
1333 struct berval authid = BER_BVNULL;
1335 c->c_needs_tls_accept = 0;
1337 /* we need to let SASL know */
1338 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1340 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1341 if( c->c_tls_ssf > c->c_ssf ) {
1342 c->c_ssf = c->c_tls_ssf;
1345 rc = dnX509peerNormalize( ssl, &authid );
1346 if ( rc != LDAP_SUCCESS ) {
1347 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1348 "unable to get TLS client DN, error=%d id=%lu\n",
1349 s, rc, c->c_connid );
1351 Statslog( LDAP_DEBUG_STATS,
1352 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1353 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1354 slap_sasl_external( c, c->c_tls_ssf, &authid );
1355 if ( authid.bv_val ) free( authid.bv_val );
1358 /* if success and data is ready, fall thru to data input loop */
1359 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1361 slapd_set_read( s, 1 );
1362 connection_return( c );
1368 #ifdef HAVE_CYRUS_SASL
1369 if ( c->c_sasl_layers ) {
1370 /* If previous layer is not removed yet, give up for now */
1371 if ( !c->c_sasl_sockctx ) {
1372 slapd_set_read( s, 1 );
1373 connection_return( c );
1377 c->c_sasl_layers = 0;
1379 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1380 if( rc != LDAP_SUCCESS ) {
1381 Debug( LDAP_DEBUG_TRACE,
1382 "connection_read(%d): SASL install error "
1383 "error=%d id=%lu, closing\n",
1384 s, rc, c->c_connid );
1386 /* c_mutex is locked */
1387 connection_closing( c, "SASL layer install failure" );
1388 connection_close( c );
1389 connection_return( c );
1395 #define CONNECTION_INPUT_LOOP 1
1396 /* #define DATA_READY_LOOP 1 */
1399 /* How do we do this without getting into a busy loop ? */
1400 rc = connection_input( c, cri );
1402 #ifdef DATA_READY_LOOP
1403 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1404 #elif defined CONNECTION_INPUT_LOOP
1411 Debug( LDAP_DEBUG_CONNS,
1412 "connection_read(%d): input error=%d id=%lu, closing.\n",
1413 s, rc, c->c_connid );
1415 /* c_mutex is locked */
1416 connection_closing( c, conn_lost_str );
1417 connection_close( c );
1418 connection_return( c );
1422 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1423 slapd_set_write( s, 0 );
1426 slapd_set_read( s, 1 );
1427 connection_return( c );
1433 connection_input( Connection *conn , conn_readinfo *cri )
1441 #ifdef LDAP_CONNECTIONLESS
1448 if ( conn->c_currentber == NULL &&
1449 ( conn->c_currentber = ber_alloc()) == NULL )
1451 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1457 #ifdef LDAP_CONNECTIONLESS
1458 if ( conn->c_is_udp ) {
1459 char peername[sizeof("IP=255.255.255.255:65336")];
1461 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1462 if (len != sizeof(struct sockaddr)) return 1;
1464 sprintf( peername, "IP=%s:%d",
1465 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1466 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1467 Statslog( LDAP_DEBUG_STATS,
1468 "conn=%lu UDP request from %s (%s) accepted.\n",
1469 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1473 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1474 if ( tag != LDAP_TAG_MESSAGE ) {
1475 int err = sock_errno();
1477 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1478 /* log, close and send error */
1479 Debug( LDAP_DEBUG_TRACE,
1480 "ber_get_next on fd %d failed errno=%d (%s)\n",
1481 conn->c_sd, err, sock_errstr(err) );
1482 ber_free( conn->c_currentber, 1 );
1483 conn->c_currentber = NULL;
1490 ber = conn->c_currentber;
1491 conn->c_currentber = NULL;
1493 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1494 /* log, close and send error */
1495 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1500 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1501 /* log, close and send error */
1502 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1508 #ifdef LDAP_CONNECTIONLESS
1509 if( conn->c_is_udp ) {
1510 if( tag == LBER_OCTETSTRING ) {
1511 ber_get_stringa( ber, &cdn );
1512 tag = ber_peek_tag(ber, &len);
1514 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1515 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1522 if(tag == LDAP_REQ_BIND) {
1523 /* immediately abandon all existing operations upon BIND */
1524 connection_abandon( conn );
1528 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++, ctx );
1531 /* clear state if the connection is being reused from inactive */
1532 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1533 memset( &conn->c_pagedresults_state, 0,
1534 sizeof( conn->c_pagedresults_state ) );
1537 op->o_res_ber = NULL;
1539 #ifdef LDAP_CONNECTIONLESS
1540 if (conn->c_is_udp) {
1542 ber_str2bv( cdn, 0, 1, &op->o_dn );
1543 op->o_protocol = LDAP_VERSION2;
1545 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1546 if (op->o_res_ber == NULL) return 1;
1548 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1549 sizeof(struct sockaddr), 0 );
1551 if (rc != sizeof(struct sockaddr)) {
1552 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1556 if (op->o_protocol == LDAP_VERSION2) {
1557 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1559 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1564 #endif /* LDAP_CONNECTIONLESS */
1568 /* Don't process requests when the conn is in the middle of a
1569 * Bind, or if it's closing. Also, don't let any single conn
1570 * use up all the available threads, and don't execute if we're
1571 * currently blocked on output. And don't execute if there are
1572 * already pending ops, let them go first. Abandon operations
1573 * get exceptions to some, but not all, cases.
1577 /* Abandon and Unbind are exempt from these checks */
1578 if (conn->c_conn_state == SLAP_C_CLOSING) {
1581 } else if (conn->c_writewaiter) {
1582 defer = "awaiting write";
1584 } else if (conn->c_n_ops_pending) {
1585 defer = "pending operations";
1589 case LDAP_REQ_ABANDON:
1590 /* Unbind is exempt from these checks */
1591 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1592 defer = "too many executing";
1594 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1599 case LDAP_REQ_UNBIND:
1604 int max = conn->c_dn.bv_len
1605 ? slap_conn_max_pending_auth
1606 : slap_conn_max_pending;
1608 Debug( LDAP_DEBUG_ANY,
1609 "connection_input: conn=%lu deferring operation: %s\n",
1610 conn->c_connid, defer, 0 );
1611 conn->c_n_ops_pending++;
1612 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1613 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1616 conn->c_n_ops_executing++;
1619 * The first op will be processed in the same thread context,
1620 * as long as there is only one op total.
1621 * Subsequent ops will be submitted to the pool by
1622 * calling connection_op_activate()
1624 if ( cri->op == NULL ) {
1625 /* the first incoming request */
1626 connection_op_queue( op );
1629 if ( !cri->nullop ) {
1631 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1632 connection_operation, (void *) cri->op );
1634 connection_op_activate( op );
1639 if ( conn->c_struct_state != SLAP_C_USED ) {
1640 /* connection must have got closed underneath us */
1645 assert( conn->c_struct_state == SLAP_C_USED );
1650 connection_resched( Connection *conn )
1654 if( conn->c_writewaiter )
1657 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1658 Debug( LDAP_DEBUG_CONNS, "connection_resched: "
1659 "attempting closing conn=%lu sd=%d\n",
1660 conn->c_connid, conn->c_sd, 0 );
1661 connection_close( conn );
1665 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1666 /* other states need different handling */
1670 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1671 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1673 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1674 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1676 /* pending operations should not be marked for abandonment */
1677 assert(!op->o_abandon);
1679 conn->c_n_ops_pending--;
1680 conn->c_n_ops_executing++;
1682 connection_op_activate( op );
1684 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1690 connection_init_log_prefix( Operation *op )
1692 if ( op->o_connid == (unsigned long)(-1) ) {
1693 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1694 "conn=-1 op=%lu", op->o_opid );
1697 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1698 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1702 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1704 op->o_conn->c_sasl_bindop = NULL;
1706 ch_free( op->o_callback );
1707 op->o_callback = NULL;
1709 return SLAP_CB_CONTINUE;
1712 static int connection_bind_cb( Operation *op, SlapReply *rs )
1714 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1715 if ( op->o_conn->c_conn_state == SLAP_C_BINDING )
1716 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1717 op->o_conn->c_sasl_bind_in_progress =
1718 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1720 /* Moved here from bind.c due to ITS#4158 */
1721 op->o_conn->c_sasl_bindop = NULL;
1722 if ( op->orb_method == LDAP_AUTH_SASL ) {
1723 if( rs->sr_err == LDAP_SUCCESS ) {
1724 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1725 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1726 /* edn is always normalized already */
1727 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1729 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1730 BER_BVZERO( &op->orb_edn );
1731 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1732 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1734 op->o_conn->c_sasl_ssf = op->orb_ssf;
1735 if( op->orb_ssf > op->o_conn->c_ssf ) {
1736 op->o_conn->c_ssf = op->orb_ssf;
1739 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1740 ber_len_t max = sockbuf_max_incoming_auth;
1741 ber_sockbuf_ctrl( op->o_conn->c_sb,
1742 LBER_SB_OPT_SET_MAX_INCOMING, &max );
1745 /* log authorization identity */
1746 Statslog( LDAP_DEBUG_STATS,
1747 "%s BIND dn=\"%s\" mech=%s sasl_ssf=%d ssf=%d\n",
1749 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1750 op->o_conn->c_authmech.bv_val,
1751 op->orb_ssf, op->o_conn->c_ssf );
1753 Debug( LDAP_DEBUG_TRACE,
1754 "do_bind: SASL/%s bind: dn=\"%s\" sasl_ssf=%d\n",
1755 op->o_conn->c_authmech.bv_val,
1756 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1759 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1760 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1761 free( op->o_conn->c_sasl_bind_mech.bv_val );
1762 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1766 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1768 ch_free( op->o_callback );
1769 op->o_callback = NULL;
1771 return SLAP_CB_CONTINUE;
1774 static void connection_op_queue( Operation *op )
1776 ber_tag_t tag = op->o_tag;
1778 if (tag == LDAP_REQ_BIND) {
1779 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1780 sc->sc_response = connection_bind_cb;
1781 sc->sc_cleanup = connection_bind_cleanup_cb;
1782 sc->sc_next = op->o_callback;
1783 op->o_callback = sc;
1784 op->o_conn->c_conn_state = SLAP_C_BINDING;
1787 if (!op->o_dn.bv_len) {
1788 op->o_authz = op->o_conn->c_authz;
1789 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1790 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1791 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1793 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1794 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1798 op->o_authtype = op->o_conn->c_authtype;
1799 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1801 if (!op->o_protocol) {
1802 op->o_protocol = op->o_conn->c_protocol
1803 ? op->o_conn->c_protocol : LDAP_VERSION3;
1806 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1807 op->o_protocol > LDAP_VERSION2)
1809 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1812 op->o_connid = op->o_conn->c_connid;
1813 connection_init_log_prefix( op );
1815 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1818 static int connection_op_activate( Operation *op )
1822 connection_op_queue( op );
1824 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1825 connection_operation, (void *) op );
1828 Debug( LDAP_DEBUG_ANY,
1829 "connection_op_activate: submit failed (%d) for conn=%lu\n",
1830 rc, op->o_connid, 0 );
1831 /* should move op to pending list */
1837 int connection_write(ber_socket_t s)
1842 assert( connections != NULL );
1844 slapd_clr_write( s, 0 );
1846 c = connection_get( s );
1848 Debug( LDAP_DEBUG_ANY,
1849 "connection_write(%ld): no connection!\n",
1856 Debug( LDAP_DEBUG_TRACE,
1857 "connection_write(%d): waking output for id=%lu\n",
1858 s, c->c_connid, 0 );
1859 ldap_pvt_thread_cond_signal( &c->c_write2_cv );
1861 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1862 slapd_set_read( s, 1 );
1864 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1865 slapd_set_write( s, 1 );
1868 /* If there are ops pending because of a writewaiter,
1871 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1872 if ( !c->c_writewaiter ) break;
1873 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
1875 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1876 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1878 /* pending operations should not be marked for abandonment */
1879 assert(!op->o_abandon);
1881 c->c_n_ops_pending--;
1882 c->c_n_ops_executing++;
1884 connection_op_activate( op );
1889 connection_return( c );
1894 typedef struct conn_fake_extblock {
1897 } conn_fake_extblock;
1900 connection_fake_destroy(
1904 Connection conn = {0};
1906 Opheader ohdr = {0};
1908 conn_fake_extblock *eb = data;
1911 op.o_hdr->oh_extensions = eb->eb_op;
1912 conn.c_extensions = eb->eb_conn;
1917 ber_memfree_x( eb, NULL );
1918 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, &op );
1919 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &conn );
1924 connection_fake_init(
1926 OperationBuffer *opbuf,
1929 connection_fake_init2( conn, opbuf, ctx, 1 );
1933 operation_fake_init(
1939 /* set memory context */
1940 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx,
1942 op->o_tmpmfuncs = &slap_sl_mfuncs;
1943 op->o_threadctx = ctx;
1944 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1946 op->o_counters = &slap_counters;
1948 op->o_connid = op->o_conn->c_connid;
1949 connection_init_log_prefix( op );
1954 connection_fake_init2(
1956 OperationBuffer *opbuf,
1960 Operation *op = (Operation *) opbuf;
1962 conn->c_connid = -1;
1963 conn->c_conn_idx = -1;
1964 conn->c_send_ldap_result = slap_send_ldap_result;
1965 conn->c_send_search_entry = slap_send_search_entry;
1966 conn->c_send_search_reference = slap_send_search_reference;
1967 conn->c_listener = (Listener *)&dummy_list;
1968 conn->c_peer_domain = slap_empty_bv;
1969 conn->c_peer_name = slap_empty_bv;
1971 memset( opbuf, 0, sizeof( *opbuf ));
1972 op->o_hdr = &opbuf->ob_hdr;
1973 op->o_controls = opbuf->ob_controls;
1975 operation_fake_init( conn, op, ctx, newmem );
1978 if ( slapi_plugins_used ) {
1979 conn_fake_extblock *eb;
1982 /* Use thread keys to make sure these eventually get cleaned up */
1983 if ( ldap_pvt_thread_pool_getkey( ctx, (void *)connection_fake_init,
1985 eb = ch_malloc( sizeof( *eb ));
1986 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
1987 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
1988 eb->eb_conn = conn->c_extensions;
1989 eb->eb_op = op->o_hdr->oh_extensions;
1990 ldap_pvt_thread_pool_setkey( ctx, (void *)connection_fake_init,
1991 eb, connection_fake_destroy, NULL, NULL );
1994 conn->c_extensions = eb->eb_conn;
1995 op->o_hdr->oh_extensions = eb->eb_op;
1998 #endif /* LDAP_SLAPI */
2000 slap_op_time( &op->o_time, &op->o_tincr );
2004 connection_assign_nextid( Connection *conn )
2006 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2007 conn->c_connid = conn_nextid++;
2008 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );