2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
33 #include <ac/socket.h>
35 #include <ac/string.h>
37 #include <ac/unistd.h>
43 #include "slapi/slapi.h"
46 /* protected by connections_mutex */
47 static ldap_pvt_thread_mutex_t connections_mutex;
48 static Connection *connections = NULL;
50 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
51 static unsigned long conn_nextid = 0;
53 static const char conn_lost_str[] = "connection lost";
55 /* structure state (protected by connections_mutex) */
56 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
57 #define SLAP_C_UNUSED 0x01
58 #define SLAP_C_USED 0x02
59 #define SLAP_C_PENDING 0x03
61 /* connection state (protected by c_mutex ) */
62 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
63 #define SLAP_C_INACTIVE 0x01 /* zero threads */
64 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
65 #define SLAP_C_BINDING 0x03 /* binding */
66 #define SLAP_C_CLOSING 0x04 /* closing */
67 #define SLAP_C_CLIENT 0x05 /* outbound client conn */
70 connection_state2str( int state )
73 case SLAP_C_INVALID: return "!";
74 case SLAP_C_INACTIVE: return "|";
75 case SLAP_C_ACTIVE: return "";
76 case SLAP_C_BINDING: return "B";
77 case SLAP_C_CLOSING: return "C";
78 case SLAP_C_CLIENT: return "L";
84 static Connection* connection_get( ber_socket_t s );
86 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
88 typedef struct conn_readinfo {
90 ldap_pvt_thread_start_t *func;
95 static int connection_input( Connection *c, conn_readinfo *cri );
97 static int connection_input( Connection *c );
99 static void connection_close( Connection *c );
101 static int connection_op_activate( Operation *op );
102 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
103 static void connection_op_queue( Operation *op );
105 static int connection_resched( Connection *conn );
106 static void connection_abandon( Connection *conn );
107 static void connection_destroy( Connection *c );
109 static ldap_pvt_thread_start_t connection_operation;
112 * Initialize connection management infrastructure.
114 int connections_init(void)
118 assert( connections == NULL );
120 if( connections != NULL) {
121 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
126 /* should check return of every call */
127 ldap_pvt_thread_mutex_init( &connections_mutex );
128 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
130 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
132 if( connections == NULL ) {
133 Debug( LDAP_DEBUG_ANY, "connections_init: "
134 "allocation (%d*%ld) of connection array failed\n",
135 dtblsize, (long) sizeof(Connection), 0 );
139 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
140 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
142 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
145 * per entry initialization of the Connection array initialization
146 * will be done by connection_init()
153 * Destroy connection management infrastructure.
156 int connections_destroy(void)
160 /* should check return of every call */
162 if( connections == NULL) {
163 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
168 for ( i = 0; i < dtblsize; i++ ) {
169 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
170 ber_sockbuf_free( connections[i].c_sb );
171 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
172 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
173 ldap_pvt_thread_cond_destroy( &connections[i].c_write_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)
229 for( c = connection_first( &connindex );
231 c = connection_next( c, &connindex ) )
233 /* Don't timeout a slow-running request or a persistent
234 * outbound connection */
235 if( c->c_n_ops_executing || c->c_conn_state == SLAP_C_CLIENT ) {
239 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
241 connection_closing( c, "idletimeout" );
242 connection_close( c );
246 connection_done( c );
251 static Connection* connection_get( ber_socket_t s )
255 Debug( LDAP_DEBUG_ARGS,
256 "connection_get(%ld)\n",
259 assert( connections != NULL );
261 if(s == AC_SOCKET_INVALID) return NULL;
264 assert( s < dtblsize );
272 ldap_pvt_thread_mutex_lock( &connections_mutex );
273 for(i=0; i<dtblsize; i++) {
274 if( connections[i].c_struct_state == SLAP_C_PENDING )
277 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
278 assert( connections[i].c_conn_state == SLAP_C_INVALID );
279 assert( connections[i].c_sb == 0 );
283 ber_sockbuf_ctrl( connections[i].c_sb,
284 LBER_SB_OPT_GET_FD, &sd );
286 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
287 assert( connections[i].c_conn_state == SLAP_C_INVALID );
288 assert( sd == AC_SOCKET_INVALID );
292 /* state can actually change from used -> unused by resched,
293 * so don't assert details here.
301 ldap_pvt_thread_mutex_unlock( &connections_mutex );
308 ldap_pvt_thread_mutex_lock( &c->c_mutex );
310 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
312 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
314 /* Avoid race condition after releasing
318 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
322 if( c->c_struct_state != SLAP_C_USED ) {
323 /* connection must have been closed due to resched */
325 assert( c->c_conn_state == SLAP_C_INVALID );
326 assert( sd == AC_SOCKET_INVALID );
328 Debug( LDAP_DEBUG_TRACE,
329 "connection_get(%d): connection not used\n",
332 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
336 Debug( LDAP_DEBUG_TRACE,
337 "connection_get(%d): got connid=%lu\n",
342 assert( c->c_struct_state == SLAP_C_USED );
343 assert( c->c_conn_state != SLAP_C_INVALID );
344 assert( sd != AC_SOCKET_INVALID );
346 #ifndef SLAPD_MONITOR
347 if ( global_idletimeout > 0 )
348 #endif /* ! SLAPD_MONITOR */
350 c->c_activitytime = slap_get_time();
357 static void connection_return( Connection *c )
359 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
362 Connection * connection_init(
366 const char* peername,
369 struct berval *authid )
375 assert( connections != NULL );
377 assert( listener != NULL );
378 assert( dnsname != NULL );
379 assert( peername != NULL );
382 assert( flags != CONN_IS_TLS );
385 if( s == AC_SOCKET_INVALID ) {
386 Debug( LDAP_DEBUG_ANY,
387 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
393 assert( s < dtblsize );
395 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
398 assert( c->c_struct_state == SLAP_C_UNUSED );
405 ldap_pvt_thread_mutex_lock( &connections_mutex );
406 for( i=0; i < dtblsize; i++) {
409 if ( connections[i].c_struct_state == SLAP_C_PENDING )
412 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
413 assert( connections[i].c_sb == 0 );
415 c->c_struct_state = SLAP_C_PENDING;
420 sd = AC_SOCKET_INVALID;
421 if (connections[i].c_sb != NULL) {
422 ber_sockbuf_ctrl( connections[i].c_sb,
423 LBER_SB_OPT_GET_FD, &sd );
426 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
427 assert( sd == AC_SOCKET_INVALID );
429 c->c_struct_state = SLAP_C_PENDING;
433 if( connections[i].c_conn_state == SLAP_C_CLIENT ) continue;
435 assert( connections[i].c_struct_state == SLAP_C_USED );
436 assert( connections[i].c_conn_state != SLAP_C_INVALID );
437 assert( sd != AC_SOCKET_INVALID );
439 ldap_pvt_thread_mutex_unlock( &connections_mutex );
442 Debug( LDAP_DEBUG_ANY,
443 "connection_init(%d): connection table full "
444 "(%d/%d)\n", s, i, dtblsize);
451 c->c_send_ldap_result = slap_send_ldap_result;
452 c->c_send_search_entry = slap_send_search_entry;
453 c->c_send_search_reference = slap_send_search_reference;
454 c->c_send_ldap_extended = slap_send_ldap_extended;
455 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
457 BER_BVZERO( &c->c_authmech );
458 BER_BVZERO( &c->c_dn );
459 BER_BVZERO( &c->c_ndn );
461 c->c_listener = NULL;
462 BER_BVZERO( &c->c_peer_domain );
463 BER_BVZERO( &c->c_peer_name );
465 LDAP_STAILQ_INIT(&c->c_ops);
466 LDAP_STAILQ_INIT(&c->c_pending_ops);
469 c->c_txn = CONN_TXN_INACTIVE;
470 c->c_txn_backend = NULL;
471 LDAP_STAILQ_INIT(&c->c_txn_ops);
474 BER_BVZERO( &c->c_sasl_bind_mech );
476 c->c_sasl_authctx = NULL;
477 c->c_sasl_sockctx = NULL;
478 c->c_sasl_extra = NULL;
479 c->c_sasl_bindop = NULL;
481 c->c_sb = ber_sockbuf_alloc( );
484 ber_len_t max = sockbuf_max_incoming;
485 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
488 c->c_currentber = NULL;
490 /* should check status of thread calls */
491 ldap_pvt_thread_mutex_init( &c->c_mutex );
492 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
493 ldap_pvt_thread_cond_init( &c->c_write_cv );
496 if ( slapi_plugins_used ) {
497 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
502 ldap_pvt_thread_mutex_lock( &c->c_mutex );
504 assert( BER_BVISNULL( &c->c_authmech ) );
505 assert( BER_BVISNULL( &c->c_dn ) );
506 assert( BER_BVISNULL( &c->c_ndn ) );
507 assert( c->c_listener == NULL );
508 assert( BER_BVISNULL( &c->c_peer_domain ) );
509 assert( BER_BVISNULL( &c->c_peer_name ) );
510 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
511 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
513 assert( c->c_txn == CONN_TXN_INACTIVE );
514 assert( c->c_txn_backend == NULL );
515 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
517 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
518 assert( c->c_sasl_done == 0 );
519 assert( c->c_sasl_authctx == NULL );
520 assert( c->c_sasl_sockctx == NULL );
521 assert( c->c_sasl_extra == NULL );
522 assert( c->c_sasl_bindop == NULL );
523 assert( c->c_currentber == NULL );
524 assert( c->c_writewaiter == 0);
526 c->c_listener = listener;
528 if ( flags == CONN_IS_CLIENT ) {
530 c->c_conn_state = SLAP_C_CLIENT;
531 c->c_struct_state = SLAP_C_USED;
532 c->c_close_reason = "?"; /* should never be needed */
533 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
534 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
539 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
540 ber_str2bv( peername, 0, 1, &c->c_peer_name );
542 c->c_n_ops_received = 0;
543 c->c_n_ops_executing = 0;
544 c->c_n_ops_pending = 0;
545 c->c_n_ops_completed = 0;
551 /* set to zero until bind, implies LDAP_VERSION3 */
554 #ifndef SLAPD_MONITOR
555 if ( global_idletimeout > 0 )
556 #endif /* ! SLAPD_MONITOR */
558 c->c_activitytime = c->c_starttime = slap_get_time();
561 #ifdef LDAP_CONNECTIONLESS
563 if( flags == CONN_IS_UDP ) {
566 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
567 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
569 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
570 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
571 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
572 LBER_SBIOD_LEVEL_PROVIDER, NULL );
577 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
578 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
580 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
581 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
585 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
586 INT_MAX, (void*)"ldap_" );
589 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
590 c /* non-NULL */ ) < 0 )
592 Debug( LDAP_DEBUG_ANY,
593 "connection_init(%d, %s): set nonblocking failed\n",
594 s, c->c_peer_name.bv_val, 0 );
597 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
598 id = c->c_connid = conn_nextid++;
599 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
601 c->c_conn_state = SLAP_C_INACTIVE;
602 c->c_struct_state = SLAP_C_USED;
603 c->c_close_reason = "?"; /* should never be needed */
605 c->c_ssf = c->c_transport_ssf = ssf;
609 if ( flags == CONN_IS_TLS ) {
611 c->c_needs_tls_accept = 1;
614 c->c_needs_tls_accept = 0;
618 slap_sasl_open( c, 0 );
619 slap_sasl_external( c, ssf, authid );
621 slapd_add_internal( s, 1 );
622 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
624 backend_connection_init(c);
629 void connection2anonymous( Connection *c )
631 assert( connections != NULL );
635 ber_len_t max = sockbuf_max_incoming;
636 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
639 if ( !BER_BVISNULL( &c->c_authmech ) ) {
640 ch_free(c->c_authmech.bv_val);
642 BER_BVZERO( &c->c_authmech );
644 if ( !BER_BVISNULL( &c->c_dn ) ) {
645 ch_free(c->c_dn.bv_val);
647 BER_BVZERO( &c->c_dn );
649 if ( !BER_BVISNULL( &c->c_ndn ) ) {
650 ch_free(c->c_ndn.bv_val);
652 BER_BVZERO( &c->c_ndn );
654 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
655 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
657 BER_BVZERO( &c->c_sasl_authz_dn );
659 c->c_authz_backend = NULL;
663 connection_destroy( Connection *c )
666 unsigned long connid;
667 const char *close_reason;
670 assert( connections != NULL );
672 assert( c->c_struct_state != SLAP_C_UNUSED );
673 assert( c->c_conn_state != SLAP_C_INVALID );
674 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
675 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
677 assert( c->c_txn == CONN_TXN_INACTIVE );
678 assert( c->c_txn_backend == NULL );
679 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
681 assert( c->c_writewaiter == 0);
683 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
684 connid = c->c_connid;
685 close_reason = c->c_close_reason;
687 ldap_pvt_thread_mutex_lock( &connections_mutex );
688 c->c_struct_state = SLAP_C_PENDING;
689 ldap_pvt_thread_mutex_unlock( &connections_mutex );
691 backend_connection_destroy(c);
696 c->c_activitytime = c->c_starttime = 0;
698 connection2anonymous( c );
699 c->c_listener = NULL;
701 if(c->c_peer_domain.bv_val != NULL) {
702 free(c->c_peer_domain.bv_val);
704 BER_BVZERO( &c->c_peer_domain );
705 if(c->c_peer_name.bv_val != NULL) {
706 free(c->c_peer_name.bv_val);
708 BER_BVZERO( &c->c_peer_name );
710 c->c_sasl_bind_in_progress = 0;
711 if(c->c_sasl_bind_mech.bv_val != NULL) {
712 free(c->c_sasl_bind_mech.bv_val);
714 BER_BVZERO( &c->c_sasl_bind_mech );
716 slap_sasl_close( c );
718 if ( c->c_currentber != NULL ) {
719 ber_free( c->c_currentber, 1 );
720 c->c_currentber = NULL;
725 /* call destructors, then constructors; avoids unnecessary allocation */
726 if ( slapi_plugins_used ) {
727 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
731 c->c_conn_state = SLAP_C_INVALID;
732 c->c_struct_state = SLAP_C_UNUSED;
733 c->c_close_reason = "?"; /* should never be needed */
736 c->c_sb = ber_sockbuf_alloc( );
738 ber_len_t max = sockbuf_max_incoming;
739 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
742 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
744 /* c must be fully reset by this point; when we call slapd_remove
745 * it may get immediately reused by a new connection.
747 if ( sd != AC_SOCKET_INVALID ) {
748 slapd_remove( sd, sb, 1, 0, 0 );
750 if ( close_reason == NULL ) {
751 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
752 connid, (long) sd, 0, 0, 0 );
754 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
755 connid, (long) sd, close_reason, 0, 0 );
760 int connection_state_closing( Connection *c )
762 /* c_mutex must be locked by caller */
766 assert( c->c_struct_state == SLAP_C_USED );
768 state = c->c_conn_state;
770 assert( state != SLAP_C_INVALID );
772 return state == SLAP_C_CLOSING;
775 static void connection_abandon( Connection *c )
777 /* c_mutex must be locked by caller */
779 Operation *o, *next, op = {0};
785 op.o_connid = c->c_connid;
786 op.o_tag = LDAP_REQ_ABANDON;
788 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
789 next = LDAP_STAILQ_NEXT( o, o_next );
790 op.orn_msgid = o->o_msgid;
792 op.o_bd = frontendDB;
793 frontendDB->be_abandon( &op, &rs );
797 /* remove operations in pending transaction */
798 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
799 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
800 LDAP_STAILQ_NEXT(o, o_next) = NULL;
804 /* clear transaction */
805 c->c_txn_backend = NULL;
806 c->c_txn = CONN_TXN_INACTIVE;
809 /* remove pending operations */
810 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
811 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
812 LDAP_STAILQ_NEXT(o, o_next) = NULL;
817 void connection_closing( Connection *c, const char *why )
819 assert( connections != NULL );
821 assert( c->c_struct_state == SLAP_C_USED );
822 assert( c->c_conn_state != SLAP_C_INVALID );
824 /* c_mutex must be locked by caller */
826 if( c->c_conn_state != SLAP_C_CLOSING ) {
829 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
830 Debug( LDAP_DEBUG_TRACE,
831 "connection_closing: readying conn=%lu sd=%d for close\n",
832 c->c_connid, sd, 0 );
833 /* update state to closing */
834 c->c_conn_state = SLAP_C_CLOSING;
835 c->c_close_reason = why;
837 /* don't listen on this port anymore */
838 slapd_clr_read( sd, 0 );
840 /* abandon active operations */
841 connection_abandon( c );
843 /* wake write blocked operations */
844 if ( c->c_writewaiter ) {
845 ldap_pvt_thread_cond_signal( &c->c_write_cv );
846 /* ITS#4667 this may allow another thread to drop into
847 * connection_resched / connection_close before we
848 * finish, but that's OK.
850 slapd_clr_write( sd, 1 );
851 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
852 ldap_pvt_thread_mutex_lock( &c->c_write_mutex );
853 ldap_pvt_thread_mutex_lock( &c->c_mutex );
854 ldap_pvt_thread_mutex_unlock( &c->c_write_mutex );
856 slapd_clr_write( sd, 1 );
859 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
860 /* Client closed connection after doing Unbind. */
861 c->c_close_reason = NULL;
866 connection_close( Connection *c )
868 ber_socket_t sd = AC_SOCKET_INVALID;
870 assert( connections != NULL );
873 /* ITS#4667 we may have gotten here twice */
874 if ( c->c_conn_state == SLAP_C_INVALID )
877 assert( c->c_struct_state == SLAP_C_USED );
878 assert( c->c_conn_state == SLAP_C_CLOSING );
880 /* NOTE: c_mutex should be locked by caller */
882 /* NOTE: don't get the file descriptor if not needed */
883 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
884 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
887 if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
888 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
890 Debug( LDAP_DEBUG_TRACE,
891 "connection_close: deferring conn=%lu sd=%d\n",
892 c->c_connid, sd, 0 );
896 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
897 c->c_connid, sd, 0 );
899 connection_destroy( c );
902 unsigned long connections_nextid(void)
905 assert( connections != NULL );
907 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
911 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
916 Connection* connection_first( ber_socket_t *index )
918 assert( connections != NULL );
919 assert( index != NULL );
921 ldap_pvt_thread_mutex_lock( &connections_mutex );
922 for( *index = 0; *index < dtblsize; (*index)++) {
923 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
927 ldap_pvt_thread_mutex_unlock( &connections_mutex );
929 return connection_next(NULL, index);
932 Connection* connection_next( Connection *c, ber_socket_t *index )
934 assert( connections != NULL );
935 assert( index != NULL );
936 assert( *index <= dtblsize );
938 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
942 ldap_pvt_thread_mutex_lock( &connections_mutex );
943 for(; *index < dtblsize; (*index)++) {
945 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
946 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
954 if( connections[*index].c_struct_state == SLAP_C_USED ) {
955 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
956 c = &connections[(*index)++];
957 if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
959 ldap_pvt_thread_mutex_unlock( &connections_mutex );
960 ldap_pvt_thread_mutex_lock( &c->c_mutex );
961 ldap_pvt_thread_mutex_lock( &connections_mutex );
962 if ( c->c_struct_state != SLAP_C_USED ) {
963 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
971 c_struct = connections[*index].c_struct_state;
972 if ( c_struct == SLAP_C_PENDING )
974 assert( c_struct == SLAP_C_UNUSED );
975 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
978 ldap_pvt_thread_mutex_unlock( &connections_mutex );
982 void connection_done( Connection *c )
984 assert( connections != NULL );
986 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
990 * connection_activity - handle the request operation op on connection
991 * conn. This routine figures out what kind of operation it is and
992 * calls the appropriate stub to handle it.
996 /* FIXME: returns 0 in case of failure */
997 #define INCR_OP_INITIATED(index) \
999 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1000 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
1001 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1003 #define INCR_OP_COMPLETED(index) \
1005 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1006 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1007 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
1008 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1010 #else /* !SLAPD_MONITOR */
1011 #define INCR_OP_INITIATED(index) do { } while (0)
1012 #define INCR_OP_COMPLETED(index) \
1014 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1015 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1016 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1018 #endif /* !SLAPD_MONITOR */
1021 * NOTE: keep in sync with enum in slapd.h
1023 static BI_op_func *opfun[] = {
1038 connection_operation( void *ctx, void *arg_v )
1040 int rc = LDAP_OTHER;
1041 Operation *op = arg_v;
1042 SlapReply rs = {REP_RESULT};
1043 ber_tag_t tag = op->o_tag;
1044 slap_op_t opidx = SLAP_OP_LAST;
1045 Connection *conn = op->o_conn;
1046 void *memctx = NULL;
1047 void *memctx_null = NULL;
1050 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
1051 /* FIXME: returns 0 in case of failure */
1052 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
1053 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1055 op->o_threadctx = ctx;
1056 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1060 case LDAP_REQ_UNBIND:
1062 case LDAP_REQ_DELETE:
1063 case LDAP_REQ_MODDN:
1064 case LDAP_REQ_MODIFY:
1065 case LDAP_REQ_COMPARE:
1066 case LDAP_REQ_SEARCH:
1067 case LDAP_REQ_ABANDON:
1068 case LDAP_REQ_EXTENDED:
1071 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1072 "conn %lu unknown LDAP request 0x%lx\n",
1073 conn->c_connid, tag, 0 );
1074 op->o_tag = LBER_ERROR;
1075 rs.sr_err = LDAP_PROTOCOL_ERROR;
1076 rs.sr_text = "unknown LDAP request";
1077 send_ldap_disconnect( op, &rs );
1078 rc = SLAPD_DISCONNECT;
1079 goto operations_error;
1082 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1083 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1084 "error: SASL bind in progress (tag=%ld).\n",
1086 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1087 "SASL bind in progress" );
1088 rc = LDAP_OPERATIONS_ERROR;
1089 goto operations_error;
1093 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1094 ( tag == LDAP_REQ_ADD ) ||
1095 ( tag == LDAP_REQ_DELETE ) ||
1096 ( tag == LDAP_REQ_MODIFY ) ||
1097 ( tag == LDAP_REQ_MODRDN )))
1099 /* Disable SLAB allocator for all update operations
1100 issued inside of a transaction */
1101 op->o_tmpmemctx = NULL;
1102 op->o_tmpmfuncs = &ch_mfuncs;
1106 /* We can use Thread-Local storage for most mallocs. We can
1107 * also use TL for ber parsing, but not on Add or Modify.
1110 memsiz = ber_len( op->o_ber ) * 64;
1111 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1113 memsiz = SLAP_SLAB_SIZE;
1115 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx, 1 );
1116 op->o_tmpmemctx = memctx;
1117 op->o_tmpmfuncs = &slap_sl_mfuncs;
1118 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1119 /* Note - the ber and its buffer are already allocated from
1120 * regular memory; this only affects subsequent mallocs that
1121 * ber_scanf may invoke.
1123 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1127 opidx = slap_req2op( tag );
1128 assert( opidx != SLAP_OP_LAST );
1129 INCR_OP_INITIATED( opidx );
1130 rc = (*(opfun[opidx]))( op, &rs );
1133 if ( rc == SLAPD_DISCONNECT ) {
1136 } else if ( opidx != SLAP_OP_LAST ) {
1137 /* increment completed operations count
1138 * only if operation was initiated
1139 * and rc != SLAPD_DISCONNECT */
1140 INCR_OP_COMPLETED( opidx );
1143 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1144 if ( rc == SLAPD_ABANDON ) {
1145 op->o_cancel = SLAP_CANCEL_ACK;
1147 op->o_cancel = LDAP_TOO_LATE;
1151 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1152 op->o_cancel != SLAP_CANCEL_DONE )
1154 ldap_pvt_thread_yield();
1157 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1159 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1161 LDAP_STAILQ_REMOVE( &conn->c_ops, op, Operation, o_next);
1162 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1164 conn->c_n_ops_executing--;
1165 conn->c_n_ops_completed++;
1169 case LDAP_REQ_UNBIND:
1170 /* c_mutex is locked */
1171 connection_closing( conn,
1172 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1176 connection_resched( conn );
1177 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1181 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1183 int connection_client_setup(
1185 ldap_pvt_thread_start_t *func,
1191 c = connection_init( s, (Listener *)&dummy_list, "", "",
1192 CONN_IS_CLIENT, 0, NULL );
1193 if ( !c ) return -1;
1195 c->c_clientfunc = func;
1196 c->c_clientarg = arg;
1198 slapd_add_internal( s, 0 );
1199 slapd_set_read( s, 1 );
1203 void connection_client_enable(
1206 slapd_set_read( s, 1 );
1209 void connection_client_stop(
1215 /* get (locked) connection */
1216 c = connection_get( s );
1218 assert( c->c_conn_state == SLAP_C_CLIENT );
1220 c->c_listener = NULL;
1221 c->c_conn_state = SLAP_C_INVALID;
1222 c->c_struct_state = SLAP_C_UNUSED;
1223 c->c_close_reason = "?"; /* should never be needed */
1225 c->c_sb = ber_sockbuf_alloc( );
1227 ber_len_t max = sockbuf_max_incoming;
1228 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1230 slapd_remove( s, sb, 0, 1, 0 );
1232 connection_return( c );
1235 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1237 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1239 static void* connection_read_thread( void* ctx, void* argv )
1242 conn_readinfo cri = { NULL, NULL, NULL, 0 };
1243 ber_socket_t s = (long)argv;
1246 * read incoming LDAP requests. If there is more than one,
1247 * the first one is returned with new_op
1249 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1250 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1251 return (void*)(long)rc;
1254 /* execute a single queued request in the same thread */
1255 if( cri.op && !cri.nullop ) {
1256 rc = (long)connection_operation( ctx, cri.op );
1257 } else if ( cri.func ) {
1258 rc = (long)cri.func( ctx, cri.arg );
1261 return (void*)(long)rc;
1264 int connection_read_activate( ber_socket_t s )
1269 * suspend reading on this file descriptor until a connection processing
1270 * thread reads data on it. Otherwise the listener thread will repeatedly
1271 * submit the same event on it to the pool.
1273 rc = slapd_clr_read( s, 0 );
1277 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1278 connection_read_thread, (void *)(long)s );
1281 Debug( LDAP_DEBUG_ANY,
1282 "connection_read_activate(%d): submit failed (%d)\n",
1290 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1292 connection_read( ber_socket_t s, conn_readinfo *cri )
1294 int connection_read(ber_socket_t s)
1300 assert( connections != NULL );
1302 /* get (locked) connection */
1303 c = connection_get( s );
1306 Debug( LDAP_DEBUG_ANY,
1307 "connection_read(%ld): no connection!\n",
1315 if( c->c_conn_state == SLAP_C_CLOSING ) {
1316 Debug( LDAP_DEBUG_TRACE,
1317 "connection_read(%d): closing, ignoring input for id=%lu\n",
1318 s, c->c_connid, 0 );
1319 connection_return( c );
1323 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1324 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1325 cri->func = c->c_clientfunc;
1326 cri->arg = c->c_clientarg;
1327 /* read should already be cleared */
1329 slapd_clr_read( s, 0 );
1330 ldap_pvt_thread_pool_submit( &connection_pool,
1331 c->c_clientfunc, c->c_clientarg );
1333 connection_return( c );
1337 Debug( LDAP_DEBUG_TRACE,
1338 "connection_read(%d): checking for input on id=%lu\n",
1339 s, c->c_connid, 0 );
1342 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1343 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1345 Debug( LDAP_DEBUG_TRACE,
1346 "connection_read(%d): TLS accept failure "
1347 "error=%d id=%lu, closing\n",
1348 s, rc, c->c_connid );
1350 c->c_needs_tls_accept = 0;
1351 /* c_mutex is locked */
1352 connection_closing( c, "TLS negotiation failure" );
1353 connection_close( c );
1354 connection_return( c );
1357 } else if ( rc == 0 ) {
1359 struct berval authid = BER_BVNULL;
1361 c->c_needs_tls_accept = 0;
1363 /* we need to let SASL know */
1364 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1366 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1367 if( c->c_tls_ssf > c->c_ssf ) {
1368 c->c_ssf = c->c_tls_ssf;
1371 rc = dnX509peerNormalize( ssl, &authid );
1372 if ( rc != LDAP_SUCCESS ) {
1373 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1374 "unable to get TLS client DN, error=%d id=%lu\n",
1375 s, rc, c->c_connid );
1377 Statslog( LDAP_DEBUG_STATS,
1378 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1379 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1380 slap_sasl_external( c, c->c_tls_ssf, &authid );
1381 if ( authid.bv_val ) free( authid.bv_val );
1384 /* if success and data is ready, fall thru to data input loop */
1385 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1387 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1388 slapd_set_read( s, 1 );
1391 connection_return( c );
1397 #ifdef HAVE_CYRUS_SASL
1398 if ( c->c_sasl_layers ) {
1399 /* If previous layer is not removed yet, give up for now */
1400 if ( !c->c_sasl_sockctx ) {
1401 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1402 slapd_set_read( s, 1 );
1405 connection_return( c );
1409 c->c_sasl_layers = 0;
1411 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1412 if( rc != LDAP_SUCCESS ) {
1413 Debug( LDAP_DEBUG_TRACE,
1414 "connection_read(%d): SASL install error "
1415 "error=%d id=%lu, closing\n",
1416 s, rc, c->c_connid );
1418 /* c_mutex is locked */
1419 connection_closing( c, "SASL layer install failure" );
1420 connection_close( c );
1421 connection_return( c );
1427 #define CONNECTION_INPUT_LOOP 1
1428 /* #define DATA_READY_LOOP 1 */
1431 /* How do we do this without getting into a busy loop ? */
1432 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1433 rc = connection_input( c, cri );
1435 rc = connection_input( c );
1438 #ifdef DATA_READY_LOOP
1439 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1440 #elif CONNECTION_INPUT_LOOP
1447 Debug( LDAP_DEBUG_CONNS,
1448 "connection_read(%d): input error=%d id=%lu, closing.\n",
1449 s, rc, c->c_connid );
1451 /* c_mutex is locked */
1452 connection_closing( c, conn_lost_str );
1453 connection_close( c );
1454 connection_return( c );
1458 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1459 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1460 slapd_set_write( s, 0 );
1463 slapd_set_read( s, 1 );
1465 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1466 slapd_set_read( s, 1 );
1469 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1470 slapd_set_write( s, 1 );
1474 connection_return( c );
1480 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1481 connection_input( Connection *conn , conn_readinfo *cri )
1483 connection_input( Connection *conn )
1492 #ifdef LDAP_CONNECTIONLESS
1498 if ( conn->c_currentber == NULL &&
1499 ( conn->c_currentber = ber_alloc()) == NULL )
1501 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1507 #ifdef LDAP_CONNECTIONLESS
1508 if ( conn->c_is_udp ) {
1509 char peername[sizeof("IP=255.255.255.255:65336")];
1511 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1512 if (len != sizeof(struct sockaddr)) return 1;
1514 sprintf( peername, "IP=%s:%d",
1515 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1516 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1517 Statslog( LDAP_DEBUG_STATS,
1518 "conn=%lu UDP request from %s (%s) accepted.\n",
1519 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1523 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1524 if ( tag != LDAP_TAG_MESSAGE ) {
1525 int err = sock_errno();
1528 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1530 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1531 /* log, close and send error */
1532 Debug( LDAP_DEBUG_TRACE,
1533 "ber_get_next on fd %d failed errno=%d (%s)\n",
1534 sd, err, sock_errstr(err) );
1535 ber_free( conn->c_currentber, 1 );
1536 conn->c_currentber = NULL;
1543 ber = conn->c_currentber;
1544 conn->c_currentber = NULL;
1546 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1547 /* log, close and send error */
1548 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1553 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1554 /* log, close and send error */
1555 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1561 #ifdef LDAP_CONNECTIONLESS
1562 if( conn->c_is_udp ) {
1563 if( tag == LBER_OCTETSTRING ) {
1564 ber_get_stringa( ber, &cdn );
1565 tag = ber_peek_tag(ber, &len);
1567 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1568 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1575 if(tag == LDAP_REQ_BIND) {
1576 /* immediately abandon all existing operations upon BIND */
1577 connection_abandon( conn );
1580 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1583 /* clear state if the connection is being reused from inactive */
1584 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1585 memset( &conn->c_pagedresults_state, 0,
1586 sizeof( conn->c_pagedresults_state ) );
1589 op->o_res_ber = NULL;
1591 #ifdef LDAP_CONNECTIONLESS
1592 if (conn->c_is_udp) {
1594 ber_str2bv( cdn, 0, 1, &op->o_dn );
1595 op->o_protocol = LDAP_VERSION2;
1597 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1598 if (op->o_res_ber == NULL) return 1;
1600 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1601 sizeof(struct sockaddr), 0 );
1603 if (rc != sizeof(struct sockaddr)) {
1604 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1608 if (op->o_protocol == LDAP_VERSION2) {
1609 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1611 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1616 #endif /* LDAP_CONNECTIONLESS */
1620 /* Don't process requests when the conn is in the middle of a
1621 * Bind, or if it's closing. Also, don't let any single conn
1622 * use up all the available threads, and don't execute if we're
1623 * currently blocked on output. And don't execute if there are
1624 * already pending ops, let them go first. Abandon operations
1625 * get exceptions to some, but not all, cases.
1629 /* Abandon and Unbind are exempt from these checks */
1630 if (conn->c_conn_state == SLAP_C_CLOSING) {
1633 } else if (conn->c_writewaiter) {
1634 defer = "awaiting write";
1636 } else if (conn->c_n_ops_pending) {
1637 defer = "pending operations";
1641 case LDAP_REQ_ABANDON:
1642 /* Unbind is exempt from these checks */
1643 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1644 defer = "too many executing";
1646 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1651 case LDAP_REQ_UNBIND:
1656 int max = conn->c_dn.bv_len
1657 ? slap_conn_max_pending_auth
1658 : slap_conn_max_pending;
1660 Debug( LDAP_DEBUG_ANY,
1661 "connection_input: conn=%lu deferring operation: %s\n",
1662 conn->c_connid, defer, 0 );
1663 conn->c_n_ops_pending++;
1664 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1665 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1668 conn->c_n_ops_executing++;
1670 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1672 * The first op will be processed in the same thread context,
1673 * as long as there is only one op total.
1674 * Subsequent ops will be submitted to the pool by
1675 * calling connection_op_activate()
1677 if ( cri->op == NULL ) {
1678 /* the first incoming request */
1679 connection_op_queue( op );
1682 if ( !cri->nullop ) {
1684 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1685 connection_operation, (void *) cri->op );
1687 connection_op_activate( op );
1690 connection_op_activate( op );
1695 if ( conn->c_struct_state != SLAP_C_USED ) {
1696 /* connection must have got closed underneath us */
1701 assert( conn->c_struct_state == SLAP_C_USED );
1706 connection_resched( Connection *conn )
1710 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1712 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1714 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1715 "attempting closing conn=%lu sd=%d\n",
1716 conn->c_connid, sd, 0 );
1717 connection_close( conn );
1721 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1722 /* other states need different handling */
1726 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1727 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1729 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1730 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1732 /* pending operations should not be marked for abandonment */
1733 assert(!op->o_abandon);
1735 conn->c_n_ops_pending--;
1736 conn->c_n_ops_executing++;
1738 connection_op_activate( op );
1740 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1746 connection_init_log_prefix( Operation *op )
1748 if ( op->o_connid == (unsigned long)(-1) ) {
1749 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1750 "conn=-1 op=%lu", op->o_opid );
1753 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1754 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1758 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1760 op->o_conn->c_sasl_bindop = NULL;
1762 ch_free( op->o_callback );
1763 op->o_callback = NULL;
1765 return SLAP_CB_CONTINUE;
1768 static int connection_bind_cb( Operation *op, SlapReply *rs )
1770 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1771 if ( op->o_conn->c_conn_state == SLAP_C_BINDING )
1772 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1773 op->o_conn->c_sasl_bind_in_progress =
1774 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1776 /* Moved here from bind.c due to ITS#4158 */
1777 op->o_conn->c_sasl_bindop = NULL;
1778 if ( op->orb_method == LDAP_AUTH_SASL ) {
1779 if( rs->sr_err == LDAP_SUCCESS ) {
1780 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1781 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1782 /* edn is always normalized already */
1783 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1785 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1786 BER_BVZERO( &op->orb_edn );
1787 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1788 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1790 op->o_conn->c_sasl_ssf = op->orb_ssf;
1791 if( op->orb_ssf > op->o_conn->c_ssf ) {
1792 op->o_conn->c_ssf = op->orb_ssf;
1795 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1796 ber_len_t max = sockbuf_max_incoming_auth;
1797 ber_sockbuf_ctrl( op->o_conn->c_sb,
1798 LBER_SB_OPT_SET_MAX_INCOMING, &max );
1801 /* log authorization identity */
1802 Statslog( LDAP_DEBUG_STATS,
1803 "%s BIND dn=\"%s\" mech=%s ssf=%d\n",
1805 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1806 op->o_conn->c_authmech.bv_val, op->orb_ssf, 0 );
1808 Debug( LDAP_DEBUG_TRACE,
1809 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
1810 op->o_conn->c_authmech.bv_val,
1811 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1814 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1815 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1816 free( op->o_conn->c_sasl_bind_mech.bv_val );
1817 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1821 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1823 ch_free( op->o_callback );
1824 op->o_callback = NULL;
1826 return SLAP_CB_CONTINUE;
1829 static void connection_op_queue( Operation *op )
1831 ber_tag_t tag = op->o_tag;
1833 if (tag == LDAP_REQ_BIND) {
1834 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1835 sc->sc_response = connection_bind_cb;
1836 sc->sc_cleanup = connection_bind_cleanup_cb;
1837 sc->sc_next = op->o_callback;
1838 op->o_callback = sc;
1839 op->o_conn->c_conn_state = SLAP_C_BINDING;
1842 if (!op->o_dn.bv_len) {
1843 op->o_authz = op->o_conn->c_authz;
1844 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1845 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1846 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1848 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1849 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1853 op->o_authtype = op->o_conn->c_authtype;
1854 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1856 if (!op->o_protocol) {
1857 op->o_protocol = op->o_conn->c_protocol
1858 ? op->o_conn->c_protocol : LDAP_VERSION3;
1861 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1862 op->o_protocol > LDAP_VERSION2)
1864 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1867 op->o_connid = op->o_conn->c_connid;
1868 connection_init_log_prefix( op );
1870 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1873 static int connection_op_activate( Operation *op )
1877 connection_op_queue( op );
1879 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1880 connection_operation, (void *) op );
1883 Debug( LDAP_DEBUG_ANY,
1884 "connection_op_activate: submit failed (%d) for conn=%lu\n",
1885 rc, op->o_connid, 0 );
1886 /* should move op to pending list */
1892 int connection_write(ber_socket_t s)
1897 assert( connections != NULL );
1899 slapd_clr_write( s, 0 );
1901 c = connection_get( s );
1903 Debug( LDAP_DEBUG_ANY,
1904 "connection_write(%ld): no connection!\n",
1911 Debug( LDAP_DEBUG_TRACE,
1912 "connection_write(%d): waking output for id=%lu\n",
1913 s, c->c_connid, 0 );
1914 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1916 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1917 slapd_set_read( s, 1 );
1919 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1920 slapd_set_write( s, 1 );
1923 /* If there are ops pending because of a writewaiter,
1926 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1927 if ( !c->c_writewaiter ) break;
1928 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
1930 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1931 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1933 /* pending operations should not be marked for abandonment */
1934 assert(!op->o_abandon);
1936 c->c_n_ops_pending--;
1937 c->c_n_ops_executing++;
1939 connection_op_activate( op );
1944 connection_return( c );
1949 typedef struct conn_fake_extblock {
1952 } conn_fake_extblock;
1955 connection_fake_destroy(
1959 Connection conn = {0};
1961 Opheader ohdr = {0};
1963 conn_fake_extblock *eb = data;
1966 op.o_hdr->oh_extensions = eb->eb_op;
1967 conn.c_extensions = eb->eb_conn;
1972 ber_memfree_x( eb, NULL );
1973 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, &op );
1974 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &conn );
1979 connection_fake_init(
1984 connection_fake_init2( conn, op, ctx, 1 );
1988 connection_fake_init2(
1994 conn->c_connid = -1;
1995 conn->c_send_ldap_result = slap_send_ldap_result;
1996 conn->c_send_search_entry = slap_send_search_entry;
1997 conn->c_send_search_reference = slap_send_search_reference;
1998 conn->c_listener = (Listener *)&dummy_list;
1999 conn->c_peer_domain = slap_empty_bv;
2000 conn->c_peer_name = slap_empty_bv;
2002 memset(op, 0, OPERATION_BUFFER_SIZE);
2003 op->o_hdr = (Opheader *)(op+1);
2004 op->o_controls = (void **)(op->o_hdr+1);
2005 /* set memory context */
2006 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx,
2008 op->o_tmpmfuncs = &slap_sl_mfuncs;
2009 op->o_threadctx = ctx;
2010 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
2013 op->o_connid = op->o_conn->c_connid;
2014 connection_init_log_prefix( op );
2017 if ( slapi_plugins_used ) {
2018 conn_fake_extblock *eb = NULL;
2020 /* Use thread keys to make sure these eventually get cleaned up */
2021 if ( ldap_pvt_thread_pool_getkey( ctx, connection_fake_init, &eb,
2023 eb = ch_malloc( sizeof( *eb ));
2024 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
2025 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
2026 eb->eb_conn = conn->c_extensions;
2027 eb->eb_op = op->o_hdr->oh_extensions;
2028 ldap_pvt_thread_pool_setkey( ctx, connection_fake_init, eb,
2029 connection_fake_destroy );
2031 conn->c_extensions = eb->eb_conn;
2032 op->o_hdr->oh_extensions = eb->eb_op;
2035 #endif /* LDAP_SLAPI */
2037 slap_op_time( &op->o_time, &op->o_tincr );
2041 connection_assign_nextid( Connection *conn )
2043 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2044 conn->c_connid = conn_nextid++;
2045 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );