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( global_idletimeout &&
246 difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
248 connection_closing( c, "idletimeout" );
249 connection_close( c );
253 if ( c->c_writewaiter && global_writetimeout ) {
255 if( difftime( c->c_activitytime+global_writetimeout, now) < 0 ) {
257 connection_closing( c, "writetimeout" );
258 connection_close( c );
263 connection_done( c );
264 if ( old && !writers )
265 slapd_clr_writetime( old );
270 static Connection* connection_get( ber_socket_t s )
274 Debug( LDAP_DEBUG_ARGS,
275 "connection_get(%ld)\n",
278 assert( connections != NULL );
280 if(s == AC_SOCKET_INVALID) return NULL;
282 assert( s < dtblsize );
286 ldap_pvt_thread_mutex_lock( &c->c_mutex );
288 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
290 if( c->c_struct_state != SLAP_C_USED ) {
291 /* connection must have been closed due to resched */
293 assert( c->c_conn_state == SLAP_C_INVALID );
294 assert( c->c_sd == AC_SOCKET_INVALID );
296 Debug( LDAP_DEBUG_CONNS,
297 "connection_get(%d): connection not used\n",
300 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
304 Debug( LDAP_DEBUG_TRACE,
305 "connection_get(%d): got connid=%lu\n",
310 assert( c->c_struct_state == SLAP_C_USED );
311 assert( c->c_conn_state != SLAP_C_INVALID );
312 assert( c->c_sd != AC_SOCKET_INVALID );
314 #ifndef SLAPD_MONITOR
315 if ( global_idletimeout > 0 )
316 #endif /* ! SLAPD_MONITOR */
318 c->c_activitytime = slap_get_time();
325 static void connection_return( Connection *c )
327 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
330 Connection * connection_init(
334 const char* peername,
337 struct berval *authid
338 LDAP_PF_LOCAL_SENDMSG_ARG(struct berval *peerbv))
343 ber_socket_t sfd = SLAP_FD2SOCK(s);
345 assert( connections != NULL );
347 assert( listener != NULL );
348 assert( dnsname != NULL );
349 assert( peername != NULL );
352 assert( !( flags & CONN_IS_TLS ));
355 if( s == AC_SOCKET_INVALID ) {
356 Debug( LDAP_DEBUG_ANY,
357 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
362 assert( s < dtblsize );
364 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
367 assert( c->c_struct_state == SLAP_C_UNUSED );
371 c->c_send_ldap_result = slap_send_ldap_result;
372 c->c_send_search_entry = slap_send_search_entry;
373 c->c_send_search_reference = slap_send_search_reference;
374 c->c_send_ldap_extended = slap_send_ldap_extended;
375 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
377 BER_BVZERO( &c->c_authmech );
378 BER_BVZERO( &c->c_dn );
379 BER_BVZERO( &c->c_ndn );
381 c->c_listener = NULL;
382 BER_BVZERO( &c->c_peer_domain );
383 BER_BVZERO( &c->c_peer_name );
385 LDAP_STAILQ_INIT(&c->c_ops);
386 LDAP_STAILQ_INIT(&c->c_pending_ops);
389 c->c_txn = CONN_TXN_INACTIVE;
390 c->c_txn_backend = NULL;
391 LDAP_STAILQ_INIT(&c->c_txn_ops);
394 BER_BVZERO( &c->c_sasl_bind_mech );
396 c->c_sasl_authctx = NULL;
397 c->c_sasl_sockctx = NULL;
398 c->c_sasl_extra = NULL;
399 c->c_sasl_bindop = NULL;
401 c->c_sb = ber_sockbuf_alloc( );
404 ber_len_t max = sockbuf_max_incoming;
405 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
408 c->c_currentber = NULL;
410 /* should check status of thread calls */
411 ldap_pvt_thread_mutex_init( &c->c_mutex );
412 ldap_pvt_thread_mutex_init( &c->c_write1_mutex );
413 ldap_pvt_thread_mutex_init( &c->c_write2_mutex );
414 ldap_pvt_thread_cond_init( &c->c_write1_cv );
415 ldap_pvt_thread_cond_init( &c->c_write2_cv );
418 if ( slapi_plugins_used ) {
419 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
424 ldap_pvt_thread_mutex_lock( &c->c_mutex );
426 assert( BER_BVISNULL( &c->c_authmech ) );
427 assert( BER_BVISNULL( &c->c_dn ) );
428 assert( BER_BVISNULL( &c->c_ndn ) );
429 assert( c->c_listener == NULL );
430 assert( BER_BVISNULL( &c->c_peer_domain ) );
431 assert( BER_BVISNULL( &c->c_peer_name ) );
432 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
433 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
435 assert( c->c_txn == CONN_TXN_INACTIVE );
436 assert( c->c_txn_backend == NULL );
437 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
439 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
440 assert( c->c_sasl_done == 0 );
441 assert( c->c_sasl_authctx == NULL );
442 assert( c->c_sasl_sockctx == NULL );
443 assert( c->c_sasl_extra == NULL );
444 assert( c->c_sasl_bindop == NULL );
445 assert( c->c_currentber == NULL );
446 assert( c->c_writewaiter == 0);
447 assert( c->c_writers == 0);
449 c->c_listener = listener;
452 if ( flags & CONN_IS_CLIENT ) {
454 ldap_pvt_thread_mutex_lock( &connections_mutex );
455 c->c_conn_state = SLAP_C_CLIENT;
456 c->c_struct_state = SLAP_C_USED;
457 ldap_pvt_thread_mutex_unlock( &connections_mutex );
458 c->c_close_reason = "?"; /* should never be needed */
459 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &sfd );
460 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
465 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
466 ber_str2bv( peername, 0, 1, &c->c_peer_name );
468 c->c_n_ops_received = 0;
469 c->c_n_ops_executing = 0;
470 c->c_n_ops_pending = 0;
471 c->c_n_ops_completed = 0;
477 /* set to zero until bind, implies LDAP_VERSION3 */
480 #ifndef SLAPD_MONITOR
481 if ( global_idletimeout > 0 )
482 #endif /* ! SLAPD_MONITOR */
484 c->c_activitytime = c->c_starttime = slap_get_time();
487 #ifdef LDAP_CONNECTIONLESS
489 if( flags & CONN_IS_UDP ) {
492 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
493 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
495 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
496 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
497 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
498 LBER_SBIOD_LEVEL_PROVIDER, NULL );
500 #endif /* LDAP_CONNECTIONLESS */
502 if ( flags & CONN_IS_IPC ) {
504 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
505 LBER_SBIOD_LEVEL_PROVIDER, (void*)"ipc_" );
507 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_fd,
508 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
509 #ifdef LDAP_PF_LOCAL_SENDMSG
510 if ( !BER_BVISEMPTY( peerbv ))
511 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_UNGET_BUF, peerbv );
514 #endif /* LDAP_PF_LOCAL */
517 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
518 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
520 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
521 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
525 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
526 INT_MAX, (void*)"ldap_" );
529 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
530 c /* non-NULL */ ) < 0 )
532 Debug( LDAP_DEBUG_ANY,
533 "connection_init(%d, %s): set nonblocking failed\n",
534 s, c->c_peer_name.bv_val, 0 );
537 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
538 id = c->c_connid = conn_nextid++;
539 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
541 ldap_pvt_thread_mutex_lock( &connections_mutex );
542 c->c_conn_state = SLAP_C_INACTIVE;
543 c->c_struct_state = SLAP_C_USED;
544 ldap_pvt_thread_mutex_unlock( &connections_mutex );
545 c->c_close_reason = "?"; /* should never be needed */
547 c->c_ssf = c->c_transport_ssf = ssf;
551 if ( flags & CONN_IS_TLS ) {
553 c->c_needs_tls_accept = 1;
556 c->c_needs_tls_accept = 0;
560 slap_sasl_open( c, 0 );
561 slap_sasl_external( c, ssf, authid );
563 slapd_add_internal( s, 1 );
564 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
566 backend_connection_init(c);
571 void connection2anonymous( Connection *c )
573 assert( connections != NULL );
577 ber_len_t max = sockbuf_max_incoming;
578 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
581 if ( !BER_BVISNULL( &c->c_authmech ) ) {
582 ch_free(c->c_authmech.bv_val);
584 BER_BVZERO( &c->c_authmech );
586 if ( !BER_BVISNULL( &c->c_dn ) ) {
587 ch_free(c->c_dn.bv_val);
589 BER_BVZERO( &c->c_dn );
591 if ( !BER_BVISNULL( &c->c_ndn ) ) {
592 ch_free(c->c_ndn.bv_val);
594 BER_BVZERO( &c->c_ndn );
596 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
597 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
599 BER_BVZERO( &c->c_sasl_authz_dn );
601 c->c_authz_backend = NULL;
605 connection_destroy( Connection *c )
607 unsigned long connid;
608 const char *close_reason;
612 assert( connections != NULL );
614 assert( c->c_struct_state != SLAP_C_UNUSED );
615 assert( c->c_conn_state != SLAP_C_INVALID );
616 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
617 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
619 assert( c->c_txn == CONN_TXN_INACTIVE );
620 assert( c->c_txn_backend == NULL );
621 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
623 assert( c->c_writewaiter == 0);
624 assert( c->c_writers == 0);
626 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
627 connid = c->c_connid;
628 close_reason = c->c_close_reason;
630 ldap_pvt_thread_mutex_lock( &connections_mutex );
631 c->c_struct_state = SLAP_C_PENDING;
632 ldap_pvt_thread_mutex_unlock( &connections_mutex );
634 backend_connection_destroy(c);
639 c->c_activitytime = c->c_starttime = 0;
641 connection2anonymous( c );
642 c->c_listener = NULL;
644 if(c->c_peer_domain.bv_val != NULL) {
645 free(c->c_peer_domain.bv_val);
647 BER_BVZERO( &c->c_peer_domain );
648 if(c->c_peer_name.bv_val != NULL) {
649 free(c->c_peer_name.bv_val);
651 BER_BVZERO( &c->c_peer_name );
653 c->c_sasl_bind_in_progress = 0;
654 if(c->c_sasl_bind_mech.bv_val != NULL) {
655 free(c->c_sasl_bind_mech.bv_val);
657 BER_BVZERO( &c->c_sasl_bind_mech );
659 slap_sasl_close( c );
661 if ( c->c_currentber != NULL ) {
662 ber_free( c->c_currentber, 1 );
663 c->c_currentber = NULL;
668 /* call destructors, then constructors; avoids unnecessary allocation */
669 if ( slapi_plugins_used ) {
670 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
675 c->c_sd = AC_SOCKET_INVALID;
676 c->c_conn_state = SLAP_C_INVALID;
677 c->c_struct_state = SLAP_C_UNUSED;
678 c->c_close_reason = "?"; /* should never be needed */
681 c->c_sb = ber_sockbuf_alloc( );
683 ber_len_t max = sockbuf_max_incoming;
684 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
687 /* c must be fully reset by this point; when we call slapd_remove
688 * it may get immediately reused by a new connection.
690 if ( sd != AC_SOCKET_INVALID ) {
691 slapd_remove( sd, sb, 1, 0, 0 );
693 if ( close_reason == NULL ) {
694 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
695 connid, (long) sd, 0, 0, 0 );
697 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
698 connid, (long) sd, close_reason, 0, 0 );
703 int connection_valid( Connection *c )
705 /* c_mutex must be locked by caller */
709 return c->c_struct_state == SLAP_C_USED &&
710 c->c_conn_state >= SLAP_C_ACTIVE &&
711 c->c_conn_state <= SLAP_C_CLIENT;
714 static void connection_abandon( Connection *c )
716 /* c_mutex must be locked by caller */
718 Operation *o, *next, op = {0};
724 op.o_connid = c->c_connid;
725 op.o_tag = LDAP_REQ_ABANDON;
727 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
728 next = LDAP_STAILQ_NEXT( o, o_next );
729 op.orn_msgid = o->o_msgid;
731 op.o_bd = frontendDB;
732 frontendDB->be_abandon( &op, &rs );
736 /* remove operations in pending transaction */
737 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
738 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
739 LDAP_STAILQ_NEXT(o, o_next) = NULL;
740 slap_op_free( o, NULL );
743 /* clear transaction */
744 c->c_txn_backend = NULL;
745 c->c_txn = CONN_TXN_INACTIVE;
748 /* remove pending operations */
749 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
750 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
751 LDAP_STAILQ_NEXT(o, o_next) = NULL;
752 slap_op_free( o, NULL );
756 void connection_closing( Connection *c, const char *why )
758 assert( connections != NULL );
760 assert( c->c_struct_state == SLAP_C_USED );
761 assert( c->c_conn_state != SLAP_C_INVALID );
763 /* c_mutex must be locked by caller */
765 if( c->c_conn_state != SLAP_C_CLOSING ) {
766 Debug( LDAP_DEBUG_CONNS,
767 "connection_closing: readying conn=%lu sd=%d for close\n",
768 c->c_connid, c->c_sd, 0 );
769 /* update state to closing */
770 c->c_conn_state = SLAP_C_CLOSING;
771 c->c_close_reason = why;
773 /* don't listen on this port anymore */
774 slapd_clr_read( c->c_sd, 0 );
776 /* abandon active operations */
777 connection_abandon( c );
779 /* wake write blocked operations */
780 ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
781 if ( c->c_writers > 0 ) {
782 c->c_writers = -c->c_writers;
783 ldap_pvt_thread_cond_broadcast( &c->c_write1_cv );
784 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
785 if ( c->c_writewaiter ) {
786 ldap_pvt_thread_mutex_lock( &c->c_write2_mutex );
787 ldap_pvt_thread_cond_signal( &c->c_write2_cv );
788 slapd_clr_write( c->c_sd, 1 );
789 ldap_pvt_thread_mutex_unlock( &c->c_write2_mutex );
791 ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
792 while ( c->c_writers ) {
793 ldap_pvt_thread_cond_wait( &c->c_write1_cv, &c->c_write1_mutex );
795 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
797 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
798 slapd_clr_write( c->c_sd, 1 );
801 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
802 /* Client closed connection after doing Unbind. */
803 c->c_close_reason = NULL;
808 connection_close( Connection *c )
810 assert( connections != NULL );
812 assert( c->c_struct_state == SLAP_C_USED );
813 assert( c->c_conn_state == SLAP_C_CLOSING );
815 /* NOTE: c_mutex should be locked by caller */
817 if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
818 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
820 Debug( LDAP_DEBUG_CONNS,
821 "connection_close: deferring conn=%lu sd=%d\n",
822 c->c_connid, c->c_sd, 0 );
826 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
827 c->c_connid, c->c_sd, 0 );
829 connection_destroy( c );
832 unsigned long connections_nextid(void)
835 assert( connections != NULL );
837 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
841 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
846 Connection* connection_first( ber_socket_t *index )
848 assert( connections != NULL );
849 assert( index != NULL );
851 ldap_pvt_thread_mutex_lock( &connections_mutex );
852 for( *index = 0; *index < dtblsize; (*index)++) {
853 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
857 ldap_pvt_thread_mutex_unlock( &connections_mutex );
859 return connection_next(NULL, index);
862 Connection* connection_next( Connection *c, ber_socket_t *index )
864 assert( connections != NULL );
865 assert( index != NULL );
866 assert( *index <= dtblsize );
868 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
872 ldap_pvt_thread_mutex_lock( &connections_mutex );
873 for(; *index < dtblsize; (*index)++) {
875 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
876 /* FIXME: accessing c_conn_state without locking c_mutex */
877 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
881 if( connections[*index].c_struct_state == SLAP_C_USED ) {
882 c = &connections[(*index)++];
883 if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
885 ldap_pvt_thread_mutex_unlock( &connections_mutex );
886 ldap_pvt_thread_mutex_lock( &c->c_mutex );
887 ldap_pvt_thread_mutex_lock( &connections_mutex );
888 if ( c->c_struct_state != SLAP_C_USED ) {
889 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
894 assert( c->c_conn_state != SLAP_C_INVALID );
898 c_struct = connections[*index].c_struct_state;
899 if ( c_struct == SLAP_C_PENDING )
901 assert( c_struct == SLAP_C_UNUSED );
902 /* FIXME: accessing c_conn_state without locking c_mutex */
903 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
906 ldap_pvt_thread_mutex_unlock( &connections_mutex );
910 void connection_done( Connection *c )
912 assert( connections != NULL );
914 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
918 * connection_activity - handle the request operation op on connection
919 * conn. This routine figures out what kind of operation it is and
920 * calls the appropriate stub to handle it.
924 /* FIXME: returns 0 in case of failure */
925 #define INCR_OP_INITIATED(index) \
927 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
928 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated_[(index)], 1); \
929 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
931 #define INCR_OP_COMPLETED(index) \
933 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
934 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
935 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed_[(index)], 1); \
936 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
938 #else /* !SLAPD_MONITOR */
939 #define INCR_OP_INITIATED(index) do { } while (0)
940 #define INCR_OP_COMPLETED(index) \
942 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
943 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
944 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
946 #endif /* !SLAPD_MONITOR */
949 * NOTE: keep in sync with enum in slapd.h
951 static BI_op_func *opfun[] = {
965 /* Counters are per-thread, not per-connection.
968 conn_counter_destroy( void *key, void *data )
970 slap_counters_t **prev, *sc;
972 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
973 for ( prev = &slap_counters.sc_next, sc = slap_counters.sc_next; sc;
974 prev = &sc->sc_next, sc = sc->sc_next ) {
979 /* Copy data to main counter */
980 ldap_pvt_mp_add( slap_counters.sc_bytes, sc->sc_bytes );
981 ldap_pvt_mp_add( slap_counters.sc_pdu, sc->sc_pdu );
982 ldap_pvt_mp_add( slap_counters.sc_entries, sc->sc_entries );
983 ldap_pvt_mp_add( slap_counters.sc_refs, sc->sc_refs );
984 ldap_pvt_mp_add( slap_counters.sc_ops_initiated, sc->sc_ops_initiated );
985 ldap_pvt_mp_add( slap_counters.sc_ops_completed, sc->sc_ops_completed );
987 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
988 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_initiated_[ i ] );
989 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_completed_[ i ] );
991 #endif /* SLAPD_MONITOR */
992 slap_counters_destroy( sc );
993 ber_memfree_x( data, NULL );
997 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
1001 conn_counter_init( Operation *op, void *ctx )
1003 slap_counters_t *sc;
1006 if ( ldap_pvt_thread_pool_getkey(
1007 ctx, (void *)conn_counter_init, &vsc, NULL ) || !vsc ) {
1008 vsc = ch_malloc( sizeof( slap_counters_t ));
1010 slap_counters_init( sc );
1011 ldap_pvt_thread_pool_setkey( ctx, (void*)conn_counter_init, vsc,
1012 conn_counter_destroy, NULL, NULL );
1014 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
1015 sc->sc_next = slap_counters.sc_next;
1016 slap_counters.sc_next = sc;
1017 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
1019 op->o_counters = vsc;
1023 connection_operation( void *ctx, void *arg_v )
1025 int rc = LDAP_OTHER, cancel;
1026 Operation *op = arg_v;
1027 SlapReply rs = {REP_RESULT};
1028 ber_tag_t tag = op->o_tag;
1029 slap_op_t opidx = SLAP_OP_LAST;
1030 Connection *conn = op->o_conn;
1031 void *memctx = NULL;
1032 void *memctx_null = NULL;
1035 conn_counter_init( op, ctx );
1036 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1037 /* FIXME: returns 0 in case of failure */
1038 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated, 1);
1039 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1041 op->o_threadctx = ctx;
1042 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1046 case LDAP_REQ_UNBIND:
1048 case LDAP_REQ_DELETE:
1049 case LDAP_REQ_MODDN:
1050 case LDAP_REQ_MODIFY:
1051 case LDAP_REQ_COMPARE:
1052 case LDAP_REQ_SEARCH:
1053 case LDAP_REQ_ABANDON:
1054 case LDAP_REQ_EXTENDED:
1057 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1058 "conn %lu unknown LDAP request 0x%lx\n",
1059 conn->c_connid, tag, 0 );
1060 op->o_tag = LBER_ERROR;
1061 rs.sr_err = LDAP_PROTOCOL_ERROR;
1062 rs.sr_text = "unknown LDAP request";
1063 send_ldap_disconnect( op, &rs );
1064 rc = SLAPD_DISCONNECT;
1065 goto operations_error;
1068 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1069 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1070 "error: SASL bind in progress (tag=%ld).\n",
1072 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1073 "SASL bind in progress" );
1074 rc = LDAP_OPERATIONS_ERROR;
1075 goto operations_error;
1079 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1080 ( tag == LDAP_REQ_ADD ) ||
1081 ( tag == LDAP_REQ_DELETE ) ||
1082 ( tag == LDAP_REQ_MODIFY ) ||
1083 ( tag == LDAP_REQ_MODRDN )))
1085 /* Disable SLAB allocator for all update operations
1086 issued inside of a transaction */
1087 op->o_tmpmemctx = NULL;
1088 op->o_tmpmfuncs = &ch_mfuncs;
1092 /* We can use Thread-Local storage for most mallocs. We can
1093 * also use TL for ber parsing, but not on Add or Modify.
1096 memsiz = ber_len( op->o_ber ) * 64;
1097 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1099 memsiz = SLAP_SLAB_SIZE;
1101 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx, 1 );
1102 op->o_tmpmemctx = memctx;
1103 op->o_tmpmfuncs = &slap_sl_mfuncs;
1104 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1105 /* Note - the ber and its buffer are already allocated from
1106 * regular memory; this only affects subsequent mallocs that
1107 * ber_scanf may invoke.
1109 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1113 opidx = slap_req2op( tag );
1114 assert( opidx != SLAP_OP_LAST );
1115 INCR_OP_INITIATED( opidx );
1116 rc = (*(opfun[opidx]))( op, &rs );
1119 if ( rc == SLAPD_DISCONNECT ) {
1122 } else if ( opidx != SLAP_OP_LAST ) {
1123 /* increment completed operations count
1124 * only if operation was initiated
1125 * and rc != SLAPD_DISCONNECT */
1126 INCR_OP_COMPLETED( opidx );
1129 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1131 if ( opidx == SLAP_OP_BIND && conn->c_conn_state == SLAP_C_BINDING )
1132 conn->c_conn_state = SLAP_C_ACTIVE;
1134 cancel = op->o_cancel;
1135 if ( cancel != SLAP_CANCEL_NONE && cancel != SLAP_CANCEL_DONE ) {
1136 if ( cancel == SLAP_CANCEL_REQ ) {
1137 op->o_cancel = rc == SLAPD_ABANDON
1138 ? SLAP_CANCEL_ACK : LDAP_TOO_LATE;
1142 /* Fake a cond_wait with thread_yield, then
1143 * verify the result properly mutex-protected.
1145 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1147 ldap_pvt_thread_yield();
1148 } while ( (cancel = op->o_cancel) != SLAP_CANCEL_NONE
1149 && cancel != SLAP_CANCEL_DONE );
1150 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1151 } while ( (cancel = op->o_cancel) != SLAP_CANCEL_NONE
1152 && cancel != SLAP_CANCEL_DONE );
1155 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1157 LDAP_STAILQ_REMOVE( &conn->c_ops, op, Operation, o_next);
1158 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1159 conn->c_n_ops_executing--;
1160 conn->c_n_ops_completed++;
1164 case LDAP_REQ_UNBIND:
1165 /* c_mutex is locked */
1166 connection_closing( conn,
1167 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1171 connection_resched( conn );
1172 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1173 slap_op_free( op, ctx );
1177 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1179 Connection *connection_client_setup(
1181 ldap_pvt_thread_start_t *func,
1185 ber_socket_t sfd = SLAP_SOCKNEW( s );
1187 c = connection_init( sfd, (Listener *)&dummy_list, "", "",
1188 CONN_IS_CLIENT, 0, NULL
1189 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
1191 c->c_clientfunc = func;
1192 c->c_clientarg = arg;
1194 slapd_add_internal( sfd, 0 );
1199 void connection_client_enable(
1202 slapd_set_read( c->c_sd, 1 );
1205 void connection_client_stop(
1209 ber_socket_t s = c->c_sd;
1211 /* get (locked) connection */
1212 c = connection_get( s );
1214 assert( c->c_conn_state == SLAP_C_CLIENT );
1216 c->c_listener = NULL;
1217 c->c_conn_state = SLAP_C_INVALID;
1218 c->c_struct_state = SLAP_C_UNUSED;
1219 c->c_sd = AC_SOCKET_INVALID;
1220 c->c_close_reason = "?"; /* should never be needed */
1222 c->c_sb = ber_sockbuf_alloc( );
1224 ber_len_t max = sockbuf_max_incoming;
1225 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1227 slapd_remove( s, sb, 0, 1, 0 );
1229 connection_return( c );
1232 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1234 static void* connection_read_thread( void* ctx, void* argv )
1237 conn_readinfo cri = { NULL, NULL, NULL, NULL, 0 };
1238 ber_socket_t s = (long)argv;
1241 * read incoming LDAP requests. If there is more than one,
1242 * the first one is returned with new_op
1245 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1246 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1247 return (void*)(long)rc;
1250 /* execute a single queued request in the same thread */
1251 if( cri.op && !cri.nullop ) {
1252 rc = (long)connection_operation( ctx, cri.op );
1253 } else if ( cri.func ) {
1254 rc = (long)cri.func( ctx, cri.arg );
1257 return (void*)(long)rc;
1260 int connection_read_activate( ber_socket_t s )
1265 * suspend reading on this file descriptor until a connection processing
1266 * thread reads data on it. Otherwise the listener thread will repeatedly
1267 * submit the same event on it to the pool.
1269 rc = slapd_clr_read( s, 0 );
1273 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1274 connection_read_thread, (void *)(long)s );
1277 Debug( LDAP_DEBUG_ANY,
1278 "connection_read_activate(%d): submit failed (%d)\n",
1286 connection_read( ber_socket_t s, conn_readinfo *cri )
1291 assert( connections != NULL );
1293 /* get (locked) connection */
1294 c = connection_get( s );
1297 Debug( LDAP_DEBUG_ANY,
1298 "connection_read(%ld): no connection!\n",
1306 if( c->c_conn_state == SLAP_C_CLOSING ) {
1307 Debug( LDAP_DEBUG_CONNS,
1308 "connection_read(%d): closing, ignoring input for id=%lu\n",
1309 s, c->c_connid, 0 );
1310 connection_return( c );
1314 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1315 cri->func = c->c_clientfunc;
1316 cri->arg = c->c_clientarg;
1317 /* read should already be cleared */
1318 connection_return( c );
1322 Debug( LDAP_DEBUG_TRACE,
1323 "connection_read(%d): checking for input on id=%lu\n",
1324 s, c->c_connid, 0 );
1327 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1328 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1330 Debug( LDAP_DEBUG_TRACE,
1331 "connection_read(%d): TLS accept failure "
1332 "error=%d id=%lu, closing\n",
1333 s, rc, c->c_connid );
1335 c->c_needs_tls_accept = 0;
1336 /* c_mutex is locked */
1337 connection_closing( c, "TLS negotiation failure" );
1338 connection_close( c );
1339 connection_return( c );
1342 } else if ( rc == 0 ) {
1344 struct berval authid = BER_BVNULL;
1346 c->c_needs_tls_accept = 0;
1348 /* we need to let SASL know */
1349 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1351 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1352 if( c->c_tls_ssf > c->c_ssf ) {
1353 c->c_ssf = c->c_tls_ssf;
1356 rc = dnX509peerNormalize( ssl, &authid );
1357 if ( rc != LDAP_SUCCESS ) {
1358 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1359 "unable to get TLS client DN, error=%d id=%lu\n",
1360 s, rc, c->c_connid );
1362 Statslog( LDAP_DEBUG_STATS,
1363 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1364 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1365 slap_sasl_external( c, c->c_tls_ssf, &authid );
1366 if ( authid.bv_val ) free( authid.bv_val );
1369 /* if success and data is ready, fall thru to data input loop */
1370 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1372 slapd_set_read( s, 1 );
1373 connection_return( c );
1379 #ifdef HAVE_CYRUS_SASL
1380 if ( c->c_sasl_layers ) {
1381 /* If previous layer is not removed yet, give up for now */
1382 if ( !c->c_sasl_sockctx ) {
1383 slapd_set_read( s, 1 );
1384 connection_return( c );
1388 c->c_sasl_layers = 0;
1390 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1391 if( rc != LDAP_SUCCESS ) {
1392 Debug( LDAP_DEBUG_TRACE,
1393 "connection_read(%d): SASL install error "
1394 "error=%d id=%lu, closing\n",
1395 s, rc, c->c_connid );
1397 /* c_mutex is locked */
1398 connection_closing( c, "SASL layer install failure" );
1399 connection_close( c );
1400 connection_return( c );
1406 #define CONNECTION_INPUT_LOOP 1
1407 /* #define DATA_READY_LOOP 1 */
1410 /* How do we do this without getting into a busy loop ? */
1411 rc = connection_input( c, cri );
1413 #ifdef DATA_READY_LOOP
1414 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1415 #elif defined CONNECTION_INPUT_LOOP
1422 Debug( LDAP_DEBUG_CONNS,
1423 "connection_read(%d): input error=%d id=%lu, closing.\n",
1424 s, rc, c->c_connid );
1426 /* c_mutex is locked */
1427 connection_closing( c, conn_lost_str );
1428 connection_close( c );
1429 connection_return( c );
1433 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1434 slapd_set_write( s, 0 );
1437 slapd_set_read( s, 1 );
1438 connection_return( c );
1444 connection_input( Connection *conn , conn_readinfo *cri )
1452 #ifdef LDAP_CONNECTIONLESS
1459 if ( conn->c_currentber == NULL &&
1460 ( conn->c_currentber = ber_alloc()) == NULL )
1462 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1468 #ifdef LDAP_CONNECTIONLESS
1469 if ( conn->c_is_udp ) {
1470 char peername[sizeof("IP=255.255.255.255:65336")];
1472 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1473 if (len != sizeof(struct sockaddr)) return 1;
1475 sprintf( peername, "IP=%s:%d",
1476 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1477 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1478 Statslog( LDAP_DEBUG_STATS,
1479 "conn=%lu UDP request from %s (%s) accepted.\n",
1480 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1484 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1485 if ( tag != LDAP_TAG_MESSAGE ) {
1486 int err = sock_errno();
1488 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1489 /* log, close and send error */
1490 Debug( LDAP_DEBUG_TRACE,
1491 "ber_get_next on fd %d failed errno=%d (%s)\n",
1492 conn->c_sd, err, sock_errstr(err) );
1493 ber_free( conn->c_currentber, 1 );
1494 conn->c_currentber = NULL;
1501 ber = conn->c_currentber;
1502 conn->c_currentber = NULL;
1504 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1505 /* log, close and send error */
1506 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1511 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1512 /* log, close and send error */
1513 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1519 #ifdef LDAP_CONNECTIONLESS
1520 if( conn->c_is_udp ) {
1521 if( tag == LBER_OCTETSTRING ) {
1522 ber_get_stringa( ber, &cdn );
1523 tag = ber_peek_tag(ber, &len);
1525 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1526 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1533 if(tag == LDAP_REQ_BIND) {
1534 /* immediately abandon all existing operations upon BIND */
1535 connection_abandon( conn );
1539 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++, ctx );
1541 Debug( LDAP_DEBUG_TRACE, "op tag 0x%lx, time %ld\n", tag,
1542 (long) op->o_time, 0);
1545 /* clear state if the connection is being reused from inactive */
1546 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1547 memset( &conn->c_pagedresults_state, 0,
1548 sizeof( conn->c_pagedresults_state ) );
1551 op->o_res_ber = NULL;
1553 #ifdef LDAP_CONNECTIONLESS
1554 if (conn->c_is_udp) {
1556 ber_str2bv( cdn, 0, 1, &op->o_dn );
1557 op->o_protocol = LDAP_VERSION2;
1559 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1560 if (op->o_res_ber == NULL) return 1;
1562 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1563 sizeof(struct sockaddr), 0 );
1565 if (rc != sizeof(struct sockaddr)) {
1566 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1570 if (op->o_protocol == LDAP_VERSION2) {
1571 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1573 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1578 #endif /* LDAP_CONNECTIONLESS */
1582 /* Don't process requests when the conn is in the middle of a
1583 * Bind, or if it's closing. Also, don't let any single conn
1584 * use up all the available threads, and don't execute if we're
1585 * currently blocked on output. And don't execute if there are
1586 * already pending ops, let them go first. Abandon operations
1587 * get exceptions to some, but not all, cases.
1591 /* Abandon and Unbind are exempt from these checks */
1592 if (conn->c_conn_state == SLAP_C_CLOSING) {
1595 } else if (conn->c_writewaiter) {
1596 defer = "awaiting write";
1598 } else if (conn->c_n_ops_pending) {
1599 defer = "pending operations";
1603 case LDAP_REQ_ABANDON:
1604 /* Unbind is exempt from these checks */
1605 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1606 defer = "too many executing";
1608 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1613 case LDAP_REQ_UNBIND:
1618 int max = conn->c_dn.bv_len
1619 ? slap_conn_max_pending_auth
1620 : slap_conn_max_pending;
1622 Debug( LDAP_DEBUG_ANY,
1623 "connection_input: conn=%lu deferring operation: %s\n",
1624 conn->c_connid, defer, 0 );
1625 conn->c_n_ops_pending++;
1626 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1627 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1630 conn->c_n_ops_executing++;
1633 * The first op will be processed in the same thread context,
1634 * as long as there is only one op total.
1635 * Subsequent ops will be submitted to the pool by
1636 * calling connection_op_activate()
1638 if ( cri->op == NULL ) {
1639 /* the first incoming request */
1640 connection_op_queue( op );
1643 if ( !cri->nullop ) {
1645 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1646 connection_operation, (void *) cri->op );
1648 connection_op_activate( op );
1653 if ( conn->c_struct_state != SLAP_C_USED ) {
1654 /* connection must have got closed underneath us */
1659 assert( conn->c_struct_state == SLAP_C_USED );
1664 connection_resched( Connection *conn )
1668 if( conn->c_writewaiter )
1671 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1672 Debug( LDAP_DEBUG_CONNS, "connection_resched: "
1673 "attempting closing conn=%lu sd=%d\n",
1674 conn->c_connid, conn->c_sd, 0 );
1675 connection_close( conn );
1679 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1680 /* other states need different handling */
1684 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1685 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1687 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1688 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1690 /* pending operations should not be marked for abandonment */
1691 assert(!op->o_abandon);
1693 conn->c_n_ops_pending--;
1694 conn->c_n_ops_executing++;
1696 connection_op_activate( op );
1698 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1704 connection_init_log_prefix( Operation *op )
1706 if ( op->o_connid == (unsigned long)(-1) ) {
1707 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1708 "conn=-1 op=%lu", op->o_opid );
1711 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1712 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1716 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1718 op->o_conn->c_sasl_bindop = NULL;
1720 ch_free( op->o_callback );
1721 op->o_callback = NULL;
1723 return SLAP_CB_CONTINUE;
1726 static int connection_bind_cb( Operation *op, SlapReply *rs )
1728 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1729 op->o_conn->c_sasl_bind_in_progress =
1730 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1732 /* Moved here from bind.c due to ITS#4158 */
1733 op->o_conn->c_sasl_bindop = NULL;
1734 if ( op->orb_method == LDAP_AUTH_SASL ) {
1735 if( rs->sr_err == LDAP_SUCCESS ) {
1736 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1737 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1738 /* edn is always normalized already */
1739 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1741 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1742 BER_BVZERO( &op->orb_edn );
1743 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1744 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1746 op->o_conn->c_sasl_ssf = op->orb_ssf;
1747 if( op->orb_ssf > op->o_conn->c_ssf ) {
1748 op->o_conn->c_ssf = op->orb_ssf;
1751 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1752 ber_len_t max = sockbuf_max_incoming_auth;
1753 ber_sockbuf_ctrl( op->o_conn->c_sb,
1754 LBER_SB_OPT_SET_MAX_INCOMING, &max );
1757 /* log authorization identity */
1758 Statslog( LDAP_DEBUG_STATS,
1759 "%s BIND dn=\"%s\" mech=%s sasl_ssf=%d ssf=%d\n",
1761 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1762 op->o_conn->c_authmech.bv_val,
1763 op->orb_ssf, op->o_conn->c_ssf );
1765 Debug( LDAP_DEBUG_TRACE,
1766 "do_bind: SASL/%s bind: dn=\"%s\" sasl_ssf=%d\n",
1767 op->o_conn->c_authmech.bv_val,
1768 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1771 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1772 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1773 free( op->o_conn->c_sasl_bind_mech.bv_val );
1774 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1778 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1780 ch_free( op->o_callback );
1781 op->o_callback = NULL;
1783 return SLAP_CB_CONTINUE;
1786 static void connection_op_queue( Operation *op )
1788 ber_tag_t tag = op->o_tag;
1790 if (tag == LDAP_REQ_BIND) {
1791 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1792 sc->sc_response = connection_bind_cb;
1793 sc->sc_cleanup = connection_bind_cleanup_cb;
1794 sc->sc_next = op->o_callback;
1795 op->o_callback = sc;
1796 op->o_conn->c_conn_state = SLAP_C_BINDING;
1799 if (!op->o_dn.bv_len) {
1800 op->o_authz = op->o_conn->c_authz;
1801 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1802 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1803 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1805 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1806 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1810 op->o_authtype = op->o_conn->c_authtype;
1811 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1813 if (!op->o_protocol) {
1814 op->o_protocol = op->o_conn->c_protocol
1815 ? op->o_conn->c_protocol : LDAP_VERSION3;
1818 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1819 op->o_protocol > LDAP_VERSION2)
1821 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1824 op->o_connid = op->o_conn->c_connid;
1825 connection_init_log_prefix( op );
1827 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1830 static int connection_op_activate( Operation *op )
1834 connection_op_queue( op );
1836 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1837 connection_operation, (void *) op );
1840 Debug( LDAP_DEBUG_ANY,
1841 "connection_op_activate: submit failed (%d) for conn=%lu\n",
1842 rc, op->o_connid, 0 );
1843 /* should move op to pending list */
1849 int connection_write(ber_socket_t s)
1854 assert( connections != NULL );
1856 slapd_clr_write( s, 0 );
1858 c = connection_get( s );
1860 Debug( LDAP_DEBUG_ANY,
1861 "connection_write(%ld): no connection!\n",
1868 Debug( LDAP_DEBUG_TRACE,
1869 "connection_write(%d): waking output for id=%lu\n",
1870 s, c->c_connid, 0 );
1871 ldap_pvt_thread_mutex_lock( &c->c_write2_mutex );
1872 ldap_pvt_thread_cond_signal( &c->c_write2_cv );
1873 ldap_pvt_thread_mutex_unlock( &c->c_write2_mutex );
1875 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1876 slapd_set_read( s, 1 );
1878 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1879 slapd_set_write( s, 1 );
1882 /* If there are ops pending because of a writewaiter,
1885 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1886 if ( !c->c_writewaiter ) break;
1887 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
1889 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1890 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1892 /* pending operations should not be marked for abandonment */
1893 assert(!op->o_abandon);
1895 c->c_n_ops_pending--;
1896 c->c_n_ops_executing++;
1898 connection_op_activate( op );
1903 connection_return( c );
1908 typedef struct conn_fake_extblock {
1911 } conn_fake_extblock;
1914 connection_fake_destroy(
1918 Connection conn = {0};
1920 Opheader ohdr = {0};
1922 conn_fake_extblock *eb = data;
1925 op.o_hdr->oh_extensions = eb->eb_op;
1926 conn.c_extensions = eb->eb_conn;
1931 ber_memfree_x( eb, NULL );
1932 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, &op );
1933 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &conn );
1938 connection_fake_init(
1940 OperationBuffer *opbuf,
1943 connection_fake_init2( conn, opbuf, ctx, 1 );
1947 operation_fake_init(
1953 /* set memory context */
1954 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx,
1956 op->o_tmpmfuncs = &slap_sl_mfuncs;
1957 op->o_threadctx = ctx;
1958 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1960 op->o_counters = &slap_counters;
1962 op->o_connid = op->o_conn->c_connid;
1963 connection_init_log_prefix( op );
1968 connection_fake_init2(
1970 OperationBuffer *opbuf,
1974 Operation *op = (Operation *) opbuf;
1976 conn->c_connid = -1;
1977 conn->c_conn_idx = -1;
1978 conn->c_send_ldap_result = slap_send_ldap_result;
1979 conn->c_send_search_entry = slap_send_search_entry;
1980 conn->c_send_search_reference = slap_send_search_reference;
1981 conn->c_send_ldap_extended = slap_send_ldap_extended;
1982 conn->c_send_ldap_intermediate = slap_send_ldap_intermediate;
1983 conn->c_listener = (Listener *)&dummy_list;
1984 conn->c_peer_domain = slap_empty_bv;
1985 conn->c_peer_name = slap_empty_bv;
1987 memset( opbuf, 0, sizeof( *opbuf ));
1988 op->o_hdr = &opbuf->ob_hdr;
1989 op->o_controls = opbuf->ob_controls;
1991 operation_fake_init( conn, op, ctx, newmem );
1994 if ( slapi_plugins_used ) {
1995 conn_fake_extblock *eb;
1998 /* Use thread keys to make sure these eventually get cleaned up */
1999 if ( ldap_pvt_thread_pool_getkey( ctx, (void *)connection_fake_init,
2001 eb = ch_malloc( sizeof( *eb ));
2002 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
2003 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
2004 eb->eb_conn = conn->c_extensions;
2005 eb->eb_op = op->o_hdr->oh_extensions;
2006 ldap_pvt_thread_pool_setkey( ctx, (void *)connection_fake_init,
2007 eb, connection_fake_destroy, NULL, NULL );
2010 conn->c_extensions = eb->eb_conn;
2011 op->o_hdr->oh_extensions = eb->eb_op;
2014 #endif /* LDAP_SLAPI */
2016 slap_op_time( &op->o_time, &op->o_tincr );
2020 connection_assign_nextid( Connection *conn )
2022 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2023 conn->c_connid = conn_nextid++;
2024 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );